-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasmgeneration.py
476 lines (384 loc) · 17 KB
/
asmgeneration.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
"""To GENERATE ASM"""
import textwrap
import sys
from scanner import Scanner
from astparsing import Parser
from ast_visual import NodeVisitor, ASTVisualizer
from subprocess import check_output
__author__ = 'rohitmehra'
class AssemblyGenerator(NodeVisitor):
def __init__(self, ast_parser):
self.parser = ast_parser
self.parser.parse()
self.variable_map = self.parser.variable_map
self.fp_map = {}
self.ncount = 1
self.dot_header = [
'\t.data\nnewline:\t.asciiz "\\n"\n\t.text\n\t.globl main\nmain:\n\tli $fp, 0x7ffffffc\n']
self.dot_body = []
# Starts from 4, updated to 0 before use
self.fp = 4
# fp stack
self.fp_Stack = []
# block_stack
self.block_stack = []
self.t = 0
self.v = 0
self.a = 0
self.block_count = 1
def get_fp(self):
return self.fp_Stack.pop()
def get_block(self):
return self.block_stack.pop()
def del_second_last_block(self):
del self.block_stack[-2]
def update_t(self):
if self.t <= 9:
self.t += 1
else:
self.t = 0
def load_Variable(self, node, t):
name = node.name
fp = self.variable_map[name].fp
s = '\tlw $t{}, {}($fp)\n'.format(t, fp)
self.dot_body.append(s)
# self.fp_Stack.append(fp)
# LOADS Number in current fp
def visit_Num(self, node):
# self.t = 0
# UPDATE fp and then use.
self.fp -= 4
# s = '\tli $t{}, {}\n\tsw $t{}, {}($fp)\n\tlw $t{}, {}($fp)\n\tadd $t{}, $t{}, $zero\n'.format(self.t,
# node.value,
# self.t, self.fp,
# self.t + 1,
# self.fp,
# self.t,
# self.t + 1)
s = '\tli $t{}, {}\n\tsw $t{}, {}($fp)\n'.format(self.t,
node.value,
self.t,
self.fp,
)
self.fp_Stack.append(self.fp)
self.dot_body.append(s)
# LOADS Bool in current fp
def visit_Bool(self, node):
# self.t = 0
# UPDATE fp and then use.
self.fp -= 4
if node.value == 'true':
value = 1
else:
value = 0
s = '\tli $t{}, {}\n\tsw $t{}, {}($fp)\n'.format(self.t,
value,
self.t,
self.fp,
)
self.fp_Stack.append(self.fp)
self.dot_body.append(s)
# DECLARATIONS CALLS & VARIABLE MAP UPDATES
def visit_Variable(self, node):
# fp starts from 4 # fp indicates each unique variable
# UPDATE fp and then use.
self.fp -= 4
s = '\tli $t{}, 0\n\tsw $t{}, {}($fp)\n'.format(
self.t, self.t, self.fp)
self.dot_body.append(s)
self.variable_map[node.name].fp = self.fp
def visit_ReadInt(self, node):
# INPUT and MOVE to current t
s = '\tli $v{}, 5\n\tsyscall\n\tadd $t{}, $v{}, $zero\n'.format(
self.v, self.t, self.v)
self.dot_body.append(s)
# Returns back to calling block and there will be save word call
# Type 1.2
def visit_WriteInt(self, node):
self.t = 0
s = '\tli $v{}, 1\n'.format(self.v)
self.dot_body.append(s)
self.update_t()
current_t = self.t
if type(node.child).__name__ == 'Variable':
self.load_Variable(node.child, current_t)
else:
self.visit(node.child)
s = '\tlw $t{}, {}($fp)\n'.format(current_t, self.get_fp())
self.dot_body.append(s)
s = '\tadd $a{}, $t{}, $zero\n\tsyscall\n\tli $v{}, 4\n\tla $a{}, newline\n\tsyscall\n'.format(self.a, self.t,
self.v, self.a)
self.dot_body.append(s)
# ASSIGNMENTS
def visit_AssignOp(self, node):
# Temp register to be used
self.t = 0
# Get the variable name
variable_name = node.left.name
# Get the fp of that variable
variable_fp = self.variable_map[variable_name].fp
# Call Right Nodes First
# ReadInt type assignments
if type(node.right).__name__ == 'ReadInt':
# Will load word from user to self.t
self.visit(node.right)
# Just Single Variable onto right
elif type(node.right).__name__ == 'Variable':
# Load right Variable to self.t
self.load_Variable(node.right, self.t)
# Either Bool or Num or BinaryOp or CompareOp
else:
self.visit(node.right)
# LOAD RHS FROM ITS fp to t+1, move to t and then to LHS variable FP
s = '\tlw $t{}, {}($fp)\n\tadd $t{}, $t{}, $zero\n'.format(self.t + 1,
self.get_fp(),
self.t,
self.t + 1)
self.dot_body.append(s)
# Store word to the LHS variable fp
s = '\tsw $t{}, {}($fp)\n'.format(self.t, variable_fp)
self.dot_body.append(s)
# Make Call for all ops then save the result in current fp
def visit_BinOp(self, node):
self.t = 0
self.fp -= 4 # fp of this op
# pushed into stack, Popped at receiving end
self.fp_Stack.append(self.fp)
current_t = self.t # t for this op
self.update_t()
left = self.t # t holding left operand
if type(node.left).__name__ == 'Variable':
# load variable from fp to left t
self.load_Variable(node.left, left)
else:
# Either Bool or Num or BinaryOp or CompareOp
self.visit(node.left)
s = '\tlw $t{}, {}($fp)\n\tadd $t{}, $t{}, $zero\n'.format(left + 1,
self.get_fp(),
left,
left + 1)
self.dot_body.append(s)
self.update_t()
right = self.t # t holding right operand
if type(node.right).__name__ == 'Variable':
# load variable from fp to right t
self.load_Variable(node.right, right)
else:
# Either Bool or Num or BinaryOp or CompareOp
self.visit(node.right)
s = '\tlw $t{}, {}($fp)\n\tadd $t{}, $t{}, $zero\n'.format(right + 1,
self.get_fp(),
right,
right + 1)
self.dot_body.append(s)
if node.op.value == '+':
s = '\tadd $t{}, $t{}, $t{}\n'.format(current_t, left, right)
self.dot_body.append(s)
elif node.op.value == '-':
s = '\tsub $t{}, $t{}, $t{}\n'.format(current_t, left, right)
self.dot_body.append(s)
elif node.op.value == '*':
s = '\tmul $t{}, $t{}, $t{}\n'.format(current_t, left, right)
self.dot_body.append(s)
elif node.op.value == 'div':
s = '\tdiv $t{}, $t{}, $t{}\n'.format(current_t, left, right)
self.dot_body.append(s)
elif node.op.value == 'mod':
s = '\trem $t{}, $t{}, $t{}\n'.format(current_t, left, right)
self.dot_body.append(s)
# Store word at fp(read from stack) of this op, will be popped at the receiver end
s = '\tsw $t{}, {}($fp)\n'.format(current_t, self.fp_Stack[-1])
self.dot_body.append(s)
def visit_CompareOp(self, node):
self.t = 0
self.fp -= 4 # fp of this op
# pushed into stack, Popped at receiving end
self.fp_Stack.append(self.fp)
current_t = self.t # t for this op
self.update_t()
left = self.t # t holding left operand
if type(node.left).__name__ == 'Variable':
# load variable from fp to left t
self.load_Variable(node.left, left)
else:
# Either Bool or Num or BinaryOp or CompareOp
self.visit(node.left)
s = '\tlw $t{}, {}($fp)\n\tadd $t{}, $t{}, $zero\n'.format(left + 1,
self.get_fp(),
left,
left + 1)
self.dot_body.append(s)
self.update_t()
right = self.t # t holding right operand
if type(node.right).__name__ == 'Variable':
# load variable from fp to right t
self.load_Variable(node.right, right)
else:
# Either Bool or Num or BinaryOp or CompareOp
self.visit(node.right)
s = '\tlw $t{}, {}($fp)\n\tadd $t{}, $t{}, $zero\n'.format(right + 1,
self.get_fp(),
right,
right + 1)
self.dot_body.append(s)
if node.op.value == '<':
s = '\tslt $t{}, $t{}, $t{}\n'.format(current_t, left, right)
self.dot_body.append(s)
elif node.op.value == '<=':
s = '\tsle $t{}, $t{}, $t{}\n'.format(current_t, left, right)
self.dot_body.append(s)
elif node.op.value == '>':
s = '\tsgt $t{}, $t{}, $t{}\n'.format(current_t, left, right)
self.dot_body.append(s)
elif node.op.value == '>=':
s = '\tsge $t{}, $t{}, $t{}\n'.format(current_t, left, right)
self.dot_body.append(s)
elif node.op.value == '!=':
s = '\tsne $t{}, $t{}, $t{}\n'.format(current_t, left, right)
self.dot_body.append(s)
elif node.op.value == '=':
s = '\tseq $t{}, $t{}, $t{}\n'.format(current_t, left, right)
self.dot_body.append(s)
# Store word at fp(read from stack) of this op, will be popped at the receiver end
s = '\tsw $t{}, {}($fp)\n'.format(current_t, self.fp_Stack[-1])
self.dot_body.append(s)
# Type 3
def visit_WhileOp(self, node):
self.block_count += 1
# adding statement block and exit block to stack
current_block = self.block_count
self.block_stack.extend([self.block_count + 1, self.block_count + 2])
self.block_count += 2
self.t = 0
# Jump from previous block to this block
s = '\tj B{}\n'.format(current_block)
self.dot_body.append(s)
# 1) CONDITIONAL BLOCK
s = 'B{}:\n'.format(current_block)
self.dot_body.append(s)
# Comparision node, execute comparision-->store result in fp--> push to fp stack
self.visit(node.comparision)
# Pop fp_stack and load from fp to current t
s = '\tlw $t{}, {}($fp)\n'.format(self.t, self.get_fp())
self.dot_body.append(s)
# If not 0/false jump to while block, create a label used to exit while loop/jump to next block.
exit_block = self.get_block()
while_block = self.get_block()
s = '\tbne $t{}, $zero, B{}\n'.format(self.t, while_block)
self.dot_body.append(s)
s = 'L{}:\n'.format(current_block)
self.dot_body.append(s)
s = '\tj B{}\n'.format(exit_block)
self.dot_body.append(s)
s = 'B{}:\n'.format(while_block)
self.dot_body.append(s)
# 2) WHILE STATEMENTS BLOCK
self.visit(node.statements)
# 3) LOOP BACK TO CONDITIONAL BLOCK
s = '\tj B{}\n'.format(current_block)
self.dot_body.append(s)
s = 'B{}:\n'.format(exit_block)
self.dot_body.append(s)
# Type 4
def visit_IfOp(self, node):
self.block_count += 1
self.block_stack.append(self.block_count)
# adding statement block and exit block to stack
current_block = self.get_block() # condition block
self.t = 0
# Jump from previous block to this block
s = '\tj B{}\n'.format(current_block)
self.dot_body.append(s)
# 1) CONDITIONAL BLOCK
s = 'B{}:\n'.format(current_block)
self.dot_body.append(s)
# Comparision node, execute comparision-->store result in fp--> push to fp stack
# node.comparison could be a compareOp or a bool variable
if type(node.comparision).__name__ == 'Variable':
self.load_Variable(node.comparision, self.t)
else:
self.visit(node.comparision)
# Pop fp_stack and load from fp to current t
s = '\tlw $t{}, {}($fp)\n'.format(self.t, self.get_fp())
self.dot_body.append(s)
# If not 0/false jump to if block
self.block_count += 1 # If block count
if_block = self.block_count
self.block_stack.append(self.block_count)
s = '\tbne $t{}, $zero, B{}\n'.format(self.t, self.block_count)
self.dot_body.append(s)
######################################
# Create label for else statements
self.block_count += 1 # Else block count
# ADDED, WILL BE POPPED OR REMAIN IN STACK
self.block_stack.append(self.block_count)
s = 'L{}:\n'.format(self.block_count)
self.dot_body.append(s)
s = '\tj B{}\n'.format(self.block_count)
self.dot_body.append(s)
# 2) IF STATEMENTS BLOCK
s = 'B{}:\n'.format(if_block)
self.dot_body.append(s)
self.visit(node.if_statements) # --------------->
# 3) if else statement exist, then call else statements in the block in label
if node.else_statements:
# POP ELSE BLOCK| WONT BE POPPED WILL REMAIN IN THE STACK TILL END
exit_block = self.get_block()
_ = self.get_block() # POP IF BLOCK
s = '\tj B{}\n'.format(exit_block + 1)
self.dot_body.append(s)
s = 'B{}:\n'.format(exit_block)
self.dot_body.append(s)
# Append else clause to this block
self.visit(node.else_statements) # --------------->
self.block_count += 1
s = 'B{}:\n'.format(self.block_count)
self.dot_body.append(s)
if not node.else_statements:
self.del_second_last_block()
s = '\nB{}:\n'.format(self.get_block())
self.dot_body.append(s)
# Assignments followed by main body
def visit_Statements(self, node):
# Add Statements to the same block it was invoked from
for child in node.children:
self.t = 0
self.visit(child)
# DONE
def visit_Declarations(self, node):
# Add Declarations to the same block it was invoked from
for child in node.children:
self.visit(child)
# DONE
def visit_Program(self, node):
# First Block
s = 'B{}:\n'.format(self.block_count)
self.dot_body.append(s)
# Add statements to the first block
for child in node.children:
self.visit(child)
# EXIT CALL
self.block_count += 1
s = '\tj B{}\n'.format(self.block_count)
self.dot_body.append(s)
s = 'B{}:\n'.format(self.block_count)
self.dot_body.append(s)
s = '\tli $v{}, 10\n\tsyscall'.format(self.v)
self.dot_body.append(s)
# print(self.block_stack, len(self.block_stack))
def gens(self):
self.visit(self.parser.tree)
content = ''.join(self.dot_header + self.dot_body)
s_file = self.parser.scanner.opfilename[:-3] + 's'
with open(s_file, 'w') as file:
file.write(content)
print('Compiled .s file Generated Successfully.')
return content
if __name__ == '__main__':
scanner = Scanner()
parser = Parser(scanner) # ast parser rm
visualize = ASTVisualizer(parser) # create ASTVisualizer object
visualize.gendot() # get the content of dot file ro
parser = Parser(scanner) # ast parser rm
asm = AssemblyGenerator(parser) # ASM GEN OBJECT
content = asm.gens() # get the content of .s file ro