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