Remove the target hook TargetInstrInfo::BlockHasNoFallThrough in favor of
[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/ADT/Triple.h"
23 #include <string>
24 #include <cassert>
25
26 namespace llvm {
27   class AsmPrinter;
28   class MCAsmParser;
29   class MCCodeEmitter;
30   class Module;
31   class MCAsmInfo;
32   class MCDisassembler;
33   class MCInstPrinter;
34   class TargetAsmParser;
35   class TargetMachine;
36   class formatted_raw_ostream;
37   class raw_ostream;
38
39   /// Target - Wrapper for Target specific information.
40   ///
41   /// For registration purposes, this is a POD type so that targets can be
42   /// registered without the use of static constructors.
43   ///
44   /// Targets should implement a single global instance of this class (which
45   /// will be zero initialized), and pass that instance to the TargetRegistry as
46   /// part of their initialization.
47   class Target {
48   public:
49     friend struct TargetRegistry;
50
51     typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
52
53     typedef const MCAsmInfo *(*AsmInfoCtorFnTy)(const Target &T,
54                                                 StringRef TT);
55     typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
56                                                   const std::string &TT,
57                                                   const std::string &Features);
58     typedef AsmPrinter *(*AsmPrinterCtorTy)(formatted_raw_ostream &OS,
59                                             TargetMachine &TM,
60                                             const MCAsmInfo *MAI,
61                                             bool VerboseAsm);
62     typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &T,
63                                                 MCAsmParser &P);
64     typedef const MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T);
65     typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
66                                                   unsigned SyntaxVariant,
67                                                   const MCAsmInfo &MAI,
68                                                   raw_ostream &O);
69     typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const Target &T,
70                                                 TargetMachine &TM);
71
72   private:
73     /// Next - The next registered target in the linked list, maintained by the
74     /// TargetRegistry.
75     Target *Next;
76
77     /// TripleMatchQualityFn - The target function for rating the match quality
78     /// of a triple.
79     TripleMatchQualityFnTy TripleMatchQualityFn;
80
81     /// Name - The target name.
82     const char *Name;
83
84     /// ShortDesc - A short description of the target.
85     const char *ShortDesc;
86
87     /// HasJIT - Whether this target supports the JIT.
88     bool HasJIT;
89
90     AsmInfoCtorFnTy AsmInfoCtorFn;
91     
92     /// TargetMachineCtorFn - Construction function for this target's
93     /// TargetMachine, if registered.
94     TargetMachineCtorTy TargetMachineCtorFn;
95
96     /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
97     /// if registered.
98     AsmPrinterCtorTy AsmPrinterCtorFn;
99
100     /// AsmParserCtorFn - Construction function for this target's AsmParser,
101     /// if registered.
102     AsmParserCtorTy AsmParserCtorFn;
103     
104     /// MCDisassemblerCtorFn - Construction function for this target's
105     /// MCDisassembler, if registered.
106     MCDisassemblerCtorTy MCDisassemblerCtorFn;
107
108     
109     /// MCInstPrinterCtorFn - Construction function for this target's 
110     /// MCInstPrinter, if registered.
111     MCInstPrinterCtorTy MCInstPrinterCtorFn;
112     
113     /// CodeEmitterCtorFn - Construction function for this target's CodeEmitter,
114     /// if registered.
115     CodeEmitterCtorTy CodeEmitterCtorFn;
116
117   public:
118     /// @name Target Information
119     /// @{
120
121     // getNext - Return the next registered target.
122     const Target *getNext() const { return Next; }
123
124     /// getName - Get the target name.
125     const char *getName() const { return Name; }
126
127     /// getShortDescription - Get a short description of the target.
128     const char *getShortDescription() const { return ShortDesc; }
129
130     /// @}
131     /// @name Feature Predicates
132     /// @{
133
134     /// hasJIT - Check if this targets supports the just-in-time compilation.
135     bool hasJIT() const { return HasJIT; }
136
137     /// hasTargetMachine - Check if this target supports code generation.
138     bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
139
140     /// hasAsmPrinter - Check if this target supports .s printing.
141     bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
142
143     /// hasAsmParser - Check if this target supports .s parsing.
144     bool hasAsmParser() const { return AsmParserCtorFn != 0; }
145     
146     /// hasMCDisassembler - Check if this target has a disassembler.
147     bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
148
149     /// hasMCInstPrinter - Check if this target has an instruction printer.
150     bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
151
152     /// hasCodeEmitter - Check if this target supports instruction encoding.
153     bool hasCodeEmitter() const { return CodeEmitterCtorFn != 0; }
154
155     /// @}
156     /// @name Feature Constructors
157     /// @{
158     
159     /// createAsmInfo - Create a MCAsmInfo implementation for the specified
160     /// target triple.
161     ///
162     /// \arg Triple - This argument is used to determine the target machine
163     /// feature set; it should always be provided. Generally this should be
164     /// either the target triple from the module, or the target triple of the
165     /// host if that does not exist.
166     const MCAsmInfo *createAsmInfo(StringRef Triple) const {
167       if (!AsmInfoCtorFn)
168         return 0;
169       return AsmInfoCtorFn(*this, Triple);
170     }
171     
172     /// createTargetMachine - Create a target specific machine implementation
173     /// for the specified \arg Triple.
174     ///
175     /// \arg Triple - This argument is used to determine the target machine
176     /// feature set; it should always be provided. Generally this should be
177     /// either the target triple from the module, or the target triple of the
178     /// host if that does not exist.
179     TargetMachine *createTargetMachine(const std::string &Triple,
180                                        const std::string &Features) const {
181       if (!TargetMachineCtorFn)
182         return 0;
183       return TargetMachineCtorFn(*this, Triple, Features);
184     }
185
186     /// createAsmPrinter - Create a target specific assembly printer pass.
187     AsmPrinter *createAsmPrinter(formatted_raw_ostream &OS, TargetMachine &TM,
188                                  const MCAsmInfo *MAI, bool Verbose) const {
189       if (!AsmPrinterCtorFn)
190         return 0;
191       return AsmPrinterCtorFn(OS, TM, MAI, Verbose);
192     }
193
194     /// createAsmParser - Create a target specific assembly parser.
195     ///
196     /// \arg Parser - The target independent parser implementation to use for
197     /// parsing and lexing.
198     TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
199       if (!AsmParserCtorFn)
200         return 0;
201       return AsmParserCtorFn(*this, Parser);
202     }
203     
204     const MCDisassembler *createMCDisassembler() const {
205       if (!MCDisassemblerCtorFn)
206         return 0;
207       return MCDisassemblerCtorFn(*this);
208     }
209
210     MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
211                                        const MCAsmInfo &MAI,
212                                        raw_ostream &O) const {
213       if (!MCInstPrinterCtorFn)
214         return 0;
215       return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI, O);
216     }
217     
218     
219     /// createCodeEmitter - Create a target specific code emitter.
220     MCCodeEmitter *createCodeEmitter(TargetMachine &TM) const {
221       if (!CodeEmitterCtorFn)
222         return 0;
223       return CodeEmitterCtorFn(*this, TM);
224     }
225
226     /// @}
227   };
228
229   /// TargetRegistry - Generic interface to target specific features.
230   struct TargetRegistry {
231     class iterator {
232       const Target *Current;
233       explicit iterator(Target *T) : Current(T) {}
234       friend struct TargetRegistry;
235     public:
236       iterator(const iterator &I) : Current(I.Current) {}
237       iterator() : Current(0) {}
238
239       bool operator==(const iterator &x) const {
240         return Current == x.Current;
241       }
242       bool operator!=(const iterator &x) const {
243         return !operator==(x);
244       }
245
246       // Iterator traversal: forward iteration only
247       iterator &operator++() {          // Preincrement
248         assert(Current && "Cannot increment end iterator!");
249         Current = Current->getNext();
250         return *this;
251       }
252       iterator operator++(int) {        // Postincrement
253         iterator tmp = *this; 
254         ++*this; 
255         return tmp;
256       }
257
258       const Target &operator*() const {
259         assert(Current && "Cannot dereference end iterator!");
260         return *Current;
261       }
262
263       const Target *operator->() const {
264         return &operator*();
265       }
266     };
267
268     /// @name Registry Access
269     /// @{
270
271     static iterator begin();
272
273     static iterator end() { return iterator(); }
274
275     /// lookupTarget - Lookup a target based on a target triple.
276     ///
277     /// \param Triple - The triple to use for finding a target.
278     /// \param Error - On failure, an error string describing why no target was
279     /// found.
280     static const Target *lookupTarget(const std::string &Triple,
281                                       std::string &Error);
282
283     /// getClosestTargetForJIT - Pick the best target that is compatible with
284     /// the current host.  If no close target can be found, this returns null
285     /// and sets the Error string to a reason.
286     ///
287     /// Maintained for compatibility through 2.6.
288     static const Target *getClosestTargetForJIT(std::string &Error);
289
290     /// @}
291     /// @name Target Registration
292     /// @{
293
294     /// RegisterTarget - Register the given target. Attempts to register a
295     /// target which has already been registered will be ignored.
296     /// 
297     /// Clients are responsible for ensuring that registration doesn't occur
298     /// while another thread is attempting to access the registry. Typically
299     /// this is done by initializing all targets at program startup.
300     ///
301     /// @param T - The target being registered.
302     /// @param Name - The target name. This should be a static string.
303     /// @param ShortDesc - A short target description. This should be a static
304     /// string. 
305     /// @param TQualityFn - The triple match quality computation function for
306     /// this target.
307     /// @param HasJIT - Whether the target supports JIT code
308     /// generation.
309     static void RegisterTarget(Target &T,
310                                const char *Name,
311                                const char *ShortDesc,
312                                Target::TripleMatchQualityFnTy TQualityFn,
313                                bool HasJIT = false);
314
315     /// RegisterAsmInfo - Register a MCAsmInfo implementation for the
316     /// given target.
317     /// 
318     /// Clients are responsible for ensuring that registration doesn't occur
319     /// while another thread is attempting to access the registry. Typically
320     /// this is done by initializing all targets at program startup.
321     /// 
322     /// @param T - The target being registered.
323     /// @param Fn - A function to construct a MCAsmInfo for the target.
324     static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
325       // Ignore duplicate registration.
326       if (!T.AsmInfoCtorFn)
327         T.AsmInfoCtorFn = Fn;
328     }
329     
330     /// RegisterTargetMachine - Register a TargetMachine implementation for the
331     /// given target.
332     /// 
333     /// Clients are responsible for ensuring that registration doesn't occur
334     /// while another thread is attempting to access the registry. Typically
335     /// this is done by initializing all targets at program startup.
336     /// 
337     /// @param T - The target being registered.
338     /// @param Fn - A function to construct a TargetMachine for the target.
339     static void RegisterTargetMachine(Target &T, 
340                                       Target::TargetMachineCtorTy Fn) {
341       // Ignore duplicate registration.
342       if (!T.TargetMachineCtorFn)
343         T.TargetMachineCtorFn = Fn;
344     }
345
346     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
347     /// target.
348     /// 
349     /// Clients are responsible for ensuring that registration doesn't occur
350     /// while another thread is attempting to access the registry. Typically
351     /// this is done by initializing all targets at program startup.
352     ///
353     /// @param T - The target being registered.
354     /// @param Fn - A function to construct an AsmPrinter for the target.
355     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
356       // Ignore duplicate registration.
357       if (!T.AsmPrinterCtorFn)
358         T.AsmPrinterCtorFn = Fn;
359     }
360
361     /// RegisterAsmParser - Register a TargetAsmParser implementation for the
362     /// given target.
363     /// 
364     /// Clients are responsible for ensuring that registration doesn't occur
365     /// while another thread is attempting to access the registry. Typically
366     /// this is done by initializing all targets at program startup.
367     ///
368     /// @param T - The target being registered.
369     /// @param Fn - A function to construct an AsmPrinter for the target.
370     static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
371       if (!T.AsmParserCtorFn)
372         T.AsmParserCtorFn = Fn;
373     }
374     
375     /// RegisterMCDisassembler - Register a MCDisassembler implementation for
376     /// the given target.
377     /// 
378     /// Clients are responsible for ensuring that registration doesn't occur
379     /// while another thread is attempting to access the registry. Typically
380     /// this is done by initializing all targets at program startup.
381     ///
382     /// @param T - The target being registered.
383     /// @param Fn - A function to construct an MCDisassembler for the target.
384     static void RegisterMCDisassembler(Target &T, 
385                                        Target::MCDisassemblerCtorTy Fn) {
386       if (!T.MCDisassemblerCtorFn)
387         T.MCDisassemblerCtorFn = Fn;
388     }
389
390     /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
391     /// given target.
392     /// 
393     /// Clients are responsible for ensuring that registration doesn't occur
394     /// while another thread is attempting to access the registry. Typically
395     /// this is done by initializing all targets at program startup.
396     ///
397     /// @param T - The target being registered.
398     /// @param Fn - A function to construct an MCInstPrinter for the target.
399     static void RegisterMCInstPrinter(Target &T,
400                                       Target::MCInstPrinterCtorTy Fn) {
401       if (!T.MCInstPrinterCtorFn)
402         T.MCInstPrinterCtorFn = Fn;
403     }
404     
405     /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
406     /// given target.
407     ///
408     /// Clients are responsible for ensuring that registration doesn't occur
409     /// while another thread is attempting to access the registry. Typically
410     /// this is done by initializing all targets at program startup.
411     ///
412     /// @param T - The target being registered.
413     /// @param Fn - A function to construct an AsmPrinter for the target.
414     static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
415       if (!T.CodeEmitterCtorFn)
416         T.CodeEmitterCtorFn = Fn;
417     }
418
419     /// @}
420   };
421
422
423   //===--------------------------------------------------------------------===//
424
425   /// RegisterTarget - Helper template for registering a target, for use in the
426   /// target's initialization function. Usage:
427   ///
428   ///
429   /// Target TheFooTarget; // The global target instance.
430   ///
431   /// extern "C" void LLVMInitializeFooTargetInfo() {
432   ///   RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
433   /// }
434   template<Triple::ArchType TargetArchType = Triple::InvalidArch,
435            bool HasJIT = false>
436   struct RegisterTarget {
437     RegisterTarget(Target &T, const char *Name, const char *Desc) {
438       TargetRegistry::RegisterTarget(T, Name, Desc,
439                                      &getTripleMatchQuality,
440                                      HasJIT);
441     }
442
443     static unsigned getTripleMatchQuality(const std::string &TT) {
444       if (Triple(TT).getArch() == TargetArchType)
445         return 20;
446       return 0;
447     }
448   };
449
450   /// RegisterAsmInfo - Helper template for registering a target assembly info
451   /// implementation.  This invokes the static "Create" method on the class to
452   /// actually do the construction.  Usage:
453   ///
454   /// extern "C" void LLVMInitializeFooTarget() {
455   ///   extern Target TheFooTarget;
456   ///   RegisterAsmInfo<FooMCAsmInfo> X(TheFooTarget);
457   /// }
458   template<class MCAsmInfoImpl>
459   struct RegisterAsmInfo {
460     RegisterAsmInfo(Target &T) {
461       TargetRegistry::RegisterAsmInfo(T, &Allocator);
462     }
463   private:
464     static const MCAsmInfo *Allocator(const Target &T, StringRef TT) {
465       return new MCAsmInfoImpl(T, TT);
466     }
467     
468   };
469
470   /// RegisterAsmInfoFn - Helper template for registering a target assembly info
471   /// implementation.  This invokes the specified function to do the
472   /// construction.  Usage:
473   ///
474   /// extern "C" void LLVMInitializeFooTarget() {
475   ///   extern Target TheFooTarget;
476   ///   RegisterAsmInfoFn X(TheFooTarget, TheFunction);
477   /// }
478   struct RegisterAsmInfoFn {
479     RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
480       TargetRegistry::RegisterAsmInfo(T, Fn);
481     }
482   };
483
484
485   /// RegisterTargetMachine - Helper template for registering a target machine
486   /// implementation, for use in the target machine initialization
487   /// function. Usage:
488   ///
489   /// extern "C" void LLVMInitializeFooTarget() {
490   ///   extern Target TheFooTarget;
491   ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
492   /// }
493   template<class TargetMachineImpl>
494   struct RegisterTargetMachine {
495     RegisterTargetMachine(Target &T) {
496       TargetRegistry::RegisterTargetMachine(T, &Allocator);
497     }
498
499   private:
500     static TargetMachine *Allocator(const Target &T, const std::string &TT,
501                                     const std::string &FS) {
502       return new TargetMachineImpl(T, TT, FS);
503     }
504   };
505
506   /// RegisterAsmPrinter - Helper template for registering a target specific
507   /// assembly printer, for use in the target machine initialization
508   /// function. Usage:
509   ///
510   /// extern "C" void LLVMInitializeFooAsmPrinter() {
511   ///   extern Target TheFooTarget;
512   ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
513   /// }
514   template<class AsmPrinterImpl>
515   struct RegisterAsmPrinter {
516     RegisterAsmPrinter(Target &T) {
517       TargetRegistry::RegisterAsmPrinter(T, &Allocator);
518     }
519
520   private:
521     static AsmPrinter *Allocator(formatted_raw_ostream &OS, TargetMachine &TM,
522                                  const MCAsmInfo *MAI, bool Verbose) {
523       return new AsmPrinterImpl(OS, TM, MAI, Verbose);
524     }
525   };
526
527   /// RegisterAsmParser - Helper template for registering a target specific
528   /// assembly parser, for use in the target machine initialization
529   /// function. Usage:
530   ///
531   /// extern "C" void LLVMInitializeFooAsmParser() {
532   ///   extern Target TheFooTarget;
533   ///   RegisterAsmParser<FooAsmParser> X(TheFooTarget);
534   /// }
535   template<class AsmParserImpl>
536   struct RegisterAsmParser {
537     RegisterAsmParser(Target &T) {
538       TargetRegistry::RegisterAsmParser(T, &Allocator);
539     }
540
541   private:
542     static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
543       return new AsmParserImpl(T, P);
544     }
545   };
546
547   /// RegisterCodeEmitter - Helper template for registering a target specific
548   /// machine code emitter, for use in the target initialization
549   /// function. Usage:
550   ///
551   /// extern "C" void LLVMInitializeFooCodeEmitter() {
552   ///   extern Target TheFooTarget;
553   ///   RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
554   /// }
555   template<class CodeEmitterImpl>
556   struct RegisterCodeEmitter {
557     RegisterCodeEmitter(Target &T) {
558       TargetRegistry::RegisterCodeEmitter(T, &Allocator);
559     }
560
561   private:
562     static MCCodeEmitter *Allocator(const Target &T, TargetMachine &TM) {
563       return new CodeEmitterImpl(T, TM);
564     }
565   };
566
567 }
568
569 #endif