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