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
|
262
|
40
|
205
|
41 class Variable(Value):
|
|
42 pass
|
|
43
|
239
|
44
|
174
|
45 class Use:
|
239
|
46 def __init__(self, user, val):
|
|
47 self.user = user
|
|
48 assert isinstance(val, Value)
|
|
49 self.val = val
|
|
50 self.val.used_by.append(self.user)
|
|
51
|
|
52 def delete(self):
|
|
53 self.val.used_by.remove(self.user)
|
|
54
|
110
|
55
|
155
|
56 class Instruction:
|
239
|
57 """ Base class for all instructions. """
|
|
58 def __init__(self):
|
|
59 # live variables at this node:
|
|
60 self.live_in = set()
|
|
61 self.live_out = set()
|
|
62 # What variables this instruction uses and defines:
|
|
63 self.defs = []
|
|
64 self.uses = []
|
259
|
65
|
239
|
66 def delete(self):
|
209
|
67 while self.uses:
|
|
68 use = self.uses.pop()
|
|
69 use.delete()
|
239
|
70 while self.defs:
|
|
71 d = self.defs.pop()
|
|
72 d.Setter = None
|
|
73
|
|
74 def addUse(self, val):
|
|
75 self.uses.append(Use(self, val))
|
|
76
|
|
77 def removeUse(self, val):
|
|
78 for u in self.uses:
|
|
79 if u.val is val:
|
|
80 theUse = u
|
|
81 theUse.delete()
|
|
82 self.uses.remove(theUse)
|
|
83
|
|
84 def addDef(self, v):
|
|
85 self.defs.append(v)
|
|
86 assert v.Setter == None
|
|
87 v.Setter = self
|
|
88
|
|
89 def removeDef(self, v):
|
|
90 assert v.Setter is self
|
|
91 v.Setter = None
|
|
92 self.defs.remove(v)
|
|
93
|
|
94 def getParent(self):
|
|
95 return self.parent
|
|
96
|
|
97 def setParent(self, p):
|
|
98 self.parent = p
|
|
99 Parent = property(getParent, setParent)
|
|
100
|
|
101 def replaceValue(self, old, new):
|
262
|
102 # TODO: make this a generic function
|
252
|
103 raise NotImplementedError('{}'.format(type(self)))
|
239
|
104
|
|
105 @property
|
|
106 def Position(self):
|
258
|
107 return self.Block.Instructions.index(self)
|
|
108
|
|
109 def precedes(self, other):
|
|
110 assert isinstance(other, Instruction)
|
|
111 if self.Block is other.Block:
|
|
112 return other.Position > self.Position
|
259
|
113 else:
|
|
114 return self.Block.precedes(other.Block)
|
|
115
|
|
116 def follows(self, other):
|
|
117 pass
|
239
|
118
|
252
|
119 @property
|
|
120 def Function(self):
|
|
121 return self.Block.parent
|
|
122
|
|
123 @property
|
|
124 def Block(self):
|
|
125 return self.Parent
|
|
126
|
239
|
127 def check(self):
|
|
128 # Check that the variables defined by this instruction
|
|
129 # are only used in the same block
|
|
130 for v in self.defs:
|
|
131 assert v.Setter is self
|
|
132 for ub in v.used_by:
|
252
|
133 assert ub.Function == self.Function
|
239
|
134
|
|
135 # Check that variables used are defined earlier:
|
|
136 for u in self.uses:
|
|
137 v = u.val
|
252
|
138 #assert self.Position > v.Setter.Position
|
239
|
139
|
|
140
|
|
141
|
158
|
142 # Function calling:
|
171
|
143 class Call(Instruction):
|
175
|
144 def __init__(self, callee, arguments, result=None):
|
110
|
145 super().__init__()
|
|
146 self.callee = callee
|
175
|
147 assert type(callee) is Function
|
110
|
148 self.arguments = arguments
|
175
|
149 for arg in arguments:
|
|
150 assert type(arg) is Value
|
|
151 self.addUse(arg)
|
|
152 self.result = result
|
|
153 if result:
|
|
154 assert type(result) is Value
|
|
155 self.addDef(result)
|
261
|
156
|
158
|
157 def __repr__(self):
|
175
|
158 if self.result:
|
|
159 pfx = '{0} = '.format(self.result)
|
|
160 else:
|
|
161 pfx = ''
|
|
162 args = ','.join([str(arg) for arg in self.arguments])
|
|
163 return pfx + '{0}({1})'.format(self.callee.name, args)
|
110
|
164
|
70
|
165
|
174
|
166 class Alloc(Instruction):
|
261
|
167 """ Allocates space on the stack """
|
|
168 def __init__(self, value):
|
|
169 super().__init__()
|
|
170 assert isinstance(value, Value)
|
|
171 self.value = value
|
|
172 self.addDef(value)
|
|
173
|
|
174 def __repr__(self):
|
|
175 return '{0} = alloc'.format(self.value)
|
|
176
|
174
|
177
|
171
|
178 class ImmLoad(Instruction):
|
261
|
179 def __init__(self, target, value):
|
|
180 super().__init__()
|
|
181 assert type(target) is Value
|
|
182 self.target = target
|
|
183 self.value = value
|
|
184 self.addDef(target)
|
|
185
|
|
186 def __repr__(self):
|
|
187 return '{} = {}'.format(self.target, self.value)
|
170
|
188
|
262
|
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):
|
265
|
288 def __init__(self, a, cond, b, lab1, lab2):
|
171
|
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
|
265
|
303 def __repr__(self):
|
|
304 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
|
261
|
305
|
265
|
306 def getTargets(self):
|
|
307 return [self.lab1, self.lab2]
|
|
308
|
|
309 def changeTarget(self, tfrom, tto):
|
219
|
310 assert tfrom is self.lab1 or tfrom is self.lab2
|
177
|
311 if tfrom is self.lab1:
|
|
312 self.lab1 = tto
|
219
|
313 elif tfrom is self.lab2:
|
177
|
314 self.lab2 = tto
|
170
|
315
|
261
|
316
|
|
317 class Return(Terminator):
|
|
318 def __init__(self, value=None):
|
|
319 super().__init__()
|
|
320 self.value = value
|
|
321 if value:
|
|
322 self.addUse(value)
|
|
323
|
|
324 def __repr__(self):
|
|
325 if self.value:
|
|
326 return 'ret {0}'.format(self.value)
|
|
327 else:
|
|
328 return 'ret'
|
|
329
|
|
330 def getTargets(self):
|
|
331 return []
|
|
332
|
|
333
|
171
|
334 class PhiNode(Instruction):
|
252
|
335 def __init__(self):
|
|
336 super().__init__()
|
|
337 self.incBB = []
|
171
|
338
|
252
|
339 def addIncoming(self, bb):
|
|
340 self.incBB.append(bb)
|
|
341
|