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