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