LLVM API Documentation

TargetRegistry.h

Go to the documentation of this file.
00001 //===-- Target/TargetRegistry.h - Target Registration -----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file exposes the TargetRegistry interface, which tools can use to access
00011 // the appropriate target specific classes (TargetMachine, AsmPrinter, etc.)
00012 // which have been registered.
00013 //
00014 // Target specific class implementations should register themselves using the
00015 // appropriate TargetRegistry interfaces.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_TARGET_TARGETREGISTRY_H
00020 #define LLVM_TARGET_TARGETREGISTRY_H
00021 
00022 #include "llvm/ADT/Triple.h"
00023 #include <string>
00024 #include <cassert>
00025 
00026 namespace llvm {
00027   class AsmPrinter;
00028   class MCAsmParser;
00029   class Module;
00030   class TargetAsmInfo;
00031   class TargetAsmParser;
00032   class TargetMachine;
00033   class formatted_raw_ostream;
00034 
00035   /// Target - Wrapper for Target specific information.
00036   ///
00037   /// For registration purposes, this is a POD type so that targets can be
00038   /// registered without the use of static constructors.
00039   ///
00040   /// Targets should implement a single global instance of this class (which
00041   /// will be zero initialized), and pass that instance to the TargetRegistry as
00042   /// part of their initialization.
00043   class Target {
00044   public:
00045     friend struct TargetRegistry;
00046 
00047     typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
00048 
00049     typedef const TargetAsmInfo *(*AsmInfoCtorFnTy)(const Target &T,
00050                                                     const StringRef &TT);
00051     typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
00052                                                   const std::string &TT,
00053                                                   const std::string &Features);
00054     typedef AsmPrinter *(*AsmPrinterCtorTy)(formatted_raw_ostream &OS,
00055                                             TargetMachine &TM,
00056                                             const TargetAsmInfo *TAI,
00057                                             bool VerboseAsm);
00058     typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &T,
00059                                                 MCAsmParser &P);
00060   private:
00061     /// Next - The next registered target in the linked list, maintained by the
00062     /// TargetRegistry.
00063     Target *Next;
00064 
00065     /// TripleMatchQualityFn - The target function for rating the match quality
00066     /// of a triple.
00067     TripleMatchQualityFnTy TripleMatchQualityFn;
00068 
00069     /// Name - The target name.
00070     const char *Name;
00071 
00072     /// ShortDesc - A short description of the target.
00073     const char *ShortDesc;
00074 
00075     /// HasJIT - Whether this target supports the JIT.
00076     bool HasJIT;
00077 
00078     AsmInfoCtorFnTy AsmInfoCtorFn;
00079     
00080     /// TargetMachineCtorFn - Construction function for this target's
00081     /// TargetMachine, if registered.
00082     TargetMachineCtorTy TargetMachineCtorFn;
00083 
00084     /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
00085     /// if registered.
00086     AsmPrinterCtorTy AsmPrinterCtorFn;
00087 
00088     /// AsmParserCtorFn - Construction function for this target's AsmParser,
00089     /// if registered.
00090     AsmParserCtorTy AsmParserCtorFn;
00091 
00092   public:
00093     // getNext - Return the next registered target.
00094     const Target *getNext() const { return Next; }
00095 
00096     /// getName - Get the target name.
00097     const char *getName() const { return Name; }
00098 
00099     /// getShortDescription - Get a short description of the target.
00100     const char *getShortDescription() const { return ShortDesc; }
00101 
00102     bool hasJIT() const { return HasJIT; }
00103 
00104     /// hasTargetMachine - Check if this target supports code generation.
00105     bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
00106 
00107     /// hasAsmPrinter - Check if this target supports .s printing.
00108     bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
00109 
00110     /// hasAsmParser - Check if this target supports .s parsing.
00111     bool hasAsmParser() const { return AsmParserCtorFn != 0; }
00112 
00113     
00114     /// createAsmInfo - Create a TargetAsmInfo implementation for the specified
00115     /// target triple.
00116     ///
00117     /// \arg Triple - This argument is used to determine the target machine
00118     /// feature set; it should always be provided. Generally this should be
00119     /// either the target triple from the module, or the target triple of the
00120     /// host if that does not exist.
00121     const TargetAsmInfo *createAsmInfo(const StringRef &Triple) const {
00122       if (!AsmInfoCtorFn)
00123         return 0;
00124       return AsmInfoCtorFn(*this, Triple);
00125     }
00126     
00127     /// createTargetMachine - Create a target specific machine implementation
00128     /// for the specified \arg Triple.
00129     ///
00130     /// \arg Triple - This argument is used to determine the target machine
00131     /// feature set; it should always be provided. Generally this should be
00132     /// either the target triple from the module, or the target triple of the
00133     /// host if that does not exist.
00134     TargetMachine *createTargetMachine(const std::string &Triple,
00135                                        const std::string &Features) const {
00136       if (!TargetMachineCtorFn)
00137         return 0;
00138       return TargetMachineCtorFn(*this, Triple, Features);
00139     }
00140 
00141     /// createAsmPrinter - Create a target specific assembly printer pass.
00142     AsmPrinter *createAsmPrinter(formatted_raw_ostream &OS, TargetMachine &TM,
00143                                  const TargetAsmInfo *TAI, bool Verbose) const {
00144       if (!AsmPrinterCtorFn)
00145         return 0;
00146       return AsmPrinterCtorFn(OS, TM, TAI, Verbose);
00147     }
00148 
00149     /// createAsmParser - Create a target specific assembly parser.
00150     ///
00151     /// \arg Parser - The target independent parser implementation to use for
00152     /// parsing and lexing.
00153     TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
00154       if (!AsmParserCtorFn)
00155         return 0;
00156       return AsmParserCtorFn(*this, Parser);
00157     }
00158   };
00159 
00160   /// TargetRegistry - Generic interface to target specific features.
00161   struct TargetRegistry {
00162     class iterator {
00163       const Target *Current;
00164       explicit iterator(Target *T) : Current(T) {}
00165       friend struct TargetRegistry;
00166     public:
00167       iterator(const iterator &I) : Current(I.Current) {}
00168       iterator() : Current(0) {}
00169 
00170       bool operator==(const iterator &x) const {
00171         return Current == x.Current;
00172       }
00173       bool operator!=(const iterator &x) const {
00174         return !operator==(x);
00175       }
00176 
00177       // Iterator traversal: forward iteration only
00178       iterator &operator++() {          // Preincrement
00179         assert(Current && "Cannot increment end iterator!");
00180         Current = Current->getNext();
00181         return *this;
00182       }
00183       iterator operator++(int) {        // Postincrement
00184         iterator tmp = *this; 
00185         ++*this; 
00186         return tmp;
00187       }
00188 
00189       const Target &operator*() const {
00190         assert(Current && "Cannot dereference end iterator!");
00191         return *Current;
00192       }
00193 
00194       const Target *operator->() const {
00195         return &operator*();
00196       }
00197     };
00198 
00199     /// @name Registry Access
00200     /// @{
00201 
00202     static iterator begin();
00203 
00204     static iterator end() { return iterator(); }
00205 
00206     /// lookupTarget - Lookup a target based on a target triple.
00207     ///
00208     /// \param Triple - The triple to use for finding a target.
00209     /// \param Error - On failure, an error string describing why no target was
00210     /// found.
00211     static const Target *lookupTarget(const std::string &Triple,
00212                                       std::string &Error);
00213 
00214     /// getClosestTargetForJIT - Pick the best target that is compatible with
00215     /// the current host.  If no close target can be found, this returns null
00216     /// and sets the Error string to a reason.
00217     ///
00218     /// Maintained for compatibility through 2.6.
00219     static const Target *getClosestTargetForJIT(std::string &Error);
00220 
00221     /// @}
00222     /// @name Target Registration
00223     /// @{
00224 
00225     /// RegisterTarget - Register the given target. Attempts to register a
00226     /// target which has already been registered will be ignored.
00227     /// 
00228     /// Clients are responsible for ensuring that registration doesn't occur
00229     /// while another thread is attempting to access the registry. Typically
00230     /// this is done by initializing all targets at program startup.
00231     ///
00232     /// @param T - The target being registered.
00233     /// @param Name - The target name. This should be a static string.
00234     /// @param ShortDesc - A short target description. This should be a static
00235     /// string. 
00236     /// @param TQualityFn - The triple match quality computation function for
00237     /// this target.
00238     /// @param HasJIT - Whether the target supports JIT code
00239     /// generation.
00240     static void RegisterTarget(Target &T,
00241                                const char *Name,
00242                                const char *ShortDesc,
00243                                Target::TripleMatchQualityFnTy TQualityFn,
00244                                bool HasJIT = false);
00245 
00246     /// RegisterAsmInfo - Register a TargetAsmInfo implementation for the
00247     /// given target.
00248     /// 
00249     /// Clients are responsible for ensuring that registration doesn't occur
00250     /// while another thread is attempting to access the registry. Typically
00251     /// this is done by initializing all targets at program startup.
00252     /// 
00253     /// @param T - The target being registered.
00254     /// @param Fn - A function to construct a TargetAsmInfo for the target.
00255     static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
00256       // Ignore duplicate registration.
00257       if (!T.AsmInfoCtorFn)
00258         T.AsmInfoCtorFn = Fn;
00259     }
00260     
00261     /// RegisterTargetMachine - Register a TargetMachine implementation for the
00262     /// given target.
00263     /// 
00264     /// Clients are responsible for ensuring that registration doesn't occur
00265     /// while another thread is attempting to access the registry. Typically
00266     /// this is done by initializing all targets at program startup.
00267     /// 
00268     /// @param T - The target being registered.
00269     /// @param Fn - A function to construct a TargetMachine for the target.
00270     static void RegisterTargetMachine(Target &T, 
00271                                       Target::TargetMachineCtorTy Fn) {
00272       // Ignore duplicate registration.
00273       if (!T.TargetMachineCtorFn)
00274         T.TargetMachineCtorFn = Fn;
00275     }
00276 
00277     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
00278     /// target.
00279     /// 
00280     /// Clients are responsible for ensuring that registration doesn't occur
00281     /// while another thread is attempting to access the registry. Typically
00282     /// this is done by initializing all targets at program startup.
00283     ///
00284     /// @param T - The target being registered.
00285     /// @param Fn - A function to construct an AsmPrinter for the target.
00286     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
00287       // Ignore duplicate registration.
00288       if (!T.AsmPrinterCtorFn)
00289         T.AsmPrinterCtorFn = Fn;
00290     }
00291 
00292     /// RegisterAsmParser - Register a TargetAsmParser implementation for the
00293     /// given target.
00294     /// 
00295     /// Clients are responsible for ensuring that registration doesn't occur
00296     /// while another thread is attempting to access the registry. Typically
00297     /// this is done by initializing all targets at program startup.
00298     ///
00299     /// @param T - The target being registered.
00300     /// @param Fn - A function to construct an AsmPrinter for the target.
00301     static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
00302       if (!T.AsmParserCtorFn)
00303         T.AsmParserCtorFn = Fn;
00304     }
00305 
00306     /// @}
00307   };
00308 
00309 
00310   //===--------------------------------------------------------------------===//
00311 
00312   /// RegisterTarget - Helper template for registering a target, for use in the
00313   /// target's initialization function. Usage:
00314   ///
00315   ///
00316   /// Target TheFooTarget; // The global target instance.
00317   ///
00318   /// extern "C" void LLVMInitializeFooTargetInfo() {
00319   ///   RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
00320   /// }
00321   template<Triple::ArchType TargetArchType = Triple::InvalidArch,
00322            bool HasJIT = false>
00323   struct RegisterTarget {
00324     RegisterTarget(Target &T, const char *Name, const char *Desc) {
00325       TargetRegistry::RegisterTarget(T, Name, Desc,
00326                                      &getTripleMatchQuality,
00327                                      HasJIT);
00328     }
00329 
00330     static unsigned getTripleMatchQuality(const std::string &TT) {
00331       if (Triple(TT.c_str()).getArch() == TargetArchType)
00332         return 20;
00333       return 0;
00334     }
00335   };
00336 
00337   /// RegisterAsmInfo - Helper template for registering a target assembly info
00338   /// implementation.  This invokes the static "Create" method on the class to
00339   /// actually do the construction.  Usage:
00340   ///
00341   /// extern "C" void LLVMInitializeFooTarget() {
00342   ///   extern Target TheFooTarget;
00343   ///   RegisterAsmInfo<FooTargetAsmInfo> X(TheFooTarget);
00344   /// }
00345   template<class TargetAsmInfoImpl>
00346   struct RegisterAsmInfo {
00347     RegisterAsmInfo(Target &T) {
00348       TargetRegistry::RegisterAsmInfo(T, &Allocator);
00349     }
00350   private:
00351     static const TargetAsmInfo *Allocator(const Target &T, const StringRef &TT){
00352       return new TargetAsmInfoImpl(T, TT);
00353     }
00354     
00355   };
00356 
00357   /// RegisterAsmInfoFn - Helper template for registering a target assembly info
00358   /// implementation.  This invokes the specified function to do the
00359   /// construction.  Usage:
00360   ///
00361   /// extern "C" void LLVMInitializeFooTarget() {
00362   ///   extern Target TheFooTarget;
00363   ///   RegisterAsmInfoFn X(TheFooTarget, TheFunction);
00364   /// }
00365   struct RegisterAsmInfoFn {
00366     RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
00367       TargetRegistry::RegisterAsmInfo(T, Fn);
00368     }
00369   };
00370 
00371 
00372   /// RegisterTargetMachine - Helper template for registering a target machine
00373   /// implementation, for use in the target machine initialization
00374   /// function. Usage:
00375   ///
00376   /// extern "C" void LLVMInitializeFooTarget() {
00377   ///   extern Target TheFooTarget;
00378   ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
00379   /// }
00380   template<class TargetMachineImpl>
00381   struct RegisterTargetMachine {
00382     RegisterTargetMachine(Target &T) {
00383       TargetRegistry::RegisterTargetMachine(T, &Allocator);
00384     }
00385 
00386   private:
00387     static TargetMachine *Allocator(const Target &T, const std::string &TT,
00388                                     const std::string &FS) {
00389       return new TargetMachineImpl(T, TT, FS);
00390     }
00391   };
00392 
00393   /// RegisterAsmPrinter - Helper template for registering a target specific
00394   /// assembly printer, for use in the target machine initialization
00395   /// function. Usage:
00396   ///
00397   /// extern "C" void LLVMInitializeFooAsmPrinter() {
00398   ///   extern Target TheFooTarget;
00399   ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
00400   /// }
00401   template<class AsmPrinterImpl>
00402   struct RegisterAsmPrinter {
00403     RegisterAsmPrinter(Target &T) {
00404       TargetRegistry::RegisterAsmPrinter(T, &Allocator);
00405     }
00406 
00407   private:
00408     static AsmPrinter *Allocator(formatted_raw_ostream &OS, TargetMachine &TM,
00409                                  const TargetAsmInfo *TAI, bool Verbose) {
00410       return new AsmPrinterImpl(OS, TM, TAI, Verbose);
00411     }
00412   };
00413 
00414   /// RegisterAsmParser - Helper template for registering a target specific
00415   /// assembly parser, for use in the target machine initialization
00416   /// function. Usage:
00417   ///
00418   /// extern "C" void LLVMInitializeFooAsmParser() {
00419   ///   extern Target TheFooTarget;
00420   ///   RegisterAsmParser<FooAsmParser> X(TheFooTarget);
00421   /// }
00422   template<class AsmParserImpl>
00423   struct RegisterAsmParser {
00424     RegisterAsmParser(Target &T) {
00425       TargetRegistry::RegisterAsmParser(T, &Allocator);
00426     }
00427 
00428   private:
00429     static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
00430       return new AsmParserImpl(T, P);
00431     }
00432   };
00433 
00434 }
00435 
00436 #endif