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