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