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