annotate python/target/basetarget.py @ 334:6f4753202b9a

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