[K/N] Small refactoring (review fixes)
This commit is contained in:
+2
-2
@@ -469,7 +469,7 @@ private class ExportedElement(val kind: ElementKind,
|
||||
}
|
||||
builder.append(" } catch (...) {")
|
||||
builder.append(" SetCurrentFrame(reinterpret_cast<KObjHeader**>(frame));\n")
|
||||
builder.append(" HandleCurrentExceptionForCInterop();\n")
|
||||
builder.append(" HandleCurrentExceptionWhenLeavingKotlinCode();\n")
|
||||
builder.append(" } \n")
|
||||
|
||||
builder.append("}\n")
|
||||
@@ -936,7 +936,7 @@ internal class CAdapterGenerator(val context: Context) : DeclarationDescriptorVi
|
||||
|void Kotlin_initRuntimeIfNeeded();
|
||||
|void Kotlin_mm_switchThreadStateRunnable() RUNTIME_NOTHROW;
|
||||
|void Kotlin_mm_switchThreadStateNative() RUNTIME_NOTHROW;
|
||||
|void HandleCurrentExceptionForCInterop();
|
||||
|void HandleCurrentExceptionWhenLeavingKotlinCode();
|
||||
|
|
||||
|KObjHeader* CreateStringFromCString(const char*, KObjHeader**);
|
||||
|char* CreateCStringFromString(const KObjHeader*);
|
||||
|
||||
+6
-5
@@ -475,10 +475,12 @@ internal abstract class FunctionGenerationContext(
|
||||
private val entryBb = basicBlockInFunction("entry", startLocation)
|
||||
protected val cleanupLandingpad = basicBlockInFunction("cleanup_landingpad", endLocation)
|
||||
|
||||
protected open val needLeaveFrameInUnwindEpilogue: Boolean
|
||||
// Functions that can be exported and called not only from Kotlin code should have `LeaveFrame`
|
||||
// because there is no guarantee of catching Kotlin exception in Kotlin code.
|
||||
protected open val needLeaveFrame: Boolean
|
||||
get() = irFunction?.annotations?.hasAnnotation(RuntimeNames.exportForCppRuntime) == true
|
||||
|| forwardingForeignExceptionsTerminatedWith != null
|
||||
|| irFunction?.origin == CBridgeOrigin.C_TO_KOTLIN_BRIDGE
|
||||
|
||||
private var setCurrentFrameIsCalled: Boolean = false
|
||||
|
||||
val stackLocalsManager = StackLocalsManagerImpl(this, stackLocalsInitBb)
|
||||
@@ -1051,7 +1053,6 @@ internal abstract class FunctionGenerationContext(
|
||||
}
|
||||
|
||||
fun generateFrameCheck() {
|
||||
assert(slotsPhi != null)
|
||||
call(context.llvm.checkCurrentFrameFunction, listOf(slotsPhi!!))
|
||||
}
|
||||
|
||||
@@ -1426,7 +1427,7 @@ internal abstract class FunctionGenerationContext(
|
||||
}
|
||||
|
||||
internal fun epilogue() {
|
||||
val needLeaveFrameInUnwindEpilogue = this.needLeaveFrameInUnwindEpilogue
|
||||
val needLeaveFrameInUnwindEpilogue = this.needLeaveFrame
|
||||
|
||||
appendingTo(prologueBb) {
|
||||
val slots = if (needSlotsPhi || needLeaveFrameInUnwindEpilogue)
|
||||
@@ -1715,7 +1716,7 @@ internal abstract class FunctionGenerationContext(
|
||||
}
|
||||
|
||||
private fun releaseVars() {
|
||||
if (needLeaveFrameInUnwindEpilogue || needSlots) {
|
||||
if (needLeaveFrame || needSlots) {
|
||||
check(!forbidRuntime) { "Attempt to leave a frame where runtime usage is forbidden" }
|
||||
call(context.llvm.leaveFrameFunction,
|
||||
listOf(slotsPhi!!, Int32(vars.skipSlots).llvm, Int32(slotCount).llvm))
|
||||
|
||||
+3
-1
@@ -53,7 +53,9 @@ internal class ObjCExportFunctionGenerationContext(
|
||||
) : FunctionGenerationContext(builder) {
|
||||
private val objCExportCodegen = builder.objCExportCodegen
|
||||
|
||||
override val needLeaveFrameInUnwindEpilogue: Boolean
|
||||
// All generated bridges by ObjCExport should have `LeaveFrame`
|
||||
// because there is no guarantee of catching Kotlin exception in Kotlin code.
|
||||
override val needLeaveFrame: Boolean
|
||||
get() = true
|
||||
|
||||
// Note: we could generate single "epilogue" and make all [ret]s just branch to it (like [DefaultFunctionGenerationContext]),
|
||||
|
||||
@@ -45,13 +45,11 @@ void ThrowException(KRef exception) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void HandleCurrentExceptionForCInterop() {
|
||||
void HandleCurrentExceptionWhenLeavingKotlinCode() {
|
||||
try {
|
||||
std::rethrow_exception(std::current_exception());
|
||||
} catch (ExceptionObjHolder& e) {
|
||||
std::terminate(); // Terminate when it's a kotlin exception.
|
||||
} catch (...) {
|
||||
throw; // Just rethrow if it's unknown.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ void ThrowException(KRef exception);
|
||||
|
||||
void SetKonanTerminateHandler();
|
||||
|
||||
void HandleCurrentExceptionForCInterop();
|
||||
void HandleCurrentExceptionWhenLeavingKotlinCode();
|
||||
|
||||
RUNTIME_NOTHROW OBJ_GETTER(Kotlin_getExceptionObject, void* holder);
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
#define RUNTIME_MEMORY_H
|
||||
|
||||
#include <utility>
|
||||
#include <functional>
|
||||
|
||||
#include "KAssert.h"
|
||||
#include "Common.h"
|
||||
|
||||
@@ -1080,7 +1080,7 @@ JobKind Worker::processQueueElement(bool blocking) {
|
||||
#if !KONAN_NO_EXCEPTIONS
|
||||
FrameOverlay* currentFrame = getCurrentFrame();
|
||||
#else
|
||||
RuntimeFail("Exceptions aren't supported!\n");
|
||||
#error "Exceptions aren't supported!"
|
||||
#endif
|
||||
try {
|
||||
#if KONAN_OBJC_INTEROP
|
||||
|
||||
@@ -45,6 +45,6 @@ FrameOverlay* mm::ShadowStack::getCurrentFrame() noexcept {
|
||||
return currentFrame_;
|
||||
}
|
||||
|
||||
void mm::ShadowStack::checkCurrentFrame(FrameOverlay* frame) noexcept {
|
||||
ALWAYS_INLINE void mm::ShadowStack::checkCurrentFrame(FrameOverlay* frame) noexcept {
|
||||
RuntimeAssert(currentFrame_ == frame, "Frame is expected to be %p, but current frame is %p", frame, currentFrame_);
|
||||
}
|
||||
@@ -55,7 +55,7 @@ public:
|
||||
void LeaveFrame(ObjHeader** start, int parameters, int count) noexcept;
|
||||
void SetCurrentFrame(ObjHeader** start) noexcept;
|
||||
FrameOverlay* getCurrentFrame() noexcept;
|
||||
void checkCurrentFrame(FrameOverlay* frame) noexcept;
|
||||
ALWAYS_INLINE void checkCurrentFrame(FrameOverlay* frame) noexcept;
|
||||
|
||||
Iterator begin() noexcept { return Iterator(currentFrame_); }
|
||||
Iterator end() noexcept { return Iterator(nullptr); }
|
||||
|
||||
Reference in New Issue
Block a user