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