307ce88536914061868c22502b3efc0e3a579fde
[oota-llvm.git] / lib / Target / ARM / Disassembler / ARMDisassembler.cpp
1 //===- ARMDisassembler.cpp - Disassembler for ARM/Thumb ISA -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #define DEBUG_TYPE "arm-disassembler"
11
12 #include "ARMDisassembler.h"
13 #include "ARM.h"
14 #include "ARMRegisterInfo.h"
15 #include "MCTargetDesc/ARMAddressingModes.h"
16 #include "MCTargetDesc/ARMBaseInfo.h"
17 #include "llvm/MC/EDInstInfo.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/Target/TargetRegistry.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/MemoryObject.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26
27 // Forward declare these because the autogenerated code will reference them.
28 // Definitions are further down.
29 static bool DecodeGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
30                                    uint64_t Address, const void *Decoder);
31 static bool DecodetGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
32                                    uint64_t Address, const void *Decoder);
33 static bool DecodetcGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
34                                    uint64_t Address, const void *Decoder);
35 static bool DecoderGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
36                                    uint64_t Address, const void *Decoder);
37 static bool DecodeSPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
38                                    uint64_t Address, const void *Decoder);
39 static bool DecodeDPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
40                                    uint64_t Address, const void *Decoder);
41 static bool DecodeDPR_8RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
42                                    uint64_t Address, const void *Decoder);
43 static bool DecodeDPR_VFP2RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
44                                    uint64_t Address, const void *Decoder);
45 static bool DecodeQPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
46                                    uint64_t Address, const void *Decoder);
47
48 static bool DecodePredicateOperand(llvm::MCInst &Inst, unsigned Val,
49                                uint64_t Address, const void *Decoder);
50 static bool DecodeCCOutOperand(llvm::MCInst &Inst, unsigned Val,
51                                uint64_t Address, const void *Decoder);
52 static bool DecodeSOImmOperand(llvm::MCInst &Inst, unsigned Val,
53                                uint64_t Address, const void *Decoder);
54 static bool DecodeBLTargetOperand(llvm::MCInst &Inst, unsigned Val,
55                                uint64_t Address, const void *Decoder);
56 static bool DecodeRegListOperand(llvm::MCInst &Inst, unsigned Val,
57                                uint64_t Address, const void *Decoder);
58 static bool DecodeSPRRegListOperand(llvm::MCInst &Inst, unsigned Val,
59                                uint64_t Address, const void *Decoder);
60 static bool DecodeDPRRegListOperand(llvm::MCInst &Inst, unsigned Val,
61                                uint64_t Address, const void *Decoder);
62
63 static bool DecodeBitfieldMaskOperand(llvm::MCInst &Inst, unsigned Insn,
64                                uint64_t Address, const void *Decoder);
65 static bool DecodeCopMemInstruction(llvm::MCInst &Inst, unsigned Insn,
66                                uint64_t Address, const void *Decoder);
67 static bool DecodeAddrMode2IdxInstruction(llvm::MCInst &Inst, unsigned Insn,
68                                uint64_t Address, const void *Decoder);
69 static bool DecodeSORegMemOperand(llvm::MCInst &Inst, unsigned Insn,
70                                uint64_t Address, const void *Decoder);
71 static bool DecodeAddrMode3Instruction(llvm::MCInst &Inst, unsigned Insn,
72                                uint64_t Address, const void *Decoder);
73 static bool DecodeSORegImmOperand(llvm::MCInst &Inst, unsigned Insn,
74                                uint64_t Address, const void *Decoder);
75 static bool DecodeSORegRegOperand(llvm::MCInst &Inst, unsigned Insn,
76                                uint64_t Address, const void *Decoder);
77
78 static bool DecodeMemMultipleWritebackInstruction(llvm::MCInst & Inst,
79                                                   unsigned Insn,
80                                                   uint64_t Adddress,
81                                                   const void *Decoder);
82 static bool DecodeSMLAInstruction(llvm::MCInst &Inst, unsigned Insn,
83                                uint64_t Address, const void *Decoder);
84 static bool DecodeAddrModeImm12Operand(llvm::MCInst &Inst, unsigned Val,
85                                uint64_t Address, const void *Decoder);
86 static bool DecodeAddrMode5Operand(llvm::MCInst &Inst, unsigned Val,
87                                uint64_t Address, const void *Decoder);
88 static bool DecodeAddrMode7Operand(llvm::MCInst &Inst, unsigned Val,
89                                uint64_t Address, const void *Decoder);
90 static bool DecodeBranchImmInstruction(llvm::MCInst &Inst, unsigned Insn,
91                                uint64_t Address, const void *Decoder);
92 static bool DecodeVCVTImmOperand(llvm::MCInst &Inst, unsigned Val,
93                                uint64_t Address, const void *Decoder);
94 static bool DecodeAddrMode6Operand(llvm::MCInst &Inst, unsigned Val,
95                                uint64_t Address, const void *Decoder);
96 static bool DecodeVLDInstruction(llvm::MCInst &Inst, unsigned Val,
97                                uint64_t Address, const void *Decoder);
98 static bool DecodeVSTInstruction(llvm::MCInst &Inst, unsigned Val,
99                                uint64_t Address, const void *Decoder);
100 static bool DecodeVLD1DupInstruction(llvm::MCInst &Inst, unsigned Val,
101                                uint64_t Address, const void *Decoder);
102 static bool DecodeVLD2DupInstruction(llvm::MCInst &Inst, unsigned Val,
103                                uint64_t Address, const void *Decoder);
104 static bool DecodeVLD3DupInstruction(llvm::MCInst &Inst, unsigned Val,
105                                uint64_t Address, const void *Decoder);
106 static bool DecodeVLD4DupInstruction(llvm::MCInst &Inst, unsigned Val,
107                                uint64_t Address, const void *Decoder);
108 static bool DecodeNEONModImmInstruction(llvm::MCInst &Inst, unsigned Val,
109                                uint64_t Address, const void *Decoder);
110 static bool DecodeVSHLMaxInstruction(llvm::MCInst &Inst, unsigned Val,
111                                uint64_t Address, const void *Decoder);
112 static bool DecodeShiftRight8Imm(llvm::MCInst &Inst, unsigned Val,
113                                uint64_t Address, const void *Decoder);
114 static bool DecodeShiftRight16Imm(llvm::MCInst &Inst, unsigned Val,
115                                uint64_t Address, const void *Decoder);
116 static bool DecodeShiftRight32Imm(llvm::MCInst &Inst, unsigned Val,
117                                uint64_t Address, const void *Decoder);
118 static bool DecodeShiftRight64Imm(llvm::MCInst &Inst, unsigned Val,
119                                uint64_t Address, const void *Decoder);
120 static bool DecodeTBLInstruction(llvm::MCInst &Inst, unsigned Insn,
121                                uint64_t Address, const void *Decoder);
122 static bool DecodeVFPfpImm(llvm::MCInst &Inst, unsigned Val,
123                                uint64_t Address, const void *Decoder);
124 static bool DecodePostIdxReg(llvm::MCInst &Inst, unsigned Insn,
125                                uint64_t Address, const void *Decoder);
126 static bool DecodeCoprocessor(llvm::MCInst &Inst, unsigned Insn,
127                                uint64_t Address, const void *Decoder);
128 static bool DecodeAddrMode3Offset(llvm::MCInst &Inst, unsigned Insn,
129                                uint64_t Address, const void *Decoder);
130
131
132 static bool DecodeThumbAddSpecialReg(llvm::MCInst &Inst, uint16_t Insn,
133                                uint64_t Address, const void *Decoder);
134 static bool DecodeThumbBROperand(llvm::MCInst &Inst, unsigned Val,
135                                uint64_t Address, const void *Decoder);
136 static bool DecodeT2BROperand(llvm::MCInst &Inst, unsigned Val,
137                                uint64_t Address, const void *Decoder);
138 static bool DecodeThumbCmpBROperand(llvm::MCInst &Inst, unsigned Val,
139                                uint64_t Address, const void *Decoder);
140 static bool DecodeThumbAddrModeRR(llvm::MCInst &Inst, unsigned Val,
141                                uint64_t Address, const void *Decoder);
142 static bool DecodeThumbAddrModeIS(llvm::MCInst &Inst, unsigned Val,
143                                uint64_t Address, const void *Decoder);
144 static bool DecodeThumbAddrModePC(llvm::MCInst &Inst, unsigned Val,
145                                uint64_t Address, const void *Decoder);
146 static bool DecodeThumbAddrModeSP(llvm::MCInst &Inst, unsigned Val,
147                                uint64_t Address, const void *Decoder);
148 static bool DecodeT2AddrModeSOReg(llvm::MCInst &Inst, unsigned Val,
149                                uint64_t Address, const void *Decoder);
150 static bool DecodeT2LoadShift(llvm::MCInst &Inst, unsigned Val,
151                                uint64_t Address, const void *Decoder);
152 static bool DecodeT2Imm8S4(llvm::MCInst &Inst, unsigned Val,
153                                uint64_t Address, const void *Decoder);
154 static bool DecodeT2AddrModeImm8s4(llvm::MCInst &Inst, unsigned Val,
155                                uint64_t Address, const void *Decoder);
156 static bool DecodeT2Imm8(llvm::MCInst &Inst, unsigned Val,
157                                uint64_t Address, const void *Decoder);
158 static bool DecodeT2AddrModeImm8(llvm::MCInst &Inst, unsigned Val,
159                                uint64_t Address, const void *Decoder);
160 static bool DecodeThumbAddSPImm(llvm::MCInst &Inst, uint16_t Val,
161                                uint64_t Address, const void *Decoder);
162 static bool DecodeThumbAddSPReg(llvm::MCInst &Inst, uint16_t Insn,
163                                 uint64_t Address, const void *Decoder);
164 static bool DecodeThumbCPS(llvm::MCInst &Inst, uint16_t Insn,
165                                 uint64_t Address, const void *Decoder);
166 static bool DecodeThumbBLXOffset(llvm::MCInst &Inst, unsigned Insn,
167                                 uint64_t Address, const void *Decoder);
168 static bool DecodeT2AddrModeImm12(llvm::MCInst &Inst, unsigned Val,
169                                 uint64_t Address, const void *Decoder);
170 static bool DecodeThumbSRImm(llvm::MCInst &Inst, unsigned Val,
171                                 uint64_t Address, const void *Decoder);
172 static bool DecodeThumb2BCCInstruction(llvm::MCInst &Inst, unsigned Val,
173                                 uint64_t Address, const void *Decoder);
174 static bool DecodeT2SOImm(llvm::MCInst &Inst, unsigned Val,
175                                 uint64_t Address, const void *Decoder);
176 static bool DecodeThumbBCCTargetOperand(llvm::MCInst &Inst, unsigned Val,
177                                 uint64_t Address, const void *Decoder);
178 static bool DecodeThumbBLTargetOperand(llvm::MCInst &Inst, unsigned Val,
179                                 uint64_t Address, const void *Decoder);
180
181 #include "ARMGenDisassemblerTables.inc"
182 #include "ARMGenInstrInfo.inc"
183 #include "ARMGenEDInfo.inc"
184
185 using namespace llvm;
186
187 static MCDisassembler *createARMDisassembler(const Target &T) {
188   return new ARMDisassembler;
189 }
190
191 static MCDisassembler *createThumbDisassembler(const Target &T) {
192   return new ThumbDisassembler;
193 }
194
195 EDInstInfo *ARMDisassembler::getEDInfo() const {
196   return instInfoARM;
197 }
198
199 EDInstInfo *ThumbDisassembler::getEDInfo() const {
200   return instInfoARM;
201 }
202
203
204 bool ARMDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
205                                      const MemoryObject &Region,
206                                      uint64_t Address,raw_ostream &os) const {
207   uint8_t bytes[4];
208
209   // We want to read exactly 4 bytes of data.
210   if (Region.readBytes(Address, 4, (uint8_t*)bytes, NULL) == -1)
211     return false;
212
213   // Encoded as a small-endian 32-bit word in the stream.
214   uint32_t insn = (bytes[3] << 24) |
215                   (bytes[2] << 16) |
216                   (bytes[1] <<  8) |
217                   (bytes[0] <<  0);
218
219   // Calling the auto-generated decoder function.
220   bool result = decodeARMInstruction32(MI, insn, Address, this);
221   if (result) {
222     Size = 4;
223     return true;
224   }
225
226   // Instructions that are shared between ARM and Thumb modes.
227   // FIXME: This shouldn't really exist.  It's an artifact of the
228   // fact that we fail to encode a few instructions properly for Thumb.
229   MI.clear();
230   result = decodeCommonInstruction32(MI, insn, Address, this);
231   if (result) {
232     Size = 4;
233     return true;
234   }
235
236   // VFP and NEON instructions, similarly, are shared between ARM
237   // and Thumb modes.
238   MI.clear();
239   result = decodeVFPInstruction32(MI, insn, Address, this);
240   if (result) {
241     Size = 4;
242     return true;
243   }
244
245   MI.clear();
246   result = decodeNEONInstruction32(MI, insn, Address, this);
247   if (result) {
248     // Add a fake predicate operand, because we share these instruction
249     // definitions with Thumb2 where these instructions are predicable.
250     if (!DecodePredicateOperand(MI, 0xE, Address, this)) return false;
251     Size = 4;
252     return true;
253   }
254
255   MI.clear();
256
257   return false;
258 }
259
260 namespace llvm {
261 extern MCInstrDesc ARMInsts[];
262 }
263
264 // Thumb1 instructions don't have explicit S bits.  Rather, they
265 // implicitly set CPSR.  Since it's not represented in the encoding, the
266 // auto-generated decoder won't inject the CPSR operand.  We need to fix
267 // that as a post-pass.
268 static void AddThumb1SBit(MCInst &MI, bool InITBlock) {
269   const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
270   MCInst::iterator I = MI.begin();
271   for (unsigned i = 0; i < MI.size(); ++i, ++I) {
272     if (OpInfo[i].isOptionalDef() && OpInfo[i].RegClass == ARM::CCRRegClassID) {
273       MI.insert(I, MCOperand::CreateReg(InITBlock ? 0 : ARM::CPSR));
274       return;
275     }
276   }
277
278   if (OpInfo[MI.size()].isOptionalDef() &&
279       OpInfo[MI.size()].RegClass == ARM::CCRRegClassID)
280     MI.insert(MI.end(), MCOperand::CreateReg(InITBlock ? 0 : ARM::CPSR));
281 }
282
283 // Most Thumb instructions don't have explicit predicates in the
284 // encoding, but rather get their predicates from IT context.  We need
285 // to fix up the predicate operands using this context information as a
286 // post-pass.
287 void ThumbDisassembler::AddThumbPredicate(MCInst &MI) const {
288   // A few instructions actually have predicates encoded in them.  Don't
289   // try to overwrite it if we're seeing one of those.
290   switch (MI.getOpcode()) {
291     case ARM::tBcc:
292     case ARM::t2Bcc:
293       return;
294     default:
295       break;
296   }
297
298   // If we're in an IT block, base the predicate on that.  Otherwise,
299   // assume a predicate of AL.
300   unsigned CC;
301   if (ITBlock.size()) {
302     CC = ITBlock.back();
303     ITBlock.pop_back();
304   } else
305     CC = ARMCC::AL;
306
307   const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
308   MCInst::iterator I = MI.begin();
309   for (unsigned i = 0; i < MI.size(); ++i, ++I) {
310     if (OpInfo[i].isPredicate()) {
311       I = MI.insert(I, MCOperand::CreateImm(CC));
312       ++I;
313       if (CC == ARMCC::AL)
314         MI.insert(I, MCOperand::CreateReg(0));
315       else
316         MI.insert(I, MCOperand::CreateReg(ARM::CPSR));
317       return;
318     }
319   }
320
321   MI.insert(MI.end(), MCOperand::CreateImm(CC));
322   if (CC == ARMCC::AL)
323     MI.insert(MI.end(), MCOperand::CreateReg(0));
324   else
325     MI.insert(MI.end(), MCOperand::CreateReg(ARM::CPSR));
326 }
327
328 // Thumb VFP instructions are a special case.  Because we share their
329 // encodings between ARM and Thumb modes, and they are predicable in ARM
330 // mode, the auto-generated decoder will give them an (incorrect)
331 // predicate operand.  We need to rewrite these operands based on the IT
332 // context as a post-pass.
333 void ThumbDisassembler::UpdateThumbVFPPredicate(MCInst &MI) const {
334   unsigned CC;
335   if (ITBlock.size()) {
336     CC = ITBlock.back();
337     ITBlock.pop_back();
338   } else
339     CC = ARMCC::AL;
340
341   const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
342   MCInst::iterator I = MI.begin();
343   for (unsigned i = 0; i < MI.size(); ++i, ++I) {
344     if (OpInfo[i].isPredicate() ) {
345       I->setImm(CC);
346       ++I;
347       if (CC == ARMCC::AL)
348         I->setReg(0);
349       else
350         I->setReg(ARM::CPSR);
351       return;
352     }
353   }
354 }
355
356
357 bool ThumbDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
358                                        const MemoryObject &Region,
359                                        uint64_t Address,raw_ostream &os) const {
360   uint8_t bytes[4];
361
362   // We want to read exactly 2 bytes of data.
363   if (Region.readBytes(Address, 2, (uint8_t*)bytes, NULL) == -1)
364     return false;
365
366   uint16_t insn16 = (bytes[1] << 8) | bytes[0];
367   bool result = decodeThumbInstruction16(MI, insn16, Address, this);
368   if (result) {
369     Size = 2;
370     bool InITBlock = ITBlock.size();
371     AddThumbPredicate(MI);
372     AddThumb1SBit(MI, InITBlock);
373     return true;
374   }
375
376   MI.clear();
377   result = decodeThumb2Instruction16(MI, insn16, Address, this);
378   if (result) {
379     Size = 2;
380     AddThumbPredicate(MI);
381
382     // If we find an IT instruction, we need to parse its condition
383     // code and mask operands so that we can apply them correctly
384     // to the subsequent instructions.
385     if (MI.getOpcode() == ARM::t2IT) {
386       unsigned firstcond = MI.getOperand(0).getImm();
387       uint32_t mask = MI.getOperand(1).getImm();
388       unsigned zeros = CountTrailingZeros_32(mask);
389       mask >>= zeros+1;
390
391       for (unsigned i = 0; i < 4 - (zeros+1); ++i) {
392         if (firstcond ^ (mask & 1))
393           ITBlock.push_back(firstcond ^ 1);
394         else
395           ITBlock.push_back(firstcond);
396         mask >>= 1;
397       }
398       ITBlock.push_back(firstcond);
399     }
400
401     return true;
402   }
403
404   // We want to read exactly 4 bytes of data.
405   if (Region.readBytes(Address, 4, (uint8_t*)bytes, NULL) == -1)
406     return false;
407
408   uint32_t insn32 = (bytes[3] <<  8) |
409                     (bytes[2] <<  0) |
410                     (bytes[1] << 24) |
411                     (bytes[0] << 16);
412   MI.clear();
413   result = decodeThumbInstruction32(MI, insn32, Address, this);
414   if (result) {
415     Size = 4;
416     bool InITBlock = ITBlock.size();
417     AddThumbPredicate(MI);
418     AddThumb1SBit(MI, InITBlock);
419     return true;
420   }
421
422   MI.clear();
423   result = decodeThumb2Instruction32(MI, insn32, Address, this);
424   if (result) {
425     Size = 4;
426     AddThumbPredicate(MI);
427     return true;
428   }
429
430   MI.clear();
431   result = decodeVFPInstruction32(MI, insn32, Address, this);
432   if (result) {
433     Size = 4;
434     UpdateThumbVFPPredicate(MI);
435     return true;
436   }
437
438   MI.clear();
439   result = decodeCommonInstruction32(MI, insn32, Address, this);
440   if (result) {
441     Size = 4;
442     AddThumbPredicate(MI);
443     return true;
444   }
445
446   return false;
447 }
448
449
450 extern "C" void LLVMInitializeARMDisassembler() {
451   TargetRegistry::RegisterMCDisassembler(TheARMTarget,
452                                          createARMDisassembler);
453   TargetRegistry::RegisterMCDisassembler(TheThumbTarget,
454                                          createThumbDisassembler);
455 }
456
457 static const unsigned GPRDecoderTable[] = {
458   ARM::R0, ARM::R1, ARM::R2, ARM::R3,
459   ARM::R4, ARM::R5, ARM::R6, ARM::R7,
460   ARM::R8, ARM::R9, ARM::R10, ARM::R11,
461   ARM::R12, ARM::SP, ARM::LR, ARM::PC
462 };
463
464 static bool DecodeGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
465                                    uint64_t Address, const void *Decoder) {
466   if (RegNo > 15)
467     return false;
468
469   unsigned Register = GPRDecoderTable[RegNo];
470   Inst.addOperand(MCOperand::CreateReg(Register));
471   return true;
472 }
473
474 static bool DecodetGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
475                                    uint64_t Address, const void *Decoder) {
476   if (RegNo > 7)
477     return false;
478   return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
479 }
480
481 static bool DecodetcGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
482                                    uint64_t Address, const void *Decoder) {
483   unsigned Register = 0;
484   switch (RegNo) {
485     case 0:
486       Register = ARM::R0;
487       break;
488     case 1:
489       Register = ARM::R1;
490       break;
491     case 2:
492       Register = ARM::R2;
493       break;
494     case 3:
495       Register = ARM::R3;
496       break;
497     case 9:
498       Register = ARM::R9;
499       break;
500     case 12:
501       Register = ARM::R12;
502       break;
503     default:
504       return false;
505     }
506
507   Inst.addOperand(MCOperand::CreateReg(Register));
508   return true;
509 }
510
511 static bool DecoderGPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
512                                    uint64_t Address, const void *Decoder) {
513   if (RegNo == 13 || RegNo == 15) return false;
514   return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
515 }
516
517 static const unsigned SPRDecoderTable[] = { 
518      ARM::S0,  ARM::S1,  ARM::S2,  ARM::S3,
519      ARM::S4,  ARM::S5,  ARM::S6,  ARM::S7,
520      ARM::S8,  ARM::S9, ARM::S10, ARM::S11,
521     ARM::S12, ARM::S13, ARM::S14, ARM::S15,
522     ARM::S16, ARM::S17, ARM::S18, ARM::S19,
523     ARM::S20, ARM::S21, ARM::S22, ARM::S23,
524     ARM::S24, ARM::S25, ARM::S26, ARM::S27,
525     ARM::S28, ARM::S29, ARM::S30, ARM::S31
526 };
527
528 static bool DecodeSPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
529                                    uint64_t Address, const void *Decoder) {
530   if (RegNo > 31)
531     return false;
532
533   unsigned Register = SPRDecoderTable[RegNo];
534   Inst.addOperand(MCOperand::CreateReg(Register));
535   return true;
536 }
537
538 static const unsigned DPRDecoderTable[] = { 
539      ARM::D0,  ARM::D1,  ARM::D2,  ARM::D3,
540      ARM::D4,  ARM::D5,  ARM::D6,  ARM::D7,
541      ARM::D8,  ARM::D9, ARM::D10, ARM::D11,
542     ARM::D12, ARM::D13, ARM::D14, ARM::D15,
543     ARM::D16, ARM::D17, ARM::D18, ARM::D19,
544     ARM::D20, ARM::D21, ARM::D22, ARM::D23,
545     ARM::D24, ARM::D25, ARM::D26, ARM::D27,
546     ARM::D28, ARM::D29, ARM::D30, ARM::D31
547 };
548
549 static bool DecodeDPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
550                                    uint64_t Address, const void *Decoder) {
551   if (RegNo > 31)
552     return false;
553
554   unsigned Register = DPRDecoderTable[RegNo];
555   Inst.addOperand(MCOperand::CreateReg(Register));
556   return true;
557 }
558
559 static bool DecodeDPR_8RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
560                                    uint64_t Address, const void *Decoder) {
561   if (RegNo > 7)
562     return false;
563   return DecodeDPRRegisterClass(Inst, RegNo, Address, Decoder);
564 }
565
566 static bool DecodeDPR_VFP2RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
567                                    uint64_t Address, const void *Decoder) {
568   if (RegNo > 15)
569     return false;
570   return DecodeDPRRegisterClass(Inst, RegNo, Address, Decoder);
571 }
572
573 static const unsigned QPRDecoderTable[] = { 
574      ARM::Q0,  ARM::Q1,  ARM::Q2,  ARM::Q3,
575      ARM::Q4,  ARM::Q5,  ARM::Q6,  ARM::Q7,
576      ARM::Q8,  ARM::Q9, ARM::Q10, ARM::Q11,
577     ARM::Q12, ARM::Q13, ARM::Q14, ARM::Q15
578 };
579
580
581 static bool DecodeQPRRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
582                                    uint64_t Address, const void *Decoder) {
583   if (RegNo > 31)
584     return false;
585   RegNo >>= 1;
586
587   unsigned Register = QPRDecoderTable[RegNo];
588   Inst.addOperand(MCOperand::CreateReg(Register));
589   return true;
590 }
591
592 static bool DecodePredicateOperand(llvm::MCInst &Inst, unsigned Val,
593                                uint64_t Address, const void *Decoder) {
594   if (Val == 0xF) return false;
595   // AL predicate is not allowed on Thumb1 branches.
596   if (Inst.getOpcode() == ARM::tBcc && Val == 0xE)
597     return false;
598   Inst.addOperand(MCOperand::CreateImm(Val));
599   if (Val == ARMCC::AL) {
600     Inst.addOperand(MCOperand::CreateReg(0));
601   } else
602     Inst.addOperand(MCOperand::CreateReg(ARM::CPSR));
603   return true;
604 }
605
606 static bool DecodeCCOutOperand(llvm::MCInst &Inst, unsigned Val,
607                                uint64_t Address, const void *Decoder) {
608   if (Val)
609     Inst.addOperand(MCOperand::CreateReg(ARM::CPSR));
610   else
611     Inst.addOperand(MCOperand::CreateReg(0));
612   return true;
613 }
614
615 static bool DecodeSOImmOperand(llvm::MCInst &Inst, unsigned Val,
616                                uint64_t Address, const void *Decoder) {
617   uint32_t imm = Val & 0xFF;
618   uint32_t rot = (Val & 0xF00) >> 7;
619   uint32_t rot_imm = (imm >> rot) | (imm << (32-rot));
620   Inst.addOperand(MCOperand::CreateImm(rot_imm));
621   return true;
622 }
623
624 static bool DecodeBLTargetOperand(llvm::MCInst &Inst, unsigned Val,
625                                uint64_t Address, const void *Decoder) {
626   Val <<= 2;
627   Inst.addOperand(MCOperand::CreateImm(SignExtend32<26>(Val)));
628   return true;
629 }
630
631 static bool DecodeSORegImmOperand(llvm::MCInst &Inst, unsigned Val,
632                                uint64_t Address, const void *Decoder) {
633
634   unsigned Rm = fieldFromInstruction32(Val, 0, 4);
635   unsigned type = fieldFromInstruction32(Val, 5, 2);
636   unsigned imm = fieldFromInstruction32(Val, 7, 5);
637
638   // Register-immediate
639   DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
640
641   ARM_AM::ShiftOpc Shift = ARM_AM::lsl;
642   switch (type) {
643     case 0:
644       Shift = ARM_AM::lsl;
645       break;
646     case 1:
647       Shift = ARM_AM::lsr;
648       break;
649     case 2:
650       Shift = ARM_AM::asr;
651       break;
652     case 3:
653       Shift = ARM_AM::ror;
654       break;
655   }
656
657   if (Shift == ARM_AM::ror && imm == 0)
658     Shift = ARM_AM::rrx;
659
660   unsigned Op = Shift | (imm << 3);
661   Inst.addOperand(MCOperand::CreateImm(Op));
662
663   return true;
664 }
665
666 static bool DecodeSORegRegOperand(llvm::MCInst &Inst, unsigned Val,
667                                uint64_t Address, const void *Decoder) {
668
669   unsigned Rm = fieldFromInstruction32(Val, 0, 4);
670   unsigned type = fieldFromInstruction32(Val, 5, 2);
671   unsigned Rs = fieldFromInstruction32(Val, 8, 4);
672
673   // Register-register
674   DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
675   DecodeGPRRegisterClass(Inst, Rs, Address, Decoder);
676
677   ARM_AM::ShiftOpc Shift = ARM_AM::lsl;
678   switch (type) {
679     case 0:
680       Shift = ARM_AM::lsl;
681       break;
682     case 1:
683       Shift = ARM_AM::lsr;
684       break;
685     case 2:
686       Shift = ARM_AM::asr;
687       break;
688     case 3:
689       Shift = ARM_AM::ror;
690       break;
691   }
692
693   Inst.addOperand(MCOperand::CreateImm(Shift));
694
695   return true;
696 }
697
698 static bool DecodeRegListOperand(llvm::MCInst &Inst, unsigned Val,
699                                  uint64_t Address, const void *Decoder) {
700   for (unsigned i = 0; i < 16; ++i) {
701     if (Val & (1 << i))
702       DecodeGPRRegisterClass(Inst, i, Address, Decoder);
703   }
704
705   return true;
706 }
707
708 static bool DecodeSPRRegListOperand(llvm::MCInst &Inst, unsigned Val,
709                                  uint64_t Address, const void *Decoder) {
710   unsigned Vd = fieldFromInstruction32(Val, 8, 4);
711   unsigned regs = Val & 0xFF;
712
713   DecodeSPRRegisterClass(Inst, Vd, Address, Decoder);
714   for (unsigned i = 0; i < (regs - 1); ++i)
715     DecodeSPRRegisterClass(Inst, ++Vd, Address, Decoder);
716
717   return true;
718 }
719
720 static bool DecodeDPRRegListOperand(llvm::MCInst &Inst, unsigned Val,
721                                  uint64_t Address, const void *Decoder) {
722   unsigned Vd = fieldFromInstruction32(Val, 8, 4);
723   unsigned regs = (Val & 0xFF) / 2;
724
725   DecodeDPRRegisterClass(Inst, Vd, Address, Decoder);
726   for (unsigned i = 0; i < (regs - 1); ++i)
727     DecodeDPRRegisterClass(Inst, ++Vd, Address, Decoder);
728
729   return true;
730 }
731
732 static bool DecodeBitfieldMaskOperand(llvm::MCInst &Inst, unsigned Val,
733                                       uint64_t Address, const void *Decoder) {
734   unsigned msb = fieldFromInstruction32(Val, 5, 5);
735   unsigned lsb = fieldFromInstruction32(Val, 0, 5);
736   uint32_t msb_mask = (1 << (msb+1)) - 1;
737   uint32_t lsb_mask = (1 << lsb) - 1;
738   Inst.addOperand(MCOperand::CreateImm(~(msb_mask ^ lsb_mask)));
739   return true;
740 }
741
742 static bool DecodeCopMemInstruction(llvm::MCInst &Inst, unsigned Insn,
743                                   uint64_t Address, const void *Decoder) {
744   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
745   unsigned CRd = fieldFromInstruction32(Insn, 12, 4);
746   unsigned coproc = fieldFromInstruction32(Insn, 8, 4);
747   unsigned imm = fieldFromInstruction32(Insn, 0, 8);
748   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
749   unsigned U = fieldFromInstruction32(Insn, 23, 1);
750
751   switch (Inst.getOpcode()) {
752     case ARM::LDC_OFFSET:
753     case ARM::LDC_PRE:
754     case ARM::LDC_POST:
755     case ARM::LDC_OPTION:
756     case ARM::LDCL_OFFSET:
757     case ARM::LDCL_PRE:
758     case ARM::LDCL_POST:
759     case ARM::LDCL_OPTION:
760     case ARM::STC_OFFSET:
761     case ARM::STC_PRE:
762     case ARM::STC_POST:
763     case ARM::STC_OPTION:
764     case ARM::STCL_OFFSET:
765     case ARM::STCL_PRE:
766     case ARM::STCL_POST:
767     case ARM::STCL_OPTION:
768       if (coproc == 0xA || coproc == 0xB)
769         return false;
770       break;
771     default:
772       break;
773   }
774
775   Inst.addOperand(MCOperand::CreateImm(coproc));
776   Inst.addOperand(MCOperand::CreateImm(CRd));
777   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
778   switch (Inst.getOpcode()) {
779     case ARM::LDC_OPTION:
780     case ARM::LDCL_OPTION:
781     case ARM::LDC2_OPTION:
782     case ARM::LDC2L_OPTION:
783     case ARM::STC_OPTION:
784     case ARM::STCL_OPTION:
785     case ARM::STC2_OPTION:
786     case ARM::STC2L_OPTION:
787     case ARM::LDCL_POST:
788     case ARM::STCL_POST:
789       break;
790     default:
791       Inst.addOperand(MCOperand::CreateReg(0));
792       break;
793   }
794
795   unsigned P = fieldFromInstruction32(Insn, 24, 1);
796   unsigned W = fieldFromInstruction32(Insn, 21, 1);
797
798   bool writeback = (P == 0) || (W == 1);
799   unsigned idx_mode = 0;
800   if (P && writeback)
801     idx_mode = ARMII::IndexModePre;
802   else if (!P && writeback)
803     idx_mode = ARMII::IndexModePost;
804
805   switch (Inst.getOpcode()) {
806     case ARM::LDCL_POST:
807     case ARM::STCL_POST:
808       imm |= U << 8;
809     case ARM::LDC_OPTION:
810     case ARM::LDCL_OPTION:
811     case ARM::LDC2_OPTION:
812     case ARM::LDC2L_OPTION:
813     case ARM::STC_OPTION:
814     case ARM::STCL_OPTION:
815     case ARM::STC2_OPTION:
816     case ARM::STC2L_OPTION:
817       Inst.addOperand(MCOperand::CreateImm(imm));
818       break;
819     default:
820       if (U)
821         Inst.addOperand(MCOperand::CreateImm(
822             ARM_AM::getAM2Opc(ARM_AM::add, imm, ARM_AM::lsl, idx_mode)));
823       else
824         Inst.addOperand(MCOperand::CreateImm(
825             ARM_AM::getAM2Opc(ARM_AM::sub, imm, ARM_AM::lsl, idx_mode)));
826       break;
827   }
828
829   switch (Inst.getOpcode()) {
830     case ARM::LDC_OFFSET:
831     case ARM::LDC_PRE:
832     case ARM::LDC_POST:
833     case ARM::LDC_OPTION:
834     case ARM::LDCL_OFFSET:
835     case ARM::LDCL_PRE:
836     case ARM::LDCL_POST:
837     case ARM::LDCL_OPTION:
838     case ARM::STC_OFFSET:
839     case ARM::STC_PRE:
840     case ARM::STC_POST:
841     case ARM::STC_OPTION:
842     case ARM::STCL_OFFSET:
843     case ARM::STCL_PRE:
844     case ARM::STCL_POST:
845     case ARM::STCL_OPTION:
846       if (!DecodePredicateOperand(Inst, pred, Address, Decoder)) return false;
847       break;
848     default:
849       break;
850   }
851
852   return true;
853 }
854
855 static bool DecodeAddrMode2IdxInstruction(llvm::MCInst &Inst, unsigned Insn,
856                                   uint64_t Address, const void *Decoder) {
857   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
858   unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
859   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
860   unsigned imm = fieldFromInstruction32(Insn, 0, 12);
861   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
862   unsigned reg = fieldFromInstruction32(Insn, 25, 1);
863   unsigned P = fieldFromInstruction32(Insn, 24, 1);
864   unsigned W = fieldFromInstruction32(Insn, 21, 1);
865
866   // On stores, the writeback operand precedes Rt.
867   switch (Inst.getOpcode()) {
868     case ARM::STR_POST_IMM:
869     case ARM::STR_POST_REG:
870     case ARM::STRTr:
871     case ARM::STRTi:
872     case ARM::STRBTr:
873     case ARM::STRBTi:
874       DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
875       break;
876     default:
877       break;
878   }
879
880   DecodeGPRRegisterClass(Inst, Rt, Address, Decoder);
881
882   // On loads, the writeback operand comes after Rt.
883   switch (Inst.getOpcode()) {
884     case ARM::LDR_POST_IMM:
885     case ARM::LDR_POST_REG:
886     case ARM::LDR_PRE:
887     case ARM::LDRBT_POST_REG:
888     case ARM::LDRBT_POST_IMM:
889     case ARM::LDRTr:
890     case ARM::LDRTi:
891       DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
892       break;
893     default:
894       break;
895   }
896
897   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
898
899   ARM_AM::AddrOpc Op = ARM_AM::add;
900   if (!fieldFromInstruction32(Insn, 23, 1))
901     Op = ARM_AM::sub;
902
903   bool writeback = (P == 0) || (W == 1);
904   unsigned idx_mode = 0;
905   if (P && writeback)
906     idx_mode = ARMII::IndexModePre;
907   else if (!P && writeback)
908     idx_mode = ARMII::IndexModePost;
909
910   if (reg) {
911     DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
912     ARM_AM::ShiftOpc Opc = ARM_AM::lsl;
913     switch( fieldFromInstruction32(Insn, 5, 2)) {
914       case 0:
915         Opc = ARM_AM::lsl;
916         break;
917       case 1:
918         Opc = ARM_AM::lsr;
919         break;
920       case 2:
921         Opc = ARM_AM::asr;
922         break;
923       case 3:
924         Opc = ARM_AM::ror;
925         break;
926       default:
927         return false;
928     }
929     unsigned amt = fieldFromInstruction32(Insn, 7, 5);
930     unsigned imm = ARM_AM::getAM2Opc(Op, amt, Opc, idx_mode);
931
932     Inst.addOperand(MCOperand::CreateImm(imm));
933   } else {
934     Inst.addOperand(MCOperand::CreateReg(0));
935     unsigned tmp = ARM_AM::getAM2Opc(Op, imm, ARM_AM::lsl, idx_mode);
936     Inst.addOperand(MCOperand::CreateImm(tmp));
937   }
938
939   if (!DecodePredicateOperand(Inst, pred, Address, Decoder)) return false;
940
941   return true;
942 }
943
944 static bool DecodeSORegMemOperand(llvm::MCInst &Inst, unsigned Val,
945                                   uint64_t Address, const void *Decoder) {
946   unsigned Rn = fieldFromInstruction32(Val, 13, 4);
947   unsigned Rm = fieldFromInstruction32(Val,  0, 4);
948   unsigned type = fieldFromInstruction32(Val, 5, 2);
949   unsigned imm = fieldFromInstruction32(Val, 7, 5);
950   unsigned U = fieldFromInstruction32(Val, 12, 1);
951
952   ARM_AM::ShiftOpc ShOp = ARM_AM::lsl;
953   switch (type) {
954     case 0:
955       ShOp = ARM_AM::lsl;
956       break;
957     case 1:
958       ShOp = ARM_AM::lsr;
959       break;
960     case 2:
961       ShOp = ARM_AM::asr;
962       break;
963     case 3:
964       ShOp = ARM_AM::ror;
965       break;
966   }
967
968   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
969   DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
970   unsigned shift;
971   if (U)
972     shift = ARM_AM::getAM2Opc(ARM_AM::add, imm, ShOp);
973   else
974     shift = ARM_AM::getAM2Opc(ARM_AM::sub, imm, ShOp);
975   Inst.addOperand(MCOperand::CreateImm(shift));
976
977   return true;
978 }
979
980 static bool DecodeAddrMode3Instruction(llvm::MCInst &Inst, unsigned Insn,
981                                   uint64_t Address, const void *Decoder) {
982   unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
983   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
984   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
985   unsigned type = fieldFromInstruction32(Insn, 22, 1);
986   unsigned imm = fieldFromInstruction32(Insn, 8, 4);
987   unsigned U = ((~fieldFromInstruction32(Insn, 23, 1)) & 1) << 8;
988   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
989   unsigned W = fieldFromInstruction32(Insn, 21, 1);
990   unsigned P = fieldFromInstruction32(Insn, 24, 1);
991
992   bool writeback = (W == 1) | (P == 0);
993   if (writeback) { // Writeback
994     if (P)
995       U |= ARMII::IndexModePre << 9;
996     else
997       U |= ARMII::IndexModePost << 9;
998
999     // On stores, the writeback operand precedes Rt.
1000     switch (Inst.getOpcode()) {
1001     case ARM::STRD:
1002     case ARM::STRD_PRE:
1003     case ARM::STRD_POST:
1004       DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1005       break;
1006     default:
1007       break;
1008     }
1009   }
1010
1011   DecodeGPRRegisterClass(Inst, Rt, Address, Decoder);
1012   switch (Inst.getOpcode()) {
1013     case ARM::STRD:
1014     case ARM::STRD_PRE:
1015     case ARM::STRD_POST:
1016     case ARM::LDRD:
1017     case ARM::LDRD_PRE:
1018     case ARM::LDRD_POST:
1019       DecodeGPRRegisterClass(Inst, Rt+1, Address, Decoder);
1020       break;
1021     default:
1022       break;
1023   }
1024
1025   if (writeback) {
1026     // On loads, the writeback operand comes after Rt.
1027     switch (Inst.getOpcode()) {
1028     case ARM::LDRD:
1029     case ARM::LDRD_PRE:
1030     case ARM::LDRD_POST:
1031     case ARM::LDRHTr:
1032     case ARM::LDRSBTr:
1033       DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1034       break;
1035     default:
1036       break;
1037     }
1038   }
1039
1040   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1041
1042   if (type) {
1043     Inst.addOperand(MCOperand::CreateReg(0));
1044     Inst.addOperand(MCOperand::CreateImm(U | (imm << 4) | Rm));
1045   } else {
1046     DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1047     Inst.addOperand(MCOperand::CreateImm(U));
1048   }
1049
1050   if (!DecodePredicateOperand(Inst, pred, Address, Decoder)) return false;
1051
1052   return true;
1053 }
1054
1055 static bool DecodeRFEInstruction(llvm::MCInst &Inst, unsigned Insn,
1056                                  uint64_t Address, const void *Decoder) {
1057   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1058   unsigned mode = fieldFromInstruction32(Insn, 23, 2);
1059
1060   switch (mode) {
1061     case 0:
1062       mode = ARM_AM::da;
1063       break;
1064     case 1:
1065       mode = ARM_AM::ia;
1066       break;
1067     case 2:
1068       mode = ARM_AM::db;
1069       break;
1070     case 3:
1071       mode = ARM_AM::ib;
1072       break;
1073   }
1074
1075   Inst.addOperand(MCOperand::CreateImm(mode));
1076   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1077
1078   return true;
1079 }
1080
1081 static bool DecodeMemMultipleWritebackInstruction(llvm::MCInst &Inst,
1082                                   unsigned Insn,
1083                                   uint64_t Address, const void *Decoder) {
1084   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1085   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
1086   unsigned reglist = fieldFromInstruction32(Insn, 0, 16);
1087
1088   if (pred == 0xF) {
1089     switch (Inst.getOpcode()) {
1090       case ARM::STMDA:
1091         Inst.setOpcode(ARM::RFEDA);
1092         break;
1093       case ARM::STMDA_UPD:
1094         Inst.setOpcode(ARM::RFEDA_UPD);
1095         break;
1096       case ARM::STMDB:
1097         Inst.setOpcode(ARM::RFEDB);
1098         break;
1099       case ARM::STMDB_UPD:
1100         Inst.setOpcode(ARM::RFEDB_UPD);
1101         break;
1102       case ARM::STMIA:
1103         Inst.setOpcode(ARM::RFEIA);
1104         break;
1105       case ARM::STMIA_UPD:
1106         Inst.setOpcode(ARM::RFEIA_UPD);
1107         break;
1108       case ARM::STMIB:
1109         Inst.setOpcode(ARM::RFEIB);
1110         break;
1111       case ARM::STMIB_UPD:
1112         Inst.setOpcode(ARM::RFEIB_UPD);
1113         break;
1114
1115     }
1116     return DecodeRFEInstruction(Inst, Insn, Address, Decoder);
1117   }
1118
1119   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1120   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder); // Tied
1121   if (!DecodePredicateOperand(Inst, pred, Address, Decoder)) return false;
1122   DecodeRegListOperand(Inst, reglist, Address, Decoder);
1123
1124   return true;
1125 }
1126
1127 static bool DecodeCPSInstruction(llvm::MCInst &Inst, unsigned Insn,
1128                                  uint64_t Address, const void *Decoder) {
1129   unsigned imod = fieldFromInstruction32(Insn, 18, 2);
1130   unsigned M = fieldFromInstruction32(Insn, 17, 1);
1131   unsigned iflags = fieldFromInstruction32(Insn, 6, 3);
1132   unsigned mode = fieldFromInstruction32(Insn, 0, 5);
1133
1134   if (M && mode && imod && iflags) {
1135     Inst.setOpcode(ARM::CPS3p);
1136     Inst.addOperand(MCOperand::CreateImm(imod));
1137     Inst.addOperand(MCOperand::CreateImm(iflags));
1138     Inst.addOperand(MCOperand::CreateImm(mode));
1139     return true;
1140   } else if (!mode && !M) {
1141     Inst.setOpcode(ARM::CPS2p);
1142     Inst.addOperand(MCOperand::CreateImm(imod));
1143     Inst.addOperand(MCOperand::CreateImm(iflags));
1144     return true;
1145   } else if (!imod && !iflags && M) {
1146     Inst.setOpcode(ARM::CPS1p);
1147     Inst.addOperand(MCOperand::CreateImm(mode));
1148     return true;
1149   }
1150
1151   return false;
1152 }
1153
1154 static bool DecodeSMLAInstruction(llvm::MCInst &Inst, unsigned Insn,
1155                                  uint64_t Address, const void *Decoder) {
1156   unsigned Rd = fieldFromInstruction32(Insn, 16, 4);
1157   unsigned Rn = fieldFromInstruction32(Insn, 0, 4);
1158   unsigned Rm = fieldFromInstruction32(Insn, 8, 4);
1159   unsigned Ra = fieldFromInstruction32(Insn, 12, 4);
1160   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
1161
1162   if (pred == 0xF)
1163     return DecodeCPSInstruction(Inst, Insn, Address, Decoder);
1164
1165   DecodeGPRRegisterClass(Inst, Rd, Address, Decoder);
1166   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1167   DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1168   DecodeGPRRegisterClass(Inst, Ra, Address, Decoder);
1169
1170   return true;
1171 }
1172
1173 static bool DecodeAddrModeImm12Operand(llvm::MCInst &Inst, unsigned Val,
1174                            uint64_t Address, const void *Decoder) {
1175   unsigned add = fieldFromInstruction32(Val, 12, 1);
1176   unsigned imm = fieldFromInstruction32(Val, 0, 12);
1177   unsigned Rn = fieldFromInstruction32(Val, 13, 4);
1178
1179   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1180
1181   if (!add) imm *= -1;
1182   if (imm == 0 && !add) imm = INT32_MIN;
1183   Inst.addOperand(MCOperand::CreateImm(imm));
1184
1185   return true;
1186 }
1187
1188 static bool DecodeAddrMode5Operand(llvm::MCInst &Inst, unsigned Val,
1189                                    uint64_t Address, const void *Decoder) {
1190   unsigned Rn = fieldFromInstruction32(Val, 9, 4);
1191   unsigned U = fieldFromInstruction32(Val, 8, 1);
1192   unsigned imm = fieldFromInstruction32(Val, 0, 8);
1193
1194   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1195
1196   if (U)
1197     Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add, imm)));
1198   else
1199     Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub, imm)));
1200
1201   return true;
1202 }
1203
1204 static bool DecodeAddrMode7Operand(llvm::MCInst &Inst, unsigned Val,
1205                                    uint64_t Address, const void *Decoder) {
1206   return DecodeGPRRegisterClass(Inst, Val, Address, Decoder);
1207 }
1208
1209 static bool DecodeBranchImmInstruction(llvm::MCInst &Inst, unsigned Insn,
1210                                    uint64_t Address, const void *Decoder) {
1211   unsigned pred = fieldFromInstruction32(Insn, 28, 4);
1212   unsigned imm = fieldFromInstruction32(Insn, 0, 24) << 2;
1213
1214   if (pred == 0xF) {
1215     Inst.setOpcode(ARM::BLXi);
1216     imm |= fieldFromInstruction32(Insn, 24, 1) << 1;
1217     Inst.addOperand(MCOperand::CreateImm(SignExtend32<26>(imm)));
1218     return true;
1219   }
1220
1221   Inst.addOperand(MCOperand::CreateImm(SignExtend32<26>(imm)));
1222   if (!DecodePredicateOperand(Inst, pred, Address, Decoder)) return false;
1223
1224   return true;
1225 }
1226
1227
1228 static bool DecodeVCVTImmOperand(llvm::MCInst &Inst, unsigned Val,
1229                                  uint64_t Address, const void *Decoder) {
1230   Inst.addOperand(MCOperand::CreateImm(64 - Val));
1231   return true;
1232 }
1233
1234 static bool DecodeAddrMode6Operand(llvm::MCInst &Inst, unsigned Val,
1235                                    uint64_t Address, const void *Decoder) {
1236   unsigned Rm = fieldFromInstruction32(Val, 0, 4);
1237   unsigned align = fieldFromInstruction32(Val, 4, 2);
1238
1239   DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1240   if (!align)
1241     Inst.addOperand(MCOperand::CreateImm(0));
1242   else
1243     Inst.addOperand(MCOperand::CreateImm(4 << align));
1244
1245   return true;
1246 }
1247
1248 static bool DecodeVLDInstruction(llvm::MCInst &Inst, unsigned Insn,
1249                                    uint64_t Address, const void *Decoder) {
1250   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1251   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1252   unsigned wb = fieldFromInstruction32(Insn, 16, 4);
1253   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1254   Rn |= fieldFromInstruction32(Insn, 4, 2) << 4;
1255   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1256
1257   // First output register
1258   DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1259
1260   // Second output register
1261   switch (Inst.getOpcode()) {
1262     case ARM::VLD1q8:
1263     case ARM::VLD1q16:
1264     case ARM::VLD1q32:
1265     case ARM::VLD1q64:
1266     case ARM::VLD1q8_UPD:
1267     case ARM::VLD1q16_UPD:
1268     case ARM::VLD1q32_UPD:
1269     case ARM::VLD1q64_UPD:
1270     case ARM::VLD1d8T:
1271     case ARM::VLD1d16T:
1272     case ARM::VLD1d32T:
1273     case ARM::VLD1d64T:
1274     case ARM::VLD1d8T_UPD:
1275     case ARM::VLD1d16T_UPD:
1276     case ARM::VLD1d32T_UPD:
1277     case ARM::VLD1d64T_UPD:
1278     case ARM::VLD1d8Q:
1279     case ARM::VLD1d16Q:
1280     case ARM::VLD1d32Q:
1281     case ARM::VLD1d64Q:
1282     case ARM::VLD1d8Q_UPD:
1283     case ARM::VLD1d16Q_UPD:
1284     case ARM::VLD1d32Q_UPD:
1285     case ARM::VLD1d64Q_UPD:
1286     case ARM::VLD2d8:
1287     case ARM::VLD2d16:
1288     case ARM::VLD2d32:
1289     case ARM::VLD2d8_UPD:
1290     case ARM::VLD2d16_UPD:
1291     case ARM::VLD2d32_UPD:
1292     case ARM::VLD2q8:
1293     case ARM::VLD2q16:
1294     case ARM::VLD2q32:
1295     case ARM::VLD2q8_UPD:
1296     case ARM::VLD2q16_UPD:
1297     case ARM::VLD2q32_UPD:
1298     case ARM::VLD3d8:
1299     case ARM::VLD3d16:
1300     case ARM::VLD3d32:
1301     case ARM::VLD3d8_UPD:
1302     case ARM::VLD3d16_UPD:
1303     case ARM::VLD3d32_UPD:
1304     case ARM::VLD4d8:
1305     case ARM::VLD4d16:
1306     case ARM::VLD4d32:
1307     case ARM::VLD4d8_UPD:
1308     case ARM::VLD4d16_UPD:
1309     case ARM::VLD4d32_UPD:
1310       DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder);
1311       break;
1312     case ARM::VLD2b8:
1313     case ARM::VLD2b16:
1314     case ARM::VLD2b32:
1315     case ARM::VLD2b8_UPD:
1316     case ARM::VLD2b16_UPD:
1317     case ARM::VLD2b32_UPD:
1318     case ARM::VLD3q8:
1319     case ARM::VLD3q16:
1320     case ARM::VLD3q32:
1321     case ARM::VLD3q8_UPD:
1322     case ARM::VLD3q16_UPD:
1323     case ARM::VLD3q32_UPD:
1324     case ARM::VLD4q8:
1325     case ARM::VLD4q16:
1326     case ARM::VLD4q32:
1327     case ARM::VLD4q8_UPD:
1328     case ARM::VLD4q16_UPD:
1329     case ARM::VLD4q32_UPD:
1330       DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder);
1331     default:
1332       break;
1333   }
1334
1335   // Third output register
1336   switch(Inst.getOpcode()) {
1337     case ARM::VLD1d8T:
1338     case ARM::VLD1d16T:
1339     case ARM::VLD1d32T:
1340     case ARM::VLD1d64T:
1341     case ARM::VLD1d8T_UPD:
1342     case ARM::VLD1d16T_UPD:
1343     case ARM::VLD1d32T_UPD:
1344     case ARM::VLD1d64T_UPD:
1345     case ARM::VLD1d8Q:
1346     case ARM::VLD1d16Q:
1347     case ARM::VLD1d32Q:
1348     case ARM::VLD1d64Q:
1349     case ARM::VLD1d8Q_UPD:
1350     case ARM::VLD1d16Q_UPD:
1351     case ARM::VLD1d32Q_UPD:
1352     case ARM::VLD1d64Q_UPD:
1353     case ARM::VLD2q8:
1354     case ARM::VLD2q16:
1355     case ARM::VLD2q32:
1356     case ARM::VLD2q8_UPD:
1357     case ARM::VLD2q16_UPD:
1358     case ARM::VLD2q32_UPD:
1359     case ARM::VLD3d8:
1360     case ARM::VLD3d16:
1361     case ARM::VLD3d32:
1362     case ARM::VLD3d8_UPD:
1363     case ARM::VLD3d16_UPD:
1364     case ARM::VLD3d32_UPD:
1365     case ARM::VLD4d8:
1366     case ARM::VLD4d16:
1367     case ARM::VLD4d32:
1368     case ARM::VLD4d8_UPD:
1369     case ARM::VLD4d16_UPD:
1370     case ARM::VLD4d32_UPD:
1371       DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder);
1372       break;
1373     case ARM::VLD3q8:
1374     case ARM::VLD3q16:
1375     case ARM::VLD3q32:
1376     case ARM::VLD3q8_UPD:
1377     case ARM::VLD3q16_UPD:
1378     case ARM::VLD3q32_UPD:
1379     case ARM::VLD4q8:
1380     case ARM::VLD4q16:
1381     case ARM::VLD4q32:
1382     case ARM::VLD4q8_UPD:
1383     case ARM::VLD4q16_UPD:
1384     case ARM::VLD4q32_UPD:
1385       DecodeDPRRegisterClass(Inst, (Rd+4)%32, Address, Decoder);
1386       break;
1387     default:
1388       break;
1389   }
1390
1391   // Fourth output register
1392   switch (Inst.getOpcode()) {
1393     case ARM::VLD1d8Q:
1394     case ARM::VLD1d16Q:
1395     case ARM::VLD1d32Q:
1396     case ARM::VLD1d64Q:
1397     case ARM::VLD1d8Q_UPD:
1398     case ARM::VLD1d16Q_UPD:
1399     case ARM::VLD1d32Q_UPD:
1400     case ARM::VLD1d64Q_UPD:
1401     case ARM::VLD2q8:
1402     case ARM::VLD2q16:
1403     case ARM::VLD2q32:
1404     case ARM::VLD2q8_UPD:
1405     case ARM::VLD2q16_UPD:
1406     case ARM::VLD2q32_UPD:
1407     case ARM::VLD4d8:
1408     case ARM::VLD4d16:
1409     case ARM::VLD4d32:
1410     case ARM::VLD4d8_UPD:
1411     case ARM::VLD4d16_UPD:
1412     case ARM::VLD4d32_UPD:
1413       DecodeDPRRegisterClass(Inst, (Rd+3)%32, Address, Decoder);
1414       break;
1415     case ARM::VLD4q8:
1416     case ARM::VLD4q16:
1417     case ARM::VLD4q32:
1418     case ARM::VLD4q8_UPD:
1419     case ARM::VLD4q16_UPD:
1420     case ARM::VLD4q32_UPD:
1421       DecodeDPRRegisterClass(Inst, (Rd+6)%32, Address, Decoder);
1422       break;
1423     default:
1424       break;
1425   }
1426
1427   // Writeback operand
1428   switch (Inst.getOpcode()) {
1429     case ARM::VLD1d8_UPD:
1430     case ARM::VLD1d16_UPD:
1431     case ARM::VLD1d32_UPD:
1432     case ARM::VLD1d64_UPD:
1433     case ARM::VLD1q8_UPD:
1434     case ARM::VLD1q16_UPD:
1435     case ARM::VLD1q32_UPD:
1436     case ARM::VLD1q64_UPD:
1437     case ARM::VLD1d8T_UPD:
1438     case ARM::VLD1d16T_UPD:
1439     case ARM::VLD1d32T_UPD:
1440     case ARM::VLD1d64T_UPD:
1441     case ARM::VLD1d8Q_UPD:
1442     case ARM::VLD1d16Q_UPD:
1443     case ARM::VLD1d32Q_UPD:
1444     case ARM::VLD1d64Q_UPD:
1445     case ARM::VLD2d8_UPD:
1446     case ARM::VLD2d16_UPD:
1447     case ARM::VLD2d32_UPD:
1448     case ARM::VLD2q8_UPD:
1449     case ARM::VLD2q16_UPD:
1450     case ARM::VLD2q32_UPD:
1451     case ARM::VLD2b8_UPD:
1452     case ARM::VLD2b16_UPD:
1453     case ARM::VLD2b32_UPD:
1454     case ARM::VLD3d8_UPD:
1455     case ARM::VLD3d16_UPD:
1456     case ARM::VLD3d32_UPD:
1457     case ARM::VLD3q8_UPD:
1458     case ARM::VLD3q16_UPD:
1459     case ARM::VLD3q32_UPD:
1460     case ARM::VLD4d8_UPD:
1461     case ARM::VLD4d16_UPD:
1462     case ARM::VLD4d32_UPD:
1463     case ARM::VLD4q8_UPD:
1464     case ARM::VLD4q16_UPD:
1465     case ARM::VLD4q32_UPD:
1466       DecodeGPRRegisterClass(Inst, wb, Address, Decoder);
1467       break;
1468     default:
1469       break;
1470   }
1471
1472   // AddrMode6 Base (register+alignment)
1473   DecodeAddrMode6Operand(Inst, Rn, Address, Decoder);
1474
1475   // AddrMode6 Offset (register)
1476   if (Rm == 0xD)
1477     Inst.addOperand(MCOperand::CreateReg(0));
1478   else if (Rm != 0xF)
1479     DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1480
1481   return true;
1482 }
1483
1484 static bool DecodeVSTInstruction(llvm::MCInst &Inst, unsigned Insn,
1485                                  uint64_t Address, const void *Decoder) {
1486   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1487   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1488   unsigned wb = fieldFromInstruction32(Insn, 16, 4);
1489   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1490   Rn |= fieldFromInstruction32(Insn, 4, 2) << 4;
1491   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1492
1493   // Writeback Operand
1494   switch (Inst.getOpcode()) {
1495     case ARM::VST1d8_UPD:
1496     case ARM::VST1d16_UPD:
1497     case ARM::VST1d32_UPD:
1498     case ARM::VST1d64_UPD:
1499     case ARM::VST1q8_UPD:
1500     case ARM::VST1q16_UPD:
1501     case ARM::VST1q32_UPD:
1502     case ARM::VST1q64_UPD:
1503     case ARM::VST1d8T_UPD:
1504     case ARM::VST1d16T_UPD:
1505     case ARM::VST1d32T_UPD:
1506     case ARM::VST1d64T_UPD:
1507     case ARM::VST1d8Q_UPD:
1508     case ARM::VST1d16Q_UPD:
1509     case ARM::VST1d32Q_UPD:
1510     case ARM::VST1d64Q_UPD:
1511     case ARM::VST2d8_UPD:
1512     case ARM::VST2d16_UPD:
1513     case ARM::VST2d32_UPD:
1514     case ARM::VST2q8_UPD:
1515     case ARM::VST2q16_UPD:
1516     case ARM::VST2q32_UPD:
1517     case ARM::VST2b8_UPD:
1518     case ARM::VST2b16_UPD:
1519     case ARM::VST2b32_UPD:
1520     case ARM::VST3d8_UPD:
1521     case ARM::VST3d16_UPD:
1522     case ARM::VST3d32_UPD:
1523     case ARM::VST3q8_UPD:
1524     case ARM::VST3q16_UPD:
1525     case ARM::VST3q32_UPD:
1526     case ARM::VST4d8_UPD:
1527     case ARM::VST4d16_UPD:
1528     case ARM::VST4d32_UPD:
1529     case ARM::VST4q8_UPD:
1530     case ARM::VST4q16_UPD:
1531     case ARM::VST4q32_UPD:
1532       DecodeGPRRegisterClass(Inst, wb, Address, Decoder);
1533       break;
1534     default:
1535       break;
1536   }
1537
1538   // AddrMode6 Base (register+alignment)
1539   DecodeAddrMode6Operand(Inst, Rn, Address, Decoder);
1540
1541   // AddrMode6 Offset (register)
1542   if (Rm == 0xD)
1543     Inst.addOperand(MCOperand::CreateReg(0));
1544   else if (Rm != 0xF)
1545     DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1546
1547   // First input register
1548   DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1549
1550   // Second input register
1551   switch (Inst.getOpcode()) {
1552     case ARM::VST1q8:
1553     case ARM::VST1q16:
1554     case ARM::VST1q32:
1555     case ARM::VST1q64:
1556     case ARM::VST1q8_UPD:
1557     case ARM::VST1q16_UPD:
1558     case ARM::VST1q32_UPD:
1559     case ARM::VST1q64_UPD:
1560     case ARM::VST1d8T:
1561     case ARM::VST1d16T:
1562     case ARM::VST1d32T:
1563     case ARM::VST1d64T:
1564     case ARM::VST1d8T_UPD:
1565     case ARM::VST1d16T_UPD:
1566     case ARM::VST1d32T_UPD:
1567     case ARM::VST1d64T_UPD:
1568     case ARM::VST1d8Q:
1569     case ARM::VST1d16Q:
1570     case ARM::VST1d32Q:
1571     case ARM::VST1d64Q:
1572     case ARM::VST1d8Q_UPD:
1573     case ARM::VST1d16Q_UPD:
1574     case ARM::VST1d32Q_UPD:
1575     case ARM::VST1d64Q_UPD:
1576     case ARM::VST2d8:
1577     case ARM::VST2d16:
1578     case ARM::VST2d32:
1579     case ARM::VST2d8_UPD:
1580     case ARM::VST2d16_UPD:
1581     case ARM::VST2d32_UPD:
1582     case ARM::VST2q8:
1583     case ARM::VST2q16:
1584     case ARM::VST2q32:
1585     case ARM::VST2q8_UPD:
1586     case ARM::VST2q16_UPD:
1587     case ARM::VST2q32_UPD:
1588     case ARM::VST3d8:
1589     case ARM::VST3d16:
1590     case ARM::VST3d32:
1591     case ARM::VST3d8_UPD:
1592     case ARM::VST3d16_UPD:
1593     case ARM::VST3d32_UPD:
1594     case ARM::VST4d8:
1595     case ARM::VST4d16:
1596     case ARM::VST4d32:
1597     case ARM::VST4d8_UPD:
1598     case ARM::VST4d16_UPD:
1599     case ARM::VST4d32_UPD:
1600       DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder);
1601       break;
1602     case ARM::VST2b8:
1603     case ARM::VST2b16:
1604     case ARM::VST2b32:
1605     case ARM::VST2b8_UPD:
1606     case ARM::VST2b16_UPD:
1607     case ARM::VST2b32_UPD:
1608     case ARM::VST3q8:
1609     case ARM::VST3q16:
1610     case ARM::VST3q32:
1611     case ARM::VST3q8_UPD:
1612     case ARM::VST3q16_UPD:
1613     case ARM::VST3q32_UPD:
1614     case ARM::VST4q8:
1615     case ARM::VST4q16:
1616     case ARM::VST4q32:
1617     case ARM::VST4q8_UPD:
1618     case ARM::VST4q16_UPD:
1619     case ARM::VST4q32_UPD:
1620       DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder);
1621       break;
1622     default:
1623       break;
1624   }
1625
1626   // Third input register
1627   switch (Inst.getOpcode()) {
1628     case ARM::VST1d8T:
1629     case ARM::VST1d16T:
1630     case ARM::VST1d32T:
1631     case ARM::VST1d64T:
1632     case ARM::VST1d8T_UPD:
1633     case ARM::VST1d16T_UPD:
1634     case ARM::VST1d32T_UPD:
1635     case ARM::VST1d64T_UPD:
1636     case ARM::VST1d8Q:
1637     case ARM::VST1d16Q:
1638     case ARM::VST1d32Q:
1639     case ARM::VST1d64Q:
1640     case ARM::VST1d8Q_UPD:
1641     case ARM::VST1d16Q_UPD:
1642     case ARM::VST1d32Q_UPD:
1643     case ARM::VST1d64Q_UPD:
1644     case ARM::VST2q8:
1645     case ARM::VST2q16:
1646     case ARM::VST2q32:
1647     case ARM::VST2q8_UPD:
1648     case ARM::VST2q16_UPD:
1649     case ARM::VST2q32_UPD:
1650     case ARM::VST3d8:
1651     case ARM::VST3d16:
1652     case ARM::VST3d32:
1653     case ARM::VST3d8_UPD:
1654     case ARM::VST3d16_UPD:
1655     case ARM::VST3d32_UPD:
1656     case ARM::VST4d8:
1657     case ARM::VST4d16:
1658     case ARM::VST4d32:
1659     case ARM::VST4d8_UPD:
1660     case ARM::VST4d16_UPD:
1661     case ARM::VST4d32_UPD:
1662       DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder);
1663       break;
1664     case ARM::VST3q8:
1665     case ARM::VST3q16:
1666     case ARM::VST3q32:
1667     case ARM::VST3q8_UPD:
1668     case ARM::VST3q16_UPD:
1669     case ARM::VST3q32_UPD:
1670     case ARM::VST4q8:
1671     case ARM::VST4q16:
1672     case ARM::VST4q32:
1673     case ARM::VST4q8_UPD:
1674     case ARM::VST4q16_UPD:
1675     case ARM::VST4q32_UPD:
1676       DecodeDPRRegisterClass(Inst, (Rd+4)%32, Address, Decoder);
1677       break;
1678     default:
1679       break;
1680   }
1681
1682   // Fourth input register
1683   switch (Inst.getOpcode()) {
1684     case ARM::VST1d8Q:
1685     case ARM::VST1d16Q:
1686     case ARM::VST1d32Q:
1687     case ARM::VST1d64Q:
1688     case ARM::VST1d8Q_UPD:
1689     case ARM::VST1d16Q_UPD:
1690     case ARM::VST1d32Q_UPD:
1691     case ARM::VST1d64Q_UPD:
1692     case ARM::VST2q8:
1693     case ARM::VST2q16:
1694     case ARM::VST2q32:
1695     case ARM::VST2q8_UPD:
1696     case ARM::VST2q16_UPD:
1697     case ARM::VST2q32_UPD:
1698     case ARM::VST4d8:
1699     case ARM::VST4d16:
1700     case ARM::VST4d32:
1701     case ARM::VST4d8_UPD:
1702     case ARM::VST4d16_UPD:
1703     case ARM::VST4d32_UPD:
1704       DecodeDPRRegisterClass(Inst, (Rd+3)%32, Address, Decoder);
1705       break;
1706     case ARM::VST4q8:
1707     case ARM::VST4q16:
1708     case ARM::VST4q32:
1709     case ARM::VST4q8_UPD:
1710     case ARM::VST4q16_UPD:
1711     case ARM::VST4q32_UPD:
1712       DecodeDPRRegisterClass(Inst, (Rd+6)%32, Address, Decoder);
1713       break;
1714     default:
1715       break;
1716   }
1717
1718   return true;
1719 }
1720
1721 static bool DecodeVLD1DupInstruction(llvm::MCInst &Inst, unsigned Insn,
1722                                     uint64_t Address, const void *Decoder) {
1723   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1724   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1725   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1726   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1727   unsigned align = fieldFromInstruction32(Insn, 4, 1);
1728   unsigned size = fieldFromInstruction32(Insn, 6, 2);
1729   unsigned regs = fieldFromInstruction32(Insn, 5, 1) + 1;
1730
1731   align *= (1 << size);
1732
1733   DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1734   if (regs == 2) DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder);
1735   if (Rm == 0xD) DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1736
1737   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1738   Inst.addOperand(MCOperand::CreateImm(align));
1739
1740   if (Rm == 0xD)
1741     Inst.addOperand(MCOperand::CreateReg(0));
1742   else if (Rm != 0xF)
1743     DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1744
1745   return true;
1746 }
1747
1748 static bool DecodeVLD2DupInstruction(llvm::MCInst &Inst, unsigned Insn,
1749                                     uint64_t Address, const void *Decoder) {
1750   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1751   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1752   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1753   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1754   unsigned align = fieldFromInstruction32(Insn, 4, 1);
1755   unsigned size = 1 << fieldFromInstruction32(Insn, 6, 2);
1756   unsigned inc = fieldFromInstruction32(Insn, 5, 1) + 1;
1757   align *= 2*size;
1758
1759   DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1760   DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder);
1761   if (Rm == 0xD) DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1762
1763   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1764   Inst.addOperand(MCOperand::CreateImm(align));
1765
1766   if (Rm == 0xD)
1767     Inst.addOperand(MCOperand::CreateReg(0));
1768   else if (Rm != 0xF)
1769     DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1770
1771   return true;
1772 }
1773
1774 static bool DecodeVLD3DupInstruction(llvm::MCInst &Inst, unsigned Insn,
1775                                     uint64_t Address, const void *Decoder) {
1776   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1777   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1778   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1779   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1780   unsigned inc = fieldFromInstruction32(Insn, 5, 1) + 1;
1781
1782   DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1783   DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder);
1784   DecodeDPRRegisterClass(Inst, (Rd+2*inc)%32, Address, Decoder);
1785   if (Rm == 0xD) DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1786
1787   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1788   Inst.addOperand(MCOperand::CreateImm(0));
1789
1790   if (Rm == 0xD)
1791     Inst.addOperand(MCOperand::CreateReg(0));
1792   else if (Rm != 0xF)
1793     DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1794
1795   return true;
1796 }
1797
1798 static bool DecodeVLD4DupInstruction(llvm::MCInst &Inst, unsigned Insn,
1799                                     uint64_t Address, const void *Decoder) {
1800   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1801   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1802   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1803   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1804   unsigned size = fieldFromInstruction32(Insn, 6, 2);
1805   unsigned inc = fieldFromInstruction32(Insn, 5, 1) + 1;
1806   unsigned align = fieldFromInstruction32(Insn, 4, 1);
1807
1808   if (size == 0x3) {
1809     size = 4;
1810     align = 16;
1811   } else {
1812     if (size == 2) {
1813       size = 1 << size;
1814       align *= 8;
1815     } else {
1816       size = 1 << size;
1817       align *= 4*size;
1818     }
1819   }
1820
1821   DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1822   DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder);
1823   DecodeDPRRegisterClass(Inst, (Rd+2*inc)%32, Address, Decoder);
1824   DecodeDPRRegisterClass(Inst, (Rd+3*inc)%32, Address, Decoder);
1825   if (Rm == 0xD) DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1826
1827   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
1828   Inst.addOperand(MCOperand::CreateImm(align));
1829
1830   if (Rm == 0xD)
1831     Inst.addOperand(MCOperand::CreateReg(0));
1832   else if (Rm != 0xF)
1833     DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
1834
1835   return true;
1836 }
1837
1838 static bool DecodeNEONModImmInstruction(llvm::MCInst &Inst, unsigned Insn,
1839                                         uint64_t Address, const void *Decoder) {
1840   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1841   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1842   unsigned imm = fieldFromInstruction32(Insn, 0, 4);
1843   imm |= fieldFromInstruction32(Insn, 16, 3) << 4;
1844   imm |= fieldFromInstruction32(Insn, 24, 1) << 7;
1845   imm |= fieldFromInstruction32(Insn, 8, 4) << 8;
1846   imm |= fieldFromInstruction32(Insn, 5, 1) << 12;
1847   unsigned Q = fieldFromInstruction32(Insn, 6, 1);
1848
1849   if (Q)
1850     DecodeQPRRegisterClass(Inst, Rd, Address, Decoder);
1851   else
1852     DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1853
1854   Inst.addOperand(MCOperand::CreateImm(imm));
1855
1856   switch (Inst.getOpcode()) {
1857     case ARM::VORRiv4i16:
1858     case ARM::VORRiv2i32:
1859     case ARM::VBICiv4i16:
1860     case ARM::VBICiv2i32:
1861       DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1862       break;
1863     case ARM::VORRiv8i16:
1864     case ARM::VORRiv4i32:
1865     case ARM::VBICiv8i16:
1866     case ARM::VBICiv4i32:
1867       DecodeQPRRegisterClass(Inst, Rd, Address, Decoder);
1868       break;
1869     default:
1870       break;
1871   }
1872
1873
1874   return true;
1875 }
1876
1877 static bool DecodeVSHLMaxInstruction(llvm::MCInst &Inst, unsigned Insn,
1878                                         uint64_t Address, const void *Decoder) {
1879   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1880   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1881   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1882   Rm |= fieldFromInstruction32(Insn, 5, 1) << 4;
1883   unsigned size = fieldFromInstruction32(Insn, 18, 2);
1884
1885   DecodeQPRRegisterClass(Inst, Rd, Address, Decoder);
1886   DecodeDPRRegisterClass(Inst, Rm, Address, Decoder);
1887   Inst.addOperand(MCOperand::CreateImm(8 << size));
1888
1889   return true;
1890 }
1891
1892 static bool DecodeShiftRight8Imm(llvm::MCInst &Inst, unsigned Val,
1893                                uint64_t Address, const void *Decoder) {
1894   Inst.addOperand(MCOperand::CreateImm(8 - Val));
1895   return true;
1896 }
1897
1898 static bool DecodeShiftRight16Imm(llvm::MCInst &Inst, unsigned Val,
1899                                uint64_t Address, const void *Decoder) {
1900   Inst.addOperand(MCOperand::CreateImm(16 - Val));
1901   return true;
1902 }
1903
1904 static bool DecodeShiftRight32Imm(llvm::MCInst &Inst, unsigned Val,
1905                                uint64_t Address, const void *Decoder) {
1906   Inst.addOperand(MCOperand::CreateImm(32 - Val));
1907   return true;
1908 }
1909
1910 static bool DecodeShiftRight64Imm(llvm::MCInst &Inst, unsigned Val,
1911                                uint64_t Address, const void *Decoder) {
1912   Inst.addOperand(MCOperand::CreateImm(64 - Val));
1913   return true;
1914 }
1915
1916 static bool DecodeTBLInstruction(llvm::MCInst &Inst, unsigned Insn,
1917                                uint64_t Address, const void *Decoder) {
1918   unsigned Rd = fieldFromInstruction32(Insn, 12, 4);
1919   Rd |= fieldFromInstruction32(Insn, 22, 1) << 4;
1920   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
1921   Rn |= fieldFromInstruction32(Insn, 7, 1) << 4;
1922   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
1923   Rm |= fieldFromInstruction32(Insn, 5, 1) << 4;
1924   unsigned op = fieldFromInstruction32(Insn, 6, 1);
1925   unsigned length = fieldFromInstruction32(Insn, 8, 2) + 1;
1926
1927   DecodeDPRRegisterClass(Inst, Rd, Address, Decoder);
1928   if (op) DecodeDPRRegisterClass(Inst, Rd, Address, Decoder); // Writeback
1929
1930   for (unsigned i = 0; i < length; ++i)
1931     DecodeDPRRegisterClass(Inst, (Rn+i)%32, Address, Decoder);
1932
1933   DecodeDPRRegisterClass(Inst, Rm, Address, Decoder);
1934
1935   return true;
1936 }
1937
1938 static bool DecodeVFPfpImm(llvm::MCInst &Inst, unsigned Val,
1939                             uint64_t Address, const void *Decoder) {
1940   // The immediate needs to be a fully instantiated float.  However, the
1941   // auto-generated decoder is only able to fill in some of the bits
1942   // necessary.  For instance, the 'b' bit is replicated multiple times,
1943   // and is even present in inverted form in one bit.  We do a little
1944   // binary parsing here to fill in those missing bits, and then
1945   // reinterpret it all as a float.
1946   union {
1947     uint32_t integer;
1948     float fp;
1949   } fp_conv;
1950
1951   fp_conv.integer = Val;
1952   uint32_t b = fieldFromInstruction32(Val, 25, 1);
1953   fp_conv.integer |= b << 26;
1954   fp_conv.integer |= b << 27;
1955   fp_conv.integer |= b << 28;
1956   fp_conv.integer |= b << 29;
1957   fp_conv.integer |= (~b & 0x1) << 30;
1958
1959   Inst.addOperand(MCOperand::CreateFPImm(fp_conv.fp));
1960   return true;
1961 }
1962
1963 static bool DecodeThumbAddSpecialReg(llvm::MCInst &Inst, uint16_t Insn,
1964                                      uint64_t Address, const void *Decoder) {
1965   unsigned dst = fieldFromInstruction16(Insn, 8, 3);
1966   unsigned imm = fieldFromInstruction16(Insn, 0, 8);
1967
1968   DecodetGPRRegisterClass(Inst, dst, Address, Decoder);
1969
1970   if (Inst.getOpcode() == ARM::tADR)
1971     Inst.addOperand(MCOperand::CreateReg(ARM::PC));
1972   else if (Inst.getOpcode() == ARM::tADDrSPi)
1973     Inst.addOperand(MCOperand::CreateReg(ARM::SP));
1974   else
1975     return false;
1976
1977   Inst.addOperand(MCOperand::CreateImm(imm));
1978   return true;
1979 }
1980
1981 static bool DecodeThumbBROperand(llvm::MCInst &Inst, unsigned Val,
1982                                  uint64_t Address, const void *Decoder) {
1983   Inst.addOperand(MCOperand::CreateImm(SignExtend32<12>(Val << 1)));
1984   return true;
1985 }
1986
1987 static bool DecodeT2BROperand(llvm::MCInst &Inst, unsigned Val,
1988                                  uint64_t Address, const void *Decoder) {
1989   Inst.addOperand(MCOperand::CreateImm(SignExtend32<21>(Val)));
1990   return true;
1991 }
1992
1993 static bool DecodeThumbCmpBROperand(llvm::MCInst &Inst, unsigned Val,
1994                                  uint64_t Address, const void *Decoder) {
1995   Inst.addOperand(MCOperand::CreateImm(SignExtend32<7>(Val << 1)));
1996   return true;
1997 }
1998
1999 static bool DecodeThumbAddrModeRR(llvm::MCInst &Inst, unsigned Val,
2000                                  uint64_t Address, const void *Decoder) {
2001   unsigned Rn = fieldFromInstruction32(Val, 0, 3);
2002   unsigned Rm = fieldFromInstruction32(Val, 3, 3);
2003
2004   DecodetGPRRegisterClass(Inst, Rn, Address, Decoder);
2005   DecodetGPRRegisterClass(Inst, Rm, Address, Decoder);
2006
2007   return true;
2008 }
2009
2010 static bool DecodeThumbAddrModeIS(llvm::MCInst &Inst, unsigned Val,
2011                                   uint64_t Address, const void *Decoder) {
2012   unsigned Rn = fieldFromInstruction32(Val, 0, 3);
2013   unsigned imm = fieldFromInstruction32(Val, 3, 5);
2014
2015   DecodetGPRRegisterClass(Inst, Rn, Address, Decoder);
2016   Inst.addOperand(MCOperand::CreateImm(imm));
2017
2018   return true;
2019 }
2020
2021 static bool DecodeThumbAddrModePC(llvm::MCInst &Inst, unsigned Val,
2022                                   uint64_t Address, const void *Decoder) {
2023   Inst.addOperand(MCOperand::CreateImm(Val << 2));
2024
2025   return true;
2026 }
2027
2028 static bool DecodeThumbAddrModeSP(llvm::MCInst &Inst, unsigned Val,
2029                                   uint64_t Address, const void *Decoder) {
2030   Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2031   Inst.addOperand(MCOperand::CreateImm(Val << 2));
2032
2033   return true;
2034 }
2035
2036 static bool DecodeT2AddrModeSOReg(llvm::MCInst &Inst, unsigned Val,
2037                                   uint64_t Address, const void *Decoder) {
2038   unsigned Rn = fieldFromInstruction32(Val, 6, 4);
2039   unsigned Rm = fieldFromInstruction32(Val, 2, 4);
2040   unsigned imm = fieldFromInstruction32(Val, 0, 2);
2041
2042   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
2043   DecoderGPRRegisterClass(Inst, Rm, Address, Decoder);
2044   Inst.addOperand(MCOperand::CreateImm(imm));
2045
2046   return true;
2047 }
2048
2049 static bool DecodeT2LoadShift(llvm::MCInst &Inst, unsigned Insn,
2050                               uint64_t Address, const void *Decoder) {
2051   if (Inst.getOpcode() != ARM::t2PLDs) {
2052     unsigned Rt = fieldFromInstruction32(Insn, 12, 4);
2053     DecodeGPRRegisterClass(Inst, Rt, Address, Decoder);
2054   }
2055
2056   unsigned Rn = fieldFromInstruction32(Insn, 16, 4);
2057   if (Rn == 0xF) {
2058     switch (Inst.getOpcode()) {
2059       case ARM::t2LDRBs:
2060         Inst.setOpcode(ARM::t2LDRBpci);
2061         break;
2062       case ARM::t2LDRHs:
2063         Inst.setOpcode(ARM::t2LDRHpci);
2064         break;
2065       case ARM::t2LDRSHs:
2066         Inst.setOpcode(ARM::t2LDRSHpci);
2067         break;
2068       case ARM::t2LDRSBs:
2069         Inst.setOpcode(ARM::t2LDRSBpci);
2070         break;
2071       case ARM::t2PLDs:
2072         Inst.setOpcode(ARM::t2PLDi12);
2073         Inst.addOperand(MCOperand::CreateReg(ARM::PC));
2074         break;
2075       default:
2076         return false;
2077     }
2078
2079     int imm = fieldFromInstruction32(Insn, 0, 12);
2080     if (!fieldFromInstruction32(Insn, 23, 1)) imm *= -1;
2081     Inst.addOperand(MCOperand::CreateImm(imm));
2082
2083     return true;
2084   }
2085
2086   unsigned addrmode = fieldFromInstruction32(Insn, 4, 2);
2087   addrmode |= fieldFromInstruction32(Insn, 0, 4) << 2;
2088   addrmode |= fieldFromInstruction32(Insn, 16, 4) << 6;
2089   DecodeT2AddrModeSOReg(Inst, addrmode, Address, Decoder);
2090
2091   return true;
2092 }
2093
2094 static bool DecodeT2Imm8S4(llvm::MCInst &Inst, unsigned Val,
2095                                    uint64_t Address, const void *Decoder) {
2096   int imm = Val & 0xFF;
2097   if (!(Val & 0x100)) imm *= -1;
2098   Inst.addOperand(MCOperand::CreateImm(imm << 2));
2099
2100   return true;
2101 }
2102
2103 static bool DecodeT2AddrModeImm8s4(llvm::MCInst &Inst, unsigned Val,
2104                                    uint64_t Address, const void *Decoder) {
2105   unsigned Rn = fieldFromInstruction32(Val, 9, 4);
2106   unsigned imm = fieldFromInstruction32(Val, 0, 9);
2107
2108   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
2109   DecodeT2Imm8S4(Inst, imm, Address, Decoder);
2110
2111   return true;
2112 }
2113
2114 static bool DecodeT2Imm8(llvm::MCInst &Inst, unsigned Val,
2115                                    uint64_t Address, const void *Decoder) {
2116   int imm = Val & 0xFF;
2117   if (!(Val & 0x100)) imm *= -1;
2118   Inst.addOperand(MCOperand::CreateImm(imm));
2119
2120   return true;
2121 }
2122
2123
2124 static bool DecodeT2AddrModeImm8(llvm::MCInst &Inst, unsigned Val,
2125                                    uint64_t Address, const void *Decoder) {
2126   unsigned Rn = fieldFromInstruction32(Val, 9, 4);
2127   unsigned imm = fieldFromInstruction32(Val, 0, 9);
2128
2129   // Some instructions always use an additive offset.
2130   switch (Inst.getOpcode()) {
2131     case ARM::t2LDRT:
2132     case ARM::t2LDRBT:
2133     case ARM::t2LDRHT:
2134     case ARM::t2LDRSBT:
2135     case ARM::t2LDRSHT:
2136       imm |= 0x100;
2137       break;
2138     default:
2139       break;
2140   }
2141
2142   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
2143   DecodeT2Imm8(Inst, imm, Address, Decoder);
2144
2145   return true;
2146 }
2147
2148
2149 static bool DecodeT2AddrModeImm12(llvm::MCInst &Inst, unsigned Val,
2150                                    uint64_t Address, const void *Decoder) {
2151   unsigned Rn = fieldFromInstruction32(Val, 13, 4);
2152   unsigned imm = fieldFromInstruction32(Val, 0, 12);
2153
2154   DecodeGPRRegisterClass(Inst, Rn, Address, Decoder);
2155   Inst.addOperand(MCOperand::CreateImm(imm));
2156
2157   return true;
2158 }
2159
2160
2161 static bool DecodeThumbAddSPImm(llvm::MCInst &Inst, uint16_t Insn,
2162                                    uint64_t Address, const void *Decoder) {
2163   unsigned imm = fieldFromInstruction16(Insn, 0, 7);
2164
2165   Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2166   Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2167   Inst.addOperand(MCOperand::CreateImm(imm));
2168
2169   return true;
2170 }
2171
2172 static bool DecodeThumbAddSPReg(llvm::MCInst &Inst, uint16_t Insn,
2173                                    uint64_t Address, const void *Decoder) {
2174   if (Inst.getOpcode() == ARM::tADDrSP) {
2175     unsigned Rdm = fieldFromInstruction16(Insn, 0, 3);
2176     Rdm |= fieldFromInstruction16(Insn, 7, 1) << 3;
2177
2178     DecodeGPRRegisterClass(Inst, Rdm, Address, Decoder);
2179     Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2180     DecodeGPRRegisterClass(Inst, Rdm, Address, Decoder);
2181   } else if (Inst.getOpcode() == ARM::tADDspr) {
2182     unsigned Rm = fieldFromInstruction16(Insn, 3, 4);
2183
2184     Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2185     Inst.addOperand(MCOperand::CreateReg(ARM::SP));
2186     DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
2187   }
2188
2189   return true;
2190 }
2191
2192 static bool DecodeThumbCPS(llvm::MCInst &Inst, uint16_t Insn,
2193                                    uint64_t Address, const void *Decoder) {
2194   unsigned imod = fieldFromInstruction16(Insn, 4, 1) | 0x2;
2195   unsigned flags = fieldFromInstruction16(Insn, 0, 3);
2196
2197   Inst.addOperand(MCOperand::CreateImm(imod));
2198   Inst.addOperand(MCOperand::CreateImm(flags));
2199
2200   return true;
2201 }
2202
2203 static bool DecodePostIdxReg(llvm::MCInst &Inst, unsigned Insn,
2204                                    uint64_t Address, const void *Decoder) {
2205   unsigned Rm = fieldFromInstruction32(Insn, 0, 4);
2206   unsigned add = fieldFromInstruction32(Insn, 4, 1);
2207
2208   DecodeGPRRegisterClass(Inst, Rm, Address, Decoder);
2209   Inst.addOperand(MCOperand::CreateImm(add));
2210
2211   return true;
2212 }
2213
2214 static bool DecodeThumbBLXOffset(llvm::MCInst &Inst, unsigned Val,
2215                                    uint64_t Address, const void *Decoder) {
2216   Inst.addOperand(MCOperand::CreateImm(SignExtend32<22>(Val << 1)));
2217   return true;
2218 }
2219
2220 static bool DecodeCoprocessor(llvm::MCInst &Inst, unsigned Val,
2221                               uint64_t Address, const void *Decoder) {
2222   if (Val == 0xA || Val == 0xB)
2223     return false;
2224
2225   Inst.addOperand(MCOperand::CreateImm(Val));
2226   return true;
2227 }
2228
2229 static bool DecodeThumbSRImm(llvm::MCInst &Inst, unsigned Val,
2230                               uint64_t Address, const void *Decoder) {
2231   if (Val == 0)
2232     Inst.addOperand(MCOperand::CreateImm(32));
2233   else
2234     Inst.addOperand(MCOperand::CreateImm(Val));
2235   return true;
2236 }
2237
2238 static bool DecodeThumb2BCCInstruction(llvm::MCInst &Inst, unsigned Insn,
2239                                        uint64_t Address, const void *Decoder) {
2240   unsigned pred = fieldFromInstruction32(Insn, 22, 4);
2241   if (pred == 0xE || pred == 0xF) {
2242     unsigned opc = fieldFromInstruction32(Insn, 4, 2);
2243     switch (opc) {
2244       default:
2245         return false;
2246       case 0:
2247         Inst.setOpcode(ARM::t2DSB);
2248         break;
2249       case 1:
2250         Inst.setOpcode(ARM::t2DMB);
2251         break;
2252       case 2:
2253         Inst.setOpcode(ARM::t2ISB);
2254         return true;
2255     }
2256
2257     unsigned imm = fieldFromInstruction32(Insn, 0, 4);
2258     Inst.addOperand(MCOperand::CreateImm(imm));
2259     return true;
2260   }
2261
2262   unsigned brtarget = fieldFromInstruction32(Insn, 0, 11) << 1;
2263   brtarget |= fieldFromInstruction32(Insn, 11, 1) << 19;
2264   brtarget |= fieldFromInstruction32(Insn, 13, 1) << 18;
2265   brtarget |= fieldFromInstruction32(Insn, 16, 6) << 12;
2266   brtarget |= fieldFromInstruction32(Insn, 26, 1) << 20;
2267
2268   DecodeT2BROperand(Inst, brtarget, Address, Decoder);
2269   if (!DecodePredicateOperand(Inst, pred, Address, Decoder))
2270     return false;
2271
2272   return true;
2273 }
2274
2275 // Decode a shifted immediate operand.  These basically consist
2276 // of an 8-bit value, and a 4-bit directive that specifies either
2277 // a splat operation or a rotation.
2278 static bool DecodeT2SOImm(llvm::MCInst &Inst, unsigned Val,
2279                           uint64_t Address, const void *Decoder) {
2280   unsigned ctrl = fieldFromInstruction32(Val, 10, 2);
2281   if (ctrl == 0) {
2282     unsigned byte = fieldFromInstruction32(Val, 8, 2);
2283     unsigned imm = fieldFromInstruction32(Val, 0, 8);
2284     switch (byte) {
2285       case 0:
2286         Inst.addOperand(MCOperand::CreateImm(imm));
2287         break;
2288       case 1:
2289         Inst.addOperand(MCOperand::CreateImm((imm << 16) | imm));
2290         break;
2291       case 2:
2292         Inst.addOperand(MCOperand::CreateImm((imm << 24) | (imm << 8)));
2293         break;
2294       case 3:
2295         Inst.addOperand(MCOperand::CreateImm((imm << 24) | (imm << 16) |
2296                                              (imm << 8)  |  imm));
2297         break;
2298     }
2299   } else {
2300     unsigned unrot = fieldFromInstruction32(Val, 0, 7) | 0x80;
2301     unsigned rot = fieldFromInstruction32(Val, 7, 5);
2302     unsigned imm = (unrot >> rot) | (unrot << ((32-rot)&31));
2303     Inst.addOperand(MCOperand::CreateImm(imm));
2304   }
2305
2306   return true;
2307 }
2308
2309 static bool DecodeThumbBCCTargetOperand(llvm::MCInst &Inst, unsigned Val,
2310                                         uint64_t Address, const void *Decoder){
2311   Inst.addOperand(MCOperand::CreateImm(Val << 1));
2312   return true;
2313 }
2314
2315 static bool DecodeThumbBLTargetOperand(llvm::MCInst &Inst, unsigned Val,
2316                                         uint64_t Address, const void *Decoder){
2317   Inst.addOperand(MCOperand::CreateImm(SignExtend32<22>(Val << 1)));
2318   return true;
2319 }
2320
2321 static bool DecodeAddrMode3Offset(llvm::MCInst &Inst, unsigned Val,
2322                                   uint64_t Address, const void *Decoder) {
2323   bool isImm = fieldFromInstruction32(Val, 9, 1);
2324   bool isAdd = fieldFromInstruction32(Val, 8, 1);
2325   unsigned imm = fieldFromInstruction32(Val, 0, 8);
2326
2327   if (!isImm) {
2328     DecodeGPRRegisterClass(Inst, imm, Address, Decoder);
2329     Inst.addOperand(MCOperand::CreateImm(!isAdd << 8));
2330   } else {
2331     Inst.addOperand(MCOperand::CreateReg(0));
2332     Inst.addOperand(MCOperand::CreateImm(imm | (!isAdd << 8)));
2333   }
2334
2335   return true;
2336 }