annotate python/target.py @ 200:5e391d9a3381

Split off asm nodes
author Windel Bouwman
date Sun, 09 Jun 2013 16:06:49 +0200
parents a690473b79e2
children d5debbfc0200
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:
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
18 def __init__(self, opcode):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
19 self.opcode = opcode
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
20
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
21 class Target:
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
22 def __init__(self):
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
23 self.registers = []
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
24 self.instructions = []
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
25
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
26 def mapOperand(self, operand):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
27 """ Try to map an operand to a target type """
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
28 if type(operand) is ASymbol:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
29 # Try to map to register:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
30 regs = {}
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
31 for r in self.registers:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
32 regs[r.name] = r
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
33 if operand.name in regs:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
34 return regs[operand.name]
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
35 else:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
36 return
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
37
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
38 def mapInstruction(self, vi):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
39 """ Map ast tree to real instruction for this target """
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
40
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
41 # map to real operands:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
42 rops = tuple(map(self.mapOperand, vi.operands))
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
43 optypes = tuple(map(type, rops))
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
44
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
45 # look for a suitable instruction
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
46 for ic in self.instructions:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
47 if ic.mnemonic == vi.opcode and ic.operands == optypes:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
48 ri = ic(*rops)
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
49 return ri
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
50 raise CompilerError('No suitable instruction found')
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
51