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);
69 typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T);
70 typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
71 unsigned SyntaxVariant,
72 const MCAsmInfo &MAI);
73 typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const Target &T,
78 /// Next - The next registered target in the linked list, maintained by the
82 /// TripleMatchQualityFn - The target function for rating the match quality
84 TripleMatchQualityFnTy TripleMatchQualityFn;
86 /// Name - The target name.
89 /// ShortDesc - A short description of the target.
90 const char *ShortDesc;
92 /// HasJIT - Whether this target supports the JIT.
95 AsmInfoCtorFnTy AsmInfoCtorFn;
97 /// TargetMachineCtorFn - Construction function for this target's
98 /// TargetMachine, if registered.
99 TargetMachineCtorTy TargetMachineCtorFn;
101 /// AsmBackendCtorFn - Construction function for this target's
102 /// TargetAsmBackend, if registered.
103 AsmBackendCtorTy AsmBackendCtorFn;
105 /// AsmLexerCtorFn - Construction function for this target's TargetAsmLexer,
107 AsmLexerCtorTy AsmLexerCtorFn;
109 /// AsmParserCtorFn - Construction function for this target's
110 /// TargetAsmParser, if registered.
111 AsmParserCtorTy AsmParserCtorFn;
113 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
115 AsmPrinterCtorTy AsmPrinterCtorFn;
117 /// MCDisassemblerCtorFn - Construction function for this target's
118 /// MCDisassembler, if registered.
119 MCDisassemblerCtorTy MCDisassemblerCtorFn;
121 /// MCInstPrinterCtorFn - Construction function for this target's
122 /// MCInstPrinter, if registered.
123 MCInstPrinterCtorTy MCInstPrinterCtorFn;
125 /// CodeEmitterCtorFn - Construction function for this target's CodeEmitter,
127 CodeEmitterCtorTy CodeEmitterCtorFn;
130 /// @name Target Information
133 // getNext - Return the next registered target.
134 const Target *getNext() const { return Next; }
136 /// getName - Get the target name.
137 const char *getName() const { return Name; }
139 /// getShortDescription - Get a short description of the target.
140 const char *getShortDescription() const { return ShortDesc; }
143 /// @name Feature Predicates
146 /// hasJIT - Check if this targets supports the just-in-time compilation.
147 bool hasJIT() const { return HasJIT; }
149 /// hasTargetMachine - Check if this target supports code generation.
150 bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
152 /// hasAsmBackend - Check if this target supports .o generation.
153 bool hasAsmBackend() const { return AsmBackendCtorFn != 0; }
155 /// hasAsmLexer - Check if this target supports .s lexing.
156 bool hasAsmLexer() const { return AsmLexerCtorFn != 0; }
158 /// hasAsmParser - Check if this target supports .s parsing.
159 bool hasAsmParser() const { return AsmParserCtorFn != 0; }
161 /// hasAsmPrinter - Check if this target supports .s printing.
162 bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
164 /// hasMCDisassembler - Check if this target has a disassembler.
165 bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
167 /// hasMCInstPrinter - Check if this target has an instruction printer.
168 bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
170 /// hasCodeEmitter - Check if this target supports instruction encoding.
171 bool hasCodeEmitter() const { return CodeEmitterCtorFn != 0; }
174 /// @name Feature Constructors
177 /// createAsmInfo - Create a MCAsmInfo implementation for the specified
180 /// \arg Triple - This argument is used to determine the target machine
181 /// feature set; it should always be provided. Generally this should be
182 /// either the target triple from the module, or the target triple of the
183 /// host if that does not exist.
184 MCAsmInfo *createAsmInfo(StringRef Triple) const {
187 return AsmInfoCtorFn(*this, Triple);
190 /// createTargetMachine - Create a target specific machine implementation
191 /// for the specified \arg Triple.
193 /// \arg Triple - This argument is used to determine the target machine
194 /// feature set; it should always be provided. Generally this should be
195 /// either the target triple from the module, or the target triple of the
196 /// host if that does not exist.
197 TargetMachine *createTargetMachine(const std::string &Triple,
198 const std::string &Features) const {
199 if (!TargetMachineCtorFn)
201 return TargetMachineCtorFn(*this, Triple, Features);
204 /// createAsmBackend - Create a target specific assembly parser.
206 /// \arg Triple - The target triple string.
207 /// \arg Backend - The target independent assembler object.
208 TargetAsmBackend *createAsmBackend(const std::string &Triple) const {
209 if (!AsmBackendCtorFn)
211 return AsmBackendCtorFn(*this, Triple);
214 /// createAsmLexer - Create a target specific assembly lexer.
216 TargetAsmLexer *createAsmLexer(const MCAsmInfo &MAI) const {
219 return AsmLexerCtorFn(*this, MAI);
222 /// createAsmParser - Create a target specific assembly parser.
224 /// \arg Parser - The target independent parser implementation to use for
225 /// parsing and lexing.
226 TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
227 if (!AsmParserCtorFn)
229 return AsmParserCtorFn(*this, Parser);
232 /// createAsmPrinter - Create a target specific assembly printer pass. This
233 /// takes ownership of the MCStreamer object.
234 AsmPrinter *createAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) const{
235 if (!AsmPrinterCtorFn)
237 return AsmPrinterCtorFn(TM, Streamer);
240 MCDisassembler *createMCDisassembler() const {
241 if (!MCDisassemblerCtorFn)
243 return MCDisassemblerCtorFn(*this);
246 MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
247 const MCAsmInfo &MAI) const {
248 if (!MCInstPrinterCtorFn)
250 return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI);
254 /// createCodeEmitter - Create a target specific code emitter.
255 MCCodeEmitter *createCodeEmitter(TargetMachine &TM, MCContext &Ctx) const {
256 if (!CodeEmitterCtorFn)
258 return CodeEmitterCtorFn(*this, TM, Ctx);
264 /// TargetRegistry - Generic interface to target specific features.
265 struct TargetRegistry {
267 const Target *Current;
268 explicit iterator(Target *T) : Current(T) {}
269 friend struct TargetRegistry;
271 iterator(const iterator &I) : Current(I.Current) {}
272 iterator() : Current(0) {}
274 bool operator==(const iterator &x) const {
275 return Current == x.Current;
277 bool operator!=(const iterator &x) const {
278 return !operator==(x);
281 // Iterator traversal: forward iteration only
282 iterator &operator++() { // Preincrement
283 assert(Current && "Cannot increment end iterator!");
284 Current = Current->getNext();
287 iterator operator++(int) { // Postincrement
288 iterator tmp = *this;
293 const Target &operator*() const {
294 assert(Current && "Cannot dereference end iterator!");
298 const Target *operator->() const {
303 /// @name Registry Access
306 static iterator begin();
308 static iterator end() { return iterator(); }
310 /// lookupTarget - Lookup a target based on a target triple.
312 /// \param Triple - The triple to use for finding a target.
313 /// \param Error - On failure, an error string describing why no target was
315 static const Target *lookupTarget(const std::string &Triple,
318 /// getClosestTargetForJIT - Pick the best target that is compatible with
319 /// the current host. If no close target can be found, this returns null
320 /// and sets the Error string to a reason.
322 /// Maintained for compatibility through 2.6.
323 static const Target *getClosestTargetForJIT(std::string &Error);
326 /// @name Target Registration
329 /// RegisterTarget - Register the given target. Attempts to register a
330 /// target which has already been registered will be ignored.
332 /// Clients are responsible for ensuring that registration doesn't occur
333 /// while another thread is attempting to access the registry. Typically
334 /// this is done by initializing all targets at program startup.
336 /// @param T - The target being registered.
337 /// @param Name - The target name. This should be a static string.
338 /// @param ShortDesc - A short target description. This should be a static
340 /// @param TQualityFn - The triple match quality computation function for
342 /// @param HasJIT - Whether the target supports JIT code
344 static void RegisterTarget(Target &T,
346 const char *ShortDesc,
347 Target::TripleMatchQualityFnTy TQualityFn,
348 bool HasJIT = false);
350 /// RegisterAsmInfo - Register a MCAsmInfo implementation for the
353 /// Clients are responsible for ensuring that registration doesn't occur
354 /// while another thread is attempting to access the registry. Typically
355 /// this is done by initializing all targets at program startup.
357 /// @param T - The target being registered.
358 /// @param Fn - A function to construct a MCAsmInfo for the target.
359 static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
360 // Ignore duplicate registration.
361 if (!T.AsmInfoCtorFn)
362 T.AsmInfoCtorFn = Fn;
365 /// RegisterTargetMachine - Register a TargetMachine implementation for the
368 /// Clients are responsible for ensuring that registration doesn't occur
369 /// while another thread is attempting to access the registry. Typically
370 /// this is done by initializing all targets at program startup.
372 /// @param T - The target being registered.
373 /// @param Fn - A function to construct a TargetMachine for the target.
374 static void RegisterTargetMachine(Target &T,
375 Target::TargetMachineCtorTy Fn) {
376 // Ignore duplicate registration.
377 if (!T.TargetMachineCtorFn)
378 T.TargetMachineCtorFn = Fn;
381 /// RegisterAsmBackend - Register a TargetAsmBackend implementation for the
384 /// Clients are responsible for ensuring that registration doesn't occur
385 /// while another thread is attempting to access the registry. Typically
386 /// this is done by initializing all targets at program startup.
388 /// @param T - The target being registered.
389 /// @param Fn - A function to construct an AsmBackend for the target.
390 static void RegisterAsmBackend(Target &T, Target::AsmBackendCtorTy Fn) {
391 if (!T.AsmBackendCtorFn)
392 T.AsmBackendCtorFn = Fn;
395 /// RegisterAsmLexer - Register a TargetAsmLexer implementation for the
398 /// Clients are responsible for ensuring that registration doesn't occur
399 /// while another thread is attempting to access the registry. Typically
400 /// this is done by initializing all targets at program startup.
402 /// @param T - The target being registered.
403 /// @param Fn - A function to construct an AsmLexer for the target.
404 static void RegisterAsmLexer(Target &T, Target::AsmLexerCtorTy Fn) {
405 if (!T.AsmLexerCtorFn)
406 T.AsmLexerCtorFn = Fn;
409 /// RegisterAsmParser - Register a TargetAsmParser implementation for the
412 /// Clients are responsible for ensuring that registration doesn't occur
413 /// while another thread is attempting to access the registry. Typically
414 /// this is done by initializing all targets at program startup.
416 /// @param T - The target being registered.
417 /// @param Fn - A function to construct an AsmParser for the target.
418 static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
419 if (!T.AsmParserCtorFn)
420 T.AsmParserCtorFn = Fn;
423 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
426 /// Clients are responsible for ensuring that registration doesn't occur
427 /// while another thread is attempting to access the registry. Typically
428 /// this is done by initializing all targets at program startup.
430 /// @param T - The target being registered.
431 /// @param Fn - A function to construct an AsmPrinter for the target.
432 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
433 // Ignore duplicate registration.
434 if (!T.AsmPrinterCtorFn)
435 T.AsmPrinterCtorFn = Fn;
438 /// RegisterMCDisassembler - Register a MCDisassembler implementation for
439 /// the given target.
441 /// Clients are responsible for ensuring that registration doesn't occur
442 /// while another thread is attempting to access the registry. Typically
443 /// this is done by initializing all targets at program startup.
445 /// @param T - The target being registered.
446 /// @param Fn - A function to construct an MCDisassembler for the target.
447 static void RegisterMCDisassembler(Target &T,
448 Target::MCDisassemblerCtorTy Fn) {
449 if (!T.MCDisassemblerCtorFn)
450 T.MCDisassemblerCtorFn = Fn;
453 /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
456 /// Clients are responsible for ensuring that registration doesn't occur
457 /// while another thread is attempting to access the registry. Typically
458 /// this is done by initializing all targets at program startup.
460 /// @param T - The target being registered.
461 /// @param Fn - A function to construct an MCInstPrinter for the target.
462 static void RegisterMCInstPrinter(Target &T,
463 Target::MCInstPrinterCtorTy Fn) {
464 if (!T.MCInstPrinterCtorFn)
465 T.MCInstPrinterCtorFn = Fn;
468 /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
471 /// Clients are responsible for ensuring that registration doesn't occur
472 /// while another thread is attempting to access the registry. Typically
473 /// this is done by initializing all targets at program startup.
475 /// @param T - The target being registered.
476 /// @param Fn - A function to construct an MCCodeEmitter for the target.
477 static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
478 if (!T.CodeEmitterCtorFn)
479 T.CodeEmitterCtorFn = Fn;
486 //===--------------------------------------------------------------------===//
488 /// RegisterTarget - Helper template for registering a target, for use in the
489 /// target's initialization function. Usage:
492 /// Target TheFooTarget; // The global target instance.
494 /// extern "C" void LLVMInitializeFooTargetInfo() {
495 /// RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
497 template<Triple::ArchType TargetArchType = Triple::InvalidArch,
499 struct RegisterTarget {
500 RegisterTarget(Target &T, const char *Name, const char *Desc) {
501 TargetRegistry::RegisterTarget(T, Name, Desc,
502 &getTripleMatchQuality,
506 static unsigned getTripleMatchQuality(const std::string &TT) {
507 if (Triple(TT).getArch() == TargetArchType)
513 /// RegisterAsmInfo - Helper template for registering a target assembly info
514 /// implementation. This invokes the static "Create" method on the class to
515 /// actually do the construction. Usage:
517 /// extern "C" void LLVMInitializeFooTarget() {
518 /// extern Target TheFooTarget;
519 /// RegisterAsmInfo<FooMCAsmInfo> X(TheFooTarget);
521 template<class MCAsmInfoImpl>
522 struct RegisterAsmInfo {
523 RegisterAsmInfo(Target &T) {
524 TargetRegistry::RegisterAsmInfo(T, &Allocator);
527 static MCAsmInfo *Allocator(const Target &T, StringRef TT) {
528 return new MCAsmInfoImpl(T, TT);
533 /// RegisterAsmInfoFn - Helper template for registering a target assembly info
534 /// implementation. This invokes the specified function to do the
535 /// construction. Usage:
537 /// extern "C" void LLVMInitializeFooTarget() {
538 /// extern Target TheFooTarget;
539 /// RegisterAsmInfoFn X(TheFooTarget, TheFunction);
541 struct RegisterAsmInfoFn {
542 RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
543 TargetRegistry::RegisterAsmInfo(T, Fn);
548 /// RegisterTargetMachine - Helper template for registering a target machine
549 /// implementation, for use in the target machine initialization
552 /// extern "C" void LLVMInitializeFooTarget() {
553 /// extern Target TheFooTarget;
554 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
556 template<class TargetMachineImpl>
557 struct RegisterTargetMachine {
558 RegisterTargetMachine(Target &T) {
559 TargetRegistry::RegisterTargetMachine(T, &Allocator);
563 static TargetMachine *Allocator(const Target &T, const std::string &TT,
564 const std::string &FS) {
565 return new TargetMachineImpl(T, TT, FS);
569 /// RegisterAsmBackend - Helper template for registering a target specific
570 /// assembler backend. Usage:
572 /// extern "C" void LLVMInitializeFooAsmBackend() {
573 /// extern Target TheFooTarget;
574 /// RegisterAsmBackend<FooAsmLexer> X(TheFooTarget);
576 template<class AsmBackendImpl>
577 struct RegisterAsmBackend {
578 RegisterAsmBackend(Target &T) {
579 TargetRegistry::RegisterAsmBackend(T, &Allocator);
583 static TargetAsmBackend *Allocator(const Target &T,
584 const std::string &Triple) {
585 return new AsmBackendImpl(T, Triple);
589 /// RegisterAsmLexer - Helper template for registering a target specific
590 /// assembly lexer, for use in the target machine initialization
593 /// extern "C" void LLVMInitializeFooAsmLexer() {
594 /// extern Target TheFooTarget;
595 /// RegisterAsmLexer<FooAsmLexer> X(TheFooTarget);
597 template<class AsmLexerImpl>
598 struct RegisterAsmLexer {
599 RegisterAsmLexer(Target &T) {
600 TargetRegistry::RegisterAsmLexer(T, &Allocator);
604 static TargetAsmLexer *Allocator(const Target &T, const MCAsmInfo &MAI) {
605 return new AsmLexerImpl(T, MAI);
609 /// RegisterAsmParser - Helper template for registering a target specific
610 /// assembly parser, for use in the target machine initialization
613 /// extern "C" void LLVMInitializeFooAsmParser() {
614 /// extern Target TheFooTarget;
615 /// RegisterAsmParser<FooAsmParser> X(TheFooTarget);
617 template<class AsmParserImpl>
618 struct RegisterAsmParser {
619 RegisterAsmParser(Target &T) {
620 TargetRegistry::RegisterAsmParser(T, &Allocator);
624 static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
625 return new AsmParserImpl(T, P);
629 /// RegisterAsmPrinter - Helper template for registering a target specific
630 /// assembly printer, for use in the target machine initialization
633 /// extern "C" void LLVMInitializeFooAsmPrinter() {
634 /// extern Target TheFooTarget;
635 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
637 template<class AsmPrinterImpl>
638 struct RegisterAsmPrinter {
639 RegisterAsmPrinter(Target &T) {
640 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
644 static AsmPrinter *Allocator(TargetMachine &TM, MCStreamer &Streamer) {
645 return new AsmPrinterImpl(TM, Streamer);
649 /// RegisterCodeEmitter - Helper template for registering a target specific
650 /// machine code emitter, for use in the target initialization
653 /// extern "C" void LLVMInitializeFooCodeEmitter() {
654 /// extern Target TheFooTarget;
655 /// RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
657 template<class CodeEmitterImpl>
658 struct RegisterCodeEmitter {
659 RegisterCodeEmitter(Target &T) {
660 TargetRegistry::RegisterCodeEmitter(T, &Allocator);
664 static MCCodeEmitter *Allocator(const Target &T, TargetMachine &TM,
666 return new CodeEmitterImpl(T, TM, Ctx);