LLVM API Documentation
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 /// The type 'TargetMachineImpl' should provide a constructor with two 00075 /// parameters: 00076 /// - const Module& M: the module that is being compiled: 00077 /// - const std::string& FS: target-specific string describing target 00078 /// flavour. 00079 00080 template<class TargetMachineImpl> 00081 struct RegisterTarget : public TargetMachineRegistry::Entry { 00082 RegisterTarget(const char *Name, const char *ShortDesc) : 00083 TargetMachineRegistry::Entry(Name, ShortDesc, &Allocator, 00084 &TargetMachineImpl::getModuleMatchQuality, 00085 &TargetMachineImpl::getJITMatchQuality) { 00086 } 00087 private: 00088 static TargetMachine *Allocator(const Module &M, const std::string &FS) { 00089 return new TargetMachineImpl(M, FS); 00090 } 00091 }; 00092 00093 /// TargetRegistrationListener - This class allows code to listen for targets 00094 /// that are dynamically registered, and be notified of it when they are. 00095 class TargetRegistrationListener { 00096 TargetRegistrationListener **Prev, *Next; 00097 public: 00098 TargetRegistrationListener(); 00099 virtual ~TargetRegistrationListener(); 00100 00101 TargetRegistrationListener *getNext() const { return Next; } 00102 00103 virtual void targetRegistered(const TargetMachineRegistry::Entry *E) = 0; 00104 }; 00105 00106 00107 //===--------------------------------------------------------------------===// 00108 /// TargetNameParser - This option can be used to provide a command line 00109 /// option to choose among the various registered targets (commonly -march). 00110 class TargetNameParser : public TargetRegistrationListener, 00111 public cl::parser<const TargetMachineRegistry::Entry*> { 00112 public: 00113 void initialize(cl::Option &O) { 00114 for (const TargetMachineRegistry::Entry *E = 00115 TargetMachineRegistry::getList(); E; E = E->getNext()) 00116 Values.push_back(std::make_pair(E->Name, 00117 std::make_pair(E, E->ShortDesc))); 00118 cl::parser<const TargetMachineRegistry::Entry*>::initialize(O); 00119 } 00120 00121 virtual void targetRegistered(const TargetMachineRegistry::Entry *E) { 00122 Values.push_back(std::make_pair(E->Name, 00123 std::make_pair(E, E->ShortDesc))); 00124 } 00125 }; 00126 } 00127 00128 #endif