diff python/transform.py @ 279:2ccd57b1d78c

Fix register allocator to do burn2 OK
author Windel Bouwman
date Sat, 12 Oct 2013 09:56:23 +0200
parents ea93e0a7a31e
children 02385f62f250
line wrap: on
line diff
--- a/python/transform.py	Sun Sep 29 14:08:15 2013 +0200
+++ b/python/transform.py	Sat Oct 12 09:56:23 2013 +0200
@@ -3,7 +3,7 @@
 """
 
 import logging
-from ir import *
+import ir
 # Standard passes:
 
 class FunctionPass:
@@ -46,34 +46,29 @@
         """ Override this virtual method """
         raise NotImplementedError()
 
-# Usefull transforms:
-class ConstantFolder(BasicBlockPass):
+class BasePass(BasicBlockPass):
     def onBasicBlock(self, bb):
-        constMap = {}
-        ins = [i for i in bb.Instructions if type(i) in [ImmLoad, BinaryOperator]]
-        for i in ins:
-            if type(i) is ImmLoad:
-                constMap[i.target] = i.value
-            elif type(i) is BinaryOperator:
-                if i.value1 in constMap and i.value2 in constMap and i.operation in ['+', '-', '*', '<<']:
-                    op = i.operation
-                    va = constMap[i.value1]
-                    vb = constMap[i.value2]
-                    if op == '+':
-                       vr = va + vb
-                    elif op == '*':
-                       vr = va * vb
-                    elif op == '-':
-                       vr = va - vb
-                    elif op == '<<':
-                        vr = va << vb
-                    else:
-                        raise NotImplementedError()
-                    constMap[i.result] = vr
-                    i.removeDef(i.result)
-                    i2 = ImmLoad(i.result, vr)
-                    logging.debug('Replacing {} with {}'.format(i, i2))
-                    i.Parent.replaceInstruction(i, i2)
+        pass
+
+# Usefull transforms:
+class ConstantFolder(BasePass):
+    def __init__(self):
+        super().__init__()
+        self.ops = {}
+        self.ops['+'] = lambda x, y: x + y
+        self.ops['-'] = lambda x, y: x - y
+        self.ops['*'] = lambda x, y: x * y
+        self.ops['<<'] = lambda x, y: x << y
+
+    def postExpr(self, expr):
+        if type(i) is BinaryOperator and i.operation in self.ops.keys() and type(i.a) is Const and type(i.b) is Const:
+            op = i.operation
+            va = i.a.value
+            vb = i.b.value
+            vr = self.ops[i.operation](va, vb)
+            return Const(vr)
+        else:
+            return expr
 
 
 class DeadCodeDeleter(BasicBlockPass):