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