LLVM API Documentation

MSP430AsmPrinter.cpp

Go to the documentation of this file.
00001 //===-- MSP430AsmPrinter.cpp - MSP430 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 the MSP430 assembly language.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "asm-printer"
00016 #include "MSP430.h"
00017 #include "MSP430InstrInfo.h"
00018 #include "MSP430TargetAsmInfo.h"
00019 #include "MSP430TargetMachine.h"
00020 #include "llvm/Constants.h"
00021 #include "llvm/DerivedTypes.h"
00022 #include "llvm/Module.h"
00023 #include "llvm/CodeGen/AsmPrinter.h"
00024 #include "llvm/CodeGen/DwarfWriter.h"
00025 #include "llvm/CodeGen/MachineModuleInfo.h"
00026 #include "llvm/CodeGen/MachineFunctionPass.h"
00027 #include "llvm/CodeGen/MachineConstantPool.h"
00028 #include "llvm/CodeGen/MachineInstr.h"
00029 #include "llvm/MC/MCStreamer.h"
00030 #include "llvm/Target/TargetData.h"
00031 #include "llvm/Target/TargetLoweringObjectFile.h"
00032 #include "llvm/Target/TargetRegistry.h"
00033 #include "llvm/ADT/Statistic.h"
00034 #include "llvm/Support/Compiler.h"
00035 #include "llvm/Support/FormattedStream.h"
00036 #include "llvm/Support/Mangler.h"
00037 #include "llvm/Support/ErrorHandling.h"
00038 
00039 using namespace llvm;
00040 
00041 STATISTIC(EmittedInsts, "Number of machine instrs printed");
00042 
00043 namespace {
00044   class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
00045   public:
00046     MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
00047                      const TargetAsmInfo *TAI, bool V)
00048       : AsmPrinter(O, TM, TAI, V) {}
00049 
00050     virtual const char *getPassName() const {
00051       return "MSP430 Assembly Printer";
00052     }
00053 
00054     void printOperand(const MachineInstr *MI, int OpNum,
00055                       const char* Modifier = 0);
00056     void printSrcMemOperand(const MachineInstr *MI, int OpNum,
00057                             const char* Modifier = 0);
00058     void printCCOperand(const MachineInstr *MI, int OpNum);
00059     void printInstruction(const MachineInstr *MI);  // autogenerated.
00060     void printMachineInstruction(const MachineInstr * MI);
00061 
00062     void emitFunctionHeader(const MachineFunction &MF);
00063     bool runOnMachineFunction(MachineFunction &F);
00064 
00065     virtual void PrintGlobalVariable(const GlobalVariable *GV) {
00066       // FIXME: No support for global variables?
00067     }
00068 
00069     void getAnalysisUsage(AnalysisUsage &AU) const {
00070       AsmPrinter::getAnalysisUsage(AU);
00071       AU.setPreservesAll();
00072     }
00073   };
00074 } // end of anonymous namespace
00075 
00076 #include "MSP430GenAsmWriter.inc"
00077 
00078 
00079 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
00080   const Function *F = MF.getFunction();
00081 
00082   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
00083 
00084   unsigned FnAlign = MF.getAlignment();
00085   EmitAlignment(FnAlign, F);
00086 
00087   switch (F->getLinkage()) {
00088   default: llvm_unreachable("Unknown linkage type!");
00089   case Function::InternalLinkage:  // Symbols default to internal.
00090   case Function::PrivateLinkage:
00091   case Function::LinkerPrivateLinkage:
00092     break;
00093   case Function::ExternalLinkage:
00094     O << "\t.globl\t" << CurrentFnName << '\n';
00095     break;
00096   case Function::LinkOnceAnyLinkage:
00097   case Function::LinkOnceODRLinkage:
00098   case Function::WeakAnyLinkage:
00099   case Function::WeakODRLinkage:
00100     O << "\t.weak\t" << CurrentFnName << '\n';
00101     break;
00102   }
00103 
00104   printVisibility(CurrentFnName, F->getVisibility());
00105 
00106   O << "\t.type\t" << CurrentFnName << ",@function\n"
00107     << CurrentFnName << ":\n";
00108 }
00109 
00110 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
00111   SetupMachineFunction(MF);
00112   O << "\n\n";
00113 
00114   // Print the 'header' of function
00115   emitFunctionHeader(MF);
00116 
00117   // Print out code for the function.
00118   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
00119        I != E; ++I) {
00120     // Print a label for the basic block.
00121     if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
00122       // This is an entry block or a block that's only reachable via a
00123       // fallthrough edge. In non-VerboseAsm mode, don't print the label.
00124     } else {
00125       printBasicBlockLabel(I, true, true, VerboseAsm);
00126       O << '\n';
00127     }
00128 
00129     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
00130          II != E; ++II)
00131       // Print the assembly for the instruction.
00132       printMachineInstruction(II);
00133   }
00134 
00135   if (TAI->hasDotTypeDotSizeDirective())
00136     O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
00137 
00138   // We didn't modify anything
00139   return false;
00140 }
00141 
00142 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
00143   ++EmittedInsts;
00144 
00145   // Call the autogenerated instruction printer routines.
00146   printInstruction(MI);
00147 }
00148 
00149 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
00150                                     const char* Modifier) {
00151   const MachineOperand &MO = MI->getOperand(OpNum);
00152   switch (MO.getType()) {
00153   case MachineOperand::MO_Register:
00154     assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
00155             "Virtual registers should be already mapped!");
00156     O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
00157     return;
00158   case MachineOperand::MO_Immediate:
00159     if (!Modifier || strcmp(Modifier, "nohash"))
00160       O << '#';
00161     O << MO.getImm();
00162     return;
00163   case MachineOperand::MO_MachineBasicBlock:
00164     printBasicBlockLabel(MO.getMBB());
00165     return;
00166   case MachineOperand::MO_GlobalAddress: {
00167     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
00168     bool isCallOp = Modifier && !strcmp(Modifier, "call");
00169     std::string Name = Mang->getMangledName(MO.getGlobal());
00170     assert(MO.getOffset() == 0 && "No offsets allowed!");
00171 
00172     if (isCallOp)
00173       O << '#';
00174     else if (isMemOp)
00175       O << '&';
00176 
00177     O << Name;
00178 
00179     return;
00180   }
00181   case MachineOperand::MO_ExternalSymbol: {
00182     bool isCallOp = Modifier && !strcmp(Modifier, "call");
00183     std::string Name(TAI->getGlobalPrefix());
00184     Name += MO.getSymbolName();
00185     if (isCallOp)
00186       O << '#';
00187     O << Name;
00188     return;
00189   }
00190   default:
00191     llvm_unreachable("Not implemented yet!");
00192   }
00193 }
00194 
00195 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
00196                                           const char* Modifier) {
00197   const MachineOperand &Base = MI->getOperand(OpNum);
00198   const MachineOperand &Disp = MI->getOperand(OpNum+1);
00199 
00200   if (Base.isGlobal())
00201     printOperand(MI, OpNum, "mem");
00202   else if (Disp.isImm() && !Base.getReg())
00203     printOperand(MI, OpNum);
00204   else if (Base.getReg()) {
00205     if (Disp.getImm()) {
00206       printOperand(MI, OpNum + 1, "nohash");
00207       O << '(';
00208       printOperand(MI, OpNum);
00209       O << ')';
00210     } else {
00211       O << '@';
00212       printOperand(MI, OpNum);
00213     }
00214   } else
00215     llvm_unreachable("Unsupported memory operand");
00216 }
00217 
00218 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
00219   unsigned CC = MI->getOperand(OpNum).getImm();
00220 
00221   switch (CC) {
00222   default:
00223    llvm_unreachable("Unsupported CC code");
00224    break;
00225   case MSP430::COND_E:
00226    O << "eq";
00227    break;
00228   case MSP430::COND_NE:
00229    O << "ne";
00230    break;
00231   case MSP430::COND_HS:
00232    O << "hs";
00233    break;
00234   case MSP430::COND_LO:
00235    O << "lo";
00236    break;
00237   case MSP430::COND_GE:
00238    O << "ge";
00239    break;
00240   case MSP430::COND_L:
00241    O << 'l';
00242    break;
00243   }
00244 }
00245 
00246 // Force static initialization.
00247 extern "C" void LLVMInitializeMSP430AsmPrinter() {
00248   RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
00249 }