-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.cc
618 lines (502 loc) · 15.4 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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#include <string>
#include "pstcode.h"
#include "codegen.h"
using namespace std;
extern int global_count_loc;
PstackCode CodeGen::generate(Visitable *vis)
{
vis->accept(this);
return code;
}
void CodeGen::visitProg(Prog *prog)
{
code.begin_prog();
code.prolog(symbols);
// Insert call to main(), to be patched up later.
code.add(I_CALL);
int patchloc = code.pos();
code.add(0);
code.add(0);
code.add(I_ENDPROG);
// Generate code for the functions.
prog->listfunction_->accept(this);
// Now look up the address of main, and throw if it wasn't defined.
if (!symbols.exists("main"))
throw UnknownFunc("main");
int level = symbols.levelof("main");
int addr = symbols["main"]->address();
// Patch up the address of main.
code.at(patchloc) = level;
code.at(patchloc + 1) = addr;
code.end_prog();
}
void CodeGen::visitGlobal(Global *global){
global->type_->accept(this);
visitIdent(global->ident_); // sets currid
// Start at 3, beginning of locals
symbols.insert(Symbol(currid, currtype, 3 + symbols.numvars()));
// Set global count in I_PROG call
code.at(global_count_loc) = symbols.numvars();
}
void CodeGen::visitFun(Fun *fun)
{
fun->type_->accept(this);
// Store return type of function
type_t return_type = currtype;
visitIdent(fun->ident_);
Ident fun_name = currid;
if (symbols.exists(fun_name))
throw Redeclared(fun_name);
// Insert bare function symbol to be added to later
symbols.insert(Symbol(fun_name, code.pos()));
code.add(I_PROC);
int patchloc = code.pos(); // to be filled with number of local variables.
code.add(0);
code.add(code.pos() + 1); // function code starts next
symbols.enter(); // since parameters are local to the function
// Adds entries to symbol table, sets funargs
fun->listfdecl_->accept(this);
// Set function type info
symbols[fun_name]->set_function_type_info(curr_arg_list_types, return_type);
int startvar = symbols.numvars();
// Generate code for function body.
fun->liststm_->accept(this);
// Last value of currtype is the real return value
type_t actual_ret_val_type = currtype;
// Fill in number of local variables.
code.at(patchloc) = symbols.numvars() - startvar;
symbols.leave();
// Return, popping off our parameters.
code.add(I_ENDPPROC);
code.add(funargs);
// Test that real return value and expected are the same
symbols[fun_name]->check_return(actual_ret_val_type);
}
void CodeGen::visitDec(Dec *dec)
{
dec->type_->accept(this); // sets currtype
dec->listident_->accept(this); // visitListIdent; uses currtype
}
void CodeGen::visitFDec(FDec *dec)
{
dec->type_->accept(this); // sets currtype
visitIdent(dec->ident_); //visitIdent; uses currtype
// First local variable (numvars = funargs) has address 3, etc.
// If this ListIdent is actually part of a parameter list, these
// addresses will be fixed up by visitListFDecl.
symbols.insert(Symbol(currid, currtype, 3 + symbols.numvars() - funargs));
}
void CodeGen::visitSDecl(SDecl *sdecl)
{
sdecl->decl_->accept(this); // visitDec
}
void CodeGen::visitSExp(SExp *sexp)
{
sexp->exp_->accept(this);
// Pop and discard the expression's value. pstack doesn't have a
// POP instruction, but a conditional jump to the next instruction
// (PC + 2) will do the trick.
code.add(I_JR_IF_TRUE);
code.add(2);
}
void CodeGen::visitSBlock(SBlock *sblock)
{
sblock->liststm_->accept(this);
}
void CodeGen::visitSWhile(SWhile *swhile)
{
int looploc = code.pos(); // Beginning of test
swhile->exp_->accept(this);
code.add(I_JR_IF_FALSE); // Jump past the body.
code.add(0);
int patchloc = code.pos() - 1;
swhile->stm_->accept(this); // Body.
code.add(I_JR);
code.add(looploc - (code.pos() - 1)); // offset to looploc
code.at(patchloc) = code.pos() - (patchloc - 1);
}
void CodeGen::visitSRepeat(SRepeat *srepeat)
{
int looploc = code.pos(); // Beginning of code
srepeat->stm_->accept(this); // Body.
srepeat->exp_->accept(this); // Test
code.add(I_JR_IF_FALSE); // Jump to the body.
int offset = looploc - (code.pos() - 1);
code.add(offset);
}
void CodeGen::visitSIf(SIf *sif)
{
sif->exp_->accept(this);
code.add(I_JR_IF_FALSE); // Jump past the body.
code.add(0);
int patchloc = code.pos() - 1;
sif->stm_->accept(this); // Body.
code.at(patchloc) = code.pos() - (patchloc - 1);
}
void CodeGen::visitSIfThen(SIfThen *sifthen)
{
sifthen->exp_->accept(this);
code.add(I_JR_IF_FALSE); // Jump past the body.
code.add(0);
int patchloc = code.pos() - 1;
sifthen->stm_->accept(this); // Body.
code.at(patchloc) = code.pos() - (patchloc - 1);
}
void CodeGen::visitSIfThenElse(SIfThenElse *sifthenelse)
{
sifthenelse->exp_->accept(this);
code.add(I_JR_IF_FALSE); // Jump past the body.
code.add(0);
int patchloc = code.pos() - 1;
sifthenelse->stm_1->accept(this); // Body.
code.at(patchloc) = code.pos() - (patchloc - 1);
sifthenelse->stm_2->accept(this); // Body.
}
void CodeGen::visitSFor(SFor *sfor)
{
int looploc = code.pos(); // Beginning of test
sfor->exp_1->accept(this);
code.add(I_JR_IF_FALSE); // Jump past the body.
code.add(0);
int patchloc = code.pos() - 1;
sfor->stm_->accept(this); // Body.
sfor->exp_2->accept(this);
code.add(I_JR);
code.add(looploc - (code.pos() - 1)); // offset to looploc
code.at(patchloc) = code.pos() - (patchloc - 1);
}
void CodeGen::visitSForScoped(SForScoped *sfor){
code.add(I_PROC);
code.add(1);
code.add(code.pos() + 1); // function code starts next
symbols.enter(); // since parameters are local to the function
code.add(I_CONSTANT);
code.add(0);
sfor->type_->accept(this);
visitIdent(sfor->ident_); // sets currid
symbols.insert(Symbol(currid, currtype, -1));
// Compute the address.
code.add(I_VARIABLE);
code.add(symbols.levelof(currid));
code.add(symbols[currid]->address());
// One copy of the address for the assignment, one for the result.
code.add_dup();
// Generate code for the value of the RHS.
sfor->exp_1->accept(this);
// Store the value at the computed address.
code.add(I_ASSIGN);
code.add(1);
// Dereference the address and return its value.
code.add(I_VALUE);
int looploc = code.pos(); // Beginning of test
sfor->exp_2->accept(this);
code.add(I_JR_IF_FALSE); // Jump past the body.
code.add(0);
int patchloc = code.pos() - 1;
sfor->stm_->accept(this); // Body.
sfor->exp_3->accept(this);
code.add(I_JR);
code.add(looploc - (code.pos() - 1)); // offset to looploc
code.at(patchloc) = code.pos() - (patchloc - 1);
symbols.leave(); // since parameters are local to the function
}
void CodeGen::visitSReturn(SReturn *sreturn)
{
// Store the top of stack (return value) at (bp-funargs)
int patchloc = code.pos();
code.add(0);
code.add(0);
code.add(-(funargs+1));
sreturn->exp_->accept(this);
if(currtype == TY_INT){
code.at(patchloc) = I_VARIABLE;
code.add(I_ASSIGN);
}else if(currtype == TY_DOUBLE){
code.at(patchloc) = R_VARIABLE;
code.add(R_ASSIGN);
}else{
throw(string("Unable to return variable type ") + type_id_to_str(currtype));
}
code.add(1);
// And return, popping off our parameters.
code.add(I_ENDPPROC);
code.add(funargs);
}
void CodeGen::visitEAss(EAss *eass)
{
visitIdent(eass->ident_); // sets currid
if (!symbols.exists(currid))
throw UnknownVar(currid);
Ident variable_ident = currid;
// Compute the address.
code.add(I_VARIABLE);
code.add(symbols.levelof(currid));
code.add(symbols[currid]->address());
// One copy of the address for the assignment, one for the result.
code.add_dup();
// Generate code for the value of the RHS.
eass->exp_->accept(this);
// Make sure expression and variable type match
if (symbols[variable_ident]->type() != currtype)
throw WrongAssignmentType(variable_ident, symbols[variable_ident]->type(), currtype);
else{
// Store the value at the computed address.
if(symbols[variable_ident]->type() == TY_INT)
code.add(I_ASSIGN);
else if(symbols[variable_ident]->type() == TY_DOUBLE){
code.add(R_ASSIGN);
}
else
throw(string("Unable to assign type " + type_id_to_str(symbols[variable_ident]->type())));
}
code.add(1);
// Dereference the address and return its value.
if(symbols[variable_ident]->type() == TY_INT){
code.add(I_VALUE);
}else{
code.add(R_VALUE);
}
}
void CodeGen::visitELt(ELt *elt)
{
elt->exp_1->accept(this);
type_t exp_1_type = currtype;
elt->exp_2->accept(this);
type_t exp_2_type = currtype;
if(exp_1_type != exp_2_type){
throw MixedTypes(string("<"), exp_1_type, exp_2_type);
}else{
if(exp_1_type == TY_INT)
code.add(I_LESS);
else{
code.add(R_LESS);
}
}
}
void CodeGen::visitEGt(EGt *egt)
{
egt->exp_1->accept(this);
type_t exp_1_type = currtype;
egt->exp_2->accept(this);
type_t exp_2_type = currtype;
if(exp_1_type != exp_2_type){
throw MixedTypes(string(">"), exp_1_type, exp_2_type);
}else{
if(exp_1_type == TY_INT)
code.add(I_GREATER);
else{
code.add(R_GREATER);
}
}
}
void CodeGen::visitEAdd(EAdd *eadd)
{
eadd->exp_1->accept(this);
type_t exp_1_type = currtype;
eadd->exp_2->accept(this);
type_t exp_2_type = currtype;
if(exp_1_type != exp_2_type){
throw MixedTypes(string("+"), exp_1_type, exp_2_type);
}else{
if(exp_1_type == TY_INT){
code.add(I_ADD);
}else{
code.add(R_ADD);
}
}
}
void CodeGen::visitESub(ESub *esub)
{
esub->exp_1->accept(this);
type_t exp_1_type = currtype;
esub->exp_2->accept(this);
type_t exp_2_type = currtype;
if(exp_1_type != exp_2_type){
throw MixedTypes(string("-"), exp_1_type, exp_2_type);
}else{
if(exp_1_type == TY_INT)
code.add(I_SUBTRACT);
else{
code.add(R_SUBTRACT);
}
}
}
void CodeGen::visitEMul(EMul *emul)
{
emul->exp_1->accept(this);
type_t exp_1_type = currtype;
emul->exp_2->accept(this);
type_t exp_2_type = currtype;
if(exp_1_type != exp_2_type){
throw MixedTypes(string("*"), exp_1_type, exp_2_type);
}else{
if(exp_1_type == TY_INT)
code.add(I_MULTIPLY);
else{
code.add(R_MULTIPLY);
}
}
}
void CodeGen::visitCall(Call *call)
{
// Store curr_arg_list in case nested function calls
vector<type_t> old_arg_type_list = curr_arg_list_types;
curr_arg_list_types.clear();
visitIdent(call->ident_);
if (!symbols.exists(currid))
throw UnknownFunc(currid);
int level = symbols.levelof(currid);
Symbol *function_symbol = symbols[currid];
int addr = function_symbol->address();
// Make room on the stack for the return value. Assumes all functions
// will return some value.
if(function_symbol->return_type == TY_DOUBLE){
code.add(R_CONSTANT);
}else{
code.add(I_CONSTANT);
}
code.add(0);
// Generate code for the expressions (which leaves their values on the
// stack when executed).
call->listexp_->accept(this);
// Check that expression types match the expected argument types
function_symbol->check_args(curr_arg_list_types);
code.add(I_CALL);
code.add(level);
code.add(addr);
// The result, if any, is left on the stack.
// Return type of call is return type of called function
currtype = function_symbol->return_type;
// Reset curr_arg_list in case nested function calls
curr_arg_list_types = old_arg_type_list;
}
void CodeGen::visitEVar(EVar *evar)
{
visitIdent(evar->ident_); // sets currid
if (!symbols.exists(currid))
throw UnknownVar(currid);
currtype = symbols[currid]->type();
if(currtype == TY_DOUBLE){
code.add(R_VARIABLE);
}else if(currtype == TY_INT){
code.add(I_VARIABLE);
}else{
throw(string("Unable to reference variable type ") + type_id_to_str(currtype));
}
// Compute the address.
code.add(symbols.levelof(currid));
code.add(symbols[currid]->address());
// Dereference it.
if(currtype == TY_DOUBLE){
code.add(R_VALUE);
}else if(currtype == TY_INT){
code.add(I_VALUE);
}
}
void CodeGen::visitEStr(EStr *estr)
{
currtype = TY_STR;
code.add(I_CONSTANT);
code.add(0); // must be patched for string address
visitString(estr->string_);
}
void CodeGen::visitEInt(EInt *eint)
{
visitInteger(eint->integer_);
currtype = TY_INT;
}
void CodeGen::visitEDouble(EDouble *edouble)
{
visitDouble(edouble->double_);
currtype = TY_DOUBLE;
}
void CodeGen::visitTInt(TInt *)
{
currtype = TY_INT;
}
void CodeGen::visitTDouble(TDouble *)
{
currtype = TY_DOUBLE;
}
void CodeGen::visitListFunction(ListFunction* listfunction)
{
// Generate code for each function in turn.
for (ListFunction::iterator i = listfunction->begin() ; i != listfunction->end() ; ++i)
{
(*i)->accept(this);
}
}
void CodeGen::visitListStm(ListStm* liststm)
{
// Generate code for each statement in turn.
for (ListStm::iterator i = liststm->begin() ; i != liststm->end() ; ++i)
{
(*i)->accept(this);
}
}
void CodeGen::visitListFDecl(ListFDecl* listdecl)
{
curr_arg_list_types.clear();
// ListFDecl is a function parameter list, so we can compute funargs here.
funargs = listdecl->size();
int currarg = 0;
for (ListFDecl::iterator i = listdecl->begin() ; i != listdecl->end() ; ++i)
{
(*i)->accept(this); // visitDec
// Store type of decleration in arg_list_types vector
curr_arg_list_types.push_back(currtype);
// The first argument (currarg = 0) has address -nargs; the last
// (currarg = nargs - 1) has address -1.
symbols[currid]->address() = currarg - funargs;
currarg++;
}
}
void CodeGen::visitListIdent(ListIdent* listident)
{
// Add all the identifiers to the symbol table. Assumes currtype is
// already set.
for (ListIdent::iterator i = listident->begin(); i != listident->end(); ++i)
{
visitIdent(*i); // sets currid
// First local variable (numvars = funargs) has address 3, etc.
// If this ListIdent is actually part of a parameter list, these
// addresses will be fixed up by visitListDecl.
symbols.insert(Symbol(currid, currtype, 3 + symbols.numvars() - funargs));
}
}
void CodeGen::visitListExp(ListExp* listexp)
{
curr_arg_list_types.clear();
// Evaluate each expression in turn, leaving all the values on the stack.
for (ListExp::iterator i = listexp->begin() ; i != listexp->end() ; ++i)
{
(*i)->accept(this);
// Store type of expression in arg_list_types vector
curr_arg_list_types.push_back(currtype);
}
}
void CodeGen::visitInteger(Integer x)
{
code.add(I_CONSTANT);
code.add(x);
}
void CodeGen::visitChar(Char x)
{
code.add(I_CONSTANT);
code.add(x);
}
void CodeGen::visitDouble(Double x)
{
code.add(R_CONSTANT);
code.add(x);
// Need to convert to double representation
code.add(I_TO_R);
}
void CodeGen::visitString(String x)
{
code.add_string(x, code.pos() - 1);
}
void CodeGen::visitIdent(Ident x)
{
currid = x;
}