-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodegen.py
867 lines (700 loc) · 26.7 KB
/
codegen.py
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
# Intermediate code generator
from collections import deque
from absmc import abstract_machine
from ast import *
# Create the abstract machine
am = abstract_machine()
outputFile = None
localVarRegisters = {} #Key: <var id>, Value: <register number>
argumentRegisters = {
0 : am.getNewArgReg()
}
#A dict of labels associated with each method in each class
#Use for method calls
methodLabels = {} #Key: <Method ID>, Value: <label name>
#offset from the base of the heap to the top of the heap
#Allocate memory for new objects here
heapTop = 0
#Use to get heap offset for object instances
heapOffset = {} #Key: <Variable id>, Value: <heap offset>
def generateCode(filename):
global outputFile
outputName = "{0}.ami".format(filename)
outputFile = open(outputName, 'w')
am.allocateStaticFields()
code = generateClassCode(ast.classtable)
outputFile.write(code)
def generateClassCode(classtable):
code = ""
# Iterate over all parsed classes
for key in classtable:
dClass = classtable[key]
code += "# ---{0}---\n".format(dClass.name)
# Generate code blocks for every method in the class
for method in dClass.methods:
code += generateMethodCode(method)
# Generate code blocks for every constructor in the class
for constr in dClass.constructors:
code += generateConstructorCode(constr)
return code
def generateMethodCode(method):
global localVarRegisters, methodLabels, am
outputCode = "" #Final output to write to file
blockLabel = getMethodLabel(method)
outputCode += "{0}:\n".format(blockLabel) # add block label
methodLabels[method.id] = blockLabel
localVarRegisters = {} #reset dict for local vars
#Allocate registers for all local vars in this method
varTable = method.vars.vars
for block in varTable:
for varname in varTable[block]:
var = varTable[block][varname]
localVarRegisters[var.id] = am.getNewReg()
#Add method paramters to this dict
params = method.vars.get_params()
for i in xrange(0, len(params)):
localVarRegisters[params[i]] = "a" + str(i)
argumentRegisters[am.argumentRegNum] = am.getNewArgReg()
if isinstance(method.body, SkipStmt):
outputCode += ""
else:
for stmt in method.body.stmtlist:
outputCode += generateStmtCode(stmt)
#Add return statement
outputCode += "ret \n\n"
return outputCode
def generateConstructorCode(constructor):
global localVarRegisters, am
code = ""
constructorLabel = "C_{0}".format(constructor.id)
c_class = ast.lookup(ast.classtable, constructor.name)
class_field_len = len(c_class.fields)
heapExtendReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(heapExtendReg, class_field_len)
hallocReg = am.getNewReg()
code += "halloc {0}, {1}\n".format(hallocReg, heapExtendReg)
localVarRegisters = {} #reset dict for local vars
#Allocate registers for all local vars in this method
varTable = constructor.vars.vars
for block in varTable:
for varname in varTable[block]:
var = varTable[block][varname]
localVarRegisters[var.id] = am.getNewReg()
#Add method paramters to this dict
params = constructor.vars.get_params()
for i in xrange(0, len(params)):
argReg = "a" + str(i)
localVarRegisters[params[i]] = argReg
argumentRegisters[am.argumentRegNum] = am.getNewArgReg()
code += "save {0}\n".format(argReg)
code += "save {0}\n".format(hallocReg)
code += "call {0}\n".format(constructorLabel)
# restore address of new obj
code += "restore {0}\n".format(hallocReg)
# restore arguments
i = len(params) - 1
while 0 <= i:
argReg = "a" + str(i)
code += "restore {0}\n".format(argReg)
i -= 1
code += "{0}:\n".format(constructorLabel)
code += generateStmtCode(constructor.body)
return code
def generateStmtCode(stmt):
if isinstance(stmt, IfStmt):
return generateIfStmtCode(stmt)
elif isinstance(stmt, WhileStmt):
return generateWhileStmtCode(stmt)
elif isinstance(stmt, ForStmt):
return generateForStmtCode(stmt)
elif isinstance(stmt, ReturnStmt):
return generateReturnStmtCode(stmt)
elif isinstance(stmt, ExprStmt):
return generateExprStmtCode(stmt)
elif isinstance(stmt, BlockStmt):
return generateBlockStmtCode(stmt)
elif isinstance(stmt, BreakStmt):
return generateBreakStmtCode(stmt)
elif isinstance(stmt, SkipStmt):
return generateSkipStmtCode(stmt)
elif isinstance(stmt, ContinueStmt):
return generateContinueStmtCode(stmt)
# statement of form if (e) S1 else S2 is evaluated by first
# evaluating e. If e evaluates to true, then S1 is executed. If e evaluates to
# false, then S2 is executed. A statement of the form if (e) S1 is executed as
# if it were if (e) S1 else ;.
def generateIfStmtCode(ifStmt):
code = ""
global am
condRegandCode = generateExprCode(ifStmt.condition)
condReg = condRegandCode[0]
condCode = condRegandCode[1]
code += condCode # add code for conditon
elseLabel = "else_{0}".format(am.getUniqueLabelNum())
code += "bz {0}, {1}\n".format(condReg, elseLabel)
# add the then code
thenCode = generateStmtCode(ifStmt.thenpart)
code += thenCode
# add the else part
# generate new label
code += "{0}:\n".format(elseLabel)
elseCode = generateStmtCode(ifStmt.elsepart)
code += elseCode
return code
# A statement of the form while (e) S1 is executed in the following steps:
# 1. Evaluate e.
# 2. If the result of evaluating e is true, then execute S1. The execution
# then starts over again at step 1.
# 3. If the result of evaluating e is false, then execution of the while statement is finished.
def generateWhileStmtCode(whileStmt):
code = ""
global am
condRegandCode = generateExprCode(whileStmt.cond)
condReg = condRegandCode[0]
condCode = condRegandCode[1]
code += condCode # add code for while condition
whileLabel = "Loop_{0}".format(am.getUniqueLabelNum())
am.loopLabelStack.append(whileLabel)
endWhileLabel = "End_loop_{0}".format(am.getUniqueLabelNum())
am.loopEndLabelStack.append(endWhileLabel)
code += "{0}:\n".format(whileLabel)
code += "bz {0}, {1}\n".format(condReg, endWhileLabel)
# generate while body code
bodyCode = generateStmtCode(whileStmt.body)
code += bodyCode
# add the jmp statement at the end of body code
code += "jmp {0}\n".format(whileLabel)
# output end while label
code += "{0}:\n".format(endWhileLabel)
return code
def generateForStmtCode(forStmt):
code = ""
global am
# output init code
initRegandCode = generateExprCode(forStmt.init)
initReg = initRegandCode[0]
initCode = initRegandCode[1]
code += initCode
continueLabel = "Loop_{0}".format(am.getUniqueLabelNum())
endLabel = "End_Loop_{0}".format(am.getUniqueLabelNum())
am.loopEndLabelStack.append(endLabel)
am.loopLabelStack.append(continueLabel)
# output condition code
condRegandCode = generateExprCode(forStmt.cond)
condReg = condRegandCode[0]
condCode = condRegandCode[1]
code += "{0}:\n".format(continueLabel)
code += condCode
code += "bz {0}, {1}\n".format(condReg, endLabel)
# output update code
oneReg = am.getNewReg()
code += "mode_immed_i {0}, 1\n".format(oneReg)
code += "iadd {0}, {1}, {2}\n".format(initReg, initReg, oneReg)
#output for body
code += generateStmtCode(forStmt.body)
code += "jmp {0}\n".format(continueLabel)
#output end label
code += "{0}:\n".format(endLabel)
return code
def generateReturnStmtCode(returnStmt):
code = ""
if returnStmt.expr:
ResultRegandCode = generateExprCode(returnStmt.expr)
code += ResultRegandCode[1]
code += "move {0}, {1}\n".format("a0", ResultRegandCode[0])
code += "ret\n"
return code
def generateExprStmtCode(exprStmt):
code = ""
expr = exprStmt.expr
if isinstance(expr, AssignExpr):
code += generateAssignExprCode(expr)
elif isinstance(expr, MethodInvocationExpr):
code += generateMethodInvocationExprCode(expr)[1]
return code
def generateBlockStmtCode(blockStmt):
code = ""
for stmt in blockStmt.stmtlist:
code += generateStmtCode(stmt)
return code
def generateBreakStmtCode(breakStmt):
global am
mostRecentEndLabel = am.loopEndLabelStack.pop()
code = "jmp {0}\n".format(mostRecentEndLabel)
return code
def generateSkipStmtCode(skipStmt):
return ""
def generateContinueStmtCode(contStmt):
global am
mostRecentLoopLabel = am.loopLabelStack.pop()
code = "jmp {0}\n".format(mostRecentLoopLabel)
return code
def generateExprCode(expr):
if isinstance(expr, ConstantExpr):
return generateConstExprCode(expr)
elif isinstance(expr, VarExpr):
return generateVarExprCode(expr)
elif isinstance(expr, UnaryExpr):
return generateUnaryExprCode(expr)
elif isinstance(expr, BinaryExpr):
return generateBinaryExprCode(expr)
elif isinstance(expr, AutoExpr):
return generateAutoExpr(expr)
elif isinstance(expr, FieldAccessExpr):
return generateFieldAccessExprCode(expr)
elif isinstance(expr, MethodInvocationExpr):
return generateMethodInvocationExprCode(expr)
elif isinstance(expr, NewObjectExpr):
return generateNewObjectExprCode(expr)
elif isinstance(expr, AssignExpr):
return generateAssignExprCode(expr)
#Generate code to load the constant expr into a register before it is used
def generateConstExprCode(constExpr):
code = ""
global am
#Register to set value
register = am.getNewReg()
if constExpr.kind == 'int':
code += "move_immed_i {0}, {1}\n".format(register, constExpr.int)
elif constExpr.kind == 'float':
code += "move_immed_f {0}, {1}\n".format(register, constExpr.float)
elif constExpr.kind == 'Null':
#TODO: Null value?
pass
elif constExpr.kind == 'True':
code += "move_immed_i {0}, 1\n".format(register)
elif constExpr.kind == 'False':
code += "move_immed_i {0}, 0\n".format(register)
#A tuple with the register holding this expression's value and the generated code string
return (register, code)
#Generate code to load the varExpr's value into a register before it is used
def generateVarExprCode(varExpr):
code = ""
global am
var = varExpr.var
code += "#LINE {0} - Load value for '{1}'\n".format(varExpr.lines, var.name)
#VarExpr is either for object instance, static field or, local variable
#Try getting heap offset
try:
#Load base of object
objOffset = heapOffset[var.id]
objBaseReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(objBaseReg, objOffset)
return (objBaseReg, code)
except KeyError, e:
pass
#Try getting static area offset
try:
offset = am.staticFieldOffset[var.id]
offsetReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(offsetReg, offset)
valueReg = am.getNewReg()
code += "hload {0}, sap, {1}\n".format(valueReg, offsetReg)
return (valueReg, code)
#Neiter loading object from heap or static area worked, must be in a temp register
except:
register = localVarRegisters[var.id]
return (register, code)
def generateUnaryExprCode(unaryExpr):
code = ""
global am
expr = unaryExpr.arg
regandcode = generateExprCode(expr)
exprReg = regandcode[0]
code += regandcode[1]
#Generate code to multiply the expression by -1
if unaryExpr.uop == 'uminus':
negOneReg = am.getNewReg()
code += "move_immed_i {0}, -1\n".format(negOneReg)
resultReg = am.getNewReg()
code += "imul {0}, {1}, {2}\n".format(resultReg, exprReg, negOneReg)
return (resultReg, code)
#Generate code to negate (NOT) the expression
elif unaryExpr.uop == 'neg':
trueLabel = getTrueCaseLabel()
afterLabel = getAfterCaseLabel()
#Code to branch if the expr value is 'true'
code += "bnz {0}, {1}\n".format(exprReg, trueLabel)
code += "move_immed_i {0}, 1\n".format(exprReg) #set the valuereg to 'false'
code += "jmp {0}\n".format(afterLabel)
#Branch code if expr is false
code += "\n{0}:\n".format(trueLabel)
code += "move_immed_i {0}, 0\n".format(exprReg) #set the valuereg to 'false'
code += "jmp {0}\n".format(afterLabel)
#Continuing after label
code += "\n{0}:\n".format(afterLabel)
return (exprReg, code)
def generateBinaryExprCode(binaryExpr):
code = ""
global am
leftExpr = binaryExpr.arg1
rightExpr = binaryExpr.arg2
#generate code and get the registers for the left and right expressions
leftRegandCode = generateExprCode(leftExpr)
rightRegandCode = generateExprCode(rightExpr)
leftExprReg = leftRegandCode[0]
rightExprReg = rightRegandCode[0]
code += leftRegandCode[1]
code += rightRegandCode[1]
#Result of binary operation will be in this register
resultReg = am.getNewReg()
if binaryExpr.bop in ['add', 'sub', 'mul', 'div', 'lt', 'leq', 'gt', 'geq']:
code += "#LINE {0} - binary '{1}' op\n".format(binaryExpr.lines, binaryExpr.bop)
opcode = "i" + binaryExpr.bop
code += "{0} {1}, {2}, {3}\n".format(opcode, resultReg, leftExprReg, rightExprReg)
elif binaryExpr.bop == 'and':
code += "#LINE {0} - binary '{1}' op\n".format(binaryExpr.lines, binaryExpr.bop)
falseLabel = getFalseCaseLabel()
afterLabel = getAfterCaseLabel()
#Code to branch if either of the expressions are not true
code += "bz {0}, {1}\n".format(leftExprReg, falseLabel)
code += "bz {0}, {1}\n".format(rightExprReg, falseLabel)
#Code to set expression's value to true
code += "#logical expression is true\n"
code += "move_immed_i {0}, 1\n".format(resultReg)
code += "jmp {0}\n".format(afterLabel)
#Code for the false case
code += "\n{0}:\n".format(falseLabel)
code += "#logical expression is false\n"
code += "move_immed_i {0}, 0\n".format(resultReg)
code += "jmp {0}\n".format(afterLabel)
#Label to continue after this conditional case
code += "\n{0}:\n".format(afterLabel)
elif binaryExpr.bop == 'or':
code += "#LINE {0} - binary '{1}' op\n".format(binaryExpr.lines, binaryExpr.bop)
trueLabel = getTrueCaseLabel()
afterLabel = getAfterCaseLabel()
#Code to branch if either of the expressions are not true
code += "bnz {0}, {1}\n".format(leftExprReg, trueLabel)
code += "bnz {0}, {1}\n".format(rightExprReg, trueLabel)
#Code to set expression's value to false
code += "#logical expression is false\n"
code += "move_immed_i {0}, 0\n".format(resultReg)
code += "jmp {0}\n".format(afterLabel)
#Code for the true case
code += "\n{0}:\n".format(trueLabel)
code += "#logical expression is true\n"
code += "move_immed_i {0}, 1\n".format(resultReg)
code += "jmp {0}\n".format(afterLabel)
#Label to continue after this conditional case
code += "\n{0}:\n".format(afterLabel)
elif binaryExpr.bop == "==":
code += "#LINE {0} - binary '{1}' op\n".format(binaryExpr.lines, binaryExpr.bop)
falseLabel = getFalseCaseLabel()
afterLabel = getAfterCaseLabel()
subResultReg = am.getNewReg()
#Code to check equivalence
code += "isub {0}, {1}, {2}\n".format(subResultReg, leftExprReg, rightExprReg)
code += "bnz {0}, {1}\n".format(subResultReg, falseLabel)
#Code to set equivalence to true
code += "#Equivalence is true\n"
code += "move_immed_i {0}, 1\n".format(resultReg)
code += "jmp {0}\n".format(afterLabel)
#Code for false equivalence
code += "\n{0}:\n".format(falseLabel)
code += "#Equivalence is false"
code += "move_immed_i {0}, 0\n".format(resultReg)
code += "jmp {0}\n".format(afterLabel)
#Label to continue after equivalence check
code += "\n{0}:\n".format(afterLabel)
else: #For binary op '!='
code += "#LINE {0} - binary '{1}' op\n".format(binaryExpr.lines, binaryExpr.bop)
falseLabel = getFalseCaseLabel()
afterLabel = getAfterCaseLabel()
subResultReg = am.getNewReg()
#Code to check equivalence
code += "isub {0}, {1}, {2}\n".format(subResultReg, leftExprReg, rightExprReg)
code += "bz {0}, {1}\n".format(subResultReg, falseLabel)
#Code to set equivalence to true
code += "#Equivalence is true\n"
code += "move_immed_i {0}, 1\n".format(resultReg)
code += "jmp {0}\n".format(afterLabel)
#Code for false equivalence
code += "\n{0}:\n".format(falseLabel)
code += "#Equivalence is false"
code += "move_immed_i {0}, 0\n".format(resultReg)
code += "jmp {0}\n".format(afterLabel)
#Label to continue after equivalence check
code += "\n{0}:\n".format(afterLabel)
return (resultReg, code)
def generateAssignExprCode(assignExpr):
code = ""
global am
#Load a register with the rhs expression
rhsRegandCode = generateExprCode(assignExpr.rhs)
#Load a register with the address of the lhs expression
lhsRegandCode = generateLCode(assignExpr.lhs)
lhsAddrReg = lhsRegandCode[0]
rhsValueReg = rhsRegandCode[0]
code += lhsRegandCode[1]
code += rhsRegandCode[1]
if len(lhsRegandCode) == 2:
zeroReg = am.getNewReg()
code += "#LINE {0} - Store the value in {1} at addr in {2}\n".format(assignExpr.lines, rhsValueReg, lhsAddrReg)
code += "move_immed_i {0}, 0\n".format(zeroReg)
code += "hstore {0}, {1}, {2}\n".format(zeroReg, lhsAddrReg, rhsValueReg)
#The length is 3 and thus, lhsAddrReg is just a temp register and NOT a heap address
else:
code += "#LINE {0} - Store the value in {1} in {2}\n".format(assignExpr.lines, rhsValueReg, lhsAddrReg)
code += "move {0}, {1}\n".format(lhsAddrReg, rhsValueReg)
return code
def generateMethodInvocationExprCode(methodInvocationExpr):
code = ""
global am
line = methodInvocationExpr.lines
method = methodInvocationExpr.method
methodLabel = methodLabels[method.id]
code += "#LINE {0} - Call method '{1}'\n".format(line, method.name)
#Save argument registers onto the stack
code += "#Save registers\n"
for i in xrange(0, len(argumentRegisters)):
argReg = "a" + str(i)
code += "save {0}\n".format(argReg)
#Call the function
code += "call {0}\n".format(methodLabel)
#Move the return value from a0 to it's own temp register
code += "#Save return value to temp register\n"
returnValReg = am.getNewReg()
code += "move {0}, a0 #return value in {0}\n".format(returnValReg)
#Restore argument registers from stack
code += "#Restore registers\n"
for i in xrange(len(argumentRegisters), 0, -1):
argReg = "a" + str(i)
code += "restore {0}\n".format(argReg)
return (returnValReg, code)
#Generate code to load the address of the field access into a register
def generateLCode(expr):
code = ""
global am
if isinstance(expr, FieldAccessExpr):
fieldAccessExpr = expr
baseExpr = fieldAccessExpr.base
field = fieldAccessExpr.field
#FIELD ACCESS USING THIS KEYWORD IS NOT SUPPORTED
if isinstance(baseExpr, ThisExpr):
code += "#LINE {0} - Load addr for 'this' field access".format(expr.lines)
objID = fieldAccessExpr.objID
baseClass = baseExpr.current_class
#Load base of object
objOffset = heapOffset[objID]
objBaseReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(objBaseReg, offset)
#Load offset of field
fieldIndex = baseClass.lookup_field_index(field)
fieldIndexReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(fieldIndexReg, fieldIndex)
#Calculate the field's heap addr
fieldAddrReg = am.getNewReg()
code += "iadd {0}, {1}, {2}".format(fieldAddrReg, objBaseReg, fieldIndexReg)
return (fieldAddrReg, code)
#FIELD ACCESS USING SUPER KEYWORD IS NOT SUPPORTED
elif isinstance(baseExpr, SuperExpr):
code += "#LINE {0} - Load addr for 'super' field access".format(expr.lines)
objID = fieldAccessExpr.objID
baseClass = baseExpr.current_class.superclass
#Load base of object
objOffset = heapOffset[objID]
objBaseReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(objBaseReg, offset)
#Load offset of field
fieldIndex = baseClass.lookup_field_index(field)
fieldIndexReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(fieldIndexReg, fieldIndex)
#Calculate the field's heap addr
fieldAddrReg = am.getNewReg()
code += "iadd {0}, {1}, {2}".format(fieldAddrReg, objBaseReg, fieldIndexReg)
return (fieldAddrReg, code)
elif isinstance(baseExpr, ClassReferenceExpr):
#Calculate address of the static variable
code += "#LINE {0} - Retrieve address of field '{1}'\n".format(fieldAccessExpr.lines, field.name)
offset = am.staticFieldOffset[field.id]
offsetReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(offsetReg, offset)
fieldAddrReg = am.getNewReg()
code += "iadd {0}, sap, {1}\n".format(fieldAddrReg, offsetReg)
return (fieldAddrReg, code)
#Otherwise, baseExpr is an object instance, and is a VarExpr
else:
code += "#LINE {0} - Load address for var field access\n".format(expr.lines)
variable = baseExpr.var
objID = variable.id
baseClass = variable.type.baseclass
#Load base of object
objOffset = heapOffset[objID]
objBaseReg = am.getNewReg()
code += "move_immed_i {0}, {1} #obj offset\n".format(objBaseReg, objOffset)
#Load offset of field
fieldIndex = baseClass.lookup_field_index(field)
fieldIndexReg = am.getNewReg()
code += "move_immed_i {0}, {1} #field offset\n".format(fieldIndexReg, fieldIndex)
#Calculate the field's heap address
fieldAddrReg = am.getNewReg()
code += "iadd {0}, {1}, {2}\n".format(fieldAddrReg, objBaseReg, fieldIndexReg)
return (fieldAddrReg, code)
elif isinstance(expr, VarExpr):
variable = expr.var
objID = variable.id
#Try using this variable ID to retrive a heap offset
try:
objOffset = heapOffset[objID]
objOffsetReg = am.getNewReg()
code += "#LINE {0} - Get var '{1}' address\n".format(expr.lines, variable.name)
code += "move_immed_i {0}, {1}\n".format(objOffsetReg, objOffset)
return (objOffsetReg, code)
#This variable doesn't have a heap offset and thus, is not an object
#Use the associated temporary register as a location
except KeyError:
varReg = localVarRegisters[variable.id]
code += "#LINE {0} - var '{1}' is in {2}\n".format(expr.lines, variable.name, varReg)
#Return 3 elements to indicate the location is just a temp register and not a
#heap addrs
return (varReg, code, True)
def generateAutoExpr(autoExpr):
code = ""
global am
addrRegandCode = generateLCode(arg)
addrReg = addrRegandCode[0]
code += addrRegandCode[1]
operation = autoExpr.oper
order = autoExpr.when
valueReg = am.getNewReg()
if order == 'pre':
#Load the expression value using the addr register and a zero register
zeroReg = am.getNewReg()
code += "move_immed_i {0}, 0\n".format(zeroReg)
code += "hload {0}, {1}, {2}\n".format(valueReg, addrReg, zeroReg)
#add or decrement the value
if operation == 'inc':
code += "iadd {0}, {0}, 1\n".format(valueReg)
else:
code += "isub {0}, {0}, 1\n".format(valueReg)
#Store the value
code += "hstore {0}, {1}, {2}\n".format(zeroReg, addrReg, valueReg)
else: #Post order
#Load the expression value using the addr register and a zero register
zeroReg = am.getNewReg()
code += "move_immed_i {0}, 0\n".format(zeroReg)
code += "hload {0}, {1}, {2}\n".format(valueReg, addrReg, zeroReg)
#Use seperate value reg that is post incremented
postValueReg = am.getNewReg()
#add or decrement the value
if operation == 'inc':
code += "iadd {0}, {1}, 1\n".format(postValueReg, valueReg)
else:
code += "isub {0}, {1}, 1\n".format(postValueReg, valueReg)
#Store the post incremented value
code += "hstore {0}, {1}, {2}\n".format(zeroReg, addrReg, postValueReg)
return (valueReg, code)
def generateFieldAccessExprCode(expr):
code = ""
global am
baseExpr = expr.base
field = expr.field
fieldAccessExpr = expr
#FIELD ACCESS USING THIS KEYWORD IS NOT SUPPORTED
if isinstance(baseExpr, ThisExpr):
code += "#LINE {0} - Load value for 'this' field access".format(expr.lines)
objID = fieldAccessExpr.objID
baseClass = baseExpr.current_class
#Load base of object
objOffset = heapOffset[objID]
objBaseReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(objBaseReg, offset)
#Load offset of field
fieldIndex = baseClass.lookup_field_index(field)
fieldIndexReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(fieldIndexReg, fieldIndex)
#Load the field's value from heap
fieldValReg = am.getNewReg()
code += "hload {0}, {1}, {2}".format(fieldValReg, objBaseReg, fieldIndexReg)
return (fieldValReg, code)
#FIELD ACCESS USING SUPER KEYWORD IS NOT SUPPORTED
elif isinstance(baseExpr, SuperExpr):
code += "#LINE {0} - Load value for 'super' field access".format(expr.lines)
objID = fieldAccessExpr.objID
baseClass = baseExpr.current_class.superclass
#Load base of object
objOffset = heapOffset[objID]
objBaseReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(objBaseReg, offset)
#Load offset of field
fieldIndex = baseClass.lookup_field_index(field)
fieldIndexReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(fieldIndexReg, fieldIndex)
#Load the field's value from heap
fieldValReg = am.getNewReg()
code += "hload {0}, {1}, {2}".format(fieldValReg, objBaseReg, fieldIndexReg)
return (fieldValReg, code)
elif isinstance(baseExpr, ClassReferenceExpr):
#Retrieve the value of the static field
code += "#LINE {0} - Retrieve value of field {1}\n".format(expr.lines, field.name)
#Get the static field offset
offset = am.staticFieldOffset[field.id]
offsetReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(offsetReg, offset)
#Load the field value from the static area
valueReg = am.getNewReg()
code += "hload {0}, sap, {1}\n".format(valueReg, offsetReg)
return (valueReg, code)
#Otherwise, baseExpr is an object instance, and is a VarExpr
else:
code += "#LINE {0} - Load address for var field access".format(expr.lines)
variable = baseExpr.var
objID = variable.id
baseClass = variable.type.baseclass
#Load base of object
objOffset = heapOffset[objID]
objBaseReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(objBaseReg, offset)
#Load offset of field
fieldIndex = baseClass.lookup_field_index(field)
fieldIndexReg = am.getNewReg()
code += "move_immed_i {0}, {1}\n".format(fieldIndexReg, fieldIndex)
#Load the field's value from heap
fieldValReg = am.getNewReg()
code += "hload {0}, {1}, {2}".format(fieldValReg, objBaseReg, fieldIndexReg)
return (fieldValReg, code)
def generateNewObjectExprCode(newObjectExpr):
global heapTop
global am
code = ""
line = newObjectExpr.lines
dClass = newObjectExpr.classref
args = newObjectExpr.args
heapOffset[newObjectExpr.objID] = heapTop
#Count the number of class fields and super class fields
numFields = 0
current = dClass
while current != None:
numFields += len(current.fields)
current = current.superclass
code += "#LINE {0} - Create new '{1}' object\n".format(line, dClass.name)
baseReg = am.getNewReg()
code += "move_immed_i {0}, {1} #Base of object in {0}\n".format(baseReg, heapTop)
numCellsReg = am.getNewReg()
code += "move_immed_i {0}, {1} #Num of cells in {0}\n".format(numCellsReg, numFields)
code += "halloc {0}, {1}\n".format(baseReg, numCellsReg)
#TODO: number of cells should be numFields - numOfStaticFields in class
heapTop += numFields
#Return the register with the heap offset for this register
return (baseReg, code)
#Use to make labels for result of conditional statements
def getTrueCaseLabel():
global am
label = "trueCase" + str(am.getUniqueLabelNum())
return label
def getFalseCaseLabel():
global am
label = "falseCase" + str(am.getUniqueLabelNum())
return label
def getAfterCaseLabel():
global am
label = "afterCase" + str(am.getUniqueLabelNum())
return label
def getBlockLabel():
global am
label = "block" + str(am.getUniqueLabelNum())
return label
def getMethodLabel(method):
label = "M_{0}_{1}".format(method.name, method.id)
return label