Skip to content

Commit

Permalink
Gross stdlib cleanup (part 1). (JetBrains#1918)
Browse files Browse the repository at this point in the history
  • Loading branch information
olonho authored Aug 23, 2018
1 parent e14570a commit 4606f02
Show file tree
Hide file tree
Showing 50 changed files with 344 additions and 247 deletions.
23 changes: 12 additions & 11 deletions Interop/JsRuntime/src/main/kotlin/jsinterop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

package kotlinx.wasm.jsinterop

import kotlin.native.*
import kotlin.native.internal.ExportForCppRuntime
import kotlinx.cinterop.*
Expand All @@ -28,15 +29,15 @@ typealias Pointer = Int
* from internalization and DCE
*/

@Used
@Retain
@SymbolName("Konan_js_allocateArena")
external public fun allocateArena(): Arena

@Used
@Retain
@SymbolName("Konan_js_freeArena")
external public fun freeArena(arena: Arena)

@Used
@Retain
@SymbolName("Konan_js_pushIntToArena")
external public fun pushIntToArena(arena: Arena, value: Int)

Expand All @@ -50,15 +51,15 @@ fun doubleUpper(value: Double): Int =
fun doubleLower(value: Double): Int =
(value.toBits() and 0x00000000ffffffff) .toInt()

@Used
@Retain
@SymbolName("ReturnSlot_getDouble")
external public fun ReturnSlot_getDouble(): Double

@Used
@Retain
@SymbolName("Kotlin_String_utf16pointer")
external public fun stringPointer(message: String): Pointer

@Used
@Retain
@SymbolName("Kotlin_String_utf16length")
external public fun stringLengthBytes(message: String): Int

Expand All @@ -69,7 +70,7 @@ fun <R> wrapFunction(func: KtFunction<R>): Int {
return ptr.toInt() // TODO: LP64 unsafe.
}

@Used
@Retain
@ExportForCppRuntime("Konan_js_runLambda")
fun runLambda(pointer: Int, argumentsArena: Arena, argumentsArenaSize: Int): Int {
val arguments = arrayListOf<JsValue>()
Expand Down Expand Up @@ -105,19 +106,19 @@ open class JsArray(arena: Arena, index: Object): JsValue(arena, index) {
get() = this.getInt("length")
}

@Used
@Retain
@SymbolName("Konan_js_getInt")
external public fun getInt(arena: Arena, obj: Object, propertyPtr: Pointer, propertyLen: Int): Int;

@Used
@Retain
@SymbolName("Konan_js_getProperty")
external public fun Konan_js_getProperty(arena: Arena, obj: Object, propertyPtr: Pointer, propertyLen: Int): Int;

@Used
@Retain
@SymbolName("Konan_js_setFunction")
external public fun setFunction(arena: Arena, obj: Object, propertyName: Pointer, propertyLength: Int , function: Int)

@Used
@Retain
@SymbolName("Konan_js_setString")
external public fun setString(arena: Arena, obj: Object, propertyName: Pointer, propertyLength: Int, stringPtr: Pointer, stringLength: Int )

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package kotlinx.cinterop

import kotlin.native.internal.getNativeNullPtr
import kotlin.native.internal.Intrinsic
import kotlin.native.internal.reinterpret
import kotlin.native.internal.Intrinsic
import kotlin.native.internal.VolatileLambda

typealias NativePtr = kotlin.native.internal.NativePtr
internal typealias NonNullNativePtr = kotlin.native.internal.NonNullNativePtr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package kotlinx.cinterop
import kotlin.native.*
import kotlin.native.internal.ExportTypeInfo

interface ObjCObject
interface ObjCClass : ObjCObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private val cKeywords = setOf(
"xor_eq"
)

private val cnameAnnotation = FqName("kotlin.native.internal.CName")
private val cnameAnnotation = FqName("kotlin.native.CName")

private fun org.jetbrains.kotlin.types.KotlinType.isGeneric() =
constructor.declarationDescriptor is TypeParameterDescriptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config) {
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() }
val immutableBinaryBlobOf by lazy {
builtIns.builtInsModule.getPackage(KonanFqNames.packageName).memberScope.getContributedFunctions("immutableBinaryBlobOf").single()
val immutableBlobOf by lazy {
builtIns.builtInsModule.getPackage(KonanFqNames.packageName).memberScope.getContributedFunctions("immutableBlobOf").single()
}

val specialDeclarationsFactory = SpecialDeclarationsFactory(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ internal val arrayTypes = setOf(
"kotlin.FloatArray",
"kotlin.DoubleArray",
"kotlin.BooleanArray",
"kotlin.native.ImmutableBinaryBlob"
"kotlin.native.ImmutableBlob"
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,12 @@ internal class KonanSymbols(context: Context, val symbolTable: SymbolTable, val
it to symbolTable.referenceSimpleFunction(context.getInternalFunctions(name).single())
}

val immutableBinaryBlob = symbolTable.referenceClass(
val immutableBlob = symbolTable.referenceClass(
builtInsPackage("kotlin", "native").getContributedClassifier(
Name.identifier("ImmutableBinaryBlob"), NoLookupLocation.FROM_BACKEND
Name.identifier("ImmutableBlob"), NoLookupLocation.FROM_BACKEND
) as ClassDescriptor
)

val immutableBinaryBlobOf = symbolTable.referenceSimpleFunction(context.immutableBinaryBlobOf)

val scheduleImpl = symbolTable.referenceSimpleFunction(context.interopBuiltIns.scheduleImplFunction)

val areEqualByValue = context.getInternalFunctions("areEqualByValue").map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ internal tailrec fun DeclarationDescriptor.isExported(): Boolean {
if (descriptorAnnotations.hasAnnotation(publishedApiAnnotation)){
return true
}
if (descriptorAnnotations.hasAnnotation(inlineExposedAnnotation)){
return true
}

if (this.isAnonymousObject)
return false
Expand All @@ -87,8 +84,7 @@ internal tailrec fun DeclarationDescriptor.isExported(): Boolean {
// TODO: this code is required because accessor doesn't have a reference to property.
if (descriptor is PropertyAccessorDescriptor) {
val property = descriptor.correspondingProperty
if (property.annotations.hasAnnotation(inlineExposedAnnotation) ||
property.annotations.hasAnnotation(publishedApiAnnotation)) return true
if (property.annotations.hasAnnotation(publishedApiAnnotation)) return true
}
}

Expand Down Expand Up @@ -119,16 +115,14 @@ internal tailrec fun DeclarationDescriptor.isExported(): Boolean {

private val symbolNameAnnotation = FqName("kotlin.native.SymbolName")

private val exportForCppRuntimeAnnotation = FqName("kotlin.native.internal.ExportForCppRuntime")
private val cnameAnnotation = FqName("kotlin.native.CName")

private val cnameAnnotation = FqName("kotlin.native.internal.CName")
private val exportForCppRuntimeAnnotation = FqName("kotlin.native.internal.ExportForCppRuntime")

private val exportForCompilerAnnotation = FqName("kotlin.native.internal.ExportForCompiler")

private val publishedApiAnnotation = FqName("kotlin.PublishedApi")

private val inlineExposedAnnotation = FqName("kotlin.internal.InlineExposed")

private fun acyclicTypeMangler(visited: MutableSet<TypeParameterDescriptor>, type: IrType): String {
val descriptor = (type.classifierOrNull as? IrTypeParameterSymbol)?.owner
if (descriptor != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1745,10 +1745,10 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
return receiver
}

context.immutableBinaryBlobOf -> {
context.immutableBlobOf -> {
@Suppress("UNCHECKED_CAST")
val arg = expression.getValueArgument(0) as IrConst<String>
return context.llvm.staticData.createImmutableBinaryBlob(arg)
return context.llvm.staticData.createImmutableBlob(arg)
}

context.ir.symbols.initInstance.descriptor -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ internal class RTTIGenerator(override val context: Context) : ContextUtils {
staticData.kotlinStringLiteral(string)
}

private val EXPORT_TYPE_INFO_FQ_NAME = FqName.fromSegments(listOf("kotlin", "native", "internal", "ExportTypeInfo"))

private fun exportTypeInfoIfRequired(classDesc: ClassDescriptor, typeInfoGlobal: LLVMValueRef?) {
val annot = classDesc.descriptor.annotations.findAnnotation(FqName("kotlin.native.ExportTypeInfo"))
val annot = classDesc.descriptor.annotations.findAnnotation(EXPORT_TYPE_INFO_FQ_NAME)
if (annot != null) {
val name = getStringValue(annot)!!
// TODO: use LLVMAddAlias?
// TODO: use LLVMAddAlias.
val global = addGlobal(name, pointerType(runtime.typeInfoType), isExported = true)
LLVMSetInitializer(global, typeInfoGlobal)
}
Expand All @@ -118,7 +120,7 @@ internal class RTTIGenerator(override val context: Context) : ContextUtils {
"kotlin.DoubleArray" to LLVMDoubleType()!!,
"kotlin.BooleanArray" to LLVMInt8Type()!!,
"kotlin.String" to LLVMInt16Type()!!,
"kotlin.native.ImmutableBinaryBlob" to LLVMInt8Type()!!
"kotlin.native.ImmutableBlob" to LLVMInt8Type()!!
)

// Keep in sync with Konan_RuntimeType.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ internal class StaticData(override val context: Context): ContextUtils {
*
* @param args data for constant creation.
*/
internal fun StaticData.createImmutableBinaryBlob(value: IrConst<String>): LLVMValueRef {
internal fun StaticData.createImmutableBlob(value: IrConst<String>): LLVMValueRef {
val args = value.value.map { Int8(it.toByte()).llvm }
return createKotlinArray(context.ir.symbols.immutableBinaryBlob.owner, args)
return createKotlinArray(context.ir.symbols.immutableBlob.owner, args)
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ internal class CallableReferenceLowering(val context: Context): FileLoweringPass
val argumentDescriptor = descriptor.valueParameters.singleOrNull {
cur.getValueArgument(it.index) == argument
}
if (argumentDescriptor != null && argumentDescriptor.annotations.findAnnotation(FqName("kotlin.native.VolatileLambda")) != null) {
if (argumentDescriptor?.annotations?.findAnnotation(VOLATILE_LAMBDA_FQ_NAME) != null) {
return expression
}
break
Expand Down Expand Up @@ -141,9 +141,9 @@ internal class CallableReferenceLowering(val context: Context): FileLoweringPass
val functionReferenceConstructor: IrConstructor)

private val COROUTINES_FQ_NAME = FqName.fromSegments(listOf("kotlin", "coroutines", "experimental"))
private val VOLATILE_LAMBDA_FQ_NAME = FqName.fromSegments(listOf("kotlin", "native", "internal", "VolatileLambda"))

private val coroutinesScope = context.irModule!!.descriptor.getPackage(COROUTINES_FQ_NAME).memberScope
private val kotlinPackageScope = context.builtIns.builtInsPackageScope

private val continuationClassDescriptor = coroutinesScope
.getContributedClassifier(Name.identifier("Continuation"), NoLookupLocation.FROM_BACKEND) as ClassDescriptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid

/**
* This pass runs after inlining and performs the following additional transformations over some operations:
* - Convert immutableBinaryBlobOf() arguments to special IrConst.
* - Convert immutableBlobOf() arguments to special IrConst.
* - Convert `obj::class` and `Class::class` to calls.
*/
internal class PostInlineLowering(val context: Context) : FileLoweringPass {
Expand Down Expand Up @@ -77,7 +77,7 @@ internal class PostInlineLowering(val context: Context) : FileLoweringPass {
// Function inlining is changing function symbol at callsite
// and unbound symbol replacement is happening later.
// So we compare descriptors for now.
if (expression.descriptor == context.immutableBinaryBlobOf) {
if (expression.descriptor == context.immutableBlobOf) {
// Convert arguments of the binary blob to special IrConst<String> structure, so that
// vararg lowering will not affect it.
val args = expression.getValueArgument(0) as? IrVararg
Expand Down Expand Up @@ -105,7 +105,7 @@ internal class PostInlineLowering(val context: Context) : FileLoweringPass {
}
expression.putValueArgument(0, IrConstImpl<String>(
expression.startOffset, expression.endOffset,
context.ir.symbols.immutableBinaryBlob.typeWithoutArguments,
context.ir.symbols.immutableBlob.typeWithoutArguments,
IrConstKind.String, builder.toString()))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ internal object DataFlowIR {

private val NAME_ESCAPES = Name.identifier("Escapes")
private val NAME_POINTS_TO = Name.identifier("PointsTo")
private val FQ_NAME_KONAN = FqName.fromSegments(listOf("kotlin", "native"))
private val FQ_NAME_KONAN = FqName.fromSegments(listOf("kotlin", "native", "internal"))

private val FQ_NAME_ESCAPES = FQ_NAME_KONAN.child(NAME_ESCAPES)
private val FQ_NAME_POINTS_TO = FQ_NAME_KONAN.child(NAME_POINTS_TO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ internal tailrec fun DeclarationDescriptor.isExported(): Boolean {
if (this.annotations.hasAnnotation(publishedApiAnnotation)){
return true
}
if (this.annotations.hasAnnotation(inlineExposedAnnotation)){
return true
}

if (DescriptorUtils.isAnonymousObject(this))
return false
Expand Down Expand Up @@ -172,8 +169,6 @@ private val exportForCompilerAnnotation = FqName("kotlin.native.internal.ExportF

private val publishedApiAnnotation = FqName("kotlin.PublishedApi")

private val inlineExposedAnnotation = FqName("kotlin.internal.InlineExposed")

private fun acyclicTypeMangler(visited: MutableSet<TypeParameterDescriptor>, type: KotlinType): String {
val descriptor = TypeUtils.getTypeParameterDescriptorOrNull(type)
if (descriptor != null) {
Expand Down
2 changes: 1 addition & 1 deletion backend.native/tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ task if_else(type: RunKonanTest) {
}

task immutable_binary_blob_in_lambda(type: RunKonanTest) {
source = "lower/immutable_binary_blob_in_lambda.kt"
source = "lower/immutable_blob_in_lambda.kt"
goldValue = "123\n"
}

Expand Down
2 changes: 1 addition & 1 deletion backend.native/tests/interop/basics/5.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import platform.posix.printf

val golden = immutableBinaryBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer(0)
val golden = immutableBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer(0)

fun main(args: Array<String>) {
printf("%s\n", golden)
Expand Down
4 changes: 2 additions & 2 deletions backend.native/tests/interop/platform_zlib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import kotlinx.cinterop.*

import platform.zlib.*

val source = immutableBinaryBlobOf(0xF3, 0x48, 0xCD, 0xC9, 0xC9, 0x57, 0x04, 0x00).asCPointer()
val golden = immutableBinaryBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer()
val source = immutableBlobOf(0xF3, 0x48, 0xCD, 0xC9, 0xC9, 0x57, 0x04, 0x00).asCPointer()
val golden = immutableBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer()

fun main(args: Array<String>) = memScoped {
val buffer = ByteArray(32)
Expand Down
9 changes: 0 additions & 9 deletions backend.native/tests/lower/immutable_binary_blob_in_lambda.kt

This file was deleted.

9 changes: 9 additions & 0 deletions backend.native/tests/lower/immutable_blob_in_lambda.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lower.immutable_blob_in_lambda

import kotlin.test.*

@Test
fun runTest() = run {
val golden = immutableBlobOf(123)
println(golden[0])
}
8 changes: 5 additions & 3 deletions backend.native/tests/produce_dynamic/simple/hello.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import kotlinx.cinterop.*

import kotlin.native.CName

// Top level functions.
fun hello() {
println("Hello, dynamic!")
Expand All @@ -13,15 +15,15 @@ open class Base {

open fun fooParam(arg0: String, arg1: Int) = println("Base.fooParam: $arg0 $arg1")

@kotlin.native.internal.CName(fullName = "", shortName = "strangeName") fun странноеИмя() = 111
@CName(fullName = "", shortName = "strangeName") fun странноеИмя() = 111

}

// Top level functions.
@kotlin.native.internal.CName(fullName = "topLevelFunctionFromC", shortName = "topLevelFunctionFromCShort")
@CName(fullName = "topLevelFunctionFromC", shortName = "topLevelFunctionFromCShort")
fun topLevelFunction(x1: Int, x2: Int) = x1 - x2

@kotlin.native.internal.CName("topLevelFunctionVoidFromC")
@CName("topLevelFunctionVoidFromC")
fun topLevelFunctionVoid(x1: Int, pointer: COpaquePointer?) {
assert(x1 == 42)
assert(pointer == null)
Expand Down
2 changes: 1 addition & 1 deletion backend.native/tests/runtime/collections/array3.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlin.test.*
import kotlin.native.*

@Test fun runTest() {
val data = immutableBinaryBlobOf(0x1, 0x2, 0x3, 0x7, 0x8, 0x9, 0x80, 0xff)
val data = immutableBlobOf(0x1, 0x2, 0x3, 0x7, 0x8, 0x9, 0x80, 0xff)
for (b in data) {
print("$b ")
}
Expand Down
Loading

0 comments on commit 4606f02

Please sign in to comment.