Changes For Bug 352
[oota-llvm.git] / include / llvm / Target / TargetMachineRegistry.h
index a6038458cbe605f9676c703b4f17b952a7e82a96..08ab56e10aec80784d950354f5b287eba5803e7d 100644 (file)
 #ifndef LLVM_TARGET_TARGETMACHINEREGISTRY_H
 #define LLVM_TARGET_TARGETMACHINEREGISTRY_H
 
+#include "llvm/Support/CommandLine.h"
+
 namespace llvm {
   class Module;
   class TargetMachine;
   class IntrinsicLowering;
 
   struct TargetMachineRegistry {
+    struct Entry;
+
+    /// TargetMachineRegistry::getList - This static method returns the list of
+    /// target machines that are registered with the system.
+    static const Entry *getList() { return List; }
+
+    /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
+    /// target that is compatible with the module.  If no close target can be
+    /// found, this returns null and sets the Error string to a reason.
+    static const Entry *getClosestStaticTargetForModule(const Module &M,
+                                                        std::string &Error);
+
+    /// getClosestTargetForJIT - Given an LLVM module, pick the best target that
+    /// is compatible with the current host and the specified module.  If no
+    /// close target can be found, this returns null and sets the Error string
+    /// to a reason.
+    static const Entry *getClosestTargetForJIT(std::string &Error);
+
+
     /// Entry - One instance of this struct is created for each target that is
     /// registered.
     struct Entry {
@@ -37,19 +58,11 @@ namespace llvm {
     protected:
       Entry(const char *N, const char *SD,
             TargetMachine *(*CF)(const Module &, IntrinsicLowering*),
-            unsigned (*MMF)(const Module &M), unsigned (*JMF)())
-      : Name(N), ShortDesc(SD), CtorFn(CF), ModuleMatchQualityFn(MMF),
-      JITMatchQualityFn(JMF), Next(List) {
-        List = this;
-      }
+            unsigned (*MMF)(const Module &M), unsigned (*JMF)());
     private:
       const Entry *Next;  // Next entry in the linked list.
     };
 
-    /// TargetMachineRegistry::getList - This static method returns the list of
-    /// target machines that are registered with the system.
-    static const Entry *getList() { return List; }
-
   private:
     static const Entry *List;
   };
@@ -71,6 +84,40 @@ namespace llvm {
       return new TargetMachineImpl(M, IL);
     }
   };
+
+  /// TargetRegistrationListener - This class allows code to listen for targets
+  /// that are dynamically registered, and be notified of it when they are.
+  class TargetRegistrationListener {
+    TargetRegistrationListener **Prev, *Next;
+  public:
+    TargetRegistrationListener();
+    virtual ~TargetRegistrationListener();
+
+    TargetRegistrationListener *getNext() const { return Next; }
+    
+    virtual void targetRegistered(const TargetMachineRegistry::Entry *E) = 0;
+  };
+
+
+  //===--------------------------------------------------------------------===//
+  /// TargetNameParser - This option can be used to provide a command line
+  /// option to choose among the various registered targets (commonly -march).
+  class TargetNameParser : public TargetRegistrationListener,
+    public cl::parser<const TargetMachineRegistry::Entry*> {
+  public:
+    void initialize(cl::Option &O) {
+      for (const TargetMachineRegistry::Entry *E =
+             TargetMachineRegistry::getList(); E; E = E->getNext())
+        Values.push_back(std::make_pair(E->Name,
+                                        std::make_pair(E, E->ShortDesc)));
+      cl::parser<const TargetMachineRegistry::Entry*>::initialize(O);
+    }
+
+    virtual void targetRegistered(const TargetMachineRegistry::Entry *E) {
+      Values.push_back(std::make_pair(E->Name,
+                                      std::make_pair(E, E->ShortDesc)));
+    }
+  };
 }
 
 #endif