Mercurial > lcfOS
annotate python/ppci/codegen/canon.py @ 353:b8ad45b3a573
Started with strings
author | Windel Bouwman |
---|---|
date | Sun, 09 Mar 2014 18:49:10 +0100 |
parents | e609d5296ee9 |
children | 5477e499b039 |
rev | line source |
---|---|
301 | 1 from .. import ir |
307 | 2 from .. import irutils |
275 | 3 from itertools import chain |
4 | |
5 def make(function, frame): | |
6 """ | |
7 Create canonicalized version of the IR-code. This means: | |
8 - Calls out of expressions. | |
9 - Other things? | |
10 """ | |
11 # Change the tree. This modifies the IR-tree! | |
12 # Move all parameters into registers | |
13 parmoves = [] | |
14 for p in function.arguments: | |
15 pt = newTemp() | |
16 frame.parMap[p] = pt | |
17 parmoves.append(ir.Move(pt, frame.argLoc(p.num))) | |
18 function.entry.instructions = parmoves + function.entry.instructions | |
19 | |
20 for block in function.Blocks: | |
21 for stmt in block.instructions: | |
22 rewriteStmt(stmt, frame) | |
23 linearize(block) | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
24 # TODO: schedule here? |
275 | 25 |
26 # Visit all nodes with some function: | |
27 # TODO: rewrite into visitor. | |
28 | |
29 # Rewrite rewrites call instructions into Eseq instructions. | |
30 | |
300 | 31 |
275 | 32 def rewriteStmt(stmt, frame): |
33 if isinstance(stmt, ir.Jump): | |
34 pass | |
35 elif isinstance(stmt, ir.CJump): | |
36 stmt.a = rewriteExp(stmt.a, frame) | |
37 stmt.b = rewriteExp(stmt.b, frame) | |
38 elif isinstance(stmt, ir.Move): | |
39 stmt.src = rewriteExp(stmt.src, frame) | |
40 stmt.dst = rewriteExp(stmt.dst, frame) | |
41 elif isinstance(stmt, ir.Terminator): | |
42 pass | |
43 elif isinstance(stmt, ir.Exp): | |
44 stmt.e = rewriteExp(stmt.e, frame) | |
45 else: | |
46 raise NotImplementedError('STMT NI: {}'.format(stmt)) | |
47 | |
307 | 48 newTemp = irutils.NamedClassGenerator('canon_reg', ir.Temp).gen |
275 | 49 |
50 def rewriteExp(exp, frame): | |
51 if isinstance(exp, ir.Binop): | |
52 exp.a = rewriteExp(exp.a, frame) | |
53 exp.b = rewriteExp(exp.b, frame) | |
54 return exp | |
55 elif isinstance(exp, ir.Const): | |
56 return exp | |
57 elif isinstance(exp, ir.Temp): | |
58 return exp | |
59 elif isinstance(exp, ir.Parameter): | |
60 return frame.parMap[exp] | |
61 elif isinstance(exp, ir.LocalVariable): | |
62 offset = frame.allocVar(exp) | |
279 | 63 return ir.Add(frame.fp, ir.Const(offset)) |
275 | 64 elif isinstance(exp, ir.Mem): |
65 exp.e = rewriteExp(exp.e, frame) | |
66 return exp | |
67 elif isinstance(exp, ir.Call): | |
68 exp.arguments = [rewriteExp(p, frame) for p in exp.arguments] | |
69 # Rewrite call into eseq: | |
70 t = newTemp() | |
71 return ir.Eseq(ir.Move(t, exp), t) | |
72 else: | |
73 raise NotImplementedError('NI: {}'.format(exp)) | |
74 | |
75 # The flatten functions pull out seq instructions to the sequence list. | |
76 | |
77 def flattenExp(exp): | |
78 if isinstance(exp, ir.Binop): | |
79 exp.a, sa = flattenExp(exp.a) | |
80 exp.b, sb = flattenExp(exp.b) | |
81 return exp, sa + sb | |
82 elif isinstance(exp, ir.Temp): | |
83 return exp, [] | |
84 elif isinstance(exp, ir.Const): | |
85 return exp, [] | |
86 elif isinstance(exp, ir.Mem): | |
87 exp.e, s = flattenExp(exp.e) | |
88 return exp, s | |
89 elif isinstance(exp, ir.Eseq): | |
90 s = flattenStmt(exp.stmt) | |
91 exp.e, se = flattenExp(exp.e) | |
92 return exp.e, s + se | |
93 elif isinstance(exp, ir.Call): | |
94 sp = [] | |
95 p = [] | |
96 for p_, sp_ in (flattenExp(p) for p in exp.arguments): | |
97 p.append(p_) | |
98 sp.extend(sp_) | |
99 exp.arguments = p | |
100 return exp, sp | |
101 else: | |
102 raise NotImplementedError('NI: {}'.format(exp)) | |
103 | |
300 | 104 |
275 | 105 def flattenStmt(stmt): |
106 if isinstance(stmt, ir.Jump): | |
107 return [stmt] | |
108 elif isinstance(stmt, ir.CJump): | |
109 stmt.a, sa = flattenExp(stmt.a) | |
110 stmt.b, sb = flattenExp(stmt.b) | |
111 return sa + sb + [stmt] | |
112 elif isinstance(stmt, ir.Move): | |
113 stmt.dst, sd = flattenExp(stmt.dst) | |
114 stmt.src, ss = flattenExp(stmt.src) | |
115 return sd + ss + [stmt] | |
116 elif isinstance(stmt, ir.Terminator): | |
117 return [stmt] | |
118 elif isinstance(stmt, ir.Exp): | |
119 stmt.e, se = flattenExp(stmt.e) | |
120 return se + [stmt] | |
121 else: | |
122 raise NotImplementedError('STMT NI: {}'.format(stmt)) | |
123 | |
124 | |
125 def linearize(block): | |
126 """ | |
127 Move seq instructions to top and flatten these in an instruction list | |
128 """ | |
129 i = list(flattenStmt(s) for s in block.instructions) | |
130 block.instructions = list(chain.from_iterable(i)) |