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