annotate python/target.py @ 202:f22b431f4113

Added arm add instruction
author Windel Bouwman
date Sat, 15 Jun 2013 10:02:50 +0200
parents d5debbfc0200
children ca1ea402f6a1
rev   line source
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
1 from asmnodes import ASymbol, AInstruction
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
2 from ppci import CompilerError
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
3
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
4 """
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
5 Base classes for defining a target
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
6 """
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
7
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
8 # Machine code interface:
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
9 class Operand:
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
10 """ Single machine operand """
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
11 pass
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
12
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
13 class Register(Operand):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
14 def __init__(self, name):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
15 self.name = name
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
16
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
17 class Instruction:
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
18 def encode(self):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
19 raise NotImplementedError('TODO')
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
20
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
21 class Target:
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
22 def __init__(self, name, desc=''):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
23 self.name = name
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
24 self.desc = desc
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
25 self.registers = []
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
26 self.instructions = []
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
27
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
28 def instruction(self, cls):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
29 """ Decorator function that registers an instruction to this target """
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
30 self.addInstruction(cls)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
31 return cls
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
32
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
33 def addInstruction(self, ins_class):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
34 self.instructions.append(ins_class)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
35
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
36 def mapOperand(self, operand):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
37 """ Try to map an operand to a target type """
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
38 if type(operand) is ASymbol:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
39 # Try to map to register:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
40 regs = {}
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
41 for r in self.registers:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
42 regs[r.name] = r
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
43 if operand.name in regs:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
44 return regs[operand.name]
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
45 raise CompilerError('Cannot map {0}'.format(operand))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
46
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
47 def mapInstruction(self, vi):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
48 """ Map ast tree to real instruction for this target """
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
49
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
50 # map to real operands:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
51 rops = tuple(map(self.mapOperand, vi.operands))
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
52 optypes = tuple(map(type, rops))
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
53
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
54 # look for a suitable instruction
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
55 for ic in self.instructions:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
56 if ic.mnemonic == vi.opcode and ic.operands == optypes:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
57 ri = ic(*rops)
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
58 return ri
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
59 raise CompilerError('No suitable instruction found for "{0}"'.format(vi))
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
60