LLVM API Documentation

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 
00026   struct TargetMachineRegistry {
00027     struct Entry;
00028 
00029     /// TargetMachineRegistry::getList - This static method returns the list of
00030     /// target machines that are registered with the system.
00031     static const Entry *getList() { return List; }
00032 
00033     /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
00034     /// target that is compatible with the module.  If no close target can be
00035     /// found, this returns null and sets the Error string to a reason.
00036     static const Entry *getClosestStaticTargetForModule(const Module &M,
00037                                                         std::string &Error);
00038 
00039     /// getClosestTargetForJIT - Given an LLVM module, pick the best target that
00040     /// is compatible with the current host and the specified module.  If no
00041     /// close target can be found, this returns null and sets the Error string
00042     /// to a reason.
00043     static const Entry *getClosestTargetForJIT(std::string &Error);
00044 
00045 
00046     /// Entry - One instance of this struct is created for each target that is
00047     /// registered.
00048     struct Entry {
00049       const char *Name;
00050       const char *ShortDesc;
00051       TargetMachine *(*CtorFn)(const Module &, const std::string &);
00052       unsigned (*ModuleMatchQualityFn)(const Module &M);
00053       unsigned (*JITMatchQualityFn)();
00054 
00055       const Entry *getNext() const { return Next; }
00056 
00057     protected:
00058       Entry(const char *N, const char *SD,
00059             TargetMachine *(*CF)(const Module &, const std::string &),
00060             unsigned (*MMF)(const Module &M), unsigned (*JMF)());
00061     private:
00062       const Entry *Next;  // Next entry in the linked list.
00063     };
00064 
00065   private:
00066     static const Entry *List;
00067   };
00068 
00069   //===--------------------------------------------------------------------===//
00070   /// RegisterTarget - This class is used to make targets automatically register
00071   /// themselves with the tool they are linked.  Targets should define an
00072   /// instance of this and implement the static methods described in the
00073   /// TargetMachine comments..
00074   template<class TargetMachineImpl>
00075   struct RegisterTarget : public TargetMachineRegistry::Entry {
00076     RegisterTarget(const char *Name, const char *ShortDesc) :
00077       TargetMachineRegistry::Entry(Name, ShortDesc, &Allocator,
00078                                    &TargetMachineImpl::getModuleMatchQuality,
00079                                    &TargetMachineImpl::getJITMatchQuality) {
00080     }
00081   private:
00082     static TargetMachine *Allocator(const Module &M, const std::string &FS) {
00083       return new TargetMachineImpl(M, FS);
00084     }
00085   };
00086 
00087   /// TargetRegistrationListener - This class allows code to listen for targets
00088   /// that are dynamically registered, and be notified of it when they are.
00089   class TargetRegistrationListener {
00090     TargetRegistrationListener **Prev, *Next;
00091   public:
00092     TargetRegistrationListener();
00093     virtual ~TargetRegistrationListener();
00094 
00095     TargetRegistrationListener *getNext() const { return Next; }
00096 
00097     virtual void targetRegistered(const TargetMachineRegistry::Entry *E) = 0;
00098   };
00099 
00100 
00101   //===--------------------------------------------------------------------===//
00102   /// TargetNameParser - This option can be used to provide a command line
00103   /// option to choose among the various registered targets (commonly -march).
00104   class TargetNameParser : public TargetRegistrationListener,
00105     public cl::parser<const TargetMachineRegistry::Entry*> {
00106   public:
00107     void initialize(cl::Option &O) {
00108       for (const TargetMachineRegistry::Entry *E =
00109              TargetMachineRegistry::getList(); E; E = E->getNext())
00110         Values.push_back(std::make_pair(E->Name,
00111                                         std::make_pair(E, E->ShortDesc)));
00112       cl::parser<const TargetMachineRegistry::Entry*>::initialize(O);
00113     }
00114 
00115     virtual void targetRegistered(const TargetMachineRegistry::Entry *E) {
00116       Values.push_back(std::make_pair(E->Name,
00117                                       std::make_pair(E, E->ShortDesc)));
00118     }
00119   };
00120 }
00121 
00122 #endif