LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

TargetMachineRegistry.h

Go to the documentation of this file.
00001 //===-- Target/TargetMachineRegistry.h - Target Registration ----*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file exposes two classes: the TargetMachineRegistry class, which allows
00011 // tools to inspect all of registered targets, and the RegisterTarget class,
00012 // which TargetMachine implementations should use to register themselves with
00013 // the system.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_TARGET_TARGETMACHINEREGISTRY_H
00018 #define LLVM_TARGET_TARGETMACHINEREGISTRY_H
00019 
00020 #include "llvm/Support/CommandLine.h"
00021 
00022 namespace llvm {
00023   class Module;
00024   class TargetMachine;
00025   class IntrinsicLowering;
00026 
00027   struct TargetMachineRegistry {
00028     struct Entry;
00029 
00030     /// TargetMachineRegistry::getList - This static method returns the list of
00031     /// target machines that are registered with the system.
00032     static const Entry *getList() { return List; }
00033 
00034     /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
00035     /// target that is compatible with the module.  If no close target can be
00036     /// found, this returns null and sets the Error string to a reason.
00037     static const Entry *getClosestStaticTargetForModule(const Module &M,
00038                                                         std::string &Error);
00039 
00040     /// getClosestTargetForJIT - Given an LLVM module, pick the best target that
00041     /// is compatible with the current host and the specified module.  If no
00042     /// close target can be found, this returns null and sets the Error string
00043     /// to a reason.
00044     static const Entry *getClosestTargetForJIT(std::string &Error);
00045 
00046 
00047     /// Entry - One instance of this struct is created for each target that is
00048     /// registered.
00049     struct Entry {
00050       const char *Name;
00051       const char *ShortDesc;
00052       TargetMachine *(*CtorFn)(const Module &, IntrinsicLowering*);
00053       unsigned (*ModuleMatchQualityFn)(const Module &M);
00054       unsigned (*JITMatchQualityFn)();
00055 
00056       const Entry *getNext() const { return Next; }
00057 
00058     protected:
00059       Entry(const char *N, const char *SD,
00060             TargetMachine *(*CF)(const Module &, IntrinsicLowering*),
00061             unsigned (*MMF)(const Module &M), unsigned (*JMF)());
00062     private:
00063       const Entry *Next;  // Next entry in the linked list.
00064     };
00065 
00066   private:
00067     static const Entry *List;
00068   };
00069 
00070   //===--------------------------------------------------------------------===//
00071   /// RegisterTarget - This class is used to make targets automatically register
00072   /// themselves with the tool they are linked.  Targets should define an
00073   /// instance of this and implement the static methods described in the
00074   /// TargetMachine comments..
00075   template<class TargetMachineImpl>
00076   struct RegisterTarget : public TargetMachineRegistry::Entry {
00077     RegisterTarget(const char *Name, const char *ShortDesc) : 
00078       TargetMachineRegistry::Entry(Name, ShortDesc, &Allocator,
00079                                    &TargetMachineImpl::getModuleMatchQuality,
00080                                    &TargetMachineImpl::getJITMatchQuality) {
00081     }
00082   private:
00083     static TargetMachine *Allocator(const Module &M, IntrinsicLowering *IL) {
00084       return new TargetMachineImpl(M, IL);
00085     }
00086   };
00087 
00088   /// TargetRegistrationListener - This class allows code to listen for targets
00089   /// that are dynamically registered, and be notified of it when they are.
00090   class TargetRegistrationListener {
00091     TargetRegistrationListener **Prev, *Next;
00092   public:
00093     TargetRegistrationListener();
00094     virtual ~TargetRegistrationListener();
00095 
00096     TargetRegistrationListener *getNext() const { return Next; }
00097     
00098     virtual void targetRegistered(const TargetMachineRegistry::Entry *E) = 0;
00099   };
00100 
00101 
00102   //===--------------------------------------------------------------------===//
00103   /// TargetNameParser - This option can be used to provide a command line
00104   /// option to choose among the various registered targets (commonly -march).
00105   class TargetNameParser : public TargetRegistrationListener,
00106     public cl::parser<const TargetMachineRegistry::Entry*> {
00107   public:
00108     void initialize(cl::Option &O) {
00109       for (const TargetMachineRegistry::Entry *E =
00110              TargetMachineRegistry::getList(); E; E = E->getNext())
00111         Values.push_back(std::make_pair(E->Name,
00112                                         std::make_pair(E, E->ShortDesc)));
00113       cl::parser<const TargetMachineRegistry::Entry*>::initialize(O);
00114     }
00115 
00116     virtual void targetRegistered(const TargetMachineRegistry::Entry *E) {
00117       Values.push_back(std::make_pair(E->Name,
00118                                       std::make_pair(E, E->ShortDesc)));
00119     }
00120   };
00121 }
00122 
00123 #endif