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