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