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