comparison 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
comparison
equal deleted inserted replaced
300:158068af716c 301:6753763d3bec
1
2 """
3 Abstract assembly language instructions.
4
5 This is the second intermediate representation.
6
7 Instructions are selected and scheduled at this stage.
8 """
9
10 from target import Instruction
11
12
13 class Frame:
14 """
15 Activation record abstraction. This class contains a flattened
16 function. Instructions are selected and scheduled at this stage.
17 Frames differ per machine.
18 """
19 def __init__(self, name):
20 self.name = name
21 self.instructions = []
22 self.stacksize = 0
23
24 def __repr__(self):
25 return 'Frame {}'.format(self.name)
26
27 def lower_to(self, outs):
28 for im in self.instructions:
29 if isinstance(im.assem, Instruction):
30 outs.emit(im.assem)
31 else:
32 outs.emit(im.assem.fromim(im))
33
34
35 class AbstractInstruction:
36 """
37 Abstract machine instruction class. This is a very simple
38 abstraction of machine instructions.
39 """
40 def __init__(self, cls, ops=(), src=(), dst=(), jumps=(), others=(), ismove=False):
41 assert type(cls) is type or isinstance(cls, Instruction)
42 self.assem = cls
43 self.ops = tuple(ops)
44 self.src = tuple(src)
45 self.dst = tuple(dst)
46 self.jumps = tuple(jumps)
47 self.others = tuple(others)
48 self.ismove = ismove
49
50 def __repr__(self):
51 return self.render()
52
53 def render(self):
54 """
55 Substitutes source, dst and labels in the string
56 """
57 x = str(self.assem)
58 for i, s in enumerate(self.src):
59 p = '%s{}'.format(i)
60 x = x.replace(p, str(s))
61 for i, d in enumerate(self.dst):
62 p = '%d{}'.format(i)
63 x = x.replace(p, str(d))
64 for i, j in enumerate(self.jumps):
65 p = '%l{}'.format(i)
66 x = x.replace(p, str(j))
67 return x
68
69
70 makeIns = AbstractInstruction