Control-Flow: Assign pseudo-values to local functions (except those

declared directly in the block)
This commit is contained in:
Alexey Sedunov
2015-04-20 14:58:41 +03:00
parent 5b1906fb13
commit d815634233
16 changed files with 388 additions and 9 deletions
@@ -111,7 +111,7 @@ public interface JetControlFlowBuilder {
@NotNull
InstructionWithValue createAnonymousObject(@NotNull JetObjectLiteralExpression expression);
@NotNull
InstructionWithValue createFunctionLiteral(@NotNull JetFunctionLiteralExpression expression);
InstructionWithValue createLambda(@NotNull JetFunction expression);
@NotNull
InstructionWithValue loadStringTemplate(@NotNull JetStringTemplateExpression expression, @NotNull List<PseudoValue> inputValues);
@@ -55,8 +55,8 @@ public abstract class JetControlFlowBuilderAdapter implements JetControlFlowBuil
@NotNull
@Override
public InstructionWithValue createFunctionLiteral(@NotNull JetFunctionLiteralExpression expression) {
return getDelegateBuilder().createFunctionLiteral(expression);
public InstructionWithValue createLambda(@NotNull JetFunction expression) {
return getDelegateBuilder().createLambda(expression);
}
@NotNull
@@ -1093,17 +1093,25 @@ public class JetControlFlowProcessor {
return parent.getParent() instanceof JetDoWhileExpression;
}
private void visitFunction(@NotNull JetFunction function) {
processLocalDeclaration(function);
boolean isAnonymousFunction = function instanceof JetFunctionLiteral || function.getName() == null;
if (isAnonymousFunction || (function.isLocal() && !(function.getParent() instanceof JetBlockExpression))) {
builder.createLambda(function);
}
}
@Override
public void visitNamedFunction(@NotNull JetNamedFunction function) {
processLocalDeclaration(function);
visitFunction(function);
}
@Override
public void visitFunctionLiteralExpression(@NotNull JetFunctionLiteralExpression expression) {
mark(expression);
JetFunctionLiteral functionLiteral = expression.getFunctionLiteral();
processLocalDeclaration(functionLiteral);
builder.createFunctionLiteral(expression);
visitFunction(functionLiteral);
copyValue(functionLiteral, expression);
}
@Override
@@ -425,8 +425,8 @@ public class JetControlFlowInstructionsGenerator extends JetControlFlowBuilderAd
@NotNull
@Override
public InstructionWithValue createFunctionLiteral(@NotNull JetFunctionLiteralExpression expression) {
return read(expression);
public InstructionWithValue createLambda(@NotNull JetFunction expression) {
return read(expression instanceof JetFunctionLiteral ? (JetFunctionLiteralExpression) expression.getParent() : expression);
}
@NotNull
@@ -0,0 +1,80 @@
== test ==
fun test(b: Boolean): (Int) -> Int {
if (b) {
fun(n: Int) = n + 1
}
else {
fun(n: Int) = n - 1
}
}
---------------------
L0:
1 <START>
v(b: Boolean)
magic[FAKE_INITIALIZER](b: Boolean) -> <v0>
w(b|<v0>)
2 mark({ if (b) { fun(n: Int) = n + 1 } else { fun(n: Int) = n - 1 } })
mark(if (b) { fun(n: Int) = n + 1 } else { fun(n: Int) = n - 1 })
r(b) -> <v1>
jf(L2|<v1>) NEXT:[mark({ fun(n: Int) = n - 1 }), mark({ fun(n: Int) = n + 1 })]
3 mark({ fun(n: Int) = n + 1 })
jmp?(L3) NEXT:[r(fun(n: Int) = n + 1) -> <v2>, d(fun(n: Int) = n + 1)]
d(fun(n: Int) = n + 1) NEXT:[<SINK>]
L3 [after local declaration]:
r(fun(n: Int) = n + 1) -> <v2> PREV:[jmp?(L3)]
2 jmp(L6) NEXT:[merge(if (b) { fun(n: Int) = n + 1 } else { fun(n: Int) = n - 1 }|<v2>, <v3>) -> <v4>]
L2 [else branch]:
3 mark({ fun(n: Int) = n - 1 }) PREV:[jf(L2|<v1>)]
jmp?(L7) NEXT:[r(fun(n: Int) = n - 1) -> <v3>, d(fun(n: Int) = n - 1)]
d(fun(n: Int) = n - 1) NEXT:[<SINK>]
L7 [after local declaration]:
r(fun(n: Int) = n - 1) -> <v3> PREV:[jmp?(L7)]
L6 ['if' expression result]:
2 merge(if (b) { fun(n: Int) = n + 1 } else { fun(n: Int) = n - 1 }|<v2>, <v3>) -> <v4> PREV:[jmp(L6), r(fun(n: Int) = n - 1) -> <v3>]
L1:
1 <END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>, d(fun(n: Int) = n + 1), d(fun(n: Int) = n - 1)]
=====================
== anonymous_0 ==
fun(n: Int) = n + 1
---------------------
L4:
4 <START>
v(n: Int)
magic[FAKE_INITIALIZER](n: Int) -> <v0>
w(n|<v0>)
r(n) -> <v1>
r(1) -> <v2>
mark(n + 1)
call(n + 1, plus|<v1>, <v2>) -> <v3>
ret(*|<v3>) L5
L5:
<END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
== anonymous_1 ==
fun(n: Int) = n - 1
---------------------
L8:
4 <START>
v(n: Int)
magic[FAKE_INITIALIZER](n: Int) -> <v0>
w(n|<v0>)
r(n) -> <v1>
r(1) -> <v2>
mark(n - 1)
call(n - 1, minus|<v1>, <v2>) -> <v3>
ret(*|<v3>) L9
L9:
<END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
@@ -0,0 +1,8 @@
fun test(b: Boolean): (Int) -> Int {
if (b) {
fun(n: Int) = n + 1
}
else {
fun(n: Int) = n - 1
}
}
@@ -0,0 +1,35 @@
== test ==
fun test(b: Boolean): (Int) -> Int {
if (b) {
fun(n: Int) = n + 1
}
else {
fun(n: Int) = n - 1
}
}
---------------------
<v0>: Boolean NEW: magic[FAKE_INITIALIZER](b: Boolean) -> <v0>
b <v1>: Boolean NEW: r(b) -> <v1>
fun(n: Int) = n + 1 <v2>: * NEW: r(fun(n: Int) = n + 1) -> <v2>
{ fun(n: Int) = n + 1 } <v2>: * COPY
fun(n: Int) = n - 1 <v3>: * NEW: r(fun(n: Int) = n - 1) -> <v3>
{ fun(n: Int) = n - 1 } <v3>: * COPY
if (b) { fun(n: Int) = n + 1 } else { fun(n: Int) = n - 1 } <v4>: * NEW: merge(if (b) { fun(n: Int) = n + 1 } else { fun(n: Int) = n - 1 }|<v2>, <v3>) -> <v4>
{ if (b) { fun(n: Int) = n + 1 } else { fun(n: Int) = n - 1 } } <v4>: * COPY
=====================
== anonymous_0 ==
fun(n: Int) = n + 1
---------------------
<v0>: Int NEW: magic[FAKE_INITIALIZER](n: Int) -> <v0>
n <v1>: Int NEW: r(n) -> <v1>
1 <v2>: Int NEW: r(1) -> <v2>
n + 1 <v3>: Int NEW: call(n + 1, plus|<v1>, <v2>) -> <v3>
=====================
== anonymous_1 ==
fun(n: Int) = n - 1
---------------------
<v0>: Int NEW: magic[FAKE_INITIALIZER](n: Int) -> <v0>
n <v1>: Int NEW: r(n) -> <v1>
1 <v2>: Int NEW: r(1) -> <v2>
n - 1 <v3>: Int NEW: call(n - 1, minus|<v1>, <v2>) -> <v3>
=====================
@@ -0,0 +1,63 @@
== test ==
fun test() {
val f = fun(n: Int): Int { return 1 }
val g = fun foo(n: Int) = 2
}
---------------------
L0:
1 <START>
2 mark({ val f = fun(n: Int): Int { return 1 } val g = fun foo(n: Int) = 2 })
v(val f = fun(n: Int): Int { return 1 })
jmp?(L2) NEXT:[r(fun(n: Int): Int { return 1 }) -> <v0>, d(fun(n: Int): Int { return 1 })]
d(fun(n: Int): Int { return 1 }) NEXT:[<SINK>]
L2 [after local declaration]:
r(fun(n: Int): Int { return 1 }) -> <v0> PREV:[jmp?(L2)]
w(f|<v0>)
v(val g = fun foo(n: Int) = 2)
jmp?(L5) NEXT:[r(fun foo(n: Int) = 2) -> <v1>, d(fun foo(n: Int) = 2)]
d(fun foo(n: Int) = 2) NEXT:[<SINK>]
L5 [after local declaration]:
r(fun foo(n: Int) = 2) -> <v1> PREV:[jmp?(L5)]
w(g|<v1>)
L1:
1 <END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>, d(fun(n: Int): Int { return 1 }), d(fun foo(n: Int) = 2)]
=====================
== anonymous_0 ==
fun(n: Int): Int { return 1 }
---------------------
L3:
3 <START>
v(n: Int)
magic[FAKE_INITIALIZER](n: Int) -> <v0>
w(n|<v0>)
4 mark({ return 1 })
r(1) -> <v1>
ret(*|<v1>) L4
L4:
3 <END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
== foo ==
fun foo(n: Int) = 2
---------------------
L6:
3 <START>
v(n: Int)
magic[FAKE_INITIALIZER](n: Int) -> <v0>
w(n|<v0>)
r(2) -> <v1>
ret(*|<v1>) L7
L7:
<END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
@@ -0,0 +1,4 @@
fun test() {
val f = fun(n: Int): Int { return 1 }
val g = fun foo(n: Int) = 2
}
@@ -0,0 +1,23 @@
== test ==
fun test() {
val f = fun(n: Int): Int { return 1 }
val g = fun foo(n: Int) = 2
}
---------------------
fun(n: Int): Int { return 1 } <v0>: {<: (Int) -> Int} NEW: r(fun(n: Int): Int { return 1 }) -> <v0>
fun foo(n: Int) = 2 <v1>: {<: (Int) -> Int} NEW: r(fun foo(n: Int) = 2) -> <v1>
=====================
== anonymous_0 ==
fun(n: Int): Int { return 1 }
---------------------
<v0>: Int NEW: magic[FAKE_INITIALIZER](n: Int) -> <v0>
1 <v1>: Int NEW: r(1) -> <v1>
return 1 !<v2>: *
{ return 1 } !<v2>: * COPY
=====================
== foo ==
fun foo(n: Int) = 2
---------------------
<v0>: Int NEW: magic[FAKE_INITIALIZER](n: Int) -> <v0>
2 <v1>: Int NEW: r(2) -> <v1>
=====================
@@ -0,0 +1,78 @@
== test ==
fun test(b: Boolean): (Int) -> Int {
if (b) {
fun foo(n: Int) = n + 1
}
else {
fun bar(n: Int) = n - 1
}
}
---------------------
L0:
1 <START>
v(b: Boolean)
magic[FAKE_INITIALIZER](b: Boolean) -> <v0>
w(b|<v0>)
2 mark({ if (b) { fun foo(n: Int) = n + 1 } else { fun bar(n: Int) = n - 1 } })
mark(if (b) { fun foo(n: Int) = n + 1 } else { fun bar(n: Int) = n - 1 })
r(b) -> <v1>
jf(L2|<v1>) NEXT:[mark({ fun bar(n: Int) = n - 1 }), mark({ fun foo(n: Int) = n + 1 })]
3 mark({ fun foo(n: Int) = n + 1 })
jmp?(L3) NEXT:[jmp(L6), d(fun foo(n: Int) = n + 1)]
d(fun foo(n: Int) = n + 1) NEXT:[<SINK>]
L3 [after local declaration]:
2 jmp(L6) NEXT:[merge(if (b) { fun foo(n: Int) = n + 1 } else { fun bar(n: Int) = n - 1 }|!<v2>, !<v3>) -> <v4>] PREV:[jmp?(L3)]
L2 [else branch]:
3 mark({ fun bar(n: Int) = n - 1 }) PREV:[jf(L2|<v1>)]
jmp?(L7) NEXT:[merge(if (b) { fun foo(n: Int) = n + 1 } else { fun bar(n: Int) = n - 1 }|!<v2>, !<v3>) -> <v4>, d(fun bar(n: Int) = n - 1)]
d(fun bar(n: Int) = n - 1) NEXT:[<SINK>]
L6 ['if' expression result]:
L7 [after local declaration]:
2 merge(if (b) { fun foo(n: Int) = n + 1 } else { fun bar(n: Int) = n - 1 }|!<v2>, !<v3>) -> <v4> PREV:[jmp(L6), jmp?(L7)]
L1:
1 <END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>, d(fun foo(n: Int) = n + 1), d(fun bar(n: Int) = n - 1)]
=====================
== foo ==
fun foo(n: Int) = n + 1
---------------------
L4:
4 <START>
v(n: Int)
magic[FAKE_INITIALIZER](n: Int) -> <v0>
w(n|<v0>)
r(n) -> <v1>
r(1) -> <v2>
mark(n + 1)
call(n + 1, plus|<v1>, <v2>) -> <v3>
ret(*|<v3>) L5
L5:
<END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
== bar ==
fun bar(n: Int) = n - 1
---------------------
L8:
4 <START>
v(n: Int)
magic[FAKE_INITIALIZER](n: Int) -> <v0>
w(n|<v0>)
r(n) -> <v1>
r(1) -> <v2>
mark(n - 1)
call(n - 1, minus|<v1>, <v2>) -> <v3>
ret(*|<v3>) L9
L9:
<END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
@@ -0,0 +1,8 @@
fun test(b: Boolean): (Int) -> Int {
if (b) {
fun foo(n: Int) = n + 1
}
else {
fun bar(n: Int) = n - 1
}
}
@@ -0,0 +1,33 @@
== test ==
fun test(b: Boolean): (Int) -> Int {
if (b) {
fun foo(n: Int) = n + 1
}
else {
fun bar(n: Int) = n - 1
}
}
---------------------
<v0>: Boolean NEW: magic[FAKE_INITIALIZER](b: Boolean) -> <v0>
b <v1>: Boolean NEW: r(b) -> <v1>
{ fun foo(n: Int) = n + 1 } !<v2>: *
{ fun bar(n: Int) = n - 1 } !<v3>: *
if (b) { fun foo(n: Int) = n + 1 } else { fun bar(n: Int) = n - 1 } <v4>: * NEW: merge(if (b) { fun foo(n: Int) = n + 1 } else { fun bar(n: Int) = n - 1 }|!<v2>, !<v3>) -> <v4>
{ if (b) { fun foo(n: Int) = n + 1 } else { fun bar(n: Int) = n - 1 } } <v4>: * COPY
=====================
== foo ==
fun foo(n: Int) = n + 1
---------------------
<v0>: Int NEW: magic[FAKE_INITIALIZER](n: Int) -> <v0>
n <v1>: Int NEW: r(n) -> <v1>
1 <v2>: Int NEW: r(1) -> <v2>
n + 1 <v3>: Int NEW: call(n + 1, plus|<v1>, <v2>) -> <v3>
=====================
== bar ==
fun bar(n: Int) = n - 1
---------------------
<v0>: Int NEW: magic[FAKE_INITIALIZER](n: Int) -> <v0>
n <v1>: Int NEW: r(n) -> <v1>
1 <v2>: Int NEW: r(1) -> <v2>
n - 1 <v3>: Int NEW: call(n - 1, minus|<v1>, <v2>) -> <v3>
=====================
@@ -102,7 +102,10 @@ public abstract class AbstractPseudocodeTest extends KotlinTestWithEnvironment {
String label;
assert (correspondingElement instanceof JetNamedDeclaration || correspondingElement instanceof JetPropertyAccessor) :
"Unexpected element class is pseudocode: " + correspondingElement.getClass();
if (correspondingElement instanceof JetFunctionLiteral) {
boolean isAnonymousFunction =
correspondingElement instanceof JetFunctionLiteral
|| (correspondingElement instanceof JetNamedFunction && correspondingElement.getName() == null);
if (isAnonymousFunction) {
label = "anonymous_" + i++;
}
else if (correspondingElement instanceof JetNamedDeclaration) {
@@ -345,12 +345,30 @@ public class ControlFlowTestGenerated extends AbstractControlFlowTest {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/cfg/declarations/functions"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("anonymousFunctionInBlock.kt")
public void testAnonymousFunctionInBlock() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cfg/declarations/functions/anonymousFunctionInBlock.kt");
doTest(fileName);
}
@TestMetadata("FailFunction.kt")
public void testFailFunction() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cfg/declarations/functions/FailFunction.kt");
doTest(fileName);
}
@TestMetadata("functionAsExpression.kt")
public void testFunctionAsExpression() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cfg/declarations/functions/functionAsExpression.kt");
doTest(fileName);
}
@TestMetadata("namedFunctionInBlock.kt")
public void testNamedFunctionInBlock() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cfg/declarations/functions/namedFunctionInBlock.kt");
doTest(fileName);
}
@TestMetadata("typeParameter.kt")
public void testTypeParameter() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cfg/declarations/functions/typeParameter.kt");
@@ -347,12 +347,30 @@ public class PseudoValueTestGenerated extends AbstractPseudoValueTest {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/cfg/declarations/functions"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("anonymousFunctionInBlock.kt")
public void testAnonymousFunctionInBlock() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cfg/declarations/functions/anonymousFunctionInBlock.kt");
doTest(fileName);
}
@TestMetadata("FailFunction.kt")
public void testFailFunction() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cfg/declarations/functions/FailFunction.kt");
doTest(fileName);
}
@TestMetadata("functionAsExpression.kt")
public void testFunctionAsExpression() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cfg/declarations/functions/functionAsExpression.kt");
doTest(fileName);
}
@TestMetadata("namedFunctionInBlock.kt")
public void testNamedFunctionInBlock() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cfg/declarations/functions/namedFunctionInBlock.kt");
doTest(fileName);
}
@TestMetadata("typeParameter.kt")
public void testTypeParameter() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cfg/declarations/functions/typeParameter.kt");