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