Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 1 | //===--- CGCXXTemp.cpp - Emit LLVM Code for C++ temporaries ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with C++ code generation of temporaries |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | using namespace clang; |
| 16 | using namespace CodeGen; |
| 17 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 18 | void CodeGenFunction::PushCXXTemporary(const CXXTemporary *Temporary, |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 19 | llvm::Value *Ptr) { |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 20 | llvm::BasicBlock *DtorBlock = createBasicBlock("temp.dtor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 21 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 22 | llvm::Value *CondPtr = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 23 | |
| 24 | // Check if temporaries need to be conditional. If so, we'll create a |
| 25 | // condition boolean, initialize it to 0 and |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 26 | if (!ConditionalTempDestructionStack.empty()) { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 27 | CondPtr = CreateTempAlloca(llvm::Type::getInt1Ty(VMContext), "cond"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 28 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 29 | // Initialize it to false. This initialization takes place right after |
| 30 | // the alloca insert point. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 31 | llvm::StoreInst *SI = |
Owen Anderson | 3b144ba | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 32 | new llvm::StoreInst(llvm::ConstantInt::getFalse(VMContext), CondPtr); |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 33 | llvm::BasicBlock *Block = AllocaInsertPt->getParent(); |
| 34 | Block->getInstList().insertAfter((llvm::Instruction *)AllocaInsertPt, SI); |
| 35 | |
| 36 | // Now set it to true. |
Owen Anderson | 3b144ba | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 37 | Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext), CondPtr); |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 38 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
| 40 | LiveTemporaries.push_back(CXXLiveTemporaryInfo(Temporary, Ptr, DtorBlock, |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 41 | CondPtr)); |
Anders Carlsson | d8bc5a9 | 2009-06-04 02:08:08 +0000 | [diff] [blame] | 42 | |
| 43 | PushCleanupBlock(DtorBlock); |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 46 | void CodeGenFunction::PopCXXTemporary() { |
| 47 | const CXXLiveTemporaryInfo& Info = LiveTemporaries.back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 49 | CleanupBlockInfo CleanupInfo = PopCleanupBlock(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | assert(CleanupInfo.CleanupBlock == Info.DtorBlock && |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 51 | "Cleanup block mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | assert(!CleanupInfo.SwitchBlock && |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 53 | "Should not have a switch block for temporary cleanup!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | assert(!CleanupInfo.EndBlock && |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 55 | "Should not have an end block for temporary cleanup!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 57 | EmitBlock(Info.DtorBlock); |
| 58 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 59 | llvm::BasicBlock *CondEnd = 0; |
| 60 | |
| 61 | // If this is a conditional temporary, we need to check the condition |
| 62 | // boolean and only call the destructor if it's true. |
| 63 | if (Info.CondPtr) { |
| 64 | llvm::BasicBlock *CondBlock = createBasicBlock("cond.dtor.call"); |
| 65 | CondEnd = createBasicBlock("cond.dtor.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 67 | llvm::Value *Cond = Builder.CreateLoad(Info.CondPtr); |
| 68 | Builder.CreateCondBr(Cond, CondBlock, CondEnd); |
| 69 | EmitBlock(CondBlock); |
| 70 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 72 | EmitCXXDestructorCall(Info.Temporary->getDestructor(), |
| 73 | Dtor_Complete, Info.ThisPtr); |
| 74 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 75 | if (CondEnd) { |
| 76 | // Reset the condition. to false. |
Owen Anderson | 3b144ba | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 77 | Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), Info.CondPtr); |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 78 | EmitBlock(CondEnd); |
| 79 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 81 | LiveTemporaries.pop_back(); |
| 82 | } |
| 83 | |
| 84 | RValue |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 85 | CodeGenFunction::EmitCXXExprWithTemporaries(const CXXExprWithTemporaries *E, |
| 86 | llvm::Value *AggLoc, |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 87 | bool IsAggLocVolatile, |
| 88 | bool IsInitializer) { |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 89 | // If we shouldn't destroy the temporaries, just emit the |
| 90 | // child expression. |
| 91 | if (!E->shouldDestroyTemporaries()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | return EmitAnyExpr(E->getSubExpr(), AggLoc, IsAggLocVolatile, |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 93 | /*IgnoreResult=*/false, IsInitializer); |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 94 | |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 95 | // Keep track of the current cleanup stack depth. |
| 96 | size_t CleanupStackDepth = CleanupEntries.size(); |
Daniel Dunbar | b4d4c4b | 2009-06-05 02:03:25 +0000 | [diff] [blame] | 97 | (void) CleanupStackDepth; |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 98 | |
| 99 | unsigned OldNumLiveTemporaries = LiveTemporaries.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
| 101 | RValue RV = EmitAnyExpr(E->getSubExpr(), AggLoc, IsAggLocVolatile, |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 102 | /*IgnoreResult=*/false, IsInitializer); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 104 | // Pop temporaries. |
| 105 | while (LiveTemporaries.size() > OldNumLiveTemporaries) |
| 106 | PopCXXTemporary(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 108 | assert(CleanupEntries.size() == CleanupStackDepth && |
| 109 | "Cleanup size mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 111 | return RV; |
| 112 | } |
Anders Carlsson | 1d84750 | 2009-06-04 02:22:12 +0000 | [diff] [blame] | 113 | |
Anders Carlsson | b9ea0b5 | 2009-09-14 01:10:45 +0000 | [diff] [blame^] | 114 | LValue CodeGenFunction::EmitCXXExprWithTemporariesLValue( |
| 115 | const CXXExprWithTemporaries *E) { |
| 116 | // If we shouldn't destroy the temporaries, just emit the |
| 117 | // child expression. |
| 118 | if (!E->shouldDestroyTemporaries()) |
| 119 | return EmitLValue(E->getSubExpr()); |
| 120 | |
| 121 | // Keep track of the current cleanup stack depth. |
| 122 | size_t CleanupStackDepth = CleanupEntries.size(); |
| 123 | (void) CleanupStackDepth; |
| 124 | |
| 125 | unsigned OldNumLiveTemporaries = LiveTemporaries.size(); |
| 126 | |
| 127 | LValue LV = EmitLValue(E->getSubExpr()); |
| 128 | |
| 129 | // Pop temporaries. |
| 130 | while (LiveTemporaries.size() > OldNumLiveTemporaries) |
| 131 | PopCXXTemporary(); |
| 132 | |
| 133 | assert(CleanupEntries.size() == CleanupStackDepth && |
| 134 | "Cleanup size mismatch!"); |
| 135 | |
| 136 | return LV; |
| 137 | } |
| 138 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | void |
Anders Carlsson | 1d84750 | 2009-06-04 02:22:12 +0000 | [diff] [blame] | 140 | CodeGenFunction::PushConditionalTempDestruction() { |
| 141 | // Store the current number of live temporaries. |
| 142 | ConditionalTempDestructionStack.push_back(LiveTemporaries.size()); |
| 143 | } |
| 144 | |
| 145 | void CodeGenFunction::PopConditionalTempDestruction() { |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 146 | size_t NumLiveTemporaries = ConditionalTempDestructionStack.back(); |
| 147 | ConditionalTempDestructionStack.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 149 | // Pop temporaries. |
| 150 | while (LiveTemporaries.size() > NumLiveTemporaries) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | assert(LiveTemporaries.back().CondPtr && |
Daniel Dunbar | b4d4c4b | 2009-06-05 02:03:25 +0000 | [diff] [blame] | 152 | "Conditional temporary must have a cond ptr!"); |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 153 | |
| 154 | PopCXXTemporary(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | } |
Anders Carlsson | 1d84750 | 2009-06-04 02:22:12 +0000 | [diff] [blame] | 156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |