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