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__()
|
222
|
99 assert isinstance(value, Value)
|
174
|
100 self.value = value
|
|
101 self.addDef(value)
|
|
102 def __repr__(self):
|
|
103 return '{0} = alloc'.format(self.value)
|
|
104
|
171
|
105 class ImmLoad(Instruction):
|
|
106 def __init__(self, target, value):
|
|
107 super().__init__()
|
204
|
108 assert type(target) is Value
|
171
|
109 self.target = target
|
170
|
110 self.value = value
|
174
|
111 self.addDef(target)
|
170
|
112 def __repr__(self):
|
171
|
113 return '{0} = {1}'.format(self.target, self.value)
|
170
|
114
|
171
|
115 # Data operations
|
70
|
116 class BinaryOperator(Instruction):
|
171
|
117 def __init__(self, result, operation, value1, value2):
|
|
118 super().__init__()
|
157
|
119 #print('operation is in binops:', operation in BinOps)
|
104
|
120 # Check types of the two operands:
|
204
|
121 assert type(value1) is Value
|
|
122 assert type(value2) is Value
|
171
|
123 self.result = result
|
174
|
124 self.addDef(result)
|
104
|
125 self.value1 = value1
|
|
126 self.value2 = value2
|
174
|
127 self.addUse(value1)
|
|
128 self.addUse(value2)
|
104
|
129 self.operation = operation
|
158
|
130 def __repr__(self):
|
171
|
131 return '{0} = {2} {1} {3}'.format(self.result, self.operation, self.value1, self.value2)
|
70
|
132
|
170
|
133 # Memory functions:
|
171
|
134 class Load(Instruction):
|
174
|
135 def __init__(self, location, value):
|
171
|
136 super().__init__()
|
174
|
137 assert type(value) is Value
|
205
|
138 assert isinstance(location, Value), "Location must be a value"
|
157
|
139 self.value = value
|
174
|
140 self.addDef(value)
|
|
141 self.location = location
|
|
142 self.addUse(self.location)
|
158
|
143 def __repr__(self):
|
221
|
144 return '{} <= [{}]'.format(self.value, self.location)
|
157
|
145
|
171
|
146 class Store(Instruction):
|
174
|
147 def __init__(self, location, value):
|
171
|
148 super().__init__()
|
174
|
149 assert type(value) is Value
|
205
|
150 assert isinstance(location, Value), "Location must be a value"
|
174
|
151 self.location = location
|
160
|
152 self.value = value
|
174
|
153 self.addUse(value)
|
175
|
154 self.addUse(location)
|
160
|
155 def __repr__(self):
|
221
|
156 return '[{}] <= {}'.format(self.location, self.value)
|
160
|
157
|
171
|
158 # Branching:
|
177
|
159 class Branch(Terminator):
|
219
|
160 def __init__(self, target):
|
171
|
161 super().__init__()
|
|
162 assert type(target) is BasicBlock
|
|
163 self.target = target
|
219
|
164 def __repr__(self):
|
|
165 return 'BRANCH {0}'.format(self.target)
|
|
166 def getTargets(self):
|
|
167 return [self.target]
|
|
168 def changeTarget(self, tfrom, tto):
|
|
169 assert tfrom is self.target
|
|
170 self.target = tto
|
170
|
171
|
177
|
172 class ConditionalBranch(Terminator):
|
171
|
173 def __init__(self, a, cond, b, lab1, lab2):
|
|
174 super().__init__()
|
|
175 self.a = a
|
|
176 assert type(a) is Value
|
170
|
177 self.cond = cond
|
221
|
178 assert cond in ['==', '<', '>']
|
171
|
179 self.b = b
|
174
|
180 self.addUse(a)
|
|
181 self.addUse(b)
|
171
|
182 assert type(b) is Value
|
|
183 assert type(lab1) is BasicBlock
|
170
|
184 self.lab1 = lab1
|
171
|
185 assert type(lab2) is BasicBlock
|
170
|
186 self.lab2 = lab2
|
|
187 def __repr__(self):
|
171
|
188 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
|
173
|
189 def getTargets(self):
|
|
190 return [self.lab1, self.lab2]
|
177
|
191 def changeTarget(self, tfrom, tto):
|
219
|
192 assert tfrom is self.lab1 or tfrom is self.lab2
|
177
|
193 if tfrom is self.lab1:
|
|
194 self.lab1 = tto
|
219
|
195 elif tfrom is self.lab2:
|
177
|
196 self.lab2 = tto
|
170
|
197
|
171
|
198 class PhiNode(Instruction):
|
|
199 def __init__(self):
|
|
200 super().__init__()
|
|
201 self.incBB = []
|
|
202 def addIncoming(self, bb):
|
|
203 self.incBB.append(bb)
|
|
204
|