3cdcb0505962068c235e0c4fafb800081a455cdf
[oota-llvm.git] / lib / Target / ARM / Disassembler / ARMDisassembler.cpp
1 //===- ARMDisassembler.cpp - Disassembler for ARM/Thumb ISA -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #define DEBUG_TYPE "arm-disassembler"
11
12 #include "ARMDisassembler.h"
13 #include "ARM.h"
14 #include "ARMRegisterInfo.h"
15 #include "MCTargetDesc/ARMAddressingModes.h"
16 #include "MCTargetDesc/ARMBaseInfo.h"
17 #include "llvm/MC/EDInstInfo.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/MemoryObject.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/TargetRegistry.h"
25 #include "llvm/Support/raw_ostream.h"
26
27 // Pull DecodeStatus and its enum values into the global namespace.
28 typedef llvm::MCDisassembler::DecodeStatus DecodeStatus;
29 #define Success llvm::MCDisassembler::Success
30 #define Unpredictable llvm::MCDisassembler::SoftFail
31 #define Fail llvm::MCDisassembler::Fail
32
33 // Helper macro to perform setwise reduction of the current running status
34 // and another status, and return if the new status is Fail.
35 #define CHECK(S,X) do {                           \
36     S = (DecodeStatus) ((int)S & (X));            \
37     if (S == Fail) return Fail;                   \
38   } while(0)
39
40 // Forward declare these because the autogenerated code will reference them.
41 // Definitions are further down.
42 static DecodeStatus DecodeGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
43                                    uint64_t Address, const void *Decoder);
44 static DecodeStatus DecodeGPRnopcRegisterClass(llvm::MCInst &Inst,
45                                                unsigned RegNo, uint64_t Address,
46                                                const void *Decoder);
47 static DecodeStatus DecodetGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
48                                    uint64_t Address, const void *Decoder);
49 static DecodeStatus DecodetcGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
50                                    uint64_t Address, const void *Decoder);
51 static DecodeStatus DecoderGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
52                                    uint64_t Address, const void *Decoder);
53 static DecodeStatus DecodeSPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
54                                    uint64_t Address, const void *Decoder);
55 static DecodeStatus DecodeDPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
56                                    uint64_t Address, const void *Decoder);
57 static DecodeStatus DecodeDPR_8RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
58                                    uint64_t Address, const void *Decoder);
59 static DecodeStatus DecodeDPR_VFP2RegisterClass(llvm::MCInst &Inst,
60                                                 unsigned RegNo,
61                                                 uint64_t Address,
62                                                 const void *Decoder);
63 static DecodeStatus DecodeQPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
64                                    uint64_t Address, const void *Decoder);
65
66 static DecodeStatus DecodePredicateOperand(llvm::MCInst &Inst, unsigned Val,
67                                uint64_t Address, const void *Decoder);
68 static DecodeStatus DecodeCCOutOperand(llvm::MCInst &Inst, unsigned Val,
69                                uint64_t Address, const void *Decoder);
70 static DecodeStatus DecodeSOImmOperand(llvm::MCInst &Inst, unsigned Val,
71                                uint64_t Address, const void *Decoder);
72 static DecodeStatus DecodeBLTargetOperand(llvm::MCInst &Inst, unsigned Val,
73                                uint64_t Address, const void *Decoder);
74 static DecodeStatus DecodeRegListOperand(llvm::MCInst &Inst, unsigned Val,
75                                uint64_t Address, const void *Decoder);
76 static DecodeStatus DecodeSPRRegListOperand(llvm::MCInst &Inst, unsigned Val,
77                                uint64_t Address, const void *Decoder);
78 static DecodeStatus DecodeDPRRegListOperand(llvm::MCInst &Inst, unsigned Val,
79                                uint64_t Address, const void *Decoder);
80
81 static DecodeStatus DecodeBitfieldMaskOperand(llvm::MCInst &Inst, unsigned Insn,
82                                uint64_t Address, const void *Decoder);
83 static DecodeStatus DecodeCopMemInstruction(llvm::MCInst &Inst, unsigned Insn,
84                                uint64_t Address, const void *Decoder);
85 static DecodeStatus DecodeAddrMode2IdxInstruction(llvm::MCInst &Inst,
86                                                   unsigned Insn,
87                                                   uint64_t Address,
88                                                   const void *Decoder);
89 static DecodeStatus DecodeSORegMemOperand(llvm::MCInst &Inst, unsigned Insn,
90                                uint64_t Address, const void *Decoder);
91 static DecodeStatus DecodeAddrMode3Instruction(llvm::MCInst &Inst,unsigned Insn,
92                                uint64_t Address, const void *Decoder);
93 static DecodeStatus DecodeSORegImmOperand(llvm::MCInst &Inst, unsigned Insn,
94                                uint64_t Address, const void *Decoder);
95 static DecodeStatus DecodeSORegRegOperand(llvm::MCInst &Inst, unsigned Insn,
96                                uint64_t Address, const void *Decoder);
97
98 static DecodeStatus DecodeMemMultipleWritebackInstruction(llvm::MCInst & Inst,
99                                                   unsigned Insn,
100                                                   uint64_t Adddress,
101                                                   const void *Decoder);
102 static DecodeStatus DecodeSMLAInstruction(llvm::MCInst &Inst, unsigned Insn,
103                                uint64_t Address, const void *Decoder);
104 static DecodeStatus DecodeCPSInstruction(llvm::MCInst &Inst, unsigned Insn,
105                                uint64_t Address, const void *Decoder);
106 static DecodeStatus DecodeT2CPSInstruction(llvm::MCInst &Inst, unsigned Insn,
107                                uint64_t Address, const void *Decoder);
108 static DecodeStatus DecodeAddrModeImm12Operand(llvm::MCInst &Inst, unsigned Val,
109                                uint64_t Address, const void *Decoder);
110 static DecodeStatus DecodeAddrMode5Operand(llvm::MCInst &Inst, unsigned Val,
111                                uint64_t Address, const void *Decoder);
112 static DecodeStatus DecodeAddrMode7Operand(llvm::MCInst &Inst, unsigned Val,
113                                uint64_t Address, const void *Decoder);
114 static DecodeStatus DecodeBranchImmInstruction(llvm::MCInst &Inst,unsigned Insn,
115                                uint64_t Address, const void *Decoder);
116 static DecodeStatus DecodeVCVTImmOperand(llvm::MCInst &Inst, unsigned Val,
117                                uint64_t Address, const void *Decoder);
118 static DecodeStatus DecodeAddrMode6Operand(llvm::MCInst &Inst, unsigned Val,
119                                uint64_t Address, const void *Decoder);
120 static DecodeStatus DecodeVLDInstruction(llvm::MCInst &Inst, unsigned Val,
121                                uint64_t Address, const void *Decoder);
122 static DecodeStatus DecodeVSTInstruction(llvm::MCInst &Inst, unsigned Val,
123                                uint64_t Address, const void *Decoder);
124 static DecodeStatus DecodeVLD1DupInstruction(llvm::MCInst &Inst, unsigned Val,
125                                uint64_t Address, const void *Decoder);
126 static DecodeStatus DecodeVLD2DupInstruction(llvm::MCInst &Inst, unsigned Val,
127                                uint64_t Address, const void *Decoder);
128 static DecodeStatus DecodeVLD3DupInstruction(llvm::MCInst &Inst, unsigned Val,
129                                uint64_t Address, const void *Decoder);
130 static DecodeStatus DecodeVLD4DupInstruction(llvm::MCInst &Inst, unsigned Val,
131                                uint64_t Address, const void *Decoder);
132 static DecodeStatus DecodeNEONModImmInstruction(llvm::MCInst &Inst,unsigned Val,
133                                uint64_t Address, const void *Decoder);
134 static DecodeStatus DecodeVSHLMaxInstruction(llvm::MCInst &Inst, unsigned Val,
135                                uint64_t Address, const void *Decoder);
136 static DecodeStatus DecodeShiftRight8Imm(llvm::MCInst &Inst, unsigned Val,
137                                uint64_t Address, const void *Decoder);
138 static DecodeStatus DecodeShiftRight16Imm(llvm::MCInst &Inst, unsigned Val,
139                                uint64_t Address, const void *Decoder);
140 static DecodeStatus DecodeShiftRight32Imm(llvm::MCInst &Inst, unsigned Val,
141                                uint64_t Address, const void *Decoder);
142 static DecodeStatus DecodeShiftRight64Imm(llvm::MCInst &Inst, unsigned Val,
143                                uint64_t Address, const void *Decoder);
144 static DecodeStatus DecodeTBLInstruction(llvm::MCInst &Inst, unsigned Insn,
145                                uint64_t Address, const void *Decoder);
146 static DecodeStatus DecodeVFPfpImm(llvm::MCInst &Inst, unsigned Val,
147                                uint64_t Address, const void *Decoder);
148 static DecodeStatus DecodePostIdxReg(llvm::MCInst &Inst, unsigned Insn,
149                                uint64_t Address, const void *Decoder);
150 static DecodeStatus DecodeCoprocessor(llvm::MCInst &Inst, unsigned Insn,
151                                uint64_t Address, const void *Decoder);
152 static DecodeStatus DecodeMemBarrierOption(llvm::MCInst &Inst, unsigned Insn,
153                                uint64_t Address, const void *Decoder);
154 static DecodeStatus DecodeMSRMask(llvm::MCInst &Inst, unsigned Insn,
155                                uint64_t Address, const void *Decoder);
156 static DecodeStatus DecodeDoubleRegLoad(llvm::MCInst &Inst, unsigned Insn,
157                                uint64_t Address, const void *Decoder);
158 static DecodeStatus DecodeDoubleRegStore(llvm::MCInst &Inst, unsigned Insn,
159                                uint64_t Address, const void *Decoder);
160 static DecodeStatus DecodeLDRPreImm(llvm::MCInst &Inst, unsigned Insn,
161                                uint64_t Address, const void *Decoder);
162 static DecodeStatus DecodeLDRPreReg(llvm::MCInst &Inst, unsigned Insn,
163                                uint64_t Address, const void *Decoder);
164 static DecodeStatus DecodeSTRPreImm(llvm::MCInst &Inst, unsigned Insn,
165                                uint64_t Address, const void *Decoder);
166 static DecodeStatus DecodeSTRPreReg(llvm::MCInst &Inst, unsigned Insn,
167                                uint64_t Address, const void *Decoder);
168 static DecodeStatus DecodeVLD1LN(llvm::MCInst &Inst, unsigned Insn,
169                                uint64_t Address, const void *Decoder);
170 static DecodeStatus DecodeVLD2LN(llvm::MCInst &Inst, unsigned Insn,
171                                uint64_t Address, const void *Decoder);
172 static DecodeStatus DecodeVLD3LN(llvm::MCInst &Inst, unsigned Insn,
173                                uint64_t Address, const void *Decoder);
174 static DecodeStatus DecodeVLD4LN(llvm::MCInst &Inst, unsigned Insn,
175                                uint64_t Address, const void *Decoder);
176 static DecodeStatus DecodeVST1LN(llvm::MCInst &Inst, unsigned Insn,
177                                uint64_t Address, const void *Decoder);
178 static DecodeStatus DecodeVST2LN(llvm::MCInst &Inst, unsigned Insn,
179                                uint64_t Address, const void *Decoder);
180 static DecodeStatus DecodeVST3LN(llvm::MCInst &Inst, unsigned Insn,
181                                uint64_t Address, const void *Decoder);
182 static DecodeStatus DecodeVST4LN(llvm::MCInst &Inst, unsigned Insn,
183                                uint64_t Address, const void *Decoder);
184 static DecodeStatus DecodeVMOVSRR(llvm::MCInst &Inst, unsigned Insn,
185                                uint64_t Address, const void *Decoder);
186 static DecodeStatus DecodeVMOVRRS(llvm::MCInst &Inst, unsigned Insn,
187                                uint64_t Address, const void *Decoder);
188
189 static DecodeStatus DecodeThumbAddSpecialReg(llvm::MCInst &Inst, uint16_t Insn,
190                                uint64_t Address, const void *Decoder);
191 static DecodeStatus DecodeThumbBROperand(llvm::MCInst &Inst, unsigned Val,
192                                uint64_t Address, const void *Decoder);
193 static DecodeStatus DecodeT2BROperand(llvm::MCInst &Inst, unsigned Val,
194                                uint64_t Address, const void *Decoder);
195 static DecodeStatus DecodeThumbCmpBROperand(llvm::MCInst &Inst, unsigned Val,
196                                uint64_t Address, const void *Decoder);
197 static DecodeStatus DecodeThumbAddrModeRR(llvm::MCInst &Inst, unsigned Val,
198                                uint64_t Address, const void *Decoder);
199 static DecodeStatus DecodeThumbAddrModeIS(llvm::MCInst &Inst, unsigned Val,
200                                uint64_t Address, const void *Decoder);
201 static DecodeStatus DecodeThumbAddrModePC(llvm::MCInst &Inst, unsigned Val,
202                                uint64_t Address, const void *Decoder);
203 static DecodeStatus DecodeThumbAddrModeSP(llvm::MCInst &Inst, unsigned Val,
204                                uint64_t Address, const void *Decoder);
205 static DecodeStatus DecodeT2AddrModeSOReg(llvm::MCInst &Inst, unsigned Val,
206                                uint64_t Address, const void *Decoder);
207 static DecodeStatus DecodeT2LoadShift(llvm::MCInst &Inst, unsigned Val,
208                                uint64_t Address, const void *Decoder);
209 static DecodeStatus DecodeT2Imm8S4(llvm::MCInst &Inst, unsigned Val,
210                                uint64_t Address, const void *Decoder);
211 static DecodeStatus DecodeT2AddrModeImm8s4(llvm::MCInst &Inst, unsigned Val,
212                                uint64_t Address, const void *Decoder);
213 static DecodeStatus DecodeT2Imm8(llvm::MCInst &Inst, unsigned Val,
214                                uint64_t Address, const void *Decoder);
215 static DecodeStatus DecodeT2AddrModeImm8(llvm::MCInst &Inst, unsigned Val,
216                                uint64_t Address, const void *Decoder);
217 static DecodeStatus DecodeThumbAddSPImm(llvm::MCInst &Inst, uint16_t Val,
218                                uint64_t Address, const void *Decoder);
219 static DecodeStatus DecodeThumbAddSPReg(llvm::MCInst &Inst, uint16_t Insn,
220                                 uint64_t Address, const void *Decoder);
221 static DecodeStatus DecodeThumbCPS(llvm::MCInst &Inst, uint16_t Insn,
222                                 uint64_t Address, const void *Decoder);
223 static DecodeStatus DecodeThumbBLXOffset(llvm::MCInst &Inst, unsigned Insn,
224                                 uint64_t Address, const void *Decoder);
225 static DecodeStatus DecodeT2AddrModeImm12(llvm::MCInst &Inst, unsigned Val,
226                                 uint64_t Address, const void *Decoder);
227 static DecodeStatus DecodeThumb2BCCInstruction(llvm::MCInst &Inst, unsigned Val,
228                                 uint64_t Address, const void *Decoder);
229 static DecodeStatus DecodeT2SOImm(llvm::MCInst &Inst, unsigned Val,
230                                 uint64_t Address, const void *Decoder);
231 static DecodeStatus DecodeThumbBCCTargetOperand(llvm::MCInst &Inst,unsigned Val,
232                                 uint64_t Address, const void *Decoder);
233 static DecodeStatus DecodeThumbBLTargetOperand(llvm::MCInst &Inst, unsigned Val,
234                                 uint64_t Address, const void *Decoder);
235 static DecodeStatus DecodeITCond(llvm::MCInst &Inst, unsigned Val,
236                                 uint64_t Address, const void *Decoder);
237 static DecodeStatus DecodeITMask(llvm::MCInst &Inst, unsigned Val,
238                                 uint64_t Address, const void *Decoder);
239
240 #include "ARMGenDisassemblerTables.inc"
241 #include "ARMGenInstrInfo.inc"
242 #include "ARMGenEDInfo.inc"
243
244 using namespace llvm;
245
246 static MCDisassembler *createARMDisassembler(const Target &T) {
247   return new ARMDisassembler;
248 }
249
250 static MCDisassembler *createThumbDisassembler(const Target &T) {
251   return new ThumbDisassembler;
252 }
253
254 EDInstInfo *ARMDisassembler::getEDInfo() const {
255   return instInfoARM;
256 }
257
258 EDInstInfo *ThumbDisassembler::getEDInfo() const {
259   return instInfoARM;
260 }
261
262 DecodeStatus ARMDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
263                                              const MemoryObject &Region,
264                                              uint64_t Address,
265                                              raw_ostream &os) const {
266   uint8_t bytes[4];
267
268   // We want to read exactly 4 bytes of data.
269   if (Region.readBytes(Address, 4, (uint8_t*)bytes, NULL) == -1) {
270     Size = 0;
271     return Fail;
272   }
273
274   // Encoded as a small-endian 32-bit word in the stream.
275   uint32_t insn = (bytes[3] << 24) |
276                   (bytes[2] << 16) |
277                   (bytes[1] <<  8) |
278                   (bytes[0] <<  0);
279
280   // Calling the auto-generated decoder function.
281   DecodeStatus result = decodeARMInstruction32(MI, insn, Address, this);
282   if (result != Fail) {
283     Size = 4;
284     return result;
285   }
286
287   // Instructions that are shared between ARM and Thumb modes.
288   // FIXME: This shouldn't really exist.  It's an artifact of the
289   // fact that we fail to encode a few instructions properly for Thumb.
290   MI.clear();
291   result = decodeCommonInstruction32(MI, insn, Address, this);
292   if (result != Fail) {
293     Size = 4;
294     return result;
295   }
296
297   // VFP and NEON instructions, similarly, are shared between ARM
298   // and Thumb modes.
299   MI.clear();
300   result = decodeVFPInstruction32(MI, insn, Address, this);
301   if (result != Fail) {
302     Size = 4;
303     return result;
304   }
305
306   MI.clear();
307   result = decodeNEONDataInstruction32(MI, insn, Address, this);
308   if (result != Fail) {
309     Size = 4;
310     // Add a fake predicate operand, because we share these instruction
311     // definitions with Thumb2 where these instructions are predicable.
312     if (!DecodePredicateOperand(MI, 0xE, Address, this)) return Fail;
313     return result;
314   }
315
316   MI.clear();
317   result = decodeNEONLoadStoreInstruction32(MI, insn, Address, this);
318   if (result != Fail) {
319     Size = 4;
320     // Add a fake predicate operand, because we share these instruction
321     // definitions with Thumb2 where these instructions are predicable.
322     if (!DecodePredicateOperand(MI, 0xE, Address, this)) return Fail;
323     return result;
324   }
325
326   MI.clear();
327   result = decodeNEONDupInstruction32(MI, insn, Address, this);
328   if (result != Fail) {
329     Size = 4;
330     // Add a fake predicate operand, because we share these instruction
331     // definitions with Thumb2 where these instructions are predicable.
332     if (!DecodePredicateOperand(MI, 0xE, Address, this)) return Fail;
333     return result;
334   }
335
336   MI.clear();
337
338   Size = 0;
339   return Fail;
340 }
341
342 namespace llvm {
343 extern MCInstrDesc ARMInsts[];
344 }
345
346 // Thumb1 instructions don't have explicit S bits.  Rather, they
347 // implicitly set CPSR.  Since it's not represented in the encoding, the
348 // auto-generated decoder won't inject the CPSR operand.  We need to fix
349 // that as a post-pass.
350 static void AddThumb1SBit(MCInst &MI, bool InITBlock) {
351   const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
352   unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
353   MCInst::iterator I = MI.begin();
354   for (unsigned i = 0; i < NumOps; ++i, ++I) {
355     if (I == MI.end()) break;
356     if (OpInfo[i].isOptionalDef() && OpInfo[i].RegClass == ARM::CCRRegClassID) {
357       if (i > 0 && OpInfo[i-1].isPredicate()) continue;
358       MI.insert(I, MCOperand::CreateReg(InITBlock ? 0 : ARM::CPSR));
359       return;
360     }
361   }
362
363   MI.insert(I, MCOperand::CreateReg(InITBlock ? 0 : ARM::CPSR));
364 }
365
366 // Most Thumb instructions don't have explicit predicates in the
367 // encoding, but rather get their predicates from IT context.  We need
368 // to fix up the predicate operands using this context information as a
369 // post-pass.
370 void ThumbDisassembler::AddThumbPredicate(MCInst &MI) const {
371   // A few instructions actually have predicates encoded in them.  Don't
372   // try to overwrite it if we're seeing one of those.
373   switch (MI.getOpcode()) {
374     case ARM::tBcc:
375     case ARM::t2Bcc:
376       return;
377     default:
378       break;
379   }
380
381   // If we're in an IT block, base the predicate on that.  Otherwise,
382   // assume a predicate of AL.
383   unsigned CC;
384   if (!ITBlock.empty()) {
385     CC = ITBlock.back();
386     if (CC == 0xF)
387       CC = ARMCC::AL;
388     ITBlock.pop_back();
389   } else
390     CC = ARMCC::AL;
391
392   const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
393   unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
394   MCInst::iterator I = MI.begin();
395   for (unsigned i = 0; i < NumOps; ++i, ++I) {
396     if (I == MI.end()) break;
397     if (OpInfo[i].isPredicate()) {
398       I = MI.insert(I, MCOperand::CreateImm(CC));
399       ++I;
400       if (CC == ARMCC::AL)
401         MI.insert(I, MCOperand::CreateReg(0));
402       else
403         MI.insert(I, MCOperand::CreateReg(ARM::CPSR));
404       return;
405     }
406   }
407
408   I = MI.insert(I, MCOperand::CreateImm(CC));
409   ++I;
410   if (CC == ARMCC::AL)
411     MI.insert(I, MCOperand::CreateReg(0));
412   else
413     MI.insert(I, MCOperand::CreateReg(ARM::CPSR));
414 }
415
416 // Thumb VFP instructions are a special case.  Because we share their
417 // encodings between ARM and Thumb modes, and they are predicable in ARM
418 // mode, the auto-generated decoder will give them an (incorrect)
419 // predicate operand.  We need to rewrite these operands based on the IT
420 // context as a post-pass.
421 void ThumbDisassembler::UpdateThumbVFPPredicate(MCInst &MI) const {
422   unsigned CC;
423   if (!ITBlock.empty()) {
424     CC = ITBlock.back();
425     ITBlock.pop_back();
426   } else
427     CC = ARMCC::AL;
428
429   const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
430   MCInst::iterator I = MI.begin();
431   unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
432   for (unsigned i = 0; i < NumOps; ++i, ++I) {
433     if (OpInfo[i].isPredicate() ) {
434       I->setImm(CC);
435       ++I;
436       if (CC == ARMCC::AL)
437         I->setReg(0);
438       else
439         I->setReg(ARM::CPSR);
440       return;
441     }
442   }
443 }
444
445 DecodeStatus ThumbDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
446                                                const MemoryObject &Region,
447                                                uint64_t Address,
448                                                raw_ostream &os) const {
449   uint8_t bytes[4];
450
451   // We want to read exactly 2 bytes of data.
452   if (Region.readBytes(Address, 2, (uint8_t*)bytes, NULL) == -1) {
453     Size = 0;
454     return Fail;
455   }
456
457   uint16_t insn16 = (bytes[1] << 8) | bytes[0];
458   DecodeStatus result = decodeThumbInstruction16(MI, insn16, Address, this);
459   if (result != Fail) {
460     Size = 2;
461     AddThumbPredicate(MI);
462     return result;
463   }
464
465   MI.clear();
466   result = decodeThumbSBitInstruction16(MI, insn16, Address, this);
467   if (result) {
468     Size = 2;
469     bool InITBlock = !ITBlock.empty();
470     AddThumbPredicate(MI);
471     AddThumb1SBit(MI, InITBlock);
472     return result;
473   }
474
475   MI.clear();
476   result = decodeThumb2Instruction16(MI, insn16, Address, this);
477   if (result != Fail) {
478     Size = 2;
479     AddThumbPredicate(MI);
480
481     // If we find an IT instruction, we need to parse its condition
482     // code and mask operands so that we can apply them correctly
483     // to the subsequent instructions.
484     if (MI.getOpcode() == ARM::t2IT) {
485       unsigned firstcond = MI.getOperand(0).getImm();
486       uint32_t mask = MI.getOperand(1).getImm();
487       unsigned zeros = CountTrailingZeros_32(mask);
488       mask >>= zeros+1;
489
490       for (unsigned i = 0; i < 4 - (zeros+1); ++i) {
491         if (firstcond ^ (mask & 1))
492           ITBlock.push_back(firstcond ^ 1);
493         else
494           ITBlock.push_back(firstcond);
495         mask >>= 1;
496       }
497       ITBlock.push_back(firstcond);
498     }
499
500     return result;
501   }
502
503   // We want to read exactly 4 bytes of data.
504   if (Region.readBytes(Address, 4, (uint8_t*)bytes, NULL) == -1) {
505     Size = 0;
506     return Fail;
507   }
508
509   uint32_t insn32 = (bytes[3] <<  8) |
510                     (bytes[2] <<  0) |
511                     (bytes[1] << 24) |
512                     (bytes[0] << 16);
513   MI.clear();
514   result = decodeThumbInstruction32(MI, insn32, Address, this);
515   if (result != Fail) {
516     Size = 4;
517     bool InITBlock = ITBlock.size();
518     AddThumbPredicate(MI);
519     AddThumb1SBit(MI, InITBlock);
520     return result;
521   }
522
523   MI.clear();
524   result = decodeThumb2Instruction32(MI, insn32, Address, this);
525   if (result != Fail) {
526     Size = 4;
527     AddThumbPredicate(MI);
528     return result;
529   }
530
531   MI.clear();
532   result = decodeCommonInstruction32(MI, insn32, Address, this);
533   if (result != Fail) {
534     Size = 4;
535     AddThumbPredicate(MI);
536     return result;
537   }
538
539   MI.clear();
540   result = decodeVFPInstruction32(MI, insn32, Address, this);
541   if (result != Fail) {
542     Size = 4;
543     UpdateThumbVFPPredicate(MI);
544     return result;
545   }
546
547   MI.clear();
548   result = decodeNEONDupInstruction32(MI, insn32, Address, this);
549   if (result != Fail) {
550     Size = 4;
551     AddThumbPredicate(MI);
552     return result;
553   }
554
555   if (fieldFromInstruction32(insn32, 24, 8) == 0xF9) {
556     MI.clear();
557     uint32_t NEONLdStInsn = insn32;
558     NEONLdStInsn &= 0xF0FFFFFF;
559     NEONLdStInsn |= 0x04000000;
560     result = decodeNEONLoadStoreInstruction32(MI, NEONLdStInsn, Address, this);
561     if (result != Fail) {
562       Size = 4;
563       AddThumbPredicate(MI);
564       return result;
565     }
566   }
567
568   if (fieldFromInstruction32(insn32, 24, 4) == 0xF) {
569     MI.clear();
570     uint32_t NEONDataInsn = insn32;
571     NEONDataInsn &= 0xF0FFFFFF; // Clear bits 27-24
572     NEONDataInsn |= (NEONDataInsn & 0x10000000) >> 4; // Move bit 28 to bit 24
573     NEONDataInsn |= 0x12000000; // Set bits 28 and 25
574     result = decodeNEONDataInstruction32(MI, NEONDataInsn, Address, this);
575     if (result != Fail) {
576       Size = 4;
577       AddThumbPredicate(MI);
578       return result;
579     }
580   }
581
582   Size = 0;
583   return Fail;
584 }
585
586
587 extern "C" void LLVMInitializeARMDisassembler() {
588   TargetRegistry::RegisterMCDisassembler(TheARMTarget,
589                                          createARMDisassembler);
590   TargetRegistry::RegisterMCDisassembler(TheThumbTarget,
591                                          createThumbDisassembler);
592 }
593
594 static const unsigned GPRDecoderTable[] = {
595   ARM::R0, ARM::R1, ARM::R2, ARM::R3,
596   ARM::R4, ARM::R5, ARM::R6, ARM::R7,
597   ARM::R8, ARM::R9, ARM::R10, ARM::R11,
598   ARM::R12, ARM::SP, ARM::LR, ARM::PC
599 };
600
601 static DecodeStatus DecodeGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
602                                    uint64_t Address, const void *Decoder) {
603   if (RegNo > 15)
604     return Fail;
605
606   unsigned Register = GPRDecoderTable[RegNo];
607   Inst.addOperand(MCOperand::CreateReg(Register));
608   return Success;
609 }
610
611 static DecodeStatus
612 DecodeGPRnopcRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
613                            uint64_t Address, const void *Decoder) {
614   if (RegNo == 15) return Fail;
615   return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
616 }
617
618 static DecodeStatus DecodetGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
619                                    uint64_t Address, const void *Decoder) {
620   if (RegNo > 7)
621     return Fail;
622   return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
623 }
624
625 static DecodeStatus DecodetcGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
626                                    uint64_t Address, const void *Decoder) {
627   unsigned Register = 0;
628   switch (RegNo) {
629     case 0:
630       Register = ARM::R0;
631       break;
632     case 1:
633       Register = ARM::R1;
634       break;
635     case 2:
636       Register = ARM::R2;
637       break;
638     case 3:
639       Register = ARM::R3;
640       break;
641     case 9:
642       Register = ARM::R9;
643       break;
644     case 12:
645       Register = ARM::R12;
646       break;
647     default:
648       return Fail;
649     }
650
651   Inst.addOperand(MCOperand::CreateReg(Register));
652   return Success;
653 }
654
655 static DecodeStatus DecoderGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
656                                    uint64_t Address, const void *Decoder) {
657   if (RegNo == 13 || RegNo == 15) return Fail;
658   return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
659 }
660
661 static const unsigned SPRDecoderTable[] = {
662      ARM::S0,  ARM::S1,  ARM::S2,  ARM::S3,
663      ARM::S4,  ARM::S5,  ARM::S6,  ARM::S7,
664      ARM::S8,  ARM::S9, ARM::S10, ARM::S11,
665     ARM::S12, ARM::S13, ARM::S14, ARM::S15,
666     ARM::S16, ARM::S17, ARM::S18, ARM::S19,
667     ARM::S20, ARM::S21, ARM::S22, ARM::S23,
668     ARM::S24, ARM::S25, ARM::S26, ARM::S27,
669     ARM::S28, ARM::S29, ARM::S30, ARM::S31
670 };
671
672 static DecodeStatus DecodeSPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
673                                    uint64_t Address, const void *Decoder) {
674   if (RegNo > 31)
675     return Fail;
676
677   unsigned Register = SPRDecoderTable[RegNo];
678   Inst.addOperand(MCOperand::CreateReg(Register));
679   return Success;
680 }
681
682 static const unsigned DPRDecoderTable[] = {
683      ARM::D0,  ARM::D1,  ARM::D2,  ARM::D3,
684      ARM::D4,  ARM::D5,  ARM::D6,  ARM::D7,
685      ARM::D8,  ARM::D9, ARM::D10, ARM::D11,
686     ARM::D12, ARM::D13, ARM::D14, ARM::D15,
687     ARM::D16, ARM::D17, ARM::D18, ARM::D19,
688     ARM::D20, ARM::D21, ARM::D22, ARM::D23,
689     ARM::D24, ARM::D25, ARM::D26, ARM::D27,
690     ARM::D28, ARM::D29, ARM::D30, ARM::D31
691 };
692
693 static DecodeStatus DecodeDPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
694                                    uint64_t Address, const void *Decoder) {
695   if (RegNo > 31)
696     return Fail;
697
698   unsigned Register = DPRDecoderTable[RegNo];
699   Inst.addOperand(MCOperand::CreateReg(Register));
700   return Success;
701 }
702
703 static DecodeStatus DecodeDPR_8RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
704                                    uint64_t Address, const void *Decoder) {
705   if (RegNo > 7)
706     return Fail;
707   return DecodeDPRRegisterClass(Inst, RegNo, Address, Decoder);
708 }
709
710 static DecodeStatus
711 DecodeDPR_VFP2RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
712                             uint64_t Address, const void *Decoder) {
713   if (RegNo > 15)
714     return Fail;
715   return DecodeDPRRegisterClass(Inst, RegNo, Address, Decoder);
716 }
717
718 static const unsigned QPRDecoderTable[] = {
719      ARM::Q0,  ARM::Q1,  ARM::Q2,  ARM::Q3,
720      ARM::Q4,  ARM::Q5,  ARM::Q6,  ARM::Q7,
721      ARM::Q8,  ARM::Q9, ARM::Q10, ARM::Q11,
722     ARM::Q12, ARM::Q13, ARM::Q14, ARM::Q15
723 };
724
725
726 static DecodeStatus DecodeQPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
727                                    uint64_t Address, const void *Decoder) {
728   if (RegNo > 31)
729     return Fail;
730   RegNo >>= 1;
731
732   unsigned Register = QPRDecoderTable[RegNo];
733   Inst.addOperand(MCOperand::CreateReg(Register));
734   return Success;
735 }
736
737 static DecodeStatus DecodePredicateOperand(llvm::MCInst &Inst, unsigned Val,
738                                uint64_t Address, const void *Decoder) {
739   if (Val == 0xF) return Fail;
740   // AL predicate is not allowed on Thumb1 branches.
741   if (Inst.getOpcode() == ARM::tBcc && Val == 0xE)
742     return Fail;
743   Inst.addOperand(MCOperand::CreateImm(Val));
744   if (Val == ARMCC::AL) {
745     Inst.addOperand(MCOperand::CreateReg(0));
746   } else
747     Inst.addOperand(MCOperand::CreateReg(ARM::CPSR));
748   return Success;
749 }
750
751 static DecodeStatus DecodeCCOutOperand(llvm::MCInst &Inst, unsigned Val,
752                                uint64_t Address, const void *Decoder) {
753   if (Val)
754     Inst.addOperand(MCOperand::CreateReg(ARM::CPSR));
755   else
756     Inst.addOperand(MCOperand::CreateReg(0));
757   return Success;
758 }
759
760 static DecodeStatus DecodeSOImmOperand(llvm::MCInst &Inst, unsigned Val,
761                                uint64_t Address, const void *Decoder) {
762   uint32_t imm = Val & 0xFF;
763   uint32_t rot = (Val & 0xF00) >> 7;
764   uint32_t rot_imm = (imm >> rot) | (imm << (32-rot));
765   Inst.addOperand(MCOperand::CreateImm(rot_imm));
766   return Success;
767 }
768
769 static DecodeStatus DecodeBLTargetOperand(llvm::MCInst &Inst, unsigned Val,
770                                uint64_t Address, const void *Decoder) {
771   Val <<= 2;
772   Inst.addOperand(MCOperand::CreateImm(SignExtend32<26>(Val)));
773   return Success;
774 }
775
776 static DecodeStatus DecodeSORegImmOperand(llvm::MCInst &Inst, unsigned Val,
777                                uint64_t Address, const void *Decoder) {
778   DecodeStatus S = Success;
779
780   unsigned Rm = fieldFromInstruction32(Val, 0, 4);
781   unsigned type = fieldFromInstruction32(Val, 5, 2);
782   unsigned imm = fieldFromInstruction32(Val, 7, 5);
783
784   // Register-immediate
785   CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
786
787   ARM_AM::ShiftOpc Shift = ARM_AM::lsl;
788   switch (type) {
789     case 0:
790       Shift = ARM_AM::lsl;
791       break;
792     case 1:
793       Shift = ARM_AM::lsr;
794       break;
795     case 2:
796       Shift = ARM_AM::asr;
797       break;
798     case 3:
799       Shift = ARM_AM::ror;
800       break;
801   }
802
803   if (Shift == ARM_AM::ror && imm == 0)
804     Shift = ARM_AM::rrx;
805
806   unsigned Op = Shift | (imm << 3);
807   Inst.addOperand(MCOperand::CreateImm(Op));
808
809   return S;
810 }
811
812 static DecodeStatus DecodeSORegRegOperand(llvm::MCInst &Inst, unsigned Val,
813                                uint64_t Address, const void *Decoder) {
814   DecodeStatus S = Success;
815
816   unsigned Rm = fieldFromInstruction32(Val, 0, 4);
817   unsigned type = fieldFromInstruction32(Val, 5, 2);
818   unsigned Rs = fieldFromInstruction32(Val, 8, 4);
819
820   // Register-register
821   CHECK(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder));
822   CHECK(S, DecodeGPRnopcRegisterClass(Inst, Rs, Address, Decoder));
823
824   ARM_AM::ShiftOpc Shift = ARM_AM::lsl;
825   switch (type) {
826     case 0:
827       Shift = ARM_AM::lsl;
828       break;
829     case 1:
830       Shift = ARM_AM::lsr;
831       break;
832     case 2:
833       Shift = ARM_AM::asr;
834       break;
835     case 3:
836       Shift = ARM_AM::ror;
837       break;
838   }
839
840   Inst.addOperand(MCOperand::CreateImm(Shift));
841
842   return S;
843 }
844
845 static DecodeStatus DecodeRegListOperand(llvm::MCInst &Inst, unsigned Val,
846                                  uint64_t Address, const void *Decoder) {
847   DecodeStatus S = Success;
848
849   // Empty register lists are not allowed.
850   if (CountPopulation_32(Val) == 0) return Fail;
851   for (unsigned i = 0; i < 16; ++i) {
852     if (Val & (1 << i)) {
853       CHECK(S, DecodeGPRRegisterClass(Inst, i, Address, Decoder));
854     }
855   }
856
857   return S;
858 }
859
860 static DecodeStatus DecodeSPRRegListOperand(llvm::MCInst &Inst, unsigned Val,
861                                  uint64_t Address, const void *Decoder) {
862   DecodeStatus S = Success;
863
864   unsigned Vd = fieldFromInstruction32(Val, 8, 4);
865   unsigned regs = Val & 0xFF;
866
867   CHECK(S, DecodeSPRRegisterClass(Inst, Vd, Address, Decoder));
868   for (unsigned i = 0; i < (regs - 1); ++i) {
869     CHECK(S, DecodeSPRRegisterClass(Inst, ++Vd, Address, Decoder));
870   }
871
872   return S;
873 }
874
875 static DecodeStatus DecodeDPRRegListOperand(llvm::MCInst &Inst, unsigned Val,
876                                  uint64_t Address, const void *Decoder) {
877   DecodeStatus S = Success;
878
879   unsigned Vd = fieldFromInstruction32(Val, 8, 4);
880   unsigned regs = (Val & 0xFF) / 2;
881
882   CHECK(S, DecodeDPRRegisterClass(Inst, Vd, Address, Decoder));
883   for (unsigned i = 0; i < (regs - 1); ++i) {
884     CHECK(S, DecodeDPRRegisterClass(Inst, ++Vd, Address, Decoder));
885   }
886
887   return S;
888 }
889
890 static DecodeStatus DecodeBitfieldMaskOperand(llvm::MCInst &Inst, unsigned Val,
891                                       uint64_t Address, const void *Decoder) {
892   // This operand encodes a mask of contiguous zeros between a specified MSB
893   // and LSB.  To decode it, we create the mask of all bits MSB-and-lower,
894   // the mask of all bits LSB-and-lower, and then xor them to create
895   // the mask of that's all ones on [msb, lsb].  Finally we not it to
896   // create the final mask.
897   unsigned msb = fieldFromInstruction32(Val, 5, 5);
898   unsigned lsb = fieldFromInstruction32(Val, 0, 5);
899   uint32_t msb_mask = (1 << (msb+1)) - 1;
900   uint32_t lsb_mask = (1 << lsb) - 1;
901   Inst.addOperand(MCOperand::CreateImm(~(msb_mask ^ lsb_mask)));
902   return Success;
903 }
904
905 static DecodeStatus DecodeCopMemInstruction(llvm::MCInst &Inst, unsigned Insn,
906                                   uint64_t Address, const void *Decoder) {
907   DecodeStatus S = Success;
908
909   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
910   unsigned CRd = fieldFromInstruction32(Insn, 12, 4);
911   unsigned coproc = fieldFromInstruction32(Insn, 8, 4);
912   unsigned imm = fieldFromInstruction32(Insn, 0, 8);
913   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
914   unsigned U = fieldFromInstruction32(Insn, 23, 1);
915
916   switch (Inst.getOpcode()) {
917     case ARM::LDC_OFFSET:
918     case ARM::LDC_PRE:
919     case ARM::LDC_POST:
920     case ARM::LDC_OPTION:
921     case ARM::LDCL_OFFSET:
922     case ARM::LDCL_PRE:
923     case ARM::LDCL_POST:
924     case ARM::LDCL_OPTION:
925     case ARM::STC_OFFSET:
926     case ARM::STC_PRE:
927     case ARM::STC_POST:
928     case ARM::STC_OPTION:
929     case ARM::STCL_OFFSET:
930     case ARM::STCL_PRE:
931     case ARM::STCL_POST:
932     case ARM::STCL_OPTION:
933       if (coproc == 0xA || coproc == 0xB)
934         return Fail;
935       break;
936     default:
937       break;
938   }
939
940   Inst.addOperand(MCOperand::CreateImm(coproc));
941   Inst.addOperand(MCOperand::CreateImm(CRd));
942   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
943   switch (Inst.getOpcode()) {
944     case ARM::LDC_OPTION:
945     case ARM::LDCL_OPTION:
946     case ARM::LDC2_OPTION:
947     case ARM::LDC2L_OPTION:
948     case ARM::STC_OPTION:
949     case ARM::STCL_OPTION:
950     case ARM::STC2_OPTION:
951     case ARM::STC2L_OPTION:
952     case ARM::LDCL_POST:
953     case ARM::STCL_POST:
954     case ARM::LDC2L_POST:
955     case ARM::STC2L_POST:
956       break;
957     default:
958       Inst.addOperand(MCOperand::CreateReg(0));
959       break;
960   }
961
962   unsigned P = fieldFromInstruction32(Insn, 24, 1);
963   unsigned W = fieldFromInstruction32(Insn, 21, 1);
964
965   bool writeback = (P == 0) || (W == 1);
966   unsigned idx_mode = 0;
967   if (P && writeback)
968     idx_mode = ARMII::IndexModePre;
969   else if (!P && writeback)
970     idx_mode = ARMII::IndexModePost;
971
972   switch (Inst.getOpcode()) {
973     case ARM::LDCL_POST:
974     case ARM::STCL_POST:
975     case ARM::LDC2L_POST:
976     case ARM::STC2L_POST:
977       imm |= U << 8;
978     case ARM::LDC_OPTION:
979     case ARM::LDCL_OPTION:
980     case ARM::LDC2_OPTION:
981     case ARM::LDC2L_OPTION:
982     case ARM::STC_OPTION:
983     case ARM::STCL_OPTION:
984     case ARM::STC2_OPTION:
985     case ARM::STC2L_OPTION:
986       Inst.addOperand(MCOperand::CreateImm(imm));
987       break;
988     default:
989       if (U)
990         Inst.addOperand(MCOperand::CreateImm(
991             ARM_AM::getAM2Opc(ARM_AM::add, imm, ARM_AM::lsl, idx_mode)));
992       else
993         Inst.addOperand(MCOperand::CreateImm(
994             ARM_AM::getAM2Opc(ARM_AM::sub, imm, ARM_AM::lsl, idx_mode)));
995       break;
996   }
997
998   switch (Inst.getOpcode()) {
999     case ARM::LDC_OFFSET:
1000     case ARM::LDC_PRE:
1001     case ARM::LDC_POST:
1002     case ARM::LDC_OPTION:
1003     case ARM::LDCL_OFFSET:
1004     case ARM::LDCL_PRE:
1005     case ARM::LDCL_POST:
1006     case ARM::LDCL_OPTION:
1007     case ARM::STC_OFFSET:
1008     case ARM::STC_PRE:
1009     case ARM::STC_POST:
1010     case ARM::STC_OPTION:
1011     case ARM::STCL_OFFSET:
1012     case ARM::STCL_PRE:
1013     case ARM::STCL_POST:
1014     case ARM::STCL_OPTION:
1015       CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
1016       break;
1017     default:
1018       break;
1019   }
1020
1021   return S;
1022 }
1023
1024 static DecodeStatus
1025 DecodeAddrMode2IdxInstruction(llvm::MCInst &Inst, unsigned Insn,
1026                               uint64_t Address, const void *Decoder) {
1027   DecodeStatus S = Success;
1028
1029   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1030   unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
1031   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1032   unsigned imm = fieldFromInstruction32(Insn, 0, 12);
1033   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
1034   unsigned reg = fieldFromInstruction32(Insn, 25, 1);
1035   unsigned P = fieldFromInstruction32(Insn, 24, 1);
1036   unsigned W = fieldFromInstruction32(Insn, 21, 1);
1037
1038   // On stores, the writeback operand precedes Rt.
1039   switch (Inst.getOpcode()) {
1040     case ARM::STR_POST_IMM:
1041     case ARM::STR_POST_REG:
1042     case ARM::STRB_POST_IMM:
1043     case ARM::STRB_POST_REG:
1044     case ARM::STRT_POST_REG:
1045     case ARM::STRT_POST_IMM:
1046     case ARM::STRBT_POST_REG:
1047     case ARM::STRBT_POST_IMM:
1048       CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
1049       break;
1050     default:
1051       break;
1052   }
1053
1054   CHECK(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder));
1055
1056   // On loads, the writeback operand comes after Rt.
1057   switch (Inst.getOpcode()) {
1058     case ARM::LDR_POST_IMM:
1059     case ARM::LDR_POST_REG:
1060     case ARM::LDRB_POST_IMM:
1061     case ARM::LDRB_POST_REG:
1062     case ARM::LDRBT_POST_REG:
1063     case ARM::LDRBT_POST_IMM:
1064     case ARM::LDRT_POST_REG:
1065     case ARM::LDRT_POST_IMM:
1066       CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
1067       break;
1068     default:
1069       break;
1070   }
1071
1072   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
1073
1074   ARM_AM::AddrOpc Op = ARM_AM::add;
1075   if (!fieldFromInstruction32(Insn, 23, 1))
1076     Op = ARM_AM::sub;
1077
1078   bool writeback = (P == 0) || (W == 1);
1079   unsigned idx_mode = 0;
1080   if (P && writeback)
1081     idx_mode = ARMII::IndexModePre;
1082   else if (!P && writeback)
1083     idx_mode = ARMII::IndexModePost;
1084
1085   if (writeback && (Rn == 15 || Rn == Rt)) S = Unpredictable; // UNPREDICTABLE
1086
1087   if (reg) {
1088     CHECK(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder));
1089     ARM_AM::ShiftOpc Opc = ARM_AM::lsl;
1090     switch( fieldFromInstruction32(Insn, 5, 2)) {
1091       case 0:
1092         Opc = ARM_AM::lsl;
1093         break;
1094       case 1:
1095         Opc = ARM_AM::lsr;
1096         break;
1097       case 2:
1098         Opc = ARM_AM::asr;
1099         break;
1100       case 3:
1101         Opc = ARM_AM::ror;
1102         break;
1103       default:
1104         return Fail;
1105     }
1106     unsigned amt = fieldFromInstruction32(Insn, 7, 5);
1107     unsigned imm = ARM_AM::getAM2Opc(Op, amt, Opc, idx_mode);
1108
1109     Inst.addOperand(MCOperand::CreateImm(imm));
1110   } else {
1111     Inst.addOperand(MCOperand::CreateReg(0));
1112     unsigned tmp = ARM_AM::getAM2Opc(Op, imm, ARM_AM::lsl, idx_mode);
1113     Inst.addOperand(MCOperand::CreateImm(tmp));
1114   }
1115
1116   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
1117
1118   return S;
1119 }
1120
1121 static DecodeStatus DecodeSORegMemOperand(llvm::MCInst &Inst, unsigned Val,
1122                                   uint64_t Address, const void *Decoder) {
1123   DecodeStatus S = Success;
1124
1125   unsigned Rn = fieldFromInstruction32(Val, 13, 4);
1126   unsigned Rm = fieldFromInstruction32(Val,  0, 4);
1127   unsigned type = fieldFromInstruction32(Val, 5, 2);
1128   unsigned imm = fieldFromInstruction32(Val, 7, 5);
1129   unsigned U = fieldFromInstruction32(Val, 12, 1);
1130
1131   ARM_AM::ShiftOpc ShOp = ARM_AM::lsl;
1132   switch (type) {
1133     case 0:
1134       ShOp = ARM_AM::lsl;
1135       break;
1136     case 1:
1137       ShOp = ARM_AM::lsr;
1138       break;
1139     case 2:
1140       ShOp = ARM_AM::asr;
1141       break;
1142     case 3:
1143       ShOp = ARM_AM::ror;
1144       break;
1145   }
1146
1147   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
1148   CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
1149   unsigned shift;
1150   if (U)
1151     shift = ARM_AM::getAM2Opc(ARM_AM::add, imm, ShOp);
1152   else
1153     shift = ARM_AM::getAM2Opc(ARM_AM::sub, imm, ShOp);
1154   Inst.addOperand(MCOperand::CreateImm(shift));
1155
1156   return S;
1157 }
1158
1159 static DecodeStatus
1160 DecodeAddrMode3Instruction(llvm::MCInst &Inst, unsigned Insn,
1161                            uint64_t Address, const void *Decoder) {
1162   DecodeStatus S = Success;
1163
1164   unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
1165   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1166   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1167   unsigned type = fieldFromInstruction32(Insn, 22, 1);
1168   unsigned imm = fieldFromInstruction32(Insn, 8, 4);
1169   unsigned U = ((~fieldFromInstruction32(Insn, 23, 1)) & 1) << 8;
1170   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
1171   unsigned W = fieldFromInstruction32(Insn, 21, 1);
1172   unsigned P = fieldFromInstruction32(Insn, 24, 1);
1173
1174   bool writeback = (W == 1) | (P == 0);
1175
1176   // For {LD,ST}RD, Rt must be even, else undefined.
1177   switch (Inst.getOpcode()) {
1178     case ARM::STRD:
1179     case ARM::STRD_PRE:
1180     case ARM::STRD_POST:
1181     case ARM::LDRD:
1182     case ARM::LDRD_PRE:
1183     case ARM::LDRD_POST:
1184       if (Rt & 0x1) return Fail;
1185       break;
1186   default:
1187     break;
1188   }
1189
1190   if (writeback) { // Writeback
1191     if (P)
1192       U |= ARMII::IndexModePre << 9;
1193     else
1194       U |= ARMII::IndexModePost << 9;
1195
1196     // On stores, the writeback operand precedes Rt.
1197     switch (Inst.getOpcode()) {
1198     case ARM::STRD:
1199     case ARM::STRD_PRE:
1200     case ARM::STRD_POST:
1201     case ARM::STRH:
1202     case ARM::STRH_PRE:
1203     case ARM::STRH_POST:
1204       CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
1205       break;
1206     default:
1207       break;
1208     }
1209   }
1210
1211   CHECK(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder));
1212   switch (Inst.getOpcode()) {
1213     case ARM::STRD:
1214     case ARM::STRD_PRE:
1215     case ARM::STRD_POST:
1216     case ARM::LDRD:
1217     case ARM::LDRD_PRE:
1218     case ARM::LDRD_POST:
1219       CHECK(S, DecodeGPRRegisterClass(Inst, Rt+1, Address, Decoder));
1220       break;
1221     default:
1222       break;
1223   }
1224
1225   if (writeback) {
1226     // On loads, the writeback operand comes after Rt.
1227     switch (Inst.getOpcode()) {
1228     case ARM::LDRD:
1229     case ARM::LDRD_PRE:
1230     case ARM::LDRD_POST:
1231     case ARM::LDRH:
1232     case ARM::LDRH_PRE:
1233     case ARM::LDRH_POST:
1234     case ARM::LDRSH:
1235     case ARM::LDRSH_PRE:
1236     case ARM::LDRSH_POST:
1237     case ARM::LDRSB:
1238     case ARM::LDRSB_PRE:
1239     case ARM::LDRSB_POST:
1240     case ARM::LDRHTr:
1241     case ARM::LDRSBTr:
1242       CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
1243       break;
1244     default:
1245       break;
1246     }
1247   }
1248
1249   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
1250
1251   if (type) {
1252     Inst.addOperand(MCOperand::CreateReg(0));
1253     Inst.addOperand(MCOperand::CreateImm(U | (imm << 4) | Rm));
1254   } else {
1255     CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
1256     Inst.addOperand(MCOperand::CreateImm(U));
1257   }
1258
1259   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
1260
1261   return S;
1262 }
1263
1264 static DecodeStatus DecodeRFEInstruction(llvm::MCInst &Inst, unsigned Insn,
1265                                  uint64_t Address, const void *Decoder) {
1266   DecodeStatus S = Success;
1267
1268   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1269   unsigned mode = fieldFromInstruction32(Insn, 23, 2);
1270
1271   switch (mode) {
1272     case 0:
1273       mode = ARM_AM::da;
1274       break;
1275     case 1:
1276       mode = ARM_AM::ia;
1277       break;
1278     case 2:
1279       mode = ARM_AM::db;
1280       break;
1281     case 3:
1282       mode = ARM_AM::ib;
1283       break;
1284   }
1285
1286   Inst.addOperand(MCOperand::CreateImm(mode));
1287   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
1288
1289   return S;
1290 }
1291
1292 static DecodeStatus DecodeMemMultipleWritebackInstruction(llvm::MCInst &Inst,
1293                                   unsigned Insn,
1294                                   uint64_t Address, const void *Decoder) {
1295   DecodeStatus S = Success;
1296
1297   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1298   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
1299   unsigned reglist = fieldFromInstruction32(Insn, 0, 16);
1300
1301   if (pred == 0xF) {
1302     switch (Inst.getOpcode()) {
1303       case ARM::LDMDA:
1304         Inst.setOpcode(ARM::RFEDA);
1305         break;
1306       case ARM::LDMDA_UPD:
1307         Inst.setOpcode(ARM::RFEDA_UPD);
1308         break;
1309       case ARM::LDMDB:
1310         Inst.setOpcode(ARM::RFEDB);
1311         break;
1312       case ARM::LDMDB_UPD:
1313         Inst.setOpcode(ARM::RFEDB_UPD);
1314         break;
1315       case ARM::LDMIA:
1316         Inst.setOpcode(ARM::RFEIA);
1317         break;
1318       case ARM::LDMIA_UPD:
1319         Inst.setOpcode(ARM::RFEIA_UPD);
1320         break;
1321       case ARM::LDMIB:
1322         Inst.setOpcode(ARM::RFEIB);
1323         break;
1324       case ARM::LDMIB_UPD:
1325         Inst.setOpcode(ARM::RFEIB_UPD);
1326         break;
1327       case ARM::STMDA:
1328         Inst.setOpcode(ARM::SRSDA);
1329         break;
1330       case ARM::STMDA_UPD:
1331         Inst.setOpcode(ARM::SRSDA_UPD);
1332         break;
1333       case ARM::STMDB:
1334         Inst.setOpcode(ARM::SRSDB);
1335         break;
1336       case ARM::STMDB_UPD:
1337         Inst.setOpcode(ARM::SRSDB_UPD);
1338         break;
1339       case ARM::STMIA:
1340         Inst.setOpcode(ARM::SRSIA);
1341         break;
1342       case ARM::STMIA_UPD:
1343         Inst.setOpcode(ARM::SRSIA_UPD);
1344         break;
1345       case ARM::STMIB:
1346         Inst.setOpcode(ARM::SRSIB);
1347         break;
1348       case ARM::STMIB_UPD:
1349         Inst.setOpcode(ARM::SRSIB_UPD);
1350         break;
1351       default:
1352         CHECK(S, Fail);
1353     }
1354
1355     // For stores (which become SRS's, the only operand is the mode.
1356     if (fieldFromInstruction32(Insn, 20, 1) == 0) {
1357       Inst.addOperand(
1358           MCOperand::CreateImm(fieldFromInstruction32(Insn, 0, 4)));
1359       return S;
1360     }
1361
1362     return DecodeRFEInstruction(Inst, Insn, Address, Decoder);
1363   }
1364
1365   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
1366   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)); // Tied
1367   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
1368   CHECK(S, DecodeRegListOperand(Inst, reglist, Address, Decoder));
1369
1370   return S;
1371 }
1372
1373 static DecodeStatus DecodeCPSInstruction(llvm::MCInst &Inst, unsigned Insn,
1374                                  uint64_t Address, const void *Decoder) {
1375   unsigned imod = fieldFromInstruction32(Insn, 18, 2);
1376   unsigned M = fieldFromInstruction32(Insn, 17, 1);
1377   unsigned iflags = fieldFromInstruction32(Insn, 6, 3);
1378   unsigned mode = fieldFromInstruction32(Insn, 0, 5);
1379
1380   DecodeStatus S = Success;
1381
1382   // imod == '01' --> UNPREDICTABLE
1383   // NOTE: Even though this is technically UNPREDICTABLE, we choose to
1384   // return failure here.  The '01' imod value is unprintable, so there's
1385   // nothing useful we could do even if we returned UNPREDICTABLE.
1386
1387   if (imod == 1) CHECK(S, Fail);
1388
1389   if (imod && M) {
1390     Inst.setOpcode(ARM::CPS3p);
1391     Inst.addOperand(MCOperand::CreateImm(imod));
1392     Inst.addOperand(MCOperand::CreateImm(iflags));
1393     Inst.addOperand(MCOperand::CreateImm(mode));
1394   } else if (imod && !M) {
1395     Inst.setOpcode(ARM::CPS2p);
1396     Inst.addOperand(MCOperand::CreateImm(imod));
1397     Inst.addOperand(MCOperand::CreateImm(iflags));
1398     if (mode) CHECK(S, Unpredictable);
1399   } else if (!imod && M) {
1400     Inst.setOpcode(ARM::CPS1p);
1401     Inst.addOperand(MCOperand::CreateImm(mode));
1402     if (iflags) CHECK(S, Unpredictable);
1403   } else {
1404     // imod == '00' && M == '0' --> UNPREDICTABLE
1405     Inst.setOpcode(ARM::CPS1p);
1406     Inst.addOperand(MCOperand::CreateImm(mode));
1407     CHECK(S, Unpredictable);
1408   }
1409
1410   return S;
1411 }
1412
1413 static DecodeStatus DecodeT2CPSInstruction(llvm::MCInst &Inst, unsigned Insn,
1414                                  uint64_t Address, const void *Decoder) {
1415   unsigned imod = fieldFromInstruction32(Insn, 9, 2);
1416   unsigned M = fieldFromInstruction32(Insn, 8, 1);
1417   unsigned iflags = fieldFromInstruction32(Insn, 5, 3);
1418   unsigned mode = fieldFromInstruction32(Insn, 0, 5);
1419
1420   DecodeStatus S = Success;
1421
1422   // imod == '01' --> UNPREDICTABLE
1423   // NOTE: Even though this is technically UNPREDICTABLE, we choose to
1424   // return failure here.  The '01' imod value is unprintable, so there's
1425   // nothing useful we could do even if we returned UNPREDICTABLE.
1426
1427   if (imod == 1) CHECK(S, Fail);
1428
1429   if (imod && M) {
1430     Inst.setOpcode(ARM::t2CPS3p);
1431     Inst.addOperand(MCOperand::CreateImm(imod));
1432     Inst.addOperand(MCOperand::CreateImm(iflags));
1433     Inst.addOperand(MCOperand::CreateImm(mode));
1434   } else if (imod && !M) {
1435     Inst.setOpcode(ARM::t2CPS2p);
1436     Inst.addOperand(MCOperand::CreateImm(imod));
1437     Inst.addOperand(MCOperand::CreateImm(iflags));
1438     if (mode) CHECK(S, Unpredictable);
1439   } else if (!imod && M) {
1440     Inst.setOpcode(ARM::t2CPS1p);
1441     Inst.addOperand(MCOperand::CreateImm(mode));
1442     if (iflags) CHECK(S, Unpredictable);
1443   } else {
1444     // imod == '00' && M == '0' --> UNPREDICTABLE
1445     Inst.setOpcode(ARM::t2CPS1p);
1446     Inst.addOperand(MCOperand::CreateImm(mode));
1447     CHECK(S, Unpredictable);
1448   }
1449
1450   return S;
1451 }
1452
1453
1454 static DecodeStatus DecodeSMLAInstruction(llvm::MCInst &Inst, unsigned Insn,
1455                                  uint64_t Address, const void *Decoder) {
1456   DecodeStatus S = Success;
1457
1458   unsigned Rd = fieldFromInstruction32(Insn, 16, 4);
1459   unsigned Rn = fieldFromInstruction32(Insn, 0, 4);
1460   unsigned Rm = fieldFromInstruction32(Insn, 8, 4);
1461   unsigned Ra = fieldFromInstruction32(Insn, 12, 4);
1462   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
1463
1464   if (pred == 0xF)
1465     return DecodeCPSInstruction(Inst, Insn, Address, Decoder);
1466
1467   CHECK(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder));
1468   CHECK(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder));
1469   CHECK(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder));
1470   CHECK(S, DecodeGPRnopcRegisterClass(Inst, Ra, Address, Decoder));
1471
1472   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
1473
1474   return S;
1475 }
1476
1477 static DecodeStatus DecodeAddrModeImm12Operand(llvm::MCInst &Inst, unsigned Val,
1478                            uint64_t Address, const void *Decoder) {
1479   DecodeStatus S = Success;
1480
1481   unsigned add = fieldFromInstruction32(Val, 12, 1);
1482   unsigned imm = fieldFromInstruction32(Val, 0, 12);
1483   unsigned Rn = fieldFromInstruction32(Val, 13, 4);
1484
1485   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
1486
1487   if (!add) imm *= -1;
1488   if (imm == 0 && !add) imm = INT32_MIN;
1489   Inst.addOperand(MCOperand::CreateImm(imm));
1490
1491   return S;
1492 }
1493
1494 static DecodeStatus DecodeAddrMode5Operand(llvm::MCInst &Inst, unsigned Val,
1495                                    uint64_t Address, const void *Decoder) {
1496   DecodeStatus S = Success;
1497
1498   unsigned Rn = fieldFromInstruction32(Val, 9, 4);
1499   unsigned U = fieldFromInstruction32(Val, 8, 1);
1500   unsigned imm = fieldFromInstruction32(Val, 0, 8);
1501
1502   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
1503
1504   if (U)
1505     Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add, imm)));
1506   else
1507     Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub, imm)));
1508
1509   return S;
1510 }
1511
1512 static DecodeStatus DecodeAddrMode7Operand(llvm::MCInst &Inst, unsigned Val,
1513                                    uint64_t Address, const void *Decoder) {
1514   return DecodeGPRRegisterClass(Inst, Val, Address, Decoder);
1515 }
1516
1517 static DecodeStatus
1518 DecodeBranchImmInstruction(llvm::MCInst &Inst, unsigned Insn,
1519                            uint64_t Address, const void *Decoder) {
1520   DecodeStatus S = Success;
1521
1522   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
1523   unsigned imm = fieldFromInstruction32(Insn, 0, 24) << 2;
1524
1525   if (pred == 0xF) {
1526     Inst.setOpcode(ARM::BLXi);
1527     imm |= fieldFromInstruction32(Insn, 24, 1) << 1;
1528     Inst.addOperand(MCOperand::CreateImm(SignExtend32<26>(imm)));
1529     return S;
1530   }
1531
1532   Inst.addOperand(MCOperand::CreateImm(SignExtend32<26>(imm)));
1533   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
1534
1535   return S;
1536 }
1537
1538
1539 static DecodeStatus DecodeVCVTImmOperand(llvm::MCInst &Inst, unsigned Val,
1540                                  uint64_t Address, const void *Decoder) {
1541   Inst.addOperand(MCOperand::CreateImm(64 - Val));
1542   return Success;
1543 }
1544
1545 static DecodeStatus DecodeAddrMode6Operand(llvm::MCInst &Inst, unsigned Val,
1546                                    uint64_t Address, const void *Decoder) {
1547   DecodeStatus S = Success;
1548
1549   unsigned Rm = fieldFromInstruction32(Val, 0, 4);
1550   unsigned align = fieldFromInstruction32(Val, 4, 2);
1551
1552   CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
1553   if (!align)
1554     Inst.addOperand(MCOperand::CreateImm(0));
1555   else
1556     Inst.addOperand(MCOperand::CreateImm(4 << align));
1557
1558   return S;
1559 }
1560
1561 static DecodeStatus DecodeVLDInstruction(llvm::MCInst &Inst, unsigned Insn,
1562                                    uint64_t Address, const void *Decoder) {
1563   DecodeStatus S = Success;
1564
1565   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1566   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1567   unsigned wb = fieldFromInstruction32(Insn, 16, 4);
1568   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1569   Rn |= fieldFromInstruction32(Insn, 4, 2) << 4;
1570   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1571
1572   // First output register
1573   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
1574
1575   // Second output register
1576   switch (Inst.getOpcode()) {
1577     case ARM::VLD1q8:
1578     case ARM::VLD1q16:
1579     case ARM::VLD1q32:
1580     case ARM::VLD1q64:
1581     case ARM::VLD1q8_UPD:
1582     case ARM::VLD1q16_UPD:
1583     case ARM::VLD1q32_UPD:
1584     case ARM::VLD1q64_UPD:
1585     case ARM::VLD1d8T:
1586     case ARM::VLD1d16T:
1587     case ARM::VLD1d32T:
1588     case ARM::VLD1d64T:
1589     case ARM::VLD1d8T_UPD:
1590     case ARM::VLD1d16T_UPD:
1591     case ARM::VLD1d32T_UPD:
1592     case ARM::VLD1d64T_UPD:
1593     case ARM::VLD1d8Q:
1594     case ARM::VLD1d16Q:
1595     case ARM::VLD1d32Q:
1596     case ARM::VLD1d64Q:
1597     case ARM::VLD1d8Q_UPD:
1598     case ARM::VLD1d16Q_UPD:
1599     case ARM::VLD1d32Q_UPD:
1600     case ARM::VLD1d64Q_UPD:
1601     case ARM::VLD2d8:
1602     case ARM::VLD2d16:
1603     case ARM::VLD2d32:
1604     case ARM::VLD2d8_UPD:
1605     case ARM::VLD2d16_UPD:
1606     case ARM::VLD2d32_UPD:
1607     case ARM::VLD2q8:
1608     case ARM::VLD2q16:
1609     case ARM::VLD2q32:
1610     case ARM::VLD2q8_UPD:
1611     case ARM::VLD2q16_UPD:
1612     case ARM::VLD2q32_UPD:
1613     case ARM::VLD3d8:
1614     case ARM::VLD3d16:
1615     case ARM::VLD3d32:
1616     case ARM::VLD3d8_UPD:
1617     case ARM::VLD3d16_UPD:
1618     case ARM::VLD3d32_UPD:
1619     case ARM::VLD4d8:
1620     case ARM::VLD4d16:
1621     case ARM::VLD4d32:
1622     case ARM::VLD4d8_UPD:
1623     case ARM::VLD4d16_UPD:
1624     case ARM::VLD4d32_UPD:
1625       CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder));
1626       break;
1627     case ARM::VLD2b8:
1628     case ARM::VLD2b16:
1629     case ARM::VLD2b32:
1630     case ARM::VLD2b8_UPD:
1631     case ARM::VLD2b16_UPD:
1632     case ARM::VLD2b32_UPD:
1633     case ARM::VLD3q8:
1634     case ARM::VLD3q16:
1635     case ARM::VLD3q32:
1636     case ARM::VLD3q8_UPD:
1637     case ARM::VLD3q16_UPD:
1638     case ARM::VLD3q32_UPD:
1639     case ARM::VLD4q8:
1640     case ARM::VLD4q16:
1641     case ARM::VLD4q32:
1642     case ARM::VLD4q8_UPD:
1643     case ARM::VLD4q16_UPD:
1644     case ARM::VLD4q32_UPD:
1645       CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder));
1646     default:
1647       break;
1648   }
1649
1650   // Third output register
1651   switch(Inst.getOpcode()) {
1652     case ARM::VLD1d8T:
1653     case ARM::VLD1d16T:
1654     case ARM::VLD1d32T:
1655     case ARM::VLD1d64T:
1656     case ARM::VLD1d8T_UPD:
1657     case ARM::VLD1d16T_UPD:
1658     case ARM::VLD1d32T_UPD:
1659     case ARM::VLD1d64T_UPD:
1660     case ARM::VLD1d8Q:
1661     case ARM::VLD1d16Q:
1662     case ARM::VLD1d32Q:
1663     case ARM::VLD1d64Q:
1664     case ARM::VLD1d8Q_UPD:
1665     case ARM::VLD1d16Q_UPD:
1666     case ARM::VLD1d32Q_UPD:
1667     case ARM::VLD1d64Q_UPD:
1668     case ARM::VLD2q8:
1669     case ARM::VLD2q16:
1670     case ARM::VLD2q32:
1671     case ARM::VLD2q8_UPD:
1672     case ARM::VLD2q16_UPD:
1673     case ARM::VLD2q32_UPD:
1674     case ARM::VLD3d8:
1675     case ARM::VLD3d16:
1676     case ARM::VLD3d32:
1677     case ARM::VLD3d8_UPD:
1678     case ARM::VLD3d16_UPD:
1679     case ARM::VLD3d32_UPD:
1680     case ARM::VLD4d8:
1681     case ARM::VLD4d16:
1682     case ARM::VLD4d32:
1683     case ARM::VLD4d8_UPD:
1684     case ARM::VLD4d16_UPD:
1685     case ARM::VLD4d32_UPD:
1686       CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder));
1687       break;
1688     case ARM::VLD3q8:
1689     case ARM::VLD3q16:
1690     case ARM::VLD3q32:
1691     case ARM::VLD3q8_UPD:
1692     case ARM::VLD3q16_UPD:
1693     case ARM::VLD3q32_UPD:
1694     case ARM::VLD4q8:
1695     case ARM::VLD4q16:
1696     case ARM::VLD4q32:
1697     case ARM::VLD4q8_UPD:
1698     case ARM::VLD4q16_UPD:
1699     case ARM::VLD4q32_UPD:
1700       CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+4)%32, Address, Decoder));
1701       break;
1702     default:
1703       break;
1704   }
1705
1706   // Fourth output register
1707   switch (Inst.getOpcode()) {
1708     case ARM::VLD1d8Q:
1709     case ARM::VLD1d16Q:
1710     case ARM::VLD1d32Q:
1711     case ARM::VLD1d64Q:
1712     case ARM::VLD1d8Q_UPD:
1713     case ARM::VLD1d16Q_UPD:
1714     case ARM::VLD1d32Q_UPD:
1715     case ARM::VLD1d64Q_UPD:
1716     case ARM::VLD2q8:
1717     case ARM::VLD2q16:
1718     case ARM::VLD2q32:
1719     case ARM::VLD2q8_UPD:
1720     case ARM::VLD2q16_UPD:
1721     case ARM::VLD2q32_UPD:
1722     case ARM::VLD4d8:
1723     case ARM::VLD4d16:
1724     case ARM::VLD4d32:
1725     case ARM::VLD4d8_UPD:
1726     case ARM::VLD4d16_UPD:
1727     case ARM::VLD4d32_UPD:
1728       CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+3)%32, Address, Decoder));
1729       break;
1730     case ARM::VLD4q8:
1731     case ARM::VLD4q16:
1732     case ARM::VLD4q32:
1733     case ARM::VLD4q8_UPD:
1734     case ARM::VLD4q16_UPD:
1735     case ARM::VLD4q32_UPD:
1736       CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+6)%32, Address, Decoder));
1737       break;
1738     default:
1739       break;
1740   }
1741
1742   // Writeback operand
1743   switch (Inst.getOpcode()) {
1744     case ARM::VLD1d8_UPD:
1745     case ARM::VLD1d16_UPD:
1746     case ARM::VLD1d32_UPD:
1747     case ARM::VLD1d64_UPD:
1748     case ARM::VLD1q8_UPD:
1749     case ARM::VLD1q16_UPD:
1750     case ARM::VLD1q32_UPD:
1751     case ARM::VLD1q64_UPD:
1752     case ARM::VLD1d8T_UPD:
1753     case ARM::VLD1d16T_UPD:
1754     case ARM::VLD1d32T_UPD:
1755     case ARM::VLD1d64T_UPD:
1756     case ARM::VLD1d8Q_UPD:
1757     case ARM::VLD1d16Q_UPD:
1758     case ARM::VLD1d32Q_UPD:
1759     case ARM::VLD1d64Q_UPD:
1760     case ARM::VLD2d8_UPD:
1761     case ARM::VLD2d16_UPD:
1762     case ARM::VLD2d32_UPD:
1763     case ARM::VLD2q8_UPD:
1764     case ARM::VLD2q16_UPD:
1765     case ARM::VLD2q32_UPD:
1766     case ARM::VLD2b8_UPD:
1767     case ARM::VLD2b16_UPD:
1768     case ARM::VLD2b32_UPD:
1769     case ARM::VLD3d8_UPD:
1770     case ARM::VLD3d16_UPD:
1771     case ARM::VLD3d32_UPD:
1772     case ARM::VLD3q8_UPD:
1773     case ARM::VLD3q16_UPD:
1774     case ARM::VLD3q32_UPD:
1775     case ARM::VLD4d8_UPD:
1776     case ARM::VLD4d16_UPD:
1777     case ARM::VLD4d32_UPD:
1778     case ARM::VLD4q8_UPD:
1779     case ARM::VLD4q16_UPD:
1780     case ARM::VLD4q32_UPD:
1781       CHECK(S, DecodeGPRRegisterClass(Inst, wb, Address, Decoder));
1782       break;
1783     default:
1784       break;
1785   }
1786
1787   // AddrMode6 Base (register+alignment)
1788   CHECK(S, DecodeAddrMode6Operand(Inst, Rn, Address, Decoder));
1789
1790   // AddrMode6 Offset (register)
1791   if (Rm == 0xD)
1792     Inst.addOperand(MCOperand::CreateReg(0));
1793   else if (Rm != 0xF) {
1794     CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
1795   }
1796
1797   return S;
1798 }
1799
1800 static DecodeStatus DecodeVSTInstruction(llvm::MCInst &Inst, unsigned Insn,
1801                                  uint64_t Address, const void *Decoder) {
1802   DecodeStatus S = Success;
1803
1804   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1805   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1806   unsigned wb = fieldFromInstruction32(Insn, 16, 4);
1807   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1808   Rn |= fieldFromInstruction32(Insn, 4, 2) << 4;
1809   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1810
1811   // Writeback Operand
1812   switch (Inst.getOpcode()) {
1813     case ARM::VST1d8_UPD:
1814     case ARM::VST1d16_UPD:
1815     case ARM::VST1d32_UPD:
1816     case ARM::VST1d64_UPD:
1817     case ARM::VST1q8_UPD:
1818     case ARM::VST1q16_UPD:
1819     case ARM::VST1q32_UPD:
1820     case ARM::VST1q64_UPD:
1821     case ARM::VST1d8T_UPD:
1822     case ARM::VST1d16T_UPD:
1823     case ARM::VST1d32T_UPD:
1824     case ARM::VST1d64T_UPD:
1825     case ARM::VST1d8Q_UPD:
1826     case ARM::VST1d16Q_UPD:
1827     case ARM::VST1d32Q_UPD:
1828     case ARM::VST1d64Q_UPD:
1829     case ARM::VST2d8_UPD:
1830     case ARM::VST2d16_UPD:
1831     case ARM::VST2d32_UPD:
1832     case ARM::VST2q8_UPD:
1833     case ARM::VST2q16_UPD:
1834     case ARM::VST2q32_UPD:
1835     case ARM::VST2b8_UPD:
1836     case ARM::VST2b16_UPD:
1837     case ARM::VST2b32_UPD:
1838     case ARM::VST3d8_UPD:
1839     case ARM::VST3d16_UPD:
1840     case ARM::VST3d32_UPD:
1841     case ARM::VST3q8_UPD:
1842     case ARM::VST3q16_UPD:
1843     case ARM::VST3q32_UPD:
1844     case ARM::VST4d8_UPD:
1845     case ARM::VST4d16_UPD:
1846     case ARM::VST4d32_UPD:
1847     case ARM::VST4q8_UPD:
1848     case ARM::VST4q16_UPD:
1849     case ARM::VST4q32_UPD:
1850       CHECK(S, DecodeGPRRegisterClass(Inst, wb, Address, Decoder));
1851       break;
1852     default:
1853       break;
1854   }
1855
1856   // AddrMode6 Base (register+alignment)
1857   CHECK(S, DecodeAddrMode6Operand(Inst, Rn, Address, Decoder));
1858
1859   // AddrMode6 Offset (register)
1860   if (Rm == 0xD)
1861     Inst.addOperand(MCOperand::CreateReg(0));
1862   else if (Rm != 0xF) {
1863     CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
1864   }
1865
1866   // First input register
1867   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
1868
1869   // Second input register
1870   switch (Inst.getOpcode()) {
1871     case ARM::VST1q8:
1872     case ARM::VST1q16:
1873     case ARM::VST1q32:
1874     case ARM::VST1q64:
1875     case ARM::VST1q8_UPD:
1876     case ARM::VST1q16_UPD:
1877     case ARM::VST1q32_UPD:
1878     case ARM::VST1q64_UPD:
1879     case ARM::VST1d8T:
1880     case ARM::VST1d16T:
1881     case ARM::VST1d32T:
1882     case ARM::VST1d64T:
1883     case ARM::VST1d8T_UPD:
1884     case ARM::VST1d16T_UPD:
1885     case ARM::VST1d32T_UPD:
1886     case ARM::VST1d64T_UPD:
1887     case ARM::VST1d8Q:
1888     case ARM::VST1d16Q:
1889     case ARM::VST1d32Q:
1890     case ARM::VST1d64Q:
1891     case ARM::VST1d8Q_UPD:
1892     case ARM::VST1d16Q_UPD:
1893     case ARM::VST1d32Q_UPD:
1894     case ARM::VST1d64Q_UPD:
1895     case ARM::VST2d8:
1896     case ARM::VST2d16:
1897     case ARM::VST2d32:
1898     case ARM::VST2d8_UPD:
1899     case ARM::VST2d16_UPD:
1900     case ARM::VST2d32_UPD:
1901     case ARM::VST2q8:
1902     case ARM::VST2q16:
1903     case ARM::VST2q32:
1904     case ARM::VST2q8_UPD:
1905     case ARM::VST2q16_UPD:
1906     case ARM::VST2q32_UPD:
1907     case ARM::VST3d8:
1908     case ARM::VST3d16:
1909     case ARM::VST3d32:
1910     case ARM::VST3d8_UPD:
1911     case ARM::VST3d16_UPD:
1912     case ARM::VST3d32_UPD:
1913     case ARM::VST4d8:
1914     case ARM::VST4d16:
1915     case ARM::VST4d32:
1916     case ARM::VST4d8_UPD:
1917     case ARM::VST4d16_UPD:
1918     case ARM::VST4d32_UPD:
1919       CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder));
1920       break;
1921     case ARM::VST2b8:
1922     case ARM::VST2b16:
1923     case ARM::VST2b32:
1924     case ARM::VST2b8_UPD:
1925     case ARM::VST2b16_UPD:
1926     case ARM::VST2b32_UPD:
1927     case ARM::VST3q8:
1928     case ARM::VST3q16:
1929     case ARM::VST3q32:
1930     case ARM::VST3q8_UPD:
1931     case ARM::VST3q16_UPD:
1932     case ARM::VST3q32_UPD:
1933     case ARM::VST4q8:
1934     case ARM::VST4q16:
1935     case ARM::VST4q32:
1936     case ARM::VST4q8_UPD:
1937     case ARM::VST4q16_UPD:
1938     case ARM::VST4q32_UPD:
1939       CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder));
1940       break;
1941     default:
1942       break;
1943   }
1944
1945   // Third input register
1946   switch (Inst.getOpcode()) {
1947     case ARM::VST1d8T:
1948     case ARM::VST1d16T:
1949     case ARM::VST1d32T:
1950     case ARM::VST1d64T:
1951     case ARM::VST1d8T_UPD:
1952     case ARM::VST1d16T_UPD:
1953     case ARM::VST1d32T_UPD:
1954     case ARM::VST1d64T_UPD:
1955     case ARM::VST1d8Q:
1956     case ARM::VST1d16Q:
1957     case ARM::VST1d32Q:
1958     case ARM::VST1d64Q:
1959     case ARM::VST1d8Q_UPD:
1960     case ARM::VST1d16Q_UPD:
1961     case ARM::VST1d32Q_UPD:
1962     case ARM::VST1d64Q_UPD:
1963     case ARM::VST2q8:
1964     case ARM::VST2q16:
1965     case ARM::VST2q32:
1966     case ARM::VST2q8_UPD:
1967     case ARM::VST2q16_UPD:
1968     case ARM::VST2q32_UPD:
1969     case ARM::VST3d8:
1970     case ARM::VST3d16:
1971     case ARM::VST3d32:
1972     case ARM::VST3d8_UPD:
1973     case ARM::VST3d16_UPD:
1974     case ARM::VST3d32_UPD:
1975     case ARM::VST4d8:
1976     case ARM::VST4d16:
1977     case ARM::VST4d32:
1978     case ARM::VST4d8_UPD:
1979     case ARM::VST4d16_UPD:
1980     case ARM::VST4d32_UPD:
1981       CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder));
1982       break;
1983     case ARM::VST3q8:
1984     case ARM::VST3q16:
1985     case ARM::VST3q32:
1986     case ARM::VST3q8_UPD:
1987     case ARM::VST3q16_UPD:
1988     case ARM::VST3q32_UPD:
1989     case ARM::VST4q8:
1990     case ARM::VST4q16:
1991     case ARM::VST4q32:
1992     case ARM::VST4q8_UPD:
1993     case ARM::VST4q16_UPD:
1994     case ARM::VST4q32_UPD:
1995       CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+4)%32, Address, Decoder));
1996       break;
1997     default:
1998       break;
1999   }
2000
2001   // Fourth input register
2002   switch (Inst.getOpcode()) {
2003     case ARM::VST1d8Q:
2004     case ARM::VST1d16Q:
2005     case ARM::VST1d32Q:
2006     case ARM::VST1d64Q:
2007     case ARM::VST1d8Q_UPD:
2008     case ARM::VST1d16Q_UPD:
2009     case ARM::VST1d32Q_UPD:
2010     case ARM::VST1d64Q_UPD:
2011     case ARM::VST2q8:
2012     case ARM::VST2q16:
2013     case ARM::VST2q32:
2014     case ARM::VST2q8_UPD:
2015     case ARM::VST2q16_UPD:
2016     case ARM::VST2q32_UPD:
2017     case ARM::VST4d8:
2018     case ARM::VST4d16:
2019     case ARM::VST4d32:
2020     case ARM::VST4d8_UPD:
2021     case ARM::VST4d16_UPD:
2022     case ARM::VST4d32_UPD:
2023       CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+3)%32, Address, Decoder));
2024       break;
2025     case ARM::VST4q8:
2026     case ARM::VST4q16:
2027     case ARM::VST4q32:
2028     case ARM::VST4q8_UPD:
2029     case ARM::VST4q16_UPD:
2030     case ARM::VST4q32_UPD:
2031       CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+6)%32, Address, Decoder));
2032       break;
2033     default:
2034       break;
2035   }
2036
2037   return S;
2038 }
2039
2040 static DecodeStatus DecodeVLD1DupInstruction(llvm::MCInst &Inst, unsigned Insn,
2041                                     uint64_t Address, const void *Decoder) {
2042   DecodeStatus S = Success;
2043
2044   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
2045   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
2046   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2047   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
2048   unsigned align = fieldFromInstruction32(Insn, 4, 1);
2049   unsigned size = fieldFromInstruction32(Insn, 6, 2);
2050   unsigned regs = fieldFromInstruction32(Insn, 5, 1) + 1;
2051
2052   align *= (1 << size);
2053
2054   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
2055   if (regs == 2) {
2056     CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder));
2057   }
2058   if (Rm != 0xF) {
2059     CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2060   }
2061
2062   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2063   Inst.addOperand(MCOperand::CreateImm(align));
2064
2065   if (Rm == 0xD)
2066     Inst.addOperand(MCOperand::CreateReg(0));
2067   else if (Rm != 0xF) {
2068     CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
2069   }
2070
2071   return S;
2072 }
2073
2074 static DecodeStatus DecodeVLD2DupInstruction(llvm::MCInst &Inst, unsigned Insn,
2075                                     uint64_t Address, const void *Decoder) {
2076   DecodeStatus S = Success;
2077
2078   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
2079   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
2080   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2081   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
2082   unsigned align = fieldFromInstruction32(Insn, 4, 1);
2083   unsigned size = 1 << fieldFromInstruction32(Insn, 6, 2);
2084   unsigned inc = fieldFromInstruction32(Insn, 5, 1) + 1;
2085   align *= 2*size;
2086
2087   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
2088   CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder));
2089   if (Rm != 0xF) {
2090     CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2091   }
2092
2093   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2094   Inst.addOperand(MCOperand::CreateImm(align));
2095
2096   if (Rm == 0xD)
2097     Inst.addOperand(MCOperand::CreateReg(0));
2098   else if (Rm != 0xF) {
2099     CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
2100   }
2101
2102   return S;
2103 }
2104
2105 static DecodeStatus DecodeVLD3DupInstruction(llvm::MCInst &Inst, unsigned Insn,
2106                                     uint64_t Address, const void *Decoder) {
2107   DecodeStatus S = Success;
2108
2109   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
2110   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
2111   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2112   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
2113   unsigned inc = fieldFromInstruction32(Insn, 5, 1) + 1;
2114
2115   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
2116   CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder));
2117   CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+2*inc)%32, Address, Decoder));
2118   if (Rm != 0xF) {
2119     CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2120   }
2121
2122   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2123   Inst.addOperand(MCOperand::CreateImm(0));
2124
2125   if (Rm == 0xD)
2126     Inst.addOperand(MCOperand::CreateReg(0));
2127   else if (Rm != 0xF) {
2128     CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
2129   }
2130
2131   return S;
2132 }
2133
2134 static DecodeStatus DecodeVLD4DupInstruction(llvm::MCInst &Inst, unsigned Insn,
2135                                     uint64_t Address, const void *Decoder) {
2136   DecodeStatus S = Success;
2137
2138   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
2139   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
2140   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2141   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
2142   unsigned size = fieldFromInstruction32(Insn, 6, 2);
2143   unsigned inc = fieldFromInstruction32(Insn, 5, 1) + 1;
2144   unsigned align = fieldFromInstruction32(Insn, 4, 1);
2145
2146   if (size == 0x3) {
2147     size = 4;
2148     align = 16;
2149   } else {
2150     if (size == 2) {
2151       size = 1 << size;
2152       align *= 8;
2153     } else {
2154       size = 1 << size;
2155       align *= 4*size;
2156     }
2157   }
2158
2159   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
2160   CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder));
2161   CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+2*inc)%32, Address, Decoder));
2162   CHECK(S, DecodeDPRRegisterClass(Inst, (Rd+3*inc)%32, Address, Decoder));
2163   if (Rm != 0xF) {
2164     CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2165   }
2166
2167   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2168   Inst.addOperand(MCOperand::CreateImm(align));
2169
2170   if (Rm == 0xD)
2171     Inst.addOperand(MCOperand::CreateReg(0));
2172   else if (Rm != 0xF) {
2173     CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
2174   }
2175
2176   return S;
2177 }
2178
2179 static DecodeStatus
2180 DecodeNEONModImmInstruction(llvm::MCInst &Inst, unsigned Insn,
2181                             uint64_t Address, const void *Decoder) {
2182   DecodeStatus S = Success;
2183
2184   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
2185   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
2186   unsigned imm = fieldFromInstruction32(Insn, 0, 4);
2187   imm |= fieldFromInstruction32(Insn, 16, 3) << 4;
2188   imm |= fieldFromInstruction32(Insn, 24, 1) << 7;
2189   imm |= fieldFromInstruction32(Insn, 8, 4) << 8;
2190   imm |= fieldFromInstruction32(Insn, 5, 1) << 12;
2191   unsigned Q = fieldFromInstruction32(Insn, 6, 1);
2192
2193   if (Q) {
2194     CHECK(S, DecodeQPRRegisterClass(Inst, Rd, Address, Decoder));
2195   } else {
2196     CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
2197   }
2198
2199   Inst.addOperand(MCOperand::CreateImm(imm));
2200
2201   switch (Inst.getOpcode()) {
2202     case ARM::VORRiv4i16:
2203     case ARM::VORRiv2i32:
2204     case ARM::VBICiv4i16:
2205     case ARM::VBICiv2i32:
2206       CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
2207       break;
2208     case ARM::VORRiv8i16:
2209     case ARM::VORRiv4i32:
2210     case ARM::VBICiv8i16:
2211     case ARM::VBICiv4i32:
2212       CHECK(S, DecodeQPRRegisterClass(Inst, Rd, Address, Decoder));
2213       break;
2214     default:
2215       break;
2216   }
2217
2218   return S;
2219 }
2220
2221 static DecodeStatus DecodeVSHLMaxInstruction(llvm::MCInst &Inst, unsigned Insn,
2222                                         uint64_t Address, const void *Decoder) {
2223   DecodeStatus S = Success;
2224
2225   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
2226   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
2227   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
2228   Rm |= fieldFromInstruction32(Insn, 5, 1) << 4;
2229   unsigned size = fieldFromInstruction32(Insn, 18, 2);
2230
2231   CHECK(S, DecodeQPRRegisterClass(Inst, Rd, Address, Decoder));
2232   CHECK(S, DecodeDPRRegisterClass(Inst, Rm, Address, Decoder));
2233   Inst.addOperand(MCOperand::CreateImm(8 << size));
2234
2235   return S;
2236 }
2237
2238 static DecodeStatus DecodeShiftRight8Imm(llvm::MCInst &Inst, unsigned Val,
2239                                uint64_t Address, const void *Decoder) {
2240   Inst.addOperand(MCOperand::CreateImm(8 - Val));
2241   return Success;
2242 }
2243
2244 static DecodeStatus DecodeShiftRight16Imm(llvm::MCInst &Inst, unsigned Val,
2245                                uint64_t Address, const void *Decoder) {
2246   Inst.addOperand(MCOperand::CreateImm(16 - Val));
2247   return Success;
2248 }
2249
2250 static DecodeStatus DecodeShiftRight32Imm(llvm::MCInst &Inst, unsigned Val,
2251                                uint64_t Address, const void *Decoder) {
2252   Inst.addOperand(MCOperand::CreateImm(32 - Val));
2253   return Success;
2254 }
2255
2256 static DecodeStatus DecodeShiftRight64Imm(llvm::MCInst &Inst, unsigned Val,
2257                                uint64_t Address, const void *Decoder) {
2258   Inst.addOperand(MCOperand::CreateImm(64 - Val));
2259   return Success;
2260 }
2261
2262 static DecodeStatus DecodeTBLInstruction(llvm::MCInst &Inst, unsigned Insn,
2263                                uint64_t Address, const void *Decoder) {
2264   DecodeStatus S = Success;
2265
2266   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
2267   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
2268   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2269   Rn |= fieldFromInstruction32(Insn, 7, 1) << 4;
2270   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
2271   Rm |= fieldFromInstruction32(Insn, 5, 1) << 4;
2272   unsigned op = fieldFromInstruction32(Insn, 6, 1);
2273   unsigned length = fieldFromInstruction32(Insn, 8, 2) + 1;
2274
2275   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
2276   if (op) {
2277     CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)); // Writeback
2278   }
2279
2280   for (unsigned i = 0; i < length; ++i) {
2281     CHECK(S, DecodeDPRRegisterClass(Inst, (Rn+i)%32, Address, Decoder));
2282   }
2283
2284   CHECK(S, DecodeDPRRegisterClass(Inst, Rm, Address, Decoder));
2285
2286   return S;
2287 }
2288
2289 static DecodeStatus DecodeVFPfpImm(llvm::MCInst &Inst, unsigned Val,
2290                             uint64_t Address, const void *Decoder) {
2291   // The immediate needs to be a fully instantiated float.  However, the
2292   // auto-generated decoder is only able to fill in some of the bits
2293   // necessary.  For instance, the 'b' bit is replicated multiple times,
2294   // and is even present in inverted form in one bit.  We do a little
2295   // binary parsing here to fill in those missing bits, and then
2296   // reinterpret it all as a float.
2297   union {
2298     uint32_t integer;
2299     float fp;
2300   } fp_conv;
2301
2302   fp_conv.integer = Val;
2303   uint32_t b = fieldFromInstruction32(Val, 25, 1);
2304   fp_conv.integer |= b << 26;
2305   fp_conv.integer |= b << 27;
2306   fp_conv.integer |= b << 28;
2307   fp_conv.integer |= b << 29;
2308   fp_conv.integer |= (~b & 0x1) << 30;
2309
2310   Inst.addOperand(MCOperand::CreateFPImm(fp_conv.fp));
2311   return Success;
2312 }
2313
2314 static DecodeStatus DecodeThumbAddSpecialReg(llvm::MCInst &Inst, uint16_t Insn,
2315                                      uint64_t Address, const void *Decoder) {
2316   DecodeStatus S = Success;
2317
2318   unsigned dst = fieldFromInstruction16(Insn, 8, 3);
2319   unsigned imm = fieldFromInstruction16(Insn, 0, 8);
2320
2321   CHECK(S, DecodetGPRRegisterClass(Inst, dst, Address, Decoder));
2322
2323   switch(Inst.getOpcode()) {
2324     default:
2325       return Fail;
2326     case ARM::tADR:
2327       break; // tADR does not explicitly represent the PC as an oeprand.
2328     case ARM::tADDrSPi:
2329       Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2330       break;
2331   }
2332
2333   Inst.addOperand(MCOperand::CreateImm(imm));
2334   return S;
2335 }
2336
2337 static DecodeStatus DecodeThumbBROperand(llvm::MCInst &Inst, unsigned Val,
2338                                  uint64_t Address, const void *Decoder) {
2339   Inst.addOperand(MCOperand::CreateImm(SignExtend32<12>(Val << 1)));
2340   return Success;
2341 }
2342
2343 static DecodeStatus DecodeT2BROperand(llvm::MCInst &Inst, unsigned Val,
2344                                  uint64_t Address, const void *Decoder) {
2345   Inst.addOperand(MCOperand::CreateImm(SignExtend32<21>(Val)));
2346   return Success;
2347 }
2348
2349 static DecodeStatus DecodeThumbCmpBROperand(llvm::MCInst &Inst, unsigned Val,
2350                                  uint64_t Address, const void *Decoder) {
2351   Inst.addOperand(MCOperand::CreateImm(SignExtend32<7>(Val << 1)));
2352   return Success;
2353 }
2354
2355 static DecodeStatus DecodeThumbAddrModeRR(llvm::MCInst &Inst, unsigned Val,
2356                                  uint64_t Address, const void *Decoder) {
2357   DecodeStatus S = Success;
2358
2359   unsigned Rn = fieldFromInstruction32(Val, 0, 3);
2360   unsigned Rm = fieldFromInstruction32(Val, 3, 3);
2361
2362   CHECK(S, DecodetGPRRegisterClass(Inst, Rn, Address, Decoder));
2363   CHECK(S, DecodetGPRRegisterClass(Inst, Rm, Address, Decoder));
2364
2365   return S;
2366 }
2367
2368 static DecodeStatus DecodeThumbAddrModeIS(llvm::MCInst &Inst, unsigned Val,
2369                                   uint64_t Address, const void *Decoder) {
2370   DecodeStatus S = Success;
2371
2372   unsigned Rn = fieldFromInstruction32(Val, 0, 3);
2373   unsigned imm = fieldFromInstruction32(Val, 3, 5);
2374
2375   CHECK(S, DecodetGPRRegisterClass(Inst, Rn, Address, Decoder));
2376   Inst.addOperand(MCOperand::CreateImm(imm));
2377
2378   return S;
2379 }
2380
2381 static DecodeStatus DecodeThumbAddrModePC(llvm::MCInst &Inst, unsigned Val,
2382                                   uint64_t Address, const void *Decoder) {
2383   Inst.addOperand(MCOperand::CreateImm(Val << 2));
2384
2385   return Success;
2386 }
2387
2388 static DecodeStatus DecodeThumbAddrModeSP(llvm::MCInst &Inst, unsigned Val,
2389                                   uint64_t Address, const void *Decoder) {
2390   Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2391   Inst.addOperand(MCOperand::CreateImm(Val));
2392
2393   return Success;
2394 }
2395
2396 static DecodeStatus DecodeT2AddrModeSOReg(llvm::MCInst &Inst, unsigned Val,
2397                                   uint64_t Address, const void *Decoder) {
2398   DecodeStatus S = Success;
2399
2400   unsigned Rn = fieldFromInstruction32(Val, 6, 4);
2401   unsigned Rm = fieldFromInstruction32(Val, 2, 4);
2402   unsigned imm = fieldFromInstruction32(Val, 0, 2);
2403
2404   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2405   CHECK(S, DecoderGPRRegisterClass(Inst, Rm, Address, Decoder));
2406   Inst.addOperand(MCOperand::CreateImm(imm));
2407
2408   return S;
2409 }
2410
2411 static DecodeStatus DecodeT2LoadShift(llvm::MCInst &Inst, unsigned Insn,
2412                               uint64_t Address, const void *Decoder) {
2413   DecodeStatus S = Success;
2414
2415   switch (Inst.getOpcode()) {
2416     case ARM::t2PLDs:
2417     case ARM::t2PLDWs:
2418     case ARM::t2PLIs:
2419       break;
2420     default: {
2421       unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
2422       CHECK(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder));
2423     }
2424   }
2425
2426   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2427   if (Rn == 0xF) {
2428     switch (Inst.getOpcode()) {
2429       case ARM::t2LDRBs:
2430         Inst.setOpcode(ARM::t2LDRBpci);
2431         break;
2432       case ARM::t2LDRHs:
2433         Inst.setOpcode(ARM::t2LDRHpci);
2434         break;
2435       case ARM::t2LDRSHs:
2436         Inst.setOpcode(ARM::t2LDRSHpci);
2437         break;
2438       case ARM::t2LDRSBs:
2439         Inst.setOpcode(ARM::t2LDRSBpci);
2440         break;
2441       case ARM::t2PLDs:
2442         Inst.setOpcode(ARM::t2PLDi12);
2443         Inst.addOperand(MCOperand::CreateReg(ARM::PC));
2444         break;
2445       default:
2446         return Fail;
2447     }
2448
2449     int imm = fieldFromInstruction32(Insn, 0, 12);
2450     if (!fieldFromInstruction32(Insn, 23, 1)) imm *= -1;
2451     Inst.addOperand(MCOperand::CreateImm(imm));
2452
2453     return S;
2454   }
2455
2456   unsigned addrmode = fieldFromInstruction32(Insn, 4, 2);
2457   addrmode |= fieldFromInstruction32(Insn, 0, 4) << 2;
2458   addrmode |= fieldFromInstruction32(Insn, 16, 4) << 6;
2459   CHECK(S, DecodeT2AddrModeSOReg(Inst, addrmode, Address, Decoder));
2460
2461   return S;
2462 }
2463
2464 static DecodeStatus DecodeT2Imm8S4(llvm::MCInst &Inst, unsigned Val,
2465                            uint64_t Address, const void *Decoder) {
2466   int imm = Val & 0xFF;
2467   if (!(Val & 0x100)) imm *= -1;
2468   Inst.addOperand(MCOperand::CreateImm(imm << 2));
2469
2470   return Success;
2471 }
2472
2473 static DecodeStatus DecodeT2AddrModeImm8s4(llvm::MCInst &Inst, unsigned Val,
2474                                    uint64_t Address, const void *Decoder) {
2475   DecodeStatus S = Success;
2476
2477   unsigned Rn = fieldFromInstruction32(Val, 9, 4);
2478   unsigned imm = fieldFromInstruction32(Val, 0, 9);
2479
2480   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2481   CHECK(S, DecodeT2Imm8S4(Inst, imm, Address, Decoder));
2482
2483   return S;
2484 }
2485
2486 static DecodeStatus DecodeT2Imm8(llvm::MCInst &Inst, unsigned Val,
2487                          uint64_t Address, const void *Decoder) {
2488   int imm = Val & 0xFF;
2489   if (!(Val & 0x100)) imm *= -1;
2490   Inst.addOperand(MCOperand::CreateImm(imm));
2491
2492   return Success;
2493 }
2494
2495
2496 static DecodeStatus DecodeT2AddrModeImm8(llvm::MCInst &Inst, unsigned Val,
2497                                  uint64_t Address, const void *Decoder) {
2498   DecodeStatus S = Success;
2499
2500   unsigned Rn = fieldFromInstruction32(Val, 9, 4);
2501   unsigned imm = fieldFromInstruction32(Val, 0, 9);
2502
2503   // Some instructions always use an additive offset.
2504   switch (Inst.getOpcode()) {
2505     case ARM::t2LDRT:
2506     case ARM::t2LDRBT:
2507     case ARM::t2LDRHT:
2508     case ARM::t2LDRSBT:
2509     case ARM::t2LDRSHT:
2510       imm |= 0x100;
2511       break;
2512     default:
2513       break;
2514   }
2515
2516   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2517   CHECK(S, DecodeT2Imm8(Inst, imm, Address, Decoder));
2518
2519   return S;
2520 }
2521
2522
2523 static DecodeStatus DecodeT2AddrModeImm12(llvm::MCInst &Inst, unsigned Val,
2524                                   uint64_t Address, const void *Decoder) {
2525   DecodeStatus S = Success;
2526
2527   unsigned Rn = fieldFromInstruction32(Val, 13, 4);
2528   unsigned imm = fieldFromInstruction32(Val, 0, 12);
2529
2530   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2531   Inst.addOperand(MCOperand::CreateImm(imm));
2532
2533   return S;
2534 }
2535
2536
2537 static DecodeStatus DecodeThumbAddSPImm(llvm::MCInst &Inst, uint16_t Insn,
2538                                 uint64_t Address, const void *Decoder) {
2539   unsigned imm = fieldFromInstruction16(Insn, 0, 7);
2540
2541   Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2542   Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2543   Inst.addOperand(MCOperand::CreateImm(imm));
2544
2545   return Success;
2546 }
2547
2548 static DecodeStatus DecodeThumbAddSPReg(llvm::MCInst &Inst, uint16_t Insn,
2549                                 uint64_t Address, const void *Decoder) {
2550   DecodeStatus S = Success;
2551
2552   if (Inst.getOpcode() == ARM::tADDrSP) {
2553     unsigned Rdm = fieldFromInstruction16(Insn, 0, 3);
2554     Rdm |= fieldFromInstruction16(Insn, 7, 1) << 3;
2555
2556     CHECK(S, DecodeGPRRegisterClass(Inst, Rdm, Address, Decoder));
2557     CHECK(S, DecodeGPRRegisterClass(Inst, Rdm, Address, Decoder));
2558     Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2559   } else if (Inst.getOpcode() == ARM::tADDspr) {
2560     unsigned Rm = fieldFromInstruction16(Insn, 3, 4);
2561
2562     Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2563     Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2564     CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
2565   }
2566
2567   return S;
2568 }
2569
2570 static DecodeStatus DecodeThumbCPS(llvm::MCInst &Inst, uint16_t Insn,
2571                            uint64_t Address, const void *Decoder) {
2572   unsigned imod = fieldFromInstruction16(Insn, 4, 1) | 0x2;
2573   unsigned flags = fieldFromInstruction16(Insn, 0, 3);
2574
2575   Inst.addOperand(MCOperand::CreateImm(imod));
2576   Inst.addOperand(MCOperand::CreateImm(flags));
2577
2578   return Success;
2579 }
2580
2581 static DecodeStatus DecodePostIdxReg(llvm::MCInst &Inst, unsigned Insn,
2582                              uint64_t Address, const void *Decoder) {
2583   DecodeStatus S = Success;
2584   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
2585   unsigned add = fieldFromInstruction32(Insn, 4, 1);
2586
2587   CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)) ;
2588   Inst.addOperand(MCOperand::CreateImm(add));
2589
2590   return S;
2591 }
2592
2593 static DecodeStatus DecodeThumbBLXOffset(llvm::MCInst &Inst, unsigned Val,
2594                                  uint64_t Address, const void *Decoder) {
2595   Inst.addOperand(MCOperand::CreateImm(SignExtend32<22>(Val << 1)));
2596   return Success;
2597 }
2598
2599 static DecodeStatus DecodeCoprocessor(llvm::MCInst &Inst, unsigned Val,
2600                               uint64_t Address, const void *Decoder) {
2601   if (Val == 0xA || Val == 0xB)
2602     return Fail;
2603
2604   Inst.addOperand(MCOperand::CreateImm(Val));
2605   return Success;
2606 }
2607
2608 static DecodeStatus
2609 DecodeThumb2BCCInstruction(llvm::MCInst &Inst, unsigned Insn,
2610                            uint64_t Address, const void *Decoder) {
2611   DecodeStatus S = Success;
2612
2613   unsigned pred = fieldFromInstruction32(Insn, 22, 4);
2614   if (pred == 0xE || pred == 0xF) {
2615     unsigned opc = fieldFromInstruction32(Insn, 4, 2);
2616     switch (opc) {
2617       default:
2618         return Fail;
2619       case 0:
2620         Inst.setOpcode(ARM::t2DSB);
2621         break;
2622       case 1:
2623         Inst.setOpcode(ARM::t2DMB);
2624         break;
2625       case 2:
2626         Inst.setOpcode(ARM::t2ISB);
2627         return Success;
2628     }
2629
2630     unsigned imm = fieldFromInstruction32(Insn, 0, 4);
2631     return DecodeMemBarrierOption(Inst, imm, Address, Decoder);
2632   }
2633
2634   unsigned brtarget = fieldFromInstruction32(Insn, 0, 11) << 1;
2635   brtarget |= fieldFromInstruction32(Insn, 11, 1) << 19;
2636   brtarget |= fieldFromInstruction32(Insn, 13, 1) << 18;
2637   brtarget |= fieldFromInstruction32(Insn, 16, 6) << 12;
2638   brtarget |= fieldFromInstruction32(Insn, 26, 1) << 20;
2639
2640   CHECK(S, DecodeT2BROperand(Inst, brtarget, Address, Decoder));
2641   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
2642
2643   return S;
2644 }
2645
2646 // Decode a shifted immediate operand.  These basically consist
2647 // of an 8-bit value, and a 4-bit directive that specifies either
2648 // a splat operation or a rotation.
2649 static DecodeStatus DecodeT2SOImm(llvm::MCInst &Inst, unsigned Val,
2650                           uint64_t Address, const void *Decoder) {
2651   unsigned ctrl = fieldFromInstruction32(Val, 10, 2);
2652   if (ctrl == 0) {
2653     unsigned byte = fieldFromInstruction32(Val, 8, 2);
2654     unsigned imm = fieldFromInstruction32(Val, 0, 8);
2655     switch (byte) {
2656       case 0:
2657         Inst.addOperand(MCOperand::CreateImm(imm));
2658         break;
2659       case 1:
2660         Inst.addOperand(MCOperand::CreateImm((imm << 16) | imm));
2661         break;
2662       case 2:
2663         Inst.addOperand(MCOperand::CreateImm((imm << 24) | (imm << 8)));
2664         break;
2665       case 3:
2666         Inst.addOperand(MCOperand::CreateImm((imm << 24) | (imm << 16) |
2667                                              (imm << 8)  |  imm));
2668         break;
2669     }
2670   } else {
2671     unsigned unrot = fieldFromInstruction32(Val, 0, 7) | 0x80;
2672     unsigned rot = fieldFromInstruction32(Val, 7, 5);
2673     unsigned imm = (unrot >> rot) | (unrot << ((32-rot)&31));
2674     Inst.addOperand(MCOperand::CreateImm(imm));
2675   }
2676
2677   return Success;
2678 }
2679
2680 static DecodeStatus
2681 DecodeThumbBCCTargetOperand(llvm::MCInst &Inst, unsigned Val,
2682                             uint64_t Address, const void *Decoder){
2683   Inst.addOperand(MCOperand::CreateImm(Val << 1));
2684   return Success;
2685 }
2686
2687 static DecodeStatus DecodeThumbBLTargetOperand(llvm::MCInst &Inst, unsigned Val,
2688                                        uint64_t Address, const void *Decoder){
2689   Inst.addOperand(MCOperand::CreateImm(SignExtend32<22>(Val << 1)));
2690   return Success;
2691 }
2692
2693 static DecodeStatus DecodeMemBarrierOption(llvm::MCInst &Inst, unsigned Val,
2694                                    uint64_t Address, const void *Decoder) {
2695   switch (Val) {
2696   default:
2697     return Fail;
2698   case 0xF: // SY
2699   case 0xE: // ST
2700   case 0xB: // ISH
2701   case 0xA: // ISHST
2702   case 0x7: // NSH
2703   case 0x6: // NSHST
2704   case 0x3: // OSH
2705   case 0x2: // OSHST
2706     break;
2707   }
2708
2709   Inst.addOperand(MCOperand::CreateImm(Val));
2710   return Success;
2711 }
2712
2713 static DecodeStatus DecodeMSRMask(llvm::MCInst &Inst, unsigned Val,
2714                           uint64_t Address, const void *Decoder) {
2715   if (!Val) return Fail;
2716   Inst.addOperand(MCOperand::CreateImm(Val));
2717   return Success;
2718 }
2719
2720 static DecodeStatus DecodeDoubleRegLoad(llvm::MCInst &Inst, unsigned Insn,
2721                                         uint64_t Address, const void *Decoder) {
2722   DecodeStatus S = Success;
2723
2724   unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
2725   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2726   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
2727
2728   if ((Rt & 1) || Rt == 0xE || Rn == 0xF) return Fail;
2729
2730   CHECK(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder));
2731   CHECK(S, DecodeGPRRegisterClass(Inst, Rt+1, Address, Decoder));
2732   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2733   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
2734
2735   return S;
2736 }
2737
2738
2739 static DecodeStatus DecodeDoubleRegStore(llvm::MCInst &Inst, unsigned Insn,
2740                                          uint64_t Address, const void *Decoder){
2741   DecodeStatus S = Success;
2742
2743   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
2744   unsigned Rt = fieldFromInstruction32(Insn, 0, 4);
2745   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2746   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
2747
2748   CHECK(S, DecoderGPRRegisterClass(Inst, Rd, Address, Decoder));
2749
2750   if ((Rt & 1) || Rt == 0xE || Rn == 0xF) return Fail;
2751   if (Rd == Rn || Rd == Rt || Rd == Rt+1) return Fail;
2752
2753   CHECK(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder));
2754   CHECK(S, DecodeGPRRegisterClass(Inst, Rt+1, Address, Decoder));
2755   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2756   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
2757
2758   return S;
2759 }
2760
2761 static DecodeStatus DecodeLDRPreImm(llvm::MCInst &Inst, unsigned Insn,
2762                             uint64_t Address, const void *Decoder) {
2763   DecodeStatus S = Success;
2764
2765   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2766   unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
2767   unsigned imm = fieldFromInstruction32(Insn, 0, 12);
2768   imm |= fieldFromInstruction32(Insn, 16, 4) << 13;
2769   imm |= fieldFromInstruction32(Insn, 23, 1) << 12;
2770   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
2771
2772   if (Rn == 0xF || Rn == Rt) CHECK(S, Unpredictable);
2773
2774   CHECK(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder));
2775   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2776   CHECK(S, DecodeAddrModeImm12Operand(Inst, imm, Address, Decoder));
2777   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
2778
2779   return S;
2780 }
2781
2782 static DecodeStatus DecodeLDRPreReg(llvm::MCInst &Inst, unsigned Insn,
2783                             uint64_t Address, const void *Decoder) {
2784   DecodeStatus S = Success;
2785
2786   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2787   unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
2788   unsigned imm = fieldFromInstruction32(Insn, 0, 12);
2789   imm |= fieldFromInstruction32(Insn, 16, 4) << 13;
2790   imm |= fieldFromInstruction32(Insn, 23, 1) << 12;
2791   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
2792   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
2793
2794   if (Rn == 0xF || Rn == Rt) CHECK(S, Unpredictable);
2795   if (Rm == 0xF) CHECK(S, Unpredictable);
2796
2797   CHECK(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder));
2798   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2799   CHECK(S, DecodeSORegMemOperand(Inst, imm, Address, Decoder));
2800   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
2801
2802   return S;
2803 }
2804
2805
2806 static DecodeStatus DecodeSTRPreImm(llvm::MCInst &Inst, unsigned Insn,
2807                             uint64_t Address, const void *Decoder) {
2808   DecodeStatus S = Success;
2809
2810   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2811   unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
2812   unsigned imm = fieldFromInstruction32(Insn, 0, 12);
2813   imm |= fieldFromInstruction32(Insn, 16, 4) << 13;
2814   imm |= fieldFromInstruction32(Insn, 23, 1) << 12;
2815   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
2816
2817   if (Rn == 0xF || Rn == Rt) CHECK(S, Unpredictable);
2818
2819   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2820   CHECK(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder));
2821   CHECK(S, DecodeAddrModeImm12Operand(Inst, imm, Address, Decoder));
2822   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
2823
2824   return S;
2825 }
2826
2827 static DecodeStatus DecodeSTRPreReg(llvm::MCInst &Inst, unsigned Insn,
2828                             uint64_t Address, const void *Decoder) {
2829   DecodeStatus S = Success;
2830
2831   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2832   unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
2833   unsigned imm = fieldFromInstruction32(Insn, 0, 12);
2834   imm |= fieldFromInstruction32(Insn, 16, 4) << 13;
2835   imm |= fieldFromInstruction32(Insn, 23, 1) << 12;
2836   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
2837
2838   if (Rn == 0xF || Rn == Rt) CHECK(S, Unpredictable);
2839
2840   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2841   CHECK(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder));
2842   CHECK(S, DecodeSORegMemOperand(Inst, imm, Address, Decoder));
2843   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
2844
2845   return S;
2846 }
2847
2848 static DecodeStatus DecodeVLD1LN(llvm::MCInst &Inst, unsigned Insn,
2849                          uint64_t Address, const void *Decoder) {
2850   DecodeStatus S = Success;
2851
2852   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2853   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
2854   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
2855   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
2856   unsigned size = fieldFromInstruction32(Insn, 10, 2);
2857
2858   unsigned align = 0;
2859   unsigned index = 0;
2860   switch (size) {
2861     default:
2862       return Fail;
2863     case 0:
2864       if (fieldFromInstruction32(Insn, 4, 1))
2865         return Fail; // UNDEFINED
2866       index = fieldFromInstruction32(Insn, 5, 3);
2867       break;
2868     case 1:
2869       if (fieldFromInstruction32(Insn, 5, 1))
2870         return Fail; // UNDEFINED
2871       index = fieldFromInstruction32(Insn, 6, 2);
2872       if (fieldFromInstruction32(Insn, 4, 1))
2873         align = 2;
2874       break;
2875     case 2:
2876       if (fieldFromInstruction32(Insn, 6, 1))
2877         return Fail; // UNDEFINED
2878       index = fieldFromInstruction32(Insn, 7, 1);
2879       if (fieldFromInstruction32(Insn, 4, 2) != 0)
2880         align = 4;
2881   }
2882
2883   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
2884   if (Rm != 0xF) { // Writeback
2885     CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2886   }
2887   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2888   Inst.addOperand(MCOperand::CreateImm(align));
2889   if (Rm != 0xF) {
2890     if (Rm != 0xD)
2891       CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
2892     else
2893       Inst.addOperand(MCOperand::CreateReg(0));
2894   }
2895
2896   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
2897   Inst.addOperand(MCOperand::CreateImm(index));
2898
2899   return S;
2900 }
2901
2902 static DecodeStatus DecodeVST1LN(llvm::MCInst &Inst, unsigned Insn,
2903                          uint64_t Address, const void *Decoder) {
2904   DecodeStatus S = Success;
2905
2906   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2907   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
2908   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
2909   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
2910   unsigned size = fieldFromInstruction32(Insn, 10, 2);
2911
2912   unsigned align = 0;
2913   unsigned index = 0;
2914   switch (size) {
2915     default:
2916       return Fail;
2917     case 0:
2918       if (fieldFromInstruction32(Insn, 4, 1))
2919         return Fail; // UNDEFINED
2920       index = fieldFromInstruction32(Insn, 5, 3);
2921       break;
2922     case 1:
2923       if (fieldFromInstruction32(Insn, 5, 1))
2924         return Fail; // UNDEFINED
2925       index = fieldFromInstruction32(Insn, 6, 2);
2926       if (fieldFromInstruction32(Insn, 4, 1))
2927         align = 2;
2928       break;
2929     case 2:
2930       if (fieldFromInstruction32(Insn, 6, 1))
2931         return Fail; // UNDEFINED
2932       index = fieldFromInstruction32(Insn, 7, 1);
2933       if (fieldFromInstruction32(Insn, 4, 2) != 0)
2934         align = 4;
2935   }
2936
2937   if (Rm != 0xF) { // Writeback
2938     CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2939   }
2940   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2941   Inst.addOperand(MCOperand::CreateImm(align));
2942   if (Rm != 0xF) {
2943     if (Rm != 0xD)
2944       CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
2945     else
2946       Inst.addOperand(MCOperand::CreateReg(0));
2947   }
2948
2949   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
2950   Inst.addOperand(MCOperand::CreateImm(index));
2951
2952   return S;
2953 }
2954
2955
2956 static DecodeStatus DecodeVLD2LN(llvm::MCInst &Inst, unsigned Insn,
2957                          uint64_t Address, const void *Decoder) {
2958   DecodeStatus S = Success;
2959
2960   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2961   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
2962   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
2963   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
2964   unsigned size = fieldFromInstruction32(Insn, 10, 2);
2965
2966   unsigned align = 0;
2967   unsigned index = 0;
2968   unsigned inc = 1;
2969   switch (size) {
2970     default:
2971       return Fail;
2972     case 0:
2973       index = fieldFromInstruction32(Insn, 5, 3);
2974       if (fieldFromInstruction32(Insn, 4, 1))
2975         align = 2;
2976       break;
2977     case 1:
2978       index = fieldFromInstruction32(Insn, 6, 2);
2979       if (fieldFromInstruction32(Insn, 4, 1))
2980         align = 4;
2981       if (fieldFromInstruction32(Insn, 5, 1))
2982         inc = 2;
2983       break;
2984     case 2:
2985       if (fieldFromInstruction32(Insn, 5, 1))
2986         return Fail; // UNDEFINED
2987       index = fieldFromInstruction32(Insn, 7, 1);
2988       if (fieldFromInstruction32(Insn, 4, 1) != 0)
2989         align = 8;
2990       if (fieldFromInstruction32(Insn, 6, 1))
2991         inc = 2;
2992       break;
2993   }
2994
2995   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
2996   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder));
2997   if (Rm != 0xF) { // Writeback
2998     CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
2999   }
3000   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
3001   Inst.addOperand(MCOperand::CreateImm(align));
3002   if (Rm != 0xF) {
3003     if (Rm != 0xD)
3004       CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
3005     else
3006       Inst.addOperand(MCOperand::CreateReg(0));
3007   }
3008
3009   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
3010   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder));
3011   Inst.addOperand(MCOperand::CreateImm(index));
3012
3013   return S;
3014 }
3015
3016 static DecodeStatus DecodeVST2LN(llvm::MCInst &Inst, unsigned Insn,
3017                          uint64_t Address, const void *Decoder) {
3018   DecodeStatus S = Success;
3019
3020   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
3021   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
3022   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
3023   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
3024   unsigned size = fieldFromInstruction32(Insn, 10, 2);
3025
3026   unsigned align = 0;
3027   unsigned index = 0;
3028   unsigned inc = 1;
3029   switch (size) {
3030     default:
3031       return Fail;
3032     case 0:
3033       index = fieldFromInstruction32(Insn, 5, 3);
3034       if (fieldFromInstruction32(Insn, 4, 1))
3035         align = 2;
3036       break;
3037     case 1:
3038       index = fieldFromInstruction32(Insn, 6, 2);
3039       if (fieldFromInstruction32(Insn, 4, 1))
3040         align = 4;
3041       if (fieldFromInstruction32(Insn, 5, 1))
3042         inc = 2;
3043       break;
3044     case 2:
3045       if (fieldFromInstruction32(Insn, 5, 1))
3046         return Fail; // UNDEFINED
3047       index = fieldFromInstruction32(Insn, 7, 1);
3048       if (fieldFromInstruction32(Insn, 4, 1) != 0)
3049         align = 8;
3050       if (fieldFromInstruction32(Insn, 6, 1))
3051         inc = 2;
3052       break;
3053   }
3054
3055   if (Rm != 0xF) { // Writeback
3056     CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
3057   }
3058   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
3059   Inst.addOperand(MCOperand::CreateImm(align));
3060   if (Rm != 0xF) {
3061     if (Rm != 0xD)
3062       CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
3063     else
3064       Inst.addOperand(MCOperand::CreateReg(0));
3065   }
3066
3067   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
3068   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder));
3069   Inst.addOperand(MCOperand::CreateImm(index));
3070
3071   return S;
3072 }
3073
3074
3075 static DecodeStatus DecodeVLD3LN(llvm::MCInst &Inst, unsigned Insn,
3076                          uint64_t Address, const void *Decoder) {
3077   DecodeStatus S = Success;
3078
3079   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
3080   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
3081   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
3082   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
3083   unsigned size = fieldFromInstruction32(Insn, 10, 2);
3084
3085   unsigned align = 0;
3086   unsigned index = 0;
3087   unsigned inc = 1;
3088   switch (size) {
3089     default:
3090       return Fail;
3091     case 0:
3092       if (fieldFromInstruction32(Insn, 4, 1))
3093         return Fail; // UNDEFINED
3094       index = fieldFromInstruction32(Insn, 5, 3);
3095       break;
3096     case 1:
3097       if (fieldFromInstruction32(Insn, 4, 1))
3098         return Fail; // UNDEFINED
3099       index = fieldFromInstruction32(Insn, 6, 2);
3100       if (fieldFromInstruction32(Insn, 5, 1))
3101         inc = 2;
3102       break;
3103     case 2:
3104       if (fieldFromInstruction32(Insn, 4, 2))
3105         return Fail; // UNDEFINED
3106       index = fieldFromInstruction32(Insn, 7, 1);
3107       if (fieldFromInstruction32(Insn, 6, 1))
3108         inc = 2;
3109       break;
3110   }
3111
3112   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
3113   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder));
3114   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder));
3115
3116   if (Rm != 0xF) { // Writeback
3117     CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
3118   }
3119   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
3120   Inst.addOperand(MCOperand::CreateImm(align));
3121   if (Rm != 0xF) { 
3122     if (Rm != 0xD)
3123       CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
3124     else
3125       Inst.addOperand(MCOperand::CreateReg(0));
3126   }
3127
3128   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
3129   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder));
3130   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder));
3131   Inst.addOperand(MCOperand::CreateImm(index));
3132
3133   return S;
3134 }
3135
3136 static DecodeStatus DecodeVST3LN(llvm::MCInst &Inst, unsigned Insn,
3137                          uint64_t Address, const void *Decoder) {
3138   DecodeStatus S = Success;
3139
3140   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
3141   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
3142   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
3143   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
3144   unsigned size = fieldFromInstruction32(Insn, 10, 2);
3145
3146   unsigned align = 0;
3147   unsigned index = 0;
3148   unsigned inc = 1;
3149   switch (size) {
3150     default:
3151       return Fail;
3152     case 0:
3153       if (fieldFromInstruction32(Insn, 4, 1))
3154         return Fail; // UNDEFINED
3155       index = fieldFromInstruction32(Insn, 5, 3);
3156       break;
3157     case 1:
3158       if (fieldFromInstruction32(Insn, 4, 1))
3159         return Fail; // UNDEFINED
3160       index = fieldFromInstruction32(Insn, 6, 2);
3161       if (fieldFromInstruction32(Insn, 5, 1))
3162         inc = 2;
3163       break;
3164     case 2:
3165       if (fieldFromInstruction32(Insn, 4, 2))
3166         return Fail; // UNDEFINED
3167       index = fieldFromInstruction32(Insn, 7, 1);
3168       if (fieldFromInstruction32(Insn, 6, 1))
3169         inc = 2;
3170       break;
3171   }
3172
3173   if (Rm != 0xF) { // Writeback
3174     CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
3175   }
3176   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
3177   Inst.addOperand(MCOperand::CreateImm(align));
3178   if (Rm != 0xF) {
3179     if (Rm != 0xD)
3180       CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
3181     else
3182       Inst.addOperand(MCOperand::CreateReg(0));
3183   }
3184
3185   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
3186   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder));
3187   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder));
3188   Inst.addOperand(MCOperand::CreateImm(index));
3189
3190   return S;
3191 }
3192
3193
3194 static DecodeStatus DecodeVLD4LN(llvm::MCInst &Inst, unsigned Insn,
3195                          uint64_t Address, const void *Decoder) {
3196   DecodeStatus S = Success;
3197
3198   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
3199   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
3200   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
3201   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
3202   unsigned size = fieldFromInstruction32(Insn, 10, 2);
3203
3204   unsigned align = 0;
3205   unsigned index = 0;
3206   unsigned inc = 1;
3207   switch (size) {
3208     default:
3209       return Fail;
3210     case 0:
3211       if (fieldFromInstruction32(Insn, 4, 1))
3212         align = 4;
3213       index = fieldFromInstruction32(Insn, 5, 3);
3214       break;
3215     case 1:
3216       if (fieldFromInstruction32(Insn, 4, 1))
3217         align = 8;
3218       index = fieldFromInstruction32(Insn, 6, 2);
3219       if (fieldFromInstruction32(Insn, 5, 1))
3220         inc = 2;
3221       break;
3222     case 2:
3223       if (fieldFromInstruction32(Insn, 4, 2))
3224         align = 4 << fieldFromInstruction32(Insn, 4, 2);
3225       index = fieldFromInstruction32(Insn, 7, 1);
3226       if (fieldFromInstruction32(Insn, 6, 1))
3227         inc = 2;
3228       break;
3229   }
3230
3231   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
3232   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder));
3233   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder));
3234   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+3*inc, Address, Decoder));
3235
3236   if (Rm != 0xF) { // Writeback
3237     CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
3238   }
3239   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
3240   Inst.addOperand(MCOperand::CreateImm(align));
3241   if (Rm != 0xF) {
3242     if (Rm != 0xD)
3243       CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
3244     else
3245       Inst.addOperand(MCOperand::CreateReg(0));
3246   }
3247
3248   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
3249   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder));
3250   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder));
3251   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+3*inc, Address, Decoder));
3252   Inst.addOperand(MCOperand::CreateImm(index));
3253
3254   return S;
3255 }
3256
3257 static DecodeStatus DecodeVST4LN(llvm::MCInst &Inst, unsigned Insn,
3258                          uint64_t Address, const void *Decoder) {
3259   DecodeStatus S = Success;
3260
3261   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
3262   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
3263   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
3264   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
3265   unsigned size = fieldFromInstruction32(Insn, 10, 2);
3266
3267   unsigned align = 0;
3268   unsigned index = 0;
3269   unsigned inc = 1;
3270   switch (size) {
3271     default:
3272       return Fail;
3273     case 0:
3274       if (fieldFromInstruction32(Insn, 4, 1))
3275         align = 4;
3276       index = fieldFromInstruction32(Insn, 5, 3);
3277       break;
3278     case 1:
3279       if (fieldFromInstruction32(Insn, 4, 1))
3280         align = 8;
3281       index = fieldFromInstruction32(Insn, 6, 2);
3282       if (fieldFromInstruction32(Insn, 5, 1))
3283         inc = 2;
3284       break;
3285     case 2:
3286       if (fieldFromInstruction32(Insn, 4, 2))
3287         align = 4 << fieldFromInstruction32(Insn, 4, 2);
3288       index = fieldFromInstruction32(Insn, 7, 1);
3289       if (fieldFromInstruction32(Insn, 6, 1))
3290         inc = 2;
3291       break;
3292   }
3293
3294   if (Rm != 0xF) { // Writeback
3295     CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
3296   }
3297   CHECK(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder));
3298   Inst.addOperand(MCOperand::CreateImm(align));
3299   if (Rm != 0xF) {
3300     if (Rm != 0xD)
3301       CHECK(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder));
3302     else
3303       Inst.addOperand(MCOperand::CreateReg(0));
3304   }
3305
3306   CHECK(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder));
3307   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder));
3308   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder));
3309   CHECK(S, DecodeDPRRegisterClass(Inst, Rd+3*inc, Address, Decoder));
3310   Inst.addOperand(MCOperand::CreateImm(index));
3311
3312   return S;
3313 }
3314
3315 static DecodeStatus DecodeVMOVSRR(llvm::MCInst &Inst, unsigned Insn,
3316                                   uint64_t Address, const void *Decoder) {
3317   DecodeStatus S = Success;
3318   unsigned Rt  = fieldFromInstruction32(Insn, 12, 4);
3319   unsigned Rt2 = fieldFromInstruction32(Insn, 16, 4);
3320   unsigned Rm  = fieldFromInstruction32(Insn,  0, 4);
3321   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
3322   Rm |= fieldFromInstruction32(Insn, 5, 1) << 4;
3323
3324   if (Rt == 0xF || Rt2 == 0xF || Rm == 0x1F)
3325     CHECK(S, Unpredictable);
3326
3327   CHECK(S, DecodeSPRRegisterClass(Inst, Rm  , Address, Decoder));
3328   CHECK(S, DecodeSPRRegisterClass(Inst, Rm+1, Address, Decoder));
3329   CHECK(S, DecodeGPRRegisterClass(Inst, Rt  , Address, Decoder));
3330   CHECK(S, DecodeGPRRegisterClass(Inst, Rt2 , Address, Decoder));
3331   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
3332
3333   return S;
3334 }
3335
3336 static DecodeStatus DecodeVMOVRRS(llvm::MCInst &Inst, unsigned Insn,
3337                                   uint64_t Address, const void *Decoder) {
3338   DecodeStatus S = Success;
3339   unsigned Rt  = fieldFromInstruction32(Insn, 12, 4);
3340   unsigned Rt2 = fieldFromInstruction32(Insn, 16, 4);
3341   unsigned Rm  = fieldFromInstruction32(Insn,  0, 4);
3342   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
3343   Rm |= fieldFromInstruction32(Insn, 5, 1) << 4;
3344
3345   if (Rt == 0xF || Rt2 == 0xF || Rm == 0x1F)
3346     CHECK(S, Unpredictable);
3347
3348   CHECK(S, DecodeGPRRegisterClass(Inst, Rt  , Address, Decoder));
3349   CHECK(S, DecodeGPRRegisterClass(Inst, Rt2 , Address, Decoder));
3350   CHECK(S, DecodeSPRRegisterClass(Inst, Rm  , Address, Decoder));
3351   CHECK(S, DecodeSPRRegisterClass(Inst, Rm+1, Address, Decoder));
3352   CHECK(S, DecodePredicateOperand(Inst, pred, Address, Decoder));
3353
3354   return S;
3355 }
3356
3357 static DecodeStatus DecodeITCond(llvm::MCInst &Inst, unsigned Cond,
3358                                  uint64_t Address, const void *Decoder) {
3359   DecodeStatus S = Success;
3360   if (Cond == 0xF) {
3361     Cond = 0xE;
3362     CHECK(S, Unpredictable);
3363   }
3364
3365   Inst.addOperand(MCOperand::CreateImm(Cond));
3366   return S;
3367 }
3368
3369 static DecodeStatus DecodeITMask(llvm::MCInst &Inst, unsigned Mask,
3370                                  uint64_t Address, const void *Decoder) {
3371   DecodeStatus S = Success;
3372   if (Mask == 0) {
3373     Mask = 0x8;
3374     CHECK(S, Unpredictable);
3375   }
3376   Inst.addOperand(MCOperand::CreateImm(Mask));
3377   return S;
3378 }
3379