Mercurial > lcfOS
annotate python/ppci/c3/codegenerator.py @ 306:b145f8e6050b
Start on c3 rewrite
author | Windel Bouwman |
---|---|
date | Mon, 09 Dec 2013 19:00:21 +0100 |
parents | 0615b5308710 |
children | e609d5296ee9 |
rev | line source |
---|---|
255 | 1 import logging |
301 | 2 from .. import ir |
155 | 3 from . import astnodes |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
4 from ppci import CompilerError |
287 | 5 from .analyse import theType |
230 | 6 |
228 | 7 |
268 | 8 class CodeGenerator(ir.Builder): |
288 | 9 """ |
274 | 10 Generates intermediate (IR) code from a package. The entry function is |
11 'genModule'. The main task of this part is to rewrite complex control | |
12 structures, such as while and for loops into simple conditional | |
13 jump statements. Also complex conditional statements are simplified. | |
14 Such as 'and' and 'or' statements are rewritten in conditional jumps. | |
15 And structured datatypes are rewritten. | |
16 """ | |
255 | 17 def __init__(self): |
261 | 18 self.logger = logging.getLogger('c3cgen') |
19 | |
217 | 20 def gencode(self, pkg): |
268 | 21 self.prepare() |
217 | 22 assert type(pkg) is astnodes.Package |
255 | 23 self.logger.info('Generating ir-code for {}'.format(pkg.name)) |
288 | 24 self.varMap = {} # Maps variables to storage locations. |
217 | 25 self.funcMap = {} |
268 | 26 self.m = ir.Module(pkg.name) |
217 | 27 self.genModule(pkg) |
268 | 28 return self.m |
170 | 29 |
217 | 30 # inner helpers: |
31 def genModule(self, pkg): | |
268 | 32 # Take care of forward declarations: |
33 for s in pkg.innerScope.Functions: | |
34 f = self.newFunction(s.name) | |
175 | 35 self.funcMap[s] = f |
272 | 36 for v in pkg.innerScope.Variables: |
37 self.varMap[v] = self.newTemp() | |
38 for s in pkg.innerScope.Functions: | |
39 self.genFunction(s) | |
174 | 40 |
268 | 41 def genFunction(self, fn): |
42 # TODO: handle arguments | |
43 f = self.funcMap[fn] | |
272 | 44 f.return_value = self.newTemp() |
268 | 45 self.setFunction(f) |
269 | 46 l2 = self.newBlock() |
47 self.emit(ir.Jump(l2)) | |
48 self.setBlock(l2) | |
268 | 49 # generate room for locals: |
174 | 50 |
268 | 51 for sym in fn.innerScope: |
52 # TODO: handle parameters different | |
272 | 53 if sym.isParameter: |
274 | 54 v = ir.Parameter(sym.name) |
55 f.addParameter(v) | |
56 elif sym.isLocal: | |
275 | 57 v = ir.LocalVariable(sym.name) |
58 f.addLocal(v) | |
274 | 59 else: |
275 | 60 #v = self.newTemp() |
61 raise NotImplementedError('{}'.format(sym)) | |
268 | 62 # TODO: make this ssa here?? |
63 self.varMap[sym] = v | |
64 | |
65 self.genCode(fn.body) | |
272 | 66 # Set the default return value to zero: |
67 # TBD: this may not be required? | |
68 self.emit(ir.Move(f.return_value, ir.Const(0))) | |
269 | 69 self.emit(ir.Jump(f.epiloog)) |
268 | 70 self.setFunction(None) |
158 | 71 |
217 | 72 def genCode(self, code): |
222 | 73 assert isinstance(code, astnodes.Statement) |
268 | 74 self.setLoc(code.loc) |
222 | 75 if type(code) is astnodes.CompoundStatement: |
221 | 76 for s in code.statements: |
77 self.genCode(s) | |
306 | 78 elif type(code) is astnodes.EmptyStatement: |
79 pass | |
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) | |
306 | 95 self.genCode(code.falsestatement) |
268 | 96 self.emit(ir.Jump(te)) |
97 self.setBlock(te) | |
222 | 98 elif type(code) is astnodes.ReturnStatement: |
303 | 99 re = self.genExprCode(code.expr) |
100 self.emit(ir.Move(self.fn.return_value, re)) | |
101 self.emit(ir.Jump(self.fn.epiloog)) | |
102 b = self.newBlock() | |
103 self.setBlock(b) | |
222 | 104 elif type(code) is astnodes.WhileStatement: |
268 | 105 bbdo = self.newBlock() |
106 bbtest = self.newBlock() | |
107 te = self.newBlock() | |
108 self.emit(ir.Jump(bbtest)) | |
109 self.setBlock(bbtest) | |
228 | 110 self.genCondCode(code.condition, bbdo, te) |
268 | 111 self.setBlock(bbdo) |
228 | 112 self.genCode(code.statement) |
268 | 113 self.emit(ir.Jump(bbtest)) |
114 self.setBlock(te) | |
222 | 115 else: |
268 | 116 raise NotImplementedError('Unknown stmt {}'.format(code)) |
230 | 117 |
217 | 118 def genCondCode(self, expr, bbtrue, bbfalse): |
228 | 119 # Implement sequential logical operators |
120 if type(expr) is astnodes.Binop: | |
268 | 121 if expr.op == 'or': |
122 l2 = self.newBlock() | |
228 | 123 self.genCondCode(expr.a, bbtrue, l2) |
268 | 124 self.setBlock(l2) |
228 | 125 self.genCondCode(expr.b, bbtrue, bbfalse) |
268 | 126 elif expr.op == 'and': |
127 l2 = self.newBlock() | |
228 | 128 self.genCondCode(expr.a, l2, bbfalse) |
268 | 129 self.setBlock(l2) |
228 | 130 self.genCondCode(expr.b, bbtrue, bbfalse) |
305 | 131 elif expr.op in ['==', '>', '<', '!=', '<=', '>=']: |
228 | 132 ta = self.genExprCode(expr.a) |
133 tb = self.genExprCode(expr.b) | |
268 | 134 self.emit(ir.CJump(ta, expr.op, tb, bbtrue, bbfalse)) |
135 else: | |
136 raise NotImplementedError('Unknown condition {}'.format(expr)) | |
228 | 137 elif type(expr) is astnodes.Literal: |
288 | 138 if expr.val: |
268 | 139 self.emit(ir.Jump(bbtrue)) |
288 | 140 else: |
268 | 141 self.emit(ir.Jump(bbfalse)) |
228 | 142 else: |
288 | 143 raise NotImplementedError('Unknown cond {}'.format(expr)) |
230 | 144 |
217 | 145 def genExprCode(self, expr): |
230 | 146 assert isinstance(expr, astnodes.Expression) |
268 | 147 if type(expr) is astnodes.Binop and expr.op in ir.Binop.ops: |
225 | 148 ra = self.genExprCode(expr.a) |
268 | 149 rb = self.genExprCode(expr.b) |
150 return ir.Binop(ra, expr.op, rb) | |
151 elif type(expr) is astnodes.Unop and expr.op == '&': | |
152 ra = self.genExprCode(expr.a) | |
279 | 153 assert type(ra) is ir.Mem |
154 return ra.e | |
225 | 155 elif type(expr) is astnodes.VariableUse: |
279 | 156 # This returns the dereferenced variable. |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
157 if expr.target.isParameter: |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
158 # TODO: now parameters are handled different. Not nice? |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
159 return self.varMap[expr.target] |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
160 else: |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
161 return ir.Mem(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: |
279 | 167 base = self.genExprCode(expr.base) |
168 assert type(base) is ir.Mem, type(base) | |
169 base = base.e | |
231 | 170 bt = theType(expr.base.typ) |
268 | 171 offset = ir.Const(bt.fieldOffset(expr.field)) |
279 | 172 return ir.Mem(ir.Add(base, offset)) |
225 | 173 elif type(expr) is astnodes.Literal: |
268 | 174 return ir.Const(expr.val) |
225 | 175 elif type(expr) is astnodes.TypeCast: |
268 | 176 # TODO: improve this mess: |
222 | 177 ar = self.genExprCode(expr.a) |
231 | 178 tt = theType(expr.to_type) |
230 | 179 if isinstance(tt, astnodes.PointerType): |
306 | 180 if expr.a.typ is expr.scope['int']: |
222 | 181 return ar |
182 elif isinstance(expr.a.typ, astnodes.PointerType): | |
183 return ar | |
184 else: | |
185 raise Exception() | |
230 | 186 else: |
233 | 187 raise NotImplementedError("not implemented") |
225 | 188 elif type(expr) is astnodes.FunctionCall: |
268 | 189 args = [self.genExprCode(e) for e in expr.args] |
305 | 190 #fn = self.funcMap[expr.proc] |
191 fn = expr.proc.name | |
268 | 192 return ir.Call(fn, args) |
225 | 193 else: |
259 | 194 raise NotImplementedError('Unknown expr {}'.format(expr)) |