171
|
1 from .basicblock import BasicBlock
|
|
2
|
|
3 class Value:
|
|
4 """ Temporary SSA value (value that is assigned only once! """
|
|
5 def __init__(self, name):
|
|
6 # TODO: add typing? for now only handle integers
|
|
7 self.name = name
|
|
8 self.interferes = set()
|
|
9 self.reg = None
|
174
|
10 self.used_by = []
|
171
|
11 def __repr__(self):
|
|
12 if self.reg:
|
|
13 n = self.reg
|
|
14 else:
|
|
15 n = self.name
|
174
|
16 return '{0}'.format(n) # + str(self.IsUsed)
|
|
17 @property
|
|
18 def IsUsed(self):
|
|
19 return len(self.used_by) > 0
|
|
20
|
|
21 class Use:
|
|
22 def __init__(self, user, val):
|
|
23 self.user = user
|
|
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):
|
|
39 for use in self.uses:
|
|
40 use.delete()
|
|
41 self.uses.clear()
|
|
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)
|
173
|
51 @property
|
|
52 def Targets(self):
|
|
53 return self.getTargets()
|
170
|
54
|
158
|
55 # Function calling:
|
171
|
56 class Call(Instruction):
|
110
|
57 def __init__(self, callee, arguments):
|
|
58 super().__init__()
|
|
59 self.callee = callee
|
|
60 self.arguments = arguments
|
158
|
61 def __repr__(self):
|
|
62 return 'CALL {0}'.format(self.callee)
|
110
|
63
|
171
|
64 class Return(Instruction):
|
174
|
65 def __init__(self, value=None):
|
|
66 super().__init__()
|
|
67 self.value = value
|
|
68 if value:
|
|
69 self.addUse(value)
|
158
|
70 def __repr__(self):
|
174
|
71 if self.value:
|
|
72 return 'Return {0}'.format(self.value)
|
|
73 else:
|
|
74 return 'Return'
|
173
|
75 def getTargets(self):
|
|
76 return []
|
70
|
77
|
174
|
78 class Alloc(Instruction):
|
|
79 """ Allocates space on the stack """
|
|
80 def __init__(self, value):
|
|
81 super().__init__()
|
|
82 self.value = value
|
|
83 self.addDef(value)
|
|
84 def __repr__(self):
|
|
85 return '{0} = alloc'.format(self.value)
|
|
86
|
171
|
87 class ImmLoad(Instruction):
|
|
88 def __init__(self, target, value):
|
|
89 super().__init__()
|
|
90 self.target = target
|
170
|
91 self.value = value
|
174
|
92 self.addDef(target)
|
170
|
93 def __repr__(self):
|
171
|
94 return '{0} = {1}'.format(self.target, self.value)
|
170
|
95
|
171
|
96 # Data operations
|
70
|
97 class BinaryOperator(Instruction):
|
171
|
98 def __init__(self, result, operation, value1, value2):
|
|
99 super().__init__()
|
157
|
100 #print('operation is in binops:', operation in BinOps)
|
104
|
101 # Check types of the two operands:
|
171
|
102 self.result = result
|
174
|
103 self.addDef(result)
|
104
|
104 self.value1 = value1
|
|
105 self.value2 = value2
|
174
|
106 self.addUse(value1)
|
|
107 self.addUse(value2)
|
104
|
108 self.operation = operation
|
158
|
109 def __repr__(self):
|
171
|
110 return '{0} = {2} {1} {3}'.format(self.result, self.operation, self.value1, self.value2)
|
70
|
111
|
170
|
112 # Memory functions:
|
171
|
113 class Load(Instruction):
|
174
|
114 def __init__(self, location, value):
|
171
|
115 super().__init__()
|
174
|
116 assert type(value) is Value
|
|
117 assert type(location) is Value, "Location must be a value"
|
157
|
118 self.value = value
|
174
|
119 self.addDef(value)
|
|
120 self.location = location
|
|
121 self.addUse(self.location)
|
158
|
122 def __repr__(self):
|
174
|
123 return '{1} = [{0}]'.format(self.location, self.value)
|
157
|
124
|
171
|
125 class Store(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"
|
|
130 self.location = location
|
160
|
131 self.value = value
|
174
|
132 self.addUse(value)
|
160
|
133 def __repr__(self):
|
174
|
134 return '[{0}] = {1}'.format(self.location, self.value)
|
160
|
135
|
171
|
136 # Branching:
|
|
137 class Branch(Instruction):
|
170
|
138 def __init__(self, target):
|
171
|
139 super().__init__()
|
|
140 assert type(target) is BasicBlock
|
|
141 self.target = target
|
158
|
142 def __repr__(self):
|
171
|
143 return 'BRANCH {0}'.format(self.target)
|
173
|
144 def getTargets(self):
|
|
145 return [self.target]
|
170
|
146
|
171
|
147 class ConditionalBranch(Instruction):
|
|
148 def __init__(self, a, cond, b, lab1, lab2):
|
|
149 super().__init__()
|
|
150 self.a = a
|
|
151 assert type(a) is Value
|
170
|
152 self.cond = cond
|
171
|
153 self.b = b
|
174
|
154 self.addUse(a)
|
|
155 self.addUse(b)
|
171
|
156 assert type(b) is Value
|
|
157 assert type(lab1) is BasicBlock
|
170
|
158 self.lab1 = lab1
|
171
|
159 assert type(lab2) is BasicBlock
|
170
|
160 self.lab2 = lab2
|
|
161 def __repr__(self):
|
171
|
162 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
|
173
|
163 def getTargets(self):
|
|
164 return [self.lab1, self.lab2]
|
170
|
165
|
171
|
166 class PhiNode(Instruction):
|
|
167 def __init__(self):
|
|
168 super().__init__()
|
|
169 self.incBB = []
|
|
170 def addIncoming(self, bb):
|
|
171 self.incBB.append(bb)
|
|
172
|