Patches to make the LLVM sources more -pedantic clean. Patch provided
[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 - Given an LLVM module, pick the best target that
40     /// is compatible with the current host and the specified module.  If no
41     /// close target can be found, this returns null and sets the Error string
42     /// to a reason.
43     static const Entry *getClosestTargetForJIT(std::string &Error);
44
45
46     /// Entry - One instance of this struct is created for each target that is
47     /// registered.
48     struct Entry {
49       const char *Name;
50       const char *ShortDesc;
51       TargetMachine *(*CtorFn)(const Module &, const std::string &);
52       unsigned (*ModuleMatchQualityFn)(const Module &M);
53       unsigned (*JITMatchQualityFn)();
54
55       const Entry *getNext() const { return Next; }
56
57     protected:
58       Entry(const char *N, const char *SD,
59             TargetMachine *(*CF)(const Module &, const std::string &),
60             unsigned (*MMF)(const Module &M), unsigned (*JMF)());
61     private:
62       const Entry *Next;  // Next entry in the linked list.
63     };
64
65   private:
66     static const Entry *List;
67   };
68
69   //===--------------------------------------------------------------------===//
70   /// RegisterTarget - This class is used to make targets automatically register
71   /// themselves with the tool they are linked.  Targets should define an
72   /// instance of this and implement the static methods described in the
73   /// TargetMachine comments.
74   /// The type 'TargetMachineImpl' should provide a constructor with two 
75   /// parameters:
76   /// - const Module& M: the module that is being compiled:
77   /// - const std::string& FS: target-specific string describing target 
78   ///   flavour.
79   
80   template<class TargetMachineImpl>
81   struct RegisterTarget : public TargetMachineRegistry::Entry {
82     RegisterTarget(const char *Name, const char *ShortDesc) :
83       TargetMachineRegistry::Entry(Name, ShortDesc, &Allocator,
84                                    &TargetMachineImpl::getModuleMatchQuality,
85                                    &TargetMachineImpl::getJITMatchQuality) {
86     }
87   private:
88     static TargetMachine *Allocator(const Module &M, const std::string &FS) {
89       return new TargetMachineImpl(M, FS);
90     }
91   };
92
93   /// TargetRegistrationListener - This class allows code to listen for targets
94   /// that are dynamically registered, and be notified of it when they are.
95   class TargetRegistrationListener {
96     TargetRegistrationListener **Prev, *Next;
97   public:
98     TargetRegistrationListener();
99     virtual ~TargetRegistrationListener();
100
101     TargetRegistrationListener *getNext() const { return Next; }
102
103     virtual void targetRegistered(const TargetMachineRegistry::Entry *E) = 0;
104   };
105
106
107   //===--------------------------------------------------------------------===//
108   /// TargetNameParser - This option can be used to provide a command line
109   /// option to choose among the various registered targets (commonly -march).
110   class TargetNameParser : public TargetRegistrationListener,
111     public cl::parser<const TargetMachineRegistry::Entry*> {
112   public:
113     void initialize(cl::Option &O) {
114       for (const TargetMachineRegistry::Entry *E =
115              TargetMachineRegistry::getList(); E; E = E->getNext())
116         Values.push_back(std::make_pair(E->Name,
117                                         std::make_pair(E, E->ShortDesc)));
118       cl::parser<const TargetMachineRegistry::Entry*>::initialize(O);
119     }
120
121     virtual void targetRegistered(const TargetMachineRegistry::Entry *E) {
122       Values.push_back(std::make_pair(E->Name,
123                                       std::make_pair(E, E->ShortDesc)));
124     }
125   };
126 }
127
128 #endif