TCO: Optimize all function calls, not only JetCallExpressions

This commit is contained in:
Andrey Breslav
2013-11-27 15:27:59 +04:00
parent 6d369b985f
commit 2a03d40233
2 changed files with 9 additions and 21 deletions
@@ -1593,17 +1593,6 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
public StackValue visitReturnExpression(@NotNull JetReturnExpression expression, StackValue receiver) {
JetExpression returnedExpression = expression.getReturnedExpression();
if (returnedExpression != null) {
if (returnedExpression instanceof JetCallExpression) {
JetCallExpression callExpression = (JetCallExpression) returnedExpression;
JetExpression calleeExpression = callExpression.getCalleeExpression();
if (calleeExpression != null) {
ResolvedCall<? extends CallableDescriptor> resolvedCall = bindingContext.get(BindingContext.RESOLVED_CALL, calleeExpression);
if (resolvedCall != null && tailRecursionGeneratorUtil.isTailRecursion(resolvedCall)) {
return tailRecursionGeneratorUtil.generateTailRecursion(resolvedCall, callExpression);
}
}
}
gen(returnedExpression, returnType);
doFinallyOnReturn();
v.areturn(returnType);
@@ -1933,10 +1922,6 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
}
}
if (tailRecursionGeneratorUtil.isTailRecursion(resolvedCall)) {
return tailRecursionGeneratorUtil.generateTailRecursion(resolvedCall, expression);
}
return invokeFunction(call, receiver, resolvedCall);
}
@@ -2011,6 +1996,10 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
StackValue receiver,
ResolvedCall<? extends CallableDescriptor> resolvedCall
) {
if (tailRecursionGeneratorUtil.isTailRecursion(resolvedCall)) {
return tailRecursionGeneratorUtil.generateTailRecursion(resolvedCall, call);
}
if (resolvedCall instanceof VariableAsFunctionResolvedCall) {
VariableAsFunctionResolvedCall variableAsFunctionResolvedCall = (VariableAsFunctionResolvedCall) resolvedCall;
ResolvedCallWithTrace<FunctionDescriptor> functionCall = variableAsFunctionResolvedCall.getFunctionCall();
@@ -17,7 +17,6 @@
package org.jetbrains.jet.codegen;
import com.google.common.collect.Lists;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
@@ -26,7 +25,7 @@ import org.jetbrains.jet.codegen.state.GenerationState;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.psi.JetCallExpression;
import org.jetbrains.jet.lang.psi.Call;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
import org.jetbrains.jet.lang.psi.ValueArgument;
@@ -66,7 +65,7 @@ public class TailRecursionGeneratorUtil {
return status != null && status.isDoGenerateTailRecursion();
}
public StackValue generateTailRecursion(ResolvedCall<? extends CallableDescriptor> resolvedCall, JetCallExpression callExpression) {
public StackValue generateTailRecursion(ResolvedCall<? extends CallableDescriptor> resolvedCall, Call call) {
CallableDescriptor fd = resolvedCall.getResultingDescriptor();
assert fd instanceof FunctionDescriptor : "the resolved call is not refer to the function descriptor so why do we use generateTailRecursion for something strange?";
CallableMethod callable = (CallableMethod) codegen.resolveToCallable((FunctionDescriptor) fd, false);
@@ -76,7 +75,7 @@ public class TailRecursionGeneratorUtil {
// we can't store values to the variables in the loop above because it will affect expressions evaluation
for (ValueParameterDescriptor parameterDescriptor : Lists.reverse(parametersStored)) {
Type asmType = types.get(parameterDescriptor.getIndex());
int index = getParameterVariableIndex(parameterDescriptor, callExpression);
int index = getParameterVariableIndex(parameterDescriptor, call);
v.store(index, asmType);
}
@@ -127,14 +126,14 @@ public class TailRecursionGeneratorUtil {
return descriptorsStored;
}
private int getParameterVariableIndex(ValueParameterDescriptor parameterDescriptor, PsiElement node) {
private int getParameterVariableIndex(ValueParameterDescriptor parameterDescriptor, Call call) {
int index = codegen.lookupLocalIndex(parameterDescriptor);
if (index == -1) {
index = codegen.lookupLocalIndex(parameterDescriptor.getOriginal());
}
if (index == -1) {
throw new CompilationException("Failed to obtain parameter index: " + parameterDescriptor.getName(), null, node);
throw new CompilationException("Failed to obtain parameter index: " + parameterDescriptor.getName(), null, call.getCallElement());
}
return index;