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