Mercurial > lcfOS
annotate python/irmach.py @ 300:158068af716c
yafm
author | Windel Bouwman |
---|---|
date | Tue, 03 Dec 2013 18:00:22 +0100 |
parents | 674789d9ff37 |
children |
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 |
299 | 10 from target import Instruction |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
11 |
277 | 12 |
274 | 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 = [] | |
275 | 22 self.stacksize = 0 |
274 | 23 |
24 def __repr__(self): | |
275 | 25 return 'Frame {}'.format(self.name) |
274 | 26 |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
27 def lower_to(self, outs): |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
28 for im in self.instructions: |
299 | 29 if isinstance(im.assem, Instruction): |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
30 outs.emit(im.assem) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
31 else: |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
32 outs.emit(im.assem.fromim(im)) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
33 |
261 | 34 |
268 | 35 class AbstractInstruction: |
274 | 36 """ |
37 Abstract machine instruction class. This is a very simple | |
38 abstraction of machine instructions. | |
39 """ | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
40 def __init__(self, cls, ops=(), src=(), dst=(), jumps=(), others=(), ismove=False): |
299 | 41 assert type(cls) is type or isinstance(cls, Instruction) |
277 | 42 self.assem = cls |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
43 self.ops = tuple(ops) |
268 | 44 self.src = tuple(src) |
45 self.dst = tuple(dst) | |
46 self.jumps = tuple(jumps) | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
47 self.others = tuple(others) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
48 self.ismove = ismove |
268 | 49 |
50 def __repr__(self): | |
275 | 51 return self.render() |
52 | |
53 def render(self): | |
54 """ | |
55 Substitutes source, dst and labels in the string | |
56 """ | |
277 | 57 x = str(self.assem) |
275 | 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 | |
299 | 68 |
69 | |
70 makeIns = AbstractInstruction |