Add a matching call to llvm.va_end when llvm.va_start was called.

Resolves #1744
This commit is contained in:
Johan Engelen 2016-09-05 19:46:26 +02:00
parent 9dbd583707
commit 63fba4efdb
2 changed files with 72 additions and 0 deletions

View file

@ -1011,6 +1011,17 @@ void DtoDefineFunction(FuncDeclaration *fd, bool linkageAvailableExternally) {
// copy _arguments to a memory location
irFunc->_arguments =
DtoAllocaDump(irFunc->_arguments, 0, "_arguments_mem");
// Push cleanup block that calls va_end to match the va_start call.
{
auto *vaendBB =
llvm::BasicBlock::Create(gIR->context(), "vaend", gIR->topfunc());
IRScope saveScope = gIR->scope();
gIR->scope() = IRScope(vaendBB);
gIR->ir->CreateCall(GET_INTRINSIC_DECL(vaend), llAp);
funcGen.scopes.pushCleanup(vaendBB, gIR->scopebb());
gIR->scope() = saveScope;
}
}
funcGen.pgo.emitCounterIncrement(fd->fbody);
@ -1019,6 +1030,17 @@ void DtoDefineFunction(FuncDeclaration *fd, bool linkageAvailableExternally) {
// output function body
Statement_toIR(fd->fbody, gIR);
// D varargs: emit the cleanup block that calls va_end.
if (f->linkage == LINKd && f->varargs == 1) {
if (!gIR->scopereturned()) {
if (!funcGen.retBlock)
funcGen.retBlock = gIR->insertBB("return");
funcGen.scopes.runCleanups(0, funcGen.retBlock);
gIR->scope() = IRScope(funcGen.retBlock);
}
funcGen.scopes.popCleanups(0);
}
llvm::BasicBlock *bb = gIR->scopebb();
if (pred_begin(bb) == pred_end(bb) &&
bb != &bb->getParent()->getEntryBlock()) {