-
Notifications
You must be signed in to change notification settings - Fork 0
/
FinalCodeGenerator.py
302 lines (292 loc) · 16.6 KB
/
FinalCodeGenerator.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
from GlobalVariable import GlobalVariable
from Parameter import Parameter
from SymbolicTable import SymbolicTable
from TemporaryVariable import TemporaryVariable
from Variable import Variable
class FinalCodeGenerator():
intCodeFileName = ""
finalCodeFileName = ""
symbolicTable: SymbolicTable
functionsAndTheirLevels = {"main": 0}
def __init__(self, fileName: str, symbolicTable: SymbolicTable):
self.intCodeFileName = fileName.split('.')[0] + ".int" #get the int code created by icg
self.finalCodeFileName = fileName.split('.')[0] + ".asm"
open(self.finalCodeFileName, 'w') #clear the file
self.symbolicTable = symbolicTable
self.produce(".data", True)
self.produce("strNewline: .ascii \"\\n\"")
self.produce(".text", True)
self.produce("L0:", True)
self.produce("j main")
def gnlvcode(self, variable):
result = self.symbolicTable.search(variable)
if (result == None):
print("Terminating Compiler")
exit(0)
(variableEntity, offset) = result
self.produce("lw t0, -4(sp)")
for _ in range(offset-1):
self.produce("lw t0, -4(t0)")
self.produce(f"addi t0, t0, -{variableEntity.offset}")
def loadVariable(self, variable, register):
if (variable[0] in "0123456789"):
num = int(variable)
self.produce(f"li {register}, {num}")
return
result = self.symbolicTable.search(variable)
if (result == None):
print("Terminating Compiler (loadVariable)")
exit(0)
(variableEntity, offset) = result
if (offset == 0):
if isinstance(variableEntity,GlobalVariable): #globalVar
actualGlobalVar = self.symbolicTable.searchGlobalEntity(variable)
if (actualGlobalVar == None):
print("Terminating Compiler (loadVariable)")
exit(0)
self.produce(f"lw {register}, -{actualGlobalVar.offset}(gp)")
if isinstance(variableEntity,Parameter) and variableEntity.mode == "VAL": #local var/parameter
self.produce(f"#{variableEntity} (with Scope offset {offset})")
self.produce(f"lw t0, -{variableEntity.offset}(sp)")
elif isinstance(variableEntity,Variable) or isinstance(variableEntity,TemporaryVariable) or (isinstance(variableEntity,Parameter) and variableEntity.mode == "REF"):
self.produce(f"#{variableEntity} (with Scope offset {offset})")
self.produce(f"lw {register}, -{variableEntity.offset}(sp)")
elif (offset < self.symbolicTable.getCurrentScope().level): #variable/parameter is in ancestor of current scope
if (isinstance(variableEntity,Parameter) and variableEntity.mode == "REF"):
self.gnlvcode(variable)
self.produce(f"lw t0, 0(t0)")
self.produce(f"lw {register}, 0(t0)")
elif (isinstance(variableEntity,Variable) or (isinstance(variableEntity,Parameter) and variableEntity.mode == "VAL")):
self.gnlvcode(variable)
self.produce(f"lw {register}, 0(t0)")
else:
return True #to check in generateFinalCode to also display the line, not perfect I know
def storeVariable(self, variable, register):
if (variable[0] in "0123456789"):
num = int(variable)
self.loadVariable(num, "t0")
self.storeVariable("t0", register)
return
result = self.symbolicTable.search(variable)
if (result == None):
print("Terminating Compiler (storeVariable)")
exit(0)
(variableEntity, offset) = result
if (offset == 0):
if isinstance(variableEntity,GlobalVariable): #globalVar
actualGlobalVar = self.symbolicTable.searchGlobalEntity(variable)
if (actualGlobalVar == None):
print("Terminating Compiler (loadVariable)")
exit(0)
self.produce(f"sw {register}, -{actualGlobalVar.offset}(gp)")
elif isinstance(variableEntity,Parameter) and variableEntity.mode == "VAL":
self.produce(f"lw t0, -{variableEntity.offset}(sp)")
self.produce(f"sw {register}, 0(t0)")
elif isinstance(variableEntity,Variable) or isinstance(variableEntity,TemporaryVariable) or (isinstance(variableEntity,Parameter) and variableEntity.mode == "VAL"):
self.produce(f"sw {register}, -{variableEntity.offset}(sp)")
elif (offset < self.symbolicTable.getCurrentScope().level):
if (isinstance(variableEntity,Parameter) and variableEntity.mode == "REF"):
self.gnlvcode(variable)
self.produce(f"lw t0, 0(t0)")
self.produce(f"sw {register}, 0(t0)")
elif (isinstance(variableEntity,Variable) or (isinstance(variableEntity,Parameter) and variableEntity.mode == "REF")):
self.gnlvcode(variable)
self.produce("lw t0, 0(t0)")
self.produce(f"sw {register}, 0(t0)")
else:
return True #to check in generateFinalCode to also display the line, not perfect I know
def produce(self, codeLine: str, isLabel : bool = False):
'''Write a line of code to the final code file'''
with open(self.finalCodeFileName, 'a') as file:
if (isLabel): file.write(f"{codeLine}\n")
else: file.write(f"\t{codeLine}\n")
def generateFinalCode(self, startLabel, endLabel):
currentParameterIndex = 0
isFirstPar = True
currentFunction = ""
with open(self.intCodeFileName, 'r') as file:
for line in file:
parts = line.split()
label = parts[0]
if (int(label.removesuffix(":")) < startLabel or int(label.removesuffix(":")) > endLabel): continue
op = parts[1]
x = parts[2]
y = parts[3]
z = parts[4]
if (int(label.removesuffix(":")) == startLabel):
currentFunction = x
self.functionsAndTheirLevels[currentFunction] = self.symbolicTable.getCurrentScope().level
self.produce(f"L{label}", True)
if (op in "+-*//%"):
self.produce("#Load x in t1")
if (self.loadVariable(x, "t1")):
print(f"Variable {x} in function {self.symbolicTable.table.scopes[-2].entities[-1].name} is a global variable but the global keyword wasnt used")
print(f"Line: {line}")
exit(0)
self.produce("#Load y in t2")
if (self.loadVariable(y, "t2")):
print(f"Variable {x} in function {self.symbolicTable.table.scopes[-2].entities[-1].name} is a global variable but the global keyword wasnt used")
print(f"Line: {line}")
exit(0)
opToCommand = {
"+": "add",
"-": "sub",
"*": "mul",
"//": "div",
"%": "rem"
}
self.produce("#Apply Operator to operants")
self.produce(f"{opToCommand[op]} t1, t2, t1")
self.produce("#Put Results in z")
self.storeVariable(z, "t1")
elif (op == ":="):
self.produce("#Load x in t1")
if(self.loadVariable(x, "t1")):
print(f"Variable {x} in function {self.symbolicTable.table.scopes[-2].entities[-1].name} is a global variable but the global keyword wasnt used")
print(f"Line: {line}")
exit(0)
self.produce("#Store t1 in z")
if(self.storeVariable(z, "t1")):
print(f"Variable {z} in function {self.symbolicTable.table.scopes[-2].entities[-1].name} is a global variable but the global keyword wasnt used")
print(f"Line: {line}")
exit(0)
elif (op == "jump"):
self.produce("#Jump to z")
self.produce(f"j L{z}")
elif (op in ["==", "!=", "<", ">", "<=", ">="]):
self.produce("#Load x in t1")
if (self.loadVariable(x, "t1")):
print(f"Variable {x} in function {self.symbolicTable.table.scopes[-2].entities[-1].name} is a global variable but the global keyword wasnt used")
print(f"Line: {line}")
exit(0)
self.produce("#Load y in t2")
if (self.loadVariable(y, "t2")):
print(f"Variable {x} in function {self.symbolicTable.table.scopes[-2].entities[-1].name} is a global variable but the global keyword wasnt used")
print(f"Line: {line}")
exit(0)
IntToFinalCorrelationTable = {
'==': "beq",
"!=": "bne",
"<": "blt",
">": "bgt",
"<=": "ble",
">=": "bge"
}
actualOp = IntToFinalCorrelationTable[op]
self.produce("#Compare t1 and t2 branch if needed")
self.produce(f"{actualOp} t1, t2, L{z}")
elif (op == "begin_block"):
self.produce(f"{x}:", True)
if (x == "main"):
functionFramelength = self.symbolicTable.search("main")[0].framelength
self.produce(f"#Add main function's framelength to sp")
self.produce(f"addi sp, sp, {functionFramelength}")
self.produce(f"#Move gp to sp")
self.produce("mv gp, sp")
else:
self.produce(f"#Store Current ra in 0(sp)")
self.produce("sw ra, 0(sp)")
elif (op == "halt"):
self.produce("#Exit")
self.produce("li a0, 0")
self.produce("li a7, 93")
self.produce("ecall")
elif (op == "par"):
if (isFirstPar):
self.produce(f"#First par, so create fp of new func")
self.produce(f"addi fp, sp, {self.symbolicTable.search(z)[0].framelength}")
isFirstPar = False
if (y == "VAL"):
parameterOffset = 12 + (currentParameterIndex * 4)
currentParameterIndex += 1
self.produce(f"#Parameter is int passed by VAL")
self.produce(f"li t0, {x}")
self.produce(f"sw t0, -{parameterOffset}(fp)")
if (y == "REF"):
parameterOffset = 12 + (currentParameterIndex * 4)
currentParameterIndex += 1
result = self.symbolicTable.search(x)
if (result == None):
print(f"Terminating Compiler (par1)")
exit(0)
(varEntity, scopeOffset) = result
if (scopeOffset == 1): #value is in caller func
self.produce(f"#Parameter is passed by REF and value is in caller func")
self.produce(f"addi t0, sp, -{varEntity.offset}") #t0 now has address of var
self.produce(f"sw t0, -{parameterOffset}(fp)") #put address in parameter
elif (scopeOffset < self.symbolicTable.getCurrentScope().level): #value is in ancestor of calling func
self.produce(f"#Parameter is passed by REF and value is in ancestor of caller func")
self.gnlvcode(x)
self.produce(f"sw t0, -{parameterOffset}(fp)")
elif (scopeOffset == self.symbolicTable.getCurrentScope().level): #value is global
self.produce(f"#Parameter is passed by REF and value global")
self.produce(f"addi t0, gp, -{varEntity.offset}")
self.produce(f"sw t0, -{parameterOffset}(fp)")
else:
print("Value is in invalid place, terminating")
elif (y == "RET"):
self.produce(f"#Parameter is return value")
returnVar = self.symbolicTable.search(x)
if (returnVar == None):
print("Terminating Compiler (par2)")
exit(0)
self.produce(f"addi t0, sp, -{returnVar[0].offset}")
self.produce("sw t0, -8(fp)")
elif (op == "call"):
#Reset Paramater vars
currentParameterIndex = 0
isFirstPar = True
#get next func entity
result = self.symbolicTable.search(x)
if (result == None):
print("Terminating Compiler (call)")
exit(0)
(FuncEntity, _) = result
#Store current act graph in next function Act Graph
functionBeingCalled = FuncEntity.name
if (functionBeingCalled == currentFunction or self.functionsAndTheirLevels[functionBeingCalled] == self.functionsAndTheirLevels[currentFunction]):
self.produce(f"#Sibling Func or Recursion")
self.produce(f"sw t0, -4(sp)")
self.produce(f"sw t0, -4(fp)")
else:
self.produce(f"#Parent-Child Func")
self.produce(f"sw sp, -4(fp)")
#get the new framelength
frameLength = FuncEntity.framelength
#Store current ra in current graph
self.produce(f"addi sp, sp, {frameLength}")
self.produce(f"jal {x}")
self.produce(f"addi sp, sp, -{frameLength}")
elif (op == "end_block"):
self.produce(f"#End of {x}")
self.produce("lw ra, 0(sp)")
self.produce("jr ra")
elif (op == "ret"):
self.produce(f"#Return {z}")
if (z[0] in "0123456789"):
self.produce(f"li t1, {z}")
else:
self.loadVariable(z, "t1")
# self.produce(f"lw t1, -{symbolicTable.search(z)[0].offset}(sp)") #load in t1 the value of the return variable
self.produce(f"lw t0, -8(sp)") #load in t0 the address of the return variable
self.produce(f"sw t1, 0(t0)")#put the return value in the address of the return variable
elif (op == "in"):
self.produce(f"#Input")
self.produce("li a7, 5")
self.produce("ecall")
self.produce(f"#Store input from a0 in {x}")
if(self.storeVariable(x, "a0")):
print(f"Variable {x} in function {self.symbolicTable.table.scopes[-2].entities[-1].name} is a global variable but the global keyword wasnt used")
print(f"Line: {line}")
exit(0)
elif (op == "out"):
self.produce(f"#Print {x}")
if(self.loadVariable(x, "a0")):
print(f"Variable {x} in function {self.symbolicTable.table.scopes[-2].entities[-1].name} is a global variable but the global keyword wasnt used")
print(f"Line: {line}")
exit(0)
self.produce("li a7, 1")
self.produce("ecall")
self.produce("la a0, strNewline")
self.produce("li a7, 4")
self.produce("ecall")