diff python/ppci/c3/codegenerator.py @ 313:04cf4d26a3bc

Added constant function
author Windel Bouwman
date Wed, 18 Dec 2013 18:02:26 +0100
parents 2c9768114877
children 084cccaa5deb
line wrap: on
line diff
--- a/python/ppci/c3/codegenerator.py	Mon Dec 16 17:58:15 2013 +0100
+++ b/python/ppci/c3/codegenerator.py	Wed Dec 18 18:02:26 2013 +0100
@@ -34,7 +34,7 @@
         self.pkg = pkg
         self.intType = pkg.scope['int']
         self.boolType = pkg.scope['bool']
-        self.logger.info('Generating ir-code for {}'.format(pkg.name))
+        self.logger.info('Generating ir-code for {}'.format(pkg.name), extra={'c3_ast':pkg})
         self.varMap = {}    # Maps variables to storage locations.
         self.funcMap = {}
         self.m = ir.Module(pkg.name)
@@ -48,7 +48,8 @@
                 self.gen_function(s)
         except SemanticError as e:
             self.error(e.msg, e.loc)
-        return self.m
+        if self.pkg.ok:
+            return self.m
 
     def error(self, msg, loc=None):
         self.pkg.ok = False
@@ -70,16 +71,14 @@
 
         for sym in fn.innerScope:
             # TODO: handle parameters different
+            self.checkType(sym.typ)
             if sym.isParameter:
-                self.checkType(sym.typ)
                 variable = ir.Parameter(sym.name)
                 f.addParameter(variable)
             elif sym.isLocal:
-                self.checkType(sym.typ)
                 variable = ir.LocalVariable(sym.name)
                 f.addLocal(variable)
             elif isinstance(sym, ast.Variable):
-                self.checkType(sym.typ)
                 variable = ir.LocalVariable(sym.name)
                 f.addLocal(variable)
             else:
@@ -222,13 +221,16 @@
                 raise NotImplementedError('Unknown unop {0}'.format(expr.op))
         elif type(expr) is ast.Identifier:
             # Generate code for this identifier.
-            expr.lvalue = True
             tg = self.resolveSymbol(expr)
             expr.kind = type(tg)
             expr.typ = tg.typ
             # This returns the dereferenced variable.
             if isinstance(tg, ast.Variable):
+                expr.lvalue = True
                 return ir.Mem(self.varMap[tg])
+            elif isinstance(tg, ast.Constant):
+                c_val = self.genExprCode(tg.value)
+                return self.evalConst(c_val)
             else:
                 raise NotImplementedError(str(tg))
         elif type(expr) is ast.Deref:
@@ -261,7 +263,7 @@
             return ir.Mem(ir.Add(base.e, offset))
         elif type(expr) is ast.Literal:
             expr.lvalue = False
-            typemap = {int: 'int', float: 'double', bool: 'bool'}
+            typemap = {int: 'int', float: 'double', bool: 'bool', str:'string'}
             if type(expr.val) in typemap:
                 expr.typ = self.pkg.scope[typemap[type(expr.val)]]
             else:
@@ -312,6 +314,12 @@
         expr.typ = ftyp.returntype
         return ir.Call(fname, args)
 
+    def evalConst(self, c):
+        if isinstance(c, ir.Const):
+            return c
+        else:
+            raise SemanticError('Cannot evaluate constant {}'.format(c))
+
     def resolveSymbol(self, sym):
         if type(sym) is ast.Member:
             base = self.resolveSymbol(sym.base)