diff --git a/translator/src/main/resources/kotlib/kotlin/BooleanArray.kt b/translator/src/main/resources/kotlib/kotlin/BooleanArray.kt new file mode 100644 index 00000000000..ed72625ba31 --- /dev/null +++ b/translator/src/main/resources/kotlib/kotlin/BooleanArray.kt @@ -0,0 +1,42 @@ +class BooleanArray(var size: Int) { + val data: Int + + /** Returns the number of elements in the array. */ + //size: Int + + init { + this.data = malloc_array(this.size) + } + + /** Returns the array element at the given [index]. This method can be called using the index operator. */ + fun get(index: Int): Boolean { + val res = kotlinclib_get_byte(this.data, index) == 1.toByte() + return res + } + + + /** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */ + fun set(index: Int, value: Boolean) { + if (value == true) { + kotlinclib_set_byte(this.data, index, 1.toByte()) + } + else{ + kotlinclib_set_byte(this.data, index, 0.toByte()) + } + } + + + fun clone(): BooleanArray { + val newInstance = BooleanArray(this.size) + var index = 0 + while (index < this.size) { + val value = this.get(index) + newInstance.set(index, value) + index = index + 1 + } + + return newInstance + } + +} + diff --git a/translator/src/main/resources/kotlib/kotlin/ShortArray.kt b/translator/src/main/resources/kotlib/kotlin/ShortArray.kt new file mode 100644 index 00000000000..f27f087df32 --- /dev/null +++ b/translator/src/main/resources/kotlib/kotlin/ShortArray.kt @@ -0,0 +1,40 @@ +external fun kotlinclib_get_short(src: Int, index: Int): Short +external fun kotlinclib_set_short(src: Int, index: Int, value: Short) + + +class ShortArray(var size: Int) { + val data: Int + + /** Returns the number of elements in the array. */ + //size: Int + + init { + this.data = malloc_array(this.size) + } + + /** Returns the array element at the given [index]. This method can be called using the index operator. */ + fun get(index: Int): Short { + return kotlinclib_get_short(this.data, index) + } + + + /** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */ + fun set(index: Int, value: Short) { + kotlinclib_set_short(this.data, index, value) + } + + + fun clone(): ShortArray { + val newInstance = ShortArray(this.size) + var index = 0 + while (index < this.size) { + val value = this.get(index) + newInstance.set(index, value) + index = index + 1 + } + + return newInstance + } + +} + diff --git a/translator/src/main/resources/kotlib/linked/array_c.ll b/translator/src/main/resources/kotlib/linked/array_c.ll index 86b0524e2d8..84c683dc201 100644 --- a/translator/src/main/resources/kotlib/linked/array_c.ll +++ b/translator/src/main/resources/kotlib/linked/array_c.ll @@ -84,4 +84,43 @@ define void @kotlinclib_set_int(i32 %data, i32 %index, i32 %value) { %11 = getelementptr inbounds i32* %8, i64 %10 store i32 %7, i32* %11, align 4 ret void +} + + +; Function Attrs: nounwind uwtable +define signext i16 @kotlinclib_get_short(i32 %data, i32 %index) { + %1 = alloca i32, align 4 + %2 = alloca i32, align 4 + store i32 %data, i32* %1, align 4 + store i32 %index, i32* %2, align 4 + %3 = load i32* %1, align 4 + %4 = sext i32 %3 to i64 + %5 = inttoptr i64 %4 to i16* + %6 = load i32* %2, align 4 + %7 = sext i32 %6 to i64 + %8 = getelementptr inbounds i16* %5, i64 %7 + %9 = load i16* %8, align 2 + ret i16 %9 +} + +; Function Attrs: nounwind uwtable +define void @kotlinclib_set_short(i32 %data, i32 %index, i16 signext %value) { + %1 = alloca i32, align 4 + %2 = alloca i32, align 4 + %3 = alloca i16, align 2 + %ptr = alloca i16*, align 8 + store i32 %data, i32* %1, align 4 + store i32 %index, i32* %2, align 4 + store i16 %value, i16* %3, align 2 + %4 = load i32* %1, align 4 + %5 = sext i32 %4 to i64 + %6 = inttoptr i64 %5 to i16* + store i16* %6, i16** %ptr, align 8 + %7 = load i16* %3, align 2 + %8 = load i16** %ptr, align 8 + %9 = load i32* %2, align 4 + %10 = sext i32 %9 to i64 + %11 = getelementptr inbounds i16* %8, i64 %10 + store i16 %7, i16* %11, align 2 + ret void } \ No newline at end of file diff --git a/translator/src/test/kotlin/tests/input/booleanarray_1.txt b/translator/src/test/kotlin/tests/input/booleanarray_1.txt new file mode 100644 index 00000000000..b63930e4883 --- /dev/null +++ b/translator/src/test/kotlin/tests/input/booleanarray_1.txt @@ -0,0 +1,2 @@ +booleanarray_1_Boolean(1) == 1 +booleanarray_1_Boolean(0) == 0 diff --git a/translator/src/test/kotlin/tests/kotlin/booleanarray_1.kt b/translator/src/test/kotlin/tests/kotlin/booleanarray_1.kt new file mode 100644 index 00000000000..4934d1c7afe --- /dev/null +++ b/translator/src/test/kotlin/tests/kotlin/booleanarray_1.kt @@ -0,0 +1,5 @@ +fun booleanarray_1(x: Boolean): Boolean { + val z = BooleanArray(10) + z.set(1, x) + return z.get(1) +} \ No newline at end of file