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