diff python/transform.py @ 176:5fd02aa38b42

Added while loop code generation
author Windel Bouwman
date Sat, 20 Apr 2013 12:00:51 +0200
parents a51b3c956386
children 460db5669efa
line wrap: on
line diff
--- a/python/transform.py	Fri Apr 19 22:15:54 2013 +0200
+++ b/python/transform.py	Sat Apr 20 12:00:51 2013 +0200
@@ -4,11 +4,14 @@
 class FunctionPass:
    def run(self, ir):
       """ Main entry point for the pass """
+      self.prepare()
       for f in ir.Functions:
          self.onFunction(f)
    def onFunction(self, f):
       """ Override this virtual method """
       raise NotImplementedError()
+   def prepare(self):
+      pass
 
 class BasicBlockPass(FunctionPass):
    def onFunction(self, f):
@@ -28,23 +31,28 @@
 
 # Usefull transforms:
 class ConstantFolder(InstructionPass):
+   def prepare(self):
+      self.constMap = {}
    def onInstruction(self, i):
       if type(i) is ImmLoad:
-         i.target.constval = i.value
+         self.constMap[i.target] = i.value
       elif type(i) is BinaryOperator:
-         a = i.value1
-         b = i.value2
-         if hasattr(a, 'constval') and hasattr(b,'constval'):
+         if i.value1 in self.constMap and i.value2 in self.constMap:
             op = i.operation
+            va = self.constMap[i.value1]
+            vb = self.constMap[i.value2]
             if op == '+':
-               i2 = ImmLoad(i.result, a.constval + b.constval)
-               i.Parent.replaceInstruction(i, i2)
+               vr = va + vb
             elif op == '*':
-               i2 = ImmLoad(i.result, a.constval * b.constval)
-               i.Parent.replaceInstruction(i, i2)
+               vr = va * vb
             elif op == '-':
-               i2 = ImmLoad(i.result, a.constval - b.constval)
-               i.Parent.replaceInstruction(i, i2)
+               vr = va - vb
+            else:
+               vr = None
+               return
+            self.constMap[i.result] = vr
+            i2 = ImmLoad(i.result, vr)
+            i.Parent.replaceInstruction(i, i2)
 
 class DeadCodeDeleter(BasicBlockPass):
    def onBasicBlock(self, bb):
@@ -71,5 +79,6 @@
 
 class Mem2RegPromotor(FunctionPass):
    def onFunction(self, f):
+      # TODO
       print(f)