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