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