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(const 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 unsigned getNumTargets() {
273 for (iterator I = begin(), E = end(); I != E; ++I)
278 static iterator begin();
280 static iterator end() { return iterator(); }
282 /// lookupTarget - Lookup a target based on a target triple.
284 /// \param Triple - The triple to use for finding a target.
285 /// \param Error - On failure, an error string describing why no target was
287 static const Target *lookupTarget(const std::string &Triple,
290 /// getClosestTargetForJIT - Pick the best target that is compatible with
291 /// the current host. If no close target can be found, this returns null
292 /// and sets the Error string to a reason.
294 /// Maintained for compatibility through 2.6.
295 static const Target *getClosestTargetForJIT(std::string &Error);
298 /// @name Target Registration
301 /// RegisterTarget - Register the given target. Attempts to register a
302 /// target which has already been registered will be ignored.
304 /// Clients are responsible for ensuring that registration doesn't occur
305 /// while another thread is attempting to access the registry. Typically
306 /// this is done by initializing all targets at program startup.
308 /// @param T - The target being registered.
309 /// @param Name - The target name. This should be a static string.
310 /// @param ShortDesc - A short target description. This should be a static
312 /// @param TQualityFn - The triple match quality computation function for
314 /// @param HasJIT - Whether the target supports JIT code
316 static void RegisterTarget(Target &T,
318 const char *ShortDesc,
319 Target::TripleMatchQualityFnTy TQualityFn,
320 bool HasJIT = false);
322 /// RegisterAsmInfo - Register a MCAsmInfo implementation for the
325 /// Clients are responsible for ensuring that registration doesn't occur
326 /// while another thread is attempting to access the registry. Typically
327 /// this is done by initializing all targets at program startup.
329 /// @param T - The target being registered.
330 /// @param Fn - A function to construct a MCAsmInfo for the target.
331 static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
332 // Ignore duplicate registration.
333 if (!T.AsmInfoCtorFn)
334 T.AsmInfoCtorFn = Fn;
337 /// RegisterTargetMachine - Register a TargetMachine implementation for the
340 /// Clients are responsible for ensuring that registration doesn't occur
341 /// while another thread is attempting to access the registry. Typically
342 /// this is done by initializing all targets at program startup.
344 /// @param T - The target being registered.
345 /// @param Fn - A function to construct a TargetMachine for the target.
346 static void RegisterTargetMachine(Target &T,
347 Target::TargetMachineCtorTy Fn) {
348 // Ignore duplicate registration.
349 if (!T.TargetMachineCtorFn)
350 T.TargetMachineCtorFn = Fn;
353 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
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 an AsmPrinter for the target.
362 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
363 // Ignore duplicate registration.
364 if (!T.AsmPrinterCtorFn)
365 T.AsmPrinterCtorFn = Fn;
368 /// RegisterAsmParser - Register a TargetAsmParser 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 an AsmPrinter for the target.
377 static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
378 if (!T.AsmParserCtorFn)
379 T.AsmParserCtorFn = Fn;
382 /// RegisterMCDisassembler - Register a MCDisassembler implementation for
383 /// the given target.
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 an MCDisassembler for the target.
391 static void RegisterMCDisassembler(Target &T,
392 Target::MCDisassemblerCtorTy Fn) {
393 if (!T.MCDisassemblerCtorFn)
394 T.MCDisassemblerCtorFn = Fn;
397 static void RegisterMCInstPrinter(Target &T,
398 Target::MCInstPrinterCtorTy Fn) {
399 if (!T.MCInstPrinterCtorFn)
400 T.MCInstPrinterCtorFn = Fn;
403 /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
406 /// Clients are responsible for ensuring that registration doesn't occur
407 /// while another thread is attempting to access the registry. Typically
408 /// this is done by initializing all targets at program startup.
410 /// @param T - The target being registered.
411 /// @param Fn - A function to construct an AsmPrinter for the target.
412 static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
413 if (!T.CodeEmitterCtorFn)
414 T.CodeEmitterCtorFn = Fn;
421 //===--------------------------------------------------------------------===//
423 /// RegisterTarget - Helper template for registering a target, for use in the
424 /// target's initialization function. Usage:
427 /// Target TheFooTarget; // The global target instance.
429 /// extern "C" void LLVMInitializeFooTargetInfo() {
430 /// RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
432 template<Triple::ArchType TargetArchType = Triple::InvalidArch,
434 struct RegisterTarget {
435 RegisterTarget(Target &T, const char *Name, const char *Desc) {
436 TargetRegistry::RegisterTarget(T, Name, Desc,
437 &getTripleMatchQuality,
441 static unsigned getTripleMatchQuality(const std::string &TT) {
442 if (Triple(TT).getArch() == TargetArchType)
448 /// RegisterAsmInfo - Helper template for registering a target assembly info
449 /// implementation. This invokes the static "Create" method on the class to
450 /// actually do the construction. Usage:
452 /// extern "C" void LLVMInitializeFooTarget() {
453 /// extern Target TheFooTarget;
454 /// RegisterAsmInfo<FooMCAsmInfo> X(TheFooTarget);
456 template<class MCAsmInfoImpl>
457 struct RegisterAsmInfo {
458 RegisterAsmInfo(Target &T) {
459 TargetRegistry::RegisterAsmInfo(T, &Allocator);
462 static const MCAsmInfo *Allocator(const Target &T, const StringRef &TT) {
463 return new MCAsmInfoImpl(T, TT);
468 /// RegisterAsmInfoFn - Helper template for registering a target assembly info
469 /// implementation. This invokes the specified function to do the
470 /// construction. Usage:
472 /// extern "C" void LLVMInitializeFooTarget() {
473 /// extern Target TheFooTarget;
474 /// RegisterAsmInfoFn X(TheFooTarget, TheFunction);
476 struct RegisterAsmInfoFn {
477 RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
478 TargetRegistry::RegisterAsmInfo(T, Fn);
483 /// RegisterTargetMachine - Helper template for registering a target machine
484 /// implementation, for use in the target machine initialization
487 /// extern "C" void LLVMInitializeFooTarget() {
488 /// extern Target TheFooTarget;
489 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
491 template<class TargetMachineImpl>
492 struct RegisterTargetMachine {
493 RegisterTargetMachine(Target &T) {
494 TargetRegistry::RegisterTargetMachine(T, &Allocator);
498 static TargetMachine *Allocator(const Target &T, const std::string &TT,
499 const std::string &FS) {
500 return new TargetMachineImpl(T, TT, FS);
504 /// RegisterAsmPrinter - Helper template for registering a target specific
505 /// assembly printer, for use in the target machine initialization
508 /// extern "C" void LLVMInitializeFooAsmPrinter() {
509 /// extern Target TheFooTarget;
510 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
512 template<class AsmPrinterImpl>
513 struct RegisterAsmPrinter {
514 RegisterAsmPrinter(Target &T) {
515 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
519 static AsmPrinter *Allocator(formatted_raw_ostream &OS, TargetMachine &TM,
520 const MCAsmInfo *MAI, bool Verbose) {
521 return new AsmPrinterImpl(OS, TM, MAI, Verbose);
525 /// RegisterAsmParser - Helper template for registering a target specific
526 /// assembly parser, for use in the target machine initialization
529 /// extern "C" void LLVMInitializeFooAsmParser() {
530 /// extern Target TheFooTarget;
531 /// RegisterAsmParser<FooAsmParser> X(TheFooTarget);
533 template<class AsmParserImpl>
534 struct RegisterAsmParser {
535 RegisterAsmParser(Target &T) {
536 TargetRegistry::RegisterAsmParser(T, &Allocator);
540 static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
541 return new AsmParserImpl(T, P);
545 /// RegisterCodeEmitter - Helper template for registering a target specific
546 /// machine code emitter, for use in the target initialization
549 /// extern "C" void LLVMInitializeFooCodeEmitter() {
550 /// extern Target TheFooTarget;
551 /// RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
553 template<class CodeEmitterImpl>
554 struct RegisterCodeEmitter {
555 RegisterCodeEmitter(Target &T) {
556 TargetRegistry::RegisterCodeEmitter(T, &Allocator);
560 static MCCodeEmitter *Allocator(const Target &T, TargetMachine &TM) {
561 return new CodeEmitterImpl(T, TM);