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