From 10fb92f9fa06a0fe09f84dd980e87996154b63d4 Mon Sep 17 00:00:00 2001 From: e5l Date: Mon, 8 Aug 2016 18:30:56 +0300 Subject: [PATCH] translator: fix malloc in ByteArray --- .../kotlinnative/translator/PropertyCodegen.kt | 3 ++- .../kotlinnative/translator/llvm/LLVMBuilder.kt | 15 +++++++++++++-- .../main/resources/kotlib/linked/x86/array_c.ll | 4 ++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/PropertyCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/PropertyCodegen.kt index 57a8dd35953..29fa64f1580 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/PropertyCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/PropertyCodegen.kt @@ -18,9 +18,10 @@ class PropertyCodegen(val state: TranslationState, val variableManager: Variable val value = varInfo.value if (kotlinType.nameIfStandardType != null) { val variableType = LLVMInstanceOfStandardType(property.name ?: return, kotlinType).type - val variable = LLVMVariable(property.name.toString(), variableType, property.name.toString(), LLVMVariableScope(), pointer = 1) + val variable = LLVMVariable(property.name.toString(), variableType, property.name.toString(), LLVMVariableScope()) variableManager.addGlobalVariable(property.name.toString(), variable) codeBuilder.defineGlobalVariable(variable, variableType.parseArg(value.toString())) + variable.pointer++ } } diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/LLVMBuilder.kt b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/LLVMBuilder.kt index cd2e6ae91ee..451bd866dcc 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/LLVMBuilder.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/LLVMBuilder.kt @@ -20,7 +20,18 @@ class LLVMBuilder(val arm: Boolean) { private fun initBuilder() { val declares = arrayOf( "declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture readonly, i64, i32, i1)", - "declare i8* @${if (arm) "malloc_static" else "malloc"}(i32)") + if (arm) "declare i8* @malloc_static(i32)" else + """ + declare i8* @malloc(i32) #0 + define i8* @malloc_static(i32 %size) #0 { + %1 = alloca i32, align 4 + store i32 %size, i32* %1, align 4 + %2 = load i32* %1, align 4 + %3 = call i8* @malloc(i32 %2) + ret i8* %3 + } + """) + declares.forEach { globalCode.appendln(it) } @@ -188,7 +199,7 @@ class LLVMBuilder(val arm: Boolean) { } fun makeStructInitializer(args: List, values: List) - = "{ ${ args.mapIndexed { i: Int, variable: LLVMVariable -> "${variable.type} ${values[i]}" }.joinToString() } }" + = "{ ${args.mapIndexed { i: Int, variable: LLVMVariable -> "${variable.type} ${values[i]}" }.joinToString()} }" fun loadAndGetVariable(source: LLVMVariable): LLVMVariable { assert(source.pointer > 0) diff --git a/translator/src/main/resources/kotlib/linked/x86/array_c.ll b/translator/src/main/resources/kotlib/linked/x86/array_c.ll index 84c683dc201..c92120c319e 100644 --- a/translator/src/main/resources/kotlib/linked/x86/array_c.ll +++ b/translator/src/main/resources/kotlib/linked/x86/array_c.ll @@ -1,10 +1,10 @@ -declare i8* @malloc(i32) #1 +declare i8* @malloc_static(i32) define i32 @malloc_array(i32 %x) #0 { %1 = alloca i32, align 4 store i32 %x, i32* %1, align 4 %2 = load i32* %1, align 4 - %3 = call i8* @malloc(i32 %2) + %3 = call i8* @malloc_static(i32 %2) %4 = ptrtoint i8* %3 to i32 ret i32 %4 }