Merge pull request #3 from JetBrains/rr/interfunctional-call-proposal
inter-procedural call proposal
This commit is contained in:
+7
@@ -1,5 +1,7 @@
|
|||||||
package org.jetbrains.kotlin.backend.native.llvm
|
package org.jetbrains.kotlin.backend.native.llvm
|
||||||
|
|
||||||
|
|
||||||
|
import kotlin_native.interop.*
|
||||||
import llvm.*
|
import llvm.*
|
||||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||||
@@ -55,6 +57,11 @@ internal class CodeGenerator(override val context:Context) : ContextUtils {
|
|||||||
fun alloca(type: KotlinType, varName: String):LLVMOpaqueValue = LLVMBuildAlloca(context.llvmBuilder, getLLVMType(type), varName)!!
|
fun alloca(type: KotlinType, varName: String):LLVMOpaqueValue = LLVMBuildAlloca(context.llvmBuilder, getLLVMType(type), varName)!!
|
||||||
fun load(value:LLVMOpaqueValue, varName: String):LLVMOpaqueValue = LLVMBuildLoad(context.llvmBuilder, value, varName)!!
|
fun load(value:LLVMOpaqueValue, varName: String):LLVMOpaqueValue = LLVMBuildLoad(context.llvmBuilder, value, varName)!!
|
||||||
fun store(value:LLVMOpaqueValue, ptr:LLVMOpaqueValue):LLVMOpaqueValue = LLVMBuildStore(context.llvmBuilder, value, ptr)!!
|
fun store(value:LLVMOpaqueValue, ptr:LLVMOpaqueValue):LLVMOpaqueValue = LLVMBuildStore(context.llvmBuilder, value, ptr)!!
|
||||||
|
fun call(descriptor: FunctionDescriptor, args: MutableList<LLVMOpaqueValue?>, result: String): LLVMOpaqueValue? {
|
||||||
|
val rargs = malloc(array[args.size](Ref to LLVMOpaqueValue))
|
||||||
|
args.forEachIndexed { i, llvmOpaqueValue -> rargs[i].value = args[i]}
|
||||||
|
return LLVMBuildCall(context.llvmBuilder, descriptor.llvmFunction.getLlvmValue(), rargs[0], args.size, result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+29
-2
@@ -1,6 +1,7 @@
|
|||||||
package org.jetbrains.kotlin.backend.native.llvm
|
package org.jetbrains.kotlin.backend.native.llvm
|
||||||
|
|
||||||
import llvm.*
|
import llvm.*
|
||||||
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
import org.jetbrains.kotlin.ir.IrElement
|
import org.jetbrains.kotlin.ir.IrElement
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||||
@@ -10,6 +11,7 @@ import org.jetbrains.kotlin.ir.expressions.*
|
|||||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
||||||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
||||||
|
|
||||||
|
|
||||||
fun emitLLVM(module: IrModuleFragment, runtimeFile: String, outFile: String) {
|
fun emitLLVM(module: IrModuleFragment, runtimeFile: String, outFile: String) {
|
||||||
val llvmModule = LLVMModuleCreateWithName("out")!! // TODO: dispose
|
val llvmModule = LLVMModuleCreateWithName("out")!! // TODO: dispose
|
||||||
val runtime = Runtime(runtimeFile) // TODO: dispose
|
val runtime = Runtime(runtimeFile) // TODO: dispose
|
||||||
@@ -76,13 +78,38 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
|
|||||||
args.add(evaluateExpression(tmp, element as IrExpression))
|
args.add(evaluateExpression(tmp, element as IrExpression))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
when (value!!.origin) {
|
when (value.descriptor) {
|
||||||
|
is FunctionDescriptor -> return evaluateFunctionCall(tmpVariableName, value, args)
|
||||||
|
else -> {
|
||||||
|
TODO()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TODO()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun evaluateSimpleFunctionCall(tmpVariableName: String, value: IrCall, args: MutableList<LLVMOpaqueValue?>): LLVMOpaqueValue? {
|
||||||
|
return generator.call(value.descriptor as org.jetbrains.kotlin.descriptors.FunctionDescriptor, args, tmpVariableName)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun evaluateFunctionCall(tmpVariableName: String, callee: IrCall, args: MutableList<LLVMOpaqueValue?>): LLVMOpaqueValue? {
|
||||||
|
val descriptor:FunctionDescriptor = callee.descriptor as FunctionDescriptor
|
||||||
|
when {
|
||||||
|
descriptor.isOperator -> return evaluateOperatorCall(tmpVariableName, callee, args)
|
||||||
|
else -> {
|
||||||
|
return evaluateSimpleFunctionCall(tmpVariableName, callee, args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun evaluateOperatorCall(tmpVariableName: String, callee: IrCall, args: MutableList<LLVMOpaqueValue?>): LLVMOpaqueValue {
|
||||||
|
when (callee!!.origin) {
|
||||||
IrStatementOrigin.PLUS -> return generator.plus(args[0]!!, args[1]!!, tmpVariableName)
|
IrStatementOrigin.PLUS -> return generator.plus(args[0]!!, args[1]!!, tmpVariableName)
|
||||||
else -> {
|
else -> {
|
||||||
TODO()
|
TODO()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TODO()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitVariable(declaration: IrVariable) {
|
override fun visitVariable(declaration: IrVariable) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
TESTS := sum
|
TESTS := sum \
|
||||||
|
sum_foo_bar
|
||||||
|
|
||||||
BIN_TESTS=$(foreach t, ${TESTS},${t}_test)
|
BIN_TESTS=$(foreach t, ${TESTS},${t}_test)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
extern void *resolve_symbol(const char*);
|
||||||
|
|
||||||
|
int
|
||||||
|
run_test() {
|
||||||
|
int (*sum)(int, int) = resolve_symbol("kfun:sumFooBar");
|
||||||
|
|
||||||
|
if (sum(2, 3) != 5) return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
fun foo(a:Int):Int = a
|
||||||
|
fun bar(a:Int):Int = a
|
||||||
|
|
||||||
|
fun sumFooBar(a:Int, b:Int):Int = foo(a) + bar(b)
|
||||||
Reference in New Issue
Block a user