Mercurial > lcfOS
diff python/ppci/c3/codegenerator.py @ 316:56e6ff84f646
Fixed burn led demo
author | Windel Bouwman |
---|---|
date | Sat, 21 Dec 2013 13:13:26 +0100 |
parents | 084cccaa5deb |
children | 6f4753202b9a |
line wrap: on
line diff
--- a/python/ppci/c3/codegenerator.py Sat Dec 21 10:03:01 2013 +0100 +++ b/python/ppci/c3/codegenerator.py Sat Dec 21 13:13:26 2013 +0100 @@ -55,10 +55,6 @@ self.pkg.ok = False self.diag.error(msg, loc) - def checkType(self, t): - """ Verify the type is correct """ - t = self.theType(t) - def gen_function(self, fn): # TODO: handle arguments f = self.funcMap[fn] @@ -70,11 +66,14 @@ # generate room for locals: for sym in fn.innerScope: - # TODO: handle parameters different - self.checkType(sym.typ) + self.the_type(sym.typ) if sym.isParameter: - variable = ir.Parameter(sym.name) - f.addParameter(variable) + p = ir.Parameter(sym.name) + variable = ir.LocalVariable(sym.name + '_copy') + f.addParameter(p) + f.addLocal(variable) + # Move parameter into local copy: + self.emit(ir.Move(ir.Mem(variable), p)) elif sym.isLocal: variable = ir.LocalVariable(sym.name) f.addLocal(variable) @@ -248,7 +247,7 @@ elif type(expr) is ast.Deref: # dereference pointer type: addr = self.genExprCode(expr.ptr) - ptr_typ = self.theType(expr.ptr.typ) + ptr_typ = self.the_type(expr.ptr.typ) expr.lvalue = True if type(ptr_typ) is ast.PointerType: expr.typ = ptr_typ.ptype @@ -258,7 +257,7 @@ elif type(expr) is ast.Member: base = self.genExprCode(expr.base) expr.lvalue = expr.base.lvalue - basetype = self.theType(expr.base.typ) + basetype = self.the_type(expr.base.typ) if type(basetype) is ast.StructureType: if basetype.hasField(expr.field): expr.typ = basetype.fieldType(expr.field) @@ -270,7 +269,7 @@ .format(expr.field, basetype), expr.loc) assert type(base) is ir.Mem, type(base) - bt = self.theType(expr.base.typ) + bt = self.the_type(expr.base.typ) offset = ir.Const(bt.fieldOffset(expr.field)) return ir.Mem(ir.Add(base.e, offset)) elif type(expr) is ast.Literal: @@ -291,8 +290,8 @@ def gen_type_cast(self, expr): """ Generate code for type casting """ ar = self.genExprCode(expr.a) - from_type = self.theType(expr.a.typ) - to_type = self.theType(expr.to_type) + from_type = self.the_type(expr.a.typ) + to_type = self.the_type(expr.to_type) if isinstance(from_type, ast.PointerType) and isinstance(to_type, ast.PointerType): expr.typ = expr.to_type return ar @@ -351,12 +350,28 @@ assert isinstance(s, ast.Symbol) return s - def theType(self, t): + def size_of(self, t): + """ Determine the byte size of a type """ + t = self.the_type(t) + if type(t) is ast.BaseType: + return t.bytesize + elif type(t) is ast.StructureType: + return sum(self.size_of(mem.typ) for mem in t.mems) + else: + raise NotImplementedError(str(t)) + + def the_type(self, t): """ Recurse until a 'real' type is found """ if type(t) is ast.DefinedType: - t = self.theType(t.typ) + t = self.the_type(t.typ) elif type(t) in [ast.Identifier, ast.Member]: - t = self.theType(self.resolveSymbol(t)) + t = self.the_type(self.resolveSymbol(t)) + elif type(t) is ast.StructureType: + # Setup offsets of fields. Is this the right place?: + offset = 0 + for mem in t.mems: + mem.offset = offset + offset = offset + self.size_of(mem.typ) elif isinstance(t, ast.Type): pass else: @@ -367,10 +382,8 @@ def equalTypes(self, a, b): """ Compare types a and b for structural equavalence. """ # Recurse into named types: - a = self.theType(a) - b = self.theType(b) - assert isinstance(a, ast.Type) - assert isinstance(b, ast.Type) + a = self.the_type(a) + b = self.the_type(b) if type(a) is type(b): if type(a) is ast.BaseType: