Mercurial > lcfOS
annotate python/c3/codegenerator.py @ 275:6f2423df0675
Fixed serve arm-as
author | Windel Bouwman |
---|---|
date | Sat, 14 Sep 2013 17:29:10 +0200 |
parents | ea93e0a7a31e |
children | 2ccd57b1d78c |
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) | |
158 # TODO: Make this work? | |
159 return ra | |
225 | 160 elif type(expr) is astnodes.VariableUse: |
268 | 161 return self.varMap[expr.target] |
225 | 162 elif type(expr) is astnodes.Deref: |
222 | 163 # dereference pointer type: |
225 | 164 addr = self.genExprCode(expr.ptr) |
268 | 165 return ir.Mem(addr) |
227 | 166 elif type(expr) is astnodes.FieldRef: |
230 | 167 b = self.genExprCode(expr.base) |
268 | 168 #b = b.e |
231 | 169 bt = theType(expr.base.typ) |
268 | 170 offset = ir.Const(bt.fieldOffset(expr.field)) |
171 return ir.Mem(ir.Binop(b, '+', offset)) | |
225 | 172 elif type(expr) is astnodes.Literal: |
268 | 173 return ir.Const(expr.val) |
225 | 174 elif type(expr) is astnodes.TypeCast: |
268 | 175 # TODO: improve this mess: |
222 | 176 ar = self.genExprCode(expr.a) |
231 | 177 tt = theType(expr.to_type) |
230 | 178 if isinstance(tt, astnodes.PointerType): |
222 | 179 if expr.a.typ is intType: |
180 return ar | |
181 elif isinstance(expr.a.typ, astnodes.PointerType): | |
182 return ar | |
183 else: | |
184 raise Exception() | |
230 | 185 else: |
233 | 186 raise NotImplementedError("not implemented") |
225 | 187 elif type(expr) is astnodes.FunctionCall: |
268 | 188 args = [self.genExprCode(e) for e in expr.args] |
259 | 189 fn = self.funcMap[expr.proc] |
268 | 190 return ir.Call(fn, args) |
225 | 191 else: |
259 | 192 raise NotImplementedError('Unknown expr {}'.format(expr)) |
157 | 193 |