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