diff python/c3/codegenerator.py @ 228:7f18ed9b6b7e

Removal of emptystatement class
author Windel Bouwman
date Sat, 13 Jul 2013 11:12:24 +0200
parents 82dfe6a32717
children 88a1e0baef65
line wrap: on
line diff
--- a/python/c3/codegenerator.py	Fri Jul 12 17:42:39 2013 +0200
+++ b/python/c3/codegenerator.py	Sat Jul 13 11:12:24 2013 +0200
@@ -2,7 +2,7 @@
 from . import astnodes
 from .scope import boolType, intType
 from ppci import CompilerError
-   
+
 class CodeGenerator:
     """ Generates intermediate code from a package """
     def gencode(self, pkg):
@@ -55,91 +55,90 @@
             for s in code.statements:
                 self.genCode(s)
         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))
+            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.ExpressionStatement:
             self.genExprCode(code.ex)
         elif type(code) is astnodes.IfStatement:
-         bbtrue = self.builder.newBB()
-         bbfalse = self.builder.newBB()
-         te = self.builder.newBB()
-         self.genCondCode(code.condition, bbtrue, bbfalse)
-         self.builder.setBB(bbtrue)
-         self.genCode(code.truestatement)
-         self.builder.addIns(ir.Branch(te))
-         self.builder.setBB(bbfalse)
-         self.genCode(code.falsestatement)
-         self.builder.addIns(ir.Branch(te))
-         self.builder.setBB(te)
-        elif type(code) is astnodes.EmptyStatement:
-         pass
+             bbtrue = self.builder.newBB()
+             bbfalse = self.builder.newBB()
+             te = self.builder.newBB()
+             self.genCondCode(code.condition, bbtrue, bbfalse)
+             self.builder.setBB(bbtrue)
+             self.genCode(code.truestatement)
+             self.builder.addIns(ir.Branch(te))
+             self.builder.setBB(bbfalse)
+             if code.falsestatement:
+                 self.genCode(code.falsestatement)
+             self.builder.addIns(ir.Branch(te))
+             self.builder.setBB(te)
         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())
+            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:
-         bbdo = self.builder.newBB()
-         bbtest = self.builder.newBB()
-         te = self.builder.newBB()
-         self.builder.addIns(ir.Branch(bbtest))
-         self.builder.setBB(bbtest)
-         self.genCondCode(code.condition, bbdo, te)
-         self.builder.setBB(bbdo)
-         self.genCode(code.statement)
-         self.builder.addIns(ir.Branch(bbtest))
-         self.builder.setBB(te)
+            bbdo = self.builder.newBB()
+            bbtest = self.builder.newBB()
+            te = self.builder.newBB()
+            self.builder.addIns(ir.Branch(bbtest))
+            self.builder.setBB(bbtest)
+            self.genCondCode(code.condition, bbdo, te)
+            self.builder.setBB(bbdo)
+            self.genCode(code.statement)
+            self.builder.addIns(ir.Branch(bbtest))
+            self.builder.setBB(te)
         else:
-         print('Unknown stmt:', code)
+            print('Unknown stmt:', code)
     def genCondCode(self, expr, bbtrue, bbfalse):
