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