2009-11-24 08:51:11 +03:00
|
|
|
//===--- CGTemporaries.cpp - Emit LLVM Code for C++ temporaries -----------===//
|
2009-06-03 22:40:21 +04:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code dealing with C++ code generation of temporaries
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
void CodeGenFunction::PushCXXTemporary(const CXXTemporary *Temporary,
|
2009-06-03 22:40:21 +04:00
|
|
|
llvm::Value *Ptr) {
|
2009-12-24 20:48:05 +03:00
|
|
|
assert((LiveTemporaries.empty() ||
|
2009-12-24 21:16:21 +03:00
|
|
|
LiveTemporaries.back().ThisPtr != Ptr ||
|
|
|
|
ConditionalBranchLevel) &&
|
2009-12-24 20:48:05 +03:00
|
|
|
"Pushed the same temporary twice; AST is likely wrong");
|
2009-06-03 23:05:16 +04:00
|
|
|
llvm::BasicBlock *DtorBlock = createBasicBlock("temp.dtor");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-04 06:47:33 +04:00
|
|
|
llvm::Value *CondPtr = 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
// Check if temporaries need to be conditional. If so, we'll create a
|
|
|
|
// condition boolean, initialize it to 0 and
|
2009-11-20 20:27:56 +03:00
|
|
|
if (ConditionalBranchLevel != 0) {
|
2009-08-14 01:57:51 +04:00
|
|
|
CondPtr = CreateTempAlloca(llvm::Type::getInt1Ty(VMContext), "cond");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-04 06:47:33 +04:00
|
|
|
// Initialize it to false. This initialization takes place right after
|
|
|
|
// the alloca insert point.
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::StoreInst *SI =
|
2009-07-31 21:39:36 +04:00
|
|
|
new llvm::StoreInst(llvm::ConstantInt::getFalse(VMContext), CondPtr);
|
2009-06-04 06:47:33 +04:00
|
|
|
llvm::BasicBlock *Block = AllocaInsertPt->getParent();
|
|
|
|
Block->getInstList().insertAfter((llvm::Instruction *)AllocaInsertPt, SI);
|
|
|
|
|
|
|
|
// Now set it to true.
|
2009-07-31 21:39:36 +04:00
|
|
|
Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext), CondPtr);
|
2009-06-04 06:47:33 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
LiveTemporaries.push_back(CXXLiveTemporaryInfo(Temporary, Ptr, DtorBlock,
|
2009-06-04 06:47:33 +04:00
|
|
|
CondPtr));
|
2009-06-04 06:08:08 +04:00
|
|
|
|
|
|
|
PushCleanupBlock(DtorBlock);
|
2009-12-17 09:08:47 +03:00
|
|
|
|
|
|
|
if (Exceptions) {
|
|
|
|
const CXXLiveTemporaryInfo& Info = LiveTemporaries.back();
|
|
|
|
llvm::BasicBlock *CondEnd = 0;
|
|
|
|
|
|
|
|
EHCleanupBlock Cleanup(*this);
|
|
|
|
|
|
|
|
// If this is a conditional temporary, we need to check the condition
|
|
|
|
// boolean and only call the destructor if it's true.
|
|
|
|
if (Info.CondPtr) {
|
|
|
|
llvm::BasicBlock *CondBlock = createBasicBlock("cond.dtor.call");
|
|
|
|
CondEnd = createBasicBlock("cond.dtor.end");
|
|
|
|
|
|
|
|
llvm::Value *Cond = Builder.CreateLoad(Info.CondPtr);
|
|
|
|
Builder.CreateCondBr(Cond, CondBlock, CondEnd);
|
|
|
|
EmitBlock(CondBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
EmitCXXDestructorCall(Info.Temporary->getDestructor(),
|
|
|
|
Dtor_Complete, Info.ThisPtr);
|
|
|
|
|
|
|
|
if (CondEnd) {
|
|
|
|
// Reset the condition. to false.
|
|
|
|
Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), Info.CondPtr);
|
|
|
|
EmitBlock(CondEnd);
|
|
|
|
}
|
|
|
|
}
|
2009-06-03 23:05:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::PopCXXTemporary() {
|
|
|
|
const CXXLiveTemporaryInfo& Info = LiveTemporaries.back();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-03 23:05:16 +04:00
|
|
|
CleanupBlockInfo CleanupInfo = PopCleanupBlock();
|
2009-09-09 19:08:12 +04:00
|
|
|
assert(CleanupInfo.CleanupBlock == Info.DtorBlock &&
|
2009-06-03 23:05:16 +04:00
|
|
|
"Cleanup block mismatch!");
|
2009-09-09 19:08:12 +04:00
|
|
|
assert(!CleanupInfo.SwitchBlock &&
|
2009-06-03 23:05:16 +04:00
|
|
|
"Should not have a switch block for temporary cleanup!");
|
2009-09-09 19:08:12 +04:00
|
|
|
assert(!CleanupInfo.EndBlock &&
|
2009-06-03 23:05:16 +04:00
|
|
|
"Should not have an end block for temporary cleanup!");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-14 05:30:44 +04:00
|
|
|
llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
|
|
|
|
if (CurBB && !CurBB->getTerminator() &&
|
|
|
|
Info.DtorBlock->getNumUses() == 0) {
|
|
|
|
CurBB->getInstList().splice(CurBB->end(), Info.DtorBlock->getInstList());
|
|
|
|
delete Info.DtorBlock;
|
|
|
|
} else
|
|
|
|
EmitBlock(Info.DtorBlock);
|
2009-06-03 23:05:16 +04:00
|
|
|
|
2009-06-04 06:47:33 +04:00
|
|
|
llvm::BasicBlock *CondEnd = 0;
|
|
|
|
|
|
|
|
// If this is a conditional temporary, we need to check the condition
|
|
|
|
// boolean and only call the destructor if it's true.
|
|
|
|
if (Info.CondPtr) {
|
|
|
|
llvm::BasicBlock *CondBlock = createBasicBlock("cond.dtor.call");
|
|
|
|
CondEnd = createBasicBlock("cond.dtor.end");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-04 06:47:33 +04:00
|
|
|
llvm::Value *Cond = Builder.CreateLoad(Info.CondPtr);
|
|
|
|
Builder.CreateCondBr(Cond, CondBlock, CondEnd);
|
|
|
|
EmitBlock(CondBlock);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-03 23:05:16 +04:00
|
|
|
EmitCXXDestructorCall(Info.Temporary->getDestructor(),
|
|
|
|
Dtor_Complete, Info.ThisPtr);
|
|
|
|
|
2009-06-04 06:47:33 +04:00
|
|
|
if (CondEnd) {
|
|
|
|
// Reset the condition. to false.
|
2009-07-31 21:39:36 +04:00
|
|
|
Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), Info.CondPtr);
|
2009-06-04 06:47:33 +04:00
|
|
|
EmitBlock(CondEnd);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-03 23:05:16 +04:00
|
|
|
LiveTemporaries.pop_back();
|
2009-06-03 22:40:21 +04:00
|
|
|
}
|
|
|
|
|
2009-06-03 23:05:16 +04:00
|
|
|
RValue
|
2009-06-03 22:40:21 +04:00
|
|
|
CodeGenFunction::EmitCXXExprWithTemporaries(const CXXExprWithTemporaries *E,
|
|
|
|
llvm::Value *AggLoc,
|
2009-08-16 11:36:22 +04:00
|
|
|
bool IsAggLocVolatile,
|
|
|
|
bool IsInitializer) {
|
2009-06-03 22:40:21 +04:00
|
|
|
// Keep track of the current cleanup stack depth.
|
|
|
|
size_t CleanupStackDepth = CleanupEntries.size();
|
2009-06-05 06:03:25 +04:00
|
|
|
(void) CleanupStackDepth;
|
2009-06-03 22:40:21 +04:00
|
|
|
|
|
|
|
unsigned OldNumLiveTemporaries = LiveTemporaries.size();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
RValue RV = EmitAnyExpr(E->getSubExpr(), AggLoc, IsAggLocVolatile,
|
2009-08-16 11:36:22 +04:00
|
|
|
/*IgnoreResult=*/false, IsInitializer);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-04 06:47:33 +04:00
|
|
|
// Pop temporaries.
|
|
|
|
while (LiveTemporaries.size() > OldNumLiveTemporaries)
|
|
|
|
PopCXXTemporary();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-04 06:47:33 +04:00
|
|
|
assert(CleanupEntries.size() == CleanupStackDepth &&
|
|
|
|
"Cleanup size mismatch!");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-03 22:40:21 +04:00
|
|
|
return RV;
|
|
|
|
}
|
2009-06-04 06:22:12 +04:00
|
|
|
|
2009-09-14 05:10:45 +04:00
|
|
|
LValue CodeGenFunction::EmitCXXExprWithTemporariesLValue(
|
|
|
|
const CXXExprWithTemporaries *E) {
|
|
|
|
// Keep track of the current cleanup stack depth.
|
|
|
|
size_t CleanupStackDepth = CleanupEntries.size();
|
|
|
|
(void) CleanupStackDepth;
|
|
|
|
|
|
|
|
unsigned OldNumLiveTemporaries = LiveTemporaries.size();
|
|
|
|
|
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
|
|
|
|
|
|
|
// Pop temporaries.
|
|
|
|
while (LiveTemporaries.size() > OldNumLiveTemporaries)
|
|
|
|
PopCXXTemporary();
|
|
|
|
|
|
|
|
assert(CleanupEntries.size() == CleanupStackDepth &&
|
|
|
|
"Cleanup size mismatch!");
|
|
|
|
|
|
|
|
return LV;
|
|
|
|
}
|