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