-
Notifications
You must be signed in to change notification settings - Fork 3
/
tac.cc
226 lines (187 loc) · 5.86 KB
/
tac.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
/* File: tac.cc
* ------------
* Implementation of Location class and Instruction class/subclasses.
*/
#include "tac.h"
#include "mips.h"
#include <string.h>
Location::Location(Segment s, int o, const char *name) :
variableName(strdup(name)), segment(s), offset(o){
// cout << "Location name = " << name << ", offset = " << offset << endl;
}
void Instruction::Print() {
printf("\t%s ;\n", printed);
}
void Instruction::Emit(Mips *mips) {
if (*printed)
mips->Emit("# %s", printed); // emit TAC as comment into assembly
EmitSpecific(mips);
}
LoadConstant::LoadConstant(Location *d, int v)
: dst(d), val(v) {
Assert(dst != NULL);
sprintf(printed, "%s = %d", dst->GetName(), val);
}
void LoadConstant::EmitSpecific(Mips *mips) {
mips->EmitLoadConstant(dst, val);
}
LoadStringConstant::LoadStringConstant(Location *d, const char *s)
: dst(d) {
Assert(dst != NULL && s != NULL);
const char *quote = (*s == '"') ? "" : "\"";
str = new char[strlen(s) + 2*strlen(quote) + 1];
sprintf(str, "%s%s%s", quote, s, quote);
quote = (strlen(str) > 50) ? "...\"" : "";
sprintf(printed, "%s = %.50s%s", dst->GetName(), str, quote);
}
void LoadStringConstant::EmitSpecific(Mips *mips) {
mips->EmitLoadStringConstant(dst, str);
}
LoadLabel::LoadLabel(Location *d, const char *l)
: dst(d), label(strdup(l)) {
Assert(dst != NULL && label != NULL);
sprintf(printed, "%s = %s", dst->GetName(), label);
}
void LoadLabel::EmitSpecific(Mips *mips) {
mips->EmitLoadLabel(dst, label);
}
Assign::Assign(Location *d, Location *s)
: dst(d), src(s) {
Assert(dst != NULL && src != NULL);
sprintf(printed, "%s = %s", dst->GetName(), src->GetName());
}
void Assign::EmitSpecific(Mips *mips) {
mips->EmitCopy(dst, src);
}
Load::Load(Location *d, Location *s, int off)
: dst(d), src(s), offset(off) {
Assert(dst != NULL && src != NULL);
if (offset)
sprintf(printed, "%s = *(%s + %d)", dst->GetName(), src->GetName(), offset);
else
sprintf(printed, "%s = *(%s)", dst->GetName(), src->GetName());
}
void Load::EmitSpecific(Mips *mips) {
mips->EmitLoad(dst, src, offset);
}
Store::Store(Location *d, Location *s, int off)
: dst(d), src(s), offset(off) {
Assert(dst != NULL && src != NULL);
if (offset)
sprintf(printed, "*(%s + %d) = %s", dst->GetName(), offset, src->GetName());
else
sprintf(printed, "*(%s) = %s", dst->GetName(), src->GetName());
}
void Store::EmitSpecific(Mips *mips) {
mips->EmitStore(dst, src, offset);
}
const char * const BinaryOp::opName[BinaryOp::NumOps] = {"+", "-", "*", "/", "%", "==", "<", "&&", "||"};
BinaryOp::OpCode BinaryOp::OpCodeForName(const char *name) {
for (int i = 0; i < NumOps; i++)
if (opName[i] && !strcmp(opName[i], name))
return (OpCode)i;
Failure("Unrecognized Tac operator: '%s'\n", name);
return Add; // can't get here, but compiler doesn't know that
}
BinaryOp::BinaryOp(OpCode c, Location *d, Location *o1, Location *o2)
: code(c), dst(d), op1(o1), op2(o2) {
Assert(dst != NULL && op1 != NULL && op2 != NULL);
Assert(code >= 0 && code < NumOps);
sprintf(printed, "%s = %s %s %s", dst->GetName(), op1->GetName(), opName[code], op2->GetName());
}
void BinaryOp::EmitSpecific(Mips *mips) {
mips->EmitBinaryOp(code, dst, op1, op2);
}
Label::Label(const char *l) : label(strdup(l)) {
Assert(label != NULL);
*printed = '\0';
}
void Label::Print() {
printf("%s:\n", label);
}
void Label::EmitSpecific(Mips *mips) {
mips->EmitLabel(label);
}
Goto::Goto(const char *l) : label(strdup(l)) {
Assert(label != NULL);
sprintf(printed, "Goto %s", label);
}
void Goto::EmitSpecific(Mips *mips) {
mips->EmitGoto(label);
}
IfZ::IfZ(Location *te, const char *l)
: test(te), label(strdup(l)) {
Assert(test != NULL && label != NULL);
sprintf(printed, "IfZ %s Goto %s", test->GetName(), label);
}
void IfZ::EmitSpecific(Mips *mips) {
mips->EmitIfZ(test, label);
}
BeginFunc::BeginFunc() {
sprintf(printed,"BeginFunc (unassigned)");
frameSize = -555; // used as sentinel to recognized unassigned value
}
void BeginFunc::SetFrameSize(int numBytesForAllLocalsAndTemps) {
frameSize = numBytesForAllLocalsAndTemps;
sprintf(printed,"BeginFunc %d", frameSize);
}
void BeginFunc::EmitSpecific(Mips *mips) {
mips->EmitBeginFunction(frameSize);
}
EndFunc::EndFunc() : Instruction() {
sprintf(printed, "EndFunc");
}
void EndFunc::EmitSpecific(Mips *mips) {
mips->EmitEndFunction();
}
Return::Return(Location *v) : val(v) {
sprintf(printed, "Return %s", val? val->GetName() : "");
}
void Return::EmitSpecific(Mips *mips) {
mips->EmitReturn(val);
}
PushParam::PushParam(Location *p)
: param(p) {
Assert(param != NULL);
sprintf(printed, "PushParam %s", param->GetName());
}
void PushParam::EmitSpecific(Mips *mips) {
mips->EmitParam(param);
}
PopParams::PopParams(int nb)
: numBytes(nb) {
sprintf(printed, "PopParams %d", numBytes);
}
void PopParams::EmitSpecific(Mips *mips) {
mips->EmitPopParams(numBytes);
}
LCall::LCall(const char *l, Location *d)
: label(strdup(l)), dst(d) {
sprintf(printed, "%s%sLCall %s", dst? dst->GetName(): "", dst?" = ":"", label);
}
void LCall::EmitSpecific(Mips *mips) {
mips->EmitLCall(dst, label);
}
ACall::ACall(Location *ma, Location *d)
: dst(d), methodAddr(ma) {
Assert(methodAddr != NULL);
sprintf(printed, "%s%sACall %s", dst? dst->GetName(): "", dst?" = ":"",
methodAddr->GetName());
}
void ACall::EmitSpecific(Mips *mips) {
mips->EmitACall(dst, methodAddr);
}
VTable::VTable(const char *l, List<const char *> *m)
: methodLabels(m), label(strdup(l)) {
Assert(methodLabels != NULL && label != NULL);
sprintf(printed, "VTable for class %s", l);
}
void VTable::Print() {
printf("VTable %s =\n", label);
for (int i = 0; i < methodLabels->NumElements(); i++)
printf("\t%s,\n", methodLabels->Nth(i));
printf("; \n");
}
void VTable::EmitSpecific(Mips *mips) {
mips->EmitVTable(label, methodLabels);
}