diff python/c3/codegenerator.py @ 225:1c7364bd74c7

Fixed pointer deref
author Windel Bouwman
date Thu, 11 Jul 2013 07:42:30 +0200
parents c3f1ce8b638f
children 82dfe6a32717
line wrap: on
line diff
--- a/python/c3/codegenerator.py	Tue Jul 09 17:59:15 2013 +0200
+++ b/python/c3/codegenerator.py	Thu Jul 11 07:42:30 2013 +0200
@@ -124,7 +124,7 @@
       else:
          print('Unknown cond', expr)
     def genExprCode(self, expr):
-      if type(expr) is astnodes.Binop:
+        if type(expr) is astnodes.Binop:
          ra = self.genExprCode(expr.a)
          rb = self.genExprCode(expr.b)
          ops = ['+', '-', '*', '/', '|', '&']
@@ -140,37 +140,38 @@
             tmp = self.builder.newTmp()
             # TODO
             return tmp
-      elif type(expr) is astnodes.Unop:
-         ra = self.genExprCode(expr.a)
-         if expr.op == '&':
-            # Address of operator
-            tmp = self.builder.newTmp('addr')
+        elif type(expr) is astnodes.Unop:
+            ra = self.genExprCode(expr.a)
+            if expr.op == '&':
+                # Address of operator
+                tmp = self.builder.newTmp('addr')
+                return tmp
+                #self.builder.addIns(ins)
+            else:
+                raise Exception('Unknown {0}'.format(expr))
+        elif type(expr) is astnodes.Constant:
+            tmp = self.builder.newTmp()
+            # TODO
             return tmp
-            #self.builder.addIns(ins)
-         elif expr.op == '*':
+        elif type(expr) is astnodes.VariableUse:
+            tmp = self.builder.newTmp()
+            loc = self.varMap[expr.target]
+            i = ir.Load(loc, tmp)
+            self.builder.addIns(i)
+            return tmp
+        elif type(expr) is astnodes.Deref:
             # dereference pointer type:
+            addr = self.genExprCode(expr.ptr)
             tmp = self.builder.newTmp('deref')
-            ins = ir.Load(ra, tmp)
+            ins = ir.Load(addr, tmp)
             self.builder.addIns(ins)
             return tmp
-         else:
-            print('Unknown {0}'.format(expr))
-      elif type(expr) is astnodes.Constant:
-         tmp = self.builder.newTmp()
-         # TODO
-         return tmp
-      elif type(expr) is astnodes.VariableUse:
-         tmp = self.builder.newTmp()
-         loc = self.varMap[expr.target]
-         i = ir.Load(loc, tmp)
-         self.builder.addIns(i)
-         return tmp
-      elif type(expr) is astnodes.Literal:
-         tmp = self.builder.newTmp()
-         ins = ir.ImmLoad(tmp, expr.val)
-         self.builder.addIns(ins)
-         return tmp
-      elif type(expr) is astnodes.TypeCast:
+        elif type(expr) is astnodes.Literal:
+            tmp = self.builder.newTmp()
+            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:
@@ -179,7 +180,7 @@
                     return ar
                 else:
                     raise Exception()
-      elif type(expr) is astnodes.FunctionCall:
+        elif type(expr) is astnodes.FunctionCall:
          tmp = self.builder.newTmp("res")
          args = []
          for arg in expr.args:
@@ -189,6 +190,6 @@
          ins = ir.Call(fn, args, tmp)
          self.builder.addIns(ins)
          return tmp
-      else:
+        else:
          raise CompilerError('Unknown expr {}'.format(expr))