Mercurial > lcfOS
diff python/ppci/irmach.py @ 301:6753763d3bec
merge codegen into ppci package
author | Windel Bouwman |
---|---|
date | Thu, 05 Dec 2013 17:02:38 +0100 |
parents | python/irmach.py@674789d9ff37 |
children | e9fe6988497c |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/ppci/irmach.py Thu Dec 05 17:02:38 2013 +0100 @@ -0,0 +1,70 @@ + +""" + Abstract assembly language instructions. + + This is the second intermediate representation. + + Instructions are selected and scheduled at this stage. +""" + +from target import Instruction + + +class Frame: + """ + Activation record abstraction. This class contains a flattened + function. Instructions are selected and scheduled at this stage. + Frames differ per machine. + """ + def __init__(self, name): + self.name = name + self.instructions = [] + self.stacksize = 0 + + def __repr__(self): + return 'Frame {}'.format(self.name) + + def lower_to(self, outs): + for im in self.instructions: + if isinstance(im.assem, Instruction): + outs.emit(im.assem) + else: + outs.emit(im.assem.fromim(im)) + + +class AbstractInstruction: + """ + Abstract machine instruction class. This is a very simple + abstraction of machine instructions. + """ + def __init__(self, cls, ops=(), src=(), dst=(), jumps=(), others=(), ismove=False): + assert type(cls) is type or isinstance(cls, Instruction) + self.assem = cls + self.ops = tuple(ops) + self.src = tuple(src) + self.dst = tuple(dst) + self.jumps = tuple(jumps) + self.others = tuple(others) + self.ismove = ismove + + def __repr__(self): + return self.render() + + def render(self): + """ + Substitutes source, dst and labels in the string + """ + x = str(self.assem) + for i, s in enumerate(self.src): + p = '%s{}'.format(i) + x = x.replace(p, str(s)) + for i, d in enumerate(self.dst): + p = '%d{}'.format(i) + x = x.replace(p, str(d)) + for i, j in enumerate(self.jumps): + p = '%l{}'.format(i) + x = x.replace(p, str(j)) + return x + + +makeIns = AbstractInstruction