TargetAsmParser doesn't need reference to Target.
[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 MCAssembler;
30   class MCAsmInfo;
31   class MCAsmParser;
32   class MCCodeEmitter;
33   class MCContext;
34   class MCDisassembler;
35   class MCInstPrinter;
36   class MCInstrInfo;
37   class MCRegisterInfo;
38   class MCStreamer;
39   class TargetAsmBackend;
40   class TargetAsmLexer;
41   class TargetAsmParser;
42   class TargetMachine;
43   class raw_ostream;
44   class formatted_raw_ostream;
45
46   MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
47                                 bool isVerboseAsm,
48                                 bool useLoc, bool useCFI,
49                                 MCInstPrinter *InstPrint,
50                                 MCCodeEmitter *CE,
51                                 TargetAsmBackend *TAB,
52                                 bool ShowInst);
53
54   /// Target - Wrapper for Target specific information.
55   ///
56   /// For registration purposes, this is a POD type so that targets can be
57   /// registered without the use of static constructors.
58   ///
59   /// Targets should implement a single global instance of this class (which
60   /// will be zero initialized), and pass that instance to the TargetRegistry as
61   /// part of their initialization.
62   class Target {
63   public:
64     friend struct TargetRegistry;
65
66     typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
67
68     typedef MCAsmInfo *(*AsmInfoCtorFnTy)(const Target &T,
69                                           StringRef TT);
70     typedef MCInstrInfo *(*MCInstrInfoCtorFnTy)(void);
71     typedef MCRegisterInfo *(*MCRegInfoCtorFnTy)(void);
72     typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
73                                                   const std::string &TT,
74                                                   const std::string &CPU,
75                                                   const std::string &Features);
76     typedef AsmPrinter *(*AsmPrinterCtorTy)(TargetMachine &TM,
77                                             MCStreamer &Streamer);
78     typedef TargetAsmBackend *(*AsmBackendCtorTy)(const Target &T,
79                                                   const std::string &TT);
80     typedef TargetAsmLexer *(*AsmLexerCtorTy)(const Target &T,
81                                               const MCAsmInfo &MAI);
82     typedef TargetAsmParser *(*AsmParserCtorTy)(StringRef TT,
83                                                 StringRef CPU, StringRef Features,
84                                                 MCAsmParser &P);
85     typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T);
86     typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
87                                                   unsigned SyntaxVariant,
88                                                   const MCAsmInfo &MAI);
89     typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const Target &T,
90                                                 TargetMachine &TM,
91                                                 MCContext &Ctx);
92     typedef MCStreamer *(*ObjectStreamerCtorTy)(const Target &T,
93                                                 const std::string &TT,
94                                                 MCContext &Ctx,
95                                                 TargetAsmBackend &TAB,
96                                                 raw_ostream &_OS,
97                                                 MCCodeEmitter *_Emitter,
98                                                 bool RelaxAll,
99                                                 bool NoExecStack);
100     typedef MCStreamer *(*AsmStreamerCtorTy)(MCContext &Ctx,
101                                              formatted_raw_ostream &OS,
102                                              bool isVerboseAsm,
103                                              bool useLoc,
104                                              bool useCFI,
105                                              MCInstPrinter *InstPrint,
106                                              MCCodeEmitter *CE,
107                                              TargetAsmBackend *TAB,
108                                              bool ShowInst);
109
110   private:
111     /// Next - The next registered target in the linked list, maintained by the
112     /// TargetRegistry.
113     Target *Next;
114
115     /// TripleMatchQualityFn - The target function for rating the match quality
116     /// of a triple.
117     TripleMatchQualityFnTy TripleMatchQualityFn;
118
119     /// Name - The target name.
120     const char *Name;
121
122     /// ShortDesc - A short description of the target.
123     const char *ShortDesc;
124
125     /// HasJIT - Whether this target supports the JIT.
126     bool HasJIT;
127
128     /// AsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
129     /// registered.
130     AsmInfoCtorFnTy AsmInfoCtorFn;
131
132     /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
133     /// if registered.
134     MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
135
136     /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
137     /// if registered.
138     MCRegInfoCtorFnTy MCRegInfoCtorFn;
139
140     /// TargetMachineCtorFn - Construction function for this target's
141     /// TargetMachine, if registered.
142     TargetMachineCtorTy TargetMachineCtorFn;
143
144     /// AsmBackendCtorFn - Construction function for this target's
145     /// TargetAsmBackend, if registered.
146     AsmBackendCtorTy AsmBackendCtorFn;
147
148     /// AsmLexerCtorFn - Construction function for this target's TargetAsmLexer,
149     /// if registered.
150     AsmLexerCtorTy AsmLexerCtorFn;
151
152     /// AsmParserCtorFn - Construction function for this target's
153     /// TargetAsmParser, if registered.
154     AsmParserCtorTy AsmParserCtorFn;
155
156     /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
157     /// if registered.
158     AsmPrinterCtorTy AsmPrinterCtorFn;
159
160     /// MCDisassemblerCtorFn - Construction function for this target's
161     /// MCDisassembler, if registered.
162     MCDisassemblerCtorTy MCDisassemblerCtorFn;
163
164     /// MCInstPrinterCtorFn - Construction function for this target's
165     /// MCInstPrinter, if registered.
166     MCInstPrinterCtorTy MCInstPrinterCtorFn;
167
168     /// CodeEmitterCtorFn - Construction function for this target's CodeEmitter,
169     /// if registered.
170     CodeEmitterCtorTy CodeEmitterCtorFn;
171
172     /// ObjectStreamerCtorFn - Construction function for this target's
173     /// ObjectStreamer, if registered.
174     ObjectStreamerCtorTy ObjectStreamerCtorFn;
175
176     /// AsmStreamerCtorFn - Construction function for this target's
177     /// AsmStreamer, if registered (default = llvm::createAsmStreamer).
178     AsmStreamerCtorTy AsmStreamerCtorFn;
179
180   public:
181     Target() : AsmStreamerCtorFn(llvm::createAsmStreamer) {}
182
183     /// @name Target Information
184     /// @{
185
186     // getNext - Return the next registered target.
187     const Target *getNext() const { return Next; }
188
189     /// getName - Get the target name.
190     const char *getName() const { return Name; }
191
192     /// getShortDescription - Get a short description of the target.
193     const char *getShortDescription() const { return ShortDesc; }
194
195     /// @}
196     /// @name Feature Predicates
197     /// @{
198
199     /// hasJIT - Check if this targets supports the just-in-time compilation.
200     bool hasJIT() const { return HasJIT; }
201
202     /// hasTargetMachine - Check if this target supports code generation.
203     bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
204
205     /// hasAsmBackend - Check if this target supports .o generation.
206     bool hasAsmBackend() const { return AsmBackendCtorFn != 0; }
207
208     /// hasAsmLexer - Check if this target supports .s lexing.
209     bool hasAsmLexer() const { return AsmLexerCtorFn != 0; }
210
211     /// hasAsmParser - Check if this target supports .s parsing.
212     bool hasAsmParser() const { return AsmParserCtorFn != 0; }
213
214     /// hasAsmPrinter - Check if this target supports .s printing.
215     bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
216
217     /// hasMCDisassembler - Check if this target has a disassembler.
218     bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
219
220     /// hasMCInstPrinter - Check if this target has an instruction printer.
221     bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
222
223     /// hasCodeEmitter - Check if this target supports instruction encoding.
224     bool hasCodeEmitter() const { return CodeEmitterCtorFn != 0; }
225
226     /// hasObjectStreamer - Check if this target supports streaming to files.
227     bool hasObjectStreamer() const { return ObjectStreamerCtorFn != 0; }
228
229     /// hasAsmStreamer - Check if this target supports streaming to files.
230     bool hasAsmStreamer() const { return AsmStreamerCtorFn != 0; }
231
232     /// @}
233     /// @name Feature Constructors
234     /// @{
235
236     /// createAsmInfo - Create a MCAsmInfo implementation for the specified
237     /// target triple.
238     ///
239     /// \arg Triple - This argument is used to determine the target machine
240     /// feature set; it should always be provided. Generally this should be
241     /// either the target triple from the module, or the target triple of the
242     /// host if that does not exist.
243     MCAsmInfo *createAsmInfo(StringRef Triple) const {
244       if (!AsmInfoCtorFn)
245         return 0;
246       return AsmInfoCtorFn(*this, Triple);
247     }
248
249     /// createMCInstrInfo - Create a MCInstrInfo implementation.
250     ///
251     MCInstrInfo *createMCInstrInfo() const {
252       if (!MCInstrInfoCtorFn)
253         return 0;
254       return MCInstrInfoCtorFn();
255     }
256
257     /// createMCRegInfo - Create a MCRegisterInfo implementation.
258     ///
259     MCRegisterInfo *createMCRegInfo() const {
260       if (!MCRegInfoCtorFn)
261         return 0;
262       return MCRegInfoCtorFn();
263     }
264
265     /// createTargetMachine - Create a target specific machine implementation
266     /// for the specified \arg Triple.
267     ///
268     /// \arg Triple - This argument is used to determine the target machine
269     /// feature set; it should always be provided. Generally this should be
270     /// either the target triple from the module, or the target triple of the
271     /// host if that does not exist.
272     TargetMachine *createTargetMachine(const std::string &Triple,
273                                        const std::string &CPU,
274                                        const std::string &Features) const {
275       if (!TargetMachineCtorFn)
276         return 0;
277       return TargetMachineCtorFn(*this, Triple, CPU, Features);
278     }
279
280     /// createAsmBackend - Create a target specific assembly parser.
281     ///
282     /// \arg Triple - The target triple string.
283     /// \arg Backend - The target independent assembler object.
284     TargetAsmBackend *createAsmBackend(const std::string &Triple) const {
285       if (!AsmBackendCtorFn)
286         return 0;
287       return AsmBackendCtorFn(*this, Triple);
288     }
289
290     /// createAsmLexer - Create a target specific assembly lexer.
291     ///
292     TargetAsmLexer *createAsmLexer(const MCAsmInfo &MAI) const {
293       if (!AsmLexerCtorFn)
294         return 0;
295       return AsmLexerCtorFn(*this, MAI);
296     }
297
298     /// createAsmParser - Create a target specific assembly parser.
299     ///
300     /// \arg Parser - The target independent parser implementation to use for
301     /// parsing and lexing.
302     TargetAsmParser *createAsmParser(StringRef Triple, StringRef CPU,
303                                      StringRef Features,
304                                      MCAsmParser &Parser) const {
305       if (!AsmParserCtorFn)
306         return 0;
307       return AsmParserCtorFn(Triple, CPU, Features, Parser);
308     }
309
310     /// createAsmPrinter - Create a target specific assembly printer pass.  This
311     /// takes ownership of the MCStreamer object.
312     AsmPrinter *createAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) const{
313       if (!AsmPrinterCtorFn)
314         return 0;
315       return AsmPrinterCtorFn(TM, Streamer);
316     }
317
318     MCDisassembler *createMCDisassembler() const {
319       if (!MCDisassemblerCtorFn)
320         return 0;
321       return MCDisassemblerCtorFn(*this);
322     }
323
324     MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
325                                        const MCAsmInfo &MAI) const {
326       if (!MCInstPrinterCtorFn)
327         return 0;
328       return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI);
329     }
330
331
332     /// createCodeEmitter - Create a target specific code emitter.
333     MCCodeEmitter *createCodeEmitter(TargetMachine &TM, MCContext &Ctx) const {
334       if (!CodeEmitterCtorFn)
335         return 0;
336       return CodeEmitterCtorFn(*this, TM, Ctx);
337     }
338
339     /// createObjectStreamer - Create a target specific MCStreamer.
340     ///
341     /// \arg TT - The target triple.
342     /// \arg Ctx - The target context.
343     /// \arg TAB - The target assembler backend object. Takes ownership.
344     /// \arg _OS - The stream object.
345     /// \arg _Emitter - The target independent assembler object.Takes ownership.
346     /// \arg RelaxAll - Relax all fixups?
347     /// \arg NoExecStack - Mark file as not needing a executable stack.
348     MCStreamer *createObjectStreamer(const std::string &TT, MCContext &Ctx,
349                                      TargetAsmBackend &TAB,
350                                      raw_ostream &_OS,
351                                      MCCodeEmitter *_Emitter,
352                                      bool RelaxAll,
353                                      bool NoExecStack) const {
354       if (!ObjectStreamerCtorFn)
355         return 0;
356       return ObjectStreamerCtorFn(*this, TT, Ctx, TAB, _OS, _Emitter, RelaxAll,
357                                   NoExecStack);
358     }
359
360     /// createAsmStreamer - Create a target specific MCStreamer.
361     MCStreamer *createAsmStreamer(MCContext &Ctx,
362                                   formatted_raw_ostream &OS,
363                                   bool isVerboseAsm,
364                                   bool useLoc,
365                                   bool useCFI,
366                                   MCInstPrinter *InstPrint,
367                                   MCCodeEmitter *CE,
368                                   TargetAsmBackend *TAB,
369                                   bool ShowInst) const {
370       // AsmStreamerCtorFn is default to llvm::createAsmStreamer
371       return AsmStreamerCtorFn(Ctx, OS, isVerboseAsm, useLoc, useCFI,
372                                InstPrint, CE, TAB, ShowInst);
373     }
374
375     /// @}
376   };
377
378   /// TargetRegistry - Generic interface to target specific features.
379   struct TargetRegistry {
380     class iterator {
381       const Target *Current;
382       explicit iterator(Target *T) : Current(T) {}
383       friend struct TargetRegistry;
384     public:
385       iterator(const iterator &I) : Current(I.Current) {}
386       iterator() : Current(0) {}
387
388       bool operator==(const iterator &x) const {
389         return Current == x.Current;
390       }
391       bool operator!=(const iterator &x) const {
392         return !operator==(x);
393       }
394
395       // Iterator traversal: forward iteration only
396       iterator &operator++() {          // Preincrement
397         assert(Current && "Cannot increment end iterator!");
398         Current = Current->getNext();
399         return *this;
400       }
401       iterator operator++(int) {        // Postincrement
402         iterator tmp = *this;
403         ++*this;
404         return tmp;
405       }
406
407       const Target &operator*() const {
408         assert(Current && "Cannot dereference end iterator!");
409         return *Current;
410       }
411
412       const Target *operator->() const {
413         return &operator*();
414       }
415     };
416
417     /// @name Registry Access
418     /// @{
419
420     static iterator begin();
421
422     static iterator end() { return iterator(); }
423
424     /// lookupTarget - Lookup a target based on a target triple.
425     ///
426     /// \param Triple - The triple to use for finding a target.
427     /// \param Error - On failure, an error string describing why no target was
428     /// found.
429     static const Target *lookupTarget(const std::string &Triple,
430                                       std::string &Error);
431
432     /// getClosestTargetForJIT - Pick the best target that is compatible with
433     /// the current host.  If no close target can be found, this returns null
434     /// and sets the Error string to a reason.
435     ///
436     /// Maintained for compatibility through 2.6.
437     static const Target *getClosestTargetForJIT(std::string &Error);
438
439     /// @}
440     /// @name Target Registration
441     /// @{
442
443     /// RegisterTarget - Register the given target. Attempts to register a
444     /// target which has already been registered will be ignored.
445     ///
446     /// Clients are responsible for ensuring that registration doesn't occur
447     /// while another thread is attempting to access the registry. Typically
448     /// this is done by initializing all targets at program startup.
449     ///
450     /// @param T - The target being registered.
451     /// @param Name - The target name. This should be a static string.
452     /// @param ShortDesc - A short target description. This should be a static
453     /// string.
454     /// @param TQualityFn - The triple match quality computation function for
455     /// this target.
456     /// @param HasJIT - Whether the target supports JIT code
457     /// generation.
458     static void RegisterTarget(Target &T,
459                                const char *Name,
460                                const char *ShortDesc,
461                                Target::TripleMatchQualityFnTy TQualityFn,
462                                bool HasJIT = false);
463
464     /// RegisterAsmInfo - Register a MCAsmInfo implementation for the
465     /// given target.
466     ///
467     /// Clients are responsible for ensuring that registration doesn't occur
468     /// while another thread is attempting to access the registry. Typically
469     /// this is done by initializing all targets at program startup.
470     ///
471     /// @param T - The target being registered.
472     /// @param Fn - A function to construct a MCAsmInfo for the target.
473     static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
474       // Ignore duplicate registration.
475       if (!T.AsmInfoCtorFn)
476         T.AsmInfoCtorFn = Fn;
477     }
478
479     /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
480     /// given target.
481     ///
482     /// Clients are responsible for ensuring that registration doesn't occur
483     /// while another thread is attempting to access the registry. Typically
484     /// this is done by initializing all targets at program startup.
485     ///
486     /// @param T - The target being registered.
487     /// @param Fn - A function to construct a MCInstrInfo for the target.
488     static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
489       // Ignore duplicate registration.
490       if (!T.MCInstrInfoCtorFn)
491         T.MCInstrInfoCtorFn = Fn;
492     }
493
494     /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
495     /// given target.
496     ///
497     /// Clients are responsible for ensuring that registration doesn't occur
498     /// while another thread is attempting to access the registry. Typically
499     /// this is done by initializing all targets at program startup.
500     ///
501     /// @param T - The target being registered.
502     /// @param Fn - A function to construct a MCRegisterInfo for the target.
503     static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
504       // Ignore duplicate registration.
505       if (!T.MCRegInfoCtorFn)
506         T.MCRegInfoCtorFn = Fn;
507     }
508
509     /// RegisterTargetMachine - Register a TargetMachine implementation for the
510     /// given target.
511     ///
512     /// Clients are responsible for ensuring that registration doesn't occur
513     /// while another thread is attempting to access the registry. Typically
514     /// this is done by initializing all targets at program startup.
515     ///
516     /// @param T - The target being registered.
517     /// @param Fn - A function to construct a TargetMachine for the target.
518     static void RegisterTargetMachine(Target &T,
519                                       Target::TargetMachineCtorTy Fn) {
520       // Ignore duplicate registration.
521       if (!T.TargetMachineCtorFn)
522         T.TargetMachineCtorFn = Fn;
523     }
524
525     /// RegisterAsmBackend - Register a TargetAsmBackend implementation for the
526     /// given target.
527     ///
528     /// Clients are responsible for ensuring that registration doesn't occur
529     /// while another thread is attempting to access the registry. Typically
530     /// this is done by initializing all targets at program startup.
531     ///
532     /// @param T - The target being registered.
533     /// @param Fn - A function to construct an AsmBackend for the target.
534     static void RegisterAsmBackend(Target &T, Target::AsmBackendCtorTy Fn) {
535       if (!T.AsmBackendCtorFn)
536         T.AsmBackendCtorFn = Fn;
537     }
538
539     /// RegisterAsmLexer - Register a TargetAsmLexer implementation for the
540     /// given target.
541     ///
542     /// Clients are responsible for ensuring that registration doesn't occur
543     /// while another thread is attempting to access the registry. Typically
544     /// this is done by initializing all targets at program startup.
545     ///
546     /// @param T - The target being registered.
547     /// @param Fn - A function to construct an AsmLexer for the target.
548     static void RegisterAsmLexer(Target &T, Target::AsmLexerCtorTy Fn) {
549       if (!T.AsmLexerCtorFn)
550         T.AsmLexerCtorFn = Fn;
551     }
552
553     /// RegisterAsmParser - Register a TargetAsmParser implementation for the
554     /// given target.
555     ///
556     /// Clients are responsible for ensuring that registration doesn't occur
557     /// while another thread is attempting to access the registry. Typically
558     /// this is done by initializing all targets at program startup.
559     ///
560     /// @param T - The target being registered.
561     /// @param Fn - A function to construct an AsmParser for the target.
562     static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
563       if (!T.AsmParserCtorFn)
564         T.AsmParserCtorFn = Fn;
565     }
566
567     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
568     /// target.
569     ///
570     /// Clients are responsible for ensuring that registration doesn't occur
571     /// while another thread is attempting to access the registry. Typically
572     /// this is done by initializing all targets at program startup.
573     ///
574     /// @param T - The target being registered.
575     /// @param Fn - A function to construct an AsmPrinter for the target.
576     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
577       // Ignore duplicate registration.
578       if (!T.AsmPrinterCtorFn)
579         T.AsmPrinterCtorFn = Fn;
580     }
581
582     /// RegisterMCDisassembler - Register a MCDisassembler implementation for
583     /// the given target.
584     ///
585     /// Clients are responsible for ensuring that registration doesn't occur
586     /// while another thread is attempting to access the registry. Typically
587     /// this is done by initializing all targets at program startup.
588     ///
589     /// @param T - The target being registered.
590     /// @param Fn - A function to construct an MCDisassembler for the target.
591     static void RegisterMCDisassembler(Target &T,
592                                        Target::MCDisassemblerCtorTy Fn) {
593       if (!T.MCDisassemblerCtorFn)
594         T.MCDisassemblerCtorFn = Fn;
595     }
596
597     /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
598     /// given target.
599     ///
600     /// Clients are responsible for ensuring that registration doesn't occur
601     /// while another thread is attempting to access the registry. Typically
602     /// this is done by initializing all targets at program startup.
603     ///
604     /// @param T - The target being registered.
605     /// @param Fn - A function to construct an MCInstPrinter for the target.
606     static void RegisterMCInstPrinter(Target &T,
607                                       Target::MCInstPrinterCtorTy Fn) {
608       if (!T.MCInstPrinterCtorFn)
609         T.MCInstPrinterCtorFn = Fn;
610     }
611
612     /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
613     /// given target.
614     ///
615     /// Clients are responsible for ensuring that registration doesn't occur
616     /// while another thread is attempting to access the registry. Typically
617     /// this is done by initializing all targets at program startup.
618     ///
619     /// @param T - The target being registered.
620     /// @param Fn - A function to construct an MCCodeEmitter for the target.
621     static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
622       if (!T.CodeEmitterCtorFn)
623         T.CodeEmitterCtorFn = Fn;
624     }
625
626     /// RegisterObjectStreamer - Register a object code MCStreamer implementation
627     /// for the given target.
628     ///
629     /// Clients are responsible for ensuring that registration doesn't occur
630     /// while another thread is attempting to access the registry. Typically
631     /// this is done by initializing all targets at program startup.
632     ///
633     /// @param T - The target being registered.
634     /// @param Fn - A function to construct an MCStreamer for the target.
635     static void RegisterObjectStreamer(Target &T, Target::ObjectStreamerCtorTy Fn) {
636       if (!T.ObjectStreamerCtorFn)
637         T.ObjectStreamerCtorFn = Fn;
638     }
639
640     /// RegisterAsmStreamer - Register an assembly MCStreamer implementation
641     /// for the given target.
642     ///
643     /// Clients are responsible for ensuring that registration doesn't occur
644     /// while another thread is attempting to access the registry. Typically
645     /// this is done by initializing all targets at program startup.
646     ///
647     /// @param T - The target being registered.
648     /// @param Fn - A function to construct an MCStreamer for the target.
649     static void RegisterAsmStreamer(Target &T, Target::AsmStreamerCtorTy Fn) {
650       if (T.AsmStreamerCtorFn == createAsmStreamer)
651         T.AsmStreamerCtorFn = Fn;
652     }
653
654     /// @}
655   };
656
657
658   //===--------------------------------------------------------------------===//
659
660   /// RegisterTarget - Helper template for registering a target, for use in the
661   /// target's initialization function. Usage:
662   ///
663   ///
664   /// Target TheFooTarget; // The global target instance.
665   ///
666   /// extern "C" void LLVMInitializeFooTargetInfo() {
667   ///   RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
668   /// }
669   template<Triple::ArchType TargetArchType = Triple::InvalidArch,
670            bool HasJIT = false>
671   struct RegisterTarget {
672     RegisterTarget(Target &T, const char *Name, const char *Desc) {
673       TargetRegistry::RegisterTarget(T, Name, Desc,
674                                      &getTripleMatchQuality,
675                                      HasJIT);
676     }
677
678     static unsigned getTripleMatchQuality(const std::string &TT) {
679       if (Triple(TT).getArch() == TargetArchType)
680         return 20;
681       return 0;
682     }
683   };
684
685   /// RegisterAsmInfo - Helper template for registering a target assembly info
686   /// implementation.  This invokes the static "Create" method on the class to
687   /// actually do the construction.  Usage:
688   ///
689   /// extern "C" void LLVMInitializeFooTarget() {
690   ///   extern Target TheFooTarget;
691   ///   RegisterAsmInfo<FooMCAsmInfo> X(TheFooTarget);
692   /// }
693   template<class MCAsmInfoImpl>
694   struct RegisterAsmInfo {
695     RegisterAsmInfo(Target &T) {
696       TargetRegistry::RegisterAsmInfo(T, &Allocator);
697     }
698   private:
699     static MCAsmInfo *Allocator(const Target &T, StringRef TT) {
700       return new MCAsmInfoImpl(T, TT);
701     }
702
703   };
704
705   /// RegisterAsmInfoFn - Helper template for registering a target assembly info
706   /// implementation.  This invokes the specified function to do the
707   /// construction.  Usage:
708   ///
709   /// extern "C" void LLVMInitializeFooTarget() {
710   ///   extern Target TheFooTarget;
711   ///   RegisterAsmInfoFn X(TheFooTarget, TheFunction);
712   /// }
713   struct RegisterAsmInfoFn {
714     RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
715       TargetRegistry::RegisterAsmInfo(T, Fn);
716     }
717   };
718
719   /// RegisterMCInstrInfo - Helper template for registering a target instruction
720   /// info implementation.  This invokes the static "Create" method on the class
721   /// to actually do the construction.  Usage:
722   ///
723   /// extern "C" void LLVMInitializeFooTarget() {
724   ///   extern Target TheFooTarget;
725   ///   RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
726   /// }
727   template<class MCInstrInfoImpl>
728   struct RegisterMCInstrInfo {
729     RegisterMCInstrInfo(Target &T) {
730       TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
731     }
732   private:
733     static MCInstrInfo *Allocator() {
734       return new MCInstrInfoImpl();
735     }
736   };
737
738   /// RegisterMCInstrInfoFn - Helper template for registering a target
739   /// instruction info implementation.  This invokes the specified function to
740   /// do the construction.  Usage:
741   ///
742   /// extern "C" void LLVMInitializeFooTarget() {
743   ///   extern Target TheFooTarget;
744   ///   RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
745   /// }
746   struct RegisterMCInstrInfoFn {
747     RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
748       TargetRegistry::RegisterMCInstrInfo(T, Fn);
749     }
750   };
751
752   /// RegisterMCRegInfo - Helper template for registering a target register info
753   /// implementation.  This invokes the static "Create" method on the class to
754   /// actually do the construction.  Usage:
755   ///
756   /// extern "C" void LLVMInitializeFooTarget() {
757   ///   extern Target TheFooTarget;
758   ///   RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
759   /// }
760   template<class MCRegisterInfoImpl>
761   struct RegisterMCRegInfo {
762     RegisterMCRegInfo(Target &T) {
763       TargetRegistry::RegisterMCRegInfo(T, &Allocator);
764     }
765   private:
766     static MCRegisterInfo *Allocator() {
767       return new MCRegisterInfoImpl();
768     }
769   };
770
771   /// RegisterMCRegInfoFn - Helper template for registering a target register
772   /// info implementation.  This invokes the specified function to do the
773   /// construction.  Usage:
774   ///
775   /// extern "C" void LLVMInitializeFooTarget() {
776   ///   extern Target TheFooTarget;
777   ///   RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
778   /// }
779   struct RegisterMCRegInfoFn {
780     RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
781       TargetRegistry::RegisterMCRegInfo(T, Fn);
782     }
783   };
784
785   /// RegisterTargetMachine - Helper template for registering a target machine
786   /// implementation, for use in the target machine initialization
787   /// function. Usage:
788   ///
789   /// extern "C" void LLVMInitializeFooTarget() {
790   ///   extern Target TheFooTarget;
791   ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
792   /// }
793   template<class TargetMachineImpl>
794   struct RegisterTargetMachine {
795     RegisterTargetMachine(Target &T) {
796       TargetRegistry::RegisterTargetMachine(T, &Allocator);
797     }
798
799   private:
800     static TargetMachine *Allocator(const Target &T, const std::string &TT,
801                                     const std::string &CPU,
802                                     const std::string &FS) {
803       return new TargetMachineImpl(T, TT, CPU, FS);
804     }
805   };
806
807   /// RegisterAsmBackend - Helper template for registering a target specific
808   /// assembler backend. Usage:
809   ///
810   /// extern "C" void LLVMInitializeFooAsmBackend() {
811   ///   extern Target TheFooTarget;
812   ///   RegisterAsmBackend<FooAsmLexer> X(TheFooTarget);
813   /// }
814   template<class AsmBackendImpl>
815   struct RegisterAsmBackend {
816     RegisterAsmBackend(Target &T) {
817       TargetRegistry::RegisterAsmBackend(T, &Allocator);
818     }
819
820   private:
821     static TargetAsmBackend *Allocator(const Target &T,
822                                        const std::string &Triple) {
823       return new AsmBackendImpl(T, Triple);
824     }
825   };
826
827   /// RegisterAsmLexer - Helper template for registering a target specific
828   /// assembly lexer, for use in the target machine initialization
829   /// function. Usage:
830   ///
831   /// extern "C" void LLVMInitializeFooAsmLexer() {
832   ///   extern Target TheFooTarget;
833   ///   RegisterAsmLexer<FooAsmLexer> X(TheFooTarget);
834   /// }
835   template<class AsmLexerImpl>
836   struct RegisterAsmLexer {
837     RegisterAsmLexer(Target &T) {
838       TargetRegistry::RegisterAsmLexer(T, &Allocator);
839     }
840
841   private:
842     static TargetAsmLexer *Allocator(const Target &T, const MCAsmInfo &MAI) {
843       return new AsmLexerImpl(T, MAI);
844     }
845   };
846
847   /// RegisterAsmParser - Helper template for registering a target specific
848   /// assembly parser, for use in the target machine initialization
849   /// function. Usage:
850   ///
851   /// extern "C" void LLVMInitializeFooAsmParser() {
852   ///   extern Target TheFooTarget;
853   ///   RegisterAsmParser<FooAsmParser> X(TheFooTarget);
854   /// }
855   template<class AsmParserImpl>
856   struct RegisterAsmParser {
857     RegisterAsmParser(Target &T) {
858       TargetRegistry::RegisterAsmParser(T, &Allocator);
859     }
860
861   private:
862     static TargetAsmParser *Allocator(StringRef TT, StringRef CPU,
863                                       StringRef FS, MCAsmParser &P) {
864       return new AsmParserImpl(TT, CPU, FS, P);
865     }
866   };
867
868   /// RegisterAsmPrinter - Helper template for registering a target specific
869   /// assembly printer, for use in the target machine initialization
870   /// function. Usage:
871   ///
872   /// extern "C" void LLVMInitializeFooAsmPrinter() {
873   ///   extern Target TheFooTarget;
874   ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
875   /// }
876   template<class AsmPrinterImpl>
877   struct RegisterAsmPrinter {
878     RegisterAsmPrinter(Target &T) {
879       TargetRegistry::RegisterAsmPrinter(T, &Allocator);
880     }
881
882   private:
883     static AsmPrinter *Allocator(TargetMachine &TM, MCStreamer &Streamer) {
884       return new AsmPrinterImpl(TM, Streamer);
885     }
886   };
887
888   /// RegisterCodeEmitter - Helper template for registering a target specific
889   /// machine code emitter, for use in the target initialization
890   /// function. Usage:
891   ///
892   /// extern "C" void LLVMInitializeFooCodeEmitter() {
893   ///   extern Target TheFooTarget;
894   ///   RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
895   /// }
896   template<class CodeEmitterImpl>
897   struct RegisterCodeEmitter {
898     RegisterCodeEmitter(Target &T) {
899       TargetRegistry::RegisterCodeEmitter(T, &Allocator);
900     }
901
902   private:
903     static MCCodeEmitter *Allocator(const Target &T, TargetMachine &TM,
904                                     MCContext &Ctx) {
905       return new CodeEmitterImpl(T, TM, Ctx);
906     }
907   };
908
909 }
910
911 #endif