-
Notifications
You must be signed in to change notification settings - Fork 3
/
codegen.cc
228 lines (184 loc) · 5.26 KB
/
codegen.cc
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
/* 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*>();
nextLocalOffset = OffsetToFirstLocal;
nextParamOffset = OffsetToFirstParam;
nextGlobalOffset = OffsetToFirstGlobal;
currFunctionFrameSize = 0;
currSeg = gpRelative;
currParametersLocations = new Hashtable<Location*>;
ReadWriteField = -1;
}
char *CodeGenerator::NewLabel()
{
static int nextLabelNum = 0;
char temp[10];
sprintf(temp, "_L%d", nextLabelNum++);
return strdup(temp);
}
Location *CodeGenerator::GenTempVar()
{
static int nextTempNum;
char temp[10];
Location *result = NULL;
sprintf(temp, "_tmp%d", nextTempNum++);
/* pp4: need to create variable in proper location
in stack frame for use as temporary. Until you
do that, the assert below will always fail to remind
you this needs to be implemented */
//need to store a pointer to the next available spot for a temp (grows downwards)
result = new Location(currSeg, nextLocalOffset, temp);
if (currSeg == fpRelative)
{
nextLocalOffset -= 4;
currFunctionFrameSize += VarSize;
}
else if (currSeg == gpRelative)
{
nextGlobalOffset += 4;
}
Assert(result != NULL);
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;
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);
}
}