From f7a1fa86a983c60fd3248eafc42d533ce21066f0 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Thu, 13 Jan 2022 13:56:01 +0300 Subject: [PATCH] ObjCExport: introduce terminatingExceptionHandler --- .../kotlin/backend/konan/llvm/CodeGenerator.kt | 13 +++++++++---- .../llvm/objcexport/ObjCExportCodeGenerator.kt | 12 ++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt index a3eb6454adf..e7ebe0e3718 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt @@ -432,7 +432,7 @@ internal abstract class FunctionGenerationContext( val function: LLVMValueRef, val codegen: CodeGenerator, private val startLocation: LocationInfo?, - private val endLocation: LocationInfo?, + protected val endLocation: LocationInfo?, switchToRunnable: Boolean, internal val irFunction: IrFunction? = null ) : ContextUtils { @@ -980,9 +980,7 @@ internal abstract class FunctionGenerationContext( } appendingTo(fatalForeignExceptionBlock) { - val exceptionRecord = extractValue(landingpad, 0) - call(context.llvm.cxaBeginCatchFunction, listOf(exceptionRecord)) - terminate() + terminateWithCurrentException(landingpad) } } @@ -993,6 +991,13 @@ internal abstract class FunctionGenerationContext( } } + fun terminateWithCurrentException(landingpad: LLVMValueRef) { + val exceptionRecord = extractValue(landingpad, 0) + // So `std::terminate` is called from C++ catch block: + call(context.llvm.cxaBeginCatchFunction, listOf(exceptionRecord)) + terminate() + } + fun terminate() { call(context.llvm.cxxStdTerminate, emptyList()) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/ObjCExportCodeGenerator.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/ObjCExportCodeGenerator.kt index 4d27d778050..9a22c428c66 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/ObjCExportCodeGenerator.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/ObjCExportCodeGenerator.kt @@ -96,6 +96,18 @@ internal class ObjCExportFunctionGenerationContext( override fun processReturns() { // Do nothing. } + + val terminatingExceptionHandler = object : ExceptionHandler.Local() { + override val unwind: LLVMBasicBlockRef by lazy { + val result = basicBlockInFunction("fatal_landingpad", endLocation) + appendingTo(result) { + val landingpad = gxxLandingpad(0) + LLVMSetCleanup(landingpad, 1) + terminateWithCurrentException(landingpad) + } + result + } + } } internal class ObjCExportFunctionGenerationContextBuilder(