Mercurial > lcfOS
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 | 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] | |
43 self.setFunction(f) | |
44 # generate room for locals: | |
174 | 45 |
268 | 46 for sym in fn.innerScope: |
47 # TODO: handle parameters different | |
48 v = self.newTemp() | |
49 # TODO: make this ssa here?? | |
50 self.varMap[sym] = v | |
51 | |
52 self.genCode(fn.body) | |
53 # TODO handle return? | |
54 self.emit(ir.Return(ir.Const(0))) | |
55 self.setFunction(None) | |
158 | 56 |
217 | 57 def genCode(self, code): |
222 | 58 assert isinstance(code, astnodes.Statement) |
268 | 59 self.setLoc(code.loc) |
222 | 60 if type(code) is astnodes.CompoundStatement: |
221 | 61 for s in code.statements: |
62 self.genCode(s) | |
222 | 63 elif type(code) is astnodes.Assignment: |
268 | 64 rval = self.genExprCode(code.rval) |
65 lval = self.genExprCode(code.lval) | |
66 self.emit(ir.Move(lval, rval)) | |
222 | 67 elif type(code) is astnodes.ExpressionStatement: |
68 self.genExprCode(code.ex) | |
69 elif type(code) is astnodes.IfStatement: | |
268 | 70 bbtrue = self.newBlock() |
71 bbfalse = self.newBlock() | |
72 te = self.newBlock() | |
73 self.genCondCode(code.condition, bbtrue, bbfalse) | |
74 self.setBlock(bbtrue) | |
75 self.genCode(code.truestatement) | |
76 self.emit(ir.Jump(te)) | |
77 self.setBlock(bbfalse) | |
78 if code.falsestatement: | |
79 self.genCode(code.falsestatement) | |
80 self.emit(ir.Jump(te)) | |
81 self.setBlock(te) | |
222 | 82 elif type(code) is astnodes.ReturnStatement: |
228 | 83 if code.expr: |
84 re = self.genExprCode(code.expr) | |
268 | 85 self.emit(ir.Return(re)) |
228 | 86 else: |
87 self.builder.addIns(ir.Return()) | |
222 | 88 elif type(code) is astnodes.WhileStatement: |
268 | 89 bbdo = self.newBlock() |
90 bbtest = self.newBlock() | |
91 te = self.newBlock() | |
92 self.emit(ir.Jump(bbtest)) | |
93 self.setBlock(bbtest) | |
228 | 94 self.genCondCode(code.condition, bbdo, te) |
268 | 95 self.setBlock(bbdo) |
228 | 96 self.genCode(code.statement) |
268 | 97 self.emit(ir.Jump(bbtest)) |
98 self.setBlock(te) | |
222 | 99 else: |
268 | 100 raise NotImplementedError('Unknown stmt {}'.format(code)) |
230 | 101 |
217 | 102 def genCondCode(self, expr, bbtrue, bbfalse): |
228 | 103 # Implement sequential logical operators |
104 assert expr.typ == boolType | |
105 if type(expr) is astnodes.Binop: | |
268 | 106 if expr.op == 'or': |
107 l2 = self.newBlock() | |
228 | 108 self.genCondCode(expr.a, bbtrue, l2) |
268 | 109 self.setBlock(l2) |
228 | 110 self.genCondCode(expr.b, bbtrue, bbfalse) |
268 | 111 elif expr.op == 'and': |
112 l2 = self.newBlock() | |
228 | 113 self.genCondCode(expr.a, l2, bbfalse) |
268 | 114 self.setBlock(l2) |
228 | 115 self.genCondCode(expr.b, bbtrue, bbfalse) |
268 | 116 elif expr.op in ['==', '>', '<']: |
228 | 117 ta = self.genExprCode(expr.a) |
118 tb = self.genExprCode(expr.b) | |
268 | 119 self.emit(ir.CJump(ta, expr.op, tb, bbtrue, bbfalse)) |
120 else: | |
121 raise NotImplementedError('Unknown condition {}'.format(expr)) | |
228 | 122 elif type(expr) is astnodes.Literal: |
123 if expr.val: | |
268 | 124 self.emit(ir.Jump(bbtrue)) |
228 | 125 else: |
268 | 126 self.emit(ir.Jump(bbfalse)) |
228 | 127 else: |
268 | 128 raise NotImplementedError('Unknown cond {}'.format(expr)) |
230 | 129 |
217 | 130 def genExprCode(self, expr): |
230 | 131 assert isinstance(expr, astnodes.Expression) |
268 | 132 if type(expr) is astnodes.Binop and expr.op in ir.Binop.ops: |
225 | 133 ra = self.genExprCode(expr.a) |
268 | 134 rb = self.genExprCode(expr.b) |
135 return ir.Binop(ra, expr.op, rb) | |
136 elif type(expr) is astnodes.Unop and expr.op == '&': | |
137 ra = self.genExprCode(expr.a) | |
138 # TODO: Make this work? | |
139 return ra | |
225 | 140 elif type(expr) is astnodes.VariableUse: |
268 | 141 return self.varMap[expr.target] |
225 | 142 elif type(expr) is astnodes.Deref: |
222 | 143 # dereference pointer type: |
225 | 144 addr = self.genExprCode(expr.ptr) |
268 | 145 return ir.Mem(addr) |
227 | 146 elif type(expr) is astnodes.FieldRef: |
230 | 147 b = self.genExprCode(expr.base) |
268 | 148 #b = b.e |
231 | 149 bt = theType(expr.base.typ) |
268 | 150 offset = ir.Const(bt.fieldOffset(expr.field)) |
151 return ir.Mem(ir.Binop(b, '+', offset)) | |
225 | 152 elif type(expr) is astnodes.Literal: |
268 | 153 return ir.Const(expr.val) |
225 | 154 elif type(expr) is astnodes.TypeCast: |
268 | 155 # TODO: improve this mess: |
222 | 156 ar = self.genExprCode(expr.a) |
231 | 157 tt = theType(expr.to_type) |
230 | 158 if isinstance(tt, astnodes.PointerType): |
222 | 159 if expr.a.typ is intType: |
160 return ar | |
161 elif isinstance(expr.a.typ, astnodes.PointerType): | |
162 return ar | |
163 else: | |
164 raise Exception() | |
230 | 165 else: |
233 | 166 raise NotImplementedError("not implemented") |
225 | 167 elif type(expr) is astnodes.FunctionCall: |
268 | 168 args = [self.genExprCode(e) for e in expr.args] |
259 | 169 fn = self.funcMap[expr.proc] |
268 | 170 return ir.Call(fn, args) |
225 | 171 else: |
259 | 172 raise NotImplementedError('Unknown expr {}'.format(expr)) |
157 | 173 |