bfb23ef930f7364e1dbc984fb715ff1539575741
[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 FunctionPass;
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 FunctionPass *(*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     FunctionPass *createAsmPrinter(formatted_raw_ostream &OS,
143                                    TargetMachine &TM,
144                                    const TargetAsmInfo *TAI,
145                                    bool Verbose) const {
146       if (!AsmPrinterCtorFn)
147         return 0;
148       return AsmPrinterCtorFn(OS, TM, TAI, Verbose);
149     }
150
151     /// createAsmParser - Create a target specific assembly parser.
152     ///
153     /// \arg Parser - The target independent parser implementation to use for
154     /// parsing and lexing.
155     TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
156       if (!AsmParserCtorFn)
157         return 0;
158       return AsmParserCtorFn(*this, Parser);
159     }
160   };
161
162   /// TargetRegistry - Generic interface to target specific features.
163   struct TargetRegistry {
164     class iterator {
165       const Target *Current;
166       explicit iterator(Target *T) : Current(T) {}
167       friend struct TargetRegistry;
168     public:
169       iterator(const iterator &I) : Current(I.Current) {}
170       iterator() : Current(0) {}
171
172       bool operator==(const iterator &x) const {
173         return Current == x.Current;
174       }
175       bool operator!=(const iterator &x) const {
176         return !operator==(x);
177       }
178
179       // Iterator traversal: forward iteration only
180       iterator &operator++() {          // Preincrement
181         assert(Current && "Cannot increment end iterator!");
182         Current = Current->getNext();
183         return *this;
184       }
185       iterator operator++(int) {        // Postincrement
186         iterator tmp = *this; 
187         ++*this; 
188         return tmp;
189       }
190
191       const Target &operator*() const {
192         assert(Current && "Cannot dereference end iterator!");
193         return *Current;
194       }
195
196       const Target *operator->() const {
197         return &operator*();
198       }
199     };
200
201     /// @name Registry Access
202     /// @{
203
204     static iterator begin();
205
206     static iterator end() { return iterator(); }
207
208     /// lookupTarget - Lookup a target based on a target triple.
209     ///
210     /// \param Triple - The triple to use for finding a target.
211     /// \param Error - On failure, an error string describing why no target was
212     /// found.
213     static const Target *lookupTarget(const std::string &Triple,
214                                       std::string &Error);
215
216     /// getClosestTargetForJIT - Pick the best target that is compatible with
217     /// the current host.  If no close target can be found, this returns null
218     /// and sets the Error string to a reason.
219     ///
220     /// Maintained for compatibility through 2.6.
221     static const Target *getClosestTargetForJIT(std::string &Error);
222
223     /// @}
224     /// @name Target Registration
225     /// @{
226
227     /// RegisterTarget - Register the given target. Attempts to register a
228     /// target which has already been registered will be ignored.
229     /// 
230     /// Clients are responsible for ensuring that registration doesn't occur
231     /// while another thread is attempting to access the registry. Typically
232     /// this is done by initializing all targets at program startup.
233     ///
234     /// @param T - The target being registered.
235     /// @param Name - The target name. This should be a static string.
236     /// @param ShortDesc - A short target description. This should be a static
237     /// string. 
238     /// @param TQualityFn - The triple match quality computation function for
239     /// this target.
240     /// @param HasJIT - Whether the target supports JIT code
241     /// generation.
242     static void RegisterTarget(Target &T,
243                                const char *Name,
244                                const char *ShortDesc,
245                                Target::TripleMatchQualityFnTy TQualityFn,
246                                bool HasJIT = false);
247
248     /// RegisterAsmInfo - Register a TargetAsmInfo implementation for the
249     /// given target.
250     /// 
251     /// Clients are responsible for ensuring that registration doesn't occur
252     /// while another thread is attempting to access the registry. Typically
253     /// this is done by initializing all targets at program startup.
254     /// 
255     /// @param T - The target being registered.
256     /// @param Fn - A function to construct a TargetAsmInfo for the target.
257     static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
258       // Ignore duplicate registration.
259       if (!T.AsmInfoCtorFn)
260         T.AsmInfoCtorFn = Fn;
261     }
262     
263     /// RegisterTargetMachine - Register a TargetMachine implementation for the
264     /// given target.
265     /// 
266     /// Clients are responsible for ensuring that registration doesn't occur
267     /// while another thread is attempting to access the registry. Typically
268     /// this is done by initializing all targets at program startup.
269     /// 
270     /// @param T - The target being registered.
271     /// @param Fn - A function to construct a TargetMachine for the target.
272     static void RegisterTargetMachine(Target &T, 
273                                       Target::TargetMachineCtorTy Fn) {
274       // Ignore duplicate registration.
275       if (!T.TargetMachineCtorFn)
276         T.TargetMachineCtorFn = Fn;
277     }
278
279     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
280     /// target.
281     /// 
282     /// Clients are responsible for ensuring that registration doesn't occur
283     /// while another thread is attempting to access the registry. Typically
284     /// this is done by initializing all targets at program startup.
285     ///
286     /// @param T - The target being registered.
287     /// @param Fn - A function to construct an AsmPrinter for the target.
288     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
289       // Ignore duplicate registration.
290       if (!T.AsmPrinterCtorFn)
291         T.AsmPrinterCtorFn = Fn;
292     }
293
294     /// RegisterAsmParser - Register a TargetAsmParser implementation for the
295     /// given target.
296     /// 
297     /// Clients are responsible for ensuring that registration doesn't occur
298     /// while another thread is attempting to access the registry. Typically
299     /// this is done by initializing all targets at program startup.
300     ///
301     /// @param T - The target being registered.
302     /// @param Fn - A function to construct an AsmPrinter for the target.
303     static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
304       if (!T.AsmParserCtorFn)
305         T.AsmParserCtorFn = Fn;
306     }
307
308     /// @}
309   };
310
311
312   //===--------------------------------------------------------------------===//
313
314   /// RegisterTarget - Helper template for registering a target, for use in the
315   /// target's initialization function. Usage:
316   ///
317   ///
318   /// Target TheFooTarget; // The global target instance.
319   ///
320   /// extern "C" void LLVMInitializeFooTargetInfo() {
321   ///   RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
322   /// }
323   template<Triple::ArchType TargetArchType = Triple::InvalidArch,
324            bool HasJIT = false>
325   struct RegisterTarget {
326     RegisterTarget(Target &T, const char *Name, const char *Desc) {
327       TargetRegistry::RegisterTarget(T, Name, Desc,
328                                      &getTripleMatchQuality,
329                                      HasJIT);
330     }
331
332     static unsigned getTripleMatchQuality(const std::string &TT) {
333       if (Triple(TT.c_str()).getArch() == TargetArchType)
334         return 20;
335       return 0;
336     }
337   };
338
339   /// RegisterAsmInfo - Helper template for registering a target assembly info
340   /// implementation.  This invokes the static "Create" method on the class to
341   /// actually do the construction.  Usage:
342   ///
343   /// extern "C" void LLVMInitializeFooTarget() {
344   ///   extern Target TheFooTarget;
345   ///   RegisterAsmInfo<FooTargetAsmInfo> X(TheFooTarget);
346   /// }
347   template<class TargetAsmInfoImpl>
348   struct RegisterAsmInfo {
349     RegisterAsmInfo(Target &T) {
350       TargetRegistry::RegisterAsmInfo(T, &Allocator);
351     }
352   private:
353     static const TargetAsmInfo *Allocator(const Target &T, const StringRef &TT){
354       return new TargetAsmInfoImpl(T, TT);
355     }
356     
357   };
358
359   /// RegisterAsmInfoFn - Helper template for registering a target assembly info
360   /// implementation.  This invokes the specified function to do the
361   /// construction.  Usage:
362   ///
363   /// extern "C" void LLVMInitializeFooTarget() {
364   ///   extern Target TheFooTarget;
365   ///   RegisterAsmInfoFn X(TheFooTarget, TheFunction);
366   /// }
367   struct RegisterAsmInfoFn {
368     RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
369       TargetRegistry::RegisterAsmInfo(T, Fn);
370     }
371   };
372
373
374   /// RegisterTargetMachine - Helper template for registering a target machine
375   /// implementation, for use in the target machine initialization
376   /// function. Usage:
377   ///
378   /// extern "C" void LLVMInitializeFooTarget() {
379   ///   extern Target TheFooTarget;
380   ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
381   /// }
382   template<class TargetMachineImpl>
383   struct RegisterTargetMachine {
384     RegisterTargetMachine(Target &T) {
385       TargetRegistry::RegisterTargetMachine(T, &Allocator);
386     }
387
388   private:
389     static TargetMachine *Allocator(const Target &T, const std::string &TT,
390                                     const std::string &FS) {
391       return new TargetMachineImpl(T, TT, FS);
392     }
393   };
394
395   /// RegisterAsmPrinter - Helper template for registering a target specific
396   /// assembly printer, for use in the target machine initialization
397   /// function. Usage:
398   ///
399   /// extern "C" void LLVMInitializeFooAsmPrinter() {
400   ///   extern Target TheFooTarget;
401   ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
402   /// }
403   template<class AsmPrinterImpl>
404   struct RegisterAsmPrinter {
405     RegisterAsmPrinter(Target &T) {
406       TargetRegistry::RegisterAsmPrinter(T, &Allocator);
407     }
408
409   private:
410     static FunctionPass *Allocator(formatted_raw_ostream &OS,
411                                    TargetMachine &TM,
412                                    const TargetAsmInfo *TAI,
413                                    bool Verbose) {
414       return new AsmPrinterImpl(OS, TM, TAI, Verbose);
415     }
416   };
417
418   /// RegisterAsmParser - Helper template for registering a target specific
419   /// assembly parser, for use in the target machine initialization
420   /// function. Usage:
421   ///
422   /// extern "C" void LLVMInitializeFooAsmParser() {
423   ///   extern Target TheFooTarget;
424   ///   RegisterAsmParser<FooAsmParser> X(TheFooTarget);
425   /// }
426   template<class AsmParserImpl>
427   struct RegisterAsmParser {
428     RegisterAsmParser(Target &T) {
429       TargetRegistry::RegisterAsmParser(T, &Allocator);
430     }
431
432   private:
433     static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
434       return new AsmParserImpl(T, P);
435     }
436   };
437
438 }
439
440 #endif