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