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