-      # Implement sequential logical operators
-      assert expr.typ == boolType
-      if type(expr) is astnodes.Binop:
-         if expr.op == 'or':
-            l2 = self.builder.newBB()
-            self.genCondCode(expr.a, bbtrue, l2)
-            self.builder.setBB(l2)
-            self.genCondCode(expr.b, bbtrue, bbfalse)
-         elif expr.op == 'and':
-            l2 = self.builder.newBB()
-            self.genCondCode(expr.a, l2, bbfalse)
-            self.builder.setBB(l2)
-            self.genCondCode(expr.b, bbtrue, bbfalse)
-         elif expr.op in ['==', '>', '<']:
-            ta = self.genExprCode(expr.a)
-            tb = self.genExprCode(expr.b)
-            i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse)
-            self.builder.addIns(i)
-         else:
-            raise NotImlementedError('Unknown condition {0}'.format(expr))
-      elif type(expr) is astnodes.Literal:
-         if expr.val:
-            self.builder.addIns(ir.Branch(bbtrue))
-         else:
-            self.builder.addIns(ir.Branch(bbfalse))
-      else:
-         print('Unknown cond', expr)
+        # Implement sequential logical operators
+        assert expr.typ == boolType
+        if type(expr) is astnodes.Binop:
+             if expr.op == 'or':
+                l2 = self.builder.newBB()
+                self.genCondCode(expr.a, bbtrue, l2)
+                self.builder.setBB(l2)
+                self.genCondCode(expr.b, bbtrue, bbfalse)
+             elif expr.op == 'and':
+                l2 = self.builder.newBB()
+                self.genCondCode(expr.a, l2, bbfalse)
+                self.builder.setBB(l2)
+                self.genCondCode(expr.b, bbtrue, bbfalse)
+             elif expr.op in ['==', '>', '<']:
+                ta = self.genExprCode(expr.a)
+                tb = self.genExprCode(expr.b)
+                i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse)
+                self.builder.addIns(i)
+             else:
+                raise NotImlementedError('Unknown condition {0}'.format(expr))
+        elif type(expr) is astnodes.Literal:
+             if expr.val:
+                self.builder.addIns(ir.Branch(bbtrue))
+             else:
+                self.builder.addIns(ir.Branch(bbfalse))
+        else:
+             print('Unknown cond', expr)
     def genExprCode(self, expr):
         if type(expr) is astnodes.Binop:
-         ra = self.genExprCode(expr.a)
-         rb = self.genExprCode(expr.b)
-         ops = ['+', '-', '*', '/', '|', '&']
-         if expr.op in ops:
-            tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', '&':'and'}
-            tmp = self.builder.newTmp(tmpnames[expr.op])
-            op = expr.op
-            ins = ir.BinaryOperator(tmp, op, ra, rb)
-            self.builder.addIns(ins)
-            return tmp
-         else:
-            print('Unknown {0}'.format(expr))
-            tmp = self.builder.newTmp()
-            # TODO
-            return tmp
+             ra = self.genExprCode(expr.a)
+             rb = self.genExprCode(expr.b)
+             ops = ['+', '-', '*', '/', '|', '&']
+             if expr.op in ops:
+                tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', '&':'and'}
+                tmp = self.builder.newTmp(tmpnames[expr.op])
+                op = expr.op
+                ins = ir.BinaryOperator(tmp, op, ra, rb)
+                self.builder.addIns(ins)
+                return tmp
+             else:
+                print('Unknown {0}'.format(expr))
+                tmp = self.builder.newTmp()
+                # TODO
+                return tmp
         elif type(expr) is astnodes.Unop:
             ra = self.genExprCode(expr.a)
             if expr.op == '&':
@@ -186,15 +185,15 @@
                 else:
                     raise Exception()
         elif type(expr) is astnodes.FunctionCall:
-         tmp = self.builder.newTmp("res")
-         args = []
-         for arg in expr.args:
-            ar = self.genExprCode(arg)
-            args.append(ar)
-         fn = self.funcMap[expr.proc]
-         ins = ir.Call(fn, args, tmp)
-         self.builder.addIns(ins)
-         return tmp
+             tmp = self.builder.newTmp("res")
+             args = []
+             for arg in expr.args:
+                ar = self.genExprCode(arg)
+                args.append(ar)
+             fn = self.funcMap[expr.proc]
+             ins = ir.Call(fn, args, tmp)
+             self.builder.addIns(ins)
+             return tmp
         else:
-         raise CompilerError('Unknown expr {}'.format(expr))
+             raise CompilerError('Unknown expr {}'.format(expr))