1 //===- MC/TargetRegistry.h - Target Registration ----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file exposes the TargetRegistry interface, which tools can use to access
10 // the appropriate target specific classes (TargetMachine, AsmPrinter, etc.)
11 // which have been registered.
12 //
13 // Target specific class implementations should register themselves using the
14 // appropriate TargetRegistry interfaces.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_MC_TARGETREGISTRY_H
19 #define LLVM_MC_TARGETREGISTRY_H
20 
21 #include "llvm-c/DisassemblerTypes.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/ADT/iterator_range.h"
26 #include "llvm/MC/MCObjectFileInfo.h"
27 #include "llvm/Support/CodeGen.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/FormattedStream.h"
30 #include <cassert>
31 #include <cstddef>
32 #include <iterator>
33 #include <memory>
34 #include <string>
35 
36 namespace llvm {
37 
38 class AsmPrinter;
39 class MCAsmBackend;
40 class MCAsmInfo;
41 class MCAsmParser;
42 class MCCodeEmitter;
43 class MCContext;
44 class MCDisassembler;
45 class MCInstPrinter;
46 class MCInstrAnalysis;
47 class MCInstrInfo;
48 class MCObjectWriter;
49 class MCRegisterInfo;
50 class MCRelocationInfo;
51 class MCStreamer;
52 class MCSubtargetInfo;
53 class MCSymbolizer;
54 class MCTargetAsmParser;
55 class MCTargetOptions;
56 class MCTargetStreamer;
57 class raw_ostream;
58 class TargetMachine;
59 class TargetOptions;
60 namespace mca {
61 class CustomBehaviour;
62 class InstrPostProcess;
63 struct SourceMgr;
64 } // namespace mca
65 
66 MCStreamer *createNullStreamer(MCContext &Ctx);
67 // Takes ownership of \p TAB and \p CE.
68 
69 /// Create a machine code streamer which will print out assembly for the native
70 /// target, suitable for compiling with a native assembler.
71 ///
72 /// \param InstPrint - If given, the instruction printer to use. If not given
73 /// the MCInst representation will be printed.  This method takes ownership of
74 /// InstPrint.
75 ///
76 /// \param CE - If given, a code emitter to use to show the instruction
77 /// encoding inline with the assembly. This method takes ownership of \p CE.
78 ///
79 /// \param TAB - If given, a target asm backend to use to show the fixup
80 /// information in conjunction with encoding information. This method takes
81 /// ownership of \p TAB.
82 ///
83 /// \param ShowInst - Whether to show the MCInst representation inline with
84 /// the assembly.
85 MCStreamer *
86 createAsmStreamer(MCContext &Ctx, std::unique_ptr<formatted_raw_ostream> OS,
87                   bool isVerboseAsm, bool useDwarfDirectory,
88                   MCInstPrinter *InstPrint, std::unique_ptr<MCCodeEmitter> &&CE,
89                   std::unique_ptr<MCAsmBackend> &&TAB, bool ShowInst);
90 
91 MCStreamer *createELFStreamer(MCContext &Ctx,
92                               std::unique_ptr<MCAsmBackend> &&TAB,
93                               std::unique_ptr<MCObjectWriter> &&OW,
94                               std::unique_ptr<MCCodeEmitter> &&CE,
95                               bool RelaxAll);
96 MCStreamer *createMachOStreamer(MCContext &Ctx,
97                                 std::unique_ptr<MCAsmBackend> &&TAB,
98                                 std::unique_ptr<MCObjectWriter> &&OW,
99                                 std::unique_ptr<MCCodeEmitter> &&CE,
100                                 bool RelaxAll, bool DWARFMustBeAtTheEnd,
101                                 bool LabelSections = false);
102 MCStreamer *createWasmStreamer(MCContext &Ctx,
103                                std::unique_ptr<MCAsmBackend> &&TAB,
104                                std::unique_ptr<MCObjectWriter> &&OW,
105                                std::unique_ptr<MCCodeEmitter> &&CE,
106                                bool RelaxAll);
107 MCStreamer *createXCOFFStreamer(MCContext &Ctx,
108                                 std::unique_ptr<MCAsmBackend> &&TAB,
109                                 std::unique_ptr<MCObjectWriter> &&OW,
110                                 std::unique_ptr<MCCodeEmitter> &&CE,
111                                 bool RelaxAll);
112 MCStreamer *createSPIRVStreamer(MCContext &Ctx,
113                                 std::unique_ptr<MCAsmBackend> &&TAB,
114                                 std::unique_ptr<MCObjectWriter> &&OW,
115                                 std::unique_ptr<MCCodeEmitter> &&CE,
116                                 bool RelaxAll);
117 MCStreamer *createDXContainerStreamer(MCContext &Ctx,
118                                       std::unique_ptr<MCAsmBackend> &&TAB,
119                                       std::unique_ptr<MCObjectWriter> &&OW,
120                                       std::unique_ptr<MCCodeEmitter> &&CE,
121                                       bool RelaxAll);
122 
123 MCRelocationInfo *createMCRelocationInfo(const Triple &TT, MCContext &Ctx);
124 
125 MCSymbolizer *createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo,
126                                  LLVMSymbolLookupCallback SymbolLookUp,
127                                  void *DisInfo, MCContext *Ctx,
128                                  std::unique_ptr<MCRelocationInfo> &&RelInfo);
129 
130 mca::CustomBehaviour *createCustomBehaviour(const MCSubtargetInfo &STI,
131                                             const mca::SourceMgr &SrcMgr,
132                                             const MCInstrInfo &MCII);
133 
134 mca::InstrPostProcess *createInstrPostProcess(const MCSubtargetInfo &STI,
135                                               const MCInstrInfo &MCII);
136 
137 /// Target - Wrapper for Target specific information.
138 ///
139 /// For registration purposes, this is a POD type so that targets can be
140 /// registered without the use of static constructors.
141 ///
142 /// Targets should implement a single global instance of this class (which
143 /// will be zero initialized), and pass that instance to the TargetRegistry as
144 /// part of their initialization.
145 class Target {
146 public:
147   friend struct TargetRegistry;
148 
149   using ArchMatchFnTy = bool (*)(Triple::ArchType Arch);
150 
151   using MCAsmInfoCtorFnTy = MCAsmInfo *(*)(const MCRegisterInfo &MRI,
152                                            const Triple &TT,
153                                            const MCTargetOptions &Options);
154   using MCObjectFileInfoCtorFnTy = MCObjectFileInfo *(*)(MCContext &Ctx,
155                                                          bool PIC,
156                                                          bool LargeCodeModel);
157   using MCInstrInfoCtorFnTy = MCInstrInfo *(*)();
158   using MCInstrAnalysisCtorFnTy = MCInstrAnalysis *(*)(const MCInstrInfo *Info);
159   using MCRegInfoCtorFnTy = MCRegisterInfo *(*)(const Triple &TT);
160   using MCSubtargetInfoCtorFnTy = MCSubtargetInfo *(*)(const Triple &TT,
161                                                        StringRef CPU,
162                                                        StringRef Features);
163   using TargetMachineCtorTy = TargetMachine
164       *(*)(const Target &T, const Triple &TT, StringRef CPU, StringRef Features,
165            const TargetOptions &Options, Optional<Reloc::Model> RM,
166            Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT);
167   // If it weren't for layering issues (this header is in llvm/Support, but
168   // depends on MC?) this should take the Streamer by value rather than rvalue
169   // reference.
170   using AsmPrinterCtorTy = AsmPrinter *(*)(
171       TargetMachine &TM, std::unique_ptr<MCStreamer> &&Streamer);
172   using MCAsmBackendCtorTy = MCAsmBackend *(*)(const Target &T,
173                                                const MCSubtargetInfo &STI,
174                                                const MCRegisterInfo &MRI,
175                                                const MCTargetOptions &Options);
176   using MCAsmParserCtorTy = MCTargetAsmParser *(*)(
177       const MCSubtargetInfo &STI, MCAsmParser &P, const MCInstrInfo &MII,
178       const MCTargetOptions &Options);
179   using MCDisassemblerCtorTy = MCDisassembler *(*)(const Target &T,
180                                                    const MCSubtargetInfo &STI,
181                                                    MCContext &Ctx);
182   using MCInstPrinterCtorTy = MCInstPrinter *(*)(const Triple &T,
183                                                  unsigned SyntaxVariant,
184                                                  const MCAsmInfo &MAI,
185                                                  const MCInstrInfo &MII,
186                                                  const MCRegisterInfo &MRI);
187   using MCCodeEmitterCtorTy = MCCodeEmitter *(*)(const MCInstrInfo &II,
188                                                  MCContext &Ctx);
189   using ELFStreamerCtorTy =
190       MCStreamer *(*)(const Triple &T, MCContext &Ctx,
191                       std::unique_ptr<MCAsmBackend> &&TAB,
192                       std::unique_ptr<MCObjectWriter> &&OW,
193                       std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
194   using MachOStreamerCtorTy =
195       MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB,
196                       std::unique_ptr<MCObjectWriter> &&OW,
197                       std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
198                       bool DWARFMustBeAtTheEnd);
199   using COFFStreamerCtorTy =
200       MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB,
201                       std::unique_ptr<MCObjectWriter> &&OW,
202                       std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
203                       bool IncrementalLinkerCompatible);
204   using WasmStreamerCtorTy =
205       MCStreamer *(*)(const Triple &T, MCContext &Ctx,
206                       std::unique_ptr<MCAsmBackend> &&TAB,
207                       std::unique_ptr<MCObjectWriter> &&OW,
208                       std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
209   using XCOFFStreamerCtorTy =
210       MCStreamer *(*)(const Triple &T, MCContext &Ctx,
211                       std::unique_ptr<MCAsmBackend> &&TAB,
212                       std::unique_ptr<MCObjectWriter> &&OW,
213                       std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
214   using SPIRVStreamerCtorTy =
215       MCStreamer *(*)(const Triple &T, MCContext &Ctx,
216                       std::unique_ptr<MCAsmBackend> &&TAB,
217                       std::unique_ptr<MCObjectWriter> &&OW,
218                       std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
219 
220   using DXContainerStreamerCtorTy =
221       MCStreamer *(*)(const Triple &T, MCContext &Ctx,
222                       std::unique_ptr<MCAsmBackend> &&TAB,
223                       std::unique_ptr<MCObjectWriter> &&OW,
224                       std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
225 
226   using NullTargetStreamerCtorTy = MCTargetStreamer *(*)(MCStreamer &S);
227   using AsmTargetStreamerCtorTy = MCTargetStreamer *(*)(
228       MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint,
229       bool IsVerboseAsm);
230   using ObjectTargetStreamerCtorTy = MCTargetStreamer *(*)(
231       MCStreamer &S, const MCSubtargetInfo &STI);
232   using MCRelocationInfoCtorTy = MCRelocationInfo *(*)(const Triple &TT,
233                                                        MCContext &Ctx);
234   using MCSymbolizerCtorTy = MCSymbolizer *(*)(
235       const Triple &TT, LLVMOpInfoCallback GetOpInfo,
236       LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx,
237       std::unique_ptr<MCRelocationInfo> &&RelInfo);
238 
239   using CustomBehaviourCtorTy =
240       mca::CustomBehaviour *(*)(const MCSubtargetInfo &STI,
241                                 const mca::SourceMgr &SrcMgr,
242                                 const MCInstrInfo &MCII);
243 
244   using InstrPostProcessCtorTy =
245       mca::InstrPostProcess *(*)(const MCSubtargetInfo &STI,
246                                  const MCInstrInfo &MCII);
247 
248 private:
249   /// Next - The next registered target in the linked list, maintained by the
250   /// TargetRegistry.
251   Target *Next;
252 
253   /// The target function for checking if an architecture is supported.
254   ArchMatchFnTy ArchMatchFn;
255 
256   /// Name - The target name.
257   const char *Name;
258 
259   /// ShortDesc - A short description of the target.
260   const char *ShortDesc;
261 
262   /// BackendName - The name of the backend implementation. This must match the
263   /// name of the 'def X : Target ...' in TableGen.
264   const char *BackendName;
265 
266   /// HasJIT - Whether this target supports the JIT.
267   bool HasJIT;
268 
269   /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
270   /// registered.
271   MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
272 
273   /// Constructor function for this target's MCObjectFileInfo, if registered.
274   MCObjectFileInfoCtorFnTy MCObjectFileInfoCtorFn;
275 
276   /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
277   /// if registered.
278   MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
279 
280   /// MCInstrAnalysisCtorFn - Constructor function for this target's
281   /// MCInstrAnalysis, if registered.
282   MCInstrAnalysisCtorFnTy MCInstrAnalysisCtorFn;
283 
284   /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
285   /// if registered.
286   MCRegInfoCtorFnTy MCRegInfoCtorFn;
287 
288   /// MCSubtargetInfoCtorFn - Constructor function for this target's
289   /// MCSubtargetInfo, if registered.
290   MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn;
291 
292   /// TargetMachineCtorFn - Construction function for this target's
293   /// TargetMachine, if registered.
294   TargetMachineCtorTy TargetMachineCtorFn;
295 
296   /// MCAsmBackendCtorFn - Construction function for this target's
297   /// MCAsmBackend, if registered.
298   MCAsmBackendCtorTy MCAsmBackendCtorFn;
299 
300   /// MCAsmParserCtorFn - Construction function for this target's
301   /// MCTargetAsmParser, if registered.
302   MCAsmParserCtorTy MCAsmParserCtorFn;
303 
304   /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
305   /// if registered.
306   AsmPrinterCtorTy AsmPrinterCtorFn;
307 
308   /// MCDisassemblerCtorFn - Construction function for this target's
309   /// MCDisassembler, if registered.
310   MCDisassemblerCtorTy MCDisassemblerCtorFn;
311 
312   /// MCInstPrinterCtorFn - Construction function for this target's
313   /// MCInstPrinter, if registered.
314   MCInstPrinterCtorTy MCInstPrinterCtorFn;
315 
316   /// MCCodeEmitterCtorFn - Construction function for this target's
317   /// CodeEmitter, if registered.
318   MCCodeEmitterCtorTy MCCodeEmitterCtorFn;
319 
320   // Construction functions for the various object formats, if registered.
321   COFFStreamerCtorTy COFFStreamerCtorFn = nullptr;
322   MachOStreamerCtorTy MachOStreamerCtorFn = nullptr;
323   ELFStreamerCtorTy ELFStreamerCtorFn = nullptr;
324   WasmStreamerCtorTy WasmStreamerCtorFn = nullptr;
325   XCOFFStreamerCtorTy XCOFFStreamerCtorFn = nullptr;
326   SPIRVStreamerCtorTy SPIRVStreamerCtorFn = nullptr;
327   DXContainerStreamerCtorTy DXContainerStreamerCtorFn = nullptr;
328 
329   /// Construction function for this target's null TargetStreamer, if
330   /// registered (default = nullptr).
331   NullTargetStreamerCtorTy NullTargetStreamerCtorFn = nullptr;
332 
333   /// Construction function for this target's asm TargetStreamer, if
334   /// registered (default = nullptr).
335   AsmTargetStreamerCtorTy AsmTargetStreamerCtorFn = nullptr;
336 
337   /// Construction function for this target's obj TargetStreamer, if
338   /// registered (default = nullptr).
339   ObjectTargetStreamerCtorTy ObjectTargetStreamerCtorFn = nullptr;
340 
341   /// MCRelocationInfoCtorFn - Construction function for this target's
342   /// MCRelocationInfo, if registered (default = llvm::createMCRelocationInfo)
343   MCRelocationInfoCtorTy MCRelocationInfoCtorFn = nullptr;
344 
345   /// MCSymbolizerCtorFn - Construction function for this target's
346   /// MCSymbolizer, if registered (default = llvm::createMCSymbolizer)
347   MCSymbolizerCtorTy MCSymbolizerCtorFn = nullptr;
348 
349   /// CustomBehaviourCtorFn - Construction function for this target's
350   /// CustomBehaviour, if registered (default = nullptr).
351   CustomBehaviourCtorTy CustomBehaviourCtorFn = nullptr;
352 
353   /// InstrPostProcessCtorFn - Construction function for this target's
354   /// InstrPostProcess, if registered (default = nullptr).
355   InstrPostProcessCtorTy InstrPostProcessCtorFn = nullptr;
356 
357 public:
358   Target() = default;
359 
360   /// @name Target Information
361   /// @{
362 
363   // getNext - Return the next registered target.
364   const Target *getNext() const { return Next; }
365 
366   /// getName - Get the target name.
367   const char *getName() const { return Name; }
368 
369   /// getShortDescription - Get a short description of the target.
370   const char *getShortDescription() const { return ShortDesc; }
371 
372   /// getBackendName - Get the backend name.
373   const char *getBackendName() const { return BackendName; }
374 
375   /// @}
376   /// @name Feature Predicates
377   /// @{
378 
379   /// hasJIT - Check if this targets supports the just-in-time compilation.
380   bool hasJIT() const { return HasJIT; }
381 
382   /// hasTargetMachine - Check if this target supports code generation.
383   bool hasTargetMachine() const { return TargetMachineCtorFn != nullptr; }
384 
385   /// hasMCAsmBackend - Check if this target supports .o generation.
386   bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != nullptr; }
387 
388   /// hasMCAsmParser - Check if this target supports assembly parsing.
389   bool hasMCAsmParser() const { return MCAsmParserCtorFn != nullptr; }
390 
391   /// @}
392   /// @name Feature Constructors
393   /// @{
394 
395   /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
396   /// target triple.
397   ///
398   /// \param TheTriple This argument is used to determine the target machine
399   /// feature set; it should always be provided. Generally this should be
400   /// either the target triple from the module, or the target triple of the
401   /// host if that does not exist.
402   MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI, StringRef TheTriple,
403                              const MCTargetOptions &Options) const {
404     if (!MCAsmInfoCtorFn)
405       return nullptr;
406     return MCAsmInfoCtorFn(MRI, Triple(TheTriple), Options);
407   }
408 
409   /// Create a MCObjectFileInfo implementation for the specified target
410   /// triple.
411   ///
412   MCObjectFileInfo *createMCObjectFileInfo(MCContext &Ctx, bool PIC,
413                                            bool LargeCodeModel = false) const {
414     if (!MCObjectFileInfoCtorFn) {
415       MCObjectFileInfo *MOFI = new MCObjectFileInfo();
416       MOFI->initMCObjectFileInfo(Ctx, PIC, LargeCodeModel);
417       return MOFI;
418     }
419     return MCObjectFileInfoCtorFn(Ctx, PIC, LargeCodeModel);
420   }
421 
422   /// createMCInstrInfo - Create a MCInstrInfo implementation.
423   ///
424   MCInstrInfo *createMCInstrInfo() const {
425     if (!MCInstrInfoCtorFn)
426       return nullptr;
427     return MCInstrInfoCtorFn();
428   }
429 
430   /// createMCInstrAnalysis - Create a MCInstrAnalysis implementation.
431   ///
432   MCInstrAnalysis *createMCInstrAnalysis(const MCInstrInfo *Info) const {
433     if (!MCInstrAnalysisCtorFn)
434       return nullptr;
435     return MCInstrAnalysisCtorFn(Info);
436   }
437 
438   /// createMCRegInfo - Create a MCRegisterInfo implementation.
439   ///
440   MCRegisterInfo *createMCRegInfo(StringRef TT) const {
441     if (!MCRegInfoCtorFn)
442       return nullptr;
443     return MCRegInfoCtorFn(Triple(TT));
444   }
445 
446   /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
447   ///
448   /// \param TheTriple This argument is used to determine the target machine
449   /// feature set; it should always be provided. Generally this should be
450   /// either the target triple from the module, or the target triple of the
451   /// host if that does not exist.
452   /// \param CPU This specifies the name of the target CPU.
453   /// \param Features This specifies the string representation of the
454   /// additional target features.
455   MCSubtargetInfo *createMCSubtargetInfo(StringRef TheTriple, StringRef CPU,
456                                          StringRef Features) const {
457     if (!MCSubtargetInfoCtorFn)
458       return nullptr;
459     return MCSubtargetInfoCtorFn(Triple(TheTriple), CPU, Features);
460   }
461 
462   /// createTargetMachine - Create a target specific machine implementation
463   /// for the specified \p Triple.
464   ///
465   /// \param TT This argument is used to determine the target machine
466   /// feature set; it should always be provided. Generally this should be
467   /// either the target triple from the module, or the target triple of the
468   /// host if that does not exist.
469   TargetMachine *createTargetMachine(StringRef TT, StringRef CPU,
470                                      StringRef Features,
471                                      const TargetOptions &Options,
472                                      Optional<Reloc::Model> RM,
473                                      Optional<CodeModel::Model> CM = None,
474                                      CodeGenOpt::Level OL = CodeGenOpt::Default,
475                                      bool JIT = false) const {
476     if (!TargetMachineCtorFn)
477       return nullptr;
478     return TargetMachineCtorFn(*this, Triple(TT), CPU, Features, Options, RM,
479                                CM, OL, JIT);
480   }
481 
482   /// createMCAsmBackend - Create a target specific assembly parser.
483   MCAsmBackend *createMCAsmBackend(const MCSubtargetInfo &STI,
484                                    const MCRegisterInfo &MRI,
485                                    const MCTargetOptions &Options) const {
486     if (!MCAsmBackendCtorFn)
487       return nullptr;
488     return MCAsmBackendCtorFn(*this, STI, MRI, Options);
489   }
490 
491   /// createMCAsmParser - Create a target specific assembly parser.
492   ///
493   /// \param Parser The target independent parser implementation to use for
494   /// parsing and lexing.
495   MCTargetAsmParser *createMCAsmParser(const MCSubtargetInfo &STI,
496                                        MCAsmParser &Parser,
497                                        const MCInstrInfo &MII,
498                                        const MCTargetOptions &Options) const {
499     if (!MCAsmParserCtorFn)
500       return nullptr;
501     return MCAsmParserCtorFn(STI, Parser, MII, Options);
502   }
503 
504   /// createAsmPrinter - Create a target specific assembly printer pass.  This
505   /// takes ownership of the MCStreamer object.
506   AsmPrinter *createAsmPrinter(TargetMachine &TM,
507                                std::unique_ptr<MCStreamer> &&Streamer) const {
508     if (!AsmPrinterCtorFn)
509       return nullptr;
510     return AsmPrinterCtorFn(TM, std::move(Streamer));
511   }
512 
513   MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI,
514                                        MCContext &Ctx) const {
515     if (!MCDisassemblerCtorFn)
516       return nullptr;
517     return MCDisassemblerCtorFn(*this, STI, Ctx);
518   }
519 
520   MCInstPrinter *createMCInstPrinter(const Triple &T, unsigned SyntaxVariant,
521                                      const MCAsmInfo &MAI,
522                                      const MCInstrInfo &MII,
523                                      const MCRegisterInfo &MRI) const {
524     if (!MCInstPrinterCtorFn)
525       return nullptr;
526     return MCInstPrinterCtorFn(T, SyntaxVariant, MAI, MII, MRI);
527   }
528 
529   /// createMCCodeEmitter - Create a target specific code emitter.
530   MCCodeEmitter *createMCCodeEmitter(const MCInstrInfo &II,
531                                      MCContext &Ctx) const {
532     if (!MCCodeEmitterCtorFn)
533       return nullptr;
534     return MCCodeEmitterCtorFn(II, Ctx);
535   }
536 
537   /// Create a target specific MCStreamer.
538   ///
539   /// \param T The target triple.
540   /// \param Ctx The target context.
541   /// \param TAB The target assembler backend object. Takes ownership.
542   /// \param OW The stream object.
543   /// \param Emitter The target independent assembler object.Takes ownership.
544   /// \param RelaxAll Relax all fixups?
545   MCStreamer *createMCObjectStreamer(const Triple &T, MCContext &Ctx,
546                                      std::unique_ptr<MCAsmBackend> &&TAB,
547                                      std::unique_ptr<MCObjectWriter> &&OW,
548                                      std::unique_ptr<MCCodeEmitter> &&Emitter,
549                                      const MCSubtargetInfo &STI, bool RelaxAll,
550                                      bool IncrementalLinkerCompatible,
551                                      bool DWARFMustBeAtTheEnd) const {
552     MCStreamer *S = nullptr;
553     switch (T.getObjectFormat()) {
554     case Triple::UnknownObjectFormat:
555       llvm_unreachable("Unknown object format");
556     case Triple::COFF:
557       assert(T.isOSWindows() && "only Windows COFF is supported");
558       S = COFFStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
559                              std::move(Emitter), RelaxAll,
560                              IncrementalLinkerCompatible);
561       break;
562     case Triple::MachO:
563       if (MachOStreamerCtorFn)
564         S = MachOStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
565                                 std::move(Emitter), RelaxAll,
566                                 DWARFMustBeAtTheEnd);
567       else
568         S = createMachOStreamer(Ctx, std::move(TAB), std::move(OW),
569                                 std::move(Emitter), RelaxAll,
570                                 DWARFMustBeAtTheEnd);
571       break;
572     case Triple::ELF:
573       if (ELFStreamerCtorFn)
574         S = ELFStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
575                               std::move(Emitter), RelaxAll);
576       else
577         S = createELFStreamer(Ctx, std::move(TAB), std::move(OW),
578                               std::move(Emitter), RelaxAll);
579       break;
580     case Triple::Wasm:
581       if (WasmStreamerCtorFn)
582         S = WasmStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
583                                std::move(Emitter), RelaxAll);
584       else
585         S = createWasmStreamer(Ctx, std::move(TAB), std::move(OW),
586                                std::move(Emitter), RelaxAll);
587       break;
588     case Triple::GOFF:
589       report_fatal_error("GOFF MCObjectStreamer not implemented yet");
590     case Triple::XCOFF:
591       if (XCOFFStreamerCtorFn)
592         S = XCOFFStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
593                                 std::move(Emitter), RelaxAll);
594       else
595         S = createXCOFFStreamer(Ctx, std::move(TAB), std::move(OW),
596                                 std::move(Emitter), RelaxAll);
597       break;
598     case Triple::SPIRV:
599       if (SPIRVStreamerCtorFn)
600         S = SPIRVStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
601                                 std::move(Emitter), RelaxAll);
602       else
603         S = createSPIRVStreamer(Ctx, std::move(TAB), std::move(OW),
604                                 std::move(Emitter), RelaxAll);
605       break;
606     case Triple::DXContainer:
607       if (DXContainerStreamerCtorFn)
608         S = DXContainerStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
609                               std::move(Emitter), RelaxAll);
610       else
611         S = createDXContainerStreamer(Ctx, std::move(TAB), std::move(OW),
612                                       std::move(Emitter), RelaxAll);
613       break;
614     }
615     if (ObjectTargetStreamerCtorFn)
616       ObjectTargetStreamerCtorFn(*S, STI);
617     return S;
618   }
619 
620   MCStreamer *createAsmStreamer(MCContext &Ctx,
621                                 std::unique_ptr<formatted_raw_ostream> OS,
622                                 bool IsVerboseAsm, bool UseDwarfDirectory,
623                                 MCInstPrinter *InstPrint,
624                                 std::unique_ptr<MCCodeEmitter> &&CE,
625                                 std::unique_ptr<MCAsmBackend> &&TAB,
626                                 bool ShowInst) const {
627     formatted_raw_ostream &OSRef = *OS;
628     MCStreamer *S = llvm::createAsmStreamer(
629         Ctx, std::move(OS), IsVerboseAsm, UseDwarfDirectory, InstPrint,
630         std::move(CE), std::move(TAB), ShowInst);
631     createAsmTargetStreamer(*S, OSRef, InstPrint, IsVerboseAsm);
632     return S;
633   }
634 
635   MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
636                                             formatted_raw_ostream &OS,
637                                             MCInstPrinter *InstPrint,
638                                             bool IsVerboseAsm) const {
639     if (AsmTargetStreamerCtorFn)
640       return AsmTargetStreamerCtorFn(S, OS, InstPrint, IsVerboseAsm);
641     return nullptr;
642   }
643 
644   MCStreamer *createNullStreamer(MCContext &Ctx) const {
645     MCStreamer *S = llvm::createNullStreamer(Ctx);
646     createNullTargetStreamer(*S);
647     return S;
648   }
649 
650   MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) const {
651     if (NullTargetStreamerCtorFn)
652       return NullTargetStreamerCtorFn(S);
653     return nullptr;
654   }
655 
656   /// createMCRelocationInfo - Create a target specific MCRelocationInfo.
657   ///
658   /// \param TT The target triple.
659   /// \param Ctx The target context.
660   MCRelocationInfo *createMCRelocationInfo(StringRef TT, MCContext &Ctx) const {
661     MCRelocationInfoCtorTy Fn = MCRelocationInfoCtorFn
662                                     ? MCRelocationInfoCtorFn
663                                     : llvm::createMCRelocationInfo;
664     return Fn(Triple(TT), Ctx);
665   }
666 
667   /// createMCSymbolizer - Create a target specific MCSymbolizer.
668   ///
669   /// \param TT The target triple.
670   /// \param GetOpInfo The function to get the symbolic information for
671   /// operands.
672   /// \param SymbolLookUp The function to lookup a symbol name.
673   /// \param DisInfo The pointer to the block of symbolic information for above
674   /// call
675   /// back.
676   /// \param Ctx The target context.
677   /// \param RelInfo The relocation information for this target. Takes
678   /// ownership.
679   MCSymbolizer *
680   createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
681                      LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo,
682                      MCContext *Ctx,
683                      std::unique_ptr<MCRelocationInfo> &&RelInfo) const {
684     MCSymbolizerCtorTy Fn =
685         MCSymbolizerCtorFn ? MCSymbolizerCtorFn : llvm::createMCSymbolizer;
686     return Fn(Triple(TT), GetOpInfo, SymbolLookUp, DisInfo, Ctx,
687               std::move(RelInfo));
688   }
689 
690   /// createCustomBehaviour - Create a target specific CustomBehaviour.
691   /// This class is used by llvm-mca and requires backend functionality.
692   mca::CustomBehaviour *createCustomBehaviour(const MCSubtargetInfo &STI,
693                                               const mca::SourceMgr &SrcMgr,
694                                               const MCInstrInfo &MCII) const {
695     if (CustomBehaviourCtorFn)
696       return CustomBehaviourCtorFn(STI, SrcMgr, MCII);
697     return nullptr;
698   }
699 
700   /// createInstrPostProcess - Create a target specific InstrPostProcess.
701   /// This class is used by llvm-mca and requires backend functionality.
702   mca::InstrPostProcess *createInstrPostProcess(const MCSubtargetInfo &STI,
703                                                 const MCInstrInfo &MCII) const {
704     if (InstrPostProcessCtorFn)
705       return InstrPostProcessCtorFn(STI, MCII);
706     return nullptr;
707   }
708 
709   /// @}
710 };
711 
712 /// TargetRegistry - Generic interface to target specific features.
713 struct TargetRegistry {
714   // FIXME: Make this a namespace, probably just move all the Register*
715   // functions into Target (currently they all just set members on the Target
716   // anyway, and Target friends this class so those functions can...
717   // function).
718   TargetRegistry() = delete;
719 
720   class iterator {
721     friend struct TargetRegistry;
722 
723     const Target *Current = nullptr;
724 
725     explicit iterator(Target *T) : Current(T) {}
726 
727   public:
728     using iterator_category = std::forward_iterator_tag;
729     using value_type = Target;
730     using difference_type = std::ptrdiff_t;
731     using pointer = value_type *;
732     using reference = value_type &;
733 
734     iterator() = default;
735 
736     bool operator==(const iterator &x) const { return Current == x.Current; }
737     bool operator!=(const iterator &x) const { return !operator==(x); }
738 
739     // Iterator traversal: forward iteration only
740     iterator &operator++() { // Preincrement
741       assert(Current && "Cannot increment end iterator!");
742       Current = Current->getNext();
743       return *this;
744     }
745     iterator operator++(int) { // Postincrement
746       iterator tmp = *this;
747       ++*this;
748       return tmp;
749     }
750 
751     const Target &operator*() const {
752       assert(Current && "Cannot dereference end iterator!");
753       return *Current;
754     }
755 
756     const Target *operator->() const { return &operator*(); }
757   };
758 
759   /// printRegisteredTargetsForVersion - Print the registered targets
760   /// appropriately for inclusion in a tool's version output.
761   static void printRegisteredTargetsForVersion(raw_ostream &OS);
762 
763   /// @name Registry Access
764   /// @{
765 
766   static iterator_range<iterator> targets();
767 
768   /// lookupTarget - Lookup a target based on a target triple.
769   ///
770   /// \param Triple - The triple to use for finding a target.
771   /// \param Error - On failure, an error string describing why no target was
772   /// found.
773   static const Target *lookupTarget(const std::string &Triple,
774                                     std::string &Error);
775 
776   /// lookupTarget - Lookup a target based on an architecture name
777   /// and a target triple.  If the architecture name is non-empty,
778   /// then the lookup is done by architecture.  Otherwise, the target
779   /// triple is used.
780   ///
781   /// \param ArchName - The architecture to use for finding a target.
782   /// \param TheTriple - The triple to use for finding a target.  The
783   /// triple is updated with canonical architecture name if a lookup
784   /// by architecture is done.
785   /// \param Error - On failure, an error string describing why no target was
786   /// found.
787   static const Target *lookupTarget(const std::string &ArchName,
788                                     Triple &TheTriple, std::string &Error);
789 
790   /// @}
791   /// @name Target Registration
792   /// @{
793 
794   /// RegisterTarget - Register the given target. Attempts to register a
795   /// target which has already been registered will be ignored.
796   ///
797   /// Clients are responsible for ensuring that registration doesn't occur
798   /// while another thread is attempting to access the registry. Typically
799   /// this is done by initializing all targets at program startup.
800   ///
801   /// @param T - The target being registered.
802   /// @param Name - The target name. This should be a static string.
803   /// @param ShortDesc - A short target description. This should be a static
804   /// string.
805   /// @param BackendName - The name of the backend. This should be a static
806   /// string that is the same for all targets that share a backend
807   /// implementation and must match the name used in the 'def X : Target ...' in
808   /// TableGen.
809   /// @param ArchMatchFn - The arch match checking function for this target.
810   /// @param HasJIT - Whether the target supports JIT code
811   /// generation.
812   static void RegisterTarget(Target &T, const char *Name, const char *ShortDesc,
813                              const char *BackendName,
814                              Target::ArchMatchFnTy ArchMatchFn,
815                              bool HasJIT = false);
816 
817   /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
818   /// given target.
819   ///
820   /// Clients are responsible for ensuring that registration doesn't occur
821   /// while another thread is attempting to access the registry. Typically
822   /// this is done by initializing all targets at program startup.
823   ///
824   /// @param T - The target being registered.
825   /// @param Fn - A function to construct a MCAsmInfo for the target.
826   static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
827     T.MCAsmInfoCtorFn = Fn;
828   }
829 
830   /// Register a MCObjectFileInfo implementation for the given target.
831   ///
832   /// Clients are responsible for ensuring that registration doesn't occur
833   /// while another thread is attempting to access the registry. Typically
834   /// this is done by initializing all targets at program startup.
835   ///
836   /// @param T - The target being registered.
837   /// @param Fn - A function to construct a MCObjectFileInfo for the target.
838   static void RegisterMCObjectFileInfo(Target &T,
839                                        Target::MCObjectFileInfoCtorFnTy Fn) {
840     T.MCObjectFileInfoCtorFn = Fn;
841   }
842 
843   /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
844   /// given target.
845   ///
846   /// Clients are responsible for ensuring that registration doesn't occur
847   /// while another thread is attempting to access the registry. Typically
848   /// this is done by initializing all targets at program startup.
849   ///
850   /// @param T - The target being registered.
851   /// @param Fn - A function to construct a MCInstrInfo for the target.
852   static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
853     T.MCInstrInfoCtorFn = Fn;
854   }
855 
856   /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for
857   /// the given target.
858   static void RegisterMCInstrAnalysis(Target &T,
859                                       Target::MCInstrAnalysisCtorFnTy Fn) {
860     T.MCInstrAnalysisCtorFn = Fn;
861   }
862 
863   /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
864   /// given target.
865   ///
866   /// Clients are responsible for ensuring that registration doesn't occur
867   /// while another thread is attempting to access the registry. Typically
868   /// this is done by initializing all targets at program startup.
869   ///
870   /// @param T - The target being registered.
871   /// @param Fn - A function to construct a MCRegisterInfo for the target.
872   static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
873     T.MCRegInfoCtorFn = Fn;
874   }
875 
876   /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
877   /// the given target.
878   ///
879   /// Clients are responsible for ensuring that registration doesn't occur
880   /// while another thread is attempting to access the registry. Typically
881   /// this is done by initializing all targets at program startup.
882   ///
883   /// @param T - The target being registered.
884   /// @param Fn - A function to construct a MCSubtargetInfo for the target.
885   static void RegisterMCSubtargetInfo(Target &T,
886                                       Target::MCSubtargetInfoCtorFnTy Fn) {
887     T.MCSubtargetInfoCtorFn = Fn;
888   }
889 
890   /// RegisterTargetMachine - Register a TargetMachine implementation for the
891   /// given target.
892   ///
893   /// Clients are responsible for ensuring that registration doesn't occur
894   /// while another thread is attempting to access the registry. Typically
895   /// this is done by initializing all targets at program startup.
896   ///
897   /// @param T - The target being registered.
898   /// @param Fn - A function to construct a TargetMachine for the target.
899   static void RegisterTargetMachine(Target &T, Target::TargetMachineCtorTy Fn) {
900     T.TargetMachineCtorFn = Fn;
901   }
902 
903   /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the
904   /// given target.
905   ///
906   /// Clients are responsible for ensuring that registration doesn't occur
907   /// while another thread is attempting to access the registry. Typically
908   /// this is done by initializing all targets at program startup.
909   ///
910   /// @param T - The target being registered.
911   /// @param Fn - A function to construct an AsmBackend for the target.
912   static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) {
913     T.MCAsmBackendCtorFn = Fn;
914   }
915 
916   /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for
917   /// the given target.
918   ///
919   /// Clients are responsible for ensuring that registration doesn't occur
920   /// while another thread is attempting to access the registry. Typically
921   /// this is done by initializing all targets at program startup.
922   ///
923   /// @param T - The target being registered.
924   /// @param Fn - A function to construct an MCTargetAsmParser for the target.
925   static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) {
926     T.MCAsmParserCtorFn = Fn;
927   }
928 
929   /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
930   /// target.
931   ///
932   /// Clients are responsible for ensuring that registration doesn't occur
933   /// while another thread is attempting to access the registry. Typically
934   /// this is done by initializing all targets at program startup.
935   ///
936   /// @param T - The target being registered.
937   /// @param Fn - A function to construct an AsmPrinter for the target.
938   static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
939     T.AsmPrinterCtorFn = Fn;
940   }
941 
942   /// RegisterMCDisassembler - Register a MCDisassembler implementation for
943   /// the given target.
944   ///
945   /// Clients are responsible for ensuring that registration doesn't occur
946   /// while another thread is attempting to access the registry. Typically
947   /// this is done by initializing all targets at program startup.
948   ///
949   /// @param T - The target being registered.
950   /// @param Fn - A function to construct an MCDisassembler for the target.
951   static void RegisterMCDisassembler(Target &T,
952                                      Target::MCDisassemblerCtorTy Fn) {
953     T.MCDisassemblerCtorFn = Fn;
954   }
955 
956   /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
957   /// given target.
958   ///
959   /// Clients are responsible for ensuring that registration doesn't occur
960   /// while another thread is attempting to access the registry. Typically
961   /// this is done by initializing all targets at program startup.
962   ///
963   /// @param T - The target being registered.
964   /// @param Fn - A function to construct an MCInstPrinter for the target.
965   static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn) {
966     T.MCInstPrinterCtorFn = Fn;
967   }
968 
969   /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the
970   /// given target.
971   ///
972   /// Clients are responsible for ensuring that registration doesn't occur
973   /// while another thread is attempting to access the registry. Typically
974   /// this is done by initializing all targets at program startup.
975   ///
976   /// @param T - The target being registered.
977   /// @param Fn - A function to construct an MCCodeEmitter for the target.
978   static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn) {
979     T.MCCodeEmitterCtorFn = Fn;
980   }
981 
982   static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn) {
983     T.COFFStreamerCtorFn = Fn;
984   }
985 
986   static void RegisterMachOStreamer(Target &T, Target::MachOStreamerCtorTy Fn) {
987     T.MachOStreamerCtorFn = Fn;
988   }
989 
990   static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn) {
991     T.ELFStreamerCtorFn = Fn;
992   }
993 
994   static void RegisterSPIRVStreamer(Target &T, Target::SPIRVStreamerCtorTy Fn) {
995     T.SPIRVStreamerCtorFn = Fn;
996   }
997 
998   static void RegisterDXContainerStreamer(Target &T, Target::DXContainerStreamerCtorTy Fn) {
999     T.DXContainerStreamerCtorFn = Fn;
1000   }
1001 
1002   static void RegisterWasmStreamer(Target &T, Target::WasmStreamerCtorTy Fn) {
1003     T.WasmStreamerCtorFn = Fn;
1004   }
1005 
1006   static void RegisterXCOFFStreamer(Target &T, Target::XCOFFStreamerCtorTy Fn) {
1007     T.XCOFFStreamerCtorFn = Fn;
1008   }
1009 
1010   static void RegisterNullTargetStreamer(Target &T,
1011                                          Target::NullTargetStreamerCtorTy Fn) {
1012     T.NullTargetStreamerCtorFn = Fn;
1013   }
1014 
1015   static void RegisterAsmTargetStreamer(Target &T,
1016                                         Target::AsmTargetStreamerCtorTy Fn) {
1017     T.AsmTargetStreamerCtorFn = Fn;
1018   }
1019 
1020   static void
1021   RegisterObjectTargetStreamer(Target &T,
1022                                Target::ObjectTargetStreamerCtorTy Fn) {
1023     T.ObjectTargetStreamerCtorFn = Fn;
1024   }
1025 
1026   /// RegisterMCRelocationInfo - Register an MCRelocationInfo
1027   /// implementation for the given target.
1028   ///
1029   /// Clients are responsible for ensuring that registration doesn't occur
1030   /// while another thread is attempting to access the registry. Typically
1031   /// this is done by initializing all targets at program startup.
1032   ///
1033   /// @param T - The target being registered.
1034   /// @param Fn - A function to construct an MCRelocationInfo for the target.
1035   static void RegisterMCRelocationInfo(Target &T,
1036                                        Target::MCRelocationInfoCtorTy Fn) {
1037     T.MCRelocationInfoCtorFn = Fn;
1038   }
1039 
1040   /// RegisterMCSymbolizer - Register an MCSymbolizer
1041   /// implementation for the given target.
1042   ///
1043   /// Clients are responsible for ensuring that registration doesn't occur
1044   /// while another thread is attempting to access the registry. Typically
1045   /// this is done by initializing all targets at program startup.
1046   ///
1047   /// @param T - The target being registered.
1048   /// @param Fn - A function to construct an MCSymbolizer for the target.
1049   static void RegisterMCSymbolizer(Target &T, Target::MCSymbolizerCtorTy Fn) {
1050     T.MCSymbolizerCtorFn = Fn;
1051   }
1052 
1053   /// RegisterCustomBehaviour - Register a CustomBehaviour
1054   /// implementation for the given target.
1055   ///
1056   /// Clients are responsible for ensuring that registration doesn't occur
1057   /// while another thread is attempting to access the registry. Typically
1058   /// this is done by initializing all targets at program startup.
1059   ///
1060   /// @param T - The target being registered.
1061   /// @param Fn - A function to construct a CustomBehaviour for the target.
1062   static void RegisterCustomBehaviour(Target &T,
1063                                       Target::CustomBehaviourCtorTy Fn) {
1064     T.CustomBehaviourCtorFn = Fn;
1065   }
1066 
1067   /// RegisterInstrPostProcess - Register an InstrPostProcess
1068   /// implementation for the given target.
1069   ///
1070   /// Clients are responsible for ensuring that registration doesn't occur
1071   /// while another thread is attempting to access the registry. Typically
1072   /// this is done by initializing all targets at program startup.
1073   ///
1074   /// @param T - The target being registered.
1075   /// @param Fn - A function to construct an InstrPostProcess for the target.
1076   static void RegisterInstrPostProcess(Target &T,
1077                                        Target::InstrPostProcessCtorTy Fn) {
1078     T.InstrPostProcessCtorFn = Fn;
1079   }
1080 
1081   /// @}
1082 };
1083 
1084 //===--------------------------------------------------------------------===//
1085 
1086 /// RegisterTarget - Helper template for registering a target, for use in the
1087 /// target's initialization function. Usage:
1088 ///
1089 ///
1090 /// Target &getTheFooTarget() { // The global target instance.
1091 ///   static Target TheFooTarget;
1092 ///   return TheFooTarget;
1093 /// }
1094 /// extern "C" void LLVMInitializeFooTargetInfo() {
1095 ///   RegisterTarget<Triple::foo> X(getTheFooTarget(), "foo", "Foo
1096 ///   description", "Foo" /* Backend Name */);
1097 /// }
1098 template <Triple::ArchType TargetArchType = Triple::UnknownArch,
1099           bool HasJIT = false>
1100 struct RegisterTarget {
1101   RegisterTarget(Target &T, const char *Name, const char *Desc,
1102                  const char *BackendName) {
1103     TargetRegistry::RegisterTarget(T, Name, Desc, BackendName, &getArchMatch,
1104                                    HasJIT);
1105   }
1106 
1107   static bool getArchMatch(Triple::ArchType Arch) {
1108     return Arch == TargetArchType;
1109   }
1110 };
1111 
1112 /// RegisterMCAsmInfo - Helper template for registering a target assembly info
1113 /// implementation.  This invokes the static "Create" method on the class to
1114 /// actually do the construction.  Usage:
1115 ///
1116 /// extern "C" void LLVMInitializeFooTarget() {
1117 ///   extern Target TheFooTarget;
1118 ///   RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
1119 /// }
1120 template <class MCAsmInfoImpl> struct RegisterMCAsmInfo {
1121   RegisterMCAsmInfo(Target &T) {
1122     TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
1123   }
1124 
1125 private:
1126   static MCAsmInfo *Allocator(const MCRegisterInfo & /*MRI*/, const Triple &TT,
1127                               const MCTargetOptions &Options) {
1128     return new MCAsmInfoImpl(TT, Options);
1129   }
1130 };
1131 
1132 /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
1133 /// implementation.  This invokes the specified function to do the
1134 /// construction.  Usage:
1135 ///
1136 /// extern "C" void LLVMInitializeFooTarget() {
1137 ///   extern Target TheFooTarget;
1138 ///   RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
1139 /// }
1140 struct RegisterMCAsmInfoFn {
1141   RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
1142     TargetRegistry::RegisterMCAsmInfo(T, Fn);
1143   }
1144 };
1145 
1146 /// Helper template for registering a target object file info implementation.
1147 /// This invokes the static "Create" method on the class to actually do the
1148 /// construction.  Usage:
1149 ///
1150 /// extern "C" void LLVMInitializeFooTarget() {
1151 ///   extern Target TheFooTarget;
1152 ///   RegisterMCObjectFileInfo<FooMCObjectFileInfo> X(TheFooTarget);
1153 /// }
1154 template <class MCObjectFileInfoImpl> struct RegisterMCObjectFileInfo {
1155   RegisterMCObjectFileInfo(Target &T) {
1156     TargetRegistry::RegisterMCObjectFileInfo(T, &Allocator);
1157   }
1158 
1159 private:
1160   static MCObjectFileInfo *Allocator(MCContext &Ctx, bool PIC,
1161                                      bool LargeCodeModel = false) {
1162     return new MCObjectFileInfoImpl(Ctx, PIC, LargeCodeModel);
1163   }
1164 };
1165 
1166 /// Helper template for registering a target object file info implementation.
1167 /// This invokes the specified function to do the construction.  Usage:
1168 ///
1169 /// extern "C" void LLVMInitializeFooTarget() {
1170 ///   extern Target TheFooTarget;
1171 ///   RegisterMCObjectFileInfoFn X(TheFooTarget, TheFunction);
1172 /// }
1173 struct RegisterMCObjectFileInfoFn {
1174   RegisterMCObjectFileInfoFn(Target &T, Target::MCObjectFileInfoCtorFnTy Fn) {
1175     TargetRegistry::RegisterMCObjectFileInfo(T, Fn);
1176   }
1177 };
1178 
1179 /// RegisterMCInstrInfo - Helper template for registering a target instruction
1180 /// info implementation.  This invokes the static "Create" method on the class
1181 /// to actually do the construction.  Usage:
1182 ///
1183 /// extern "C" void LLVMInitializeFooTarget() {
1184 ///   extern Target TheFooTarget;
1185 ///   RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
1186 /// }
1187 template <class MCInstrInfoImpl> struct RegisterMCInstrInfo {
1188   RegisterMCInstrInfo(Target &T) {
1189     TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
1190   }
1191 
1192 private:
1193   static MCInstrInfo *Allocator() { return new MCInstrInfoImpl(); }
1194 };
1195 
1196 /// RegisterMCInstrInfoFn - Helper template for registering a target
1197 /// instruction info implementation.  This invokes the specified function to
1198 /// do the construction.  Usage:
1199 ///
1200 /// extern "C" void LLVMInitializeFooTarget() {
1201 ///   extern Target TheFooTarget;
1202 ///   RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
1203 /// }
1204 struct RegisterMCInstrInfoFn {
1205   RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
1206     TargetRegistry::RegisterMCInstrInfo(T, Fn);
1207   }
1208 };
1209 
1210 /// RegisterMCInstrAnalysis - Helper template for registering a target
1211 /// instruction analyzer implementation.  This invokes the static "Create"
1212 /// method on the class to actually do the construction.  Usage:
1213 ///
1214 /// extern "C" void LLVMInitializeFooTarget() {
1215 ///   extern Target TheFooTarget;
1216 ///   RegisterMCInstrAnalysis<FooMCInstrAnalysis> X(TheFooTarget);
1217 /// }
1218 template <class MCInstrAnalysisImpl> struct RegisterMCInstrAnalysis {
1219   RegisterMCInstrAnalysis(Target &T) {
1220     TargetRegistry::RegisterMCInstrAnalysis(T, &Allocator);
1221   }
1222 
1223 private:
1224   static MCInstrAnalysis *Allocator(const MCInstrInfo *Info) {
1225     return new MCInstrAnalysisImpl(Info);
1226   }
1227 };
1228 
1229 /// RegisterMCInstrAnalysisFn - Helper template for registering a target
1230 /// instruction analyzer implementation.  This invokes the specified function
1231 /// to do the construction.  Usage:
1232 ///
1233 /// extern "C" void LLVMInitializeFooTarget() {
1234 ///   extern Target TheFooTarget;
1235 ///   RegisterMCInstrAnalysisFn X(TheFooTarget, TheFunction);
1236 /// }
1237 struct RegisterMCInstrAnalysisFn {
1238   RegisterMCInstrAnalysisFn(Target &T, Target::MCInstrAnalysisCtorFnTy Fn) {
1239     TargetRegistry::RegisterMCInstrAnalysis(T, Fn);
1240   }
1241 };
1242 
1243 /// RegisterMCRegInfo - Helper template for registering a target register info
1244 /// implementation.  This invokes the static "Create" method on the class to
1245 /// actually do the construction.  Usage:
1246 ///
1247 /// extern "C" void LLVMInitializeFooTarget() {
1248 ///   extern Target TheFooTarget;
1249 ///   RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
1250 /// }
1251 template <class MCRegisterInfoImpl> struct RegisterMCRegInfo {
1252   RegisterMCRegInfo(Target &T) {
1253     TargetRegistry::RegisterMCRegInfo(T, &Allocator);
1254   }
1255 
1256 private:
1257   static MCRegisterInfo *Allocator(const Triple & /*TT*/) {
1258     return new MCRegisterInfoImpl();
1259   }
1260 };
1261 
1262 /// RegisterMCRegInfoFn - Helper template for registering a target register
1263 /// info implementation.  This invokes the specified function to do the
1264 /// construction.  Usage:
1265 ///
1266 /// extern "C" void LLVMInitializeFooTarget() {
1267 ///   extern Target TheFooTarget;
1268 ///   RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
1269 /// }
1270 struct RegisterMCRegInfoFn {
1271   RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
1272     TargetRegistry::RegisterMCRegInfo(T, Fn);
1273   }
1274 };
1275 
1276 /// RegisterMCSubtargetInfo - Helper template for registering a target
1277 /// subtarget info implementation.  This invokes the static "Create" method
1278 /// on the class to actually do the construction.  Usage:
1279 ///
1280 /// extern "C" void LLVMInitializeFooTarget() {
1281 ///   extern Target TheFooTarget;
1282 ///   RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
1283 /// }
1284 template <class MCSubtargetInfoImpl> struct RegisterMCSubtargetInfo {
1285   RegisterMCSubtargetInfo(Target &T) {
1286     TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
1287   }
1288 
1289 private:
1290   static MCSubtargetInfo *Allocator(const Triple & /*TT*/, StringRef /*CPU*/,
1291                                     StringRef /*FS*/) {
1292     return new MCSubtargetInfoImpl();
1293   }
1294 };
1295 
1296 /// RegisterMCSubtargetInfoFn - Helper template for registering a target
1297 /// subtarget info implementation.  This invokes the specified function to
1298 /// do the construction.  Usage:
1299 ///
1300 /// extern "C" void LLVMInitializeFooTarget() {
1301 ///   extern Target TheFooTarget;
1302 ///   RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
1303 /// }
1304 struct RegisterMCSubtargetInfoFn {
1305   RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
1306     TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
1307   }
1308 };
1309 
1310 /// RegisterTargetMachine - Helper template for registering a target machine
1311 /// implementation, for use in the target machine initialization
1312 /// function. Usage:
1313 ///
1314 /// extern "C" void LLVMInitializeFooTarget() {
1315 ///   extern Target TheFooTarget;
1316 ///   RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
1317 /// }
1318 template <class TargetMachineImpl> struct RegisterTargetMachine {
1319   RegisterTargetMachine(Target &T) {
1320     TargetRegistry::RegisterTargetMachine(T, &Allocator);
1321   }
1322 
1323 private:
1324   static TargetMachine *
1325   Allocator(const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
1326             const TargetOptions &Options, Optional<Reloc::Model> RM,
1327             Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT) {
1328     return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL, JIT);
1329   }
1330 };
1331 
1332 /// RegisterMCAsmBackend - Helper template for registering a target specific
1333 /// assembler backend. Usage:
1334 ///
1335 /// extern "C" void LLVMInitializeFooMCAsmBackend() {
1336 ///   extern Target TheFooTarget;
1337 ///   RegisterMCAsmBackend<FooAsmLexer> X(TheFooTarget);
1338 /// }
1339 template <class MCAsmBackendImpl> struct RegisterMCAsmBackend {
1340   RegisterMCAsmBackend(Target &T) {
1341     TargetRegistry::RegisterMCAsmBackend(T, &Allocator);
1342   }
1343 
1344 private:
1345   static MCAsmBackend *Allocator(const Target &T, const MCSubtargetInfo &STI,
1346                                  const MCRegisterInfo &MRI,
1347                                  const MCTargetOptions &Options) {
1348     return new MCAsmBackendImpl(T, STI, MRI);
1349   }
1350 };
1351 
1352 /// RegisterMCAsmParser - Helper template for registering a target specific
1353 /// assembly parser, for use in the target machine initialization
1354 /// function. Usage:
1355 ///
1356 /// extern "C" void LLVMInitializeFooMCAsmParser() {
1357 ///   extern Target TheFooTarget;
1358 ///   RegisterMCAsmParser<FooAsmParser> X(TheFooTarget);
1359 /// }
1360 template <class MCAsmParserImpl> struct RegisterMCAsmParser {
1361   RegisterMCAsmParser(Target &T) {
1362     TargetRegistry::RegisterMCAsmParser(T, &Allocator);
1363   }
1364 
1365 private:
1366   static MCTargetAsmParser *Allocator(const MCSubtargetInfo &STI,
1367                                       MCAsmParser &P, const MCInstrInfo &MII,
1368                                       const MCTargetOptions &Options) {
1369     return new MCAsmParserImpl(STI, P, MII, Options);
1370   }
1371 };
1372 
1373 /// RegisterAsmPrinter - Helper template for registering a target specific
1374 /// assembly printer, for use in the target machine initialization
1375 /// function. Usage:
1376 ///
1377 /// extern "C" void LLVMInitializeFooAsmPrinter() {
1378 ///   extern Target TheFooTarget;
1379 ///   RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
1380 /// }
1381 template <class AsmPrinterImpl> struct RegisterAsmPrinter {
1382   RegisterAsmPrinter(Target &T) {
1383     TargetRegistry::RegisterAsmPrinter(T, &Allocator);
1384   }
1385 
1386 private:
1387   static AsmPrinter *Allocator(TargetMachine &TM,
1388                                std::unique_ptr<MCStreamer> &&Streamer) {
1389     return new AsmPrinterImpl(TM, std::move(Streamer));
1390   }
1391 };
1392 
1393 /// RegisterMCCodeEmitter - Helper template for registering a target specific
1394 /// machine code emitter, for use in the target initialization
1395 /// function. Usage:
1396 ///
1397 /// extern "C" void LLVMInitializeFooMCCodeEmitter() {
1398 ///   extern Target TheFooTarget;
1399 ///   RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget);
1400 /// }
1401 template <class MCCodeEmitterImpl> struct RegisterMCCodeEmitter {
1402   RegisterMCCodeEmitter(Target &T) {
1403     TargetRegistry::RegisterMCCodeEmitter(T, &Allocator);
1404   }
1405 
1406 private:
1407   static MCCodeEmitter *Allocator(const MCInstrInfo & /*II*/,
1408                                   MCContext & /*Ctx*/) {
1409     return new MCCodeEmitterImpl();
1410   }
1411 };
1412 
1413 } // end namespace llvm
1414 
1415 #endif // LLVM_MC_TARGETREGISTRY_H
1416