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