LLVM API Documentation

MipsMCTargetDesc.cpp
Go to the documentation of this file.
00001 //===-- MipsMCTargetDesc.cpp - Mips Target Descriptions -------------------===//
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 provides Mips specific target descriptions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "InstPrinter/MipsInstPrinter.h"
00015 #include "MipsELFStreamer.h"
00016 #include "MipsMCAsmInfo.h"
00017 #include "MipsMCNaCl.h"
00018 #include "MipsMCTargetDesc.h"
00019 #include "MipsTargetStreamer.h"
00020 #include "llvm/ADT/Triple.h"
00021 #include "llvm/MC/MCCodeGenInfo.h"
00022 #include "llvm/MC/MCELFStreamer.h"
00023 #include "llvm/MC/MCInstrInfo.h"
00024 #include "llvm/MC/MCRegisterInfo.h"
00025 #include "llvm/MC/MCSubtargetInfo.h"
00026 #include "llvm/MC/MCSymbol.h"
00027 #include "llvm/MC/MachineLocation.h"
00028 #include "llvm/Support/CommandLine.h"
00029 #include "llvm/Support/ErrorHandling.h"
00030 #include "llvm/Support/FormattedStream.h"
00031 #include "llvm/Support/TargetRegistry.h"
00032 
00033 using namespace llvm;
00034 
00035 #define GET_INSTRINFO_MC_DESC
00036 #include "MipsGenInstrInfo.inc"
00037 
00038 #define GET_SUBTARGETINFO_MC_DESC
00039 #include "MipsGenSubtargetInfo.inc"
00040 
00041 #define GET_REGINFO_MC_DESC
00042 #include "MipsGenRegisterInfo.inc"
00043 
00044 /// Select the Mips CPU for the given triple and cpu name.
00045 /// FIXME: Merge with the copy in MipsSubtarget.cpp
00046 static inline StringRef selectMipsCPU(StringRef TT, StringRef CPU) {
00047   if (CPU.empty() || CPU == "generic") {
00048     Triple TheTriple(TT);
00049     if (TheTriple.getArch() == Triple::mips ||
00050         TheTriple.getArch() == Triple::mipsel)
00051       CPU = "mips32";
00052     else
00053       CPU = "mips64";
00054   }
00055   return CPU;
00056 }
00057 
00058 static MCInstrInfo *createMipsMCInstrInfo() {
00059   MCInstrInfo *X = new MCInstrInfo();
00060   InitMipsMCInstrInfo(X);
00061   return X;
00062 }
00063 
00064 static MCRegisterInfo *createMipsMCRegisterInfo(StringRef TT) {
00065   MCRegisterInfo *X = new MCRegisterInfo();
00066   InitMipsMCRegisterInfo(X, Mips::RA);
00067   return X;
00068 }
00069 
00070 static MCSubtargetInfo *createMipsMCSubtargetInfo(StringRef TT, StringRef CPU,
00071                                                   StringRef FS) {
00072   CPU = selectMipsCPU(TT, CPU);
00073   MCSubtargetInfo *X = new MCSubtargetInfo();
00074   InitMipsMCSubtargetInfo(X, TT, CPU, FS);
00075   return X;
00076 }
00077 
00078 static MCAsmInfo *createMipsMCAsmInfo(const MCRegisterInfo &MRI, StringRef TT) {
00079   MCAsmInfo *MAI = new MipsMCAsmInfo(TT);
00080 
00081   unsigned SP = MRI.getDwarfRegNum(Mips::SP, true);
00082   MCCFIInstruction Inst = MCCFIInstruction::createDefCfa(nullptr, SP, 0);
00083   MAI->addInitialFrameState(Inst);
00084 
00085   return MAI;
00086 }
00087 
00088 static MCCodeGenInfo *createMipsMCCodeGenInfo(StringRef TT, Reloc::Model RM,
00089                                               CodeModel::Model CM,
00090                                               CodeGenOpt::Level OL) {
00091   MCCodeGenInfo *X = new MCCodeGenInfo();
00092   if (CM == CodeModel::JITDefault)
00093     RM = Reloc::Static;
00094   else if (RM == Reloc::Default)
00095     RM = Reloc::PIC_;
00096   X->InitMCCodeGenInfo(RM, CM, OL);
00097   return X;
00098 }
00099 
00100 static MCInstPrinter *createMipsMCInstPrinter(const Target &T,
00101                                               unsigned SyntaxVariant,
00102                                               const MCAsmInfo &MAI,
00103                                               const MCInstrInfo &MII,
00104                                               const MCRegisterInfo &MRI,
00105                                               const MCSubtargetInfo &STI) {
00106   return new MipsInstPrinter(MAI, MII, MRI);
00107 }
00108 
00109 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
00110                                     MCContext &Context, MCAsmBackend &MAB,
00111                                     raw_ostream &OS, MCCodeEmitter *Emitter,
00112                                     const MCSubtargetInfo &STI,
00113                                     bool RelaxAll, bool NoExecStack) {
00114   MCStreamer *S;
00115   if (!Triple(TT).isOSNaCl())
00116     S = createMipsELFStreamer(Context, MAB, OS, Emitter, STI, RelaxAll,
00117                               NoExecStack);
00118   else
00119     S = createMipsNaClELFStreamer(Context, MAB, OS, Emitter, STI, RelaxAll,
00120                                   NoExecStack);
00121   new MipsTargetELFStreamer(*S, STI);
00122   return S;
00123 }
00124 
00125 static MCStreamer *
00126 createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
00127                     bool isVerboseAsm, bool useDwarfDirectory,
00128                     MCInstPrinter *InstPrint, MCCodeEmitter *CE,
00129                     MCAsmBackend *TAB, bool ShowInst) {
00130   MCStreamer *S = llvm::createAsmStreamer(
00131       Ctx, OS, isVerboseAsm, useDwarfDirectory, InstPrint, CE, TAB, ShowInst);
00132   new MipsTargetAsmStreamer(*S, OS);
00133   return S;
00134 }
00135 
00136 static MCStreamer *createMipsNullStreamer(MCContext &Ctx) {
00137   MCStreamer *S = llvm::createNullStreamer(Ctx);
00138   new MipsTargetStreamer(*S);
00139   return S;
00140 }
00141 
00142 extern "C" void LLVMInitializeMipsTargetMC() {
00143   // Register the MC asm info.
00144   RegisterMCAsmInfoFn X(TheMipsTarget, createMipsMCAsmInfo);
00145   RegisterMCAsmInfoFn Y(TheMipselTarget, createMipsMCAsmInfo);
00146   RegisterMCAsmInfoFn A(TheMips64Target, createMipsMCAsmInfo);
00147   RegisterMCAsmInfoFn B(TheMips64elTarget, createMipsMCAsmInfo);
00148 
00149   // Register the MC codegen info.
00150   TargetRegistry::RegisterMCCodeGenInfo(TheMipsTarget,
00151                                         createMipsMCCodeGenInfo);
00152   TargetRegistry::RegisterMCCodeGenInfo(TheMipselTarget,
00153                                         createMipsMCCodeGenInfo);
00154   TargetRegistry::RegisterMCCodeGenInfo(TheMips64Target,
00155                                         createMipsMCCodeGenInfo);
00156   TargetRegistry::RegisterMCCodeGenInfo(TheMips64elTarget,
00157                                         createMipsMCCodeGenInfo);
00158 
00159   // Register the MC instruction info.
00160   TargetRegistry::RegisterMCInstrInfo(TheMipsTarget, createMipsMCInstrInfo);
00161   TargetRegistry::RegisterMCInstrInfo(TheMipselTarget, createMipsMCInstrInfo);
00162   TargetRegistry::RegisterMCInstrInfo(TheMips64Target, createMipsMCInstrInfo);
00163   TargetRegistry::RegisterMCInstrInfo(TheMips64elTarget,
00164                                       createMipsMCInstrInfo);
00165 
00166   // Register the MC register info.
00167   TargetRegistry::RegisterMCRegInfo(TheMipsTarget, createMipsMCRegisterInfo);
00168   TargetRegistry::RegisterMCRegInfo(TheMipselTarget, createMipsMCRegisterInfo);
00169   TargetRegistry::RegisterMCRegInfo(TheMips64Target, createMipsMCRegisterInfo);
00170   TargetRegistry::RegisterMCRegInfo(TheMips64elTarget,
00171                                     createMipsMCRegisterInfo);
00172 
00173   // Register the MC Code Emitter
00174   TargetRegistry::RegisterMCCodeEmitter(TheMipsTarget,
00175                                         createMipsMCCodeEmitterEB);
00176   TargetRegistry::RegisterMCCodeEmitter(TheMipselTarget,
00177                                         createMipsMCCodeEmitterEL);
00178   TargetRegistry::RegisterMCCodeEmitter(TheMips64Target,
00179                                         createMipsMCCodeEmitterEB);
00180   TargetRegistry::RegisterMCCodeEmitter(TheMips64elTarget,
00181                                         createMipsMCCodeEmitterEL);
00182 
00183   // Register the object streamer.
00184   TargetRegistry::RegisterMCObjectStreamer(TheMipsTarget, createMCStreamer);
00185   TargetRegistry::RegisterMCObjectStreamer(TheMipselTarget, createMCStreamer);
00186   TargetRegistry::RegisterMCObjectStreamer(TheMips64Target, createMCStreamer);
00187   TargetRegistry::RegisterMCObjectStreamer(TheMips64elTarget,
00188                                            createMCStreamer);
00189 
00190   // Register the asm streamer.
00191   TargetRegistry::RegisterAsmStreamer(TheMipsTarget, createMCAsmStreamer);
00192   TargetRegistry::RegisterAsmStreamer(TheMipselTarget, createMCAsmStreamer);
00193   TargetRegistry::RegisterAsmStreamer(TheMips64Target, createMCAsmStreamer);
00194   TargetRegistry::RegisterAsmStreamer(TheMips64elTarget, createMCAsmStreamer);
00195 
00196   TargetRegistry::RegisterNullStreamer(TheMipsTarget, createMipsNullStreamer);
00197   TargetRegistry::RegisterNullStreamer(TheMipselTarget, createMipsNullStreamer);
00198   TargetRegistry::RegisterNullStreamer(TheMips64Target, createMipsNullStreamer);
00199   TargetRegistry::RegisterNullStreamer(TheMips64elTarget,
00200                                        createMipsNullStreamer);
00201 
00202   // Register the asm backend.
00203   TargetRegistry::RegisterMCAsmBackend(TheMipsTarget,
00204                                        createMipsAsmBackendEB32);
00205   TargetRegistry::RegisterMCAsmBackend(TheMipselTarget,
00206                                        createMipsAsmBackendEL32);
00207   TargetRegistry::RegisterMCAsmBackend(TheMips64Target,
00208                                        createMipsAsmBackendEB64);
00209   TargetRegistry::RegisterMCAsmBackend(TheMips64elTarget,
00210                                        createMipsAsmBackendEL64);
00211 
00212   // Register the MC subtarget info.
00213   TargetRegistry::RegisterMCSubtargetInfo(TheMipsTarget,
00214                                           createMipsMCSubtargetInfo);
00215   TargetRegistry::RegisterMCSubtargetInfo(TheMipselTarget,
00216                                           createMipsMCSubtargetInfo);
00217   TargetRegistry::RegisterMCSubtargetInfo(TheMips64Target,
00218                                           createMipsMCSubtargetInfo);
00219   TargetRegistry::RegisterMCSubtargetInfo(TheMips64elTarget,
00220                                           createMipsMCSubtargetInfo);
00221 
00222   // Register the MCInstPrinter.
00223   TargetRegistry::RegisterMCInstPrinter(TheMipsTarget,
00224                                         createMipsMCInstPrinter);
00225   TargetRegistry::RegisterMCInstPrinter(TheMipselTarget,
00226                                         createMipsMCInstPrinter);
00227   TargetRegistry::RegisterMCInstPrinter(TheMips64Target,
00228                                         createMipsMCInstPrinter);
00229   TargetRegistry::RegisterMCInstPrinter(TheMips64elTarget,
00230                                         createMipsMCInstPrinter);
00231 }