-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeGen.c
231 lines (193 loc) · 3.71 KB
/
CodeGen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include "CodeGen.h"
extern FILE *aFile;
int NextLabel = 1;
int NextWord = 1;
struct TmpReg
{
unsigned char Free;
unsigned char Used;
char *Name;
} Registers[10] = {{1, 0, "$t0"}, {1, 0, "$t1"}, {1, 0, "$t2"}, {1, 0, "$t3"}, {1, 0, "$t4"}, {1, 0, "$t5"}, {1, 0, "$t6"}, {1, 0, "$t7"}, {1, 0, "$t8"}, {1, 0, "$t9"}};
#define MAXREG 10
char Buf[16];
char *
CopyStr(char *AStr)
{
return (AStr) ? strdup(AStr) : NULL;
}
struct InstrSeq *
GenInstr(char *Label, char *OpCode, char *Oprnd1, char *Oprnd2, char *Oprnd3)
{
struct InstrSeq *instr;
instr = (struct InstrSeq *)malloc(sizeof(struct InstrSeq));
instr->Label = CopyStr(Label);
instr->OpCode = CopyStr(OpCode);
instr->Oprnd1 = CopyStr(Oprnd1);
instr->Oprnd2 = CopyStr(Oprnd2);
instr->Oprnd3 = CopyStr(Oprnd3);
instr->Next = NULL;
return instr;
}
extern struct InstrSeq *
AppendSeq(struct InstrSeq *Seq1, struct InstrSeq *Seq2)
{
struct InstrSeq *instr;
if (!Seq1)
return Seq2;
instr = Seq1;
while (instr->Next)
instr = instr->Next;
instr->Next = Seq2;
return Seq1;
}
void WriteSeq(struct InstrSeq *ASeq)
{
struct InstrSeq *instr;
printf("WriteSeq\n");
instr = ASeq;
while (instr)
{
if (instr->Label)
fprintf(aFile, "%s:", instr->Label);
if (instr->OpCode)
{
fprintf(aFile, "\t%s\t", instr->OpCode);
if (instr->Oprnd1)
fprintf(aFile, "\t%s", instr->Oprnd1);
if (instr->Oprnd2)
fprintf(aFile, ", %s", instr->Oprnd2);
if (instr->Oprnd3)
fprintf(aFile, ", %s", instr->Oprnd3);
}
fprintf(aFile, "\n");
instr = instr->Next;
}
if (aFile != stdout)
fclose(aFile);
}
char *
GenLabel()
{
char *label;
label = (char *)malloc(8);
sprintf(label, "L%d", NextLabel++);
return label;
}
char *GenWord()
{
char *word;
word = (char *)malloc(8);
sprintf(word, "_W%d", NextWord++);
return word;
}
int AvailTmpReg()
{
int i;
for (i = 0; i < MAXREG; i++)
{
if (Registers[i].Free)
{
Registers[i].Free = 0;
Registers[i].Used = 1;
return i;
}
}
return -1;
}
char *
TmpRegName(int RegNum)
{
if ((RegNum >= 0) && (RegNum < MAXREG))
{
return Registers[RegNum].Name;
}
else
{
return NULL;
}
}
void ReleaseTmpReg(int ANum)
{
if ((ANum >= 0) && (ANum < MAXREG))
{
Registers[ANum].Free = 1;
}
return;
}
void ResetAllTmpReg()
{
int i;
for (i = 0; i < MAXREG; i++)
{
Registers[i].Free = 1;
Registers[i].Used = 0;
}
return;
}
struct InstrSeq *
SaveSeq()
{
struct InstrSeq *save, *code;
int i, scnt;
char addr[8], offset[8];
scnt = 0;
save = NULL;
code = NULL;
for (i = 0; i < MAXREG; i++)
{
if (!Registers[i].Free)
{
scnt++;
sprintf(addr, "%d($sp)", scnt * 4);
save = AppendSeq(save, GenInstr(NULL, "sw", TmpRegName(i), addr, NULL));
}
}
if (scnt > 0)
{
sprintf(offset, "%d", scnt * 4);
code = GenInstr(NULL, "subu", "$sp", "$sp", offset);
AppendSeq(code, save);
}
return code;
}
struct InstrSeq *
RestoreSeq()
{
struct InstrSeq *code, *save;
int i, scnt;
char addr[8], offset[8];
scnt = 0;
save = NULL;
code = NULL;
for (i = 0; i < MAXREG; i++)
{
if (!Registers[i].Free)
{
scnt++;
sprintf(addr, "%d($sp)", scnt * 4);
save = AppendSeq(save, GenInstr(NULL, "lw", TmpRegName(i), addr, NULL));
}
}
if (scnt > 0)
{
sprintf(offset, "%d", scnt * 4);
code = AppendSeq(save, GenInstr(NULL, "addu", "$sp", "$sp", offset));
}
return code;
}
char *
Imm(int Val)
{
sprintf(Buf, "%d", Val);
return Buf;
}
char *
RegOff(int Offset, char *Reg)
{
sprintf(Buf, "%d(%s)", Offset, Reg);
return Buf;
}