LLVM API Documentation

PPCMCTargetDesc.cpp
Go to the documentation of this file.
00001 //===-- PPCMCTargetDesc.cpp - PowerPC 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 PowerPC specific target descriptions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "PPCMCTargetDesc.h"
00015 #include "InstPrinter/PPCInstPrinter.h"
00016 #include "PPCMCAsmInfo.h"
00017 #include "PPCTargetStreamer.h"
00018 #include "llvm/MC/MCCodeGenInfo.h"
00019 #include "llvm/MC/MCELF.h"
00020 #include "llvm/MC/MCELFStreamer.h"
00021 #include "llvm/MC/MCExpr.h"
00022 #include "llvm/MC/MCInstrInfo.h"
00023 #include "llvm/MC/MCRegisterInfo.h"
00024 #include "llvm/MC/MCStreamer.h"
00025 #include "llvm/MC/MCSubtargetInfo.h"
00026 #include "llvm/MC/MCSymbol.h"
00027 #include "llvm/MC/MachineLocation.h"
00028 #include "llvm/Support/ELF.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 "PPCGenInstrInfo.inc"
00037 
00038 #define GET_SUBTARGETINFO_MC_DESC
00039 #include "PPCGenSubtargetInfo.inc"
00040 
00041 #define GET_REGINFO_MC_DESC
00042 #include "PPCGenRegisterInfo.inc"
00043 
00044 // Pin the vtable to this file.
00045 PPCTargetStreamer::~PPCTargetStreamer() {}
00046 PPCTargetStreamer::PPCTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
00047 
00048 static MCInstrInfo *createPPCMCInstrInfo() {
00049   MCInstrInfo *X = new MCInstrInfo();
00050   InitPPCMCInstrInfo(X);
00051   return X;
00052 }
00053 
00054 static MCRegisterInfo *createPPCMCRegisterInfo(StringRef TT) {
00055   Triple TheTriple(TT);
00056   bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
00057                   TheTriple.getArch() == Triple::ppc64le);
00058   unsigned Flavour = isPPC64 ? 0 : 1;
00059   unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
00060 
00061   MCRegisterInfo *X = new MCRegisterInfo();
00062   InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
00063   return X;
00064 }
00065 
00066 static MCSubtargetInfo *createPPCMCSubtargetInfo(StringRef TT, StringRef CPU,
00067                                                  StringRef FS) {
00068   MCSubtargetInfo *X = new MCSubtargetInfo();
00069   InitPPCMCSubtargetInfo(X, TT, CPU, FS);
00070   return X;
00071 }
00072 
00073 static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI, StringRef TT) {
00074   Triple TheTriple(TT);
00075   bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
00076                   TheTriple.getArch() == Triple::ppc64le);
00077 
00078   MCAsmInfo *MAI;
00079   if (TheTriple.isOSDarwin())
00080     MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple);
00081   else
00082     MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
00083 
00084   // Initial state of the frame pointer is R1.
00085   unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
00086   MCCFIInstruction Inst =
00087       MCCFIInstruction::createDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
00088   MAI->addInitialFrameState(Inst);
00089 
00090   return MAI;
00091 }
00092 
00093 static MCCodeGenInfo *createPPCMCCodeGenInfo(StringRef TT, Reloc::Model RM,
00094                                              CodeModel::Model CM,
00095                                              CodeGenOpt::Level OL) {
00096   MCCodeGenInfo *X = new MCCodeGenInfo();
00097 
00098   if (RM == Reloc::Default) {
00099     Triple T(TT);
00100     if (T.isOSDarwin())
00101       RM = Reloc::DynamicNoPIC;
00102     else
00103       RM = Reloc::Static;
00104   }
00105   if (CM == CodeModel::Default) {
00106     Triple T(TT);
00107     if (!T.isOSDarwin() &&
00108         (T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le))
00109       CM = CodeModel::Medium;
00110   }
00111   X->InitMCCodeGenInfo(RM, CM, OL);
00112   return X;
00113 }
00114 
00115 namespace {
00116 class PPCTargetAsmStreamer : public PPCTargetStreamer {
00117   formatted_raw_ostream &OS;
00118 
00119 public:
00120   PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
00121       : PPCTargetStreamer(S), OS(OS) {}
00122   void emitTCEntry(const MCSymbol &S) override {
00123     OS << "\t.tc ";
00124     OS << S.getName();
00125     OS << "[TC],";
00126     OS << S.getName();
00127     OS << '\n';
00128   }
00129   void emitMachine(StringRef CPU) override {
00130     OS << "\t.machine " << CPU << '\n';
00131   }
00132   void emitAbiVersion(int AbiVersion) override {
00133     OS << "\t.abiversion " << AbiVersion << '\n';
00134   }
00135   void emitLocalEntry(MCSymbol *S, const MCExpr *LocalOffset) override {
00136     OS << "\t.localentry\t" << *S << ", " << *LocalOffset << '\n';
00137   }
00138 };
00139 
00140 class PPCTargetELFStreamer : public PPCTargetStreamer {
00141 public:
00142   PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
00143   MCELFStreamer &getStreamer() {
00144     return static_cast<MCELFStreamer &>(Streamer);
00145   }
00146   void emitTCEntry(const MCSymbol &S) override {
00147     // Creates a R_PPC64_TOC relocation
00148     Streamer.EmitSymbolValue(&S, 8);
00149   }
00150   void emitMachine(StringRef CPU) override {
00151     // FIXME: Is there anything to do in here or does this directive only
00152     // limit the parser?
00153   }
00154   void emitAbiVersion(int AbiVersion) override {
00155     MCAssembler &MCA = getStreamer().getAssembler();
00156     unsigned Flags = MCA.getELFHeaderEFlags();
00157     Flags &= ~ELF::EF_PPC64_ABI;
00158     Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
00159     MCA.setELFHeaderEFlags(Flags);
00160   }
00161   void emitLocalEntry(MCSymbol *S, const MCExpr *LocalOffset) override {
00162     MCAssembler &MCA = getStreamer().getAssembler();
00163     MCSymbolData &Data = getStreamer().getOrCreateSymbolData(S);
00164 
00165     int64_t Res;
00166     if (!LocalOffset->EvaluateAsAbsolute(Res, MCA))
00167       report_fatal_error(".localentry expression must be absolute.");
00168 
00169     unsigned Encoded = ELF::encodePPC64LocalEntryOffset(Res);
00170     if (Res != ELF::decodePPC64LocalEntryOffset(Encoded))
00171       report_fatal_error(".localentry expression cannot be encoded.");
00172 
00173     // The "other" values are stored in the last 6 bits of the second byte.
00174     // The traditional defines for STO values assume the full byte and thus
00175     // the shift to pack it.
00176     unsigned Other = MCELF::getOther(Data) << 2;
00177     Other &= ~ELF::STO_PPC64_LOCAL_MASK;
00178     Other |= Encoded;
00179     MCELF::setOther(Data, Other >> 2);
00180 
00181     // For GAS compatibility, unless we already saw a .abiversion directive,
00182     // set e_flags to indicate ELFv2 ABI.
00183     unsigned Flags = MCA.getELFHeaderEFlags();
00184     if ((Flags & ELF::EF_PPC64_ABI) == 0)
00185       MCA.setELFHeaderEFlags(Flags | 2);
00186   }
00187 };
00188 
00189 class PPCTargetMachOStreamer : public PPCTargetStreamer {
00190 public:
00191   PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
00192   void emitTCEntry(const MCSymbol &S) override {
00193     llvm_unreachable("Unknown pseudo-op: .tc");
00194   }
00195   void emitMachine(StringRef CPU) override {
00196     // FIXME: We should update the CPUType, CPUSubType in the Object file if
00197     // the new values are different from the defaults.
00198   }
00199   void emitAbiVersion(int AbiVersion) override {
00200     llvm_unreachable("Unknown pseudo-op: .abiversion");
00201   }
00202   void emitLocalEntry(MCSymbol *S, const MCExpr *LocalOffset) override {
00203     llvm_unreachable("Unknown pseudo-op: .localentry");
00204   }
00205 };
00206 }
00207 
00208 // This is duplicated code. Refactor this.
00209 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
00210                                     MCContext &Ctx, MCAsmBackend &MAB,
00211                                     raw_ostream &OS,
00212                                     MCCodeEmitter *Emitter,
00213                                     const MCSubtargetInfo &STI,
00214                                     bool RelaxAll,
00215                                     bool NoExecStack) {
00216   if (Triple(TT).isOSDarwin()) {
00217     MCStreamer *S = createMachOStreamer(Ctx, MAB, OS, Emitter, RelaxAll);
00218     new PPCTargetMachOStreamer(*S);
00219     return S;
00220   }
00221 
00222   MCStreamer *S =
00223       createELFStreamer(Ctx, MAB, OS, Emitter, RelaxAll, NoExecStack);
00224   new PPCTargetELFStreamer(*S);
00225   return S;
00226 }
00227 
00228 static MCStreamer *
00229 createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
00230                     bool isVerboseAsm, bool useDwarfDirectory,
00231                     MCInstPrinter *InstPrint, MCCodeEmitter *CE,
00232                     MCAsmBackend *TAB, bool ShowInst) {
00233 
00234   MCStreamer *S = llvm::createAsmStreamer(
00235       Ctx, OS, isVerboseAsm, useDwarfDirectory, InstPrint, CE, TAB, ShowInst);
00236   new PPCTargetAsmStreamer(*S, OS);
00237   return S;
00238 }
00239 
00240 static MCInstPrinter *createPPCMCInstPrinter(const Target &T,
00241                                              unsigned SyntaxVariant,
00242                                              const MCAsmInfo &MAI,
00243                                              const MCInstrInfo &MII,
00244                                              const MCRegisterInfo &MRI,
00245                                              const MCSubtargetInfo &STI) {
00246   bool isDarwin = Triple(STI.getTargetTriple()).isOSDarwin();
00247   return new PPCInstPrinter(MAI, MII, MRI, isDarwin);
00248 }
00249 
00250 extern "C" void LLVMInitializePowerPCTargetMC() {
00251   // Register the MC asm info.
00252   RegisterMCAsmInfoFn C(ThePPC32Target, createPPCMCAsmInfo);
00253   RegisterMCAsmInfoFn D(ThePPC64Target, createPPCMCAsmInfo);  
00254   RegisterMCAsmInfoFn E(ThePPC64LETarget, createPPCMCAsmInfo);  
00255 
00256   // Register the MC codegen info.
00257   TargetRegistry::RegisterMCCodeGenInfo(ThePPC32Target, createPPCMCCodeGenInfo);
00258   TargetRegistry::RegisterMCCodeGenInfo(ThePPC64Target, createPPCMCCodeGenInfo);
00259   TargetRegistry::RegisterMCCodeGenInfo(ThePPC64LETarget,
00260                                         createPPCMCCodeGenInfo);
00261 
00262   // Register the MC instruction info.
00263   TargetRegistry::RegisterMCInstrInfo(ThePPC32Target, createPPCMCInstrInfo);
00264   TargetRegistry::RegisterMCInstrInfo(ThePPC64Target, createPPCMCInstrInfo);
00265   TargetRegistry::RegisterMCInstrInfo(ThePPC64LETarget,
00266                                       createPPCMCInstrInfo);
00267 
00268   // Register the MC register info.
00269   TargetRegistry::RegisterMCRegInfo(ThePPC32Target, createPPCMCRegisterInfo);
00270   TargetRegistry::RegisterMCRegInfo(ThePPC64Target, createPPCMCRegisterInfo);
00271   TargetRegistry::RegisterMCRegInfo(ThePPC64LETarget, createPPCMCRegisterInfo);
00272 
00273   // Register the MC subtarget info.
00274   TargetRegistry::RegisterMCSubtargetInfo(ThePPC32Target,
00275                                           createPPCMCSubtargetInfo);
00276   TargetRegistry::RegisterMCSubtargetInfo(ThePPC64Target,
00277                                           createPPCMCSubtargetInfo);
00278   TargetRegistry::RegisterMCSubtargetInfo(ThePPC64LETarget,
00279                                           createPPCMCSubtargetInfo);
00280 
00281   // Register the MC Code Emitter
00282   TargetRegistry::RegisterMCCodeEmitter(ThePPC32Target, createPPCMCCodeEmitter);
00283   TargetRegistry::RegisterMCCodeEmitter(ThePPC64Target, createPPCMCCodeEmitter);
00284   TargetRegistry::RegisterMCCodeEmitter(ThePPC64LETarget,
00285                                         createPPCMCCodeEmitter);
00286   
00287     // Register the asm backend.
00288   TargetRegistry::RegisterMCAsmBackend(ThePPC32Target, createPPCAsmBackend);
00289   TargetRegistry::RegisterMCAsmBackend(ThePPC64Target, createPPCAsmBackend);
00290   TargetRegistry::RegisterMCAsmBackend(ThePPC64LETarget, createPPCAsmBackend);
00291   
00292   // Register the object streamer.
00293   TargetRegistry::RegisterMCObjectStreamer(ThePPC32Target, createMCStreamer);
00294   TargetRegistry::RegisterMCObjectStreamer(ThePPC64Target, createMCStreamer);
00295   TargetRegistry::RegisterMCObjectStreamer(ThePPC64LETarget, createMCStreamer);
00296 
00297   // Register the asm streamer.
00298   TargetRegistry::RegisterAsmStreamer(ThePPC32Target, createMCAsmStreamer);
00299   TargetRegistry::RegisterAsmStreamer(ThePPC64Target, createMCAsmStreamer);
00300   TargetRegistry::RegisterAsmStreamer(ThePPC64LETarget, createMCAsmStreamer);
00301 
00302   // Register the MCInstPrinter.
00303   TargetRegistry::RegisterMCInstPrinter(ThePPC32Target, createPPCMCInstPrinter);
00304   TargetRegistry::RegisterMCInstPrinter(ThePPC64Target, createPPCMCInstPrinter);
00305   TargetRegistry::RegisterMCInstPrinter(ThePPC64LETarget,
00306                                         createPPCMCInstPrinter);
00307 }