Mercurial > lcfOS
annotate python/ppci/c3/codegenerator.py @ 383:173e20a47fda
Added linker description loader
author | Windel Bouwman |
---|---|
date | Sun, 27 Apr 2014 17:40:39 +0200 |
parents | c49459768aaa |
children | 2ec730e45ea1 |
rev | line source |
---|---|
255 | 1 import logging |
354 | 2 import struct |
301 | 3 from .. import ir |
307 | 4 from .. import irutils |
308 | 5 from . import astnodes as ast |
230 | 6 |
228 | 7 |
307 | 8 class SemanticError(Exception): |
308 | 9 """ Error thrown when a semantic issue is observed """ |
307 | 10 def __init__(self, msg, loc): |
308 | 11 super().__init__() |
307 | 12 self.msg = msg |
13 self.loc = loc | |
14 | |
15 | |
16 class CodeGenerator(irutils.Builder): | |
288 | 17 """ |
274 | 18 Generates intermediate (IR) code from a package. The entry function is |
19 'genModule'. The main task of this part is to rewrite complex control | |
20 structures, such as while and for loops into simple conditional | |
21 jump statements. Also complex conditional statements are simplified. | |
22 Such as 'and' and 'or' statements are rewritten in conditional jumps. | |
23 And structured datatypes are rewritten. | |
307 | 24 |
25 Type checking is done in one run with code generation. | |
274 | 26 """ |
307 | 27 def __init__(self, diag): |
261 | 28 self.logger = logging.getLogger('c3cgen') |
307 | 29 self.diag = diag |
261 | 30 |
217 | 31 def gencode(self, pkg): |
308 | 32 """ Generate code for a single module """ |
268 | 33 self.prepare() |
308 | 34 assert type(pkg) is ast.Package |
307 | 35 self.pkg = pkg |
36 self.intType = pkg.scope['int'] | |
37 self.boolType = pkg.scope['bool'] | |
334 | 38 self.logger.debug('Generating ir-code for {}'.format(pkg.name), extra={'c3_ast':pkg}) |
288 | 39 self.varMap = {} # Maps variables to storage locations. |
268 | 40 self.m = ir.Module(pkg.name) |
307 | 41 try: |
362 | 42 # Only generate function if function contains a body: |
43 real_functions = list(filter(lambda f: f.body, pkg.innerScope.Functions)) | |
307 | 44 for v in pkg.innerScope.Variables: |
364 | 45 v2 = ir.GlobalVariable(v.name) |
46 self.varMap[v] = v2 | |
363 | 47 if not v.isLocal: |
364 | 48 self.m.add_variable(v2) |
362 | 49 for s in real_functions: |
308 | 50 self.gen_function(s) |
307 | 51 except SemanticError as e: |
52 self.error(e.msg, e.loc) | |
313 | 53 if self.pkg.ok: |
54 return self.m | |
308 | 55 |
56 def error(self, msg, loc=None): | |
57 self.pkg.ok = False | |
58 self.diag.error(msg, loc) | |
307 | 59 |
308 | 60 def gen_function(self, fn): |
268 | 61 # TODO: handle arguments |
363 | 62 f = self.newFunction(fn.name) |
272 | 63 f.return_value = self.newTemp() |
268 | 64 self.setFunction(f) |
269 | 65 l2 = self.newBlock() |
66 self.emit(ir.Jump(l2)) | |
67 self.setBlock(l2) | |
268 | 68 # generate room for locals: |
174 | 69 |
268 | 70 for sym in fn.innerScope: |
316 | 71 self.the_type(sym.typ) |
272 | 72 if sym.isParameter: |
316 | 73 p = ir.Parameter(sym.name) |
74 variable = ir.LocalVariable(sym.name + '_copy') | |
75 f.addParameter(p) | |
76 f.addLocal(variable) | |
77 # Move parameter into local copy: | |
78 self.emit(ir.Move(ir.Mem(variable), p)) | |
274 | 79 elif sym.isLocal: |
308 | 80 variable = ir.LocalVariable(sym.name) |
81 f.addLocal(variable) | |
82 elif isinstance(sym, ast.Variable): | |
83 variable = ir.LocalVariable(sym.name) | |
84 f.addLocal(variable) | |
274 | 85 else: |
275 | 86 raise NotImplementedError('{}'.format(sym)) |
308 | 87 self.varMap[sym] = variable |
268 | 88 |
89 self.genCode(fn.body) | |
272 | 90 self.emit(ir.Move(f.return_value, ir.Const(0))) |
269 | 91 self.emit(ir.Jump(f.epiloog)) |
268 | 92 self.setFunction(None) |
158 | 93 |
217 | 94 def genCode(self, code): |
308 | 95 """ Wrapper around gen_stmt to catch errors """ |
307 | 96 try: |
308 | 97 self.gen_stmt(code) |
307 | 98 except SemanticError as e: |
99 self.error(e.msg, e.loc) | |
100 | |
308 | 101 def gen_stmt(self, code): |
102 """ Generate code for a statement """ | |
103 assert isinstance(code, ast.Statement) | |
268 | 104 self.setLoc(code.loc) |
308 | 105 if type(code) is ast.Compound: |
221 | 106 for s in code.statements: |
107 self.genCode(s) | |
308 | 108 elif type(code) is ast.Empty: |
306 | 109 pass |
308 | 110 elif type(code) is ast.Assignment: |
268 | 111 lval = self.genExprCode(code.lval) |
307 | 112 rval = self.genExprCode(code.rval) |
113 if not self.equalTypes(code.lval.typ, code.rval.typ): | |
315 | 114 msg = 'Cannot assign {} to {}'.format(code.rval.typ, code.lval.typ) |
307 | 115 raise SemanticError(msg, code.loc) |
116 if not code.lval.lvalue: | |
117 raise SemanticError('No valid lvalue {}'.format(code.lval), code.lval.loc) | |
268 | 118 self.emit(ir.Move(lval, rval)) |
308 | 119 elif type(code) is ast.ExpressionStatement: |
275 | 120 self.emit(ir.Exp(self.genExprCode(code.ex))) |
308 | 121 elif type(code) is ast.If: |
268 | 122 bbtrue = self.newBlock() |
123 bbfalse = self.newBlock() | |
124 te = self.newBlock() | |
308 | 125 self.gen_cond_code(code.condition, bbtrue, bbfalse) |
268 | 126 self.setBlock(bbtrue) |
127 self.genCode(code.truestatement) | |
128 self.emit(ir.Jump(te)) | |
129 self.setBlock(bbfalse) | |
306 | 130 self.genCode(code.falsestatement) |
268 | 131 self.emit(ir.Jump(te)) |
132 self.setBlock(te) | |
308 | 133 elif type(code) is ast.Return: |
303 | 134 re = self.genExprCode(code.expr) |
135 self.emit(ir.Move(self.fn.return_value, re)) | |
136 self.emit(ir.Jump(self.fn.epiloog)) | |
137 b = self.newBlock() | |
138 self.setBlock(b) | |
308 | 139 elif type(code) is ast.While: |
268 | 140 bbdo = self.newBlock() |
141 bbtest = self.newBlock() | |
142 te = self.newBlock() | |
143 self.emit(ir.Jump(bbtest)) | |
144 self.setBlock(bbtest) | |
308 | 145 self.gen_cond_code(code.condition, bbdo, te) |
268 | 146 self.setBlock(bbdo) |
228 | 147 self.genCode(code.statement) |
268 | 148 self.emit(ir.Jump(bbtest)) |
149 self.setBlock(te) | |
315 | 150 elif type(code) is ast.For: |
151 bbdo = self.newBlock() | |
152 bbtest = self.newBlock() | |
153 te = self.newBlock() | |
154 self.genCode(code.init) | |
155 self.emit(ir.Jump(bbtest)) | |
156 self.setBlock(bbtest) | |
157 self.gen_cond_code(code.condition, bbdo, te) | |
158 self.setBlock(bbdo) | |
159 self.genCode(code.statement) | |
360 | 160 self.genCode(code.final) |
315 | 161 self.emit(ir.Jump(bbtest)) |
162 self.setBlock(te) | |
222 | 163 else: |
268 | 164 raise NotImplementedError('Unknown stmt {}'.format(code)) |
230 | 165 |
308 | 166 def gen_cond_code(self, expr, bbtrue, bbfalse): |
167 """ Generate conditional logic. | |
168 Implement sequential logical operators. """ | |
169 if type(expr) is ast.Binop: | |
268 | 170 if expr.op == 'or': |
171 l2 = self.newBlock() | |
308 | 172 self.gen_cond_code(expr.a, bbtrue, l2) |
173 if not self.equalTypes(expr.a.typ, self.boolType): | |
307 | 174 raise SemanticError('Must be boolean', expr.a.loc) |
268 | 175 self.setBlock(l2) |
308 | 176 self.gen_cond_code(expr.b, bbtrue, bbfalse) |
177 if not self.equalTypes(expr.b.typ, self.boolType): | |
307 | 178 raise SemanticError('Must be boolean', expr.b.loc) |
268 | 179 elif expr.op == 'and': |
180 l2 = self.newBlock() | |
308 | 181 self.gen_cond_code(expr.a, l2, bbfalse) |
182 if not self.equalTypes(expr.a.typ, self.boolType): | |
307 | 183 self.error('Must be boolean', expr.a.loc) |
268 | 184 self.setBlock(l2) |
308 | 185 self.gen_cond_code(expr.b, bbtrue, bbfalse) |
186 if not self.equalTypes(expr.b.typ, self.boolType): | |
307 | 187 raise SemanticError('Must be boolean', expr.b.loc) |
305 | 188 elif expr.op in ['==', '>', '<', '!=', '<=', '>=']: |
228 | 189 ta = self.genExprCode(expr.a) |
190 tb = self.genExprCode(expr.b) | |
307 | 191 if not self.equalTypes(expr.a.typ, expr.b.typ): |
192 raise SemanticError('Types unequal {} != {}' | |
193 .format(expr.a.typ, expr.b.typ), expr.loc) | |
268 | 194 self.emit(ir.CJump(ta, expr.op, tb, bbtrue, bbfalse)) |
195 else: | |
311 | 196 raise SemanticError('non-bool: {}'.format(expr.op), expr.loc) |
307 | 197 expr.typ = self.boolType |
308 | 198 elif type(expr) is ast.Literal: |
199 self.genExprCode(expr) | |
288 | 200 if expr.val: |
268 | 201 self.emit(ir.Jump(bbtrue)) |
288 | 202 else: |
268 | 203 self.emit(ir.Jump(bbfalse)) |
228 | 204 else: |
288 | 205 raise NotImplementedError('Unknown cond {}'.format(expr)) |
354 | 206 |
207 # Check that the condition is a boolean value: | |
307 | 208 if not self.equalTypes(expr.typ, self.boolType): |
209 self.error('Condition must be boolean', expr.loc) | |
230 | 210 |
217 | 211 def genExprCode(self, expr): |
308 | 212 """ Generate code for an expression. Return the generated ir-value """ |
213 assert isinstance(expr, ast.Expression) | |
214 if type(expr) is ast.Binop: | |
307 | 215 expr.lvalue = False |
216 if expr.op in ['+', '-', '*', '/', '<<', '>>', '|', '&']: | |
217 ra = self.genExprCode(expr.a) | |
218 rb = self.genExprCode(expr.b) | |
219 if self.equalTypes(expr.a.typ, self.intType) and \ | |
220 self.equalTypes(expr.b.typ, self.intType): | |
221 expr.typ = expr.a.typ | |
222 else: | |
223 raise SemanticError('Can only add integers', expr.loc) | |
224 else: | |
225 raise NotImplementedError("Cannot use equality as expressions") | |
268 | 226 return ir.Binop(ra, expr.op, rb) |
308 | 227 elif type(expr) is ast.Unop: |
307 | 228 if expr.op == '&': |
229 ra = self.genExprCode(expr.a) | |
308 | 230 expr.typ = ast.PointerType(expr.a.typ) |
307 | 231 if not expr.a.lvalue: |
232 raise SemanticError('No valid lvalue', expr.a.loc) | |
233 expr.lvalue = False | |
234 assert type(ra) is ir.Mem | |
235 return ra.e | |
236 else: | |
237 raise NotImplementedError('Unknown unop {0}'.format(expr.op)) | |
308 | 238 elif type(expr) is ast.Identifier: |
307 | 239 # Generate code for this identifier. |
240 tg = self.resolveSymbol(expr) | |
241 expr.kind = type(tg) | |
242 expr.typ = tg.typ | |
279 | 243 # This returns the dereferenced variable. |
308 | 244 if isinstance(tg, ast.Variable): |
313 | 245 expr.lvalue = True |
307 | 246 return ir.Mem(self.varMap[tg]) |
313 | 247 elif isinstance(tg, ast.Constant): |
248 c_val = self.genExprCode(tg.value) | |
249 return self.evalConst(c_val) | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
250 else: |
308 | 251 raise NotImplementedError(str(tg)) |
252 elif type(expr) is ast.Deref: | |
222 | 253 # dereference pointer type: |
225 | 254 addr = self.genExprCode(expr.ptr) |
316 | 255 ptr_typ = self.the_type(expr.ptr.typ) |
307 | 256 expr.lvalue = True |
308 | 257 if type(ptr_typ) is ast.PointerType: |
307 | 258 expr.typ = ptr_typ.ptype |
259 return ir.Mem(addr) | |
260 else: | |
261 raise SemanticError('Cannot deref non-pointer', expr.loc) | |
308 | 262 elif type(expr) is ast.Member: |
279 | 263 base = self.genExprCode(expr.base) |
307 | 264 expr.lvalue = expr.base.lvalue |
316 | 265 basetype = self.the_type(expr.base.typ) |
308 | 266 if type(basetype) is ast.StructureType: |
307 | 267 if basetype.hasField(expr.field): |
268 expr.typ = basetype.fieldType(expr.field) | |
269 else: | |
270 raise SemanticError('{} does not contain field {}' | |
308 | 271 .format(basetype, expr.field), expr.loc) |
307 | 272 else: |
308 | 273 raise SemanticError('Cannot select {} of non-structure type {}' |
274 .format(expr.field, basetype), expr.loc) | |
307 | 275 |
279 | 276 assert type(base) is ir.Mem, type(base) |
316 | 277 bt = self.the_type(expr.base.typ) |
268 | 278 offset = ir.Const(bt.fieldOffset(expr.field)) |
308 | 279 return ir.Mem(ir.Add(base.e, offset)) |
354 | 280 elif type(expr) is ast.Index: |
281 """ Array indexing """ | |
282 base = self.genExprCode(expr.base) | |
283 idx = self.genExprCode(expr.i) | |
284 base_typ = self.the_type(expr.base.typ) | |
285 if not isinstance(base_typ, ast.ArrayType): | |
286 raise SemanticError('Cannot index non-array type {}'.format(base_typ), expr.base.loc) | |
287 idx_type = self.the_type(expr.i.typ) | |
288 if not self.equalTypes(idx_type, self.intType): | |
289 raise SemanticError('Index must be int not {}'.format(idx_type), expr.i.loc) | |
290 assert type(base) is ir.Mem | |
291 element_type = self.the_type(base_typ.element_type) | |
292 element_size = self.size_of(element_type) | |
293 expr.typ = base_typ.element_type | |
294 expr.lvalue = True | |
295 | |
296 return ir.Mem(ir.Add(base.e, ir.Mul(idx, ir.Const(element_size)))) | |
308 | 297 elif type(expr) is ast.Literal: |
307 | 298 expr.lvalue = False |
313 | 299 typemap = {int: 'int', float: 'double', bool: 'bool', str:'string'} |
307 | 300 if type(expr.val) in typemap: |
301 expr.typ = self.pkg.scope[typemap[type(expr.val)]] | |
302 else: | |
312 | 303 raise SemanticError('Unknown literal type {}'.format(expr.val), expr.loc) |
354 | 304 # Construct correct const value: |
305 if type(expr.val) is str: | |
306 cval = struct.pack('<I', len(expr.val)) + expr.val.encode('ascii') | |
307 return ir.Addr(ir.Const(cval)) | |
308 else: | |
309 return ir.Const(expr.val) | |
308 | 310 elif type(expr) is ast.TypeCast: |
311 return self.gen_type_cast(expr) | |
312 elif type(expr) is ast.FunctionCall: | |
313 return self.gen_function_call(expr) | |
225 | 314 else: |
259 | 315 raise NotImplementedError('Unknown expr {}'.format(expr)) |
307 | 316 |
308 | 317 def gen_type_cast(self, expr): |
318 """ Generate code for type casting """ | |
319 ar = self.genExprCode(expr.a) | |
316 | 320 from_type = self.the_type(expr.a.typ) |
321 to_type = self.the_type(expr.to_type) | |
308 | 322 if isinstance(from_type, ast.PointerType) and isinstance(to_type, ast.PointerType): |
323 expr.typ = expr.to_type | |
324 return ar | |
364 | 325 elif self.equalTypes(self.intType, from_type) and \ |
308 | 326 isinstance(to_type, ast.PointerType): |
327 expr.typ = expr.to_type | |
328 return ar | |
364 | 329 elif self.equalTypes(self.intType, to_type) \ |
330 and isinstance(from_type, ast.PointerType): | |
331 expr.typ = expr.to_type | |
332 return ar | |
354 | 333 elif type(from_type) is ast.BaseType and from_type.name == 'byte' and \ |
334 type(to_type) is ast.BaseType and to_type.name == 'int': | |
335 expr.typ = expr.to_type | |
336 return ar | |
308 | 337 else: |
338 raise SemanticError('Cannot cast {} to {}' | |
339 .format(from_type, to_type), expr.loc) | |
353 | 340 |
308 | 341 def gen_function_call(self, expr): |
342 """ Generate code for a function call """ | |
343 # Evaluate the arguments: | |
344 args = [self.genExprCode(e) for e in expr.args] | |
345 # Check arguments: | |
346 tg = self.resolveSymbol(expr.proc) | |
347 if type(tg) is not ast.Function: | |
348 raise SemanticError('cannot call {}'.format(tg)) | |
349 ftyp = tg.typ | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
334
diff
changeset
|
350 fname = tg.package.name + '_' + tg.name |
308 | 351 ptypes = ftyp.parametertypes |
352 if len(expr.args) != len(ptypes): | |
353 raise SemanticError('{} requires {} arguments, {} given' | |
354 .format(fname, len(ptypes), len(expr.args)), expr.loc) | |
355 for arg, at in zip(expr.args, ptypes): | |
356 if not self.equalTypes(arg.typ, at): | |
357 raise SemanticError('Got {}, expected {}' | |
358 .format(arg.typ, at), arg.loc) | |
359 # determine return type: | |
360 expr.typ = ftyp.returntype | |
361 return ir.Call(fname, args) | |
362 | |
313 | 363 def evalConst(self, c): |
364 if isinstance(c, ir.Const): | |
365 return c | |
366 else: | |
367 raise SemanticError('Cannot evaluate constant {}'.format(c)) | |
368 | |
307 | 369 def resolveSymbol(self, sym): |
308 | 370 if type(sym) is ast.Member: |
307 | 371 base = self.resolveSymbol(sym.base) |
308 | 372 if type(base) is not ast.Package: |
373 raise SemanticError('Base is not a package', sym.loc) | |
307 | 374 scope = base.innerScope |
375 name = sym.field | |
308 | 376 elif type(sym) is ast.Identifier: |
307 | 377 scope = sym.scope |
378 name = sym.target | |
379 else: | |
380 raise NotImplementedError(str(sym)) | |
381 if name in scope: | |
382 s = scope[name] | |
383 else: | |
384 raise SemanticError('{} undefined'.format(name), sym.loc) | |
308 | 385 assert isinstance(s, ast.Symbol) |
307 | 386 return s |
387 | |
316 | 388 def size_of(self, t): |
389 """ Determine the byte size of a type """ | |
390 t = self.the_type(t) | |
391 if type(t) is ast.BaseType: | |
392 return t.bytesize | |
393 elif type(t) is ast.StructureType: | |
394 return sum(self.size_of(mem.typ) for mem in t.mems) | |
354 | 395 elif type(t) is ast.ArrayType: |
396 return t.size * self.size_of(t.element_type) | |
316 | 397 else: |
398 raise NotImplementedError(str(t)) | |
399 | |
400 def the_type(self, t): | |
307 | 401 """ Recurse until a 'real' type is found """ |
308 | 402 if type(t) is ast.DefinedType: |
316 | 403 t = self.the_type(t.typ) |
308 | 404 elif type(t) in [ast.Identifier, ast.Member]: |
316 | 405 t = self.the_type(self.resolveSymbol(t)) |
406 elif type(t) is ast.StructureType: | |
407 # Setup offsets of fields. Is this the right place?: | |
408 offset = 0 | |
409 for mem in t.mems: | |
410 mem.offset = offset | |
411 offset = offset + self.size_of(mem.typ) | |
308 | 412 elif isinstance(t, ast.Type): |
307 | 413 pass |
414 else: | |
415 raise NotImplementedError(str(t)) | |
308 | 416 assert isinstance(t, ast.Type) |
307 | 417 return t |
418 | |
419 def equalTypes(self, a, b): | |
420 """ Compare types a and b for structural equavalence. """ | |
421 # Recurse into named types: | |
316 | 422 a = self.the_type(a) |
423 b = self.the_type(b) | |
307 | 424 |
425 if type(a) is type(b): | |
308 | 426 if type(a) is ast.BaseType: |
307 | 427 return a.name == b.name |
308 | 428 elif type(a) is ast.PointerType: |
307 | 429 return self.equalTypes(a.ptype, b.ptype) |
308 | 430 elif type(a) is ast.StructureType: |
307 | 431 if len(a.mems) != len(b.mems): |
432 return False | |
433 return all(self.equalTypes(am.typ, bm.typ) for am, bm in | |
434 zip(a.mems, b.mems)) | |
354 | 435 elif type(a) is ast.ArrayType: |
436 return self.equalTypes(a.element_type, b.element_type) | |
307 | 437 else: |
438 raise NotImplementedError('{} not implemented'.format(type(a))) | |
439 return False |