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):
|
171
|
8 # TODO: add typing? for now only handle integers
|
|
9 self.name = name
|
174
|
10 self.used_by = []
|
230
|
11
|
|
12 def __repr__(self):
|
|
13 return '{0}'.format(self.name) # + str(self.IsUsed)
|
|
14
|
|
15 @property
|
|
16 def IsUsed(self):
|
174
|
17 return len(self.used_by) > 0
|
|
18
|
205
|
19 class Variable(Value):
|
|
20 pass
|
|
21
|
174
|
22 class Use:
|
|
23 def __init__(self, user, val):
|
|
24 self.user = user
|
205
|
25 assert isinstance(val, Value)
|
174
|
26 self.val = val
|
|
27 self.val.used_by.append(self.user)
|
|
28 def delete(self):
|
|
29 self.val.used_by.remove(self.user)
|
110
|
30
|
155
|
31 class Instruction:
|
104
|
32 """ Base class for all instructions. """
|
171
|
33 def __init__(self):
|
|
34 # live variables at this node:
|
|
35 self.live_in = set()
|
|
36 self.live_out = set()
|
|
37 # What variables this instruction uses and defines:
|
174
|
38 self.defs = []
|
|
39 self.uses = []
|
|
40 def delete(self):
|
209
|
41 while self.uses:
|
|
42 use = self.uses.pop()
|
|
43 use.delete()
|
174
|
44 def addUse(self, val):
|
|
45 self.uses.append(Use(self, val))
|
|
46 def addDef(self, v):
|
|
47 self.defs.append(v)
|
173
|
48 def getParent(self):
|
|
49 return self.parent
|
174
|
50 def setParent(self, p):
|
|
51 self.parent = p
|
|
52 Parent = property(getParent, setParent)
|
177
|
53
|
|
54 class Terminator(Instruction):
|
173
|
55 @property
|
|
56 def Targets(self):
|
|
57 return self.getTargets()
|
177
|
58 def changeTarget(self, tfrom, tto):
|
|
59 pass
|
170
|
60
|
158
|
61 # Function calling:
|
171
|
62 class Call(Instruction):
|
175
|
63 def __init__(self, callee, arguments, result=None):
|
110
|
64 super().__init__()
|
|
65 self.callee = callee
|
175
|
66 assert type(callee) is Function
|
110
|
67 self.arguments = arguments
|
175
|
68 for arg in arguments:
|
|
69 assert type(arg) is Value
|
|
70 self.addUse(arg)
|
|
71 self.result = result
|
|
72 if result:
|
|
73 assert type(result) is Value
|
|
74 self.addDef(result)
|
158
|
75 def __repr__(self):
|
175
|
76 if self.result:
|
|
77 pfx = '{0} = '.format(self.result)
|
|
78 else:
|
|
79 pfx = ''
|
|
80 args = ','.join([str(arg) for arg in self.arguments])
|
|
81 return pfx + '{0}({1})'.format(self.callee.name, args)
|
110
|
82
|
177
|
83 class Return(Terminator):
|
174
|
84 def __init__(self, value=None):
|
|
85 super().__init__()
|
|
86 self.value = value
|
|
87 if value:
|
|
88 self.addUse(value)
|
158
|
89 def __repr__(self):
|
174
|
90 if self.value:
|
230
|
91 return 'ret {0}'.format(self.value)
|
174
|
92 else:
|
230
|
93 return 'ret'
|
173
|
94 def getTargets(self):
|
|
95 return []
|
70
|
96
|
174
|
97 class Alloc(Instruction):
|
|
98 """ Allocates space on the stack """
|
|
99 def __init__(self, value):
|
|
100 super().__init__()
|
222
|
101 assert isinstance(value, Value)
|
174
|
102 self.value = value
|
|
103 self.addDef(value)
|
|
104 def __repr__(self):
|
|
105 return '{0} = alloc'.format(self.value)
|
|
106
|
171
|
107 class ImmLoad(Instruction):
|
|
108 def __init__(self, target, value):
|
|
109 super().__init__()
|
204
|
110 assert type(target) is Value
|
171
|
111 self.target = target
|
170
|
112 self.value = value
|
174
|
113 self.addDef(target)
|
170
|
114 def __repr__(self):
|
171
|
115 return '{0} = {1}'.format(self.target, self.value)
|
170
|
116
|
171
|
117 # Data operations
|
70
|
118 class BinaryOperator(Instruction):
|
230
|
119 def __init__(self, result, operation, value1, value2):
|
171
|
120 super().__init__()
|
157
|
121 #print('operation is in binops:', operation in BinOps)
|
104
|
122 # Check types of the two operands:
|
230
|
123 assert type(value1) is Value, str(value1) + str(type(value1))
|
|
124 assert type(value2) is Value, value2
|
171
|
125 self.result = result
|
174
|
126 self.addDef(result)
|
104
|
127 self.value1 = value1
|
|
128 self.value2 = value2
|
174
|
129 self.addUse(value1)
|
|
130 self.addUse(value2)
|
104
|
131 self.operation = operation
|
230
|
132 def __repr__(self):
|
|
133 a, b = self.value1, self.value2
|
|
134 return '{} = {} {} {}'.format(self.result, a, self.operation, b)
|
70
|
135
|
170
|
136 # Memory functions:
|
171
|
137 class Load(Instruction):
|
174
|
138 def __init__(self, location, value):
|
171
|
139 super().__init__()
|
174
|
140 assert type(value) is Value
|
205
|
141 assert isinstance(location, Value), "Location must be a value"
|
157
|
142 self.value = value
|
174
|
143 self.addDef(value)
|
|
144 self.location = location
|
|
145 self.addUse(self.location)
|
158
|
146 def __repr__(self):
|
230
|
147 return '{} = [{}]'.format(self.value, self.location)
|
157
|
148
|
171
|
149 class Store(Instruction):
|
174
|
150 def __init__(self, location, value):
|
171
|
151 super().__init__()
|
230
|
152 assert type(value) is Value, value
|
205
|
153 assert isinstance(location, Value), "Location must be a value"
|
174
|
154 self.location = location
|
160
|
155 self.value = value
|
174
|
156 self.addUse(value)
|
175
|
157 self.addUse(location)
|
160
|
158 def __repr__(self):
|
230
|
159 return '[{}] = {}'.format(self.location, self.value)
|
160
|
160
|
171
|
161 # Branching:
|
177
|
162 class Branch(Terminator):
|
219
|
163 def __init__(self, target):
|
171
|
164 super().__init__()
|
|
165 assert type(target) is BasicBlock
|
|
166 self.target = target
|
219
|
167 def __repr__(self):
|
|
168 return 'BRANCH {0}'.format(self.target)
|
|
169 def getTargets(self):
|
|
170 return [self.target]
|
|
171 def changeTarget(self, tfrom, tto):
|
|
172 assert tfrom is self.target
|
|
173 self.target = tto
|
170
|
174
|
177
|
175 class ConditionalBranch(Terminator):
|
171
|
176 def __init__(self, a, cond, b, lab1, lab2):
|
|
177 super().__init__()
|
|
178 self.a = a
|
|
179 assert type(a) is Value
|
170
|
180 self.cond = cond
|
221
|
181 assert cond in ['==', '<', '>']
|
171
|
182 self.b = b
|
174
|
183 self.addUse(a)
|
|
184 self.addUse(b)
|
171
|
185 assert type(b) is Value
|
|
186 assert type(lab1) is BasicBlock
|
170
|
187 self.lab1 = lab1
|
171
|
188 assert type(lab2) is BasicBlock
|
170
|
189 self.lab2 = lab2
|
|
190 def __repr__(self):
|
171
|
191 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
|
173
|
192 def getTargets(self):
|
|
193 return [self.lab1, self.lab2]
|
177
|
194 def changeTarget(self, tfrom, tto):
|
219
|
195 assert tfrom is self.lab1 or tfrom is self.lab2
|
177
|
196 if tfrom is self.lab1:
|
|
197 self.lab1 = tto
|
219
|
198 elif tfrom is self.lab2:
|
177
|
199 self.lab2 = tto
|
170
|
200
|
171
|
201 class PhiNode(Instruction):
|
|
202 def __init__(self):
|
|
203 super().__init__()
|
|
204 self.incBB = []
|
|
205 def addIncoming(self, bb):
|
|
206 self.incBB.append(bb)
|
|
207
|