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