diff --git a/car_llvmkot/kotlib/kotlin_main.ll b/car_llvmkot/kotlib/kotlin_main.ll index d8e32d0047e..e0b9dce0fcf 100644 --- a/car_llvmkot/kotlib/kotlin_main.ll +++ b/car_llvmkot/kotlib/kotlin_main.ll @@ -1,60 +1,14 @@ declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture readonly, i64, i32, i1) declare i8* @malloc_static(i32) attributes #0 = { nounwind "stack-protector-buffer-size"="8" "target-cpu"="cortex-m3" "target-features"="+hwdiv,+strict-align" } -%class.ComplexRef = type { i32, %class.Simple* } -define void @ComplexRef(%class.ComplexRef* %classvariable.this, i32 %i, %class.Simple* %s) #0 +define i32 @reassigment_1(i32 %x) #0 { -%classvariable.this.addr = alloca %class.ComplexRef, align 4 -%i.addr = alloca i32, align 4 -store i32 %i, i32* %i.addr, align 4 -%var1 = load i32* %i.addr, align 4 -%var2 = getelementptr inbounds %class.ComplexRef* %classvariable.this.addr, i32 0, i32 0 -store i32 %var1, i32* %var2, align 4 -%var3 = getelementptr inbounds %class.ComplexRef* %classvariable.this.addr, i32 0, i32 1 -store %class.Simple* %s, %class.Simple** %var3, align 4 -%var4 = bitcast %class.ComplexRef* %classvariable.this to i8* -%var5 = bitcast %class.ComplexRef* %classvariable.this.addr to i8* -call void @llvm.memcpy.p0i8.p0i8.i64(i8* %var4, i8* %var5, i64 8, i32 4, i1 false) -ret void -} -%class.Simple = type { i32 } -define void @Simple(%class.Simple* %classvariable.this, i32 %i) #0 -{ -%classvariable.this.addr = alloca %class.Simple, align 4 -%i.addr = alloca i32, align 4 -store i32 %i, i32* %i.addr, align 4 -%var6 = load i32* %i.addr, align 4 -%var7 = getelementptr inbounds %class.Simple* %classvariable.this.addr, i32 0, i32 0 -store i32 %var6, i32* %var7, align 4 -%var8 = bitcast %class.Simple* %classvariable.this to i8* -%var9 = bitcast %class.Simple* %classvariable.this.addr to i8* -call void @llvm.memcpy.p0i8.p0i8.i64(i8* %var8, i8* %var9, i64 4, i32 4, i1 false) -ret void -} -define void @myFunction(%class.ComplexRef* %ref) #0 -{ -%var10 = getelementptr inbounds %class.ComplexRef* %ref, i32 0, i32 0 -%var11 = load i32* %var10, align 4 -store i32 1, i32* %var10, align 4 -ret void -} -define i32 @const(i32 %i) #0 -{ -%i.addr = alloca i32, align 4 -store i32 %i, i32* %i.addr, align 4 -%var12 = load i32* %i.addr, align 4 -ret i32 %var12 -} -define void @kotlin_main() #0 -{ -%var13 = add nsw i32 3, 4 -%var14 = call i32 @const(i32 %var13) -%var15 = alloca i32, align 4 -store i32 %var14, i32* %var15, align 4 -%var16 = load i32* %var15, align 4 -%var17 = call i32 @const(i32 %var16) -%var18 = alloca i32, align 4 -store i32 %var17, i32* %var18, align 4 -ret void +%x.addr = alloca i32, align 4 +store i32 %x, i32* %x.addr, align 4 +%var1 = load i32* %x.addr, align 4 +%var2 = add nsw i32 %var1, 1 +%var3 = add nsw i32 %var2, 1 +store i32 %var3, i32 %var2, align 4 +ret i32 %var2 } diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/FunctionCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/FunctionCodegen.kt index 69fbb2ffd21..ff7744536e8 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/FunctionCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/FunctionCodegen.kt @@ -199,9 +199,7 @@ class FunctionCodegen(val state: TranslationState, val variableManager: Variable val names = parseArgList(expr.lastChild as KtCallExpression, scopeDepth) methodArgs.addAll(names) - return evaluateFunctionCallExpression(LLVMVariable(methodName, returnType, scope = LLVMVariableScope()), methodArgs, method.args) - - + return evaluateFunctionCallExpression(LLVMVariable(methodName, returnType, scope = LLVMVariableScope()), loadArgsIfRequired(methodArgs, method.args)) } } @@ -226,36 +224,27 @@ class FunctionCodegen(val state: TranslationState, val variableManager: Variable if (state.functions.containsKey(function)) { val descriptor = state.functions[function] ?: return null - return evaluateFunctionCallExpression(LLVMVariable(function, descriptor.returnType.type, scope = LLVMVariableScope()), names, descriptor.args) + val args = loadArgsIfRequired(names, descriptor.args) + return evaluateFunctionCallExpression(LLVMVariable(function, descriptor.returnType.type, scope = LLVMVariableScope()), args) } if (state.classes.containsKey(function)) { val descriptor = state.classes[function] ?: return null - return evaluateConstructorCallExpression(LLVMVariable(function, descriptor.type, scope = LLVMVariableScope()), names) + val args = loadArgsIfRequired(names, descriptor.fields) + return evaluateConstructorCallExpression(LLVMVariable(function, descriptor.type, scope = LLVMVariableScope()), args) } val localFunction = variableManager.getLLVMvalue(function) if (localFunction != null) { val type = localFunction.type as LLVMFunctionType - return evaluateFunctionCallExpression(LLVMVariable(function, type.returnType.type, scope = LLVMRegisterScope()), names, type.arguments) + val args = loadArgsIfRequired(names, type.arguments) + return evaluateFunctionCallExpression(LLVMVariable(function, type.returnType.type, scope = LLVMRegisterScope()), args) } return null } - private fun evaluateFunctionCallExpression(function: LLVMVariable, names: List, args: List): LLVMSingleValue? { - - val names = names.mapIndexed(fun(i: Int, value: LLVMSingleValue): LLVMSingleValue { - var result = value - - if (result.pointer > 0 && args[i].pointer == 0) { - result = codeBuilder.getNewVariable(args[i].type) - codeBuilder.loadVariable(result, value as LLVMVariable) - } - - return result - }).toList() - + private fun evaluateFunctionCallExpression(function: LLVMVariable, names: List): LLVMSingleValue? { val returnType = function.type when (returnType) { is LLVMVoidType -> { @@ -288,7 +277,18 @@ class FunctionCodegen(val state: TranslationState, val variableManager: Variable return null } - private fun evaluateConstructorCallExpression(function: LLVMVariable, names: ArrayList): LLVMSingleValue? { + private fun loadArgsIfRequired(names: List, args: List) = names.mapIndexed(fun(i: Int, value: LLVMSingleValue): LLVMSingleValue { + var result = value + + if (result.pointer > 0 && args[i].pointer == 0) { + result = codeBuilder.getNewVariable(args[i].type) + codeBuilder.loadVariable(result, value as LLVMVariable) + } + + return result + }).toList() + + private fun evaluateConstructorCallExpression(function: LLVMVariable, names: List): LLVMSingleValue? { val result = codeBuilder.getNewVariable(function.type, pointer = 1) codeBuilder.allocStaticVar(result) @@ -401,7 +401,6 @@ class FunctionCodegen(val state: TranslationState, val variableManager: Variable val elseKeyword = thenExpression.getNextSiblingIgnoringWhitespaceAndComments() ?: return null val elseExpression = elseKeyword.getNextSiblingIgnoringWhitespaceAndComments() ?: return null - return when (containReturn) { false -> executeIfBlock(condition.firstChild as KtBinaryExpression, thenExpression.firstChild, elseExpression.firstChild, scopeDepth + 1) true -> executeIfExpression(condition.firstChild as KtBinaryExpression, thenExpression.firstChild, elseExpression.firstChild, scopeDepth + 1) diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/LLVMCall.kt b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/LLVMCall.kt index 02a435b1290..0b115e51b73 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/LLVMCall.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/LLVMCall.kt @@ -2,7 +2,7 @@ package org.kotlinnative.translator.llvm import org.kotlinnative.translator.llvm.types.LLVMType -class LLVMCall(val returnType: LLVMType, val name: String, val arguments: List) : LLVMSingleValue(returnType) { +class LLVMCall(val returnType: LLVMType, val name: String, val arguments: Collection) : LLVMSingleValue(returnType) { override fun toString(): String = "call $returnType $name(${arguments.joinToString { "${it.getType()} ${it.toString()}" }})"