Include the Target& in the TargetMachineRegisterEntry.
[oota-llvm.git] / include / llvm / Target / TargetRegistry.h
1 //===-- Target/TargetRegistry.h - Target Registration -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file exposes the TargetRegistry interface, which tools can use to access
11 // the appropriate target specific classes (TargetMachine, AsmPrinter, etc.)
12 // which have been registered.
13 //
14 // Target specific class implementations should register themselves using the
15 // appropriate TargetRegistry interfaces.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_TARGET_TARGETREGISTRY_H
20 #define LLVM_TARGET_TARGETREGISTRY_H
21
22 #include <string>
23 #include <cassert>
24
25 namespace llvm {
26   class FunctionPass;
27   class Module;
28   class TargetMachine;
29   class formatted_raw_ostream;
30
31   /// Target - Wrapper for Target specific information.
32   ///
33   /// For registration purposes, this is a POD type so that targets can be
34   /// registered without the use of static constructors.
35   ///
36   /// Targets should implement a single global instance of this class (which
37   /// will be zero initialized), and pass that instance to the TargetRegistry as
38   /// part of their initialization.
39   class Target {
40   private:
41     typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
42     typedef unsigned (*ModuleMatchQualityFnTy)(const Module &M);
43     typedef unsigned (*JITMatchQualityFnTy)();
44
45     typedef TargetMachine *(*TargetMachineCtorTy)(const Module &, 
46                                                   const std::string &);
47     typedef FunctionPass *(*AsmPrinterCtorTy)(formatted_raw_ostream &,
48                                               TargetMachine &,
49                                               bool);
50
51     friend struct TargetRegistry;
52     // FIXME: Temporary hack, please remove.
53     friend struct TargetMachineRegistry;
54
55     /// Next - The next registered target in the linked list, maintained by the
56     /// TargetRegistry.
57     Target *Next;
58
59     /// TripleMatchQualityFn - The target function for rating the match quality
60     /// of a triple.
61     TripleMatchQualityFnTy TripleMatchQualityFn;
62
63     /// ModuleMatchQualityFn - The target function for rating the match quality
64     /// of a module.
65     ModuleMatchQualityFnTy ModuleMatchQualityFn;
66
67     /// JITMatchQualityFn - The target function for rating the match quality
68     /// with the host.
69     JITMatchQualityFnTy JITMatchQualityFn;
70
71     /// Name - The target name.
72     const char *Name;
73
74     /// ShortDesc - A short description of the target.
75     const char *ShortDesc;
76
77     /// TargetMachineCtorFn - Construction function for this target's
78     /// TargetMachine, if registered.
79     TargetMachineCtorTy TargetMachineCtorFn;
80
81     /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
82     /// if registered.
83     AsmPrinterCtorTy AsmPrinterCtorFn;
84
85   public:
86     /// getName - Get the target name.
87     const char *getName() const { return Name; }
88
89     /// getShortDescription - Get a short description of the target.
90     const char *getShortDescription() const { return ShortDesc; }
91
92     /// createTargetMachine - Create a target specific machine implementation.
93     TargetMachine *createTargetMachine(const Module &M,
94                                        const std::string &Features) const {
95       if (!TargetMachineCtorFn)
96         return 0;
97       return TargetMachineCtorFn(M, Features);
98     }
99
100     /// createAsmPrinter - Create a target specific assembly printer pass.
101     FunctionPass *createAsmPrinter(formatted_raw_ostream &OS,
102                                    TargetMachine &M,
103                                    bool Verbose) const {
104       if (!AsmPrinterCtorFn)
105         return 0;
106       return AsmPrinterCtorFn(OS, M, Verbose);
107     }
108   };
109
110   /// TargetRegistry - Generic interface to target specific features.
111   //
112   // FIXME: Provide Target* iterator.
113   struct TargetRegistry {
114     /// @name Registry Access
115     /// @{
116
117     /// getClosestStaticTargetForTriple - Given a target triple, pick the most
118     /// capable target for that triple.
119     static const Target *getClosestStaticTargetForTriple(const std::string &TT,
120                                                          std::string &Error);
121
122     /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
123     /// target that is compatible with the module.  If no close target can be
124     /// found, this returns null and sets the Error string to a reason.
125     static const Target *getClosestStaticTargetForModule(const Module &M,
126                                                         std::string &Error);
127
128     /// getClosestTargetForJIT - Pick the best target that is compatible with
129     /// the current host.  If no close target can be found, this returns null
130     /// and sets the Error string to a reason.
131     //
132     // FIXME: Do we still need this interface, clients can always look for the
133     // match for the host triple.
134     static const Target *getClosestTargetForJIT(std::string &Error);
135
136     /// @}
137     /// @name Target Registration
138     /// @{
139
140     /// RegisterTarget - Register the given target. Attempts to register a
141     /// target which has already been registered will be ignored.
142     /// 
143     /// Clients are responsible for ensuring that registration doesn't occur
144     /// while another thread is attempting to access the registry. Typically
145     /// this is done by initializing all targets at program startup.
146     ///
147     /// @param T - The target being registered.
148     /// @param Name - The target name. This should be a static string.
149     /// @param ShortDesc - A short target description. This should be a static
150     /// string. 
151     /// @param TQualityFn - The triple match quality computation function for
152     /// this target.
153     /// @param MQualityFn - The module match quality computation function for
154     /// this target.
155     /// @param JITMatchQualityFn - The JIT match quality computation function
156     /// for this target.
157     static void RegisterTarget(Target &T,
158                                const char *Name,
159                                const char *ShortDesc,
160                                Target::TripleMatchQualityFnTy TQualityFn,
161                                Target::ModuleMatchQualityFnTy MQualityFn,
162                                Target::JITMatchQualityFnTy JITQualityFn);
163                                
164     /// RegisterTargetMachine - Register a TargetMachine implementation for the
165     /// given target.
166     /// 
167     /// Clients are responsible for ensuring that registration doesn't occur
168     /// while another thread is attempting to access the registry. Typically
169     /// this is done by initializing all targets at program startup.
170     /// 
171     /// @param T - The target being registered.
172     /// @param Fn - A function to construct a TargetMachine for the target.
173     static void RegisterTargetMachine(Target &T, 
174                                       Target::TargetMachineCtorTy Fn) {
175       assert(!T.TargetMachineCtorFn && "Constructor already registered!");
176       T.TargetMachineCtorFn = Fn;
177     }
178
179     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
180     /// target.
181     /// 
182     /// Clients are responsible for ensuring that registration doesn't occur
183     /// while another thread is attempting to access the registry. Typically
184     /// this is done by initializing all targets at program startup.
185     ///
186     /// @param T - The target being registered.
187     /// @param Fn - A function to construct an AsmPrinter for the target.
188     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
189       assert(!T.AsmPrinterCtorFn && "Constructor already registered!");
190       T.AsmPrinterCtorFn = Fn;
191     }
192
193     /// @}
194   };
195
196 }
197
198 #endif