Mercurial > lcfOS
annotate python/c3/codegenerator.py @ 281:4496cae24d7f
Improved logview
author | Windel Bouwman |
---|---|
date | Sat, 02 Nov 2013 11:11:40 +0100 |
parents | 02385f62f250 |
children | 1c7c1e619be8 |
rev | line source |
---|---|
255 | 1 import logging |
155 | 2 import ir |
3 from . import astnodes | |
222 | 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 | 6 from .typecheck import theType |
230 | 7 |
228 | 8 |
268 | 9 class CodeGenerator(ir.Builder): |
274 | 10 """ |
11 Generates intermediate (IR) code from a package. The entry function is | |
12 'genModule'. The main task of this part is to rewrite complex control | |
13 structures, such as while and for loops into simple conditional | |
14 jump statements. Also complex conditional statements are simplified. | |
15 Such as 'and' and 'or' statements are rewritten in conditional jumps. | |
16 And structured datatypes are rewritten. | |
17 """ | |
255 | 18 def __init__(self): |
261 | 19 self.logger = logging.getLogger('c3cgen') |
20 | |
217 | 21 def gencode(self, pkg): |
268 | 22 self.prepare() |
217 | 23 assert type(pkg) is astnodes.Package |
255 | 24 self.logger.info('Generating ir-code for {}'.format(pkg.name)) |
217 | 25 self.varMap = {} # Maps variables to storage locations. |
26 self.funcMap = {} | |
268 | 27 self.m = ir.Module(pkg.name) |
217 | 28 self.genModule(pkg) |
268 | 29 return self.m |
170 | 30 |
217 | 31 # inner helpers: |
32 def genModule(self, pkg): | |
268 | 33 # Take care of forward declarations: |
34 for s in pkg.innerScope.Functions: | |
35 f = self.newFunction(s.name) | |
175 | 36 self.funcMap[s] = f |
272 | 37 for v in pkg.innerScope.Variables: |
38 self.varMap[v] = self.newTemp() | |
39 for s in pkg.innerScope.Functions: | |
40 self.genFunction(s) | |
174 | 41 |
268 | 42 def genFunction(self, fn): |
43 # TODO: handle arguments | |
44 f = self.funcMap[fn] | |
272 | 45 f.return_value = self.newTemp() |
269 | 46 # TODO reserve room for stack, this can be done at later point? |
268 | 47 self.setFunction(f) |
269 | 48 l2 = self.newBlock() |
49 self.emit(ir.Jump(l2)) | |
50 self.setBlock(l2) | |
268 | 51 # generate room for locals: |
174 | 52 |
268 | 53 for sym in fn.innerScope: |
54 # TODO: handle parameters different | |
272 | 55 if sym.isParameter: |
274 | 56 v = ir.Parameter(sym.name) |
57 f.addParameter(v) | |
58 elif sym.isLocal: | |
275 | 59 v = ir.LocalVariable(sym.name) |
60 f.addLocal(v) | |
274 | 61 else: |
275 | 62 #v = self.newTemp() |
63 raise NotImplementedError('{}'.format(sym)) | |
268 | 64 # TODO: make this ssa here?? |
65 self.varMap[sym] = v | |
66 | |
67 self.genCode(fn.body) | |
272 | 68 # Set the default return value to zero: |
69 # TBD: this may not be required? | |
70 self.emit(ir.Move(f.return_value, ir.Const(0))) | |
269 | 71 self.emit(ir.Jump(f.epiloog)) |
268 | 72 self.setFunction(None) |
158 | 73 |
217 | 74 def genCode(self, code): |
222 | 75 assert isinstance(code, astnodes.Statement) |
268 | 76 self.setLoc(code.loc) |
222 | 77 if type(code) is astnodes.CompoundStatement: |
221 | 78 for s in code.statements: |
79 self.genCode(s) | |
222 | 80 elif type(code) is astnodes.Assignment: |
268 | 81 rval = self.genExprCode(code.rval) |
82 lval = self.genExprCode(code.lval) | |
83 self.emit(ir.Move(lval, rval)) | |
222 | 84 elif type(code) is astnodes.ExpressionStatement: |
275 | 85 self.emit(ir.Exp(self.genExprCode(code.ex))) |
222 | 86 elif type(code) is astnodes.IfStatement: |
268 | 87 bbtrue = self.newBlock() |
88 bbfalse = self.newBlock() | |
89 te = self.newBlock() | |
90 self.genCondCode(code.condition, bbtrue, bbfalse) | |
91 self.setBlock(bbtrue) | |
92 self.genCode(code.truestatement) | |
93 self.emit(ir.Jump(te)) | |
94 self.setBlock(bbfalse) | |
95 if code.falsestatement: | |
96 self.genCode(code.falsestatement) | |
97 self.emit(ir.Jump(te)) | |
98 self.setBlock(te) | |
222 | 99 elif type(code) is astnodes.ReturnStatement: |
228 | 100 if code.expr: |
101 re = self.genExprCode(code.expr) | |
272 | 102 self.emit(ir.Move(self.fn.return_value, re)) |
103 self.emit(ir.Jump(self.fn.epiloog)) | |
274 | 104 b = self.newBlock() |
105 self.setBlock(b) | |
228 | 106 else: |
107 self.builder.addIns(ir.Return()) | |
222 | 108 elif type(code) is astnodes.WhileStatement: |
268 | 109 bbdo = self.newBlock() |
110 bbtest = self.newBlock() | |
111 te = self.newBlock() | |
112 self.emit(ir.Jump(bbtest)) | |
113 self.setBlock(bbtest) | |
228 | 114 self.genCondCode(code.condition, bbdo, te) |
268 | 115 self.setBlock(bbdo) |
228 | 116 self.genCode(code.statement) |
268 | 117 self.emit(ir.Jump(bbtest)) |
118 self.setBlock(te) | |
222 | 119 else: |
268 | 120 raise NotImplementedError('Unknown stmt {}'.format(code)) |
230 | 121 |
217 | 122 def genCondCode(self, expr, bbtrue, bbfalse): |
228 | 123 # Implement sequential logical operators |
124 assert expr.typ == boolType | |
125 if type(expr) is astnodes.Binop: | |
268 | 126 if expr.op == 'or': |
127 l2 = self.newBlock() | |
228 | 128 self.genCondCode(expr.a, bbtrue, l2) |
268 | 129 self.setBlock(l2) |
228 | 130 self.genCondCode(expr.b, bbtrue, bbfalse) |
268 | 131 elif expr.op == 'and': |
132 l2 = self.newBlock() | |
228 | 133 self.genCondCode(expr.a, l2, bbfalse) |
268 | 134 self.setBlock(l2) |
228 | 135 self.genCondCode(expr.b, bbtrue, bbfalse) |
268 | 136 elif expr.op in ['==', '>', '<']: |
228 | 137 ta = self.genExprCode(expr.a) |
138 tb = self.genExprCode(expr.b) | |
268 | 139 self.emit(ir.CJump(ta, expr.op, tb, bbtrue, bbfalse)) |
140 else: | |
141 raise NotImplementedError('Unknown condition {}'.format(expr)) | |
228 | 142 elif type(expr) is astnodes.Literal: |
143 if expr.val: | |
268 | 144 self.emit(ir.Jump(bbtrue)) |
228 | 145 else: |
268 | 146 self.emit(ir.Jump(bbfalse)) |
228 | 147 else: |
268 | 148 raise NotImplementedError('Unknown cond {}'.format(expr)) |
230 | 149 |
217 | 150 def genExprCode(self, expr): |
230 | 151 assert isinstance(expr, astnodes.Expression) |
268 | 152 if type(expr) is astnodes.Binop and expr.op in ir.Binop.ops: |
225 | 153 ra = self.genExprCode(expr.a) |
268 | 154 rb = self.genExprCode(expr.b) |
155 return ir.Binop(ra, expr.op, rb) | |
156 elif type(expr) is astnodes.Unop and expr.op == '&': | |
157 ra = self.genExprCode(expr.a) | |
279 | 158 assert type(ra) is ir.Mem |
159 return ra.e | |
225 | 160 elif type(expr) is astnodes.VariableUse: |
279 | 161 # This returns the dereferenced variable. |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
162 if expr.target.isParameter: |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
163 # TODO: now parameters are handled different. Not nice? |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
164 return self.varMap[expr.target] |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
165 else: |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
166 return ir.Mem(self.varMap[expr.target]) |
225 | 167 elif type(expr) is astnodes.Deref: |
222 | 168 # dereference pointer type: |
225 | 169 addr = self.genExprCode(expr.ptr) |
268 | 170 return ir.Mem(addr) |
227 | 171 elif type(expr) is astnodes.FieldRef: |
279 | 172 base = self.genExprCode(expr.base) |
173 assert type(base) is ir.Mem, type(base) | |
174 base = base.e | |
231 | 175 bt = theType(expr.base.typ) |
268 | 176 offset = ir.Const(bt.fieldOffset(expr.field)) |
279 | 177 return ir.Mem(ir.Add(base, offset)) |
225 | 178 elif type(expr) is astnodes.Literal: |
268 | 179 return ir.Const(expr.val) |
225 | 180 elif type(expr) is astnodes.TypeCast: |
268 | 181 # TODO: improve this mess: |
222 | 182 ar = self.genExprCode(expr.a) |
231 | 183 tt = theType(expr.to_type) |
230 | 184 if isinstance(tt, astnodes.PointerType): |
222 | 185 if expr.a.typ is intType: |
186 return ar | |
187 elif isinstance(expr.a.typ, astnodes.PointerType): | |
188 return ar | |
189 else: | |
190 raise Exception() | |
230 | 191 else: |
233 | 192 raise NotImplementedError("not implemented") |
225 | 193 elif type(expr) is astnodes.FunctionCall: |
268 | 194 args = [self.genExprCode(e) for e in expr.args] |
259 | 195 fn = self.funcMap[expr.proc] |
268 | 196 return ir.Call(fn, args) |
225 | 197 else: |
259 | 198 raise NotImplementedError('Unknown expr {}'.format(expr)) |
157 | 199 |