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