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