#ifndef LLVM_TARGET_TARGETREGISTRY_H
#define LLVM_TARGET_TARGETREGISTRY_H
+#include "llvm/ADT/Triple.h"
#include <string>
#include <cassert>
namespace llvm {
- class FunctionPass;
+ class AsmPrinter;
+ class MCAsmParser;
+ class MCCodeEmitter;
class Module;
+ class MCAsmInfo;
+ class MCDisassembler;
+ class MCInstPrinter;
+ class TargetAsmParser;
class TargetMachine;
class formatted_raw_ostream;
+ class raw_ostream;
/// Target - Wrapper for Target specific information.
///
/// will be zero initialized), and pass that instance to the TargetRegistry as
/// part of their initialization.
class Target {
- private:
+ public:
+ friend struct TargetRegistry;
+
typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
- typedef unsigned (*ModuleMatchQualityFnTy)(const Module &M);
- typedef unsigned (*JITMatchQualityFnTy)();
- typedef TargetMachine *(*TargetMachineCtorTy)(const Module &,
- const std::string &);
- typedef FunctionPass *(*AsmPrinterCtorTy)(formatted_raw_ostream &,
- TargetMachine &,
- bool);
-
- friend struct TargetRegistry;
- // FIXME: Temporary hack, please remove.
- friend struct TargetMachineRegistry;
+ typedef const MCAsmInfo *(*AsmInfoCtorFnTy)(const Target &T,
+ const StringRef &TT);
+ typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
+ const std::string &TT,
+ const std::string &Features);
+ typedef AsmPrinter *(*AsmPrinterCtorTy)(formatted_raw_ostream &OS,
+ TargetMachine &TM,
+ const MCAsmInfo *MAI,
+ bool VerboseAsm);
+ typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &T,
+ MCAsmParser &P);
+ typedef const MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T);
+ typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
+ unsigned SyntaxVariant,
+ const MCAsmInfo &MAI,
+ raw_ostream &O);
+ typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const Target &T,
+ TargetMachine &TM);
+ private:
/// Next - The next registered target in the linked list, maintained by the
/// TargetRegistry.
Target *Next;
/// of a triple.
TripleMatchQualityFnTy TripleMatchQualityFn;
- /// ModuleMatchQualityFn - The target function for rating the match quality
- /// of a module.
- ModuleMatchQualityFnTy ModuleMatchQualityFn;
-
- /// JITMatchQualityFn - The target function for rating the match quality
- /// with the host.
- JITMatchQualityFnTy JITMatchQualityFn;
-
/// Name - The target name.
const char *Name;
/// ShortDesc - A short description of the target.
const char *ShortDesc;
+ /// HasJIT - Whether this target supports the JIT.
+ bool HasJIT;
+
+ AsmInfoCtorFnTy AsmInfoCtorFn;
+
/// TargetMachineCtorFn - Construction function for this target's
/// TargetMachine, if registered.
TargetMachineCtorTy TargetMachineCtorFn;
/// if registered.
AsmPrinterCtorTy AsmPrinterCtorFn;
+ /// AsmParserCtorFn - Construction function for this target's AsmParser,
+ /// if registered.
+ AsmParserCtorTy AsmParserCtorFn;
+
+ /// MCDisassemblerCtorFn - Construction function for this target's
+ /// MCDisassembler, if registered.
+ MCDisassemblerCtorTy MCDisassemblerCtorFn;
+
+
+ /// MCInstPrinterCtorFn - Construction function for this target's
+ /// MCInstPrinter, if registered.
+ MCInstPrinterCtorTy MCInstPrinterCtorFn;
+
+ /// CodeEmitterCtorFn - Construction function for this target's CodeEmitter,
+ /// if registered.
+ CodeEmitterCtorTy CodeEmitterCtorFn;
+
public:
+ /// @name Target Information
+ /// @{
+
+ // getNext - Return the next registered target.
+ const Target *getNext() const { return Next; }
+
/// getName - Get the target name.
const char *getName() const { return Name; }
/// getShortDescription - Get a short description of the target.
const char *getShortDescription() const { return ShortDesc; }
- /// getJITMatchQuality - Get the quality of this targets match for use as a
- /// JIT.
- unsigned getJITMatchQuality() const { return JITMatchQualityFn(); }
+ /// @}
+ /// @name Feature Predicates
+ /// @{
+
+ /// hasJIT - Check if this targets supports the just-in-time compilation.
+ bool hasJIT() const { return HasJIT; }
+
+ /// hasTargetMachine - Check if this target supports code generation.
+ bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
+
+ /// hasAsmPrinter - Check if this target supports .s printing.
+ bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
+
+ /// hasAsmParser - Check if this target supports .s parsing.
+ bool hasAsmParser() const { return AsmParserCtorFn != 0; }
+
+ /// hasMCDisassembler - Check if this target has a disassembler.
+ bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
- /// createTargetMachine - Create a target specific machine implementation.
- TargetMachine *createTargetMachine(const Module &M,
+ /// hasMCInstPrinter - Check if this target has an instruction printer.
+ bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
+
+ /// hasCodeEmitter - Check if this target supports instruction encoding.
+ bool hasCodeEmitter() const { return CodeEmitterCtorFn != 0; }
+
+ /// @}
+ /// @name Feature Constructors
+ /// @{
+
+ /// createAsmInfo - Create a MCAsmInfo implementation for the specified
+ /// target triple.
+ ///
+ /// \arg Triple - This argument is used to determine the target machine
+ /// feature set; it should always be provided. Generally this should be
+ /// either the target triple from the module, or the target triple of the
+ /// host if that does not exist.
+ const MCAsmInfo *createAsmInfo(const StringRef &Triple) const {
+ if (!AsmInfoCtorFn)
+ return 0;
+ return AsmInfoCtorFn(*this, Triple);
+ }
+
+ /// createTargetMachine - Create a target specific machine implementation
+ /// for the specified \arg Triple.
+ ///
+ /// \arg Triple - This argument is used to determine the target machine
+ /// feature set; it should always be provided. Generally this should be
+ /// either the target triple from the module, or the target triple of the
+ /// host if that does not exist.
+ TargetMachine *createTargetMachine(const std::string &Triple,
const std::string &Features) const {
if (!TargetMachineCtorFn)
return 0;
- return TargetMachineCtorFn(M, Features);
+ return TargetMachineCtorFn(*this, Triple, Features);
}
/// createAsmPrinter - Create a target specific assembly printer pass.
- FunctionPass *createAsmPrinter(formatted_raw_ostream &OS,
- TargetMachine &M,
- bool Verbose) const {
+ AsmPrinter *createAsmPrinter(formatted_raw_ostream &OS, TargetMachine &TM,
+ const MCAsmInfo *MAI, bool Verbose) const {
if (!AsmPrinterCtorFn)
return 0;
- return AsmPrinterCtorFn(OS, M, Verbose);
+ return AsmPrinterCtorFn(OS, TM, MAI, Verbose);
+ }
+
+ /// createAsmParser - Create a target specific assembly parser.
+ ///
+ /// \arg Parser - The target independent parser implementation to use for
+ /// parsing and lexing.
+ TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
+ if (!AsmParserCtorFn)
+ return 0;
+ return AsmParserCtorFn(*this, Parser);
}
+
+ const MCDisassembler *createMCDisassembler() const {
+ if (!MCDisassemblerCtorFn)
+ return 0;
+ return MCDisassemblerCtorFn(*this);
+ }
+
+ MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
+ const MCAsmInfo &MAI,
+ raw_ostream &O) const {
+ if (!MCInstPrinterCtorFn)
+ return 0;
+ return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI, O);
+ }
+
+
+ /// createCodeEmitter - Create a target specific code emitter.
+ MCCodeEmitter *createCodeEmitter(TargetMachine &TM) const {
+ if (!CodeEmitterCtorFn)
+ return 0;
+ return CodeEmitterCtorFn(*this, TM);
+ }
+
+ /// @}
};
/// TargetRegistry - Generic interface to target specific features.
- //
- // FIXME: Provide Target* iterator.
struct TargetRegistry {
+ class iterator {
+ const Target *Current;
+ explicit iterator(Target *T) : Current(T) {}
+ friend struct TargetRegistry;
+ public:
+ iterator(const iterator &I) : Current(I.Current) {}
+ iterator() : Current(0) {}
+
+ bool operator==(const iterator &x) const {
+ return Current == x.Current;
+ }
+ bool operator!=(const iterator &x) const {
+ return !operator==(x);
+ }
+
+ // Iterator traversal: forward iteration only
+ iterator &operator++() { // Preincrement
+ assert(Current && "Cannot increment end iterator!");
+ Current = Current->getNext();
+ return *this;
+ }
+ iterator operator++(int) { // Postincrement
+ iterator tmp = *this;
+ ++*this;
+ return tmp;
+ }
+
+ const Target &operator*() const {
+ assert(Current && "Cannot dereference end iterator!");
+ return *Current;
+ }
+
+ const Target *operator->() const {
+ return &operator*();
+ }
+ };
+
/// @name Registry Access
/// @{
- /// getClosestStaticTargetForTriple - Given a target triple, pick the most
- /// capable target for that triple.
- static const Target *getClosestStaticTargetForTriple(const std::string &TT,
- std::string &Error);
+ static iterator begin();
- /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
- /// target that is compatible with the module. If no close target can be
- /// found, this returns null and sets the Error string to a reason.
- static const Target *getClosestStaticTargetForModule(const Module &M,
- std::string &Error);
+ static iterator end() { return iterator(); }
+
+ /// lookupTarget - Lookup a target based on a target triple.
+ ///
+ /// \param Triple - The triple to use for finding a target.
+ /// \param Error - On failure, an error string describing why no target was
+ /// found.
+ static const Target *lookupTarget(const std::string &Triple,
+ std::string &Error);
/// getClosestTargetForJIT - Pick the best target that is compatible with
/// the current host. If no close target can be found, this returns null
/// and sets the Error string to a reason.
- //
- // FIXME: Do we still need this interface, clients can always look for the
- // match for the host triple.
+ ///
+ /// Maintained for compatibility through 2.6.
static const Target *getClosestTargetForJIT(std::string &Error);
/// @}
/// string.
/// @param TQualityFn - The triple match quality computation function for
/// this target.
- /// @param MQualityFn - The module match quality computation function for
- /// this target.
- /// @param JITMatchQualityFn - The JIT match quality computation function
- /// for this target.
+ /// @param HasJIT - Whether the target supports JIT code
+ /// generation.
static void RegisterTarget(Target &T,
const char *Name,
const char *ShortDesc,
Target::TripleMatchQualityFnTy TQualityFn,
- Target::ModuleMatchQualityFnTy MQualityFn,
- Target::JITMatchQualityFnTy JITQualityFn);
-
+ bool HasJIT = false);
+
+ /// RegisterAsmInfo - Register a MCAsmInfo implementation for the
+ /// given target.
+ ///
+ /// Clients are responsible for ensuring that registration doesn't occur
+ /// while another thread is attempting to access the registry. Typically
+ /// this is done by initializing all targets at program startup.
+ ///
+ /// @param T - The target being registered.
+ /// @param Fn - A function to construct a MCAsmInfo for the target.
+ static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
+ // Ignore duplicate registration.
+ if (!T.AsmInfoCtorFn)
+ T.AsmInfoCtorFn = Fn;
+ }
+
/// RegisterTargetMachine - Register a TargetMachine implementation for the
/// given target.
///
/// @param Fn - A function to construct a TargetMachine for the target.
static void RegisterTargetMachine(Target &T,
Target::TargetMachineCtorTy Fn) {
- assert(!T.TargetMachineCtorFn && "Constructor already registered!");
- T.TargetMachineCtorFn = Fn;
+ // Ignore duplicate registration.
+ if (!T.TargetMachineCtorFn)
+ T.TargetMachineCtorFn = Fn;
}
/// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
/// @param T - The target being registered.
/// @param Fn - A function to construct an AsmPrinter for the target.
static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
- assert(!T.AsmPrinterCtorFn && "Constructor already registered!");
- T.AsmPrinterCtorFn = Fn;
+ // Ignore duplicate registration.
+ if (!T.AsmPrinterCtorFn)
+ T.AsmPrinterCtorFn = Fn;
+ }
+
+ /// RegisterAsmParser - Register a TargetAsmParser implementation for the
+ /// given target.
+ ///
+ /// Clients are responsible for ensuring that registration doesn't occur
+ /// while another thread is attempting to access the registry. Typically
+ /// this is done by initializing all targets at program startup.
+ ///
+ /// @param T - The target being registered.
+ /// @param Fn - A function to construct an AsmPrinter for the target.
+ static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
+ if (!T.AsmParserCtorFn)
+ T.AsmParserCtorFn = Fn;
+ }
+
+ /// RegisterMCDisassembler - Register a MCDisassembler implementation for
+ /// the given target.
+ ///
+ /// Clients are responsible for ensuring that registration doesn't occur
+ /// while another thread is attempting to access the registry. Typically
+ /// this is done by initializing all targets at program startup.
+ ///
+ /// @param T - The target being registered.
+ /// @param Fn - A function to construct an MCDisassembler for the target.
+ static void RegisterMCDisassembler(Target &T,
+ Target::MCDisassemblerCtorTy Fn) {
+ if (!T.MCDisassemblerCtorFn)
+ T.MCDisassemblerCtorFn = Fn;
+ }
+
+ static void RegisterMCInstPrinter(Target &T,
+ Target::MCInstPrinterCtorTy Fn) {
+ if (!T.MCInstPrinterCtorFn)
+ T.MCInstPrinterCtorFn = Fn;
+ }
+
+ /// RegisterCodeEmitter - Register a MCCodeEmitter implementation for the
+ /// given target.
+ ///
+ /// Clients are responsible for ensuring that registration doesn't occur
+ /// while another thread is attempting to access the registry. Typically
+ /// this is done by initializing all targets at program startup.
+ ///
+ /// @param T - The target being registered.
+ /// @param Fn - A function to construct an AsmPrinter for the target.
+ static void RegisterCodeEmitter(Target &T, Target::CodeEmitterCtorTy Fn) {
+ if (!T.CodeEmitterCtorFn)
+ T.CodeEmitterCtorFn = Fn;
}
/// @}
};
+
+ //===--------------------------------------------------------------------===//
+
+ /// RegisterTarget - Helper template for registering a target, for use in the
+ /// target's initialization function. Usage:
+ ///
+ ///
+ /// Target TheFooTarget; // The global target instance.
+ ///
+ /// extern "C" void LLVMInitializeFooTargetInfo() {
+ /// RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
+ /// }
+ template<Triple::ArchType TargetArchType = Triple::InvalidArch,
+ bool HasJIT = false>
+ struct RegisterTarget {
+ RegisterTarget(Target &T, const char *Name, const char *Desc) {
+ TargetRegistry::RegisterTarget(T, Name, Desc,
+ &getTripleMatchQuality,
+ HasJIT);
+ }
+
+ static unsigned getTripleMatchQuality(const std::string &TT) {
+ if (Triple(TT).getArch() == TargetArchType)
+ return 20;
+ return 0;
+ }
+ };
+
+ /// RegisterAsmInfo - Helper template for registering a target assembly info
+ /// implementation. This invokes the static "Create" method on the class to
+ /// actually do the construction. Usage:
+ ///
+ /// extern "C" void LLVMInitializeFooTarget() {
+ /// extern Target TheFooTarget;
+ /// RegisterAsmInfo<FooMCAsmInfo> X(TheFooTarget);
+ /// }
+ template<class MCAsmInfoImpl>
+ struct RegisterAsmInfo {
+ RegisterAsmInfo(Target &T) {
+ TargetRegistry::RegisterAsmInfo(T, &Allocator);
+ }
+ private:
+ static const MCAsmInfo *Allocator(const Target &T, const StringRef &TT) {
+ return new MCAsmInfoImpl(T, TT);
+ }
+
+ };
+
+ /// RegisterAsmInfoFn - Helper template for registering a target assembly info
+ /// implementation. This invokes the specified function to do the
+ /// construction. Usage:
+ ///
+ /// extern "C" void LLVMInitializeFooTarget() {
+ /// extern Target TheFooTarget;
+ /// RegisterAsmInfoFn X(TheFooTarget, TheFunction);
+ /// }
+ struct RegisterAsmInfoFn {
+ RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
+ TargetRegistry::RegisterAsmInfo(T, Fn);
+ }
+ };
+
+
+ /// RegisterTargetMachine - Helper template for registering a target machine
+ /// implementation, for use in the target machine initialization
+ /// function. Usage:
+ ///
+ /// extern "C" void LLVMInitializeFooTarget() {
+ /// extern Target TheFooTarget;
+ /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
+ /// }
+ template<class TargetMachineImpl>
+ struct RegisterTargetMachine {
+ RegisterTargetMachine(Target &T) {
+ TargetRegistry::RegisterTargetMachine(T, &Allocator);
+ }
+
+ private:
+ static TargetMachine *Allocator(const Target &T, const std::string &TT,
+ const std::string &FS) {
+ return new TargetMachineImpl(T, TT, FS);
+ }
+ };
+
+ /// RegisterAsmPrinter - Helper template for registering a target specific
+ /// assembly printer, for use in the target machine initialization
+ /// function. Usage:
+ ///
+ /// extern "C" void LLVMInitializeFooAsmPrinter() {
+ /// extern Target TheFooTarget;
+ /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
+ /// }
+ template<class AsmPrinterImpl>
+ struct RegisterAsmPrinter {
+ RegisterAsmPrinter(Target &T) {
+ TargetRegistry::RegisterAsmPrinter(T, &Allocator);
+ }
+
+ private:
+ static AsmPrinter *Allocator(formatted_raw_ostream &OS, TargetMachine &TM,
+ const MCAsmInfo *MAI, bool Verbose) {
+ return new AsmPrinterImpl(OS, TM, MAI, Verbose);
+ }
+ };
+
+ /// RegisterAsmParser - Helper template for registering a target specific
+ /// assembly parser, for use in the target machine initialization
+ /// function. Usage:
+ ///
+ /// extern "C" void LLVMInitializeFooAsmParser() {
+ /// extern Target TheFooTarget;
+ /// RegisterAsmParser<FooAsmParser> X(TheFooTarget);
+ /// }
+ template<class AsmParserImpl>
+ struct RegisterAsmParser {
+ RegisterAsmParser(Target &T) {
+ TargetRegistry::RegisterAsmParser(T, &Allocator);
+ }
+
+ private:
+ static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
+ return new AsmParserImpl(T, P);
+ }
+ };
+
+ /// RegisterCodeEmitter - Helper template for registering a target specific
+ /// machine code emitter, for use in the target initialization
+ /// function. Usage:
+ ///
+ /// extern "C" void LLVMInitializeFooCodeEmitter() {
+ /// extern Target TheFooTarget;
+ /// RegisterCodeEmitter<FooCodeEmitter> X(TheFooTarget);
+ /// }
+ template<class CodeEmitterImpl>
+ struct RegisterCodeEmitter {
+ RegisterCodeEmitter(Target &T) {
+ TargetRegistry::RegisterCodeEmitter(T, &Allocator);
+ }
+
+ private:
+ static MCCodeEmitter *Allocator(const Target &T, TargetMachine &TM) {
+ return new CodeEmitterImpl(T, TM);
+ }
+ };
+
}
#endif