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