Mercurial > lcfOS
annotate python/ppci/irmach.py @ 323:e9fe6988497c
Used burg for generating expressions
author | Windel Bouwman |
---|---|
date | Thu, 30 Jan 2014 19:03:24 +0100 |
parents | 6753763d3bec |
children | 86b02c98a717 |
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: |
323 | 14 """ |
274 | 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 """ | |
323 | 57 if isinstance(self.assem, Instruction): |
58 x = str(self.assem) | |
59 else: | |
60 cn = self.assem.__name__ | |
61 x = '{}, def={}, use={}, other={}' | |
62 x = x.format(cn, self.dst, self.src, self.others) | |
275 | 63 return x |
299 | 64 |
65 | |
66 makeIns = AbstractInstruction |