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;
41 class formatted_raw_ostream;
44 /// Target - Wrapper for Target specific information.
46 /// For registration purposes, this is a POD type so that targets can be
47 /// registered without the use of static constructors.
49 /// Targets should implement a single global instance of this class (which
50 /// will be zero initialized), and pass that instance to the TargetRegistry as
51 /// part of their initialization.
54 friend struct TargetRegistry;
56 typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
58 typedef MCAsmInfo *(*AsmInfoCtorFnTy)(const Target &T,
60 typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
61 const std::string &TT,
62 const std::string &Features);
63 typedef AsmPrinter *(*AsmPrinterCtorTy)(formatted_raw_ostream &OS,
65 MCStreamer &Streamer);
66 typedef TargetAsmBackend *(*AsmBackendCtorTy)(const Target &T,
67 const std::string &TT);
68 typedef TargetAsmLexer *(*AsmLexerCtorTy)(const Target &T,
69 const MCAsmInfo &MAI);
70 typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &T,MCAsmParser &P);
71 typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T);
72 typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
73 unsigned SyntaxVariant,
76 typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const Target &T,
81 /// Next - The next registered target in the linked list, maintained by the
85 /// TripleMatchQualityFn - The target function for rating the match quality
87 TripleMatchQualityFnTy TripleMatchQualityFn;
89 /// Name - The target name.
92 /// ShortDesc - A short description of the target.
93 const char *ShortDesc;
95 /// HasJIT - Whether this target supports the JIT.
98 AsmInfoCtorFnTy AsmInfoCtorFn;
100 /// TargetMachineCtorFn - Construction function for this target's
101 /// TargetMachine, if registered.
102 TargetMachineCtorTy TargetMachineCtorFn;
104 /// AsmBackendCtorFn - Construction function for this target's
105 /// TargetAsmBackend, if registered.
106 AsmBackendCtorTy AsmBackendCtorFn;
108 /// AsmLexerCtorFn - Construction function for this target's TargetAsmLexer,
110 AsmLexerCtorTy AsmLexerCtorFn;
112 /// AsmParserCtorFn - Construction function for this target's
113 /// TargetAsmParser, if registered.
114 AsmParserCtorTy AsmParserCtorFn;
116 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
118 AsmPrinterCtorTy AsmPrinterCtorFn;
120 /// MCDisassemblerCtorFn - Construction function for this target's
121 /// MCDisassembler, if registered.
122 MCDisassemblerCtorTy MCDisassemblerCtorFn;
124 /// MCInstPrinterCtorFn - Construction function for this target's
125 /// MCInstPrinter, if registered.
126 MCInstPrinterCtorTy MCInstPrinterCtorFn;
128 /// CodeEmitterCtorFn - Construction function for this target's CodeEmitter,
130 CodeEmitterCtorTy CodeEmitterCtorFn;
133 /// @name Target Information
136 // getNext - Return the next registered target.
137 const Target *getNext() const { return Next; }
139 /// getName - Get the target name.
140 const char *getName() const { return Name; }
142 /// getShortDescription - Get a short description of the target.
143 const char *getShortDescription() const { return ShortDesc; }
146 /// @name Feature Predicates
149 /// hasJIT - Check if this targets supports the just-in-time compilation.
150 bool hasJIT() const { return HasJIT; }
152 /// hasTargetMachine - Check if this target supports code generation.
153 bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
155 /// hasAsmBackend - Check if this target supports .o generation.
156 bool hasAsmBackend() const { return AsmBackendCtorFn != 0; }
158 /// hasAsmLexer - Check if this target supports .s lexing.
159 bool hasAsmLexer() const { return AsmLexerCtorFn != 0; }
161 /// hasAsmParser - Check if this target supports .s parsing.
162 bool hasAsmParser() const { return AsmParserCtorFn != 0; }
164 /// hasAsmPrinter - Check if this target supports .s printing.
165 bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
167 /// hasMCDisassembler - Check if this target has a disassembler.
168 bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
170 /// hasMCInstPrinter - Check if this target has an instruction printer.
171 bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
173 /// hasCodeEmitter - Check if this target supports instruction encoding.
174 bool hasCodeEmitter() const { return CodeEmitterCtorFn != 0; }
177 /// @name Feature Constructors
180 /// createAsmInfo - Create a MCAsmInfo implementation for the specified
183 /// \arg Triple - This argument is used to determine the target machine
184 /// feature set; it should always be provided. Generally this should be
185 /// either the target triple from the module, or the target triple of the
186 /// host if that does not exist.
187 MCAsmInfo *createAsmInfo(StringRef Triple) const {
190 return AsmInfoCtorFn(*this, Triple);
193 /// createTargetMachine - Create a target specific machine implementation
194 /// for the specified \arg Triple.
196 /// \arg Triple - This argument is used to determine the target machine
197 /// feature set; it should always be provided. Generally this should be
198 /// either the target triple from the module, or the target triple of the
199 /// host if that does not exist.
200 TargetMachine *createTargetMachine(const std::string &Triple,
201 const std::string &Features) const {
202 if (!TargetMachineCtorFn)
204 return TargetMachineCtorFn(*this, Triple, Features);
207 /// createAsmBackend - Create a target specific assembly parser.
209 /// \arg Triple - The target triple string.
210 /// \arg Backend - The target independent assembler object.
211 TargetAsmBackend *createAsmBackend(const std::string &Triple) const {
212 if (!AsmBackendCtorFn)
214 return AsmBackendCtorFn(*this, Triple);
217 /// createAsmLexer - Create a target specific assembly lexer.
219 TargetAsmLexer *createAsmLexer(const MCAsmInfo &MAI) const {
222 return AsmLexerCtorFn(*this, MAI);
225 /// createAsmParser - Create a target specific assembly parser.
227 /// \arg Parser - The target independent parser implementation to use for
228 /// parsing and lexing.
229 TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
230 if (!AsmParserCtorFn)
232 return AsmParserCtorFn(*this, Parser);
235 /// createAsmPrinter - Create a target specific assembly printer pass. This
236 /// takes ownership of the MCStreamer object.
237 AsmPrinter *createAsmPrinter(formatted_raw_ostream &OS, TargetMachine &TM,
238 MCStreamer &Streamer) const {
239 if (!AsmPrinterCtorFn)
241 return AsmPrinterCtorFn(OS, TM, Streamer);
244 MCDisassembler *createMCDisassembler() const {
245 if (!MCDisassemblerCtorFn)
247 return MCDisassemblerCtorFn(*this);
250 MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
251 const MCAsmInfo &MAI,
252 raw_ostream &O) const {
253 if (!MCInstPrinterCtorFn)
255 return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI, O);
259 /// createCodeEmitter - Create a target specific code emitter.
260 MCCodeEmitter *createCodeEmitter(TargetMachine &TM, MCContext &Ctx) const {
261 if (!CodeEmitterCtorFn)
263 return CodeEmitterCtorFn(*this, TM, Ctx);
269 /// TargetRegistry - Generic interface to target specific features.
270 struct TargetRegistry {
272 const Target *Current;
273 explicit iterator(Target *T) : Current(T) {}
274 friend struct TargetRegistry;
276 iterator(const iterator &I) : Current(I.Current) {}
277 iterator() : Current(0) {}
279 bool operator==(const iterator &x) const {
280 return Current == x.Current;
282 bool operator!=(const iterator &x) const {
283 return !operator==(x);
286 // Iterator traversal: forward iteration only
287 iterator &operator++() { // Preincrement
288 assert(Current && "Cannot increment end iterator!");
289 Current = Current->getNext();
292 iterator operator++(int) { // Postincrement
293 iterator tmp = *this;
298 const Target &operator*() const {
299 assert(Current && "Cannot dereference end iterator!");
303 const Target *operator->() const {
308 /// @name Registry Access
311 static iterator begin();
313 static iterator end() { return iterator(); }
315 /// lookupTarget - Lookup a target based on a target triple.
317 /// \param Triple - The triple to use for finding a target.
318 /// \param Error - On failure, an error string describing why no target was
320 static const Target *lookupTarget(const std::string &Triple,
323 /// getClosestTargetForJIT - Pick the best target that is compatible with
324 /// the current host. If no close target can be found, this returns null
325 /// and sets the Error string to a reason.
327 /// Maintained for compatibility through 2.6.
328 static const Target *getClosestTargetForJIT(std::string &Error);
331 /// @name Target Registration
334 /// RegisterTarget - Register the given target. Attempts to register a
335 /// target which has already been registered will be ignored.
337 /// Clients are responsible for ensuring that registration doesn't occur
338 /// while another thread is attempting to access the registry. Typically
339 /// this is done by initializing all targets at program startup.
341 /// @param T - The target being registered.
342 /// @param Name - The target name. This should be a static string.
343 /// @param ShortDesc - A short target description. This should be a static
345 /// @param TQualityFn - The triple match quality computation function for
347 /// @param HasJIT - Whether the target supports JIT code
349 static void RegisterTarget(Target &T,
351 const char *ShortDesc,
352 Target::TripleMatchQualityFnTy TQualityFn,
353 bool HasJIT = false);
355 /// RegisterAsmInfo - Register a MCAsmInfo implementation for the
358 /// Clients are responsible for ensuring that registration doesn't occur
359 /// while another thread is attempting to access the registry. Typically
360 /// this is done by initializing all targets at program startup.
362 /// @param T - The target being registered.
363 /// @param Fn - A function to construct a MCAsmInfo for the target.
364 static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
365 // Ignore duplicate registration.
366 if (!T.AsmInfoCtorFn)
367 T.AsmInfoCtorFn = Fn;
370 /// RegisterTargetMachine - Register a TargetMachine implementation for the
373 /// Clients are responsible for ensuring that registration doesn't occur
374 /// while another thread is attempting to access the registry. Typically
375 /// this is done by initializing all targets at program startup.
377 /// @param T - The target being registered.
378 /// @param Fn - A function to construct a TargetMachine for the target.
379 static void RegisterTargetMachine(Target &T,
380 Target::TargetMachineCtorTy Fn) {
381 // Ignore duplicate registration.
382 if (!T.TargetMachineCtorFn)
383 T.TargetMachineCtorFn = Fn;
386 /// RegisterAsmBackend - Register a TargetAsmBackend implementation for the
389 /// Clients are responsible for ensuring that registration doesn't occur
390 /// while another thread is attempting to access the registry. Typically
391 /// this is done by initializing all targets at program startup.
393 /// @param T - The target being registered.
394 /// @param Fn - A function to construct an AsmBackend for the target.
395 static void RegisterAsmBackend(Target &T, Target::AsmBackendCtorTy Fn) {
396 if (!T.AsmBackendCtorFn)
397 T.AsmBackendCtorFn = Fn;
400 /// RegisterAsmLexer - Register a TargetAsmLexer implementation for the
403 /// Clients are responsible for ensuring that registration doesn't occur
404 /// while another thread is attempting to access the registry. Typically
405 /// this is done by initializing all targets at program startup.
407 /// @param T - The target being registered.
408 /// @param Fn - A function to construct an AsmLexer for the target.
409 static void RegisterAsmLexer(Target &T, Target::AsmLexerCtorTy Fn) {
410 if (!T.AsmLexerCtorFn)
411 T.AsmLexerCtorFn = Fn;
414 /// RegisterAsmParser - Register a TargetAsmParser implementation for the
417 /// Clients are responsible for ensuring that registration doesn't occur
418 /// while another thread is attempting to access the registry. Typically
419 /// this is done by initializing all targets at program startup.
421 /// @param T - The target being registered.
422 /// @param Fn - A function to construct an AsmParser for the target.
423 static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
424 if (!T.AsmParserCtorFn)
425 T.AsmParserCtorFn = Fn;
428 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
431 /// Clients are responsible for ensuring that registration doesn't occur
432 /// while another thread is attempting to access the registry. Typically
433 /// this is done by initializing all targets at program startup.
435 /// @param T - The target being registered.
436 /// @param Fn - A function to construct an AsmPrinter for the target.
437 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
438 // Ignore duplicate registration.
439 if (!T.AsmPrinterCtorFn)
440 T.AsmPrinterCtorFn = Fn;
443 /// RegisterMCDisassembler - Register a MCDisassembler implementation for
444 /// the given target.
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 MCDisassembler for the target.
452 static void RegisterMCDisassembler(Target &T,
453 Target::MCDisassemblerCtorTy Fn) {
454 if (!T.MCDisassemblerCtorFn)
455 T.MCDisassemblerCtorFn = Fn;
458 /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
461 /// Clients are responsible for ensuring that registration doesn't occur
462 /// while another thread is attempting to access the registry. Typically
463 /// this is done by initializing all targets at program startup.
465 /// @param T - The target being registered.
466 /// @param Fn - A function to construct an MCInstPrinter for the target.
467 static void RegisterMCInstPrinter(Target &T,
468 Target::MCInstPrinterCtorTy Fn) {
469 if (!T.MCInstPrinterCtorFn)
470 T.MCInstPrinterCtorFn = Fn;
473 /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
476 /// Clients are responsible for ensuring that registration doesn't occur
477 /// while another thread is attempting to access the registry. Typically
478 /// this is done by initializing all targets at program startup.
480 /// @param T - The target being registered.
481 /// @param Fn - A function to construct an MCCodeEmitter for the target.
482 static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
483 if (!T.CodeEmitterCtorFn)
484 T.CodeEmitterCtorFn = Fn;
491 //===--------------------------------------------------------------------===//
493 /// RegisterTarget - Helper template for registering a target, for use in the
494 /// target's initialization function. Usage:
497 /// Target TheFooTarget; // The global target instance.
499 /// extern "C" void LLVMInitializeFooTargetInfo() {
500 /// RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
502 template<Triple::ArchType TargetArchType = Triple::InvalidArch,
504 struct RegisterTarget {
505 RegisterTarget(Target &T, const char *Name, const char *Desc) {
506 TargetRegistry::RegisterTarget(T, Name, Desc,
507 &getTripleMatchQuality,
511 static unsigned getTripleMatchQuality(const std::string &TT) {
512 if (Triple(TT).getArch() == TargetArchType)
518 /// RegisterAsmInfo - Helper template for registering a target assembly info
519 /// implementation. This invokes the static "Create" method on the class to
520 /// actually do the construction. Usage:
522 /// extern "C" void LLVMInitializeFooTarget() {
523 /// extern Target TheFooTarget;
524 /// RegisterAsmInfo<FooMCAsmInfo> X(TheFooTarget);
526 template<class MCAsmInfoImpl>
527 struct RegisterAsmInfo {
528 RegisterAsmInfo(Target &T) {
529 TargetRegistry::RegisterAsmInfo(T, &Allocator);
532 static MCAsmInfo *Allocator(const Target &T, StringRef TT) {
533 return new MCAsmInfoImpl(T, TT);
538 /// RegisterAsmInfoFn - Helper template for registering a target assembly info
539 /// implementation. This invokes the specified function to do the
540 /// construction. Usage:
542 /// extern "C" void LLVMInitializeFooTarget() {
543 /// extern Target TheFooTarget;
544 /// RegisterAsmInfoFn X(TheFooTarget, TheFunction);
546 struct RegisterAsmInfoFn {
547 RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
548 TargetRegistry::RegisterAsmInfo(T, Fn);
553 /// RegisterTargetMachine - Helper template for registering a target machine
554 /// implementation, for use in the target machine initialization
557 /// extern "C" void LLVMInitializeFooTarget() {
558 /// extern Target TheFooTarget;
559 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
561 template<class TargetMachineImpl>
562 struct RegisterTargetMachine {
563 RegisterTargetMachine(Target &T) {
564 TargetRegistry::RegisterTargetMachine(T, &Allocator);
568 static TargetMachine *Allocator(const Target &T, const std::string &TT,
569 const std::string &FS) {
570 return new TargetMachineImpl(T, TT, FS);
574 /// RegisterAsmBackend - Helper template for registering a target specific
575 /// assembler backend. Usage:
577 /// extern "C" void LLVMInitializeFooAsmBackend() {
578 /// extern Target TheFooTarget;
579 /// RegisterAsmBackend<FooAsmLexer> X(TheFooTarget);
581 template<class AsmBackendImpl>
582 struct RegisterAsmBackend {
583 RegisterAsmBackend(Target &T) {
584 TargetRegistry::RegisterAsmBackend(T, &Allocator);
588 static TargetAsmBackend *Allocator(const Target &T,
589 const std::string &Triple) {
590 return new AsmBackendImpl(T, Triple);
594 /// RegisterAsmLexer - Helper template for registering a target specific
595 /// assembly lexer, for use in the target machine initialization
598 /// extern "C" void LLVMInitializeFooAsmLexer() {
599 /// extern Target TheFooTarget;
600 /// RegisterAsmLexer<FooAsmLexer> X(TheFooTarget);
602 template<class AsmLexerImpl>
603 struct RegisterAsmLexer {
604 RegisterAsmLexer(Target &T) {
605 TargetRegistry::RegisterAsmLexer(T, &Allocator);
609 static TargetAsmLexer *Allocator(const Target &T, const MCAsmInfo &MAI) {
610 return new AsmLexerImpl(T, MAI);
614 /// RegisterAsmParser - Helper template for registering a target specific
615 /// assembly parser, for use in the target machine initialization
618 /// extern "C" void LLVMInitializeFooAsmParser() {
619 /// extern Target TheFooTarget;
620 /// RegisterAsmParser<FooAsmParser> X(TheFooTarget);
622 template<class AsmParserImpl>
623 struct RegisterAsmParser {
624 RegisterAsmParser(Target &T) {
625 TargetRegistry::RegisterAsmParser(T, &Allocator);
629 static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
630 return new AsmParserImpl(T, P);
634 /// RegisterAsmPrinter - Helper template for registering a target specific
635 /// assembly printer, for use in the target machine initialization
638 /// extern "C" void LLVMInitializeFooAsmPrinter() {
639 /// extern Target TheFooTarget;
640 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
642 template<class AsmPrinterImpl>
643 struct RegisterAsmPrinter {
644 RegisterAsmPrinter(Target &T) {
645 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
649 static AsmPrinter *Allocator(formatted_raw_ostream &OS, TargetMachine &TM,
650 MCStreamer &Streamer) {
651 return new AsmPrinterImpl(OS, TM, Streamer);
655 /// RegisterCodeEmitter - Helper template for registering a target specific
656 /// machine code emitter, for use in the target initialization
659 /// extern "C" void LLVMInitializeFooCodeEmitter() {
660 /// extern Target TheFooTarget;
661 /// RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
663 template<class CodeEmitterImpl>
664 struct RegisterCodeEmitter {
665 RegisterCodeEmitter(Target &T) {
666 TargetRegistry::RegisterCodeEmitter(T, &Allocator);
670 static MCCodeEmitter *Allocator(const Target &T, TargetMachine &TM,
672 return new CodeEmitterImpl(T, TM, Ctx);