]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/include/llvm/Target/TargetRegistry.h
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / include / llvm / Target / TargetRegistry.h
1 //===-- Target/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_TARGET_TARGETREGISTRY_H
20 #define LLVM_TARGET_TARGETREGISTRY_H
21
22 #include "llvm/ADT/Triple.h"
23 #include <string>
24 #include <cassert>
25
26 namespace llvm {
27   class AsmPrinter;
28   class Module;
29   class MCAssembler;
30   class MCAsmInfo;
31   class MCAsmParser;
32   class MCCodeEmitter;
33   class MCContext;
34   class MCDisassembler;
35   class MCInstPrinter;
36   class MCInstrInfo;
37   class MCRegisterInfo;
38   class MCStreamer;
39   class MCSubtargetInfo;
40   class TargetAsmBackend;
41   class TargetAsmLexer;
42   class TargetAsmParser;
43   class TargetMachine;
44   class raw_ostream;
45   class formatted_raw_ostream;
46
47   MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
48                                 bool isVerboseAsm,
49                                 bool useLoc, bool useCFI,
50                                 MCInstPrinter *InstPrint,
51                                 MCCodeEmitter *CE,
52                                 TargetAsmBackend *TAB,
53                                 bool ShowInst);
54
55   /// Target - Wrapper for Target specific information.
56   ///
57   /// For registration purposes, this is a POD type so that targets can be
58   /// registered without the use of static constructors.
59   ///
60   /// Targets should implement a single global instance of this class (which
61   /// will be zero initialized), and pass that instance to the TargetRegistry as
62   /// part of their initialization.
63   class Target {
64   public:
65     friend struct TargetRegistry;
66
67     typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
68
69     typedef MCAsmInfo *(*MCAsmInfoCtorFnTy)(const Target &T,
70                                             StringRef TT);
71     typedef MCInstrInfo *(*MCInstrInfoCtorFnTy)(void);
72     typedef MCRegisterInfo *(*MCRegInfoCtorFnTy)(void);
73     typedef MCSubtargetInfo *(*MCSubtargetInfoCtorFnTy)(StringRef TT,
74                                                         StringRef CPU,
75                                                         StringRef Features);
76     typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
77                                                   const std::string &TT,
78                                                   const std::string &CPU,
79                                                   const std::string &Features);
80     typedef AsmPrinter *(*AsmPrinterCtorTy)(TargetMachine &TM,
81                                             MCStreamer &Streamer);
82     typedef TargetAsmBackend *(*AsmBackendCtorTy)(const Target &T,
83                                                   const std::string &TT);
84     typedef TargetAsmLexer *(*AsmLexerCtorTy)(const Target &T,
85                                               const MCAsmInfo &MAI);
86     typedef TargetAsmParser *(*AsmParserCtorTy)(MCSubtargetInfo &STI,
87                                                 MCAsmParser &P);
88     typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T);
89     typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
90                                                   unsigned SyntaxVariant,
91                                                   const MCAsmInfo &MAI);
92     typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const MCInstrInfo &II,
93                                                 const MCSubtargetInfo &STI,
94                                                 MCContext &Ctx);
95     typedef MCStreamer *(*ObjectStreamerCtorTy)(const Target &T,
96                                                 const std::string &TT,
97                                                 MCContext &Ctx,
98                                                 TargetAsmBackend &TAB,
99                                                 raw_ostream &_OS,
100                                                 MCCodeEmitter *_Emitter,
101                                                 bool RelaxAll,
102                                                 bool NoExecStack);
103     typedef MCStreamer *(*AsmStreamerCtorTy)(MCContext &Ctx,
104                                              formatted_raw_ostream &OS,
105                                              bool isVerboseAsm,
106                                              bool useLoc,
107                                              bool useCFI,
108                                              MCInstPrinter *InstPrint,
109                                              MCCodeEmitter *CE,
110                                              TargetAsmBackend *TAB,
111                                              bool ShowInst);
112
113   private:
114     /// Next - The next registered target in the linked list, maintained by the
115     /// TargetRegistry.
116     Target *Next;
117
118     /// TripleMatchQualityFn - The target function for rating the match quality
119     /// of a triple.
120     TripleMatchQualityFnTy TripleMatchQualityFn;
121
122     /// Name - The target name.
123     const char *Name;
124
125     /// ShortDesc - A short description of the target.
126     const char *ShortDesc;
127
128     /// HasJIT - Whether this target supports the JIT.
129     bool HasJIT;
130
131     /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
132     /// registered.
133     MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
134
135     /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
136     /// if registered.
137     MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
138
139     /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
140     /// if registered.
141     MCRegInfoCtorFnTy MCRegInfoCtorFn;
142
143     /// MCSubtargetInfoCtorFn - Constructor function for this target's
144     /// MCSubtargetInfo, if registered.
145     MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn;
146
147     /// TargetMachineCtorFn - Construction function for this target's
148     /// TargetMachine, if registered.
149     TargetMachineCtorTy TargetMachineCtorFn;
150
151     /// AsmBackendCtorFn - Construction function for this target's
152     /// TargetAsmBackend, if registered.
153     AsmBackendCtorTy AsmBackendCtorFn;
154
155     /// AsmLexerCtorFn - Construction function for this target's TargetAsmLexer,
156     /// if registered.
157     AsmLexerCtorTy AsmLexerCtorFn;
158
159     /// AsmParserCtorFn - Construction function for this target's
160     /// TargetAsmParser, if registered.
161     AsmParserCtorTy AsmParserCtorFn;
162
163     /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
164     /// if registered.
165     AsmPrinterCtorTy AsmPrinterCtorFn;
166
167     /// MCDisassemblerCtorFn - Construction function for this target's
168     /// MCDisassembler, if registered.
169     MCDisassemblerCtorTy MCDisassemblerCtorFn;
170
171     /// MCInstPrinterCtorFn - Construction function for this target's
172     /// MCInstPrinter, if registered.
173     MCInstPrinterCtorTy MCInstPrinterCtorFn;
174
175     /// CodeEmitterCtorFn - Construction function for this target's CodeEmitter,
176     /// if registered.
177     CodeEmitterCtorTy CodeEmitterCtorFn;
178
179     /// ObjectStreamerCtorFn - Construction function for this target's
180     /// ObjectStreamer, if registered.
181     ObjectStreamerCtorTy ObjectStreamerCtorFn;
182
183     /// AsmStreamerCtorFn - Construction function for this target's
184     /// AsmStreamer, if registered (default = llvm::createAsmStreamer).
185     AsmStreamerCtorTy AsmStreamerCtorFn;
186
187   public:
188     Target() : AsmStreamerCtorFn(llvm::createAsmStreamer) {}
189
190     /// @name Target Information
191     /// @{
192
193     // getNext - Return the next registered target.
194     const Target *getNext() const { return Next; }
195
196     /// getName - Get the target name.
197     const char *getName() const { return Name; }
198
199     /// getShortDescription - Get a short description of the target.
200     const char *getShortDescription() const { return ShortDesc; }
201
202     /// @}
203     /// @name Feature Predicates
204     /// @{
205
206     /// hasJIT - Check if this targets supports the just-in-time compilation.
207     bool hasJIT() const { return HasJIT; }
208
209     /// hasTargetMachine - Check if this target supports code generation.
210     bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
211
212     /// hasAsmBackend - Check if this target supports .o generation.
213     bool hasAsmBackend() const { return AsmBackendCtorFn != 0; }
214
215     /// hasAsmLexer - Check if this target supports .s lexing.
216     bool hasAsmLexer() const { return AsmLexerCtorFn != 0; }
217
218     /// hasAsmParser - Check if this target supports .s parsing.
219     bool hasAsmParser() const { return AsmParserCtorFn != 0; }
220
221     /// hasAsmPrinter - Check if this target supports .s printing.
222     bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
223
224     /// hasMCDisassembler - Check if this target has a disassembler.
225     bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
226
227     /// hasMCInstPrinter - Check if this target has an instruction printer.
228     bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
229
230     /// hasCodeEmitter - Check if this target supports instruction encoding.
231     bool hasCodeEmitter() const { return CodeEmitterCtorFn != 0; }
232
233     /// hasObjectStreamer - Check if this target supports streaming to files.
234     bool hasObjectStreamer() const { return ObjectStreamerCtorFn != 0; }
235
236     /// hasAsmStreamer - Check if this target supports streaming to files.
237     bool hasAsmStreamer() const { return AsmStreamerCtorFn != 0; }
238
239     /// @}
240     /// @name Feature Constructors
241     /// @{
242
243     /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
244     /// target triple.
245     ///
246     /// \arg Triple - This argument is used to determine the target machine
247     /// feature set; it should always be provided. Generally this should be
248     /// either the target triple from the module, or the target triple of the
249     /// host if that does not exist.
250     MCAsmInfo *createMCAsmInfo(StringRef Triple) const {
251       if (!MCAsmInfoCtorFn)
252         return 0;
253       return MCAsmInfoCtorFn(*this, Triple);
254     }
255
256     /// createMCInstrInfo - Create a MCInstrInfo implementation.
257     ///
258     MCInstrInfo *createMCInstrInfo() const {
259       if (!MCInstrInfoCtorFn)
260         return 0;
261       return MCInstrInfoCtorFn();
262     }
263
264     /// createMCRegInfo - Create a MCRegisterInfo implementation.
265     ///
266     MCRegisterInfo *createMCRegInfo() const {
267       if (!MCRegInfoCtorFn)
268         return 0;
269       return MCRegInfoCtorFn();
270     }
271
272     /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
273     ///
274     /// \arg 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     /// \arg CPU - This specifies the name of the target CPU.
279     /// \arg Features - This specifies the string representation of the
280     /// additional target features.
281     MCSubtargetInfo *createMCSubtargetInfo(StringRef Triple, StringRef CPU,
282                                            StringRef Features) const {
283       if (!MCSubtargetInfoCtorFn)
284         return 0;
285       return MCSubtargetInfoCtorFn(Triple, CPU, Features);
286     }
287
288     /// createTargetMachine - Create a target specific machine implementation
289     /// for the specified \arg Triple.
290     ///
291     /// \arg Triple - This argument is used to determine the target machine
292     /// feature set; it should always be provided. Generally this should be
293     /// either the target triple from the module, or the target triple of the
294     /// host if that does not exist.
295     TargetMachine *createTargetMachine(const std::string &Triple,
296                                        const std::string &CPU,
297                                        const std::string &Features) const {
298       if (!TargetMachineCtorFn)
299         return 0;
300       return TargetMachineCtorFn(*this, Triple, CPU, Features);
301     }
302
303     /// createAsmBackend - Create a target specific assembly parser.
304     ///
305     /// \arg Triple - The target triple string.
306     /// \arg Backend - The target independent assembler object.
307     TargetAsmBackend *createAsmBackend(const std::string &Triple) const {
308       if (!AsmBackendCtorFn)
309         return 0;
310       return AsmBackendCtorFn(*this, Triple);
311     }
312
313     /// createAsmLexer - Create a target specific assembly lexer.
314     ///
315     TargetAsmLexer *createAsmLexer(const MCAsmInfo &MAI) const {
316       if (!AsmLexerCtorFn)
317         return 0;
318       return AsmLexerCtorFn(*this, MAI);
319     }
320
321     /// createAsmParser - Create a target specific assembly parser.
322     ///
323     /// \arg Parser - The target independent parser implementation to use for
324     /// parsing and lexing.
325     TargetAsmParser *createAsmParser(MCSubtargetInfo &STI,
326                                      MCAsmParser &Parser) const {
327       if (!AsmParserCtorFn)
328         return 0;
329       return AsmParserCtorFn(STI, Parser);
330     }
331
332     /// createAsmPrinter - Create a target specific assembly printer pass.  This
333     /// takes ownership of the MCStreamer object.
334     AsmPrinter *createAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) const{
335       if (!AsmPrinterCtorFn)
336         return 0;
337       return AsmPrinterCtorFn(TM, Streamer);
338     }
339
340     MCDisassembler *createMCDisassembler() const {
341       if (!MCDisassemblerCtorFn)
342         return 0;
343       return MCDisassemblerCtorFn(*this);
344     }
345
346     MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
347                                        const MCAsmInfo &MAI) const {
348       if (!MCInstPrinterCtorFn)
349         return 0;
350       return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI);
351     }
352
353
354     /// createCodeEmitter - Create a target specific code emitter.
355     MCCodeEmitter *createCodeEmitter(const MCInstrInfo &II,
356                                      const MCSubtargetInfo &STI,
357                                      MCContext &Ctx) const {
358       if (!CodeEmitterCtorFn)
359         return 0;
360       return CodeEmitterCtorFn(II, STI, Ctx);
361     }
362
363     /// createObjectStreamer - Create a target specific MCStreamer.
364     ///
365     /// \arg TT - The target triple.
366     /// \arg Ctx - The target context.
367     /// \arg TAB - The target assembler backend object. Takes ownership.
368     /// \arg _OS - The stream object.
369     /// \arg _Emitter - The target independent assembler object.Takes ownership.
370     /// \arg RelaxAll - Relax all fixups?
371     /// \arg NoExecStack - Mark file as not needing a executable stack.
372     MCStreamer *createObjectStreamer(const std::string &TT, MCContext &Ctx,
373                                      TargetAsmBackend &TAB,
374                                      raw_ostream &_OS,
375                                      MCCodeEmitter *_Emitter,
376                                      bool RelaxAll,
377                                      bool NoExecStack) const {
378       if (!ObjectStreamerCtorFn)
379         return 0;
380       return ObjectStreamerCtorFn(*this, TT, Ctx, TAB, _OS, _Emitter, RelaxAll,
381                                   NoExecStack);
382     }
383
384     /// createAsmStreamer - Create a target specific MCStreamer.
385     MCStreamer *createAsmStreamer(MCContext &Ctx,
386                                   formatted_raw_ostream &OS,
387                                   bool isVerboseAsm,
388                                   bool useLoc,
389                                   bool useCFI,
390                                   MCInstPrinter *InstPrint,
391                                   MCCodeEmitter *CE,
392                                   TargetAsmBackend *TAB,
393                                   bool ShowInst) const {
394       // AsmStreamerCtorFn is default to llvm::createAsmStreamer
395       return AsmStreamerCtorFn(Ctx, OS, isVerboseAsm, useLoc, useCFI,
396                                InstPrint, CE, TAB, ShowInst);
397     }
398
399     /// @}
400   };
401
402   /// TargetRegistry - Generic interface to target specific features.
403   struct TargetRegistry {
404     class iterator {
405       const Target *Current;
406       explicit iterator(Target *T) : Current(T) {}
407       friend struct TargetRegistry;
408     public:
409       iterator(const iterator &I) : Current(I.Current) {}
410       iterator() : Current(0) {}
411
412       bool operator==(const iterator &x) const {
413         return Current == x.Current;
414       }
415       bool operator!=(const iterator &x) const {
416         return !operator==(x);
417       }
418
419       // Iterator traversal: forward iteration only
420       iterator &operator++() {          // Preincrement
421         assert(Current && "Cannot increment end iterator!");
422         Current = Current->getNext();
423         return *this;
424       }
425       iterator operator++(int) {        // Postincrement
426         iterator tmp = *this;
427         ++*this;
428         return tmp;
429       }
430
431       const Target &operator*() const {
432         assert(Current && "Cannot dereference end iterator!");
433         return *Current;
434       }
435
436       const Target *operator->() const {
437         return &operator*();
438       }
439     };
440
441     /// @name Registry Access
442     /// @{
443
444     static iterator begin();
445
446     static iterator end() { return iterator(); }
447
448     /// lookupTarget - Lookup a target based on a target triple.
449     ///
450     /// \param Triple - The triple to use for finding a target.
451     /// \param Error - On failure, an error string describing why no target was
452     /// found.
453     static const Target *lookupTarget(const std::string &Triple,
454                                       std::string &Error);
455
456     /// getClosestTargetForJIT - Pick the best target that is compatible with
457     /// the current host.  If no close target can be found, this returns null
458     /// and sets the Error string to a reason.
459     ///
460     /// Maintained for compatibility through 2.6.
461     static const Target *getClosestTargetForJIT(std::string &Error);
462
463     /// @}
464     /// @name Target Registration
465     /// @{
466
467     /// RegisterTarget - Register the given target. Attempts to register a
468     /// target which has already been registered will be ignored.
469     ///
470     /// Clients are responsible for ensuring that registration doesn't occur
471     /// while another thread is attempting to access the registry. Typically
472     /// this is done by initializing all targets at program startup.
473     ///
474     /// @param T - The target being registered.
475     /// @param Name - The target name. This should be a static string.
476     /// @param ShortDesc - A short target description. This should be a static
477     /// string.
478     /// @param TQualityFn - The triple match quality computation function for
479     /// this target.
480     /// @param HasJIT - Whether the target supports JIT code
481     /// generation.
482     static void RegisterTarget(Target &T,
483                                const char *Name,
484                                const char *ShortDesc,
485                                Target::TripleMatchQualityFnTy TQualityFn,
486                                bool HasJIT = false);
487
488     /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
489     /// given target.
490     ///
491     /// Clients are responsible for ensuring that registration doesn't occur
492     /// while another thread is attempting to access the registry. Typically
493     /// this is done by initializing all targets at program startup.
494     ///
495     /// @param T - The target being registered.
496     /// @param Fn - A function to construct a MCAsmInfo for the target.
497     static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
498       // Ignore duplicate registration.
499       if (!T.MCAsmInfoCtorFn)
500         T.MCAsmInfoCtorFn = Fn;
501     }
502
503     /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
504     /// given target.
505     ///
506     /// Clients are responsible for ensuring that registration doesn't occur
507     /// while another thread is attempting to access the registry. Typically
508     /// this is done by initializing all targets at program startup.
509     ///
510     /// @param T - The target being registered.
511     /// @param Fn - A function to construct a MCInstrInfo for the target.
512     static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
513       // Ignore duplicate registration.
514       if (!T.MCInstrInfoCtorFn)
515         T.MCInstrInfoCtorFn = Fn;
516     }
517
518     /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
519     /// given target.
520     ///
521     /// Clients are responsible for ensuring that registration doesn't occur
522     /// while another thread is attempting to access the registry. Typically
523     /// this is done by initializing all targets at program startup.
524     ///
525     /// @param T - The target being registered.
526     /// @param Fn - A function to construct a MCRegisterInfo for the target.
527     static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
528       // Ignore duplicate registration.
529       if (!T.MCRegInfoCtorFn)
530         T.MCRegInfoCtorFn = Fn;
531     }
532
533     /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
534     /// the given target.
535     ///
536     /// Clients are responsible for ensuring that registration doesn't occur
537     /// while another thread is attempting to access the registry. Typically
538     /// this is done by initializing all targets at program startup.
539     ///
540     /// @param T - The target being registered.
541     /// @param Fn - A function to construct a MCSubtargetInfo for the target.
542     static void RegisterMCSubtargetInfo(Target &T,
543                                         Target::MCSubtargetInfoCtorFnTy Fn) {
544       // Ignore duplicate registration.
545       if (!T.MCSubtargetInfoCtorFn)
546         T.MCSubtargetInfoCtorFn = Fn;
547     }
548
549     /// RegisterTargetMachine - Register a TargetMachine implementation for the
550     /// given target.
551     ///
552     /// Clients are responsible for ensuring that registration doesn't occur
553     /// while another thread is attempting to access the registry. Typically
554     /// this is done by initializing all targets at program startup.
555     ///
556     /// @param T - The target being registered.
557     /// @param Fn - A function to construct a TargetMachine for the target.
558     static void RegisterTargetMachine(Target &T,
559                                       Target::TargetMachineCtorTy Fn) {
560       // Ignore duplicate registration.
561       if (!T.TargetMachineCtorFn)
562         T.TargetMachineCtorFn = Fn;
563     }
564
565     /// RegisterAsmBackend - Register a TargetAsmBackend implementation for the
566     /// given target.
567     ///
568     /// Clients are responsible for ensuring that registration doesn't occur
569     /// while another thread is attempting to access the registry. Typically
570     /// this is done by initializing all targets at program startup.
571     ///
572     /// @param T - The target being registered.
573     /// @param Fn - A function to construct an AsmBackend for the target.
574     static void RegisterAsmBackend(Target &T, Target::AsmBackendCtorTy Fn) {
575       if (!T.AsmBackendCtorFn)
576         T.AsmBackendCtorFn = Fn;
577     }
578
579     /// RegisterAsmLexer - Register a TargetAsmLexer implementation for the
580     /// given target.
581     ///
582     /// Clients are responsible for ensuring that registration doesn't occur
583     /// while another thread is attempting to access the registry. Typically
584     /// this is done by initializing all targets at program startup.
585     ///
586     /// @param T - The target being registered.
587     /// @param Fn - A function to construct an AsmLexer for the target.
588     static void RegisterAsmLexer(Target &T, Target::AsmLexerCtorTy Fn) {
589       if (!T.AsmLexerCtorFn)
590         T.AsmLexerCtorFn = Fn;
591     }
592
593     /// RegisterAsmParser - Register a TargetAsmParser implementation for the
594     /// given target.
595     ///
596     /// Clients are responsible for ensuring that registration doesn't occur
597     /// while another thread is attempting to access the registry. Typically
598     /// this is done by initializing all targets at program startup.
599     ///
600     /// @param T - The target being registered.
601     /// @param Fn - A function to construct an AsmParser for the target.
602     static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
603       if (!T.AsmParserCtorFn)
604         T.AsmParserCtorFn = Fn;
605     }
606
607     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
608     /// target.
609     ///
610     /// Clients are responsible for ensuring that registration doesn't occur
611     /// while another thread is attempting to access the registry. Typically
612     /// this is done by initializing all targets at program startup.
613     ///
614     /// @param T - The target being registered.
615     /// @param Fn - A function to construct an AsmPrinter for the target.
616     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
617       // Ignore duplicate registration.
618       if (!T.AsmPrinterCtorFn)
619         T.AsmPrinterCtorFn = Fn;
620     }
621
622     /// RegisterMCDisassembler - Register a MCDisassembler implementation for
623     /// the given target.
624     ///
625     /// Clients are responsible for ensuring that registration doesn't occur
626     /// while another thread is attempting to access the registry. Typically
627     /// this is done by initializing all targets at program startup.
628     ///
629     /// @param T - The target being registered.
630     /// @param Fn - A function to construct an MCDisassembler for the target.
631     static void RegisterMCDisassembler(Target &T,
632                                        Target::MCDisassemblerCtorTy Fn) {
633       if (!T.MCDisassemblerCtorFn)
634         T.MCDisassemblerCtorFn = Fn;
635     }
636
637     /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
638     /// given target.
639     ///
640     /// Clients are responsible for ensuring that registration doesn't occur
641     /// while another thread is attempting to access the registry. Typically
642     /// this is done by initializing all targets at program startup.
643     ///
644     /// @param T - The target being registered.
645     /// @param Fn - A function to construct an MCInstPrinter for the target.
646     static void RegisterMCInstPrinter(Target &T,
647                                       Target::MCInstPrinterCtorTy Fn) {
648       if (!T.MCInstPrinterCtorFn)
649         T.MCInstPrinterCtorFn = Fn;
650     }
651
652     /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
653     /// given target.
654     ///
655     /// Clients are responsible for ensuring that registration doesn't occur
656     /// while another thread is attempting to access the registry. Typically
657     /// this is done by initializing all targets at program startup.
658     ///
659     /// @param T - The target being registered.
660     /// @param Fn - A function to construct an MCCodeEmitter for the target.
661     static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
662       if (!T.CodeEmitterCtorFn)
663         T.CodeEmitterCtorFn = Fn;
664     }
665
666     /// RegisterObjectStreamer - Register a object code MCStreamer implementation
667     /// for the given target.
668     ///
669     /// Clients are responsible for ensuring that registration doesn't occur
670     /// while another thread is attempting to access the registry. Typically
671     /// this is done by initializing all targets at program startup.
672     ///
673     /// @param T - The target being registered.
674     /// @param Fn - A function to construct an MCStreamer for the target.
675     static void RegisterObjectStreamer(Target &T, Target::ObjectStreamerCtorTy Fn) {
676       if (!T.ObjectStreamerCtorFn)
677         T.ObjectStreamerCtorFn = Fn;
678     }
679
680     /// RegisterAsmStreamer - Register an assembly MCStreamer implementation
681     /// for the given target.
682     ///
683     /// Clients are responsible for ensuring that registration doesn't occur
684     /// while another thread is attempting to access the registry. Typically
685     /// this is done by initializing all targets at program startup.
686     ///
687     /// @param T - The target being registered.
688     /// @param Fn - A function to construct an MCStreamer for the target.
689     static void RegisterAsmStreamer(Target &T, Target::AsmStreamerCtorTy Fn) {
690       if (T.AsmStreamerCtorFn == createAsmStreamer)
691         T.AsmStreamerCtorFn = Fn;
692     }
693
694     /// @}
695   };
696
697
698   //===--------------------------------------------------------------------===//
699
700   /// RegisterTarget - Helper template for registering a target, for use in the
701   /// target's initialization function. Usage:
702   ///
703   ///
704   /// Target TheFooTarget; // The global target instance.
705   ///
706   /// extern "C" void LLVMInitializeFooTargetInfo() {
707   ///   RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
708   /// }
709   template<Triple::ArchType TargetArchType = Triple::InvalidArch,
710            bool HasJIT = false>
711   struct RegisterTarget {
712     RegisterTarget(Target &T, const char *Name, const char *Desc) {
713       TargetRegistry::RegisterTarget(T, Name, Desc,
714                                      &getTripleMatchQuality,
715                                      HasJIT);
716     }
717
718     static unsigned getTripleMatchQuality(const std::string &TT) {
719       if (Triple(TT).getArch() == TargetArchType)
720         return 20;
721       return 0;
722     }
723   };
724
725   /// RegisterMCAsmInfo - Helper template for registering a target assembly info
726   /// implementation.  This invokes the static "Create" method on the class to
727   /// actually do the construction.  Usage:
728   ///
729   /// extern "C" void LLVMInitializeFooTarget() {
730   ///   extern Target TheFooTarget;
731   ///   RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
732   /// }
733   template<class MCAsmInfoImpl>
734   struct RegisterMCAsmInfo {
735     RegisterMCAsmInfo(Target &T) {
736       TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
737     }
738   private:
739     static MCAsmInfo *Allocator(const Target &T, StringRef TT) {
740       return new MCAsmInfoImpl(T, TT);
741     }
742
743   };
744
745   /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
746   /// implementation.  This invokes the specified function to do the
747   /// construction.  Usage:
748   ///
749   /// extern "C" void LLVMInitializeFooTarget() {
750   ///   extern Target TheFooTarget;
751   ///   RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
752   /// }
753   struct RegisterMCAsmInfoFn {
754     RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
755       TargetRegistry::RegisterMCAsmInfo(T, Fn);
756     }
757   };
758
759   /// RegisterMCInstrInfo - Helper template for registering a target instruction
760   /// info implementation.  This invokes the static "Create" method on the class
761   /// to actually do the construction.  Usage:
762   ///
763   /// extern "C" void LLVMInitializeFooTarget() {
764   ///   extern Target TheFooTarget;
765   ///   RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
766   /// }
767   template<class MCInstrInfoImpl>
768   struct RegisterMCInstrInfo {
769     RegisterMCInstrInfo(Target &T) {
770       TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
771     }
772   private:
773     static MCInstrInfo *Allocator() {
774       return new MCInstrInfoImpl();
775     }
776   };
777
778   /// RegisterMCInstrInfoFn - Helper template for registering a target
779   /// instruction info implementation.  This invokes the specified function to
780   /// do the construction.  Usage:
781   ///
782   /// extern "C" void LLVMInitializeFooTarget() {
783   ///   extern Target TheFooTarget;
784   ///   RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
785   /// }
786   struct RegisterMCInstrInfoFn {
787     RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
788       TargetRegistry::RegisterMCInstrInfo(T, Fn);
789     }
790   };
791
792   /// RegisterMCRegInfo - Helper template for registering a target register info
793   /// implementation.  This invokes the static "Create" method on the class to
794   /// actually do the construction.  Usage:
795   ///
796   /// extern "C" void LLVMInitializeFooTarget() {
797   ///   extern Target TheFooTarget;
798   ///   RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
799   /// }
800   template<class MCRegisterInfoImpl>
801   struct RegisterMCRegInfo {
802     RegisterMCRegInfo(Target &T) {
803       TargetRegistry::RegisterMCRegInfo(T, &Allocator);
804     }
805   private:
806     static MCRegisterInfo *Allocator() {
807       return new MCRegisterInfoImpl();
808     }
809   };
810
811   /// RegisterMCRegInfoFn - Helper template for registering a target register
812   /// info implementation.  This invokes the specified function to do the
813   /// construction.  Usage:
814   ///
815   /// extern "C" void LLVMInitializeFooTarget() {
816   ///   extern Target TheFooTarget;
817   ///   RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
818   /// }
819   struct RegisterMCRegInfoFn {
820     RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
821       TargetRegistry::RegisterMCRegInfo(T, Fn);
822     }
823   };
824
825   /// RegisterMCSubtargetInfo - Helper template for registering a target
826   /// subtarget info implementation.  This invokes the static "Create" method
827   /// on the class to actually do the construction.  Usage:
828   ///
829   /// extern "C" void LLVMInitializeFooTarget() {
830   ///   extern Target TheFooTarget;
831   ///   RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
832   /// }
833   template<class MCSubtargetInfoImpl>
834   struct RegisterMCSubtargetInfo {
835     RegisterMCSubtargetInfo(Target &T) {
836       TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
837     }
838   private:
839     static MCSubtargetInfo *Allocator(StringRef TT, StringRef CPU,
840                                       StringRef FS) {
841       return new MCSubtargetInfoImpl();
842     }
843   };
844
845   /// RegisterMCSubtargetInfoFn - Helper template for registering a target
846   /// subtarget info implementation.  This invokes the specified function to
847   /// do the construction.  Usage:
848   ///
849   /// extern "C" void LLVMInitializeFooTarget() {
850   ///   extern Target TheFooTarget;
851   ///   RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
852   /// }
853   struct RegisterMCSubtargetInfoFn {
854     RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
855       TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
856     }
857   };
858
859   /// RegisterTargetMachine - Helper template for registering a target machine
860   /// implementation, for use in the target machine initialization
861   /// function. Usage:
862   ///
863   /// extern "C" void LLVMInitializeFooTarget() {
864   ///   extern Target TheFooTarget;
865   ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
866   /// }
867   template<class TargetMachineImpl>
868   struct RegisterTargetMachine {
869     RegisterTargetMachine(Target &T) {
870       TargetRegistry::RegisterTargetMachine(T, &Allocator);
871     }
872
873   private:
874     static TargetMachine *Allocator(const Target &T, const std::string &TT,
875                                     const std::string &CPU,
876                                     const std::string &FS) {
877       return new TargetMachineImpl(T, TT, CPU, FS);
878     }
879   };
880
881   /// RegisterAsmBackend - Helper template for registering a target specific
882   /// assembler backend. Usage:
883   ///
884   /// extern "C" void LLVMInitializeFooAsmBackend() {
885   ///   extern Target TheFooTarget;
886   ///   RegisterAsmBackend<FooAsmLexer> X(TheFooTarget);
887   /// }
888   template<class AsmBackendImpl>
889   struct RegisterAsmBackend {
890     RegisterAsmBackend(Target &T) {
891       TargetRegistry::RegisterAsmBackend(T, &Allocator);
892     }
893
894   private:
895     static TargetAsmBackend *Allocator(const Target &T,
896                                        const std::string &Triple) {
897       return new AsmBackendImpl(T, Triple);
898     }
899   };
900
901   /// RegisterAsmLexer - Helper template for registering a target specific
902   /// assembly lexer, for use in the target machine initialization
903   /// function. Usage:
904   ///
905   /// extern "C" void LLVMInitializeFooAsmLexer() {
906   ///   extern Target TheFooTarget;
907   ///   RegisterAsmLexer<FooAsmLexer> X(TheFooTarget);
908   /// }
909   template<class AsmLexerImpl>
910   struct RegisterAsmLexer {
911     RegisterAsmLexer(Target &T) {
912       TargetRegistry::RegisterAsmLexer(T, &Allocator);
913     }
914
915   private:
916     static TargetAsmLexer *Allocator(const Target &T, const MCAsmInfo &MAI) {
917       return new AsmLexerImpl(T, MAI);
918     }
919   };
920
921   /// RegisterAsmParser - Helper template for registering a target specific
922   /// assembly parser, for use in the target machine initialization
923   /// function. Usage:
924   ///
925   /// extern "C" void LLVMInitializeFooAsmParser() {
926   ///   extern Target TheFooTarget;
927   ///   RegisterAsmParser<FooAsmParser> X(TheFooTarget);
928   /// }
929   template<class AsmParserImpl>
930   struct RegisterAsmParser {
931     RegisterAsmParser(Target &T) {
932       TargetRegistry::RegisterAsmParser(T, &Allocator);
933     }
934
935   private:
936     static TargetAsmParser *Allocator(MCSubtargetInfo &STI, MCAsmParser &P) {
937       return new AsmParserImpl(STI, P);
938     }
939   };
940
941   /// RegisterAsmPrinter - Helper template for registering a target specific
942   /// assembly printer, for use in the target machine initialization
943   /// function. Usage:
944   ///
945   /// extern "C" void LLVMInitializeFooAsmPrinter() {
946   ///   extern Target TheFooTarget;
947   ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
948   /// }
949   template<class AsmPrinterImpl>
950   struct RegisterAsmPrinter {
951     RegisterAsmPrinter(Target &T) {
952       TargetRegistry::RegisterAsmPrinter(T, &Allocator);
953     }
954
955   private:
956     static AsmPrinter *Allocator(TargetMachine &TM, MCStreamer &Streamer) {
957       return new AsmPrinterImpl(TM, Streamer);
958     }
959   };
960
961   /// RegisterCodeEmitter - Helper template for registering a target specific
962   /// machine code emitter, for use in the target initialization
963   /// function. Usage:
964   ///
965   /// extern "C" void LLVMInitializeFooCodeEmitter() {
966   ///   extern Target TheFooTarget;
967   ///   RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
968   /// }
969   template<class CodeEmitterImpl>
970   struct RegisterCodeEmitter {
971     RegisterCodeEmitter(Target &T) {
972       TargetRegistry::RegisterCodeEmitter(T, &Allocator);
973     }
974
975   private:
976     static MCCodeEmitter *Allocator(const MCInstrInfo &II,
977                                     const MCSubtargetInfo &STI,
978                                     MCContext &Ctx) {
979       return new CodeEmitterImpl();
980     }
981   };
982
983 }
984
985 #endif