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