Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1 | //===--- CGException.cpp - Emit LLVM Code for C++ exceptions ----*- C++ -*-===// |
Anders Carlsson | 4b08db7 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Anders Carlsson | 4b08db7 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This contains code dealing with C++ exception related code generation. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "CodeGenFunction.h" |
David Majnemer | 442d0a2 | 2014-11-25 07:20:20 +0000 | [diff] [blame] | 14 | #include "CGCXXABI.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 15 | #include "CGCleanup.h" |
Benjamin Kramer | 793bd55 | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 17 | #include "ConstantEmitter.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 | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetBuiltins.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Intrinsics.h" |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 25 | #include "llvm/IR/IntrinsicInst.h" |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 26 | #include "llvm/Support/SaveAndRestore.h" |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 27 | |
Anders Carlsson | 4b08db7 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 31 | static llvm::FunctionCallee getFreeExceptionFn(CodeGenModule &CGM) { |
Mike Stump | 3327021 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 32 | // void __cxa_free_exception(void *thrown_exception); |
Mike Stump | 75546b8 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 34 | llvm::FunctionType *FTy = |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 35 | llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*isVarArg=*/false); |
Mike Stump | 75546b8 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 36 | |
John McCall | 2c33ba8 | 2013-02-12 03:51:38 +0000 | [diff] [blame] | 37 | return CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception"); |
Mike Stump | 3327021 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 38 | } |
| 39 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 40 | static llvm::FunctionCallee getUnexpectedFn(CodeGenModule &CGM) { |
Richard Smith | 2f7aa19 | 2013-06-20 23:03:35 +0000 | [diff] [blame] | 41 | // void __cxa_call_unexpected(void *thrown_exception); |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 43 | llvm::FunctionType *FTy = |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 44 | llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*isVarArg=*/false); |
Mike Stump | 75546b8 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 45 | |
John McCall | 2c33ba8 | 2013-02-12 03:51:38 +0000 | [diff] [blame] | 46 | return CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected"); |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 47 | } |
| 48 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 49 | llvm::FunctionCallee CodeGenModule::getTerminateFn() { |
Mike Stump | 3327021 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 50 | // void __terminate(); |
| 51 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 52 | llvm::FunctionType *FTy = |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 53 | llvm::FunctionType::get(VoidTy, /*isVarArg=*/false); |
Mike Stump | 75546b8 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 55 | StringRef name; |
John McCall | 9de1978 | 2011-07-06 01:22:26 +0000 | [diff] [blame] | 56 | |
| 57 | // In C++, use std::terminate(). |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 58 | if (getLangOpts().CPlusPlus && |
| 59 | getTarget().getCXXABI().isItaniumFamily()) { |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 60 | name = "_ZSt9terminatev"; |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 61 | } else if (getLangOpts().CPlusPlus && |
| 62 | getTarget().getCXXABI().isMicrosoft()) { |
David Majnemer | b710a93 | 2015-05-11 03:57:49 +0000 | [diff] [blame] | 63 | if (getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2015)) |
David Majnemer | fb6ffca | 2015-05-10 21:38:26 +0000 | [diff] [blame] | 64 | name = "__std_terminate"; |
| 65 | else |
Reid Kleckner | fb93154 | 2018-03-16 20:36:49 +0000 | [diff] [blame] | 66 | name = "?terminate@@YAXXZ"; |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 67 | } else if (getLangOpts().ObjC && |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 68 | getLangOpts().ObjCRuntime.hasTerminate()) |
John McCall | 9de1978 | 2011-07-06 01:22:26 +0000 | [diff] [blame] | 69 | name = "objc_terminate"; |
| 70 | else |
| 71 | name = "abort"; |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 72 | return CreateRuntimeFunction(FTy, name); |
David Chisnall | f9c4225 | 2010-05-17 13:49:20 +0000 | [diff] [blame] | 73 | } |
| 74 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 75 | static llvm::FunctionCallee getCatchallRethrowFn(CodeGenModule &CGM, |
| 76 | StringRef Name) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 77 | llvm::FunctionType *FTy = |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 78 | llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*isVarArg=*/false); |
John McCall | 36ea372 | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 79 | |
John McCall | 2c33ba8 | 2013-02-12 03:51:38 +0000 | [diff] [blame] | 80 | return CGM.CreateRuntimeFunction(FTy, Name); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 83 | const EHPersonality EHPersonality::GNU_C = { "__gcc_personality_v0", nullptr }; |
Benjamin Kramer | 793bd55 | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 84 | const EHPersonality |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 85 | EHPersonality::GNU_C_SJLJ = { "__gcc_personality_sj0", nullptr }; |
| 86 | const EHPersonality |
Reid Kleckner | 8f45c9c | 2014-09-15 17:19:16 +0000 | [diff] [blame] | 87 | EHPersonality::GNU_C_SEH = { "__gcc_personality_seh0", nullptr }; |
| 88 | const EHPersonality |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 89 | EHPersonality::NeXT_ObjC = { "__objc_personality_v0", nullptr }; |
| 90 | const EHPersonality |
| 91 | EHPersonality::GNU_CPlusPlus = { "__gxx_personality_v0", nullptr }; |
| 92 | const EHPersonality |
| 93 | EHPersonality::GNU_CPlusPlus_SJLJ = { "__gxx_personality_sj0", nullptr }; |
Benjamin Kramer | 793bd55 | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 94 | const EHPersonality |
Reid Kleckner | 8f45c9c | 2014-09-15 17:19:16 +0000 | [diff] [blame] | 95 | EHPersonality::GNU_CPlusPlus_SEH = { "__gxx_personality_seh0", nullptr }; |
| 96 | const EHPersonality |
Benjamin Kramer | 793bd55 | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 97 | EHPersonality::GNU_ObjC = {"__gnu_objc_personality_v0", "objc_exception_throw"}; |
| 98 | const EHPersonality |
Benjamin Kramer | 796c1d9 | 2017-01-08 22:58:07 +0000 | [diff] [blame] | 99 | EHPersonality::GNU_ObjC_SJLJ = {"__gnu_objc_personality_sj0", "objc_exception_throw"}; |
| 100 | const EHPersonality |
| 101 | EHPersonality::GNU_ObjC_SEH = {"__gnu_objc_personality_seh0", "objc_exception_throw"}; |
| 102 | const EHPersonality |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 103 | EHPersonality::GNU_ObjCXX = { "__gnustep_objcxx_personality_v0", nullptr }; |
David Chisnall | 2ec1b10d | 2013-01-11 15:33:01 +0000 | [diff] [blame] | 104 | const EHPersonality |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 105 | EHPersonality::GNUstep_ObjC = { "__gnustep_objc_personality_v0", nullptr }; |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 106 | const EHPersonality |
| 107 | EHPersonality::MSVC_except_handler = { "_except_handler3", nullptr }; |
| 108 | const EHPersonality |
| 109 | EHPersonality::MSVC_C_specific_handler = { "__C_specific_handler", nullptr }; |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 110 | const EHPersonality |
| 111 | EHPersonality::MSVC_CxxFrameHandler3 = { "__CxxFrameHandler3", nullptr }; |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 112 | const EHPersonality |
| 113 | EHPersonality::GNU_Wasm_CPlusPlus = { "__gxx_wasm_personality_v0", nullptr }; |
John McCall | 36ea372 | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 114 | |
Heejin Ahn | 0083179 | 2018-06-04 18:23:00 +0000 | [diff] [blame] | 115 | static const EHPersonality &getCPersonality(const TargetInfo &Target, |
Reid Kleckner | e070b99 | 2014-11-14 02:01:10 +0000 | [diff] [blame] | 116 | const LangOptions &L) { |
Heejin Ahn | 0083179 | 2018-06-04 18:23:00 +0000 | [diff] [blame] | 117 | const llvm::Triple &T = Target.getTriple(); |
Shoaib Meenai | a5fc603 | 2018-06-08 00:41:01 +0000 | [diff] [blame] | 118 | if (T.isWindowsMSVCEnvironment()) |
| 119 | return EHPersonality::MSVC_CxxFrameHandler3; |
John McCall | 2faab30 | 2010-11-07 02:35:25 +0000 | [diff] [blame] | 120 | if (L.SjLjExceptions) |
| 121 | return EHPersonality::GNU_C_SJLJ; |
Saleem Abdulrasool | 3e70132 | 2018-03-09 07:06:42 +0000 | [diff] [blame] | 122 | if (L.DWARFExceptions) |
| 123 | return EHPersonality::GNU_C; |
Martell Malone | c950c65 | 2017-11-29 07:25:12 +0000 | [diff] [blame] | 124 | if (L.SEHExceptions) |
Reid Kleckner | 8f45c9c | 2014-09-15 17:19:16 +0000 | [diff] [blame] | 125 | return EHPersonality::GNU_C_SEH; |
John McCall | 36ea372 | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 126 | return EHPersonality::GNU_C; |
| 127 | } |
| 128 | |
Heejin Ahn | 0083179 | 2018-06-04 18:23:00 +0000 | [diff] [blame] | 129 | static const EHPersonality &getObjCPersonality(const TargetInfo &Target, |
Reid Kleckner | e070b99 | 2014-11-14 02:01:10 +0000 | [diff] [blame] | 130 | const LangOptions &L) { |
Heejin Ahn | 0083179 | 2018-06-04 18:23:00 +0000 | [diff] [blame] | 131 | const llvm::Triple &T = Target.getTriple(); |
Shoaib Meenai | a5fc603 | 2018-06-08 00:41:01 +0000 | [diff] [blame] | 132 | if (T.isWindowsMSVCEnvironment()) |
| 133 | return EHPersonality::MSVC_CxxFrameHandler3; |
| 134 | |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 135 | switch (L.ObjCRuntime.getKind()) { |
| 136 | case ObjCRuntime::FragileMacOSX: |
Heejin Ahn | 0083179 | 2018-06-04 18:23:00 +0000 | [diff] [blame] | 137 | return getCPersonality(Target, L); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 138 | case ObjCRuntime::MacOSX: |
| 139 | case ObjCRuntime::iOS: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 140 | case ObjCRuntime::WatchOS: |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 141 | return EHPersonality::NeXT_ObjC; |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 142 | case ObjCRuntime::GNUstep: |
David Chisnall | 2ec1b10d | 2013-01-11 15:33:01 +0000 | [diff] [blame] | 143 | if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7)) |
| 144 | return EHPersonality::GNUstep_ObjC; |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 145 | LLVM_FALLTHROUGH; |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 146 | case ObjCRuntime::GCC: |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 147 | case ObjCRuntime::ObjFW: |
Benjamin Kramer | 796c1d9 | 2017-01-08 22:58:07 +0000 | [diff] [blame] | 148 | if (L.SjLjExceptions) |
| 149 | return EHPersonality::GNU_ObjC_SJLJ; |
Martell Malone | c950c65 | 2017-11-29 07:25:12 +0000 | [diff] [blame] | 150 | if (L.SEHExceptions) |
Benjamin Kramer | 796c1d9 | 2017-01-08 22:58:07 +0000 | [diff] [blame] | 151 | return EHPersonality::GNU_ObjC_SEH; |
John McCall | 36ea372 | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 152 | return EHPersonality::GNU_ObjC; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 153 | } |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 154 | llvm_unreachable("bad runtime kind"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Heejin Ahn | 0083179 | 2018-06-04 18:23:00 +0000 | [diff] [blame] | 157 | static const EHPersonality &getCXXPersonality(const TargetInfo &Target, |
| 158 | const LangOptions &L) { |
| 159 | const llvm::Triple &T = Target.getTriple(); |
Shoaib Meenai | a5fc603 | 2018-06-08 00:41:01 +0000 | [diff] [blame] | 160 | if (T.isWindowsMSVCEnvironment()) |
| 161 | return EHPersonality::MSVC_CxxFrameHandler3; |
John McCall | 36ea372 | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 162 | if (L.SjLjExceptions) |
| 163 | return EHPersonality::GNU_CPlusPlus_SJLJ; |
Saleem Abdulrasool | 3e70132 | 2018-03-09 07:06:42 +0000 | [diff] [blame] | 164 | if (L.DWARFExceptions) |
| 165 | return EHPersonality::GNU_CPlusPlus; |
Martell Malone | c950c65 | 2017-11-29 07:25:12 +0000 | [diff] [blame] | 166 | if (L.SEHExceptions) |
Reid Kleckner | 8f45c9c | 2014-09-15 17:19:16 +0000 | [diff] [blame] | 167 | return EHPersonality::GNU_CPlusPlus_SEH; |
Heejin Ahn | e8b2b88 | 2019-09-12 04:01:37 +0000 | [diff] [blame] | 168 | if (L.WasmExceptions) |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 169 | return EHPersonality::GNU_Wasm_CPlusPlus; |
Reid Kleckner | e070b99 | 2014-11-14 02:01:10 +0000 | [diff] [blame] | 170 | return EHPersonality::GNU_CPlusPlus; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /// Determines the personality function to use when both C++ |
| 174 | /// and Objective-C exceptions are being caught. |
Heejin Ahn | 0083179 | 2018-06-04 18:23:00 +0000 | [diff] [blame] | 175 | static const EHPersonality &getObjCXXPersonality(const TargetInfo &Target, |
| 176 | const LangOptions &L) { |
Shoaib Meenai | a5fc603 | 2018-06-08 00:41:01 +0000 | [diff] [blame] | 177 | if (Target.getTriple().isWindowsMSVCEnvironment()) |
| 178 | return EHPersonality::MSVC_CxxFrameHandler3; |
| 179 | |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 180 | switch (L.ObjCRuntime.getKind()) { |
Saleem Abdulrasool | 8f6d944 | 2017-11-02 00:25:40 +0000 | [diff] [blame] | 181 | // In the fragile ABI, just use C++ exception handling and hope |
| 182 | // they're not doing crazy exception mixing. |
| 183 | case ObjCRuntime::FragileMacOSX: |
Heejin Ahn | 0083179 | 2018-06-04 18:23:00 +0000 | [diff] [blame] | 184 | return getCXXPersonality(Target, L); |
Saleem Abdulrasool | 8f6d944 | 2017-11-02 00:25:40 +0000 | [diff] [blame] | 185 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 186 | // The ObjC personality defers to the C++ personality for non-ObjC |
| 187 | // handlers. Unlike the C++ case, we use the same personality |
| 188 | // function on targets using (backend-driven) SJLJ EH. |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 189 | case ObjCRuntime::MacOSX: |
| 190 | case ObjCRuntime::iOS: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 191 | case ObjCRuntime::WatchOS: |
Heejin Ahn | 0083179 | 2018-06-04 18:23:00 +0000 | [diff] [blame] | 192 | return getObjCPersonality(Target, L); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 193 | |
Saleem Abdulrasool | 8f6d944 | 2017-11-02 00:25:40 +0000 | [diff] [blame] | 194 | case ObjCRuntime::GNUstep: |
| 195 | return EHPersonality::GNU_ObjCXX; |
David Chisnall | f9c4225 | 2010-05-17 13:49:20 +0000 | [diff] [blame] | 196 | |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 197 | // The GCC runtime's personality function inherently doesn't support |
Saleem Abdulrasool | 8f6d944 | 2017-11-02 00:25:40 +0000 | [diff] [blame] | 198 | // mixed EH. Use the ObjC personality just to avoid returning null. |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 199 | case ObjCRuntime::GCC: |
Benjamin Kramer | 9851cb7 | 2017-04-01 17:59:01 +0000 | [diff] [blame] | 200 | case ObjCRuntime::ObjFW: |
Heejin Ahn | 0083179 | 2018-06-04 18:23:00 +0000 | [diff] [blame] | 201 | return getObjCPersonality(Target, L); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 202 | } |
| 203 | llvm_unreachable("bad runtime kind"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 206 | static const EHPersonality &getSEHPersonalityMSVC(const llvm::Triple &T) { |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 207 | if (T.getArch() == llvm::Triple::x86) |
| 208 | return EHPersonality::MSVC_except_handler; |
| 209 | return EHPersonality::MSVC_C_specific_handler; |
| 210 | } |
| 211 | |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 212 | const EHPersonality &EHPersonality::get(CodeGenModule &CGM, |
| 213 | const FunctionDecl *FD) { |
Reid Kleckner | e070b99 | 2014-11-14 02:01:10 +0000 | [diff] [blame] | 214 | const llvm::Triple &T = CGM.getTarget().getTriple(); |
| 215 | const LangOptions &L = CGM.getLangOpts(); |
Heejin Ahn | 1eb074d | 2018-06-01 01:01:37 +0000 | [diff] [blame] | 216 | const TargetInfo &Target = CGM.getTarget(); |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 217 | |
Reid Kleckner | 0148565 | 2015-09-17 17:04:13 +0000 | [diff] [blame] | 218 | // Functions using SEH get an SEH personality. |
| 219 | if (FD && FD->usesSEHTry()) |
| 220 | return getSEHPersonalityMSVC(T); |
| 221 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 222 | if (L.ObjC) |
Heejin Ahn | 0083179 | 2018-06-04 18:23:00 +0000 | [diff] [blame] | 223 | return L.CPlusPlus ? getObjCXXPersonality(Target, L) |
| 224 | : getObjCPersonality(Target, L); |
| 225 | return L.CPlusPlus ? getCXXPersonality(Target, L) |
| 226 | : getCPersonality(Target, L); |
John McCall | 36ea372 | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 227 | } |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 228 | |
David Majnemer | c28d46e | 2015-07-22 23:46:21 +0000 | [diff] [blame] | 229 | const EHPersonality &EHPersonality::get(CodeGenFunction &CGF) { |
Reid Kleckner | 65fa869 | 2017-10-13 16:55:14 +0000 | [diff] [blame] | 230 | const auto *FD = CGF.CurCodeDecl; |
| 231 | // For outlined finallys and filters, use the SEH personality in case they |
| 232 | // contain more SEH. This mostly only affects finallys. Filters could |
| 233 | // hypothetically use gnu statement expressions to sneak in nested SEH. |
| 234 | FD = FD ? FD : CGF.CurSEHParent; |
| 235 | return get(CGF.CGM, dyn_cast_or_null<FunctionDecl>(FD)); |
David Majnemer | c28d46e | 2015-07-22 23:46:21 +0000 | [diff] [blame] | 236 | } |
| 237 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 238 | static llvm::FunctionCallee getPersonalityFn(CodeGenModule &CGM, |
| 239 | const EHPersonality &Personality) { |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 240 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, true), |
| 241 | Personality.PersonalityFn, |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 242 | llvm::AttributeList(), /*Local=*/true); |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static llvm::Constant *getOpaquePersonalityFn(CodeGenModule &CGM, |
| 246 | const EHPersonality &Personality) { |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 247 | llvm::FunctionCallee Fn = getPersonalityFn(CGM, Personality); |
Dylan McKay | 02bbe41 | 2018-11-09 19:42:05 +0000 | [diff] [blame] | 248 | llvm::PointerType* Int8PtrTy = llvm::PointerType::get( |
| 249 | llvm::Type::getInt8Ty(CGM.getLLVMContext()), |
| 250 | CGM.getDataLayout().getProgramAddressSpace()); |
| 251 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 252 | return llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(Fn.getCallee()), |
| 253 | Int8PtrTy); |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Vedant Kumar | db60947 | 2015-09-11 15:40:05 +0000 | [diff] [blame] | 256 | /// Check whether a landingpad instruction only uses C++ features. |
| 257 | static bool LandingPadHasOnlyCXXUses(llvm::LandingPadInst *LPI) { |
| 258 | for (unsigned I = 0, E = LPI->getNumClauses(); I != E; ++I) { |
| 259 | // Look for something that would've been returned by the ObjC |
| 260 | // runtime's GetEHType() method. |
| 261 | llvm::Value *Val = LPI->getClause(I)->stripPointerCasts(); |
| 262 | if (LPI->isCatch(I)) { |
| 263 | // Check if the catch value has the ObjC prefix. |
| 264 | if (llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Val)) |
| 265 | // ObjC EH selector entries are always global variables with |
| 266 | // names starting like this. |
| 267 | if (GV->getName().startswith("OBJC_EHTYPE")) |
| 268 | return false; |
| 269 | } else { |
| 270 | // Check if any of the filter values have the ObjC prefix. |
| 271 | llvm::Constant *CVal = cast<llvm::Constant>(Val); |
| 272 | for (llvm::User::op_iterator |
| 273 | II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) { |
| 274 | if (llvm::GlobalVariable *GV = |
| 275 | cast<llvm::GlobalVariable>((*II)->stripPointerCasts())) |
| 276 | // ObjC EH selector entries are always global variables with |
| 277 | // names starting like this. |
| 278 | if (GV->getName().startswith("OBJC_EHTYPE")) |
| 279 | return false; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | return true; |
| 284 | } |
| 285 | |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 286 | /// Check whether a personality function could reasonably be swapped |
| 287 | /// for a C++ personality function. |
| 288 | static bool PersonalityHasOnlyCXXUses(llvm::Constant *Fn) { |
Chandler Carruth | 4d01fff | 2014-03-09 03:16:50 +0000 | [diff] [blame] | 289 | for (llvm::User *U : Fn->users()) { |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 290 | // Conditionally white-list bitcasts. |
Chandler Carruth | 4d01fff | 2014-03-09 03:16:50 +0000 | [diff] [blame] | 291 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(U)) { |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 292 | if (CE->getOpcode() != llvm::Instruction::BitCast) return false; |
| 293 | if (!PersonalityHasOnlyCXXUses(CE)) |
| 294 | return false; |
| 295 | continue; |
| 296 | } |
| 297 | |
Vedant Kumar | db60947 | 2015-09-11 15:40:05 +0000 | [diff] [blame] | 298 | // Otherwise it must be a function. |
| 299 | llvm::Function *F = dyn_cast<llvm::Function>(U); |
| 300 | if (!F) return false; |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 301 | |
Vedant Kumar | db60947 | 2015-09-11 15:40:05 +0000 | [diff] [blame] | 302 | for (auto BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 303 | if (BB->isLandingPad()) |
| 304 | if (!LandingPadHasOnlyCXXUses(BB->getLandingPadInst())) |
| 305 | return false; |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | /// Try to use the C++ personality function in ObjC++. Not doing this |
| 313 | /// can cause some incompatibilities with gcc, which is more |
| 314 | /// aggressive about only using the ObjC++ personality in a function |
| 315 | /// when it really needs it. |
| 316 | void CodeGenModule::SimplifyPersonality() { |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 317 | // If we're not in ObjC++ -fexceptions, there's nothing to do. |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 318 | if (!LangOpts.CPlusPlus || !LangOpts.ObjC || !LangOpts.Exceptions) |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 319 | return; |
| 320 | |
John McCall | 3c22393 | 2012-11-14 17:48:31 +0000 | [diff] [blame] | 321 | // Both the problem this endeavors to fix and the way the logic |
| 322 | // above works is specific to the NeXT runtime. |
| 323 | if (!LangOpts.ObjCRuntime.isNeXTFamily()) |
| 324 | return; |
| 325 | |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 326 | const EHPersonality &ObjCXX = EHPersonality::get(*this, /*FD=*/nullptr); |
Heejin Ahn | 0083179 | 2018-06-04 18:23:00 +0000 | [diff] [blame] | 327 | const EHPersonality &CXX = getCXXPersonality(getTarget(), LangOpts); |
Benjamin Kramer | 793bd55 | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 328 | if (&ObjCXX == &CXX) |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 329 | return; |
| 330 | |
Benjamin Kramer | 793bd55 | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 331 | assert(std::strcmp(ObjCXX.PersonalityFn, CXX.PersonalityFn) != 0 && |
| 332 | "Different EHPersonalities using the same personality function."); |
| 333 | |
| 334 | llvm::Function *Fn = getModule().getFunction(ObjCXX.PersonalityFn); |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 335 | |
| 336 | // Nothing to do if it's unused. |
| 337 | if (!Fn || Fn->use_empty()) return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 338 | |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 339 | // Can't do the optimization if it has non-C++ uses. |
| 340 | if (!PersonalityHasOnlyCXXUses(Fn)) return; |
| 341 | |
| 342 | // Create the C++ personality function and kill off the old |
| 343 | // function. |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 344 | llvm::FunctionCallee CXXFn = getPersonalityFn(*this, CXX); |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 345 | |
| 346 | // This can happen if the user is screwing with us. |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 347 | if (Fn->getType() != CXXFn.getCallee()->getType()) |
| 348 | return; |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 349 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 350 | Fn->replaceAllUsesWith(CXXFn.getCallee()); |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 351 | Fn->eraseFromParent(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /// Returns the value to inject into a selector to indicate the |
| 355 | /// presence of a catch-all. |
| 356 | static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) { |
| 357 | // Possibly we should use @llvm.eh.catch.all.value here. |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 358 | return llvm::ConstantPointerNull::get(CGF.Int8PtrTy); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 359 | } |
| 360 | |
John McCall | bb02601 | 2010-07-13 21:17:51 +0000 | [diff] [blame] | 361 | namespace { |
| 362 | /// A cleanup to free the exception object if its initialization |
| 363 | /// throws. |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 364 | struct FreeException final : EHScopeStack::Cleanup { |
John McCall | 5fcf8da | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 365 | llvm::Value *exn; |
| 366 | FreeException(llvm::Value *exn) : exn(exn) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 367 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 368 | CGF.EmitNounwindRuntimeCall(getFreeExceptionFn(CGF.CGM), exn); |
John McCall | bb02601 | 2010-07-13 21:17:51 +0000 | [diff] [blame] | 369 | } |
| 370 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 371 | } // end anonymous namespace |
John McCall | bb02601 | 2010-07-13 21:17:51 +0000 | [diff] [blame] | 372 | |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 373 | // Emits an exception expression into the given location. This |
| 374 | // differs from EmitAnyExprToMem only in that, if a final copy-ctor |
| 375 | // call is required, an exception within that copy ctor causes |
| 376 | // std::terminate to be invoked. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 377 | void CodeGenFunction::EmitAnyExprToExn(const Expr *e, Address addr) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 378 | // Make sure the exception object is cleaned up if there's an |
| 379 | // exception during initialization. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 380 | pushFullExprCleanup<FreeException>(EHCleanup, addr.getPointer()); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 381 | EHScopeStack::stable_iterator cleanup = EHStack.stable_begin(); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 382 | |
| 383 | // __cxa_allocate_exception returns a void*; we need to cast this |
| 384 | // to the appropriate type for the object. |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 385 | llvm::Type *ty = ConvertTypeForMem(e->getType())->getPointerTo(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 386 | Address typedAddr = Builder.CreateBitCast(addr, ty); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 387 | |
| 388 | // FIXME: this isn't quite right! If there's a final unelided call |
| 389 | // to a copy constructor, then according to [except.terminate]p1 we |
| 390 | // must call std::terminate() if that constructor throws, because |
| 391 | // technically that copy occurs after the exception expression is |
| 392 | // evaluated but before the exception is caught. But the best way |
| 393 | // to handle that is to teach EmitAggExpr to do the final copy |
| 394 | // differently if it can't be elided. |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 395 | EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(), |
| 396 | /*IsInit*/ true); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 397 | |
John McCall | e4df6c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 398 | // Deactivate the cleanup block. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 399 | DeactivateCleanupBlock(cleanup, |
| 400 | cast<llvm::Instruction>(typedAddr.getPointer())); |
Mike Stump | 5406614 | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 401 | } |
| 402 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 403 | Address CodeGenFunction::getExceptionSlot() { |
John McCall | 9b382dd | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 404 | if (!ExceptionSlot) |
| 405 | ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 406 | return Address(ExceptionSlot, getPointerAlign()); |
Mike Stump | 5406614 | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 407 | } |
| 408 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 409 | Address CodeGenFunction::getEHSelectorSlot() { |
John McCall | 9b382dd | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 410 | if (!EHSelectorSlot) |
| 411 | EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 412 | return Address(EHSelectorSlot, CharUnits::fromQuantity(4)); |
John McCall | 9b382dd | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Bill Wendling | 79a70e4 | 2011-09-15 18:57:19 +0000 | [diff] [blame] | 415 | llvm::Value *CodeGenFunction::getExceptionFromSlot() { |
| 416 | return Builder.CreateLoad(getExceptionSlot(), "exn"); |
| 417 | } |
| 418 | |
| 419 | llvm::Value *CodeGenFunction::getSelectorFromSlot() { |
| 420 | return Builder.CreateLoad(getEHSelectorSlot(), "sel"); |
| 421 | } |
| 422 | |
Richard Smith | ea85232 | 2013-05-07 21:53:22 +0000 | [diff] [blame] | 423 | void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E, |
| 424 | bool KeepInsertionPoint) { |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 425 | if (const Expr *SubExpr = E->getSubExpr()) { |
| 426 | QualType ThrowType = SubExpr->getType(); |
| 427 | if (ThrowType->isObjCObjectPointerType()) { |
| 428 | const Stmt *ThrowStmt = E->getSubExpr(); |
| 429 | const ObjCAtThrowStmt S(E->getExprLoc(), const_cast<Stmt *>(ThrowStmt)); |
| 430 | CGM.getObjCRuntime().EmitThrowStmt(*this, S, false); |
| 431 | } else { |
| 432 | CGM.getCXXABI().emitThrow(*this, E); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 433 | } |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 434 | } else { |
| 435 | CGM.getCXXABI().emitRethrow(*this, /*isNoReturn=*/true); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 436 | } |
Mike Stump | 75546b8 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 437 | |
John McCall | 20f6ab8 | 2011-01-12 03:41:02 +0000 | [diff] [blame] | 438 | // throw is an expression, and the expression emitters expect us |
| 439 | // to leave ourselves at a valid insertion point. |
Richard Smith | ea85232 | 2013-05-07 21:53:22 +0000 | [diff] [blame] | 440 | if (KeepInsertionPoint) |
| 441 | EmitBlock(createBasicBlock("throw.cont")); |
Anders Carlsson | 4b08db7 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 442 | } |
Mike Stump | 58ef18b | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 443 | |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 444 | void CodeGenFunction::EmitStartEHSpec(const Decl *D) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 445 | if (!CGM.getLangOpts().CXXExceptions) |
Anders Carlsson | 9878f9f | 2010-02-06 23:59:05 +0000 | [diff] [blame] | 446 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 447 | |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 448 | const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 449 | if (!FD) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 450 | // Check if CapturedDecl is nothrow and create terminate scope for it. |
| 451 | if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) { |
| 452 | if (CD->isNothrow()) |
| 453 | EHStack.pushTerminate(); |
| 454 | } |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 455 | return; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 456 | } |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 457 | const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 458 | if (!Proto) |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 459 | return; |
| 460 | |
Sebastian Redl | 0b94c9f | 2011-03-15 18:42:48 +0000 | [diff] [blame] | 461 | ExceptionSpecificationType EST = Proto->getExceptionSpecType(); |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 462 | if (isNoexceptExceptionSpec(EST) && Proto->canThrow() == CT_Cannot) { |
| 463 | // noexcept functions are simple terminate scopes. |
| 464 | EHStack.pushTerminate(); |
Sebastian Redl | 0b94c9f | 2011-03-15 18:42:48 +0000 | [diff] [blame] | 465 | } else if (EST == EST_Dynamic || EST == EST_DynamicNone) { |
David Majnemer | 1f192e2 | 2015-04-01 04:45:52 +0000 | [diff] [blame] | 466 | // TODO: Revisit exception specifications for the MS ABI. There is a way to |
| 467 | // encode these in an object file but MSVC doesn't do anything with it. |
| 468 | if (getTarget().getCXXABI().isMicrosoft()) |
| 469 | return; |
Sebastian Redl | 0b94c9f | 2011-03-15 18:42:48 +0000 | [diff] [blame] | 470 | unsigned NumExceptions = Proto->getNumExceptions(); |
| 471 | EHFilterScope *Filter = EHStack.pushFilter(NumExceptions); |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 472 | |
Sebastian Redl | 0b94c9f | 2011-03-15 18:42:48 +0000 | [diff] [blame] | 473 | for (unsigned I = 0; I != NumExceptions; ++I) { |
| 474 | QualType Ty = Proto->getExceptionType(I); |
| 475 | QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType(); |
| 476 | llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType, |
| 477 | /*ForEH=*/true); |
| 478 | Filter->setFilter(I, EHType); |
| 479 | } |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 480 | } |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 481 | } |
| 482 | |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 483 | /// Emit the dispatch block for a filter scope if necessary. |
| 484 | static void emitFilterDispatchBlock(CodeGenFunction &CGF, |
| 485 | EHFilterScope &filterScope) { |
| 486 | llvm::BasicBlock *dispatchBlock = filterScope.getCachedEHDispatchBlock(); |
| 487 | if (!dispatchBlock) return; |
| 488 | if (dispatchBlock->use_empty()) { |
| 489 | delete dispatchBlock; |
| 490 | return; |
| 491 | } |
| 492 | |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 493 | CGF.EmitBlockAfterUses(dispatchBlock); |
| 494 | |
| 495 | // If this isn't a catch-all filter, we need to check whether we got |
| 496 | // here because the filter triggered. |
| 497 | if (filterScope.getNumFilters()) { |
| 498 | // Load the selector value. |
Bill Wendling | 79a70e4 | 2011-09-15 18:57:19 +0000 | [diff] [blame] | 499 | llvm::Value *selector = CGF.getSelectorFromSlot(); |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 500 | llvm::BasicBlock *unexpectedBB = CGF.createBasicBlock("ehspec.unexpected"); |
| 501 | |
| 502 | llvm::Value *zero = CGF.Builder.getInt32(0); |
| 503 | llvm::Value *failsFilter = |
Nico Weber | 1bebad1 | 2015-02-11 22:33:32 +0000 | [diff] [blame] | 504 | CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails"); |
| 505 | CGF.Builder.CreateCondBr(failsFilter, unexpectedBB, |
| 506 | CGF.getEHResumeBlock(false)); |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 507 | |
| 508 | CGF.EmitBlock(unexpectedBB); |
| 509 | } |
| 510 | |
| 511 | // Call __cxa_call_unexpected. This doesn't need to be an invoke |
| 512 | // because __cxa_call_unexpected magically filters exceptions |
| 513 | // according to the last landing pad the exception was thrown |
| 514 | // into. Seriously. |
Bill Wendling | 79a70e4 | 2011-09-15 18:57:19 +0000 | [diff] [blame] | 515 | llvm::Value *exn = CGF.getExceptionFromSlot(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 516 | CGF.EmitRuntimeCall(getUnexpectedFn(CGF.CGM), exn) |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 517 | ->setDoesNotReturn(); |
| 518 | CGF.Builder.CreateUnreachable(); |
| 519 | } |
| 520 | |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 521 | void CodeGenFunction::EmitEndEHSpec(const Decl *D) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 522 | if (!CGM.getLangOpts().CXXExceptions) |
Anders Carlsson | 9878f9f | 2010-02-06 23:59:05 +0000 | [diff] [blame] | 523 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 524 | |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 525 | const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 526 | if (!FD) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 527 | // Check if CapturedDecl is nothrow and pop terminate scope for it. |
| 528 | if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) { |
| 529 | if (CD->isNothrow()) |
| 530 | EHStack.popTerminate(); |
| 531 | } |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 532 | return; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 533 | } |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 534 | const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 535 | if (!Proto) |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 536 | return; |
| 537 | |
Sebastian Redl | 0b94c9f | 2011-03-15 18:42:48 +0000 | [diff] [blame] | 538 | ExceptionSpecificationType EST = Proto->getExceptionSpecType(); |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 539 | if (isNoexceptExceptionSpec(EST) && Proto->canThrow() == CT_Cannot) { |
| 540 | EHStack.popTerminate(); |
Sebastian Redl | 0b94c9f | 2011-03-15 18:42:48 +0000 | [diff] [blame] | 541 | } else if (EST == EST_Dynamic || EST == EST_DynamicNone) { |
David Majnemer | 1f192e2 | 2015-04-01 04:45:52 +0000 | [diff] [blame] | 542 | // TODO: Revisit exception specifications for the MS ABI. There is a way to |
| 543 | // encode these in an object file but MSVC doesn't do anything with it. |
| 544 | if (getTarget().getCXXABI().isMicrosoft()) |
| 545 | return; |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 546 | EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin()); |
| 547 | emitFilterDispatchBlock(*this, filterScope); |
Sebastian Redl | 0b94c9f | 2011-03-15 18:42:48 +0000 | [diff] [blame] | 548 | EHStack.popFilter(); |
| 549 | } |
Mike Stump | 1d84921 | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Mike Stump | 58ef18b | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 552 | void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { |
John McCall | b609d3f | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 553 | EnterCXXTryStmt(S); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 554 | EmitStmt(S.getTryBlock()); |
John McCall | b609d3f | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 555 | ExitCXXTryStmt(S); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 556 | } |
| 557 | |
John McCall | b609d3f | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 558 | void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 559 | unsigned NumHandlers = S.getNumHandlers(); |
| 560 | EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers); |
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 | for (unsigned I = 0; I != NumHandlers; ++I) { |
| 563 | const CXXCatchStmt *C = S.getHandler(I); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 564 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 565 | llvm::BasicBlock *Handler = createBasicBlock("catch"); |
| 566 | if (C->getExceptionDecl()) { |
| 567 | // FIXME: Dropping the reference type on the type into makes it |
| 568 | // impossible to correctly implement catch-by-reference |
| 569 | // semantics for pointers. Unfortunately, this is what all |
| 570 | // existing compilers do, and it's not clear that the standard |
| 571 | // personality routine is capable of doing this right. See C++ DR 388: |
| 572 | // 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] | 573 | Qualifiers CaughtTypeQuals; |
| 574 | QualType CaughtType = CGM.getContext().getUnqualifiedArrayType( |
| 575 | C->getCaughtType().getNonReferenceType(), CaughtTypeQuals); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 576 | |
Reid Kleckner | 10aa770 | 2015-09-16 20:15:55 +0000 | [diff] [blame] | 577 | CatchTypeInfo TypeInfo{nullptr, 0}; |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 578 | if (CaughtType->isObjCObjectPointerType()) |
Reid Kleckner | 10aa770 | 2015-09-16 20:15:55 +0000 | [diff] [blame] | 579 | TypeInfo.RTTI = CGM.getObjCRuntime().GetEHType(CaughtType); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 580 | else |
Reid Kleckner | 10aa770 | 2015-09-16 20:15:55 +0000 | [diff] [blame] | 581 | TypeInfo = CGM.getCXXABI().getAddrOfCXXCatchHandlerType( |
| 582 | CaughtType, C->getCaughtType()); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 583 | CatchScope->setHandler(I, TypeInfo, Handler); |
| 584 | } else { |
| 585 | // No exception decl indicates '...', a catch-all. |
Reid Kleckner | 10aa770 | 2015-09-16 20:15:55 +0000 | [diff] [blame] | 586 | CatchScope->setHandler(I, CGM.getCXXABI().getCatchAllTypeInfo(), Handler); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 589 | } |
| 590 | |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 591 | llvm::BasicBlock * |
| 592 | CodeGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator si) { |
Reid Kleckner | 129552b | 2015-10-08 01:13:52 +0000 | [diff] [blame] | 593 | if (EHPersonality::get(*this).usesFuncletPads()) |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 594 | return getFuncletEHDispatchBlock(si); |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 595 | |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 596 | // The dispatch block for the end of the scope chain is a block that |
| 597 | // just resumes unwinding. |
| 598 | if (si == EHStack.stable_end()) |
David Chisnall | 9a837be | 2012-11-07 16:50:40 +0000 | [diff] [blame] | 599 | return getEHResumeBlock(true); |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 600 | |
| 601 | // Otherwise, we should look at the actual scope. |
| 602 | EHScope &scope = *EHStack.find(si); |
| 603 | |
| 604 | llvm::BasicBlock *dispatchBlock = scope.getCachedEHDispatchBlock(); |
| 605 | if (!dispatchBlock) { |
| 606 | switch (scope.getKind()) { |
| 607 | case EHScope::Catch: { |
| 608 | // Apply a special case to a single catch-all. |
| 609 | EHCatchScope &catchScope = cast<EHCatchScope>(scope); |
| 610 | if (catchScope.getNumHandlers() == 1 && |
| 611 | catchScope.getHandler(0).isCatchAll()) { |
| 612 | dispatchBlock = catchScope.getHandler(0).Block; |
| 613 | |
| 614 | // Otherwise, make a dispatch block. |
| 615 | } else { |
| 616 | dispatchBlock = createBasicBlock("catch.dispatch"); |
| 617 | } |
| 618 | break; |
| 619 | } |
| 620 | |
| 621 | case EHScope::Cleanup: |
| 622 | dispatchBlock = createBasicBlock("ehcleanup"); |
| 623 | break; |
| 624 | |
| 625 | case EHScope::Filter: |
| 626 | dispatchBlock = createBasicBlock("filter.dispatch"); |
| 627 | break; |
| 628 | |
| 629 | case EHScope::Terminate: |
| 630 | dispatchBlock = getTerminateHandler(); |
| 631 | break; |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 632 | |
Reid Kleckner | 2586aac | 2015-09-10 22:11:13 +0000 | [diff] [blame] | 633 | case EHScope::PadEnd: |
| 634 | llvm_unreachable("PadEnd unnecessary for Itanium!"); |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 635 | } |
| 636 | scope.setCachedEHDispatchBlock(dispatchBlock); |
| 637 | } |
| 638 | return dispatchBlock; |
| 639 | } |
| 640 | |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 641 | llvm::BasicBlock * |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 642 | CodeGenFunction::getFuncletEHDispatchBlock(EHScopeStack::stable_iterator SI) { |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 643 | // Returning nullptr indicates that the previous dispatch block should unwind |
| 644 | // to caller. |
| 645 | if (SI == EHStack.stable_end()) |
| 646 | return nullptr; |
| 647 | |
| 648 | // Otherwise, we should look at the actual scope. |
| 649 | EHScope &EHS = *EHStack.find(SI); |
| 650 | |
| 651 | llvm::BasicBlock *DispatchBlock = EHS.getCachedEHDispatchBlock(); |
| 652 | if (DispatchBlock) |
| 653 | return DispatchBlock; |
| 654 | |
| 655 | if (EHS.getKind() == EHScope::Terminate) |
Reid Kleckner | 06f19a0 | 2018-01-02 21:34:16 +0000 | [diff] [blame] | 656 | DispatchBlock = getTerminateFunclet(); |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 657 | else |
| 658 | DispatchBlock = createBasicBlock(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 659 | CGBuilderTy Builder(*this, DispatchBlock); |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 660 | |
| 661 | switch (EHS.getKind()) { |
| 662 | case EHScope::Catch: |
| 663 | DispatchBlock->setName("catch.dispatch"); |
| 664 | break; |
| 665 | |
| 666 | case EHScope::Cleanup: |
| 667 | DispatchBlock->setName("ehcleanup"); |
| 668 | break; |
| 669 | |
| 670 | case EHScope::Filter: |
| 671 | llvm_unreachable("exception specifications not handled yet!"); |
| 672 | |
| 673 | case EHScope::Terminate: |
| 674 | DispatchBlock->setName("terminate"); |
| 675 | break; |
| 676 | |
Reid Kleckner | 2586aac | 2015-09-10 22:11:13 +0000 | [diff] [blame] | 677 | case EHScope::PadEnd: |
| 678 | llvm_unreachable("PadEnd dispatch block missing!"); |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 679 | } |
| 680 | EHS.setCachedEHDispatchBlock(DispatchBlock); |
| 681 | return DispatchBlock; |
| 682 | } |
| 683 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 684 | /// Check whether this is a non-EH scope, i.e. a scope which doesn't |
| 685 | /// affect exception handling. Currently, the only non-EH scopes are |
| 686 | /// normal-only cleanup scopes. |
| 687 | static bool isNonEHScope(const EHScope &S) { |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 688 | switch (S.getKind()) { |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 689 | case EHScope::Cleanup: |
| 690 | return !cast<EHCleanupScope>(S).isEHCleanup(); |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 691 | case EHScope::Filter: |
| 692 | case EHScope::Catch: |
| 693 | case EHScope::Terminate: |
Reid Kleckner | 2586aac | 2015-09-10 22:11:13 +0000 | [diff] [blame] | 694 | case EHScope::PadEnd: |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 695 | return false; |
| 696 | } |
| 697 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 698 | llvm_unreachable("Invalid EHScope Kind!"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | llvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() { |
| 702 | assert(EHStack.requiresLandingPad()); |
| 703 | assert(!EHStack.empty()); |
| 704 | |
Reid Kleckner | 8f1b1f5 | 2016-03-01 19:51:48 +0000 | [diff] [blame] | 705 | // If exceptions are disabled and SEH is not in use, then there is no invoke |
| 706 | // destination. SEH "works" even if exceptions are off. In practice, this |
| 707 | // means that C++ destructors and other EH cleanups don't run, which is |
| 708 | // consistent with MSVC's behavior. |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 709 | const LangOptions &LO = CGM.getLangOpts(); |
| 710 | if (!LO.Exceptions) { |
| 711 | if (!LO.Borland && !LO.MicrosoftExt) |
| 712 | return nullptr; |
Reid Kleckner | e7b3f7c | 2015-02-11 00:00:21 +0000 | [diff] [blame] | 713 | if (!currentFunctionUsesSEHTry()) |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 714 | return nullptr; |
| 715 | } |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 716 | |
Justin Lebar | 3e6449b | 2016-10-04 23:41:49 +0000 | [diff] [blame] | 717 | // CUDA device code doesn't have exceptions. |
| 718 | if (LO.CUDA && LO.CUDAIsDevice) |
| 719 | return nullptr; |
| 720 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 721 | // Check the innermost scope for a cached landing pad. If this is |
| 722 | // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad. |
| 723 | llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad(); |
| 724 | if (LP) return LP; |
| 725 | |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 726 | const EHPersonality &Personality = EHPersonality::get(*this); |
| 727 | |
| 728 | if (!CurFn->hasPersonalityFn()) |
| 729 | CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality)); |
| 730 | |
Reid Kleckner | 129552b | 2015-10-08 01:13:52 +0000 | [diff] [blame] | 731 | if (Personality.usesFuncletPads()) { |
| 732 | // We don't need separate landing pads in the funclet model. |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 733 | LP = getEHDispatchBlock(EHStack.getInnermostEHScope()); |
| 734 | } else { |
| 735 | // Build the landing pad for this scope. |
| 736 | LP = EmitLandingPad(); |
| 737 | } |
| 738 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 739 | assert(LP); |
| 740 | |
| 741 | // Cache the landing pad on the innermost scope. If this is a |
| 742 | // non-EH scope, cache the landing pad on the enclosing scope, too. |
| 743 | for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) { |
| 744 | ir->setCachedLandingPad(LP); |
| 745 | if (!isNonEHScope(*ir)) break; |
| 746 | } |
| 747 | |
| 748 | return LP; |
| 749 | } |
| 750 | |
| 751 | llvm::BasicBlock *CodeGenFunction::EmitLandingPad() { |
| 752 | assert(EHStack.requiresLandingPad()); |
| 753 | |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 754 | EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope()); |
| 755 | switch (innermostEHScope.getKind()) { |
| 756 | case EHScope::Terminate: |
| 757 | return getTerminateLandingPad(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 758 | |
Reid Kleckner | 2586aac | 2015-09-10 22:11:13 +0000 | [diff] [blame] | 759 | case EHScope::PadEnd: |
| 760 | llvm_unreachable("PadEnd unnecessary for Itanium!"); |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 761 | |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 762 | case EHScope::Catch: |
| 763 | case EHScope::Cleanup: |
| 764 | case EHScope::Filter: |
| 765 | if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad()) |
| 766 | return lpad; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | // Save the current IR generation state. |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 770 | CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP(); |
Calixte Denizet | fcd661d | 2018-09-24 18:24:18 +0000 | [diff] [blame] | 771 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, CurEHLocation); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 772 | |
| 773 | // Create and configure the landing pad. |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 774 | llvm::BasicBlock *lpad = createBasicBlock("lpad"); |
| 775 | EmitBlock(lpad); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 776 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 777 | llvm::LandingPadInst *LPadInst = |
| 778 | Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty), 0); |
Bill Wendling | f0724e8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 779 | |
| 780 | llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0); |
| 781 | Builder.CreateStore(LPadExn, getExceptionSlot()); |
| 782 | llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1); |
| 783 | Builder.CreateStore(LPadSel, getEHSelectorSlot()); |
| 784 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 785 | // Save the exception pointer. It's safe to use a single exception |
| 786 | // pointer per function because EH cleanups can never have nested |
| 787 | // try/catches. |
Bill Wendling | f0724e8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 788 | // Build the landingpad instruction. |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 789 | |
| 790 | // Accumulate all the handlers in scope. |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 791 | bool hasCatchAll = false; |
| 792 | bool hasCleanup = false; |
| 793 | bool hasFilter = false; |
| 794 | SmallVector<llvm::Value*, 4> filterTypes; |
| 795 | llvm::SmallPtrSet<llvm::Value*, 4> catchTypes; |
Nico Weber | e68b9f3 | 2015-02-25 16:25:00 +0000 | [diff] [blame] | 796 | for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end(); I != E; |
| 797 | ++I) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 798 | |
| 799 | switch (I->getKind()) { |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 800 | case EHScope::Cleanup: |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 801 | // If we have a cleanup, remember that. |
| 802 | hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup()); |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 803 | continue; |
| 804 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 805 | case EHScope::Filter: { |
| 806 | 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] | 807 | assert(!hasCatchAll && "EH filter reached after catch-all"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 808 | |
Bill Wendling | f0724e8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 809 | // Filter scopes get added to the landingpad in weird ways. |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 810 | EHFilterScope &filter = cast<EHFilterScope>(*I); |
| 811 | hasFilter = true; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 812 | |
Bill Wendling | 8c4b716 | 2011-09-22 20:32:54 +0000 | [diff] [blame] | 813 | // Add all the filter values. |
| 814 | for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i) |
| 815 | filterTypes.push_back(filter.getFilter(i)); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 816 | goto done; |
| 817 | } |
| 818 | |
| 819 | case EHScope::Terminate: |
| 820 | // Terminate scopes are basically catch-alls. |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 821 | assert(!hasCatchAll); |
| 822 | hasCatchAll = true; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 823 | goto done; |
| 824 | |
| 825 | case EHScope::Catch: |
| 826 | break; |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 827 | |
Reid Kleckner | 2586aac | 2015-09-10 22:11:13 +0000 | [diff] [blame] | 828 | case EHScope::PadEnd: |
| 829 | llvm_unreachable("PadEnd unnecessary for Itanium!"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 830 | } |
| 831 | |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 832 | EHCatchScope &catchScope = cast<EHCatchScope>(*I); |
| 833 | for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) { |
| 834 | EHCatchScope::Handler handler = catchScope.getHandler(hi); |
Reid Kleckner | 10aa770 | 2015-09-16 20:15:55 +0000 | [diff] [blame] | 835 | assert(handler.Type.Flags == 0 && |
| 836 | "landingpads do not support catch handler flags"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 837 | |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 838 | // If this is a catch-all, register that and abort. |
Reid Kleckner | 10aa770 | 2015-09-16 20:15:55 +0000 | [diff] [blame] | 839 | if (!handler.Type.RTTI) { |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 840 | assert(!hasCatchAll); |
| 841 | hasCatchAll = true; |
| 842 | goto done; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | // Check whether we already have a handler for this type. |
Reid Kleckner | 10aa770 | 2015-09-16 20:15:55 +0000 | [diff] [blame] | 846 | if (catchTypes.insert(handler.Type.RTTI).second) |
Bill Wendling | f0724e8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 847 | // If not, add it directly to the landingpad. |
Reid Kleckner | 10aa770 | 2015-09-16 20:15:55 +0000 | [diff] [blame] | 848 | LPadInst->addClause(handler.Type.RTTI); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 849 | } |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | done: |
Bill Wendling | f0724e8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 853 | // If we have a catch-all, add null to the landingpad. |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 854 | assert(!(hasCatchAll && hasFilter)); |
| 855 | if (hasCatchAll) { |
Bill Wendling | f0724e8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 856 | LPadInst->addClause(getCatchAllValue(*this)); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 857 | |
| 858 | // 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] | 859 | // right place in the landingpad, which is to say, at the end. |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 860 | } else if (hasFilter) { |
Bill Wendling | 58e58fe | 2011-09-19 22:08:36 +0000 | [diff] [blame] | 861 | // Create a filter expression: a constant array indicating which filter |
| 862 | // types there are. The personality routine only lands here if the filter |
| 863 | // doesn't match. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 864 | SmallVector<llvm::Constant*, 8> Filters; |
Bill Wendling | f0724e8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 865 | llvm::ArrayType *AType = |
| 866 | llvm::ArrayType::get(!filterTypes.empty() ? |
| 867 | filterTypes[0]->getType() : Int8PtrTy, |
| 868 | filterTypes.size()); |
| 869 | |
| 870 | for (unsigned i = 0, e = filterTypes.size(); i != e; ++i) |
| 871 | Filters.push_back(cast<llvm::Constant>(filterTypes[i])); |
| 872 | llvm::Constant *FilterArray = llvm::ConstantArray::get(AType, Filters); |
| 873 | LPadInst->addClause(FilterArray); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 874 | |
| 875 | // Also check whether we need a cleanup. |
Bill Wendling | f0724e8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 876 | if (hasCleanup) |
| 877 | LPadInst->setCleanup(true); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 878 | |
| 879 | // Otherwise, signal that we at least have cleanups. |
Logan Chien | e9c8ccb | 2014-07-01 11:47:10 +0000 | [diff] [blame] | 880 | } else if (hasCleanup) { |
| 881 | LPadInst->setCleanup(true); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Bill Wendling | f0724e8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 884 | assert((LPadInst->getNumClauses() > 0 || LPadInst->isCleanup()) && |
| 885 | "landingpad instruction has no clauses!"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 886 | |
| 887 | // Tell the backend how to generate the landing pad. |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 888 | Builder.CreateBr(getEHDispatchBlock(EHStack.getInnermostEHScope())); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 889 | |
| 890 | // Restore the old IR generation state. |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 891 | Builder.restoreIP(savedIP); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 892 | |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 893 | return lpad; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 894 | } |
| 895 | |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 896 | static void emitCatchPadBlock(CodeGenFunction &CGF, EHCatchScope &CatchScope) { |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 897 | llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock(); |
| 898 | assert(DispatchBlock); |
| 899 | |
| 900 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveIP(); |
| 901 | CGF.EmitBlockAfterUses(DispatchBlock); |
| 902 | |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 903 | llvm::Value *ParentPad = CGF.CurrentFuncletPad; |
| 904 | if (!ParentPad) |
| 905 | ParentPad = llvm::ConstantTokenNone::get(CGF.getLLVMContext()); |
| 906 | llvm::BasicBlock *UnwindBB = |
| 907 | CGF.getEHDispatchBlock(CatchScope.getEnclosingEHScope()); |
| 908 | |
| 909 | unsigned NumHandlers = CatchScope.getNumHandlers(); |
| 910 | llvm::CatchSwitchInst *CatchSwitch = |
| 911 | CGF.Builder.CreateCatchSwitch(ParentPad, UnwindBB, NumHandlers); |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 912 | |
| 913 | // Test against each of the exception types we claim to catch. |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 914 | for (unsigned I = 0; I < NumHandlers; ++I) { |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 915 | const EHCatchScope::Handler &Handler = CatchScope.getHandler(I); |
| 916 | |
Reid Kleckner | 10aa770 | 2015-09-16 20:15:55 +0000 | [diff] [blame] | 917 | CatchTypeInfo TypeInfo = Handler.Type; |
| 918 | if (!TypeInfo.RTTI) |
| 919 | TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy); |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 920 | |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 921 | CGF.Builder.SetInsertPoint(Handler.Block); |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 922 | |
| 923 | if (EHPersonality::get(CGF).isMSVCXXPersonality()) { |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 924 | CGF.Builder.CreateCatchPad( |
| 925 | CatchSwitch, {TypeInfo.RTTI, CGF.Builder.getInt32(TypeInfo.Flags), |
| 926 | llvm::Constant::getNullValue(CGF.VoidPtrTy)}); |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 927 | } else { |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 928 | CGF.Builder.CreateCatchPad(CatchSwitch, {TypeInfo.RTTI}); |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 929 | } |
| 930 | |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 931 | CatchSwitch->addHandler(Handler.Block); |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 932 | } |
| 933 | CGF.Builder.restoreIP(SavedIP); |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 936 | // Wasm uses Windows-style EH instructions, but it merges all catch clauses into |
| 937 | // one big catchpad, within which we use Itanium's landingpad-style selector |
| 938 | // comparison instructions. |
| 939 | static void emitWasmCatchPadBlock(CodeGenFunction &CGF, |
| 940 | EHCatchScope &CatchScope) { |
| 941 | llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock(); |
| 942 | assert(DispatchBlock); |
| 943 | |
| 944 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveIP(); |
| 945 | CGF.EmitBlockAfterUses(DispatchBlock); |
| 946 | |
| 947 | llvm::Value *ParentPad = CGF.CurrentFuncletPad; |
| 948 | if (!ParentPad) |
| 949 | ParentPad = llvm::ConstantTokenNone::get(CGF.getLLVMContext()); |
| 950 | llvm::BasicBlock *UnwindBB = |
| 951 | CGF.getEHDispatchBlock(CatchScope.getEnclosingEHScope()); |
| 952 | |
| 953 | unsigned NumHandlers = CatchScope.getNumHandlers(); |
| 954 | llvm::CatchSwitchInst *CatchSwitch = |
| 955 | CGF.Builder.CreateCatchSwitch(ParentPad, UnwindBB, NumHandlers); |
| 956 | |
| 957 | // We don't use a landingpad instruction, so generate intrinsic calls to |
| 958 | // provide exception and selector values. |
| 959 | llvm::BasicBlock *WasmCatchStartBlock = CGF.createBasicBlock("catch.start"); |
| 960 | CatchSwitch->addHandler(WasmCatchStartBlock); |
| 961 | CGF.EmitBlockAfterUses(WasmCatchStartBlock); |
| 962 | |
| 963 | // Create a catchpad instruction. |
| 964 | SmallVector<llvm::Value *, 4> CatchTypes; |
| 965 | for (unsigned I = 0, E = NumHandlers; I < E; ++I) { |
| 966 | const EHCatchScope::Handler &Handler = CatchScope.getHandler(I); |
| 967 | CatchTypeInfo TypeInfo = Handler.Type; |
| 968 | if (!TypeInfo.RTTI) |
| 969 | TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy); |
| 970 | CatchTypes.push_back(TypeInfo.RTTI); |
| 971 | } |
| 972 | auto *CPI = CGF.Builder.CreateCatchPad(CatchSwitch, CatchTypes); |
| 973 | |
| 974 | // Create calls to wasm.get.exception and wasm.get.ehselector intrinsics. |
| 975 | // Before they are lowered appropriately later, they provide values for the |
| 976 | // exception and selector. |
James Y Knight | 8799cae | 2019-02-03 21:53:49 +0000 | [diff] [blame] | 977 | llvm::Function *GetExnFn = |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 978 | CGF.CGM.getIntrinsic(llvm::Intrinsic::wasm_get_exception); |
James Y Knight | 8799cae | 2019-02-03 21:53:49 +0000 | [diff] [blame] | 979 | llvm::Function *GetSelectorFn = |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 980 | CGF.CGM.getIntrinsic(llvm::Intrinsic::wasm_get_ehselector); |
| 981 | llvm::CallInst *Exn = CGF.Builder.CreateCall(GetExnFn, CPI); |
| 982 | CGF.Builder.CreateStore(Exn, CGF.getExceptionSlot()); |
| 983 | llvm::CallInst *Selector = CGF.Builder.CreateCall(GetSelectorFn, CPI); |
| 984 | |
James Y Knight | 8799cae | 2019-02-03 21:53:49 +0000 | [diff] [blame] | 985 | llvm::Function *TypeIDFn = CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 986 | |
| 987 | // If there's only a single catch-all, branch directly to its handler. |
| 988 | if (CatchScope.getNumHandlers() == 1 && |
| 989 | CatchScope.getHandler(0).isCatchAll()) { |
| 990 | CGF.Builder.CreateBr(CatchScope.getHandler(0).Block); |
| 991 | CGF.Builder.restoreIP(SavedIP); |
| 992 | return; |
| 993 | } |
| 994 | |
| 995 | // Test against each of the exception types we claim to catch. |
| 996 | for (unsigned I = 0, E = NumHandlers;; ++I) { |
| 997 | assert(I < E && "ran off end of handlers!"); |
| 998 | const EHCatchScope::Handler &Handler = CatchScope.getHandler(I); |
| 999 | CatchTypeInfo TypeInfo = Handler.Type; |
| 1000 | if (!TypeInfo.RTTI) |
| 1001 | TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy); |
| 1002 | |
| 1003 | // Figure out the next block. |
| 1004 | llvm::BasicBlock *NextBlock; |
| 1005 | |
| 1006 | bool EmitNextBlock = false, NextIsEnd = false; |
| 1007 | |
| 1008 | // If this is the last handler, we're at the end, and the next block is a |
| 1009 | // block that contains a call to the rethrow function, so we can unwind to |
| 1010 | // the enclosing EH scope. The call itself will be generated later. |
| 1011 | if (I + 1 == E) { |
| 1012 | NextBlock = CGF.createBasicBlock("rethrow"); |
| 1013 | EmitNextBlock = true; |
| 1014 | NextIsEnd = true; |
| 1015 | |
| 1016 | // If the next handler is a catch-all, we're at the end, and the |
| 1017 | // next block is that handler. |
| 1018 | } else if (CatchScope.getHandler(I + 1).isCatchAll()) { |
| 1019 | NextBlock = CatchScope.getHandler(I + 1).Block; |
| 1020 | NextIsEnd = true; |
| 1021 | |
| 1022 | // Otherwise, we're not at the end and we need a new block. |
| 1023 | } else { |
| 1024 | NextBlock = CGF.createBasicBlock("catch.fallthrough"); |
| 1025 | EmitNextBlock = true; |
| 1026 | } |
| 1027 | |
| 1028 | // Figure out the catch type's index in the LSDA's type table. |
| 1029 | llvm::CallInst *TypeIndex = CGF.Builder.CreateCall(TypeIDFn, TypeInfo.RTTI); |
| 1030 | TypeIndex->setDoesNotThrow(); |
| 1031 | |
| 1032 | llvm::Value *MatchesTypeIndex = |
| 1033 | CGF.Builder.CreateICmpEQ(Selector, TypeIndex, "matches"); |
| 1034 | CGF.Builder.CreateCondBr(MatchesTypeIndex, Handler.Block, NextBlock); |
| 1035 | |
| 1036 | if (EmitNextBlock) |
| 1037 | CGF.EmitBlock(NextBlock); |
| 1038 | if (NextIsEnd) |
| 1039 | break; |
| 1040 | } |
| 1041 | |
| 1042 | CGF.Builder.restoreIP(SavedIP); |
| 1043 | } |
| 1044 | |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1045 | /// Emit the structure of the dispatch block for the given catch scope. |
| 1046 | /// It is an invariant that the dispatch block already exists. |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 1047 | static void emitCatchDispatchBlock(CodeGenFunction &CGF, |
| 1048 | EHCatchScope &catchScope) { |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 1049 | if (EHPersonality::get(CGF).isWasmPersonality()) |
| 1050 | return emitWasmCatchPadBlock(CGF, catchScope); |
Reid Kleckner | 129552b | 2015-10-08 01:13:52 +0000 | [diff] [blame] | 1051 | if (EHPersonality::get(CGF).usesFuncletPads()) |
| 1052 | return emitCatchPadBlock(CGF, catchScope); |
David Majnemer | dbf1045 | 2015-07-31 17:58:45 +0000 | [diff] [blame] | 1053 | |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1054 | llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock(); |
| 1055 | assert(dispatchBlock); |
| 1056 | |
| 1057 | // If there's only a single catch-all, getEHDispatchBlock returned |
| 1058 | // that catch-all as the dispatch block. |
| 1059 | if (catchScope.getNumHandlers() == 1 && |
| 1060 | catchScope.getHandler(0).isCatchAll()) { |
| 1061 | assert(dispatchBlock == catchScope.getHandler(0).Block); |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 1062 | return; |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP(); |
| 1066 | CGF.EmitBlockAfterUses(dispatchBlock); |
| 1067 | |
| 1068 | // Select the right handler. |
James Y Knight | 8799cae | 2019-02-03 21:53:49 +0000 | [diff] [blame] | 1069 | llvm::Function *llvm_eh_typeid_for = |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1070 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); |
| 1071 | |
| 1072 | // Load the selector value. |
Bill Wendling | 79a70e4 | 2011-09-15 18:57:19 +0000 | [diff] [blame] | 1073 | llvm::Value *selector = CGF.getSelectorFromSlot(); |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1074 | |
| 1075 | // Test against each of the exception types we claim to catch. |
| 1076 | for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) { |
| 1077 | assert(i < e && "ran off end of handlers!"); |
| 1078 | const EHCatchScope::Handler &handler = catchScope.getHandler(i); |
| 1079 | |
Reid Kleckner | 10aa770 | 2015-09-16 20:15:55 +0000 | [diff] [blame] | 1080 | llvm::Value *typeValue = handler.Type.RTTI; |
| 1081 | assert(handler.Type.Flags == 0 && |
| 1082 | "landingpads do not support catch handler flags"); |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1083 | assert(typeValue && "fell into catch-all case!"); |
| 1084 | typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy); |
| 1085 | |
| 1086 | // Figure out the next block. |
| 1087 | bool nextIsEnd; |
| 1088 | llvm::BasicBlock *nextBlock; |
| 1089 | |
| 1090 | // If this is the last handler, we're at the end, and the next |
| 1091 | // block is the block for the enclosing EH scope. |
| 1092 | if (i + 1 == e) { |
| 1093 | nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope()); |
| 1094 | nextIsEnd = true; |
| 1095 | |
| 1096 | // If the next handler is a catch-all, we're at the end, and the |
| 1097 | // next block is that handler. |
| 1098 | } else if (catchScope.getHandler(i+1).isCatchAll()) { |
| 1099 | nextBlock = catchScope.getHandler(i+1).Block; |
| 1100 | nextIsEnd = true; |
| 1101 | |
| 1102 | // Otherwise, we're not at the end and we need a new block. |
| 1103 | } else { |
| 1104 | nextBlock = CGF.createBasicBlock("catch.fallthrough"); |
| 1105 | nextIsEnd = false; |
| 1106 | } |
| 1107 | |
| 1108 | // Figure out the catch type's index in the LSDA's type table. |
| 1109 | llvm::CallInst *typeIndex = |
| 1110 | CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue); |
| 1111 | typeIndex->setDoesNotThrow(); |
| 1112 | |
| 1113 | llvm::Value *matchesTypeIndex = |
| 1114 | CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches"); |
| 1115 | CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock); |
| 1116 | |
| 1117 | // If the next handler is a catch-all, we're completely done. |
| 1118 | if (nextIsEnd) { |
| 1119 | CGF.Builder.restoreIP(savedIP); |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 1120 | return; |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1121 | } |
Ahmed Charles | 289896d | 2012-02-19 11:57:29 +0000 | [diff] [blame] | 1122 | // Otherwise we need to emit and continue at that block. |
| 1123 | CGF.EmitBlock(nextBlock); |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1124 | } |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | void CodeGenFunction::popCatchScope() { |
| 1128 | EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin()); |
| 1129 | if (catchScope.hasEHBranches()) |
| 1130 | emitCatchDispatchBlock(*this, catchScope); |
| 1131 | EHStack.popCatch(); |
| 1132 | } |
| 1133 | |
John McCall | b609d3f | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1134 | void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1135 | unsigned NumHandlers = S.getNumHandlers(); |
| 1136 | EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin()); |
| 1137 | assert(CatchScope.getNumHandlers() == NumHandlers); |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 1138 | llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock(); |
Mike Stump | 58ef18b | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1139 | |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1140 | // If the catch was not required, bail out now. |
| 1141 | if (!CatchScope.hasEHBranches()) { |
Kostya Serebryany | ba4aced | 2014-01-09 09:22:32 +0000 | [diff] [blame] | 1142 | CatchScope.clearHandlerBlocks(); |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1143 | EHStack.popCatch(); |
| 1144 | return; |
| 1145 | } |
| 1146 | |
| 1147 | // Emit the structure of the EH dispatch for this catch. |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 1148 | emitCatchDispatchBlock(*this, CatchScope); |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1149 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1150 | // Copy the handler blocks off before we pop the EH stack. Emitting |
| 1151 | // the handlers might scribble on this memory. |
Benjamin Kramer | da32cf8 | 2015-08-04 15:38:49 +0000 | [diff] [blame] | 1152 | SmallVector<EHCatchScope::Handler, 8> Handlers( |
| 1153 | CatchScope.begin(), CatchScope.begin() + NumHandlers); |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1154 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1155 | EHStack.popCatch(); |
Mike Stump | 58ef18b | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1156 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1157 | // The fall-through block. |
| 1158 | llvm::BasicBlock *ContBB = createBasicBlock("try.cont"); |
Mike Stump | 58ef18b | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1159 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1160 | // We just emitted the body of the try; jump to the continue block. |
| 1161 | if (HaveInsertPoint()) |
| 1162 | Builder.CreateBr(ContBB); |
Mike Stump | 9732915 | 2009-12-02 19:53:57 +0000 | [diff] [blame] | 1163 | |
John McCall | d8d00be | 2012-06-15 05:27:05 +0000 | [diff] [blame] | 1164 | // Determine if we need an implicit rethrow for all these catch handlers; |
| 1165 | // see the comment below. |
| 1166 | bool doImplicitRethrow = false; |
John McCall | b609d3f | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1167 | if (IsFnTryBlock) |
John McCall | d8d00be | 2012-06-15 05:27:05 +0000 | [diff] [blame] | 1168 | doImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) || |
| 1169 | isa<CXXConstructorDecl>(CurCodeDecl); |
John McCall | b609d3f | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1170 | |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 1171 | // Wasm uses Windows-style EH instructions, but merges all catch clauses into |
| 1172 | // one big catchpad. So we save the old funclet pad here before we traverse |
| 1173 | // each catch handler. |
| 1174 | SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad( |
| 1175 | CurrentFuncletPad); |
| 1176 | llvm::BasicBlock *WasmCatchStartBlock = nullptr; |
| 1177 | if (EHPersonality::get(*this).isWasmPersonality()) { |
| 1178 | auto *CatchSwitch = |
| 1179 | cast<llvm::CatchSwitchInst>(DispatchBlock->getFirstNonPHI()); |
| 1180 | WasmCatchStartBlock = CatchSwitch->hasUnwindDest() |
| 1181 | ? CatchSwitch->getSuccessor(1) |
| 1182 | : CatchSwitch->getSuccessor(0); |
| 1183 | auto *CPI = cast<llvm::CatchPadInst>(WasmCatchStartBlock->getFirstNonPHI()); |
| 1184 | CurrentFuncletPad = CPI; |
| 1185 | } |
| 1186 | |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1187 | // Perversely, we emit the handlers backwards precisely because we |
| 1188 | // want them to appear in source order. In all of these cases, the |
| 1189 | // catch block will have exactly one predecessor, which will be a |
| 1190 | // particular block in the catch dispatch. However, in the case of |
| 1191 | // a catch-all, one of the dispatch blocks will branch to two |
| 1192 | // different handlers, and EmitBlockAfterUses will cause the second |
| 1193 | // handler to be moved before the first. |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 1194 | bool HasCatchAll = false; |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1195 | for (unsigned I = NumHandlers; I != 0; --I) { |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 1196 | HasCatchAll |= Handlers[I - 1].isCatchAll(); |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1197 | llvm::BasicBlock *CatchBlock = Handlers[I-1].Block; |
| 1198 | EmitBlockAfterUses(CatchBlock); |
Mike Stump | 75546b8 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 1199 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1200 | // Catch the exception if this isn't a catch-all. |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1201 | const CXXCatchStmt *C = S.getHandler(I-1); |
Mike Stump | 58ef18b | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1202 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1203 | // Enter a cleanup scope, including the catch variable and the |
| 1204 | // end-catch. |
| 1205 | RunCleanupsScope CatchScope(*this); |
Mike Stump | 58ef18b | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1206 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1207 | // Initialize the catch variable and set up the cleanups. |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 1208 | SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad( |
| 1209 | CurrentFuncletPad); |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 1210 | CGM.getCXXABI().emitBeginCatch(*this, C); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1211 | |
Justin Bogner | ea278c3 | 2014-01-07 00:20:28 +0000 | [diff] [blame] | 1212 | // Emit the PGO counter increment. |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1213 | incrementProfileCounter(C); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1214 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1215 | // Perform the body of the catch. |
| 1216 | EmitStmt(C->getHandlerBlock()); |
| 1217 | |
John McCall | d8d00be | 2012-06-15 05:27:05 +0000 | [diff] [blame] | 1218 | // [except.handle]p11: |
| 1219 | // The currently handled exception is rethrown if control |
| 1220 | // reaches the end of a handler of the function-try-block of a |
| 1221 | // constructor or destructor. |
| 1222 | |
| 1223 | // It is important that we only do this on fallthrough and not on |
| 1224 | // return. Note that it's illegal to put a return in a |
| 1225 | // constructor function-try-block's catch handler (p14), so this |
| 1226 | // really only applies to destructors. |
| 1227 | if (doImplicitRethrow && HaveInsertPoint()) { |
David Majnemer | 442d0a2 | 2014-11-25 07:20:20 +0000 | [diff] [blame] | 1228 | CGM.getCXXABI().emitRethrow(*this, /*isNoReturn*/false); |
John McCall | d8d00be | 2012-06-15 05:27:05 +0000 | [diff] [blame] | 1229 | Builder.CreateUnreachable(); |
| 1230 | Builder.ClearInsertionPoint(); |
| 1231 | } |
| 1232 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1233 | // Fall out through the catch cleanups. |
| 1234 | CatchScope.ForceCleanup(); |
| 1235 | |
| 1236 | // Branch out of the try. |
| 1237 | if (HaveInsertPoint()) |
| 1238 | Builder.CreateBr(ContBB); |
Mike Stump | 58ef18b | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 1241 | // Because in wasm we merge all catch clauses into one big catchpad, in case |
| 1242 | // none of the types in catch handlers matches after we test against each of |
| 1243 | // them, we should unwind to the next EH enclosing scope. We generate a call |
| 1244 | // to rethrow function here to do that. |
| 1245 | if (EHPersonality::get(*this).isWasmPersonality() && !HasCatchAll) { |
| 1246 | assert(WasmCatchStartBlock); |
| 1247 | // Navigate for the "rethrow" block we created in emitWasmCatchPadBlock(). |
| 1248 | // Wasm uses landingpad-style conditional branches to compare selectors, so |
| 1249 | // we follow the false destination for each of the cond branches to reach |
| 1250 | // the rethrow block. |
| 1251 | llvm::BasicBlock *RethrowBlock = WasmCatchStartBlock; |
Chandler Carruth | e303c87 | 2018-10-15 10:42:50 +0000 | [diff] [blame] | 1252 | while (llvm::Instruction *TI = RethrowBlock->getTerminator()) { |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 1253 | auto *BI = cast<llvm::BranchInst>(TI); |
| 1254 | assert(BI->isConditional()); |
| 1255 | RethrowBlock = BI->getSuccessor(1); |
| 1256 | } |
| 1257 | assert(RethrowBlock != WasmCatchStartBlock && RethrowBlock->empty()); |
| 1258 | Builder.SetInsertPoint(RethrowBlock); |
Heejin Ahn | 7e66a50 | 2019-03-16 05:39:12 +0000 | [diff] [blame] | 1259 | llvm::Function *RethrowInCatchFn = |
| 1260 | CGM.getIntrinsic(llvm::Intrinsic::wasm_rethrow_in_catch); |
| 1261 | EmitNoreturnRuntimeCallOrInvoke(RethrowInCatchFn, {}); |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1264 | EmitBlock(ContBB); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1265 | incrementProfileCounter(&S); |
Mike Stump | 58ef18b | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1266 | } |
Mike Stump | aff69af | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1267 | |
John McCall | 1e67040 | 2010-07-21 00:52:03 +0000 | [diff] [blame] | 1268 | namespace { |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 1269 | struct CallEndCatchForFinally final : EHScopeStack::Cleanup { |
John McCall | 1e67040 | 2010-07-21 00:52:03 +0000 | [diff] [blame] | 1270 | llvm::Value *ForEHVar; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1271 | llvm::FunctionCallee EndCatchFn; |
| 1272 | CallEndCatchForFinally(llvm::Value *ForEHVar, |
| 1273 | llvm::FunctionCallee EndCatchFn) |
| 1274 | : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {} |
John McCall | 1e67040 | 2010-07-21 00:52:03 +0000 | [diff] [blame] | 1275 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1276 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 1e67040 | 2010-07-21 00:52:03 +0000 | [diff] [blame] | 1277 | llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch"); |
| 1278 | llvm::BasicBlock *CleanupContBB = |
| 1279 | CGF.createBasicBlock("finally.cleanup.cont"); |
| 1280 | |
| 1281 | llvm::Value *ShouldEndCatch = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1282 | CGF.Builder.CreateFlagLoad(ForEHVar, "finally.endcatch"); |
John McCall | 1e67040 | 2010-07-21 00:52:03 +0000 | [diff] [blame] | 1283 | CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB); |
| 1284 | CGF.EmitBlock(EndCatchBB); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1285 | CGF.EmitRuntimeCallOrInvoke(EndCatchFn); // catch-all, so might throw |
John McCall | 1e67040 | 2010-07-21 00:52:03 +0000 | [diff] [blame] | 1286 | CGF.EmitBlock(CleanupContBB); |
| 1287 | } |
| 1288 | }; |
John McCall | 906da4b | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1289 | |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 1290 | struct PerformFinally final : EHScopeStack::Cleanup { |
John McCall | 906da4b | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1291 | const Stmt *Body; |
| 1292 | llvm::Value *ForEHVar; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1293 | llvm::FunctionCallee EndCatchFn; |
| 1294 | llvm::FunctionCallee RethrowFn; |
John McCall | 906da4b | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1295 | llvm::Value *SavedExnVar; |
| 1296 | |
| 1297 | PerformFinally(const Stmt *Body, llvm::Value *ForEHVar, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1298 | llvm::FunctionCallee EndCatchFn, |
| 1299 | llvm::FunctionCallee RethrowFn, llvm::Value *SavedExnVar) |
| 1300 | : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn), |
| 1301 | RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {} |
John McCall | 906da4b | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1302 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1303 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 906da4b | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1304 | // Enter a cleanup to call the end-catch function if one was provided. |
| 1305 | if (EndCatchFn) |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1306 | CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup, |
| 1307 | ForEHVar, EndCatchFn); |
John McCall | 906da4b | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1308 | |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 1309 | // Save the current cleanup destination in case there are |
| 1310 | // cleanups in the finally block. |
| 1311 | llvm::Value *SavedCleanupDest = |
| 1312 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(), |
| 1313 | "cleanup.dest.saved"); |
| 1314 | |
John McCall | 906da4b | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1315 | // Emit the finally block. |
| 1316 | CGF.EmitStmt(Body); |
| 1317 | |
| 1318 | // If the end of the finally is reachable, check whether this was |
| 1319 | // for EH. If so, rethrow. |
| 1320 | if (CGF.HaveInsertPoint()) { |
| 1321 | llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow"); |
| 1322 | llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont"); |
| 1323 | |
| 1324 | llvm::Value *ShouldRethrow = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1325 | CGF.Builder.CreateFlagLoad(ForEHVar, "finally.shouldthrow"); |
John McCall | 906da4b | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1326 | CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB); |
| 1327 | |
| 1328 | CGF.EmitBlock(RethrowBB); |
| 1329 | if (SavedExnVar) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1330 | CGF.EmitRuntimeCallOrInvoke(RethrowFn, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1331 | CGF.Builder.CreateAlignedLoad(SavedExnVar, CGF.getPointerAlign())); |
John McCall | 906da4b | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1332 | } else { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1333 | CGF.EmitRuntimeCallOrInvoke(RethrowFn); |
John McCall | 906da4b | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1334 | } |
| 1335 | CGF.Builder.CreateUnreachable(); |
| 1336 | |
| 1337 | CGF.EmitBlock(ContBB); |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 1338 | |
| 1339 | // Restore the cleanup destination. |
| 1340 | CGF.Builder.CreateStore(SavedCleanupDest, |
| 1341 | CGF.getNormalCleanupDestSlot()); |
John McCall | 906da4b | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | // Leave the end-catch cleanup. As an optimization, pretend that |
| 1345 | // the fallthrough path was inaccessible; we've dynamically proven |
| 1346 | // that we're not in the EH case along that path. |
| 1347 | if (EndCatchFn) { |
| 1348 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
| 1349 | CGF.PopCleanupBlock(); |
| 1350 | CGF.Builder.restoreIP(SavedIP); |
| 1351 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1352 | |
John McCall | 906da4b | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1353 | // Now make sure we actually have an insertion point or the |
| 1354 | // cleanup gods will hate us. |
| 1355 | CGF.EnsureInsertPoint(); |
| 1356 | } |
| 1357 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1358 | } // end anonymous namespace |
John McCall | 1e67040 | 2010-07-21 00:52:03 +0000 | [diff] [blame] | 1359 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1360 | /// Enters a finally block for an implementation using zero-cost |
| 1361 | /// exceptions. This is mostly general, but hard-codes some |
| 1362 | /// language/ABI-specific behavior in the catch-all sections. |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1363 | void CodeGenFunction::FinallyInfo::enter(CodeGenFunction &CGF, const Stmt *body, |
| 1364 | llvm::FunctionCallee beginCatchFn, |
| 1365 | llvm::FunctionCallee endCatchFn, |
| 1366 | llvm::FunctionCallee rethrowFn) { |
| 1367 | assert((!!beginCatchFn) == (!!endCatchFn) && |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1368 | "begin/end catch functions not paired"); |
John McCall | 6b0feb7 | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1369 | assert(rethrowFn && "rethrow function is required"); |
| 1370 | |
| 1371 | BeginCatchFn = beginCatchFn; |
Mike Stump | aff69af | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1372 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1373 | // The rethrow function has one of the following two types: |
| 1374 | // void (*)() |
| 1375 | // void (*)(void*) |
| 1376 | // In the latter case we need to pass it the exception object. |
| 1377 | // But we can't use the exception slot because the @finally might |
| 1378 | // have a landing pad (which would overwrite the exception slot). |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1379 | llvm::FunctionType *rethrowFnTy = rethrowFn.getFunctionType(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1380 | SavedExnVar = nullptr; |
John McCall | 6b0feb7 | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1381 | if (rethrowFnTy->getNumParams()) |
| 1382 | SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn"); |
Mike Stump | aff69af | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1383 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1384 | // A finally block is a statement which must be executed on any edge |
| 1385 | // out of a given scope. Unlike a cleanup, the finally block may |
| 1386 | // contain arbitrary control flow leading out of itself. In |
| 1387 | // addition, finally blocks should always be executed, even if there |
| 1388 | // are no catch handlers higher on the stack. Therefore, we |
| 1389 | // surround the protected scope with a combination of a normal |
| 1390 | // cleanup (to catch attempts to break out of the block via normal |
| 1391 | // control flow) and an EH catch-all (semantically "outside" any try |
| 1392 | // statement to which the finally block might have been attached). |
| 1393 | // The finally block itself is generated in the context of a cleanup |
| 1394 | // which conditionally leaves the catch-all. |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1395 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1396 | // Jump destination for performing the finally block on an exception |
| 1397 | // edge. We'll never actually reach this block, so unreachable is |
| 1398 | // fine. |
John McCall | 6b0feb7 | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1399 | RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock()); |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1400 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1401 | // Whether the finally block is being executed for EH purposes. |
John McCall | 6b0feb7 | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1402 | ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1403 | CGF.Builder.CreateFlagStore(false, ForEHVar); |
Mike Stump | aff69af | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1404 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1405 | // Enter a normal cleanup which will perform the @finally block. |
John McCall | 6b0feb7 | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1406 | CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body, |
| 1407 | ForEHVar, endCatchFn, |
| 1408 | rethrowFn, SavedExnVar); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1409 | |
| 1410 | // Enter a catch-all scope. |
John McCall | 6b0feb7 | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1411 | llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall"); |
| 1412 | EHCatchScope *catchScope = CGF.EHStack.pushCatch(1); |
| 1413 | catchScope->setCatchAllHandler(0, catchBB); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
John McCall | 6b0feb7 | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1416 | void CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1417 | // Leave the finally catch-all. |
John McCall | 6b0feb7 | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1418 | EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin()); |
| 1419 | llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block; |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1420 | |
| 1421 | CGF.popCatchScope(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1422 | |
John McCall | 6b0feb7 | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1423 | // If there are any references to the catch-all block, emit it. |
| 1424 | if (catchBB->use_empty()) { |
| 1425 | delete catchBB; |
| 1426 | } else { |
| 1427 | CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP(); |
| 1428 | CGF.EmitBlock(catchBB); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1429 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1430 | llvm::Value *exn = nullptr; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1431 | |
John McCall | 6b0feb7 | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1432 | // If there's a begin-catch function, call it. |
| 1433 | if (BeginCatchFn) { |
Bill Wendling | 79a70e4 | 2011-09-15 18:57:19 +0000 | [diff] [blame] | 1434 | exn = CGF.getExceptionFromSlot(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1435 | CGF.EmitNounwindRuntimeCall(BeginCatchFn, exn); |
John McCall | 6b0feb7 | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | // If we need to remember the exception pointer to rethrow later, do so. |
| 1439 | if (SavedExnVar) { |
Bill Wendling | 79a70e4 | 2011-09-15 18:57:19 +0000 | [diff] [blame] | 1440 | if (!exn) exn = CGF.getExceptionFromSlot(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1441 | CGF.Builder.CreateAlignedStore(exn, SavedExnVar, CGF.getPointerAlign()); |
John McCall | 6b0feb7 | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | // Tell the cleanups in the finally block that we're do this for EH. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1445 | CGF.Builder.CreateFlagStore(true, ForEHVar); |
John McCall | 6b0feb7 | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1446 | |
| 1447 | // Thread a jump through the finally cleanup. |
| 1448 | CGF.EmitBranchThroughCleanup(RethrowDest); |
| 1449 | |
| 1450 | CGF.Builder.restoreIP(savedIP); |
| 1451 | } |
| 1452 | |
| 1453 | // Finally, leave the @finally cleanup. |
| 1454 | CGF.PopCleanupBlock(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() { |
| 1458 | if (TerminateLandingPad) |
| 1459 | return TerminateLandingPad; |
| 1460 | |
| 1461 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
| 1462 | |
| 1463 | // This will get inserted at the end of the function. |
| 1464 | TerminateLandingPad = createBasicBlock("terminate.lpad"); |
| 1465 | Builder.SetInsertPoint(TerminateLandingPad); |
| 1466 | |
| 1467 | // Tell the backend that this is a landing pad. |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 1468 | const EHPersonality &Personality = EHPersonality::get(*this); |
David Majnemer | fcbdb6e | 2015-06-17 20:53:19 +0000 | [diff] [blame] | 1469 | |
| 1470 | if (!CurFn->hasPersonalityFn()) |
| 1471 | CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality)); |
| 1472 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 1473 | llvm::LandingPadInst *LPadInst = |
| 1474 | Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty), 0); |
Bill Wendling | f0724e8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 1475 | LPadInst->addClause(getCatchAllValue(*this)); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1476 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1477 | llvm::Value *Exn = nullptr; |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 1478 | if (getLangOpts().CPlusPlus) |
| 1479 | Exn = Builder.CreateExtractValue(LPadInst, 0); |
| 1480 | llvm::CallInst *terminateCall = |
| 1481 | CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn); |
John McCall | e142ad5 | 2013-02-12 03:51:46 +0000 | [diff] [blame] | 1482 | terminateCall->setDoesNotReturn(); |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1483 | Builder.CreateUnreachable(); |
Mike Stump | aff69af | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1484 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1485 | // Restore the saved insertion state. |
| 1486 | Builder.restoreIP(SavedIP); |
John McCall | dac3ea6 | 2010-04-30 00:06:43 +0000 | [diff] [blame] | 1487 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1488 | return TerminateLandingPad; |
Mike Stump | aff69af | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1489 | } |
Mike Stump | 2b48887 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 1490 | |
| 1491 | llvm::BasicBlock *CodeGenFunction::getTerminateHandler() { |
Mike Stump | f5cbb08 | 2009-12-10 00:02:42 +0000 | [diff] [blame] | 1492 | if (TerminateHandler) |
| 1493 | return TerminateHandler; |
| 1494 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1495 | // Set up the terminate handler. This block is inserted at the very |
| 1496 | // end of the function by FinishFunction. |
Mike Stump | f5cbb08 | 2009-12-10 00:02:42 +0000 | [diff] [blame] | 1497 | TerminateHandler = createBasicBlock("terminate.handler"); |
Reid Kleckner | 06f19a0 | 2018-01-02 21:34:16 +0000 | [diff] [blame] | 1498 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1499 | Builder.SetInsertPoint(TerminateHandler); |
Reid Kleckner | 06f19a0 | 2018-01-02 21:34:16 +0000 | [diff] [blame] | 1500 | |
David Majnemer | feeefb2 | 2015-12-14 18:34:18 +0000 | [diff] [blame] | 1501 | llvm::Value *Exn = nullptr; |
Reid Kleckner | 06f19a0 | 2018-01-02 21:34:16 +0000 | [diff] [blame] | 1502 | if (getLangOpts().CPlusPlus) |
| 1503 | Exn = getExceptionFromSlot(); |
David Majnemer | feeefb2 | 2015-12-14 18:34:18 +0000 | [diff] [blame] | 1504 | llvm::CallInst *terminateCall = |
| 1505 | CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn); |
| 1506 | terminateCall->setDoesNotReturn(); |
| 1507 | Builder.CreateUnreachable(); |
Mike Stump | 2b48887 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 1508 | |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1509 | // Restore the saved insertion state. |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1510 | Builder.restoreIP(SavedIP); |
Mike Stump | 25b20fc | 2009-12-09 23:31:35 +0000 | [diff] [blame] | 1511 | |
Mike Stump | 2b48887 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 1512 | return TerminateHandler; |
| 1513 | } |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1514 | |
Reid Kleckner | 06f19a0 | 2018-01-02 21:34:16 +0000 | [diff] [blame] | 1515 | llvm::BasicBlock *CodeGenFunction::getTerminateFunclet() { |
| 1516 | assert(EHPersonality::get(*this).usesFuncletPads() && |
| 1517 | "use getTerminateLandingPad for non-funclet EH"); |
| 1518 | |
| 1519 | llvm::BasicBlock *&TerminateFunclet = TerminateFunclets[CurrentFuncletPad]; |
| 1520 | if (TerminateFunclet) |
| 1521 | return TerminateFunclet; |
| 1522 | |
| 1523 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
| 1524 | |
| 1525 | // Set up the terminate handler. This block is inserted at the very |
| 1526 | // end of the function by FinishFunction. |
| 1527 | TerminateFunclet = createBasicBlock("terminate.handler"); |
| 1528 | Builder.SetInsertPoint(TerminateFunclet); |
| 1529 | |
| 1530 | // Create the cleanuppad using the current parent pad as its token. Use 'none' |
| 1531 | // if this is a top-level terminate scope, which is the common case. |
| 1532 | SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad( |
| 1533 | CurrentFuncletPad); |
| 1534 | llvm::Value *ParentPad = CurrentFuncletPad; |
| 1535 | if (!ParentPad) |
| 1536 | ParentPad = llvm::ConstantTokenNone::get(CGM.getLLVMContext()); |
| 1537 | CurrentFuncletPad = Builder.CreateCleanupPad(ParentPad); |
| 1538 | |
| 1539 | // Emit the __std_terminate call. |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 1540 | llvm::Value *Exn = nullptr; |
| 1541 | // In case of wasm personality, we need to pass the exception value to |
| 1542 | // __clang_call_terminate function. |
| 1543 | if (getLangOpts().CPlusPlus && |
| 1544 | EHPersonality::get(*this).isWasmPersonality()) { |
James Y Knight | 8799cae | 2019-02-03 21:53:49 +0000 | [diff] [blame] | 1545 | llvm::Function *GetExnFn = |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 1546 | CGM.getIntrinsic(llvm::Intrinsic::wasm_get_exception); |
| 1547 | Exn = Builder.CreateCall(GetExnFn, CurrentFuncletPad); |
| 1548 | } |
Reid Kleckner | 06f19a0 | 2018-01-02 21:34:16 +0000 | [diff] [blame] | 1549 | llvm::CallInst *terminateCall = |
Heejin Ahn | c647919 | 2018-05-31 22:18:13 +0000 | [diff] [blame] | 1550 | CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn); |
Reid Kleckner | 06f19a0 | 2018-01-02 21:34:16 +0000 | [diff] [blame] | 1551 | terminateCall->setDoesNotReturn(); |
| 1552 | Builder.CreateUnreachable(); |
| 1553 | |
| 1554 | // Restore the saved insertion state. |
| 1555 | Builder.restoreIP(SavedIP); |
| 1556 | |
| 1557 | return TerminateFunclet; |
| 1558 | } |
| 1559 | |
David Chisnall | 9a837be | 2012-11-07 16:50:40 +0000 | [diff] [blame] | 1560 | llvm::BasicBlock *CodeGenFunction::getEHResumeBlock(bool isCleanup) { |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1561 | if (EHResumeBlock) return EHResumeBlock; |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1562 | |
| 1563 | CGBuilderTy::InsertPoint SavedIP = Builder.saveIP(); |
| 1564 | |
| 1565 | // 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] | 1566 | EHResumeBlock = createBasicBlock("eh.resume"); |
| 1567 | Builder.SetInsertPoint(EHResumeBlock); |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1568 | |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 1569 | const EHPersonality &Personality = EHPersonality::get(*this); |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1570 | |
| 1571 | // This can always be a call because we necessarily didn't find |
| 1572 | // anything on the EH stack which needs our help. |
Benjamin Kramer | 793bd55 | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 1573 | const char *RethrowName = Personality.CatchallRethrowFn; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1574 | if (RethrowName != nullptr && !isCleanup) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1575 | EmitRuntimeCall(getCatchallRethrowFn(CGM, RethrowName), |
Nico Weber | ff62a6a | 2015-02-26 22:34:33 +0000 | [diff] [blame] | 1576 | getExceptionFromSlot())->setDoesNotReturn(); |
Logan Chien | e9c8ccb | 2014-07-01 11:47:10 +0000 | [diff] [blame] | 1577 | Builder.CreateUnreachable(); |
| 1578 | Builder.restoreIP(SavedIP); |
| 1579 | return EHResumeBlock; |
John McCall | 9b382dd | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Logan Chien | e9c8ccb | 2014-07-01 11:47:10 +0000 | [diff] [blame] | 1582 | // Recreate the landingpad's return value for the 'resume' instruction. |
| 1583 | llvm::Value *Exn = getExceptionFromSlot(); |
| 1584 | llvm::Value *Sel = getSelectorFromSlot(); |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1585 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 1586 | llvm::Type *LPadType = llvm::StructType::get(Exn->getType(), Sel->getType()); |
Logan Chien | e9c8ccb | 2014-07-01 11:47:10 +0000 | [diff] [blame] | 1587 | llvm::Value *LPadVal = llvm::UndefValue::get(LPadType); |
| 1588 | LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val"); |
| 1589 | LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val"); |
| 1590 | |
| 1591 | Builder.CreateResume(LPadVal); |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1592 | Builder.restoreIP(SavedIP); |
John McCall | 8e4c74b | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1593 | return EHResumeBlock; |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1594 | } |
Reid Kleckner | 543a16c | 2013-09-16 21:46:30 +0000 | [diff] [blame] | 1595 | |
| 1596 | void CodeGenFunction::EmitSEHTryStmt(const SEHTryStmt &S) { |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1597 | EnterSEHTryStmt(S); |
Reid Kleckner | a593000 | 2015-02-11 21:40:48 +0000 | [diff] [blame] | 1598 | { |
Nico Weber | 5779f84 | 2015-02-12 23:16:11 +0000 | [diff] [blame] | 1599 | JumpDest TryExit = getJumpDestInCurrentScope("__try.__leave"); |
Nico Weber | 5779f84 | 2015-02-12 23:16:11 +0000 | [diff] [blame] | 1600 | |
Reid Kleckner | 11c033e | 2015-02-12 23:40:45 +0000 | [diff] [blame] | 1601 | SEHTryEpilogueStack.push_back(&TryExit); |
Reid Kleckner | a593000 | 2015-02-11 21:40:48 +0000 | [diff] [blame] | 1602 | EmitStmt(S.getTryBlock()); |
Reid Kleckner | 11c033e | 2015-02-12 23:40:45 +0000 | [diff] [blame] | 1603 | SEHTryEpilogueStack.pop_back(); |
Nico Weber | 5779f84 | 2015-02-12 23:16:11 +0000 | [diff] [blame] | 1604 | |
| 1605 | if (!TryExit.getBlock()->use_empty()) |
| 1606 | EmitBlock(TryExit.getBlock(), /*IsFinished=*/true); |
| 1607 | else |
| 1608 | delete TryExit.getBlock(); |
Reid Kleckner | a593000 | 2015-02-11 21:40:48 +0000 | [diff] [blame] | 1609 | } |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1610 | ExitSEHTryStmt(S); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | namespace { |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 1614 | struct PerformSEHFinally final : EHScopeStack::Cleanup { |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1615 | llvm::Function *OutlinedFinally; |
Reid Kleckner | 5539152 | 2015-10-08 21:14:56 +0000 | [diff] [blame] | 1616 | PerformSEHFinally(llvm::Function *OutlinedFinally) |
| 1617 | : OutlinedFinally(OutlinedFinally) {} |
Reid Kleckner | aca01db | 2015-02-04 22:37:07 +0000 | [diff] [blame] | 1618 | |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1619 | void Emit(CodeGenFunction &CGF, Flags F) override { |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1620 | ASTContext &Context = CGF.getContext(); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1621 | CodeGenModule &CGM = CGF.CGM; |
Reid Kleckner | 6587044 | 2015-06-09 17:47:50 +0000 | [diff] [blame] | 1622 | |
Reid Kleckner | d0d9a1f | 2015-07-01 17:10:10 +0000 | [diff] [blame] | 1623 | CallArgList Args; |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1624 | |
| 1625 | // Compute the two argument values. |
| 1626 | QualType ArgTys[2] = {Context.UnsignedCharTy, Context.VoidPtrTy}; |
Sanjin Sijaric | cfa2a2a | 2019-01-16 07:39:44 +0000 | [diff] [blame] | 1627 | llvm::Value *FP = nullptr; |
| 1628 | // If CFG.IsOutlinedSEHHelper is true, then we are within a finally block. |
| 1629 | if (CGF.IsOutlinedSEHHelper) { |
| 1630 | FP = &CGF.CurFn->arg_begin()[1]; |
| 1631 | } else { |
James Y Knight | 8799cae | 2019-02-03 21:53:49 +0000 | [diff] [blame] | 1632 | llvm::Function *LocalAddrFn = |
Sanjin Sijaric | cfa2a2a | 2019-01-16 07:39:44 +0000 | [diff] [blame] | 1633 | CGM.getIntrinsic(llvm::Intrinsic::localaddress); |
| 1634 | FP = CGF.Builder.CreateCall(LocalAddrFn); |
| 1635 | } |
| 1636 | |
Reid Kleckner | eb11c41 | 2015-07-01 21:00:00 +0000 | [diff] [blame] | 1637 | llvm::Value *IsForEH = |
| 1638 | llvm::ConstantInt::get(CGF.ConvertType(ArgTys[0]), F.isForEHCleanup()); |
| 1639 | Args.add(RValue::get(IsForEH), ArgTys[0]); |
| 1640 | Args.add(RValue::get(FP), ArgTys[1]); |
Reid Kleckner | d0d9a1f | 2015-07-01 17:10:10 +0000 | [diff] [blame] | 1641 | |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1642 | // Arrange a two-arg function info and type. |
Reid Kleckner | eb11c41 | 2015-07-01 21:00:00 +0000 | [diff] [blame] | 1643 | const CGFunctionInfo &FnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 1644 | CGM.getTypes().arrangeBuiltinFunctionCall(Context.VoidTy, Args); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1645 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 1646 | auto Callee = CGCallee::forDirect(OutlinedFinally); |
| 1647 | CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1648 | } |
| 1649 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1650 | } // end anonymous namespace |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1651 | |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1652 | namespace { |
| 1653 | /// Find all local variable captures in the statement. |
| 1654 | struct CaptureFinder : ConstStmtVisitor<CaptureFinder> { |
| 1655 | CodeGenFunction &ParentCGF; |
| 1656 | const VarDecl *ParentThis; |
John McCall | 0a49015 | 2015-09-08 21:15:22 +0000 | [diff] [blame] | 1657 | llvm::SmallSetVector<const VarDecl *, 4> Captures; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1658 | Address SEHCodeSlot = Address::invalid(); |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1659 | CaptureFinder(CodeGenFunction &ParentCGF, const VarDecl *ParentThis) |
| 1660 | : ParentCGF(ParentCGF), ParentThis(ParentThis) {} |
| 1661 | |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1662 | // Return true if we need to do any capturing work. |
| 1663 | bool foundCaptures() { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1664 | return !Captures.empty() || SEHCodeSlot.isValid(); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1667 | void Visit(const Stmt *S) { |
| 1668 | // See if this is a capture, then recurse. |
| 1669 | ConstStmtVisitor<CaptureFinder>::Visit(S); |
| 1670 | for (const Stmt *Child : S->children()) |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1671 | if (Child) |
| 1672 | Visit(Child); |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | void VisitDeclRefExpr(const DeclRefExpr *E) { |
| 1676 | // If this is already a capture, just make sure we capture 'this'. |
| 1677 | if (E->refersToEnclosingVariableOrCapture()) { |
John McCall | 0a49015 | 2015-09-08 21:15:22 +0000 | [diff] [blame] | 1678 | Captures.insert(ParentThis); |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1679 | return; |
| 1680 | } |
| 1681 | |
| 1682 | const auto *D = dyn_cast<VarDecl>(E->getDecl()); |
| 1683 | if (D && D->isLocalVarDeclOrParm() && D->hasLocalStorage()) |
John McCall | 0a49015 | 2015-09-08 21:15:22 +0000 | [diff] [blame] | 1684 | Captures.insert(D); |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | void VisitCXXThisExpr(const CXXThisExpr *E) { |
John McCall | 0a49015 | 2015-09-08 21:15:22 +0000 | [diff] [blame] | 1688 | Captures.insert(ParentThis); |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1689 | } |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1690 | |
| 1691 | void VisitCallExpr(const CallExpr *E) { |
| 1692 | // We only need to add parent frame allocations for these builtins in x86. |
| 1693 | if (ParentCGF.getTarget().getTriple().getArch() != llvm::Triple::x86) |
| 1694 | return; |
| 1695 | |
| 1696 | unsigned ID = E->getBuiltinCallee(); |
| 1697 | switch (ID) { |
| 1698 | case Builtin::BI__exception_code: |
| 1699 | case Builtin::BI_exception_code: |
| 1700 | // This is the simple case where we are the outermost finally. All we |
| 1701 | // have to do here is make sure we escape this and recover it in the |
| 1702 | // outlined handler. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1703 | if (!SEHCodeSlot.isValid()) |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1704 | SEHCodeSlot = ParentCGF.SEHCodeSlotStack.back(); |
| 1705 | break; |
| 1706 | } |
| 1707 | } |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1708 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1709 | } // end anonymous namespace |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1710 | |
Reid Kleckner | 39329d57b | 2015-12-16 00:26:37 +0000 | [diff] [blame] | 1711 | Address CodeGenFunction::recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF, |
| 1712 | Address ParentVar, |
| 1713 | llvm::Value *ParentFP) { |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1714 | llvm::CallInst *RecoverCall = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1715 | CGBuilderTy Builder(*this, AllocaInsertPt); |
| 1716 | if (auto *ParentAlloca = dyn_cast<llvm::AllocaInst>(ParentVar.getPointer())) { |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1717 | // Mark the variable escaped if nobody else referenced it and compute the |
Reid Kleckner | 98cb8ba | 2015-07-07 22:26:07 +0000 | [diff] [blame] | 1718 | // localescape index. |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1719 | auto InsertPair = ParentCGF.EscapedLocals.insert( |
| 1720 | std::make_pair(ParentAlloca, ParentCGF.EscapedLocals.size())); |
| 1721 | int FrameEscapeIdx = InsertPair.first->second; |
Reid Kleckner | 98cb8ba | 2015-07-07 22:26:07 +0000 | [diff] [blame] | 1722 | // call i8* @llvm.localrecover(i8* bitcast(@parentFn), i8* %fp, i32 N) |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1723 | llvm::Function *FrameRecoverFn = llvm::Intrinsic::getDeclaration( |
Reid Kleckner | 98cb8ba | 2015-07-07 22:26:07 +0000 | [diff] [blame] | 1724 | &CGM.getModule(), llvm::Intrinsic::localrecover); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1725 | llvm::Constant *ParentI8Fn = |
| 1726 | llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy); |
| 1727 | RecoverCall = Builder.CreateCall( |
| 1728 | FrameRecoverFn, {ParentI8Fn, ParentFP, |
| 1729 | llvm::ConstantInt::get(Int32Ty, FrameEscapeIdx)}); |
| 1730 | |
| 1731 | } else { |
| 1732 | // If the parent didn't have an alloca, we're doing some nested outlining. |
Reid Kleckner | 98cb8ba | 2015-07-07 22:26:07 +0000 | [diff] [blame] | 1733 | // Just clone the existing localrecover call, but tweak the FP argument to |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1734 | // use our FP value. All other arguments are constants. |
| 1735 | auto *ParentRecover = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1736 | cast<llvm::IntrinsicInst>(ParentVar.getPointer()->stripPointerCasts()); |
Reid Kleckner | 98cb8ba | 2015-07-07 22:26:07 +0000 | [diff] [blame] | 1737 | assert(ParentRecover->getIntrinsicID() == llvm::Intrinsic::localrecover && |
| 1738 | "expected alloca or localrecover in parent LocalDeclMap"); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1739 | RecoverCall = cast<llvm::CallInst>(ParentRecover->clone()); |
| 1740 | RecoverCall->setArgOperand(1, ParentFP); |
| 1741 | RecoverCall->insertBefore(AllocaInsertPt); |
| 1742 | } |
| 1743 | |
| 1744 | // Bitcast the variable, rename it, and insert it in the local decl map. |
| 1745 | llvm::Value *ChildVar = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1746 | Builder.CreateBitCast(RecoverCall, ParentVar.getType()); |
| 1747 | ChildVar->setName(ParentVar.getName()); |
| 1748 | return Address(ChildVar, ParentVar.getAlignment()); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1751 | void CodeGenFunction::EmitCapturedLocals(CodeGenFunction &ParentCGF, |
Reid Kleckner | 0b9bbbf | 2015-06-09 17:49:42 +0000 | [diff] [blame] | 1752 | const Stmt *OutlinedStmt, |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1753 | bool IsFilter) { |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1754 | // Find all captures in the Stmt. |
| 1755 | CaptureFinder Finder(ParentCGF, ParentCGF.CXXABIThisDecl); |
| 1756 | Finder.Visit(OutlinedStmt); |
| 1757 | |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1758 | // We can exit early on x86_64 when there are no captures. We just have to |
| 1759 | // save the exception code in filters so that __exception_code() works. |
| 1760 | if (!Finder.foundCaptures() && |
| 1761 | CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) { |
| 1762 | if (IsFilter) |
| 1763 | EmitSEHExceptionCodeSave(ParentCGF, nullptr, nullptr); |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1764 | return; |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1765 | } |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1766 | |
Reid Kleckner | 39329d57b | 2015-12-16 00:26:37 +0000 | [diff] [blame] | 1767 | llvm::Value *EntryFP = nullptr; |
| 1768 | CGBuilderTy Builder(CGM, AllocaInsertPt); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1769 | if (IsFilter && CGM.getTarget().getTriple().getArch() == llvm::Triple::x86) { |
| 1770 | // 32-bit SEH filters need to be careful about FP recovery. The end of the |
| 1771 | // EH registration is passed in as the EBP physical register. We can |
Reid Kleckner | 39329d57b | 2015-12-16 00:26:37 +0000 | [diff] [blame] | 1772 | // recover that with llvm.frameaddress(1). |
| 1773 | EntryFP = Builder.CreateCall( |
Christudasan Devadasan | 8c5e6fa | 2019-07-22 12:50:30 +0000 | [diff] [blame] | 1774 | CGM.getIntrinsic(llvm::Intrinsic::frameaddress, AllocaInt8PtrTy), |
| 1775 | {Builder.getInt32(1)}); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1776 | } else { |
| 1777 | // Otherwise, for x64 and 32-bit finally functions, the parent FP is the |
| 1778 | // second parameter. |
| 1779 | auto AI = CurFn->arg_begin(); |
| 1780 | ++AI; |
Reid Kleckner | 39329d57b | 2015-12-16 00:26:37 +0000 | [diff] [blame] | 1781 | EntryFP = &*AI; |
| 1782 | } |
| 1783 | |
| 1784 | llvm::Value *ParentFP = EntryFP; |
| 1785 | if (IsFilter) { |
| 1786 | // Given whatever FP the runtime provided us in EntryFP, recover the true |
| 1787 | // frame pointer of the parent function. We only need to do this in filters, |
| 1788 | // since finally funclets recover the parent FP for us. |
| 1789 | llvm::Function *RecoverFPIntrin = |
Eli Friedman | c4c43b2 | 2019-01-16 00:50:44 +0000 | [diff] [blame] | 1790 | CGM.getIntrinsic(llvm::Intrinsic::eh_recoverfp); |
Reid Kleckner | 39329d57b | 2015-12-16 00:26:37 +0000 | [diff] [blame] | 1791 | llvm::Constant *ParentI8Fn = |
| 1792 | llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy); |
| 1793 | ParentFP = Builder.CreateCall(RecoverFPIntrin, {ParentI8Fn, EntryFP}); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1794 | } |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1795 | |
Reid Kleckner | 98cb8ba | 2015-07-07 22:26:07 +0000 | [diff] [blame] | 1796 | // Create llvm.localrecover calls for all captures. |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1797 | for (const VarDecl *VD : Finder.Captures) { |
| 1798 | if (isa<ImplicitParamDecl>(VD)) { |
| 1799 | CGM.ErrorUnsupported(VD, "'this' captured by SEH"); |
| 1800 | CXXThisValue = llvm::UndefValue::get(ConvertTypeForMem(VD->getType())); |
| 1801 | continue; |
| 1802 | } |
| 1803 | if (VD->getType()->isVariablyModifiedType()) { |
| 1804 | CGM.ErrorUnsupported(VD, "VLA captured by SEH"); |
| 1805 | continue; |
| 1806 | } |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1807 | assert((isa<ImplicitParamDecl>(VD) || VD->isLocalVarDeclOrParm()) && |
| 1808 | "captured non-local variable"); |
| 1809 | |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1810 | // If this decl hasn't been declared yet, it will be declared in the |
| 1811 | // OutlinedStmt. |
| 1812 | auto I = ParentCGF.LocalDeclMap.find(VD); |
| 1813 | if (I == ParentCGF.LocalDeclMap.end()) |
| 1814 | continue; |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1815 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1816 | Address ParentVar = I->second; |
Reid Kleckner | 39329d57b | 2015-12-16 00:26:37 +0000 | [diff] [blame] | 1817 | setAddrOfLocalVar( |
| 1818 | VD, recoverAddrOfEscapedLocal(ParentCGF, ParentVar, ParentFP)); |
Nico Weber | e4f974c | 2015-07-02 06:10:53 +0000 | [diff] [blame] | 1819 | } |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1820 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1821 | if (Finder.SEHCodeSlot.isValid()) { |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1822 | SEHCodeSlotStack.push_back( |
| 1823 | recoverAddrOfEscapedLocal(ParentCGF, Finder.SEHCodeSlot, ParentFP)); |
| 1824 | } |
| 1825 | |
| 1826 | if (IsFilter) |
Reid Kleckner | 39329d57b | 2015-12-16 00:26:37 +0000 | [diff] [blame] | 1827 | EmitSEHExceptionCodeSave(ParentCGF, ParentFP, EntryFP); |
Reid Kleckner | 31a1bb0 | 2015-04-08 22:23:48 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1830 | /// Arrange a function prototype that can be called by Windows exception |
| 1831 | /// handling personalities. On Win64, the prototype looks like: |
| 1832 | /// RetTy func(void *EHPtrs, void *ParentFP); |
| 1833 | void CodeGenFunction::startOutlinedSEHHelper(CodeGenFunction &ParentCGF, |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1834 | bool IsFilter, |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1835 | const Stmt *OutlinedStmt) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1836 | SourceLocation StartLoc = OutlinedStmt->getBeginLoc(); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1837 | |
| 1838 | // Get the mangled function name. |
| 1839 | SmallString<128> Name; |
| 1840 | { |
| 1841 | llvm::raw_svector_ostream OS(Name); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1842 | const NamedDecl *ParentSEHFn = ParentCGF.CurSEHParent; |
David Majnemer | 25eb165 | 2016-03-01 19:42:53 +0000 | [diff] [blame] | 1843 | assert(ParentSEHFn && "No CurSEHParent!"); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1844 | MangleContext &Mangler = CGM.getCXXABI().getMangleContext(); |
| 1845 | if (IsFilter) |
David Majnemer | 25eb165 | 2016-03-01 19:42:53 +0000 | [diff] [blame] | 1846 | Mangler.mangleSEHFilterExpression(ParentSEHFn, OS); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1847 | else |
David Majnemer | 25eb165 | 2016-03-01 19:42:53 +0000 | [diff] [blame] | 1848 | Mangler.mangleSEHFinallyBlock(ParentSEHFn, OS); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | FunctionArgList Args; |
| 1852 | if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 || !IsFilter) { |
| 1853 | // All SEH finally functions take two parameters. Win64 filters take two |
| 1854 | // parameters. Win32 filters take no parameters. |
| 1855 | if (IsFilter) { |
| 1856 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1857 | getContext(), /*DC=*/nullptr, StartLoc, |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1858 | &getContext().Idents.get("exception_pointers"), |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1859 | getContext().VoidPtrTy, ImplicitParamDecl::Other)); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1860 | } else { |
| 1861 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1862 | getContext(), /*DC=*/nullptr, StartLoc, |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1863 | &getContext().Idents.get("abnormal_termination"), |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1864 | getContext().UnsignedCharTy, ImplicitParamDecl::Other)); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1865 | } |
| 1866 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1867 | getContext(), /*DC=*/nullptr, StartLoc, |
| 1868 | &getContext().Idents.get("frame_pointer"), getContext().VoidPtrTy, |
| 1869 | ImplicitParamDecl::Other)); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | QualType RetTy = IsFilter ? getContext().LongTy : getContext().VoidTy; |
| 1873 | |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 1874 | const CGFunctionInfo &FnInfo = |
| 1875 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(RetTy, Args); |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1876 | |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1877 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1878 | llvm::Function *Fn = llvm::Function::Create( |
| 1879 | FnTy, llvm::GlobalValue::InternalLinkage, Name.str(), &CGM.getModule()); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1880 | |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1881 | IsOutlinedSEHHelper = true; |
Nico Weber | f2a39a7 | 2015-04-13 20:03:03 +0000 | [diff] [blame] | 1882 | |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1883 | StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1884 | OutlinedStmt->getBeginLoc(), OutlinedStmt->getBeginLoc()); |
David Majnemer | 25eb165 | 2016-03-01 19:42:53 +0000 | [diff] [blame] | 1885 | CurSEHParent = ParentCGF.CurSEHParent; |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1886 | |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 1887 | CGM.SetLLVMFunctionAttributes(GlobalDecl(), FnInfo, CurFn); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1888 | EmitCapturedLocals(ParentCGF, OutlinedStmt, IsFilter); |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | /// Create a stub filter function that will ultimately hold the code of the |
| 1892 | /// filter expression. The EH preparation passes in LLVM will outline the code |
| 1893 | /// from the main function body into this stub. |
| 1894 | llvm::Function * |
| 1895 | CodeGenFunction::GenerateSEHFilterFunction(CodeGenFunction &ParentCGF, |
| 1896 | const SEHExceptStmt &Except) { |
| 1897 | const Expr *FilterExpr = Except.getFilterExpr(); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1898 | startOutlinedSEHHelper(ParentCGF, true, FilterExpr); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1899 | |
| 1900 | // Emit the original filter expression, convert to i32, and return. |
| 1901 | llvm::Value *R = EmitScalarExpr(FilterExpr); |
David Majnemer | 2ccba83 | 2015-04-17 06:57:25 +0000 | [diff] [blame] | 1902 | R = Builder.CreateIntCast(R, ConvertType(getContext().LongTy), |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1903 | FilterExpr->getType()->isSignedIntegerType()); |
| 1904 | Builder.CreateStore(R, ReturnValue); |
| 1905 | |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1906 | FinishFunction(FilterExpr->getEndLoc()); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1907 | |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1908 | return CurFn; |
| 1909 | } |
| 1910 | |
| 1911 | llvm::Function * |
| 1912 | CodeGenFunction::GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF, |
| 1913 | const SEHFinallyStmt &Finally) { |
| 1914 | const Stmt *FinallyBlock = Finally.getBlock(); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1915 | startOutlinedSEHHelper(ParentCGF, false, FinallyBlock); |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1916 | |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1917 | // Emit the original filter expression, convert to i32, and return. |
| 1918 | EmitStmt(FinallyBlock); |
| 1919 | |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1920 | FinishFunction(FinallyBlock->getEndLoc()); |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1921 | |
| 1922 | return CurFn; |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1925 | void CodeGenFunction::EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF, |
| 1926 | llvm::Value *ParentFP, |
Reid Kleckner | 39329d57b | 2015-12-16 00:26:37 +0000 | [diff] [blame] | 1927 | llvm::Value *EntryFP) { |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1928 | // Get the pointer to the EXCEPTION_POINTERS struct. This is returned by the |
| 1929 | // __exception_info intrinsic. |
| 1930 | if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) { |
| 1931 | // On Win64, the info is passed as the first parameter to the filter. |
Duncan P. N. Exon Smith | 9f5260a | 2015-11-06 23:00:41 +0000 | [diff] [blame] | 1932 | SEHInfo = &*CurFn->arg_begin(); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1933 | SEHCodeSlotStack.push_back( |
| 1934 | CreateMemTemp(getContext().IntTy, "__exception_code")); |
| 1935 | } else { |
| 1936 | // On Win32, the EBP on entry to the filter points to the end of an |
| 1937 | // exception registration object. It contains 6 32-bit fields, and the info |
| 1938 | // pointer is stored in the second field. So, GEP 20 bytes backwards and |
| 1939 | // load the pointer. |
Reid Kleckner | 39329d57b | 2015-12-16 00:26:37 +0000 | [diff] [blame] | 1940 | SEHInfo = Builder.CreateConstInBoundsGEP1_32(Int8Ty, EntryFP, -20); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1941 | SEHInfo = Builder.CreateBitCast(SEHInfo, Int8PtrTy->getPointerTo()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1942 | SEHInfo = Builder.CreateAlignedLoad(Int8PtrTy, SEHInfo, getPointerAlign()); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1943 | SEHCodeSlotStack.push_back(recoverAddrOfEscapedLocal( |
| 1944 | ParentCGF, ParentCGF.SEHCodeSlotStack.back(), ParentFP)); |
| 1945 | } |
| 1946 | |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1947 | // Save the exception code in the exception slot to unify exception access in |
| 1948 | // the filter function and the landing pad. |
| 1949 | // struct EXCEPTION_POINTERS { |
| 1950 | // EXCEPTION_RECORD *ExceptionRecord; |
| 1951 | // CONTEXT *ContextRecord; |
| 1952 | // }; |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1953 | // int exceptioncode = exception_pointers->ExceptionRecord->ExceptionCode; |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1954 | llvm::Type *RecordTy = CGM.Int32Ty->getPointerTo(); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 1955 | llvm::Type *PtrsTy = llvm::StructType::get(RecordTy, CGM.VoidPtrTy); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1956 | llvm::Value *Ptrs = Builder.CreateBitCast(SEHInfo, PtrsTy->getPointerTo()); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 1957 | llvm::Value *Rec = Builder.CreateStructGEP(PtrsTy, Ptrs, 0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1958 | Rec = Builder.CreateAlignedLoad(Rec, getPointerAlign()); |
| 1959 | llvm::Value *Code = Builder.CreateAlignedLoad(Rec, getIntAlign()); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1960 | assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except"); |
| 1961 | Builder.CreateStore(Code, SEHCodeSlotStack.back()); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | llvm::Value *CodeGenFunction::EmitSEHExceptionInfo() { |
| 1965 | // Sema should diagnose calling this builtin outside of a filter context, but |
| 1966 | // don't crash if we screw up. |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1967 | if (!SEHInfo) |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1968 | return llvm::UndefValue::get(Int8PtrTy); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1969 | assert(SEHInfo->getType() == Int8PtrTy); |
| 1970 | return SEHInfo; |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | llvm::Value *CodeGenFunction::EmitSEHExceptionCode() { |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1974 | assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1975 | return Builder.CreateLoad(SEHCodeSlotStack.back()); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Reid Kleckner | aca01db | 2015-02-04 22:37:07 +0000 | [diff] [blame] | 1978 | llvm::Value *CodeGenFunction::EmitSEHAbnormalTermination() { |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1979 | // Abnormal termination is just the first parameter to the outlined finally |
| 1980 | // helper. |
| 1981 | auto AI = CurFn->arg_begin(); |
| 1982 | return Builder.CreateZExt(&*AI, Int32Ty); |
Reid Kleckner | aca01db | 2015-02-04 22:37:07 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1985 | void CodeGenFunction::pushSEHCleanup(CleanupKind Kind, |
| 1986 | llvm::Function *FinallyFunc) { |
| 1987 | EHStack.pushCleanup<PerformSEHFinally>(Kind, FinallyFunc); |
| 1988 | } |
| 1989 | |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1990 | void CodeGenFunction::EnterSEHTryStmt(const SEHTryStmt &S) { |
| 1991 | CodeGenFunction HelperCGF(CGM, /*suppressNewContext=*/true); |
| 1992 | if (const SEHFinallyStmt *Finally = S.getFinallyHandler()) { |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1993 | // Outline the finally block. |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 1994 | llvm::Function *FinallyFunc = |
| 1995 | HelperCGF.GenerateSEHFinallyFunction(*this, *Finally); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 1996 | |
| 1997 | // Push a cleanup for __finally blocks. |
Reid Kleckner | 5539152 | 2015-10-08 21:14:56 +0000 | [diff] [blame] | 1998 | EHStack.pushCleanup<PerformSEHFinally>(NormalAndEHCleanup, FinallyFunc); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 1999 | return; |
| 2000 | } |
| 2001 | |
| 2002 | // Otherwise, we must have an __except block. |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 2003 | const SEHExceptStmt *Except = S.getExceptHandler(); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 2004 | assert(Except); |
| 2005 | EHCatchScope *CatchScope = EHStack.pushCatch(1); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 2006 | SEHCodeSlotStack.push_back( |
| 2007 | CreateMemTemp(getContext().IntTy, "__exception_code")); |
Reid Kleckner | 2a2e156 | 2015-01-22 02:25:56 +0000 | [diff] [blame] | 2008 | |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 2009 | // If the filter is known to evaluate to 1, then we can use the clause |
| 2010 | // "catch i8* null". We can't do this on x86 because the filter has to save |
| 2011 | // the exception code. |
Reid Kleckner | 2a2e156 | 2015-01-22 02:25:56 +0000 | [diff] [blame] | 2012 | llvm::Constant *C = |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 2013 | ConstantEmitter(*this).tryEmitAbstract(Except->getFilterExpr(), |
| 2014 | getContext().IntTy); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 2015 | if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 && C && |
| 2016 | C->isOneValue()) { |
Reid Kleckner | 2a2e156 | 2015-01-22 02:25:56 +0000 | [diff] [blame] | 2017 | CatchScope->setCatchAllHandler(0, createBasicBlock("__except")); |
| 2018 | return; |
| 2019 | } |
| 2020 | |
| 2021 | // In general, we have to emit an outlined filter function. Use the function |
| 2022 | // in place of the RTTI typeinfo global that C++ EH uses. |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 2023 | llvm::Function *FilterFunc = |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 2024 | HelperCGF.GenerateSEHFilterFunction(*this, *Except); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 2025 | llvm::Constant *OpaqueFunc = |
| 2026 | llvm::ConstantExpr::getBitCast(FilterFunc, Int8PtrTy); |
Reid Kleckner | 8be1847 | 2015-09-16 21:06:09 +0000 | [diff] [blame] | 2027 | CatchScope->setHandler(0, OpaqueFunc, createBasicBlock("__except.ret")); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 2030 | void CodeGenFunction::ExitSEHTryStmt(const SEHTryStmt &S) { |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 2031 | // Just pop the cleanup if it's a __finally block. |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 2032 | if (S.getFinallyHandler()) { |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 2033 | PopCleanupBlock(); |
| 2034 | return; |
| 2035 | } |
| 2036 | |
| 2037 | // Otherwise, we must have an __except block. |
Reid Kleckner | aca01db | 2015-02-04 22:37:07 +0000 | [diff] [blame] | 2038 | const SEHExceptStmt *Except = S.getExceptHandler(); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 2039 | assert(Except && "__try must have __finally xor __except"); |
| 2040 | EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin()); |
| 2041 | |
| 2042 | // Don't emit the __except block if the __try block lacked invokes. |
| 2043 | // TODO: Model unwind edges from instructions, either with iload / istore or |
| 2044 | // a try body function. |
| 2045 | if (!CatchScope.hasEHBranches()) { |
| 2046 | CatchScope.clearHandlerBlocks(); |
| 2047 | EHStack.popCatch(); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 2048 | SEHCodeSlotStack.pop_back(); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 2049 | return; |
| 2050 | } |
| 2051 | |
| 2052 | // The fall-through block. |
| 2053 | llvm::BasicBlock *ContBB = createBasicBlock("__try.cont"); |
| 2054 | |
| 2055 | // We just emitted the body of the __try; jump to the continue block. |
| 2056 | if (HaveInsertPoint()) |
| 2057 | Builder.CreateBr(ContBB); |
| 2058 | |
| 2059 | // Check if our filter function returned true. |
| 2060 | emitCatchDispatchBlock(*this, CatchScope); |
| 2061 | |
| 2062 | // Grab the block before we pop the handler. |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 2063 | llvm::BasicBlock *CatchPadBB = CatchScope.getHandler(0).Block; |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 2064 | EHStack.popCatch(); |
| 2065 | |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 2066 | EmitBlockAfterUses(CatchPadBB); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 2067 | |
Reid Kleckner | 129552b | 2015-10-08 01:13:52 +0000 | [diff] [blame] | 2068 | // __except blocks don't get outlined into funclets, so immediately do a |
| 2069 | // catchret. |
Reid Kleckner | 129552b | 2015-10-08 01:13:52 +0000 | [diff] [blame] | 2070 | llvm::CatchPadInst *CPI = |
| 2071 | cast<llvm::CatchPadInst>(CatchPadBB->getFirstNonPHI()); |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 2072 | llvm::BasicBlock *ExceptBB = createBasicBlock("__except"); |
Reid Kleckner | 129552b | 2015-10-08 01:13:52 +0000 | [diff] [blame] | 2073 | Builder.CreateCatchRet(CPI, ExceptBB); |
| 2074 | EmitBlock(ExceptBB); |
| 2075 | |
| 2076 | // On Win64, the exception code is returned in EAX. Copy it into the slot. |
| 2077 | if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) { |
| 2078 | llvm::Function *SEHCodeIntrin = |
| 2079 | CGM.getIntrinsic(llvm::Intrinsic::eh_exceptioncode); |
| 2080 | llvm::Value *Code = Builder.CreateCall(SEHCodeIntrin, {CPI}); |
| 2081 | Builder.CreateStore(Code, SEHCodeSlotStack.back()); |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 2084 | // Emit the __except body. |
| 2085 | EmitStmt(Except->getBlock()); |
| 2086 | |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 2087 | // End the lifetime of the exception code. |
| 2088 | SEHCodeSlotStack.pop_back(); |
| 2089 | |
Reid Kleckner | 3a417c3 | 2015-01-30 22:16:45 +0000 | [diff] [blame] | 2090 | if (HaveInsertPoint()) |
| 2091 | Builder.CreateBr(ContBB); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 2092 | |
| 2093 | EmitBlock(ContBB); |
Reid Kleckner | 543a16c | 2013-09-16 21:46:30 +0000 | [diff] [blame] | 2094 | } |
Nico Weber | 9b98207 | 2014-07-07 00:12:30 +0000 | [diff] [blame] | 2095 | |
| 2096 | void CodeGenFunction::EmitSEHLeaveStmt(const SEHLeaveStmt &S) { |
Nico Weber | 5779f84 | 2015-02-12 23:16:11 +0000 | [diff] [blame] | 2097 | // If this code is reachable then emit a stop point (if generating |
| 2098 | // debug info). We have to do this ourselves because we are on the |
| 2099 | // "simple" statement path. |
| 2100 | if (HaveInsertPoint()) |
| 2101 | EmitStopPoint(&S); |
| 2102 | |
Reid Kleckner | ebaf28d | 2015-04-14 20:59:00 +0000 | [diff] [blame] | 2103 | // This must be a __leave from a __finally block, which we warn on and is UB. |
| 2104 | // Just emit unreachable. |
| 2105 | if (!isSEHTryScope()) { |
| 2106 | Builder.CreateUnreachable(); |
| 2107 | Builder.ClearInsertionPoint(); |
| 2108 | return; |
| 2109 | } |
| 2110 | |
Nico Weber | 5779f84 | 2015-02-12 23:16:11 +0000 | [diff] [blame] | 2111 | EmitBranchThroughCleanup(*SEHTryEpilogueStack.back()); |
Nico Weber | 9b98207 | 2014-07-07 00:12:30 +0000 | [diff] [blame] | 2112 | } |