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