Mercurial > lcfOS
diff python/ir.py @ 280:02385f62f250
Rework from str interface to Instruction interface
author | Windel Bouwman |
---|---|
date | Sat, 02 Nov 2013 10:03:26 +0100 |
parents | 2ccd57b1d78c |
children | 534b94b40aa8 |
line wrap: on
line diff
--- a/python/ir.py Sat Oct 12 09:56:23 2013 +0200 +++ b/python/ir.py Sat Nov 02 10:03:26 2013 +0100 @@ -76,7 +76,9 @@ def __init__(self, name): self.name = name self.entry = Block('{}_entry'.format(name)) + self.entry.function = self self.epiloog = Block('{}_epilog'.format(name)) + self.epiloog.function = self self.epiloog.addInstruction(Terminator()) self.return_value = Temp('{}_retval'.format(name)) self.arguments = [] @@ -86,16 +88,15 @@ args = ','.join(str(a) for a in self.arguments) return 'Function {}({})'.format(self.name, args) - def addBB(self, bb): + def addBlock(self, bb): self.bbs.append(bb) - bb.parent = self - addBlock = addBB + bb.function = self - def removeBasicBlock(self, bb): - self.bbs.remove(bb) - bb.parent = None + def removeBlock(self, bb): + #self.bbs.remove(bb) + bb.function = None - def getBBs(self): + def getBlocks(self): bbs = [self.entry] worklist = [self.entry] while worklist: @@ -117,7 +118,7 @@ return bb raise KeyError(name) - Blocks = property(getBBs) + Blocks = property(getBlocks) @property def Entry(self): @@ -150,8 +151,11 @@ """ def __init__(self, name): self.name = name + self.function = None self.instructions = [] + parent = property(lambda s: s.function) + def __repr__(self): return 'Block {0}'.format(self.name) @@ -172,14 +176,14 @@ i.delete() self.instructions.remove(i) - def getInstructions(self): + @property + def Instructions(self): return self.instructions - Instructions = property(getInstructions) - def getLastIns(self): + @property + def LastInstruction(self): if not self.Empty: return self.instructions[-1] - LastInstruction = property(getLastIns) @property def Empty(self): @@ -197,7 +201,7 @@ def getPredecessors(self): preds = [] - for bb in self.parent.BasicBlocks: + for bb in self.parent.Blocks: if self in bb.Successors: preds.append(bb) return preds @@ -359,35 +363,32 @@ return '{}'.format(self.e) -class Label(Statement): - # TODO: is this duplicate with block? - def __init__(self, name): - self.name = name - self.statements = [] - - def __repr__(self): - return 'LABEL {}:'.format(self.name) - - # Branching: class LastStatement(Statement): - pass + def changeTarget(self, old, new): + idx = self.Targets.index(old) + self.Targets[idx] = new class Terminator(LastStatement): """ Instruction that terminates the terminal block """ - Targets = [] + def __init__(self): + self.Targets = [] + def __repr__(self): return 'Terminator' class Jump(LastStatement): def __init__(self, target): - self.target = target self.Targets = [target] + def setTarget(self, t): + self.Targets[0] = t + target = property(lambda s: s.Targets[0], setTarget) + def __repr__(self): - return 'BRANCH {}'.format(self.target.name) + return 'JUMP {}'.format(self.target.name) class CJump(LastStatement): @@ -397,10 +398,11 @@ self.a = a self.cond = cond self.b = b - self.lab_yes = lab_yes - self.lab_no = lab_no self.Targets = [lab_yes, lab_no] + lab_yes = property(lambda s: s.Targets[0]) + lab_no = property(lambda s: s.Targets[1]) + def __repr__(self): return 'IF {} {} {} THEN {} ELSE {}'.format(self.a, self.cond, self.b, self.lab_yes, self.lab_no) @@ -431,7 +433,7 @@ def prepare(self): self.newTemp = NamedClassGenerator('reg', Temp).gen - self.newBlock = NamedClassGenerator('block', Block).gen + self.newBlock2 = NamedClassGenerator('block', Block).gen self.bb = None self.m = None self.fn = None @@ -442,9 +444,15 @@ self.m = m def newFunction(self, name): - f = Function(name) - self.m.addFunc(f) - return f + f = Function(name) + self.m.addFunc(f) + return f + + def newBlock(self): + assert self.fn + b = self.newBlock2() + b.function = self.fn + return b def setFunction(self, f): self.fn = f