comparison python/ppci/target/thumb/instructions.py @ 346:3bb7dcfe5529

expanded arm target
author Windel Bouwman
date Fri, 07 Mar 2014 17:05:32 +0100
parents 86b02c98a717
children
comparison
equal deleted inserted replaced
345:b4882ff0ed06 346:3bb7dcfe5529
1 from ..basetarget import Register, Instruction, Target, Label 1 from ..basetarget import Register, Instruction, Target, Label
2 from ..basetarget import Imm32, Imm8, Imm7, Imm3
3 from ..token import u16, u32 2 from ..token import u16, u32
4 from .armtoken import ThumbToken 3 from .armtoken import ThumbToken
5 from ..arm.registers import R0, ArmRegister, SP 4 from ..arm.registers import R0, ArmRegister, SP
6 5
7 6
11 pass 10 pass
12 11
13 12
14 class Dcd(ThumbInstruction): 13 class Dcd(ThumbInstruction):
15 def __init__(self, expr): 14 def __init__(self, expr):
16 if isinstance(expr, Imm32): 15 if isinstance(expr, int):
17 self.expr = expr.imm
18 self.label = None
19 elif isinstance(expr, int):
20 self.expr = expr 16 self.expr = expr
21 self.label = None 17 self.label = None
22 else: 18 else:
23 raise NotImplementedError() 19 raise NotImplementedError()
24 20
69 65
70 66
71 class Str2(LS_imm5_base): 67 class Str2(LS_imm5_base):
72 opcode = 0xC 68 opcode = 0xC
73 69
74 @classmethod
75 def fromim(cls, im):
76 return cls(im.src[1], im.src[0], im.others[0])
77
78 70
79 class Ldr2(LS_imm5_base): 71 class Ldr2(LS_imm5_base):
80 opcode = 0xD 72 opcode = 0xD
81 73
82 @classmethod
83 def fromim(cls, im):
84 return cls(im.dst[0], im.src[0], im.others[0])
85 74
86 class ls_sp_base_imm8(ThumbInstruction): 75 class ls_sp_base_imm8(ThumbInstruction):
87 def __init__(self, rt, offset): 76 def __init__(self, rt, offset):
88 self.rt = rt 77 self.rt = rt
89 self.offset = offset 78 self.offset = offset
98 87
99 def __repr__(self): 88 def __repr__(self):
100 mnemonic = self.__class__.__name__ 89 mnemonic = self.__class__.__name__
101 return '{} {}, [sp,#{}]'.format(mnemonic, self.rt, self.offset) 90 return '{} {}, [sp,#{}]'.format(mnemonic, self.rt, self.offset)
102 91
103 def align(x, m):
104 while ((x % m) != 0):
105 x = x + 1
106 return x
107 92
108 def Ldr(*args): 93 def Ldr(*args):
109 if len(args) == 2 and isinstance(args[0], ArmRegister) \ 94 if len(args) == 2 and isinstance(args[0], ArmRegister) \
110 and isinstance(args[1], str): 95 and isinstance(args[1], str):
111 return Ldr3(*args) 96 return Ldr3(*args)
113 raise Exception() 98 raise Exception()
114 99
115 100
116 class Ldr3(ThumbInstruction): 101 class Ldr3(ThumbInstruction):
117 """ ldr Rt, LABEL, load value from pc relative position """ 102 """ ldr Rt, LABEL, load value from pc relative position """
118 mnemonic = 'ldr'
119 def __init__(self, rt, label): 103 def __init__(self, rt, label):
120 self.rt = rt 104 self.rt = rt
121 self.label = label 105 self.label = label
122
123 @classmethod
124 def fromim(cls, im):
125 return cls(im.dst[0], im.others[0])
126 106
127 def relocations(self): 107 def relocations(self):
128 return [(self.label, 'lit_add_8')] 108 return [(self.label, 'lit_add_8')]
129 109
130 def encode(self): 110 def encode(self):
155 assert imm < 256 135 assert imm < 256
156 self.imm = imm 136 self.imm = imm
157 self.rd = rd 137 self.rd = rd
158 self.token = ThumbToken() 138 self.token = ThumbToken()
159 139
160 @classmethod
161 def fromim(cls, im):
162 return cls(im.dst[0], im.others[0])
163
164 def encode(self): 140 def encode(self):
165 rd = self.rd.num 141 rd = self.rd.num
166 self.token[8:11] = rd 142 self.token[8:11] = rd
167 self.token[0:8] = self.imm 143 self.token[0:8] = self.imm
168 self.token[11:16] = self.opcode 144 self.token[11:16] = self.opcode
170 146
171 def __repr__(self): 147 def __repr__(self):
172 return 'MOV {}, {}'.format(self.rd, self.imm) 148 return 'MOV {}, {}'.format(self.rd, self.imm)
173 149
174 150
175
176 # Arithmatics: 151 # Arithmatics:
177
178 152
179 153
180 class regregimm3_base(ThumbInstruction): 154 class regregimm3_base(ThumbInstruction):
181 def __init__(self, rd, rn, imm3): 155 def __init__(self, rd, rn, imm3):
182 self.rd = rd 156 self.rd = rd
183 self.rn = rn 157 self.rn = rn
184 assert imm3 < 8 158 assert imm3 < 8
185 self.imm3 = imm3 159 self.imm3 = imm3
186 self.token = ThumbToken() 160 self.token = ThumbToken()
187
188 @classmethod
189 def fromim(cls, im):
190 return cls(im.dst[0], im.src[0], im.others[0])
191 161
192 def encode(self): 162 def encode(self):
193 rd = self.rd.num 163 rd = self.rd.num
194 self.token[0:3] = rd 164 self.token[0:3] = rd
195 self.token[3:6] = self.rn.num 165 self.token[3:6] = self.rn.num
242 def __init__(self, rd, rn, rm): 212 def __init__(self, rd, rn, rm):
243 self.rd = rd 213 self.rd = rd
244 self.rn = rn 214 self.rn = rn
245 self.rm = rm 215 self.rm = rm
246 216
247 @classmethod
248 def fromim(cls, im):
249 return cls(im.dst[0], im.src[0], im.src[1])
250
251 def encode(self): 217 def encode(self):
252 at = ThumbToken() 218 at = ThumbToken()
253 at.rd = self.rd.num 219 at.rd = self.rd.num
254 rn = self.rn.num 220 rn = self.rn.num
255 rm = self.rm.num 221 rm = self.rm.num
276 """ mov rd, rm """ 242 """ mov rd, rm """
277 mnemonic = 'MOV' 243 mnemonic = 'MOV'
278 def __init__(self, rd, rm): 244 def __init__(self, rd, rm):
279 self.rd = rd 245 self.rd = rd
280 self.rm = rm 246 self.rm = rm
281
282 @classmethod
283 def fromim(cls, im):
284 return cls(im.dst[0], im.src[0])
285 247
286 def encode(self): 248 def encode(self):
287 at = ThumbToken() 249 at = ThumbToken()
288 at.rd = self.rd.num & 0x7 250 at.rd = self.rd.num & 0x7
289 D = (self.rd.num >> 3) & 0x1 251 D = (self.rd.num >> 3) & 0x1
303 mnemonic = 'MUL' 265 mnemonic = 'MUL'
304 def __init__(self, rn, rdm): 266 def __init__(self, rn, rdm):
305 self.rn = rn 267 self.rn = rn
306 self.rdm = rdm 268 self.rdm = rdm
307 269
308 @classmethod
309 def fromim(cls, im):
310 assert im.src[1] is im.dst[0]
311 return cls(im.src[0], im.dst[0])
312
313 def encode(self): 270 def encode(self):
314 at = ThumbToken() 271 at = ThumbToken()
315 rn = self.rn.num 272 rn = self.rn.num
316 at.rd = self.rdm.num 273 at.rd = self.rdm.num
317 opcode = 0b0100001101 274 opcode = 0b0100001101
327 class regreg_base(ThumbInstruction): 284 class regreg_base(ThumbInstruction):
328 """ ??? Rdn, Rm """ 285 """ ??? Rdn, Rm """
329 def __init__(self, rdn, rm): 286 def __init__(self, rdn, rm):
330 self.rdn = rdn 287 self.rdn = rdn
331 self.rm = rm 288 self.rm = rm
332
333 @classmethod
334 def fromim(cls, im):
335 return cls(im.src[0], im.src[1])
336 289
337 def encode(self): 290 def encode(self):
338 at = ThumbToken() 291 at = ThumbToken()
339 at.rd = self.rdn.num 292 at.rd = self.rdn.num
340 rm = self.rm.num 293 rm = self.rm.num