Reapply r79127. It was fixed by d0k.
[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 "llvm/ADT/Triple.h"
23 #include <string>
24 #include <cassert>
25
26 namespace llvm {
27   class AsmPrinter;
28   class MCAsmParser;
29   class Module;
30   class TargetAsmInfo;
31   class TargetAsmParser;
32   class TargetMachine;
33   class formatted_raw_ostream;
34
35   /// Target - Wrapper for Target specific information.
36   ///
37   /// For registration purposes, this is a POD type so that targets can be
38   /// registered without the use of static constructors.
39   ///
40   /// Targets should implement a single global instance of this class (which
41   /// will be zero initialized), and pass that instance to the TargetRegistry as
42   /// part of their initialization.
43   class Target {
44   public:
45     friend struct TargetRegistry;
46
47     typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
48
49     typedef const TargetAsmInfo *(*AsmInfoCtorFnTy)(const Target &T,
50                                                     const StringRef &TT);
51     typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
52                                                   const std::string &TT,
53                                                   const std::string &Features);
54     typedef AsmPrinter *(*AsmPrinterCtorTy)(formatted_raw_ostream &OS,
55                                             TargetMachine &TM,
56                                             const TargetAsmInfo *TAI,
57                                             bool VerboseAsm);
58     typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &T,
59                                                 MCAsmParser &P);
60   private:
61     /// Next - The next registered target in the linked list, maintained by the
62     /// TargetRegistry.
63     Target *Next;
64
65     /// TripleMatchQualityFn - The target function for rating the match quality
66     /// of a triple.
67     TripleMatchQualityFnTy TripleMatchQualityFn;
68
69     /// Name - The target name.
70     const char *Name;
71
72     /// ShortDesc - A short description of the target.
73     const char *ShortDesc;
74
75     /// HasJIT - Whether this target supports the JIT.
76     bool HasJIT;
77
78     AsmInfoCtorFnTy AsmInfoCtorFn;
79     
80     /// TargetMachineCtorFn - Construction function for this target's
81     /// TargetMachine, if registered.
82     TargetMachineCtorTy TargetMachineCtorFn;
83
84     /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
85     /// if registered.
86     AsmPrinterCtorTy AsmPrinterCtorFn;
87
88     /// AsmParserCtorFn - Construction function for this target's AsmParser,
89     /// if registered.
90     AsmParserCtorTy AsmParserCtorFn;
91
92   public:
93     // getNext - Return the next registered target.
94     const Target *getNext() const { return Next; }
95
96     /// getName - Get the target name.
97     const char *getName() const { return Name; }
98
99     /// getShortDescription - Get a short description of the target.
100     const char *getShortDescription() const { return ShortDesc; }
101
102     bool hasJIT() const { return HasJIT; }
103
104     /// hasTargetMachine - Check if this target supports code generation.
105     bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
106
107     /// hasAsmPrinter - Check if this target supports .s printing.
108     bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
109
110     /// hasAsmParser - Check if this target supports .s parsing.
111     bool hasAsmParser() const { return AsmParserCtorFn != 0; }
112
113     
114     /// createAsmInfo - Create a TargetAsmInfo implementation for the specified
115     /// target triple.
116     ///
117     /// \arg Triple - This argument is used to determine the target machine
118     /// feature set; it should always be provided. Generally this should be
119     /// either the target triple from the module, or the target triple of the
120     /// host if that does not exist.
121     const TargetAsmInfo *createAsmInfo(const StringRef &Triple) const {
122       if (!AsmInfoCtorFn)
123         return 0;
124       return AsmInfoCtorFn(*this, Triple);
125     }
126     
127     /// createTargetMachine - Create a target specific machine implementation
128     /// for the specified \arg Triple.
129     ///
130     /// \arg Triple - This argument is used to determine the target machine
131     /// feature set; it should always be provided. Generally this should be
132     /// either the target triple from the module, or the target triple of the
133     /// host if that does not exist.
134     TargetMachine *createTargetMachine(const std::string &Triple,
135                                        const std::string &Features) const {
136       if (!TargetMachineCtorFn)
137         return 0;
138       return TargetMachineCtorFn(*this, Triple, Features);
139     }
140
141     /// createAsmPrinter - Create a target specific assembly printer pass.
142     AsmPrinter *createAsmPrinter(formatted_raw_ostream &OS, TargetMachine &TM,
143                                  const TargetAsmInfo *TAI, bool Verbose) const {
144       if (!AsmPrinterCtorFn)
145         return 0;
146       return AsmPrinterCtorFn(OS, TM, TAI, Verbose);
147     }
148
149     /// createAsmParser - Create a target specific assembly parser.
150     ///
151     /// \arg Parser - The target independent parser implementation to use for
152     /// parsing and lexing.
153     TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
154       if (!AsmParserCtorFn)
155         return 0;
156       return AsmParserCtorFn(*this, Parser);
157     }
158   };
159
160   /// TargetRegistry - Generic interface to target specific features.
161   struct TargetRegistry {
162     class iterator {
163       const Target *Current;
164       explicit iterator(Target *T) : Current(T) {}
165       friend struct TargetRegistry;
166     public:
167       iterator(const iterator &I) : Current(I.Current) {}
168       iterator() : Current(0) {}
169
170       bool operator==(const iterator &x) const {
171         return Current == x.Current;
172       }
173       bool operator!=(const iterator &x) const {
174         return !operator==(x);
175       }
176
177       // Iterator traversal: forward iteration only
178       iterator &operator++() {          // Preincrement
179         assert(Current && "Cannot increment end iterator!");
180         Current = Current->getNext();
181         return *this;
182       }
183       iterator operator++(int) {        // Postincrement
184         iterator tmp = *this; 
185         ++*this; 
186         return tmp;
187       }
188
189       const Target &operator*() const {
190         assert(Current && "Cannot dereference end iterator!");
191         return *Current;
192       }
193
194       const Target *operator->() const {
195         return &operator*();
196       }
197     };
198
199     /// @name Registry Access
200     /// @{
201
202     static iterator begin();
203
204     static iterator end() { return iterator(); }
205
206     /// lookupTarget - Lookup a target based on a target triple.
207     ///
208     /// \param Triple - The triple to use for finding a target.
209     /// \param Error - On failure, an error string describing why no target was
210     /// found.
211     static const Target *lookupTarget(const std::string &Triple,
212                                       std::string &Error);
213
214     /// getClosestTargetForJIT - Pick the best target that is compatible with
215     /// the current host.  If no close target can be found, this returns null
216     /// and sets the Error string to a reason.
217     ///
218     /// Maintained for compatibility through 2.6.
219     static const Target *getClosestTargetForJIT(std::string &Error);
220
221     /// @}
222     /// @name Target Registration
223     /// @{
224
225     /// RegisterTarget - Register the given target. Attempts to register a
226     /// target which has already been registered will be ignored.
227     /// 
228     /// Clients are responsible for ensuring that registration doesn't occur
229     /// while another thread is attempting to access the registry. Typically
230     /// this is done by initializing all targets at program startup.
231     ///
232     /// @param T - The target being registered.
233     /// @param Name - The target name. This should be a static string.
234     /// @param ShortDesc - A short target description. This should be a static
235     /// string. 
236     /// @param TQualityFn - The triple match quality computation function for
237     /// this target.
238     /// @param HasJIT - Whether the target supports JIT code
239     /// generation.
240     static void RegisterTarget(Target &T,
241                                const char *Name,
242                                const char *ShortDesc,
243                                Target::TripleMatchQualityFnTy TQualityFn,
244                                bool HasJIT = false);
245
246     /// RegisterAsmInfo - Register a TargetAsmInfo implementation for the
247     /// given target.
248     /// 
249     /// Clients are responsible for ensuring that registration doesn't occur
250     /// while another thread is attempting to access the registry. Typically
251     /// this is done by initializing all targets at program startup.
252     /// 
253     /// @param T - The target being registered.
254     /// @param Fn - A function to construct a TargetAsmInfo for the target.
255     static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
256       // Ignore duplicate registration.
257       if (!T.AsmInfoCtorFn)
258         T.AsmInfoCtorFn = Fn;
259     }
260     
261     /// RegisterTargetMachine - Register a TargetMachine implementation for the
262     /// given target.
263     /// 
264     /// Clients are responsible for ensuring that registration doesn't occur
265     /// while another thread is attempting to access the registry. Typically
266     /// this is done by initializing all targets at program startup.
267     /// 
268     /// @param T - The target being registered.
269     /// @param Fn - A function to construct a TargetMachine for the target.
270     static void RegisterTargetMachine(Target &T, 
271                                       Target::TargetMachineCtorTy Fn) {
272       // Ignore duplicate registration.
273       if (!T.TargetMachineCtorFn)
274         T.TargetMachineCtorFn = Fn;
275     }
276
277     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
278     /// target.
279     /// 
280     /// Clients are responsible for ensuring that registration doesn't occur
281     /// while another thread is attempting to access the registry. Typically
282     /// this is done by initializing all targets at program startup.
283     ///
284     /// @param T - The target being registered.
285     /// @param Fn - A function to construct an AsmPrinter for the target.
286     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
287       // Ignore duplicate registration.
288       if (!T.AsmPrinterCtorFn)
289         T.AsmPrinterCtorFn = Fn;
290     }
291
292     /// RegisterAsmParser - Register a TargetAsmParser implementation for the
293     /// given target.
294     /// 
295     /// Clients are responsible for ensuring that registration doesn't occur
296     /// while another thread is attempting to access the registry. Typically
297     /// this is done by initializing all targets at program startup.
298     ///
299     /// @param T - The target being registered.
300     /// @param Fn - A function to construct an AsmPrinter for the target.
301     static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
302       if (!T.AsmParserCtorFn)
303         T.AsmParserCtorFn = Fn;
304     }
305
306     /// @}
307   };
308
309
310   //===--------------------------------------------------------------------===//
311
312   /// RegisterTarget - Helper template for registering a target, for use in the
313   /// target's initialization function. Usage:
314   ///
315   ///
316   /// Target TheFooTarget; // The global target instance.
317   ///
318   /// extern "C" void LLVMInitializeFooTargetInfo() {
319   ///   RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
320   /// }
321   template<Triple::ArchType TargetArchType = Triple::InvalidArch,
322            bool HasJIT = false>
323   struct RegisterTarget {
324     RegisterTarget(Target &T, const char *Name, const char *Desc) {
325       TargetRegistry::RegisterTarget(T, Name, Desc,
326                                      &getTripleMatchQuality,
327                                      HasJIT);
328     }
329
330     static unsigned getTripleMatchQuality(const std::string &TT) {
331       if (Triple(TT.c_str()).getArch() == TargetArchType)
332         return 20;
333       return 0;
334     }
335   };
336
337   /// RegisterAsmInfo - Helper template for registering a target assembly info
338   /// implementation.  This invokes the static "Create" method on the class to
339   /// actually do the construction.  Usage:
340   ///
341   /// extern "C" void LLVMInitializeFooTarget() {
342   ///   extern Target TheFooTarget;
343   ///   RegisterAsmInfo<FooTargetAsmInfo> X(TheFooTarget);
344   /// }
345   template<class TargetAsmInfoImpl>
346   struct RegisterAsmInfo {
347     RegisterAsmInfo(Target &T) {
348       TargetRegistry::RegisterAsmInfo(T, &Allocator);
349     }
350   private:
351     static const TargetAsmInfo *Allocator(const Target &T, const StringRef &TT){
352       return new TargetAsmInfoImpl(T, TT);
353     }
354     
355   };
356
357   /// RegisterAsmInfoFn - Helper template for registering a target assembly info
358   /// implementation.  This invokes the specified function to do the
359   /// construction.  Usage:
360   ///
361   /// extern "C" void LLVMInitializeFooTarget() {
362   ///   extern Target TheFooTarget;
363   ///   RegisterAsmInfoFn X(TheFooTarget, TheFunction);
364   /// }
365   struct RegisterAsmInfoFn {
366     RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
367       TargetRegistry::RegisterAsmInfo(T, Fn);
368     }
369   };
370
371
372   /// RegisterTargetMachine - Helper template for registering a target machine
373   /// implementation, for use in the target machine initialization
374   /// function. Usage:
375   ///
376   /// extern "C" void LLVMInitializeFooTarget() {
377   ///   extern Target TheFooTarget;
378   ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
379   /// }
380   template<class TargetMachineImpl>
381   struct RegisterTargetMachine {
382     RegisterTargetMachine(Target &T) {
383       TargetRegistry::RegisterTargetMachine(T, &Allocator);
384     }
385
386   private:
387     static TargetMachine *Allocator(const Target &T, const std::string &TT,
388                                     const std::string &FS) {
389       return new TargetMachineImpl(T, TT, FS);
390     }
391   };
392
393   /// RegisterAsmPrinter - Helper template for registering a target specific
394   /// assembly printer, for use in the target machine initialization
395   /// function. Usage:
396   ///
397   /// extern "C" void LLVMInitializeFooAsmPrinter() {
398   ///   extern Target TheFooTarget;
399   ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
400   /// }
401   template<class AsmPrinterImpl>
402   struct RegisterAsmPrinter {
403     RegisterAsmPrinter(Target &T) {
404       TargetRegistry::RegisterAsmPrinter(T, &Allocator);
405     }
406
407   private:
408     static AsmPrinter *Allocator(formatted_raw_ostream &OS, TargetMachine &TM,
409                                  const TargetAsmInfo *TAI, bool Verbose) {
410       return new AsmPrinterImpl(OS, TM, TAI, Verbose);
411     }
412   };
413
414   /// RegisterAsmParser - Helper template for registering a target specific
415   /// assembly parser, for use in the target machine initialization
416   /// function. Usage:
417   ///
418   /// extern "C" void LLVMInitializeFooAsmParser() {
419   ///   extern Target TheFooTarget;
420   ///   RegisterAsmParser<FooAsmParser> X(TheFooTarget);
421   /// }
422   template<class AsmParserImpl>
423   struct RegisterAsmParser {
424     RegisterAsmParser(Target &T) {
425       TargetRegistry::RegisterAsmParser(T, &Allocator);
426     }
427
428   private:
429     static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
430       return new AsmParserImpl(T, P);
431     }
432   };
433
434 }
435
436 #endif