171
|
1 from .basicblock import BasicBlock
|
175
|
2 from .function import Function
|
171
|
3
|
205
|
4
|
171
|
5 class Value:
|
230
|
6 """ Temporary SSA value (value that is assigned only once! """
|
|
7 def __init__(self, name):
|
239
|
8 # TODO: add typing? for now only handle integers
|
|
9 self.name = name
|
|
10 self.used_by = []
|
|
11 self.Setter = None
|
230
|
12
|
|
13 def __repr__(self):
|
|
14 return '{0}'.format(self.name) # + str(self.IsUsed)
|
|
15
|
|
16 @property
|
252
|
17 def UsedInBlocks(self):
|
|
18 bbs = [i.parent for i in self.used_by + [self.Setter]]
|
|
19 assert all(isinstance(bb, BasicBlock) for bb in bbs)
|
|
20 return set(bbs)
|
|
21
|
|
22 @property
|
230
|
23 def IsUsed(self):
|
239
|
24 return len(self.used_by) > 0
|
|
25
|
252
|
26 Used = IsUsed
|
|
27
|
239
|
28 def onlyUsedInBlock(self, bb):
|
259
|
29 return all(use.Block is bb for use in self.used_by)
|
239
|
30
|
258
|
31 def lastUse(self, ins):
|
|
32 assert ins in self.used_by
|
|
33 return all(not ins.precedes(ub) for ub in self.used_by)
|
174
|
34
|
205
|
35 class Variable(Value):
|
|
36 pass
|
|
37
|
239
|
38
|
174
|
39 class Use:
|
239
|
40 def __init__(self, user, val):
|
|
41 self.user = user
|
|
42 assert isinstance(val, Value)
|
|
43 self.val = val
|
|
44 self.val.used_by.append(self.user)
|
|
45
|
|
46 def delete(self):
|
|
47 self.val.used_by.remove(self.user)
|
|
48
|
110
|
49
|
155
|
50 class Instruction:
|
239
|
51 """ Base class for all instructions. """
|
|
52 def __init__(self):
|
|
53 # live variables at this node:
|
|
54 self.live_in = set()
|
|
55 self.live_out = set()
|
|
56 # What variables this instruction uses and defines:
|
|
57 self.defs = []
|
|
58 self.uses = []
|
259
|
59
|
239
|
60 def delete(self):
|
209
|
61 while self.uses:
|
|
62 use = self.uses.pop()
|
|
63 use.delete()
|
239
|
64 while self.defs:
|
|
65 d = self.defs.pop()
|
|
66 d.Setter = None
|
|
67
|
|
68 def addUse(self, val):
|
|
69 self.uses.append(Use(self, val))
|
|
70
|
|
71 def removeUse(self, val):
|
|
72 for u in self.uses:
|
|
73 if u.val is val:
|
|
74 theUse = u
|
|
75 theUse.delete()
|
|
76 self.uses.remove(theUse)
|
|
77
|
|
78 def addDef(self, v):
|
|
79 self.defs.append(v)
|
|
80 assert v.Setter == None
|
|
81 v.Setter = self
|
|
82
|
|
83 def removeDef(self, v):
|
|
84 assert v.Setter is self
|
|
85 v.Setter = None
|
|
86 self.defs.remove(v)
|
|
87
|
|
88 def getParent(self):
|
|
89 return self.parent
|
|
90
|
|
91 def setParent(self, p):
|
|
92 self.parent = p
|
|
93 Parent = property(getParent, setParent)
|
|
94
|
|
95 def replaceValue(self, old, new):
|
252
|
96 raise NotImplementedError('{}'.format(type(self)))
|
239
|
97
|
|
98 @property
|
|
99 def Position(self):
|
258
|
100 return self.Block.Instructions.index(self)
|
|
101
|
|
102 def precedes(self, other):
|
|
103 assert isinstance(other, Instruction)
|
|
104 if self.Block is other.Block:
|
|
105 return other.Position > self.Position
|
259
|
106 else:
|
|
107 return self.Block.precedes(other.Block)
|
|
108
|
|
109 def follows(self, other):
|
|
110 pass
|
239
|
111
|
252
|
112 @property
|
|
113 def Function(self):
|
|
114 return self.Block.parent
|
|
115
|
|
116 @property
|
|
117 def Block(self):
|
|
118 return self.Parent
|
|
119
|
239
|
120 def check(self):
|
|
121 # Check that the variables defined by this instruction
|
|
122 # are only used in the same block
|
|
123 for v in self.defs:
|
|
124 assert v.Setter is self
|
|
125 for ub in v.used_by:
|
252
|
126 assert ub.Function == self.Function
|
239
|
127
|
|
128 # Check that variables used are defined earlier:
|
|
129 for u in self.uses:
|
|
130 v = u.val
|
252
|
131 #assert self.Position > v.Setter.Position
|
239
|
132
|
|
133
|
|
134
|
177
|
135
|
|
136 class Terminator(Instruction):
|
239
|
137 @property
|
|
138 def Targets(self):
|
|
139 return self.getTargets()
|
|
140
|
|
141 def changeTarget(self, tfrom, tto):
|
|
142 pass
|
|
143
|
170
|
144
|
158
|
145 # Function calling:
|
171
|
146 class Call(Instruction):
|
175
|
147 def __init__(self, callee, arguments, result=None):
|
110
|
148 super().__init__()
|
|
149 self.callee = callee
|
175
|
150 assert type(callee) is Function
|
110
|
151 self.arguments = arguments
|
175
|
152 for arg in arguments:
|
|
153 assert type(arg) is Value
|
|
154 self.addUse(arg)
|
|
155 self.result = result
|
|
156 if result:
|
|
157 assert type(result) is Value
|
|
158 self.addDef(result)
|
158
|
159 def __repr__(self):
|
175
|
160 if self.result:
|
|
161 pfx = '{0} = '.format(self.result)
|
|
162 else:
|
|
163 pfx = ''
|
|
164 args = ','.join([str(arg) for arg in self.arguments])
|
|
165 return pfx + '{0}({1})'.format(self.callee.name, args)
|
110
|
166
|
177
|
167 class Return(Terminator):
|
174
|
168 def __init__(self, value=None):
|
|
169 super().__init__()
|
|
170 self.value = value
|
|
171 if value:
|
|
172 self.addUse(value)
|
158
|
173 def __repr__(self):
|
174
|
174 if self.value:
|
230
|
175 return 'ret {0}'.format(self.value)
|
174
|
176 else:
|
230
|
177 return 'ret'
|
173
|
178 def getTargets(self):
|
|
179 return []
|
70
|
180
|
174
|
181 class Alloc(Instruction):
|
|
182 """ Allocates space on the stack """
|
|
183 def __init__(self, value):
|
|
184 super().__init__()
|
222
|
185 assert isinstance(value, Value)
|
174
|
186 self.value = value
|
|
187 self.addDef(value)
|
|
188 def __repr__(self):
|
|
189 return '{0} = alloc'.format(self.value)
|
|
190
|
171
|
191 class ImmLoad(Instruction):
|
|
192 def __init__(self, target, value):
|
|
193 super().__init__()
|
204
|
194 assert type(target) is Value
|
171
|
195 self.target = target
|
170
|
196 self.value = value
|
174
|
197 self.addDef(target)
|
170
|
198 def __repr__(self):
|
239
|
199 return '{} = {}'.format(self.target, self.value)
|
170
|
200
|
171
|
201 # Data operations
|
70
|
202 class BinaryOperator(Instruction):
|
230
|
203 def __init__(self, result, operation, value1, value2):
|
171
|
204 super().__init__()
|
157
|
205 #print('operation is in binops:', operation in BinOps)
|
104
|
206 # Check types of the two operands:
|
230
|
207 assert type(value1) is Value, str(value1) + str(type(value1))
|
|
208 assert type(value2) is Value, value2
|
171
|
209 self.result = result
|
174
|
210 self.addDef(result)
|
104
|
211 self.value1 = value1
|
|
212 self.value2 = value2
|
174
|
213 self.addUse(value1)
|
|
214 self.addUse(value2)
|
104
|
215 self.operation = operation
|
239
|
216
|
230
|
217 def __repr__(self):
|
|
218 a, b = self.value1, self.value2
|
|
219 return '{} = {} {} {}'.format(self.result, a, self.operation, b)
|
70
|
220
|
239
|
221 def replaceValue(self, old, new):
|
|
222 if old is self.value1:
|
|
223 self.value1 = new
|
|
224 elif old is self.value2:
|
|
225 self.value2 = new
|
|
226 elif old is self.result:
|
|
227 self.result = new
|
|
228 else:
|
|
229 raise Exception()
|
|
230 self.removeUse(old)
|
|
231 self.addUse(new)
|
|
232
|
170
|
233 # Memory functions:
|
171
|
234 class Load(Instruction):
|
252
|
235 def __init__(self, location, value):
|
|
236 super().__init__()
|
|
237 assert type(value) is Value
|
|
238 assert isinstance(location, Value), "Location must be a value"
|
|
239 self.value = value
|
|
240 self.addDef(value)
|
|
241 self.location = location
|
|
242 self.addUse(self.location)
|
|
243
|
|
244 def __repr__(self):
|
|
245 return '{} = [{}]'.format(self.value, self.location)
|
157
|
246
|
171
|
247 class Store(Instruction):
|
252
|
248 def __init__(self, location, value):
|
259
|
249 super().__init__()
|
|
250 assert type(value) is Value, value
|
|
251 assert isinstance(location, Value), "Location must be a value"
|
|
252 self.location = location
|
|
253 self.value = value
|
|
254 self.addUse(value)
|
|
255 self.addUse(location)
|
|
256
|
252
|
257 def __repr__(self):
|
|
258 return '[{}] = {}'.format(self.location, self.value)
|
|
259
|
|
260 def replaceValue(self, old, new):
|
|
261 if old is self.value:
|
|
262 self.value = new
|
|
263 elif old is self.location:
|
|
264 self.location = new
|
|
265 else:
|
|
266 raise Exception()
|
|
267 self.removeUse(old)
|
|
268 self.addUse(new)
|
160
|
269
|
171
|
270 # Branching:
|
177
|
271 class Branch(Terminator):
|
219
|
272 def __init__(self, target):
|
171
|
273 super().__init__()
|
|
274 assert type(target) is BasicBlock
|
|
275 self.target = target
|
219
|
276 def __repr__(self):
|
|
277 return 'BRANCH {0}'.format(self.target)
|
|
278 def getTargets(self):
|
|
279 return [self.target]
|
|
280 def changeTarget(self, tfrom, tto):
|
|
281 assert tfrom is self.target
|
|
282 self.target = tto
|
170
|
283
|
177
|
284 class ConditionalBranch(Terminator):
|
171
|
285 def __init__(self, a, cond, b, lab1, lab2):
|
|
286 super().__init__()
|
|
287 self.a = a
|
|
288 assert type(a) is Value
|
170
|
289 self.cond = cond
|
221
|
290 assert cond in ['==', '<', '>']
|
171
|
291 self.b = b
|
174
|
292 self.addUse(a)
|
|
293 self.addUse(b)
|
171
|
294 assert type(b) is Value
|
|
295 assert type(lab1) is BasicBlock
|
170
|
296 self.lab1 = lab1
|
171
|
297 assert type(lab2) is BasicBlock
|
170
|
298 self.lab2 = lab2
|
|
299 def __repr__(self):
|
171
|
300 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
|
173
|
301 def getTargets(self):
|
|
302 return [self.lab1, self.lab2]
|
177
|
303 def changeTarget(self, tfrom, tto):
|
219
|
304 assert tfrom is self.lab1 or tfrom is self.lab2
|
177
|
305 if tfrom is self.lab1:
|
|
306 self.lab1 = tto
|
219
|
307 elif tfrom is self.lab2:
|
177
|
308 self.lab2 = tto
|
170
|
309
|
171
|
310 class PhiNode(Instruction):
|
252
|
311 def __init__(self):
|
|
312 super().__init__()
|
|
313 self.incBB = []
|
171
|
314
|
252
|
315 def addIncoming(self, bb):
|
|
316 self.incBB.append(bb)
|
|
317
|