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