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