diff --git a/dmd/func.c b/dmd/func.c index 9414d978..a18651ea 100644 --- a/dmd/func.c +++ b/dmd/func.c @@ -2566,7 +2566,7 @@ void StaticDtorDeclaration::semantic(Scope *sc) Statement *s = new DeclarationStatement(0, v); sa->push(s); Expression *e = new IdentifierExp(0, id); - e = new AddAssignExp(0, e, new IntegerExp(-1)); + e = new AddAssignExp(0, e, new IntegerExp((uint64_t)-1)); e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(0)); s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL); sa->push(s); diff --git a/dmd2/func.c b/dmd2/func.c index 62967637..1b468986 100644 --- a/dmd2/func.c +++ b/dmd2/func.c @@ -2722,7 +2722,7 @@ void StaticDtorDeclaration::semantic(Scope *sc) Statement *s = new DeclarationStatement(0, v); sa->push(s); Expression *e = new IdentifierExp(0, id); - e = new AddAssignExp(0, e, new IntegerExp(-1)); + e = new AddAssignExp(0, e, new IntegerExp((uint64_t)-1)); e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(0)); s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL); sa->push(s); diff --git a/gen/arrays.cpp b/gen/arrays.cpp index c310abee..43645f73 100644 --- a/gen/arrays.cpp +++ b/gen/arrays.cpp @@ -290,7 +290,7 @@ LLConstant* DtoConstArrayInitializer(ArrayInitializer* arrinit) // fill out any null entries still left with default values // element default initializer - LLConstant* defelem = elemty->defaultInit(arrinit->loc)->toConstElem(gIR); + LLConstant* defelem = DtoConstExpInit(arrinit->loc, elemty, elemty->defaultInit(arrinit->loc)); bool mismatch2 = (defelem->getType() != llelemty); for (size_t i = 0; i < arrlen; i++) diff --git a/gen/asm-x86-64.h b/gen/asm-x86-64.h index dd3a12d0..46e065b9 100644 --- a/gen/asm-x86-64.h +++ b/gen/asm-x86-64.h @@ -1558,6 +1558,10 @@ struct AsmProcessor break; } + // osx needs an extra underscore + if (global.params.os == OSMacOSX) + insnTemplate->writestring("_"); + // print out the mangle insnTemplate->writestring(vd->mangle()); vd->nakedUse = true; diff --git a/gen/functions.cpp b/gen/functions.cpp index e4f8ce73..6d403913 100644 --- a/gen/functions.cpp +++ b/gen/functions.cpp @@ -528,7 +528,7 @@ void DtoDeclareFunction(FuncDeclaration* fdecl) // calling convention if (!vafunc && fdecl->llvmInternal != LLVMintrinsic) - func->setCallingConv(DtoCallingConv(f->linkage)); + func->setCallingConv(DtoCallingConv(fdecl->loc, f->linkage)); else // fall back to C, it should be the right thing to do func->setCallingConv(llvm::CallingConv::C); @@ -546,12 +546,16 @@ void DtoDeclareFunction(FuncDeclaration* fdecl) } // static ctor - if (fdecl->isStaticCtorDeclaration() && fdecl->getModule() == gIR->dmodule) { - gIR->ctors.push_back(fdecl); + if (fdecl->isStaticCtorDeclaration()) { + if (fdecl->getModule() == gIR->dmodule || fdecl->inTemplateInstance()) { + gIR->ctors.push_back(fdecl); + } } // static dtor - else if (fdecl->isStaticDtorDeclaration() && fdecl->getModule() == gIR->dmodule) { - gIR->dtors.push_back(fdecl); + else if (fdecl->isStaticDtorDeclaration()) { + if (fdecl->getModule() == gIR->dmodule || fdecl->inTemplateInstance()) { + gIR->dtors.push_back(fdecl); + } } // we never reference parameters of function prototypes diff --git a/gen/llvmhelpers.cpp b/gen/llvmhelpers.cpp index 82f565e1..18dd481b 100644 --- a/gen/llvmhelpers.cpp +++ b/gen/llvmhelpers.cpp @@ -109,7 +109,7 @@ llvm::AllocaInst* DtoAlloca(const LLType* lltype, LLValue* arraysize, const std: // ASSERT HELPER ////////////////////////////////////////////////////////////////////////////////////////*/ -void DtoAssert(Loc* loc, DValue* msg) +void DtoAssert(Module* M, Loc* loc, DValue* msg) { std::vector args; @@ -124,7 +124,12 @@ void DtoAssert(Loc* loc, DValue* msg) } // file param - args.push_back(DtoLoad(gIR->dmodule->ir.irModule->fileName)); + + // we might be generating for an imported template function + if (!M->ir.irModule) + M->ir.irModule = new IrModule(M, M->srcfile->toChars()); + + args.push_back(DtoLoad(M->ir.irModule->fileName)); // line param LLConstant* c = DtoConstUint(loc->linnum); @@ -826,6 +831,7 @@ DValue* DtoPaintType(Loc& loc, DValue* val, Type* to) // TEMPLATE HELPERS ////////////////////////////////////////////////////////////////////////////////////////*/ +// FIXME: when is this the right one to use instead of Dsymbol::inTemplateInstance() ? bool DtoIsTemplateInstance(Dsymbol* s) { if (!s) return false; diff --git a/gen/llvmhelpers.h b/gen/llvmhelpers.h index 6764f7a9..5363d96f 100644 --- a/gen/llvmhelpers.h +++ b/gen/llvmhelpers.h @@ -16,7 +16,7 @@ llvm::AllocaInst* DtoAlloca(const LLType* lltype, const std::string& name = ""); llvm::AllocaInst* DtoAlloca(const LLType* lltype, LLValue* arraysize, const std::string& name = ""); // assertion generator -void DtoAssert(Loc* loc, DValue* msg); +void DtoAssert(Module* M, Loc* loc, DValue* msg); // return the LabelStatement from the current function with the given identifier or NULL if not found LabelStatement* DtoLabelStatement(Identifier* ident); @@ -113,7 +113,7 @@ void DtoOverloadedIntrinsicName(TemplateInstance* ti, TemplateDeclaration* td, s //////////////////////////////////////////// /// convert DMD calling conv to LLVM -unsigned DtoCallingConv(LINK l); +unsigned DtoCallingConv(Loc loc, LINK l); /// TypeFunction* DtoTypeFunction(DValue* fnval); diff --git a/gen/tocall.cpp b/gen/tocall.cpp index 11f34f52..6a191d0b 100644 --- a/gen/tocall.cpp +++ b/gen/tocall.cpp @@ -33,7 +33,7 @@ TypeFunction* DtoTypeFunction(DValue* fnval) ////////////////////////////////////////////////////////////////////////////////////////// -unsigned DtoCallingConv(LINK l) +unsigned DtoCallingConv(Loc loc, LINK l) { if (l == LINKc || l == LINKcpp || l == LINKintrinsic) return llvm::CallingConv::C; @@ -50,7 +50,10 @@ unsigned DtoCallingConv(LINK l) else if (l == LINKwindows) return llvm::CallingConv::X86_StdCall; else - assert(0 && "Unsupported calling convention"); + { + error(loc, "unsupported calling convention"); + fatal(); + } } ////////////////////////////////////////////////////////////////////////////////////////// @@ -235,7 +238,7 @@ DValue* DtoCallFunction(Loc& loc, Type* resulttype, DValue* fnval, Expressions* bool nestedcall = tf->usesNest; bool dvarargs = (tf->linkage == LINKd && tf->varargs == 1); - unsigned callconv = DtoCallingConv(tf->linkage); + unsigned callconv = DtoCallingConv(loc, tf->linkage); // get callee llvm value LLValue* callable = DtoCallableValue(fnval); diff --git a/gen/toir.cpp b/gen/toir.cpp index b3b10392..6ee7d329 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -1632,6 +1632,8 @@ DValue* NewExp::toElem(IRState* p) DVarValue tmpvar(newtype, mem); // default initialize + // FIXME: should this use DtoConstExpInit instead ? + // or is static arrays the only troublemaker? Expression* exp = newtype->defaultInit(loc); DValue* iv = exp->toElem(gIR); DtoAssign(loc, &tmpvar, iv); @@ -1766,7 +1768,7 @@ DValue* AssertExp::toElem(IRState* p) // call assert runtime functions p->scope() = IRScope(assertbb,endbb); - DtoAssert(&loc, msg ? msg->toElem(p) : NULL); + DtoAssert(p->func()->decl->getModule(), &loc, msg ? msg->toElem(p) : NULL); // rewrite the scope p->scope() = IRScope(endbb,oldend); @@ -1941,7 +1943,7 @@ DValue* HaltExp::toElem(IRState* p) // FIXME: DMD inserts a trap here... we probably should as well !?! #if 1 - DtoAssert(&loc, NULL); + DtoAssert(p->func()->decl->getModule(), &loc, NULL); #else // call the new (?) trap intrinsic p->ir->CreateCall(GET_INTRINSIC_DECL(trap),""); diff --git a/gen/toobj.cpp b/gen/toobj.cpp index 64472570..c6bedfb0 100644 --- a/gen/toobj.cpp +++ b/gen/toobj.cpp @@ -99,8 +99,9 @@ void Module::genobjfile(int multiobj) ir.module = new llvm::Module(mname); // module ir state - // might already exist via import, just overwrite... - //FIXME: is there a good reason for overwriting? + // might already exist via import, just overwrite since + // the global created for the filename must belong to the right llvm module + // FIXME: but shouldn't this always get reset between modules? like other IrSymbols this->ir.irModule = new IrModule(this, srcfile->toChars()); // set target stuff @@ -417,7 +418,7 @@ llvm::Function* build_module_ctor() const llvm::FunctionType* fnTy = llvm::FunctionType::get(LLType::VoidTy,argsTy,false); assert(gIR->module->getFunction(name) == NULL); llvm::Function* fn = llvm::Function::Create(fnTy, llvm::GlobalValue::InternalLinkage, name, gIR->module); - fn->setCallingConv(DtoCallingConv(LINKd)); + fn->setCallingConv(DtoCallingConv(0, LINKd)); llvm::BasicBlock* bb = llvm::BasicBlock::Create("entry", fn); IRBuilder<> builder(bb); @@ -432,7 +433,7 @@ llvm::Function* build_module_ctor() for (size_t i=0; ictors[i]->ir.irFunc->func; llvm::CallInst* call = builder.CreateCall(f,""); - call->setCallingConv(DtoCallingConv(LINKd)); + call->setCallingConv(DtoCallingConv(0, LINKd)); } // debug info end @@ -462,7 +463,7 @@ static llvm::Function* build_module_dtor() const llvm::FunctionType* fnTy = llvm::FunctionType::get(LLType::VoidTy,argsTy,false); assert(gIR->module->getFunction(name) == NULL); llvm::Function* fn = llvm::Function::Create(fnTy, llvm::GlobalValue::InternalLinkage, name, gIR->module); - fn->setCallingConv(DtoCallingConv(LINKd)); + fn->setCallingConv(DtoCallingConv(0, LINKd)); llvm::BasicBlock* bb = llvm::BasicBlock::Create("entry", fn); IRBuilder<> builder(bb); @@ -477,7 +478,7 @@ static llvm::Function* build_module_dtor() for (size_t i=0; idtors[i]->ir.irFunc->func; llvm::CallInst* call = builder.CreateCall(f,""); - call->setCallingConv(DtoCallingConv(LINKd)); + call->setCallingConv(DtoCallingConv(0, LINKd)); } // debug info end @@ -507,7 +508,7 @@ static llvm::Function* build_module_unittest() const llvm::FunctionType* fnTy = llvm::FunctionType::get(LLType::VoidTy,argsTy,false); assert(gIR->module->getFunction(name) == NULL); llvm::Function* fn = llvm::Function::Create(fnTy, llvm::GlobalValue::InternalLinkage, name, gIR->module); - fn->setCallingConv(DtoCallingConv(LINKd)); + fn->setCallingConv(DtoCallingConv(0, LINKd)); llvm::BasicBlock* bb = llvm::BasicBlock::Create("entry", fn); IRBuilder<> builder(bb); @@ -522,7 +523,7 @@ static llvm::Function* build_module_unittest() for (size_t i=0; iunitTests[i]->ir.irFunc->func; llvm::CallInst* call = builder.CreateCall(f,""); - call->setCallingConv(DtoCallingConv(LINKd)); + call->setCallingConv(DtoCallingConv(0, LINKd)); } // debug info end diff --git a/ir/irfunction.cpp b/ir/irfunction.cpp index 37258854..8c139ada 100644 --- a/ir/irfunction.cpp +++ b/ir/irfunction.cpp @@ -33,9 +33,6 @@ IrFunction::IrFunction(FuncDeclaration* fd) dwarfSubProg = NULL; - srcfileArg = NULL; - msgArg = NULL; - nextUnique.push(0); } diff --git a/ir/irfunction.h b/ir/irfunction.h index e611a84d..bd1a4296 100644 --- a/ir/irfunction.h +++ b/ir/irfunction.h @@ -30,9 +30,6 @@ struct IrFunction : IrBase llvm::Constant* dwarfSubProg; - llvm::AllocaInst* srcfileArg; - llvm::AllocaInst* msgArg; - // pushes a unique label scope of the given name void pushUniqueLabelScope(const char* name); // pops a label scope diff --git a/tests/mini/naked_asm4.d b/tests/mini/naked_asm4.d index 5303dc66..116a92c9 100644 --- a/tests/mini/naked_asm4.d +++ b/tests/mini/naked_asm4.d @@ -8,7 +8,7 @@ void foo() hlt; pass: ret; } - version(X86_64) + else version(X86_64) asm { naked;