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