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