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