LLVM API Documentation
00001 //===-- X86ATTInstPrinter.cpp - AT&T 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 AT&T-style 00011 // assembly. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "asm-printer" 00016 #include "llvm/MC/MCInst.h" 00017 #include "X86ATTAsmPrinter.h" 00018 #include "llvm/Target/TargetAsmInfo.h" 00019 #include "llvm/Support/ErrorHandling.h" 00020 #include "llvm/Support/FormattedStream.h" 00021 using namespace llvm; 00022 00023 // Include the auto-generated portion of the assembly writer. 00024 #define MachineInstr MCInst 00025 #define NO_ASM_WRITER_BOILERPLATE 00026 #include "X86GenAsmWriter.inc" 00027 #undef MachineInstr 00028 00029 void X86ATTAsmPrinter::printSSECC(const MCInst *MI, unsigned Op) { 00030 switch (MI->getOperand(Op).getImm()) { 00031 default: llvm_unreachable("Invalid ssecc argument!"); 00032 case 0: O << "eq"; break; 00033 case 1: O << "lt"; break; 00034 case 2: O << "le"; break; 00035 case 3: O << "unord"; break; 00036 case 4: O << "neq"; break; 00037 case 5: O << "nlt"; break; 00038 case 6: O << "nle"; break; 00039 case 7: O << "ord"; break; 00040 } 00041 } 00042 00043 00044 void X86ATTAsmPrinter::printPICLabel(const MCInst *MI, unsigned Op) { 00045 llvm_unreachable("This is only used for MOVPC32r," 00046 "should lower before asm printing!"); 00047 } 00048 00049 00050 /// print_pcrel_imm - This is used to print an immediate value that ends up 00051 /// being encoded as a pc-relative value. These print slightly differently, for 00052 /// example, a $ is not emitted. 00053 void X86ATTAsmPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) { 00054 const MCOperand &Op = MI->getOperand(OpNo); 00055 00056 if (Op.isImm()) 00057 O << Op.getImm(); 00058 else if (Op.isMCValue()) 00059 Op.getMCValue().print(O); 00060 else if (Op.isMBBLabel()) 00061 // FIXME: Keep in sync with printBasicBlockLabel. printBasicBlockLabel 00062 // should eventually call into this code, not the other way around. 00063 O << TAI->getPrivateGlobalPrefix() << "BB" << Op.getMBBLabelFunction() 00064 << '_' << Op.getMBBLabelBlock(); 00065 else 00066 llvm_unreachable("Unknown pcrel immediate operand"); 00067 } 00068 00069 00070 void X86ATTAsmPrinter::printOperand(const MCInst *MI, unsigned OpNo, 00071 const char *Modifier) { 00072 assert(Modifier == 0 && "Modifiers should not be used"); 00073 00074 const MCOperand &Op = MI->getOperand(OpNo); 00075 if (Op.isReg()) { 00076 O << '%'; 00077 unsigned Reg = Op.getReg(); 00078 #if 0 00079 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) { 00080 EVT VT = (strcmp(Modifier+6,"64") == 0) ? 00081 EVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? EVT::i32 : 00082 ((strcmp(Modifier+6,"16") == 0) ? EVT::i16 : EVT::i8)); 00083 Reg = getX86SubSuperRegister(Reg, VT); 00084 } 00085 #endif 00086 O << TRI->getAsmName(Reg); 00087 return; 00088 } else if (Op.isImm()) { 00089 //if (!Modifier || (strcmp(Modifier, "debug") && strcmp(Modifier, "mem"))) 00090 O << '$'; 00091 O << Op.getImm(); 00092 return; 00093 } else if (Op.isMCValue()) { 00094 O << '$'; 00095 Op.getMCValue().print(O); 00096 return; 00097 } 00098 00099 O << "<<UNKNOWN OPERAND KIND>>"; 00100 } 00101 00102 void X86ATTAsmPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) { 00103 00104 const MCOperand &BaseReg = MI->getOperand(Op); 00105 const MCOperand &IndexReg = MI->getOperand(Op+2); 00106 const MCOperand &DispSpec = MI->getOperand(Op+3); 00107 00108 if (DispSpec.isImm()) { 00109 int64_t DispVal = DispSpec.getImm(); 00110 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) 00111 O << DispVal; 00112 } else if (DispSpec.isMCValue()) { 00113 DispSpec.getMCValue().print(O); 00114 } else { 00115 llvm_unreachable("non-immediate displacement for LEA?"); 00116 //assert(DispSpec.isGlobal() || DispSpec.isCPI() || 00117 // DispSpec.isJTI() || DispSpec.isSymbol()); 00118 //printOperand(MI, Op+3, "mem"); 00119 } 00120 00121 if (IndexReg.getReg() || BaseReg.getReg()) { 00122 // There are cases where we can end up with ESP/RSP in the indexreg slot. 00123 // If this happens, swap the base/index register to support assemblers that 00124 // don't work when the index is *SP. 00125 // FIXME: REMOVE THIS. 00126 assert(IndexReg.getReg() != X86::ESP && IndexReg.getReg() != X86::RSP); 00127 00128 O << '('; 00129 if (BaseReg.getReg()) 00130 printOperand(MI, Op); 00131 00132 if (IndexReg.getReg()) { 00133 O << ','; 00134 printOperand(MI, Op+2); 00135 unsigned ScaleVal = MI->getOperand(Op+1).getImm(); 00136 if (ScaleVal != 1) 00137 O << ',' << ScaleVal; 00138 } 00139 O << ')'; 00140 } 00141 } 00142 00143 void X86ATTAsmPrinter::printMemReference(const MCInst *MI, unsigned Op) { 00144 const MCOperand &Segment = MI->getOperand(Op+4); 00145 if (Segment.getReg()) { 00146 printOperand(MI, Op+4); 00147 O << ':'; 00148 } 00149 printLeaMemReference(MI, Op); 00150 }