From 527efa661ba1476c8667ed41c3c5f09e0ad3b397 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Tue, 6 Dec 2016 16:03:23 +0300 Subject: [PATCH] Boundary values and their toString() test. (#90) --- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 10 ++- backend.native/tests/build.gradle | 12 +++- .../tests/runtime/basic/tostring3.kt | 69 +++++++++++++++++++ .../runtime/{basic => collections}/array0.kt | 0 .../runtime/{basic => collections}/array1.kt | 0 runtime/src/main/kotlin/kotlin/Primitives.kt | 20 +++--- .../main/kotlin/kotlin/collections/HashMap.kt | 18 +---- 7 files changed, 100 insertions(+), 29 deletions(-) create mode 100644 backend.native/tests/runtime/basic/tostring3.kt rename backend.native/tests/runtime/{basic => collections}/array0.kt (100%) rename backend.native/tests/runtime/{basic => collections}/array1.kt (100%) 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 aca2e09f801..36bc112923d 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 @@ -620,6 +620,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid LLVMSetInitializer(objectPtr, codegen.kNullObjHeaderPtr) } + val bbCurrent = codegen.currentBlock val bbInit = codegen.basicBlock("label_init") val bbExit = codegen.basicBlock("label_continue") val onePtr = codegen.intToPtr(kImmInt64One, codegen.kObjHeaderPtr, codegen.newVar()) @@ -633,11 +634,16 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid val initFunction = value.descriptor.constructors.first { it.valueParameters.size == 0 } val ctor = codegen.llvmFunction(initFunction) val args = listOf(objectPtr, typeInfo, allocHint, ctor) - currentCodeContext.genCall(context.initInstanceFunction, args, codegen.newVar()) + val newValue = currentCodeContext.genCall( + context.initInstanceFunction, args, codegen.newVar()) codegen.br(bbExit) codegen.positionAtEnd(bbExit) - return objectVal + val valuePhi = codegen.phi(codegen.getLLVMType(value.type), codegen.newVar()) + codegen.addPhiIncoming(valuePhi, + bbCurrent to objectVal, bbInit to newValue) + + return valuePhi } //-------------------------------------------------------------------------// diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 37335410b0e..66fa6d98482 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -342,6 +342,14 @@ task tostring2(type: RunKonanTest) { source = "runtime/basic/tostring2.kt" } +task tostring3(type: RunKonanTest) { + goldValue = "-128\n127\n-32768\n32767\n" + + "-2147483648\n2147483647\n-9223372036854775808\n9223372036854775807\n" + + "1.17549E-38\n3.40282E+38\n-INF\nINF\nNAN\n" + + "4.94066E-324\n1.79769E+308\nINF\nINF\nNAN\n" + source = "runtime/basic/tostring3.kt" +} + task empty_substring(type: RunKonanTest) { goldValue = "\n" source = "runtime/basic/empty_substring.kt" @@ -349,12 +357,12 @@ task empty_substring(type: RunKonanTest) { task array0(type: RunKonanTest) { goldValue = "5\n6\n7\n8\n9\n10\n11\n12\n13\n" - source = "runtime/basic/array0.kt" + source = "runtime/collections/array0.kt" } task array1(type: RunKonanTest) { goldValue = "4 2\n1-1\n69\n38\n" - source = "runtime/basic/array1.kt" + source = "runtime/collections/array1.kt" } task if_else(type: UnitKonanTest) { diff --git a/backend.native/tests/runtime/basic/tostring3.kt b/backend.native/tests/runtime/basic/tostring3.kt new file mode 100644 index 00000000000..1b5af9096a2 --- /dev/null +++ b/backend.native/tests/runtime/basic/tostring3.kt @@ -0,0 +1,69 @@ +fun testByte() { + val values = ByteArray(2) + values[0] = Byte.MIN_VALUE + values[1] = Byte.MAX_VALUE + for (v in values) { + println(v) + } +} + +fun testShort() { + val values = ShortArray(2) + values[0] = Short.MIN_VALUE + values[1] = Short.MAX_VALUE + for (v in values) { + println(v) + } +} + +fun testInt() { + val values = IntArray(2) + values[0] = Int.MIN_VALUE + values[1] = Int.MAX_VALUE + for (v in values) { + println(v) + } +} + +fun testLong() { + val values = LongArray(2) + values[0] = Long.MIN_VALUE + values[1] = Long.MAX_VALUE + for (v in values) { + println(v) + } +} + +fun testFloat() { + val values = FloatArray(5) + values[0] = Float.MIN_VALUE + values[1] = Float.MAX_VALUE + values[2] = Float.NEGATIVE_INFINITY + values[3] = Float.POSITIVE_INFINITY + values[4] = Float.NaN + for (v in values) { + println(v) + } +} + +fun testDouble() { + val values = DoubleArray(5) + values[0] = Double.MIN_VALUE + values[1] = Double.MAX_VALUE + values[2] = Double.NEGATIVE_INFINITY + values[3] = Double.POSITIVE_INFINITY + values[4] = Double.NaN + for (v in values) { + println(v) + } +} + +fun main(args : Array) { + testByte() + testShort() + testInt() + testLong() + testFloat() + testDouble() + +} \ No newline at end of file diff --git a/backend.native/tests/runtime/basic/array0.kt b/backend.native/tests/runtime/collections/array0.kt similarity index 100% rename from backend.native/tests/runtime/basic/array0.kt rename to backend.native/tests/runtime/collections/array0.kt diff --git a/backend.native/tests/runtime/basic/array1.kt b/backend.native/tests/runtime/collections/array1.kt similarity index 100% rename from backend.native/tests/runtime/basic/array1.kt rename to backend.native/tests/runtime/collections/array1.kt diff --git a/runtime/src/main/kotlin/kotlin/Primitives.kt b/runtime/src/main/kotlin/kotlin/Primitives.kt index 5b80b103ccd..c6021b88487 100644 --- a/runtime/src/main/kotlin/kotlin/Primitives.kt +++ b/runtime/src/main/kotlin/kotlin/Primitives.kt @@ -915,27 +915,27 @@ public final class Float : Number(), Comparable { /** * A constant holding the smallest *positive* nonzero value of Float. */ - //public const val MIN_VALUE: Float + public const val MIN_VALUE: Float = 1.17549435E-38f /** * A constant holding the largest positive finite value of Float. */ - //public const val MAX_VALUE: Float + public const val MAX_VALUE: Float = 3.4028235E+38f /** * A constant holding the positive infinity value of Float. */ - //public val POSITIVE_INFINITY: Float + public val POSITIVE_INFINITY: Float = 1.0f / 0.0f /** * A constant holding the negative infinity value of Float. */ - //public const val NEGATIVE_INFINITY: Float + public val NEGATIVE_INFINITY: Float = -1.0f / 0.0f /** * A constant holding the "not a number" value of Float. */ - //public const val NaN: Float + public val NaN: Float = 0.0f / 0.0f } /** @@ -1125,27 +1125,27 @@ public final class Double : Number(), Comparable { /** * A constant holding the smallest *positive* nonzero value of Double. */ - //public const val MIN_VALUE: Double + public const val MIN_VALUE: Double = 4.9e-324 /** * A constant holding the largest positive finite value of Double. */ - //public const val MAX_VALUE: Double + public const val MAX_VALUE: Double = 1.7976931348623157e+308 /** * A constant holding the positive infinity value of Double. */ - // public const val POSITIVE_INFINITY: Double + public val POSITIVE_INFINITY: Double = 1.0 / 0.0 /** * A constant holding the negative infinity value of Double. */ - // public const val NEGATIVE_INFINITY: Double + public val NEGATIVE_INFINITY: Double = -1.0 / 0.0 /** * A constant holding the "not a number" value of Double. */ - // public const val NaN: Double + public val NaN: Double = 0.0 / 0.0 } /** diff --git a/runtime/src/main/kotlin/kotlin/collections/HashMap.kt b/runtime/src/main/kotlin/kotlin/collections/HashMap.kt index cc80e75f3d4..f968c6e901f 100644 --- a/runtime/src/main/kotlin/kotlin/collections/HashMap.kt +++ b/runtime/src/main/kotlin/kotlin/collections/HashMap.kt @@ -1,16 +1,5 @@ package kotlin.collections - -// TODO: remove -private val MAGIC: Long = 2654435769 // golden ratio -private val INITIAL_CAPACITY = 8 -private val INITIAL_MAX_PROBE_DISTANCE = 2 -private val TOMBSTONE = -1 - -private fun computeHashSize(capacity: Int): Int = (capacity.coerceAtLeast(1) * 3).highestOneBit() - -private fun computeShift(hashSize: Int): Int = hashSize.numberOfLeadingZeros() + 1 - class HashMap private constructor( private var keysArray: Array, private var valuesArray: Array?, // allocated only when actually used, always null in pure HashSet @@ -182,7 +171,7 @@ class HashMap private constructor( return newValuesArray } - private fun hash(key: K) = (key.hashCode() * MAGIC.toInt()) ushr hashShift + private fun hash(key: K) = (key.hashCode() * MAGIC) ushr hashShift private fun compact() { var i = 0 @@ -475,8 +464,7 @@ class HashMap private constructor( internal fun valuesIterator() = ValuesItr(this) internal fun entriesIterator() = EntriesItr(this) - // TODO: fix! - /* private companion object { + private companion object { const val MAGIC = 2654435769L.toInt() // golden ratio const val INITIAL_CAPACITY = 8 const val INITIAL_MAX_PROBE_DISTANCE = 2 @@ -485,7 +473,7 @@ class HashMap private constructor( fun computeHashSize(capacity: Int): Int = (capacity.coerceAtLeast(1) * 3).highestOneBit() fun computeShift(hashSize: Int): Int = hashSize.numberOfLeadingZeros() + 1 - } */ + } internal open class Itr( internal val map: HashMap