1 //===-- ARMInstPrinter.cpp - Convert ARM MCInst to assembly syntax --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class prints an ARM MCInst to a .s file.
12 //===----------------------------------------------------------------------===//
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"
25 #define GET_INSTRUCTION_NAME
26 #include "ARMGenAsmWriter.inc"
28 StringRef ARMInstPrinter::getOpcodeName(unsigned Opcode) const {
29 return getInstructionName(Opcode);
33 void ARMInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
34 unsigned Opcode = MI->getOpcode();
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);
44 O << '\t' << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()));
45 printSBitModifierOperand(MI, 6, O);
46 printPredicateOperand(MI, 4, O);
48 O << '\t' << getRegisterName(Dst.getReg())
49 << ", " << getRegisterName(MO1.getReg());
51 if (ARM_AM::getSORegShOp(MO3.getImm()) == ARM_AM::rrx)
57 O << getRegisterName(MO2.getReg());
58 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
60 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
66 if ((Opcode == ARM::STMDB_UPD || Opcode == ARM::t2STMDB_UPD) &&
67 MI->getOperand(0).getReg() == ARM::SP) {
69 printPredicateOperand(MI, 2, O);
70 if (Opcode == ARM::t2STMDB_UPD)
73 printRegisterList(MI, 4, O);
78 if ((Opcode == ARM::LDMIA_UPD || Opcode == ARM::t2LDMIA_UPD) &&
79 MI->getOperand(0).getReg() == ARM::SP) {
81 printPredicateOperand(MI, 2, O);
82 if (Opcode == ARM::t2LDMIA_UPD)
85 printRegisterList(MI, 4, O);
90 if ((Opcode == ARM::VSTMSDB_UPD || Opcode == ARM::VSTMDDB_UPD) &&
91 MI->getOperand(0).getReg() == ARM::SP) {
93 printPredicateOperand(MI, 2, O);
95 printRegisterList(MI, 4, O);
100 if ((Opcode == ARM::VLDMSIA_UPD || Opcode == ARM::VLDMDIA_UPD) &&
101 MI->getOperand(0).getReg() == ARM::SP) {
103 printPredicateOperand(MI, 2, O);
105 printRegisterList(MI, 4, O);
109 printInstruction(MI, O);
112 void ARMInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
114 const MCOperand &Op = MI->getOperand(OpNo);
116 unsigned Reg = Op.getReg();
117 O << getRegisterName(Reg);
118 } else if (Op.isImm()) {
119 O << '#' << Op.getImm();
121 assert(Op.isExpr() && "unknown operand kind in printOperand");
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!");
132 unsigned Imm = ARM_AM::getSOImmValImm(V);
133 unsigned Rot = ARM_AM::getSOImmValRot(V);
135 // Print low-level immediate formation info, per
136 // A5.1.3: "Data-processing operands - Immediate".
138 O << "#" << Imm << ", " << Rot;
139 // Pretty printed version.
141 *CommentStream << (int)ARM_AM::rotr32(Imm, Rot) << "\n";
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,
152 const MCOperand &MO = MI->getOperand(OpNum);
153 assert(MO.isImm() && "Not a valid so_imm value!");
154 printSOImm(O, MO.getImm(), CommentStream, &MAI);
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:
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,
164 const MCOperand &MO1 = MI->getOperand(OpNum);
165 const MCOperand &MO2 = MI->getOperand(OpNum+1);
166 const MCOperand &MO3 = MI->getOperand(OpNum+2);
168 O << getRegisterName(MO1.getReg());
170 // Print the shift opc.
171 ARM_AM::ShiftOpc ShOpc = ARM_AM::getSORegShOp(MO3.getImm());
172 O << ", " << ARM_AM::getShiftOpcStr(ShOpc);
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());
182 void ARMInstPrinter::printAddrMode2Operand(const MCInst *MI, unsigned Op,
184 const MCOperand &MO1 = MI->getOperand(Op);
185 const MCOperand &MO2 = MI->getOperand(Op+1);
186 const MCOperand &MO3 = MI->getOperand(Op+2);
188 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
189 printOperand(MI, Op, O);
193 O << "[" << getRegisterName(MO1.getReg());
196 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
198 << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO3.getImm()))
199 << ARM_AM::getAM2Offset(MO3.getImm());
205 << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO3.getImm()))
206 << getRegisterName(MO2.getReg());
208 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
210 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
215 void ARMInstPrinter::printAddrMode2OffsetOperand(const MCInst *MI,
218 const MCOperand &MO1 = MI->getOperand(OpNum);
219 const MCOperand &MO2 = MI->getOperand(OpNum+1);
222 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
224 << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO2.getImm()))
229 O << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO2.getImm()))
230 << getRegisterName(MO1.getReg());
232 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
234 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
238 void ARMInstPrinter::printAddrMode3Operand(const MCInst *MI, unsigned OpNum,
240 const MCOperand &MO1 = MI->getOperand(OpNum);
241 const MCOperand &MO2 = MI->getOperand(OpNum+1);
242 const MCOperand &MO3 = MI->getOperand(OpNum+2);
244 O << '[' << getRegisterName(MO1.getReg());
247 O << ", " << (char)ARM_AM::getAM3Op(MO3.getImm())
248 << getRegisterName(MO2.getReg()) << ']';
252 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
254 << ARM_AM::getAddrOpcStr(ARM_AM::getAM3Op(MO3.getImm()))
259 void ARMInstPrinter::printAddrMode3OffsetOperand(const MCInst *MI,
262 const MCOperand &MO1 = MI->getOperand(OpNum);
263 const MCOperand &MO2 = MI->getOperand(OpNum+1);
266 O << (char)ARM_AM::getAM3Op(MO2.getImm())
267 << getRegisterName(MO1.getReg());
271 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
273 << ARM_AM::getAddrOpcStr(ARM_AM::getAM3Op(MO2.getImm()))
277 void ARMInstPrinter::printLdStmModeOperand(const MCInst *MI, unsigned OpNum,
279 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MI->getOperand(OpNum)
281 O << ARM_AM::getAMSubModeStr(Mode);
284 void ARMInstPrinter::printAddrMode5Operand(const MCInst *MI, unsigned OpNum,
286 const MCOperand &MO1 = MI->getOperand(OpNum);
287 const MCOperand &MO2 = MI->getOperand(OpNum+1);
289 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
290 printOperand(MI, OpNum, O);
294 O << "[" << getRegisterName(MO1.getReg());
296 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
298 << ARM_AM::getAddrOpcStr(ARM_AM::getAM5Op(MO2.getImm()))
304 void ARMInstPrinter::printAddrMode6Operand(const MCInst *MI, unsigned OpNum,
306 const MCOperand &MO1 = MI->getOperand(OpNum);
307 const MCOperand &MO2 = MI->getOperand(OpNum+1);
309 O << "[" << getRegisterName(MO1.getReg());
311 // FIXME: Both darwin as and GNU as violate ARM docs here.
312 O << ", :" << (MO2.getImm() << 3);
317 void ARMInstPrinter::printAddrMode6OffsetOperand(const MCInst *MI,
320 const MCOperand &MO = MI->getOperand(OpNum);
321 if (MO.getReg() == 0)
324 O << ", " << getRegisterName(MO.getReg());
327 void ARMInstPrinter::printBitfieldInvMaskImmOperand(const MCInst *MI,
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;
338 void ARMInstPrinter::printMemBOption(const MCInst *MI, unsigned OpNum,
340 unsigned val = MI->getOperand(OpNum).getImm();
341 O << ARM_MB::MemBOptToString(val);
344 void ARMInstPrinter::printShiftImmOperand(const MCInst *MI, unsigned OpNum,
346 unsigned ShiftOp = MI->getOperand(OpNum).getImm();
347 ARM_AM::ShiftOpc Opc = ARM_AM::getSORegShOp(ShiftOp);
349 case ARM_AM::no_shift:
358 assert(0 && "unexpected shift opcode for shift immediate operand");
360 O << ARM_AM::getSORegOffset(ShiftOp);
363 void ARMInstPrinter::printRegisterList(const MCInst *MI, unsigned OpNum,
366 for (unsigned i = OpNum, e = MI->getNumOperands(); i != e; ++i) {
367 if (i != OpNum) O << ", ";
368 O << getRegisterName(MI->getOperand(i).getReg());
373 void ARMInstPrinter::printSetendOperand(const MCInst *MI, unsigned OpNum,
375 const MCOperand &Op = MI->getOperand(OpNum);
382 void ARMInstPrinter::printCPSIMod(const MCInst *MI, unsigned OpNum,
384 const MCOperand &Op = MI->getOperand(OpNum);
385 O << ARM_PROC::IModToString(Op.getImm());
388 void ARMInstPrinter::printCPSIFlag(const MCInst *MI, unsigned OpNum,
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);
397 void ARMInstPrinter::printMSRMaskOperand(const MCInst *MI, unsigned OpNum,
399 const MCOperand &Op = MI->getOperand(OpNum);
400 unsigned Mask = Op.getImm();
403 if (Mask & 8) O << 'f';
404 if (Mask & 4) O << 's';
405 if (Mask & 2) O << 'x';
406 if (Mask & 1) O << 'c';
410 void ARMInstPrinter::printNegZeroOperand(const MCInst *MI, unsigned OpNum,
412 const MCOperand &Op = MI->getOperand(OpNum);
415 O << '-' << (-Op.getImm() - 1);
420 void ARMInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNum,
422 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
424 O << ARMCondCodeToString(CC);
427 void ARMInstPrinter::printMandatoryPredicateOperand(const MCInst *MI,
430 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
431 O << ARMCondCodeToString(CC);
434 void ARMInstPrinter::printSBitModifierOperand(const MCInst *MI, unsigned OpNum,
436 if (MI->getOperand(OpNum).getReg()) {
437 assert(MI->getOperand(OpNum).getReg() == ARM::CPSR &&
438 "Expect ARM CPSR register!");
443 void ARMInstPrinter::printNoHashImmediate(const MCInst *MI, unsigned OpNum,
445 O << MI->getOperand(OpNum).getImm();
448 void ARMInstPrinter::printPImmediate(const MCInst *MI, unsigned OpNum,
450 O << "p" << MI->getOperand(OpNum).getImm();
453 void ARMInstPrinter::printCImmediate(const MCInst *MI, unsigned OpNum,
455 O << "c" << MI->getOperand(OpNum).getImm();
458 void ARMInstPrinter::printPCLabel(const MCInst *MI, unsigned OpNum,
460 llvm_unreachable("Unhandled PC-relative pseudo-instruction!");
463 void ARMInstPrinter::printThumbS4ImmOperand(const MCInst *MI, unsigned OpNum,
465 O << "#" << MI->getOperand(OpNum).getImm() * 4;
468 void ARMInstPrinter::printThumbITMask(const MCInst *MI, unsigned OpNum,
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;
484 void ARMInstPrinter::printThumbAddrModeRROperand(const MCInst *MI, unsigned Op,
486 const MCOperand &MO1 = MI->getOperand(Op);
487 const MCOperand &MO2 = MI->getOperand(Op + 1);
489 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
490 printOperand(MI, Op, O);
494 O << "[" << getRegisterName(MO1.getReg());
495 if (unsigned RegNum = MO2.getReg())
496 O << ", " << getRegisterName(RegNum);
500 void ARMInstPrinter::printThumbAddrModeImm5SOperand(const MCInst *MI,
504 const MCOperand &MO1 = MI->getOperand(Op);
505 const MCOperand &MO2 = MI->getOperand(Op + 1);
507 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
508 printOperand(MI, Op, O);
512 O << "[" << getRegisterName(MO1.getReg());
513 if (unsigned ImmOffs = MO2.getImm())
514 O << ", #" << ImmOffs * Scale;
518 void ARMInstPrinter::printThumbAddrModeImm5S1Operand(const MCInst *MI,
521 printThumbAddrModeImm5SOperand(MI, Op, O, 1);
524 void ARMInstPrinter::printThumbAddrModeImm5S2Operand(const MCInst *MI,
527 printThumbAddrModeImm5SOperand(MI, Op, O, 2);
530 void ARMInstPrinter::printThumbAddrModeImm5S4Operand(const MCInst *MI,
533 printThumbAddrModeImm5SOperand(MI, Op, O, 4);
536 void ARMInstPrinter::printThumbAddrModeSPOperand(const MCInst *MI, unsigned Op,
538 printThumbAddrModeImm5SOperand(MI, Op, O, 4);
541 // Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
542 // register with shift forms.
544 // REG IMM, SH_OPC - e.g. R5, LSL #3
545 void ARMInstPrinter::printT2SOOperand(const MCInst *MI, unsigned OpNum,
547 const MCOperand &MO1 = MI->getOperand(OpNum);
548 const MCOperand &MO2 = MI->getOperand(OpNum+1);
550 unsigned Reg = MO1.getReg();
551 O << getRegisterName(Reg);
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());
561 void ARMInstPrinter::printAddrModeImm12Operand(const MCInst *MI, unsigned OpNum,
563 const MCOperand &MO1 = MI->getOperand(OpNum);
564 const MCOperand &MO2 = MI->getOperand(OpNum+1);
566 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
567 printOperand(MI, OpNum, O);
571 O << "[" << getRegisterName(MO1.getReg());
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)
579 O << ", #-" << -OffImm;
581 O << ", #" << OffImm;
585 void ARMInstPrinter::printT2AddrModeImm8Operand(const MCInst *MI,
588 const MCOperand &MO1 = MI->getOperand(OpNum);
589 const MCOperand &MO2 = MI->getOperand(OpNum+1);
591 O << "[" << getRegisterName(MO1.getReg());
593 int32_t OffImm = (int32_t)MO2.getImm();
596 O << ", #-" << -OffImm;
598 O << ", #" << OffImm;
602 void ARMInstPrinter::printT2AddrModeImm8s4Operand(const MCInst *MI,
605 const MCOperand &MO1 = MI->getOperand(OpNum);
606 const MCOperand &MO2 = MI->getOperand(OpNum+1);
608 O << "[" << getRegisterName(MO1.getReg());
610 int32_t OffImm = (int32_t)MO2.getImm() / 4;
613 O << ", #-" << -OffImm * 4;
615 O << ", #" << OffImm * 4;
619 void ARMInstPrinter::printT2AddrModeImm8OffsetOperand(const MCInst *MI,
622 const MCOperand &MO1 = MI->getOperand(OpNum);
623 int32_t OffImm = (int32_t)MO1.getImm();
626 O << "#-" << -OffImm;
631 void ARMInstPrinter::printT2AddrModeImm8s4OffsetOperand(const MCInst *MI,
634 const MCOperand &MO1 = MI->getOperand(OpNum);
635 int32_t OffImm = (int32_t)MO1.getImm() / 4;
638 O << "#-" << -OffImm * 4;
640 O << "#" << OffImm * 4;
643 void ARMInstPrinter::printT2AddrModeSoRegOperand(const MCInst *MI,
646 const MCOperand &MO1 = MI->getOperand(OpNum);
647 const MCOperand &MO2 = MI->getOperand(OpNum+1);
648 const MCOperand &MO3 = MI->getOperand(OpNum+2);
650 O << "[" << getRegisterName(MO1.getReg());
652 assert(MO2.getReg() && "Invalid so_reg load / store address!");
653 O << ", " << getRegisterName(MO2.getReg());
655 unsigned ShAmt = MO3.getImm();
657 assert(ShAmt <= 3 && "Not a valid Thumb2 addressing mode!");
658 O << ", lsl #" << ShAmt;
663 void ARMInstPrinter::printVFPf32ImmOperand(const MCInst *MI, unsigned OpNum,
665 const MCOperand &MO = MI->getOperand(OpNum);
668 O << (float)MO.getFPImm();
675 FPUnion.I = MO.getImm();
680 void ARMInstPrinter::printVFPf64ImmOperand(const MCInst *MI, unsigned OpNum,
682 const MCOperand &MO = MI->getOperand(OpNum);
687 // We expect the binary encoding of a floating point number here.
693 FPUnion.I = MO.getImm();
698 void ARMInstPrinter::printNEONModImmOperand(const MCInst *MI, unsigned OpNum,
700 unsigned EncodedImm = MI->getOperand(OpNum).getImm();
702 uint64_t Val = ARM_AM::decodeNEONModImm(EncodedImm, EltBits);
703 O << "#0x" << utohexstr(Val);