-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_generate.py
170 lines (150 loc) · 4.99 KB
/
code_generate.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
print 'This program assumes there are only two registers and works for three adress code statements as inputs.'
print 'For the condition where no registers are empty and no operands are in the register, the register which is used first and has not been used since is emptied by using a counter s'
i = open("ip.txt","r")
o = open("op_gen.txt","w")
ad = []
s=[0,0]
dc = {'+': 'ADD','-': 'SUB','*': 'MUL','/': 'DIV'}
t=[]
print ad
print t
for line in i:
#print line
line = line.strip()
x,y = line.split('=')
t = y.split(' ')
#print x
#print t
#print ad
if t[0] in ad and t[2] in ad:
#print 'bth'
o.write(dc[t[1]] + " R" + str(ad.index(t[0])) + ", R" + str(ad.index(t[2])))
o.write('\n')
ind=ad.index(t[0])
ad[ind] = x
s[ind] = 100;
if ind == 0:
s[1]= s[1]-10
else:
s[0] = s[1] -10
elif t[0] in ad:
#print 't0'
o.write(dc[t[1]] + " " + t[2]+ ",R" +str(ad.index(t[0])))
o.write('\n')
ind=ad.index(t[0])
ad[ind] = x
s[ind] = 100;
if ind == 0:
s[1]= s[1]-10
else:
s[0] = s[1] -10
elif t[2] in ad:
#print 't1'
o.write(dc[t[1]] + " R" + str(ad.index(t[2])) + "," + t[0])
o.write('\n')
ind=ad.index(t[2])
ad[ind] = x
s[ind] = 100;
if ind == 0:
s[1]= s[1]-10
else:
s[0] = s[1] -10
else:
if not ad :
#print 'else 1'
ad.append(t[0]);
o.write("MOV " + t[0] + ",R" + str(ad.index(t[0])))
o.write('\n')
o.write(dc[t[1]] + " " + t[2]+ ",R" +str(ad.index(t[0])))
o.write('\n')
ind = ad.index(t[0])
ad[ind] = x
s[ind] = 100;
if ind == 0:
s[1]= s[1]-10
else:
s[0] = s[1] -10
elif ad:
if len(ad) < 2:
if t[2] not in ad:
ad.append(t[2])
o.write("MOV " + t[2] + ",R" + str(ad.index(t[2])))
o.write('\n')
o.write(dc[t[1]] + " " + t[0]+ ",R" +str(ad.index(t[2])))
o.write('\n')
ind=ad.index(t[2])
ad[ind] = x
s[ind] = 100;
if ind == 0:
s[1]= s[1]-10
else:
s[0] = s[1] -10
elif t[0] not in ad:
ad.append(t[0])
o.write("MOV " + t[0] + ",R" + str(ad.index(t[0])))
o.write('\n')
o.write(dc[t[1]] + " " + t[2]+ ",R" +str(ad.index(t[0])))
o.write('\n')
ind=ad.index(t[0])
ad[ind] = x
s[ind] = 1000;
if ind == 0:
s[1]= s[1]-100
else:
s[0] = s[1] -100
else:
if t[2] not in ad:
c+=1;
z=min[s[0],s[1]]
o.write("MOV " + ",R" + str(z) + "x" + str(c))
o.write("MOV " + t[2] + ",R" + str(z))
ad[z]= x
s[z] = 1000
if z == 0:
s[1]= s[1]-100
else:
s[0] = s[1] -100
o.write(dc[t[1]] + " " + t[2]+ ",R" +str(ad.index(t[0])))
ad[ad.index(t[0])] = x
o.write('\n')
ind=ad.index(t[0])
ad[ind] = x
s[ind] = 1000;
if ind == 0:
s[1]= s[1]-100
else:
s[0] = s[1] -100
if t[0] not in ad:
c+=1;
z=min[s[0],s[1]]
o.write("MOV " + ",R" + str(z) + "x" + str(c))
o.write("MOV " + t[0] + ",R" + str(z))
ad[z]= x
s[z] = 1000
if z == 0:
s[1]= s[1]-100
else:
s[0] = s[1] -100
o.write(dc[t[1]] + " " + t[2]+ ",R" +str(ad.index(t[0])))
o.write('\n')
ind=ad.index(t[0])
ad[ind] = x
s[ind] = 1000;
if ind == 0:
s[1]= s[1]-100
else:
s[0] = s[1] -100
print ad
print t
#print ad
print ad
print t
o.write("MOV R" + str(ind) + "," + x)
o.close();
i.close();
/*Input: ip.txt
t=a - b
u=a - c
v=t + u
d=v + u
*/