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