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