ba6bbde131d45f27395650fb37039a8ab63dcc88
[oota-llvm.git] / include / llvm / Support / TargetRegistry.h
1 //===-- Support/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_SUPPORT_TARGETREGISTRY_H
20 #define LLVM_SUPPORT_TARGETREGISTRY_H
21
22 #include "llvm-c/Disassembler.h"
23 #include "llvm/ADT/Triple.h"
24 #include "llvm/Support/CodeGen.h"
25 #include <cassert>
26 #include <memory>
27 #include <string>
28
29 namespace llvm {
30   class AsmPrinter;
31   class MCAsmBackend;
32   class MCAsmInfo;
33   class MCAsmParser;
34   class MCCodeEmitter;
35   class MCCodeGenInfo;
36   class MCContext;
37   class MCDisassembler;
38   class MCInstrAnalysis;
39   class MCInstPrinter;
40   class MCInstrInfo;
41   class MCRegisterInfo;
42   class MCStreamer;
43   class MCSubtargetInfo;
44   class MCSymbolizer;
45   class MCRelocationInfo;
46   class MCTargetAsmParser;
47   class MCTargetOptions;
48   class MCTargetStreamer;
49   class TargetMachine;
50   class TargetOptions;
51   class raw_ostream;
52   class formatted_raw_ostream;
53
54   MCStreamer *createNullStreamer(MCContext &Ctx);
55   MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
56                                 bool isVerboseAsm, bool useDwarfDirectory,
57                                 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
58                                 MCAsmBackend *TAB, bool ShowInst);
59
60   /// Takes ownership of \p TAB and \p CE.
61   MCStreamer *createELFStreamer(MCContext &Ctx, MCAsmBackend &TAB,
62                                 raw_ostream &OS, MCCodeEmitter *CE,
63                                 bool RelaxAll);
64   MCStreamer *createMachOStreamer(MCContext &Ctx, MCAsmBackend &TAB,
65                                   raw_ostream &OS, MCCodeEmitter *CE,
66                                   bool RelaxAll, bool DWARFMustBeAtTheEnd,
67                                   bool LabelSections = false);
68
69   MCRelocationInfo *createMCRelocationInfo(StringRef TT, MCContext &Ctx);
70
71   MCSymbolizer *createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
72                                    LLVMSymbolLookupCallback SymbolLookUp,
73                                    void *DisInfo, MCContext *Ctx,
74                                    std::unique_ptr<MCRelocationInfo> &&RelInfo);
75
76   /// Target - Wrapper for Target specific information.
77   ///
78   /// For registration purposes, this is a POD type so that targets can be
79   /// registered without the use of static constructors.
80   ///
81   /// Targets should implement a single global instance of this class (which
82   /// will be zero initialized), and pass that instance to the TargetRegistry as
83   /// part of their initialization.
84   class Target {
85   public:
86     friend struct TargetRegistry;
87
88     typedef bool (*ArchMatchFnTy)(Triple::ArchType Arch);
89
90     typedef MCAsmInfo *(*MCAsmInfoCtorFnTy)(const MCRegisterInfo &MRI,
91                                             StringRef TT);
92     typedef MCCodeGenInfo *(*MCCodeGenInfoCtorFnTy)(StringRef TT,
93                                                     Reloc::Model RM,
94                                                     CodeModel::Model CM,
95                                                     CodeGenOpt::Level OL);
96     typedef MCInstrInfo *(*MCInstrInfoCtorFnTy)(void);
97     typedef MCInstrAnalysis *(*MCInstrAnalysisCtorFnTy)(const MCInstrInfo*Info);
98     typedef MCRegisterInfo *(*MCRegInfoCtorFnTy)(StringRef TT);
99     typedef MCSubtargetInfo *(*MCSubtargetInfoCtorFnTy)(StringRef TT,
100                                                         StringRef CPU,
101                                                         StringRef Features);
102     typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
103                                                   StringRef TT,
104                                                   StringRef CPU,
105                                                   StringRef Features,
106                                                   const TargetOptions &Options,
107                                                   Reloc::Model RM,
108                                                   CodeModel::Model CM,
109                                                   CodeGenOpt::Level OL);
110     // If it weren't for layering issues (this header is in llvm/Support, but
111     // depends on MC?) this should take the Streamer by value rather than rvalue
112     // reference.
113     typedef AsmPrinter *(*AsmPrinterCtorTy)(
114         TargetMachine &TM, std::unique_ptr<MCStreamer> &&Streamer);
115     typedef MCAsmBackend *(*MCAsmBackendCtorTy)(const Target &T,
116                                                 const MCRegisterInfo &MRI,
117                                                 StringRef TT,
118                                                 StringRef CPU);
119     typedef MCTargetAsmParser *(*MCAsmParserCtorTy)(
120         MCSubtargetInfo &STI,
121         MCAsmParser &P,
122         const MCInstrInfo &MII,
123         const MCTargetOptions &Options);
124     typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T,
125                                                     const MCSubtargetInfo &STI,
126                                                     MCContext &Ctx);
127     typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
128                                                   unsigned SyntaxVariant,
129                                                   const MCAsmInfo &MAI,
130                                                   const MCInstrInfo &MII,
131                                                   const MCRegisterInfo &MRI,
132                                                   const MCSubtargetInfo &STI);
133     typedef MCCodeEmitter *(*MCCodeEmitterCtorTy)(const MCInstrInfo &II,
134                                                   const MCRegisterInfo &MRI,
135                                                   MCContext &Ctx);
136     typedef MCStreamer *(*ELFStreamerCtorTy)(const Triple &T, MCContext &Ctx,
137                                              MCAsmBackend &TAB, raw_ostream &OS,
138                                              MCCodeEmitter *Emitter,
139                                              bool RelaxAll);
140     typedef MCStreamer *(*MachOStreamerCtorTy)(
141         MCContext &Ctx, MCAsmBackend &TAB, raw_ostream &OS,
142         MCCodeEmitter *Emitter, bool RelaxAll, bool DWARFMustBeAtTheEnd);
143     typedef MCStreamer *(*COFFStreamerCtorTy)(MCContext &Ctx, MCAsmBackend &TAB,
144                                               raw_ostream &OS,
145                                               MCCodeEmitter *Emitter,
146                                               bool RelaxAll);
147     typedef MCTargetStreamer *(*NullTargetStreamerCtorTy)(MCStreamer &S);
148     typedef MCTargetStreamer *(*AsmTargetStreamerCtorTy)(
149         MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint,
150         bool IsVerboseAsm);
151     typedef MCTargetStreamer *(*ObjectTargetStreamerCtorTy)(
152         MCStreamer &S, const MCSubtargetInfo &STI);
153     typedef MCRelocationInfo *(*MCRelocationInfoCtorTy)(StringRef TT,
154                                                         MCContext &Ctx);
155     typedef MCSymbolizer *(*MCSymbolizerCtorTy)(
156         StringRef TT, LLVMOpInfoCallback GetOpInfo,
157         LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx,
158         std::unique_ptr<MCRelocationInfo> &&RelInfo);
159
160   private:
161     /// Next - The next registered target in the linked list, maintained by the
162     /// TargetRegistry.
163     Target *Next;
164
165     /// The target function for checking if an architecture is supported.
166     ArchMatchFnTy ArchMatchFn;
167
168     /// Name - The target name.
169     const char *Name;
170
171     /// ShortDesc - A short description of the target.
172     const char *ShortDesc;
173
174     /// HasJIT - Whether this target supports the JIT.
175     bool HasJIT;
176
177     /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
178     /// registered.
179     MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
180
181     /// MCCodeGenInfoCtorFn - Constructor function for this target's
182     /// MCCodeGenInfo, if registered.
183     MCCodeGenInfoCtorFnTy MCCodeGenInfoCtorFn;
184
185     /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
186     /// if registered.
187     MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
188
189     /// MCInstrAnalysisCtorFn - Constructor function for this target's
190     /// MCInstrAnalysis, if registered.
191     MCInstrAnalysisCtorFnTy MCInstrAnalysisCtorFn;
192
193     /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
194     /// if registered.
195     MCRegInfoCtorFnTy MCRegInfoCtorFn;
196
197     /// MCSubtargetInfoCtorFn - Constructor function for this target's
198     /// MCSubtargetInfo, if registered.
199     MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn;
200
201     /// TargetMachineCtorFn - Construction function for this target's
202     /// TargetMachine, if registered.
203     TargetMachineCtorTy TargetMachineCtorFn;
204
205     /// MCAsmBackendCtorFn - Construction function for this target's
206     /// MCAsmBackend, if registered.
207     MCAsmBackendCtorTy MCAsmBackendCtorFn;
208
209     /// MCAsmParserCtorFn - Construction function for this target's
210     /// MCTargetAsmParser, if registered.
211     MCAsmParserCtorTy MCAsmParserCtorFn;
212
213     /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
214     /// if registered.
215     AsmPrinterCtorTy AsmPrinterCtorFn;
216
217     /// MCDisassemblerCtorFn - Construction function for this target's
218     /// MCDisassembler, if registered.
219     MCDisassemblerCtorTy MCDisassemblerCtorFn;
220
221     /// MCInstPrinterCtorFn - Construction function for this target's
222     /// MCInstPrinter, if registered.
223     MCInstPrinterCtorTy MCInstPrinterCtorFn;
224
225     /// MCCodeEmitterCtorFn - Construction function for this target's
226     /// CodeEmitter, if registered.
227     MCCodeEmitterCtorTy MCCodeEmitterCtorFn;
228
229     // Construction functions for the various object formats, if registered.
230     COFFStreamerCtorTy COFFStreamerCtorFn;
231     MachOStreamerCtorTy MachOStreamerCtorFn;
232     ELFStreamerCtorTy ELFStreamerCtorFn;
233
234     /// Construction function for this target's null TargetStreamer, if
235     /// registered (default = nullptr).
236     NullTargetStreamerCtorTy NullTargetStreamerCtorFn;
237
238     /// Construction function for this target's asm TargetStreamer, if
239     /// registered (default = nullptr).
240     AsmTargetStreamerCtorTy AsmTargetStreamerCtorFn;
241
242     /// Construction function for this target's obj TargetStreamer, if
243     /// registered (default = nullptr).
244     ObjectTargetStreamerCtorTy ObjectTargetStreamerCtorFn;
245
246     /// MCRelocationInfoCtorFn - Construction function for this target's
247     /// MCRelocationInfo, if registered (default = llvm::createMCRelocationInfo)
248     MCRelocationInfoCtorTy MCRelocationInfoCtorFn;
249
250     /// MCSymbolizerCtorFn - Construction function for this target's
251     /// MCSymbolizer, if registered (default = llvm::createMCSymbolizer)
252     MCSymbolizerCtorTy MCSymbolizerCtorFn;
253
254   public:
255     Target()
256         : COFFStreamerCtorFn(nullptr), MachOStreamerCtorFn(nullptr),
257           ELFStreamerCtorFn(nullptr), NullTargetStreamerCtorFn(nullptr),
258           AsmTargetStreamerCtorFn(nullptr), ObjectTargetStreamerCtorFn(nullptr),
259           MCRelocationInfoCtorFn(nullptr), MCSymbolizerCtorFn(nullptr) {}
260
261     /// @name Target Information
262     /// @{
263
264     // getNext - Return the next registered target.
265     const Target *getNext() const { return Next; }
266
267     /// getName - Get the target name.
268     const char *getName() const { return Name; }
269
270     /// getShortDescription - Get a short description of the target.
271     const char *getShortDescription() const { return ShortDesc; }
272
273     /// @}
274     /// @name Feature Predicates
275     /// @{
276
277     /// hasJIT - Check if this targets supports the just-in-time compilation.
278     bool hasJIT() const { return HasJIT; }
279
280     /// hasTargetMachine - Check if this target supports code generation.
281     bool hasTargetMachine() const { return TargetMachineCtorFn != nullptr; }
282
283     /// hasMCAsmBackend - Check if this target supports .o generation.
284     bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != nullptr; }
285
286     /// @}
287     /// @name Feature Constructors
288     /// @{
289
290     /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
291     /// target triple.
292     ///
293     /// \param Triple This argument is used to determine the target machine
294     /// feature set; it should always be provided. Generally this should be
295     /// either the target triple from the module, or the target triple of the
296     /// host if that does not exist.
297     MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI,
298                                StringRef Triple) const {
299       if (!MCAsmInfoCtorFn)
300         return nullptr;
301       return MCAsmInfoCtorFn(MRI, Triple);
302     }
303
304     /// createMCCodeGenInfo - Create a MCCodeGenInfo implementation.
305     ///
306     MCCodeGenInfo *createMCCodeGenInfo(StringRef Triple, Reloc::Model RM,
307                                        CodeModel::Model CM,
308                                        CodeGenOpt::Level OL) const {
309       if (!MCCodeGenInfoCtorFn)
310         return nullptr;
311       return MCCodeGenInfoCtorFn(Triple, RM, CM, OL);
312     }
313
314     /// createMCInstrInfo - Create a MCInstrInfo implementation.
315     ///
316     MCInstrInfo *createMCInstrInfo() const {
317       if (!MCInstrInfoCtorFn)
318         return nullptr;
319       return MCInstrInfoCtorFn();
320     }
321
322     /// createMCInstrAnalysis - Create a MCInstrAnalysis implementation.
323     ///
324     MCInstrAnalysis *createMCInstrAnalysis(const MCInstrInfo *Info) const {
325       if (!MCInstrAnalysisCtorFn)
326         return nullptr;
327       return MCInstrAnalysisCtorFn(Info);
328     }
329
330     /// createMCRegInfo - Create a MCRegisterInfo implementation.
331     ///
332     MCRegisterInfo *createMCRegInfo(StringRef Triple) const {
333       if (!MCRegInfoCtorFn)
334         return nullptr;
335       return MCRegInfoCtorFn(Triple);
336     }
337
338     /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
339     ///
340     /// \param Triple This argument is used to determine the target machine
341     /// feature set; it should always be provided. Generally this should be
342     /// either the target triple from the module, or the target triple of the
343     /// host if that does not exist.
344     /// \param CPU This specifies the name of the target CPU.
345     /// \param Features This specifies the string representation of the
346     /// additional target features.
347     MCSubtargetInfo *createMCSubtargetInfo(StringRef Triple, StringRef CPU,
348                                            StringRef Features) const {
349       if (!MCSubtargetInfoCtorFn)
350         return nullptr;
351       return MCSubtargetInfoCtorFn(Triple, CPU, Features);
352     }
353
354     /// createTargetMachine - Create a target specific machine implementation
355     /// for the specified \p Triple.
356     ///
357     /// \param Triple This argument is used to determine the target machine
358     /// feature set; it should always be provided. Generally this should be
359     /// either the target triple from the module, or the target triple of the
360     /// host if that does not exist.
361     TargetMachine *createTargetMachine(StringRef Triple, StringRef CPU,
362                              StringRef Features, const TargetOptions &Options,
363                              Reloc::Model RM = Reloc::Default,
364                              CodeModel::Model CM = CodeModel::Default,
365                              CodeGenOpt::Level OL = CodeGenOpt::Default) const {
366       if (!TargetMachineCtorFn)
367         return nullptr;
368       return TargetMachineCtorFn(*this, Triple, CPU, Features, Options,
369                                  RM, CM, OL);
370     }
371
372     /// createMCAsmBackend - Create a target specific assembly parser.
373     ///
374     /// \param Triple The target triple string.
375     MCAsmBackend *createMCAsmBackend(const MCRegisterInfo &MRI,
376                                      StringRef Triple, StringRef CPU) const {
377       if (!MCAsmBackendCtorFn)
378         return nullptr;
379       return MCAsmBackendCtorFn(*this, MRI, Triple, CPU);
380     }
381
382     /// createMCAsmParser - Create a target specific assembly parser.
383     ///
384     /// \param Parser The target independent parser implementation to use for
385     /// parsing and lexing.
386     MCTargetAsmParser *createMCAsmParser(
387         MCSubtargetInfo &STI,
388         MCAsmParser &Parser,
389         const MCInstrInfo &MII,
390         const MCTargetOptions &Options) const {
391       if (!MCAsmParserCtorFn)
392         return nullptr;
393       return MCAsmParserCtorFn(STI, Parser, MII, Options);
394     }
395
396     /// createAsmPrinter - Create a target specific assembly printer pass.  This
397     /// takes ownership of the MCStreamer object.
398     AsmPrinter *createAsmPrinter(TargetMachine &TM,
399                                  std::unique_ptr<MCStreamer> &&Streamer) const {
400       if (!AsmPrinterCtorFn)
401         return nullptr;
402       return AsmPrinterCtorFn(TM, std::move(Streamer));
403     }
404
405     MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI,
406                                          MCContext &Ctx) const {
407       if (!MCDisassemblerCtorFn)
408         return nullptr;
409       return MCDisassemblerCtorFn(*this, STI, Ctx);
410     }
411
412     MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
413                                        const MCAsmInfo &MAI,
414                                        const MCInstrInfo &MII,
415                                        const MCRegisterInfo &MRI,
416                                        const MCSubtargetInfo &STI) const {
417       if (!MCInstPrinterCtorFn)
418         return nullptr;
419       return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI, MII, MRI, STI);
420     }
421
422
423     /// createMCCodeEmitter - Create a target specific code emitter.
424     MCCodeEmitter *createMCCodeEmitter(const MCInstrInfo &II,
425                                        const MCRegisterInfo &MRI,
426                                        MCContext &Ctx) const {
427       if (!MCCodeEmitterCtorFn)
428         return nullptr;
429       return MCCodeEmitterCtorFn(II, MRI, Ctx);
430     }
431
432     /// Create a target specific MCStreamer.
433     ///
434     /// \param T The target triple.
435     /// \param Ctx The target context.
436     /// \param TAB The target assembler backend object. Takes ownership.
437     /// \param OS The stream object.
438     /// \param Emitter The target independent assembler object.Takes ownership.
439     /// \param RelaxAll Relax all fixups?
440     MCStreamer *createMCObjectStreamer(const Triple &T, MCContext &Ctx,
441                                        MCAsmBackend &TAB, raw_ostream &OS,
442                                        MCCodeEmitter *Emitter,
443                                        const MCSubtargetInfo &STI,
444                                        bool RelaxAll,
445                                        bool DWARFMustBeAtTheEnd) const {
446       MCStreamer *S;
447       switch (T.getObjectFormat()) {
448       default:
449         llvm_unreachable("Unknown object format");
450       case Triple::COFF:
451         assert(T.isOSWindows() && "only Windows COFF is supported");
452         S = COFFStreamerCtorFn(Ctx, TAB, OS, Emitter, RelaxAll);
453         break;
454       case Triple::MachO:
455         if (MachOStreamerCtorFn)
456           S = MachOStreamerCtorFn(Ctx, TAB, OS, Emitter, RelaxAll,
457                                   DWARFMustBeAtTheEnd);
458         else
459           S = createMachOStreamer(Ctx, TAB, OS, Emitter, RelaxAll,
460                                   DWARFMustBeAtTheEnd);
461         break;
462       case Triple::ELF:
463         if (ELFStreamerCtorFn)
464           S = ELFStreamerCtorFn(T, Ctx, TAB, OS, Emitter, RelaxAll);
465         else
466           S = createELFStreamer(Ctx, TAB, OS, Emitter, RelaxAll);
467         break;
468       }
469       if (ObjectTargetStreamerCtorFn)
470         ObjectTargetStreamerCtorFn(*S, STI);
471       return S;
472     }
473
474     MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
475                                   bool IsVerboseAsm, bool UseDwarfDirectory,
476                                   MCInstPrinter *InstPrint, MCCodeEmitter *CE,
477                                   MCAsmBackend *TAB, bool ShowInst) const {
478       MCStreamer *S =
479           llvm::createAsmStreamer(Ctx, OS, IsVerboseAsm, UseDwarfDirectory,
480                                   InstPrint, CE, TAB, ShowInst);
481       createAsmTargetStreamer(*S, OS, InstPrint, IsVerboseAsm);
482       return S;
483     }
484
485     MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
486                                               formatted_raw_ostream &OS,
487                                               MCInstPrinter *InstPrint,
488                                               bool IsVerboseAsm) const {
489       if (AsmTargetStreamerCtorFn)
490         return AsmTargetStreamerCtorFn(S, OS, InstPrint, IsVerboseAsm);
491       return nullptr;
492     }
493
494     MCStreamer *createNullStreamer(MCContext &Ctx) const {
495       MCStreamer *S = llvm::createNullStreamer(Ctx);
496       createNullTargetStreamer(*S);
497       return S;
498     }
499
500     MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) const {
501       if (NullTargetStreamerCtorFn)
502         return NullTargetStreamerCtorFn(S);
503       return nullptr;
504     }
505
506     /// createMCRelocationInfo - Create a target specific MCRelocationInfo.
507     ///
508     /// \param TT The target triple.
509     /// \param Ctx The target context.
510     MCRelocationInfo *
511       createMCRelocationInfo(StringRef TT, MCContext &Ctx) const {
512       MCRelocationInfoCtorTy Fn = MCRelocationInfoCtorFn
513                                       ? MCRelocationInfoCtorFn
514                                       : llvm::createMCRelocationInfo;
515       return Fn(TT, Ctx);
516     }
517
518     /// createMCSymbolizer - Create a target specific MCSymbolizer.
519     ///
520     /// \param TT The target triple.
521     /// \param GetOpInfo The function to get the symbolic information for operands.
522     /// \param SymbolLookUp The function to lookup a symbol name.
523     /// \param DisInfo The pointer to the block of symbolic information for above call
524     /// back.
525     /// \param Ctx The target context.
526     /// \param RelInfo The relocation information for this target. Takes ownership.
527     MCSymbolizer *
528     createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
529                        LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo,
530                        MCContext *Ctx,
531                        std::unique_ptr<MCRelocationInfo> &&RelInfo) const {
532       MCSymbolizerCtorTy Fn =
533           MCSymbolizerCtorFn ? MCSymbolizerCtorFn : llvm::createMCSymbolizer;
534       return Fn(TT, GetOpInfo, SymbolLookUp, DisInfo, Ctx, std::move(RelInfo));
535     }
536
537     /// @}
538   };
539
540   /// TargetRegistry - Generic interface to target specific features.
541   struct TargetRegistry {
542     class iterator {
543       const Target *Current;
544       explicit iterator(Target *T) : Current(T) {}
545       friend struct TargetRegistry;
546     public:
547       iterator() : Current(nullptr) {}
548
549       bool operator==(const iterator &x) const {
550         return Current == x.Current;
551       }
552       bool operator!=(const iterator &x) const {
553         return !operator==(x);
554       }
555
556       // Iterator traversal: forward iteration only
557       iterator &operator++() {          // Preincrement
558         assert(Current && "Cannot increment end iterator!");
559         Current = Current->getNext();
560         return *this;
561       }
562       iterator operator++(int) {        // Postincrement
563         iterator tmp = *this;
564         ++*this;
565         return tmp;
566       }
567
568       const Target &operator*() const {
569         assert(Current && "Cannot dereference end iterator!");
570         return *Current;
571       }
572
573       const Target *operator->() const {
574         return &operator*();
575       }
576     };
577
578     /// printRegisteredTargetsForVersion - Print the registered targets
579     /// appropriately for inclusion in a tool's version output.
580     static void printRegisteredTargetsForVersion();
581
582     /// @name Registry Access
583     /// @{
584
585     static iterator begin();
586
587     static iterator end() { return iterator(); }
588
589     /// lookupTarget - Lookup a target based on a target triple.
590     ///
591     /// \param Triple - The triple to use for finding a target.
592     /// \param Error - On failure, an error string describing why no target was
593     /// found.
594     static const Target *lookupTarget(const std::string &Triple,
595                                       std::string &Error);
596
597     /// lookupTarget - Lookup a target based on an architecture name
598     /// and a target triple.  If the architecture name is non-empty,
599     /// then the lookup is done by architecture.  Otherwise, the target
600     /// triple is used.
601     ///
602     /// \param ArchName - The architecture to use for finding a target.
603     /// \param TheTriple - The triple to use for finding a target.  The
604     /// triple is updated with canonical architecture name if a lookup
605     /// by architecture is done.
606     /// \param Error - On failure, an error string describing why no target was
607     /// found.
608     static const Target *lookupTarget(const std::string &ArchName,
609                                       Triple &TheTriple,
610                                       std::string &Error);
611
612     /// @}
613     /// @name Target Registration
614     /// @{
615
616     /// RegisterTarget - Register the given target. Attempts to register a
617     /// target which has already been registered will be ignored.
618     ///
619     /// Clients are responsible for ensuring that registration doesn't occur
620     /// while another thread is attempting to access the registry. Typically
621     /// this is done by initializing all targets at program startup.
622     ///
623     /// @param T - The target being registered.
624     /// @param Name - The target name. This should be a static string.
625     /// @param ShortDesc - A short target description. This should be a static
626     /// string.
627     /// @param ArchMatchFn - The arch match checking function for this target.
628     /// @param HasJIT - Whether the target supports JIT code
629     /// generation.
630     static void RegisterTarget(Target &T,
631                                const char *Name,
632                                const char *ShortDesc,
633                                Target::ArchMatchFnTy ArchMatchFn,
634                                bool HasJIT = false);
635
636     /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
637     /// given target.
638     ///
639     /// Clients are responsible for ensuring that registration doesn't occur
640     /// while another thread is attempting to access the registry. Typically
641     /// this is done by initializing all targets at program startup.
642     ///
643     /// @param T - The target being registered.
644     /// @param Fn - A function to construct a MCAsmInfo for the target.
645     static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
646       T.MCAsmInfoCtorFn = Fn;
647     }
648
649     /// RegisterMCCodeGenInfo - Register a MCCodeGenInfo implementation for the
650     /// given target.
651     ///
652     /// Clients are responsible for ensuring that registration doesn't occur
653     /// while another thread is attempting to access the registry. Typically
654     /// this is done by initializing all targets at program startup.
655     ///
656     /// @param T - The target being registered.
657     /// @param Fn - A function to construct a MCCodeGenInfo for the target.
658     static void RegisterMCCodeGenInfo(Target &T,
659                                      Target::MCCodeGenInfoCtorFnTy Fn) {
660       T.MCCodeGenInfoCtorFn = Fn;
661     }
662
663     /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
664     /// given target.
665     ///
666     /// Clients are responsible for ensuring that registration doesn't occur
667     /// while another thread is attempting to access the registry. Typically
668     /// this is done by initializing all targets at program startup.
669     ///
670     /// @param T - The target being registered.
671     /// @param Fn - A function to construct a MCInstrInfo for the target.
672     static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
673       T.MCInstrInfoCtorFn = Fn;
674     }
675
676     /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for
677     /// the given target.
678     static void RegisterMCInstrAnalysis(Target &T,
679                                         Target::MCInstrAnalysisCtorFnTy Fn) {
680       T.MCInstrAnalysisCtorFn = Fn;
681     }
682
683     /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
684     /// given target.
685     ///
686     /// Clients are responsible for ensuring that registration doesn't occur
687     /// while another thread is attempting to access the registry. Typically
688     /// this is done by initializing all targets at program startup.
689     ///
690     /// @param T - The target being registered.
691     /// @param Fn - A function to construct a MCRegisterInfo for the target.
692     static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
693       T.MCRegInfoCtorFn = Fn;
694     }
695
696     /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
697     /// the given target.
698     ///
699     /// Clients are responsible for ensuring that registration doesn't occur
700     /// while another thread is attempting to access the registry. Typically
701     /// this is done by initializing all targets at program startup.
702     ///
703     /// @param T - The target being registered.
704     /// @param Fn - A function to construct a MCSubtargetInfo for the target.
705     static void RegisterMCSubtargetInfo(Target &T,
706                                         Target::MCSubtargetInfoCtorFnTy Fn) {
707       T.MCSubtargetInfoCtorFn = Fn;
708     }
709
710     /// RegisterTargetMachine - Register a TargetMachine implementation for the
711     /// given target.
712     ///
713     /// Clients are responsible for ensuring that registration doesn't occur
714     /// while another thread is attempting to access the registry. Typically
715     /// this is done by initializing all targets at program startup.
716     ///
717     /// @param T - The target being registered.
718     /// @param Fn - A function to construct a TargetMachine for the target.
719     static void RegisterTargetMachine(Target &T,
720                                       Target::TargetMachineCtorTy Fn) {
721       T.TargetMachineCtorFn = Fn;
722     }
723
724     /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the
725     /// given target.
726     ///
727     /// Clients are responsible for ensuring that registration doesn't occur
728     /// while another thread is attempting to access the registry. Typically
729     /// this is done by initializing all targets at program startup.
730     ///
731     /// @param T - The target being registered.
732     /// @param Fn - A function to construct an AsmBackend for the target.
733     static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) {
734       T.MCAsmBackendCtorFn = Fn;
735     }
736
737     /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for
738     /// the given target.
739     ///
740     /// Clients are responsible for ensuring that registration doesn't occur
741     /// while another thread is attempting to access the registry. Typically
742     /// this is done by initializing all targets at program startup.
743     ///
744     /// @param T - The target being registered.
745     /// @param Fn - A function to construct an MCTargetAsmParser for the target.
746     static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) {
747       T.MCAsmParserCtorFn = Fn;
748     }
749
750     /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
751     /// target.
752     ///
753     /// Clients are responsible for ensuring that registration doesn't occur
754     /// while another thread is attempting to access the registry. Typically
755     /// this is done by initializing all targets at program startup.
756     ///
757     /// @param T - The target being registered.
758     /// @param Fn - A function to construct an AsmPrinter for the target.
759     static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
760       T.AsmPrinterCtorFn = Fn;
761     }
762
763     /// RegisterMCDisassembler - Register a MCDisassembler implementation for
764     /// the given target.
765     ///
766     /// Clients are responsible for ensuring that registration doesn't occur
767     /// while another thread is attempting to access the registry. Typically
768     /// this is done by initializing all targets at program startup.
769     ///
770     /// @param T - The target being registered.
771     /// @param Fn - A function to construct an MCDisassembler for the target.
772     static void RegisterMCDisassembler(Target &T,
773                                        Target::MCDisassemblerCtorTy Fn) {
774       T.MCDisassemblerCtorFn = Fn;
775     }
776
777     /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
778     /// given target.
779     ///
780     /// Clients are responsible for ensuring that registration doesn't occur
781     /// while another thread is attempting to access the registry. Typically
782     /// this is done by initializing all targets at program startup.
783     ///
784     /// @param T - The target being registered.
785     /// @param Fn - A function to construct an MCInstPrinter for the target.
786     static void RegisterMCInstPrinter(Target &T,
787                                       Target::MCInstPrinterCtorTy Fn) {
788       T.MCInstPrinterCtorFn = Fn;
789     }
790
791     /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the
792     /// given target.
793     ///
794     /// Clients are responsible for ensuring that registration doesn't occur
795     /// while another thread is attempting to access the registry. Typically
796     /// this is done by initializing all targets at program startup.
797     ///
798     /// @param T - The target being registered.
799     /// @param Fn - A function to construct an MCCodeEmitter for the target.
800     static void RegisterMCCodeEmitter(Target &T,
801                                       Target::MCCodeEmitterCtorTy Fn) {
802       T.MCCodeEmitterCtorFn = Fn;
803     }
804
805     static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn) {
806       T.COFFStreamerCtorFn = Fn;
807     }
808
809     static void RegisterMachOStreamer(Target &T,
810                                       Target::MachOStreamerCtorTy Fn) {
811       T.MachOStreamerCtorFn = Fn;
812     }
813
814     static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn) {
815       T.ELFStreamerCtorFn = Fn;
816     }
817
818     static void
819     RegisterNullTargetStreamer(Target &T, Target::NullTargetStreamerCtorTy Fn) {
820       T.NullTargetStreamerCtorFn = Fn;
821     }
822
823     static void RegisterAsmTargetStreamer(Target &T,
824                                           Target::AsmTargetStreamerCtorTy Fn) {
825       T.AsmTargetStreamerCtorFn = Fn;
826     }
827
828     static void
829     RegisterObjectTargetStreamer(Target &T,
830                                  Target::ObjectTargetStreamerCtorTy Fn) {
831       T.ObjectTargetStreamerCtorFn = Fn;
832     }
833
834     /// RegisterMCRelocationInfo - Register an MCRelocationInfo
835     /// implementation for the given target.
836     ///
837     /// Clients are responsible for ensuring that registration doesn't occur
838     /// while another thread is attempting to access the registry. Typically
839     /// this is done by initializing all targets at program startup.
840     ///
841     /// @param T - The target being registered.
842     /// @param Fn - A function to construct an MCRelocationInfo for the target.
843     static void RegisterMCRelocationInfo(Target &T,
844                                          Target::MCRelocationInfoCtorTy Fn) {
845       T.MCRelocationInfoCtorFn = Fn;
846     }
847
848     /// RegisterMCSymbolizer - Register an MCSymbolizer
849     /// implementation for the given target.
850     ///
851     /// Clients are responsible for ensuring that registration doesn't occur
852     /// while another thread is attempting to access the registry. Typically
853     /// this is done by initializing all targets at program startup.
854     ///
855     /// @param T - The target being registered.
856     /// @param Fn - A function to construct an MCSymbolizer for the target.
857     static void RegisterMCSymbolizer(Target &T,
858                                      Target::MCSymbolizerCtorTy Fn) {
859       T.MCSymbolizerCtorFn = Fn;
860     }
861
862     /// @}
863   };
864
865
866   //===--------------------------------------------------------------------===//
867
868   /// RegisterTarget - Helper template for registering a target, for use in the
869   /// target's initialization function. Usage:
870   ///
871   ///
872   /// Target TheFooTarget; // The global target instance.
873   ///
874   /// extern "C" void LLVMInitializeFooTargetInfo() {
875   ///   RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
876   /// }
877   template<Triple::ArchType TargetArchType = Triple::UnknownArch,
878            bool HasJIT = false>
879   struct RegisterTarget {
880     RegisterTarget(Target &T, const char *Name, const char *Desc) {
881       TargetRegistry::RegisterTarget(T, Name, Desc, &getArchMatch, HasJIT);
882     }
883
884     static bool getArchMatch(Triple::ArchType Arch) {
885       return Arch == TargetArchType;
886     }
887   };
888
889   /// RegisterMCAsmInfo - Helper template for registering a target assembly info
890   /// implementation.  This invokes the static "Create" method on the class to
891   /// actually do the construction.  Usage:
892   ///
893   /// extern "C" void LLVMInitializeFooTarget() {
894   ///   extern Target TheFooTarget;
895   ///   RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
896   /// }
897   template<class MCAsmInfoImpl>
898   struct RegisterMCAsmInfo {
899     RegisterMCAsmInfo(Target &T) {
900       TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
901     }
902   private:
903     static MCAsmInfo *Allocator(const MCRegisterInfo &/*MRI*/, StringRef TT) {
904       return new MCAsmInfoImpl(TT);
905     }
906
907   };
908
909   /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
910   /// implementation.  This invokes the specified function to do the
911   /// construction.  Usage:
912   ///
913   /// extern "C" void LLVMInitializeFooTarget() {
914   ///   extern Target TheFooTarget;
915   ///   RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
916   /// }
917   struct RegisterMCAsmInfoFn {
918     RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
919       TargetRegistry::RegisterMCAsmInfo(T, Fn);
920     }
921   };
922
923   /// RegisterMCCodeGenInfo - Helper template for registering a target codegen info
924   /// implementation.  This invokes the static "Create" method on the class
925   /// to actually do the construction.  Usage:
926   ///
927   /// extern "C" void LLVMInitializeFooTarget() {
928   ///   extern Target TheFooTarget;
929   ///   RegisterMCCodeGenInfo<FooMCCodeGenInfo> X(TheFooTarget);
930   /// }
931   template<class MCCodeGenInfoImpl>
932   struct RegisterMCCodeGenInfo {
933     RegisterMCCodeGenInfo(Target &T) {
934       TargetRegistry::RegisterMCCodeGenInfo(T, &Allocator);
935     }
936   private:
937     static MCCodeGenInfo *Allocator(StringRef /*TT*/, Reloc::Model /*RM*/,
938                                     CodeModel::Model /*CM*/,
939                                     CodeGenOpt::Level /*OL*/) {
940       return new MCCodeGenInfoImpl();
941     }
942   };
943
944   /// RegisterMCCodeGenInfoFn - Helper template for registering a target codegen
945   /// info implementation.  This invokes the specified function to do the
946   /// construction.  Usage:
947   ///
948   /// extern "C" void LLVMInitializeFooTarget() {
949   ///   extern Target TheFooTarget;
950   ///   RegisterMCCodeGenInfoFn X(TheFooTarget, TheFunction);
951   /// }
952   struct RegisterMCCodeGenInfoFn {
953     RegisterMCCodeGenInfoFn(Target &T, Target::MCCodeGenInfoCtorFnTy Fn) {
954       TargetRegistry::RegisterMCCodeGenInfo(T, Fn);
955     }
956   };
957
958   /// RegisterMCInstrInfo - Helper template for registering a target instruction
959   /// info implementation.  This invokes the static "Create" method on the class
960   /// to actually do the construction.  Usage:
961   ///
962   /// extern "C" void LLVMInitializeFooTarget() {
963   ///   extern Target TheFooTarget;
964   ///   RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
965   /// }
966   template<class MCInstrInfoImpl>
967   struct RegisterMCInstrInfo {
968     RegisterMCInstrInfo(Target &T) {
969       TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
970     }
971   private:
972     static MCInstrInfo *Allocator() {
973       return new MCInstrInfoImpl();
974     }
975   };
976
977   /// RegisterMCInstrInfoFn - Helper template for registering a target
978   /// instruction info implementation.  This invokes the specified function to
979   /// do the construction.  Usage:
980   ///
981   /// extern "C" void LLVMInitializeFooTarget() {
982   ///   extern Target TheFooTarget;
983   ///   RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
984   /// }
985   struct RegisterMCInstrInfoFn {
986     RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
987       TargetRegistry::RegisterMCInstrInfo(T, Fn);
988     }
989   };
990
991   /// RegisterMCInstrAnalysis - Helper template for registering a target
992   /// instruction analyzer implementation.  This invokes the static "Create"
993   /// method on the class to actually do the construction.  Usage:
994   ///
995   /// extern "C" void LLVMInitializeFooTarget() {
996   ///   extern Target TheFooTarget;
997   ///   RegisterMCInstrAnalysis<FooMCInstrAnalysis> X(TheFooTarget);
998   /// }
999   template<class MCInstrAnalysisImpl>
1000   struct RegisterMCInstrAnalysis {
1001     RegisterMCInstrAnalysis(Target &T) {
1002       TargetRegistry::RegisterMCInstrAnalysis(T, &Allocator);
1003     }
1004   private:
1005     static MCInstrAnalysis *Allocator(const MCInstrInfo *Info) {
1006       return new MCInstrAnalysisImpl(Info);
1007     }
1008   };
1009
1010   /// RegisterMCInstrAnalysisFn - Helper template for registering a target
1011   /// instruction analyzer implementation.  This invokes the specified function
1012   /// to do the construction.  Usage:
1013   ///
1014   /// extern "C" void LLVMInitializeFooTarget() {
1015   ///   extern Target TheFooTarget;
1016   ///   RegisterMCInstrAnalysisFn X(TheFooTarget, TheFunction);
1017   /// }
1018   struct RegisterMCInstrAnalysisFn {
1019     RegisterMCInstrAnalysisFn(Target &T, Target::MCInstrAnalysisCtorFnTy Fn) {
1020       TargetRegistry::RegisterMCInstrAnalysis(T, Fn);
1021     }
1022   };
1023
1024   /// RegisterMCRegInfo - Helper template for registering a target register info
1025   /// implementation.  This invokes the static "Create" method on the class to
1026   /// actually do the construction.  Usage:
1027   ///
1028   /// extern "C" void LLVMInitializeFooTarget() {
1029   ///   extern Target TheFooTarget;
1030   ///   RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
1031   /// }
1032   template<class MCRegisterInfoImpl>
1033   struct RegisterMCRegInfo {
1034     RegisterMCRegInfo(Target &T) {
1035       TargetRegistry::RegisterMCRegInfo(T, &Allocator);
1036     }
1037   private:
1038     static MCRegisterInfo *Allocator(StringRef /*TT*/) {
1039       return new MCRegisterInfoImpl();
1040     }
1041   };
1042
1043   /// RegisterMCRegInfoFn - Helper template for registering a target register
1044   /// info implementation.  This invokes the specified function to do the
1045   /// construction.  Usage:
1046   ///
1047   /// extern "C" void LLVMInitializeFooTarget() {
1048   ///   extern Target TheFooTarget;
1049   ///   RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
1050   /// }
1051   struct RegisterMCRegInfoFn {
1052     RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
1053       TargetRegistry::RegisterMCRegInfo(T, Fn);
1054     }
1055   };
1056
1057   /// RegisterMCSubtargetInfo - Helper template for registering a target
1058   /// subtarget info implementation.  This invokes the static "Create" method
1059   /// on the class to actually do the construction.  Usage:
1060   ///
1061   /// extern "C" void LLVMInitializeFooTarget() {
1062   ///   extern Target TheFooTarget;
1063   ///   RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
1064   /// }
1065   template<class MCSubtargetInfoImpl>
1066   struct RegisterMCSubtargetInfo {
1067     RegisterMCSubtargetInfo(Target &T) {
1068       TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
1069     }
1070   private:
1071     static MCSubtargetInfo *Allocator(StringRef /*TT*/, StringRef /*CPU*/,
1072                                       StringRef /*FS*/) {
1073       return new MCSubtargetInfoImpl();
1074     }
1075   };
1076
1077   /// RegisterMCSubtargetInfoFn - Helper template for registering a target
1078   /// subtarget info implementation.  This invokes the specified function to
1079   /// do the construction.  Usage:
1080   ///
1081   /// extern "C" void LLVMInitializeFooTarget() {
1082   ///   extern Target TheFooTarget;
1083   ///   RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
1084   /// }
1085   struct RegisterMCSubtargetInfoFn {
1086     RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
1087       TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
1088     }
1089   };
1090
1091   /// RegisterTargetMachine - Helper template for registering a target machine
1092   /// implementation, for use in the target machine initialization
1093   /// function. Usage:
1094   ///
1095   /// extern "C" void LLVMInitializeFooTarget() {
1096   ///   extern Target TheFooTarget;
1097   ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
1098   /// }
1099   template<class TargetMachineImpl>
1100   struct RegisterTargetMachine {
1101     RegisterTargetMachine(Target &T) {
1102       TargetRegistry::RegisterTargetMachine(T, &Allocator);
1103     }
1104
1105   private:
1106     static TargetMachine *Allocator(const Target &T, StringRef TT,
1107                                     StringRef CPU, StringRef FS,
1108                                     const TargetOptions &Options,
1109                                     Reloc::Model RM,
1110                                     CodeModel::Model CM,
1111                                     CodeGenOpt::Level OL) {
1112       return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL);
1113     }
1114   };
1115
1116   /// RegisterMCAsmBackend - Helper template for registering a target specific
1117   /// assembler backend. Usage:
1118   ///
1119   /// extern "C" void LLVMInitializeFooMCAsmBackend() {
1120   ///   extern Target TheFooTarget;
1121   ///   RegisterMCAsmBackend<FooAsmLexer> X(TheFooTarget);
1122   /// }
1123   template<class MCAsmBackendImpl>
1124   struct RegisterMCAsmBackend {
1125     RegisterMCAsmBackend(Target &T) {
1126       TargetRegistry::RegisterMCAsmBackend(T, &Allocator);
1127     }
1128
1129   private:
1130     static MCAsmBackend *Allocator(const Target &T,
1131                                    const MCRegisterInfo &MRI,
1132                                    StringRef Triple, StringRef CPU) {
1133       return new MCAsmBackendImpl(T, MRI, Triple, CPU);
1134     }
1135   };
1136
1137   /// RegisterMCAsmParser - Helper template for registering a target specific
1138   /// assembly parser, for use in the target machine initialization
1139   /// function. Usage:
1140   ///
1141   /// extern "C" void LLVMInitializeFooMCAsmParser() {
1142   ///   extern Target TheFooTarget;
1143   ///   RegisterMCAsmParser<FooAsmParser> X(TheFooTarget);
1144   /// }
1145   template<class MCAsmParserImpl>
1146   struct RegisterMCAsmParser {
1147     RegisterMCAsmParser(Target &T) {
1148       TargetRegistry::RegisterMCAsmParser(T, &Allocator);
1149     }
1150
1151   private:
1152     static MCTargetAsmParser *Allocator(MCSubtargetInfo &STI, MCAsmParser &P,
1153                                         const MCInstrInfo &MII,
1154                                         const MCTargetOptions &Options) {
1155       return new MCAsmParserImpl(STI, P, MII, Options);
1156     }
1157   };
1158
1159   /// RegisterAsmPrinter - Helper template for registering a target specific
1160   /// assembly printer, for use in the target machine initialization
1161   /// function. Usage:
1162   ///
1163   /// extern "C" void LLVMInitializeFooAsmPrinter() {
1164   ///   extern Target TheFooTarget;
1165   ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
1166   /// }
1167   template<class AsmPrinterImpl>
1168   struct RegisterAsmPrinter {
1169     RegisterAsmPrinter(Target &T) {
1170       TargetRegistry::RegisterAsmPrinter(T, &Allocator);
1171     }
1172
1173   private:
1174     static AsmPrinter *Allocator(TargetMachine &TM,
1175                                  std::unique_ptr<MCStreamer> &&Streamer) {
1176       return new AsmPrinterImpl(TM, std::move(Streamer));
1177     }
1178   };
1179
1180   /// RegisterMCCodeEmitter - Helper template for registering a target specific
1181   /// machine code emitter, for use in the target initialization
1182   /// function. Usage:
1183   ///
1184   /// extern "C" void LLVMInitializeFooMCCodeEmitter() {
1185   ///   extern Target TheFooTarget;
1186   ///   RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget);
1187   /// }
1188   template<class MCCodeEmitterImpl>
1189   struct RegisterMCCodeEmitter {
1190     RegisterMCCodeEmitter(Target &T) {
1191       TargetRegistry::RegisterMCCodeEmitter(T, &Allocator);
1192     }
1193
1194   private:
1195     static MCCodeEmitter *Allocator(const MCInstrInfo & /*II*/,
1196                                     const MCRegisterInfo & /*MRI*/,
1197                                     MCContext & /*Ctx*/) {
1198       return new MCCodeEmitterImpl();
1199     }
1200   };
1201
1202 }
1203
1204 #endif