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