-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapper.py
325 lines (304 loc) · 10.3 KB
/
mapper.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
import sys
def checker(vars, a):
if a in vars :
return a
elif len(a.split('_')) in [3,4]:
if len(a.split('_')) == 4:
return "$a" + a.split('_')[-1]
return a;
else :
return "$"+a
def isfloatreg(a):
if len(a) == 2 and a.startswith('f') and a[1].isdigit():
return True
elif len(a.split('_'))==4:
return True
else:
return False
def iswordreg(a):
if len(a) == 2 and a.startswith('t') and a[1].isdigit():
return True
elif len(a.split('_'))==4:
return True
else:
return False
# --------- all the varialbe declaration here ----------
with open (sys.argv[1], "r") as myfile:
data=myfile.readlines()
print(".data")
vars = []
types = {}
for i in data:
x = i.split('\n')[0].split(',')
print(x[0], end=":")
vars.append(x[0])
if x[2] != "0":
print(" .space "+x[2])
else:
if(x[1] == "int"):
print(" .word 0")
types[x[0]] = "int"
else :
types[x[0]] = "float"
print(" .float 0.0")
print("newline: .asciiz \"\\n\"")
print(".text")
print(".globl main")
# -------- making list of local variables used in each fn---------
with open (sys.argv[2], "r") as myfile:
icode=myfile.readlines()
localvars = {}
for i in icode:
y = i.split('\n')[0].split(' ')
y = list(filter(('').__ne__, y))
if y[1] == "func" and y[2] == "begin":
currentFn = y[3]
localvars[currentFn] = list()
for j in y:
if j.endswith("_" + currentFn) and j not in localvars[currentFn]:
localvars[currentFn].append(j)
# print(localvars)
# ----------------------------------------------------------------
for i in icode:
y = i.split('\n')[0].split(' ')
y = list(filter(('').__ne__, y))
y[0] = 'L'+y[0]
# print(y)
if(len(y) < 2):
continue;
if y[1] != "func":
print(y[0], end=" ")
if(y[1] == "goto"):
y[2] = 'L'+ y[2] # goto statemements
print("b " + y[2], end="")
elif y[1] == "func":
if y[2] == "begin":
counter = 0 # i in $ai initialised to zero.
currentFn = y[3]
print(currentFn + ":", end="")
else:
print(y[0], end=" ")
print("jr $ra")
elif y[1] in ["return", "refparam", "param", "call"]: # func statements
if y[1] == "return":
if len(y)==2:
print("jr $ra", end="")
elif iswordreg(y[2]): # return t0
print("move $v0, " + checker(vars, y[2]))
print("jr $ra", end="")
else: # return f0
print("mfc1 $v0, " + checker(vars, y[2]))
print("jr $ra", end="")
elif y[1] == "refparam":
if iswordreg(y[2]): # refparam t0
print("move "+checker(vars, y[2])+", $v0", end="")
else: # refparam f0
print("mtc1 $v0, "+checker(vars, y[2]), end="")
elif y[1] == "param":
if counter == 0: # if we're on the first param, move the stack
z = ""
size = 108 + len(localvars[currentFn])*4
print("addi $sp, $sp, -" + str(size))
else:
z = str(counter*4)
print("sw $a" + str(counter) + ", " + z + "($sp)") # push the $ai into the stack
if iswordreg(y[2]): # load new value in the $ai
print("move $a" + str(counter) + ", " + checker(vars, y[2]), end="") # param t0
else:
print("mfc1 $a" + str(counter) + ", " + checker(vars, y[2]), end="") # param f0
counter = counter+1
elif y[1] == "call":
#push remaining ai
if counter == 0: # if we're on the first param, move the stack
z = ""
size = 108 + len(localvars[currentFn])*4
print("addi $sp,$sp,-" + str(size))
else:
z = str(counter*4)
for x in range(0, 4-counter):
print("sw $a" + str(counter+x) + ", " + z + "($sp)") # push the $ai into the stack
z = str((counter+x+1)*4)
#push t0-t9
for x in range(10):
print("sw $t" + str(x) + ", " + str(4*(4+x)) + "($sp)") # push the $ti into the stack
#push f0-f9 and f20
for x in range(10):
print("swc1 $f" + str(x) + ", " + str(4*(14+x)) + "($sp)") # push the $ti into the stack
print("swc1 $f20, 96($sp)") # push the $ti into the stack
#push s0
print("sw $s0, 100($sp)") # push the $ti into the stack
#push ra
print("sw $ra, 104($sp)") # push the $ti into the stack
#push all variables in this function to stack
for i, local in enumerate(localvars[currentFn]):
print("lw $t0, " + local)
print("sw $t0, " + str(108 + i*4) + "($sp)") # push the $ti into the stack
print("jal " + y[2].split(',')[0])
# restore all the registers
print("lw $a0, ($sp)")
print("lw $a1, 4($sp)")
print("lw $a2, 8($sp)")
print("lw $a3, 12($sp)")
#load all variables in this function to stack
for i, local in enumerate(localvars[currentFn]):
print("lw $t0, " + str(108 + i*4) + "($sp)") # load the $ti into the stack
print("sw $t0, " + local)
#load ti
for x in range(0,10):
print("lw $t" + str(x) + ", " + str(4*(4+x)) + "($sp)") # restore the $ti from the stack
#load f0-f9 and f20
for x in range(10):
print("lwc1 $f" + str(x) + ", " + str(4*(14+x)) + "($sp)") # load the $fi into the stack
print("lwc1 $f20, 96($sp)") # load the $fi into the stack
#load s0
print("lw $s0, 100($sp)") # load the $s0 into the stack
#load ra
print("lw $ra, 104($sp)") # load the $ra into the stack
# pop !!
size = 108 + len(localvars[currentFn])*4
print("addi $sp, $sp, " + str(size), end="")
counter = 0
elif(y[1].startswith("if")):
# print(y)
a = y[1].split('(')[1]
b = y[3].split(')')[0]
if isfloatreg(a) and isfloatreg(b): # (f0 < f1)
operator = {
"<" : "c.lt.s",
"<=" : "c.le.s",
"==" : "c.eq.s",
">" : "c.lt.s",
">=" : "c.le.s",
"!=" : "c.eq.s",
}
if y[2] in [">", ">="]:
a,b = b,a
print(operator.get(y[2]) + " " + checker(vars, a) + ", " + checker(vars, b)) # if $f2 operator $f4 then code = 1 else code = 0
if y[2]=="!=":
print("bc1f L" + y[-1], end="") # if code == 0 then jump to label
else:
print("bc1t L" + y[-1], end="") # if code == 1 then jump to label
else: # (t0 < t1)
operator = {
"<" : "bltz",
">" : "bgtz",
"<=" : "blez",
">=" : "bgez",
"==" : "beqz",
"!=" : "bnez",
}
if(b[0].isdigit()):
print("li $s0, " + b)
print("sub $s0, " + checker(vars,a) + ", $s0")
else:
print("sub $s0, " + checker(vars,a) + ", " + checker(vars,b))
print(" " + operator.get(y[2]) + " $s0, L" + y[-1], end="") # we're comparing two registers
elif y[1].startswith("print"):
reg = y[1].split(')')[0].split('(')[1]
print("addi $sp, $sp, -8")
print("sw $a0, ($sp)")
print("sw $v0, 4($sp)")
if(isfloatreg(reg)):
print("li $v0, 2 \nmov.s $f12, "+checker(vars, reg)+" \nsyscall") # print float value in reg
else:
print("li $v0, 1 \nmove $a0, "+checker(vars, reg)+" \nsyscall") # print int value in reg
#printing newline
print("li $v0, 4\nla $a0, newline\nsyscall")
print("lw $a0, ($sp)")
print("lw $v0, 4($sp)")
print("addi $sp, $sp, 8", end="")
elif y[1].startswith("Load"):
print("la $"+y[2]+", "+y[3], end="")
else:
if any('[' in x for x in y):
y[1]=y[1]+y[2]+y[3]
a,b = y[1].split('=')
if y[1][-1]==']':
# print("lol")
t1,t2 = b.split('[')
t2 = t2.split(']')[0]
if iswordreg(a): # t0 = t1[t2]
print("add $"+ t1+", $"+t1+", $"+t2)
print("lw $"+a+", ($"+t1+")", end="")
else: # f0 = t1[t2]
print("add $"+ t1+", $"+t1+", $"+t2)
print("l.s $"+a+", ($"+t1+")", end="")
else:
# print("hey")
t1,t2 = a.split('[')
t2 = t2.split(']')[0]
if iswordreg(b):
print("add $"+ t1+", $"+t1+", $"+t2)
print("sw $"+b+", ($"+t1+")", end="")
else:
print("add $"+ t1+", $"+t1+", $"+t2)
print("s.s $"+b+", ($"+t1+")", end="")
elif(len(y) == 4): # assignments
a = y[-3]
b = y[-1]
if(b[0].isdigit() or b[0]=="-"):
if isfloatreg(a) or isfloatreg(b):
print("li.s " + checker(vars,a) + ", " + b, end="")
else:
print("li " + checker(vars,a) + ", " + b, end="")
else:
if b.startswith('ConvertToInt'):
b = b.split('(')[1].split(')')[0]
print("cvt.w.s " + checker(vars, b) + ", " + checker(vars, b)) # "converting float to int"
print("\tmfc1 " + checker(vars, a) + ", " + checker(vars, b), end="")
elif b.startswith('ConvertToFloat'):
b = b.split('(')[1].split(')')[0]
print("mtc1 " + checker(vars, b) + ", " + checker(vars, a))
print("\tcvt.s.w " + checker(vars, a) + ", " + checker(vars, a), end="") # "converting int to float"
elif a in vars:
if isfloatreg(a) or isfloatreg(b):
print("s.s " + checker(vars,b) + ", " + checker(vars,a), end="") # var = f0
else:
print("sw " + checker(vars,b) + ", " + checker(vars,a), end="") # var = t0
else :
if checker(vars,b).startswith("$a"):
if isfloatreg(a):
print("mtc1 " + checker(vars,b) + ", " + checker(vars,a), end="")
else:
print("move " + checker(vars,a) + ", " + checker(vars,b), end="")
elif checker(vars,a).startswith("$a"):
if isfloatreg(b):
print("mfc1 " + checker(vars,a) + ", " + checker(vars,b), end="")
else:
print("move " + checker(vars,a) + ", " + checker(vars,b), end="")
elif isfloatreg(a) and isfloatreg(b):
print("mov.s " + checker(vars,a) + ", " + checker(vars,b), end="") # f0 = f1
elif isfloatreg(a) or isfloatreg(b):
print("l.s " + checker(vars,a) + ", " + checker(vars,b), end="") # f0 = var
else:
if iswordreg(a) and iswordreg(b):
print("move " + checker(vars,a) + ", " + checker(vars,b), end="") # t0 = t1
else:
print("lw " + checker(vars,a) + ", " + checker(vars,b), end="") # t0 = var
else:
# print(y)
a = y[-5]
b = y[-3]
opt = y[-2]
c = y[-1]
# print(y)
operator = {
"+" : "add",
"-" : "sub",
"*" : "mul",
}
x = " "
if isfloatreg(a) or isfloatreg(b):
x = ".s "
if opt in ["+","-","*"]:
print(operator.get(opt) + x + checker(vars,a) + ", " + checker(vars,b) + ", " + checker(vars,c), end="")
else:
if isfloatreg(a) or isfloatreg(b):
print("div.s " + checker(vars, a) + ", " + checker(vars, b) + ", " + checker(vars, c)) # f0 = f1 / f2
else:
print("div " + checker(vars,b) + ", " + checker(vars,c)) # t0 = t1 / t2
if(opt == "%"):
print("mfhi " + checker(vars,a), end="") # quotient
else:
print("mflo " + checker(vars,a), end="") # remainder
print()