Thumb instructions which have reglist operands at the end and predicate operands
[oota-llvm.git] / lib / Target / ARM / Disassembler / ThumbDisassemblerCore.h
1 //===- ThumbDisassemblerCore.h - Thumb disassembler helpers -----*- 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 // This file is part of the ARM Disassembler.
11 // It contains code for disassembling a Thumb instr.  It is to be included by
12 // ARMDisassemblerCore.cpp because it contains the static DisassembleThumbFrm()
13 // function which acts as the dispatcher to disassemble a Thumb instruction.
14 //
15 //===----------------------------------------------------------------------===//
16
17 ///////////////////////////////
18 //                           //
19 //     Utility Functions     //
20 //                           //
21 ///////////////////////////////
22
23 // Utilities for 16-bit Thumb instructions.
24 /*
25 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
26                [  tRt ]
27                       [ tRm ]  [ tRn ]  [ tRd ]
28                          D  [   Rm   ]  [  Rd ]
29
30                       [ imm3]
31                [    imm5    ]
32                    i     [    imm5   ]
33                             [       imm7      ]
34                          [       imm8         ]
35                [             imm11            ]
36
37             [   cond  ]
38 */
39
40 // Extract tRt: Inst{10-8}.
41 static inline unsigned getT1tRt(uint32_t insn) {
42   return slice(insn, 10, 8);
43 }
44
45 // Extract tRm: Inst{8-6}.
46 static inline unsigned getT1tRm(uint32_t insn) {
47   return slice(insn, 8, 6);
48 }
49
50 // Extract tRn: Inst{5-3}.
51 static inline unsigned getT1tRn(uint32_t insn) {
52   return slice(insn, 5, 3);
53 }
54
55 // Extract tRd: Inst{2-0}.
56 static inline unsigned getT1tRd(uint32_t insn) {
57   return slice(insn, 2, 0);
58 }
59
60 // Extract [D:Rd]: Inst{7:2-0}.
61 static inline unsigned getT1Rd(uint32_t insn) {
62   return slice(insn, 7, 7) << 3 | slice(insn, 2, 0);
63 }
64
65 // Extract Rm: Inst{6-3}.
66 static inline unsigned getT1Rm(uint32_t insn) {
67   return slice(insn, 6, 3);
68 }
69
70 // Extract imm3: Inst{8-6}.
71 static inline unsigned getT1Imm3(uint32_t insn) {
72   return slice(insn, 8, 6);
73 }
74
75 // Extract imm5: Inst{10-6}.
76 static inline unsigned getT1Imm5(uint32_t insn) {
77   return slice(insn, 10, 6);
78 }
79
80 // Extract i:imm5: Inst{9:7-3}.
81 static inline unsigned getT1Imm6(uint32_t insn) {
82   return slice(insn, 9, 9) << 5 | slice(insn, 7, 3);
83 }
84
85 // Extract imm7: Inst{6-0}.
86 static inline unsigned getT1Imm7(uint32_t insn) {
87   return slice(insn, 6, 0);
88 }
89
90 // Extract imm8: Inst{7-0}.
91 static inline unsigned getT1Imm8(uint32_t insn) {
92   return slice(insn, 7, 0);
93 }
94
95 // Extract imm11: Inst{10-0}.
96 static inline unsigned getT1Imm11(uint32_t insn) {
97   return slice(insn, 10, 0);
98 }
99
100 // Extract cond: Inst{11-8}.
101 static inline unsigned getT1Cond(uint32_t insn) {
102   return slice(insn, 11, 8);
103 }
104
105 static inline bool IsGPR(unsigned RegClass) {
106   return RegClass == ARM::GPRRegClassID;
107 }
108
109 // Utilities for 32-bit Thumb instructions.
110
111 // Extract imm4: Inst{19-16}.
112 static inline unsigned getImm4(uint32_t insn) {
113   return slice(insn, 19, 16);
114 }
115
116 // Extract imm3: Inst{14-12}.
117 static inline unsigned getImm3(uint32_t insn) {
118   return slice(insn, 14, 12);
119 }
120
121 // Extract imm8: Inst{7-0}.
122 static inline unsigned getImm8(uint32_t insn) {
123   return slice(insn, 7, 0);
124 }
125
126 // A8.6.61 LDRB (immediate, Thumb) and friends
127 // +/-: Inst{9}
128 // imm8: Inst{7-0}
129 static inline int decodeImm8(uint32_t insn) {
130   int Offset = getImm8(insn);
131   return slice(insn, 9, 9) ? Offset : -Offset;
132 }
133
134 // Extract imm12: Inst{11-0}.
135 static inline unsigned getImm12(uint32_t insn) {
136   return slice(insn, 11, 0);
137 }
138
139 // A8.6.63 LDRB (literal) and friends
140 // +/-: Inst{23}
141 // imm12: Inst{11-0}
142 static inline int decodeImm12(uint32_t insn) {
143   int Offset = getImm12(insn);
144   return slice(insn, 23, 23) ? Offset : -Offset;
145 }
146
147 // Extract imm2: Inst{7-6}.
148 static inline unsigned getImm2(uint32_t insn) {
149   return slice(insn, 7, 6);
150 }
151
152 // For BFI, BFC, t2SBFX, and t2UBFX.
153 // Extract lsb: Inst{14-12:7-6}.
154 static inline unsigned getLsb(uint32_t insn) {
155   return getImm3(insn) << 2 | getImm2(insn);
156 }
157
158 // For BFI and BFC.
159 // Extract msb: Inst{4-0}.
160 static inline unsigned getMsb(uint32_t insn) {
161   return slice(insn, 4, 0);
162 }
163
164 // For t2SBFX and t2UBFX.
165 // Extract widthminus1: Inst{4-0}.
166 static inline unsigned getWidthMinus1(uint32_t insn) {
167   return slice(insn, 4, 0);
168 }
169
170 // For t2ADDri12 and t2SUBri12.
171 // imm12 = i:imm3:imm8;
172 static inline unsigned getIImm3Imm8(uint32_t insn) {
173   return slice(insn, 26, 26) << 11 | getImm3(insn) << 8 | getImm8(insn);
174 }
175
176 // For t2MOVi16 and t2MOVTi16.
177 // imm16 = imm4:i:imm3:imm8;
178 static inline unsigned getImm16(uint32_t insn) {
179   return getImm4(insn) << 12 | slice(insn, 26, 26) << 11 |
180     getImm3(insn) << 8 | getImm8(insn);
181 }
182
183 // Inst{5-4} encodes the shift type.
184 static inline unsigned getShiftTypeBits(uint32_t insn) {
185   return slice(insn, 5, 4);
186 }
187
188 // Inst{14-12}:Inst{7-6} encodes the imm5 shift amount.
189 static inline unsigned getShiftAmtBits(uint32_t insn) {
190   return getImm3(insn) << 2 | getImm2(insn);
191 }
192
193 // A8.6.17 BFC
194 // Encoding T1 ARMv6T2, ARMv7
195 // LLVM-specific encoding for #<lsb> and #<width>
196 static inline bool getBitfieldInvMask(uint32_t insn, uint32_t &mask) {
197   uint32_t lsb = getImm3(insn) << 2 | getImm2(insn);
198   uint32_t msb = getMsb(insn);
199   uint32_t Val = 0;
200   if (msb < lsb) {
201     DEBUG(errs() << "Encoding error: msb < lsb\n");
202     return false;
203   }
204   for (uint32_t i = lsb; i <= msb; ++i)
205     Val |= (1 << i);
206   mask = ~Val;
207   return true;
208 }
209
210 // A8.4 Shifts applied to a register
211 // A8.4.1 Constant shifts
212 // A8.4.3 Pseudocode details of instruction-specified shifts and rotates
213 //
214 // decodeImmShift() returns the shift amount and the the shift opcode.
215 // Note that, as of Jan-06-2010, LLVM does not support rrx shifted operands yet.
216 static inline unsigned decodeImmShift(unsigned bits2, unsigned imm5,
217                                       ARM_AM::ShiftOpc &ShOp) {
218
219   assert(imm5 < 32 && "Invalid imm5 argument");
220   switch (bits2) {
221   default: assert(0 && "No such value");
222   case 0:
223     ShOp = ARM_AM::lsl;
224     return imm5;
225   case 1:
226     ShOp = ARM_AM::lsr;
227     return (imm5 == 0 ? 32 : imm5);
228   case 2:
229     ShOp = ARM_AM::asr;
230     return (imm5 == 0 ? 32 : imm5);
231   case 3:
232     ShOp = (imm5 == 0 ? ARM_AM::rrx : ARM_AM::ror);
233     return (imm5 == 0 ? 1 : imm5);
234   }
235 }
236
237 // A6.3.2 Modified immediate constants in Thumb instructions
238 //
239 // ThumbExpandImm() returns the modified immediate constant given an imm12 for
240 // Thumb data-processing instructions with modified immediate.
241 // See also A6.3.1 Data-processing (modified immediate).
242 static inline unsigned ThumbExpandImm(unsigned imm12) {
243   assert(imm12 <= 0xFFF && "Invalid imm12 argument");
244
245   // If the leading two bits is 0b00, the modified immediate constant is
246   // obtained by splatting the low 8 bits into the first byte, every other byte,
247   // or every byte of a 32-bit value.
248   //
249   // Otherwise, a rotate right of '1':imm12<6:0> by the amount imm12<11:7> is
250   // performed.
251
252   if (slice(imm12, 11, 10) == 0) {
253     unsigned short control = slice(imm12, 9, 8);
254     unsigned imm8 = slice(imm12, 7, 0);
255     switch (control) {
256     default:
257       assert(0 && "No such value");
258       return 0;
259     case 0:
260       return imm8;
261     case 1:
262       return imm8 << 16 | imm8;
263     case 2:
264       return imm8 << 24 | imm8 << 8;
265     case 3:
266       return imm8 << 24 | imm8 << 16 | imm8 << 8 | imm8;
267     }
268   } else {
269     // A rotate is required.
270     unsigned Val = 1 << 7 | slice(imm12, 6, 0);
271     unsigned Amt = slice(imm12, 11, 7);
272     return ARM_AM::rotr32(Val, Amt);
273   }
274 }
275
276 static inline int decodeImm32_B_EncodingT3(uint32_t insn) {
277   bool S = slice(insn, 26, 26);
278   bool J1 = slice(insn, 13, 13);
279   bool J2 = slice(insn, 11, 11);
280   unsigned Imm21 = slice(insn, 21, 16) << 12 | slice(insn, 10, 0) << 1;
281   if (S) Imm21 |= 1 << 20;
282   if (J2) Imm21 |= 1 << 19;
283   if (J1) Imm21 |= 1 << 18;
284
285   return SignExtend32<21>(Imm21);
286 }
287
288 static inline int decodeImm32_B_EncodingT4(uint32_t insn) {
289   unsigned S = slice(insn, 26, 26);
290   bool I1 = slice(insn, 13, 13) == S;
291   bool I2 = slice(insn, 11, 11) == S;
292   unsigned Imm25 = slice(insn, 25, 16) << 12 | slice(insn, 10, 0) << 1;
293   if (S) Imm25 |= 1 << 24;
294   if (I1) Imm25 |= 1 << 23;
295   if (I2) Imm25 |= 1 << 22;
296
297   return SignExtend32<25>(Imm25);
298 }
299
300 static inline int decodeImm32_BL(uint32_t insn) {
301   unsigned S = slice(insn, 26, 26);
302   bool I1 = slice(insn, 13, 13) == S;
303   bool I2 = slice(insn, 11, 11) == S;
304   unsigned Imm25 = slice(insn, 25, 16) << 12 | slice(insn, 10, 0) << 1;
305   if (S) Imm25 |= 1 << 24;
306   if (I1) Imm25 |= 1 << 23;
307   if (I2) Imm25 |= 1 << 22;
308
309   return SignExtend32<25>(Imm25);
310 }
311
312 static inline int decodeImm32_BLX(uint32_t insn) {
313   unsigned S = slice(insn, 26, 26);
314   bool I1 = slice(insn, 13, 13) == S;
315   bool I2 = slice(insn, 11, 11) == S;
316   unsigned Imm25 = slice(insn, 25, 16) << 12 | slice(insn, 10, 1) << 2;
317   if (S) Imm25 |= 1 << 24;
318   if (I1) Imm25 |= 1 << 23;
319   if (I2) Imm25 |= 1 << 22;
320
321   return SignExtend32<25>(Imm25);
322 }
323
324 // See, for example, A8.6.221 SXTAB16.
325 static inline unsigned decodeRotate(uint32_t insn) {
326   unsigned rotate = slice(insn, 5, 4);
327   return rotate << 3;
328 }
329
330 ///////////////////////////////////////////////
331 //                                           //
332 // Thumb1 instruction disassembly functions. //
333 //                                           //
334 ///////////////////////////////////////////////
335
336 // See "Utilities for 16-bit Thumb instructions" for register naming convention.
337
338 // A6.2.1 Shift (immediate), add, subtract, move, and compare
339 //
340 // shift immediate:         tRd CPSR tRn imm5
341 // add/sub register:        tRd CPSR tRn tRm
342 // add/sub 3-bit immediate: tRd CPSR tRn imm3
343 // add/sub 8-bit immediate: tRt CPSR tRt(TIED_TO) imm8
344 // mov/cmp immediate:       tRt [CPSR] imm8 (CPSR present for mov)
345 //
346 // Special case:
347 // tMOVSr:                  tRd tRn
348 static bool DisassembleThumb1General(MCInst &MI, unsigned Opcode, uint32_t insn,
349     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
350
351   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
352   unsigned &OpIdx = NumOpsAdded;
353
354   OpIdx = 0;
355
356   assert(NumOps >= 2 && OpInfo[0].RegClass == ARM::tGPRRegClassID
357          && "Invalid arguments");
358
359   bool Imm3 = (Opcode == ARM::tADDi3 || Opcode == ARM::tSUBi3);
360
361   // Use Rt implies use imm8.
362   bool UseRt = (Opcode == ARM::tADDi8 || Opcode == ARM::tSUBi8 ||
363                 Opcode == ARM::tMOVi8 || Opcode == ARM::tCMPi8);
364
365   // Add the destination operand.
366   MI.addOperand(MCOperand::CreateReg(
367                   getRegisterEnum(B, ARM::tGPRRegClassID,
368                                   UseRt ? getT1tRt(insn) : getT1tRd(insn))));
369   ++OpIdx;
370
371   // Check whether the next operand to be added is a CCR Register.
372   if (OpInfo[OpIdx].RegClass == ARM::CCRRegClassID) {
373     assert(OpInfo[OpIdx].isOptionalDef() && "Optional def operand expected");
374     MI.addOperand(MCOperand::CreateReg(B->InITBlock() ? 0 : ARM::CPSR));
375     ++OpIdx;
376   }
377
378   // Check whether the next operand to be added is a Thumb1 Register.
379   assert(OpIdx < NumOps && "More operands expected");
380   if (OpInfo[OpIdx].RegClass == ARM::tGPRRegClassID) {
381     // For UseRt, the reg operand is tied to the first reg operand.
382     MI.addOperand(MCOperand::CreateReg(
383                     getRegisterEnum(B, ARM::tGPRRegClassID,
384                                     UseRt ? getT1tRt(insn) : getT1tRn(insn))));
385     ++OpIdx;
386   }
387
388   // Special case for tMOVSr.
389   if (OpIdx == NumOps)
390     return true;
391
392   // The next available operand is either a reg operand or an imm operand.
393   if (OpInfo[OpIdx].RegClass == ARM::tGPRRegClassID) {
394     // Three register operand instructions.
395     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
396                                                        getT1tRm(insn))));
397   } else {
398     assert(OpInfo[OpIdx].RegClass == 0 &&
399            !OpInfo[OpIdx].isPredicate() && !OpInfo[OpIdx].isOptionalDef()
400            && "Pure imm operand expected");
401     MI.addOperand(MCOperand::CreateImm(UseRt ? getT1Imm8(insn)
402                                              : (Imm3 ? getT1Imm3(insn)
403                                                      : getT1Imm5(insn))));
404   }
405   ++OpIdx;
406
407   return true;
408 }
409
410 // A6.2.2 Data-processing
411 //
412 // tCMPr, tTST, tCMN: tRd tRn
413 // tMVN, tRSB:        tRd CPSR tRn
414 // Others:            tRd CPSR tRd(TIED_TO) tRn
415 static bool DisassembleThumb1DP(MCInst &MI, unsigned Opcode, uint32_t insn,
416     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
417
418   const TargetInstrDesc &TID = ARMInsts[Opcode];
419   const TargetOperandInfo *OpInfo = TID.OpInfo;
420   unsigned &OpIdx = NumOpsAdded;
421
422   OpIdx = 0;
423
424   assert(NumOps >= 2 && OpInfo[0].RegClass == ARM::tGPRRegClassID &&
425          (OpInfo[1].RegClass == ARM::CCRRegClassID
426           || OpInfo[1].RegClass == ARM::tGPRRegClassID)
427          && "Invalid arguments");
428
429   // Add the destination operand.
430   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
431                                                      getT1tRd(insn))));
432   ++OpIdx;
433
434   // Check whether the next operand to be added is a CCR Register.
435   if (OpInfo[OpIdx].RegClass == ARM::CCRRegClassID) {
436     assert(OpInfo[OpIdx].isOptionalDef() && "Optional def operand expected");
437     MI.addOperand(MCOperand::CreateReg(B->InITBlock() ? 0 : ARM::CPSR));
438     ++OpIdx;
439   }
440
441   // We have either { tRd(TIED_TO), tRn } or { tRn } remaining.
442   // Process the TIED_TO operand first.
443
444   assert(OpIdx < NumOps && OpInfo[OpIdx].RegClass == ARM::tGPRRegClassID
445          && "Thumb reg operand expected");
446   int Idx;
447   if ((Idx = TID.getOperandConstraint(OpIdx, TOI::TIED_TO)) != -1) {
448     // The reg operand is tied to the first reg operand.
449     MI.addOperand(MI.getOperand(Idx));
450     ++OpIdx;
451   }
452
453   // Process possible next reg operand.
454   if (OpIdx < NumOps && OpInfo[OpIdx].RegClass == ARM::tGPRRegClassID) {
455     // Add tRn operand.
456     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
457                                                        getT1tRn(insn))));
458     ++OpIdx;
459   }
460
461   return true;
462 }
463
464 // A6.2.3 Special data instructions and branch and exchange
465 //
466 // tADDhirr: Rd Rd(TIED_TO) Rm
467 // tCMPhir:  Rd Rm
468 // tMOVr, tMOVgpr2gpr, tMOVgpr2tgpr, tMOVtgpr2gpr: Rd|tRd Rm|tRn
469 // tBX_RET: 0 operand
470 // tBX_RET_vararg: Rm
471 // tBLXr_r9: Rm
472 static bool DisassembleThumb1Special(MCInst &MI, unsigned Opcode, uint32_t insn,
473     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
474
475   // tBX_RET has 0 operand.
476   if (NumOps == 0)
477     return true;
478
479   // BX/BLX has 1 reg operand: Rm.
480   if (NumOps == 1) {
481     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
482                                                        getT1Rm(insn))));
483     NumOpsAdded = 1;
484     return true;
485   }
486
487   const TargetInstrDesc &TID = ARMInsts[Opcode];
488   const TargetOperandInfo *OpInfo = TID.OpInfo;
489   unsigned &OpIdx = NumOpsAdded;
490
491   OpIdx = 0;
492
493   // Add the destination operand.
494   unsigned RegClass = OpInfo[OpIdx].RegClass;
495   MI.addOperand(MCOperand::CreateReg(
496                   getRegisterEnum(B, RegClass,
497                                   IsGPR(RegClass) ? getT1Rd(insn)
498                                                   : getT1tRd(insn))));
499   ++OpIdx;
500
501   // We have either { Rd(TIED_TO), Rm } or { Rm|tRn } remaining.
502   // Process the TIED_TO operand first.
503
504   assert(OpIdx < NumOps && "More operands expected");
505   int Idx;
506   if ((Idx = TID.getOperandConstraint(OpIdx, TOI::TIED_TO)) != -1) {
507     // The reg operand is tied to the first reg operand.
508     MI.addOperand(MI.getOperand(Idx));
509     ++OpIdx;
510   }
511
512   // The next reg operand is either Rm or tRn.
513   assert(OpIdx < NumOps && "More operands expected");
514   RegClass = OpInfo[OpIdx].RegClass;
515   MI.addOperand(MCOperand::CreateReg(
516                   getRegisterEnum(B, RegClass,
517                                   IsGPR(RegClass) ? getT1Rm(insn)
518                                                   : getT1tRn(insn))));
519   ++OpIdx;
520
521   return true;
522 }
523
524 // A8.6.59 LDR (literal)
525 //
526 // tLDRpci: tRt imm8*4
527 static bool DisassembleThumb1LdPC(MCInst &MI, unsigned Opcode, uint32_t insn,
528     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
529
530   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
531   if (!OpInfo) return false;
532
533   assert(NumOps >= 2 && OpInfo[0].RegClass == ARM::tGPRRegClassID &&
534          (OpInfo[1].RegClass == 0 &&
535           !OpInfo[1].isPredicate() &&
536           !OpInfo[1].isOptionalDef())
537          && "Invalid arguments");
538
539   // Add the destination operand.
540   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
541                                                      getT1tRt(insn))));
542
543   // And the (imm8 << 2) operand.
544   MI.addOperand(MCOperand::CreateImm(getT1Imm8(insn) << 2));
545
546   NumOpsAdded = 2;
547
548   return true;
549 }
550
551 // Thumb specific addressing modes (see ARMInstrThumb.td):
552 //
553 // t_addrmode_rr := reg + reg
554 //
555 // t_addrmode_s4 := reg + reg
556 //                  reg + imm5 * 4
557 //
558 // t_addrmode_s2 := reg + reg
559 //                  reg + imm5 * 2
560 //
561 // t_addrmode_s1 := reg + reg
562 //                  reg + imm5
563 //
564 // t_addrmode_sp := sp + imm8 * 4
565 //
566
567 // A6.2.4 Load/store single data item
568 //
569 // Load/Store Register (reg|imm):      tRd tRn imm5 tRm
570 // Load Register Signed Byte|Halfword: tRd tRn tRm
571 static bool DisassembleThumb1LdSt(unsigned opA, MCInst &MI, unsigned Opcode,
572     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
573
574   const TargetInstrDesc &TID = ARMInsts[Opcode];
575   const TargetOperandInfo *OpInfo = TID.OpInfo;
576   unsigned &OpIdx = NumOpsAdded;
577
578   // Table A6-5 16-bit Thumb Load/store instructions
579   // opA = 0b0101 for STR/LDR (register) and friends.
580   // Otherwise, we have STR/LDR (immediate) and friends.
581   bool Imm5 = (opA != 5);
582
583   assert(NumOps >= 2
584          && OpInfo[0].RegClass == ARM::tGPRRegClassID
585          && OpInfo[1].RegClass == ARM::tGPRRegClassID
586          && "Expect >= 2 operands and first two as thumb reg operands");
587
588   // Add the destination reg and the base reg.
589   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
590                                                      getT1tRd(insn))));
591   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
592                                                      getT1tRn(insn))));
593   OpIdx = 2;
594
595   // We have either { imm5, tRm } or { tRm } remaining.
596   // Process the imm5 first.  Note that STR/LDR (register) should skip the imm5
597   // offset operand for t_addrmode_s[1|2|4].
598
599   assert(OpIdx < NumOps && "More operands expected");
600
601   if (OpInfo[OpIdx].RegClass == 0 && !OpInfo[OpIdx].isPredicate() &&
602       !OpInfo[OpIdx].isOptionalDef()) {
603
604     MI.addOperand(MCOperand::CreateImm(Imm5 ? getT1Imm5(insn) : 0));
605     ++OpIdx;
606   }
607
608   // The next reg operand is tRm, the offset.
609   assert(OpIdx < NumOps && OpInfo[OpIdx].RegClass == ARM::tGPRRegClassID
610          && "Thumb reg operand expected");
611   MI.addOperand(MCOperand::CreateReg(
612                   Imm5 ? 0
613                        : getRegisterEnum(B, ARM::tGPRRegClassID,
614                                          getT1tRm(insn))));
615   ++OpIdx;
616
617   return true;
618 }
619
620 // A6.2.4 Load/store single data item
621 //
622 // Load/Store Register SP relative: tRt ARM::SP imm8
623 static bool DisassembleThumb1LdStSP(MCInst &MI, unsigned Opcode, uint32_t insn,
624     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
625
626   assert((Opcode == ARM::tLDRspi || Opcode == ARM::tSTRspi)
627          && "Invalid opcode");
628
629   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
630   if (!OpInfo) return false;
631
632   assert(NumOps >= 3 &&
633          OpInfo[0].RegClass == ARM::tGPRRegClassID &&
634          OpInfo[1].RegClass == ARM::GPRRegClassID &&
635          (OpInfo[2].RegClass == 0 &&
636           !OpInfo[2].isPredicate() &&
637           !OpInfo[2].isOptionalDef())
638          && "Invalid arguments");
639
640   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
641                                                      getT1tRt(insn))));
642   MI.addOperand(MCOperand::CreateReg(ARM::SP));
643   MI.addOperand(MCOperand::CreateImm(getT1Imm8(insn)));
644   NumOpsAdded = 3;
645   return true;
646 }
647
648 // Table A6-1 16-bit Thumb instruction encoding
649 // A8.6.10 ADR
650 //
651 // tADDrPCi: tRt imm8
652 static bool DisassembleThumb1AddPCi(MCInst &MI, unsigned Opcode, uint32_t insn,
653     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
654
655   assert(Opcode == ARM::tADDrPCi && "Invalid opcode");
656
657   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
658   if (!OpInfo) return false;
659
660   assert(NumOps >= 2 && OpInfo[0].RegClass == ARM::tGPRRegClassID &&
661          (OpInfo[1].RegClass == 0 &&
662           !OpInfo[1].isPredicate() &&
663           !OpInfo[1].isOptionalDef())
664          && "Invalid arguments");
665
666   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
667                                                      getT1tRt(insn))));
668   MI.addOperand(MCOperand::CreateImm(getT1Imm8(insn)));
669   NumOpsAdded = 2;
670   return true;
671 }
672
673 // Table A6-1 16-bit Thumb instruction encoding
674 // A8.6.8 ADD (SP plus immediate)
675 //
676 // tADDrSPi: tRt ARM::SP imm8
677 static bool DisassembleThumb1AddSPi(MCInst &MI, unsigned Opcode, uint32_t insn,
678     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
679
680   assert(Opcode == ARM::tADDrSPi && "Invalid opcode");
681
682   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
683   if (!OpInfo) return false;
684
685   assert(NumOps >= 3 &&
686          OpInfo[0].RegClass == ARM::tGPRRegClassID &&
687          OpInfo[1].RegClass == ARM::GPRRegClassID &&
688          (OpInfo[2].RegClass == 0 &&
689           !OpInfo[2].isPredicate() &&
690           !OpInfo[2].isOptionalDef())
691          && "Invalid arguments");
692
693   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
694                                                      getT1tRt(insn))));
695   MI.addOperand(MCOperand::CreateReg(ARM::SP));
696   MI.addOperand(MCOperand::CreateImm(getT1Imm8(insn)));
697   NumOpsAdded = 3;
698   return true;
699 }
700
701 // tPUSH, tPOP: Pred-Imm Pred-CCR register_list
702 //
703 // where register_list = low registers + [lr] for PUSH or
704 //                       low registers + [pc] for POP
705 //
706 // "low registers" is specified by Inst{7-0}
707 // lr|pc is specified by Inst{8}
708 static bool DisassembleThumb1PushPop(MCInst &MI, unsigned Opcode, uint32_t insn,
709     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
710
711   assert((Opcode == ARM::tPUSH || Opcode == ARM::tPOP) && "Invalid opcode");
712
713   unsigned &OpIdx = NumOpsAdded;
714
715   // Handling the two predicate operands before the reglist.
716   if (B->DoPredicateOperands(MI, Opcode, insn, NumOps))
717     OpIdx += 2;
718   else {
719     DEBUG(errs() << "Expected predicate operands not found.\n");
720     return false;
721   }
722
723   unsigned RegListBits = slice(insn, 8, 8) << (Opcode == ARM::tPUSH ? 14 : 15)
724     | slice(insn, 7, 0);
725
726   // Fill the variadic part of reglist.
727   for (unsigned i = 0; i < 16; ++i) {
728     if ((RegListBits >> i) & 1) {
729       MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
730                                                          i)));
731       ++OpIdx;
732     }
733   }
734
735   return true;
736 }
737
738 // A6.2.5 Miscellaneous 16-bit instructions
739 // Delegate to DisassembleThumb1PushPop() for tPUSH & tPOP.
740 //
741 // tADDspi, tSUBspi: ARM::SP ARM::SP(TIED_TO) imm7
742 // t2IT:             firstcond=Inst{7-4} mask=Inst{3-0}
743 // tCBNZ, tCBZ:      tRd imm6*2
744 // tBKPT:            imm8
745 // tNOP, tSEV, tYIELD, tWFE, tWFI:
746 //   no operand (except predicate pair)
747 // tSETENDBE, tSETENDLE, :
748 //   no operand
749 // Others:           tRd tRn
750 static bool DisassembleThumb1Misc(MCInst &MI, unsigned Opcode, uint32_t insn,
751     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
752
753   if (NumOps == 0)
754     return true;
755
756   if (Opcode == ARM::tPUSH || Opcode == ARM::tPOP)
757     return DisassembleThumb1PushPop(MI, Opcode, insn, NumOps, NumOpsAdded, B);
758
759   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
760
761   // Predicate operands are handled elsewhere.
762   if (NumOps == 2 &&
763       OpInfo[0].isPredicate() && OpInfo[1].isPredicate() &&
764       OpInfo[0].RegClass == 0 && OpInfo[1].RegClass == ARM::CCRRegClassID) {
765     return true;
766   }
767
768   if (Opcode == ARM::tADDspi || Opcode == ARM::tSUBspi) {
769     // Special case handling for tADDspi and tSUBspi.
770     // A8.6.8 ADD (SP plus immediate) & A8.6.215 SUB (SP minus immediate)
771     MI.addOperand(MCOperand::CreateReg(ARM::SP));
772     MI.addOperand(MCOperand::CreateReg(ARM::SP));
773     MI.addOperand(MCOperand::CreateImm(getT1Imm7(insn)));
774     NumOpsAdded = 3;
775     return true;
776   }
777
778   if (Opcode == ARM::t2IT) {
779     // Special case handling for If-Then.
780     // A8.6.50 IT
781     // Tag the (firstcond[0] bit << 4) along with mask.
782
783     // firstcond
784     MI.addOperand(MCOperand::CreateImm(slice(insn, 7, 4)));
785
786     // firstcond[0] and mask
787     MI.addOperand(MCOperand::CreateImm(slice(insn, 4, 0)));
788     NumOpsAdded = 2;
789     return true;
790   }
791
792   if (Opcode == ARM::tBKPT) {
793     MI.addOperand(MCOperand::CreateImm(getT1Imm8(insn))); // breakpoint value
794     NumOpsAdded = 1;
795     return true;
796   }
797
798   // CPS has a singleton $opt operand that contains the following information:
799   // opt{4-0} = don't care
800   // opt{5} = 0 (false)
801   // opt{8-6} = AIF from Inst{2-0}
802   // opt{10-9} = 1:imod from Inst{4} with 0b10 as enable and 0b11 as disable
803   if (Opcode == ARM::tCPS) {
804     unsigned Option = slice(insn, 2, 0) << 6 | slice(insn, 4, 4) << 9 | 1 << 10;
805     MI.addOperand(MCOperand::CreateImm(Option));
806     NumOpsAdded = 1;
807     return true;
808   }
809
810   assert(NumOps >= 2 && OpInfo[0].RegClass == ARM::tGPRRegClassID &&
811          (OpInfo[1].RegClass==0 || OpInfo[1].RegClass==ARM::tGPRRegClassID)
812          && "Expect >=2 operands");
813
814   // Add the destination operand.
815   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
816                                                      getT1tRd(insn))));
817
818   if (OpInfo[1].RegClass == ARM::tGPRRegClassID) {
819     // Two register instructions.
820     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
821                                                        getT1tRn(insn))));
822   } else {
823     // CBNZ, CBZ
824     assert((Opcode == ARM::tCBNZ || Opcode == ARM::tCBZ) && "Invalid opcode");
825     MI.addOperand(MCOperand::CreateImm(getT1Imm6(insn) * 2));
826   }
827
828   NumOpsAdded = 2;
829
830   return true;
831 }
832
833 // A8.6.53  LDM / LDMIA
834 // A8.6.189 STM / STMIA
835 //
836 // tLDM_UPD/tSTM_UPD: tRt tRt AM4ModeImm Pred-Imm Pred-CCR register_list
837 // tLDM:              tRt AM4ModeImm Pred-Imm Pred-CCR register_list
838 static bool DisassembleThumb1LdStMul(bool Ld, MCInst &MI, unsigned Opcode,
839     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
840
841   assert((Opcode == ARM::tLDM || Opcode == ARM::tLDM_UPD ||
842           Opcode == ARM::tSTM_UPD) && "Invalid opcode");
843
844   unsigned &OpIdx = NumOpsAdded;
845
846   unsigned tRt = getT1tRt(insn);
847
848   OpIdx = 0;
849
850   // WB register, if necessary.
851   if (Opcode == ARM::tLDM_UPD || Opcode == ARM::tSTM_UPD) {
852     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
853                                                        tRt)));
854     ++OpIdx;
855   }
856
857   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
858                                                      tRt)));
859   ++OpIdx;
860
861   // A8.6.53 LDM / LDMIA / LDMFD - Encoding T1
862   // A8.6.53 STM / STMIA / STMEA - Encoding T1
863   MI.addOperand(MCOperand::CreateImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)));
864   ++OpIdx;
865
866   // Handling the two predicate operands before the reglist.
867   if (B->DoPredicateOperands(MI, Opcode, insn, NumOps))
868     OpIdx += 2;
869   else {
870     DEBUG(errs() << "Expected predicate operands not found.\n");
871     return false;
872   }
873
874   unsigned RegListBits = slice(insn, 7, 0);
875
876   // Fill the variadic part of reglist.
877   for (unsigned i = 0; i < 8; ++i) {
878     if ((RegListBits >> i) & 1) {
879       MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
880                                                          i)));
881       ++OpIdx;
882     }
883   }
884
885   return true;
886 }
887
888 static bool DisassembleThumb1LdMul(MCInst &MI, unsigned Opcode, uint32_t insn,
889     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
890   return DisassembleThumb1LdStMul(true, MI, Opcode, insn, NumOps, NumOpsAdded,
891                                   B);
892 }
893
894 static bool DisassembleThumb1StMul(MCInst &MI, unsigned Opcode, uint32_t insn,
895     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
896   return DisassembleThumb1LdStMul(false, MI, Opcode, insn, NumOps, NumOpsAdded,
897                                   B);
898 }
899
900 // A8.6.16 B Encoding T1
901 // cond = Inst{11-8} & imm8 = Inst{7-0}
902 // imm32 = SignExtend(imm8:'0', 32)
903 //
904 // tBcc: offset Pred-Imm Pred-CCR
905 // tSVC: imm8 Pred-Imm Pred-CCR
906 // tTRAP: 0 operand (early return)
907 static bool DisassembleThumb1CondBr(MCInst &MI, unsigned Opcode, uint32_t insn,
908     unsigned short NumOps, unsigned &NumOpsAdded, BO) {
909
910   if (Opcode == ARM::tTRAP)
911     return true;
912
913   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
914   if (!OpInfo) return false;
915
916   assert(NumOps == 3 && OpInfo[0].RegClass == 0 &&
917          OpInfo[1].isPredicate() && OpInfo[2].RegClass == ARM::CCRRegClassID
918          && "Exactly 3 operands expected");
919
920   unsigned Imm8 = getT1Imm8(insn);
921   MI.addOperand(MCOperand::CreateImm(
922                   Opcode == ARM::tBcc ? SignExtend32<9>(Imm8 << 1) + 4
923                                       : (int)Imm8));
924
925   // Predicate operands by ARMBasicMCBuilder::TryPredicateAndSBitModifier().
926   NumOpsAdded = 1;
927
928   return true;
929 }
930
931 // A8.6.16 B Encoding T2
932 // imm11 = Inst{10-0}
933 // imm32 = SignExtend(imm11:'0', 32)
934 //
935 // tB: offset
936 static bool DisassembleThumb1Br(MCInst &MI, unsigned Opcode, uint32_t insn,
937     unsigned short NumOps, unsigned &NumOpsAdded, BO) {
938
939   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
940   if (!OpInfo) return false;
941
942   assert(NumOps == 1 && OpInfo[0].RegClass == 0 && "1 imm operand expected");
943
944   unsigned Imm11 = getT1Imm11(insn);
945
946   // When executing a Thumb instruction, PC reads as the address of the current
947   // instruction plus 4.  The assembler subtracts 4 from the difference between
948   // the branch instruction and the target address, disassembler has to add 4 to
949   // to compensate.
950   MI.addOperand(MCOperand::CreateImm(SignExtend32<12>(Imm11 << 1) + 4));
951
952   NumOpsAdded = 1;
953
954   return true;
955
956 }
957
958 // See A6.2 16-bit Thumb instruction encoding for instruction classes
959 // corresponding to op.
960 //
961 // Table A6-1 16-bit Thumb instruction encoding (abridged)
962 // op           Instruction or instruction class
963 // ------       --------------------------------------------------------------------
964 // 00xxxx       Shift (immediate), add, subtract, move, and compare on page A6-7
965 // 010000       Data-processing on page A6-8
966 // 010001       Special data instructions and branch and exchange on page A6-9
967 // 01001x       Load from Literal Pool, see LDR (literal) on page A8-122
968 // 0101xx       Load/store single data item on page A6-10
969 // 011xxx
970 // 100xxx
971 // 10100x       Generate PC-relative address, see ADR on page A8-32
972 // 10101x       Generate SP-relative address, see ADD (SP plus immediate) on page A8-28
973 // 1011xx       Miscellaneous 16-bit instructions on page A6-11
974 // 11000x       Store multiple registers, see STM / STMIA / STMEA on page A8-374
975 // 11001x       Load multiple registers, see LDM / LDMIA / LDMFD on page A8-110 a
976 // 1101xx       Conditional branch, and Supervisor Call on page A6-13
977 // 11100x       Unconditional Branch, see B on page A8-44
978 //
979 static bool DisassembleThumb1(uint16_t op, MCInst &MI, unsigned Opcode,
980     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
981
982   unsigned op1 = slice(op, 5, 4);
983   unsigned op2 = slice(op, 3, 2);
984   unsigned op3 = slice(op, 1, 0);
985   unsigned opA = slice(op, 5, 2);
986   switch (op1) {
987   case 0:
988     // A6.2.1 Shift (immediate), add, subtract, move, and compare
989     return DisassembleThumb1General(MI, Opcode, insn, NumOps, NumOpsAdded, B);
990   case 1:
991     switch (op2) {
992     case 0:
993       switch (op3) {
994       case 0:
995         // A6.2.2 Data-processing
996         return DisassembleThumb1DP(MI, Opcode, insn, NumOps, NumOpsAdded, B);
997       case 1:
998         // A6.2.3 Special data instructions and branch and exchange
999         return DisassembleThumb1Special(MI, Opcode, insn, NumOps, NumOpsAdded,
1000                                         B);
1001       default:
1002         // A8.6.59 LDR (literal)
1003         return DisassembleThumb1LdPC(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1004       }
1005       break;
1006     default:
1007       // A6.2.4 Load/store single data item
1008       return DisassembleThumb1LdSt(opA, MI, Opcode, insn, NumOps, NumOpsAdded,
1009                                    B);
1010       break;
1011     }
1012     break;
1013   case 2:
1014     switch (op2) {
1015     case 0:
1016       // A6.2.4 Load/store single data item
1017       return DisassembleThumb1LdSt(opA, MI, Opcode, insn, NumOps, NumOpsAdded,
1018                                    B);
1019     case 1:
1020       // A6.2.4 Load/store single data item
1021       return DisassembleThumb1LdStSP(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1022     case 2:
1023       if (op3 <= 1) {
1024         // A8.6.10 ADR
1025         return DisassembleThumb1AddPCi(MI, Opcode, insn, NumOps, NumOpsAdded,
1026                                        B);
1027       } else {
1028         // A8.6.8 ADD (SP plus immediate)
1029         return DisassembleThumb1AddSPi(MI, Opcode, insn, NumOps, NumOpsAdded,
1030                                        B);
1031       }
1032     default:
1033       // A6.2.5 Miscellaneous 16-bit instructions
1034       return DisassembleThumb1Misc(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1035     }
1036     break;
1037   case 3:
1038     switch (op2) {
1039     case 0:
1040       if (op3 <= 1) {
1041         // A8.6.189 STM / STMIA / STMEA
1042         return DisassembleThumb1StMul(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1043       } else {
1044         // A8.6.53 LDM / LDMIA / LDMFD
1045         return DisassembleThumb1LdMul(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1046       }
1047     case 1:
1048       // A6.2.6 Conditional branch, and Supervisor Call
1049       return DisassembleThumb1CondBr(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1050     case 2:
1051       // Unconditional Branch, see B on page A8-44
1052       return DisassembleThumb1Br(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1053     default:
1054       assert(0 && "Unreachable code");
1055       break;
1056     }
1057     break;
1058   default:
1059     assert(0 && "Unreachable code");
1060     break;
1061   }
1062
1063   return false;
1064 }
1065
1066 ///////////////////////////////////////////////
1067 //                                           //
1068 // Thumb2 instruction disassembly functions. //
1069 //                                           //
1070 ///////////////////////////////////////////////
1071
1072 ///////////////////////////////////////////////////////////
1073 //                                                       //
1074 // Note: the register naming follows the ARM convention! //
1075 //                                                       //
1076 ///////////////////////////////////////////////////////////
1077
1078 static inline bool Thumb2SRSOpcode(unsigned Opcode) {
1079   switch (Opcode) {
1080   default:
1081     return false;
1082   case ARM::t2SRSDBW: case ARM::t2SRSDB:
1083   case ARM::t2SRSIAW: case ARM::t2SRSIA:
1084     return true;
1085   }
1086 }
1087
1088 static inline bool Thumb2RFEOpcode(unsigned Opcode) {
1089   switch (Opcode) {
1090   default:
1091     return false;
1092   case ARM::t2RFEDBW: case ARM::t2RFEDB:
1093   case ARM::t2RFEIAW: case ARM::t2RFEIA:
1094     return true;
1095   }
1096 }
1097
1098 // t2SRS[IA|DB]W/t2SRS[IA|DB]: mode_imm = Inst{4-0}
1099 static bool DisassembleThumb2SRS(MCInst &MI, unsigned Opcode, uint32_t insn,
1100     unsigned short NumOps, unsigned &NumOpsAdded) {
1101   MI.addOperand(MCOperand::CreateImm(slice(insn, 4, 0)));
1102   NumOpsAdded = 1;
1103   return true;
1104 }
1105
1106 // t2RFE[IA|DB]W/t2RFE[IA|DB]: Rn
1107 static bool DisassembleThumb2RFE(MCInst &MI, unsigned Opcode, uint32_t insn,
1108     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1109   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1110                                                      decodeRn(insn))));
1111   NumOpsAdded = 1;
1112   return true;
1113 }
1114
1115 static bool DisassembleThumb2LdStMul(MCInst &MI, unsigned Opcode, uint32_t insn,
1116     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1117
1118   if (Thumb2SRSOpcode(Opcode))
1119     return DisassembleThumb2SRS(MI, Opcode, insn, NumOps, NumOpsAdded);
1120
1121   if (Thumb2RFEOpcode(Opcode))
1122     return DisassembleThumb2RFE(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1123
1124   assert((Opcode == ARM::t2LDM || Opcode == ARM::t2LDM_UPD ||
1125           Opcode == ARM::t2STM || Opcode == ARM::t2STM_UPD)
1126          && "Invalid opcode");
1127   assert(NumOps >= 5 && "Thumb2 LdStMul expects NumOps >= 5");
1128
1129   unsigned &OpIdx = NumOpsAdded;
1130
1131   OpIdx = 0;
1132
1133   unsigned Base = getRegisterEnum(B, ARM::GPRRegClassID, decodeRn(insn));
1134
1135   // Writeback to base.
1136   if (Opcode == ARM::t2LDM_UPD || Opcode == ARM::t2STM_UPD) {
1137     MI.addOperand(MCOperand::CreateReg(Base));
1138     ++OpIdx;
1139   }
1140
1141   MI.addOperand(MCOperand::CreateReg(Base));
1142   ++OpIdx;
1143
1144   ARM_AM::AMSubMode SubMode = getAMSubModeForBits(getPUBits(insn));
1145   MI.addOperand(MCOperand::CreateImm(ARM_AM::getAM4ModeImm(SubMode)));
1146   ++OpIdx;
1147
1148   // Handling the two predicate operands before the reglist.
1149   if (B->DoPredicateOperands(MI, Opcode, insn, NumOps))
1150     OpIdx += 2;
1151   else {
1152     DEBUG(errs() << "Expected predicate operands not found.\n");
1153     return false;
1154   }
1155
1156   unsigned RegListBits = insn & ((1 << 16) - 1);
1157
1158   // Fill the variadic part of reglist.
1159   for (unsigned i = 0; i < 16; ++i) {
1160     if ((RegListBits >> i) & 1) {
1161       MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1162                                                          i)));
1163       ++OpIdx;
1164     }
1165   }
1166
1167   return true;
1168 }
1169
1170 // t2LDREX: Rd Rn
1171 // t2LDREXD: Rd Rs Rn
1172 // t2LDREXB, t2LDREXH: Rd Rn
1173 // t2STREX: Rs Rd Rn
1174 // t2STREXD: Rm Rd Rs Rn
1175 // t2STREXB, t2STREXH: Rm Rd Rn
1176 static bool DisassembleThumb2LdStEx(MCInst &MI, unsigned Opcode, uint32_t insn,
1177     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1178
1179   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
1180   if (!OpInfo) return false;
1181
1182   unsigned &OpIdx = NumOpsAdded;
1183
1184   OpIdx = 0;
1185
1186   assert(NumOps >= 2
1187          && OpInfo[0].RegClass == ARM::GPRRegClassID
1188          && OpInfo[1].RegClass == ARM::GPRRegClassID
1189          && "Expect >=2 operands and first two as reg operands");
1190
1191   bool isStore = (ARM::t2STREX <= Opcode && Opcode <= ARM::t2STREXH);
1192   bool isSW = (Opcode == ARM::t2LDREX || Opcode == ARM::t2STREX);
1193   bool isDW = (Opcode == ARM::t2LDREXD || Opcode == ARM::t2STREXD);
1194
1195   // Add the destination operand for store.
1196   if (isStore) {
1197     MI.addOperand(MCOperand::CreateReg(
1198                     getRegisterEnum(B, ARM::GPRRegClassID,
1199                                     isSW ? decodeRs(insn) : decodeRm(insn))));
1200     ++OpIdx;
1201   }
1202
1203   // Source operand for store and destination operand for load.
1204   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1205                                                      decodeRd(insn))));
1206   ++OpIdx;
1207
1208   // Thumb2 doubleword complication: with an extra source/destination operand.
1209   if (isDW) {
1210     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1211                                                        decodeRs(insn))));
1212     ++OpIdx;
1213   }
1214
1215   // Finally add the pointer operand.
1216   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1217                                                      decodeRn(insn))));
1218   ++OpIdx;
1219
1220   return true;
1221 }
1222
1223 // LLVM, as of Jan-05-2010, does not output <Rt2>, i.e., Rs, in the asm.
1224 // Whereas the ARM Arch. Manual does not require that t2 = t+1 like in ARM ISA.
1225 //
1226 // t2LDRDi8: Rd Rs Rn imm8s4 (offset mode)
1227 // t2LDRDpci: Rd Rs imm8s4 (Not decoded, prefer the generic t2LDRDi8 version)
1228 // t2STRDi8: Rd Rs Rn imm8s4 (offset mode)
1229 //
1230 // Ditto for t2LDRD_PRE, t2LDRD_POST, t2STRD_PRE, t2STRD_POST, which are for
1231 // disassembly only and do not have a tied_to writeback base register operand.
1232 static bool DisassembleThumb2LdStDual(MCInst &MI, unsigned Opcode,
1233     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1234
1235   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
1236   if (!OpInfo) return false;
1237
1238   assert(NumOps >= 4
1239          && OpInfo[0].RegClass == ARM::GPRRegClassID
1240          && OpInfo[1].RegClass == ARM::GPRRegClassID
1241          && OpInfo[2].RegClass == ARM::GPRRegClassID
1242          && OpInfo[3].RegClass == 0
1243          && "Expect >= 4 operands and first 3 as reg operands");
1244
1245   // Add the <Rt> <Rt2> operands.
1246   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1247                                                      decodeRd(insn))));
1248   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1249                                                      decodeRs(insn))));
1250   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1251                                                      decodeRn(insn))));
1252
1253   // Finally add (+/-)imm8*4, depending on the U bit.
1254   int Offset = getImm8(insn) * 4;
1255   if (getUBit(insn) == 0)
1256     Offset = -Offset;
1257   MI.addOperand(MCOperand::CreateImm(Offset));
1258   NumOpsAdded = 4;
1259
1260   return true;
1261 }
1262
1263 // PC-based defined for Codegen, which do not get decoded by design:
1264 //
1265 // t2TBB, t2TBH: Rm immDontCare immDontCare
1266 //
1267 // Generic version defined for disassembly:
1268 //
1269 // t2TBBgen, t2TBHgen: Rn Rm Pred-Imm Pred-CCR
1270 static bool DisassembleThumb2TB(MCInst &MI, unsigned Opcode,
1271     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1272
1273   assert(NumOps >= 2 && "Expect >= 2 operands");
1274
1275   // The generic version of TBB/TBH needs a base register.
1276   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1277                                                      decodeRn(insn))));
1278   // Add the index register.
1279   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1280                                                      decodeRm(insn))));
1281   NumOpsAdded = 2;
1282
1283   return true;
1284 }
1285
1286 static inline bool Thumb2ShiftOpcode(unsigned Opcode) {
1287   switch (Opcode) {
1288   default:
1289     return false;
1290   case ARM::t2MOVCClsl: case ARM::t2MOVCClsr:
1291   case ARM::t2MOVCCasr: case ARM::t2MOVCCror:
1292   case ARM::t2LSLri:    case ARM::t2LSRri:
1293   case ARM::t2ASRri:    case ARM::t2RORri:
1294     return true;
1295   }
1296 }
1297
1298 // A6.3.11 Data-processing (shifted register)
1299 //
1300 // Two register operands (Rn=0b1111 no 1st operand reg): Rs Rm
1301 // Two register operands (Rs=0b1111 no dst operand reg): Rn Rm
1302 // Three register operands: Rs Rn Rm
1303 // Three register operands: (Rn=0b1111 Conditional Move) Rs Ro(TIED_TO) Rm
1304 //
1305 // Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
1306 // register with shift forms: (Rm, ConstantShiftSpecifier).
1307 // Constant shift specifier: Imm = (ShOp | ShAmt<<3).
1308 //
1309 // There are special instructions, like t2MOVsra_flag and t2MOVsrl_flag, which
1310 // only require two register operands: Rd, Rm in ARM Reference Manual terms, and
1311 // nothing else, because the shift amount is already specified.
1312 // Similar case holds for t2MOVrx, t2ADDrr, ..., etc.
1313 static bool DisassembleThumb2DPSoReg(MCInst &MI, unsigned Opcode, uint32_t insn,
1314     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1315
1316   const TargetInstrDesc &TID = ARMInsts[Opcode];
1317   const TargetOperandInfo *OpInfo = TID.OpInfo;
1318   unsigned &OpIdx = NumOpsAdded;
1319
1320   // Special case handling.
1321   if (Opcode == ARM::t2BR_JT) {
1322     assert(NumOps == 4
1323            && OpInfo[0].RegClass == ARM::GPRRegClassID
1324            && OpInfo[1].RegClass == ARM::GPRRegClassID
1325            && OpInfo[2].RegClass == 0
1326            && OpInfo[3].RegClass == 0
1327            && "Exactlt 4 operands expect and first two as reg operands");
1328     // Only need to populate the src reg operand.
1329     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1330                                                        decodeRm(insn))));
1331     MI.addOperand(MCOperand::CreateReg(0));
1332     MI.addOperand(MCOperand::CreateImm(0));
1333     MI.addOperand(MCOperand::CreateImm(0));
1334     NumOpsAdded = 4;
1335     return true;
1336   }
1337
1338   OpIdx = 0;
1339
1340   assert(NumOps >= 2
1341          && OpInfo[0].RegClass == ARM::GPRRegClassID
1342          && OpInfo[1].RegClass == ARM::GPRRegClassID
1343          && "Expect >= 2 operands and first two as reg operands");
1344
1345   bool ThreeReg = (NumOps > 2 && OpInfo[2].RegClass == ARM::GPRRegClassID);
1346   bool NoDstReg = (decodeRs(insn) == 0xF);
1347
1348   // Build the register operands, followed by the constant shift specifier.
1349
1350   MI.addOperand(MCOperand::CreateReg(
1351                   getRegisterEnum(B, ARM::GPRRegClassID,
1352                                   NoDstReg ? decodeRn(insn) : decodeRs(insn))));
1353   ++OpIdx;
1354
1355   if (ThreeReg) {
1356     int Idx;
1357     if ((Idx = TID.getOperandConstraint(OpIdx, TOI::TIED_TO)) != -1) {
1358       // Process tied_to operand constraint.
1359       MI.addOperand(MI.getOperand(Idx));
1360       ++OpIdx;
1361     } else if (!NoDstReg) {
1362       MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1363                                                          decodeRn(insn))));
1364       ++OpIdx;
1365     } else {
1366       DEBUG(errs() << "Thumb encoding error: d==15 for three-reg operands.\n");
1367       return false;
1368     }
1369   }
1370
1371   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1372                                                      decodeRm(insn))));
1373   ++OpIdx;
1374
1375   if (NumOps == OpIdx)
1376     return true;
1377
1378   if (OpInfo[OpIdx].RegClass == 0 && !OpInfo[OpIdx].isPredicate()
1379       && !OpInfo[OpIdx].isOptionalDef()) {
1380
1381     if (Thumb2ShiftOpcode(Opcode))
1382       MI.addOperand(MCOperand::CreateImm(getShiftAmtBits(insn)));
1383     else {
1384       // Build the constant shift specifier operand.
1385       unsigned bits2 = getShiftTypeBits(insn);
1386       unsigned imm5 = getShiftAmtBits(insn);
1387       ARM_AM::ShiftOpc ShOp = ARM_AM::no_shift;
1388       unsigned ShAmt = decodeImmShift(bits2, imm5, ShOp);
1389
1390       // PKHBT/PKHTB are special in that we need the decodeImmShift() call to
1391       // decode the shift amount from raw imm5 and bits2, but we DO NOT need
1392       // to encode the ShOp, as it's in the asm string already.
1393       if (Opcode == ARM::t2PKHBT || Opcode == ARM::t2PKHTB)
1394         MI.addOperand(MCOperand::CreateImm(ShAmt));
1395       else
1396         MI.addOperand(MCOperand::CreateImm(ARM_AM::getSORegOpc(ShOp, ShAmt)));
1397     }
1398     ++OpIdx;
1399   }
1400
1401   return true;
1402 }
1403
1404 // A6.3.1 Data-processing (modified immediate)
1405 //
1406 // Two register operands: Rs Rn ModImm
1407 // One register operands (Rs=0b1111 no explicit dest reg): Rn ModImm
1408 // One register operands (Rn=0b1111 no explicit src reg): Rs ModImm - {t2MOVi, t2MVNi}
1409 //
1410 // ModImm = ThumbExpandImm(i:imm3:imm8)
1411 static bool DisassembleThumb2DPModImm(MCInst &MI, unsigned Opcode,
1412     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1413
1414   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
1415   unsigned &OpIdx = NumOpsAdded;
1416
1417   OpIdx = 0;
1418
1419   assert(NumOps >= 2 && OpInfo[0].RegClass == ARM::GPRRegClassID
1420          && "Expect >= 2 operands and first one as reg operand");
1421
1422   bool TwoReg = (OpInfo[1].RegClass == ARM::GPRRegClassID);
1423   bool NoDstReg = (decodeRs(insn) == 0xF);
1424
1425   // Build the register operands, followed by the modified immediate.
1426
1427   MI.addOperand(MCOperand::CreateReg(
1428                   getRegisterEnum(B, ARM::GPRRegClassID,
1429                                   NoDstReg ? decodeRn(insn) : decodeRs(insn))));
1430   ++OpIdx;
1431
1432   if (TwoReg) {
1433     if (NoDstReg) {
1434       DEBUG(errs() << "Thumb encoding error: d==15 for DPModImm 2-reg instr.\n");
1435       return false;
1436     }
1437     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1438                                                        decodeRn(insn))));
1439     ++OpIdx;
1440   }
1441
1442   // The modified immediate operand should come next.
1443   assert(OpIdx < NumOps && OpInfo[OpIdx].RegClass == 0 &&
1444          !OpInfo[OpIdx].isPredicate() && !OpInfo[OpIdx].isOptionalDef()
1445          && "Pure imm operand expected");
1446
1447   // i:imm3:imm8
1448   // A6.3.2 Modified immediate constants in Thumb instructions
1449   unsigned imm12 = getIImm3Imm8(insn);
1450   MI.addOperand(MCOperand::CreateImm(ThumbExpandImm(imm12)));
1451   ++OpIdx;
1452
1453   return true;
1454 }
1455
1456 static inline bool Thumb2SaturateOpcode(unsigned Opcode) {
1457   switch (Opcode) {
1458   case ARM::t2SSATlsl: case ARM::t2SSATasr: case ARM::t2SSAT16:
1459   case ARM::t2USATlsl: case ARM::t2USATasr: case ARM::t2USAT16:
1460     return true;
1461   default:
1462     return false;
1463   }
1464 }
1465
1466 static inline unsigned decodeThumb2SaturatePos(unsigned Opcode, uint32_t insn) {
1467   switch (Opcode) {
1468   case ARM::t2SSATlsl:
1469   case ARM::t2SSATasr:
1470     return slice(insn, 4, 0) + 1;
1471   case ARM::t2SSAT16:
1472     return slice(insn, 3, 0) + 1;
1473   case ARM::t2USATlsl:
1474   case ARM::t2USATasr:
1475     return slice(insn, 4, 0);
1476   case ARM::t2USAT16:
1477     return slice(insn, 3, 0);
1478   default:
1479     assert(0 && "Invalid opcode passed in");
1480     return 0;
1481   }
1482 }
1483
1484 // A6.3.3 Data-processing (plain binary immediate)
1485 //
1486 // o t2ADDri12, t2SUBri12: Rs Rn imm12
1487 // o t2LEApcrel (ADR): Rs imm12
1488 // o t2BFC (BFC): Rs Ro(TIED_TO) bf_inv_mask_imm
1489 // o t2BFI (BFI) (Currently not defined in LLVM as of Jan-07-2010)
1490 // o t2MOVi16: Rs imm16
1491 // o t2MOVTi16: Rs imm16
1492 // o t2SBFX (SBFX): Rs Rn lsb width
1493 // o t2UBFX (UBFX): Rs Rn lsb width
1494 // o t2BFI (BFI): Rs Rn lsb width
1495 //
1496 // [Signed|Unsigned] Saturate [16]
1497 //
1498 // o t2SSAT[lsl|asr], t2USAT[lsl|asr]: Rs sat_pos Rn shamt
1499 // o t2SSAT16, t2USAT16: Rs sat_pos Rn
1500 static bool DisassembleThumb2DPBinImm(MCInst &MI, unsigned Opcode,
1501     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1502
1503   const TargetInstrDesc &TID = ARMInsts[Opcode];
1504   const TargetOperandInfo *OpInfo = TID.OpInfo;
1505   unsigned &OpIdx = NumOpsAdded;
1506
1507   OpIdx = 0;
1508
1509   assert(NumOps >= 2 && OpInfo[0].RegClass == ARM::GPRRegClassID
1510          && "Expect >= 2 operands and first one as reg operand");
1511
1512   bool TwoReg = (OpInfo[1].RegClass == ARM::GPRRegClassID);
1513
1514   // Build the register operand(s), followed by the immediate(s).
1515
1516   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1517                                                      decodeRs(insn))));
1518   ++OpIdx;
1519
1520   // t2SSAT/t2SSAT16/t2USAT/t2USAT16 has imm operand after Rd.
1521   if (Thumb2SaturateOpcode(Opcode)) {
1522     MI.addOperand(MCOperand::CreateImm(decodeThumb2SaturatePos(Opcode, insn)));
1523
1524     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1525                                                        decodeRn(insn))));
1526
1527     if (Opcode == ARM::t2SSAT16 || Opcode == ARM::t2USAT16) {
1528       OpIdx += 2;
1529       return true;
1530     }
1531
1532     // For SSAT operand reg (Rn) has been disassembled above.
1533     // Now disassemble the shift amount.
1534
1535     // Inst{14-12:7-6} encodes the imm5 shift amount.
1536     unsigned ShAmt = slice(insn, 14, 12) << 2 | slice(insn, 7, 6);
1537
1538     MI.addOperand(MCOperand::CreateImm(ShAmt));
1539
1540     OpIdx += 3;
1541     return true;
1542   }
1543
1544   if (TwoReg) {
1545     assert(NumOps >= 3 && "Expect >= 3 operands");
1546     int Idx;
1547     if ((Idx = TID.getOperandConstraint(OpIdx, TOI::TIED_TO)) != -1) {
1548       // Process tied_to operand constraint.
1549       MI.addOperand(MI.getOperand(Idx));
1550     } else {
1551       // Add src reg operand.
1552       MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1553                                                          decodeRn(insn))));
1554     }
1555     ++OpIdx;
1556   }
1557
1558   assert(OpInfo[OpIdx].RegClass == 0 && !OpInfo[OpIdx].isPredicate()
1559          && !OpInfo[OpIdx].isOptionalDef()
1560          && "Pure imm operand expected");
1561
1562   // Pre-increment OpIdx.
1563   ++OpIdx;
1564
1565   if (Opcode == ARM::t2ADDri12 || Opcode == ARM::t2SUBri12
1566       || Opcode == ARM::t2LEApcrel)
1567     MI.addOperand(MCOperand::CreateImm(getIImm3Imm8(insn)));
1568   else if (Opcode == ARM::t2MOVi16 || Opcode == ARM::t2MOVTi16)
1569     MI.addOperand(MCOperand::CreateImm(getImm16(insn)));
1570   else if (Opcode == ARM::t2BFC) {
1571     uint32_t mask = 0;
1572     if (getBitfieldInvMask(insn, mask))
1573       MI.addOperand(MCOperand::CreateImm(mask));
1574     else
1575       return false;
1576   } else {
1577     // Handle the case of: lsb width
1578     assert((Opcode == ARM::t2SBFX || Opcode == ARM::t2UBFX ||
1579             Opcode == ARM::t2BFI) && "Invalid opcode");
1580     MI.addOperand(MCOperand::CreateImm(getLsb(insn)));
1581     if (Opcode == ARM::t2BFI) {
1582       if (getMsb(insn) < getLsb(insn)) {
1583         DEBUG(errs() << "Encoding error: msb < lsb\n");
1584         return false;
1585       }
1586       MI.addOperand(MCOperand::CreateImm(getMsb(insn) - getLsb(insn) + 1));
1587     } else
1588       MI.addOperand(MCOperand::CreateImm(getWidthMinus1(insn) + 1));
1589
1590     ++OpIdx;
1591   }
1592
1593   return true;
1594 }
1595
1596 // A6.3.4 Table A6-15 Miscellaneous control instructions
1597 // A8.6.41 DMB
1598 // A8.6.42 DSB
1599 // A8.6.49 ISB
1600 static inline bool t2MiscCtrlInstr(uint32_t insn) {
1601   if (slice(insn, 31, 20) == 0xf3b && slice(insn, 15, 14) == 2 &&
1602       slice(insn, 12, 12) == 0)
1603     return true;
1604
1605   return false;
1606 }
1607
1608 // A6.3.4 Branches and miscellaneous control
1609 //
1610 // A8.6.16 B
1611 // Branches: t2B, t2Bcc -> imm operand
1612 //
1613 // Branches: t2TPsoft -> no operand
1614 //
1615 // A8.6.23 BL, BLX (immediate)
1616 // Branches (defined in ARMInstrThumb.td): tBLr9, tBLXi_r9 -> imm operand
1617 //
1618 // A8.6.26
1619 // t2BXJ -> Rn
1620 //
1621 // Miscellaneous control: t2Int_MemBarrierV7 (and its t2DMB variants),
1622 // t2Int_SyncBarrierV7 (and its t2DSB varianst), t2ISBsy, t2CLREX
1623 //   -> no operand (except pred-imm pred-ccr for CLREX, memory barrier variants)
1624 //
1625 // Hint: t2NOP, t2YIELD, t2WFE, t2WFI, t2SEV
1626 //   -> no operand (except pred-imm pred-ccr)
1627 //
1628 // t2DBG -> imm4 = Inst{3-0}
1629 //
1630 // t2MRS/t2MRSsys -> Rs
1631 // t2MSR/t2MSRsys -> Rn mask=Inst{11-8}
1632 // t2SMC -> imm4 = Inst{19-16}
1633 static bool DisassembleThumb2BrMiscCtrl(MCInst &MI, unsigned Opcode,
1634     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1635
1636   if (NumOps == 0)
1637     return true;
1638
1639   if (t2MiscCtrlInstr(insn))
1640     return true;
1641
1642   switch (Opcode) {
1643   case ARM::t2CLREX:
1644   case ARM::t2NOP:
1645   case ARM::t2YIELD:
1646   case ARM::t2WFE:
1647   case ARM::t2WFI:
1648   case ARM::t2SEV:
1649     return true;
1650   default:
1651     break;
1652   }
1653
1654   // CPS has a singleton $opt operand that contains the following information:
1655   // opt{4-0} = mode from Inst{4-0}
1656   // opt{5} = changemode from Inst{8}
1657   // opt{8-6} = AIF from Inst{7-5}
1658   // opt{10-9} = imod from Inst{10-9} with 0b10 as enable and 0b11 as disable
1659   if (Opcode == ARM::t2CPS) {
1660     unsigned Option = slice(insn, 4, 0) | slice(insn, 8, 8) << 5 |
1661       slice(insn, 7, 5) << 6 | slice(insn, 10, 9) << 9;
1662     MI.addOperand(MCOperand::CreateImm(Option));
1663     NumOpsAdded = 1;
1664     return true;
1665   }
1666
1667   // DBG has its option specified in Inst{3-0}.
1668   if (Opcode == ARM::t2DBG) {
1669     MI.addOperand(MCOperand::CreateImm(slice(insn, 3, 0)));
1670     NumOpsAdded = 1;
1671     return true;
1672   }
1673
1674   // MRS and MRSsys take one GPR reg Rs.
1675   if (Opcode == ARM::t2MRS || Opcode == ARM::t2MRSsys) {
1676     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1677                                                        decodeRs(insn))));
1678     NumOpsAdded = 1;
1679     return true;
1680   }
1681   // BXJ takes one GPR reg Rn.
1682   if (Opcode == ARM::t2BXJ) {
1683     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1684                                                        decodeRn(insn))));
1685     NumOpsAdded = 1;
1686     return true;
1687   }
1688   // MSR and MSRsys take one GPR reg Rn, followed by the mask.
1689   if (Opcode == ARM::t2MSR || Opcode == ARM::t2MSRsys || Opcode == ARM::t2BXJ) {
1690     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1691                                                        decodeRn(insn))));
1692     MI.addOperand(MCOperand::CreateImm(slice(insn, 11, 8)));
1693     NumOpsAdded = 2;
1694     return true;
1695   }
1696   // SMC take imm4.
1697   if (Opcode == ARM::t2SMC) {
1698     MI.addOperand(MCOperand::CreateImm(slice(insn, 19, 16)));
1699     NumOpsAdded = 1;
1700     return true;
1701   }
1702
1703   // Add the imm operand.
1704   int Offset = 0;
1705
1706   switch (Opcode) {
1707   default:
1708     assert(0 && "Unreachable code");
1709     return false;
1710   case ARM::t2B:
1711     Offset = decodeImm32_B_EncodingT4(insn);
1712     break;
1713   case ARM::t2Bcc:
1714     Offset = decodeImm32_B_EncodingT3(insn);
1715     break;
1716   case ARM::tBLr9:
1717     Offset = decodeImm32_BL(insn);
1718     break;
1719   case ARM::tBLXi_r9:
1720     Offset = decodeImm32_BLX(insn);
1721     break;
1722   }
1723   // When executing a Thumb instruction, PC reads as the address of the current
1724   // instruction plus 4.  The assembler subtracts 4 from the difference between
1725   // the branch instruction and the target address, disassembler has to add 4 to
1726   // to compensate.
1727   MI.addOperand(MCOperand::CreateImm(Offset + 4));
1728
1729   NumOpsAdded = 1;
1730
1731   return true;
1732 }
1733
1734 static inline bool Thumb2PreloadOpcode(unsigned Opcode) {
1735   switch (Opcode) {
1736   default:
1737     return false;
1738   case ARM::t2PLDi12:   case ARM::t2PLDi8:   case ARM::t2PLDpci:
1739   case ARM::t2PLDr:     case ARM::t2PLDs:
1740   case ARM::t2PLDWi12:  case ARM::t2PLDWi8:  case ARM::t2PLDWpci:
1741   case ARM::t2PLDWr:    case ARM::t2PLDWs:
1742   case ARM::t2PLIi12:   case ARM::t2PLIi8:   case ARM::t2PLIpci:
1743   case ARM::t2PLIr:     case ARM::t2PLIs:
1744     return true;
1745   }
1746 }
1747
1748 static bool DisassembleThumb2PreLoad(MCInst &MI, unsigned Opcode, uint32_t insn,
1749     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1750
1751   // Preload Data/Instruction requires either 2 or 3 operands.
1752   // t2PLDi12, t2PLDi8, t2PLDpci: Rn [+/-]imm12/imm8
1753   // t2PLDr:                      Rn Rm
1754   // t2PLDs:                      Rn Rm imm2=Inst{5-4}
1755   // Same pattern applies for t2PLDW* and t2PLI*.
1756
1757   const TargetInstrDesc &TID = ARMInsts[Opcode];
1758   const TargetOperandInfo *OpInfo = TID.OpInfo;
1759   unsigned &OpIdx = NumOpsAdded;
1760
1761   OpIdx = 0;
1762
1763   assert(NumOps >= 2 &&
1764          OpInfo[0].RegClass == ARM::GPRRegClassID &&
1765          "Expect >= 2 operands and first one as reg operand");
1766
1767   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1768                                                      decodeRn(insn))));
1769   ++OpIdx;
1770
1771   if (OpInfo[OpIdx].RegClass == ARM::GPRRegClassID) {
1772     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1773                                                        decodeRm(insn))));
1774   } else {
1775     assert(OpInfo[OpIdx].RegClass == 0 && !OpInfo[OpIdx].isPredicate()
1776            && !OpInfo[OpIdx].isOptionalDef()
1777            && "Pure imm operand expected");
1778     int Offset = 0;
1779     if (Opcode == ARM::t2PLDpci || Opcode == ARM::t2PLDWpci ||
1780              Opcode == ARM::t2PLIpci) {
1781       bool Negative = slice(insn, 23, 23) == 0;
1782       unsigned Imm12 = getImm12(insn);
1783       Offset = Negative ? -1 - Imm12 : 1 * Imm12;      
1784     } else if (Opcode == ARM::t2PLDi8 || Opcode == ARM::t2PLDWi8 ||
1785                Opcode == ARM::t2PLIi8) {
1786       // A8.6.117 Encoding T2: add = FALSE
1787       unsigned Imm8 = getImm8(insn);
1788       Offset = -1 - Imm8;
1789     } else // The i12 forms.  See, for example, A8.6.117 Encoding T1.
1790       Offset = decodeImm12(insn);
1791     MI.addOperand(MCOperand::CreateImm(Offset));
1792   }
1793   ++OpIdx;
1794
1795   if (OpIdx < NumOps && OpInfo[OpIdx].RegClass == 0 &&
1796       !OpInfo[OpIdx].isPredicate() && !OpInfo[OpIdx].isOptionalDef()) {
1797     // Fills in the shift amount for t2PLDs, t2PLDWs, t2PLIs.
1798     MI.addOperand(MCOperand::CreateImm(slice(insn, 5, 4)));
1799     ++OpIdx;
1800   }
1801
1802   return true;
1803 }
1804
1805 // A8.6.63 LDRB (literal)
1806 // A8.6.79 LDRSB (literal)
1807 // A8.6.75 LDRH (literal)
1808 // A8.6.83 LDRSH (literal)
1809 // A8.6.59 LDR (literal)
1810 //
1811 // These instrs calculate an address from the PC value and an immediate offset.
1812 // Rd Rn=PC (+/-)imm12 (+ if Inst{23} == 0b1)
1813 static bool DisassembleThumb2Ldpci(MCInst &MI, unsigned Opcode,
1814     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1815
1816   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
1817   if (!OpInfo) return false;
1818
1819   assert(NumOps >= 2 &&
1820          OpInfo[0].RegClass == ARM::GPRRegClassID &&
1821          OpInfo[1].RegClass == 0 &&
1822          "Expect >= 2 operands, first as reg, and second as imm operand");
1823
1824   // Build the register operand, followed by the (+/-)imm12 immediate.
1825
1826   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1827                                                      decodeRd(insn))));
1828
1829   MI.addOperand(MCOperand::CreateImm(decodeImm12(insn)));
1830
1831   NumOpsAdded = 2;
1832
1833   return true;
1834 }
1835
1836 // A6.3.10 Store single data item
1837 // A6.3.9 Load byte, memory hints
1838 // A6.3.8 Load halfword, memory hints
1839 // A6.3.7 Load word
1840 //
1841 // For example,
1842 //
1843 // t2LDRi12:   Rd Rn (+)imm12
1844 // t2LDRi8:    Rd Rn (+/-)imm8 (+ if Inst{9} == 0b1)
1845 // t2LDRs:     Rd Rn Rm ConstantShiftSpecifier (see also DisassembleThumb2DPSoReg)
1846 // t2LDR_POST: Rd Rn Rn(TIED_TO) (+/-)imm8 (+ if Inst{9} == 0b1)
1847 // t2LDR_PRE:  Rd Rn Rn(TIED_TO) (+/-)imm8 (+ if Inst{9} == 0b1)
1848 //
1849 // t2STRi12:   Rd Rn (+)imm12
1850 // t2STRi8:    Rd Rn (+/-)imm8 (+ if Inst{9} == 0b1)
1851 // t2STRs:     Rd Rn Rm ConstantShiftSpecifier (see also DisassembleThumb2DPSoReg)
1852 // t2STR_POST: Rn Rd Rn(TIED_TO) (+/-)imm8 (+ if Inst{9} == 0b1)
1853 // t2STR_PRE:  Rn Rd Rn(TIED_TO) (+/-)imm8 (+ if Inst{9} == 0b1)
1854 //
1855 // Note that for indexed modes, the Rn(TIED_TO) operand needs to be populated
1856 // correctly, as LLVM AsmPrinter depends on it.  For indexed stores, the first
1857 // operand is Rn; for all the other instructions, Rd is the first operand.
1858 //
1859 // Delegates to DisassembleThumb2PreLoad() for preload data/instruction.
1860 // Delegates to DisassembleThumb2Ldpci() for load * literal operations.
1861 static bool DisassembleThumb2LdSt(bool Load, MCInst &MI, unsigned Opcode,
1862     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1863
1864   unsigned Rn = decodeRn(insn);
1865
1866   if (Thumb2PreloadOpcode(Opcode))
1867     return DisassembleThumb2PreLoad(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1868
1869   // See, for example, A6.3.7 Load word: Table A6-18 Load word.
1870   if (Load && Rn == 15)
1871     return DisassembleThumb2Ldpci(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1872
1873   const TargetInstrDesc &TID = ARMInsts[Opcode];
1874   const TargetOperandInfo *OpInfo = TID.OpInfo;
1875   unsigned &OpIdx = NumOpsAdded;
1876
1877   OpIdx = 0;
1878
1879   assert(NumOps >= 3 &&
1880          OpInfo[0].RegClass == ARM::GPRRegClassID &&
1881          OpInfo[1].RegClass == ARM::GPRRegClassID &&
1882          "Expect >= 3 operands and first two as reg operands");
1883
1884   bool ThreeReg = (OpInfo[2].RegClass == ARM::GPRRegClassID);
1885   bool TIED_TO = ThreeReg && TID.getOperandConstraint(2, TOI::TIED_TO) != -1;
1886   bool Imm12 = !ThreeReg && slice(insn, 23, 23) == 1; // ARMInstrThumb2.td
1887
1888   // Build the register operands, followed by the immediate.
1889   unsigned R0, R1, R2 = 0;
1890   unsigned Rd = decodeRd(insn);
1891   int Imm = 0;
1892
1893   if (!Load && TIED_TO) {
1894     R0 = Rn;
1895     R1 = Rd;
1896   } else {
1897     R0 = Rd;
1898     R1 = Rn;
1899   }
1900   if (ThreeReg) {
1901     if (TIED_TO) {
1902       R2 = Rn;
1903       Imm = decodeImm8(insn);
1904     } else {
1905       R2 = decodeRm(insn);
1906       // See, for example, A8.6.64 LDRB (register).
1907       // And ARMAsmPrinter::printT2AddrModeSoRegOperand().
1908       // LSL is the default shift opc, and LLVM does not expect it to be encoded
1909       // as part of the immediate operand.
1910       // Imm = ARM_AM::getSORegOpc(ARM_AM::lsl, slice(insn, 5, 4));
1911       Imm = slice(insn, 5, 4);
1912     }
1913   } else {
1914     if (Imm12)
1915       Imm = getImm12(insn);
1916     else
1917       Imm = decodeImm8(insn);
1918   }
1919   
1920   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1921                                                      R0)));
1922   ++OpIdx;
1923   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1924                                                      R1)));
1925   ++OpIdx;
1926
1927   if (ThreeReg) {
1928     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1929                                                        R2)));
1930     ++OpIdx;
1931   }
1932
1933   assert(OpInfo[OpIdx].RegClass == 0 && !OpInfo[OpIdx].isPredicate()
1934          && !OpInfo[OpIdx].isOptionalDef()
1935          && "Pure imm operand expected");
1936
1937   MI.addOperand(MCOperand::CreateImm(Imm));
1938   ++OpIdx;
1939
1940   return true;
1941 }
1942
1943 // A6.3.12 Data-processing (register)
1944 //
1945 // Two register operands [rotate]:   Rs Rm [rotation(= (rotate:'000'))]
1946 // Three register operands only:     Rs Rn Rm
1947 // Three register operands [rotate]: Rs Rn Rm [rotation(= (rotate:'000'))]
1948 //
1949 // Parallel addition and subtraction 32-bit Thumb instructions: Rs Rn Rm
1950 //
1951 // Miscellaneous operations: Rs [Rn] Rm
1952 static bool DisassembleThumb2DPReg(MCInst &MI, unsigned Opcode, uint32_t insn,
1953     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1954
1955   const TargetInstrDesc &TID = ARMInsts[Opcode];
1956   const TargetOperandInfo *OpInfo = TID.OpInfo;
1957   unsigned &OpIdx = NumOpsAdded;
1958
1959   OpIdx = 0;
1960
1961   assert(NumOps >= 2 &&
1962          OpInfo[0].RegClass == ARM::GPRRegClassID &&
1963          OpInfo[1].RegClass == ARM::GPRRegClassID &&
1964          "Expect >= 2 operands and first two as reg operands");
1965
1966   // Build the register operands, followed by the optional rotation amount.
1967
1968   bool ThreeReg = NumOps > 2 && OpInfo[2].RegClass == ARM::GPRRegClassID;
1969
1970   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1971                                                      decodeRs(insn))));
1972   ++OpIdx;
1973
1974   if (ThreeReg) {
1975     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1976                                                        decodeRn(insn))));
1977     ++OpIdx;
1978   }
1979
1980   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1981                                                      decodeRm(insn))));
1982   ++OpIdx;
1983
1984   if (OpIdx < NumOps && OpInfo[OpIdx].RegClass == 0
1985       && !OpInfo[OpIdx].isPredicate() && !OpInfo[OpIdx].isOptionalDef()) {
1986     // Add the rotation amount immediate.
1987     MI.addOperand(MCOperand::CreateImm(decodeRotate(insn)));
1988     ++OpIdx;
1989   }
1990
1991   return true;
1992 }
1993
1994 // A6.3.16 Multiply, multiply accumulate, and absolute difference
1995 //
1996 // t2MLA, t2MLS, t2SMMLA, t2SMMLS: Rs Rn Rm Ra=Inst{15-12}
1997 // t2MUL, t2SMMUL:                 Rs Rn Rm
1998 // t2SMLA[BB|BT|TB|TT|WB|WT]:      Rs Rn Rm Ra=Inst{15-12}
1999 // t2SMUL[BB|BT|TB|TT|WB|WT]:      Rs Rn Rm
2000 //
2001 // Dual halfword multiply: t2SMUAD[X], t2SMUSD[X], t2SMLAD[X], t2SMLSD[X]:
2002 //   Rs Rn Rm Ra=Inst{15-12}
2003 //
2004 // Unsigned Sum of Absolute Differences [and Accumulate]
2005 //    Rs Rn Rm [Ra=Inst{15-12}]
2006 static bool DisassembleThumb2Mul(MCInst &MI, unsigned Opcode, uint32_t insn,
2007     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
2008
2009   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
2010
2011   assert(NumOps >= 3 &&
2012          OpInfo[0].RegClass == ARM::GPRRegClassID &&
2013          OpInfo[1].RegClass == ARM::GPRRegClassID &&
2014          OpInfo[2].RegClass == ARM::GPRRegClassID &&
2015          "Expect >= 3 operands and first three as reg operands");
2016
2017   // Build the register operands.
2018
2019   bool FourReg = NumOps > 3 && OpInfo[3].RegClass == ARM::GPRRegClassID;
2020
2021   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2022                                                      decodeRs(insn))));
2023
2024   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2025                                                      decodeRn(insn))));
2026
2027   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2028                                                      decodeRm(insn))));
2029
2030   if (FourReg)
2031     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2032                                                        decodeRd(insn))));
2033
2034   NumOpsAdded = FourReg ? 4 : 3;
2035
2036   return true;
2037 }
2038
2039 // A6.3.17 Long multiply, long multiply accumulate, and divide
2040 //
2041 // t2SMULL, t2UMULL, t2SMLAL, t2UMLAL, t2UMAAL: RdLo RdHi Rn Rm
2042 // where RdLo = Inst{15-12} and RdHi = Inst{11-8}
2043 //
2044 // Halfword multiple accumulate long: t2SMLAL<x><y>: RdLo RdHi Rn Rm
2045 // where RdLo = Inst{15-12} and RdHi = Inst{11-8}
2046 //
2047 // Dual halfword multiple: t2SMLALD[X], t2SMLSLD[X]: RdLo RdHi Rn Rm
2048 // where RdLo = Inst{15-12} and RdHi = Inst{11-8}
2049 //
2050 // Signed/Unsigned divide: t2SDIV, t2UDIV: Rs Rn Rm
2051 static bool DisassembleThumb2LongMul(MCInst &MI, unsigned Opcode, uint32_t insn,
2052     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
2053
2054   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
2055
2056   assert(NumOps >= 3 &&
2057          OpInfo[0].RegClass == ARM::GPRRegClassID &&
2058          OpInfo[1].RegClass == ARM::GPRRegClassID &&
2059          OpInfo[2].RegClass == ARM::GPRRegClassID &&
2060          "Expect >= 3 operands and first three as reg operands");
2061
2062   bool FourReg = NumOps > 3 && OpInfo[3].RegClass == ARM::GPRRegClassID;
2063
2064   // Build the register operands.
2065
2066   if (FourReg)
2067     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2068                                                        decodeRd(insn))));
2069
2070   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2071                                                      decodeRs(insn))));
2072
2073   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2074                                                      decodeRn(insn))));
2075
2076   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2077                                                      decodeRm(insn))));
2078
2079   if (FourReg)
2080     NumOpsAdded = 4;
2081   else
2082     NumOpsAdded = 3;
2083
2084   return true;
2085 }
2086
2087 // See A6.3 32-bit Thumb instruction encoding for instruction classes
2088 // corresponding to (op1, op2, op).
2089 //
2090 // Table A6-9 32-bit Thumb instruction encoding
2091 // op1  op2             op      Instruction class, see
2092 // ---  ------- --      ------------------------------------------------------------
2093 // 01   00xx0xx -       Load/store multiple on page A6-23
2094 //              00xx1xx -       Load/store dual, load/store exclusive, table branch on page A6-24
2095 //              01xxxxx -       Data-processing (shifted register) on page A6-31
2096 //              1xxxxxx -       Coprocessor instructions on page A6-40
2097 // 10   x0xxxxx 0       Data-processing (modified immediate) on page A6-15
2098 //              x1xxxxx 0       Data-processing (plain binary immediate) on page A6-19
2099 //              -               1       Branches and miscellaneous control on page A6-20
2100 // 11   000xxx0 -       Store single data item on page A6-30
2101 //              001xxx0 -       Advanced SIMD element or structure load/store instructions on page A7-27
2102 //              00xx001 -       Load byte, memory hints on page A6-28
2103 //              00xx011 -       Load halfword, memory hints on page A6-26
2104 //              00xx101 -       Load word on page A6-25
2105 //              00xx111 -       UNDEFINED
2106 //              010xxxx -       Data-processing (register) on page A6-33
2107 //              0110xxx -       Multiply, multiply accumulate, and absolute difference on page A6-38
2108 //              0111xxx -       Long multiply, long multiply accumulate, and divide on page A6-39
2109 //              1xxxxxx -       Coprocessor instructions on page A6-40
2110 //
2111 static bool DisassembleThumb2(uint16_t op1, uint16_t op2, uint16_t op,
2112     MCInst &MI, unsigned Opcode, uint32_t insn, unsigned short NumOps,
2113     unsigned &NumOpsAdded, BO B) {
2114
2115   switch (op1) {
2116   case 1:
2117     if (slice(op2, 6, 5) == 0) {
2118       if (slice(op2, 2, 2) == 0) {
2119         // Load/store multiple.
2120         return DisassembleThumb2LdStMul(MI, Opcode, insn, NumOps, NumOpsAdded,
2121                                         B);
2122       }
2123
2124       // Load/store dual, load/store exclusive, table branch, otherwise.
2125       assert(slice(op2, 2, 2) == 1 && "Encoding error");
2126       if ((ARM::t2LDREX <= Opcode && Opcode <= ARM::t2LDREXH) ||
2127           (ARM::t2STREX <= Opcode && Opcode <= ARM::t2STREXH)) {
2128         // Load/store exclusive.
2129         return DisassembleThumb2LdStEx(MI, Opcode, insn, NumOps, NumOpsAdded,
2130                                        B);
2131       }
2132       if (Opcode == ARM::t2LDRDi8 ||
2133           Opcode == ARM::t2LDRD_PRE || Opcode == ARM::t2LDRD_POST ||
2134           Opcode == ARM::t2STRDi8 ||
2135           Opcode == ARM::t2STRD_PRE || Opcode == ARM::t2STRD_POST) {
2136         // Load/store dual.
2137         return DisassembleThumb2LdStDual(MI, Opcode, insn, NumOps, NumOpsAdded,
2138                                          B);
2139       }
2140       if (Opcode == ARM::t2TBBgen || Opcode == ARM::t2TBHgen) {
2141         // Table branch.
2142         return DisassembleThumb2TB(MI, Opcode, insn, NumOps, NumOpsAdded, B);
2143       }
2144     } else if (slice(op2, 6, 5) == 1) {
2145       // Data-processing (shifted register).
2146       return DisassembleThumb2DPSoReg(MI, Opcode, insn, NumOps, NumOpsAdded, B);
2147     }
2148
2149     // FIXME: A6.3.18 Coprocessor instructions
2150     // But see ThumbDisassembler::getInstruction().
2151
2152     break;
2153   case 2:
2154     if (op == 0) {
2155       if (slice(op2, 5, 5) == 0) {
2156         // Data-processing (modified immediate)
2157         return DisassembleThumb2DPModImm(MI, Opcode, insn, NumOps, NumOpsAdded,
2158                                          B);
2159       } else {
2160         // Data-processing (plain binary immediate)
2161         return DisassembleThumb2DPBinImm(MI, Opcode, insn, NumOps, NumOpsAdded,
2162                                          B);
2163       }
2164     } else {
2165       // Branches and miscellaneous control on page A6-20.
2166       return DisassembleThumb2BrMiscCtrl(MI, Opcode, insn, NumOps, NumOpsAdded,
2167                                          B);
2168     }
2169
2170     break;
2171   case 3:
2172     switch (slice(op2, 6, 5)) {
2173     case 0:
2174       // Load/store instructions...
2175       if (slice(op2, 0, 0) == 0) {
2176         if (slice(op2, 4, 4) == 0) {
2177           // Store single data item on page A6-30
2178           return DisassembleThumb2LdSt(false, MI,Opcode,insn,NumOps,NumOpsAdded,
2179                                        B);
2180         } else {
2181           // FIXME: Advanced SIMD element or structure load/store instructions.
2182           // But see ThumbDisassembler::getInstruction().
2183           ;
2184         }
2185       } else {
2186         // Table A6-9 32-bit Thumb instruction encoding: Load byte|halfword|word
2187         return DisassembleThumb2LdSt(true, MI,Opcode,insn,NumOps,NumOpsAdded, B);
2188       }
2189       break;
2190     case 1:
2191       if (slice(op2, 4, 4) == 0) {
2192         // A6.3.12 Data-processing (register)
2193         return DisassembleThumb2DPReg(MI, Opcode, insn, NumOps, NumOpsAdded, B);
2194       } else if (slice(op2, 3, 3) == 0) {
2195         // A6.3.16 Multiply, multiply accumulate, and absolute difference
2196         return DisassembleThumb2Mul(MI, Opcode, insn, NumOps, NumOpsAdded, B);
2197       } else {
2198         // A6.3.17 Long multiply, long multiply accumulate, and divide
2199         return DisassembleThumb2LongMul(MI, Opcode, insn, NumOps, NumOpsAdded,
2200                                         B);
2201       }
2202       break;
2203     default:
2204       // FIXME: A6.3.18 Coprocessor instructions
2205       // But see ThumbDisassembler::getInstruction().
2206       ;
2207       break;
2208     }
2209
2210     break;
2211   default:
2212     assert(0 && "Encoding error for Thumb2 instruction!");
2213     break;
2214   }
2215
2216   return false;
2217 }
2218
2219 static bool DisassembleThumbFrm(MCInst &MI, unsigned Opcode, uint32_t insn,
2220     unsigned short NumOps, unsigned &NumOpsAdded, BO Builder) {
2221
2222   uint16_t HalfWord = slice(insn, 31, 16);
2223
2224   if (HalfWord == 0) {
2225     // A6.2 16-bit Thumb instruction encoding
2226     // op = bits[15:10]
2227     uint16_t op = slice(insn, 15, 10);
2228     return DisassembleThumb1(op, MI, Opcode, insn, NumOps, NumOpsAdded,
2229                              Builder);
2230   }
2231
2232   unsigned bits15_11 = slice(HalfWord, 15, 11);
2233
2234   // A6.1 Thumb instruction set encoding
2235   if (!(bits15_11 == 0x1D || bits15_11 == 0x1E || bits15_11 == 0x1F)) {
2236     assert("Bits[15:11] first halfword of Thumb2 instruction is out of range");
2237     return false;
2238   }
2239
2240   // A6.3 32-bit Thumb instruction encoding
2241   
2242   uint16_t op1 = slice(HalfWord, 12, 11);
2243   uint16_t op2 = slice(HalfWord, 10, 4);
2244   uint16_t op = slice(insn, 15, 15);
2245
2246   return DisassembleThumb2(op1, op2, op, MI, Opcode, insn, NumOps, NumOpsAdded,
2247                            Builder);
2248 }