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