-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_code.c
More file actions
448 lines (400 loc) · 14 KB
/
gen_code.c
File metadata and controls
448 lines (400 loc) · 14 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
/* $Id: gen_code_stubs.c,v 1.7 2023/03/27 16:39:12 leavens Exp $ */
#include "utilities.h"
#include "gen_code.h"
/* HOW TO COMPILE AND RUN THE CODE
// TO COMPILE
gcc -o compiler ast.c ast.h code.c code.h compiler_main.c file_location.c file_location.h gen_code.c gen_code.h id_attrs.c id_attrs.h id_use.c id_use.h instruction.c instruction.h label.c label.h lexer.c lexer.h lexer_output.c lexer_output.h lexical_address.c lexical_address.h parser.c parser.h reserved.c reserved.h scope.c scope.h scope_check.c scope_check.h symtab.c symtab.h token.c token.h unparser.c unparser.h utilities.c utilities.h
gcc compiler ast.c ast.h code.c code.h compiler_main.c file_location.c file_location.h gen_code.c gen_code.h id_attrs.c id_attrs.h id_use.c id_use.h instruction.c instruction.h label.c label.h lexer.c lexer.h lexer_output.c lexer_output.h lexical_address.c lexical_address.h parser.c parser.h reserved.c reserved.h scope.c scope.h scope_check.c scope_check.h symtab.c symtab.h token.c token.h unparser.c unparser.h utilities.c utilities.h
(must compile vm AND main code before running anything the first time)
//In the vm subdirectory, call cd vm first
gcc -o vm instruction.c machine.c machine_main.c stack.c utilities.c
// TO RUN
// PART 1
./compiler hw4-vmtest1.pl0 > hw4-vmtest1.myvi
or
make hw4-vmtest1.myvi
// PART 2
vm/vm hw4-vmtest1.myvi > hw4-vmtest1.myvo 2>&1
or
make hw4-vmtest1.myvo
All test cases
make check-outputs
*/
code_seq procs;
//Also a global data struct for the starting points of the procedures.
static int procBodysSize = 1;
static int constDecls = 0;
static int varDecls = 0;
// Initialize the code generator
void gen_code_initialize()
{
//bail_with_error("gen_code_initialize not implemented yet!");
}
// Generate code for the given AST
code_seq gen_code_program(AST *prog)
{
// ⟨program⟩ ::= ⟨block⟩ in pl0, so just go on to gen_code_block.
procs = code_seq_empty();
code_seq ret = gen_code_block(prog);
ret = code_seq_concat(procs, ret);
if(procBodysSize > 1){
ret = code_seq_concat(code_jmp(procBodysSize), ret);
}
return(ret);
}
// generate code for blk
code_seq gen_code_block(AST *blk)
{
code_seq ret = code_seq_singleton(code_inc(LINKS_SIZE));
ret = code_seq_concat(ret, gen_code_constDecls(blk->data.program.cds));
ret = code_seq_concat(ret, gen_code_varDecls(blk->data.program.vds));
//Proc decls returns void because they are stored in a global data structure.
gen_code_procDecls(blk->data.program.pds);
ret = code_seq_concat(ret, gen_code_stmt(blk->data.program.stmt));
ret = code_seq_add_to_end(ret, code_hlt());
return ret;
}
// generate code for the declarations in cds
code_seq gen_code_constDecls(AST_list cds)
{
code_seq ret = code_seq_empty();
while (!ast_list_is_empty(cds)) {
ret = code_seq_concat(ret, gen_code_constDecl(ast_list_first(cds)));
cds = ast_list_rest(cds);
}
return ret;
}
// generate code for the const declaration cd
code_seq gen_code_constDecl(AST *cd)
{
constDecls++;
code_seq ret = code_seq_singleton(code_lit(cd->data.const_decl.num_val));
return ret;
}
// generate code for the declarations in vds
code_seq gen_code_varDecls(AST_list vds)
{
code_seq ret = code_seq_empty();
while (!ast_list_is_empty(vds)) {
ret = code_seq_concat(ret, gen_code_varDecl(ast_list_first(vds)));
vds = ast_list_rest(vds);
}
return ret;
}
// generate code for the var declaration vd
code_seq gen_code_varDecl(AST *vd)
{
varDecls++;
return code_seq_singleton(code_inc(1));
}
// generate code for the declarations in pds
void gen_code_procDecls(AST_list pds)
{
while (!ast_list_is_empty(pds)) {
gen_code_procDecl(ast_list_first(pds));
pds = ast_list_rest(pds);
}
}
// ⟨proc-decl⟩ ::= procedure ⟨ident⟩ ; ⟨block⟩ ;
void gen_code_procDecl(AST *pd)
{
//Add the procedure for the AST to the array/linked list.
label_set(pd->data.proc_decl.lab, procBodysSize);
procs = code_seq_concat(procs, gen_code_procBlock(pd->data.proc_decl.block));
code_seq_fix_labels(procs);
}
// generate code for blk
code_seq gen_code_procBlock(AST *blk)
{
varDecls = 0;
constDecls = 0;
code_seq ret = code_seq_empty();
ret = code_seq_concat(ret, gen_code_constDecls(blk->data.program.cds));
ret = code_seq_concat(ret, gen_code_varDecls(blk->data.program.vds));
int totalSize = varDecls + constDecls;
//Proc decls returns void because they are stored in a global data structure.
gen_code_procDecls(blk->data.program.pds);
ret = code_seq_concat(ret, gen_code_stmt(blk->data.program.stmt));
if(totalSize > 0){
ret = code_seq_concat(ret, code_inc(-totalSize));
}
ret = code_seq_add_to_end(ret, code_rtn());
procBodysSize += code_seq_size(ret);
return ret;
}
// generate code for the statement
code_seq gen_code_stmt(AST *stmt)
{
switch (stmt->type_tag) {
case assign_ast:
return gen_code_assignStmt(stmt);
break;
case call_ast:
return gen_code_callStmt(stmt);
break;
case begin_ast:
return gen_code_beginStmt(stmt);
break;
case if_ast:
return gen_code_ifStmt(stmt);
break;
case while_ast:
return gen_code_whileStmt(stmt);
break;
case read_ast:
return gen_code_readStmt(stmt);
break;
case write_ast:
return gen_code_writeStmt(stmt);
break;
case skip_ast:
return gen_code_skipStmt(stmt);
break;
default:
bail_with_error("Bad AST passed to gen_code_stmt!");
// The following should never execute
return code_seq_empty();
}
}
// generate code for the statement
code_seq gen_code_assignStmt(AST *stmt)
{
/* design of code seq:
[get fp for the variable on top of stack]
[get value of expression on top of stack]
STO([offset for the variable])
*/
unsigned int outLevels = stmt->data.assign_stmt.ident->data.ident.idu->levelsOutward;
code_seq ret = code_compute_fp(outLevels);
ret = code_seq_concat(ret, gen_code_expr(stmt->data.assign_stmt.exp));
unsigned int ofst = stmt->data.assign_stmt.ident->data.ident.idu->attrs->loc_offset;
ret = code_seq_add_to_end(ret, code_sto(ofst));
return ret;
}
// TODO
// generate code for the statement
code_seq gen_code_callStmt(AST *stmt)
{
return(code_cal(stmt->data.call_stmt.ident->data.ident.idu->attrs->lab));
}
// TODO Correct begin stmt
// generate code for the statement
code_seq gen_code_beginStmt(AST *stmt)
{
/* design of code_seq
[save old BP on stack, PBP]
[adjust the BP]
[allocate variables declared]
[concatenated code for each stmt]
[if there are variables, pop them off the stack]
[RBP]
*/
// save the static link (surronging scope's BP) on stack
code_seq ret = code_seq_empty();
// add code for all the statements
AST_list stmts = stmt->data.begin_stmt.stmts;
while (!ast_list_is_empty(stmts)) {
ret = code_seq_concat(ret, gen_code_stmt(ast_list_first(stmts)));
stmts = ast_list_rest(stmts);
}
//if (num_vds > 0) {
// if there are variables, trim the variables
//ret = code_seq_add_to_end(ret, code_inc(- num_vds));
//}
return ret;
}
// generate code for the statement
code_seq gen_code_ifStmt(AST *stmt)
{
/* design:
[code for pushing the condition on top of stack]
JPC 2
JMP [around the body]
[code for the body]
*/
code_seq cond = gen_code_cond(stmt->data.if_stmt.cond);
code_seq ret = code_seq_add_to_end(cond, code_jpc(2));
code_seq thenbody = gen_code_stmt(stmt->data.if_stmt.thenstmt);
code_seq elsebody = gen_code_stmt(stmt->data.if_stmt.elsestmt);
ret = code_seq_add_to_end(ret, code_jmp(code_seq_size(thenbody) + 2));
ret = code_seq_concat(ret, thenbody);
ret = code_seq_add_to_end(ret, code_jmp(code_seq_size(elsebody) + 1));
ret = code_seq_concat(ret, elsebody);
return(ret);
/* Code given by professor
code_seq condc = gen_code_expr(stmt->data.if_stmt.exp);
code_seq bodyc = gen_code_stmt(stmt->data.if_stmt.stmt);
code_seq ret = code_seq_add_to_end(condc, code_jpc(2));
ret = code_seq_add_to_end(ret, code_jmp(code_seq_size(bodyc)+1));
ret = code_seq_concat(ret, bodyc);
return ret;
*/
}
// TODO fix whilestmt
// generate code for the statement
code_seq gen_code_whileStmt(AST *stmt)
{
// // Replace the following with your implementation
// code_seq cond = gen_code_whileStmt(stmt->data.while_stmt.cond);
// code_seq ret = code_seq_add_to_end(cond, code_jpc(2));
// code_seq thenbody = gen_code_stmt(stmt->data.if_stmt.thenstmt);
// code_seq elsebody = gen_code_stmt(stmt->data.if_stmt.elsestmt);
// ret = code_seq_add_to_end(ret, code_jmp(code_seq_size(thenbody) + 2));
// ret = code_seq_concat(ret, thenbody);
// ret = code_seq_add_to_end(ret, code_jmp(code_seq_size(elsebody) + 1));
// ret = code_seq_concat(ret, elsebody);
code_seq cond = gen_code_cond(stmt->data.while_stmt.cond);
code_seq ret = code_seq_add_to_end(cond, code_jpc(2));
code_seq stmtbody = gen_code_stmt(stmt->data.while_stmt.stmt);
ret = code_seq_add_to_end(ret, code_jmp(code_seq_size(stmtbody) + 2));
ret = code_seq_concat(ret, stmtbody);
ret = code_seq_add_to_end(ret, code_jmp( -(code_seq_size(cond)) ));
return(ret);
}
// generate code for the statement
code_seq gen_code_readStmt(AST *stmt)
{
/* design:
[code to put the fp for the variable on top of stack]
CHI
STO [(variable offset)]
*/
id_use *idu = stmt->data.read_stmt.ident->data.ident.idu;
code_seq ret = code_compute_fp(idu->levelsOutward);
ret = code_seq_add_to_end(ret, code_chi());
ret = code_seq_add_to_end(ret, code_sto(idu->attrs->loc_offset));
return ret;
}
// generate code for the statement
code_seq gen_code_writeStmt(AST *stmt)
{
/* design:
[code to put the exp's value on top of stack
CHO
*/
code_seq ret = gen_code_expr(stmt->data.write_stmt.exp);
return code_seq_add_to_end(ret, code_cho());
}
// generate code for the statement
code_seq gen_code_skipStmt(AST *stmt)
{
return code_nop();
}
// generate code for the condition
code_seq gen_code_cond(AST *cond)
{
// if (DEBUG) {
// fprintf(stderr, "generating condition: %s\n", cond->type_tag == odd_cond_ast ? "ODD" : "EVEN");
// }
if(cond->type_tag == odd_cond_ast){
return gen_code_odd_cond(cond);
}else{
return gen_code_bin_cond(cond);
}
}
// TODO fix this idk what should happen here
// generate code for the condition
code_seq gen_code_odd_cond(AST *cond)
{
code_seq ret = gen_code_expr(cond->data.odd_cond.exp);
ret = code_seq_concat(ret, code_lit(2));
ret = code_seq_concat(ret, code_mod());
return ret;
}
// generate code for the condition
code_seq gen_code_bin_cond(AST *cond)
{
code_seq ret = gen_code_expr(cond->data.bin_cond.leftexp);
ret = code_seq_concat(ret, gen_code_expr(cond->data.bin_cond.rightexp));
switch (cond->data.bin_cond.relop) {
case eqop:
return code_seq_add_to_end(ret, code_eql());
break;
case neqop:
return code_seq_add_to_end(ret, code_neq());
break;
case ltop:
return code_seq_add_to_end(ret, code_lss());
break;
case leqop:
return code_seq_add_to_end(ret, code_leq());
break;
case gtop:
return code_seq_add_to_end(ret, code_gtr());
break;
case geqop:
return code_seq_add_to_end(ret, code_geq());
break;
default:
bail_with_error("gen_code_bin_expr passed AST with bad relop!");
// The following should never execute
return code_seq_empty();
}
}
// TODO fix this for pl0.
// generate code for the expresion
code_seq gen_code_expr(AST *exp)
{
switch (exp->type_tag) {
case number_ast:
return gen_code_number_expr(exp);
break;
case ident_ast:
return gen_code_ident_expr(exp);
break;
case bin_expr_ast:
return gen_code_bin_expr(exp);
break;
default:
bail_with_error("gen_code_expr passed bad AST!");
// The following should never execute
return code_seq_empty();
break;
}
}
// generate code for the expression (exp)
code_seq gen_code_bin_expr(AST *exp)
{
/* design:
[code to push left exp's value on top of stack]
[code to push right exp's value on top of stack]
[instruction that implements the operation op]
*/
code_seq ret = gen_code_expr(exp->data.bin_expr.leftexp);
ret = code_seq_concat(ret, gen_code_expr(exp->data.bin_expr.rightexp));
switch (exp->data.bin_expr.arith_op) {
case addop:
return code_seq_add_to_end(ret, code_add());
break;
case subop:
return code_seq_add_to_end(ret, code_sub());
break;
case divop:
return code_seq_add_to_end(ret, code_div());
break;
case multop:
return code_seq_add_to_end(ret, code_mul());
break;
default:
bail_with_error("gen_code_bin_expr passed AST with bad op!");
// The following should never execute
return code_seq_empty();
}
}
// generate code for the ident expression (ident)
code_seq gen_code_ident_expr(AST *ident)
{
/* design:
[code to load fp for the variable]
LOD [offset for the variable]
*/
id_use *idu = ident->data.ident.idu;
lexical_address *la = lexical_address_create(idu->levelsOutward, idu->attrs->loc_offset);
return code_load_from_lexical_address(la);
}
// generate code for the number expression (num)
code_seq gen_code_number_expr(AST *num)
{
// TODO fix compilation error
return code_seq_singleton(code_lit((num->data.number.value)));
}