From f0950a92c3fe6851b60958b4f6898bfb06ff242b Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Mon, 30 Jan 2017 18:36:12 +0300 Subject: [PATCH] Various small fixes. (#208) --- .../jetbrains/kotlin/backend/konan/Context.kt | 2 - .../backend/konan/llvm/CodeGenerator.kt | 4 +- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 2 +- .../backend/konan/llvm/LlvmDeclarations.kt | 2 +- .../kotlin/backend/konan/llvm/StaticData.kt | 2 +- runtime/src/main/cpp/Memory.cpp | 5 +- runtime/src/main/cpp/Memory.h | 2 +- runtime/src/main/kotlin/kotlin/Array.kt | 1 - runtime/src/main/kotlin/kotlin/Arrays.kt | 120 ++++++++++++++++++ runtime/src/main/kotlin/kotlin/String.kt | 1 + .../main/kotlin/kotlin/collections/HashMap.kt | 7 +- .../kotlin/kotlin/internal/Annotations.kt | 8 ++ 12 files changed, 143 insertions(+), 13 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt index 8b759fb899d..41b4ed80cf5 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt @@ -17,8 +17,6 @@ import java.lang.System.out internal final class Context(val config: KonanConfig) : KonanBackendContext() { - val debug = true - var moduleDescriptor: ModuleDescriptor? = null // TODO: make lateinit? diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt index 6eb5fc0c44b..5e0ac5f26fa 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt @@ -32,8 +32,8 @@ internal class CodeGenerator(override val context: Context) : ContextUtils { prologue(llvmFunction, LLVMGetReturnType(getLlvmFunctionType(descriptor))!!) - if (!descriptor.isExported() && !context.debug) { - LLVMSetLinkage(llvmFunction, LLVMLinkage.LLVMPrivateLinkage) + if (!descriptor.isExported()) { + LLVMSetLinkage(llvmFunction, LLVMLinkage.LLVMInternalLinkage) // (Cannot do this before the function body is created). } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index 7d3e03b103e..b8cef7fc9eb 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -614,7 +614,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid context.llvm.fileInitializers.add(expression) } - LLVMSetLinkage(globalProperty, LLVMLinkage.LLVMPrivateLinkage) + LLVMSetLinkage(globalProperty, LLVMLinkage.LLVMInternalLinkage) // (Cannot do this before the global is initialized). return diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmDeclarations.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmDeclarations.kt index 7681cbef81e..08c7070c5f0 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmDeclarations.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmDeclarations.kt @@ -239,7 +239,7 @@ private class DeclarationsGeneratorVisitor(override val context: Context) : typeInfoSymbolName)!! if (!descriptor.isExported()) { - LLVMSetLinkage(llvmTypeInfoPtr, LLVMLinkage.LLVMPrivateLinkage) + LLVMSetLinkage(llvmTypeInfoPtr, LLVMLinkage.LLVMInternalLinkage) } typeInfoPtr = constPointer(llvmTypeInfoPtr) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/StaticData.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/StaticData.kt index 61ff132c58d..1721db48778 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/StaticData.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/StaticData.kt @@ -28,7 +28,7 @@ internal class StaticData(override val context: Context): ContextUtils { val llvmGlobal = LLVMAddGlobal(module, type, name)!! if (!isExported) { - LLVMSetLinkage(llvmGlobal, LLVMLinkage.LLVMPrivateLinkage) + LLVMSetLinkage(llvmGlobal, LLVMLinkage.LLVMInternalLinkage) } return llvmGlobal diff --git a/runtime/src/main/cpp/Memory.cpp b/runtime/src/main/cpp/Memory.cpp index 6d5f50e61f8..f4492dcdd1b 100644 --- a/runtime/src/main/cpp/Memory.cpp +++ b/runtime/src/main/cpp/Memory.cpp @@ -559,8 +559,7 @@ OBJ_GETTER(InitInstance, RETURN_OBJ(value); } - AllocInstance(type_info, OBJ_RESULT); - ObjHeader* object = *OBJ_RESULT; + ObjHeader* object = AllocInstance(type_info, OBJ_RESULT); UpdateGlobalRef(location, object); try { ctor(object); @@ -574,7 +573,7 @@ OBJ_GETTER(InitInstance, } catch (...) { UpdateLocalRef(OBJ_RESULT, nullptr); UpdateGlobalRef(location, nullptr); - RETURN_OBJ(nullptr); + throw; } } diff --git a/runtime/src/main/cpp/Memory.h b/runtime/src/main/cpp/Memory.h index fe21299e41f..afc9a99ad88 100644 --- a/runtime/src/main/cpp/Memory.h +++ b/runtime/src/main/cpp/Memory.h @@ -323,7 +323,7 @@ OBJ_GETTER(AllocInstance, const TypeInfo* type_info) RUNTIME_NOTHROW; OBJ_GETTER(AllocArrayInstance, const TypeInfo* type_info, uint32_t elements) RUNTIME_NOTHROW; OBJ_GETTER(AllocStringInstance, const char* data, uint32_t length) RUNTIME_NOTHROW; OBJ_GETTER(InitInstance, - ObjHeader** location, const TypeInfo* type_info, void (*ctor)(ObjHeader*)) RUNTIME_NOTHROW; + ObjHeader** location, const TypeInfo* type_info, void (*ctor)(ObjHeader*)); // // Object reference management. diff --git a/runtime/src/main/kotlin/kotlin/Array.kt b/runtime/src/main/kotlin/kotlin/Array.kt index b1b9bf9e46d..8acea4de9f7 100644 --- a/runtime/src/main/kotlin/kotlin/Array.kt +++ b/runtime/src/main/kotlin/kotlin/Array.kt @@ -46,7 +46,6 @@ private class IteratorImpl(val collection: Array) : Iterator { } } -public fun arrayOf(vararg elements: T) : Array = elements @kotlin.internal.InlineOnly public inline operator fun Array.plus(elements: Array): Array { diff --git a/runtime/src/main/kotlin/kotlin/Arrays.kt b/runtime/src/main/kotlin/kotlin/Arrays.kt index b5fa8b5985a..d9902de728f 100644 --- a/runtime/src/main/kotlin/kotlin/Arrays.kt +++ b/runtime/src/main/kotlin/kotlin/Arrays.kt @@ -1,6 +1,7 @@ package kotlin import kotlin.collections.* +import kotlin.internal.PureReifiable // TODO: make all iterator() methods inline. @@ -883,3 +884,122 @@ public fun BooleanArray.toHashSet(): HashSet { public fun CharArray.toHashSet(): HashSet { return toCollection(HashSet(mapCapacity(size))) } + +// From Library.kt. +/** + * Returns an array of objects of the given type with the given [size], initialized with null values. + */ +public inline fun arrayOfNulls(size: Int): Array = + @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") + arrayOfUninitializedElements(size) + +/** + * Returns an array containing the specified elements. + */ +public inline fun arrayOf(vararg elements: T): Array = elements as Array + +// TODO: optimize those operations. +/** + * Returns an array containing the specified [Double] numbers. + */ + +public fun doubleArrayOf(vararg elements: Double): DoubleArray { + val result = DoubleArray(elements.size) + var index = 0 + while (index < elements.size) { + result[index] = elements[index] + index++ + } + return result +} + +/** + * Returns an array containing the specified [Float] numbers. + */ +public fun floatArrayOf(vararg elements: Float): FloatArray { + val result = FloatArray(elements.size) + var index = 0 + while (index < elements.size) { + result[index] = elements[index] + index++ + } + return result +} + +/** + * Returns an array containing the specified [Long] numbers. + */ +public fun longArrayOf(vararg elements: Long): LongArray { + val result = LongArray(elements.size) + var index = 0 + while (index < elements.size) { + result[index] = elements[index] + index++ + } + return result +} + +/** + * Returns an array containing the specified [Int] numbers. + */ +public fun intArrayOf(vararg elements: Int): IntArray { + val result = IntArray(elements.size) + var index = 0 + while (index < elements.size) { + result[index] = elements[index] + index++ + } + return result +} + +/** + * Returns an array containing the specified characters. + */ +public fun charArrayOf(vararg elements: Char): CharArray { + val result = CharArray(elements.size) + var index = 0 + while (index < elements.size) { + result[index] = elements[index] + index++ + } + return result +} + +/** + * Returns an array containing the specified [Short] numbers. + */ +public fun shortArrayOf(vararg elements: Short): ShortArray { + val result = ShortArray(elements.size) + var index = 0 + while (index < elements.size) { + result[index] = elements[index] + index++ + } + return result +} + +/** + * Returns an array containing the specified [Byte] numbers. + */ +public fun byteArrayOf(vararg elements: Byte): ByteArray { + val result = ByteArray(elements.size) + var index = 0 + while (index < elements.size) { + result[index] = elements[index] + index++ + } + return result +} + +/** + * Returns an array containing the specified boolean values. + */ +public fun booleanArrayOf(vararg elements: Boolean): BooleanArray { + val result = BooleanArray(elements.size) + var index = 0 + while (index < elements.size) { + result[index] = elements[index] + index++ + } + return result +} \ No newline at end of file diff --git a/runtime/src/main/kotlin/kotlin/String.kt b/runtime/src/main/kotlin/kotlin/String.kt index 4a698a10dbf..0b1d9e953f3 100644 --- a/runtime/src/main/kotlin/kotlin/String.kt +++ b/runtime/src/main/kotlin/kotlin/String.kt @@ -43,6 +43,7 @@ public final class String : Comparable, CharSequence { // TODO: in big Kotlin this operations are in kotlin.kotlin_builtins. private val kNullString = "" + public operator fun kotlin.String?.plus(other: kotlin.Any?): kotlin.String = this?.plus(other?.toString() ?: kNullString) ?: other?.toString() ?: kNullString diff --git a/runtime/src/main/kotlin/kotlin/collections/HashMap.kt b/runtime/src/main/kotlin/kotlin/collections/HashMap.kt index a91e9a29281..7d56b7a517e 100644 --- a/runtime/src/main/kotlin/kotlin/collections/HashMap.kt +++ b/runtime/src/main/kotlin/kotlin/collections/HashMap.kt @@ -37,7 +37,12 @@ class HashMap private constructor( override fun containsKey(key: K): Boolean = findKey(key) >= 0 override fun containsValue(value: V): Boolean = findValue(value) >= 0 - override fun get(key: K): V? { + + operator fun set(key: K, value: V): Unit { + put(key, value) + } + + override operator fun get(key: K): V? { val index = findKey(key) if (index < 0) return null return valuesArray!![index] diff --git a/runtime/src/main/kotlin/kotlin/internal/Annotations.kt b/runtime/src/main/kotlin/kotlin/internal/Annotations.kt index 1c80ba3ed9c..83f2baad656 100644 --- a/runtime/src/main/kotlin/kotlin/internal/Annotations.kt +++ b/runtime/src/main/kotlin/kotlin/internal/Annotations.kt @@ -52,3 +52,11 @@ internal annotation class InlineOnly @Target(AnnotationTarget.CLASS, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY) @Retention(AnnotationRetention.BINARY) internal annotation class InlineExposed + +/** + * Specifies that the corresponding type parameter is not used for unsafe operations such as casts or 'is' checks + * That means it's completely safe to use generic types as argument for such parameter. + */ +@Target(AnnotationTarget.TYPE_PARAMETER) +@Retention(AnnotationRetention.BINARY) +internal annotation class PureReifiable