OSDN Git Service

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