LLVM API Documentation

AlphaAsmPrinter.cpp

Go to the documentation of this file.
00001 //===-- AlphaAsmPrinter.cpp - Alpha LLVM assembly writer ------------------===//
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 contains a printer that converts from our internal representation
00011 // of machine-dependent LLVM code to GAS-format Alpha assembly language.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "asm-printer"
00016 #include "Alpha.h"
00017 #include "AlphaInstrInfo.h"
00018 #include "AlphaTargetMachine.h"
00019 #include "llvm/Module.h"
00020 #include "llvm/Type.h"
00021 #include "llvm/Assembly/Writer.h"
00022 #include "llvm/CodeGen/AsmPrinter.h"
00023 #include "llvm/CodeGen/DwarfWriter.h"
00024 #include "llvm/MC/MCStreamer.h"
00025 #include "llvm/Target/TargetAsmInfo.h"
00026 #include "llvm/Target/TargetLoweringObjectFile.h"
00027 #include "llvm/Target/TargetMachine.h"
00028 #include "llvm/Target/TargetRegistry.h"
00029 #include "llvm/Support/Compiler.h"
00030 #include "llvm/Support/ErrorHandling.h"
00031 #include "llvm/Support/Mangler.h"
00032 #include "llvm/Support/FormattedStream.h"
00033 #include "llvm/ADT/Statistic.h"
00034 using namespace llvm;
00035 
00036 STATISTIC(EmittedInsts, "Number of machine instrs printed");
00037 
00038 namespace {
00039   struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
00040     /// Unique incrementer for label values for referencing Global values.
00041     ///
00042 
00043     explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
00044                              const TargetAsmInfo *T, bool V)
00045       : AsmPrinter(o, tm, T, V) {}
00046 
00047     virtual const char *getPassName() const {
00048       return "Alpha Assembly Printer";
00049     }
00050     void printInstruction(const MachineInstr *MI);
00051     void printOp(const MachineOperand &MO, bool IsCallOp = false);
00052     void printOperand(const MachineInstr *MI, int opNum);
00053     void printBaseOffsetPair(const MachineInstr *MI, int i, bool brackets=true);
00054     void PrintGlobalVariable(const GlobalVariable *GVar);
00055     bool runOnMachineFunction(MachineFunction &F);
00056     bool doInitialization(Module &M);
00057 
00058     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
00059                          unsigned AsmVariant, const char *ExtraCode);
00060     bool PrintAsmMemoryOperand(const MachineInstr *MI,
00061                                unsigned OpNo,
00062                                unsigned AsmVariant,
00063                                const char *ExtraCode);
00064   };
00065 } // end of anonymous namespace
00066 
00067 #include "AlphaGenAsmWriter.inc"
00068 
00069 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
00070 {
00071   const MachineOperand &MO = MI->getOperand(opNum);
00072   if (MO.getType() == MachineOperand::MO_Register) {
00073     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
00074            "Not physreg??");
00075     O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
00076   } else if (MO.isImm()) {
00077     O << MO.getImm();
00078     assert(MO.getImm() < (1 << 30));
00079   } else {
00080     printOp(MO);
00081   }
00082 }
00083 
00084 
00085 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
00086   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
00087 
00088   switch (MO.getType()) {
00089   case MachineOperand::MO_Register:
00090     O << RI.get(MO.getReg()).AsmName;
00091     return;
00092 
00093   case MachineOperand::MO_Immediate:
00094     llvm_unreachable("printOp() does not handle immediate values");
00095     return;
00096 
00097   case MachineOperand::MO_MachineBasicBlock:
00098     printBasicBlockLabel(MO.getMBB());
00099     return;
00100 
00101   case MachineOperand::MO_ConstantPoolIndex:
00102     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
00103       << MO.getIndex();
00104     return;
00105 
00106   case MachineOperand::MO_ExternalSymbol:
00107     O << MO.getSymbolName();
00108     return;
00109 
00110   case MachineOperand::MO_GlobalAddress:
00111     O << Mang->getMangledName(MO.getGlobal());
00112     return;
00113 
00114   case MachineOperand::MO_JumpTableIndex:
00115     O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
00116       << '_' << MO.getIndex();
00117     return;
00118 
00119   default:
00120     O << "<unknown operand type: " << MO.getType() << ">";
00121     return;
00122   }
00123 }
00124 
00125 /// runOnMachineFunction - This uses the printMachineInstruction()
00126 /// method to print assembly for each instruction.
00127 ///
00128 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
00129   this->MF = &MF;
00130 
00131   SetupMachineFunction(MF);
00132   O << "\n\n";
00133 
00134   // Print out constants referenced by the function
00135   EmitConstantPool(MF.getConstantPool());
00136 
00137   // Print out jump tables referenced by the function
00138   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
00139 
00140   // Print out labels for the function.
00141   const Function *F = MF.getFunction();
00142   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
00143 
00144   EmitAlignment(MF.getAlignment(), F);
00145   switch (F->getLinkage()) {
00146   default: llvm_unreachable("Unknown linkage type!");
00147   case Function::InternalLinkage:  // Symbols default to internal.
00148   case Function::PrivateLinkage:
00149   case Function::LinkerPrivateLinkage:
00150     break;
00151    case Function::ExternalLinkage:
00152      O << "\t.globl " << CurrentFnName << "\n";
00153      break;
00154   case Function::WeakAnyLinkage:
00155   case Function::WeakODRLinkage:
00156   case Function::LinkOnceAnyLinkage:
00157   case Function::LinkOnceODRLinkage:
00158     O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
00159     break;
00160   }
00161 
00162   printVisibility(CurrentFnName, F->getVisibility());
00163 
00164   O << "\t.ent " << CurrentFnName << "\n";
00165 
00166   O << CurrentFnName << ":\n";
00167 
00168   // Print out code for the function.
00169   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
00170        I != E; ++I) {
00171     if (I != MF.begin()) {
00172       printBasicBlockLabel(I, true, true);
00173       O << '\n';
00174     }
00175     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
00176          II != E; ++II) {
00177       // Print the assembly for the instruction.
00178       ++EmittedInsts;
00179       printInstruction(II);
00180     }
00181   }
00182 
00183   O << "\t.end " << CurrentFnName << "\n";
00184 
00185   // We didn't modify anything.
00186   return false;
00187 }
00188 
00189 bool AlphaAsmPrinter::doInitialization(Module &M)
00190 {
00191   if(TM.getSubtarget<AlphaSubtarget>().hasCT())
00192     O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
00193   else
00194     O << "\t.arch ev6\n";
00195   O << "\t.set noat\n";
00196   return AsmPrinter::doInitialization(M);
00197 }
00198 
00199 void AlphaAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
00200   const TargetData *TD = TM.getTargetData();
00201 
00202   if (!GVar->hasInitializer()) return;  // External global require no code
00203 
00204   // Check to see if this is a special global used by LLVM, if so, emit it.
00205   if (EmitSpecialLLVMGlobal(GVar))
00206     return;
00207 
00208   std::string name = Mang->getMangledName(GVar);
00209   Constant *C = GVar->getInitializer();
00210   unsigned Size = TD->getTypeAllocSize(C->getType());
00211   unsigned Align = TD->getPreferredAlignmentLog(GVar);
00212 
00213   // 0: Switch to section
00214   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
00215                                                                   TM));
00216 
00217   // 1: Check visibility
00218   printVisibility(name, GVar->getVisibility());
00219 
00220   // 2: Kind
00221   switch (GVar->getLinkage()) {
00222    case GlobalValue::LinkOnceAnyLinkage:
00223    case GlobalValue::LinkOnceODRLinkage:
00224    case GlobalValue::WeakAnyLinkage:
00225    case GlobalValue::WeakODRLinkage:
00226    case GlobalValue::CommonLinkage:
00227     O << TAI->getWeakRefDirective() << name << '\n';
00228     break;
00229    case GlobalValue::AppendingLinkage:
00230    case GlobalValue::ExternalLinkage:
00231       O << TAI->getGlobalDirective() << name << "\n";
00232       break;
00233     case GlobalValue::InternalLinkage:
00234     case GlobalValue::PrivateLinkage:
00235     case GlobalValue::LinkerPrivateLinkage:
00236       break;
00237     default:
00238       llvm_unreachable("Unknown linkage type!");
00239     }
00240 
00241   // 3: Type, Size, Align
00242   if (TAI->hasDotTypeDotSizeDirective()) {
00243     O << "\t.type\t" << name << ", @object\n";
00244     O << "\t.size\t" << name << ", " << Size << "\n";
00245   }
00246 
00247   EmitAlignment(Align, GVar);
00248 
00249   O << name << ":\n";
00250 
00251   EmitGlobalConstant(C);
00252   O << '\n';
00253 }
00254 
00255 /// PrintAsmOperand - Print out an operand for an inline asm expression.
00256 ///
00257 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
00258                                       unsigned AsmVariant,
00259                                       const char *ExtraCode) {
00260   printOperand(MI, OpNo);
00261   return false;
00262 }
00263 
00264 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
00265                                             unsigned OpNo,
00266                                             unsigned AsmVariant,
00267                                             const char *ExtraCode) {
00268   if (ExtraCode && ExtraCode[0])
00269     return true; // Unknown modifier.
00270   O << "0(";
00271   printOperand(MI, OpNo);
00272   O << ")";
00273   return false;
00274 }
00275 
00276 // Force static initialization.
00277 extern "C" void LLVMInitializeAlphaAsmPrinter() { 
00278   RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);
00279 }