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
|
274
|
10 class Frame:
|
|
11 """
|
|
12 Activation record abstraction. This class contains a flattened
|
|
13 function. Instructions are selected and scheduled at this stage.
|
|
14 Frames differ per machine.
|
|
15 """
|
|
16 def __init__(self, name):
|
|
17 self.name = name
|
|
18 self.instructions = []
|
275
|
19 self.stacksize = 0
|
274
|
20
|
|
21 def __repr__(self):
|
275
|
22 return 'Frame {}'.format(self.name)
|
274
|
23
|
275
|
24 def makeIns(*args, **kwargs):
|
|
25 return AbstractInstruction(*args, **kwargs)
|
261
|
26
|
268
|
27 class AbstractInstruction:
|
274
|
28 """
|
|
29 Abstract machine instruction class. This is a very simple
|
|
30 abstraction of machine instructions.
|
|
31 """
|
268
|
32 def __init__(self, assem, src=(), dst=(), jumps=()):
|
|
33 self.assem = assem
|
|
34 self.src = tuple(src)
|
|
35 self.dst = tuple(dst)
|
|
36 self.jumps = tuple(jumps)
|
|
37
|
|
38 def __repr__(self):
|
274
|
39 s = str(self.src) if self.src else ''
|
|
40 d = str(self.dst) if self.dst else ''
|
|
41 l = str(self.jumps) if self.jumps else ''
|
275
|
42 #return self.assem + s + d + l
|
|
43 return self.render()
|
|
44
|
|
45 def render(self):
|
|
46 """
|
|
47 Substitutes source, dst and labels in the string
|
|
48 """
|
|
49 x = self.assem
|
|
50 for i, s in enumerate(self.src):
|
|
51 p = '%s{}'.format(i)
|
|
52 x = x.replace(p, str(s))
|
|
53 for i, d in enumerate(self.dst):
|
|
54 p = '%d{}'.format(i)
|
|
55 x = x.replace(p, str(d))
|
|
56 for i, j in enumerate(self.jumps):
|
|
57 p = '%l{}'.format(i)
|
|
58 x = x.replace(p, str(j))
|
|
59
|
|
60 return x
|
261
|
61
|
|
62
|