Mercurial > lcfOS
annotate python/ppci/codegen/canon.py @ 354:5477e499b039
Added some sort of string functionality
author | Windel Bouwman |
---|---|
date | Thu, 13 Mar 2014 18:59:06 +0100 |
parents | e609d5296ee9 |
children | c49459768aaa |
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 | |
354 | 67 elif isinstance(exp, ir.Addr): |
68 exp.e = rewriteExp(exp.e, frame) | |
69 return exp | |
275 | 70 elif isinstance(exp, ir.Call): |
71 exp.arguments = [rewriteExp(p, frame) for p in exp.arguments] | |
72 # Rewrite call into eseq: | |
73 t = newTemp() | |
74 return ir.Eseq(ir.Move(t, exp), t) | |
75 else: | |
354 | 76 raise NotImplementedError('NI: {}, {}'.format(exp, type(exp))) |
275 | 77 |
78 # The flatten functions pull out seq instructions to the sequence list. | |
79 | |
80 def flattenExp(exp): | |
81 if isinstance(exp, ir.Binop): | |
82 exp.a, sa = flattenExp(exp.a) | |
83 exp.b, sb = flattenExp(exp.b) | |
84 return exp, sa + sb | |
85 elif isinstance(exp, ir.Temp): | |
86 return exp, [] | |
87 elif isinstance(exp, ir.Const): | |
88 return exp, [] | |
89 elif isinstance(exp, ir.Mem): | |
90 exp.e, s = flattenExp(exp.e) | |
91 return exp, s | |
354 | 92 elif isinstance(exp, ir.Addr): |
93 exp.e, s = flattenExp(exp.e) | |
94 return exp, s | |
275 | 95 elif isinstance(exp, ir.Eseq): |
96 s = flattenStmt(exp.stmt) | |
97 exp.e, se = flattenExp(exp.e) | |
98 return exp.e, s + se | |
99 elif isinstance(exp, ir.Call): | |
100 sp = [] | |
101 p = [] | |
102 for p_, sp_ in (flattenExp(p) for p in exp.arguments): | |
103 p.append(p_) | |
104 sp.extend(sp_) | |
105 exp.arguments = p | |
106 return exp, sp | |
107 else: | |
108 raise NotImplementedError('NI: {}'.format(exp)) | |
109 | |
300 | 110 |
275 | 111 def flattenStmt(stmt): |
112 if isinstance(stmt, ir.Jump): | |
113 return [stmt] | |
114 elif isinstance(stmt, ir.CJump): | |
115 stmt.a, sa = flattenExp(stmt.a) | |
116 stmt.b, sb = flattenExp(stmt.b) | |
117 return sa + sb + [stmt] | |
118 elif isinstance(stmt, ir.Move): | |
119 stmt.dst, sd = flattenExp(stmt.dst) | |
120 stmt.src, ss = flattenExp(stmt.src) | |
121 return sd + ss + [stmt] | |
122 elif isinstance(stmt, ir.Terminator): | |
123 return [stmt] | |
124 elif isinstance(stmt, ir.Exp): | |
125 stmt.e, se = flattenExp(stmt.e) | |
126 return se + [stmt] | |
127 else: | |
128 raise NotImplementedError('STMT NI: {}'.format(stmt)) | |
129 | |
130 | |
131 def linearize(block): | |
132 """ | |
133 Move seq instructions to top and flatten these in an instruction list | |
134 """ | |
135 i = list(flattenStmt(s) for s in block.instructions) | |
136 block.instructions = list(chain.from_iterable(i)) |