diff python/ir/instruction.py @ 261:444b9df2ed99

try to split up code generation
author Windel Bouwman
date Fri, 09 Aug 2013 09:05:13 +0200
parents ac603eb66b63
children ed14e077124c
line wrap: on
line diff
--- a/python/ir/instruction.py	Tue Aug 06 18:29:53 2013 +0200
+++ b/python/ir/instruction.py	Fri Aug 09 09:05:13 2013 +0200
@@ -32,6 +32,11 @@
         assert ins in self.used_by
         return all(not ins.precedes(ub) for ub in self.used_by)
 
+    def replaceby(self, v2):
+        for use_ins in self.used_by:
+            use_ins.replaceValue(self, v2.value)
+        assert not self.Used
+
 class Variable(Value):
     pass
 
@@ -133,13 +138,6 @@
 
 
 
-class Terminator(Instruction):
-    @property
-    def Targets(self):
-        return self.getTargets()
-
-    def changeTarget(self, tfrom, tto):
-        pass
 
 
 # Function calling:
@@ -156,6 +154,7 @@
       if result:
          assert type(result) is Value
          self.addDef(result)
+
    def __repr__(self):
       if self.result:
          pfx = '{0} = '.format(self.result)
@@ -164,55 +163,43 @@
       args = ','.join([str(arg) for arg in self.arguments])
       return pfx + '{0}({1})'.format(self.callee.name, args)
 
-class Return(Terminator):
-   def __init__(self, value=None):
-      super().__init__()
-      self.value = value
-      if value:
-         self.addUse(value)
-   def __repr__(self):
-      if self.value:
-         return 'ret {0}'.format(self.value)
-      else:
-         return 'ret'
-   def getTargets(self):
-      return []
 
 class Alloc(Instruction):
-   """ Allocates space on the stack """
-   def __init__(self, value):
-      super().__init__()
-      assert isinstance(value, Value)
-      self.value = value
-      self.addDef(value)
-   def __repr__(self):
-      return '{0} = alloc'.format(self.value)
+    """ Allocates space on the stack """
+    def __init__(self, value):
+        super().__init__()
+        assert isinstance(value, Value)
+        self.value = value
+        self.addDef(value)
+
+    def __repr__(self):
+        return '{0} = alloc'.format(self.value)
+
 
 class ImmLoad(Instruction):
-   def __init__(self, target, value):
-      super().__init__()
-      assert type(target) is Value
-      self.target = target
-      self.value = value
-      self.addDef(target)
-   def __repr__(self):
-      return '{} = {}'.format(self.target, self.value)
+    def __init__(self, target, value):
+        super().__init__()
+        assert type(target) is Value
+        self.target = target
+        self.value = value
+        self.addDef(target)
+
+    def __repr__(self):
+        return '{} = {}'.format(self.target, self.value)
 
 # Data operations
 class BinaryOperator(Instruction):
     def __init__(self, result, operation, value1, value2):
-      super().__init__()
-      #print('operation is in binops:', operation in BinOps)
-      # Check types of the two operands:
-      assert type(value1) is Value, str(value1) + str(type(value1))
-      assert type(value2) is Value, value2
-      self.result = result
-      self.addDef(result)
-      self.value1 = value1
-      self.value2 = value2
-      self.addUse(value1)
-      self.addUse(value2)
-      self.operation = operation
+        super().__init__()
+        assert type(value1) is Value, str(value1) + str(type(value1))
+        assert type(value2) is Value, value2
+        self.result = result
+        self.addDef(result)
+        self.value1 = value1
+        self.value2 = value2
+        self.addUse(value1)
+        self.addUse(value2)
+        self.operation = operation
 
     def __repr__(self):
         a, b = self.value1, self.value2
@@ -230,6 +217,7 @@
         self.removeUse(old)
         self.addUse(new)
 
+
 # Memory functions:
 class Load(Instruction):
     def __init__(self, location, value):
@@ -244,6 +232,7 @@
     def __repr__(self):
         return '{} = [{}]'.format(self.value, self.location)
 
+
 class Store(Instruction):
     def __init__(self, location, value):
         super().__init__()
@@ -267,20 +256,34 @@
         self.removeUse(old)
         self.addUse(new)
 
+
 # Branching:
+class Terminator(Instruction):
+    @property
+    def Targets(self):
+        return self.getTargets()
+
+    def changeTarget(self, tfrom, tto):
+        pass
+
+
 class Branch(Terminator):
     def __init__(self, target):
-      super().__init__()
-      assert type(target) is BasicBlock
-      self.target = target
+        super().__init__()
+        assert type(target) is BasicBlock
+        self.target = target
+
     def __repr__(self):
         return 'BRANCH {0}'.format(self.target)
+
     def getTargets(self):
         return [self.target]
+
     def changeTarget(self, tfrom, tto):
         assert tfrom is self.target
         self.target = tto
 
+
 class ConditionalBranch(Terminator):
    def __init__(self, a, cond, b, lab1, lab2):
       super().__init__()
@@ -296,10 +299,12 @@
       self.lab1 = lab1
       assert type(lab2) is BasicBlock
       self.lab2 = lab2
+
    def __repr__(self):
       return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
    def getTargets(self):
       return [self.lab1, self.lab2]
+
    def changeTarget(self, tfrom, tto):
       assert tfrom is self.lab1 or tfrom is self.lab2
       if tfrom is self.lab1:
@@ -307,6 +312,24 @@
       elif tfrom is self.lab2:
          self.lab2 = tto
 
+
+class Return(Terminator):
+    def __init__(self, value=None):
+        super().__init__()
+        self.value = value
+        if value:
+            self.addUse(value)
+
+    def __repr__(self):
+        if self.value:
+            return 'ret {0}'.format(self.value)
+        else:
+            return 'ret'
+
+    def getTargets(self):
+        return []
+
+
 class PhiNode(Instruction):
     def __init__(self):
         super().__init__()