LLVM API Documentation

BlackfinAsmPrinter.cpp

Go to the documentation of this file.
00001 //===-- BlackfinAsmPrinter.cpp - Blackfin 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 BLACKFIN assembly language.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "asm-printer"
00016 #include "Blackfin.h"
00017 #include "BlackfinInstrInfo.h"
00018 #include "llvm/Constants.h"
00019 #include "llvm/DerivedTypes.h"
00020 #include "llvm/Module.h"
00021 #include "llvm/CodeGen/AsmPrinter.h"
00022 #include "llvm/CodeGen/DwarfWriter.h"
00023 #include "llvm/CodeGen/MachineFunctionPass.h"
00024 #include "llvm/CodeGen/MachineConstantPool.h"
00025 #include "llvm/CodeGen/MachineInstr.h"
00026 #include "llvm/MC/MCStreamer.h"
00027 #include "llvm/Target/TargetAsmInfo.h"
00028 #include "llvm/Target/TargetData.h"
00029 #include "llvm/Target/TargetLoweringObjectFile.h"
00030 #include "llvm/Target/TargetRegistry.h"
00031 #include "llvm/Support/Mangler.h"
00032 #include "llvm/ADT/Statistic.h"
00033 #include "llvm/Support/FormattedStream.h"
00034 
00035 using namespace llvm;
00036 
00037 STATISTIC(EmittedInsts, "Number of machine instrs printed");
00038 
00039 namespace {
00040   class VISIBILITY_HIDDEN BlackfinAsmPrinter : public AsmPrinter {
00041   public:
00042     BlackfinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
00043                        const TargetAsmInfo *TAI, bool V)
00044       : AsmPrinter(O, TM, TAI, V) {}
00045 
00046     virtual const char *getPassName() const {
00047       return "Blackfin Assembly Printer";
00048     }
00049 
00050     void printOperand(const MachineInstr *MI, int opNum);
00051     void printMemoryOperand(const MachineInstr *MI, int opNum);
00052     void printInstruction(const MachineInstr *MI);  // autogenerated.
00053     void emitLinkage(const std::string &n, GlobalValue::LinkageTypes l);
00054     bool runOnMachineFunction(MachineFunction &F);
00055     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
00056                          unsigned AsmVariant, const char *ExtraCode);
00057     bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
00058                                unsigned AsmVariant, const char *ExtraCode);
00059     void PrintGlobalVariable(const GlobalVariable* GVar);
00060   };
00061 } // end of anonymous namespace
00062 
00063 #include "BlackfinGenAsmWriter.inc"
00064 
00065 extern "C" void LLVMInitializeBlackfinAsmPrinter() {
00066   RegisterAsmPrinter<BlackfinAsmPrinter> X(TheBlackfinTarget);
00067 }
00068 
00069 void BlackfinAsmPrinter::emitLinkage(const std::string &name,
00070                                      GlobalValue::LinkageTypes l) {
00071   switch (l) {
00072   default: llvm_unreachable("Unknown linkage type!");
00073   case GlobalValue::InternalLinkage:  // Symbols default to internal.
00074   case GlobalValue::PrivateLinkage:
00075   case GlobalValue::LinkerPrivateLinkage:
00076     break;
00077   case GlobalValue::ExternalLinkage:
00078     O << TAI->getGlobalDirective() << name << "\n";
00079     break;
00080   case GlobalValue::LinkOnceAnyLinkage:
00081   case GlobalValue::LinkOnceODRLinkage:
00082   case GlobalValue::WeakAnyLinkage:
00083   case GlobalValue::WeakODRLinkage:
00084     O << TAI->getGlobalDirective() << name << "\n";
00085     O << TAI->getWeakDefDirective() << name << "\n";
00086     break;
00087   }
00088 }
00089 
00090 void BlackfinAsmPrinter::PrintGlobalVariable(const GlobalVariable* GV) {
00091   const TargetData *TD = TM.getTargetData();
00092 
00093   if (!GV->hasInitializer() || EmitSpecialLLVMGlobal(GV))
00094     return;
00095 
00096   std::string name = Mang->getMangledName(GV);
00097   Constant *C = GV->getInitializer();
00098 
00099   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,
00100                                                                   TM));
00101   emitLinkage(name, GV->getLinkage());
00102   EmitAlignment(TD->getPreferredAlignmentLog(GV), GV);
00103   printVisibility(name, GV->getVisibility());
00104 
00105   O << "\t.type " << name << ", STT_OBJECT\n";
00106   O << "\t.size " << name << ',' << TD->getTypeAllocSize(C->getType()) << '\n';
00107   O << name << ":\n";
00108   EmitGlobalConstant(C);
00109 }
00110 
00111 /// runOnMachineFunction - This uses the printInstruction()
00112 /// method to print assembly for each instruction.
00113 ///
00114 bool BlackfinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
00115   SetupMachineFunction(MF);
00116   EmitConstantPool(MF.getConstantPool());
00117   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
00118 
00119   const Function *F = MF.getFunction();
00120   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
00121   EmitAlignment(2, F);
00122   emitLinkage(CurrentFnName, F->getLinkage());
00123   printVisibility(CurrentFnName, F->getVisibility());
00124 
00125   O << "\t.type\t" << CurrentFnName << ", STT_FUNC\n"
00126     << CurrentFnName << ":\n";
00127 
00128   if (DW)
00129     DW->BeginFunction(&MF);
00130 
00131   // Print out code for the function.
00132   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
00133        I != E; ++I) {
00134     // Print a label for the basic block.
00135     if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
00136       // This is an entry block or a block that's only reachable via a
00137       // fallthrough edge. In non-VerboseAsm mode, don't print the label.
00138     } else {
00139       printBasicBlockLabel(I, true, true, VerboseAsm);
00140       O << '\n';
00141     }
00142 
00143     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
00144          II != E; ++II) {
00145       // Print the assembly for the instruction.
00146       printInstruction(II);
00147       ++EmittedInsts;
00148     }
00149   }
00150 
00151   O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
00152 
00153   if (DW)
00154     DW->EndFunction(&MF);
00155 
00156   return false;
00157 }
00158 
00159 void BlackfinAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
00160   const MachineOperand &MO = MI->getOperand (opNum);
00161   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
00162   switch (MO.getType()) {
00163   case MachineOperand::MO_Register:
00164     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
00165            "Virtual registers should be already mapped!");
00166     O << RI.get(MO.getReg()).AsmName;
00167     break;
00168 
00169   case MachineOperand::MO_Immediate:
00170     O << MO.getImm();
00171     break;
00172   case MachineOperand::MO_MachineBasicBlock:
00173     printBasicBlockLabel(MO.getMBB(), false, false, false);
00174     return;
00175   case MachineOperand::MO_GlobalAddress:
00176     O << Mang->getMangledName(MO.getGlobal());
00177     printOffset(MO.getOffset());
00178     break;
00179   case MachineOperand::MO_ExternalSymbol:
00180     O << Mang->makeNameProper(MO.getSymbolName());
00181     break;
00182   case MachineOperand::MO_ConstantPoolIndex:
00183     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
00184       << MO.getIndex();
00185     break;
00186   case MachineOperand::MO_JumpTableIndex:
00187     O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
00188       << '_' << MO.getIndex();
00189     break;
00190   default:
00191     llvm_unreachable("<unknown operand type>");
00192     break;
00193   }
00194 }
00195 
00196 void BlackfinAsmPrinter::printMemoryOperand(const MachineInstr *MI, int opNum) {
00197   printOperand(MI, opNum);
00198 
00199   if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
00200     return;
00201 
00202   O << " + ";
00203   printOperand(MI, opNum+1);
00204 }
00205 
00206 /// PrintAsmOperand - Print out an operand for an inline asm expression.
00207 ///
00208 bool BlackfinAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
00209                                          unsigned OpNo,
00210                                          unsigned AsmVariant,
00211                                          const char *ExtraCode) {
00212   if (ExtraCode && ExtraCode[0]) {
00213     if (ExtraCode[1] != 0) return true; // Unknown modifier.
00214 
00215     switch (ExtraCode[0]) {
00216     default: return true;  // Unknown modifier.
00217     case 'r':
00218       break;
00219     }
00220   }
00221 
00222   printOperand(MI, OpNo);
00223 
00224   return false;
00225 }
00226 
00227 bool BlackfinAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
00228                                                unsigned OpNo,
00229                                                unsigned AsmVariant,
00230                                                const char *ExtraCode) {
00231   if (ExtraCode && ExtraCode[0])
00232     return true;  // Unknown modifier
00233 
00234   O << '[';
00235   printOperand(MI, OpNo);
00236   O << ']';
00237 
00238   return false;
00239 }