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