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