1 //===-- X86IntelInstPrinter.cpp - Intel 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 Intel-style
13 //===----------------------------------------------------------------------===//
15 #include "X86IntelInstPrinter.h"
16 #include "MCTargetDesc/X86BaseInfo.h"
17 #include "MCTargetDesc/X86MCTargetDesc.h"
18 #include "X86InstComments.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/FormattedStream.h"
27 #define DEBUG_TYPE "asm-printer"
29 #include "X86GenAsmWriter1.inc"
31 void X86IntelInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
32 OS << getRegisterName(RegNo);
35 void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
37 const MCSubtargetInfo &STI) {
38 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
39 uint64_t TSFlags = Desc.TSFlags;
41 if (TSFlags & X86II::LOCK)
44 printInstruction(MI, OS);
46 // Next always print the annotation.
47 printAnnotation(OS, Annot);
49 // If verbose assembly is enabled, we can print some informative comments.
51 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
54 void X86IntelInstPrinter::printSSEAVXCC(const MCInst *MI, unsigned Op,
56 int64_t Imm = MI->getOperand(Op).getImm();
58 default: llvm_unreachable("Invalid avxcc argument!");
59 case 0: O << "eq"; break;
60 case 1: O << "lt"; break;
61 case 2: O << "le"; break;
62 case 3: O << "unord"; break;
63 case 4: O << "neq"; break;
64 case 5: O << "nlt"; break;
65 case 6: O << "nle"; break;
66 case 7: O << "ord"; break;
67 case 8: O << "eq_uq"; break;
68 case 9: O << "nge"; break;
69 case 0xa: O << "ngt"; break;
70 case 0xb: O << "false"; break;
71 case 0xc: O << "neq_oq"; break;
72 case 0xd: O << "ge"; break;
73 case 0xe: O << "gt"; break;
74 case 0xf: O << "true"; break;
75 case 0x10: O << "eq_os"; break;
76 case 0x11: O << "lt_oq"; break;
77 case 0x12: O << "le_oq"; break;
78 case 0x13: O << "unord_s"; break;
79 case 0x14: O << "neq_us"; break;
80 case 0x15: O << "nlt_uq"; break;
81 case 0x16: O << "nle_uq"; break;
82 case 0x17: O << "ord_s"; break;
83 case 0x18: O << "eq_us"; break;
84 case 0x19: O << "nge_uq"; break;
85 case 0x1a: O << "ngt_uq"; break;
86 case 0x1b: O << "false_os"; break;
87 case 0x1c: O << "neq_os"; break;
88 case 0x1d: O << "ge_oq"; break;
89 case 0x1e: O << "gt_oq"; break;
90 case 0x1f: O << "true_us"; break;
94 void X86IntelInstPrinter::printXOPCC(const MCInst *MI, unsigned Op,
96 int64_t Imm = MI->getOperand(Op).getImm();
98 default: llvm_unreachable("Invalid xopcc argument!");
99 case 0: O << "lt"; break;
100 case 1: O << "le"; break;
101 case 2: O << "gt"; break;
102 case 3: O << "ge"; break;
103 case 4: O << "eq"; break;
104 case 5: O << "neq"; break;
105 case 6: O << "false"; break;
106 case 7: O << "true"; break;
110 void X86IntelInstPrinter::printRoundingControl(const MCInst *MI, unsigned Op,
112 int64_t Imm = MI->getOperand(Op).getImm() & 0x3;
114 case 0: O << "{rn-sae}"; break;
115 case 1: O << "{rd-sae}"; break;
116 case 2: O << "{ru-sae}"; break;
117 case 3: O << "{rz-sae}"; break;
121 /// printPCRelImm - This is used to print an immediate value that ends up
122 /// being encoded as a pc-relative value.
123 void X86IntelInstPrinter::printPCRelImm(const MCInst *MI, unsigned OpNo,
125 const MCOperand &Op = MI->getOperand(OpNo);
127 O << formatImm(Op.getImm());
129 assert(Op.isExpr() && "unknown pcrel immediate operand");
130 // If a symbolic branch target was added as a constant expression then print
131 // that address in hex.
132 const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr());
134 if (BranchTarget && BranchTarget->EvaluateAsAbsolute(Address)) {
135 O << formatHex((uint64_t)Address);
138 // Otherwise, just print the expression.
144 void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
146 const MCOperand &Op = MI->getOperand(OpNo);
148 printRegName(O, Op.getReg());
149 } else if (Op.isImm()) {
150 O << formatImm((int64_t)Op.getImm());
152 assert(Op.isExpr() && "unknown operand kind in printOperand");
157 void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
159 const MCOperand &BaseReg = MI->getOperand(Op+X86::AddrBaseReg);
160 unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
161 const MCOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
162 const MCOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
163 const MCOperand &SegReg = MI->getOperand(Op+X86::AddrSegmentReg);
165 // If this has a segment register, print it.
166 if (SegReg.getReg()) {
167 printOperand(MI, Op+X86::AddrSegmentReg, O);
173 bool NeedPlus = false;
174 if (BaseReg.getReg()) {
175 printOperand(MI, Op+X86::AddrBaseReg, O);
179 if (IndexReg.getReg()) {
180 if (NeedPlus) O << " + ";
182 O << ScaleVal << '*';
183 printOperand(MI, Op+X86::AddrIndexReg, O);
187 if (!DispSpec.isImm()) {
188 if (NeedPlus) O << " + ";
189 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
190 O << *DispSpec.getExpr();
192 int64_t DispVal = DispSpec.getImm();
193 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
202 O << formatImm(DispVal);
209 void X86IntelInstPrinter::printSrcIdx(const MCInst *MI, unsigned Op,
211 const MCOperand &SegReg = MI->getOperand(Op+1);
213 // If this has a segment register, print it.
214 if (SegReg.getReg()) {
215 printOperand(MI, Op+1, O);
219 printOperand(MI, Op, O);
223 void X86IntelInstPrinter::printDstIdx(const MCInst *MI, unsigned Op,
225 // DI accesses are always ES-based.
227 printOperand(MI, Op, O);
231 void X86IntelInstPrinter::printMemOffset(const MCInst *MI, unsigned Op,
233 const MCOperand &DispSpec = MI->getOperand(Op);
234 const MCOperand &SegReg = MI->getOperand(Op+1);
236 // If this has a segment register, print it.
237 if (SegReg.getReg()) {
238 printOperand(MI, Op+1, O);
244 if (DispSpec.isImm()) {
245 O << formatImm(DispSpec.getImm());
247 assert(DispSpec.isExpr() && "non-immediate displacement?");
248 O << *DispSpec.getExpr();
254 void X86IntelInstPrinter::printU8Imm(const MCInst *MI, unsigned Op,
256 O << formatImm(MI->getOperand(Op).getImm() & 0xff);