1 //===- Support/TargetRegistry.h - Target Registration -----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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.
13 // Target specific class implementations should register themselves using the
14 // appropriate TargetRegistry interfaces.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_SUPPORT_TARGETREGISTRY_H
19 #define LLVM_SUPPORT_TARGETREGISTRY_H
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/Support/CodeGen.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/FormattedStream.h"
46 class MCInstrAnalysis;
50 class MCRelocationInfo;
52 class MCSubtargetInfo;
54 class MCTargetAsmParser;
55 class MCTargetOptions;
56 class MCTargetStreamer;
58 class raw_pwrite_stream;
62 MCStreamer *createNullStreamer(MCContext &Ctx);
63 // Takes ownership of \p TAB and \p CE.
65 /// Create a machine code streamer which will print out assembly for the native
66 /// target, suitable for compiling with a native assembler.
68 /// \param InstPrint - If given, the instruction printer to use. If not given
69 /// the MCInst representation will be printed. This method takes ownership of
72 /// \param CE - If given, a code emitter to use to show the instruction
73 /// encoding inline with the assembly. This method takes ownership of \p CE.
75 /// \param TAB - If given, a target asm backend to use to show the fixup
76 /// information in conjunction with encoding information. This method takes
77 /// ownership of \p TAB.
79 /// \param ShowInst - Whether to show the MCInst representation inline with
82 createAsmStreamer(MCContext &Ctx, std::unique_ptr<formatted_raw_ostream> OS,
83 bool isVerboseAsm, bool useDwarfDirectory,
84 MCInstPrinter *InstPrint, std::unique_ptr<MCCodeEmitter> &&CE,
85 std::unique_ptr<MCAsmBackend> &&TAB, bool ShowInst);
87 MCStreamer *createELFStreamer(MCContext &Ctx,
88 std::unique_ptr<MCAsmBackend> &&TAB,
89 std::unique_ptr<MCObjectWriter> &&OW,
90 std::unique_ptr<MCCodeEmitter> &&CE,
92 MCStreamer *createMachOStreamer(MCContext &Ctx,
93 std::unique_ptr<MCAsmBackend> &&TAB,
94 std::unique_ptr<MCObjectWriter> &&OW,
95 std::unique_ptr<MCCodeEmitter> &&CE,
96 bool RelaxAll, bool DWARFMustBeAtTheEnd,
97 bool LabelSections = false);
98 MCStreamer *createWasmStreamer(MCContext &Ctx,
99 std::unique_ptr<MCAsmBackend> &&TAB,
100 std::unique_ptr<MCObjectWriter> &&OW,
101 std::unique_ptr<MCCodeEmitter> &&CE,
103 MCStreamer *createXCOFFStreamer(MCContext &Ctx,
104 std::unique_ptr<MCAsmBackend> &&TAB,
105 std::unique_ptr<MCObjectWriter> &&OW,
106 std::unique_ptr<MCCodeEmitter> &&CE,
109 MCRelocationInfo *createMCRelocationInfo(const Triple &TT, MCContext &Ctx);
111 MCSymbolizer *createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo,
112 LLVMSymbolLookupCallback SymbolLookUp,
113 void *DisInfo, MCContext *Ctx,
114 std::unique_ptr<MCRelocationInfo> &&RelInfo);
116 /// Target - Wrapper for Target specific information.
118 /// For registration purposes, this is a POD type so that targets can be
119 /// registered without the use of static constructors.
121 /// Targets should implement a single global instance of this class (which
122 /// will be zero initialized), and pass that instance to the TargetRegistry as
123 /// part of their initialization.
126 friend struct TargetRegistry;
128 using ArchMatchFnTy = bool (*)(Triple::ArchType Arch);
130 using MCAsmInfoCtorFnTy = MCAsmInfo *(*)(const MCRegisterInfo &MRI,
132 const MCTargetOptions &Options);
133 using MCInstrInfoCtorFnTy = MCInstrInfo *(*)();
134 using MCInstrAnalysisCtorFnTy = MCInstrAnalysis *(*)(const MCInstrInfo *Info);
135 using MCRegInfoCtorFnTy = MCRegisterInfo *(*)(const Triple &TT);
136 using MCSubtargetInfoCtorFnTy = MCSubtargetInfo *(*)(const Triple &TT,
139 using TargetMachineCtorTy = TargetMachine
140 *(*)(const Target &T, const Triple &TT, StringRef CPU, StringRef Features,
141 const TargetOptions &Options, Optional<Reloc::Model> RM,
142 Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT);
143 // If it weren't for layering issues (this header is in llvm/Support, but
144 // depends on MC?) this should take the Streamer by value rather than rvalue
146 using AsmPrinterCtorTy = AsmPrinter *(*)(
147 TargetMachine &TM, std::unique_ptr<MCStreamer> &&Streamer);
148 using MCAsmBackendCtorTy = MCAsmBackend *(*)(const Target &T,
149 const MCSubtargetInfo &STI,
150 const MCRegisterInfo &MRI,
151 const MCTargetOptions &Options);
152 using MCAsmParserCtorTy = MCTargetAsmParser *(*)(
153 const MCSubtargetInfo &STI, MCAsmParser &P, const MCInstrInfo &MII,
154 const MCTargetOptions &Options);
155 using MCDisassemblerCtorTy = MCDisassembler *(*)(const Target &T,
156 const MCSubtargetInfo &STI,
158 using MCInstPrinterCtorTy = MCInstPrinter *(*)(const Triple &T,
159 unsigned SyntaxVariant,
160 const MCAsmInfo &MAI,
161 const MCInstrInfo &MII,
162 const MCRegisterInfo &MRI);
163 using MCCodeEmitterCtorTy = MCCodeEmitter *(*)(const MCInstrInfo &II,
164 const MCRegisterInfo &MRI,
166 using ELFStreamerCtorTy =
167 MCStreamer *(*)(const Triple &T, MCContext &Ctx,
168 std::unique_ptr<MCAsmBackend> &&TAB,
169 std::unique_ptr<MCObjectWriter> &&OW,
170 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
171 using MachOStreamerCtorTy =
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 DWARFMustBeAtTheEnd);
176 using COFFStreamerCtorTy =
177 MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB,
178 std::unique_ptr<MCObjectWriter> &&OW,
179 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
180 bool IncrementalLinkerCompatible);
181 using WasmStreamerCtorTy =
182 MCStreamer *(*)(const Triple &T, MCContext &Ctx,
183 std::unique_ptr<MCAsmBackend> &&TAB,
184 std::unique_ptr<MCObjectWriter> &&OW,
185 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
186 using NullTargetStreamerCtorTy = MCTargetStreamer *(*)(MCStreamer &S);
187 using AsmTargetStreamerCtorTy = MCTargetStreamer *(*)(
188 MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint,
190 using ObjectTargetStreamerCtorTy = MCTargetStreamer *(*)(
191 MCStreamer &S, const MCSubtargetInfo &STI);
192 using MCRelocationInfoCtorTy = MCRelocationInfo *(*)(const Triple &TT,
194 using MCSymbolizerCtorTy = MCSymbolizer *(*)(
195 const Triple &TT, LLVMOpInfoCallback GetOpInfo,
196 LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx,
197 std::unique_ptr<MCRelocationInfo> &&RelInfo);
200 /// Next - The next registered target in the linked list, maintained by the
204 /// The target function for checking if an architecture is supported.
205 ArchMatchFnTy ArchMatchFn;
207 /// Name - The target name.
210 /// ShortDesc - A short description of the target.
211 const char *ShortDesc;
213 /// BackendName - The name of the backend implementation. This must match the
214 /// name of the 'def X : Target ...' in TableGen.
215 const char *BackendName;
217 /// HasJIT - Whether this target supports the JIT.
220 /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
222 MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
224 /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
226 MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
228 /// MCInstrAnalysisCtorFn - Constructor function for this target's
229 /// MCInstrAnalysis, if registered.
230 MCInstrAnalysisCtorFnTy MCInstrAnalysisCtorFn;
232 /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
234 MCRegInfoCtorFnTy MCRegInfoCtorFn;
236 /// MCSubtargetInfoCtorFn - Constructor function for this target's
237 /// MCSubtargetInfo, if registered.
238 MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn;
240 /// TargetMachineCtorFn - Construction function for this target's
241 /// TargetMachine, if registered.
242 TargetMachineCtorTy TargetMachineCtorFn;
244 /// MCAsmBackendCtorFn - Construction function for this target's
245 /// MCAsmBackend, if registered.
246 MCAsmBackendCtorTy MCAsmBackendCtorFn;
248 /// MCAsmParserCtorFn - Construction function for this target's
249 /// MCTargetAsmParser, if registered.
250 MCAsmParserCtorTy MCAsmParserCtorFn;
252 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
254 AsmPrinterCtorTy AsmPrinterCtorFn;
256 /// MCDisassemblerCtorFn - Construction function for this target's
257 /// MCDisassembler, if registered.
258 MCDisassemblerCtorTy MCDisassemblerCtorFn;
260 /// MCInstPrinterCtorFn - Construction function for this target's
261 /// MCInstPrinter, if registered.
262 MCInstPrinterCtorTy MCInstPrinterCtorFn;
264 /// MCCodeEmitterCtorFn - Construction function for this target's
265 /// CodeEmitter, if registered.
266 MCCodeEmitterCtorTy MCCodeEmitterCtorFn;
268 // Construction functions for the various object formats, if registered.
269 COFFStreamerCtorTy COFFStreamerCtorFn = nullptr;
270 MachOStreamerCtorTy MachOStreamerCtorFn = nullptr;
271 ELFStreamerCtorTy ELFStreamerCtorFn = nullptr;
272 WasmStreamerCtorTy WasmStreamerCtorFn = nullptr;
274 /// Construction function for this target's null TargetStreamer, if
275 /// registered (default = nullptr).
276 NullTargetStreamerCtorTy NullTargetStreamerCtorFn = nullptr;
278 /// Construction function for this target's asm TargetStreamer, if
279 /// registered (default = nullptr).
280 AsmTargetStreamerCtorTy AsmTargetStreamerCtorFn = nullptr;
282 /// Construction function for this target's obj TargetStreamer, if
283 /// registered (default = nullptr).
284 ObjectTargetStreamerCtorTy ObjectTargetStreamerCtorFn = nullptr;
286 /// MCRelocationInfoCtorFn - Construction function for this target's
287 /// MCRelocationInfo, if registered (default = llvm::createMCRelocationInfo)
288 MCRelocationInfoCtorTy MCRelocationInfoCtorFn = nullptr;
290 /// MCSymbolizerCtorFn - Construction function for this target's
291 /// MCSymbolizer, if registered (default = llvm::createMCSymbolizer)
292 MCSymbolizerCtorTy MCSymbolizerCtorFn = nullptr;
297 /// @name Target Information
300 // getNext - Return the next registered target.
301 const Target *getNext() const { return Next; }
303 /// getName - Get the target name.
304 const char *getName() const { return Name; }
306 /// getShortDescription - Get a short description of the target.
307 const char *getShortDescription() const { return ShortDesc; }
309 /// getBackendName - Get the backend name.
310 const char *getBackendName() const { return BackendName; }
313 /// @name Feature Predicates
316 /// hasJIT - Check if this targets supports the just-in-time compilation.
317 bool hasJIT() const { return HasJIT; }
319 /// hasTargetMachine - Check if this target supports code generation.
320 bool hasTargetMachine() const { return TargetMachineCtorFn != nullptr; }
322 /// hasMCAsmBackend - Check if this target supports .o generation.
323 bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != nullptr; }
325 /// hasMCAsmParser - Check if this target supports assembly parsing.
326 bool hasMCAsmParser() const { return MCAsmParserCtorFn != nullptr; }
329 /// @name Feature Constructors
332 /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
335 /// \param TheTriple This argument is used to determine the target machine
336 /// feature set; it should always be provided. Generally this should be
337 /// either the target triple from the module, or the target triple of the
338 /// host if that does not exist.
339 MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI, StringRef TheTriple,
340 const MCTargetOptions &Options) const {
341 if (!MCAsmInfoCtorFn)
343 return MCAsmInfoCtorFn(MRI, Triple(TheTriple), Options);
346 /// createMCInstrInfo - Create a MCInstrInfo implementation.
348 MCInstrInfo *createMCInstrInfo() const {
349 if (!MCInstrInfoCtorFn)
351 return MCInstrInfoCtorFn();
354 /// createMCInstrAnalysis - Create a MCInstrAnalysis implementation.
356 MCInstrAnalysis *createMCInstrAnalysis(const MCInstrInfo *Info) const {
357 if (!MCInstrAnalysisCtorFn)
359 return MCInstrAnalysisCtorFn(Info);
362 /// createMCRegInfo - Create a MCRegisterInfo implementation.
364 MCRegisterInfo *createMCRegInfo(StringRef TT) const {
365 if (!MCRegInfoCtorFn)
367 return MCRegInfoCtorFn(Triple(TT));
370 /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
372 /// \param TheTriple This argument is used to determine the target machine
373 /// feature set; it should always be provided. Generally this should be
374 /// either the target triple from the module, or the target triple of the
375 /// host if that does not exist.
376 /// \param CPU This specifies the name of the target CPU.
377 /// \param Features This specifies the string representation of the
378 /// additional target features.
379 MCSubtargetInfo *createMCSubtargetInfo(StringRef TheTriple, StringRef CPU,
380 StringRef Features) const {
381 if (!MCSubtargetInfoCtorFn)
383 return MCSubtargetInfoCtorFn(Triple(TheTriple), CPU, Features);
386 /// createTargetMachine - Create a target specific machine implementation
387 /// for the specified \p Triple.
389 /// \param TT This argument is used to determine the target machine
390 /// feature set; it should always be provided. Generally this should be
391 /// either the target triple from the module, or the target triple of the
392 /// host if that does not exist.
393 TargetMachine *createTargetMachine(StringRef TT, StringRef CPU,
395 const TargetOptions &Options,
396 Optional<Reloc::Model> RM,
397 Optional<CodeModel::Model> CM = None,
398 CodeGenOpt::Level OL = CodeGenOpt::Default,
399 bool JIT = false) const {
400 if (!TargetMachineCtorFn)
402 return TargetMachineCtorFn(*this, Triple(TT), CPU, Features, Options, RM,
406 /// createMCAsmBackend - Create a target specific assembly parser.
407 MCAsmBackend *createMCAsmBackend(const MCSubtargetInfo &STI,
408 const MCRegisterInfo &MRI,
409 const MCTargetOptions &Options) const {
410 if (!MCAsmBackendCtorFn)
412 return MCAsmBackendCtorFn(*this, STI, MRI, Options);
415 /// createMCAsmParser - Create a target specific assembly parser.
417 /// \param Parser The target independent parser implementation to use for
418 /// parsing and lexing.
419 MCTargetAsmParser *createMCAsmParser(const MCSubtargetInfo &STI,
421 const MCInstrInfo &MII,
422 const MCTargetOptions &Options) const {
423 if (!MCAsmParserCtorFn)
425 return MCAsmParserCtorFn(STI, Parser, MII, Options);
428 /// createAsmPrinter - Create a target specific assembly printer pass. This
429 /// takes ownership of the MCStreamer object.
430 AsmPrinter *createAsmPrinter(TargetMachine &TM,
431 std::unique_ptr<MCStreamer> &&Streamer) const {
432 if (!AsmPrinterCtorFn)
434 return AsmPrinterCtorFn(TM, std::move(Streamer));
437 MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI,
438 MCContext &Ctx) const {
439 if (!MCDisassemblerCtorFn)
441 return MCDisassemblerCtorFn(*this, STI, Ctx);
444 MCInstPrinter *createMCInstPrinter(const Triple &T, unsigned SyntaxVariant,
445 const MCAsmInfo &MAI,
446 const MCInstrInfo &MII,
447 const MCRegisterInfo &MRI) const {
448 if (!MCInstPrinterCtorFn)
450 return MCInstPrinterCtorFn(T, SyntaxVariant, MAI, MII, MRI);
453 /// createMCCodeEmitter - Create a target specific code emitter.
454 MCCodeEmitter *createMCCodeEmitter(const MCInstrInfo &II,
455 const MCRegisterInfo &MRI,
456 MCContext &Ctx) const {
457 if (!MCCodeEmitterCtorFn)
459 return MCCodeEmitterCtorFn(II, MRI, Ctx);
462 /// Create a target specific MCStreamer.
464 /// \param T The target triple.
465 /// \param Ctx The target context.
466 /// \param TAB The target assembler backend object. Takes ownership.
467 /// \param OW The stream object.
468 /// \param Emitter The target independent assembler object.Takes ownership.
469 /// \param RelaxAll Relax all fixups?
470 MCStreamer *createMCObjectStreamer(const Triple &T, MCContext &Ctx,
471 std::unique_ptr<MCAsmBackend> &&TAB,
472 std::unique_ptr<MCObjectWriter> &&OW,
473 std::unique_ptr<MCCodeEmitter> &&Emitter,
474 const MCSubtargetInfo &STI, bool RelaxAll,
475 bool IncrementalLinkerCompatible,
476 bool DWARFMustBeAtTheEnd) const {
477 MCStreamer *S = nullptr;
478 switch (T.getObjectFormat()) {
479 case Triple::UnknownObjectFormat:
480 llvm_unreachable("Unknown object format");
482 assert(T.isOSWindows() && "only Windows COFF is supported");
483 S = COFFStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
484 std::move(Emitter), RelaxAll,
485 IncrementalLinkerCompatible);
488 if (MachOStreamerCtorFn)
489 S = MachOStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
490 std::move(Emitter), RelaxAll,
491 DWARFMustBeAtTheEnd);
493 S = createMachOStreamer(Ctx, std::move(TAB), std::move(OW),
494 std::move(Emitter), RelaxAll,
495 DWARFMustBeAtTheEnd);
498 if (ELFStreamerCtorFn)
499 S = ELFStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
500 std::move(Emitter), RelaxAll);
502 S = createELFStreamer(Ctx, std::move(TAB), std::move(OW),
503 std::move(Emitter), RelaxAll);
506 if (WasmStreamerCtorFn)
507 S = WasmStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
508 std::move(Emitter), RelaxAll);
510 S = createWasmStreamer(Ctx, std::move(TAB), std::move(OW),
511 std::move(Emitter), RelaxAll);
514 S = createXCOFFStreamer(Ctx, std::move(TAB), std::move(OW),
515 std::move(Emitter), RelaxAll);
518 if (ObjectTargetStreamerCtorFn)
519 ObjectTargetStreamerCtorFn(*S, STI);
523 MCStreamer *createAsmStreamer(MCContext &Ctx,
524 std::unique_ptr<formatted_raw_ostream> OS,
525 bool IsVerboseAsm, bool UseDwarfDirectory,
526 MCInstPrinter *InstPrint,
527 std::unique_ptr<MCCodeEmitter> &&CE,
528 std::unique_ptr<MCAsmBackend> &&TAB,
529 bool ShowInst) const {
530 formatted_raw_ostream &OSRef = *OS;
531 MCStreamer *S = llvm::createAsmStreamer(
532 Ctx, std::move(OS), IsVerboseAsm, UseDwarfDirectory, InstPrint,
533 std::move(CE), std::move(TAB), ShowInst);
534 createAsmTargetStreamer(*S, OSRef, InstPrint, IsVerboseAsm);
538 MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
539 formatted_raw_ostream &OS,
540 MCInstPrinter *InstPrint,
541 bool IsVerboseAsm) const {
542 if (AsmTargetStreamerCtorFn)
543 return AsmTargetStreamerCtorFn(S, OS, InstPrint, IsVerboseAsm);
547 MCStreamer *createNullStreamer(MCContext &Ctx) const {
548 MCStreamer *S = llvm::createNullStreamer(Ctx);
549 createNullTargetStreamer(*S);
553 MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) const {
554 if (NullTargetStreamerCtorFn)
555 return NullTargetStreamerCtorFn(S);
559 /// createMCRelocationInfo - Create a target specific MCRelocationInfo.
561 /// \param TT The target triple.
562 /// \param Ctx The target context.
563 MCRelocationInfo *createMCRelocationInfo(StringRef TT, MCContext &Ctx) const {
564 MCRelocationInfoCtorTy Fn = MCRelocationInfoCtorFn
565 ? MCRelocationInfoCtorFn
566 : llvm::createMCRelocationInfo;
567 return Fn(Triple(TT), Ctx);
570 /// createMCSymbolizer - Create a target specific MCSymbolizer.
572 /// \param TT The target triple.
573 /// \param GetOpInfo The function to get the symbolic information for
575 /// \param SymbolLookUp The function to lookup a symbol name.
576 /// \param DisInfo The pointer to the block of symbolic information for above
579 /// \param Ctx The target context.
580 /// \param RelInfo The relocation information for this target. Takes
583 createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
584 LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo,
586 std::unique_ptr<MCRelocationInfo> &&RelInfo) const {
587 MCSymbolizerCtorTy Fn =
588 MCSymbolizerCtorFn ? MCSymbolizerCtorFn : llvm::createMCSymbolizer;
589 return Fn(Triple(TT), GetOpInfo, SymbolLookUp, DisInfo, Ctx,
596 /// TargetRegistry - Generic interface to target specific features.
597 struct TargetRegistry {
598 // FIXME: Make this a namespace, probably just move all the Register*
599 // functions into Target (currently they all just set members on the Target
600 // anyway, and Target friends this class so those functions can...
602 TargetRegistry() = delete;
605 : public std::iterator<std::forward_iterator_tag, Target, ptrdiff_t> {
606 friend struct TargetRegistry;
608 const Target *Current = nullptr;
610 explicit iterator(Target *T) : Current(T) {}
613 iterator() = default;
615 bool operator==(const iterator &x) const { return Current == x.Current; }
616 bool operator!=(const iterator &x) const { return !operator==(x); }
618 // Iterator traversal: forward iteration only
619 iterator &operator++() { // Preincrement
620 assert(Current && "Cannot increment end iterator!");
621 Current = Current->getNext();
624 iterator operator++(int) { // Postincrement
625 iterator tmp = *this;
630 const Target &operator*() const {
631 assert(Current && "Cannot dereference end iterator!");
635 const Target *operator->() const { return &operator*(); }
638 /// printRegisteredTargetsForVersion - Print the registered targets
639 /// appropriately for inclusion in a tool's version output.
640 static void printRegisteredTargetsForVersion(raw_ostream &OS);
642 /// @name Registry Access
645 static iterator_range<iterator> targets();
647 /// lookupTarget - Lookup a target based on a target triple.
649 /// \param Triple - The triple to use for finding a target.
650 /// \param Error - On failure, an error string describing why no target was
652 static const Target *lookupTarget(const std::string &Triple,
655 /// lookupTarget - Lookup a target based on an architecture name
656 /// and a target triple. If the architecture name is non-empty,
657 /// then the lookup is done by architecture. Otherwise, the target
660 /// \param ArchName - The architecture to use for finding a target.
661 /// \param TheTriple - The triple to use for finding a target. The
662 /// triple is updated with canonical architecture name if a lookup
663 /// by architecture is done.
664 /// \param Error - On failure, an error string describing why no target was
666 static const Target *lookupTarget(const std::string &ArchName,
667 Triple &TheTriple, std::string &Error);
670 /// @name Target Registration
673 /// RegisterTarget - Register the given target. Attempts to register a
674 /// target which has already been registered will be ignored.
676 /// Clients are responsible for ensuring that registration doesn't occur
677 /// while another thread is attempting to access the registry. Typically
678 /// this is done by initializing all targets at program startup.
680 /// @param T - The target being registered.
681 /// @param Name - The target name. This should be a static string.
682 /// @param ShortDesc - A short target description. This should be a static
684 /// @param BackendName - The name of the backend. This should be a static
685 /// string that is the same for all targets that share a backend
686 /// implementation and must match the name used in the 'def X : Target ...' in
688 /// @param ArchMatchFn - The arch match checking function for this target.
689 /// @param HasJIT - Whether the target supports JIT code
691 static void RegisterTarget(Target &T, const char *Name, const char *ShortDesc,
692 const char *BackendName,
693 Target::ArchMatchFnTy ArchMatchFn,
694 bool HasJIT = false);
696 /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
699 /// Clients are responsible for ensuring that registration doesn't occur
700 /// while another thread is attempting to access the registry. Typically
701 /// this is done by initializing all targets at program startup.
703 /// @param T - The target being registered.
704 /// @param Fn - A function to construct a MCAsmInfo for the target.
705 static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
706 T.MCAsmInfoCtorFn = Fn;
709 /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
712 /// Clients are responsible for ensuring that registration doesn't occur
713 /// while another thread is attempting to access the registry. Typically
714 /// this is done by initializing all targets at program startup.
716 /// @param T - The target being registered.
717 /// @param Fn - A function to construct a MCInstrInfo for the target.
718 static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
719 T.MCInstrInfoCtorFn = Fn;
722 /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for
723 /// the given target.
724 static void RegisterMCInstrAnalysis(Target &T,
725 Target::MCInstrAnalysisCtorFnTy Fn) {
726 T.MCInstrAnalysisCtorFn = Fn;
729 /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
732 /// Clients are responsible for ensuring that registration doesn't occur
733 /// while another thread is attempting to access the registry. Typically
734 /// this is done by initializing all targets at program startup.
736 /// @param T - The target being registered.
737 /// @param Fn - A function to construct a MCRegisterInfo for the target.
738 static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
739 T.MCRegInfoCtorFn = Fn;
742 /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
743 /// the given target.
745 /// Clients are responsible for ensuring that registration doesn't occur
746 /// while another thread is attempting to access the registry. Typically
747 /// this is done by initializing all targets at program startup.
749 /// @param T - The target being registered.
750 /// @param Fn - A function to construct a MCSubtargetInfo for the target.
751 static void RegisterMCSubtargetInfo(Target &T,
752 Target::MCSubtargetInfoCtorFnTy Fn) {
753 T.MCSubtargetInfoCtorFn = Fn;
756 /// RegisterTargetMachine - Register a TargetMachine implementation for the
759 /// Clients are responsible for ensuring that registration doesn't occur
760 /// while another thread is attempting to access the registry. Typically
761 /// this is done by initializing all targets at program startup.
763 /// @param T - The target being registered.
764 /// @param Fn - A function to construct a TargetMachine for the target.
765 static void RegisterTargetMachine(Target &T, Target::TargetMachineCtorTy Fn) {
766 T.TargetMachineCtorFn = Fn;
769 /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the
772 /// Clients are responsible for ensuring that registration doesn't occur
773 /// while another thread is attempting to access the registry. Typically
774 /// this is done by initializing all targets at program startup.
776 /// @param T - The target being registered.
777 /// @param Fn - A function to construct an AsmBackend for the target.
778 static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) {
779 T.MCAsmBackendCtorFn = Fn;
782 /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for
783 /// the given target.
785 /// Clients are responsible for ensuring that registration doesn't occur
786 /// while another thread is attempting to access the registry. Typically
787 /// this is done by initializing all targets at program startup.
789 /// @param T - The target being registered.
790 /// @param Fn - A function to construct an MCTargetAsmParser for the target.
791 static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) {
792 T.MCAsmParserCtorFn = Fn;
795 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
798 /// Clients are responsible for ensuring that registration doesn't occur
799 /// while another thread is attempting to access the registry. Typically
800 /// this is done by initializing all targets at program startup.
802 /// @param T - The target being registered.
803 /// @param Fn - A function to construct an AsmPrinter for the target.
804 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
805 T.AsmPrinterCtorFn = Fn;
808 /// RegisterMCDisassembler - Register a MCDisassembler implementation for
809 /// the given target.
811 /// Clients are responsible for ensuring that registration doesn't occur
812 /// while another thread is attempting to access the registry. Typically
813 /// this is done by initializing all targets at program startup.
815 /// @param T - The target being registered.
816 /// @param Fn - A function to construct an MCDisassembler for the target.
817 static void RegisterMCDisassembler(Target &T,
818 Target::MCDisassemblerCtorTy Fn) {
819 T.MCDisassemblerCtorFn = Fn;
822 /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
825 /// Clients are responsible for ensuring that registration doesn't occur
826 /// while another thread is attempting to access the registry. Typically
827 /// this is done by initializing all targets at program startup.
829 /// @param T - The target being registered.
830 /// @param Fn - A function to construct an MCInstPrinter for the target.
831 static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn) {
832 T.MCInstPrinterCtorFn = Fn;
835 /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the
838 /// Clients are responsible for ensuring that registration doesn't occur
839 /// while another thread is attempting to access the registry. Typically
840 /// this is done by initializing all targets at program startup.
842 /// @param T - The target being registered.
843 /// @param Fn - A function to construct an MCCodeEmitter for the target.
844 static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn) {
845 T.MCCodeEmitterCtorFn = Fn;
848 static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn) {
849 T.COFFStreamerCtorFn = Fn;
852 static void RegisterMachOStreamer(Target &T, Target::MachOStreamerCtorTy Fn) {
853 T.MachOStreamerCtorFn = Fn;
856 static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn) {
857 T.ELFStreamerCtorFn = Fn;
860 static void RegisterWasmStreamer(Target &T, Target::WasmStreamerCtorTy Fn) {
861 T.WasmStreamerCtorFn = Fn;
864 static void RegisterNullTargetStreamer(Target &T,
865 Target::NullTargetStreamerCtorTy Fn) {
866 T.NullTargetStreamerCtorFn = Fn;
869 static void RegisterAsmTargetStreamer(Target &T,
870 Target::AsmTargetStreamerCtorTy Fn) {
871 T.AsmTargetStreamerCtorFn = Fn;
875 RegisterObjectTargetStreamer(Target &T,
876 Target::ObjectTargetStreamerCtorTy Fn) {
877 T.ObjectTargetStreamerCtorFn = Fn;
880 /// RegisterMCRelocationInfo - Register an MCRelocationInfo
881 /// implementation for the given target.
883 /// Clients are responsible for ensuring that registration doesn't occur
884 /// while another thread is attempting to access the registry. Typically
885 /// this is done by initializing all targets at program startup.
887 /// @param T - The target being registered.
888 /// @param Fn - A function to construct an MCRelocationInfo for the target.
889 static void RegisterMCRelocationInfo(Target &T,
890 Target::MCRelocationInfoCtorTy Fn) {
891 T.MCRelocationInfoCtorFn = Fn;
894 /// RegisterMCSymbolizer - Register an MCSymbolizer
895 /// implementation for the given target.
897 /// Clients are responsible for ensuring that registration doesn't occur
898 /// while another thread is attempting to access the registry. Typically
899 /// this is done by initializing all targets at program startup.
901 /// @param T - The target being registered.
902 /// @param Fn - A function to construct an MCSymbolizer for the target.
903 static void RegisterMCSymbolizer(Target &T, Target::MCSymbolizerCtorTy Fn) {
904 T.MCSymbolizerCtorFn = Fn;
910 //===--------------------------------------------------------------------===//
912 /// RegisterTarget - Helper template for registering a target, for use in the
913 /// target's initialization function. Usage:
916 /// Target &getTheFooTarget() { // The global target instance.
917 /// static Target TheFooTarget;
918 /// return TheFooTarget;
920 /// extern "C" void LLVMInitializeFooTargetInfo() {
921 /// RegisterTarget<Triple::foo> X(getTheFooTarget(), "foo", "Foo
922 /// description", "Foo" /* Backend Name */);
924 template <Triple::ArchType TargetArchType = Triple::UnknownArch,
926 struct RegisterTarget {
927 RegisterTarget(Target &T, const char *Name, const char *Desc,
928 const char *BackendName) {
929 TargetRegistry::RegisterTarget(T, Name, Desc, BackendName, &getArchMatch,
933 static bool getArchMatch(Triple::ArchType Arch) {
934 return Arch == TargetArchType;
938 /// RegisterMCAsmInfo - Helper template for registering a target assembly info
939 /// implementation. This invokes the static "Create" method on the class to
940 /// actually do the construction. Usage:
942 /// extern "C" void LLVMInitializeFooTarget() {
943 /// extern Target TheFooTarget;
944 /// RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
946 template <class MCAsmInfoImpl> struct RegisterMCAsmInfo {
947 RegisterMCAsmInfo(Target &T) {
948 TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
952 static MCAsmInfo *Allocator(const MCRegisterInfo & /*MRI*/, const Triple &TT,
953 const MCTargetOptions &Options) {
954 return new MCAsmInfoImpl(TT, Options);
958 /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
959 /// implementation. This invokes the specified function to do the
960 /// construction. Usage:
962 /// extern "C" void LLVMInitializeFooTarget() {
963 /// extern Target TheFooTarget;
964 /// RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
966 struct RegisterMCAsmInfoFn {
967 RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
968 TargetRegistry::RegisterMCAsmInfo(T, Fn);
972 /// RegisterMCInstrInfo - Helper template for registering a target instruction
973 /// info implementation. This invokes the static "Create" method on the class
974 /// to actually do the construction. Usage:
976 /// extern "C" void LLVMInitializeFooTarget() {
977 /// extern Target TheFooTarget;
978 /// RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
980 template <class MCInstrInfoImpl> struct RegisterMCInstrInfo {
981 RegisterMCInstrInfo(Target &T) {
982 TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
986 static MCInstrInfo *Allocator() { return new MCInstrInfoImpl(); }
989 /// RegisterMCInstrInfoFn - Helper template for registering a target
990 /// instruction info implementation. This invokes the specified function to
991 /// do the construction. Usage:
993 /// extern "C" void LLVMInitializeFooTarget() {
994 /// extern Target TheFooTarget;
995 /// RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
997 struct RegisterMCInstrInfoFn {
998 RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
999 TargetRegistry::RegisterMCInstrInfo(T, Fn);
1003 /// RegisterMCInstrAnalysis - Helper template for registering a target
1004 /// instruction analyzer implementation. This invokes the static "Create"
1005 /// method on the class to actually do the construction. Usage:
1007 /// extern "C" void LLVMInitializeFooTarget() {
1008 /// extern Target TheFooTarget;
1009 /// RegisterMCInstrAnalysis<FooMCInstrAnalysis> X(TheFooTarget);
1011 template <class MCInstrAnalysisImpl> struct RegisterMCInstrAnalysis {
1012 RegisterMCInstrAnalysis(Target &T) {
1013 TargetRegistry::RegisterMCInstrAnalysis(T, &Allocator);
1017 static MCInstrAnalysis *Allocator(const MCInstrInfo *Info) {
1018 return new MCInstrAnalysisImpl(Info);
1022 /// RegisterMCInstrAnalysisFn - Helper template for registering a target
1023 /// instruction analyzer implementation. This invokes the specified function
1024 /// to do the construction. Usage:
1026 /// extern "C" void LLVMInitializeFooTarget() {
1027 /// extern Target TheFooTarget;
1028 /// RegisterMCInstrAnalysisFn X(TheFooTarget, TheFunction);
1030 struct RegisterMCInstrAnalysisFn {
1031 RegisterMCInstrAnalysisFn(Target &T, Target::MCInstrAnalysisCtorFnTy Fn) {
1032 TargetRegistry::RegisterMCInstrAnalysis(T, Fn);
1036 /// RegisterMCRegInfo - Helper template for registering a target register info
1037 /// implementation. This invokes the static "Create" method on the class to
1038 /// actually do the construction. Usage:
1040 /// extern "C" void LLVMInitializeFooTarget() {
1041 /// extern Target TheFooTarget;
1042 /// RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
1044 template <class MCRegisterInfoImpl> struct RegisterMCRegInfo {
1045 RegisterMCRegInfo(Target &T) {
1046 TargetRegistry::RegisterMCRegInfo(T, &Allocator);
1050 static MCRegisterInfo *Allocator(const Triple & /*TT*/) {
1051 return new MCRegisterInfoImpl();
1055 /// RegisterMCRegInfoFn - Helper template for registering a target register
1056 /// info implementation. This invokes the specified function to do the
1057 /// construction. Usage:
1059 /// extern "C" void LLVMInitializeFooTarget() {
1060 /// extern Target TheFooTarget;
1061 /// RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
1063 struct RegisterMCRegInfoFn {
1064 RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
1065 TargetRegistry::RegisterMCRegInfo(T, Fn);
1069 /// RegisterMCSubtargetInfo - Helper template for registering a target
1070 /// subtarget info implementation. This invokes the static "Create" method
1071 /// on the class to actually do the construction. Usage:
1073 /// extern "C" void LLVMInitializeFooTarget() {
1074 /// extern Target TheFooTarget;
1075 /// RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
1077 template <class MCSubtargetInfoImpl> struct RegisterMCSubtargetInfo {
1078 RegisterMCSubtargetInfo(Target &T) {
1079 TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
1083 static MCSubtargetInfo *Allocator(const Triple & /*TT*/, StringRef /*CPU*/,
1085 return new MCSubtargetInfoImpl();
1089 /// RegisterMCSubtargetInfoFn - Helper template for registering a target
1090 /// subtarget info implementation. This invokes the specified function to
1091 /// do the construction. Usage:
1093 /// extern "C" void LLVMInitializeFooTarget() {
1094 /// extern Target TheFooTarget;
1095 /// RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
1097 struct RegisterMCSubtargetInfoFn {
1098 RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
1099 TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
1103 /// RegisterTargetMachine - Helper template for registering a target machine
1104 /// implementation, for use in the target machine initialization
1105 /// function. Usage:
1107 /// extern "C" void LLVMInitializeFooTarget() {
1108 /// extern Target TheFooTarget;
1109 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
1111 template <class TargetMachineImpl> struct RegisterTargetMachine {
1112 RegisterTargetMachine(Target &T) {
1113 TargetRegistry::RegisterTargetMachine(T, &Allocator);
1117 static TargetMachine *
1118 Allocator(const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
1119 const TargetOptions &Options, Optional<Reloc::Model> RM,
1120 Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT) {
1121 return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL, JIT);
1125 /// RegisterMCAsmBackend - Helper template for registering a target specific
1126 /// assembler backend. Usage:
1128 /// extern "C" void LLVMInitializeFooMCAsmBackend() {
1129 /// extern Target TheFooTarget;
1130 /// RegisterMCAsmBackend<FooAsmLexer> X(TheFooTarget);
1132 template <class MCAsmBackendImpl> struct RegisterMCAsmBackend {
1133 RegisterMCAsmBackend(Target &T) {
1134 TargetRegistry::RegisterMCAsmBackend(T, &Allocator);
1138 static MCAsmBackend *Allocator(const Target &T, const MCSubtargetInfo &STI,
1139 const MCRegisterInfo &MRI,
1140 const MCTargetOptions &Options) {
1141 return new MCAsmBackendImpl(T, STI, MRI);
1145 /// RegisterMCAsmParser - Helper template for registering a target specific
1146 /// assembly parser, for use in the target machine initialization
1147 /// function. Usage:
1149 /// extern "C" void LLVMInitializeFooMCAsmParser() {
1150 /// extern Target TheFooTarget;
1151 /// RegisterMCAsmParser<FooAsmParser> X(TheFooTarget);
1153 template <class MCAsmParserImpl> struct RegisterMCAsmParser {
1154 RegisterMCAsmParser(Target &T) {
1155 TargetRegistry::RegisterMCAsmParser(T, &Allocator);
1159 static MCTargetAsmParser *Allocator(const MCSubtargetInfo &STI,
1160 MCAsmParser &P, const MCInstrInfo &MII,
1161 const MCTargetOptions &Options) {
1162 return new MCAsmParserImpl(STI, P, MII, Options);
1166 /// RegisterAsmPrinter - Helper template for registering a target specific
1167 /// assembly printer, for use in the target machine initialization
1168 /// function. Usage:
1170 /// extern "C" void LLVMInitializeFooAsmPrinter() {
1171 /// extern Target TheFooTarget;
1172 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
1174 template <class AsmPrinterImpl> struct RegisterAsmPrinter {
1175 RegisterAsmPrinter(Target &T) {
1176 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
1180 static AsmPrinter *Allocator(TargetMachine &TM,
1181 std::unique_ptr<MCStreamer> &&Streamer) {
1182 return new AsmPrinterImpl(TM, std::move(Streamer));
1186 /// RegisterMCCodeEmitter - Helper template for registering a target specific
1187 /// machine code emitter, for use in the target initialization
1188 /// function. Usage:
1190 /// extern "C" void LLVMInitializeFooMCCodeEmitter() {
1191 /// extern Target TheFooTarget;
1192 /// RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget);
1194 template <class MCCodeEmitterImpl> struct RegisterMCCodeEmitter {
1195 RegisterMCCodeEmitter(Target &T) {
1196 TargetRegistry::RegisterMCCodeEmitter(T, &Allocator);
1200 static MCCodeEmitter *Allocator(const MCInstrInfo & /*II*/,
1201 const MCRegisterInfo & /*MRI*/,
1202 MCContext & /*Ctx*/) {
1203 return new MCCodeEmitterImpl();
1207 } // end namespace llvm
1209 #endif // LLVM_SUPPORT_TARGETREGISTRY_H