annotate python/target.py @ 220:3f6c30a5d234

Major change in expression parsing to enable pointers and structs
author Windel Bouwman
date Sat, 06 Jul 2013 21:32:20 +0200
parents 1fa3e0050b49
children 83781bd10fdb
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)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
24
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
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
36 class Label:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
37 def __init__(self, name):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
38 self.name = name
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
39
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
40 @classmethod
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
41 def Create(cls, vop):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
42 if type(vop) is ASymbol:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
43 name = vop.name
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
44 return cls(name)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
45
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
46 class Register(Operand):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
47 def __init__(self, name):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
48 self.name = name
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
49
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
50 class Instruction:
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
51 def encode(self):
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
52 raise NotImplementedError('Instruction {0} has no encode yet, TODO'.format(type(self)))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
53
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
54 class Target:
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
55 def __init__(self, name, desc=''):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
56 self.name = name
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
57 self.desc = desc
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
58 self.registers = []
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
59 self.instructions = []
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
60
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
61 def instruction(self, cls):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
62 """ Decorator function that registers an instruction to this target """
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
63 self.addInstruction(cls)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
64 return cls
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
65
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
66 def check(self):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
67 """ Check target """
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
68 for i in self.instructions:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
69 assert hasattr(i, 'mnemonic')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
70 assert hasattr(i, 'operands'), str(i)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
71 assert type(i.mnemonic) is str
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
72 assert type(i.operands) is tuple, str(i)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
73
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
74 def addInstruction(self, ins_class):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
75 self.instructions.append(ins_class)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
76
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
77 def mapOperand(self, operand):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
78 """ Try to map an operand to a target type """
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
79 if type(operand) is ASymbol:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
80 # Try to map to register:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
81 regs = {}
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
82 for r in self.registers:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
83 regs[r.name] = r
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
84 if operand.name in regs:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
85 return regs[operand.name]
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
86 raise CompilerError('Cannot map {0}'.format(operand))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
87
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
88 def mapInstruction(self, vi):
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
89 assert type(vi) is AInstruction
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
90 """ Map ast tree to real instruction for this target """
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
91
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
92 # map to real operands:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
93
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
94 # look for a suitable instruction
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
95 for ic in self.instructions:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 206
diff changeset
96 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
97 # Try to map operands to the correct operand types:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
98 rops = [roptype.Create(vop) for roptype, vop in zip(ic.operands, vi.operands)]
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
99
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
100 # Check if we succeeded:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
101 optypes = tuple(map(type, rops))
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
102 if ic.operands == optypes:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
103 return ic(*rops)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
104 raise CompilerError('No suitable instruction found for "{0}"'.format(vi))
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
105