view python/ir/function.py @ 270:cdc76d183bcc

first register allocator
author Windel Bouwman
date Mon, 19 Aug 2013 21:14:28 +0200
parents 5f8c04a8d26b
children cf7d5fb7d9c8
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)
        if self.epiloog in bbs:
            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