- RegisterMCAsmLexer<X86AsmLexer> X(TheX86_32Target);
这个函数里面有个Allocator的赋值,相关的C++技术模拟代码如下,用VC2010编译成功并运行:
// CppTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
class Target;
class MCRegisterInfo {
};
class MCAsmLexer {
};
class MCAsmInfo {
};
class MCTargetAsmLexer {
// MCTargetAsmLexer(const MCTargetAsmLexer &); // DO NOT IMPLEMENT
// void operator=(const MCTargetAsmLexer &); // DO NOT IMPLEMENT
protected: // Can only create subclasses.
MCTargetAsmLexer(const Target &T)
: TheTarget(T), Lexer(NULL) {
}
/// TheTarget - The Target that this machine was created for.
const Target &TheTarget;
MCAsmLexer *Lexer;
};
class Target
{
public:
char a;
typedef MCTargetAsmLexer *(*MCAsmLexerCtorTy)(const Target &T,
const MCRegisterInfo &MRI,
const MCAsmInfo &MAI);
MCAsmLexerCtorTy MCAsmLexerCtorFn;
};
struct TargetRegistry {
static void RegisterMCAsmLexer(Target &T, Target::MCAsmLexerCtorTy Fn) {
if (!T.MCAsmLexerCtorFn)
T.MCAsmLexerCtorFn = Fn;
}
};
template<class MCAsmLexerImpl>
struct RegisterMCAsmLexer {
RegisterMCAsmLexer(Target &T) {
TargetRegistry::RegisterMCAsmLexer(T, &Allocator);
}
private:
static MCTargetAsmLexer *Allocator(const Target &T,
const MCRegisterInfo &MRI,
const MCAsmInfo &MAI) {
return new MCAsmLexerImpl(T, MRI, MAI);
}
};
class X86AsmLexer : public MCTargetAsmLexer {
const MCAsmInfo &AsmInfo;
bool tentativeIsValid;
public:
X86AsmLexer(const Target &T, const MCRegisterInfo &MRI, const MCAsmInfo &MAI)
: MCTargetAsmLexer(T), AsmInfo(MAI), tentativeIsValid(false) {
}
};
Target TheX86_32Target;
int _tmain(int argc, _TCHAR* argv[])
{
RegisterMCAsmLexer<X86AsmLexer> X(TheX86_32Target);
return 0;
}