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