1 //===-- Target/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_TARGET_TARGETREGISTRY_H
20 #define LLVM_TARGET_TARGETREGISTRY_H
22 #include "llvm/ADT/Triple.h"
37 class TargetAsmBackend;
39 class TargetAsmParser;
43 /// Target - Wrapper for Target specific information.
45 /// For registration purposes, this is a POD type so that targets can be
46 /// registered without the use of static constructors.
48 /// Targets should implement a single global instance of this class (which
49 /// will be zero initialized), and pass that instance to the TargetRegistry as
50 /// part of their initialization.
53 friend struct TargetRegistry;
55 typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
57 typedef MCAsmInfo *(*AsmInfoCtorFnTy)(const Target &T,
59 typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
60 const std::string &TT,
61 const std::string &Features);
62 typedef AsmPrinter *(*AsmPrinterCtorTy)(TargetMachine &TM,
63 MCStreamer &Streamer);
64 typedef TargetAsmBackend *(*AsmBackendCtorTy)(const Target &T,
65 const std::string &TT);
66 typedef TargetAsmLexer *(*AsmLexerCtorTy)(const Target &T,
67 const MCAsmInfo &MAI);
68 typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &T,MCAsmParser &P,
70 typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T);
71 typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
72 unsigned SyntaxVariant,
73 const MCAsmInfo &MAI);
74 typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const Target &T,
77 typedef MCStreamer *(*ObjectStreamerCtorTy)(const Target &T,
78 const std::string &TT,
80 TargetAsmBackend &TAB,
82 MCCodeEmitter *_Emitter,
86 /// Next - The next registered target in the linked list, maintained by the
90 /// TripleMatchQualityFn - The target function for rating the match quality
92 TripleMatchQualityFnTy TripleMatchQualityFn;
94 /// Name - The target name.
97 /// ShortDesc - A short description of the target.
98 const char *ShortDesc;
100 /// HasJIT - Whether this target supports the JIT.
103 AsmInfoCtorFnTy AsmInfoCtorFn;
105 /// TargetMachineCtorFn - Construction function for this target's
106 /// TargetMachine, if registered.
107 TargetMachineCtorTy TargetMachineCtorFn;
109 /// AsmBackendCtorFn - Construction function for this target's
110 /// TargetAsmBackend, if registered.
111 AsmBackendCtorTy AsmBackendCtorFn;
113 /// AsmLexerCtorFn - Construction function for this target's TargetAsmLexer,
115 AsmLexerCtorTy AsmLexerCtorFn;
117 /// AsmParserCtorFn - Construction function for this target's
118 /// TargetAsmParser, if registered.
119 AsmParserCtorTy AsmParserCtorFn;
121 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
123 AsmPrinterCtorTy AsmPrinterCtorFn;
125 /// MCDisassemblerCtorFn - Construction function for this target's
126 /// MCDisassembler, if registered.
127 MCDisassemblerCtorTy MCDisassemblerCtorFn;
129 /// MCInstPrinterCtorFn - Construction function for this target's
130 /// MCInstPrinter, if registered.
131 MCInstPrinterCtorTy MCInstPrinterCtorFn;
133 /// CodeEmitterCtorFn - Construction function for this target's CodeEmitter,
135 CodeEmitterCtorTy CodeEmitterCtorFn;
137 /// ObjectStreamerCtorFn - Construction function for this target's
138 /// ObjectStreamer, if registered.
139 ObjectStreamerCtorTy ObjectStreamerCtorFn;
142 /// @name Target Information
145 // getNext - Return the next registered target.
146 const Target *getNext() const { return Next; }
148 /// getName - Get the target name.
149 const char *getName() const { return Name; }
151 /// getShortDescription - Get a short description of the target.
152 const char *getShortDescription() const { return ShortDesc; }
155 /// @name Feature Predicates
158 /// hasJIT - Check if this targets supports the just-in-time compilation.
159 bool hasJIT() const { return HasJIT; }
161 /// hasTargetMachine - Check if this target supports code generation.
162 bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
164 /// hasAsmBackend - Check if this target supports .o generation.
165 bool hasAsmBackend() const { return AsmBackendCtorFn != 0; }
167 /// hasAsmLexer - Check if this target supports .s lexing.
168 bool hasAsmLexer() const { return AsmLexerCtorFn != 0; }
170 /// hasAsmParser - Check if this target supports .s parsing.
171 bool hasAsmParser() const { return AsmParserCtorFn != 0; }
173 /// hasAsmPrinter - Check if this target supports .s printing.
174 bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
176 /// hasMCDisassembler - Check if this target has a disassembler.
177 bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
179 /// hasMCInstPrinter - Check if this target has an instruction printer.
180 bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
182 /// hasCodeEmitter - Check if this target supports instruction encoding.
183 bool hasCodeEmitter() const { return CodeEmitterCtorFn != 0; }
185 /// hasObjectStreamer - Check if this target supports streaming to files.
186 bool hasObjectStreamer() const { return ObjectStreamerCtorFn != 0; }
189 /// @name Feature Constructors
192 /// createAsmInfo - Create a MCAsmInfo implementation for the specified
195 /// \arg Triple - This argument is used to determine the target machine
196 /// feature set; it should always be provided. Generally this should be
197 /// either the target triple from the module, or the target triple of the
198 /// host if that does not exist.
199 MCAsmInfo *createAsmInfo(StringRef Triple) const {
202 return AsmInfoCtorFn(*this, Triple);
205 /// createTargetMachine - Create a target specific machine implementation
206 /// for the specified \arg Triple.
208 /// \arg Triple - This argument is used to determine the target machine
209 /// feature set; it should always be provided. Generally this should be
210 /// either the target triple from the module, or the target triple of the
211 /// host if that does not exist.
212 TargetMachine *createTargetMachine(const std::string &Triple,
213 const std::string &Features) const {
214 if (!TargetMachineCtorFn)
216 return TargetMachineCtorFn(*this, Triple, Features);
219 /// createAsmBackend - Create a target specific assembly parser.
221 /// \arg Triple - The target triple string.
222 /// \arg Backend - The target independent assembler object.
223 TargetAsmBackend *createAsmBackend(const std::string &Triple) const {
224 if (!AsmBackendCtorFn)
226 return AsmBackendCtorFn(*this, Triple);
229 /// createAsmLexer - Create a target specific assembly lexer.
231 TargetAsmLexer *createAsmLexer(const MCAsmInfo &MAI) const {
234 return AsmLexerCtorFn(*this, MAI);
237 /// createAsmParser - Create a target specific assembly parser.
239 /// \arg Parser - The target independent parser implementation to use for
240 /// parsing and lexing.
241 TargetAsmParser *createAsmParser(MCAsmParser &Parser,
242 TargetMachine &TM) const {
243 if (!AsmParserCtorFn)
245 return AsmParserCtorFn(*this, Parser, TM);
248 /// createAsmPrinter - Create a target specific assembly printer pass. This
249 /// takes ownership of the MCStreamer object.
250 AsmPrinter *createAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) const{
251 if (!AsmPrinterCtorFn)
253 return AsmPrinterCtorFn(TM, Streamer);
256 MCDisassembler *createMCDisassembler() const {
257 if (!MCDisassemblerCtorFn)
259 return MCDisassemblerCtorFn(*this);
262 MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
263 const MCAsmInfo &MAI) const {
264 if (!MCInstPrinterCtorFn)
266 return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI);
270 /// createCodeEmitter - Create a target specific code emitter.
271 MCCodeEmitter *createCodeEmitter(TargetMachine &TM, MCContext &Ctx) const {
272 if (!CodeEmitterCtorFn)
274 return CodeEmitterCtorFn(*this, TM, Ctx);
277 /// createObjectStreamer - Create a target specific MCStreamer.
279 /// \arg TT - The target triple.
280 /// \arg Ctx - The target context.
281 /// \arg TAB - The target assembler backend object. Takes ownership.
282 /// \arg _OS - The stream object.
283 /// \arg _Emitter - The target independent assembler object.Takes ownership.
284 /// \arg RelaxAll - Relax all fixups?
285 MCStreamer *createObjectStreamer(const std::string &TT, MCContext &Ctx,
286 TargetAsmBackend &TAB,
288 MCCodeEmitter *_Emitter,
289 bool RelaxAll) const {
290 if (!ObjectStreamerCtorFn)
292 return ObjectStreamerCtorFn(*this, TT, Ctx, TAB, _OS, _Emitter, RelaxAll);
298 /// TargetRegistry - Generic interface to target specific features.
299 struct TargetRegistry {
301 const Target *Current;
302 explicit iterator(Target *T) : Current(T) {}
303 friend struct TargetRegistry;
305 iterator(const iterator &I) : Current(I.Current) {}
306 iterator() : Current(0) {}
308 bool operator==(const iterator &x) const {
309 return Current == x.Current;
311 bool operator!=(const iterator &x) const {
312 return !operator==(x);
315 // Iterator traversal: forward iteration only
316 iterator &operator++() { // Preincrement
317 assert(Current && "Cannot increment end iterator!");
318 Current = Current->getNext();
321 iterator operator++(int) { // Postincrement
322 iterator tmp = *this;
327 const Target &operator*() const {
328 assert(Current && "Cannot dereference end iterator!");
332 const Target *operator->() const {
337 /// @name Registry Access
340 static iterator begin();
342 static iterator end() { return iterator(); }
344 /// lookupTarget - Lookup a target based on a target triple.
346 /// \param Triple - The triple to use for finding a target.
347 /// \param Error - On failure, an error string describing why no target was
349 static const Target *lookupTarget(const std::string &Triple,
352 /// getClosestTargetForJIT - Pick the best target that is compatible with
353 /// the current host. If no close target can be found, this returns null
354 /// and sets the Error string to a reason.
356 /// Maintained for compatibility through 2.6.
357 static const Target *getClosestTargetForJIT(std::string &Error);
360 /// @name Target Registration
363 /// RegisterTarget - Register the given target. Attempts to register a
364 /// target which has already been registered will be ignored.
366 /// Clients are responsible for ensuring that registration doesn't occur
367 /// while another thread is attempting to access the registry. Typically
368 /// this is done by initializing all targets at program startup.
370 /// @param T - The target being registered.
371 /// @param Name - The target name. This should be a static string.
372 /// @param ShortDesc - A short target description. This should be a static
374 /// @param TQualityFn - The triple match quality computation function for
376 /// @param HasJIT - Whether the target supports JIT code
378 static void RegisterTarget(Target &T,
380 const char *ShortDesc,
381 Target::TripleMatchQualityFnTy TQualityFn,
382 bool HasJIT = false);
384 /// RegisterAsmInfo - Register a MCAsmInfo implementation for the
387 /// Clients are responsible for ensuring that registration doesn't occur
388 /// while another thread is attempting to access the registry. Typically
389 /// this is done by initializing all targets at program startup.
391 /// @param T - The target being registered.
392 /// @param Fn - A function to construct a MCAsmInfo for the target.
393 static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
394 // Ignore duplicate registration.
395 if (!T.AsmInfoCtorFn)
396 T.AsmInfoCtorFn = Fn;
399 /// RegisterTargetMachine - Register a TargetMachine implementation for the
402 /// Clients are responsible for ensuring that registration doesn't occur
403 /// while another thread is attempting to access the registry. Typically
404 /// this is done by initializing all targets at program startup.
406 /// @param T - The target being registered.
407 /// @param Fn - A function to construct a TargetMachine for the target.
408 static void RegisterTargetMachine(Target &T,
409 Target::TargetMachineCtorTy Fn) {
410 // Ignore duplicate registration.
411 if (!T.TargetMachineCtorFn)
412 T.TargetMachineCtorFn = Fn;
415 /// RegisterAsmBackend - Register a TargetAsmBackend implementation for the
418 /// Clients are responsible for ensuring that registration doesn't occur
419 /// while another thread is attempting to access the registry. Typically
420 /// this is done by initializing all targets at program startup.
422 /// @param T - The target being registered.
423 /// @param Fn - A function to construct an AsmBackend for the target.
424 static void RegisterAsmBackend(Target &T, Target::AsmBackendCtorTy Fn) {
425 if (!T.AsmBackendCtorFn)
426 T.AsmBackendCtorFn = Fn;
429 /// RegisterAsmLexer - Register a TargetAsmLexer implementation for the
432 /// Clients are responsible for ensuring that registration doesn't occur
433 /// while another thread is attempting to access the registry. Typically
434 /// this is done by initializing all targets at program startup.
436 /// @param T - The target being registered.
437 /// @param Fn - A function to construct an AsmLexer for the target.
438 static void RegisterAsmLexer(Target &T, Target::AsmLexerCtorTy Fn) {
439 if (!T.AsmLexerCtorFn)
440 T.AsmLexerCtorFn = Fn;
443 /// RegisterAsmParser - Register a TargetAsmParser implementation for the
446 /// Clients are responsible for ensuring that registration doesn't occur
447 /// while another thread is attempting to access the registry. Typically
448 /// this is done by initializing all targets at program startup.
450 /// @param T - The target being registered.
451 /// @param Fn - A function to construct an AsmParser for the target.
452 static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
453 if (!T.AsmParserCtorFn)
454 T.AsmParserCtorFn = Fn;
457 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
460 /// Clients are responsible for ensuring that registration doesn't occur
461 /// while another thread is attempting to access the registry. Typically
462 /// this is done by initializing all targets at program startup.
464 /// @param T - The target being registered.
465 /// @param Fn - A function to construct an AsmPrinter for the target.
466 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
467 // Ignore duplicate registration.
468 if (!T.AsmPrinterCtorFn)
469 T.AsmPrinterCtorFn = Fn;
472 /// RegisterMCDisassembler - Register a MCDisassembler implementation for
473 /// the given target.
475 /// Clients are responsible for ensuring that registration doesn't occur
476 /// while another thread is attempting to access the registry. Typically
477 /// this is done by initializing all targets at program startup.
479 /// @param T - The target being registered.
480 /// @param Fn - A function to construct an MCDisassembler for the target.
481 static void RegisterMCDisassembler(Target &T,
482 Target::MCDisassemblerCtorTy Fn) {
483 if (!T.MCDisassemblerCtorFn)
484 T.MCDisassemblerCtorFn = Fn;
487 /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
490 /// Clients are responsible for ensuring that registration doesn't occur
491 /// while another thread is attempting to access the registry. Typically
492 /// this is done by initializing all targets at program startup.
494 /// @param T - The target being registered.
495 /// @param Fn - A function to construct an MCInstPrinter for the target.
496 static void RegisterMCInstPrinter(Target &T,
497 Target::MCInstPrinterCtorTy Fn) {
498 if (!T.MCInstPrinterCtorFn)
499 T.MCInstPrinterCtorFn = Fn;
502 /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
505 /// Clients are responsible for ensuring that registration doesn't occur
506 /// while another thread is attempting to access the registry. Typically
507 /// this is done by initializing all targets at program startup.
509 /// @param T - The target being registered.
510 /// @param Fn - A function to construct an MCCodeEmitter for the target.
511 static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
512 if (!T.CodeEmitterCtorFn)
513 T.CodeEmitterCtorFn = Fn;
516 /// RegisterObjectStreamer - Register an MCStreamer implementation
517 /// for the given target.
519 /// Clients are responsible for ensuring that registration doesn't occur
520 /// while another thread is attempting to access the registry. Typically
521 /// this is done by initializing all targets at program startup.
523 /// @param T - The target being registered.
524 /// @param Fn - A function to construct an MCStreamer for the target.
525 static void RegisterObjectStreamer(Target &T, Target::ObjectStreamerCtorTy Fn) {
526 if (!T.ObjectStreamerCtorFn)
527 T.ObjectStreamerCtorFn = Fn;
534 //===--------------------------------------------------------------------===//
536 /// RegisterTarget - Helper template for registering a target, for use in the
537 /// target's initialization function. Usage:
540 /// Target TheFooTarget; // The global target instance.
542 /// extern "C" void LLVMInitializeFooTargetInfo() {
543 /// RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
545 template<Triple::ArchType TargetArchType = Triple::InvalidArch,
547 struct RegisterTarget {
548 RegisterTarget(Target &T, const char *Name, const char *Desc) {
549 TargetRegistry::RegisterTarget(T, Name, Desc,
550 &getTripleMatchQuality,
554 static unsigned getTripleMatchQuality(const std::string &TT) {
555 if (Triple(TT).getArch() == TargetArchType)
561 /// RegisterAsmInfo - Helper template for registering a target assembly info
562 /// implementation. This invokes the static "Create" method on the class to
563 /// actually do the construction. Usage:
565 /// extern "C" void LLVMInitializeFooTarget() {
566 /// extern Target TheFooTarget;
567 /// RegisterAsmInfo<FooMCAsmInfo> X(TheFooTarget);
569 template<class MCAsmInfoImpl>
570 struct RegisterAsmInfo {
571 RegisterAsmInfo(Target &T) {
572 TargetRegistry::RegisterAsmInfo(T, &Allocator);
575 static MCAsmInfo *Allocator(const Target &T, StringRef TT) {
576 return new MCAsmInfoImpl(T, TT);
581 /// RegisterAsmInfoFn - Helper template for registering a target assembly info
582 /// implementation. This invokes the specified function to do the
583 /// construction. Usage:
585 /// extern "C" void LLVMInitializeFooTarget() {
586 /// extern Target TheFooTarget;
587 /// RegisterAsmInfoFn X(TheFooTarget, TheFunction);
589 struct RegisterAsmInfoFn {
590 RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
591 TargetRegistry::RegisterAsmInfo(T, Fn);
596 /// RegisterTargetMachine - Helper template for registering a target machine
597 /// implementation, for use in the target machine initialization
600 /// extern "C" void LLVMInitializeFooTarget() {
601 /// extern Target TheFooTarget;
602 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
604 template<class TargetMachineImpl>
605 struct RegisterTargetMachine {
606 RegisterTargetMachine(Target &T) {
607 TargetRegistry::RegisterTargetMachine(T, &Allocator);
611 static TargetMachine *Allocator(const Target &T, const std::string &TT,
612 const std::string &FS) {
613 return new TargetMachineImpl(T, TT, FS);
617 /// RegisterAsmBackend - Helper template for registering a target specific
618 /// assembler backend. Usage:
620 /// extern "C" void LLVMInitializeFooAsmBackend() {
621 /// extern Target TheFooTarget;
622 /// RegisterAsmBackend<FooAsmLexer> X(TheFooTarget);
624 template<class AsmBackendImpl>
625 struct RegisterAsmBackend {
626 RegisterAsmBackend(Target &T) {
627 TargetRegistry::RegisterAsmBackend(T, &Allocator);
631 static TargetAsmBackend *Allocator(const Target &T,
632 const std::string &Triple) {
633 return new AsmBackendImpl(T, Triple);
637 /// RegisterAsmLexer - Helper template for registering a target specific
638 /// assembly lexer, for use in the target machine initialization
641 /// extern "C" void LLVMInitializeFooAsmLexer() {
642 /// extern Target TheFooTarget;
643 /// RegisterAsmLexer<FooAsmLexer> X(TheFooTarget);
645 template<class AsmLexerImpl>
646 struct RegisterAsmLexer {
647 RegisterAsmLexer(Target &T) {
648 TargetRegistry::RegisterAsmLexer(T, &Allocator);
652 static TargetAsmLexer *Allocator(const Target &T, const MCAsmInfo &MAI) {
653 return new AsmLexerImpl(T, MAI);
657 /// RegisterAsmParser - Helper template for registering a target specific
658 /// assembly parser, for use in the target machine initialization
661 /// extern "C" void LLVMInitializeFooAsmParser() {
662 /// extern Target TheFooTarget;
663 /// RegisterAsmParser<FooAsmParser> X(TheFooTarget);
665 template<class AsmParserImpl>
666 struct RegisterAsmParser {
667 RegisterAsmParser(Target &T) {
668 TargetRegistry::RegisterAsmParser(T, &Allocator);
672 static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P,
674 return new AsmParserImpl(T, P, TM);
678 /// RegisterAsmPrinter - Helper template for registering a target specific
679 /// assembly printer, for use in the target machine initialization
682 /// extern "C" void LLVMInitializeFooAsmPrinter() {
683 /// extern Target TheFooTarget;
684 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
686 template<class AsmPrinterImpl>
687 struct RegisterAsmPrinter {
688 RegisterAsmPrinter(Target &T) {
689 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
693 static AsmPrinter *Allocator(TargetMachine &TM, MCStreamer &Streamer) {
694 return new AsmPrinterImpl(TM, Streamer);
698 /// RegisterCodeEmitter - Helper template for registering a target specific
699 /// machine code emitter, for use in the target initialization
702 /// extern "C" void LLVMInitializeFooCodeEmitter() {
703 /// extern Target TheFooTarget;
704 /// RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
706 template<class CodeEmitterImpl>
707 struct RegisterCodeEmitter {
708 RegisterCodeEmitter(Target &T) {
709 TargetRegistry::RegisterCodeEmitter(T, &Allocator);
713 static MCCodeEmitter *Allocator(const Target &T, TargetMachine &TM,
715 return new CodeEmitterImpl(T, TM, Ctx);