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