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