-
Notifications
You must be signed in to change notification settings - Fork 0
/
quads.c
1524 lines (1136 loc) · 32.7 KB
/
quads.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
/* quads.c
* Basic quad fucntions and code generation
* Written by DK and JC for CS 48.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "quads.h"
#include "ast.h"
#include "Symtab.h"
#include "assemblygen.h"
#define DOASSEMBLY 1
//GLOBAL VARIABLES
int currentQuad = -1; //index into quads
int tempCount = 0; //for unique temp names
int goffset = 0; //global offset for dmem addresses
int foffset = -4; //function offset for dmem addresses
//must remember in function declaration to change foffset back to -4
Quad **quads; //array of Quads
SymbolTable *symtab; //symbol table
char namesOfOps[][10] = {"rd", "gotoq", "if_f", "asn", "lab", "mul", "divi", "add", "sub", "eq", "wri", "halt", "neq",
"lt", "gt", "gteq", "lteq", "sym", "ret", "ens", "exs", "loadpar", "jne", "con", "brk"};
//Get the wider of the two - int or double
int MaxType(Address a, Address b)
{
//printf("Called MaxType\n");
SymNode *an;
SymNode *bn;
if (a.kind == String)
{
//printf("a.kind is string\n");
an = LookupInSymbolTable(symtab, a.contents.name);
//printf("looked up a\n");
typeenum te = GetTypeAttr(an);
if (te == DouT)
return 8;
}
if (b.kind == String)
{
//printf("b.kind is string\n");
bn = LookupInSymbolTable(symtab, b.contents.name);
typeenum te = GetTypeAttr(bn);
if (te == DouT)
return 8;
}
//set the result to the right type
if (a.kind == DouConst || b.kind == DouConst)
{
return 8;
}
//printf("Finished MaxType\n");
return 4;
}
//Actually creates the quads by recursing;
//We will return the quad that was the last result or -1 if we have no result
int CG(ast_node n)
{
//printf("Called CG\n");
//temporaries we're going to use throughout the CG function, just for
//convenience
Address ar1, ar2, ar3, ar4, ar5, tqa, ta, topatch, e, nq;
int lrp, rrp;
int tq, t, testq, gq;
int typer;
SymNode *sn;
value v;
ast_node next;
ast_node x;
if (n == NULL)
{
//printf("Error CG called on NULL node\n");
return -1;
}
switch (n->node_type) {
case CONTINUE_ST:
e.kind = Empty;
GenQuad(con, e, e, e);
break;
case BREAK_ST:
e.kind = Empty;
GenQuad(brk, e, e, e);
break;
//just has id which we set its attr to constant
case CONSTANT:
//first we get the id
lrp = CG(n->left_child);
//id
ar1 = quads[lrp]->addr1;
sn = LookupInSymbolTable(symtab, ar1.contents.name);
SetFlagsAttr(sn, 1);
break;
case OP_SIZEOF:
//first we get the id
lrp = CG(n->left_child);
//id
ar1 = quads[lrp]->addr1;
sn = LookupInSymbolTable(symtab, ar1.contents.name);
t = GetTypeAttr(sn);
switch (t) {
case IntT:
tq = 4;
break;
case DouT:
tq = 8;
break;
case IArT:
gq = GetSizeAttr(sn);
tq = 4*gq;
break;
case DArT:
gq = GetSizeAttr(sn);
tq = 8*gq;
break;
default:
tq = 4;
break;
}
//Address ar1, ar2, ar3;
ar1.kind = String;
ar1.contents.name = NewTemp(4);
//we are assigning this literal value to ar1
ar2.kind = IntConst;
ar2.contents.val = tq;
//we don't need ar3
ar3.kind = Empty;
return GenQuad(asn, ar1, ar2, ar3);
break;
case ARRAY_PULL:
//printf("ARRAY_PULL Case in CG\n");
e.kind = Empty;
//first we get the id, the name
lrp = CG(n->left_child);
//then we get the left child, right sibling, which is the index of the array
rrp = CG(n->left_child->right_sibling);
//id
ar1 = quads[lrp]->addr1;
//index
ar2 = quads[rrp]->addr1;
ar3.kind = String;
sn = LookupInSymbolTable(symtab, ar1.contents.name);
//if int array
if (GetTypeAttr(sn) == IArT) {
typer = 4;
}
//if double array
else {
typer = 8;
}
t = GetOffsetAttr(sn);
//we have a constant
if (ar2.kind != String) {
tq = ar2.contents.val;
}
//temporary must be changed
else {
tq = 4;
}
if (typer == 4) {
ar3.contents.name = NewTemp(4);
}
else {
ar3.contents.name = NewTemp(8);
}
sn = LookupInSymbolTable(symtab, ar3.contents.name);
if (typer == 4) {
SetTypeAttr(sn, IntT);
SetOffsetAttr(sn, t-(tq*typer));
}
else {
SetTypeAttr(sn, DouT);
SetOffsetAttr(sn, t-(tq*typer));
}
return GenQuad(sym, ar3, e, e);
break;
case INT_ARRAY_DEC:
//printf("INT_ARRAY_DEC Case in CG\n");
//first we get the id, the name
lrp = CG(n->left_child);
//then we get the left child, right sibling, which is the size of the array
rrp = CG(n->left_child->right_sibling);
//id
ar1 = quads[lrp]->addr1;
//size
ar2 = quads[rrp]->addr1;
if (ar2.kind == IntConst)
{
t = ar2.contents.val;
}
//otherwise we have a string
else
{
sn = LookupInSymbolTable(symtab, ar2.contents.name);
v = GetValueAttr(sn);
t = v.ival;
}
e.kind = Empty;
//going to insert into symbol table
//printf("inserting int int array id into sym table \n");
//printf("%s is the symbol going in\n", ar1.contents.name);
sn = InsertIntoSymbolTable(symtab, ar1.contents.name);
//printf("Setting type attribute for that\n");
SetTypeAttr(sn, IArT);
//printf("Set type successfully\n");
SetSizeAttr(sn, t);
//printf("Set size successfully\n");
if (sn->level == 1)
{
SetOffsetAttr(sn, goffset);
goffset -= 4*t;
}
else {
SetOffsetAttr(sn, foffset);
foffset -= 4*t;
}
//PLACEHOLDER - Need to set the TYPE ATTRIBUTE in the symbol table to int
break;
case DOU_ARRAY_DEC:
//printf("DOU_ARRAY_DEC Case in CG\n");
//first we get the id, the name
lrp = CG(n->left_child);
//then we get the left child, right sibling, which is the size of the array
rrp = CG(n->left_child->right_sibling);
//id
ar1 = quads[lrp]->addr1;
//size
ar2 = quads[rrp]->addr1;
if (ar2.kind == IntConst)
{
t = ar2.contents.val;
}
//otherwise we have a string
else
{
sn = LookupInSymbolTable(symtab, ar2.contents.name);
v = GetValueAttr(sn);
t = v.ival;
}
e.kind = Empty;
//going to insert into symbol table
//printf("inserting int int array id into sym table \n");
//printf("%s is the symbol going in\n", ar1.contents.name);
sn = InsertIntoSymbolTable(symtab, ar1.contents.name);
//printf("Setting type attribute for that\n");
SetTypeAttr(sn, DArT);
//printf("Set type successfully\n");
SetSizeAttr(sn, t);
//printf("Set size successfully\n");
if (sn->level == 1) {
SetOffsetAttr(sn, goffset);
goffset -= 8*t;
}
else {
SetOffsetAttr(sn, foffset);
foffset -= 8*t;
}
//PLACEHOLDER - Need to set the TYPE ATTRIBUTE in the symbol table to int
break;
case SWITCH_ST:
//this is the id or int we compare against
lrp = CG(n->left_child);
ar1 = quads[lrp]->addr1;
e.kind = Empty;
//traverse the cases
next = n->left_child->right_sibling;
while (next->node_type == SWITCH_CASE)
{
//get the case
rrp = CG(next->left_child);
ar2 = quads[rrp]->addr1;
//if the values are not equal, skip this case
gq = GenQuad(jne, e, ar2, ar1);
//process the sequence
CG(next->left_child->right_sibling);
nq.kind = IntConst;
nq.contents.val = NextQuad();
PatchQuad(gq, 1, nq);
next=next->right_sibling;
}
//last node should be the default
CG(next);
break;
case SWITCH_CASE:
break;
//we have a function with parameters
//Based on Louden p 442
case FUNC_DEF:
//printf("In FUNC_DEF Case\n");
//add the function name to the symbol table
//
e.kind = Empty;
//we must reset f-offset for memory management purposes
foffset = -4;
//this will generate a sym quad which is the start of the function
//it's also a dummy quad for integer defintions
//we're using it like ENT in this case in Louden
lrp = CG(n->left_child);
//we expect an ID, so add it to the symbol table
sn = InsertIntoSymbolTable(symtab, quads[lrp]->addr1.contents.name);
//this is the quad where we'll need to jump if this function is called
v.ival = lrp;
SetValueAttr(sn, v);
//we're going to ignore the parameters as per louden p 441
next = n->left_child->right_sibling;
while (next->node_type == FUNC_PARAM)
{
next=next->right_sibling;
}
//now we're done so we should be at the SEQ of actual instructions
CG(next);
//finish it off with a ret
GenQuad(ret, e, e, e);
break;
//has id and parameters - we jump to the quad of the id
//Based on Louden p 442
case FUNC_CALL:
//printf("FUNC_CALL in Switch of CG\n");
e.kind = Empty;
//load all the params
next = n->left_child;
while (next->node_type == FUNC_PARAM)
{
//we're telling the assembler we want it to load parameters for a function call
lrp = CG(next);
GenQuad(loadpar, quads[lrp]->addr1, e, e);
next=next->right_sibling;
}
//the id has in the symbol table a value associated with it
//which represents the quad we want to jump to
lrp = CG(next);
//get the name of the ID and look up its place
sn = LookupInSymbolTable(symtab, quads[lrp]->addr1.contents.name);
v = GetValueAttr(sn);
t = v.ival;
ar1.kind = IntConst;
ar1.contents.val = t;
GenQuad(gotoq, ar1, e, e);
//jump to the location of the id
break;
case FUNC_PARAM:
return CG(n->left_child);
break;
case RETURN_S:
//printf("In RETURN Case\n");
gq = GenQuad(ret, e, e, e);
//if we have a return after the statement
if(n->left_child != NULL)
{
lrp = CG(n->left_child);
PatchQuad(gq, 1, quads[lrp]->addr1);
}
break;
// "==" (IS EQUAL TO) operation
case OP_EQUALS:
//printf("In OP_EQUALS Case\n");
//Address ar1, ar2, ar3;
ar1.kind = String;
ar1.contents.name = NewTemp(4);
//left result needs to be put in
if (n->left_child == NULL) {
printf("Error child is null\n");
}
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;
//printf("%s the value\n", ar2.contents.name);
//right child's result needs to be the other operand
if (n->left_child->right_sibling == NULL) {
printf("Error left_child->right sibling is null\n");
}
rrp = CG(n->left_child->right_sibling);
ar3 = quads[rrp]->addr1;
//printf("%s the value\n", ar3.contents.name);
return GenQuad(eq, ar1, ar2, ar3);
break;
//ROOT is nothing in itself, so we just start recursing down the tree
case ROOT:
//printf("In ROOT case\n");
CG(n->left_child);
break;
// "!=" (IS NOT EQUAL TO) operation
case OP_NOT_EQUALS:
ar1.kind = String;
ar1.contents.name = NewTemp(4);
//left result needs to be put in
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;
//right child's result needs to be the other operand
rrp = CG(n->left_child->right_sibling);
ar3 = quads[rrp]->addr1;
return GenQuad(neq, ar1, ar2, ar3);
break;
// ">" (GREATER THAN) operation
case OP_GREATER_THAN:
ar1.kind = String;
ar1.contents.name = NewTemp(4);
//left result needs to be put in
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;
//right child's result needs to be the other operand
rrp = CG(n->left_child->right_sibling);
ar3 = quads[rrp]->addr1;
return GenQuad(gt, ar1, ar2, ar3);
break;
// "<" (LESS THAN) operation
case OP_LESS_THAN:
//printf("LESS THAN Case in CG\n");
ar1.kind = String;
ar1.contents.name = NewTemp(4);
//left result needs to be put in
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;
//right child's result needs to be the other operand
rrp = CG(n->left_child->right_sibling);
ar3 = quads[rrp]->addr1;
return GenQuad(lt, ar1, ar2, ar3);
break;
// ">=" (GREATER THAN OR EQUAL TO) operation
case OP_GREATER_EQUALS:
ar1.kind = String;
ar1.contents.name = NewTemp(4);
//left result needs to be put in
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;
//right child's result needs to be the other operand
rrp = CG(n->left_child->right_sibling);
ar3 = quads[rrp]->addr1;
return GenQuad(gteq, ar1, ar2, ar3);
break;
// "<=" (LESS THAN OR EQUAL TO) operation
case OP_LESS_EQUALS:
ar1.kind = String;
ar1.contents.name = NewTemp(4);
//left result needs to be put in
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;
//right child's result needs to be the other operand
rrp = CG(n->left_child->right_sibling);
ar3 = quads[rrp]->addr1;
return GenQuad(lteq, ar1, ar2, ar3);
break;
// "||" (OR) operation
case OP_OR:
ar1.kind = String;
ar1.contents.name = NewTemp(4);
ar2.kind = IntConst;
ar2.contents.val = 1;
ar3.kind = IntConst;
ar3.contents.val = 0;
ar4.kind = String;
ar4.contents.name = NewTemp(4);
e.kind = Empty;
lrp = CG(n->left_child);
ta = quads[lrp]->addr1;
gq = GenQuad(if_f, ta, e, e);
GenQuad(asn, ar4, ar2, e);
typer = GenQuad(gotoq, e, e, e);
t = CG(n->left_child->right_sibling);
ar5.kind = IntConst;
ar5.contents.val = t;
ta = quads[t]->addr1;
testq = GenQuad(if_f, ta, e, e);
GenQuad(asn, ar4, ar2, e);
tq = GenQuad(gotoq, e, e, e);
nq.kind = IntConst;
nq.contents.val = NextQuad();
PatchQuad(testq, 2, nq);
GenQuad(asn, ar4, ar3, e);
PatchQuad(typer, 1, nq);
PatchQuad(gq, 2, ar5);
nq.kind = IntConst;
nq.contents.val = NextQuad();
PatchQuad(tq, 1, nq);
rrp = GenQuad(asn, ar1, ar4, e);
return rrp;
break;
// "&&" (AND) operation - based on THC code
case OP_AND:
ar1.kind = String;
ar1.contents.name = NewTemp(4);
ar2.kind = IntConst;
ar2.contents.val = 1;
ar3.kind = IntConst;
ar3.contents.val = 0;
ar4.kind = String;
ar4.contents.name = NewTemp(4);
e.kind = Empty;
lrp = CG(n->left_child);
ta = quads[lrp]->addr1;
gq = GenQuad(if_f, ta, e, e);
t = CG(n->left_child->right_sibling);
ta = quads[t]->addr1;
testq = GenQuad(if_f, ta, e, e);
GenQuad(asn, ar4, ar2, e);
tq = GenQuad(gotoq, e, e, e);
nq.kind = IntConst;
nq.contents.val = NextQuad();
PatchQuad(gq, 2, nq);
PatchQuad(testq, 2, nq);
GenQuad(asn, ar4, ar3, e);
nq.kind = IntConst;
nq.contents.val = NextQuad();
PatchQuad(tq, 1, nq);
rrp = GenQuad(asn, ar1, ar4, e);
return rrp;
break;
case OP_NOT:
ar1.kind = String;
ar1.contents.name = NewTemp(4);
ar2.kind = IntConst;
ar2.contents.val = 1;
ar3.kind = IntConst;
ar3.contents.val = 0;
ar4.kind = String;
ar4.contents.name = NewTemp(4);
e.kind = Empty;
t = CG(n->left_child);
ta = quads[t]->addr1;
//if false go to the end and give it a 0
gq = GenQuad(if_f, ta, e, e);
GenQuad(asn, ar4, ar2, e);
tq = GenQuad(gotoq, e, e, e);
nq.kind = IntConst;
nq.contents.val = NextQuad();
PatchQuad(gq, 2, nq);
GenQuad(asn, ar4, ar3, e);
nq.kind = IntConst;
nq.contents.val = NextQuad();
PatchQuad(tq, 1, nq);
rrp = GenQuad(asn, ar1, ar4, e);
return rrp;
break;
//if it's a SEQ, we want to just recursively produce code for all the children
case SEQ:
//adapted from THC if code
e.kind = Empty;
//EnterScope(symtab);
//we must tell the assembly generator we've entered a new scope
GenQuad(ens, e, e, e);
x = n->left_child;
while (x != NULL) {
CG(x);
x = x->right_sibling;
}
GenQuad(exs, e, e, e);
//we must tell the assembly generator we've exited a scope
//LeaveScope(symtab);
break;
//Here we do the addition and then we return the position so higher up nodes can find the result
case OP_PLUS:
//printf("OP_PLUS Case in CG\n");
ar1.kind = String;
//left result needs to be put in
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;
//right child's result needs to be the other operand
rrp = CG(n->left_child->right_sibling);
ar3 = quads[rrp]->addr1;
ar1.contents.name = NewTemp(MaxType(ar2, ar3));
return GenQuad(add, ar1, ar2, ar3);
break;
case OP_MINUS:
ar1.kind = String;
//left result needs to be put in
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;
//right child's result needs to be the other operand
rrp = CG(n->left_child->right_sibling);
ar3 = quads[rrp]->addr1;
ar1.contents.name = NewTemp(MaxType(ar2, ar3));
return GenQuad(sub, ar1, ar2, ar3);
break;
case OP_TIMES:
//printf("OP_TIMES Case in CG\n");
ar1.kind = String;
//left result needs to be put in
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;
//right child's result needs to be the other operand
rrp = CG(n->left_child->right_sibling);
ar3 = quads[rrp]->addr1;
ar1.contents.name = NewTemp(MaxType(ar2, ar3));
return GenQuad(mul, ar1, ar2, ar3);
break;
case OP_DIVIDE:
ar1.kind = String;
//left result needs to be put in
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;
//right child's result needs to be the other operand
rrp = CG(n->left_child->right_sibling);
ar3 = quads[rrp]->addr1;
ar1.contents.name = NewTemp(MaxType(ar2, ar3));
return GenQuad(divi, ar1, ar2, ar3);
break;
//negate a number
case OP_NEGATIVE:
//printf("OP_NEGATIVE Case in CG\n");
ar1.kind = String;
//this should only have one child
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;
//we need to subtract from 0
ar3.kind = IntConst;
ar3.contents.val = 0;
ar1.contents.name = NewTemp(MaxType(ar2, ar3));
return GenQuad(sub, ar1, ar3, ar2);
break;
case INT_LITERAL:
//printf("INT_LITERAL Case in CG\n");
//Address ar1, ar2, ar3;
ar1.kind = String;
ar1.contents.name = NewTemp(4);
//we are assigning this literal value to ar1
ar2.kind = IntConst;
//printf("n->value.int_value %d \n", n->value.int_value);
ar2.contents.val = n->value.int_value;
//we don't need ar3
ar3.kind = Empty;
return GenQuad(asn, ar1, ar2, ar3);
break;
case DOUBLE_LITERAL:
//printf("DOUBLE_LITERAL Case in CG\n");
ar1.kind = String;
ar1.contents.name = NewTemp(8);
//we are assigning this literal value to ar1
ar2.kind = DouConst;
ar2.contents.dval = n->value.double_value;
//we don't need ar3
ar3.kind = Empty;
return GenQuad(asn, ar1, ar2, ar3);
break;
case STRING_LITERAL:
ar1.kind = String;
ar1.contents.name = NewTemp(240);
//we are assigning this literal value to ar1
ar2.kind = String;
ar2.contents.name = n->value.string;
//we don't need ar3
ar3.kind = Empty;
return GenQuad(asn, ar1, ar2, ar3);
break;
//we must insert in the symbol table the child's name
case INT_DEC:
//printf("INT_DEC Case in CG\n");
lrp = CG(n->left_child);
ar1 = quads[lrp]->addr1;
ar2.kind = Empty;
ar3.kind = Empty;
//going to insert into symbol table
//printf("inserting int dec id into sym table \n");
//printf("%s is the symbol going in\n", ar1.contents.name);
sn = InsertIntoSymbolTable(symtab, ar1.contents.name);
//printf("Setting type attribute for that\n");
SetTypeAttr(sn, IntT);
//printf("Set type successfully\n");
if (sn->level == 1) {
SetOffsetAttr(sn, goffset);
goffset -= 4;
}
else {
SetOffsetAttr(sn, foffset);
foffset -= 4;
}
//PLACEHOLDER - Need to set the TYPE ATTRIBUTE in the symbol table to int
break;
//we must insert the child in the symbol table
case DOU_DEC:
//printf("DOU_DEC Case in CG\n");
lrp = CG(n->left_child);
ar1 = quads[lrp]->addr1;
ar2.kind = Empty;
ar3.kind = Empty;
sn = InsertIntoSymbolTable(symtab, ar1.contents.name);
SetTypeAttr(sn, DouT);
if (sn->level == 1) {
SetOffsetAttr(sn, goffset);
goffset -= 8;
}
else {
SetOffsetAttr(sn, foffset);
foffset -= 8;
}
//PLACEHOLDER - Need to set the TYPE ATTRIBUTE in the symbol table to double
break;
case FOR_LOOP:
//printf("FORLOOP Case in CG\n");
//the first statement is always executed
CG(n->left_child);
//the inequality is statement two
t = CG(n->left_child->right_sibling);
ta = quads[t]->addr1;
testq = GenQuad(if_f, ta, topatch, e);
//we do the sequence before the final statement
CG(n->left_child->right_sibling->right_sibling->right_sibling);
//now we do the third statement in the for-loop header
CG(n->left_child->right_sibling->right_sibling);
//then we loop around
e.kind = Empty;
nq.kind = IntConst;
nq.contents.val = t;
GenQuad(gotoq, nq, e, e);
//finally we must patch the inequality
nq.kind = IntConst;
nq.contents.val = NextQuad();
PatchQuad(testq, 2, nq);
break;
case OP_PRE_INCR:
//printf("PRE++ Case in CG\n");
//left result needs to be put in
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;
//right child's result needs to be the other operand
ar3.kind = IntConst;
ar3.contents.val = 1;
return GenQuad(add, ar2, ar2, ar3);
break;
case OP_POST_INCR:
//printf("POST++ Case in CG\n");
//left result needs to be put in
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;
//right child's result needs to be the other operand
ar3.kind = IntConst;
ar3.contents.val = 1;
return GenQuad(add, ar2, ar2, ar3);
break;
case OP_PRE_DECR:
//printf("PRE-- Case in CG\n");
//left result needs to be put in
lrp = CG(n->left_child);
ar2 = quads[lrp]->addr1;