annotate python/target.py @ 234:83781bd10fdb

wip
author Windel Bouwman
date Sun, 14 Jul 2013 19:29:21 +0200
parents 1fa3e0050b49
children ff40407c0240
rev   line source
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
1 from asmnodes import ASymbol, AInstruction, ALabel, ANumber
200
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
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
13 # standard immediates:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
14
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
15 class Imm8:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
16 def __init__(self, imm):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
17 assert imm < 256
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
18 self.imm = imm
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
19
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
20 @classmethod
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
21 def Create(cls, vop):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
22 if type(vop) is ANumber and vop.number < 256:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
23 return cls(vop.number)
234
Windel Bouwman
parents: 219
diff changeset
24
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
25 class Imm3:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
26 def __init__(self, imm):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
27 assert imm < 8
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
28 assert type(imm) is int
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
29 self.imm = imm
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
30
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
31 @classmethod
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
32 def Create(cls, vop):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
33 if type(vop) is ANumber and vop.number < 8:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
34 return cls(vop.number)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
35
234
Windel Bouwman
parents: 219
diff changeset
36 class Instruction:
Windel Bouwman
parents: 219
diff changeset
37 def encode(self):
Windel Bouwman
parents: 219
diff changeset
38 raise NotImplementedError('Instruction {0} has no encode yet, TODO'.format(type(self)))
Windel Bouwman
parents: 219
diff changeset
39 def resolve(self, f):
Windel Bouwman
parents: 219
diff changeset
40 pass
Windel Bouwman
parents: 219
diff changeset
41
Windel Bouwman
parents: 219
diff changeset
42
Windel Bouwman
parents: 219
diff changeset
43 class PseudoInstruction(Instruction):
Windel Bouwman
parents: 219
diff changeset
44 pass
Windel Bouwman
parents: 219
diff changeset
45
Windel Bouwman
parents: 219
diff changeset
46
Windel Bouwman
parents: 219
diff changeset
47 class Label(PseudoInstruction):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
48 def __init__(self, name):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
49 self.name = name
234
Windel Bouwman
parents: 219
diff changeset
50 self.address = 0
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
51
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
52 @classmethod
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
53 def Create(cls, vop):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
54 if type(vop) is ASymbol:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
55 name = vop.name
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
56 return cls(name)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
57
234
Windel Bouwman
parents: 219
diff changeset
58 class Comment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
59 def __init__(self, txt):
Windel Bouwman
parents: 219
diff changeset
60 self.txt = txt
Windel Bouwman
parents: 219
diff changeset
61 def __repr__(self):
Windel Bouwman
parents: 219
diff changeset
62 return '; {}'.format(self.txt)
Windel Bouwman
parents: 219
diff changeset
63
Windel Bouwman
parents: 219
diff changeset
64 class Alignment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
65 def __init__(self, a):
Windel Bouwman
parents: 219
diff changeset
66 self.align = a
Windel Bouwman
parents: 219
diff changeset
67 def encode(self):
Windel Bouwman
parents: 219
diff changeset
68 pad = []
Windel Bouwman
parents: 219
diff changeset
69 address = self.address
Windel Bouwman
parents: 219
diff changeset
70 while (address % i.align) != 0:
Windel Bouwman
parents: 219
diff changeset
71 address += 1
Windel Bouwman
parents: 219
diff changeset
72 pad.append(0)
Windel Bouwman
parents: 219
diff changeset
73 return bytes(pad)
Windel Bouwman
parents: 219
diff changeset
74
Windel Bouwman
parents: 219
diff changeset
75
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
76 class Register(Operand):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
77 def __init__(self, name):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
78 self.name = name
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
79
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
80
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
81 class Target:
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
82 def __init__(self, name, desc=''):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
83 self.name = name
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
84 self.desc = desc
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
85 self.registers = []
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
86 self.instructions = []
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
87
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
88 def instruction(self, cls):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
89 """ Decorator function that registers an instruction to this target """
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
90 self.addInstruction(cls)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
91 return cls
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
92
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
93 def check(self):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
94 """ Check target """
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
95 for i in self.instructions:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
96 assert hasattr(i, 'mnemonic')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
97 assert hasattr(i, 'operands'), str(i)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
98 assert type(i.mnemonic) is str
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
99 assert type(i.operands) is tuple, str(i)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
100
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
101 def addInstruction(self, ins_class):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
102 self.instructions.append(ins_class)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
103
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
104 def mapOperand(self, operand):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
105 """ Try to map an operand to a target type """
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
106 if type(operand) is ASymbol:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
107 # Try to map to register:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
108 regs = {}
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
109 for r in self.registers:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
110 regs[r.name] = r
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
111 if operand.name in regs:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
112 return regs[operand.name]
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
113 raise CompilerError('Cannot map {0}'.format(operand))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
114
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
115 def mapInstruction(self, vi):
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
116 assert type(vi) is AInstruction
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
117 """ Map ast tree to real instruction for this target """
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
118
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
119 # map to real operands:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
120
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
121 # look for a suitable instruction
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
122 for ic in self.instructions:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 206
diff changeset
123 if ic.mnemonic.upper() == vi.mnemonic.upper() and len(ic.operands) == len(vi.operands):
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
124 # Try to map operands to the correct operand types:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
125 rops = [roptype.Create(vop) for roptype, vop in zip(ic.operands, vi.operands)]
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
126
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
127 # Check if we succeeded:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
128 optypes = tuple(map(type, rops))
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
129 if ic.operands == optypes:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
130 return ic(*rops)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
131 raise CompilerError('No suitable instruction found for "{0}"'.format(vi))
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
132