blob: a3fa138ba64ee383de9e8974bcbce15947d6dcc3 [file] [log] [blame]
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001//===- ARMDisassembler.cpp - Disassembler for ARM/Thumb ISA -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Johnny Chenb68a3ee2010-04-02 22:27:38 +00009
10#define DEBUG_TYPE "arm-disassembler"
11
12#include "ARMDisassembler.h"
Owen Anderson8d7d2e12011-08-09 20:55:18 +000013#include "ARM.h"
14#include "ARMRegisterInfo.h"
15#include "MCTargetDesc/ARMAddressingModes.h"
16#include "MCTargetDesc/ARMBaseInfo.h"
Sean Callanan9899f702010-04-13 21:21:57 +000017#include "llvm/MC/EDInstInfo.h"
Johnny Chenb68a3ee2010-04-02 22:27:38 +000018#include "llvm/MC/MCInst.h"
Owen Anderson8d7d2e12011-08-09 20:55:18 +000019#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCContext.h"
Johnny Chenb68a3ee2010-04-02 22:27:38 +000021#include "llvm/Target/TargetRegistry.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/MemoryObject.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
26
Owen Anderson8d7d2e12011-08-09 20:55:18 +000027// Forward declare these because the autogenerated code will reference them.
28// Definitions are further down.
29static bool DecodeGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
30 uint64_t Address, const void *Decoder);
Owen Anderson51c98052011-08-09 22:48:45 +000031static bool DecodeGPRnopcRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
32 uint64_t Address, const void *Decoder);
Owen Anderson8d7d2e12011-08-09 20:55:18 +000033static bool DecodetGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
34 uint64_t Address, const void *Decoder);
35static bool DecodetcGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
36 uint64_t Address, const void *Decoder);
37static bool DecoderGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
38 uint64_t Address, const void *Decoder);
39static bool DecodeSPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
40 uint64_t Address, const void *Decoder);
41static bool DecodeDPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
42 uint64_t Address, const void *Decoder);
43static bool DecodeDPR_8RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
44 uint64_t Address, const void *Decoder);
45static bool DecodeDPR_VFP2RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
46 uint64_t Address, const void *Decoder);
47static bool DecodeQPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
48 uint64_t Address, const void *Decoder);
Johnny Chen270159f2010-08-12 01:40:54 +000049
Owen Anderson8d7d2e12011-08-09 20:55:18 +000050static bool DecodePredicateOperand(llvm::MCInst &Inst, unsigned Val,
51 uint64_t Address, const void *Decoder);
52static bool DecodeCCOutOperand(llvm::MCInst &Inst, unsigned Val,
53 uint64_t Address, const void *Decoder);
54static bool DecodeSOImmOperand(llvm::MCInst &Inst, unsigned Val,
55 uint64_t Address, const void *Decoder);
56static bool DecodeBLTargetOperand(llvm::MCInst &Inst, unsigned Val,
57 uint64_t Address, const void *Decoder);
58static bool DecodeRegListOperand(llvm::MCInst &Inst, unsigned Val,
59 uint64_t Address, const void *Decoder);
60static bool DecodeSPRRegListOperand(llvm::MCInst &Inst, unsigned Val,
61 uint64_t Address, const void *Decoder);
62static bool DecodeDPRRegListOperand(llvm::MCInst &Inst, unsigned Val,
63 uint64_t Address, const void *Decoder);
Johnny Chenb68a3ee2010-04-02 22:27:38 +000064
Owen Anderson8d7d2e12011-08-09 20:55:18 +000065static bool DecodeBitfieldMaskOperand(llvm::MCInst &Inst, unsigned Insn,
66 uint64_t Address, const void *Decoder);
67static bool DecodeCopMemInstruction(llvm::MCInst &Inst, unsigned Insn,
68 uint64_t Address, const void *Decoder);
69static bool DecodeAddrMode2IdxInstruction(llvm::MCInst &Inst, unsigned Insn,
70 uint64_t Address, const void *Decoder);
71static bool DecodeSORegMemOperand(llvm::MCInst &Inst, unsigned Insn,
72 uint64_t Address, const void *Decoder);
73static bool DecodeAddrMode3Instruction(llvm::MCInst &Inst, unsigned Insn,
74 uint64_t Address, const void *Decoder);
75static bool DecodeSORegImmOperand(llvm::MCInst &Inst, unsigned Insn,
76 uint64_t Address, const void *Decoder);
77static bool DecodeSORegRegOperand(llvm::MCInst &Inst, unsigned Insn,
78 uint64_t Address, const void *Decoder);
79
80static bool DecodeMemMultipleWritebackInstruction(llvm::MCInst & Inst,
81 unsigned Insn,
82 uint64_t Adddress,
83 const void *Decoder);
84static bool DecodeSMLAInstruction(llvm::MCInst &Inst, unsigned Insn,
85 uint64_t Address, const void *Decoder);
Owen Anderson35008c22011-08-09 23:05:39 +000086static bool DecodeCPSInstruction(llvm::MCInst &Inst, unsigned Insn,
87 uint64_t Address, const void *Decoder);
Owen Anderson8d7d2e12011-08-09 20:55:18 +000088static bool DecodeAddrModeImm12Operand(llvm::MCInst &Inst, unsigned Val,
89 uint64_t Address, const void *Decoder);
90static bool DecodeAddrMode5Operand(llvm::MCInst &Inst, unsigned Val,
91 uint64_t Address, const void *Decoder);
92static bool DecodeAddrMode7Operand(llvm::MCInst &Inst, unsigned Val,
93 uint64_t Address, const void *Decoder);
94static bool DecodeBranchImmInstruction(llvm::MCInst &Inst, unsigned Insn,
95 uint64_t Address, const void *Decoder);
96static bool DecodeVCVTImmOperand(llvm::MCInst &Inst, unsigned Val,
97 uint64_t Address, const void *Decoder);
98static bool DecodeAddrMode6Operand(llvm::MCInst &Inst, unsigned Val,
99 uint64_t Address, const void *Decoder);
100static bool DecodeVLDInstruction(llvm::MCInst &Inst, unsigned Val,
101 uint64_t Address, const void *Decoder);
102static bool DecodeVSTInstruction(llvm::MCInst &Inst, unsigned Val,
103 uint64_t Address, const void *Decoder);
104static bool DecodeVLD1DupInstruction(llvm::MCInst &Inst, unsigned Val,
105 uint64_t Address, const void *Decoder);
106static bool DecodeVLD2DupInstruction(llvm::MCInst &Inst, unsigned Val,
107 uint64_t Address, const void *Decoder);
108static bool DecodeVLD3DupInstruction(llvm::MCInst &Inst, unsigned Val,
109 uint64_t Address, const void *Decoder);
110static bool DecodeVLD4DupInstruction(llvm::MCInst &Inst, unsigned Val,
111 uint64_t Address, const void *Decoder);
112static bool DecodeNEONModImmInstruction(llvm::MCInst &Inst, unsigned Val,
113 uint64_t Address, const void *Decoder);
114static bool DecodeVSHLMaxInstruction(llvm::MCInst &Inst, unsigned Val,
115 uint64_t Address, const void *Decoder);
116static bool DecodeShiftRight8Imm(llvm::MCInst &Inst, unsigned Val,
117 uint64_t Address, const void *Decoder);
118static bool DecodeShiftRight16Imm(llvm::MCInst &Inst, unsigned Val,
119 uint64_t Address, const void *Decoder);
120static bool DecodeShiftRight32Imm(llvm::MCInst &Inst, unsigned Val,
121 uint64_t Address, const void *Decoder);
122static bool DecodeShiftRight64Imm(llvm::MCInst &Inst, unsigned Val,
123 uint64_t Address, const void *Decoder);
124static bool DecodeTBLInstruction(llvm::MCInst &Inst, unsigned Insn,
125 uint64_t Address, const void *Decoder);
126static bool DecodeVFPfpImm(llvm::MCInst &Inst, unsigned Val,
127 uint64_t Address, const void *Decoder);
128static bool DecodePostIdxReg(llvm::MCInst &Inst, unsigned Insn,
129 uint64_t Address, const void *Decoder);
130static bool DecodeCoprocessor(llvm::MCInst &Inst, unsigned Insn,
131 uint64_t Address, const void *Decoder);
132static bool DecodeAddrMode3Offset(llvm::MCInst &Inst, unsigned Insn,
133 uint64_t Address, const void *Decoder);
134
135
136static bool DecodeThumbAddSpecialReg(llvm::MCInst &Inst, uint16_t Insn,
137 uint64_t Address, const void *Decoder);
138static bool DecodeThumbBROperand(llvm::MCInst &Inst, unsigned Val,
139 uint64_t Address, const void *Decoder);
140static bool DecodeT2BROperand(llvm::MCInst &Inst, unsigned Val,
141 uint64_t Address, const void *Decoder);
142static bool DecodeThumbCmpBROperand(llvm::MCInst &Inst, unsigned Val,
143 uint64_t Address, const void *Decoder);
144static bool DecodeThumbAddrModeRR(llvm::MCInst &Inst, unsigned Val,
145 uint64_t Address, const void *Decoder);
146static bool DecodeThumbAddrModeIS(llvm::MCInst &Inst, unsigned Val,
147 uint64_t Address, const void *Decoder);
148static bool DecodeThumbAddrModePC(llvm::MCInst &Inst, unsigned Val,
149 uint64_t Address, const void *Decoder);
150static bool DecodeThumbAddrModeSP(llvm::MCInst &Inst, unsigned Val,
151 uint64_t Address, const void *Decoder);
152static bool DecodeT2AddrModeSOReg(llvm::MCInst &Inst, unsigned Val,
153 uint64_t Address, const void *Decoder);
154static bool DecodeT2LoadShift(llvm::MCInst &Inst, unsigned Val,
155 uint64_t Address, const void *Decoder);
156static bool DecodeT2Imm8S4(llvm::MCInst &Inst, unsigned Val,
157 uint64_t Address, const void *Decoder);
158static bool DecodeT2AddrModeImm8s4(llvm::MCInst &Inst, unsigned Val,
159 uint64_t Address, const void *Decoder);
160static bool DecodeT2Imm8(llvm::MCInst &Inst, unsigned Val,
161 uint64_t Address, const void *Decoder);
162static bool DecodeT2AddrModeImm8(llvm::MCInst &Inst, unsigned Val,
163 uint64_t Address, const void *Decoder);
164static bool DecodeThumbAddSPImm(llvm::MCInst &Inst, uint16_t Val,
165 uint64_t Address, const void *Decoder);
166static bool DecodeThumbAddSPReg(llvm::MCInst &Inst, uint16_t Insn,
167 uint64_t Address, const void *Decoder);
168static bool DecodeThumbCPS(llvm::MCInst &Inst, uint16_t Insn,
169 uint64_t Address, const void *Decoder);
170static bool DecodeThumbBLXOffset(llvm::MCInst &Inst, unsigned Insn,
171 uint64_t Address, const void *Decoder);
172static bool DecodeT2AddrModeImm12(llvm::MCInst &Inst, unsigned Val,
173 uint64_t Address, const void *Decoder);
174static bool DecodeThumbSRImm(llvm::MCInst &Inst, unsigned Val,
175 uint64_t Address, const void *Decoder);
176static bool DecodeThumb2BCCInstruction(llvm::MCInst &Inst, unsigned Val,
177 uint64_t Address, const void *Decoder);
178static bool DecodeT2SOImm(llvm::MCInst &Inst, unsigned Val,
179 uint64_t Address, const void *Decoder);
180static bool DecodeThumbBCCTargetOperand(llvm::MCInst &Inst, unsigned Val,
181 uint64_t Address, const void *Decoder);
182static bool DecodeThumbBLTargetOperand(llvm::MCInst &Inst, unsigned Val,
183 uint64_t Address, const void *Decoder);
184
185#include "ARMGenDisassemblerTables.inc"
186#include "ARMGenInstrInfo.inc"
Oscar Fuentes38e13902010-09-28 11:48:19 +0000187#include "ARMGenEDInfo.inc"
Sean Callanan9899f702010-04-13 21:21:57 +0000188
189using namespace llvm;
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000190
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000191static MCDisassembler *createARMDisassembler(const Target &T) {
192 return new ARMDisassembler;
193}
194
195static MCDisassembler *createThumbDisassembler(const Target &T) {
196 return new ThumbDisassembler;
197}
198
Sean Callanan9899f702010-04-13 21:21:57 +0000199EDInstInfo *ARMDisassembler::getEDInfo() const {
200 return instInfoARM;
201}
202
203EDInstInfo *ThumbDisassembler::getEDInfo() const {
204 return instInfoARM;
205}
Owen Anderson8d7d2e12011-08-09 20:55:18 +0000206
207
208bool ARMDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
209 const MemoryObject &Region,
210 uint64_t Address,raw_ostream &os) const {
211 uint8_t bytes[4];
212
213 // We want to read exactly 4 bytes of data.
214 if (Region.readBytes(Address, 4, (uint8_t*)bytes, NULL) == -1)
215 return false;
216
217 // Encoded as a small-endian 32-bit word in the stream.
218 uint32_t insn = (bytes[3] << 24) |
219 (bytes[2] << 16) |
220 (bytes[1] << 8) |
221 (bytes[0] << 0);
222
223 // Calling the auto-generated decoder function.
224 bool result = decodeARMInstruction32(MI, insn, Address, this);
225 if (result) {
226 Size = 4;
227 return true;
228 }
229
230 // Instructions that are shared between ARM and Thumb modes.
231 // FIXME: This shouldn't really exist. It's an artifact of the
232 // fact that we fail to encode a few instructions properly for Thumb.
233 MI.clear();
234 result = decodeCommonInstruction32(MI, insn, Address, this);
235 if (result) {
236 Size = 4;
237 return true;
238 }
239
240 // VFP and NEON instructions, similarly, are shared between ARM
241 // and Thumb modes.
242 MI.clear();
243 result = decodeVFPInstruction32(MI, insn, Address, this);
244 if (result) {
245 Size = 4;
246 return true;
247 }
248
249 MI.clear();
250 result = decodeNEONInstruction32(MI, insn, Address, this);
251 if (result) {
252 // Add a fake predicate operand, because we share these instruction
253 // definitions with Thumb2 where these instructions are predicable.
254 if (!DecodePredicateOperand(MI, 0xE, Address, this)) return false;
255 Size = 4;
256 return true;
257 }
258
259 MI.clear();
260
261 return false;
262}
263
264namespace llvm {
265extern MCInstrDesc ARMInsts[];
266}
267
268// Thumb1 instructions don't have explicit S bits. Rather, they
269// implicitly set CPSR. Since it's not represented in the encoding, the
270// auto-generated decoder won't inject the CPSR operand. We need to fix
271// that as a post-pass.
272static void AddThumb1SBit(MCInst &MI, bool InITBlock) {
273 const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
274 MCInst::iterator I = MI.begin();
275 for (unsigned i = 0; i < MI.size(); ++i, ++I) {
276 if (OpInfo[i].isOptionalDef() && OpInfo[i].RegClass == ARM::CCRRegClassID) {
277 MI.insert(I, MCOperand::CreateReg(InITBlock ? 0 : ARM::CPSR));
278 return;
279 }
280 }
281
282 if (OpInfo[MI.size()].isOptionalDef() &&
283 OpInfo[MI.size()].RegClass == ARM::CCRRegClassID)
284 MI.insert(MI.end(), MCOperand::CreateReg(InITBlock ? 0 : ARM::CPSR));
285}
286
287// Most Thumb instructions don't have explicit predicates in the
288// encoding, but rather get their predicates from IT context. We need
289// to fix up the predicate operands using this context information as a
290// post-pass.
291void ThumbDisassembler::AddThumbPredicate(MCInst &MI) const {
292 // A few instructions actually have predicates encoded in them. Don't
293 // try to overwrite it if we're seeing one of those.
294 switch (MI.getOpcode()) {
295 case ARM::tBcc:
296 case ARM::t2Bcc:
297 return;
298 default:
299 break;
300 }
301
302 // If we're in an IT block, base the predicate on that. Otherwise,
303 // assume a predicate of AL.
304 unsigned CC;
305 if (ITBlock.size()) {
306 CC = ITBlock.back();
307 ITBlock.pop_back();
308 } else
309 CC = ARMCC::AL;
310
311 const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
312 MCInst::iterator I = MI.begin();
313 for (unsigned i = 0; i < MI.size(); ++i, ++I) {
314 if (OpInfo[i].isPredicate()) {
315 I = MI.insert(I, MCOperand::CreateImm(CC));
316 ++I;
317 if (CC == ARMCC::AL)
318 MI.insert(I, MCOperand::CreateReg(0));
319 else
320 MI.insert(I, MCOperand::CreateReg(ARM::CPSR));
321 return;
322 }
323 }
324
325 MI.insert(MI.end(), MCOperand::CreateImm(CC));
326 if (CC == ARMCC::AL)
327 MI.insert(MI.end(), MCOperand::CreateReg(0));
328 else
329 MI.insert(MI.end(), MCOperand::CreateReg(ARM::CPSR));
330}
331
332// Thumb VFP instructions are a special case. Because we share their
333// encodings between ARM and Thumb modes, and they are predicable in ARM
334// mode, the auto-generated decoder will give them an (incorrect)
335// predicate operand. We need to rewrite these operands based on the IT
336// context as a post-pass.
337void ThumbDisassembler::UpdateThumbVFPPredicate(MCInst &MI) const {
338 unsigned CC;
339 if (ITBlock.size()) {
340 CC = ITBlock.back();
341 ITBlock.pop_back();
342 } else
343 CC = ARMCC::AL;
344
345 const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
346 MCInst::iterator I = MI.begin();
347 for (unsigned i = 0; i < MI.size(); ++i, ++I) {
348 if (OpInfo[i].isPredicate() ) {
349 I->setImm(CC);
350 ++I;
351 if (CC == ARMCC::AL)
352 I->setReg(0);
353 else
354 I->setReg(ARM::CPSR);
355 return;
356 }
357 }
358}
359
360
361bool ThumbDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
362 const MemoryObject &Region,
363 uint64_t Address,raw_ostream &os) const {
364 uint8_t bytes[4];
365
366 // We want to read exactly 2 bytes of data.
367 if (Region.readBytes(Address, 2, (uint8_t*)bytes, NULL) == -1)
368 return false;
369
370 uint16_t insn16 = (bytes[1] << 8) | bytes[0];
371 bool result = decodeThumbInstruction16(MI, insn16, Address, this);
372 if (result) {
373 Size = 2;
374 bool InITBlock = ITBlock.size();
375 AddThumbPredicate(MI);
376 AddThumb1SBit(MI, InITBlock);
377 return true;
378 }
379
380 MI.clear();
381 result = decodeThumb2Instruction16(MI, insn16, Address, this);
382 if (result) {
383 Size = 2;
384 AddThumbPredicate(MI);
385
386 // If we find an IT instruction, we need to parse its condition
387 // code and mask operands so that we can apply them correctly
388 // to the subsequent instructions.
389 if (MI.getOpcode() == ARM::t2IT) {
390 unsigned firstcond = MI.getOperand(0).getImm();
391 uint32_t mask = MI.getOperand(1).getImm();
392 unsigned zeros = CountTrailingZeros_32(mask);
393 mask >>= zeros+1;
394
395 for (unsigned i = 0; i < 4 - (zeros+1); ++i) {
396 if (firstcond ^ (mask & 1))
397 ITBlock.push_back(firstcond ^ 1);
398 else
399 ITBlock.push_back(firstcond);
400 mask >>= 1;
401 }
402 ITBlock.push_back(firstcond);
403 }
404
405 return true;
406 }
407
408 // We want to read exactly 4 bytes of data.
409 if (Region.readBytes(Address, 4, (uint8_t*)bytes, NULL) == -1)
410 return false;
411
412 uint32_t insn32 = (bytes[3] << 8) |
413 (bytes[2] << 0) |
414 (bytes[1] << 24) |
415 (bytes[0] << 16);
416 MI.clear();
417 result = decodeThumbInstruction32(MI, insn32, Address, this);
418 if (result) {
419 Size = 4;
420 bool InITBlock = ITBlock.size();
421 AddThumbPredicate(MI);
422 AddThumb1SBit(MI, InITBlock);
423 return true;
424 }
425
426 MI.clear();
427 result = decodeThumb2Instruction32(MI, insn32, Address, this);
428 if (result) {
429 Size = 4;
430 AddThumbPredicate(MI);
431 return true;
432 }
433
434 MI.clear();
435 result = decodeVFPInstruction32(MI, insn32, Address, this);
436 if (result) {
437 Size = 4;
438 UpdateThumbVFPPredicate(MI);
439 return true;
440 }
441
442 MI.clear();
443 result = decodeCommonInstruction32(MI, insn32, Address, this);
444 if (result) {
445 Size = 4;
446 AddThumbPredicate(MI);
447 return true;
448 }
449
450 return false;
451}
452
453
454extern "C" void LLVMInitializeARMDisassembler() {
455 TargetRegistry::RegisterMCDisassembler(TheARMTarget,
456 createARMDisassembler);
457 TargetRegistry::RegisterMCDisassembler(TheThumbTarget,
458 createThumbDisassembler);
459}
460
461static const unsigned GPRDecoderTable[] = {
462 ARM::R0, ARM::R1, ARM::R2, ARM::R3,
463 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
464 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
465 ARM::R12, ARM::SP, ARM::LR, ARM::PC
466};
467
468static bool DecodeGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
469 uint64_t Address, const void *Decoder) {
470 if (RegNo > 15)
471 return false;
472
473 unsigned Register = GPRDecoderTable[RegNo];
474 Inst.addOperand(MCOperand::CreateReg(Register));
475 return true;
476}
477
Owen Anderson51c98052011-08-09 22:48:45 +0000478static bool DecodeGPRnopcRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
479 uint64_t Address, const void *Decoder) {
480 if (RegNo == 15) return false;
481 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
482}
483
Owen Anderson8d7d2e12011-08-09 20:55:18 +0000484static bool DecodetGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
485 uint64_t Address, const void *Decoder) {
486 if (RegNo > 7)
487 return false;
488 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
489}
490
491static bool DecodetcGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
492 uint64_t Address, const void *Decoder) {
493 unsigned Register = 0;
494 switch (RegNo) {
495 case 0:
496 Register = ARM::R0;
497 break;
498 case 1:
499 Register = ARM::R1;
500 break;
501 case 2:
502 Register = ARM::R2;
503 break;
504 case 3:
505 Register = ARM::R3;
506 break;
507 case 9:
508 Register = ARM::R9;
509 break;
510 case 12:
511 Register = ARM::R12;
512 break;
513 default:
514 return false;
515 }
516
517 Inst.addOperand(MCOperand::CreateReg(Register));
518 return true;
519}
520
521static bool DecoderGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
522 uint64_t Address, const void *Decoder) {
523 if (RegNo == 13 || RegNo == 15) return false;
524 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
525}
526
527static const unsigned SPRDecoderTable[] = {
528 ARM::S0, ARM::S1, ARM::S2, ARM::S3,
529 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
530 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
531 ARM::S12, ARM::S13, ARM::S14, ARM::S15,
532 ARM::S16, ARM::S17, ARM::S18, ARM::S19,
533 ARM::S20, ARM::S21, ARM::S22, ARM::S23,
534 ARM::S24, ARM::S25, ARM::S26, ARM::S27,
535 ARM::S28, ARM::S29, ARM::S30, ARM::S31
536};
537
538static bool DecodeSPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
539 uint64_t Address, const void *Decoder) {
540 if (RegNo > 31)
541 return false;
542
543 unsigned Register = SPRDecoderTable[RegNo];
544 Inst.addOperand(MCOperand::CreateReg(Register));
545 return true;
546}
547
548static const unsigned DPRDecoderTable[] = {
549 ARM::D0, ARM::D1, ARM::D2, ARM::D3,
550 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
551 ARM::D8, ARM::D9, ARM::D10, ARM::D11,
552 ARM::D12, ARM::D13, ARM::D14, ARM::D15,
553 ARM::D16, ARM::D17, ARM::D18, ARM::D19,
554 ARM::D20, ARM::D21, ARM::D22, ARM::D23,
555 ARM::D24, ARM::D25, ARM::D26, ARM::D27,
556 ARM::D28, ARM::D29, ARM::D30, ARM::D31
557};
558
559static bool DecodeDPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
560 uint64_t Address, const void *Decoder) {
561 if (RegNo > 31)
562 return false;
563
564 unsigned Register = DPRDecoderTable[RegNo];
565 Inst.addOperand(MCOperand::CreateReg(Register));
566 return true;
567}
568
569static bool DecodeDPR_8RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
570 uint64_t Address, const void *Decoder) {
571 if (RegNo > 7)
572 return false;
573 return DecodeDPRRegisterClass(Inst, RegNo, Address, Decoder);
574}
575
576static bool DecodeDPR_VFP2RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
577 uint64_t Address, const void *Decoder) {
578 if (RegNo > 15)
579 return false;
580 return DecodeDPRRegisterClass(Inst, RegNo, Address, Decoder);
581}
582
583static const unsigned QPRDecoderTable[] = {
584 ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
585 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7,
586 ARM::Q8, ARM::Q9, ARM::Q10, ARM::Q11,
587 ARM::Q12, ARM::Q13, ARM::Q14, ARM::Q15
588};
589
590
591static bool DecodeQPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
592 uint64_t Address, const void *Decoder) {
593 if (RegNo > 31)
594 return false;
595 RegNo >>= 1;
596
597 unsigned Register = QPRDecoderTable[RegNo];
598 Inst.addOperand(MCOperand::CreateReg(Register));
599 return true;
600}
601
602static bool DecodePredicateOperand(llvm::MCInst &Inst, unsigned Val,
603 uint64_t Address, const void *Decoder) {
604 if (Val == 0xF) return false;
Owen Andersonbd9091c2011-08-09 21:07:45 +0000605 // AL predicate is not allowed on Thumb1 branches.
606 if (Inst.getOpcode() == ARM::tBcc && Val == 0xE)
607 return false;
Owen Anderson8d7d2e12011-08-09 20:55:18 +0000608 Inst.addOperand(MCOperand::CreateImm(Val));
609 if (Val == ARMCC::AL) {
610 Inst.addOperand(MCOperand::CreateReg(0));
611 } else
612 Inst.addOperand(MCOperand::CreateReg(ARM::CPSR));
613 return true;
614}
615
616static bool DecodeCCOutOperand(llvm::MCInst &Inst, unsigned Val,
617 uint64_t Address, const void *Decoder) {
618 if (Val)
619 Inst.addOperand(MCOperand::CreateReg(ARM::CPSR));
620 else
621 Inst.addOperand(MCOperand::CreateReg(0));
622 return true;
623}
624
625static bool DecodeSOImmOperand(llvm::MCInst &Inst, unsigned Val,
626 uint64_t Address, const void *Decoder) {
627 uint32_t imm = Val & 0xFF;
628 uint32_t rot = (Val & 0xF00) >> 7;
629 uint32_t rot_imm = (imm >> rot) | (imm << (32-rot));
630 Inst.addOperand(MCOperand::CreateImm(rot_imm));
631 return true;
632}
633
634static bool DecodeBLTargetOperand(llvm::MCInst &Inst, unsigned Val,
635 uint64_t Address, const void *Decoder) {
636 Val <<= 2;
637 Inst.addOperand(MCOperand::CreateImm(SignExtend32<26>(Val)));
638 return true;
639}
640
641static bool DecodeSORegImmOperand(llvm::MCInst &Inst, unsigned Val,
642 uint64_t Address, const void *Decoder) {
643
644 unsigned Rm = fieldFromInstruction32(Val, 0, 4);
645 unsigned type = fieldFromInstruction32(Val, 5, 2);
646 unsigned imm = fieldFromInstruction32(Val, 7, 5);
647
648 // Register-immediate
649 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
650
651 ARM_AM::ShiftOpc Shift = ARM_AM::lsl;
652 switch (type) {
653 case 0:
654 Shift = ARM_AM::lsl;
655 break;
656 case 1:
657 Shift = ARM_AM::lsr;
658 break;
659 case 2:
660 Shift = ARM_AM::asr;
661 break;
662 case 3:
663 Shift = ARM_AM::ror;
664 break;
665 }
666
667 if (Shift == ARM_AM::ror && imm == 0)
668 Shift = ARM_AM::rrx;
669
670 unsigned Op = Shift | (imm << 3);
671 Inst.addOperand(MCOperand::CreateImm(Op));
672
673 return true;
674}
675
676static bool DecodeSORegRegOperand(llvm::MCInst &Inst, unsigned Val,
677 uint64_t Address, const void *Decoder) {
678
679 unsigned Rm = fieldFromInstruction32(Val, 0, 4);
680 unsigned type = fieldFromInstruction32(Val, 5, 2);
681 unsigned Rs = fieldFromInstruction32(Val, 8, 4);
682
683 // Register-register
684 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
685 DecodeGPRRegisterClass(Inst, Rs, Address, Decoder);
686
687 ARM_AM::ShiftOpc Shift = ARM_AM::lsl;
688 switch (type) {
689 case 0:
690 Shift = ARM_AM::lsl;
691 break;
692 case 1:
693 Shift = ARM_AM::lsr;
694 break;
695 case 2:
696 Shift = ARM_AM::asr;
697 break;
698 case 3:
699 Shift = ARM_AM::ror;
700 break;
701 }
702
703 Inst.addOperand(MCOperand::CreateImm(Shift));
704
705 return true;
706}
707
708static bool DecodeRegListOperand(llvm::MCInst &Inst, unsigned Val,
709 uint64_t Address, const void *Decoder) {
710 for (unsigned i = 0; i < 16; ++i) {
711 if (Val & (1 << i))
712 DecodeGPRRegisterClass(Inst, i, Address, Decoder);
713 }
714
715 return true;
716}
717
718static bool DecodeSPRRegListOperand(llvm::MCInst &Inst, unsigned Val,
719 uint64_t Address, const void *Decoder) {
720 unsigned Vd = fieldFromInstruction32(Val, 8, 4);
721 unsigned regs = Val & 0xFF;
722
723 DecodeSPRRegisterClass(Inst, Vd, Address, Decoder);
724 for (unsigned i = 0; i < (regs - 1); ++i)
725 DecodeSPRRegisterClass(Inst, ++Vd, Address, Decoder);
726
727 return true;
728}
729
730static bool DecodeDPRRegListOperand(llvm::MCInst &Inst, unsigned Val,
731 uint64_t Address, const void *Decoder) {
732 unsigned Vd = fieldFromInstruction32(Val, 8, 4);
733 unsigned regs = (Val & 0xFF) / 2;
734
735 DecodeDPRRegisterClass(Inst, Vd, Address, Decoder);
736 for (unsigned i = 0; i < (regs - 1); ++i)
737 DecodeDPRRegisterClass(Inst, ++Vd, Address, Decoder);
738
739 return true;
740}
741
742static bool DecodeBitfieldMaskOperand(llvm::MCInst &Inst, unsigned Val,
743 uint64_t Address, const void *Decoder) {
744 unsigned msb = fieldFromInstruction32(Val, 5, 5);
745 unsigned lsb = fieldFromInstruction32(Val, 0, 5);
746 uint32_t msb_mask = (1 << (msb+1)) - 1;
747 uint32_t lsb_mask = (1 << lsb) - 1;
748 Inst.addOperand(MCOperand::CreateImm(~(msb_mask ^ lsb_mask)));
749 return true;
750}
751
752static bool DecodeCopMemInstruction(llvm::MCInst &Inst, unsigned Insn,
753 uint64_t Address, const void *Decoder) {
754 unsigned pred = fieldFromInstruction32(Insn, 28, 4);
755 unsigned CRd = fieldFromInstruction32(Insn, 12, 4);
756 unsigned coproc = fieldFromInstruction32(Insn, 8, 4);
757 unsigned imm = fieldFromInstruction32(Insn, 0, 8);
758 unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
759 unsigned U = fieldFromInstruction32(Insn, 23, 1);
760
761 switch (Inst.getOpcode()) {
762 case ARM::LDC_OFFSET:
763 case ARM::LDC_PRE:
764 case ARM::LDC_POST:
765 case ARM::LDC_OPTION:
766 case ARM::LDCL_OFFSET:
767 case ARM::LDCL_PRE:
768 case ARM::LDCL_POST:
769 case ARM::LDCL_OPTION:
770 case ARM::STC_OFFSET:
771 case ARM::STC_PRE:
772 case ARM::STC_POST:
773 case ARM::STC_OPTION:
774 case ARM::STCL_OFFSET:
775 case ARM::STCL_PRE:
776 case ARM::STCL_POST:
777 case ARM::STCL_OPTION:
778 if (coproc == 0xA || coproc == 0xB)
779 return false;
780 break;
781 default:
782 break;
783 }
784
785 Inst.addOperand(MCOperand::CreateImm(coproc));
786 Inst.addOperand(MCOperand::CreateImm(CRd));
787 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
788 switch (Inst.getOpcode()) {
789 case ARM::LDC_OPTION:
790 case ARM::LDCL_OPTION:
791 case ARM::LDC2_OPTION:
792 case ARM::LDC2L_OPTION:
793 case ARM::STC_OPTION:
794 case ARM::STCL_OPTION:
795 case ARM::STC2_OPTION:
796 case ARM::STC2L_OPTION:
797 case ARM::LDCL_POST:
798 case ARM::STCL_POST:
799 break;
800 default:
801 Inst.addOperand(MCOperand::CreateReg(0));
802 break;
803 }
804
805 unsigned P = fieldFromInstruction32(Insn, 24, 1);
806 unsigned W = fieldFromInstruction32(Insn, 21, 1);
807
808 bool writeback = (P == 0) || (W == 1);
809 unsigned idx_mode = 0;
810 if (P && writeback)
811 idx_mode = ARMII::IndexModePre;
812 else if (!P && writeback)
813 idx_mode = ARMII::IndexModePost;
814
815 switch (Inst.getOpcode()) {
816 case ARM::LDCL_POST:
817 case ARM::STCL_POST:
818 imm |= U << 8;
819 case ARM::LDC_OPTION:
820 case ARM::LDCL_OPTION:
821 case ARM::LDC2_OPTION:
822 case ARM::LDC2L_OPTION:
823 case ARM::STC_OPTION:
824 case ARM::STCL_OPTION:
825 case ARM::STC2_OPTION:
826 case ARM::STC2L_OPTION:
827 Inst.addOperand(MCOperand::CreateImm(imm));
828 break;
829 default:
830 if (U)
831 Inst.addOperand(MCOperand::CreateImm(
832 ARM_AM::getAM2Opc(ARM_AM::add, imm, ARM_AM::lsl, idx_mode)));
833 else
834 Inst.addOperand(MCOperand::CreateImm(
835 ARM_AM::getAM2Opc(ARM_AM::sub, imm, ARM_AM::lsl, idx_mode)));
836 break;
837 }
838
839 switch (Inst.getOpcode()) {
840 case ARM::LDC_OFFSET:
841 case ARM::LDC_PRE:
842 case ARM::LDC_POST:
843 case ARM::LDC_OPTION:
844 case ARM::LDCL_OFFSET:
845 case ARM::LDCL_PRE:
846 case ARM::LDCL_POST:
847 case ARM::LDCL_OPTION:
848 case ARM::STC_OFFSET:
849 case ARM::STC_PRE:
850 case ARM::STC_POST:
851 case ARM::STC_OPTION:
852 case ARM::STCL_OFFSET:
853 case ARM::STCL_PRE:
854 case ARM::STCL_POST:
855 case ARM::STCL_OPTION:
856 if (!DecodePredicateOperand(Inst, pred, Address, Decoder)) return false;
857 break;
858 default:
859 break;
860 }
861
862 return true;
863}
864
865static bool DecodeAddrMode2IdxInstruction(llvm::MCInst &Inst, unsigned Insn,
866 uint64_t Address, const void *Decoder) {
867 unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
868 unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
869 unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
870 unsigned imm = fieldFromInstruction32(Insn, 0, 12);
871 unsigned pred = fieldFromInstruction32(Insn, 28, 4);
872 unsigned reg = fieldFromInstruction32(Insn, 25, 1);
873 unsigned P = fieldFromInstruction32(Insn, 24, 1);
874 unsigned W = fieldFromInstruction32(Insn, 21, 1);
875
876 // On stores, the writeback operand precedes Rt.
877 switch (Inst.getOpcode()) {
878 case ARM::STR_POST_IMM:
879 case ARM::STR_POST_REG:
880 case ARM::STRTr:
881 case ARM::STRTi:
882 case ARM::STRBTr:
883 case ARM::STRBTi:
884 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
885 break;
886 default:
887 break;
888 }
889
890 DecodeGPRRegisterClass(Inst, Rt, Address, Decoder);
891
892 // On loads, the writeback operand comes after Rt.
893 switch (Inst.getOpcode()) {
894 case ARM::LDR_POST_IMM:
895 case ARM::LDR_POST_REG:
896 case ARM::LDR_PRE:
897 case ARM::LDRBT_POST_REG:
898 case ARM::LDRBT_POST_IMM:
899 case ARM::LDRTr:
900 case ARM::LDRTi:
901 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
902 break;
903 default:
904 break;
905 }
906
907 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
908
909 ARM_AM::AddrOpc Op = ARM_AM::add;
910 if (!fieldFromInstruction32(Insn, 23, 1))
911 Op = ARM_AM::sub;
912
913 bool writeback = (P == 0) || (W == 1);
914 unsigned idx_mode = 0;
915 if (P && writeback)
916 idx_mode = ARMII::IndexModePre;
917 else if (!P && writeback)
918 idx_mode = ARMII::IndexModePost;
919
920 if (reg) {
921 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
922 ARM_AM::ShiftOpc Opc = ARM_AM::lsl;
923 switch( fieldFromInstruction32(Insn, 5, 2)) {
924 case 0:
925 Opc = ARM_AM::lsl;
926 break;
927 case 1:
928 Opc = ARM_AM::lsr;
929 break;
930 case 2:
931 Opc = ARM_AM::asr;
932 break;
933 case 3:
934 Opc = ARM_AM::ror;
935 break;
936 default:
937 return false;
938 }
939 unsigned amt = fieldFromInstruction32(Insn, 7, 5);
940 unsigned imm = ARM_AM::getAM2Opc(Op, amt, Opc, idx_mode);
941
942 Inst.addOperand(MCOperand::CreateImm(imm));
943 } else {
944 Inst.addOperand(MCOperand::CreateReg(0));
945 unsigned tmp = ARM_AM::getAM2Opc(Op, imm, ARM_AM::lsl, idx_mode);
946 Inst.addOperand(MCOperand::CreateImm(tmp));
947 }
948
949 if (!DecodePredicateOperand(Inst, pred, Address, Decoder)) return false;
950
951 return true;
952}
953
954static bool DecodeSORegMemOperand(llvm::MCInst &Inst, unsigned Val,
955 uint64_t Address, const void *Decoder) {
956 unsigned Rn = fieldFromInstruction32(Val, 13, 4);
957 unsigned Rm = fieldFromInstruction32(Val, 0, 4);
958 unsigned type = fieldFromInstruction32(Val, 5, 2);
959 unsigned imm = fieldFromInstruction32(Val, 7, 5);
960 unsigned U = fieldFromInstruction32(Val, 12, 1);
961
Owen Anderson51157d22011-08-09 21:38:14 +0000962 ARM_AM::ShiftOpc ShOp = ARM_AM::lsl;
Owen Anderson8d7d2e12011-08-09 20:55:18 +0000963 switch (type) {
964 case 0:
965 ShOp = ARM_AM::lsl;
966 break;
967 case 1:
968 ShOp = ARM_AM::lsr;
969 break;
970 case 2:
971 ShOp = ARM_AM::asr;
972 break;
973 case 3:
974 ShOp = ARM_AM::ror;
975 break;
976 }
977
978 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
979 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
980 unsigned shift;
981 if (U)
982 shift = ARM_AM::getAM2Opc(ARM_AM::add, imm, ShOp);
983 else
984 shift = ARM_AM::getAM2Opc(ARM_AM::sub, imm, ShOp);
985 Inst.addOperand(MCOperand::CreateImm(shift));
986
987 return true;
988}
989
990static bool DecodeAddrMode3Instruction(llvm::MCInst &Inst, unsigned Insn,
991 uint64_t Address, const void *Decoder) {
992 unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
993 unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
994 unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
995 unsigned type = fieldFromInstruction32(Insn, 22, 1);
996 unsigned imm = fieldFromInstruction32(Insn, 8, 4);
997 unsigned U = ((~fieldFromInstruction32(Insn, 23, 1)) & 1) << 8;
998 unsigned pred = fieldFromInstruction32(Insn, 28, 4);
999 unsigned W = fieldFromInstruction32(Insn, 21, 1);
1000 unsigned P = fieldFromInstruction32(Insn, 24, 1);
1001
1002 bool writeback = (W == 1) | (P == 0);
1003 if (writeback) { // Writeback
1004 if (P)
1005 U |= ARMII::IndexModePre << 9;
1006 else
1007 U |= ARMII::IndexModePost << 9;
1008
1009 // On stores, the writeback operand precedes Rt.
1010 switch (Inst.getOpcode()) {
1011 case ARM::STRD:
1012 case ARM::STRD_PRE:
1013 case ARM::STRD_POST:
1014 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1015 break;
1016 default:
1017 break;
1018 }
1019 }
1020
1021 DecodeGPRRegisterClass(Inst, Rt, Address, Decoder);
1022 switch (Inst.getOpcode()) {
1023 case ARM::STRD:
1024 case ARM::STRD_PRE:
1025 case ARM::STRD_POST:
1026 case ARM::LDRD:
1027 case ARM::LDRD_PRE:
1028 case ARM::LDRD_POST:
1029 DecodeGPRRegisterClass(Inst, Rt+1, Address, Decoder);
1030 break;
1031 default:
1032 break;
1033 }
1034
1035 if (writeback) {
1036 // On loads, the writeback operand comes after Rt.
1037 switch (Inst.getOpcode()) {
1038 case ARM::LDRD:
1039 case ARM::LDRD_PRE:
1040 case ARM::LDRD_POST:
1041 case ARM::LDRHTr:
1042 case ARM::LDRSBTr:
1043 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1044 break;
1045 default:
1046 break;
1047 }
1048 }
1049
1050 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1051
1052 if (type) {
1053 Inst.addOperand(MCOperand::CreateReg(0));
1054 Inst.addOperand(MCOperand::CreateImm(U | (imm << 4) | Rm));
1055 } else {
1056 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1057 Inst.addOperand(MCOperand::CreateImm(U));
1058 }
1059
1060 if (!DecodePredicateOperand(Inst, pred, Address, Decoder)) return false;
1061
1062 return true;
1063}
1064
1065static bool DecodeRFEInstruction(llvm::MCInst &Inst, unsigned Insn,
1066 uint64_t Address, const void *Decoder) {
1067 unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1068 unsigned mode = fieldFromInstruction32(Insn, 23, 2);
1069
1070 switch (mode) {
1071 case 0:
1072 mode = ARM_AM::da;
1073 break;
1074 case 1:
1075 mode = ARM_AM::ia;
1076 break;
1077 case 2:
1078 mode = ARM_AM::db;
1079 break;
1080 case 3:
1081 mode = ARM_AM::ib;
1082 break;
1083 }
1084
1085 Inst.addOperand(MCOperand::CreateImm(mode));
1086 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1087
1088 return true;
1089}
1090
1091static bool DecodeMemMultipleWritebackInstruction(llvm::MCInst &Inst,
1092 unsigned Insn,
1093 uint64_t Address, const void *Decoder) {
1094 unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1095 unsigned pred = fieldFromInstruction32(Insn, 28, 4);
1096 unsigned reglist = fieldFromInstruction32(Insn, 0, 16);
1097
1098 if (pred == 0xF) {
1099 switch (Inst.getOpcode()) {
1100 case ARM::STMDA:
1101 Inst.setOpcode(ARM::RFEDA);
1102 break;
1103 case ARM::STMDA_UPD:
1104 Inst.setOpcode(ARM::RFEDA_UPD);
1105 break;
1106 case ARM::STMDB:
1107 Inst.setOpcode(ARM::RFEDB);
1108 break;
1109 case ARM::STMDB_UPD:
1110 Inst.setOpcode(ARM::RFEDB_UPD);
1111 break;
1112 case ARM::STMIA:
1113 Inst.setOpcode(ARM::RFEIA);
1114 break;
1115 case ARM::STMIA_UPD:
1116 Inst.setOpcode(ARM::RFEIA_UPD);
1117 break;
1118 case ARM::STMIB:
1119 Inst.setOpcode(ARM::RFEIB);
1120 break;
1121 case ARM::STMIB_UPD:
1122 Inst.setOpcode(ARM::RFEIB_UPD);
1123 break;
1124
1125 }
1126 return DecodeRFEInstruction(Inst, Insn, Address, Decoder);
1127 }
1128
1129 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1130 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder); // Tied
1131 if (!DecodePredicateOperand(Inst, pred, Address, Decoder)) return false;
1132 DecodeRegListOperand(Inst, reglist, Address, Decoder);
1133
1134 return true;
1135}
1136
1137static bool DecodeCPSInstruction(llvm::MCInst &Inst, unsigned Insn,
1138 uint64_t Address, const void *Decoder) {
1139 unsigned imod = fieldFromInstruction32(Insn, 18, 2);
1140 unsigned M = fieldFromInstruction32(Insn, 17, 1);
1141 unsigned iflags = fieldFromInstruction32(Insn, 6, 3);
1142 unsigned mode = fieldFromInstruction32(Insn, 0, 5);
1143
Owen Anderson35008c22011-08-09 23:05:39 +00001144 // imod == '01' --> UNPREDICTABLE
1145 if (imod == 1) return false;
1146
Owen Anderson8d7d2e12011-08-09 20:55:18 +00001147 if (M && mode && imod && iflags) {
1148 Inst.setOpcode(ARM::CPS3p);
1149 Inst.addOperand(MCOperand::CreateImm(imod));
1150 Inst.addOperand(MCOperand::CreateImm(iflags));
1151 Inst.addOperand(MCOperand::CreateImm(mode));
1152 return true;
1153 } else if (!mode && !M) {
1154 Inst.setOpcode(ARM::CPS2p);
1155 Inst.addOperand(MCOperand::CreateImm(imod));
1156 Inst.addOperand(MCOperand::CreateImm(iflags));
1157 return true;
1158 } else if (!imod && !iflags && M) {
1159 Inst.setOpcode(ARM::CPS1p);
1160 Inst.addOperand(MCOperand::CreateImm(mode));
1161 return true;
1162 }
1163
1164 return false;
1165}
1166
1167static bool DecodeSMLAInstruction(llvm::MCInst &Inst, unsigned Insn,
1168 uint64_t Address, const void *Decoder) {
1169 unsigned Rd = fieldFromInstruction32(Insn, 16, 4);
1170 unsigned Rn = fieldFromInstruction32(Insn, 0, 4);
1171 unsigned Rm = fieldFromInstruction32(Insn, 8, 4);
1172 unsigned Ra = fieldFromInstruction32(Insn, 12, 4);
1173 unsigned pred = fieldFromInstruction32(Insn, 28, 4);
1174
1175 if (pred == 0xF)
1176 return DecodeCPSInstruction(Inst, Insn, Address, Decoder);
1177
1178 DecodeGPRRegisterClass(Inst, Rd, Address, Decoder);
1179 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1180 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1181 DecodeGPRRegisterClass(Inst, Ra, Address, Decoder);
1182
1183 return true;
1184}
1185
1186static bool DecodeAddrModeImm12Operand(llvm::MCInst &Inst, unsigned Val,
1187 uint64_t Address, const void *Decoder) {
1188 unsigned add = fieldFromInstruction32(Val, 12, 1);
1189 unsigned imm = fieldFromInstruction32(Val, 0, 12);
1190 unsigned Rn = fieldFromInstruction32(Val, 13, 4);
1191
1192 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1193
1194 if (!add) imm *= -1;
1195 if (imm == 0 && !add) imm = INT32_MIN;
1196 Inst.addOperand(MCOperand::CreateImm(imm));
1197
1198 return true;
1199}
1200
1201static bool DecodeAddrMode5Operand(llvm::MCInst &Inst, unsigned Val,
1202 uint64_t Address, const void *Decoder) {
1203 unsigned Rn = fieldFromInstruction32(Val, 9, 4);
1204 unsigned U = fieldFromInstruction32(Val, 8, 1);
1205 unsigned imm = fieldFromInstruction32(Val, 0, 8);
1206
1207 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1208
1209 if (U)
1210 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add, imm)));
1211 else
1212 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub, imm)));
1213
1214 return true;
1215}
1216
1217static bool DecodeAddrMode7Operand(llvm::MCInst &Inst, unsigned Val,
1218 uint64_t Address, const void *Decoder) {
1219 return DecodeGPRRegisterClass(Inst, Val, Address, Decoder);
1220}
1221
1222static bool DecodeBranchImmInstruction(llvm::MCInst &Inst, unsigned Insn,
1223 uint64_t Address, const void *Decoder) {
1224 unsigned pred = fieldFromInstruction32(Insn, 28, 4);
1225 unsigned imm = fieldFromInstruction32(Insn, 0, 24) << 2;
1226
1227 if (pred == 0xF) {
1228 Inst.setOpcode(ARM::BLXi);
1229 imm |= fieldFromInstruction32(Insn, 24, 1) << 1;
Benjamin Kramer793b8112011-08-09 22:02:50 +00001230 Inst.addOperand(MCOperand::CreateImm(SignExtend32<26>(imm)));
Owen Anderson8d7d2e12011-08-09 20:55:18 +00001231 return true;
1232 }
1233
Benjamin Kramer793b8112011-08-09 22:02:50 +00001234 Inst.addOperand(MCOperand::CreateImm(SignExtend32<26>(imm)));
Owen Anderson8d7d2e12011-08-09 20:55:18 +00001235 if (!DecodePredicateOperand(Inst, pred, Address, Decoder)) return false;
1236
1237 return true;
1238}
1239
1240
1241static bool DecodeVCVTImmOperand(llvm::MCInst &Inst, unsigned Val,
1242 uint64_t Address, const void *Decoder) {
1243 Inst.addOperand(MCOperand::CreateImm(64 - Val));
1244 return true;
1245}
1246
1247static bool DecodeAddrMode6Operand(llvm::MCInst &Inst, unsigned Val,
1248 uint64_t Address, const void *Decoder) {
1249 unsigned Rm = fieldFromInstruction32(Val, 0, 4);
1250 unsigned align = fieldFromInstruction32(Val, 4, 2);
1251
1252 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1253 if (!align)
1254 Inst.addOperand(MCOperand::CreateImm(0));
1255 else
1256 Inst.addOperand(MCOperand::CreateImm(4 << align));
1257
1258 return true;
1259}
1260
1261static bool DecodeVLDInstruction(llvm::MCInst &Inst, unsigned Insn,
1262 uint64_t Address, const void *Decoder) {
1263 unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1264 Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1265 unsigned wb = fieldFromInstruction32(Insn, 16, 4);
1266 unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1267 Rn |= fieldFromInstruction32(Insn, 4, 2) << 4;
1268 unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1269
1270 // First output register
1271 DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1272
1273 // Second output register
1274 switch (Inst.getOpcode()) {
1275 case ARM::VLD1q8:
1276 case ARM::VLD1q16:
1277 case ARM::VLD1q32:
1278 case ARM::VLD1q64:
1279 case ARM::VLD1q8_UPD:
1280 case ARM::VLD1q16_UPD:
1281 case ARM::VLD1q32_UPD:
1282 case ARM::VLD1q64_UPD:
1283 case ARM::VLD1d8T:
1284 case ARM::VLD1d16T:
1285 case ARM::VLD1d32T:
1286 case ARM::VLD1d64T:
1287 case ARM::VLD1d8T_UPD:
1288 case ARM::VLD1d16T_UPD:
1289 case ARM::VLD1d32T_UPD:
1290 case ARM::VLD1d64T_UPD:
1291 case ARM::VLD1d8Q:
1292 case ARM::VLD1d16Q:
1293 case ARM::VLD1d32Q:
1294 case ARM::VLD1d64Q:
1295 case ARM::VLD1d8Q_UPD:
1296 case ARM::VLD1d16Q_UPD:
1297 case ARM::VLD1d32Q_UPD:
1298 case ARM::VLD1d64Q_UPD:
1299 case ARM::VLD2d8:
1300 case ARM::VLD2d16:
1301 case ARM::VLD2d32:
1302 case ARM::VLD2d8_UPD:
1303 case ARM::VLD2d16_UPD:
1304 case ARM::VLD2d32_UPD:
1305 case ARM::VLD2q8:
1306 case ARM::VLD2q16:
1307 case ARM::VLD2q32:
1308 case ARM::VLD2q8_UPD:
1309 case ARM::VLD2q16_UPD:
1310 case ARM::VLD2q32_UPD:
1311 case ARM::VLD3d8:
1312 case ARM::VLD3d16:
1313 case ARM::VLD3d32:
1314 case ARM::VLD3d8_UPD:
1315 case ARM::VLD3d16_UPD:
1316 case ARM::VLD3d32_UPD:
1317 case ARM::VLD4d8:
1318 case ARM::VLD4d16:
1319 case ARM::VLD4d32:
1320 case ARM::VLD4d8_UPD:
1321 case ARM::VLD4d16_UPD:
1322 case ARM::VLD4d32_UPD:
1323 DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder);
1324 break;
1325 case ARM::VLD2b8:
1326 case ARM::VLD2b16:
1327 case ARM::VLD2b32:
1328 case ARM::VLD2b8_UPD:
1329 case ARM::VLD2b16_UPD:
1330 case ARM::VLD2b32_UPD:
1331 case ARM::VLD3q8:
1332 case ARM::VLD3q16:
1333 case ARM::VLD3q32:
1334 case ARM::VLD3q8_UPD:
1335 case ARM::VLD3q16_UPD:
1336 case ARM::VLD3q32_UPD:
1337 case ARM::VLD4q8:
1338 case ARM::VLD4q16:
1339 case ARM::VLD4q32:
1340 case ARM::VLD4q8_UPD:
1341 case ARM::VLD4q16_UPD:
1342 case ARM::VLD4q32_UPD:
1343 DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder);
1344 default:
1345 break;
1346 }
1347
1348 // Third output register
1349 switch(Inst.getOpcode()) {
1350 case ARM::VLD1d8T:
1351 case ARM::VLD1d16T:
1352 case ARM::VLD1d32T:
1353 case ARM::VLD1d64T:
1354 case ARM::VLD1d8T_UPD:
1355 case ARM::VLD1d16T_UPD:
1356 case ARM::VLD1d32T_UPD:
1357 case ARM::VLD1d64T_UPD:
1358 case ARM::VLD1d8Q:
1359 case ARM::VLD1d16Q:
1360 case ARM::VLD1d32Q:
1361 case ARM::VLD1d64Q:
1362 case ARM::VLD1d8Q_UPD:
1363 case ARM::VLD1d16Q_UPD:
1364 case ARM::VLD1d32Q_UPD:
1365 case ARM::VLD1d64Q_UPD:
1366 case ARM::VLD2q8:
1367 case ARM::VLD2q16:
1368 case ARM::VLD2q32:
1369 case ARM::VLD2q8_UPD:
1370 case ARM::VLD2q16_UPD:
1371 case ARM::VLD2q32_UPD:
1372 case ARM::VLD3d8:
1373 case ARM::VLD3d16:
1374 case ARM::VLD3d32:
1375 case ARM::VLD3d8_UPD:
1376 case ARM::VLD3d16_UPD:
1377 case ARM::VLD3d32_UPD:
1378 case ARM::VLD4d8:
1379 case ARM::VLD4d16:
1380 case ARM::VLD4d32:
1381 case ARM::VLD4d8_UPD:
1382 case ARM::VLD4d16_UPD:
1383 case ARM::VLD4d32_UPD:
1384 DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder);
1385 break;
1386 case ARM::VLD3q8:
1387 case ARM::VLD3q16:
1388 case ARM::VLD3q32:
1389 case ARM::VLD3q8_UPD:
1390 case ARM::VLD3q16_UPD:
1391 case ARM::VLD3q32_UPD:
1392 case ARM::VLD4q8:
1393 case ARM::VLD4q16:
1394 case ARM::VLD4q32:
1395 case ARM::VLD4q8_UPD:
1396 case ARM::VLD4q16_UPD:
1397 case ARM::VLD4q32_UPD:
1398 DecodeDPRRegisterClass(Inst, (Rd+4)%32, Address, Decoder);
1399 break;
1400 default:
1401 break;
1402 }
1403
1404 // Fourth output register
1405 switch (Inst.getOpcode()) {
1406 case ARM::VLD1d8Q:
1407 case ARM::VLD1d16Q:
1408 case ARM::VLD1d32Q:
1409 case ARM::VLD1d64Q:
1410 case ARM::VLD1d8Q_UPD:
1411 case ARM::VLD1d16Q_UPD:
1412 case ARM::VLD1d32Q_UPD:
1413 case ARM::VLD1d64Q_UPD:
1414 case ARM::VLD2q8:
1415 case ARM::VLD2q16:
1416 case ARM::VLD2q32:
1417 case ARM::VLD2q8_UPD:
1418 case ARM::VLD2q16_UPD:
1419 case ARM::VLD2q32_UPD:
1420 case ARM::VLD4d8:
1421 case ARM::VLD4d16:
1422 case ARM::VLD4d32:
1423 case ARM::VLD4d8_UPD:
1424 case ARM::VLD4d16_UPD:
1425 case ARM::VLD4d32_UPD:
1426 DecodeDPRRegisterClass(Inst, (Rd+3)%32, Address, Decoder);
1427 break;
1428 case ARM::VLD4q8:
1429 case ARM::VLD4q16:
1430 case ARM::VLD4q32:
1431 case ARM::VLD4q8_UPD:
1432 case ARM::VLD4q16_UPD:
1433 case ARM::VLD4q32_UPD:
1434 DecodeDPRRegisterClass(Inst, (Rd+6)%32, Address, Decoder);
1435 break;
1436 default:
1437 break;
1438 }
1439
1440 // Writeback operand
1441 switch (Inst.getOpcode()) {
1442 case ARM::VLD1d8_UPD:
1443 case ARM::VLD1d16_UPD:
1444 case ARM::VLD1d32_UPD:
1445 case ARM::VLD1d64_UPD:
1446 case ARM::VLD1q8_UPD:
1447 case ARM::VLD1q16_UPD:
1448 case ARM::VLD1q32_UPD:
1449 case ARM::VLD1q64_UPD:
1450 case ARM::VLD1d8T_UPD:
1451 case ARM::VLD1d16T_UPD:
1452 case ARM::VLD1d32T_UPD:
1453 case ARM::VLD1d64T_UPD:
1454 case ARM::VLD1d8Q_UPD:
1455 case ARM::VLD1d16Q_UPD:
1456 case ARM::VLD1d32Q_UPD:
1457 case ARM::VLD1d64Q_UPD:
1458 case ARM::VLD2d8_UPD:
1459 case ARM::VLD2d16_UPD:
1460 case ARM::VLD2d32_UPD:
1461 case ARM::VLD2q8_UPD:
1462 case ARM::VLD2q16_UPD:
1463 case ARM::VLD2q32_UPD:
1464 case ARM::VLD2b8_UPD:
1465 case ARM::VLD2b16_UPD:
1466 case ARM::VLD2b32_UPD:
1467 case ARM::VLD3d8_UPD:
1468 case ARM::VLD3d16_UPD:
1469 case ARM::VLD3d32_UPD:
1470 case ARM::VLD3q8_UPD:
1471 case ARM::VLD3q16_UPD:
1472 case ARM::VLD3q32_UPD:
1473 case ARM::VLD4d8_UPD:
1474 case ARM::VLD4d16_UPD:
1475 case ARM::VLD4d32_UPD:
1476 case ARM::VLD4q8_UPD:
1477 case ARM::VLD4q16_UPD:
1478 case ARM::VLD4q32_UPD:
1479 DecodeGPRRegisterClass(Inst, wb, Address, Decoder);
1480 break;
1481 default:
1482 break;
1483 }
1484
1485 // AddrMode6 Base (register+alignment)
1486 DecodeAddrMode6Operand(Inst, Rn, Address, Decoder);
1487
1488 // AddrMode6 Offset (register)
1489 if (Rm == 0xD)
1490 Inst.addOperand(MCOperand::CreateReg(0));
1491 else if (Rm != 0xF)
1492 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1493
1494 return true;
1495}
1496
1497static bool DecodeVSTInstruction(llvm::MCInst &Inst, unsigned Insn,
1498 uint64_t Address, const void *Decoder) {
1499 unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1500 Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1501 unsigned wb = fieldFromInstruction32(Insn, 16, 4);
1502 unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1503 Rn |= fieldFromInstruction32(Insn, 4, 2) << 4;
1504 unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1505
1506 // Writeback Operand
1507 switch (Inst.getOpcode()) {
1508 case ARM::VST1d8_UPD:
1509 case ARM::VST1d16_UPD:
1510 case ARM::VST1d32_UPD:
1511 case ARM::VST1d64_UPD:
1512 case ARM::VST1q8_UPD:
1513 case ARM::VST1q16_UPD:
1514 case ARM::VST1q32_UPD:
1515 case ARM::VST1q64_UPD:
1516 case ARM::VST1d8T_UPD:
1517 case ARM::VST1d16T_UPD:
1518 case ARM::VST1d32T_UPD:
1519 case ARM::VST1d64T_UPD:
1520 case ARM::VST1d8Q_UPD:
1521 case ARM::VST1d16Q_UPD:
1522 case ARM::VST1d32Q_UPD:
1523 case ARM::VST1d64Q_UPD:
1524 case ARM::VST2d8_UPD:
1525 case ARM::VST2d16_UPD:
1526 case ARM::VST2d32_UPD:
1527 case ARM::VST2q8_UPD:
1528 case ARM::VST2q16_UPD:
1529 case ARM::VST2q32_UPD:
1530 case ARM::VST2b8_UPD:
1531 case ARM::VST2b16_UPD:
1532 case ARM::VST2b32_UPD:
1533 case ARM::VST3d8_UPD:
1534 case ARM::VST3d16_UPD:
1535 case ARM::VST3d32_UPD:
1536 case ARM::VST3q8_UPD:
1537 case ARM::VST3q16_UPD:
1538 case ARM::VST3q32_UPD:
1539 case ARM::VST4d8_UPD:
1540 case ARM::VST4d16_UPD:
1541 case ARM::VST4d32_UPD:
1542 case ARM::VST4q8_UPD:
1543 case ARM::VST4q16_UPD:
1544 case ARM::VST4q32_UPD:
1545 DecodeGPRRegisterClass(Inst, wb, Address, Decoder);
1546 break;
1547 default:
1548 break;
1549 }
1550
1551 // AddrMode6 Base (register+alignment)
1552 DecodeAddrMode6Operand(Inst, Rn, Address, Decoder);
1553
1554 // AddrMode6 Offset (register)
1555 if (Rm == 0xD)
1556 Inst.addOperand(MCOperand::CreateReg(0));
1557 else if (Rm != 0xF)
1558 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1559
1560 // First input register
1561 DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1562
1563 // Second input register
1564 switch (Inst.getOpcode()) {
1565 case ARM::VST1q8:
1566 case ARM::VST1q16:
1567 case ARM::VST1q32:
1568 case ARM::VST1q64:
1569 case ARM::VST1q8_UPD:
1570 case ARM::VST1q16_UPD:
1571 case ARM::VST1q32_UPD:
1572 case ARM::VST1q64_UPD:
1573 case ARM::VST1d8T:
1574 case ARM::VST1d16T:
1575 case ARM::VST1d32T:
1576 case ARM::VST1d64T:
1577 case ARM::VST1d8T_UPD:
1578 case ARM::VST1d16T_UPD:
1579 case ARM::VST1d32T_UPD:
1580 case ARM::VST1d64T_UPD:
1581 case ARM::VST1d8Q:
1582 case ARM::VST1d16Q:
1583 case ARM::VST1d32Q:
1584 case ARM::VST1d64Q:
1585 case ARM::VST1d8Q_UPD:
1586 case ARM::VST1d16Q_UPD:
1587 case ARM::VST1d32Q_UPD:
1588 case ARM::VST1d64Q_UPD:
1589 case ARM::VST2d8:
1590 case ARM::VST2d16:
1591 case ARM::VST2d32:
1592 case ARM::VST2d8_UPD:
1593 case ARM::VST2d16_UPD:
1594 case ARM::VST2d32_UPD:
1595 case ARM::VST2q8:
1596 case ARM::VST2q16:
1597 case ARM::VST2q32:
1598 case ARM::VST2q8_UPD:
1599 case ARM::VST2q16_UPD:
1600 case ARM::VST2q32_UPD:
1601 case ARM::VST3d8:
1602 case ARM::VST3d16:
1603 case ARM::VST3d32:
1604 case ARM::VST3d8_UPD:
1605 case ARM::VST3d16_UPD:
1606 case ARM::VST3d32_UPD:
1607 case ARM::VST4d8:
1608 case ARM::VST4d16:
1609 case ARM::VST4d32:
1610 case ARM::VST4d8_UPD:
1611 case ARM::VST4d16_UPD:
1612 case ARM::VST4d32_UPD:
1613 DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder);
1614 break;
1615 case ARM::VST2b8:
1616 case ARM::VST2b16:
1617 case ARM::VST2b32:
1618 case ARM::VST2b8_UPD:
1619 case ARM::VST2b16_UPD:
1620 case ARM::VST2b32_UPD:
1621 case ARM::VST3q8:
1622 case ARM::VST3q16:
1623 case ARM::VST3q32:
1624 case ARM::VST3q8_UPD:
1625 case ARM::VST3q16_UPD:
1626 case ARM::VST3q32_UPD:
1627 case ARM::VST4q8:
1628 case ARM::VST4q16:
1629 case ARM::VST4q32:
1630 case ARM::VST4q8_UPD:
1631 case ARM::VST4q16_UPD:
1632 case ARM::VST4q32_UPD:
1633 DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder);
1634 break;
1635 default:
1636 break;
1637 }
1638
1639 // Third input register
1640 switch (Inst.getOpcode()) {
1641 case ARM::VST1d8T:
1642 case ARM::VST1d16T:
1643 case ARM::VST1d32T:
1644 case ARM::VST1d64T:
1645 case ARM::VST1d8T_UPD:
1646 case ARM::VST1d16T_UPD:
1647 case ARM::VST1d32T_UPD:
1648 case ARM::VST1d64T_UPD:
1649 case ARM::VST1d8Q:
1650 case ARM::VST1d16Q:
1651 case ARM::VST1d32Q:
1652 case ARM::VST1d64Q:
1653 case ARM::VST1d8Q_UPD:
1654 case ARM::VST1d16Q_UPD:
1655 case ARM::VST1d32Q_UPD:
1656 case ARM::VST1d64Q_UPD:
1657 case ARM::VST2q8:
1658 case ARM::VST2q16:
1659 case ARM::VST2q32:
1660 case ARM::VST2q8_UPD:
1661 case ARM::VST2q16_UPD:
1662 case ARM::VST2q32_UPD:
1663 case ARM::VST3d8:
1664 case ARM::VST3d16:
1665 case ARM::VST3d32:
1666 case ARM::VST3d8_UPD:
1667 case ARM::VST3d16_UPD:
1668 case ARM::VST3d32_UPD:
1669 case ARM::VST4d8:
1670 case ARM::VST4d16:
1671 case ARM::VST4d32:
1672 case ARM::VST4d8_UPD:
1673 case ARM::VST4d16_UPD:
1674 case ARM::VST4d32_UPD:
1675 DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder);
1676 break;
1677 case ARM::VST3q8:
1678 case ARM::VST3q16:
1679 case ARM::VST3q32:
1680 case ARM::VST3q8_UPD:
1681 case ARM::VST3q16_UPD:
1682 case ARM::VST3q32_UPD:
1683 case ARM::VST4q8:
1684 case ARM::VST4q16:
1685 case ARM::VST4q32:
1686 case ARM::VST4q8_UPD:
1687 case ARM::VST4q16_UPD:
1688 case ARM::VST4q32_UPD:
1689 DecodeDPRRegisterClass(Inst, (Rd+4)%32, Address, Decoder);
1690 break;
1691 default:
1692 break;
1693 }
1694
1695 // Fourth input register
1696 switch (Inst.getOpcode()) {
1697 case ARM::VST1d8Q:
1698 case ARM::VST1d16Q:
1699 case ARM::VST1d32Q:
1700 case ARM::VST1d64Q:
1701 case ARM::VST1d8Q_UPD:
1702 case ARM::VST1d16Q_UPD:
1703 case ARM::VST1d32Q_UPD:
1704 case ARM::VST1d64Q_UPD:
1705 case ARM::VST2q8:
1706 case ARM::VST2q16:
1707 case ARM::VST2q32:
1708 case ARM::VST2q8_UPD:
1709 case ARM::VST2q16_UPD:
1710 case ARM::VST2q32_UPD:
1711 case ARM::VST4d8:
1712 case ARM::VST4d16:
1713 case ARM::VST4d32:
1714 case ARM::VST4d8_UPD:
1715 case ARM::VST4d16_UPD:
1716 case ARM::VST4d32_UPD:
1717 DecodeDPRRegisterClass(Inst, (Rd+3)%32, Address, Decoder);
1718 break;
1719 case ARM::VST4q8:
1720 case ARM::VST4q16:
1721 case ARM::VST4q32:
1722 case ARM::VST4q8_UPD:
1723 case ARM::VST4q16_UPD:
1724 case ARM::VST4q32_UPD:
1725 DecodeDPRRegisterClass(Inst, (Rd+6)%32, Address, Decoder);
1726 break;
1727 default:
1728 break;
1729 }
1730
1731 return true;
1732}
1733
1734static bool DecodeVLD1DupInstruction(llvm::MCInst &Inst, unsigned Insn,
1735 uint64_t Address, const void *Decoder) {
1736 unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1737 Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1738 unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1739 unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1740 unsigned align = fieldFromInstruction32(Insn, 4, 1);
1741 unsigned size = fieldFromInstruction32(Insn, 6, 2);
1742 unsigned regs = fieldFromInstruction32(Insn, 5, 1) + 1;
1743
1744 align *= (1 << size);
1745
1746 DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1747 if (regs == 2) DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder);
1748 if (Rm == 0xD) DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1749
1750 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1751 Inst.addOperand(MCOperand::CreateImm(align));
1752
1753 if (Rm == 0xD)
1754 Inst.addOperand(MCOperand::CreateReg(0));
1755 else if (Rm != 0xF)
1756 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1757
1758 return true;
1759}
1760
1761static bool DecodeVLD2DupInstruction(llvm::MCInst &Inst, unsigned Insn,
1762 uint64_t Address, const void *Decoder) {
1763 unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1764 Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1765 unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1766 unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1767 unsigned align = fieldFromInstruction32(Insn, 4, 1);
1768 unsigned size = 1 << fieldFromInstruction32(Insn, 6, 2);
1769 unsigned inc = fieldFromInstruction32(Insn, 5, 1) + 1;
1770 align *= 2*size;
1771
1772 DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1773 DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder);
1774 if (Rm == 0xD) DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1775
1776 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1777 Inst.addOperand(MCOperand::CreateImm(align));
1778
1779 if (Rm == 0xD)
1780 Inst.addOperand(MCOperand::CreateReg(0));
1781 else if (Rm != 0xF)
1782 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1783
1784 return true;
1785}
1786
1787static bool DecodeVLD3DupInstruction(llvm::MCInst &Inst, unsigned Insn,
1788 uint64_t Address, const void *Decoder) {
1789 unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1790 Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1791 unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1792 unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1793 unsigned inc = fieldFromInstruction32(Insn, 5, 1) + 1;
1794
1795 DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1796 DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder);
1797 DecodeDPRRegisterClass(Inst, (Rd+2*inc)%32, Address, Decoder);
1798 if (Rm == 0xD) DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1799
1800 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1801 Inst.addOperand(MCOperand::CreateImm(0));
1802
1803 if (Rm == 0xD)
1804 Inst.addOperand(MCOperand::CreateReg(0));
1805 else if (Rm != 0xF)
1806 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1807
1808 return true;
1809}
1810
1811static bool DecodeVLD4DupInstruction(llvm::MCInst &Inst, unsigned Insn,
1812 uint64_t Address, const void *Decoder) {
1813 unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1814 Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1815 unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1816 unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1817 unsigned size = fieldFromInstruction32(Insn, 6, 2);
1818 unsigned inc = fieldFromInstruction32(Insn, 5, 1) + 1;
1819 unsigned align = fieldFromInstruction32(Insn, 4, 1);
1820
1821 if (size == 0x3) {
1822 size = 4;
1823 align = 16;
1824 } else {
1825 if (size == 2) {
1826 size = 1 << size;
1827 align *= 8;
1828 } else {
1829 size = 1 << size;
1830 align *= 4*size;
1831 }
1832 }
1833
1834 DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1835 DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder);
1836 DecodeDPRRegisterClass(Inst, (Rd+2*inc)%32, Address, Decoder);
1837 DecodeDPRRegisterClass(Inst, (Rd+3*inc)%32, Address, Decoder);
1838 if (Rm == 0xD) DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1839
1840 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1841 Inst.addOperand(MCOperand::CreateImm(align));
1842
1843 if (Rm == 0xD)
1844 Inst.addOperand(MCOperand::CreateReg(0));
1845 else if (Rm != 0xF)
1846 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1847
1848 return true;
1849}
1850
1851static bool DecodeNEONModImmInstruction(llvm::MCInst &Inst, unsigned Insn,
1852 uint64_t Address, const void *Decoder) {
1853 unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1854 Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1855 unsigned imm = fieldFromInstruction32(Insn, 0, 4);
1856 imm |= fieldFromInstruction32(Insn, 16, 3) << 4;
1857 imm |= fieldFromInstruction32(Insn, 24, 1) << 7;
1858 imm |= fieldFromInstruction32(Insn, 8, 4) << 8;
1859 imm |= fieldFromInstruction32(Insn, 5, 1) << 12;
1860 unsigned Q = fieldFromInstruction32(Insn, 6, 1);
1861
1862 if (Q)
1863 DecodeQPRRegisterClass(Inst, Rd, Address, Decoder);
1864 else
1865 DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1866
1867 Inst.addOperand(MCOperand::CreateImm(imm));
1868
1869 switch (Inst.getOpcode()) {
1870 case ARM::VORRiv4i16:
1871 case ARM::VORRiv2i32:
1872 case ARM::VBICiv4i16:
1873 case ARM::VBICiv2i32:
1874 DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1875 break;
1876 case ARM::VORRiv8i16:
1877 case ARM::VORRiv4i32:
1878 case ARM::VBICiv8i16:
1879 case ARM::VBICiv4i32:
1880 DecodeQPRRegisterClass(Inst, Rd, Address, Decoder);
1881 break;
1882 default:
1883 break;
1884 }
1885
1886
1887 return true;
1888}
1889
1890static bool DecodeVSHLMaxInstruction(llvm::MCInst &Inst, unsigned Insn,
1891 uint64_t Address, const void *Decoder) {
1892 unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1893 Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1894 unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1895 Rm |= fieldFromInstruction32(Insn, 5, 1) << 4;
1896 unsigned size = fieldFromInstruction32(Insn, 18, 2);
1897
1898 DecodeQPRRegisterClass(Inst, Rd, Address, Decoder);
1899 DecodeDPRRegisterClass(Inst, Rm, Address, Decoder);
1900 Inst.addOperand(MCOperand::CreateImm(8 << size));
1901
1902 return true;
1903}
1904
1905static bool DecodeShiftRight8Imm(llvm::MCInst &Inst, unsigned Val,
1906 uint64_t Address, const void *Decoder) {
1907 Inst.addOperand(MCOperand::CreateImm(8 - Val));
1908 return true;
1909}
1910
1911static bool DecodeShiftRight16Imm(llvm::MCInst &Inst, unsigned Val,
1912 uint64_t Address, const void *Decoder) {
1913 Inst.addOperand(MCOperand::CreateImm(16 - Val));
1914 return true;
1915}
1916
1917static bool DecodeShiftRight32Imm(llvm::MCInst &Inst, unsigned Val,
1918 uint64_t Address, const void *Decoder) {
1919 Inst.addOperand(MCOperand::CreateImm(32 - Val));
1920 return true;
1921}
1922
1923static bool DecodeShiftRight64Imm(llvm::MCInst &Inst, unsigned Val,
1924 uint64_t Address, const void *Decoder) {
1925 Inst.addOperand(MCOperand::CreateImm(64 - Val));
1926 return true;
1927}
1928
1929static bool DecodeTBLInstruction(llvm::MCInst &Inst, unsigned Insn,
1930 uint64_t Address, const void *Decoder) {
1931 unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1932 Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1933 unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1934 Rn |= fieldFromInstruction32(Insn, 7, 1) << 4;
1935 unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1936 Rm |= fieldFromInstruction32(Insn, 5, 1) << 4;
1937 unsigned op = fieldFromInstruction32(Insn, 6, 1);
1938 unsigned length = fieldFromInstruction32(Insn, 8, 2) + 1;
1939
1940 DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1941 if (op) DecodeDPRRegisterClass(Inst, Rd, Address, Decoder); // Writeback
1942
1943 for (unsigned i = 0; i < length; ++i)
1944 DecodeDPRRegisterClass(Inst, (Rn+i)%32, Address, Decoder);
1945
1946 DecodeDPRRegisterClass(Inst, Rm, Address, Decoder);
1947
1948 return true;
1949}
1950
1951static bool DecodeVFPfpImm(llvm::MCInst &Inst, unsigned Val,
1952 uint64_t Address, const void *Decoder) {
1953 // The immediate needs to be a fully instantiated float. However, the
1954 // auto-generated decoder is only able to fill in some of the bits
1955 // necessary. For instance, the 'b' bit is replicated multiple times,
1956 // and is even present in inverted form in one bit. We do a little
1957 // binary parsing here to fill in those missing bits, and then
1958 // reinterpret it all as a float.
1959 union {
1960 uint32_t integer;
1961 float fp;
1962 } fp_conv;
1963
1964 fp_conv.integer = Val;
1965 uint32_t b = fieldFromInstruction32(Val, 25, 1);
1966 fp_conv.integer |= b << 26;
1967 fp_conv.integer |= b << 27;
1968 fp_conv.integer |= b << 28;
1969 fp_conv.integer |= b << 29;
1970 fp_conv.integer |= (~b & 0x1) << 30;
1971
1972 Inst.addOperand(MCOperand::CreateFPImm(fp_conv.fp));
1973 return true;
1974}
1975
1976static bool DecodeThumbAddSpecialReg(llvm::MCInst &Inst, uint16_t Insn,
1977 uint64_t Address, const void *Decoder) {
1978 unsigned dst = fieldFromInstruction16(Insn, 8, 3);
1979 unsigned imm = fieldFromInstruction16(Insn, 0, 8);
1980
1981 DecodetGPRRegisterClass(Inst, dst, Address, Decoder);
1982
1983 if (Inst.getOpcode() == ARM::tADR)
1984 Inst.addOperand(MCOperand::CreateReg(ARM::PC));
1985 else if (Inst.getOpcode() == ARM::tADDrSPi)
1986 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
1987 else
1988 return false;
1989
1990 Inst.addOperand(MCOperand::CreateImm(imm));
1991 return true;
1992}
1993
1994static bool DecodeThumbBROperand(llvm::MCInst &Inst, unsigned Val,
1995 uint64_t Address, const void *Decoder) {
1996 Inst.addOperand(MCOperand::CreateImm(SignExtend32<12>(Val << 1)));
1997 return true;
1998}
1999
2000static bool DecodeT2BROperand(llvm::MCInst &Inst, unsigned Val,
2001 uint64_t Address, const void *Decoder) {
2002 Inst.addOperand(MCOperand::CreateImm(SignExtend32<21>(Val)));
2003 return true;
2004}
2005
2006static bool DecodeThumbCmpBROperand(llvm::MCInst &Inst, unsigned Val,
2007 uint64_t Address, const void *Decoder) {
2008 Inst.addOperand(MCOperand::CreateImm(SignExtend32<7>(Val << 1)));
2009 return true;
2010}
2011
2012static bool DecodeThumbAddrModeRR(llvm::MCInst &Inst, unsigned Val,
2013 uint64_t Address, const void *Decoder) {
2014 unsigned Rn = fieldFromInstruction32(Val, 0, 3);
2015 unsigned Rm = fieldFromInstruction32(Val, 3, 3);
2016
2017 DecodetGPRRegisterClass(Inst, Rn, Address, Decoder);
2018 DecodetGPRRegisterClass(Inst, Rm, Address, Decoder);
2019
2020 return true;
2021}
2022
2023static bool DecodeThumbAddrModeIS(llvm::MCInst &Inst, unsigned Val,
2024 uint64_t Address, const void *Decoder) {
2025 unsigned Rn = fieldFromInstruction32(Val, 0, 3);
2026 unsigned imm = fieldFromInstruction32(Val, 3, 5);
2027
2028 DecodetGPRRegisterClass(Inst, Rn, Address, Decoder);
2029 Inst.addOperand(MCOperand::CreateImm(imm));
2030
2031 return true;
2032}
2033
2034static bool DecodeThumbAddrModePC(llvm::MCInst &Inst, unsigned Val,
2035 uint64_t Address, const void *Decoder) {
2036 Inst.addOperand(MCOperand::CreateImm(Val << 2));
2037
2038 return true;
2039}
2040
2041static bool DecodeThumbAddrModeSP(llvm::MCInst &Inst, unsigned Val,
2042 uint64_t Address, const void *Decoder) {
2043 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2044 Inst.addOperand(MCOperand::CreateImm(Val << 2));
2045
2046 return true;
2047}
2048
2049static bool DecodeT2AddrModeSOReg(llvm::MCInst &Inst, unsigned Val,
2050 uint64_t Address, const void *Decoder) {
2051 unsigned Rn = fieldFromInstruction32(Val, 6, 4);
2052 unsigned Rm = fieldFromInstruction32(Val, 2, 4);
2053 unsigned imm = fieldFromInstruction32(Val, 0, 2);
2054
2055 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
2056 DecoderGPRRegisterClass(Inst, Rm, Address, Decoder);
2057 Inst.addOperand(MCOperand::CreateImm(imm));
2058
2059 return true;
2060}
2061
2062static bool DecodeT2LoadShift(llvm::MCInst &Inst, unsigned Insn,
2063 uint64_t Address, const void *Decoder) {
2064 if (Inst.getOpcode() != ARM::t2PLDs) {
2065 unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
2066 DecodeGPRRegisterClass(Inst, Rt, Address, Decoder);
2067 }
2068
2069 unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2070 if (Rn == 0xF) {
2071 switch (Inst.getOpcode()) {
2072 case ARM::t2LDRBs:
2073 Inst.setOpcode(ARM::t2LDRBpci);
2074 break;
2075 case ARM::t2LDRHs:
2076 Inst.setOpcode(ARM::t2LDRHpci);
2077 break;
2078 case ARM::t2LDRSHs:
2079 Inst.setOpcode(ARM::t2LDRSHpci);
2080 break;
2081 case ARM::t2LDRSBs:
2082 Inst.setOpcode(ARM::t2LDRSBpci);
2083 break;
2084 case ARM::t2PLDs:
2085 Inst.setOpcode(ARM::t2PLDi12);
2086 Inst.addOperand(MCOperand::CreateReg(ARM::PC));
2087 break;
2088 default:
2089 return false;
2090 }
2091
2092 int imm = fieldFromInstruction32(Insn, 0, 12);
2093 if (!fieldFromInstruction32(Insn, 23, 1)) imm *= -1;
2094 Inst.addOperand(MCOperand::CreateImm(imm));
2095
2096 return true;
2097 }
2098
2099 unsigned addrmode = fieldFromInstruction32(Insn, 4, 2);
2100 addrmode |= fieldFromInstruction32(Insn, 0, 4) << 2;
2101 addrmode |= fieldFromInstruction32(Insn, 16, 4) << 6;
2102 DecodeT2AddrModeSOReg(Inst, addrmode, Address, Decoder);
2103
2104 return true;
2105}
2106
2107static bool DecodeT2Imm8S4(llvm::MCInst &Inst, unsigned Val,
2108 uint64_t Address, const void *Decoder) {
2109 int imm = Val & 0xFF;
2110 if (!(Val & 0x100)) imm *= -1;
2111 Inst.addOperand(MCOperand::CreateImm(imm << 2));
2112
2113 return true;
2114}
2115
2116static bool DecodeT2AddrModeImm8s4(llvm::MCInst &Inst, unsigned Val,
2117 uint64_t Address, const void *Decoder) {
2118 unsigned Rn = fieldFromInstruction32(Val, 9, 4);
2119 unsigned imm = fieldFromInstruction32(Val, 0, 9);
2120
2121 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
2122 DecodeT2Imm8S4(Inst, imm, Address, Decoder);
2123
2124 return true;
2125}
2126
2127static bool DecodeT2Imm8(llvm::MCInst &Inst, unsigned Val,
2128 uint64_t Address, const void *Decoder) {
2129 int imm = Val & 0xFF;
2130 if (!(Val & 0x100)) imm *= -1;
2131 Inst.addOperand(MCOperand::CreateImm(imm));
2132
2133 return true;
2134}
2135
2136
2137static bool DecodeT2AddrModeImm8(llvm::MCInst &Inst, unsigned Val,
2138 uint64_t Address, const void *Decoder) {
2139 unsigned Rn = fieldFromInstruction32(Val, 9, 4);
2140 unsigned imm = fieldFromInstruction32(Val, 0, 9);
2141
2142 // Some instructions always use an additive offset.
2143 switch (Inst.getOpcode()) {
2144 case ARM::t2LDRT:
2145 case ARM::t2LDRBT:
2146 case ARM::t2LDRHT:
2147 case ARM::t2LDRSBT:
2148 case ARM::t2LDRSHT:
2149 imm |= 0x100;
2150 break;
2151 default:
2152 break;
2153 }
2154
2155 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
2156 DecodeT2Imm8(Inst, imm, Address, Decoder);
2157
2158 return true;
2159}
2160
2161
2162static bool DecodeT2AddrModeImm12(llvm::MCInst &Inst, unsigned Val,
2163 uint64_t Address, const void *Decoder) {
2164 unsigned Rn = fieldFromInstruction32(Val, 13, 4);
2165 unsigned imm = fieldFromInstruction32(Val, 0, 12);
2166
2167 DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
2168 Inst.addOperand(MCOperand::CreateImm(imm));
2169
2170 return true;
2171}
2172
2173
2174static bool DecodeThumbAddSPImm(llvm::MCInst &Inst, uint16_t Insn,
2175 uint64_t Address, const void *Decoder) {
2176 unsigned imm = fieldFromInstruction16(Insn, 0, 7);
2177
2178 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2179 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2180 Inst.addOperand(MCOperand::CreateImm(imm));
2181
2182 return true;
2183}
2184
2185static bool DecodeThumbAddSPReg(llvm::MCInst &Inst, uint16_t Insn,
2186 uint64_t Address, const void *Decoder) {
2187 if (Inst.getOpcode() == ARM::tADDrSP) {
2188 unsigned Rdm = fieldFromInstruction16(Insn, 0, 3);
2189 Rdm |= fieldFromInstruction16(Insn, 7, 1) << 3;
2190
2191 DecodeGPRRegisterClass(Inst, Rdm, Address, Decoder);
2192 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2193 DecodeGPRRegisterClass(Inst, Rdm, Address, Decoder);
2194 } else if (Inst.getOpcode() == ARM::tADDspr) {
2195 unsigned Rm = fieldFromInstruction16(Insn, 3, 4);
2196
2197 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2198 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2199 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
2200 }
2201
2202 return true;
2203}
2204
2205static bool DecodeThumbCPS(llvm::MCInst &Inst, uint16_t Insn,
2206 uint64_t Address, const void *Decoder) {
2207 unsigned imod = fieldFromInstruction16(Insn, 4, 1) | 0x2;
2208 unsigned flags = fieldFromInstruction16(Insn, 0, 3);
2209
2210 Inst.addOperand(MCOperand::CreateImm(imod));
2211 Inst.addOperand(MCOperand::CreateImm(flags));
2212
2213 return true;
2214}
2215
2216static bool DecodePostIdxReg(llvm::MCInst &Inst, unsigned Insn,
2217 uint64_t Address, const void *Decoder) {
2218 unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
2219 unsigned add = fieldFromInstruction32(Insn, 4, 1);
2220
2221 DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
2222 Inst.addOperand(MCOperand::CreateImm(add));
2223
2224 return true;
2225}
2226
2227static bool DecodeThumbBLXOffset(llvm::MCInst &Inst, unsigned Val,
2228 uint64_t Address, const void *Decoder) {
2229 Inst.addOperand(MCOperand::CreateImm(SignExtend32<22>(Val << 1)));
2230 return true;
2231}
2232
2233static bool DecodeCoprocessor(llvm::MCInst &Inst, unsigned Val,
2234 uint64_t Address, const void *Decoder) {
2235 if (Val == 0xA || Val == 0xB)
2236 return false;
2237
2238 Inst.addOperand(MCOperand::CreateImm(Val));
2239 return true;
2240}
2241
2242static bool DecodeThumbSRImm(llvm::MCInst &Inst, unsigned Val,
2243 uint64_t Address, const void *Decoder) {
2244 if (Val == 0)
2245 Inst.addOperand(MCOperand::CreateImm(32));
2246 else
2247 Inst.addOperand(MCOperand::CreateImm(Val));
2248 return true;
2249}
2250
2251static bool DecodeThumb2BCCInstruction(llvm::MCInst &Inst, unsigned Insn,
2252 uint64_t Address, const void *Decoder) {
2253 unsigned pred = fieldFromInstruction32(Insn, 22, 4);
2254 if (pred == 0xE || pred == 0xF) {
2255 unsigned opc = fieldFromInstruction32(Insn, 4, 2);
2256 switch (opc) {
2257 default:
2258 return false;
2259 case 0:
2260 Inst.setOpcode(ARM::t2DSB);
2261 break;
2262 case 1:
2263 Inst.setOpcode(ARM::t2DMB);
2264 break;
2265 case 2:
2266 Inst.setOpcode(ARM::t2ISB);
2267 return true;
2268 }
2269
2270 unsigned imm = fieldFromInstruction32(Insn, 0, 4);
2271 Inst.addOperand(MCOperand::CreateImm(imm));
2272 return true;
2273 }
2274
2275 unsigned brtarget = fieldFromInstruction32(Insn, 0, 11) << 1;
2276 brtarget |= fieldFromInstruction32(Insn, 11, 1) << 19;
2277 brtarget |= fieldFromInstruction32(Insn, 13, 1) << 18;
2278 brtarget |= fieldFromInstruction32(Insn, 16, 6) << 12;
2279 brtarget |= fieldFromInstruction32(Insn, 26, 1) << 20;
2280
2281 DecodeT2BROperand(Inst, brtarget, Address, Decoder);
2282 if (!DecodePredicateOperand(Inst, pred, Address, Decoder))
2283 return false;
2284
2285 return true;
2286}
2287
2288// Decode a shifted immediate operand. These basically consist
2289// of an 8-bit value, and a 4-bit directive that specifies either
2290// a splat operation or a rotation.
2291static bool DecodeT2SOImm(llvm::MCInst &Inst, unsigned Val,
2292 uint64_t Address, const void *Decoder) {
2293 unsigned ctrl = fieldFromInstruction32(Val, 10, 2);
2294 if (ctrl == 0) {
2295 unsigned byte = fieldFromInstruction32(Val, 8, 2);
2296 unsigned imm = fieldFromInstruction32(Val, 0, 8);
2297 switch (byte) {
2298 case 0:
2299 Inst.addOperand(MCOperand::CreateImm(imm));
2300 break;
2301 case 1:
2302 Inst.addOperand(MCOperand::CreateImm((imm << 16) | imm));
2303 break;
2304 case 2:
2305 Inst.addOperand(MCOperand::CreateImm((imm << 24) | (imm << 8)));
2306 break;
2307 case 3:
2308 Inst.addOperand(MCOperand::CreateImm((imm << 24) | (imm << 16) |
2309 (imm << 8) | imm));
2310 break;
2311 }
2312 } else {
2313 unsigned unrot = fieldFromInstruction32(Val, 0, 7) | 0x80;
2314 unsigned rot = fieldFromInstruction32(Val, 7, 5);
2315 unsigned imm = (unrot >> rot) | (unrot << ((32-rot)&31));
2316 Inst.addOperand(MCOperand::CreateImm(imm));
2317 }
2318
2319 return true;
2320}
2321
2322static bool DecodeThumbBCCTargetOperand(llvm::MCInst &Inst, unsigned Val,
2323 uint64_t Address, const void *Decoder){
2324 Inst.addOperand(MCOperand::CreateImm(Val << 1));
2325 return true;
2326}
2327
2328static bool DecodeThumbBLTargetOperand(llvm::MCInst &Inst, unsigned Val,
2329 uint64_t Address, const void *Decoder){
2330 Inst.addOperand(MCOperand::CreateImm(SignExtend32<22>(Val << 1)));
2331 return true;
2332}
2333
2334static bool DecodeAddrMode3Offset(llvm::MCInst &Inst, unsigned Val,
2335 uint64_t Address, const void *Decoder) {
2336 bool isImm = fieldFromInstruction32(Val, 9, 1);
2337 bool isAdd = fieldFromInstruction32(Val, 8, 1);
2338 unsigned imm = fieldFromInstruction32(Val, 0, 8);
2339
2340 if (!isImm) {
2341 DecodeGPRRegisterClass(Inst, imm, Address, Decoder);
2342 Inst.addOperand(MCOperand::CreateImm(!isAdd << 8));
2343 } else {
2344 Inst.addOperand(MCOperand::CreateReg(0));
2345 Inst.addOperand(MCOperand::CreateImm(imm | (!isAdd << 8)));
2346 }
2347
2348 return true;
2349}