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