Restore codegen.
This commit is contained in:
+42
-42
@@ -27,12 +27,12 @@ internal class RTTIGeneratorVisitor(context: Context) : IrElementVisitorVoid {
|
|||||||
val generator = RTTIGenerator(context)
|
val generator = RTTIGenerator(context)
|
||||||
|
|
||||||
override fun visitElement(element: IrElement) {
|
override fun visitElement(element: IrElement) {
|
||||||
element.acceptChildrenVoid(this)
|
element.acceptChildrenVoid(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitClass(declaration: IrClass) {
|
override fun visitClass(declaration: IrClass) {
|
||||||
super.visitClass(declaration)
|
super.visitClass(declaration)
|
||||||
generator.generate(declaration.descriptor)
|
generator.generate(declaration.descriptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -43,63 +43,63 @@ internal class RTTIGeneratorVisitor(context: Context) : IrElementVisitorVoid {
|
|||||||
internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid {
|
internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid {
|
||||||
val generator = CodeGenerator(context)
|
val generator = CodeGenerator(context)
|
||||||
override fun visitFunction(declaration: IrFunction) {
|
override fun visitFunction(declaration: IrFunction) {
|
||||||
generator.function(declaration)
|
generator.function(declaration)
|
||||||
declaration.acceptChildrenVoid(this)
|
declaration.acceptChildrenVoid(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitElement(element: IrElement) {
|
override fun visitElement(element: IrElement) {
|
||||||
element.acceptChildrenVoid(this)
|
element.acceptChildrenVoid(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitSetVariable(expression: IrSetVariable) {
|
override fun visitSetVariable(expression: IrSetVariable) {
|
||||||
val value = evaluateExpression(generator.tmpVariable(), expression.value)
|
val value = evaluateExpression(generator.tmpVariable(), expression.value)
|
||||||
generator.store(value!!, generator.variable(expression.descriptor.name.asString())!!)
|
generator.store(value!!, generator.variable(expression.descriptor.name.asString())!!)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun evaluateExpression(tmpVariableName: String, value: IrExpression?): LLVMOpaqueValue? {
|
private fun evaluateExpression(tmpVariableName: String, value: IrExpression?): LLVMOpaqueValue? {
|
||||||
when (value) {
|
when (value) {
|
||||||
is IrCall -> return evaluateCall(tmpVariableName, value)
|
is IrCall -> return evaluateCall(tmpVariableName, value)
|
||||||
is IrGetValue -> return generator.load(generator.variable(value.descriptor.name.asString())!!, tmpVariableName)
|
is IrGetValue -> return generator.load(generator.variable(value.descriptor.name.asString())!!, tmpVariableName)
|
||||||
null -> return null
|
null -> return null
|
||||||
else -> {
|
else -> {
|
||||||
TODO()
|
TODO()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun evaluateCall(tmpVariableName: String, value: IrCall?): LLVMOpaqueValue? {
|
private fun evaluateCall(tmpVariableName: String, value: IrCall?): LLVMOpaqueValue? {
|
||||||
/* TODO: should we count on receiver?
|
/* TODO: should we count on receiver?
|
||||||
* val tmp = tmpVar()
|
* val tmp = tmpVar()
|
||||||
* val lhs = evaluateExpression(tmp, value.dispatchReceiver!!)
|
* val lhs = evaluateExpression(tmp, value.dispatchReceiver!!)
|
||||||
*/
|
*/
|
||||||
val args = mutableListOf<LLVMOpaqueValue?>()
|
val args = mutableListOf<LLVMOpaqueValue?>()
|
||||||
value!!.acceptChildrenVoid(object:IrElementVisitorVoid{
|
value!!.acceptChildrenVoid(object:IrElementVisitorVoid{
|
||||||
override fun visitElement(element: IrElement) {
|
override fun visitElement(element: IrElement) {
|
||||||
val tmp = generator.tmpVariable()
|
val tmp = generator.tmpVariable()
|
||||||
args.add(evaluateExpression(tmp, element as IrExpression))
|
args.add(evaluateExpression(tmp, element as IrExpression))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
when (value!!.origin) {
|
when (value!!.origin) {
|
||||||
IrStatementOrigin.PLUS -> return generator.plus(args[0]!!, args[1]!!, tmpVariableName)
|
IrStatementOrigin.PLUS -> return generator.plus(args[0]!!, args[1]!!, tmpVariableName)
|
||||||
else -> {
|
else -> {
|
||||||
TODO()
|
TODO()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TODO()
|
TODO()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitVariable(declaration: IrVariable) {
|
override fun visitVariable(declaration: IrVariable) {
|
||||||
val variableName = declaration.descriptor.name.asString()
|
val variableName = declaration.descriptor.name.asString()
|
||||||
val variableType = declaration.descriptor.type
|
val variableType = declaration.descriptor.type
|
||||||
generator.registerVariable(variableName, generator.alloca(variableType, variableName))
|
generator.registerVariable(variableName, generator.alloca(variableType, variableName))
|
||||||
|
|
||||||
evaluateExpression(variableName, declaration.initializer)
|
evaluateExpression(variableName, declaration.initializer)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitReturn(expression: IrReturn) {
|
override fun visitReturn(expression: IrReturn) {
|
||||||
val tmpVarName = generator.tmpVariable()
|
val tmpVarName = generator.tmpVariable()
|
||||||
val value:LLVMOpaqueValue?
|
val value:LLVMOpaqueValue?
|
||||||
value = evaluateExpression(tmpVarName, expression.value)
|
value = evaluateExpression(tmpVarName, expression.value)
|
||||||
LLVMBuildRet(context.llvmBuilder, value)
|
LLVMBuildRet(context.llvmBuilder, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-9
@@ -13,7 +13,7 @@ internal abstract class CompileTimeValue {
|
|||||||
abstract fun getLlvmValue(): LLVMOpaqueValue?
|
abstract fun getLlvmValue(): LLVMOpaqueValue?
|
||||||
|
|
||||||
fun getLlvmType(): LLVMOpaqueType? {
|
fun getLlvmType(): LLVMOpaqueType? {
|
||||||
return LLVMTypeOf(getLlvmValue())
|
return LLVMTypeOf(getLlvmValue())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -21,10 +21,10 @@ internal abstract class CompileTimeValue {
|
|||||||
internal class ConstArray(val elemType: LLVMOpaqueType?, val elements: List<CompileTimeValue>) : CompileTimeValue() {
|
internal class ConstArray(val elemType: LLVMOpaqueType?, val elements: List<CompileTimeValue>) : CompileTimeValue() {
|
||||||
|
|
||||||
override fun getLlvmValue(): LLVMOpaqueValue? {
|
override fun getLlvmValue(): LLVMOpaqueValue? {
|
||||||
val values = elements.map { it.getLlvmValue() }.toTypedArray()
|
val values = elements.map { it.getLlvmValue() }.toTypedArray()
|
||||||
val valuesNativeArrayPtr = mallocNativeArrayOf(LLVMOpaqueValue, *values)[0] // FIXME: dispose
|
val valuesNativeArrayPtr = mallocNativeArrayOf(LLVMOpaqueValue, *values)[0] // FIXME: dispose
|
||||||
|
|
||||||
return LLVMConstArray(elemType, valuesNativeArrayPtr, values.size)
|
return LLVMConstArray(elemType, valuesNativeArrayPtr, values.size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,9 +33,9 @@ internal open class Struct(val type: LLVMOpaqueType?, val elements: List<Compile
|
|||||||
constructor(type: LLVMOpaqueType?, vararg elements: CompileTimeValue) : this(type, elements.toList())
|
constructor(type: LLVMOpaqueType?, vararg elements: CompileTimeValue) : this(type, elements.toList())
|
||||||
|
|
||||||
override fun getLlvmValue(): LLVMOpaqueValue? {
|
override fun getLlvmValue(): LLVMOpaqueValue? {
|
||||||
val values = elements.map { it.getLlvmValue() }.toTypedArray()
|
val values = elements.map { it.getLlvmValue() }.toTypedArray()
|
||||||
val valuesNativeArrayPtr = mallocNativeArrayOf(LLVMOpaqueValue, *values)[0] // FIXME: dispose
|
val valuesNativeArrayPtr = mallocNativeArrayOf(LLVMOpaqueValue, *values)[0] // FIXME: dispose
|
||||||
return LLVMConstNamedStruct(type, valuesNativeArrayPtr, values.size)
|
return LLVMConstNamedStruct(type, valuesNativeArrayPtr, values.size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,8 +67,8 @@ internal fun pointerType(pointeeType: LLVMOpaqueType?) = LLVMPointerType(pointee
|
|||||||
internal fun getLlvmFunctionType(function: FunctionDescriptor): LLVMOpaqueType? {
|
internal fun getLlvmFunctionType(function: FunctionDescriptor): LLVMOpaqueType? {
|
||||||
val returnType = getLLVMType(function.returnType!!)
|
val returnType = getLLVMType(function.returnType!!)
|
||||||
val params = function.dispatchReceiverParameter.singletonOrEmptyList() +
|
val params = function.dispatchReceiverParameter.singletonOrEmptyList() +
|
||||||
function.extensionReceiverParameter.singletonOrEmptyList() +
|
function.extensionReceiverParameter.singletonOrEmptyList() +
|
||||||
function.valueParameters
|
function.valueParameters
|
||||||
|
|
||||||
val paramTypes = params.map { getLLVMType(it.type) }.toTypedArray()
|
val paramTypes = params.map { getLLVMType(it.type) }.toTypedArray()
|
||||||
|
|
||||||
|
|||||||
+129
-129
@@ -15,157 +15,157 @@ 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)
|
|
||||||
return classType
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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()
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
LLVMStructSetBody(classType, fieldTypesNativeArrayPtr, fieldTypes.size, 0)
|
||||||
|
return classType
|
||||||
}
|
}
|
||||||
|
|
||||||
return inheritedVtableSlots.map { it!! } + methods.filter { it in entriesForNewVtableSlots }
|
// TODO: optimize
|
||||||
}
|
private fun getVtableEntries(classDesc: ClassDescriptor): List<FunctionDescriptor> {
|
||||||
|
val superVtableEntries = if (KotlinBuiltIns.isAny(classDesc)) {
|
||||||
|
emptyList()
|
||||||
|
} else {
|
||||||
|
getVtableEntries(classDesc.getSuperClassOrAny())
|
||||||
|
}
|
||||||
|
|
||||||
private fun getMethodTableEntries(classDesc: ClassDescriptor): List<FunctionDescriptor> {
|
val inheritedVtableSlots = Array<FunctionDescriptor?>(superVtableEntries.size, { null })
|
||||||
val contributedDescriptors = classDesc.unsubstitutedMemberScope.getContributedDescriptors()
|
val methods = getMethodTableEntries(classDesc) // TODO: ensure order is well-defined
|
||||||
// (includes declarations from supers)
|
val entriesForNewVtableSlots = methods.toMutableSet()
|
||||||
|
|
||||||
val functions = contributedDescriptors.filterIsInstance<FunctionDescriptor>()
|
methods.forEach { method ->
|
||||||
|
superVtableEntries.forEachIndexed { i, superMethod ->
|
||||||
|
if (OverridingUtil.overrides(method, superMethod)) {
|
||||||
|
assert (inheritedVtableSlots[i] == null)
|
||||||
|
inheritedVtableSlots[i] = method
|
||||||
|
|
||||||
val properties = contributedDescriptors.filterIsInstance<PropertyDescriptor>()
|
assert (method in entriesForNewVtableSlots)
|
||||||
val getters = properties.mapNotNull { it.getter }
|
entriesForNewVtableSlots.remove(method)
|
||||||
val setters = properties.mapNotNull { it.setter }
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val allMethods = functions + getters + setters
|
return inheritedVtableSlots.map { it!! } + methods.filter { it in entriesForNewVtableSlots }
|
||||||
|
|
||||||
// TODO: adding or removing 'open' modifier will break the binary compatibility
|
|
||||||
return allMethods.filter { it.modality != Modality.FINAL }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun generate(classDesc: ClassDescriptor) {
|
|
||||||
|
|
||||||
val className = classDesc.fqNameSafe
|
|
||||||
|
|
||||||
val classType = createStructFor(className, classDesc.fields)
|
|
||||||
|
|
||||||
val name = className.nameHash
|
|
||||||
|
|
||||||
val size = LLVMStoreSizeOfType(runtime.targetData, classType).toInt()
|
|
||||||
|
|
||||||
val superType = classDesc.getSuperClassOrAny().llvmTypeInfoPtr
|
|
||||||
|
|
||||||
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 fields = classDesc.fields.mapIndexed { index, field ->
|
private fun getMethodTableEntries(classDesc: ClassDescriptor): List<FunctionDescriptor> {
|
||||||
// Note: using FQ name because a class may have multiple fields with the same name due to property overriding
|
val contributedDescriptors = classDesc.unsubstitutedMemberScope.getContributedDescriptors()
|
||||||
val nameSignature = field.fqNameSafe.nameHash // FIXME: add signature
|
// (includes declarations from supers)
|
||||||
val fieldOffset = LLVMOffsetOfElement(runtime.targetData, classType, index)
|
|
||||||
FieldTableRecord(nameSignature, fieldOffset.toInt())
|
|
||||||
}.sortedBy { it.nameSignature }
|
|
||||||
|
|
||||||
val fieldsPtr = addGlobalConstArray("kfields:$className", runtime.fieldTableRecordType, fields)
|
val functions = contributedDescriptors.filterIsInstance<FunctionDescriptor>()
|
||||||
|
|
||||||
// TODO: compile-time resolution limits binary compatibility
|
val properties = contributedDescriptors.filterIsInstance<PropertyDescriptor>()
|
||||||
val vtable = getVtableEntries(classDesc).map { it.implementation.entryPointAddress }
|
val getters = properties.mapNotNull { it.getter }
|
||||||
val vtablePtr = addGlobalConstArray("kvtable:$className", pointerType(int8Type), vtable)
|
val setters = properties.mapNotNull { it.setter }
|
||||||
|
|
||||||
val methods = getMethodTableEntries(classDesc).map {
|
val allMethods = functions + getters + setters
|
||||||
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)
|
// TODO: adding or removing 'open' modifier will break the binary compatibility
|
||||||
|
return allMethods.filter { it.modality != Modality.FINAL }
|
||||||
|
}
|
||||||
|
|
||||||
val typeInfo = TypeInfo(name, size,
|
fun generate(classDesc: ClassDescriptor) {
|
||||||
|
|
||||||
|
val className = classDesc.fqNameSafe
|
||||||
|
|
||||||
|
val classType = createStructFor(className, classDesc.fields)
|
||||||
|
|
||||||
|
val name = className.nameHash
|
||||||
|
|
||||||
|
val size = LLVMStoreSizeOfType(runtime.targetData, classType).toInt()
|
||||||
|
|
||||||
|
val superType = classDesc.getSuperClassOrAny().llvmTypeInfoPtr
|
||||||
|
|
||||||
|
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 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)
|
||||||
|
|
||||||
|
// 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 methodsPtr = addGlobalConstArray("kmethods:$className", runtime.methodTableRecordType, methods)
|
||||||
|
|
||||||
|
val typeInfo = TypeInfo(name, size,
|
||||||
superType,
|
superType,
|
||||||
objOffsetsPtr, objOffsets.size,
|
objOffsetsPtr, objOffsets.size,
|
||||||
interfacesPtr, interfaces.size,
|
interfacesPtr, interfaces.size,
|
||||||
@@ -173,9 +173,9 @@ internal class RTTIGenerator(override val context: Context) : ContextUtils {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+26
-26
@@ -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