Eliminate some uses of immAllOnes, just use -1, it does
[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 Module;
29   class MCAsmInfo;
30   class MCAsmParser;
31   class MCCodeEmitter;
32   class MCContext;
33   class MCDisassembler;
34   class MCInstPrinter;
35   class MCStreamer;
36   class TargetAsmLexer;
37   class TargetAsmParser;
38   class TargetMachine;
39   class formatted_raw_ostream;
40   class raw_ostream;
41
42   /// Target - Wrapper for Target specific information.
43   ///
44   /// For registration purposes, this is a POD type so that targets can be
45   /// registered without the use of static constructors.
46   ///
47   /// Targets should implement a single global instance of this class (which
48   /// will be zero initialized), and pass that instance to the TargetRegistry as
49   /// part of their initialization.
50   class Target {
51   public:
52     friend struct TargetRegistry;
53
54     typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
55
56     typedef const MCAsmInfo *(*AsmInfoCtorFnTy)(const Target &T,
57                                                 StringRef TT);
58     typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
59                                                   const std::string &TT,
60                                                   const std::string &Features);
61     typedef AsmPrinter *(*AsmPrinterCtorTy)(formatted_raw_ostream &OS,
62                                             TargetMachine &TM,
63                                             MCContext &Ctx,
64                                             MCStreamer &Streamer,
65                                             const MCAsmInfo *MAI);
66     typedef TargetAsmLexer *(*AsmLexerCtorTy)(const Target &T,
67                                               const MCAsmInfo &MAI);
68     typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &T,MCAsmParser &P);
69     typedef const MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T);
70     typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
71                                                   unsigned SyntaxVariant,
72                                                   const MCAsmInfo &MAI,
73                                                   raw_ostream &O);
74     typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const Target &T,
75                                                 TargetMachine &TM,
76                                                 MCContext &Ctx);
77
78   private:
79     /// Next - The next registered target in the linked list, maintained by the
80     /// TargetRegistry.
81     Target *Next;
82
83     /// TripleMatchQualityFn - The target function for rating the match quality
84     /// of a triple.
85     TripleMatchQualityFnTy TripleMatchQualityFn;
86
87     /// Name - The target name.
88     const char *Name;
89
90     /// ShortDesc - A short description of the target.
91     const char *ShortDesc;
92
93     /// HasJIT - Whether this target supports the JIT.
94     bool HasJIT;
95
96     AsmInfoCtorFnTy AsmInfoCtorFn;
97     
98     /// TargetMachineCtorFn - Construction function for this target's
99     /// TargetMachine, if registered.
100     TargetMachineCtorTy TargetMachineCtorFn;
101
102     /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
103     /// if registered.
104     AsmPrinterCtorTy AsmPrinterCtorFn;
105
106     /// AsmLexerCtorFn - Construction function for this target's TargetAsmLexer,
107     /// if registered.
108     AsmLexerCtorTy AsmLexerCtorFn;
109     
110     /// AsmParserCtorFn - Construction function for this target's
111     /// TargetAsmParser, if registered.
112     AsmParserCtorTy AsmParserCtorFn;
113     
114     /// MCDisassemblerCtorFn - Construction function for this target's
115     /// MCDisassembler, if registered.
116     MCDisassemblerCtorTy MCDisassemblerCtorFn;
117
118     
119     /// MCInstPrinterCtorFn - Construction function for this target's 
120     /// MCInstPrinter, if registered.
121     MCInstPrinterCtorTy MCInstPrinterCtorFn;
122     
123     /// CodeEmitterCtorFn - Construction function for this target's CodeEmitter,
124     /// if registered.
125     CodeEmitterCtorTy CodeEmitterCtorFn;
126
127   public:
128     /// @name Target Information
129     /// @{
130
131     // getNext - Return the next registered target.
132     const Target *getNext() const { return Next; }
133
134     /// getName - Get the target name.
135     const char *getName() const { return Name; }
136
137     /// getShortDescription - Get a short description of the target.
138     const char *getShortDescription() const { return ShortDesc; }
139
140     /// @}
141     /// @name Feature Predicates
142     /// @{
143
144     /// hasJIT - Check if this targets supports the just-in-time compilation.
145     bool hasJIT() const { return HasJIT; }
146
147     /// hasTargetMachine - Check if this target supports code generation.
148     bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
149
150     /// hasAsmPrinter - Check if this target supports .s printing.
151     bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
152
153     /// hasAsmParser - Check if this target supports .s parsing.
154     bool hasAsmParser() const { return AsmParserCtorFn != 0; }
155     
156     /// hasMCDisassembler - Check if this target has a disassembler.
157     bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
158
159     /// hasMCInstPrinter - Check if this target has an instruction printer.
160     bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
161
162     /// hasCodeEmitter - Check if this target supports instruction encoding.
163     bool hasCodeEmitter() const { return CodeEmitterCtorFn != 0; }
164
165     /// @}
166     /// @name Feature Constructors
167     /// @{
168     
169     /// createAsmInfo - Create a MCAsmInfo implementation for the specified
170     /// target triple.
171     ///
172     /// \arg Triple - This argument is used to determine the target machine
173     /// feature set; it should always be provided. Generally this should be
174     /// either the target triple from the module, or the target triple of the
175     /// host if that does not exist.
176     const MCAsmInfo *createAsmInfo(StringRef Triple) const {
177       if (!AsmInfoCtorFn)
178         return 0;
179       return AsmInfoCtorFn(*this, Triple);
180     }
181     
182     /// createTargetMachine - Create a target specific machine implementation
183     /// for the specified \arg Triple.
184     ///
185     /// \arg Triple - This argument is used to determine the target machine
186     /// feature set; it should always be provided. Generally this should be
187     /// either the target triple from the module, or the target triple of the
188     /// host if that does not exist.
189     TargetMachine *createTargetMachine(const std::string &Triple,
190                                        const std::string &Features) const {
191       if (!TargetMachineCtorFn)
192         return 0;
193       return TargetMachineCtorFn(*this, Triple, Features);
194     }
195
196     /// createAsmPrinter - Create a target specific assembly printer pass.  This
197     /// takes ownership of the MCContext and MCStreamer objects but not the MAI.
198     AsmPrinter *createAsmPrinter(formatted_raw_ostream &OS, TargetMachine &TM,
199                                  MCContext &Ctx, MCStreamer &Streamer,
200                                  const MCAsmInfo *MAI) const {
201       if (!AsmPrinterCtorFn)
202         return 0;
203       return AsmPrinterCtorFn(OS, TM, Ctx, Streamer, MAI);
204     }
205
206     /// createAsmLexer - Create a target specific assembly lexer.
207     ///
208     TargetAsmLexer *createAsmLexer(const MCAsmInfo &MAI) const {
209       if (!AsmLexerCtorFn)
210         return 0;
211       return AsmLexerCtorFn(*this, MAI);
212     }
213     
214     /// createAsmParser - Create a target specific assembly parser.
215     ///
216     /// \arg Parser - The target independent parser implementation to use for
217     /// parsing and lexing.
218     TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
219       if (!AsmParserCtorFn)
220         return 0;
221       return AsmParserCtorFn(*this, Parser);
222     }
223     
224     const MCDisassembler *createMCDisassembler() const {
225       if (!MCDisassemblerCtorFn)
226         return 0;
227       return MCDisassemblerCtorFn(*this);
228     }
229
230     MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
231                                        const MCAsmInfo &MAI,
232                                        raw_ostream &O) const {
233       if (!MCInstPrinterCtorFn)
234         return 0;
235       return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI, O);
236     }
237     
238     
239     /// createCodeEmitter - Create a target specific code emitter.
240     MCCodeEmitter *createCodeEmitter(TargetMachine &TM, MCContext &Ctx) const {
241       if (!CodeEmitterCtorFn)
242         return 0;
243       return CodeEmitterCtorFn(*this, TM, Ctx);
244     }
245
246     /// @}
247   };
248
249   /// TargetRegistry - Generic interface to target specific features.
250   struct TargetRegistry {
251     class iterator {
252       const Target *Current;
253       explicit iterator(Target *T) : Current(T) {}
254       friend struct TargetRegistry;
255     public:
256       iterator(const iterator &I) : Current(I.Current) {}
257       iterator() : Current(0) {}
258
259       bool operator==(const iterator &x) const {
260         return Current == x.Current;
261       }
262       bool operator!=(const iterator &x) const {
263         return !operator==(x);
264       }
265
266       // Iterator traversal: forward iteration only
267       iterator &operator++() {          // Preincrement
268         assert(Current && "Cannot increment end iterator!");
269         Current = Current->getNext();
270         return *this;
271       }
272       iterator operator++(int) {        // Postincrement
273         iterator tmp = *this; 
274         ++*this; 
275         return tmp;
276       }
277
278       const Target &operator*() const {
279         assert(Current && "Cannot dereference end iterator!");
280         return *Current;
281       }
282
283       const Target *operator->() const {
284         return &operator*();
285       }
286     };
287
288     /// @name Registry Access
289     /// @{
290
291     static iterator begin();
292
293     static iterator end() { return iterator(); }
294
295     /// lookupTarget - Lookup a target based on a target triple.
296     ///
297     /// \param Triple - The triple to use for finding a target.
298     /// \param Error - On failure, an error string describing why no target was
299     /// found.
300     static const Target *lookupTarget(const std::string &Triple,
301                                       std::string &Error);
302
303     /// getClosestTargetForJIT - Pick the best target that is compatible with
304     /// the current host.  If no close target can be found, this returns null
305     /// and sets the Error string to a reason.
306     ///
307     /// Maintained for compatibility through 2.6.
308     static const Target *getClosestTargetForJIT(std::string &Error);
309
310     /// @}
311     /// @name Target Registration
312     /// @{
313
314     /// RegisterTarget - Register the given target. Attempts to register a
315     /// target which has already been registered will be ignored.
316     /// 
317     /// Clients are responsible for ensuring that registration doesn't occur
318     /// while another thread is attempting to access the registry. Typically
319     /// this is done by initializing all targets at program startup.
320     ///
321     /// @param T - The target being registered.
322     /// @param Name - The target name. This should be a static string.
323     /// @param ShortDesc - A short target description. This should be a static
324     /// string. 
325     /// @param TQualityFn - The triple match quality computation function for
326     /// this target.
327     /// @param HasJIT - Whether the target supports JIT code
328     /// generation.
329     static void RegisterTarget(Target &T,
330                                const char *Name,
331                                const char *ShortDesc,
332                                Target::TripleMatchQualityFnTy TQualityFn,
333                                bool HasJIT = false);
334
335     /// RegisterAsmInfo - Register a MCAsmInfo implementation for the
336     /// given target.
337     /// 
338     /// Clients are responsible for ensuring that registration doesn't occur
339     /// while another thread is attempting to access the registry. Typically
340     /// this is done by initializing all targets at program startup.
341     /// 
342     /// @param T - The target being registered.
343     /// @param Fn - A function to construct a MCAsmInfo for the target.
344     static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
345       // Ignore duplicate registration.
346       if (!T.AsmInfoCtorFn)
347         T.AsmInfoCtorFn = Fn;
348     }
349     
350     /// RegisterTargetMachine - Register a TargetMachine implementation for the
351     /// given target.
352     /// 
353     /// Clients are responsible for ensuring that registration doesn't occur
354     /// while another thread is attempting to access the registry. Typically
355     /// this is done by initializing all targets at program startup.
356     /// 
357     /// @param T - The target being registered.
358     /// @param Fn - A function to construct a TargetMachine for the target.
359     static void RegisterTargetMachine(Target &T, 
360                                       Target::TargetMachineCtorTy Fn) {
361       // Ignore duplicate registration.
362       if (!T.TargetMachineCtorFn)
363         T.TargetMachineCtorFn = Fn;
364     }
365
366     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
367     /// target.
368     /// 
369     /// Clients are responsible for ensuring that registration doesn't occur
370     /// while another thread is attempting to access the registry. Typically
371     /// this is done by initializing all targets at program startup.
372     ///
373     /// @param T - The target being registered.
374     /// @param Fn - A function to construct an AsmPrinter for the target.
375     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
376       // Ignore duplicate registration.
377       if (!T.AsmPrinterCtorFn)
378         T.AsmPrinterCtorFn = Fn;
379     }
380
381     /// RegisterAsmLexer - Register a TargetAsmLexer implementation for the
382     /// given target.
383     /// 
384     /// Clients are responsible for ensuring that registration doesn't occur
385     /// while another thread is attempting to access the registry. Typically
386     /// this is done by initializing all targets at program startup.
387     ///
388     /// @param T - The target being registered.
389     /// @param Fn - A function to construct an AsmPrinter for the target.
390     static void RegisterAsmLexer(Target &T, Target::AsmLexerCtorTy Fn) {
391       if (!T.AsmLexerCtorFn)
392         T.AsmLexerCtorFn = Fn;
393     }
394     
395     /// RegisterAsmParser - Register a TargetAsmParser implementation for the
396     /// given target.
397     /// 
398     /// Clients are responsible for ensuring that registration doesn't occur
399     /// while another thread is attempting to access the registry. Typically
400     /// this is done by initializing all targets at program startup.
401     ///
402     /// @param T - The target being registered.
403     /// @param Fn - A function to construct an AsmPrinter for the target.
404     static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
405       if (!T.AsmParserCtorFn)
406         T.AsmParserCtorFn = Fn;
407     }
408     
409     /// RegisterMCDisassembler - Register a MCDisassembler implementation for
410     /// the given target.
411     /// 
412     /// Clients are responsible for ensuring that registration doesn't occur
413     /// while another thread is attempting to access the registry. Typically
414     /// this is done by initializing all targets at program startup.
415     ///
416     /// @param T - The target being registered.
417     /// @param Fn - A function to construct an MCDisassembler for the target.
418     static void RegisterMCDisassembler(Target &T, 
419                                        Target::MCDisassemblerCtorTy Fn) {
420       if (!T.MCDisassemblerCtorFn)
421         T.MCDisassemblerCtorFn = Fn;
422     }
423
424     /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
425     /// given target.
426     /// 
427     /// Clients are responsible for ensuring that registration doesn't occur
428     /// while another thread is attempting to access the registry. Typically
429     /// this is done by initializing all targets at program startup.
430     ///
431     /// @param T - The target being registered.
432     /// @param Fn - A function to construct an MCInstPrinter for the target.
433     static void RegisterMCInstPrinter(Target &T,
434                                       Target::MCInstPrinterCtorTy Fn) {
435       if (!T.MCInstPrinterCtorFn)
436         T.MCInstPrinterCtorFn = Fn;
437     }
438     
439     /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
440     /// given target.
441     ///
442     /// Clients are responsible for ensuring that registration doesn't occur
443     /// while another thread is attempting to access the registry. Typically
444     /// this is done by initializing all targets at program startup.
445     ///
446     /// @param T - The target being registered.
447     /// @param Fn - A function to construct an AsmPrinter for the target.
448     static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
449       if (!T.CodeEmitterCtorFn)
450         T.CodeEmitterCtorFn = Fn;
451     }
452
453     /// @}
454   };
455
456
457   //===--------------------------------------------------------------------===//
458
459   /// RegisterTarget - Helper template for registering a target, for use in the
460   /// target's initialization function. Usage:
461   ///
462   ///
463   /// Target TheFooTarget; // The global target instance.
464   ///
465   /// extern "C" void LLVMInitializeFooTargetInfo() {
466   ///   RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
467   /// }
468   template<Triple::ArchType TargetArchType = Triple::InvalidArch,
469            bool HasJIT = false>
470   struct RegisterTarget {
471     RegisterTarget(Target &T, const char *Name, const char *Desc) {
472       TargetRegistry::RegisterTarget(T, Name, Desc,
473                                      &getTripleMatchQuality,
474                                      HasJIT);
475     }
476
477     static unsigned getTripleMatchQuality(const std::string &TT) {
478       if (Triple(TT).getArch() == TargetArchType)
479         return 20;
480       return 0;
481     }
482   };
483
484   /// RegisterAsmInfo - Helper template for registering a target assembly info
485   /// implementation.  This invokes the static "Create" method on the class to
486   /// actually do the construction.  Usage:
487   ///
488   /// extern "C" void LLVMInitializeFooTarget() {
489   ///   extern Target TheFooTarget;
490   ///   RegisterAsmInfo<FooMCAsmInfo> X(TheFooTarget);
491   /// }
492   template<class MCAsmInfoImpl>
493   struct RegisterAsmInfo {
494     RegisterAsmInfo(Target &T) {
495       TargetRegistry::RegisterAsmInfo(T, &Allocator);
496     }
497   private:
498     static const MCAsmInfo *Allocator(const Target &T, StringRef TT) {
499       return new MCAsmInfoImpl(T, TT);
500     }
501     
502   };
503
504   /// RegisterAsmInfoFn - Helper template for registering a target assembly info
505   /// implementation.  This invokes the specified function to do the
506   /// construction.  Usage:
507   ///
508   /// extern "C" void LLVMInitializeFooTarget() {
509   ///   extern Target TheFooTarget;
510   ///   RegisterAsmInfoFn X(TheFooTarget, TheFunction);
511   /// }
512   struct RegisterAsmInfoFn {
513     RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
514       TargetRegistry::RegisterAsmInfo(T, Fn);
515     }
516   };
517
518
519   /// RegisterTargetMachine - Helper template for registering a target machine
520   /// implementation, for use in the target machine initialization
521   /// function. Usage:
522   ///
523   /// extern "C" void LLVMInitializeFooTarget() {
524   ///   extern Target TheFooTarget;
525   ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
526   /// }
527   template<class TargetMachineImpl>
528   struct RegisterTargetMachine {
529     RegisterTargetMachine(Target &T) {
530       TargetRegistry::RegisterTargetMachine(T, &Allocator);
531     }
532
533   private:
534     static TargetMachine *Allocator(const Target &T, const std::string &TT,
535                                     const std::string &FS) {
536       return new TargetMachineImpl(T, TT, FS);
537     }
538   };
539
540   /// RegisterAsmPrinter - Helper template for registering a target specific
541   /// assembly printer, for use in the target machine initialization
542   /// function. Usage:
543   ///
544   /// extern "C" void LLVMInitializeFooAsmPrinter() {
545   ///   extern Target TheFooTarget;
546   ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
547   /// }
548   template<class AsmPrinterImpl>
549   struct RegisterAsmPrinter {
550     RegisterAsmPrinter(Target &T) {
551       TargetRegistry::RegisterAsmPrinter(T, &Allocator);
552     }
553
554   private:
555     static AsmPrinter *Allocator(formatted_raw_ostream &OS, TargetMachine &TM,
556                                  MCContext &Ctx, MCStreamer &Streamer,
557                                  const MCAsmInfo *MAI) {
558       return new AsmPrinterImpl(OS, TM, Ctx, Streamer, MAI);
559     }
560   };
561
562   /// RegisterAsmLexer - Helper template for registering a target specific
563   /// assembly lexer, for use in the target machine initialization
564   /// function. Usage:
565   ///
566   /// extern "C" void LLVMInitializeFooAsmLexer() {
567   ///   extern Target TheFooTarget;
568   ///   RegisterAsmLexer<FooAsmLexer> X(TheFooTarget);
569   /// }
570   template<class AsmLexerImpl>
571   struct RegisterAsmLexer {
572     RegisterAsmLexer(Target &T) {
573       TargetRegistry::RegisterAsmLexer(T, &Allocator);
574     }
575     
576   private:
577     static TargetAsmLexer *Allocator(const Target &T, const MCAsmInfo &MAI) {
578       return new AsmLexerImpl(T, MAI);
579     }
580   };
581
582   /// RegisterAsmParser - Helper template for registering a target specific
583   /// assembly parser, for use in the target machine initialization
584   /// function. Usage:
585   ///
586   /// extern "C" void LLVMInitializeFooAsmParser() {
587   ///   extern Target TheFooTarget;
588   ///   RegisterAsmParser<FooAsmParser> X(TheFooTarget);
589   /// }
590   template<class AsmParserImpl>
591   struct RegisterAsmParser {
592     RegisterAsmParser(Target &T) {
593       TargetRegistry::RegisterAsmParser(T, &Allocator);
594     }
595
596   private:
597     static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
598       return new AsmParserImpl(T, P);
599     }
600   };
601
602   /// RegisterCodeEmitter - Helper template for registering a target specific
603   /// machine code emitter, for use in the target initialization
604   /// function. Usage:
605   ///
606   /// extern "C" void LLVMInitializeFooCodeEmitter() {
607   ///   extern Target TheFooTarget;
608   ///   RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
609   /// }
610   template<class CodeEmitterImpl>
611   struct RegisterCodeEmitter {
612     RegisterCodeEmitter(Target &T) {
613       TargetRegistry::RegisterCodeEmitter(T, &Allocator);
614     }
615
616   private:
617     static MCCodeEmitter *Allocator(const Target &T, TargetMachine &TM,
618                                     MCContext &Ctx) {
619       return new CodeEmitterImpl(T, TM, Ctx);
620     }
621   };
622
623 }
624
625 #endif