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