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)
|
173
|
46 @property
|
|
47 def Targets(self):
|
|
48 return self.getTargets()
|
170
|
49
|
158
|
50 # Function calling:
|
171
|
51 class Call(Instruction):
|
175
|
52 def __init__(self, callee, arguments, result=None):
|
110
|
53 super().__init__()
|
|
54 self.callee = callee
|
175
|
55 assert type(callee) is Function
|
110
|
56 self.arguments = arguments
|
175
|
57 for arg in arguments:
|
|
58 assert type(arg) is Value
|
|
59 self.addUse(arg)
|
|
60 self.result = result
|
|
61 if result:
|
|
62 assert type(result) is Value
|
|
63 self.addDef(result)
|
158
|
64 def __repr__(self):
|
175
|
65 if self.result:
|
|
66 pfx = '{0} = '.format(self.result)
|
|
67 else:
|
|
68 pfx = ''
|
|
69 args = ','.join([str(arg) for arg in self.arguments])
|
|
70 return pfx + '{0}({1})'.format(self.callee.name, args)
|
110
|
71
|
171
|
72 class Return(Instruction):
|
174
|
73 def __init__(self, value=None):
|
|
74 super().__init__()
|
|
75 self.value = value
|
|
76 if value:
|
|
77 self.addUse(value)
|
158
|
78 def __repr__(self):
|
174
|
79 if self.value:
|
|
80 return 'Return {0}'.format(self.value)
|
|
81 else:
|
|
82 return 'Return'
|
173
|
83 def getTargets(self):
|
|
84 return []
|
70
|
85
|
174
|
86 class Alloc(Instruction):
|
|
87 """ Allocates space on the stack """
|
|
88 def __init__(self, value):
|
|
89 super().__init__()
|
|
90 self.value = value
|
|
91 self.addDef(value)
|
|
92 def __repr__(self):
|
|
93 return '{0} = alloc'.format(self.value)
|
|
94
|
171
|
95 class ImmLoad(Instruction):
|
|
96 def __init__(self, target, value):
|
|
97 super().__init__()
|
|
98 self.target = target
|
170
|
99 self.value = value
|
174
|
100 self.addDef(target)
|
170
|
101 def __repr__(self):
|
171
|
102 return '{0} = {1}'.format(self.target, self.value)
|
170
|
103
|
171
|
104 # Data operations
|
70
|
105 class BinaryOperator(Instruction):
|
171
|
106 def __init__(self, result, operation, value1, value2):
|
|
107 super().__init__()
|
157
|
108 #print('operation is in binops:', operation in BinOps)
|
104
|
109 # Check types of the two operands:
|
171
|
110 self.result = result
|
174
|
111 self.addDef(result)
|
104
|
112 self.value1 = value1
|
|
113 self.value2 = value2
|
174
|
114 self.addUse(value1)
|
|
115 self.addUse(value2)
|
104
|
116 self.operation = operation
|
158
|
117 def __repr__(self):
|
171
|
118 return '{0} = {2} {1} {3}'.format(self.result, self.operation, self.value1, self.value2)
|
70
|
119
|
170
|
120 # Memory functions:
|
171
|
121 class Load(Instruction):
|
174
|
122 def __init__(self, location, value):
|
171
|
123 super().__init__()
|
174
|
124 assert type(value) is Value
|
|
125 assert type(location) is Value, "Location must be a value"
|
157
|
126 self.value = value
|
174
|
127 self.addDef(value)
|
|
128 self.location = location
|
|
129 self.addUse(self.location)
|
158
|
130 def __repr__(self):
|
174
|
131 return '{1} = [{0}]'.format(self.location, self.value)
|
157
|
132
|
171
|
133 class Store(Instruction):
|
174
|
134 def __init__(self, location, value):
|
171
|
135 super().__init__()
|
174
|
136 assert type(value) is Value
|
|
137 assert type(location) is Value, "Location must be a value"
|
|
138 self.location = location
|
160
|
139 self.value = value
|
174
|
140 self.addUse(value)
|
175
|
141 self.addUse(location)
|
160
|
142 def __repr__(self):
|
174
|
143 return '[{0}] = {1}'.format(self.location, self.value)
|
160
|
144
|
171
|
145 # Branching:
|
|
146 class Branch(Instruction):
|
170
|
147 def __init__(self, target):
|
171
|
148 super().__init__()
|
|
149 assert type(target) is BasicBlock
|
|
150 self.target = target
|
158
|
151 def __repr__(self):
|
171
|
152 return 'BRANCH {0}'.format(self.target)
|
173
|
153 def getTargets(self):
|
|
154 return [self.target]
|
170
|
155
|
171
|
156 class ConditionalBranch(Instruction):
|
|
157 def __init__(self, a, cond, b, lab1, lab2):
|
|
158 super().__init__()
|
|
159 self.a = a
|
|
160 assert type(a) is Value
|
170
|
161 self.cond = cond
|
171
|
162 self.b = b
|
174
|
163 self.addUse(a)
|
|
164 self.addUse(b)
|
171
|
165 assert type(b) is Value
|
|
166 assert type(lab1) is BasicBlock
|
170
|
167 self.lab1 = lab1
|
171
|
168 assert type(lab2) is BasicBlock
|
170
|
169 self.lab2 = lab2
|
|
170 def __repr__(self):
|
171
|
171 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
|
173
|
172 def getTargets(self):
|
|
173 return [self.lab1, self.lab2]
|
170
|
174
|
171
|
175 class PhiNode(Instruction):
|
|
176 def __init__(self):
|
|
177 super().__init__()
|
|
178 self.incBB = []
|
|
179 def addIncoming(self, bb):
|
|
180 self.incBB.append(bb)
|
|
181
|