diff --git a/dmd/template.c b/dmd/template.c index fd39101b..9f1e6729 100644 --- a/dmd/template.c +++ b/dmd/template.c @@ -2875,6 +2875,7 @@ TemplateInstance::TemplateInstance(Loc loc, Identifier *ident) this->errors = 0; // LDC + this->emittedInModule = NULL; this->tinst = NULL; this->tmodule = NULL; } diff --git a/dmd/template.h b/dmd/template.h index 7c022828..6e6ae81b 100644 --- a/dmd/template.h +++ b/dmd/template.h @@ -327,6 +327,7 @@ struct TemplateInstance : ScopeDsymbol // LDC TemplateInstance *tinst; // enclosing template instance Module* tmodule; // module from outermost enclosing template instantiation + Module* emittedInModule; // which module this template instance has been emitted in void printInstantiationTrace(); }; diff --git a/gen/cl_options.cpp b/gen/cl_options.cpp index c8870328..75cdde02 100644 --- a/gen/cl_options.cpp +++ b/gen/cl_options.cpp @@ -326,6 +326,11 @@ static cl::opt > release("release", cl::ValueDisallowed); +cl::opt singleObj("singleobj", + cl::desc("Create only a single output object file"), + cl::ZeroOrMore); + + static cl::extrahelp footer("\n" "-d-debug can also be specified without options, in which case it enables all\n" "debug checks (i.e. (asserts, boundchecks, contracts and invariants) as well\n" diff --git a/gen/cl_options.h b/gen/cl_options.h index 32dcbeb0..c7a8b67d 100644 --- a/gen/cl_options.h +++ b/gen/cl_options.h @@ -43,6 +43,7 @@ namespace opts { extern cl::opt mCPU; extern cl::list mAttrs; extern cl::opt mTargetTriple; + extern cl::opt singleObj; // Arguments to -d-debug extern std::vector debugArgs; diff --git a/gen/llvmhelpers.cpp b/gen/llvmhelpers.cpp index 07a0206e..76f0de18 100644 --- a/gen/llvmhelpers.cpp +++ b/gen/llvmhelpers.cpp @@ -20,6 +20,7 @@ #include "gen/functions.h" #include "gen/typeinf.h" #include "gen/todebug.h" +#include "gen/cl_options.h" #include "ir/irmodule.h" #include @@ -833,11 +834,11 @@ DValue* DtoPaintType(Loc& loc, DValue* val, Type* to) // TEMPLATE HELPERS ////////////////////////////////////////////////////////////////////////////////////////*/ -Module* DtoIsTemplateInstance(Dsymbol* s) +TemplateInstance* DtoIsTemplateInstance(Dsymbol* s) { if (!s) return NULL; if (s->isTemplateInstance() && !s->isTemplateMixin()) - return s->isTemplateInstance()->tmodule; + return s->isTemplateInstance(); else if (s->parent) return DtoIsTemplateInstance(s->parent); return NULL; @@ -1536,34 +1537,25 @@ void DtoOverloadedIntrinsicName(TemplateInstance* ti, TemplateDeclaration* td, s bool mustDefineSymbol(Dsymbol* s) { -#if 1 - return s->getModule() == gIR->dmodule || DtoIsTemplateInstance(s) != NULL; -#else - Module* M = DtoIsTemplateInstance(s); - // if it's a template instance, check the instantiating module - // not the module that defines the template - if (M) { - //Logger::println("TINST %s from %s cur %s", s->toPrettyChars(), M->toChars(), gIR->dmodule->toChars()); - return M == gIR->dmodule; + TemplateInstance* tinst = DtoIsTemplateInstance(s); + if (tinst) + { + if (!opts::singleObj) + return true; + + if (!tinst->emittedInModule) + tinst->emittedInModule = gIR->dmodule; + return tinst->emittedInModule == gIR->dmodule; } - return s->getCompilationModule() == gIR->dmodule; -#endif + + return s->getModule() == gIR->dmodule; } ////////////////////////////////////////////////////////////////////////////////////////// bool needsTemplateLinkage(Dsymbol* s) { -#if 1 - return DtoIsTemplateInstance(s) != NULL; -#else - Module* M = DtoIsTemplateInstance(s); - // only return true if the symbol is a template instances - // and if this instance originated in the current module - if (M) - return M == gIR->dmodule; - return false; -#endif + return DtoIsTemplateInstance(s) && mustDefineSymbol(s); } ////////////////////////////////////////////////////////////////////////////////////////// diff --git a/gen/llvmhelpers.h b/gen/llvmhelpers.h index 3cf5b47d..5c4e92bd 100644 --- a/gen/llvmhelpers.h +++ b/gen/llvmhelpers.h @@ -62,7 +62,7 @@ DValue* DtoCast(Loc& loc, DValue* val, Type* to); DValue* DtoPaintType(Loc& loc, DValue* val, Type* to); // is template instance check, returns module where instantiated -Module* DtoIsTemplateInstance(Dsymbol* s); +TemplateInstance* DtoIsTemplateInstance(Dsymbol* s); // these are all basically drivers for the codegeneration called by the main loop void DtoResolveDsymbol(Dsymbol* dsym); diff --git a/gen/main.cpp b/gen/main.cpp index 6a5495d7..779cad42 100644 --- a/gen/main.cpp +++ b/gen/main.cpp @@ -43,10 +43,6 @@ extern void getenv_setargv(const char *envvar, int *pargc, char** *pargv); extern void backend_init(); extern void backend_term(); -static cl::opt singleObj("singleobj", - cl::desc("Create only a single output object file"), - cl::ZeroOrMore); - static cl::opt noDefaultLib("nodefaultlib", cl::desc("Don't add a default library for linking implicitly"), cl::ZeroOrMore); diff --git a/gen/todebug.cpp b/gen/todebug.cpp index 5567e7a7..1d1fb715 100644 --- a/gen/todebug.cpp +++ b/gen/todebug.cpp @@ -503,6 +503,12 @@ llvm::DICompileUnit DtoDwarfCompileUnit(Module* m) false, // isMain, false // isOptimized ); + + // if the linkage stays internal, we can't llvm-link the generated modules together: + // llvm's DwarfWriter uses path and filename to determine the symbol name and we'd + // end up with duplicate symbols + m->ir.irModule->diCompileUnit.getGV()->setLinkage(DEBUGINFO_LINKONCE_LINKAGE_TYPE); + m->ir.irModule->diCompileUnit.getGV()->setName(std::string("llvm.dbg.compile_unit_") + srcpath + m->srcfile->name->toChars()); return m->ir.irModule->diCompileUnit; }