Moved NativePtr from interop to konan.internal

This commit is contained in:
Igor Chevdar
2017-04-26 12:56:24 +03:00
parent 0750626c29
commit adc1faa6c5
9 changed files with 75 additions and 36 deletions
@@ -30,28 +30,22 @@ import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
private val cPointerName = "CPointer"
private val nativePointedName = "NativePointed"
private val nativePtrName = "NativePtr"
internal class InteropBuiltIns(builtIns: KonanBuiltIns) {
object FqNames {
val packageName = FqName("kotlinx.cinterop")
val nativePtr = packageName.child(Name.identifier(nativePtrName)).toUnsafe()
val cPointer = packageName.child(Name.identifier(cPointerName)).toUnsafe()
val nativePointed = packageName.child(Name.identifier(nativePointedName)).toUnsafe()
}
private val packageScope = builtIns.builtInsModule.getPackage(FqNames.packageName).memberScope
val getNativeNullPtr = packageScope.getContributedFunctions("getNativeNullPtr").single()
val getPointerSize = packageScope.getContributedFunctions("getPointerSize").single()
val nullableInteropValueTypes = listOf(ValueType.C_POINTER, ValueType.NATIVE_POINTED)
private val nativePtr = packageScope.getContributedClassifier(nativePtrName) as ClassDescriptor
private val nativePointed = packageScope.getContributedClassifier(nativePointedName) as ClassDescriptor
val cPointer = this.packageScope.getContributedClassifier(cPointerName) as ClassDescriptor
@@ -125,7 +119,7 @@ internal class InteropBuiltIns(builtIns: KonanBuiltIns) {
private val primitives = listOf(
builtIns.byte, builtIns.short, builtIns.int, builtIns.long,
builtIns.float, builtIns.double,
nativePtr
builtIns.nativePtr
)
val readPrimitive = primitives.map {
@@ -136,10 +130,6 @@ internal class InteropBuiltIns(builtIns: KonanBuiltIns) {
nativeMemUtils.unsubstitutedMemberScope.getContributedFunctions("put" + it.name).single()
}.toSet()
val nativePtrPlusLong = nativePtr.unsubstitutedMemberScope.getContributedFunctions("plus").single()
val nativePtrToLong = nativePtr.unsubstitutedMemberScope.getContributedFunctions("toLong").single()
val bitsToFloat = packageScope.getContributedFunctions("bitsToFloat").single()
val bitsToDouble = packageScope.getContributedFunctions("bitsToDouble").single()
@@ -17,13 +17,16 @@
package org.jetbrains.kotlin.backend.konan
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.ImportPath
import org.jetbrains.kotlin.resolve.MultiTargetPlatform
import org.jetbrains.kotlin.resolve.PlatformConfigurator
import org.jetbrains.kotlin.resolve.TargetPlatform
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.storage.StorageManager
val STDLIB_MODULE_NAME = Name.special("<stdlib>")
@@ -32,9 +35,31 @@ fun ModuleDescriptor.isStdlib(): Boolean {
return name == STDLIB_MODULE_NAME
}
private val nativePtrName = "NativePtr"
class KonanBuiltIns(storageManager: StorageManager) : KotlinBuiltIns(storageManager) {
override fun getClassDescriptorFactories() =
super.getClassDescriptorFactories() + KonanBuiltInClassDescriptorFactory(storageManager, builtInsModule)
object FqNames {
val packageName = FqName("konan.internal")
val nativePtr = packageName.child(Name.identifier(nativePtrName)).toUnsafe()
}
private val packageScope by lazy { builtInsModule.getPackage(FqNames.packageName).memberScope }
val nativePtr by lazy { packageScope.getContributedClassifier(nativePtrName) as ClassDescriptor }
val nativePtrPlusLong by lazy { nativePtr.unsubstitutedMemberScope.getContributedFunctions("plus").single() }
val nativePtrToLong by lazy { nativePtr.unsubstitutedMemberScope.getContributedFunctions("toLong").single() }
val getNativeNullPtr by lazy { packageScope.getContributedFunctions("getNativeNullPtr").single() }
private fun MemberScope.getContributedClassifier(name: String) =
this.getContributedClassifier(Name.identifier(name), NoLookupLocation.FROM_BUILTINS)
private fun MemberScope.getContributedFunctions(name: String) =
this.getContributedFunctions(Name.identifier(name), NoLookupLocation.FROM_BUILTINS)
}
object KonanPlatform : TargetPlatform("Konan") {
@@ -41,7 +41,7 @@ enum class ValueType(val classFqName: FqNameUnsafe, val isNullable: Boolean = fa
DOUBLE(KotlinBuiltIns.FQ_NAMES._double),
UNBOUND_CALLABLE_REFERENCE(FqNameUnsafe("konan.internal.UnboundCallableReference")),
NATIVE_PTR(InteropBuiltIns.FqNames.nativePtr),
NATIVE_PTR(KonanBuiltIns.FqNames.nativePtr),
NATIVE_POINTED(InteropBuiltIns.FqNames.nativePointed, isNullable = true),
C_POINTER(InteropBuiltIns.FqNames.cPointer, isNullable = true)
@@ -1799,13 +1799,13 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
val resumePoints = mutableListOf<LLVMBasicBlockRef>()
using (SuspendableExpressionScope(resumePoints)) {
codegen.condBr(codegen.icmpEq(suspensionPointId, kIntPtrZero), bbStart, bbDispatch)
codegen.condBr(codegen.icmpEq(suspensionPointId, kNullInt8Ptr), bbStart, bbDispatch)
codegen.positionAtEnd(bbStart)
val result = evaluateExpression(expression.result)
codegen.appendingTo(bbDispatch) {
codegen.indirectBr(codegen.intToPtr(suspensionPointId, int8TypePtr), resumePoints)
codegen.indirectBr(suspensionPointId, resumePoints)
}
return result
}
@@ -1815,7 +1815,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
val bbResume: LLVMBasicBlockRef): InnerScopeImpl() {
override fun genGetValue(descriptor: ValueDescriptor): LLVMValueRef {
if (descriptor == suspensionPointId)
return codegen.ptrToInt(codegen.blockAddress(bbResume), LLVMInt64Type()!!) // TODO: intptr.
return codegen.blockAddress(bbResume)
return super.genGetValue(descriptor)
}
}
@@ -1979,10 +1979,10 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
codegen.store(args[2], pointer)
codegen.theUnitInstanceRef.llvm
}
interop.nativePtrPlusLong -> codegen.gep(args[0], args[1])
interop.getNativeNullPtr -> kNullInt8Ptr
context.builtIns.nativePtrPlusLong -> codegen.gep(args[0], args[1])
context.builtIns.getNativeNullPtr -> kNullInt8Ptr
interop.getPointerSize -> Int32(LLVMPointerSize(codegen.llvmTargetData)).llvm
interop.nativePtrToLong -> {
context.builtIns.nativePtrToLong -> {
val intPtrValue = codegen.ptrToInt(args.single(), codegen.intPtrType)
val resultType = codegen.getLLVMType(descriptor.returnType!!)
@@ -146,7 +146,7 @@ private class AutoboxingTransformer(val context: Context) : AbstractValueUsageTr
override fun IrExpression.useAs(type: KotlinType): IrExpression {
val interop = context.interopBuiltIns
if (this.isNullConst() && interop.nullableInteropValueTypes.any { type.isRepresentedAs(it) }) {
return IrCallImpl(startOffset, endOffset, interop.getNativeNullPtr).uncheckedCast(type)
return IrCallImpl(startOffset, endOffset, context.builtIns.getNativeNullPtr).uncheckedCast(type)
}
val actualType = when (this) {
@@ -60,6 +60,7 @@ internal class InteropLowering(val context: Context) : FileLoweringPass {
private class InteropTransformer(val context: Context, val irFile: IrFile) : IrBuildingTransformer(context) {
val interop = context.interopBuiltIns
val konanBuiltins = context.builtIns
private fun MemberScope.getSingleContributedFunction(name: String,
predicate: (SimpleFunctionDescriptor) -> Boolean) =
@@ -119,7 +120,7 @@ private class InteropTransformer(val context: Context, val irFile: IrFile) : IrB
val elementType = array.type.arguments.single().type
val elementSize = sizeOf(elementType) ?: return null
val resultRawPtr = irCall(interop.nativePtrPlusLong).apply {
val resultRawPtr = irCall(konanBuiltins.nativePtrPlusLong).apply {
dispatchReceiver = irCall(interop.cPointerGetRawValue).apply {
extensionReceiver = array
}