Home | History | Annotate | Download | only in Support
      1 //===- Support/TargetRegistry.h - Target Registration -----------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file exposes the TargetRegistry interface, which tools can use to access
     11 // the appropriate target specific classes (TargetMachine, AsmPrinter, etc.)
     12 // which have been registered.
     13 //
     14 // Target specific class implementations should register themselves using the
     15 // appropriate TargetRegistry interfaces.
     16 //
     17 //===----------------------------------------------------------------------===//
     18 
     19 #ifndef LLVM_SUPPORT_TARGETREGISTRY_H
     20 #define LLVM_SUPPORT_TARGETREGISTRY_H
     21 
     22 #include "llvm-c/Disassembler.h"
     23 #include "llvm/ADT/Optional.h"
     24 #include "llvm/ADT/StringRef.h"
     25 #include "llvm/ADT/Triple.h"
     26 #include "llvm/ADT/iterator_range.h"
     27 #include "llvm/Support/CodeGen.h"
     28 #include "llvm/Support/ErrorHandling.h"
     29 #include "llvm/Support/FormattedStream.h"
     30 #include <algorithm>
     31 #include <cassert>
     32 #include <cstddef>
     33 #include <iterator>
     34 #include <memory>
     35 #include <string>
     36 
     37 namespace llvm {
     38 
     39 class AsmPrinter;
     40 class MCAsmBackend;
     41 class MCAsmInfo;
     42 class MCAsmParser;
     43 class MCCodeEmitter;
     44 class MCContext;
     45 class MCDisassembler;
     46 class MCInstPrinter;
     47 class MCInstrAnalysis;
     48 class MCInstrInfo;
     49 class MCRegisterInfo;
     50 class MCRelocationInfo;
     51 class MCStreamer;
     52 class MCSubtargetInfo;
     53 class MCSymbolizer;
     54 class MCTargetAsmParser;
     55 class MCTargetOptions;
     56 class MCTargetStreamer;
     57 class raw_ostream;
     58 class raw_pwrite_stream;
     59 class TargetMachine;
     60 class TargetOptions;
     61 
     62 MCStreamer *createNullStreamer(MCContext &Ctx);
     63 MCStreamer *createAsmStreamer(MCContext &Ctx,
     64                               std::unique_ptr<formatted_raw_ostream> OS,
     65                               bool isVerboseAsm, bool useDwarfDirectory,
     66                               MCInstPrinter *InstPrint, MCCodeEmitter *CE,
     67                               MCAsmBackend *TAB, bool ShowInst);
     68 
     69 /// Takes ownership of \p TAB and \p CE.
     70 MCStreamer *createELFStreamer(MCContext &Ctx,
     71                               std::unique_ptr<MCAsmBackend> &&TAB,
     72                               raw_pwrite_stream &OS,
     73                               std::unique_ptr<MCCodeEmitter> &&CE,
     74                               bool RelaxAll);
     75 MCStreamer *createMachOStreamer(MCContext &Ctx,
     76                                 std::unique_ptr<MCAsmBackend> &&TAB,
     77                                 raw_pwrite_stream &OS,
     78                                 std::unique_ptr<MCCodeEmitter> &&CE,
     79                                 bool RelaxAll, bool DWARFMustBeAtTheEnd,
     80                                 bool LabelSections = false);
     81 MCStreamer *createWasmStreamer(MCContext &Ctx,
     82                                std::unique_ptr<MCAsmBackend> &&TAB,
     83                                raw_pwrite_stream &OS,
     84                                std::unique_ptr<MCCodeEmitter> &&CE,
     85                                bool RelaxAll);
     86 
     87 MCRelocationInfo *createMCRelocationInfo(const Triple &TT, MCContext &Ctx);
     88 
     89 MCSymbolizer *createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo,
     90                                  LLVMSymbolLookupCallback SymbolLookUp,
     91                                  void *DisInfo, MCContext *Ctx,
     92                                  std::unique_ptr<MCRelocationInfo> &&RelInfo);
     93 
     94 /// Target - Wrapper for Target specific information.
     95 ///
     96 /// For registration purposes, this is a POD type so that targets can be
     97 /// registered without the use of static constructors.
     98 ///
     99 /// Targets should implement a single global instance of this class (which
    100 /// will be zero initialized), and pass that instance to the TargetRegistry as
    101 /// part of their initialization.
    102 class Target {
    103 public:
    104   friend struct TargetRegistry;
    105 
    106   using ArchMatchFnTy = bool (*)(Triple::ArchType Arch);
    107 
    108   using MCAsmInfoCtorFnTy = MCAsmInfo *(*)(const MCRegisterInfo &MRI,
    109                                            const Triple &TT);
    110   using MCInstrInfoCtorFnTy = MCInstrInfo *(*)();
    111   using MCInstrAnalysisCtorFnTy = MCInstrAnalysis *(*)(const MCInstrInfo *Info);
    112   using MCRegInfoCtorFnTy = MCRegisterInfo *(*)(const Triple &TT);
    113   using MCSubtargetInfoCtorFnTy = MCSubtargetInfo *(*)(const Triple &TT,
    114                                                        StringRef CPU,
    115                                                        StringRef Features);
    116   using TargetMachineCtorTy = TargetMachine
    117       *(*)(const Target &T, const Triple &TT, StringRef CPU, StringRef Features,
    118            const TargetOptions &Options, Optional<Reloc::Model> RM,
    119            Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT);
    120   // If it weren't for layering issues (this header is in llvm/Support, but
    121   // depends on MC?) this should take the Streamer by value rather than rvalue
    122   // reference.
    123   using AsmPrinterCtorTy = AsmPrinter *(*)(
    124       TargetMachine &TM, std::unique_ptr<MCStreamer> &&Streamer);
    125   using MCAsmBackendCtorTy = MCAsmBackend *(*)(const Target &T,
    126                                                const MCRegisterInfo &MRI,
    127                                                const Triple &TT, StringRef CPU,
    128                                                const MCTargetOptions &Options);
    129   using MCAsmParserCtorTy = MCTargetAsmParser *(*)(
    130       const MCSubtargetInfo &STI, MCAsmParser &P, const MCInstrInfo &MII,
    131       const MCTargetOptions &Options);
    132   using MCDisassemblerCtorTy = MCDisassembler *(*)(const Target &T,
    133                                                    const MCSubtargetInfo &STI,
    134                                                    MCContext &Ctx);
    135   using MCInstPrinterCtorTy = MCInstPrinter *(*)(const Triple &T,
    136                                                  unsigned SyntaxVariant,
    137                                                  const MCAsmInfo &MAI,
    138                                                  const MCInstrInfo &MII,
    139                                                  const MCRegisterInfo &MRI);
    140   using MCCodeEmitterCtorTy = MCCodeEmitter *(*)(const MCInstrInfo &II,
    141                                                  const MCRegisterInfo &MRI,
    142                                                  MCContext &Ctx);
    143   using ELFStreamerCtorTy =
    144       MCStreamer *(*)(const Triple &T, MCContext &Ctx,
    145                       std::unique_ptr<MCAsmBackend> &&TAB,
    146                       raw_pwrite_stream &OS,
    147                       std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
    148   using MachOStreamerCtorTy =
    149       MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB,
    150                       raw_pwrite_stream &OS,
    151                       std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
    152                       bool DWARFMustBeAtTheEnd);
    153   using COFFStreamerCtorTy =
    154       MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB,
    155                       raw_pwrite_stream &OS,
    156                       std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
    157                       bool IncrementalLinkerCompatible);
    158   using WasmStreamerCtorTy =
    159       MCStreamer *(*)(const Triple &T, MCContext &Ctx,
    160                       std::unique_ptr<MCAsmBackend> &&TAB,
    161                       raw_pwrite_stream &OS,
    162                       std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
    163   using NullTargetStreamerCtorTy = MCTargetStreamer *(*)(MCStreamer &S);
    164   using AsmTargetStreamerCtorTy = MCTargetStreamer *(*)(
    165       MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint,
    166       bool IsVerboseAsm);
    167   using ObjectTargetStreamerCtorTy = MCTargetStreamer *(*)(
    168       MCStreamer &S, const MCSubtargetInfo &STI);
    169   using MCRelocationInfoCtorTy = MCRelocationInfo *(*)(const Triple &TT,
    170                                                        MCContext &Ctx);
    171   using MCSymbolizerCtorTy = MCSymbolizer *(*)(
    172       const Triple &TT, LLVMOpInfoCallback GetOpInfo,
    173       LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx,
    174       std::unique_ptr<MCRelocationInfo> &&RelInfo);
    175 
    176 private:
    177   /// Next - The next registered target in the linked list, maintained by the
    178   /// TargetRegistry.
    179   Target *Next;
    180 
    181   /// The target function for checking if an architecture is supported.
    182   ArchMatchFnTy ArchMatchFn;
    183 
    184   /// Name - The target name.
    185   const char *Name;
    186 
    187   /// ShortDesc - A short description of the target.
    188   const char *ShortDesc;
    189 
    190   /// HasJIT - Whether this target supports the JIT.
    191   bool HasJIT;
    192 
    193   /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
    194   /// registered.
    195   MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
    196 
    197   /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
    198   /// if registered.
    199   MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
    200 
    201   /// MCInstrAnalysisCtorFn - Constructor function for this target's
    202   /// MCInstrAnalysis, if registered.
    203   MCInstrAnalysisCtorFnTy MCInstrAnalysisCtorFn;
    204 
    205   /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
    206   /// if registered.
    207   MCRegInfoCtorFnTy MCRegInfoCtorFn;
    208 
    209   /// MCSubtargetInfoCtorFn - Constructor function for this target's
    210   /// MCSubtargetInfo, if registered.
    211   MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn;
    212 
    213   /// TargetMachineCtorFn - Construction function for this target's
    214   /// TargetMachine, if registered.
    215   TargetMachineCtorTy TargetMachineCtorFn;
    216 
    217   /// MCAsmBackendCtorFn - Construction function for this target's
    218   /// MCAsmBackend, if registered.
    219   MCAsmBackendCtorTy MCAsmBackendCtorFn;
    220 
    221   /// MCAsmParserCtorFn - Construction function for this target's
    222   /// MCTargetAsmParser, if registered.
    223   MCAsmParserCtorTy MCAsmParserCtorFn;
    224 
    225   /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
    226   /// if registered.
    227   AsmPrinterCtorTy AsmPrinterCtorFn;
    228 
    229   /// MCDisassemblerCtorFn - Construction function for this target's
    230   /// MCDisassembler, if registered.
    231   MCDisassemblerCtorTy MCDisassemblerCtorFn;
    232 
    233   /// MCInstPrinterCtorFn - Construction function for this target's
    234   /// MCInstPrinter, if registered.
    235   MCInstPrinterCtorTy MCInstPrinterCtorFn;
    236 
    237   /// MCCodeEmitterCtorFn - Construction function for this target's
    238   /// CodeEmitter, if registered.
    239   MCCodeEmitterCtorTy MCCodeEmitterCtorFn;
    240 
    241   // Construction functions for the various object formats, if registered.
    242   COFFStreamerCtorTy COFFStreamerCtorFn = nullptr;
    243   MachOStreamerCtorTy MachOStreamerCtorFn = nullptr;
    244   ELFStreamerCtorTy ELFStreamerCtorFn = nullptr;
    245   WasmStreamerCtorTy WasmStreamerCtorFn = nullptr;
    246 
    247   /// Construction function for this target's null TargetStreamer, if
    248   /// registered (default = nullptr).
    249   NullTargetStreamerCtorTy NullTargetStreamerCtorFn = nullptr;
    250 
    251   /// Construction function for this target's asm TargetStreamer, if
    252   /// registered (default = nullptr).
    253   AsmTargetStreamerCtorTy AsmTargetStreamerCtorFn = nullptr;
    254 
    255   /// Construction function for this target's obj TargetStreamer, if
    256   /// registered (default = nullptr).
    257   ObjectTargetStreamerCtorTy ObjectTargetStreamerCtorFn = nullptr;
    258 
    259   /// MCRelocationInfoCtorFn - Construction function for this target's
    260   /// MCRelocationInfo, if registered (default = llvm::createMCRelocationInfo)
    261   MCRelocationInfoCtorTy MCRelocationInfoCtorFn = nullptr;
    262 
    263   /// MCSymbolizerCtorFn - Construction function for this target's
    264   /// MCSymbolizer, if registered (default = llvm::createMCSymbolizer)
    265   MCSymbolizerCtorTy MCSymbolizerCtorFn = nullptr;
    266 
    267 public:
    268   Target() = default;
    269 
    270   /// @name Target Information
    271   /// @{
    272 
    273   // getNext - Return the next registered target.
    274   const Target *getNext() const { return Next; }
    275 
    276   /// getName - Get the target name.
    277   const char *getName() const { return Name; }
    278 
    279   /// getShortDescription - Get a short description of the target.
    280   const char *getShortDescription() const { return ShortDesc; }
    281 
    282   /// @}
    283   /// @name Feature Predicates
    284   /// @{
    285 
    286   /// hasJIT - Check if this targets supports the just-in-time compilation.
    287   bool hasJIT() const { return HasJIT; }
    288 
    289   /// hasTargetMachine - Check if this target supports code generation.
    290   bool hasTargetMachine() const { return TargetMachineCtorFn != nullptr; }
    291 
    292   /// hasMCAsmBackend - Check if this target supports .o generation.
    293   bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != nullptr; }
    294 
    295   /// hasMCAsmParser - Check if this target supports assembly parsing.
    296   bool hasMCAsmParser() const { return MCAsmParserCtorFn != nullptr; }
    297 
    298   /// @}
    299   /// @name Feature Constructors
    300   /// @{
    301 
    302   /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
    303   /// target triple.
    304   ///
    305   /// \param TheTriple This argument is used to determine the target machine
    306   /// feature set; it should always be provided. Generally this should be
    307   /// either the target triple from the module, or the target triple of the
    308   /// host if that does not exist.
    309   MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI,
    310                              StringRef TheTriple) const {
    311     if (!MCAsmInfoCtorFn)
    312       return nullptr;
    313     return MCAsmInfoCtorFn(MRI, Triple(TheTriple));
    314   }
    315 
    316   /// createMCInstrInfo - Create a MCInstrInfo implementation.
    317   ///
    318   MCInstrInfo *createMCInstrInfo() const {
    319     if (!MCInstrInfoCtorFn)
    320       return nullptr;
    321     return MCInstrInfoCtorFn();
    322   }
    323 
    324   /// createMCInstrAnalysis - Create a MCInstrAnalysis implementation.
    325   ///
    326   MCInstrAnalysis *createMCInstrAnalysis(const MCInstrInfo *Info) const {
    327     if (!MCInstrAnalysisCtorFn)
    328       return nullptr;
    329     return MCInstrAnalysisCtorFn(Info);
    330   }
    331 
    332   /// createMCRegInfo - Create a MCRegisterInfo implementation.
    333   ///
    334   MCRegisterInfo *createMCRegInfo(StringRef TT) const {
    335     if (!MCRegInfoCtorFn)
    336       return nullptr;
    337     return MCRegInfoCtorFn(Triple(TT));
    338   }
    339 
    340   /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
    341   ///
    342   /// \param TheTriple This argument is used to determine the target machine
    343   /// feature set; it should always be provided. Generally this should be
    344   /// either the target triple from the module, or the target triple of the
    345   /// host if that does not exist.
    346   /// \param CPU This specifies the name of the target CPU.
    347   /// \param Features This specifies the string representation of the
    348   /// additional target features.
    349   MCSubtargetInfo *createMCSubtargetInfo(StringRef TheTriple, StringRef CPU,
    350                                          StringRef Features) const {
    351     if (!MCSubtargetInfoCtorFn)
    352       return nullptr;
    353     return MCSubtargetInfoCtorFn(Triple(TheTriple), CPU, Features);
    354   }
    355 
    356   /// createTargetMachine - Create a target specific machine implementation
    357   /// for the specified \p Triple.
    358   ///
    359   /// \param TT This argument is used to determine the target machine
    360   /// feature set; it should always be provided. Generally this should be
    361   /// either the target triple from the module, or the target triple of the
    362   /// host if that does not exist.
    363   TargetMachine *createTargetMachine(StringRef TT, StringRef CPU,
    364                                      StringRef Features,
    365                                      const TargetOptions &Options,
    366                                      Optional<Reloc::Model> RM,
    367                                      Optional<CodeModel::Model> CM = None,
    368                                      CodeGenOpt::Level OL = CodeGenOpt::Default,
    369                                      bool JIT = false) const {
    370     if (!TargetMachineCtorFn)
    371       return nullptr;
    372     return TargetMachineCtorFn(*this, Triple(TT), CPU, Features, Options, RM,
    373                                CM, OL, JIT);
    374   }
    375 
    376   /// createMCAsmBackend - Create a target specific assembly parser.
    377   ///
    378   /// \param TheTriple The target triple string.
    379   MCAsmBackend *createMCAsmBackend(const MCRegisterInfo &MRI,
    380                                    StringRef TheTriple, StringRef CPU,
    381                                    const MCTargetOptions &Options)
    382                                    const {
    383     if (!MCAsmBackendCtorFn)
    384       return nullptr;
    385     return MCAsmBackendCtorFn(*this, MRI, Triple(TheTriple), CPU, Options);
    386   }
    387 
    388   /// createMCAsmParser - Create a target specific assembly parser.
    389   ///
    390   /// \param Parser The target independent parser implementation to use for
    391   /// parsing and lexing.
    392   MCTargetAsmParser *createMCAsmParser(const MCSubtargetInfo &STI,
    393                                        MCAsmParser &Parser,
    394                                        const MCInstrInfo &MII,
    395                                        const MCTargetOptions &Options) const {
    396     if (!MCAsmParserCtorFn)
    397       return nullptr;
    398     return MCAsmParserCtorFn(STI, Parser, MII, Options);
    399   }
    400 
    401   /// createAsmPrinter - Create a target specific assembly printer pass.  This
    402   /// takes ownership of the MCStreamer object.
    403   AsmPrinter *createAsmPrinter(TargetMachine &TM,
    404                                std::unique_ptr<MCStreamer> &&Streamer) const {
    405     if (!AsmPrinterCtorFn)
    406       return nullptr;
    407     return AsmPrinterCtorFn(TM, std::move(Streamer));
    408   }
    409 
    410   MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI,
    411                                        MCContext &Ctx) const {
    412     if (!MCDisassemblerCtorFn)
    413       return nullptr;
    414     return MCDisassemblerCtorFn(*this, STI, Ctx);
    415   }
    416 
    417   MCInstPrinter *createMCInstPrinter(const Triple &T, unsigned SyntaxVariant,
    418                                      const MCAsmInfo &MAI,
    419                                      const MCInstrInfo &MII,
    420                                      const MCRegisterInfo &MRI) const {
    421     if (!MCInstPrinterCtorFn)
    422       return nullptr;
    423     return MCInstPrinterCtorFn(T, SyntaxVariant, MAI, MII, MRI);
    424   }
    425 
    426   /// createMCCodeEmitter - Create a target specific code emitter.
    427   MCCodeEmitter *createMCCodeEmitter(const MCInstrInfo &II,
    428                                      const MCRegisterInfo &MRI,
    429                                      MCContext &Ctx) const {
    430     if (!MCCodeEmitterCtorFn)
    431       return nullptr;
    432     return MCCodeEmitterCtorFn(II, MRI, Ctx);
    433   }
    434 
    435   /// Create a target specific MCStreamer.
    436   ///
    437   /// \param T The target triple.
    438   /// \param Ctx The target context.
    439   /// \param TAB The target assembler backend object. Takes ownership.
    440   /// \param OS The stream object.
    441   /// \param Emitter The target independent assembler object.Takes ownership.
    442   /// \param RelaxAll Relax all fixups?
    443   MCStreamer *createMCObjectStreamer(const Triple &T, MCContext &Ctx,
    444                                      std::unique_ptr<MCAsmBackend> &&TAB,
    445                                      raw_pwrite_stream &OS,
    446                                      std::unique_ptr<MCCodeEmitter> &&Emitter,
    447                                      const MCSubtargetInfo &STI, bool RelaxAll,
    448                                      bool IncrementalLinkerCompatible,
    449                                      bool DWARFMustBeAtTheEnd) const {
    450     MCStreamer *S;
    451     switch (T.getObjectFormat()) {
    452     default:
    453       llvm_unreachable("Unknown object format");
    454     case Triple::COFF:
    455       assert(T.isOSWindows() && "only Windows COFF is supported");
    456       S = COFFStreamerCtorFn(Ctx, std::move(TAB), OS, std::move(Emitter),
    457                              RelaxAll, IncrementalLinkerCompatible);
    458       break;
    459     case Triple::MachO:
    460       if (MachOStreamerCtorFn)
    461         S = MachOStreamerCtorFn(Ctx, std::move(TAB), OS, std::move(Emitter),
    462                                 RelaxAll, DWARFMustBeAtTheEnd);
    463       else
    464         S = createMachOStreamer(Ctx, std::move(TAB), OS, std::move(Emitter),
    465                                 RelaxAll, DWARFMustBeAtTheEnd);
    466       break;
    467     case Triple::ELF:
    468       if (ELFStreamerCtorFn)
    469         S = ELFStreamerCtorFn(T, Ctx, std::move(TAB), OS, std::move(Emitter),
    470                               RelaxAll);
    471       else
    472         S = createELFStreamer(Ctx, std::move(TAB), OS, std::move(Emitter),
    473                               RelaxAll);
    474       break;
    475     case Triple::Wasm:
    476       if (WasmStreamerCtorFn)
    477         S = WasmStreamerCtorFn(T, Ctx, std::move(TAB), OS, std::move(Emitter),
    478                                RelaxAll);
    479       else
    480         S = createWasmStreamer(Ctx, std::move(TAB), OS, std::move(Emitter),
    481                                RelaxAll);
    482       break;
    483     }
    484     if (ObjectTargetStreamerCtorFn)
    485       ObjectTargetStreamerCtorFn(*S, STI);
    486     return S;
    487   }
    488 
    489   MCStreamer *createAsmStreamer(MCContext &Ctx,
    490                                 std::unique_ptr<formatted_raw_ostream> OS,
    491                                 bool IsVerboseAsm, bool UseDwarfDirectory,
    492                                 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
    493                                 MCAsmBackend *TAB, bool ShowInst) const {
    494     formatted_raw_ostream &OSRef = *OS;
    495     MCStreamer *S = llvm::createAsmStreamer(Ctx, std::move(OS), IsVerboseAsm,
    496                                             UseDwarfDirectory, InstPrint, CE,
    497                                             TAB, ShowInst);
    498     createAsmTargetStreamer(*S, OSRef, InstPrint, IsVerboseAsm);
    499     return S;
    500   }
    501 
    502   MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
    503                                             formatted_raw_ostream &OS,
    504                                             MCInstPrinter *InstPrint,
    505                                             bool IsVerboseAsm) const {
    506     if (AsmTargetStreamerCtorFn)
    507       return AsmTargetStreamerCtorFn(S, OS, InstPrint, IsVerboseAsm);
    508     return nullptr;
    509   }
    510 
    511   MCStreamer *createNullStreamer(MCContext &Ctx) const {
    512     MCStreamer *S = llvm::createNullStreamer(Ctx);
    513     createNullTargetStreamer(*S);
    514     return S;
    515   }
    516 
    517   MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) const {
    518     if (NullTargetStreamerCtorFn)
    519       return NullTargetStreamerCtorFn(S);
    520     return nullptr;
    521   }
    522 
    523   /// createMCRelocationInfo - Create a target specific MCRelocationInfo.
    524   ///
    525   /// \param TT The target triple.
    526   /// \param Ctx The target context.
    527   MCRelocationInfo *createMCRelocationInfo(StringRef TT, MCContext &Ctx) const {
    528     MCRelocationInfoCtorTy Fn = MCRelocationInfoCtorFn
    529                                     ? MCRelocationInfoCtorFn
    530                                     : llvm::createMCRelocationInfo;
    531     return Fn(Triple(TT), Ctx);
    532   }
    533 
    534   /// createMCSymbolizer - Create a target specific MCSymbolizer.
    535   ///
    536   /// \param TT The target triple.
    537   /// \param GetOpInfo The function to get the symbolic information for
    538   /// operands.
    539   /// \param SymbolLookUp The function to lookup a symbol name.
    540   /// \param DisInfo The pointer to the block of symbolic information for above
    541   /// call
    542   /// back.
    543   /// \param Ctx The target context.
    544   /// \param RelInfo The relocation information for this target. Takes
    545   /// ownership.
    546   MCSymbolizer *
    547   createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
    548                      LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo,
    549                      MCContext *Ctx,
    550                      std::unique_ptr<MCRelocationInfo> &&RelInfo) const {
    551     MCSymbolizerCtorTy Fn =
    552         MCSymbolizerCtorFn ? MCSymbolizerCtorFn : llvm::createMCSymbolizer;
    553     return Fn(Triple(TT), GetOpInfo, SymbolLookUp, DisInfo, Ctx,
    554               std::move(RelInfo));
    555   }
    556 
    557   /// @}
    558 };
    559 
    560 /// TargetRegistry - Generic interface to target specific features.
    561 struct TargetRegistry {
    562   // FIXME: Make this a namespace, probably just move all the Register*
    563   // functions into Target (currently they all just set members on the Target
    564   // anyway, and Target friends this class so those functions can...
    565   // function).
    566   TargetRegistry() = delete;
    567 
    568   class iterator
    569       : public std::iterator<std::forward_iterator_tag, Target, ptrdiff_t> {
    570     friend struct TargetRegistry;
    571 
    572     const Target *Current = nullptr;
    573 
    574     explicit iterator(Target *T) : Current(T) {}
    575 
    576   public:
    577     iterator() = default;
    578 
    579     bool operator==(const iterator &x) const { return Current == x.Current; }
    580     bool operator!=(const iterator &x) const { return !operator==(x); }
    581 
    582     // Iterator traversal: forward iteration only
    583     iterator &operator++() { // Preincrement
    584       assert(Current && "Cannot increment end iterator!");
    585       Current = Current->getNext();
    586       return *this;
    587     }
    588     iterator operator++(int) { // Postincrement
    589       iterator tmp = *this;
    590       ++*this;
    591       return tmp;
    592     }
    593 
    594     const Target &operator*() const {
    595       assert(Current && "Cannot dereference end iterator!");
    596       return *Current;
    597     }
    598 
    599     const Target *operator->() const { return &operator*(); }
    600   };
    601 
    602   /// printRegisteredTargetsForVersion - Print the registered targets
    603   /// appropriately for inclusion in a tool's version output.
    604   static void printRegisteredTargetsForVersion(raw_ostream &OS);
    605 
    606   /// @name Registry Access
    607   /// @{
    608 
    609   static iterator_range<iterator> targets();
    610 
    611   /// lookupTarget - Lookup a target based on a target triple.
    612   ///
    613   /// \param Triple - The triple to use for finding a target.
    614   /// \param Error - On failure, an error string describing why no target was
    615   /// found.
    616   static const Target *lookupTarget(const std::string &Triple,
    617                                     std::string &Error);
    618 
    619   /// lookupTarget - Lookup a target based on an architecture name
    620   /// and a target triple.  If the architecture name is non-empty,
    621   /// then the lookup is done by architecture.  Otherwise, the target
    622   /// triple is used.
    623   ///
    624   /// \param ArchName - The architecture to use for finding a target.
    625   /// \param TheTriple - The triple to use for finding a target.  The
    626   /// triple is updated with canonical architecture name if a lookup
    627   /// by architecture is done.
    628   /// \param Error - On failure, an error string describing why no target was
    629   /// found.
    630   static const Target *lookupTarget(const std::string &ArchName,
    631                                     Triple &TheTriple, std::string &Error);
    632 
    633   /// @}
    634   /// @name Target Registration
    635   /// @{
    636 
    637   /// RegisterTarget - Register the given target. Attempts to register a
    638   /// target which has already been registered will be ignored.
    639   ///
    640   /// Clients are responsible for ensuring that registration doesn't occur
    641   /// while another thread is attempting to access the registry. Typically
    642   /// this is done by initializing all targets at program startup.
    643   ///
    644   /// @param T - The target being registered.
    645   /// @param Name - The target name. This should be a static string.
    646   /// @param ShortDesc - A short target description. This should be a static
    647   /// string.
    648   /// @param ArchMatchFn - The arch match checking function for this target.
    649   /// @param HasJIT - Whether the target supports JIT code
    650   /// generation.
    651   static void RegisterTarget(Target &T, const char *Name, const char *ShortDesc,
    652                              Target::ArchMatchFnTy ArchMatchFn,
    653                              bool HasJIT = false);
    654 
    655   /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
    656   /// given target.
    657   ///
    658   /// Clients are responsible for ensuring that registration doesn't occur
    659   /// while another thread is attempting to access the registry. Typically
    660   /// this is done by initializing all targets at program startup.
    661   ///
    662   /// @param T - The target being registered.
    663   /// @param Fn - A function to construct a MCAsmInfo for the target.
    664   static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
    665     T.MCAsmInfoCtorFn = Fn;
    666   }
    667 
    668   /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
    669   /// given target.
    670   ///
    671   /// Clients are responsible for ensuring that registration doesn't occur
    672   /// while another thread is attempting to access the registry. Typically
    673   /// this is done by initializing all targets at program startup.
    674   ///
    675   /// @param T - The target being registered.
    676   /// @param Fn - A function to construct a MCInstrInfo for the target.
    677   static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
    678     T.MCInstrInfoCtorFn = Fn;
    679   }
    680 
    681   /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for
    682   /// the given target.
    683   static void RegisterMCInstrAnalysis(Target &T,
    684                                       Target::MCInstrAnalysisCtorFnTy Fn) {
    685     T.MCInstrAnalysisCtorFn = Fn;
    686   }
    687 
    688   /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
    689   /// given target.
    690   ///
    691   /// Clients are responsible for ensuring that registration doesn't occur
    692   /// while another thread is attempting to access the registry. Typically
    693   /// this is done by initializing all targets at program startup.
    694   ///
    695   /// @param T - The target being registered.
    696   /// @param Fn - A function to construct a MCRegisterInfo for the target.
    697   static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
    698     T.MCRegInfoCtorFn = Fn;
    699   }
    700 
    701   /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
    702   /// the given target.
    703   ///
    704   /// Clients are responsible for ensuring that registration doesn't occur
    705   /// while another thread is attempting to access the registry. Typically
    706   /// this is done by initializing all targets at program startup.
    707   ///
    708   /// @param T - The target being registered.
    709   /// @param Fn - A function to construct a MCSubtargetInfo for the target.
    710   static void RegisterMCSubtargetInfo(Target &T,
    711                                       Target::MCSubtargetInfoCtorFnTy Fn) {
    712     T.MCSubtargetInfoCtorFn = Fn;
    713   }
    714 
    715   /// RegisterTargetMachine - Register a TargetMachine implementation for the
    716   /// given target.
    717   ///
    718   /// Clients are responsible for ensuring that registration doesn't occur
    719   /// while another thread is attempting to access the registry. Typically
    720   /// this is done by initializing all targets at program startup.
    721   ///
    722   /// @param T - The target being registered.
    723   /// @param Fn - A function to construct a TargetMachine for the target.
    724   static void RegisterTargetMachine(Target &T, Target::TargetMachineCtorTy Fn) {
    725     T.TargetMachineCtorFn = Fn;
    726   }
    727 
    728   /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the
    729   /// given target.
    730   ///
    731   /// Clients are responsible for ensuring that registration doesn't occur
    732   /// while another thread is attempting to access the registry. Typically
    733   /// this is done by initializing all targets at program startup.
    734   ///
    735   /// @param T - The target being registered.
    736   /// @param Fn - A function to construct an AsmBackend for the target.
    737   static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) {
    738     T.MCAsmBackendCtorFn = Fn;
    739   }
    740 
    741   /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for
    742   /// the given target.
    743   ///
    744   /// Clients are responsible for ensuring that registration doesn't occur
    745   /// while another thread is attempting to access the registry. Typically
    746   /// this is done by initializing all targets at program startup.
    747   ///
    748   /// @param T - The target being registered.
    749   /// @param Fn - A function to construct an MCTargetAsmParser for the target.
    750   static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) {
    751     T.MCAsmParserCtorFn = Fn;
    752   }
    753 
    754   /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
    755   /// target.
    756   ///
    757   /// Clients are responsible for ensuring that registration doesn't occur
    758   /// while another thread is attempting to access the registry. Typically
    759   /// this is done by initializing all targets at program startup.
    760   ///
    761   /// @param T - The target being registered.
    762   /// @param Fn - A function to construct an AsmPrinter for the target.
    763   static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
    764     T.AsmPrinterCtorFn = Fn;
    765   }
    766 
    767   /// RegisterMCDisassembler - Register a MCDisassembler implementation for
    768   /// the given target.
    769   ///
    770   /// Clients are responsible for ensuring that registration doesn't occur
    771   /// while another thread is attempting to access the registry. Typically
    772   /// this is done by initializing all targets at program startup.
    773   ///
    774   /// @param T - The target being registered.
    775   /// @param Fn - A function to construct an MCDisassembler for the target.
    776   static void RegisterMCDisassembler(Target &T,
    777                                      Target::MCDisassemblerCtorTy Fn) {
    778     T.MCDisassemblerCtorFn = Fn;
    779   }
    780 
    781   /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
    782   /// given target.
    783   ///
    784   /// Clients are responsible for ensuring that registration doesn't occur
    785   /// while another thread is attempting to access the registry. Typically
    786   /// this is done by initializing all targets at program startup.
    787   ///
    788   /// @param T - The target being registered.
    789   /// @param Fn - A function to construct an MCInstPrinter for the target.
    790   static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn) {
    791     T.MCInstPrinterCtorFn = Fn;
    792   }
    793 
    794   /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the
    795   /// given target.
    796   ///
    797   /// Clients are responsible for ensuring that registration doesn't occur
    798   /// while another thread is attempting to access the registry. Typically
    799   /// this is done by initializing all targets at program startup.
    800   ///
    801   /// @param T - The target being registered.
    802   /// @param Fn - A function to construct an MCCodeEmitter for the target.
    803   static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn) {
    804     T.MCCodeEmitterCtorFn = Fn;
    805   }
    806 
    807   static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn) {
    808     T.COFFStreamerCtorFn = Fn;
    809   }
    810 
    811   static void RegisterMachOStreamer(Target &T, Target::MachOStreamerCtorTy Fn) {
    812     T.MachOStreamerCtorFn = Fn;
    813   }
    814 
    815   static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn) {
    816     T.ELFStreamerCtorFn = Fn;
    817   }
    818 
    819   static void RegisterWasmStreamer(Target &T, Target::WasmStreamerCtorTy Fn) {
    820     T.WasmStreamerCtorFn = Fn;
    821   }
    822 
    823   static void RegisterNullTargetStreamer(Target &T,
    824                                          Target::NullTargetStreamerCtorTy Fn) {
    825     T.NullTargetStreamerCtorFn = Fn;
    826   }
    827 
    828   static void RegisterAsmTargetStreamer(Target &T,
    829                                         Target::AsmTargetStreamerCtorTy Fn) {
    830     T.AsmTargetStreamerCtorFn = Fn;
    831   }
    832 
    833   static void
    834   RegisterObjectTargetStreamer(Target &T,
    835                                Target::ObjectTargetStreamerCtorTy Fn) {
    836     T.ObjectTargetStreamerCtorFn = Fn;
    837   }
    838 
    839   /// RegisterMCRelocationInfo - Register an MCRelocationInfo
    840   /// implementation for the given target.
    841   ///
    842   /// Clients are responsible for ensuring that registration doesn't occur
    843   /// while another thread is attempting to access the registry. Typically
    844   /// this is done by initializing all targets at program startup.
    845   ///
    846   /// @param T - The target being registered.
    847   /// @param Fn - A function to construct an MCRelocationInfo for the target.
    848   static void RegisterMCRelocationInfo(Target &T,
    849                                        Target::MCRelocationInfoCtorTy Fn) {
    850     T.MCRelocationInfoCtorFn = Fn;
    851   }
    852 
    853   /// RegisterMCSymbolizer - Register an MCSymbolizer
    854   /// implementation for the given target.
    855   ///
    856   /// Clients are responsible for ensuring that registration doesn't occur
    857   /// while another thread is attempting to access the registry. Typically
    858   /// this is done by initializing all targets at program startup.
    859   ///
    860   /// @param T - The target being registered.
    861   /// @param Fn - A function to construct an MCSymbolizer for the target.
    862   static void RegisterMCSymbolizer(Target &T, Target::MCSymbolizerCtorTy Fn) {
    863     T.MCSymbolizerCtorFn = Fn;
    864   }
    865 
    866   /// @}
    867 };
    868 
    869 //===--------------------------------------------------------------------===//
    870 
    871 /// RegisterTarget - Helper template for registering a target, for use in the
    872 /// target's initialization function. Usage:
    873 ///
    874 ///
    875 /// Target &getTheFooTarget() { // The global target instance.
    876 ///   static Target TheFooTarget;
    877 ///   return TheFooTarget;
    878 /// }
    879 /// extern "C" void LLVMInitializeFooTargetInfo() {
    880 ///   RegisterTarget<Triple::foo> X(getTheFooTarget(), "foo", "Foo
    881 ///   description");
    882 /// }
    883 template <Triple::ArchType TargetArchType = Triple::UnknownArch,
    884           bool HasJIT = false>
    885 struct RegisterTarget {
    886   RegisterTarget(Target &T, const char *Name, const char *Desc) {
    887     TargetRegistry::RegisterTarget(T, Name, Desc, &getArchMatch, HasJIT);
    888   }
    889 
    890   static bool getArchMatch(Triple::ArchType Arch) {
    891     return Arch == TargetArchType;
    892   }
    893 };
    894 
    895 /// RegisterMCAsmInfo - Helper template for registering a target assembly info
    896 /// implementation.  This invokes the static "Create" method on the class to
    897 /// actually do the construction.  Usage:
    898 ///
    899 /// extern "C" void LLVMInitializeFooTarget() {
    900 ///   extern Target TheFooTarget;
    901 ///   RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
    902 /// }
    903 template <class MCAsmInfoImpl> struct RegisterMCAsmInfo {
    904   RegisterMCAsmInfo(Target &T) {
    905     TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
    906   }
    907 
    908 private:
    909   static MCAsmInfo *Allocator(const MCRegisterInfo & /*MRI*/,
    910                               const Triple &TT) {
    911     return new MCAsmInfoImpl(TT);
    912   }
    913 };
    914 
    915 /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
    916 /// implementation.  This invokes the specified function to do the
    917 /// construction.  Usage:
    918 ///
    919 /// extern "C" void LLVMInitializeFooTarget() {
    920 ///   extern Target TheFooTarget;
    921 ///   RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
    922 /// }
    923 struct RegisterMCAsmInfoFn {
    924   RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
    925     TargetRegistry::RegisterMCAsmInfo(T, Fn);
    926   }
    927 };
    928 
    929 /// RegisterMCInstrInfo - Helper template for registering a target instruction
    930 /// info implementation.  This invokes the static "Create" method on the class
    931 /// to actually do the construction.  Usage:
    932 ///
    933 /// extern "C" void LLVMInitializeFooTarget() {
    934 ///   extern Target TheFooTarget;
    935 ///   RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
    936 /// }
    937 template <class MCInstrInfoImpl> struct RegisterMCInstrInfo {
    938   RegisterMCInstrInfo(Target &T) {
    939     TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
    940   }
    941 
    942 private:
    943   static MCInstrInfo *Allocator() { return new MCInstrInfoImpl(); }
    944 };
    945 
    946 /// RegisterMCInstrInfoFn - Helper template for registering a target
    947 /// instruction info implementation.  This invokes the specified function to
    948 /// do the construction.  Usage:
    949 ///
    950 /// extern "C" void LLVMInitializeFooTarget() {
    951 ///   extern Target TheFooTarget;
    952 ///   RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
    953 /// }
    954 struct RegisterMCInstrInfoFn {
    955   RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
    956     TargetRegistry::RegisterMCInstrInfo(T, Fn);
    957   }
    958 };
    959 
    960 /// RegisterMCInstrAnalysis - Helper template for registering a target
    961 /// instruction analyzer implementation.  This invokes the static "Create"
    962 /// method on the class to actually do the construction.  Usage:
    963 ///
    964 /// extern "C" void LLVMInitializeFooTarget() {
    965 ///   extern Target TheFooTarget;
    966 ///   RegisterMCInstrAnalysis<FooMCInstrAnalysis> X(TheFooTarget);
    967 /// }
    968 template <class MCInstrAnalysisImpl> struct RegisterMCInstrAnalysis {
    969   RegisterMCInstrAnalysis(Target &T) {
    970     TargetRegistry::RegisterMCInstrAnalysis(T, &Allocator);
    971   }
    972 
    973 private:
    974   static MCInstrAnalysis *Allocator(const MCInstrInfo *Info) {
    975     return new MCInstrAnalysisImpl(Info);
    976   }
    977 };
    978 
    979 /// RegisterMCInstrAnalysisFn - Helper template for registering a target
    980 /// instruction analyzer implementation.  This invokes the specified function
    981 /// to do the construction.  Usage:
    982 ///
    983 /// extern "C" void LLVMInitializeFooTarget() {
    984 ///   extern Target TheFooTarget;
    985 ///   RegisterMCInstrAnalysisFn X(TheFooTarget, TheFunction);
    986 /// }
    987 struct RegisterMCInstrAnalysisFn {
    988   RegisterMCInstrAnalysisFn(Target &T, Target::MCInstrAnalysisCtorFnTy Fn) {
    989     TargetRegistry::RegisterMCInstrAnalysis(T, Fn);
    990   }
    991 };
    992 
    993 /// RegisterMCRegInfo - Helper template for registering a target register info
    994 /// implementation.  This invokes the static "Create" method on the class to
    995 /// actually do the construction.  Usage:
    996 ///
    997 /// extern "C" void LLVMInitializeFooTarget() {
    998 ///   extern Target TheFooTarget;
    999 ///   RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
   1000 /// }
   1001 template <class MCRegisterInfoImpl> struct RegisterMCRegInfo {
   1002   RegisterMCRegInfo(Target &T) {
   1003     TargetRegistry::RegisterMCRegInfo(T, &Allocator);
   1004   }
   1005 
   1006 private:
   1007   static MCRegisterInfo *Allocator(const Triple & /*TT*/) {
   1008     return new MCRegisterInfoImpl();
   1009   }
   1010 };
   1011 
   1012 /// RegisterMCRegInfoFn - Helper template for registering a target register
   1013 /// info implementation.  This invokes the specified function to do the
   1014 /// construction.  Usage:
   1015 ///
   1016 /// extern "C" void LLVMInitializeFooTarget() {
   1017 ///   extern Target TheFooTarget;
   1018 ///   RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
   1019 /// }
   1020 struct RegisterMCRegInfoFn {
   1021   RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
   1022     TargetRegistry::RegisterMCRegInfo(T, Fn);
   1023   }
   1024 };
   1025 
   1026 /// RegisterMCSubtargetInfo - Helper template for registering a target
   1027 /// subtarget info implementation.  This invokes the static "Create" method
   1028 /// on the class to actually do the construction.  Usage:
   1029 ///
   1030 /// extern "C" void LLVMInitializeFooTarget() {
   1031 ///   extern Target TheFooTarget;
   1032 ///   RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
   1033 /// }
   1034 template <class MCSubtargetInfoImpl> struct RegisterMCSubtargetInfo {
   1035   RegisterMCSubtargetInfo(Target &T) {
   1036     TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
   1037   }
   1038 
   1039 private:
   1040   static MCSubtargetInfo *Allocator(const Triple & /*TT*/, StringRef /*CPU*/,
   1041                                     StringRef /*FS*/) {
   1042     return new MCSubtargetInfoImpl();
   1043   }
   1044 };
   1045 
   1046 /// RegisterMCSubtargetInfoFn - Helper template for registering a target
   1047 /// subtarget info implementation.  This invokes the specified function to
   1048 /// do the construction.  Usage:
   1049 ///
   1050 /// extern "C" void LLVMInitializeFooTarget() {
   1051 ///   extern Target TheFooTarget;
   1052 ///   RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
   1053 /// }
   1054 struct RegisterMCSubtargetInfoFn {
   1055   RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
   1056     TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
   1057   }
   1058 };
   1059 
   1060 /// RegisterTargetMachine - Helper template for registering a target machine
   1061 /// implementation, for use in the target machine initialization
   1062 /// function. Usage:
   1063 ///
   1064 /// extern "C" void LLVMInitializeFooTarget() {
   1065 ///   extern Target TheFooTarget;
   1066 ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
   1067 /// }
   1068 template <class TargetMachineImpl> struct RegisterTargetMachine {
   1069   RegisterTargetMachine(Target &T) {
   1070     TargetRegistry::RegisterTargetMachine(T, &Allocator);
   1071   }
   1072 
   1073 private:
   1074   static TargetMachine *
   1075   Allocator(const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
   1076             const TargetOptions &Options, Optional<Reloc::Model> RM,
   1077             Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT) {
   1078     return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL, JIT);
   1079   }
   1080 };
   1081 
   1082 /// RegisterMCAsmBackend - Helper template for registering a target specific
   1083 /// assembler backend. Usage:
   1084 ///
   1085 /// extern "C" void LLVMInitializeFooMCAsmBackend() {
   1086 ///   extern Target TheFooTarget;
   1087 ///   RegisterMCAsmBackend<FooAsmLexer> X(TheFooTarget);
   1088 /// }
   1089 template <class MCAsmBackendImpl> struct RegisterMCAsmBackend {
   1090   RegisterMCAsmBackend(Target &T) {
   1091     TargetRegistry::RegisterMCAsmBackend(T, &Allocator);
   1092   }
   1093 
   1094 private:
   1095   static MCAsmBackend *Allocator(const Target &T, const MCRegisterInfo &MRI,
   1096                                  const Triple &TheTriple, StringRef CPU,
   1097                                  const MCTargetOptions &Options) {
   1098     return new MCAsmBackendImpl(T, MRI, TheTriple, CPU);
   1099   }
   1100 };
   1101 
   1102 /// RegisterMCAsmParser - Helper template for registering a target specific
   1103 /// assembly parser, for use in the target machine initialization
   1104 /// function. Usage:
   1105 ///
   1106 /// extern "C" void LLVMInitializeFooMCAsmParser() {
   1107 ///   extern Target TheFooTarget;
   1108 ///   RegisterMCAsmParser<FooAsmParser> X(TheFooTarget);
   1109 /// }
   1110 template <class MCAsmParserImpl> struct RegisterMCAsmParser {
   1111   RegisterMCAsmParser(Target &T) {
   1112     TargetRegistry::RegisterMCAsmParser(T, &Allocator);
   1113   }
   1114 
   1115 private:
   1116   static MCTargetAsmParser *Allocator(const MCSubtargetInfo &STI,
   1117                                       MCAsmParser &P, const MCInstrInfo &MII,
   1118                                       const MCTargetOptions &Options) {
   1119     return new MCAsmParserImpl(STI, P, MII, Options);
   1120   }
   1121 };
   1122 
   1123 /// RegisterAsmPrinter - Helper template for registering a target specific
   1124 /// assembly printer, for use in the target machine initialization
   1125 /// function. Usage:
   1126 ///
   1127 /// extern "C" void LLVMInitializeFooAsmPrinter() {
   1128 ///   extern Target TheFooTarget;
   1129 ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
   1130 /// }
   1131 template <class AsmPrinterImpl> struct RegisterAsmPrinter {
   1132   RegisterAsmPrinter(Target &T) {
   1133     TargetRegistry::RegisterAsmPrinter(T, &Allocator);
   1134   }
   1135 
   1136 private:
   1137   static AsmPrinter *Allocator(TargetMachine &TM,
   1138                                std::unique_ptr<MCStreamer> &&Streamer) {
   1139     return new AsmPrinterImpl(TM, std::move(Streamer));
   1140   }
   1141 };
   1142 
   1143 /// RegisterMCCodeEmitter - Helper template for registering a target specific
   1144 /// machine code emitter, for use in the target initialization
   1145 /// function. Usage:
   1146 ///
   1147 /// extern "C" void LLVMInitializeFooMCCodeEmitter() {
   1148 ///   extern Target TheFooTarget;
   1149 ///   RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget);
   1150 /// }
   1151 template <class MCCodeEmitterImpl> struct RegisterMCCodeEmitter {
   1152   RegisterMCCodeEmitter(Target &T) {
   1153     TargetRegistry::RegisterMCCodeEmitter(T, &Allocator);
   1154   }
   1155 
   1156 private:
   1157   static MCCodeEmitter *Allocator(const MCInstrInfo & /*II*/,
   1158                                   const MCRegisterInfo & /*MRI*/,
   1159                                   MCContext & /*Ctx*/) {
   1160     return new MCCodeEmitterImpl();
   1161   }
   1162 };
   1163 
   1164 } // end namespace llvm
   1165 
   1166 #endif // LLVM_SUPPORT_TARGETREGISTRY_H
   1167