Increment vgates in static constructors. That fixes static destructors of templates.

This commit is contained in:
Alexey Prokhin 2011-01-02 17:43:02 +03:00
parent a86f414bc1
commit 8a49067776
4 changed files with 39 additions and 13 deletions

View file

@ -509,9 +509,11 @@ void DtoDeclareFunction(FuncDeclaration* fdecl)
}
}
// shared static dtor
else if (fdecl->isSharedStaticDtorDeclaration()) {
else if (StaticDtorDeclaration *dtorDecl = fdecl->isSharedStaticDtorDeclaration()) {
if (mustDefineSymbol(fdecl)) {
gIR->sharedDtors.push_front(fdecl);
if (dtorDecl->vgate)
gIR->sharedGates.push_front(dtorDecl->vgate);
}
} else
#endif
@ -522,9 +524,13 @@ void DtoDeclareFunction(FuncDeclaration* fdecl)
}
}
// static dtor
else if (fdecl->isStaticDtorDeclaration()) {
else if (StaticDtorDeclaration *dtorDecl = fdecl->isStaticDtorDeclaration()) {
if (mustDefineSymbol(fdecl)) {
gIR->dtors.insert(gIR->dtors.begin(), fdecl);
gIR->dtors.push_front(fdecl);
#if DMDV2
if (dtorDecl->vgate)
gIR->gates.push_front(dtorDecl->vgate);
#endif
}
}

View file

@ -161,11 +161,14 @@ struct IRState
// static ctors/dtors/unittests
typedef std::list<FuncDeclaration*> FuncDeclList;
typedef std::list<VarDeclaration*> GatesList;
FuncDeclList ctors;
FuncDeclList dtors;
#if DMDV2
FuncDeclList sharedCtors;
FuncDeclList sharedDtors;
GatesList gates;
GatesList sharedGates;
#endif
FuncDeclList unitTests;

View file

@ -388,14 +388,16 @@ void assemble(const llvm::sys::Path& asmpath, const llvm::sys::Path& objpath)
/* ================================================================== */
static llvm::Function* build_module_function(const std::string &name, const std::list<FuncDeclaration*> &funcs)
static llvm::Function* build_module_function(const std::string &name, const std::list<FuncDeclaration*> &funcs,
const std::list<VarDeclaration*> &gates = std::list<VarDeclaration*>())
{
if (gates.empty()) {
if (funcs.empty())
return NULL;
size_t n = funcs.size();
if (n == 1)
if (funcs.size() == 1)
return funcs.front()->ir.irFunc->func;
}
std::vector<const LLType*> argsTy;
const llvm::FunctionType* fnTy = llvm::FunctionType::get(LLType::getVoidTy(gIR->context()),argsTy,false);
@ -412,13 +414,24 @@ static llvm::Function* build_module_function(const std::string &name, const std:
DtoDwarfSubProgramInternal(name.c_str(), name.c_str());
#endif
typedef std::list<FuncDeclaration*>::const_iterator Iterator;
for (Iterator itr = funcs.begin(), end = funcs.end(); itr != end; ++itr) {
// Call ctor's
typedef std::list<FuncDeclaration*>::const_iterator FuncIterator;
for (FuncIterator itr = funcs.begin(), end = funcs.end(); itr != end; ++itr) {
llvm::Function* f = (*itr)->ir.irFunc->func;
llvm::CallInst* call = builder.CreateCall(f,"");
call->setCallingConv(DtoCallingConv(0, LINKd));
}
// Increment vgate's
typedef std::list<VarDeclaration*>::const_iterator GatesIterator;
for (GatesIterator itr = gates.begin(), end = gates.end(); itr != end; ++itr) {
assert((*itr)->ir.irGlobal);
llvm::Value* val = (*itr)->ir.irGlobal->value;
llvm::Value* rval = builder.CreateLoad(val, "vgate");
llvm::Value* res = builder.CreateAdd(rval, DtoConstUint(1), "vgate");
builder.CreateStore(res, val);
}
builder.CreateRetVoid();
return fn;
}
@ -430,7 +443,11 @@ llvm::Function* build_module_ctor()
std::string name("_D");
name.append(gIR->dmodule->mangle());
name.append("6__ctorZ");
#if DMDV2
return build_module_function(name, gIR->ctors, gIR->gates);
#else
return build_module_function(name, gIR->ctors);
#endif
}
// build module dtor
@ -462,7 +479,7 @@ llvm::Function* build_module_shared_ctor()
std::string name("_D");
name.append(gIR->dmodule->mangle());
name.append("13__shared_ctorZ");
return build_module_function(name, gIR->sharedCtors);
return build_module_function(name, gIR->sharedCtors, gIR->sharedGates);
}
// build module shared dtor