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