annotate python/target.py @ 242:c94965418684

Added tag burnchain for changeset ce6d390043a7
author Windel Bouwman
date Mon, 22 Jul 2013 22:56:51 +0200
parents 8786811a5a59
children e41e4109addd
rev   line source
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
1 from asmnodes import ASymbol, AInstruction, 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
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
36 class Imm32:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
37 def __init__(self, imm):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
38 assert imm < 2**32
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
39 assert type(imm) is int
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
40 self.imm = imm
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
41
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
42 @classmethod
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
43 def Create(cls, vop):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
44 if type(vop) is ANumber and vop.number < 2**32:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
45 return cls(vop.number)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
46
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
47
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
48 class LabelRef:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
49 def __init__(self, name):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
50 self.name = name
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
51
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
52 @classmethod
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
53 def Create(cls, vop):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
54 if type(vop) is ASymbol:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
55 return cls(vop.name)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
56
234
Windel Bouwman
parents: 219
diff changeset
57 class Instruction:
Windel Bouwman
parents: 219
diff changeset
58 def encode(self):
Windel Bouwman
parents: 219
diff changeset
59 raise NotImplementedError('Instruction {0} has no encode yet, TODO'.format(type(self)))
Windel Bouwman
parents: 219
diff changeset
60 def resolve(self, f):
Windel Bouwman
parents: 219
diff changeset
61 pass
Windel Bouwman
parents: 219
diff changeset
62
Windel Bouwman
parents: 219
diff changeset
63
Windel Bouwman
parents: 219
diff changeset
64 class PseudoInstruction(Instruction):
Windel Bouwman
parents: 219
diff changeset
65 pass
Windel Bouwman
parents: 219
diff changeset
66
Windel Bouwman
parents: 219
diff changeset
67
Windel Bouwman
parents: 219
diff changeset
68 class Label(PseudoInstruction):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
69 def __init__(self, name):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
70 self.name = name
234
Windel Bouwman
parents: 219
diff changeset
71 self.address = 0
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
72
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
73 def __repr__(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
74 return '{}:'.format(self.name)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
75
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
76 def encode(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
77 return bytes()
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
78
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
79 @classmethod
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
80 def Create(cls, vop):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
81 if type(vop) is ASymbol:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
82 name = vop.name
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
83 return cls(name)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
84
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
85
234
Windel Bouwman
parents: 219
diff changeset
86 class Comment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
87 def __init__(self, txt):
Windel Bouwman
parents: 219
diff changeset
88 self.txt = txt
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
89 def encode(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
90 return bytes()
234
Windel Bouwman
parents: 219
diff changeset
91 def __repr__(self):
Windel Bouwman
parents: 219
diff changeset
92 return '; {}'.format(self.txt)
Windel Bouwman
parents: 219
diff changeset
93
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
94
234
Windel Bouwman
parents: 219
diff changeset
95 class Alignment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
96 def __init__(self, a):
Windel Bouwman
parents: 219
diff changeset
97 self.align = a
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
98
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
99 def __repr__(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
100 return 'ALIGN({})'.format(self.align)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
101
234
Windel Bouwman
parents: 219
diff changeset
102 def encode(self):
Windel Bouwman
parents: 219
diff changeset
103 pad = []
Windel Bouwman
parents: 219
diff changeset
104 address = self.address
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
105 while (address % self.align) != 0:
234
Windel Bouwman
parents: 219
diff changeset
106 address += 1
Windel Bouwman
parents: 219
diff changeset
107 pad.append(0)
Windel Bouwman
parents: 219
diff changeset
108 return bytes(pad)
Windel Bouwman
parents: 219
diff changeset
109
Windel Bouwman
parents: 219
diff changeset
110
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
111 class Register(Operand):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
112 def __init__(self, name):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
113 self.name = name
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
114
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
115
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
116 class Target:
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
117 def __init__(self, name, desc=''):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
118 self.name = name
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
119 self.desc = desc
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
120 self.registers = []
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
121 self.instructions = []
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
122
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
123 def instruction(self, cls):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
124 """ Decorator function that registers an instruction to this target """
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
125 self.addInstruction(cls)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
126 return cls
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
127
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
128 def check(self):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
129 """ Check target """
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
130 for i in self.instructions:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
131 assert hasattr(i, 'mnemonic')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
132 assert hasattr(i, 'operands'), str(i)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
133 assert type(i.mnemonic) is str
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
134 assert type(i.operands) is tuple, str(i)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
135
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
136 def addInstruction(self, ins_class):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
137 self.instructions.append(ins_class)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
138
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
139 def mapOperand(self, operand):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
140 """ Try to map an operand to a target type """
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
141 if type(operand) is ASymbol:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
142 # Try to map to register:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
143 regs = {}
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
144 for r in self.registers:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
145 regs[r.name] = r
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
146 if operand.name in regs:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
147 return regs[operand.name]
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
148 raise CompilerError('Cannot map {0}'.format(operand))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
149
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
150 def mapInstruction(self, vi):
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
151 assert type(vi) is AInstruction
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
152 """ Map ast tree to real instruction for this target """
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
153
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
154 # map to real operands:
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
155 if vi.mnemonic.upper() == 'ALIGN' and len(vi.operands) == 1:
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
156 if type(vi.operands[0]) == ANumber:
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
157 return Alignment(vi.operands[0].number)
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
158
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
159 # look for a suitable instruction
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
160 for ic in self.instructions:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 206
diff changeset
161 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
162 # Try to map operands to the correct operand types:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
163 rops = [roptype.Create(vop) for roptype, vop in zip(ic.operands, vi.operands)]
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
164
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
165 # Check if we succeeded:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
166 optypes = tuple(map(type, rops))
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
167 if ic.operands == optypes:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
168 return ic(*rops)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
169 raise CompilerError('No suitable instruction found for "{0}"'.format(vi))
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
170