LLVM  mainline
TargetRegistry.h
Go to the documentation of this file.
00001 //===-- Support/TargetRegistry.h - Target Registration ----------*- C++ -*-===//
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 exposes the TargetRegistry interface, which tools can use to access
00011 // the appropriate target specific classes (TargetMachine, AsmPrinter, etc.)
00012 // which have been registered.
00013 //
00014 // Target specific class implementations should register themselves using the
00015 // appropriate TargetRegistry interfaces.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_SUPPORT_TARGETREGISTRY_H
00020 #define LLVM_SUPPORT_TARGETREGISTRY_H
00021 
00022 #include "llvm-c/Disassembler.h"
00023 #include "llvm/ADT/Triple.h"
00024 #include "llvm/Support/CodeGen.h"
00025 #include <cassert>
00026 #include <memory>
00027 #include <string>
00028 
00029 namespace llvm {
00030   class AsmPrinter;
00031   class MCAsmBackend;
00032   class MCAsmInfo;
00033   class MCAsmParser;
00034   class MCCodeEmitter;
00035   class MCCodeGenInfo;
00036   class MCContext;
00037   class MCDisassembler;
00038   class MCInstrAnalysis;
00039   class MCInstPrinter;
00040   class MCInstrInfo;
00041   class MCRegisterInfo;
00042   class MCStreamer;
00043   class MCSubtargetInfo;
00044   class MCSymbolizer;
00045   class MCRelocationInfo;
00046   class MCTargetAsmParser;
00047   class MCTargetOptions;
00048   class MCTargetStreamer;
00049   class TargetMachine;
00050   class TargetOptions;
00051   class raw_ostream;
00052   class formatted_raw_ostream;
00053 
00054   MCStreamer *createNullStreamer(MCContext &Ctx);
00055   MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
00056                                 bool isVerboseAsm, bool useDwarfDirectory,
00057                                 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
00058                                 MCAsmBackend *TAB, bool ShowInst);
00059 
00060   /// Takes ownership of \p TAB and \p CE.
00061   MCStreamer *createELFStreamer(MCContext &Ctx, MCAsmBackend &TAB,
00062                                 raw_ostream &OS, MCCodeEmitter *CE,
00063                                 bool RelaxAll);
00064   MCStreamer *createMachOStreamer(MCContext &Ctx, MCAsmBackend &TAB,
00065                                   raw_ostream &OS, MCCodeEmitter *CE,
00066                                   bool RelaxAll, bool DWARFMustBeAtTheEnd,
00067                                   bool LabelSections = false);
00068 
00069   MCRelocationInfo *createMCRelocationInfo(StringRef TT, MCContext &Ctx);
00070 
00071   MCSymbolizer *createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
00072                                    LLVMSymbolLookupCallback SymbolLookUp,
00073                                    void *DisInfo, MCContext *Ctx,
00074                                    std::unique_ptr<MCRelocationInfo> &&RelInfo);
00075 
00076   /// Target - Wrapper for Target specific information.
00077   ///
00078   /// For registration purposes, this is a POD type so that targets can be
00079   /// registered without the use of static constructors.
00080   ///
00081   /// Targets should implement a single global instance of this class (which
00082   /// will be zero initialized), and pass that instance to the TargetRegistry as
00083   /// part of their initialization.
00084   class Target {
00085   public:
00086     friend struct TargetRegistry;
00087 
00088     typedef bool (*ArchMatchFnTy)(Triple::ArchType Arch);
00089 
00090     typedef MCAsmInfo *(*MCAsmInfoCtorFnTy)(const MCRegisterInfo &MRI,
00091                                             StringRef TT);
00092     typedef MCCodeGenInfo *(*MCCodeGenInfoCtorFnTy)(StringRef TT,
00093                                                     Reloc::Model RM,
00094                                                     CodeModel::Model CM,
00095                                                     CodeGenOpt::Level OL);
00096     typedef MCInstrInfo *(*MCInstrInfoCtorFnTy)(void);
00097     typedef MCInstrAnalysis *(*MCInstrAnalysisCtorFnTy)(const MCInstrInfo*Info);
00098     typedef MCRegisterInfo *(*MCRegInfoCtorFnTy)(StringRef TT);
00099     typedef MCSubtargetInfo *(*MCSubtargetInfoCtorFnTy)(StringRef TT,
00100                                                         StringRef CPU,
00101                                                         StringRef Features);
00102     typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
00103                                                   StringRef TT,
00104                                                   StringRef CPU,
00105                                                   StringRef Features,
00106                                                   const TargetOptions &Options,
00107                                                   Reloc::Model RM,
00108                                                   CodeModel::Model CM,
00109                                                   CodeGenOpt::Level OL);
00110     // If it weren't for layering issues (this header is in llvm/Support, but
00111     // depends on MC?) this should take the Streamer by value rather than rvalue
00112     // reference.
00113     typedef AsmPrinter *(*AsmPrinterCtorTy)(
00114         TargetMachine &TM, std::unique_ptr<MCStreamer> &&Streamer);
00115     typedef MCAsmBackend *(*MCAsmBackendCtorTy)(const Target &T,
00116                                                 const MCRegisterInfo &MRI,
00117                                                 StringRef TT,
00118                                                 StringRef CPU);
00119     typedef MCTargetAsmParser *(*MCAsmParserCtorTy)(
00120         MCSubtargetInfo &STI,
00121         MCAsmParser &P,
00122         const MCInstrInfo &MII,
00123         const MCTargetOptions &Options);
00124     typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T,
00125                                                     const MCSubtargetInfo &STI,
00126                                                     MCContext &Ctx);
00127     typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
00128                                                   unsigned SyntaxVariant,
00129                                                   const MCAsmInfo &MAI,
00130                                                   const MCInstrInfo &MII,
00131                                                   const MCRegisterInfo &MRI,
00132                                                   const MCSubtargetInfo &STI);
00133     typedef MCCodeEmitter *(*MCCodeEmitterCtorTy)(const MCInstrInfo &II,
00134                                                   const MCRegisterInfo &MRI,
00135                                                   MCContext &Ctx);
00136     typedef MCStreamer *(*ELFStreamerCtorTy)(const Triple &T, MCContext &Ctx,
00137                                              MCAsmBackend &TAB, raw_ostream &OS,
00138                                              MCCodeEmitter *Emitter,
00139                                              bool RelaxAll);
00140     typedef MCStreamer *(*MachOStreamerCtorTy)(
00141         MCContext &Ctx, MCAsmBackend &TAB, raw_ostream &OS,
00142         MCCodeEmitter *Emitter, bool RelaxAll, bool DWARFMustBeAtTheEnd);
00143     typedef MCStreamer *(*COFFStreamerCtorTy)(MCContext &Ctx, MCAsmBackend &TAB,
00144                                               raw_ostream &OS,
00145                                               MCCodeEmitter *Emitter,
00146                                               bool RelaxAll);
00147     typedef MCTargetStreamer *(*NullTargetStreamerCtorTy)(MCStreamer &S);
00148     typedef MCTargetStreamer *(*AsmTargetStreamerCtorTy)(
00149         MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint,
00150         bool IsVerboseAsm);
00151     typedef MCTargetStreamer *(*ObjectTargetStreamerCtorTy)(
00152         MCStreamer &S, const MCSubtargetInfo &STI);
00153     typedef MCRelocationInfo *(*MCRelocationInfoCtorTy)(StringRef TT,
00154                                                         MCContext &Ctx);
00155     typedef MCSymbolizer *(*MCSymbolizerCtorTy)(
00156         StringRef TT, LLVMOpInfoCallback GetOpInfo,
00157         LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx,
00158         std::unique_ptr<MCRelocationInfo> &&RelInfo);
00159 
00160   private:
00161     /// Next - The next registered target in the linked list, maintained by the
00162     /// TargetRegistry.
00163     Target *Next;
00164 
00165     /// The target function for checking if an architecture is supported.
00166     ArchMatchFnTy ArchMatchFn;
00167 
00168     /// Name - The target name.
00169     const char *Name;
00170 
00171     /// ShortDesc - A short description of the target.
00172     const char *ShortDesc;
00173 
00174     /// HasJIT - Whether this target supports the JIT.
00175     bool HasJIT;
00176 
00177     /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
00178     /// registered.
00179     MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
00180 
00181     /// MCCodeGenInfoCtorFn - Constructor function for this target's
00182     /// MCCodeGenInfo, if registered.
00183     MCCodeGenInfoCtorFnTy MCCodeGenInfoCtorFn;
00184 
00185     /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
00186     /// if registered.
00187     MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
00188 
00189     /// MCInstrAnalysisCtorFn - Constructor function for this target's
00190     /// MCInstrAnalysis, if registered.
00191     MCInstrAnalysisCtorFnTy MCInstrAnalysisCtorFn;
00192 
00193     /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
00194     /// if registered.
00195     MCRegInfoCtorFnTy MCRegInfoCtorFn;
00196 
00197     /// MCSubtargetInfoCtorFn - Constructor function for this target's
00198     /// MCSubtargetInfo, if registered.
00199     MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn;
00200 
00201     /// TargetMachineCtorFn - Construction function for this target's
00202     /// TargetMachine, if registered.
00203     TargetMachineCtorTy TargetMachineCtorFn;
00204 
00205     /// MCAsmBackendCtorFn - Construction function for this target's
00206     /// MCAsmBackend, if registered.
00207     MCAsmBackendCtorTy MCAsmBackendCtorFn;
00208 
00209     /// MCAsmParserCtorFn - Construction function for this target's
00210     /// MCTargetAsmParser, if registered.
00211     MCAsmParserCtorTy MCAsmParserCtorFn;
00212 
00213     /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
00214     /// if registered.
00215     AsmPrinterCtorTy AsmPrinterCtorFn;
00216 
00217     /// MCDisassemblerCtorFn - Construction function for this target's
00218     /// MCDisassembler, if registered.
00219     MCDisassemblerCtorTy MCDisassemblerCtorFn;
00220 
00221     /// MCInstPrinterCtorFn - Construction function for this target's
00222     /// MCInstPrinter, if registered.
00223     MCInstPrinterCtorTy MCInstPrinterCtorFn;
00224 
00225     /// MCCodeEmitterCtorFn - Construction function for this target's
00226     /// CodeEmitter, if registered.
00227     MCCodeEmitterCtorTy MCCodeEmitterCtorFn;
00228 
00229     // Construction functions for the various object formats, if registered.
00230     COFFStreamerCtorTy COFFStreamerCtorFn;
00231     MachOStreamerCtorTy MachOStreamerCtorFn;
00232     ELFStreamerCtorTy ELFStreamerCtorFn;
00233 
00234     /// Construction function for this target's null TargetStreamer, if
00235     /// registered (default = nullptr).
00236     NullTargetStreamerCtorTy NullTargetStreamerCtorFn;
00237 
00238     /// Construction function for this target's asm TargetStreamer, if
00239     /// registered (default = nullptr).
00240     AsmTargetStreamerCtorTy AsmTargetStreamerCtorFn;
00241 
00242     /// Construction function for this target's obj TargetStreamer, if
00243     /// registered (default = nullptr).
00244     ObjectTargetStreamerCtorTy ObjectTargetStreamerCtorFn;
00245 
00246     /// MCRelocationInfoCtorFn - Construction function for this target's
00247     /// MCRelocationInfo, if registered (default = llvm::createMCRelocationInfo)
00248     MCRelocationInfoCtorTy MCRelocationInfoCtorFn;
00249 
00250     /// MCSymbolizerCtorFn - Construction function for this target's
00251     /// MCSymbolizer, if registered (default = llvm::createMCSymbolizer)
00252     MCSymbolizerCtorTy MCSymbolizerCtorFn;
00253 
00254   public:
00255     Target()
00256         : COFFStreamerCtorFn(nullptr), MachOStreamerCtorFn(nullptr),
00257           ELFStreamerCtorFn(nullptr), NullTargetStreamerCtorFn(nullptr),
00258           AsmTargetStreamerCtorFn(nullptr), ObjectTargetStreamerCtorFn(nullptr),
00259           MCRelocationInfoCtorFn(nullptr), MCSymbolizerCtorFn(nullptr) {}
00260 
00261     /// @name Target Information
00262     /// @{
00263 
00264     // getNext - Return the next registered target.
00265     const Target *getNext() const { return Next; }
00266 
00267     /// getName - Get the target name.
00268     const char *getName() const { return Name; }
00269 
00270     /// getShortDescription - Get a short description of the target.
00271     const char *getShortDescription() const { return ShortDesc; }
00272 
00273     /// @}
00274     /// @name Feature Predicates
00275     /// @{
00276 
00277     /// hasJIT - Check if this targets supports the just-in-time compilation.
00278     bool hasJIT() const { return HasJIT; }
00279 
00280     /// hasTargetMachine - Check if this target supports code generation.
00281     bool hasTargetMachine() const { return TargetMachineCtorFn != nullptr; }
00282 
00283     /// hasMCAsmBackend - Check if this target supports .o generation.
00284     bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != nullptr; }
00285 
00286     /// @}
00287     /// @name Feature Constructors
00288     /// @{
00289 
00290     /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
00291     /// target triple.
00292     ///
00293     /// \param Triple This argument is used to determine the target machine
00294     /// feature set; it should always be provided. Generally this should be
00295     /// either the target triple from the module, or the target triple of the
00296     /// host if that does not exist.
00297     MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI,
00298                                StringRef Triple) const {
00299       if (!MCAsmInfoCtorFn)
00300         return nullptr;
00301       return MCAsmInfoCtorFn(MRI, Triple);
00302     }
00303 
00304     /// createMCCodeGenInfo - Create a MCCodeGenInfo implementation.
00305     ///
00306     MCCodeGenInfo *createMCCodeGenInfo(StringRef Triple, Reloc::Model RM,
00307                                        CodeModel::Model CM,
00308                                        CodeGenOpt::Level OL) const {
00309       if (!MCCodeGenInfoCtorFn)
00310         return nullptr;
00311       return MCCodeGenInfoCtorFn(Triple, RM, CM, OL);
00312     }
00313 
00314     /// createMCInstrInfo - Create a MCInstrInfo implementation.
00315     ///
00316     MCInstrInfo *createMCInstrInfo() const {
00317       if (!MCInstrInfoCtorFn)
00318         return nullptr;
00319       return MCInstrInfoCtorFn();
00320     }
00321 
00322     /// createMCInstrAnalysis - Create a MCInstrAnalysis implementation.
00323     ///
00324     MCInstrAnalysis *createMCInstrAnalysis(const MCInstrInfo *Info) const {
00325       if (!MCInstrAnalysisCtorFn)
00326         return nullptr;
00327       return MCInstrAnalysisCtorFn(Info);
00328     }
00329 
00330     /// createMCRegInfo - Create a MCRegisterInfo implementation.
00331     ///
00332     MCRegisterInfo *createMCRegInfo(StringRef Triple) const {
00333       if (!MCRegInfoCtorFn)
00334         return nullptr;
00335       return MCRegInfoCtorFn(Triple);
00336     }
00337 
00338     /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
00339     ///
00340     /// \param Triple This argument is used to determine the target machine
00341     /// feature set; it should always be provided. Generally this should be
00342     /// either the target triple from the module, or the target triple of the
00343     /// host if that does not exist.
00344     /// \param CPU This specifies the name of the target CPU.
00345     /// \param Features This specifies the string representation of the
00346     /// additional target features.
00347     MCSubtargetInfo *createMCSubtargetInfo(StringRef Triple, StringRef CPU,
00348                                            StringRef Features) const {
00349       if (!MCSubtargetInfoCtorFn)
00350         return nullptr;
00351       return MCSubtargetInfoCtorFn(Triple, CPU, Features);
00352     }
00353 
00354     /// createTargetMachine - Create a target specific machine implementation
00355     /// for the specified \p Triple.
00356     ///
00357     /// \param Triple This argument is used to determine the target machine
00358     /// feature set; it should always be provided. Generally this should be
00359     /// either the target triple from the module, or the target triple of the
00360     /// host if that does not exist.
00361     TargetMachine *createTargetMachine(StringRef Triple, StringRef CPU,
00362                              StringRef Features, const TargetOptions &Options,
00363                              Reloc::Model RM = Reloc::Default,
00364                              CodeModel::Model CM = CodeModel::Default,
00365                              CodeGenOpt::Level OL = CodeGenOpt::Default) const {
00366       if (!TargetMachineCtorFn)
00367         return nullptr;
00368       return TargetMachineCtorFn(*this, Triple, CPU, Features, Options,
00369                                  RM, CM, OL);
00370     }
00371 
00372     /// createMCAsmBackend - Create a target specific assembly parser.
00373     ///
00374     /// \param Triple The target triple string.
00375     MCAsmBackend *createMCAsmBackend(const MCRegisterInfo &MRI,
00376                                      StringRef Triple, StringRef CPU) const {
00377       if (!MCAsmBackendCtorFn)
00378         return nullptr;
00379       return MCAsmBackendCtorFn(*this, MRI, Triple, CPU);
00380     }
00381 
00382     /// createMCAsmParser - Create a target specific assembly parser.
00383     ///
00384     /// \param Parser The target independent parser implementation to use for
00385     /// parsing and lexing.
00386     MCTargetAsmParser *createMCAsmParser(
00387         MCSubtargetInfo &STI,
00388         MCAsmParser &Parser,
00389         const MCInstrInfo &MII,
00390         const MCTargetOptions &Options) const {
00391       if (!MCAsmParserCtorFn)
00392         return nullptr;
00393       return MCAsmParserCtorFn(STI, Parser, MII, Options);
00394     }
00395 
00396     /// createAsmPrinter - Create a target specific assembly printer pass.  This
00397     /// takes ownership of the MCStreamer object.
00398     AsmPrinter *createAsmPrinter(TargetMachine &TM,
00399                                  std::unique_ptr<MCStreamer> &&Streamer) const {
00400       if (!AsmPrinterCtorFn)
00401         return nullptr;
00402       return AsmPrinterCtorFn(TM, std::move(Streamer));
00403     }
00404 
00405     MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI,
00406                                          MCContext &Ctx) const {
00407       if (!MCDisassemblerCtorFn)
00408         return nullptr;
00409       return MCDisassemblerCtorFn(*this, STI, Ctx);
00410     }
00411 
00412     MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
00413                                        const MCAsmInfo &MAI,
00414                                        const MCInstrInfo &MII,
00415                                        const MCRegisterInfo &MRI,
00416                                        const MCSubtargetInfo &STI) const {
00417       if (!MCInstPrinterCtorFn)
00418         return nullptr;
00419       return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI, MII, MRI, STI);
00420     }
00421 
00422 
00423     /// createMCCodeEmitter - Create a target specific code emitter.
00424     MCCodeEmitter *createMCCodeEmitter(const MCInstrInfo &II,
00425                                        const MCRegisterInfo &MRI,
00426                                        MCContext &Ctx) const {
00427       if (!MCCodeEmitterCtorFn)
00428         return nullptr;
00429       return MCCodeEmitterCtorFn(II, MRI, Ctx);
00430     }
00431 
00432     /// Create a target specific MCStreamer.
00433     ///
00434     /// \param T The target triple.
00435     /// \param Ctx The target context.
00436     /// \param TAB The target assembler backend object. Takes ownership.
00437     /// \param OS The stream object.
00438     /// \param Emitter The target independent assembler object.Takes ownership.
00439     /// \param RelaxAll Relax all fixups?
00440     MCStreamer *createMCObjectStreamer(const Triple &T, MCContext &Ctx,
00441                                        MCAsmBackend &TAB, raw_ostream &OS,
00442                                        MCCodeEmitter *Emitter,
00443                                        const MCSubtargetInfo &STI,
00444                                        bool RelaxAll,
00445                                        bool DWARFMustBeAtTheEnd) const {
00446       MCStreamer *S;
00447       switch (T.getObjectFormat()) {
00448       default:
00449         llvm_unreachable("Unknown object format");
00450       case Triple::COFF:
00451         assert(T.isOSWindows() && "only Windows COFF is supported");
00452         S = COFFStreamerCtorFn(Ctx, TAB, OS, Emitter, RelaxAll);
00453         break;
00454       case Triple::MachO:
00455         if (MachOStreamerCtorFn)
00456           S = MachOStreamerCtorFn(Ctx, TAB, OS, Emitter, RelaxAll,
00457                                   DWARFMustBeAtTheEnd);
00458         else
00459           S = createMachOStreamer(Ctx, TAB, OS, Emitter, RelaxAll,
00460                                   DWARFMustBeAtTheEnd);
00461         break;
00462       case Triple::ELF:
00463         if (ELFStreamerCtorFn)
00464           S = ELFStreamerCtorFn(T, Ctx, TAB, OS, Emitter, RelaxAll);
00465         else
00466           S = createELFStreamer(Ctx, TAB, OS, Emitter, RelaxAll);
00467         break;
00468       }
00469       if (ObjectTargetStreamerCtorFn)
00470         ObjectTargetStreamerCtorFn(*S, STI);
00471       return S;
00472     }
00473 
00474     MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
00475                                   bool IsVerboseAsm, bool UseDwarfDirectory,
00476                                   MCInstPrinter *InstPrint, MCCodeEmitter *CE,
00477                                   MCAsmBackend *TAB, bool ShowInst) const {
00478       MCStreamer *S =
00479           llvm::createAsmStreamer(Ctx, OS, IsVerboseAsm, UseDwarfDirectory,
00480                                   InstPrint, CE, TAB, ShowInst);
00481       createAsmTargetStreamer(*S, OS, InstPrint, IsVerboseAsm);
00482       return S;
00483     }
00484 
00485     MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
00486                                               formatted_raw_ostream &OS,
00487                                               MCInstPrinter *InstPrint,
00488                                               bool IsVerboseAsm) const {
00489       if (AsmTargetStreamerCtorFn)
00490         return AsmTargetStreamerCtorFn(S, OS, InstPrint, IsVerboseAsm);
00491       return nullptr;
00492     }
00493 
00494     MCStreamer *createNullStreamer(MCContext &Ctx) const {
00495       MCStreamer *S = llvm::createNullStreamer(Ctx);
00496       createNullTargetStreamer(*S);
00497       return S;
00498     }
00499 
00500     MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) const {
00501       if (NullTargetStreamerCtorFn)
00502         return NullTargetStreamerCtorFn(S);
00503       return nullptr;
00504     }
00505 
00506     /// createMCRelocationInfo - Create a target specific MCRelocationInfo.
00507     ///
00508     /// \param TT The target triple.
00509     /// \param Ctx The target context.
00510     MCRelocationInfo *
00511       createMCRelocationInfo(StringRef TT, MCContext &Ctx) const {
00512       MCRelocationInfoCtorTy Fn = MCRelocationInfoCtorFn
00513                                       ? MCRelocationInfoCtorFn
00514                                       : llvm::createMCRelocationInfo;
00515       return Fn(TT, Ctx);
00516     }
00517 
00518     /// createMCSymbolizer - Create a target specific MCSymbolizer.
00519     ///
00520     /// \param TT The target triple.
00521     /// \param GetOpInfo The function to get the symbolic information for operands.
00522     /// \param SymbolLookUp The function to lookup a symbol name.
00523     /// \param DisInfo The pointer to the block of symbolic information for above call
00524     /// back.
00525     /// \param Ctx The target context.
00526     /// \param RelInfo The relocation information for this target. Takes ownership.
00527     MCSymbolizer *
00528     createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
00529                        LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo,
00530                        MCContext *Ctx,
00531                        std::unique_ptr<MCRelocationInfo> &&RelInfo) const {
00532       MCSymbolizerCtorTy Fn =
00533           MCSymbolizerCtorFn ? MCSymbolizerCtorFn : llvm::createMCSymbolizer;
00534       return Fn(TT, GetOpInfo, SymbolLookUp, DisInfo, Ctx, std::move(RelInfo));
00535     }
00536 
00537     /// @}
00538   };
00539 
00540   /// TargetRegistry - Generic interface to target specific features.
00541   struct TargetRegistry {
00542     class iterator {
00543       const Target *Current;
00544       explicit iterator(Target *T) : Current(T) {}
00545       friend struct TargetRegistry;
00546     public:
00547       iterator() : Current(nullptr) {}
00548 
00549       bool operator==(const iterator &x) const {
00550         return Current == x.Current;
00551       }
00552       bool operator!=(const iterator &x) const {
00553         return !operator==(x);
00554       }
00555 
00556       // Iterator traversal: forward iteration only
00557       iterator &operator++() {          // Preincrement
00558         assert(Current && "Cannot increment end iterator!");
00559         Current = Current->getNext();
00560         return *this;
00561       }
00562       iterator operator++(int) {        // Postincrement
00563         iterator tmp = *this;
00564         ++*this;
00565         return tmp;
00566       }
00567 
00568       const Target &operator*() const {
00569         assert(Current && "Cannot dereference end iterator!");
00570         return *Current;
00571       }
00572 
00573       const Target *operator->() const {
00574         return &operator*();
00575       }
00576     };
00577 
00578     /// printRegisteredTargetsForVersion - Print the registered targets
00579     /// appropriately for inclusion in a tool's version output.
00580     static void printRegisteredTargetsForVersion();
00581 
00582     /// @name Registry Access
00583     /// @{
00584 
00585     static iterator begin();
00586 
00587     static iterator end() { return iterator(); }
00588 
00589     /// lookupTarget - Lookup a target based on a target triple.
00590     ///
00591     /// \param Triple - The triple to use for finding a target.
00592     /// \param Error - On failure, an error string describing why no target was
00593     /// found.
00594     static const Target *lookupTarget(const std::string &Triple,
00595                                       std::string &Error);
00596 
00597     /// lookupTarget - Lookup a target based on an architecture name
00598     /// and a target triple.  If the architecture name is non-empty,
00599     /// then the lookup is done by architecture.  Otherwise, the target
00600     /// triple is used.
00601     ///
00602     /// \param ArchName - The architecture to use for finding a target.
00603     /// \param TheTriple - The triple to use for finding a target.  The
00604     /// triple is updated with canonical architecture name if a lookup
00605     /// by architecture is done.
00606     /// \param Error - On failure, an error string describing why no target was
00607     /// found.
00608     static const Target *lookupTarget(const std::string &ArchName,
00609                                       Triple &TheTriple,
00610                                       std::string &Error);
00611 
00612     /// @}
00613     /// @name Target Registration
00614     /// @{
00615 
00616     /// RegisterTarget - Register the given target. Attempts to register a
00617     /// target which has already been registered will be ignored.
00618     ///
00619     /// Clients are responsible for ensuring that registration doesn't occur
00620     /// while another thread is attempting to access the registry. Typically
00621     /// this is done by initializing all targets at program startup.
00622     ///
00623     /// @param T - The target being registered.
00624     /// @param Name - The target name. This should be a static string.
00625     /// @param ShortDesc - A short target description. This should be a static
00626     /// string.
00627     /// @param ArchMatchFn - The arch match checking function for this target.
00628     /// @param HasJIT - Whether the target supports JIT code
00629     /// generation.
00630     static void RegisterTarget(Target &T,
00631                                const char *Name,
00632                                const char *ShortDesc,
00633                                Target::ArchMatchFnTy ArchMatchFn,
00634                                bool HasJIT = false);
00635 
00636     /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
00637     /// given target.
00638     ///
00639     /// Clients are responsible for ensuring that registration doesn't occur
00640     /// while another thread is attempting to access the registry. Typically
00641     /// this is done by initializing all targets at program startup.
00642     ///
00643     /// @param T - The target being registered.
00644     /// @param Fn - A function to construct a MCAsmInfo for the target.
00645     static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
00646       T.MCAsmInfoCtorFn = Fn;
00647     }
00648 
00649     /// RegisterMCCodeGenInfo - Register a MCCodeGenInfo implementation for the
00650     /// given target.
00651     ///
00652     /// Clients are responsible for ensuring that registration doesn't occur
00653     /// while another thread is attempting to access the registry. Typically
00654     /// this is done by initializing all targets at program startup.
00655     ///
00656     /// @param T - The target being registered.
00657     /// @param Fn - A function to construct a MCCodeGenInfo for the target.
00658     static void RegisterMCCodeGenInfo(Target &T,
00659                                      Target::MCCodeGenInfoCtorFnTy Fn) {
00660       T.MCCodeGenInfoCtorFn = Fn;
00661     }
00662 
00663     /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
00664     /// given target.
00665     ///
00666     /// Clients are responsible for ensuring that registration doesn't occur
00667     /// while another thread is attempting to access the registry. Typically
00668     /// this is done by initializing all targets at program startup.
00669     ///
00670     /// @param T - The target being registered.
00671     /// @param Fn - A function to construct a MCInstrInfo for the target.
00672     static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
00673       T.MCInstrInfoCtorFn = Fn;
00674     }
00675 
00676     /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for
00677     /// the given target.
00678     static void RegisterMCInstrAnalysis(Target &T,
00679                                         Target::MCInstrAnalysisCtorFnTy Fn) {
00680       T.MCInstrAnalysisCtorFn = Fn;
00681     }
00682 
00683     /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
00684     /// given target.
00685     ///
00686     /// Clients are responsible for ensuring that registration doesn't occur
00687     /// while another thread is attempting to access the registry. Typically
00688     /// this is done by initializing all targets at program startup.
00689     ///
00690     /// @param T - The target being registered.
00691     /// @param Fn - A function to construct a MCRegisterInfo for the target.
00692     static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
00693       T.MCRegInfoCtorFn = Fn;
00694     }
00695 
00696     /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
00697     /// the given target.
00698     ///
00699     /// Clients are responsible for ensuring that registration doesn't occur
00700     /// while another thread is attempting to access the registry. Typically
00701     /// this is done by initializing all targets at program startup.
00702     ///
00703     /// @param T - The target being registered.
00704     /// @param Fn - A function to construct a MCSubtargetInfo for the target.
00705     static void RegisterMCSubtargetInfo(Target &T,
00706                                         Target::MCSubtargetInfoCtorFnTy Fn) {
00707       T.MCSubtargetInfoCtorFn = Fn;
00708     }
00709 
00710     /// RegisterTargetMachine - Register a TargetMachine implementation for the
00711     /// given target.
00712     ///
00713     /// Clients are responsible for ensuring that registration doesn't occur
00714     /// while another thread is attempting to access the registry. Typically
00715     /// this is done by initializing all targets at program startup.
00716     ///
00717     /// @param T - The target being registered.
00718     /// @param Fn - A function to construct a TargetMachine for the target.
00719     static void RegisterTargetMachine(Target &T,
00720                                       Target::TargetMachineCtorTy Fn) {
00721       T.TargetMachineCtorFn = Fn;
00722     }
00723 
00724     /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the
00725     /// given target.
00726     ///
00727     /// Clients are responsible for ensuring that registration doesn't occur
00728     /// while another thread is attempting to access the registry. Typically
00729     /// this is done by initializing all targets at program startup.
00730     ///
00731     /// @param T - The target being registered.
00732     /// @param Fn - A function to construct an AsmBackend for the target.
00733     static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) {
00734       T.MCAsmBackendCtorFn = Fn;
00735     }
00736 
00737     /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for
00738     /// the given target.
00739     ///
00740     /// Clients are responsible for ensuring that registration doesn't occur
00741     /// while another thread is attempting to access the registry. Typically
00742     /// this is done by initializing all targets at program startup.
00743     ///
00744     /// @param T - The target being registered.
00745     /// @param Fn - A function to construct an MCTargetAsmParser for the target.
00746     static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) {
00747       T.MCAsmParserCtorFn = Fn;
00748     }
00749 
00750     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
00751     /// target.
00752     ///
00753     /// Clients are responsible for ensuring that registration doesn't occur
00754     /// while another thread is attempting to access the registry. Typically
00755     /// this is done by initializing all targets at program startup.
00756     ///
00757     /// @param T - The target being registered.
00758     /// @param Fn - A function to construct an AsmPrinter for the target.
00759     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
00760       T.AsmPrinterCtorFn = Fn;
00761     }
00762 
00763     /// RegisterMCDisassembler - Register a MCDisassembler implementation for
00764     /// the given target.
00765     ///
00766     /// Clients are responsible for ensuring that registration doesn't occur
00767     /// while another thread is attempting to access the registry. Typically
00768     /// this is done by initializing all targets at program startup.
00769     ///
00770     /// @param T - The target being registered.
00771     /// @param Fn - A function to construct an MCDisassembler for the target.
00772     static void RegisterMCDisassembler(Target &T,
00773                                        Target::MCDisassemblerCtorTy Fn) {
00774       T.MCDisassemblerCtorFn = Fn;
00775     }
00776 
00777     /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
00778     /// given target.
00779     ///
00780     /// Clients are responsible for ensuring that registration doesn't occur
00781     /// while another thread is attempting to access the registry. Typically
00782     /// this is done by initializing all targets at program startup.
00783     ///
00784     /// @param T - The target being registered.
00785     /// @param Fn - A function to construct an MCInstPrinter for the target.
00786     static void RegisterMCInstPrinter(Target &T,
00787                                       Target::MCInstPrinterCtorTy Fn) {
00788       T.MCInstPrinterCtorFn = Fn;
00789     }
00790 
00791     /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the
00792     /// given target.
00793     ///
00794     /// Clients are responsible for ensuring that registration doesn't occur
00795     /// while another thread is attempting to access the registry. Typically
00796     /// this is done by initializing all targets at program startup.
00797     ///
00798     /// @param T - The target being registered.
00799     /// @param Fn - A function to construct an MCCodeEmitter for the target.
00800     static void RegisterMCCodeEmitter(Target &T,
00801                                       Target::MCCodeEmitterCtorTy Fn) {
00802       T.MCCodeEmitterCtorFn = Fn;
00803     }
00804 
00805     static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn) {
00806       T.COFFStreamerCtorFn = Fn;
00807     }
00808 
00809     static void RegisterMachOStreamer(Target &T,
00810                                       Target::MachOStreamerCtorTy Fn) {
00811       T.MachOStreamerCtorFn = Fn;
00812     }
00813 
00814     static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn) {
00815       T.ELFStreamerCtorFn = Fn;
00816     }
00817 
00818     static void
00819     RegisterNullTargetStreamer(Target &T, Target::NullTargetStreamerCtorTy Fn) {
00820       T.NullTargetStreamerCtorFn = Fn;
00821     }
00822 
00823     static void RegisterAsmTargetStreamer(Target &T,
00824                                           Target::AsmTargetStreamerCtorTy Fn) {
00825       T.AsmTargetStreamerCtorFn = Fn;
00826     }
00827 
00828     static void
00829     RegisterObjectTargetStreamer(Target &T,
00830                                  Target::ObjectTargetStreamerCtorTy Fn) {
00831       T.ObjectTargetStreamerCtorFn = Fn;
00832     }
00833 
00834     /// RegisterMCRelocationInfo - Register an MCRelocationInfo
00835     /// implementation for the given target.
00836     ///
00837     /// Clients are responsible for ensuring that registration doesn't occur
00838     /// while another thread is attempting to access the registry. Typically
00839     /// this is done by initializing all targets at program startup.
00840     ///
00841     /// @param T - The target being registered.
00842     /// @param Fn - A function to construct an MCRelocationInfo for the target.
00843     static void RegisterMCRelocationInfo(Target &T,
00844                                          Target::MCRelocationInfoCtorTy Fn) {
00845       T.MCRelocationInfoCtorFn = Fn;
00846     }
00847 
00848     /// RegisterMCSymbolizer - Register an MCSymbolizer
00849     /// implementation for the given target.
00850     ///
00851     /// Clients are responsible for ensuring that registration doesn't occur
00852     /// while another thread is attempting to access the registry. Typically
00853     /// this is done by initializing all targets at program startup.
00854     ///
00855     /// @param T - The target being registered.
00856     /// @param Fn - A function to construct an MCSymbolizer for the target.
00857     static void RegisterMCSymbolizer(Target &T,
00858                                      Target::MCSymbolizerCtorTy Fn) {
00859       T.MCSymbolizerCtorFn = Fn;
00860     }
00861 
00862     /// @}
00863   };
00864 
00865 
00866   //===--------------------------------------------------------------------===//
00867 
00868   /// RegisterTarget - Helper template for registering a target, for use in the
00869   /// target's initialization function. Usage:
00870   ///
00871   ///
00872   /// Target TheFooTarget; // The global target instance.
00873   ///
00874   /// extern "C" void LLVMInitializeFooTargetInfo() {
00875   ///   RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
00876   /// }
00877   template<Triple::ArchType TargetArchType = Triple::UnknownArch,
00878            bool HasJIT = false>
00879   struct RegisterTarget {
00880     RegisterTarget(Target &T, const char *Name, const char *Desc) {
00881       TargetRegistry::RegisterTarget(T, Name, Desc, &getArchMatch, HasJIT);
00882     }
00883 
00884     static bool getArchMatch(Triple::ArchType Arch) {
00885       return Arch == TargetArchType;
00886     }
00887   };
00888 
00889   /// RegisterMCAsmInfo - Helper template for registering a target assembly info
00890   /// implementation.  This invokes the static "Create" method on the class to
00891   /// actually do the construction.  Usage:
00892   ///
00893   /// extern "C" void LLVMInitializeFooTarget() {
00894   ///   extern Target TheFooTarget;
00895   ///   RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
00896   /// }
00897   template<class MCAsmInfoImpl>
00898   struct RegisterMCAsmInfo {
00899     RegisterMCAsmInfo(Target &T) {
00900       TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
00901     }
00902   private:
00903     static MCAsmInfo *Allocator(const MCRegisterInfo &/*MRI*/, StringRef TT) {
00904       return new MCAsmInfoImpl(TT);
00905     }
00906 
00907   };
00908 
00909   /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
00910   /// implementation.  This invokes the specified function to do the
00911   /// construction.  Usage:
00912   ///
00913   /// extern "C" void LLVMInitializeFooTarget() {
00914   ///   extern Target TheFooTarget;
00915   ///   RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
00916   /// }
00917   struct RegisterMCAsmInfoFn {
00918     RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
00919       TargetRegistry::RegisterMCAsmInfo(T, Fn);
00920     }
00921   };
00922 
00923   /// RegisterMCCodeGenInfo - Helper template for registering a target codegen info
00924   /// implementation.  This invokes the static "Create" method on the class
00925   /// to actually do the construction.  Usage:
00926   ///
00927   /// extern "C" void LLVMInitializeFooTarget() {
00928   ///   extern Target TheFooTarget;
00929   ///   RegisterMCCodeGenInfo<FooMCCodeGenInfo> X(TheFooTarget);
00930   /// }
00931   template<class MCCodeGenInfoImpl>
00932   struct RegisterMCCodeGenInfo {
00933     RegisterMCCodeGenInfo(Target &T) {
00934       TargetRegistry::RegisterMCCodeGenInfo(T, &Allocator);
00935     }
00936   private:
00937     static MCCodeGenInfo *Allocator(StringRef /*TT*/, Reloc::Model /*RM*/,
00938                                     CodeModel::Model /*CM*/,
00939                                     CodeGenOpt::Level /*OL*/) {
00940       return new MCCodeGenInfoImpl();
00941     }
00942   };
00943 
00944   /// RegisterMCCodeGenInfoFn - Helper template for registering a target codegen
00945   /// info implementation.  This invokes the specified function to do the
00946   /// construction.  Usage:
00947   ///
00948   /// extern "C" void LLVMInitializeFooTarget() {
00949   ///   extern Target TheFooTarget;
00950   ///   RegisterMCCodeGenInfoFn X(TheFooTarget, TheFunction);
00951   /// }
00952   struct RegisterMCCodeGenInfoFn {
00953     RegisterMCCodeGenInfoFn(Target &T, Target::MCCodeGenInfoCtorFnTy Fn) {
00954       TargetRegistry::RegisterMCCodeGenInfo(T, Fn);
00955     }
00956   };
00957 
00958   /// RegisterMCInstrInfo - Helper template for registering a target instruction
00959   /// info implementation.  This invokes the static "Create" method on the class
00960   /// to actually do the construction.  Usage:
00961   ///
00962   /// extern "C" void LLVMInitializeFooTarget() {
00963   ///   extern Target TheFooTarget;
00964   ///   RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
00965   /// }
00966   template<class MCInstrInfoImpl>
00967   struct RegisterMCInstrInfo {
00968     RegisterMCInstrInfo(Target &T) {
00969       TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
00970     }
00971   private:
00972     static MCInstrInfo *Allocator() {
00973       return new MCInstrInfoImpl();
00974     }
00975   };
00976 
00977   /// RegisterMCInstrInfoFn - Helper template for registering a target
00978   /// instruction info implementation.  This invokes the specified function to
00979   /// do the construction.  Usage:
00980   ///
00981   /// extern "C" void LLVMInitializeFooTarget() {
00982   ///   extern Target TheFooTarget;
00983   ///   RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
00984   /// }
00985   struct RegisterMCInstrInfoFn {
00986     RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
00987       TargetRegistry::RegisterMCInstrInfo(T, Fn);
00988     }
00989   };
00990 
00991   /// RegisterMCInstrAnalysis - Helper template for registering a target
00992   /// instruction analyzer implementation.  This invokes the static "Create"
00993   /// method on the class to actually do the construction.  Usage:
00994   ///
00995   /// extern "C" void LLVMInitializeFooTarget() {
00996   ///   extern Target TheFooTarget;
00997   ///   RegisterMCInstrAnalysis<FooMCInstrAnalysis> X(TheFooTarget);
00998   /// }
00999   template<class MCInstrAnalysisImpl>
01000   struct RegisterMCInstrAnalysis {
01001     RegisterMCInstrAnalysis(Target &T) {
01002       TargetRegistry::RegisterMCInstrAnalysis(T, &Allocator);
01003     }
01004   private:
01005     static MCInstrAnalysis *Allocator(const MCInstrInfo *Info) {
01006       return new MCInstrAnalysisImpl(Info);
01007     }
01008   };
01009 
01010   /// RegisterMCInstrAnalysisFn - Helper template for registering a target
01011   /// instruction analyzer implementation.  This invokes the specified function
01012   /// to do the construction.  Usage:
01013   ///
01014   /// extern "C" void LLVMInitializeFooTarget() {
01015   ///   extern Target TheFooTarget;
01016   ///   RegisterMCInstrAnalysisFn X(TheFooTarget, TheFunction);
01017   /// }
01018   struct RegisterMCInstrAnalysisFn {
01019     RegisterMCInstrAnalysisFn(Target &T, Target::MCInstrAnalysisCtorFnTy Fn) {
01020       TargetRegistry::RegisterMCInstrAnalysis(T, Fn);
01021     }
01022   };
01023 
01024   /// RegisterMCRegInfo - Helper template for registering a target register info
01025   /// implementation.  This invokes the static "Create" method on the class to
01026   /// actually do the construction.  Usage:
01027   ///
01028   /// extern "C" void LLVMInitializeFooTarget() {
01029   ///   extern Target TheFooTarget;
01030   ///   RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
01031   /// }
01032   template<class MCRegisterInfoImpl>
01033   struct RegisterMCRegInfo {
01034     RegisterMCRegInfo(Target &T) {
01035       TargetRegistry::RegisterMCRegInfo(T, &Allocator);
01036     }
01037   private:
01038     static MCRegisterInfo *Allocator(StringRef /*TT*/) {
01039       return new MCRegisterInfoImpl();
01040     }
01041   };
01042 
01043   /// RegisterMCRegInfoFn - Helper template for registering a target register
01044   /// info implementation.  This invokes the specified function to do the
01045   /// construction.  Usage:
01046   ///
01047   /// extern "C" void LLVMInitializeFooTarget() {
01048   ///   extern Target TheFooTarget;
01049   ///   RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
01050   /// }
01051   struct RegisterMCRegInfoFn {
01052     RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
01053       TargetRegistry::RegisterMCRegInfo(T, Fn);
01054     }
01055   };
01056 
01057   /// RegisterMCSubtargetInfo - Helper template for registering a target
01058   /// subtarget info implementation.  This invokes the static "Create" method
01059   /// on the class to actually do the construction.  Usage:
01060   ///
01061   /// extern "C" void LLVMInitializeFooTarget() {
01062   ///   extern Target TheFooTarget;
01063   ///   RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
01064   /// }
01065   template<class MCSubtargetInfoImpl>
01066   struct RegisterMCSubtargetInfo {
01067     RegisterMCSubtargetInfo(Target &T) {
01068       TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
01069     }
01070   private:
01071     static MCSubtargetInfo *Allocator(StringRef /*TT*/, StringRef /*CPU*/,
01072                                       StringRef /*FS*/) {
01073       return new MCSubtargetInfoImpl();
01074     }
01075   };
01076 
01077   /// RegisterMCSubtargetInfoFn - Helper template for registering a target
01078   /// subtarget info implementation.  This invokes the specified function to
01079   /// do the construction.  Usage:
01080   ///
01081   /// extern "C" void LLVMInitializeFooTarget() {
01082   ///   extern Target TheFooTarget;
01083   ///   RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
01084   /// }
01085   struct RegisterMCSubtargetInfoFn {
01086     RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
01087       TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
01088     }
01089   };
01090 
01091   /// RegisterTargetMachine - Helper template for registering a target machine
01092   /// implementation, for use in the target machine initialization
01093   /// function. Usage:
01094   ///
01095   /// extern "C" void LLVMInitializeFooTarget() {
01096   ///   extern Target TheFooTarget;
01097   ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
01098   /// }
01099   template<class TargetMachineImpl>
01100   struct RegisterTargetMachine {
01101     RegisterTargetMachine(Target &T) {
01102       TargetRegistry::RegisterTargetMachine(T, &Allocator);
01103     }
01104 
01105   private:
01106     static TargetMachine *Allocator(const Target &T, StringRef TT,
01107                                     StringRef CPU, StringRef FS,
01108                                     const TargetOptions &Options,
01109                                     Reloc::Model RM,
01110                                     CodeModel::Model CM,
01111                                     CodeGenOpt::Level OL) {
01112       return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL);
01113     }
01114   };
01115 
01116   /// RegisterMCAsmBackend - Helper template for registering a target specific
01117   /// assembler backend. Usage:
01118   ///
01119   /// extern "C" void LLVMInitializeFooMCAsmBackend() {
01120   ///   extern Target TheFooTarget;
01121   ///   RegisterMCAsmBackend<FooAsmLexer> X(TheFooTarget);
01122   /// }
01123   template<class MCAsmBackendImpl>
01124   struct RegisterMCAsmBackend {
01125     RegisterMCAsmBackend(Target &T) {
01126       TargetRegistry::RegisterMCAsmBackend(T, &Allocator);
01127     }
01128 
01129   private:
01130     static MCAsmBackend *Allocator(const Target &T,
01131                                    const MCRegisterInfo &MRI,
01132                                    StringRef Triple, StringRef CPU) {
01133       return new MCAsmBackendImpl(T, MRI, Triple, CPU);
01134     }
01135   };
01136 
01137   /// RegisterMCAsmParser - Helper template for registering a target specific
01138   /// assembly parser, for use in the target machine initialization
01139   /// function. Usage:
01140   ///
01141   /// extern "C" void LLVMInitializeFooMCAsmParser() {
01142   ///   extern Target TheFooTarget;
01143   ///   RegisterMCAsmParser<FooAsmParser> X(TheFooTarget);
01144   /// }
01145   template<class MCAsmParserImpl>
01146   struct RegisterMCAsmParser {
01147     RegisterMCAsmParser(Target &T) {
01148       TargetRegistry::RegisterMCAsmParser(T, &Allocator);
01149     }
01150 
01151   private:
01152     static MCTargetAsmParser *Allocator(MCSubtargetInfo &STI, MCAsmParser &P,
01153                                         const MCInstrInfo &MII,
01154                                         const MCTargetOptions &Options) {
01155       return new MCAsmParserImpl(STI, P, MII, Options);
01156     }
01157   };
01158 
01159   /// RegisterAsmPrinter - Helper template for registering a target specific
01160   /// assembly printer, for use in the target machine initialization
01161   /// function. Usage:
01162   ///
01163   /// extern "C" void LLVMInitializeFooAsmPrinter() {
01164   ///   extern Target TheFooTarget;
01165   ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
01166   /// }
01167   template<class AsmPrinterImpl>
01168   struct RegisterAsmPrinter {
01169     RegisterAsmPrinter(Target &T) {
01170       TargetRegistry::RegisterAsmPrinter(T, &Allocator);
01171     }
01172 
01173   private:
01174     static AsmPrinter *Allocator(TargetMachine &TM,
01175                                  std::unique_ptr<MCStreamer> &&Streamer) {
01176       return new AsmPrinterImpl(TM, std::move(Streamer));
01177     }
01178   };
01179 
01180   /// RegisterMCCodeEmitter - Helper template for registering a target specific
01181   /// machine code emitter, for use in the target initialization
01182   /// function. Usage:
01183   ///
01184   /// extern "C" void LLVMInitializeFooMCCodeEmitter() {
01185   ///   extern Target TheFooTarget;
01186   ///   RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget);
01187   /// }
01188   template<class MCCodeEmitterImpl>
01189   struct RegisterMCCodeEmitter {
01190     RegisterMCCodeEmitter(Target &T) {
01191       TargetRegistry::RegisterMCCodeEmitter(T, &Allocator);
01192     }
01193 
01194   private:
01195     static MCCodeEmitter *Allocator(const MCInstrInfo & /*II*/,
01196                                     const MCRegisterInfo & /*MRI*/,
01197                                     MCContext & /*Ctx*/) {
01198       return new MCCodeEmitterImpl();
01199     }
01200   };
01201 
01202 }
01203 
01204 #endif