-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.cpp
More file actions
1373 lines (1175 loc) · 42.5 KB
/
node.cpp
File metadata and controls
1373 lines (1175 loc) · 42.5 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
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 <stdio.h>
#include <list>
#include <string>
#include <stack>
#include <iostream>
#include <fstream>
#include <iostream>
#include <vector>
#include "node.h"
#define PRINTDEBUG(x) //std::cout << x << std::endl; // comment out print statement to remove the printing
bool type_error = false; //boolean value true denotes type_error present in Tree, false Tree syntax is type valid
ClassDecl *currentClass;
std::string currentClassName;
std::map<std::string, ClassDecl *> classTable; //map of classes in program
std::map<std::string, VarDecl *> type_class_scope; //map of fields in current class scope
std::map<std::string, VarDecl *> type_local_scope; //map of variables in current scope inlcuding current class variables and method variables
std::map<std::string, std::string> scope_type;
std::map<std::string, int> scope;
std::vector<std::string> class_variables;
std::string currentMethodName;
std::string buffer;
std::vector<std::string> text;
std::vector<std::string> data;
/****************** REGISTER STACK ***********************/
std::stack<std::string> registerStack;
std::string SUPPRESS = "SUPPRESS";
std::string SUPPRESSED = "SUPPRESSED";
std::string LITERAL = "LITERAL";
std::string SYMBOLIC = "SYMBOLIC";
int register_number = 4;
/**
* Input Register of Choice during pop spill
* If top of stack is a spill, the pop_reg will
* hold the value of the popped spill
* SUPPRESS value means pop from the stack silently
* NON-SUPPRESS means a loud pop with assembly code
**/
std::string r_pop(std::string pop_reg) {
std::string reg = registerStack.top();
if(reg.at(0) != '#' && reg != "r0") {
//SYMBOLIC, decrement register_number
register_number--;
}
registerStack.pop();
if(pop_reg == SUPPRESS && reg == "r0") {
//do nothing
return SUPPRESSED;
} else if(reg == "r0") {
//top of stack was spilled, pop the register
buffer += " pop {"+pop_reg+"}\n";
return pop_reg;
} else {
return reg;
}
}
/**
* Register 0 is reserved for function return
* Registers 1-11 are reserved for the register stack (size: 11)
* push() allocates a register for reservation, and pushes it to the stack
* if the stack is full (r11 already reserved) the register must be spilled into stack memory
**/
std::string r_push(std::string type) {
//int register_number = registerStack.size()+4;
if(type == SYMBOLIC) {
//type is SYMBOLIC, push register onto stack
if(register_number > 7) {
//stack is full, spill the register, push onto stack
registerStack.push("r0");
return "r0";
}
std::string reg = "r" + std::to_string(register_number);
registerStack.push(reg);
register_number++;
return reg;
} else {
//type is LITERAL, push literal onto stack
registerStack.push(type);
return "";
}
}
/**
* Checks if the input register is a scratch register
* If the input register is a scratch register then it is spilled
* the scratch register will be spilled to the stack
**/
void check_spill(std::string reg) {
if(reg == "r0" || reg == "r1" || reg == "r2" || reg == "r3") {
buffer += " push {"+reg+"}\n";
}
}
/******************* IDENTIFIER CLASS *********************/
Identifier::Identifier(const std::string str): id(str) {}
std::string Identifier::toString() {
return id;
}
/******************* EXP SUB-CLASSES ****************************/
And::And(Exp *lhs, Exp *rhs, int lineno): lhs(lhs), rhs(rhs), lineno(lineno) {}
std::string And::visit() {
PRINTDEBUG("(And)");
std::string left = lhs->visit();
std::string right = rhs->visit();
if(left != "boolean" || right != "boolean") {
std::cerr << "Type Violation in Line " << lineno << " : bad operand types for binary operator '&&'" << std::endl;
type_error = true;
}
return "boolean";
}
void And::evaluate(std::string ret_type) {
lhs->evaluate(SYMBOLIC);
rhs->evaluate(LITERAL);
std::string right = r_pop("r1");
std::string left = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
buffer += " and "+reg+", "+left+", "+right+"\n"; //add values from r0 and r1, store in r0
check_spill(reg);
}
Or::Or(Exp *lhs, Exp *rhs, int lineno): lhs(lhs), rhs(rhs), lineno(lineno) {}
std::string Or::visit() {
PRINTDEBUG("(Or)");
std::string left = lhs->visit();
std::string right = rhs->visit();
if(left != "boolean" || right != "boolean") {
std::cerr << "Type Violation in Line " << lineno << " : bad operand types for binary operator '||'" << std::endl;
type_error = true;
}
return "boolean";
}
void Or::evaluate(std::string ret_type) {
lhs->evaluate(SYMBOLIC);
rhs->evaluate(LITERAL);
std::string right = r_pop("r1");
std::string left = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
buffer += " orr "+reg+", "+left+", "+right+"\n"; //add values from r0 and r1, store in r0
check_spill(reg);
}
Is::Is(Exp *lhs, Exp *rhs, int lineno): lhs(lhs), rhs(rhs), lineno(lineno) {}
std::string Is::visit() {
PRINTDEBUG("(Is)");
std::string left = lhs->visit();
std::string right = rhs->visit();
if(left != right) {
std::cerr << "Type Violation in Line " << lineno << " : incomparable types for binary operator '=='" << std::endl;
type_error = true;
}
return "boolean";
}
void Is::evaluate(std::string ret_type) {
lhs->evaluate(SYMBOLIC);
rhs->evaluate(LITERAL);
std::string right = r_pop("r1");
std::string left = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
//compare equality
buffer += " cmp "+left+", "+right+"\n";
buffer += " beq 1f\n";
buffer += " ldr "+reg+", =0\n";
buffer += " b 2f\n";
//branch 1
buffer += "1: \n";
buffer += " ldr "+reg+", =1\n";
//branch 2
buffer += "2: \n";
//push result to stack
check_spill(reg);
}
IsNot::IsNot(Exp *lhs, Exp *rhs, int lineno): lhs(lhs), rhs(rhs), lineno(lineno) {}
std::string IsNot::visit() {
PRINTDEBUG("(IsNot)")
std::string left = lhs->visit();
std::string right = rhs->visit();
if(left != right) {
std::cerr << "Type Violation in Line " << lineno << " : incomparable types for binary operator '!='" << std::endl;
type_error = true;
}
return "boolean";
}
void IsNot::evaluate(std::string ret_type) {
lhs->evaluate(SYMBOLIC);
rhs->evaluate(LITERAL);
std::string right = r_pop("r1");
std::string left = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
//compare equality
buffer += " cmp "+left+", "+right+"\n";
buffer += " bne 1f\n";
buffer += " ldr "+reg+", =0\n";
buffer += " b 2f\n";
//branch 1
buffer += "1: \n";
buffer += " ldr "+reg+", =1\n";
//branch 2
buffer += "2: \n";
//push result to the stack
check_spill(reg);
}
LessThan::LessThan(Exp *lhs, Exp *rhs, int lineno): lhs(lhs), rhs(rhs), lineno(lineno) {}
std::string LessThan::visit() {
PRINTDEBUG("(LessThan)")
std::string left = lhs->visit();
std::string right = rhs->visit();
if(left != "int" || right != "int") {
std::cerr << "Type Violation in Line " << lineno << " : bad operand types for binary operator '<'" << std::endl;
type_error = true;
}
return "boolean";
}
void LessThan::evaluate(std::string ret_type) {
lhs->evaluate(SYMBOLIC);
rhs->evaluate(LITERAL);
std::string right = r_pop("r1");
std::string left = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
//compare equality
buffer += " cmp "+left+", "+right+"\n";
buffer += " blt 1f\n";
buffer += " ldr "+reg+", =0\n";
buffer += " b 2f\n";
//branch 1
buffer += "1: \n";
buffer += " ldr "+reg+", =1\n";
//branch 2
buffer += "2: \n";
//push result to the stack
check_spill(reg);
}
LessThanEqual::LessThanEqual(Exp *lhs, Exp *rhs, int lineno): lhs(lhs), rhs(rhs), lineno(lineno) {}
std::string LessThanEqual::visit() {
PRINTDEBUG("LessThanEqual)")
std::string left = lhs->visit();
std::string right = rhs->visit();
if(left != "int" || right != "int") {
std::cerr << "Type Violation in Line " << lineno << " : bad operand types for binary operator '<='" << std::endl;
type_error = true;
}
return "boolean";
}
void LessThanEqual::evaluate(std::string ret_type) {
//evaluate expr
lhs->evaluate(SYMBOLIC);
rhs->evaluate(LITERAL);
std::string right = r_pop("r1");
std::string left = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
//compare equality
buffer += " cmp "+left+", "+right+"\n";
buffer += " ble 1f\n";
buffer += " ldr "+reg+", =0\n";
buffer += " b 2f\n";
//branch 1
buffer += "1: \n";
buffer += " ldr "+reg+", =1\n";
//branch 2
buffer += "2: \n";
//push result to the stack
check_spill(reg);
}
GreaterThan::GreaterThan(Exp *lhs, Exp *rhs, int lineno): lhs(lhs), rhs(rhs), lineno(lineno) {}
std::string GreaterThan::visit() {
PRINTDEBUG("(GreaterThan)")
std::string left = lhs->visit();
std::string right = rhs->visit();
if(left != "int" || right != "int") {
std::cerr << "Type Violation in Line " << lineno << " : bad operand types for binary operator '>'" << std::endl;
type_error = true;
}
return "boolean";
}
void GreaterThan::evaluate(std::string ret_type) {
lhs->evaluate(SYMBOLIC);
rhs->evaluate(LITERAL);
std::string right = r_pop("r1");
std::string left = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
//compare equality
buffer += " cmp "+left+", "+right+"\n";
buffer += " bgt 1f\n";
buffer += " ldr "+reg+", =0\n";
buffer += " b 2f\n";
//branch 1
buffer += "1: \n";
buffer += " ldr "+reg+", =1\n";
//branch 2
buffer += "2: \n";
check_spill(reg);
}
GreaterThanEqual::GreaterThanEqual(Exp *lhs, Exp *rhs, int lineno): lhs(lhs), rhs(rhs), lineno(lineno) {}
std::string GreaterThanEqual::visit() {
PRINTDEBUG("(GreaterThanEqual)")
std::string left = lhs->visit();
std::string right = rhs->visit();
if(left != "int" || right != "int") {
std::cerr << "Type Violation in Line " << lineno << " : bad operand types for binary operator '>='" << std::endl;
type_error = true;
}
return "boolean";
}
void GreaterThanEqual::evaluate(std::string ret_type) {
lhs->evaluate(SYMBOLIC);
rhs->evaluate(LITERAL);
std::string right = r_pop("r1");
std::string left = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
//compare equality
buffer += " cmp "+left+", "+right+"\n";
buffer += " bge 1f\n";
buffer += " ldr "+reg+", =0\n";
buffer += " b 2f\n";
//branch 1
buffer += "1: \n";
buffer += " ldr "+reg+", =1\n";
//branch 2
buffer += "2: \n";
check_spill(reg);
}
Plus::Plus(Exp *lhs, Exp *rhs, int lineno): lhs(lhs), rhs(rhs), lineno(lineno) {}
std::string Plus::visit() {
PRINTDEBUG("(Plus)")
std::string left = lhs->visit();
std::string right = rhs->visit();
if(left != "int" || right != "int") {
std::cerr << "Type Violation in Line " << lineno << " : bad operand types for binary operator '+'" << std::endl;
type_error = true;
}
return "int";
}
void Plus::evaluate(std::string ret_type) {
lhs->evaluate(SYMBOLIC);
rhs->evaluate(LITERAL);
std::string right = r_pop("r1");
std::string left = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
buffer += " add "+reg+", "+left+", "+right+"\n"; //add values from r0 and r1, store in r0
check_spill(reg);
}
Minus::Minus(Exp *lhs, Exp *rhs, int lineno): lhs(lhs), rhs(rhs), lineno(lineno) {}
std::string Minus::visit() {
PRINTDEBUG("(Minus)")
std::string left = lhs->visit();
std::string right = rhs->visit();
if(left != "int" || right != "int") {
std::cerr << "Type Violation in Line " << lineno << " : bad operand types for binary operator '-'" << std::endl;
type_error = true;
}
return "int";
}
void Minus::evaluate(std::string ret_type) {
lhs->evaluate(SYMBOLIC);
rhs->evaluate(LITERAL);
std::string right = r_pop("r1");
std::string left = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
buffer += " sub "+reg+", "+left+", "+right+"\n"; //add values from r0 and r1, store in r0
check_spill(reg);
}
Times::Times(Exp *lhs, Exp *rhs, int lineno): lhs(lhs), rhs(rhs), lineno(lineno) {}
std::string Times::visit() {
PRINTDEBUG("(Times)")
std::string left = lhs->visit();
std::string right = rhs->visit();
if(left != "int" || right != "int") {
std::cerr << "Type Violation in Line " << lineno << " : bad operand types for binary operator '*'" << std::endl;
type_error = true;
}
return "int";
}
void Times::evaluate(std::string ret_type) {
lhs->evaluate(SYMBOLIC);
rhs->evaluate(SYMBOLIC);
std::string right = r_pop("r1");
std::string left = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
buffer += " mul "+reg+", "+left+", "+right+"\n"; //add values from r0 and r1, store in r0
check_spill(reg);
}
Div::Div(Exp *lhs, Exp *rhs, int lineno): lhs(lhs), rhs(rhs), lineno(lineno) {}
std::string Div::visit() {
PRINTDEBUG("(Div)")
std::string left = lhs->visit();
std::string right = rhs->visit();
if(left != "int" || right != "int") {
std::cerr << "Type Violation in Line " << lineno << " : bad operand types for binary operator '/'" << std::endl;
type_error = true;
}
return "int";
}
void Div::evaluate(std::string ret_type) {
//std::cerr << "Division operator '/' is not supported by ARM, and not implemented by compiler\n";
lhs->evaluate(SYMBOLIC);
rhs->evaluate(SYMBOLIC);
std::string right = r_pop("r1");
std::string left = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
buffer += " mov r1, #0\n";
buffer += " mov r3, "+right+"\n";
buffer += " cmp "+left+", "+right+"\n";
buffer += " bge 5f\n";
buffer += " ldr r2, =0\n";
buffer += " b 6f\n";
buffer += "5:\n";
buffer += " ldr r2, =1\n";
buffer += "6:\n";
buffer += " cmp r2, #1\n";
buffer += " beq 7f\n";
buffer += " b 8f\n";
buffer += "7:\n";
buffer += " add "+right+", "+right+", r3\n";
buffer += " add r1, r1, #1\n";
buffer += " cmp "+left+", "+right+"\n";
buffer += " bge 5f\n";
buffer += " ldr r2, =0\n";
buffer += " b 6f\n";
buffer += "5:\n";
buffer += " ldr r2, =1\n";
buffer += "6:\n";
buffer += " cmp r2, #1\n";
buffer += " beq 7b\n";
buffer += " b 8f\n";
buffer += "8:\n";
buffer += " mov "+reg+", r1\n";
//check if register needs to spill, other wise reg should hold value
check_spill(reg);
}
ArrayLookup::ArrayLookup(Identifier *i, std::list<Exp *> *el): i(i), el(el) {}
std::string ArrayLookup::visit() {
PRINTDEBUG("(ArrayLookup)")
return type_local_scope[i->toString()]->t->getType();
}
void ArrayLookup::evaluate(std::string ret_type) {
//iterate over index epxressions
std::string id = i->toString();
std::list<Exp *>::iterator expIter = el->begin();
for(expIter = el->begin(); expIter != el->end(); expIter++){
(*expIter)->evaluate(SYMBOLIC);
break;
//only works for 1D array TODO
}
//calculate index
std::string reg = r_pop("r2");
buffer += " mov r1, #4\n"; //add values from r0 and r1, store in r0
buffer += " mul r3, r1, "+reg+"\n"; //add values from r0 and r1, store in r0
//load value at index of array
buffer += " ldr r12, ="+currentClass->getName()+"_"+currentMethodName+"_"+id+"\n"; //store the location sp + offset in r1
buffer += " add r12, r12, r3\n";
//place value at index of array
reg = r_push(SYMBOLIC);
buffer += " ldr "+reg+", [r12]\n"; //store the value of r1 on the stack at location r1 (sp + offset)
check_spill(reg);
}
ArrayLength::ArrayLength(Exp *e): e(e) {}
std::string ArrayLength::visit() {
PRINTDEBUG("(ArrayLength)")
return "int";
}
void ArrayLength::evaluate(std::string ret_type) {
int val = 1;
void *ptr = &val;
}
Call::Call(Exp *e, Identifier *i, std::list<Exp *> *el, int lineno): e(e), i(i), el(el), lineno(lineno) {}
std::string Call::visit() {
PRINTDEBUG("(Call)")
std::string objectType = e->visit();
//check if class object type exists
if(!classTable.count(objectType)){
std::cerr << "Type Violation in Line " << lineno << " : error cannot find class:" << objectType << std::endl;
type_error = true;
return "int";
}
ClassDecl *cl = classTable[objectType];
std::string methodName = i->toString();
std::map<std::string, MethodDecl *> methods = cl->methods;
//check if method exists
if(!methods.count(methodName)){
std::cerr << "Type Violation in Line " << lineno << " : error method:" << methodName << " does not belong to class:" << objectType << std::endl;
type_error = true;
return "int";
}
MethodDecl *method = methods[methodName];
std::list<Formal *> *fl = method->fl;
std::string returnType = method->t->getType();
//check if arguments size matches parameters size
int defSize = fl->size();
int argSize = el->size();
if(defSize != argSize) {
std::cerr << "Type Violation in Line " << lineno << " : error expected:" << defSize << " argument(s) but recieved:" << argSize << std::endl;
type_error = true;
return returnType;
}
//check if parameters are valid
std::list<Formal *>::iterator formalIter;
std::list<Exp *>::iterator expIter = el->begin();
for(formalIter = fl->begin(); formalIter != fl->end(); formalIter++){
std::string param_Type = (*formalIter)->t->getType();
std::string expr_Type = (*expIter)->visit();
//type check parameter
if(param_Type != expr_Type) {
std::cerr << "Type Violation in Line " << lineno << " : error invalid arg types for method:" << methodName << std::endl;
type_error = true;
return returnType;
}
expIter++;
}
//iterate over exper list for shits
return returnType;
}
void Call::evaluate(std::string ret_type) {
//init method and class name
std::string className = currentClass->getName();
std::string methodName = i->toString();
//push parameters onto stack
int n = el->size();
if(n > 0) {
int k = 0;
//push first parameter
std::list<Exp *>::iterator expIter = el->end();
expIter--; (*expIter)->evaluate(SYMBOLIC);
std::string reg = r_pop(SUPPRESS);
if(reg != SUPPRESSED)
buffer += " push {"+reg+"}\n";
//push remaining paramters
for(expIter = el->begin(); expIter != el->end(); ++expIter){
if(k == n - 1)
break;
(*expIter)->evaluate(SYMBOLIC); k++;
reg = r_pop(SUPPRESS);
if(reg != SUPPRESSED)
buffer += " push {"+reg+"}\n";
}
}
//call function
buffer += " bl "+className+"_"+methodName+"\n";
//push return value onto stack
std::string reg = r_push(SYMBOLIC);
buffer += " mov "+reg+", r0\n";
check_spill(reg);
}
IntegerLiteral::IntegerLiteral(int i): num(i) {}
std::string IntegerLiteral::visit() {
PRINTDEBUG("(IntegerLiteral)")
return "int";
}
void IntegerLiteral::evaluate(std::string ret_type) {
if(ret_type == LITERAL) {
r_push("#"+std::to_string(num));
} else {
std::string reg = r_push(SYMBOLIC);
buffer += " ldr "+reg+", ="+std::to_string(num)+"\n"; //load value into register 0
check_spill(reg);
}
}
std::string True::visit() {
PRINTDEBUG("(True)")
return "boolean";
}
void True::evaluate(std::string ret_type) {
if(ret_type == LITERAL) {
r_push("#1");
} else {
std::string reg = r_push(SYMBOLIC);
buffer += " ldr "+reg+", =1\n"; //load value into r0
check_spill(reg);
}
}
std::string False::visit() {
PRINTDEBUG("(False)")
return "boolean";
}
void False::evaluate(std::string ret_type) {
if(ret_type == LITERAL) {
r_push("#0");
} else {
std::string reg = r_push(SYMBOLIC);
buffer += " ldr "+reg+", =0\n"; //load value into r0
check_spill(reg);
}
}
std::string This::visit() {
return currentClass->getName();
}
void This::evaluate(std::string ret_type) {
ClassDecl *cl = currentClass;
void *ptr = &(*cl);
currentClassName = cl->getName();
}
IdentifierExp::IdentifierExp(std::string str): id(str) {}
std::string IdentifierExp::visit() {
PRINTDEBUG("(IdentifierExp)")
return type_local_scope[id]->t->getType();
}
void IdentifierExp::evaluate(std::string ret_type) {
int offset = scope[id];
std::string reg = r_push(SYMBOLIC);
//load var from data
buffer += " ldr "+reg+", ="+currentClass->getName()+"_"+currentMethodName+"_"+id+"\n"; //store the address of sp + offset in r0
buffer += " ldr "+reg+", ["+reg+"]\n"; //load into r0 the value store at r0 stack location
check_spill(reg);
}
NewArray::NewArray(std::list<Exp *> *el): el(el) {}
std::string NewArray::visit() {
PRINTDEBUG("(NewArray)")
return "int []";
}
void NewArray::evaluate(std::string ret_type) {
//std::cerr << "new array\n";
//iterate over index epxressions
std::list<Exp *>::iterator expIter = el->begin();
for(expIter = el->begin(); expIter != el->end(); expIter++){
(*expIter)->evaluate(SYMBOLIC);
break;
//only works for 1D array TODO
}
//calculate size
std::string reg = r_pop("r0");
buffer += " mov r1, #4\n"; //add values from r0 and r1, store in r0
buffer += " mul r0, r1, "+reg+"\n"; //add values from r0 and r1, store in r0
//malloc
buffer += " bl malloc\n"; //add values from r0 and r1, store in r0
//return regist address to malloc memory
reg = r_push(SYMBOLIC);
buffer += " mov "+reg+", r0\n"; //add values from r0 and r1, store in r0
check_spill(reg);
}
NewObject::NewObject(Identifier *i): i(i) {}
std::string NewObject::visit() {
PRINTDEBUG("(NewObject)")
return i->toString();
}
void NewObject::evaluate(std::string ret_type) {
ClassDecl *cl = classTable[i->toString()];
void *ptr = &(*cl);
currentClass = cl;
currentClassName = cl->getName();
}
Not::Not(Exp *e, int lineno): e(e), lineno(lineno) {}
std::string Not::visit() {
PRINTDEBUG("(Not)")
if(e->visit() != "boolean") {
std::cerr << "Type Violation in Line " << lineno << " : incomparable types for binary operator '!'" << std::endl;
type_error = true;
}
return "boolean";
}
void Not::evaluate(std::string ret_type) {
e->evaluate(SYMBOLIC);
std::string op = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
//compare equality
buffer += " cmp "+op+", #0\n";
buffer += " beq 1f\n";
buffer += " ldr "+reg+", =0\n";
buffer += " b 2f\n";
//branch 1
buffer += "1: \n";
buffer += " ldr "+reg+", =1\n";
//branch 2
buffer += "2: \n";
check_spill(reg);
}
NegativeExp::NegativeExp(Exp *e, int lineno): e(e), lineno(lineno) {}
std::string NegativeExp::visit() {
PRINTDEBUG("(NegativeExp)")
if(e->visit() != "int") {
std::cerr << "Type Violation in Line " << lineno << " : incomparable types for binary operator '-'" << std::endl;
type_error = true;
}
return "int";
}
void NegativeExp::evaluate(std::string ret_type) {
e->evaluate(SYMBOLIC);
std::string op = r_pop("r0");
std::string reg = r_push(SYMBOLIC);
buffer += " neg "+reg+", "+op+"\n";
check_spill(reg);
}
PositiveExp::PositiveExp(Exp *e, int lineno): e(e), lineno(lineno) {}
std::string PositiveExp::visit() {
PRINTDEBUG("(PositiveExp)")
if(e->visit() != "int") {
std::cerr << "Type Violation in Line " << lineno << " : incomparable types for binary operator '+'" << std::endl;
type_error = true;
}
return "int";
}
void PositiveExp::evaluate(std::string ret_type) {
e->evaluate(SYMBOLIC);
}
Index::Index(Exp *e): e(e) {}
std::string Index::visit() {
PRINTDEBUG("(Index)")
}
void Index::evaluate(std::string ret_type) {
e->evaluate(LITERAL);
//std::cerr << "calculating index\n";
}
/******************* STATEMENT CLASS ****************************/
Block::Block(std::list<Statement *> *sl): sl(sl) {}
void Block::visit() {
//evaluate Block
std::list<Statement *>::iterator stmtIter;
for(stmtIter = sl->begin(); stmtIter != sl->end(); stmtIter++){
//(*stmtIter)->visit();
PRINTDEBUG("(Statement)")
}
PRINTDEBUG("(Block)")
}
void Block::evaluate() {
//evaluate Block
std::list<Statement *>::iterator stmtIter;
for(stmtIter = sl->begin(); stmtIter != sl->end(); stmtIter++){
(*stmtIter)->evaluate();
PRINTDEBUG("(Statement)")
}
PRINTDEBUG("(Block)")
buffer += "\n";
}
If::If(Exp *e, Statement *s1, Statement *s2, int lineno): e(e), s1(s1), s2(s2), lineno(lineno) {}
void If::visit() {
PRINTDEBUG("(If)")
if(e->visit() != "boolean") {
std::cerr << "Type Violation in Line " << lineno << " : incomparable types" << std::endl;
type_error = true;
}
s1->visit();
PRINTDEBUG("(Else)")
s2->visit();
}
void If::evaluate() {
//evaluate boolean expr
e->evaluate(SYMBOLIC);
std::string reg = r_pop("r0");
//compare equality
buffer += " cmp "+reg+", #1\n";
buffer += " beq 1f\n";
s2->evaluate();
buffer += " b 2f\n";
//branch 1
buffer += "1: \n";
s1->evaluate();
//branch 2
buffer += "2: \n";
}
While::While(Exp *e, Statement *s, int lineno): e(e), s(s), lineno(lineno) {}
void While::visit() {
if(e->visit() != "boolean") {
std::cerr << "Type Violation in Line " << lineno << " : incomparable types" << std::endl;
type_error = true;
}
s->visit();
PRINTDEBUG("(While)")
}
void While::evaluate() {
e->evaluate(SYMBOLIC);
std::string reg = r_pop("r0");
//check if loop should begin
buffer += " cmp "+reg+", #1\n";
buffer += " beq 3f\n";
//done
buffer += " b 4f\n";
//branch 1
buffer += "3: \n";
s->evaluate();
e->evaluate(SYMBOLIC);
reg = r_pop("r1");
buffer += " cmp "+reg+", #1\n";
buffer += " beq 3b\n";
buffer += " b 4f\n";
//done
buffer += "4: \n";
}
Print::Print(Exp *e, int lineno): e(e), lineno(lineno) {}
void Print::visit() {
if(e->visit() != "int") {
std::cerr << "Type Violation in Line " << lineno << " : bad operand types" << std::endl;
type_error = true;
}
PRINTDEBUG("(Print)")
}
void Print::evaluate() {
e->evaluate(LITERAL);
std::string reg = r_pop("r1");
buffer += " mov r1, "+reg+"\n";
buffer += " ldr r0, =int_print\n";
buffer += " bl printf\n";
}
Println::Println(Exp *e, int lineno): e(e), lineno(lineno) {}
void Println::visit() {
PRINTDEBUG("(Println)");
if(e->visit() != "int") {
std::cerr << "Type Violation in Line " << lineno << " : bad operand types" << std::endl;
type_error = true;
}
}
void Println::evaluate() {
e->evaluate(LITERAL);
std::string reg = r_pop("r1");
buffer += " mov r1, "+reg+"\n";
buffer += " ldr r0, =int_println\n";
buffer += " bl printf\n";
}
PrintString::PrintString(std::string str): str(str) {}
void PrintString::visit() {
PRINTDEBUG("(PrintString)")
}
void PrintString::evaluate() {
int n = text.size() + 1;
std::string txt = "string"+std::to_string(n)+": .asciz \""+str+"\"\n";
buffer += " ldr r0, =string"+std::to_string(n)+"\n";
buffer += " bl printf\n";
text.push_back(txt);
}
PrintStringln::PrintStringln(const std::string str): str(str) {}
void PrintStringln::visit() {
PRINTDEBUG("(PrintStringln)")
}
void PrintStringln::evaluate() {
int n = text.size() + 1;
std::string txt = "string"+std::to_string(n)+": .asciz \""+str+"\\n\"\n";
buffer += " ldr r0, =string"+std::to_string(n)+"\n";
buffer += " bl printf\n";
text.push_back(txt);
}
Assign::Assign(Identifier *i, Exp *e, int lineno): i(i), e(e), lineno(lineno) {}
void Assign::visit() {
PRINTDEBUG("(Assign)")
VarDecl *var = type_local_scope[i->toString()];
if(var->t->getType() != (e->visit())) {
std::cerr << "Type Violation in Line " << lineno << " : invalid assignment type" << std::endl;
type_error = true;
}
}
void Assign::evaluate() {
std::string id = i->toString();
int offset = scope[id];
// std::cerr << "Assign\n";
//evaluate expr
e->evaluate(SYMBOLIC);
std::string reg = r_pop("r0");
//std::cout << "assigning at sp+" << offset << " to var:" << id << std::endl;
//assign into data
buffer += " ldr r12, ="+currentClass->getName()+"_"+currentMethodName+"_"+id+"\n"; //store the location sp + offset in r1
buffer += " str "+reg+", [r12]\n"; //store the value of r0 on the stack at location r1 (sp + offset)
}
ArrayAssign::ArrayAssign(Identifier *i, std::list<Exp *> *el, Exp *e): i(i), el(el), e(e) {}
void ArrayAssign::visit() {
PRINTDEBUG("(ArrayAssign DANGER: no type checking for array assignment established)")
}
void ArrayAssign::evaluate() {
PRINTDEBUG("(Statment Evaluation Broken)")
std::string id = i->toString();
//iterate over index epxressions
std::list<Exp *>::iterator expIter = el->begin();
for(expIter = el->begin(); expIter != el->end(); expIter++){
(*expIter)->evaluate(SYMBOLIC);
break;
//only works for 1D array TODO
}
//calculate index
std::string reg = r_pop("r2");
buffer += " mov r1, #4\n"; //add values from r0 and r1, store in r0
buffer += " mul r3, r1, "+reg+"\n"; //add values from r0 and r1, store in r0
//evaluate expr
e->evaluate(SYMBOLIC);
reg = r_pop("r2");
//assign value into address offset of array