Fix spaces.

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