annotate python/ppci/ir.py @ 310:e95e5572cd6d

Added utils doc page
author Windel Bouwman
date Fri, 13 Dec 2013 14:10:10 +0100
parents 68b01c8abf8a
children 2c9768114877
rev   line source
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
1 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
2 Intermediate representation (IR) code classes.
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
3 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
4
310
e95e5572cd6d Added utils doc page
Windel Bouwman
parents: 309
diff changeset
5
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
6 class Module:
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
7 """ Container unit for variables and functions. """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
8 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
9 self.name = name
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
10 self.functions = []
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
11 self.variables = []
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
12
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
13 def __repr__(self):
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
14 return 'module {0}'.format(self.name)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
15
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
16 def add_function(self, f):
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
17 """ Add a function to this module """
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
18 self.functions.append(f)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
19
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
20 def addVariable(self, v):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
21 self.variables.append(v)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
22
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
23 def getVariables(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
24 return self.variables
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
25
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
26 Variables = property(getVariables)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
27
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
28 def getFunctions(self):
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
29 return self.functions
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
30
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
31 Functions = property(getFunctions)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
32
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
33 def findFunction(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
34 for f in self.funcs:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
35 if f.name == name:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
36 return f
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
37 raise KeyError(name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
38
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
39 getFunction = findFunction
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
40
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
41 # Analysis functions:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
42 def check(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
43 """ Perform sanity check on module """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
44 for f in self.Functions:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
45 f.check()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
46
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
47
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
48 class Function:
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
49 """ Represents a function. """
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
50 def __init__(self, name, module=None):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
51 self.name = name
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
52 self.entry = Block('{}_entry'.format(name))
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
53 self.entry.function = self
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
54 self.epiloog = Block('{}_epilog'.format(name))
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
55 self.epiloog.function = self
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
56 self.epiloog.addInstruction(Terminator())
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
57 self.return_value = Temp('{}_retval'.format(name))
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
58 self.arguments = []
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
59 self.localvars = []
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
60 if module:
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
61 module.add_function(self)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
62
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
63 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
64 args = ','.join(str(a) for a in self.arguments)
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
65 return 'function i32 {}({})'.format(self.name, args)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
66
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
67 def add_block(self, bb):
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
68 #self.bbs.append(bb)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
69 bb.function = self
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
70
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
71 def removeBlock(self, bb):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
72 #self.bbs.remove(bb)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
73 bb.function = None
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
74
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
75 def getBlocks(self):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
76 bbs = [self.entry]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
77 worklist = [self.entry]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
78 while worklist:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
79 b = worklist.pop()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
80 for sb in b.Successors:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
81 if sb not in bbs:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
82 bbs.append(sb)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
83 worklist.append(sb)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
84 bbs.remove(self.entry)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
85 if self.epiloog in bbs:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
86 bbs.remove(self.epiloog)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
87 bbs.insert(0, self.entry)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
88 bbs.append(self.epiloog)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
89 return bbs
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
90
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
91 def findBasicBlock(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
92 for bb in self.bbs:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
93 if bb.name == name:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
94 return bb
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
95 raise KeyError(name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
96
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
97 Blocks = property(getBlocks)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
98
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
99 @property
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
100 def Entry(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
101 return self.entry
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
102
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
103 def check(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
104 for b in self.Blocks:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
105 b.check()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
106
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
107 def addParameter(self, p):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
108 assert type(p) is Parameter
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
109 p.num = len(self.arguments)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
110 self.arguments.append(p)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
111
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
112 def addLocal(self, l):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
113 assert type(l) is LocalVariable
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
114 self.localvars.append(l)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
115
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
116
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
117 class Block:
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
118 """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
119 Uninterrupted sequence of instructions with a label at the start.
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
120 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
121 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
122 self.name = name
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
123 self.function = None
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
124 self.instructions = []
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
125
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
126 parent = property(lambda s: s.function)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
127
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
128 def __repr__(self):
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
129 return '{0}:'.format(self.name)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
130
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
131 def addInstruction(self, i):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
132 i.parent = self
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
133 assert not isinstance(self.LastInstruction, LastStatement)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
134 self.instructions.append(i)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
135
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
136 def replaceInstruction(self, i1, i2):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
137 idx = self.instructions.index(i1)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
138 i1.parent = None
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
139 i1.delete()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
140 i2.parent = self
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
141 self.instructions[idx] = i2
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
142
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
143 def removeInstruction(self, i):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
144 i.parent = None
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
145 i.delete()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
146 self.instructions.remove(i)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
147
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
148 @property
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
149 def Instructions(self):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
150 return self.instructions
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
151
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
152 @property
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
153 def LastInstruction(self):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
154 if not self.Empty:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
155 return self.instructions[-1]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
156
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
157 @property
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
158 def Empty(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
159 return len(self.instructions) == 0
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
160
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
161 @property
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
162 def FirstInstruction(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
163 return self.instructions[0]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
164
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
165 def getSuccessors(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
166 if not self.Empty:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
167 return self.LastInstruction.Targets
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
168 return []
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
169 Successors = property(getSuccessors)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
170
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
171 def getPredecessors(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
172 preds = []
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
173 for bb in self.parent.Blocks:
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
174 if self in bb.Successors:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
175 preds.append(bb)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
176 return preds
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
177 Predecessors = property(getPredecessors)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
178
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
179 def precedes(self, other):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
180 raise NotImplementedError()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
181
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
182 def check(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
183 assert isinstance(self.LastInstruction, LastStatement)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
184 for i in self.instructions[:-1]:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
185 assert not isinstance(i, LastStatement)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
186
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
187
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
188 # Instructions:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
189 class Term:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
190 def __init__(self, x):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
191 self.x = x
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
192
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
193
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
194 def match_tree(tree, pattern):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
195 if type(pattern) is Term:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
196 return True, {pattern: tree}
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
197 elif type(pattern) is Binop and type(tree) is Binop and \
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
198 tree.operation == pattern.operation:
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
199 res_a, mp_a = match_tree(tree.a, pattern.a)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
200 res_b, mp_b = match_tree(tree.b, pattern.b)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
201 assert not (mp_a.keys() & mp_b.keys())
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
202 mp_a.update(mp_b)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
203 return res_a and res_b, mp_a
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
204 elif type(pattern) is Const and type(tree) is Const and \
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
205 pattern.value == tree.value:
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
206 return True, {}
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
207 else:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
208 return False, {}
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
209
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
210
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
211 class Expression:
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
212 """ Base class for an expression """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
213 pass
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
214
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
215
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
216 class Const(Expression):
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
217 """ Represents a constant value """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
218 def __init__(self, value):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
219 self.value = value
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
220
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
221 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
222 return 'Const {}'.format(self.value)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
223
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
224
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
225 class Call(Expression):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
226 """ Call a function with some arguments """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
227 def __init__(self, f, arguments):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
228 self.f = f
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
229 self.arguments = arguments
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
230
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
231 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
232 args = ', '.join([str(arg) for arg in self.arguments])
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
233 return '{}({})'.format(self.f, args)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
234
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
235
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
236 # Data operations
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
237 class Binop(Expression):
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
238 """ Generic binary operation """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
239 ops = ['+', '-', '*', '/', '|', '&', '<<', '>>']
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
240
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
241 def __init__(self, value1, operation, value2):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
242 assert operation in Binop.ops
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
243 self.a = value1
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
244 self.b = value2
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
245 self.operation = operation
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
246
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
247 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
248 a, b = self.a, self.b
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
249 return '({} {} {})'.format(a, self.operation, b)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
250
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
251
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
252 def Add(a, b):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
253 """ Add a and b """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
254 return Binop(a, '+', b)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
255
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 280
diff changeset
256
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
257 def Sub(a, b):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
258 """ Substract b from a """
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
259 return Binop(a, '-', b)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
260
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 280
diff changeset
261
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
262 def Mul(a, b):
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
263 """ Multiply a by b """
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
264 return Binop(a, '*', b)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
265
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 280
diff changeset
266
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
267 def Div(a, b):
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
268 """ Divide a in b pieces """
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
269 return Binop(a, '/', b)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
270
303
be7f60545368 Final fixups
Windel Bouwman
parents: 301
diff changeset
271
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
272 class Eseq(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
273 """ Sequence of instructions where the last is an expression """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
274 def __init__(self, stmt, e):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
275 self.stmt = stmt
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
276 self.e = e
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
278 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
279 return '({}, {})'.format(self.stmt, self.e)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
280
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
281
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
282 class Alloc(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
283 """ Allocates space on the stack """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
284 def __init__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
285 super().__init__()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
286
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
287 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
288 return 'Alloc'
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
289
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
290
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
291 class Variable(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
292 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
293 self.name = name
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
294
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
295 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
296 return 'Var {}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
297
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
298
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
299 class LocalVariable(Variable):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
300 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
301 return 'Local {}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
302
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
303
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
304 class Parameter(Variable):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
305 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
306 return 'Param {}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
307
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
308
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
309 class Temp(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
310 """ Temporary storage, same as register """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
311 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
312 self.name = name
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
313
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
314 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
315 return 'TMP_{}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
316
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
317
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
318 class Mem(Expression):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
319 """ Memory access """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
320 def __init__(self, e):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
321 self.e = e
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
322
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
323 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
324 return '[{}]'.format(self.e)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
325
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
326
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
327 class Statement:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
328 """ Base class for all instructions. """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
329 pass
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
330
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
331
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
332 class Move(Statement):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
333 """ Move source to destination """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
334 def __init__(self, dst, src):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
335 self.dst = dst
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
336 self.src = src
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
337
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
338 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
339 return '{} = {}'.format(self.dst, self.src)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
340
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
341
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
342 class Exp(Statement):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
343 def __init__(self, e):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
344 self.e = e
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
345
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
346 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
347 return '{}'.format(self.e)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
348
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
349
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
350 # Branching:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
351 class LastStatement(Statement):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
352 def changeTarget(self, old, new):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
353 idx = self.Targets.index(old)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
354 self.Targets[idx] = new
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
355
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
356
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
357 class Terminator(LastStatement):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
358 """ Instruction that terminates the terminal block """
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
359 def __init__(self):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
360 self.Targets = []
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
361
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
362 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
363 return 'Terminator'
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
364
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
365
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
366 class Jump(LastStatement):
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
367 """ Jump statement to some target location """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
368 def __init__(self, target):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
369 self.Targets = [target]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
370
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
371 def setTarget(self, t):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
372 self.Targets[0] = t
310
e95e5572cd6d Added utils doc page
Windel Bouwman
parents: 309
diff changeset
373
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
374 target = property(lambda s: s.Targets[0], setTarget)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
375
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
376 def __repr__(self):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
377 return 'JUMP {}'.format(self.target.name)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
378
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
379
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
380 class CJump(LastStatement):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
381 """ Conditional jump to true or false labels. """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
382 conditions = ['==', '<', '>', '>=', '<=', '!=']
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
383
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
384 def __init__(self, a, cond, b, lab_yes, lab_no):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
385 assert cond in CJump.conditions
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
386 self.a = a
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
387 self.cond = cond
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
388 self.b = b
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
389 self.Targets = [lab_yes, lab_no]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
390
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
391 lab_yes = property(lambda s: s.Targets[0])
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
392 lab_no = property(lambda s: s.Targets[1])
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
393
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
394 def __repr__(self):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
395 return 'IF {} {} {} THEN {} ELSE {}'\
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
396 .format(self.a, self.cond, self.b, self.lab_yes, self.lab_no)