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/Disassembler.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/Support/CodeGen.h"
26 #include "llvm/Support/FormattedStream.h"
39 class MCInstrAnalysis;
44 class MCSubtargetInfo;
46 class MCRelocationInfo;
47 class MCTargetAsmParser;
48 class MCTargetOptions;
49 class MCTargetStreamer;
53 class raw_pwrite_stream;
54 class formatted_raw_ostream;
56 MCStreamer *createNullStreamer(MCContext &Ctx);
57 MCStreamer *createAsmStreamer(MCContext &Ctx,
58 std::unique_ptr<formatted_raw_ostream> OS,
59 bool isVerboseAsm, bool useDwarfDirectory,
60 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
61 MCAsmBackend *TAB, bool ShowInst);
63 /// Takes ownership of \p TAB and \p CE.
64 MCStreamer *createELFStreamer(MCContext &Ctx, MCAsmBackend &TAB,
65 raw_pwrite_stream &OS, MCCodeEmitter *CE,
67 MCStreamer *createMachOStreamer(MCContext &Ctx, MCAsmBackend &TAB,
68 raw_pwrite_stream &OS, MCCodeEmitter *CE,
69 bool RelaxAll, bool DWARFMustBeAtTheEnd,
70 bool LabelSections = false);
72 MCRelocationInfo *createMCRelocationInfo(const Triple &TT, MCContext &Ctx);
74 MCSymbolizer *createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo,
75 LLVMSymbolLookupCallback SymbolLookUp,
76 void *DisInfo, MCContext *Ctx,
77 std::unique_ptr<MCRelocationInfo> &&RelInfo);
79 /// Target - Wrapper for Target specific information.
81 /// For registration purposes, this is a POD type so that targets can be
82 /// registered without the use of static constructors.
84 /// Targets should implement a single global instance of this class (which
85 /// will be zero initialized), and pass that instance to the TargetRegistry as
86 /// part of their initialization.
89 friend struct TargetRegistry;
91 typedef bool (*ArchMatchFnTy)(Triple::ArchType Arch);
93 typedef MCAsmInfo *(*MCAsmInfoCtorFnTy)(const MCRegisterInfo &MRI,
95 typedef void (*MCAdjustCodeGenOptsFnTy)(const Triple &TT, Reloc::Model RM,
96 CodeModel::Model &CM);
98 typedef MCInstrInfo *(*MCInstrInfoCtorFnTy)(void);
99 typedef MCInstrAnalysis *(*MCInstrAnalysisCtorFnTy)(const MCInstrInfo *Info);
100 typedef MCRegisterInfo *(*MCRegInfoCtorFnTy)(const Triple &TT);
101 typedef MCSubtargetInfo *(*MCSubtargetInfoCtorFnTy)(const Triple &TT,
104 typedef TargetMachine *(*TargetMachineCtorTy)(
105 const Target &T, const Triple &TT, StringRef CPU, StringRef Features,
106 const TargetOptions &Options, Optional<Reloc::Model> RM,
107 CodeModel::Model CM, CodeGenOpt::Level OL);
108 // If it weren't for layering issues (this header is in llvm/Support, but
109 // depends on MC?) this should take the Streamer by value rather than rvalue
111 typedef AsmPrinter *(*AsmPrinterCtorTy)(
112 TargetMachine &TM, std::unique_ptr<MCStreamer> &&Streamer);
113 typedef MCAsmBackend *(*MCAsmBackendCtorTy)(const Target &T,
114 const MCRegisterInfo &MRI,
115 const Triple &TT, StringRef CPU,
116 const MCTargetOptions &Options);
117 typedef MCTargetAsmParser *(*MCAsmParserCtorTy)(
118 const MCSubtargetInfo &STI, MCAsmParser &P, const MCInstrInfo &MII,
119 const MCTargetOptions &Options);
120 typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T,
121 const MCSubtargetInfo &STI,
123 typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Triple &T,
124 unsigned SyntaxVariant,
125 const MCAsmInfo &MAI,
126 const MCInstrInfo &MII,
127 const MCRegisterInfo &MRI);
128 typedef MCCodeEmitter *(*MCCodeEmitterCtorTy)(const MCInstrInfo &II,
129 const MCRegisterInfo &MRI,
131 typedef MCStreamer *(*ELFStreamerCtorTy)(const Triple &T, MCContext &Ctx,
133 raw_pwrite_stream &OS,
134 MCCodeEmitter *Emitter,
136 typedef MCStreamer *(*MachOStreamerCtorTy)(MCContext &Ctx, MCAsmBackend &TAB,
137 raw_pwrite_stream &OS,
138 MCCodeEmitter *Emitter,
140 bool DWARFMustBeAtTheEnd);
141 typedef MCStreamer *(*COFFStreamerCtorTy)(MCContext &Ctx, MCAsmBackend &TAB,
142 raw_pwrite_stream &OS,
143 MCCodeEmitter *Emitter,
145 bool IncrementalLinkerCompatible);
146 typedef MCTargetStreamer *(*NullTargetStreamerCtorTy)(MCStreamer &S);
147 typedef MCTargetStreamer *(*AsmTargetStreamerCtorTy)(
148 MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint,
150 typedef MCTargetStreamer *(*ObjectTargetStreamerCtorTy)(
151 MCStreamer &S, const MCSubtargetInfo &STI);
152 typedef MCRelocationInfo *(*MCRelocationInfoCtorTy)(const Triple &TT,
154 typedef MCSymbolizer *(*MCSymbolizerCtorTy)(
155 const Triple &TT, LLVMOpInfoCallback GetOpInfo,
156 LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx,
157 std::unique_ptr<MCRelocationInfo> &&RelInfo);
160 /// Next - The next registered target in the linked list, maintained by the
164 /// The target function for checking if an architecture is supported.
165 ArchMatchFnTy ArchMatchFn;
167 /// Name - The target name.
170 /// ShortDesc - A short description of the target.
171 const char *ShortDesc;
173 /// HasJIT - Whether this target supports the JIT.
176 /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
178 MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
180 MCAdjustCodeGenOptsFnTy MCAdjustCodeGenOptsFn;
182 /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
184 MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
186 /// MCInstrAnalysisCtorFn - Constructor function for this target's
187 /// MCInstrAnalysis, if registered.
188 MCInstrAnalysisCtorFnTy MCInstrAnalysisCtorFn;
190 /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
192 MCRegInfoCtorFnTy MCRegInfoCtorFn;
194 /// MCSubtargetInfoCtorFn - Constructor function for this target's
195 /// MCSubtargetInfo, if registered.
196 MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn;
198 /// TargetMachineCtorFn - Construction function for this target's
199 /// TargetMachine, if registered.
200 TargetMachineCtorTy TargetMachineCtorFn;
202 /// MCAsmBackendCtorFn - Construction function for this target's
203 /// MCAsmBackend, if registered.
204 MCAsmBackendCtorTy MCAsmBackendCtorFn;
206 /// MCAsmParserCtorFn - Construction function for this target's
207 /// MCTargetAsmParser, if registered.
208 MCAsmParserCtorTy MCAsmParserCtorFn;
210 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
212 AsmPrinterCtorTy AsmPrinterCtorFn;
214 /// MCDisassemblerCtorFn - Construction function for this target's
215 /// MCDisassembler, if registered.
216 MCDisassemblerCtorTy MCDisassemblerCtorFn;
218 /// MCInstPrinterCtorFn - Construction function for this target's
219 /// MCInstPrinter, if registered.
220 MCInstPrinterCtorTy MCInstPrinterCtorFn;
222 /// MCCodeEmitterCtorFn - Construction function for this target's
223 /// CodeEmitter, if registered.
224 MCCodeEmitterCtorTy MCCodeEmitterCtorFn;
226 // Construction functions for the various object formats, if registered.
227 COFFStreamerCtorTy COFFStreamerCtorFn;
228 MachOStreamerCtorTy MachOStreamerCtorFn;
229 ELFStreamerCtorTy ELFStreamerCtorFn;
231 /// Construction function for this target's null TargetStreamer, if
232 /// registered (default = nullptr).
233 NullTargetStreamerCtorTy NullTargetStreamerCtorFn;
235 /// Construction function for this target's asm TargetStreamer, if
236 /// registered (default = nullptr).
237 AsmTargetStreamerCtorTy AsmTargetStreamerCtorFn;
239 /// Construction function for this target's obj TargetStreamer, if
240 /// registered (default = nullptr).
241 ObjectTargetStreamerCtorTy ObjectTargetStreamerCtorFn;
243 /// MCRelocationInfoCtorFn - Construction function for this target's
244 /// MCRelocationInfo, if registered (default = llvm::createMCRelocationInfo)
245 MCRelocationInfoCtorTy MCRelocationInfoCtorFn;
247 /// MCSymbolizerCtorFn - Construction function for this target's
248 /// MCSymbolizer, if registered (default = llvm::createMCSymbolizer)
249 MCSymbolizerCtorTy MCSymbolizerCtorFn;
253 : COFFStreamerCtorFn(nullptr), MachOStreamerCtorFn(nullptr),
254 ELFStreamerCtorFn(nullptr), NullTargetStreamerCtorFn(nullptr),
255 AsmTargetStreamerCtorFn(nullptr), ObjectTargetStreamerCtorFn(nullptr),
256 MCRelocationInfoCtorFn(nullptr), MCSymbolizerCtorFn(nullptr) {}
258 /// @name Target Information
261 // getNext - Return the next registered target.
262 const Target *getNext() const { return Next; }
264 /// getName - Get the target name.
265 const char *getName() const { return Name; }
267 /// getShortDescription - Get a short description of the target.
268 const char *getShortDescription() const { return ShortDesc; }
271 /// @name Feature Predicates
274 /// hasJIT - Check if this targets supports the just-in-time compilation.
275 bool hasJIT() const { return HasJIT; }
277 /// hasTargetMachine - Check if this target supports code generation.
278 bool hasTargetMachine() const { return TargetMachineCtorFn != nullptr; }
280 /// hasMCAsmBackend - Check if this target supports .o generation.
281 bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != nullptr; }
283 /// hasMCAsmParser - Check if this target supports assembly parsing.
284 bool hasMCAsmParser() const { return MCAsmParserCtorFn != nullptr; }
287 /// @name Feature Constructors
290 /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
293 /// \param TheTriple This argument is used to determine the target machine
294 /// feature set; it should always be provided. Generally this should be
295 /// either the target triple from the module, or the target triple of the
296 /// host if that does not exist.
297 MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI,
298 StringRef TheTriple) const {
299 if (!MCAsmInfoCtorFn)
301 return MCAsmInfoCtorFn(MRI, Triple(TheTriple));
304 void adjustCodeGenOpts(const Triple &TT, Reloc::Model RM,
305 CodeModel::Model &CM) const {
306 if (MCAdjustCodeGenOptsFn)
307 MCAdjustCodeGenOptsFn(TT, RM, CM);
310 /// createMCInstrInfo - Create a MCInstrInfo implementation.
312 MCInstrInfo *createMCInstrInfo() const {
313 if (!MCInstrInfoCtorFn)
315 return MCInstrInfoCtorFn();
318 /// createMCInstrAnalysis - Create a MCInstrAnalysis implementation.
320 MCInstrAnalysis *createMCInstrAnalysis(const MCInstrInfo *Info) const {
321 if (!MCInstrAnalysisCtorFn)
323 return MCInstrAnalysisCtorFn(Info);
326 /// createMCRegInfo - Create a MCRegisterInfo implementation.
328 MCRegisterInfo *createMCRegInfo(StringRef TT) const {
329 if (!MCRegInfoCtorFn)
331 return MCRegInfoCtorFn(Triple(TT));
334 /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
336 /// \param TheTriple This argument is used to determine the target machine
337 /// feature set; it should always be provided. Generally this should be
338 /// either the target triple from the module, or the target triple of the
339 /// host if that does not exist.
340 /// \param CPU This specifies the name of the target CPU.
341 /// \param Features This specifies the string representation of the
342 /// additional target features.
343 MCSubtargetInfo *createMCSubtargetInfo(StringRef TheTriple, StringRef CPU,
344 StringRef Features) const {
345 if (!MCSubtargetInfoCtorFn)
347 return MCSubtargetInfoCtorFn(Triple(TheTriple), CPU, Features);
350 /// createTargetMachine - Create a target specific machine implementation
351 /// for the specified \p Triple.
353 /// \param TT This argument is used to determine the target machine
354 /// feature set; it should always be provided. Generally this should be
355 /// either the target triple from the module, or the target triple of the
356 /// host if that does not exist.
358 createTargetMachine(StringRef TT, StringRef CPU, StringRef Features,
359 const TargetOptions &Options, Optional<Reloc::Model> RM,
360 CodeModel::Model CM = CodeModel::Default,
361 CodeGenOpt::Level OL = CodeGenOpt::Default) const {
362 if (!TargetMachineCtorFn)
364 return TargetMachineCtorFn(*this, Triple(TT), CPU, Features, Options, RM,
368 /// createMCAsmBackend - Create a target specific assembly parser.
370 /// \param TheTriple The target triple string.
371 MCAsmBackend *createMCAsmBackend(const MCRegisterInfo &MRI,
372 StringRef TheTriple, StringRef CPU,
373 const MCTargetOptions &Options)
375 if (!MCAsmBackendCtorFn)
377 return MCAsmBackendCtorFn(*this, MRI, Triple(TheTriple), CPU, Options);
380 /// createMCAsmParser - Create a target specific assembly parser.
382 /// \param Parser The target independent parser implementation to use for
383 /// parsing and lexing.
384 MCTargetAsmParser *createMCAsmParser(const MCSubtargetInfo &STI,
386 const MCInstrInfo &MII,
387 const MCTargetOptions &Options) const {
388 if (!MCAsmParserCtorFn)
390 return MCAsmParserCtorFn(STI, Parser, MII, Options);
393 /// createAsmPrinter - Create a target specific assembly printer pass. This
394 /// takes ownership of the MCStreamer object.
395 AsmPrinter *createAsmPrinter(TargetMachine &TM,
396 std::unique_ptr<MCStreamer> &&Streamer) const {
397 if (!AsmPrinterCtorFn)
399 return AsmPrinterCtorFn(TM, std::move(Streamer));
402 MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI,
403 MCContext &Ctx) const {
404 if (!MCDisassemblerCtorFn)
406 return MCDisassemblerCtorFn(*this, STI, Ctx);
409 MCInstPrinter *createMCInstPrinter(const Triple &T, unsigned SyntaxVariant,
410 const MCAsmInfo &MAI,
411 const MCInstrInfo &MII,
412 const MCRegisterInfo &MRI) const {
413 if (!MCInstPrinterCtorFn)
415 return MCInstPrinterCtorFn(T, SyntaxVariant, MAI, MII, MRI);
418 /// createMCCodeEmitter - Create a target specific code emitter.
419 MCCodeEmitter *createMCCodeEmitter(const MCInstrInfo &II,
420 const MCRegisterInfo &MRI,
421 MCContext &Ctx) const {
422 if (!MCCodeEmitterCtorFn)
424 return MCCodeEmitterCtorFn(II, MRI, Ctx);
427 /// Create a target specific MCStreamer.
429 /// \param T The target triple.
430 /// \param Ctx The target context.
431 /// \param TAB The target assembler backend object. Takes ownership.
432 /// \param OS The stream object.
433 /// \param Emitter The target independent assembler object.Takes ownership.
434 /// \param RelaxAll Relax all fixups?
435 MCStreamer *createMCObjectStreamer(const Triple &T, MCContext &Ctx,
436 MCAsmBackend &TAB, raw_pwrite_stream &OS,
437 MCCodeEmitter *Emitter,
438 const MCSubtargetInfo &STI, bool RelaxAll,
439 bool IncrementalLinkerCompatible,
440 bool DWARFMustBeAtTheEnd) const {
442 switch (T.getObjectFormat()) {
444 llvm_unreachable("Unknown object format");
446 assert(T.isOSWindows() && "only Windows COFF is supported");
447 S = COFFStreamerCtorFn(Ctx, TAB, OS, Emitter, RelaxAll,
448 IncrementalLinkerCompatible);
451 if (MachOStreamerCtorFn)
452 S = MachOStreamerCtorFn(Ctx, TAB, OS, Emitter, RelaxAll,
453 DWARFMustBeAtTheEnd);
455 S = createMachOStreamer(Ctx, TAB, OS, Emitter, RelaxAll,
456 DWARFMustBeAtTheEnd);
459 if (ELFStreamerCtorFn)
460 S = ELFStreamerCtorFn(T, Ctx, TAB, OS, Emitter, RelaxAll);
462 S = createELFStreamer(Ctx, TAB, OS, Emitter, RelaxAll);
465 if (ObjectTargetStreamerCtorFn)
466 ObjectTargetStreamerCtorFn(*S, STI);
470 MCStreamer *createAsmStreamer(MCContext &Ctx,
471 std::unique_ptr<formatted_raw_ostream> OS,
472 bool IsVerboseAsm, bool UseDwarfDirectory,
473 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
474 MCAsmBackend *TAB, bool ShowInst) const {
475 formatted_raw_ostream &OSRef = *OS;
476 MCStreamer *S = llvm::createAsmStreamer(Ctx, std::move(OS), IsVerboseAsm,
477 UseDwarfDirectory, InstPrint, CE,
479 createAsmTargetStreamer(*S, OSRef, InstPrint, IsVerboseAsm);
483 MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
484 formatted_raw_ostream &OS,
485 MCInstPrinter *InstPrint,
486 bool IsVerboseAsm) const {
487 if (AsmTargetStreamerCtorFn)
488 return AsmTargetStreamerCtorFn(S, OS, InstPrint, IsVerboseAsm);
492 MCStreamer *createNullStreamer(MCContext &Ctx) const {
493 MCStreamer *S = llvm::createNullStreamer(Ctx);
494 createNullTargetStreamer(*S);
498 MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) const {
499 if (NullTargetStreamerCtorFn)
500 return NullTargetStreamerCtorFn(S);
504 /// createMCRelocationInfo - Create a target specific MCRelocationInfo.
506 /// \param TT The target triple.
507 /// \param Ctx The target context.
508 MCRelocationInfo *createMCRelocationInfo(StringRef TT, MCContext &Ctx) const {
509 MCRelocationInfoCtorTy Fn = MCRelocationInfoCtorFn
510 ? MCRelocationInfoCtorFn
511 : llvm::createMCRelocationInfo;
512 return Fn(Triple(TT), Ctx);
515 /// createMCSymbolizer - Create a target specific MCSymbolizer.
517 /// \param TT The target triple.
518 /// \param GetOpInfo The function to get the symbolic information for
520 /// \param SymbolLookUp The function to lookup a symbol name.
521 /// \param DisInfo The pointer to the block of symbolic information for above
524 /// \param Ctx The target context.
525 /// \param RelInfo The relocation information for this target. Takes
528 createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
529 LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo,
531 std::unique_ptr<MCRelocationInfo> &&RelInfo) const {
532 MCSymbolizerCtorTy Fn =
533 MCSymbolizerCtorFn ? MCSymbolizerCtorFn : llvm::createMCSymbolizer;
534 return Fn(Triple(TT), GetOpInfo, SymbolLookUp, DisInfo, Ctx,
541 /// TargetRegistry - Generic interface to target specific features.
542 struct TargetRegistry {
543 // FIXME: Make this a namespace, probably just move all the Register*
544 // functions into Target (currently they all just set members on the Target
545 // anyway, and Target friends this class so those functions can...
547 TargetRegistry() = delete;
550 : public std::iterator<std::forward_iterator_tag, Target, ptrdiff_t> {
551 const Target *Current;
552 explicit iterator(Target *T) : Current(T) {}
553 friend struct TargetRegistry;
556 iterator() : Current(nullptr) {}
558 bool operator==(const iterator &x) const { return Current == x.Current; }
559 bool operator!=(const iterator &x) const { return !operator==(x); }
561 // Iterator traversal: forward iteration only
562 iterator &operator++() { // Preincrement
563 assert(Current && "Cannot increment end iterator!");
564 Current = Current->getNext();
567 iterator operator++(int) { // Postincrement
568 iterator tmp = *this;
573 const Target &operator*() const {
574 assert(Current && "Cannot dereference end iterator!");
578 const Target *operator->() const { return &operator*(); }
581 /// printRegisteredTargetsForVersion - Print the registered targets
582 /// appropriately for inclusion in a tool's version output.
583 static void printRegisteredTargetsForVersion();
585 /// @name Registry Access
588 static iterator_range<iterator> targets();
590 /// lookupTarget - Lookup a target based on a target triple.
592 /// \param Triple - The triple to use for finding a target.
593 /// \param Error - On failure, an error string describing why no target was
595 static const Target *lookupTarget(const std::string &Triple,
598 /// lookupTarget - Lookup a target based on an architecture name
599 /// and a target triple. If the architecture name is non-empty,
600 /// then the lookup is done by architecture. Otherwise, the target
603 /// \param ArchName - The architecture to use for finding a target.
604 /// \param TheTriple - The triple to use for finding a target. The
605 /// triple is updated with canonical architecture name if a lookup
606 /// by architecture is done.
607 /// \param Error - On failure, an error string describing why no target was
609 static const Target *lookupTarget(const std::string &ArchName,
610 Triple &TheTriple, std::string &Error);
613 /// @name Target Registration
616 /// RegisterTarget - Register the given target. Attempts to register a
617 /// target which has already been registered will be ignored.
619 /// Clients are responsible for ensuring that registration doesn't occur
620 /// while another thread is attempting to access the registry. Typically
621 /// this is done by initializing all targets at program startup.
623 /// @param T - The target being registered.
624 /// @param Name - The target name. This should be a static string.
625 /// @param ShortDesc - A short target description. This should be a static
627 /// @param ArchMatchFn - The arch match checking function for this target.
628 /// @param HasJIT - Whether the target supports JIT code
630 static void RegisterTarget(Target &T, const char *Name, const char *ShortDesc,
631 Target::ArchMatchFnTy ArchMatchFn,
632 bool HasJIT = false);
634 /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
637 /// Clients are responsible for ensuring that registration doesn't occur
638 /// while another thread is attempting to access the registry. Typically
639 /// this is done by initializing all targets at program startup.
641 /// @param T - The target being registered.
642 /// @param Fn - A function to construct a MCAsmInfo for the target.
643 static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
644 T.MCAsmInfoCtorFn = Fn;
647 static void registerMCAdjustCodeGenOpts(Target &T,
648 Target::MCAdjustCodeGenOptsFnTy Fn) {
649 T.MCAdjustCodeGenOptsFn = Fn;
652 /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
655 /// Clients are responsible for ensuring that registration doesn't occur
656 /// while another thread is attempting to access the registry. Typically
657 /// this is done by initializing all targets at program startup.
659 /// @param T - The target being registered.
660 /// @param Fn - A function to construct a MCInstrInfo for the target.
661 static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
662 T.MCInstrInfoCtorFn = Fn;
665 /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for
666 /// the given target.
667 static void RegisterMCInstrAnalysis(Target &T,
668 Target::MCInstrAnalysisCtorFnTy Fn) {
669 T.MCInstrAnalysisCtorFn = Fn;
672 /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
675 /// Clients are responsible for ensuring that registration doesn't occur
676 /// while another thread is attempting to access the registry. Typically
677 /// this is done by initializing all targets at program startup.
679 /// @param T - The target being registered.
680 /// @param Fn - A function to construct a MCRegisterInfo for the target.
681 static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
682 T.MCRegInfoCtorFn = Fn;
685 /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
686 /// the given target.
688 /// Clients are responsible for ensuring that registration doesn't occur
689 /// while another thread is attempting to access the registry. Typically
690 /// this is done by initializing all targets at program startup.
692 /// @param T - The target being registered.
693 /// @param Fn - A function to construct a MCSubtargetInfo for the target.
694 static void RegisterMCSubtargetInfo(Target &T,
695 Target::MCSubtargetInfoCtorFnTy Fn) {
696 T.MCSubtargetInfoCtorFn = Fn;
699 /// RegisterTargetMachine - Register a TargetMachine implementation for the
702 /// Clients are responsible for ensuring that registration doesn't occur
703 /// while another thread is attempting to access the registry. Typically
704 /// this is done by initializing all targets at program startup.
706 /// @param T - The target being registered.
707 /// @param Fn - A function to construct a TargetMachine for the target.
708 static void RegisterTargetMachine(Target &T, Target::TargetMachineCtorTy Fn) {
709 T.TargetMachineCtorFn = Fn;
712 /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the
715 /// Clients are responsible for ensuring that registration doesn't occur
716 /// while another thread is attempting to access the registry. Typically
717 /// this is done by initializing all targets at program startup.
719 /// @param T - The target being registered.
720 /// @param Fn - A function to construct an AsmBackend for the target.
721 static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) {
722 T.MCAsmBackendCtorFn = Fn;
725 /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for
726 /// the given target.
728 /// Clients are responsible for ensuring that registration doesn't occur
729 /// while another thread is attempting to access the registry. Typically
730 /// this is done by initializing all targets at program startup.
732 /// @param T - The target being registered.
733 /// @param Fn - A function to construct an MCTargetAsmParser for the target.
734 static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) {
735 T.MCAsmParserCtorFn = Fn;
738 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
741 /// Clients are responsible for ensuring that registration doesn't occur
742 /// while another thread is attempting to access the registry. Typically
743 /// this is done by initializing all targets at program startup.
745 /// @param T - The target being registered.
746 /// @param Fn - A function to construct an AsmPrinter for the target.
747 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
748 T.AsmPrinterCtorFn = Fn;
751 /// RegisterMCDisassembler - Register a MCDisassembler implementation for
752 /// the given target.
754 /// Clients are responsible for ensuring that registration doesn't occur
755 /// while another thread is attempting to access the registry. Typically
756 /// this is done by initializing all targets at program startup.
758 /// @param T - The target being registered.
759 /// @param Fn - A function to construct an MCDisassembler for the target.
760 static void RegisterMCDisassembler(Target &T,
761 Target::MCDisassemblerCtorTy Fn) {
762 T.MCDisassemblerCtorFn = Fn;
765 /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
768 /// Clients are responsible for ensuring that registration doesn't occur
769 /// while another thread is attempting to access the registry. Typically
770 /// this is done by initializing all targets at program startup.
772 /// @param T - The target being registered.
773 /// @param Fn - A function to construct an MCInstPrinter for the target.
774 static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn) {
775 T.MCInstPrinterCtorFn = Fn;
778 /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the
781 /// Clients are responsible for ensuring that registration doesn't occur
782 /// while another thread is attempting to access the registry. Typically
783 /// this is done by initializing all targets at program startup.
785 /// @param T - The target being registered.
786 /// @param Fn - A function to construct an MCCodeEmitter for the target.
787 static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn) {
788 T.MCCodeEmitterCtorFn = Fn;
791 static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn) {
792 T.COFFStreamerCtorFn = Fn;
795 static void RegisterMachOStreamer(Target &T, Target::MachOStreamerCtorTy Fn) {
796 T.MachOStreamerCtorFn = Fn;
799 static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn) {
800 T.ELFStreamerCtorFn = Fn;
803 static void RegisterNullTargetStreamer(Target &T,
804 Target::NullTargetStreamerCtorTy Fn) {
805 T.NullTargetStreamerCtorFn = Fn;
808 static void RegisterAsmTargetStreamer(Target &T,
809 Target::AsmTargetStreamerCtorTy Fn) {
810 T.AsmTargetStreamerCtorFn = Fn;
814 RegisterObjectTargetStreamer(Target &T,
815 Target::ObjectTargetStreamerCtorTy Fn) {
816 T.ObjectTargetStreamerCtorFn = Fn;
819 /// RegisterMCRelocationInfo - Register an MCRelocationInfo
820 /// implementation for the given target.
822 /// Clients are responsible for ensuring that registration doesn't occur
823 /// while another thread is attempting to access the registry. Typically
824 /// this is done by initializing all targets at program startup.
826 /// @param T - The target being registered.
827 /// @param Fn - A function to construct an MCRelocationInfo for the target.
828 static void RegisterMCRelocationInfo(Target &T,
829 Target::MCRelocationInfoCtorTy Fn) {
830 T.MCRelocationInfoCtorFn = Fn;
833 /// RegisterMCSymbolizer - Register an MCSymbolizer
834 /// implementation for the given target.
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.
840 /// @param T - The target being registered.
841 /// @param Fn - A function to construct an MCSymbolizer for the target.
842 static void RegisterMCSymbolizer(Target &T, Target::MCSymbolizerCtorTy Fn) {
843 T.MCSymbolizerCtorFn = Fn;
849 //===--------------------------------------------------------------------===//
851 /// RegisterTarget - Helper template for registering a target, for use in the
852 /// target's initialization function. Usage:
855 /// Target &getTheFooTarget() { // The global target instance.
856 /// static Target TheFooTarget;
857 /// return TheFooTarget;
859 /// extern "C" void LLVMInitializeFooTargetInfo() {
860 /// RegisterTarget<Triple::foo> X(getTheFooTarget(), "foo", "Foo
863 template <Triple::ArchType TargetArchType = Triple::UnknownArch,
865 struct RegisterTarget {
866 RegisterTarget(Target &T, const char *Name, const char *Desc) {
867 TargetRegistry::RegisterTarget(T, Name, Desc, &getArchMatch, HasJIT);
870 static bool getArchMatch(Triple::ArchType Arch) {
871 return Arch == TargetArchType;
875 /// RegisterMCAsmInfo - Helper template for registering a target assembly info
876 /// implementation. This invokes the static "Create" method on the class to
877 /// actually do the construction. Usage:
879 /// extern "C" void LLVMInitializeFooTarget() {
880 /// extern Target TheFooTarget;
881 /// RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
883 template <class MCAsmInfoImpl> struct RegisterMCAsmInfo {
884 RegisterMCAsmInfo(Target &T) {
885 TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
889 static MCAsmInfo *Allocator(const MCRegisterInfo & /*MRI*/,
891 return new MCAsmInfoImpl(TT);
895 /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
896 /// implementation. This invokes the specified function to do the
897 /// construction. Usage:
899 /// extern "C" void LLVMInitializeFooTarget() {
900 /// extern Target TheFooTarget;
901 /// RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
903 struct RegisterMCAsmInfoFn {
904 RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
905 TargetRegistry::RegisterMCAsmInfo(T, Fn);
909 struct RegisterMCAdjustCodeGenOptsFn {
910 RegisterMCAdjustCodeGenOptsFn(Target &T, Target::MCAdjustCodeGenOptsFnTy Fn) {
911 TargetRegistry::registerMCAdjustCodeGenOpts(T, Fn);
915 /// RegisterMCInstrInfo - Helper template for registering a target instruction
916 /// info implementation. This invokes the static "Create" method on the class
917 /// to actually do the construction. Usage:
919 /// extern "C" void LLVMInitializeFooTarget() {
920 /// extern Target TheFooTarget;
921 /// RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
923 template <class MCInstrInfoImpl> struct RegisterMCInstrInfo {
924 RegisterMCInstrInfo(Target &T) {
925 TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
929 static MCInstrInfo *Allocator() { return new MCInstrInfoImpl(); }
932 /// RegisterMCInstrInfoFn - Helper template for registering a target
933 /// instruction info implementation. This invokes the specified function to
934 /// do the construction. Usage:
936 /// extern "C" void LLVMInitializeFooTarget() {
937 /// extern Target TheFooTarget;
938 /// RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
940 struct RegisterMCInstrInfoFn {
941 RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
942 TargetRegistry::RegisterMCInstrInfo(T, Fn);
946 /// RegisterMCInstrAnalysis - Helper template for registering a target
947 /// instruction analyzer implementation. This invokes the static "Create"
948 /// method on the class to actually do the construction. Usage:
950 /// extern "C" void LLVMInitializeFooTarget() {
951 /// extern Target TheFooTarget;
952 /// RegisterMCInstrAnalysis<FooMCInstrAnalysis> X(TheFooTarget);
954 template <class MCInstrAnalysisImpl> struct RegisterMCInstrAnalysis {
955 RegisterMCInstrAnalysis(Target &T) {
956 TargetRegistry::RegisterMCInstrAnalysis(T, &Allocator);
960 static MCInstrAnalysis *Allocator(const MCInstrInfo *Info) {
961 return new MCInstrAnalysisImpl(Info);
965 /// RegisterMCInstrAnalysisFn - Helper template for registering a target
966 /// instruction analyzer implementation. This invokes the specified function
967 /// to do the construction. Usage:
969 /// extern "C" void LLVMInitializeFooTarget() {
970 /// extern Target TheFooTarget;
971 /// RegisterMCInstrAnalysisFn X(TheFooTarget, TheFunction);
973 struct RegisterMCInstrAnalysisFn {
974 RegisterMCInstrAnalysisFn(Target &T, Target::MCInstrAnalysisCtorFnTy Fn) {
975 TargetRegistry::RegisterMCInstrAnalysis(T, Fn);
979 /// RegisterMCRegInfo - Helper template for registering a target register info
980 /// implementation. This invokes the static "Create" method on the class to
981 /// actually do the construction. Usage:
983 /// extern "C" void LLVMInitializeFooTarget() {
984 /// extern Target TheFooTarget;
985 /// RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
987 template <class MCRegisterInfoImpl> struct RegisterMCRegInfo {
988 RegisterMCRegInfo(Target &T) {
989 TargetRegistry::RegisterMCRegInfo(T, &Allocator);
993 static MCRegisterInfo *Allocator(const Triple & /*TT*/) {
994 return new MCRegisterInfoImpl();
998 /// RegisterMCRegInfoFn - Helper template for registering a target register
999 /// info implementation. This invokes the specified function to do the
1000 /// construction. Usage:
1002 /// extern "C" void LLVMInitializeFooTarget() {
1003 /// extern Target TheFooTarget;
1004 /// RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
1006 struct RegisterMCRegInfoFn {
1007 RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
1008 TargetRegistry::RegisterMCRegInfo(T, Fn);
1012 /// RegisterMCSubtargetInfo - Helper template for registering a target
1013 /// subtarget info implementation. This invokes the static "Create" method
1014 /// on the class to actually do the construction. Usage:
1016 /// extern "C" void LLVMInitializeFooTarget() {
1017 /// extern Target TheFooTarget;
1018 /// RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
1020 template <class MCSubtargetInfoImpl> struct RegisterMCSubtargetInfo {
1021 RegisterMCSubtargetInfo(Target &T) {
1022 TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
1026 static MCSubtargetInfo *Allocator(const Triple & /*TT*/, StringRef /*CPU*/,
1028 return new MCSubtargetInfoImpl();
1032 /// RegisterMCSubtargetInfoFn - Helper template for registering a target
1033 /// subtarget info implementation. This invokes the specified function to
1034 /// do the construction. Usage:
1036 /// extern "C" void LLVMInitializeFooTarget() {
1037 /// extern Target TheFooTarget;
1038 /// RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
1040 struct RegisterMCSubtargetInfoFn {
1041 RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
1042 TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
1046 /// RegisterTargetMachine - Helper template for registering a target machine
1047 /// implementation, for use in the target machine initialization
1048 /// function. Usage:
1050 /// extern "C" void LLVMInitializeFooTarget() {
1051 /// extern Target TheFooTarget;
1052 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
1054 template <class TargetMachineImpl> struct RegisterTargetMachine {
1055 RegisterTargetMachine(Target &T) {
1056 TargetRegistry::RegisterTargetMachine(T, &Allocator);
1060 static TargetMachine *Allocator(const Target &T, const Triple &TT,
1061 StringRef CPU, StringRef FS,
1062 const TargetOptions &Options,
1063 Optional<Reloc::Model> RM,
1064 CodeModel::Model CM, CodeGenOpt::Level OL) {
1065 return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL);
1069 /// RegisterMCAsmBackend - Helper template for registering a target specific
1070 /// assembler backend. Usage:
1072 /// extern "C" void LLVMInitializeFooMCAsmBackend() {
1073 /// extern Target TheFooTarget;
1074 /// RegisterMCAsmBackend<FooAsmLexer> X(TheFooTarget);
1076 template <class MCAsmBackendImpl> struct RegisterMCAsmBackend {
1077 RegisterMCAsmBackend(Target &T) {
1078 TargetRegistry::RegisterMCAsmBackend(T, &Allocator);
1082 static MCAsmBackend *Allocator(const Target &T, const MCRegisterInfo &MRI,
1083 const Triple &TheTriple, StringRef CPU,
1084 const MCTargetOptions &Options) {
1085 return new MCAsmBackendImpl(T, MRI, TheTriple, CPU);
1089 /// RegisterMCAsmParser - Helper template for registering a target specific
1090 /// assembly parser, for use in the target machine initialization
1091 /// function. Usage:
1093 /// extern "C" void LLVMInitializeFooMCAsmParser() {
1094 /// extern Target TheFooTarget;
1095 /// RegisterMCAsmParser<FooAsmParser> X(TheFooTarget);
1097 template <class MCAsmParserImpl> struct RegisterMCAsmParser {
1098 RegisterMCAsmParser(Target &T) {
1099 TargetRegistry::RegisterMCAsmParser(T, &Allocator);
1103 static MCTargetAsmParser *Allocator(const MCSubtargetInfo &STI,
1104 MCAsmParser &P, const MCInstrInfo &MII,
1105 const MCTargetOptions &Options) {
1106 return new MCAsmParserImpl(STI, P, MII, Options);
1110 /// RegisterAsmPrinter - Helper template for registering a target specific
1111 /// assembly printer, for use in the target machine initialization
1112 /// function. Usage:
1114 /// extern "C" void LLVMInitializeFooAsmPrinter() {
1115 /// extern Target TheFooTarget;
1116 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
1118 template <class AsmPrinterImpl> struct RegisterAsmPrinter {
1119 RegisterAsmPrinter(Target &T) {
1120 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
1124 static AsmPrinter *Allocator(TargetMachine &TM,
1125 std::unique_ptr<MCStreamer> &&Streamer) {
1126 return new AsmPrinterImpl(TM, std::move(Streamer));
1130 /// RegisterMCCodeEmitter - Helper template for registering a target specific
1131 /// machine code emitter, for use in the target initialization
1132 /// function. Usage:
1134 /// extern "C" void LLVMInitializeFooMCCodeEmitter() {
1135 /// extern Target TheFooTarget;
1136 /// RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget);
1138 template <class MCCodeEmitterImpl> struct RegisterMCCodeEmitter {
1139 RegisterMCCodeEmitter(Target &T) {
1140 TargetRegistry::RegisterMCCodeEmitter(T, &Allocator);
1144 static MCCodeEmitter *Allocator(const MCInstrInfo & /*II*/,
1145 const MCRegisterInfo & /*MRI*/,
1146 MCContext & /*Ctx*/) {
1147 return new MCCodeEmitterImpl();