0f63b4c9454189a769938fe8724421b3cf4bf910
[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 TargetMachine;
49   class TargetOptions;
50   class raw_ostream;
51   class formatted_raw_ostream;
52
53   MCStreamer *createAsmStreamer(MCContext &Ctx,
54                                 formatted_raw_ostream &OS, bool isVerboseAsm,
55                                 bool useLoc, 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)(MCSubtargetInfo &STI,
109                                                     MCAsmParser &P,
110                                                     const MCInstrInfo &MII);
111     typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T,
112                                                     const MCSubtargetInfo &STI);
113     typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
114                                                   unsigned SyntaxVariant,
115                                                   const MCAsmInfo &MAI,
116                                                   const MCInstrInfo &MII,
117                                                   const MCRegisterInfo &MRI,
118                                                   const MCSubtargetInfo &STI);
119     typedef MCCodeEmitter *(*MCCodeEmitterCtorTy)(const MCInstrInfo &II,
120                                                   const MCRegisterInfo &MRI,
121                                                   const MCSubtargetInfo &STI,
122                                                   MCContext &Ctx);
123     typedef MCStreamer *(*MCObjectStreamerCtorTy)(const Target &T,
124                                                   StringRef TT,
125                                                   MCContext &Ctx,
126                                                   MCAsmBackend &TAB,
127                                                   raw_ostream &_OS,
128                                                   MCCodeEmitter *_Emitter,
129                                                   bool RelaxAll,
130                                                   bool NoExecStack);
131     typedef MCStreamer *(*AsmStreamerCtorTy)(MCContext &Ctx,
132                                              formatted_raw_ostream &OS,
133                                              bool isVerboseAsm,
134                                              bool useLoc,
135                                              bool useCFI,
136                                              bool useDwarfDirectory,
137                                              MCInstPrinter *InstPrint,
138                                              MCCodeEmitter *CE,
139                                              MCAsmBackend *TAB,
140                                              bool ShowInst);
141     typedef MCRelocationInfo *(*MCRelocationInfoCtorTy)(StringRef TT,
142                                                         MCContext &Ctx);
143     typedef MCSymbolizer *(*MCSymbolizerCtorTy)(StringRef TT,
144                                    LLVMOpInfoCallback GetOpInfo,
145                                    LLVMSymbolLookupCallback SymbolLookUp,
146                                    void *DisInfo,
147                                    MCContext *Ctx,
148                                    MCRelocationInfo *RelInfo);
149
150   private:
151     /// Next - The next registered target in the linked list, maintained by the
152     /// TargetRegistry.
153     Target *Next;
154
155     /// The target function for checking if an architecture is supported.
156     ArchMatchFnTy ArchMatchFn;
157
158     /// Name - The target name.
159     const char *Name;
160
161     /// ShortDesc - A short description of the target.
162     const char *ShortDesc;
163
164     /// HasJIT - Whether this target supports the JIT.
165     bool HasJIT;
166
167     /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
168     /// registered.
169     MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
170
171     /// MCCodeGenInfoCtorFn - Constructor function for this target's
172     /// MCCodeGenInfo, if registered.
173     MCCodeGenInfoCtorFnTy MCCodeGenInfoCtorFn;
174
175     /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
176     /// if registered.
177     MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
178
179     /// MCInstrAnalysisCtorFn - Constructor function for this target's
180     /// MCInstrAnalysis, if registered.
181     MCInstrAnalysisCtorFnTy MCInstrAnalysisCtorFn;
182
183     /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
184     /// if registered.
185     MCRegInfoCtorFnTy MCRegInfoCtorFn;
186
187     /// MCSubtargetInfoCtorFn - Constructor function for this target's
188     /// MCSubtargetInfo, if registered.
189     MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn;
190
191     /// TargetMachineCtorFn - Construction function for this target's
192     /// TargetMachine, if registered.
193     TargetMachineCtorTy TargetMachineCtorFn;
194
195     /// MCAsmBackendCtorFn - Construction function for this target's
196     /// MCAsmBackend, if registered.
197     MCAsmBackendCtorTy MCAsmBackendCtorFn;
198
199     /// MCAsmParserCtorFn - Construction function for this target's
200     /// MCTargetAsmParser, if registered.
201     MCAsmParserCtorTy MCAsmParserCtorFn;
202
203     /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
204     /// if registered.
205     AsmPrinterCtorTy AsmPrinterCtorFn;
206
207     /// MCDisassemblerCtorFn - Construction function for this target's
208     /// MCDisassembler, if registered.
209     MCDisassemblerCtorTy MCDisassemblerCtorFn;
210
211     /// MCInstPrinterCtorFn - Construction function for this target's
212     /// MCInstPrinter, if registered.
213     MCInstPrinterCtorTy MCInstPrinterCtorFn;
214
215     /// MCCodeEmitterCtorFn - Construction function for this target's
216     /// CodeEmitter, if registered.
217     MCCodeEmitterCtorTy MCCodeEmitterCtorFn;
218
219     /// MCObjectStreamerCtorFn - Construction function for this target's
220     /// MCObjectStreamer, if registered.
221     MCObjectStreamerCtorTy MCObjectStreamerCtorFn;
222
223     /// AsmStreamerCtorFn - Construction function for this target's
224     /// AsmStreamer, if registered (default = llvm::createAsmStreamer).
225     AsmStreamerCtorTy AsmStreamerCtorFn;
226
227     /// MCRelocationInfoCtorFn - Construction function for this target's
228     /// MCRelocationInfo, if registered (default = llvm::createMCRelocationInfo)
229     MCRelocationInfoCtorTy MCRelocationInfoCtorFn;
230
231     /// MCSymbolizerCtorFn - Construction function for this target's
232     /// MCSymbolizer, if registered (default = llvm::createMCSymbolizer)
233     MCSymbolizerCtorTy MCSymbolizerCtorFn;
234
235   public:
236     Target()
237         : AsmStreamerCtorFn(0), MCRelocationInfoCtorFn(0),
238           MCSymbolizerCtorFn(0) {}
239
240     /// @name Target Information
241     /// @{
242
243     // getNext - Return the next registered target.
244     const Target *getNext() const { return Next; }
245
246     /// getName - Get the target name.
247     const char *getName() const { return Name; }
248
249     /// getShortDescription - Get a short description of the target.
250     const char *getShortDescription() const { return ShortDesc; }
251
252     /// @}
253     /// @name Feature Predicates
254     /// @{
255
256     /// hasJIT - Check if this targets supports the just-in-time compilation.
257     bool hasJIT() const { return HasJIT; }
258
259     /// hasTargetMachine - Check if this target supports code generation.
260     bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
261
262     /// hasMCAsmBackend - Check if this target supports .o generation.
263     bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != 0; }
264
265     /// @}
266     /// @name Feature Constructors
267     /// @{
268
269     /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
270     /// target triple.
271     ///
272     /// \param Triple This argument is used to determine the target machine
273     /// feature set; it should always be provided. Generally this should be
274     /// either the target triple from the module, or the target triple of the
275     /// host if that does not exist.
276     MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI,
277                                StringRef Triple) const {
278       if (!MCAsmInfoCtorFn)
279         return 0;
280       return MCAsmInfoCtorFn(MRI, Triple);
281     }
282
283     /// createMCCodeGenInfo - Create a MCCodeGenInfo implementation.
284     ///
285     MCCodeGenInfo *createMCCodeGenInfo(StringRef Triple, Reloc::Model RM,
286                                        CodeModel::Model CM,
287                                        CodeGenOpt::Level OL) const {
288       if (!MCCodeGenInfoCtorFn)
289         return 0;
290       return MCCodeGenInfoCtorFn(Triple, RM, CM, OL);
291     }
292
293     /// createMCInstrInfo - Create a MCInstrInfo implementation.
294     ///
295     MCInstrInfo *createMCInstrInfo() const {
296       if (!MCInstrInfoCtorFn)
297         return 0;
298       return MCInstrInfoCtorFn();
299     }
300
301     /// createMCInstrAnalysis - Create a MCInstrAnalysis implementation.
302     ///
303     MCInstrAnalysis *createMCInstrAnalysis(const MCInstrInfo *Info) const {
304       if (!MCInstrAnalysisCtorFn)
305         return 0;
306       return MCInstrAnalysisCtorFn(Info);
307     }
308
309     /// createMCRegInfo - Create a MCRegisterInfo implementation.
310     ///
311     MCRegisterInfo *createMCRegInfo(StringRef Triple) const {
312       if (!MCRegInfoCtorFn)
313         return 0;
314       return MCRegInfoCtorFn(Triple);
315     }
316
317     /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
318     ///
319     /// \param Triple This argument is used to determine the target machine
320     /// feature set; it should always be provided. Generally this should be
321     /// either the target triple from the module, or the target triple of the
322     /// host if that does not exist.
323     /// \param CPU This specifies the name of the target CPU.
324     /// \param Features This specifies the string representation of the
325     /// additional target features.
326     MCSubtargetInfo *createMCSubtargetInfo(StringRef Triple, StringRef CPU,
327                                            StringRef Features) const {
328       if (!MCSubtargetInfoCtorFn)
329         return 0;
330       return MCSubtargetInfoCtorFn(Triple, CPU, Features);
331     }
332
333     /// createTargetMachine - Create a target specific machine implementation
334     /// for the specified \p Triple.
335     ///
336     /// \param Triple This argument is used to determine the target machine
337     /// feature set; it should always be provided. Generally this should be
338     /// either the target triple from the module, or the target triple of the
339     /// host if that does not exist.
340     TargetMachine *createTargetMachine(StringRef Triple, StringRef CPU,
341                              StringRef Features, const TargetOptions &Options,
342                              Reloc::Model RM = Reloc::Default,
343                              CodeModel::Model CM = CodeModel::Default,
344                              CodeGenOpt::Level OL = CodeGenOpt::Default) const {
345       if (!TargetMachineCtorFn)
346         return 0;
347       return TargetMachineCtorFn(*this, Triple, CPU, Features, Options,
348                                  RM, CM, OL);
349     }
350
351     /// createMCAsmBackend - Create a target specific assembly parser.
352     ///
353     /// \param Triple The target triple string.
354     MCAsmBackend *createMCAsmBackend(const MCRegisterInfo &MRI,
355                                      StringRef Triple, StringRef CPU) const {
356       if (!MCAsmBackendCtorFn)
357         return 0;
358       return MCAsmBackendCtorFn(*this, MRI, Triple, CPU);
359     }
360
361     /// createMCAsmParser - Create a target specific assembly parser.
362     ///
363     /// \param Parser The target independent parser implementation to use for
364     /// parsing and lexing.
365     MCTargetAsmParser *createMCAsmParser(MCSubtargetInfo &STI,
366                                          MCAsmParser &Parser,
367                                          const MCInstrInfo &MII) const {
368       if (!MCAsmParserCtorFn)
369         return 0;
370       return MCAsmParserCtorFn(STI, Parser, MII);
371     }
372
373     /// createAsmPrinter - Create a target specific assembly printer pass.  This
374     /// takes ownership of the MCStreamer object.
375     AsmPrinter *createAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) const{
376       if (!AsmPrinterCtorFn)
377         return 0;
378       return AsmPrinterCtorFn(TM, Streamer);
379     }
380
381     MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI) const {
382       if (!MCDisassemblerCtorFn)
383         return 0;
384       return MCDisassemblerCtorFn(*this, STI);
385     }
386
387     MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
388                                        const MCAsmInfo &MAI,
389                                        const MCInstrInfo &MII,
390                                        const MCRegisterInfo &MRI,
391                                        const MCSubtargetInfo &STI) const {
392       if (!MCInstPrinterCtorFn)
393         return 0;
394       return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI, MII, MRI, STI);
395     }
396
397
398     /// createMCCodeEmitter - Create a target specific code emitter.
399     MCCodeEmitter *createMCCodeEmitter(const MCInstrInfo &II,
400                                        const MCRegisterInfo &MRI,
401                                        const MCSubtargetInfo &STI,
402                                        MCContext &Ctx) const {
403       if (!MCCodeEmitterCtorFn)
404         return 0;
405       return MCCodeEmitterCtorFn(II, MRI, STI, Ctx);
406     }
407
408     /// createMCObjectStreamer - Create a target specific MCStreamer.
409     ///
410     /// \param TT The target triple.
411     /// \param Ctx The target context.
412     /// \param TAB The target assembler backend object. Takes ownership.
413     /// \param _OS The stream object.
414     /// \param _Emitter The target independent assembler object.Takes ownership.
415     /// \param RelaxAll Relax all fixups?
416     /// \param NoExecStack Mark file as not needing a executable stack.
417     MCStreamer *createMCObjectStreamer(StringRef TT, MCContext &Ctx,
418                                        MCAsmBackend &TAB,
419                                        raw_ostream &_OS,
420                                        MCCodeEmitter *_Emitter,
421                                        bool RelaxAll,
422                                        bool NoExecStack) const {
423       if (!MCObjectStreamerCtorFn)
424         return 0;
425       return MCObjectStreamerCtorFn(*this, TT, Ctx, TAB, _OS, _Emitter,
426                                     RelaxAll, NoExecStack);
427     }
428
429     /// createAsmStreamer - Create a target specific MCStreamer.
430     MCStreamer *createAsmStreamer(MCContext &Ctx,
431                                   formatted_raw_ostream &OS,
432                                   bool isVerboseAsm,
433                                   bool useLoc,
434                                   bool useCFI,
435                                   bool useDwarfDirectory,
436                                   MCInstPrinter *InstPrint,
437                                   MCCodeEmitter *CE,
438                                   MCAsmBackend *TAB,
439                                   bool ShowInst) const {
440       if (AsmStreamerCtorFn)
441         return AsmStreamerCtorFn(Ctx, OS, isVerboseAsm, useLoc, useCFI,
442                                  useDwarfDirectory, InstPrint, CE, TAB,
443                                  ShowInst);
444       return llvm::createAsmStreamer(Ctx, OS, isVerboseAsm, useLoc, useCFI,
445                                      useDwarfDirectory, InstPrint, CE, TAB,
446                                      ShowInst);
447     }
448
449     /// createMCRelocationInfo - Create a target specific MCRelocationInfo.
450     ///
451     /// \param TT The target triple.
452     /// \param Ctx The target context.
453     MCRelocationInfo *
454       createMCRelocationInfo(StringRef TT, MCContext &Ctx) const {
455       MCRelocationInfoCtorTy Fn = MCRelocationInfoCtorFn
456                                       ? MCRelocationInfoCtorFn
457                                       : llvm::createMCRelocationInfo;
458       return Fn(TT, Ctx);
459     }
460
461     /// createMCSymbolizer - Create a target specific MCSymbolizer.
462     ///
463     /// \param TT The target triple.
464     /// \param GetOpInfo The function to get the symbolic information for operands.
465     /// \param SymbolLookUp The function to lookup a symbol name.
466     /// \param DisInfo The pointer to the block of symbolic information for above call
467     /// back.
468     /// \param Ctx The target context.
469     /// \param RelInfo The relocation information for this target. Takes ownership.
470     MCSymbolizer *
471     createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
472                        LLVMSymbolLookupCallback SymbolLookUp,
473                        void *DisInfo,
474                        MCContext *Ctx, MCRelocationInfo *RelInfo) const {
475       MCSymbolizerCtorTy Fn =
476           MCSymbolizerCtorFn ? MCSymbolizerCtorFn : llvm::createMCSymbolizer;
477       return Fn(TT, GetOpInfo, SymbolLookUp, DisInfo, Ctx, RelInfo);
478     }
479
480     /// @}
481   };
482
483   /// TargetRegistry - Generic interface to target specific features.
484   struct TargetRegistry {
485     class iterator {
486       const Target *Current;
487       explicit iterator(Target *T) : Current(T) {}
488       friend struct TargetRegistry;
489     public:
490       iterator(const iterator &I) : Current(I.Current) {}
491       iterator() : Current(0) {}
492
493       bool operator==(const iterator &x) const {
494         return Current == x.Current;
495       }
496       bool operator!=(const iterator &x) const {
497         return !operator==(x);
498       }
499
500       // Iterator traversal: forward iteration only
501       iterator &operator++() {          // Preincrement
502         assert(Current && "Cannot increment end iterator!");
503         Current = Current->getNext();
504         return *this;
505       }
506       iterator operator++(int) {        // Postincrement
507         iterator tmp = *this;
508         ++*this;
509         return tmp;
510       }
511
512       const Target &operator*() const {
513         assert(Current && "Cannot dereference end iterator!");
514         return *Current;
515       }
516
517       const Target *operator->() const {
518         return &operator*();
519       }
520     };
521
522     /// printRegisteredTargetsForVersion - Print the registered targets
523     /// appropriately for inclusion in a tool's version output.
524     static void printRegisteredTargetsForVersion();
525
526     /// @name Registry Access
527     /// @{
528
529     static iterator begin();
530
531     static iterator end() { return iterator(); }
532
533     /// lookupTarget - Lookup a target based on a target triple.
534     ///
535     /// \param Triple - The triple to use for finding a target.
536     /// \param Error - On failure, an error string describing why no target was
537     /// found.
538     static const Target *lookupTarget(const std::string &Triple,
539                                       std::string &Error);
540
541     /// lookupTarget - Lookup a target based on an architecture name
542     /// and a target triple.  If the architecture name is non-empty,
543     /// then the lookup is done by architecture.  Otherwise, the target
544     /// triple is used.
545     ///
546     /// \param ArchName - The architecture to use for finding a target.
547     /// \param TheTriple - The triple to use for finding a target.  The
548     /// triple is updated with canonical architecture name if a lookup
549     /// by architecture is done.
550     /// \param Error - On failure, an error string describing why no target was
551     /// found.
552     static const Target *lookupTarget(const std::string &ArchName,
553                                       Triple &TheTriple,
554                                       std::string &Error);
555
556     /// getClosestTargetForJIT - Pick the best target that is compatible with
557     /// the current host.  If no close target can be found, this returns null
558     /// and sets the Error string to a reason.
559     ///
560     /// Maintained for compatibility through 2.6.
561     static const Target *getClosestTargetForJIT(std::string &Error);
562
563     /// @}
564     /// @name Target Registration
565     /// @{
566
567     /// RegisterTarget - Register the given target. Attempts to register a
568     /// target which has already been registered will be ignored.
569     ///
570     /// Clients are responsible for ensuring that registration doesn't occur
571     /// while another thread is attempting to access the registry. Typically
572     /// this is done by initializing all targets at program startup.
573     ///
574     /// @param T - The target being registered.
575     /// @param Name - The target name. This should be a static string.
576     /// @param ShortDesc - A short target description. This should be a static
577     /// string.
578     /// @param ArchMatchFn - The arch match checking function for this target.
579     /// @param HasJIT - Whether the target supports JIT code
580     /// generation.
581     static void RegisterTarget(Target &T,
582                                const char *Name,
583                                const char *ShortDesc,
584                                Target::ArchMatchFnTy ArchMatchFn,
585                                bool HasJIT = false);
586
587     /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
588     /// given target.
589     ///
590     /// Clients are responsible for ensuring that registration doesn't occur
591     /// while another thread is attempting to access the registry. Typically
592     /// this is done by initializing all targets at program startup.
593     ///
594     /// @param T - The target being registered.
595     /// @param Fn - A function to construct a MCAsmInfo for the target.
596     static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
597       T.MCAsmInfoCtorFn = Fn;
598     }
599
600     /// RegisterMCCodeGenInfo - Register a MCCodeGenInfo implementation for the
601     /// given target.
602     ///
603     /// Clients are responsible for ensuring that registration doesn't occur
604     /// while another thread is attempting to access the registry. Typically
605     /// this is done by initializing all targets at program startup.
606     ///
607     /// @param T - The target being registered.
608     /// @param Fn - A function to construct a MCCodeGenInfo for the target.
609     static void RegisterMCCodeGenInfo(Target &T,
610                                      Target::MCCodeGenInfoCtorFnTy Fn) {
611       T.MCCodeGenInfoCtorFn = Fn;
612     }
613
614     /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
615     /// given target.
616     ///
617     /// Clients are responsible for ensuring that registration doesn't occur
618     /// while another thread is attempting to access the registry. Typically
619     /// this is done by initializing all targets at program startup.
620     ///
621     /// @param T - The target being registered.
622     /// @param Fn - A function to construct a MCInstrInfo for the target.
623     static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
624       T.MCInstrInfoCtorFn = Fn;
625     }
626
627     /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for
628     /// the given target.
629     static void RegisterMCInstrAnalysis(Target &T,
630                                         Target::MCInstrAnalysisCtorFnTy Fn) {
631       T.MCInstrAnalysisCtorFn = Fn;
632     }
633
634     /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
635     /// given target.
636     ///
637     /// Clients are responsible for ensuring that registration doesn't occur
638     /// while another thread is attempting to access the registry. Typically
639     /// this is done by initializing all targets at program startup.
640     ///
641     /// @param T - The target being registered.
642     /// @param Fn - A function to construct a MCRegisterInfo for the target.
643     static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
644       T.MCRegInfoCtorFn = Fn;
645     }
646
647     /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
648     /// the given target.
649     ///
650     /// Clients are responsible for ensuring that registration doesn't occur
651     /// while another thread is attempting to access the registry. Typically
652     /// this is done by initializing all targets at program startup.
653     ///
654     /// @param T - The target being registered.
655     /// @param Fn - A function to construct a MCSubtargetInfo for the target.
656     static void RegisterMCSubtargetInfo(Target &T,
657                                         Target::MCSubtargetInfoCtorFnTy Fn) {
658       T.MCSubtargetInfoCtorFn = Fn;
659     }
660
661     /// RegisterTargetMachine - Register a TargetMachine implementation for the
662     /// given target.
663     ///
664     /// Clients are responsible for ensuring that registration doesn't occur
665     /// while another thread is attempting to access the registry. Typically
666     /// this is done by initializing all targets at program startup.
667     ///
668     /// @param T - The target being registered.
669     /// @param Fn - A function to construct a TargetMachine for the target.
670     static void RegisterTargetMachine(Target &T,
671                                       Target::TargetMachineCtorTy Fn) {
672       T.TargetMachineCtorFn = Fn;
673     }
674
675     /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the
676     /// given target.
677     ///
678     /// Clients are responsible for ensuring that registration doesn't occur
679     /// while another thread is attempting to access the registry. Typically
680     /// this is done by initializing all targets at program startup.
681     ///
682     /// @param T - The target being registered.
683     /// @param Fn - A function to construct an AsmBackend for the target.
684     static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) {
685       T.MCAsmBackendCtorFn = Fn;
686     }
687
688     /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for
689     /// the given target.
690     ///
691     /// Clients are responsible for ensuring that registration doesn't occur
692     /// while another thread is attempting to access the registry. Typically
693     /// this is done by initializing all targets at program startup.
694     ///
695     /// @param T - The target being registered.
696     /// @param Fn - A function to construct an MCTargetAsmParser for the target.
697     static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) {
698       T.MCAsmParserCtorFn = Fn;
699     }
700
701     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
702     /// target.
703     ///
704     /// Clients are responsible for ensuring that registration doesn't occur
705     /// while another thread is attempting to access the registry. Typically
706     /// this is done by initializing all targets at program startup.
707     ///
708     /// @param T - The target being registered.
709     /// @param Fn - A function to construct an AsmPrinter for the target.
710     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
711       T.AsmPrinterCtorFn = Fn;
712     }
713
714     /// RegisterMCDisassembler - Register a MCDisassembler implementation for
715     /// the given target.
716     ///
717     /// Clients are responsible for ensuring that registration doesn't occur
718     /// while another thread is attempting to access the registry. Typically
719     /// this is done by initializing all targets at program startup.
720     ///
721     /// @param T - The target being registered.
722     /// @param Fn - A function to construct an MCDisassembler for the target.
723     static void RegisterMCDisassembler(Target &T,
724                                        Target::MCDisassemblerCtorTy Fn) {
725       T.MCDisassemblerCtorFn = Fn;
726     }
727
728     /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
729     /// given target.
730     ///
731     /// Clients are responsible for ensuring that registration doesn't occur
732     /// while another thread is attempting to access the registry. Typically
733     /// this is done by initializing all targets at program startup.
734     ///
735     /// @param T - The target being registered.
736     /// @param Fn - A function to construct an MCInstPrinter for the target.
737     static void RegisterMCInstPrinter(Target &T,
738                                       Target::MCInstPrinterCtorTy Fn) {
739       T.MCInstPrinterCtorFn = Fn;
740     }
741
742     /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the
743     /// given target.
744     ///
745     /// Clients are responsible for ensuring that registration doesn't occur
746     /// while another thread is attempting to access the registry. Typically
747     /// this is done by initializing all targets at program startup.
748     ///
749     /// @param T - The target being registered.
750     /// @param Fn - A function to construct an MCCodeEmitter for the target.
751     static void RegisterMCCodeEmitter(Target &T,
752                                       Target::MCCodeEmitterCtorTy Fn) {
753       T.MCCodeEmitterCtorFn = Fn;
754     }
755
756     /// RegisterMCObjectStreamer - Register a object code MCStreamer
757     /// implementation for the given target.
758     ///
759     /// Clients are responsible for ensuring that registration doesn't occur
760     /// while another thread is attempting to access the registry. Typically
761     /// this is done by initializing all targets at program startup.
762     ///
763     /// @param T - The target being registered.
764     /// @param Fn - A function to construct an MCStreamer for the target.
765     static void RegisterMCObjectStreamer(Target &T,
766                                          Target::MCObjectStreamerCtorTy Fn) {
767       T.MCObjectStreamerCtorFn = Fn;
768     }
769
770     /// RegisterAsmStreamer - Register an assembly MCStreamer implementation
771     /// for the given target.
772     ///
773     /// Clients are responsible for ensuring that registration doesn't occur
774     /// while another thread is attempting to access the registry. Typically
775     /// this is done by initializing all targets at program startup.
776     ///
777     /// @param T - The target being registered.
778     /// @param Fn - A function to construct an MCStreamer for the target.
779     static void RegisterAsmStreamer(Target &T, Target::AsmStreamerCtorTy Fn) {
780       T.AsmStreamerCtorFn = 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       return new MCAsmParserImpl(STI, P, MII);
1104     }
1105   };
1106
1107   /// RegisterAsmPrinter - Helper template for registering a target specific
1108   /// assembly printer, for use in the target machine initialization
1109   /// function. Usage:
1110   ///
1111   /// extern "C" void LLVMInitializeFooAsmPrinter() {
1112   ///   extern Target TheFooTarget;
1113   ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
1114   /// }
1115   template<class AsmPrinterImpl>
1116   struct RegisterAsmPrinter {
1117     RegisterAsmPrinter(Target &T) {
1118       TargetRegistry::RegisterAsmPrinter(T, &Allocator);
1119     }
1120
1121   private:
1122     static AsmPrinter *Allocator(TargetMachine &TM, MCStreamer &Streamer) {
1123       return new AsmPrinterImpl(TM, Streamer);
1124     }
1125   };
1126
1127   /// RegisterMCCodeEmitter - Helper template for registering a target specific
1128   /// machine code emitter, for use in the target initialization
1129   /// function. Usage:
1130   ///
1131   /// extern "C" void LLVMInitializeFooMCCodeEmitter() {
1132   ///   extern Target TheFooTarget;
1133   ///   RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget);
1134   /// }
1135   template<class MCCodeEmitterImpl>
1136   struct RegisterMCCodeEmitter {
1137     RegisterMCCodeEmitter(Target &T) {
1138       TargetRegistry::RegisterMCCodeEmitter(T, &Allocator);
1139     }
1140
1141   private:
1142     static MCCodeEmitter *Allocator(const MCInstrInfo &/*II*/,
1143                                     const MCRegisterInfo &/*MRI*/,
1144                                     const MCSubtargetInfo &/*STI*/,
1145                                     MCContext &/*Ctx*/) {
1146       return new MCCodeEmitterImpl();
1147     }
1148   };
1149
1150 }
1151
1152 #endif