Fix spaces.
This commit is contained in:
+124
-124
@@ -15,167 +15,167 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny
|
|||||||
|
|
||||||
internal class RTTIGenerator(override val context: Context) : ContextUtils {
|
internal class RTTIGenerator(override val context: Context) : ContextUtils {
|
||||||
|
|
||||||
val runtime: Runtime
|
val runtime: Runtime
|
||||||
get() = context.runtime
|
get() = context.runtime
|
||||||
|
|
||||||
|
|
||||||
private inner class FieldTableRecord(val nameSignature: Long, val fieldOffset: Int) :
|
private inner class FieldTableRecord(val nameSignature: Long, val fieldOffset: Int) :
|
||||||
Struct(runtime.fieldTableRecordType, Int64(nameSignature), Int32(fieldOffset))
|
Struct(runtime.fieldTableRecordType, Int64(nameSignature), Int32(fieldOffset))
|
||||||
|
|
||||||
private inner class MethodTableRecord(val nameSignature: Long, val methodEntryPoint: CompileTimeValue) :
|
private inner class MethodTableRecord(val nameSignature: Long, val methodEntryPoint: CompileTimeValue) :
|
||||||
Struct(runtime.methodTableRecordType, Int64(nameSignature), methodEntryPoint)
|
Struct(runtime.methodTableRecordType, Int64(nameSignature), methodEntryPoint)
|
||||||
|
|
||||||
private inner class TypeInfo(val name: Long, val size: Int,
|
private inner class TypeInfo(val name: Long, val size: Int,
|
||||||
val superType: CompileTimeValue,
|
val superType: CompileTimeValue,
|
||||||
val objOffsets: CompileTimeValue,
|
val objOffsets: CompileTimeValue,
|
||||||
val objOffsetsCount: Int,
|
val objOffsetsCount: Int,
|
||||||
val interfaces: CompileTimeValue,
|
val interfaces: CompileTimeValue,
|
||||||
val interfacesCount: Int,
|
val interfacesCount: Int,
|
||||||
val vtable: CompileTimeValue,
|
val vtable: CompileTimeValue,
|
||||||
val methods: CompileTimeValue,
|
val methods: CompileTimeValue,
|
||||||
val methodsCount: Int,
|
val methodsCount: Int,
|
||||||
val fields: CompileTimeValue,
|
val fields: CompileTimeValue,
|
||||||
val fieldsCount: Int) :
|
val fieldsCount: Int) :
|
||||||
Struct(
|
Struct(
|
||||||
runtime.typeInfoType,
|
runtime.typeInfoType,
|
||||||
|
|
||||||
Struct(runtime.globalhHashType, ConstArray(LLVMInt8Type(), Array(20, {i -> Int8(1)}).toList())),
|
Struct(runtime.globalhHashType, ConstArray(LLVMInt8Type(), Array(20, {i -> Int8(1)}).toList())),
|
||||||
Int32(size),
|
Int32(size),
|
||||||
|
|
||||||
superType,
|
superType,
|
||||||
|
|
||||||
objOffsets,
|
objOffsets,
|
||||||
Int32(objOffsetsCount),
|
Int32(objOffsetsCount),
|
||||||
|
|
||||||
interfaces,
|
interfaces,
|
||||||
Int32(interfacesCount),
|
Int32(interfacesCount),
|
||||||
|
|
||||||
vtable,
|
vtable,
|
||||||
|
|
||||||
methods,
|
methods,
|
||||||
Int32(methodsCount),
|
Int32(methodsCount),
|
||||||
|
|
||||||
fields,
|
fields,
|
||||||
Int32(fieldsCount)
|
Int32(fieldsCount)
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: probably it should be moved out of this class and shared.
|
// TODO: probably it should be moved out of this class and shared.
|
||||||
private fun createStructFor(className: FqName, fields: List<PropertyDescriptor>): LLVMOpaqueType? {
|
private fun createStructFor(className: FqName, fields: List<PropertyDescriptor>): LLVMOpaqueType? {
|
||||||
val classType = LLVMStructCreateNamed(LLVMGetModuleContext(context.llvmModule), "kclass:" + className)
|
val classType = LLVMStructCreateNamed(LLVMGetModuleContext(context.llvmModule), "kclass:" + className)
|
||||||
val fieldTypes = fields.map { getLLVMType(it.returnType!!) }.toTypedArray()
|
val fieldTypes = fields.map { getLLVMType(it.returnType!!) }.toTypedArray()
|
||||||
val fieldTypesNativeArrayPtr = if (fieldTypes.size > 0) {
|
val fieldTypesNativeArrayPtr = if (fieldTypes.size > 0) {
|
||||||
mallocNativeArrayOf(LLVMOpaqueType, *fieldTypes)[0] // TODO: dispose
|
mallocNativeArrayOf(LLVMOpaqueType, *fieldTypes)[0] // TODO: dispose
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
||||||
LLVMStructSetBody(classType, fieldTypesNativeArrayPtr, fieldTypes.size, 0)
|
LLVMStructSetBody(classType, fieldTypesNativeArrayPtr, fieldTypes.size, 0)
|
||||||
return classType
|
return classType
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: optimize
|
// TODO: optimize
|
||||||
private fun getVtableEntries(classDesc: ClassDescriptor): List<FunctionDescriptor> {
|
private fun getVtableEntries(classDesc: ClassDescriptor): List<FunctionDescriptor> {
|
||||||
val superVtableEntries = if (KotlinBuiltIns.isAny(classDesc)) {
|
val superVtableEntries = if (KotlinBuiltIns.isAny(classDesc)) {
|
||||||
emptyList()
|
emptyList()
|
||||||
} else {
|
} else {
|
||||||
getVtableEntries(classDesc.getSuperClassOrAny())
|
getVtableEntries(classDesc.getSuperClassOrAny())
|
||||||
}
|
}
|
||||||
|
|
||||||
val inheritedVtableSlots = Array<FunctionDescriptor?>(superVtableEntries.size, { null })
|
val inheritedVtableSlots = Array<FunctionDescriptor?>(superVtableEntries.size, { null })
|
||||||
val methods = getMethodTableEntries(classDesc) // TODO: ensure order is well-defined
|
val methods = getMethodTableEntries(classDesc) // TODO: ensure order is well-defined
|
||||||
val entriesForNewVtableSlots = methods.toMutableSet()
|
val entriesForNewVtableSlots = methods.toMutableSet()
|
||||||
|
|
||||||
methods.forEach { method ->
|
methods.forEach { method ->
|
||||||
superVtableEntries.forEachIndexed { i, superMethod ->
|
superVtableEntries.forEachIndexed { i, superMethod ->
|
||||||
if (OverridingUtil.overrides(method, superMethod)) {
|
if (OverridingUtil.overrides(method, superMethod)) {
|
||||||
assert (inheritedVtableSlots[i] == null)
|
assert (inheritedVtableSlots[i] == null)
|
||||||
inheritedVtableSlots[i] = method
|
inheritedVtableSlots[i] = method
|
||||||
|
|
||||||
assert (method in entriesForNewVtableSlots)
|
assert (method in entriesForNewVtableSlots)
|
||||||
entriesForNewVtableSlots.remove(method)
|
entriesForNewVtableSlots.remove(method)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return inheritedVtableSlots.map { it!! } + methods.filter { it in entriesForNewVtableSlots }
|
return inheritedVtableSlots.map { it!! } + methods.filter { it in entriesForNewVtableSlots }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getMethodTableEntries(classDesc: ClassDescriptor): List<FunctionDescriptor> {
|
private fun getMethodTableEntries(classDesc: ClassDescriptor): List<FunctionDescriptor> {
|
||||||
val contributedDescriptors = classDesc.unsubstitutedMemberScope.getContributedDescriptors()
|
val contributedDescriptors = classDesc.unsubstitutedMemberScope.getContributedDescriptors()
|
||||||
// (includes declarations from supers)
|
// (includes declarations from supers)
|
||||||
|
|
||||||
val functions = contributedDescriptors.filterIsInstance<FunctionDescriptor>()
|
val functions = contributedDescriptors.filterIsInstance<FunctionDescriptor>()
|
||||||
|
|
||||||
val properties = contributedDescriptors.filterIsInstance<PropertyDescriptor>()
|
val properties = contributedDescriptors.filterIsInstance<PropertyDescriptor>()
|
||||||
val getters = properties.mapNotNull { it.getter }
|
val getters = properties.mapNotNull { it.getter }
|
||||||
val setters = properties.mapNotNull { it.setter }
|
val setters = properties.mapNotNull { it.setter }
|
||||||
|
|
||||||
val allMethods = functions + getters + setters
|
val allMethods = functions + getters + setters
|
||||||
|
|
||||||
// TODO: adding or removing 'open' modifier will break the binary compatibility
|
// TODO: adding or removing 'open' modifier will break the binary compatibility
|
||||||
return allMethods.filter { it.modality != Modality.FINAL }
|
return allMethods.filter { it.modality != Modality.FINAL }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun generate(classDesc: ClassDescriptor) {
|
fun generate(classDesc: ClassDescriptor) {
|
||||||
|
|
||||||
val className = classDesc.fqNameSafe
|
val className = classDesc.fqNameSafe
|
||||||
|
|
||||||
val classType = createStructFor(className, classDesc.fields)
|
val classType = createStructFor(className, classDesc.fields)
|
||||||
|
|
||||||
val name = className.nameHash
|
val name = className.nameHash
|
||||||
|
|
||||||
val size = LLVMStoreSizeOfType(runtime.targetData, classType).toInt()
|
val size = LLVMStoreSizeOfType(runtime.targetData, classType).toInt()
|
||||||
|
|
||||||
val superType = classDesc.getSuperClassOrAny().llvmTypeInfoPtr
|
val superType = classDesc.getSuperClassOrAny().llvmTypeInfoPtr
|
||||||
|
|
||||||
val interfaces = classDesc.implementedInterfaces.map { it.llvmTypeInfoPtr }
|
val interfaces = classDesc.implementedInterfaces.map { it.llvmTypeInfoPtr }
|
||||||
val interfacesPtr = addGlobalConstArray("kintf:$className", pointerType(runtime.typeInfoType), interfaces)
|
val interfacesPtr = addGlobalConstArray("kintf:$className", pointerType(runtime.typeInfoType), interfaces)
|
||||||
|
|
||||||
val refFieldIndices = classDesc.fields.mapIndexedNotNull { index, field ->
|
val refFieldIndices = classDesc.fields.mapIndexedNotNull { index, field ->
|
||||||
val type = field.returnType!!
|
val type = field.returnType!!
|
||||||
if (!KotlinBuiltIns.isPrimitiveType(type)) {
|
if (!KotlinBuiltIns.isPrimitiveType(type)) {
|
||||||
index
|
index
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO: reuse offsets obtained for 'fields' below
|
// TODO: reuse offsets obtained for 'fields' below
|
||||||
val objOffsets = refFieldIndices.map { LLVMOffsetOfElement(runtime.targetData, classType, it) }
|
val objOffsets = refFieldIndices.map { LLVMOffsetOfElement(runtime.targetData, classType, it) }
|
||||||
val objOffsetsPtr = addGlobalConstArray("krefs:$className", int32Type, objOffsets.map { Int32(it.toInt()) })
|
val objOffsetsPtr = addGlobalConstArray("krefs:$className", int32Type, objOffsets.map { Int32(it.toInt()) })
|
||||||
|
|
||||||
val fields = classDesc.fields.mapIndexed { index, field ->
|
val fields = classDesc.fields.mapIndexed { index, field ->
|
||||||
// Note: using FQ name because a class may have multiple fields with the same name due to property overriding
|
// Note: using FQ name because a class may have multiple fields with the same name due to property overriding
|
||||||
val nameSignature = field.fqNameSafe.nameHash // FIXME: add signature
|
val nameSignature = field.fqNameSafe.nameHash // FIXME: add signature
|
||||||
val fieldOffset = LLVMOffsetOfElement(runtime.targetData, classType, index)
|
val fieldOffset = LLVMOffsetOfElement(runtime.targetData, classType, index)
|
||||||
FieldTableRecord(nameSignature, fieldOffset.toInt())
|
FieldTableRecord(nameSignature, fieldOffset.toInt())
|
||||||
}.sortedBy { it.nameSignature }
|
}.sortedBy { it.nameSignature }
|
||||||
|
|
||||||
val fieldsPtr = addGlobalConstArray("kfields:$className", runtime.fieldTableRecordType, fields)
|
val fieldsPtr = addGlobalConstArray("kfields:$className", runtime.fieldTableRecordType, fields)
|
||||||
|
|
||||||
// TODO: compile-time resolution limits binary compatibility
|
// TODO: compile-time resolution limits binary compatibility
|
||||||
val vtable = getVtableEntries(classDesc).map { it.implementation.entryPointAddress }
|
val vtable = getVtableEntries(classDesc).map { it.implementation.entryPointAddress }
|
||||||
val vtablePtr = addGlobalConstArray("kvtable:$className", pointerType(int8Type), vtable)
|
val vtablePtr = addGlobalConstArray("kvtable:$className", pointerType(int8Type), vtable)
|
||||||
|
|
||||||
val methods = getMethodTableEntries(classDesc).map {
|
val methods = getMethodTableEntries(classDesc).map {
|
||||||
val nameSignature = it.name.nameHash // FIXME: add signature
|
val nameSignature = it.name.nameHash // FIXME: add signature
|
||||||
// TODO: compile-time resolution limits binary compatibility
|
// TODO: compile-time resolution limits binary compatibility
|
||||||
val methodEntryPoint = it.implementation.entryPointAddress
|
val methodEntryPoint = it.implementation.entryPointAddress
|
||||||
MethodTableRecord(nameSignature, methodEntryPoint)
|
MethodTableRecord(nameSignature, methodEntryPoint)
|
||||||
}.sortedBy { it.nameSignature }
|
}.sortedBy { it.nameSignature }
|
||||||
|
|
||||||
val methodsPtr = addGlobalConstArray("kmethods:$className", runtime.methodTableRecordType, methods)
|
val methodsPtr = addGlobalConstArray("kmethods:$className", runtime.methodTableRecordType, methods)
|
||||||
|
|
||||||
val typeInfo = TypeInfo(name, size,
|
val typeInfo = TypeInfo(name, size,
|
||||||
superType,
|
superType,
|
||||||
objOffsetsPtr, objOffsets.size,
|
objOffsetsPtr, objOffsets.size,
|
||||||
interfacesPtr, interfaces.size,
|
interfacesPtr, interfaces.size,
|
||||||
vtablePtr,
|
vtablePtr,
|
||||||
methodsPtr, methods.size,
|
methodsPtr, methods.size,
|
||||||
fieldsPtr, fields.size)
|
fieldsPtr, fields.size)
|
||||||
|
|
||||||
val typeInfoGlobal = classDesc.llvmTypeInfoPtr.getLlvmValue() // TODO: it is a hack
|
val typeInfoGlobal = classDesc.llvmTypeInfoPtr.getLlvmValue() // TODO: it is a hack
|
||||||
LLVMSetInitializer(typeInfoGlobal, typeInfo.getLlvmValue())
|
LLVMSetInitializer(typeInfoGlobal, typeInfo.getLlvmValue())
|
||||||
LLVMSetGlobalConstant(typeInfoGlobal, 1)
|
LLVMSetGlobalConstant(typeInfoGlobal, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-27
@@ -4,41 +4,41 @@ import kotlin_native.interop.*
|
|||||||
import llvm.*
|
import llvm.*
|
||||||
|
|
||||||
class Runtime(private val bitcodeFile: String) {
|
class Runtime(private val bitcodeFile: String) {
|
||||||
val llvmModule: LLVMOpaqueModule
|
val llvmModule: LLVMOpaqueModule
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val arena = Arena()
|
val arena = Arena()
|
||||||
try {
|
try {
|
||||||
|
|
||||||
val bufRef = arena.alloc(LLVMOpaqueMemoryBuffer.ref)
|
val bufRef = arena.alloc(LLVMOpaqueMemoryBuffer.ref)
|
||||||
val errorRef = arena.alloc(Int8Box.ref)
|
val errorRef = arena.alloc(Int8Box.ref)
|
||||||
val res = LLVMCreateMemoryBufferWithContentsOfFile(bitcodeFile, bufRef, errorRef)
|
val res = LLVMCreateMemoryBufferWithContentsOfFile(bitcodeFile, bufRef, errorRef)
|
||||||
if (res != 0) {
|
if (res != 0) {
|
||||||
throw Error(errorRef.value?.asCString()?.toString())
|
throw Error(errorRef.value?.asCString()?.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
val moduleRef = arena.alloc(LLVMOpaqueModule.ref)
|
val moduleRef = arena.alloc(LLVMOpaqueModule.ref)
|
||||||
val parseRes = LLVMParseBitcode2(bufRef.value, moduleRef)
|
val parseRes = LLVMParseBitcode2(bufRef.value, moduleRef)
|
||||||
if (parseRes != 0) {
|
if (parseRes != 0) {
|
||||||
throw Error(parseRes.toString())
|
throw Error(parseRes.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
llvmModule = moduleRef.value!!
|
llvmModule = moduleRef.value!!
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
arena.clear()
|
arena.clear()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val typeInfoType = LLVMGetTypeByName(llvmModule, "struct.TypeInfo")
|
val typeInfoType = LLVMGetTypeByName(llvmModule, "struct.TypeInfo")
|
||||||
val fieldTableRecordType = LLVMGetTypeByName(llvmModule, "struct.FieldTableRecord")
|
val fieldTableRecordType = LLVMGetTypeByName(llvmModule, "struct.FieldTableRecord")
|
||||||
val methodTableRecordType = LLVMGetTypeByName(llvmModule, "struct.MethodTableRecord")
|
val methodTableRecordType = LLVMGetTypeByName(llvmModule, "struct.MethodTableRecord")
|
||||||
val globalhHashType = LLVMGetTypeByName(llvmModule, "struct.GlobalHash")
|
val globalhHashType = LLVMGetTypeByName(llvmModule, "struct.GlobalHash")
|
||||||
|
|
||||||
val target = LLVMGetTarget(llvmModule)!!.asCString().toString()
|
val target = LLVMGetTarget(llvmModule)!!.asCString().toString()
|
||||||
|
|
||||||
val dataLayout = LLVMGetDataLayout(llvmModule)!!.asCString().toString()
|
val dataLayout = LLVMGetDataLayout(llvmModule)!!.asCString().toString()
|
||||||
|
|
||||||
val targetData = LLVMCreateTargetData(dataLayout)
|
val targetData = LLVMCreateTargetData(dataLayout)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user