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