view python/ir/function.py @ 269:5f8c04a8d26b

Towards better modularity
author Windel Bouwman
date Sun, 18 Aug 2013 17:43:18 +0200
parents 5ec7580976d9
children cdc76d183bcc
line wrap: on
line source

from .basicblock import Block

class Function:
    def __init__(self, name):
        self.name = name
        self.entry = Block('{}_entry'.format(name))
        self.epiloog = Block('{}_epilog'.format(name))

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

    def addBB(self, bb):
        self.bbs.append(bb)
        bb.parent = self
    addBasicBlock = addBB

    def removeBasicBlock(self, bb):
        self.bbs.remove(bb)
        bb.parent = None

    def getBBs(self):
        bbs = [self.entry]
        worklist = [self.entry]
        while worklist:
            b = worklist.pop()
            for sb in b.Successors:
                if sb not in bbs:
                    bbs.append(sb)
                    worklist.append(sb)
        bbs.remove(self.entry)
        bbs.remove(self.epiloog)
        bbs.insert(0, self.entry)
        bbs.append(self.epiloog)
        return bbs

    def findBasicBlock(self, name):
        for bb in self.bbs:
            if bb.name == name:
                return bb
        raise KeyError(name)

    BasicBlocks = property(getBBs)

    @property
    def Entry(self):
        return self.entry

    def check(self):
        for bb in self.BasicBlocks:
            bb.check()

    def call(self, *args):
        varmap = {}
        bb = self.Entry
        ip = 0
        while True:
            i = bb.Instructions[ip]
            ip += 1
            return