annotate python/ppci/codegen/canon.py @ 368:d2ddfe134c48

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