Fix the comment for getClosestTargetForJIT to reflect the fact that
[oota-llvm.git] / include / llvm / Target / TargetMachineRegistry.h
1 //===-- Target/TargetMachineRegistry.h - Target Registration ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file exposes two classes: the TargetMachineRegistry class, which allows
11 // tools to inspect all of registered targets, and the RegisterTarget class,
12 // which TargetMachine implementations should use to register themselves with
13 // the system.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_TARGET_TARGETMACHINEREGISTRY_H
18 #define LLVM_TARGET_TARGETMACHINEREGISTRY_H
19
20 #include "llvm/Support/CommandLine.h"
21
22 namespace llvm {
23   class Module;
24   class TargetMachine;
25
26   struct TargetMachineRegistry {
27     struct Entry;
28
29     /// TargetMachineRegistry::getList - This static method returns the list of
30     /// target machines that are registered with the system.
31     static const Entry *getList() { return List; }
32
33     /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
34     /// target that is compatible with the module.  If no close target can be
35     /// found, this returns null and sets the Error string to a reason.
36     static const Entry *getClosestStaticTargetForModule(const Module &M,
37                                                         std::string &Error);
38
39     /// getClosestTargetForJIT - Pick the best target that is compatible with
40     /// the current host.  If no close target can be found, this returns null
41     /// and sets the Error string to a reason.
42     static const Entry *getClosestTargetForJIT(std::string &Error);
43
44
45     /// Entry - One instance of this struct is created for each target that is
46     /// registered.
47     struct Entry {
48       const char *Name;
49       const char *ShortDesc;
50       TargetMachine *(*CtorFn)(const Module &, const std::string &);
51       unsigned (*ModuleMatchQualityFn)(const Module &M);
52       unsigned (*JITMatchQualityFn)();
53
54       const Entry *getNext() const { return Next; }
55
56     protected:
57       Entry(const char *N, const char *SD,
58             TargetMachine *(*CF)(const Module &, const std::string &),
59             unsigned (*MMF)(const Module &M), unsigned (*JMF)());
60     private:
61       const Entry *Next;  // Next entry in the linked list.
62     };
63
64   private:
65     static const Entry *List;
66   };
67
68   //===--------------------------------------------------------------------===//
69   /// RegisterTarget - This class is used to make targets automatically register
70   /// themselves with the tool they are linked.  Targets should define an
71   /// instance of this and implement the static methods described in the
72   /// TargetMachine comments.
73   /// The type 'TargetMachineImpl' should provide a constructor with two 
74   /// parameters:
75   /// - const Module& M: the module that is being compiled:
76   /// - const std::string& FS: target-specific string describing target 
77   ///   flavour.
78   
79   template<class TargetMachineImpl>
80   struct RegisterTarget : public TargetMachineRegistry::Entry {
81     RegisterTarget(const char *Name, const char *ShortDesc) :
82       TargetMachineRegistry::Entry(Name, ShortDesc, &Allocator,
83                                    &TargetMachineImpl::getModuleMatchQuality,
84                                    &TargetMachineImpl::getJITMatchQuality) {
85     }
86   private:
87     static TargetMachine *Allocator(const Module &M, const std::string &FS) {
88       return new TargetMachineImpl(M, FS);
89     }
90   };
91
92   /// TargetRegistrationListener - This class allows code to listen for targets
93   /// that are dynamically registered, and be notified of it when they are.
94   class TargetRegistrationListener {
95     TargetRegistrationListener **Prev, *Next;
96   public:
97     TargetRegistrationListener();
98     virtual ~TargetRegistrationListener();
99
100     TargetRegistrationListener *getNext() const { return Next; }
101
102     virtual void targetRegistered(const TargetMachineRegistry::Entry *E) = 0;
103   };
104
105
106   //===--------------------------------------------------------------------===//
107   /// TargetNameParser - This option can be used to provide a command line
108   /// option to choose among the various registered targets (commonly -march).
109   class TargetNameParser : public TargetRegistrationListener,
110     public cl::parser<const TargetMachineRegistry::Entry*> {
111   public:
112     void initialize(cl::Option &O) {
113       for (const TargetMachineRegistry::Entry *E =
114              TargetMachineRegistry::getList(); E; E = E->getNext())
115         Values.push_back(std::make_pair(E->Name,
116                                         std::make_pair(E, E->ShortDesc)));
117       cl::parser<const TargetMachineRegistry::Entry*>::initialize(O);
118     }
119
120     virtual void targetRegistered(const TargetMachineRegistry::Entry *E) {
121       Values.push_back(std::make_pair(E->Name,
122                                       std::make_pair(E, E->ShortDesc)));
123     }
124   };
125 }
126
127 #endif