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