diff python/c3/codegenerator.py @ 272:e64bae57cda8

refactor ir
author Windel Bouwman
date Sat, 31 Aug 2013 17:58:54 +0200
parents 5f8c04a8d26b
children ea93e0a7a31e
line wrap: on
line diff
--- a/python/c3/codegenerator.py	Tue Aug 20 18:56:02 2013 +0200
+++ b/python/c3/codegenerator.py	Sat Aug 31 17:58:54 2013 +0200
@@ -27,19 +27,16 @@
         for s in pkg.innerScope.Functions:
             f = self.newFunction(s.name)
             self.funcMap[s] = f
-        for s in pkg.innerScope:
-            if type(s) is astnodes.Variable:
-                #v = self.builder.newTmp(s.name)
-                self.varMap[s] = self.newTemp()
-                pass
-            elif type(s) is astnodes.Function:
-                self.genFunction(s)
-            else:
-                raise NotImplementedError(str(s))
+        for v in pkg.innerScope.Variables:
+            #print(v)
+            self.varMap[v] = self.newTemp()
+        for s in pkg.innerScope.Functions:
+            self.genFunction(s)
 
     def genFunction(self, fn):
         # TODO: handle arguments
         f = self.funcMap[fn]
+        f.return_value = self.newTemp()
         # TODO reserve room for stack, this can be done at later point?
         self.setFunction(f)
         l2 = self.newBlock()
@@ -49,14 +46,21 @@
 
         for sym in fn.innerScope:
             # TODO: handle parameters different
+            if sym.isParameter:
+                print('param', sym)
+                ir.Parameter(sym.name)
+            if sym.isLocal:
+                print('local', sym)
+            
             v = self.newTemp()
             # TODO: make this ssa here??
             self.varMap[sym] = v
 
         self.genCode(fn.body)
-        # TODO handle return?
+        # Set the default return value to zero:
+        # TBD: this may not be required?
+        self.emit(ir.Move(f.return_value, ir.Const(0)))
         self.emit(ir.Jump(f.epiloog))
-        #self.emit(ir.Return(ir.Const(0)))
         self.setFunction(None)
 
     def genCode(self, code):
@@ -87,7 +91,8 @@
         elif type(code) is astnodes.ReturnStatement:
             if code.expr:
                 re = self.genExprCode(code.expr)
-                self.emit(ir.Return(re))
+                self.emit(ir.Move(self.fn.return_value, re))
+                self.emit(ir.Jump(self.fn.epiloog))
             else:
                 self.builder.addIns(ir.Return())
         elif type(code) is astnodes.WhileStatement: