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"
34 class TargetAsmParser;
36 class formatted_raw_ostream;
39 /// Target - Wrapper for Target specific information.
41 /// For registration purposes, this is a POD type so that targets can be
42 /// registered without the use of static constructors.
44 /// Targets should implement a single global instance of this class (which
45 /// will be zero initialized), and pass that instance to the TargetRegistry as
46 /// part of their initialization.
49 friend struct TargetRegistry;
51 typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
53 typedef const MCAsmInfo *(*AsmInfoCtorFnTy)(const Target &T,
55 typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
56 const std::string &TT,
57 const std::string &Features);
58 typedef AsmPrinter *(*AsmPrinterCtorTy)(formatted_raw_ostream &OS,
62 typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &T,
64 typedef const MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T);
65 typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
66 unsigned SyntaxVariant,
69 typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const Target &T,
73 /// Next - The next registered target in the linked list, maintained by the
77 /// TripleMatchQualityFn - The target function for rating the match quality
79 TripleMatchQualityFnTy TripleMatchQualityFn;
81 /// Name - The target name.
84 /// ShortDesc - A short description of the target.
85 const char *ShortDesc;
87 /// HasJIT - Whether this target supports the JIT.
90 AsmInfoCtorFnTy AsmInfoCtorFn;
92 /// TargetMachineCtorFn - Construction function for this target's
93 /// TargetMachine, if registered.
94 TargetMachineCtorTy TargetMachineCtorFn;
96 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
98 AsmPrinterCtorTy AsmPrinterCtorFn;
100 /// AsmParserCtorFn - Construction function for this target's AsmParser,
102 AsmParserCtorTy AsmParserCtorFn;
104 /// MCDisassemblerCtorFn - Construction function for this target's
105 /// MCDisassembler, if registered.
106 MCDisassemblerCtorTy MCDisassemblerCtorFn;
109 /// MCInstPrinterCtorFn - Construction function for this target's
110 /// MCInstPrinter, if registered.
111 MCInstPrinterCtorTy MCInstPrinterCtorFn;
113 /// CodeEmitterCtorFn - Construction function for this target's CodeEmitter,
115 CodeEmitterCtorTy CodeEmitterCtorFn;
118 /// @name Target Information
121 // getNext - Return the next registered target.
122 const Target *getNext() const { return Next; }
124 /// getName - Get the target name.
125 const char *getName() const { return Name; }
127 /// getShortDescription - Get a short description of the target.
128 const char *getShortDescription() const { return ShortDesc; }
131 /// @name Feature Predicates
134 /// hasJIT - Check if this targets supports the just-in-time compilation.
135 bool hasJIT() const { return HasJIT; }
137 /// hasTargetMachine - Check if this target supports code generation.
138 bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
140 /// hasAsmPrinter - Check if this target supports .s printing.
141 bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
143 /// hasAsmParser - Check if this target supports .s parsing.
144 bool hasAsmParser() const { return AsmParserCtorFn != 0; }
146 /// hasMCDisassembler - Check if this target has a disassembler.
147 bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
149 /// hasMCInstPrinter - Check if this target has an instruction printer.
150 bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
152 /// hasCodeEmitter - Check if this target supports instruction encoding.
153 bool hasCodeEmitter() const { return CodeEmitterCtorFn != 0; }
156 /// @name Feature Constructors
159 /// createAsmInfo - Create a MCAsmInfo implementation for the specified
162 /// \arg Triple - This argument is used to determine the target machine
163 /// feature set; it should always be provided. Generally this should be
164 /// either the target triple from the module, or the target triple of the
165 /// host if that does not exist.
166 const MCAsmInfo *createAsmInfo(StringRef Triple) const {
169 return AsmInfoCtorFn(*this, Triple);
172 /// createTargetMachine - Create a target specific machine implementation
173 /// for the specified \arg Triple.
175 /// \arg Triple - This argument is used to determine the target machine
176 /// feature set; it should always be provided. Generally this should be
177 /// either the target triple from the module, or the target triple of the
178 /// host if that does not exist.
179 TargetMachine *createTargetMachine(const std::string &Triple,
180 const std::string &Features) const {
181 if (!TargetMachineCtorFn)
183 return TargetMachineCtorFn(*this, Triple, Features);
186 /// createAsmPrinter - Create a target specific assembly printer pass.
187 AsmPrinter *createAsmPrinter(formatted_raw_ostream &OS, TargetMachine &TM,
188 const MCAsmInfo *MAI, bool Verbose) const {
189 if (!AsmPrinterCtorFn)
191 return AsmPrinterCtorFn(OS, TM, MAI, Verbose);
194 /// createAsmParser - Create a target specific assembly parser.
196 /// \arg Parser - The target independent parser implementation to use for
197 /// parsing and lexing.
198 TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
199 if (!AsmParserCtorFn)
201 return AsmParserCtorFn(*this, Parser);
204 const MCDisassembler *createMCDisassembler() const {
205 if (!MCDisassemblerCtorFn)
207 return MCDisassemblerCtorFn(*this);
210 MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
211 const MCAsmInfo &MAI,
212 raw_ostream &O) const {
213 if (!MCInstPrinterCtorFn)
215 return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI, O);
219 /// createCodeEmitter - Create a target specific code emitter.
220 MCCodeEmitter *createCodeEmitter(TargetMachine &TM) const {
221 if (!CodeEmitterCtorFn)
223 return CodeEmitterCtorFn(*this, TM);
229 /// TargetRegistry - Generic interface to target specific features.
230 struct TargetRegistry {
232 const Target *Current;
233 explicit iterator(Target *T) : Current(T) {}
234 friend struct TargetRegistry;
236 iterator(const iterator &I) : Current(I.Current) {}
237 iterator() : Current(0) {}
239 bool operator==(const iterator &x) const {
240 return Current == x.Current;
242 bool operator!=(const iterator &x) const {
243 return !operator==(x);
246 // Iterator traversal: forward iteration only
247 iterator &operator++() { // Preincrement
248 assert(Current && "Cannot increment end iterator!");
249 Current = Current->getNext();
252 iterator operator++(int) { // Postincrement
253 iterator tmp = *this;
258 const Target &operator*() const {
259 assert(Current && "Cannot dereference end iterator!");
263 const Target *operator->() const {
268 /// @name Registry Access
271 static iterator begin();
273 static iterator end() { return iterator(); }
275 /// lookupTarget - Lookup a target based on a target triple.
277 /// \param Triple - The triple to use for finding a target.
278 /// \param Error - On failure, an error string describing why no target was
280 static const Target *lookupTarget(const std::string &Triple,
283 /// getClosestTargetForJIT - Pick the best target that is compatible with
284 /// the current host. If no close target can be found, this returns null
285 /// and sets the Error string to a reason.
287 /// Maintained for compatibility through 2.6.
288 static const Target *getClosestTargetForJIT(std::string &Error);
291 /// @name Target Registration
294 /// RegisterTarget - Register the given target. Attempts to register a
295 /// target which has already been registered will be ignored.
297 /// Clients are responsible for ensuring that registration doesn't occur
298 /// while another thread is attempting to access the registry. Typically
299 /// this is done by initializing all targets at program startup.
301 /// @param T - The target being registered.
302 /// @param Name - The target name. This should be a static string.
303 /// @param ShortDesc - A short target description. This should be a static
305 /// @param TQualityFn - The triple match quality computation function for
307 /// @param HasJIT - Whether the target supports JIT code
309 static void RegisterTarget(Target &T,
311 const char *ShortDesc,
312 Target::TripleMatchQualityFnTy TQualityFn,
313 bool HasJIT = false);
315 /// RegisterAsmInfo - Register a MCAsmInfo implementation for the
318 /// Clients are responsible for ensuring that registration doesn't occur
319 /// while another thread is attempting to access the registry. Typically
320 /// this is done by initializing all targets at program startup.
322 /// @param T - The target being registered.
323 /// @param Fn - A function to construct a MCAsmInfo for the target.
324 static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
325 // Ignore duplicate registration.
326 if (!T.AsmInfoCtorFn)
327 T.AsmInfoCtorFn = Fn;
330 /// RegisterTargetMachine - Register a TargetMachine implementation for the
333 /// Clients are responsible for ensuring that registration doesn't occur
334 /// while another thread is attempting to access the registry. Typically
335 /// this is done by initializing all targets at program startup.
337 /// @param T - The target being registered.
338 /// @param Fn - A function to construct a TargetMachine for the target.
339 static void RegisterTargetMachine(Target &T,
340 Target::TargetMachineCtorTy Fn) {
341 // Ignore duplicate registration.
342 if (!T.TargetMachineCtorFn)
343 T.TargetMachineCtorFn = Fn;
346 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
349 /// Clients are responsible for ensuring that registration doesn't occur
350 /// while another thread is attempting to access the registry. Typically
351 /// this is done by initializing all targets at program startup.
353 /// @param T - The target being registered.
354 /// @param Fn - A function to construct an AsmPrinter for the target.
355 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
356 // Ignore duplicate registration.
357 if (!T.AsmPrinterCtorFn)
358 T.AsmPrinterCtorFn = Fn;
361 /// RegisterAsmParser - Register a TargetAsmParser implementation for the
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 Fn - A function to construct an AsmPrinter for the target.
370 static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
371 if (!T.AsmParserCtorFn)
372 T.AsmParserCtorFn = Fn;
375 /// RegisterMCDisassembler - Register a MCDisassembler implementation for
376 /// the given target.
378 /// Clients are responsible for ensuring that registration doesn't occur
379 /// while another thread is attempting to access the registry. Typically
380 /// this is done by initializing all targets at program startup.
382 /// @param T - The target being registered.
383 /// @param Fn - A function to construct an MCDisassembler for the target.
384 static void RegisterMCDisassembler(Target &T,
385 Target::MCDisassemblerCtorTy Fn) {
386 if (!T.MCDisassemblerCtorFn)
387 T.MCDisassemblerCtorFn = Fn;
390 /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
393 /// Clients are responsible for ensuring that registration doesn't occur
394 /// while another thread is attempting to access the registry. Typically
395 /// this is done by initializing all targets at program startup.
397 /// @param T - The target being registered.
398 /// @param Fn - A function to construct an MCInstPrinter for the target.
399 static void RegisterMCInstPrinter(Target &T,
400 Target::MCInstPrinterCtorTy Fn) {
401 if (!T.MCInstPrinterCtorFn)
402 T.MCInstPrinterCtorFn = Fn;
405 /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
408 /// Clients are responsible for ensuring that registration doesn't occur
409 /// while another thread is attempting to access the registry. Typically
410 /// this is done by initializing all targets at program startup.
412 /// @param T - The target being registered.
413 /// @param Fn - A function to construct an AsmPrinter for the target.
414 static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
415 if (!T.CodeEmitterCtorFn)
416 T.CodeEmitterCtorFn = Fn;
423 //===--------------------------------------------------------------------===//
425 /// RegisterTarget - Helper template for registering a target, for use in the
426 /// target's initialization function. Usage:
429 /// Target TheFooTarget; // The global target instance.
431 /// extern "C" void LLVMInitializeFooTargetInfo() {
432 /// RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
434 template<Triple::ArchType TargetArchType = Triple::InvalidArch,
436 struct RegisterTarget {
437 RegisterTarget(Target &T, const char *Name, const char *Desc) {
438 TargetRegistry::RegisterTarget(T, Name, Desc,
439 &getTripleMatchQuality,
443 static unsigned getTripleMatchQuality(const std::string &TT) {
444 if (Triple(TT).getArch() == TargetArchType)
450 /// RegisterAsmInfo - Helper template for registering a target assembly info
451 /// implementation. This invokes the static "Create" method on the class to
452 /// actually do the construction. Usage:
454 /// extern "C" void LLVMInitializeFooTarget() {
455 /// extern Target TheFooTarget;
456 /// RegisterAsmInfo<FooMCAsmInfo> X(TheFooTarget);
458 template<class MCAsmInfoImpl>
459 struct RegisterAsmInfo {
460 RegisterAsmInfo(Target &T) {
461 TargetRegistry::RegisterAsmInfo(T, &Allocator);
464 static const MCAsmInfo *Allocator(const Target &T, StringRef TT) {
465 return new MCAsmInfoImpl(T, TT);
470 /// RegisterAsmInfoFn - Helper template for registering a target assembly info
471 /// implementation. This invokes the specified function to do the
472 /// construction. Usage:
474 /// extern "C" void LLVMInitializeFooTarget() {
475 /// extern Target TheFooTarget;
476 /// RegisterAsmInfoFn X(TheFooTarget, TheFunction);
478 struct RegisterAsmInfoFn {
479 RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
480 TargetRegistry::RegisterAsmInfo(T, Fn);
485 /// RegisterTargetMachine - Helper template for registering a target machine
486 /// implementation, for use in the target machine initialization
489 /// extern "C" void LLVMInitializeFooTarget() {
490 /// extern Target TheFooTarget;
491 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
493 template<class TargetMachineImpl>
494 struct RegisterTargetMachine {
495 RegisterTargetMachine(Target &T) {
496 TargetRegistry::RegisterTargetMachine(T, &Allocator);
500 static TargetMachine *Allocator(const Target &T, const std::string &TT,
501 const std::string &FS) {
502 return new TargetMachineImpl(T, TT, FS);
506 /// RegisterAsmPrinter - Helper template for registering a target specific
507 /// assembly printer, for use in the target machine initialization
510 /// extern "C" void LLVMInitializeFooAsmPrinter() {
511 /// extern Target TheFooTarget;
512 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
514 template<class AsmPrinterImpl>
515 struct RegisterAsmPrinter {
516 RegisterAsmPrinter(Target &T) {
517 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
521 static AsmPrinter *Allocator(formatted_raw_ostream &OS, TargetMachine &TM,
522 const MCAsmInfo *MAI, bool Verbose) {
523 return new AsmPrinterImpl(OS, TM, MAI, Verbose);
527 /// RegisterAsmParser - Helper template for registering a target specific
528 /// assembly parser, for use in the target machine initialization
531 /// extern "C" void LLVMInitializeFooAsmParser() {
532 /// extern Target TheFooTarget;
533 /// RegisterAsmParser<FooAsmParser> X(TheFooTarget);
535 template<class AsmParserImpl>
536 struct RegisterAsmParser {
537 RegisterAsmParser(Target &T) {
538 TargetRegistry::RegisterAsmParser(T, &Allocator);
542 static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
543 return new AsmParserImpl(T, P);
547 /// RegisterCodeEmitter - Helper template for registering a target specific
548 /// machine code emitter, for use in the target initialization
551 /// extern "C" void LLVMInitializeFooCodeEmitter() {
552 /// extern Target TheFooTarget;
553 /// RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
555 template<class CodeEmitterImpl>
556 struct RegisterCodeEmitter {
557 RegisterCodeEmitter(Target &T) {
558 TargetRegistry::RegisterCodeEmitter(T, &Allocator);
562 static MCCodeEmitter *Allocator(const Target &T, TargetMachine &TM) {
563 return new CodeEmitterImpl(T, TM);