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