annotate python/target.py @ 201:d5debbfc0200

Added all 27 core instructions of msp430
author Windel Bouwman
date Thu, 13 Jun 2013 00:07:28 +0200
parents 5e391d9a3381
children f22b431f4113
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 """
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
30 self.instructions.append(cls)
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
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
33 def mapOperand(self, operand):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
34 """ Try to map an operand to a target type """
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
35 if type(operand) is ASymbol:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
36 # Try to map to register:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
37 regs = {}
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
38 for r in self.registers:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
39 regs[r.name] = r
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
40 if operand.name in regs:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
41 return regs[operand.name]
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
42 raise CompilerError('Cannot map {0}'.format(operand))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
43
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
44 def mapInstruction(self, vi):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
45 """ Map ast tree to real instruction for this target """
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
46
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
47 # map to real operands:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
48 rops = tuple(map(self.mapOperand, vi.operands))
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
49 optypes = tuple(map(type, rops))
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
50
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
51 # look for a suitable instruction
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
52 for ic in self.instructions:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
53 if ic.mnemonic == vi.opcode and ic.operands == optypes:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
54 ri = ic(*rops)
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
55 return ri
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
56 raise CompilerError('No suitable instruction found for "{0}"'.format(vi))
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
57