235
|
1 from asmnodes import ASymbol, AInstruction, ANumber
|
200
|
2 from ppci import CompilerError
|
199
|
3
|
|
4 """
|
|
5 Base classes for defining a target
|
|
6 """
|
|
7
|
|
8 # Machine code interface:
|
|
9 class Operand:
|
|
10 """ Single machine operand """
|
|
11 pass
|
|
12
|
206
|
13 # standard immediates:
|
|
14
|
|
15 class Imm8:
|
|
16 def __init__(self, imm):
|
|
17 assert imm < 256
|
|
18 self.imm = imm
|
|
19
|
|
20 @classmethod
|
|
21 def Create(cls, vop):
|
|
22 if type(vop) is ANumber and vop.number < 256:
|
|
23 return cls(vop.number)
|
234
|
24
|
275
|
25 class Imm7:
|
|
26 def __init__(self, imm):
|
|
27 assert imm < 128
|
|
28 self.imm = imm
|
|
29
|
|
30 @classmethod
|
|
31 def Create(cls, vop):
|
|
32 if type(vop) is ANumber and vop.number < 128:
|
|
33 return cls(vop.number)
|
|
34
|
206
|
35 class Imm3:
|
|
36 def __init__(self, imm):
|
|
37 assert imm < 8
|
|
38 assert type(imm) is int
|
|
39 self.imm = imm
|
|
40
|
|
41 @classmethod
|
|
42 def Create(cls, vop):
|
|
43 if type(vop) is ANumber and vop.number < 8:
|
|
44 return cls(vop.number)
|
|
45
|
235
|
46 class Imm32:
|
|
47 def __init__(self, imm):
|
|
48 assert imm < 2**32
|
|
49 assert type(imm) is int
|
|
50 self.imm = imm
|
|
51
|
|
52 @classmethod
|
|
53 def Create(cls, vop):
|
|
54 if type(vop) is ANumber and vop.number < 2**32:
|
|
55 return cls(vop.number)
|
|
56
|
|
57
|
|
58 class LabelRef:
|
|
59 def __init__(self, name):
|
|
60 self.name = name
|
|
61
|
|
62 @classmethod
|
|
63 def Create(cls, vop):
|
|
64 if type(vop) is ASymbol:
|
|
65 return cls(vop.name)
|
|
66
|
234
|
67 class Instruction:
|
|
68 def encode(self):
|
|
69 raise NotImplementedError('Instruction {0} has no encode yet, TODO'.format(type(self)))
|
|
70 def resolve(self, f):
|
|
71 pass
|
|
72
|
|
73
|
|
74 class PseudoInstruction(Instruction):
|
|
75 pass
|
|
76
|
|
77
|
|
78 class Label(PseudoInstruction):
|
206
|
79 def __init__(self, name):
|
|
80 self.name = name
|
234
|
81 self.address = 0
|
206
|
82
|
235
|
83 def __repr__(self):
|
|
84 return '{}:'.format(self.name)
|
|
85
|
|
86 def encode(self):
|
|
87 return bytes()
|
|
88
|
206
|
89 @classmethod
|
|
90 def Create(cls, vop):
|
|
91 if type(vop) is ASymbol:
|
|
92 name = vop.name
|
|
93 return cls(name)
|
|
94
|
235
|
95
|
234
|
96 class Comment(PseudoInstruction):
|
|
97 def __init__(self, txt):
|
|
98 self.txt = txt
|
249
|
99
|
235
|
100 def encode(self):
|
|
101 return bytes()
|
249
|
102
|
234
|
103 def __repr__(self):
|
|
104 return '; {}'.format(self.txt)
|
|
105
|
235
|
106
|
234
|
107 class Alignment(PseudoInstruction):
|
|
108 def __init__(self, a):
|
|
109 self.align = a
|
235
|
110
|
|
111 def __repr__(self):
|
|
112 return 'ALIGN({})'.format(self.align)
|
|
113
|
234
|
114 def encode(self):
|
|
115 pad = []
|
|
116 address = self.address
|
235
|
117 while (address % self.align) != 0:
|
234
|
118 address += 1
|
|
119 pad.append(0)
|
|
120 return bytes(pad)
|
|
121
|
249
|
122 class DebugInfo(PseudoInstruction):
|
|
123 def __init__(self, i):
|
|
124 self.info = i
|
|
125
|
|
126 def __repr__(self):
|
|
127 return 'DebugInfo: {}'.format(self.info)
|
|
128
|
|
129 def encode(self):
|
|
130 return bytes()
|
234
|
131
|
199
|
132 class Register(Operand):
|
|
133 def __init__(self, name):
|
|
134 self.name = name
|
|
135
|
|
136
|
|
137 class Target:
|
201
|
138 def __init__(self, name, desc=''):
|
|
139 self.name = name
|
|
140 self.desc = desc
|
200
|
141 self.registers = []
|
199
|
142 self.instructions = []
|
200
|
143
|
201
|
144 def instruction(self, cls):
|
|
145 """ Decorator function that registers an instruction to this target """
|
202
|
146 self.addInstruction(cls)
|
201
|
147 return cls
|
|
148
|
206
|
149 def check(self):
|
|
150 """ Check target """
|
|
151 for i in self.instructions:
|
|
152 assert hasattr(i, 'mnemonic')
|
|
153 assert hasattr(i, 'operands'), str(i)
|
|
154 assert type(i.mnemonic) is str
|
|
155 assert type(i.operands) is tuple, str(i)
|
|
156
|
202
|
157 def addInstruction(self, ins_class):
|
|
158 self.instructions.append(ins_class)
|
|
159
|
200
|
160 def mapOperand(self, operand):
|
|
161 """ Try to map an operand to a target type """
|
|
162 if type(operand) is ASymbol:
|
|
163 # Try to map to register:
|
|
164 regs = {}
|
|
165 for r in self.registers:
|
|
166 regs[r.name] = r
|
|
167 if operand.name in regs:
|
|
168 return regs[operand.name]
|
201
|
169 raise CompilerError('Cannot map {0}'.format(operand))
|
199
|
170
|
200
|
171 def mapInstruction(self, vi):
|
203
|
172 assert type(vi) is AInstruction
|
200
|
173 """ Map ast tree to real instruction for this target """
|
199
|
174
|
200
|
175 # map to real operands:
|
236
|
176 if vi.mnemonic.upper() == 'ALIGN' and len(vi.operands) == 1:
|
|
177 if type(vi.operands[0]) == ANumber:
|
|
178 return Alignment(vi.operands[0].number)
|
200
|
179
|
|
180 # look for a suitable instruction
|
|
181 for ic in self.instructions:
|
219
|
182 if ic.mnemonic.upper() == vi.mnemonic.upper() and len(ic.operands) == len(vi.operands):
|
203
|
183 # Try to map operands to the correct operand types:
|
|
184 rops = [roptype.Create(vop) for roptype, vop in zip(ic.operands, vi.operands)]
|
|
185
|
|
186 # Check if we succeeded:
|
|
187 optypes = tuple(map(type, rops))
|
|
188 if ic.operands == optypes:
|
|
189 return ic(*rops)
|
201
|
190 raise CompilerError('No suitable instruction found for "{0}"'.format(vi))
|
200
|
191
|