Mercurial > lcfOS
annotate python/c3/codegenerator.py @ 271:cf7d5fb7d9c8
Reorganization
author | Windel Bouwman |
---|---|
date | Tue, 20 Aug 2013 18:56:02 +0200 |
parents | 5f8c04a8d26b |
children | e64bae57cda8 |
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): |
261 | 10 """ Generates intermediate code from a package """ |
255 | 11 def __init__(self): |
261 | 12 self.logger = logging.getLogger('c3cgen') |
13 | |
217 | 14 def gencode(self, pkg): |
268 | 15 self.prepare() |
217 | 16 assert type(pkg) is astnodes.Package |
255 | 17 self.logger.info('Generating ir-code for {}'.format(pkg.name)) |
217 | 18 self.varMap = {} # Maps variables to storage locations. |
19 self.funcMap = {} | |
268 | 20 self.m = ir.Module(pkg.name) |
217 | 21 self.genModule(pkg) |
268 | 22 return self.m |
170 | 23 |
217 | 24 # inner helpers: |
25 def genModule(self, pkg): | |
268 | 26 # Take care of forward declarations: |
27 for s in pkg.innerScope.Functions: | |
28 f = self.newFunction(s.name) | |
175 | 29 self.funcMap[s] = f |
268 | 30 for s in pkg.innerScope: |
31 if type(s) is astnodes.Variable: | |
32 #v = self.builder.newTmp(s.name) | |
33 self.varMap[s] = self.newTemp() | |
34 pass | |
35 elif type(s) is astnodes.Function: | |
36 self.genFunction(s) | |
37 else: | |
38 raise NotImplementedError(str(s)) | |
174 | 39 |
268 | 40 def genFunction(self, fn): |
41 # TODO: handle arguments | |
42 f = self.funcMap[fn] | |
269 | 43 # TODO reserve room for stack, this can be done at later point? |
268 | 44 self.setFunction(f) |
269 | 45 l2 = self.newBlock() |
46 self.emit(ir.Jump(l2)) | |
47 self.setBlock(l2) | |
268 | 48 # generate room for locals: |
174 | 49 |
268 | 50 for sym in fn.innerScope: |
51 # TODO: handle parameters different | |
52 v = self.newTemp() | |
53 # TODO: make this ssa here?? | |
54 self.varMap[sym] = v | |
55 | |
56 self.genCode(fn.body) | |
57 # TODO handle return? | |
269 | 58 self.emit(ir.Jump(f.epiloog)) |
59 #self.emit(ir.Return(ir.Const(0))) | |
268 | 60 self.setFunction(None) |
158 | 61 |
217 | 62 def genCode(self, code): |
222 | 63 assert isinstance(code, astnodes.Statement) |
268 | 64 self.setLoc(code.loc) |
222 | 65 if type(code) is astnodes.CompoundStatement: |
221 | 66 for s in code.statements: |
67 self.genCode(s) | |
222 | 68 elif type(code) is astnodes.Assignment: |
268 | 69 rval = self.genExprCode(code.rval) |
70 lval = self.genExprCode(code.lval) | |
71 self.emit(ir.Move(lval, rval)) | |
222 | 72 elif type(code) is astnodes.ExpressionStatement: |
73 self.genExprCode(code.ex) | |
74 elif type(code) is astnodes.IfStatement: | |
268 | 75 bbtrue = self.newBlock() |
76 bbfalse = self.newBlock() | |
77 te = self.newBlock() | |
78 self.genCondCode(code.condition, bbtrue, bbfalse) | |
79 self.setBlock(bbtrue) | |
80 self.genCode(code.truestatement) | |
81 self.emit(ir.Jump(te)) | |
82 self.setBlock(bbfalse) | |
83 if code.falsestatement: | |
84 self.genCode(code.falsestatement) | |
85 self.emit(ir.Jump(te)) | |
86 self.setBlock(te) | |
222 | 87 elif type(code) is astnodes.ReturnStatement: |
228 | 88 if code.expr: |
89 re = self.genExprCode(code.expr) | |
268 | 90 self.emit(ir.Return(re)) |
228 | 91 else: |
92 self.builder.addIns(ir.Return()) | |
222 | 93 elif type(code) is astnodes.WhileStatement: |
268 | 94 bbdo = self.newBlock() |
95 bbtest = self.newBlock() | |
96 te = self.newBlock() | |
97 self.emit(ir.Jump(bbtest)) | |
98 self.setBlock(bbtest) | |
228 | 99 self.genCondCode(code.condition, bbdo, te) |
268 | 100 self.setBlock(bbdo) |
228 | 101 self.genCode(code.statement) |
268 | 102 self.emit(ir.Jump(bbtest)) |
103 self.setBlock(te) | |
222 | 104 else: |
268 | 105 raise NotImplementedError('Unknown stmt {}'.format(code)) |
230 | 106 |
217 | 107 def genCondCode(self, expr, bbtrue, bbfalse): |
228 | 108 # Implement sequential logical operators |
109 assert expr.typ == boolType | |
110 if type(expr) is astnodes.Binop: | |
268 | 111 if expr.op == 'or': |
112 l2 = self.newBlock() | |
228 | 113 self.genCondCode(expr.a, bbtrue, l2) |
268 | 114 self.setBlock(l2) |
228 | 115 self.genCondCode(expr.b, bbtrue, bbfalse) |
268 | 116 elif expr.op == 'and': |
117 l2 = self.newBlock() | |
228 | 118 self.genCondCode(expr.a, l2, bbfalse) |
268 | 119 self.setBlock(l2) |
228 | 120 self.genCondCode(expr.b, bbtrue, bbfalse) |
268 | 121 elif expr.op in ['==', '>', '<']: |
228 | 122 ta = self.genExprCode(expr.a) |
123 tb = self.genExprCode(expr.b) | |
268 | 124 self.emit(ir.CJump(ta, expr.op, tb, bbtrue, bbfalse)) |
125 else: | |
126 raise NotImplementedError('Unknown condition {}'.format(expr)) | |
228 | 127 elif type(expr) is astnodes.Literal: |
128 if expr.val: | |
268 | 129 self.emit(ir.Jump(bbtrue)) |
228 | 130 else: |
268 | 131 self.emit(ir.Jump(bbfalse)) |
228 | 132 else: |
268 | 133 raise NotImplementedError('Unknown cond {}'.format(expr)) |
230 | 134 |
217 | 135 def genExprCode(self, expr): |
230 | 136 assert isinstance(expr, astnodes.Expression) |
268 | 137 if type(expr) is astnodes.Binop and expr.op in ir.Binop.ops: |
225 | 138 ra = self.genExprCode(expr.a) |
268 | 139 rb = self.genExprCode(expr.b) |
140 return ir.Binop(ra, expr.op, rb) | |
141 elif type(expr) is astnodes.Unop and expr.op == '&': | |
142 ra = self.genExprCode(expr.a) | |
143 # TODO: Make this work? | |
144 return ra | |
225 | 145 elif type(expr) is astnodes.VariableUse: |
268 | 146 return self.varMap[expr.target] |
225 | 147 elif type(expr) is astnodes.Deref: |
222 | 148 # dereference pointer type: |
225 | 149 addr = self.genExprCode(expr.ptr) |
268 | 150 return ir.Mem(addr) |
227 | 151 elif type(expr) is astnodes.FieldRef: |
230 | 152 b = self.genExprCode(expr.base) |
268 | 153 #b = b.e |
231 | 154 bt = theType(expr.base.typ) |
268 | 155 offset = ir.Const(bt.fieldOffset(expr.field)) |
156 return ir.Mem(ir.Binop(b, '+', offset)) | |
225 | 157 elif type(expr) is astnodes.Literal: |
268 | 158 return ir.Const(expr.val) |
225 | 159 elif type(expr) is astnodes.TypeCast: |
268 | 160 # TODO: improve this mess: |
222 | 161 ar = self.genExprCode(expr.a) |
231 | 162 tt = theType(expr.to_type) |
230 | 163 if isinstance(tt, astnodes.PointerType): |
222 | 164 if expr.a.typ is intType: |
165 return ar | |
166 elif isinstance(expr.a.typ, astnodes.PointerType): | |
167 return ar | |
168 else: | |
169 raise Exception() | |
230 | 170 else: |
233 | 171 raise NotImplementedError("not implemented") |
225 | 172 elif type(expr) is astnodes.FunctionCall: |
268 | 173 args = [self.genExprCode(e) for e in expr.args] |
259 | 174 fn = self.funcMap[expr.proc] |
268 | 175 return ir.Call(fn, args) |
225 | 176 else: |
259 | 177 raise NotImplementedError('Unknown expr {}'.format(expr)) |
157 | 178 |