Home | History | Annotate | Download | only in Target
      1 //===-- Target/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_TARGET_TARGETREGISTRY_H
     20 #define LLVM_TARGET_TARGETREGISTRY_H
     21 
     22 #include "llvm/MC/MCCodeGenInfo.h"
     23 #include "llvm/ADT/Triple.h"
     24 #include <string>
     25 #include <cassert>
     26 
     27 namespace llvm {
     28   class AsmPrinter;
     29   class Module;
     30   class MCAssembler;
     31   class MCAsmInfo;
     32   class MCAsmParser;
     33   class MCCodeEmitter;
     34   class MCContext;
     35   class MCDisassembler;
     36   class MCInstPrinter;
     37   class MCInstrInfo;
     38   class MCRegisterInfo;
     39   class MCStreamer;
     40   class MCSubtargetInfo;
     41   class MCCodeGenInfo;
     42   class TargetAsmBackend;
     43   class TargetAsmLexer;
     44   class TargetAsmParser;
     45   class TargetMachine;
     46   class raw_ostream;
     47   class formatted_raw_ostream;
     48 
     49   MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
     50                                 bool isVerboseAsm,
     51                                 bool useLoc, bool useCFI,
     52                                 MCInstPrinter *InstPrint,
     53                                 MCCodeEmitter *CE,
     54                                 TargetAsmBackend *TAB,
     55                                 bool ShowInst);
     56 
     57   /// Target - Wrapper for Target specific information.
     58   ///
     59   /// For registration purposes, this is a POD type so that targets can be
     60   /// registered without the use of static constructors.
     61   ///
     62   /// Targets should implement a single global instance of this class (which
     63   /// will be zero initialized), and pass that instance to the TargetRegistry as
     64   /// part of their initialization.
     65   class Target {
     66   public:
     67     friend struct TargetRegistry;
     68 
     69     typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
     70 
     71     typedef MCAsmInfo *(*MCAsmInfoCtorFnTy)(const Target &T,
     72                                             StringRef TT);
     73     typedef MCCodeGenInfo *(*MCCodeGenInfoCtorFnTy)(StringRef TT, Reloc::Model M);
     74     typedef MCInstrInfo *(*MCInstrInfoCtorFnTy)(void);
     75     typedef MCRegisterInfo *(*MCRegInfoCtorFnTy)(StringRef TT);
     76     typedef MCSubtargetInfo *(*MCSubtargetInfoCtorFnTy)(StringRef TT,
     77                                                         StringRef CPU,
     78                                                         StringRef Features);
     79     typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
     80                                                   StringRef TT,
     81                                                   StringRef CPU,
     82                                                   StringRef Features,
     83                                                   Reloc::Model RM);
     84     typedef AsmPrinter *(*AsmPrinterCtorTy)(TargetMachine &TM,
     85                                             MCStreamer &Streamer);
     86     typedef TargetAsmBackend *(*AsmBackendCtorTy)(const Target &T,
     87                                                   const std::string &TT);
     88     typedef TargetAsmLexer *(*AsmLexerCtorTy)(const Target &T,
     89                                               const MCAsmInfo &MAI);
     90     typedef TargetAsmParser *(*AsmParserCtorTy)(MCSubtargetInfo &STI,
     91                                                 MCAsmParser &P);
     92     typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T);
     93     typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
     94                                                   unsigned SyntaxVariant,
     95                                                   const MCAsmInfo &MAI);
     96     typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const MCInstrInfo &II,
     97                                                 const MCSubtargetInfo &STI,
     98                                                 MCContext &Ctx);
     99     typedef MCStreamer *(*ObjectStreamerCtorTy)(const Target &T,
    100                                                 const std::string &TT,
    101                                                 MCContext &Ctx,
    102                                                 TargetAsmBackend &TAB,
    103                                                 raw_ostream &_OS,
    104                                                 MCCodeEmitter *_Emitter,
    105                                                 bool RelaxAll,
    106                                                 bool NoExecStack);
    107     typedef MCStreamer *(*AsmStreamerCtorTy)(MCContext &Ctx,
    108                                              formatted_raw_ostream &OS,
    109                                              bool isVerboseAsm,
    110                                              bool useLoc,
    111                                              bool useCFI,
    112                                              MCInstPrinter *InstPrint,
    113                                              MCCodeEmitter *CE,
    114                                              TargetAsmBackend *TAB,
    115                                              bool ShowInst);
    116 
    117   private:
    118     /// Next - The next registered target in the linked list, maintained by the
    119     /// TargetRegistry.
    120     Target *Next;
    121 
    122     /// TripleMatchQualityFn - The target function for rating the match quality
    123     /// of a triple.
    124     TripleMatchQualityFnTy TripleMatchQualityFn;
    125 
    126     /// Name - The target name.
    127     const char *Name;
    128 
    129     /// ShortDesc - A short description of the target.
    130     const char *ShortDesc;
    131 
    132     /// HasJIT - Whether this target supports the JIT.
    133     bool HasJIT;
    134 
    135     /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
    136     /// registered.
    137     MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
    138 
    139     /// MCCodeGenInfoCtorFn - Constructor function for this target's MCCodeGenInfo,
    140     /// if registered.
    141     MCCodeGenInfoCtorFnTy MCCodeGenInfoCtorFn;
    142 
    143     /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
    144     /// if registered.
    145     MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
    146 
    147     /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
    148     /// if registered.
    149     MCRegInfoCtorFnTy MCRegInfoCtorFn;
    150 
    151     /// MCSubtargetInfoCtorFn - Constructor function for this target's
    152     /// MCSubtargetInfo, if registered.
    153     MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn;
    154 
    155     /// TargetMachineCtorFn - Construction function for this target's
    156     /// TargetMachine, if registered.
    157     TargetMachineCtorTy TargetMachineCtorFn;
    158 
    159     /// AsmBackendCtorFn - Construction function for this target's
    160     /// TargetAsmBackend, if registered.
    161     AsmBackendCtorTy AsmBackendCtorFn;
    162 
    163     /// AsmLexerCtorFn - Construction function for this target's TargetAsmLexer,
    164     /// if registered.
    165     AsmLexerCtorTy AsmLexerCtorFn;
    166 
    167     /// AsmParserCtorFn - Construction function for this target's
    168     /// TargetAsmParser, if registered.
    169     AsmParserCtorTy AsmParserCtorFn;
    170 
    171     /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
    172     /// if registered.
    173     AsmPrinterCtorTy AsmPrinterCtorFn;
    174 
    175     /// MCDisassemblerCtorFn - Construction function for this target's
    176     /// MCDisassembler, if registered.
    177     MCDisassemblerCtorTy MCDisassemblerCtorFn;
    178 
    179     /// MCInstPrinterCtorFn - Construction function for this target's
    180     /// MCInstPrinter, if registered.
    181     MCInstPrinterCtorTy MCInstPrinterCtorFn;
    182 
    183     /// CodeEmitterCtorFn - Construction function for this target's CodeEmitter,
    184     /// if registered.
    185     CodeEmitterCtorTy CodeEmitterCtorFn;
    186 
    187     /// ObjectStreamerCtorFn - Construction function for this target's
    188     /// ObjectStreamer, if registered.
    189     ObjectStreamerCtorTy ObjectStreamerCtorFn;
    190 
    191     /// AsmStreamerCtorFn - Construction function for this target's
    192     /// AsmStreamer, if registered (default = llvm::createAsmStreamer).
    193     AsmStreamerCtorTy AsmStreamerCtorFn;
    194 
    195   public:
    196     Target() : AsmStreamerCtorFn(llvm::createAsmStreamer) {}
    197 
    198     /// @name Target Information
    199     /// @{
    200 
    201     // getNext - Return the next registered target.
    202     const Target *getNext() const { return Next; }
    203 
    204     /// getName - Get the target name.
    205     const char *getName() const { return Name; }
    206 
    207     /// getShortDescription - Get a short description of the target.
    208     const char *getShortDescription() const { return ShortDesc; }
    209 
    210     /// @}
    211     /// @name Feature Predicates
    212     /// @{
    213 
    214     /// hasJIT - Check if this targets supports the just-in-time compilation.
    215     bool hasJIT() const { return HasJIT; }
    216 
    217     /// hasTargetMachine - Check if this target supports code generation.
    218     bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
    219 
    220     /// hasAsmBackend - Check if this target supports .o generation.
    221     bool hasAsmBackend() const { return AsmBackendCtorFn != 0; }
    222 
    223     /// hasAsmLexer - Check if this target supports .s lexing.
    224     bool hasAsmLexer() const { return AsmLexerCtorFn != 0; }
    225 
    226     /// hasAsmParser - Check if this target supports .s parsing.
    227     bool hasAsmParser() const { return AsmParserCtorFn != 0; }
    228 
    229     /// hasAsmPrinter - Check if this target supports .s printing.
    230     bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
    231 
    232     /// hasMCDisassembler - Check if this target has a disassembler.
    233     bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
    234 
    235     /// hasMCInstPrinter - Check if this target has an instruction printer.
    236     bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
    237 
    238     /// hasCodeEmitter - Check if this target supports instruction encoding.
    239     bool hasCodeEmitter() const { return CodeEmitterCtorFn != 0; }
    240 
    241     /// hasObjectStreamer - Check if this target supports streaming to files.
    242     bool hasObjectStreamer() const { return ObjectStreamerCtorFn != 0; }
    243 
    244     /// hasAsmStreamer - Check if this target supports streaming to files.
    245     bool hasAsmStreamer() const { return AsmStreamerCtorFn != 0; }
    246 
    247     /// @}
    248     /// @name Feature Constructors
    249     /// @{
    250 
    251     /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
    252     /// target triple.
    253     ///
    254     /// \arg Triple - This argument is used to determine the target machine
    255     /// feature set; it should always be provided. Generally this should be
    256     /// either the target triple from the module, or the target triple of the
    257     /// host if that does not exist.
    258     MCAsmInfo *createMCAsmInfo(StringRef Triple) const {
    259       if (!MCAsmInfoCtorFn)
    260         return 0;
    261       return MCAsmInfoCtorFn(*this, Triple);
    262     }
    263 
    264     /// createMCCodeGenInfo - Create a MCCodeGenInfo implementation.
    265     ///
    266     MCCodeGenInfo *createMCCodeGenInfo(StringRef Triple, Reloc::Model M) const {
    267       if (!MCCodeGenInfoCtorFn)
    268         return 0;
    269       return MCCodeGenInfoCtorFn(Triple, M);
    270     }
    271 
    272     /// createMCInstrInfo - Create a MCInstrInfo implementation.
    273     ///
    274     MCInstrInfo *createMCInstrInfo() const {
    275       if (!MCInstrInfoCtorFn)
    276         return 0;
    277       return MCInstrInfoCtorFn();
    278     }
    279 
    280     /// createMCRegInfo - Create a MCRegisterInfo implementation.
    281     ///
    282     MCRegisterInfo *createMCRegInfo(StringRef Triple) const {
    283       if (!MCRegInfoCtorFn)
    284         return 0;
    285       return MCRegInfoCtorFn(Triple);
    286     }
    287 
    288     /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
    289     ///
    290     /// \arg Triple - This argument is used to determine the target machine
    291     /// feature set; it should always be provided. Generally this should be
    292     /// either the target triple from the module, or the target triple of the
    293     /// host if that does not exist.
    294     /// \arg CPU - This specifies the name of the target CPU.
    295     /// \arg Features - This specifies the string representation of the
    296     /// additional target features.
    297     MCSubtargetInfo *createMCSubtargetInfo(StringRef Triple, StringRef CPU,
    298                                            StringRef Features) const {
    299       if (!MCSubtargetInfoCtorFn)
    300         return 0;
    301       return MCSubtargetInfoCtorFn(Triple, CPU, Features);
    302     }
    303 
    304     /// createTargetMachine - Create a target specific machine implementation
    305     /// for the specified \arg Triple.
    306     ///
    307     /// \arg Triple - This argument is used to determine the target machine
    308     /// feature set; it should always be provided. Generally this should be
    309     /// either the target triple from the module, or the target triple of the
    310     /// host if that does not exist.
    311     TargetMachine *createTargetMachine(StringRef Triple, StringRef CPU,
    312                                        StringRef Features,
    313                                        Reloc::Model RM = Reloc::Default) const {
    314       if (!TargetMachineCtorFn)
    315         return 0;
    316       return TargetMachineCtorFn(*this, Triple, CPU, Features, RM);
    317     }
    318 
    319     /// createAsmBackend - Create a target specific assembly parser.
    320     ///
    321     /// \arg Triple - The target triple string.
    322     /// \arg Backend - The target independent assembler object.
    323     TargetAsmBackend *createAsmBackend(const std::string &Triple) const {
    324       if (!AsmBackendCtorFn)
    325         return 0;
    326       return AsmBackendCtorFn(*this, Triple);
    327     }
    328 
    329     /// createAsmLexer - Create a target specific assembly lexer.
    330     ///
    331     TargetAsmLexer *createAsmLexer(const MCAsmInfo &MAI) const {
    332       if (!AsmLexerCtorFn)
    333         return 0;
    334       return AsmLexerCtorFn(*this, MAI);
    335     }
    336 
    337     /// createAsmParser - Create a target specific assembly parser.
    338     ///
    339     /// \arg Parser - The target independent parser implementation to use for
    340     /// parsing and lexing.
    341     TargetAsmParser *createAsmParser(MCSubtargetInfo &STI,
    342                                      MCAsmParser &Parser) const {
    343       if (!AsmParserCtorFn)
    344         return 0;
    345       return AsmParserCtorFn(STI, Parser);
    346     }
    347 
    348     /// createAsmPrinter - Create a target specific assembly printer pass.  This
    349     /// takes ownership of the MCStreamer object.
    350     AsmPrinter *createAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) const{
    351       if (!AsmPrinterCtorFn)
    352         return 0;
    353       return AsmPrinterCtorFn(TM, Streamer);
    354     }
    355 
    356     MCDisassembler *createMCDisassembler() const {
    357       if (!MCDisassemblerCtorFn)
    358         return 0;
    359       return MCDisassemblerCtorFn(*this);
    360     }
    361 
    362     MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
    363                                        const MCAsmInfo &MAI) const {
    364       if (!MCInstPrinterCtorFn)
    365         return 0;
    366       return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI);
    367     }
    368 
    369 
    370     /// createCodeEmitter - Create a target specific code emitter.
    371     MCCodeEmitter *createCodeEmitter(const MCInstrInfo &II,
    372                                      const MCSubtargetInfo &STI,
    373                                      MCContext &Ctx) const {
    374       if (!CodeEmitterCtorFn)
    375         return 0;
    376       return CodeEmitterCtorFn(II, STI, Ctx);
    377     }
    378 
    379     /// createObjectStreamer - Create a target specific MCStreamer.
    380     ///
    381     /// \arg TT - The target triple.
    382     /// \arg Ctx - The target context.
    383     /// \arg TAB - The target assembler backend object. Takes ownership.
    384     /// \arg _OS - The stream object.
    385     /// \arg _Emitter - The target independent assembler object.Takes ownership.
    386     /// \arg RelaxAll - Relax all fixups?
    387     /// \arg NoExecStack - Mark file as not needing a executable stack.
    388     MCStreamer *createObjectStreamer(const std::string &TT, MCContext &Ctx,
    389                                      TargetAsmBackend &TAB,
    390                                      raw_ostream &_OS,
    391                                      MCCodeEmitter *_Emitter,
    392                                      bool RelaxAll,
    393                                      bool NoExecStack) const {
    394       if (!ObjectStreamerCtorFn)
    395         return 0;
    396       return ObjectStreamerCtorFn(*this, TT, Ctx, TAB, _OS, _Emitter, RelaxAll,
    397                                   NoExecStack);
    398     }
    399 
    400     /// createAsmStreamer - Create a target specific MCStreamer.
    401     MCStreamer *createAsmStreamer(MCContext &Ctx,
    402                                   formatted_raw_ostream &OS,
    403                                   bool isVerboseAsm,
    404                                   bool useLoc,
    405                                   bool useCFI,
    406                                   MCInstPrinter *InstPrint,
    407                                   MCCodeEmitter *CE,
    408                                   TargetAsmBackend *TAB,
    409                                   bool ShowInst) const {
    410       // AsmStreamerCtorFn is default to llvm::createAsmStreamer
    411       return AsmStreamerCtorFn(Ctx, OS, isVerboseAsm, useLoc, useCFI,
    412                                InstPrint, CE, TAB, ShowInst);
    413     }
    414 
    415     /// @}
    416   };
    417 
    418   /// TargetRegistry - Generic interface to target specific features.
    419   struct TargetRegistry {
    420     class iterator {
    421       const Target *Current;
    422       explicit iterator(Target *T) : Current(T) {}
    423       friend struct TargetRegistry;
    424     public:
    425       iterator(const iterator &I) : Current(I.Current) {}
    426       iterator() : Current(0) {}
    427 
    428       bool operator==(const iterator &x) const {
    429         return Current == x.Current;
    430       }
    431       bool operator!=(const iterator &x) const {
    432         return !operator==(x);
    433       }
    434 
    435       // Iterator traversal: forward iteration only
    436       iterator &operator++() {          // Preincrement
    437         assert(Current && "Cannot increment end iterator!");
    438         Current = Current->getNext();
    439         return *this;
    440       }
    441       iterator operator++(int) {        // Postincrement
    442         iterator tmp = *this;
    443         ++*this;
    444         return tmp;
    445       }
    446 
    447       const Target &operator*() const {
    448         assert(Current && "Cannot dereference end iterator!");
    449         return *Current;
    450       }
    451 
    452       const Target *operator->() const {
    453         return &operator*();
    454       }
    455     };
    456 
    457     /// @name Registry Access
    458     /// @{
    459 
    460     static iterator begin();
    461 
    462     static iterator end() { return iterator(); }
    463 
    464     /// lookupTarget - Lookup a target based on a target triple.
    465     ///
    466     /// \param Triple - The triple to use for finding a target.
    467     /// \param Error - On failure, an error string describing why no target was
    468     /// found.
    469     static const Target *lookupTarget(const std::string &Triple,
    470                                       std::string &Error);
    471 
    472     /// getClosestTargetForJIT - Pick the best target that is compatible with
    473     /// the current host.  If no close target can be found, this returns null
    474     /// and sets the Error string to a reason.
    475     ///
    476     /// Maintained for compatibility through 2.6.
    477     static const Target *getClosestTargetForJIT(std::string &Error);
    478 
    479     /// @}
    480     /// @name Target Registration
    481     /// @{
    482 
    483     /// RegisterTarget - Register the given target. Attempts to register a
    484     /// target which has already been registered will be ignored.
    485     ///
    486     /// Clients are responsible for ensuring that registration doesn't occur
    487     /// while another thread is attempting to access the registry. Typically
    488     /// this is done by initializing all targets at program startup.
    489     ///
    490     /// @param T - The target being registered.
    491     /// @param Name - The target name. This should be a static string.
    492     /// @param ShortDesc - A short target description. This should be a static
    493     /// string.
    494     /// @param TQualityFn - The triple match quality computation function for
    495     /// this target.
    496     /// @param HasJIT - Whether the target supports JIT code
    497     /// generation.
    498     static void RegisterTarget(Target &T,
    499                                const char *Name,
    500                                const char *ShortDesc,
    501                                Target::TripleMatchQualityFnTy TQualityFn,
    502                                bool HasJIT = false);
    503 
    504     /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
    505     /// given target.
    506     ///
    507     /// Clients are responsible for ensuring that registration doesn't occur
    508     /// while another thread is attempting to access the registry. Typically
    509     /// this is done by initializing all targets at program startup.
    510     ///
    511     /// @param T - The target being registered.
    512     /// @param Fn - A function to construct a MCAsmInfo for the target.
    513     static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
    514       // Ignore duplicate registration.
    515       if (!T.MCAsmInfoCtorFn)
    516         T.MCAsmInfoCtorFn = Fn;
    517     }
    518 
    519     /// RegisterMCCodeGenInfo - Register a MCCodeGenInfo implementation for the
    520     /// given target.
    521     ///
    522     /// Clients are responsible for ensuring that registration doesn't occur
    523     /// while another thread is attempting to access the registry. Typically
    524     /// this is done by initializing all targets at program startup.
    525     ///
    526     /// @param T - The target being registered.
    527     /// @param Fn - A function to construct a MCCodeGenInfo for the target.
    528     static void RegisterMCCodeGenInfo(Target &T,
    529                                      Target::MCCodeGenInfoCtorFnTy Fn) {
    530       // Ignore duplicate registration.
    531       if (!T.MCCodeGenInfoCtorFn)
    532         T.MCCodeGenInfoCtorFn = Fn;
    533     }
    534 
    535     /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
    536     /// given target.
    537     ///
    538     /// Clients are responsible for ensuring that registration doesn't occur
    539     /// while another thread is attempting to access the registry. Typically
    540     /// this is done by initializing all targets at program startup.
    541     ///
    542     /// @param T - The target being registered.
    543     /// @param Fn - A function to construct a MCInstrInfo for the target.
    544     static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
    545       // Ignore duplicate registration.
    546       if (!T.MCInstrInfoCtorFn)
    547         T.MCInstrInfoCtorFn = Fn;
    548     }
    549 
    550     /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
    551     /// given target.
    552     ///
    553     /// Clients are responsible for ensuring that registration doesn't occur
    554     /// while another thread is attempting to access the registry. Typically
    555     /// this is done by initializing all targets at program startup.
    556     ///
    557     /// @param T - The target being registered.
    558     /// @param Fn - A function to construct a MCRegisterInfo for the target.
    559     static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
    560       // Ignore duplicate registration.
    561       if (!T.MCRegInfoCtorFn)
    562         T.MCRegInfoCtorFn = Fn;
    563     }
    564 
    565     /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
    566     /// the given target.
    567     ///
    568     /// Clients are responsible for ensuring that registration doesn't occur
    569     /// while another thread is attempting to access the registry. Typically
    570     /// this is done by initializing all targets at program startup.
    571     ///
    572     /// @param T - The target being registered.
    573     /// @param Fn - A function to construct a MCSubtargetInfo for the target.
    574     static void RegisterMCSubtargetInfo(Target &T,
    575                                         Target::MCSubtargetInfoCtorFnTy Fn) {
    576       // Ignore duplicate registration.
    577       if (!T.MCSubtargetInfoCtorFn)
    578         T.MCSubtargetInfoCtorFn = Fn;
    579     }
    580 
    581     /// RegisterTargetMachine - Register a TargetMachine implementation for the
    582     /// given target.
    583     ///
    584     /// Clients are responsible for ensuring that registration doesn't occur
    585     /// while another thread is attempting to access the registry. Typically
    586     /// this is done by initializing all targets at program startup.
    587     ///
    588     /// @param T - The target being registered.
    589     /// @param Fn - A function to construct a TargetMachine for the target.
    590     static void RegisterTargetMachine(Target &T,
    591                                       Target::TargetMachineCtorTy Fn) {
    592       // Ignore duplicate registration.
    593       if (!T.TargetMachineCtorFn)
    594         T.TargetMachineCtorFn = Fn;
    595     }
    596 
    597     /// RegisterAsmBackend - Register a TargetAsmBackend implementation for the
    598     /// given target.
    599     ///
    600     /// Clients are responsible for ensuring that registration doesn't occur
    601     /// while another thread is attempting to access the registry. Typically
    602     /// this is done by initializing all targets at program startup.
    603     ///
    604     /// @param T - The target being registered.
    605     /// @param Fn - A function to construct an AsmBackend for the target.
    606     static void RegisterAsmBackend(Target &T, Target::AsmBackendCtorTy Fn) {
    607       if (!T.AsmBackendCtorFn)
    608         T.AsmBackendCtorFn = Fn;
    609     }
    610 
    611     /// RegisterAsmLexer - Register a TargetAsmLexer implementation for the
    612     /// given target.
    613     ///
    614     /// Clients are responsible for ensuring that registration doesn't occur
    615     /// while another thread is attempting to access the registry. Typically
    616     /// this is done by initializing all targets at program startup.
    617     ///
    618     /// @param T - The target being registered.
    619     /// @param Fn - A function to construct an AsmLexer for the target.
    620     static void RegisterAsmLexer(Target &T, Target::AsmLexerCtorTy Fn) {
    621       if (!T.AsmLexerCtorFn)
    622         T.AsmLexerCtorFn = Fn;
    623     }
    624 
    625     /// RegisterAsmParser - Register a TargetAsmParser implementation for the
    626     /// given target.
    627     ///
    628     /// Clients are responsible for ensuring that registration doesn't occur
    629     /// while another thread is attempting to access the registry. Typically
    630     /// this is done by initializing all targets at program startup.
    631     ///
    632     /// @param T - The target being registered.
    633     /// @param Fn - A function to construct an AsmParser for the target.
    634     static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
    635       if (!T.AsmParserCtorFn)
    636         T.AsmParserCtorFn = Fn;
    637     }
    638 
    639     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
    640     /// target.
    641     ///
    642     /// Clients are responsible for ensuring that registration doesn't occur
    643     /// while another thread is attempting to access the registry. Typically
    644     /// this is done by initializing all targets at program startup.
    645     ///
    646     /// @param T - The target being registered.
    647     /// @param Fn - A function to construct an AsmPrinter for the target.
    648     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
    649       // Ignore duplicate registration.
    650       if (!T.AsmPrinterCtorFn)
    651         T.AsmPrinterCtorFn = Fn;
    652     }
    653 
    654     /// RegisterMCDisassembler - Register a MCDisassembler implementation for
    655     /// the given target.
    656     ///
    657     /// Clients are responsible for ensuring that registration doesn't occur
    658     /// while another thread is attempting to access the registry. Typically
    659     /// this is done by initializing all targets at program startup.
    660     ///
    661     /// @param T - The target being registered.
    662     /// @param Fn - A function to construct an MCDisassembler for the target.
    663     static void RegisterMCDisassembler(Target &T,
    664                                        Target::MCDisassemblerCtorTy Fn) {
    665       if (!T.MCDisassemblerCtorFn)
    666         T.MCDisassemblerCtorFn = Fn;
    667     }
    668 
    669     /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
    670     /// given target.
    671     ///
    672     /// Clients are responsible for ensuring that registration doesn't occur
    673     /// while another thread is attempting to access the registry. Typically
    674     /// this is done by initializing all targets at program startup.
    675     ///
    676     /// @param T - The target being registered.
    677     /// @param Fn - A function to construct an MCInstPrinter for the target.
    678     static void RegisterMCInstPrinter(Target &T,
    679                                       Target::MCInstPrinterCtorTy Fn) {
    680       if (!T.MCInstPrinterCtorFn)
    681         T.MCInstPrinterCtorFn = Fn;
    682     }
    683 
    684     /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
    685     /// given target.
    686     ///
    687     /// Clients are responsible for ensuring that registration doesn't occur
    688     /// while another thread is attempting to access the registry. Typically
    689     /// this is done by initializing all targets at program startup.
    690     ///
    691     /// @param T - The target being registered.
    692     /// @param Fn - A function to construct an MCCodeEmitter for the target.
    693     static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
    694       if (!T.CodeEmitterCtorFn)
    695         T.CodeEmitterCtorFn = Fn;
    696     }
    697 
    698     /// RegisterObjectStreamer - Register a object code MCStreamer implementation
    699     /// for the given target.
    700     ///
    701     /// Clients are responsible for ensuring that registration doesn't occur
    702     /// while another thread is attempting to access the registry. Typically
    703     /// this is done by initializing all targets at program startup.
    704     ///
    705     /// @param T - The target being registered.
    706     /// @param Fn - A function to construct an MCStreamer for the target.
    707     static void RegisterObjectStreamer(Target &T, Target::ObjectStreamerCtorTy Fn) {
    708       if (!T.ObjectStreamerCtorFn)
    709         T.ObjectStreamerCtorFn = Fn;
    710     }
    711 
    712     /// RegisterAsmStreamer - Register an assembly MCStreamer implementation
    713     /// for the given target.
    714     ///
    715     /// Clients are responsible for ensuring that registration doesn't occur
    716     /// while another thread is attempting to access the registry. Typically
    717     /// this is done by initializing all targets at program startup.
    718     ///
    719     /// @param T - The target being registered.
    720     /// @param Fn - A function to construct an MCStreamer for the target.
    721     static void RegisterAsmStreamer(Target &T, Target::AsmStreamerCtorTy Fn) {
    722       if (T.AsmStreamerCtorFn == createAsmStreamer)
    723         T.AsmStreamerCtorFn = Fn;
    724     }
    725 
    726     /// @}
    727   };
    728 
    729 
    730   //===--------------------------------------------------------------------===//
    731 
    732   /// RegisterTarget - Helper template for registering a target, for use in the
    733   /// target's initialization function. Usage:
    734   ///
    735   ///
    736   /// Target TheFooTarget; // The global target instance.
    737   ///
    738   /// extern "C" void LLVMInitializeFooTargetInfo() {
    739   ///   RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
    740   /// }
    741   template<Triple::ArchType TargetArchType = Triple::InvalidArch,
    742            bool HasJIT = false>
    743   struct RegisterTarget {
    744     RegisterTarget(Target &T, const char *Name, const char *Desc) {
    745       TargetRegistry::RegisterTarget(T, Name, Desc,
    746                                      &getTripleMatchQuality,
    747                                      HasJIT);
    748     }
    749 
    750     static unsigned getTripleMatchQuality(const std::string &TT) {
    751       if (Triple(TT).getArch() == TargetArchType)
    752         return 20;
    753       return 0;
    754     }
    755   };
    756 
    757   /// RegisterMCAsmInfo - Helper template for registering a target assembly info
    758   /// implementation.  This invokes the static "Create" method on the class to
    759   /// actually do the construction.  Usage:
    760   ///
    761   /// extern "C" void LLVMInitializeFooTarget() {
    762   ///   extern Target TheFooTarget;
    763   ///   RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
    764   /// }
    765   template<class MCAsmInfoImpl>
    766   struct RegisterMCAsmInfo {
    767     RegisterMCAsmInfo(Target &T) {
    768       TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
    769     }
    770   private:
    771     static MCAsmInfo *Allocator(const Target &T, StringRef TT) {
    772       return new MCAsmInfoImpl(T, TT);
    773     }
    774 
    775   };
    776 
    777   /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
    778   /// implementation.  This invokes the specified function to do the
    779   /// construction.  Usage:
    780   ///
    781   /// extern "C" void LLVMInitializeFooTarget() {
    782   ///   extern Target TheFooTarget;
    783   ///   RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
    784   /// }
    785   struct RegisterMCAsmInfoFn {
    786     RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
    787       TargetRegistry::RegisterMCAsmInfo(T, Fn);
    788     }
    789   };
    790 
    791   /// RegisterMCCodeGenInfo - Helper template for registering a target codegen info
    792   /// implementation.  This invokes the static "Create" method on the class
    793   /// to actually do the construction.  Usage:
    794   ///
    795   /// extern "C" void LLVMInitializeFooTarget() {
    796   ///   extern Target TheFooTarget;
    797   ///   RegisterMCCodeGenInfo<FooMCCodeGenInfo> X(TheFooTarget);
    798   /// }
    799   template<class MCCodeGenInfoImpl>
    800   struct RegisterMCCodeGenInfo {
    801     RegisterMCCodeGenInfo(Target &T) {
    802       TargetRegistry::RegisterMCCodeGenInfo(T, &Allocator);
    803     }
    804   private:
    805     static MCCodeGenInfo *Allocator(StringRef TT, Reloc::Model M) {
    806       return new MCCodeGenInfoImpl();
    807     }
    808   };
    809 
    810   /// RegisterMCCodeGenInfoFn - Helper template for registering a target codegen
    811   /// info implementation.  This invokes the specified function to do the
    812   /// construction.  Usage:
    813   ///
    814   /// extern "C" void LLVMInitializeFooTarget() {
    815   ///   extern Target TheFooTarget;
    816   ///   RegisterMCCodeGenInfoFn X(TheFooTarget, TheFunction);
    817   /// }
    818   struct RegisterMCCodeGenInfoFn {
    819     RegisterMCCodeGenInfoFn(Target &T, Target::MCCodeGenInfoCtorFnTy Fn) {
    820       TargetRegistry::RegisterMCCodeGenInfo(T, Fn);
    821     }
    822   };
    823 
    824   /// RegisterMCInstrInfo - Helper template for registering a target instruction
    825   /// info implementation.  This invokes the static "Create" method on the class
    826   /// to actually do the construction.  Usage:
    827   ///
    828   /// extern "C" void LLVMInitializeFooTarget() {
    829   ///   extern Target TheFooTarget;
    830   ///   RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
    831   /// }
    832   template<class MCInstrInfoImpl>
    833   struct RegisterMCInstrInfo {
    834     RegisterMCInstrInfo(Target &T) {
    835       TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
    836     }
    837   private:
    838     static MCInstrInfo *Allocator() {
    839       return new MCInstrInfoImpl();
    840     }
    841   };
    842 
    843   /// RegisterMCInstrInfoFn - Helper template for registering a target
    844   /// instruction info implementation.  This invokes the specified function to
    845   /// do the construction.  Usage:
    846   ///
    847   /// extern "C" void LLVMInitializeFooTarget() {
    848   ///   extern Target TheFooTarget;
    849   ///   RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
    850   /// }
    851   struct RegisterMCInstrInfoFn {
    852     RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
    853       TargetRegistry::RegisterMCInstrInfo(T, Fn);
    854     }
    855   };
    856 
    857   /// RegisterMCRegInfo - Helper template for registering a target register info
    858   /// implementation.  This invokes the static "Create" method on the class to
    859   /// actually do the construction.  Usage:
    860   ///
    861   /// extern "C" void LLVMInitializeFooTarget() {
    862   ///   extern Target TheFooTarget;
    863   ///   RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
    864   /// }
    865   template<class MCRegisterInfoImpl>
    866   struct RegisterMCRegInfo {
    867     RegisterMCRegInfo(Target &T) {
    868       TargetRegistry::RegisterMCRegInfo(T, &Allocator);
    869     }
    870   private:
    871     static MCRegisterInfo *Allocator(StringRef TT) {
    872       return new MCRegisterInfoImpl();
    873     }
    874   };
    875 
    876   /// RegisterMCRegInfoFn - Helper template for registering a target register
    877   /// info implementation.  This invokes the specified function to do the
    878   /// construction.  Usage:
    879   ///
    880   /// extern "C" void LLVMInitializeFooTarget() {
    881   ///   extern Target TheFooTarget;
    882   ///   RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
    883   /// }
    884   struct RegisterMCRegInfoFn {
    885     RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
    886       TargetRegistry::RegisterMCRegInfo(T, Fn);
    887     }
    888   };
    889 
    890   /// RegisterMCSubtargetInfo - Helper template for registering a target
    891   /// subtarget info implementation.  This invokes the static "Create" method
    892   /// on the class to actually do the construction.  Usage:
    893   ///
    894   /// extern "C" void LLVMInitializeFooTarget() {
    895   ///   extern Target TheFooTarget;
    896   ///   RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
    897   /// }
    898   template<class MCSubtargetInfoImpl>
    899   struct RegisterMCSubtargetInfo {
    900     RegisterMCSubtargetInfo(Target &T) {
    901       TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
    902     }
    903   private:
    904     static MCSubtargetInfo *Allocator(StringRef TT, StringRef CPU,
    905                                       StringRef FS) {
    906       return new MCSubtargetInfoImpl();
    907     }
    908   };
    909 
    910   /// RegisterMCSubtargetInfoFn - Helper template for registering a target
    911   /// subtarget info implementation.  This invokes the specified function to
    912   /// do the construction.  Usage:
    913   ///
    914   /// extern "C" void LLVMInitializeFooTarget() {
    915   ///   extern Target TheFooTarget;
    916   ///   RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
    917   /// }
    918   struct RegisterMCSubtargetInfoFn {
    919     RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
    920       TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
    921     }
    922   };
    923 
    924   /// RegisterTargetMachine - Helper template for registering a target machine
    925   /// implementation, for use in the target machine initialization
    926   /// function. Usage:
    927   ///
    928   /// extern "C" void LLVMInitializeFooTarget() {
    929   ///   extern Target TheFooTarget;
    930   ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
    931   /// }
    932   template<class TargetMachineImpl>
    933   struct RegisterTargetMachine {
    934     RegisterTargetMachine(Target &T) {
    935       TargetRegistry::RegisterTargetMachine(T, &Allocator);
    936     }
    937 
    938   private:
    939     static TargetMachine *Allocator(const Target &T, StringRef TT,
    940                                     StringRef CPU, StringRef FS,
    941                                     Reloc::Model RM) {
    942       return new TargetMachineImpl(T, TT, CPU, FS, RM);
    943     }
    944   };
    945 
    946   /// RegisterAsmBackend - Helper template for registering a target specific
    947   /// assembler backend. Usage:
    948   ///
    949   /// extern "C" void LLVMInitializeFooAsmBackend() {
    950   ///   extern Target TheFooTarget;
    951   ///   RegisterAsmBackend<FooAsmLexer> X(TheFooTarget);
    952   /// }
    953   template<class AsmBackendImpl>
    954   struct RegisterAsmBackend {
    955     RegisterAsmBackend(Target &T) {
    956       TargetRegistry::RegisterAsmBackend(T, &Allocator);
    957     }
    958 
    959   private:
    960     static TargetAsmBackend *Allocator(const Target &T,
    961                                        const std::string &Triple) {
    962       return new AsmBackendImpl(T, Triple);
    963     }
    964   };
    965 
    966   /// RegisterAsmLexer - Helper template for registering a target specific
    967   /// assembly lexer, for use in the target machine initialization
    968   /// function. Usage:
    969   ///
    970   /// extern "C" void LLVMInitializeFooAsmLexer() {
    971   ///   extern Target TheFooTarget;
    972   ///   RegisterAsmLexer<FooAsmLexer> X(TheFooTarget);
    973   /// }
    974   template<class AsmLexerImpl>
    975   struct RegisterAsmLexer {
    976     RegisterAsmLexer(Target &T) {
    977       TargetRegistry::RegisterAsmLexer(T, &Allocator);
    978     }
    979 
    980   private:
    981     static TargetAsmLexer *Allocator(const Target &T, const MCAsmInfo &MAI) {
    982       return new AsmLexerImpl(T, MAI);
    983     }
    984   };
    985 
    986   /// RegisterAsmParser - Helper template for registering a target specific
    987   /// assembly parser, for use in the target machine initialization
    988   /// function. Usage:
    989   ///
    990   /// extern "C" void LLVMInitializeFooAsmParser() {
    991   ///   extern Target TheFooTarget;
    992   ///   RegisterAsmParser<FooAsmParser> X(TheFooTarget);
    993   /// }
    994   template<class AsmParserImpl>
    995   struct RegisterAsmParser {
    996     RegisterAsmParser(Target &T) {
    997       TargetRegistry::RegisterAsmParser(T, &Allocator);
    998     }
    999 
   1000   private:
   1001     static TargetAsmParser *Allocator(MCSubtargetInfo &STI, MCAsmParser &P) {
   1002       return new AsmParserImpl(STI, P);
   1003     }
   1004   };
   1005 
   1006   /// RegisterAsmPrinter - Helper template for registering a target specific
   1007   /// assembly printer, for use in the target machine initialization
   1008   /// function. Usage:
   1009   ///
   1010   /// extern "C" void LLVMInitializeFooAsmPrinter() {
   1011   ///   extern Target TheFooTarget;
   1012   ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
   1013   /// }
   1014   template<class AsmPrinterImpl>
   1015   struct RegisterAsmPrinter {
   1016     RegisterAsmPrinter(Target &T) {
   1017       TargetRegistry::RegisterAsmPrinter(T, &Allocator);
   1018     }
   1019 
   1020   private:
   1021     static AsmPrinter *Allocator(TargetMachine &TM, MCStreamer &Streamer) {
   1022       return new AsmPrinterImpl(TM, Streamer);
   1023     }
   1024   };
   1025 
   1026   /// RegisterCodeEmitter - Helper template for registering a target specific
   1027   /// machine code emitter, for use in the target initialization
   1028   /// function. Usage:
   1029   ///
   1030   /// extern "C" void LLVMInitializeFooCodeEmitter() {
   1031   ///   extern Target TheFooTarget;
   1032   ///   RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
   1033   /// }
   1034   template<class CodeEmitterImpl>
   1035   struct RegisterCodeEmitter {
   1036     RegisterCodeEmitter(Target &T) {
   1037       TargetRegistry::RegisterCodeEmitter(T, &Allocator);
   1038     }
   1039 
   1040   private:
   1041     static MCCodeEmitter *Allocator(const MCInstrInfo &II,
   1042                                     const MCSubtargetInfo &STI,
   1043                                     MCContext &Ctx) {
   1044       return new CodeEmitterImpl();
   1045     }
   1046   };
   1047 
   1048 }
   1049 
   1050 #endif
   1051