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