MC: Sketch registry support for target specific assembler backends.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@96762 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar 2010-02-21 21:53:53 +00:00
parent 24b5425072
commit e7bd886cb8
3 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,35 @@
//===-- llvm/Target/TargetAsmBackend.h - Target Asm Backend -----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TARGET_TARGETASMBACKEND_H
#define LLVM_TARGET_TARGETASMBACKEND_H
namespace llvm {
class Target;
/// TargetAsmBackend - Generic interface to target specific assembler backends.
class TargetAsmBackend {
TargetAsmBackend(const TargetAsmBackend &); // DO NOT IMPLEMENT
void operator=(const TargetAsmBackend &); // DO NOT IMPLEMENT
protected: // Can only create subclasses.
TargetAsmBackend(const Target &);
/// TheTarget - The Target that this machine was created for.
const Target &TheTarget;
public:
virtual ~TargetAsmBackend();
const Target &getTarget() const { return TheTarget; }
};
} // End llvm namespace
#endif

View File

@ -26,6 +26,7 @@
namespace llvm {
class AsmPrinter;
class Module;
class MCAssembler;
class MCAsmInfo;
class MCAsmParser;
class MCCodeEmitter;
@ -33,6 +34,7 @@ namespace llvm {
class MCDisassembler;
class MCInstPrinter;
class MCStreamer;
class TargetAsmBackend;
class TargetAsmLexer;
class TargetAsmParser;
class TargetMachine;
@ -63,6 +65,8 @@ namespace llvm {
MCContext &Ctx,
MCStreamer &Streamer,
const MCAsmInfo *MAI);
typedef TargetAsmBackend *(*AsmBackendCtorTy)(const Target &T,
MCAssembler &A);
typedef TargetAsmLexer *(*AsmLexerCtorTy)(const Target &T,
const MCAsmInfo &MAI);
typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &T,MCAsmParser &P);
@ -99,6 +103,10 @@ namespace llvm {
/// TargetMachine, if registered.
TargetMachineCtorTy TargetMachineCtorFn;
/// AsmBackendCtorFn - Construction function for this target's
/// TargetAsmBackend, if registered.
AsmBackendCtorTy AsmBackendCtorFn;
/// AsmLexerCtorFn - Construction function for this target's TargetAsmLexer,
/// if registered.
AsmLexerCtorTy AsmLexerCtorFn;
@ -146,6 +154,9 @@ namespace llvm {
/// hasTargetMachine - Check if this target supports code generation.
bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
/// hasAsmBackend - Check if this target supports .o generation.
bool hasAsmBackend() const { return AsmBackendCtorFn != 0; }
/// hasAsmLexer - Check if this target supports .s lexing.
bool hasAsmLexer() const { return AsmLexerCtorFn != 0; }
@ -195,6 +206,15 @@ namespace llvm {
return TargetMachineCtorFn(*this, Triple, Features);
}
/// createAsmBackend - Create a target specific assembly parser.
///
/// \arg Backend - The target independent assembler object.
TargetAsmBackend *createAsmBackend(MCAssembler &Backend) const {
if (!AsmBackendCtorFn)
return 0;
return AsmBackendCtorFn(*this, Backend);
}
/// createAsmLexer - Create a target specific assembly lexer.
///
TargetAsmLexer *createAsmLexer(const MCAsmInfo &MAI) const {
@ -365,6 +385,20 @@ namespace llvm {
T.TargetMachineCtorFn = Fn;
}
/// RegisterAsmBackend - Register a TargetAsmBackend 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 AsmBackend for the target.
static void RegisterAsmBackend(Target &T, Target::AsmBackendCtorTy Fn) {
if (!T.AsmBackendCtorFn)
T.AsmBackendCtorFn = Fn;
}
/// RegisterAsmLexer - Register a TargetAsmLexer implementation for the
/// given target.
///
@ -539,6 +573,25 @@ namespace llvm {
}
};
/// RegisterAsmBackend - Helper template for registering a target specific
/// assembler backend. Usage:
///
/// extern "C" void LLVMInitializeFooAsmBackend() {
/// extern Target TheFooTarget;
/// RegisterAsmBackend<FooAsmLexer> X(TheFooTarget);
/// }
template<class AsmBackendImpl>
struct RegisterAsmBackend {
RegisterAsmBackend(Target &T) {
TargetRegistry::RegisterAsmBackend(T, &Allocator);
}
private:
static TargetAsmBackend *Allocator(const Target &T, MCAssembler &Backend) {
return new AsmBackendImpl(T, Backend);
}
};
/// RegisterAsmLexer - Helper template for registering a target specific
/// assembly lexer, for use in the target machine initialization
/// function. Usage:

View File

@ -0,0 +1,19 @@
//===-- TargetAsmBackend.cpp - Target Assembly Backend ---------------------==//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Target/TargetAsmBackend.h"
using namespace llvm;
TargetAsmBackend::TargetAsmBackend(const Target &T)
: TheTarget(T)
{
}
TargetAsmBackend::~TargetAsmBackend() {
}