From 91d723da7b68be5245c3ac58aa2a36d04658cfb8 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 26 Jul 2008 20:23:23 +0000 Subject: [PATCH] implement codegen support for labels at the end of stmtexprs. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@54100 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExprScalar.cpp | 3 ++- lib/CodeGen/CGStmt.cpp | 22 +++++++++++++++++----- lib/CodeGen/CodeGenFunction.h | 1 + 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index 37579c6da1..da8350b19f 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -583,7 +583,8 @@ Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) { } Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { - return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getScalarVal(); + return CGF.EmitCompoundStmt(*E->getSubStmt(), + !E->getType()->isVoidType()).getScalarVal(); } diff --git a/lib/CodeGen/CGStmt.cpp b/lib/CodeGen/CGStmt.cpp index 78d8563bfc..9743c7cadf 100644 --- a/lib/CodeGen/CGStmt.cpp +++ b/lib/CodeGen/CGStmt.cpp @@ -86,8 +86,6 @@ void CodeGenFunction::EmitStmt(const Stmt *S) { RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, llvm::Value *AggLoc, bool isAggVol) { // FIXME: handle vla's etc. - if (S.body_empty() || !isa(S.body_back())) GetLast = false; - CGDebugInfo *DI = CGM.getDebugInfo(); if (DI) { if (S.getLBracLoc().isValid()) @@ -108,7 +106,17 @@ RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, if (!GetLast) return RValue::get(0); - return EmitAnyExpr(cast(S.body_back()), AggLoc); + // We have to special case labels here. They are statements, but when put at + // the end of a statement expression, they yield the value of their + // subexpression. Handle this by walking through all labels we encounter, + // emitting them before we evaluate the subexpr. + const Stmt *LastStmt = S.body_back(); + while (const LabelStmt *LS = dyn_cast(LastStmt)) { + EmitLabel(*LS); + LastStmt = LS->getSubStmt(); + } + + return EmitAnyExpr(cast(LastStmt), AggLoc); } void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) { @@ -130,10 +138,14 @@ void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) { Builder.SetInsertPoint(BB); } -void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { +void CodeGenFunction::EmitLabel(const LabelStmt &S) { llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S); - EmitBlock(NextBB); +} + + +void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { + EmitLabel(S); EmitStmt(S.getSubStmt()); } diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index 7d11a74d8c..5bf6bc1fea 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -359,6 +359,7 @@ public: void EmitStmt(const Stmt *S); RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false, llvm::Value *AggLoc = 0, bool isAggVol = false); + void EmitLabel(const LabelStmt &S); // helper for EmitLabelStmt. void EmitLabelStmt(const LabelStmt &S); void EmitGotoStmt(const GotoStmt &S); void EmitIfStmt(const IfStmt &S); -- 2.40.0