1 //===-- X86ATTInstPrinter.cpp - AT&T assembly instruction printing --------===//
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 file includes code for rendering MCInst instances as AT&T-style
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
16 #include "X86ATTInstPrinter.h"
17 #include "X86InstComments.h"
18 #include "MCTargetDesc/X86MCTargetDesc.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/FormattedStream.h"
28 // Include the auto-generated portion of the assembly writer.
29 #define GET_INSTRUCTION_NAME
30 #define PRINT_ALIAS_INSTR
31 #include "X86GenAsmWriter.inc"
33 X86ATTInstPrinter::X86ATTInstPrinter(const MCAsmInfo &MAI)
34 : MCInstPrinter(MAI) {
37 void X86ATTInstPrinter::printRegName(raw_ostream &OS,
38 unsigned RegNo) const {
39 OS << '%' << getRegisterName(RegNo);
42 void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
43 // Try to print any aliases first.
44 if (!printAliasInstr(MI, OS))
45 printInstruction(MI, OS);
47 // If verbose assembly is enabled, we can print some informative comments.
49 printAnnotations(MI, *CommentStream);
50 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
54 StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
55 return getInstructionName(Opcode);
58 void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
60 switch (MI->getOperand(Op).getImm()) {
61 default: assert(0 && "Invalid ssecc argument!");
62 case 0: O << "eq"; break;
63 case 1: O << "lt"; break;
64 case 2: O << "le"; break;
65 case 3: O << "unord"; break;
66 case 4: O << "neq"; break;
67 case 5: O << "nlt"; break;
68 case 6: O << "nle"; break;
69 case 7: O << "ord"; break;
73 /// print_pcrel_imm - This is used to print an immediate value that ends up
74 /// being encoded as a pc-relative value (e.g. for jumps and calls). These
75 /// print slightly differently than normal immediates. For example, a $ is not
77 void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
79 const MCOperand &Op = MI->getOperand(OpNo);
81 // Print this as a signed 32-bit value.
82 O << (int)Op.getImm();
84 assert(Op.isExpr() && "unknown pcrel immediate operand");
89 void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
91 const MCOperand &Op = MI->getOperand(OpNo);
93 O << '%' << getRegisterName(Op.getReg());
94 } else if (Op.isImm()) {
95 // Print X86 immediates as signed values.
96 O << '$' << (int64_t)Op.getImm();
98 if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
99 *CommentStream << format("imm = 0x%llX\n", (long long)Op.getImm());
102 assert(Op.isExpr() && "unknown operand kind in printOperand");
103 O << '$' << *Op.getExpr();
107 void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
109 const MCOperand &BaseReg = MI->getOperand(Op);
110 const MCOperand &IndexReg = MI->getOperand(Op+2);
111 const MCOperand &DispSpec = MI->getOperand(Op+3);
112 const MCOperand &SegReg = MI->getOperand(Op+4);
114 // If this has a segment register, print it.
115 if (SegReg.getReg()) {
116 printOperand(MI, Op+4, O);
120 if (DispSpec.isImm()) {
121 int64_t DispVal = DispSpec.getImm();
122 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
125 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
126 O << *DispSpec.getExpr();
129 if (IndexReg.getReg() || BaseReg.getReg()) {
131 if (BaseReg.getReg())
132 printOperand(MI, Op, O);
134 if (IndexReg.getReg()) {
136 printOperand(MI, Op+2, O);
137 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
139 O << ',' << ScaleVal;