- Move CodeModel from a TargetMachine global option to MCCodeGenInfo.
[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     /// @name Registry Access
463     /// @{
464
465     static iterator begin();
466
467     static iterator end() { return iterator(); }
468
469     /// lookupTarget - Lookup a target based on a target triple.
470     ///
471     /// \param Triple - The triple to use for finding a target.
472     /// \param Error - On failure, an error string describing why no target was
473     /// found.
474     static const Target *lookupTarget(const std::string &Triple,
475                                       std::string &Error);
476
477     /// getClosestTargetForJIT - Pick the best target that is compatible with
478     /// the current host.  If no close target can be found, this returns null
479     /// and sets the Error string to a reason.
480     ///
481     /// Maintained for compatibility through 2.6.
482     static const Target *getClosestTargetForJIT(std::string &Error);
483
484     /// @}
485     /// @name Target Registration
486     /// @{
487
488     /// RegisterTarget - Register the given target. Attempts to register a
489     /// target which has already been registered will be ignored.
490     ///
491     /// Clients are responsible for ensuring that registration doesn't occur
492     /// while another thread is attempting to access the registry. Typically
493     /// this is done by initializing all targets at program startup.
494     ///
495     /// @param T - The target being registered.
496     /// @param Name - The target name. This should be a static string.
497     /// @param ShortDesc - A short target description. This should be a static
498     /// string.
499     /// @param TQualityFn - The triple match quality computation function for
500     /// this target.
501     /// @param HasJIT - Whether the target supports JIT code
502     /// generation.
503     static void RegisterTarget(Target &T,
504                                const char *Name,
505                                const char *ShortDesc,
506                                Target::TripleMatchQualityFnTy TQualityFn,
507                                bool HasJIT = false);
508
509     /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
510     /// given target.
511     ///
512     /// Clients are responsible for ensuring that registration doesn't occur
513     /// while another thread is attempting to access the registry. Typically
514     /// this is done by initializing all targets at program startup.
515     ///
516     /// @param T - The target being registered.
517     /// @param Fn - A function to construct a MCAsmInfo for the target.
518     static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
519       // Ignore duplicate registration.
520       if (!T.MCAsmInfoCtorFn)
521         T.MCAsmInfoCtorFn = Fn;
522     }
523
524     /// RegisterMCCodeGenInfo - Register a MCCodeGenInfo implementation for the
525     /// given target.
526     ///
527     /// Clients are responsible for ensuring that registration doesn't occur
528     /// while another thread is attempting to access the registry. Typically
529     /// this is done by initializing all targets at program startup.
530     ///
531     /// @param T - The target being registered.
532     /// @param Fn - A function to construct a MCCodeGenInfo for the target.
533     static void RegisterMCCodeGenInfo(Target &T,
534                                      Target::MCCodeGenInfoCtorFnTy Fn) {
535       // Ignore duplicate registration.
536       if (!T.MCCodeGenInfoCtorFn)
537         T.MCCodeGenInfoCtorFn = Fn;
538     }
539
540     /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
541     /// given target.
542     ///
543     /// Clients are responsible for ensuring that registration doesn't occur
544     /// while another thread is attempting to access the registry. Typically
545     /// this is done by initializing all targets at program startup.
546     ///
547     /// @param T - The target being registered.
548     /// @param Fn - A function to construct a MCInstrInfo for the target.
549     static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
550       // Ignore duplicate registration.
551       if (!T.MCInstrInfoCtorFn)
552         T.MCInstrInfoCtorFn = Fn;
553     }
554
555     /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
556     /// given target.
557     ///
558     /// Clients are responsible for ensuring that registration doesn't occur
559     /// while another thread is attempting to access the registry. Typically
560     /// this is done by initializing all targets at program startup.
561     ///
562     /// @param T - The target being registered.
563     /// @param Fn - A function to construct a MCRegisterInfo for the target.
564     static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
565       // Ignore duplicate registration.
566       if (!T.MCRegInfoCtorFn)
567         T.MCRegInfoCtorFn = Fn;
568     }
569
570     /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
571     /// the given target.
572     ///
573     /// Clients are responsible for ensuring that registration doesn't occur
574     /// while another thread is attempting to access the registry. Typically
575     /// this is done by initializing all targets at program startup.
576     ///
577     /// @param T - The target being registered.
578     /// @param Fn - A function to construct a MCSubtargetInfo for the target.
579     static void RegisterMCSubtargetInfo(Target &T,
580                                         Target::MCSubtargetInfoCtorFnTy Fn) {
581       // Ignore duplicate registration.
582       if (!T.MCSubtargetInfoCtorFn)
583         T.MCSubtargetInfoCtorFn = Fn;
584     }
585
586     /// RegisterTargetMachine - Register a TargetMachine implementation for the
587     /// given target.
588     ///
589     /// Clients are responsible for ensuring that registration doesn't occur
590     /// while another thread is attempting to access the registry. Typically
591     /// this is done by initializing all targets at program startup.
592     ///
593     /// @param T - The target being registered.
594     /// @param Fn - A function to construct a TargetMachine for the target.
595     static void RegisterTargetMachine(Target &T,
596                                       Target::TargetMachineCtorTy Fn) {
597       // Ignore duplicate registration.
598       if (!T.TargetMachineCtorFn)
599         T.TargetMachineCtorFn = Fn;
600     }
601
602     /// RegisterAsmBackend - Register a TargetAsmBackend implementation for the
603     /// given target.
604     ///
605     /// Clients are responsible for ensuring that registration doesn't occur
606     /// while another thread is attempting to access the registry. Typically
607     /// this is done by initializing all targets at program startup.
608     ///
609     /// @param T - The target being registered.
610     /// @param Fn - A function to construct an AsmBackend for the target.
611     static void RegisterAsmBackend(Target &T, Target::AsmBackendCtorTy Fn) {
612       if (!T.AsmBackendCtorFn)
613         T.AsmBackendCtorFn = Fn;
614     }
615
616     /// RegisterAsmLexer - Register a TargetAsmLexer implementation for the
617     /// given target.
618     ///
619     /// Clients are responsible for ensuring that registration doesn't occur
620     /// while another thread is attempting to access the registry. Typically
621     /// this is done by initializing all targets at program startup.
622     ///
623     /// @param T - The target being registered.
624     /// @param Fn - A function to construct an AsmLexer for the target.
625     static void RegisterAsmLexer(Target &T, Target::AsmLexerCtorTy Fn) {
626       if (!T.AsmLexerCtorFn)
627         T.AsmLexerCtorFn = Fn;
628     }
629
630     /// RegisterAsmParser - Register a TargetAsmParser implementation for the
631     /// given target.
632     ///
633     /// Clients are responsible for ensuring that registration doesn't occur
634     /// while another thread is attempting to access the registry. Typically
635     /// this is done by initializing all targets at program startup.
636     ///
637     /// @param T - The target being registered.
638     /// @param Fn - A function to construct an AsmParser for the target.
639     static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
640       if (!T.AsmParserCtorFn)
641         T.AsmParserCtorFn = Fn;
642     }
643
644     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
645     /// target.
646     ///
647     /// Clients are responsible for ensuring that registration doesn't occur
648     /// while another thread is attempting to access the registry. Typically
649     /// this is done by initializing all targets at program startup.
650     ///
651     /// @param T - The target being registered.
652     /// @param Fn - A function to construct an AsmPrinter for the target.
653     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
654       // Ignore duplicate registration.
655       if (!T.AsmPrinterCtorFn)
656         T.AsmPrinterCtorFn = Fn;
657     }
658
659     /// RegisterMCDisassembler - Register a MCDisassembler implementation for
660     /// the given target.
661     ///
662     /// Clients are responsible for ensuring that registration doesn't occur
663     /// while another thread is attempting to access the registry. Typically
664     /// this is done by initializing all targets at program startup.
665     ///
666     /// @param T - The target being registered.
667     /// @param Fn - A function to construct an MCDisassembler for the target.
668     static void RegisterMCDisassembler(Target &T,
669                                        Target::MCDisassemblerCtorTy Fn) {
670       if (!T.MCDisassemblerCtorFn)
671         T.MCDisassemblerCtorFn = Fn;
672     }
673
674     /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
675     /// given target.
676     ///
677     /// Clients are responsible for ensuring that registration doesn't occur
678     /// while another thread is attempting to access the registry. Typically
679     /// this is done by initializing all targets at program startup.
680     ///
681     /// @param T - The target being registered.
682     /// @param Fn - A function to construct an MCInstPrinter for the target.
683     static void RegisterMCInstPrinter(Target &T,
684                                       Target::MCInstPrinterCtorTy Fn) {
685       if (!T.MCInstPrinterCtorFn)
686         T.MCInstPrinterCtorFn = Fn;
687     }
688
689     /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
690     /// given target.
691     ///
692     /// Clients are responsible for ensuring that registration doesn't occur
693     /// while another thread is attempting to access the registry. Typically
694     /// this is done by initializing all targets at program startup.
695     ///
696     /// @param T - The target being registered.
697     /// @param Fn - A function to construct an MCCodeEmitter for the target.
698     static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
699       if (!T.CodeEmitterCtorFn)
700         T.CodeEmitterCtorFn = Fn;
701     }
702
703     /// RegisterObjectStreamer - Register a object code MCStreamer implementation
704     /// for the given target.
705     ///
706     /// Clients are responsible for ensuring that registration doesn't occur
707     /// while another thread is attempting to access the registry. Typically
708     /// this is done by initializing all targets at program startup.
709     ///
710     /// @param T - The target being registered.
711     /// @param Fn - A function to construct an MCStreamer for the target.
712     static void RegisterObjectStreamer(Target &T, Target::ObjectStreamerCtorTy Fn) {
713       if (!T.ObjectStreamerCtorFn)
714         T.ObjectStreamerCtorFn = Fn;
715     }
716
717     /// RegisterAsmStreamer - Register an assembly MCStreamer implementation
718     /// for the given target.
719     ///
720     /// Clients are responsible for ensuring that registration doesn't occur
721     /// while another thread is attempting to access the registry. Typically
722     /// this is done by initializing all targets at program startup.
723     ///
724     /// @param T - The target being registered.
725     /// @param Fn - A function to construct an MCStreamer for the target.
726     static void RegisterAsmStreamer(Target &T, Target::AsmStreamerCtorTy Fn) {
727       if (T.AsmStreamerCtorFn == createAsmStreamer)
728         T.AsmStreamerCtorFn = Fn;
729     }
730
731     /// @}
732   };
733
734
735   //===--------------------------------------------------------------------===//
736
737   /// RegisterTarget - Helper template for registering a target, for use in the
738   /// target's initialization function. Usage:
739   ///
740   ///
741   /// Target TheFooTarget; // The global target instance.
742   ///
743   /// extern "C" void LLVMInitializeFooTargetInfo() {
744   ///   RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
745   /// }
746   template<Triple::ArchType TargetArchType = Triple::InvalidArch,
747            bool HasJIT = false>
748   struct RegisterTarget {
749     RegisterTarget(Target &T, const char *Name, const char *Desc) {
750       TargetRegistry::RegisterTarget(T, Name, Desc,
751                                      &getTripleMatchQuality,
752                                      HasJIT);
753     }
754
755     static unsigned getTripleMatchQuality(const std::string &TT) {
756       if (Triple(TT).getArch() == TargetArchType)
757         return 20;
758       return 0;
759     }
760   };
761
762   /// RegisterMCAsmInfo - Helper template for registering a target assembly info
763   /// implementation.  This invokes the static "Create" method on the class to
764   /// actually do the construction.  Usage:
765   ///
766   /// extern "C" void LLVMInitializeFooTarget() {
767   ///   extern Target TheFooTarget;
768   ///   RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
769   /// }
770   template<class MCAsmInfoImpl>
771   struct RegisterMCAsmInfo {
772     RegisterMCAsmInfo(Target &T) {
773       TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
774     }
775   private:
776     static MCAsmInfo *Allocator(const Target &T, StringRef TT) {
777       return new MCAsmInfoImpl(T, TT);
778     }
779
780   };
781
782   /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
783   /// implementation.  This invokes the specified function to do the
784   /// construction.  Usage:
785   ///
786   /// extern "C" void LLVMInitializeFooTarget() {
787   ///   extern Target TheFooTarget;
788   ///   RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
789   /// }
790   struct RegisterMCAsmInfoFn {
791     RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
792       TargetRegistry::RegisterMCAsmInfo(T, Fn);
793     }
794   };
795
796   /// RegisterMCCodeGenInfo - Helper template for registering a target codegen info
797   /// implementation.  This invokes the static "Create" method on the class
798   /// to actually do the construction.  Usage:
799   ///
800   /// extern "C" void LLVMInitializeFooTarget() {
801   ///   extern Target TheFooTarget;
802   ///   RegisterMCCodeGenInfo<FooMCCodeGenInfo> X(TheFooTarget);
803   /// }
804   template<class MCCodeGenInfoImpl>
805   struct RegisterMCCodeGenInfo {
806     RegisterMCCodeGenInfo(Target &T) {
807       TargetRegistry::RegisterMCCodeGenInfo(T, &Allocator);
808     }
809   private:
810     static MCCodeGenInfo *Allocator(StringRef TT,
811                                     Reloc::Model RM, CodeModel::Model CM) {
812       return new MCCodeGenInfoImpl();
813     }
814   };
815
816   /// RegisterMCCodeGenInfoFn - Helper template for registering a target codegen
817   /// info implementation.  This invokes the specified function to do the
818   /// construction.  Usage:
819   ///
820   /// extern "C" void LLVMInitializeFooTarget() {
821   ///   extern Target TheFooTarget;
822   ///   RegisterMCCodeGenInfoFn X(TheFooTarget, TheFunction);
823   /// }
824   struct RegisterMCCodeGenInfoFn {
825     RegisterMCCodeGenInfoFn(Target &T, Target::MCCodeGenInfoCtorFnTy Fn) {
826       TargetRegistry::RegisterMCCodeGenInfo(T, Fn);
827     }
828   };
829
830   /// RegisterMCInstrInfo - Helper template for registering a target instruction
831   /// info implementation.  This invokes the static "Create" method on the class
832   /// to actually do the construction.  Usage:
833   ///
834   /// extern "C" void LLVMInitializeFooTarget() {
835   ///   extern Target TheFooTarget;
836   ///   RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
837   /// }
838   template<class MCInstrInfoImpl>
839   struct RegisterMCInstrInfo {
840     RegisterMCInstrInfo(Target &T) {
841       TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
842     }
843   private:
844     static MCInstrInfo *Allocator() {
845       return new MCInstrInfoImpl();
846     }
847   };
848
849   /// RegisterMCInstrInfoFn - Helper template for registering a target
850   /// instruction info implementation.  This invokes the specified function to
851   /// do the construction.  Usage:
852   ///
853   /// extern "C" void LLVMInitializeFooTarget() {
854   ///   extern Target TheFooTarget;
855   ///   RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
856   /// }
857   struct RegisterMCInstrInfoFn {
858     RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
859       TargetRegistry::RegisterMCInstrInfo(T, Fn);
860     }
861   };
862
863   /// RegisterMCRegInfo - Helper template for registering a target register info
864   /// implementation.  This invokes the static "Create" method on the class to
865   /// actually do the construction.  Usage:
866   ///
867   /// extern "C" void LLVMInitializeFooTarget() {
868   ///   extern Target TheFooTarget;
869   ///   RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
870   /// }
871   template<class MCRegisterInfoImpl>
872   struct RegisterMCRegInfo {
873     RegisterMCRegInfo(Target &T) {
874       TargetRegistry::RegisterMCRegInfo(T, &Allocator);
875     }
876   private:
877     static MCRegisterInfo *Allocator(StringRef TT) {
878       return new MCRegisterInfoImpl();
879     }
880   };
881
882   /// RegisterMCRegInfoFn - Helper template for registering a target register
883   /// info implementation.  This invokes the specified function to do the
884   /// construction.  Usage:
885   ///
886   /// extern "C" void LLVMInitializeFooTarget() {
887   ///   extern Target TheFooTarget;
888   ///   RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
889   /// }
890   struct RegisterMCRegInfoFn {
891     RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
892       TargetRegistry::RegisterMCRegInfo(T, Fn);
893     }
894   };
895
896   /// RegisterMCSubtargetInfo - Helper template for registering a target
897   /// subtarget info implementation.  This invokes the static "Create" method
898   /// on the class to actually do the construction.  Usage:
899   ///
900   /// extern "C" void LLVMInitializeFooTarget() {
901   ///   extern Target TheFooTarget;
902   ///   RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
903   /// }
904   template<class MCSubtargetInfoImpl>
905   struct RegisterMCSubtargetInfo {
906     RegisterMCSubtargetInfo(Target &T) {
907       TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
908     }
909   private:
910     static MCSubtargetInfo *Allocator(StringRef TT, StringRef CPU,
911                                       StringRef FS) {
912       return new MCSubtargetInfoImpl();
913     }
914   };
915
916   /// RegisterMCSubtargetInfoFn - Helper template for registering a target
917   /// subtarget info implementation.  This invokes the specified function to
918   /// do the construction.  Usage:
919   ///
920   /// extern "C" void LLVMInitializeFooTarget() {
921   ///   extern Target TheFooTarget;
922   ///   RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
923   /// }
924   struct RegisterMCSubtargetInfoFn {
925     RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
926       TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
927     }
928   };
929
930   /// RegisterTargetMachine - Helper template for registering a target machine
931   /// implementation, for use in the target machine initialization
932   /// function. Usage:
933   ///
934   /// extern "C" void LLVMInitializeFooTarget() {
935   ///   extern Target TheFooTarget;
936   ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
937   /// }
938   template<class TargetMachineImpl>
939   struct RegisterTargetMachine {
940     RegisterTargetMachine(Target &T) {
941       TargetRegistry::RegisterTargetMachine(T, &Allocator);
942     }
943
944   private:
945     static TargetMachine *Allocator(const Target &T, StringRef TT,
946                                     StringRef CPU, StringRef FS,
947                                     Reloc::Model RM,
948                                     CodeModel::Model CM) {
949       return new TargetMachineImpl(T, TT, CPU, FS, RM, CM);
950     }
951   };
952
953   /// RegisterAsmBackend - Helper template for registering a target specific
954   /// assembler backend. Usage:
955   ///
956   /// extern "C" void LLVMInitializeFooAsmBackend() {
957   ///   extern Target TheFooTarget;
958   ///   RegisterAsmBackend<FooAsmLexer> X(TheFooTarget);
959   /// }
960   template<class AsmBackendImpl>
961   struct RegisterAsmBackend {
962     RegisterAsmBackend(Target &T) {
963       TargetRegistry::RegisterAsmBackend(T, &Allocator);
964     }
965
966   private:
967     static TargetAsmBackend *Allocator(const Target &T,
968                                        const std::string &Triple) {
969       return new AsmBackendImpl(T, Triple);
970     }
971   };
972
973   /// RegisterAsmLexer - Helper template for registering a target specific
974   /// assembly lexer, for use in the target machine initialization
975   /// function. Usage:
976   ///
977   /// extern "C" void LLVMInitializeFooAsmLexer() {
978   ///   extern Target TheFooTarget;
979   ///   RegisterAsmLexer<FooAsmLexer> X(TheFooTarget);
980   /// }
981   template<class AsmLexerImpl>
982   struct RegisterAsmLexer {
983     RegisterAsmLexer(Target &T) {
984       TargetRegistry::RegisterAsmLexer(T, &Allocator);
985     }
986
987   private:
988     static TargetAsmLexer *Allocator(const Target &T, const MCAsmInfo &MAI) {
989       return new AsmLexerImpl(T, MAI);
990     }
991   };
992
993   /// RegisterAsmParser - Helper template for registering a target specific
994   /// assembly parser, for use in the target machine initialization
995   /// function. Usage:
996   ///
997   /// extern "C" void LLVMInitializeFooAsmParser() {
998   ///   extern Target TheFooTarget;
999   ///   RegisterAsmParser<FooAsmParser> X(TheFooTarget);
1000   /// }
1001   template<class AsmParserImpl>
1002   struct RegisterAsmParser {
1003     RegisterAsmParser(Target &T) {
1004       TargetRegistry::RegisterAsmParser(T, &Allocator);
1005     }
1006
1007   private:
1008     static TargetAsmParser *Allocator(MCSubtargetInfo &STI, MCAsmParser &P) {
1009       return new AsmParserImpl(STI, P);
1010     }
1011   };
1012
1013   /// RegisterAsmPrinter - Helper template for registering a target specific
1014   /// assembly printer, for use in the target machine initialization
1015   /// function. Usage:
1016   ///
1017   /// extern "C" void LLVMInitializeFooAsmPrinter() {
1018   ///   extern Target TheFooTarget;
1019   ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
1020   /// }
1021   template<class AsmPrinterImpl>
1022   struct RegisterAsmPrinter {
1023     RegisterAsmPrinter(Target &T) {
1024       TargetRegistry::RegisterAsmPrinter(T, &Allocator);
1025     }
1026
1027   private:
1028     static AsmPrinter *Allocator(TargetMachine &TM, MCStreamer &Streamer) {
1029       return new AsmPrinterImpl(TM, Streamer);
1030     }
1031   };
1032
1033   /// RegisterCodeEmitter - Helper template for registering a target specific
1034   /// machine code emitter, for use in the target initialization
1035   /// function. Usage:
1036   ///
1037   /// extern "C" void LLVMInitializeFooCodeEmitter() {
1038   ///   extern Target TheFooTarget;
1039   ///   RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
1040   /// }
1041   template<class CodeEmitterImpl>
1042   struct RegisterCodeEmitter {
1043     RegisterCodeEmitter(Target &T) {
1044       TargetRegistry::RegisterCodeEmitter(T, &Allocator);
1045     }
1046
1047   private:
1048     static MCCodeEmitter *Allocator(const MCInstrInfo &II,
1049                                     const MCSubtargetInfo &STI,
1050                                     MCContext &Ctx) {
1051       return new CodeEmitterImpl();
1052     }
1053   };
1054
1055 }
1056
1057 #endif