2009-07-15 04:24:58 +00:00
|
|
|
//===-- Target/TargetRegistry.h - Target Registration -----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file exposes the TargetRegistry interface, which tools can use to access
|
|
|
|
// the appropriate target specific classes (TargetMachine, AsmPrinter, etc.)
|
|
|
|
// which have been registered.
|
|
|
|
//
|
|
|
|
// Target specific class implementations should register themselves using the
|
|
|
|
// appropriate TargetRegistry interfaces.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TARGET_TARGETREGISTRY_H
|
|
|
|
#define LLVM_TARGET_TARGETREGISTRY_H
|
|
|
|
|
2009-07-25 06:49:55 +00:00
|
|
|
// FIXME: We shouldn't need this header, but we need it until there is a
|
|
|
|
// different interface to get the TargetAsmInfo.
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2009-07-15 04:24:58 +00:00
|
|
|
#include <string>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class FunctionPass;
|
|
|
|
class Module;
|
2009-07-17 20:42:00 +00:00
|
|
|
class TargetAsmParser;
|
2009-07-15 04:24:58 +00:00
|
|
|
class TargetMachine;
|
|
|
|
class formatted_raw_ostream;
|
|
|
|
|
|
|
|
/// Target - Wrapper for Target specific information.
|
|
|
|
///
|
|
|
|
/// For registration purposes, this is a POD type so that targets can be
|
|
|
|
/// registered without the use of static constructors.
|
2009-07-15 07:09:29 +00:00
|
|
|
///
|
|
|
|
/// Targets should implement a single global instance of this class (which
|
|
|
|
/// will be zero initialized), and pass that instance to the TargetRegistry as
|
|
|
|
/// part of their initialization.
|
|
|
|
class Target {
|
2009-07-15 04:24:58 +00:00
|
|
|
private:
|
|
|
|
typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
|
|
|
|
typedef unsigned (*ModuleMatchQualityFnTy)(const Module &M);
|
|
|
|
typedef unsigned (*JITMatchQualityFnTy)();
|
|
|
|
|
2009-07-15 20:24:03 +00:00
|
|
|
typedef TargetMachine *(*TargetMachineCtorTy)(const Target &,
|
|
|
|
const Module &,
|
2009-07-15 04:24:58 +00:00
|
|
|
const std::string &);
|
|
|
|
typedef FunctionPass *(*AsmPrinterCtorTy)(formatted_raw_ostream &,
|
|
|
|
TargetMachine &,
|
|
|
|
bool);
|
2009-07-17 20:42:00 +00:00
|
|
|
typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &);
|
2009-07-15 04:24:58 +00:00
|
|
|
|
2009-07-15 07:09:29 +00:00
|
|
|
friend struct TargetRegistry;
|
2009-07-15 04:24:58 +00:00
|
|
|
|
|
|
|
/// Next - The next registered target in the linked list, maintained by the
|
|
|
|
/// TargetRegistry.
|
|
|
|
Target *Next;
|
|
|
|
|
|
|
|
/// TripleMatchQualityFn - The target function for rating the match quality
|
|
|
|
/// 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;
|
|
|
|
|
|
|
|
/// TargetMachineCtorFn - Construction function for this target's
|
|
|
|
/// TargetMachine, if registered.
|
|
|
|
TargetMachineCtorTy TargetMachineCtorFn;
|
|
|
|
|
|
|
|
/// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
|
|
|
|
/// if registered.
|
|
|
|
AsmPrinterCtorTy AsmPrinterCtorFn;
|
|
|
|
|
2009-07-17 20:42:00 +00:00
|
|
|
/// AsmParserCtorFn - Construction function for this target's AsmParser,
|
|
|
|
/// if registered.
|
|
|
|
AsmParserCtorTy AsmParserCtorFn;
|
|
|
|
|
2009-07-15 04:24:58 +00:00
|
|
|
public:
|
2009-07-16 02:38:28 +00:00
|
|
|
// getNext - Return the next registered target.
|
|
|
|
const Target *getNext() const { return Next; }
|
|
|
|
|
2009-07-15 04:24:58 +00:00
|
|
|
/// 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; }
|
|
|
|
|
2009-07-15 20:24:03 +00:00
|
|
|
/// getJITMatchQuality - Get the quality of this targets match for use as a
|
|
|
|
/// JIT.
|
|
|
|
unsigned getJITMatchQuality() const { return JITMatchQualityFn(); }
|
|
|
|
|
2009-07-16 02:06:09 +00:00
|
|
|
/// 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; }
|
|
|
|
|
2009-07-17 20:42:00 +00:00
|
|
|
/// hasAsmParser - Check if this target supports .s parsing.
|
|
|
|
bool hasAsmParser() const { return AsmParserCtorFn != 0; }
|
|
|
|
|
2009-07-15 04:24:58 +00:00
|
|
|
/// createTargetMachine - Create a target specific machine implementation.
|
|
|
|
TargetMachine *createTargetMachine(const Module &M,
|
2009-07-15 20:24:03 +00:00
|
|
|
const std::string &Features) const {
|
2009-07-15 04:24:58 +00:00
|
|
|
if (!TargetMachineCtorFn)
|
|
|
|
return 0;
|
2009-07-15 20:24:03 +00:00
|
|
|
return TargetMachineCtorFn(*this, M, Features);
|
2009-07-15 04:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// createAsmPrinter - Create a target specific assembly printer pass.
|
|
|
|
FunctionPass *createAsmPrinter(formatted_raw_ostream &OS,
|
|
|
|
TargetMachine &M,
|
2009-07-15 20:24:03 +00:00
|
|
|
bool Verbose) const {
|
2009-07-15 04:24:58 +00:00
|
|
|
if (!AsmPrinterCtorFn)
|
|
|
|
return 0;
|
|
|
|
return AsmPrinterCtorFn(OS, M, Verbose);
|
|
|
|
}
|
2009-07-17 20:42:00 +00:00
|
|
|
|
|
|
|
/// createAsmParser - Create a target specific assembly parser.
|
|
|
|
TargetAsmParser *createAsmParser() const {
|
|
|
|
if (!AsmParserCtorFn)
|
|
|
|
return 0;
|
|
|
|
return AsmParserCtorFn(*this);
|
|
|
|
}
|
2009-07-15 04:24:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// TargetRegistry - Generic interface to target specific features.
|
|
|
|
//
|
|
|
|
// FIXME: Provide Target* iterator.
|
|
|
|
struct TargetRegistry {
|
2009-07-16 02:06:09 +00:00
|
|
|
class iterator {
|
|
|
|
const Target *Current;
|
|
|
|
explicit iterator(Target *T) : Current(T) {}
|
2009-07-18 23:22:46 +00:00
|
|
|
friend struct TargetRegistry;
|
2009-07-16 02:06:09 +00:00
|
|
|
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!");
|
2009-07-16 02:38:28 +00:00
|
|
|
Current = Current->getNext();
|
2009-07-16 02:06:09 +00:00
|
|
|
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*();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-07-15 04:24:58 +00:00
|
|
|
/// @name Registry Access
|
|
|
|
/// @{
|
|
|
|
|
2009-07-16 02:06:09 +00:00
|
|
|
static iterator begin();
|
|
|
|
|
|
|
|
static iterator end() { return iterator(); }
|
|
|
|
|
2009-07-15 04:24:58 +00:00
|
|
|
/// getClosestStaticTargetForTriple - Given a target triple, pick the most
|
|
|
|
/// capable target for that triple.
|
|
|
|
static const Target *getClosestStaticTargetForTriple(const std::string &TT,
|
|
|
|
std::string &Error);
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
static const Target *getClosestTargetForJIT(std::string &Error);
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Target Registration
|
|
|
|
/// @{
|
|
|
|
|
2009-07-15 20:24:03 +00:00
|
|
|
/// RegisterTarget - Register the given target. Attempts to register a
|
|
|
|
/// target which has already been registered will be ignored.
|
2009-07-15 07:09:29 +00:00
|
|
|
///
|
|
|
|
/// 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.
|
2009-07-15 04:24:58 +00:00
|
|
|
///
|
|
|
|
/// @param T - The target being registered.
|
|
|
|
/// @param Name - The target name. This should be a static string.
|
|
|
|
/// @param ShortDesc - A short target description. This should be a static
|
|
|
|
/// 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.
|
|
|
|
static void RegisterTarget(Target &T,
|
|
|
|
const char *Name,
|
|
|
|
const char *ShortDesc,
|
|
|
|
Target::TripleMatchQualityFnTy TQualityFn,
|
|
|
|
Target::ModuleMatchQualityFnTy MQualityFn,
|
|
|
|
Target::JITMatchQualityFnTy JITQualityFn);
|
|
|
|
|
|
|
|
/// RegisterTargetMachine - Register a TargetMachine implementation for the
|
|
|
|
/// given target.
|
|
|
|
///
|
2009-07-15 07:09:29 +00:00
|
|
|
/// 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.
|
|
|
|
///
|
2009-07-15 04:24:58 +00:00
|
|
|
/// @param T - The target being registered.
|
|
|
|
/// @param Fn - A function to construct a TargetMachine for the target.
|
|
|
|
static void RegisterTargetMachine(Target &T,
|
|
|
|
Target::TargetMachineCtorTy Fn) {
|
2009-07-17 20:42:00 +00:00
|
|
|
// Ignore duplicate registration.
|
|
|
|
if (!T.TargetMachineCtorFn)
|
|
|
|
T.TargetMachineCtorFn = Fn;
|
2009-07-15 04:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
|
|
|
|
/// target.
|
|
|
|
///
|
2009-07-15 07:09:29 +00:00
|
|
|
/// 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.
|
|
|
|
///
|
2009-07-15 04:24:58 +00:00
|
|
|
/// @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) {
|
2009-07-17 20:42:00 +00:00
|
|
|
// 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;
|
2009-07-15 04:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
};
|
|
|
|
|
2009-07-25 06:49:55 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// RegisterTarget - Helper template for registering a target, for use in the
|
|
|
|
/// target's initialization function. Usage:
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Target TheFooTarget; // The global target instance.
|
|
|
|
///
|
|
|
|
/// namespace {
|
|
|
|
/// struct FooInfo {
|
|
|
|
/// static unsigned getJITMatchQuality() { ... }
|
|
|
|
/// static unsigned getTripleMatchQuality(const std::string &) { ... }
|
|
|
|
/// static unsigned getModuleMatchQuality(const Module &) { ... }
|
|
|
|
/// };
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// extern "C" void LLVMInitializeFooTargetInfo() {
|
|
|
|
/// RegisterTarget<FooAsmPrinter> X(TheFooTarget, "foo", "Foo description");
|
|
|
|
/// }
|
|
|
|
template<class TargetInfoImpl>
|
|
|
|
struct RegisterTarget {
|
|
|
|
RegisterTarget(Target &T, const char *Name, const char *Desc) {
|
|
|
|
TargetRegistry::RegisterTarget(T, Name, Desc,
|
|
|
|
&TargetInfoImpl::getTripleMatchQuality,
|
|
|
|
&TargetInfoImpl::getModuleMatchQuality,
|
|
|
|
&TargetInfoImpl::getJITMatchQuality);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// 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 Module &M,
|
|
|
|
const std::string &FS) {
|
|
|
|
return new TargetMachineImpl(T, M, 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 FunctionPass *Allocator(formatted_raw_ostream &OS,
|
|
|
|
TargetMachine &TM,
|
|
|
|
bool Verbose) {
|
|
|
|
return new AsmPrinterImpl(OS, TM, TM.getTargetAsmInfo(), Verbose);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// RegisterAsmParser - Helper template for registering a target specific asm
|
|
|
|
/// parser, for use in the target machine initialization function. Usage:
|
|
|
|
///
|
|
|
|
/// extern "C" void LLVMInitializeFooAsmPrinter() {
|
|
|
|
/// extern Target TheFooTarget;
|
|
|
|
/// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
|
|
|
|
/// }
|
|
|
|
template<class AsmParserImpl>
|
|
|
|
struct RegisterAsmParser {
|
|
|
|
RegisterAsmParser(Target &T) {
|
|
|
|
TargetRegistry::RegisterAsmParser(T, &Allocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static TargetAsmParser *Allocator(const Target &T) {
|
|
|
|
return new AsmParserImpl(T);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-07-15 04:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|