Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1 | //===--- CGException.cpp - Emit LLVM Code for C++ exceptions ----*- C++ -*-===// |
Anders Carlsson | 756b5c4 | 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" |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
John McCall | 36f893c | 2011-01-28 11:13:47 +0000 | [diff] [blame] | 16 | #include "CGCleanup.h" |
Benjamin Kramer | af2771b | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 17 | #include "CGObjCRuntime.h" |
John McCall | 204b075 | 2010-07-20 22:17:55 +0000 | [diff] [blame] | 18 | #include "TargetInfo.h" |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 19 | #include "clang/AST/Mangle.h" |
Benjamin Kramer | af2771b | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtCXX.h" |
Chandler Carruth | b1ba0ef | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtObjC.h" |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 22 | #include "clang/AST/StmtVisitor.h" |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 23 | #include "clang/Basic/TargetBuiltins.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 24 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Intrinsics.h" |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 26 | #include "llvm/IR/IntrinsicInst.h" |
| 27 | #include "llvm/Support/SaveAndRestore.h" |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 28 | |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | using namespace CodeGen; |
| 31 | |
John McCall | 629df01 | 2013-02-12 03:51:38 +0000 | [diff] [blame] | 32 | static llvm::Constant *getFreeExceptionFn(CodeGenModule &CGM) { |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 33 | // void __cxa_free_exception(void *thrown_exception); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 35 | llvm::FunctionType *FTy = |
John McCall | 629df01 | 2013-02-12 03:51:38 +0000 | [diff] [blame] | 36 | llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 37 | |
John McCall | 629df01 | 2013-02-12 03:51:38 +0000 | [diff] [blame] | 38 | return CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception"); |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 39 | } |
| 40 | |
John McCall | 629df01 | 2013-02-12 03:51:38 +0000 | [diff] [blame] | 41 | static llvm::Constant *getUnexpectedFn(CodeGenModule &CGM) { |
Richard Smith | 14b0e4b | 2013-06-20 23:03:35 +0000 | [diff] [blame] | 42 | // void __cxa_call_unexpected(void *thrown_exception); |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 44 | llvm::FunctionType *FTy = |
John McCall | 629df01 | 2013-02-12 03:51:38 +0000 | [diff] [blame] | 45 | llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 46 | |
John McCall | 629df01 | 2013-02-12 03:51:38 +0000 | [diff] [blame] | 47 | return CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected"); |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 50 | llvm::Constant *CodeGenModule::getTerminateFn() { |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 51 | // void __terminate(); |
| 52 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 53 | llvm::FunctionType *FTy = |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 54 | llvm::FunctionType::get(VoidTy, /*IsVarArgs=*/false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 56 | StringRef name; |
John McCall | 256a76e | 2011-07-06 01:22:26 +0000 | [diff] [blame] | 57 | |
| 58 | // In C++, use std::terminate(). |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 59 | if (getLangOpts().CPlusPlus && |
| 60 | getTarget().getCXXABI().isItaniumFamily()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 61 | name = "_ZSt9terminatev"; |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 62 | } else if (getLangOpts().CPlusPlus && |
| 63 | getTarget().getCXXABI().isMicrosoft()) { |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 64 | if (getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2015)) |
| 65 | name = "__std_terminate"; |
| 66 | else |
| 67 | name = "\01?terminate@@YAXXZ"; |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 68 | } else if (getLangOpts().ObjC1 && |
| 69 | getLangOpts().ObjCRuntime.hasTerminate()) |
John McCall | 256a76e | 2011-07-06 01:22:26 +0000 | [diff] [blame] | 70 | name = "objc_terminate"; |
| 71 | else |
| 72 | name = "abort"; |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 73 | return CreateRuntimeFunction(FTy, name); |
David Chisnall | 79a9ad8 | 2010-05-17 13:49:20 +0000 | [diff] [blame] | 74 | } |
| 75 | |
John McCall | 629df01 | 2013-02-12 03:51:38 +0000 | [diff] [blame] | 76 | static llvm::Constant *getCatchallRethrowFn(CodeGenModule &CGM, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 77 | StringRef Name) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 78 | llvm::FunctionType *FTy = |
John McCall | 629df01 | 2013-02-12 03:51:38 +0000 | [diff] [blame] | 79 | llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false); |
John McCall | 8262b6a | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 80 | |
John McCall | 629df01 | 2013-02-12 03:51:38 +0000 | [diff] [blame] | 81 | return CGM.CreateRuntimeFunction(FTy, Name); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 84 | const EHPersonality EHPersonality::GNU_C = { "__gcc_personality_v0", nullptr }; |
Benjamin Kramer | af2771b | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 85 | const EHPersonality |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 86 | EHPersonality::GNU_C_SJLJ = { "__gcc_personality_sj0", nullptr }; |
| 87 | const EHPersonality |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 88 | EHPersonality::GNU_C_SEH = { "__gcc_personality_seh0", nullptr }; |
| 89 | const EHPersonality |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [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 | af2771b | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 95 | const EHPersonality |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 96 | EHPersonality::GNU_CPlusPlus_SEH = { "__gxx_personality_seh0", nullptr }; |
| 97 | const EHPersonality |
Benjamin Kramer | af2771b | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 98 | EHPersonality::GNU_ObjC = {"__gnu_objc_personality_v0", "objc_exception_throw"}; |
| 99 | const EHPersonality |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 100 | EHPersonality::GNU_ObjCXX = { "__gnustep_objcxx_personality_v0", nullptr }; |
David Chisnall | 65bd4ac | 2013-01-11 15:33:01 +0000 | [diff] [blame] | 101 | const EHPersonality |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 102 | EHPersonality::GNUstep_ObjC = { "__gnustep_objc_personality_v0", nullptr }; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [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 }; |
| 107 | const EHPersonality |
| 108 | EHPersonality::MSVC_CxxFrameHandler3 = { "__CxxFrameHandler3", nullptr }; |
John McCall | 8262b6a | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 109 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [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 | 4468078 | 2010-11-07 02:35:25 +0000 | [diff] [blame] | 118 | if (L.SjLjExceptions) |
| 119 | return EHPersonality::GNU_C_SJLJ; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 120 | else if (useLibGCCSEHPersonality(T)) |
| 121 | return EHPersonality::GNU_C_SEH; |
John McCall | 8262b6a | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 122 | return EHPersonality::GNU_C; |
| 123 | } |
| 124 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 125 | static const EHPersonality &getObjCPersonality(const llvm::Triple &T, |
| 126 | const LangOptions &L) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 127 | switch (L.ObjCRuntime.getKind()) { |
| 128 | case ObjCRuntime::FragileMacOSX: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 129 | return getCPersonality(T, L); |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 130 | case ObjCRuntime::MacOSX: |
| 131 | case ObjCRuntime::iOS: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 132 | case ObjCRuntime::WatchOS: |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 133 | return EHPersonality::NeXT_ObjC; |
David Chisnall | 11d3f4c | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 134 | case ObjCRuntime::GNUstep: |
David Chisnall | 65bd4ac | 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 | 11d3f4c | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 138 | case ObjCRuntime::GCC: |
John McCall | f7226fb | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 139 | case ObjCRuntime::ObjFW: |
John McCall | 8262b6a | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 140 | return EHPersonality::GNU_ObjC; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 141 | } |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 142 | llvm_unreachable("bad runtime kind"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 145 | static const EHPersonality &getCXXPersonality(const llvm::Triple &T, |
| 146 | const LangOptions &L) { |
John McCall | 8262b6a | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 147 | if (L.SjLjExceptions) |
| 148 | return EHPersonality::GNU_CPlusPlus_SJLJ; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 149 | else if (useLibGCCSEHPersonality(T)) |
| 150 | return EHPersonality::GNU_CPlusPlus_SEH; |
| 151 | return EHPersonality::GNU_CPlusPlus; |
John McCall | f1549f6 | 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. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 156 | static const EHPersonality &getObjCXXPersonality(const llvm::Triple &T, |
| 157 | const LangOptions &L) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 158 | switch (L.ObjCRuntime.getKind()) { |
John McCall | f1549f6 | 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 | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 162 | case ObjCRuntime::MacOSX: |
| 163 | case ObjCRuntime::iOS: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 164 | case ObjCRuntime::WatchOS: |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 165 | return EHPersonality::NeXT_ObjC; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 166 | |
John McCall | 260611a | 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: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 170 | return getCXXPersonality(T, L); |
David Chisnall | 79a9ad8 | 2010-05-17 13:49:20 +0000 | [diff] [blame] | 171 | |
David Chisnall | 11d3f4c | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 172 | // The GCC runtime's personality function inherently doesn't support |
John McCall | 8262b6a | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 173 | // mixed EH. Use the C++ personality just to avoid returning null. |
David Chisnall | 11d3f4c | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 174 | case ObjCRuntime::GCC: |
John McCall | f7226fb | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 175 | case ObjCRuntime::ObjFW: // XXX: this will change soon |
David Chisnall | 11d3f4c | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 176 | return EHPersonality::GNU_ObjC; |
| 177 | case ObjCRuntime::GNUstep: |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 178 | return EHPersonality::GNU_ObjCXX; |
| 179 | } |
| 180 | llvm_unreachable("bad runtime kind"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 183 | static const EHPersonality &getSEHPersonalityMSVC(const llvm::Triple &T) { |
| 184 | if (T.getArch() == llvm::Triple::x86) |
| 185 | return EHPersonality::MSVC_except_handler; |
| 186 | return EHPersonality::MSVC_C_specific_handler; |
| 187 | } |
| 188 | |
| 189 | const EHPersonality &EHPersonality::get(CodeGenModule &CGM, |
| 190 | const FunctionDecl *FD) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 191 | const llvm::Triple &T = CGM.getTarget().getTriple(); |
| 192 | const LangOptions &L = CGM.getLangOpts(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 193 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 194 | // Functions using SEH get an SEH personality. |
| 195 | if (FD && FD->usesSEHTry()) |
| 196 | return getSEHPersonalityMSVC(T); |
| 197 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [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) { |
| 202 | if (L.SjLjExceptions) |
| 203 | return EHPersonality::GNU_CPlusPlus_SJLJ; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 204 | else |
| 205 | return EHPersonality::MSVC_CxxFrameHandler3; |
| 206 | } |
| 207 | |
John McCall | 8262b6a | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 208 | if (L.CPlusPlus && L.ObjC1) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 209 | return getObjCXXPersonality(T, L); |
John McCall | 8262b6a | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 210 | else if (L.CPlusPlus) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 211 | return getCXXPersonality(T, L); |
John McCall | 8262b6a | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 212 | else if (L.ObjC1) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 213 | return getObjCPersonality(T, L); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 214 | else |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 215 | return getCPersonality(T, L); |
John McCall | 8262b6a | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 216 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 217 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [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 | b259383 | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 222 | static llvm::Constant *getPersonalityFn(CodeGenModule &CGM, |
John McCall | 8262b6a | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 223 | const EHPersonality &Personality) { |
John McCall | 8262b6a | 2010-07-17 00:43:08 +0000 | [diff] [blame] | 224 | llvm::Constant *Fn = |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 225 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, true), |
Benjamin Kramer | af2771b | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 226 | Personality.PersonalityFn); |
John McCall | b259383 | 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 | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 233 | return llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
John McCall | b259383 | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [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 | b259383 | 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) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 269 | for (llvm::User *U : Fn->users()) { |
John McCall | b259383 | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 270 | // Conditionally white-list bitcasts. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 271 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(U)) { |
John McCall | b259383 | 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 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [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 | b259383 | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 281 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [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 | b259383 | 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 | b259383 | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 297 | // If we're not in ObjC++ -fexceptions, there's nothing to do. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 298 | if (!LangOpts.CPlusPlus || !LangOpts.ObjC1 || !LangOpts.Exceptions) |
John McCall | b259383 | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 299 | return; |
| 300 | |
John McCall | 70cd619 | 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 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 306 | const EHPersonality &ObjCXX = EHPersonality::get(*this, /*FD=*/nullptr); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 307 | const EHPersonality &CXX = |
| 308 | getCXXPersonality(getTarget().getTriple(), LangOpts); |
Benjamin Kramer | af2771b | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 309 | if (&ObjCXX == &CXX) |
John McCall | b259383 | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 310 | return; |
| 311 | |
Benjamin Kramer | af2771b | 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 | b259383 | 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 | f1549f6 | 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 | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 338 | return llvm::ConstantPointerNull::get(CGF.Int8PtrTy); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 339 | } |
| 340 | |
John McCall | 09faeab | 2010-07-13 21:17:51 +0000 | [diff] [blame] | 341 | namespace { |
| 342 | /// A cleanup to free the exception object if its initialization |
| 343 | /// throws. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 344 | struct FreeException final : EHScopeStack::Cleanup { |
John McCall | c4a1a84 | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 345 | llvm::Value *exn; |
| 346 | FreeException(llvm::Value *exn) : exn(exn) {} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 347 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 348 | CGF.EmitNounwindRuntimeCall(getFreeExceptionFn(CGF.CGM), exn); |
John McCall | 09faeab | 2010-07-13 21:17:51 +0000 | [diff] [blame] | 349 | } |
| 350 | }; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 351 | } // end anonymous namespace |
John McCall | 09faeab | 2010-07-13 21:17:51 +0000 | [diff] [blame] | 352 | |
John McCall | ac41816 | 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. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 357 | void CodeGenFunction::EmitAnyExprToExn(const Expr *e, Address addr) { |
John McCall | f1549f6 | 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. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 360 | pushFullExprCleanup<FreeException>(EHCleanup, addr.getPointer()); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 361 | EHScopeStack::stable_iterator cleanup = EHStack.stable_begin(); |
John McCall | ac41816 | 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. |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 365 | llvm::Type *ty = ConvertTypeForMem(e->getType())->getPointerTo(); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 366 | Address typedAddr = Builder.CreateBitCast(addr, ty); |
John McCall | ac41816 | 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. |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 375 | EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(), |
| 376 | /*IsInit*/ true); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 377 | |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 378 | // Deactivate the cleanup block. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 379 | DeactivateCleanupBlock(cleanup, |
| 380 | cast<llvm::Instruction>(typedAddr.getPointer())); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 383 | Address CodeGenFunction::getExceptionSlot() { |
John McCall | 93c332a | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 384 | if (!ExceptionSlot) |
| 385 | ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot"); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 386 | return Address(ExceptionSlot, getPointerAlign()); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 389 | Address CodeGenFunction::getEHSelectorSlot() { |
John McCall | 93c332a | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 390 | if (!EHSelectorSlot) |
| 391 | EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot"); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 392 | return Address(EHSelectorSlot, CharUnits::fromQuantity(4)); |
John McCall | 93c332a | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Bill Wendling | ae27059 | 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 | 4c71b8c | 2013-05-07 21:53:22 +0000 | [diff] [blame] | 403 | void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E, |
| 404 | bool KeepInsertionPoint) { |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [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 | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 413 | } |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 414 | } else { |
| 415 | CGM.getCXXABI().emitRethrow(*this, /*isNoReturn=*/true); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 416 | } |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 417 | |
John McCall | cd5b22e | 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 | 4c71b8c | 2013-05-07 21:53:22 +0000 | [diff] [blame] | 420 | if (KeepInsertionPoint) |
| 421 | EmitBlock(createBasicBlock("throw.cont")); |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 422 | } |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 423 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 424 | void CodeGenFunction::EmitStartEHSpec(const Decl *D) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 425 | if (!CGM.getLangOpts().CXXExceptions) |
Anders Carlsson | a994ee4 | 2010-02-06 23:59:05 +0000 | [diff] [blame] | 426 | return; |
| 427 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 428 | const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 429 | if (!FD) { |
| 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 | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 435 | return; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 436 | } |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 437 | const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 438 | if (!Proto) |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 439 | return; |
| 440 | |
Sebastian Redl | a968e97 | 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) { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [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 | a968e97 | 2011-03-15 18:42:48 +0000 | [diff] [blame] | 452 | unsigned NumExceptions = Proto->getNumExceptions(); |
| 453 | EHFilterScope *Filter = EHStack.pushFilter(NumExceptions); |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 454 | |
Sebastian Redl | a968e97 | 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 | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 462 | } |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 463 | } |
| 464 | |
John McCall | 777d6e5 | 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 | 777d6e5 | 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 | ae27059 | 2011-09-15 18:57:19 +0000 | [diff] [blame] | 481 | llvm::Value *selector = CGF.getSelectorFromSlot(); |
John McCall | 777d6e5 | 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 = |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 486 | CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails"); |
| 487 | CGF.Builder.CreateCondBr(failsFilter, unexpectedBB, |
| 488 | CGF.getEHResumeBlock(false)); |
John McCall | 777d6e5 | 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 | ae27059 | 2011-09-15 18:57:19 +0000 | [diff] [blame] | 497 | llvm::Value *exn = CGF.getExceptionFromSlot(); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 498 | CGF.EmitRuntimeCall(getUnexpectedFn(CGF.CGM), exn) |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 499 | ->setDoesNotReturn(); |
| 500 | CGF.Builder.CreateUnreachable(); |
| 501 | } |
| 502 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 503 | void CodeGenFunction::EmitEndEHSpec(const Decl *D) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 504 | if (!CGM.getLangOpts().CXXExceptions) |
Anders Carlsson | a994ee4 | 2010-02-06 23:59:05 +0000 | [diff] [blame] | 505 | return; |
| 506 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 507 | const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 508 | if (!FD) { |
| 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 | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 514 | return; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 515 | } |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 516 | const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 517 | if (!Proto) |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 518 | return; |
| 519 | |
Sebastian Redl | a968e97 | 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) { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [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 | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 530 | EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin()); |
| 531 | emitFilterDispatchBlock(*this, filterScope); |
Sebastian Redl | a968e97 | 2011-03-15 18:42:48 +0000 | [diff] [blame] | 532 | EHStack.popFilter(); |
| 533 | } |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 536 | void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 537 | EnterCXXTryStmt(S); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 538 | EmitStmt(S.getTryBlock()); |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 539 | ExitCXXTryStmt(S); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 540 | } |
| 541 | |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 542 | void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 543 | unsigned NumHandlers = S.getNumHandlers(); |
| 544 | EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 545 | |
John McCall | f1549f6 | 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 | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 548 | |
John McCall | f1549f6 | 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 |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 557 | Qualifiers CaughtTypeQuals; |
| 558 | QualType CaughtType = CGM.getContext().getUnqualifiedArrayType( |
| 559 | C->getCaughtType().getNonReferenceType(), CaughtTypeQuals); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 560 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 561 | CatchTypeInfo TypeInfo{nullptr, 0}; |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 562 | if (CaughtType->isObjCObjectPointerType()) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 563 | TypeInfo.RTTI = CGM.getObjCRuntime().GetEHType(CaughtType); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 564 | else |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 565 | TypeInfo = CGM.getCXXABI().getAddrOfCXXCatchHandlerType( |
| 566 | CaughtType, C->getCaughtType()); |
John McCall | f1549f6 | 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. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 570 | CatchScope->setHandler(I, CGM.getCXXABI().getCatchAllTypeInfo(), Handler); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 573 | } |
| 574 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 575 | llvm::BasicBlock * |
| 576 | CodeGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator si) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 577 | if (EHPersonality::get(*this).usesFuncletPads()) |
| 578 | return getMSVCDispatchBlock(si); |
| 579 | |
John McCall | 777d6e5 | 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 | c686004 | 2012-11-07 16:50:40 +0000 | [diff] [blame] | 583 | return getEHResumeBlock(true); |
John McCall | 777d6e5 | 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; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 616 | |
| 617 | case EHScope::PadEnd: |
| 618 | llvm_unreachable("PadEnd unnecessary for Itanium!"); |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 619 | } |
| 620 | scope.setCachedEHDispatchBlock(dispatchBlock); |
| 621 | } |
| 622 | return dispatchBlock; |
| 623 | } |
| 624 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [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(); |
| 643 | CGBuilderTy Builder(*this, DispatchBlock); |
| 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 | |
| 661 | case EHScope::PadEnd: |
| 662 | llvm_unreachable("PadEnd dispatch block missing!"); |
| 663 | } |
| 664 | EHS.setCachedEHDispatchBlock(DispatchBlock); |
| 665 | return DispatchBlock; |
| 666 | } |
| 667 | |
John McCall | f1549f6 | 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 | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 672 | switch (S.getKind()) { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 673 | case EHScope::Cleanup: |
| 674 | return !cast<EHCleanupScope>(S).isEHCleanup(); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 675 | case EHScope::Filter: |
| 676 | case EHScope::Catch: |
| 677 | case EHScope::Terminate: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 678 | case EHScope::PadEnd: |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 679 | return false; |
| 680 | } |
| 681 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 682 | llvm_unreachable("Invalid EHScope Kind!"); |
John McCall | f1549f6 | 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 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 689 | // If exceptions are disabled and SEH is not in use, then there is no invoke |
| 690 | // destination. SEH "works" even if exceptions are off. In practice, this |
| 691 | // means that C++ destructors and other EH cleanups don't run, which is |
| 692 | // consistent with MSVC's behavior. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 693 | const LangOptions &LO = CGM.getLangOpts(); |
| 694 | if (!LO.Exceptions) { |
| 695 | if (!LO.Borland && !LO.MicrosoftExt) |
| 696 | return nullptr; |
| 697 | if (!currentFunctionUsesSEHTry()) |
| 698 | return nullptr; |
| 699 | } |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 700 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 701 | // Check the innermost scope for a cached landing pad. If this is |
| 702 | // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad. |
| 703 | llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad(); |
| 704 | if (LP) return LP; |
| 705 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 706 | const EHPersonality &Personality = EHPersonality::get(*this); |
| 707 | |
| 708 | if (!CurFn->hasPersonalityFn()) |
| 709 | CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality)); |
| 710 | |
| 711 | if (Personality.usesFuncletPads()) { |
| 712 | // We don't need separate landing pads in the funclet model. |
| 713 | LP = getEHDispatchBlock(EHStack.getInnermostEHScope()); |
| 714 | } else { |
| 715 | // Build the landing pad for this scope. |
| 716 | LP = EmitLandingPad(); |
| 717 | } |
| 718 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 719 | assert(LP); |
| 720 | |
| 721 | // Cache the landing pad on the innermost scope. If this is a |
| 722 | // non-EH scope, cache the landing pad on the enclosing scope, too. |
| 723 | for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) { |
| 724 | ir->setCachedLandingPad(LP); |
| 725 | if (!isNonEHScope(*ir)) break; |
| 726 | } |
| 727 | |
| 728 | return LP; |
| 729 | } |
| 730 | |
| 731 | llvm::BasicBlock *CodeGenFunction::EmitLandingPad() { |
| 732 | assert(EHStack.requiresLandingPad()); |
| 733 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 734 | EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope()); |
| 735 | switch (innermostEHScope.getKind()) { |
| 736 | case EHScope::Terminate: |
| 737 | return getTerminateLandingPad(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 738 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 739 | case EHScope::PadEnd: |
| 740 | llvm_unreachable("PadEnd unnecessary for Itanium!"); |
| 741 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 742 | case EHScope::Catch: |
| 743 | case EHScope::Cleanup: |
| 744 | case EHScope::Filter: |
| 745 | if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad()) |
| 746 | return lpad; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | // Save the current IR generation state. |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 750 | CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 751 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, CurEHLocation); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 752 | |
| 753 | // Create and configure the landing pad. |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 754 | llvm::BasicBlock *lpad = createBasicBlock("lpad"); |
| 755 | EmitBlock(lpad); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 756 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 757 | llvm::LandingPadInst *LPadInst = Builder.CreateLandingPad( |
| 758 | llvm::StructType::get(Int8PtrTy, Int32Ty, nullptr), 0); |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 759 | |
| 760 | llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0); |
| 761 | Builder.CreateStore(LPadExn, getExceptionSlot()); |
| 762 | llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1); |
| 763 | Builder.CreateStore(LPadSel, getEHSelectorSlot()); |
| 764 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 765 | // Save the exception pointer. It's safe to use a single exception |
| 766 | // pointer per function because EH cleanups can never have nested |
| 767 | // try/catches. |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 768 | // Build the landingpad instruction. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 769 | |
| 770 | // Accumulate all the handlers in scope. |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 771 | bool hasCatchAll = false; |
| 772 | bool hasCleanup = false; |
| 773 | bool hasFilter = false; |
| 774 | SmallVector<llvm::Value*, 4> filterTypes; |
| 775 | llvm::SmallPtrSet<llvm::Value*, 4> catchTypes; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 776 | for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end(); I != E; |
| 777 | ++I) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 778 | |
| 779 | switch (I->getKind()) { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 780 | case EHScope::Cleanup: |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 781 | // If we have a cleanup, remember that. |
| 782 | hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup()); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 783 | continue; |
| 784 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 785 | case EHScope::Filter: { |
| 786 | assert(I.next() == EHStack.end() && "EH filter is not end of EH stack"); |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 787 | assert(!hasCatchAll && "EH filter reached after catch-all"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 788 | |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 789 | // Filter scopes get added to the landingpad in weird ways. |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 790 | EHFilterScope &filter = cast<EHFilterScope>(*I); |
| 791 | hasFilter = true; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 792 | |
Bill Wendling | 8990daf | 2011-09-22 20:32:54 +0000 | [diff] [blame] | 793 | // Add all the filter values. |
| 794 | for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i) |
| 795 | filterTypes.push_back(filter.getFilter(i)); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 796 | goto done; |
| 797 | } |
| 798 | |
| 799 | case EHScope::Terminate: |
| 800 | // Terminate scopes are basically catch-alls. |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 801 | assert(!hasCatchAll); |
| 802 | hasCatchAll = true; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 803 | goto done; |
| 804 | |
| 805 | case EHScope::Catch: |
| 806 | break; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 807 | |
| 808 | case EHScope::PadEnd: |
| 809 | llvm_unreachable("PadEnd unnecessary for Itanium!"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 810 | } |
| 811 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 812 | EHCatchScope &catchScope = cast<EHCatchScope>(*I); |
| 813 | for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) { |
| 814 | EHCatchScope::Handler handler = catchScope.getHandler(hi); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 815 | assert(handler.Type.Flags == 0 && |
| 816 | "landingpads do not support catch handler flags"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 817 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 818 | // If this is a catch-all, register that and abort. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 819 | if (!handler.Type.RTTI) { |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 820 | assert(!hasCatchAll); |
| 821 | hasCatchAll = true; |
| 822 | goto done; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | // Check whether we already have a handler for this type. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 826 | if (catchTypes.insert(handler.Type.RTTI).second) |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 827 | // If not, add it directly to the landingpad. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 828 | LPadInst->addClause(handler.Type.RTTI); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 829 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | done: |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 833 | // If we have a catch-all, add null to the landingpad. |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 834 | assert(!(hasCatchAll && hasFilter)); |
| 835 | if (hasCatchAll) { |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 836 | LPadInst->addClause(getCatchAllValue(*this)); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 837 | |
| 838 | // If we have an EH filter, we need to add those handlers in the |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 839 | // right place in the landingpad, which is to say, at the end. |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 840 | } else if (hasFilter) { |
Bill Wendling | 40ccacc | 2011-09-19 22:08:36 +0000 | [diff] [blame] | 841 | // Create a filter expression: a constant array indicating which filter |
| 842 | // types there are. The personality routine only lands here if the filter |
| 843 | // doesn't match. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 844 | SmallVector<llvm::Constant*, 8> Filters; |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 845 | llvm::ArrayType *AType = |
| 846 | llvm::ArrayType::get(!filterTypes.empty() ? |
| 847 | filterTypes[0]->getType() : Int8PtrTy, |
| 848 | filterTypes.size()); |
| 849 | |
| 850 | for (unsigned i = 0, e = filterTypes.size(); i != e; ++i) |
| 851 | Filters.push_back(cast<llvm::Constant>(filterTypes[i])); |
| 852 | llvm::Constant *FilterArray = llvm::ConstantArray::get(AType, Filters); |
| 853 | LPadInst->addClause(FilterArray); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 854 | |
| 855 | // Also check whether we need a cleanup. |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 856 | if (hasCleanup) |
| 857 | LPadInst->setCleanup(true); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 858 | |
| 859 | // Otherwise, signal that we at least have cleanups. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 860 | } else if (hasCleanup) { |
| 861 | LPadInst->setCleanup(true); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 864 | assert((LPadInst->getNumClauses() > 0 || LPadInst->isCleanup()) && |
| 865 | "landingpad instruction has no clauses!"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 866 | |
| 867 | // Tell the backend how to generate the landing pad. |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 868 | Builder.CreateBr(getEHDispatchBlock(EHStack.getInnermostEHScope())); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 869 | |
| 870 | // Restore the old IR generation state. |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 871 | Builder.restoreIP(savedIP); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 872 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 873 | return lpad; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 876 | static void emitCatchPadBlock(CodeGenFunction &CGF, EHCatchScope &CatchScope) { |
| 877 | llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock(); |
| 878 | assert(DispatchBlock); |
| 879 | |
| 880 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveIP(); |
| 881 | CGF.EmitBlockAfterUses(DispatchBlock); |
| 882 | |
| 883 | llvm::Value *ParentPad = CGF.CurrentFuncletPad; |
| 884 | if (!ParentPad) |
| 885 | ParentPad = llvm::ConstantTokenNone::get(CGF.getLLVMContext()); |
| 886 | llvm::BasicBlock *UnwindBB = |
| 887 | CGF.getEHDispatchBlock(CatchScope.getEnclosingEHScope()); |
| 888 | |
| 889 | unsigned NumHandlers = CatchScope.getNumHandlers(); |
| 890 | llvm::CatchSwitchInst *CatchSwitch = |
| 891 | CGF.Builder.CreateCatchSwitch(ParentPad, UnwindBB, NumHandlers); |
| 892 | |
| 893 | // Test against each of the exception types we claim to catch. |
| 894 | for (unsigned I = 0; I < NumHandlers; ++I) { |
| 895 | const EHCatchScope::Handler &Handler = CatchScope.getHandler(I); |
| 896 | |
| 897 | CatchTypeInfo TypeInfo = Handler.Type; |
| 898 | if (!TypeInfo.RTTI) |
| 899 | TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy); |
| 900 | |
| 901 | CGF.Builder.SetInsertPoint(Handler.Block); |
| 902 | |
| 903 | if (EHPersonality::get(CGF).isMSVCXXPersonality()) { |
| 904 | CGF.Builder.CreateCatchPad( |
| 905 | CatchSwitch, {TypeInfo.RTTI, CGF.Builder.getInt32(TypeInfo.Flags), |
| 906 | llvm::Constant::getNullValue(CGF.VoidPtrTy)}); |
| 907 | } else { |
| 908 | CGF.Builder.CreateCatchPad(CatchSwitch, {TypeInfo.RTTI}); |
| 909 | } |
| 910 | |
| 911 | CatchSwitch->addHandler(Handler.Block); |
| 912 | } |
| 913 | CGF.Builder.restoreIP(SavedIP); |
| 914 | } |
| 915 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 916 | /// Emit the structure of the dispatch block for the given catch scope. |
| 917 | /// It is an invariant that the dispatch block already exists. |
| 918 | static void emitCatchDispatchBlock(CodeGenFunction &CGF, |
| 919 | EHCatchScope &catchScope) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 920 | if (EHPersonality::get(CGF).usesFuncletPads()) |
| 921 | return emitCatchPadBlock(CGF, catchScope); |
| 922 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 923 | llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock(); |
| 924 | assert(dispatchBlock); |
| 925 | |
| 926 | // If there's only a single catch-all, getEHDispatchBlock returned |
| 927 | // that catch-all as the dispatch block. |
| 928 | if (catchScope.getNumHandlers() == 1 && |
| 929 | catchScope.getHandler(0).isCatchAll()) { |
| 930 | assert(dispatchBlock == catchScope.getHandler(0).Block); |
| 931 | return; |
| 932 | } |
| 933 | |
| 934 | CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP(); |
| 935 | CGF.EmitBlockAfterUses(dispatchBlock); |
| 936 | |
| 937 | // Select the right handler. |
| 938 | llvm::Value *llvm_eh_typeid_for = |
| 939 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); |
| 940 | |
| 941 | // Load the selector value. |
Bill Wendling | ae27059 | 2011-09-15 18:57:19 +0000 | [diff] [blame] | 942 | llvm::Value *selector = CGF.getSelectorFromSlot(); |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 943 | |
| 944 | // Test against each of the exception types we claim to catch. |
| 945 | for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) { |
| 946 | assert(i < e && "ran off end of handlers!"); |
| 947 | const EHCatchScope::Handler &handler = catchScope.getHandler(i); |
| 948 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 949 | llvm::Value *typeValue = handler.Type.RTTI; |
| 950 | assert(handler.Type.Flags == 0 && |
| 951 | "landingpads do not support catch handler flags"); |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 952 | assert(typeValue && "fell into catch-all case!"); |
| 953 | typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy); |
| 954 | |
| 955 | // Figure out the next block. |
| 956 | bool nextIsEnd; |
| 957 | llvm::BasicBlock *nextBlock; |
| 958 | |
| 959 | // If this is the last handler, we're at the end, and the next |
| 960 | // block is the block for the enclosing EH scope. |
| 961 | if (i + 1 == e) { |
| 962 | nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope()); |
| 963 | nextIsEnd = true; |
| 964 | |
| 965 | // If the next handler is a catch-all, we're at the end, and the |
| 966 | // next block is that handler. |
| 967 | } else if (catchScope.getHandler(i+1).isCatchAll()) { |
| 968 | nextBlock = catchScope.getHandler(i+1).Block; |
| 969 | nextIsEnd = true; |
| 970 | |
| 971 | // Otherwise, we're not at the end and we need a new block. |
| 972 | } else { |
| 973 | nextBlock = CGF.createBasicBlock("catch.fallthrough"); |
| 974 | nextIsEnd = false; |
| 975 | } |
| 976 | |
| 977 | // Figure out the catch type's index in the LSDA's type table. |
| 978 | llvm::CallInst *typeIndex = |
| 979 | CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue); |
| 980 | typeIndex->setDoesNotThrow(); |
| 981 | |
| 982 | llvm::Value *matchesTypeIndex = |
| 983 | CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches"); |
| 984 | CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock); |
| 985 | |
| 986 | // If the next handler is a catch-all, we're completely done. |
| 987 | if (nextIsEnd) { |
| 988 | CGF.Builder.restoreIP(savedIP); |
| 989 | return; |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 990 | } |
Ahmed Charles | e8e92b9 | 2012-02-19 11:57:29 +0000 | [diff] [blame] | 991 | // Otherwise we need to emit and continue at that block. |
| 992 | CGF.EmitBlock(nextBlock); |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 993 | } |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | void CodeGenFunction::popCatchScope() { |
| 997 | EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin()); |
| 998 | if (catchScope.hasEHBranches()) |
| 999 | emitCatchDispatchBlock(*this, catchScope); |
| 1000 | EHStack.popCatch(); |
| 1001 | } |
| 1002 | |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1003 | void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1004 | unsigned NumHandlers = S.getNumHandlers(); |
| 1005 | EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin()); |
| 1006 | assert(CatchScope.getNumHandlers() == NumHandlers); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1007 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1008 | // If the catch was not required, bail out now. |
| 1009 | if (!CatchScope.hasEHBranches()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1010 | CatchScope.clearHandlerBlocks(); |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1011 | EHStack.popCatch(); |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| 1015 | // Emit the structure of the EH dispatch for this catch. |
| 1016 | emitCatchDispatchBlock(*this, CatchScope); |
| 1017 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1018 | // Copy the handler blocks off before we pop the EH stack. Emitting |
| 1019 | // the handlers might scribble on this memory. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1020 | SmallVector<EHCatchScope::Handler, 8> Handlers( |
| 1021 | CatchScope.begin(), CatchScope.begin() + NumHandlers); |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1022 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1023 | EHStack.popCatch(); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1024 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1025 | // The fall-through block. |
| 1026 | llvm::BasicBlock *ContBB = createBasicBlock("try.cont"); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1027 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1028 | // We just emitted the body of the try; jump to the continue block. |
| 1029 | if (HaveInsertPoint()) |
| 1030 | Builder.CreateBr(ContBB); |
Mike Stump | 639787c | 2009-12-02 19:53:57 +0000 | [diff] [blame] | 1031 | |
John McCall | f553301 | 2012-06-15 05:27:05 +0000 | [diff] [blame] | 1032 | // Determine if we need an implicit rethrow for all these catch handlers; |
| 1033 | // see the comment below. |
| 1034 | bool doImplicitRethrow = false; |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1035 | if (IsFnTryBlock) |
John McCall | f553301 | 2012-06-15 05:27:05 +0000 | [diff] [blame] | 1036 | doImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) || |
| 1037 | isa<CXXConstructorDecl>(CurCodeDecl); |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1038 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1039 | // Perversely, we emit the handlers backwards precisely because we |
| 1040 | // want them to appear in source order. In all of these cases, the |
| 1041 | // catch block will have exactly one predecessor, which will be a |
| 1042 | // particular block in the catch dispatch. However, in the case of |
| 1043 | // a catch-all, one of the dispatch blocks will branch to two |
| 1044 | // different handlers, and EmitBlockAfterUses will cause the second |
| 1045 | // handler to be moved before the first. |
| 1046 | for (unsigned I = NumHandlers; I != 0; --I) { |
| 1047 | llvm::BasicBlock *CatchBlock = Handlers[I-1].Block; |
| 1048 | EmitBlockAfterUses(CatchBlock); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 1049 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1050 | // Catch the exception if this isn't a catch-all. |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1051 | const CXXCatchStmt *C = S.getHandler(I-1); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1052 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1053 | // Enter a cleanup scope, including the catch variable and the |
| 1054 | // end-catch. |
| 1055 | RunCleanupsScope CatchScope(*this); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1056 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1057 | // Initialize the catch variable and set up the cleanups. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1058 | SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad( |
| 1059 | CurrentFuncletPad); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 1060 | CGM.getCXXABI().emitBeginCatch(*this, C); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1061 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1062 | // Emit the PGO counter increment. |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1063 | incrementProfileCounter(C); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1064 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1065 | // Perform the body of the catch. |
| 1066 | EmitStmt(C->getHandlerBlock()); |
| 1067 | |
John McCall | f553301 | 2012-06-15 05:27:05 +0000 | [diff] [blame] | 1068 | // [except.handle]p11: |
| 1069 | // The currently handled exception is rethrown if control |
| 1070 | // reaches the end of a handler of the function-try-block of a |
| 1071 | // constructor or destructor. |
| 1072 | |
| 1073 | // It is important that we only do this on fallthrough and not on |
| 1074 | // return. Note that it's illegal to put a return in a |
| 1075 | // constructor function-try-block's catch handler (p14), so this |
| 1076 | // really only applies to destructors. |
| 1077 | if (doImplicitRethrow && HaveInsertPoint()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1078 | CGM.getCXXABI().emitRethrow(*this, /*isNoReturn*/false); |
John McCall | f553301 | 2012-06-15 05:27:05 +0000 | [diff] [blame] | 1079 | Builder.CreateUnreachable(); |
| 1080 | Builder.ClearInsertionPoint(); |
| 1081 | } |
| 1082 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1083 | // Fall out through the catch cleanups. |
| 1084 | CatchScope.ForceCleanup(); |
| 1085 | |
| 1086 | // Branch out of the try. |
| 1087 | if (HaveInsertPoint()) |
| 1088 | Builder.CreateBr(ContBB); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1091 | EmitBlock(ContBB); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1092 | incrementProfileCounter(&S); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 1093 | } |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1094 | |
John McCall | 55b20fc | 2010-07-21 00:52:03 +0000 | [diff] [blame] | 1095 | namespace { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1096 | struct CallEndCatchForFinally final : EHScopeStack::Cleanup { |
John McCall | 55b20fc | 2010-07-21 00:52:03 +0000 | [diff] [blame] | 1097 | llvm::Value *ForEHVar; |
| 1098 | llvm::Value *EndCatchFn; |
| 1099 | CallEndCatchForFinally(llvm::Value *ForEHVar, llvm::Value *EndCatchFn) |
| 1100 | : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {} |
| 1101 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1102 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 55b20fc | 2010-07-21 00:52:03 +0000 | [diff] [blame] | 1103 | llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch"); |
| 1104 | llvm::BasicBlock *CleanupContBB = |
| 1105 | CGF.createBasicBlock("finally.cleanup.cont"); |
| 1106 | |
| 1107 | llvm::Value *ShouldEndCatch = |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1108 | CGF.Builder.CreateFlagLoad(ForEHVar, "finally.endcatch"); |
John McCall | 55b20fc | 2010-07-21 00:52:03 +0000 | [diff] [blame] | 1109 | CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB); |
| 1110 | CGF.EmitBlock(EndCatchBB); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1111 | CGF.EmitRuntimeCallOrInvoke(EndCatchFn); // catch-all, so might throw |
John McCall | 55b20fc | 2010-07-21 00:52:03 +0000 | [diff] [blame] | 1112 | CGF.EmitBlock(CleanupContBB); |
| 1113 | } |
| 1114 | }; |
John McCall | 7719971 | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1115 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1116 | struct PerformFinally final : EHScopeStack::Cleanup { |
John McCall | 7719971 | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1117 | const Stmt *Body; |
| 1118 | llvm::Value *ForEHVar; |
| 1119 | llvm::Value *EndCatchFn; |
| 1120 | llvm::Value *RethrowFn; |
| 1121 | llvm::Value *SavedExnVar; |
| 1122 | |
| 1123 | PerformFinally(const Stmt *Body, llvm::Value *ForEHVar, |
| 1124 | llvm::Value *EndCatchFn, |
| 1125 | llvm::Value *RethrowFn, llvm::Value *SavedExnVar) |
| 1126 | : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn), |
| 1127 | RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {} |
| 1128 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1129 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 7719971 | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1130 | // Enter a cleanup to call the end-catch function if one was provided. |
| 1131 | if (EndCatchFn) |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1132 | CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup, |
| 1133 | ForEHVar, EndCatchFn); |
John McCall | 7719971 | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1134 | |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 1135 | // Save the current cleanup destination in case there are |
| 1136 | // cleanups in the finally block. |
| 1137 | llvm::Value *SavedCleanupDest = |
| 1138 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(), |
| 1139 | "cleanup.dest.saved"); |
| 1140 | |
John McCall | 7719971 | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1141 | // Emit the finally block. |
| 1142 | CGF.EmitStmt(Body); |
| 1143 | |
| 1144 | // If the end of the finally is reachable, check whether this was |
| 1145 | // for EH. If so, rethrow. |
| 1146 | if (CGF.HaveInsertPoint()) { |
| 1147 | llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow"); |
| 1148 | llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont"); |
| 1149 | |
| 1150 | llvm::Value *ShouldRethrow = |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1151 | CGF.Builder.CreateFlagLoad(ForEHVar, "finally.shouldthrow"); |
John McCall | 7719971 | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1152 | CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB); |
| 1153 | |
| 1154 | CGF.EmitBlock(RethrowBB); |
| 1155 | if (SavedExnVar) { |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1156 | CGF.EmitRuntimeCallOrInvoke(RethrowFn, |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1157 | CGF.Builder.CreateAlignedLoad(SavedExnVar, CGF.getPointerAlign())); |
John McCall | 7719971 | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1158 | } else { |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1159 | CGF.EmitRuntimeCallOrInvoke(RethrowFn); |
John McCall | 7719971 | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1160 | } |
| 1161 | CGF.Builder.CreateUnreachable(); |
| 1162 | |
| 1163 | CGF.EmitBlock(ContBB); |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 1164 | |
| 1165 | // Restore the cleanup destination. |
| 1166 | CGF.Builder.CreateStore(SavedCleanupDest, |
| 1167 | CGF.getNormalCleanupDestSlot()); |
John McCall | 7719971 | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | // Leave the end-catch cleanup. As an optimization, pretend that |
| 1171 | // the fallthrough path was inaccessible; we've dynamically proven |
| 1172 | // that we're not in the EH case along that path. |
| 1173 | if (EndCatchFn) { |
| 1174 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
| 1175 | CGF.PopCleanupBlock(); |
| 1176 | CGF.Builder.restoreIP(SavedIP); |
| 1177 | } |
| 1178 | |
| 1179 | // Now make sure we actually have an insertion point or the |
| 1180 | // cleanup gods will hate us. |
| 1181 | CGF.EnsureInsertPoint(); |
| 1182 | } |
| 1183 | }; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1184 | } // end anonymous namespace |
John McCall | 55b20fc | 2010-07-21 00:52:03 +0000 | [diff] [blame] | 1185 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1186 | /// Enters a finally block for an implementation using zero-cost |
| 1187 | /// exceptions. This is mostly general, but hard-codes some |
| 1188 | /// language/ABI-specific behavior in the catch-all sections. |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1189 | void CodeGenFunction::FinallyInfo::enter(CodeGenFunction &CGF, |
| 1190 | const Stmt *body, |
| 1191 | llvm::Constant *beginCatchFn, |
| 1192 | llvm::Constant *endCatchFn, |
| 1193 | llvm::Constant *rethrowFn) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1194 | assert((beginCatchFn != nullptr) == (endCatchFn != nullptr) && |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1195 | "begin/end catch functions not paired"); |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1196 | assert(rethrowFn && "rethrow function is required"); |
| 1197 | |
| 1198 | BeginCatchFn = beginCatchFn; |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1199 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1200 | // The rethrow function has one of the following two types: |
| 1201 | // void (*)() |
| 1202 | // void (*)(void*) |
| 1203 | // In the latter case we need to pass it the exception object. |
| 1204 | // But we can't use the exception slot because the @finally might |
| 1205 | // have a landing pad (which would overwrite the exception slot). |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1206 | llvm::FunctionType *rethrowFnTy = |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1207 | cast<llvm::FunctionType>( |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1208 | cast<llvm::PointerType>(rethrowFn->getType())->getElementType()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1209 | SavedExnVar = nullptr; |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1210 | if (rethrowFnTy->getNumParams()) |
| 1211 | SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn"); |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1212 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1213 | // A finally block is a statement which must be executed on any edge |
| 1214 | // out of a given scope. Unlike a cleanup, the finally block may |
| 1215 | // contain arbitrary control flow leading out of itself. In |
| 1216 | // addition, finally blocks should always be executed, even if there |
| 1217 | // are no catch handlers higher on the stack. Therefore, we |
| 1218 | // surround the protected scope with a combination of a normal |
| 1219 | // cleanup (to catch attempts to break out of the block via normal |
| 1220 | // control flow) and an EH catch-all (semantically "outside" any try |
| 1221 | // statement to which the finally block might have been attached). |
| 1222 | // The finally block itself is generated in the context of a cleanup |
| 1223 | // which conditionally leaves the catch-all. |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1224 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1225 | // Jump destination for performing the finally block on an exception |
| 1226 | // edge. We'll never actually reach this block, so unreachable is |
| 1227 | // fine. |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1228 | RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock()); |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1229 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1230 | // Whether the finally block is being executed for EH purposes. |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1231 | ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh"); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1232 | CGF.Builder.CreateFlagStore(false, ForEHVar); |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1233 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1234 | // Enter a normal cleanup which will perform the @finally block. |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1235 | CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body, |
| 1236 | ForEHVar, endCatchFn, |
| 1237 | rethrowFn, SavedExnVar); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1238 | |
| 1239 | // Enter a catch-all scope. |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1240 | llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall"); |
| 1241 | EHCatchScope *catchScope = CGF.EHStack.pushCatch(1); |
| 1242 | catchScope->setCatchAllHandler(0, catchBB); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1245 | void CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1246 | // Leave the finally catch-all. |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1247 | EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin()); |
| 1248 | llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block; |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1249 | |
| 1250 | CGF.popCatchScope(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1251 | |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1252 | // If there are any references to the catch-all block, emit it. |
| 1253 | if (catchBB->use_empty()) { |
| 1254 | delete catchBB; |
| 1255 | } else { |
| 1256 | CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP(); |
| 1257 | CGF.EmitBlock(catchBB); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1258 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1259 | llvm::Value *exn = nullptr; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1260 | |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1261 | // If there's a begin-catch function, call it. |
| 1262 | if (BeginCatchFn) { |
Bill Wendling | ae27059 | 2011-09-15 18:57:19 +0000 | [diff] [blame] | 1263 | exn = CGF.getExceptionFromSlot(); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1264 | CGF.EmitNounwindRuntimeCall(BeginCatchFn, exn); |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | // If we need to remember the exception pointer to rethrow later, do so. |
| 1268 | if (SavedExnVar) { |
Bill Wendling | ae27059 | 2011-09-15 18:57:19 +0000 | [diff] [blame] | 1269 | if (!exn) exn = CGF.getExceptionFromSlot(); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1270 | CGF.Builder.CreateAlignedStore(exn, SavedExnVar, CGF.getPointerAlign()); |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | // Tell the cleanups in the finally block that we're do this for EH. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1274 | CGF.Builder.CreateFlagStore(true, ForEHVar); |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 1275 | |
| 1276 | // Thread a jump through the finally cleanup. |
| 1277 | CGF.EmitBranchThroughCleanup(RethrowDest); |
| 1278 | |
| 1279 | CGF.Builder.restoreIP(savedIP); |
| 1280 | } |
| 1281 | |
| 1282 | // Finally, leave the @finally cleanup. |
| 1283 | CGF.PopCleanupBlock(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() { |
| 1287 | if (TerminateLandingPad) |
| 1288 | return TerminateLandingPad; |
| 1289 | |
| 1290 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
| 1291 | |
| 1292 | // This will get inserted at the end of the function. |
| 1293 | TerminateLandingPad = createBasicBlock("terminate.lpad"); |
| 1294 | Builder.SetInsertPoint(TerminateLandingPad); |
| 1295 | |
| 1296 | // Tell the backend that this is a landing pad. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1297 | const EHPersonality &Personality = EHPersonality::get(*this); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1298 | |
| 1299 | if (!CurFn->hasPersonalityFn()) |
| 1300 | CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality)); |
| 1301 | |
| 1302 | llvm::LandingPadInst *LPadInst = Builder.CreateLandingPad( |
| 1303 | llvm::StructType::get(Int8PtrTy, Int32Ty, nullptr), 0); |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 1304 | LPadInst->addClause(getCatchAllValue(*this)); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1305 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1306 | llvm::Value *Exn = nullptr; |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 1307 | if (getLangOpts().CPlusPlus) |
| 1308 | Exn = Builder.CreateExtractValue(LPadInst, 0); |
| 1309 | llvm::CallInst *terminateCall = |
| 1310 | CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn); |
John McCall | 66b2277 | 2013-02-12 03:51:46 +0000 | [diff] [blame] | 1311 | terminateCall->setDoesNotReturn(); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1312 | Builder.CreateUnreachable(); |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1313 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1314 | // Restore the saved insertion state. |
| 1315 | Builder.restoreIP(SavedIP); |
John McCall | 891f80e | 2010-04-30 00:06:43 +0000 | [diff] [blame] | 1316 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1317 | return TerminateLandingPad; |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 1318 | } |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 1319 | |
| 1320 | llvm::BasicBlock *CodeGenFunction::getTerminateHandler() { |
Mike Stump | 182f383 | 2009-12-10 00:02:42 +0000 | [diff] [blame] | 1321 | if (TerminateHandler) |
| 1322 | return TerminateHandler; |
| 1323 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1324 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
Mike Stump | 7695809 | 2009-12-09 23:31:35 +0000 | [diff] [blame] | 1325 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1326 | // Set up the terminate handler. This block is inserted at the very |
| 1327 | // end of the function by FinishFunction. |
Mike Stump | 182f383 | 2009-12-10 00:02:42 +0000 | [diff] [blame] | 1328 | TerminateHandler = createBasicBlock("terminate.handler"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1329 | Builder.SetInsertPoint(TerminateHandler); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1330 | llvm::Value *Exn = nullptr; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1331 | SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad( |
| 1332 | CurrentFuncletPad); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1333 | if (EHPersonality::get(*this).usesFuncletPads()) { |
| 1334 | llvm::Value *ParentPad = CurrentFuncletPad; |
| 1335 | if (!ParentPad) |
| 1336 | ParentPad = llvm::ConstantTokenNone::get(CGM.getLLVMContext()); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1337 | CurrentFuncletPad = Builder.CreateCleanupPad(ParentPad); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1338 | } else { |
| 1339 | if (getLangOpts().CPlusPlus) |
| 1340 | Exn = getExceptionFromSlot(); |
| 1341 | } |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 1342 | llvm::CallInst *terminateCall = |
| 1343 | CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn); |
John McCall | 45ff380 | 2013-06-20 21:37:43 +0000 | [diff] [blame] | 1344 | terminateCall->setDoesNotReturn(); |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 1345 | Builder.CreateUnreachable(); |
| 1346 | |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1347 | // Restore the saved insertion state. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1348 | Builder.restoreIP(SavedIP); |
Mike Stump | 7695809 | 2009-12-09 23:31:35 +0000 | [diff] [blame] | 1349 | |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 1350 | return TerminateHandler; |
| 1351 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1352 | |
David Chisnall | c686004 | 2012-11-07 16:50:40 +0000 | [diff] [blame] | 1353 | llvm::BasicBlock *CodeGenFunction::getEHResumeBlock(bool isCleanup) { |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1354 | if (EHResumeBlock) return EHResumeBlock; |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1355 | |
| 1356 | CGBuilderTy::InsertPoint SavedIP = Builder.saveIP(); |
| 1357 | |
| 1358 | // We emit a jump to a notional label at the outermost unwind state. |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1359 | EHResumeBlock = createBasicBlock("eh.resume"); |
| 1360 | Builder.SetInsertPoint(EHResumeBlock); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1361 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1362 | const EHPersonality &Personality = EHPersonality::get(*this); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1363 | |
| 1364 | // This can always be a call because we necessarily didn't find |
| 1365 | // anything on the EH stack which needs our help. |
Benjamin Kramer | af2771b | 2012-02-08 12:41:24 +0000 | [diff] [blame] | 1366 | const char *RethrowName = Personality.CatchallRethrowFn; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1367 | if (RethrowName != nullptr && !isCleanup) { |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1368 | EmitRuntimeCall(getCatchallRethrowFn(CGM, RethrowName), |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1369 | getExceptionFromSlot())->setDoesNotReturn(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1370 | Builder.CreateUnreachable(); |
| 1371 | Builder.restoreIP(SavedIP); |
| 1372 | return EHResumeBlock; |
John McCall | 93c332a | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1375 | // Recreate the landingpad's return value for the 'resume' instruction. |
| 1376 | llvm::Value *Exn = getExceptionFromSlot(); |
| 1377 | llvm::Value *Sel = getSelectorFromSlot(); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1378 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1379 | llvm::Type *LPadType = llvm::StructType::get(Exn->getType(), |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1380 | Sel->getType(), nullptr); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1381 | llvm::Value *LPadVal = llvm::UndefValue::get(LPadType); |
| 1382 | LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val"); |
| 1383 | LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val"); |
| 1384 | |
| 1385 | Builder.CreateResume(LPadVal); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1386 | Builder.restoreIP(SavedIP); |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1387 | return EHResumeBlock; |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1388 | } |
Reid Kleckner | 98592d9 | 2013-09-16 21:46:30 +0000 | [diff] [blame] | 1389 | |
| 1390 | void CodeGenFunction::EmitSEHTryStmt(const SEHTryStmt &S) { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1391 | EnterSEHTryStmt(S); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1392 | { |
| 1393 | JumpDest TryExit = getJumpDestInCurrentScope("__try.__leave"); |
| 1394 | |
| 1395 | SEHTryEpilogueStack.push_back(&TryExit); |
| 1396 | EmitStmt(S.getTryBlock()); |
| 1397 | SEHTryEpilogueStack.pop_back(); |
| 1398 | |
| 1399 | if (!TryExit.getBlock()->use_empty()) |
| 1400 | EmitBlock(TryExit.getBlock(), /*IsFinished=*/true); |
| 1401 | else |
| 1402 | delete TryExit.getBlock(); |
| 1403 | } |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1404 | ExitSEHTryStmt(S); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | namespace { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1408 | struct PerformSEHFinally final : EHScopeStack::Cleanup { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1409 | llvm::Function *OutlinedFinally; |
| 1410 | PerformSEHFinally(llvm::Function *OutlinedFinally) |
| 1411 | : OutlinedFinally(OutlinedFinally) {} |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1412 | |
| 1413 | void Emit(CodeGenFunction &CGF, Flags F) override { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1414 | ASTContext &Context = CGF.getContext(); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1415 | CodeGenModule &CGM = CGF.CGM; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1416 | |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1417 | CallArgList Args; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1418 | |
| 1419 | // Compute the two argument values. |
| 1420 | QualType ArgTys[2] = {Context.UnsignedCharTy, Context.VoidPtrTy}; |
| 1421 | llvm::Value *LocalAddrFn = CGM.getIntrinsic(llvm::Intrinsic::localaddress); |
| 1422 | llvm::Value *FP = CGF.Builder.CreateCall(LocalAddrFn); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1423 | llvm::Value *IsForEH = |
| 1424 | llvm::ConstantInt::get(CGF.ConvertType(ArgTys[0]), F.isForEHCleanup()); |
| 1425 | Args.add(RValue::get(IsForEH), ArgTys[0]); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1426 | Args.add(RValue::get(FP), ArgTys[1]); |
| 1427 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1428 | // Arrange a two-arg function info and type. |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1429 | const CGFunctionInfo &FnInfo = |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1430 | CGM.getTypes().arrangeBuiltinFunctionCall(Context.VoidTy, Args); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1431 | |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1432 | CGF.EmitCall(FnInfo, OutlinedFinally, ReturnValueSlot(), Args); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1433 | } |
| 1434 | }; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1435 | } // end anonymous namespace |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1436 | |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1437 | namespace { |
| 1438 | /// Find all local variable captures in the statement. |
| 1439 | struct CaptureFinder : ConstStmtVisitor<CaptureFinder> { |
| 1440 | CodeGenFunction &ParentCGF; |
| 1441 | const VarDecl *ParentThis; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1442 | llvm::SmallSetVector<const VarDecl *, 4> Captures; |
| 1443 | Address SEHCodeSlot = Address::invalid(); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1444 | CaptureFinder(CodeGenFunction &ParentCGF, const VarDecl *ParentThis) |
| 1445 | : ParentCGF(ParentCGF), ParentThis(ParentThis) {} |
| 1446 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1447 | // Return true if we need to do any capturing work. |
| 1448 | bool foundCaptures() { |
| 1449 | return !Captures.empty() || SEHCodeSlot.isValid(); |
| 1450 | } |
| 1451 | |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1452 | void Visit(const Stmt *S) { |
| 1453 | // See if this is a capture, then recurse. |
| 1454 | ConstStmtVisitor<CaptureFinder>::Visit(S); |
| 1455 | for (const Stmt *Child : S->children()) |
| 1456 | if (Child) |
| 1457 | Visit(Child); |
| 1458 | } |
| 1459 | |
| 1460 | void VisitDeclRefExpr(const DeclRefExpr *E) { |
| 1461 | // If this is already a capture, just make sure we capture 'this'. |
| 1462 | if (E->refersToEnclosingVariableOrCapture()) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1463 | Captures.insert(ParentThis); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1464 | return; |
| 1465 | } |
| 1466 | |
| 1467 | const auto *D = dyn_cast<VarDecl>(E->getDecl()); |
| 1468 | if (D && D->isLocalVarDeclOrParm() && D->hasLocalStorage()) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1469 | Captures.insert(D); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | void VisitCXXThisExpr(const CXXThisExpr *E) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1473 | Captures.insert(ParentThis); |
| 1474 | } |
| 1475 | |
| 1476 | void VisitCallExpr(const CallExpr *E) { |
| 1477 | // We only need to add parent frame allocations for these builtins in x86. |
| 1478 | if (ParentCGF.getTarget().getTriple().getArch() != llvm::Triple::x86) |
| 1479 | return; |
| 1480 | |
| 1481 | unsigned ID = E->getBuiltinCallee(); |
| 1482 | switch (ID) { |
| 1483 | case Builtin::BI__exception_code: |
| 1484 | case Builtin::BI_exception_code: |
| 1485 | // This is the simple case where we are the outermost finally. All we |
| 1486 | // have to do here is make sure we escape this and recover it in the |
| 1487 | // outlined handler. |
| 1488 | if (!SEHCodeSlot.isValid()) |
| 1489 | SEHCodeSlot = ParentCGF.SEHCodeSlotStack.back(); |
| 1490 | break; |
| 1491 | } |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1492 | } |
| 1493 | }; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1494 | } // end anonymous namespace |
| 1495 | |
| 1496 | Address CodeGenFunction::recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF, |
| 1497 | Address ParentVar, |
| 1498 | llvm::Value *ParentFP) { |
| 1499 | llvm::CallInst *RecoverCall = nullptr; |
| 1500 | CGBuilderTy Builder(*this, AllocaInsertPt); |
| 1501 | if (auto *ParentAlloca = dyn_cast<llvm::AllocaInst>(ParentVar.getPointer())) { |
| 1502 | // Mark the variable escaped if nobody else referenced it and compute the |
| 1503 | // localescape index. |
| 1504 | auto InsertPair = ParentCGF.EscapedLocals.insert( |
| 1505 | std::make_pair(ParentAlloca, ParentCGF.EscapedLocals.size())); |
| 1506 | int FrameEscapeIdx = InsertPair.first->second; |
| 1507 | // call i8* @llvm.localrecover(i8* bitcast(@parentFn), i8* %fp, i32 N) |
| 1508 | llvm::Function *FrameRecoverFn = llvm::Intrinsic::getDeclaration( |
| 1509 | &CGM.getModule(), llvm::Intrinsic::localrecover); |
| 1510 | llvm::Constant *ParentI8Fn = |
| 1511 | llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy); |
| 1512 | RecoverCall = Builder.CreateCall( |
| 1513 | FrameRecoverFn, {ParentI8Fn, ParentFP, |
| 1514 | llvm::ConstantInt::get(Int32Ty, FrameEscapeIdx)}); |
| 1515 | |
| 1516 | } else { |
| 1517 | // If the parent didn't have an alloca, we're doing some nested outlining. |
| 1518 | // Just clone the existing localrecover call, but tweak the FP argument to |
| 1519 | // use our FP value. All other arguments are constants. |
| 1520 | auto *ParentRecover = |
| 1521 | cast<llvm::IntrinsicInst>(ParentVar.getPointer()->stripPointerCasts()); |
| 1522 | assert(ParentRecover->getIntrinsicID() == llvm::Intrinsic::localrecover && |
| 1523 | "expected alloca or localrecover in parent LocalDeclMap"); |
| 1524 | RecoverCall = cast<llvm::CallInst>(ParentRecover->clone()); |
| 1525 | RecoverCall->setArgOperand(1, ParentFP); |
| 1526 | RecoverCall->insertBefore(AllocaInsertPt); |
| 1527 | } |
| 1528 | |
| 1529 | // Bitcast the variable, rename it, and insert it in the local decl map. |
| 1530 | llvm::Value *ChildVar = |
| 1531 | Builder.CreateBitCast(RecoverCall, ParentVar.getType()); |
| 1532 | ChildVar->setName(ParentVar.getName()); |
| 1533 | return Address(ChildVar, ParentVar.getAlignment()); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | void CodeGenFunction::EmitCapturedLocals(CodeGenFunction &ParentCGF, |
| 1537 | const Stmt *OutlinedStmt, |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1538 | bool IsFilter) { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1539 | // Find all captures in the Stmt. |
| 1540 | CaptureFinder Finder(ParentCGF, ParentCGF.CXXABIThisDecl); |
| 1541 | Finder.Visit(OutlinedStmt); |
| 1542 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1543 | // We can exit early on x86_64 when there are no captures. We just have to |
| 1544 | // save the exception code in filters so that __exception_code() works. |
| 1545 | if (!Finder.foundCaptures() && |
| 1546 | CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) { |
| 1547 | if (IsFilter) |
| 1548 | EmitSEHExceptionCodeSave(ParentCGF, nullptr, nullptr); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1549 | return; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1550 | } |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1551 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1552 | llvm::Value *EntryFP = nullptr; |
| 1553 | CGBuilderTy Builder(CGM, AllocaInsertPt); |
| 1554 | if (IsFilter && CGM.getTarget().getTriple().getArch() == llvm::Triple::x86) { |
| 1555 | // 32-bit SEH filters need to be careful about FP recovery. The end of the |
| 1556 | // EH registration is passed in as the EBP physical register. We can |
| 1557 | // recover that with llvm.frameaddress(1). |
| 1558 | EntryFP = Builder.CreateCall( |
| 1559 | CGM.getIntrinsic(llvm::Intrinsic::frameaddress), {Builder.getInt32(1)}); |
| 1560 | } else { |
| 1561 | // Otherwise, for x64 and 32-bit finally functions, the parent FP is the |
| 1562 | // second parameter. |
| 1563 | auto AI = CurFn->arg_begin(); |
| 1564 | ++AI; |
| 1565 | EntryFP = &*AI; |
| 1566 | } |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1567 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1568 | llvm::Value *ParentFP = EntryFP; |
| 1569 | if (IsFilter) { |
| 1570 | // Given whatever FP the runtime provided us in EntryFP, recover the true |
| 1571 | // frame pointer of the parent function. We only need to do this in filters, |
| 1572 | // since finally funclets recover the parent FP for us. |
| 1573 | llvm::Function *RecoverFPIntrin = |
| 1574 | CGM.getIntrinsic(llvm::Intrinsic::x86_seh_recoverfp); |
| 1575 | llvm::Constant *ParentI8Fn = |
| 1576 | llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy); |
| 1577 | ParentFP = Builder.CreateCall(RecoverFPIntrin, {ParentI8Fn, EntryFP}); |
| 1578 | } |
| 1579 | |
| 1580 | // Create llvm.localrecover calls for all captures. |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1581 | for (const VarDecl *VD : Finder.Captures) { |
| 1582 | if (isa<ImplicitParamDecl>(VD)) { |
| 1583 | CGM.ErrorUnsupported(VD, "'this' captured by SEH"); |
| 1584 | CXXThisValue = llvm::UndefValue::get(ConvertTypeForMem(VD->getType())); |
| 1585 | continue; |
| 1586 | } |
| 1587 | if (VD->getType()->isVariablyModifiedType()) { |
| 1588 | CGM.ErrorUnsupported(VD, "VLA captured by SEH"); |
| 1589 | continue; |
| 1590 | } |
| 1591 | assert((isa<ImplicitParamDecl>(VD) || VD->isLocalVarDeclOrParm()) && |
| 1592 | "captured non-local variable"); |
| 1593 | |
| 1594 | // If this decl hasn't been declared yet, it will be declared in the |
| 1595 | // OutlinedStmt. |
| 1596 | auto I = ParentCGF.LocalDeclMap.find(VD); |
| 1597 | if (I == ParentCGF.LocalDeclMap.end()) |
| 1598 | continue; |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1599 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1600 | Address ParentVar = I->second; |
| 1601 | setAddrOfLocalVar( |
| 1602 | VD, recoverAddrOfEscapedLocal(ParentCGF, ParentVar, ParentFP)); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1603 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1604 | |
| 1605 | if (Finder.SEHCodeSlot.isValid()) { |
| 1606 | SEHCodeSlotStack.push_back( |
| 1607 | recoverAddrOfEscapedLocal(ParentCGF, Finder.SEHCodeSlot, ParentFP)); |
| 1608 | } |
| 1609 | |
| 1610 | if (IsFilter) |
| 1611 | EmitSEHExceptionCodeSave(ParentCGF, ParentFP, EntryFP); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | /// Arrange a function prototype that can be called by Windows exception |
| 1615 | /// handling personalities. On Win64, the prototype looks like: |
| 1616 | /// RetTy func(void *EHPtrs, void *ParentFP); |
| 1617 | void CodeGenFunction::startOutlinedSEHHelper(CodeGenFunction &ParentCGF, |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1618 | bool IsFilter, |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1619 | const Stmt *OutlinedStmt) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1620 | SourceLocation StartLoc = OutlinedStmt->getLocStart(); |
| 1621 | |
| 1622 | // Get the mangled function name. |
| 1623 | SmallString<128> Name; |
| 1624 | { |
| 1625 | llvm::raw_svector_ostream OS(Name); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1626 | const FunctionDecl *ParentSEHFn = ParentCGF.CurSEHParent; |
| 1627 | assert(ParentSEHFn && "No CurSEHParent!"); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1628 | MangleContext &Mangler = CGM.getCXXABI().getMangleContext(); |
| 1629 | if (IsFilter) |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1630 | Mangler.mangleSEHFilterExpression(ParentSEHFn, OS); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1631 | else |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1632 | Mangler.mangleSEHFinallyBlock(ParentSEHFn, OS); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | FunctionArgList Args; |
| 1636 | if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 || !IsFilter) { |
| 1637 | // All SEH finally functions take two parameters. Win64 filters take two |
| 1638 | // parameters. Win32 filters take no parameters. |
| 1639 | if (IsFilter) { |
| 1640 | Args.push_back(ImplicitParamDecl::Create( |
| 1641 | getContext(), nullptr, StartLoc, |
| 1642 | &getContext().Idents.get("exception_pointers"), |
| 1643 | getContext().VoidPtrTy)); |
| 1644 | } else { |
| 1645 | Args.push_back(ImplicitParamDecl::Create( |
| 1646 | getContext(), nullptr, StartLoc, |
| 1647 | &getContext().Idents.get("abnormal_termination"), |
| 1648 | getContext().UnsignedCharTy)); |
| 1649 | } |
| 1650 | Args.push_back(ImplicitParamDecl::Create( |
| 1651 | getContext(), nullptr, StartLoc, |
| 1652 | &getContext().Idents.get("frame_pointer"), getContext().VoidPtrTy)); |
| 1653 | } |
| 1654 | |
| 1655 | QualType RetTy = IsFilter ? getContext().LongTy : getContext().VoidTy; |
| 1656 | |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1657 | llvm::Function *ParentFn = ParentCGF.CurFn; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1658 | const CGFunctionInfo &FnInfo = |
| 1659 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(RetTy, Args); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1660 | |
| 1661 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
| 1662 | llvm::Function *Fn = llvm::Function::Create( |
| 1663 | FnTy, llvm::GlobalValue::InternalLinkage, Name.str(), &CGM.getModule()); |
| 1664 | // The filter is either in the same comdat as the function, or it's internal. |
| 1665 | if (llvm::Comdat *C = ParentFn->getComdat()) { |
| 1666 | Fn->setComdat(C); |
| 1667 | } else if (ParentFn->hasWeakLinkage() || ParentFn->hasLinkOnceLinkage()) { |
| 1668 | llvm::Comdat *C = CGM.getModule().getOrInsertComdat(ParentFn->getName()); |
| 1669 | ParentFn->setComdat(C); |
| 1670 | Fn->setComdat(C); |
| 1671 | } else { |
| 1672 | Fn->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1673 | } |
| 1674 | |
| 1675 | IsOutlinedSEHHelper = true; |
| 1676 | |
| 1677 | StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args, |
| 1678 | OutlinedStmt->getLocStart(), OutlinedStmt->getLocStart()); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1679 | CurSEHParent = ParentCGF.CurSEHParent; |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1680 | |
| 1681 | CGM.SetLLVMFunctionAttributes(nullptr, FnInfo, CurFn); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1682 | EmitCapturedLocals(ParentCGF, OutlinedStmt, IsFilter); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1683 | } |
| 1684 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1685 | /// Create a stub filter function that will ultimately hold the code of the |
| 1686 | /// filter expression. The EH preparation passes in LLVM will outline the code |
| 1687 | /// from the main function body into this stub. |
| 1688 | llvm::Function * |
| 1689 | CodeGenFunction::GenerateSEHFilterFunction(CodeGenFunction &ParentCGF, |
| 1690 | const SEHExceptStmt &Except) { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1691 | const Expr *FilterExpr = Except.getFilterExpr(); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1692 | startOutlinedSEHHelper(ParentCGF, true, FilterExpr); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1693 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1694 | // Emit the original filter expression, convert to i32, and return. |
| 1695 | llvm::Value *R = EmitScalarExpr(FilterExpr); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1696 | R = Builder.CreateIntCast(R, ConvertType(getContext().LongTy), |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1697 | FilterExpr->getType()->isSignedIntegerType()); |
| 1698 | Builder.CreateStore(R, ReturnValue); |
| 1699 | |
| 1700 | FinishFunction(FilterExpr->getLocEnd()); |
| 1701 | |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1702 | return CurFn; |
| 1703 | } |
| 1704 | |
| 1705 | llvm::Function * |
| 1706 | CodeGenFunction::GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF, |
| 1707 | const SEHFinallyStmt &Finally) { |
| 1708 | const Stmt *FinallyBlock = Finally.getBlock(); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1709 | startOutlinedSEHHelper(ParentCGF, false, FinallyBlock); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1710 | |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1711 | // Emit the original filter expression, convert to i32, and return. |
| 1712 | EmitStmt(FinallyBlock); |
| 1713 | |
| 1714 | FinishFunction(FinallyBlock->getLocEnd()); |
| 1715 | |
| 1716 | return CurFn; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1719 | void CodeGenFunction::EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF, |
| 1720 | llvm::Value *ParentFP, |
| 1721 | llvm::Value *EntryFP) { |
| 1722 | // Get the pointer to the EXCEPTION_POINTERS struct. This is returned by the |
| 1723 | // __exception_info intrinsic. |
| 1724 | if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) { |
| 1725 | // On Win64, the info is passed as the first parameter to the filter. |
| 1726 | SEHInfo = &*CurFn->arg_begin(); |
| 1727 | SEHCodeSlotStack.push_back( |
| 1728 | CreateMemTemp(getContext().IntTy, "__exception_code")); |
| 1729 | } else { |
| 1730 | // On Win32, the EBP on entry to the filter points to the end of an |
| 1731 | // exception registration object. It contains 6 32-bit fields, and the info |
| 1732 | // pointer is stored in the second field. So, GEP 20 bytes backwards and |
| 1733 | // load the pointer. |
| 1734 | SEHInfo = Builder.CreateConstInBoundsGEP1_32(Int8Ty, EntryFP, -20); |
| 1735 | SEHInfo = Builder.CreateBitCast(SEHInfo, Int8PtrTy->getPointerTo()); |
| 1736 | SEHInfo = Builder.CreateAlignedLoad(Int8PtrTy, SEHInfo, getPointerAlign()); |
| 1737 | SEHCodeSlotStack.push_back(recoverAddrOfEscapedLocal( |
| 1738 | ParentCGF, ParentCGF.SEHCodeSlotStack.back(), ParentFP)); |
| 1739 | } |
| 1740 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1741 | // Save the exception code in the exception slot to unify exception access in |
| 1742 | // the filter function and the landing pad. |
| 1743 | // struct EXCEPTION_POINTERS { |
| 1744 | // EXCEPTION_RECORD *ExceptionRecord; |
| 1745 | // CONTEXT *ContextRecord; |
| 1746 | // }; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1747 | // int exceptioncode = exception_pointers->ExceptionRecord->ExceptionCode; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1748 | llvm::Type *RecordTy = CGM.Int32Ty->getPointerTo(); |
| 1749 | llvm::Type *PtrsTy = llvm::StructType::get(RecordTy, CGM.VoidPtrTy, nullptr); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1750 | llvm::Value *Ptrs = Builder.CreateBitCast(SEHInfo, PtrsTy->getPointerTo()); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1751 | llvm::Value *Rec = Builder.CreateStructGEP(PtrsTy, Ptrs, 0); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1752 | Rec = Builder.CreateAlignedLoad(Rec, getPointerAlign()); |
| 1753 | llvm::Value *Code = Builder.CreateAlignedLoad(Rec, getIntAlign()); |
| 1754 | assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except"); |
| 1755 | Builder.CreateStore(Code, SEHCodeSlotStack.back()); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | llvm::Value *CodeGenFunction::EmitSEHExceptionInfo() { |
| 1759 | // Sema should diagnose calling this builtin outside of a filter context, but |
| 1760 | // don't crash if we screw up. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1761 | if (!SEHInfo) |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1762 | return llvm::UndefValue::get(Int8PtrTy); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1763 | assert(SEHInfo->getType() == Int8PtrTy); |
| 1764 | return SEHInfo; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | llvm::Value *CodeGenFunction::EmitSEHExceptionCode() { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1768 | assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except"); |
| 1769 | return Builder.CreateLoad(SEHCodeSlotStack.back()); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | llvm::Value *CodeGenFunction::EmitSEHAbnormalTermination() { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1773 | // Abnormal termination is just the first parameter to the outlined finally |
| 1774 | // helper. |
| 1775 | auto AI = CurFn->arg_begin(); |
| 1776 | return Builder.CreateZExt(&*AI, Int32Ty); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1777 | } |
| 1778 | |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1779 | void CodeGenFunction::EnterSEHTryStmt(const SEHTryStmt &S) { |
| 1780 | CodeGenFunction HelperCGF(CGM, /*suppressNewContext=*/true); |
| 1781 | if (const SEHFinallyStmt *Finally = S.getFinallyHandler()) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1782 | // Outline the finally block. |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1783 | llvm::Function *FinallyFunc = |
| 1784 | HelperCGF.GenerateSEHFinallyFunction(*this, *Finally); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1785 | |
| 1786 | // Push a cleanup for __finally blocks. |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1787 | EHStack.pushCleanup<PerformSEHFinally>(NormalAndEHCleanup, FinallyFunc); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1788 | return; |
| 1789 | } |
| 1790 | |
| 1791 | // Otherwise, we must have an __except block. |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1792 | const SEHExceptStmt *Except = S.getExceptHandler(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1793 | assert(Except); |
| 1794 | EHCatchScope *CatchScope = EHStack.pushCatch(1); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1795 | SEHCodeSlotStack.push_back( |
| 1796 | CreateMemTemp(getContext().IntTy, "__exception_code")); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1797 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1798 | // If the filter is known to evaluate to 1, then we can use the clause |
| 1799 | // "catch i8* null". We can't do this on x86 because the filter has to save |
| 1800 | // the exception code. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1801 | llvm::Constant *C = |
| 1802 | CGM.EmitConstantExpr(Except->getFilterExpr(), getContext().IntTy, this); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1803 | if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 && C && |
| 1804 | C->isOneValue()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1805 | CatchScope->setCatchAllHandler(0, createBasicBlock("__except")); |
| 1806 | return; |
| 1807 | } |
| 1808 | |
| 1809 | // In general, we have to emit an outlined filter function. Use the function |
| 1810 | // in place of the RTTI typeinfo global that C++ EH uses. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1811 | llvm::Function *FilterFunc = |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1812 | HelperCGF.GenerateSEHFilterFunction(*this, *Except); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1813 | llvm::Constant *OpaqueFunc = |
| 1814 | llvm::ConstantExpr::getBitCast(FilterFunc, Int8PtrTy); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1815 | CatchScope->setHandler(0, OpaqueFunc, createBasicBlock("__except.ret")); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1818 | void CodeGenFunction::ExitSEHTryStmt(const SEHTryStmt &S) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1819 | // Just pop the cleanup if it's a __finally block. |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1820 | if (S.getFinallyHandler()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1821 | PopCleanupBlock(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1822 | return; |
| 1823 | } |
| 1824 | |
| 1825 | // Otherwise, we must have an __except block. |
| 1826 | const SEHExceptStmt *Except = S.getExceptHandler(); |
| 1827 | assert(Except && "__try must have __finally xor __except"); |
| 1828 | EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin()); |
| 1829 | |
| 1830 | // Don't emit the __except block if the __try block lacked invokes. |
| 1831 | // TODO: Model unwind edges from instructions, either with iload / istore or |
| 1832 | // a try body function. |
| 1833 | if (!CatchScope.hasEHBranches()) { |
| 1834 | CatchScope.clearHandlerBlocks(); |
| 1835 | EHStack.popCatch(); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1836 | SEHCodeSlotStack.pop_back(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1837 | return; |
| 1838 | } |
| 1839 | |
| 1840 | // The fall-through block. |
| 1841 | llvm::BasicBlock *ContBB = createBasicBlock("__try.cont"); |
| 1842 | |
| 1843 | // We just emitted the body of the __try; jump to the continue block. |
| 1844 | if (HaveInsertPoint()) |
| 1845 | Builder.CreateBr(ContBB); |
| 1846 | |
| 1847 | // Check if our filter function returned true. |
| 1848 | emitCatchDispatchBlock(*this, CatchScope); |
| 1849 | |
| 1850 | // Grab the block before we pop the handler. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1851 | llvm::BasicBlock *CatchPadBB = CatchScope.getHandler(0).Block; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1852 | EHStack.popCatch(); |
| 1853 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1854 | EmitBlockAfterUses(CatchPadBB); |
| 1855 | |
| 1856 | // __except blocks don't get outlined into funclets, so immediately do a |
| 1857 | // catchret. |
| 1858 | llvm::CatchPadInst *CPI = |
| 1859 | cast<llvm::CatchPadInst>(CatchPadBB->getFirstNonPHI()); |
| 1860 | llvm::BasicBlock *ExceptBB = createBasicBlock("__except"); |
| 1861 | Builder.CreateCatchRet(CPI, ExceptBB); |
| 1862 | EmitBlock(ExceptBB); |
| 1863 | |
| 1864 | // On Win64, the exception code is returned in EAX. Copy it into the slot. |
| 1865 | if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) { |
| 1866 | llvm::Function *SEHCodeIntrin = |
| 1867 | CGM.getIntrinsic(llvm::Intrinsic::eh_exceptioncode); |
| 1868 | llvm::Value *Code = Builder.CreateCall(SEHCodeIntrin, {CPI}); |
| 1869 | Builder.CreateStore(Code, SEHCodeSlotStack.back()); |
| 1870 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1871 | |
| 1872 | // Emit the __except body. |
| 1873 | EmitStmt(Except->getBlock()); |
| 1874 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1875 | // End the lifetime of the exception code. |
| 1876 | SEHCodeSlotStack.pop_back(); |
| 1877 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1878 | if (HaveInsertPoint()) |
| 1879 | Builder.CreateBr(ContBB); |
| 1880 | |
| 1881 | EmitBlock(ContBB); |
Reid Kleckner | 98592d9 | 2013-09-16 21:46:30 +0000 | [diff] [blame] | 1882 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1883 | |
| 1884 | void CodeGenFunction::EmitSEHLeaveStmt(const SEHLeaveStmt &S) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1885 | // If this code is reachable then emit a stop point (if generating |
| 1886 | // debug info). We have to do this ourselves because we are on the |
| 1887 | // "simple" statement path. |
| 1888 | if (HaveInsertPoint()) |
| 1889 | EmitStopPoint(&S); |
| 1890 | |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 1891 | // This must be a __leave from a __finally block, which we warn on and is UB. |
| 1892 | // Just emit unreachable. |
| 1893 | if (!isSEHTryScope()) { |
| 1894 | Builder.CreateUnreachable(); |
| 1895 | Builder.ClearInsertionPoint(); |
| 1896 | return; |
| 1897 | } |
| 1898 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1899 | EmitBranchThroughCleanup(*SEHTryEpilogueStack.back()); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1900 | } |