Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 1 | //===--- CGException.cpp - Emit LLVM Code for C++ exceptions --------------===// |
| 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++ exception related code generation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 14 | #include "clang/AST/StmtCXX.h" |
| 15 | |
| 16 | #include "llvm/Intrinsics.h" |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CallSite.h" |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 18 | |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 20 | #include "CGException.h" |
| 21 | |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 25 | /// Push an entry of the given size onto this protected-scope stack. |
| 26 | char *EHScopeStack::allocate(size_t Size) { |
| 27 | if (!StartOfBuffer) { |
| 28 | unsigned Capacity = 1024; |
| 29 | while (Capacity < Size) Capacity *= 2; |
| 30 | StartOfBuffer = new char[Capacity]; |
| 31 | StartOfData = EndOfBuffer = StartOfBuffer + Capacity; |
| 32 | } else if (static_cast<size_t>(StartOfData - StartOfBuffer) < Size) { |
| 33 | unsigned CurrentCapacity = EndOfBuffer - StartOfBuffer; |
| 34 | unsigned UsedCapacity = CurrentCapacity - (StartOfData - StartOfBuffer); |
| 35 | |
| 36 | unsigned NewCapacity = CurrentCapacity; |
| 37 | do { |
| 38 | NewCapacity *= 2; |
| 39 | } while (NewCapacity < UsedCapacity + Size); |
| 40 | |
| 41 | char *NewStartOfBuffer = new char[NewCapacity]; |
| 42 | char *NewEndOfBuffer = NewStartOfBuffer + NewCapacity; |
| 43 | char *NewStartOfData = NewEndOfBuffer - UsedCapacity; |
| 44 | memcpy(NewStartOfData, StartOfData, UsedCapacity); |
| 45 | delete [] StartOfBuffer; |
| 46 | StartOfBuffer = NewStartOfBuffer; |
| 47 | EndOfBuffer = NewEndOfBuffer; |
| 48 | StartOfData = NewStartOfData; |
| 49 | } |
| 50 | |
| 51 | assert(StartOfBuffer + Size <= StartOfData); |
| 52 | StartOfData -= Size; |
| 53 | return StartOfData; |
| 54 | } |
| 55 | |
| 56 | EHScopeStack::stable_iterator |
| 57 | EHScopeStack::getEnclosingEHCleanup(iterator it) const { |
| 58 | assert(it != end()); |
| 59 | do { |
| 60 | if (isa<EHCleanupScope>(*it)) { |
| 61 | if (cast<EHCleanupScope>(*it).isEHCleanup()) |
| 62 | return stabilize(it); |
| 63 | return cast<EHCleanupScope>(*it).getEnclosingEHCleanup(); |
| 64 | } |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame^] | 65 | if (isa<EHLazyCleanupScope>(*it)) { |
| 66 | if (cast<EHLazyCleanupScope>(*it).isEHCleanup()) |
| 67 | return stabilize(it); |
| 68 | return cast<EHLazyCleanupScope>(*it).getEnclosingEHCleanup(); |
| 69 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 70 | ++it; |
| 71 | } while (it != end()); |
| 72 | return stable_end(); |
| 73 | } |
| 74 | |
| 75 | |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame^] | 76 | void *EHScopeStack::pushLazyCleanup(CleanupKind Kind, size_t Size) { |
| 77 | assert(((Size % sizeof(void*)) == 0) && "cleanup type is misaligned"); |
| 78 | char *Buffer = allocate(EHLazyCleanupScope::getSizeForCleanupSize(Size)); |
| 79 | bool IsNormalCleanup = Kind != EHCleanup; |
| 80 | bool IsEHCleanup = Kind != NormalCleanup; |
| 81 | EHLazyCleanupScope *Scope = |
| 82 | new (Buffer) EHLazyCleanupScope(IsNormalCleanup, |
| 83 | IsEHCleanup, |
| 84 | Size, |
| 85 | BranchFixups.size(), |
| 86 | InnermostNormalCleanup, |
| 87 | InnermostEHCleanup); |
| 88 | if (IsNormalCleanup) |
| 89 | InnermostNormalCleanup = stable_begin(); |
| 90 | if (IsEHCleanup) |
| 91 | InnermostEHCleanup = stable_begin(); |
| 92 | |
| 93 | return Scope->getCleanupBuffer(); |
| 94 | } |
| 95 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 96 | void EHScopeStack::pushCleanup(llvm::BasicBlock *NormalEntry, |
| 97 | llvm::BasicBlock *NormalExit, |
| 98 | llvm::BasicBlock *EHEntry, |
| 99 | llvm::BasicBlock *EHExit) { |
| 100 | char *Buffer = allocate(EHCleanupScope::getSize()); |
| 101 | new (Buffer) EHCleanupScope(BranchFixups.size(), |
| 102 | InnermostNormalCleanup, |
| 103 | InnermostEHCleanup, |
| 104 | NormalEntry, NormalExit, EHEntry, EHExit); |
| 105 | if (NormalEntry) |
| 106 | InnermostNormalCleanup = stable_begin(); |
| 107 | if (EHEntry) |
| 108 | InnermostEHCleanup = stable_begin(); |
| 109 | } |
| 110 | |
| 111 | void EHScopeStack::popCleanup() { |
| 112 | assert(!empty() && "popping exception stack when not empty"); |
| 113 | |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame^] | 114 | if (isa<EHLazyCleanupScope>(*begin())) { |
| 115 | EHLazyCleanupScope &Cleanup = cast<EHLazyCleanupScope>(*begin()); |
| 116 | InnermostNormalCleanup = Cleanup.getEnclosingNormalCleanup(); |
| 117 | InnermostEHCleanup = Cleanup.getEnclosingEHCleanup(); |
| 118 | StartOfData += Cleanup.getAllocatedSize(); |
| 119 | } else { |
| 120 | assert(isa<EHCleanupScope>(*begin())); |
| 121 | EHCleanupScope &Cleanup = cast<EHCleanupScope>(*begin()); |
| 122 | InnermostNormalCleanup = Cleanup.getEnclosingNormalCleanup(); |
| 123 | InnermostEHCleanup = Cleanup.getEnclosingEHCleanup(); |
| 124 | StartOfData += EHCleanupScope::getSize(); |
| 125 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 126 | |
| 127 | // Check whether we can shrink the branch-fixups stack. |
| 128 | if (!BranchFixups.empty()) { |
| 129 | // If we no longer have any normal cleanups, all the fixups are |
| 130 | // complete. |
| 131 | if (!hasNormalCleanups()) |
| 132 | BranchFixups.clear(); |
| 133 | |
| 134 | // Otherwise we can still trim out unnecessary nulls. |
| 135 | else |
| 136 | popNullFixups(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | EHFilterScope *EHScopeStack::pushFilter(unsigned NumFilters) { |
| 141 | char *Buffer = allocate(EHFilterScope::getSizeForNumFilters(NumFilters)); |
| 142 | CatchDepth++; |
| 143 | return new (Buffer) EHFilterScope(NumFilters); |
| 144 | } |
| 145 | |
| 146 | void EHScopeStack::popFilter() { |
| 147 | assert(!empty() && "popping exception stack when not empty"); |
| 148 | |
| 149 | EHFilterScope &Filter = cast<EHFilterScope>(*begin()); |
| 150 | StartOfData += EHFilterScope::getSizeForNumFilters(Filter.getNumFilters()); |
| 151 | |
| 152 | assert(CatchDepth > 0 && "mismatched filter push/pop"); |
| 153 | CatchDepth--; |
| 154 | } |
| 155 | |
| 156 | EHCatchScope *EHScopeStack::pushCatch(unsigned NumHandlers) { |
| 157 | char *Buffer = allocate(EHCatchScope::getSizeForNumHandlers(NumHandlers)); |
| 158 | CatchDepth++; |
| 159 | return new (Buffer) EHCatchScope(NumHandlers); |
| 160 | } |
| 161 | |
| 162 | void EHScopeStack::pushTerminate() { |
| 163 | char *Buffer = allocate(EHTerminateScope::getSize()); |
| 164 | CatchDepth++; |
| 165 | new (Buffer) EHTerminateScope(); |
| 166 | } |
| 167 | |
| 168 | /// Remove any 'null' fixups on the stack. However, we can't pop more |
| 169 | /// fixups than the fixup depth on the innermost normal cleanup, or |
| 170 | /// else fixups that we try to add to that cleanup will end up in the |
| 171 | /// wrong place. We *could* try to shrink fixup depths, but that's |
| 172 | /// actually a lot of work for little benefit. |
| 173 | void EHScopeStack::popNullFixups() { |
| 174 | // We expect this to only be called when there's still an innermost |
| 175 | // normal cleanup; otherwise there really shouldn't be any fixups. |
| 176 | assert(hasNormalCleanups()); |
| 177 | |
| 178 | EHScopeStack::iterator it = find(InnermostNormalCleanup); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame^] | 179 | unsigned MinSize; |
| 180 | if (isa<EHCleanupScope>(*it)) |
| 181 | MinSize = cast<EHCleanupScope>(*it).getFixupDepth(); |
| 182 | else |
| 183 | MinSize = cast<EHLazyCleanupScope>(*it).getFixupDepth(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 184 | assert(BranchFixups.size() >= MinSize && "fixup stack out of order"); |
| 185 | |
| 186 | while (BranchFixups.size() > MinSize && |
| 187 | BranchFixups.back().Destination == 0) |
| 188 | BranchFixups.pop_back(); |
| 189 | } |
| 190 | |
| 191 | void EHScopeStack::resolveBranchFixups(llvm::BasicBlock *Dest) { |
| 192 | assert(Dest && "null block passed to resolveBranchFixups"); |
| 193 | |
| 194 | if (BranchFixups.empty()) return; |
| 195 | assert(hasNormalCleanups() && |
| 196 | "branch fixups exist with no normal cleanups on stack"); |
| 197 | |
| 198 | for (unsigned I = 0, E = BranchFixups.size(); I != E; ++I) |
| 199 | if (BranchFixups[I].Destination == Dest) |
| 200 | BranchFixups[I].Destination = 0; |
| 201 | |
| 202 | popNullFixups(); |
| 203 | } |
| 204 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 205 | static llvm::Constant *getAllocateExceptionFn(CodeGenFunction &CGF) { |
| 206 | // void *__cxa_allocate_exception(size_t thrown_size); |
| 207 | const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); |
| 208 | std::vector<const llvm::Type*> Args(1, SizeTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 209 | |
| 210 | const llvm::FunctionType *FTy = |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 211 | llvm::FunctionType::get(llvm::Type::getInt8PtrTy(CGF.getLLVMContext()), |
| 212 | Args, false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 213 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 214 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception"); |
| 215 | } |
| 216 | |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 217 | static llvm::Constant *getFreeExceptionFn(CodeGenFunction &CGF) { |
| 218 | // void __cxa_free_exception(void *thrown_exception); |
| 219 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 220 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 221 | |
| 222 | const llvm::FunctionType *FTy = |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 223 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 224 | Args, false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 225 | |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 226 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception"); |
| 227 | } |
| 228 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 229 | static llvm::Constant *getThrowFn(CodeGenFunction &CGF) { |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 230 | // void __cxa_throw(void *thrown_exception, std::type_info *tinfo, |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 231 | // void (*dest) (void *)); |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 232 | |
| 233 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 234 | std::vector<const llvm::Type*> Args(3, Int8PtrTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 235 | |
| 236 | const llvm::FunctionType *FTy = |
Mike Stump | b4eea69 | 2009-11-20 00:56:31 +0000 | [diff] [blame] | 237 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 238 | Args, false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 239 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 240 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw"); |
| 241 | } |
| 242 | |
Mike Stump | b4eea69 | 2009-11-20 00:56:31 +0000 | [diff] [blame] | 243 | static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) { |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 244 | // void __cxa_rethrow(); |
Mike Stump | b4eea69 | 2009-11-20 00:56:31 +0000 | [diff] [blame] | 245 | |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 246 | const llvm::FunctionType *FTy = |
Mike Stump | b4eea69 | 2009-11-20 00:56:31 +0000 | [diff] [blame] | 247 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 248 | |
Mike Stump | b4eea69 | 2009-11-20 00:56:31 +0000 | [diff] [blame] | 249 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow"); |
| 250 | } |
| 251 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 252 | static llvm::Constant *getGetExceptionPtrFn(CodeGenFunction &CGF) { |
| 253 | // void *__cxa_get_exception_ptr(void*); |
| 254 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 255 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
| 256 | |
| 257 | const llvm::FunctionType *FTy = |
| 258 | llvm::FunctionType::get(Int8PtrTy, Args, false); |
| 259 | |
| 260 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr"); |
| 261 | } |
| 262 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 263 | static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 264 | // void *__cxa_begin_catch(void*); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 265 | |
| 266 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 267 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 268 | |
| 269 | const llvm::FunctionType *FTy = |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 270 | llvm::FunctionType::get(Int8PtrTy, Args, false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 271 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 272 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch"); |
| 273 | } |
| 274 | |
| 275 | static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) { |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 276 | // void __cxa_end_catch(); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 277 | |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 278 | const llvm::FunctionType *FTy = |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 279 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 280 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 281 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch"); |
| 282 | } |
| 283 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 284 | static llvm::Constant *getUnexpectedFn(CodeGenFunction &CGF) { |
| 285 | // void __cxa_call_unexepcted(void *thrown_exception); |
| 286 | |
| 287 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 288 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 289 | |
| 290 | const llvm::FunctionType *FTy = |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 291 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 292 | Args, false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 293 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 294 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected"); |
| 295 | } |
| 296 | |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 297 | llvm::Constant *CodeGenFunction::getUnwindResumeOrRethrowFn() { |
| 298 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 299 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 300 | |
| 301 | const llvm::FunctionType *FTy = |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 302 | llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), Args, |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 303 | false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 304 | |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 305 | if (CGM.getLangOptions().SjLjExceptions) |
| 306 | return CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume"); |
| 307 | return CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow"); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 310 | static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) { |
| 311 | // void __terminate(); |
| 312 | |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 313 | const llvm::FunctionType *FTy = |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 314 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 315 | |
David Chisnall | 79a9ad8 | 2010-05-17 13:49:20 +0000 | [diff] [blame] | 316 | return CGF.CGM.CreateRuntimeFunction(FTy, |
| 317 | CGF.CGM.getLangOptions().CPlusPlus ? "_ZSt9terminatev" : "abort"); |
| 318 | } |
| 319 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 320 | static const char *getCPersonalityFn(CodeGenFunction &CGF) { |
| 321 | return "__gcc_personality_v0"; |
| 322 | } |
| 323 | |
| 324 | static const char *getObjCPersonalityFn(CodeGenFunction &CGF) { |
| 325 | if (CGF.CGM.getLangOptions().NeXTRuntime) { |
| 326 | if (CGF.CGM.getLangOptions().ObjCNonFragileABI) |
| 327 | return "__objc_personality_v0"; |
Daniel Dunbar | 8019c45 | 2010-05-28 19:43:36 +0000 | [diff] [blame] | 328 | else |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 329 | return getCPersonalityFn(CGF); |
| 330 | } else { |
| 331 | return "__gnu_objc_personality_v0"; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | static const char *getCXXPersonalityFn(CodeGenFunction &CGF) { |
| 336 | if (CGF.CGM.getLangOptions().SjLjExceptions) |
| 337 | return "__gxx_personality_sj0"; |
| 338 | else |
| 339 | return "__gxx_personality_v0"; |
| 340 | } |
| 341 | |
| 342 | /// Determines the personality function to use when both C++ |
| 343 | /// and Objective-C exceptions are being caught. |
| 344 | static const char *getObjCXXPersonalityFn(CodeGenFunction &CGF) { |
| 345 | // The ObjC personality defers to the C++ personality for non-ObjC |
| 346 | // handlers. Unlike the C++ case, we use the same personality |
| 347 | // function on targets using (backend-driven) SJLJ EH. |
| 348 | if (CGF.CGM.getLangOptions().NeXTRuntime) { |
| 349 | if (CGF.CGM.getLangOptions().ObjCNonFragileABI) |
| 350 | return "__objc_personality_v0"; |
| 351 | |
| 352 | // In the fragile ABI, just use C++ exception handling and hope |
| 353 | // they're not doing crazy exception mixing. |
| 354 | else |
| 355 | return getCXXPersonalityFn(CGF); |
Chandler Carruth | dcf22ad | 2010-05-17 20:58:49 +0000 | [diff] [blame] | 356 | } |
David Chisnall | 79a9ad8 | 2010-05-17 13:49:20 +0000 | [diff] [blame] | 357 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 358 | // I'm pretty sure the GNU runtime doesn't support mixed EH. |
| 359 | // TODO: we don't necessarily need mixed EH here; remember what |
| 360 | // kind of exceptions we actually try to catch in this function. |
| 361 | CGF.CGM.ErrorUnsupported(CGF.CurCodeDecl, |
| 362 | "the GNU Objective C runtime does not support " |
| 363 | "catching C++ and Objective C exceptions in the " |
| 364 | "same function"); |
| 365 | // Use the C++ personality just to avoid returning null. |
| 366 | return getCXXPersonalityFn(CGF); |
| 367 | } |
| 368 | |
| 369 | static llvm::Constant *getPersonalityFn(CodeGenFunction &CGF) { |
| 370 | const char *Name; |
| 371 | const LangOptions &Opts = CGF.CGM.getLangOptions(); |
| 372 | if (Opts.CPlusPlus && Opts.ObjC1) |
| 373 | Name = getObjCXXPersonalityFn(CGF); |
| 374 | else if (Opts.CPlusPlus) |
| 375 | Name = getCXXPersonalityFn(CGF); |
| 376 | else if (Opts.ObjC1) |
| 377 | Name = getObjCPersonalityFn(CGF); |
| 378 | else |
| 379 | Name = getCPersonalityFn(CGF); |
| 380 | |
David Chisnall | 79a9ad8 | 2010-05-17 13:49:20 +0000 | [diff] [blame] | 381 | llvm::Constant *Personality = |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 382 | CGF.CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
| 383 | llvm::Type::getInt32Ty( |
| 384 | CGF.CGM.getLLVMContext()), |
| 385 | true), |
| 386 | Name); |
| 387 | return llvm::ConstantExpr::getBitCast(Personality, CGF.CGM.PtrToInt8Ty); |
| 388 | } |
| 389 | |
| 390 | /// Returns the value to inject into a selector to indicate the |
| 391 | /// presence of a catch-all. |
| 392 | static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) { |
| 393 | // Possibly we should use @llvm.eh.catch.all.value here. |
| 394 | return llvm::ConstantPointerNull::get(CGF.CGM.PtrToInt8Ty); |
| 395 | } |
| 396 | |
| 397 | /// Returns the value to inject into a selector to indicate the |
| 398 | /// presence of a cleanup. |
| 399 | static llvm::Constant *getCleanupValue(CodeGenFunction &CGF) { |
| 400 | return llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0); |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 401 | } |
| 402 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 403 | // Emits an exception expression into the given location. This |
| 404 | // differs from EmitAnyExprToMem only in that, if a final copy-ctor |
| 405 | // call is required, an exception within that copy ctor causes |
| 406 | // std::terminate to be invoked. |
| 407 | static void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *E, |
| 408 | llvm::Value *ExnLoc) { |
| 409 | // We want to release the allocated exception object if this |
| 410 | // expression throws. We do this by pushing an EH-only cleanup |
| 411 | // block which, furthermore, deactivates itself after the expression |
| 412 | // is complete. |
| 413 | llvm::AllocaInst *ShouldFreeVar = |
| 414 | CGF.CreateTempAlloca(llvm::Type::getInt1Ty(CGF.getLLVMContext()), |
| 415 | "should-free-exnobj.var"); |
| 416 | CGF.InitTempAlloca(ShouldFreeVar, |
| 417 | llvm::ConstantInt::getFalse(CGF.getLLVMContext())); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 418 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 419 | // A variable holding the exception pointer. This is necessary |
| 420 | // because the throw expression does not necessarily dominate the |
| 421 | // cleanup, for example if it appears in a conditional expression. |
| 422 | llvm::AllocaInst *ExnLocVar = |
| 423 | CGF.CreateTempAlloca(ExnLoc->getType(), "exnobj.var"); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 424 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 425 | // Make sure the exception object is cleaned up if there's an |
| 426 | // exception during initialization. |
| 427 | // FIXME: StmtExprs probably force this to include a non-EH |
| 428 | // handler. |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 429 | { |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame^] | 430 | CodeGenFunction::CleanupBlock Cleanup(CGF, EHCleanup); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 431 | llvm::BasicBlock *FreeBB = CGF.createBasicBlock("free-exnobj"); |
| 432 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock("free-exnobj.done"); |
Mike Stump | f2945c0 | 2009-12-17 06:08:47 +0000 | [diff] [blame] | 433 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 434 | llvm::Value *ShouldFree = CGF.Builder.CreateLoad(ShouldFreeVar, |
| 435 | "should-free-exnobj"); |
| 436 | CGF.Builder.CreateCondBr(ShouldFree, FreeBB, DoneBB); |
| 437 | CGF.EmitBlock(FreeBB); |
| 438 | llvm::Value *ExnLocLocal = CGF.Builder.CreateLoad(ExnLocVar, "exnobj"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 439 | CGF.Builder.CreateCall(getFreeExceptionFn(CGF), ExnLocLocal) |
| 440 | ->setDoesNotThrow(); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 441 | CGF.EmitBlock(DoneBB); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 442 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 443 | EHScopeStack::stable_iterator Cleanup = CGF.EHStack.stable_begin(); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 444 | |
| 445 | CGF.Builder.CreateStore(ExnLoc, ExnLocVar); |
| 446 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(CGF.getLLVMContext()), |
| 447 | ShouldFreeVar); |
| 448 | |
| 449 | // __cxa_allocate_exception returns a void*; we need to cast this |
| 450 | // to the appropriate type for the object. |
| 451 | const llvm::Type *Ty = CGF.ConvertType(E->getType())->getPointerTo(); |
| 452 | llvm::Value *TypedExnLoc = CGF.Builder.CreateBitCast(ExnLoc, Ty); |
| 453 | |
| 454 | // FIXME: this isn't quite right! If there's a final unelided call |
| 455 | // to a copy constructor, then according to [except.terminate]p1 we |
| 456 | // must call std::terminate() if that constructor throws, because |
| 457 | // technically that copy occurs after the exception expression is |
| 458 | // evaluated but before the exception is caught. But the best way |
| 459 | // to handle that is to teach EmitAggExpr to do the final copy |
| 460 | // differently if it can't be elided. |
| 461 | CGF.EmitAnyExprToMem(E, TypedExnLoc, /*Volatile*/ false); |
| 462 | |
| 463 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()), |
| 464 | ShouldFreeVar); |
| 465 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 466 | // Technically, the exception object is like a temporary; it has to |
| 467 | // be cleaned up when its full-expression is complete. |
| 468 | // Unfortunately, the AST represents full-expressions by creating a |
| 469 | // CXXExprWithTemporaries, which it only does when there are actually |
| 470 | // temporaries. |
| 471 | // |
| 472 | // If any cleanups have been added since we pushed ours, they must |
| 473 | // be from temporaries; this will get popped at the same time. |
| 474 | // Otherwise we need to pop ours off. FIXME: this is very brittle. |
| 475 | if (Cleanup == CGF.EHStack.stable_begin()) |
| 476 | CGF.PopCleanupBlock(); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 477 | } |
| 478 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 479 | llvm::Value *CodeGenFunction::getExceptionSlot() { |
| 480 | if (!ExceptionSlot) { |
| 481 | const llvm::Type *i8p = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 482 | ExceptionSlot = CreateTempAlloca(i8p, "exn.slot"); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 483 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 484 | return ExceptionSlot; |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 487 | void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) { |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 488 | if (!E->getSubExpr()) { |
Douglas Gregor | 1eb2e59 | 2010-05-16 00:44:00 +0000 | [diff] [blame] | 489 | if (getInvokeDest()) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 490 | Builder.CreateInvoke(getReThrowFn(*this), |
| 491 | getUnreachableBlock(), |
| 492 | getInvokeDest()) |
Douglas Gregor | 1eb2e59 | 2010-05-16 00:44:00 +0000 | [diff] [blame] | 493 | ->setDoesNotReturn(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 494 | } else { |
Douglas Gregor | 1eb2e59 | 2010-05-16 00:44:00 +0000 | [diff] [blame] | 495 | Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 496 | Builder.CreateUnreachable(); |
| 497 | } |
Douglas Gregor | 1eb2e59 | 2010-05-16 00:44:00 +0000 | [diff] [blame] | 498 | |
| 499 | // Clear the insertion point to indicate we are in unreachable code. |
| 500 | Builder.ClearInsertionPoint(); |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 501 | return; |
| 502 | } |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 503 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 504 | QualType ThrowType = E->getSubExpr()->getType(); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 505 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 506 | // Now allocate the exception object. |
| 507 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 508 | uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity(); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 509 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 510 | llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 511 | llvm::CallInst *ExceptionPtr = |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 512 | Builder.CreateCall(AllocExceptionFn, |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 513 | llvm::ConstantInt::get(SizeTy, TypeSize), |
| 514 | "exception"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 515 | ExceptionPtr->setDoesNotThrow(); |
Anders Carlsson | 8370c58 | 2009-12-11 00:32:37 +0000 | [diff] [blame] | 516 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 517 | EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 518 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 519 | // Now throw the exception. |
| 520 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
John McCall | 9dffe6f | 2010-04-30 01:15:21 +0000 | [diff] [blame] | 521 | llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType, true); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 522 | |
| 523 | // The address of the destructor. If the exception type has a |
| 524 | // trivial destructor (or isn't a record), we just pass null. |
| 525 | llvm::Constant *Dtor = 0; |
| 526 | if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) { |
| 527 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 528 | if (!Record->hasTrivialDestructor()) { |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 529 | CXXDestructorDecl *DtorD = Record->getDestructor(); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 530 | Dtor = CGM.GetAddrOfCXXDestructor(DtorD, Dtor_Complete); |
| 531 | Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy); |
| 532 | } |
| 533 | } |
| 534 | if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 535 | |
Mike Stump | 0a3816e | 2009-12-04 01:51:45 +0000 | [diff] [blame] | 536 | if (getInvokeDest()) { |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 537 | llvm::InvokeInst *ThrowCall = |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 538 | Builder.CreateInvoke3(getThrowFn(*this), |
| 539 | getUnreachableBlock(), getInvokeDest(), |
Mike Stump | 0a3816e | 2009-12-04 01:51:45 +0000 | [diff] [blame] | 540 | ExceptionPtr, TypeInfo, Dtor); |
| 541 | ThrowCall->setDoesNotReturn(); |
Mike Stump | 0a3816e | 2009-12-04 01:51:45 +0000 | [diff] [blame] | 542 | } else { |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 543 | llvm::CallInst *ThrowCall = |
Mike Stump | 0a3816e | 2009-12-04 01:51:45 +0000 | [diff] [blame] | 544 | Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor); |
| 545 | ThrowCall->setDoesNotReturn(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 546 | Builder.CreateUnreachable(); |
Mike Stump | 0a3816e | 2009-12-04 01:51:45 +0000 | [diff] [blame] | 547 | } |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 548 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 549 | // Clear the insertion point to indicate we are in unreachable code. |
| 550 | Builder.ClearInsertionPoint(); |
Mike Stump | c2ab486 | 2009-12-07 20:12:14 +0000 | [diff] [blame] | 551 | |
| 552 | // FIXME: For now, emit a dummy basic block because expr emitters in generally |
| 553 | // are not ready to handle emitting expressions at unreachable points. |
| 554 | EnsureInsertPoint(); |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 555 | } |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 556 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 557 | void CodeGenFunction::EmitStartEHSpec(const Decl *D) { |
Anders Carlsson | a994ee4 | 2010-02-06 23:59:05 +0000 | [diff] [blame] | 558 | if (!Exceptions) |
| 559 | return; |
| 560 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 561 | const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); |
| 562 | if (FD == 0) |
| 563 | return; |
| 564 | const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); |
| 565 | if (Proto == 0) |
| 566 | return; |
| 567 | |
| 568 | assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack"); |
| 569 | |
| 570 | if (!Proto->hasExceptionSpec()) |
| 571 | return; |
| 572 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 573 | unsigned NumExceptions = Proto->getNumExceptions(); |
| 574 | EHFilterScope *Filter = EHStack.pushFilter(NumExceptions); |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 575 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 576 | for (unsigned I = 0; I != NumExceptions; ++I) { |
| 577 | QualType Ty = Proto->getExceptionType(I); |
| 578 | QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType(); |
John McCall | 9dffe6f | 2010-04-30 01:15:21 +0000 | [diff] [blame] | 579 | llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType, true); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 580 | Filter->setFilter(I, EHType); |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 581 | } |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | void CodeGenFunction::EmitEndEHSpec(const Decl *D) { |
Anders Carlsson | a994ee4 | 2010-02-06 23:59:05 +0000 | [diff] [blame] | 585 | if (!Exceptions) |
| 586 | return; |
| 587 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 588 | const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); |
| 589 | if (FD == 0) |
| 590 | return; |
| 591 | const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); |
| 592 | if (Proto == 0) |
| 593 | return; |
| 594 | |
| 595 | if (!Proto->hasExceptionSpec()) |
| 596 | return; |
| 597 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 598 | EHStack.popFilter(); |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 601 | void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 602 | EnterCXXTryStmt(S); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 603 | EmitStmt(S.getTryBlock()); |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 604 | ExitCXXTryStmt(S); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 605 | } |
| 606 | |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 607 | void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 608 | unsigned NumHandlers = S.getNumHandlers(); |
| 609 | EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 610 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 611 | for (unsigned I = 0; I != NumHandlers; ++I) { |
| 612 | const CXXCatchStmt *C = S.getHandler(I); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 613 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 614 | llvm::BasicBlock *Handler = createBasicBlock("catch"); |
| 615 | if (C->getExceptionDecl()) { |
| 616 | // FIXME: Dropping the reference type on the type into makes it |
| 617 | // impossible to correctly implement catch-by-reference |
| 618 | // semantics for pointers. Unfortunately, this is what all |
| 619 | // existing compilers do, and it's not clear that the standard |
| 620 | // personality routine is capable of doing this right. See C++ DR 388: |
| 621 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#388 |
| 622 | QualType CaughtType = C->getCaughtType(); |
| 623 | CaughtType = CaughtType.getNonReferenceType().getUnqualifiedType(); |
| 624 | llvm::Value *TypeInfo = CGM.GetAddrOfRTTIDescriptor(CaughtType, true); |
| 625 | CatchScope->setHandler(I, TypeInfo, Handler); |
| 626 | } else { |
| 627 | // No exception decl indicates '...', a catch-all. |
| 628 | CatchScope->setCatchAllHandler(I, Handler); |
| 629 | } |
| 630 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /// Check whether this is a non-EH scope, i.e. a scope which doesn't |
| 634 | /// affect exception handling. Currently, the only non-EH scopes are |
| 635 | /// normal-only cleanup scopes. |
| 636 | static bool isNonEHScope(const EHScope &S) { |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame^] | 637 | switch (S.getKind()) { |
| 638 | case EHScope::Cleanup: |
| 639 | return !cast<EHCleanupScope>(S).isEHCleanup(); |
| 640 | case EHScope::LazyCleanup: |
| 641 | return !cast<EHLazyCleanupScope>(S).isEHCleanup(); |
| 642 | case EHScope::Filter: |
| 643 | case EHScope::Catch: |
| 644 | case EHScope::Terminate: |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | // Suppress warning. |
| 649 | return false; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | llvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() { |
| 653 | assert(EHStack.requiresLandingPad()); |
| 654 | assert(!EHStack.empty()); |
| 655 | |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame^] | 656 | if (!Exceptions) |
| 657 | return 0; |
| 658 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 659 | // Check the innermost scope for a cached landing pad. If this is |
| 660 | // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad. |
| 661 | llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad(); |
| 662 | if (LP) return LP; |
| 663 | |
| 664 | // Build the landing pad for this scope. |
| 665 | LP = EmitLandingPad(); |
| 666 | assert(LP); |
| 667 | |
| 668 | // Cache the landing pad on the innermost scope. If this is a |
| 669 | // non-EH scope, cache the landing pad on the enclosing scope, too. |
| 670 | for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) { |
| 671 | ir->setCachedLandingPad(LP); |
| 672 | if (!isNonEHScope(*ir)) break; |
| 673 | } |
| 674 | |
| 675 | return LP; |
| 676 | } |
| 677 | |
| 678 | llvm::BasicBlock *CodeGenFunction::EmitLandingPad() { |
| 679 | assert(EHStack.requiresLandingPad()); |
| 680 | |
| 681 | // This function contains a hack to work around a design flaw in |
| 682 | // LLVM's EH IR which breaks semantics after inlining. This same |
| 683 | // hack is implemented in llvm-gcc. |
| 684 | // |
| 685 | // The LLVM EH abstraction is basically a thin veneer over the |
| 686 | // traditional GCC zero-cost design: for each range of instructions |
| 687 | // in the function, there is (at most) one "landing pad" with an |
| 688 | // associated chain of EH actions. A language-specific personality |
| 689 | // function interprets this chain of actions and (1) decides whether |
| 690 | // or not to resume execution at the landing pad and (2) if so, |
| 691 | // provides an integer indicating why it's stopping. In LLVM IR, |
| 692 | // the association of a landing pad with a range of instructions is |
| 693 | // achieved via an invoke instruction, the chain of actions becomes |
| 694 | // the arguments to the @llvm.eh.selector call, and the selector |
| 695 | // call returns the integer indicator. Other than the required |
| 696 | // presence of two intrinsic function calls in the landing pad, |
| 697 | // the IR exactly describes the layout of the output code. |
| 698 | // |
| 699 | // A principal advantage of this design is that it is completely |
| 700 | // language-agnostic; in theory, the LLVM optimizers can treat |
| 701 | // landing pads neutrally, and targets need only know how to lower |
| 702 | // the intrinsics to have a functioning exceptions system (assuming |
| 703 | // that platform exceptions follow something approximately like the |
| 704 | // GCC design). Unfortunately, landing pads cannot be combined in a |
| 705 | // language-agnostic way: given selectors A and B, there is no way |
| 706 | // to make a single landing pad which faithfully represents the |
| 707 | // semantics of propagating an exception first through A, then |
| 708 | // through B, without knowing how the personality will interpret the |
| 709 | // (lowered form of the) selectors. This means that inlining has no |
| 710 | // choice but to crudely chain invokes (i.e., to ignore invokes in |
| 711 | // the inlined function, but to turn all unwindable calls into |
| 712 | // invokes), which is only semantically valid if every unwind stops |
| 713 | // at every landing pad. |
| 714 | // |
| 715 | // Therefore, the invoke-inline hack is to guarantee that every |
| 716 | // landing pad has a catch-all. |
| 717 | const bool UseInvokeInlineHack = true; |
| 718 | |
| 719 | for (EHScopeStack::iterator ir = EHStack.begin(); ; ) { |
| 720 | assert(ir != EHStack.end() && |
| 721 | "stack requiring landing pad is nothing but non-EH scopes?"); |
| 722 | |
| 723 | // If this is a terminate scope, just use the singleton terminate |
| 724 | // landing pad. |
| 725 | if (isa<EHTerminateScope>(*ir)) |
| 726 | return getTerminateLandingPad(); |
| 727 | |
| 728 | // If this isn't an EH scope, iterate; otherwise break out. |
| 729 | if (!isNonEHScope(*ir)) break; |
| 730 | ++ir; |
| 731 | |
| 732 | // We haven't checked this scope for a cached landing pad yet. |
| 733 | if (llvm::BasicBlock *LP = ir->getCachedLandingPad()) |
| 734 | return LP; |
| 735 | } |
| 736 | |
| 737 | // Save the current IR generation state. |
| 738 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
| 739 | |
| 740 | // Create and configure the landing pad. |
| 741 | llvm::BasicBlock *LP = createBasicBlock("lpad"); |
| 742 | EmitBlock(LP); |
| 743 | |
| 744 | // Save the exception pointer. It's safe to use a single exception |
| 745 | // pointer per function because EH cleanups can never have nested |
| 746 | // try/catches. |
| 747 | llvm::CallInst *Exn = |
| 748 | Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::eh_exception), "exn"); |
| 749 | Exn->setDoesNotThrow(); |
| 750 | Builder.CreateStore(Exn, getExceptionSlot()); |
| 751 | |
| 752 | // Build the selector arguments. |
| 753 | llvm::SmallVector<llvm::Value*, 8> EHSelector; |
| 754 | EHSelector.push_back(Exn); |
| 755 | EHSelector.push_back(getPersonalityFn(*this)); |
| 756 | |
| 757 | // Accumulate all the handlers in scope. |
| 758 | llvm::DenseMap<llvm::Value*, JumpDest> EHHandlers; |
| 759 | JumpDest CatchAll; |
| 760 | bool HasEHCleanup = false; |
| 761 | bool HasEHFilter = false; |
| 762 | llvm::SmallVector<llvm::Value*, 8> EHFilters; |
| 763 | for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end(); |
| 764 | I != E; ++I) { |
| 765 | |
| 766 | switch (I->getKind()) { |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame^] | 767 | case EHScope::LazyCleanup: |
| 768 | if (!HasEHCleanup) |
| 769 | HasEHCleanup = cast<EHLazyCleanupScope>(*I).isEHCleanup(); |
| 770 | // We otherwise don't care about cleanups. |
| 771 | continue; |
| 772 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 773 | case EHScope::Cleanup: |
| 774 | if (!HasEHCleanup) |
| 775 | HasEHCleanup = cast<EHCleanupScope>(*I).isEHCleanup(); |
| 776 | // We otherwise don't care about cleanups. |
| 777 | continue; |
| 778 | |
| 779 | case EHScope::Filter: { |
| 780 | assert(I.next() == EHStack.end() && "EH filter is not end of EH stack"); |
| 781 | assert(!CatchAll.Block && "EH filter reached after catch-all"); |
| 782 | |
| 783 | // Filter scopes get added to the selector in wierd ways. |
| 784 | EHFilterScope &Filter = cast<EHFilterScope>(*I); |
| 785 | HasEHFilter = true; |
| 786 | |
| 787 | // Add all the filter values which we aren't already explicitly |
| 788 | // catching. |
| 789 | for (unsigned I = 0, E = Filter.getNumFilters(); I != E; ++I) { |
| 790 | llvm::Value *FV = Filter.getFilter(I); |
| 791 | if (!EHHandlers.count(FV)) |
| 792 | EHFilters.push_back(FV); |
| 793 | } |
| 794 | goto done; |
| 795 | } |
| 796 | |
| 797 | case EHScope::Terminate: |
| 798 | // Terminate scopes are basically catch-alls. |
| 799 | assert(!CatchAll.Block); |
| 800 | CatchAll.Block = getTerminateHandler(); |
| 801 | CatchAll.ScopeDepth = EHStack.getEnclosingEHCleanup(I); |
| 802 | goto done; |
| 803 | |
| 804 | case EHScope::Catch: |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | EHCatchScope &Catch = cast<EHCatchScope>(*I); |
| 809 | for (unsigned HI = 0, HE = Catch.getNumHandlers(); HI != HE; ++HI) { |
| 810 | EHCatchScope::Handler Handler = Catch.getHandler(HI); |
| 811 | |
| 812 | // Catch-all. We should only have one of these per catch. |
| 813 | if (!Handler.Type) { |
| 814 | assert(!CatchAll.Block); |
| 815 | CatchAll.Block = Handler.Block; |
| 816 | CatchAll.ScopeDepth = EHStack.getEnclosingEHCleanup(I); |
| 817 | continue; |
| 818 | } |
| 819 | |
| 820 | // Check whether we already have a handler for this type. |
| 821 | JumpDest &Dest = EHHandlers[Handler.Type]; |
| 822 | if (Dest.Block) continue; |
| 823 | |
| 824 | EHSelector.push_back(Handler.Type); |
| 825 | Dest.Block = Handler.Block; |
| 826 | Dest.ScopeDepth = EHStack.getEnclosingEHCleanup(I); |
| 827 | } |
| 828 | |
| 829 | // Stop if we found a catch-all. |
| 830 | if (CatchAll.Block) break; |
| 831 | } |
| 832 | |
| 833 | done: |
| 834 | unsigned LastToEmitInLoop = EHSelector.size(); |
| 835 | |
| 836 | // If we have a catch-all, add null to the selector. |
| 837 | if (CatchAll.Block) { |
| 838 | EHSelector.push_back(getCatchAllValue(CGF)); |
| 839 | |
| 840 | // If we have an EH filter, we need to add those handlers in the |
| 841 | // right place in the selector, which is to say, at the end. |
| 842 | } else if (HasEHFilter) { |
| 843 | // Create a filter expression: an integer constant saying how many |
| 844 | // filters there are (+1 to avoid ambiguity with 0 for cleanup), |
| 845 | // followed by the filter types. The personality routine only |
| 846 | // lands here if the filter doesn't match. |
| 847 | EHSelector.push_back(llvm::ConstantInt::get(Builder.getInt32Ty(), |
| 848 | EHFilters.size() + 1)); |
| 849 | EHSelector.append(EHFilters.begin(), EHFilters.end()); |
| 850 | |
| 851 | // Also check whether we need a cleanup. |
| 852 | if (UseInvokeInlineHack || HasEHCleanup) |
| 853 | EHSelector.push_back(UseInvokeInlineHack |
| 854 | ? getCatchAllValue(CGF) |
| 855 | : getCleanupValue(CGF)); |
| 856 | |
| 857 | // Otherwise, signal that we at least have cleanups. |
| 858 | } else if (UseInvokeInlineHack || HasEHCleanup) { |
| 859 | EHSelector.push_back(UseInvokeInlineHack |
| 860 | ? getCatchAllValue(CGF) |
| 861 | : getCleanupValue(CGF)); |
| 862 | } else { |
| 863 | assert(LastToEmitInLoop > 2); |
| 864 | LastToEmitInLoop--; |
| 865 | } |
| 866 | |
| 867 | assert(EHSelector.size() >= 3 && "selector call has only two arguments!"); |
| 868 | |
| 869 | // Tell the backend how to generate the landing pad. |
| 870 | llvm::CallInst *Selection = |
| 871 | Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::eh_selector), |
| 872 | EHSelector.begin(), EHSelector.end(), "eh.selector"); |
| 873 | Selection->setDoesNotThrow(); |
| 874 | |
| 875 | // Select the right handler. |
| 876 | llvm::Value *llvm_eh_typeid_for = |
| 877 | CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); |
| 878 | |
| 879 | // The results of llvm_eh_typeid_for aren't reliable --- at least |
| 880 | // not locally --- so we basically have to do this as an 'if' chain. |
| 881 | // We walk through the first N-1 catch clauses, testing and chaining, |
| 882 | // and then fall into the final clause (which is either a cleanup, a |
| 883 | // filter (possibly with a cleanup), a catch-all, or another catch). |
| 884 | for (unsigned I = 2; I != LastToEmitInLoop; ++I) { |
| 885 | llvm::Value *Type = EHSelector[I]; |
| 886 | JumpDest Dest = EHHandlers[Type]; |
| 887 | assert(Dest.Block && "no handler entry for value in selector?"); |
| 888 | |
| 889 | // Figure out where to branch on a match. As a debug code-size |
| 890 | // optimization, if the scope depth matches the innermost cleanup, |
| 891 | // we branch directly to the catch handler. |
| 892 | llvm::BasicBlock *Match = Dest.Block; |
| 893 | bool MatchNeedsCleanup = Dest.ScopeDepth != EHStack.getInnermostEHCleanup(); |
| 894 | if (MatchNeedsCleanup) |
| 895 | Match = createBasicBlock("eh.match"); |
| 896 | |
| 897 | llvm::BasicBlock *Next = createBasicBlock("eh.next"); |
| 898 | |
| 899 | // Check whether the exception matches. |
| 900 | llvm::CallInst *Id |
| 901 | = Builder.CreateCall(llvm_eh_typeid_for, |
| 902 | Builder.CreateBitCast(Type, CGM.PtrToInt8Ty)); |
| 903 | Id->setDoesNotThrow(); |
| 904 | Builder.CreateCondBr(Builder.CreateICmpEQ(Selection, Id), |
| 905 | Match, Next); |
| 906 | |
| 907 | // Emit match code if necessary. |
| 908 | if (MatchNeedsCleanup) { |
| 909 | EmitBlock(Match); |
| 910 | EmitBranchThroughEHCleanup(Dest); |
| 911 | } |
| 912 | |
| 913 | // Continue to the next match. |
| 914 | EmitBlock(Next); |
| 915 | } |
| 916 | |
| 917 | // Emit the final case in the selector. |
| 918 | // This might be a catch-all.... |
| 919 | if (CatchAll.Block) { |
| 920 | assert(isa<llvm::ConstantPointerNull>(EHSelector.back())); |
| 921 | EmitBranchThroughEHCleanup(CatchAll); |
| 922 | |
| 923 | // ...or an EH filter... |
| 924 | } else if (HasEHFilter) { |
| 925 | llvm::Value *SavedSelection = Selection; |
| 926 | |
| 927 | // First, unwind out to the outermost scope if necessary. |
| 928 | if (EHStack.hasEHCleanups()) { |
| 929 | // The end here might not dominate the beginning, so we might need to |
| 930 | // save the selector if we need it. |
| 931 | llvm::AllocaInst *SelectorVar = 0; |
| 932 | if (HasEHCleanup) { |
| 933 | SelectorVar = CreateTempAlloca(Builder.getInt32Ty(), "selector.var"); |
| 934 | Builder.CreateStore(Selection, SelectorVar); |
| 935 | } |
| 936 | |
| 937 | llvm::BasicBlock *CleanupContBB = createBasicBlock("ehspec.cleanup.cont"); |
| 938 | EmitBranchThroughEHCleanup(JumpDest(CleanupContBB, EHStack.stable_end())); |
| 939 | EmitBlock(CleanupContBB); |
| 940 | |
| 941 | if (HasEHCleanup) |
| 942 | SavedSelection = Builder.CreateLoad(SelectorVar, "ehspec.saved-selector"); |
| 943 | } |
| 944 | |
| 945 | // If there was a cleanup, we'll need to actually check whether we |
| 946 | // landed here because the filter triggered. |
| 947 | if (UseInvokeInlineHack || HasEHCleanup) { |
| 948 | llvm::BasicBlock *RethrowBB = createBasicBlock("cleanup"); |
| 949 | llvm::BasicBlock *UnexpectedBB = createBasicBlock("ehspec.unexpected"); |
| 950 | |
| 951 | llvm::Constant *Zero = llvm::ConstantInt::get(Builder.getInt32Ty(), 0); |
| 952 | llvm::Value *FailsFilter = |
| 953 | Builder.CreateICmpSLT(SavedSelection, Zero, "ehspec.fails"); |
| 954 | Builder.CreateCondBr(FailsFilter, UnexpectedBB, RethrowBB); |
| 955 | |
| 956 | // The rethrow block is where we land if this was a cleanup. |
| 957 | // TODO: can this be _Unwind_Resume if the InvokeInlineHack is off? |
| 958 | EmitBlock(RethrowBB); |
| 959 | Builder.CreateCall(getUnwindResumeOrRethrowFn(), |
| 960 | Builder.CreateLoad(getExceptionSlot())) |
| 961 | ->setDoesNotReturn(); |
| 962 | Builder.CreateUnreachable(); |
| 963 | |
| 964 | EmitBlock(UnexpectedBB); |
| 965 | } |
| 966 | |
| 967 | // Call __cxa_call_unexpected. This doesn't need to be an invoke |
| 968 | // because __cxa_call_unexpected magically filters exceptions |
| 969 | // according to the last landing pad the exception was thrown |
| 970 | // into. Seriously. |
| 971 | Builder.CreateCall(getUnexpectedFn(*this), |
| 972 | Builder.CreateLoad(getExceptionSlot())) |
| 973 | ->setDoesNotReturn(); |
| 974 | Builder.CreateUnreachable(); |
| 975 | |
| 976 | // ...or a normal catch handler... |
| 977 | } else if (!UseInvokeInlineHack && !HasEHCleanup) { |
| 978 | llvm::Value *Type = EHSelector.back(); |
| 979 | EmitBranchThroughEHCleanup(EHHandlers[Type]); |
| 980 | |
| 981 | // ...or a cleanup. |
| 982 | } else { |
| 983 | // We emit a jump to a notional label at the outermost unwind state. |
| 984 | llvm::BasicBlock *Unwind = createBasicBlock("eh.resume"); |
| 985 | JumpDest Dest(Unwind, EHStack.stable_end()); |
| 986 | EmitBranchThroughEHCleanup(Dest); |
| 987 | |
| 988 | // The unwind block. We have to reload the exception here because |
| 989 | // we might have unwound through arbitrary blocks, so the landing |
| 990 | // pad might not dominate. |
| 991 | EmitBlock(Unwind); |
| 992 | |
| 993 | // This can always be a call because we necessarily didn't find |
| 994 | // anything on the EH stack which needs our help. |
| 995 | Builder.CreateCall(getUnwindResumeOrRethrowFn(), |
| 996 | Builder.CreateLoad(getExceptionSlot())) |
| 997 | ->setDoesNotReturn(); |
| 998 | Builder.CreateUnreachable(); |
| 999 | } |
| 1000 | |
| 1001 | // Restore the old IR generation state. |
| 1002 | Builder.restoreIP(SavedIP); |
| 1003 | |
| 1004 | return LP; |
| 1005 | } |
| 1006 | |
| 1007 | /// Emits a call to __cxa_begin_catch and enters a cleanup to call |
| 1008 | /// __cxa_end_catch. |
| 1009 | static llvm::Value *CallBeginCatch(CodeGenFunction &CGF, llvm::Value *Exn) { |
| 1010 | llvm::CallInst *Call = CGF.Builder.CreateCall(getBeginCatchFn(CGF), Exn); |
| 1011 | Call->setDoesNotThrow(); |
| 1012 | |
| 1013 | { |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame^] | 1014 | CodeGenFunction::CleanupBlock EndCatchCleanup(CGF, NormalAndEHCleanup); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1015 | |
| 1016 | // __cxa_end_catch never throws, so this can just be a call. |
| 1017 | CGF.Builder.CreateCall(getEndCatchFn(CGF))->setDoesNotThrow(); |
| 1018 | } |
| 1019 | |
| 1020 | return Call; |
| 1021 | } |
| 1022 | |
| 1023 | /// A "special initializer" callback for initializing a catch |
| 1024 | /// parameter during catch initialization. |
| 1025 | static void InitCatchParam(CodeGenFunction &CGF, |
| 1026 | const VarDecl &CatchParam, |
| 1027 | llvm::Value *ParamAddr) { |
| 1028 | // Load the exception from where the landing pad saved it. |
| 1029 | llvm::Value *Exn = CGF.Builder.CreateLoad(CGF.getExceptionSlot(), "exn"); |
| 1030 | |
| 1031 | CanQualType CatchType = |
| 1032 | CGF.CGM.getContext().getCanonicalType(CatchParam.getType()); |
| 1033 | const llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType); |
| 1034 | |
| 1035 | // If we're catching by reference, we can just cast the object |
| 1036 | // pointer to the appropriate pointer. |
| 1037 | if (isa<ReferenceType>(CatchType)) { |
| 1038 | // __cxa_begin_catch returns the adjusted object pointer. |
| 1039 | llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn); |
| 1040 | llvm::Value *ExnCast = |
| 1041 | CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref"); |
| 1042 | CGF.Builder.CreateStore(ExnCast, ParamAddr); |
| 1043 | return; |
| 1044 | } |
| 1045 | |
| 1046 | // Non-aggregates (plus complexes). |
| 1047 | bool IsComplex = false; |
| 1048 | if (!CGF.hasAggregateLLVMType(CatchType) || |
| 1049 | (IsComplex = CatchType->isAnyComplexType())) { |
| 1050 | llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn); |
| 1051 | |
| 1052 | // If the catch type is a pointer type, __cxa_begin_catch returns |
| 1053 | // the pointer by value. |
| 1054 | if (CatchType->hasPointerRepresentation()) { |
| 1055 | llvm::Value *CastExn = |
| 1056 | CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted"); |
| 1057 | CGF.Builder.CreateStore(CastExn, ParamAddr); |
| 1058 | return; |
| 1059 | } |
| 1060 | |
| 1061 | // Otherwise, it returns a pointer into the exception object. |
| 1062 | |
| 1063 | const llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok |
| 1064 | llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy); |
| 1065 | |
| 1066 | if (IsComplex) { |
| 1067 | CGF.StoreComplexToAddr(CGF.LoadComplexFromAddr(Cast, /*volatile*/ false), |
| 1068 | ParamAddr, /*volatile*/ false); |
| 1069 | } else { |
| 1070 | llvm::Value *ExnLoad = CGF.Builder.CreateLoad(Cast, "exn.scalar"); |
| 1071 | CGF.EmitStoreOfScalar(ExnLoad, ParamAddr, /*volatile*/ false, CatchType); |
| 1072 | } |
| 1073 | return; |
| 1074 | } |
| 1075 | |
| 1076 | // FIXME: this *really* needs to be done via a proper, Sema-emitted |
| 1077 | // initializer expression. |
| 1078 | |
| 1079 | CXXRecordDecl *RD = CatchType.getTypePtr()->getAsCXXRecordDecl(); |
| 1080 | assert(RD && "aggregate catch type was not a record!"); |
| 1081 | |
| 1082 | const llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok |
| 1083 | |
| 1084 | if (RD->hasTrivialCopyConstructor()) { |
| 1085 | llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn); |
| 1086 | llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy); |
| 1087 | CGF.EmitAggregateCopy(ParamAddr, Cast, CatchType); |
| 1088 | return; |
| 1089 | } |
| 1090 | |
| 1091 | // We have to call __cxa_get_exception_ptr to get the adjusted |
| 1092 | // pointer before copying. |
| 1093 | llvm::CallInst *AdjustedExn = |
| 1094 | CGF.Builder.CreateCall(getGetExceptionPtrFn(CGF), Exn); |
| 1095 | AdjustedExn->setDoesNotThrow(); |
| 1096 | llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy); |
| 1097 | |
| 1098 | CXXConstructorDecl *CD = RD->getCopyConstructor(CGF.getContext(), 0); |
| 1099 | assert(CD && "record has no copy constructor!"); |
| 1100 | llvm::Value *CopyCtor = CGF.CGM.GetAddrOfCXXConstructor(CD, Ctor_Complete); |
| 1101 | |
| 1102 | CallArgList CallArgs; |
| 1103 | CallArgs.push_back(std::make_pair(RValue::get(ParamAddr), |
| 1104 | CD->getThisType(CGF.getContext()))); |
| 1105 | CallArgs.push_back(std::make_pair(RValue::get(Cast), |
| 1106 | CD->getParamDecl(0)->getType())); |
| 1107 | |
| 1108 | const FunctionProtoType *FPT |
| 1109 | = CD->getType()->getAs<FunctionProtoType>(); |
| 1110 | |
| 1111 | // Call the copy ctor in a terminate scope. |
| 1112 | CGF.EHStack.pushTerminate(); |
| 1113 | CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
| 1114 | CopyCtor, ReturnValueSlot(), CallArgs, CD); |
| 1115 | CGF.EHStack.popTerminate(); |
| 1116 | |
| 1117 | // Finally we can call __cxa_begin_catch. |
| 1118 | CallBeginCatch(CGF, Exn); |
| 1119 | } |
| 1120 | |
| 1121 | /// Begins a catch statement by initializing the catch variable and |
| 1122 | /// calling __cxa_begin_catch. |
| 1123 | static void BeginCatch(CodeGenFunction &CGF, |
| 1124 | const CXXCatchStmt *S) { |
| 1125 | // We have to be very careful with the ordering of cleanups here: |
| 1126 | // C++ [except.throw]p4: |
| 1127 | // The destruction [of the exception temporary] occurs |
| 1128 | // immediately after the destruction of the object declared in |
| 1129 | // the exception-declaration in the handler. |
| 1130 | // |
| 1131 | // So the precise ordering is: |
| 1132 | // 1. Construct catch variable. |
| 1133 | // 2. __cxa_begin_catch |
| 1134 | // 3. Enter __cxa_end_catch cleanup |
| 1135 | // 4. Enter dtor cleanup |
| 1136 | // |
| 1137 | // We do this by initializing the exception variable with a |
| 1138 | // "special initializer", InitCatchParam. Delegation sequence: |
| 1139 | // - ExitCXXTryStmt opens a RunCleanupsScope |
| 1140 | // - EmitLocalBlockVarDecl creates the variable and debug info |
| 1141 | // - InitCatchParam initializes the variable from the exception |
| 1142 | // - CallBeginCatch calls __cxa_begin_catch |
| 1143 | // - CallBeginCatch enters the __cxa_end_catch cleanup |
| 1144 | // - EmitLocalBlockVarDecl enters the variable destructor cleanup |
| 1145 | // - EmitCXXTryStmt emits the code for the catch body |
| 1146 | // - EmitCXXTryStmt close the RunCleanupsScope |
| 1147 | |
| 1148 | VarDecl *CatchParam = S->getExceptionDecl(); |
| 1149 | if (!CatchParam) { |
| 1150 | llvm::Value *Exn = CGF.Builder.CreateLoad(CGF.getExceptionSlot(), "exn"); |
| 1151 | CallBeginCatch(CGF, Exn); |
| 1152 | return; |
| 1153 | } |
| 1154 | |
| 1155 | // Emit the local. |
| 1156 | CGF.EmitLocalBlockVarDecl(*CatchParam, &InitCatchParam); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1159 | void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1160 | unsigned NumHandlers = S.getNumHandlers(); |
| 1161 | EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin()); |
| 1162 | assert(CatchScope.getNumHandlers() == NumHandlers); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1163 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1164 | // Copy the handler blocks off before we pop the EH stack. Emitting |
| 1165 | // the handlers might scribble on this memory. |
| 1166 | llvm::SmallVector<EHCatchScope::Handler, 8> Handlers(NumHandlers); |
| 1167 | memcpy(Handlers.data(), CatchScope.begin(), |
| 1168 | NumHandlers * sizeof(EHCatchScope::Handler)); |
| 1169 | EHStack.popCatch(); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1170 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1171 | // The fall-through block. |
| 1172 | llvm::BasicBlock *ContBB = createBasicBlock("try.cont"); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1173 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1174 | // We just emitted the body of the try; jump to the continue block. |
| 1175 | if (HaveInsertPoint()) |
| 1176 | Builder.CreateBr(ContBB); |
Mike Stump | 639787c | 2009-12-02 19:53:57 +0000 | [diff] [blame] | 1177 | |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1178 | // Determine if we need an implicit rethrow for all these catch handlers. |
| 1179 | bool ImplicitRethrow = false; |
| 1180 | if (IsFnTryBlock) |
| 1181 | ImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) || |
| 1182 | isa<CXXConstructorDecl>(CurCodeDecl); |
| 1183 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1184 | for (unsigned I = 0; I != NumHandlers; ++I) { |
| 1185 | llvm::BasicBlock *CatchBlock = Handlers[I].Block; |
| 1186 | EmitBlock(CatchBlock); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 1187 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1188 | // Catch the exception if this isn't a catch-all. |
| 1189 | const CXXCatchStmt *C = S.getHandler(I); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1190 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1191 | // Enter a cleanup scope, including the catch variable and the |
| 1192 | // end-catch. |
| 1193 | RunCleanupsScope CatchScope(*this); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1194 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1195 | // Initialize the catch variable and set up the cleanups. |
| 1196 | BeginCatch(*this, C); |
| 1197 | |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1198 | // If there's an implicit rethrow, push a normal "cleanup" to call |
| 1199 | // _cxa_rethrow. This needs to happen before _cxa_end_catch is |
| 1200 | // called. |
| 1201 | if (ImplicitRethrow) { |
| 1202 | CleanupBlock Rethrow(*this, NormalCleanup); |
| 1203 | EmitCallOrInvoke(getReThrowFn(*this), 0, 0); |
| 1204 | } |
| 1205 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1206 | // Perform the body of the catch. |
| 1207 | EmitStmt(C->getHandlerBlock()); |
| 1208 | |
| 1209 | // Fall out through the catch cleanups. |
| 1210 | CatchScope.ForceCleanup(); |
| 1211 | |
| 1212 | // Branch out of the try. |
| 1213 | if (HaveInsertPoint()) |
| 1214 | Builder.CreateBr(ContBB); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1217 | EmitBlock(ContBB); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1218 | } |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1219 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1220 | /// Enters a finally block for an implementation using zero-cost |
| 1221 | /// exceptions. This is mostly general, but hard-codes some |
| 1222 | /// language/ABI-specific behavior in the catch-all sections. |
| 1223 | CodeGenFunction::FinallyInfo |
| 1224 | CodeGenFunction::EnterFinallyBlock(const Stmt *Body, |
| 1225 | llvm::Constant *BeginCatchFn, |
| 1226 | llvm::Constant *EndCatchFn, |
| 1227 | llvm::Constant *RethrowFn) { |
| 1228 | assert((BeginCatchFn != 0) == (EndCatchFn != 0) && |
| 1229 | "begin/end catch functions not paired"); |
| 1230 | assert(RethrowFn && "rethrow function is required"); |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1231 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1232 | // The rethrow function has one of the following two types: |
| 1233 | // void (*)() |
| 1234 | // void (*)(void*) |
| 1235 | // In the latter case we need to pass it the exception object. |
| 1236 | // But we can't use the exception slot because the @finally might |
| 1237 | // have a landing pad (which would overwrite the exception slot). |
| 1238 | const llvm::FunctionType *RethrowFnTy = |
| 1239 | cast<llvm::FunctionType>( |
| 1240 | cast<llvm::PointerType>(RethrowFn->getType()) |
| 1241 | ->getElementType()); |
| 1242 | llvm::Value *SavedExnVar = 0; |
| 1243 | if (RethrowFnTy->getNumParams()) |
| 1244 | SavedExnVar = CreateTempAlloca(Builder.getInt8PtrTy(), "finally.exn"); |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1245 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1246 | // A finally block is a statement which must be executed on any edge |
| 1247 | // out of a given scope. Unlike a cleanup, the finally block may |
| 1248 | // contain arbitrary control flow leading out of itself. In |
| 1249 | // addition, finally blocks should always be executed, even if there |
| 1250 | // are no catch handlers higher on the stack. Therefore, we |
| 1251 | // surround the protected scope with a combination of a normal |
| 1252 | // cleanup (to catch attempts to break out of the block via normal |
| 1253 | // control flow) and an EH catch-all (semantically "outside" any try |
| 1254 | // statement to which the finally block might have been attached). |
| 1255 | // The finally block itself is generated in the context of a cleanup |
| 1256 | // which conditionally leaves the catch-all. |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1257 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1258 | FinallyInfo Info; |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1259 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1260 | // Jump destination for performing the finally block on an exception |
| 1261 | // edge. We'll never actually reach this block, so unreachable is |
| 1262 | // fine. |
| 1263 | JumpDest RethrowDest = getJumpDestInCurrentScope(getUnreachableBlock()); |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1264 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1265 | // Whether the finally block is being executed for EH purposes. |
| 1266 | llvm::AllocaInst *ForEHVar = CreateTempAlloca(CGF.Builder.getInt1Ty(), |
| 1267 | "finally.for-eh"); |
| 1268 | InitTempAlloca(ForEHVar, llvm::ConstantInt::getFalse(getLLVMContext())); |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1269 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1270 | // Enter a normal cleanup which will perform the @finally block. |
| 1271 | { |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame^] | 1272 | CodeGenFunction::CleanupBlock Cleanup(*this, NormalCleanup); |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1273 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1274 | // Enter a cleanup to call the end-catch function if one was provided. |
| 1275 | if (EndCatchFn) { |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame^] | 1276 | CodeGenFunction::CleanupBlock FinallyExitCleanup(CGF, NormalAndEHCleanup); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1277 | |
| 1278 | llvm::BasicBlock *EndCatchBB = createBasicBlock("finally.endcatch"); |
| 1279 | llvm::BasicBlock *CleanupContBB = createBasicBlock("finally.cleanup.cont"); |
| 1280 | |
| 1281 | llvm::Value *ShouldEndCatch = |
| 1282 | Builder.CreateLoad(ForEHVar, "finally.endcatch"); |
| 1283 | Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB); |
| 1284 | EmitBlock(EndCatchBB); |
| 1285 | Builder.CreateCall(EndCatchFn)->setDoesNotThrow(); |
| 1286 | EmitBlock(CleanupContBB); |
| 1287 | } |
| 1288 | |
| 1289 | // Emit the finally block. |
| 1290 | EmitStmt(Body); |
| 1291 | |
| 1292 | // If the end of the finally is reachable, check whether this was |
| 1293 | // for EH. If so, rethrow. |
| 1294 | if (HaveInsertPoint()) { |
| 1295 | llvm::BasicBlock *RethrowBB = createBasicBlock("finally.rethrow"); |
| 1296 | llvm::BasicBlock *ContBB = createBasicBlock("finally.cont"); |
| 1297 | |
| 1298 | llvm::Value *ShouldRethrow = |
| 1299 | Builder.CreateLoad(ForEHVar, "finally.shouldthrow"); |
| 1300 | Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB); |
| 1301 | |
| 1302 | EmitBlock(RethrowBB); |
| 1303 | if (SavedExnVar) { |
| 1304 | llvm::Value *Args[] = { Builder.CreateLoad(SavedExnVar) }; |
| 1305 | EmitCallOrInvoke(RethrowFn, Args, Args+1); |
| 1306 | } else { |
| 1307 | EmitCallOrInvoke(RethrowFn, 0, 0); |
| 1308 | } |
| 1309 | Builder.CreateUnreachable(); |
| 1310 | |
| 1311 | EmitBlock(ContBB); |
| 1312 | } |
| 1313 | |
| 1314 | // Leave the end-catch cleanup. As an optimization, pretend that |
| 1315 | // the fallthrough path was inaccessible; we've dynamically proven |
| 1316 | // that we're not in the EH case along that path. |
| 1317 | if (EndCatchFn) { |
| 1318 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
| 1319 | PopCleanupBlock(); |
| 1320 | Builder.restoreIP(SavedIP); |
| 1321 | } |
| 1322 | |
| 1323 | // Now make sure we actually have an insertion point or the |
| 1324 | // cleanup gods will hate us. |
| 1325 | EnsureInsertPoint(); |
| 1326 | } |
| 1327 | |
| 1328 | // Enter a catch-all scope. |
| 1329 | llvm::BasicBlock *CatchAllBB = createBasicBlock("finally.catchall"); |
| 1330 | CGBuilderTy::InsertPoint SavedIP = Builder.saveIP(); |
| 1331 | Builder.SetInsertPoint(CatchAllBB); |
| 1332 | |
| 1333 | // If there's a begin-catch function, call it. |
| 1334 | if (BeginCatchFn) { |
| 1335 | Builder.CreateCall(BeginCatchFn, Builder.CreateLoad(getExceptionSlot())) |
| 1336 | ->setDoesNotThrow(); |
| 1337 | } |
| 1338 | |
| 1339 | // If we need to remember the exception pointer to rethrow later, do so. |
| 1340 | if (SavedExnVar) { |
| 1341 | llvm::Value *SavedExn = Builder.CreateLoad(getExceptionSlot()); |
| 1342 | Builder.CreateStore(SavedExn, SavedExnVar); |
| 1343 | } |
| 1344 | |
| 1345 | // Tell the finally block that we're in EH. |
| 1346 | Builder.CreateStore(llvm::ConstantInt::getTrue(getLLVMContext()), ForEHVar); |
| 1347 | |
| 1348 | // Thread a jump through the finally cleanup. |
| 1349 | EmitBranchThroughCleanup(RethrowDest); |
| 1350 | |
| 1351 | Builder.restoreIP(SavedIP); |
| 1352 | |
| 1353 | EHCatchScope *CatchScope = EHStack.pushCatch(1); |
| 1354 | CatchScope->setCatchAllHandler(0, CatchAllBB); |
| 1355 | |
| 1356 | return Info; |
| 1357 | } |
| 1358 | |
| 1359 | void CodeGenFunction::ExitFinallyBlock(FinallyInfo &Info) { |
| 1360 | // Leave the finally catch-all. |
| 1361 | EHCatchScope &Catch = cast<EHCatchScope>(*EHStack.begin()); |
| 1362 | llvm::BasicBlock *CatchAllBB = Catch.getHandler(0).Block; |
| 1363 | EHStack.popCatch(); |
| 1364 | |
| 1365 | // And leave the normal cleanup. |
| 1366 | PopCleanupBlock(); |
| 1367 | |
| 1368 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
| 1369 | EmitBlock(CatchAllBB, true); |
| 1370 | |
| 1371 | Builder.restoreIP(SavedIP); |
| 1372 | } |
| 1373 | |
| 1374 | llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() { |
| 1375 | if (TerminateLandingPad) |
| 1376 | return TerminateLandingPad; |
| 1377 | |
| 1378 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
| 1379 | |
| 1380 | // This will get inserted at the end of the function. |
| 1381 | TerminateLandingPad = createBasicBlock("terminate.lpad"); |
| 1382 | Builder.SetInsertPoint(TerminateLandingPad); |
| 1383 | |
| 1384 | // Tell the backend that this is a landing pad. |
| 1385 | llvm::CallInst *Exn = |
| 1386 | Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::eh_exception), "exn"); |
| 1387 | Exn->setDoesNotThrow(); |
| 1388 | |
| 1389 | // Tell the backend what the exception table should be: |
| 1390 | // nothing but a catch-all. |
| 1391 | llvm::Value *Args[3] = { Exn, getPersonalityFn(*this), |
| 1392 | getCatchAllValue(*this) }; |
| 1393 | Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::eh_selector), |
| 1394 | Args, Args+3, "eh.selector") |
| 1395 | ->setDoesNotThrow(); |
| 1396 | |
| 1397 | llvm::CallInst *TerminateCall = Builder.CreateCall(getTerminateFn(*this)); |
| 1398 | TerminateCall->setDoesNotReturn(); |
| 1399 | TerminateCall->setDoesNotThrow(); |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1400 | CGF.Builder.CreateUnreachable(); |
| 1401 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1402 | // Restore the saved insertion state. |
| 1403 | Builder.restoreIP(SavedIP); |
John McCall | 891f80e | 2010-04-30 00:06:43 +0000 | [diff] [blame] | 1404 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1405 | return TerminateLandingPad; |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1406 | } |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 1407 | |
| 1408 | llvm::BasicBlock *CodeGenFunction::getTerminateHandler() { |
Mike Stump | 182f383 | 2009-12-10 00:02:42 +0000 | [diff] [blame] | 1409 | if (TerminateHandler) |
| 1410 | return TerminateHandler; |
| 1411 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1412 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
Mike Stump | 7695809 | 2009-12-09 23:31:35 +0000 | [diff] [blame] | 1413 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1414 | // Set up the terminate handler. This block is inserted at the very |
| 1415 | // end of the function by FinishFunction. |
Mike Stump | 182f383 | 2009-12-10 00:02:42 +0000 | [diff] [blame] | 1416 | TerminateHandler = createBasicBlock("terminate.handler"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1417 | Builder.SetInsertPoint(TerminateHandler); |
| 1418 | llvm::CallInst *TerminateCall = Builder.CreateCall(getTerminateFn(*this)); |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 1419 | TerminateCall->setDoesNotReturn(); |
| 1420 | TerminateCall->setDoesNotThrow(); |
| 1421 | Builder.CreateUnreachable(); |
| 1422 | |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1423 | // Restore the saved insertion state. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1424 | Builder.restoreIP(SavedIP); |
Mike Stump | 7695809 | 2009-12-09 23:31:35 +0000 | [diff] [blame] | 1425 | |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 1426 | return TerminateHandler; |
| 1427 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1428 | |
| 1429 | CodeGenFunction::CleanupBlock::CleanupBlock(CodeGenFunction &CGF, |
| 1430 | CleanupKind Kind) |
| 1431 | : CGF(CGF), SavedIP(CGF.Builder.saveIP()), NormalCleanupExitBB(0) { |
| 1432 | llvm::BasicBlock *EntryBB = CGF.createBasicBlock("cleanup"); |
| 1433 | CGF.Builder.SetInsertPoint(EntryBB); |
| 1434 | |
| 1435 | switch (Kind) { |
| 1436 | case NormalAndEHCleanup: |
| 1437 | NormalCleanupEntryBB = EHCleanupEntryBB = EntryBB; |
| 1438 | break; |
| 1439 | |
| 1440 | case NormalCleanup: |
| 1441 | NormalCleanupEntryBB = EntryBB; |
| 1442 | EHCleanupEntryBB = 0; |
| 1443 | break; |
| 1444 | |
| 1445 | case EHCleanup: |
| 1446 | NormalCleanupEntryBB = 0; |
| 1447 | EHCleanupEntryBB = EntryBB; |
| 1448 | CGF.EHStack.pushTerminate(); |
| 1449 | break; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | void CodeGenFunction::CleanupBlock::beginEHCleanup() { |
| 1454 | assert(EHCleanupEntryBB == 0 && "already started an EH cleanup"); |
| 1455 | NormalCleanupExitBB = CGF.Builder.GetInsertBlock(); |
| 1456 | assert(NormalCleanupExitBB && "end of normal cleanup is unreachable"); |
| 1457 | |
| 1458 | EHCleanupEntryBB = CGF.createBasicBlock("eh.cleanup"); |
| 1459 | CGF.Builder.SetInsertPoint(EHCleanupEntryBB); |
| 1460 | CGF.EHStack.pushTerminate(); |
| 1461 | } |
| 1462 | |
| 1463 | CodeGenFunction::CleanupBlock::~CleanupBlock() { |
| 1464 | llvm::BasicBlock *EHCleanupExitBB = 0; |
| 1465 | |
| 1466 | // If we're currently writing the EH cleanup... |
| 1467 | if (EHCleanupEntryBB) { |
| 1468 | // Set the EH cleanup exit block. |
| 1469 | EHCleanupExitBB = CGF.Builder.GetInsertBlock(); |
| 1470 | assert(EHCleanupExitBB && "end of EH cleanup is unreachable"); |
| 1471 | |
| 1472 | // If we're actually writing both at once, set the normal exit, too. |
| 1473 | if (EHCleanupEntryBB == NormalCleanupEntryBB) |
| 1474 | NormalCleanupExitBB = EHCleanupExitBB; |
| 1475 | |
| 1476 | // Otherwise, we must have pushed a terminate handler. |
| 1477 | else |
| 1478 | CGF.EHStack.popTerminate(); |
| 1479 | |
| 1480 | // Otherwise, just set the normal cleanup exit block. |
| 1481 | } else { |
| 1482 | NormalCleanupExitBB = CGF.Builder.GetInsertBlock(); |
| 1483 | assert(NormalCleanupExitBB && "end of normal cleanup is unreachable"); |
| 1484 | } |
| 1485 | |
| 1486 | CGF.EHStack.pushCleanup(NormalCleanupEntryBB, NormalCleanupExitBB, |
| 1487 | EHCleanupEntryBB, EHCleanupExitBB); |
| 1488 | |
| 1489 | CGF.Builder.restoreIP(SavedIP); |
| 1490 | } |
| 1491 | |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame^] | 1492 | void EHScopeStack::LazyCleanup::_anchor() {} |