forked from eugenechevski/CodeGeneratorSPL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_code.c
More file actions
570 lines (534 loc) · 18 KB
/
gen_code.c
File metadata and controls
570 lines (534 loc) · 18 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
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
#include "gen_code.h"
#include <limits.h>
#include <string.h>
#include "spl.tab.h"
#include "ast.h"
#include "code.h"
#include "id_use.h"
#include "literal_table.h"
#include "gen_code.h"
#include "utilities.h"
#include "regname.h"
#define STACK_SPACE 4096
// Initialize the code generator
void gen_code_initialize()
{
literal_table_initialize();
}
// Requires: bf if open for writing in binary
// and prior to this scope checking and type checking have been done.
// Write all the instructions in cs to bf in order
static void gen_code_output_seq(BOFFILE bf, code_seq cs)
{
while (!code_seq_is_empty(cs))
{
bin_instr_t inst = code_seq_first(cs)->instr;
instruction_write_bin_instr(bf, inst);
cs = code_seq_rest(cs);
}
}
// Return a header appropriate for the give code
static BOFHeader gen_code_program_header(code_seq main_cs)
{
BOFHeader ret;
// magic
strncpy(ret.magic, "MAGIC", 4);
// text start address
ret.text_start_address = 0;
// text length
ret.text_length = code_seq_size(main_cs) * BYTES_PER_WORD;
// data start address
int dsa = MAX(ret.text_length, 1024) + BYTES_PER_WORD;
ret.data_start_address = dsa;
// data length
ret.data_length = literal_table_size() * BYTES_PER_WORD;
// stack bottom address
int sba = dsa + ret.data_length + STACK_SPACE;
ret.stack_bottom_addr = sba;
return ret;
}
static void gen_code_output_literals(BOFFILE bf)
{
literal_table_start_iteration();
while (literal_table_iteration_has_next())
{
word_type w = literal_table_iteration_next();
// debug_print("Writing literal %f to BOF file\n", w);
bof_write_word(bf, w);
}
literal_table_end_iteration();
}
// Requires: bf is open for writing in binary
// Write the program's BOFFILE to bf
static void gen_code_output_program(BOFFILE bf, code_seq main_cs)
{
BOFHeader bfh = gen_code_program_header(main_cs);
bof_write_header(bf, bfh);
gen_code_output_seq(bf, main_cs);
gen_code_output_literals(bf);
bof_close(bf);
}
// Requires: bf if open for writing in binary
// Generate code for prog into bf
void gen_code_program(BOFFILE bf, block_t prog)
{
code_seq main_cs;
// We want to make the main program's AR look like all blocks... so:
// allocate space and initialize any variables
main_cs = gen_code_var_decls(prog.var_decls);
int vars_len_in_bytes = (code_seq_size(main_cs) / 2) * BYTES_PER_WORD;
// there is no static link for the program as a whole,
// so nothing to do for saving FP into A0 as would be done for a block
code_seq_concat(main_cs, code_save_registers_for_AR());
code_seq_concat(main_cs,
gen_code_stmt(prog.stmt));
code_seq_concat(main_cs,
code_restore_registers_from_AR());
code_seq_concat(main_cs,
code_deallocate_stack_space(vars_len_in_bytes));
code_seq_add_to_end(main_cs, code_exit());
gen_code_output_program(bf, main_cs);
}
// Generate code for the var_decls_t vds to out
// There are 2 instructions generated for each identifier declared
// (one to allocate space and another to initialize that space)
code_seq gen_code_var_decls(var_decls_t vds)
{
code_seq ret = code_seq_empty();
var_decl_t *vdp = vds.var_decls;
while (vdp != NULL)
{
// generate these in reverse order,
// so the addressing offsets work properly
ret = code_seq_concat(gen_code_var_decl(*vdp), ret);
vdp = vdp->next;
}
return ret;
}
// Generate code for a single <var-decl>, vd,
// There are 2 instructions generated for each identifier declared
// (one to allocate space and another to initialize that space)
code_seq gen_code_var_decl(var_decl_t vd)
{
return gen_code_idents(vd.idents, vd.type);
}
// Generate code for the identififers in idents with type vt
// in reverse order (so the first declared are allocated last).
// There are 2 instructions generated for each identifier declared
// (one to allocate space and another to initialize that space)
code_seq gen_code_idents(idents_t idents,
type_exp_e vt)
{
code_seq ret = code_seq_empty();
ident_t *idp = idents.idents;
while (idp != NULL)
{
code_seq alloc_and_init = code_seq_singleton(code_addi(SP, SP,
-BYTES_PER_WORD));
switch (vt)
{
case float_te:
alloc_and_init = code_seq_add_to_end(alloc_and_init,
code_fsw(SP, 0, 0));
break;
case bool_te:
alloc_and_init = code_seq_add_to_end(alloc_and_init,
code_sw(SP, 0, 0));
break;
default:
bail_with_error("Bad type_exp_e (%d) passed to gen_code_idents!",
vt);
break;
}
// Generate these in revese order,
// so addressing works propertly
ret = code_seq_concat(alloc_and_init, ret);
idp = idp->next;
}
return ret;
}
// Generate code for stmt
code_seq gen_code_stmt(stmt_t stmt)
{
switch (stmt.stmt_kind)
{
case assign_stmt:
return gen_code_assign_stmt(stmt.data.assign_stmt);
break;
case begin_stmt:
return gen_code_begin_stmt(stmt.data.begin_stmt);
break;
case if_stmt:
return gen_code_if_stmt(stmt.data.if_stmt);
break;
case read_stmt:
return gen_code_read_stmt(stmt.data.read_stmt);
break;
case write_stmt:
return gen_code_write_stmt(stmt.data.write_stmt);
break;
default:
bail_with_error("Call to gen_code_stmt with an AST that is not a statement!");
break;
}
// The following can never execute, but this quiets gcc's warning
return code_seq_empty();
}
// Generate code for stmt
code_seq gen_code_assign_stmt(assign_stmt_t stmt)
{
// can't call gen_code_ident,
// since stmt.name is not an ident_t
code_seq ret;
// put value of expression in $v0
ret = gen_code_expr(*(stmt.expr));
assert(stmt.idu != NULL);
assert(id_use_get_attrs(stmt.idu) != NULL);
type_exp_e typ = id_use_get_attrs(stmt.idu)->type;
ret = code_seq_concat(ret, code_pop_stack_into_reg(V0, typ));
// put frame pointer from the lexical address of the name
// (using stmt.idu) into $t9
ret = code_seq_concat(ret,
code_compute_fp(T9, stmt.idu->levelsOutward));
unsigned int offset_count = id_use_get_attrs(stmt.idu)->offset_count;
assert(offset_count <= USHRT_MAX); // it has to fit!
switch (id_use_get_attrs(stmt.idu)->type)
{
case float_te:
ret = code_seq_add_to_end(ret,
code_fsw(T9, V0, offset_count));
break;
case bool_te:
ret = code_seq_add_to_end(ret,
code_sw(T9, V0, offset_count));
break;
default:
bail_with_error("Bad var_type (%d) for ident in assignment stmt!",
id_use_get_attrs(stmt.idu)->type);
break;
}
return ret;
}
// Generate code for stmt
code_seq gen_code_begin_stmt(begin_stmt_t stmt)
{
code_seq ret;
// allocate space and initialize any variables in block
ret = gen_code_var_decls(stmt.var_decls);
int vars_len_in_bytes = (code_seq_size(ret) / 2) * BYTES_PER_WORD;
// in FLOAT, surrounding scope's base is FP, so that is the static link
ret = code_seq_add_to_end(ret, code_add(0, FP, A0));
ret = code_seq_concat(ret, code_save_registers_for_AR());
ret = code_seq_concat(ret, gen_code_stmts(stmt.stmts));
ret = code_seq_concat(ret, code_restore_registers_from_AR());
ret = code_seq_concat(ret, code_deallocate_stack_space(vars_len_in_bytes));
return ret;
}
// Generate code for the list of statments given by stmts
code_seq gen_code_stmts(stmts_t stmts)
{
code_seq ret = code_seq_empty();
stmt_t *sp = stmts.stmts;
while (sp != NULL)
{
ret = code_seq_concat(ret, gen_code_stmt(*sp));
sp = sp->next;
}
return ret;
}
// Generate code for the if-statment given by stmt
code_seq gen_code_if_stmt(if_stmt_t stmt)
{
// put truth value of stmt.expr in $v0
code_seq ret = gen_code_expr(stmt.expr);
ret = code_seq_concat(ret, code_pop_stack_into_reg(V0, bool_te));
code_seq cbody = gen_code_stmt(*(stmt.body));
int cbody_len = code_seq_size(cbody);
// skip over body if $v0 contains false
ret = code_seq_add_to_end(ret,
code_beq(V0, 0, cbody_len));
return code_seq_concat(ret, cbody);
}
// Generate code for the read statment given by stmt
code_seq gen_code_read_stmt(read_stmt_t stmt)
{
// put number read into $v0
code_seq ret = code_seq_singleton(code_rch());
// put frame pointer from the lexical address of the name
// (using stmt.idu) into $t9
assert(stmt.idu != NULL);
ret = code_seq_concat(ret,
code_compute_fp(T9, stmt.idu->levelsOutward));
assert(id_use_get_attrs(stmt.idu) != NULL);
unsigned int offset_count = id_use_get_attrs(stmt.idu)->offset_count;
assert(offset_count <= USHRT_MAX); // it has to fit!
ret = code_seq_add_to_end(ret,
code_seq_singleton(code_fsw(T9, V0, offset_count)));
return ret;
}
// Generate code for the write statment given by stmt.
code_seq gen_code_write_stmt(write_stmt_t stmt)
{
// put the result into $a0 to get ready for PCH
code_seq ret = gen_code_expr(stmt.expr);
ret = code_seq_concat(ret, code_pop_stack_into_reg(A0, float_te));
ret = code_seq_add_to_end(ret, code_pflt());
return ret;
}
// Generate code for the expression exp
// putting the result on top of the stack,
// and using V0 and AT as temporary registers
// May also modify SP, HI,LO when executed
code_seq gen_code_expr(expr_t exp)
{
switch (exp.expr_kind)
{
case expr_bin_op:
return gen_code_binary_op_expr(exp.data.binary);
break;
case expr_ident:
return gen_code_ident(exp.data.ident);
break;
case expr_number:
return gen_code_number(exp.data.number);
break;
case expr_logical_not:
return gen_code_logical_not_expr(*(exp.data.logical_not));
break;
default:
bail_with_error("Unexpected expr_kind_e (%d) in gen_code_expr",
exp.expr_kind);
break;
}
// never happens, but suppresses a warning from gcc
return code_seq_empty();
}
// Generate code for the expression exp
// putting the result on top of the stack,
// and using V0 and AT as temporary registers
// May also modify SP, HI,LO when executed
code_seq gen_code_binary_op_expr(binary_op_expr_t exp)
{
// put the values of the two subexpressions on the stack
code_seq ret = gen_code_expr(*(exp.expr1));
ret = code_seq_concat(ret, gen_code_expr(*(exp.expr2)));
// check the types match
type_exp_e t1 = ast_expr_type(*(exp.expr1));
assert(ast_expr_type(*(exp.expr2)) == t1);
// do the operation, putting the result on the stack
ret = code_seq_concat(ret, gen_code_op(exp.op, t1));
return ret;
}
// Generate code to apply op to the
// 2nd from top and top of the stack,
// putting the result on top of the stack in their place,
// and using V0 and AT as temporary registers
// Modifies SP when executed
code_seq gen_code_op(token_t op, type_exp_e typ)
{
switch (op.code)
{
case eqsym:
case neqsym:
case ltsym:
case leqsym:
case gtsym:
case geqsym:
return gen_code_rel_op(op, typ);
break;
case plussym:
case minussym:
case multsym:
case divsym:
assert(typ == float_te);
return gen_code_arith_op(op);
break;
default:
bail_with_error("Unknown token code (%d) in gen_code_op",
op.code);
break;
}
return code_seq_empty();
}
// Generate code to apply the floating-point arith_op to the
// 2nd from top and top of the stack,
// putting the result on top of the stack in their place,
// and using V0 and AT as temporary registers
// Also modifies SP when executed
code_seq gen_code_arith_op(token_t arith_op)
{
// load top of the stack (the second operand) into AT
code_seq ret = code_pop_stack_into_reg(AT, float_te);
// load next element of the stack into V0
ret = code_seq_concat(ret, code_pop_stack_into_reg(V0, float_te));
code_seq do_op = code_seq_empty();
switch (arith_op.code)
{
case plussym:
do_op = code_seq_add_to_end(do_op, code_fadd(V0, AT, V0));
break;
case minussym:
do_op = code_seq_add_to_end(do_op, code_fsub(V0, AT, V0));
break;
case multsym:
do_op = code_seq_add_to_end(do_op, code_fmul(V0, AT, V0));
break;
case divsym:
do_op = code_seq_add_to_end(do_op, code_fdiv(V0, AT, V0));
break;
default:
bail_with_error("Unexpected arithOp (%d) in gen_code_arith_op",
arith_op.code);
break;
}
do_op = code_seq_concat(do_op, code_push_reg_on_stack(V0, float_te));
return code_seq_concat(ret, do_op);
}
// Generate code for the rel_op
// applied to 2nd from top and top of the stack,
// putting the result on top of the stack in their place,
// and using V0 and AT as temporary registers
// Also modifies SP when executed
code_seq gen_code_rel_op(token_t rel_op, type_exp_e typ)
{
// load top of the stack (the second operand) into AT
code_seq ret = code_pop_stack_into_reg(AT, typ);
// load next element of the stack into V0
ret = code_seq_concat(ret, code_pop_stack_into_reg(V0, typ));
// start out by doing the comparison
// and skipping the next 2 instructions if it's true
code_seq do_op = code_seq_empty();
switch (rel_op.code)
{
case eqsym:
if (typ == float_te)
{
do_op = code_seq_singleton(code_bfeq(V0, AT, 2));
}
else
{
do_op = code_seq_singleton(code_beq(V0, AT, 2));
}
break;
case neqsym:
if (typ == float_te)
{
do_op = code_seq_singleton(code_bfne(V0, AT, 2));
}
else
{
do_op = code_seq_singleton(code_bne(V0, AT, 2));
}
break;
case ltsym:
if (typ == float_te)
{
do_op = code_seq_singleton(code_fsub(V0, AT, V0));
do_op = code_seq_add_to_end(do_op, code_bfltz(V0, 2));
}
else
{
do_op = code_seq_singleton(code_sub(V0, AT, V0));
do_op = code_seq_add_to_end(do_op, code_bltz(V0, 2));
}
break;
case leqsym:
if (typ == float_te)
{
do_op = code_seq_singleton(code_fsub(V0, AT, V0));
do_op = code_seq_add_to_end(do_op, code_bflez(V0, 2));
}
else
{
do_op = code_seq_singleton(code_sub(V0, AT, V0));
do_op = code_seq_add_to_end(do_op, code_blez(V0, 2));
}
break;
case gtsym:
if (typ == float_te)
{
do_op = code_seq_singleton(code_fsub(V0, AT, V0));
do_op = code_seq_add_to_end(do_op, code_bfgtz(V0, 2));
}
else
{
do_op = code_seq_singleton(code_sub(V0, AT, V0));
do_op = code_seq_add_to_end(do_op, code_bgtz(V0, 2));
}
break;
case geqsym:
if (typ == float_te)
{
do_op = code_seq_singleton(code_fsub(V0, AT, V0));
do_op = code_seq_add_to_end(do_op, code_bfgez(V0, 2));
}
else
{
do_op = code_seq_singleton(code_sub(V0, AT, V0));
do_op = code_seq_add_to_end(do_op, code_bgez(V0, 2));
}
break;
default:
bail_with_error("Unknown token code (%d) in gen_code_rel_op",
rel_op.code);
break;
}
ret = code_seq_concat(ret, do_op);
// rest of the code for the comparisons
ret = code_seq_add_to_end(ret, code_add(0, 0, AT)); // put false in AT
ret = code_seq_add_to_end(ret, code_beq(0, 0, 1)); // skip next instr
ret = code_seq_add_to_end(ret, code_addi(0, AT, 1)); // put true in AT
ret = code_seq_concat(ret, code_push_reg_on_stack(AT, bool_te));
return ret;
}
// Generate code to put the value of the given identifier
// on top of the stack
// Modifies T9, V0, and SP when executed
code_seq gen_code_ident(ident_t id)
{
assert(id.idu != NULL);
code_seq ret = code_compute_fp(T9, id.idu->levelsOutward);
assert(id_use_get_attrs(id.idu) != NULL);
unsigned int offset_count = id_use_get_attrs(id.idu)->offset_count;
assert(offset_count <= USHRT_MAX); // it has to fit!
type_exp_e typ = id_use_get_attrs(id.idu)->type;
if (typ == float_te)
{
ret = code_seq_add_to_end(ret,
code_flw(T9, V0, offset_count));
}
else
{
ret = code_seq_add_to_end(ret,
code_lw(T9, V0, offset_count));
}
return code_seq_concat(ret, code_push_reg_on_stack(V0, typ));
}
// Generate code to put the given number on top of the stack
// Modifies V0 when executed
code_seq gen_code_number(number_t num)
{
unsigned int global_offset = literal_table_lookup(num.text, num.value);
return code_seq_concat(code_seq_singleton(code_flw(GP, V0, global_offset)),
code_push_reg_on_stack(V0, float_te));
}
// Generate code for the expression exp
// putting the result on top of the stack,
// and using V0 and AT as temporary registers
// May also modify SP, HI,LO when executed
code_seq gen_code_logical_not_expr(expr_t exp)
{
code_seq ret = gen_code_expr(exp);
ret = code_seq_concat(ret, code_pop_stack_into_reg(AT, bool_te));
// if 0 skip next 2 instructions
ret = code_seq_add_to_end(ret, code_beq(0, AT, 2));
// it was 1, so put 0 in AT
ret = code_seq_add_to_end(ret, code_add(0, 0, AT));
// and skip the next instruction
ret = code_seq_add_to_end(ret, code_beq(0, 0, 1));
// put 1 in AT
ret = code_seq_add_to_end(ret, code_addi(0, AT, 1));
// push the result on the stack
ret = code_seq_concat(ret, code_push_reg_on_stack(AT, bool_te));
return ret;
}