-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaststatements.cpp
354 lines (325 loc) · 7.53 KB
/
aststatements.cpp
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "aststatements.h"
extern symboltable symtab;
extern memallocator alloc;
//
//
//
//////////// statementlist
//
//
//
void statementlist::debug(int ind)
{
cout << indent(ind) << string("statementlist.") << endl;
statements.debug(ind + 1);
}
void statementlist::semanticCheck()
{
for (statements.begin() ; !statements.atEnd() ; statements.gotoNext()) {
statement* s = statements.getCurrent();
// for now do the first one
s->semanticCheck();
}
}
registerid statementlist::generateCode(operation* &p)
{
for (statements.begin() ; !statements.atEnd() ; statements.gotoNext()) {
statement* s = statements.getCurrent();
// for now do the first one
s->generateCode(p);
}
return -1;
}
//
//
//
//////////////// letstatement
//
//
//
void letstatement::debug(int ind)
{
cout << indent(ind) << "letstatement" << endl;
lv->debug(ind + 1);
value->debug(ind + 1);
}
void letstatement::semanticCheck()
{
// if the let statement is a simple assignment to a variable,
// the variable doesn't have to be typed.
// otherwise it does.
lv->semanticCheck();
value->semanticCheck();
if (typeid(*lv) == typeid(varRef)) {
varRef* vr = (varRef*)lv;
symtableentry* ste = symtab.idlookup(vr->varname);
type* tid = ste->datatype;
if (tid == 0) {
ste->datatype = value->datatype;
} else if (tid != value->datatype) {
cout << "previously typed identifier " << ste->name << " redefined." << endl;
debug(0);
exit(1);
}
} else {
// in this case, just make sure the types are the same.
if (lv->datatype != value->datatype) {
cout << "previously typed variable redefined." << endl;
debug(0);
exit(1);
}
}
}
registerid letstatement::generateCode(operation* &p) {
// the issue here is that the let statement could in fact be a return statement,
// if the name we are "letting" to is the name of the function we are in.
cout << "NOT IMPLEMENTED: letstatement::generatecode" << endl;
exit(1);
registerid valloc = value->generateCode(p);
lv->generateStoreCode(valloc, p);
return 0;
}
letstatement::~letstatement()
{
delete lv;
delete value;
}
//
//
//
////////////// dostatement
//
//
//
void dostatement::debug(int ind)
{
cout << indent(ind) << "dostatement " << endl;
allstatements->debug(ind + 1);
}
void dostatement::semanticCheck()
{
if (condexp) {
condexp->semanticCheck();
}
allstatements->semanticCheck();
}
registerid dostatement::generateCode(operation* &p)
{
// the dostatement code consists of
// 1. cond/noop
// 2. body
// 3. jump/noop
// 4. noop
// where
// 1. is either a noop, or code that tests the condition and jumps to 4 if it's not true
// 2. is the statementlist
// 3. is a jump back to 1. (if repeat) or not (if no repeat)
// 4. is always a noop
//
operation* jumpback;
operation* end = new op_noop();
if (condexp != 0) {
operation* startpos = p;
registerid testloc = condexp->generateCode(p);
jumpback = startpos->next;
operation *test = new op_bz(testloc, end);
p = p->append(test);
} else {
jumpback = new op_noop();
p = p->append(jumpback);
}
allstatements->generateCode(p);
if (allstatements->repeats()) {
operation *k = new op_jump(jumpback);
p = p->append(k);
}
p = p->append(end);
return 0;
}
dostatement::~dostatement()
{
delete allstatements;
delete condexp;
}
//
//
//
//////////// choosestatement
//
//
//
void choosestatement::debug(int ind)
{
cout << indent(ind) << "choosestatement " << endl;
allstatements->debug(ind + 1);
}
void choosestatement::semanticCheck()
{
allstatements->semanticCheck();
}
registerid choosestatement::generateCode(operation* &p)
{
/*
first I create a no-op to jump to after everything is done.
then I generate the code for all do statements,
each time saving the jump-in op pointer for that statement.
after each do statement, I insert a hard jump to the no-op.
finally, I extract the conditional from each do-statement
and regenerate the code for that conditional.
finally I insert those tests with jumps before the start of the dostatements
test1
jump to 1
test2
jump to 2
test3
jump to 3
jump to end
dostatement1
jump to end
dostatement2
jump to end
dostatement2
jump to end
end (noop)
*/
operation* end = new op_noop();
operation* startp = p; // we are going to insert the conditions later
astnodelist<statement*> nodelist = allstatements->getNodeList();
vector<operation*> jumptos;
for (nodelist.begin() ; !nodelist.atEnd() ; nodelist.gotoNext()) {
// these all should be dostatements or we will have serious problems.
dostatement* dost = (dostatement*)nodelist.getCurrent();
operation* before = p;
dost->generateCode(p);
jumptos.push_back(before->next);
operation* endjump = new op_jump(end);
p = p->append(endjump);
}
p = p->append(end);
// that's all good, now add the conditions.
operation* newp = new op_noop();
operation* anotherstart = newp;
vector<operation*>::iterator jumpiter;
for ( nodelist.begin() , jumpiter = jumptos.begin()
; !nodelist.atEnd() , jumpiter != jumptos.end()
; nodelist.gotoNext() , jumpiter++
) {
// these all should be dostatements or we will have serious problems.
dostatement* dost = (dostatement*)nodelist.getCurrent();
operation* jumpto = (*jumpiter);
expression * test = dost->getConditional();
if (test != 0) {
registerid loc = test->generateCode(newp);
// if loc is true, then branch to jumpto
operation* testit = new op_bnz(loc, jumpto);
newp = newp->append(testit);
} else {
// if there is no test, then we always jump.
operation* alwaysjump = new op_jump(jumpto);
newp = newp->append(alwaysjump);
}
}
// if nothing tested true, we jump out of here
operation* nothingjump = new op_jump(end);
newp = newp->append(nothingjump);
// ok now splice it in.
operation *spn = startp->next;
startp->next = anotherstart;
anotherstart->previous = startp;
newp->next = spn;
spn->previous = newp;
return 0;
}
choosestatement::~choosestatement()
{
delete allstatements;
}
//
//
//
//////////// expressionStatement
//
//
//
void expressionstatement::debug(int ind)
{
cout << indent(ind) << "expressionstatement " << endl;
expr->debug(ind + 1);
}
void expressionstatement::semanticCheck()
{
expr->semanticCheck();
}
registerid expressionstatement::generateCode(operation* &p)
{
expr->generateCode(p);
return 0;
}
expressionstatement::~expressionstatement()
{
delete expr;
}
/*
//
//
//
//////////// returnstatement
//
//
//
void returnstatement::debug(int ind)
{
cout << indent(ind) << "returnstatement " << endl;
value->debug(ind + 1);
}
void returnstatement::semanticCheck()
{
value->semanticCheck();
}
registerid returnstatement::generateCode(operation* &p)
{
// return is really
// set the stack pointer to be equal to the frame pointer
// pop the return address off the stack
// push the return value
// then jump to the address
//p = p->append(new op_return());
registerid val = value->generateCode(p);
registerid jumpto = alloc.getNextRegister();
registerid sp = SPREGID;
registerid fp = FPREGID;
p = p->append(new op_move(sp, fp));
p = p->append(new op_pop(jumpto));
p = p->append(new op_push(val));
p = p->append(new op_jumpreg(jumpto));
return 0;
}
returnstatement::~returnstatement()
{
delete t;
delete value;
}
*/
//
//
//
//////////// callstatement
//
//
//
void callstatement::debug(int ind)
{
cout << indent(ind) << "callstatement (" << symtab.idlookup(n)->name << ")" << endl;
e->debug(ind + 1);
}
registerid callstatement::generateCode(operation* &p)
{
cout << "not implemented:callstatement" <<endl;
exit(1);
return 0;
}
void callstatement::semanticCheck()
{
cout << "not implemented:callstatement" <<endl;
exit(1);
}