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
|
235
|
89 def encode(self):
|
|
90 return bytes()
|
234
|
91 def __repr__(self):
|
|
92 return '; {}'.format(self.txt)
|
|
93
|
235
|
94
|
234
|
95 class Alignment(PseudoInstruction):
|
|
96 def __init__(self, a):
|
|
97 self.align = a
|
235
|
98
|
|
99 def __repr__(self):
|
|
100 return 'ALIGN({})'.format(self.align)
|
|
101
|
234
|
102 def encode(self):
|
|
103 pad = []
|
|
104 address = self.address
|
235
|
105 while (address % self.align) != 0:
|
234
|
106 address += 1
|
|
107 pad.append(0)
|
|
108 return bytes(pad)
|
|
109
|
|
110
|
199
|
111 class Register(Operand):
|
|
112 def __init__(self, name):
|
|
113 self.name = name
|
|
114
|
|
115
|
|
116 class Target:
|
201
|
117 def __init__(self, name, desc=''):
|
|
118 self.name = name
|
|
119 self.desc = desc
|
200
|
120 self.registers = []
|
199
|
121 self.instructions = []
|
200
|
122
|
201
|
123 def instruction(self, cls):
|
|
124 """ Decorator function that registers an instruction to this target """
|
202
|
125 self.addInstruction(cls)
|
201
|
126 return cls
|
|
127
|
206
|
128 def check(self):
|
|
129 """ Check target """
|
|
130 for i in self.instructions:
|
|
131 assert hasattr(i, 'mnemonic')
|
|
132 assert hasattr(i, 'operands'), str(i)
|
|
133 assert type(i.mnemonic) is str
|
|
134 assert type(i.operands) is tuple, str(i)
|
|
135
|
202
|
136 def addInstruction(self, ins_class):
|
|
137 self.instructions.append(ins_class)
|
|
138
|
200
|
139 def mapOperand(self, operand):
|
|
140 """ Try to map an operand to a target type """
|
|
141 if type(operand) is ASymbol:
|
|
142 # Try to map to register:
|
|
143 regs = {}
|
|
144 for r in self.registers:
|
|
145 regs[r.name] = r
|
|
146 if operand.name in regs:
|
|
147 return regs[operand.name]
|
201
|
148 raise CompilerError('Cannot map {0}'.format(operand))
|
199
|
149
|
200
|
150 def mapInstruction(self, vi):
|
203
|
151 assert type(vi) is AInstruction
|
200
|
152 """ Map ast tree to real instruction for this target """
|
199
|
153
|
200
|
154 # map to real operands:
|
236
|
155 if vi.mnemonic.upper() == 'ALIGN' and len(vi.operands) == 1:
|
|
156 if type(vi.operands[0]) == ANumber:
|
|
157 return Alignment(vi.operands[0].number)
|
200
|
158
|
|
159 # look for a suitable instruction
|
|
160 for ic in self.instructions:
|
219
|
161 if ic.mnemonic.upper() == vi.mnemonic.upper() and len(ic.operands) == len(vi.operands):
|
203
|
162 # Try to map operands to the correct operand types:
|
|
163 rops = [roptype.Create(vop) for roptype, vop in zip(ic.operands, vi.operands)]
|
|
164
|
|
165 # Check if we succeeded:
|
|
166 optypes = tuple(map(type, rops))
|
|
167 if ic.operands == optypes:
|
|
168 return ic(*rops)
|
201
|
169 raise CompilerError('No suitable instruction found for "{0}"'.format(vi))
|
200
|
170
|