Better error-handling of getBitFieldInvMask() where msb < lsb (encoding error),
[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   MI.addOperand(MCOperand::CreateImm(ARMCC::AL));
717   MI.addOperand(MCOperand::CreateReg(ARM::CPSR));
718   OpIdx = 2;
719
720   // Fill the variadic part of reglist.
721   unsigned RegListBits = slice(insn, 8, 8) << (Opcode == ARM::tPUSH ? 14 : 15)
722     | slice(insn, 7, 0);
723   for (unsigned i = 0; i < 16; ++i) {
724     if ((RegListBits >> i) & 1) {
725       MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
726                                                          i)));
727       ++OpIdx;
728     }
729   }
730
731   return true;
732 }
733
734 // A6.2.5 Miscellaneous 16-bit instructions
735 // Delegate to DisassembleThumb1PushPop() for tPUSH & tPOP.
736 //
737 // tADDspi, tSUBspi: ARM::SP ARM::SP(TIED_TO) imm7
738 // t2IT:             firstcond=Inst{7-4} mask=Inst{3-0}
739 // tCBNZ, tCBZ:      tRd imm6*2
740 // tBKPT:            imm8
741 // tNOP, tSEV, tYIELD, tWFE, tWFI:
742 //   no operand (except predicate pair)
743 // tSETENDBE, tSETENDLE, :
744 //   no operand
745 // Others:           tRd tRn
746 static bool DisassembleThumb1Misc(MCInst &MI, unsigned Opcode, uint32_t insn,
747     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
748
749   if (NumOps == 0)
750     return true;
751
752   if (Opcode == ARM::tPUSH || Opcode == ARM::tPOP)
753     return DisassembleThumb1PushPop(MI, Opcode, insn, NumOps, NumOpsAdded, B);
754
755   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
756
757   // Predicate operands are handled elsewhere.
758   if (NumOps == 2 &&
759       OpInfo[0].isPredicate() && OpInfo[1].isPredicate() &&
760       OpInfo[0].RegClass == 0 && OpInfo[1].RegClass == ARM::CCRRegClassID) {
761     return true;
762   }
763
764   if (Opcode == ARM::tADDspi || Opcode == ARM::tSUBspi) {
765     // Special case handling for tADDspi and tSUBspi.
766     // A8.6.8 ADD (SP plus immediate) & A8.6.215 SUB (SP minus immediate)
767     MI.addOperand(MCOperand::CreateReg(ARM::SP));
768     MI.addOperand(MCOperand::CreateReg(ARM::SP));
769     MI.addOperand(MCOperand::CreateImm(getT1Imm7(insn)));
770     NumOpsAdded = 3;
771     return true;
772   }
773
774   if (Opcode == ARM::t2IT) {
775     // Special case handling for If-Then.
776     // A8.6.50 IT
777     // Tag the (firstcond[0] bit << 4) along with mask.
778
779     // firstcond
780     MI.addOperand(MCOperand::CreateImm(slice(insn, 7, 4)));
781
782     // firstcond[0] and mask
783     MI.addOperand(MCOperand::CreateImm(slice(insn, 4, 0)));
784     NumOpsAdded = 2;
785     return true;
786   }
787
788   if (Opcode == ARM::tBKPT) {
789     MI.addOperand(MCOperand::CreateImm(getT1Imm8(insn))); // breakpoint value
790     NumOpsAdded = 1;
791     return true;
792   }
793
794   // CPS has a singleton $opt operand that contains the following information:
795   // opt{4-0} = don't care
796   // opt{5} = 0 (false)
797   // opt{8-6} = AIF from Inst{2-0}
798   // opt{10-9} = 1:imod from Inst{4} with 0b10 as enable and 0b11 as disable
799   if (Opcode == ARM::tCPS) {
800     unsigned Option = slice(insn, 2, 0) << 6 | slice(insn, 4, 4) << 9 | 1 << 10;
801     MI.addOperand(MCOperand::CreateImm(Option));
802     NumOpsAdded = 1;
803     return true;
804   }
805
806   assert(NumOps >= 2 && OpInfo[0].RegClass == ARM::tGPRRegClassID &&
807          (OpInfo[1].RegClass==0 || OpInfo[1].RegClass==ARM::tGPRRegClassID)
808          && "Expect >=2 operands");
809
810   // Add the destination operand.
811   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
812                                                      getT1tRd(insn))));
813
814   if (OpInfo[1].RegClass == ARM::tGPRRegClassID) {
815     // Two register instructions.
816     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
817                                                        getT1tRn(insn))));
818   } else {
819     // CBNZ, CBZ
820     assert((Opcode == ARM::tCBNZ || Opcode == ARM::tCBZ) && "Invalid opcode");
821     MI.addOperand(MCOperand::CreateImm(getT1Imm6(insn) * 2));
822   }
823
824   NumOpsAdded = 2;
825
826   return true;
827 }
828
829 // A8.6.53  LDM / LDMIA
830 // A8.6.189 STM / STMIA
831 //
832 // tLDM_UPD/tSTM_UPD: tRt tRt AM4ModeImm Pred-Imm Pred-CCR register_list
833 // tLDM:              tRt AM4ModeImm Pred-Imm Pred-CCR register_list
834 static bool DisassembleThumb1LdStMul(bool Ld, MCInst &MI, unsigned Opcode,
835     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
836
837   assert((Opcode == ARM::tLDM || Opcode == ARM::tLDM_UPD ||
838           Opcode == ARM::tSTM_UPD) && "Invalid opcode");
839
840   unsigned &OpIdx = NumOpsAdded;
841
842   unsigned tRt = getT1tRt(insn);
843   unsigned RegListBits = slice(insn, 7, 0);
844
845   OpIdx = 0;
846
847   // WB register, if necessary.
848   if (Opcode == ARM::tLDM_UPD || Opcode == ARM::tSTM_UPD) {
849     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
850                                                        tRt)));
851     ++OpIdx;
852   }
853
854   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
855                                                      tRt)));
856   ++OpIdx;
857
858   // A8.6.53 LDM / LDMIA / LDMFD - Encoding T1
859   MI.addOperand(MCOperand::CreateImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)));
860   ++OpIdx;
861
862   // Handling the two predicate operands before the reglist.
863   MI.addOperand(MCOperand::CreateImm(ARMCC::AL));
864   MI.addOperand(MCOperand::CreateReg(ARM::CPSR));
865   OpIdx += 2;
866
867   // Fill the variadic part of reglist.
868   for (unsigned i = 0; i < 8; ++i) {
869     if ((RegListBits >> i) & 1) {
870       MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::tGPRRegClassID,
871                                                          i)));
872       ++OpIdx;
873     }
874   }
875
876   return true;
877 }
878
879 static bool DisassembleThumb1LdMul(MCInst &MI, unsigned Opcode, uint32_t insn,
880     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
881   return DisassembleThumb1LdStMul(true, MI, Opcode, insn, NumOps, NumOpsAdded,
882                                   B);
883 }
884
885 static bool DisassembleThumb1StMul(MCInst &MI, unsigned Opcode, uint32_t insn,
886     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
887   return DisassembleThumb1LdStMul(false, MI, Opcode, insn, NumOps, NumOpsAdded,
888                                   B);
889 }
890
891 // A8.6.16 B Encoding T1
892 // cond = Inst{11-8} & imm8 = Inst{7-0}
893 // imm32 = SignExtend(imm8:'0', 32)
894 //
895 // tBcc: offset Pred-Imm Pred-CCR
896 // tSVC: imm8 Pred-Imm Pred-CCR
897 // tTRAP: 0 operand (early return)
898 static bool DisassembleThumb1CondBr(MCInst &MI, unsigned Opcode, uint32_t insn,
899     unsigned short NumOps, unsigned &NumOpsAdded, BO) {
900
901   if (Opcode == ARM::tTRAP)
902     return true;
903
904   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
905   if (!OpInfo) return false;
906
907   assert(NumOps == 3 && OpInfo[0].RegClass == 0 &&
908          OpInfo[1].isPredicate() && OpInfo[2].RegClass == ARM::CCRRegClassID
909          && "Exactly 3 operands expected");
910
911   unsigned Imm8 = getT1Imm8(insn);
912   MI.addOperand(MCOperand::CreateImm(
913                   Opcode == ARM::tBcc ? SignExtend32<9>(Imm8 << 1) + 4
914                                       : (int)Imm8));
915
916   // Predicate operands by ARMBasicMCBuilder::TryPredicateAndSBitModifier().
917   NumOpsAdded = 1;
918
919   return true;
920 }
921
922 // A8.6.16 B Encoding T2
923 // imm11 = Inst{10-0}
924 // imm32 = SignExtend(imm11:'0', 32)
925 //
926 // tB: offset
927 static bool DisassembleThumb1Br(MCInst &MI, unsigned Opcode, uint32_t insn,
928     unsigned short NumOps, unsigned &NumOpsAdded, BO) {
929
930   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
931   if (!OpInfo) return false;
932
933   assert(NumOps == 1 && OpInfo[0].RegClass == 0 && "1 imm operand expected");
934
935   unsigned Imm11 = getT1Imm11(insn);
936
937   // When executing a Thumb instruction, PC reads as the address of the current
938   // instruction plus 4.  The assembler subtracts 4 from the difference between
939   // the branch instruction and the target address, disassembler has to add 4 to
940   // to compensate.
941   MI.addOperand(MCOperand::CreateImm(SignExtend32<12>(Imm11 << 1) + 4));
942
943   NumOpsAdded = 1;
944
945   return true;
946
947 }
948
949 // See A6.2 16-bit Thumb instruction encoding for instruction classes
950 // corresponding to op.
951 //
952 // Table A6-1 16-bit Thumb instruction encoding (abridged)
953 // op           Instruction or instruction class
954 // ------       --------------------------------------------------------------------
955 // 00xxxx       Shift (immediate), add, subtract, move, and compare on page A6-7
956 // 010000       Data-processing on page A6-8
957 // 010001       Special data instructions and branch and exchange on page A6-9
958 // 01001x       Load from Literal Pool, see LDR (literal) on page A8-122
959 // 0101xx       Load/store single data item on page A6-10
960 // 011xxx
961 // 100xxx
962 // 10100x       Generate PC-relative address, see ADR on page A8-32
963 // 10101x       Generate SP-relative address, see ADD (SP plus immediate) on page A8-28
964 // 1011xx       Miscellaneous 16-bit instructions on page A6-11
965 // 11000x       Store multiple registers, see STM / STMIA / STMEA on page A8-374
966 // 11001x       Load multiple registers, see LDM / LDMIA / LDMFD on page A8-110 a
967 // 1101xx       Conditional branch, and Supervisor Call on page A6-13
968 // 11100x       Unconditional Branch, see B on page A8-44
969 //
970 static bool DisassembleThumb1(uint16_t op, MCInst &MI, unsigned Opcode,
971     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
972
973   unsigned op1 = slice(op, 5, 4);
974   unsigned op2 = slice(op, 3, 2);
975   unsigned op3 = slice(op, 1, 0);
976   unsigned opA = slice(op, 5, 2);
977   switch (op1) {
978   case 0:
979     // A6.2.1 Shift (immediate), add, subtract, move, and compare
980     return DisassembleThumb1General(MI, Opcode, insn, NumOps, NumOpsAdded, B);
981   case 1:
982     switch (op2) {
983     case 0:
984       switch (op3) {
985       case 0:
986         // A6.2.2 Data-processing
987         return DisassembleThumb1DP(MI, Opcode, insn, NumOps, NumOpsAdded, B);
988       case 1:
989         // A6.2.3 Special data instructions and branch and exchange
990         return DisassembleThumb1Special(MI, Opcode, insn, NumOps, NumOpsAdded,
991                                         B);
992       default:
993         // A8.6.59 LDR (literal)
994         return DisassembleThumb1LdPC(MI, Opcode, insn, NumOps, NumOpsAdded, B);
995       }
996       break;
997     default:
998       // A6.2.4 Load/store single data item
999       return DisassembleThumb1LdSt(opA, MI, Opcode, insn, NumOps, NumOpsAdded,
1000                                    B);
1001       break;
1002     }
1003     break;
1004   case 2:
1005     switch (op2) {
1006     case 0:
1007       // A6.2.4 Load/store single data item
1008       return DisassembleThumb1LdSt(opA, MI, Opcode, insn, NumOps, NumOpsAdded,
1009                                    B);
1010     case 1:
1011       // A6.2.4 Load/store single data item
1012       return DisassembleThumb1LdStSP(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1013     case 2:
1014       if (op3 <= 1) {
1015         // A8.6.10 ADR
1016         return DisassembleThumb1AddPCi(MI, Opcode, insn, NumOps, NumOpsAdded,
1017                                        B);
1018       } else {
1019         // A8.6.8 ADD (SP plus immediate)
1020         return DisassembleThumb1AddSPi(MI, Opcode, insn, NumOps, NumOpsAdded,
1021                                        B);
1022       }
1023     default:
1024       // A6.2.5 Miscellaneous 16-bit instructions
1025       return DisassembleThumb1Misc(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1026     }
1027     break;
1028   case 3:
1029     switch (op2) {
1030     case 0:
1031       if (op3 <= 1) {
1032         // A8.6.189 STM / STMIA / STMEA
1033         return DisassembleThumb1StMul(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1034       } else {
1035         // A8.6.53 LDM / LDMIA / LDMFD
1036         return DisassembleThumb1LdMul(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1037       }
1038     case 1:
1039       // A6.2.6 Conditional branch, and Supervisor Call
1040       return DisassembleThumb1CondBr(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1041     case 2:
1042       // Unconditional Branch, see B on page A8-44
1043       return DisassembleThumb1Br(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1044     default:
1045       assert(0 && "Unreachable code");
1046       break;
1047     }
1048     break;
1049   default:
1050     assert(0 && "Unreachable code");
1051     break;
1052   }
1053
1054   return false;
1055 }
1056
1057 ///////////////////////////////////////////////
1058 //                                           //
1059 // Thumb2 instruction disassembly functions. //
1060 //                                           //
1061 ///////////////////////////////////////////////
1062
1063 ///////////////////////////////////////////////////////////
1064 //                                                       //
1065 // Note: the register naming follows the ARM convention! //
1066 //                                                       //
1067 ///////////////////////////////////////////////////////////
1068
1069 static inline bool Thumb2SRSOpcode(unsigned Opcode) {
1070   switch (Opcode) {
1071   default:
1072     return false;
1073   case ARM::t2SRSDBW: case ARM::t2SRSDB:
1074   case ARM::t2SRSIAW: case ARM::t2SRSIA:
1075     return true;
1076   }
1077 }
1078
1079 static inline bool Thumb2RFEOpcode(unsigned Opcode) {
1080   switch (Opcode) {
1081   default:
1082     return false;
1083   case ARM::t2RFEDBW: case ARM::t2RFEDB:
1084   case ARM::t2RFEIAW: case ARM::t2RFEIA:
1085     return true;
1086   }
1087 }
1088
1089 // t2SRS[IA|DB]W/t2SRS[IA|DB]: mode_imm = Inst{4-0}
1090 static bool DisassembleThumb2SRS(MCInst &MI, unsigned Opcode, uint32_t insn,
1091     unsigned short NumOps, unsigned &NumOpsAdded) {
1092   MI.addOperand(MCOperand::CreateImm(slice(insn, 4, 0)));
1093   NumOpsAdded = 1;
1094   return true;
1095 }
1096
1097 // t2RFE[IA|DB]W/t2RFE[IA|DB]: Rn
1098 static bool DisassembleThumb2RFE(MCInst &MI, unsigned Opcode, uint32_t insn,
1099     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1100   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1101                                                      decodeRn(insn))));
1102   NumOpsAdded = 1;
1103   return true;
1104 }
1105
1106 static bool DisassembleThumb2LdStMul(MCInst &MI, unsigned Opcode, uint32_t insn,
1107     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1108
1109   if (Thumb2SRSOpcode(Opcode))
1110     return DisassembleThumb2SRS(MI, Opcode, insn, NumOps, NumOpsAdded);
1111
1112   if (Thumb2RFEOpcode(Opcode))
1113     return DisassembleThumb2RFE(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1114
1115   assert((Opcode == ARM::t2LDM || Opcode == ARM::t2LDM_UPD ||
1116           Opcode == ARM::t2STM || Opcode == ARM::t2STM_UPD)
1117          && "Invalid opcode");
1118   assert(NumOps >= 5 && "Thumb2 LdStMul expects NumOps >= 5");
1119
1120   unsigned &OpIdx = NumOpsAdded;
1121
1122   OpIdx = 0;
1123
1124   unsigned Base = getRegisterEnum(B, ARM::GPRRegClassID, decodeRn(insn));
1125
1126   // Writeback to base.
1127   if (Opcode == ARM::t2LDM_UPD || Opcode == ARM::t2STM_UPD) {
1128     MI.addOperand(MCOperand::CreateReg(Base));
1129     ++OpIdx;
1130   }
1131
1132   MI.addOperand(MCOperand::CreateReg(Base));
1133   ++OpIdx;
1134
1135   ARM_AM::AMSubMode SubMode = getAMSubModeForBits(getPUBits(insn));
1136   MI.addOperand(MCOperand::CreateImm(ARM_AM::getAM4ModeImm(SubMode)));
1137   ++OpIdx;
1138
1139   // Handling the two predicate operands before the reglist.
1140   MI.addOperand(MCOperand::CreateImm(ARMCC::AL));
1141   MI.addOperand(MCOperand::CreateReg(ARM::CPSR));
1142   OpIdx += 2;
1143
1144   // Fill the variadic part of reglist.
1145   unsigned RegListBits = insn & ((1 << 16) - 1);
1146   for (unsigned i = 0; i < 16; ++i) {
1147     if ((RegListBits >> i) & 1) {
1148       MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1149                                                          i)));
1150       ++OpIdx;
1151     }
1152   }
1153
1154   return true;
1155 }
1156
1157 // t2LDREX: Rd Rn
1158 // t2LDREXD: Rd Rs Rn
1159 // t2LDREXB, t2LDREXH: Rd Rn
1160 // t2STREX: Rs Rd Rn
1161 // t2STREXD: Rm Rd Rs Rn
1162 // t2STREXB, t2STREXH: Rm Rd Rn
1163 static bool DisassembleThumb2LdStEx(MCInst &MI, unsigned Opcode, uint32_t insn,
1164     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1165
1166   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
1167   if (!OpInfo) return false;
1168
1169   unsigned &OpIdx = NumOpsAdded;
1170
1171   OpIdx = 0;
1172
1173   assert(NumOps >= 2
1174          && OpInfo[0].RegClass == ARM::GPRRegClassID
1175          && OpInfo[1].RegClass == ARM::GPRRegClassID
1176          && "Expect >=2 operands and first two as reg operands");
1177
1178   bool isStore = (ARM::t2STREX <= Opcode && Opcode <= ARM::t2STREXH);
1179   bool isSW = (Opcode == ARM::t2LDREX || Opcode == ARM::t2STREX);
1180   bool isDW = (Opcode == ARM::t2LDREXD || Opcode == ARM::t2STREXD);
1181
1182   // Add the destination operand for store.
1183   if (isStore) {
1184     MI.addOperand(MCOperand::CreateReg(
1185                     getRegisterEnum(B, ARM::GPRRegClassID,
1186                                     isSW ? decodeRs(insn) : decodeRm(insn))));
1187     ++OpIdx;
1188   }
1189
1190   // Source operand for store and destination operand for load.
1191   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1192                                                      decodeRd(insn))));
1193   ++OpIdx;
1194
1195   // Thumb2 doubleword complication: with an extra source/destination operand.
1196   if (isDW) {
1197     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1198                                                        decodeRs(insn))));
1199     ++OpIdx;
1200   }
1201
1202   // Finally add the pointer operand.
1203   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1204                                                      decodeRn(insn))));
1205   ++OpIdx;
1206
1207   return true;
1208 }
1209
1210 // LLVM, as of Jan-05-2010, does not output <Rt2>, i.e., Rs, in the asm.
1211 // Whereas the ARM Arch. Manual does not require that t2 = t+1 like in ARM ISA.
1212 //
1213 // t2LDRDi8: Rd Rs Rn imm8s4 (offset mode)
1214 // t2LDRDpci: Rd Rs imm8s4 (Not decoded, prefer the generic t2LDRDi8 version)
1215 // t2STRDi8: Rd Rs Rn imm8s4 (offset mode)
1216 //
1217 // Ditto for t2LDRD_PRE, t2LDRD_POST, t2STRD_PRE, t2STRD_POST, which are for
1218 // disassembly only and do not have a tied_to writeback base register operand.
1219 static bool DisassembleThumb2LdStDual(MCInst &MI, unsigned Opcode,
1220     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1221
1222   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
1223   if (!OpInfo) return false;
1224
1225   assert(NumOps >= 4
1226          && OpInfo[0].RegClass == ARM::GPRRegClassID
1227          && OpInfo[1].RegClass == ARM::GPRRegClassID
1228          && OpInfo[2].RegClass == ARM::GPRRegClassID
1229          && OpInfo[3].RegClass == 0
1230          && "Expect >= 4 operands and first 3 as reg operands");
1231
1232   // Add the <Rt> <Rt2> operands.
1233   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1234                                                      decodeRd(insn))));
1235   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1236                                                      decodeRs(insn))));
1237   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1238                                                      decodeRn(insn))));
1239
1240   // Finally add (+/-)imm8*4, depending on the U bit.
1241   int Offset = getImm8(insn) * 4;
1242   if (getUBit(insn) == 0)
1243     Offset = -Offset;
1244   MI.addOperand(MCOperand::CreateImm(Offset));
1245   NumOpsAdded = 4;
1246
1247   return true;
1248 }
1249
1250 // PC-based defined for Codegen, which do not get decoded by design:
1251 //
1252 // t2TBB, t2TBH: Rm immDontCare immDontCare
1253 //
1254 // Generic version defined for disassembly:
1255 //
1256 // t2TBBgen, t2TBHgen: Rn Rm Pred-Imm Pred-CCR
1257 static bool DisassembleThumb2TB(MCInst &MI, unsigned Opcode,
1258     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1259
1260   assert(NumOps >= 2 && "Expect >= 2 operands");
1261
1262   // The generic version of TBB/TBH needs a base register.
1263   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1264                                                      decodeRn(insn))));
1265   // Add the index register.
1266   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1267                                                      decodeRm(insn))));
1268   NumOpsAdded = 2;
1269
1270   return true;
1271 }
1272
1273 static inline bool Thumb2ShiftOpcode(unsigned Opcode) {
1274   switch (Opcode) {
1275   default:
1276     return false;
1277   case ARM::t2MOVCClsl: case ARM::t2MOVCClsr:
1278   case ARM::t2MOVCCasr: case ARM::t2MOVCCror:
1279   case ARM::t2LSLri:    case ARM::t2LSRri:
1280   case ARM::t2ASRri:    case ARM::t2RORri:
1281     return true;
1282   }
1283 }
1284
1285 // A6.3.11 Data-processing (shifted register)
1286 //
1287 // Two register operands (Rn=0b1111 no 1st operand reg): Rs Rm
1288 // Two register operands (Rs=0b1111 no dst operand reg): Rn Rm
1289 // Three register operands: Rs Rn Rm
1290 // Three register operands: (Rn=0b1111 Conditional Move) Rs Ro(TIED_TO) Rm
1291 //
1292 // Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
1293 // register with shift forms: (Rm, ConstantShiftSpecifier).
1294 // Constant shift specifier: Imm = (ShOp | ShAmt<<3).
1295 //
1296 // There are special instructions, like t2MOVsra_flag and t2MOVsrl_flag, which
1297 // only require two register operands: Rd, Rm in ARM Reference Manual terms, and
1298 // nothing else, because the shift amount is already specified.
1299 // Similar case holds for t2MOVrx, t2ADDrr, ..., etc.
1300 static bool DisassembleThumb2DPSoReg(MCInst &MI, unsigned Opcode, uint32_t insn,
1301     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1302
1303   const TargetInstrDesc &TID = ARMInsts[Opcode];
1304   const TargetOperandInfo *OpInfo = TID.OpInfo;
1305   unsigned &OpIdx = NumOpsAdded;
1306
1307   // Special case handling.
1308   if (Opcode == ARM::t2BR_JT) {
1309     assert(NumOps == 4
1310            && OpInfo[0].RegClass == ARM::GPRRegClassID
1311            && OpInfo[1].RegClass == ARM::GPRRegClassID
1312            && OpInfo[2].RegClass == 0
1313            && OpInfo[3].RegClass == 0
1314            && "Exactlt 4 operands expect and first two as reg operands");
1315     // Only need to populate the src reg operand.
1316     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1317                                                        decodeRm(insn))));
1318     MI.addOperand(MCOperand::CreateReg(0));
1319     MI.addOperand(MCOperand::CreateImm(0));
1320     MI.addOperand(MCOperand::CreateImm(0));
1321     NumOpsAdded = 4;
1322     return true;
1323   }
1324
1325   OpIdx = 0;
1326
1327   assert(NumOps >= 2
1328          && OpInfo[0].RegClass == ARM::GPRRegClassID
1329          && OpInfo[1].RegClass == ARM::GPRRegClassID
1330          && "Expect >= 2 operands and first two as reg operands");
1331
1332   bool ThreeReg = (NumOps > 2 && OpInfo[2].RegClass == ARM::GPRRegClassID);
1333   bool NoDstReg = (decodeRs(insn) == 0xF);
1334
1335   // Build the register operands, followed by the constant shift specifier.
1336
1337   MI.addOperand(MCOperand::CreateReg(
1338                   getRegisterEnum(B, ARM::GPRRegClassID,
1339                                   NoDstReg ? decodeRn(insn) : decodeRs(insn))));
1340   ++OpIdx;
1341
1342   if (ThreeReg) {
1343     int Idx;
1344     if ((Idx = TID.getOperandConstraint(OpIdx, TOI::TIED_TO)) != -1) {
1345       // Process tied_to operand constraint.
1346       MI.addOperand(MI.getOperand(Idx));
1347       ++OpIdx;
1348     } else if (!NoDstReg) {
1349       MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1350                                                          decodeRn(insn))));
1351       ++OpIdx;
1352     } else {
1353       DEBUG(errs() << "Thumb encoding error: d==15 for three-reg operands.\n");
1354       return false;
1355     }
1356   }
1357
1358   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1359                                                      decodeRm(insn))));
1360   ++OpIdx;
1361
1362   if (NumOps == OpIdx)
1363     return true;
1364
1365   if (OpInfo[OpIdx].RegClass == 0 && !OpInfo[OpIdx].isPredicate()
1366       && !OpInfo[OpIdx].isOptionalDef()) {
1367
1368     if (Thumb2ShiftOpcode(Opcode))
1369       MI.addOperand(MCOperand::CreateImm(getShiftAmtBits(insn)));
1370     else {
1371       // Build the constant shift specifier operand.
1372       unsigned bits2 = getShiftTypeBits(insn);
1373       unsigned imm5 = getShiftAmtBits(insn);
1374       ARM_AM::ShiftOpc ShOp = ARM_AM::no_shift;
1375       unsigned ShAmt = decodeImmShift(bits2, imm5, ShOp);
1376
1377       // PKHBT/PKHTB are special in that we need the decodeImmShift() call to
1378       // decode the shift amount from raw imm5 and bits2, but we DO NOT need
1379       // to encode the ShOp, as it's in the asm string already.
1380       if (Opcode == ARM::t2PKHBT || Opcode == ARM::t2PKHTB)
1381         MI.addOperand(MCOperand::CreateImm(ShAmt));
1382       else
1383         MI.addOperand(MCOperand::CreateImm(ARM_AM::getSORegOpc(ShOp, ShAmt)));
1384     }
1385     ++OpIdx;
1386   }
1387
1388   return true;
1389 }
1390
1391 // A6.3.1 Data-processing (modified immediate)
1392 //
1393 // Two register operands: Rs Rn ModImm
1394 // One register operands (Rs=0b1111 no explicit dest reg): Rn ModImm
1395 // One register operands (Rn=0b1111 no explicit src reg): Rs ModImm - {t2MOVi, t2MVNi}
1396 //
1397 // ModImm = ThumbExpandImm(i:imm3:imm8)
1398 static bool DisassembleThumb2DPModImm(MCInst &MI, unsigned Opcode,
1399     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1400
1401   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
1402   unsigned &OpIdx = NumOpsAdded;
1403
1404   OpIdx = 0;
1405
1406   assert(NumOps >= 2 && OpInfo[0].RegClass == ARM::GPRRegClassID
1407          && "Expect >= 2 operands and first one as reg operand");
1408
1409   bool TwoReg = (OpInfo[1].RegClass == ARM::GPRRegClassID);
1410   bool NoDstReg = (decodeRs(insn) == 0xF);
1411
1412   // Build the register operands, followed by the modified immediate.
1413
1414   MI.addOperand(MCOperand::CreateReg(
1415                   getRegisterEnum(B, ARM::GPRRegClassID,
1416                                   NoDstReg ? decodeRn(insn) : decodeRs(insn))));
1417   ++OpIdx;
1418
1419   if (TwoReg) {
1420     if (NoDstReg) {
1421       DEBUG(errs() << "Thumb encoding error: d==15 for DPModImm 2-reg instr.\n");
1422       return false;
1423     }
1424     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1425                                                        decodeRn(insn))));
1426     ++OpIdx;
1427   }
1428
1429   // The modified immediate operand should come next.
1430   assert(OpIdx < NumOps && OpInfo[OpIdx].RegClass == 0 &&
1431          !OpInfo[OpIdx].isPredicate() && !OpInfo[OpIdx].isOptionalDef()
1432          && "Pure imm operand expected");
1433
1434   // i:imm3:imm8
1435   // A6.3.2 Modified immediate constants in Thumb instructions
1436   unsigned imm12 = getIImm3Imm8(insn);
1437   MI.addOperand(MCOperand::CreateImm(ThumbExpandImm(imm12)));
1438   ++OpIdx;
1439
1440   return true;
1441 }
1442
1443 static inline bool Thumb2SaturateOpcode(unsigned Opcode) {
1444   switch (Opcode) {
1445   case ARM::t2SSATlsl: case ARM::t2SSATasr: case ARM::t2SSAT16:
1446   case ARM::t2USATlsl: case ARM::t2USATasr: case ARM::t2USAT16:
1447     return true;
1448   default:
1449     return false;
1450   }
1451 }
1452
1453 static inline unsigned decodeThumb2SaturatePos(unsigned Opcode, uint32_t insn) {
1454   switch (Opcode) {
1455   case ARM::t2SSATlsl:
1456   case ARM::t2SSATasr:
1457     return slice(insn, 4, 0) + 1;
1458   case ARM::t2SSAT16:
1459     return slice(insn, 3, 0) + 1;
1460   case ARM::t2USATlsl:
1461   case ARM::t2USATasr:
1462     return slice(insn, 4, 0);
1463   case ARM::t2USAT16:
1464     return slice(insn, 3, 0);
1465   default:
1466     assert(0 && "Invalid opcode passed in");
1467     return 0;
1468   }
1469 }
1470
1471 // A6.3.3 Data-processing (plain binary immediate)
1472 //
1473 // o t2ADDri12, t2SUBri12: Rs Rn imm12
1474 // o t2LEApcrel (ADR): Rs imm12
1475 // o t2BFC (BFC): Rs Ro(TIED_TO) bf_inv_mask_imm
1476 // o t2BFI (BFI) (Currently not defined in LLVM as of Jan-07-2010)
1477 // o t2MOVi16: Rs imm16
1478 // o t2MOVTi16: Rs imm16
1479 // o t2SBFX (SBFX): Rs Rn lsb width
1480 // o t2UBFX (UBFX): Rs Rn lsb width
1481 // o t2BFI (BFI): Rs Rn lsb width
1482 //
1483 // [Signed|Unsigned] Saturate [16]
1484 //
1485 // o t2SSAT[lsl|asr], t2USAT[lsl|asr]: Rs sat_pos Rn shamt
1486 // o t2SSAT16, t2USAT16: Rs sat_pos Rn
1487 static bool DisassembleThumb2DPBinImm(MCInst &MI, unsigned Opcode,
1488     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1489
1490   const TargetInstrDesc &TID = ARMInsts[Opcode];
1491   const TargetOperandInfo *OpInfo = TID.OpInfo;
1492   unsigned &OpIdx = NumOpsAdded;
1493
1494   OpIdx = 0;
1495
1496   assert(NumOps >= 2 && OpInfo[0].RegClass == ARM::GPRRegClassID
1497          && "Expect >= 2 operands and first one as reg operand");
1498
1499   bool TwoReg = (OpInfo[1].RegClass == ARM::GPRRegClassID);
1500
1501   // Build the register operand(s), followed by the immediate(s).
1502
1503   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1504                                                      decodeRs(insn))));
1505   ++OpIdx;
1506
1507   // t2SSAT/t2SSAT16/t2USAT/t2USAT16 has imm operand after Rd.
1508   if (Thumb2SaturateOpcode(Opcode)) {
1509     MI.addOperand(MCOperand::CreateImm(decodeThumb2SaturatePos(Opcode, insn)));
1510
1511     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1512                                                        decodeRn(insn))));
1513
1514     if (Opcode == ARM::t2SSAT16 || Opcode == ARM::t2USAT16) {
1515       OpIdx += 2;
1516       return true;
1517     }
1518
1519     // For SSAT operand reg (Rn) has been disassembled above.
1520     // Now disassemble the shift amount.
1521
1522     // Inst{14-12:7-6} encodes the imm5 shift amount.
1523     unsigned ShAmt = slice(insn, 14, 12) << 2 | slice(insn, 7, 6);
1524
1525     MI.addOperand(MCOperand::CreateImm(ShAmt));
1526
1527     OpIdx += 3;
1528     return true;
1529   }
1530
1531   if (TwoReg) {
1532     assert(NumOps >= 3 && "Expect >= 3 operands");
1533     int Idx;
1534     if ((Idx = TID.getOperandConstraint(OpIdx, TOI::TIED_TO)) != -1) {
1535       // Process tied_to operand constraint.
1536       MI.addOperand(MI.getOperand(Idx));
1537     } else {
1538       // Add src reg operand.
1539       MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1540                                                          decodeRn(insn))));
1541     }
1542     ++OpIdx;
1543   }
1544
1545   assert(OpInfo[OpIdx].RegClass == 0 && !OpInfo[OpIdx].isPredicate()
1546          && !OpInfo[OpIdx].isOptionalDef()
1547          && "Pure imm operand expected");
1548
1549   // Pre-increment OpIdx.
1550   ++OpIdx;
1551
1552   if (Opcode == ARM::t2ADDri12 || Opcode == ARM::t2SUBri12
1553       || Opcode == ARM::t2LEApcrel)
1554     MI.addOperand(MCOperand::CreateImm(getIImm3Imm8(insn)));
1555   else if (Opcode == ARM::t2MOVi16 || Opcode == ARM::t2MOVTi16)
1556     MI.addOperand(MCOperand::CreateImm(getImm16(insn)));
1557   else if (Opcode == ARM::t2BFC) {
1558     uint32_t mask = 0;
1559     if (getBitfieldInvMask(insn, mask))
1560       MI.addOperand(MCOperand::CreateImm(mask));
1561     else
1562       return false;
1563   } else {
1564     // Handle the case of: lsb width
1565     assert((Opcode == ARM::t2SBFX || Opcode == ARM::t2UBFX ||
1566             Opcode == ARM::t2BFI) && "Invalid opcode");
1567     MI.addOperand(MCOperand::CreateImm(getLsb(insn)));
1568     if (Opcode == ARM::t2BFI) {
1569       if (getMsb(insn) < getLsb(insn)) {
1570         DEBUG(errs() << "Encoding error: msb < lsb\n");
1571         return false;
1572       }
1573       MI.addOperand(MCOperand::CreateImm(getMsb(insn) - getLsb(insn) + 1));
1574     } else
1575       MI.addOperand(MCOperand::CreateImm(getWidthMinus1(insn) + 1));
1576
1577     ++OpIdx;
1578   }
1579
1580   return true;
1581 }
1582
1583 // A6.3.4 Table A6-15 Miscellaneous control instructions
1584 // A8.6.41 DMB
1585 // A8.6.42 DSB
1586 // A8.6.49 ISB
1587 static inline bool t2MiscCtrlInstr(uint32_t insn) {
1588   if (slice(insn, 31, 20) == 0xf3b && slice(insn, 15, 14) == 2 &&
1589       slice(insn, 12, 12) == 0)
1590     return true;
1591
1592   return false;
1593 }
1594
1595 // A6.3.4 Branches and miscellaneous control
1596 //
1597 // A8.6.16 B
1598 // Branches: t2B, t2Bcc -> imm operand
1599 //
1600 // Branches: t2TPsoft -> no operand
1601 //
1602 // A8.6.23 BL, BLX (immediate)
1603 // Branches (defined in ARMInstrThumb.td): tBLr9, tBLXi_r9 -> imm operand
1604 //
1605 // A8.6.26
1606 // t2BXJ -> Rn
1607 //
1608 // Miscellaneous control: t2Int_MemBarrierV7 (and its t2DMB variants),
1609 // t2Int_SyncBarrierV7 (and its t2DSB varianst), t2ISBsy, t2CLREX
1610 //   -> no operand (except pred-imm pred-ccr for CLREX, memory barrier variants)
1611 //
1612 // Hint: t2NOP, t2YIELD, t2WFE, t2WFI, t2SEV
1613 //   -> no operand (except pred-imm pred-ccr)
1614 //
1615 // t2DBG -> imm4 = Inst{3-0}
1616 //
1617 // t2MRS/t2MRSsys -> Rs
1618 // t2MSR/t2MSRsys -> Rn mask=Inst{11-8}
1619 // t2SMC -> imm4 = Inst{19-16}
1620 static bool DisassembleThumb2BrMiscCtrl(MCInst &MI, unsigned Opcode,
1621     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1622
1623   if (NumOps == 0)
1624     return true;
1625
1626   if (t2MiscCtrlInstr(insn))
1627     return true;
1628
1629   switch (Opcode) {
1630   case ARM::t2CLREX:
1631   case ARM::t2NOP:
1632   case ARM::t2YIELD:
1633   case ARM::t2WFE:
1634   case ARM::t2WFI:
1635   case ARM::t2SEV:
1636     return true;
1637   default:
1638     break;
1639   }
1640
1641   // CPS has a singleton $opt operand that contains the following information:
1642   // opt{4-0} = mode from Inst{4-0}
1643   // opt{5} = changemode from Inst{8}
1644   // opt{8-6} = AIF from Inst{7-5}
1645   // opt{10-9} = imod from Inst{10-9} with 0b10 as enable and 0b11 as disable
1646   if (Opcode == ARM::t2CPS) {
1647     unsigned Option = slice(insn, 4, 0) | slice(insn, 8, 8) << 5 |
1648       slice(insn, 7, 5) << 6 | slice(insn, 10, 9) << 9;
1649     MI.addOperand(MCOperand::CreateImm(Option));
1650     NumOpsAdded = 1;
1651     return true;
1652   }
1653
1654   // DBG has its option specified in Inst{3-0}.
1655   if (Opcode == ARM::t2DBG) {
1656     MI.addOperand(MCOperand::CreateImm(slice(insn, 3, 0)));
1657     NumOpsAdded = 1;
1658     return true;
1659   }
1660
1661   // MRS and MRSsys take one GPR reg Rs.
1662   if (Opcode == ARM::t2MRS || Opcode == ARM::t2MRSsys) {
1663     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1664                                                        decodeRs(insn))));
1665     NumOpsAdded = 1;
1666     return true;
1667   }
1668   // BXJ takes one GPR reg Rn.
1669   if (Opcode == ARM::t2BXJ) {
1670     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1671                                                        decodeRn(insn))));
1672     NumOpsAdded = 1;
1673     return true;
1674   }
1675   // MSR and MSRsys take one GPR reg Rn, followed by the mask.
1676   if (Opcode == ARM::t2MSR || Opcode == ARM::t2MSRsys || Opcode == ARM::t2BXJ) {
1677     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1678                                                        decodeRn(insn))));
1679     MI.addOperand(MCOperand::CreateImm(slice(insn, 11, 8)));
1680     NumOpsAdded = 2;
1681     return true;
1682   }
1683   // SMC take imm4.
1684   if (Opcode == ARM::t2SMC) {
1685     MI.addOperand(MCOperand::CreateImm(slice(insn, 19, 16)));
1686     NumOpsAdded = 1;
1687     return true;
1688   }
1689
1690   // Add the imm operand.
1691   int Offset = 0;
1692
1693   switch (Opcode) {
1694   default:
1695     assert(0 && "Unreachable code");
1696     return false;
1697   case ARM::t2B:
1698     Offset = decodeImm32_B_EncodingT4(insn);
1699     break;
1700   case ARM::t2Bcc:
1701     Offset = decodeImm32_B_EncodingT3(insn);
1702     break;
1703   case ARM::tBLr9:
1704     Offset = decodeImm32_BL(insn);
1705     break;
1706   case ARM::tBLXi_r9:
1707     Offset = decodeImm32_BLX(insn);
1708     break;
1709   }
1710   // When executing a Thumb instruction, PC reads as the address of the current
1711   // instruction plus 4.  The assembler subtracts 4 from the difference between
1712   // the branch instruction and the target address, disassembler has to add 4 to
1713   // to compensate.
1714   MI.addOperand(MCOperand::CreateImm(Offset + 4));
1715
1716   NumOpsAdded = 1;
1717
1718   return true;
1719 }
1720
1721 static inline bool Thumb2PreloadOpcode(unsigned Opcode) {
1722   switch (Opcode) {
1723   default:
1724     return false;
1725   case ARM::t2PLDi12:   case ARM::t2PLDi8:   case ARM::t2PLDpci:
1726   case ARM::t2PLDr:     case ARM::t2PLDs:
1727   case ARM::t2PLDWi12:  case ARM::t2PLDWi8:  case ARM::t2PLDWpci:
1728   case ARM::t2PLDWr:    case ARM::t2PLDWs:
1729   case ARM::t2PLIi12:   case ARM::t2PLIi8:   case ARM::t2PLIpci:
1730   case ARM::t2PLIr:     case ARM::t2PLIs:
1731     return true;
1732   }
1733 }
1734
1735 static bool DisassembleThumb2PreLoad(MCInst &MI, unsigned Opcode, uint32_t insn,
1736     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1737
1738   // Preload Data/Instruction requires either 2 or 3 operands.
1739   // t2PLDi12, t2PLDi8, t2PLDpci: Rn [+/-]imm12/imm8
1740   // t2PLDr:                      Rn Rm
1741   // t2PLDs:                      Rn Rm imm2=Inst{5-4}
1742   // Same pattern applies for t2PLDW* and t2PLI*.
1743
1744   const TargetInstrDesc &TID = ARMInsts[Opcode];
1745   const TargetOperandInfo *OpInfo = TID.OpInfo;
1746   unsigned &OpIdx = NumOpsAdded;
1747
1748   OpIdx = 0;
1749
1750   assert(NumOps >= 2 &&
1751          OpInfo[0].RegClass == ARM::GPRRegClassID &&
1752          "Expect >= 2 operands and first one as reg operand");
1753
1754   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1755                                                      decodeRn(insn))));
1756   ++OpIdx;
1757
1758   if (OpInfo[OpIdx].RegClass == ARM::GPRRegClassID) {
1759     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1760                                                        decodeRm(insn))));
1761   } else {
1762     assert(OpInfo[OpIdx].RegClass == 0 && !OpInfo[OpIdx].isPredicate()
1763            && !OpInfo[OpIdx].isOptionalDef()
1764            && "Pure imm operand expected");
1765     int Offset = 0;
1766     if (Opcode == ARM::t2PLDpci || Opcode == ARM::t2PLDWpci ||
1767              Opcode == ARM::t2PLIpci) {
1768       bool Negative = slice(insn, 23, 23) == 0;
1769       unsigned Imm12 = getImm12(insn);
1770       Offset = Negative ? -1 - Imm12 : 1 * Imm12;      
1771     } else if (Opcode == ARM::t2PLDi8 || Opcode == ARM::t2PLDWi8 ||
1772                Opcode == ARM::t2PLIi8) {
1773       // A8.6.117 Encoding T2: add = FALSE
1774       unsigned Imm8 = getImm8(insn);
1775       Offset = -1 - Imm8;
1776     } else // The i12 forms.  See, for example, A8.6.117 Encoding T1.
1777       Offset = decodeImm12(insn);
1778     MI.addOperand(MCOperand::CreateImm(Offset));
1779   }
1780   ++OpIdx;
1781
1782   if (OpIdx < NumOps && OpInfo[OpIdx].RegClass == 0 &&
1783       !OpInfo[OpIdx].isPredicate() && !OpInfo[OpIdx].isOptionalDef()) {
1784     // Fills in the shift amount for t2PLDs, t2PLDWs, t2PLIs.
1785     MI.addOperand(MCOperand::CreateImm(slice(insn, 5, 4)));
1786     ++OpIdx;
1787   }
1788
1789   return true;
1790 }
1791
1792 // A8.6.63 LDRB (literal)
1793 // A8.6.79 LDRSB (literal)
1794 // A8.6.75 LDRH (literal)
1795 // A8.6.83 LDRSH (literal)
1796 // A8.6.59 LDR (literal)
1797 //
1798 // These instrs calculate an address from the PC value and an immediate offset.
1799 // Rd Rn=PC (+/-)imm12 (+ if Inst{23} == 0b1)
1800 static bool DisassembleThumb2Ldpci(MCInst &MI, unsigned Opcode,
1801     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1802
1803   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
1804   if (!OpInfo) return false;
1805
1806   assert(NumOps >= 2 &&
1807          OpInfo[0].RegClass == ARM::GPRRegClassID &&
1808          OpInfo[1].RegClass == 0 &&
1809          "Expect >= 2 operands, first as reg, and second as imm operand");
1810
1811   // Build the register operand, followed by the (+/-)imm12 immediate.
1812
1813   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1814                                                      decodeRd(insn))));
1815
1816   MI.addOperand(MCOperand::CreateImm(decodeImm12(insn)));
1817
1818   NumOpsAdded = 2;
1819
1820   return true;
1821 }
1822
1823 // A6.3.10 Store single data item
1824 // A6.3.9 Load byte, memory hints
1825 // A6.3.8 Load halfword, memory hints
1826 // A6.3.7 Load word
1827 //
1828 // For example,
1829 //
1830 // t2LDRi12:   Rd Rn (+)imm12
1831 // t2LDRi8:    Rd Rn (+/-)imm8 (+ if Inst{9} == 0b1)
1832 // t2LDRs:     Rd Rn Rm ConstantShiftSpecifier (see also DisassembleThumb2DPSoReg)
1833 // t2LDR_POST: Rd Rn Rn(TIED_TO) (+/-)imm8 (+ if Inst{9} == 0b1)
1834 // t2LDR_PRE:  Rd Rn Rn(TIED_TO) (+/-)imm8 (+ if Inst{9} == 0b1)
1835 //
1836 // t2STRi12:   Rd Rn (+)imm12
1837 // t2STRi8:    Rd Rn (+/-)imm8 (+ if Inst{9} == 0b1)
1838 // t2STRs:     Rd Rn Rm ConstantShiftSpecifier (see also DisassembleThumb2DPSoReg)
1839 // t2STR_POST: Rn Rd Rn(TIED_TO) (+/-)imm8 (+ if Inst{9} == 0b1)
1840 // t2STR_PRE:  Rn Rd Rn(TIED_TO) (+/-)imm8 (+ if Inst{9} == 0b1)
1841 //
1842 // Note that for indexed modes, the Rn(TIED_TO) operand needs to be populated
1843 // correctly, as LLVM AsmPrinter depends on it.  For indexed stores, the first
1844 // operand is Rn; for all the other instructions, Rd is the first operand.
1845 //
1846 // Delegates to DisassembleThumb2PreLoad() for preload data/instruction.
1847 // Delegates to DisassembleThumb2Ldpci() for load * literal operations.
1848 static bool DisassembleThumb2LdSt(bool Load, MCInst &MI, unsigned Opcode,
1849     uint32_t insn, unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1850
1851   unsigned Rn = decodeRn(insn);
1852
1853   if (Thumb2PreloadOpcode(Opcode))
1854     return DisassembleThumb2PreLoad(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1855
1856   // See, for example, A6.3.7 Load word: Table A6-18 Load word.
1857   if (Load && Rn == 15)
1858     return DisassembleThumb2Ldpci(MI, Opcode, insn, NumOps, NumOpsAdded, B);
1859
1860   const TargetInstrDesc &TID = ARMInsts[Opcode];
1861   const TargetOperandInfo *OpInfo = TID.OpInfo;
1862   unsigned &OpIdx = NumOpsAdded;
1863
1864   OpIdx = 0;
1865
1866   assert(NumOps >= 3 &&
1867          OpInfo[0].RegClass == ARM::GPRRegClassID &&
1868          OpInfo[1].RegClass == ARM::GPRRegClassID &&
1869          "Expect >= 3 operands and first two as reg operands");
1870
1871   bool ThreeReg = (OpInfo[2].RegClass == ARM::GPRRegClassID);
1872   bool TIED_TO = ThreeReg && TID.getOperandConstraint(2, TOI::TIED_TO) != -1;
1873   bool Imm12 = !ThreeReg && slice(insn, 23, 23) == 1; // ARMInstrThumb2.td
1874
1875   // Build the register operands, followed by the immediate.
1876   unsigned R0, R1, R2 = 0;
1877   unsigned Rd = decodeRd(insn);
1878   int Imm = 0;
1879
1880   if (!Load && TIED_TO) {
1881     R0 = Rn;
1882     R1 = Rd;
1883   } else {
1884     R0 = Rd;
1885     R1 = Rn;
1886   }
1887   if (ThreeReg) {
1888     if (TIED_TO) {
1889       R2 = Rn;
1890       Imm = decodeImm8(insn);
1891     } else {
1892       R2 = decodeRm(insn);
1893       // See, for example, A8.6.64 LDRB (register).
1894       // And ARMAsmPrinter::printT2AddrModeSoRegOperand().
1895       // LSL is the default shift opc, and LLVM does not expect it to be encoded
1896       // as part of the immediate operand.
1897       // Imm = ARM_AM::getSORegOpc(ARM_AM::lsl, slice(insn, 5, 4));
1898       Imm = slice(insn, 5, 4);
1899     }
1900   } else {
1901     if (Imm12)
1902       Imm = getImm12(insn);
1903     else
1904       Imm = decodeImm8(insn);
1905   }
1906   
1907   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1908                                                      R0)));
1909   ++OpIdx;
1910   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1911                                                      R1)));
1912   ++OpIdx;
1913
1914   if (ThreeReg) {
1915     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1916                                                        R2)));
1917     ++OpIdx;
1918   }
1919
1920   assert(OpInfo[OpIdx].RegClass == 0 && !OpInfo[OpIdx].isPredicate()
1921          && !OpInfo[OpIdx].isOptionalDef()
1922          && "Pure imm operand expected");
1923
1924   MI.addOperand(MCOperand::CreateImm(Imm));
1925   ++OpIdx;
1926
1927   return true;
1928 }
1929
1930 // A6.3.12 Data-processing (register)
1931 //
1932 // Two register operands [rotate]:   Rs Rm [rotation(= (rotate:'000'))]
1933 // Three register operands only:     Rs Rn Rm
1934 // Three register operands [rotate]: Rs Rn Rm [rotation(= (rotate:'000'))]
1935 //
1936 // Parallel addition and subtraction 32-bit Thumb instructions: Rs Rn Rm
1937 //
1938 // Miscellaneous operations: Rs [Rn] Rm
1939 static bool DisassembleThumb2DPReg(MCInst &MI, unsigned Opcode, uint32_t insn,
1940     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1941
1942   const TargetInstrDesc &TID = ARMInsts[Opcode];
1943   const TargetOperandInfo *OpInfo = TID.OpInfo;
1944   unsigned &OpIdx = NumOpsAdded;
1945
1946   OpIdx = 0;
1947
1948   assert(NumOps >= 2 &&
1949          OpInfo[0].RegClass == ARM::GPRRegClassID &&
1950          OpInfo[1].RegClass == ARM::GPRRegClassID &&
1951          "Expect >= 2 operands and first two as reg operands");
1952
1953   // Build the register operands, followed by the optional rotation amount.
1954
1955   bool ThreeReg = NumOps > 2 && OpInfo[2].RegClass == ARM::GPRRegClassID;
1956
1957   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1958                                                      decodeRs(insn))));
1959   ++OpIdx;
1960
1961   if (ThreeReg) {
1962     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1963                                                        decodeRn(insn))));
1964     ++OpIdx;
1965   }
1966
1967   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
1968                                                      decodeRm(insn))));
1969   ++OpIdx;
1970
1971   if (OpIdx < NumOps && OpInfo[OpIdx].RegClass == 0
1972       && !OpInfo[OpIdx].isPredicate() && !OpInfo[OpIdx].isOptionalDef()) {
1973     // Add the rotation amount immediate.
1974     MI.addOperand(MCOperand::CreateImm(decodeRotate(insn)));
1975     ++OpIdx;
1976   }
1977
1978   return true;
1979 }
1980
1981 // A6.3.16 Multiply, multiply accumulate, and absolute difference
1982 //
1983 // t2MLA, t2MLS, t2SMMLA, t2SMMLS: Rs Rn Rm Ra=Inst{15-12}
1984 // t2MUL, t2SMMUL:                 Rs Rn Rm
1985 // t2SMLA[BB|BT|TB|TT|WB|WT]:      Rs Rn Rm Ra=Inst{15-12}
1986 // t2SMUL[BB|BT|TB|TT|WB|WT]:      Rs Rn Rm
1987 //
1988 // Dual halfword multiply: t2SMUAD[X], t2SMUSD[X], t2SMLAD[X], t2SMLSD[X]:
1989 //   Rs Rn Rm Ra=Inst{15-12}
1990 //
1991 // Unsigned Sum of Absolute Differences [and Accumulate]
1992 //    Rs Rn Rm [Ra=Inst{15-12}]
1993 static bool DisassembleThumb2Mul(MCInst &MI, unsigned Opcode, uint32_t insn,
1994     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
1995
1996   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
1997
1998   assert(NumOps >= 3 &&
1999          OpInfo[0].RegClass == ARM::GPRRegClassID &&
2000          OpInfo[1].RegClass == ARM::GPRRegClassID &&
2001          OpInfo[2].RegClass == ARM::GPRRegClassID &&
2002          "Expect >= 3 operands and first three as reg operands");
2003
2004   // Build the register operands.
2005
2006   bool FourReg = NumOps > 3 && OpInfo[3].RegClass == ARM::GPRRegClassID;
2007
2008   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2009                                                      decodeRs(insn))));
2010
2011   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2012                                                      decodeRn(insn))));
2013
2014   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2015                                                      decodeRm(insn))));
2016
2017   if (FourReg)
2018     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2019                                                        decodeRd(insn))));
2020
2021   NumOpsAdded = FourReg ? 4 : 3;
2022
2023   return true;
2024 }
2025
2026 // A6.3.17 Long multiply, long multiply accumulate, and divide
2027 //
2028 // t2SMULL, t2UMULL, t2SMLAL, t2UMLAL, t2UMAAL: RdLo RdHi Rn Rm
2029 // where RdLo = Inst{15-12} and RdHi = Inst{11-8}
2030 //
2031 // Halfword multiple accumulate long: t2SMLAL<x><y>: RdLo RdHi Rn Rm
2032 // where RdLo = Inst{15-12} and RdHi = Inst{11-8}
2033 //
2034 // Dual halfword multiple: t2SMLALD[X], t2SMLSLD[X]: RdLo RdHi Rn Rm
2035 // where RdLo = Inst{15-12} and RdHi = Inst{11-8}
2036 //
2037 // Signed/Unsigned divide: t2SDIV, t2UDIV: Rs Rn Rm
2038 static bool DisassembleThumb2LongMul(MCInst &MI, unsigned Opcode, uint32_t insn,
2039     unsigned short NumOps, unsigned &NumOpsAdded, BO B) {
2040
2041   const TargetOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
2042
2043   assert(NumOps >= 3 &&
2044          OpInfo[0].RegClass == ARM::GPRRegClassID &&
2045          OpInfo[1].RegClass == ARM::GPRRegClassID &&
2046          OpInfo[2].RegClass == ARM::GPRRegClassID &&
2047          "Expect >= 3 operands and first three as reg operands");
2048
2049   bool FourReg = NumOps > 3 && OpInfo[3].RegClass == ARM::GPRRegClassID;
2050
2051   // Build the register operands.
2052
2053   if (FourReg)
2054     MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2055                                                        decodeRd(insn))));
2056
2057   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2058                                                      decodeRs(insn))));
2059
2060   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2061                                                      decodeRn(insn))));
2062
2063   MI.addOperand(MCOperand::CreateReg(getRegisterEnum(B, ARM::GPRRegClassID,
2064                                                      decodeRm(insn))));
2065
2066   if (FourReg)
2067     NumOpsAdded = 4;
2068   else
2069     NumOpsAdded = 3;
2070
2071   return true;
2072 }
2073
2074 // See A6.3 32-bit Thumb instruction encoding for instruction classes
2075 // corresponding to (op1, op2, op).
2076 //
2077 // Table A6-9 32-bit Thumb instruction encoding
2078 // op1  op2             op      Instruction class, see
2079 // ---  ------- --      ------------------------------------------------------------
2080 // 01   00xx0xx -       Load/store multiple on page A6-23
2081 //              00xx1xx -       Load/store dual, load/store exclusive, table branch on page A6-24
2082 //              01xxxxx -       Data-processing (shifted register) on page A6-31
2083 //              1xxxxxx -       Coprocessor instructions on page A6-40
2084 // 10   x0xxxxx 0       Data-processing (modified immediate) on page A6-15
2085 //              x1xxxxx 0       Data-processing (plain binary immediate) on page A6-19
2086 //              -               1       Branches and miscellaneous control on page A6-20
2087 // 11   000xxx0 -       Store single data item on page A6-30
2088 //              001xxx0 -       Advanced SIMD element or structure load/store instructions on page A7-27
2089 //              00xx001 -       Load byte, memory hints on page A6-28
2090 //              00xx011 -       Load halfword, memory hints on page A6-26
2091 //              00xx101 -       Load word on page A6-25
2092 //              00xx111 -       UNDEFINED
2093 //              010xxxx -       Data-processing (register) on page A6-33
2094 //              0110xxx -       Multiply, multiply accumulate, and absolute difference on page A6-38
2095 //              0111xxx -       Long multiply, long multiply accumulate, and divide on page A6-39
2096 //              1xxxxxx -       Coprocessor instructions on page A6-40
2097 //
2098 static bool DisassembleThumb2(uint16_t op1, uint16_t op2, uint16_t op,
2099     MCInst &MI, unsigned Opcode, uint32_t insn, unsigned short NumOps,
2100     unsigned &NumOpsAdded, BO B) {
2101
2102   switch (op1) {
2103   case 1:
2104     if (slice(op2, 6, 5) == 0) {
2105       if (slice(op2, 2, 2) == 0) {
2106         // Load/store multiple.
2107         return DisassembleThumb2LdStMul(MI, Opcode, insn, NumOps, NumOpsAdded,
2108                                         B);
2109       }
2110
2111       // Load/store dual, load/store exclusive, table branch, otherwise.
2112       assert(slice(op2, 2, 2) == 1 && "Encoding error");
2113       if ((ARM::t2LDREX <= Opcode && Opcode <= ARM::t2LDREXH) ||
2114           (ARM::t2STREX <= Opcode && Opcode <= ARM::t2STREXH)) {
2115         // Load/store exclusive.
2116         return DisassembleThumb2LdStEx(MI, Opcode, insn, NumOps, NumOpsAdded,
2117                                        B);
2118       }
2119       if (Opcode == ARM::t2LDRDi8 ||
2120           Opcode == ARM::t2LDRD_PRE || Opcode == ARM::t2LDRD_POST ||
2121           Opcode == ARM::t2STRDi8 ||
2122           Opcode == ARM::t2STRD_PRE || Opcode == ARM::t2STRD_POST) {
2123         // Load/store dual.
2124         return DisassembleThumb2LdStDual(MI, Opcode, insn, NumOps, NumOpsAdded,
2125                                          B);
2126       }
2127       if (Opcode == ARM::t2TBBgen || Opcode == ARM::t2TBHgen) {
2128         // Table branch.
2129         return DisassembleThumb2TB(MI, Opcode, insn, NumOps, NumOpsAdded, B);
2130       }
2131     } else if (slice(op2, 6, 5) == 1) {
2132       // Data-processing (shifted register).
2133       return DisassembleThumb2DPSoReg(MI, Opcode, insn, NumOps, NumOpsAdded, B);
2134     }
2135
2136     // FIXME: A6.3.18 Coprocessor instructions
2137     // But see ThumbDisassembler::getInstruction().
2138
2139     break;
2140   case 2:
2141     if (op == 0) {
2142       if (slice(op2, 5, 5) == 0) {
2143         // Data-processing (modified immediate)
2144         return DisassembleThumb2DPModImm(MI, Opcode, insn, NumOps, NumOpsAdded,
2145                                          B);
2146       } else {
2147         // Data-processing (plain binary immediate)
2148         return DisassembleThumb2DPBinImm(MI, Opcode, insn, NumOps, NumOpsAdded,
2149                                          B);
2150       }
2151     } else {
2152       // Branches and miscellaneous control on page A6-20.
2153       return DisassembleThumb2BrMiscCtrl(MI, Opcode, insn, NumOps, NumOpsAdded,
2154                                          B);
2155     }
2156
2157     break;
2158   case 3:
2159     switch (slice(op2, 6, 5)) {
2160     case 0:
2161       // Load/store instructions...
2162       if (slice(op2, 0, 0) == 0) {
2163         if (slice(op2, 4, 4) == 0) {
2164           // Store single data item on page A6-30
2165           return DisassembleThumb2LdSt(false, MI,Opcode,insn,NumOps,NumOpsAdded,
2166                                        B);
2167         } else {
2168           // FIXME: Advanced SIMD element or structure load/store instructions.
2169           // But see ThumbDisassembler::getInstruction().
2170           ;
2171         }
2172       } else {
2173         // Table A6-9 32-bit Thumb instruction encoding: Load byte|halfword|word
2174         return DisassembleThumb2LdSt(true, MI,Opcode,insn,NumOps,NumOpsAdded, B);
2175       }
2176       break;
2177     case 1:
2178       if (slice(op2, 4, 4) == 0) {
2179         // A6.3.12 Data-processing (register)
2180         return DisassembleThumb2DPReg(MI, Opcode, insn, NumOps, NumOpsAdded, B);
2181       } else if (slice(op2, 3, 3) == 0) {
2182         // A6.3.16 Multiply, multiply accumulate, and absolute difference
2183         return DisassembleThumb2Mul(MI, Opcode, insn, NumOps, NumOpsAdded, B);
2184       } else {
2185         // A6.3.17 Long multiply, long multiply accumulate, and divide
2186         return DisassembleThumb2LongMul(MI, Opcode, insn, NumOps, NumOpsAdded,
2187                                         B);
2188       }
2189       break;
2190     default:
2191       // FIXME: A6.3.18 Coprocessor instructions
2192       // But see ThumbDisassembler::getInstruction().
2193       ;
2194       break;
2195     }
2196
2197     break;
2198   default:
2199     assert(0 && "Encoding error for Thumb2 instruction!");
2200     break;
2201   }
2202
2203   return false;
2204 }
2205
2206 static bool DisassembleThumbFrm(MCInst &MI, unsigned Opcode, uint32_t insn,
2207     unsigned short NumOps, unsigned &NumOpsAdded, BO Builder) {
2208
2209   uint16_t HalfWord = slice(insn, 31, 16);
2210
2211   if (HalfWord == 0) {
2212     // A6.2 16-bit Thumb instruction encoding
2213     // op = bits[15:10]
2214     uint16_t op = slice(insn, 15, 10);
2215     return DisassembleThumb1(op, MI, Opcode, insn, NumOps, NumOpsAdded,
2216                              Builder);
2217   }
2218
2219   unsigned bits15_11 = slice(HalfWord, 15, 11);
2220
2221   // A6.1 Thumb instruction set encoding
2222   if (!(bits15_11 == 0x1D || bits15_11 == 0x1E || bits15_11 == 0x1F)) {
2223     assert("Bits[15:11] first halfword of Thumb2 instruction is out of range");
2224     return false;
2225   }
2226
2227   // A6.3 32-bit Thumb instruction encoding
2228   
2229   uint16_t op1 = slice(HalfWord, 12, 11);
2230   uint16_t op2 = slice(HalfWord, 10, 4);
2231   uint16_t op = slice(insn, 15, 15);
2232
2233   return DisassembleThumb2(op1, op2, op, MI, Opcode, insn, NumOps, NumOpsAdded,
2234                            Builder);
2235 }