annotate python/ppci/codegen/canon.py @ 334:6f4753202b9a

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