[K/N][codegen] Refactored getting types from runtime

This commit is contained in:
Igor Chevdar
2022-08-30 11:46:42 +03:00
committed by Space
parent 79d1ca0504
commit b8bf82114b
4 changed files with 28 additions and 22 deletions
@@ -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 importRtFunction(name: String) = importFunction(name, runtime.llvmModule)
private fun importRtGlobal(name: String) = importGlobal(name, runtime.llvmModule)
val allocInstanceFunction = importRtFunction("AllocInstance") val allocInstanceFunction = importRtFunction("AllocInstance")
val allocArrayFunction = importRtFunction("AllocArrayInstance") val allocArrayFunction = importRtFunction("AllocArrayInstance")
val initThreadLocalSingleton = importRtFunction("InitThreadLocalSingleton") val initThreadLocalSingleton = importRtFunction("InitThreadLocalSingleton")
@@ -19,9 +19,9 @@ class Runtime(bitcodeFile: String) {
val calculatedLLVMTypes: MutableMap<IrType, LLVMTypeRef> = HashMap() val calculatedLLVMTypes: MutableMap<IrType, LLVMTypeRef> = HashMap()
val addedLLVMExternalFunctions: MutableMap<IrFunction, LlvmCallable> = HashMap() val addedLLVMExternalFunctions: MutableMap<IrFunction, LlvmCallable> = HashMap()
internal fun getStructTypeOrNull(name: String) = LLVMGetTypeByName(llvmModule, "struct.$name") private fun getStructTypeOrNull(name: String) = LLVMGetTypeByName(llvmModule, "struct.$name")
internal fun getStructType(name: String) = getStructTypeOrNull(name) private fun getStructType(name: String) = getStructTypeOrNull(name)
?: throw Error("struct.$name is not found in the Runtime module.") ?: error("struct.$name is not found in the Runtime module.")
val typeInfoType = getStructType("TypeInfo") val typeInfoType = getStructType("TypeInfo")
val extendedTypeInfoType = getStructType("ExtendedTypeInfo") val extendedTypeInfoType = getStructType("ExtendedTypeInfo")
@@ -50,6 +50,19 @@ class Runtime(bitcodeFile: String) {
val kotlinToObjCMethodAdapter by lazy { getStructType("KotlinToObjCMethodAdapter") } val kotlinToObjCMethodAdapter by lazy { getStructType("KotlinToObjCMethodAdapter") }
val typeInfoObjCExportAddition by lazy { getStructType("TypeInfoObjCExportAddition") } 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 { val pointerSize: Int by lazy {
LLVMABISizeOfType(targetData, objHeaderPtrType).toInt() LLVMABISizeOfType(targetData, objHeaderPtrType).toInt()
} }
@@ -57,7 +57,7 @@ internal class ObjCDataGenerator(val codegen: CodeGenerator) {
global.pointer.bitcast(pointerType(int8TypePtr)) global.pointer.bitcast(pointerType(int8TypePtr))
} }
val classObjectType = codegen.runtime.getStructType("_class_t") private val classObjectType = codegen.runtime.objCClassObjectType
fun exportClass(name: String) { fun exportClass(name: String) {
context.llvm.usedGlobals += getClassGlobal(name, isMetaclass = false).llvm context.llvm.usedGlobals += getClassGlobal(name, isMetaclass = false).llvm
@@ -83,7 +83,7 @@ internal class ObjCDataGenerator(val codegen: CodeGenerator) {
private val emptyCache = constPointer( private val emptyCache = constPointer(
codegen.importGlobal( codegen.importGlobal(
"_objc_empty_cache", "_objc_empty_cache",
codegen.runtime.getStructType("_objc_cache"), codegen.runtime.objCCache,
CurrentKlibModuleOrigin CurrentKlibModuleOrigin
) )
) )
@@ -96,14 +96,13 @@ internal class ObjCDataGenerator(val codegen: CodeGenerator) {
fun emitClass(name: String, superName: String, instanceMethods: List<Method>) { fun emitClass(name: String, superName: String, instanceMethods: List<Method>) {
val runtime = context.llvm.runtime val runtime = context.llvm.runtime
fun struct(name: String) = runtime.getStructType(name)
val classRoType = struct("_class_ro_t") val classRoType = runtime.objCClassRoType
val methodType = struct("_objc_method") val methodType = runtime.objCMethodType
val methodListType = struct("__method_list_t") val methodListType = runtime.objCMethodListType
val protocolListType = struct("_objc_protocol_list") val protocolListType = runtime.objCProtocolListType
val ivarListType = struct("_ivar_list_t") val ivarListType = runtime.objCIVarListType
val propListType = struct("_prop_list_t") val propListType = runtime.objCPropListType
val classNameLiteral = classNames.get(name) val classNameLiteral = classNames.get(name)
@@ -123,8 +123,7 @@ private fun FunctionGenerationContext.loadBlockInvoke(
blockPtr: LLVMValueRef, blockPtr: LLVMValueRef,
bridge: BlockPointerBridge bridge: BlockPointerBridge
): LLVMValueRef { ): LLVMValueRef {
val blockLiteralType = codegen.runtime.getStructType("Block_literal_1") val invokePtr = structGep(bitcast(pointerType(codegen.runtime.blockLiteralType), blockPtr), 3)
val invokePtr = structGep(bitcast(pointerType(blockLiteralType), blockPtr), 3)
return bitcast(pointerType(bridge.blockType.blockInvokeLlvmType.llvmFunctionType), load(invokePtr)) 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 "" get() = numberOfParameters.toString() + if (returnsVoid) "V" else ""
internal class BlockGenerator(private val codegen: CodeGenerator) { internal class BlockGenerator(private val codegen: CodeGenerator) {
private val kRefSharedHolderType = LLVMGetTypeByName(codegen.runtime.llvmModule, "class.KRefSharedHolder")!!
private val blockLiteralType = structType( private val blockLiteralType = structType(
codegen.runtime.getStructType("Block_literal_1"), codegen.runtime.blockLiteralType,
kRefSharedHolderType codegen.runtime.kRefSharedHolderType
) )
private val blockDescriptorType = codegen.runtime.getStructType("Block_descriptor_1")
val disposeHelper = generateFunction( val disposeHelper = generateFunction(
codegen, codegen,
functionType(voidType, false, int8TypePtr), 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(0L),
codegen.context.LongInt(LLVMStoreSizeOfType(codegen.runtime.targetData, blockLiteralType)), codegen.context.LongInt(LLVMStoreSizeOfType(codegen.runtime.targetData, blockLiteralType)),
constPointer(copyHelper), constPointer(copyHelper),