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