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