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