-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen.cpp
More file actions
452 lines (369 loc) · 12.4 KB
/
gen.cpp
File metadata and controls
452 lines (369 loc) · 12.4 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include <stdio.h>
#include <stdlib.h> // exit ()
#include <stdarg.h>
#include <stack>
#include "ast.h"
#include "symtab.h"
/* This stack is used to implement break statements.
"break" is implemented as a goto to the exit label of the most closely enclosing loop (or switch statement).
This label is the top of the stack. Below it on the stack is the exit label of the enclosing loop and so on.
The stack is empty when we are not inside a loop (or switch statement).
*/
static
std::stack<int> exitlabels;
static
int newTemp ()
{
static int counter = 1;
return counter++;
}
static
int newlabel ()
{
static int counter = 1;
return counter++;
}
void pushlabel (int label)
{
exitlabels.push(label);
}
void poplabel ()
{
if(!exitlabels.empty())
exitlabels.pop();
else
errorMsg ("Error pop of exitlabels not in loop or switch\n");
}
// emit works just like printf -- we use emit
// to generate code and print it to the standard output.
void emit (const char *format, ...)
{
// if (errors > 0) return; // do not generate code if there are errors. This should be controlled by if !defined (DEBUG))
printf (" "); // this is meant to add a nice indentation.
// Use emitlabel() to print a label without the indentation.
va_list argptr;
va_start (argptr, format);
// all the arguments following 'format' are passed on to vprintf
vprintf (format, argptr);
va_end (argptr);
}
/* use this to emit a label without using indentation */
void emitlabel (int label)
{
printf ("label%d:\n", label);
}
/* there are two versions of each arithmetic operator in the
generated code. One is used for operands having type int.
The other is used for operands having type float.
*/
struct operator_names {
const char *int_name;
const char *float_name;
};
static
struct operator_names
opNames [] = { {"+", "@+"},
{ "-", "@-"},
{ "*", "@*" },
{ "/", "@/" },
{ "power", "@power" },
{ "%"}};
/* convert operator to string suitable for the given type
e.g. opName (PLUS, _INT) returns "+"
opName (PLUS, _FLOAT) returns "@+"
*/
const char *
opName (enum op op, myType t)
{
if (op > MODULO) { fprintf (stderr, "internal compiler error #1"); exit (1); }
if (op == MODULO)
return opNames [op].int_name;
if (t == _INT)
return opNames [op].int_name;
else
return opNames [op].float_name;
}
int BinaryOp::genExp ()
{
int left_operand_result = _left->genExp ();
int right_operand_result = _right->genExp ();
if( _op == MODULO )
{
if(_left->_type != _INT || _right->_type != _INT)
errorMsg ("line %d: error - modulo op must work only on int operands\n", _line);
}
const char *the_op = opName (_op, _type);
if (_left->_type != _right->_type)
{
int castOperand = newTemp();
if(_left->_type == _INT)
{
emit ("_t%d = static_cast<float> _t%d\n", castOperand, left_operand_result);
left_operand_result = castOperand;
}
else
{
emit ("_t%d = static_cast<float> _t%d\n", castOperand, right_operand_result);
right_operand_result = castOperand;
}
}
_result = newTemp ();
emit ("_t%d = _t%d %s _t%d\n", _result, left_operand_result, the_op, right_operand_result);
return _result;
}
int NumNode::genExp ()
{
_result = newTemp ();
if (_type == _INT)
emit ("_t%d = %d\n", _result, _u.ival);
else
emit ("_t%d = %.2f\n", _result, _u.fval);
return _result;
}
int IdNode::genExp ()
{
_result = newTemp ();
emit ("_t%d = %s\n", _result, _name);
return _result;
}
void SimpleBoolExp::genBoolExp (int truelabel, int falselabel)
{
if (truelabel == FALL_THROUGH && falselabel == FALL_THROUGH)
return; // no need for code
const char *the_op;
int left_result = _left->genExp ();
int right_result = _right->genExp ();
switch (_op) {
case LT:
the_op = "<";
break;
case GT:
the_op = ">";
break;
case LE:
the_op = "<=";
break;
case GE:
the_op = ">=";
break;
case EQ:
the_op = "==";
break;
case NE:
the_op = "!=";
break;
default:
fprintf (stderr, "internal compiler error #3\n"); exit (1);
}
if (truelabel == FALL_THROUGH)
emit ("ifFalse _t%d %s _t%d goto label%d\n", left_result, the_op, right_result, falselabel);
else if (falselabel == FALL_THROUGH)
emit ("if _t%d %s _t%d goto label%d\n", left_result, the_op, right_result, truelabel);
else { // no fall through
emit ("if _t%d %s _t%d goto label%d\n", left_result, the_op, right_result, truelabel);
emit ("goto label%d\n", falselabel);
}
}
void Or::genBoolExp (int truelabel, int falselabel)
{
if (truelabel == FALL_THROUGH && falselabel == FALL_THROUGH)
return; // no need for code
if (truelabel == FALL_THROUGH) {
int next_label = newlabel(); // FALL_THROUGH implemented by jumping to next_label
_left->genBoolExp (next_label, // if left operand is true then the OR expression
//is true so jump to next_label (thus falling through
// to the code following the code for the OR expression)
FALL_THROUGH); // if left operand is false then
// fall through and evaluate right operand
_right->genBoolExp (FALL_THROUGH, falselabel);
emitlabel (next_label);
} else if (falselabel == FALL_THROUGH) {
_left->genBoolExp (truelabel, // if left operand is true then the OR expresson is true
// so jump to truelabel (without evaluating right operand)
FALL_THROUGH); // if left operand is false then
// fall through and evaluate right operand
_right->genBoolExp (truelabel, FALL_THROUGH);
} else { // no fall through
_left->genBoolExp (truelabel, // if left operand is true then the or expresson is true
// so jump to truelabel (without evaluating right operand)
FALL_THROUGH); // if left operand is false then
// fall through and evaluate right operand
_right->genBoolExp (truelabel, falselabel);
}
}
void And::genBoolExp (int truelabel, int falselabel)
{
if (truelabel == FALL_THROUGH && falselabel == FALL_THROUGH)
return; // no need for code
if (truelabel == FALL_THROUGH) {
_left->genBoolExp (FALL_THROUGH, // if left operand is true then fall through and evaluate
// right operand.
falselabel); // if left operand is false then the AND expression is
// false so jump to falselabel);
_right->genBoolExp (FALL_THROUGH, falselabel);
} else if (falselabel == FALL_THROUGH) {
int next_label = newlabel(); // FALL_THROUGH implemented by jumping to next_label
_left->genBoolExp (FALL_THROUGH, // if left operand is true then fall through and
// evaluate right operand
next_label); // if left operand is false then the AND expression
// is false so jump to next_label (thus falling through to
// the code following the code for the AND expression)
_right->genBoolExp (truelabel, FALL_THROUGH);
emitlabel(next_label);
} else { // no fall through
_left->genBoolExp (FALL_THROUGH, // if left operand is true then fall through and
// evaluate right operand
falselabel); // if left operand is false then the AND expression is false
// so jump to falselabel (without evaluating the right operand)
_right->genBoolExp (truelabel, falselabel);
}
}
void Not::genBoolExp (int truelabel, int falselabel)
{
_operand->genBoolExp (falselabel, truelabel);
}
void Fand::genBoolExp (int truelabel, int falselabel)
{
if (truelabel == FALL_THROUGH && falselabel == FALL_THROUGH)
return; // no need for code
if (truelabel == FALL_THROUGH) {
_left->genBoolExp (falselabel, // if left operand is true then the FAND expression is
// false so jump to falselabel);
FALL_THROUGH); // if left operand is false then fall through and evaluate
// right operand.
_right->genBoolExp (falselabel, FALL_THROUGH);
} else if (falselabel == FALL_THROUGH) {
int next_label = newlabel(); // FALL_THROUGH implemented by jumping to next_label
_left->genBoolExp (next_label, // if left operand is true then the FAND expression
// is false so jump to next_label (thus falling through to
// the code following the code for the FAND expression)
FALL_THROUGH); // if left operand is false then fall through and
// evaluate right operand
_right->genBoolExp (FALL_THROUGH, truelabel);
emitlabel(next_label);
} else { // no fall through
_left->genBoolExp (falselabel, // if left operand is true then the FAND expression is true
// so jump to false label (without evaluating the right operand)
FALL_THROUGH);// if left operand is false then fall through and
// evaluate right operand
_right->genBoolExp (falselabel, truelabel);
}
}
void ReadStmt::genStmt()
{
myType idtype = _id->_type;
if (idtype == _INT)
emit ("iread %s\n", _id->_name);
else
emit ("fread %s\n", _id->_name);
}
void WriteStmt::genStmt()
{
if(_exp->_type != _INT && _exp->_type != _FLOAT)
errorMsg("line %d: error: var type undefined.\n",_line);
_exp-> genExp();
myType exptype = _exp->_type;
if (exptype == _INT)
emit ("iwrite _t%d\n", _exp->_result);
else
emit ("fwrite _t%d\n", _exp->_result);
}
void AssignStmt::genStmt()
{
int result = _rhs->genExp();
myType idtype = _lhs->_type;
if (idtype == _rhs->_type)
emit ("%s = _t%d\n", _lhs->_name, result);
else
{
if(idtype == _INT){
emit ("%s = static_cast<int> _t%d", _lhs->_name, result);
fprintf (stderr, " \tline:%d warning: data may lost\n",_line);
}
else
emit ("%s = static_cast<float> _t%d\n", _lhs->_name, result);
}
}
void IfStmt::genStmt()
{
int elseStmtlabel = newlabel ();
int exitlabel = newlabel ();
_condition->genBoolExp (FALL_THROUGH, elseStmtlabel);
_thenStmt->genStmt ();
emit ("goto label%d\n", exitlabel);
emitlabel(elseStmtlabel);
_elseStmt->genStmt();
emitlabel(exitlabel);
}
void WhileStmt::genStmt()
{
int condlabel = newlabel ();
int exitlabel = newlabel ();
pushlabel(exitlabel);
emitlabel(condlabel);
_condition->genBoolExp (FALL_THROUGH, exitlabel);
_body->genStmt ();
emit ("goto label%d\n", condlabel);
emitlabel (exitlabel);
poplabel ();
}
void Block::genStmt()
{
for (Stmt *stmt = _stmtlist; stmt != NULL; stmt = stmt->_next)
stmt->genStmt();
}
void SwitchStmt::genStmt()
{
int result = _exp->genExp ();
if( _exp->_type != _INT )
errorMsg ("line %d: error - switch expression must have type int\n", _line);
int condlabel = newlabel ();
int exitlabel = newlabel ();
int defaultlabel = newlabel ();
pushlabel(exitlabel);
emit ("goto label%d\n", condlabel);
Case *currentCase = _caselist;
while(currentCase != NULL)
{
currentCase->_label = newlabel ();
emitlabel(currentCase->_label);
currentCase->_stmt->genStmt();
if(currentCase->_hasBreak)
emit ("goto label%d\n", exitlabel);
currentCase = currentCase->_next;
}
emitlabel(defaultlabel);
_default_stmt->genStmt();
emit ("goto label%d\n", exitlabel);
emitlabel(condlabel);
currentCase = _caselist;
while(currentCase != NULL)
{
emit ("case _t%d %d label%d\n", result, currentCase->_number, currentCase->_label);
currentCase = currentCase->_next;
}
emit ("case _t%d _t%d label%d\n", result, result, defaultlabel);
emitlabel(exitlabel);
poplabel ();
}
void BreakStmt::genStmt()
{
if(!exitlabels.empty())
emit ("goto label%d\n", exitlabels.top ());
else
errorMsg ("line %d. Break not in loop or switch case\n", _line);
}
void ForStmt::genStmt()
{
_init->genStmt ();
int condlabel = newlabel ();
int exitlabel = newlabel ();
pushlabel(exitlabel);
emitlabel(condlabel);
_condition->genBoolExp (FALL_THROUGH, exitlabel);
_body->genStmt ();
_afterStep->genStmt ();
emit ("goto label%d\n", condlabel);
emitlabel(exitlabel);
poplabel ();
}