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,
76 typedef MCStreamer *(*ObjectStreamerCtorTy)(const Target &T,
77 const std::string &TT,
79 TargetAsmBackend &TAB,
81 MCCodeEmitter *_Emitter,
85 /// Next - The next registered target in the linked list, maintained by the
89 /// TripleMatchQualityFn - The target function for rating the match quality
91 TripleMatchQualityFnTy TripleMatchQualityFn;
93 /// Name - The target name.
96 /// ShortDesc - A short description of the target.
97 const char *ShortDesc;
99 /// HasJIT - Whether this target supports the JIT.
102 AsmInfoCtorFnTy AsmInfoCtorFn;
104 /// TargetMachineCtorFn - Construction function for this target's
105 /// TargetMachine, if registered.
106 TargetMachineCtorTy TargetMachineCtorFn;
108 /// AsmBackendCtorFn - Construction function for this target's
109 /// TargetAsmBackend, if registered.
110 AsmBackendCtorTy AsmBackendCtorFn;
112 /// AsmLexerCtorFn - Construction function for this target's TargetAsmLexer,
114 AsmLexerCtorTy AsmLexerCtorFn;
116 /// AsmParserCtorFn - Construction function for this target's
117 /// TargetAsmParser, if registered.
118 AsmParserCtorTy AsmParserCtorFn;
120 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
122 AsmPrinterCtorTy AsmPrinterCtorFn;
124 /// MCDisassemblerCtorFn - Construction function for this target's
125 /// MCDisassembler, if registered.
126 MCDisassemblerCtorTy MCDisassemblerCtorFn;
128 /// MCInstPrinterCtorFn - Construction function for this target's
129 /// MCInstPrinter, if registered.
130 MCInstPrinterCtorTy MCInstPrinterCtorFn;
132 /// CodeEmitterCtorFn - Construction function for this target's CodeEmitter,
134 CodeEmitterCtorTy CodeEmitterCtorFn;
136 /// ObjectStreamerCtorFn - Construction function for this target's
137 /// ObjectStreamer, if registered.
138 ObjectStreamerCtorTy ObjectStreamerCtorFn;
141 /// @name Target Information
144 // getNext - Return the next registered target.
145 const Target *getNext() const { return Next; }
147 /// getName - Get the target name.
148 const char *getName() const { return Name; }
150 /// getShortDescription - Get a short description of the target.
151 const char *getShortDescription() const { return ShortDesc; }
154 /// @name Feature Predicates
157 /// hasJIT - Check if this targets supports the just-in-time compilation.
158 bool hasJIT() const { return HasJIT; }
160 /// hasTargetMachine - Check if this target supports code generation.
161 bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
163 /// hasAsmBackend - Check if this target supports .o generation.
164 bool hasAsmBackend() const { return AsmBackendCtorFn != 0; }
166 /// hasAsmLexer - Check if this target supports .s lexing.
167 bool hasAsmLexer() const { return AsmLexerCtorFn != 0; }
169 /// hasAsmParser - Check if this target supports .s parsing.
170 bool hasAsmParser() const { return AsmParserCtorFn != 0; }
172 /// hasAsmPrinter - Check if this target supports .s printing.
173 bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
175 /// hasMCDisassembler - Check if this target has a disassembler.
176 bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
178 /// hasMCInstPrinter - Check if this target has an instruction printer.
179 bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
181 /// hasCodeEmitter - Check if this target supports instruction encoding.
182 bool hasCodeEmitter() const { return CodeEmitterCtorFn != 0; }
184 /// hasObjectStreamer - Check if this target supports streaming to files.
185 bool hasObjectStreamer() const { return ObjectStreamerCtorFn != 0; }
188 /// @name Feature Constructors
191 /// createAsmInfo - Create a MCAsmInfo implementation for the specified
194 /// \arg Triple - This argument is used to determine the target machine
195 /// feature set; it should always be provided. Generally this should be
196 /// either the target triple from the module, or the target triple of the
197 /// host if that does not exist.
198 MCAsmInfo *createAsmInfo(StringRef Triple) const {
201 return AsmInfoCtorFn(*this, Triple);
204 /// createTargetMachine - Create a target specific machine implementation
205 /// for the specified \arg Triple.
207 /// \arg Triple - This argument is used to determine the target machine
208 /// feature set; it should always be provided. Generally this should be
209 /// either the target triple from the module, or the target triple of the
210 /// host if that does not exist.
211 TargetMachine *createTargetMachine(const std::string &Triple,
212 const std::string &Features) const {
213 if (!TargetMachineCtorFn)
215 return TargetMachineCtorFn(*this, Triple, Features);
218 /// createAsmBackend - Create a target specific assembly parser.
220 /// \arg Triple - The target triple string.
221 /// \arg Backend - The target independent assembler object.
222 TargetAsmBackend *createAsmBackend(const std::string &Triple) const {
223 if (!AsmBackendCtorFn)
225 return AsmBackendCtorFn(*this, Triple);
228 /// createAsmLexer - Create a target specific assembly lexer.
230 TargetAsmLexer *createAsmLexer(const MCAsmInfo &MAI) const {
233 return AsmLexerCtorFn(*this, MAI);
236 /// createAsmParser - Create a target specific assembly parser.
238 /// \arg Parser - The target independent parser implementation to use for
239 /// parsing and lexing.
240 TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
241 if (!AsmParserCtorFn)
243 return AsmParserCtorFn(*this, Parser);
246 /// createAsmPrinter - Create a target specific assembly printer pass. This
247 /// takes ownership of the MCStreamer object.
248 AsmPrinter *createAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) const{
249 if (!AsmPrinterCtorFn)
251 return AsmPrinterCtorFn(TM, Streamer);
254 MCDisassembler *createMCDisassembler() const {
255 if (!MCDisassemblerCtorFn)
257 return MCDisassemblerCtorFn(*this);
260 MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
261 const MCAsmInfo &MAI) const {
262 if (!MCInstPrinterCtorFn)
264 return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI);
268 /// createCodeEmitter - Create a target specific code emitter.
269 MCCodeEmitter *createCodeEmitter(TargetMachine &TM, MCContext &Ctx) const {
270 if (!CodeEmitterCtorFn)
272 return CodeEmitterCtorFn(*this, TM, Ctx);
275 /// createObjectStreamer - Create a target specific MCStreamer.
277 /// \arg TT - The target triple.
278 /// \arg Ctx - The target context.
279 /// \arg TAB - The target assembler backend object.
280 /// \arg _OS - The stream object.
281 /// \arg _Emitter - The target independent assembler object.
282 /// \arg RelaxAll - Relax all fixups?
283 MCStreamer *createObjectStreamer(const std::string &TT, MCContext &Ctx,
284 TargetAsmBackend &TAB,
286 MCCodeEmitter *_Emitter,
287 bool RelaxAll) const {
288 if (!ObjectStreamerCtorFn)
290 return ObjectStreamerCtorFn(*this, TT, Ctx, TAB, _OS, _Emitter, RelaxAll);
296 /// TargetRegistry - Generic interface to target specific features.
297 struct TargetRegistry {
299 const Target *Current;
300 explicit iterator(Target *T) : Current(T) {}
301 friend struct TargetRegistry;
303 iterator(const iterator &I) : Current(I.Current) {}
304 iterator() : Current(0) {}
306 bool operator==(const iterator &x) const {
307 return Current == x.Current;
309 bool operator!=(const iterator &x) const {
310 return !operator==(x);
313 // Iterator traversal: forward iteration only
314 iterator &operator++() { // Preincrement
315 assert(Current && "Cannot increment end iterator!");
316 Current = Current->getNext();
319 iterator operator++(int) { // Postincrement
320 iterator tmp = *this;
325 const Target &operator*() const {
326 assert(Current && "Cannot dereference end iterator!");
330 const Target *operator->() const {
335 /// @name Registry Access
338 static iterator begin();
340 static iterator end() { return iterator(); }
342 /// lookupTarget - Lookup a target based on a target triple.
344 /// \param Triple - The triple to use for finding a target.
345 /// \param Error - On failure, an error string describing why no target was
347 static const Target *lookupTarget(const std::string &Triple,
350 /// getClosestTargetForJIT - Pick the best target that is compatible with
351 /// the current host. If no close target can be found, this returns null
352 /// and sets the Error string to a reason.
354 /// Maintained for compatibility through 2.6.
355 static const Target *getClosestTargetForJIT(std::string &Error);
358 /// @name Target Registration
361 /// RegisterTarget - Register the given target. Attempts to register a
362 /// target which has already been registered will be ignored.
364 /// Clients are responsible for ensuring that registration doesn't occur
365 /// while another thread is attempting to access the registry. Typically
366 /// this is done by initializing all targets at program startup.
368 /// @param T - The target being registered.
369 /// @param Name - The target name. This should be a static string.
370 /// @param ShortDesc - A short target description. This should be a static
372 /// @param TQualityFn - The triple match quality computation function for
374 /// @param HasJIT - Whether the target supports JIT code
376 static void RegisterTarget(Target &T,
378 const char *ShortDesc,
379 Target::TripleMatchQualityFnTy TQualityFn,
380 bool HasJIT = false);
382 /// RegisterAsmInfo - Register a MCAsmInfo implementation for the
385 /// Clients are responsible for ensuring that registration doesn't occur
386 /// while another thread is attempting to access the registry. Typically
387 /// this is done by initializing all targets at program startup.
389 /// @param T - The target being registered.
390 /// @param Fn - A function to construct a MCAsmInfo for the target.
391 static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
392 // Ignore duplicate registration.
393 if (!T.AsmInfoCtorFn)
394 T.AsmInfoCtorFn = Fn;
397 /// RegisterTargetMachine - Register a TargetMachine implementation for the
400 /// Clients are responsible for ensuring that registration doesn't occur
401 /// while another thread is attempting to access the registry. Typically
402 /// this is done by initializing all targets at program startup.
404 /// @param T - The target being registered.
405 /// @param Fn - A function to construct a TargetMachine for the target.
406 static void RegisterTargetMachine(Target &T,
407 Target::TargetMachineCtorTy Fn) {
408 // Ignore duplicate registration.
409 if (!T.TargetMachineCtorFn)
410 T.TargetMachineCtorFn = Fn;
413 /// RegisterAsmBackend - Register a TargetAsmBackend implementation for the
416 /// Clients are responsible for ensuring that registration doesn't occur
417 /// while another thread is attempting to access the registry. Typically
418 /// this is done by initializing all targets at program startup.
420 /// @param T - The target being registered.
421 /// @param Fn - A function to construct an AsmBackend for the target.
422 static void RegisterAsmBackend(Target &T, Target::AsmBackendCtorTy Fn) {
423 if (!T.AsmBackendCtorFn)
424 T.AsmBackendCtorFn = Fn;
427 /// RegisterAsmLexer - Register a TargetAsmLexer implementation for the
430 /// Clients are responsible for ensuring that registration doesn't occur
431 /// while another thread is attempting to access the registry. Typically
432 /// this is done by initializing all targets at program startup.
434 /// @param T - The target being registered.
435 /// @param Fn - A function to construct an AsmLexer for the target.
436 static void RegisterAsmLexer(Target &T, Target::AsmLexerCtorTy Fn) {
437 if (!T.AsmLexerCtorFn)
438 T.AsmLexerCtorFn = Fn;
441 /// RegisterAsmParser - Register a TargetAsmParser implementation for the
444 /// Clients are responsible for ensuring that registration doesn't occur
445 /// while another thread is attempting to access the registry. Typically
446 /// this is done by initializing all targets at program startup.
448 /// @param T - The target being registered.
449 /// @param Fn - A function to construct an AsmParser for the target.
450 static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
451 if (!T.AsmParserCtorFn)
452 T.AsmParserCtorFn = Fn;
455 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
458 /// Clients are responsible for ensuring that registration doesn't occur
459 /// while another thread is attempting to access the registry. Typically
460 /// this is done by initializing all targets at program startup.
462 /// @param T - The target being registered.
463 /// @param Fn - A function to construct an AsmPrinter for the target.
464 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
465 // Ignore duplicate registration.
466 if (!T.AsmPrinterCtorFn)
467 T.AsmPrinterCtorFn = Fn;
470 /// RegisterMCDisassembler - Register a MCDisassembler implementation for
471 /// the given target.
473 /// Clients are responsible for ensuring that registration doesn't occur
474 /// while another thread is attempting to access the registry. Typically
475 /// this is done by initializing all targets at program startup.
477 /// @param T - The target being registered.
478 /// @param Fn - A function to construct an MCDisassembler for the target.
479 static void RegisterMCDisassembler(Target &T,
480 Target::MCDisassemblerCtorTy Fn) {
481 if (!T.MCDisassemblerCtorFn)
482 T.MCDisassemblerCtorFn = Fn;
485 /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
488 /// Clients are responsible for ensuring that registration doesn't occur
489 /// while another thread is attempting to access the registry. Typically
490 /// this is done by initializing all targets at program startup.
492 /// @param T - The target being registered.
493 /// @param Fn - A function to construct an MCInstPrinter for the target.
494 static void RegisterMCInstPrinter(Target &T,
495 Target::MCInstPrinterCtorTy Fn) {
496 if (!T.MCInstPrinterCtorFn)
497 T.MCInstPrinterCtorFn = Fn;
500 /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
503 /// Clients are responsible for ensuring that registration doesn't occur
504 /// while another thread is attempting to access the registry. Typically
505 /// this is done by initializing all targets at program startup.
507 /// @param T - The target being registered.
508 /// @param Fn - A function to construct an MCCodeEmitter for the target.
509 static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
510 if (!T.CodeEmitterCtorFn)
511 T.CodeEmitterCtorFn = Fn;
514 /// RegisterObjectStreamer - Register an MCStreamer implementation
515 /// for the given target.
517 /// Clients are responsible for ensuring that registration doesn't occur
518 /// while another thread is attempting to access the registry. Typically
519 /// this is done by initializing all targets at program startup.
521 /// @param T - The target being registered.
522 /// @param Fn - A function to construct an MCStreamer for the target.
523 static void RegisterObjectStreamer(Target &T, Target::ObjectStreamerCtorTy Fn) {
524 if (!T.ObjectStreamerCtorFn)
525 T.ObjectStreamerCtorFn = Fn;
532 //===--------------------------------------------------------------------===//
534 /// RegisterTarget - Helper template for registering a target, for use in the
535 /// target's initialization function. Usage:
538 /// Target TheFooTarget; // The global target instance.
540 /// extern "C" void LLVMInitializeFooTargetInfo() {
541 /// RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
543 template<Triple::ArchType TargetArchType = Triple::InvalidArch,
545 struct RegisterTarget {
546 RegisterTarget(Target &T, const char *Name, const char *Desc) {
547 TargetRegistry::RegisterTarget(T, Name, Desc,
548 &getTripleMatchQuality,
552 static unsigned getTripleMatchQuality(const std::string &TT) {
553 if (Triple(TT).getArch() == TargetArchType)
559 /// RegisterAsmInfo - Helper template for registering a target assembly info
560 /// implementation. This invokes the static "Create" method on the class to
561 /// actually do the construction. Usage:
563 /// extern "C" void LLVMInitializeFooTarget() {
564 /// extern Target TheFooTarget;
565 /// RegisterAsmInfo<FooMCAsmInfo> X(TheFooTarget);
567 template<class MCAsmInfoImpl>
568 struct RegisterAsmInfo {
569 RegisterAsmInfo(Target &T) {
570 TargetRegistry::RegisterAsmInfo(T, &Allocator);
573 static MCAsmInfo *Allocator(const Target &T, StringRef TT) {
574 return new MCAsmInfoImpl(T, TT);
579 /// RegisterAsmInfoFn - Helper template for registering a target assembly info
580 /// implementation. This invokes the specified function to do the
581 /// construction. Usage:
583 /// extern "C" void LLVMInitializeFooTarget() {
584 /// extern Target TheFooTarget;
585 /// RegisterAsmInfoFn X(TheFooTarget, TheFunction);
587 struct RegisterAsmInfoFn {
588 RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
589 TargetRegistry::RegisterAsmInfo(T, Fn);
594 /// RegisterTargetMachine - Helper template for registering a target machine
595 /// implementation, for use in the target machine initialization
598 /// extern "C" void LLVMInitializeFooTarget() {
599 /// extern Target TheFooTarget;
600 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
602 template<class TargetMachineImpl>
603 struct RegisterTargetMachine {
604 RegisterTargetMachine(Target &T) {
605 TargetRegistry::RegisterTargetMachine(T, &Allocator);
609 static TargetMachine *Allocator(const Target &T, const std::string &TT,
610 const std::string &FS) {
611 return new TargetMachineImpl(T, TT, FS);
615 /// RegisterAsmBackend - Helper template for registering a target specific
616 /// assembler backend. Usage:
618 /// extern "C" void LLVMInitializeFooAsmBackend() {
619 /// extern Target TheFooTarget;
620 /// RegisterAsmBackend<FooAsmLexer> X(TheFooTarget);
622 template<class AsmBackendImpl>
623 struct RegisterAsmBackend {
624 RegisterAsmBackend(Target &T) {
625 TargetRegistry::RegisterAsmBackend(T, &Allocator);
629 static TargetAsmBackend *Allocator(const Target &T,
630 const std::string &Triple) {
631 return new AsmBackendImpl(T, Triple);
635 /// RegisterAsmLexer - Helper template for registering a target specific
636 /// assembly lexer, for use in the target machine initialization
639 /// extern "C" void LLVMInitializeFooAsmLexer() {
640 /// extern Target TheFooTarget;
641 /// RegisterAsmLexer<FooAsmLexer> X(TheFooTarget);
643 template<class AsmLexerImpl>
644 struct RegisterAsmLexer {
645 RegisterAsmLexer(Target &T) {
646 TargetRegistry::RegisterAsmLexer(T, &Allocator);
650 static TargetAsmLexer *Allocator(const Target &T, const MCAsmInfo &MAI) {
651 return new AsmLexerImpl(T, MAI);
655 /// RegisterAsmParser - Helper template for registering a target specific
656 /// assembly parser, for use in the target machine initialization
659 /// extern "C" void LLVMInitializeFooAsmParser() {
660 /// extern Target TheFooTarget;
661 /// RegisterAsmParser<FooAsmParser> X(TheFooTarget);
663 template<class AsmParserImpl>
664 struct RegisterAsmParser {
665 RegisterAsmParser(Target &T) {
666 TargetRegistry::RegisterAsmParser(T, &Allocator);
670 static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
671 return new AsmParserImpl(T, P);
675 /// RegisterAsmPrinter - Helper template for registering a target specific
676 /// assembly printer, for use in the target machine initialization
679 /// extern "C" void LLVMInitializeFooAsmPrinter() {
680 /// extern Target TheFooTarget;
681 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
683 template<class AsmPrinterImpl>
684 struct RegisterAsmPrinter {
685 RegisterAsmPrinter(Target &T) {
686 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
690 static AsmPrinter *Allocator(TargetMachine &TM, MCStreamer &Streamer) {
691 return new AsmPrinterImpl(TM, Streamer);
695 /// RegisterCodeEmitter - Helper template for registering a target specific
696 /// machine code emitter, for use in the target initialization
699 /// extern "C" void LLVMInitializeFooCodeEmitter() {
700 /// extern Target TheFooTarget;
701 /// RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
703 template<class CodeEmitterImpl>
704 struct RegisterCodeEmitter {
705 RegisterCodeEmitter(Target &T) {
706 TargetRegistry::RegisterCodeEmitter(T, &Allocator);
710 static MCCodeEmitter *Allocator(const Target &T, TargetMachine &TM,
712 return new CodeEmitterImpl(T, TM, Ctx);