translator: add BooleanArray and ShortArray, tests

This commit is contained in:
Alexey Stepanov
2016-08-08 12:48:33 +03:00
parent 2af51674ca
commit c26ea6b5ae
5 changed files with 128 additions and 0 deletions
@@ -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
}
}
@@ -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
}
}
@@ -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
}
@@ -0,0 +1,2 @@
booleanarray_1_Boolean(1) == 1
booleanarray_1_Boolean(0) == 0
@@ -0,0 +1,5 @@
fun booleanarray_1(x: Boolean): Boolean {
val z = BooleanArray(10)
z.set(1, x)
return z.get(1)
}