-
Notifications
You must be signed in to change notification settings - Fork 1
/
kittyir.c
1496 lines (1152 loc) · 42.5 KB
/
kittyir.c
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "kittyir.h"
#include "irInstructions.h"
#include "typechecker/stack.h"
#include "parserscanner/kittytree.h"
static int current_label = 0;
static int has_errors = 0;
static int has_prints = 0;
static int runtime_enabled = 0;
extern linked_list *ir_lines; // plug IR code in here
extern stackT *functionStack;
extern stackT *loopStack;
static linked_list *data_lines; // for allocates, we use variables
ARGUMENT *eax, *ebx, *ecx, *edx, *edi, *esi, *ebp, *esp;
ARGUMENT *one, *zero;
ARGUMENT *heapAddress, *heapFreePointer;
ARGUMENT *printFormInt, *printFormNull, *printFormTrue, *printFormFalse;
ARGUMENT *wordSizePointer;
ARGUMENT *printfLabel;
IR_INSTRUCTION *pushEax, *pushEbx, *pushEcx, *pushEdx, *pushEdi, *pushEsi,
*pushEbp, *pushEsp;
IR_INSTRUCTION *popEax, *popEbx, *popEcx, *popEdx, *popEdi, *popEsi, *popEbp,
*popEsp;
/*
* Initialization of common arguments
*/
void init_argument_constants() {
init_registers();
one = make_argument_constant(1);
zero = make_argument_constant(0);
heapAddress = make_argument_label("$heap.");
heapFreePointer = make_argument_label("heap.NEXT");
printFormFalse = make_argument_label("$form.FALSE");
printFormTrue = make_argument_label("$form.TRUE");
printFormNull = make_argument_label("$form.NULL");
printFormInt = make_argument_label("$form.NUM");
wordSizePointer = make_argument_plain_constant(WORD_SIZE);
printfLabel = make_argument_label("printf");
}
void init_stack_instructions() {
pushEax = make_instruction_pushl(eax);
pushEbx = make_instruction_pushl(ebx);
pushEcx = make_instruction_pushl(ecx);
pushEdx = make_instruction_pushl(edx);
pushEdi = make_instruction_pushl(edi);
pushEsi = make_instruction_pushl(esi);
pushEbp = make_instruction_pushl(ebp);
pushEsp = make_instruction_pushl(esp);
popEax = make_instruction_popl(eax);
popEbx = make_instruction_popl(ebx);
popEcx = make_instruction_popl(ecx);
popEdx = make_instruction_popl(edx);
popEdi = make_instruction_popl(edi);
popEsi = make_instruction_popl(esi);
popEbp = make_instruction_popl(ebp);
popEsp = make_instruction_popl(esp);
}
void init_registers() {
eax = make_argument_register("eax");
ebx = make_argument_register("ebx");
ecx = make_argument_register("ecx");
edx = make_argument_register("edx");
edi = make_argument_register("edi");
esi = make_argument_register("esi");
ebp = make_argument_register("ebp");
esp = make_argument_register("esp");
}
void init_heap() {
if ( get_length(data_lines) > 0 ) {
append_element(ir_lines, make_instruction_movl(heapAddress,
heapFreePointer));
}
}
void IR_build( BODY *program, int rtc ) {
fprintf(stderr, "Initializing intermediate code generation phase\n");
runtime_enabled = rtc;
data_lines = initialize_list();
init_argument_constants();
init_stack_instructions();
// adding text section for completion
append_element(ir_lines, make_instruction_directive(".text"));
IR_builder_decl_list(program->decl_list);
// make ".globl main" directive
append_element(ir_lines, make_instruction_directive(".globl main"));
// make "main:" label line
append_element(ir_lines, make_instruction_label("main"));
function_prolog(program->symbolTable);
init_heap();
IR_builder_statement_list(program->statement_list);
append_element(ir_lines, make_instruction_movl(zero, eax));
// return signal zero: all good
function_epilog();
program->symbolTable->localVars = 0; // resetting local variables counter
append_element(ir_lines, make_instruction_ret());
build_data_section();
}
void IR_builder_function(FUNC *func) {
funcStackPush(functionStack, func);
// we can now refer to the current function
// move the handling of the declaration list here instead of the body to
// avoid nested function getting generated inside each others
IR_builder_decl_list(func->body->decl_list);
// header makes the function label
IR_builder_head(func->head);
IR_builder_body(func->body);
func->symbolTable->localVars = 0; // reset local variables in scope
funcStackPop(functionStack);
// leaving the function
}
void IR_builder_head(HEAD *head) {
char *functionStartLabel;
SYMBOL *symbol = getSymbol(head->symbolTable, head->id);
GET_FUNCTION_LABEL(functionStartLabel, symbol->name, symbol->offset);
append_element(ir_lines, make_instruction_label(functionStartLabel));
}
void IR_builder_body (BODY *body) {
function_prolog(body->symbolTable);
IR_builder_statement_list(body->statement_list);
}
void IR_builder_var_decl_list ( VAR_DECL_LIST *vdecl) {
switch(vdecl->kind){
case VAR_DECL_LIST_LIST:
IR_builder_var_decl_list(vdecl->var_decl_list);
IR_builder_var_type(vdecl->var_type);
break;
case VAR_DECL_LIST_TYPE:
IR_builder_var_type(vdecl->var_type);
break;
}
}
void IR_builder_var_type ( VAR_TYPE * vtype ) {
// for int and bool we allocate place in stack.
// else we maybe have to heap it
switch (vtype->type->kind) {
case TYPE_INT:
if ( vtype->symbol->symbolKind == LOCAL_VARIABLE_SYMBOL ) {
vtype->symbolTable->localVars += WORD_SIZE;
}
break;
case TYPE_BOOL:
if ( vtype->symbol->symbolKind == LOCAL_VARIABLE_SYMBOL ) {
vtype->symbolTable->localVars += WORD_SIZE;
}
break;
default:
append_element(data_lines, vtype);
break;
}
}
void IR_builder_decl_list ( DECL_LIST *dlst ) {
switch(dlst->kind) {
case DECL_LIST_LIST:
IR_builder_decl_list(dlst->decl_list);
IR_builder_declaration(dlst->declaration);
break;
case DECL_LIST_EMPTY:
break;
}
}
void IR_builder_declaration ( DECLARATION *decl ) {
switch(decl->kind) {
case DECLARATION_ID:
break;
case DECLARATION_FUNC:
IR_builder_function(decl->value.function);
break;
case DECLARATION_VAR:
IR_builder_var_decl_list(decl->value.var_decl_list);
break;
}
}
void IR_builder_statement_list ( STATEMENT_LIST *slst ) {
switch(slst->kind) {
case STATEMENT_LIST_LIST:
IR_builder_statement_list(slst->statement_list);
IR_builder_statement(slst->statement);
break;
case STATEMENT_LIST_STATEMENT:
IR_builder_statement(slst->statement);
break;
default:
break;
}
}
void IR_builder_statement ( STATEMENT *st ) {
int labelIdCounter = 0;
int numberOfRecordMembers;
int noWriteArguments = 1;
char *endLabel;
char *elseLabel;
char *printLabel;
char *falseLabel;
char *trueLabel;
ARGUMENT *variable;
ARGUMENT *collection;
switch(st->kind) {
case STATEMENT_RETURN:
IR_builder_expression(st->value.statement_return.exp);
append_element(ir_lines, popEax);
function_epilog();
append_element(ir_lines, make_instruction_ret());
break;
case STATEMENT_WRITE:
has_prints = 1;
switch(st->value.exp->symbolType->type){
case SYMBOL_BOOL:
IR_builder_expression(st->value.exp);
// sub-expression
append_element(ir_lines, popEax);
labelIdCounter = GET_NEXT_LABEL_ID;
GET_FLOW_CONTROL_LABEL(falseLabel, "printFalse",
labelIdCounter);
GET_FLOW_CONTROL_LABEL(trueLabel, "printTrue",
labelIdCounter);
GET_FLOW_CONTROL_LABEL(printLabel,"printBool",
labelIdCounter);
// compare boolean value to true
append_element(ir_lines, make_instruction_cmp(one, eax));
// true has to be printed?
append_element(ir_lines,
make_instruction_jne(falseLabel));
// making a push to the stack with result of expression
append_element(ir_lines, pushEax);
// true case here
append_element(ir_lines, make_instruction_pushl(
printFormTrue));
// jump to printf call
append_element(ir_lines,
make_instruction_jmp(printLabel));
// false label section
append_element(ir_lines,
make_instruction_label(falseLabel));
// making a push to the stack with result of expression
append_element(ir_lines, pushEax);
// false case here
append_element(ir_lines, make_instruction_pushl(
printFormFalse));
// printing section
append_element(ir_lines, make_instruction_label(
printLabel));
break;
case SYMBOL_NULL:
append_element(ir_lines,
make_instruction_pushl(printFormNull));
break;
case SYMBOL_INT:
IR_builder_expression(st->value.exp);
// already on stack
noWriteArguments++;
append_element(ir_lines,
make_instruction_pushl(printFormInt));
break;
default:
break;
}
// call to print
append_element(ir_lines, make_instruction_call(printfLabel));
add_to_stack_pointer(noWriteArguments); // clean up
break;
case STATEMENT_ADDASSIGN:
case STATEMENT_SUBASSIGN:
case STATEMENT_MULASSIGN:
case STATEMENT_DIVASSIGN:
case STATEMENT_MODASSIGN:
case STATEMENT_ASSIGN:
IR_builder_expression(st->value.statement_assign.exp);
variable = IR_builder_variable(st->value.statement_assign.var);
append_element(ir_lines, popEbx);
// expression
switch (st->kind) {
case STATEMENT_ADDASSIGN:
append_element(ir_lines, make_instruction_movl(variable,
eax));
append_element(ir_lines, make_instruction_addl(ebx, eax));
append_element(ir_lines, make_instruction_movl(eax,
variable));
break;
case STATEMENT_SUBASSIGN:
append_element(ir_lines, make_instruction_movl(variable,
eax));
append_element(ir_lines, make_instruction_subl(ebx, eax));
append_element(ir_lines, make_instruction_movl(eax,
variable));
break;
case STATEMENT_MULASSIGN:
append_element(ir_lines, make_instruction_movl(variable,
eax));
append_element(ir_lines, make_instruction_imul(ebx, eax));
append_element(ir_lines, make_instruction_movl(eax,
variable));
break;
case STATEMENT_DIVASSIGN:
case STATEMENT_MODASSIGN:
append_element(ir_lines, make_instruction_movl(variable,
eax));
if(runtime_enabled){
division_by_zero_runtime_check(st->lineno, ebx);
}
append_element(ir_lines, make_instruction_xor(edx, edx));
// Clear edx for modulo
append_element(ir_lines, make_instruction_div(ebx));
// divide eax with eax to get result in eax
if ( st->kind == STATEMENT_DIVASSIGN ) {
append_element(ir_lines,
make_instruction_movl(eax, variable));
// result: quotient on the stack
} else {
append_element(ir_lines,
make_instruction_movl(edx, variable));
// else: remainder on stack
}
break;
case STATEMENT_ASSIGN:
append_element(ir_lines, make_instruction_movl(ebx,
variable));
break;
default:
break;
}
break;
case STATEMENT_IFBRANCH:
// generate code for boolean expression(s)
IR_builder_expression(st->value.statement_if_branch.condition);
append_element(ir_lines, popEax);
labelIdCounter = GET_NEXT_LABEL_ID;
GET_FLOW_CONTROL_LABEL(elseLabel, "else", labelIdCounter);
GET_FLOW_CONTROL_LABEL(endLabel, "endIf", labelIdCounter);
//Comparison with "true" boolean value
append_element(ir_lines, make_instruction_cmp(one, eax));
if (st->value.statement_if_branch.opt_else->kind !=
OPT_ELSE_EMPTY) {
// if not equal goto else part
append_element(ir_lines, make_instruction_jne(elseLabel));
} else {
// if not equal goto end-of-if
append_element(ir_lines,
make_instruction_jne(endLabel));
}
IR_builder_statement(st->value.statement_if_branch.statement);
// build statements in if-case
if (st->value.statement_if_branch.opt_else->kind !=
OPT_ELSE_EMPTY) {
// we have to jump over
//else when if-case is true
append_element(ir_lines,
make_instruction_jmp(endLabel));
// make else-label
append_element(ir_lines, make_instruction_label(elseLabel));
IR_builder_statement( // build else statements
st->value.statement_if_branch.opt_else->statement);
}
// end-of-if label
append_element(ir_lines, make_instruction_label(endLabel));
break;
case STATEMENT_ALLOCATE:
switch(st->value.statement_allocate.var->symboltype->type){
case SYMBOL_ARRAY:
// assume that the checker has checked if "length of" is
// present
variable = IR_builder_variable(
st->value.statement_allocate.var);
append_element(ir_lines, make_instruction_movl(
heapFreePointer, eax));
append_element(ir_lines,
make_instruction_movl(eax, variable));
// xored to get zero, aka the first index
append_element(ir_lines,
make_instruction_xor(ebx, ebx));
IR_builder_opt_length(st->value.statement_allocate
.opt_length);
append_element(ir_lines, popEcx);
if(runtime_enabled){
// edi and esi is free here
negative_array_size_check(st->lineno, ecx);
}
// move the array size to the first index
append_element(ir_lines, make_instruction_movl(ecx,
make_argument_indexing(NULL, eax, ebx)));
// array length + 1 since we use space
append_element(ir_lines,
make_instruction_incl(ecx));
// getting array size in bytes
append_element(ir_lines, make_instruction_imul(
make_argument_constant(WORD_SIZE), ecx));
if(runtime_enabled){
// eax is free, ebx is free
out_of_memory_runtime_check(st->lineno, ecx);
}
// update the heap free pointer
append_element(ir_lines, make_instruction_addl(ecx,
heapFreePointer));
break;
case SYMBOL_RECORD:
variable = IR_builder_variable(st->value.
statement_allocate.var);
// copy heapFree pointer address to variable
append_element(ir_lines, make_instruction_movl(
heapFreePointer, eax));
append_element(ir_lines,
make_instruction_movl(eax, variable));
numberOfRecordMembers = st->value.statement_allocate.
var->symboltype->arguments;
// we need number of members in record
append_element(ir_lines, make_instruction_movl(
make_argument_constant(numberOfRecordMembers)
, ecx));
append_element(ir_lines, make_instruction_imul(
make_argument_constant(WORD_SIZE), ecx));
if(runtime_enabled){
// eax and ebx is free
out_of_memory_runtime_check(st->lineno, ecx);
}
// add to the next pointer
append_element(ir_lines, make_instruction_addl(ecx,
heapFreePointer));
break;
default:
break;
}
break;
case STATEMENT_WHILE:
loopStackPush(loopStack, st);
labelIdCounter = GET_NEXT_LABEL_ID;
GET_FLOW_CONTROL_LABEL(st->start_label, "whileStart",
labelIdCounter);
GET_FLOW_CONTROL_LABEL(st->end_label, "whileEnd", labelIdCounter);
// while-start label insert
append_element(ir_lines, make_instruction_label(st->start_label));
// evaluating expressions
IR_builder_expression(st->value.statement_while.condition);
append_element(ir_lines, popEax);
//Compare evaluated expression with true
append_element(ir_lines, make_instruction_cmp(one, eax));
// jump to end if while condition is false
append_element(ir_lines, make_instruction_jne(st->end_label));
// generate code for statements
IR_builder_statement(st->value.statement_while.statement);
// repeating statement jump
append_element(ir_lines, make_instruction_jmp(
st->start_label));
// insertion of while-end
append_element(ir_lines, make_instruction_label(
st->end_label));
loopStackPop(loopStack);
break;
case STATEMENT_FOR:
loopStackPush(loopStack, st);
labelIdCounter = GET_NEXT_LABEL_ID;
GET_FLOW_CONTROL_LABEL(st->start_label, "forStart",
labelIdCounter);
GET_FLOW_CONTROL_LABEL(st->end_label, "forEnd", labelIdCounter);
IR_builder_statement(st->value.statement_for.assignment);
append_element(ir_lines, make_instruction_label(st->start_label));
IR_builder_expression(st->value.statement_for.condition);
append_element(ir_lines, popEax);
//Compare evaluated expression with true
append_element(ir_lines, make_instruction_cmp(one, eax));
// jump to end if while condition is false
append_element(ir_lines, make_instruction_jne(st->end_label));
// build statements
IR_builder_statement(st->value.statement_for.statement);
// the update statement
IR_builder_statement(st->value.statement_for.update);
// go back to start
append_element(ir_lines, make_instruction_jmp(st->start_label));
// end of for-loop
append_element(ir_lines, make_instruction_label(st->end_label));
loopStackPop(loopStack);
break;
case STATEMENT_FOREACH:
loopStackPush(loopStack, st);
labelIdCounter = GET_NEXT_LABEL_ID;
GET_FLOW_CONTROL_LABEL(st->start_label, "foreachStart",
labelIdCounter);
GET_FLOW_CONTROL_LABEL(st->end_label,
"foreachEnd", labelIdCounter);
// foreach preamble: get and push array size on stack,
// load element variable with the first element, save the current
// index. All in a days work.
collection = IR_builder_variable(st->value.statement_foreach.
collection);
append_element( ir_lines, make_instruction_movl(collection, ebx));
append_element( ir_lines, make_instruction_xor(eax, eax) );
append_element( ir_lines, make_instruction_movl(
make_argument_indexing(NULL, ebx, eax), ecx ) );
append_element( ir_lines, make_instruction_incl(eax) );
// START of foreach loop
append_element( ir_lines,
make_instruction_label(st->start_label) );
// compare index with array size
append_element( ir_lines, make_instruction_cmp(ecx, eax) );
append_element( ir_lines, make_instruction_jg(st->end_label) );
append_element( ir_lines, pushEcx); // save index and array size
append_element( ir_lines, pushEax);
append_element( ir_lines, pushEbx ); // save collection address
// get new element and put in variable
append_element( ir_lines, make_instruction_movl(
make_argument_indexing(NULL, ebx, eax), ebx) );
variable = IR_builder_variable(st->value.statement_foreach.
element );
append_element( ir_lines, make_instruction_movl(ebx, variable) );
// do statements
IR_builder_statement(st->value.statement_foreach.statement);
// update array with variable
variable = IR_builder_variable(st->value.statement_foreach.
element );
append_element(ir_lines, popEbx); // get address
append_element(ir_lines, popEax); // get index
append_element(ir_lines, popEcx); // get array size
append_element(ir_lines, make_instruction_movl(variable, edx));
append_element(ir_lines, make_instruction_movl(edx,
make_argument_indexing(NULL, ebx, eax)));
append_element( ir_lines, make_instruction_incl(eax));
append_element( ir_lines, make_instruction_jmp(st->start_label));
append_element( ir_lines, make_instruction_label(st->end_label));
loopStackPop(loopStack);
break;
case STATEMENT_LISTS:
IR_builder_statement_list(st->value.statement_list);
break;
case STATEMENT_BREAK:
append_element( ir_lines, make_instruction_jmp(
loopStackPeek(loopStack)->end_label) );
break;
case STATEMENT_CONTINUE:
append_element( ir_lines, make_instruction_jmp(
loopStackPeek(loopStack)->start_label) );
break;
}
}
void IR_builder_opt_length ( OPT_LENGTH *opt_length ) {
IR_builder_expression(opt_length->exp);
}
ARGUMENT *IR_builder_variable (VAR *var) {
int offsetValue;
int callScopeId;
int definedScopeId;
SYMBOL *symbol;
ARGUMENT *result;
ARGUMENT *offset;
ARGUMENT *base;
SYMBOL_TABLE *childTable;
switch ( var->kind ) {
case VAR_ID:
symbol = getSymbol(var->symboltable, var->id);
result = NULL;
if( symbol != NULL ) {
offsetValue = (symbol->offset) * WORD_SIZE;
if ( ( symbol->symbolType->type == SYMBOL_ARRAY ||
symbol->symbolType->type == SYMBOL_RECORD ) &&
!symbol->symbolKind == PARAMETER_SYMBOL ) {
// They're on the heap so we just use labels
result = make_argument_label(var->id);
} else if (symbol->tableId < var->symboltable->id) {
// basically, if variable is not in current,
// use static link
callScopeId = var->symboltable->id;
definedScopeId = symbol->tableId;
append_element(ir_lines, make_instruction_movl(
make_argument_address(8, ebp), ecx));
callScopeId--;
while ( definedScopeId != callScopeId) {
// get previous base pointer by iteration
append_element(ir_lines, make_instruction_movl(
make_argument_address(8, ecx), ecx));
callScopeId--;
}
if ( symbol->symbolKind == LOCAL_VARIABLE_SYMBOL ) {
result = make_argument_address( -1 * offsetValue,
ecx );
} else {
offsetValue = offsetValue + (2 * WORD_SIZE);
result = make_argument_address( offsetValue, ecx );
// beware of the return address on the stack
}
} else {
if ( symbol->symbolKind == LOCAL_VARIABLE_SYMBOL ) {
result = make_argument_address( -1 * offsetValue,
ebp );
} else {
offsetValue = offsetValue + (2 * WORD_SIZE);
result = make_argument_address( offsetValue, ebp );
// beware of the return address on the stack
}
}
}
return result;
case VAR_ARRAY:
base = IR_builder_variable(var->value.var_array.var);
append_element(ir_lines, make_instruction_movl(base, esi));
if(runtime_enabled){
null_pointer_runtime_check(var->lineno, esi);
}
IR_builder_expression(var->value.var_array.exp);
append_element(ir_lines, popEdi);
// exp
if(runtime_enabled){
out_of_bounds_runtime_check(var->lineno, esi, edi);
}
append_element(ir_lines, make_instruction_incl(edi));
// increment since we use the first element as the size
// return the indexing into the array
return make_argument_indexing(NULL, esi, edi);
case VAR_RECORD:
base = IR_builder_variable(var->value.var_record.var);
childTable = var->value.var_record.var->symboltype->childScope;
// This must be the child table
if( ( symbol = getSymbol(childTable, var->value.var_record.id) )
!= NULL) {
offset = make_argument_constant(symbol->offset - 1);
// member index in the record as argument
// member index is zero indiced
}
append_element(ir_lines, make_instruction_movl(base, esi));
if(runtime_enabled){
null_pointer_runtime_check(var->lineno, esi);
}
append_element(ir_lines, make_instruction_movl(offset, edi));
// returns much the same as arrays
return make_argument_indexing(NULL, esi, edi);
}
return NULL;
}
void IR_builder_expression ( EXPRES *exp ) {
char *trueLabel;
char *falseLabel;
char *endLabel;
int labelIdCounter = 0;
ARGUMENT *truth;
switch(exp->kind){
case EXPRES_TERM:
IR_builder_term(exp->value.term);
break;
case EXPRES_PLUS:
case EXPRES_MINUS:
case EXPRES_TIMES:
IR_builder_expression(exp->value.sides.left);
IR_builder_expression(exp->value.sides.right);
append_element(ir_lines, popEbx);
// rhs
append_element(ir_lines, popEax);
// lhs
switch (exp->kind) {
case EXPRES_PLUS:
append_element(ir_lines, make_instruction_addl(ebx, eax));
break;
case EXPRES_MINUS:
append_element(ir_lines, make_instruction_subl(ebx, eax));
break;
case EXPRES_TIMES:
append_element(ir_lines, make_instruction_imul(ebx, eax));
break;
default:
break;
}
append_element(ir_lines, pushEax);
break;
case EXPRES_DIVIDE:
case EXPRES_MODULO:
IR_builder_expression(exp->value.sides.left);
IR_builder_expression(exp->value.sides.right);
append_element(ir_lines, popEbx);
// rhs
append_element(ir_lines, popEax);
// lhs
if(runtime_enabled){
division_by_zero_runtime_check(exp->lineno, ebx);
}
append_element(ir_lines, make_instruction_xor(edx, edx));
// Clear edx for modulo
append_element(ir_lines, make_instruction_div(ebx));
// divide eax with eax to get result in eax
if ( exp->kind == EXPRES_DIVIDE ) {
append_element(ir_lines, pushEax);
// result: quotient on the stack
} else {
append_element(ir_lines, pushEdx);
// else: remainder on stack
}
break;
case EXPRES_EQ:
case EXPRES_NEQ:
case EXPRES_GREATER:
case EXPRES_LESS:
case EXPRES_LEQ:
case EXPRES_GEQ:
IR_builder_expression(exp->value.sides.left);
IR_builder_expression(exp->value.sides.right);
labelIdCounter = GET_NEXT_LABEL_ID;
GET_FLOW_CONTROL_LABEL(trueLabel, "boolOPtrue", labelIdCounter);
GET_FLOW_CONTROL_LABEL(endLabel, "boolOPend", labelIdCounter);
append_element(ir_lines, popEbx);
// rhs
append_element(ir_lines, popEax);
// lhs
append_element(ir_lines, make_instruction_cmp( ebx, eax ));
// compare both sides
IR_INSTRUCTION *trueJump;
switch(exp->kind){
case EXPRES_EQ:
trueJump = make_instruction_je(trueLabel);
break;
case EXPRES_NEQ:
trueJump = make_instruction_jne(trueLabel);
break;
case EXPRES_GREATER:
trueJump = make_instruction_jg(trueLabel);
break;
case EXPRES_LESS:
trueJump = make_instruction_jl(trueLabel);
break;
case EXPRES_LEQ:
trueJump = make_instruction_JLE(trueLabel);
break;
case EXPRES_GEQ:
trueJump = make_instruction_JGE(trueLabel);
break;
default:
break;
}
append_element(ir_lines, trueJump);
// the jump to true instruction
append_element(ir_lines, make_instruction_movl(zero, ebx));
// the false case
append_element(ir_lines, make_instruction_jmp(endLabel));
append_element(ir_lines, make_instruction_label(trueLabel));
append_element(ir_lines, make_instruction_movl(one, ebx));
// the true case
append_element(ir_lines, make_instruction_label(endLabel));
append_element(ir_lines, pushEbx);
break;
case EXPRES_AND:
labelIdCounter = GET_NEXT_LABEL_ID;
GET_FLOW_CONTROL_LABEL(falseLabel,"ANDfalse",labelIdCounter);
GET_FLOW_CONTROL_LABEL(endLabel,"ANDend",labelIdCounter);
truth = one;
IR_INSTRUCTION *jumpToFalse = make_instruction_jne(falseLabel);
// lazy evaluation on the false case
IR_builder_expression(exp->value.sides.right);
append_element(ir_lines, popEbx);
append_element(ir_lines, make_instruction_cmp(truth, ebx));
append_element(ir_lines, jumpToFalse);
// rhs
IR_builder_expression(exp->value.sides.left);
append_element(ir_lines, popEbx);
append_element(ir_lines, make_instruction_cmp(truth, ebx));
append_element(ir_lines, jumpToFalse);
// lhs
append_element(ir_lines,make_instruction_movl(truth, ebx));
append_element(ir_lines, make_instruction_jmp(endLabel));
// in case both arguments are true
append_element(ir_lines, make_instruction_label(falseLabel));
append_element(ir_lines, make_instruction_movl(zero, ebx));
// in case one of the arguments are false
append_element(ir_lines, make_instruction_label(endLabel));
append_element(ir_lines, pushEbx);
break;
case EXPRES_OR:
labelIdCounter = GET_NEXT_LABEL_ID;
GET_FLOW_CONTROL_LABEL(trueLabel, "ORtrue", labelIdCounter);
GET_FLOW_CONTROL_LABEL(endLabel, "ORend", labelIdCounter);
truth = one;
IR_INSTRUCTION *jumpToTrue = make_instruction_je(trueLabel);
// Note: using lazy evaluation here
IR_builder_expression(exp->value.sides.left);
append_element(ir_lines, popEbx);
append_element(ir_lines, make_instruction_cmp(truth, ebx));
append_element(ir_lines, jumpToTrue);
// LHS
IR_builder_expression(exp->value.sides.right);
append_element(ir_lines, popEbx);
append_element(ir_lines,make_instruction_cmp(truth, ebx));
append_element(ir_lines, jumpToTrue);
// RHS
append_element(ir_lines, make_instruction_movl(zero, ebx));
// false case
append_element(ir_lines, make_instruction_jmp(endLabel));
append_element(ir_lines, make_instruction_label(trueLabel));
append_element(ir_lines, make_instruction_movl(truth, ebx));
// true case
append_element(ir_lines, make_instruction_label(endLabel));
append_element(ir_lines, pushEbx);
break;
}
}
void IR_builder_term ( TERM *term ) {
SYMBOL *symbol;
ARGUMENT *variable;
char *positiveNumberLabel;