blob: 4d25e15fcc97e728ec08514d46b23971bffd49fc [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);
Amaury de la Vieuville43cb13a2013-06-10 14:17:08 +0000282static DecodeStatus DecodeInstSyncBarrierOption(MCInst &Inst, unsigned Insn,
283 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000284static DecodeStatus DecodeMSRMask(MCInst &Inst, unsigned Insn,
Owen Anderson60663402011-08-11 20:21:46 +0000285 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000286static DecodeStatus DecodeDoubleRegLoad(MCInst &Inst, unsigned Insn,
Owen Andersonb685c9f2011-08-11 21:34:58 +0000287 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000288static DecodeStatus DecodeDoubleRegStore(MCInst &Inst, unsigned Insn,
Owen Andersonc5798a3a52011-08-12 17:58:32 +0000289 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000290static DecodeStatus DecodeLDRPreImm(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 DecodeLDRPreReg(MCInst &Inst, unsigned Insn,
Owen Anderson16d33f32011-08-26 20:43:14 +0000293 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000294static DecodeStatus DecodeSTRPreImm(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 DecodeSTRPreReg(MCInst &Inst, unsigned Insn,
Owen Anderson3987a612011-08-12 18:12:39 +0000297 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000298static DecodeStatus DecodeVLD1LN(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 DecodeVLD2LN(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 DecodeVLD3LN(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 DecodeVLD4LN(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 DecodeVST1LN(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 DecodeVST2LN(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 DecodeVST3LN(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 DecodeVST4LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +0000313 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000314static DecodeStatus DecodeVMOVSRR(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 DecodeVMOVRRS(MCInst &Inst, unsigned Insn,
Owen Andersondf698b02011-08-22 20:27:12 +0000317 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000318static DecodeStatus DecodeSwap(MCInst &Inst, unsigned Insn,
Owen Andersondde461c2011-10-28 18:02:13 +0000319 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000320static DecodeStatus DecodeVCVTD(MCInst &Inst, unsigned Insn,
Owen Anderson0ac90582011-11-15 19:55:00 +0000321 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000322static DecodeStatus DecodeVCVTQ(MCInst &Inst, unsigned Insn,
Owen Anderson0ac90582011-11-15 19:55:00 +0000323 uint64_t Address, const void *Decoder);
Quentin Colombet6f03f622013-04-17 18:46:12 +0000324static DecodeStatus DecodeImm0_4(MCInst &Inst, unsigned Insn, uint64_t Address,
325 const void *Decoder);
Owen Anderson0ac90582011-11-15 19:55:00 +0000326
Owen Andersone0152a72011-08-09 20:55:18 +0000327
Craig Topperf6e7e122012-03-27 07:21:54 +0000328static DecodeStatus DecodeThumbAddSpecialReg(MCInst &Inst, uint16_t Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000329 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000330static DecodeStatus DecodeThumbBROperand(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 DecodeT2BROperand(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 DecodeThumbCmpBROperand(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 DecodeThumbAddrModeRR(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 DecodeThumbAddrModeIS(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 DecodeThumbAddrModePC(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 DecodeThumbAddrModeSP(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 DecodeT2AddrModeSOReg(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 DecodeT2LoadShift(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 DecodeT2Imm8S4(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 DecodeT2AddrModeImm8s4(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000351 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000352static DecodeStatus DecodeT2AddrModeImm0_1020s4(MCInst &Inst,unsigned Val,
Jim Grosbacha05627e2011-09-09 18:37:27 +0000353 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000354static DecodeStatus DecodeT2Imm8(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 DecodeT2AddrModeImm8(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000357 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000358static DecodeStatus DecodeThumbAddSPImm(MCInst &Inst, uint16_t Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000359 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000360static DecodeStatus DecodeThumbAddSPReg(MCInst &Inst, uint16_t Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000361 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000362static DecodeStatus DecodeThumbCPS(MCInst &Inst, uint16_t Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000363 uint64_t Address, const void *Decoder);
Amaury de la Vieuville631df632013-06-08 13:38:52 +0000364static DecodeStatus DecodeQADDInstruction(MCInst &Inst, unsigned Insn,
365 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000366static DecodeStatus DecodeThumbBLXOffset(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +0000367 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000368static DecodeStatus DecodeT2AddrModeImm12(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000369 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000370static DecodeStatus DecodeThumbTableBranch(MCInst &Inst, unsigned Val,
Jim Grosbach05541f42011-09-19 22:21:13 +0000371 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000372static DecodeStatus DecodeThumb2BCCInstruction(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 DecodeT2SOImm(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 DecodeThumbBCCTargetOperand(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 DecodeThumbBLTargetOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +0000379 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000380static DecodeStatus DecodeIT(MCInst &Inst, unsigned Val,
Owen Anderson37612a32011-08-24 22:40:22 +0000381 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000382static DecodeStatus DecodeT2LDRDPreInstruction(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 DecodeT2STRDPreInstruction(MCInst &Inst,unsigned Insn,
Jim Grosbach7db8d692011-09-08 22:07:06 +0000385 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000386static DecodeStatus DecodeT2Adr(MCInst &Inst, unsigned Val,
Owen Anderson5bfb0e02011-09-09 22:24:36 +0000387 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000388static DecodeStatus DecodeT2LdStPre(MCInst &Inst, unsigned Val,
Owen Andersona9ebf6f2011-09-12 18:56:30 +0000389 uint64_t Address, const void *Decoder);
Craig Topperf6e7e122012-03-27 07:21:54 +0000390static DecodeStatus DecodeT2ShifterImmOperand(MCInst &Inst, unsigned Val,
Owen Andersonf01e2de2011-09-26 21:06:22 +0000391 uint64_t Address, const void *Decoder);
392
Craig Topperf6e7e122012-03-27 07:21:54 +0000393static DecodeStatus DecodeLDR(MCInst &Inst, unsigned Val,
Silviu Barangad213f212012-03-22 13:24:43 +0000394 uint64_t Address, const void *Decoder);
Silviu Baranga41f1fcd2012-04-18 13:12:50 +0000395static DecodeStatus DecodeMRRC2(llvm::MCInst &Inst, unsigned Val,
396 uint64_t Address, const void *Decoder);
Owen Andersone0152a72011-08-09 20:55:18 +0000397#include "ARMGenDisassemblerTables.inc"
Sean Callanan814e69b2010-04-13 21:21:57 +0000398
James Molloy4c493e82011-09-07 17:24:38 +0000399static MCDisassembler *createARMDisassembler(const Target &T, const MCSubtargetInfo &STI) {
400 return new ARMDisassembler(STI);
Johnny Chen7b999ea2010-04-02 22:27:38 +0000401}
402
James Molloy4c493e82011-09-07 17:24:38 +0000403static MCDisassembler *createThumbDisassembler(const Target &T, const MCSubtargetInfo &STI) {
404 return new ThumbDisassembler(STI);
Johnny Chen7b999ea2010-04-02 22:27:38 +0000405}
406
Owen Anderson03aadae2011-09-01 23:23:50 +0000407DecodeStatus ARMDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
Derek Schuff56b662c2012-02-29 01:09:06 +0000408 const MemoryObject &Region,
Jim Grosbachd14b70d2011-08-17 21:58:18 +0000409 uint64_t Address,
Owen Andersona0c3b972011-09-15 23:38:46 +0000410 raw_ostream &os,
411 raw_ostream &cs) const {
Kevin Enderby5dcda642011-10-04 22:44:48 +0000412 CommentStream = &cs;
413
Owen Andersone0152a72011-08-09 20:55:18 +0000414 uint8_t bytes[4];
415
James Molloy8067df92011-09-07 19:42:28 +0000416 assert(!(STI.getFeatureBits() & ARM::ModeThumb) &&
417 "Asked to disassemble an ARM instruction but Subtarget is in Thumb mode!");
418
Owen Andersone0152a72011-08-09 20:55:18 +0000419 // We want to read exactly 4 bytes of data.
Benjamin Kramer534d3a42013-05-24 10:54:58 +0000420 if (Region.readBytes(Address, 4, bytes) == -1) {
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000421 Size = 0;
James Molloydb4ce602011-09-01 18:02:14 +0000422 return MCDisassembler::Fail;
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000423 }
Owen Andersone0152a72011-08-09 20:55:18 +0000424
425 // Encoded as a small-endian 32-bit word in the stream.
426 uint32_t insn = (bytes[3] << 24) |
427 (bytes[2] << 16) |
428 (bytes[1] << 8) |
429 (bytes[0] << 0);
430
431 // Calling the auto-generated decoder function.
Jim Grosbachecaef492012-08-14 19:06:05 +0000432 DecodeStatus result = decodeInstruction(DecoderTableARM32, MI, insn,
433 Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000434 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000435 Size = 4;
Owen Andersona4043c42011-08-17 17:44:15 +0000436 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000437 }
438
Owen Andersone0152a72011-08-09 20:55:18 +0000439 // VFP and NEON instructions, similarly, are shared between ARM
440 // and Thumb modes.
441 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000442 result = decodeInstruction(DecoderTableVFP32, MI, insn, Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000443 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000444 Size = 4;
Owen Andersona4043c42011-08-17 17:44:15 +0000445 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000446 }
447
448 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000449 result = decodeInstruction(DecoderTableNEONData32, MI, insn, Address,
450 this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000451 if (result != MCDisassembler::Fail) {
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000452 Size = 4;
Owen Andersone0152a72011-08-09 20:55:18 +0000453 // Add a fake predicate operand, because we share these instruction
454 // definitions with Thumb2 where these instructions are predicable.
Owen Anderson03aadae2011-09-01 23:23:50 +0000455 if (!DecodePredicateOperand(MI, 0xE, Address, this))
456 return MCDisassembler::Fail;
Owen Andersona4043c42011-08-17 17:44:15 +0000457 return result;
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000458 }
459
460 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000461 result = decodeInstruction(DecoderTableNEONLoadStore32, MI, insn, Address,
462 this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000463 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000464 Size = 4;
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000465 // Add a fake predicate operand, because we share these instruction
466 // definitions with Thumb2 where these instructions are predicable.
Owen Anderson03aadae2011-09-01 23:23:50 +0000467 if (!DecodePredicateOperand(MI, 0xE, Address, this))
468 return MCDisassembler::Fail;
Owen Andersona4043c42011-08-17 17:44:15 +0000469 return result;
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000470 }
471
472 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000473 result = decodeInstruction(DecoderTableNEONDup32, MI, insn, Address,
474 this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000475 if (result != MCDisassembler::Fail) {
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000476 Size = 4;
477 // Add a fake predicate operand, because we share these instruction
478 // definitions with Thumb2 where these instructions are predicable.
Owen Anderson03aadae2011-09-01 23:23:50 +0000479 if (!DecodePredicateOperand(MI, 0xE, Address, this))
480 return MCDisassembler::Fail;
Owen Andersona4043c42011-08-17 17:44:15 +0000481 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000482 }
483
484 MI.clear();
485
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000486 Size = 0;
James Molloydb4ce602011-09-01 18:02:14 +0000487 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000488}
489
490namespace llvm {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000491extern const MCInstrDesc ARMInsts[];
Owen Andersone0152a72011-08-09 20:55:18 +0000492}
493
Kevin Enderby5dcda642011-10-04 22:44:48 +0000494/// tryAddingSymbolicOperand - trys to add a symbolic operand in place of the
495/// immediate Value in the MCInst. The immediate Value has had any PC
496/// adjustment made by the caller. If the instruction is a branch instruction
497/// then isBranch is true, else false. If the getOpInfo() function was set as
498/// part of the setupForSymbolicDisassembly() call then that function is called
499/// to get any symbolic information at the Address for this instruction. If
500/// that returns non-zero then the symbolic information it returns is used to
501/// create an MCExpr and that is added as an operand to the MCInst. If
502/// getOpInfo() returns zero and isBranch is true then a symbol look up for
503/// Value is done and if a symbol is found an MCExpr is created with that, else
504/// an MCExpr with Value is created. This function returns true if it adds an
505/// operand to the MCInst and false otherwise.
506static bool tryAddingSymbolicOperand(uint64_t Address, int32_t Value,
507 bool isBranch, uint64_t InstSize,
508 MCInst &MI, const void *Decoder) {
509 const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000510 // FIXME: Does it make sense for value to be negative?
511 return Dis->tryAddingSymbolicOperand(MI, (uint32_t)Value, Address, isBranch,
512 /* Offset */ 0, InstSize);
Kevin Enderby5dcda642011-10-04 22:44:48 +0000513}
514
515/// tryAddingPcLoadReferenceComment - trys to add a comment as to what is being
516/// referenced by a load instruction with the base register that is the Pc.
517/// These can often be values in a literal pool near the Address of the
518/// instruction. The Address of the instruction and its immediate Value are
519/// used as a possible literal pool entry. The SymbolLookUp call back will
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000520/// return the name of a symbol referenced by the literal pool's entry if
Kevin Enderby5dcda642011-10-04 22:44:48 +0000521/// the referenced address is that of a symbol. Or it will return a pointer to
522/// a literal 'C' string if the referenced address of the literal pool's entry
523/// is an address into a section with 'C' string literals.
524static void tryAddingPcLoadReferenceComment(uint64_t Address, int Value,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000525 const void *Decoder) {
Kevin Enderby5dcda642011-10-04 22:44:48 +0000526 const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000527 Dis->tryAddingPcLoadReferenceComment(Value, Address);
Kevin Enderby5dcda642011-10-04 22:44:48 +0000528}
529
Owen Andersone0152a72011-08-09 20:55:18 +0000530// Thumb1 instructions don't have explicit S bits. Rather, they
531// implicitly set CPSR. Since it's not represented in the encoding, the
532// auto-generated decoder won't inject the CPSR operand. We need to fix
533// that as a post-pass.
534static void AddThumb1SBit(MCInst &MI, bool InITBlock) {
535 const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
Owen Anderson187e1e42011-08-17 18:14:48 +0000536 unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
Owen Andersone0152a72011-08-09 20:55:18 +0000537 MCInst::iterator I = MI.begin();
Owen Anderson187e1e42011-08-17 18:14:48 +0000538 for (unsigned i = 0; i < NumOps; ++i, ++I) {
539 if (I == MI.end()) break;
Owen Andersone0152a72011-08-09 20:55:18 +0000540 if (OpInfo[i].isOptionalDef() && OpInfo[i].RegClass == ARM::CCRRegClassID) {
Owen Anderson187e1e42011-08-17 18:14:48 +0000541 if (i > 0 && OpInfo[i-1].isPredicate()) continue;
Owen Andersone0152a72011-08-09 20:55:18 +0000542 MI.insert(I, MCOperand::CreateReg(InITBlock ? 0 : ARM::CPSR));
543 return;
544 }
545 }
546
Owen Anderson187e1e42011-08-17 18:14:48 +0000547 MI.insert(I, MCOperand::CreateReg(InITBlock ? 0 : ARM::CPSR));
Owen Andersone0152a72011-08-09 20:55:18 +0000548}
549
550// Most Thumb instructions don't have explicit predicates in the
551// encoding, but rather get their predicates from IT context. We need
552// to fix up the predicate operands using this context information as a
553// post-pass.
Owen Anderson2fefa422011-09-08 22:42:49 +0000554MCDisassembler::DecodeStatus
555ThumbDisassembler::AddThumbPredicate(MCInst &MI) const {
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000556 MCDisassembler::DecodeStatus S = Success;
557
Owen Andersone0152a72011-08-09 20:55:18 +0000558 // A few instructions actually have predicates encoded in them. Don't
559 // try to overwrite it if we're seeing one of those.
560 switch (MI.getOpcode()) {
561 case ARM::tBcc:
562 case ARM::t2Bcc:
Owen Anderson2fefa422011-09-08 22:42:49 +0000563 case ARM::tCBZ:
564 case ARM::tCBNZ:
Owen Anderson61e46042011-09-19 23:47:10 +0000565 case ARM::tCPS:
566 case ARM::t2CPS3p:
567 case ARM::t2CPS2p:
568 case ARM::t2CPS1p:
Owen Anderson163be012011-09-19 23:57:20 +0000569 case ARM::tMOVSr:
Owen Anderson44f76ea2011-10-13 17:58:39 +0000570 case ARM::tSETEND:
Owen Anderson33d39532011-09-08 22:48:37 +0000571 // Some instructions (mostly conditional branches) are not
572 // allowed in IT blocks.
Richard Bartone9600002012-04-24 11:13:20 +0000573 if (ITBlock.instrInITBlock())
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000574 S = SoftFail;
575 else
576 return Success;
577 break;
578 case ARM::tB:
579 case ARM::t2B:
Owen Andersonf902d922011-09-19 22:34:23 +0000580 case ARM::t2TBB:
581 case ARM::t2TBH:
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000582 // Some instructions (mostly unconditional branches) can
583 // only appears at the end of, or outside of, an IT.
Richard Bartone9600002012-04-24 11:13:20 +0000584 if (ITBlock.instrInITBlock() && !ITBlock.instrLastInITBlock())
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000585 S = SoftFail;
Owen Anderson2fefa422011-09-08 22:42:49 +0000586 break;
Owen Andersone0152a72011-08-09 20:55:18 +0000587 default:
588 break;
589 }
590
591 // If we're in an IT block, base the predicate on that. Otherwise,
592 // assume a predicate of AL.
593 unsigned CC;
Richard Bartone9600002012-04-24 11:13:20 +0000594 CC = ITBlock.getITCC();
595 if (CC == 0xF)
Owen Andersone0152a72011-08-09 20:55:18 +0000596 CC = ARMCC::AL;
Richard Bartone9600002012-04-24 11:13:20 +0000597 if (ITBlock.instrInITBlock())
598 ITBlock.advanceITState();
Owen Andersone0152a72011-08-09 20:55:18 +0000599
600 const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
Owen Anderson187e1e42011-08-17 18:14:48 +0000601 unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
Owen Andersone0152a72011-08-09 20:55:18 +0000602 MCInst::iterator I = MI.begin();
Owen Anderson187e1e42011-08-17 18:14:48 +0000603 for (unsigned i = 0; i < NumOps; ++i, ++I) {
604 if (I == MI.end()) break;
Owen Andersone0152a72011-08-09 20:55:18 +0000605 if (OpInfo[i].isPredicate()) {
606 I = MI.insert(I, MCOperand::CreateImm(CC));
607 ++I;
608 if (CC == ARMCC::AL)
609 MI.insert(I, MCOperand::CreateReg(0));
610 else
611 MI.insert(I, MCOperand::CreateReg(ARM::CPSR));
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000612 return S;
Owen Andersone0152a72011-08-09 20:55:18 +0000613 }
614 }
615
Owen Anderson187e1e42011-08-17 18:14:48 +0000616 I = MI.insert(I, MCOperand::CreateImm(CC));
617 ++I;
Owen Andersone0152a72011-08-09 20:55:18 +0000618 if (CC == ARMCC::AL)
Owen Anderson187e1e42011-08-17 18:14:48 +0000619 MI.insert(I, MCOperand::CreateReg(0));
Owen Andersone0152a72011-08-09 20:55:18 +0000620 else
Owen Anderson187e1e42011-08-17 18:14:48 +0000621 MI.insert(I, MCOperand::CreateReg(ARM::CPSR));
Owen Anderson2fefa422011-09-08 22:42:49 +0000622
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000623 return S;
Owen Andersone0152a72011-08-09 20:55:18 +0000624}
625
626// Thumb VFP instructions are a special case. Because we share their
627// encodings between ARM and Thumb modes, and they are predicable in ARM
628// mode, the auto-generated decoder will give them an (incorrect)
629// predicate operand. We need to rewrite these operands based on the IT
630// context as a post-pass.
631void ThumbDisassembler::UpdateThumbVFPPredicate(MCInst &MI) const {
632 unsigned CC;
Richard Bartone9600002012-04-24 11:13:20 +0000633 CC = ITBlock.getITCC();
634 if (ITBlock.instrInITBlock())
635 ITBlock.advanceITState();
Owen Andersone0152a72011-08-09 20:55:18 +0000636
637 const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
638 MCInst::iterator I = MI.begin();
Owen Anderson216cfaa2011-08-24 21:35:46 +0000639 unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
640 for (unsigned i = 0; i < NumOps; ++i, ++I) {
Owen Andersone0152a72011-08-09 20:55:18 +0000641 if (OpInfo[i].isPredicate() ) {
642 I->setImm(CC);
643 ++I;
644 if (CC == ARMCC::AL)
645 I->setReg(0);
646 else
647 I->setReg(ARM::CPSR);
648 return;
649 }
650 }
651}
652
Owen Anderson03aadae2011-09-01 23:23:50 +0000653DecodeStatus ThumbDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
Derek Schuff56b662c2012-02-29 01:09:06 +0000654 const MemoryObject &Region,
Jim Grosbachd14b70d2011-08-17 21:58:18 +0000655 uint64_t Address,
Owen Andersona0c3b972011-09-15 23:38:46 +0000656 raw_ostream &os,
657 raw_ostream &cs) const {
Kevin Enderby5dcda642011-10-04 22:44:48 +0000658 CommentStream = &cs;
659
Owen Andersone0152a72011-08-09 20:55:18 +0000660 uint8_t bytes[4];
661
James Molloy8067df92011-09-07 19:42:28 +0000662 assert((STI.getFeatureBits() & ARM::ModeThumb) &&
663 "Asked to disassemble in Thumb mode but Subtarget is in ARM mode!");
664
Owen Andersone0152a72011-08-09 20:55:18 +0000665 // We want to read exactly 2 bytes of data.
Benjamin Kramer534d3a42013-05-24 10:54:58 +0000666 if (Region.readBytes(Address, 2, bytes) == -1) {
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000667 Size = 0;
James Molloydb4ce602011-09-01 18:02:14 +0000668 return MCDisassembler::Fail;
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000669 }
Owen Andersone0152a72011-08-09 20:55:18 +0000670
671 uint16_t insn16 = (bytes[1] << 8) | bytes[0];
Jim Grosbachecaef492012-08-14 19:06:05 +0000672 DecodeStatus result = decodeInstruction(DecoderTableThumb16, MI, insn16,
673 Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000674 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000675 Size = 2;
Owen Anderson2fefa422011-09-08 22:42:49 +0000676 Check(result, AddThumbPredicate(MI));
Owen Andersona4043c42011-08-17 17:44:15 +0000677 return result;
Owen Anderson91a8f9b2011-08-16 23:45:44 +0000678 }
679
680 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000681 result = decodeInstruction(DecoderTableThumbSBit16, MI, insn16,
682 Address, this, STI);
Owen Anderson91a8f9b2011-08-16 23:45:44 +0000683 if (result) {
684 Size = 2;
Richard Bartone9600002012-04-24 11:13:20 +0000685 bool InITBlock = ITBlock.instrInITBlock();
Owen Anderson2fefa422011-09-08 22:42:49 +0000686 Check(result, AddThumbPredicate(MI));
Owen Andersone0152a72011-08-09 20:55:18 +0000687 AddThumb1SBit(MI, InITBlock);
Owen Andersona4043c42011-08-17 17:44:15 +0000688 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000689 }
690
691 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000692 result = decodeInstruction(DecoderTableThumb216, MI, insn16,
693 Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000694 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000695 Size = 2;
Owen Anderson6a5c1502011-10-06 23:33:11 +0000696
697 // Nested IT blocks are UNPREDICTABLE. Must be checked before we add
698 // the Thumb predicate.
Richard Bartone9600002012-04-24 11:13:20 +0000699 if (MI.getOpcode() == ARM::t2IT && ITBlock.instrInITBlock())
Owen Anderson6a5c1502011-10-06 23:33:11 +0000700 result = MCDisassembler::SoftFail;
701
Owen Anderson2fefa422011-09-08 22:42:49 +0000702 Check(result, AddThumbPredicate(MI));
Owen Andersone0152a72011-08-09 20:55:18 +0000703
704 // If we find an IT instruction, we need to parse its condition
705 // code and mask operands so that we can apply them correctly
706 // to the subsequent instructions.
707 if (MI.getOpcode() == ARM::t2IT) {
Owen Andersonf1e38442011-09-14 21:06:21 +0000708
Richard Bartone9600002012-04-24 11:13:20 +0000709 unsigned Firstcond = MI.getOperand(0).getImm();
Owen Anderson2fa06a72011-08-30 22:58:27 +0000710 unsigned Mask = MI.getOperand(1).getImm();
Richard Bartone9600002012-04-24 11:13:20 +0000711 ITBlock.setITState(Firstcond, Mask);
Owen Andersone0152a72011-08-09 20:55:18 +0000712 }
713
Owen Andersona4043c42011-08-17 17:44:15 +0000714 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000715 }
716
717 // We want to read exactly 4 bytes of data.
Benjamin Kramer534d3a42013-05-24 10:54:58 +0000718 if (Region.readBytes(Address, 4, bytes) == -1) {
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000719 Size = 0;
James Molloydb4ce602011-09-01 18:02:14 +0000720 return MCDisassembler::Fail;
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000721 }
Owen Andersone0152a72011-08-09 20:55:18 +0000722
723 uint32_t insn32 = (bytes[3] << 8) |
724 (bytes[2] << 0) |
725 (bytes[1] << 24) |
726 (bytes[0] << 16);
727 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000728 result = decodeInstruction(DecoderTableThumb32, MI, insn32, Address,
729 this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000730 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000731 Size = 4;
Richard Bartone9600002012-04-24 11:13:20 +0000732 bool InITBlock = ITBlock.instrInITBlock();
Owen Anderson2fefa422011-09-08 22:42:49 +0000733 Check(result, AddThumbPredicate(MI));
Owen Andersone0152a72011-08-09 20:55:18 +0000734 AddThumb1SBit(MI, InITBlock);
Owen Andersona4043c42011-08-17 17:44:15 +0000735 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000736 }
737
738 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000739 result = decodeInstruction(DecoderTableThumb232, MI, insn32, Address,
740 this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000741 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000742 Size = 4;
Owen Anderson2fefa422011-09-08 22:42:49 +0000743 Check(result, AddThumbPredicate(MI));
Owen Andersona4043c42011-08-17 17:44:15 +0000744 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000745 }
746
747 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000748 result = decodeInstruction(DecoderTableVFP32, MI, insn32, Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000749 if (result != MCDisassembler::Fail) {
Owen Andersone0152a72011-08-09 20:55:18 +0000750 Size = 4;
751 UpdateThumbVFPPredicate(MI);
Owen Andersona4043c42011-08-17 17:44:15 +0000752 return result;
Owen Andersone0152a72011-08-09 20:55:18 +0000753 }
754
755 MI.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +0000756 result = decodeInstruction(DecoderTableNEONDup32, MI, insn32, Address,
757 this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000758 if (result != MCDisassembler::Fail) {
Owen Andersona6201f02011-08-15 23:38:54 +0000759 Size = 4;
Owen Anderson2fefa422011-09-08 22:42:49 +0000760 Check(result, AddThumbPredicate(MI));
Owen Andersona4043c42011-08-17 17:44:15 +0000761 return result;
Owen Andersona6201f02011-08-15 23:38:54 +0000762 }
763
Jim Grosbachecaef492012-08-14 19:06:05 +0000764 if (fieldFromInstruction(insn32, 24, 8) == 0xF9) {
Owen Andersona6201f02011-08-15 23:38:54 +0000765 MI.clear();
766 uint32_t NEONLdStInsn = insn32;
767 NEONLdStInsn &= 0xF0FFFFFF;
768 NEONLdStInsn |= 0x04000000;
Jim Grosbachecaef492012-08-14 19:06:05 +0000769 result = decodeInstruction(DecoderTableNEONLoadStore32, MI, NEONLdStInsn,
770 Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000771 if (result != MCDisassembler::Fail) {
Owen Andersona6201f02011-08-15 23:38:54 +0000772 Size = 4;
Owen Anderson2fefa422011-09-08 22:42:49 +0000773 Check(result, AddThumbPredicate(MI));
Owen Andersona4043c42011-08-17 17:44:15 +0000774 return result;
Owen Andersona6201f02011-08-15 23:38:54 +0000775 }
776 }
777
Jim Grosbachecaef492012-08-14 19:06:05 +0000778 if (fieldFromInstruction(insn32, 24, 4) == 0xF) {
Owen Andersona6201f02011-08-15 23:38:54 +0000779 MI.clear();
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000780 uint32_t NEONDataInsn = insn32;
781 NEONDataInsn &= 0xF0FFFFFF; // Clear bits 27-24
782 NEONDataInsn |= (NEONDataInsn & 0x10000000) >> 4; // Move bit 28 to bit 24
783 NEONDataInsn |= 0x12000000; // Set bits 28 and 25
Jim Grosbachecaef492012-08-14 19:06:05 +0000784 result = decodeInstruction(DecoderTableNEONData32, MI, NEONDataInsn,
785 Address, this, STI);
James Molloydb4ce602011-09-01 18:02:14 +0000786 if (result != MCDisassembler::Fail) {
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000787 Size = 4;
Owen Anderson2fefa422011-09-08 22:42:49 +0000788 Check(result, AddThumbPredicate(MI));
Owen Andersona4043c42011-08-17 17:44:15 +0000789 return result;
Owen Andersonc86a5bd2011-08-10 19:01:10 +0000790 }
791 }
792
Benjamin Krameraa38dba2011-08-26 18:21:36 +0000793 Size = 0;
James Molloydb4ce602011-09-01 18:02:14 +0000794 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000795}
796
797
798extern "C" void LLVMInitializeARMDisassembler() {
799 TargetRegistry::RegisterMCDisassembler(TheARMTarget,
800 createARMDisassembler);
801 TargetRegistry::RegisterMCDisassembler(TheThumbTarget,
802 createThumbDisassembler);
803}
804
Craig Topperca658c22012-03-11 07:16:55 +0000805static const uint16_t GPRDecoderTable[] = {
Owen Andersone0152a72011-08-09 20:55:18 +0000806 ARM::R0, ARM::R1, ARM::R2, ARM::R3,
807 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
808 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
809 ARM::R12, ARM::SP, ARM::LR, ARM::PC
810};
811
Craig Topperf6e7e122012-03-27 07:21:54 +0000812static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000813 uint64_t Address, const void *Decoder) {
814 if (RegNo > 15)
James Molloydb4ce602011-09-01 18:02:14 +0000815 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000816
817 unsigned Register = GPRDecoderTable[RegNo];
818 Inst.addOperand(MCOperand::CreateReg(Register));
James Molloydb4ce602011-09-01 18:02:14 +0000819 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +0000820}
821
Owen Anderson03aadae2011-09-01 23:23:50 +0000822static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +0000823DecodeGPRnopcRegisterClass(MCInst &Inst, unsigned RegNo,
Jim Grosbachd14b70d2011-08-17 21:58:18 +0000824 uint64_t Address, const void *Decoder) {
Silviu Baranga32a49332012-03-20 15:54:56 +0000825 DecodeStatus S = MCDisassembler::Success;
826
827 if (RegNo == 15)
828 S = MCDisassembler::SoftFail;
829
830 Check(S, DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder));
831
832 return S;
Owen Anderson042619f2011-08-09 22:48:45 +0000833}
834
Mihai Popadc1764c52013-05-13 14:10:04 +0000835static DecodeStatus
836DecodeGPRwithAPSRRegisterClass(MCInst &Inst, unsigned RegNo,
837 uint64_t Address, const void *Decoder) {
838 DecodeStatus S = MCDisassembler::Success;
839
840 if (RegNo == 15)
841 {
842 Inst.addOperand(MCOperand::CreateReg(ARM::APSR_NZCV));
843 return MCDisassembler::Success;
844 }
845
846 Check(S, DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder));
847 return S;
848}
849
Craig Topperf6e7e122012-03-27 07:21:54 +0000850static DecodeStatus DecodetGPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000851 uint64_t Address, const void *Decoder) {
852 if (RegNo > 7)
James Molloydb4ce602011-09-01 18:02:14 +0000853 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000854 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
855}
856
Craig Topperf6e7e122012-03-27 07:21:54 +0000857static DecodeStatus DecodetcGPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000858 uint64_t Address, const void *Decoder) {
859 unsigned Register = 0;
860 switch (RegNo) {
861 case 0:
862 Register = ARM::R0;
863 break;
864 case 1:
865 Register = ARM::R1;
866 break;
867 case 2:
868 Register = ARM::R2;
869 break;
870 case 3:
871 Register = ARM::R3;
872 break;
873 case 9:
874 Register = ARM::R9;
875 break;
876 case 12:
877 Register = ARM::R12;
878 break;
879 default:
James Molloydb4ce602011-09-01 18:02:14 +0000880 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000881 }
882
883 Inst.addOperand(MCOperand::CreateReg(Register));
James Molloydb4ce602011-09-01 18:02:14 +0000884 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +0000885}
886
Craig Topperf6e7e122012-03-27 07:21:54 +0000887static DecodeStatus DecoderGPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000888 uint64_t Address, const void *Decoder) {
James Molloydb4ce602011-09-01 18:02:14 +0000889 if (RegNo == 13 || RegNo == 15) return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000890 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
891}
892
Craig Topperca658c22012-03-11 07:16:55 +0000893static const uint16_t SPRDecoderTable[] = {
Owen Andersone0152a72011-08-09 20:55:18 +0000894 ARM::S0, ARM::S1, ARM::S2, ARM::S3,
895 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
896 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
897 ARM::S12, ARM::S13, ARM::S14, ARM::S15,
898 ARM::S16, ARM::S17, ARM::S18, ARM::S19,
899 ARM::S20, ARM::S21, ARM::S22, ARM::S23,
900 ARM::S24, ARM::S25, ARM::S26, ARM::S27,
901 ARM::S28, ARM::S29, ARM::S30, ARM::S31
902};
903
Craig Topperf6e7e122012-03-27 07:21:54 +0000904static DecodeStatus DecodeSPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000905 uint64_t Address, const void *Decoder) {
906 if (RegNo > 31)
James Molloydb4ce602011-09-01 18:02:14 +0000907 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000908
909 unsigned Register = SPRDecoderTable[RegNo];
910 Inst.addOperand(MCOperand::CreateReg(Register));
James Molloydb4ce602011-09-01 18:02:14 +0000911 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +0000912}
913
Craig Topperca658c22012-03-11 07:16:55 +0000914static const uint16_t DPRDecoderTable[] = {
Owen Andersone0152a72011-08-09 20:55:18 +0000915 ARM::D0, ARM::D1, ARM::D2, ARM::D3,
916 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
917 ARM::D8, ARM::D9, ARM::D10, ARM::D11,
918 ARM::D12, ARM::D13, ARM::D14, ARM::D15,
919 ARM::D16, ARM::D17, ARM::D18, ARM::D19,
920 ARM::D20, ARM::D21, ARM::D22, ARM::D23,
921 ARM::D24, ARM::D25, ARM::D26, ARM::D27,
922 ARM::D28, ARM::D29, ARM::D30, ARM::D31
923};
924
Craig Topperf6e7e122012-03-27 07:21:54 +0000925static DecodeStatus DecodeDPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000926 uint64_t Address, const void *Decoder) {
927 if (RegNo > 31)
James Molloydb4ce602011-09-01 18:02:14 +0000928 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000929
930 unsigned Register = DPRDecoderTable[RegNo];
931 Inst.addOperand(MCOperand::CreateReg(Register));
James Molloydb4ce602011-09-01 18:02:14 +0000932 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +0000933}
934
Craig Topperf6e7e122012-03-27 07:21:54 +0000935static DecodeStatus DecodeDPR_8RegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000936 uint64_t Address, const void *Decoder) {
937 if (RegNo > 7)
James Molloydb4ce602011-09-01 18:02:14 +0000938 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000939 return DecodeDPRRegisterClass(Inst, RegNo, Address, Decoder);
940}
941
Owen Anderson03aadae2011-09-01 23:23:50 +0000942static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +0000943DecodeDPR_VFP2RegisterClass(MCInst &Inst, unsigned RegNo,
Jim Grosbachd14b70d2011-08-17 21:58:18 +0000944 uint64_t Address, const void *Decoder) {
Owen Andersone0152a72011-08-09 20:55:18 +0000945 if (RegNo > 15)
James Molloydb4ce602011-09-01 18:02:14 +0000946 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000947 return DecodeDPRRegisterClass(Inst, RegNo, Address, Decoder);
948}
949
Craig Topperca658c22012-03-11 07:16:55 +0000950static const uint16_t QPRDecoderTable[] = {
Owen Andersone0152a72011-08-09 20:55:18 +0000951 ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
952 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7,
953 ARM::Q8, ARM::Q9, ARM::Q10, ARM::Q11,
954 ARM::Q12, ARM::Q13, ARM::Q14, ARM::Q15
955};
956
957
Craig Topperf6e7e122012-03-27 07:21:54 +0000958static DecodeStatus DecodeQPRRegisterClass(MCInst &Inst, unsigned RegNo,
Owen Andersone0152a72011-08-09 20:55:18 +0000959 uint64_t Address, const void *Decoder) {
Mihai Popadcf09222013-05-20 14:42:43 +0000960 if (RegNo > 31 || (RegNo & 1) != 0)
James Molloydb4ce602011-09-01 18:02:14 +0000961 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +0000962 RegNo >>= 1;
963
964 unsigned Register = QPRDecoderTable[RegNo];
965 Inst.addOperand(MCOperand::CreateReg(Register));
James Molloydb4ce602011-09-01 18:02:14 +0000966 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +0000967}
968
Craig Topperca658c22012-03-11 07:16:55 +0000969static const uint16_t DPairDecoderTable[] = {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000970 ARM::Q0, ARM::D1_D2, ARM::Q1, ARM::D3_D4, ARM::Q2, ARM::D5_D6,
971 ARM::Q3, ARM::D7_D8, ARM::Q4, ARM::D9_D10, ARM::Q5, ARM::D11_D12,
972 ARM::Q6, ARM::D13_D14, ARM::Q7, ARM::D15_D16, ARM::Q8, ARM::D17_D18,
973 ARM::Q9, ARM::D19_D20, ARM::Q10, ARM::D21_D22, ARM::Q11, ARM::D23_D24,
974 ARM::Q12, ARM::D25_D26, ARM::Q13, ARM::D27_D28, ARM::Q14, ARM::D29_D30,
975 ARM::Q15
976};
977
Craig Topperf6e7e122012-03-27 07:21:54 +0000978static DecodeStatus DecodeDPairRegisterClass(MCInst &Inst, unsigned RegNo,
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000979 uint64_t Address, const void *Decoder) {
980 if (RegNo > 30)
981 return MCDisassembler::Fail;
982
983 unsigned Register = DPairDecoderTable[RegNo];
984 Inst.addOperand(MCOperand::CreateReg(Register));
985 return MCDisassembler::Success;
986}
987
Craig Topperca658c22012-03-11 07:16:55 +0000988static const uint16_t DPairSpacedDecoderTable[] = {
Jim Grosbache5307f92012-03-05 21:43:40 +0000989 ARM::D0_D2, ARM::D1_D3, ARM::D2_D4, ARM::D3_D5,
990 ARM::D4_D6, ARM::D5_D7, ARM::D6_D8, ARM::D7_D9,
991 ARM::D8_D10, ARM::D9_D11, ARM::D10_D12, ARM::D11_D13,
992 ARM::D12_D14, ARM::D13_D15, ARM::D14_D16, ARM::D15_D17,
993 ARM::D16_D18, ARM::D17_D19, ARM::D18_D20, ARM::D19_D21,
994 ARM::D20_D22, ARM::D21_D23, ARM::D22_D24, ARM::D23_D25,
995 ARM::D24_D26, ARM::D25_D27, ARM::D26_D28, ARM::D27_D29,
996 ARM::D28_D30, ARM::D29_D31
997};
998
Craig Topperf6e7e122012-03-27 07:21:54 +0000999static DecodeStatus DecodeDPairSpacedRegisterClass(MCInst &Inst,
Jim Grosbache5307f92012-03-05 21:43:40 +00001000 unsigned RegNo,
1001 uint64_t Address,
1002 const void *Decoder) {
1003 if (RegNo > 29)
1004 return MCDisassembler::Fail;
1005
1006 unsigned Register = DPairSpacedDecoderTable[RegNo];
1007 Inst.addOperand(MCOperand::CreateReg(Register));
1008 return MCDisassembler::Success;
1009}
1010
Craig Topperf6e7e122012-03-27 07:21:54 +00001011static DecodeStatus DecodePredicateOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001012 uint64_t Address, const void *Decoder) {
James Molloydb4ce602011-09-01 18:02:14 +00001013 if (Val == 0xF) return MCDisassembler::Fail;
Owen Anderson7a2401d2011-08-09 21:07:45 +00001014 // AL predicate is not allowed on Thumb1 branches.
1015 if (Inst.getOpcode() == ARM::tBcc && Val == 0xE)
James Molloydb4ce602011-09-01 18:02:14 +00001016 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001017 Inst.addOperand(MCOperand::CreateImm(Val));
1018 if (Val == ARMCC::AL) {
1019 Inst.addOperand(MCOperand::CreateReg(0));
1020 } else
1021 Inst.addOperand(MCOperand::CreateReg(ARM::CPSR));
James Molloydb4ce602011-09-01 18:02:14 +00001022 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00001023}
1024
Craig Topperf6e7e122012-03-27 07:21:54 +00001025static DecodeStatus DecodeCCOutOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001026 uint64_t Address, const void *Decoder) {
1027 if (Val)
1028 Inst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1029 else
1030 Inst.addOperand(MCOperand::CreateReg(0));
James Molloydb4ce602011-09-01 18:02:14 +00001031 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00001032}
1033
Craig Topperf6e7e122012-03-27 07:21:54 +00001034static DecodeStatus DecodeSOImmOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001035 uint64_t Address, const void *Decoder) {
1036 uint32_t imm = Val & 0xFF;
1037 uint32_t rot = (Val & 0xF00) >> 7;
Eli Friedmana7ad9f32011-10-13 23:36:06 +00001038 uint32_t rot_imm = (imm >> rot) | (imm << ((32-rot) & 0x1F));
Owen Andersone0152a72011-08-09 20:55:18 +00001039 Inst.addOperand(MCOperand::CreateImm(rot_imm));
James Molloydb4ce602011-09-01 18:02:14 +00001040 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00001041}
1042
Craig Topperf6e7e122012-03-27 07:21:54 +00001043static DecodeStatus DecodeSORegImmOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001044 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001045 DecodeStatus S = MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00001046
Jim Grosbachecaef492012-08-14 19:06:05 +00001047 unsigned Rm = fieldFromInstruction(Val, 0, 4);
1048 unsigned type = fieldFromInstruction(Val, 5, 2);
1049 unsigned imm = fieldFromInstruction(Val, 7, 5);
Owen Andersone0152a72011-08-09 20:55:18 +00001050
1051 // Register-immediate
Owen Anderson03aadae2011-09-01 23:23:50 +00001052 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
1053 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001054
1055 ARM_AM::ShiftOpc Shift = ARM_AM::lsl;
1056 switch (type) {
1057 case 0:
1058 Shift = ARM_AM::lsl;
1059 break;
1060 case 1:
1061 Shift = ARM_AM::lsr;
1062 break;
1063 case 2:
1064 Shift = ARM_AM::asr;
1065 break;
1066 case 3:
1067 Shift = ARM_AM::ror;
1068 break;
1069 }
1070
1071 if (Shift == ARM_AM::ror && imm == 0)
1072 Shift = ARM_AM::rrx;
1073
1074 unsigned Op = Shift | (imm << 3);
1075 Inst.addOperand(MCOperand::CreateImm(Op));
1076
Owen Andersona4043c42011-08-17 17:44:15 +00001077 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001078}
1079
Craig Topperf6e7e122012-03-27 07:21:54 +00001080static DecodeStatus DecodeSORegRegOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001081 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001082 DecodeStatus S = MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00001083
Jim Grosbachecaef492012-08-14 19:06:05 +00001084 unsigned Rm = fieldFromInstruction(Val, 0, 4);
1085 unsigned type = fieldFromInstruction(Val, 5, 2);
1086 unsigned Rs = fieldFromInstruction(Val, 8, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00001087
1088 // Register-register
Owen Anderson03aadae2011-09-01 23:23:50 +00001089 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
1090 return MCDisassembler::Fail;
1091 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rs, Address, Decoder)))
1092 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001093
1094 ARM_AM::ShiftOpc Shift = ARM_AM::lsl;
1095 switch (type) {
1096 case 0:
1097 Shift = ARM_AM::lsl;
1098 break;
1099 case 1:
1100 Shift = ARM_AM::lsr;
1101 break;
1102 case 2:
1103 Shift = ARM_AM::asr;
1104 break;
1105 case 3:
1106 Shift = ARM_AM::ror;
1107 break;
1108 }
1109
1110 Inst.addOperand(MCOperand::CreateImm(Shift));
1111
Owen Andersona4043c42011-08-17 17:44:15 +00001112 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001113}
1114
Craig Topperf6e7e122012-03-27 07:21:54 +00001115static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001116 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001117 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001118
Owen Anderson53db43b2011-09-09 23:13:33 +00001119 bool writebackLoad = false;
1120 unsigned writebackReg = 0;
1121 switch (Inst.getOpcode()) {
1122 default:
1123 break;
1124 case ARM::LDMIA_UPD:
1125 case ARM::LDMDB_UPD:
1126 case ARM::LDMIB_UPD:
1127 case ARM::LDMDA_UPD:
1128 case ARM::t2LDMIA_UPD:
1129 case ARM::t2LDMDB_UPD:
1130 writebackLoad = true;
1131 writebackReg = Inst.getOperand(0).getReg();
1132 break;
1133 }
1134
Owen Anderson60663402011-08-11 20:21:46 +00001135 // Empty register lists are not allowed.
Benjamin Kramer8bad66e2013-05-19 22:01:57 +00001136 if (Val == 0) return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001137 for (unsigned i = 0; i < 16; ++i) {
Owen Andersoned253852011-08-11 18:24:51 +00001138 if (Val & (1 << i)) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001139 if (!Check(S, DecodeGPRRegisterClass(Inst, i, Address, Decoder)))
1140 return MCDisassembler::Fail;
Owen Anderson53db43b2011-09-09 23:13:33 +00001141 // Writeback not allowed if Rn is in the target list.
1142 if (writebackLoad && writebackReg == Inst.end()[-1].getReg())
1143 Check(S, MCDisassembler::SoftFail);
Owen Andersoned253852011-08-11 18:24:51 +00001144 }
Owen Andersone0152a72011-08-09 20:55:18 +00001145 }
1146
Owen Andersona4043c42011-08-17 17:44:15 +00001147 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001148}
1149
Craig Topperf6e7e122012-03-27 07:21:54 +00001150static DecodeStatus DecodeSPRRegListOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001151 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001152 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001153
Jim Grosbachecaef492012-08-14 19:06:05 +00001154 unsigned Vd = fieldFromInstruction(Val, 8, 5);
1155 unsigned regs = fieldFromInstruction(Val, 0, 8);
Owen Andersone0152a72011-08-09 20:55:18 +00001156
Tim Northover4173e292013-05-31 15:55:51 +00001157 // In case of unpredictable encoding, tweak the operands.
1158 if (regs == 0 || (Vd + regs) > 32) {
1159 regs = Vd + regs > 32 ? 32 - Vd : regs;
1160 regs = std::max( 1u, regs);
1161 S = MCDisassembler::SoftFail;
1162 }
1163
Owen Anderson03aadae2011-09-01 23:23:50 +00001164 if (!Check(S, DecodeSPRRegisterClass(Inst, Vd, Address, Decoder)))
1165 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00001166 for (unsigned i = 0; i < (regs - 1); ++i) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001167 if (!Check(S, DecodeSPRRegisterClass(Inst, ++Vd, Address, Decoder)))
1168 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00001169 }
Owen Andersone0152a72011-08-09 20:55:18 +00001170
Owen Andersona4043c42011-08-17 17:44:15 +00001171 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001172}
1173
Craig Topperf6e7e122012-03-27 07:21:54 +00001174static DecodeStatus DecodeDPRRegListOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001175 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001176 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001177
Jim Grosbachecaef492012-08-14 19:06:05 +00001178 unsigned Vd = fieldFromInstruction(Val, 8, 5);
Tim Northover4173e292013-05-31 15:55:51 +00001179 unsigned regs = fieldFromInstruction(Val, 1, 7);
Silviu Baranga9560af82012-05-03 16:38:40 +00001180
Tim Northover4173e292013-05-31 15:55:51 +00001181 // In case of unpredictable encoding, tweak the operands.
1182 if (regs == 0 || regs > 16 || (Vd + regs) > 32) {
1183 regs = Vd + regs > 32 ? 32 - Vd : regs;
1184 regs = std::max( 1u, regs);
1185 regs = std::min(16u, regs);
1186 S = MCDisassembler::SoftFail;
1187 }
Owen Andersone0152a72011-08-09 20:55:18 +00001188
Owen Anderson03aadae2011-09-01 23:23:50 +00001189 if (!Check(S, DecodeDPRRegisterClass(Inst, Vd, Address, Decoder)))
1190 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00001191 for (unsigned i = 0; i < (regs - 1); ++i) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001192 if (!Check(S, DecodeDPRRegisterClass(Inst, ++Vd, Address, Decoder)))
1193 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00001194 }
Owen Andersone0152a72011-08-09 20:55:18 +00001195
Owen Andersona4043c42011-08-17 17:44:15 +00001196 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001197}
1198
Craig Topperf6e7e122012-03-27 07:21:54 +00001199static DecodeStatus DecodeBitfieldMaskOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001200 uint64_t Address, const void *Decoder) {
Owen Anderson5d69f632011-08-10 17:36:48 +00001201 // This operand encodes a mask of contiguous zeros between a specified MSB
1202 // and LSB. To decode it, we create the mask of all bits MSB-and-lower,
1203 // the mask of all bits LSB-and-lower, and then xor them to create
Jim Grosbachd14b70d2011-08-17 21:58:18 +00001204 // the mask of that's all ones on [msb, lsb]. Finally we not it to
Owen Anderson5d69f632011-08-10 17:36:48 +00001205 // create the final mask.
Jim Grosbachecaef492012-08-14 19:06:05 +00001206 unsigned msb = fieldFromInstruction(Val, 5, 5);
1207 unsigned lsb = fieldFromInstruction(Val, 0, 5);
Owen Anderson3ca958c2011-09-16 22:29:48 +00001208
Owen Anderson502cd9d2011-09-16 23:30:01 +00001209 DecodeStatus S = MCDisassembler::Success;
Kevin Enderby136d6742012-11-29 23:47:11 +00001210 if (lsb > msb) {
1211 Check(S, MCDisassembler::SoftFail);
1212 // The check above will cause the warning for the "potentially undefined
1213 // instruction encoding" but we can't build a bad MCOperand value here
1214 // with a lsb > msb or else printing the MCInst will cause a crash.
1215 lsb = msb;
1216 }
Owen Anderson502cd9d2011-09-16 23:30:01 +00001217
Owen Andersonb925e932011-09-16 23:04:48 +00001218 uint32_t msb_mask = 0xFFFFFFFF;
1219 if (msb != 31) msb_mask = (1U << (msb+1)) - 1;
1220 uint32_t lsb_mask = (1U << lsb) - 1;
Owen Anderson3ca958c2011-09-16 22:29:48 +00001221
Owen Andersone0152a72011-08-09 20:55:18 +00001222 Inst.addOperand(MCOperand::CreateImm(~(msb_mask ^ lsb_mask)));
Owen Anderson502cd9d2011-09-16 23:30:01 +00001223 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001224}
1225
Craig Topperf6e7e122012-03-27 07:21:54 +00001226static DecodeStatus DecodeCopMemInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00001227 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001228 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001229
Jim Grosbachecaef492012-08-14 19:06:05 +00001230 unsigned pred = fieldFromInstruction(Insn, 28, 4);
1231 unsigned CRd = fieldFromInstruction(Insn, 12, 4);
1232 unsigned coproc = fieldFromInstruction(Insn, 8, 4);
1233 unsigned imm = fieldFromInstruction(Insn, 0, 8);
1234 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
1235 unsigned U = fieldFromInstruction(Insn, 23, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00001236
1237 switch (Inst.getOpcode()) {
1238 case ARM::LDC_OFFSET:
1239 case ARM::LDC_PRE:
1240 case ARM::LDC_POST:
1241 case ARM::LDC_OPTION:
1242 case ARM::LDCL_OFFSET:
1243 case ARM::LDCL_PRE:
1244 case ARM::LDCL_POST:
1245 case ARM::LDCL_OPTION:
1246 case ARM::STC_OFFSET:
1247 case ARM::STC_PRE:
1248 case ARM::STC_POST:
1249 case ARM::STC_OPTION:
1250 case ARM::STCL_OFFSET:
1251 case ARM::STCL_PRE:
1252 case ARM::STCL_POST:
1253 case ARM::STCL_OPTION:
Owen Anderson18d17aa2011-09-07 21:10:42 +00001254 case ARM::t2LDC_OFFSET:
1255 case ARM::t2LDC_PRE:
1256 case ARM::t2LDC_POST:
1257 case ARM::t2LDC_OPTION:
1258 case ARM::t2LDCL_OFFSET:
1259 case ARM::t2LDCL_PRE:
1260 case ARM::t2LDCL_POST:
1261 case ARM::t2LDCL_OPTION:
1262 case ARM::t2STC_OFFSET:
1263 case ARM::t2STC_PRE:
1264 case ARM::t2STC_POST:
1265 case ARM::t2STC_OPTION:
1266 case ARM::t2STCL_OFFSET:
1267 case ARM::t2STCL_PRE:
1268 case ARM::t2STCL_POST:
1269 case ARM::t2STCL_OPTION:
Owen Andersone0152a72011-08-09 20:55:18 +00001270 if (coproc == 0xA || coproc == 0xB)
James Molloydb4ce602011-09-01 18:02:14 +00001271 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001272 break;
1273 default:
1274 break;
1275 }
1276
1277 Inst.addOperand(MCOperand::CreateImm(coproc));
1278 Inst.addOperand(MCOperand::CreateImm(CRd));
Owen Anderson03aadae2011-09-01 23:23:50 +00001279 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1280 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001281
Owen Andersone0152a72011-08-09 20:55:18 +00001282 switch (Inst.getOpcode()) {
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001283 case ARM::t2LDC2_OFFSET:
1284 case ARM::t2LDC2L_OFFSET:
1285 case ARM::t2LDC2_PRE:
1286 case ARM::t2LDC2L_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001287 case ARM::t2STC2_OFFSET:
1288 case ARM::t2STC2L_OFFSET:
1289 case ARM::t2STC2_PRE:
1290 case ARM::t2STC2L_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001291 case ARM::LDC2_OFFSET:
1292 case ARM::LDC2L_OFFSET:
1293 case ARM::LDC2_PRE:
1294 case ARM::LDC2L_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001295 case ARM::STC2_OFFSET:
1296 case ARM::STC2L_OFFSET:
1297 case ARM::STC2_PRE:
1298 case ARM::STC2L_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001299 case ARM::t2LDC_OFFSET:
1300 case ARM::t2LDCL_OFFSET:
1301 case ARM::t2LDC_PRE:
1302 case ARM::t2LDCL_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001303 case ARM::t2STC_OFFSET:
1304 case ARM::t2STCL_OFFSET:
1305 case ARM::t2STC_PRE:
1306 case ARM::t2STCL_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001307 case ARM::LDC_OFFSET:
1308 case ARM::LDCL_OFFSET:
1309 case ARM::LDC_PRE:
1310 case ARM::LDCL_PRE:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001311 case ARM::STC_OFFSET:
1312 case ARM::STCL_OFFSET:
1313 case ARM::STC_PRE:
1314 case ARM::STCL_PRE:
Jim Grosbacha098a892011-10-12 21:59:02 +00001315 imm = ARM_AM::getAM5Opc(U ? ARM_AM::add : ARM_AM::sub, imm);
1316 Inst.addOperand(MCOperand::CreateImm(imm));
1317 break;
1318 case ARM::t2LDC2_POST:
1319 case ARM::t2LDC2L_POST:
1320 case ARM::t2STC2_POST:
1321 case ARM::t2STC2L_POST:
1322 case ARM::LDC2_POST:
1323 case ARM::LDC2L_POST:
1324 case ARM::STC2_POST:
1325 case ARM::STC2L_POST:
1326 case ARM::t2LDC_POST:
1327 case ARM::t2LDCL_POST:
1328 case ARM::t2STC_POST:
1329 case ARM::t2STCL_POST:
1330 case ARM::LDC_POST:
1331 case ARM::LDCL_POST:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001332 case ARM::STC_POST:
1333 case ARM::STCL_POST:
Owen Andersone0152a72011-08-09 20:55:18 +00001334 imm |= U << 8;
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001335 // fall through.
Owen Andersone0152a72011-08-09 20:55:18 +00001336 default:
Jim Grosbach54a20ed2011-10-12 20:54:17 +00001337 // The 'option' variant doesn't encode 'U' in the immediate since
1338 // the immediate is unsigned [0,255].
1339 Inst.addOperand(MCOperand::CreateImm(imm));
Owen Andersone0152a72011-08-09 20:55:18 +00001340 break;
1341 }
1342
1343 switch (Inst.getOpcode()) {
1344 case ARM::LDC_OFFSET:
1345 case ARM::LDC_PRE:
1346 case ARM::LDC_POST:
1347 case ARM::LDC_OPTION:
1348 case ARM::LDCL_OFFSET:
1349 case ARM::LDCL_PRE:
1350 case ARM::LDCL_POST:
1351 case ARM::LDCL_OPTION:
1352 case ARM::STC_OFFSET:
1353 case ARM::STC_PRE:
1354 case ARM::STC_POST:
1355 case ARM::STC_OPTION:
1356 case ARM::STCL_OFFSET:
1357 case ARM::STCL_PRE:
1358 case ARM::STCL_POST:
1359 case ARM::STCL_OPTION:
Owen Anderson03aadae2011-09-01 23:23:50 +00001360 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
1361 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001362 break;
1363 default:
1364 break;
1365 }
1366
Owen Andersona4043c42011-08-17 17:44:15 +00001367 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001368}
1369
Owen Anderson03aadae2011-09-01 23:23:50 +00001370static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00001371DecodeAddrMode2IdxInstruction(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00001372 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001373 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001374
Jim Grosbachecaef492012-08-14 19:06:05 +00001375 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
1376 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
1377 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
1378 unsigned imm = fieldFromInstruction(Insn, 0, 12);
1379 unsigned pred = fieldFromInstruction(Insn, 28, 4);
1380 unsigned reg = fieldFromInstruction(Insn, 25, 1);
1381 unsigned P = fieldFromInstruction(Insn, 24, 1);
1382 unsigned W = fieldFromInstruction(Insn, 21, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00001383
1384 // On stores, the writeback operand precedes Rt.
1385 switch (Inst.getOpcode()) {
1386 case ARM::STR_POST_IMM:
1387 case ARM::STR_POST_REG:
Owen Anderson3a850f22011-08-11 20:47:56 +00001388 case ARM::STRB_POST_IMM:
1389 case ARM::STRB_POST_REG:
Jim Grosbache2594212011-08-11 22:18:00 +00001390 case ARM::STRT_POST_REG:
1391 case ARM::STRT_POST_IMM:
Jim Grosbach2a502602011-08-11 20:04:56 +00001392 case ARM::STRBT_POST_REG:
1393 case ARM::STRBT_POST_IMM:
Owen Anderson03aadae2011-09-01 23:23:50 +00001394 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1395 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001396 break;
1397 default:
1398 break;
1399 }
1400
Owen Anderson03aadae2011-09-01 23:23:50 +00001401 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
1402 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001403
1404 // On loads, the writeback operand comes after Rt.
1405 switch (Inst.getOpcode()) {
1406 case ARM::LDR_POST_IMM:
1407 case ARM::LDR_POST_REG:
Owen Anderson3a850f22011-08-11 20:47:56 +00001408 case ARM::LDRB_POST_IMM:
1409 case ARM::LDRB_POST_REG:
Owen Andersone0152a72011-08-09 20:55:18 +00001410 case ARM::LDRBT_POST_REG:
1411 case ARM::LDRBT_POST_IMM:
Jim Grosbachd5d63592011-08-10 23:43:54 +00001412 case ARM::LDRT_POST_REG:
1413 case ARM::LDRT_POST_IMM:
Owen Anderson03aadae2011-09-01 23:23:50 +00001414 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1415 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001416 break;
1417 default:
1418 break;
1419 }
1420
Owen Anderson03aadae2011-09-01 23:23:50 +00001421 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1422 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001423
1424 ARM_AM::AddrOpc Op = ARM_AM::add;
Jim Grosbachecaef492012-08-14 19:06:05 +00001425 if (!fieldFromInstruction(Insn, 23, 1))
Owen Andersone0152a72011-08-09 20:55:18 +00001426 Op = ARM_AM::sub;
1427
1428 bool writeback = (P == 0) || (W == 1);
1429 unsigned idx_mode = 0;
1430 if (P && writeback)
1431 idx_mode = ARMII::IndexModePre;
1432 else if (!P && writeback)
1433 idx_mode = ARMII::IndexModePost;
1434
Owen Anderson03aadae2011-09-01 23:23:50 +00001435 if (writeback && (Rn == 15 || Rn == Rt))
1436 S = MCDisassembler::SoftFail; // UNPREDICTABLE
Owen Anderson3477f2c2011-08-11 19:00:18 +00001437
Owen Andersone0152a72011-08-09 20:55:18 +00001438 if (reg) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001439 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
1440 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001441 ARM_AM::ShiftOpc Opc = ARM_AM::lsl;
Jim Grosbachecaef492012-08-14 19:06:05 +00001442 switch( fieldFromInstruction(Insn, 5, 2)) {
Owen Andersone0152a72011-08-09 20:55:18 +00001443 case 0:
1444 Opc = ARM_AM::lsl;
1445 break;
1446 case 1:
1447 Opc = ARM_AM::lsr;
1448 break;
1449 case 2:
1450 Opc = ARM_AM::asr;
1451 break;
1452 case 3:
1453 Opc = ARM_AM::ror;
1454 break;
1455 default:
James Molloydb4ce602011-09-01 18:02:14 +00001456 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001457 }
Jim Grosbachecaef492012-08-14 19:06:05 +00001458 unsigned amt = fieldFromInstruction(Insn, 7, 5);
Tim Northover0c97e762012-09-22 11:18:12 +00001459 if (Opc == ARM_AM::ror && amt == 0)
1460 Opc = ARM_AM::rrx;
Owen Andersone0152a72011-08-09 20:55:18 +00001461 unsigned imm = ARM_AM::getAM2Opc(Op, amt, Opc, idx_mode);
1462
1463 Inst.addOperand(MCOperand::CreateImm(imm));
1464 } else {
1465 Inst.addOperand(MCOperand::CreateReg(0));
1466 unsigned tmp = ARM_AM::getAM2Opc(Op, imm, ARM_AM::lsl, idx_mode);
1467 Inst.addOperand(MCOperand::CreateImm(tmp));
1468 }
1469
Owen Anderson03aadae2011-09-01 23:23:50 +00001470 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
1471 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001472
Owen Andersona4043c42011-08-17 17:44:15 +00001473 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001474}
1475
Craig Topperf6e7e122012-03-27 07:21:54 +00001476static DecodeStatus DecodeSORegMemOperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00001477 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001478 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001479
Jim Grosbachecaef492012-08-14 19:06:05 +00001480 unsigned Rn = fieldFromInstruction(Val, 13, 4);
1481 unsigned Rm = fieldFromInstruction(Val, 0, 4);
1482 unsigned type = fieldFromInstruction(Val, 5, 2);
1483 unsigned imm = fieldFromInstruction(Val, 7, 5);
1484 unsigned U = fieldFromInstruction(Val, 12, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00001485
Owen Andersond151b092011-08-09 21:38:14 +00001486 ARM_AM::ShiftOpc ShOp = ARM_AM::lsl;
Owen Andersone0152a72011-08-09 20:55:18 +00001487 switch (type) {
1488 case 0:
1489 ShOp = ARM_AM::lsl;
1490 break;
1491 case 1:
1492 ShOp = ARM_AM::lsr;
1493 break;
1494 case 2:
1495 ShOp = ARM_AM::asr;
1496 break;
1497 case 3:
1498 ShOp = ARM_AM::ror;
1499 break;
1500 }
1501
Tim Northover0c97e762012-09-22 11:18:12 +00001502 if (ShOp == ARM_AM::ror && imm == 0)
1503 ShOp = ARM_AM::rrx;
1504
Owen Anderson03aadae2011-09-01 23:23:50 +00001505 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1506 return MCDisassembler::Fail;
1507 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
1508 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001509 unsigned shift;
1510 if (U)
1511 shift = ARM_AM::getAM2Opc(ARM_AM::add, imm, ShOp);
1512 else
1513 shift = ARM_AM::getAM2Opc(ARM_AM::sub, imm, ShOp);
1514 Inst.addOperand(MCOperand::CreateImm(shift));
1515
Owen Andersona4043c42011-08-17 17:44:15 +00001516 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001517}
1518
Owen Anderson03aadae2011-09-01 23:23:50 +00001519static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00001520DecodeAddrMode3Instruction(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00001521 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001522 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001523
Jim Grosbachecaef492012-08-14 19:06:05 +00001524 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
1525 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
1526 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
1527 unsigned type = fieldFromInstruction(Insn, 22, 1);
1528 unsigned imm = fieldFromInstruction(Insn, 8, 4);
1529 unsigned U = ((~fieldFromInstruction(Insn, 23, 1)) & 1) << 8;
1530 unsigned pred = fieldFromInstruction(Insn, 28, 4);
1531 unsigned W = fieldFromInstruction(Insn, 21, 1);
1532 unsigned P = fieldFromInstruction(Insn, 24, 1);
Silviu Baranga4afd7d22012-03-22 14:14:49 +00001533 unsigned Rt2 = Rt + 1;
Owen Andersone0152a72011-08-09 20:55:18 +00001534
1535 bool writeback = (W == 1) | (P == 0);
Owen Anderson1d5d2ca2011-08-15 20:51:32 +00001536
1537 // For {LD,ST}RD, Rt must be even, else undefined.
1538 switch (Inst.getOpcode()) {
1539 case ARM::STRD:
1540 case ARM::STRD_PRE:
1541 case ARM::STRD_POST:
1542 case ARM::LDRD:
1543 case ARM::LDRD_PRE:
1544 case ARM::LDRD_POST:
Silviu Baranga4afd7d22012-03-22 14:14:49 +00001545 if (Rt & 0x1) S = MCDisassembler::SoftFail;
1546 break;
1547 default:
1548 break;
1549 }
1550 switch (Inst.getOpcode()) {
1551 case ARM::STRD:
1552 case ARM::STRD_PRE:
1553 case ARM::STRD_POST:
1554 if (P == 0 && W == 1)
1555 S = MCDisassembler::SoftFail;
1556
1557 if (writeback && (Rn == 15 || Rn == Rt || Rn == Rt2))
1558 S = MCDisassembler::SoftFail;
1559 if (type && Rm == 15)
1560 S = MCDisassembler::SoftFail;
1561 if (Rt2 == 15)
1562 S = MCDisassembler::SoftFail;
Jim Grosbachecaef492012-08-14 19:06:05 +00001563 if (!type && fieldFromInstruction(Insn, 8, 4))
Silviu Baranga4afd7d22012-03-22 14:14:49 +00001564 S = MCDisassembler::SoftFail;
1565 break;
1566 case ARM::STRH:
1567 case ARM::STRH_PRE:
1568 case ARM::STRH_POST:
1569 if (Rt == 15)
1570 S = MCDisassembler::SoftFail;
1571 if (writeback && (Rn == 15 || Rn == Rt))
1572 S = MCDisassembler::SoftFail;
1573 if (!type && Rm == 15)
1574 S = MCDisassembler::SoftFail;
1575 break;
1576 case ARM::LDRD:
1577 case ARM::LDRD_PRE:
1578 case ARM::LDRD_POST:
1579 if (type && Rn == 15){
1580 if (Rt2 == 15)
1581 S = MCDisassembler::SoftFail;
1582 break;
1583 }
1584 if (P == 0 && W == 1)
1585 S = MCDisassembler::SoftFail;
1586 if (!type && (Rt2 == 15 || Rm == 15 || Rm == Rt || Rm == Rt2))
1587 S = MCDisassembler::SoftFail;
1588 if (!type && writeback && Rn == 15)
1589 S = MCDisassembler::SoftFail;
1590 if (writeback && (Rn == Rt || Rn == Rt2))
1591 S = MCDisassembler::SoftFail;
1592 break;
1593 case ARM::LDRH:
1594 case ARM::LDRH_PRE:
1595 case ARM::LDRH_POST:
1596 if (type && Rn == 15){
1597 if (Rt == 15)
1598 S = MCDisassembler::SoftFail;
1599 break;
1600 }
1601 if (Rt == 15)
1602 S = MCDisassembler::SoftFail;
1603 if (!type && Rm == 15)
1604 S = MCDisassembler::SoftFail;
1605 if (!type && writeback && (Rn == 15 || Rn == Rt))
1606 S = MCDisassembler::SoftFail;
1607 break;
1608 case ARM::LDRSH:
1609 case ARM::LDRSH_PRE:
1610 case ARM::LDRSH_POST:
1611 case ARM::LDRSB:
1612 case ARM::LDRSB_PRE:
1613 case ARM::LDRSB_POST:
1614 if (type && Rn == 15){
1615 if (Rt == 15)
1616 S = MCDisassembler::SoftFail;
1617 break;
1618 }
1619 if (type && (Rt == 15 || (writeback && Rn == Rt)))
1620 S = MCDisassembler::SoftFail;
1621 if (!type && (Rt == 15 || Rm == 15))
1622 S = MCDisassembler::SoftFail;
1623 if (!type && writeback && (Rn == 15 || Rn == Rt))
1624 S = MCDisassembler::SoftFail;
Owen Anderson1d5d2ca2011-08-15 20:51:32 +00001625 break;
Owen Anderson03aadae2011-09-01 23:23:50 +00001626 default:
1627 break;
Owen Anderson1d5d2ca2011-08-15 20:51:32 +00001628 }
1629
Owen Andersone0152a72011-08-09 20:55:18 +00001630 if (writeback) { // Writeback
1631 if (P)
1632 U |= ARMII::IndexModePre << 9;
1633 else
1634 U |= ARMII::IndexModePost << 9;
1635
1636 // On stores, the writeback operand precedes Rt.
1637 switch (Inst.getOpcode()) {
1638 case ARM::STRD:
1639 case ARM::STRD_PRE:
1640 case ARM::STRD_POST:
Owen Anderson60138ea2011-08-12 20:02:50 +00001641 case ARM::STRH:
1642 case ARM::STRH_PRE:
1643 case ARM::STRH_POST:
Owen Anderson03aadae2011-09-01 23:23:50 +00001644 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1645 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001646 break;
1647 default:
1648 break;
1649 }
1650 }
1651
Owen Anderson03aadae2011-09-01 23:23:50 +00001652 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
1653 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001654 switch (Inst.getOpcode()) {
1655 case ARM::STRD:
1656 case ARM::STRD_PRE:
1657 case ARM::STRD_POST:
1658 case ARM::LDRD:
1659 case ARM::LDRD_PRE:
1660 case ARM::LDRD_POST:
Owen Anderson03aadae2011-09-01 23:23:50 +00001661 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt+1, Address, Decoder)))
1662 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001663 break;
1664 default:
1665 break;
1666 }
1667
1668 if (writeback) {
1669 // On loads, the writeback operand comes after Rt.
1670 switch (Inst.getOpcode()) {
1671 case ARM::LDRD:
1672 case ARM::LDRD_PRE:
1673 case ARM::LDRD_POST:
Owen Anderson2d1d7a12011-08-12 20:36:11 +00001674 case ARM::LDRH:
1675 case ARM::LDRH_PRE:
1676 case ARM::LDRH_POST:
1677 case ARM::LDRSH:
1678 case ARM::LDRSH_PRE:
1679 case ARM::LDRSH_POST:
1680 case ARM::LDRSB:
1681 case ARM::LDRSB_PRE:
1682 case ARM::LDRSB_POST:
Owen Andersone0152a72011-08-09 20:55:18 +00001683 case ARM::LDRHTr:
1684 case ARM::LDRSBTr:
Owen Anderson03aadae2011-09-01 23:23:50 +00001685 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1686 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001687 break;
1688 default:
1689 break;
1690 }
1691 }
1692
Owen Anderson03aadae2011-09-01 23:23:50 +00001693 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1694 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001695
1696 if (type) {
1697 Inst.addOperand(MCOperand::CreateReg(0));
1698 Inst.addOperand(MCOperand::CreateImm(U | (imm << 4) | Rm));
1699 } else {
Owen Anderson03aadae2011-09-01 23:23:50 +00001700 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
1701 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001702 Inst.addOperand(MCOperand::CreateImm(U));
1703 }
1704
Owen Anderson03aadae2011-09-01 23:23:50 +00001705 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
1706 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001707
Owen Andersona4043c42011-08-17 17:44:15 +00001708 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001709}
1710
Craig Topperf6e7e122012-03-27 07:21:54 +00001711static DecodeStatus DecodeRFEInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00001712 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001713 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001714
Jim Grosbachecaef492012-08-14 19:06:05 +00001715 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
1716 unsigned mode = fieldFromInstruction(Insn, 23, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00001717
1718 switch (mode) {
1719 case 0:
1720 mode = ARM_AM::da;
1721 break;
1722 case 1:
1723 mode = ARM_AM::ia;
1724 break;
1725 case 2:
1726 mode = ARM_AM::db;
1727 break;
1728 case 3:
1729 mode = ARM_AM::ib;
1730 break;
1731 }
1732
1733 Inst.addOperand(MCOperand::CreateImm(mode));
Owen Anderson03aadae2011-09-01 23:23:50 +00001734 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1735 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001736
Owen Andersona4043c42011-08-17 17:44:15 +00001737 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001738}
1739
Amaury de la Vieuville631df632013-06-08 13:38:52 +00001740static DecodeStatus DecodeQADDInstruction(MCInst &Inst, unsigned Insn,
1741 uint64_t Address, const void *Decoder) {
1742 DecodeStatus S = MCDisassembler::Success;
1743
1744 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
1745 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
1746 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
1747 unsigned pred = fieldFromInstruction(Insn, 28, 4);
1748
1749 if (pred == 0xF)
1750 return DecodeCPSInstruction(Inst, Insn, Address, Decoder);
1751
1752 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
1753 return MCDisassembler::Fail;
1754 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
1755 return MCDisassembler::Fail;
1756 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
1757 return MCDisassembler::Fail;
1758 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
1759 return MCDisassembler::Fail;
1760 return S;
1761}
1762
Craig Topperf6e7e122012-03-27 07:21:54 +00001763static DecodeStatus DecodeMemMultipleWritebackInstruction(MCInst &Inst,
Owen Andersone0152a72011-08-09 20:55:18 +00001764 unsigned Insn,
1765 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001766 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001767
Jim Grosbachecaef492012-08-14 19:06:05 +00001768 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
1769 unsigned pred = fieldFromInstruction(Insn, 28, 4);
1770 unsigned reglist = fieldFromInstruction(Insn, 0, 16);
Owen Andersone0152a72011-08-09 20:55:18 +00001771
1772 if (pred == 0xF) {
Amaury de la Vieuville68bcd022013-06-08 13:43:59 +00001773 // Ambiguous with RFE and SRS
Owen Andersone0152a72011-08-09 20:55:18 +00001774 switch (Inst.getOpcode()) {
Owen Anderson192a7602011-08-18 22:31:17 +00001775 case ARM::LDMDA:
Owen Andersone0152a72011-08-09 20:55:18 +00001776 Inst.setOpcode(ARM::RFEDA);
1777 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001778 case ARM::LDMDA_UPD:
Owen Andersone0152a72011-08-09 20:55:18 +00001779 Inst.setOpcode(ARM::RFEDA_UPD);
1780 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001781 case ARM::LDMDB:
Owen Andersone0152a72011-08-09 20:55:18 +00001782 Inst.setOpcode(ARM::RFEDB);
1783 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001784 case ARM::LDMDB_UPD:
Owen Andersone0152a72011-08-09 20:55:18 +00001785 Inst.setOpcode(ARM::RFEDB_UPD);
1786 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001787 case ARM::LDMIA:
Owen Andersone0152a72011-08-09 20:55:18 +00001788 Inst.setOpcode(ARM::RFEIA);
1789 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001790 case ARM::LDMIA_UPD:
Owen Andersone0152a72011-08-09 20:55:18 +00001791 Inst.setOpcode(ARM::RFEIA_UPD);
1792 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001793 case ARM::LDMIB:
Owen Andersone0152a72011-08-09 20:55:18 +00001794 Inst.setOpcode(ARM::RFEIB);
1795 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001796 case ARM::LDMIB_UPD:
Owen Andersone0152a72011-08-09 20:55:18 +00001797 Inst.setOpcode(ARM::RFEIB_UPD);
1798 break;
Owen Anderson192a7602011-08-18 22:31:17 +00001799 case ARM::STMDA:
1800 Inst.setOpcode(ARM::SRSDA);
1801 break;
1802 case ARM::STMDA_UPD:
1803 Inst.setOpcode(ARM::SRSDA_UPD);
1804 break;
1805 case ARM::STMDB:
1806 Inst.setOpcode(ARM::SRSDB);
1807 break;
1808 case ARM::STMDB_UPD:
1809 Inst.setOpcode(ARM::SRSDB_UPD);
1810 break;
1811 case ARM::STMIA:
1812 Inst.setOpcode(ARM::SRSIA);
1813 break;
1814 case ARM::STMIA_UPD:
1815 Inst.setOpcode(ARM::SRSIA_UPD);
1816 break;
1817 case ARM::STMIB:
1818 Inst.setOpcode(ARM::SRSIB);
1819 break;
1820 case ARM::STMIB_UPD:
1821 Inst.setOpcode(ARM::SRSIB_UPD);
1822 break;
1823 default:
Amaury de la Vieuville68bcd022013-06-08 13:43:59 +00001824 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001825 }
Owen Anderson192a7602011-08-18 22:31:17 +00001826
1827 // For stores (which become SRS's, the only operand is the mode.
Jim Grosbachecaef492012-08-14 19:06:05 +00001828 if (fieldFromInstruction(Insn, 20, 1) == 0) {
Amaury de la Vieuville68bcd022013-06-08 13:43:59 +00001829 // Check SRS encoding constraints
1830 if (!(fieldFromInstruction(Insn, 22, 1) == 1 &&
1831 fieldFromInstruction(Insn, 20, 1) == 0))
1832 return MCDisassembler::Fail;
1833
Owen Anderson192a7602011-08-18 22:31:17 +00001834 Inst.addOperand(
Jim Grosbachecaef492012-08-14 19:06:05 +00001835 MCOperand::CreateImm(fieldFromInstruction(Insn, 0, 4)));
Owen Anderson192a7602011-08-18 22:31:17 +00001836 return S;
1837 }
1838
Owen Andersone0152a72011-08-09 20:55:18 +00001839 return DecodeRFEInstruction(Inst, Insn, Address, Decoder);
1840 }
1841
Owen Anderson03aadae2011-09-01 23:23:50 +00001842 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1843 return MCDisassembler::Fail;
1844 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
1845 return MCDisassembler::Fail; // Tied
1846 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
1847 return MCDisassembler::Fail;
1848 if (!Check(S, DecodeRegListOperand(Inst, reglist, Address, Decoder)))
1849 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00001850
Owen Andersona4043c42011-08-17 17:44:15 +00001851 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001852}
1853
Craig Topperf6e7e122012-03-27 07:21:54 +00001854static DecodeStatus DecodeCPSInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00001855 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00001856 unsigned imod = fieldFromInstruction(Insn, 18, 2);
1857 unsigned M = fieldFromInstruction(Insn, 17, 1);
1858 unsigned iflags = fieldFromInstruction(Insn, 6, 3);
1859 unsigned mode = fieldFromInstruction(Insn, 0, 5);
Owen Andersone0152a72011-08-09 20:55:18 +00001860
Owen Anderson03aadae2011-09-01 23:23:50 +00001861 DecodeStatus S = MCDisassembler::Success;
Owen Anderson3d2e0e9d2011-08-09 23:05:39 +00001862
Amaury de la Vieuville631df632013-06-08 13:38:52 +00001863 // This decoder is called from multiple location that do not check
1864 // the full encoding is valid before they do.
1865 if (fieldFromInstruction(Insn, 5, 1) != 0 ||
1866 fieldFromInstruction(Insn, 16, 1) != 0 ||
1867 fieldFromInstruction(Insn, 20, 8) != 0x10)
1868 return MCDisassembler::Fail;
1869
Owen Anderson67d6f112011-08-18 22:11:02 +00001870 // imod == '01' --> UNPREDICTABLE
1871 // NOTE: Even though this is technically UNPREDICTABLE, we choose to
1872 // return failure here. The '01' imod value is unprintable, so there's
1873 // nothing useful we could do even if we returned UNPREDICTABLE.
1874
James Molloydb4ce602011-09-01 18:02:14 +00001875 if (imod == 1) return MCDisassembler::Fail;
Owen Anderson67d6f112011-08-18 22:11:02 +00001876
1877 if (imod && M) {
Owen Andersone0152a72011-08-09 20:55:18 +00001878 Inst.setOpcode(ARM::CPS3p);
1879 Inst.addOperand(MCOperand::CreateImm(imod));
1880 Inst.addOperand(MCOperand::CreateImm(iflags));
1881 Inst.addOperand(MCOperand::CreateImm(mode));
Owen Anderson67d6f112011-08-18 22:11:02 +00001882 } else if (imod && !M) {
Owen Andersone0152a72011-08-09 20:55:18 +00001883 Inst.setOpcode(ARM::CPS2p);
1884 Inst.addOperand(MCOperand::CreateImm(imod));
1885 Inst.addOperand(MCOperand::CreateImm(iflags));
James Molloydb4ce602011-09-01 18:02:14 +00001886 if (mode) S = MCDisassembler::SoftFail;
Owen Anderson67d6f112011-08-18 22:11:02 +00001887 } else if (!imod && M) {
Owen Andersone0152a72011-08-09 20:55:18 +00001888 Inst.setOpcode(ARM::CPS1p);
1889 Inst.addOperand(MCOperand::CreateImm(mode));
James Molloydb4ce602011-09-01 18:02:14 +00001890 if (iflags) S = MCDisassembler::SoftFail;
Owen Anderson5d2db892011-08-18 22:15:25 +00001891 } else {
Owen Anderson67d6f112011-08-18 22:11:02 +00001892 // imod == '00' && M == '0' --> UNPREDICTABLE
Owen Anderson5d2db892011-08-18 22:15:25 +00001893 Inst.setOpcode(ARM::CPS1p);
1894 Inst.addOperand(MCOperand::CreateImm(mode));
James Molloydb4ce602011-09-01 18:02:14 +00001895 S = MCDisassembler::SoftFail;
Owen Anderson5d2db892011-08-18 22:15:25 +00001896 }
Owen Andersone0152a72011-08-09 20:55:18 +00001897
Owen Anderson67d6f112011-08-18 22:11:02 +00001898 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00001899}
1900
Craig Topperf6e7e122012-03-27 07:21:54 +00001901static DecodeStatus DecodeT2CPSInstruction(MCInst &Inst, unsigned Insn,
Owen Anderson9b7bd152011-08-23 17:45:18 +00001902 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00001903 unsigned imod = fieldFromInstruction(Insn, 9, 2);
1904 unsigned M = fieldFromInstruction(Insn, 8, 1);
1905 unsigned iflags = fieldFromInstruction(Insn, 5, 3);
1906 unsigned mode = fieldFromInstruction(Insn, 0, 5);
Owen Anderson9b7bd152011-08-23 17:45:18 +00001907
Owen Anderson03aadae2011-09-01 23:23:50 +00001908 DecodeStatus S = MCDisassembler::Success;
Owen Anderson9b7bd152011-08-23 17:45:18 +00001909
1910 // imod == '01' --> UNPREDICTABLE
1911 // NOTE: Even though this is technically UNPREDICTABLE, we choose to
1912 // return failure here. The '01' imod value is unprintable, so there's
1913 // nothing useful we could do even if we returned UNPREDICTABLE.
1914
James Molloydb4ce602011-09-01 18:02:14 +00001915 if (imod == 1) return MCDisassembler::Fail;
Owen Anderson9b7bd152011-08-23 17:45:18 +00001916
1917 if (imod && M) {
1918 Inst.setOpcode(ARM::t2CPS3p);
1919 Inst.addOperand(MCOperand::CreateImm(imod));
1920 Inst.addOperand(MCOperand::CreateImm(iflags));
1921 Inst.addOperand(MCOperand::CreateImm(mode));
1922 } else if (imod && !M) {
1923 Inst.setOpcode(ARM::t2CPS2p);
1924 Inst.addOperand(MCOperand::CreateImm(imod));
1925 Inst.addOperand(MCOperand::CreateImm(iflags));
James Molloydb4ce602011-09-01 18:02:14 +00001926 if (mode) S = MCDisassembler::SoftFail;
Owen Anderson9b7bd152011-08-23 17:45:18 +00001927 } else if (!imod && M) {
1928 Inst.setOpcode(ARM::t2CPS1p);
1929 Inst.addOperand(MCOperand::CreateImm(mode));
James Molloydb4ce602011-09-01 18:02:14 +00001930 if (iflags) S = MCDisassembler::SoftFail;
Owen Anderson9b7bd152011-08-23 17:45:18 +00001931 } else {
Quentin Colombeta83d5e92013-04-26 17:54:54 +00001932 // imod == '00' && M == '0' --> this is a HINT instruction
1933 int imm = fieldFromInstruction(Insn, 0, 8);
1934 // HINT are defined only for immediate in [0..4]
1935 if(imm > 4) return MCDisassembler::Fail;
1936 Inst.setOpcode(ARM::t2HINT);
1937 Inst.addOperand(MCOperand::CreateImm(imm));
Owen Anderson9b7bd152011-08-23 17:45:18 +00001938 }
1939
1940 return S;
1941}
1942
Craig Topperf6e7e122012-03-27 07:21:54 +00001943static DecodeStatus DecodeT2MOVTWInstruction(MCInst &Inst, unsigned Insn,
Kevin Enderby5dcda642011-10-04 22:44:48 +00001944 uint64_t Address, const void *Decoder) {
1945 DecodeStatus S = MCDisassembler::Success;
1946
Jim Grosbachecaef492012-08-14 19:06:05 +00001947 unsigned Rd = fieldFromInstruction(Insn, 8, 4);
Kevin Enderby5dcda642011-10-04 22:44:48 +00001948 unsigned imm = 0;
1949
Jim Grosbachecaef492012-08-14 19:06:05 +00001950 imm |= (fieldFromInstruction(Insn, 0, 8) << 0);
1951 imm |= (fieldFromInstruction(Insn, 12, 3) << 8);
1952 imm |= (fieldFromInstruction(Insn, 16, 4) << 12);
1953 imm |= (fieldFromInstruction(Insn, 26, 1) << 11);
Kevin Enderby5dcda642011-10-04 22:44:48 +00001954
1955 if (Inst.getOpcode() == ARM::t2MOVTi16)
1956 if (!Check(S, DecoderGPRRegisterClass(Inst, Rd, Address, Decoder)))
1957 return MCDisassembler::Fail;
1958 if (!Check(S, DecoderGPRRegisterClass(Inst, Rd, Address, Decoder)))
1959 return MCDisassembler::Fail;
1960
1961 if (!tryAddingSymbolicOperand(Address, imm, false, 4, Inst, Decoder))
1962 Inst.addOperand(MCOperand::CreateImm(imm));
1963
1964 return S;
1965}
1966
Craig Topperf6e7e122012-03-27 07:21:54 +00001967static DecodeStatus DecodeArmMOVTWInstruction(MCInst &Inst, unsigned Insn,
Kevin Enderby5dcda642011-10-04 22:44:48 +00001968 uint64_t Address, const void *Decoder) {
1969 DecodeStatus S = MCDisassembler::Success;
1970
Jim Grosbachecaef492012-08-14 19:06:05 +00001971 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
1972 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Kevin Enderby5dcda642011-10-04 22:44:48 +00001973 unsigned imm = 0;
1974
Jim Grosbachecaef492012-08-14 19:06:05 +00001975 imm |= (fieldFromInstruction(Insn, 0, 12) << 0);
1976 imm |= (fieldFromInstruction(Insn, 16, 4) << 12);
Kevin Enderby5dcda642011-10-04 22:44:48 +00001977
1978 if (Inst.getOpcode() == ARM::MOVTi16)
Tim Northovera155ab22013-04-19 09:58:09 +00001979 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
Kevin Enderby5dcda642011-10-04 22:44:48 +00001980 return MCDisassembler::Fail;
Tim Northovera155ab22013-04-19 09:58:09 +00001981
1982 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
Kevin Enderby5dcda642011-10-04 22:44:48 +00001983 return MCDisassembler::Fail;
1984
1985 if (!tryAddingSymbolicOperand(Address, imm, false, 4, Inst, Decoder))
1986 Inst.addOperand(MCOperand::CreateImm(imm));
1987
1988 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
1989 return MCDisassembler::Fail;
1990
1991 return S;
1992}
Owen Anderson9b7bd152011-08-23 17:45:18 +00001993
Craig Topperf6e7e122012-03-27 07:21:54 +00001994static DecodeStatus DecodeSMLAInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00001995 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00001996 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00001997
Jim Grosbachecaef492012-08-14 19:06:05 +00001998 unsigned Rd = fieldFromInstruction(Insn, 16, 4);
1999 unsigned Rn = fieldFromInstruction(Insn, 0, 4);
2000 unsigned Rm = fieldFromInstruction(Insn, 8, 4);
2001 unsigned Ra = fieldFromInstruction(Insn, 12, 4);
2002 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00002003
2004 if (pred == 0xF)
2005 return DecodeCPSInstruction(Inst, Insn, Address, Decoder);
2006
Owen Anderson03aadae2011-09-01 23:23:50 +00002007 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
2008 return MCDisassembler::Fail;
2009 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
2010 return MCDisassembler::Fail;
2011 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
2012 return MCDisassembler::Fail;
2013 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Ra, Address, Decoder)))
2014 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002015
Owen Anderson03aadae2011-09-01 23:23:50 +00002016 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
2017 return MCDisassembler::Fail;
Owen Anderson2f7aa732011-08-11 22:05:38 +00002018
Owen Andersona4043c42011-08-17 17:44:15 +00002019 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002020}
2021
Craig Topperf6e7e122012-03-27 07:21:54 +00002022static DecodeStatus DecodeAddrModeImm12Operand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002023 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002024 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002025
Jim Grosbachecaef492012-08-14 19:06:05 +00002026 unsigned add = fieldFromInstruction(Val, 12, 1);
2027 unsigned imm = fieldFromInstruction(Val, 0, 12);
2028 unsigned Rn = fieldFromInstruction(Val, 13, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00002029
Owen Anderson03aadae2011-09-01 23:23:50 +00002030 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2031 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002032
2033 if (!add) imm *= -1;
2034 if (imm == 0 && !add) imm = INT32_MIN;
2035 Inst.addOperand(MCOperand::CreateImm(imm));
Kevin Enderby5dcda642011-10-04 22:44:48 +00002036 if (Rn == 15)
2037 tryAddingPcLoadReferenceComment(Address, Address + imm + 8, Decoder);
Owen Andersone0152a72011-08-09 20:55:18 +00002038
Owen Andersona4043c42011-08-17 17:44:15 +00002039 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002040}
2041
Craig Topperf6e7e122012-03-27 07:21:54 +00002042static DecodeStatus DecodeAddrMode5Operand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002043 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002044 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002045
Jim Grosbachecaef492012-08-14 19:06:05 +00002046 unsigned Rn = fieldFromInstruction(Val, 9, 4);
2047 unsigned U = fieldFromInstruction(Val, 8, 1);
2048 unsigned imm = fieldFromInstruction(Val, 0, 8);
Owen Andersone0152a72011-08-09 20:55:18 +00002049
Owen Anderson03aadae2011-09-01 23:23:50 +00002050 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2051 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002052
2053 if (U)
2054 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add, imm)));
2055 else
2056 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub, imm)));
2057
Owen Andersona4043c42011-08-17 17:44:15 +00002058 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002059}
2060
Craig Topperf6e7e122012-03-27 07:21:54 +00002061static DecodeStatus DecodeAddrMode7Operand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002062 uint64_t Address, const void *Decoder) {
2063 return DecodeGPRRegisterClass(Inst, Val, Address, Decoder);
2064}
2065
Owen Anderson03aadae2011-09-01 23:23:50 +00002066static DecodeStatus
Kevin Enderby40d4e472012-04-12 23:13:34 +00002067DecodeT2BInstruction(MCInst &Inst, unsigned Insn,
2068 uint64_t Address, const void *Decoder) {
Kevin Enderby6fd96242012-10-29 23:27:20 +00002069 DecodeStatus Status = MCDisassembler::Success;
2070
2071 // Note the J1 and J2 values are from the encoded instruction. So here
2072 // change them to I1 and I2 values via as documented:
2073 // I1 = NOT(J1 EOR S);
2074 // I2 = NOT(J2 EOR S);
2075 // and build the imm32 with one trailing zero as documented:
2076 // imm32 = SignExtend(S:I1:I2:imm10:imm11:'0', 32);
2077 unsigned S = fieldFromInstruction(Insn, 26, 1);
2078 unsigned J1 = fieldFromInstruction(Insn, 13, 1);
2079 unsigned J2 = fieldFromInstruction(Insn, 11, 1);
2080 unsigned I1 = !(J1 ^ S);
2081 unsigned I2 = !(J2 ^ S);
2082 unsigned imm10 = fieldFromInstruction(Insn, 16, 10);
2083 unsigned imm11 = fieldFromInstruction(Insn, 0, 11);
2084 unsigned tmp = (S << 23) | (I1 << 22) | (I2 << 21) | (imm10 << 11) | imm11;
2085 int imm32 = SignExtend32<24>(tmp << 1);
2086 if (!tryAddingSymbolicOperand(Address, Address + imm32 + 4,
Kevin Enderby40d4e472012-04-12 23:13:34 +00002087 true, 4, Inst, Decoder))
Kevin Enderby6fd96242012-10-29 23:27:20 +00002088 Inst.addOperand(MCOperand::CreateImm(imm32));
2089
2090 return Status;
Kevin Enderby40d4e472012-04-12 23:13:34 +00002091}
2092
2093static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00002094DecodeBranchImmInstruction(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00002095 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002096 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002097
Jim Grosbachecaef492012-08-14 19:06:05 +00002098 unsigned pred = fieldFromInstruction(Insn, 28, 4);
2099 unsigned imm = fieldFromInstruction(Insn, 0, 24) << 2;
Owen Andersone0152a72011-08-09 20:55:18 +00002100
2101 if (pred == 0xF) {
2102 Inst.setOpcode(ARM::BLXi);
Jim Grosbachecaef492012-08-14 19:06:05 +00002103 imm |= fieldFromInstruction(Insn, 24, 1) << 1;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00002104 if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<26>(imm) + 8,
2105 true, 4, Inst, Decoder))
Benjamin Kramer406dc172011-08-09 22:02:50 +00002106 Inst.addOperand(MCOperand::CreateImm(SignExtend32<26>(imm)));
Owen Andersona4043c42011-08-17 17:44:15 +00002107 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002108 }
2109
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00002110 if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<26>(imm) + 8,
2111 true, 4, Inst, Decoder))
Kevin Enderby5dcda642011-10-04 22:44:48 +00002112 Inst.addOperand(MCOperand::CreateImm(SignExtend32<26>(imm)));
Owen Anderson03aadae2011-09-01 23:23:50 +00002113 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
2114 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002115
Owen Andersona4043c42011-08-17 17:44:15 +00002116 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002117}
2118
2119
Craig Topperf6e7e122012-03-27 07:21:54 +00002120static DecodeStatus DecodeAddrMode6Operand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002121 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002122 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002123
Jim Grosbachecaef492012-08-14 19:06:05 +00002124 unsigned Rm = fieldFromInstruction(Val, 0, 4);
2125 unsigned align = fieldFromInstruction(Val, 4, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00002126
Owen Anderson03aadae2011-09-01 23:23:50 +00002127 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
2128 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002129 if (!align)
2130 Inst.addOperand(MCOperand::CreateImm(0));
2131 else
2132 Inst.addOperand(MCOperand::CreateImm(4 << align));
2133
Owen Andersona4043c42011-08-17 17:44:15 +00002134 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002135}
2136
Craig Topperf6e7e122012-03-27 07:21:54 +00002137static DecodeStatus DecodeVLDInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002138 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002139 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002140
Jim Grosbachecaef492012-08-14 19:06:05 +00002141 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2142 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2143 unsigned wb = fieldFromInstruction(Insn, 16, 4);
2144 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
2145 Rn |= fieldFromInstruction(Insn, 4, 2) << 4;
2146 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00002147
2148 // First output register
Jim Grosbachc988e0c2012-03-05 19:33:30 +00002149 switch (Inst.getOpcode()) {
Jim Grosbach13a292c2012-03-06 22:01:44 +00002150 case ARM::VLD1q16: case ARM::VLD1q32: case ARM::VLD1q64: case ARM::VLD1q8:
2151 case ARM::VLD1q16wb_fixed: case ARM::VLD1q16wb_register:
2152 case ARM::VLD1q32wb_fixed: case ARM::VLD1q32wb_register:
2153 case ARM::VLD1q64wb_fixed: case ARM::VLD1q64wb_register:
2154 case ARM::VLD1q8wb_fixed: case ARM::VLD1q8wb_register:
2155 case ARM::VLD2d16: case ARM::VLD2d32: case ARM::VLD2d8:
2156 case ARM::VLD2d16wb_fixed: case ARM::VLD2d16wb_register:
2157 case ARM::VLD2d32wb_fixed: case ARM::VLD2d32wb_register:
2158 case ARM::VLD2d8wb_fixed: case ARM::VLD2d8wb_register:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00002159 if (!Check(S, DecodeDPairRegisterClass(Inst, Rd, Address, Decoder)))
2160 return MCDisassembler::Fail;
2161 break;
Jim Grosbache5307f92012-03-05 21:43:40 +00002162 case ARM::VLD2b16:
2163 case ARM::VLD2b32:
2164 case ARM::VLD2b8:
2165 case ARM::VLD2b16wb_fixed:
2166 case ARM::VLD2b16wb_register:
2167 case ARM::VLD2b32wb_fixed:
2168 case ARM::VLD2b32wb_register:
2169 case ARM::VLD2b8wb_fixed:
2170 case ARM::VLD2b8wb_register:
2171 if (!Check(S, DecodeDPairSpacedRegisterClass(Inst, Rd, Address, Decoder)))
2172 return MCDisassembler::Fail;
2173 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00002174 default:
2175 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2176 return MCDisassembler::Fail;
2177 }
Owen Andersone0152a72011-08-09 20:55:18 +00002178
2179 // Second output register
2180 switch (Inst.getOpcode()) {
Owen Andersone0152a72011-08-09 20:55:18 +00002181 case ARM::VLD3d8:
2182 case ARM::VLD3d16:
2183 case ARM::VLD3d32:
2184 case ARM::VLD3d8_UPD:
2185 case ARM::VLD3d16_UPD:
2186 case ARM::VLD3d32_UPD:
2187 case ARM::VLD4d8:
2188 case ARM::VLD4d16:
2189 case ARM::VLD4d32:
2190 case ARM::VLD4d8_UPD:
2191 case ARM::VLD4d16_UPD:
2192 case ARM::VLD4d32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002193 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder)))
2194 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002195 break;
Owen Andersone0152a72011-08-09 20:55:18 +00002196 case ARM::VLD3q8:
2197 case ARM::VLD3q16:
2198 case ARM::VLD3q32:
2199 case ARM::VLD3q8_UPD:
2200 case ARM::VLD3q16_UPD:
2201 case ARM::VLD3q32_UPD:
2202 case ARM::VLD4q8:
2203 case ARM::VLD4q16:
2204 case ARM::VLD4q32:
2205 case ARM::VLD4q8_UPD:
2206 case ARM::VLD4q16_UPD:
2207 case ARM::VLD4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002208 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder)))
2209 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002210 default:
2211 break;
2212 }
2213
2214 // Third output register
2215 switch(Inst.getOpcode()) {
Owen Andersone0152a72011-08-09 20:55:18 +00002216 case ARM::VLD3d8:
2217 case ARM::VLD3d16:
2218 case ARM::VLD3d32:
2219 case ARM::VLD3d8_UPD:
2220 case ARM::VLD3d16_UPD:
2221 case ARM::VLD3d32_UPD:
2222 case ARM::VLD4d8:
2223 case ARM::VLD4d16:
2224 case ARM::VLD4d32:
2225 case ARM::VLD4d8_UPD:
2226 case ARM::VLD4d16_UPD:
2227 case ARM::VLD4d32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002228 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder)))
2229 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002230 break;
2231 case ARM::VLD3q8:
2232 case ARM::VLD3q16:
2233 case ARM::VLD3q32:
2234 case ARM::VLD3q8_UPD:
2235 case ARM::VLD3q16_UPD:
2236 case ARM::VLD3q32_UPD:
2237 case ARM::VLD4q8:
2238 case ARM::VLD4q16:
2239 case ARM::VLD4q32:
2240 case ARM::VLD4q8_UPD:
2241 case ARM::VLD4q16_UPD:
2242 case ARM::VLD4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002243 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+4)%32, Address, Decoder)))
2244 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002245 break;
2246 default:
2247 break;
2248 }
2249
2250 // Fourth output register
2251 switch (Inst.getOpcode()) {
Owen Andersone0152a72011-08-09 20:55:18 +00002252 case ARM::VLD4d8:
2253 case ARM::VLD4d16:
2254 case ARM::VLD4d32:
2255 case ARM::VLD4d8_UPD:
2256 case ARM::VLD4d16_UPD:
2257 case ARM::VLD4d32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002258 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+3)%32, Address, Decoder)))
2259 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002260 break;
2261 case ARM::VLD4q8:
2262 case ARM::VLD4q16:
2263 case ARM::VLD4q32:
2264 case ARM::VLD4q8_UPD:
2265 case ARM::VLD4q16_UPD:
2266 case ARM::VLD4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002267 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+6)%32, Address, Decoder)))
2268 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002269 break;
2270 default:
2271 break;
2272 }
2273
2274 // Writeback operand
2275 switch (Inst.getOpcode()) {
Jim Grosbach2098cb12011-10-24 21:45:13 +00002276 case ARM::VLD1d8wb_fixed:
2277 case ARM::VLD1d16wb_fixed:
2278 case ARM::VLD1d32wb_fixed:
2279 case ARM::VLD1d64wb_fixed:
2280 case ARM::VLD1d8wb_register:
2281 case ARM::VLD1d16wb_register:
2282 case ARM::VLD1d32wb_register:
2283 case ARM::VLD1d64wb_register:
2284 case ARM::VLD1q8wb_fixed:
2285 case ARM::VLD1q16wb_fixed:
2286 case ARM::VLD1q32wb_fixed:
2287 case ARM::VLD1q64wb_fixed:
2288 case ARM::VLD1q8wb_register:
2289 case ARM::VLD1q16wb_register:
2290 case ARM::VLD1q32wb_register:
2291 case ARM::VLD1q64wb_register:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00002292 case ARM::VLD1d8Twb_fixed:
2293 case ARM::VLD1d8Twb_register:
2294 case ARM::VLD1d16Twb_fixed:
2295 case ARM::VLD1d16Twb_register:
2296 case ARM::VLD1d32Twb_fixed:
2297 case ARM::VLD1d32Twb_register:
2298 case ARM::VLD1d64Twb_fixed:
2299 case ARM::VLD1d64Twb_register:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00002300 case ARM::VLD1d8Qwb_fixed:
2301 case ARM::VLD1d8Qwb_register:
2302 case ARM::VLD1d16Qwb_fixed:
2303 case ARM::VLD1d16Qwb_register:
2304 case ARM::VLD1d32Qwb_fixed:
2305 case ARM::VLD1d32Qwb_register:
2306 case ARM::VLD1d64Qwb_fixed:
2307 case ARM::VLD1d64Qwb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00002308 case ARM::VLD2d8wb_fixed:
2309 case ARM::VLD2d16wb_fixed:
2310 case ARM::VLD2d32wb_fixed:
2311 case ARM::VLD2q8wb_fixed:
2312 case ARM::VLD2q16wb_fixed:
2313 case ARM::VLD2q32wb_fixed:
2314 case ARM::VLD2d8wb_register:
2315 case ARM::VLD2d16wb_register:
2316 case ARM::VLD2d32wb_register:
2317 case ARM::VLD2q8wb_register:
2318 case ARM::VLD2q16wb_register:
2319 case ARM::VLD2q32wb_register:
2320 case ARM::VLD2b8wb_fixed:
2321 case ARM::VLD2b16wb_fixed:
2322 case ARM::VLD2b32wb_fixed:
2323 case ARM::VLD2b8wb_register:
2324 case ARM::VLD2b16wb_register:
2325 case ARM::VLD2b32wb_register:
Kevin Enderbyd2980cd2012-04-11 00:25:40 +00002326 Inst.addOperand(MCOperand::CreateImm(0));
2327 break;
Owen Andersone0152a72011-08-09 20:55:18 +00002328 case ARM::VLD3d8_UPD:
2329 case ARM::VLD3d16_UPD:
2330 case ARM::VLD3d32_UPD:
2331 case ARM::VLD3q8_UPD:
2332 case ARM::VLD3q16_UPD:
2333 case ARM::VLD3q32_UPD:
2334 case ARM::VLD4d8_UPD:
2335 case ARM::VLD4d16_UPD:
2336 case ARM::VLD4d32_UPD:
2337 case ARM::VLD4q8_UPD:
2338 case ARM::VLD4q16_UPD:
2339 case ARM::VLD4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002340 if (!Check(S, DecodeGPRRegisterClass(Inst, wb, Address, Decoder)))
2341 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002342 break;
2343 default:
2344 break;
2345 }
2346
2347 // AddrMode6 Base (register+alignment)
Owen Anderson03aadae2011-09-01 23:23:50 +00002348 if (!Check(S, DecodeAddrMode6Operand(Inst, Rn, Address, Decoder)))
2349 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002350
2351 // AddrMode6 Offset (register)
Jim Grosbach2098cb12011-10-24 21:45:13 +00002352 switch (Inst.getOpcode()) {
2353 default:
2354 // The below have been updated to have explicit am6offset split
2355 // between fixed and register offset. For those instructions not
2356 // yet updated, we need to add an additional reg0 operand for the
2357 // fixed variant.
2358 //
2359 // The fixed offset encodes as Rm == 0xd, so we check for that.
2360 if (Rm == 0xd) {
2361 Inst.addOperand(MCOperand::CreateReg(0));
2362 break;
2363 }
2364 // Fall through to handle the register offset variant.
2365 case ARM::VLD1d8wb_fixed:
2366 case ARM::VLD1d16wb_fixed:
2367 case ARM::VLD1d32wb_fixed:
2368 case ARM::VLD1d64wb_fixed:
Owen Anderson8a6ebd02011-10-27 22:53:10 +00002369 case ARM::VLD1d8Twb_fixed:
2370 case ARM::VLD1d16Twb_fixed:
2371 case ARM::VLD1d32Twb_fixed:
2372 case ARM::VLD1d64Twb_fixed:
Owen Anderson40703f42011-10-31 17:17:32 +00002373 case ARM::VLD1d8Qwb_fixed:
2374 case ARM::VLD1d16Qwb_fixed:
2375 case ARM::VLD1d32Qwb_fixed:
2376 case ARM::VLD1d64Qwb_fixed:
Jim Grosbach2098cb12011-10-24 21:45:13 +00002377 case ARM::VLD1d8wb_register:
2378 case ARM::VLD1d16wb_register:
2379 case ARM::VLD1d32wb_register:
2380 case ARM::VLD1d64wb_register:
2381 case ARM::VLD1q8wb_fixed:
2382 case ARM::VLD1q16wb_fixed:
2383 case ARM::VLD1q32wb_fixed:
2384 case ARM::VLD1q64wb_fixed:
2385 case ARM::VLD1q8wb_register:
2386 case ARM::VLD1q16wb_register:
2387 case ARM::VLD1q32wb_register:
2388 case ARM::VLD1q64wb_register:
2389 // The fixed offset post-increment encodes Rm == 0xd. The no-writeback
2390 // variant encodes Rm == 0xf. Anything else is a register offset post-
2391 // increment and we need to add the register operand to the instruction.
2392 if (Rm != 0xD && Rm != 0xF &&
2393 !Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
Owen Anderson03aadae2011-09-01 23:23:50 +00002394 return MCDisassembler::Fail;
Jim Grosbach2098cb12011-10-24 21:45:13 +00002395 break;
Kevin Enderbyd2980cd2012-04-11 00:25:40 +00002396 case ARM::VLD2d8wb_fixed:
2397 case ARM::VLD2d16wb_fixed:
2398 case ARM::VLD2d32wb_fixed:
2399 case ARM::VLD2b8wb_fixed:
2400 case ARM::VLD2b16wb_fixed:
2401 case ARM::VLD2b32wb_fixed:
2402 case ARM::VLD2q8wb_fixed:
2403 case ARM::VLD2q16wb_fixed:
2404 case ARM::VLD2q32wb_fixed:
2405 break;
Owen Andersoned253852011-08-11 18:24:51 +00002406 }
Owen Andersone0152a72011-08-09 20:55:18 +00002407
Owen Andersona4043c42011-08-17 17:44:15 +00002408 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002409}
2410
Mihai Popaf41e3f52013-05-20 14:57:05 +00002411static DecodeStatus DecodeVST1Instruction(MCInst& Inst, unsigned Insn,
2412 uint64_t Addr, const void* Decoder) {
2413 unsigned type = fieldFromInstruction(Insn, 8, 4);
2414 unsigned align = fieldFromInstruction(Insn, 4, 2);
2415 if(type == 7 && (align & 2)) return MCDisassembler::Fail;
2416 if(type == 10 && align == 3) return MCDisassembler::Fail;
2417 if(type == 6 && (align & 2)) return MCDisassembler::Fail;
2418
2419 return DecodeVSTInstruction(Inst, Insn, Addr, Decoder);
2420}
2421
2422static DecodeStatus DecodeVST2Instruction(MCInst& Inst, unsigned Insn,
2423 uint64_t Addr, const void* Decoder) {
2424 unsigned size = fieldFromInstruction(Insn, 6, 2);
2425 if(size == 3) return MCDisassembler::Fail;
2426
2427 unsigned type = fieldFromInstruction(Insn, 8, 4);
2428 unsigned align = fieldFromInstruction(Insn, 4, 2);
2429 if(type == 8 && align == 3) return MCDisassembler::Fail;
2430 if(type == 9 && align == 3) return MCDisassembler::Fail;
2431
2432 return DecodeVSTInstruction(Inst, Insn, Addr, Decoder);
2433}
2434
2435static DecodeStatus DecodeVST3Instruction(MCInst& Inst, unsigned Insn,
2436 uint64_t Addr, const void* Decoder) {
2437 unsigned size = fieldFromInstruction(Insn, 6, 2);
2438 if(size == 3) return MCDisassembler::Fail;
2439
2440 unsigned align = fieldFromInstruction(Insn, 4, 2);
2441 if(align & 2) return MCDisassembler::Fail;
2442
2443 return DecodeVSTInstruction(Inst, Insn, Addr, Decoder);
2444}
2445
2446static DecodeStatus DecodeVST4Instruction(MCInst& Inst, unsigned Insn,
2447 uint64_t Addr, const void* Decoder) {
2448 unsigned size = fieldFromInstruction(Insn, 6, 2);
2449 if(size == 3) return MCDisassembler::Fail;
2450
2451 return DecodeVSTInstruction(Inst, Insn, Addr, Decoder);
2452}
2453
Craig Topperf6e7e122012-03-27 07:21:54 +00002454static DecodeStatus DecodeVSTInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002455 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002456 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002457
Jim Grosbachecaef492012-08-14 19:06:05 +00002458 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2459 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2460 unsigned wb = fieldFromInstruction(Insn, 16, 4);
2461 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
2462 Rn |= fieldFromInstruction(Insn, 4, 2) << 4;
2463 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00002464
2465 // Writeback Operand
2466 switch (Inst.getOpcode()) {
Jim Grosbach05df4602011-10-31 21:50:31 +00002467 case ARM::VST1d8wb_fixed:
2468 case ARM::VST1d16wb_fixed:
2469 case ARM::VST1d32wb_fixed:
2470 case ARM::VST1d64wb_fixed:
2471 case ARM::VST1d8wb_register:
2472 case ARM::VST1d16wb_register:
2473 case ARM::VST1d32wb_register:
2474 case ARM::VST1d64wb_register:
2475 case ARM::VST1q8wb_fixed:
2476 case ARM::VST1q16wb_fixed:
2477 case ARM::VST1q32wb_fixed:
2478 case ARM::VST1q64wb_fixed:
2479 case ARM::VST1q8wb_register:
2480 case ARM::VST1q16wb_register:
2481 case ARM::VST1q32wb_register:
2482 case ARM::VST1q64wb_register:
Jim Grosbach98d032f2011-11-29 22:38:04 +00002483 case ARM::VST1d8Twb_fixed:
2484 case ARM::VST1d16Twb_fixed:
2485 case ARM::VST1d32Twb_fixed:
2486 case ARM::VST1d64Twb_fixed:
2487 case ARM::VST1d8Twb_register:
2488 case ARM::VST1d16Twb_register:
2489 case ARM::VST1d32Twb_register:
2490 case ARM::VST1d64Twb_register:
Jim Grosbach5ee209c2011-11-29 22:58:48 +00002491 case ARM::VST1d8Qwb_fixed:
2492 case ARM::VST1d16Qwb_fixed:
2493 case ARM::VST1d32Qwb_fixed:
2494 case ARM::VST1d64Qwb_fixed:
2495 case ARM::VST1d8Qwb_register:
2496 case ARM::VST1d16Qwb_register:
2497 case ARM::VST1d32Qwb_register:
2498 case ARM::VST1d64Qwb_register:
Jim Grosbach88ac7612011-12-14 21:32:11 +00002499 case ARM::VST2d8wb_fixed:
2500 case ARM::VST2d16wb_fixed:
2501 case ARM::VST2d32wb_fixed:
2502 case ARM::VST2d8wb_register:
2503 case ARM::VST2d16wb_register:
2504 case ARM::VST2d32wb_register:
2505 case ARM::VST2q8wb_fixed:
2506 case ARM::VST2q16wb_fixed:
2507 case ARM::VST2q32wb_fixed:
2508 case ARM::VST2q8wb_register:
2509 case ARM::VST2q16wb_register:
2510 case ARM::VST2q32wb_register:
2511 case ARM::VST2b8wb_fixed:
2512 case ARM::VST2b16wb_fixed:
2513 case ARM::VST2b32wb_fixed:
2514 case ARM::VST2b8wb_register:
2515 case ARM::VST2b16wb_register:
2516 case ARM::VST2b32wb_register:
Kevin Enderby72f18bb2012-04-11 22:40:17 +00002517 if (Rm == 0xF)
2518 return MCDisassembler::Fail;
Kevin Enderby7e7d5ee2012-03-21 20:54:32 +00002519 Inst.addOperand(MCOperand::CreateImm(0));
2520 break;
Owen Andersone0152a72011-08-09 20:55:18 +00002521 case ARM::VST3d8_UPD:
2522 case ARM::VST3d16_UPD:
2523 case ARM::VST3d32_UPD:
2524 case ARM::VST3q8_UPD:
2525 case ARM::VST3q16_UPD:
2526 case ARM::VST3q32_UPD:
2527 case ARM::VST4d8_UPD:
2528 case ARM::VST4d16_UPD:
2529 case ARM::VST4d32_UPD:
2530 case ARM::VST4q8_UPD:
2531 case ARM::VST4q16_UPD:
2532 case ARM::VST4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002533 if (!Check(S, DecodeGPRRegisterClass(Inst, wb, Address, Decoder)))
2534 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002535 break;
2536 default:
2537 break;
2538 }
2539
2540 // AddrMode6 Base (register+alignment)
Owen Anderson03aadae2011-09-01 23:23:50 +00002541 if (!Check(S, DecodeAddrMode6Operand(Inst, Rn, Address, Decoder)))
2542 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002543
2544 // AddrMode6 Offset (register)
Owen Anderson69e54a72011-11-01 22:18:13 +00002545 switch (Inst.getOpcode()) {
2546 default:
2547 if (Rm == 0xD)
2548 Inst.addOperand(MCOperand::CreateReg(0));
2549 else if (Rm != 0xF) {
2550 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
2551 return MCDisassembler::Fail;
2552 }
2553 break;
2554 case ARM::VST1d8wb_fixed:
2555 case ARM::VST1d16wb_fixed:
2556 case ARM::VST1d32wb_fixed:
2557 case ARM::VST1d64wb_fixed:
2558 case ARM::VST1q8wb_fixed:
2559 case ARM::VST1q16wb_fixed:
2560 case ARM::VST1q32wb_fixed:
2561 case ARM::VST1q64wb_fixed:
Kevin Enderby7e7d5ee2012-03-21 20:54:32 +00002562 case ARM::VST1d8Twb_fixed:
2563 case ARM::VST1d16Twb_fixed:
2564 case ARM::VST1d32Twb_fixed:
2565 case ARM::VST1d64Twb_fixed:
2566 case ARM::VST1d8Qwb_fixed:
2567 case ARM::VST1d16Qwb_fixed:
2568 case ARM::VST1d32Qwb_fixed:
2569 case ARM::VST1d64Qwb_fixed:
2570 case ARM::VST2d8wb_fixed:
2571 case ARM::VST2d16wb_fixed:
2572 case ARM::VST2d32wb_fixed:
2573 case ARM::VST2q8wb_fixed:
2574 case ARM::VST2q16wb_fixed:
2575 case ARM::VST2q32wb_fixed:
2576 case ARM::VST2b8wb_fixed:
2577 case ARM::VST2b16wb_fixed:
2578 case ARM::VST2b32wb_fixed:
Owen Anderson69e54a72011-11-01 22:18:13 +00002579 break;
Owen Andersoned253852011-08-11 18:24:51 +00002580 }
Owen Andersone0152a72011-08-09 20:55:18 +00002581
Owen Anderson69e54a72011-11-01 22:18:13 +00002582
Owen Andersone0152a72011-08-09 20:55:18 +00002583 // First input register
Jim Grosbachc988e0c2012-03-05 19:33:30 +00002584 switch (Inst.getOpcode()) {
2585 case ARM::VST1q16:
2586 case ARM::VST1q32:
2587 case ARM::VST1q64:
2588 case ARM::VST1q8:
2589 case ARM::VST1q16wb_fixed:
2590 case ARM::VST1q16wb_register:
2591 case ARM::VST1q32wb_fixed:
2592 case ARM::VST1q32wb_register:
2593 case ARM::VST1q64wb_fixed:
2594 case ARM::VST1q64wb_register:
2595 case ARM::VST1q8wb_fixed:
2596 case ARM::VST1q8wb_register:
2597 case ARM::VST2d16:
2598 case ARM::VST2d32:
2599 case ARM::VST2d8:
2600 case ARM::VST2d16wb_fixed:
2601 case ARM::VST2d16wb_register:
2602 case ARM::VST2d32wb_fixed:
2603 case ARM::VST2d32wb_register:
2604 case ARM::VST2d8wb_fixed:
2605 case ARM::VST2d8wb_register:
2606 if (!Check(S, DecodeDPairRegisterClass(Inst, Rd, Address, Decoder)))
2607 return MCDisassembler::Fail;
2608 break;
Jim Grosbache5307f92012-03-05 21:43:40 +00002609 case ARM::VST2b16:
2610 case ARM::VST2b32:
2611 case ARM::VST2b8:
2612 case ARM::VST2b16wb_fixed:
2613 case ARM::VST2b16wb_register:
2614 case ARM::VST2b32wb_fixed:
2615 case ARM::VST2b32wb_register:
2616 case ARM::VST2b8wb_fixed:
2617 case ARM::VST2b8wb_register:
2618 if (!Check(S, DecodeDPairSpacedRegisterClass(Inst, Rd, Address, Decoder)))
2619 return MCDisassembler::Fail;
2620 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00002621 default:
2622 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2623 return MCDisassembler::Fail;
2624 }
Owen Andersone0152a72011-08-09 20:55:18 +00002625
2626 // Second input register
2627 switch (Inst.getOpcode()) {
Owen Andersone0152a72011-08-09 20:55:18 +00002628 case ARM::VST3d8:
2629 case ARM::VST3d16:
2630 case ARM::VST3d32:
2631 case ARM::VST3d8_UPD:
2632 case ARM::VST3d16_UPD:
2633 case ARM::VST3d32_UPD:
2634 case ARM::VST4d8:
2635 case ARM::VST4d16:
2636 case ARM::VST4d32:
2637 case ARM::VST4d8_UPD:
2638 case ARM::VST4d16_UPD:
2639 case ARM::VST4d32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002640 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder)))
2641 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002642 break;
Owen Andersone0152a72011-08-09 20:55:18 +00002643 case ARM::VST3q8:
2644 case ARM::VST3q16:
2645 case ARM::VST3q32:
2646 case ARM::VST3q8_UPD:
2647 case ARM::VST3q16_UPD:
2648 case ARM::VST3q32_UPD:
2649 case ARM::VST4q8:
2650 case ARM::VST4q16:
2651 case ARM::VST4q32:
2652 case ARM::VST4q8_UPD:
2653 case ARM::VST4q16_UPD:
2654 case ARM::VST4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002655 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder)))
2656 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002657 break;
2658 default:
2659 break;
2660 }
2661
2662 // Third input register
2663 switch (Inst.getOpcode()) {
Owen Andersone0152a72011-08-09 20:55:18 +00002664 case ARM::VST3d8:
2665 case ARM::VST3d16:
2666 case ARM::VST3d32:
2667 case ARM::VST3d8_UPD:
2668 case ARM::VST3d16_UPD:
2669 case ARM::VST3d32_UPD:
2670 case ARM::VST4d8:
2671 case ARM::VST4d16:
2672 case ARM::VST4d32:
2673 case ARM::VST4d8_UPD:
2674 case ARM::VST4d16_UPD:
2675 case ARM::VST4d32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002676 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder)))
2677 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002678 break;
2679 case ARM::VST3q8:
2680 case ARM::VST3q16:
2681 case ARM::VST3q32:
2682 case ARM::VST3q8_UPD:
2683 case ARM::VST3q16_UPD:
2684 case ARM::VST3q32_UPD:
2685 case ARM::VST4q8:
2686 case ARM::VST4q16:
2687 case ARM::VST4q32:
2688 case ARM::VST4q8_UPD:
2689 case ARM::VST4q16_UPD:
2690 case ARM::VST4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002691 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+4)%32, Address, Decoder)))
2692 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002693 break;
2694 default:
2695 break;
2696 }
2697
2698 // Fourth input register
2699 switch (Inst.getOpcode()) {
Owen Andersone0152a72011-08-09 20:55:18 +00002700 case ARM::VST4d8:
2701 case ARM::VST4d16:
2702 case ARM::VST4d32:
2703 case ARM::VST4d8_UPD:
2704 case ARM::VST4d16_UPD:
2705 case ARM::VST4d32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002706 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+3)%32, Address, Decoder)))
2707 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002708 break;
2709 case ARM::VST4q8:
2710 case ARM::VST4q16:
2711 case ARM::VST4q32:
2712 case ARM::VST4q8_UPD:
2713 case ARM::VST4q16_UPD:
2714 case ARM::VST4q32_UPD:
Owen Anderson03aadae2011-09-01 23:23:50 +00002715 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+6)%32, Address, Decoder)))
2716 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002717 break;
2718 default:
2719 break;
2720 }
2721
Owen Andersona4043c42011-08-17 17:44:15 +00002722 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002723}
2724
Craig Topperf6e7e122012-03-27 07:21:54 +00002725static DecodeStatus DecodeVLD1DupInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002726 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002727 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002728
Jim Grosbachecaef492012-08-14 19:06:05 +00002729 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2730 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2731 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
2732 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
2733 unsigned align = fieldFromInstruction(Insn, 4, 1);
2734 unsigned size = fieldFromInstruction(Insn, 6, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00002735
Tim Northover00e071a2012-09-06 15:27:12 +00002736 if (size == 0 && align == 1)
2737 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002738 align *= (1 << size);
2739
Jim Grosbach13a292c2012-03-06 22:01:44 +00002740 switch (Inst.getOpcode()) {
2741 case ARM::VLD1DUPq16: case ARM::VLD1DUPq32: case ARM::VLD1DUPq8:
2742 case ARM::VLD1DUPq16wb_fixed: case ARM::VLD1DUPq16wb_register:
2743 case ARM::VLD1DUPq32wb_fixed: case ARM::VLD1DUPq32wb_register:
2744 case ARM::VLD1DUPq8wb_fixed: case ARM::VLD1DUPq8wb_register:
2745 if (!Check(S, DecodeDPairRegisterClass(Inst, Rd, Address, Decoder)))
2746 return MCDisassembler::Fail;
2747 break;
2748 default:
2749 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2750 return MCDisassembler::Fail;
2751 break;
2752 }
Owen Andersonac92e772011-08-22 18:22:06 +00002753 if (Rm != 0xF) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002754 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2755 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002756 }
Owen Andersone0152a72011-08-09 20:55:18 +00002757
Owen Anderson03aadae2011-09-01 23:23:50 +00002758 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2759 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002760 Inst.addOperand(MCOperand::CreateImm(align));
2761
Jim Grosbacha68c9a82011-11-30 19:35:44 +00002762 // The fixed offset post-increment encodes Rm == 0xd. The no-writeback
2763 // variant encodes Rm == 0xf. Anything else is a register offset post-
2764 // increment and we need to add the register operand to the instruction.
2765 if (Rm != 0xD && Rm != 0xF &&
2766 !Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
2767 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002768
Owen Andersona4043c42011-08-17 17:44:15 +00002769 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002770}
2771
Craig Topperf6e7e122012-03-27 07:21:54 +00002772static DecodeStatus DecodeVLD2DupInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002773 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002774 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002775
Jim Grosbachecaef492012-08-14 19:06:05 +00002776 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2777 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2778 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
2779 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
2780 unsigned align = fieldFromInstruction(Insn, 4, 1);
2781 unsigned size = 1 << fieldFromInstruction(Insn, 6, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00002782 align *= 2*size;
2783
Jim Grosbach13a292c2012-03-06 22:01:44 +00002784 switch (Inst.getOpcode()) {
2785 case ARM::VLD2DUPd16: case ARM::VLD2DUPd32: case ARM::VLD2DUPd8:
2786 case ARM::VLD2DUPd16wb_fixed: case ARM::VLD2DUPd16wb_register:
2787 case ARM::VLD2DUPd32wb_fixed: case ARM::VLD2DUPd32wb_register:
2788 case ARM::VLD2DUPd8wb_fixed: case ARM::VLD2DUPd8wb_register:
2789 if (!Check(S, DecodeDPairRegisterClass(Inst, Rd, Address, Decoder)))
2790 return MCDisassembler::Fail;
2791 break;
Jim Grosbached428bc2012-03-06 23:10:38 +00002792 case ARM::VLD2DUPd16x2: case ARM::VLD2DUPd32x2: case ARM::VLD2DUPd8x2:
2793 case ARM::VLD2DUPd16x2wb_fixed: case ARM::VLD2DUPd16x2wb_register:
2794 case ARM::VLD2DUPd32x2wb_fixed: case ARM::VLD2DUPd32x2wb_register:
2795 case ARM::VLD2DUPd8x2wb_fixed: case ARM::VLD2DUPd8x2wb_register:
2796 if (!Check(S, DecodeDPairSpacedRegisterClass(Inst, Rd, Address, Decoder)))
2797 return MCDisassembler::Fail;
2798 break;
Jim Grosbach13a292c2012-03-06 22:01:44 +00002799 default:
2800 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2801 return MCDisassembler::Fail;
2802 break;
2803 }
Kevin Enderby520eb3b2012-03-06 18:33:12 +00002804
2805 if (Rm != 0xF)
2806 Inst.addOperand(MCOperand::CreateImm(0));
Owen Andersone0152a72011-08-09 20:55:18 +00002807
Owen Anderson03aadae2011-09-01 23:23:50 +00002808 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2809 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002810 Inst.addOperand(MCOperand::CreateImm(align));
2811
Kevin Enderby29ae5382012-04-17 00:49:27 +00002812 if (Rm != 0xD && Rm != 0xF) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002813 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
2814 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002815 }
Owen Andersone0152a72011-08-09 20:55:18 +00002816
Owen Andersona4043c42011-08-17 17:44:15 +00002817 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002818}
2819
Craig Topperf6e7e122012-03-27 07:21:54 +00002820static DecodeStatus DecodeVLD3DupInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002821 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002822 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002823
Jim Grosbachecaef492012-08-14 19:06:05 +00002824 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2825 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2826 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
2827 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
2828 unsigned inc = fieldFromInstruction(Insn, 5, 1) + 1;
Owen Andersone0152a72011-08-09 20:55:18 +00002829
Owen Anderson03aadae2011-09-01 23:23:50 +00002830 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2831 return MCDisassembler::Fail;
2832 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder)))
2833 return MCDisassembler::Fail;
2834 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2*inc)%32, Address, Decoder)))
2835 return MCDisassembler::Fail;
Owen Andersonac92e772011-08-22 18:22:06 +00002836 if (Rm != 0xF) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002837 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2838 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002839 }
Owen Andersone0152a72011-08-09 20:55:18 +00002840
Owen Anderson03aadae2011-09-01 23:23:50 +00002841 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2842 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002843 Inst.addOperand(MCOperand::CreateImm(0));
2844
2845 if (Rm == 0xD)
2846 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersoned253852011-08-11 18:24:51 +00002847 else if (Rm != 0xF) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002848 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
2849 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002850 }
Owen Andersone0152a72011-08-09 20:55:18 +00002851
Owen Andersona4043c42011-08-17 17:44:15 +00002852 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002853}
2854
Craig Topperf6e7e122012-03-27 07:21:54 +00002855static DecodeStatus DecodeVLD4DupInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002856 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002857 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002858
Jim Grosbachecaef492012-08-14 19:06:05 +00002859 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2860 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2861 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
2862 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
2863 unsigned size = fieldFromInstruction(Insn, 6, 2);
2864 unsigned inc = fieldFromInstruction(Insn, 5, 1) + 1;
2865 unsigned align = fieldFromInstruction(Insn, 4, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00002866
2867 if (size == 0x3) {
Tim Northover00e071a2012-09-06 15:27:12 +00002868 if (align == 0)
2869 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002870 size = 4;
2871 align = 16;
2872 } else {
2873 if (size == 2) {
2874 size = 1 << size;
2875 align *= 8;
2876 } else {
2877 size = 1 << size;
2878 align *= 4*size;
2879 }
2880 }
2881
Owen Anderson03aadae2011-09-01 23:23:50 +00002882 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2883 return MCDisassembler::Fail;
2884 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder)))
2885 return MCDisassembler::Fail;
2886 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2*inc)%32, Address, Decoder)))
2887 return MCDisassembler::Fail;
2888 if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+3*inc)%32, Address, Decoder)))
2889 return MCDisassembler::Fail;
Owen Andersonac92e772011-08-22 18:22:06 +00002890 if (Rm != 0xF) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002891 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2892 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002893 }
Owen Andersone0152a72011-08-09 20:55:18 +00002894
Owen Anderson03aadae2011-09-01 23:23:50 +00002895 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
2896 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002897 Inst.addOperand(MCOperand::CreateImm(align));
2898
2899 if (Rm == 0xD)
2900 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersoned253852011-08-11 18:24:51 +00002901 else if (Rm != 0xF) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002902 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
2903 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002904 }
Owen Andersone0152a72011-08-09 20:55:18 +00002905
Owen Andersona4043c42011-08-17 17:44:15 +00002906 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002907}
2908
Owen Anderson03aadae2011-09-01 23:23:50 +00002909static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00002910DecodeNEONModImmInstruction(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00002911 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002912 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002913
Jim Grosbachecaef492012-08-14 19:06:05 +00002914 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2915 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2916 unsigned imm = fieldFromInstruction(Insn, 0, 4);
2917 imm |= fieldFromInstruction(Insn, 16, 3) << 4;
2918 imm |= fieldFromInstruction(Insn, 24, 1) << 7;
2919 imm |= fieldFromInstruction(Insn, 8, 4) << 8;
2920 imm |= fieldFromInstruction(Insn, 5, 1) << 12;
2921 unsigned Q = fieldFromInstruction(Insn, 6, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00002922
Owen Andersoned253852011-08-11 18:24:51 +00002923 if (Q) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002924 if (!Check(S, DecodeQPRRegisterClass(Inst, Rd, Address, Decoder)))
2925 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002926 } else {
Owen Anderson03aadae2011-09-01 23:23:50 +00002927 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2928 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00002929 }
Owen Andersone0152a72011-08-09 20:55:18 +00002930
2931 Inst.addOperand(MCOperand::CreateImm(imm));
2932
2933 switch (Inst.getOpcode()) {
2934 case ARM::VORRiv4i16:
2935 case ARM::VORRiv2i32:
2936 case ARM::VBICiv4i16:
2937 case ARM::VBICiv2i32:
Owen Anderson03aadae2011-09-01 23:23:50 +00002938 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
2939 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002940 break;
2941 case ARM::VORRiv8i16:
2942 case ARM::VORRiv4i32:
2943 case ARM::VBICiv8i16:
2944 case ARM::VBICiv4i32:
Owen Anderson03aadae2011-09-01 23:23:50 +00002945 if (!Check(S, DecodeQPRRegisterClass(Inst, Rd, Address, Decoder)))
2946 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002947 break;
2948 default:
2949 break;
2950 }
2951
Owen Andersona4043c42011-08-17 17:44:15 +00002952 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002953}
2954
Craig Topperf6e7e122012-03-27 07:21:54 +00002955static DecodeStatus DecodeVSHLMaxInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002956 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00002957 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00002958
Jim Grosbachecaef492012-08-14 19:06:05 +00002959 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
2960 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
2961 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
2962 Rm |= fieldFromInstruction(Insn, 5, 1) << 4;
2963 unsigned size = fieldFromInstruction(Insn, 18, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00002964
Owen Anderson03aadae2011-09-01 23:23:50 +00002965 if (!Check(S, DecodeQPRRegisterClass(Inst, Rd, Address, Decoder)))
2966 return MCDisassembler::Fail;
2967 if (!Check(S, DecodeDPRRegisterClass(Inst, Rm, Address, Decoder)))
2968 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00002969 Inst.addOperand(MCOperand::CreateImm(8 << size));
2970
Owen Andersona4043c42011-08-17 17:44:15 +00002971 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00002972}
2973
Craig Topperf6e7e122012-03-27 07:21:54 +00002974static DecodeStatus DecodeShiftRight8Imm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002975 uint64_t Address, const void *Decoder) {
2976 Inst.addOperand(MCOperand::CreateImm(8 - Val));
James Molloydb4ce602011-09-01 18:02:14 +00002977 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00002978}
2979
Craig Topperf6e7e122012-03-27 07:21:54 +00002980static DecodeStatus DecodeShiftRight16Imm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002981 uint64_t Address, const void *Decoder) {
2982 Inst.addOperand(MCOperand::CreateImm(16 - Val));
James Molloydb4ce602011-09-01 18:02:14 +00002983 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00002984}
2985
Craig Topperf6e7e122012-03-27 07:21:54 +00002986static DecodeStatus DecodeShiftRight32Imm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002987 uint64_t Address, const void *Decoder) {
2988 Inst.addOperand(MCOperand::CreateImm(32 - Val));
James Molloydb4ce602011-09-01 18:02:14 +00002989 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00002990}
2991
Craig Topperf6e7e122012-03-27 07:21:54 +00002992static DecodeStatus DecodeShiftRight64Imm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00002993 uint64_t Address, const void *Decoder) {
2994 Inst.addOperand(MCOperand::CreateImm(64 - Val));
James Molloydb4ce602011-09-01 18:02:14 +00002995 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00002996}
2997
Craig Topperf6e7e122012-03-27 07:21:54 +00002998static DecodeStatus DecodeTBLInstruction(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00002999 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003000 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003001
Jim Grosbachecaef492012-08-14 19:06:05 +00003002 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
3003 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
3004 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3005 Rn |= fieldFromInstruction(Insn, 7, 1) << 4;
3006 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
3007 Rm |= fieldFromInstruction(Insn, 5, 1) << 4;
3008 unsigned op = fieldFromInstruction(Insn, 6, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00003009
Owen Anderson03aadae2011-09-01 23:23:50 +00003010 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3011 return MCDisassembler::Fail;
Owen Andersoned253852011-08-11 18:24:51 +00003012 if (op) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003013 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3014 return MCDisassembler::Fail; // Writeback
Owen Andersoned253852011-08-11 18:24:51 +00003015 }
Owen Andersone0152a72011-08-09 20:55:18 +00003016
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003017 switch (Inst.getOpcode()) {
3018 case ARM::VTBL2:
3019 case ARM::VTBX2:
3020 if (!Check(S, DecodeDPairRegisterClass(Inst, Rn, Address, Decoder)))
3021 return MCDisassembler::Fail;
3022 break;
3023 default:
3024 if (!Check(S, DecodeDPRRegisterClass(Inst, Rn, Address, Decoder)))
3025 return MCDisassembler::Fail;
3026 }
Owen Andersone0152a72011-08-09 20:55:18 +00003027
Owen Anderson03aadae2011-09-01 23:23:50 +00003028 if (!Check(S, DecodeDPRRegisterClass(Inst, Rm, Address, Decoder)))
3029 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003030
Owen Andersona4043c42011-08-17 17:44:15 +00003031 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003032}
3033
Craig Topperf6e7e122012-03-27 07:21:54 +00003034static DecodeStatus DecodeThumbAddSpecialReg(MCInst &Inst, uint16_t Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00003035 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003036 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003037
Jim Grosbachecaef492012-08-14 19:06:05 +00003038 unsigned dst = fieldFromInstruction(Insn, 8, 3);
3039 unsigned imm = fieldFromInstruction(Insn, 0, 8);
Owen Andersone0152a72011-08-09 20:55:18 +00003040
Owen Anderson03aadae2011-09-01 23:23:50 +00003041 if (!Check(S, DecodetGPRRegisterClass(Inst, dst, Address, Decoder)))
3042 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003043
Owen Andersona01bcbf2011-08-26 18:09:22 +00003044 switch(Inst.getOpcode()) {
Owen Anderson5658b492011-08-26 19:39:26 +00003045 default:
James Molloydb4ce602011-09-01 18:02:14 +00003046 return MCDisassembler::Fail;
Owen Andersona01bcbf2011-08-26 18:09:22 +00003047 case ARM::tADR:
Owen Anderson240d20a2011-08-26 21:47:57 +00003048 break; // tADR does not explicitly represent the PC as an operand.
Owen Andersona01bcbf2011-08-26 18:09:22 +00003049 case ARM::tADDrSPi:
3050 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
3051 break;
Owen Andersona01bcbf2011-08-26 18:09:22 +00003052 }
Owen Andersone0152a72011-08-09 20:55:18 +00003053
3054 Inst.addOperand(MCOperand::CreateImm(imm));
Owen Andersona4043c42011-08-17 17:44:15 +00003055 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003056}
3057
Craig Topperf6e7e122012-03-27 07:21:54 +00003058static DecodeStatus DecodeThumbBROperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003059 uint64_t Address, const void *Decoder) {
Kevin Enderby40d4e472012-04-12 23:13:34 +00003060 if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<12>(Val<<1) + 4,
3061 true, 2, Inst, Decoder))
3062 Inst.addOperand(MCOperand::CreateImm(SignExtend32<12>(Val << 1)));
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 DecodeT2BROperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003067 uint64_t Address, const void *Decoder) {
Kevin Enderbycabbae62012-05-04 22:09:52 +00003068 if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<21>(Val) + 4,
Kevin Enderby40d4e472012-04-12 23:13:34 +00003069 true, 4, Inst, Decoder))
3070 Inst.addOperand(MCOperand::CreateImm(SignExtend32<21>(Val)));
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 DecodeThumbCmpBROperand(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003075 uint64_t Address, const void *Decoder) {
Gordon Keiser772cf462013-03-28 19:22:28 +00003076 if (!tryAddingSymbolicOperand(Address, Address + (Val<<1) + 4,
Kevin Enderby40d4e472012-04-12 23:13:34 +00003077 true, 2, Inst, Decoder))
Gordon Keiser772cf462013-03-28 19:22:28 +00003078 Inst.addOperand(MCOperand::CreateImm(Val << 1));
James Molloydb4ce602011-09-01 18:02:14 +00003079 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003080}
3081
Craig Topperf6e7e122012-03-27 07:21:54 +00003082static DecodeStatus DecodeThumbAddrModeRR(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003083 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003084 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003085
Jim Grosbachecaef492012-08-14 19:06:05 +00003086 unsigned Rn = fieldFromInstruction(Val, 0, 3);
3087 unsigned Rm = fieldFromInstruction(Val, 3, 3);
Owen Andersone0152a72011-08-09 20:55:18 +00003088
Owen Anderson03aadae2011-09-01 23:23:50 +00003089 if (!Check(S, DecodetGPRRegisterClass(Inst, Rn, Address, Decoder)))
3090 return MCDisassembler::Fail;
3091 if (!Check(S, DecodetGPRRegisterClass(Inst, Rm, Address, Decoder)))
3092 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003093
Owen Andersona4043c42011-08-17 17:44:15 +00003094 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003095}
3096
Craig Topperf6e7e122012-03-27 07:21:54 +00003097static DecodeStatus DecodeThumbAddrModeIS(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003098 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003099 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003100
Jim Grosbachecaef492012-08-14 19:06:05 +00003101 unsigned Rn = fieldFromInstruction(Val, 0, 3);
3102 unsigned imm = fieldFromInstruction(Val, 3, 5);
Owen Andersone0152a72011-08-09 20:55:18 +00003103
Owen Anderson03aadae2011-09-01 23:23:50 +00003104 if (!Check(S, DecodetGPRRegisterClass(Inst, Rn, Address, Decoder)))
3105 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003106 Inst.addOperand(MCOperand::CreateImm(imm));
3107
Owen Andersona4043c42011-08-17 17:44:15 +00003108 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003109}
3110
Craig Topperf6e7e122012-03-27 07:21:54 +00003111static DecodeStatus DecodeThumbAddrModePC(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003112 uint64_t Address, const void *Decoder) {
Kevin Enderby5dcda642011-10-04 22:44:48 +00003113 unsigned imm = Val << 2;
3114
3115 Inst.addOperand(MCOperand::CreateImm(imm));
3116 tryAddingPcLoadReferenceComment(Address, (Address & ~2u) + imm + 4, Decoder);
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 DecodeThumbAddrModeSP(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003122 uint64_t Address, const void *Decoder) {
3123 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
Owen Andersonb4981322011-08-22 17:56:58 +00003124 Inst.addOperand(MCOperand::CreateImm(Val));
Owen Andersone0152a72011-08-09 20:55:18 +00003125
James Molloydb4ce602011-09-01 18:02:14 +00003126 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003127}
3128
Craig Topperf6e7e122012-03-27 07:21:54 +00003129static DecodeStatus DecodeT2AddrModeSOReg(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003130 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003131 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003132
Jim Grosbachecaef492012-08-14 19:06:05 +00003133 unsigned Rn = fieldFromInstruction(Val, 6, 4);
3134 unsigned Rm = fieldFromInstruction(Val, 2, 4);
3135 unsigned imm = fieldFromInstruction(Val, 0, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00003136
Owen Anderson03aadae2011-09-01 23:23:50 +00003137 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3138 return MCDisassembler::Fail;
3139 if (!Check(S, DecoderGPRRegisterClass(Inst, Rm, Address, Decoder)))
3140 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003141 Inst.addOperand(MCOperand::CreateImm(imm));
3142
Owen Andersona4043c42011-08-17 17:44:15 +00003143 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003144}
3145
Craig Topperf6e7e122012-03-27 07:21:54 +00003146static DecodeStatus DecodeT2LoadShift(MCInst &Inst, unsigned Insn,
Owen Andersone0152a72011-08-09 20:55:18 +00003147 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003148 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003149
Owen Anderson924bcfc2011-08-23 17:51:38 +00003150 switch (Inst.getOpcode()) {
3151 case ARM::t2PLDs:
3152 case ARM::t2PLDWs:
3153 case ARM::t2PLIs:
3154 break;
3155 default: {
Jim Grosbachecaef492012-08-14 19:06:05 +00003156 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
Owen Anderson987a8782011-09-23 21:07:25 +00003157 if (!Check(S, DecoderGPRRegisterClass(Inst, Rt, Address, Decoder)))
Owen Anderson03aadae2011-09-01 23:23:50 +00003158 return MCDisassembler::Fail;
Owen Anderson924bcfc2011-08-23 17:51:38 +00003159 }
Owen Andersone0152a72011-08-09 20:55:18 +00003160 }
3161
Jim Grosbachecaef492012-08-14 19:06:05 +00003162 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00003163 if (Rn == 0xF) {
3164 switch (Inst.getOpcode()) {
3165 case ARM::t2LDRBs:
3166 Inst.setOpcode(ARM::t2LDRBpci);
3167 break;
3168 case ARM::t2LDRHs:
3169 Inst.setOpcode(ARM::t2LDRHpci);
3170 break;
3171 case ARM::t2LDRSHs:
3172 Inst.setOpcode(ARM::t2LDRSHpci);
3173 break;
3174 case ARM::t2LDRSBs:
3175 Inst.setOpcode(ARM::t2LDRSBpci);
3176 break;
3177 case ARM::t2PLDs:
3178 Inst.setOpcode(ARM::t2PLDi12);
3179 Inst.addOperand(MCOperand::CreateReg(ARM::PC));
3180 break;
3181 default:
James Molloydb4ce602011-09-01 18:02:14 +00003182 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003183 }
3184
Jim Grosbachecaef492012-08-14 19:06:05 +00003185 int imm = fieldFromInstruction(Insn, 0, 12);
3186 if (!fieldFromInstruction(Insn, 23, 1)) imm *= -1;
Owen Andersone0152a72011-08-09 20:55:18 +00003187 Inst.addOperand(MCOperand::CreateImm(imm));
3188
Owen Andersona4043c42011-08-17 17:44:15 +00003189 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003190 }
3191
Jim Grosbachecaef492012-08-14 19:06:05 +00003192 unsigned addrmode = fieldFromInstruction(Insn, 4, 2);
3193 addrmode |= fieldFromInstruction(Insn, 0, 4) << 2;
3194 addrmode |= fieldFromInstruction(Insn, 16, 4) << 6;
Owen Anderson03aadae2011-09-01 23:23:50 +00003195 if (!Check(S, DecodeT2AddrModeSOReg(Inst, addrmode, Address, Decoder)))
3196 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003197
Owen Andersona4043c42011-08-17 17:44:15 +00003198 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003199}
3200
Craig Topperf6e7e122012-03-27 07:21:54 +00003201static DecodeStatus DecodeT2Imm8S4(MCInst &Inst, unsigned Val,
Owen Anderson5d69f632011-08-10 17:36:48 +00003202 uint64_t Address, const void *Decoder) {
Jiangning Liu6a43bf72012-08-02 08:29:50 +00003203 if (Val == 0)
3204 Inst.addOperand(MCOperand::CreateImm(INT32_MIN));
3205 else {
3206 int imm = Val & 0xFF;
3207
3208 if (!(Val & 0x100)) imm *= -1;
Richard Smith228e6d42012-08-24 23:29:28 +00003209 Inst.addOperand(MCOperand::CreateImm(imm * 4));
Jiangning Liu6a43bf72012-08-02 08:29:50 +00003210 }
Owen Andersone0152a72011-08-09 20:55:18 +00003211
James Molloydb4ce602011-09-01 18:02:14 +00003212 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003213}
3214
Craig Topperf6e7e122012-03-27 07:21:54 +00003215static DecodeStatus DecodeT2AddrModeImm8s4(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003216 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003217 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003218
Jim Grosbachecaef492012-08-14 19:06:05 +00003219 unsigned Rn = fieldFromInstruction(Val, 9, 4);
3220 unsigned imm = fieldFromInstruction(Val, 0, 9);
Owen Andersone0152a72011-08-09 20:55:18 +00003221
Owen Anderson03aadae2011-09-01 23:23:50 +00003222 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3223 return MCDisassembler::Fail;
3224 if (!Check(S, DecodeT2Imm8S4(Inst, imm, Address, Decoder)))
3225 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003226
Owen Andersona4043c42011-08-17 17:44:15 +00003227 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003228}
3229
Craig Topperf6e7e122012-03-27 07:21:54 +00003230static DecodeStatus DecodeT2AddrModeImm0_1020s4(MCInst &Inst,unsigned Val,
Jim Grosbacha05627e2011-09-09 18:37:27 +00003231 uint64_t Address, const void *Decoder) {
3232 DecodeStatus S = MCDisassembler::Success;
3233
Jim Grosbachecaef492012-08-14 19:06:05 +00003234 unsigned Rn = fieldFromInstruction(Val, 8, 4);
3235 unsigned imm = fieldFromInstruction(Val, 0, 8);
Jim Grosbacha05627e2011-09-09 18:37:27 +00003236
3237 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
3238 return MCDisassembler::Fail;
3239
3240 Inst.addOperand(MCOperand::CreateImm(imm));
3241
3242 return S;
3243}
3244
Craig Topperf6e7e122012-03-27 07:21:54 +00003245static DecodeStatus DecodeT2Imm8(MCInst &Inst, unsigned Val,
Owen Anderson5d69f632011-08-10 17:36:48 +00003246 uint64_t Address, const void *Decoder) {
Owen Andersone0152a72011-08-09 20:55:18 +00003247 int imm = Val & 0xFF;
Owen Andersonfe823652011-09-16 21:08:33 +00003248 if (Val == 0)
3249 imm = INT32_MIN;
3250 else if (!(Val & 0x100))
3251 imm *= -1;
Owen Andersone0152a72011-08-09 20:55:18 +00003252 Inst.addOperand(MCOperand::CreateImm(imm));
3253
James Molloydb4ce602011-09-01 18:02:14 +00003254 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003255}
3256
3257
Craig Topperf6e7e122012-03-27 07:21:54 +00003258static DecodeStatus DecodeT2AddrModeImm8(MCInst &Inst, unsigned Val,
Owen Anderson5d69f632011-08-10 17:36:48 +00003259 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003260 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003261
Jim Grosbachecaef492012-08-14 19:06:05 +00003262 unsigned Rn = fieldFromInstruction(Val, 9, 4);
3263 unsigned imm = fieldFromInstruction(Val, 0, 9);
Owen Andersone0152a72011-08-09 20:55:18 +00003264
3265 // Some instructions always use an additive offset.
3266 switch (Inst.getOpcode()) {
3267 case ARM::t2LDRT:
3268 case ARM::t2LDRBT:
3269 case ARM::t2LDRHT:
3270 case ARM::t2LDRSBT:
3271 case ARM::t2LDRSHT:
Owen Andersonddfcec92011-09-19 18:07:10 +00003272 case ARM::t2STRT:
3273 case ARM::t2STRBT:
3274 case ARM::t2STRHT:
Owen Andersone0152a72011-08-09 20:55:18 +00003275 imm |= 0x100;
3276 break;
3277 default:
3278 break;
3279 }
3280
Owen Anderson03aadae2011-09-01 23:23:50 +00003281 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3282 return MCDisassembler::Fail;
3283 if (!Check(S, DecodeT2Imm8(Inst, imm, Address, Decoder)))
3284 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003285
Owen Andersona4043c42011-08-17 17:44:15 +00003286 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003287}
3288
Craig Topperf6e7e122012-03-27 07:21:54 +00003289static DecodeStatus DecodeT2LdStPre(MCInst &Inst, unsigned Insn,
Owen Andersona9ebf6f2011-09-12 18:56:30 +00003290 uint64_t Address, const void *Decoder) {
3291 DecodeStatus S = MCDisassembler::Success;
3292
Jim Grosbachecaef492012-08-14 19:06:05 +00003293 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
3294 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3295 unsigned addr = fieldFromInstruction(Insn, 0, 8);
3296 addr |= fieldFromInstruction(Insn, 9, 1) << 8;
Owen Andersona9ebf6f2011-09-12 18:56:30 +00003297 addr |= Rn << 9;
Jim Grosbachecaef492012-08-14 19:06:05 +00003298 unsigned load = fieldFromInstruction(Insn, 20, 1);
Owen Andersona9ebf6f2011-09-12 18:56:30 +00003299
3300 if (!load) {
3301 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3302 return MCDisassembler::Fail;
3303 }
3304
Joe Abbeyf686be42013-03-26 13:58:53 +00003305 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
Owen Andersona9ebf6f2011-09-12 18:56:30 +00003306 return MCDisassembler::Fail;
3307
3308 if (load) {
3309 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3310 return MCDisassembler::Fail;
3311 }
3312
3313 if (!Check(S, DecodeT2AddrModeImm8(Inst, addr, Address, Decoder)))
3314 return MCDisassembler::Fail;
3315
3316 return S;
3317}
Owen Andersone0152a72011-08-09 20:55:18 +00003318
Craig Topperf6e7e122012-03-27 07:21:54 +00003319static DecodeStatus DecodeT2AddrModeImm12(MCInst &Inst, unsigned Val,
Owen Anderson5d69f632011-08-10 17:36:48 +00003320 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003321 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003322
Jim Grosbachecaef492012-08-14 19:06:05 +00003323 unsigned Rn = fieldFromInstruction(Val, 13, 4);
3324 unsigned imm = fieldFromInstruction(Val, 0, 12);
Owen Andersone0152a72011-08-09 20:55:18 +00003325
Owen Anderson03aadae2011-09-01 23:23:50 +00003326 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3327 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003328 Inst.addOperand(MCOperand::CreateImm(imm));
3329
Owen Andersona4043c42011-08-17 17:44:15 +00003330 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003331}
3332
3333
Craig Topperf6e7e122012-03-27 07:21:54 +00003334static DecodeStatus DecodeThumbAddSPImm(MCInst &Inst, uint16_t Insn,
Owen Anderson5d69f632011-08-10 17:36:48 +00003335 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003336 unsigned imm = fieldFromInstruction(Insn, 0, 7);
Owen Andersone0152a72011-08-09 20:55:18 +00003337
3338 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
3339 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
3340 Inst.addOperand(MCOperand::CreateImm(imm));
3341
James Molloydb4ce602011-09-01 18:02:14 +00003342 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003343}
3344
Craig Topperf6e7e122012-03-27 07:21:54 +00003345static DecodeStatus DecodeThumbAddSPReg(MCInst &Inst, uint16_t Insn,
Owen Anderson5d69f632011-08-10 17:36:48 +00003346 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003347 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003348
Owen Andersone0152a72011-08-09 20:55:18 +00003349 if (Inst.getOpcode() == ARM::tADDrSP) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003350 unsigned Rdm = fieldFromInstruction(Insn, 0, 3);
3351 Rdm |= fieldFromInstruction(Insn, 7, 1) << 3;
Owen Andersone0152a72011-08-09 20:55:18 +00003352
Owen Anderson03aadae2011-09-01 23:23:50 +00003353 if (!Check(S, DecodeGPRRegisterClass(Inst, Rdm, Address, Decoder)))
3354 return MCDisassembler::Fail;
Jim Grosbach9d8f6f32012-04-27 23:51:33 +00003355 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
Owen Anderson03aadae2011-09-01 23:23:50 +00003356 if (!Check(S, DecodeGPRRegisterClass(Inst, Rdm, Address, Decoder)))
3357 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003358 } else if (Inst.getOpcode() == ARM::tADDspr) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003359 unsigned Rm = fieldFromInstruction(Insn, 3, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00003360
3361 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
3362 Inst.addOperand(MCOperand::CreateReg(ARM::SP));
Owen Anderson03aadae2011-09-01 23:23:50 +00003363 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
3364 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003365 }
3366
Owen Andersona4043c42011-08-17 17:44:15 +00003367 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003368}
3369
Craig Topperf6e7e122012-03-27 07:21:54 +00003370static DecodeStatus DecodeThumbCPS(MCInst &Inst, uint16_t Insn,
Owen Anderson5d69f632011-08-10 17:36:48 +00003371 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003372 unsigned imod = fieldFromInstruction(Insn, 4, 1) | 0x2;
3373 unsigned flags = fieldFromInstruction(Insn, 0, 3);
Owen Andersone0152a72011-08-09 20:55:18 +00003374
3375 Inst.addOperand(MCOperand::CreateImm(imod));
3376 Inst.addOperand(MCOperand::CreateImm(flags));
3377
James Molloydb4ce602011-09-01 18:02:14 +00003378 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003379}
3380
Craig Topperf6e7e122012-03-27 07:21:54 +00003381static DecodeStatus DecodePostIdxReg(MCInst &Inst, unsigned Insn,
Owen Anderson5d69f632011-08-10 17:36:48 +00003382 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003383 DecodeStatus S = MCDisassembler::Success;
Jim Grosbachecaef492012-08-14 19:06:05 +00003384 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
3385 unsigned add = fieldFromInstruction(Insn, 4, 1);
Owen Andersone0152a72011-08-09 20:55:18 +00003386
Silviu Barangad213f212012-03-22 13:24:43 +00003387 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
Owen Anderson03aadae2011-09-01 23:23:50 +00003388 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003389 Inst.addOperand(MCOperand::CreateImm(add));
3390
Owen Andersona4043c42011-08-17 17:44:15 +00003391 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003392}
3393
Craig Topperf6e7e122012-03-27 07:21:54 +00003394static DecodeStatus DecodeThumbBLXOffset(MCInst &Inst, unsigned Val,
Owen Anderson5d69f632011-08-10 17:36:48 +00003395 uint64_t Address, const void *Decoder) {
NAKAMURA Takumi70c1aa02012-05-22 21:47:02 +00003396 // Val is passed in as S:J1:J2:imm10H:imm10L:'0'
Kevin Enderby91422302012-05-03 22:41:56 +00003397 // Note only one trailing zero not two. Also the J1 and J2 values are from
3398 // the encoded instruction. So here change to I1 and I2 values via:
3399 // I1 = NOT(J1 EOR S);
3400 // I2 = NOT(J2 EOR S);
3401 // and build the imm32 with two trailing zeros as documented:
NAKAMURA Takumi70c1aa02012-05-22 21:47:02 +00003402 // imm32 = SignExtend(S:I1:I2:imm10H:imm10L:'00', 32);
Kevin Enderby91422302012-05-03 22:41:56 +00003403 unsigned S = (Val >> 23) & 1;
3404 unsigned J1 = (Val >> 22) & 1;
3405 unsigned J2 = (Val >> 21) & 1;
3406 unsigned I1 = !(J1 ^ S);
3407 unsigned I2 = !(J2 ^ S);
3408 unsigned tmp = (Val & ~0x600000) | (I1 << 22) | (I2 << 21);
3409 int imm32 = SignExtend32<25>(tmp << 1);
3410
Jim Grosbach79ebc512011-10-20 17:28:20 +00003411 if (!tryAddingSymbolicOperand(Address,
Kevin Enderby91422302012-05-03 22:41:56 +00003412 (Address & ~2u) + imm32 + 4,
Kevin Enderby5dcda642011-10-04 22:44:48 +00003413 true, 4, Inst, Decoder))
Kevin Enderby91422302012-05-03 22:41:56 +00003414 Inst.addOperand(MCOperand::CreateImm(imm32));
James Molloydb4ce602011-09-01 18:02:14 +00003415 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003416}
3417
Craig Topperf6e7e122012-03-27 07:21:54 +00003418static DecodeStatus DecodeCoprocessor(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003419 uint64_t Address, const void *Decoder) {
3420 if (Val == 0xA || Val == 0xB)
James Molloydb4ce602011-09-01 18:02:14 +00003421 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003422
3423 Inst.addOperand(MCOperand::CreateImm(Val));
James Molloydb4ce602011-09-01 18:02:14 +00003424 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003425}
3426
Owen Anderson03aadae2011-09-01 23:23:50 +00003427static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00003428DecodeThumbTableBranch(MCInst &Inst, unsigned Insn,
Jim Grosbach05541f42011-09-19 22:21:13 +00003429 uint64_t Address, const void *Decoder) {
3430 DecodeStatus S = MCDisassembler::Success;
3431
Jim Grosbachecaef492012-08-14 19:06:05 +00003432 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3433 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
Jim Grosbach05541f42011-09-19 22:21:13 +00003434
3435 if (Rn == ARM::SP) S = MCDisassembler::SoftFail;
3436 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3437 return MCDisassembler::Fail;
3438 if (!Check(S, DecoderGPRRegisterClass(Inst, Rm, Address, Decoder)))
3439 return MCDisassembler::Fail;
3440 return S;
3441}
3442
3443static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00003444DecodeThumb2BCCInstruction(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00003445 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003446 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003447
Jim Grosbachecaef492012-08-14 19:06:05 +00003448 unsigned pred = fieldFromInstruction(Insn, 22, 4);
Owen Andersone0152a72011-08-09 20:55:18 +00003449 if (pred == 0xE || pred == 0xF) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003450 unsigned opc = fieldFromInstruction(Insn, 4, 28);
Owen Andersone0152a72011-08-09 20:55:18 +00003451 switch (opc) {
3452 default:
James Molloydb4ce602011-09-01 18:02:14 +00003453 return MCDisassembler::Fail;
Owen Anderson4af0aa92011-08-31 22:00:41 +00003454 case 0xf3bf8f4:
Owen Andersone0152a72011-08-09 20:55:18 +00003455 Inst.setOpcode(ARM::t2DSB);
3456 break;
Owen Anderson4af0aa92011-08-31 22:00:41 +00003457 case 0xf3bf8f5:
Owen Andersone0152a72011-08-09 20:55:18 +00003458 Inst.setOpcode(ARM::t2DMB);
3459 break;
Owen Anderson4af0aa92011-08-31 22:00:41 +00003460 case 0xf3bf8f6:
Owen Andersone0152a72011-08-09 20:55:18 +00003461 Inst.setOpcode(ARM::t2ISB);
Owen Andersoncd5612d2011-09-07 17:55:19 +00003462 break;
Owen Andersone0152a72011-08-09 20:55:18 +00003463 }
3464
Jim Grosbachecaef492012-08-14 19:06:05 +00003465 unsigned imm = fieldFromInstruction(Insn, 0, 4);
Owen Andersone0089312011-08-09 23:25:42 +00003466 return DecodeMemBarrierOption(Inst, imm, Address, Decoder);
Owen Andersone0152a72011-08-09 20:55:18 +00003467 }
3468
Jim Grosbachecaef492012-08-14 19:06:05 +00003469 unsigned brtarget = fieldFromInstruction(Insn, 0, 11) << 1;
3470 brtarget |= fieldFromInstruction(Insn, 11, 1) << 19;
3471 brtarget |= fieldFromInstruction(Insn, 13, 1) << 18;
3472 brtarget |= fieldFromInstruction(Insn, 16, 6) << 12;
3473 brtarget |= fieldFromInstruction(Insn, 26, 1) << 20;
Owen Andersone0152a72011-08-09 20:55:18 +00003474
Owen Anderson03aadae2011-09-01 23:23:50 +00003475 if (!Check(S, DecodeT2BROperand(Inst, brtarget, Address, Decoder)))
3476 return MCDisassembler::Fail;
3477 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3478 return MCDisassembler::Fail;
Owen Andersone0152a72011-08-09 20:55:18 +00003479
Owen Andersona4043c42011-08-17 17:44:15 +00003480 return S;
Owen Andersone0152a72011-08-09 20:55:18 +00003481}
3482
3483// Decode a shifted immediate operand. These basically consist
3484// of an 8-bit value, and a 4-bit directive that specifies either
3485// a splat operation or a rotation.
Craig Topperf6e7e122012-03-27 07:21:54 +00003486static DecodeStatus DecodeT2SOImm(MCInst &Inst, unsigned Val,
Owen Andersone0152a72011-08-09 20:55:18 +00003487 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003488 unsigned ctrl = fieldFromInstruction(Val, 10, 2);
Owen Andersone0152a72011-08-09 20:55:18 +00003489 if (ctrl == 0) {
Jim Grosbachecaef492012-08-14 19:06:05 +00003490 unsigned byte = fieldFromInstruction(Val, 8, 2);
3491 unsigned imm = fieldFromInstruction(Val, 0, 8);
Owen Andersone0152a72011-08-09 20:55:18 +00003492 switch (byte) {
3493 case 0:
3494 Inst.addOperand(MCOperand::CreateImm(imm));
3495 break;
3496 case 1:
3497 Inst.addOperand(MCOperand::CreateImm((imm << 16) | imm));
3498 break;
3499 case 2:
3500 Inst.addOperand(MCOperand::CreateImm((imm << 24) | (imm << 8)));
3501 break;
3502 case 3:
3503 Inst.addOperand(MCOperand::CreateImm((imm << 24) | (imm << 16) |
3504 (imm << 8) | imm));
3505 break;
3506 }
3507 } else {
Jim Grosbachecaef492012-08-14 19:06:05 +00003508 unsigned unrot = fieldFromInstruction(Val, 0, 7) | 0x80;
3509 unsigned rot = fieldFromInstruction(Val, 7, 5);
Owen Andersone0152a72011-08-09 20:55:18 +00003510 unsigned imm = (unrot >> rot) | (unrot << ((32-rot)&31));
3511 Inst.addOperand(MCOperand::CreateImm(imm));
3512 }
3513
James Molloydb4ce602011-09-01 18:02:14 +00003514 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003515}
3516
Owen Anderson03aadae2011-09-01 23:23:50 +00003517static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00003518DecodeThumbBCCTargetOperand(MCInst &Inst, unsigned Val,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00003519 uint64_t Address, const void *Decoder){
Richard Bartonf1ef87d2012-06-06 09:12:53 +00003520 if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<9>(Val<<1) + 4,
Kevin Enderby40d4e472012-04-12 23:13:34 +00003521 true, 2, Inst, Decoder))
Richard Bartonf1ef87d2012-06-06 09:12:53 +00003522 Inst.addOperand(MCOperand::CreateImm(SignExtend32<9>(Val << 1)));
James Molloydb4ce602011-09-01 18:02:14 +00003523 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003524}
3525
Craig Topperf6e7e122012-03-27 07:21:54 +00003526static DecodeStatus DecodeThumbBLTargetOperand(MCInst &Inst, unsigned Val,
Owen Anderson5d69f632011-08-10 17:36:48 +00003527 uint64_t Address, const void *Decoder){
Kevin Enderby91422302012-05-03 22:41:56 +00003528 // Val is passed in as S:J1:J2:imm10:imm11
3529 // Note no trailing zero after imm11. Also the J1 and J2 values are from
3530 // the encoded instruction. So here change to I1 and I2 values via:
3531 // I1 = NOT(J1 EOR S);
3532 // I2 = NOT(J2 EOR S);
3533 // and build the imm32 with one trailing zero as documented:
NAKAMURA Takumi70c1aa02012-05-22 21:47:02 +00003534 // imm32 = SignExtend(S:I1:I2:imm10:imm11:'0', 32);
Kevin Enderby91422302012-05-03 22:41:56 +00003535 unsigned S = (Val >> 23) & 1;
3536 unsigned J1 = (Val >> 22) & 1;
3537 unsigned J2 = (Val >> 21) & 1;
3538 unsigned I1 = !(J1 ^ S);
3539 unsigned I2 = !(J2 ^ S);
3540 unsigned tmp = (Val & ~0x600000) | (I1 << 22) | (I2 << 21);
3541 int imm32 = SignExtend32<25>(tmp << 1);
3542
3543 if (!tryAddingSymbolicOperand(Address, Address + imm32 + 4,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00003544 true, 4, Inst, Decoder))
Kevin Enderby91422302012-05-03 22:41:56 +00003545 Inst.addOperand(MCOperand::CreateImm(imm32));
James Molloydb4ce602011-09-01 18:02:14 +00003546 return MCDisassembler::Success;
Owen Andersone0152a72011-08-09 20:55:18 +00003547}
3548
Craig Topperf6e7e122012-03-27 07:21:54 +00003549static DecodeStatus DecodeMemBarrierOption(MCInst &Inst, unsigned Val,
Owen Andersone0089312011-08-09 23:25:42 +00003550 uint64_t Address, const void *Decoder) {
Jiangning Liu288e1af2012-08-02 08:21:27 +00003551 if (Val & ~0xf)
James Molloydb4ce602011-09-01 18:02:14 +00003552 return MCDisassembler::Fail;
Owen Andersone0089312011-08-09 23:25:42 +00003553
3554 Inst.addOperand(MCOperand::CreateImm(Val));
James Molloydb4ce602011-09-01 18:02:14 +00003555 return MCDisassembler::Success;
Owen Andersone0089312011-08-09 23:25:42 +00003556}
3557
Amaury de la Vieuville43cb13a2013-06-10 14:17:08 +00003558static DecodeStatus DecodeInstSyncBarrierOption(MCInst &Inst, unsigned Val,
3559 uint64_t Address, const void *Decoder) {
3560 if (Val & ~0xf)
3561 return MCDisassembler::Fail;
3562
3563 Inst.addOperand(MCOperand::CreateImm(Val));
3564 return MCDisassembler::Success;
3565}
3566
Craig Topperf6e7e122012-03-27 07:21:54 +00003567static DecodeStatus DecodeMSRMask(MCInst &Inst, unsigned Val,
Owen Anderson60663402011-08-11 20:21:46 +00003568 uint64_t Address, const void *Decoder) {
James Molloydb4ce602011-09-01 18:02:14 +00003569 if (!Val) return MCDisassembler::Fail;
Owen Anderson60663402011-08-11 20:21:46 +00003570 Inst.addOperand(MCOperand::CreateImm(Val));
James Molloydb4ce602011-09-01 18:02:14 +00003571 return MCDisassembler::Success;
Owen Anderson60663402011-08-11 20:21:46 +00003572}
Owen Andersonb685c9f2011-08-11 21:34:58 +00003573
Craig Topperf6e7e122012-03-27 07:21:54 +00003574static DecodeStatus DecodeDoubleRegLoad(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00003575 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003576 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003577
Jim Grosbachecaef492012-08-14 19:06:05 +00003578 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
3579 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3580 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Andersonc5798a3a52011-08-12 17:58:32 +00003581
James Molloydb4ce602011-09-01 18:02:14 +00003582 if ((Rt & 1) || Rt == 0xE || Rn == 0xF) return MCDisassembler::Fail;
Owen Andersonc5798a3a52011-08-12 17:58:32 +00003583
Owen Anderson03aadae2011-09-01 23:23:50 +00003584 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
3585 return MCDisassembler::Fail;
3586 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt+1, Address, Decoder)))
3587 return MCDisassembler::Fail;
3588 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3589 return MCDisassembler::Fail;
3590 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3591 return MCDisassembler::Fail;
Owen Andersonc5798a3a52011-08-12 17:58:32 +00003592
Owen Andersona4043c42011-08-17 17:44:15 +00003593 return S;
Owen Andersonc5798a3a52011-08-12 17:58:32 +00003594}
3595
3596
Craig Topperf6e7e122012-03-27 07:21:54 +00003597static DecodeStatus DecodeDoubleRegStore(MCInst &Inst, unsigned Insn,
Jim Grosbachd14b70d2011-08-17 21:58:18 +00003598 uint64_t Address, const void *Decoder){
Owen Anderson03aadae2011-09-01 23:23:50 +00003599 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003600
Jim Grosbachecaef492012-08-14 19:06:05 +00003601 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
3602 unsigned Rt = fieldFromInstruction(Insn, 0, 4);
3603 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3604 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Andersonb685c9f2011-08-11 21:34:58 +00003605
Tim Northover27ff5042013-04-19 15:44:32 +00003606 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
Owen Anderson03aadae2011-09-01 23:23:50 +00003607 return MCDisassembler::Fail;
Owen Andersonb685c9f2011-08-11 21:34:58 +00003608
James Molloydb4ce602011-09-01 18:02:14 +00003609 if ((Rt & 1) || Rt == 0xE || Rn == 0xF) return MCDisassembler::Fail;
3610 if (Rd == Rn || Rd == Rt || Rd == Rt+1) return MCDisassembler::Fail;
Owen Andersonb685c9f2011-08-11 21:34:58 +00003611
Owen Anderson03aadae2011-09-01 23:23:50 +00003612 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
3613 return MCDisassembler::Fail;
3614 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt+1, Address, Decoder)))
3615 return MCDisassembler::Fail;
3616 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3617 return MCDisassembler::Fail;
3618 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3619 return MCDisassembler::Fail;
Owen Andersonb685c9f2011-08-11 21:34:58 +00003620
Owen Andersona4043c42011-08-17 17:44:15 +00003621 return S;
Owen Andersonb685c9f2011-08-11 21:34:58 +00003622}
3623
Craig Topperf6e7e122012-03-27 07:21:54 +00003624static DecodeStatus DecodeLDRPreImm(MCInst &Inst, unsigned Insn,
Owen Anderson16d33f32011-08-26 20:43:14 +00003625 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003626 DecodeStatus S = MCDisassembler::Success;
Owen Anderson16d33f32011-08-26 20:43:14 +00003627
Jim Grosbachecaef492012-08-14 19:06:05 +00003628 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3629 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
3630 unsigned imm = fieldFromInstruction(Insn, 0, 12);
3631 imm |= fieldFromInstruction(Insn, 16, 4) << 13;
3632 imm |= fieldFromInstruction(Insn, 23, 1) << 12;
3633 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Anderson16d33f32011-08-26 20:43:14 +00003634
James Molloydb4ce602011-09-01 18:02:14 +00003635 if (Rn == 0xF || Rn == Rt) S = MCDisassembler::SoftFail;
Owen Anderson16d33f32011-08-26 20:43:14 +00003636
Owen Anderson03aadae2011-09-01 23:23:50 +00003637 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
3638 return MCDisassembler::Fail;
3639 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3640 return MCDisassembler::Fail;
3641 if (!Check(S, DecodeAddrModeImm12Operand(Inst, imm, Address, Decoder)))
3642 return MCDisassembler::Fail;
3643 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3644 return MCDisassembler::Fail;
Owen Anderson16d33f32011-08-26 20:43:14 +00003645
3646 return S;
3647}
3648
Craig Topperf6e7e122012-03-27 07:21:54 +00003649static DecodeStatus DecodeLDRPreReg(MCInst &Inst, unsigned Insn,
Owen Anderson16d33f32011-08-26 20:43:14 +00003650 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003651 DecodeStatus S = MCDisassembler::Success;
Owen Anderson16d33f32011-08-26 20:43:14 +00003652
Jim Grosbachecaef492012-08-14 19:06:05 +00003653 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3654 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
3655 unsigned imm = fieldFromInstruction(Insn, 0, 12);
3656 imm |= fieldFromInstruction(Insn, 16, 4) << 13;
3657 imm |= fieldFromInstruction(Insn, 23, 1) << 12;
3658 unsigned pred = fieldFromInstruction(Insn, 28, 4);
3659 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
Owen Anderson16d33f32011-08-26 20:43:14 +00003660
James Molloydb4ce602011-09-01 18:02:14 +00003661 if (Rn == 0xF || Rn == Rt) S = MCDisassembler::SoftFail;
3662 if (Rm == 0xF) S = MCDisassembler::SoftFail;
Owen Anderson16d33f32011-08-26 20:43:14 +00003663
Owen Anderson03aadae2011-09-01 23:23:50 +00003664 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
3665 return MCDisassembler::Fail;
3666 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3667 return MCDisassembler::Fail;
3668 if (!Check(S, DecodeSORegMemOperand(Inst, imm, Address, Decoder)))
3669 return MCDisassembler::Fail;
3670 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3671 return MCDisassembler::Fail;
Owen Anderson16d33f32011-08-26 20:43:14 +00003672
3673 return S;
3674}
3675
3676
Craig Topperf6e7e122012-03-27 07:21:54 +00003677static DecodeStatus DecodeSTRPreImm(MCInst &Inst, unsigned Insn,
Owen Anderson3987a612011-08-12 18:12:39 +00003678 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003679 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003680
Jim Grosbachecaef492012-08-14 19:06:05 +00003681 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3682 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
3683 unsigned imm = fieldFromInstruction(Insn, 0, 12);
3684 imm |= fieldFromInstruction(Insn, 16, 4) << 13;
3685 imm |= fieldFromInstruction(Insn, 23, 1) << 12;
3686 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Andersonb685c9f2011-08-11 21:34:58 +00003687
James Molloydb4ce602011-09-01 18:02:14 +00003688 if (Rn == 0xF || Rn == Rt) S = MCDisassembler::SoftFail;
Owen Anderson3987a612011-08-12 18:12:39 +00003689
Owen Anderson03aadae2011-09-01 23:23:50 +00003690 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3691 return MCDisassembler::Fail;
3692 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
3693 return MCDisassembler::Fail;
3694 if (!Check(S, DecodeAddrModeImm12Operand(Inst, imm, Address, Decoder)))
3695 return MCDisassembler::Fail;
3696 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3697 return MCDisassembler::Fail;
Owen Anderson3987a612011-08-12 18:12:39 +00003698
Owen Andersona4043c42011-08-17 17:44:15 +00003699 return S;
Owen Anderson3987a612011-08-12 18:12:39 +00003700}
3701
Craig Topperf6e7e122012-03-27 07:21:54 +00003702static DecodeStatus DecodeSTRPreReg(MCInst &Inst, unsigned Insn,
Owen Anderson3987a612011-08-12 18:12:39 +00003703 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003704 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003705
Jim Grosbachecaef492012-08-14 19:06:05 +00003706 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3707 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
3708 unsigned imm = fieldFromInstruction(Insn, 0, 12);
3709 imm |= fieldFromInstruction(Insn, 16, 4) << 13;
3710 imm |= fieldFromInstruction(Insn, 23, 1) << 12;
3711 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Anderson3987a612011-08-12 18:12:39 +00003712
James Molloydb4ce602011-09-01 18:02:14 +00003713 if (Rn == 0xF || Rn == Rt) S = MCDisassembler::SoftFail;
Owen Anderson3987a612011-08-12 18:12:39 +00003714
Owen Anderson03aadae2011-09-01 23:23:50 +00003715 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3716 return MCDisassembler::Fail;
3717 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
3718 return MCDisassembler::Fail;
3719 if (!Check(S, DecodeSORegMemOperand(Inst, imm, Address, Decoder)))
3720 return MCDisassembler::Fail;
3721 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
3722 return MCDisassembler::Fail;
Owen Anderson3987a612011-08-12 18:12:39 +00003723
Owen Andersona4043c42011-08-17 17:44:15 +00003724 return S;
Owen Anderson3987a612011-08-12 18:12:39 +00003725}
Owen Andersonb9d82f42011-08-15 18:44:44 +00003726
Craig Topperf6e7e122012-03-27 07:21:54 +00003727static DecodeStatus DecodeVLD1LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00003728 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003729 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003730
Jim Grosbachecaef492012-08-14 19:06:05 +00003731 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3732 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
3733 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
3734 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
3735 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003736
3737 unsigned align = 0;
3738 unsigned index = 0;
3739 switch (size) {
3740 default:
James Molloydb4ce602011-09-01 18:02:14 +00003741 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003742 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00003743 if (fieldFromInstruction(Insn, 4, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003744 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003745 index = fieldFromInstruction(Insn, 5, 3);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003746 break;
3747 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00003748 if (fieldFromInstruction(Insn, 5, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003749 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003750 index = fieldFromInstruction(Insn, 6, 2);
3751 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003752 align = 2;
3753 break;
3754 case 2:
Jim Grosbachecaef492012-08-14 19:06:05 +00003755 if (fieldFromInstruction(Insn, 6, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003756 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003757 index = fieldFromInstruction(Insn, 7, 1);
Tim Northoverfb3cdd82012-09-06 15:17:49 +00003758
3759 switch (fieldFromInstruction(Insn, 4, 2)) {
3760 case 0 :
3761 align = 0; break;
3762 case 3:
3763 align = 4; break;
3764 default:
3765 return MCDisassembler::Fail;
3766 }
3767 break;
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 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00003773 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3774 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003775 }
Owen Anderson03aadae2011-09-01 23:23:50 +00003776 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3777 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003778 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00003779 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00003780 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003781 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
3782 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00003783 } else
Owen Anderson721c3702011-08-22 18:42:13 +00003784 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00003785 }
3786
Owen Anderson03aadae2011-09-01 23:23:50 +00003787 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3788 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003789 Inst.addOperand(MCOperand::CreateImm(index));
3790
Owen Andersona4043c42011-08-17 17:44:15 +00003791 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003792}
3793
Craig Topperf6e7e122012-03-27 07:21:54 +00003794static DecodeStatus DecodeVST1LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00003795 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003796 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003797
Jim Grosbachecaef492012-08-14 19:06:05 +00003798 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3799 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
3800 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
3801 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
3802 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003803
3804 unsigned align = 0;
3805 unsigned index = 0;
3806 switch (size) {
3807 default:
James Molloydb4ce602011-09-01 18:02:14 +00003808 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003809 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00003810 if (fieldFromInstruction(Insn, 4, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003811 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003812 index = fieldFromInstruction(Insn, 5, 3);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003813 break;
3814 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00003815 if (fieldFromInstruction(Insn, 5, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003816 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003817 index = fieldFromInstruction(Insn, 6, 2);
3818 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003819 align = 2;
3820 break;
3821 case 2:
Jim Grosbachecaef492012-08-14 19:06:05 +00003822 if (fieldFromInstruction(Insn, 6, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003823 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003824 index = fieldFromInstruction(Insn, 7, 1);
Tim Northoverfb3cdd82012-09-06 15:17:49 +00003825
3826 switch (fieldFromInstruction(Insn, 4, 2)) {
3827 case 0:
3828 align = 0; break;
3829 case 3:
3830 align = 4; break;
3831 default:
3832 return MCDisassembler::Fail;
3833 }
3834 break;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003835 }
3836
3837 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00003838 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3839 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003840 }
Owen Anderson03aadae2011-09-01 23:23:50 +00003841 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3842 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003843 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00003844 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00003845 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003846 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
3847 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00003848 } else
Owen Anderson721c3702011-08-22 18:42:13 +00003849 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00003850 }
3851
Owen Anderson03aadae2011-09-01 23:23:50 +00003852 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3853 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003854 Inst.addOperand(MCOperand::CreateImm(index));
3855
Owen Andersona4043c42011-08-17 17:44:15 +00003856 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003857}
3858
3859
Craig Topperf6e7e122012-03-27 07:21:54 +00003860static DecodeStatus DecodeVLD2LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00003861 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003862 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003863
Jim Grosbachecaef492012-08-14 19:06:05 +00003864 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3865 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
3866 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
3867 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
3868 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003869
3870 unsigned align = 0;
3871 unsigned index = 0;
3872 unsigned inc = 1;
3873 switch (size) {
3874 default:
James Molloydb4ce602011-09-01 18:02:14 +00003875 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003876 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00003877 index = fieldFromInstruction(Insn, 5, 3);
3878 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003879 align = 2;
3880 break;
3881 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00003882 index = fieldFromInstruction(Insn, 6, 2);
3883 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003884 align = 4;
Jim Grosbachecaef492012-08-14 19:06:05 +00003885 if (fieldFromInstruction(Insn, 5, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003886 inc = 2;
3887 break;
3888 case 2:
Jim Grosbachecaef492012-08-14 19:06:05 +00003889 if (fieldFromInstruction(Insn, 5, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003890 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003891 index = fieldFromInstruction(Insn, 7, 1);
3892 if (fieldFromInstruction(Insn, 4, 1) != 0)
Owen Andersonb9d82f42011-08-15 18:44:44 +00003893 align = 8;
Jim Grosbachecaef492012-08-14 19:06:05 +00003894 if (fieldFromInstruction(Insn, 6, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003895 inc = 2;
3896 break;
3897 }
3898
Owen Anderson03aadae2011-09-01 23:23:50 +00003899 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3900 return MCDisassembler::Fail;
3901 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
3902 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003903 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00003904 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3905 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003906 }
Owen Anderson03aadae2011-09-01 23:23:50 +00003907 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3908 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003909 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00003910 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00003911 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003912 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
3913 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00003914 } else
Owen Anderson721c3702011-08-22 18:42:13 +00003915 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00003916 }
3917
Owen Anderson03aadae2011-09-01 23:23:50 +00003918 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3919 return MCDisassembler::Fail;
3920 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
3921 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003922 Inst.addOperand(MCOperand::CreateImm(index));
3923
Owen Andersona4043c42011-08-17 17:44:15 +00003924 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003925}
3926
Craig Topperf6e7e122012-03-27 07:21:54 +00003927static DecodeStatus DecodeVST2LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00003928 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003929 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003930
Jim Grosbachecaef492012-08-14 19:06:05 +00003931 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3932 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
3933 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
3934 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
3935 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00003936
3937 unsigned align = 0;
3938 unsigned index = 0;
3939 unsigned inc = 1;
3940 switch (size) {
3941 default:
James Molloydb4ce602011-09-01 18:02:14 +00003942 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003943 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00003944 index = fieldFromInstruction(Insn, 5, 3);
3945 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003946 align = 2;
3947 break;
3948 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00003949 index = fieldFromInstruction(Insn, 6, 2);
3950 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003951 align = 4;
Jim Grosbachecaef492012-08-14 19:06:05 +00003952 if (fieldFromInstruction(Insn, 5, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003953 inc = 2;
3954 break;
3955 case 2:
Jim Grosbachecaef492012-08-14 19:06:05 +00003956 if (fieldFromInstruction(Insn, 5, 1))
James Molloydb4ce602011-09-01 18:02:14 +00003957 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00003958 index = fieldFromInstruction(Insn, 7, 1);
3959 if (fieldFromInstruction(Insn, 4, 1) != 0)
Owen Andersonb9d82f42011-08-15 18:44:44 +00003960 align = 8;
Jim Grosbachecaef492012-08-14 19:06:05 +00003961 if (fieldFromInstruction(Insn, 6, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00003962 inc = 2;
3963 break;
3964 }
3965
3966 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00003967 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3968 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003969 }
Owen Anderson03aadae2011-09-01 23:23:50 +00003970 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
3971 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003972 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00003973 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00003974 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003975 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
3976 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00003977 } else
Owen Anderson721c3702011-08-22 18:42:13 +00003978 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00003979 }
3980
Owen Anderson03aadae2011-09-01 23:23:50 +00003981 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
3982 return MCDisassembler::Fail;
3983 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
3984 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003985 Inst.addOperand(MCOperand::CreateImm(index));
3986
Owen Andersona4043c42011-08-17 17:44:15 +00003987 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00003988}
3989
3990
Craig Topperf6e7e122012-03-27 07:21:54 +00003991static DecodeStatus DecodeVLD3LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00003992 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00003993 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00003994
Jim Grosbachecaef492012-08-14 19:06:05 +00003995 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
3996 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
3997 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
3998 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
3999 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004000
4001 unsigned align = 0;
4002 unsigned index = 0;
4003 unsigned inc = 1;
4004 switch (size) {
4005 default:
James Molloydb4ce602011-09-01 18:02:14 +00004006 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004007 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00004008 if (fieldFromInstruction(Insn, 4, 1))
James Molloydb4ce602011-09-01 18:02:14 +00004009 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00004010 index = fieldFromInstruction(Insn, 5, 3);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004011 break;
4012 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00004013 if (fieldFromInstruction(Insn, 4, 1))
James Molloydb4ce602011-09-01 18:02:14 +00004014 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00004015 index = fieldFromInstruction(Insn, 6, 2);
4016 if (fieldFromInstruction(Insn, 5, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004017 inc = 2;
4018 break;
4019 case 2:
Jim Grosbachecaef492012-08-14 19:06:05 +00004020 if (fieldFromInstruction(Insn, 4, 2))
James Molloydb4ce602011-09-01 18:02:14 +00004021 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00004022 index = fieldFromInstruction(Insn, 7, 1);
4023 if (fieldFromInstruction(Insn, 6, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004024 inc = 2;
4025 break;
4026 }
4027
Owen Anderson03aadae2011-09-01 23:23:50 +00004028 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
4029 return MCDisassembler::Fail;
4030 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
4031 return MCDisassembler::Fail;
4032 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
4033 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004034
4035 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00004036 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4037 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004038 }
Owen Anderson03aadae2011-09-01 23:23:50 +00004039 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4040 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004041 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson2fa06a72011-08-30 22:58:27 +00004042 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00004043 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004044 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
4045 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00004046 } else
Owen Anderson721c3702011-08-22 18:42:13 +00004047 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00004048 }
4049
Owen Anderson03aadae2011-09-01 23:23:50 +00004050 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
4051 return MCDisassembler::Fail;
4052 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
4053 return MCDisassembler::Fail;
4054 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
4055 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004056 Inst.addOperand(MCOperand::CreateImm(index));
4057
Owen Andersona4043c42011-08-17 17:44:15 +00004058 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004059}
4060
Craig Topperf6e7e122012-03-27 07:21:54 +00004061static DecodeStatus DecodeVST3LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00004062 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004063 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00004064
Jim Grosbachecaef492012-08-14 19:06:05 +00004065 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
4066 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
4067 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
4068 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
4069 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004070
4071 unsigned align = 0;
4072 unsigned index = 0;
4073 unsigned inc = 1;
4074 switch (size) {
4075 default:
James Molloydb4ce602011-09-01 18:02:14 +00004076 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004077 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00004078 if (fieldFromInstruction(Insn, 4, 1))
James Molloydb4ce602011-09-01 18:02:14 +00004079 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00004080 index = fieldFromInstruction(Insn, 5, 3);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004081 break;
4082 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00004083 if (fieldFromInstruction(Insn, 4, 1))
James Molloydb4ce602011-09-01 18:02:14 +00004084 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00004085 index = fieldFromInstruction(Insn, 6, 2);
4086 if (fieldFromInstruction(Insn, 5, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004087 inc = 2;
4088 break;
4089 case 2:
Jim Grosbachecaef492012-08-14 19:06:05 +00004090 if (fieldFromInstruction(Insn, 4, 2))
James Molloydb4ce602011-09-01 18:02:14 +00004091 return MCDisassembler::Fail; // UNDEFINED
Jim Grosbachecaef492012-08-14 19:06:05 +00004092 index = fieldFromInstruction(Insn, 7, 1);
4093 if (fieldFromInstruction(Insn, 6, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004094 inc = 2;
4095 break;
4096 }
4097
4098 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00004099 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4100 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004101 }
Owen Anderson03aadae2011-09-01 23:23:50 +00004102 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4103 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004104 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00004105 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00004106 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004107 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
4108 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00004109 } else
Owen Anderson721c3702011-08-22 18:42:13 +00004110 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00004111 }
4112
Owen Anderson03aadae2011-09-01 23:23:50 +00004113 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
4114 return MCDisassembler::Fail;
4115 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
4116 return MCDisassembler::Fail;
4117 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
4118 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004119 Inst.addOperand(MCOperand::CreateImm(index));
4120
Owen Andersona4043c42011-08-17 17:44:15 +00004121 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004122}
4123
4124
Craig Topperf6e7e122012-03-27 07:21:54 +00004125static DecodeStatus DecodeVLD4LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00004126 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004127 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00004128
Jim Grosbachecaef492012-08-14 19:06:05 +00004129 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
4130 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
4131 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
4132 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
4133 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004134
4135 unsigned align = 0;
4136 unsigned index = 0;
4137 unsigned inc = 1;
4138 switch (size) {
4139 default:
James Molloydb4ce602011-09-01 18:02:14 +00004140 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004141 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00004142 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004143 align = 4;
Jim Grosbachecaef492012-08-14 19:06:05 +00004144 index = fieldFromInstruction(Insn, 5, 3);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004145 break;
4146 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00004147 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004148 align = 8;
Jim Grosbachecaef492012-08-14 19:06:05 +00004149 index = fieldFromInstruction(Insn, 6, 2);
4150 if (fieldFromInstruction(Insn, 5, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004151 inc = 2;
4152 break;
4153 case 2:
Tim Northoverfb3cdd82012-09-06 15:17:49 +00004154 switch (fieldFromInstruction(Insn, 4, 2)) {
4155 case 0:
4156 align = 0; break;
4157 case 3:
4158 return MCDisassembler::Fail;
4159 default:
4160 align = 4 << fieldFromInstruction(Insn, 4, 2); break;
4161 }
4162
Jim Grosbachecaef492012-08-14 19:06:05 +00004163 index = fieldFromInstruction(Insn, 7, 1);
4164 if (fieldFromInstruction(Insn, 6, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004165 inc = 2;
4166 break;
4167 }
4168
Owen Anderson03aadae2011-09-01 23:23:50 +00004169 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
4170 return MCDisassembler::Fail;
4171 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
4172 return MCDisassembler::Fail;
4173 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
4174 return MCDisassembler::Fail;
4175 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+3*inc, Address, Decoder)))
4176 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004177
4178 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00004179 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4180 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004181 }
Owen Anderson03aadae2011-09-01 23:23:50 +00004182 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4183 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004184 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00004185 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00004186 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004187 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
4188 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00004189 } else
Owen Anderson721c3702011-08-22 18:42:13 +00004190 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00004191 }
4192
Owen Anderson03aadae2011-09-01 23:23:50 +00004193 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
4194 return MCDisassembler::Fail;
4195 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
4196 return MCDisassembler::Fail;
4197 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
4198 return MCDisassembler::Fail;
4199 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+3*inc, Address, Decoder)))
4200 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004201 Inst.addOperand(MCOperand::CreateImm(index));
4202
Owen Andersona4043c42011-08-17 17:44:15 +00004203 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004204}
4205
Craig Topperf6e7e122012-03-27 07:21:54 +00004206static DecodeStatus DecodeVST4LN(MCInst &Inst, unsigned Insn,
Owen Andersonb9d82f42011-08-15 18:44:44 +00004207 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004208 DecodeStatus S = MCDisassembler::Success;
Owen Andersona4043c42011-08-17 17:44:15 +00004209
Jim Grosbachecaef492012-08-14 19:06:05 +00004210 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
4211 unsigned Rm = fieldFromInstruction(Insn, 0, 4);
4212 unsigned Rd = fieldFromInstruction(Insn, 12, 4);
4213 Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
4214 unsigned size = fieldFromInstruction(Insn, 10, 2);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004215
4216 unsigned align = 0;
4217 unsigned index = 0;
4218 unsigned inc = 1;
4219 switch (size) {
4220 default:
James Molloydb4ce602011-09-01 18:02:14 +00004221 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004222 case 0:
Jim Grosbachecaef492012-08-14 19:06:05 +00004223 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004224 align = 4;
Jim Grosbachecaef492012-08-14 19:06:05 +00004225 index = fieldFromInstruction(Insn, 5, 3);
Owen Andersonb9d82f42011-08-15 18:44:44 +00004226 break;
4227 case 1:
Jim Grosbachecaef492012-08-14 19:06:05 +00004228 if (fieldFromInstruction(Insn, 4, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004229 align = 8;
Jim Grosbachecaef492012-08-14 19:06:05 +00004230 index = fieldFromInstruction(Insn, 6, 2);
4231 if (fieldFromInstruction(Insn, 5, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004232 inc = 2;
4233 break;
4234 case 2:
Tim Northoverfb3cdd82012-09-06 15:17:49 +00004235 switch (fieldFromInstruction(Insn, 4, 2)) {
4236 case 0:
4237 align = 0; break;
4238 case 3:
4239 return MCDisassembler::Fail;
4240 default:
4241 align = 4 << fieldFromInstruction(Insn, 4, 2); break;
4242 }
4243
Jim Grosbachecaef492012-08-14 19:06:05 +00004244 index = fieldFromInstruction(Insn, 7, 1);
4245 if (fieldFromInstruction(Insn, 6, 1))
Owen Andersonb9d82f42011-08-15 18:44:44 +00004246 inc = 2;
4247 break;
4248 }
4249
4250 if (Rm != 0xF) { // Writeback
Owen Anderson03aadae2011-09-01 23:23:50 +00004251 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4252 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004253 }
Owen Anderson03aadae2011-09-01 23:23:50 +00004254 if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
4255 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004256 Inst.addOperand(MCOperand::CreateImm(align));
Owen Anderson721c3702011-08-22 18:42:13 +00004257 if (Rm != 0xF) {
James Molloydb4ce602011-09-01 18:02:14 +00004258 if (Rm != 0xD) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004259 if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
4260 return MCDisassembler::Fail;
James Molloydb4ce602011-09-01 18:02:14 +00004261 } else
Owen Anderson721c3702011-08-22 18:42:13 +00004262 Inst.addOperand(MCOperand::CreateReg(0));
Owen Andersonb9d82f42011-08-15 18:44:44 +00004263 }
4264
Owen Anderson03aadae2011-09-01 23:23:50 +00004265 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
4266 return MCDisassembler::Fail;
4267 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
4268 return MCDisassembler::Fail;
4269 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
4270 return MCDisassembler::Fail;
4271 if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+3*inc, Address, Decoder)))
4272 return MCDisassembler::Fail;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004273 Inst.addOperand(MCOperand::CreateImm(index));
4274
Owen Andersona4043c42011-08-17 17:44:15 +00004275 return S;
Owen Andersonb9d82f42011-08-15 18:44:44 +00004276}
4277
Craig Topperf6e7e122012-03-27 07:21:54 +00004278static DecodeStatus DecodeVMOVSRR(MCInst &Inst, unsigned Insn,
Owen Andersondf698b02011-08-22 20:27:12 +00004279 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004280 DecodeStatus S = MCDisassembler::Success;
Jim Grosbachecaef492012-08-14 19:06:05 +00004281 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
4282 unsigned Rt2 = fieldFromInstruction(Insn, 16, 4);
4283 unsigned Rm = fieldFromInstruction(Insn, 5, 1);
4284 unsigned pred = fieldFromInstruction(Insn, 28, 4);
4285 Rm |= fieldFromInstruction(Insn, 0, 4) << 1;
Owen Andersondf698b02011-08-22 20:27:12 +00004286
4287 if (Rt == 0xF || Rt2 == 0xF || Rm == 0x1F)
James Molloydb4ce602011-09-01 18:02:14 +00004288 S = MCDisassembler::SoftFail;
Owen Andersondf698b02011-08-22 20:27:12 +00004289
Owen Anderson03aadae2011-09-01 23:23:50 +00004290 if (!Check(S, DecodeSPRRegisterClass(Inst, Rm , Address, Decoder)))
4291 return MCDisassembler::Fail;
4292 if (!Check(S, DecodeSPRRegisterClass(Inst, Rm+1, Address, Decoder)))
4293 return MCDisassembler::Fail;
4294 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt , Address, Decoder)))
4295 return MCDisassembler::Fail;
4296 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt2 , Address, Decoder)))
4297 return MCDisassembler::Fail;
4298 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
4299 return MCDisassembler::Fail;
Owen Andersondf698b02011-08-22 20:27:12 +00004300
4301 return S;
4302}
4303
Craig Topperf6e7e122012-03-27 07:21:54 +00004304static DecodeStatus DecodeVMOVRRS(MCInst &Inst, unsigned Insn,
Owen Andersondf698b02011-08-22 20:27:12 +00004305 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004306 DecodeStatus S = MCDisassembler::Success;
Jim Grosbachecaef492012-08-14 19:06:05 +00004307 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
4308 unsigned Rt2 = fieldFromInstruction(Insn, 16, 4);
4309 unsigned Rm = fieldFromInstruction(Insn, 5, 1);
4310 unsigned pred = fieldFromInstruction(Insn, 28, 4);
4311 Rm |= fieldFromInstruction(Insn, 0, 4) << 1;
Owen Andersondf698b02011-08-22 20:27:12 +00004312
4313 if (Rt == 0xF || Rt2 == 0xF || Rm == 0x1F)
James Molloydb4ce602011-09-01 18:02:14 +00004314 S = MCDisassembler::SoftFail;
Owen Andersondf698b02011-08-22 20:27:12 +00004315
Owen Anderson03aadae2011-09-01 23:23:50 +00004316 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt , Address, Decoder)))
4317 return MCDisassembler::Fail;
4318 if (!Check(S, DecodeGPRRegisterClass(Inst, Rt2 , Address, Decoder)))
4319 return MCDisassembler::Fail;
4320 if (!Check(S, DecodeSPRRegisterClass(Inst, Rm , Address, Decoder)))
4321 return MCDisassembler::Fail;
4322 if (!Check(S, DecodeSPRRegisterClass(Inst, Rm+1, Address, Decoder)))
4323 return MCDisassembler::Fail;
4324 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
4325 return MCDisassembler::Fail;
Owen Andersondf698b02011-08-22 20:27:12 +00004326
4327 return S;
4328}
Owen Andersoneb1367b2011-08-22 23:44:04 +00004329
Craig Topperf6e7e122012-03-27 07:21:54 +00004330static DecodeStatus DecodeIT(MCInst &Inst, unsigned Insn,
Owen Anderson2fa06a72011-08-30 22:58:27 +00004331 uint64_t Address, const void *Decoder) {
Owen Anderson03aadae2011-09-01 23:23:50 +00004332 DecodeStatus S = MCDisassembler::Success;
Jim Grosbachecaef492012-08-14 19:06:05 +00004333 unsigned pred = fieldFromInstruction(Insn, 4, 4);
4334 unsigned mask = fieldFromInstruction(Insn, 0, 4);
Owen Anderson2fa06a72011-08-30 22:58:27 +00004335
4336 if (pred == 0xF) {
4337 pred = 0xE;
James Molloydb4ce602011-09-01 18:02:14 +00004338 S = MCDisassembler::SoftFail;
Owen Anderson52300412011-08-24 17:21:43 +00004339 }
4340
Richard Bartonf435b092012-04-27 08:42:59 +00004341 if (mask == 0x0) {
Owen Anderson2fa06a72011-08-30 22:58:27 +00004342 mask |= 0x8;
James Molloydb4ce602011-09-01 18:02:14 +00004343 S = MCDisassembler::SoftFail;
Owen Anderson37612a32011-08-24 22:40:22 +00004344 }
Owen Anderson2fa06a72011-08-30 22:58:27 +00004345
4346 Inst.addOperand(MCOperand::CreateImm(pred));
4347 Inst.addOperand(MCOperand::CreateImm(mask));
Owen Anderson37612a32011-08-24 22:40:22 +00004348 return S;
4349}
Jim Grosbach7db8d692011-09-08 22:07:06 +00004350
4351static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00004352DecodeT2LDRDPreInstruction(MCInst &Inst, unsigned Insn,
Jim Grosbach7db8d692011-09-08 22:07:06 +00004353 uint64_t Address, const void *Decoder) {
4354 DecodeStatus S = MCDisassembler::Success;
4355
Jim Grosbachecaef492012-08-14 19:06:05 +00004356 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
4357 unsigned Rt2 = fieldFromInstruction(Insn, 8, 4);
4358 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
4359 unsigned addr = fieldFromInstruction(Insn, 0, 8);
4360 unsigned W = fieldFromInstruction(Insn, 21, 1);
4361 unsigned U = fieldFromInstruction(Insn, 23, 1);
4362 unsigned P = fieldFromInstruction(Insn, 24, 1);
Jim Grosbach7db8d692011-09-08 22:07:06 +00004363 bool writeback = (W == 1) | (P == 0);
4364
4365 addr |= (U << 8) | (Rn << 9);
4366
4367 if (writeback && (Rn == Rt || Rn == Rt2))
4368 Check(S, MCDisassembler::SoftFail);
4369 if (Rt == Rt2)
4370 Check(S, MCDisassembler::SoftFail);
4371
4372 // Rt
4373 if (!Check(S, DecoderGPRRegisterClass(Inst, Rt, Address, Decoder)))
4374 return MCDisassembler::Fail;
4375 // Rt2
4376 if (!Check(S, DecoderGPRRegisterClass(Inst, Rt2, Address, Decoder)))
4377 return MCDisassembler::Fail;
4378 // Writeback operand
4379 if (!Check(S, DecoderGPRRegisterClass(Inst, Rn, Address, Decoder)))
4380 return MCDisassembler::Fail;
4381 // addr
4382 if (!Check(S, DecodeT2AddrModeImm8s4(Inst, addr, Address, Decoder)))
4383 return MCDisassembler::Fail;
4384
4385 return S;
4386}
4387
4388static DecodeStatus
Craig Topperf6e7e122012-03-27 07:21:54 +00004389DecodeT2STRDPreInstruction(MCInst &Inst, unsigned Insn,
Jim Grosbach7db8d692011-09-08 22:07:06 +00004390 uint64_t Address, const void *Decoder) {
4391 DecodeStatus S = MCDisassembler::Success;
4392
Jim Grosbachecaef492012-08-14 19:06:05 +00004393 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
4394 unsigned Rt2 = fieldFromInstruction(Insn, 8, 4);
4395 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
4396 unsigned addr = fieldFromInstruction(Insn, 0, 8);
4397 unsigned W = fieldFromInstruction(Insn, 21, 1);
4398 unsigned U = fieldFromInstruction(Insn, 23, 1);
4399 unsigned P = fieldFromInstruction(Insn, 24, 1);
Jim Grosbach7db8d692011-09-08 22:07:06 +00004400 bool writeback = (W == 1) | (P == 0);
4401
4402 addr |= (U << 8) | (Rn << 9);
4403
4404 if (writeback && (Rn == Rt || Rn == Rt2))
4405 Check(S, MCDisassembler::SoftFail);
4406
4407 // Writeback operand
4408 if (!Check(S, DecoderGPRRegisterClass(Inst, Rn, Address, Decoder)))
4409 return MCDisassembler::Fail;
4410 // Rt
4411 if (!Check(S, DecoderGPRRegisterClass(Inst, Rt, Address, Decoder)))
4412 return MCDisassembler::Fail;
4413 // Rt2
4414 if (!Check(S, DecoderGPRRegisterClass(Inst, Rt2, Address, Decoder)))
4415 return MCDisassembler::Fail;
4416 // addr
4417 if (!Check(S, DecodeT2AddrModeImm8s4(Inst, addr, Address, Decoder)))
4418 return MCDisassembler::Fail;
4419
4420 return S;
4421}
Owen Anderson5bfb0e02011-09-09 22:24:36 +00004422
Craig Topperf6e7e122012-03-27 07:21:54 +00004423static DecodeStatus DecodeT2Adr(MCInst &Inst, uint32_t Insn,
Owen Anderson5bfb0e02011-09-09 22:24:36 +00004424 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00004425 unsigned sign1 = fieldFromInstruction(Insn, 21, 1);
4426 unsigned sign2 = fieldFromInstruction(Insn, 23, 1);
Owen Anderson5bfb0e02011-09-09 22:24:36 +00004427 if (sign1 != sign2) return MCDisassembler::Fail;
4428
Jim Grosbachecaef492012-08-14 19:06:05 +00004429 unsigned Val = fieldFromInstruction(Insn, 0, 8);
4430 Val |= fieldFromInstruction(Insn, 12, 3) << 8;
4431 Val |= fieldFromInstruction(Insn, 26, 1) << 11;
Owen Anderson5bfb0e02011-09-09 22:24:36 +00004432 Val |= sign1 << 12;
4433 Inst.addOperand(MCOperand::CreateImm(SignExtend32<13>(Val)));
4434
4435 return MCDisassembler::Success;
4436}
4437
Craig Topperf6e7e122012-03-27 07:21:54 +00004438static DecodeStatus DecodeT2ShifterImmOperand(MCInst &Inst, uint32_t Val,
Owen Andersonf01e2de2011-09-26 21:06:22 +00004439 uint64_t Address,
4440 const void *Decoder) {
4441 DecodeStatus S = MCDisassembler::Success;
4442
4443 // Shift of "asr #32" is not allowed in Thumb2 mode.
4444 if (Val == 0x20) S = MCDisassembler::SoftFail;
4445 Inst.addOperand(MCOperand::CreateImm(Val));
4446 return S;
4447}
4448
Craig Topperf6e7e122012-03-27 07:21:54 +00004449static DecodeStatus DecodeSwap(MCInst &Inst, unsigned Insn,
Owen Andersondde461c2011-10-28 18:02:13 +00004450 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00004451 unsigned Rt = fieldFromInstruction(Insn, 12, 4);
4452 unsigned Rt2 = fieldFromInstruction(Insn, 0, 4);
4453 unsigned Rn = fieldFromInstruction(Insn, 16, 4);
4454 unsigned pred = fieldFromInstruction(Insn, 28, 4);
Owen Andersondde461c2011-10-28 18:02:13 +00004455
4456 if (pred == 0xF)
4457 return DecodeCPSInstruction(Inst, Insn, Address, Decoder);
4458
4459 DecodeStatus S = MCDisassembler::Success;
Silviu Barangaca45af92012-04-18 14:18:57 +00004460
4461 if (Rt == Rn || Rn == Rt2)
4462 S = MCDisassembler::SoftFail;
4463
Owen Andersondde461c2011-10-28 18:02:13 +00004464 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt, Address, Decoder)))
4465 return MCDisassembler::Fail;
4466 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt2, Address, Decoder)))
4467 return MCDisassembler::Fail;
4468 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
4469 return MCDisassembler::Fail;
4470 if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
4471 return MCDisassembler::Fail;
4472
4473 return S;
4474}
Owen Anderson0ac90582011-11-15 19:55:00 +00004475
Craig Topperf6e7e122012-03-27 07:21:54 +00004476static DecodeStatus DecodeVCVTD(MCInst &Inst, unsigned Insn,
Owen Anderson0ac90582011-11-15 19:55:00 +00004477 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00004478 unsigned Vd = (fieldFromInstruction(Insn, 12, 4) << 0);
4479 Vd |= (fieldFromInstruction(Insn, 22, 1) << 4);
4480 unsigned Vm = (fieldFromInstruction(Insn, 0, 4) << 0);
4481 Vm |= (fieldFromInstruction(Insn, 5, 1) << 4);
4482 unsigned imm = fieldFromInstruction(Insn, 16, 6);
4483 unsigned cmode = fieldFromInstruction(Insn, 8, 4);
Amaury de la Vieuvillef4ec0c852013-06-08 13:54:05 +00004484 unsigned op = fieldFromInstruction(Insn, 5, 1);
Owen Anderson0ac90582011-11-15 19:55:00 +00004485
4486 DecodeStatus S = MCDisassembler::Success;
4487
4488 // VMOVv2f32 is ambiguous with these decodings.
Owen Anderson05060f02011-11-15 20:30:41 +00004489 if (!(imm & 0x38) && cmode == 0xF) {
Amaury de la Vieuvillef4ec0c852013-06-08 13:54:05 +00004490 if (op == 1) return MCDisassembler::Fail;
Owen Anderson0ac90582011-11-15 19:55:00 +00004491 Inst.setOpcode(ARM::VMOVv2f32);
4492 return DecodeNEONModImmInstruction(Inst, Insn, Address, Decoder);
4493 }
4494
Amaury de la Vieuvilleea7bb572013-06-08 13:29:11 +00004495 if (!(imm & 0x20)) return MCDisassembler::Fail;
Owen Anderson0ac90582011-11-15 19:55:00 +00004496
4497 if (!Check(S, DecodeDPRRegisterClass(Inst, Vd, Address, Decoder)))
4498 return MCDisassembler::Fail;
4499 if (!Check(S, DecodeDPRRegisterClass(Inst, Vm, Address, Decoder)))
4500 return MCDisassembler::Fail;
4501 Inst.addOperand(MCOperand::CreateImm(64 - imm));
4502
4503 return S;
4504}
4505
Craig Topperf6e7e122012-03-27 07:21:54 +00004506static DecodeStatus DecodeVCVTQ(MCInst &Inst, unsigned Insn,
Owen Anderson0ac90582011-11-15 19:55:00 +00004507 uint64_t Address, const void *Decoder) {
Jim Grosbachecaef492012-08-14 19:06:05 +00004508 unsigned Vd = (fieldFromInstruction(Insn, 12, 4) << 0);
4509 Vd |= (fieldFromInstruction(Insn, 22, 1) << 4);
4510 unsigned Vm = (fieldFromInstruction(Insn, 0, 4) << 0);
4511 Vm |= (fieldFromInstruction(Insn, 5, 1) << 4);
4512 unsigned imm = fieldFromInstruction(Insn, 16, 6);
4513 unsigned cmode = fieldFromInstruction(Insn, 8, 4);
Amaury de la Vieuvillef4ec0c852013-06-08 13:54:05 +00004514 unsigned op = fieldFromInstruction(Insn, 5, 1);
Owen Anderson0ac90582011-11-15 19:55:00 +00004515
4516 DecodeStatus S = MCDisassembler::Success;
4517
4518 // VMOVv4f32 is ambiguous with these decodings.
4519 if (!(imm & 0x38) && cmode == 0xF) {
Amaury de la Vieuvillef4ec0c852013-06-08 13:54:05 +00004520 if (op == 1) return MCDisassembler::Fail;
Owen Anderson0ac90582011-11-15 19:55:00 +00004521 Inst.setOpcode(ARM::VMOVv4f32);
4522 return DecodeNEONModImmInstruction(Inst, Insn, Address, Decoder);
4523 }
4524
Amaury de la Vieuvilleea7bb572013-06-08 13:29:11 +00004525 if (!(imm & 0x20)) return MCDisassembler::Fail;
Owen Anderson0ac90582011-11-15 19:55:00 +00004526
4527 if (!Check(S, DecodeQPRRegisterClass(Inst, Vd, Address, Decoder)))
4528 return MCDisassembler::Fail;
4529 if (!Check(S, DecodeQPRRegisterClass(Inst, Vm, Address, Decoder)))
4530 return MCDisassembler::Fail;
4531 Inst.addOperand(MCOperand::CreateImm(64 - imm));
4532
4533 return S;
4534}
Silviu Barangad213f212012-03-22 13:24:43 +00004535
Quentin Colombet6f03f622013-04-17 18:46:12 +00004536static DecodeStatus DecodeImm0_4(MCInst &Inst, unsigned Insn, uint64_t Address,
4537 const void *Decoder)
4538{
4539 unsigned Imm = fieldFromInstruction(Insn, 0, 3);
4540 if (Imm > 4) return MCDisassembler::Fail;
4541 Inst.addOperand(MCOperand::CreateImm(Imm));
4542 return MCDisassembler::Success;
4543}
4544
Craig Topperf6e7e122012-03-27 07:21:54 +00004545static DecodeStatus DecodeLDR(MCInst &Inst, unsigned Val,
Silviu Barangad213f212012-03-22 13:24:43 +00004546 uint64_t Address, const void *Decoder) {
4547 DecodeStatus S = MCDisassembler::Success;
4548
Jim Grosbachecaef492012-08-14 19:06:05 +00004549 unsigned Rn = fieldFromInstruction(Val, 16, 4);
4550 unsigned Rt = fieldFromInstruction(Val, 12, 4);
4551 unsigned Rm = fieldFromInstruction(Val, 0, 4);
4552 Rm |= (fieldFromInstruction(Val, 23, 1) << 4);
4553 unsigned Cond = fieldFromInstruction(Val, 28, 4);
Silviu Barangad213f212012-03-22 13:24:43 +00004554
Jim Grosbachecaef492012-08-14 19:06:05 +00004555 if (fieldFromInstruction(Val, 8, 4) != 0 || Rn == Rt)
Silviu Barangad213f212012-03-22 13:24:43 +00004556 S = MCDisassembler::SoftFail;
4557
4558 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt, Address, Decoder)))
4559 return MCDisassembler::Fail;
4560 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
4561 return MCDisassembler::Fail;
4562 if (!Check(S, DecodeAddrMode7Operand(Inst, Rn, Address, Decoder)))
4563 return MCDisassembler::Fail;
4564 if (!Check(S, DecodePostIdxReg(Inst, Rm, Address, Decoder)))
4565 return MCDisassembler::Fail;
4566 if (!Check(S, DecodePredicateOperand(Inst, Cond, Address, Decoder)))
4567 return MCDisassembler::Fail;
4568
4569 return S;
4570}
4571
Silviu Baranga41f1fcd2012-04-18 13:12:50 +00004572static DecodeStatus DecodeMRRC2(llvm::MCInst &Inst, unsigned Val,
4573 uint64_t Address, const void *Decoder) {
4574
4575 DecodeStatus S = MCDisassembler::Success;
4576
Jim Grosbachecaef492012-08-14 19:06:05 +00004577 unsigned CRm = fieldFromInstruction(Val, 0, 4);
4578 unsigned opc1 = fieldFromInstruction(Val, 4, 4);
4579 unsigned cop = fieldFromInstruction(Val, 8, 4);
4580 unsigned Rt = fieldFromInstruction(Val, 12, 4);
4581 unsigned Rt2 = fieldFromInstruction(Val, 16, 4);
Silviu Baranga41f1fcd2012-04-18 13:12:50 +00004582
4583 if ((cop & ~0x1) == 0xa)
4584 return MCDisassembler::Fail;
4585
4586 if (Rt == Rt2)
4587 S = MCDisassembler::SoftFail;
4588
4589 Inst.addOperand(MCOperand::CreateImm(cop));
4590 Inst.addOperand(MCOperand::CreateImm(opc1));
4591 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt, Address, Decoder)))
4592 return MCDisassembler::Fail;
4593 if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt2, Address, Decoder)))
4594 return MCDisassembler::Fail;
4595 Inst.addOperand(MCOperand::CreateImm(CRm));
4596
4597 return S;
4598}
4599