OSDN Git Service

am b5eb99b8: am 37a6adeb: Merge "Update to LLVM 3.5a."
[android-x86/external-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() : Current(0) {}
490
491       bool operator==(const iterator &x) const {
492         return Current == x.Current;
493       }
494       bool operator!=(const iterator &x) const {
495         return !operator==(x);
496       }
497
498       // Iterator traversal: forward iteration only
499       iterator &operator++() {          // Preincrement
500         assert(Current && "Cannot increment end iterator!");
501         Current = Current->getNext();
502         return *this;
503       }
504       iterator operator++(int) {        // Postincrement
505         iterator tmp = *this;
506         ++*this;
507         return tmp;
508       }
509
510       const Target &operator*() const {
511         assert(Current && "Cannot dereference end iterator!");
512         return *Current;
513       }
514
515       const Target *operator->() const {
516         return &operator*();
517       }
518     };
519
520     /// printRegisteredTargetsForVersion - Print the registered targets
521     /// appropriately for inclusion in a tool's version output.
522     static void printRegisteredTargetsForVersion();
523
524     /// @name Registry Access
525     /// @{
526
527     static iterator begin();
528
529     static iterator end() { return iterator(); }
530
531     /// lookupTarget - Lookup a target based on a target triple.
532     ///
533     /// \param Triple - The triple to use for finding a target.
534     /// \param Error - On failure, an error string describing why no target was
535     /// found.
536     static const Target *lookupTarget(const std::string &Triple,
537                                       std::string &Error);
538
539     /// lookupTarget - Lookup a target based on an architecture name
540     /// and a target triple.  If the architecture name is non-empty,
541     /// then the lookup is done by architecture.  Otherwise, the target
542     /// triple is used.
543     ///
544     /// \param ArchName - The architecture to use for finding a target.
545     /// \param TheTriple - The triple to use for finding a target.  The
546     /// triple is updated with canonical architecture name if a lookup
547     /// by architecture is done.
548     /// \param Error - On failure, an error string describing why no target was
549     /// found.
550     static const Target *lookupTarget(const std::string &ArchName,
551                                       Triple &TheTriple,
552                                       std::string &Error);
553
554     /// getClosestTargetForJIT - Pick the best target that is compatible with
555     /// the current host.  If no close target can be found, this returns null
556     /// and sets the Error string to a reason.
557     ///
558     /// Maintained for compatibility through 2.6.
559     static const Target *getClosestTargetForJIT(std::string &Error);
560
561     /// @}
562     /// @name Target Registration
563     /// @{
564
565     /// RegisterTarget - Register the given target. Attempts to register a
566     /// target which has already been registered will be ignored.
567     ///
568     /// Clients are responsible for ensuring that registration doesn't occur
569     /// while another thread is attempting to access the registry. Typically
570     /// this is done by initializing all targets at program startup.
571     ///
572     /// @param T - The target being registered.
573     /// @param Name - The target name. This should be a static string.
574     /// @param ShortDesc - A short target description. This should be a static
575     /// string.
576     /// @param ArchMatchFn - The arch match checking function for this target.
577     /// @param HasJIT - Whether the target supports JIT code
578     /// generation.
579     static void RegisterTarget(Target &T,
580                                const char *Name,
581                                const char *ShortDesc,
582                                Target::ArchMatchFnTy ArchMatchFn,
583                                bool HasJIT = false);
584
585     /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
586     /// given target.
587     ///
588     /// Clients are responsible for ensuring that registration doesn't occur
589     /// while another thread is attempting to access the registry. Typically
590     /// this is done by initializing all targets at program startup.
591     ///
592     /// @param T - The target being registered.
593     /// @param Fn - A function to construct a MCAsmInfo for the target.
594     static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
595       T.MCAsmInfoCtorFn = Fn;
596     }
597
598     /// RegisterMCCodeGenInfo - Register a MCCodeGenInfo implementation for the
599     /// given target.
600     ///
601     /// Clients are responsible for ensuring that registration doesn't occur
602     /// while another thread is attempting to access the registry. Typically
603     /// this is done by initializing all targets at program startup.
604     ///
605     /// @param T - The target being registered.
606     /// @param Fn - A function to construct a MCCodeGenInfo for the target.
607     static void RegisterMCCodeGenInfo(Target &T,
608                                      Target::MCCodeGenInfoCtorFnTy Fn) {
609       T.MCCodeGenInfoCtorFn = Fn;
610     }
611
612     /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
613     /// given target.
614     ///
615     /// Clients are responsible for ensuring that registration doesn't occur
616     /// while another thread is attempting to access the registry. Typically
617     /// this is done by initializing all targets at program startup.
618     ///
619     /// @param T - The target being registered.
620     /// @param Fn - A function to construct a MCInstrInfo for the target.
621     static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
622       T.MCInstrInfoCtorFn = Fn;
623     }
624
625     /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for
626     /// the given target.
627     static void RegisterMCInstrAnalysis(Target &T,
628                                         Target::MCInstrAnalysisCtorFnTy Fn) {
629       T.MCInstrAnalysisCtorFn = Fn;
630     }
631
632     /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
633     /// given target.
634     ///
635     /// Clients are responsible for ensuring that registration doesn't occur
636     /// while another thread is attempting to access the registry. Typically
637     /// this is done by initializing all targets at program startup.
638     ///
639     /// @param T - The target being registered.
640     /// @param Fn - A function to construct a MCRegisterInfo for the target.
641     static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
642       T.MCRegInfoCtorFn = Fn;
643     }
644
645     /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
646     /// the given target.
647     ///
648     /// Clients are responsible for ensuring that registration doesn't occur
649     /// while another thread is attempting to access the registry. Typically
650     /// this is done by initializing all targets at program startup.
651     ///
652     /// @param T - The target being registered.
653     /// @param Fn - A function to construct a MCSubtargetInfo for the target.
654     static void RegisterMCSubtargetInfo(Target &T,
655                                         Target::MCSubtargetInfoCtorFnTy Fn) {
656       T.MCSubtargetInfoCtorFn = Fn;
657     }
658
659     /// RegisterTargetMachine - Register a TargetMachine implementation for the
660     /// given target.
661     ///
662     /// Clients are responsible for ensuring that registration doesn't occur
663     /// while another thread is attempting to access the registry. Typically
664     /// this is done by initializing all targets at program startup.
665     ///
666     /// @param T - The target being registered.
667     /// @param Fn - A function to construct a TargetMachine for the target.
668     static void RegisterTargetMachine(Target &T,
669                                       Target::TargetMachineCtorTy Fn) {
670       T.TargetMachineCtorFn = Fn;
671     }
672
673     /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the
674     /// given target.
675     ///
676     /// Clients are responsible for ensuring that registration doesn't occur
677     /// while another thread is attempting to access the registry. Typically
678     /// this is done by initializing all targets at program startup.
679     ///
680     /// @param T - The target being registered.
681     /// @param Fn - A function to construct an AsmBackend for the target.
682     static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) {
683       T.MCAsmBackendCtorFn = Fn;
684     }
685
686     /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for
687     /// the given target.
688     ///
689     /// Clients are responsible for ensuring that registration doesn't occur
690     /// while another thread is attempting to access the registry. Typically
691     /// this is done by initializing all targets at program startup.
692     ///
693     /// @param T - The target being registered.
694     /// @param Fn - A function to construct an MCTargetAsmParser for the target.
695     static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) {
696       T.MCAsmParserCtorFn = Fn;
697     }
698
699     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
700     /// target.
701     ///
702     /// Clients are responsible for ensuring that registration doesn't occur
703     /// while another thread is attempting to access the registry. Typically
704     /// this is done by initializing all targets at program startup.
705     ///
706     /// @param T - The target being registered.
707     /// @param Fn - A function to construct an AsmPrinter for the target.
708     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
709       T.AsmPrinterCtorFn = Fn;
710     }
711
712     /// RegisterMCDisassembler - Register a MCDisassembler implementation for
713     /// the given target.
714     ///
715     /// Clients are responsible for ensuring that registration doesn't occur
716     /// while another thread is attempting to access the registry. Typically
717     /// this is done by initializing all targets at program startup.
718     ///
719     /// @param T - The target being registered.
720     /// @param Fn - A function to construct an MCDisassembler for the target.
721     static void RegisterMCDisassembler(Target &T,
722                                        Target::MCDisassemblerCtorTy Fn) {
723       T.MCDisassemblerCtorFn = Fn;
724     }
725
726     /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
727     /// given target.
728     ///
729     /// Clients are responsible for ensuring that registration doesn't occur
730     /// while another thread is attempting to access the registry. Typically
731     /// this is done by initializing all targets at program startup.
732     ///
733     /// @param T - The target being registered.
734     /// @param Fn - A function to construct an MCInstPrinter for the target.
735     static void RegisterMCInstPrinter(Target &T,
736                                       Target::MCInstPrinterCtorTy Fn) {
737       T.MCInstPrinterCtorFn = Fn;
738     }
739
740     /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the
741     /// given target.
742     ///
743     /// Clients are responsible for ensuring that registration doesn't occur
744     /// while another thread is attempting to access the registry. Typically
745     /// this is done by initializing all targets at program startup.
746     ///
747     /// @param T - The target being registered.
748     /// @param Fn - A function to construct an MCCodeEmitter for the target.
749     static void RegisterMCCodeEmitter(Target &T,
750                                       Target::MCCodeEmitterCtorTy Fn) {
751       T.MCCodeEmitterCtorFn = Fn;
752     }
753
754     /// RegisterMCObjectStreamer - Register a object code MCStreamer
755     /// implementation for the given target.
756     ///
757     /// Clients are responsible for ensuring that registration doesn't occur
758     /// while another thread is attempting to access the registry. Typically
759     /// this is done by initializing all targets at program startup.
760     ///
761     /// @param T - The target being registered.
762     /// @param Fn - A function to construct an MCStreamer for the target.
763     static void RegisterMCObjectStreamer(Target &T,
764                                          Target::MCObjectStreamerCtorTy Fn) {
765       T.MCObjectStreamerCtorFn = Fn;
766     }
767
768     /// RegisterAsmStreamer - Register an assembly MCStreamer implementation
769     /// for the given target.
770     ///
771     /// Clients are responsible for ensuring that registration doesn't occur
772     /// while another thread is attempting to access the registry. Typically
773     /// this is done by initializing all targets at program startup.
774     ///
775     /// @param T - The target being registered.
776     /// @param Fn - A function to construct an MCStreamer for the target.
777     static void RegisterAsmStreamer(Target &T, Target::AsmStreamerCtorTy Fn) {
778       T.AsmStreamerCtorFn = Fn;
779     }
780
781     /// RegisterMCRelocationInfo - Register an MCRelocationInfo
782     /// implementation for the given target.
783     ///
784     /// Clients are responsible for ensuring that registration doesn't occur
785     /// while another thread is attempting to access the registry. Typically
786     /// this is done by initializing all targets at program startup.
787     ///
788     /// @param T - The target being registered.
789     /// @param Fn - A function to construct an MCRelocationInfo for the target.
790     static void RegisterMCRelocationInfo(Target &T,
791                                          Target::MCRelocationInfoCtorTy Fn) {
792       T.MCRelocationInfoCtorFn = Fn;
793     }
794
795     /// RegisterMCSymbolizer - Register an MCSymbolizer
796     /// implementation for the given target.
797     ///
798     /// Clients are responsible for ensuring that registration doesn't occur
799     /// while another thread is attempting to access the registry. Typically
800     /// this is done by initializing all targets at program startup.
801     ///
802     /// @param T - The target being registered.
803     /// @param Fn - A function to construct an MCSymbolizer for the target.
804     static void RegisterMCSymbolizer(Target &T,
805                                      Target::MCSymbolizerCtorTy Fn) {
806       T.MCSymbolizerCtorFn = Fn;
807     }
808
809     /// @}
810   };
811
812
813   //===--------------------------------------------------------------------===//
814
815   /// RegisterTarget - Helper template for registering a target, for use in the
816   /// target's initialization function. Usage:
817   ///
818   ///
819   /// Target TheFooTarget; // The global target instance.
820   ///
821   /// extern "C" void LLVMInitializeFooTargetInfo() {
822   ///   RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
823   /// }
824   template<Triple::ArchType TargetArchType = Triple::UnknownArch,
825            bool HasJIT = false>
826   struct RegisterTarget {
827     RegisterTarget(Target &T, const char *Name, const char *Desc) {
828       TargetRegistry::RegisterTarget(T, Name, Desc, &getArchMatch, HasJIT);
829     }
830
831     static bool getArchMatch(Triple::ArchType Arch) {
832       return Arch == TargetArchType;
833     }
834   };
835
836   /// RegisterMCAsmInfo - Helper template for registering a target assembly info
837   /// implementation.  This invokes the static "Create" method on the class to
838   /// actually do the construction.  Usage:
839   ///
840   /// extern "C" void LLVMInitializeFooTarget() {
841   ///   extern Target TheFooTarget;
842   ///   RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
843   /// }
844   template<class MCAsmInfoImpl>
845   struct RegisterMCAsmInfo {
846     RegisterMCAsmInfo(Target &T) {
847       TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
848     }
849   private:
850     static MCAsmInfo *Allocator(const MCRegisterInfo &/*MRI*/, StringRef TT) {
851       return new MCAsmInfoImpl(TT);
852     }
853
854   };
855
856   /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
857   /// implementation.  This invokes the specified function to do the
858   /// construction.  Usage:
859   ///
860   /// extern "C" void LLVMInitializeFooTarget() {
861   ///   extern Target TheFooTarget;
862   ///   RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
863   /// }
864   struct RegisterMCAsmInfoFn {
865     RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
866       TargetRegistry::RegisterMCAsmInfo(T, Fn);
867     }
868   };
869
870   /// RegisterMCCodeGenInfo - Helper template for registering a target codegen info
871   /// implementation.  This invokes the static "Create" method on the class
872   /// to actually do the construction.  Usage:
873   ///
874   /// extern "C" void LLVMInitializeFooTarget() {
875   ///   extern Target TheFooTarget;
876   ///   RegisterMCCodeGenInfo<FooMCCodeGenInfo> X(TheFooTarget);
877   /// }
878   template<class MCCodeGenInfoImpl>
879   struct RegisterMCCodeGenInfo {
880     RegisterMCCodeGenInfo(Target &T) {
881       TargetRegistry::RegisterMCCodeGenInfo(T, &Allocator);
882     }
883   private:
884     static MCCodeGenInfo *Allocator(StringRef /*TT*/, Reloc::Model /*RM*/,
885                                     CodeModel::Model /*CM*/,
886                                     CodeGenOpt::Level /*OL*/) {
887       return new MCCodeGenInfoImpl();
888     }
889   };
890
891   /// RegisterMCCodeGenInfoFn - Helper template for registering a target codegen
892   /// info implementation.  This invokes the specified function to do the
893   /// construction.  Usage:
894   ///
895   /// extern "C" void LLVMInitializeFooTarget() {
896   ///   extern Target TheFooTarget;
897   ///   RegisterMCCodeGenInfoFn X(TheFooTarget, TheFunction);
898   /// }
899   struct RegisterMCCodeGenInfoFn {
900     RegisterMCCodeGenInfoFn(Target &T, Target::MCCodeGenInfoCtorFnTy Fn) {
901       TargetRegistry::RegisterMCCodeGenInfo(T, Fn);
902     }
903   };
904
905   /// RegisterMCInstrInfo - Helper template for registering a target instruction
906   /// info implementation.  This invokes the static "Create" method on the class
907   /// to actually do the construction.  Usage:
908   ///
909   /// extern "C" void LLVMInitializeFooTarget() {
910   ///   extern Target TheFooTarget;
911   ///   RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
912   /// }
913   template<class MCInstrInfoImpl>
914   struct RegisterMCInstrInfo {
915     RegisterMCInstrInfo(Target &T) {
916       TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
917     }
918   private:
919     static MCInstrInfo *Allocator() {
920       return new MCInstrInfoImpl();
921     }
922   };
923
924   /// RegisterMCInstrInfoFn - Helper template for registering a target
925   /// instruction info implementation.  This invokes the specified function to
926   /// do the construction.  Usage:
927   ///
928   /// extern "C" void LLVMInitializeFooTarget() {
929   ///   extern Target TheFooTarget;
930   ///   RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
931   /// }
932   struct RegisterMCInstrInfoFn {
933     RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
934       TargetRegistry::RegisterMCInstrInfo(T, Fn);
935     }
936   };
937
938   /// RegisterMCInstrAnalysis - Helper template for registering a target
939   /// instruction analyzer implementation.  This invokes the static "Create"
940   /// method on the class to actually do the construction.  Usage:
941   ///
942   /// extern "C" void LLVMInitializeFooTarget() {
943   ///   extern Target TheFooTarget;
944   ///   RegisterMCInstrAnalysis<FooMCInstrAnalysis> X(TheFooTarget);
945   /// }
946   template<class MCInstrAnalysisImpl>
947   struct RegisterMCInstrAnalysis {
948     RegisterMCInstrAnalysis(Target &T) {
949       TargetRegistry::RegisterMCInstrAnalysis(T, &Allocator);
950     }
951   private:
952     static MCInstrAnalysis *Allocator(const MCInstrInfo *Info) {
953       return new MCInstrAnalysisImpl(Info);
954     }
955   };
956
957   /// RegisterMCInstrAnalysisFn - Helper template for registering a target
958   /// instruction analyzer implementation.  This invokes the specified function
959   /// to do the construction.  Usage:
960   ///
961   /// extern "C" void LLVMInitializeFooTarget() {
962   ///   extern Target TheFooTarget;
963   ///   RegisterMCInstrAnalysisFn X(TheFooTarget, TheFunction);
964   /// }
965   struct RegisterMCInstrAnalysisFn {
966     RegisterMCInstrAnalysisFn(Target &T, Target::MCInstrAnalysisCtorFnTy Fn) {
967       TargetRegistry::RegisterMCInstrAnalysis(T, Fn);
968     }
969   };
970
971   /// RegisterMCRegInfo - Helper template for registering a target register info
972   /// implementation.  This invokes the static "Create" method on the class to
973   /// actually do the construction.  Usage:
974   ///
975   /// extern "C" void LLVMInitializeFooTarget() {
976   ///   extern Target TheFooTarget;
977   ///   RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
978   /// }
979   template<class MCRegisterInfoImpl>
980   struct RegisterMCRegInfo {
981     RegisterMCRegInfo(Target &T) {
982       TargetRegistry::RegisterMCRegInfo(T, &Allocator);
983     }
984   private:
985     static MCRegisterInfo *Allocator(StringRef /*TT*/) {
986       return new MCRegisterInfoImpl();
987     }
988   };
989
990   /// RegisterMCRegInfoFn - Helper template for registering a target register
991   /// info implementation.  This invokes the specified function to do the
992   /// construction.  Usage:
993   ///
994   /// extern "C" void LLVMInitializeFooTarget() {
995   ///   extern Target TheFooTarget;
996   ///   RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
997   /// }
998   struct RegisterMCRegInfoFn {
999     RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
1000       TargetRegistry::RegisterMCRegInfo(T, Fn);
1001     }
1002   };
1003
1004   /// RegisterMCSubtargetInfo - Helper template for registering a target
1005   /// subtarget info implementation.  This invokes the static "Create" method
1006   /// on the class to actually do the construction.  Usage:
1007   ///
1008   /// extern "C" void LLVMInitializeFooTarget() {
1009   ///   extern Target TheFooTarget;
1010   ///   RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
1011   /// }
1012   template<class MCSubtargetInfoImpl>
1013   struct RegisterMCSubtargetInfo {
1014     RegisterMCSubtargetInfo(Target &T) {
1015       TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
1016     }
1017   private:
1018     static MCSubtargetInfo *Allocator(StringRef /*TT*/, StringRef /*CPU*/,
1019                                       StringRef /*FS*/) {
1020       return new MCSubtargetInfoImpl();
1021     }
1022   };
1023
1024   /// RegisterMCSubtargetInfoFn - Helper template for registering a target
1025   /// subtarget info implementation.  This invokes the specified function to
1026   /// do the construction.  Usage:
1027   ///
1028   /// extern "C" void LLVMInitializeFooTarget() {
1029   ///   extern Target TheFooTarget;
1030   ///   RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
1031   /// }
1032   struct RegisterMCSubtargetInfoFn {
1033     RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
1034       TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
1035     }
1036   };
1037
1038   /// RegisterTargetMachine - Helper template for registering a target machine
1039   /// implementation, for use in the target machine initialization
1040   /// function. Usage:
1041   ///
1042   /// extern "C" void LLVMInitializeFooTarget() {
1043   ///   extern Target TheFooTarget;
1044   ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
1045   /// }
1046   template<class TargetMachineImpl>
1047   struct RegisterTargetMachine {
1048     RegisterTargetMachine(Target &T) {
1049       TargetRegistry::RegisterTargetMachine(T, &Allocator);
1050     }
1051
1052   private:
1053     static TargetMachine *Allocator(const Target &T, StringRef TT,
1054                                     StringRef CPU, StringRef FS,
1055                                     const TargetOptions &Options,
1056                                     Reloc::Model RM,
1057                                     CodeModel::Model CM,
1058                                     CodeGenOpt::Level OL) {
1059       return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL);
1060     }
1061   };
1062
1063   /// RegisterMCAsmBackend - Helper template for registering a target specific
1064   /// assembler backend. Usage:
1065   ///
1066   /// extern "C" void LLVMInitializeFooMCAsmBackend() {
1067   ///   extern Target TheFooTarget;
1068   ///   RegisterMCAsmBackend<FooAsmLexer> X(TheFooTarget);
1069   /// }
1070   template<class MCAsmBackendImpl>
1071   struct RegisterMCAsmBackend {
1072     RegisterMCAsmBackend(Target &T) {
1073       TargetRegistry::RegisterMCAsmBackend(T, &Allocator);
1074     }
1075
1076   private:
1077     static MCAsmBackend *Allocator(const Target &T,
1078                                    const MCRegisterInfo &MRI,
1079                                    StringRef Triple, StringRef CPU) {
1080       return new MCAsmBackendImpl(T, MRI, Triple, CPU);
1081     }
1082   };
1083
1084   /// RegisterMCAsmParser - Helper template for registering a target specific
1085   /// assembly parser, for use in the target machine initialization
1086   /// function. Usage:
1087   ///
1088   /// extern "C" void LLVMInitializeFooMCAsmParser() {
1089   ///   extern Target TheFooTarget;
1090   ///   RegisterMCAsmParser<FooAsmParser> X(TheFooTarget);
1091   /// }
1092   template<class MCAsmParserImpl>
1093   struct RegisterMCAsmParser {
1094     RegisterMCAsmParser(Target &T) {
1095       TargetRegistry::RegisterMCAsmParser(T, &Allocator);
1096     }
1097
1098   private:
1099     static MCTargetAsmParser *Allocator(MCSubtargetInfo &STI, MCAsmParser &P,
1100                                         const MCInstrInfo &MII) {
1101       return new MCAsmParserImpl(STI, P, MII);
1102     }
1103   };
1104
1105   /// RegisterAsmPrinter - Helper template for registering a target specific
1106   /// assembly printer, for use in the target machine initialization
1107   /// function. Usage:
1108   ///
1109   /// extern "C" void LLVMInitializeFooAsmPrinter() {
1110   ///   extern Target TheFooTarget;
1111   ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
1112   /// }
1113   template<class AsmPrinterImpl>
1114   struct RegisterAsmPrinter {
1115     RegisterAsmPrinter(Target &T) {
1116       TargetRegistry::RegisterAsmPrinter(T, &Allocator);
1117     }
1118
1119   private:
1120     static AsmPrinter *Allocator(TargetMachine &TM, MCStreamer &Streamer) {
1121       return new AsmPrinterImpl(TM, Streamer);
1122     }
1123   };
1124
1125   /// RegisterMCCodeEmitter - Helper template for registering a target specific
1126   /// machine code emitter, for use in the target initialization
1127   /// function. Usage:
1128   ///
1129   /// extern "C" void LLVMInitializeFooMCCodeEmitter() {
1130   ///   extern Target TheFooTarget;
1131   ///   RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget);
1132   /// }
1133   template<class MCCodeEmitterImpl>
1134   struct RegisterMCCodeEmitter {
1135     RegisterMCCodeEmitter(Target &T) {
1136       TargetRegistry::RegisterMCCodeEmitter(T, &Allocator);
1137     }
1138
1139   private:
1140     static MCCodeEmitter *Allocator(const MCInstrInfo &/*II*/,
1141                                     const MCRegisterInfo &/*MRI*/,
1142                                     const MCSubtargetInfo &/*STI*/,
1143                                     MCContext &/*Ctx*/) {
1144       return new MCCodeEmitterImpl();
1145     }
1146   };
1147
1148 }
1149
1150 #endif