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