1 //===- Support/TargetRegistry.h - Target Registration -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the TargetRegistry interface, which tools can use to access
11 // the appropriate target specific classes (TargetMachine, AsmPrinter, etc.)
12 // which have been registered.
14 // Target specific class implementations should register themselves using the
15 // appropriate TargetRegistry interfaces.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_SUPPORT_TARGETREGISTRY_H
20 #define LLVM_SUPPORT_TARGETREGISTRY_H
22 #include "llvm-c/DisassemblerTypes.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/ADT/Triple.h"
26 #include "llvm/ADT/iterator_range.h"
27 #include "llvm/Support/CodeGen.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/FormattedStream.h"
47 class MCInstrAnalysis;
51 class MCRelocationInfo;
53 class MCSubtargetInfo;
55 class MCTargetAsmParser;
56 class MCTargetOptions;
57 class MCTargetStreamer;
59 class raw_pwrite_stream;
63 MCStreamer *createNullStreamer(MCContext &Ctx);
64 // Takes ownership of \p TAB and \p CE.
66 /// Create a machine code streamer which will print out assembly for the native
67 /// target, suitable for compiling with a native assembler.
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
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.
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.
80 /// \param ShowInst - Whether to show the MCInst representation inline with
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);
88 MCStreamer *createELFStreamer(MCContext &Ctx,
89 std::unique_ptr<MCAsmBackend> &&TAB,
90 std::unique_ptr<MCObjectWriter> &&OW,
91 std::unique_ptr<MCCodeEmitter> &&CE,
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,
105 MCRelocationInfo *createMCRelocationInfo(const Triple &TT, MCContext &Ctx);
107 MCSymbolizer *createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo,
108 LLVMSymbolLookupCallback SymbolLookUp,
109 void *DisInfo, MCContext *Ctx,
110 std::unique_ptr<MCRelocationInfo> &&RelInfo);
112 /// Target - Wrapper for Target specific information.
114 /// For registration purposes, this is a POD type so that targets can be
115 /// registered without the use of static constructors.
117 /// Targets should implement a single global instance of this class (which
118 /// will be zero initialized), and pass that instance to the TargetRegistry as
119 /// part of their initialization.
122 friend struct TargetRegistry;
124 using ArchMatchFnTy = bool (*)(Triple::ArchType Arch);
126 using MCAsmInfoCtorFnTy = MCAsmInfo *(*)(const MCRegisterInfo &MRI,
128 using MCInstrInfoCtorFnTy = MCInstrInfo *(*)();
129 using MCInstrAnalysisCtorFnTy = MCInstrAnalysis *(*)(const MCInstrInfo *Info);
130 using MCRegInfoCtorFnTy = MCRegisterInfo *(*)(const Triple &TT);
131 using MCSubtargetInfoCtorFnTy = MCSubtargetInfo *(*)(const Triple &TT,
134 using TargetMachineCtorTy = TargetMachine
135 *(*)(const Target &T, const Triple &TT, StringRef CPU, StringRef Features,
136 const TargetOptions &Options, Optional<Reloc::Model> RM,
137 Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT);
138 // If it weren't for layering issues (this header is in llvm/Support, but
139 // depends on MC?) this should take the Streamer by value rather than rvalue
141 using AsmPrinterCtorTy = AsmPrinter *(*)(
142 TargetMachine &TM, std::unique_ptr<MCStreamer> &&Streamer);
143 using MCAsmBackendCtorTy = MCAsmBackend *(*)(const Target &T,
144 const MCSubtargetInfo &STI,
145 const MCRegisterInfo &MRI,
146 const MCTargetOptions &Options);
147 using MCAsmParserCtorTy = MCTargetAsmParser *(*)(
148 const MCSubtargetInfo &STI, MCAsmParser &P, const MCInstrInfo &MII,
149 const MCTargetOptions &Options);
150 using MCDisassemblerCtorTy = MCDisassembler *(*)(const Target &T,
151 const MCSubtargetInfo &STI,
153 using MCInstPrinterCtorTy = MCInstPrinter *(*)(const Triple &T,
154 unsigned SyntaxVariant,
155 const MCAsmInfo &MAI,
156 const MCInstrInfo &MII,
157 const MCRegisterInfo &MRI);
158 using MCCodeEmitterCtorTy = MCCodeEmitter *(*)(const MCInstrInfo &II,
159 const MCRegisterInfo &MRI,
161 using ELFStreamerCtorTy =
162 MCStreamer *(*)(const Triple &T, MCContext &Ctx,
163 std::unique_ptr<MCAsmBackend> &&TAB,
164 std::unique_ptr<MCObjectWriter> &&OW,
165 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
166 using MachOStreamerCtorTy =
167 MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB,
168 std::unique_ptr<MCObjectWriter> &&OW,
169 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
170 bool DWARFMustBeAtTheEnd);
171 using COFFStreamerCtorTy =
172 MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB,
173 std::unique_ptr<MCObjectWriter> &&OW,
174 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
175 bool IncrementalLinkerCompatible);
176 using WasmStreamerCtorTy =
177 MCStreamer *(*)(const Triple &T, MCContext &Ctx,
178 std::unique_ptr<MCAsmBackend> &&TAB,
179 std::unique_ptr<MCObjectWriter> &&OW,
180 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
181 using NullTargetStreamerCtorTy = MCTargetStreamer *(*)(MCStreamer &S);
182 using AsmTargetStreamerCtorTy = MCTargetStreamer *(*)(
183 MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint,
185 using ObjectTargetStreamerCtorTy = MCTargetStreamer *(*)(
186 MCStreamer &S, const MCSubtargetInfo &STI);
187 using MCRelocationInfoCtorTy = MCRelocationInfo *(*)(const Triple &TT,
189 using MCSymbolizerCtorTy = MCSymbolizer *(*)(
190 const Triple &TT, LLVMOpInfoCallback GetOpInfo,
191 LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx,
192 std::unique_ptr<MCRelocationInfo> &&RelInfo);
195 /// Next - The next registered target in the linked list, maintained by the
199 /// The target function for checking if an architecture is supported.
200 ArchMatchFnTy ArchMatchFn;
202 /// Name - The target name.
205 /// ShortDesc - A short description of the target.
206 const char *ShortDesc;
208 /// BackendName - The name of the backend implementation. This must match the
209 /// name of the 'def X : Target ...' in TableGen.
210 const char *BackendName;
212 /// HasJIT - Whether this target supports the JIT.
215 /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
217 MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
219 /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
221 MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
223 /// MCInstrAnalysisCtorFn - Constructor function for this target's
224 /// MCInstrAnalysis, if registered.
225 MCInstrAnalysisCtorFnTy MCInstrAnalysisCtorFn;
227 /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
229 MCRegInfoCtorFnTy MCRegInfoCtorFn;
231 /// MCSubtargetInfoCtorFn - Constructor function for this target's
232 /// MCSubtargetInfo, if registered.
233 MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn;
235 /// TargetMachineCtorFn - Construction function for this target's
236 /// TargetMachine, if registered.
237 TargetMachineCtorTy TargetMachineCtorFn;
239 /// MCAsmBackendCtorFn - Construction function for this target's
240 /// MCAsmBackend, if registered.
241 MCAsmBackendCtorTy MCAsmBackendCtorFn;
243 /// MCAsmParserCtorFn - Construction function for this target's
244 /// MCTargetAsmParser, if registered.
245 MCAsmParserCtorTy MCAsmParserCtorFn;
247 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
249 AsmPrinterCtorTy AsmPrinterCtorFn;
251 /// MCDisassemblerCtorFn - Construction function for this target's
252 /// MCDisassembler, if registered.
253 MCDisassemblerCtorTy MCDisassemblerCtorFn;
255 /// MCInstPrinterCtorFn - Construction function for this target's
256 /// MCInstPrinter, if registered.
257 MCInstPrinterCtorTy MCInstPrinterCtorFn;
259 /// MCCodeEmitterCtorFn - Construction function for this target's
260 /// CodeEmitter, if registered.
261 MCCodeEmitterCtorTy MCCodeEmitterCtorFn;
263 // Construction functions for the various object formats, if registered.
264 COFFStreamerCtorTy COFFStreamerCtorFn = nullptr;
265 MachOStreamerCtorTy MachOStreamerCtorFn = nullptr;
266 ELFStreamerCtorTy ELFStreamerCtorFn = nullptr;
267 WasmStreamerCtorTy WasmStreamerCtorFn = nullptr;
269 /// Construction function for this target's null TargetStreamer, if
270 /// registered (default = nullptr).
271 NullTargetStreamerCtorTy NullTargetStreamerCtorFn = nullptr;
273 /// Construction function for this target's asm TargetStreamer, if
274 /// registered (default = nullptr).
275 AsmTargetStreamerCtorTy AsmTargetStreamerCtorFn = nullptr;
277 /// Construction function for this target's obj TargetStreamer, if
278 /// registered (default = nullptr).
279 ObjectTargetStreamerCtorTy ObjectTargetStreamerCtorFn = nullptr;
281 /// MCRelocationInfoCtorFn - Construction function for this target's
282 /// MCRelocationInfo, if registered (default = llvm::createMCRelocationInfo)
283 MCRelocationInfoCtorTy MCRelocationInfoCtorFn = nullptr;
285 /// MCSymbolizerCtorFn - Construction function for this target's
286 /// MCSymbolizer, if registered (default = llvm::createMCSymbolizer)
287 MCSymbolizerCtorTy MCSymbolizerCtorFn = nullptr;
292 /// @name Target Information
295 // getNext - Return the next registered target.
296 const Target *getNext() const { return Next; }
298 /// getName - Get the target name.
299 const char *getName() const { return Name; }
301 /// getShortDescription - Get a short description of the target.
302 const char *getShortDescription() const { return ShortDesc; }
304 /// getBackendName - Get the backend name.
305 const char *getBackendName() const { return BackendName; }
308 /// @name Feature Predicates
311 /// hasJIT - Check if this targets supports the just-in-time compilation.
312 bool hasJIT() const { return HasJIT; }
314 /// hasTargetMachine - Check if this target supports code generation.
315 bool hasTargetMachine() const { return TargetMachineCtorFn != nullptr; }
317 /// hasMCAsmBackend - Check if this target supports .o generation.
318 bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != nullptr; }
320 /// hasMCAsmParser - Check if this target supports assembly parsing.
321 bool hasMCAsmParser() const { return MCAsmParserCtorFn != nullptr; }
324 /// @name Feature Constructors
327 /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
330 /// \param TheTriple This argument is used to determine the target machine
331 /// feature set; it should always be provided. Generally this should be
332 /// either the target triple from the module, or the target triple of the
333 /// host if that does not exist.
334 MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI,
335 StringRef TheTriple) const {
336 if (!MCAsmInfoCtorFn)
338 return MCAsmInfoCtorFn(MRI, Triple(TheTriple));
341 /// createMCInstrInfo - Create a MCInstrInfo implementation.
343 MCInstrInfo *createMCInstrInfo() const {
344 if (!MCInstrInfoCtorFn)
346 return MCInstrInfoCtorFn();
349 /// createMCInstrAnalysis - Create a MCInstrAnalysis implementation.
351 MCInstrAnalysis *createMCInstrAnalysis(const MCInstrInfo *Info) const {
352 if (!MCInstrAnalysisCtorFn)
354 return MCInstrAnalysisCtorFn(Info);
357 /// createMCRegInfo - Create a MCRegisterInfo implementation.
359 MCRegisterInfo *createMCRegInfo(StringRef TT) const {
360 if (!MCRegInfoCtorFn)
362 return MCRegInfoCtorFn(Triple(TT));
365 /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
367 /// \param TheTriple This argument is used to determine the target machine
368 /// feature set; it should always be provided. Generally this should be
369 /// either the target triple from the module, or the target triple of the
370 /// host if that does not exist.
371 /// \param CPU This specifies the name of the target CPU.
372 /// \param Features This specifies the string representation of the
373 /// additional target features.
374 MCSubtargetInfo *createMCSubtargetInfo(StringRef TheTriple, StringRef CPU,
375 StringRef Features) const {
376 if (!MCSubtargetInfoCtorFn)
378 return MCSubtargetInfoCtorFn(Triple(TheTriple), CPU, Features);
381 /// createTargetMachine - Create a target specific machine implementation
382 /// for the specified \p Triple.
384 /// \param TT This argument is used to determine the target machine
385 /// feature set; it should always be provided. Generally this should be
386 /// either the target triple from the module, or the target triple of the
387 /// host if that does not exist.
388 TargetMachine *createTargetMachine(StringRef TT, StringRef CPU,
390 const TargetOptions &Options,
391 Optional<Reloc::Model> RM,
392 Optional<CodeModel::Model> CM = None,
393 CodeGenOpt::Level OL = CodeGenOpt::Default,
394 bool JIT = false) const {
395 if (!TargetMachineCtorFn)
397 return TargetMachineCtorFn(*this, Triple(TT), CPU, Features, Options, RM,
401 /// createMCAsmBackend - Create a target specific assembly parser.
402 MCAsmBackend *createMCAsmBackend(const MCSubtargetInfo &STI,
403 const MCRegisterInfo &MRI,
404 const MCTargetOptions &Options) const {
405 if (!MCAsmBackendCtorFn)
407 return MCAsmBackendCtorFn(*this, STI, MRI, Options);
410 /// createMCAsmParser - Create a target specific assembly parser.
412 /// \param Parser The target independent parser implementation to use for
413 /// parsing and lexing.
414 MCTargetAsmParser *createMCAsmParser(const MCSubtargetInfo &STI,
416 const MCInstrInfo &MII,
417 const MCTargetOptions &Options) const {
418 if (!MCAsmParserCtorFn)
420 return MCAsmParserCtorFn(STI, Parser, MII, Options);
423 /// createAsmPrinter - Create a target specific assembly printer pass. This
424 /// takes ownership of the MCStreamer object.
425 AsmPrinter *createAsmPrinter(TargetMachine &TM,
426 std::unique_ptr<MCStreamer> &&Streamer) const {
427 if (!AsmPrinterCtorFn)
429 return AsmPrinterCtorFn(TM, std::move(Streamer));
432 MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI,
433 MCContext &Ctx) const {
434 if (!MCDisassemblerCtorFn)
436 return MCDisassemblerCtorFn(*this, STI, Ctx);
439 MCInstPrinter *createMCInstPrinter(const Triple &T, unsigned SyntaxVariant,
440 const MCAsmInfo &MAI,
441 const MCInstrInfo &MII,
442 const MCRegisterInfo &MRI) const {
443 if (!MCInstPrinterCtorFn)
445 return MCInstPrinterCtorFn(T, SyntaxVariant, MAI, MII, MRI);
448 /// createMCCodeEmitter - Create a target specific code emitter.
449 MCCodeEmitter *createMCCodeEmitter(const MCInstrInfo &II,
450 const MCRegisterInfo &MRI,
451 MCContext &Ctx) const {
452 if (!MCCodeEmitterCtorFn)
454 return MCCodeEmitterCtorFn(II, MRI, Ctx);
457 /// Create a target specific MCStreamer.
459 /// \param T The target triple.
460 /// \param Ctx The target context.
461 /// \param TAB The target assembler backend object. Takes ownership.
462 /// \param OW The stream object.
463 /// \param Emitter The target independent assembler object.Takes ownership.
464 /// \param RelaxAll Relax all fixups?
465 MCStreamer *createMCObjectStreamer(const Triple &T, MCContext &Ctx,
466 std::unique_ptr<MCAsmBackend> &&TAB,
467 std::unique_ptr<MCObjectWriter> &&OW,
468 std::unique_ptr<MCCodeEmitter> &&Emitter,
469 const MCSubtargetInfo &STI, bool RelaxAll,
470 bool IncrementalLinkerCompatible,
471 bool DWARFMustBeAtTheEnd) const {
473 switch (T.getObjectFormat()) {
475 llvm_unreachable("Unknown object format");
477 assert(T.isOSWindows() && "only Windows COFF is supported");
478 S = COFFStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
479 std::move(Emitter), RelaxAll,
480 IncrementalLinkerCompatible);
483 if (MachOStreamerCtorFn)
484 S = MachOStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
485 std::move(Emitter), RelaxAll,
486 DWARFMustBeAtTheEnd);
488 S = createMachOStreamer(Ctx, std::move(TAB), std::move(OW),
489 std::move(Emitter), RelaxAll,
490 DWARFMustBeAtTheEnd);
493 if (ELFStreamerCtorFn)
494 S = ELFStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
495 std::move(Emitter), RelaxAll);
497 S = createELFStreamer(Ctx, std::move(TAB), std::move(OW),
498 std::move(Emitter), RelaxAll);
501 if (WasmStreamerCtorFn)
502 S = WasmStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
503 std::move(Emitter), RelaxAll);
505 S = createWasmStreamer(Ctx, std::move(TAB), std::move(OW),
506 std::move(Emitter), RelaxAll);
509 if (ObjectTargetStreamerCtorFn)
510 ObjectTargetStreamerCtorFn(*S, STI);
514 MCStreamer *createAsmStreamer(MCContext &Ctx,
515 std::unique_ptr<formatted_raw_ostream> OS,
516 bool IsVerboseAsm, bool UseDwarfDirectory,
517 MCInstPrinter *InstPrint,
518 std::unique_ptr<MCCodeEmitter> &&CE,
519 std::unique_ptr<MCAsmBackend> &&TAB,
520 bool ShowInst) const {
521 formatted_raw_ostream &OSRef = *OS;
522 MCStreamer *S = llvm::createAsmStreamer(
523 Ctx, std::move(OS), IsVerboseAsm, UseDwarfDirectory, InstPrint,
524 std::move(CE), std::move(TAB), ShowInst);
525 createAsmTargetStreamer(*S, OSRef, InstPrint, IsVerboseAsm);
529 MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
530 formatted_raw_ostream &OS,
531 MCInstPrinter *InstPrint,
532 bool IsVerboseAsm) const {
533 if (AsmTargetStreamerCtorFn)
534 return AsmTargetStreamerCtorFn(S, OS, InstPrint, IsVerboseAsm);
538 MCStreamer *createNullStreamer(MCContext &Ctx) const {
539 MCStreamer *S = llvm::createNullStreamer(Ctx);
540 createNullTargetStreamer(*S);
544 MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) const {
545 if (NullTargetStreamerCtorFn)
546 return NullTargetStreamerCtorFn(S);
550 /// createMCRelocationInfo - Create a target specific MCRelocationInfo.
552 /// \param TT The target triple.
553 /// \param Ctx The target context.
554 MCRelocationInfo *createMCRelocationInfo(StringRef TT, MCContext &Ctx) const {
555 MCRelocationInfoCtorTy Fn = MCRelocationInfoCtorFn
556 ? MCRelocationInfoCtorFn
557 : llvm::createMCRelocationInfo;
558 return Fn(Triple(TT), Ctx);
561 /// createMCSymbolizer - Create a target specific MCSymbolizer.
563 /// \param TT The target triple.
564 /// \param GetOpInfo The function to get the symbolic information for
566 /// \param SymbolLookUp The function to lookup a symbol name.
567 /// \param DisInfo The pointer to the block of symbolic information for above
570 /// \param Ctx The target context.
571 /// \param RelInfo The relocation information for this target. Takes
574 createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
575 LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo,
577 std::unique_ptr<MCRelocationInfo> &&RelInfo) const {
578 MCSymbolizerCtorTy Fn =
579 MCSymbolizerCtorFn ? MCSymbolizerCtorFn : llvm::createMCSymbolizer;
580 return Fn(Triple(TT), GetOpInfo, SymbolLookUp, DisInfo, Ctx,
587 /// TargetRegistry - Generic interface to target specific features.
588 struct TargetRegistry {
589 // FIXME: Make this a namespace, probably just move all the Register*
590 // functions into Target (currently they all just set members on the Target
591 // anyway, and Target friends this class so those functions can...
593 TargetRegistry() = delete;
596 : public std::iterator<std::forward_iterator_tag, Target, ptrdiff_t> {
597 friend struct TargetRegistry;
599 const Target *Current = nullptr;
601 explicit iterator(Target *T) : Current(T) {}
604 iterator() = default;
606 bool operator==(const iterator &x) const { return Current == x.Current; }
607 bool operator!=(const iterator &x) const { return !operator==(x); }
609 // Iterator traversal: forward iteration only
610 iterator &operator++() { // Preincrement
611 assert(Current && "Cannot increment end iterator!");
612 Current = Current->getNext();
615 iterator operator++(int) { // Postincrement
616 iterator tmp = *this;
621 const Target &operator*() const {
622 assert(Current && "Cannot dereference end iterator!");
626 const Target *operator->() const { return &operator*(); }
629 /// printRegisteredTargetsForVersion - Print the registered targets
630 /// appropriately for inclusion in a tool's version output.
631 static void printRegisteredTargetsForVersion(raw_ostream &OS);
633 /// @name Registry Access
636 static iterator_range<iterator> targets();
638 /// lookupTarget - Lookup a target based on a target triple.
640 /// \param Triple - The triple to use for finding a target.
641 /// \param Error - On failure, an error string describing why no target was
643 static const Target *lookupTarget(const std::string &Triple,
646 /// lookupTarget - Lookup a target based on an architecture name
647 /// and a target triple. If the architecture name is non-empty,
648 /// then the lookup is done by architecture. Otherwise, the target
651 /// \param ArchName - The architecture to use for finding a target.
652 /// \param TheTriple - The triple to use for finding a target. The
653 /// triple is updated with canonical architecture name if a lookup
654 /// by architecture is done.
655 /// \param Error - On failure, an error string describing why no target was
657 static const Target *lookupTarget(const std::string &ArchName,
658 Triple &TheTriple, std::string &Error);
661 /// @name Target Registration
664 /// RegisterTarget - Register the given target. Attempts to register a
665 /// target which has already been registered will be ignored.
667 /// Clients are responsible for ensuring that registration doesn't occur
668 /// while another thread is attempting to access the registry. Typically
669 /// this is done by initializing all targets at program startup.
671 /// @param T - The target being registered.
672 /// @param Name - The target name. This should be a static string.
673 /// @param ShortDesc - A short target description. This should be a static
675 /// @param BackendName - The name of the backend. This should be a static
676 /// string that is the same for all targets that share a backend
677 /// implementation and must match the name used in the 'def X : Target ...' in
679 /// @param ArchMatchFn - The arch match checking function for this target.
680 /// @param HasJIT - Whether the target supports JIT code
682 static void RegisterTarget(Target &T, const char *Name, const char *ShortDesc,
683 const char *BackendName,
684 Target::ArchMatchFnTy ArchMatchFn,
685 bool HasJIT = false);
687 /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
690 /// Clients are responsible for ensuring that registration doesn't occur
691 /// while another thread is attempting to access the registry. Typically
692 /// this is done by initializing all targets at program startup.
694 /// @param T - The target being registered.
695 /// @param Fn - A function to construct a MCAsmInfo for the target.
696 static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
697 T.MCAsmInfoCtorFn = Fn;
700 /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
703 /// Clients are responsible for ensuring that registration doesn't occur
704 /// while another thread is attempting to access the registry. Typically
705 /// this is done by initializing all targets at program startup.
707 /// @param T - The target being registered.
708 /// @param Fn - A function to construct a MCInstrInfo for the target.
709 static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
710 T.MCInstrInfoCtorFn = Fn;
713 /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for
714 /// the given target.
715 static void RegisterMCInstrAnalysis(Target &T,
716 Target::MCInstrAnalysisCtorFnTy Fn) {
717 T.MCInstrAnalysisCtorFn = Fn;
720 /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
723 /// Clients are responsible for ensuring that registration doesn't occur
724 /// while another thread is attempting to access the registry. Typically
725 /// this is done by initializing all targets at program startup.
727 /// @param T - The target being registered.
728 /// @param Fn - A function to construct a MCRegisterInfo for the target.
729 static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
730 T.MCRegInfoCtorFn = Fn;
733 /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
734 /// the given target.
736 /// Clients are responsible for ensuring that registration doesn't occur
737 /// while another thread is attempting to access the registry. Typically
738 /// this is done by initializing all targets at program startup.
740 /// @param T - The target being registered.
741 /// @param Fn - A function to construct a MCSubtargetInfo for the target.
742 static void RegisterMCSubtargetInfo(Target &T,
743 Target::MCSubtargetInfoCtorFnTy Fn) {
744 T.MCSubtargetInfoCtorFn = Fn;
747 /// RegisterTargetMachine - Register a TargetMachine implementation for the
750 /// Clients are responsible for ensuring that registration doesn't occur
751 /// while another thread is attempting to access the registry. Typically
752 /// this is done by initializing all targets at program startup.
754 /// @param T - The target being registered.
755 /// @param Fn - A function to construct a TargetMachine for the target.
756 static void RegisterTargetMachine(Target &T, Target::TargetMachineCtorTy Fn) {
757 T.TargetMachineCtorFn = Fn;
760 /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the
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.
767 /// @param T - The target being registered.
768 /// @param Fn - A function to construct an AsmBackend for the target.
769 static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) {
770 T.MCAsmBackendCtorFn = Fn;
773 /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for
774 /// the given target.
776 /// Clients are responsible for ensuring that registration doesn't occur
777 /// while another thread is attempting to access the registry. Typically
778 /// this is done by initializing all targets at program startup.
780 /// @param T - The target being registered.
781 /// @param Fn - A function to construct an MCTargetAsmParser for the target.
782 static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) {
783 T.MCAsmParserCtorFn = Fn;
786 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
789 /// Clients are responsible for ensuring that registration doesn't occur
790 /// while another thread is attempting to access the registry. Typically
791 /// this is done by initializing all targets at program startup.
793 /// @param T - The target being registered.
794 /// @param Fn - A function to construct an AsmPrinter for the target.
795 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
796 T.AsmPrinterCtorFn = Fn;
799 /// RegisterMCDisassembler - Register a MCDisassembler implementation for
800 /// the given target.
802 /// Clients are responsible for ensuring that registration doesn't occur
803 /// while another thread is attempting to access the registry. Typically
804 /// this is done by initializing all targets at program startup.
806 /// @param T - The target being registered.
807 /// @param Fn - A function to construct an MCDisassembler for the target.
808 static void RegisterMCDisassembler(Target &T,
809 Target::MCDisassemblerCtorTy Fn) {
810 T.MCDisassemblerCtorFn = Fn;
813 /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
816 /// Clients are responsible for ensuring that registration doesn't occur
817 /// while another thread is attempting to access the registry. Typically
818 /// this is done by initializing all targets at program startup.
820 /// @param T - The target being registered.
821 /// @param Fn - A function to construct an MCInstPrinter for the target.
822 static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn) {
823 T.MCInstPrinterCtorFn = Fn;
826 /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the
829 /// Clients are responsible for ensuring that registration doesn't occur
830 /// while another thread is attempting to access the registry. Typically
831 /// this is done by initializing all targets at program startup.
833 /// @param T - The target being registered.
834 /// @param Fn - A function to construct an MCCodeEmitter for the target.
835 static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn) {
836 T.MCCodeEmitterCtorFn = Fn;
839 static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn) {
840 T.COFFStreamerCtorFn = Fn;
843 static void RegisterMachOStreamer(Target &T, Target::MachOStreamerCtorTy Fn) {
844 T.MachOStreamerCtorFn = Fn;
847 static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn) {
848 T.ELFStreamerCtorFn = Fn;
851 static void RegisterWasmStreamer(Target &T, Target::WasmStreamerCtorTy Fn) {
852 T.WasmStreamerCtorFn = Fn;
855 static void RegisterNullTargetStreamer(Target &T,
856 Target::NullTargetStreamerCtorTy Fn) {
857 T.NullTargetStreamerCtorFn = Fn;
860 static void RegisterAsmTargetStreamer(Target &T,
861 Target::AsmTargetStreamerCtorTy Fn) {
862 T.AsmTargetStreamerCtorFn = Fn;
866 RegisterObjectTargetStreamer(Target &T,
867 Target::ObjectTargetStreamerCtorTy Fn) {
868 T.ObjectTargetStreamerCtorFn = Fn;
871 /// RegisterMCRelocationInfo - Register an MCRelocationInfo
872 /// implementation for the given target.
874 /// Clients are responsible for ensuring that registration doesn't occur
875 /// while another thread is attempting to access the registry. Typically
876 /// this is done by initializing all targets at program startup.
878 /// @param T - The target being registered.
879 /// @param Fn - A function to construct an MCRelocationInfo for the target.
880 static void RegisterMCRelocationInfo(Target &T,
881 Target::MCRelocationInfoCtorTy Fn) {
882 T.MCRelocationInfoCtorFn = Fn;
885 /// RegisterMCSymbolizer - Register an MCSymbolizer
886 /// implementation for the given target.
888 /// Clients are responsible for ensuring that registration doesn't occur
889 /// while another thread is attempting to access the registry. Typically
890 /// this is done by initializing all targets at program startup.
892 /// @param T - The target being registered.
893 /// @param Fn - A function to construct an MCSymbolizer for the target.
894 static void RegisterMCSymbolizer(Target &T, Target::MCSymbolizerCtorTy Fn) {
895 T.MCSymbolizerCtorFn = Fn;
901 //===--------------------------------------------------------------------===//
903 /// RegisterTarget - Helper template for registering a target, for use in the
904 /// target's initialization function. Usage:
907 /// Target &getTheFooTarget() { // The global target instance.
908 /// static Target TheFooTarget;
909 /// return TheFooTarget;
911 /// extern "C" void LLVMInitializeFooTargetInfo() {
912 /// RegisterTarget<Triple::foo> X(getTheFooTarget(), "foo", "Foo
913 /// description", "Foo" /* Backend Name */);
915 template <Triple::ArchType TargetArchType = Triple::UnknownArch,
917 struct RegisterTarget {
918 RegisterTarget(Target &T, const char *Name, const char *Desc,
919 const char *BackendName) {
920 TargetRegistry::RegisterTarget(T, Name, Desc, BackendName, &getArchMatch,
924 static bool getArchMatch(Triple::ArchType Arch) {
925 return Arch == TargetArchType;
929 /// RegisterMCAsmInfo - Helper template for registering a target assembly info
930 /// implementation. This invokes the static "Create" method on the class to
931 /// actually do the construction. Usage:
933 /// extern "C" void LLVMInitializeFooTarget() {
934 /// extern Target TheFooTarget;
935 /// RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
937 template <class MCAsmInfoImpl> struct RegisterMCAsmInfo {
938 RegisterMCAsmInfo(Target &T) {
939 TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
943 static MCAsmInfo *Allocator(const MCRegisterInfo & /*MRI*/,
945 return new MCAsmInfoImpl(TT);
949 /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
950 /// implementation. This invokes the specified function to do the
951 /// construction. Usage:
953 /// extern "C" void LLVMInitializeFooTarget() {
954 /// extern Target TheFooTarget;
955 /// RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
957 struct RegisterMCAsmInfoFn {
958 RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
959 TargetRegistry::RegisterMCAsmInfo(T, Fn);
963 /// RegisterMCInstrInfo - Helper template for registering a target instruction
964 /// info implementation. This invokes the static "Create" method on the class
965 /// to actually do the construction. Usage:
967 /// extern "C" void LLVMInitializeFooTarget() {
968 /// extern Target TheFooTarget;
969 /// RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
971 template <class MCInstrInfoImpl> struct RegisterMCInstrInfo {
972 RegisterMCInstrInfo(Target &T) {
973 TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
977 static MCInstrInfo *Allocator() { return new MCInstrInfoImpl(); }
980 /// RegisterMCInstrInfoFn - Helper template for registering a target
981 /// instruction info implementation. This invokes the specified function to
982 /// do the construction. Usage:
984 /// extern "C" void LLVMInitializeFooTarget() {
985 /// extern Target TheFooTarget;
986 /// RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
988 struct RegisterMCInstrInfoFn {
989 RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
990 TargetRegistry::RegisterMCInstrInfo(T, Fn);
994 /// RegisterMCInstrAnalysis - Helper template for registering a target
995 /// instruction analyzer implementation. This invokes the static "Create"
996 /// method on the class to actually do the construction. Usage:
998 /// extern "C" void LLVMInitializeFooTarget() {
999 /// extern Target TheFooTarget;
1000 /// RegisterMCInstrAnalysis<FooMCInstrAnalysis> X(TheFooTarget);
1002 template <class MCInstrAnalysisImpl> struct RegisterMCInstrAnalysis {
1003 RegisterMCInstrAnalysis(Target &T) {
1004 TargetRegistry::RegisterMCInstrAnalysis(T, &Allocator);
1008 static MCInstrAnalysis *Allocator(const MCInstrInfo *Info) {
1009 return new MCInstrAnalysisImpl(Info);
1013 /// RegisterMCInstrAnalysisFn - Helper template for registering a target
1014 /// instruction analyzer implementation. This invokes the specified function
1015 /// to do the construction. Usage:
1017 /// extern "C" void LLVMInitializeFooTarget() {
1018 /// extern Target TheFooTarget;
1019 /// RegisterMCInstrAnalysisFn X(TheFooTarget, TheFunction);
1021 struct RegisterMCInstrAnalysisFn {
1022 RegisterMCInstrAnalysisFn(Target &T, Target::MCInstrAnalysisCtorFnTy Fn) {
1023 TargetRegistry::RegisterMCInstrAnalysis(T, Fn);
1027 /// RegisterMCRegInfo - Helper template for registering a target register info
1028 /// implementation. This invokes the static "Create" method on the class to
1029 /// actually do the construction. Usage:
1031 /// extern "C" void LLVMInitializeFooTarget() {
1032 /// extern Target TheFooTarget;
1033 /// RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
1035 template <class MCRegisterInfoImpl> struct RegisterMCRegInfo {
1036 RegisterMCRegInfo(Target &T) {
1037 TargetRegistry::RegisterMCRegInfo(T, &Allocator);
1041 static MCRegisterInfo *Allocator(const Triple & /*TT*/) {
1042 return new MCRegisterInfoImpl();
1046 /// RegisterMCRegInfoFn - Helper template for registering a target register
1047 /// info implementation. This invokes the specified function to do the
1048 /// construction. Usage:
1050 /// extern "C" void LLVMInitializeFooTarget() {
1051 /// extern Target TheFooTarget;
1052 /// RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
1054 struct RegisterMCRegInfoFn {
1055 RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
1056 TargetRegistry::RegisterMCRegInfo(T, Fn);
1060 /// RegisterMCSubtargetInfo - Helper template for registering a target
1061 /// subtarget info implementation. This invokes the static "Create" method
1062 /// on the class to actually do the construction. Usage:
1064 /// extern "C" void LLVMInitializeFooTarget() {
1065 /// extern Target TheFooTarget;
1066 /// RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
1068 template <class MCSubtargetInfoImpl> struct RegisterMCSubtargetInfo {
1069 RegisterMCSubtargetInfo(Target &T) {
1070 TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
1074 static MCSubtargetInfo *Allocator(const Triple & /*TT*/, StringRef /*CPU*/,
1076 return new MCSubtargetInfoImpl();
1080 /// RegisterMCSubtargetInfoFn - Helper template for registering a target
1081 /// subtarget info implementation. This invokes the specified function to
1082 /// do the construction. Usage:
1084 /// extern "C" void LLVMInitializeFooTarget() {
1085 /// extern Target TheFooTarget;
1086 /// RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
1088 struct RegisterMCSubtargetInfoFn {
1089 RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
1090 TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
1094 /// RegisterTargetMachine - Helper template for registering a target machine
1095 /// implementation, for use in the target machine initialization
1096 /// function. Usage:
1098 /// extern "C" void LLVMInitializeFooTarget() {
1099 /// extern Target TheFooTarget;
1100 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
1102 template <class TargetMachineImpl> struct RegisterTargetMachine {
1103 RegisterTargetMachine(Target &T) {
1104 TargetRegistry::RegisterTargetMachine(T, &Allocator);
1108 static TargetMachine *
1109 Allocator(const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
1110 const TargetOptions &Options, Optional<Reloc::Model> RM,
1111 Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT) {
1112 return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL, JIT);
1116 /// RegisterMCAsmBackend - Helper template for registering a target specific
1117 /// assembler backend. Usage:
1119 /// extern "C" void LLVMInitializeFooMCAsmBackend() {
1120 /// extern Target TheFooTarget;
1121 /// RegisterMCAsmBackend<FooAsmLexer> X(TheFooTarget);
1123 template <class MCAsmBackendImpl> struct RegisterMCAsmBackend {
1124 RegisterMCAsmBackend(Target &T) {
1125 TargetRegistry::RegisterMCAsmBackend(T, &Allocator);
1129 static MCAsmBackend *Allocator(const Target &T, const MCSubtargetInfo &STI,
1130 const MCRegisterInfo &MRI,
1131 const MCTargetOptions &Options) {
1132 return new MCAsmBackendImpl(T, STI, MRI);
1136 /// RegisterMCAsmParser - Helper template for registering a target specific
1137 /// assembly parser, for use in the target machine initialization
1138 /// function. Usage:
1140 /// extern "C" void LLVMInitializeFooMCAsmParser() {
1141 /// extern Target TheFooTarget;
1142 /// RegisterMCAsmParser<FooAsmParser> X(TheFooTarget);
1144 template <class MCAsmParserImpl> struct RegisterMCAsmParser {
1145 RegisterMCAsmParser(Target &T) {
1146 TargetRegistry::RegisterMCAsmParser(T, &Allocator);
1150 static MCTargetAsmParser *Allocator(const MCSubtargetInfo &STI,
1151 MCAsmParser &P, const MCInstrInfo &MII,
1152 const MCTargetOptions &Options) {
1153 return new MCAsmParserImpl(STI, P, MII, Options);
1157 /// RegisterAsmPrinter - Helper template for registering a target specific
1158 /// assembly printer, for use in the target machine initialization
1159 /// function. Usage:
1161 /// extern "C" void LLVMInitializeFooAsmPrinter() {
1162 /// extern Target TheFooTarget;
1163 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
1165 template <class AsmPrinterImpl> struct RegisterAsmPrinter {
1166 RegisterAsmPrinter(Target &T) {
1167 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
1171 static AsmPrinter *Allocator(TargetMachine &TM,
1172 std::unique_ptr<MCStreamer> &&Streamer) {
1173 return new AsmPrinterImpl(TM, std::move(Streamer));
1177 /// RegisterMCCodeEmitter - Helper template for registering a target specific
1178 /// machine code emitter, for use in the target initialization
1179 /// function. Usage:
1181 /// extern "C" void LLVMInitializeFooMCCodeEmitter() {
1182 /// extern Target TheFooTarget;
1183 /// RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget);
1185 template <class MCCodeEmitterImpl> struct RegisterMCCodeEmitter {
1186 RegisterMCCodeEmitter(Target &T) {
1187 TargetRegistry::RegisterMCCodeEmitter(T, &Allocator);
1191 static MCCodeEmitter *Allocator(const MCInstrInfo & /*II*/,
1192 const MCRegisterInfo & /*MRI*/,
1193 MCContext & /*Ctx*/) {
1194 return new MCCodeEmitterImpl();
1198 } // end namespace llvm
1200 #endif // LLVM_SUPPORT_TARGETREGISTRY_H