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