translator: add IntArray, fix llvm of ByteArray

This commit is contained in:
Alexey Stepanov
2016-08-08 11:52:22 +03:00
parent d336520b64
commit 0aa37ee43a
4 changed files with 90 additions and 8 deletions
@@ -0,0 +1,40 @@
external fun kotlinclib_get_int(src: Int, index: Int): Int
external fun kotlinclib_set_int(src: Int, index: Int, value: Int)
class IntArray(var size: Int) {
val data: Int
/** Returns the number of elements in the array. */
//val size: Int
init {
this.data = malloc_array(4 * this.size)
}
/** Returns the array element at the given [index]. This method can be called using the index operator. */
fun get(index: Int): Int {
return kotlinclib_get_int(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: Int) {
kotlinclib_set_int(this.data, index, value)
}
fun clone(): IntArray {
val newInstance = IntArray(this.size)
var index = 0
while (index < this.size) {
val value = this.get(index)
newInstance.set(index, value)
index = index + 1
}
return newInstance
}
}
@@ -13,19 +13,16 @@ define i32 @malloc_array(i32 %x) #0 {
define signext i8 @kotlinclib_get_byte(i32 %data, i32 %index) {
%1 = alloca i32, align 4
%2 = alloca i32, align 4
%ptr = alloca i8*, align 8
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 i8*
store i8* %5, i8** %ptr, align 8
%6 = load i8** %ptr, align 8
%7 = load i32* %2, align 4
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8* %6, i64 %8
%10 = load i8* %9, align 1
ret i8 %10
%6 = load i32* %2, align 4
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8* %5, i64 %7
%9 = load i8* %8, align 1
ret i8 %9
}
; Function Attrs: nounwind uwtable
@@ -48,4 +45,43 @@ define void @kotlinclib_set_byte(i32 %data, i32 %index, i8 signext %value) {
%11 = getelementptr inbounds i8* %8, i64 %10
store i8 %7, i8* %11, align 1
ret void
}
; Function Attrs: nounwind uwtable
define i32 @kotlinclib_get_int(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 i32*
%6 = load i32* %2, align 4
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i32* %5, i64 %7
%9 = load i32* %8, align 4
ret i32 %9
}
; Function Attrs: nounwind uwtable
define void @kotlinclib_set_int(i32 %data, i32 %index, i32 %value) {
%1 = alloca i32, align 4
%2 = alloca i32, align 4
%3 = alloca i32, align 4
%ptr = alloca i32*, align 8
store i32 %data, i32* %1, align 4
store i32 %index, i32* %2, align 4
store i32 %value, i32* %3, align 4
%4 = load i32* %1, align 4
%5 = sext i32 %4 to i64
%6 = inttoptr i64 %5 to i32*
store i32* %6, i32** %ptr, align 8
%7 = load i32* %3, align 4
%8 = load i32** %ptr, align 8
%9 = load i32* %2, align 4
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i32* %8, i64 %10
store i32 %7, i32* %11, align 4
ret void
}
@@ -0,0 +1 @@
intarray_1_Int(123) == 123
@@ -0,0 +1,5 @@
fun intarray_1(x: Int): Int {
val z = IntArray(10)
z.set(1, x)
return z.get(1)
}