annotate python/c3/codegenerator.py @ 217:8b2e5f3cd579

Removed some stale python source files
author Windel Bouwman
date Fri, 05 Jul 2013 14:13:59 +0200
parents d77cb5962cc5
children 3f6c30a5d234
rev   line source
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
1 import ir
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
2 from . import astnodes
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
3 from .scope import boolType
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
4
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
5 class CodeGenerator:
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
6 """ Generates intermediate code from a package """
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
7 def gencode(self, pkg):
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
8 assert type(pkg) is astnodes.Package
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
9 self.varMap = {} # Maps variables to storage locations.
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
10 self.funcMap = {}
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
11 self.builder = ir.Builder()
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
12 m = ir.Module(pkg.name)
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
13 self.builder.setModule(m)
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
14 self.genModule(pkg)
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
15 return m
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
16
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
17 # inner helpers:
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
18 def genModule(self, pkg):
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
19 # Take care of forward declarations:
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
20 for s in pkg.innerScope.Functions:
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 186
diff changeset
21 f = self.builder.newFunction(s.name)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
22 self.funcMap[s] = f
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
23 for s in pkg.innerScope:
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
24 if type(s) is astnodes.Variable:
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
25 v = self.builder.newVariable(s.name)
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 177
diff changeset
26 #self.builder.addIns(ir.Alloc(v))
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 177
diff changeset
27 self.varMap[s] = v
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
28 elif type(s) is astnodes.Function:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
29 # TODO: handle arguments
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
30 f = self.funcMap[s]
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 186
diff changeset
31 self.builder.setFunction(f)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
32 bb = self.builder.newBB()
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
33 f.entry = bb
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
34 self.builder.setBB(bb)
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
35 # generate room for locals:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
36
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
37 for sym in s.innerScope:
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
38 #print(sym, sym.isParameter)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
39 # TODO: handle parameters different
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
40 v = self.builder.newTmp(sym.name)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
41 self.builder.addIns(ir.Alloc(v))
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
42 self.varMap[sym] = v
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
43
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
44 self.genCode(s.body)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
45 # TODO handle return?
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
46 self.builder.addIns(ir.Return())
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 186
diff changeset
47 self.builder.setFunction(None)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
48 else:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
49 print(s)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
50
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
51 def genCode(self, code):
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
52 if type(code) is astnodes.CompoundStatement:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
53 for s in code.statements:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
54 self.genCode(s)
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
55 elif type(code) is astnodes.Assignment:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
56 re = self.genExprCode(code.rval)
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
57 loc = self.varMap[code.lval.target]
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
58 self.builder.addIns(ir.Store(loc, re))
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
59 elif type(code) is astnodes.IfStatement:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
60 bbtrue = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
61 bbfalse = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
62 te = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
63 self.genCondCode(code.condition, bbtrue, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
64 self.builder.setBB(bbtrue)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
65 self.genCode(code.truestatement)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
66 self.builder.addIns(ir.Branch(te))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
67 self.builder.setBB(bbfalse)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
68 self.genCode(code.falsestatement)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
69 self.builder.addIns(ir.Branch(te))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
70 self.builder.setBB(te)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
71 elif type(code) is astnodes.FunctionCall:
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
72 print('TODO')
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
73 pass
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
74 elif type(code) is astnodes.EmptyStatement:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
75 pass
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
76 elif type(code) is astnodes.ReturnStatement:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
77 if code.expr:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
78 re = self.genExprCode(code.expr)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
79 self.builder.addIns(ir.Return(re))
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
80 else:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
81 self.builder.addIns(ir.Return())
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
82 elif type(code) is astnodes.WhileStatement:
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
83 bbdo = self.builder.newBB()
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
84 bbtest = self.builder.newBB()
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
85 te = self.builder.newBB()
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
86 self.builder.addIns(ir.Branch(bbtest))
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
87 self.builder.setBB(bbtest)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
88 self.genCondCode(code.condition, bbdo, te)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
89 self.builder.setBB(bbdo)
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
90 self.genCode(code.statement)
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
91 self.builder.addIns(ir.Branch(bbtest))
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
92 self.builder.setBB(te)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
93 else:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
94 print('Unknown stmt:', code)
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
95 def genCondCode(self, expr, bbtrue, bbfalse):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
96 # Implement sequential logical operators
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
97 assert expr.typ == boolType
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
98 if type(expr) is astnodes.Binop:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
99 if expr.op == 'or':
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
100 l2 = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
101 self.genCondCode(expr.a, bbtrue, l2)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
102 self.builder.setBB(l2)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
103 self.genCondCode(expr.b, bbtrue, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
104 elif expr.op == 'and':
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
105 l2 = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
106 self.genCondCode(expr.a, l2, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
107 self.builder.setBB(l2)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
108 self.genCondCode(expr.b, bbtrue, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
109 elif expr.op in ['==', '>', '<']:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
110 ta = self.genExprCode(expr.a)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
111 tb = self.genExprCode(expr.b)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
112 i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
113 self.builder.addIns(i)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
114 else:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
115 raise NotImlementedError('Unknown condition {0}'.format(expr))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
116 elif type(expr) is astnodes.Literal:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
117 if expr.val:
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
118 self.builder.addIns(ir.Branch(bbtrue))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
119 else:
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
120 self.builder.addIns(ir.Branch(bbfalse))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
121 else:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
122 print('Unknown cond', expr)
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
123 def genExprCode(self, expr):
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
124 if type(expr) is astnodes.Binop:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
125 ra = self.genExprCode(expr.a)
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
126 rb = self.genExprCode(expr.b)
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
127 ops = ['+', '-', '*', '/']
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
128 if expr.op in ops:
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
129 tmpnames = {'+':'addtmp', '-':'subtmp', '*': 'multmp', '/':'divtmp'}
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
130 tmp = self.builder.newTmp(tmpnames[expr.op])
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
131 op = expr.op
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
132 ins = ir.BinaryOperator(tmp, op, ra, rb)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
133 self.builder.addIns(ins)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
134 return tmp
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
135 else:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
136 print('Unknown {0}'.format(expr))
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
137 tmp = self.builder.newTmp()
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
138 # TODO
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
139 return tmp
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
140 elif type(expr) is astnodes.Constant:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
141 tmp = self.builder.newTmp()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
142 # TODO
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
143 return tmp
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
144 elif type(expr) is astnodes.VariableUse:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
145 tmp = self.builder.newTmp()
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
146 loc = self.varMap[expr.target]
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
147 i = ir.Load(loc, tmp)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
148 self.builder.addIns(i)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
149 return tmp
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
150 elif type(expr) is astnodes.Literal:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
151 tmp = self.builder.newTmp()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
152 ins = ir.ImmLoad(tmp, expr.val)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
153 self.builder.addIns(ins)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
154 return tmp
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
155 elif type(expr) is astnodes.FunctionCall:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
156 tmp = self.builder.newTmp("res")
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
157 args = []
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
158 for arg in expr.args:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
159 ar = self.genExprCode(arg)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
160 args.append(ar)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
161 fn = self.funcMap[expr.proc]
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
162 ins = ir.Call(fn, args, tmp)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
163 self.builder.addIns(ins)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
164 return tmp
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
165 else:
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
166 print('Unknown expr:', code)
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
167