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 returnType: LLVMVariable? = null
var wasReturnOnTopLevel = false var wasReturnOnTopLevel = false
protected fun evaluateCodeBlock(expr: PsiElement?, startLabel: LLVMLabel? = null, finishLabel: LLVMLabel? = null, scopeDepth: Int = 0, isBlock: Boolean = true) {
protected fun evaluateCodeBlock(expr: PsiElement?, startLabel: LLVMLabel? = null, finishLabel: LLVMLabel? = null, scopeDepth: Int = 0) {
codeBuilder.markWithLabel(startLabel) codeBuilder.markWithLabel(startLabel)
if (isBlock) {
expressionWalker(expr, scopeDepth) 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) codeBuilder.addUnconditionalJump(finishLabel ?: return)
} }
@@ -69,7 +69,7 @@ class FunctionCodegen(override val state: TranslationState,
codeBuilder.addStartExpression() codeBuilder.addStartExpression()
generateLoadArguments() generateLoadArguments()
evaluateCodeBlock(function.bodyExpression, scopeDepth = topLevel) evaluateCodeBlock(function.bodyExpression, scopeDepth = topLevel, isBlock = function.hasBlockBody())
if (!wasReturnOnTopLevel) if (!wasReturnOnTopLevel)
codeBuilder.addAnyReturn(returnType!!.type) 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)