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