-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.cc
More file actions
322 lines (277 loc) · 9.47 KB
/
codegen.cc
File metadata and controls
322 lines (277 loc) · 9.47 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* File: codegen.cc
* ----------------
* Implementation for the CodeGenerator class. The methods don't do anything
* too fancy, mostly just create objects of the various Tac instruction
* classes and append them to the list.
*/
#include "codegen.h"
#include <string.h>
#include "tac.h"
#include "mips.h"
CodeGenerator::CodeGenerator()
{
code = new List<Instruction*>();
currentStackSize = 0;
}
char *CodeGenerator::NewLabel()
{
static int nextLabelNum = 0;
char temp[10];
sprintf(temp, "_L%d", nextLabelNum++);
return strdup(temp);
}
Location *CodeGenerator::GenGlobalVar(const char* name) {
static int nextGlobalOffset;
Location *result = new Location(gpRelative, nextGlobalOffset, name);
nextGlobalOffset += 4;
return result;
}
Location *CodeGenerator::GenTempVar()
{
static int nextTempNum;
char temp[10];
sprintf(temp, "_tmp%d", nextTempNum++);
Location *result = GetNewLocationOnStack(temp);
Assert(result != NULL);
return result;
}
Location *CodeGenerator::GetNewLocationOnStack(const char* name) {
Location* result = new Location(fpRelative, (this->currentStackSize) * (-4) + OffsetToFirstLocal, name);
this->currentStackSize++;
return result;
}
Location *CodeGenerator::GenLoadConstant(int value)
{
Location *result = GenTempVar();
code->Append(new LoadConstant(result, value));
return result;
}
Location *CodeGenerator::GenLoadConstant(const char *s)
{
Location *result = GenTempVar();
code->Append(new LoadStringConstant(result, s));
return result;
}
Location *CodeGenerator::GenLoadLabel(const char *label)
{
Location *result = GenTempVar();
code->Append(new LoadLabel(result, label));
return result;
}
void CodeGenerator::GenAssign(Location *dst, Location *src)
{
code->Append(new Assign(dst, src));
}
Location *CodeGenerator::GenLoad(Location *ref, int offset)
{
Location *result = GenTempVar();
code->Append(new Load(result, ref, offset));
return result;
}
void CodeGenerator::GenStore(Location *dst, Location *src, int offset)
{
code->Append(new Store(dst, src, offset));
}
Location *CodeGenerator::GenBinaryOp(const char *opName, Location *op1, Location *op2)
{
Location *result = GenTempVar();
code->Append(new BinaryOp(BinaryOp::OpCodeForName(opName), result, op1, op2));
return result;
}
void CodeGenerator::GenLabel(const char *label)
{
code->Append(new Label(label));
}
void CodeGenerator::GenIfZ(Location *test, const char *label)
{
code->Append(new IfZ(test, label));
}
void CodeGenerator::GenGoto(const char *label)
{
code->Append(new Goto(label));
}
void CodeGenerator::GenReturn(Location *val)
{
code->Append(new Return(val));
}
BeginFunc *CodeGenerator::GenBeginFunc()
{
BeginFunc *result = new BeginFunc;
this->currentStackSize = 0;
code->Append(result);
return result;
}
void CodeGenerator::GenEndFunc()
{
code->Append(new EndFunc());
}
void CodeGenerator::GenPushParam(Location *param)
{
code->Append(new PushParam(param));
}
void CodeGenerator::GenPopParams(int numBytesOfParams)
{
Assert(numBytesOfParams >= 0 && numBytesOfParams % VarSize == 0); // sanity check
if (numBytesOfParams > 0)
code->Append(new PopParams(numBytesOfParams));
}
Location *CodeGenerator::GenLCall(const char *label, bool fnHasReturnValue)
{
Location *result = fnHasReturnValue ? GenTempVar() : NULL;
code->Append(new LCall(label, result));
return result;
}
Location *CodeGenerator::GenACall(Location *fnAddr, bool fnHasReturnValue)
{
Location *result = fnHasReturnValue ? GenTempVar() : NULL;
code->Append(new ACall(fnAddr, result));
return result;
}
static struct _builtin {
const char *label;
int numArgs;
bool hasReturn;
} builtins[] =
{{"_Alloc", 1, true},
{"_ReadLine", 0, true},
{"_ReadInteger", 0, true},
{"_StringEqual", 2, true},
{"_PrintInt", 1, false},
{"_PrintString", 1, false},
{"_PrintBool", 1, false},
{"_Halt", 0, false}};
Location *CodeGenerator::GenBuiltInCall(BuiltIn bn,Location *arg1, Location *arg2)
{
Assert(bn >= 0 && bn < NumBuiltIns);
struct _builtin *b = &builtins[bn];
Location *result = NULL;
if (b->hasReturn) result = GenTempVar();
// verify appropriate number of non-NULL arguments given
Assert((b->numArgs == 0 && !arg1 && !arg2)
|| (b->numArgs == 1 && arg1 && !arg2)
|| (b->numArgs == 2 && arg1 && arg2));
if (arg2) code->Append(new PushParam(arg2));
if (arg1) code->Append(new PushParam(arg1));
code->Append(new LCall(b->label, result));
GenPopParams(VarSize*b->numArgs);
return result;
}
void CodeGenerator::GenVTable(const char *className, List<const char *> *methodLabels)
{
code->Append(new VTable(className, methodLabels));
}
void CodeGenerator::DoFinalCodeGen()
{
if (IsDebugOn("tac")) { // if debug don't translate to mips, just print Tac
for (int i = 0; i < code->NumElements(); i++)
code->Nth(i)->Print();
} else {
Mips mips;
mips.EmitPreamble();
for (int i = 0; i < code->NumElements(); i++)
code->Nth(i)->Emit(&mips);
printf( "_PrintInt:\n"
"subu $sp, $sp, 8 # decrement sp to make space to save ra, fp\n"
"sw $fp, 8($sp) # save fp\n"
"sw $ra, 4($sp) # save ra\n"
"addiu $fp, $sp, 8 # set up new fp\n"
"li $v0, 1\t\t# system call code for print_int\n"
"lw $a0, 4($fp)\t\t # integer to print\n"
"syscall\t\t # print it;\n"
"move $sp, $fp # pop callee frame off stack\n"
"lw $ra, -4($fp) # restore saved ra\n"
"lw $fp, 0($fp) # restore saved fp\n"
"jr $ra # return from function\n");
//"jr $ra\t\t# return from function\n");
printf( "_PrintString:\n"
"li $v0, 4\t\t# system call code for print_string\n"
"lw $a0, 4($sp)\t\t # integer to print\n"
"syscall\t\t # print it;\n"
"jr $ra\t\t# return from function\n");
printf( "_ReadLine:\n"
"li $a0, 1\t\t # place to print\n"
"li $v0, 9\t\t# system call code for read_string\n"
"syscall\t\t # print it;\n"
"move $a0, $v0\t\t # place to print\n"
"li $v0, 8\t\t# system call code for read_string\n"
"syscall\t\t # print it;\n"
"move $v0, $a0\n"
"move $sp, $fp\t\t# pop callee frame off stack\n"
"jr $ra\t\t# return from function\n");
printf("_StringEqual:\n"
"cmploop:\n"
" lb $t2,4($sp) \n"
" lb $t3,8($sp) \n"
" bne $t2,$t3,cmpne \n"
" beq $t2,$zero,cmpeq \n"
" beq $t3,$zero,cmpeq \n"
" addi $s2,$s2,1 \n"
" addi $s3,$s3,1 \n"
" j cmploop \n"
" cmpne: \n"
" li $v0, 0 \n"
" j cout \n"
" cmpeq: \n"
" li $v0, 1 \n"
" cout: \n"
" move $sp, $fp\t\t# pop callee frame off stack\n"
" jr $ra\t\t# return from function\n");
printf( "_Alloc:\n"
"li $v0, 9\t\t# system call code for print_int\n"
"lw $a0, 4($sp)\t\t # integer to print\n"
"syscall\t\t # print it;\n"
"move $sp, $fp\t\t# pop callee frame off stack\n"
"jr $ra\t\t# return from function\n");
printf( "_Halt:\n"
"li $v0, 10\t\t# system call code for print_int\n"
"syscall\t\t # print it;\n");
//printf( "_PrintBool:\n"
// ".data # create string constant marked with label"
// " _true : .asciiz \"true\" "
// " _false : .asciiz \"false\" "
// ".text"
// " lw $t0, 4($sp)"
// " beqz $t0, _printfalse"
// " la $t1, _true # load label"
// " b _out "
// "_printfalse:"
// " la $t1, _false # load label"
// "_out:"
// "move $a0, $t1\t\t # integer to print\n"
// "li $v0, 4\t\t# system call code for print_string\n"
//
// "syscall\t\t # print it;\n"
// "move $sp, $fp\t\t# pop callee frame off stack\n"
// "jr $ra\t\t# return from function\n");
}
}
/* printf( "_PrintInt:\n"
"sw $ra, -4($fp)\t# save ra\n"
"sw $fp, 0($fp)\t# save fp\n"
"li $v0, 1\t\t# system call code for print_int\n"
"lw $a0, 4($sp)\t\t # integer to print\n"
"syscall\t\t # print it;\n"
"move $sp, $fp\t\t# pop callee frame off stack\n"
"lw $ra, -4($fp)\t# restore saved ra\n"
"lw $fp, 0($fp)\t# restore saved fp\n"
"jr $ra\t\t# return from function\n");
printf( "_PrintString:\n"
"sw $ra, -4($fp)\t# save ra\n"
"sw $fp, 0($fp)\t# save fp\n"
"li $v0, 4\t\t# system call code for print_string\n"
"lw $a0, 4($sp)\t\t # integer to print\n"
"syscall\t\t # print it;\n"
"move $sp, $fp\t\t# pop callee frame off stack\n"
"lw $ra, -4($fp)\t# restore saved ra\n"
"lw $fp, 0($fp)\t# restore saved fp\n"
"jr $ra\t\t# return from function\n");
printf( "_PrintBool:\n"
"sw $ra, -4($fp)\t# save ra\n"
"sw $fp, 0($fp)\t# save fp\n"
"li $v0, 4\t\t# system call code for print_string\n"
"lw $a0, 4($sp)\t\t # integer to print\n"
"syscall\t\t # print it;\n"
"move $sp, $fp\t\t# pop callee frame off stack\n"
"lw $ra, -4($fp)\t# restore saved ra\n"
"lw $fp, 0($fp)\t# restore saved fp\n"
"jr $ra\t\t# return from function\n"); */