-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal.cpp
1237 lines (1155 loc) · 42.1 KB
/
final.cpp
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
/******************************************************************************
* C++ code file : final.cpp
* Project : Pazcal Compiler
* Written by : Lolos Konstantinos, Podimata Charikleia
* (lolos.kostis@gmail.com, charapod@gmail.com)
* Date : 2014-2015
* Description : Everything related to final code generation
******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "general.h"
#include "symbol.h"
#include "error.h"
#include "stdio.h"
#include "final.h"
#include <list>
#include <string>
using namespace std;
/* functions */
void translateQuad(quad);
void op_plus(quad);
void op_minus(quad);
void op_mult(quad);
void op_div(quad);
void op_mod(quad);
void op_equals(quad);
void op_not(quad); //unary operator
void op_not_equals(quad);
void op_gt(quad);
void op_lt(quad);
void op_ge(quad);
void op_le(quad);
void q_assign(quad);
void q_unit(quad);
void q_endu(quad);
void q_jump(quad);
void q_par(quad);
void q_call(quad);
void q_ret(quad);
void q_retv(quad);
void q_array(quad);
void reg_to_var(const char *reg, SymbolEntry *var);
void reg_to_var(const char *reg, KIND kind, int offset);
void var_to_reg(SymbolEntry *var, const char *reg);
int offset(SymbolEntry *var);
const char * ptr_type(int kind);
const char * mov_op(int kind);
void preamble(vector<quad> quads);
void allocate_constants(vector<quad> quads);
void allocate_globals(vector<quad> quads);
void init_globals(vector<quad> quads);
void bool_to_reg(SymbolEntry *var, const char * reg);
void reg_to_bool(const char * reg, SymbolEntry *var);
void fstack_to_var(SymbolEntry *var);
void fstack_to_var(KIND kind, int offset, bool by_ref);
void push_real_to_fstack(SymbolEntry *real);
void push_int_to_fstack(SymbolEntry *var);
void push_char_to_fstack(SymbolEntry *var);
void alloc_data(SymbolEntry *var);
void alloc_global(SymbolEntry *var);
void push_from_fstack();
const char * quad_to_s(quad q);
void translate_comparison(quad q);
/* global variables */
Type curRetType;
int real_counter = 0;
int str_counter = 0;
int byte_counter = 0;
int int_counter = 0;
/* Generates the final code from the given list of quads */
void generateFinalCode(vector<quad> quads)
{
unsigned int i;
preamble(quads);
bool inFunction = false;
for (i = 0; i < quads.size(); ++i) {
if (equal(quads[i].op, "unit"))
inFunction = true;
if (inFunction) {
fprintf(asm_out, "__L%d: # %s\n", i, quad_to_s(quads[i]));
translateQuad(quads[i]);
}
if (equal(quads[i].op, "endu"))
inFunction = false;
}
}
/* String representation for quads for final code comments */
const char *quad_to_s(quad q)
{
int len = strlen(q.op) + strlen(q.x) + strlen(q.y) + strlen(q.z) + 10;
char *s = (char *) newAlloc(len);
sprintf(s, "%s, %s, %s, %s", q.op, q.x, q.y, q.z);
return s;
}
/* Creates the first part of the assembly code.
Allocates all needed memory and calls the main function */
void preamble(vector<quad> quads)
{
fprintf(asm_out, ".section .data\n");
allocate_constants(quads);
allocate_globals(quads);
fprintf(asm_out, ".section .text\n");
fprintf(asm_out, ".globl _start\n");
fprintf(asm_out, "\n");
fprintf(asm_out, "_start: \n");
fprintf(asm_out, "\tmovl\t%%esp, %%ebp\n");
init_globals(quads);
fprintf(asm_out, "\n");
fprintf(asm_out, "\tpushl\t%%ebp\n");
fprintf(asm_out, "\tcall\t__main\n");
fprintf(asm_out, "\n");
fprintf(asm_out, "\taddl\t$4, %%esp\n");
fprintf(asm_out, "\tmovl\t$1, %%eax\n");
fprintf(asm_out, "\tmovl\t$0, %%ebx\n");
fprintf(asm_out, "\tint\t$0x80\n");
fprintf(asm_out, "\n");
}
/* Allocates memory for all constants in the code */
void allocate_constants(vector<quad> quads)
{
for (auto q : quads) {
alloc_data(q.xe);
alloc_data(q.ye);
alloc_data(q.ze);
}
}
/* Allocates memory for the given valiable if it is a constnant
and has not yet been allocated. */
void alloc_data(SymbolEntry *var)
{
if (!var || var->final_code_name)
return;
if (isConst(var) && is_real_type(var)) {
char * name = (char *) newAlloc(20);
sprintf(name, "__fl%d", real_counter++);
fprintf(asm_out, "%s: .tfloat %Lf\n", name, get_real_val(var));
var->final_code_name = name;
}
else if (isConst(var) && isArray(var)) { //string literal
char * name = (char *) newAlloc(20);
sprintf(name, "__str%d", str_counter++);
fprintf(asm_out, "%s: .string \"%s\"\n", name, get_str_val(var));
var->final_code_name = name;
}
}
/* Allocates memory for all global variables */
void allocate_globals(vector<quad> quads)
{
for (auto q : quads) {
alloc_global(q.xe);
alloc_global(q.ye);
alloc_global(q.ze);
}
}
/* Allocates memory for the given variable if it is a global
and has not yet been allocated. */
void alloc_global(SymbolEntry *var)
{
if (!var || var->final_code_name || !var->isGlobal)
return;
int c;
char *name;
RepReal r;
KIND k = var_kind(var);
switch (k) {
case TYPE_BOOLEAN:
c = 0;
if (isConst(var))
c = bool_val(var) ? 1 : 0;
name = (char *) newAlloc(20);
sprintf(name, "__byte%d", byte_counter++);
var->final_code_name = name;
fprintf(asm_out, "%s: .byte %d\n", name, c);
break;
case TYPE_CHAR:
c = 0;
if (isConst(var))
c = char_val(var);
name = (char *) newAlloc(20);
sprintf(name, "__byte%d", byte_counter++);
var->final_code_name = name;
fprintf(asm_out, "%s: .byte %d\n", name, c);
break;
case TYPE_INTEGER:
c = 0;
if (isConst(var))
c = int_val(var);
name = (char *) newAlloc(20);
sprintf(name, "__int%d", int_counter++);
var->final_code_name = name;
fprintf(asm_out, "%s: .long %d\n", name, c);
break;
case TYPE_ARRAY:
name = (char *) newAlloc(20);
sprintf(name, "__byte%d", byte_counter++);
var->final_code_name = name;
fprintf(asm_out, "%s:\n", name);
c = sizeOfType(var_type(var));
fprintf(asm_out, ".rept\t%d\n", c);
fprintf(asm_out, ".byte\t0\n");
fprintf(asm_out, ".endr\n");
break;
case TYPE_IARRAY:
internal("alloc global called with IARRAY type");
break;
case TYPE_REAL:
r = 0;
if (isConst(var))
r = get_real_val(var);
name = (char *) newAlloc(20);
sprintf(name, "__fl%d", real_counter++);
var->final_code_name = name;
fprintf(asm_out, "%s: .tfloat %Lf\n", name, r);
break;
default:
internal("alloc_global called with incompatible var type");
}
}
/* Generates the code that does the global variable initialization.
This code goes in the preamble and gets executed before calling the main function. */
void init_globals(vector<quad> quads)
{
unsigned int i;
bool inFunction = false;
for (i = 0; i < quads.size(); ++i) {
if (equal(quads[i].op, "unit"))
inFunction = true;
if (!inFunction) {
fprintf(asm_out, "__L%d: # %s\n", i, quad_to_s(quads[i]));
translateQuad(quads[i]);
}
if (equal(quads[i].op, "endu"))
inFunction = false;
}
}
/* Translates a quad to final code */
void translateQuad(quad q)
{
if (equal(q.op, "+")) op_plus(q);
else if (equal(q.op, "-")) op_minus(q);
else if (equal(q.op, "*")) op_mult(q);
else if (equal(q.op, "/")) op_div(q);
else if (equal(q.op, "%")) op_mod(q);
else if (equal(q.op, "==")) op_equals(q);
else if (equal(q.op, "not")) op_not(q);
else if (equal(q.op, "!=")) op_not_equals(q);
else if (equal(q.op, ">")) op_gt(q);
else if (equal(q.op, "<")) op_lt(q);
else if (equal(q.op, ">=")) op_ge(q);
else if (equal(q.op, "<=")) op_le(q);
else if (equal(q.op, ":=")) q_assign(q);
else if (equal(q.op, "unit")) q_unit(q);
else if (equal(q.op, "endu")) q_endu(q);
else if (equal(q.op, "jump")) q_jump(q);
else if (equal(q.op, "par")) q_par(q);
else if (equal(q.op, "call")) q_call(q);
else if (equal(q.op, "ret")) q_ret(q);
else if (equal(q.op, "retv")) q_retv(q);
else if (equal(q.op, "array")) q_array(q);
else if (equal(q.op, "no_op")); // do nothing
else internal("Unknown quad op: %s", q.op);
}
/* Addition quad */
void op_plus(quad q)
{
KIND x_type = var_kind(q.xe);
KIND y_type = var_kind(q.ye);
if (x_type != TYPE_REAL && y_type != TYPE_REAL) {
var_to_reg(q.ye, "%ebx");
var_to_reg(q.xe, "%eax");
fprintf(asm_out, "\taddl\t%%ebx, %%eax\n");
reg_to_var("%eax", q.ze);
}
else if (x_type == TYPE_INTEGER && y_type == TYPE_REAL) {
push_int_to_fstack(q.xe);
push_real_to_fstack(q.ye);
fprintf(asm_out, "\tfaddp\t%%st(1), %%st(0)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_CHAR && y_type == TYPE_REAL) {
push_char_to_fstack(q.xe);
push_real_to_fstack(q.ye);
fprintf(asm_out, "\tfaddp\t%%st(1), %%st(0)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_REAL && y_type == TYPE_INTEGER) {
push_real_to_fstack(q.xe);
push_int_to_fstack(q.ye);
fprintf(asm_out, "\tfaddp\t%%st(1), %%st(0)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_REAL && y_type == TYPE_CHAR) {
push_real_to_fstack(q.xe);
push_char_to_fstack(q.ye);
fprintf(asm_out, "\tfaddp\t%%st(1), %%st(0)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_REAL && y_type == TYPE_REAL) {
push_real_to_fstack(q.ye);
push_real_to_fstack(q.xe);
fprintf(asm_out, "\tfaddp\t%%st(1)\n");
fstack_to_var(q.ze);
}
}
/* Minus quad */
void op_minus(quad q)
{
KIND x_type = var_kind(q.xe);
KIND y_type = var_kind(q.ye);
if (x_type != TYPE_REAL && y_type != TYPE_REAL) {
var_to_reg(q.xe, "%eax");
var_to_reg(q.ye, "%ebx");
fprintf(asm_out, "\tsubl\t%%ebx, %%eax\n");
reg_to_var("%eax", q.ze);
}
else if (x_type == TYPE_INTEGER && y_type == TYPE_REAL) {
push_real_to_fstack(q.ye);
push_int_to_fstack(q.xe);
fprintf(asm_out, "\tfsubp\t%%st(1)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_CHAR && y_type == TYPE_REAL) {
push_real_to_fstack(q.ye);
push_char_to_fstack(q.xe);
fprintf(asm_out, "\tfsubp\t%%st(1)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_REAL && y_type == TYPE_INTEGER) {
push_int_to_fstack(q.ye);
push_real_to_fstack(q.xe);
fprintf(asm_out, "\tfsubp\t%%st(1)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_REAL && y_type == TYPE_CHAR) {
push_char_to_fstack(q.ye);
push_real_to_fstack(q.xe);
fprintf(asm_out, "\tfsubp\t%%st(1)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_REAL && y_type == TYPE_REAL) {
push_real_to_fstack(q.ye);
push_real_to_fstack(q.xe);
fprintf(asm_out, "\tfsubp\t%%st(1)\n");
fstack_to_var(q.ze);
}
}
/* Multiplication quad */
void op_mult(quad q)
{
KIND x_type = var_kind(q.xe);
KIND y_type = var_kind(q.ye);
if (x_type != TYPE_REAL && y_type != TYPE_REAL) {
var_to_reg(q.xe, "%eax");
var_to_reg(q.ye, "%ebx");
fprintf(asm_out, "\tmul\t\t%%ebx\n");
reg_to_var("%eax", q.ze);
}
else if (x_type == TYPE_INTEGER && y_type == TYPE_REAL) {
push_int_to_fstack(q.xe);
push_real_to_fstack(q.ye);
fprintf(asm_out, "\tfmulp\t%%st(1)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_CHAR && y_type == TYPE_REAL) {
push_real_to_fstack(q.ye);
push_char_to_fstack(q.xe);
fprintf(asm_out, "\tfmulp\t%%st(1)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_REAL && y_type == TYPE_INTEGER) {
push_int_to_fstack(q.ye);
push_real_to_fstack(q.xe);
fprintf(asm_out, "\tfmulp\t%%st, %%st(1)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_REAL && y_type == TYPE_CHAR) {
push_char_to_fstack(q.ye);
push_real_to_fstack(q.xe);
fprintf(asm_out, "\tfmulp\t%%st(1)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_REAL && y_type == TYPE_REAL) {
push_real_to_fstack(q.ye);
push_real_to_fstack(q.xe);
fprintf(asm_out, "\tfmulp\t%%st(1)\n");
fstack_to_var(q.ze);
}
}
/* Division quad */
void op_div(quad q)
{
KIND x_type = var_kind(q.xe);
KIND y_type = var_kind(q.ye);
if (x_type != TYPE_REAL && y_type != TYPE_REAL) {
fprintf(asm_out, "\tmovl\t$0, %%edx\n");
var_to_reg(q.xe, "%eax");
var_to_reg(q.ye, "%ebx");
fprintf(asm_out, "\tidiv\t%%ebx\n");
reg_to_var("%eax", q.ze);
}
else if (x_type == TYPE_INTEGER && y_type == TYPE_REAL) {
push_real_to_fstack(q.ye);
push_int_to_fstack(q.xe);
fprintf(asm_out, "\tfdivp\t%%st(1)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_CHAR && y_type == TYPE_REAL) {
push_real_to_fstack(q.ye);
push_char_to_fstack(q.xe);
fprintf(asm_out, "\tfdivp\t%%st(1)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_REAL && y_type == TYPE_INTEGER) {
push_int_to_fstack(q.ye);
push_real_to_fstack(q.xe);
fprintf(asm_out, "\tfdivp\t%%st(1)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_REAL && y_type == TYPE_CHAR) {
push_char_to_fstack(q.ye);
push_real_to_fstack(q.xe);
fprintf(asm_out, "\tfdivp\t%%st(1)\n");
fstack_to_var(q.ze);
}
else if (x_type == TYPE_REAL && y_type == TYPE_REAL) {
push_real_to_fstack(q.ye);
push_real_to_fstack(q.xe);
fprintf(asm_out, "\tfdivp\t%%st(1)\n");
fstack_to_var(q.ze);
}
}
/* Modulo quad */
void op_mod(quad q)
{
KIND x_type = var_kind(q.xe);
KIND y_type = var_kind(q.ye);
if (x_type != TYPE_REAL && y_type != TYPE_REAL) {
fprintf(asm_out, "\tmovl\t$0, %%edx\n");
var_to_reg(q.xe, "%eax");
var_to_reg(q.ye, "%ebx");
fprintf(asm_out, "\tidiv\t%%ebx\n");
reg_to_var("%edx", q.ze); // in x86, dx:ax has the result for division
}
else
internal("Mod applied to non integer values.");
}
/* Final code for unary operator "not" or "!".
Operand q.xe is always a boolean. */
void op_not(quad q)
{
bool_to_reg(q.xe, "%al");
fprintf(asm_out, "\tcmpb\t$0, %%al\n");
fprintf(asm_out, "\tsete\t%%al\n");
reg_to_var("%al", q.ze);
/*if (isGlobal(q.xe)) {
bool_to_reg(q.xe, "%al");
fprintf(asm_out, "\tcmpb\t$0, %%al\n");
fprintf(asm_out, "\tsete\t%%al\n");
}
else {
fprintf(asm_out, "\tcmpb\t$0, %d(%%ebp)\n", offset(q.xe));
fprintf(asm_out, "\tsete\t%%al\n");
}
reg_to_var("%al", q.ze);*/
}
/* Equals comparison quad */
void op_equals(quad q)
{
translate_comparison(q);
if (q.ze) { // it is assigning the value to a temp variable
fprintf(asm_out, "\tcmove\t%%edx, %%ecx\n");
reg_to_var("%cl", q.ze);
}
else { // it jumps to the quad number q.z
fprintf(asm_out, "\tje\t__L%s\n", q.z);
}
}
/* Not equals comparison quad */
void op_not_equals(quad q)
{
translate_comparison(q);
if (q.ze) { // it is assigning the value to a temp variable
fprintf(asm_out, "\tcmovne\t%%edx, %%ecx\n");
reg_to_var("%cl", q.ze);
}
else { // it jumps to the quad number q.z
fprintf(asm_out, "\tjne\t__L%s\n", q.z);
}
}
/* Greater than quad */
void op_gt(quad q)
{
translate_comparison(q);
if (is_real_type(q.xe) || is_real_type(q.ye)) {
if (q.ze) { // it is assigning the value to a temp variable
fprintf(asm_out, "\tcmovnbe\t%%edx, %%ecx\n");
reg_to_var("%cl", q.ze);
}
else { // it jumps to the quad number q.z
fprintf(asm_out, "\tjg\t__L%s\n", q.z);
}
}
else {
if (q.ze) { // it is assigning the value to a temp variable
fprintf(asm_out, "\tcmovg\t%%edx, %%ecx\n");
reg_to_var("%cl", q.ze);
}
else { // it jumps to the quad number q.z
fprintf(asm_out, "\tjg\t__L%s\n", q.z);
}
}
}
/* Less than quad */
void op_lt(quad q)
{
translate_comparison(q);
if (is_real_type(q.xe) || is_real_type(q.ye)) {
if (q.ze) { // it is assigning the value to a temp variable
fprintf(asm_out, "\tcmovb\t%%edx, %%ecx\n");
reg_to_var("%cl", q.ze);
}
else { // it jumps to the quad number q.z
fprintf(asm_out, "\tjl\t__L%s\n", q.z);
}
}
else {
if (q.ze) { // it is assigning the value to a temp variable
fprintf(asm_out, "\tcmovl\t%%edx, %%ecx\n");
reg_to_var("%cl", q.ze);
}
else { // it jumps to the quad number q.z
fprintf(asm_out, "\tjl\t__L%s\n", q.z);
}
}
}
/* Greater or equal quad */
void op_ge(quad q)
{
translate_comparison(q);
if (is_real_type(q.xe) || is_real_type(q.ye)) {
if (q.ze) { // it is assigning the value to a temp variable
fprintf(asm_out, "\tcmovnb\t%%edx, %%ecx\n");
reg_to_var("%cl", q.ze);
}
else { // it jumps to the quad number q.z
fprintf(asm_out, "\tjge\t__L%s\n", q.z);
}
}
else {
if (q.ze) { // it is assigning the value to a temp variable
fprintf(asm_out, "\tcmovge\t%%edx, %%ecx\n");
reg_to_var("%cl", q.ze);
}
else { // it jumps to the quad number q.z
fprintf(asm_out, "\tjge\t__L%s\n", q.z);
}
}
}
/* Less or equal quad */
void op_le(quad q)
{
translate_comparison(q);
if (is_real_type(q.xe) || is_real_type(q.ye)) {
if (q.ze) { // it is assigning the value to a temp variable
fprintf(asm_out, "\tcmovbe\t%%edx, %%ecx\n");
reg_to_var("%cl", q.ze);
}
else { // it jumps to the quad number q.z
fprintf(asm_out, "\tjle\t__L%s\n", q.z);
}
}
else {
if (q.ze) { // it is assigning the value to a temp variable
fprintf(asm_out, "\tcmovle\t%%edx, %%ecx\n");
reg_to_var("%cl", q.ze);
}
else { // it jumps to the quad number q.z
fprintf(asm_out, "\tjle\t__L%s\n", q.z);
}
}
}
/* Assignment quad */
void q_assign(quad q)
{
if (isConst(q.ze))
internal("Assigned to constant.");
KIND x_type = var_kind(q.xe);
KIND z_type = var_kind(q.ze);
if (x_type == TYPE_BOOLEAN && z_type == TYPE_BOOLEAN) {
bool_to_reg(q.xe, "%al");
reg_to_bool("%al", q.ze);
}
else if (x_type != TYPE_REAL && z_type != TYPE_REAL) {
var_to_reg(q.xe, "%eax");
if ((z_type == TYPE_CHAR) || (z_type == TYPE_BOOLEAN))
reg_to_var("%al", q.ze);
else
reg_to_var("%eax", q.ze);
}
else if (x_type == TYPE_INTEGER && z_type == TYPE_REAL) {
push_int_to_fstack(q.xe);
fstack_to_var(q.ze);
}
else if (x_type == TYPE_CHAR && z_type == TYPE_REAL) {
push_char_to_fstack(q.xe);
fstack_to_var(q.ze);
}
else if ((x_type == TYPE_REAL) &&
(z_type == TYPE_INTEGER || z_type == TYPE_CHAR || z_type == TYPE_REAL)) {
push_real_to_fstack(q.xe);
fstack_to_var(q.ze);
}
}
/* Unit quad (start of a function) */
void q_unit(quad q)
{
unsigned int negOffset;
if (q.xe == NULL) {
fprintf(asm_out, "__main:\n");
fprintf(asm_out, "\tpushl\t%%ebp\n");
fprintf(asm_out, "\tmovl\t%%esp, %%ebp\n");
negOffset = program_offset; // this is the main function
} else {
curRetType = q.xe->u.eFunction.resultType;
negOffset = q.xe->u.eFunction.negOffset; // any other function
fprintf(asm_out, "%s:\n", q.x);
fprintf(asm_out, "\tpushl\t%%ebp\n");
fprintf(asm_out, "\tmovl\t%%esp, %%ebp\n");
}
fprintf(asm_out, "\tsubl\t$%d, %%esp\n", -negOffset);
}
/* Endu quad (end of a function) */
void q_endu(quad q)
{
fprintf(asm_out, "\tmovl\t%%ebp, %%esp\n");
fprintf(asm_out, "\tpopl\t%%ebp\n");
fprintf(asm_out, "\tret\n\n");
curRetType = NULL;
}
/* Jump quads */
void q_jump(quad q)
{
fprintf(asm_out, "\tjmp __L%s\n", q.z);
}
/* Parameter quad (parameter passing for function calls) */
void q_par(quad q)
{
int off = offset(q.xe);
KIND real_kind = var_kind(q.xe);
KIND typical_kind = var_kind(q.ze);
if (equal(q.y, "RET")) {
fprintf(asm_out, "\tmovl\t%%ebp, %%eax\n");
fprintf(asm_out, "\taddl\t$%d, %%eax\n", off);
fprintf(asm_out, "\tpushl\t%%eax\n");
}
else if (equal(q.y, "V")) { // pass by value
if (real_kind == TYPE_BOOLEAN && typical_kind == TYPE_BOOLEAN) {
var_to_reg(q.xe, "%eax");
fprintf(asm_out, "\tpushl\t%%eax\n");
}
else if (real_kind == TYPE_CHAR) {
if (typical_kind == TYPE_CHAR) {
var_to_reg(q.xe, "%eax");
fprintf(asm_out, "\tpushl\t%%eax\n");
}
else if (typical_kind == TYPE_INTEGER) {
var_to_reg(q.xe, "%eax");
fprintf(asm_out, "\tpushl\t%%eax\n");
}
else if (typical_kind == TYPE_REAL) {
push_char_to_fstack(q.xe);
push_from_fstack();
}
else
internal("char type passed to illegal typical parameter type");
}
else if (real_kind == TYPE_INTEGER) {
if (typical_kind == TYPE_CHAR) {
var_to_reg(q.xe, "%eax");
fprintf(asm_out, "\tpushb\t%%al\n");
}
else if (typical_kind == TYPE_INTEGER) {
var_to_reg(q.xe, "%eax");
fprintf(asm_out, "\tpushl\t%%eax\n");
}
else if (typical_kind == TYPE_REAL) {
push_int_to_fstack(q.xe);
push_from_fstack();
}
else
internal("int type passed to illegal typical parameter type");
}
else if (real_kind == TYPE_REAL) {
if (typical_kind != TYPE_REAL)
internal("real type can only be passed to real typical parameters");
push_real_to_fstack(q.xe);
push_from_fstack();
}
else
internal("parameter passed by value with incompatible type");
}
else if (equal(q.y, "R")) { // pass by reference
if (isConst(q.xe) || isGlobal(q.xe)) {
fprintf(asm_out, "\tmovl\t$%s, %%eax\n", q.xe->final_code_name);
fprintf(asm_out, "\tpushl\t%%eax\n");
}
else {
fprintf(asm_out, "\tmovl\t%%ebp, %%eax\n");
fprintf(asm_out, "\taddl\t$%d, %%eax\n", off); // eax holds the address of the variable
if (is_by_ref(q.xe))
fprintf(asm_out, "\tpushl\t(%%eax)\n"); // if was already passed by reference then dereference it
else // so that the actual address is passed instead of a pointer to its pointer
fprintf(asm_out, "\tpushl\t%%eax\n");
}
}
}
/* Call quad (calls a function) */
void q_call(quad q)
{
int extra_space = 4; // space for access link and possibly return value address
if (q.ze->u.eFunction.resultType->kind != TYPE_VOID) // if z has a return type
extra_space += 4; // make room for it in the stack
fprintf(asm_out, "\tpushl\t%%ebp\n");
fprintf(asm_out, "\tcall\t%s\n", q.z);
fprintf(asm_out, "\taddl\t$%d, %%esp\n", extra_space + calc_param_size(q.ze));
}
/* Return quad (returns from a procedure) */
void q_ret(quad q)
{
fprintf(asm_out, "\tmovl\t%%ebp, %%esp\n");
fprintf(asm_out, "\tpopl\t%%ebp\n");
fprintf(asm_out, "\tret\n");
}
/* Returns value quad (returns from a function) */
void q_retv(quad q)
{
if (!curRetType)
internal("Current return type of function is null");
KIND ret_kind = var_kind(q.xe);
KIND func_kind = curRetType->kind;
int ret_addr_offset = 12;
if (ret_kind == TYPE_BOOLEAN && func_kind == TYPE_BOOLEAN) {
bool_to_reg(q.xe, "%al");
reg_to_var("%al", func_kind, ret_addr_offset);
}
else if (ret_kind != TYPE_REAL && func_kind != TYPE_REAL) {
var_to_reg(q.xe, "%eax");
reg_to_var("%eax", func_kind, ret_addr_offset);
}
else if (ret_kind == TYPE_INTEGER && func_kind == TYPE_REAL) {
push_int_to_fstack(q.xe);
fstack_to_var(func_kind, ret_addr_offset, true);
}
else if (ret_kind == TYPE_CHAR && func_kind == TYPE_REAL) {
push_char_to_fstack(q.xe);
fstack_to_var(func_kind, ret_addr_offset, true);
}
else if ((ret_kind == TYPE_REAL) &&
(func_kind == TYPE_INTEGER || func_kind == TYPE_CHAR || func_kind == TYPE_REAL)) {
push_real_to_fstack(q.xe);
fstack_to_var(func_kind, ret_addr_offset, true);
}
else
internal("retv called with unknown parameter type");
fprintf(asm_out, "\tmovl\t%%ebp, %%esp\n");
fprintf(asm_out, "\tpopl\t%%ebp\n");
fprintf(asm_out, "\tret\n");
}
/* Array quad */
void q_array(quad q)
{
Type refType;
EntryType eType = entry_type(q.xe);
switch (eType) {
case ENTRY_VARIABLE:
refType = q.xe->u.eVariable.type->refType;
break;
case ENTRY_PARAMETER:
refType = q.xe->u.eParameter.type->refType;
break;
case ENTRY_TEMPORARY:
refType = q.xe->u.eTemporary.type->refType;
break;
default:
internal("q_array called with invalid entry type");
}
int ref_size = sizeOfType(refType);
int x_offset = offset(q.xe);
int z_offset = offset(q.ze);
var_to_reg(q.ye, "%eax"); // eax holds the index value
fprintf(asm_out, "\tmovl\t$%d, %%ecx\n", ref_size); // ecx holds the ref_size
fprintf(asm_out, "\timull\t%%ecx\n"); // eax holds the distance from the first element
if (isGlobal(q.xe)) {
fprintf(asm_out, "\tmovl\t$%s, %%ecx\n", q.xe->final_code_name);
}
else {
fprintf(asm_out, "\tmovl\t%%ebp, %%ecx\n");
fprintf(asm_out, "\taddl\t$%d, %%ecx\n", x_offset); // ecx holds the address of the array variable
}
if (is_by_ref(q.xe))
fprintf(asm_out, "\taddl\t(%%ecx), %%eax\n"); // eax holds the address of the element
else
fprintf(asm_out, "\taddl\t%%ecx, %%eax\n");
fprintf(asm_out, "\tmovl\t%%eax, %d(%%ebp)\n", z_offset); // store the address in the z temporary variable
}
/* Moves a variable to a register */
void var_to_reg(SymbolEntry *var, const char *reg)
{
int off = offset(var);
KIND kind = var_kind(var);
const char *op = mov_op(kind);
if (isGlobal(var)) {
if (kind == TYPE_INTEGER) {
if (isConst(var))
fprintf(asm_out, "\tmovl\t$%d, %s\n", int_val(var), reg); //use immediate instr
else
fprintf(asm_out, "\tmovl\t%s, %s\n", var->final_code_name, reg);
}
else if (kind == TYPE_CHAR || kind == TYPE_BOOLEAN) {
if (isConst(var))
fprintf(asm_out, "\tmovl\t$%d, %s\n", char_val(var), reg); //use immediate instr
else {
fprintf(asm_out, "\tmovl\t$0, %s\n", reg); // zero the register
fprintf(asm_out, "\tmovsbl\t%s, %s\n", var->final_code_name, reg);
}
}
}
else if (is_by_ref(var)) {
fprintf(asm_out, "\tmovl\t%d(%%ebp), %%esi\n", off);
if (kind == TYPE_INTEGER)
fprintf(asm_out, "\tmovl\t0(%%esi), %s\n", reg);
else if (kind == TYPE_CHAR || kind == TYPE_BOOLEAN)
fprintf(asm_out, "\tmovsbl\t0(%%esi), %s\n", reg);
else
internal("var_to_reg called with invalid var type");
}
else if (kind == TYPE_INTEGER) {
if (isConst(var))
fprintf(asm_out, "\tmovl\t$%d, %s\n", int_val(var), reg); //use immediate instr
else
fprintf(asm_out, "\t%s\t%d(%%ebp), %s\n", op, off, reg);
}
else if (kind == TYPE_CHAR || kind == TYPE_BOOLEAN) {
if (isConst(var))
fprintf(asm_out, "\tmovl\t$%d, %s\n", char_val(var), reg); //use immediate instr
else {
fprintf(asm_out, "\tmovl\t$0, %s\n", reg); // zero the register
fprintf(asm_out, "\tmovsbl\t%d(%%ebp), %s\n", off, reg);
}
}
else
internal("var_to_reg called with invalid var type");
}
/* Moves a boolean variable to a register */
void bool_to_reg(SymbolEntry *var, const char *reg)
{
if (var_kind(var) != TYPE_BOOLEAN)
internal("Reg_to_bool called with non boolean var");
int off = offset(var);
const char *op = mov_op(TYPE_BOOLEAN);
if (isGlobal(var)) {
if (isConst(var)) {
int val = bool_val(var) ? 1 : 0;
fprintf(asm_out, "\tmovb\t$%d, %s\n", val, reg);
}
else
fprintf(asm_out, "\t%s\t%s, %s\n", op, var->final_code_name, reg);
}
else if (is_by_ref(var)) {
fprintf(asm_out, "\tmovl\t%d(%%ebp), %%esi\n", off);
fprintf(asm_out, "\tmovb\t0(%%esi), %s\n", reg);
}
else {
if (isConst(var)) {
int val = bool_val(var) ? 1 : 0;
fprintf(asm_out, "\tmovb\t$%d, %s\n", val, reg);
}
else
fprintf(asm_out, "\t%s\t%d(%%ebp), %s\n", op, off, reg);
}
}
/* Moves a value from a register to a variable */
void reg_to_var(const char *reg, SymbolEntry *var)
{
int off = offset(var);
KIND kind = var_kind(var);
const char *op = mov_op(kind);
if (isGlobal(var)) {
fprintf(asm_out, "\t%s\t%s, %s\n", op, reg, var->final_code_name);
}
else if (is_by_ref(var)) {
fprintf(asm_out, "\tmovl\t%d(%%ebp), %%esi\n", off);
fprintf(asm_out, "\t%s\t%s, 0(%%esi)\n", op, reg);
}
else {
fprintf(asm_out, "\t%s\t%s, %d(%%ebp)\n", op, reg, off);
}
}
/* Moves a value of the given type from a register to the given offset in the stack */
void reg_to_var(const char *reg, KIND kind, int offset)
{
const char *op = mov_op(kind);
fprintf(asm_out, "\tmovl\t%d(%%ebp), %%esi\n", offset);
fprintf(asm_out, "\t%s\t%s, 0(%%esi)\n", op, reg);
}
/* Moves a value from a register to the given boolean variable */
void reg_to_bool(const char *reg, SymbolEntry *var)
{
if (var_kind(var) != TYPE_BOOLEAN)
internal("Reg_to_bool called with non boolean var");
int off = offset(var);
const char *op = mov_op(TYPE_BOOLEAN);
if (isGlobal(var)) {
fprintf(asm_out, "\t%s\t%s, %s\n", op, reg, var->final_code_name);
}
else if (is_by_ref(var)) {
fprintf(asm_out, "\tmovl\t%d(%%ebp), %%esi\n", off);
fprintf(asm_out, "\t%s\t%s, 0(%%esi)\n", op, reg);
}
else {
fprintf(asm_out, "\t%s\t%s, %d(%%ebp)\n", op, reg, off);
}
}
/* Pushes a 10 byte float to the x87 floating point stack */
void push_real_to_fstack(SymbolEntry *real)
{
if (var_kind(real) != TYPE_REAL)
internal("Push_real_to_fstack called with non real var");
if (isConst(real) || isGlobal(real))
fprintf(asm_out, "\tfldt\t%s\n", real->final_code_name);
else if (is_by_ref(real)) {
fprintf(asm_out, "\tmovl\t%d(%%ebp), %%esi\n", offset(real)); // move its location to esi
fprintf(asm_out, "\tfldt\t(%%esi)\n"); // move from the stack to the fp stack