From be0f76a712ddfdb6c0e4e5240174c21178280fa3 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Sat, 7 Feb 2009 23:50:39 +0000 Subject: [PATCH] Add support for emitting cleanup blocks. Make EmitCompoundStatement emit cleanup blocks if necessary llvm-svn: 64051 --- clang/lib/CodeGen/CGStmt.cpp | 7 ++++++- clang/lib/CodeGen/CodeGenFunction.cpp | 21 +++++++++++++++++++++ clang/lib/CodeGen/CodeGenFunction.h | 7 +++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp index ac97c6329dab..fc7a7c7f8f1e 100644 --- a/clang/lib/CodeGen/CGStmt.cpp +++ b/clang/lib/CodeGen/CGStmt.cpp @@ -123,7 +123,7 @@ bool CodeGenFunction::EmitSimpleStmt(const Stmt *S) { /// (for use by the statement expression extension). RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, llvm::Value *AggLoc, bool isAggVol) { - // FIXME: handle vla's etc. + CGDebugInfo *DI = CGM.getDebugInfo(); if (DI) { EnsureInsertPoint(); @@ -131,6 +131,9 @@ RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, DI->EmitRegionStart(CurFn, Builder); } + // Keep track of the current cleanup stack depth. + size_t CleanupStackDepth = CleanupEntries.size(); + // Push a null stack save value. StackSaveValues.push_back(0); @@ -171,6 +174,8 @@ RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, Builder.CreateCall(F, V); } + EmitCleanupBlocks(CleanupStackDepth); + return RV; } diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 4ba4d600f864..838de70218d5 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -521,3 +521,24 @@ llvm::BasicBlock *CodeGenFunction::CreateCleanupBlock() return CleanupBlock; } + +void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) +{ + assert(CleanupEntries.size() >= OldCleanupStackSize && + "Cleanup stack mismatch!"); + + while (CleanupEntries.size() > OldCleanupStackSize) + EmitCleanupBlock(); +} + +void CodeGenFunction::EmitCleanupBlock() +{ + CleanupEntry &CE = CleanupEntries.back(); + + llvm::BasicBlock *CleanupBlock = CE.CleanupBlock; + + CleanupEntries.pop_back(); + + EmitBlock(CleanupBlock); +} + diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index 1f037600f51f..8a0220724e5d 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -151,6 +151,10 @@ public: } }; + /// EmitCleanupBlocks - Takes the old cleanup stack size and emits the cleanup + /// blocks that have been added. + void EmitCleanupBlocks(size_t OldCleanupStackSize); + private: /// LabelIDs - Track arbitrary ids assigned to labels for use in /// implementing the GCC address-of-label extension and indirect @@ -762,6 +766,9 @@ private: llvm::Value* EmitAsmInput(const AsmStmt &S, TargetInfo::ConstraintInfo Info, const Expr *InputExpr, std::string &ConstraintStr); + /// EmitCleanupBlock - emits a single cleanup block. + void EmitCleanupBlock(); + }; } // end namespace CodeGen } // end namespace clang