Base level translator: it could compile primitive functions with arithmetic operations:
note: operations are hardcoded e.g. operato plus resolved in corresponding instruction.
fun sum(a:Int, b:Int) = a + b
produces following code:
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.12.0"
define i32 @"kfun:sum"(i32, i32) {
entry:
%a = alloca i32
store i32 %0, i32* %a
%b = alloca i32
store i32 %1, i32* %b
%tmp1 = load i32, i32* %a
%tmp2 = load i32, i32* %b
%tmp0 = add i32 %tmp1, %tmp2
ret i32 %tmp0
}
Note: this code is optimizable with -mem2reg
> opt-mp-3.8 -mem2reg sum.BCkt -o - | llvm-dis-mp-3.8 -o -
; ModuleID = '<stdin>'
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.12.0"
define i32 @"kfun:sum"(i32, i32) {
entry:
%tmp0 = add i32 %0, %1
ret i32 %tmp0
}
This commit is contained in:
committed by
Vasily Levchenko
parent
436a950c65
commit
dbdbc9a844
+67
@@ -0,0 +1,67 @@
|
||||
package org.jetbrains.kotlin.backend.native.llvm
|
||||
|
||||
import llvm.*
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import java.util.*
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Created by minamoto on 07/10/2016.
|
||||
*/
|
||||
|
||||
internal class CodeGenerator(override val context:Context) : ContextUtils {
|
||||
var currentFunction:FunctionDescriptor? = null
|
||||
|
||||
fun function(declaration: IrFunction) {
|
||||
index = 0
|
||||
currentFunction = declaration.descriptor
|
||||
val fn = LLVMAddFunction(context.llvmModule, declaration.descriptor.symbolName, getLlvmFunctionType(declaration.descriptor))
|
||||
val block = LLVMAppendBasicBlock(fn, "entry")
|
||||
LLVMPositionBuilderAtEnd(context.llvmBuilder, block)
|
||||
function2variables.put(declaration.descriptor, mutableMapOf())
|
||||
declaration.descriptor.valueParameters.forEachIndexed { i, descriptor ->
|
||||
val name = descriptor.name.asString()
|
||||
val type = descriptor.type
|
||||
val v = alloca(type, name)
|
||||
store(LLVMGetParam(fn, i)!!, v)
|
||||
currentFunction!!.registerVariable(name, v)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun tmpVariable():String = currentFunction!!.tmpVariable()
|
||||
|
||||
val variablesGlobal = mapOf<String, LLVMOpaqueValue?>()
|
||||
fun variable(varName:String):LLVMOpaqueValue? = currentFunction!!.variable(varName)
|
||||
|
||||
fun FunctionDescriptor.param(num:Int):LLVMOpaqueValue? = LLVMGetParam(llvmFunction.getLlvmValue(), num)
|
||||
|
||||
var index:Int = 0
|
||||
private var FunctionDescriptor.tmpVariableIndex: Int
|
||||
get() = index
|
||||
set(i:Int){ index = i}
|
||||
|
||||
fun FunctionDescriptor.tmpVariable():String = "tmp${tmpVariableIndex++}"
|
||||
|
||||
fun registerVariable(varName: String, value:LLVMOpaqueValue) = currentFunction!!.registerVariable(varName, value)
|
||||
|
||||
val function2variables = mutableMapOf<FunctionDescriptor, MutableMap<String, LLVMOpaqueValue?>>()
|
||||
|
||||
|
||||
val FunctionDescriptor.variables: MutableMap<String, LLVMOpaqueValue?>
|
||||
get() = this@CodeGenerator.function2variables[this]!!
|
||||
|
||||
|
||||
fun FunctionDescriptor.registerVariable(varName: String, value:LLVMOpaqueValue?) = variables.put(varName, value)
|
||||
private fun FunctionDescriptor.variable(varName: String): LLVMOpaqueValue? = variables[varName]
|
||||
|
||||
fun plus(arg0:LLVMOpaqueValue, arg1:LLVMOpaqueValue, result:String):LLVMOpaqueValue = LLVMBuildAdd(context.llvmBuilder, arg0, arg1, result)!!
|
||||
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)!!
|
||||
}
|
||||
|
||||
|
||||
+72
-1
@@ -3,7 +3,10 @@ package org.jetbrains.kotlin.backend.native.llvm
|
||||
import llvm.*
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||
import org.jetbrains.kotlin.ir.declarations.IrVariable
|
||||
import org.jetbrains.kotlin.ir.expressions.*
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
||||
|
||||
@@ -16,6 +19,7 @@ fun emitLLVM(module: IrModuleFragment, runtimeFile: String, outFile: String) {
|
||||
val context = Context(module, runtime, llvmModule) // TODO: dispose
|
||||
|
||||
module.accept(RTTIGeneratorVisitor(context), null)
|
||||
module.accept(CodeGeneratorVisitor(context), null)
|
||||
LLVMWriteBitcodeToFile(llvmModule, outFile)
|
||||
}
|
||||
|
||||
@@ -31,4 +35,71 @@ internal class RTTIGeneratorVisitor(context: Context) : IrElementVisitorVoid {
|
||||
generator.generate(declaration.descriptor)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: split CodeGeneratorVisitor on Visitor and Generator :)
|
||||
*/
|
||||
internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid {
|
||||
val generator = CodeGenerator(context)
|
||||
override fun visitFunction(declaration: IrFunction) {
|
||||
generator.function(declaration)
|
||||
declaration.acceptChildrenVoid(this)
|
||||
}
|
||||
|
||||
override fun visitElement(element: IrElement) {
|
||||
element.acceptChildrenVoid(this)
|
||||
}
|
||||
|
||||
override fun visitSetVariable(expression: IrSetVariable) {
|
||||
val value = evaluateExpression(generator.tmpVariable(), expression.value)
|
||||
generator.store(value!!, generator.variable(expression.descriptor.name.asString())!!)
|
||||
}
|
||||
|
||||
private fun evaluateExpression(tmpVariableName: String, value: IrExpression?): LLVMOpaqueValue? {
|
||||
when (value) {
|
||||
is IrCall -> return evaluateCall(tmpVariableName, value)
|
||||
is IrGetValue -> return generator.load(generator.variable(value.descriptor.name.asString())!!, tmpVariableName)
|
||||
null -> return null
|
||||
else -> {
|
||||
TODO()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun evaluateCall(tmpVariableName: String, value: IrCall?): LLVMOpaqueValue? {
|
||||
/* TODO: should we count on receiver?
|
||||
* val tmp = tmpVar()
|
||||
* val lhs = evaluateExpression(tmp, value.dispatchReceiver!!)
|
||||
*/
|
||||
val args = mutableListOf<LLVMOpaqueValue?>()
|
||||
value!!.acceptChildrenVoid(object:IrElementVisitorVoid{
|
||||
override fun visitElement(element: IrElement) {
|
||||
val tmp = generator.tmpVariable()
|
||||
args.add(evaluateExpression(tmp, element as IrExpression))
|
||||
}
|
||||
})
|
||||
when (value!!.origin) {
|
||||
IrStatementOrigin.PLUS -> return generator.plus(args[0]!!, args[1]!!, tmpVariableName)
|
||||
else -> {
|
||||
TODO()
|
||||
}
|
||||
}
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun visitVariable(declaration: IrVariable) {
|
||||
val variableName = declaration.descriptor.name.asString()
|
||||
val variableType = declaration.descriptor.type
|
||||
generator.registerVariable(variableName, generator.alloca(variableType, variableName))
|
||||
|
||||
evaluateExpression(variableName, declaration.initializer)
|
||||
}
|
||||
|
||||
override fun visitReturn(expression: IrReturn) {
|
||||
val tmpVarName = generator.tmpVariable()
|
||||
val value:LLVMOpaqueValue?
|
||||
value = evaluateExpression(tmpVarName, expression.value)
|
||||
LLVMBuildRet(context.llvmBuilder, value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
int foo() { return 1; }
|
||||
int bar() {return 2;}
|
||||
int sum() {return foo() + bar();}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo():Int = 1
|
||||
//fun bar():Int = 2
|
||||
//fun sum():Int = foo() + bar()
|
||||
@@ -0,0 +1,5 @@
|
||||
fun sum(a:Int, b:Int):Int {
|
||||
var c:Int
|
||||
c = a + b
|
||||
return c
|
||||
}
|
||||
Reference in New Issue
Block a user