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