Mercurial > lcfOS
annotate python/ppci/c3/codegenerator.py @ 303:be7f60545368
Final fixups
author | Windel Bouwman |
---|---|
date | Fri, 06 Dec 2013 12:37:48 +0100 |
parents | 6753763d3bec |
children | 0615b5308710 |
rev | line source |
---|---|
255 | 1 import logging |
301 | 2 from .. import ir |
155 | 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 |
287 | 6 from .analyse import theType |
230 | 7 |
228 | 8 |
268 | 9 class CodeGenerator(ir.Builder): |
288 | 10 """ |
274 | 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)) |
288 | 25 self.varMap = {} # Maps variables to storage locations. |
217 | 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 self.varMap[v] = self.newTemp() | |
39 for s in pkg.innerScope.Functions: | |
40 self.genFunction(s) | |
174 | 41 |
268 | 42 def genFunction(self, fn): |
43 # TODO: handle arguments | |
44 f = self.funcMap[fn] | |
272 | 45 f.return_value = self.newTemp() |
269 | 46 # TODO reserve room for stack, this can be done at later point? |
268 | 47 self.setFunction(f) |
269 | 48 l2 = self.newBlock() |
49 self.emit(ir.Jump(l2)) | |
50 self.setBlock(l2) | |
268 | 51 # generate room for locals: |
174 | 52 |
268 | 53 for sym in fn.innerScope: |
54 # TODO: handle parameters different | |
272 | 55 if sym.isParameter: |
274 | 56 v = ir.Parameter(sym.name) |
57 f.addParameter(v) | |
58 elif sym.isLocal: | |
275 | 59 v = ir.LocalVariable(sym.name) |
60 f.addLocal(v) | |
274 | 61 else: |
275 | 62 #v = self.newTemp() |
63 raise NotImplementedError('{}'.format(sym)) | |
268 | 64 # TODO: make this ssa here?? |
65 self.varMap[sym] = v | |
66 | |
67 self.genCode(fn.body) | |
272 | 68 # Set the default return value to zero: |
69 # TBD: this may not be required? | |
70 self.emit(ir.Move(f.return_value, ir.Const(0))) | |
269 | 71 self.emit(ir.Jump(f.epiloog)) |
268 | 72 self.setFunction(None) |
158 | 73 |
217 | 74 def genCode(self, code): |
222 | 75 assert isinstance(code, astnodes.Statement) |
268 | 76 self.setLoc(code.loc) |
222 | 77 if type(code) is astnodes.CompoundStatement: |
221 | 78 for s in code.statements: |
79 self.genCode(s) | |
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) | |
95 if code.falsestatement: | |
96 self.genCode(code.falsestatement) | |
97 self.emit(ir.Jump(te)) | |
98 self.setBlock(te) | |
222 | 99 elif type(code) is astnodes.ReturnStatement: |
303 | 100 assert code.expr |
101 re = self.genExprCode(code.expr) | |
102 self.emit(ir.Move(self.fn.return_value, re)) | |
103 self.emit(ir.Jump(self.fn.epiloog)) | |
104 b = self.newBlock() | |
105 self.setBlock(b) | |
222 | 106 elif type(code) is astnodes.WhileStatement: |
268 | 107 bbdo = self.newBlock() |
108 bbtest = self.newBlock() | |
109 te = self.newBlock() | |
110 self.emit(ir.Jump(bbtest)) | |
111 self.setBlock(bbtest) | |
228 | 112 self.genCondCode(code.condition, bbdo, te) |
268 | 113 self.setBlock(bbdo) |
228 | 114 self.genCode(code.statement) |
268 | 115 self.emit(ir.Jump(bbtest)) |
116 self.setBlock(te) | |
222 | 117 else: |
268 | 118 raise NotImplementedError('Unknown stmt {}'.format(code)) |
230 | 119 |
217 | 120 def genCondCode(self, expr, bbtrue, bbfalse): |
228 | 121 # Implement sequential logical operators |
122 assert expr.typ == boolType | |
123 if type(expr) is astnodes.Binop: | |
268 | 124 if expr.op == 'or': |
125 l2 = self.newBlock() | |
228 | 126 self.genCondCode(expr.a, bbtrue, l2) |
268 | 127 self.setBlock(l2) |
228 | 128 self.genCondCode(expr.b, bbtrue, bbfalse) |
268 | 129 elif expr.op == 'and': |
130 l2 = self.newBlock() | |
228 | 131 self.genCondCode(expr.a, l2, bbfalse) |
268 | 132 self.setBlock(l2) |
228 | 133 self.genCondCode(expr.b, bbtrue, bbfalse) |
268 | 134 elif expr.op in ['==', '>', '<']: |
228 | 135 ta = self.genExprCode(expr.a) |
136 tb = self.genExprCode(expr.b) | |
268 | 137 self.emit(ir.CJump(ta, expr.op, tb, bbtrue, bbfalse)) |
138 else: | |
139 raise NotImplementedError('Unknown condition {}'.format(expr)) | |
228 | 140 elif type(expr) is astnodes.Literal: |
288 | 141 if expr.val: |
268 | 142 self.emit(ir.Jump(bbtrue)) |
288 | 143 else: |
268 | 144 self.emit(ir.Jump(bbfalse)) |
228 | 145 else: |
288 | 146 raise NotImplementedError('Unknown cond {}'.format(expr)) |
230 | 147 |
217 | 148 def genExprCode(self, expr): |
230 | 149 assert isinstance(expr, astnodes.Expression) |
268 | 150 if type(expr) is astnodes.Binop and expr.op in ir.Binop.ops: |
225 | 151 ra = self.genExprCode(expr.a) |
268 | 152 rb = self.genExprCode(expr.b) |
153 return ir.Binop(ra, expr.op, rb) | |
154 elif type(expr) is astnodes.Unop and expr.op == '&': | |
155 ra = self.genExprCode(expr.a) | |
279 | 156 assert type(ra) is ir.Mem |
157 return ra.e | |
225 | 158 elif type(expr) is astnodes.VariableUse: |
279 | 159 # This returns the dereferenced variable. |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
160 if expr.target.isParameter: |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
161 # TODO: now parameters are handled different. Not nice? |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
162 return self.varMap[expr.target] |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
163 else: |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
164 return ir.Mem(self.varMap[expr.target]) |
225 | 165 elif type(expr) is astnodes.Deref: |
222 | 166 # dereference pointer type: |
225 | 167 addr = self.genExprCode(expr.ptr) |
268 | 168 return ir.Mem(addr) |
227 | 169 elif type(expr) is astnodes.FieldRef: |
279 | 170 base = self.genExprCode(expr.base) |
171 assert type(base) is ir.Mem, type(base) | |
172 base = base.e | |
231 | 173 bt = theType(expr.base.typ) |
268 | 174 offset = ir.Const(bt.fieldOffset(expr.field)) |
279 | 175 return ir.Mem(ir.Add(base, offset)) |
225 | 176 elif type(expr) is astnodes.Literal: |
268 | 177 return ir.Const(expr.val) |
225 | 178 elif type(expr) is astnodes.TypeCast: |
268 | 179 # TODO: improve this mess: |
222 | 180 ar = self.genExprCode(expr.a) |
231 | 181 tt = theType(expr.to_type) |
230 | 182 if isinstance(tt, astnodes.PointerType): |
222 | 183 if expr.a.typ is intType: |
184 return ar | |
185 elif isinstance(expr.a.typ, astnodes.PointerType): | |
186 return ar | |
187 else: | |
188 raise Exception() | |
230 | 189 else: |
233 | 190 raise NotImplementedError("not implemented") |
225 | 191 elif type(expr) is astnodes.FunctionCall: |
268 | 192 args = [self.genExprCode(e) for e in expr.args] |
259 | 193 fn = self.funcMap[expr.proc] |
268 | 194 return ir.Call(fn, args) |
225 | 195 else: |
259 | 196 raise NotImplementedError('Unknown expr {}'.format(expr)) |