forked from srijanshetty/javascript-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunTime.py
executable file
·319 lines (248 loc) · 11.7 KB
/
runTime.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
#! /usr/bin/python
from sys import argv
from helpers import runtimeCode as RuntimeCode
from parser import parseProgram
#########################################################################################
def generateCode(inputFile, debug=False, outputFile='pro'):
# Read the inputFile and parse the program
program = open(inputFile).read()
ST, TAC, debug = parseProgram(program)
# Log the data for inspection
if debug:
TAC.printCode('TAC')
debug.log(ST.symbolTable, 'symbols')
debug.log(ST.functionList, 'functionList')
debug.log(ST.addressDescriptor, 'addressDescriptor')
# We create a new object which will store the code
RTC = RuntimeCode.RuntimeCode(ST, TAC)
counter = 0;
# Fix the labesl of the code, replace numbers with labels
RTC.fixLabels()
# We now traverse over the entire code to generate data
for function in TAC.code:
RTC.addFunction(function)
# Different stuff for main
if (function == 'main'):
#allocate space for the registers by updating stack pointer
RTC.addLine(['sub', '$sp', '$sp', '200'])
#set fame pointer of the callee
RTC.addLine(['la', '$fp', '200($sp)', ''])
RTC.addLine(['la', '$s5', '__display__', ''])
RTC.addLine(['lw', '$s7', '0($s5)', ''])
#set display[level]
RTC.addLine(['la', '$v0', '-' + str(ST.getAttributeFromFunctionList(function, 'width')) + '($sp)', ''])
RTC.addLine(['sw','$v0', '0($s5)', ''])
RTC.addLine(['li', '$v0', ST.getAttributeFromFunctionList(function, 'width'), ''])
RTC.addLine(['sub', '$sp', '$sp', '$v0'])
else:
#allocate space for the registers by updating stack pointer
RTC.addLine(['sub', '$sp','$sp','72'])
#store return address of the caller
RTC.addLine(['sw','$ra','0($sp)',''])
#sstore the frame pointer of the caller
RTC.addLine(['sw','$fp','4($sp)',''])
#set fame pointer of the callee
RTC.addLine(['la','$fp','72($sp)',''])
#storing display[level]
RTC.addLine(['li','$v0',ST.getAttributeFromFunctionList(function, 'level'),''])
RTC.addLine(['la', '$s5', '__display__', ''])
RTC.addLine(['add', '$v0', '$v0', '$v0'])
RTC.addLine(['add', '$v0', '$v0', '$v0'])
RTC.addLine(['add', '$s6', '$v0', '$s5'])
RTC.addLine(['lw','$s7','0($s6)',''])
RTC.addLine(['sw','$s7','8($sp)',''])
#set display[level]
RTC.addLine(['la', '$v0', '-' + str(ST.getAttributeFromFunctionList(function, 'width'))+'($sp)' , ''])
RTC.addLine(['sw','$v0','0($s6)',''])
#store remaining registers
RTC.addLine(['sw','$t0','12($sp)',''])
RTC.addLine(['sw','$t1','16($sp)',''])
RTC.addLine(['sw','$t2','20($sp)',''])
RTC.addLine(['sw','$t3','24($sp)',''])
RTC.addLine(['sw','$t4','28($sp)',''])
RTC.addLine(['sw','$t5','32($sp)',''])
RTC.addLine(['sw','$t6','36($sp)',''])
RTC.addLine(['sw','$t7','40($sp)',''])
RTC.addLine(['sw','$t8','44($sp)',''])
RTC.addLine(['sw','$t9','48($sp)',''])
RTC.addLine(['sw','$s0','52($sp)',''])
RTC.addLine(['sw','$s1','56($sp)',''])
RTC.addLine(['sw','$s2','60($sp)',''])
RTC.addLine(['sw','$s3','64($sp)',''])
RTC.addLine(['sw','$s4','68($sp)',''])
# Create space for local data
RTC.addLine(['li','$v0',ST.getAttributeFromFunctionList(function, 'width'),''])
RTC.addLine(['sub','$sp','$sp','$v0'])
# Copy the parameters
for x in range(ST.getAttributeFromFunctionList(function, 'numParam')):
RTC.addLine(['sw','$a' + str(x), str(4*x) + '($sp)', ''])
for line in TAC.code[function]:
if line[3] == 'JUMPLABEL':
counter = 0 ;
reg = RTC.nextReg(line[2])
RTC.addLine(['jal', reg, '', ''])
RTC.reloadParents(ST.getAttributeFromFunctionList(function, 'level'), function)
elif line[3] == 'JUMPBACK':
RTC.addLine(['b', function + 'end', '', ''])
elif line[3] == 'PARAM':
reg = RTC.nextReg(line[0])
RTC.addLine(['move', '$a'+str(counter), reg,''])
counter = counter +1 ;
elif line[3] == '=':
reg1 = RTC.nextReg(line[0])
reg2 = RTC.nextReg(line[1])
RTC.addLine(['move', reg1, reg2, ''])
RTC.flushTemporary(line[0])
elif line[3] == '=i':
reg = RTC.nextReg(line[0])
RTC.addLine(['li', reg, line[1], ''])
RTC.flushTemporary(line[0])
elif line[3] == '=REF':
reg = RTC.nextReg(line[0])
RTC.addLine(['la', reg, line[1], ''])
RTC.flushTemporary(line[0])
elif line[3] == 'uni-':
reg1 = RTC.nextReg(line[0])
reg2 = RTC.nextReg(line[1])
RTC.addLine(['neg', reg1, reg2, ''])
RTC.flushTemporary(line[0])
elif line[3] == '+':
reg1 = RTC.nextReg(line[0])
reg2 = RTC.nextReg(line[1])
reg3 = RTC.nextReg(line[2])
RTC.addLine(['add', reg1, reg2, reg3])
RTC.flushTemporary(line[0])
elif line[3] == '-':
reg1 = RTC.nextReg(line[0])
reg2 = RTC.nextReg(line[1])
reg3 = RTC.nextReg(line[2])
RTC.addLine(['sub', reg1, reg2, reg3])
RTC.flushTemporary(line[0])
elif line[3] == '*':
reg1 = RTC.nextReg(line[1])
reg2 = RTC.nextReg(line[2])
reg3 = RTC.nextReg(line[0])
RTC.addLine(['mult', reg1, reg2,''])
RTC.addLine(['mflo', reg3,'',''])
RTC.flushTemporary(line[0])
elif line[3] == '/':
reg1 = RTC.nextReg(line[1])
reg2 = RTC.nextReg(line[2])
reg3 = RTC.nextReg(line[0])
RTC.addLine(['div', reg1, reg2, ''])
RTC.addLine(['mflo', reg3, '', ''])
RTC.flushTemporary(line[0])
elif line[3] == '%':
reg1 = RTC.nextReg(line[1])
reg2 = RTC.nextReg(line[2])
reg3 = RTC.nextReg(line[0])
RTC.addLine(['div', reg1, reg2, ''])
RTC.addLine(['mfhi', reg3, '', ''])
RTC.flushTemporary(line[0])
elif line[3] == '<':
reg1 = RTC.nextReg(line[0])
reg2 = RTC.nextReg(line[1])
reg3 = RTC.nextReg(line[2])
RTC.addLine(['slt', reg1, reg2, reg3])
RTC.flushTemporary(line[0])
elif line[3] == '>':
reg1 = RTC.nextReg(line[0])
reg2 = RTC.nextReg(line[1])
reg3 = RTC.nextReg(line[2])
RTC.addLine(['sgt', reg1, reg2, reg3])
RTC.flushTemporary(line[0])
elif line[3] == '<=':
reg1 = RTC.nextReg(line[0])
reg2 = RTC.nextReg(line[1])
reg3 = RTC.nextReg(line[2])
RTC.addLine(['sle', reg1, reg2, reg3])
RTC.flushTemporary(line[0])
elif line[3] == '>=':
reg1 = RTC.nextReg(line[0])
reg2 = RTC.nextReg(line[1])
reg3 = RTC.nextReg(line[2])
RTC.addLine(['sge', reg1, reg2, reg3])
RTC.flushTemporary(line[0])
elif line[3] == '==':
reg1 = RTC.nextReg(line[0])
reg2 = RTC.nextReg(line[1])
reg3 = RTC.nextReg(line[2])
RTC.addLine(['seq', reg1, reg2, reg3])
RTC.flushTemporary(line[0])
elif line[3] == '!=':
reg1 = RTC.nextReg(line[0])
reg2 = RTC.nextReg(line[1])
reg3 = RTC.nextReg(line[2])
RTC.addLine(['sne', reg1, reg2, reg3])
RTC.flushTemporary(line[0])
elif line[3] == 'COND_GOTO_Z':
reg1 = RTC.nextReg(line[0])
RTC.addLine(['beq', reg1, '$0', line[2]])
elif line[3] == 'GOTO':
RTC.addLine(['b', line[2], '', ''])
elif line[3] == 'FUNCTION_RETURN':
reg1 = RTC.nextReg(line[0])
RTC.addLine(['move', reg1, '$v0', ''])
elif line[3] == 'RETURN':
reg1 = RTC.nextReg(line[0])
RTC.addLine(['move', '$v0', reg1, ''])
RTC.addLine(['b', function + 'end', '', ''])
elif line[3] == 'HALT':
RTC.addLine(['jal', 'exit', '', ''])
elif line[3] == 'PRINT' and line[0] == '':
RTC.addLine(['jal', 'print_newline', '', ''])
elif line[3] == 'PRINT' and line[2] == 'UNDEFINED':
RTC.addLine(['jal', 'print_undefined', '', ''])
elif line[3] == 'PRINT':
reg = RTC.nextReg(line[0])
RTC.addLine(['move', '$a0', reg, ''])
if line[2] == 'NUMBER':
RTC.addLine(['jal', 'print_integer', '', ''])
elif line[2] == 'STRING':
RTC.addLine(['jal', 'print_string', '', ''])
else:
RTC.addLine(['jal', 'print_boolean', '', ''])
else:
RTC.addLine(line)
# Data unloading happens for all function save main
if function != 'main':
# Add a label to point to the end of the function
RTC.addLine(['LABEL', function + 'end', '', ''])
# Remove the local data
RTC.addLine(['addi','$sp','$sp',ST.getAttributeFromFunctionList(function,'width')])
# Get enviornment pointers
RTC.addLine(['lw','$ra','0($sp)',''])
RTC.addLine(['lw','$fp','4($sp)',''])
RTC.addLine(['lw','$a0','8($sp)',''])
# diplay level
RTC.addLine(['li','$a1',ST.getAttributeFromFunctionList(function, 'level'),''])
RTC.addLine(['la', '$s5', '__display__', ''])
RTC.addLine(['add', '$a1', '$a1', '$a1'])
RTC.addLine(['add', '$a1', '$a1', '$a1'])
RTC.addLine(['add', '$s6', '$a1', '$s5'])
RTC.addLine(['sw','$a0','0($s6)',''])
# Registers
RTC.addLine(['lw','$t0','12($sp)',''])
RTC.addLine(['lw','$t1','16($sp)',''])
RTC.addLine(['lw','$t2','20($sp)',''])
RTC.addLine(['lw','$t3','24($sp)',''])
RTC.addLine(['lw','$t4','28($sp)',''])
RTC.addLine(['lw','$t5','32($sp)',''])
RTC.addLine(['lw','$t6','36($sp)',''])
RTC.addLine(['lw','$t7','40($sp)',''])
RTC.addLine(['lw','$t8','44($sp)',''])
RTC.addLine(['lw','$t9','48($sp)',''])
RTC.addLine(['lw','$s0','52($sp)',''])
RTC.addLine(['lw','$s1','56($sp)',''])
RTC.addLine(['lw','$s2','60($sp)',''])
RTC.addLine(['lw','$s3','64($sp)',''])
RTC.addLine(['lw','$s4','68($sp)',''])
RTC.addLine(['addi','$sp','$sp','72'])
# Jump to the calling procedure
RTC.addLine(['jr','$ra','',''])
# Print the generated code
RTC.printCode(outputFile)
if __name__ == '__main__':
# Parse the program to get the threeAddressCode
filename, inputFile = argv
generateCode(inputFile, outputFile='pro')