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