Mercurial > lcfOS
annotate python/irmach.py @ 292:534b94b40aa8
Fixup reorganize
author | Windel Bouwman |
---|---|
date | Wed, 27 Nov 2013 08:06:42 +0100 |
parents | 02385f62f250 |
children | 674789d9ff37 |
rev | line source |
---|---|
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 |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
11 import target |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
12 |
277 | 13 |
274 | 14 class Frame: |
15 """ | |
16 Activation record abstraction. This class contains a flattened | |
17 function. Instructions are selected and scheduled at this stage. | |
18 Frames differ per machine. | |
19 """ | |
20 def __init__(self, name): | |
21 self.name = name | |
22 self.instructions = [] | |
275 | 23 self.stacksize = 0 |
274 | 24 |
25 def __repr__(self): | |
275 | 26 return 'Frame {}'.format(self.name) |
274 | 27 |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
28 def lower_to(self, outs): |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
29 for im in self.instructions: |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
30 if isinstance(im.assem, target.Instruction): |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
31 outs.emit(im.assem) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
32 else: |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
33 outs.emit(im.assem.fromim(im)) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
34 |
261 | 35 |
268 | 36 class AbstractInstruction: |
274 | 37 """ |
38 Abstract machine instruction class. This is a very simple | |
39 abstraction of machine instructions. | |
40 """ | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
41 def __init__(self, cls, ops=(), src=(), dst=(), jumps=(), others=(), ismove=False): |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
42 assert type(cls) is type or isinstance(cls, target.Instruction) |
277 | 43 self.assem = cls |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
44 self.ops = tuple(ops) |
268 | 45 self.src = tuple(src) |
46 self.dst = tuple(dst) | |
47 self.jumps = tuple(jumps) | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
48 self.others = tuple(others) |
277 | 49 c = lambda s: tuple(map(type, s)) == (ir.Temp, ) |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
50 self.ismove = ismove |
268 | 51 |
52 def __repr__(self): | |
275 | 53 return self.render() |
54 | |
55 def render(self): | |
56 """ | |
57 Substitutes source, dst and labels in the string | |
58 """ | |
277 | 59 x = str(self.assem) |
275 | 60 for i, s in enumerate(self.src): |
61 p = '%s{}'.format(i) | |
62 x = x.replace(p, str(s)) | |
63 for i, d in enumerate(self.dst): | |
64 p = '%d{}'.format(i) | |
65 x = x.replace(p, str(d)) | |
66 for i, j in enumerate(self.jumps): | |
67 p = '%l{}'.format(i) | |
68 x = x.replace(p, str(j)) | |
69 return x |