translator: single-expression function

This commit is contained in:
e5l
2016-07-22 13:26:17 +03:00
parent 2193c0c6dd
commit c243ab7fc0
4 changed files with 37 additions and 6 deletions
@@ -23,10 +23,30 @@ abstract class BlockCodegen(open val state: TranslationState, open val variableM
var returnType: LLVMVariable? = null
var wasReturnOnTopLevel = false
protected fun evaluateCodeBlock(expr: PsiElement?, startLabel: LLVMLabel? = null, finishLabel: LLVMLabel? = null, scopeDepth: Int = 0) {
protected fun evaluateCodeBlock(expr: PsiElement?, startLabel: LLVMLabel? = null, finishLabel: LLVMLabel? = null, scopeDepth: Int = 0, isBlock: Boolean = true) {
codeBuilder.markWithLabel(startLabel)
expressionWalker(expr, scopeDepth)
if (isBlock) {
expressionWalker(expr, scopeDepth)
} else {
var result = evaluateExpression(expr, scopeDepth)!!
when (result) {
is LLVMVariable -> {
if (result.pointer == 1 && result.type !is LLVMReferenceType) {
result = codeBuilder.loadAndGetVariable(result)
}
if (result.type is LLVMReferenceType) {
codeBuilder.addAnyReturn(LLVMVoidType())
} else {
codeBuilder.addReturnOperator(result)
}
}
else -> codeBuilder.addAnyReturn(result.type!!, result.toString())
}
wasReturnOnTopLevel = true
}
codeBuilder.addUnconditionalJump(finishLabel ?: return)
}
@@ -171,7 +191,7 @@ abstract class BlockCodegen(open val state: TranslationState, open val variableM
val names = parseArgList(call as KtCallExpression, scopeDepth)
val typePath = type.location.joinToString(".")
val types = if (names.size > 0) "_${names.joinToString(separator = "_", transform ={ it.type!!.mangle() })}" else ""
val types = if (names.size > 0) "_${names.joinToString(separator = "_", transform = { it.type!!.mangle() })}" else ""
val methodName = "${if (typePath.length > 0) "$typePath." else ""}${clazz.structName}.${selectorName.substringBefore('(')}$types"
val method = clazz.methods[methodName]!!
@@ -212,7 +232,7 @@ abstract class BlockCodegen(open val state: TranslationState, open val variableM
private fun evaluateReferenceExpression(expr: KtReferenceExpression, scopeDepth: Int, classScope: ClassCodegen? = null): LLVMSingleValue? = when (expr) {
is KtArrayAccessExpression -> evaluateArrayAccessExpression(expr, scopeDepth + 1)
else -> if ((expr is KtNameReferenceExpression) && (classScope != null)) evaluateNameReferenceExpression(expr, classScope)
else variableManager.getLLVMvalue(expr.firstChild.text)
else variableManager.getLLVMvalue(expr.firstChild.text)
}
private fun evaluateCallExpression(expr: KtCallExpression, scopeDepth: Int, classScope: ClassCodegen? = null): LLVMSingleValue? {
@@ -69,7 +69,7 @@ class FunctionCodegen(override val state: TranslationState,
codeBuilder.addStartExpression()
generateLoadArguments()
evaluateCodeBlock(function.bodyExpression, scopeDepth = topLevel)
evaluateCodeBlock(function.bodyExpression, scopeDepth = topLevel, isBlock = function.hasBlockBody())
if (!wasReturnOnTopLevel)
codeBuilder.addAnyReturn(returnType!!.type)
@@ -0,0 +1,3 @@
inc_Int(1) == 2
dinc_Int(1) == 3
sum_Int_Int(1, 1) == 2
@@ -0,0 +1,8 @@
class MyClass(val i: Int)
fun inc(i: Int) = i + 1
fun dinc(i: Int) = inc(inc(i))
fun sum(i: Int, j: Int) = i + j
fun gen() = MyClass(1)