annotate python/c3/codegenerator.py @ 268:5ec7580976d9

Op naar tree-IR
author Windel Bouwman
date Wed, 14 Aug 2013 20:12:40 +0200
parents 444b9df2ed99
children 5f8c04a8d26b
rev   line source
255
7416c923a02a Added more logging
Windel Bouwman
parents: 252
diff changeset
1 import logging
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
2 import ir
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
3 from . import astnodes
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
4 from .scope import boolType, intType
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
5 from ppci import CompilerError
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 230
diff changeset
6 from .typecheck import theType
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
7
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
8
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
9 class CodeGenerator(ir.Builder):
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
10 """ Generates intermediate code from a package """
255
7416c923a02a Added more logging
Windel Bouwman
parents: 252
diff changeset
11 def __init__(self):
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
12 self.logger = logging.getLogger('c3cgen')
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
13
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
14 def gencode(self, pkg):
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
15 self.prepare()
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
16 assert type(pkg) is astnodes.Package
255
7416c923a02a Added more logging
Windel Bouwman
parents: 252
diff changeset
17 self.logger.info('Generating ir-code for {}'.format(pkg.name))
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
18 self.varMap = {} # Maps variables to storage locations.
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
19 self.funcMap = {}
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
20 self.m = ir.Module(pkg.name)
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
21 self.genModule(pkg)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
22 return self.m
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
23
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
24 # inner helpers:
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
25 def genModule(self, pkg):
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
26 # Take care of forward declarations:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
27 for s in pkg.innerScope.Functions:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
28 f = self.newFunction(s.name)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
29 self.funcMap[s] = f
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
30 for s in pkg.innerScope:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
31 if type(s) is astnodes.Variable:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
32 #v = self.builder.newTmp(s.name)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
33 self.varMap[s] = self.newTemp()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
34 pass
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
35 elif type(s) is astnodes.Function:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
36 self.genFunction(s)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
37 else:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
38 raise NotImplementedError(str(s))
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
39
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
40 def genFunction(self, fn):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
41 # TODO: handle arguments
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
42 f = self.funcMap[fn]
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
43 self.setFunction(f)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
44 # generate room for locals:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
45
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
46 for sym in fn.innerScope:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
47 # TODO: handle parameters different
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
48 v = self.newTemp()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
49 # TODO: make this ssa here??
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
50 self.varMap[sym] = v
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
51
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
52 self.genCode(fn.body)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
53 # TODO handle return?
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
54 self.emit(ir.Return(ir.Const(0)))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
55 self.setFunction(None)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
56
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
57 def genCode(self, code):
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
58 assert isinstance(code, astnodes.Statement)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
59 self.setLoc(code.loc)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
60 if type(code) is astnodes.CompoundStatement:
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
61 for s in code.statements:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
62 self.genCode(s)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
63 elif type(code) is astnodes.Assignment:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
64 rval = self.genExprCode(code.rval)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
65 lval = self.genExprCode(code.lval)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
66 self.emit(ir.Move(lval, rval))
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
67 elif type(code) is astnodes.ExpressionStatement:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
68 self.genExprCode(code.ex)
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
69 elif type(code) is astnodes.IfStatement:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
70 bbtrue = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
71 bbfalse = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
72 te = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
73 self.genCondCode(code.condition, bbtrue, bbfalse)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
74 self.setBlock(bbtrue)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
75 self.genCode(code.truestatement)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
76 self.emit(ir.Jump(te))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
77 self.setBlock(bbfalse)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
78 if code.falsestatement:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
79 self.genCode(code.falsestatement)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
80 self.emit(ir.Jump(te))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
81 self.setBlock(te)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
82 elif type(code) is astnodes.ReturnStatement:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
83 if code.expr:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
84 re = self.genExprCode(code.expr)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
85 self.emit(ir.Return(re))
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
86 else:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
87 self.builder.addIns(ir.Return())
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
88 elif type(code) is astnodes.WhileStatement:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
89 bbdo = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
90 bbtest = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
91 te = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
92 self.emit(ir.Jump(bbtest))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
93 self.setBlock(bbtest)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
94 self.genCondCode(code.condition, bbdo, te)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
95 self.setBlock(bbdo)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
96 self.genCode(code.statement)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
97 self.emit(ir.Jump(bbtest))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
98 self.setBlock(te)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
99 else:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
100 raise NotImplementedError('Unknown stmt {}'.format(code))
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
101
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
102 def genCondCode(self, expr, bbtrue, bbfalse):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
103 # Implement sequential logical operators
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
104 assert expr.typ == boolType
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
105 if type(expr) is astnodes.Binop:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
106 if expr.op == 'or':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
107 l2 = self.newBlock()
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
108 self.genCondCode(expr.a, bbtrue, l2)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
109 self.setBlock(l2)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
110 self.genCondCode(expr.b, bbtrue, bbfalse)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
111 elif expr.op == 'and':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
112 l2 = self.newBlock()
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
113 self.genCondCode(expr.a, l2, bbfalse)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
114 self.setBlock(l2)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
115 self.genCondCode(expr.b, bbtrue, bbfalse)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
116 elif expr.op in ['==', '>', '<']:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
117 ta = self.genExprCode(expr.a)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
118 tb = self.genExprCode(expr.b)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
119 self.emit(ir.CJump(ta, expr.op, tb, bbtrue, bbfalse))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
120 else:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
121 raise NotImplementedError('Unknown condition {}'.format(expr))
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
122 elif type(expr) is astnodes.Literal:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
123 if expr.val:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
124 self.emit(ir.Jump(bbtrue))
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
125 else:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
126 self.emit(ir.Jump(bbfalse))
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
127 else:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
128 raise NotImplementedError('Unknown cond {}'.format(expr))
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
129
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
130 def genExprCode(self, expr):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
131 assert isinstance(expr, astnodes.Expression)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
132 if type(expr) is astnodes.Binop and expr.op in ir.Binop.ops:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
133 ra = self.genExprCode(expr.a)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
134 rb = self.genExprCode(expr.b)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
135 return ir.Binop(ra, expr.op, rb)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
136 elif type(expr) is astnodes.Unop and expr.op == '&':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
137 ra = self.genExprCode(expr.a)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
138 # TODO: Make this work?
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
139 return ra
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
140 elif type(expr) is astnodes.VariableUse:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
141 return self.varMap[expr.target]
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
142 elif type(expr) is astnodes.Deref:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
143 # dereference pointer type:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
144 addr = self.genExprCode(expr.ptr)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
145 return ir.Mem(addr)
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 225
diff changeset
146 elif type(expr) is astnodes.FieldRef:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
147 b = self.genExprCode(expr.base)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
148 #b = b.e
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 230
diff changeset
149 bt = theType(expr.base.typ)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
150 offset = ir.Const(bt.fieldOffset(expr.field))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
151 return ir.Mem(ir.Binop(b, '+', offset))
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
152 elif type(expr) is astnodes.Literal:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
153 return ir.Const(expr.val)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
154 elif type(expr) is astnodes.TypeCast:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
155 # TODO: improve this mess:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
156 ar = self.genExprCode(expr.a)
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 230
diff changeset
157 tt = theType(expr.to_type)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
158 if isinstance(tt, astnodes.PointerType):
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
159 if expr.a.typ is intType:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
160 return ar
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
161 elif isinstance(expr.a.typ, astnodes.PointerType):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
162 return ar
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
163 else:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
164 raise Exception()
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
165 else:
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 232
diff changeset
166 raise NotImplementedError("not implemented")
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
167 elif type(expr) is astnodes.FunctionCall:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
168 args = [self.genExprCode(e) for e in expr.args]
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
169 fn = self.funcMap[expr.proc]
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
170 return ir.Call(fn, args)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
171 else:
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
172 raise NotImplementedError('Unknown expr {}'.format(expr))
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
173