mirror of
https://github.com/xomboverlord/ldc.git
synced 2026-08-02 04:20:05 +02:00
[svn r121] Finished ModuleInfo implementation.
Static ctors/dtors now work according to spec. Changed class vtable types slightly in some cases. Overridden functions now always take the the type of the first class declaring the method as this parameter. This helps when using headers (w. implementation somewhere else)
This commit is contained in:
@@ -167,10 +167,12 @@ void DtoResolveClass(ClassDeclaration* cd)
|
||||
|
||||
if (FuncDeclaration* fd = dsym->isFuncDeclaration()) {
|
||||
DtoResolveFunction(fd);
|
||||
assert(fd->type->ty == Tfunction);
|
||||
TypeFunction* tf = (TypeFunction*)fd->type;
|
||||
const llvm::Type* fpty = llvm::PointerType::get(tf->llvmType->get());
|
||||
sinits_ty.push_back(fpty);
|
||||
//assert(fd->type->ty == Tfunction);
|
||||
//TypeFunction* tf = (TypeFunction*)fd->type;
|
||||
//const llvm::Type* fpty = llvm::PointerType::get(tf->llvmType->get());
|
||||
const llvm::FunctionType* vfty = DtoBaseFunctionType(fd);
|
||||
const llvm::Type* vfpty = llvm::PointerType::get(vfty);
|
||||
sinits_ty.push_back(vfpty);
|
||||
}
|
||||
else if (ClassDeclaration* cd2 = dsym->isClassDeclaration()) {
|
||||
Logger::println("*** ClassDeclaration in vtable: %s", cd2->toChars());
|
||||
@@ -375,6 +377,7 @@ void DtoConstInitClass(ClassDeclaration* cd)
|
||||
assert(cd->type->ty == Tclass);
|
||||
TypeClass* ts = (TypeClass*)cd->type;
|
||||
const llvm::StructType* structtype = isaStruct(ts->llvmType->get());
|
||||
const llvm::StructType* vtbltype = isaStruct(ts->llvmVtblType->get());
|
||||
|
||||
// generate initializer
|
||||
#if 0
|
||||
@@ -406,6 +409,9 @@ void DtoConstInitClass(ClassDeclaration* cd)
|
||||
DtoForceDeclareDsymbol(fd);
|
||||
assert(fd->llvmValue);
|
||||
llvm::Constant* c = llvm::cast<llvm::Constant>(fd->llvmValue);
|
||||
// cast if necessary (overridden method)
|
||||
if (c->getType() != vtbltype->getElementType(k))
|
||||
c = llvm::ConstantExpr::getBitCast(c, vtbltype->getElementType(k));
|
||||
sinits.push_back(c);
|
||||
}
|
||||
else if (ClassDeclaration* cd2 = dsym->isClassDeclaration()) {
|
||||
@@ -1037,8 +1043,8 @@ void DtoDefineClassInfo(ClassDeclaration* cd)
|
||||
if (cd->defaultCtor && !cd->isInterfaceDeclaration()) {
|
||||
DtoForceDeclareDsymbol(cd->defaultCtor);
|
||||
c = isaConstant(cd->defaultCtor->llvmValue);
|
||||
//const llvm::Type* toTy = cinfo->llvmInitZ->getOperand(11)->getType();
|
||||
c = llvm::ConstantExpr::getBitCast(c, llvm::PointerType::get(llvm::Type::Int8Ty)); // toTy);
|
||||
const llvm::Type* toTy = cinfo->llvmInitZ->getOperand(12)->getType();
|
||||
c = llvm::ConstantExpr::getBitCast(c, toTy);
|
||||
}
|
||||
else {
|
||||
c = cinfo->llvmInitZ->getOperand(12);
|
||||
|
||||
@@ -468,23 +468,6 @@ void DtoDefineFunc(FuncDeclaration* fd)
|
||||
{
|
||||
fd->llvmDModule = gIR->dmodule;
|
||||
|
||||
// handle static constructor / destructor
|
||||
if (fd->isStaticCtorDeclaration() || fd->isStaticDtorDeclaration()) {
|
||||
const llvm::ArrayType* sctor_type = llvm::ArrayType::get(llvm::PointerType::get(functype),1);
|
||||
//Logger::cout() << "static ctor type: " << *sctor_type << '\n';
|
||||
|
||||
llvm::Constant* sctor_func = llvm::cast<llvm::Constant>(fd->llvmValue);
|
||||
//Logger::cout() << "static ctor func: " << *sctor_func << '\n';
|
||||
|
||||
llvm::Constant* sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_func,1);
|
||||
|
||||
//Logger::cout() << "static ctor init: " << *sctor_init << '\n';
|
||||
|
||||
// output the llvm.global_ctors array
|
||||
const char* varname = fd->isStaticCtorDeclaration() ? "_d_module_ctor_array" : "_d_module_dtor_array";
|
||||
llvm::GlobalVariable* sctor_arr = new llvm::GlobalVariable(sctor_type, false, llvm::GlobalValue::AppendingLinkage, sctor_init, varname, gIR->module);
|
||||
}
|
||||
|
||||
// function definition
|
||||
if (fd->fbody != 0)
|
||||
{
|
||||
@@ -492,18 +475,6 @@ void DtoDefineFunc(FuncDeclaration* fd)
|
||||
assert(fd->llvmIRFunc);
|
||||
gIR->functions.push_back(fd->llvmIRFunc);
|
||||
|
||||
/* // moved to declaration
|
||||
// this handling
|
||||
if (f->llvmUsesThis) {
|
||||
Logger::println("uses this");
|
||||
if (f->llvmRetInPtr)
|
||||
fd->llvmThisVar = ++func->arg_begin();
|
||||
else
|
||||
fd->llvmThisVar = func->arg_begin();
|
||||
assert(fd->llvmThisVar != 0);
|
||||
}
|
||||
*/
|
||||
|
||||
if (fd->isMain())
|
||||
gIR->emitMain = true;
|
||||
|
||||
@@ -681,7 +652,7 @@ void DtoMain()
|
||||
llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func);
|
||||
|
||||
// call static ctors
|
||||
llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_ctors");
|
||||
llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleCtor");
|
||||
llvm::Instruction* apt = new llvm::CallInst(fn,"",bb);
|
||||
|
||||
// call user main function
|
||||
@@ -720,7 +691,7 @@ void DtoMain()
|
||||
call->setCallingConv(ir.mainFunc->getCallingConv());
|
||||
|
||||
// call static dtors
|
||||
fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_dtors");
|
||||
fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleDtor");
|
||||
new llvm::CallInst(fn,"",bb);
|
||||
|
||||
// return
|
||||
@@ -728,3 +699,31 @@ void DtoMain()
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const llvm::FunctionType* DtoBaseFunctionType(FuncDeclaration* fdecl)
|
||||
{
|
||||
Dsymbol* parent = fdecl->toParent();
|
||||
ClassDeclaration* cd = parent->isClassDeclaration();
|
||||
assert(cd);
|
||||
|
||||
FuncDeclaration* f = fdecl;
|
||||
|
||||
while (cd)
|
||||
{
|
||||
ClassDeclaration* base = cd->baseClass;
|
||||
if (!base)
|
||||
break;
|
||||
FuncDeclaration* f2 = base->findFunc(fdecl->ident, (TypeFunction*)fdecl->type);
|
||||
if (f2) {
|
||||
f = f2;
|
||||
cd = base;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
DtoResolveDsymbol(f);
|
||||
return llvm::cast<llvm::FunctionType>(DtoType(f->type));
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
const llvm::FunctionType* DtoFunctionType(Type* t, const llvm::Type* thistype, bool ismain = false);
|
||||
const llvm::FunctionType* DtoFunctionType(FuncDeclaration* fdecl);
|
||||
|
||||
const llvm::FunctionType* DtoBaseFunctionType(FuncDeclaration* fdecl);
|
||||
|
||||
void DtoResolveFunction(FuncDeclaration* fdecl);
|
||||
void DtoDeclareFunction(FuncDeclaration* fdecl);
|
||||
void DtoDefineFunc(FuncDeclaration* fd);
|
||||
|
||||
@@ -123,6 +123,7 @@ Module::genobjfile()
|
||||
if (!global.params.novalidate) {
|
||||
std::string verifyErr;
|
||||
Logger::println("Verifying module...");
|
||||
LOG_SCOPE;
|
||||
if (llvm::verifyModule(*ir.module,llvm::ReturnStatusAction,&verifyErr))
|
||||
{
|
||||
error("%s", verifyErr.c_str());
|
||||
@@ -364,6 +365,14 @@ void Module::genmoduleinfo()
|
||||
// declare
|
||||
// flags will be modified at runtime so can't make it constant
|
||||
llvm::GlobalVariable* gvar = new llvm::GlobalVariable(moduleinfoTy, false, llvm::GlobalValue::ExternalLinkage, constMI, MIname, gIR->module);
|
||||
|
||||
// declare the appending array
|
||||
const llvm::ArrayType* appendArrTy = llvm::ArrayType::get(llvm::PointerType::get(llvm::Type::Int8Ty), 1);
|
||||
std::vector<llvm::Constant*> appendInits;
|
||||
appendInits.push_back(llvm::ConstantExpr::getBitCast(gvar, llvm::PointerType::get(llvm::Type::Int8Ty)));
|
||||
llvm::Constant* appendInit = llvm::ConstantArray::get(appendArrTy, appendInits);
|
||||
std::string appendName("_d_moduleinfo_array");
|
||||
llvm::GlobalVariable* appendVar = new llvm::GlobalVariable(appendArrTy, true, llvm::GlobalValue::AppendingLinkage, appendInit, appendName, gIR->module);
|
||||
}
|
||||
|
||||
/* ================================================================== */
|
||||
|
||||
Reference in New Issue
Block a user