annotate python/irmach.py @ 282:c137f1fe3e65

Add codeship hook
author Windel Bouwman
date Fri, 15 Nov 2013 09:32:37 +0100
parents 02385f62f250
children 534b94b40aa8
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
277
046017431c6a Started register allocator
Windel Bouwman
parents: 275
diff changeset
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
046017431c6a Started register allocator
Windel Bouwman
parents: 275
diff changeset
13
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
14 class Frame:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
15 """
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
16 Activation record abstraction. This class contains a flattened
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
17 function. Instructions are selected and scheduled at this stage.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
18 Frames differ per machine.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
19 """
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
20 def __init__(self, name):
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
21 self.name = name
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
22 self.instructions = []
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
23 self.stacksize = 0
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
24
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
25 def __repr__(self):
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
26 return 'Frame {}'.format(self.name)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
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
444b9df2ed99 try to split up code generation
Windel Bouwman
parents:
diff changeset
35
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
36 class AbstractInstruction:
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
37 """
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
38 Abstract machine instruction class. This is a very simple
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
39 abstraction of machine instructions.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
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
046017431c6a Started register allocator
Windel Bouwman
parents: 275
diff changeset
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
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
45 self.src = tuple(src)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
46 self.dst = tuple(dst)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
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
046017431c6a Started register allocator
Windel Bouwman
parents: 275
diff changeset
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
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
51
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
52 def __repr__(self):
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
53 return self.render()
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
54
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
55 def render(self):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
56 """
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
57 Substitutes source, dst and labels in the string
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
58 """
277
046017431c6a Started register allocator
Windel Bouwman
parents: 275
diff changeset
59 x = str(self.assem)
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
60 for i, s in enumerate(self.src):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
61 p = '%s{}'.format(i)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
62 x = x.replace(p, str(s))
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
63 for i, d in enumerate(self.dst):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
64 p = '%d{}'.format(i)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
65 x = x.replace(p, str(d))
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
66 for i, j in enumerate(self.jumps):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
67 p = '%l{}'.format(i)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
68 x = x.replace(p, str(j))
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
69 return x
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents:
diff changeset
70
444b9df2ed99 try to split up code generation
Windel Bouwman
parents:
diff changeset
71