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