From e0972b0793fcedce8381722bae7a3ae3735547e8 Mon Sep 17 00:00:00 2001 From: Tomas Lindquist Olsen Date: Mon, 1 Dec 2008 02:10:16 +0100 Subject: [PATCH] Fixed ModuleInfo generation to no longer use the ModuleInfo class' default initializer for types/defaults, it's unsafe as initializers don't necesarily match the "formal" type. There might be explicit padding. Changed -g switch to emit DW_LANG_D debug info, make demangling work with a patched GDB, still more work to do for full support of D's Dwarf extensions. Added getNullValue helper method. --- dmd/mars.c | 4 ++- dmd2/mars.c | 4 ++- gen/todebug.cpp | 5 +++- gen/tollvm.cpp | 5 ++++ gen/tollvm.h | 1 + gen/toobj.cpp | 67 ++++++++++++++++++++++++++----------------------- 6 files changed, 51 insertions(+), 35 deletions(-) diff --git a/dmd/mars.c b/dmd/mars.c index 5e842783..50f0f422 100644 --- a/dmd/mars.c +++ b/dmd/mars.c @@ -374,8 +374,10 @@ int main(int argc, char *argv[], char** envp) global.params.link = 0; else if (strcmp(p + 1, "fPIC") == 0) global.params.pic = 1; - else if (strcmp(p + 1, "g") == 0 || strcmp(p + 1, "gc") == 0) + else if (strcmp(p + 1, "g") == 0) global.params.symdebug = 1; + else if (strcmp(p + 1, "gc") == 0) + global.params.symdebug = 2; else if (strcmp(p + 1, "v") == 0) global.params.verbose = 1; else if (strcmp(p + 1, "vv") == 0) { diff --git a/dmd2/mars.c b/dmd2/mars.c index 05b01cf5..a4ee1524 100644 --- a/dmd2/mars.c +++ b/dmd2/mars.c @@ -391,8 +391,10 @@ int main(int argc, char *argv[], char** envp) global.params.link = 0; else if (strcmp(p + 1, "fPIC") == 0) global.params.pic = 1; - else if (strcmp(p + 1, "g") == 0 || strcmp(p + 1, "gc") == 0) + else if (strcmp(p + 1, "g") == 0) global.params.symdebug = 1; + else if (strcmp(p + 1, "gc") == 0) + global.params.symdebug = 2; else if (strcmp(p + 1, "v") == 0) global.params.verbose = 1; else if (strcmp(p + 1, "vv") == 0) { diff --git a/gen/todebug.cpp b/gen/todebug.cpp index 0fa4e21b..f244a487 100644 --- a/gen/todebug.cpp +++ b/gen/todebug.cpp @@ -134,7 +134,10 @@ static LLGlobalVariable* dwarfCompileUnit(Module* m) vals[0] = DBG_TAG(DW_TAG_compile_unit); vals[1] = DBG_CAST(getDwarfAnchor(DW_TAG_compile_unit)); - vals[2] = DtoConstUint(DW_LANG_C);// _D)); // doesn't seem to work + if (global.params.symdebug == 2) + vals[2] = DtoConstUint(DW_LANG_C); + else + vals[2] = DtoConstUint(DW_LANG_D); vals[3] = DtoConstStringPtr(FileName::name(m->srcfile->name->toChars()), "llvm.metadata"); std::string srcpath(FileName::path(m->srcfile->name->toChars())); if (!FileName::absolute(srcpath.c_str())) { diff --git a/gen/tollvm.cpp b/gen/tollvm.cpp index 9d2678f9..a499dfff 100644 --- a/gen/tollvm.cpp +++ b/gen/tollvm.cpp @@ -673,6 +673,11 @@ llvm::ConstantPointerNull* getNullPtr(const LLType* t) return llvm::ConstantPointerNull::get(pt); } +LLConstant* getNullValue(const LLType* t) +{ + return LLConstant::getNullValue(t); +} + ////////////////////////////////////////////////////////////////////////////////////////// size_t getTypeBitSize(const LLType* t) diff --git a/gen/tollvm.h b/gen/tollvm.h index 621e88b5..ad407b86 100644 --- a/gen/tollvm.h +++ b/gen/tollvm.h @@ -87,6 +87,7 @@ LLGlobalVariable* isaGlobalVar(LLValue* v); const LLPointerType* getPtrToType(const LLType* t); const LLPointerType* getVoidPtrType(); llvm::ConstantPointerNull* getNullPtr(const LLType* t); +LLConstant* getNullValue(const LLType* t); // type sizes size_t getTypeBitSize(const LLType* t); diff --git a/gen/toobj.cpp b/gen/toobj.cpp index 8b1c83df..77a04e96 100644 --- a/gen/toobj.cpp +++ b/gen/toobj.cpp @@ -679,18 +679,20 @@ static LLFunction* build_module_reference_and_ctor(LLConstant* moduleinfo) void Module::genmoduleinfo() { -// The layout is: -// { -// void **vptr; -// monitor_t monitor; -// char[] name; // class name -// ModuleInfo importedModules[]; -// ClassInfo localClasses[]; -// uint flags; // initialization state -// void *ctor; -// void *dtor; -// void *unitTest; -// } +// The layout is: +// { +// char[] name; +// ModuleInfo[] importedModules; +// ClassInfo[] localClasses; +// uint flags; +// +// void function() ctor; +// void function() dtor; +// void function() unitTest; +// +// void* xgetMembers; +// void function() ictor; +// } // resolve ModuleInfo assert(moduleinfo); @@ -705,7 +707,6 @@ void Module::genmoduleinfo() // moduleinfo llvm struct type const llvm::StructType* moduleinfoTy = isaStruct(moduleinfo->type->ir.type->get()); - // classinfo llvm struct type const llvm::StructType* classinfoTy = isaStruct(ClassDeclaration::classinfo->type->ir.type->get()); @@ -733,16 +734,15 @@ void Module::genmoduleinfo() { Module *m = (Module *)aimports.data[i]; if (!m->needModuleInfo()) - aimports_dim--; - else { // declare - // create name - std::string m_name("_D"); - m_name.append(m->mangle()); - m_name.append("8__ModuleZ"); - llvm::GlobalVariable* m_gvar = gIR->module->getGlobalVariable(m_name); - if (!m_gvar) m_gvar = new llvm::GlobalVariable(moduleinfoTy, false, llvm::GlobalValue::ExternalLinkage, NULL, m_name, gIR->module); - importInits.push_back(m_gvar); - } + continue; + + // declare the imported module info + std::string m_name("_D"); + m_name.append(m->mangle()); + m_name.append("8__ModuleZ"); + llvm::GlobalVariable* m_gvar = gIR->module->getGlobalVariable(m_name); + if (!m_gvar) m_gvar = new llvm::GlobalVariable(moduleinfoTy, false, llvm::GlobalValue::ExternalLinkage, NULL, m_name, gIR->module); + importInits.push_back(m_gvar); } // has import array? if (!importInits.empty()) @@ -758,7 +758,7 @@ void Module::genmoduleinfo() c = DtoConstSlice(DtoConstSize_t(importInits.size()), c); } else - c = moduleinfo->ir.irStruct->constInit->getOperand(3); + c = DtoConstSlice( DtoConstSize_t(0), getNullValue(getPtrToType(moduleinfoTy)) ); initVec.push_back(c); // localClasses[] @@ -789,7 +789,7 @@ void Module::genmoduleinfo() } Logger::println("class: %s", cd->toPrettyChars()); assert(cd->ir.irStruct->classInfo); - c = llvm::ConstantExpr::getBitCast(cd->ir.irStruct->classInfo, getPtrToType(classinfoTy)); + c = DtoBitCast(cd->ir.irStruct->classInfo, getPtrToType(classinfoTy)); classInits.push_back(c); } // has class array? @@ -802,11 +802,11 @@ void Module::genmoduleinfo() m_name.append("9__classesZ"); assert(gIR->module->getGlobalVariable(m_name) == NULL); llvm::GlobalVariable* m_gvar = new llvm::GlobalVariable(classArrTy, true, llvm::GlobalValue::InternalLinkage, c, m_name, gIR->module); - c = llvm::ConstantExpr::getBitCast(m_gvar, getPtrToType(classArrTy->getElementType())); + c = DtoBitCast(m_gvar, getPtrToType(classinfoTy)); c = DtoConstSlice(DtoConstSize_t(classInits.size()), c); } else - c = moduleinfo->ir.irStruct->constInit->getOperand(4); + c = DtoConstSlice( DtoConstSize_t(0), getNullValue(getPtrToType(classinfoTy)) ); initVec.push_back(c); // flags @@ -815,27 +815,30 @@ void Module::genmoduleinfo() c = DtoConstUint(4); // flags (4 means MIstandalone) initVec.push_back(c); + // function pointer type for next three fields + const LLType* fnptrTy = getPtrToType(LLFunctionType::get(LLType::VoidTy, std::vector(), false)); + // ctor llvm::Function* fctor = build_module_ctor(); - c = fctor ? fctor : moduleinfo->ir.irStruct->constInit->getOperand(6); + c = fctor ? fctor : getNullValue(fnptrTy); initVec.push_back(c); // dtor llvm::Function* fdtor = build_module_dtor(); - c = fdtor ? fdtor : moduleinfo->ir.irStruct->constInit->getOperand(7); + c = fdtor ? fdtor : getNullValue(fnptrTy); initVec.push_back(c); // unitTest llvm::Function* unittest = build_module_unittest(); - c = unittest ? unittest : moduleinfo->ir.irStruct->constInit->getOperand(8); + c = unittest ? unittest : getNullValue(fnptrTy); initVec.push_back(c); // xgetMembers - c = moduleinfo->ir.irStruct->constInit->getOperand(9); + c = getNullValue(getVoidPtrType()); initVec.push_back(c); // ictor - c = moduleinfo->ir.irStruct->constInit->getOperand(10); + c = getNullValue(fnptrTy); initVec.push_back(c); /*Logger::println("MODULE INFO INITIALIZERS");