interprocedural call implementation
This commit is contained in:
committed by
Vasily Levchenko
parent
31c022f0a6
commit
390edc146d
+7
@@ -1,5 +1,7 @@
|
||||
package org.jetbrains.kotlin.backend.native.llvm
|
||||
|
||||
|
||||
import kotlin_native.interop.*
|
||||
import llvm.*
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
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 load(value:LLVMOpaqueValue, varName: String):LLVMOpaqueValue = LLVMBuildLoad(context.llvmBuilder, value, varName)!!
|
||||
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
|
||||
|
||||
import llvm.*
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
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.acceptChildrenVoid
|
||||
|
||||
|
||||
fun emitLLVM(module: IrModuleFragment, runtimeFile: String, outFile: String) {
|
||||
val llvmModule = LLVMModuleCreateWithName("out")!! // 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))
|
||||
}
|
||||
})
|
||||
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)
|
||||
else -> {
|
||||
TODO()
|
||||
}
|
||||
}
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun visitVariable(declaration: IrVariable) {
|
||||
|
||||
Reference in New Issue
Block a user