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
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:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
14 """
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 """
277
046017431c6a Started register allocator
Windel Bouwman
parents: 275
diff changeset
57 x = str(self.assem)
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
58 for i, s in enumerate(self.src):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
59 p = '%s{}'.format(i)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
60 x = x.replace(p, str(s))
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
61 for i, d in enumerate(self.dst):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
62 p = '%d{}'.format(i)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
63 x = x.replace(p, str(d))
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
64 for i, j in enumerate(self.jumps):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
65 p = '%l{}'.format(i)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
66 x = x.replace(p, str(j))
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
67 return x
299
674789d9ff37 Added a doc
Windel Bouwman
parents: 292
diff changeset
68
674789d9ff37 Added a doc
Windel Bouwman
parents: 292
diff changeset
69
674789d9ff37 Added a doc
Windel Bouwman
parents: 292
diff changeset
70 makeIns = AbstractInstruction