translator: single-expression function
This commit is contained in:
@@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +191,7 @@ abstract class BlockCodegen(open val state: TranslationState, open val variableM
|
|||||||
|
|
||||||
val names = parseArgList(call as KtCallExpression, scopeDepth)
|
val names = parseArgList(call as KtCallExpression, scopeDepth)
|
||||||
val typePath = type.location.joinToString(".")
|
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 methodName = "${if (typePath.length > 0) "$typePath." else ""}${clazz.structName}.${selectorName.substringBefore('(')}$types"
|
||||||
|
|
||||||
val method = clazz.methods[methodName]!!
|
val method = clazz.methods[methodName]!!
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
Reference in New Issue
Block a user