annotate python/canon.py @ 275:6f2423df0675

Fixed serve arm-as
author Windel Bouwman
date Sat, 14 Sep 2013 17:29:10 +0200
parents
children 2ccd57b1d78c
rev   line source
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
1
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
2 import ir
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)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
24
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
25 # Visit all nodes with some function:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
26 # TODO: rewrite into visitor.
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
27
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
28 # Rewrite rewrites call instructions into Eseq instructions.
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
29
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
30 def rewriteStmt(stmt, frame):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
31 if isinstance(stmt, ir.Jump):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
32 pass
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
33 elif isinstance(stmt, ir.CJump):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
34 stmt.a = rewriteExp(stmt.a, frame)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
35 stmt.b = rewriteExp(stmt.b, frame)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
36 elif isinstance(stmt, ir.Move):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
37 stmt.src = rewriteExp(stmt.src, frame)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
38 stmt.dst = rewriteExp(stmt.dst, frame)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
39 elif isinstance(stmt, ir.Terminator):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
40 pass
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
41 elif isinstance(stmt, ir.Exp):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
42 stmt.e = rewriteExp(stmt.e, frame)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
43 else:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
44 raise NotImplementedError('STMT NI: {}'.format(stmt))
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
45
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
46 newTemp = ir.NamedClassGenerator('canon_reg', ir.Temp).gen
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
47
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
48 def rewriteExp(exp, frame):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
49 if isinstance(exp, ir.Binop):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
50 exp.a = rewriteExp(exp.a, frame)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
51 exp.b = rewriteExp(exp.b, frame)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
52 return exp
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
53 elif isinstance(exp, ir.Const):
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.Temp):
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.Parameter):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
58 return frame.parMap[exp]
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
59 elif isinstance(exp, ir.LocalVariable):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
60 offset = frame.allocVar(exp)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
61 return ir.Mem(ir.Binop(frame.fp, '+', ir.Const(offset)))
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
62 elif isinstance(exp, ir.Mem):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
63 exp.e = rewriteExp(exp.e, frame)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
64 return exp
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
65 elif isinstance(exp, ir.Call):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
66 exp.arguments = [rewriteExp(p, frame) for p in exp.arguments]
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
67 # Rewrite call into eseq:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
68 t = newTemp()
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
69 return ir.Eseq(ir.Move(t, exp), t)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
70 else:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
71 raise NotImplementedError('NI: {}'.format(exp))
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
72
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
73 # The flatten functions pull out seq instructions to the sequence list.
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
74
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
75 def flattenExp(exp):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
76 if isinstance(exp, ir.Binop):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
77 exp.a, sa = flattenExp(exp.a)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
78 exp.b, sb = flattenExp(exp.b)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
79 return exp, sa + sb
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
80 elif isinstance(exp, ir.Temp):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
81 return exp, []
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
82 elif isinstance(exp, ir.Const):
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.Mem):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
85 exp.e, s = flattenExp(exp.e)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
86 return exp, s
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
87 elif isinstance(exp, ir.Eseq):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
88 s = flattenStmt(exp.stmt)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
89 exp.e, se = flattenExp(exp.e)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
90 return exp.e, s + se
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
91 elif isinstance(exp, ir.Call):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
92 sp = []
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
93 p = []
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
94 for p_, sp_ in (flattenExp(p) for p in exp.arguments):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
95 p.append(p_)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
96 sp.extend(sp_)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
97 exp.arguments = p
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
98 return exp, sp
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
99 else:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
100 raise NotImplementedError('NI: {}'.format(exp))
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
101
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
102 def flattenStmt(stmt):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
103 if isinstance(stmt, ir.Jump):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
104 return [stmt]
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
105 elif isinstance(stmt, ir.CJump):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
106 stmt.a, sa = flattenExp(stmt.a)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
107 stmt.b, sb = flattenExp(stmt.b)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
108 return sa + sb + [stmt]
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
109 elif isinstance(stmt, ir.Move):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
110 stmt.dst, sd = flattenExp(stmt.dst)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
111 stmt.src, ss = flattenExp(stmt.src)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
112 return sd + ss + [stmt]
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
113 elif isinstance(stmt, ir.Terminator):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
114 return [stmt]
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
115 elif isinstance(stmt, ir.Exp):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
116 stmt.e, se = flattenExp(stmt.e)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
117 return se + [stmt]
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
118 else:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
119 raise NotImplementedError('STMT NI: {}'.format(stmt))
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
120
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
121
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
122 def linearize(block):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
123 """
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
124 Move seq instructions to top and flatten these in an instruction list
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
125 """
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
126 i = list(flattenStmt(s) for s in block.instructions)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
127 block.instructions = list(chain.from_iterable(i))
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents:
diff changeset
128