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