annotate python/target.py @ 278:9fca39eebe50

First implementation of regalloc with coalsesc
author Windel Bouwman
date Sun, 29 Sep 2013 14:08:15 +0200
parents 046017431c6a
children 02385f62f250
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):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
60 self.name = name
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
61
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
62 @classmethod
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
63 def Create(cls, vop):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
64 if type(vop) is ASymbol:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
65 return cls(vop.name)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
66
234
Windel Bouwman
parents: 219
diff changeset
67 class Instruction:
Windel Bouwman
parents: 219
diff changeset
68 def encode(self):
Windel Bouwman
parents: 219
diff changeset
69 raise NotImplementedError('Instruction {0} has no encode yet, TODO'.format(type(self)))
Windel Bouwman
parents: 219
diff changeset
70 def resolve(self, f):
Windel Bouwman
parents: 219
diff changeset
71 pass
Windel Bouwman
parents: 219
diff changeset
72
Windel Bouwman
parents: 219
diff changeset
73
Windel Bouwman
parents: 219
diff changeset
74 class PseudoInstruction(Instruction):
Windel Bouwman
parents: 219
diff changeset
75 pass
Windel Bouwman
parents: 219
diff changeset
76
Windel Bouwman
parents: 219
diff changeset
77
Windel Bouwman
parents: 219
diff changeset
78 class Label(PseudoInstruction):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
79 def __init__(self, name):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
80 self.name = name
234
Windel Bouwman
parents: 219
diff changeset
81 self.address = 0
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
82
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
83 def __repr__(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
84 return '{}:'.format(self.name)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
85
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
86 def encode(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
87 return bytes()
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
88
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
89 @classmethod
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
90 def Create(cls, vop):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
91 if type(vop) is ASymbol:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
92 name = vop.name
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
93 return cls(name)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
94
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
95
234
Windel Bouwman
parents: 219
diff changeset
96 class Comment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
97 def __init__(self, txt):
Windel Bouwman
parents: 219
diff changeset
98 self.txt = txt
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
99
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
100 def encode(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
101 return bytes()
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
102
234
Windel Bouwman
parents: 219
diff changeset
103 def __repr__(self):
Windel Bouwman
parents: 219
diff changeset
104 return '; {}'.format(self.txt)
Windel Bouwman
parents: 219
diff changeset
105
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
106
234
Windel Bouwman
parents: 219
diff changeset
107 class Alignment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
108 def __init__(self, a):
Windel Bouwman
parents: 219
diff changeset
109 self.align = a
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
110
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
111 def __repr__(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
112 return 'ALIGN({})'.format(self.align)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
113
234
Windel Bouwman
parents: 219
diff changeset
114 def encode(self):
Windel Bouwman
parents: 219
diff changeset
115 pad = []
Windel Bouwman
parents: 219
diff changeset
116 address = self.address
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
117 while (address % self.align) != 0:
234
Windel Bouwman
parents: 219
diff changeset
118 address += 1
Windel Bouwman
parents: 219
diff changeset
119 pad.append(0)
Windel Bouwman
parents: 219
diff changeset
120 return bytes(pad)
Windel Bouwman
parents: 219
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
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
132 class Register(Operand):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
133 def __init__(self, name):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
134 self.name = name
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
135
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
136
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
137 class Target:
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
138 def __init__(self, name, desc=''):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
139 self.name = name
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
140 self.desc = desc
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
141 self.registers = []
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
142 self.instructions = []
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
143
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
144 def instruction(self, cls):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
145 """ Decorator function that registers an instruction to this target """
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
146 self.addInstruction(cls)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
147 return cls
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
148
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
149 def check(self):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
150 """ Check target """
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
151 for i in self.instructions:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
152 assert hasattr(i, 'mnemonic')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
153 assert hasattr(i, 'operands'), str(i)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
154 assert type(i.mnemonic) is str
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
155 assert type(i.operands) is tuple, str(i)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
156
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
157 def addInstruction(self, ins_class):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
158 self.instructions.append(ins_class)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
159
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
160 def mapOperand(self, operand):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
161 """ Try to map an operand to a target type """
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
162 if type(operand) is ASymbol:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
163 # Try to map to register:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
164 regs = {}
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
165 for r in self.registers:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
166 regs[r.name] = r
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
167 if operand.name in regs:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
168 return regs[operand.name]
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
169 raise CompilerError('Cannot map {0}'.format(operand))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
170
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
171 def mapInstruction(self, vi):
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
172 assert type(vi) is AInstruction
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
173 """ Map ast tree to real instruction for this target """
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
174
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
175 # map to real operands:
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
176 if vi.mnemonic.upper() == 'ALIGN' and len(vi.operands) == 1:
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
177 if type(vi.operands[0]) == ANumber:
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
178 return Alignment(vi.operands[0].number)
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
179
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
180 # look for a suitable instruction
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
181 for ic in self.instructions:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 206
diff changeset
182 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
183 # Try to map operands to the correct operand types:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
184 rops = [roptype.Create(vop) for roptype, vop in zip(ic.operands, vi.operands)]
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
185
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
186 # Check if we succeeded:
277
046017431c6a Started register allocator
Windel Bouwman
parents: 275
diff changeset
187 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
188 return ic(*rops)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
189 raise CompilerError('No suitable instruction found for "{0}"'.format(vi))
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
190