view python/ir/basicblock.py @ 268:5ec7580976d9

Op naar tree-IR
author Windel Bouwman
date Wed, 14 Aug 2013 20:12:40 +0200
parents 04c19282a5aa
children 5f8c04a8d26b
line wrap: on
line source


# from .instruction import Statement

class Block:
    """ 
        Uninterrupted sequence of instructions with a label at the start.
    """
    def __init__(self, name):
        self.name = name
        self.instructions = []

    def __repr__(self):
        return 'Block {0}'.format(self.name)

    def addInstruction(self, i):
        #assert isinstance(i, Statement)
        i.parent = self
        self.instructions.append(i)

    def replaceInstruction(self, i1, i2):
        idx = self.instructions.index(i1)
        i1.parent = None
        i1.delete()
        i2.parent = self
        self.instructions[idx] = i2

    def removeInstruction(self, i):
        i.parent = None
        i.delete()
        self.instructions.remove(i)

    def getInstructions(self):
        return self.instructions
    Instructions = property(getInstructions)

    def getLastIns(self):
        return self.instructions[-1]
    LastInstruction = property(getLastIns)

    @property
    def Empty(self):
        return len(self.instructions) == 0

    @property
    def FirstInstruction(self):
        return self.instructions[0]

    def getSuccessors(self):
        if not self.Empty:
            return self.LastInstruction.Targets
        return []
    Successors = property(getSuccessors)

    def getPredecessors(self):
        preds = []
        for bb in self.parent.BasicBlocks:
            if self in bb.Successors:
                preds.append(bb)
        return preds
    Predecessors = property(getPredecessors)

    def precedes(self, other):
        raise NotImplementedError()

    def check(self):
        pass