-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen_code.c
More file actions
688 lines (574 loc) · 22 KB
/
gen_code.c
File metadata and controls
688 lines (574 loc) · 22 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
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#include "gen_code.h"
#define STACK_SPACE 4096
// Initialize the code generator
void gen_code_initialize()
{
literal_table_initialize();
}
// Write a sequence of instructions to the BOF file
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);
}
}
// Generate the BOF header
BOFHeader gen_code_program_header(code_seq main_cs) {
BOFHeader ret;
strncpy(ret.magic, "BO32", 4); // SPL magic number
ret.text_start_address = 0;
ret.text_length = code_seq_size(main_cs) * BYTES_PER_WORD;
ret.data_start_address = MAX(ret.text_length, 1024) + BYTES_PER_WORD;
ret.data_length = literal_table_size() * BYTES_PER_WORD;
ret.stack_bottom_addr = ret.data_start_address + ret.data_length + STACK_SPACE;
return ret;
}
// Write literals to the BOF file
void gen_code_output_literals(BOFFILE bf) {
literal_table_start_iteration();
while (literal_table_iteration_has_next()) {
word_type w = literal_table_iteration_next();
bof_write_word(bf, w);
}
literal_table_end_iteration();
}
// Requires: bf is open for writing in binary
// Write the program's BOFFILE to bf
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);
}
// Generate code for the entire program
void gen_code_program(BOFFILE bf, block_t prog) {
code_seq main_cs = code_utils_set_up_program();
// Handle variable declarations
code_seq var_decls_cs = gen_code_var_decls(prog.var_decls);
code_seq_concat(&main_cs, var_decls_cs);
// Handle constant declarations
code_seq const_decls_cs = gen_code_const_decls(prog.const_decls);
code_seq_concat(&main_cs, const_decls_cs);
// Generate code for statements
code_seq stmts_cs = gen_code_stmts(prog.stmts);
code_seq_concat(&main_cs, stmts_cs);
/* // Deallocate stack space for variables
int total_stack_space = calculate_total_stack_space(prog.var_decls);
code_seq dealloc_cs = code_utils_deallocate_stack_space(total_stack_space);
code_seq_concat(&main_cs, dealloc_cs); */
// Tear down activation record and add exit instruction
code_seq tear_down_cs = code_utils_tear_down_program();
code_seq_concat(&main_cs, tear_down_cs);
code_seq_add_to_end(&main_cs, code_exit(0));
gen_code_output_program(bf, main_cs);
}
/*
int calculate_total_stack_space(var_decls_t var_decls)
{
int total_space = 0;
var_decl_t *vdp = var_decls.var_decls;
while (vdp != NULL)
{
// Assuming each identifier takes up one word
int num_idents = count_idents(vdp->ident_list);
total_space += num_idents * BYTES_PER_WORD;
vdp = vdp->next;
}
return total_space;
}
int count_idents(ident_list_t idents)
{
int count = 0;
ident_t *idp = idents.start;
while (idp != NULL)
{
count++;
idp = idp->next;
}
return count;
} */
// Generate code for variable declarations
code_seq gen_code_var_decls(var_decls_t vds) {
code_seq ret = code_seq_empty();
var_decl_t *var_decl = vds.var_decls;
while (var_decl != NULL) {
code_seq var_decl_cs = gen_code_var_decl(*var_decl);
code_seq_concat(&ret, var_decl_cs);
var_decl = var_decl->next;
}
return ret;
}
// Generate code for a single variable declaration
code_seq gen_code_var_decl(var_decl_t vd) {
return gen_code_var_idents(vd.ident_list);
}
// Generate code for variable identifiers
code_seq gen_code_var_idents(ident_list_t idents) {
/*
Layout 2:
offset
[ ... ]
[ local variables ]
[ ... ]
FP -->[ local constants ] 0
[ saved SP ]-1
[ registers FP ]-2
[ link ]-3
[ RA ]-4
[ temporary storage ]
SP -->[ ... ]
How to initialize variables?
[allocate a word on the stack]
[zero out that word on the stack]
code_lit(SP, 0, 0)
SRI $sp, 1
LIT $sp, 0, 0
TRANSLATION SCHEME FOR VARIABLE NAMES
(AND CONSTANTS)
want to use CPW instruction to get the value
and put it on the stack
need the lexical address for finding the value
instructions in the SSM all use a base register
and an offset
I used $r3 for this
suppose the lexical address of the variable, x, is
(levelsOut, ofst)
# The following is done by code_utils_compute_fp
# load the FP for x's stack frame into $r3
[load FP into $r3] # this is for current AR
[load the next link into $r3] }
... } levelsOut times
[load the next link into $r3] }
# $r3 is the base of x's AR
[push x's value using $r3 as base onto stack]
CPW $sp, 0, $r3, ofst
*/
code_seq ret = code_seq_empty();
ident_t *ident = idents.start;
// Instructions are appended in reverse order
// so that the first declared identifier is allocated last
while (ident != NULL) {
// Allocate space for the identifier on the stack
code_seq alloc = code_utils_allocate_stack_space(1);
// Find the value of the variable by traversing the link
// and copying the value into the allocated space from the offset
// Using $r3 as the base register
lexical_address *ld = id_use_2_lexical_address(ident->idu);
code_seq base = code_utils_compute_fp(R3, ld->levelsOutward);
code_seq_add_to_end(&alloc, &base);
code_seq_add_to_end(&alloc, code_cpw(SP, 0, R3, ld->offsetInAR)); // Copy value from R3 + offset to SP
// Concatenate this identifier's code into the result sequence
code_seq_add_to_end(&alloc, code_lit(SP, 0, 0)); // Zero out the allocated space
code_seq_add_to_end(&alloc, code_sri(SP, 1)); // Increment SP
break;
// Concatenate this identifier's code into the result sequence
code_seq_concat(&ret, alloc);
// Move to the next identifier in the list
ident = ident->next;
}
return ret;
}
// Generate code for constant declarations
code_seq gen_code_const_decls(const_decls_t cds) {
code_seq ret = code_seq_empty();
const_decl_t *const_decl = cds.start;
while (const_decl != NULL) {
code_seq const_decl_cs = gen_code_const_decl(*const_decl);
code_seq_concat(&ret, const_decl_cs);
const_decl = const_decl->next;
}
return ret;
}
// Generate code for a single constant declaration
code_seq gen_code_const_decl(const_decl_t cd) {
return gen_code_const_def_list(cd.const_def_list);
}
// Generate code for a list of constant definitions
code_seq gen_code_const_def_list(const_def_list_t cdl) {
code_seq ret = code_seq_empty();
const_def_t *const_def = cdl.start;
while (const_def != NULL) {
code_seq const_def_cs = gen_code_const_def(*const_def);
code_seq_concat(&ret, const_def_cs);
const_def = const_def->next;
}
return ret;
}
// Generate code for a single constant definition
code_seq gen_code_const_def(const_def_t cd) {
/*
How to initialize constants?
use literal table to find each constant's offset
then copy from $gp+offset to the storage allocated
*/
ident_t ident = cd.ident;
// Allocate space for the identifier on the stack
code_seq alloc = code_utils_allocate_stack_space(1);
// Initialize constant using literal table
// Find the offset of the constant
unsigned int offset = id_use_get_attrs(ident.idu)->offset_count;
code_seq_add_to_end(&alloc, code_cpw(SP, 0, GP, offset)); // Load constant from GP + offset
}
// 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.stmt_list.start;
while (sp != NULL)
{
code_seq_concat(&ret, gen_code_stmt(*sp));
sp = sp->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 if_stmt:
return gen_code_if_stmt(stmt.data.if_stmt);
break;
case while_stmt:
return gen_code_while_stmt(stmt.data.while_stmt);
break;
case read_stmt:
return gen_code_read_stmt(stmt.data.read_stmt);
break;
case print_stmt:
return gen_code_print_stmt(stmt.data.print_stmt);
break;
case block_stmt:
return gen_code_block_stmt(stmt.data.block_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 an assignment statement
code_seq gen_code_assign_stmt(assign_stmt_t stmt) {
code_seq ret = code_seq_empty();
// Generate code for the RHS expression
code_seq rhs_code = gen_code_expr(*stmt.expr); // Evaluate stmt.expr and push result onto stack
code_seq_concat(&ret, rhs_code);
// Get lexical address information for the LHS variable
if (stmt.idu == NULL || id_use_get_attrs(stmt.idu) == NULL) {
bail_with_error("Invalid id_use or attributes in gen_code_assign_stmt!");
}
id_attrs *attrs = id_use_get_attrs(stmt.idu);
unsigned int levelsOut = stmt.idu->levelsOutward;
unsigned int offsetInAR = attrs->offset_count;
// Compute the frame pointer for the LHS variable
code_seq fp_code = code_utils_compute_fp(R3, levelsOut); // Load correct FP into $R3
code_seq_concat(&ret, fp_code);
// Store the RHS value into the memory location of the LHS variable
if (attrs->kind == variable_idk) {
code_seq_add_to_end(&ret, code_cpw(R3, offsetInAR, SP, 0)); // Store value in SP+0 to R3+offsetInAR
} else {
bail_with_error("Unsupported kind of identifier in gen_code_assign_stmt!");
}
return ret;
}
// Generate code for the if-statement
code_seq gen_code_if_stmt(if_stmt_t stmt) {
code_seq ret = code_seq_empty();
// Generate code for the condition
code_seq condition_code = gen_code_condition(stmt.condition); // Evaluate condition
code_seq_concat(&ret, condition_code);
// Conditional branching
int else_jump_offset = 0;
int end_jump_offset = 0;
if (stmt.else_stmts != NULL) {
else_jump_offset = code_seq_size(gen_code_stmts(*stmt.then_stmts)) + 2; // Jump over "then"
end_jump_offset = code_seq_size(gen_code_stmts(*stmt.else_stmts)) + 1; // Jump over "else"
} else {
else_jump_offset = code_seq_size(gen_code_stmts(*stmt.then_stmts)) + 1; // Jump to end
}
// Add branch instruction to skip "then" block if condition is false
code_seq_add_to_end(&ret, code_beq(SP, 0, else_jump_offset));
// Generate code for the "then" block
code_seq then_code = gen_code_stmts(*stmt.then_stmts); // Generate "then" stmts
code_seq_concat(&ret, then_code);
// If there’s an "else" block, jump past it
if (stmt.else_stmts != NULL) {
code_seq_add_to_end(&ret, code_jrel(end_jump_offset));
}
// Generate code for the "else" block (if present)
if (stmt.else_stmts != NULL) {
code_seq else_code = gen_code_stmts(*stmt.else_stmts); // Generate "else" stmts
code_seq_concat(&ret, else_code);
}
return ret;
}
code_seq gen_code_condition(condition_t cond) {
switch (cond.cond_kind) {
case ck_db:
return gen_code_db_condition(cond.data.db_cond);
case ck_rel:
return gen_code_rel_op_condition(cond.data.rel_op_cond);
default:
bail_with_error("Unsupported condition kind in gen_code_condition!");
}
}
// Generate code for a relational condition
code_seq gen_code_rel_op_condition(rel_op_condition_t cond) {
code_seq ret = code_seq_empty();
// Generate code for the two expressions
// Evaluate the two expressions and push the results onto the stack
code_seq expr1_code = gen_code_expr(cond.expr1);
code_seq expr2_code = gen_code_expr(cond.expr2);
// Apply the relational operator to the two expressions
code_seq rel_op_code = gen_code_rel_op(cond.rel_op);
// Concatenate the code sequences
code_seq_concat(&ret, rel_op_code);
return ret;
}
// Generate code for a divisible condition
code_seq gen_code_db_condition(db_condition_t cond) {
code_seq ret = code_seq_empty();
// Generate code for the two expressions
// Evaluate the two expressions and push the results onto the stack
code_seq divisor_code = gen_code_expr(cond.divisor); // SP+1
code_seq dividend_code = gen_code_expr(cond.dividend); // SP+0
// Apply the division operation to the two expressions
code_seq* div_op_code = code_div(SP, 1);
// Concatenate the code sequences
code_seq_concat(&ret, *div_op_code);
return ret;
}
// Generate code for a while statement
code_seq gen_code_while_stmt(while_stmt_t stmt) {
code_seq ret = code_seq_empty();
// Mark the start of the loop (loop entry)
int loop_entry_offset = code_seq_size(ret);
// Generate code for the condition
code_seq condition_code = gen_code_condition(stmt.condition);
code_seq_concat(&ret, condition_code);
// Add a conditional jump to skip the loop body if condition is false
int exit_offset = code_seq_size(gen_code_stmts(*stmt.body)) + 2; // Skip body and jump back
code_seq_add_to_end(&ret, code_beq(SP, 0, exit_offset)); // If false, jump out of loop
// Generate code for the loop body
code_seq body_code = gen_code_stmts(*stmt.body);
code_seq_concat(&ret, body_code);
// Add a jump back to the loop entry
int back_jump_offset = -(code_seq_size(condition_code) + code_seq_size(body_code) + 1);
code_seq_add_to_end(&ret, code_jrel(back_jump_offset)); // Jump to loop entry
// Mark the end of the loop (exit point)
return ret;
}
// Generate code for a read statement
code_seq gen_code_read_stmt(read_stmt_t stmt) {
code_seq ret = code_seq_empty();
// Use READ instruction to read input and push it onto the stack
code_seq_add_to_end(&ret, code_rch(SP, 0)); // Read input into stack top
// Get lexical address information for the variable
if (stmt.idu == NULL || id_use_get_attrs(stmt.idu) == NULL) {
bail_with_error("Invalid id_use or attributes in gen_code_read_stmt!");
}
id_attrs *attrs = id_use_get_attrs(stmt.idu);
unsigned int levelsOut = stmt.idu->levelsOutward;
unsigned int offsetInAR = attrs->offset_count;
// Compute the frame pointer for the variable
code_seq fp_code = code_utils_compute_fp(R3, levelsOut); // Load correct FP into $R3
code_seq_concat(&ret, fp_code);
// Store the read value from the stack into the variable's memory location
code_seq_add_to_end(&ret, code_cpw(R3, offsetInAR, SP, 0)); // Store value in SP+0 to R3+offsetInAR
return ret;
}
// Generate code for a print statement
code_seq gen_code_print_stmt(print_stmt_t stmt) {
code_seq ret = code_seq_empty();
// Generate code to evaluate the expression
code_seq expr_code = gen_code_expr(stmt.expr);
code_seq_concat(&ret, expr_code);
// Print the value at the top of the stack using
// PSTR instruction
code_seq_add_to_end(&ret, code_pstr(SP, 0)); // Print the value at SP+0
return ret;
}
// Generate code for the block statement given by stmt
code_seq gen_code_block_stmt(block_stmt_t stmt)
{
bail_with_error("gen_code_block_stmt not implemented!");
return;
}
// Generate code for the expression exp
// putting the result on top of the stack,
code_seq gen_code_expr(expr_t exp)
{
switch (exp.expr_kind)
{
case expr_bin:
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_negated:
return gen_code_negated(exp.data.negated);
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,
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));
code_seq_concat(&ret, gen_code_expr(*(exp.expr2)));
// do the operation, putting the result on the stack
code_seq_concat(&ret, gen_code_op(exp.arith_op));
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,
code_seq gen_code_op(token_t op)
{
switch (op.code)
{
case eqsym:
case neqsym:
case ltsym:
case leqsym:
case gtsym:
case geqsym:
return gen_code_rel_op(op);
break;
case plussym:
case minussym:
case multsym:
case divsym:
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 arithmetic operation to the top two stack elements
code_seq gen_code_arith_op(token_t arith_op) {
code_seq ret = code_seq_empty();
// Select the instruction based on the arithmetic operator
switch (arith_op.code) {
case plussym: {
// Add the second-to-top (SP+1) to the top of the stack (SP+0)
code_seq_add_to_end(&ret, code_add(SP, 1, SP, 0)); // v1 + v2
break;
}
case minussym: {
// Subtract the top of the stack (SP+0) from the second-to-top (SP+1)
code_seq_add_to_end(&ret, code_sub(SP, 1, SP, 0)); // v1 - v2
break;
}
case multsym: {
// Multiply the second-to-top (SP+1) by the top of the stack (SP+0)
code_seq_add_to_end(&ret, code_mul(SP, 1)); // Multiply SP+1 by SP
break;
}
case divsym: {
// Divide the second-to-top (SP+1) by the top of the stack (SP+0)
code_seq_add_to_end(&ret, code_div(SP, 1)); // Divide SP+1 by SP
break;
}
default:
bail_with_error("Unsupported arithmetic operator in gen_code_arith_op!");
}
// Adjust the stack pointer (pop one operand, leaving the result)
code_seq_add_to_end(&ret, code_ari(SP, 1)); // Deallocate 1 word from the stack
return ret;
}
// Generate code for a relational operator
code_seq gen_code_rel_op(token_t rel_op) {
code_seq ret = code_seq_empty();
// Subtract the top two stack elements (SP+1 - SP+0)
code_seq_add_to_end(&ret, code_sub(SP, 1, SP, 0)); // SP+1 - SP+0
// Generate conditional branching
switch (rel_op.code) {
case eqsym: { // ==
code_seq_add_to_end(&ret, code_beq(SP, 0, 3)); // If SP == 0, jump ahead 3 instructions
break;
}
case neqsym: { // !=
code_seq_add_to_end(&ret, code_bne(SP, 0, 3)); // If SP != 0, jump ahead 3 instructions
break;
}
case ltsym: { // <
code_seq_add_to_end(&ret, code_bltz(SP, 0, 3)); // If SP < 0, jump ahead 3 instructions
break;
}
case leqsym: { // <=
code_seq_add_to_end(&ret, code_blez(SP, 0, 3)); // If SP <= 0, jump ahead 3 instructions
break;
}
case gtsym: { // >
code_seq_add_to_end(&ret, code_bgtz(SP, 0, 3)); // If SP > 0, jump ahead 3 instructions
break;
}
case geqsym: { // >=
code_seq_add_to_end(&ret, code_bgez(SP, 0, 3)); // If SP >= 0, jump ahead 3 instructions
break;
}
default:
bail_with_error("Unsupported relational operator in gen_code_rel_op!");
}
// Push false (0) onto the stack if the condition is false
code_seq_add_to_end(&ret, code_lit(SP, 0, 0)); // Push 0 (false)
code_seq_add_to_end(&ret, code_jrel(2)); // Jump over the true case
// Push true (1) onto the stack if the condition is true
code_seq_add_to_end(&ret, code_lit(SP, 0, 1)); // Push 1 (true)
// Adjust the stack pointer
code_seq_add_to_end(&ret, code_ari(SP, 1)); // Pop one operand from the stack
return ret;
}
// Generate code to put the value of the given identifier
// on top of the stack
code_seq gen_code_ident(ident_t id)
{
// Find the value of the variable by traversing the link
// and copying the value into the allocated space from the offset
// Using $r3 as the base register
lexical_address *ld = id_use_2_lexical_address(id.idu);
code_seq ret = code_utils_compute_fp(R3, ld->levelsOutward);
code_seq_add_to_end(&ret, code_cpw(SP, 0, R3, ld->offsetInAR)); // Copy value from R3 + offset to SP
return ret;
}
// Generate code to put the given number on top of the stack
code_seq gen_code_number(number_t num)
{
unsigned int global_offset = literal_table_lookup(num.text, num.value);
code_seq ret = code_utils_allocate_stack_space(1);
code_seq_add_to_end(&ret, code_cpw(SP, 0, GP, global_offset));
return ret;
}
// Generate code for the expression exp
// putting the result on top of the stack,
code_seq gen_code_negated(negated_expr_t exp)
{
// evaluate the subexpression
code_seq ret = gen_code_expr(*exp.expr->data.negated.expr);
// negate the result
code_seq neg_res = code_seq_singleton(code_neg(SP, 0, SP, 0));
code_seq_add_to_end(&ret, &neg_res);
return ret;
}