Mercurial > lcfOS
annotate python/ppci/c3/codegenerator.py @ 392:bb4289c84907
Added some sort of drop event test
author | Windel Bouwman |
---|---|
date | Fri, 16 May 2014 13:05:10 +0200 |
parents | 2ec730e45ea1 |
children | 6ae782a085e0 |
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'] | |
389 | 38 self.pointerSize = 4 |
39 self.logger.debug('Generating ir-code for {}'.format(pkg.name), | |
40 extra={'c3_ast': pkg}) | |
288 | 41 self.varMap = {} # Maps variables to storage locations. |
268 | 42 self.m = ir.Module(pkg.name) |
307 | 43 try: |
389 | 44 for typ in pkg.Types: |
45 self.check_type(typ) | |
362 | 46 # Only generate function if function contains a body: |
389 | 47 real_functions = list(filter( |
48 lambda f: f.body, pkg.innerScope.Functions)) | |
307 | 49 for v in pkg.innerScope.Variables: |
364 | 50 v2 = ir.GlobalVariable(v.name) |
51 self.varMap[v] = v2 | |
363 | 52 if not v.isLocal: |
364 | 53 self.m.add_variable(v2) |
362 | 54 for s in real_functions: |
308 | 55 self.gen_function(s) |
307 | 56 except SemanticError as e: |
57 self.error(e.msg, e.loc) | |
313 | 58 if self.pkg.ok: |
59 return self.m | |
308 | 60 |
61 def error(self, msg, loc=None): | |
62 self.pkg.ok = False | |
63 self.diag.error(msg, loc) | |
307 | 64 |
308 | 65 def gen_function(self, fn): |
268 | 66 # TODO: handle arguments |
363 | 67 f = self.newFunction(fn.name) |
272 | 68 f.return_value = self.newTemp() |
268 | 69 self.setFunction(f) |
269 | 70 l2 = self.newBlock() |
71 self.emit(ir.Jump(l2)) | |
72 self.setBlock(l2) | |
268 | 73 # generate room for locals: |
174 | 74 |
268 | 75 for sym in fn.innerScope: |
389 | 76 self.check_type(sym.typ) |
272 | 77 if sym.isParameter: |
316 | 78 p = ir.Parameter(sym.name) |
79 variable = ir.LocalVariable(sym.name + '_copy') | |
80 f.addParameter(p) | |
81 f.addLocal(variable) | |
82 # Move parameter into local copy: | |
83 self.emit(ir.Move(ir.Mem(variable), p)) | |
274 | 84 elif sym.isLocal: |
308 | 85 variable = ir.LocalVariable(sym.name) |
86 f.addLocal(variable) | |
87 elif isinstance(sym, ast.Variable): | |
88 variable = ir.LocalVariable(sym.name) | |
89 f.addLocal(variable) | |
274 | 90 else: |
275 | 91 raise NotImplementedError('{}'.format(sym)) |
308 | 92 self.varMap[sym] = variable |
268 | 93 |
94 self.genCode(fn.body) | |
272 | 95 self.emit(ir.Move(f.return_value, ir.Const(0))) |
269 | 96 self.emit(ir.Jump(f.epiloog)) |
268 | 97 self.setFunction(None) |
158 | 98 |
217 | 99 def genCode(self, code): |
308 | 100 """ Wrapper around gen_stmt to catch errors """ |
307 | 101 try: |
308 | 102 self.gen_stmt(code) |
307 | 103 except SemanticError as e: |
104 self.error(e.msg, e.loc) | |
105 | |
308 | 106 def gen_stmt(self, code): |
107 """ Generate code for a statement """ | |
108 assert isinstance(code, ast.Statement) | |
268 | 109 self.setLoc(code.loc) |
308 | 110 if type(code) is ast.Compound: |
221 | 111 for s in code.statements: |
112 self.genCode(s) | |
308 | 113 elif type(code) is ast.Empty: |
306 | 114 pass |
308 | 115 elif type(code) is ast.Assignment: |
268 | 116 lval = self.genExprCode(code.lval) |
307 | 117 rval = self.genExprCode(code.rval) |
389 | 118 if not self.equal_types(code.lval.typ, code.rval.typ): |
119 raise SemanticError('Cannot assign {} to {}' | |
120 .format(code.rval.typ, code.lval.typ), | |
121 code.loc) | |
307 | 122 if not code.lval.lvalue: |
389 | 123 raise SemanticError('No valid lvalue {}'.format(code.lval), |
124 code.lval.loc) | |
268 | 125 self.emit(ir.Move(lval, rval)) |
308 | 126 elif type(code) is ast.ExpressionStatement: |
275 | 127 self.emit(ir.Exp(self.genExprCode(code.ex))) |
308 | 128 elif type(code) is ast.If: |
268 | 129 bbtrue = self.newBlock() |
130 bbfalse = self.newBlock() | |
131 te = self.newBlock() | |
308 | 132 self.gen_cond_code(code.condition, bbtrue, bbfalse) |
268 | 133 self.setBlock(bbtrue) |
134 self.genCode(code.truestatement) | |
135 self.emit(ir.Jump(te)) | |
136 self.setBlock(bbfalse) | |
306 | 137 self.genCode(code.falsestatement) |
268 | 138 self.emit(ir.Jump(te)) |
139 self.setBlock(te) | |
308 | 140 elif type(code) is ast.Return: |
303 | 141 re = self.genExprCode(code.expr) |
142 self.emit(ir.Move(self.fn.return_value, re)) | |
143 self.emit(ir.Jump(self.fn.epiloog)) | |
144 b = self.newBlock() | |
145 self.setBlock(b) | |
308 | 146 elif type(code) is ast.While: |
268 | 147 bbdo = self.newBlock() |
148 bbtest = self.newBlock() | |
149 te = self.newBlock() | |
150 self.emit(ir.Jump(bbtest)) | |
151 self.setBlock(bbtest) | |
308 | 152 self.gen_cond_code(code.condition, bbdo, te) |
268 | 153 self.setBlock(bbdo) |
228 | 154 self.genCode(code.statement) |
268 | 155 self.emit(ir.Jump(bbtest)) |
156 self.setBlock(te) | |
315 | 157 elif type(code) is ast.For: |
158 bbdo = self.newBlock() | |
159 bbtest = self.newBlock() | |
160 te = self.newBlock() | |
161 self.genCode(code.init) | |
162 self.emit(ir.Jump(bbtest)) | |
163 self.setBlock(bbtest) | |
164 self.gen_cond_code(code.condition, bbdo, te) | |
165 self.setBlock(bbdo) | |
166 self.genCode(code.statement) | |
360 | 167 self.genCode(code.final) |
315 | 168 self.emit(ir.Jump(bbtest)) |
169 self.setBlock(te) | |
222 | 170 else: |
268 | 171 raise NotImplementedError('Unknown stmt {}'.format(code)) |
230 | 172 |
308 | 173 def gen_cond_code(self, expr, bbtrue, bbfalse): |
174 """ Generate conditional logic. | |
175 Implement sequential logical operators. """ | |
176 if type(expr) is ast.Binop: | |
268 | 177 if expr.op == 'or': |
178 l2 = self.newBlock() | |
308 | 179 self.gen_cond_code(expr.a, bbtrue, l2) |
389 | 180 if not self.equal_types(expr.a.typ, self.boolType): |
307 | 181 raise SemanticError('Must be boolean', expr.a.loc) |
268 | 182 self.setBlock(l2) |
308 | 183 self.gen_cond_code(expr.b, bbtrue, bbfalse) |
389 | 184 if not self.equal_types(expr.b.typ, self.boolType): |
307 | 185 raise SemanticError('Must be boolean', expr.b.loc) |
268 | 186 elif expr.op == 'and': |
187 l2 = self.newBlock() | |
308 | 188 self.gen_cond_code(expr.a, l2, bbfalse) |
389 | 189 if not self.equal_types(expr.a.typ, self.boolType): |
307 | 190 self.error('Must be boolean', expr.a.loc) |
268 | 191 self.setBlock(l2) |
308 | 192 self.gen_cond_code(expr.b, bbtrue, bbfalse) |
389 | 193 if not self.equal_types(expr.b.typ, self.boolType): |
307 | 194 raise SemanticError('Must be boolean', expr.b.loc) |
305 | 195 elif expr.op in ['==', '>', '<', '!=', '<=', '>=']: |
228 | 196 ta = self.genExprCode(expr.a) |
197 tb = self.genExprCode(expr.b) | |
389 | 198 if not self.equal_types(expr.a.typ, expr.b.typ): |
307 | 199 raise SemanticError('Types unequal {} != {}' |
389 | 200 .format(expr.a.typ, expr.b.typ), |
201 expr.loc) | |
268 | 202 self.emit(ir.CJump(ta, expr.op, tb, bbtrue, bbfalse)) |
203 else: | |
311 | 204 raise SemanticError('non-bool: {}'.format(expr.op), expr.loc) |
307 | 205 expr.typ = self.boolType |
308 | 206 elif type(expr) is ast.Literal: |
207 self.genExprCode(expr) | |
288 | 208 if expr.val: |
268 | 209 self.emit(ir.Jump(bbtrue)) |
288 | 210 else: |
268 | 211 self.emit(ir.Jump(bbfalse)) |
228 | 212 else: |
288 | 213 raise NotImplementedError('Unknown cond {}'.format(expr)) |
354 | 214 |
215 # Check that the condition is a boolean value: | |
389 | 216 if not self.equal_types(expr.typ, self.boolType): |
307 | 217 self.error('Condition must be boolean', expr.loc) |
230 | 218 |
217 | 219 def genExprCode(self, expr): |
308 | 220 """ Generate code for an expression. Return the generated ir-value """ |
221 assert isinstance(expr, ast.Expression) | |
222 if type(expr) is ast.Binop: | |
307 | 223 expr.lvalue = False |
224 if expr.op in ['+', '-', '*', '/', '<<', '>>', '|', '&']: | |
225 ra = self.genExprCode(expr.a) | |
226 rb = self.genExprCode(expr.b) | |
389 | 227 if self.equal_types(expr.a.typ, self.intType) and \ |
228 self.equal_types(expr.b.typ, self.intType): | |
307 | 229 expr.typ = expr.a.typ |
230 else: | |
231 raise SemanticError('Can only add integers', expr.loc) | |
232 else: | |
233 raise NotImplementedError("Cannot use equality as expressions") | |
268 | 234 return ir.Binop(ra, expr.op, rb) |
308 | 235 elif type(expr) is ast.Unop: |
307 | 236 if expr.op == '&': |
237 ra = self.genExprCode(expr.a) | |
308 | 238 expr.typ = ast.PointerType(expr.a.typ) |
307 | 239 if not expr.a.lvalue: |
240 raise SemanticError('No valid lvalue', expr.a.loc) | |
241 expr.lvalue = False | |
242 assert type(ra) is ir.Mem | |
243 return ra.e | |
244 else: | |
245 raise NotImplementedError('Unknown unop {0}'.format(expr.op)) | |
308 | 246 elif type(expr) is ast.Identifier: |
307 | 247 # Generate code for this identifier. |
248 tg = self.resolveSymbol(expr) | |
249 expr.kind = type(tg) | |
250 expr.typ = tg.typ | |
279 | 251 # This returns the dereferenced variable. |
308 | 252 if isinstance(tg, ast.Variable): |
313 | 253 expr.lvalue = True |
307 | 254 return ir.Mem(self.varMap[tg]) |
313 | 255 elif isinstance(tg, ast.Constant): |
256 c_val = self.genExprCode(tg.value) | |
257 return self.evalConst(c_val) | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
258 else: |
308 | 259 raise NotImplementedError(str(tg)) |
260 elif type(expr) is ast.Deref: | |
222 | 261 # dereference pointer type: |
225 | 262 addr = self.genExprCode(expr.ptr) |
316 | 263 ptr_typ = self.the_type(expr.ptr.typ) |
307 | 264 expr.lvalue = True |
308 | 265 if type(ptr_typ) is ast.PointerType: |
307 | 266 expr.typ = ptr_typ.ptype |
267 return ir.Mem(addr) | |
268 else: | |
269 raise SemanticError('Cannot deref non-pointer', expr.loc) | |
308 | 270 elif type(expr) is ast.Member: |
279 | 271 base = self.genExprCode(expr.base) |
307 | 272 expr.lvalue = expr.base.lvalue |
316 | 273 basetype = self.the_type(expr.base.typ) |
308 | 274 if type(basetype) is ast.StructureType: |
307 | 275 if basetype.hasField(expr.field): |
276 expr.typ = basetype.fieldType(expr.field) | |
277 else: | |
278 raise SemanticError('{} does not contain field {}' | |
389 | 279 .format(basetype, expr.field), |
280 expr.loc) | |
307 | 281 else: |
308 | 282 raise SemanticError('Cannot select {} of non-structure type {}' |
283 .format(expr.field, basetype), expr.loc) | |
307 | 284 |
279 | 285 assert type(base) is ir.Mem, type(base) |
316 | 286 bt = self.the_type(expr.base.typ) |
268 | 287 offset = ir.Const(bt.fieldOffset(expr.field)) |
308 | 288 return ir.Mem(ir.Add(base.e, offset)) |
354 | 289 elif type(expr) is ast.Index: |
290 """ Array indexing """ | |
291 base = self.genExprCode(expr.base) | |
292 idx = self.genExprCode(expr.i) | |
293 base_typ = self.the_type(expr.base.typ) | |
294 if not isinstance(base_typ, ast.ArrayType): | |
389 | 295 raise SemanticError('Cannot index non-array type {}' |
296 .format(base_typ), | |
297 expr.base.loc) | |
354 | 298 idx_type = self.the_type(expr.i.typ) |
389 | 299 if not self.equal_types(idx_type, self.intType): |
300 raise SemanticError('Index must be int not {}' | |
301 .format(idx_type), expr.i.loc) | |
354 | 302 assert type(base) is ir.Mem |
303 element_type = self.the_type(base_typ.element_type) | |
304 element_size = self.size_of(element_type) | |
305 expr.typ = base_typ.element_type | |
306 expr.lvalue = True | |
307 | |
308 return ir.Mem(ir.Add(base.e, ir.Mul(idx, ir.Const(element_size)))) | |
308 | 309 elif type(expr) is ast.Literal: |
307 | 310 expr.lvalue = False |
389 | 311 typemap = {int: 'int', |
312 float: 'double', | |
313 bool: 'bool', | |
314 str: 'string'} | |
307 | 315 if type(expr.val) in typemap: |
316 expr.typ = self.pkg.scope[typemap[type(expr.val)]] | |
317 else: | |
389 | 318 raise SemanticError('Unknown literal type {}' |
319 .format(expr.val), expr.loc) | |
354 | 320 # Construct correct const value: |
321 if type(expr.val) is str: | |
389 | 322 cval = self.pack_string(expr.val) |
354 | 323 return ir.Addr(ir.Const(cval)) |
324 else: | |
325 return ir.Const(expr.val) | |
308 | 326 elif type(expr) is ast.TypeCast: |
327 return self.gen_type_cast(expr) | |
328 elif type(expr) is ast.FunctionCall: | |
329 return self.gen_function_call(expr) | |
225 | 330 else: |
259 | 331 raise NotImplementedError('Unknown expr {}'.format(expr)) |
307 | 332 |
389 | 333 def pack_string(self, txt): |
334 """ Pack a string using 4 bytes length followed by text data """ | |
335 length = struct.pack('<I', len(txt)) | |
336 data = txt.encode('ascii') | |
337 return length + data | |
338 | |
308 | 339 def gen_type_cast(self, expr): |
340 """ Generate code for type casting """ | |
341 ar = self.genExprCode(expr.a) | |
316 | 342 from_type = self.the_type(expr.a.typ) |
343 to_type = self.the_type(expr.to_type) | |
389 | 344 if isinstance(from_type, ast.PointerType) and \ |
308 | 345 isinstance(to_type, ast.PointerType): |
346 expr.typ = expr.to_type | |
347 return ar | |
389 | 348 elif self.equal_types(self.intType, from_type) and \ |
349 isinstance(to_type, ast.PointerType): | |
350 expr.typ = expr.to_type | |
351 return ar | |
352 elif self.equal_types(self.intType, to_type) \ | |
364 | 353 and isinstance(from_type, ast.PointerType): |
354 expr.typ = expr.to_type | |
355 return ar | |
354 | 356 elif type(from_type) is ast.BaseType and from_type.name == 'byte' and \ |
357 type(to_type) is ast.BaseType and to_type.name == 'int': | |
358 expr.typ = expr.to_type | |
359 return ar | |
308 | 360 else: |
361 raise SemanticError('Cannot cast {} to {}' | |
362 .format(from_type, to_type), expr.loc) | |
353 | 363 |
308 | 364 def gen_function_call(self, expr): |
365 """ Generate code for a function call """ | |
366 # Evaluate the arguments: | |
367 args = [self.genExprCode(e) for e in expr.args] | |
368 # Check arguments: | |
369 tg = self.resolveSymbol(expr.proc) | |
370 if type(tg) is not ast.Function: | |
371 raise SemanticError('cannot call {}'.format(tg)) | |
372 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
|
373 fname = tg.package.name + '_' + tg.name |
308 | 374 ptypes = ftyp.parametertypes |
375 if len(expr.args) != len(ptypes): | |
376 raise SemanticError('{} requires {} arguments, {} given' | |
389 | 377 .format(fname, len(ptypes), len(expr.args)), |
378 expr.loc) | |
308 | 379 for arg, at in zip(expr.args, ptypes): |
389 | 380 if not self.equal_types(arg.typ, at): |
308 | 381 raise SemanticError('Got {}, expected {}' |
389 | 382 .format(arg.typ, at), arg.loc) |
308 | 383 # determine return type: |
384 expr.typ = ftyp.returntype | |
385 return ir.Call(fname, args) | |
386 | |
313 | 387 def evalConst(self, c): |
388 if isinstance(c, ir.Const): | |
389 return c | |
390 else: | |
391 raise SemanticError('Cannot evaluate constant {}'.format(c)) | |
392 | |
307 | 393 def resolveSymbol(self, sym): |
308 | 394 if type(sym) is ast.Member: |
307 | 395 base = self.resolveSymbol(sym.base) |
308 | 396 if type(base) is not ast.Package: |
397 raise SemanticError('Base is not a package', sym.loc) | |
307 | 398 scope = base.innerScope |
399 name = sym.field | |
308 | 400 elif type(sym) is ast.Identifier: |
307 | 401 scope = sym.scope |
402 name = sym.target | |
403 else: | |
404 raise NotImplementedError(str(sym)) | |
405 if name in scope: | |
406 s = scope[name] | |
407 else: | |
408 raise SemanticError('{} undefined'.format(name), sym.loc) | |
308 | 409 assert isinstance(s, ast.Symbol) |
307 | 410 return s |
411 | |
316 | 412 def size_of(self, t): |
413 """ Determine the byte size of a type """ | |
414 t = self.the_type(t) | |
415 if type(t) is ast.BaseType: | |
416 return t.bytesize | |
417 elif type(t) is ast.StructureType: | |
418 return sum(self.size_of(mem.typ) for mem in t.mems) | |
354 | 419 elif type(t) is ast.ArrayType: |
420 return t.size * self.size_of(t.element_type) | |
389 | 421 elif type(t) is ast.PointerType: |
422 return self.pointerSize | |
316 | 423 else: |
424 raise NotImplementedError(str(t)) | |
425 | |
389 | 426 def the_type(self, t, reveil_defined=True): |
427 """ Recurse until a 'real' type is found | |
428 When reveil_defined is True, defined types are resolved to | |
429 their backing types. | |
430 """ | |
308 | 431 if type(t) is ast.DefinedType: |
389 | 432 if reveil_defined: |
433 t = self.the_type(t.typ) | |
308 | 434 elif type(t) in [ast.Identifier, ast.Member]: |
389 | 435 t = self.the_type(self.resolveSymbol(t), reveil_defined) |
308 | 436 elif isinstance(t, ast.Type): |
307 | 437 pass |
438 else: | |
439 raise NotImplementedError(str(t)) | |
308 | 440 assert isinstance(t, ast.Type) |
307 | 441 return t |
442 | |
389 | 443 def equal_types(self, a, b, byname=False): |
444 """ Compare types a and b for structural equavalence. | |
445 if byname is True stop on defined types. | |
446 """ | |
307 | 447 # Recurse into named types: |
389 | 448 a = self.the_type(a, not byname) |
449 b = self.the_type(b, not byname) | |
307 | 450 |
389 | 451 # Check types for sanity: |
452 self.check_type(a) | |
453 self.check_type(b) | |
454 | |
455 # Do structural equivalence check: | |
307 | 456 if type(a) is type(b): |
308 | 457 if type(a) is ast.BaseType: |
307 | 458 return a.name == b.name |
308 | 459 elif type(a) is ast.PointerType: |
389 | 460 # If a pointed type is detected, stop structural |
461 # equivalence: | |
462 return self.equal_types(a.ptype, b.ptype, byname=True) | |
308 | 463 elif type(a) is ast.StructureType: |
307 | 464 if len(a.mems) != len(b.mems): |
465 return False | |
389 | 466 return all(self.equal_types(am.typ, bm.typ) for am, bm in |
307 | 467 zip(a.mems, b.mems)) |
354 | 468 elif type(a) is ast.ArrayType: |
389 | 469 return self.equal_types(a.element_type, b.element_type) |
470 elif type(a) is ast.DefinedType: | |
471 # Try by name in case of defined types: | |
472 return a.name == b.name | |
307 | 473 else: |
474 raise NotImplementedError('{} not implemented'.format(type(a))) | |
475 return False | |
389 | 476 |
477 def check_type(self, t, first=True, byname=False): | |
478 """ Determine struct offsets and check for recursiveness by using | |
479 mark and sweep algorithm. | |
480 The calling function could call this function with first set | |
481 to clear the marks. | |
482 """ | |
483 | |
484 # Reset the mark and sweep: | |
485 if first: | |
486 self.got_types = set() | |
487 | |
488 # Resolve the type: | |
489 t = self.the_type(t, not byname) | |
490 | |
491 # Check for recursion: | |
492 if t in self.got_types: | |
493 raise SemanticError('Recursive data type {}'.format(t), None) | |
494 | |
495 if type(t) is ast.BaseType: | |
496 pass | |
497 elif type(t) is ast.PointerType: | |
498 # If a pointed type is detected, stop structural | |
499 # equivalence: | |
500 self.check_type(t.ptype, first=False, byname=True) | |
501 elif type(t) is ast.StructureType: | |
502 self.got_types.add(t) | |
503 # Setup offsets of fields. Is this the right place?: | |
504 offset = 0 | |
505 for struct_member in t.mems: | |
506 self.check_type(struct_member.typ, first=False) | |
507 struct_member.offset = offset | |
508 offset = offset + self.size_of(struct_member.typ) | |
509 elif type(t) is ast.ArrayType: | |
510 self.check_type(t.element_type, first=False) | |
511 elif type(t) is ast.DefinedType: | |
512 pass | |
513 else: | |
514 raise NotImplementedError('{} not implemented'.format(type(t))) |