annotate python/ppci/irmach.py @ 334:6f4753202b9a

Added more recipes
author Windel Bouwman
date Thu, 13 Feb 2014 22:02:08 +0100
parents e9fe6988497c
children 86b02c98a717
rev   line source
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents:
diff changeset
1
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
2 """
270
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
3 Abstract assembly language instructions.
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
4
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
5 This is the second intermediate representation.
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
6
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
7 Instructions are selected and scheduled at this stage.
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
8 """
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents:
diff changeset
9
299
674789d9ff37 Added a doc
Windel Bouwman
parents: 292
diff changeset
10 from target import Instruction
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
11
277
046017431c6a Started register allocator
Windel Bouwman
parents: 275
diff changeset
12
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
13 class Frame:
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 301
diff changeset
14 """
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
15 Activation record abstraction. This class contains a flattened
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
16 function. Instructions are selected and scheduled at this stage.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
17 Frames differ per machine.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
18 """
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
19 def __init__(self, name):
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
20 self.name = name
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
21 self.instructions = []
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
22 self.stacksize = 0
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
23
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
24 def __repr__(self):
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
25 return 'Frame {}'.format(self.name)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
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
674789d9ff37 Added a doc
Windel Bouwman
parents: 292
diff changeset
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
444b9df2ed99 try to split up code generation
Windel Bouwman
parents:
diff changeset
34
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
35 class AbstractInstruction:
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
36 """
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
37 Abstract machine instruction class. This is a very simple
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
38 abstraction of machine instructions.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
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
674789d9ff37 Added a doc
Windel Bouwman
parents: 292
diff changeset
41 assert type(cls) is type or isinstance(cls, Instruction)
277
046017431c6a Started register allocator
Windel Bouwman
parents: 275
diff changeset
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
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
44 self.src = tuple(src)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
45 self.dst = tuple(dst)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
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
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
49
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
50 def __repr__(self):
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
51 return self.render()
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
52
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
53 def render(self):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
54 """
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
55 Substitutes source, dst and labels in the string
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
56 """
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 301
diff changeset
57 if isinstance(self.assem, Instruction):
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 301
diff changeset
58 x = str(self.assem)
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 301
diff changeset
59 else:
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 301
diff changeset
60 cn = self.assem.__name__
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 301
diff changeset
61 x = '{}, def={}, use={}, other={}'
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 301
diff changeset
62 x = x.format(cn, self.dst, self.src, self.others)
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
63 return x
299
674789d9ff37 Added a doc
Windel Bouwman
parents: 292
diff changeset
64
674789d9ff37 Added a doc
Windel Bouwman
parents: 292
diff changeset
65
674789d9ff37 Added a doc
Windel Bouwman
parents: 292
diff changeset
66 makeIns = AbstractInstruction