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