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,
74 const MCAsmInfo &MAI);
75 typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const Target &T,
80 /// Next - The next registered target in the linked list, maintained by the
84 /// TripleMatchQualityFn - The target function for rating the match quality
86 TripleMatchQualityFnTy TripleMatchQualityFn;
88 /// Name - The target name.
91 /// ShortDesc - A short description of the target.
92 const char *ShortDesc;
94 /// HasJIT - Whether this target supports the JIT.
97 AsmInfoCtorFnTy AsmInfoCtorFn;
99 /// TargetMachineCtorFn - Construction function for this target's
100 /// TargetMachine, if registered.
101 TargetMachineCtorTy TargetMachineCtorFn;
103 /// AsmBackendCtorFn - Construction function for this target's
104 /// TargetAsmBackend, if registered.
105 AsmBackendCtorTy AsmBackendCtorFn;
107 /// AsmLexerCtorFn - Construction function for this target's TargetAsmLexer,
109 AsmLexerCtorTy AsmLexerCtorFn;
111 /// AsmParserCtorFn - Construction function for this target's
112 /// TargetAsmParser, if registered.
113 AsmParserCtorTy AsmParserCtorFn;
115 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
117 AsmPrinterCtorTy AsmPrinterCtorFn;
119 /// MCDisassemblerCtorFn - Construction function for this target's
120 /// MCDisassembler, if registered.
121 MCDisassemblerCtorTy MCDisassemblerCtorFn;
123 /// MCInstPrinterCtorFn - Construction function for this target's
124 /// MCInstPrinter, if registered.
125 MCInstPrinterCtorTy MCInstPrinterCtorFn;
127 /// CodeEmitterCtorFn - Construction function for this target's CodeEmitter,
129 CodeEmitterCtorTy CodeEmitterCtorFn;
132 /// @name Target Information
135 // getNext - Return the next registered target.
136 const Target *getNext() const { return Next; }
138 /// getName - Get the target name.
139 const char *getName() const { return Name; }
141 /// getShortDescription - Get a short description of the target.
142 const char *getShortDescription() const { return ShortDesc; }
145 /// @name Feature Predicates
148 /// hasJIT - Check if this targets supports the just-in-time compilation.
149 bool hasJIT() const { return HasJIT; }
151 /// hasTargetMachine - Check if this target supports code generation.
152 bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
154 /// hasAsmBackend - Check if this target supports .o generation.
155 bool hasAsmBackend() const { return AsmBackendCtorFn != 0; }
157 /// hasAsmLexer - Check if this target supports .s lexing.
158 bool hasAsmLexer() const { return AsmLexerCtorFn != 0; }
160 /// hasAsmParser - Check if this target supports .s parsing.
161 bool hasAsmParser() const { return AsmParserCtorFn != 0; }
163 /// hasAsmPrinter - Check if this target supports .s printing.
164 bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
166 /// hasMCDisassembler - Check if this target has a disassembler.
167 bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
169 /// hasMCInstPrinter - Check if this target has an instruction printer.
170 bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
172 /// hasCodeEmitter - Check if this target supports instruction encoding.
173 bool hasCodeEmitter() const { return CodeEmitterCtorFn != 0; }
176 /// @name Feature Constructors
179 /// createAsmInfo - Create a MCAsmInfo implementation for the specified
182 /// \arg Triple - This argument is used to determine the target machine
183 /// feature set; it should always be provided. Generally this should be
184 /// either the target triple from the module, or the target triple of the
185 /// host if that does not exist.
186 MCAsmInfo *createAsmInfo(StringRef Triple) const {
189 return AsmInfoCtorFn(*this, Triple);
192 /// createTargetMachine - Create a target specific machine implementation
193 /// for the specified \arg Triple.
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 TargetMachine *createTargetMachine(const std::string &Triple,
200 const std::string &Features) const {
201 if (!TargetMachineCtorFn)
203 return TargetMachineCtorFn(*this, Triple, Features);
206 /// createAsmBackend - Create a target specific assembly parser.
208 /// \arg Triple - The target triple string.
209 /// \arg Backend - The target independent assembler object.
210 TargetAsmBackend *createAsmBackend(const std::string &Triple) const {
211 if (!AsmBackendCtorFn)
213 return AsmBackendCtorFn(*this, Triple);
216 /// createAsmLexer - Create a target specific assembly lexer.
218 TargetAsmLexer *createAsmLexer(const MCAsmInfo &MAI) const {
221 return AsmLexerCtorFn(*this, MAI);
224 /// createAsmParser - Create a target specific assembly parser.
226 /// \arg Parser - The target independent parser implementation to use for
227 /// parsing and lexing.
228 TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
229 if (!AsmParserCtorFn)
231 return AsmParserCtorFn(*this, Parser);
234 /// createAsmPrinter - Create a target specific assembly printer pass. This
235 /// takes ownership of the MCStreamer object.
236 AsmPrinter *createAsmPrinter(formatted_raw_ostream &OS, TargetMachine &TM,
237 MCStreamer &Streamer) const {
238 if (!AsmPrinterCtorFn)
240 return AsmPrinterCtorFn(OS, TM, Streamer);
243 MCDisassembler *createMCDisassembler() const {
244 if (!MCDisassemblerCtorFn)
246 return MCDisassemblerCtorFn(*this);
249 MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
250 const MCAsmInfo &MAI) const {
251 if (!MCInstPrinterCtorFn)
253 return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI);
257 /// createCodeEmitter - Create a target specific code emitter.
258 MCCodeEmitter *createCodeEmitter(TargetMachine &TM, MCContext &Ctx) const {
259 if (!CodeEmitterCtorFn)
261 return CodeEmitterCtorFn(*this, TM, Ctx);
267 /// TargetRegistry - Generic interface to target specific features.
268 struct TargetRegistry {
270 const Target *Current;
271 explicit iterator(Target *T) : Current(T) {}
272 friend struct TargetRegistry;
274 iterator(const iterator &I) : Current(I.Current) {}
275 iterator() : Current(0) {}
277 bool operator==(const iterator &x) const {
278 return Current == x.Current;
280 bool operator!=(const iterator &x) const {
281 return !operator==(x);
284 // Iterator traversal: forward iteration only
285 iterator &operator++() { // Preincrement
286 assert(Current && "Cannot increment end iterator!");
287 Current = Current->getNext();
290 iterator operator++(int) { // Postincrement
291 iterator tmp = *this;
296 const Target &operator*() const {
297 assert(Current && "Cannot dereference end iterator!");
301 const Target *operator->() const {
306 /// @name Registry Access
309 static iterator begin();
311 static iterator end() { return iterator(); }
313 /// lookupTarget - Lookup a target based on a target triple.
315 /// \param Triple - The triple to use for finding a target.
316 /// \param Error - On failure, an error string describing why no target was
318 static const Target *lookupTarget(const std::string &Triple,
321 /// getClosestTargetForJIT - Pick the best target that is compatible with
322 /// the current host. If no close target can be found, this returns null
323 /// and sets the Error string to a reason.
325 /// Maintained for compatibility through 2.6.
326 static const Target *getClosestTargetForJIT(std::string &Error);
329 /// @name Target Registration
332 /// RegisterTarget - Register the given target. Attempts to register a
333 /// target which has already been registered will be ignored.
335 /// Clients are responsible for ensuring that registration doesn't occur
336 /// while another thread is attempting to access the registry. Typically
337 /// this is done by initializing all targets at program startup.
339 /// @param T - The target being registered.
340 /// @param Name - The target name. This should be a static string.
341 /// @param ShortDesc - A short target description. This should be a static
343 /// @param TQualityFn - The triple match quality computation function for
345 /// @param HasJIT - Whether the target supports JIT code
347 static void RegisterTarget(Target &T,
349 const char *ShortDesc,
350 Target::TripleMatchQualityFnTy TQualityFn,
351 bool HasJIT = false);
353 /// RegisterAsmInfo - Register a MCAsmInfo implementation for the
356 /// Clients are responsible for ensuring that registration doesn't occur
357 /// while another thread is attempting to access the registry. Typically
358 /// this is done by initializing all targets at program startup.
360 /// @param T - The target being registered.
361 /// @param Fn - A function to construct a MCAsmInfo for the target.
362 static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
363 // Ignore duplicate registration.
364 if (!T.AsmInfoCtorFn)
365 T.AsmInfoCtorFn = Fn;
368 /// RegisterTargetMachine - Register a TargetMachine implementation for the
371 /// Clients are responsible for ensuring that registration doesn't occur
372 /// while another thread is attempting to access the registry. Typically
373 /// this is done by initializing all targets at program startup.
375 /// @param T - The target being registered.
376 /// @param Fn - A function to construct a TargetMachine for the target.
377 static void RegisterTargetMachine(Target &T,
378 Target::TargetMachineCtorTy Fn) {
379 // Ignore duplicate registration.
380 if (!T.TargetMachineCtorFn)
381 T.TargetMachineCtorFn = Fn;
384 /// RegisterAsmBackend - Register a TargetAsmBackend 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 an AsmBackend for the target.
393 static void RegisterAsmBackend(Target &T, Target::AsmBackendCtorTy Fn) {
394 if (!T.AsmBackendCtorFn)
395 T.AsmBackendCtorFn = Fn;
398 /// RegisterAsmLexer - Register a TargetAsmLexer implementation for the
401 /// Clients are responsible for ensuring that registration doesn't occur
402 /// while another thread is attempting to access the registry. Typically
403 /// this is done by initializing all targets at program startup.
405 /// @param T - The target being registered.
406 /// @param Fn - A function to construct an AsmLexer for the target.
407 static void RegisterAsmLexer(Target &T, Target::AsmLexerCtorTy Fn) {
408 if (!T.AsmLexerCtorFn)
409 T.AsmLexerCtorFn = Fn;
412 /// RegisterAsmParser - Register a TargetAsmParser implementation for the
415 /// Clients are responsible for ensuring that registration doesn't occur
416 /// while another thread is attempting to access the registry. Typically
417 /// this is done by initializing all targets at program startup.
419 /// @param T - The target being registered.
420 /// @param Fn - A function to construct an AsmParser for the target.
421 static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
422 if (!T.AsmParserCtorFn)
423 T.AsmParserCtorFn = Fn;
426 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
429 /// Clients are responsible for ensuring that registration doesn't occur
430 /// while another thread is attempting to access the registry. Typically
431 /// this is done by initializing all targets at program startup.
433 /// @param T - The target being registered.
434 /// @param Fn - A function to construct an AsmPrinter for the target.
435 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
436 // Ignore duplicate registration.
437 if (!T.AsmPrinterCtorFn)
438 T.AsmPrinterCtorFn = Fn;
441 /// RegisterMCDisassembler - Register a MCDisassembler implementation for
442 /// the given target.
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 MCDisassembler for the target.
450 static void RegisterMCDisassembler(Target &T,
451 Target::MCDisassemblerCtorTy Fn) {
452 if (!T.MCDisassemblerCtorFn)
453 T.MCDisassemblerCtorFn = Fn;
456 /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
459 /// Clients are responsible for ensuring that registration doesn't occur
460 /// while another thread is attempting to access the registry. Typically
461 /// this is done by initializing all targets at program startup.
463 /// @param T - The target being registered.
464 /// @param Fn - A function to construct an MCInstPrinter for the target.
465 static void RegisterMCInstPrinter(Target &T,
466 Target::MCInstPrinterCtorTy Fn) {
467 if (!T.MCInstPrinterCtorFn)
468 T.MCInstPrinterCtorFn = Fn;
471 /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
474 /// Clients are responsible for ensuring that registration doesn't occur
475 /// while another thread is attempting to access the registry. Typically
476 /// this is done by initializing all targets at program startup.
478 /// @param T - The target being registered.
479 /// @param Fn - A function to construct an MCCodeEmitter for the target.
480 static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
481 if (!T.CodeEmitterCtorFn)
482 T.CodeEmitterCtorFn = Fn;
489 //===--------------------------------------------------------------------===//
491 /// RegisterTarget - Helper template for registering a target, for use in the
492 /// target's initialization function. Usage:
495 /// Target TheFooTarget; // The global target instance.
497 /// extern "C" void LLVMInitializeFooTargetInfo() {
498 /// RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
500 template<Triple::ArchType TargetArchType = Triple::InvalidArch,
502 struct RegisterTarget {
503 RegisterTarget(Target &T, const char *Name, const char *Desc) {
504 TargetRegistry::RegisterTarget(T, Name, Desc,
505 &getTripleMatchQuality,
509 static unsigned getTripleMatchQuality(const std::string &TT) {
510 if (Triple(TT).getArch() == TargetArchType)
516 /// RegisterAsmInfo - Helper template for registering a target assembly info
517 /// implementation. This invokes the static "Create" method on the class to
518 /// actually do the construction. Usage:
520 /// extern "C" void LLVMInitializeFooTarget() {
521 /// extern Target TheFooTarget;
522 /// RegisterAsmInfo<FooMCAsmInfo> X(TheFooTarget);
524 template<class MCAsmInfoImpl>
525 struct RegisterAsmInfo {
526 RegisterAsmInfo(Target &T) {
527 TargetRegistry::RegisterAsmInfo(T, &Allocator);
530 static MCAsmInfo *Allocator(const Target &T, StringRef TT) {
531 return new MCAsmInfoImpl(T, TT);
536 /// RegisterAsmInfoFn - Helper template for registering a target assembly info
537 /// implementation. This invokes the specified function to do the
538 /// construction. Usage:
540 /// extern "C" void LLVMInitializeFooTarget() {
541 /// extern Target TheFooTarget;
542 /// RegisterAsmInfoFn X(TheFooTarget, TheFunction);
544 struct RegisterAsmInfoFn {
545 RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
546 TargetRegistry::RegisterAsmInfo(T, Fn);
551 /// RegisterTargetMachine - Helper template for registering a target machine
552 /// implementation, for use in the target machine initialization
555 /// extern "C" void LLVMInitializeFooTarget() {
556 /// extern Target TheFooTarget;
557 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
559 template<class TargetMachineImpl>
560 struct RegisterTargetMachine {
561 RegisterTargetMachine(Target &T) {
562 TargetRegistry::RegisterTargetMachine(T, &Allocator);
566 static TargetMachine *Allocator(const Target &T, const std::string &TT,
567 const std::string &FS) {
568 return new TargetMachineImpl(T, TT, FS);
572 /// RegisterAsmBackend - Helper template for registering a target specific
573 /// assembler backend. Usage:
575 /// extern "C" void LLVMInitializeFooAsmBackend() {
576 /// extern Target TheFooTarget;
577 /// RegisterAsmBackend<FooAsmLexer> X(TheFooTarget);
579 template<class AsmBackendImpl>
580 struct RegisterAsmBackend {
581 RegisterAsmBackend(Target &T) {
582 TargetRegistry::RegisterAsmBackend(T, &Allocator);
586 static TargetAsmBackend *Allocator(const Target &T,
587 const std::string &Triple) {
588 return new AsmBackendImpl(T, Triple);
592 /// RegisterAsmLexer - Helper template for registering a target specific
593 /// assembly lexer, for use in the target machine initialization
596 /// extern "C" void LLVMInitializeFooAsmLexer() {
597 /// extern Target TheFooTarget;
598 /// RegisterAsmLexer<FooAsmLexer> X(TheFooTarget);
600 template<class AsmLexerImpl>
601 struct RegisterAsmLexer {
602 RegisterAsmLexer(Target &T) {
603 TargetRegistry::RegisterAsmLexer(T, &Allocator);
607 static TargetAsmLexer *Allocator(const Target &T, const MCAsmInfo &MAI) {
608 return new AsmLexerImpl(T, MAI);
612 /// RegisterAsmParser - Helper template for registering a target specific
613 /// assembly parser, for use in the target machine initialization
616 /// extern "C" void LLVMInitializeFooAsmParser() {
617 /// extern Target TheFooTarget;
618 /// RegisterAsmParser<FooAsmParser> X(TheFooTarget);
620 template<class AsmParserImpl>
621 struct RegisterAsmParser {
622 RegisterAsmParser(Target &T) {
623 TargetRegistry::RegisterAsmParser(T, &Allocator);
627 static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
628 return new AsmParserImpl(T, P);
632 /// RegisterAsmPrinter - Helper template for registering a target specific
633 /// assembly printer, for use in the target machine initialization
636 /// extern "C" void LLVMInitializeFooAsmPrinter() {
637 /// extern Target TheFooTarget;
638 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
640 template<class AsmPrinterImpl>
641 struct RegisterAsmPrinter {
642 RegisterAsmPrinter(Target &T) {
643 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
647 static AsmPrinter *Allocator(formatted_raw_ostream &OS, TargetMachine &TM,
648 MCStreamer &Streamer) {
649 return new AsmPrinterImpl(OS, TM, Streamer);
653 /// RegisterCodeEmitter - Helper template for registering a target specific
654 /// machine code emitter, for use in the target initialization
657 /// extern "C" void LLVMInitializeFooCodeEmitter() {
658 /// extern Target TheFooTarget;
659 /// RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
661 template<class CodeEmitterImpl>
662 struct RegisterCodeEmitter {
663 RegisterCodeEmitter(Target &T) {
664 TargetRegistry::RegisterCodeEmitter(T, &Allocator);
668 static MCCodeEmitter *Allocator(const Target &T, TargetMachine &TM,
670 return new CodeEmitterImpl(T, TM, Ctx);