annotate python/target/basetarget.py @ 290:7b38782ed496

File moves
author Windel Bouwman
date Sun, 24 Nov 2013 11:24:15 +0100
parents python/target.py@02385f62f250
children 534b94b40aa8
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
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
25 class Imm7:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
26 def __init__(self, imm):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
27 assert imm < 128
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
28 self.imm = imm
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
29
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
30 @classmethod
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
31 def Create(cls, vop):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
32 if type(vop) is ANumber and vop.number < 128:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
33 return cls(vop.number)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
34
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
35 class Imm3:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
36 def __init__(self, imm):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
37 assert imm < 8
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
38 assert type(imm) is int
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
39 self.imm = imm
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
40
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
41 @classmethod
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
42 def Create(cls, vop):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
43 if type(vop) is ANumber and vop.number < 8:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
44 return cls(vop.number)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
45
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
46 class Imm32:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
47 def __init__(self, imm):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
48 assert imm < 2**32
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
49 assert type(imm) is int
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
50 self.imm = imm
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 ANumber and vop.number < 2**32:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
55 return cls(vop.number)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
56
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
57
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
58 class LabelRef:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
59 def __init__(self, name):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
60 assert type(name) is str
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
61 self.name = name
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
62
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
63 @classmethod
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
64 def Create(cls, vop):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
65 if type(vop) is ASymbol:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
66 return cls(vop.name)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
67
234
Windel Bouwman
parents: 219
diff changeset
68 class Instruction:
Windel Bouwman
parents: 219
diff changeset
69 def encode(self):
Windel Bouwman
parents: 219
diff changeset
70 raise NotImplementedError('Instruction {0} has no encode yet, TODO'.format(type(self)))
Windel Bouwman
parents: 219
diff changeset
71 def resolve(self, f):
Windel Bouwman
parents: 219
diff changeset
72 pass
Windel Bouwman
parents: 219
diff changeset
73
Windel Bouwman
parents: 219
diff changeset
74
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
75 class Nop(Instruction):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
76 """ Instruction that does nothing and has zero size """
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
77 def encode(self):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
78 return bytes()
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
79
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
80
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
81
234
Windel Bouwman
parents: 219
diff changeset
82 class PseudoInstruction(Instruction):
Windel Bouwman
parents: 219
diff changeset
83 pass
Windel Bouwman
parents: 219
diff changeset
84
Windel Bouwman
parents: 219
diff changeset
85
Windel Bouwman
parents: 219
diff changeset
86 class Label(PseudoInstruction):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
87 def __init__(self, name):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
88 self.name = name
234
Windel Bouwman
parents: 219
diff changeset
89 self.address = 0
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
90
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
91 def __repr__(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
92 return '{}:'.format(self.name)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
93
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
94 def encode(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
95 return bytes()
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
96
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
97 @classmethod
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
98 def Create(cls, vop):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
99 if type(vop) is ASymbol:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
100 name = vop.name
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
101 return cls(name)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
102
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
103
234
Windel Bouwman
parents: 219
diff changeset
104 class Comment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
105 def __init__(self, txt):
Windel Bouwman
parents: 219
diff changeset
106 self.txt = txt
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
107
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
108 def encode(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
109 return bytes()
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
110
234
Windel Bouwman
parents: 219
diff changeset
111 def __repr__(self):
Windel Bouwman
parents: 219
diff changeset
112 return '; {}'.format(self.txt)
Windel Bouwman
parents: 219
diff changeset
113
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
114
234
Windel Bouwman
parents: 219
diff changeset
115 class Alignment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
116 def __init__(self, a):
Windel Bouwman
parents: 219
diff changeset
117 self.align = a
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
118
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
119 def __repr__(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
120 return 'ALIGN({})'.format(self.align)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
121
234
Windel Bouwman
parents: 219
diff changeset
122 def encode(self):
Windel Bouwman
parents: 219
diff changeset
123 pad = []
Windel Bouwman
parents: 219
diff changeset
124 address = self.address
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
125 while (address % self.align) != 0:
234
Windel Bouwman
parents: 219
diff changeset
126 address += 1
Windel Bouwman
parents: 219
diff changeset
127 pad.append(0)
Windel Bouwman
parents: 219
diff changeset
128 return bytes(pad)
Windel Bouwman
parents: 219
diff changeset
129
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
130 class DebugInfo(PseudoInstruction):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
131 def __init__(self, i):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
132 self.info = i
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
133
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
134 def __repr__(self):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
135 return 'DebugInfo: {}'.format(self.info)
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
136
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
137 def encode(self):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
138 return bytes()
234
Windel Bouwman
parents: 219
diff changeset
139
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
140 class Register(Operand):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
141 def __init__(self, name):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
142 self.name = name
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
143
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
144
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
145 class Target:
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
146 def __init__(self, name, desc=''):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
147 self.name = name
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
148 self.desc = desc
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
149 self.registers = []
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
150 self.instructions = []
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
151
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
152 def instruction(self, cls):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
153 """ Decorator function that registers an instruction to this target """
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
154 self.addInstruction(cls)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
155 return cls
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
156
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
157 def check(self):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
158 """ Check target """
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
159 for i in self.instructions:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
160 assert hasattr(i, 'mnemonic')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
161 assert hasattr(i, 'operands'), str(i)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
162 assert type(i.mnemonic) is str
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
163 assert type(i.operands) is tuple, str(i)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
164
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
165 def addInstruction(self, ins_class):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
166 self.instructions.append(ins_class)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
167
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
168 def mapOperand(self, operand):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
169 """ Try to map an operand to a target type """
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
170 if type(operand) is ASymbol:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
171 # Try to map to register:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
172 regs = {}
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
173 for r in self.registers:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
174 regs[r.name] = r
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
175 if operand.name in regs:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
176 return regs[operand.name]
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
177 raise CompilerError('Cannot map {0}'.format(operand))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
178
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
179 def mapInstruction(self, vi):
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
180 assert type(vi) is AInstruction
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
181 """ Map ast tree to real instruction for this target """
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
182
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
183 # map to real operands:
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
184 if vi.mnemonic.upper() == 'ALIGN' and len(vi.operands) == 1:
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
185 if type(vi.operands[0]) == ANumber:
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
186 return Alignment(vi.operands[0].number)
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
187
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
188 # look for a suitable instruction
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
189 for ic in self.instructions:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 206
diff changeset
190 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
191 # Try to map operands to the correct operand types:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
192 rops = [roptype.Create(vop) for roptype, vop in zip(ic.operands, vi.operands)]
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
193
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
194 # Check if we succeeded:
277
046017431c6a Started register allocator
Windel Bouwman
parents: 275
diff changeset
195 if all(isinstance(rop, optype) for rop, optype in zip(rops, ic.operands)):
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
196 return ic(*rops)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
197 raise CompilerError('No suitable instruction found for "{0}"'.format(vi))
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
198