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