-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtargetcode.py
210 lines (190 loc) · 5.42 KB
/
targetcode.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
import sys
import codecs
original_stdout = sys.stdout # Save a reference to the original standard output
f= codecs.open('text.asm', 'w',encoding='utf-8')
sys.stdout = f
# with codecs.open('sample.txt', encoding='utf-8') as f:
# i = f.read().encode('utf-8')
# basic.run(i.decode('utf-8'))
tac = codecs.open("tac.txt",encoding='utf-8').read()
t1="t1"
t2="t2"
t3="t3"
a0="a0"
v0 = "v0"
class SymbolTable:
def __init__(self):
self.symbols = {}
self.cur=0
def get(self, name):
if isinstance(name,int) : return name
if(not self.available(name)):
SystemExit("Undeclared Variable "+ name)
value=self.symbols.get(name, None)
return value
def set(self, name):
if self.available(name): return
self.symbols[name]=self.cur+4
self.cur+=4
def remove(self,name):
del self.symbols[name]
def available(self,name):
return name in self.symbols
print("main:")
print('add $sp, $sp, -400')
addresstable = SymbolTable()
def load(register,val):
global addresstable
try:
numeric = int(val)
print(f'li ${register}, {numeric}')
except:
temp=400-addresstable.get(val)
print(f'lw ${register},{temp} ($sp)')
def save(register,val):
global addresstable
temp=400-addresstable.get(val)
print(f'sw ${register},{temp} ($sp)')
count = 1
def normalize(register):
global count
print(f'move $t4, ${register}')
print(f'li ${register}, 1')
print(f'bne $t4, 0, yareyare{count}') # changed from bgt to bne
print(f'li ${register}, 0')
print(f'yareyare{count}:')
count+=1
for code in tac.split('\n'):
code = code.strip()
v=code.split(' ')
#print(v)
if(len(v)==0): continue
if(len(v)==1):
if(v[0].encode('utf-8') in (b'\r', b'')): continue
else:
k=v[0].encode('utf-8')
print(f'{v[0]}:')
elif len(v)==2:
if(v[0]=="input"):
print("li $v0, 5")
print("syscall")
save(v0,v[1])
elif v[0]=='output':
print("li $v0, 1")
load(a0,v[1])
print('syscall')
elif v[0] =='goto':
print(f" b {v[1]}")
else:
print('$$$',v,"$$$")
elif len(v) ==3: #assignment
try:
val = int(v[2])
addresstable.set(v[0])
print(f'li $t1, {v[2]}')
save(t1,v[0])
except:
val = v[2]
load(t1,val)
addresstable.set(v[0])
print(f'move $t2, $t1')
save(t2,v[0])
elif len(v)==4: # if statement
if(v[0]=="IFNOT"):
load(t1,v[1])
normalize(t1)
print(f'bne $t1, 1, {v[3]}')
elif v[0]=="IF":
load(t1,v[1])
normalize(t1)
print(f'beq $t1, 1, {v[3]}')
elif v[2]== '!':
load(t1,v[3])
normalize(t1)
print("li $t2 1")
print("sub $t3, $t2, $t1")
save(v[0],t3)
else : pass
elif len(v) ==5: #operation
op = v[3]
addresstable.set(v[0])
if v[3] == '+':
load(t1,v[2])
load(t2,v[4])
print(f'add $t3, $t1, $t2')
save(t3,v[0])
elif v[3]=='-':
load(t1,v[2])
load(t2,v[4])
print(f'sub $t3, $t1, $t2')
save(t3,v[0])
elif v[3]=='*':
load(t1,v[2])
load(t2,v[4])
print(f'mult $t1, $t2')
print("mflo $t3")
save(t3,v[0])
elif v[3]=='/':
load(t1,v[2])
load(t2,v[4])
print(f'div $t1, $t2')
print("mflo $t3")
save(t3,v[0])
elif v[3]=='>':
load(t1,v[2])
load(t2,v[4])
print("li $t3, 0")
print(f"ble $t1, $t2 ,yareyare{count}")
print("li $t3, 1")
print(f'yareyare{count}:')
save(t3,v[0])
count+=1
elif v[3]=='<':
load(t1,v[2])
load(t2,v[4])
print("li $t3, 0")
print(f"bge $t1, $t2 ,yareyare{count}")
print("li $t3, 1")
print(f'yareyare{count}:')
save(t3,v[0])
count+=1
elif v[3]=='>=':
load(t1,v[2])
load(t2,v[4])
print("li $t3, 0")
print(f"blt $t1, $t2 ,yareyare{count}")
print("li $t3, 1")
print(f'yareyare{count}:')
save(t3,v[0])
count+=1
elif v[3]== '<=':
load(t1,v[2])
load(t2,v[4])
print("li $t3, 0")
print(f"bgt $t1, $t2 ,yareyare{count}")
print("li $t3, 1")
print(f'yareyare{count}:')
save(t3,v[0])
count+=1
elif v[3] == '==':
load(t1,v[2])
load(t2,v[4])
print("li $t3, 0")
print(f"bne $t1, $t2 ,yareyare{count}")
print("li $t3, 1")
print(f'yareyare{count}:')
save(t3,v[0])
count+=1
elif v[3] == '!=':
load(t1,v[2])
load(t2,v[4])
print("li $t3, 0")
print(f"beq $t1, $t2 ,yareyare{count}")
print("li $t3, 1")
print(f'yareyare{count}:')
save(t3,v[0])
count+=1
else:
print(f'###{code}###')
print("li $v0, 10")
print("syscall")