261
|
1
|
269
|
2 """
|
270
|
3 Abstract assembly language instructions.
|
|
4
|
|
5 This is the second intermediate representation.
|
|
6
|
|
7 Instructions are selected and scheduled at this stage.
|
269
|
8 """
|
261
|
9
|
277
|
10 import ir
|
|
11
|
274
|
12 class Frame:
|
|
13 """
|
|
14 Activation record abstraction. This class contains a flattened
|
|
15 function. Instructions are selected and scheduled at this stage.
|
|
16 Frames differ per machine.
|
|
17 """
|
|
18 def __init__(self, name):
|
|
19 self.name = name
|
|
20 self.instructions = []
|
275
|
21 self.stacksize = 0
|
274
|
22
|
|
23 def __repr__(self):
|
275
|
24 return 'Frame {}'.format(self.name)
|
274
|
25
|
275
|
26 def makeIns(*args, **kwargs):
|
|
27 return AbstractInstruction(*args, **kwargs)
|
261
|
28
|
268
|
29 class AbstractInstruction:
|
274
|
30 """
|
|
31 Abstract machine instruction class. This is a very simple
|
|
32 abstraction of machine instructions.
|
|
33 """
|
277
|
34 def __init__(self, cls, ops=(), src=(), dst=(), jumps=()):
|
|
35 self.assem = cls
|
|
36 self.ops = ops
|
268
|
37 self.src = tuple(src)
|
|
38 self.dst = tuple(dst)
|
|
39 self.jumps = tuple(jumps)
|
277
|
40 c = lambda s: tuple(map(type, s)) == (ir.Temp, )
|
|
41 self.ismove = c(src) and c(dst) and cls.lower().startswith('mov')
|
268
|
42
|
|
43 def __repr__(self):
|
275
|
44 return self.render()
|
|
45
|
|
46 def render(self):
|
|
47 """
|
|
48 Substitutes source, dst and labels in the string
|
|
49 """
|
277
|
50 x = str(self.assem)
|
275
|
51 for i, s in enumerate(self.src):
|
|
52 p = '%s{}'.format(i)
|
|
53 x = x.replace(p, str(s))
|
|
54 for i, d in enumerate(self.dst):
|
|
55 p = '%d{}'.format(i)
|
|
56 x = x.replace(p, str(d))
|
|
57 for i, j in enumerate(self.jumps):
|
|
58 p = '%l{}'.format(i)
|
|
59 x = x.replace(p, str(j))
|
|
60 return x
|
261
|
61
|
|
62
|