LLVM API Documentation
00001 //===-- X86IntelInstPrinter.cpp - Intel assembly instruction printing -----===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file includes code for rendering MCInst instances as Intel-style 00011 // assembly. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "asm-printer" 00016 #include "X86IntelInstPrinter.h" 00017 #include "MCTargetDesc/X86BaseInfo.h" 00018 #include "MCTargetDesc/X86MCTargetDesc.h" 00019 #include "X86InstComments.h" 00020 #include "llvm/MC/MCExpr.h" 00021 #include "llvm/MC/MCInst.h" 00022 #include "llvm/MC/MCInstrInfo.h" 00023 #include "llvm/Support/ErrorHandling.h" 00024 #include "llvm/Support/FormattedStream.h" 00025 #include <cctype> 00026 using namespace llvm; 00027 00028 #include "X86GenAsmWriter1.inc" 00029 00030 void X86IntelInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const { 00031 OS << getRegisterName(RegNo); 00032 } 00033 00034 void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS, 00035 StringRef Annot) { 00036 const MCInstrDesc &Desc = MII.get(MI->getOpcode()); 00037 uint64_t TSFlags = Desc.TSFlags; 00038 00039 if (TSFlags & X86II::LOCK) 00040 OS << "\tlock\n"; 00041 00042 printInstruction(MI, OS); 00043 00044 // Next always print the annotation. 00045 printAnnotation(OS, Annot); 00046 00047 // If verbose assembly is enabled, we can print some informative comments. 00048 if (CommentStream) 00049 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName); 00050 } 00051 00052 void X86IntelInstPrinter::printSSECC(const MCInst *MI, unsigned Op, 00053 raw_ostream &O) { 00054 int64_t Imm = MI->getOperand(Op).getImm() & 0xf; 00055 switch (Imm) { 00056 default: llvm_unreachable("Invalid ssecc argument!"); 00057 case 0: O << "eq"; break; 00058 case 1: O << "lt"; break; 00059 case 2: O << "le"; break; 00060 case 3: O << "unord"; break; 00061 case 4: O << "neq"; break; 00062 case 5: O << "nlt"; break; 00063 case 6: O << "nle"; break; 00064 case 7: O << "ord"; break; 00065 case 8: O << "eq_uq"; break; 00066 case 9: O << "nge"; break; 00067 case 0xa: O << "ngt"; break; 00068 case 0xb: O << "false"; break; 00069 case 0xc: O << "neq_oq"; break; 00070 case 0xd: O << "ge"; break; 00071 case 0xe: O << "gt"; break; 00072 case 0xf: O << "true"; break; 00073 } 00074 } 00075 00076 void X86IntelInstPrinter::printAVXCC(const MCInst *MI, unsigned Op, 00077 raw_ostream &O) { 00078 int64_t Imm = MI->getOperand(Op).getImm() & 0x1f; 00079 switch (Imm) { 00080 default: llvm_unreachable("Invalid avxcc argument!"); 00081 case 0: O << "eq"; break; 00082 case 1: O << "lt"; break; 00083 case 2: O << "le"; break; 00084 case 3: O << "unord"; break; 00085 case 4: O << "neq"; break; 00086 case 5: O << "nlt"; break; 00087 case 6: O << "nle"; break; 00088 case 7: O << "ord"; break; 00089 case 8: O << "eq_uq"; break; 00090 case 9: O << "nge"; break; 00091 case 0xa: O << "ngt"; break; 00092 case 0xb: O << "false"; break; 00093 case 0xc: O << "neq_oq"; break; 00094 case 0xd: O << "ge"; break; 00095 case 0xe: O << "gt"; break; 00096 case 0xf: O << "true"; break; 00097 case 0x10: O << "eq_os"; break; 00098 case 0x11: O << "lt_oq"; break; 00099 case 0x12: O << "le_oq"; break; 00100 case 0x13: O << "unord_s"; break; 00101 case 0x14: O << "neq_us"; break; 00102 case 0x15: O << "nlt_uq"; break; 00103 case 0x16: O << "nle_uq"; break; 00104 case 0x17: O << "ord_s"; break; 00105 case 0x18: O << "eq_us"; break; 00106 case 0x19: O << "nge_uq"; break; 00107 case 0x1a: O << "ngt_uq"; break; 00108 case 0x1b: O << "false_os"; break; 00109 case 0x1c: O << "neq_os"; break; 00110 case 0x1d: O << "ge_oq"; break; 00111 case 0x1e: O << "gt_oq"; break; 00112 case 0x1f: O << "true_us"; break; 00113 } 00114 } 00115 00116 /// printPCRelImm - This is used to print an immediate value that ends up 00117 /// being encoded as a pc-relative value. 00118 void X86IntelInstPrinter::printPCRelImm(const MCInst *MI, unsigned OpNo, 00119 raw_ostream &O) { 00120 const MCOperand &Op = MI->getOperand(OpNo); 00121 if (Op.isImm()) 00122 O << Op.getImm(); 00123 else { 00124 assert(Op.isExpr() && "unknown pcrel immediate operand"); 00125 // If a symbolic branch target was added as a constant expression then print 00126 // that address in hex. 00127 const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr()); 00128 int64_t Address; 00129 if (BranchTarget && BranchTarget->EvaluateAsAbsolute(Address)) { 00130 O << "0x"; 00131 O.write_hex(Address); 00132 } 00133 else { 00134 // Otherwise, just print the expression. 00135 O << *Op.getExpr(); 00136 } 00137 } 00138 } 00139 00140 static void PrintRegName(raw_ostream &O, StringRef RegName) { 00141 for (unsigned i = 0, e = RegName.size(); i != e; ++i) 00142 O << (char)toupper(RegName[i]); 00143 } 00144 00145 void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, 00146 raw_ostream &O) { 00147 const MCOperand &Op = MI->getOperand(OpNo); 00148 if (Op.isReg()) { 00149 PrintRegName(O, getRegisterName(Op.getReg())); 00150 } else if (Op.isImm()) { 00151 O << Op.getImm(); 00152 } else { 00153 assert(Op.isExpr() && "unknown operand kind in printOperand"); 00154 O << *Op.getExpr(); 00155 } 00156 } 00157 00158 void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op, 00159 raw_ostream &O) { 00160 const MCOperand &BaseReg = MI->getOperand(Op); 00161 unsigned ScaleVal = MI->getOperand(Op+1).getImm(); 00162 const MCOperand &IndexReg = MI->getOperand(Op+2); 00163 const MCOperand &DispSpec = MI->getOperand(Op+3); 00164 const MCOperand &SegReg = MI->getOperand(Op+4); 00165 00166 // If this has a segment register, print it. 00167 if (SegReg.getReg()) { 00168 printOperand(MI, Op+4, O); 00169 O << ':'; 00170 } 00171 00172 O << '['; 00173 00174 bool NeedPlus = false; 00175 if (BaseReg.getReg()) { 00176 printOperand(MI, Op, O); 00177 NeedPlus = true; 00178 } 00179 00180 if (IndexReg.getReg()) { 00181 if (NeedPlus) O << " + "; 00182 if (ScaleVal != 1) 00183 O << ScaleVal << '*'; 00184 printOperand(MI, Op+2, O); 00185 NeedPlus = true; 00186 } 00187 00188 if (!DispSpec.isImm()) { 00189 if (NeedPlus) O << " + "; 00190 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?"); 00191 O << *DispSpec.getExpr(); 00192 } else { 00193 int64_t DispVal = DispSpec.getImm(); 00194 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) { 00195 if (NeedPlus) { 00196 if (DispVal > 0) 00197 O << " + "; 00198 else { 00199 O << " - "; 00200 DispVal = -DispVal; 00201 } 00202 } 00203 O << DispVal; 00204 } 00205 } 00206 00207 O << ']'; 00208 }