blob: bb0fc9b371e343efa2d69d6f99e04a9b8d092e02 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMDisassembler.cpp - Disassembler for ARM/Thumb ISA --------------===//
Johnny Chen7b999ea2010-04-02 22:27:38 +00002//
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 Chen7b999ea2010-04-02 22:27:38 +00009
10#define DEBUG_TYPE "arm-disassembler"
11
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/MC/MCDisassembler.h"
Owen Andersone0152a72011-08-09 20:55:18 +000013#include "MCTargetDesc/ARMAddressingModes.h"
14#include "MCTargetDesc/ARMBaseInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/ARMMCExpr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCFixedLenDisassembler.h"
Johnny Chen7b999ea2010-04-02 22:27:38 +000019#include "llvm/MC/MCInst.h"
Benjamin Kramer48b5bbf2011-11-11 12:39:41 +000020#include "llvm/MC/MCInstrDesc.h"
Dylan Noblesmith7a3973d2012-04-03 15:48:14 +000021#include "llvm/MC/MCSubtargetInfo.h"
Johnny Chen7b999ea2010-04-02 22:27:38 +000022#include "llvm/Support/Debug.h"
Johnny Chen7b999ea2010-04-02 22:27:38 +000023#include "llvm/Support/ErrorHandling.h"
Jim Grosbachecaef492012-08-14 19:06:05 +000024#include "llvm/Support/LEB128.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/MemoryObject.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000026#include "llvm/Support/TargetRegistry.h"
Johnny Chen7b999ea2010-04-02 22:27:38 +000027#include "llvm/Support/raw_ostream.h"
Richard Bartone9600002012-04-24 11:13:20 +000028#include <vector>
Johnny Chen7b999ea2010-04-02 22:27:38 +000029
James Molloydb4ce602011-09-01 18:02:14 +000030using namespace llvm;
Owen Andersona4043c42011-08-17 17:44:15 +000031
Owen Anderson03aadae2011-09-01 23:23:50 +000032typedef MCDisassembler::DecodeStatus DecodeStatus;
33
Owen Andersoned96b582011-09-01 23:35:51 +000034namespace {
Richard Bartone9600002012-04-24 11:13:20 +000035 // Handles the condition code status of instructions in IT blocks
36 class ITStatus
37 {
38 public:
39 // Returns the condition code for instruction in IT block
40 unsigned getITCC() {
41 unsigned CC = ARMCC::AL;
42 if (instrInITBlock())
43 CC = ITStates.back();
44 return CC;
45 }
46
47 // Advances the IT block state to the next T or E
48 void advanceITState() {
49 ITStates.pop_back();
50 }
51
52 // Returns true if the current instruction is in an IT block
53 bool instrInITBlock() {
54 return !ITStates.empty();
55 }
56
57 // Returns true if current instruction is the last instruction in an IT block
58 bool instrLastInITBlock() {
59 return ITStates.size() == 1;
60 }
61
62 // Called when decoding an IT instruction. Sets the IT state for the following
63 // instructions that for the IT block. Firstcond and Mask correspond to the
64 // fields in the IT instruction encoding.
65 void setITState(char Firstcond, char Mask) {
66 // (3 - the number of trailing zeros) is the number of then / else.
Richard Bartonf435b092012-04-27 08:42:59 +000067 unsigned CondBit0 = Firstcond & 1;
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000068 unsigned NumTZ = countTrailingZeros<uint8_t>(Mask);
Richard Bartone9600002012-04-24 11:13:20 +000069 unsigned char CCBits = static_cast<unsigned char>(Firstcond & 0xf);
70 assert(NumTZ <= 3 && "Invalid IT mask!");
71 // push condition codes onto the stack the correct order for the pops
72 for (unsigned Pos = NumTZ+1; Pos <= 3; ++Pos) {
73 bool T = ((Mask >> Pos) & 1) == CondBit0;
74 if (T)
75 ITStates.push_back(CCBits);
76 else
77 ITStates.push_back(CCBits ^ 1);
78 }
79 ITStates.push_back(CCBits);
80 }
81
82 private:
83 std::vector<unsigned char> ITStates;
84 };
85}
86
87namespace {
Owen Andersoned96b582011-09-01 23:35:51 +000088/// ARMDisassembler - ARM disassembler for all ARM platforms.
89class ARMDisassembler : public MCDisassembler {
90public:
91 /// Constructor - Initializes the disassembler.
92 ///
James Molloy4c493e82011-09-07 17:24:38 +000093 ARMDisassembler(const MCSubtargetInfo &STI) :
94 MCDisassembler(STI) {
Owen Andersoned96b582011-09-01 23:35:51 +000095 }
96
97 ~ARMDisassembler() {
98 }
99
100 /// getInstruction - See MCDisassembler.
101 DecodeStatus getInstruction(MCInst &instr,
102 uint64_t &size,
Derek Schuff56b662c2012-02-29 01:09:06 +0000103 const MemoryObject &region,
Owen Andersoned96b582011-09-01 23:35:51 +0000104 uint64_t address,
Owen Andersona0c3b972011-09-15 23:38:46 +0000105 raw_ostream &vStream,
106 raw_ostream &cStream) const;
Owen Andersoned96b582011-09-01 23:35:51 +0000107};
108
109/// ThumbDisassembler - Thumb disassembler for all Thumb platforms.
110class ThumbDisassembler : public MCDisassembler {
111public:
112 /// Constructor - Initializes the disassembler.
113 ///
James Molloy4c493e82011-09-07 17:24:38 +0000114 ThumbDisassembler(const MCSubtargetInfo &STI) :
115 MCDisassembler(STI) {
Owen Andersoned96b582011-09-01 23:35:51 +0000116 }
117
118 ~ThumbDisassembler() {
119 }
120
121 /// getInstruction - See MCDisassembler.
122 DecodeStatus getInstruction(MCInst &instr,
123 uint64_t &size,
Derek Schuff56b662c2012-02-29 01:09:06 +0000124 const MemoryObject &region,
Owen Andersoned96b582011-09-01 23:35:51 +0000125 uint64_t address,
Owen Andersona0c3b972011-09-15 23:38:46 +0000126 raw_ostream &vStream,
127 raw_ostream &cStream) const;
Owen Andersoned96b582011-09-01 23:35:51 +0000128
Owen Andersoned96b582011-09-01 23:35:51 +0000129private:
Richard Bartone9600002012-04-24 11:13:20 +0000130 mutable ITStatus ITBlock;
Owen Anderson2fefa422011-09-08 22:42:49 +0000131 DecodeStatus AddThumbPredicate(MCInst&) const;
Owen Andersoned96b582011-09-01 23:35:51 +0000132 void UpdateThumbVFPPredicate(MCInst&) const;
133};
134}
135
Owen Anderson03aadae2011-09-01 23:23:50 +0000136static bool Check(DecodeStatus &Out, DecodeStatus In) {
James Molloydb4ce602011-09-01 18:02:14 +0000137 switch (In) {
138 case MCDisassembler::Success:
139 // Out stays the same.
140 return true;
141 case MCDisassembler::SoftFail:
142 Out = In;
143 return true;
144 case MCDisassembler::Fail:
145 Out = In;
146 return false;
147 }
David Blaikie46a9f012012-01-20 21:51:11 +0000148 llvm_unreachable("Invalid DecodeStatus!");
James Molloydb4ce602011-09-01 18:02:14 +0000149}
Owen Andersona4043c42011-08-17 17:44:15 +0000150
James Molloy8067df92011-09-07 19:42:28 +0000151
Owen Andersone0152a72011-08-09 20:55:18 +0000152// Forward declare these because the autogenerated code will reference them.
153// Definitions are further down.
Craig Topperf6e7e122012-03-27 07:21:54 +0000154static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000155 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000156static DecodeStatus DecodeGPRnopcRegisterClass(MCInst &Inst,
Jim Grosbachd14b70d2011-08-17 21:58:18 +0000157 unsigned RegNo, uint64_t Address,
158 const void *Decoder);
Mihai Popadc1764c52013-05-13 14:10:04 +0000159static DecodeStatus DecodeGPRwithAPSRRegisterClass(MCInst &Inst,
160 unsigned RegNo, uint64_t Address,
161 const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000162static DecodeStatus DecodetGPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000163 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000164static DecodeStatus DecodetcGPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000165 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000166static DecodeStatus DecoderGPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000167 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000168static DecodeStatus DecodeSPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000169 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000170static DecodeStatus DecodeDPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000171 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000172static DecodeStatus DecodeDPR_8RegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000173 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000174static DecodeStatus DecodeDPR_VFP2RegisterClass(MCInst &Inst,
Jim Grosbachd14b70d2011-08-17 21:58:18 +0000175 unsigned RegNo,
176 uint64_t Address,
177 const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000178static DecodeStatus DecodeQPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000179 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000180static DecodeStatus DecodeDPairRegisterClass(MCInst &Inst, unsigned RegNo,
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000181 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000182static DecodeStatus DecodeDPairSpacedRegisterClass(MCInst &Inst,
Jim Grosbache5307f92012-03-05 21:43:40 +0000183 unsigned RegNo, uint64_t Address,
184 const void *Decoder);
Johnny Chen74491bb2010-08-12 01:40:54 +0000185
Craig Topperf6e7e122012-03-27 07:21:54 +0000186static DecodeStatus DecodePredicateOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000187 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000188static DecodeStatus DecodeCCOutOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000189 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000190static DecodeStatus DecodeSOImmOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000191 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000192static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000193 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000194static DecodeStatus DecodeSPRRegListOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000195 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000196static DecodeStatus DecodeDPRRegListOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000197 uint64_t Address, const void *Decoder);
Johnny Chen7b999ea2010-04-02 22:27:38 +0000198
Craig Topperf6e7e122012-03-27 07:21:54 +0000199static DecodeStatus DecodeBitfieldMaskOperand(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000200 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000201static DecodeStatus DecodeCopMemInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000202 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000203static DecodeStatus DecodeAddrMode2IdxInstruction(MCInst &Inst,
Jim Grosbachd14b70d2011-08-17 21:58:18 +0000204 unsigned Insn,
205 uint64_t Address,
206 const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000207static DecodeStatus DecodeSORegMemOperand(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000208 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000209static DecodeStatus DecodeAddrMode3Instruction(MCInst &Inst,unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000210 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000211static DecodeStatus DecodeSORegImmOperand(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000212 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000213static DecodeStatus DecodeSORegRegOperand(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000214 uint64_t Address, const void *Decoder);
215
Craig Topperf6e7e122012-03-27 07:21:54 +0000216static DecodeStatus DecodeMemMultipleWritebackInstruction(MCInst & Inst,
Owen Andersone0152a72011-08-09 20:55:18 +0000217 unsigned Insn,
218 uint64_t Adddress,
219 const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000220static DecodeStatus DecodeT2MOVTWInstruction(MCInst &Inst, unsigned Insn,
Kevin Enderby5dcda642011-10-04 22:44:48 +0000221 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000222static DecodeStatus DecodeArmMOVTWInstruction(MCInst &Inst, unsigned Insn,
Kevin Enderby5dcda642011-10-04 22:44:48 +0000223 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000224static DecodeStatus DecodeSMLAInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000225 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000226static DecodeStatus DecodeCPSInstruction(MCInst &Inst, unsigned Insn,
Owen Anderson3d2e0e9d2011-08-09 23:05:39 +0000227 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000228static DecodeStatus DecodeT2CPSInstruction(MCInst &Inst, unsigned Insn,
Owen Anderson9b7bd152011-08-23 17:45:18 +0000229 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000230static DecodeStatus DecodeAddrModeImm12Operand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000231 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000232static DecodeStatus DecodeAddrMode5Operand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000233 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000234static DecodeStatus DecodeAddrMode7Operand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000235 uint64_t Address, const void *Decoder);
Kevin Enderby40d4e472012-04-12 23:13:34 +0000236static DecodeStatus DecodeT2BInstruction(MCInst &Inst, unsigned Insn,
237 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000238static DecodeStatus DecodeBranchImmInstruction(MCInst &Inst,unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000239 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000240static DecodeStatus DecodeAddrMode6Operand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000241 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000242static DecodeStatus DecodeVLDInstruction(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000243 uint64_t Address, const void *Decoder);
Mihai Popaf41e3f52013-05-20 14:57:05 +0000244static DecodeStatus DecodeVST1Instruction(MCInst &Inst, unsigned Val,
245 uint64_t Address, const void *Decoder);
246static DecodeStatus DecodeVST2Instruction(MCInst &Inst, unsigned Val,
247 uint64_t Address, const void *Decoder);
248static DecodeStatus DecodeVST3Instruction(MCInst &Inst, unsigned Val,
249 uint64_t Address, const void *Decoder);
250static DecodeStatus DecodeVST4Instruction(MCInst &Inst, unsigned Val,
251 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000252static DecodeStatus DecodeVSTInstruction(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000253 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000254static DecodeStatus DecodeVLD1DupInstruction(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000255 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000256static DecodeStatus DecodeVLD2DupInstruction(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000257 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000258static DecodeStatus DecodeVLD3DupInstruction(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000259 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000260static DecodeStatus DecodeVLD4DupInstruction(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000261 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000262static DecodeStatus DecodeNEONModImmInstruction(MCInst &Inst,unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000263 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000264static DecodeStatus DecodeVSHLMaxInstruction(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000265 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000266static DecodeStatus DecodeShiftRight8Imm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000267 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000268static DecodeStatus DecodeShiftRight16Imm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000269 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000270static DecodeStatus DecodeShiftRight32Imm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000271 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000272static DecodeStatus DecodeShiftRight64Imm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000273 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000274static DecodeStatus DecodeTBLInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000275 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000276static DecodeStatus DecodePostIdxReg(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000277 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000278static DecodeStatus DecodeCoprocessor(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000279 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000280static DecodeStatus DecodeMemBarrierOption(MCInst &Inst, unsigned Insn,
Owen Andersone0089312011-08-09 23:25:42 +0000281 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000282static DecodeStatus DecodeMSRMask(MCInst &Inst, unsigned Insn,
Owen Anderson60663402011-08-11 20:21:46 +0000283 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000284static DecodeStatus DecodeDoubleRegLoad(MCInst &Inst, unsigned Insn,
Owen Andersonb685c9f2011-08-11 21:34:58 +0000285 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000286static DecodeStatus DecodeDoubleRegStore(MCInst &Inst, unsigned Insn,
Owen Andersonc5798a3a52011-08-12 17:58:32 +0000287 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000288static DecodeStatus DecodeLDRPreImm(MCInst &Inst, unsigned Insn,
Owen Anderson16d33f32011-08-26 20:43:14 +0000289 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000290static DecodeStatus DecodeLDRPreReg(MCInst &Inst, unsigned Insn,
Owen Anderson16d33f32011-08-26 20:43:14 +0000291 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000292static DecodeStatus DecodeSTRPreImm(MCInst &Inst, unsigned Insn,
Owen Anderson3987a612011-08-12 18:12:39 +0000293 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000294static DecodeStatus DecodeSTRPreReg(MCInst &Inst, unsigned Insn,
Owen Anderson3987a612011-08-12 18:12:39 +0000295 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000296static DecodeStatus DecodeVLD1LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +0000297 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000298static DecodeStatus DecodeVLD2LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +0000299 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000300static DecodeStatus DecodeVLD3LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +0000301 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000302static DecodeStatus DecodeVLD4LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +0000303 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000304static DecodeStatus DecodeVST1LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +0000305 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000306static DecodeStatus DecodeVST2LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +0000307 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000308static DecodeStatus DecodeVST3LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +0000309 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000310static DecodeStatus DecodeVST4LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +0000311 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000312static DecodeStatus DecodeVMOVSRR(MCInst &Inst, unsigned Insn,
Owen Andersondf698b02011-08-22 20:27:12 +0000313 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000314static DecodeStatus DecodeVMOVRRS(MCInst &Inst, unsigned Insn,
Owen Andersondf698b02011-08-22 20:27:12 +0000315 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000316static DecodeStatus DecodeSwap(MCInst &Inst, unsigned Insn,
Owen Andersondde461c2011-10-28 18:02:13 +0000317 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000318static DecodeStatus DecodeVCVTD(MCInst &Inst, unsigned Insn,
Owen Anderson0ac90582011-11-15 19:55:00 +0000319 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000320static DecodeStatus DecodeVCVTQ(MCInst &Inst, unsigned Insn,
Owen Anderson0ac90582011-11-15 19:55:00 +0000321 uint64_t Address, const void *Decoder);
Quentin Colombet6f03f622013-04-17 18:46:12 +0000322static DecodeStatus DecodeImm0_4(MCInst &Inst, unsigned Insn, uint64_t Address,
323 const void *Decoder);
Owen Anderson0ac90582011-11-15 19:55:00 +0000324
Owen Andersone0152a72011-08-09 20:55:18 +0000325
Craig Topperf6e7e122012-03-27 07:21:54 +0000326static DecodeStatus DecodeThumbAddSpecialReg(MCInst &Inst, uint16_t Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000327 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000328static DecodeStatus DecodeThumbBROperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000329 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000330static DecodeStatus DecodeT2BROperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000331 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000332static DecodeStatus DecodeThumbCmpBROperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000333 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000334static DecodeStatus DecodeThumbAddrModeRR(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000335 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000336static DecodeStatus DecodeThumbAddrModeIS(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000337 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000338static DecodeStatus DecodeThumbAddrModePC(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000339 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000340static DecodeStatus DecodeThumbAddrModeSP(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000341 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000342static DecodeStatus DecodeT2AddrModeSOReg(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000343 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000344static DecodeStatus DecodeT2LoadShift(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000345 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000346static DecodeStatus DecodeT2Imm8S4(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000347 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000348static DecodeStatus DecodeT2AddrModeImm8s4(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000349 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000350static DecodeStatus DecodeT2AddrModeImm0_1020s4(MCInst &Inst,unsigned Val,
Jim Grosbacha05627e2011-09-09 18:37:27 +0000351 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000352static DecodeStatus DecodeT2Imm8(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000353 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000354static DecodeStatus DecodeT2AddrModeImm8(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000355 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000356static DecodeStatus DecodeThumbAddSPImm(MCInst &Inst, uint16_t Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000357 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000358static DecodeStatus DecodeThumbAddSPReg(MCInst &Inst, uint16_t Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000359 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000360static DecodeStatus DecodeThumbCPS(MCInst &Inst, uint16_t Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000361 uint64_t Address, const void *Decoder);
Amaury de la Vieuville631df632013-06-08 13:38:52 +0000362static DecodeStatus DecodeQADDInstruction(MCInst &Inst, unsigned Insn,
363 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000364static DecodeStatus DecodeThumbBLXOffset(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000365 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000366static DecodeStatus DecodeT2AddrModeImm12(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000367 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000368static DecodeStatus DecodeThumbTableBranch(MCInst &Inst, unsigned Val,
Jim Grosbach05541f42011-09-19 22:21:13 +0000369 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000370static DecodeStatus DecodeThumb2BCCInstruction(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000371 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000372static DecodeStatus DecodeT2SOImm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000373 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000374static DecodeStatus DecodeThumbBCCTargetOperand(MCInst &Inst,unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000375 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000376static DecodeStatus DecodeThumbBLTargetOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000377 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000378static DecodeStatus DecodeIT(MCInst &Inst, unsigned Val,
Owen Anderson37612a32011-08-24 22:40:22 +0000379 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000380static DecodeStatus DecodeT2LDRDPreInstruction(MCInst &Inst,unsigned Insn,
Jim Grosbach7db8d692011-09-08 22:07:06 +0000381 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000382static DecodeStatus DecodeT2STRDPreInstruction(MCInst &Inst,unsigned Insn,
Jim Grosbach7db8d692011-09-08 22:07:06 +0000383 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000384static DecodeStatus DecodeT2Adr(MCInst &Inst, unsigned Val,
Owen Anderson5bfb0e02011-09-09 22:24:36 +0000385 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000386static DecodeStatus DecodeT2LdStPre(MCInst &Inst, unsigned Val,
Owen Andersona9ebf6f2011-09-12 18:56:30 +0000387 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000388static DecodeStatus DecodeT2ShifterImmOperand(MCInst &Inst, unsigned Val,
Owen Andersonf01e2de2011-09-26 21:06:22 +0000389 uint64_t Address, const void *Decoder);
390
Craig Topperf6e7e122012-03-27 07:21:54 +0000391static DecodeStatus DecodeLDR(MCInst &Inst, unsigned Val,
Silviu Barangad213f212012-03-22 13:24:43 +0000392 uint64_t Address, const void *Decoder);
Silviu Baranga41f1fcd2012-04-18 13:12:50 +0000393static DecodeStatus DecodeMRRC2(llvm::MCInst &Inst, unsigned Val,
394 uint64_t Address, const void *Decoder);
Owen Andersone0152a72011-08-09 20:55:18 +0000395#include "ARMGenDisassemblerTables.inc"
Sean Callanan814e69b2010-04-13 21:21:57 +0000396
James Molloy4c493e82011-09-07 17:24:38 +0000397static MCDisassembler *createARMDisassembler(const Target &T, const MCSubtargetInfo &STI) {
398 return new ARMDisassembler(STI);
Johnny Chen7b999ea2010-04-02 22:27:38 +0000399}
400
James Molloy4c493e82011-09-07 17:24:38 +0000401static MCDisassembler *createThumbDisassembler(const Target &T, const MCSubtargetInfo &STI) {
402 return new ThumbDisassembler(STI);
Johnny Chen7b999ea2010-04-02 22:27:38 +0000403}
404
Owen Anderson03aadae2011-09-01 23:23:50 +0000405DecodeStatus ARMDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
Derek Schuff56b662c2012-02-29 01:09:06 +0000406 const MemoryObject &Region,
Jim Grosbachd14b70d2011-08-17 21:58:18 +0000407 uint64_t Address,
Owen Andersona0c3b972011-09-15 23:38:46 +0000408 raw_ostream &os,
409 raw_ostream &cs) const {
Kevin Enderby5dcda642011-10-04 22:44:48 +0000410 CommentStream = &cs;
411
Owen Andersone0152a72011-08-09 20:55:18 +0000412 uint8_t bytes[4];
413
James Molloy8067df92011-09-07 19:42:28 +0000414 assert(!(STI.getFeatureBits() & ARM::ModeThumb) &&
415 "Asked to disassemble an ARM instruction but Subtarget is in Thumb mode!");
416
Owen Andersone0152a72011-08-09 20:55:18 +0000417 // We want to read exactly 4 bytes of data.
Benjamin Kramer534d3a42013-05-24 10:54:58 +0000418 if (Region.readBytes(Address, 4, bytes) == -1) {
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000419 Size = 0;
James Molloydb4ce602011-09-01 18:02:14 +0000420 return MCDisassembler::Fail;
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000421 }
Owen Andersone0152a72011-08-09 20:55:18 +0000422
423 // Encoded as a small-endian 32-bit word in the stream.
424 uint32_t insn = (bytes[3] << 24) |
425 (bytes[2] << 16) |
426 (bytes[1] << 8) |
427 (bytes[0] << 0);
428
429 // Calling the auto-generated decoder function.
Jim Grosbachecaef492012-08-14 19:06:05 +0000430 DecodeStatus result = decodeInstruction(DecoderTableARM32, MI, insn,
431 Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000432 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000433 Size = 4;
Owen Andersona4043c42011-08-17 17:44:15 +0000434 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000435 }
436
Owen Andersone0152a72011-08-09 20:55:18 +0000437 // VFP and NEON instructions, similarly, are shared between ARM
438 // and Thumb modes.
439 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000440 result = decodeInstruction(DecoderTableVFP32, MI, insn, Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000441 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000442 Size = 4;
Owen Andersona4043c42011-08-17 17:44:15 +0000443 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000444 }
445
446 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000447 result = decodeInstruction(DecoderTableNEONData32, MI, insn, Address,
448 this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000449 if (result != MCDisassembler::Fail) {
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000450 Size = 4;
Owen Andersone0152a72011-08-09 20:55:18 +0000451 // Add a fake predicate operand, because we share these instruction
452 // definitions with Thumb2 where these instructions are predicable.
Owen Anderson03aadae2011-09-01 23:23:50 +0000453 if (!DecodePredicateOperand(MI, 0xE, Address, this))
454 return MCDisassembler::Fail;
Owen Andersona4043c42011-08-17 17:44:15 +0000455 return result;
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000456 }
457
458 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000459 result = decodeInstruction(DecoderTableNEONLoadStore32, MI, insn, Address,
460 this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000461 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000462 Size = 4;
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000463 // Add a fake predicate operand, because we share these instruction
464 // definitions with Thumb2 where these instructions are predicable.
Owen Anderson03aadae2011-09-01 23:23:50 +0000465 if (!DecodePredicateOperand(MI, 0xE, Address, this))
466 return MCDisassembler::Fail;
Owen Andersona4043c42011-08-17 17:44:15 +0000467 return result;
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000468 }
469
470 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000471 result = decodeInstruction(DecoderTableNEONDup32, MI, insn, Address,
472 this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000473 if (result != MCDisassembler::Fail) {
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000474 Size = 4;
475 // Add a fake predicate operand, because we share these instruction
476 // definitions with Thumb2 where these instructions are predicable.
Owen Anderson03aadae2011-09-01 23:23:50 +0000477 if (!DecodePredicateOperand(MI, 0xE, Address, this))
478 return MCDisassembler::Fail;
Owen Andersona4043c42011-08-17 17:44:15 +0000479 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000480 }
481
482 MI.clear();
483
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000484 Size = 0;
James Molloydb4ce602011-09-01 18:02:14 +0000485 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000486}
487
488namespace llvm {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000489extern const MCInstrDesc ARMInsts[];
Owen Andersone0152a72011-08-09 20:55:18 +0000490}
491
Kevin Enderby5dcda642011-10-04 22:44:48 +0000492/// tryAddingSymbolicOperand - trys to add a symbolic operand in place of the
493/// immediate Value in the MCInst. The immediate Value has had any PC
494/// adjustment made by the caller. If the instruction is a branch instruction
495/// then isBranch is true, else false. If the getOpInfo() function was set as
496/// part of the setupForSymbolicDisassembly() call then that function is called
497/// to get any symbolic information at the Address for this instruction. If
498/// that returns non-zero then the symbolic information it returns is used to
499/// create an MCExpr and that is added as an operand to the MCInst. If
500/// getOpInfo() returns zero and isBranch is true then a symbol look up for
501/// Value is done and if a symbol is found an MCExpr is created with that, else
502/// an MCExpr with Value is created. This function returns true if it adds an
503/// operand to the MCInst and false otherwise.
504static bool tryAddingSymbolicOperand(uint64_t Address, int32_t Value,
505 bool isBranch, uint64_t InstSize,
506 MCInst &MI, const void *Decoder) {
507 const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000508 // FIXME: Does it make sense for value to be negative?
509 return Dis->tryAddingSymbolicOperand(MI, (uint32_t)Value, Address, isBranch,
510 /* Offset */ 0, InstSize);
Kevin Enderby5dcda642011-10-04 22:44:48 +0000511}
512
513/// tryAddingPcLoadReferenceComment - trys to add a comment as to what is being
514/// referenced by a load instruction with the base register that is the Pc.
515/// These can often be values in a literal pool near the Address of the
516/// instruction. The Address of the instruction and its immediate Value are
517/// used as a possible literal pool entry. The SymbolLookUp call back will
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000518/// return the name of a symbol referenced by the literal pool's entry if
Kevin Enderby5dcda642011-10-04 22:44:48 +0000519/// the referenced address is that of a symbol. Or it will return a pointer to
520/// a literal 'C' string if the referenced address of the literal pool's entry
521/// is an address into a section with 'C' string literals.
522static void tryAddingPcLoadReferenceComment(uint64_t Address, int Value,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000523 const void *Decoder) {
Kevin Enderby5dcda642011-10-04 22:44:48 +0000524 const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000525 Dis->tryAddingPcLoadReferenceComment(Value, Address);
Kevin Enderby5dcda642011-10-04 22:44:48 +0000526}
527
Owen Andersone0152a72011-08-09 20:55:18 +0000528// Thumb1 instructions don't have explicit S bits. Rather, they
529// implicitly set CPSR. Since it's not represented in the encoding, the
530// auto-generated decoder won't inject the CPSR operand. We need to fix
531// that as a post-pass.
532static void AddThumb1SBit(MCInst &MI, bool InITBlock) {
533 const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
Owen Anderson187e1e42011-08-17 18:14:48 +0000534 unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
Owen Andersone0152a72011-08-09 20:55:18 +0000535 MCInst::iterator I = MI.begin();
Owen Anderson187e1e42011-08-17 18:14:48 +0000536 for (unsigned i = 0; i < NumOps; ++i, ++I) {
537 if (I == MI.end()) break;
Owen Andersone0152a72011-08-09 20:55:18 +0000538 if (OpInfo[i].isOptionalDef() && OpInfo[i].RegClass == ARM::CCRRegClassID) {
Owen Anderson187e1e42011-08-17 18:14:48 +0000539 if (i > 0 && OpInfo[i-1].isPredicate()) continue;
Owen Andersone0152a72011-08-09 20:55:18 +0000540 MI.insert(I, MCOperand::CreateReg(InITBlock ? 0 : ARM::CPSR));
541 return;
542 }
543 }
544
Owen Anderson187e1e42011-08-17 18:14:48 +0000545 MI.insert(I, MCOperand::CreateReg(InITBlock ? 0 : ARM::CPSR));
Owen Andersone0152a72011-08-09 20:55:18 +0000546}
547
548// Most Thumb instructions don't have explicit predicates in the
549// encoding, but rather get their predicates from IT context. We need
550// to fix up the predicate operands using this context information as a
551// post-pass.
Owen Anderson2fefa422011-09-08 22:42:49 +0000552MCDisassembler::DecodeStatus
553ThumbDisassembler::AddThumbPredicate(MCInst &MI) const {
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000554 MCDisassembler::DecodeStatus S = Success;
555
Owen Andersone0152a72011-08-09 20:55:18 +0000556 // A few instructions actually have predicates encoded in them. Don't
557 // try to overwrite it if we're seeing one of those.
558 switch (MI.getOpcode()) {
559 case ARM::tBcc:
560 case ARM::t2Bcc:
Owen Anderson2fefa422011-09-08 22:42:49 +0000561 case ARM::tCBZ:
562 case ARM::tCBNZ:
Owen Anderson61e46042011-09-19 23:47:10 +0000563 case ARM::tCPS:
564 case ARM::t2CPS3p:
565 case ARM::t2CPS2p:
566 case ARM::t2CPS1p:
Owen Anderson163be012011-09-19 23:57:20 +0000567 case ARM::tMOVSr:
Owen Anderson44f76ea2011-10-13 17:58:39 +0000568 case ARM::tSETEND:
Owen Anderson33d39532011-09-08 22:48:37 +0000569 // Some instructions (mostly conditional branches) are not
570 // allowed in IT blocks.
Richard Bartone9600002012-04-24 11:13:20 +0000571 if (ITBlock.instrInITBlock())
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000572 S = SoftFail;
573 else
574 return Success;
575 break;
576 case ARM::tB:
577 case ARM::t2B:
Owen Andersonf902d922011-09-19 22:34:23 +0000578 case ARM::t2TBB:
579 case ARM::t2TBH:
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000580 // Some instructions (mostly unconditional branches) can
581 // only appears at the end of, or outside of, an IT.
Richard Bartone9600002012-04-24 11:13:20 +0000582 if (ITBlock.instrInITBlock() && !ITBlock.instrLastInITBlock())
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000583 S = SoftFail;
Owen Anderson2fefa422011-09-08 22:42:49 +0000584 break;
Owen Andersone0152a72011-08-09 20:55:18 +0000585 default:
586 break;
587 }
588
589 // If we're in an IT block, base the predicate on that. Otherwise,
590 // assume a predicate of AL.
591 unsigned CC;
Richard Bartone9600002012-04-24 11:13:20 +0000592 CC = ITBlock.getITCC();
593 if (CC == 0xF)
Owen Andersone0152a72011-08-09 20:55:18 +0000594 CC = ARMCC::AL;
Richard Bartone9600002012-04-24 11:13:20 +0000595 if (ITBlock.instrInITBlock())
596 ITBlock.advanceITState();
Owen Andersone0152a72011-08-09 20:55:18 +0000597
598 const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
Owen Anderson187e1e42011-08-17 18:14:48 +0000599 unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
Owen Andersone0152a72011-08-09 20:55:18 +0000600 MCInst::iterator I = MI.begin();
Owen Anderson187e1e42011-08-17 18:14:48 +0000601 for (unsigned i = 0; i < NumOps; ++i, ++I) {
602 if (I == MI.end()) break;
Owen Andersone0152a72011-08-09 20:55:18 +0000603 if (OpInfo[i].isPredicate()) {
604 I = MI.insert(I, MCOperand::CreateImm(CC));
605 ++I;
606 if (CC == ARMCC::AL)
607 MI.insert(I, MCOperand::CreateReg(0));
608 else
609 MI.insert(I, MCOperand::CreateReg(ARM::CPSR));
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000610 return S;
Owen Andersone0152a72011-08-09 20:55:18 +0000611 }
612 }
613
Owen Anderson187e1e42011-08-17 18:14:48 +0000614 I = MI.insert(I, MCOperand::CreateImm(CC));
615 ++I;
Owen Andersone0152a72011-08-09 20:55:18 +0000616 if (CC == ARMCC::AL)
Owen Anderson187e1e42011-08-17 18:14:48 +0000617 MI.insert(I, MCOperand::CreateReg(0));
Owen Andersone0152a72011-08-09 20:55:18 +0000618 else
Owen Anderson187e1e42011-08-17 18:14:48 +0000619 MI.insert(I, MCOperand::CreateReg(ARM::CPSR));
Owen Anderson2fefa422011-09-08 22:42:49 +0000620
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000621 return S;
Owen Andersone0152a72011-08-09 20:55:18 +0000622}
623
624// Thumb VFP instructions are a special case. Because we share their
625// encodings between ARM and Thumb modes, and they are predicable in ARM
626// mode, the auto-generated decoder will give them an (incorrect)
627// predicate operand. We need to rewrite these operands based on the IT
628// context as a post-pass.
629void ThumbDisassembler::UpdateThumbVFPPredicate(MCInst &MI) const {
630 unsigned CC;
Richard Bartone9600002012-04-24 11:13:20 +0000631 CC = ITBlock.getITCC();
632 if (ITBlock.instrInITBlock())
633 ITBlock.advanceITState();
Owen Andersone0152a72011-08-09 20:55:18 +0000634
635 const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
636 MCInst::iterator I = MI.begin();
Owen Anderson216cfaa2011-08-24 21:35:46 +0000637 unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
638 for (unsigned i = 0; i < NumOps; ++i, ++I) {
Owen Andersone0152a72011-08-09 20:55:18 +0000639 if (OpInfo[i].isPredicate() ) {
640 I->setImm(CC);
641 ++I;
642 if (CC == ARMCC::AL)
643 I->setReg(0);
644 else
645 I->setReg(ARM::CPSR);
646 return;
647 }
648 }
649}
650
Owen Anderson03aadae2011-09-01 23:23:50 +0000651DecodeStatus ThumbDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
Derek Schuff56b662c2012-02-29 01:09:06 +0000652 const MemoryObject &Region,
Jim Grosbachd14b70d2011-08-17 21:58:18 +0000653 uint64_t Address,
Owen Andersona0c3b972011-09-15 23:38:46 +0000654 raw_ostream &os,
655 raw_ostream &cs) const {
Kevin Enderby5dcda642011-10-04 22:44:48 +0000656 CommentStream = &cs;
657
Owen Andersone0152a72011-08-09 20:55:18 +0000658 uint8_t bytes[4];
659
James Molloy8067df92011-09-07 19:42:28 +0000660 assert((STI.getFeatureBits() & ARM::ModeThumb) &&
661 "Asked to disassemble in Thumb mode but Subtarget is in ARM mode!");
662
Owen Andersone0152a72011-08-09 20:55:18 +0000663 // We want to read exactly 2 bytes of data.
Benjamin Kramer534d3a42013-05-24 10:54:58 +0000664 if (Region.readBytes(Address, 2, bytes) == -1) {
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000665 Size = 0;
James Molloydb4ce602011-09-01 18:02:14 +0000666 return MCDisassembler::Fail;
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000667 }
Owen Andersone0152a72011-08-09 20:55:18 +0000668
669 uint16_t insn16 = (bytes[1] << 8) | bytes[0];
Jim Grosbachecaef492012-08-14 19:06:05 +0000670 DecodeStatus result = decodeInstruction(DecoderTableThumb16, MI, insn16,
671 Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000672 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000673 Size = 2;
Owen Anderson2fefa422011-09-08 22:42:49 +0000674 Check(result, AddThumbPredicate(MI));
Owen Andersona4043c42011-08-17 17:44:15 +0000675 return result;
Owen Anderson91a8f9b2011-08-16 23:45:44 +0000676 }
677
678 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000679 result = decodeInstruction(DecoderTableThumbSBit16, MI, insn16,
680 Address, this, STI);
Owen Anderson91a8f9b2011-08-16 23:45:44 +0000681 if (result) {
682 Size = 2;
Richard Bartone9600002012-04-24 11:13:20 +0000683 bool InITBlock = ITBlock.instrInITBlock();
Owen Anderson2fefa422011-09-08 22:42:49 +0000684 Check(result, AddThumbPredicate(MI));
Owen Andersone0152a72011-08-09 20:55:18 +0000685 AddThumb1SBit(MI, InITBlock);
Owen Andersona4043c42011-08-17 17:44:15 +0000686 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000687 }
688
689 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000690 result = decodeInstruction(DecoderTableThumb216, MI, insn16,
691 Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000692 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000693 Size = 2;
Owen Anderson6a5c1502011-10-06 23:33:11 +0000694
695 // Nested IT blocks are UNPREDICTABLE. Must be checked before we add
696 // the Thumb predicate.
Richard Bartone9600002012-04-24 11:13:20 +0000697 if (MI.getOpcode() == ARM::t2IT && ITBlock.instrInITBlock())
Owen Anderson6a5c1502011-10-06 23:33:11 +0000698 result = MCDisassembler::SoftFail;
699
Owen Anderson2fefa422011-09-08 22:42:49 +0000700 Check(result, AddThumbPredicate(MI));
Owen Andersone0152a72011-08-09 20:55:18 +0000701
702 // If we find an IT instruction, we need to parse its condition
703 // code and mask operands so that we can apply them correctly
704 // to the subsequent instructions.
705 if (MI.getOpcode() == ARM::t2IT) {
Owen Andersonf1e38442011-09-14 21:06:21 +0000706
Richard Bartone9600002012-04-24 11:13:20 +0000707 unsigned Firstcond = MI.getOperand(0).getImm();
Owen Anderson2fa06a72011-08-30 22:58:27 +0000708 unsigned Mask = MI.getOperand(1).getImm();
Richard Bartone9600002012-04-24 11:13:20 +0000709 ITBlock.setITState(Firstcond, Mask);
Owen Andersone0152a72011-08-09 20:55:18 +0000710 }
711
Owen Andersona4043c42011-08-17 17:44:15 +0000712 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000713 }
714
715 // We want to read exactly 4 bytes of data.
Benjamin Kramer534d3a42013-05-24 10:54:58 +0000716 if (Region.readBytes(Address, 4, bytes) == -1) {
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000717 Size = 0;
James Molloydb4ce602011-09-01 18:02:14 +0000718 return MCDisassembler::Fail;
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000719 }
Owen Andersone0152a72011-08-09 20:55:18 +0000720
721 uint32_t insn32 = (bytes[3] << 8) |
722 (bytes[2] << 0) |
723 (bytes[1] << 24) |
724 (bytes[0] << 16);
725 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000726 result = decodeInstruction(DecoderTableThumb32, MI, insn32, Address,
727 this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000728 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000729 Size = 4;
Richard Bartone9600002012-04-24 11:13:20 +0000730 bool InITBlock = ITBlock.instrInITBlock();
Owen Anderson2fefa422011-09-08 22:42:49 +0000731 Check(result, AddThumbPredicate(MI));
Owen Andersone0152a72011-08-09 20:55:18 +0000732 AddThumb1SBit(MI, InITBlock);
Owen Andersona4043c42011-08-17 17:44:15 +0000733 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000734 }
735
736 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000737 result = decodeInstruction(DecoderTableThumb232, MI, insn32, Address,
738 this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000739 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000740 Size = 4;
Owen Anderson2fefa422011-09-08 22:42:49 +0000741 Check(result, AddThumbPredicate(MI));
Owen Andersona4043c42011-08-17 17:44:15 +0000742 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000743 }
744
745 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000746 result = decodeInstruction(DecoderTableVFP32, MI, insn32, Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000747 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000748 Size = 4;
749 UpdateThumbVFPPredicate(MI);
Owen Andersona4043c42011-08-17 17:44:15 +0000750 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000751 }
752
753 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000754 result = decodeInstruction(DecoderTableNEONDup32, MI, insn32, Address,
755 this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000756 if (result != MCDisassembler::Fail) {
Owen Andersona6201f02011-08-15 23:38:54 +0000757 Size = 4;
Owen Anderson2fefa422011-09-08 22:42:49 +0000758 Check(result, AddThumbPredicate(MI));
Owen Andersona4043c42011-08-17 17:44:15 +0000759 return result;
Owen Andersona6201f02011-08-15 23:38:54 +0000760 }
761
Jim Grosbachecaef492012-08-14 19:06:05 +0000762 if (fieldFromInstruction(insn32, 24, 8) == 0xF9) {
Owen Andersona6201f02011-08-15 23:38:54 +0000763 MI.clear();
764 uint32_t NEONLdStInsn = insn32;
765 NEONLdStInsn &= 0xF0FFFFFF;
766 NEONLdStInsn |= 0x04000000;
Jim Grosbachecaef492012-08-14 19:06:05 +0000767 result = decodeInstruction(DecoderTableNEONLoadStore32, MI, NEONLdStInsn,
768 Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000769 if (result != MCDisassembler::Fail) {
Owen Andersona6201f02011-08-15 23:38:54 +0000770 Size = 4;
Owen Anderson2fefa422011-09-08 22:42:49 +0000771 Check(result, AddThumbPredicate(MI));
Owen Andersona4043c42011-08-17 17:44:15 +0000772 return result;
Owen Andersona6201f02011-08-15 23:38:54 +0000773 }
774 }
775
Jim Grosbachecaef492012-08-14 19:06:05 +0000776 if (fieldFromInstruction(insn32, 24, 4) == 0xF) {
Owen Andersona6201f02011-08-15 23:38:54 +0000777 MI.clear();
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000778 uint32_t NEONDataInsn = insn32;
779 NEONDataInsn &= 0xF0FFFFFF; // Clear bits 27-24
780 NEONDataInsn |= (NEONDataInsn & 0x10000000) >> 4; // Move bit 28 to bit 24
781 NEONDataInsn |= 0x12000000; // Set bits 28 and 25
Jim Grosbachecaef492012-08-14 19:06:05 +0000782 result = decodeInstruction(DecoderTableNEONData32, MI, NEONDataInsn,
783 Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000784 if (result != MCDisassembler::Fail) {
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000785 Size = 4;
Owen Anderson2fefa422011-09-08 22:42:49 +0000786 Check(result, AddThumbPredicate(MI));
Owen Andersona4043c42011-08-17 17:44:15 +0000787 return result;
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000788 }
789 }
790
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000791 Size = 0;
James Molloydb4ce602011-09-01 18:02:14 +0000792 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000793}
794
795
796extern "C" void LLVMInitializeARMDisassembler() {
797 TargetRegistry::RegisterMCDisassembler(TheARMTarget,
798 createARMDisassembler);
799 TargetRegistry::RegisterMCDisassembler(TheThumbTarget,
800 createThumbDisassembler);
801}
802
Craig Topperca658c22012-03-11 07:16:55 +0000803static const uint16_t GPRDecoderTable[] = {
Owen Andersone0152a72011-08-09 20:55:18 +0000804 ARM::R0, ARM::R1, ARM::R2, ARM::R3,
805 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
806 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
807 ARM::R12, ARM::SP, ARM::LR, ARM::PC
808};
809
Craig Topperf6e7e122012-03-27 07:21:54 +0000810static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000811 uint64_t Address, const void *Decoder) {
812 if (RegNo > 15)
James Molloydb4ce602011-09-01 18:02:14 +0000813 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000814
815 unsigned Register = GPRDecoderTable[RegNo];
816 Inst.addOperand(MCOperand::CreateReg(Register));
James Molloydb4ce602011-09-01 18:02:14 +0000817 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +0000818}
819
Owen Anderson03aadae2011-09-01 23:23:50 +0000820static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +0000821DecodeGPRnopcRegisterClass(MCInst &Inst, unsigned RegNo,
Jim Grosbachd14b70d2011-08-17 21:58:18 +0000822 uint64_t Address, const void *Decoder) {
Silviu Baranga32a49332012-03-20 15:54:56 +0000823 DecodeStatus S = MCDisassembler::Success;
824
825 if (RegNo == 15)
826 S = MCDisassembler::SoftFail;
827
828 Check(S, DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder));
829
830 return S;
Owen Anderson042619f2011-08-09 22:48:45 +0000831}
832
Mihai Popadc1764c52013-05-13 14:10:04 +0000833static DecodeStatus
834DecodeGPRwithAPSRRegisterClass(MCInst &Inst, unsigned RegNo,
835 uint64_t Address, const void *Decoder) {
836 DecodeStatus S = MCDisassembler::Success;
837
838 if (RegNo == 15)
839 {
840 Inst.addOperand(MCOperand::CreateReg(ARM::APSR_NZCV));
841 return MCDisassembler::Success;
842 }
843
844 Check(S, DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder));
845 return S;
846}
847
Craig Topperf6e7e122012-03-27 07:21:54 +0000848static DecodeStatus DecodetGPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000849 uint64_t Address, const void *Decoder) {
850 if (RegNo > 7)
James Molloydb4ce602011-09-01 18:02:14 +0000851 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000852 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
853}
854
Craig Topperf6e7e122012-03-27 07:21:54 +0000855static DecodeStatus DecodetcGPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000856 uint64_t Address, const void *Decoder) {
857 unsigned Register = 0;
858 switch (RegNo) {
859 case 0:
860 Register = ARM::R0;
861 break;
862 case 1:
863 Register = ARM::R1;
864 break;
865 case 2:
866 Register = ARM::R2;
867 break;
868 case 3:
869 Register = ARM::R3;
870 break;
871 case 9:
872 Register = ARM::R9;
873 break;
874 case 12:
875 Register = ARM::R12;
876 break;
877 default:
James Molloydb4ce602011-09-01 18:02:14 +0000878 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000879 }
880
881 Inst.addOperand(MCOperand::CreateReg(Register));
James Molloydb4ce602011-09-01 18:02:14 +0000882 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +0000883}
884
Craig Topperf6e7e122012-03-27 07:21:54 +0000885static DecodeStatus DecoderGPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000886 uint64_t Address, const void *Decoder) {
James Molloydb4ce602011-09-01 18:02:14 +0000887 if (RegNo == 13 || RegNo == 15) return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000888 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
889}
890
Craig Topperca658c22012-03-11 07:16:55 +0000891static const uint16_t SPRDecoderTable[] = {
Owen Andersone0152a72011-08-09 20:55:18 +0000892 ARM::S0, ARM::S1, ARM::S2, ARM::S3,
893 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
894 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
895 ARM::S12, ARM::S13, ARM::S14, ARM::S15,
896 ARM::S16, ARM::S17, ARM::S18, ARM::S19,
897 ARM::S20, ARM::S21, ARM::S22, ARM::S23,
898 ARM::S24, ARM::S25, ARM::S26, ARM::S27,
899 ARM::S28, ARM::S29, ARM::S30, ARM::S31
900};
901
Craig Topperf6e7e122012-03-27 07:21:54 +0000902static DecodeStatus DecodeSPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000903 uint64_t Address, const void *Decoder) {
904 if (RegNo > 31)
James Molloydb4ce602011-09-01 18:02:14 +0000905 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000906
907 unsigned Register = SPRDecoderTable[RegNo];
908 Inst.addOperand(MCOperand::CreateReg(Register));
James Molloydb4ce602011-09-01 18:02:14 +0000909 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +0000910}
911
Craig Topperca658c22012-03-11 07:16:55 +0000912static const uint16_t DPRDecoderTable[] = {
Owen Andersone0152a72011-08-09 20:55:18 +0000913 ARM::D0, ARM::D1, ARM::D2, ARM::D3,
914 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
915 ARM::D8, ARM::D9, ARM::D10, ARM::D11,
916 ARM::D12, ARM::D13, ARM::D14, ARM::D15,
917 ARM::D16, ARM::D17, ARM::D18, ARM::D19,
918 ARM::D20, ARM::D21, ARM::D22, ARM::D23,
919 ARM::D24, ARM::D25, ARM::D26, ARM::D27,
920 ARM::D28, ARM::D29, ARM::D30, ARM::D31
921};
922
Craig Topperf6e7e122012-03-27 07:21:54 +0000923static DecodeStatus DecodeDPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000924 uint64_t Address, const void *Decoder) {
925 if (RegNo > 31)
James Molloydb4ce602011-09-01 18:02:14 +0000926 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000927
928 unsigned Register = DPRDecoderTable[RegNo];
929 Inst.addOperand(MCOperand::CreateReg(Register));
James Molloydb4ce602011-09-01 18:02:14 +0000930 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +0000931}
932
Craig Topperf6e7e122012-03-27 07:21:54 +0000933static DecodeStatus DecodeDPR_8RegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000934 uint64_t Address, const void *Decoder) {
935 if (RegNo > 7)
James Molloydb4ce602011-09-01 18:02:14 +0000936 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000937 return DecodeDPRRegisterClass(Inst, RegNo, Address, Decoder);
938}
939
Owen Anderson03aadae2011-09-01 23:23:50 +0000940static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +0000941DecodeDPR_VFP2RegisterClass(MCInst &Inst, unsigned RegNo,
Jim Grosbachd14b70d2011-08-17 21:58:18 +0000942 uint64_t Address, const void *Decoder) {
Owen Andersone0152a72011-08-09 20:55:18 +0000943 if (RegNo > 15)
James Molloydb4ce602011-09-01 18:02:14 +0000944 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000945 return DecodeDPRRegisterClass(Inst, RegNo, Address, Decoder);
946}
947
Craig Topperca658c22012-03-11 07:16:55 +0000948static const uint16_t QPRDecoderTable[] = {
Owen Andersone0152a72011-08-09 20:55:18 +0000949 ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
950 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7,
951 ARM::Q8, ARM::Q9, ARM::Q10, ARM::Q11,
952 ARM::Q12, ARM::Q13, ARM::Q14, ARM::Q15
953};
954
955
Craig Topperf6e7e122012-03-27 07:21:54 +0000956static DecodeStatus DecodeQPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000957 uint64_t Address, const void *Decoder) {
Mihai Popadcf09222013-05-20 14:42:43 +0000958 if (RegNo > 31 || (RegNo & 1) != 0)
James Molloydb4ce602011-09-01 18:02:14 +0000959 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000960 RegNo >>= 1;
961
962 unsigned Register = QPRDecoderTable[RegNo];
963 Inst.addOperand(MCOperand::CreateReg(Register));
James Molloydb4ce602011-09-01 18:02:14 +0000964 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +0000965}
966
Craig Topperca658c22012-03-11 07:16:55 +0000967static const uint16_t DPairDecoderTable[] = {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000968 ARM::Q0, ARM::D1_D2, ARM::Q1, ARM::D3_D4, ARM::Q2, ARM::D5_D6,
969 ARM::Q3, ARM::D7_D8, ARM::Q4, ARM::D9_D10, ARM::Q5, ARM::D11_D12,
970 ARM::Q6, ARM::D13_D14, ARM::Q7, ARM::D15_D16, ARM::Q8, ARM::D17_D18,
971 ARM::Q9, ARM::D19_D20, ARM::Q10, ARM::D21_D22, ARM::Q11, ARM::D23_D24,
972 ARM::Q12, ARM::D25_D26, ARM::Q13, ARM::D27_D28, ARM::Q14, ARM::D29_D30,
973 ARM::Q15
974};
975
Craig Topperf6e7e122012-03-27 07:21:54 +0000976static DecodeStatus DecodeDPairRegisterClass(MCInst &Inst, unsigned RegNo,
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000977 uint64_t Address, const void *Decoder) {
978 if (RegNo > 30)
979 return MCDisassembler::Fail;
980
981 unsigned Register = DPairDecoderTable[RegNo];
982 Inst.addOperand(MCOperand::CreateReg(Register));
983 return MCDisassembler::Success;
984}
985
Craig Topperca658c22012-03-11 07:16:55 +0000986static const uint16_t DPairSpacedDecoderTable[] = {
Jim Grosbache5307f92012-03-05 21:43:40 +0000987 ARM::D0_D2, ARM::D1_D3, ARM::D2_D4, ARM::D3_D5,
988 ARM::D4_D6, ARM::D5_D7, ARM::D6_D8, ARM::D7_D9,
989 ARM::D8_D10, ARM::D9_D11, ARM::D10_D12, ARM::D11_D13,
990 ARM::D12_D14, ARM::D13_D15, ARM::D14_D16, ARM::D15_D17,
991 ARM::D16_D18, ARM::D17_D19, ARM::D18_D20, ARM::D19_D21,
992 ARM::D20_D22, ARM::D21_D23, ARM::D22_D24, ARM::D23_D25,
993 ARM::D24_D26, ARM::D25_D27, ARM::D26_D28, ARM::D27_D29,
994 ARM::D28_D30, ARM::D29_D31
995};
996
Craig Topperf6e7e122012-03-27 07:21:54 +0000997static DecodeStatus DecodeDPairSpacedRegisterClass(MCInst &Inst,
Jim Grosbache5307f92012-03-05 21:43:40 +0000998 unsigned RegNo,
999 uint64_t Address,
1000 const void *Decoder) {
1001 if (RegNo > 29)
1002 return MCDisassembler::Fail;
1003
1004 unsigned Register = DPairSpacedDecoderTable[RegNo];
1005 Inst.addOperand(MCOperand::CreateReg(Register));
1006 return MCDisassembler::Success;
1007}
1008
Craig Topperf6e7e122012-03-27 07:21:54 +00001009static DecodeStatus DecodePredicateOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001010 uint64_t Address, const void *Decoder) {
James Molloydb4ce602011-09-01 18:02:14 +00001011 if (Val == 0xF) return MCDisassembler::Fail;
Owen Anderson7a2401d2011-08-09 21:07:45 +00001012 // AL predicate is not allowed on Thumb1 branches.
1013 if (Inst.getOpcode() == ARM::tBcc && Val == 0xE)
James Molloydb4ce602011-09-01 18:02:14 +00001014 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001015 Inst.addOperand(MCOperand::CreateImm(Val));
1016 if (Val == ARMCC::AL) {
1017 Inst.addOperand(MCOperand::CreateReg(0));
1018 } else
1019 Inst.addOperand(MCOperand::CreateReg(ARM::CPSR));
James Molloydb4ce602011-09-01 18:02:14 +00001020 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00001021}
1022
Craig Topperf6e7e122012-03-27 07:21:54 +00001023static DecodeStatus DecodeCCOutOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001024 uint64_t Address, const void *Decoder) {
1025 if (Val)
1026 Inst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1027 else
1028 Inst.addOperand(MCOperand::CreateReg(0));
James Molloydb4ce602011-09-01 18:02:14 +00001029 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00001030}
1031
Craig Topperf6e7e122012-03-27 07:21:54 +00001032static DecodeStatus DecodeSOImmOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001033 uint64_t Address, const void *Decoder) {
1034 uint32_t imm = Val & 0xFF;
1035 uint32_t rot = (Val & 0xF00) >> 7;
Eli Friedmana7ad9f32011-10-13 23:36:06 +00001036 uint32_t rot_imm = (imm >> rot) | (imm << ((32-rot) & 0x1F));
Owen Andersone0152a72011-08-09 20:55:18 +00001037 Inst.addOperand(MCOperand::CreateImm(rot_imm));
James Molloydb4ce602011-09-01 18:02:14 +00001038 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00001039}
1040
Craig Topperf6e7e122012-03-27 07:21:54 +00001041static DecodeStatus DecodeSORegImmOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001042 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001043 DecodeStatus S = MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00001044
Jim Grosbachecaef492012-08-14 19:06:05 +00001045 unsigned Rm = fieldFromInstruction(Val, 0, 4);
1046 unsigned type = fieldFromInstruction(Val, 5, 2);
1047 unsigned imm = fieldFromInstruction(Val, 7, 5);
Owen Andersone0152a72011-08-09 20:55:18 +00001048
1049 // Register-immediate
Owen Anderson03aadae2011-09-01 23:23:50 +00001050 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
1051 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001052
1053 ARM_AM::ShiftOpc Shift = ARM_AM::lsl;
1054 switch (type) {
1055 case 0:
1056 Shift = ARM_AM::lsl;
1057 break;
1058 case 1:
1059 Shift = ARM_AM::lsr;
1060 break;
1061 case 2:
1062 Shift = ARM_AM::asr;
1063 break;
1064 case 3:
1065 Shift = ARM_AM::ror;
1066 break;
1067 }
1068
1069 if (Shift == ARM_AM::ror && imm == 0)
1070 Shift = ARM_AM::rrx;
1071
1072 unsigned Op = Shift | (imm << 3);
1073 Inst.addOperand(MCOperand::CreateImm(Op));
1074
Owen Andersona4043c42011-08-17 17:44:15 +00001075 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001076}
1077
Craig Topperf6e7e122012-03-27 07:21:54 +00001078static DecodeStatus DecodeSORegRegOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001079 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001080 DecodeStatus S = MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00001081
Jim Grosbachecaef492012-08-14 19:06:05 +00001082 unsigned Rm = fieldFromInstruction(Val, 0, 4);
1083 unsigned type = fieldFromInstruction(Val, 5, 2);
1084 unsigned Rs = fieldFromInstruction(Val, 8, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00001085
1086 // Register-register
Owen Anderson03aadae2011-09-01 23:23:50 +00001087 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
1088 return MCDisassembler::Fail;
1089 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rs, Address, Decoder)))
1090 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001091
1092 ARM_AM::ShiftOpc Shift = ARM_AM::lsl;
1093 switch (type) {
1094 case 0:
1095 Shift = ARM_AM::lsl;
1096 break;
1097 case 1:
1098 Shift = ARM_AM::lsr;
1099 break;
1100 case 2:
1101 Shift = ARM_AM::asr;
1102 break;
1103 case 3:
1104 Shift = ARM_AM::ror;
1105 break;
1106 }
1107
1108 Inst.addOperand(MCOperand::CreateImm(Shift));
1109
Owen Andersona4043c42011-08-17 17:44:15 +00001110 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001111}
1112
Craig Topperf6e7e122012-03-27 07:21:54 +00001113static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001114 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001115 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001116
Owen Anderson53db43b2011-09-09 23:13:33 +00001117 bool writebackLoad = false;
1118 unsigned writebackReg = 0;
1119 switch (Inst.getOpcode()) {
1120 default:
1121 break;
1122 case ARM::LDMIA_UPD:
1123 case ARM::LDMDB_UPD:
1124 case ARM::LDMIB_UPD:
1125 case ARM::LDMDA_UPD:
1126 case ARM::t2LDMIA_UPD:
1127 case ARM::t2LDMDB_UPD:
1128 writebackLoad = true;
1129 writebackReg = Inst.getOperand(0).getReg();
1130 break;
1131 }
1132
Owen Anderson60663402011-08-11 20:21:46 +00001133 // Empty register lists are not allowed.
Benjamin Kramer8bad66e2013-05-19 22:01:57 +00001134 if (Val == 0) return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001135 for (unsigned i = 0; i < 16; ++i) {
Owen Andersoned253852011-08-11 18:24:51 +00001136 if (Val & (1 << i)) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001137 if (!Check(S, DecodeGPRRegisterClass(Inst, i, Address, Decoder)))
1138 return MCDisassembler::Fail;
Owen Anderson53db43b2011-09-09 23:13:33 +00001139 // Writeback not allowed if Rn is in the target list.
1140 if (writebackLoad && writebackReg == Inst.end()[-1].getReg())
1141 Check(S, MCDisassembler::SoftFail);
Owen Andersoned253852011-08-11 18:24:51 +00001142 }
Owen Andersone0152a72011-08-09 20:55:18 +00001143 }
1144
Owen Andersona4043c42011-08-17 17:44:15 +00001145 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001146}
1147
Craig Topperf6e7e122012-03-27 07:21:54 +00001148static DecodeStatus DecodeSPRRegListOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001149 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001150 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001151
Jim Grosbachecaef492012-08-14 19:06:05 +00001152 unsigned Vd = fieldFromInstruction(Val, 8, 5);
1153 unsigned regs = fieldFromInstruction(Val, 0, 8);
Owen Andersone0152a72011-08-09 20:55:18 +00001154
Tim Northover4173e292013-05-31 15:55:51 +00001155 // In case of unpredictable encoding, tweak the operands.
1156 if (regs == 0 || (Vd + regs) > 32) {
1157 regs = Vd + regs > 32 ? 32 - Vd : regs;
1158 regs = std::max( 1u, regs);
1159 S = MCDisassembler::SoftFail;
1160 }
1161
Owen Anderson03aadae2011-09-01 23:23:50 +00001162 if (!Check(S, DecodeSPRRegisterClass(Inst, Vd, Address, Decoder)))
1163 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00001164 for (unsigned i = 0; i < (regs - 1); ++i) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001165 if (!Check(S, DecodeSPRRegisterClass(Inst, ++Vd, Address, Decoder)))
1166 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00001167 }
Owen Andersone0152a72011-08-09 20:55:18 +00001168
Owen Andersona4043c42011-08-17 17:44:15 +00001169 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001170}
1171
Craig Topperf6e7e122012-03-27 07:21:54 +00001172static DecodeStatus DecodeDPRRegListOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001173 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001174 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001175
Jim Grosbachecaef492012-08-14 19:06:05 +00001176 unsigned Vd = fieldFromInstruction(Val, 8, 5);
Tim Northover4173e292013-05-31 15:55:51 +00001177 unsigned regs = fieldFromInstruction(Val, 1, 7);
Silviu Baranga9560af82012-05-03 16:38:40 +00001178
Tim Northover4173e292013-05-31 15:55:51 +00001179 // In case of unpredictable encoding, tweak the operands.
1180 if (regs == 0 || regs > 16 || (Vd + regs) > 32) {
1181 regs = Vd + regs > 32 ? 32 - Vd : regs;
1182 regs = std::max( 1u, regs);
1183 regs = std::min(16u, regs);
1184 S = MCDisassembler::SoftFail;
1185 }
Owen Andersone0152a72011-08-09 20:55:18 +00001186
Owen Anderson03aadae2011-09-01 23:23:50 +00001187 if (!Check(S, DecodeDPRRegisterClass(Inst, Vd, Address, Decoder)))
1188 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00001189 for (unsigned i = 0; i < (regs - 1); ++i) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001190 if (!Check(S, DecodeDPRRegisterClass(Inst, ++Vd, Address, Decoder)))
1191 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00001192 }
Owen Andersone0152a72011-08-09 20:55:18 +00001193
Owen Andersona4043c42011-08-17 17:44:15 +00001194 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001195}
1196
Craig Topperf6e7e122012-03-27 07:21:54 +00001197static DecodeStatus DecodeBitfieldMaskOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001198 uint64_t Address, const void *Decoder) {
Owen Anderson5d69f632011-08-10 17:36:48 +00001199 // This operand encodes a mask of contiguous zeros between a specified MSB
1200 // and LSB. To decode it, we create the mask of all bits MSB-and-lower,
1201 // the mask of all bits LSB-and-lower, and then xor them to create
Jim Grosbachd14b70d2011-08-17 21:58:18 +00001202 // the mask of that's all ones on [msb, lsb]. Finally we not it to
Owen Anderson5d69f632011-08-10 17:36:48 +00001203 // create the final mask.
Jim Grosbachecaef492012-08-14 19:06:05 +00001204 unsigned msb = fieldFromInstruction(Val, 5, 5);
1205 unsigned lsb = fieldFromInstruction(Val, 0, 5);
Owen Anderson3ca958c2011-09-16 22:29:48 +00001206
Owen Anderson502cd9d2011-09-16 23:30:01 +00001207 DecodeStatus S = MCDisassembler::Success;
Kevin Enderby136d6742012-11-29 23:47:11 +00001208 if (lsb > msb) {
1209 Check(S, MCDisassembler::SoftFail);
1210 // The check above will cause the warning for the "potentially undefined
1211 // instruction encoding" but we can't build a bad MCOperand value here
1212 // with a lsb > msb or else printing the MCInst will cause a crash.
1213 lsb = msb;
1214 }
Owen Anderson502cd9d2011-09-16 23:30:01 +00001215
Owen Andersonb925e932011-09-16 23:04:48 +00001216 uint32_t msb_mask = 0xFFFFFFFF;
1217 if (msb != 31) msb_mask = (1U << (msb+1)) - 1;
1218 uint32_t lsb_mask = (1U << lsb) - 1;
Owen Anderson3ca958c2011-09-16 22:29:48 +00001219
Owen Andersone0152a72011-08-09 20:55:18 +00001220 Inst.addOperand(MCOperand::CreateImm(~(msb_mask ^ lsb_mask)));
Owen Anderson502cd9d2011-09-16 23:30:01 +00001221 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001222}
1223
Craig Topperf6e7e122012-03-27 07:21:54 +00001224static DecodeStatus DecodeCopMemInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00001225 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001226 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001227
Jim Grosbachecaef492012-08-14 19:06:05 +00001228 unsigned pred = fieldFromInstruction(Insn, 28, 4);
1229 unsigned CRd = fieldFromInstruction(Insn, 12, 4);
1230 unsigned coproc = fieldFromInstruction(Insn, 8, 4);
1231 unsigned imm = fieldFromInstruction(Insn, 0, 8);
1232 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
1233 unsigned U = fieldFromInstruction(Insn, 23, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00001234
1235 switch (Inst.getOpcode()) {
1236 case ARM::LDC_OFFSET:
1237 case ARM::LDC_PRE:
1238 case ARM::LDC_POST:
1239 case ARM::LDC_OPTION:
1240 case ARM::LDCL_OFFSET:
1241 case ARM::LDCL_PRE:
1242 case ARM::LDCL_POST:
1243 case ARM::LDCL_OPTION:
1244 case ARM::STC_OFFSET:
1245 case ARM::STC_PRE:
1246 case ARM::STC_POST:
1247 case ARM::STC_OPTION:
1248 case ARM::STCL_OFFSET:
1249 case ARM::STCL_PRE:
1250 case ARM::STCL_POST:
1251 case ARM::STCL_OPTION:
Owen Anderson18d17aa2011-09-07 21:10:42 +00001252 case ARM::t2LDC_OFFSET:
1253 case ARM::t2LDC_PRE:
1254 case ARM::t2LDC_POST:
1255 case ARM::t2LDC_OPTION:
1256 case ARM::t2LDCL_OFFSET:
1257 case ARM::t2LDCL_PRE:
1258 case ARM::t2LDCL_POST:
1259 case ARM::t2LDCL_OPTION:
1260 case ARM::t2STC_OFFSET:
1261 case ARM::t2STC_PRE:
1262 case ARM::t2STC_POST:
1263 case ARM::t2STC_OPTION:
1264 case ARM::t2STCL_OFFSET:
1265 case ARM::t2STCL_PRE:
1266 case ARM::t2STCL_POST:
1267 case ARM::t2STCL_OPTION:
Owen Andersone0152a72011-08-09 20:55:18 +00001268 if (coproc == 0xA || coproc == 0xB)
James Molloydb4ce602011-09-01 18:02:14 +00001269 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001270 break;
1271 default:
1272 break;
1273 }
1274
1275 Inst.addOperand(MCOperand::CreateImm(coproc));
1276 Inst.addOperand(MCOperand::CreateImm(CRd));
Owen Anderson03aadae2011-09-01 23:23:50 +00001277 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1278 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001279
Owen Andersone0152a72011-08-09 20:55:18 +00001280 switch (Inst.getOpcode()) {
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001281 case ARM::t2LDC2_OFFSET:
1282 case ARM::t2LDC2L_OFFSET:
1283 case ARM::t2LDC2_PRE:
1284 case ARM::t2LDC2L_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001285 case ARM::t2STC2_OFFSET:
1286 case ARM::t2STC2L_OFFSET:
1287 case ARM::t2STC2_PRE:
1288 case ARM::t2STC2L_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001289 case ARM::LDC2_OFFSET:
1290 case ARM::LDC2L_OFFSET:
1291 case ARM::LDC2_PRE:
1292 case ARM::LDC2L_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001293 case ARM::STC2_OFFSET:
1294 case ARM::STC2L_OFFSET:
1295 case ARM::STC2_PRE:
1296 case ARM::STC2L_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001297 case ARM::t2LDC_OFFSET:
1298 case ARM::t2LDCL_OFFSET:
1299 case ARM::t2LDC_PRE:
1300 case ARM::t2LDCL_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001301 case ARM::t2STC_OFFSET:
1302 case ARM::t2STCL_OFFSET:
1303 case ARM::t2STC_PRE:
1304 case ARM::t2STCL_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001305 case ARM::LDC_OFFSET:
1306 case ARM::LDCL_OFFSET:
1307 case ARM::LDC_PRE:
1308 case ARM::LDCL_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001309 case ARM::STC_OFFSET:
1310 case ARM::STCL_OFFSET:
1311 case ARM::STC_PRE:
1312 case ARM::STCL_PRE:
Jim Grosbacha098a892011-10-12 21:59:02 +00001313 imm = ARM_AM::getAM5Opc(U ? ARM_AM::add : ARM_AM::sub, imm);
1314 Inst.addOperand(MCOperand::CreateImm(imm));
1315 break;
1316 case ARM::t2LDC2_POST:
1317 case ARM::t2LDC2L_POST:
1318 case ARM::t2STC2_POST:
1319 case ARM::t2STC2L_POST:
1320 case ARM::LDC2_POST:
1321 case ARM::LDC2L_POST:
1322 case ARM::STC2_POST:
1323 case ARM::STC2L_POST:
1324 case ARM::t2LDC_POST:
1325 case ARM::t2LDCL_POST:
1326 case ARM::t2STC_POST:
1327 case ARM::t2STCL_POST:
1328 case ARM::LDC_POST:
1329 case ARM::LDCL_POST:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001330 case ARM::STC_POST:
1331 case ARM::STCL_POST:
Owen Andersone0152a72011-08-09 20:55:18 +00001332 imm |= U << 8;
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001333 // fall through.
Owen Andersone0152a72011-08-09 20:55:18 +00001334 default:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001335 // The 'option' variant doesn't encode 'U' in the immediate since
1336 // the immediate is unsigned [0,255].
1337 Inst.addOperand(MCOperand::CreateImm(imm));
Owen Andersone0152a72011-08-09 20:55:18 +00001338 break;
1339 }
1340
1341 switch (Inst.getOpcode()) {
1342 case ARM::LDC_OFFSET:
1343 case ARM::LDC_PRE:
1344 case ARM::LDC_POST:
1345 case ARM::LDC_OPTION:
1346 case ARM::LDCL_OFFSET:
1347 case ARM::LDCL_PRE:
1348 case ARM::LDCL_POST:
1349 case ARM::LDCL_OPTION:
1350 case ARM::STC_OFFSET:
1351 case ARM::STC_PRE:
1352 case ARM::STC_POST:
1353 case ARM::STC_OPTION:
1354 case ARM::STCL_OFFSET:
1355 case ARM::STCL_PRE:
1356 case ARM::STCL_POST:
1357 case ARM::STCL_OPTION:
Owen Anderson03aadae2011-09-01 23:23:50 +00001358 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
1359 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001360 break;
1361 default:
1362 break;
1363 }
1364
Owen Andersona4043c42011-08-17 17:44:15 +00001365 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001366}
1367
Owen Anderson03aadae2011-09-01 23:23:50 +00001368static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00001369DecodeAddrMode2IdxInstruction(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00001370 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001371 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001372
Jim Grosbachecaef492012-08-14 19:06:05 +00001373 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
1374 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
1375 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
1376 unsigned imm = fieldFromInstruction(Insn, 0, 12);
1377 unsigned pred = fieldFromInstruction(Insn, 28, 4);
1378 unsigned reg = fieldFromInstruction(Insn, 25, 1);
1379 unsigned P = fieldFromInstruction(Insn, 24, 1);
1380 unsigned W = fieldFromInstruction(Insn, 21, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00001381
1382 // On stores, the writeback operand precedes Rt.
1383 switch (Inst.getOpcode()) {
1384 case ARM::STR_POST_IMM:
1385 case ARM::STR_POST_REG:
Owen Anderson3a850f22011-08-11 20:47:56 +00001386 case ARM::STRB_POST_IMM:
1387 case ARM::STRB_POST_REG:
Jim Grosbache2594212011-08-11 22:18:00 +00001388 case ARM::STRT_POST_REG:
1389 case ARM::STRT_POST_IMM:
Jim Grosbach2a502602011-08-11 20:04:56 +00001390 case ARM::STRBT_POST_REG:
1391 case ARM::STRBT_POST_IMM:
Owen Anderson03aadae2011-09-01 23:23:50 +00001392 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1393 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001394 break;
1395 default:
1396 break;
1397 }
1398
Owen Anderson03aadae2011-09-01 23:23:50 +00001399 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
1400 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001401
1402 // On loads, the writeback operand comes after Rt.
1403 switch (Inst.getOpcode()) {
1404 case ARM::LDR_POST_IMM:
1405 case ARM::LDR_POST_REG:
Owen Anderson3a850f22011-08-11 20:47:56 +00001406 case ARM::LDRB_POST_IMM:
1407 case ARM::LDRB_POST_REG:
Owen Andersone0152a72011-08-09 20:55:18 +00001408 case ARM::LDRBT_POST_REG:
1409 case ARM::LDRBT_POST_IMM:
Jim Grosbachd5d63592011-08-10 23:43:54 +00001410 case ARM::LDRT_POST_REG:
1411 case ARM::LDRT_POST_IMM:
Owen Anderson03aadae2011-09-01 23:23:50 +00001412 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1413 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001414 break;
1415 default:
1416 break;
1417 }
1418
Owen Anderson03aadae2011-09-01 23:23:50 +00001419 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1420 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001421
1422 ARM_AM::AddrOpc Op = ARM_AM::add;
Jim Grosbachecaef492012-08-14 19:06:05 +00001423 if (!fieldFromInstruction(Insn, 23, 1))
Owen Andersone0152a72011-08-09 20:55:18 +00001424 Op = ARM_AM::sub;
1425
1426 bool writeback = (P == 0) || (W == 1);
1427 unsigned idx_mode = 0;
1428 if (P && writeback)
1429 idx_mode = ARMII::IndexModePre;
1430 else if (!P && writeback)
1431 idx_mode = ARMII::IndexModePost;
1432
Owen Anderson03aadae2011-09-01 23:23:50 +00001433 if (writeback && (Rn == 15 || Rn == Rt))
1434 S = MCDisassembler::SoftFail; // UNPREDICTABLE
Owen Anderson3477f2c2011-08-11 19:00:18 +00001435
Owen Andersone0152a72011-08-09 20:55:18 +00001436 if (reg) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001437 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
1438 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001439 ARM_AM::ShiftOpc Opc = ARM_AM::lsl;
Jim Grosbachecaef492012-08-14 19:06:05 +00001440 switch( fieldFromInstruction(Insn, 5, 2)) {
Owen Andersone0152a72011-08-09 20:55:18 +00001441 case 0:
1442 Opc = ARM_AM::lsl;
1443 break;
1444 case 1:
1445 Opc = ARM_AM::lsr;
1446 break;
1447 case 2:
1448 Opc = ARM_AM::asr;
1449 break;
1450 case 3:
1451 Opc = ARM_AM::ror;
1452 break;
1453 default:
James Molloydb4ce602011-09-01 18:02:14 +00001454 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001455 }
Jim Grosbachecaef492012-08-14 19:06:05 +00001456 unsigned amt = fieldFromInstruction(Insn, 7, 5);
Tim Northover0c97e762012-09-22 11:18:12 +00001457 if (Opc == ARM_AM::ror && amt == 0)
1458 Opc = ARM_AM::rrx;
Owen Andersone0152a72011-08-09 20:55:18 +00001459 unsigned imm = ARM_AM::getAM2Opc(Op, amt, Opc, idx_mode);
1460
1461 Inst.addOperand(MCOperand::CreateImm(imm));
1462 } else {
1463 Inst.addOperand(MCOperand::CreateReg(0));
1464 unsigned tmp = ARM_AM::getAM2Opc(Op, imm, ARM_AM::lsl, idx_mode);
1465 Inst.addOperand(MCOperand::CreateImm(tmp));
1466 }
1467
Owen Anderson03aadae2011-09-01 23:23:50 +00001468 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
1469 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001470
Owen Andersona4043c42011-08-17 17:44:15 +00001471 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001472}
1473
Craig Topperf6e7e122012-03-27 07:21:54 +00001474static DecodeStatus DecodeSORegMemOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001475 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001476 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001477
Jim Grosbachecaef492012-08-14 19:06:05 +00001478 unsigned Rn = fieldFromInstruction(Val, 13, 4);
1479 unsigned Rm = fieldFromInstruction(Val, 0, 4);
1480 unsigned type = fieldFromInstruction(Val, 5, 2);
1481 unsigned imm = fieldFromInstruction(Val, 7, 5);
1482 unsigned U = fieldFromInstruction(Val, 12, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00001483
Owen Andersond151b092011-08-09 21:38:14 +00001484 ARM_AM::ShiftOpc ShOp = ARM_AM::lsl;
Owen Andersone0152a72011-08-09 20:55:18 +00001485 switch (type) {
1486 case 0:
1487 ShOp = ARM_AM::lsl;
1488 break;
1489 case 1:
1490 ShOp = ARM_AM::lsr;
1491 break;
1492 case 2:
1493 ShOp = ARM_AM::asr;
1494 break;
1495 case 3:
1496 ShOp = ARM_AM::ror;
1497 break;
1498 }
1499
Tim Northover0c97e762012-09-22 11:18:12 +00001500 if (ShOp == ARM_AM::ror && imm == 0)
1501 ShOp = ARM_AM::rrx;
1502
Owen Anderson03aadae2011-09-01 23:23:50 +00001503 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1504 return MCDisassembler::Fail;
1505 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
1506 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001507 unsigned shift;
1508 if (U)
1509 shift = ARM_AM::getAM2Opc(ARM_AM::add, imm, ShOp);
1510 else
1511 shift = ARM_AM::getAM2Opc(ARM_AM::sub, imm, ShOp);
1512 Inst.addOperand(MCOperand::CreateImm(shift));
1513
Owen Andersona4043c42011-08-17 17:44:15 +00001514 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001515}
1516
Owen Anderson03aadae2011-09-01 23:23:50 +00001517static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00001518DecodeAddrMode3Instruction(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00001519 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001520 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001521
Jim Grosbachecaef492012-08-14 19:06:05 +00001522 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
1523 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
1524 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
1525 unsigned type = fieldFromInstruction(Insn, 22, 1);
1526 unsigned imm = fieldFromInstruction(Insn, 8, 4);
1527 unsigned U = ((~fieldFromInstruction(Insn, 23, 1)) & 1) << 8;
1528 unsigned pred = fieldFromInstruction(Insn, 28, 4);
1529 unsigned W = fieldFromInstruction(Insn, 21, 1);
1530 unsigned P = fieldFromInstruction(Insn, 24, 1);
Silviu Baranga4afd7d22012-03-22 14:14:49 +00001531 unsigned Rt2 = Rt + 1;
Owen Andersone0152a72011-08-09 20:55:18 +00001532
1533 bool writeback = (W == 1) | (P == 0);
Owen Anderson1d5d2ca2011-08-15 20:51:32 +00001534
1535 // For {LD,ST}RD, Rt must be even, else undefined.
1536 switch (Inst.getOpcode()) {
1537 case ARM::STRD:
1538 case ARM::STRD_PRE:
1539 case ARM::STRD_POST:
1540 case ARM::LDRD:
1541 case ARM::LDRD_PRE:
1542 case ARM::LDRD_POST:
Silviu Baranga4afd7d22012-03-22 14:14:49 +00001543 if (Rt & 0x1) S = MCDisassembler::SoftFail;
1544 break;
1545 default:
1546 break;
1547 }
1548 switch (Inst.getOpcode()) {
1549 case ARM::STRD:
1550 case ARM::STRD_PRE:
1551 case ARM::STRD_POST:
1552 if (P == 0 && W == 1)
1553 S = MCDisassembler::SoftFail;
1554
1555 if (writeback && (Rn == 15 || Rn == Rt || Rn == Rt2))
1556 S = MCDisassembler::SoftFail;
1557 if (type && Rm == 15)
1558 S = MCDisassembler::SoftFail;
1559 if (Rt2 == 15)
1560 S = MCDisassembler::SoftFail;
Jim Grosbachecaef492012-08-14 19:06:05 +00001561 if (!type && fieldFromInstruction(Insn, 8, 4))
Silviu Baranga4afd7d22012-03-22 14:14:49 +00001562 S = MCDisassembler::SoftFail;
1563 break;
1564 case ARM::STRH:
1565 case ARM::STRH_PRE:
1566 case ARM::STRH_POST:
1567 if (Rt == 15)
1568 S = MCDisassembler::SoftFail;
1569 if (writeback && (Rn == 15 || Rn == Rt))
1570 S = MCDisassembler::SoftFail;
1571 if (!type && Rm == 15)
1572 S = MCDisassembler::SoftFail;
1573 break;
1574 case ARM::LDRD:
1575 case ARM::LDRD_PRE:
1576 case ARM::LDRD_POST:
1577 if (type && Rn == 15){
1578 if (Rt2 == 15)
1579 S = MCDisassembler::SoftFail;
1580 break;
1581 }
1582 if (P == 0 && W == 1)
1583 S = MCDisassembler::SoftFail;
1584 if (!type && (Rt2 == 15 || Rm == 15 || Rm == Rt || Rm == Rt2))
1585 S = MCDisassembler::SoftFail;
1586 if (!type && writeback && Rn == 15)
1587 S = MCDisassembler::SoftFail;
1588 if (writeback && (Rn == Rt || Rn == Rt2))
1589 S = MCDisassembler::SoftFail;
1590 break;
1591 case ARM::LDRH:
1592 case ARM::LDRH_PRE:
1593 case ARM::LDRH_POST:
1594 if (type && Rn == 15){
1595 if (Rt == 15)
1596 S = MCDisassembler::SoftFail;
1597 break;
1598 }
1599 if (Rt == 15)
1600 S = MCDisassembler::SoftFail;
1601 if (!type && Rm == 15)
1602 S = MCDisassembler::SoftFail;
1603 if (!type && writeback && (Rn == 15 || Rn == Rt))
1604 S = MCDisassembler::SoftFail;
1605 break;
1606 case ARM::LDRSH:
1607 case ARM::LDRSH_PRE:
1608 case ARM::LDRSH_POST:
1609 case ARM::LDRSB:
1610 case ARM::LDRSB_PRE:
1611 case ARM::LDRSB_POST:
1612 if (type && Rn == 15){
1613 if (Rt == 15)
1614 S = MCDisassembler::SoftFail;
1615 break;
1616 }
1617 if (type && (Rt == 15 || (writeback && Rn == Rt)))
1618 S = MCDisassembler::SoftFail;
1619 if (!type && (Rt == 15 || Rm == 15))
1620 S = MCDisassembler::SoftFail;
1621 if (!type && writeback && (Rn == 15 || Rn == Rt))
1622 S = MCDisassembler::SoftFail;
Owen Anderson1d5d2ca2011-08-15 20:51:32 +00001623 break;
Owen Anderson03aadae2011-09-01 23:23:50 +00001624 default:
1625 break;
Owen Anderson1d5d2ca2011-08-15 20:51:32 +00001626 }
1627
Owen Andersone0152a72011-08-09 20:55:18 +00001628 if (writeback) { // Writeback
1629 if (P)
1630 U |= ARMII::IndexModePre << 9;
1631 else
1632 U |= ARMII::IndexModePost << 9;
1633
1634 // On stores, the writeback operand precedes Rt.
1635 switch (Inst.getOpcode()) {
1636 case ARM::STRD:
1637 case ARM::STRD_PRE:
1638 case ARM::STRD_POST:
Owen Anderson60138ea2011-08-12 20:02:50 +00001639 case ARM::STRH:
1640 case ARM::STRH_PRE:
1641 case ARM::STRH_POST:
Owen Anderson03aadae2011-09-01 23:23:50 +00001642 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1643 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001644 break;
1645 default:
1646 break;
1647 }
1648 }
1649
Owen Anderson03aadae2011-09-01 23:23:50 +00001650 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
1651 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001652 switch (Inst.getOpcode()) {
1653 case ARM::STRD:
1654 case ARM::STRD_PRE:
1655 case ARM::STRD_POST:
1656 case ARM::LDRD:
1657 case ARM::LDRD_PRE:
1658 case ARM::LDRD_POST:
Owen Anderson03aadae2011-09-01 23:23:50 +00001659 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt+1, Address, Decoder)))
1660 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001661 break;
1662 default:
1663 break;
1664 }
1665
1666 if (writeback) {
1667 // On loads, the writeback operand comes after Rt.
1668 switch (Inst.getOpcode()) {
1669 case ARM::LDRD:
1670 case ARM::LDRD_PRE:
1671 case ARM::LDRD_POST:
Owen Anderson2d1d7a12011-08-12 20:36:11 +00001672 case ARM::LDRH:
1673 case ARM::LDRH_PRE:
1674 case ARM::LDRH_POST:
1675 case ARM::LDRSH:
1676 case ARM::LDRSH_PRE:
1677 case ARM::LDRSH_POST:
1678 case ARM::LDRSB:
1679 case ARM::LDRSB_PRE:
1680 case ARM::LDRSB_POST:
Owen Andersone0152a72011-08-09 20:55:18 +00001681 case ARM::LDRHTr:
1682 case ARM::LDRSBTr:
Owen Anderson03aadae2011-09-01 23:23:50 +00001683 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1684 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001685 break;
1686 default:
1687 break;
1688 }
1689 }
1690
Owen Anderson03aadae2011-09-01 23:23:50 +00001691 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1692 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001693
1694 if (type) {
1695 Inst.addOperand(MCOperand::CreateReg(0));
1696 Inst.addOperand(MCOperand::CreateImm(U | (imm << 4) | Rm));
1697 } else {
Owen Anderson03aadae2011-09-01 23:23:50 +00001698 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
1699 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001700 Inst.addOperand(MCOperand::CreateImm(U));
1701 }
1702
Owen Anderson03aadae2011-09-01 23:23:50 +00001703 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
1704 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001705
Owen Andersona4043c42011-08-17 17:44:15 +00001706 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001707}
1708
Craig Topperf6e7e122012-03-27 07:21:54 +00001709static DecodeStatus DecodeRFEInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00001710 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001711 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001712
Jim Grosbachecaef492012-08-14 19:06:05 +00001713 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
1714 unsigned mode = fieldFromInstruction(Insn, 23, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00001715
1716 switch (mode) {
1717 case 0:
1718 mode = ARM_AM::da;
1719 break;
1720 case 1:
1721 mode = ARM_AM::ia;
1722 break;
1723 case 2:
1724 mode = ARM_AM::db;
1725 break;
1726 case 3:
1727 mode = ARM_AM::ib;
1728 break;
1729 }
1730
1731 Inst.addOperand(MCOperand::CreateImm(mode));
Owen Anderson03aadae2011-09-01 23:23:50 +00001732 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1733 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001734
Owen Andersona4043c42011-08-17 17:44:15 +00001735 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001736}
1737
Amaury de la Vieuville631df632013-06-08 13:38:52 +00001738static DecodeStatus DecodeQADDInstruction(MCInst &Inst, unsigned Insn,
1739 uint64_t Address, const void *Decoder) {
1740 DecodeStatus S = MCDisassembler::Success;
1741
1742 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
1743 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
1744 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
1745 unsigned pred = fieldFromInstruction(Insn, 28, 4);
1746
1747 if (pred == 0xF)
1748 return DecodeCPSInstruction(Inst, Insn, Address, Decoder);
1749
1750 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
1751 return MCDisassembler::Fail;
1752 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
1753 return MCDisassembler::Fail;
1754 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
1755 return MCDisassembler::Fail;
1756 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
1757 return MCDisassembler::Fail;
1758 return S;
1759}
1760
Craig Topperf6e7e122012-03-27 07:21:54 +00001761static DecodeStatus DecodeMemMultipleWritebackInstruction(MCInst &Inst,
Owen Andersone0152a72011-08-09 20:55:18 +00001762 unsigned Insn,
1763 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001764 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001765
Jim Grosbachecaef492012-08-14 19:06:05 +00001766 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
1767 unsigned pred = fieldFromInstruction(Insn, 28, 4);
1768 unsigned reglist = fieldFromInstruction(Insn, 0, 16);
Owen Andersone0152a72011-08-09 20:55:18 +00001769
1770 if (pred == 0xF) {
1771 switch (Inst.getOpcode()) {
Owen Anderson192a7602011-08-18 22:31:17 +00001772 case ARM::LDMDA:
Owen Andersone0152a72011-08-09 20:55:18 +00001773 Inst.setOpcode(ARM::RFEDA);
1774 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001775 case ARM::LDMDA_UPD:
Owen Andersone0152a72011-08-09 20:55:18 +00001776 Inst.setOpcode(ARM::RFEDA_UPD);
1777 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001778 case ARM::LDMDB:
Owen Andersone0152a72011-08-09 20:55:18 +00001779 Inst.setOpcode(ARM::RFEDB);
1780 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001781 case ARM::LDMDB_UPD:
Owen Andersone0152a72011-08-09 20:55:18 +00001782 Inst.setOpcode(ARM::RFEDB_UPD);
1783 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001784 case ARM::LDMIA:
Owen Andersone0152a72011-08-09 20:55:18 +00001785 Inst.setOpcode(ARM::RFEIA);
1786 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001787 case ARM::LDMIA_UPD:
Owen Andersone0152a72011-08-09 20:55:18 +00001788 Inst.setOpcode(ARM::RFEIA_UPD);
1789 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001790 case ARM::LDMIB:
Owen Andersone0152a72011-08-09 20:55:18 +00001791 Inst.setOpcode(ARM::RFEIB);
1792 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001793 case ARM::LDMIB_UPD:
Owen Andersone0152a72011-08-09 20:55:18 +00001794 Inst.setOpcode(ARM::RFEIB_UPD);
1795 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001796 case ARM::STMDA:
1797 Inst.setOpcode(ARM::SRSDA);
1798 break;
1799 case ARM::STMDA_UPD:
1800 Inst.setOpcode(ARM::SRSDA_UPD);
1801 break;
1802 case ARM::STMDB:
1803 Inst.setOpcode(ARM::SRSDB);
1804 break;
1805 case ARM::STMDB_UPD:
1806 Inst.setOpcode(ARM::SRSDB_UPD);
1807 break;
1808 case ARM::STMIA:
1809 Inst.setOpcode(ARM::SRSIA);
1810 break;
1811 case ARM::STMIA_UPD:
1812 Inst.setOpcode(ARM::SRSIA_UPD);
1813 break;
1814 case ARM::STMIB:
1815 Inst.setOpcode(ARM::SRSIB);
1816 break;
1817 case ARM::STMIB_UPD:
1818 Inst.setOpcode(ARM::SRSIB_UPD);
1819 break;
1820 default:
James Molloydb4ce602011-09-01 18:02:14 +00001821 if (!Check(S, MCDisassembler::Fail)) return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001822 }
Owen Anderson192a7602011-08-18 22:31:17 +00001823
1824 // For stores (which become SRS's, the only operand is the mode.
Jim Grosbachecaef492012-08-14 19:06:05 +00001825 if (fieldFromInstruction(Insn, 20, 1) == 0) {
Owen Anderson192a7602011-08-18 22:31:17 +00001826 Inst.addOperand(
Jim Grosbachecaef492012-08-14 19:06:05 +00001827 MCOperand::CreateImm(fieldFromInstruction(Insn, 0, 4)));
Owen Anderson192a7602011-08-18 22:31:17 +00001828 return S;
1829 }
1830
Owen Andersone0152a72011-08-09 20:55:18 +00001831 return DecodeRFEInstruction(Inst, Insn, Address, Decoder);
1832 }
1833
Owen Anderson03aadae2011-09-01 23:23:50 +00001834 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1835 return MCDisassembler::Fail;
1836 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1837 return MCDisassembler::Fail; // Tied
1838 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
1839 return MCDisassembler::Fail;
1840 if (!Check(S, DecodeRegListOperand(Inst, reglist, Address, Decoder)))
1841 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001842
Owen Andersona4043c42011-08-17 17:44:15 +00001843 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001844}
1845
Craig Topperf6e7e122012-03-27 07:21:54 +00001846static DecodeStatus DecodeCPSInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00001847 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00001848 unsigned imod = fieldFromInstruction(Insn, 18, 2);
1849 unsigned M = fieldFromInstruction(Insn, 17, 1);
1850 unsigned iflags = fieldFromInstruction(Insn, 6, 3);
1851 unsigned mode = fieldFromInstruction(Insn, 0, 5);
Owen Andersone0152a72011-08-09 20:55:18 +00001852
Owen Anderson03aadae2011-09-01 23:23:50 +00001853 DecodeStatus S = MCDisassembler::Success;
Owen Anderson3d2e0e9d2011-08-09 23:05:39 +00001854
Amaury de la Vieuville631df632013-06-08 13:38:52 +00001855 // This decoder is called from multiple location that do not check
1856 // the full encoding is valid before they do.
1857 if (fieldFromInstruction(Insn, 5, 1) != 0 ||
1858 fieldFromInstruction(Insn, 16, 1) != 0 ||
1859 fieldFromInstruction(Insn, 20, 8) != 0x10)
1860 return MCDisassembler::Fail;
1861
Owen Anderson67d6f112011-08-18 22:11:02 +00001862 // imod == '01' --> UNPREDICTABLE
1863 // NOTE: Even though this is technically UNPREDICTABLE, we choose to
1864 // return failure here. The '01' imod value is unprintable, so there's
1865 // nothing useful we could do even if we returned UNPREDICTABLE.
1866
James Molloydb4ce602011-09-01 18:02:14 +00001867 if (imod == 1) return MCDisassembler::Fail;
Owen Anderson67d6f112011-08-18 22:11:02 +00001868
1869 if (imod && M) {
Owen Andersone0152a72011-08-09 20:55:18 +00001870 Inst.setOpcode(ARM::CPS3p);
1871 Inst.addOperand(MCOperand::CreateImm(imod));
1872 Inst.addOperand(MCOperand::CreateImm(iflags));
1873 Inst.addOperand(MCOperand::CreateImm(mode));
Owen Anderson67d6f112011-08-18 22:11:02 +00001874 } else if (imod && !M) {
Owen Andersone0152a72011-08-09 20:55:18 +00001875 Inst.setOpcode(ARM::CPS2p);
1876 Inst.addOperand(MCOperand::CreateImm(imod));
1877 Inst.addOperand(MCOperand::CreateImm(iflags));
James Molloydb4ce602011-09-01 18:02:14 +00001878 if (mode) S = MCDisassembler::SoftFail;
Owen Anderson67d6f112011-08-18 22:11:02 +00001879 } else if (!imod && M) {
Owen Andersone0152a72011-08-09 20:55:18 +00001880 Inst.setOpcode(ARM::CPS1p);
1881 Inst.addOperand(MCOperand::CreateImm(mode));
James Molloydb4ce602011-09-01 18:02:14 +00001882 if (iflags) S = MCDisassembler::SoftFail;
Owen Anderson5d2db892011-08-18 22:15:25 +00001883 } else {
Owen Anderson67d6f112011-08-18 22:11:02 +00001884 // imod == '00' && M == '0' --> UNPREDICTABLE
Owen Anderson5d2db892011-08-18 22:15:25 +00001885 Inst.setOpcode(ARM::CPS1p);
1886 Inst.addOperand(MCOperand::CreateImm(mode));
James Molloydb4ce602011-09-01 18:02:14 +00001887 S = MCDisassembler::SoftFail;
Owen Anderson5d2db892011-08-18 22:15:25 +00001888 }
Owen Andersone0152a72011-08-09 20:55:18 +00001889
Owen Anderson67d6f112011-08-18 22:11:02 +00001890 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001891}
1892
Craig Topperf6e7e122012-03-27 07:21:54 +00001893static DecodeStatus DecodeT2CPSInstruction(MCInst &Inst, unsigned Insn,
Owen Anderson9b7bd152011-08-23 17:45:18 +00001894 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00001895 unsigned imod = fieldFromInstruction(Insn, 9, 2);
1896 unsigned M = fieldFromInstruction(Insn, 8, 1);
1897 unsigned iflags = fieldFromInstruction(Insn, 5, 3);
1898 unsigned mode = fieldFromInstruction(Insn, 0, 5);
Owen Anderson9b7bd152011-08-23 17:45:18 +00001899
Owen Anderson03aadae2011-09-01 23:23:50 +00001900 DecodeStatus S = MCDisassembler::Success;
Owen Anderson9b7bd152011-08-23 17:45:18 +00001901
1902 // imod == '01' --> UNPREDICTABLE
1903 // NOTE: Even though this is technically UNPREDICTABLE, we choose to
1904 // return failure here. The '01' imod value is unprintable, so there's
1905 // nothing useful we could do even if we returned UNPREDICTABLE.
1906
James Molloydb4ce602011-09-01 18:02:14 +00001907 if (imod == 1) return MCDisassembler::Fail;
Owen Anderson9b7bd152011-08-23 17:45:18 +00001908
1909 if (imod && M) {
1910 Inst.setOpcode(ARM::t2CPS3p);
1911 Inst.addOperand(MCOperand::CreateImm(imod));
1912 Inst.addOperand(MCOperand::CreateImm(iflags));
1913 Inst.addOperand(MCOperand::CreateImm(mode));
1914 } else if (imod && !M) {
1915 Inst.setOpcode(ARM::t2CPS2p);
1916 Inst.addOperand(MCOperand::CreateImm(imod));
1917 Inst.addOperand(MCOperand::CreateImm(iflags));
James Molloydb4ce602011-09-01 18:02:14 +00001918 if (mode) S = MCDisassembler::SoftFail;
Owen Anderson9b7bd152011-08-23 17:45:18 +00001919 } else if (!imod && M) {
1920 Inst.setOpcode(ARM::t2CPS1p);
1921 Inst.addOperand(MCOperand::CreateImm(mode));
James Molloydb4ce602011-09-01 18:02:14 +00001922 if (iflags) S = MCDisassembler::SoftFail;
Owen Anderson9b7bd152011-08-23 17:45:18 +00001923 } else {
Quentin Colombeta83d5e92013-04-26 17:54:54 +00001924 // imod == '00' && M == '0' --> this is a HINT instruction
1925 int imm = fieldFromInstruction(Insn, 0, 8);
1926 // HINT are defined only for immediate in [0..4]
1927 if(imm > 4) return MCDisassembler::Fail;
1928 Inst.setOpcode(ARM::t2HINT);
1929 Inst.addOperand(MCOperand::CreateImm(imm));
Owen Anderson9b7bd152011-08-23 17:45:18 +00001930 }
1931
1932 return S;
1933}
1934
Craig Topperf6e7e122012-03-27 07:21:54 +00001935static DecodeStatus DecodeT2MOVTWInstruction(MCInst &Inst, unsigned Insn,
Kevin Enderby5dcda642011-10-04 22:44:48 +00001936 uint64_t Address, const void *Decoder) {
1937 DecodeStatus S = MCDisassembler::Success;
1938
Jim Grosbachecaef492012-08-14 19:06:05 +00001939 unsigned Rd = fieldFromInstruction(Insn, 8, 4);
Kevin Enderby5dcda642011-10-04 22:44:48 +00001940 unsigned imm = 0;
1941
Jim Grosbachecaef492012-08-14 19:06:05 +00001942 imm |= (fieldFromInstruction(Insn, 0, 8) << 0);
1943 imm |= (fieldFromInstruction(Insn, 12, 3) << 8);
1944 imm |= (fieldFromInstruction(Insn, 16, 4) << 12);
1945 imm |= (fieldFromInstruction(Insn, 26, 1) << 11);
Kevin Enderby5dcda642011-10-04 22:44:48 +00001946
1947 if (Inst.getOpcode() == ARM::t2MOVTi16)
1948 if (!Check(S, DecoderGPRRegisterClass(Inst, Rd, Address, Decoder)))
1949 return MCDisassembler::Fail;
1950 if (!Check(S, DecoderGPRRegisterClass(Inst, Rd, Address, Decoder)))
1951 return MCDisassembler::Fail;
1952
1953 if (!tryAddingSymbolicOperand(Address, imm, false, 4, Inst, Decoder))
1954 Inst.addOperand(MCOperand::CreateImm(imm));
1955
1956 return S;
1957}
1958
Craig Topperf6e7e122012-03-27 07:21:54 +00001959static DecodeStatus DecodeArmMOVTWInstruction(MCInst &Inst, unsigned Insn,
Kevin Enderby5dcda642011-10-04 22:44:48 +00001960 uint64_t Address, const void *Decoder) {
1961 DecodeStatus S = MCDisassembler::Success;
1962
Jim Grosbachecaef492012-08-14 19:06:05 +00001963 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
1964 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Kevin Enderby5dcda642011-10-04 22:44:48 +00001965 unsigned imm = 0;
1966
Jim Grosbachecaef492012-08-14 19:06:05 +00001967 imm |= (fieldFromInstruction(Insn, 0, 12) << 0);
1968 imm |= (fieldFromInstruction(Insn, 16, 4) << 12);
Kevin Enderby5dcda642011-10-04 22:44:48 +00001969
1970 if (Inst.getOpcode() == ARM::MOVTi16)
Tim Northovera155ab22013-04-19 09:58:09 +00001971 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
Kevin Enderby5dcda642011-10-04 22:44:48 +00001972 return MCDisassembler::Fail;
Tim Northovera155ab22013-04-19 09:58:09 +00001973
1974 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
Kevin Enderby5dcda642011-10-04 22:44:48 +00001975 return MCDisassembler::Fail;
1976
1977 if (!tryAddingSymbolicOperand(Address, imm, false, 4, Inst, Decoder))
1978 Inst.addOperand(MCOperand::CreateImm(imm));
1979
1980 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
1981 return MCDisassembler::Fail;
1982
1983 return S;
1984}
Owen Anderson9b7bd152011-08-23 17:45:18 +00001985
Craig Topperf6e7e122012-03-27 07:21:54 +00001986static DecodeStatus DecodeSMLAInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00001987 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001988 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001989
Jim Grosbachecaef492012-08-14 19:06:05 +00001990 unsigned Rd = fieldFromInstruction(Insn, 16, 4);
1991 unsigned Rn = fieldFromInstruction(Insn, 0, 4);
1992 unsigned Rm = fieldFromInstruction(Insn, 8, 4);
1993 unsigned Ra = fieldFromInstruction(Insn, 12, 4);
1994 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00001995
1996 if (pred == 0xF)
1997 return DecodeCPSInstruction(Inst, Insn, Address, Decoder);
1998
Owen Anderson03aadae2011-09-01 23:23:50 +00001999 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
2000 return MCDisassembler::Fail;
2001 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
2002 return MCDisassembler::Fail;
2003 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
2004 return MCDisassembler::Fail;
2005 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Ra, Address, Decoder)))
2006 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002007
Owen Anderson03aadae2011-09-01 23:23:50 +00002008 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
2009 return MCDisassembler::Fail;
Owen Anderson2f7aa732011-08-11 22:05:38 +00002010
Owen Andersona4043c42011-08-17 17:44:15 +00002011 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002012}
2013
Craig Topperf6e7e122012-03-27 07:21:54 +00002014static DecodeStatus DecodeAddrModeImm12Operand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002015 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002016 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002017
Jim Grosbachecaef492012-08-14 19:06:05 +00002018 unsigned add = fieldFromInstruction(Val, 12, 1);
2019 unsigned imm = fieldFromInstruction(Val, 0, 12);
2020 unsigned Rn = fieldFromInstruction(Val, 13, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00002021
Owen Anderson03aadae2011-09-01 23:23:50 +00002022 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2023 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002024
2025 if (!add) imm *= -1;
2026 if (imm == 0 && !add) imm = INT32_MIN;
2027 Inst.addOperand(MCOperand::CreateImm(imm));
Kevin Enderby5dcda642011-10-04 22:44:48 +00002028 if (Rn == 15)
2029 tryAddingPcLoadReferenceComment(Address, Address + imm + 8, Decoder);
Owen Andersone0152a72011-08-09 20:55:18 +00002030
Owen Andersona4043c42011-08-17 17:44:15 +00002031 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002032}
2033
Craig Topperf6e7e122012-03-27 07:21:54 +00002034static DecodeStatus DecodeAddrMode5Operand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002035 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002036 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002037
Jim Grosbachecaef492012-08-14 19:06:05 +00002038 unsigned Rn = fieldFromInstruction(Val, 9, 4);
2039 unsigned U = fieldFromInstruction(Val, 8, 1);
2040 unsigned imm = fieldFromInstruction(Val, 0, 8);
Owen Andersone0152a72011-08-09 20:55:18 +00002041
Owen Anderson03aadae2011-09-01 23:23:50 +00002042 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2043 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002044
2045 if (U)
2046 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add, imm)));
2047 else
2048 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub, imm)));
2049
Owen Andersona4043c42011-08-17 17:44:15 +00002050 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002051}
2052
Craig Topperf6e7e122012-03-27 07:21:54 +00002053static DecodeStatus DecodeAddrMode7Operand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002054 uint64_t Address, const void *Decoder) {
2055 return DecodeGPRRegisterClass(Inst, Val, Address, Decoder);
2056}
2057
Owen Anderson03aadae2011-09-01 23:23:50 +00002058static DecodeStatus
Kevin Enderby40d4e472012-04-12 23:13:34 +00002059DecodeT2BInstruction(MCInst &Inst, unsigned Insn,
2060 uint64_t Address, const void *Decoder) {
Kevin Enderby6fd96242012-10-29 23:27:20 +00002061 DecodeStatus Status = MCDisassembler::Success;
2062
2063 // Note the J1 and J2 values are from the encoded instruction. So here
2064 // change them to I1 and I2 values via as documented:
2065 // I1 = NOT(J1 EOR S);
2066 // I2 = NOT(J2 EOR S);
2067 // and build the imm32 with one trailing zero as documented:
2068 // imm32 = SignExtend(S:I1:I2:imm10:imm11:'0', 32);
2069 unsigned S = fieldFromInstruction(Insn, 26, 1);
2070 unsigned J1 = fieldFromInstruction(Insn, 13, 1);
2071 unsigned J2 = fieldFromInstruction(Insn, 11, 1);
2072 unsigned I1 = !(J1 ^ S);
2073 unsigned I2 = !(J2 ^ S);
2074 unsigned imm10 = fieldFromInstruction(Insn, 16, 10);
2075 unsigned imm11 = fieldFromInstruction(Insn, 0, 11);
2076 unsigned tmp = (S << 23) | (I1 << 22) | (I2 << 21) | (imm10 << 11) | imm11;
2077 int imm32 = SignExtend32<24>(tmp << 1);
2078 if (!tryAddingSymbolicOperand(Address, Address + imm32 + 4,
Kevin Enderby40d4e472012-04-12 23:13:34 +00002079 true, 4, Inst, Decoder))
Kevin Enderby6fd96242012-10-29 23:27:20 +00002080 Inst.addOperand(MCOperand::CreateImm(imm32));
2081
2082 return Status;
Kevin Enderby40d4e472012-04-12 23:13:34 +00002083}
2084
2085static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00002086DecodeBranchImmInstruction(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00002087 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002088 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002089
Jim Grosbachecaef492012-08-14 19:06:05 +00002090 unsigned pred = fieldFromInstruction(Insn, 28, 4);
2091 unsigned imm = fieldFromInstruction(Insn, 0, 24) << 2;
Owen Andersone0152a72011-08-09 20:55:18 +00002092
2093 if (pred == 0xF) {
2094 Inst.setOpcode(ARM::BLXi);
Jim Grosbachecaef492012-08-14 19:06:05 +00002095 imm |= fieldFromInstruction(Insn, 24, 1) << 1;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00002096 if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<26>(imm) + 8,
2097 true, 4, Inst, Decoder))
Benjamin Kramer406dc172011-08-09 22:02:50 +00002098 Inst.addOperand(MCOperand::CreateImm(SignExtend32<26>(imm)));
Owen Andersona4043c42011-08-17 17:44:15 +00002099 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002100 }
2101
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00002102 if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<26>(imm) + 8,
2103 true, 4, Inst, Decoder))
Kevin Enderby5dcda642011-10-04 22:44:48 +00002104 Inst.addOperand(MCOperand::CreateImm(SignExtend32<26>(imm)));
Owen Anderson03aadae2011-09-01 23:23:50 +00002105 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
2106 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002107
Owen Andersona4043c42011-08-17 17:44:15 +00002108 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002109}
2110
2111
Craig Topperf6e7e122012-03-27 07:21:54 +00002112static DecodeStatus DecodeAddrMode6Operand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002113 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002114 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002115
Jim Grosbachecaef492012-08-14 19:06:05 +00002116 unsigned Rm = fieldFromInstruction(Val, 0, 4);
2117 unsigned align = fieldFromInstruction(Val, 4, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00002118
Owen Anderson03aadae2011-09-01 23:23:50 +00002119 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
2120 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002121 if (!align)
2122 Inst.addOperand(MCOperand::CreateImm(0));
2123 else
2124 Inst.addOperand(MCOperand::CreateImm(4 << align));
2125
Owen Andersona4043c42011-08-17 17:44:15 +00002126 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002127}
2128
Craig Topperf6e7e122012-03-27 07:21:54 +00002129static DecodeStatus DecodeVLDInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002130 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002131 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002132
Jim Grosbachecaef492012-08-14 19:06:05 +00002133 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2134 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2135 unsigned wb = fieldFromInstruction(Insn, 16, 4);
2136 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
2137 Rn |= fieldFromInstruction(Insn, 4, 2) << 4;
2138 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00002139
2140 // First output register
Jim Grosbachc988e0c2012-03-05 19:33:30 +00002141 switch (Inst.getOpcode()) {
Jim Grosbach13a292c2012-03-06 22:01:44 +00002142 case ARM::VLD1q16: case ARM::VLD1q32: case ARM::VLD1q64: case ARM::VLD1q8:
2143 case ARM::VLD1q16wb_fixed: case ARM::VLD1q16wb_register:
2144 case ARM::VLD1q32wb_fixed: case ARM::VLD1q32wb_register:
2145 case ARM::VLD1q64wb_fixed: case ARM::VLD1q64wb_register:
2146 case ARM::VLD1q8wb_fixed: case ARM::VLD1q8wb_register:
2147 case ARM::VLD2d16: case ARM::VLD2d32: case ARM::VLD2d8:
2148 case ARM::VLD2d16wb_fixed: case ARM::VLD2d16wb_register:
2149 case ARM::VLD2d32wb_fixed: case ARM::VLD2d32wb_register:
2150 case ARM::VLD2d8wb_fixed: case ARM::VLD2d8wb_register:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00002151 if (!Check(S, DecodeDPairRegisterClass(Inst, Rd, Address, Decoder)))
2152 return MCDisassembler::Fail;
2153 break;
Jim Grosbache5307f92012-03-05 21:43:40 +00002154 case ARM::VLD2b16:
2155 case ARM::VLD2b32:
2156 case ARM::VLD2b8:
2157 case ARM::VLD2b16wb_fixed:
2158 case ARM::VLD2b16wb_register:
2159 case ARM::VLD2b32wb_fixed:
2160 case ARM::VLD2b32wb_register:
2161 case ARM::VLD2b8wb_fixed:
2162 case ARM::VLD2b8wb_register:
2163 if (!Check(S, DecodeDPairSpacedRegisterClass(Inst, Rd, Address, Decoder)))
2164 return MCDisassembler::Fail;
2165 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00002166 default:
2167 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2168 return MCDisassembler::Fail;
2169 }
Owen Andersone0152a72011-08-09 20:55:18 +00002170
2171 // Second output register
2172 switch (Inst.getOpcode()) {
Owen Andersone0152a72011-08-09 20:55:18 +00002173 case ARM::VLD3d8:
2174 case ARM::VLD3d16:
2175 case ARM::VLD3d32:
2176 case ARM::VLD3d8_UPD:
2177 case ARM::VLD3d16_UPD:
2178 case ARM::VLD3d32_UPD:
2179 case ARM::VLD4d8:
2180 case ARM::VLD4d16:
2181 case ARM::VLD4d32:
2182 case ARM::VLD4d8_UPD:
2183 case ARM::VLD4d16_UPD:
2184 case ARM::VLD4d32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002185 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder)))
2186 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002187 break;
Owen Andersone0152a72011-08-09 20:55:18 +00002188 case ARM::VLD3q8:
2189 case ARM::VLD3q16:
2190 case ARM::VLD3q32:
2191 case ARM::VLD3q8_UPD:
2192 case ARM::VLD3q16_UPD:
2193 case ARM::VLD3q32_UPD:
2194 case ARM::VLD4q8:
2195 case ARM::VLD4q16:
2196 case ARM::VLD4q32:
2197 case ARM::VLD4q8_UPD:
2198 case ARM::VLD4q16_UPD:
2199 case ARM::VLD4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002200 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder)))
2201 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002202 default:
2203 break;
2204 }
2205
2206 // Third output register
2207 switch(Inst.getOpcode()) {
Owen Andersone0152a72011-08-09 20:55:18 +00002208 case ARM::VLD3d8:
2209 case ARM::VLD3d16:
2210 case ARM::VLD3d32:
2211 case ARM::VLD3d8_UPD:
2212 case ARM::VLD3d16_UPD:
2213 case ARM::VLD3d32_UPD:
2214 case ARM::VLD4d8:
2215 case ARM::VLD4d16:
2216 case ARM::VLD4d32:
2217 case ARM::VLD4d8_UPD:
2218 case ARM::VLD4d16_UPD:
2219 case ARM::VLD4d32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002220 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder)))
2221 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002222 break;
2223 case ARM::VLD3q8:
2224 case ARM::VLD3q16:
2225 case ARM::VLD3q32:
2226 case ARM::VLD3q8_UPD:
2227 case ARM::VLD3q16_UPD:
2228 case ARM::VLD3q32_UPD:
2229 case ARM::VLD4q8:
2230 case ARM::VLD4q16:
2231 case ARM::VLD4q32:
2232 case ARM::VLD4q8_UPD:
2233 case ARM::VLD4q16_UPD:
2234 case ARM::VLD4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002235 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+4)%32, Address, Decoder)))
2236 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002237 break;
2238 default:
2239 break;
2240 }
2241
2242 // Fourth output register
2243 switch (Inst.getOpcode()) {
Owen Andersone0152a72011-08-09 20:55:18 +00002244 case ARM::VLD4d8:
2245 case ARM::VLD4d16:
2246 case ARM::VLD4d32:
2247 case ARM::VLD4d8_UPD:
2248 case ARM::VLD4d16_UPD:
2249 case ARM::VLD4d32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002250 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+3)%32, Address, Decoder)))
2251 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002252 break;
2253 case ARM::VLD4q8:
2254 case ARM::VLD4q16:
2255 case ARM::VLD4q32:
2256 case ARM::VLD4q8_UPD:
2257 case ARM::VLD4q16_UPD:
2258 case ARM::VLD4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002259 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+6)%32, Address, Decoder)))
2260 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002261 break;
2262 default:
2263 break;
2264 }
2265
2266 // Writeback operand
2267 switch (Inst.getOpcode()) {
Jim Grosbach2098cb12011-10-24 21:45:13 +00002268 case ARM::VLD1d8wb_fixed:
2269 case ARM::VLD1d16wb_fixed:
2270 case ARM::VLD1d32wb_fixed:
2271 case ARM::VLD1d64wb_fixed:
2272 case ARM::VLD1d8wb_register:
2273 case ARM::VLD1d16wb_register:
2274 case ARM::VLD1d32wb_register:
2275 case ARM::VLD1d64wb_register:
2276 case ARM::VLD1q8wb_fixed:
2277 case ARM::VLD1q16wb_fixed:
2278 case ARM::VLD1q32wb_fixed:
2279 case ARM::VLD1q64wb_fixed:
2280 case ARM::VLD1q8wb_register:
2281 case ARM::VLD1q16wb_register:
2282 case ARM::VLD1q32wb_register:
2283 case ARM::VLD1q64wb_register:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00002284 case ARM::VLD1d8Twb_fixed:
2285 case ARM::VLD1d8Twb_register:
2286 case ARM::VLD1d16Twb_fixed:
2287 case ARM::VLD1d16Twb_register:
2288 case ARM::VLD1d32Twb_fixed:
2289 case ARM::VLD1d32Twb_register:
2290 case ARM::VLD1d64Twb_fixed:
2291 case ARM::VLD1d64Twb_register:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00002292 case ARM::VLD1d8Qwb_fixed:
2293 case ARM::VLD1d8Qwb_register:
2294 case ARM::VLD1d16Qwb_fixed:
2295 case ARM::VLD1d16Qwb_register:
2296 case ARM::VLD1d32Qwb_fixed:
2297 case ARM::VLD1d32Qwb_register:
2298 case ARM::VLD1d64Qwb_fixed:
2299 case ARM::VLD1d64Qwb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00002300 case ARM::VLD2d8wb_fixed:
2301 case ARM::VLD2d16wb_fixed:
2302 case ARM::VLD2d32wb_fixed:
2303 case ARM::VLD2q8wb_fixed:
2304 case ARM::VLD2q16wb_fixed:
2305 case ARM::VLD2q32wb_fixed:
2306 case ARM::VLD2d8wb_register:
2307 case ARM::VLD2d16wb_register:
2308 case ARM::VLD2d32wb_register:
2309 case ARM::VLD2q8wb_register:
2310 case ARM::VLD2q16wb_register:
2311 case ARM::VLD2q32wb_register:
2312 case ARM::VLD2b8wb_fixed:
2313 case ARM::VLD2b16wb_fixed:
2314 case ARM::VLD2b32wb_fixed:
2315 case ARM::VLD2b8wb_register:
2316 case ARM::VLD2b16wb_register:
2317 case ARM::VLD2b32wb_register:
Kevin Enderbyd2980cd2012-04-11 00:25:40 +00002318 Inst.addOperand(MCOperand::CreateImm(0));
2319 break;
Owen Andersone0152a72011-08-09 20:55:18 +00002320 case ARM::VLD3d8_UPD:
2321 case ARM::VLD3d16_UPD:
2322 case ARM::VLD3d32_UPD:
2323 case ARM::VLD3q8_UPD:
2324 case ARM::VLD3q16_UPD:
2325 case ARM::VLD3q32_UPD:
2326 case ARM::VLD4d8_UPD:
2327 case ARM::VLD4d16_UPD:
2328 case ARM::VLD4d32_UPD:
2329 case ARM::VLD4q8_UPD:
2330 case ARM::VLD4q16_UPD:
2331 case ARM::VLD4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002332 if (!Check(S, DecodeGPRRegisterClass(Inst, wb, Address, Decoder)))
2333 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002334 break;
2335 default:
2336 break;
2337 }
2338
2339 // AddrMode6 Base (register+alignment)
Owen Anderson03aadae2011-09-01 23:23:50 +00002340 if (!Check(S, DecodeAddrMode6Operand(Inst, Rn, Address, Decoder)))
2341 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002342
2343 // AddrMode6 Offset (register)
Jim Grosbach2098cb12011-10-24 21:45:13 +00002344 switch (Inst.getOpcode()) {
2345 default:
2346 // The below have been updated to have explicit am6offset split
2347 // between fixed and register offset. For those instructions not
2348 // yet updated, we need to add an additional reg0 operand for the
2349 // fixed variant.
2350 //
2351 // The fixed offset encodes as Rm == 0xd, so we check for that.
2352 if (Rm == 0xd) {
2353 Inst.addOperand(MCOperand::CreateReg(0));
2354 break;
2355 }
2356 // Fall through to handle the register offset variant.
2357 case ARM::VLD1d8wb_fixed:
2358 case ARM::VLD1d16wb_fixed:
2359 case ARM::VLD1d32wb_fixed:
2360 case ARM::VLD1d64wb_fixed:
Owen Anderson8a6ebd02011-10-27 22:53:10 +00002361 case ARM::VLD1d8Twb_fixed:
2362 case ARM::VLD1d16Twb_fixed:
2363 case ARM::VLD1d32Twb_fixed:
2364 case ARM::VLD1d64Twb_fixed:
Owen Anderson40703f42011-10-31 17:17:32 +00002365 case ARM::VLD1d8Qwb_fixed:
2366 case ARM::VLD1d16Qwb_fixed:
2367 case ARM::VLD1d32Qwb_fixed:
2368 case ARM::VLD1d64Qwb_fixed:
Jim Grosbach2098cb12011-10-24 21:45:13 +00002369 case ARM::VLD1d8wb_register:
2370 case ARM::VLD1d16wb_register:
2371 case ARM::VLD1d32wb_register:
2372 case ARM::VLD1d64wb_register:
2373 case ARM::VLD1q8wb_fixed:
2374 case ARM::VLD1q16wb_fixed:
2375 case ARM::VLD1q32wb_fixed:
2376 case ARM::VLD1q64wb_fixed:
2377 case ARM::VLD1q8wb_register:
2378 case ARM::VLD1q16wb_register:
2379 case ARM::VLD1q32wb_register:
2380 case ARM::VLD1q64wb_register:
2381 // The fixed offset post-increment encodes Rm == 0xd. The no-writeback
2382 // variant encodes Rm == 0xf. Anything else is a register offset post-
2383 // increment and we need to add the register operand to the instruction.
2384 if (Rm != 0xD && Rm != 0xF &&
2385 !Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
Owen Anderson03aadae2011-09-01 23:23:50 +00002386 return MCDisassembler::Fail;
Jim Grosbach2098cb12011-10-24 21:45:13 +00002387 break;
Kevin Enderbyd2980cd2012-04-11 00:25:40 +00002388 case ARM::VLD2d8wb_fixed:
2389 case ARM::VLD2d16wb_fixed:
2390 case ARM::VLD2d32wb_fixed:
2391 case ARM::VLD2b8wb_fixed:
2392 case ARM::VLD2b16wb_fixed:
2393 case ARM::VLD2b32wb_fixed:
2394 case ARM::VLD2q8wb_fixed:
2395 case ARM::VLD2q16wb_fixed:
2396 case ARM::VLD2q32wb_fixed:
2397 break;
Owen Andersoned253852011-08-11 18:24:51 +00002398 }
Owen Andersone0152a72011-08-09 20:55:18 +00002399
Owen Andersona4043c42011-08-17 17:44:15 +00002400 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002401}
2402
Mihai Popaf41e3f52013-05-20 14:57:05 +00002403static DecodeStatus DecodeVST1Instruction(MCInst& Inst, unsigned Insn,
2404 uint64_t Addr, const void* Decoder) {
2405 unsigned type = fieldFromInstruction(Insn, 8, 4);
2406 unsigned align = fieldFromInstruction(Insn, 4, 2);
2407 if(type == 7 && (align & 2)) return MCDisassembler::Fail;
2408 if(type == 10 && align == 3) return MCDisassembler::Fail;
2409 if(type == 6 && (align & 2)) return MCDisassembler::Fail;
2410
2411 return DecodeVSTInstruction(Inst, Insn, Addr, Decoder);
2412}
2413
2414static DecodeStatus DecodeVST2Instruction(MCInst& Inst, unsigned Insn,
2415 uint64_t Addr, const void* Decoder) {
2416 unsigned size = fieldFromInstruction(Insn, 6, 2);
2417 if(size == 3) return MCDisassembler::Fail;
2418
2419 unsigned type = fieldFromInstruction(Insn, 8, 4);
2420 unsigned align = fieldFromInstruction(Insn, 4, 2);
2421 if(type == 8 && align == 3) return MCDisassembler::Fail;
2422 if(type == 9 && align == 3) return MCDisassembler::Fail;
2423
2424 return DecodeVSTInstruction(Inst, Insn, Addr, Decoder);
2425}
2426
2427static DecodeStatus DecodeVST3Instruction(MCInst& Inst, unsigned Insn,
2428 uint64_t Addr, const void* Decoder) {
2429 unsigned size = fieldFromInstruction(Insn, 6, 2);
2430 if(size == 3) return MCDisassembler::Fail;
2431
2432 unsigned align = fieldFromInstruction(Insn, 4, 2);
2433 if(align & 2) return MCDisassembler::Fail;
2434
2435 return DecodeVSTInstruction(Inst, Insn, Addr, Decoder);
2436}
2437
2438static DecodeStatus DecodeVST4Instruction(MCInst& Inst, unsigned Insn,
2439 uint64_t Addr, const void* Decoder) {
2440 unsigned size = fieldFromInstruction(Insn, 6, 2);
2441 if(size == 3) return MCDisassembler::Fail;
2442
2443 return DecodeVSTInstruction(Inst, Insn, Addr, Decoder);
2444}
2445
Craig Topperf6e7e122012-03-27 07:21:54 +00002446static DecodeStatus DecodeVSTInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002447 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002448 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002449
Jim Grosbachecaef492012-08-14 19:06:05 +00002450 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2451 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2452 unsigned wb = fieldFromInstruction(Insn, 16, 4);
2453 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
2454 Rn |= fieldFromInstruction(Insn, 4, 2) << 4;
2455 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00002456
2457 // Writeback Operand
2458 switch (Inst.getOpcode()) {
Jim Grosbach05df4602011-10-31 21:50:31 +00002459 case ARM::VST1d8wb_fixed:
2460 case ARM::VST1d16wb_fixed:
2461 case ARM::VST1d32wb_fixed:
2462 case ARM::VST1d64wb_fixed:
2463 case ARM::VST1d8wb_register:
2464 case ARM::VST1d16wb_register:
2465 case ARM::VST1d32wb_register:
2466 case ARM::VST1d64wb_register:
2467 case ARM::VST1q8wb_fixed:
2468 case ARM::VST1q16wb_fixed:
2469 case ARM::VST1q32wb_fixed:
2470 case ARM::VST1q64wb_fixed:
2471 case ARM::VST1q8wb_register:
2472 case ARM::VST1q16wb_register:
2473 case ARM::VST1q32wb_register:
2474 case ARM::VST1q64wb_register:
Jim Grosbach98d032f2011-11-29 22:38:04 +00002475 case ARM::VST1d8Twb_fixed:
2476 case ARM::VST1d16Twb_fixed:
2477 case ARM::VST1d32Twb_fixed:
2478 case ARM::VST1d64Twb_fixed:
2479 case ARM::VST1d8Twb_register:
2480 case ARM::VST1d16Twb_register:
2481 case ARM::VST1d32Twb_register:
2482 case ARM::VST1d64Twb_register:
Jim Grosbach5ee209c2011-11-29 22:58:48 +00002483 case ARM::VST1d8Qwb_fixed:
2484 case ARM::VST1d16Qwb_fixed:
2485 case ARM::VST1d32Qwb_fixed:
2486 case ARM::VST1d64Qwb_fixed:
2487 case ARM::VST1d8Qwb_register:
2488 case ARM::VST1d16Qwb_register:
2489 case ARM::VST1d32Qwb_register:
2490 case ARM::VST1d64Qwb_register:
Jim Grosbach88ac7612011-12-14 21:32:11 +00002491 case ARM::VST2d8wb_fixed:
2492 case ARM::VST2d16wb_fixed:
2493 case ARM::VST2d32wb_fixed:
2494 case ARM::VST2d8wb_register:
2495 case ARM::VST2d16wb_register:
2496 case ARM::VST2d32wb_register:
2497 case ARM::VST2q8wb_fixed:
2498 case ARM::VST2q16wb_fixed:
2499 case ARM::VST2q32wb_fixed:
2500 case ARM::VST2q8wb_register:
2501 case ARM::VST2q16wb_register:
2502 case ARM::VST2q32wb_register:
2503 case ARM::VST2b8wb_fixed:
2504 case ARM::VST2b16wb_fixed:
2505 case ARM::VST2b32wb_fixed:
2506 case ARM::VST2b8wb_register:
2507 case ARM::VST2b16wb_register:
2508 case ARM::VST2b32wb_register:
Kevin Enderby72f18bb2012-04-11 22:40:17 +00002509 if (Rm == 0xF)
2510 return MCDisassembler::Fail;
Kevin Enderby7e7d5ee2012-03-21 20:54:32 +00002511 Inst.addOperand(MCOperand::CreateImm(0));
2512 break;
Owen Andersone0152a72011-08-09 20:55:18 +00002513 case ARM::VST3d8_UPD:
2514 case ARM::VST3d16_UPD:
2515 case ARM::VST3d32_UPD:
2516 case ARM::VST3q8_UPD:
2517 case ARM::VST3q16_UPD:
2518 case ARM::VST3q32_UPD:
2519 case ARM::VST4d8_UPD:
2520 case ARM::VST4d16_UPD:
2521 case ARM::VST4d32_UPD:
2522 case ARM::VST4q8_UPD:
2523 case ARM::VST4q16_UPD:
2524 case ARM::VST4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002525 if (!Check(S, DecodeGPRRegisterClass(Inst, wb, Address, Decoder)))
2526 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002527 break;
2528 default:
2529 break;
2530 }
2531
2532 // AddrMode6 Base (register+alignment)
Owen Anderson03aadae2011-09-01 23:23:50 +00002533 if (!Check(S, DecodeAddrMode6Operand(Inst, Rn, Address, Decoder)))
2534 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002535
2536 // AddrMode6 Offset (register)
Owen Anderson69e54a72011-11-01 22:18:13 +00002537 switch (Inst.getOpcode()) {
2538 default:
2539 if (Rm == 0xD)
2540 Inst.addOperand(MCOperand::CreateReg(0));
2541 else if (Rm != 0xF) {
2542 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
2543 return MCDisassembler::Fail;
2544 }
2545 break;
2546 case ARM::VST1d8wb_fixed:
2547 case ARM::VST1d16wb_fixed:
2548 case ARM::VST1d32wb_fixed:
2549 case ARM::VST1d64wb_fixed:
2550 case ARM::VST1q8wb_fixed:
2551 case ARM::VST1q16wb_fixed:
2552 case ARM::VST1q32wb_fixed:
2553 case ARM::VST1q64wb_fixed:
Kevin Enderby7e7d5ee2012-03-21 20:54:32 +00002554 case ARM::VST1d8Twb_fixed:
2555 case ARM::VST1d16Twb_fixed:
2556 case ARM::VST1d32Twb_fixed:
2557 case ARM::VST1d64Twb_fixed:
2558 case ARM::VST1d8Qwb_fixed:
2559 case ARM::VST1d16Qwb_fixed:
2560 case ARM::VST1d32Qwb_fixed:
2561 case ARM::VST1d64Qwb_fixed:
2562 case ARM::VST2d8wb_fixed:
2563 case ARM::VST2d16wb_fixed:
2564 case ARM::VST2d32wb_fixed:
2565 case ARM::VST2q8wb_fixed:
2566 case ARM::VST2q16wb_fixed:
2567 case ARM::VST2q32wb_fixed:
2568 case ARM::VST2b8wb_fixed:
2569 case ARM::VST2b16wb_fixed:
2570 case ARM::VST2b32wb_fixed:
Owen Anderson69e54a72011-11-01 22:18:13 +00002571 break;
Owen Andersoned253852011-08-11 18:24:51 +00002572 }
Owen Andersone0152a72011-08-09 20:55:18 +00002573
Owen Anderson69e54a72011-11-01 22:18:13 +00002574
Owen Andersone0152a72011-08-09 20:55:18 +00002575 // First input register
Jim Grosbachc988e0c2012-03-05 19:33:30 +00002576 switch (Inst.getOpcode()) {
2577 case ARM::VST1q16:
2578 case ARM::VST1q32:
2579 case ARM::VST1q64:
2580 case ARM::VST1q8:
2581 case ARM::VST1q16wb_fixed:
2582 case ARM::VST1q16wb_register:
2583 case ARM::VST1q32wb_fixed:
2584 case ARM::VST1q32wb_register:
2585 case ARM::VST1q64wb_fixed:
2586 case ARM::VST1q64wb_register:
2587 case ARM::VST1q8wb_fixed:
2588 case ARM::VST1q8wb_register:
2589 case ARM::VST2d16:
2590 case ARM::VST2d32:
2591 case ARM::VST2d8:
2592 case ARM::VST2d16wb_fixed:
2593 case ARM::VST2d16wb_register:
2594 case ARM::VST2d32wb_fixed:
2595 case ARM::VST2d32wb_register:
2596 case ARM::VST2d8wb_fixed:
2597 case ARM::VST2d8wb_register:
2598 if (!Check(S, DecodeDPairRegisterClass(Inst, Rd, Address, Decoder)))
2599 return MCDisassembler::Fail;
2600 break;
Jim Grosbache5307f92012-03-05 21:43:40 +00002601 case ARM::VST2b16:
2602 case ARM::VST2b32:
2603 case ARM::VST2b8:
2604 case ARM::VST2b16wb_fixed:
2605 case ARM::VST2b16wb_register:
2606 case ARM::VST2b32wb_fixed:
2607 case ARM::VST2b32wb_register:
2608 case ARM::VST2b8wb_fixed:
2609 case ARM::VST2b8wb_register:
2610 if (!Check(S, DecodeDPairSpacedRegisterClass(Inst, Rd, Address, Decoder)))
2611 return MCDisassembler::Fail;
2612 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00002613 default:
2614 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2615 return MCDisassembler::Fail;
2616 }
Owen Andersone0152a72011-08-09 20:55:18 +00002617
2618 // Second input register
2619 switch (Inst.getOpcode()) {
Owen Andersone0152a72011-08-09 20:55:18 +00002620 case ARM::VST3d8:
2621 case ARM::VST3d16:
2622 case ARM::VST3d32:
2623 case ARM::VST3d8_UPD:
2624 case ARM::VST3d16_UPD:
2625 case ARM::VST3d32_UPD:
2626 case ARM::VST4d8:
2627 case ARM::VST4d16:
2628 case ARM::VST4d32:
2629 case ARM::VST4d8_UPD:
2630 case ARM::VST4d16_UPD:
2631 case ARM::VST4d32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002632 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder)))
2633 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002634 break;
Owen Andersone0152a72011-08-09 20:55:18 +00002635 case ARM::VST3q8:
2636 case ARM::VST3q16:
2637 case ARM::VST3q32:
2638 case ARM::VST3q8_UPD:
2639 case ARM::VST3q16_UPD:
2640 case ARM::VST3q32_UPD:
2641 case ARM::VST4q8:
2642 case ARM::VST4q16:
2643 case ARM::VST4q32:
2644 case ARM::VST4q8_UPD:
2645 case ARM::VST4q16_UPD:
2646 case ARM::VST4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002647 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder)))
2648 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002649 break;
2650 default:
2651 break;
2652 }
2653
2654 // Third input register
2655 switch (Inst.getOpcode()) {
Owen Andersone0152a72011-08-09 20:55:18 +00002656 case ARM::VST3d8:
2657 case ARM::VST3d16:
2658 case ARM::VST3d32:
2659 case ARM::VST3d8_UPD:
2660 case ARM::VST3d16_UPD:
2661 case ARM::VST3d32_UPD:
2662 case ARM::VST4d8:
2663 case ARM::VST4d16:
2664 case ARM::VST4d32:
2665 case ARM::VST4d8_UPD:
2666 case ARM::VST4d16_UPD:
2667 case ARM::VST4d32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002668 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder)))
2669 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002670 break;
2671 case ARM::VST3q8:
2672 case ARM::VST3q16:
2673 case ARM::VST3q32:
2674 case ARM::VST3q8_UPD:
2675 case ARM::VST3q16_UPD:
2676 case ARM::VST3q32_UPD:
2677 case ARM::VST4q8:
2678 case ARM::VST4q16:
2679 case ARM::VST4q32:
2680 case ARM::VST4q8_UPD:
2681 case ARM::VST4q16_UPD:
2682 case ARM::VST4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002683 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+4)%32, Address, Decoder)))
2684 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002685 break;
2686 default:
2687 break;
2688 }
2689
2690 // Fourth input register
2691 switch (Inst.getOpcode()) {
Owen Andersone0152a72011-08-09 20:55:18 +00002692 case ARM::VST4d8:
2693 case ARM::VST4d16:
2694 case ARM::VST4d32:
2695 case ARM::VST4d8_UPD:
2696 case ARM::VST4d16_UPD:
2697 case ARM::VST4d32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002698 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+3)%32, Address, Decoder)))
2699 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002700 break;
2701 case ARM::VST4q8:
2702 case ARM::VST4q16:
2703 case ARM::VST4q32:
2704 case ARM::VST4q8_UPD:
2705 case ARM::VST4q16_UPD:
2706 case ARM::VST4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002707 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+6)%32, Address, Decoder)))
2708 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002709 break;
2710 default:
2711 break;
2712 }
2713
Owen Andersona4043c42011-08-17 17:44:15 +00002714 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002715}
2716
Craig Topperf6e7e122012-03-27 07:21:54 +00002717static DecodeStatus DecodeVLD1DupInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002718 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002719 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002720
Jim Grosbachecaef492012-08-14 19:06:05 +00002721 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2722 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2723 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
2724 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
2725 unsigned align = fieldFromInstruction(Insn, 4, 1);
2726 unsigned size = fieldFromInstruction(Insn, 6, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00002727
Tim Northover00e071a2012-09-06 15:27:12 +00002728 if (size == 0 && align == 1)
2729 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002730 align *= (1 << size);
2731
Jim Grosbach13a292c2012-03-06 22:01:44 +00002732 switch (Inst.getOpcode()) {
2733 case ARM::VLD1DUPq16: case ARM::VLD1DUPq32: case ARM::VLD1DUPq8:
2734 case ARM::VLD1DUPq16wb_fixed: case ARM::VLD1DUPq16wb_register:
2735 case ARM::VLD1DUPq32wb_fixed: case ARM::VLD1DUPq32wb_register:
2736 case ARM::VLD1DUPq8wb_fixed: case ARM::VLD1DUPq8wb_register:
2737 if (!Check(S, DecodeDPairRegisterClass(Inst, Rd, Address, Decoder)))
2738 return MCDisassembler::Fail;
2739 break;
2740 default:
2741 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2742 return MCDisassembler::Fail;
2743 break;
2744 }
Owen Andersonac92e772011-08-22 18:22:06 +00002745 if (Rm != 0xF) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002746 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2747 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002748 }
Owen Andersone0152a72011-08-09 20:55:18 +00002749
Owen Anderson03aadae2011-09-01 23:23:50 +00002750 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2751 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002752 Inst.addOperand(MCOperand::CreateImm(align));
2753
Jim Grosbacha68c9a82011-11-30 19:35:44 +00002754 // The fixed offset post-increment encodes Rm == 0xd. The no-writeback
2755 // variant encodes Rm == 0xf. Anything else is a register offset post-
2756 // increment and we need to add the register operand to the instruction.
2757 if (Rm != 0xD && Rm != 0xF &&
2758 !Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
2759 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002760
Owen Andersona4043c42011-08-17 17:44:15 +00002761 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002762}
2763
Craig Topperf6e7e122012-03-27 07:21:54 +00002764static DecodeStatus DecodeVLD2DupInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002765 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002766 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002767
Jim Grosbachecaef492012-08-14 19:06:05 +00002768 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2769 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2770 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
2771 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
2772 unsigned align = fieldFromInstruction(Insn, 4, 1);
2773 unsigned size = 1 << fieldFromInstruction(Insn, 6, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00002774 align *= 2*size;
2775
Jim Grosbach13a292c2012-03-06 22:01:44 +00002776 switch (Inst.getOpcode()) {
2777 case ARM::VLD2DUPd16: case ARM::VLD2DUPd32: case ARM::VLD2DUPd8:
2778 case ARM::VLD2DUPd16wb_fixed: case ARM::VLD2DUPd16wb_register:
2779 case ARM::VLD2DUPd32wb_fixed: case ARM::VLD2DUPd32wb_register:
2780 case ARM::VLD2DUPd8wb_fixed: case ARM::VLD2DUPd8wb_register:
2781 if (!Check(S, DecodeDPairRegisterClass(Inst, Rd, Address, Decoder)))
2782 return MCDisassembler::Fail;
2783 break;
Jim Grosbached428bc2012-03-06 23:10:38 +00002784 case ARM::VLD2DUPd16x2: case ARM::VLD2DUPd32x2: case ARM::VLD2DUPd8x2:
2785 case ARM::VLD2DUPd16x2wb_fixed: case ARM::VLD2DUPd16x2wb_register:
2786 case ARM::VLD2DUPd32x2wb_fixed: case ARM::VLD2DUPd32x2wb_register:
2787 case ARM::VLD2DUPd8x2wb_fixed: case ARM::VLD2DUPd8x2wb_register:
2788 if (!Check(S, DecodeDPairSpacedRegisterClass(Inst, Rd, Address, Decoder)))
2789 return MCDisassembler::Fail;
2790 break;
Jim Grosbach13a292c2012-03-06 22:01:44 +00002791 default:
2792 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2793 return MCDisassembler::Fail;
2794 break;
2795 }
Kevin Enderby520eb3b2012-03-06 18:33:12 +00002796
2797 if (Rm != 0xF)
2798 Inst.addOperand(MCOperand::CreateImm(0));
Owen Andersone0152a72011-08-09 20:55:18 +00002799
Owen Anderson03aadae2011-09-01 23:23:50 +00002800 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2801 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002802 Inst.addOperand(MCOperand::CreateImm(align));
2803
Kevin Enderby29ae5382012-04-17 00:49:27 +00002804 if (Rm != 0xD && Rm != 0xF) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002805 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
2806 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002807 }
Owen Andersone0152a72011-08-09 20:55:18 +00002808
Owen Andersona4043c42011-08-17 17:44:15 +00002809 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002810}
2811
Craig Topperf6e7e122012-03-27 07:21:54 +00002812static DecodeStatus DecodeVLD3DupInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002813 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002814 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002815
Jim Grosbachecaef492012-08-14 19:06:05 +00002816 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2817 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2818 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
2819 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
2820 unsigned inc = fieldFromInstruction(Insn, 5, 1) + 1;
Owen Andersone0152a72011-08-09 20:55:18 +00002821
Owen Anderson03aadae2011-09-01 23:23:50 +00002822 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2823 return MCDisassembler::Fail;
2824 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder)))
2825 return MCDisassembler::Fail;
2826 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2*inc)%32, Address, Decoder)))
2827 return MCDisassembler::Fail;
Owen Andersonac92e772011-08-22 18:22:06 +00002828 if (Rm != 0xF) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002829 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2830 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002831 }
Owen Andersone0152a72011-08-09 20:55:18 +00002832
Owen Anderson03aadae2011-09-01 23:23:50 +00002833 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2834 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002835 Inst.addOperand(MCOperand::CreateImm(0));
2836
2837 if (Rm == 0xD)
2838 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersoned253852011-08-11 18:24:51 +00002839 else if (Rm != 0xF) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002840 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
2841 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002842 }
Owen Andersone0152a72011-08-09 20:55:18 +00002843
Owen Andersona4043c42011-08-17 17:44:15 +00002844 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002845}
2846
Craig Topperf6e7e122012-03-27 07:21:54 +00002847static DecodeStatus DecodeVLD4DupInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002848 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002849 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002850
Jim Grosbachecaef492012-08-14 19:06:05 +00002851 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2852 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2853 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
2854 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
2855 unsigned size = fieldFromInstruction(Insn, 6, 2);
2856 unsigned inc = fieldFromInstruction(Insn, 5, 1) + 1;
2857 unsigned align = fieldFromInstruction(Insn, 4, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00002858
2859 if (size == 0x3) {
Tim Northover00e071a2012-09-06 15:27:12 +00002860 if (align == 0)
2861 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002862 size = 4;
2863 align = 16;
2864 } else {
2865 if (size == 2) {
2866 size = 1 << size;
2867 align *= 8;
2868 } else {
2869 size = 1 << size;
2870 align *= 4*size;
2871 }
2872 }
2873
Owen Anderson03aadae2011-09-01 23:23:50 +00002874 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2875 return MCDisassembler::Fail;
2876 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder)))
2877 return MCDisassembler::Fail;
2878 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2*inc)%32, Address, Decoder)))
2879 return MCDisassembler::Fail;
2880 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+3*inc)%32, Address, Decoder)))
2881 return MCDisassembler::Fail;
Owen Andersonac92e772011-08-22 18:22:06 +00002882 if (Rm != 0xF) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002883 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2884 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002885 }
Owen Andersone0152a72011-08-09 20:55:18 +00002886
Owen Anderson03aadae2011-09-01 23:23:50 +00002887 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2888 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002889 Inst.addOperand(MCOperand::CreateImm(align));
2890
2891 if (Rm == 0xD)
2892 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersoned253852011-08-11 18:24:51 +00002893 else if (Rm != 0xF) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002894 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
2895 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002896 }
Owen Andersone0152a72011-08-09 20:55:18 +00002897
Owen Andersona4043c42011-08-17 17:44:15 +00002898 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002899}
2900
Owen Anderson03aadae2011-09-01 23:23:50 +00002901static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00002902DecodeNEONModImmInstruction(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00002903 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002904 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002905
Jim Grosbachecaef492012-08-14 19:06:05 +00002906 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2907 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2908 unsigned imm = fieldFromInstruction(Insn, 0, 4);
2909 imm |= fieldFromInstruction(Insn, 16, 3) << 4;
2910 imm |= fieldFromInstruction(Insn, 24, 1) << 7;
2911 imm |= fieldFromInstruction(Insn, 8, 4) << 8;
2912 imm |= fieldFromInstruction(Insn, 5, 1) << 12;
2913 unsigned Q = fieldFromInstruction(Insn, 6, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00002914
Owen Andersoned253852011-08-11 18:24:51 +00002915 if (Q) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002916 if (!Check(S, DecodeQPRRegisterClass(Inst, Rd, Address, Decoder)))
2917 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002918 } else {
Owen Anderson03aadae2011-09-01 23:23:50 +00002919 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2920 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002921 }
Owen Andersone0152a72011-08-09 20:55:18 +00002922
2923 Inst.addOperand(MCOperand::CreateImm(imm));
2924
2925 switch (Inst.getOpcode()) {
2926 case ARM::VORRiv4i16:
2927 case ARM::VORRiv2i32:
2928 case ARM::VBICiv4i16:
2929 case ARM::VBICiv2i32:
Owen Anderson03aadae2011-09-01 23:23:50 +00002930 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2931 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002932 break;
2933 case ARM::VORRiv8i16:
2934 case ARM::VORRiv4i32:
2935 case ARM::VBICiv8i16:
2936 case ARM::VBICiv4i32:
Owen Anderson03aadae2011-09-01 23:23:50 +00002937 if (!Check(S, DecodeQPRRegisterClass(Inst, Rd, Address, Decoder)))
2938 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002939 break;
2940 default:
2941 break;
2942 }
2943
Owen Andersona4043c42011-08-17 17:44:15 +00002944 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002945}
2946
Craig Topperf6e7e122012-03-27 07:21:54 +00002947static DecodeStatus DecodeVSHLMaxInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002948 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002949 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002950
Jim Grosbachecaef492012-08-14 19:06:05 +00002951 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2952 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2953 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
2954 Rm |= fieldFromInstruction(Insn, 5, 1) << 4;
2955 unsigned size = fieldFromInstruction(Insn, 18, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00002956
Owen Anderson03aadae2011-09-01 23:23:50 +00002957 if (!Check(S, DecodeQPRRegisterClass(Inst, Rd, Address, Decoder)))
2958 return MCDisassembler::Fail;
2959 if (!Check(S, DecodeDPRRegisterClass(Inst, Rm, Address, Decoder)))
2960 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002961 Inst.addOperand(MCOperand::CreateImm(8 << size));
2962
Owen Andersona4043c42011-08-17 17:44:15 +00002963 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002964}
2965
Craig Topperf6e7e122012-03-27 07:21:54 +00002966static DecodeStatus DecodeShiftRight8Imm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002967 uint64_t Address, const void *Decoder) {
2968 Inst.addOperand(MCOperand::CreateImm(8 - Val));
James Molloydb4ce602011-09-01 18:02:14 +00002969 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00002970}
2971
Craig Topperf6e7e122012-03-27 07:21:54 +00002972static DecodeStatus DecodeShiftRight16Imm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002973 uint64_t Address, const void *Decoder) {
2974 Inst.addOperand(MCOperand::CreateImm(16 - Val));
James Molloydb4ce602011-09-01 18:02:14 +00002975 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00002976}
2977
Craig Topperf6e7e122012-03-27 07:21:54 +00002978static DecodeStatus DecodeShiftRight32Imm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002979 uint64_t Address, const void *Decoder) {
2980 Inst.addOperand(MCOperand::CreateImm(32 - Val));
James Molloydb4ce602011-09-01 18:02:14 +00002981 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00002982}
2983
Craig Topperf6e7e122012-03-27 07:21:54 +00002984static DecodeStatus DecodeShiftRight64Imm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002985 uint64_t Address, const void *Decoder) {
2986 Inst.addOperand(MCOperand::CreateImm(64 - Val));
James Molloydb4ce602011-09-01 18:02:14 +00002987 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00002988}
2989
Craig Topperf6e7e122012-03-27 07:21:54 +00002990static DecodeStatus DecodeTBLInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002991 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002992 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002993
Jim Grosbachecaef492012-08-14 19:06:05 +00002994 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2995 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2996 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
2997 Rn |= fieldFromInstruction(Insn, 7, 1) << 4;
2998 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
2999 Rm |= fieldFromInstruction(Insn, 5, 1) << 4;
3000 unsigned op = fieldFromInstruction(Insn, 6, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00003001
Owen Anderson03aadae2011-09-01 23:23:50 +00003002 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3003 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00003004 if (op) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003005 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3006 return MCDisassembler::Fail; // Writeback
Owen Andersoned253852011-08-11 18:24:51 +00003007 }
Owen Andersone0152a72011-08-09 20:55:18 +00003008
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003009 switch (Inst.getOpcode()) {
3010 case ARM::VTBL2:
3011 case ARM::VTBX2:
3012 if (!Check(S, DecodeDPairRegisterClass(Inst, Rn, Address, Decoder)))
3013 return MCDisassembler::Fail;
3014 break;
3015 default:
3016 if (!Check(S, DecodeDPRRegisterClass(Inst, Rn, Address, Decoder)))
3017 return MCDisassembler::Fail;
3018 }
Owen Andersone0152a72011-08-09 20:55:18 +00003019
Owen Anderson03aadae2011-09-01 23:23:50 +00003020 if (!Check(S, DecodeDPRRegisterClass(Inst, Rm, Address, Decoder)))
3021 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003022
Owen Andersona4043c42011-08-17 17:44:15 +00003023 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003024}
3025
Craig Topperf6e7e122012-03-27 07:21:54 +00003026static DecodeStatus DecodeThumbAddSpecialReg(MCInst &Inst, uint16_t Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00003027 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003028 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003029
Jim Grosbachecaef492012-08-14 19:06:05 +00003030 unsigned dst = fieldFromInstruction(Insn, 8, 3);
3031 unsigned imm = fieldFromInstruction(Insn, 0, 8);
Owen Andersone0152a72011-08-09 20:55:18 +00003032
Owen Anderson03aadae2011-09-01 23:23:50 +00003033 if (!Check(S, DecodetGPRRegisterClass(Inst, dst, Address, Decoder)))
3034 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003035
Owen Andersona01bcbf2011-08-26 18:09:22 +00003036 switch(Inst.getOpcode()) {
Owen Anderson5658b492011-08-26 19:39:26 +00003037 default:
James Molloydb4ce602011-09-01 18:02:14 +00003038 return MCDisassembler::Fail;
Owen Andersona01bcbf2011-08-26 18:09:22 +00003039 case ARM::tADR:
Owen Anderson240d20a2011-08-26 21:47:57 +00003040 break; // tADR does not explicitly represent the PC as an operand.
Owen Andersona01bcbf2011-08-26 18:09:22 +00003041 case ARM::tADDrSPi:
3042 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
3043 break;
Owen Andersona01bcbf2011-08-26 18:09:22 +00003044 }
Owen Andersone0152a72011-08-09 20:55:18 +00003045
3046 Inst.addOperand(MCOperand::CreateImm(imm));
Owen Andersona4043c42011-08-17 17:44:15 +00003047 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003048}
3049
Craig Topperf6e7e122012-03-27 07:21:54 +00003050static DecodeStatus DecodeThumbBROperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003051 uint64_t Address, const void *Decoder) {
Kevin Enderby40d4e472012-04-12 23:13:34 +00003052 if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<12>(Val<<1) + 4,
3053 true, 2, Inst, Decoder))
3054 Inst.addOperand(MCOperand::CreateImm(SignExtend32<12>(Val << 1)));
James Molloydb4ce602011-09-01 18:02:14 +00003055 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003056}
3057
Craig Topperf6e7e122012-03-27 07:21:54 +00003058static DecodeStatus DecodeT2BROperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003059 uint64_t Address, const void *Decoder) {
Kevin Enderbycabbae62012-05-04 22:09:52 +00003060 if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<21>(Val) + 4,
Kevin Enderby40d4e472012-04-12 23:13:34 +00003061 true, 4, Inst, Decoder))
3062 Inst.addOperand(MCOperand::CreateImm(SignExtend32<21>(Val)));
James Molloydb4ce602011-09-01 18:02:14 +00003063 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003064}
3065
Craig Topperf6e7e122012-03-27 07:21:54 +00003066static DecodeStatus DecodeThumbCmpBROperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003067 uint64_t Address, const void *Decoder) {
Gordon Keiser772cf462013-03-28 19:22:28 +00003068 if (!tryAddingSymbolicOperand(Address, Address + (Val<<1) + 4,
Kevin Enderby40d4e472012-04-12 23:13:34 +00003069 true, 2, Inst, Decoder))
Gordon Keiser772cf462013-03-28 19:22:28 +00003070 Inst.addOperand(MCOperand::CreateImm(Val << 1));
James Molloydb4ce602011-09-01 18:02:14 +00003071 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003072}
3073
Craig Topperf6e7e122012-03-27 07:21:54 +00003074static DecodeStatus DecodeThumbAddrModeRR(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003075 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003076 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003077
Jim Grosbachecaef492012-08-14 19:06:05 +00003078 unsigned Rn = fieldFromInstruction(Val, 0, 3);
3079 unsigned Rm = fieldFromInstruction(Val, 3, 3);
Owen Andersone0152a72011-08-09 20:55:18 +00003080
Owen Anderson03aadae2011-09-01 23:23:50 +00003081 if (!Check(S, DecodetGPRRegisterClass(Inst, Rn, Address, Decoder)))
3082 return MCDisassembler::Fail;
3083 if (!Check(S, DecodetGPRRegisterClass(Inst, Rm, Address, Decoder)))
3084 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003085
Owen Andersona4043c42011-08-17 17:44:15 +00003086 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003087}
3088
Craig Topperf6e7e122012-03-27 07:21:54 +00003089static DecodeStatus DecodeThumbAddrModeIS(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003090 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003091 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003092
Jim Grosbachecaef492012-08-14 19:06:05 +00003093 unsigned Rn = fieldFromInstruction(Val, 0, 3);
3094 unsigned imm = fieldFromInstruction(Val, 3, 5);
Owen Andersone0152a72011-08-09 20:55:18 +00003095
Owen Anderson03aadae2011-09-01 23:23:50 +00003096 if (!Check(S, DecodetGPRRegisterClass(Inst, Rn, Address, Decoder)))
3097 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003098 Inst.addOperand(MCOperand::CreateImm(imm));
3099
Owen Andersona4043c42011-08-17 17:44:15 +00003100 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003101}
3102
Craig Topperf6e7e122012-03-27 07:21:54 +00003103static DecodeStatus DecodeThumbAddrModePC(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003104 uint64_t Address, const void *Decoder) {
Kevin Enderby5dcda642011-10-04 22:44:48 +00003105 unsigned imm = Val << 2;
3106
3107 Inst.addOperand(MCOperand::CreateImm(imm));
3108 tryAddingPcLoadReferenceComment(Address, (Address & ~2u) + imm + 4, Decoder);
Owen Andersone0152a72011-08-09 20:55:18 +00003109
James Molloydb4ce602011-09-01 18:02:14 +00003110 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003111}
3112
Craig Topperf6e7e122012-03-27 07:21:54 +00003113static DecodeStatus DecodeThumbAddrModeSP(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003114 uint64_t Address, const void *Decoder) {
3115 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
Owen Andersonb4981322011-08-22 17:56:58 +00003116 Inst.addOperand(MCOperand::CreateImm(Val));
Owen Andersone0152a72011-08-09 20:55:18 +00003117
James Molloydb4ce602011-09-01 18:02:14 +00003118 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003119}
3120
Craig Topperf6e7e122012-03-27 07:21:54 +00003121static DecodeStatus DecodeT2AddrModeSOReg(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003122 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003123 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003124
Jim Grosbachecaef492012-08-14 19:06:05 +00003125 unsigned Rn = fieldFromInstruction(Val, 6, 4);
3126 unsigned Rm = fieldFromInstruction(Val, 2, 4);
3127 unsigned imm = fieldFromInstruction(Val, 0, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00003128
Owen Anderson03aadae2011-09-01 23:23:50 +00003129 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3130 return MCDisassembler::Fail;
3131 if (!Check(S, DecoderGPRRegisterClass(Inst, Rm, Address, Decoder)))
3132 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003133 Inst.addOperand(MCOperand::CreateImm(imm));
3134
Owen Andersona4043c42011-08-17 17:44:15 +00003135 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003136}
3137
Craig Topperf6e7e122012-03-27 07:21:54 +00003138static DecodeStatus DecodeT2LoadShift(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00003139 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003140 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003141
Owen Anderson924bcfc2011-08-23 17:51:38 +00003142 switch (Inst.getOpcode()) {
3143 case ARM::t2PLDs:
3144 case ARM::t2PLDWs:
3145 case ARM::t2PLIs:
3146 break;
3147 default: {
Jim Grosbachecaef492012-08-14 19:06:05 +00003148 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
Owen Anderson987a8782011-09-23 21:07:25 +00003149 if (!Check(S, DecoderGPRRegisterClass(Inst, Rt, Address, Decoder)))
Owen Anderson03aadae2011-09-01 23:23:50 +00003150 return MCDisassembler::Fail;
Owen Anderson924bcfc2011-08-23 17:51:38 +00003151 }
Owen Andersone0152a72011-08-09 20:55:18 +00003152 }
3153
Jim Grosbachecaef492012-08-14 19:06:05 +00003154 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00003155 if (Rn == 0xF) {
3156 switch (Inst.getOpcode()) {
3157 case ARM::t2LDRBs:
3158 Inst.setOpcode(ARM::t2LDRBpci);
3159 break;
3160 case ARM::t2LDRHs:
3161 Inst.setOpcode(ARM::t2LDRHpci);
3162 break;
3163 case ARM::t2LDRSHs:
3164 Inst.setOpcode(ARM::t2LDRSHpci);
3165 break;
3166 case ARM::t2LDRSBs:
3167 Inst.setOpcode(ARM::t2LDRSBpci);
3168 break;
3169 case ARM::t2PLDs:
3170 Inst.setOpcode(ARM::t2PLDi12);
3171 Inst.addOperand(MCOperand::CreateReg(ARM::PC));
3172 break;
3173 default:
James Molloydb4ce602011-09-01 18:02:14 +00003174 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003175 }
3176
Jim Grosbachecaef492012-08-14 19:06:05 +00003177 int imm = fieldFromInstruction(Insn, 0, 12);
3178 if (!fieldFromInstruction(Insn, 23, 1)) imm *= -1;
Owen Andersone0152a72011-08-09 20:55:18 +00003179 Inst.addOperand(MCOperand::CreateImm(imm));
3180
Owen Andersona4043c42011-08-17 17:44:15 +00003181 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003182 }
3183
Jim Grosbachecaef492012-08-14 19:06:05 +00003184 unsigned addrmode = fieldFromInstruction(Insn, 4, 2);
3185 addrmode |= fieldFromInstruction(Insn, 0, 4) << 2;
3186 addrmode |= fieldFromInstruction(Insn, 16, 4) << 6;
Owen Anderson03aadae2011-09-01 23:23:50 +00003187 if (!Check(S, DecodeT2AddrModeSOReg(Inst, addrmode, Address, Decoder)))
3188 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003189
Owen Andersona4043c42011-08-17 17:44:15 +00003190 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003191}
3192
Craig Topperf6e7e122012-03-27 07:21:54 +00003193static DecodeStatus DecodeT2Imm8S4(MCInst &Inst, unsigned Val,
Owen Anderson5d69f632011-08-10 17:36:48 +00003194 uint64_t Address, const void *Decoder) {
Jiangning Liu6a43bf72012-08-02 08:29:50 +00003195 if (Val == 0)
3196 Inst.addOperand(MCOperand::CreateImm(INT32_MIN));
3197 else {
3198 int imm = Val & 0xFF;
3199
3200 if (!(Val & 0x100)) imm *= -1;
Richard Smith228e6d42012-08-24 23:29:28 +00003201 Inst.addOperand(MCOperand::CreateImm(imm * 4));
Jiangning Liu6a43bf72012-08-02 08:29:50 +00003202 }
Owen Andersone0152a72011-08-09 20:55:18 +00003203
James Molloydb4ce602011-09-01 18:02:14 +00003204 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003205}
3206
Craig Topperf6e7e122012-03-27 07:21:54 +00003207static DecodeStatus DecodeT2AddrModeImm8s4(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003208 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003209 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003210
Jim Grosbachecaef492012-08-14 19:06:05 +00003211 unsigned Rn = fieldFromInstruction(Val, 9, 4);
3212 unsigned imm = fieldFromInstruction(Val, 0, 9);
Owen Andersone0152a72011-08-09 20:55:18 +00003213
Owen Anderson03aadae2011-09-01 23:23:50 +00003214 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3215 return MCDisassembler::Fail;
3216 if (!Check(S, DecodeT2Imm8S4(Inst, imm, Address, Decoder)))
3217 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003218
Owen Andersona4043c42011-08-17 17:44:15 +00003219 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003220}
3221
Craig Topperf6e7e122012-03-27 07:21:54 +00003222static DecodeStatus DecodeT2AddrModeImm0_1020s4(MCInst &Inst,unsigned Val,
Jim Grosbacha05627e2011-09-09 18:37:27 +00003223 uint64_t Address, const void *Decoder) {
3224 DecodeStatus S = MCDisassembler::Success;
3225
Jim Grosbachecaef492012-08-14 19:06:05 +00003226 unsigned Rn = fieldFromInstruction(Val, 8, 4);
3227 unsigned imm = fieldFromInstruction(Val, 0, 8);
Jim Grosbacha05627e2011-09-09 18:37:27 +00003228
3229 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
3230 return MCDisassembler::Fail;
3231
3232 Inst.addOperand(MCOperand::CreateImm(imm));
3233
3234 return S;
3235}
3236
Craig Topperf6e7e122012-03-27 07:21:54 +00003237static DecodeStatus DecodeT2Imm8(MCInst &Inst, unsigned Val,
Owen Anderson5d69f632011-08-10 17:36:48 +00003238 uint64_t Address, const void *Decoder) {
Owen Andersone0152a72011-08-09 20:55:18 +00003239 int imm = Val & 0xFF;
Owen Andersonfe823652011-09-16 21:08:33 +00003240 if (Val == 0)
3241 imm = INT32_MIN;
3242 else if (!(Val & 0x100))
3243 imm *= -1;
Owen Andersone0152a72011-08-09 20:55:18 +00003244 Inst.addOperand(MCOperand::CreateImm(imm));
3245
James Molloydb4ce602011-09-01 18:02:14 +00003246 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003247}
3248
3249
Craig Topperf6e7e122012-03-27 07:21:54 +00003250static DecodeStatus DecodeT2AddrModeImm8(MCInst &Inst, unsigned Val,
Owen Anderson5d69f632011-08-10 17:36:48 +00003251 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003252 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003253
Jim Grosbachecaef492012-08-14 19:06:05 +00003254 unsigned Rn = fieldFromInstruction(Val, 9, 4);
3255 unsigned imm = fieldFromInstruction(Val, 0, 9);
Owen Andersone0152a72011-08-09 20:55:18 +00003256
3257 // Some instructions always use an additive offset.
3258 switch (Inst.getOpcode()) {
3259 case ARM::t2LDRT:
3260 case ARM::t2LDRBT:
3261 case ARM::t2LDRHT:
3262 case ARM::t2LDRSBT:
3263 case ARM::t2LDRSHT:
Owen Andersonddfcec92011-09-19 18:07:10 +00003264 case ARM::t2STRT:
3265 case ARM::t2STRBT:
3266 case ARM::t2STRHT:
Owen Andersone0152a72011-08-09 20:55:18 +00003267 imm |= 0x100;
3268 break;
3269 default:
3270 break;
3271 }
3272
Owen Anderson03aadae2011-09-01 23:23:50 +00003273 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3274 return MCDisassembler::Fail;
3275 if (!Check(S, DecodeT2Imm8(Inst, imm, Address, Decoder)))
3276 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003277
Owen Andersona4043c42011-08-17 17:44:15 +00003278 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003279}
3280
Craig Topperf6e7e122012-03-27 07:21:54 +00003281static DecodeStatus DecodeT2LdStPre(MCInst &Inst, unsigned Insn,
Owen Andersona9ebf6f2011-09-12 18:56:30 +00003282 uint64_t Address, const void *Decoder) {
3283 DecodeStatus S = MCDisassembler::Success;
3284
Jim Grosbachecaef492012-08-14 19:06:05 +00003285 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
3286 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3287 unsigned addr = fieldFromInstruction(Insn, 0, 8);
3288 addr |= fieldFromInstruction(Insn, 9, 1) << 8;
Owen Andersona9ebf6f2011-09-12 18:56:30 +00003289 addr |= Rn << 9;
Jim Grosbachecaef492012-08-14 19:06:05 +00003290 unsigned load = fieldFromInstruction(Insn, 20, 1);
Owen Andersona9ebf6f2011-09-12 18:56:30 +00003291
3292 if (!load) {
3293 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3294 return MCDisassembler::Fail;
3295 }
3296
Joe Abbeyf686be42013-03-26 13:58:53 +00003297 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
Owen Andersona9ebf6f2011-09-12 18:56:30 +00003298 return MCDisassembler::Fail;
3299
3300 if (load) {
3301 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3302 return MCDisassembler::Fail;
3303 }
3304
3305 if (!Check(S, DecodeT2AddrModeImm8(Inst, addr, Address, Decoder)))
3306 return MCDisassembler::Fail;
3307
3308 return S;
3309}
Owen Andersone0152a72011-08-09 20:55:18 +00003310
Craig Topperf6e7e122012-03-27 07:21:54 +00003311static DecodeStatus DecodeT2AddrModeImm12(MCInst &Inst, unsigned Val,
Owen Anderson5d69f632011-08-10 17:36:48 +00003312 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003313 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003314
Jim Grosbachecaef492012-08-14 19:06:05 +00003315 unsigned Rn = fieldFromInstruction(Val, 13, 4);
3316 unsigned imm = fieldFromInstruction(Val, 0, 12);
Owen Andersone0152a72011-08-09 20:55:18 +00003317
Owen Anderson03aadae2011-09-01 23:23:50 +00003318 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3319 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003320 Inst.addOperand(MCOperand::CreateImm(imm));
3321
Owen Andersona4043c42011-08-17 17:44:15 +00003322 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003323}
3324
3325
Craig Topperf6e7e122012-03-27 07:21:54 +00003326static DecodeStatus DecodeThumbAddSPImm(MCInst &Inst, uint16_t Insn,
Owen Anderson5d69f632011-08-10 17:36:48 +00003327 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003328 unsigned imm = fieldFromInstruction(Insn, 0, 7);
Owen Andersone0152a72011-08-09 20:55:18 +00003329
3330 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
3331 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
3332 Inst.addOperand(MCOperand::CreateImm(imm));
3333
James Molloydb4ce602011-09-01 18:02:14 +00003334 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003335}
3336
Craig Topperf6e7e122012-03-27 07:21:54 +00003337static DecodeStatus DecodeThumbAddSPReg(MCInst &Inst, uint16_t Insn,
Owen Anderson5d69f632011-08-10 17:36:48 +00003338 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003339 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003340
Owen Andersone0152a72011-08-09 20:55:18 +00003341 if (Inst.getOpcode() == ARM::tADDrSP) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003342 unsigned Rdm = fieldFromInstruction(Insn, 0, 3);
3343 Rdm |= fieldFromInstruction(Insn, 7, 1) << 3;
Owen Andersone0152a72011-08-09 20:55:18 +00003344
Owen Anderson03aadae2011-09-01 23:23:50 +00003345 if (!Check(S, DecodeGPRRegisterClass(Inst, Rdm, Address, Decoder)))
3346 return MCDisassembler::Fail;
Jim Grosbach9d8f6f32012-04-27 23:51:33 +00003347 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
Owen Anderson03aadae2011-09-01 23:23:50 +00003348 if (!Check(S, DecodeGPRRegisterClass(Inst, Rdm, Address, Decoder)))
3349 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003350 } else if (Inst.getOpcode() == ARM::tADDspr) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003351 unsigned Rm = fieldFromInstruction(Insn, 3, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00003352
3353 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
3354 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
Owen Anderson03aadae2011-09-01 23:23:50 +00003355 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
3356 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003357 }
3358
Owen Andersona4043c42011-08-17 17:44:15 +00003359 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003360}
3361
Craig Topperf6e7e122012-03-27 07:21:54 +00003362static DecodeStatus DecodeThumbCPS(MCInst &Inst, uint16_t Insn,
Owen Anderson5d69f632011-08-10 17:36:48 +00003363 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003364 unsigned imod = fieldFromInstruction(Insn, 4, 1) | 0x2;
3365 unsigned flags = fieldFromInstruction(Insn, 0, 3);
Owen Andersone0152a72011-08-09 20:55:18 +00003366
3367 Inst.addOperand(MCOperand::CreateImm(imod));
3368 Inst.addOperand(MCOperand::CreateImm(flags));
3369
James Molloydb4ce602011-09-01 18:02:14 +00003370 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003371}
3372
Craig Topperf6e7e122012-03-27 07:21:54 +00003373static DecodeStatus DecodePostIdxReg(MCInst &Inst, unsigned Insn,
Owen Anderson5d69f632011-08-10 17:36:48 +00003374 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003375 DecodeStatus S = MCDisassembler::Success;
Jim Grosbachecaef492012-08-14 19:06:05 +00003376 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
3377 unsigned add = fieldFromInstruction(Insn, 4, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00003378
Silviu Barangad213f212012-03-22 13:24:43 +00003379 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
Owen Anderson03aadae2011-09-01 23:23:50 +00003380 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003381 Inst.addOperand(MCOperand::CreateImm(add));
3382
Owen Andersona4043c42011-08-17 17:44:15 +00003383 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003384}
3385
Craig Topperf6e7e122012-03-27 07:21:54 +00003386static DecodeStatus DecodeThumbBLXOffset(MCInst &Inst, unsigned Val,
Owen Anderson5d69f632011-08-10 17:36:48 +00003387 uint64_t Address, const void *Decoder) {
NAKAMURA Takumi70c1aa02012-05-22 21:47:02 +00003388 // Val is passed in as S:J1:J2:imm10H:imm10L:'0'
Kevin Enderby91422302012-05-03 22:41:56 +00003389 // Note only one trailing zero not two. Also the J1 and J2 values are from
3390 // the encoded instruction. So here change to I1 and I2 values via:
3391 // I1 = NOT(J1 EOR S);
3392 // I2 = NOT(J2 EOR S);
3393 // and build the imm32 with two trailing zeros as documented:
NAKAMURA Takumi70c1aa02012-05-22 21:47:02 +00003394 // imm32 = SignExtend(S:I1:I2:imm10H:imm10L:'00', 32);
Kevin Enderby91422302012-05-03 22:41:56 +00003395 unsigned S = (Val >> 23) & 1;
3396 unsigned J1 = (Val >> 22) & 1;
3397 unsigned J2 = (Val >> 21) & 1;
3398 unsigned I1 = !(J1 ^ S);
3399 unsigned I2 = !(J2 ^ S);
3400 unsigned tmp = (Val & ~0x600000) | (I1 << 22) | (I2 << 21);
3401 int imm32 = SignExtend32<25>(tmp << 1);
3402
Jim Grosbach79ebc512011-10-20 17:28:20 +00003403 if (!tryAddingSymbolicOperand(Address,
Kevin Enderby91422302012-05-03 22:41:56 +00003404 (Address & ~2u) + imm32 + 4,
Kevin Enderby5dcda642011-10-04 22:44:48 +00003405 true, 4, Inst, Decoder))
Kevin Enderby91422302012-05-03 22:41:56 +00003406 Inst.addOperand(MCOperand::CreateImm(imm32));
James Molloydb4ce602011-09-01 18:02:14 +00003407 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003408}
3409
Craig Topperf6e7e122012-03-27 07:21:54 +00003410static DecodeStatus DecodeCoprocessor(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003411 uint64_t Address, const void *Decoder) {
3412 if (Val == 0xA || Val == 0xB)
James Molloydb4ce602011-09-01 18:02:14 +00003413 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003414
3415 Inst.addOperand(MCOperand::CreateImm(Val));
James Molloydb4ce602011-09-01 18:02:14 +00003416 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003417}
3418
Owen Anderson03aadae2011-09-01 23:23:50 +00003419static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00003420DecodeThumbTableBranch(MCInst &Inst, unsigned Insn,
Jim Grosbach05541f42011-09-19 22:21:13 +00003421 uint64_t Address, const void *Decoder) {
3422 DecodeStatus S = MCDisassembler::Success;
3423
Jim Grosbachecaef492012-08-14 19:06:05 +00003424 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3425 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
Jim Grosbach05541f42011-09-19 22:21:13 +00003426
3427 if (Rn == ARM::SP) S = MCDisassembler::SoftFail;
3428 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3429 return MCDisassembler::Fail;
3430 if (!Check(S, DecoderGPRRegisterClass(Inst, Rm, Address, Decoder)))
3431 return MCDisassembler::Fail;
3432 return S;
3433}
3434
3435static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00003436DecodeThumb2BCCInstruction(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00003437 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003438 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003439
Jim Grosbachecaef492012-08-14 19:06:05 +00003440 unsigned pred = fieldFromInstruction(Insn, 22, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00003441 if (pred == 0xE || pred == 0xF) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003442 unsigned opc = fieldFromInstruction(Insn, 4, 28);
Owen Andersone0152a72011-08-09 20:55:18 +00003443 switch (opc) {
3444 default:
James Molloydb4ce602011-09-01 18:02:14 +00003445 return MCDisassembler::Fail;
Owen Anderson4af0aa92011-08-31 22:00:41 +00003446 case 0xf3bf8f4:
Owen Andersone0152a72011-08-09 20:55:18 +00003447 Inst.setOpcode(ARM::t2DSB);
3448 break;
Owen Anderson4af0aa92011-08-31 22:00:41 +00003449 case 0xf3bf8f5:
Owen Andersone0152a72011-08-09 20:55:18 +00003450 Inst.setOpcode(ARM::t2DMB);
3451 break;
Owen Anderson4af0aa92011-08-31 22:00:41 +00003452 case 0xf3bf8f6:
Owen Andersone0152a72011-08-09 20:55:18 +00003453 Inst.setOpcode(ARM::t2ISB);
Owen Andersoncd5612d2011-09-07 17:55:19 +00003454 break;
Owen Andersone0152a72011-08-09 20:55:18 +00003455 }
3456
Jim Grosbachecaef492012-08-14 19:06:05 +00003457 unsigned imm = fieldFromInstruction(Insn, 0, 4);
Owen Andersone0089312011-08-09 23:25:42 +00003458 return DecodeMemBarrierOption(Inst, imm, Address, Decoder);
Owen Andersone0152a72011-08-09 20:55:18 +00003459 }
3460
Jim Grosbachecaef492012-08-14 19:06:05 +00003461 unsigned brtarget = fieldFromInstruction(Insn, 0, 11) << 1;
3462 brtarget |= fieldFromInstruction(Insn, 11, 1) << 19;
3463 brtarget |= fieldFromInstruction(Insn, 13, 1) << 18;
3464 brtarget |= fieldFromInstruction(Insn, 16, 6) << 12;
3465 brtarget |= fieldFromInstruction(Insn, 26, 1) << 20;
Owen Andersone0152a72011-08-09 20:55:18 +00003466
Owen Anderson03aadae2011-09-01 23:23:50 +00003467 if (!Check(S, DecodeT2BROperand(Inst, brtarget, Address, Decoder)))
3468 return MCDisassembler::Fail;
3469 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3470 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003471
Owen Andersona4043c42011-08-17 17:44:15 +00003472 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003473}
3474
3475// Decode a shifted immediate operand. These basically consist
3476// of an 8-bit value, and a 4-bit directive that specifies either
3477// a splat operation or a rotation.
Craig Topperf6e7e122012-03-27 07:21:54 +00003478static DecodeStatus DecodeT2SOImm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003479 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003480 unsigned ctrl = fieldFromInstruction(Val, 10, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00003481 if (ctrl == 0) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003482 unsigned byte = fieldFromInstruction(Val, 8, 2);
3483 unsigned imm = fieldFromInstruction(Val, 0, 8);
Owen Andersone0152a72011-08-09 20:55:18 +00003484 switch (byte) {
3485 case 0:
3486 Inst.addOperand(MCOperand::CreateImm(imm));
3487 break;
3488 case 1:
3489 Inst.addOperand(MCOperand::CreateImm((imm << 16) | imm));
3490 break;
3491 case 2:
3492 Inst.addOperand(MCOperand::CreateImm((imm << 24) | (imm << 8)));
3493 break;
3494 case 3:
3495 Inst.addOperand(MCOperand::CreateImm((imm << 24) | (imm << 16) |
3496 (imm << 8) | imm));
3497 break;
3498 }
3499 } else {
Jim Grosbachecaef492012-08-14 19:06:05 +00003500 unsigned unrot = fieldFromInstruction(Val, 0, 7) | 0x80;
3501 unsigned rot = fieldFromInstruction(Val, 7, 5);
Owen Andersone0152a72011-08-09 20:55:18 +00003502 unsigned imm = (unrot >> rot) | (unrot << ((32-rot)&31));
3503 Inst.addOperand(MCOperand::CreateImm(imm));
3504 }
3505
James Molloydb4ce602011-09-01 18:02:14 +00003506 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003507}
3508
Owen Anderson03aadae2011-09-01 23:23:50 +00003509static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00003510DecodeThumbBCCTargetOperand(MCInst &Inst, unsigned Val,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00003511 uint64_t Address, const void *Decoder){
Richard Bartonf1ef87d2012-06-06 09:12:53 +00003512 if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<9>(Val<<1) + 4,
Kevin Enderby40d4e472012-04-12 23:13:34 +00003513 true, 2, Inst, Decoder))
Richard Bartonf1ef87d2012-06-06 09:12:53 +00003514 Inst.addOperand(MCOperand::CreateImm(SignExtend32<9>(Val << 1)));
James Molloydb4ce602011-09-01 18:02:14 +00003515 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003516}
3517
Craig Topperf6e7e122012-03-27 07:21:54 +00003518static DecodeStatus DecodeThumbBLTargetOperand(MCInst &Inst, unsigned Val,
Owen Anderson5d69f632011-08-10 17:36:48 +00003519 uint64_t Address, const void *Decoder){
Kevin Enderby91422302012-05-03 22:41:56 +00003520 // Val is passed in as S:J1:J2:imm10:imm11
3521 // Note no trailing zero after imm11. Also the J1 and J2 values are from
3522 // the encoded instruction. So here change to I1 and I2 values via:
3523 // I1 = NOT(J1 EOR S);
3524 // I2 = NOT(J2 EOR S);
3525 // and build the imm32 with one trailing zero as documented:
NAKAMURA Takumi70c1aa02012-05-22 21:47:02 +00003526 // imm32 = SignExtend(S:I1:I2:imm10:imm11:'0', 32);
Kevin Enderby91422302012-05-03 22:41:56 +00003527 unsigned S = (Val >> 23) & 1;
3528 unsigned J1 = (Val >> 22) & 1;
3529 unsigned J2 = (Val >> 21) & 1;
3530 unsigned I1 = !(J1 ^ S);
3531 unsigned I2 = !(J2 ^ S);
3532 unsigned tmp = (Val & ~0x600000) | (I1 << 22) | (I2 << 21);
3533 int imm32 = SignExtend32<25>(tmp << 1);
3534
3535 if (!tryAddingSymbolicOperand(Address, Address + imm32 + 4,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00003536 true, 4, Inst, Decoder))
Kevin Enderby91422302012-05-03 22:41:56 +00003537 Inst.addOperand(MCOperand::CreateImm(imm32));
James Molloydb4ce602011-09-01 18:02:14 +00003538 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003539}
3540
Craig Topperf6e7e122012-03-27 07:21:54 +00003541static DecodeStatus DecodeMemBarrierOption(MCInst &Inst, unsigned Val,
Owen Andersone0089312011-08-09 23:25:42 +00003542 uint64_t Address, const void *Decoder) {
Jiangning Liu288e1af2012-08-02 08:21:27 +00003543 if (Val & ~0xf)
James Molloydb4ce602011-09-01 18:02:14 +00003544 return MCDisassembler::Fail;
Owen Andersone0089312011-08-09 23:25:42 +00003545
3546 Inst.addOperand(MCOperand::CreateImm(Val));
James Molloydb4ce602011-09-01 18:02:14 +00003547 return MCDisassembler::Success;
Owen Andersone0089312011-08-09 23:25:42 +00003548}
3549
Craig Topperf6e7e122012-03-27 07:21:54 +00003550static DecodeStatus DecodeMSRMask(MCInst &Inst, unsigned Val,
Owen Anderson60663402011-08-11 20:21:46 +00003551 uint64_t Address, const void *Decoder) {
James Molloydb4ce602011-09-01 18:02:14 +00003552 if (!Val) return MCDisassembler::Fail;
Owen Anderson60663402011-08-11 20:21:46 +00003553 Inst.addOperand(MCOperand::CreateImm(Val));
James Molloydb4ce602011-09-01 18:02:14 +00003554 return MCDisassembler::Success;
Owen Anderson60663402011-08-11 20:21:46 +00003555}
Owen Andersonb685c9f2011-08-11 21:34:58 +00003556
Craig Topperf6e7e122012-03-27 07:21:54 +00003557static DecodeStatus DecodeDoubleRegLoad(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00003558 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003559 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003560
Jim Grosbachecaef492012-08-14 19:06:05 +00003561 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
3562 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3563 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Andersonc5798a3a52011-08-12 17:58:32 +00003564
James Molloydb4ce602011-09-01 18:02:14 +00003565 if ((Rt & 1) || Rt == 0xE || Rn == 0xF) return MCDisassembler::Fail;
Owen Andersonc5798a3a52011-08-12 17:58:32 +00003566
Owen Anderson03aadae2011-09-01 23:23:50 +00003567 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
3568 return MCDisassembler::Fail;
3569 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt+1, Address, Decoder)))
3570 return MCDisassembler::Fail;
3571 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3572 return MCDisassembler::Fail;
3573 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3574 return MCDisassembler::Fail;
Owen Andersonc5798a3a52011-08-12 17:58:32 +00003575
Owen Andersona4043c42011-08-17 17:44:15 +00003576 return S;
Owen Andersonc5798a3a52011-08-12 17:58:32 +00003577}
3578
3579
Craig Topperf6e7e122012-03-27 07:21:54 +00003580static DecodeStatus DecodeDoubleRegStore(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00003581 uint64_t Address, const void *Decoder){
Owen Anderson03aadae2011-09-01 23:23:50 +00003582 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003583
Jim Grosbachecaef492012-08-14 19:06:05 +00003584 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
3585 unsigned Rt = fieldFromInstruction(Insn, 0, 4);
3586 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3587 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Andersonb685c9f2011-08-11 21:34:58 +00003588
Tim Northover27ff5042013-04-19 15:44:32 +00003589 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
Owen Anderson03aadae2011-09-01 23:23:50 +00003590 return MCDisassembler::Fail;
Owen Andersonb685c9f2011-08-11 21:34:58 +00003591
James Molloydb4ce602011-09-01 18:02:14 +00003592 if ((Rt & 1) || Rt == 0xE || Rn == 0xF) return MCDisassembler::Fail;
3593 if (Rd == Rn || Rd == Rt || Rd == Rt+1) return MCDisassembler::Fail;
Owen Andersonb685c9f2011-08-11 21:34:58 +00003594
Owen Anderson03aadae2011-09-01 23:23:50 +00003595 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
3596 return MCDisassembler::Fail;
3597 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt+1, Address, Decoder)))
3598 return MCDisassembler::Fail;
3599 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3600 return MCDisassembler::Fail;
3601 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3602 return MCDisassembler::Fail;
Owen Andersonb685c9f2011-08-11 21:34:58 +00003603
Owen Andersona4043c42011-08-17 17:44:15 +00003604 return S;
Owen Andersonb685c9f2011-08-11 21:34:58 +00003605}
3606
Craig Topperf6e7e122012-03-27 07:21:54 +00003607static DecodeStatus DecodeLDRPreImm(MCInst &Inst, unsigned Insn,
Owen Anderson16d33f32011-08-26 20:43:14 +00003608 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003609 DecodeStatus S = MCDisassembler::Success;
Owen Anderson16d33f32011-08-26 20:43:14 +00003610
Jim Grosbachecaef492012-08-14 19:06:05 +00003611 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3612 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
3613 unsigned imm = fieldFromInstruction(Insn, 0, 12);
3614 imm |= fieldFromInstruction(Insn, 16, 4) << 13;
3615 imm |= fieldFromInstruction(Insn, 23, 1) << 12;
3616 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Anderson16d33f32011-08-26 20:43:14 +00003617
James Molloydb4ce602011-09-01 18:02:14 +00003618 if (Rn == 0xF || Rn == Rt) S = MCDisassembler::SoftFail;
Owen Anderson16d33f32011-08-26 20:43:14 +00003619
Owen Anderson03aadae2011-09-01 23:23:50 +00003620 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
3621 return MCDisassembler::Fail;
3622 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3623 return MCDisassembler::Fail;
3624 if (!Check(S, DecodeAddrModeImm12Operand(Inst, imm, Address, Decoder)))
3625 return MCDisassembler::Fail;
3626 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3627 return MCDisassembler::Fail;
Owen Anderson16d33f32011-08-26 20:43:14 +00003628
3629 return S;
3630}
3631
Craig Topperf6e7e122012-03-27 07:21:54 +00003632static DecodeStatus DecodeLDRPreReg(MCInst &Inst, unsigned Insn,
Owen Anderson16d33f32011-08-26 20:43:14 +00003633 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003634 DecodeStatus S = MCDisassembler::Success;
Owen Anderson16d33f32011-08-26 20:43:14 +00003635
Jim Grosbachecaef492012-08-14 19:06:05 +00003636 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3637 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
3638 unsigned imm = fieldFromInstruction(Insn, 0, 12);
3639 imm |= fieldFromInstruction(Insn, 16, 4) << 13;
3640 imm |= fieldFromInstruction(Insn, 23, 1) << 12;
3641 unsigned pred = fieldFromInstruction(Insn, 28, 4);
3642 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
Owen Anderson16d33f32011-08-26 20:43:14 +00003643
James Molloydb4ce602011-09-01 18:02:14 +00003644 if (Rn == 0xF || Rn == Rt) S = MCDisassembler::SoftFail;
3645 if (Rm == 0xF) S = MCDisassembler::SoftFail;
Owen Anderson16d33f32011-08-26 20:43:14 +00003646
Owen Anderson03aadae2011-09-01 23:23:50 +00003647 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
3648 return MCDisassembler::Fail;
3649 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3650 return MCDisassembler::Fail;
3651 if (!Check(S, DecodeSORegMemOperand(Inst, imm, Address, Decoder)))
3652 return MCDisassembler::Fail;
3653 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3654 return MCDisassembler::Fail;
Owen Anderson16d33f32011-08-26 20:43:14 +00003655
3656 return S;
3657}
3658
3659
Craig Topperf6e7e122012-03-27 07:21:54 +00003660static DecodeStatus DecodeSTRPreImm(MCInst &Inst, unsigned Insn,
Owen Anderson3987a612011-08-12 18:12:39 +00003661 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003662 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003663
Jim Grosbachecaef492012-08-14 19:06:05 +00003664 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3665 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
3666 unsigned imm = fieldFromInstruction(Insn, 0, 12);
3667 imm |= fieldFromInstruction(Insn, 16, 4) << 13;
3668 imm |= fieldFromInstruction(Insn, 23, 1) << 12;
3669 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Andersonb685c9f2011-08-11 21:34:58 +00003670
James Molloydb4ce602011-09-01 18:02:14 +00003671 if (Rn == 0xF || Rn == Rt) S = MCDisassembler::SoftFail;
Owen Anderson3987a612011-08-12 18:12:39 +00003672
Owen Anderson03aadae2011-09-01 23:23:50 +00003673 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3674 return MCDisassembler::Fail;
3675 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
3676 return MCDisassembler::Fail;
3677 if (!Check(S, DecodeAddrModeImm12Operand(Inst, imm, Address, Decoder)))
3678 return MCDisassembler::Fail;
3679 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3680 return MCDisassembler::Fail;
Owen Anderson3987a612011-08-12 18:12:39 +00003681
Owen Andersona4043c42011-08-17 17:44:15 +00003682 return S;
Owen Anderson3987a612011-08-12 18:12:39 +00003683}
3684
Craig Topperf6e7e122012-03-27 07:21:54 +00003685static DecodeStatus DecodeSTRPreReg(MCInst &Inst, unsigned Insn,
Owen Anderson3987a612011-08-12 18:12:39 +00003686 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003687 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003688
Jim Grosbachecaef492012-08-14 19:06:05 +00003689 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3690 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
3691 unsigned imm = fieldFromInstruction(Insn, 0, 12);
3692 imm |= fieldFromInstruction(Insn, 16, 4) << 13;
3693 imm |= fieldFromInstruction(Insn, 23, 1) << 12;
3694 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Anderson3987a612011-08-12 18:12:39 +00003695
James Molloydb4ce602011-09-01 18:02:14 +00003696 if (Rn == 0xF || Rn == Rt) S = MCDisassembler::SoftFail;
Owen Anderson3987a612011-08-12 18:12:39 +00003697
Owen Anderson03aadae2011-09-01 23:23:50 +00003698 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3699 return MCDisassembler::Fail;
3700 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
3701 return MCDisassembler::Fail;
3702 if (!Check(S, DecodeSORegMemOperand(Inst, imm, Address, Decoder)))
3703 return MCDisassembler::Fail;
3704 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3705 return MCDisassembler::Fail;
Owen Anderson3987a612011-08-12 18:12:39 +00003706
Owen Andersona4043c42011-08-17 17:44:15 +00003707 return S;
Owen Anderson3987a612011-08-12 18:12:39 +00003708}
Owen Andersonb9d82f42011-08-15 18:44:44 +00003709
Craig Topperf6e7e122012-03-27 07:21:54 +00003710static DecodeStatus DecodeVLD1LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00003711 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003712 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003713
Jim Grosbachecaef492012-08-14 19:06:05 +00003714 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3715 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
3716 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
3717 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
3718 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003719
3720 unsigned align = 0;
3721 unsigned index = 0;
3722 switch (size) {
3723 default:
James Molloydb4ce602011-09-01 18:02:14 +00003724 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003725 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00003726 if (fieldFromInstruction(Insn, 4, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003727 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003728 index = fieldFromInstruction(Insn, 5, 3);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003729 break;
3730 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00003731 if (fieldFromInstruction(Insn, 5, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003732 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003733 index = fieldFromInstruction(Insn, 6, 2);
3734 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003735 align = 2;
3736 break;
3737 case 2:
Jim Grosbachecaef492012-08-14 19:06:05 +00003738 if (fieldFromInstruction(Insn, 6, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003739 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003740 index = fieldFromInstruction(Insn, 7, 1);
Tim Northoverfb3cdd82012-09-06 15:17:49 +00003741
3742 switch (fieldFromInstruction(Insn, 4, 2)) {
3743 case 0 :
3744 align = 0; break;
3745 case 3:
3746 align = 4; break;
3747 default:
3748 return MCDisassembler::Fail;
3749 }
3750 break;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003751 }
3752
Owen Anderson03aadae2011-09-01 23:23:50 +00003753 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3754 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003755 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00003756 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3757 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003758 }
Owen Anderson03aadae2011-09-01 23:23:50 +00003759 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3760 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003761 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00003762 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00003763 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003764 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
3765 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00003766 } else
Owen Anderson721c3702011-08-22 18:42:13 +00003767 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00003768 }
3769
Owen Anderson03aadae2011-09-01 23:23:50 +00003770 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3771 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003772 Inst.addOperand(MCOperand::CreateImm(index));
3773
Owen Andersona4043c42011-08-17 17:44:15 +00003774 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003775}
3776
Craig Topperf6e7e122012-03-27 07:21:54 +00003777static DecodeStatus DecodeVST1LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00003778 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003779 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003780
Jim Grosbachecaef492012-08-14 19:06:05 +00003781 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3782 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
3783 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
3784 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
3785 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003786
3787 unsigned align = 0;
3788 unsigned index = 0;
3789 switch (size) {
3790 default:
James Molloydb4ce602011-09-01 18:02:14 +00003791 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003792 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00003793 if (fieldFromInstruction(Insn, 4, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003794 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003795 index = fieldFromInstruction(Insn, 5, 3);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003796 break;
3797 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00003798 if (fieldFromInstruction(Insn, 5, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003799 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003800 index = fieldFromInstruction(Insn, 6, 2);
3801 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003802 align = 2;
3803 break;
3804 case 2:
Jim Grosbachecaef492012-08-14 19:06:05 +00003805 if (fieldFromInstruction(Insn, 6, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003806 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003807 index = fieldFromInstruction(Insn, 7, 1);
Tim Northoverfb3cdd82012-09-06 15:17:49 +00003808
3809 switch (fieldFromInstruction(Insn, 4, 2)) {
3810 case 0:
3811 align = 0; break;
3812 case 3:
3813 align = 4; break;
3814 default:
3815 return MCDisassembler::Fail;
3816 }
3817 break;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003818 }
3819
3820 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00003821 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3822 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003823 }
Owen Anderson03aadae2011-09-01 23:23:50 +00003824 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3825 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003826 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00003827 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00003828 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003829 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
3830 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00003831 } else
Owen Anderson721c3702011-08-22 18:42:13 +00003832 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00003833 }
3834
Owen Anderson03aadae2011-09-01 23:23:50 +00003835 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3836 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003837 Inst.addOperand(MCOperand::CreateImm(index));
3838
Owen Andersona4043c42011-08-17 17:44:15 +00003839 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003840}
3841
3842
Craig Topperf6e7e122012-03-27 07:21:54 +00003843static DecodeStatus DecodeVLD2LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00003844 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003845 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003846
Jim Grosbachecaef492012-08-14 19:06:05 +00003847 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3848 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
3849 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
3850 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
3851 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003852
3853 unsigned align = 0;
3854 unsigned index = 0;
3855 unsigned inc = 1;
3856 switch (size) {
3857 default:
James Molloydb4ce602011-09-01 18:02:14 +00003858 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003859 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00003860 index = fieldFromInstruction(Insn, 5, 3);
3861 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003862 align = 2;
3863 break;
3864 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00003865 index = fieldFromInstruction(Insn, 6, 2);
3866 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003867 align = 4;
Jim Grosbachecaef492012-08-14 19:06:05 +00003868 if (fieldFromInstruction(Insn, 5, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003869 inc = 2;
3870 break;
3871 case 2:
Jim Grosbachecaef492012-08-14 19:06:05 +00003872 if (fieldFromInstruction(Insn, 5, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003873 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003874 index = fieldFromInstruction(Insn, 7, 1);
3875 if (fieldFromInstruction(Insn, 4, 1) != 0)
Owen Andersonb9d82f42011-08-15 18:44:44 +00003876 align = 8;
Jim Grosbachecaef492012-08-14 19:06:05 +00003877 if (fieldFromInstruction(Insn, 6, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003878 inc = 2;
3879 break;
3880 }
3881
Owen Anderson03aadae2011-09-01 23:23:50 +00003882 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3883 return MCDisassembler::Fail;
3884 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
3885 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003886 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00003887 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3888 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003889 }
Owen Anderson03aadae2011-09-01 23:23:50 +00003890 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3891 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003892 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00003893 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00003894 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003895 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
3896 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00003897 } else
Owen Anderson721c3702011-08-22 18:42:13 +00003898 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00003899 }
3900
Owen Anderson03aadae2011-09-01 23:23:50 +00003901 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3902 return MCDisassembler::Fail;
3903 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
3904 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003905 Inst.addOperand(MCOperand::CreateImm(index));
3906
Owen Andersona4043c42011-08-17 17:44:15 +00003907 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003908}
3909
Craig Topperf6e7e122012-03-27 07:21:54 +00003910static DecodeStatus DecodeVST2LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00003911 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003912 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003913
Jim Grosbachecaef492012-08-14 19:06:05 +00003914 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3915 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
3916 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
3917 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
3918 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003919
3920 unsigned align = 0;
3921 unsigned index = 0;
3922 unsigned inc = 1;
3923 switch (size) {
3924 default:
James Molloydb4ce602011-09-01 18:02:14 +00003925 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003926 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00003927 index = fieldFromInstruction(Insn, 5, 3);
3928 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003929 align = 2;
3930 break;
3931 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00003932 index = fieldFromInstruction(Insn, 6, 2);
3933 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003934 align = 4;
Jim Grosbachecaef492012-08-14 19:06:05 +00003935 if (fieldFromInstruction(Insn, 5, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003936 inc = 2;
3937 break;
3938 case 2:
Jim Grosbachecaef492012-08-14 19:06:05 +00003939 if (fieldFromInstruction(Insn, 5, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003940 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003941 index = fieldFromInstruction(Insn, 7, 1);
3942 if (fieldFromInstruction(Insn, 4, 1) != 0)
Owen Andersonb9d82f42011-08-15 18:44:44 +00003943 align = 8;
Jim Grosbachecaef492012-08-14 19:06:05 +00003944 if (fieldFromInstruction(Insn, 6, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003945 inc = 2;
3946 break;
3947 }
3948
3949 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00003950 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3951 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003952 }
Owen Anderson03aadae2011-09-01 23:23:50 +00003953 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3954 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003955 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00003956 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00003957 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003958 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
3959 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00003960 } else
Owen Anderson721c3702011-08-22 18:42:13 +00003961 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00003962 }
3963
Owen Anderson03aadae2011-09-01 23:23:50 +00003964 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3965 return MCDisassembler::Fail;
3966 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
3967 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003968 Inst.addOperand(MCOperand::CreateImm(index));
3969
Owen Andersona4043c42011-08-17 17:44:15 +00003970 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003971}
3972
3973
Craig Topperf6e7e122012-03-27 07:21:54 +00003974static DecodeStatus DecodeVLD3LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00003975 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003976 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003977
Jim Grosbachecaef492012-08-14 19:06:05 +00003978 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3979 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
3980 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
3981 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
3982 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003983
3984 unsigned align = 0;
3985 unsigned index = 0;
3986 unsigned inc = 1;
3987 switch (size) {
3988 default:
James Molloydb4ce602011-09-01 18:02:14 +00003989 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003990 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00003991 if (fieldFromInstruction(Insn, 4, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003992 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003993 index = fieldFromInstruction(Insn, 5, 3);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003994 break;
3995 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00003996 if (fieldFromInstruction(Insn, 4, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003997 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003998 index = fieldFromInstruction(Insn, 6, 2);
3999 if (fieldFromInstruction(Insn, 5, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004000 inc = 2;
4001 break;
4002 case 2:
Jim Grosbachecaef492012-08-14 19:06:05 +00004003 if (fieldFromInstruction(Insn, 4, 2))
James Molloydb4ce602011-09-01 18:02:14 +00004004 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00004005 index = fieldFromInstruction(Insn, 7, 1);
4006 if (fieldFromInstruction(Insn, 6, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004007 inc = 2;
4008 break;
4009 }
4010
Owen Anderson03aadae2011-09-01 23:23:50 +00004011 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
4012 return MCDisassembler::Fail;
4013 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
4014 return MCDisassembler::Fail;
4015 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
4016 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004017
4018 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00004019 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4020 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004021 }
Owen Anderson03aadae2011-09-01 23:23:50 +00004022 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4023 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004024 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson2fa06a72011-08-30 22:58:27 +00004025 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00004026 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004027 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
4028 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00004029 } else
Owen Anderson721c3702011-08-22 18:42:13 +00004030 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00004031 }
4032
Owen Anderson03aadae2011-09-01 23:23:50 +00004033 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
4034 return MCDisassembler::Fail;
4035 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
4036 return MCDisassembler::Fail;
4037 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
4038 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004039 Inst.addOperand(MCOperand::CreateImm(index));
4040
Owen Andersona4043c42011-08-17 17:44:15 +00004041 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004042}
4043
Craig Topperf6e7e122012-03-27 07:21:54 +00004044static DecodeStatus DecodeVST3LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00004045 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004046 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00004047
Jim Grosbachecaef492012-08-14 19:06:05 +00004048 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
4049 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
4050 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
4051 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
4052 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004053
4054 unsigned align = 0;
4055 unsigned index = 0;
4056 unsigned inc = 1;
4057 switch (size) {
4058 default:
James Molloydb4ce602011-09-01 18:02:14 +00004059 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004060 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00004061 if (fieldFromInstruction(Insn, 4, 1))
James Molloydb4ce602011-09-01 18:02:14 +00004062 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00004063 index = fieldFromInstruction(Insn, 5, 3);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004064 break;
4065 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00004066 if (fieldFromInstruction(Insn, 4, 1))
James Molloydb4ce602011-09-01 18:02:14 +00004067 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00004068 index = fieldFromInstruction(Insn, 6, 2);
4069 if (fieldFromInstruction(Insn, 5, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004070 inc = 2;
4071 break;
4072 case 2:
Jim Grosbachecaef492012-08-14 19:06:05 +00004073 if (fieldFromInstruction(Insn, 4, 2))
James Molloydb4ce602011-09-01 18:02:14 +00004074 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00004075 index = fieldFromInstruction(Insn, 7, 1);
4076 if (fieldFromInstruction(Insn, 6, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004077 inc = 2;
4078 break;
4079 }
4080
4081 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00004082 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4083 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004084 }
Owen Anderson03aadae2011-09-01 23:23:50 +00004085 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4086 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004087 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00004088 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00004089 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004090 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
4091 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00004092 } else
Owen Anderson721c3702011-08-22 18:42:13 +00004093 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00004094 }
4095
Owen Anderson03aadae2011-09-01 23:23:50 +00004096 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
4097 return MCDisassembler::Fail;
4098 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
4099 return MCDisassembler::Fail;
4100 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
4101 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004102 Inst.addOperand(MCOperand::CreateImm(index));
4103
Owen Andersona4043c42011-08-17 17:44:15 +00004104 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004105}
4106
4107
Craig Topperf6e7e122012-03-27 07:21:54 +00004108static DecodeStatus DecodeVLD4LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00004109 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004110 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00004111
Jim Grosbachecaef492012-08-14 19:06:05 +00004112 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
4113 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
4114 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
4115 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
4116 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004117
4118 unsigned align = 0;
4119 unsigned index = 0;
4120 unsigned inc = 1;
4121 switch (size) {
4122 default:
James Molloydb4ce602011-09-01 18:02:14 +00004123 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004124 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00004125 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004126 align = 4;
Jim Grosbachecaef492012-08-14 19:06:05 +00004127 index = fieldFromInstruction(Insn, 5, 3);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004128 break;
4129 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00004130 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004131 align = 8;
Jim Grosbachecaef492012-08-14 19:06:05 +00004132 index = fieldFromInstruction(Insn, 6, 2);
4133 if (fieldFromInstruction(Insn, 5, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004134 inc = 2;
4135 break;
4136 case 2:
Tim Northoverfb3cdd82012-09-06 15:17:49 +00004137 switch (fieldFromInstruction(Insn, 4, 2)) {
4138 case 0:
4139 align = 0; break;
4140 case 3:
4141 return MCDisassembler::Fail;
4142 default:
4143 align = 4 << fieldFromInstruction(Insn, 4, 2); break;
4144 }
4145
Jim Grosbachecaef492012-08-14 19:06:05 +00004146 index = fieldFromInstruction(Insn, 7, 1);
4147 if (fieldFromInstruction(Insn, 6, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004148 inc = 2;
4149 break;
4150 }
4151
Owen Anderson03aadae2011-09-01 23:23:50 +00004152 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
4153 return MCDisassembler::Fail;
4154 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
4155 return MCDisassembler::Fail;
4156 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
4157 return MCDisassembler::Fail;
4158 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+3*inc, Address, Decoder)))
4159 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004160
4161 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00004162 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4163 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004164 }
Owen Anderson03aadae2011-09-01 23:23:50 +00004165 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4166 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004167 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00004168 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00004169 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004170 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
4171 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00004172 } else
Owen Anderson721c3702011-08-22 18:42:13 +00004173 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00004174 }
4175
Owen Anderson03aadae2011-09-01 23:23:50 +00004176 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
4177 return MCDisassembler::Fail;
4178 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
4179 return MCDisassembler::Fail;
4180 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
4181 return MCDisassembler::Fail;
4182 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+3*inc, Address, Decoder)))
4183 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004184 Inst.addOperand(MCOperand::CreateImm(index));
4185
Owen Andersona4043c42011-08-17 17:44:15 +00004186 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004187}
4188
Craig Topperf6e7e122012-03-27 07:21:54 +00004189static DecodeStatus DecodeVST4LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00004190 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004191 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00004192
Jim Grosbachecaef492012-08-14 19:06:05 +00004193 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
4194 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
4195 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
4196 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
4197 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004198
4199 unsigned align = 0;
4200 unsigned index = 0;
4201 unsigned inc = 1;
4202 switch (size) {
4203 default:
James Molloydb4ce602011-09-01 18:02:14 +00004204 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004205 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00004206 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004207 align = 4;
Jim Grosbachecaef492012-08-14 19:06:05 +00004208 index = fieldFromInstruction(Insn, 5, 3);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004209 break;
4210 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00004211 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004212 align = 8;
Jim Grosbachecaef492012-08-14 19:06:05 +00004213 index = fieldFromInstruction(Insn, 6, 2);
4214 if (fieldFromInstruction(Insn, 5, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004215 inc = 2;
4216 break;
4217 case 2:
Tim Northoverfb3cdd82012-09-06 15:17:49 +00004218 switch (fieldFromInstruction(Insn, 4, 2)) {
4219 case 0:
4220 align = 0; break;
4221 case 3:
4222 return MCDisassembler::Fail;
4223 default:
4224 align = 4 << fieldFromInstruction(Insn, 4, 2); break;
4225 }
4226
Jim Grosbachecaef492012-08-14 19:06:05 +00004227 index = fieldFromInstruction(Insn, 7, 1);
4228 if (fieldFromInstruction(Insn, 6, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004229 inc = 2;
4230 break;
4231 }
4232
4233 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00004234 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4235 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004236 }
Owen Anderson03aadae2011-09-01 23:23:50 +00004237 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4238 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004239 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00004240 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00004241 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004242 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
4243 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00004244 } else
Owen Anderson721c3702011-08-22 18:42:13 +00004245 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00004246 }
4247
Owen Anderson03aadae2011-09-01 23:23:50 +00004248 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
4249 return MCDisassembler::Fail;
4250 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
4251 return MCDisassembler::Fail;
4252 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
4253 return MCDisassembler::Fail;
4254 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+3*inc, Address, Decoder)))
4255 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004256 Inst.addOperand(MCOperand::CreateImm(index));
4257
Owen Andersona4043c42011-08-17 17:44:15 +00004258 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004259}
4260
Craig Topperf6e7e122012-03-27 07:21:54 +00004261static DecodeStatus DecodeVMOVSRR(MCInst &Inst, unsigned Insn,
Owen Andersondf698b02011-08-22 20:27:12 +00004262 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004263 DecodeStatus S = MCDisassembler::Success;
Jim Grosbachecaef492012-08-14 19:06:05 +00004264 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
4265 unsigned Rt2 = fieldFromInstruction(Insn, 16, 4);
4266 unsigned Rm = fieldFromInstruction(Insn, 5, 1);
4267 unsigned pred = fieldFromInstruction(Insn, 28, 4);
4268 Rm |= fieldFromInstruction(Insn, 0, 4) << 1;
Owen Andersondf698b02011-08-22 20:27:12 +00004269
4270 if (Rt == 0xF || Rt2 == 0xF || Rm == 0x1F)
James Molloydb4ce602011-09-01 18:02:14 +00004271 S = MCDisassembler::SoftFail;
Owen Andersondf698b02011-08-22 20:27:12 +00004272
Owen Anderson03aadae2011-09-01 23:23:50 +00004273 if (!Check(S, DecodeSPRRegisterClass(Inst, Rm , Address, Decoder)))
4274 return MCDisassembler::Fail;
4275 if (!Check(S, DecodeSPRRegisterClass(Inst, Rm+1, Address, Decoder)))
4276 return MCDisassembler::Fail;
4277 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt , Address, Decoder)))
4278 return MCDisassembler::Fail;
4279 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt2 , Address, Decoder)))
4280 return MCDisassembler::Fail;
4281 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
4282 return MCDisassembler::Fail;
Owen Andersondf698b02011-08-22 20:27:12 +00004283
4284 return S;
4285}
4286
Craig Topperf6e7e122012-03-27 07:21:54 +00004287static DecodeStatus DecodeVMOVRRS(MCInst &Inst, unsigned Insn,
Owen Andersondf698b02011-08-22 20:27:12 +00004288 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004289 DecodeStatus S = MCDisassembler::Success;
Jim Grosbachecaef492012-08-14 19:06:05 +00004290 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
4291 unsigned Rt2 = fieldFromInstruction(Insn, 16, 4);
4292 unsigned Rm = fieldFromInstruction(Insn, 5, 1);
4293 unsigned pred = fieldFromInstruction(Insn, 28, 4);
4294 Rm |= fieldFromInstruction(Insn, 0, 4) << 1;
Owen Andersondf698b02011-08-22 20:27:12 +00004295
4296 if (Rt == 0xF || Rt2 == 0xF || Rm == 0x1F)
James Molloydb4ce602011-09-01 18:02:14 +00004297 S = MCDisassembler::SoftFail;
Owen Andersondf698b02011-08-22 20:27:12 +00004298
Owen Anderson03aadae2011-09-01 23:23:50 +00004299 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt , Address, Decoder)))
4300 return MCDisassembler::Fail;
4301 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt2 , Address, Decoder)))
4302 return MCDisassembler::Fail;
4303 if (!Check(S, DecodeSPRRegisterClass(Inst, Rm , Address, Decoder)))
4304 return MCDisassembler::Fail;
4305 if (!Check(S, DecodeSPRRegisterClass(Inst, Rm+1, Address, Decoder)))
4306 return MCDisassembler::Fail;
4307 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
4308 return MCDisassembler::Fail;
Owen Andersondf698b02011-08-22 20:27:12 +00004309
4310 return S;
4311}
Owen Andersoneb1367b2011-08-22 23:44:04 +00004312
Craig Topperf6e7e122012-03-27 07:21:54 +00004313static DecodeStatus DecodeIT(MCInst &Inst, unsigned Insn,
Owen Anderson2fa06a72011-08-30 22:58:27 +00004314 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004315 DecodeStatus S = MCDisassembler::Success;
Jim Grosbachecaef492012-08-14 19:06:05 +00004316 unsigned pred = fieldFromInstruction(Insn, 4, 4);
4317 unsigned mask = fieldFromInstruction(Insn, 0, 4);
Owen Anderson2fa06a72011-08-30 22:58:27 +00004318
4319 if (pred == 0xF) {
4320 pred = 0xE;
James Molloydb4ce602011-09-01 18:02:14 +00004321 S = MCDisassembler::SoftFail;
Owen Anderson52300412011-08-24 17:21:43 +00004322 }
4323
Richard Bartonf435b092012-04-27 08:42:59 +00004324 if (mask == 0x0) {
Owen Anderson2fa06a72011-08-30 22:58:27 +00004325 mask |= 0x8;
James Molloydb4ce602011-09-01 18:02:14 +00004326 S = MCDisassembler::SoftFail;
Owen Anderson37612a32011-08-24 22:40:22 +00004327 }
Owen Anderson2fa06a72011-08-30 22:58:27 +00004328
4329 Inst.addOperand(MCOperand::CreateImm(pred));
4330 Inst.addOperand(MCOperand::CreateImm(mask));
Owen Anderson37612a32011-08-24 22:40:22 +00004331 return S;
4332}
Jim Grosbach7db8d692011-09-08 22:07:06 +00004333
4334static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00004335DecodeT2LDRDPreInstruction(MCInst &Inst, unsigned Insn,
Jim Grosbach7db8d692011-09-08 22:07:06 +00004336 uint64_t Address, const void *Decoder) {
4337 DecodeStatus S = MCDisassembler::Success;
4338
Jim Grosbachecaef492012-08-14 19:06:05 +00004339 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
4340 unsigned Rt2 = fieldFromInstruction(Insn, 8, 4);
4341 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
4342 unsigned addr = fieldFromInstruction(Insn, 0, 8);
4343 unsigned W = fieldFromInstruction(Insn, 21, 1);
4344 unsigned U = fieldFromInstruction(Insn, 23, 1);
4345 unsigned P = fieldFromInstruction(Insn, 24, 1);
Jim Grosbach7db8d692011-09-08 22:07:06 +00004346 bool writeback = (W == 1) | (P == 0);
4347
4348 addr |= (U << 8) | (Rn << 9);
4349
4350 if (writeback && (Rn == Rt || Rn == Rt2))
4351 Check(S, MCDisassembler::SoftFail);
4352 if (Rt == Rt2)
4353 Check(S, MCDisassembler::SoftFail);
4354
4355 // Rt
4356 if (!Check(S, DecoderGPRRegisterClass(Inst, Rt, Address, Decoder)))
4357 return MCDisassembler::Fail;
4358 // Rt2
4359 if (!Check(S, DecoderGPRRegisterClass(Inst, Rt2, Address, Decoder)))
4360 return MCDisassembler::Fail;
4361 // Writeback operand
4362 if (!Check(S, DecoderGPRRegisterClass(Inst, Rn, Address, Decoder)))
4363 return MCDisassembler::Fail;
4364 // addr
4365 if (!Check(S, DecodeT2AddrModeImm8s4(Inst, addr, Address, Decoder)))
4366 return MCDisassembler::Fail;
4367
4368 return S;
4369}
4370
4371static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00004372DecodeT2STRDPreInstruction(MCInst &Inst, unsigned Insn,
Jim Grosbach7db8d692011-09-08 22:07:06 +00004373 uint64_t Address, const void *Decoder) {
4374 DecodeStatus S = MCDisassembler::Success;
4375
Jim Grosbachecaef492012-08-14 19:06:05 +00004376 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
4377 unsigned Rt2 = fieldFromInstruction(Insn, 8, 4);
4378 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
4379 unsigned addr = fieldFromInstruction(Insn, 0, 8);
4380 unsigned W = fieldFromInstruction(Insn, 21, 1);
4381 unsigned U = fieldFromInstruction(Insn, 23, 1);
4382 unsigned P = fieldFromInstruction(Insn, 24, 1);
Jim Grosbach7db8d692011-09-08 22:07:06 +00004383 bool writeback = (W == 1) | (P == 0);
4384
4385 addr |= (U << 8) | (Rn << 9);
4386
4387 if (writeback && (Rn == Rt || Rn == Rt2))
4388 Check(S, MCDisassembler::SoftFail);
4389
4390 // Writeback operand
4391 if (!Check(S, DecoderGPRRegisterClass(Inst, Rn, Address, Decoder)))
4392 return MCDisassembler::Fail;
4393 // Rt
4394 if (!Check(S, DecoderGPRRegisterClass(Inst, Rt, Address, Decoder)))
4395 return MCDisassembler::Fail;
4396 // Rt2
4397 if (!Check(S, DecoderGPRRegisterClass(Inst, Rt2, Address, Decoder)))
4398 return MCDisassembler::Fail;
4399 // addr
4400 if (!Check(S, DecodeT2AddrModeImm8s4(Inst, addr, Address, Decoder)))
4401 return MCDisassembler::Fail;
4402
4403 return S;
4404}
Owen Anderson5bfb0e02011-09-09 22:24:36 +00004405
Craig Topperf6e7e122012-03-27 07:21:54 +00004406static DecodeStatus DecodeT2Adr(MCInst &Inst, uint32_t Insn,
Owen Anderson5bfb0e02011-09-09 22:24:36 +00004407 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00004408 unsigned sign1 = fieldFromInstruction(Insn, 21, 1);
4409 unsigned sign2 = fieldFromInstruction(Insn, 23, 1);
Owen Anderson5bfb0e02011-09-09 22:24:36 +00004410 if (sign1 != sign2) return MCDisassembler::Fail;
4411
Jim Grosbachecaef492012-08-14 19:06:05 +00004412 unsigned Val = fieldFromInstruction(Insn, 0, 8);
4413 Val |= fieldFromInstruction(Insn, 12, 3) << 8;
4414 Val |= fieldFromInstruction(Insn, 26, 1) << 11;
Owen Anderson5bfb0e02011-09-09 22:24:36 +00004415 Val |= sign1 << 12;
4416 Inst.addOperand(MCOperand::CreateImm(SignExtend32<13>(Val)));
4417
4418 return MCDisassembler::Success;
4419}
4420
Craig Topperf6e7e122012-03-27 07:21:54 +00004421static DecodeStatus DecodeT2ShifterImmOperand(MCInst &Inst, uint32_t Val,
Owen Andersonf01e2de2011-09-26 21:06:22 +00004422 uint64_t Address,
4423 const void *Decoder) {
4424 DecodeStatus S = MCDisassembler::Success;
4425
4426 // Shift of "asr #32" is not allowed in Thumb2 mode.
4427 if (Val == 0x20) S = MCDisassembler::SoftFail;
4428 Inst.addOperand(MCOperand::CreateImm(Val));
4429 return S;
4430}
4431
Craig Topperf6e7e122012-03-27 07:21:54 +00004432static DecodeStatus DecodeSwap(MCInst &Inst, unsigned Insn,
Owen Andersondde461c2011-10-28 18:02:13 +00004433 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00004434 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
4435 unsigned Rt2 = fieldFromInstruction(Insn, 0, 4);
4436 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
4437 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Andersondde461c2011-10-28 18:02:13 +00004438
4439 if (pred == 0xF)
4440 return DecodeCPSInstruction(Inst, Insn, Address, Decoder);
4441
4442 DecodeStatus S = MCDisassembler::Success;
Silviu Barangaca45af92012-04-18 14:18:57 +00004443
4444 if (Rt == Rn || Rn == Rt2)
4445 S = MCDisassembler::SoftFail;
4446
Owen Andersondde461c2011-10-28 18:02:13 +00004447 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt, Address, Decoder)))
4448 return MCDisassembler::Fail;
4449 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt2, Address, Decoder)))
4450 return MCDisassembler::Fail;
4451 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
4452 return MCDisassembler::Fail;
4453 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
4454 return MCDisassembler::Fail;
4455
4456 return S;
4457}
Owen Anderson0ac90582011-11-15 19:55:00 +00004458
Craig Topperf6e7e122012-03-27 07:21:54 +00004459static DecodeStatus DecodeVCVTD(MCInst &Inst, unsigned Insn,
Owen Anderson0ac90582011-11-15 19:55:00 +00004460 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00004461 unsigned Vd = (fieldFromInstruction(Insn, 12, 4) << 0);
4462 Vd |= (fieldFromInstruction(Insn, 22, 1) << 4);
4463 unsigned Vm = (fieldFromInstruction(Insn, 0, 4) << 0);
4464 Vm |= (fieldFromInstruction(Insn, 5, 1) << 4);
4465 unsigned imm = fieldFromInstruction(Insn, 16, 6);
4466 unsigned cmode = fieldFromInstruction(Insn, 8, 4);
Owen Anderson0ac90582011-11-15 19:55:00 +00004467
4468 DecodeStatus S = MCDisassembler::Success;
4469
4470 // VMOVv2f32 is ambiguous with these decodings.
Owen Anderson05060f02011-11-15 20:30:41 +00004471 if (!(imm & 0x38) && cmode == 0xF) {
Owen Anderson0ac90582011-11-15 19:55:00 +00004472 Inst.setOpcode(ARM::VMOVv2f32);
4473 return DecodeNEONModImmInstruction(Inst, Insn, Address, Decoder);
4474 }
4475
Amaury de la Vieuvilleea7bb572013-06-08 13:29:11 +00004476 if (!(imm & 0x20)) return MCDisassembler::Fail;
Owen Anderson0ac90582011-11-15 19:55:00 +00004477
4478 if (!Check(S, DecodeDPRRegisterClass(Inst, Vd, Address, Decoder)))
4479 return MCDisassembler::Fail;
4480 if (!Check(S, DecodeDPRRegisterClass(Inst, Vm, Address, Decoder)))
4481 return MCDisassembler::Fail;
4482 Inst.addOperand(MCOperand::CreateImm(64 - imm));
4483
4484 return S;
4485}
4486
Craig Topperf6e7e122012-03-27 07:21:54 +00004487static DecodeStatus DecodeVCVTQ(MCInst &Inst, unsigned Insn,
Owen Anderson0ac90582011-11-15 19:55:00 +00004488 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00004489 unsigned Vd = (fieldFromInstruction(Insn, 12, 4) << 0);
4490 Vd |= (fieldFromInstruction(Insn, 22, 1) << 4);
4491 unsigned Vm = (fieldFromInstruction(Insn, 0, 4) << 0);
4492 Vm |= (fieldFromInstruction(Insn, 5, 1) << 4);
4493 unsigned imm = fieldFromInstruction(Insn, 16, 6);
4494 unsigned cmode = fieldFromInstruction(Insn, 8, 4);
Owen Anderson0ac90582011-11-15 19:55:00 +00004495
4496 DecodeStatus S = MCDisassembler::Success;
4497
4498 // VMOVv4f32 is ambiguous with these decodings.
4499 if (!(imm & 0x38) && cmode == 0xF) {
4500 Inst.setOpcode(ARM::VMOVv4f32);
4501 return DecodeNEONModImmInstruction(Inst, Insn, Address, Decoder);
4502 }
4503
Amaury de la Vieuvilleea7bb572013-06-08 13:29:11 +00004504 if (!(imm & 0x20)) return MCDisassembler::Fail;
Owen Anderson0ac90582011-11-15 19:55:00 +00004505
4506 if (!Check(S, DecodeQPRRegisterClass(Inst, Vd, Address, Decoder)))
4507 return MCDisassembler::Fail;
4508 if (!Check(S, DecodeQPRRegisterClass(Inst, Vm, Address, Decoder)))
4509 return MCDisassembler::Fail;
4510 Inst.addOperand(MCOperand::CreateImm(64 - imm));
4511
4512 return S;
4513}
Silviu Barangad213f212012-03-22 13:24:43 +00004514
Quentin Colombet6f03f622013-04-17 18:46:12 +00004515static DecodeStatus DecodeImm0_4(MCInst &Inst, unsigned Insn, uint64_t Address,
4516 const void *Decoder)
4517{
4518 unsigned Imm = fieldFromInstruction(Insn, 0, 3);
4519 if (Imm > 4) return MCDisassembler::Fail;
4520 Inst.addOperand(MCOperand::CreateImm(Imm));
4521 return MCDisassembler::Success;
4522}
4523
Craig Topperf6e7e122012-03-27 07:21:54 +00004524static DecodeStatus DecodeLDR(MCInst &Inst, unsigned Val,
Silviu Barangad213f212012-03-22 13:24:43 +00004525 uint64_t Address, const void *Decoder) {
4526 DecodeStatus S = MCDisassembler::Success;
4527
Jim Grosbachecaef492012-08-14 19:06:05 +00004528 unsigned Rn = fieldFromInstruction(Val, 16, 4);
4529 unsigned Rt = fieldFromInstruction(Val, 12, 4);
4530 unsigned Rm = fieldFromInstruction(Val, 0, 4);
4531 Rm |= (fieldFromInstruction(Val, 23, 1) << 4);
4532 unsigned Cond = fieldFromInstruction(Val, 28, 4);
Silviu Barangad213f212012-03-22 13:24:43 +00004533
Jim Grosbachecaef492012-08-14 19:06:05 +00004534 if (fieldFromInstruction(Val, 8, 4) != 0 || Rn == Rt)
Silviu Barangad213f212012-03-22 13:24:43 +00004535 S = MCDisassembler::SoftFail;
4536
4537 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt, Address, Decoder)))
4538 return MCDisassembler::Fail;
4539 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
4540 return MCDisassembler::Fail;
4541 if (!Check(S, DecodeAddrMode7Operand(Inst, Rn, Address, Decoder)))
4542 return MCDisassembler::Fail;
4543 if (!Check(S, DecodePostIdxReg(Inst, Rm, Address, Decoder)))
4544 return MCDisassembler::Fail;
4545 if (!Check(S, DecodePredicateOperand(Inst, Cond, Address, Decoder)))
4546 return MCDisassembler::Fail;
4547
4548 return S;
4549}
4550
Silviu Baranga41f1fcd2012-04-18 13:12:50 +00004551static DecodeStatus DecodeMRRC2(llvm::MCInst &Inst, unsigned Val,
4552 uint64_t Address, const void *Decoder) {
4553
4554 DecodeStatus S = MCDisassembler::Success;
4555
Jim Grosbachecaef492012-08-14 19:06:05 +00004556 unsigned CRm = fieldFromInstruction(Val, 0, 4);
4557 unsigned opc1 = fieldFromInstruction(Val, 4, 4);
4558 unsigned cop = fieldFromInstruction(Val, 8, 4);
4559 unsigned Rt = fieldFromInstruction(Val, 12, 4);
4560 unsigned Rt2 = fieldFromInstruction(Val, 16, 4);
Silviu Baranga41f1fcd2012-04-18 13:12:50 +00004561
4562 if ((cop & ~0x1) == 0xa)
4563 return MCDisassembler::Fail;
4564
4565 if (Rt == Rt2)
4566 S = MCDisassembler::SoftFail;
4567
4568 Inst.addOperand(MCOperand::CreateImm(cop));
4569 Inst.addOperand(MCOperand::CreateImm(opc1));
4570 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt, Address, Decoder)))
4571 return MCDisassembler::Fail;
4572 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt2, Address, Decoder)))
4573 return MCDisassembler::Fail;
4574 Inst.addOperand(MCOperand::CreateImm(CRm));
4575
4576 return S;
4577}
4578