From 55632f5df2f830cb2a2cdb9d77824ce32eddf6c9 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Fri, 19 May 2023 17:49:46 +0300 Subject: [PATCH] [K/N][runtime] Used internal isInstance --- .../kotlin/backend/konan/cexport/CAdapterApiExporter.kt | 4 ++-- kotlin-native/llvmDebugInfoC/src/scripts/konan_lldb.py | 4 ++-- kotlin-native/runtime/src/debug/cpp/KDebug.cpp | 4 ++++ kotlin-native/runtime/src/legacymm/cpp/Weak.cpp | 2 +- kotlin-native/runtime/src/main/cpp/Exceptions.cpp | 2 +- kotlin-native/runtime/src/main/cpp/KDebug.h | 3 +++ kotlin-native/runtime/src/main/cpp/ObjCExportErrors.mm | 9 ++++++--- kotlin-native/runtime/src/main/cpp/Types.cpp | 9 +++++++-- kotlin-native/runtime/src/main/cpp/Types.h | 3 ++- kotlin-native/runtime/src/mm/cpp/Memory.cpp | 2 +- 10 files changed, 29 insertions(+), 13 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cexport/CAdapterApiExporter.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cexport/CAdapterApiExporter.kt index 11058352f8458..dec59d9d36e83 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cexport/CAdapterApiExporter.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cexport/CAdapterApiExporter.kt @@ -273,7 +273,7 @@ internal class CAdapterApiExporter( |KObjHeader* DerefStablePointer(void*, KObjHeader**) RUNTIME_NOTHROW; |void* CreateStablePointer(KObjHeader*) RUNTIME_NOTHROW; |void DisposeStablePointer(void*) RUNTIME_NOTHROW; - |${prefix}_KBoolean IsInstance(const KObjHeader*, const KTypeInfo*) RUNTIME_NOTHROW; + |${prefix}_KBoolean IsInstanceInternal(const KObjHeader*, const KTypeInfo*) RUNTIME_NOTHROW; |void EnterFrame(KObjHeader** start, int parameters, int count) RUNTIME_NOTHROW; |void LeaveFrame(KObjHeader** start, int parameters, int count) RUNTIME_NOTHROW; |void SetCurrentFrame(KObjHeader** start) RUNTIME_NOTHROW; @@ -338,7 +338,7 @@ internal class CAdapterApiExporter( | Kotlin_initRuntimeIfNeeded(); | ScopedRunnableState stateGuard; | KObjHolder holder; - | return IsInstance(DerefStablePointer(ref, holder.slot()), (const KTypeInfo*)type); + | return IsInstanceInternal(DerefStablePointer(ref, holder.slot()), (const KTypeInfo*)type); |} """.trimMargin()) predefinedTypes.forEach { diff --git a/kotlin-native/llvmDebugInfoC/src/scripts/konan_lldb.py b/kotlin-native/llvmDebugInfoC/src/scripts/konan_lldb.py index e688c2d397f1a..b1ae942ee6ac1 100644 --- a/kotlin-native/llvmDebugInfoC/src/scripts/konan_lldb.py +++ b/kotlin-native/llvmDebugInfoC/src/scripts/konan_lldb.py @@ -93,11 +93,11 @@ def _type_info_by_address(address, debugger = lldb.debugger): return candidates def is_instance_of(addr, typeinfo): - return evaluate("(bool)IsInstance({:#x}, {:#x})".format(addr, typeinfo)).GetValue() == "true" + return evaluate("(bool)Konan_DebugIsInstance({:#x}, {:#x})".format(addr, typeinfo)).GetValue() == "true" def is_string_or_array(value): start = time.monotonic() - soa = evaluate("(int)IsInstance({0:#x}, {1:#x}) ? 1 : ((int)Konan_DebugIsArray({0:#x})) ? 2 : 0)" + soa = evaluate("(int)Konan_DebugIsInstance({0:#x}, {1:#x}) ? 1 : ((int)Konan_DebugIsArray({0:#x})) ? 2 : 0)" .format(value.unsigned, _symbol_loaded_address('kclass:kotlin.String'))).unsigned log(lambda: "is_string_or_array:{:#x}:{}".format(value.unsigned, soa)) bench(start, lambda: "is_string_or_array({:#x}) = {}".format(value.unsigned, soa)) diff --git a/kotlin-native/runtime/src/debug/cpp/KDebug.cpp b/kotlin-native/runtime/src/debug/cpp/KDebug.cpp index a4ceb765d50c8..946d39971bfa3 100644 --- a/kotlin-native/runtime/src/debug/cpp/KDebug.cpp +++ b/kotlin-native/runtime/src/debug/cpp/KDebug.cpp @@ -273,6 +273,10 @@ RUNTIME_USED RUNTIME_WEAK int32_t Konan_DebugIsArray(KRef obj) { return impl(obj); } +RUNTIME_USED RUNTIME_WEAK int32_t Konan_DebugIsInstance(KRef obj, const TypeInfo* typeInfo) { + return IsInstanceInternal(obj, typeInfo); +} + RUNTIME_USED RUNTIME_WEAK int32_t Konan_DebugGetFieldCount(KRef obj) { auto* impl = getImpl(obj, DO_DebugGetFieldCount); if (impl == nullptr) return 0; diff --git a/kotlin-native/runtime/src/legacymm/cpp/Weak.cpp b/kotlin-native/runtime/src/legacymm/cpp/Weak.cpp index 0b538f1e7529c..82eaf375dd03b 100644 --- a/kotlin-native/runtime/src/legacymm/cpp/Weak.cpp +++ b/kotlin-native/runtime/src/legacymm/cpp/Weak.cpp @@ -63,7 +63,7 @@ OBJ_GETTER(Konan_getWeakReferenceImpl, ObjHeader* referred) { } #if KONAN_OBJC_INTEROP - if (IsInstance(referred, theObjCObjectWrapperTypeInfo)) { + if (IsInstanceInternal(referred, theObjCObjectWrapperTypeInfo)) { RETURN_RESULT_OF(makeObjCWeakReferenceImpl, referred->GetAssociatedObject()); } #endif // KONAN_OBJC_INTEROP diff --git a/kotlin-native/runtime/src/main/cpp/Exceptions.cpp b/kotlin-native/runtime/src/main/cpp/Exceptions.cpp index 93b5042c3bdfd..01fcd765f43fb 100644 --- a/kotlin-native/runtime/src/main/cpp/Exceptions.cpp +++ b/kotlin-native/runtime/src/main/cpp/Exceptions.cpp @@ -36,7 +36,7 @@ extern "C" void Kotlin_runUnhandledExceptionHook(KRef exception); extern "C" void ReportUnhandledException(KRef exception); void ThrowException(KRef exception) { - RuntimeAssert(exception != nullptr && IsInstance(exception, theThrowableTypeInfo), + RuntimeAssert(exception != nullptr && IsInstanceInternal(exception, theThrowableTypeInfo), "Throwing something non-throwable"); #if KONAN_NO_EXCEPTIONS PrintThrowable(exception); diff --git a/kotlin-native/runtime/src/main/cpp/KDebug.h b/kotlin-native/runtime/src/main/cpp/KDebug.h index ddf69b952bb0f..a2faaa226b56c 100644 --- a/kotlin-native/runtime/src/main/cpp/KDebug.h +++ b/kotlin-native/runtime/src/main/cpp/KDebug.h @@ -59,6 +59,9 @@ int32_t Konan_DebugPrint(KRef obj); RUNTIME_USED RUNTIME_WEAK int32_t Konan_DebugIsArray(KRef obj); +RUNTIME_USED RUNTIME_WEAK +int32_t Konan_DebugIsInstance(KRef obj, const TypeInfo* typeInfo); + // Returns number of fields in an objects, or elements in an array. RUNTIME_USED RUNTIME_WEAK int32_t Konan_DebugGetFieldCount(KRef obj); diff --git a/kotlin-native/runtime/src/main/cpp/ObjCExportErrors.mm b/kotlin-native/runtime/src/main/cpp/ObjCExportErrors.mm index 2f9c93462470d..b3f0dab450c21 100644 --- a/kotlin-native/runtime/src/main/cpp/ObjCExportErrors.mm +++ b/kotlin-native/runtime/src/main/cpp/ObjCExportErrors.mm @@ -48,9 +48,12 @@ static void printlnMessage(const char* message) { static char kotlinExceptionOriginChar; static bool isExceptionOfType(KRef exception, const TypeInfo** types) { - if (types) for (int i = 0; types[i] != nullptr; ++i) { - // TODO: use fast instance check when possible. - if (IsInstance(exception, types[i])) return true; + if (types) { + const TypeInfo* type = exception->type_info(); + for (int i = 0; types[i] != nullptr; ++i) { + // TODO: use fast instance check when possible. + if (IsSubtype(type, types[i])) return true; + } } return false; diff --git a/kotlin-native/runtime/src/main/cpp/Types.cpp b/kotlin-native/runtime/src/main/cpp/Types.cpp index 7de0518d9141e..e0a5e60db178f 100644 --- a/kotlin-native/runtime/src/main/cpp/Types.cpp +++ b/kotlin-native/runtime/src/main/cpp/Types.cpp @@ -8,7 +8,12 @@ extern "C" { -KBoolean IsInstance(const ObjHeader* obj, const TypeInfo* type_info) { +// Note: keeping it for compatibility with external tools only, will be deprecated and removed in the future. +RUNTIME_PURE RUNTIME_USED RUNTIME_WEAK KBoolean IsInstance(const ObjHeader* obj, const TypeInfo* type_info) { + return IsInstanceInternal(obj, type_info); +} + +KBoolean IsInstanceInternal(const ObjHeader* obj, const TypeInfo* type_info) { // We assume null check is handled by caller. RuntimeAssert(obj != nullptr, "must not be null"); const TypeInfo* obj_type_info = obj->type_info(); @@ -42,7 +47,7 @@ KBoolean IsArray(KConstRef obj) { } KBoolean Kotlin_TypeInfo_isInstance(KConstRef obj, KNativePtr typeInfo) { - return IsInstance(obj, reinterpret_cast(typeInfo)); + return IsInstanceInternal(obj, reinterpret_cast(typeInfo)); } OBJ_GETTER(Kotlin_TypeInfo_getPackageName, KNativePtr typeInfo, KBoolean checkFlags) { diff --git a/kotlin-native/runtime/src/main/cpp/Types.h b/kotlin-native/runtime/src/main/cpp/Types.h index 5d74127188e26..5fa2ce39e61a3 100644 --- a/kotlin-native/runtime/src/main/cpp/Types.h +++ b/kotlin-native/runtime/src/main/cpp/Types.h @@ -72,7 +72,8 @@ extern const TypeInfo* theWorkerBoundReferenceTypeInfo; extern const TypeInfo* theCleanerImplTypeInfo; extern const TypeInfo* theRegularWeakReferenceImplTypeInfo; -KBoolean IsInstance(const ObjHeader* obj, const TypeInfo* type_info) RUNTIME_PURE RUNTIME_USED; // Used in konan_lldb.py +KBoolean IsInstance(const ObjHeader* obj, const TypeInfo* type_info) RUNTIME_PURE RUNTIME_USED RUNTIME_WEAK; +KBoolean IsInstanceInternal(const ObjHeader* obj, const TypeInfo* type_info) RUNTIME_PURE; KBoolean IsSubtype(const TypeInfo* obj_type_info, const TypeInfo* type_info) RUNTIME_PURE; KBoolean IsSubclassFast(const TypeInfo* obj_type_info, int32_t lo, int32_t hi) RUNTIME_PURE; void CheckCast(const ObjHeader* obj, const TypeInfo* type_info); diff --git a/kotlin-native/runtime/src/mm/cpp/Memory.cpp b/kotlin-native/runtime/src/mm/cpp/Memory.cpp index ee26240342622..fb68843403e6f 100644 --- a/kotlin-native/runtime/src/mm/cpp/Memory.cpp +++ b/kotlin-native/runtime/src/mm/cpp/Memory.cpp @@ -608,7 +608,7 @@ RUNTIME_NOTHROW extern "C" OBJ_GETTER(Konan_getWeakReferenceImpl, ObjHeader* ref RETURN_RESULT_OF(makePermanentWeakReferenceImpl, referred); } #if KONAN_OBJC_INTEROP - if (IsInstance(referred, theObjCObjectWrapperTypeInfo)) { + if (IsInstanceInternal(referred, theObjCObjectWrapperTypeInfo)) { RETURN_RESULT_OF(makeObjCWeakReferenceImpl, referred->GetAssociatedObject()); } #endif // KONAN_OBJC_INTEROP