Fix encoding and add parsing support for the arm/thumb CPS instruction:
[oota-llvm.git] / lib / Target / ARM / InstPrinter / ARMInstPrinter.cpp
1 //===-- ARMInstPrinter.cpp - Convert ARM MCInst to assembly syntax --------===//
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 class prints an ARM MCInst to a .s file.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "asm-printer"
15 #include "ARMBaseInfo.h"
16 #include "ARMInstPrinter.h"
17 #include "ARMAddressingModes.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/Support/raw_ostream.h"
23 using namespace llvm;
24
25 #define GET_INSTRUCTION_NAME
26 #include "ARMGenAsmWriter.inc"
27
28 StringRef ARMInstPrinter::getOpcodeName(unsigned Opcode) const {
29   return getInstructionName(Opcode);
30 }
31
32
33 void ARMInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
34   unsigned Opcode = MI->getOpcode();
35
36   // Check for MOVs and print canonical forms, instead.
37   if (Opcode == ARM::MOVs) {
38     // FIXME: Thumb variants?
39     const MCOperand &Dst = MI->getOperand(0);
40     const MCOperand &MO1 = MI->getOperand(1);
41     const MCOperand &MO2 = MI->getOperand(2);
42     const MCOperand &MO3 = MI->getOperand(3);
43
44     O << '\t' << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()));
45     printSBitModifierOperand(MI, 6, O);
46     printPredicateOperand(MI, 4, O);
47
48     O << '\t' << getRegisterName(Dst.getReg())
49       << ", " << getRegisterName(MO1.getReg());
50
51     if (ARM_AM::getSORegShOp(MO3.getImm()) == ARM_AM::rrx)
52       return;
53
54     O << ", ";
55
56     if (MO2.getReg()) {
57       O << getRegisterName(MO2.getReg());
58       assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
59     } else {
60       O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
61     }
62     return;
63   }
64
65   // A8.6.123 PUSH
66   if ((Opcode == ARM::STMDB_UPD || Opcode == ARM::t2STMDB_UPD) &&
67       MI->getOperand(0).getReg() == ARM::SP) {
68     O << '\t' << "push";
69     printPredicateOperand(MI, 2, O);
70     if (Opcode == ARM::t2STMDB_UPD)
71       O << ".w";
72     O << '\t';
73     printRegisterList(MI, 4, O);
74     return;
75   }
76
77   // A8.6.122 POP
78   if ((Opcode == ARM::LDMIA_UPD || Opcode == ARM::t2LDMIA_UPD) &&
79       MI->getOperand(0).getReg() == ARM::SP) {
80     O << '\t' << "pop";
81     printPredicateOperand(MI, 2, O);
82     if (Opcode == ARM::t2LDMIA_UPD)
83       O << ".w";
84     O << '\t';
85     printRegisterList(MI, 4, O);
86     return;
87   }
88
89   // A8.6.355 VPUSH
90   if ((Opcode == ARM::VSTMSDB_UPD || Opcode == ARM::VSTMDDB_UPD) &&
91       MI->getOperand(0).getReg() == ARM::SP) {
92     O << '\t' << "vpush";
93     printPredicateOperand(MI, 2, O);
94     O << '\t';
95     printRegisterList(MI, 4, O);
96     return;
97   }
98
99   // A8.6.354 VPOP
100   if ((Opcode == ARM::VLDMSIA_UPD || Opcode == ARM::VLDMDIA_UPD) &&
101       MI->getOperand(0).getReg() == ARM::SP) {
102     O << '\t' << "vpop";
103     printPredicateOperand(MI, 2, O);
104     O << '\t';
105     printRegisterList(MI, 4, O);
106     return;
107   }
108
109   printInstruction(MI, O);
110 }
111
112 void ARMInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
113                                   raw_ostream &O) {
114   const MCOperand &Op = MI->getOperand(OpNo);
115   if (Op.isReg()) {
116     unsigned Reg = Op.getReg();
117     O << getRegisterName(Reg);
118   } else if (Op.isImm()) {
119     O << '#' << Op.getImm();
120   } else {
121     assert(Op.isExpr() && "unknown operand kind in printOperand");
122     O << *Op.getExpr();
123   }
124 }
125
126 static void printSOImm(raw_ostream &O, int64_t V, raw_ostream *CommentStream,
127                        const MCAsmInfo *MAI) {
128   // Break it up into two parts that make up a shifter immediate.
129   V = ARM_AM::getSOImmVal(V);
130   assert(V != -1 && "Not a valid so_imm value!");
131
132   unsigned Imm = ARM_AM::getSOImmValImm(V);
133   unsigned Rot = ARM_AM::getSOImmValRot(V);
134
135   // Print low-level immediate formation info, per
136   // A5.1.3: "Data-processing operands - Immediate".
137   if (Rot) {
138     O << "#" << Imm << ", " << Rot;
139     // Pretty printed version.
140     if (CommentStream)
141       *CommentStream << (int)ARM_AM::rotr32(Imm, Rot) << "\n";
142   } else {
143     O << "#" << Imm;
144   }
145 }
146
147
148 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
149 /// immediate in bits 0-7.
150 void ARMInstPrinter::printSOImmOperand(const MCInst *MI, unsigned OpNum,
151                                        raw_ostream &O) {
152   const MCOperand &MO = MI->getOperand(OpNum);
153   assert(MO.isImm() && "Not a valid so_imm value!");
154   printSOImm(O, MO.getImm(), CommentStream, &MAI);
155 }
156
157 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
158 // "Addressing Mode 1 - Data-processing operands" forms.  This includes:
159 //    REG 0   0           - e.g. R5
160 //    REG REG 0,SH_OPC    - e.g. R5, ROR R3
161 //    REG 0   IMM,SH_OPC  - e.g. R5, LSL #3
162 void ARMInstPrinter::printSORegOperand(const MCInst *MI, unsigned OpNum,
163                                        raw_ostream &O) {
164   const MCOperand &MO1 = MI->getOperand(OpNum);
165   const MCOperand &MO2 = MI->getOperand(OpNum+1);
166   const MCOperand &MO3 = MI->getOperand(OpNum+2);
167
168   O << getRegisterName(MO1.getReg());
169
170   // Print the shift opc.
171   ARM_AM::ShiftOpc ShOpc = ARM_AM::getSORegShOp(MO3.getImm());
172   O << ", " << ARM_AM::getShiftOpcStr(ShOpc);
173   if (MO2.getReg()) {
174     O << ' ' << getRegisterName(MO2.getReg());
175     assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
176   } else if (ShOpc != ARM_AM::rrx) {
177     O << " #" << ARM_AM::getSORegOffset(MO3.getImm());
178   }
179 }
180
181
182 void ARMInstPrinter::printAddrMode2Operand(const MCInst *MI, unsigned Op,
183                                            raw_ostream &O) {
184   const MCOperand &MO1 = MI->getOperand(Op);
185   const MCOperand &MO2 = MI->getOperand(Op+1);
186   const MCOperand &MO3 = MI->getOperand(Op+2);
187
188   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
189     printOperand(MI, Op, O);
190     return;
191   }
192
193   O << "[" << getRegisterName(MO1.getReg());
194
195   if (!MO2.getReg()) {
196     if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
197       O << ", #"
198         << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO3.getImm()))
199         << ARM_AM::getAM2Offset(MO3.getImm());
200     O << "]";
201     return;
202   }
203
204   O << ", "
205     << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO3.getImm()))
206     << getRegisterName(MO2.getReg());
207
208   if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
209     O << ", "
210     << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
211     << " #" << ShImm;
212   O << "]";
213 }
214
215 void ARMInstPrinter::printAddrMode2OffsetOperand(const MCInst *MI,
216                                                  unsigned OpNum,
217                                                  raw_ostream &O) {
218   const MCOperand &MO1 = MI->getOperand(OpNum);
219   const MCOperand &MO2 = MI->getOperand(OpNum+1);
220
221   if (!MO1.getReg()) {
222     unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
223     O << '#'
224       << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO2.getImm()))
225       << ImmOffs;
226     return;
227   }
228
229   O << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO2.getImm()))
230     << getRegisterName(MO1.getReg());
231
232   if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
233     O << ", "
234     << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
235     << " #" << ShImm;
236 }
237
238 void ARMInstPrinter::printAddrMode3Operand(const MCInst *MI, unsigned OpNum,
239                                            raw_ostream &O) {
240   const MCOperand &MO1 = MI->getOperand(OpNum);
241   const MCOperand &MO2 = MI->getOperand(OpNum+1);
242   const MCOperand &MO3 = MI->getOperand(OpNum+2);
243
244   O << '[' << getRegisterName(MO1.getReg());
245
246   if (MO2.getReg()) {
247     O << ", " << (char)ARM_AM::getAM3Op(MO3.getImm())
248       << getRegisterName(MO2.getReg()) << ']';
249     return;
250   }
251
252   if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
253     O << ", #"
254       << ARM_AM::getAddrOpcStr(ARM_AM::getAM3Op(MO3.getImm()))
255       << ImmOffs;
256   O << ']';
257 }
258
259 void ARMInstPrinter::printAddrMode3OffsetOperand(const MCInst *MI,
260                                                  unsigned OpNum,
261                                                  raw_ostream &O) {
262   const MCOperand &MO1 = MI->getOperand(OpNum);
263   const MCOperand &MO2 = MI->getOperand(OpNum+1);
264
265   if (MO1.getReg()) {
266     O << (char)ARM_AM::getAM3Op(MO2.getImm())
267     << getRegisterName(MO1.getReg());
268     return;
269   }
270
271   unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
272   O << '#'
273     << ARM_AM::getAddrOpcStr(ARM_AM::getAM3Op(MO2.getImm()))
274     << ImmOffs;
275 }
276
277 void ARMInstPrinter::printLdStmModeOperand(const MCInst *MI, unsigned OpNum,
278                                            raw_ostream &O) {
279   ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MI->getOperand(OpNum)
280                                                  .getImm());
281   O << ARM_AM::getAMSubModeStr(Mode);
282 }
283
284 void ARMInstPrinter::printAddrMode5Operand(const MCInst *MI, unsigned OpNum,
285                                            raw_ostream &O) {
286   const MCOperand &MO1 = MI->getOperand(OpNum);
287   const MCOperand &MO2 = MI->getOperand(OpNum+1);
288
289   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
290     printOperand(MI, OpNum, O);
291     return;
292   }
293
294   O << "[" << getRegisterName(MO1.getReg());
295
296   if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
297     O << ", #"
298       << ARM_AM::getAddrOpcStr(ARM_AM::getAM5Op(MO2.getImm()))
299       << ImmOffs * 4;
300   }
301   O << "]";
302 }
303
304 void ARMInstPrinter::printAddrMode6Operand(const MCInst *MI, unsigned OpNum,
305                                            raw_ostream &O) {
306   const MCOperand &MO1 = MI->getOperand(OpNum);
307   const MCOperand &MO2 = MI->getOperand(OpNum+1);
308
309   O << "[" << getRegisterName(MO1.getReg());
310   if (MO2.getImm()) {
311     // FIXME: Both darwin as and GNU as violate ARM docs here.
312     O << ", :" << (MO2.getImm() << 3);
313   }
314   O << "]";
315 }
316
317 void ARMInstPrinter::printAddrMode6OffsetOperand(const MCInst *MI,
318                                                  unsigned OpNum,
319                                                  raw_ostream &O) {
320   const MCOperand &MO = MI->getOperand(OpNum);
321   if (MO.getReg() == 0)
322     O << "!";
323   else
324     O << ", " << getRegisterName(MO.getReg());
325 }
326
327 void ARMInstPrinter::printBitfieldInvMaskImmOperand(const MCInst *MI,
328                                                     unsigned OpNum,
329                                                     raw_ostream &O) {
330   const MCOperand &MO = MI->getOperand(OpNum);
331   uint32_t v = ~MO.getImm();
332   int32_t lsb = CountTrailingZeros_32(v);
333   int32_t width = (32 - CountLeadingZeros_32 (v)) - lsb;
334   assert(MO.isImm() && "Not a valid bf_inv_mask_imm value!");
335   O << '#' << lsb << ", #" << width;
336 }
337
338 void ARMInstPrinter::printMemBOption(const MCInst *MI, unsigned OpNum,
339                                      raw_ostream &O) {
340   unsigned val = MI->getOperand(OpNum).getImm();
341   O << ARM_MB::MemBOptToString(val);
342 }
343
344 void ARMInstPrinter::printShiftImmOperand(const MCInst *MI, unsigned OpNum,
345                                           raw_ostream &O) {
346   unsigned ShiftOp = MI->getOperand(OpNum).getImm();
347   ARM_AM::ShiftOpc Opc = ARM_AM::getSORegShOp(ShiftOp);
348   switch (Opc) {
349   case ARM_AM::no_shift:
350     return;
351   case ARM_AM::lsl:
352     O << ", lsl #";
353     break;
354   case ARM_AM::asr:
355     O << ", asr #";
356     break;
357   default:
358     assert(0 && "unexpected shift opcode for shift immediate operand");
359   }
360   O << ARM_AM::getSORegOffset(ShiftOp);
361 }
362
363 void ARMInstPrinter::printRegisterList(const MCInst *MI, unsigned OpNum,
364                                        raw_ostream &O) {
365   O << "{";
366   for (unsigned i = OpNum, e = MI->getNumOperands(); i != e; ++i) {
367     if (i != OpNum) O << ", ";
368     O << getRegisterName(MI->getOperand(i).getReg());
369   }
370   O << "}";
371 }
372
373 void ARMInstPrinter::printSetendOperand(const MCInst *MI, unsigned OpNum,
374                                         raw_ostream &O) {
375   const MCOperand &Op = MI->getOperand(OpNum);
376   if (Op.getImm())
377     O << "be";
378   else
379     O << "le";
380 }
381
382 void ARMInstPrinter::printCPSIMod(const MCInst *MI, unsigned OpNum,
383                                   raw_ostream &O) {
384   const MCOperand &Op = MI->getOperand(OpNum);
385   O << ARM_PROC::IModToString(Op.getImm());
386 }
387
388 void ARMInstPrinter::printCPSIFlag(const MCInst *MI, unsigned OpNum,
389                                    raw_ostream &O) {
390   const MCOperand &Op = MI->getOperand(OpNum);
391   unsigned IFlags = Op.getImm();
392   for (int i=2; i >= 0; --i)
393     if (IFlags & (1 << i))
394       O << ARM_PROC::IFlagsToString(1 << i);
395 }
396
397 void ARMInstPrinter::printMSRMaskOperand(const MCInst *MI, unsigned OpNum,
398                                          raw_ostream &O) {
399   const MCOperand &Op = MI->getOperand(OpNum);
400   unsigned Mask = Op.getImm();
401   if (Mask) {
402     O << '_';
403     if (Mask & 8) O << 'f';
404     if (Mask & 4) O << 's';
405     if (Mask & 2) O << 'x';
406     if (Mask & 1) O << 'c';
407   }
408 }
409
410 void ARMInstPrinter::printNegZeroOperand(const MCInst *MI, unsigned OpNum,
411                                          raw_ostream &O) {
412   const MCOperand &Op = MI->getOperand(OpNum);
413   O << '#';
414   if (Op.getImm() < 0)
415     O << '-' << (-Op.getImm() - 1);
416   else
417     O << Op.getImm();
418 }
419
420 void ARMInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNum,
421                                            raw_ostream &O) {
422   ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
423   if (CC != ARMCC::AL)
424     O << ARMCondCodeToString(CC);
425 }
426
427 void ARMInstPrinter::printMandatoryPredicateOperand(const MCInst *MI,
428                                                     unsigned OpNum,
429                                                     raw_ostream &O) {
430   ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
431   O << ARMCondCodeToString(CC);
432 }
433
434 void ARMInstPrinter::printSBitModifierOperand(const MCInst *MI, unsigned OpNum,
435                                               raw_ostream &O) {
436   if (MI->getOperand(OpNum).getReg()) {
437     assert(MI->getOperand(OpNum).getReg() == ARM::CPSR &&
438            "Expect ARM CPSR register!");
439     O << 's';
440   }
441 }
442
443 void ARMInstPrinter::printNoHashImmediate(const MCInst *MI, unsigned OpNum,
444                                           raw_ostream &O) {
445   O << MI->getOperand(OpNum).getImm();
446 }
447
448 void ARMInstPrinter::printPImmediate(const MCInst *MI, unsigned OpNum,
449                                           raw_ostream &O) {
450   O << "p" << MI->getOperand(OpNum).getImm();
451 }
452
453 void ARMInstPrinter::printCImmediate(const MCInst *MI, unsigned OpNum,
454                                           raw_ostream &O) {
455   O << "c" << MI->getOperand(OpNum).getImm();
456 }
457
458 void ARMInstPrinter::printPCLabel(const MCInst *MI, unsigned OpNum,
459                                   raw_ostream &O) {
460   llvm_unreachable("Unhandled PC-relative pseudo-instruction!");
461 }
462
463 void ARMInstPrinter::printThumbS4ImmOperand(const MCInst *MI, unsigned OpNum,
464                                             raw_ostream &O) {
465   O << "#" <<  MI->getOperand(OpNum).getImm() * 4;
466 }
467
468 void ARMInstPrinter::printThumbITMask(const MCInst *MI, unsigned OpNum,
469                                       raw_ostream &O) {
470   // (3 - the number of trailing zeros) is the number of then / else.
471   unsigned Mask = MI->getOperand(OpNum).getImm();
472   unsigned CondBit0 = Mask >> 4 & 1;
473   unsigned NumTZ = CountTrailingZeros_32(Mask);
474   assert(NumTZ <= 3 && "Invalid IT mask!");
475   for (unsigned Pos = 3, e = NumTZ; Pos > e; --Pos) {
476     bool T = ((Mask >> Pos) & 1) == CondBit0;
477     if (T)
478       O << 't';
479     else
480       O << 'e';
481   }
482 }
483
484 void ARMInstPrinter::printThumbAddrModeRROperand(const MCInst *MI, unsigned Op,
485                                                  raw_ostream &O) {
486   const MCOperand &MO1 = MI->getOperand(Op);
487   const MCOperand &MO2 = MI->getOperand(Op + 1);
488
489   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
490     printOperand(MI, Op, O);
491     return;
492   }
493
494   O << "[" << getRegisterName(MO1.getReg());
495   if (unsigned RegNum = MO2.getReg())
496     O << ", " << getRegisterName(RegNum);
497   O << "]";
498 }
499
500 void ARMInstPrinter::printThumbAddrModeImm5SOperand(const MCInst *MI,
501                                                     unsigned Op,
502                                                     raw_ostream &O,
503                                                     unsigned Scale) {
504   const MCOperand &MO1 = MI->getOperand(Op);
505   const MCOperand &MO2 = MI->getOperand(Op + 1);
506
507   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
508     printOperand(MI, Op, O);
509     return;
510   }
511
512   O << "[" << getRegisterName(MO1.getReg());
513   if (unsigned ImmOffs = MO2.getImm())
514     O << ", #" << ImmOffs * Scale;
515   O << "]";
516 }
517
518 void ARMInstPrinter::printThumbAddrModeImm5S1Operand(const MCInst *MI,
519                                                      unsigned Op,
520                                                      raw_ostream &O) {
521   printThumbAddrModeImm5SOperand(MI, Op, O, 1);
522 }
523
524 void ARMInstPrinter::printThumbAddrModeImm5S2Operand(const MCInst *MI,
525                                                      unsigned Op,
526                                                      raw_ostream &O) {
527   printThumbAddrModeImm5SOperand(MI, Op, O, 2);
528 }
529
530 void ARMInstPrinter::printThumbAddrModeImm5S4Operand(const MCInst *MI,
531                                                      unsigned Op,
532                                                      raw_ostream &O) {
533   printThumbAddrModeImm5SOperand(MI, Op, O, 4);
534 }
535
536 void ARMInstPrinter::printThumbAddrModeSPOperand(const MCInst *MI, unsigned Op,
537                                                  raw_ostream &O) {
538   printThumbAddrModeImm5SOperand(MI, Op, O, 4);
539 }
540
541 // Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
542 // register with shift forms.
543 // REG 0   0           - e.g. R5
544 // REG IMM, SH_OPC     - e.g. R5, LSL #3
545 void ARMInstPrinter::printT2SOOperand(const MCInst *MI, unsigned OpNum,
546                                       raw_ostream &O) {
547   const MCOperand &MO1 = MI->getOperand(OpNum);
548   const MCOperand &MO2 = MI->getOperand(OpNum+1);
549
550   unsigned Reg = MO1.getReg();
551   O << getRegisterName(Reg);
552
553   // Print the shift opc.
554   assert(MO2.isImm() && "Not a valid t2_so_reg value!");
555   ARM_AM::ShiftOpc ShOpc = ARM_AM::getSORegShOp(MO2.getImm());
556   O << ", " << ARM_AM::getShiftOpcStr(ShOpc);
557   if (ShOpc != ARM_AM::rrx)
558     O << " #" << ARM_AM::getSORegOffset(MO2.getImm());
559 }
560
561 void ARMInstPrinter::printAddrModeImm12Operand(const MCInst *MI, unsigned OpNum,
562                                                raw_ostream &O) {
563   const MCOperand &MO1 = MI->getOperand(OpNum);
564   const MCOperand &MO2 = MI->getOperand(OpNum+1);
565
566   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
567     printOperand(MI, OpNum, O);
568     return;
569   }
570
571   O << "[" << getRegisterName(MO1.getReg());
572
573   int32_t OffImm = (int32_t)MO2.getImm();
574   bool isSub = OffImm < 0;
575   // Special value for #-0. All others are normal.
576   if (OffImm == INT32_MIN)
577     OffImm = 0;
578   if (isSub)
579     O << ", #-" << -OffImm;
580   else if (OffImm > 0)
581     O << ", #" << OffImm;
582   O << "]";
583 }
584
585 void ARMInstPrinter::printT2AddrModeImm8Operand(const MCInst *MI,
586                                                 unsigned OpNum,
587                                                 raw_ostream &O) {
588   const MCOperand &MO1 = MI->getOperand(OpNum);
589   const MCOperand &MO2 = MI->getOperand(OpNum+1);
590
591   O << "[" << getRegisterName(MO1.getReg());
592
593   int32_t OffImm = (int32_t)MO2.getImm();
594   // Don't print +0.
595   if (OffImm < 0)
596     O << ", #-" << -OffImm;
597   else if (OffImm > 0)
598     O << ", #" << OffImm;
599   O << "]";
600 }
601
602 void ARMInstPrinter::printT2AddrModeImm8s4Operand(const MCInst *MI,
603                                                   unsigned OpNum,
604                                                   raw_ostream &O) {
605   const MCOperand &MO1 = MI->getOperand(OpNum);
606   const MCOperand &MO2 = MI->getOperand(OpNum+1);
607
608   O << "[" << getRegisterName(MO1.getReg());
609
610   int32_t OffImm = (int32_t)MO2.getImm() / 4;
611   // Don't print +0.
612   if (OffImm < 0)
613     O << ", #-" << -OffImm * 4;
614   else if (OffImm > 0)
615     O << ", #" << OffImm * 4;
616   O << "]";
617 }
618
619 void ARMInstPrinter::printT2AddrModeImm8OffsetOperand(const MCInst *MI,
620                                                       unsigned OpNum,
621                                                       raw_ostream &O) {
622   const MCOperand &MO1 = MI->getOperand(OpNum);
623   int32_t OffImm = (int32_t)MO1.getImm();
624   // Don't print +0.
625   if (OffImm < 0)
626     O << "#-" << -OffImm;
627   else if (OffImm > 0)
628     O << "#" << OffImm;
629 }
630
631 void ARMInstPrinter::printT2AddrModeImm8s4OffsetOperand(const MCInst *MI,
632                                                         unsigned OpNum,
633                                                         raw_ostream &O) {
634   const MCOperand &MO1 = MI->getOperand(OpNum);
635   int32_t OffImm = (int32_t)MO1.getImm() / 4;
636   // Don't print +0.
637   if (OffImm < 0)
638     O << "#-" << -OffImm * 4;
639   else if (OffImm > 0)
640     O << "#" << OffImm * 4;
641 }
642
643 void ARMInstPrinter::printT2AddrModeSoRegOperand(const MCInst *MI,
644                                                  unsigned OpNum,
645                                                  raw_ostream &O) {
646   const MCOperand &MO1 = MI->getOperand(OpNum);
647   const MCOperand &MO2 = MI->getOperand(OpNum+1);
648   const MCOperand &MO3 = MI->getOperand(OpNum+2);
649
650   O << "[" << getRegisterName(MO1.getReg());
651
652   assert(MO2.getReg() && "Invalid so_reg load / store address!");
653   O << ", " << getRegisterName(MO2.getReg());
654
655   unsigned ShAmt = MO3.getImm();
656   if (ShAmt) {
657     assert(ShAmt <= 3 && "Not a valid Thumb2 addressing mode!");
658     O << ", lsl #" << ShAmt;
659   }
660   O << "]";
661 }
662
663 void ARMInstPrinter::printVFPf32ImmOperand(const MCInst *MI, unsigned OpNum,
664                                            raw_ostream &O) {
665   const MCOperand &MO = MI->getOperand(OpNum);
666   O << '#';
667   if (MO.isFPImm()) {
668     O << (float)MO.getFPImm();
669   } else {
670     union {
671       uint32_t I;
672       float F;
673     } FPUnion;
674
675     FPUnion.I = MO.getImm();
676     O << FPUnion.F;
677   }
678 }
679
680 void ARMInstPrinter::printVFPf64ImmOperand(const MCInst *MI, unsigned OpNum,
681                                            raw_ostream &O) {
682   const MCOperand &MO = MI->getOperand(OpNum);
683   O << '#';
684   if (MO.isFPImm()) {
685     O << MO.getFPImm();
686   } else {
687     // We expect the binary encoding of a floating point number here.
688     union {
689       uint64_t I;
690       double D;
691     } FPUnion;
692
693     FPUnion.I = MO.getImm();
694     O << FPUnion.D;
695   }
696 }
697
698 void ARMInstPrinter::printNEONModImmOperand(const MCInst *MI, unsigned OpNum,
699                                             raw_ostream &O) {
700   unsigned EncodedImm = MI->getOperand(OpNum).getImm();
701   unsigned EltBits;
702   uint64_t Val = ARM_AM::decodeNEONModImm(EncodedImm, EltBits);
703   O << "#0x" << utohexstr(Val);
704 }