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