diff python/c3/codegenerator.py @ 222:c3f1ce8b638f

Fixup of parser
author Windel Bouwman
date Tue, 09 Jul 2013 17:36:31 +0200
parents 848c4b15fd0b
children 1c7364bd74c7
line wrap: on
line diff
--- a/python/c3/codegenerator.py	Mon Jul 08 22:21:44 2013 +0200
+++ b/python/c3/codegenerator.py	Tue Jul 09 17:36:31 2013 +0200
@@ -1,6 +1,6 @@
 import ir
 from . import astnodes
-from .scope import boolType
+from .scope import boolType, intType
 from ppci import CompilerError
    
 class CodeGenerator:
@@ -17,11 +17,11 @@
 
     # inner helpers:
     def genModule(self, pkg):
-      # Take care of forward declarations:
-      for s in pkg.innerScope.Functions:
+       # Take care of forward declarations:
+       for s in pkg.innerScope.Functions:
             f = self.builder.newFunction(s.name)
             self.funcMap[s] = f
-      for s in pkg.innerScope:
+       for s in pkg.innerScope:
          if type(s) is astnodes.Variable:
               v = self.builder.newVariable(s.name)
               #self.builder.addIns(ir.Alloc(v))
@@ -50,16 +50,19 @@
             print(s)
 
     def genCode(self, code):
-      if type(code) is astnodes.CompoundStatement:
+        assert isinstance(code, astnodes.Statement)
+        if type(code) is astnodes.CompoundStatement:
             for s in code.statements:
                 self.genCode(s)
-      elif type(code) is astnodes.Assignment:
+        elif type(code) is astnodes.Assignment:
          re = self.genExprCode(code.rval)
          # TODO: Handle pointers
          loc = self.genExprCode(code.lval)
          # determine location of variable
          self.builder.addIns(ir.Store(loc, re))
-      elif type(code) is astnodes.IfStatement:
+        elif type(code) is astnodes.ExpressionStatement:
+            self.genExprCode(code.ex)
+        elif type(code) is astnodes.IfStatement:
          bbtrue = self.builder.newBB()
          bbfalse = self.builder.newBB()
          te = self.builder.newBB()
@@ -71,15 +74,15 @@
          self.genCode(code.falsestatement)
          self.builder.addIns(ir.Branch(te))
          self.builder.setBB(te)
-      elif type(code) is astnodes.EmptyStatement:
+        elif type(code) is astnodes.EmptyStatement:
          pass
-      elif type(code) is astnodes.ReturnStatement:
+        elif type(code) is astnodes.ReturnStatement:
          if code.expr:
             re = self.genExprCode(code.expr)
             self.builder.addIns(ir.Return(re))
          else:
             self.builder.addIns(ir.Return())
-      elif type(code) is astnodes.WhileStatement:
+        elif type(code) is astnodes.WhileStatement:
          bbdo = self.builder.newBB()
          bbtest = self.builder.newBB()
          te = self.builder.newBB()
@@ -90,7 +93,7 @@
          self.genCode(code.statement)
          self.builder.addIns(ir.Branch(bbtest))
          self.builder.setBB(te)
-      else:
+        else:
          print('Unknown stmt:', code)
     def genCondCode(self, expr, bbtrue, bbfalse):
       # Implement sequential logical operators
@@ -138,12 +141,18 @@
             # TODO
             return tmp
       elif type(expr) is astnodes.Unop:
+         ra = self.genExprCode(expr.a)
          if expr.op == '&':
             # Address of operator
-            ra = self.genExprCode(expr.a)
             tmp = self.builder.newTmp('addr')
             return tmp
             #self.builder.addIns(ins)
+         elif expr.op == '*':
+            # dereference pointer type:
+            tmp = self.builder.newTmp('deref')
+            ins = ir.Load(ra, tmp)
+            self.builder.addIns(ins)
+            return tmp
          else:
             print('Unknown {0}'.format(expr))
       elif type(expr) is astnodes.Constant:
@@ -161,6 +170,15 @@
          ins = ir.ImmLoad(tmp, expr.val)
          self.builder.addIns(ins)
          return tmp
+      elif type(expr) is astnodes.TypeCast:
+            ar = self.genExprCode(expr.a)
+            if isinstance(expr.to_type, astnodes.PointerType):
+                if expr.a.typ is intType:
+                    return ar
+                elif isinstance(expr.a.typ, astnodes.PointerType):
+                    return ar
+                else:
+                    raise Exception()
       elif type(expr) is astnodes.FunctionCall:
          tmp = self.builder.newTmp("res")
          args = []