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