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