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 TargetAsmParser;
39 class formatted_raw_ostream;
42 /// Target - Wrapper for Target specific information.
44 /// For registration purposes, this is a POD type so that targets can be
45 /// registered without the use of static constructors.
47 /// Targets should implement a single global instance of this class (which
48 /// will be zero initialized), and pass that instance to the TargetRegistry as
49 /// part of their initialization.
52 friend struct TargetRegistry;
54 typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
56 typedef const MCAsmInfo *(*AsmInfoCtorFnTy)(const Target &T,
58 typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
59 const std::string &TT,
60 const std::string &Features);
61 typedef AsmPrinter *(*AsmPrinterCtorTy)(formatted_raw_ostream &OS,
65 const MCAsmInfo *MAI);
66 typedef TargetAsmLexer *(*AsmLexerCtorTy)(const Target &T,
67 const MCAsmInfo &MAI);
68 typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &T,MCAsmParser &P);
69 typedef const MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T);
70 typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
71 unsigned SyntaxVariant,
74 typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const Target &T,
79 /// Next - The next registered target in the linked list, maintained by the
83 /// TripleMatchQualityFn - The target function for rating the match quality
85 TripleMatchQualityFnTy TripleMatchQualityFn;
87 /// Name - The target name.
90 /// ShortDesc - A short description of the target.
91 const char *ShortDesc;
93 /// HasJIT - Whether this target supports the JIT.
96 AsmInfoCtorFnTy AsmInfoCtorFn;
98 /// TargetMachineCtorFn - Construction function for this target's
99 /// TargetMachine, if registered.
100 TargetMachineCtorTy TargetMachineCtorFn;
102 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
104 AsmPrinterCtorTy AsmPrinterCtorFn;
106 /// AsmLexerCtorFn - Construction function for this target's TargetAsmLexer,
108 AsmLexerCtorTy AsmLexerCtorFn;
110 /// AsmParserCtorFn - Construction function for this target's
111 /// TargetAsmParser, if registered.
112 AsmParserCtorTy AsmParserCtorFn;
114 /// MCDisassemblerCtorFn - Construction function for this target's
115 /// MCDisassembler, if registered.
116 MCDisassemblerCtorTy MCDisassemblerCtorFn;
119 /// MCInstPrinterCtorFn - Construction function for this target's
120 /// MCInstPrinter, if registered.
121 MCInstPrinterCtorTy MCInstPrinterCtorFn;
123 /// CodeEmitterCtorFn - Construction function for this target's CodeEmitter,
125 CodeEmitterCtorTy CodeEmitterCtorFn;
128 /// @name Target Information
131 // getNext - Return the next registered target.
132 const Target *getNext() const { return Next; }
134 /// getName - Get the target name.
135 const char *getName() const { return Name; }
137 /// getShortDescription - Get a short description of the target.
138 const char *getShortDescription() const { return ShortDesc; }
141 /// @name Feature Predicates
144 /// hasJIT - Check if this targets supports the just-in-time compilation.
145 bool hasJIT() const { return HasJIT; }
147 /// hasTargetMachine - Check if this target supports code generation.
148 bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
150 /// hasAsmPrinter - Check if this target supports .s printing.
151 bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
153 /// hasAsmParser - Check if this target supports .s parsing.
154 bool hasAsmParser() const { return AsmParserCtorFn != 0; }
156 /// hasMCDisassembler - Check if this target has a disassembler.
157 bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
159 /// hasMCInstPrinter - Check if this target has an instruction printer.
160 bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
162 /// hasCodeEmitter - Check if this target supports instruction encoding.
163 bool hasCodeEmitter() const { return CodeEmitterCtorFn != 0; }
166 /// @name Feature Constructors
169 /// createAsmInfo - Create a MCAsmInfo implementation for the specified
172 /// \arg Triple - This argument is used to determine the target machine
173 /// feature set; it should always be provided. Generally this should be
174 /// either the target triple from the module, or the target triple of the
175 /// host if that does not exist.
176 const MCAsmInfo *createAsmInfo(StringRef Triple) const {
179 return AsmInfoCtorFn(*this, Triple);
182 /// createTargetMachine - Create a target specific machine implementation
183 /// for the specified \arg Triple.
185 /// \arg Triple - This argument is used to determine the target machine
186 /// feature set; it should always be provided. Generally this should be
187 /// either the target triple from the module, or the target triple of the
188 /// host if that does not exist.
189 TargetMachine *createTargetMachine(const std::string &Triple,
190 const std::string &Features) const {
191 if (!TargetMachineCtorFn)
193 return TargetMachineCtorFn(*this, Triple, Features);
196 /// createAsmPrinter - Create a target specific assembly printer pass. This
197 /// takes ownership of the MCContext and MCStreamer objects but not the MAI.
198 AsmPrinter *createAsmPrinter(formatted_raw_ostream &OS, TargetMachine &TM,
199 MCContext &Ctx, MCStreamer &Streamer,
200 const MCAsmInfo *MAI) const {
201 if (!AsmPrinterCtorFn)
203 return AsmPrinterCtorFn(OS, TM, Ctx, Streamer, MAI);
206 /// createAsmLexer - Create a target specific assembly lexer.
208 TargetAsmLexer *createAsmLexer(const MCAsmInfo &MAI) const {
211 return AsmLexerCtorFn(*this, MAI);
214 /// createAsmParser - Create a target specific assembly parser.
216 /// \arg Parser - The target independent parser implementation to use for
217 /// parsing and lexing.
218 TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
219 if (!AsmParserCtorFn)
221 return AsmParserCtorFn(*this, Parser);
224 const MCDisassembler *createMCDisassembler() const {
225 if (!MCDisassemblerCtorFn)
227 return MCDisassemblerCtorFn(*this);
230 MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
231 const MCAsmInfo &MAI,
232 raw_ostream &O) const {
233 if (!MCInstPrinterCtorFn)
235 return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI, O);
239 /// createCodeEmitter - Create a target specific code emitter.
240 MCCodeEmitter *createCodeEmitter(TargetMachine &TM, MCContext &Ctx) const {
241 if (!CodeEmitterCtorFn)
243 return CodeEmitterCtorFn(*this, TM, Ctx);
249 /// TargetRegistry - Generic interface to target specific features.
250 struct TargetRegistry {
252 const Target *Current;
253 explicit iterator(Target *T) : Current(T) {}
254 friend struct TargetRegistry;
256 iterator(const iterator &I) : Current(I.Current) {}
257 iterator() : Current(0) {}
259 bool operator==(const iterator &x) const {
260 return Current == x.Current;
262 bool operator!=(const iterator &x) const {
263 return !operator==(x);
266 // Iterator traversal: forward iteration only
267 iterator &operator++() { // Preincrement
268 assert(Current && "Cannot increment end iterator!");
269 Current = Current->getNext();
272 iterator operator++(int) { // Postincrement
273 iterator tmp = *this;
278 const Target &operator*() const {
279 assert(Current && "Cannot dereference end iterator!");
283 const Target *operator->() const {
288 /// @name Registry Access
291 static iterator begin();
293 static iterator end() { return iterator(); }
295 /// lookupTarget - Lookup a target based on a target triple.
297 /// \param Triple - The triple to use for finding a target.
298 /// \param Error - On failure, an error string describing why no target was
300 static const Target *lookupTarget(const std::string &Triple,
303 /// getClosestTargetForJIT - Pick the best target that is compatible with
304 /// the current host. If no close target can be found, this returns null
305 /// and sets the Error string to a reason.
307 /// Maintained for compatibility through 2.6.
308 static const Target *getClosestTargetForJIT(std::string &Error);
311 /// @name Target Registration
314 /// RegisterTarget - Register the given target. Attempts to register a
315 /// target which has already been registered will be ignored.
317 /// Clients are responsible for ensuring that registration doesn't occur
318 /// while another thread is attempting to access the registry. Typically
319 /// this is done by initializing all targets at program startup.
321 /// @param T - The target being registered.
322 /// @param Name - The target name. This should be a static string.
323 /// @param ShortDesc - A short target description. This should be a static
325 /// @param TQualityFn - The triple match quality computation function for
327 /// @param HasJIT - Whether the target supports JIT code
329 static void RegisterTarget(Target &T,
331 const char *ShortDesc,
332 Target::TripleMatchQualityFnTy TQualityFn,
333 bool HasJIT = false);
335 /// RegisterAsmInfo - Register a MCAsmInfo implementation for the
338 /// Clients are responsible for ensuring that registration doesn't occur
339 /// while another thread is attempting to access the registry. Typically
340 /// this is done by initializing all targets at program startup.
342 /// @param T - The target being registered.
343 /// @param Fn - A function to construct a MCAsmInfo for the target.
344 static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
345 // Ignore duplicate registration.
346 if (!T.AsmInfoCtorFn)
347 T.AsmInfoCtorFn = Fn;
350 /// RegisterTargetMachine - Register a TargetMachine 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 TargetMachine for the target.
359 static void RegisterTargetMachine(Target &T,
360 Target::TargetMachineCtorTy Fn) {
361 // Ignore duplicate registration.
362 if (!T.TargetMachineCtorFn)
363 T.TargetMachineCtorFn = Fn;
366 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
369 /// Clients are responsible for ensuring that registration doesn't occur
370 /// while another thread is attempting to access the registry. Typically
371 /// this is done by initializing all targets at program startup.
373 /// @param T - The target being registered.
374 /// @param Fn - A function to construct an AsmPrinter for the target.
375 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
376 // Ignore duplicate registration.
377 if (!T.AsmPrinterCtorFn)
378 T.AsmPrinterCtorFn = Fn;
381 /// RegisterAsmLexer - Register a TargetAsmLexer 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 AsmPrinter for the target.
390 static void RegisterAsmLexer(Target &T, Target::AsmLexerCtorTy Fn) {
391 if (!T.AsmLexerCtorFn)
392 T.AsmLexerCtorFn = Fn;
395 /// RegisterAsmParser - Register a TargetAsmParser 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 AsmPrinter for the target.
404 static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
405 if (!T.AsmParserCtorFn)
406 T.AsmParserCtorFn = Fn;
409 /// RegisterMCDisassembler - Register a MCDisassembler implementation for
410 /// the given target.
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 MCDisassembler for the target.
418 static void RegisterMCDisassembler(Target &T,
419 Target::MCDisassemblerCtorTy Fn) {
420 if (!T.MCDisassemblerCtorFn)
421 T.MCDisassemblerCtorFn = Fn;
424 /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
427 /// Clients are responsible for ensuring that registration doesn't occur
428 /// while another thread is attempting to access the registry. Typically
429 /// this is done by initializing all targets at program startup.
431 /// @param T - The target being registered.
432 /// @param Fn - A function to construct an MCInstPrinter for the target.
433 static void RegisterMCInstPrinter(Target &T,
434 Target::MCInstPrinterCtorTy Fn) {
435 if (!T.MCInstPrinterCtorFn)
436 T.MCInstPrinterCtorFn = Fn;
439 /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
442 /// Clients are responsible for ensuring that registration doesn't occur
443 /// while another thread is attempting to access the registry. Typically
444 /// this is done by initializing all targets at program startup.
446 /// @param T - The target being registered.
447 /// @param Fn - A function to construct an AsmPrinter for the target.
448 static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
449 if (!T.CodeEmitterCtorFn)
450 T.CodeEmitterCtorFn = Fn;
457 //===--------------------------------------------------------------------===//
459 /// RegisterTarget - Helper template for registering a target, for use in the
460 /// target's initialization function. Usage:
463 /// Target TheFooTarget; // The global target instance.
465 /// extern "C" void LLVMInitializeFooTargetInfo() {
466 /// RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
468 template<Triple::ArchType TargetArchType = Triple::InvalidArch,
470 struct RegisterTarget {
471 RegisterTarget(Target &T, const char *Name, const char *Desc) {
472 TargetRegistry::RegisterTarget(T, Name, Desc,
473 &getTripleMatchQuality,
477 static unsigned getTripleMatchQuality(const std::string &TT) {
478 if (Triple(TT).getArch() == TargetArchType)
484 /// RegisterAsmInfo - Helper template for registering a target assembly info
485 /// implementation. This invokes the static "Create" method on the class to
486 /// actually do the construction. Usage:
488 /// extern "C" void LLVMInitializeFooTarget() {
489 /// extern Target TheFooTarget;
490 /// RegisterAsmInfo<FooMCAsmInfo> X(TheFooTarget);
492 template<class MCAsmInfoImpl>
493 struct RegisterAsmInfo {
494 RegisterAsmInfo(Target &T) {
495 TargetRegistry::RegisterAsmInfo(T, &Allocator);
498 static const MCAsmInfo *Allocator(const Target &T, StringRef TT) {
499 return new MCAsmInfoImpl(T, TT);
504 /// RegisterAsmInfoFn - Helper template for registering a target assembly info
505 /// implementation. This invokes the specified function to do the
506 /// construction. Usage:
508 /// extern "C" void LLVMInitializeFooTarget() {
509 /// extern Target TheFooTarget;
510 /// RegisterAsmInfoFn X(TheFooTarget, TheFunction);
512 struct RegisterAsmInfoFn {
513 RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
514 TargetRegistry::RegisterAsmInfo(T, Fn);
519 /// RegisterTargetMachine - Helper template for registering a target machine
520 /// implementation, for use in the target machine initialization
523 /// extern "C" void LLVMInitializeFooTarget() {
524 /// extern Target TheFooTarget;
525 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
527 template<class TargetMachineImpl>
528 struct RegisterTargetMachine {
529 RegisterTargetMachine(Target &T) {
530 TargetRegistry::RegisterTargetMachine(T, &Allocator);
534 static TargetMachine *Allocator(const Target &T, const std::string &TT,
535 const std::string &FS) {
536 return new TargetMachineImpl(T, TT, FS);
540 /// RegisterAsmPrinter - Helper template for registering a target specific
541 /// assembly printer, for use in the target machine initialization
544 /// extern "C" void LLVMInitializeFooAsmPrinter() {
545 /// extern Target TheFooTarget;
546 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
548 template<class AsmPrinterImpl>
549 struct RegisterAsmPrinter {
550 RegisterAsmPrinter(Target &T) {
551 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
555 static AsmPrinter *Allocator(formatted_raw_ostream &OS, TargetMachine &TM,
556 MCContext &Ctx, MCStreamer &Streamer,
557 const MCAsmInfo *MAI) {
558 return new AsmPrinterImpl(OS, TM, Ctx, Streamer, MAI);
562 /// RegisterAsmLexer - Helper template for registering a target specific
563 /// assembly lexer, for use in the target machine initialization
566 /// extern "C" void LLVMInitializeFooAsmLexer() {
567 /// extern Target TheFooTarget;
568 /// RegisterAsmLexer<FooAsmLexer> X(TheFooTarget);
570 template<class AsmLexerImpl>
571 struct RegisterAsmLexer {
572 RegisterAsmLexer(Target &T) {
573 TargetRegistry::RegisterAsmLexer(T, &Allocator);
577 static TargetAsmLexer *Allocator(const Target &T, const MCAsmInfo &MAI) {
578 return new AsmLexerImpl(T, MAI);
582 /// RegisterAsmParser - Helper template for registering a target specific
583 /// assembly parser, for use in the target machine initialization
586 /// extern "C" void LLVMInitializeFooAsmParser() {
587 /// extern Target TheFooTarget;
588 /// RegisterAsmParser<FooAsmParser> X(TheFooTarget);
590 template<class AsmParserImpl>
591 struct RegisterAsmParser {
592 RegisterAsmParser(Target &T) {
593 TargetRegistry::RegisterAsmParser(T, &Allocator);
597 static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
598 return new AsmParserImpl(T, P);
602 /// RegisterCodeEmitter - Helper template for registering a target specific
603 /// machine code emitter, for use in the target initialization
606 /// extern "C" void LLVMInitializeFooCodeEmitter() {
607 /// extern Target TheFooTarget;
608 /// RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
610 template<class CodeEmitterImpl>
611 struct RegisterCodeEmitter {
612 RegisterCodeEmitter(Target &T) {
613 TargetRegistry::RegisterCodeEmitter(T, &Allocator);
617 static MCCodeEmitter *Allocator(const Target &T, TargetMachine &TM,
619 return new CodeEmitterImpl(T, TM, Ctx);