LLVM API Documentation

X86ATTAsmPrinter.cpp

Go to the documentation of this file.
00001 //===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly ----===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains a printer that converts from our internal representation
00011 // of machine-dependent LLVM code to AT&T format assembly
00012 // language. This printer is the output mechanism used by `llc'.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "X86ATTAsmPrinter.h"
00017 #include "X86.h"
00018 #include "X86TargetMachine.h"
00019 #include "llvm/Module.h"
00020 #include "llvm/Support/Mangler.h"
00021 #include "llvm/Target/TargetOptions.h"
00022 #include <iostream>
00023 using namespace llvm;
00024 
00025 /// runOnMachineFunction - This uses the printMachineInstruction()
00026 /// method to print assembly for each instruction.
00027 ///
00028 bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
00029   //  if (forDarwin) {
00030     // Let PassManager know we need debug information and relay
00031     // the MachineDebugInfo address on to DwarfWriter.
00032     DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
00033     //  }
00034 
00035   SetupMachineFunction(MF);
00036   O << "\n\n";
00037 
00038   // Print out constants referenced by the function
00039   EmitConstantPool(MF.getConstantPool());
00040 
00041   // Print out labels for the function.
00042   const Function *F = MF.getFunction();
00043   switch (F->getLinkage()) {
00044   default: assert(0 && "Unknown linkage type!");
00045   case Function::InternalLinkage:  // Symbols default to internal.
00046     SwitchSection(".text", F);
00047     EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
00048     break;
00049   case Function::ExternalLinkage:
00050     SwitchSection(".text", F);
00051     EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
00052     O << "\t.globl\t" << CurrentFnName << "\n";
00053     break;
00054   case Function::WeakLinkage:
00055   case Function::LinkOnceLinkage:
00056     if (forDarwin) {
00057       SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
00058                     F);
00059       O << "\t.globl\t" << CurrentFnName << "\n";
00060       O << "\t.weak_definition\t" << CurrentFnName << "\n";
00061     } else {
00062       EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
00063       O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
00064         << ",\"ax\",@progbits\n";
00065       O << "\t.weak " << CurrentFnName << "\n";
00066     }
00067     break;
00068   }
00069   O << CurrentFnName << ":\n";
00070 
00071   if (forDarwin) {
00072     // Emit pre-function debug information.
00073     DW.BeginFunction(&MF);
00074   }
00075 
00076   // Print out code for the function.
00077   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
00078        I != E; ++I) {
00079     // Print a label for the basic block.
00080     if (I->pred_begin() != I->pred_end())
00081       O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
00082         << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
00083         << "\n";
00084     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
00085          II != E; ++II) {
00086       // Print the assembly for the instruction.
00087       O << "\t";
00088       printMachineInstruction(II);
00089     }
00090   }
00091   if (HasDotTypeDotSizeDirective)
00092     O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
00093 
00094   if (forDarwin) {
00095     // Emit post-function debug information.
00096     DW.EndFunction();
00097   }
00098 
00099   // We didn't modify anything.
00100   return false;
00101 }
00102 
00103 void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
00104                                     const char *Modifier) {
00105   const MachineOperand &MO = MI->getOperand(OpNo);
00106   const MRegisterInfo &RI = *TM.getRegisterInfo();
00107   switch (MO.getType()) {
00108   case MachineOperand::MO_VirtualRegister:
00109   case MachineOperand::MO_MachineRegister:
00110     assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
00111            "Virtual registers should not make it this far!");
00112     O << '%';
00113     for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
00114       O << (char)tolower(*Name);
00115     return;
00116 
00117   case MachineOperand::MO_SignExtendedImmed:
00118   case MachineOperand::MO_UnextendedImmed:
00119     if (!Modifier || strcmp(Modifier, "debug") != 0)
00120       O << '$';
00121     O << (int)MO.getImmedValue();
00122     return;
00123   case MachineOperand::MO_MachineBasicBlock: {
00124     MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
00125     O << PrivateGlobalPrefix << "BB"
00126       << Mang->getValueName(MBBOp->getParent()->getFunction())
00127       << "_" << MBBOp->getNumber () << "\t# "
00128       << MBBOp->getBasicBlock ()->getName ();
00129     return;
00130   }
00131   case MachineOperand::MO_PCRelativeDisp:
00132     std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
00133     abort ();
00134     return;
00135   case MachineOperand::MO_ConstantPoolIndex: {
00136     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
00137     if (!isMemOp) O << '$';
00138     O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
00139       << MO.getConstantPoolIndex();
00140     if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
00141       O << "-\"L" << getFunctionNumber() << "$pb\"";
00142     int Offset = MO.getOffset();
00143     if (Offset > 0)
00144       O << "+" << Offset;
00145     else if (Offset < 0)
00146       O << Offset;
00147     return;
00148   }
00149   case MachineOperand::MO_GlobalAddress: {
00150     bool isCallOp = Modifier && !strcmp(Modifier, "call");
00151     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
00152     if (!isMemOp && !isCallOp) O << '$';
00153     // Darwin block shameless ripped from PPCAsmPrinter.cpp
00154     if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
00155       GlobalValue *GV = MO.getGlobal();
00156       std::string Name = Mang->getValueName(GV);
00157       // Link-once, External, or Weakly-linked global variables need
00158       // non-lazily-resolved stubs
00159       if (GV->isExternal() || GV->hasWeakLinkage() ||
00160           GV->hasLinkOnceLinkage()) {
00161         // Dynamically-resolved functions need a stub for the function.
00162         if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
00163           FnStubs.insert(Name);
00164           O << "L" << Name << "$stub";
00165         } else {
00166           GVStubs.insert(Name);
00167           O << "L" << Name << "$non_lazy_ptr";
00168         }
00169       } else {
00170         O << Mang->getValueName(GV);
00171       } 
00172       if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
00173         O << "-\"L" << getFunctionNumber() << "$pb\"";
00174    } else
00175       O << Mang->getValueName(MO.getGlobal());
00176     int Offset = MO.getOffset();
00177     if (Offset > 0)
00178       O << "+" << Offset;
00179     else if (Offset < 0)
00180       O << Offset;
00181     return;
00182   }
00183   case MachineOperand::MO_ExternalSymbol: {
00184     bool isCallOp = Modifier && !strcmp(Modifier, "call");
00185     if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
00186       std::string Name(GlobalPrefix);
00187       Name += MO.getSymbolName();
00188       FnStubs.insert(Name);
00189       O << "L" << Name << "$stub";
00190       return;
00191     }
00192     if (!isCallOp) O << '$';
00193     O << GlobalPrefix << MO.getSymbolName();
00194     return;
00195   }
00196   default:
00197     O << "<unknown operand type>"; return;
00198   }
00199 }
00200 
00201 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
00202   unsigned char value = MI->getOperand(Op).getImmedValue();
00203   assert(value <= 7 && "Invalid ssecc argument!");
00204   switch (value) {
00205   case 0: O << "eq"; break;
00206   case 1: O << "lt"; break;
00207   case 2: O << "le"; break;
00208   case 3: O << "unord"; break;
00209   case 4: O << "neq"; break;
00210   case 5: O << "nlt"; break;
00211   case 6: O << "nle"; break;
00212   case 7: O << "ord"; break;
00213   }
00214 }
00215 
00216 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
00217   assert(isMem(MI, Op) && "Invalid memory reference!");
00218 
00219   const MachineOperand &BaseReg  = MI->getOperand(Op);
00220   int ScaleVal                   = MI->getOperand(Op+1).getImmedValue();
00221   const MachineOperand &IndexReg = MI->getOperand(Op+2);
00222   const MachineOperand &DispSpec = MI->getOperand(Op+3);
00223 
00224   if (BaseReg.isFrameIndex()) {
00225     O << "[frame slot #" << BaseReg.getFrameIndex();
00226     if (DispSpec.getImmedValue())
00227       O << " + " << DispSpec.getImmedValue();
00228     O << "]";
00229     return;
00230   }
00231 
00232   if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
00233     printOperand(MI, Op+3, "mem");
00234   } else {
00235     int DispVal = DispSpec.getImmedValue();
00236     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
00237       O << DispVal;
00238   }
00239 
00240   if (IndexReg.getReg() || BaseReg.getReg()) {
00241     O << "(";
00242     if (BaseReg.getReg())
00243       printOperand(MI, Op);
00244 
00245     if (IndexReg.getReg()) {
00246       O << ",";
00247       printOperand(MI, Op+2);
00248       if (ScaleVal != 1)
00249         O << "," << ScaleVal;
00250     }
00251 
00252     O << ")";
00253   }
00254 }
00255 
00256 void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
00257   O << "\"L" << getFunctionNumber() << "$pb\"\n";
00258   O << "\"L" << getFunctionNumber() << "$pb\":";
00259 }
00260 
00261 /// printMachineInstruction -- Print out a single X86 LLVM instruction
00262 /// MI in Intel syntax to the current output stream.
00263 ///
00264 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
00265   ++EmittedInsts;
00266   // This works around some Darwin assembler bugs.
00267   if (forDarwin) {
00268     switch (MI->getOpcode()) {
00269     case X86::REP_MOVSB:
00270       O << "rep/movsb (%esi),(%edi)\n";
00271       return;
00272     case X86::REP_MOVSD:
00273       O << "rep/movsl (%esi),(%edi)\n";
00274       return;
00275     case X86::REP_MOVSW:
00276       O << "rep/movsw (%esi),(%edi)\n";
00277       return;
00278     case X86::REP_STOSB:
00279       O << "rep/stosb\n";
00280       return;
00281     case X86::REP_STOSD:
00282       O << "rep/stosl\n";
00283       return;
00284     case X86::REP_STOSW:
00285       O << "rep/stosw\n";
00286       return;
00287     default:
00288       break;
00289     }
00290   }
00291 
00292   // Call the autogenerated instruction printer routines.
00293   printInstruction(MI);
00294 }
00295 
00296 // Include the auto-generated portion of the assembly writer.
00297 #include "X86GenAsmWriter.inc"
00298