From b8bf82114b5f1a8ff59b01a03e44427edadad855 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Tue, 30 Aug 2022 11:46:42 +0300 Subject: [PATCH] [K/N][codegen] Refactored getting types from runtime --- .../kotlin/backend/konan/llvm/ContextUtils.kt | 2 -- .../kotlin/backend/konan/llvm/Runtime.kt | 19 ++++++++++++++++--- .../konan/llvm/objc/ObjCDataGenerator.kt | 17 ++++++++--------- .../llvm/objcexport/BlockPointerSupport.kt | 12 ++++-------- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/ContextUtils.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/ContextUtils.kt index edf19feef5e..d84d9f03008 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/ContextUtils.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/ContextUtils.kt @@ -456,8 +456,6 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) : Runti private fun importRtFunction(name: String) = importFunction(name, runtime.llvmModule) - private fun importRtGlobal(name: String) = importGlobal(name, runtime.llvmModule) - val allocInstanceFunction = importRtFunction("AllocInstance") val allocArrayFunction = importRtFunction("AllocArrayInstance") val initThreadLocalSingleton = importRtFunction("InitThreadLocalSingleton") diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Runtime.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Runtime.kt index 8909309c6d1..9f2aa80f874 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Runtime.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Runtime.kt @@ -19,9 +19,9 @@ class Runtime(bitcodeFile: String) { val calculatedLLVMTypes: MutableMap = HashMap() val addedLLVMExternalFunctions: MutableMap = HashMap() - internal fun getStructTypeOrNull(name: String) = LLVMGetTypeByName(llvmModule, "struct.$name") - internal fun getStructType(name: String) = getStructTypeOrNull(name) - ?: throw Error("struct.$name is not found in the Runtime module.") + private fun getStructTypeOrNull(name: String) = LLVMGetTypeByName(llvmModule, "struct.$name") + private fun getStructType(name: String) = getStructTypeOrNull(name) + ?: error("struct.$name is not found in the Runtime module.") val typeInfoType = getStructType("TypeInfo") val extendedTypeInfoType = getStructType("ExtendedTypeInfo") @@ -50,6 +50,19 @@ class Runtime(bitcodeFile: String) { val kotlinToObjCMethodAdapter by lazy { getStructType("KotlinToObjCMethodAdapter") } val typeInfoObjCExportAddition by lazy { getStructType("TypeInfoObjCExportAddition") } + val objCClassObjectType by lazy { getStructType("_class_t") } + val objCCache by lazy { getStructType("_objc_cache") } + val objCClassRoType by lazy { getStructType("_class_ro_t") } + val objCMethodType by lazy { getStructType("_objc_method") } + val objCMethodListType by lazy { getStructType("__method_list_t") } + val objCProtocolListType by lazy { getStructType("_objc_protocol_list") } + val objCIVarListType by lazy { getStructType("_ivar_list_t") } + val objCPropListType by lazy { getStructType("_prop_list_t") } + + val kRefSharedHolderType by lazy { LLVMGetTypeByName(llvmModule, "class.KRefSharedHolder")!! } + val blockLiteralType by lazy { getStructType("Block_literal_1") } + val blockDescriptorType by lazy { getStructType("Block_descriptor_1") } + val pointerSize: Int by lazy { LLVMABISizeOfType(targetData, objHeaderPtrType).toInt() } diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objc/ObjCDataGenerator.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objc/ObjCDataGenerator.kt index c80ac89e36a..3cfe73665a5 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objc/ObjCDataGenerator.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objc/ObjCDataGenerator.kt @@ -57,7 +57,7 @@ internal class ObjCDataGenerator(val codegen: CodeGenerator) { global.pointer.bitcast(pointerType(int8TypePtr)) } - val classObjectType = codegen.runtime.getStructType("_class_t") + private val classObjectType = codegen.runtime.objCClassObjectType fun exportClass(name: String) { context.llvm.usedGlobals += getClassGlobal(name, isMetaclass = false).llvm @@ -83,7 +83,7 @@ internal class ObjCDataGenerator(val codegen: CodeGenerator) { private val emptyCache = constPointer( codegen.importGlobal( "_objc_empty_cache", - codegen.runtime.getStructType("_objc_cache"), + codegen.runtime.objCCache, CurrentKlibModuleOrigin ) ) @@ -96,14 +96,13 @@ internal class ObjCDataGenerator(val codegen: CodeGenerator) { fun emitClass(name: String, superName: String, instanceMethods: List) { val runtime = context.llvm.runtime - fun struct(name: String) = runtime.getStructType(name) - val classRoType = struct("_class_ro_t") - val methodType = struct("_objc_method") - val methodListType = struct("__method_list_t") - val protocolListType = struct("_objc_protocol_list") - val ivarListType = struct("_ivar_list_t") - val propListType = struct("_prop_list_t") + val classRoType = runtime.objCClassRoType + val methodType = runtime.objCMethodType + val methodListType = runtime.objCMethodListType + val protocolListType = runtime.objCProtocolListType + val ivarListType = runtime.objCIVarListType + val propListType = runtime.objCPropListType val classNameLiteral = classNames.get(name) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/BlockPointerSupport.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/BlockPointerSupport.kt index 6efdf0bdb94..ce1f888d5fe 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/BlockPointerSupport.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/BlockPointerSupport.kt @@ -123,8 +123,7 @@ private fun FunctionGenerationContext.loadBlockInvoke( blockPtr: LLVMValueRef, bridge: BlockPointerBridge ): LLVMValueRef { - val blockLiteralType = codegen.runtime.getStructType("Block_literal_1") - val invokePtr = structGep(bitcast(pointerType(blockLiteralType), blockPtr), 3) + val invokePtr = structGep(bitcast(pointerType(codegen.runtime.blockLiteralType), blockPtr), 3) return bitcast(pointerType(bridge.blockType.blockInvokeLlvmType.llvmFunctionType), load(invokePtr)) } @@ -157,15 +156,12 @@ private val BlockPointerBridge.nameSuffix: String get() = numberOfParameters.toString() + if (returnsVoid) "V" else "" internal class BlockGenerator(private val codegen: CodeGenerator) { - private val kRefSharedHolderType = LLVMGetTypeByName(codegen.runtime.llvmModule, "class.KRefSharedHolder")!! private val blockLiteralType = structType( - codegen.runtime.getStructType("Block_literal_1"), - kRefSharedHolderType + codegen.runtime.blockLiteralType, + codegen.runtime.kRefSharedHolderType ) - private val blockDescriptorType = codegen.runtime.getStructType("Block_descriptor_1") - val disposeHelper = generateFunction( codegen, functionType(voidType, false, int8TypePtr), @@ -234,7 +230,7 @@ internal class BlockGenerator(private val codegen: CodeGenerator) { } } - return Struct(blockDescriptorType, + return Struct(codegen.runtime.blockDescriptorType, codegen.context.LongInt(0L), codegen.context.LongInt(LLVMStoreSizeOfType(codegen.runtime.targetData, blockLiteralType)), constPointer(copyHelper),