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 #include "X86IntelInstPrinter.h" 00016 #include "MCTargetDesc/X86BaseInfo.h" 00017 #include "MCTargetDesc/X86MCTargetDesc.h" 00018 #include "X86InstComments.h" 00019 #include "llvm/MC/MCExpr.h" 00020 #include "llvm/MC/MCInst.h" 00021 #include "llvm/MC/MCInstrInfo.h" 00022 #include "llvm/Support/ErrorHandling.h" 00023 #include "llvm/Support/FormattedStream.h" 00024 #include <cctype> 00025 using namespace llvm; 00026 00027 #define DEBUG_TYPE "asm-printer" 00028 00029 #include "X86GenAsmWriter1.inc" 00030 00031 void X86IntelInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const { 00032 OS << getRegisterName(RegNo); 00033 } 00034 00035 void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS, 00036 StringRef Annot) { 00037 const MCInstrDesc &Desc = MII.get(MI->getOpcode()); 00038 uint64_t TSFlags = Desc.TSFlags; 00039 00040 if (TSFlags & X86II::LOCK) 00041 OS << "\tlock\n"; 00042 00043 printInstruction(MI, OS); 00044 00045 // Next always print the annotation. 00046 printAnnotation(OS, Annot); 00047 00048 // If verbose assembly is enabled, we can print some informative comments. 00049 if (CommentStream) 00050 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName); 00051 } 00052 00053 void X86IntelInstPrinter::printSSECC(const MCInst *MI, unsigned Op, 00054 raw_ostream &O) { 00055 int64_t Imm = MI->getOperand(Op).getImm() & 0xf; 00056 switch (Imm) { 00057 default: llvm_unreachable("Invalid ssecc argument!"); 00058 case 0: O << "eq"; break; 00059 case 1: O << "lt"; break; 00060 case 2: O << "le"; break; 00061 case 3: O << "unord"; break; 00062 case 4: O << "neq"; break; 00063 case 5: O << "nlt"; break; 00064 case 6: O << "nle"; break; 00065 case 7: O << "ord"; break; 00066 case 8: O << "eq_uq"; break; 00067 case 9: O << "nge"; break; 00068 case 0xa: O << "ngt"; break; 00069 case 0xb: O << "false"; break; 00070 case 0xc: O << "neq_oq"; break; 00071 case 0xd: O << "ge"; break; 00072 case 0xe: O << "gt"; break; 00073 case 0xf: O << "true"; break; 00074 } 00075 } 00076 00077 void X86IntelInstPrinter::printAVXCC(const MCInst *MI, unsigned Op, 00078 raw_ostream &O) { 00079 int64_t Imm = MI->getOperand(Op).getImm() & 0x1f; 00080 switch (Imm) { 00081 default: llvm_unreachable("Invalid avxcc argument!"); 00082 case 0: O << "eq"; break; 00083 case 1: O << "lt"; break; 00084 case 2: O << "le"; break; 00085 case 3: O << "unord"; break; 00086 case 4: O << "neq"; break; 00087 case 5: O << "nlt"; break; 00088 case 6: O << "nle"; break; 00089 case 7: O << "ord"; break; 00090 case 8: O << "eq_uq"; break; 00091 case 9: O << "nge"; break; 00092 case 0xa: O << "ngt"; break; 00093 case 0xb: O << "false"; break; 00094 case 0xc: O << "neq_oq"; break; 00095 case 0xd: O << "ge"; break; 00096 case 0xe: O << "gt"; break; 00097 case 0xf: O << "true"; break; 00098 case 0x10: O << "eq_os"; break; 00099 case 0x11: O << "lt_oq"; break; 00100 case 0x12: O << "le_oq"; break; 00101 case 0x13: O << "unord_s"; break; 00102 case 0x14: O << "neq_us"; break; 00103 case 0x15: O << "nlt_uq"; break; 00104 case 0x16: O << "nle_uq"; break; 00105 case 0x17: O << "ord_s"; break; 00106 case 0x18: O << "eq_us"; break; 00107 case 0x19: O << "nge_uq"; break; 00108 case 0x1a: O << "ngt_uq"; break; 00109 case 0x1b: O << "false_os"; break; 00110 case 0x1c: O << "neq_os"; break; 00111 case 0x1d: O << "ge_oq"; break; 00112 case 0x1e: O << "gt_oq"; break; 00113 case 0x1f: O << "true_us"; break; 00114 } 00115 } 00116 00117 void X86IntelInstPrinter::printRoundingControl(const MCInst *MI, unsigned Op, 00118 raw_ostream &O) { 00119 int64_t Imm = MI->getOperand(Op).getImm() & 0x3; 00120 switch (Imm) { 00121 case 0: O << "{rn-sae}"; break; 00122 case 1: O << "{rd-sae}"; break; 00123 case 2: O << "{ru-sae}"; break; 00124 case 3: O << "{rz-sae}"; break; 00125 } 00126 } 00127 00128 /// printPCRelImm - This is used to print an immediate value that ends up 00129 /// being encoded as a pc-relative value. 00130 void X86IntelInstPrinter::printPCRelImm(const MCInst *MI, unsigned OpNo, 00131 raw_ostream &O) { 00132 const MCOperand &Op = MI->getOperand(OpNo); 00133 if (Op.isImm()) 00134 O << formatImm(Op.getImm()); 00135 else { 00136 assert(Op.isExpr() && "unknown pcrel immediate operand"); 00137 // If a symbolic branch target was added as a constant expression then print 00138 // that address in hex. 00139 const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr()); 00140 int64_t Address; 00141 if (BranchTarget && BranchTarget->EvaluateAsAbsolute(Address)) { 00142 O << formatHex((uint64_t)Address); 00143 } 00144 else { 00145 // Otherwise, just print the expression. 00146 O << *Op.getExpr(); 00147 } 00148 } 00149 } 00150 00151 void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, 00152 raw_ostream &O) { 00153 const MCOperand &Op = MI->getOperand(OpNo); 00154 if (Op.isReg()) { 00155 printRegName(O, Op.getReg()); 00156 } else if (Op.isImm()) { 00157 O << formatImm((int64_t)Op.getImm()); 00158 } else { 00159 assert(Op.isExpr() && "unknown operand kind in printOperand"); 00160 O << *Op.getExpr(); 00161 } 00162 } 00163 00164 void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op, 00165 raw_ostream &O) { 00166 const MCOperand &BaseReg = MI->getOperand(Op+X86::AddrBaseReg); 00167 unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm(); 00168 const MCOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg); 00169 const MCOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp); 00170 const MCOperand &SegReg = MI->getOperand(Op+X86::AddrSegmentReg); 00171 00172 // If this has a segment register, print it. 00173 if (SegReg.getReg()) { 00174 printOperand(MI, Op+X86::AddrSegmentReg, O); 00175 O << ':'; 00176 } 00177 00178 O << '['; 00179 00180 bool NeedPlus = false; 00181 if (BaseReg.getReg()) { 00182 printOperand(MI, Op+X86::AddrBaseReg, O); 00183 NeedPlus = true; 00184 } 00185 00186 if (IndexReg.getReg()) { 00187 if (NeedPlus) O << " + "; 00188 if (ScaleVal != 1) 00189 O << ScaleVal << '*'; 00190 printOperand(MI, Op+X86::AddrIndexReg, O); 00191 NeedPlus = true; 00192 } 00193 00194 if (!DispSpec.isImm()) { 00195 if (NeedPlus) O << " + "; 00196 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?"); 00197 O << *DispSpec.getExpr(); 00198 } else { 00199 int64_t DispVal = DispSpec.getImm(); 00200 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) { 00201 if (NeedPlus) { 00202 if (DispVal > 0) 00203 O << " + "; 00204 else { 00205 O << " - "; 00206 DispVal = -DispVal; 00207 } 00208 } 00209 O << formatImm(DispVal); 00210 } 00211 } 00212 00213 O << ']'; 00214 } 00215 00216 void X86IntelInstPrinter::printSrcIdx(const MCInst *MI, unsigned Op, 00217 raw_ostream &O) { 00218 const MCOperand &SegReg = MI->getOperand(Op+1); 00219 00220 // If this has a segment register, print it. 00221 if (SegReg.getReg()) { 00222 printOperand(MI, Op+1, O); 00223 O << ':'; 00224 } 00225 O << '['; 00226 printOperand(MI, Op, O); 00227 O << ']'; 00228 } 00229 00230 void X86IntelInstPrinter::printDstIdx(const MCInst *MI, unsigned Op, 00231 raw_ostream &O) { 00232 // DI accesses are always ES-based. 00233 O << "es:["; 00234 printOperand(MI, Op, O); 00235 O << ']'; 00236 } 00237 00238 void X86IntelInstPrinter::printMemOffset(const MCInst *MI, unsigned Op, 00239 raw_ostream &O) { 00240 const MCOperand &DispSpec = MI->getOperand(Op); 00241 const MCOperand &SegReg = MI->getOperand(Op+1); 00242 00243 // If this has a segment register, print it. 00244 if (SegReg.getReg()) { 00245 printOperand(MI, Op+1, O); 00246 O << ':'; 00247 } 00248 00249 O << '['; 00250 00251 if (DispSpec.isImm()) { 00252 O << formatImm(DispSpec.getImm()); 00253 } else { 00254 assert(DispSpec.isExpr() && "non-immediate displacement?"); 00255 O << *DispSpec.getExpr(); 00256 } 00257 00258 O << ']'; 00259 }