Jump on 'continue' to condition entry points for all loops

This commit is contained in:
Svetlana Isakova
2014-08-29 21:36:16 +04:00
parent 25a0854dbd
commit 0b4f313b6d
18 changed files with 267 additions and 23 deletions
@@ -16,17 +16,22 @@
package org.jetbrains.jet.lang.cfg;
import com.google.common.collect.Sets;
import org.jetbrains.jet.lang.psi.JetElement;
import java.util.Set;
public class BreakableBlockInfo extends BlockInfo {
private final JetElement element;
private final Label entryPoint;
private final Label exitPoint;
protected final Set<Label> referablePoints;
public BreakableBlockInfo(JetElement element, Label entryPoint, Label exitPoint) {
this.element = element;
this.entryPoint = entryPoint;
this.exitPoint = exitPoint;
referablePoints = Sets.newHashSet(entryPoint, exitPoint);
}
public JetElement getElement() {
@@ -40,4 +45,8 @@ public class BreakableBlockInfo extends BlockInfo {
public Label getExitPoint() {
return exitPoint;
}
public Set<Label> getReferablePoints() {
return referablePoints;
}
}
@@ -953,11 +953,7 @@ public class JetControlFlowProcessor {
JetElement loop = getCorrespondingLoop(expression);
if (loop != null) {
checkJumpDoesNotCrossFunctionBoundary(expression, loop);
if (loop instanceof JetDoWhileExpression) {
builder.jump(builder.getConditionEntryPoint(loop), expression);
} else {
builder.jump(builder.getEntryPoint(loop), expression);
}
builder.jump(builder.getConditionEntryPoint(loop), expression);
}
}
@@ -16,8 +16,11 @@
package org.jetbrains.jet.lang.cfg;
import com.google.common.collect.Sets;
import org.jetbrains.jet.lang.psi.JetElement;
import java.util.Set;
public class LoopInfo extends BreakableBlockInfo {
private final Label bodyEntryPoint;
private final Label conditionEntryPoint;
@@ -26,6 +29,8 @@ public class LoopInfo extends BreakableBlockInfo {
super(element, entryPoint, exitPoint);
this.bodyEntryPoint = bodyEntryPoint;
this.conditionEntryPoint = conditionEntryPoint;
referablePoints.add(bodyEntryPoint);
referablePoints.add(conditionEntryPoint);
}
public Label getBodyEntryPoint() {
@@ -16,6 +16,7 @@
package org.jetbrains.jet.lang.cfg.pseudocode;
import com.google.common.collect.Sets;
import com.intellij.util.containers.Stack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -246,8 +247,7 @@ public class JetControlFlowInstructionsGenerator extends JetControlFlowBuilderAd
BlockInfo blockInfo = allBlocks.get(i);
if (blockInfo instanceof BreakableBlockInfo) {
BreakableBlockInfo breakableBlockInfo = (BreakableBlockInfo) blockInfo;
if (jumpTarget == breakableBlockInfo.getExitPoint() || jumpTarget == breakableBlockInfo.getEntryPoint()
|| jumpTarget == error) {
if (breakableBlockInfo.getReferablePoints().contains(jumpTarget) || jumpTarget == error) {
for (int j = finallyBlocks.size() - 1; j >= 0; j--) {
finallyBlocks.get(j).generateFinallyBlock();
}
@@ -49,7 +49,7 @@ L7 ['if' expression result]:
r(b) -> <v11> USE: in: {} out: {b=READ}
mark(use(b))
call(use(b), use|<v11>) -> <v12>
jmp(L4 [loop entry point]) USE: in: {} out: {}
jmp(L3 ['for' loop condition entry point]) USE: in: {} out: {}
- 3 jmp?(L4 [loop entry point])
L2 ['for' loop exit point]:
read (Unit) INIT: in: {i=D, numbers=ID} out: {i=D, numbers=ID}
@@ -461,10 +461,10 @@ L0:
call(1..a, rangeTo|<v1>, <v2>) -> <v3>
v(i)
L3 ['for' loop condition entry point]:
jmp?(L2 ['for' loop exit point]) NEXT:[read (Unit), magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4>]
jmp?(L2 ['for' loop exit point]) NEXT:[read (Unit), magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4>] PREV:[v(i), jmp(L3 ['for' loop condition entry point])]
L4 [loop entry point]:
L5 [body entry point]:
magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4> PREV:[jmp?(L2 ['for' loop exit point]), jmp(L4 [loop entry point]), jmp?(L4 [loop entry point])]
magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4> PREV:[jmp?(L2 ['for' loop exit point]), jmp?(L4 [loop entry point])]
w(i|<v4>)
mark(for (i in 1..a) { try { 1 if (2 > 3) { continue @l } } finally { 2 } })
4 mark({ try { 1 if (2 > 3) { continue @l } } finally { 2 } })
@@ -483,7 +483,7 @@ L8 [start finally]:
7 mark({ 2 })
r(2) -> <v9>
L9 [finish finally]:
6 jmp(L4 [loop entry point]) NEXT:[magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4>]
6 jmp(L3 ['for' loop condition entry point]) NEXT:[jmp?(L2 ['for' loop exit point])]
- 5 jmp(L10 ['if' expression result]) NEXT:[merge(if (2 > 3) { continue @l }|!<v10>) -> <v11>] PREV:[]
L7 [else branch]:
read (Unit) PREV:[jf(L7 [else branch]|<v8>)]
@@ -539,10 +539,10 @@ L0:
call(1..a, rangeTo|<v1>, <v2>) -> <v3>
v(i)
L4 ['for' loop condition entry point]:
jmp?(L3 ['for' loop exit point]) NEXT:[read (Unit), magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4>]
jmp?(L3 ['for' loop exit point]) NEXT:[read (Unit), magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4>] PREV:[v(i), jmp(L4 ['for' loop condition entry point])]
L5 [loop entry point]:
L6 [body entry point]:
magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4> PREV:[jmp?(L3 ['for' loop exit point]), jmp(L5 [loop entry point]), jmp?(L5 [loop entry point])]
magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4> PREV:[jmp?(L3 ['for' loop exit point]), jmp?(L5 [loop entry point])]
w(i|<v4>)
mark(for (i in 1..a) { 1 if (2 > 3) { continue @l } })
5 mark({ 1 if (2 > 3) { continue @l } })
@@ -554,7 +554,7 @@ L6 [body entry point]:
call(2 > 3, compareTo|<v6>, <v7>) -> <v8>
jf(L7 [else branch]|<v8>) NEXT:[read (Unit), mark({ continue @l })]
6 mark({ continue @l })
jmp(L5 [loop entry point]) NEXT:[magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4>]
jmp(L4 ['for' loop condition entry point]) NEXT:[jmp?(L3 ['for' loop exit point])]
- 5 jmp(L8 ['if' expression result]) NEXT:[merge(if (2 > 3) { continue @l }|!<v9>) -> <v10>] PREV:[]
L7 [else branch]:
read (Unit) PREV:[jf(L7 [else branch]|<v8>)]
@@ -612,10 +612,10 @@ L0:
call(1..a, rangeTo|<v1>, <v2>) -> <v3>
v(i)
L4 ['for' loop condition entry point]:
jmp?(L3 ['for' loop exit point]) NEXT:[read (Unit), magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4>]
jmp?(L3 ['for' loop exit point]) NEXT:[read (Unit), magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4>] PREV:[v(i), jmp(L4 ['for' loop condition entry point])]
L5 [loop entry point]:
L6 [body entry point]:
magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4> PREV:[jmp?(L3 ['for' loop exit point]), jmp(L5 [loop entry point]), jmp?(L5 [loop entry point])]
magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4> PREV:[jmp?(L3 ['for' loop exit point]), jmp?(L5 [loop entry point])]
w(i|<v4>)
mark(for (i in 1..a) { 1 if (2 > 3) { continue @l } })
5 mark({ 1 if (2 > 3) { continue @l } })
@@ -627,7 +627,7 @@ L6 [body entry point]:
call(2 > 3, compareTo|<v6>, <v7>) -> <v8>
jf(L7 [else branch]|<v8>) NEXT:[read (Unit), mark({ continue @l })]
6 mark({ continue @l })
jmp(L5 [loop entry point]) NEXT:[magic[LOOP_RANGE_ITERATION](1..a|<v3>) -> <v4>]
jmp(L4 ['for' loop condition entry point]) NEXT:[jmp?(L3 ['for' loop exit point])]
- 5 jmp(L8 ['if' expression result]) NEXT:[merge(if (2 > 3) { continue @l }|!<v9>) -> <v10>] PREV:[]
L7 [else branch]:
read (Unit) PREV:[jf(L7 [else branch]|<v8>)]
@@ -142,7 +142,7 @@ L0:
2 mark({ while (cond()) { try { doSmth() } catch (e: NullPointerException) { doSmth1() } catch (e: Exception) { doSmth2() } finally { if (cond()) return else continue } } })
L2 [loop entry point]:
L5 [condition entry point]:
mark(cond()) PREV:[mark({ while (cond()) { try { doSmth() } catch (e: NullPointerException) { doSmth1() } catch (e: Exception) { doSmth2() } finally { if (cond()) return else continue } } }), jmp(L2 [loop entry point]), jmp(L2 [loop entry point])]
mark(cond()) PREV:[mark({ while (cond()) { try { doSmth() } catch (e: NullPointerException) { doSmth1() } catch (e: Exception) { doSmth2() } finally { if (cond()) return else continue } } }), jmp(L5 [condition entry point]), jmp(L5 [condition entry point])]
call(cond(), cond) -> <v0>
mark(while (cond()) { try { doSmth() } catch (e: NullPointerException) { doSmth1() } catch (e: Exception) { doSmth2() } finally { if (cond()) return else continue } })
jf(L3 [loop exit point]|<v0>) NEXT:[read (Unit), mark({ try { doSmth() } catch (e: NullPointerException) { doSmth1() } catch (e: Exception) { doSmth2() } finally { if (cond()) return else continue } })]
@@ -180,11 +180,11 @@ L11 [start finally]:
mark(if (cond()) return else continue)
mark(cond())
call(cond(), cond) -> <v6>
jf(L12 [else branch]|<v6>) NEXT:[jmp(L2 [loop entry point]), ret L1]
jf(L12 [else branch]|<v6>) NEXT:[jmp(L5 [condition entry point]), ret L1]
ret L1 NEXT:[<END>]
- jmp(L13 ['if' expression result]) NEXT:[merge(if (cond()) return else continue|!<v7>, !<v8>) -> <v9>] PREV:[]
L12 [else branch]:
jmp(L2 [loop entry point]) NEXT:[mark(cond())] PREV:[jf(L12 [else branch]|<v6>)]
jmp(L5 [condition entry point]) NEXT:[mark(cond())] PREV:[jf(L12 [else branch]|<v6>)]
L13 ['if' expression result]:
- merge(if (cond()) return else continue|!<v7>, !<v8>) -> <v9> PREV:[]
L14 [finish finally]:
@@ -194,10 +194,10 @@ L10 [skipFinallyToErrorBlock]:
mark(if (cond()) return else continue)
mark(cond())
call(cond(), cond) -> <v6>
jf(copy L12 [else branch]|<v6>) NEXT:[jmp(L2 [loop entry point]), ret L1]
jf(copy L12 [else branch]|<v6>) NEXT:[jmp(L5 [condition entry point]), ret L1]
ret L1 NEXT:[<END>]
- jmp(copy L13 ['if' expression result]) NEXT:[merge(if (cond()) return else continue|!<v7>, !<v8>) -> <v9>] PREV:[]
jmp(L2 [loop entry point]) NEXT:[mark(cond())] PREV:[jf(copy L12 [else branch]|<v6>)]
jmp(L5 [condition entry point]) NEXT:[mark(cond())] PREV:[jf(copy L12 [else branch]|<v6>)]
- merge(if (cond()) return else continue|!<v7>, !<v8>) -> <v9> PREV:[]
- 3 merge(try { doSmth() } catch (e: NullPointerException) { doSmth1() } catch (e: Exception) { doSmth2() } finally { if (cond()) return else continue }|<v1>, <v3>, <v5>) -> <v10> PREV:[]
- 2 jmp(L2 [loop entry point]) NEXT:[mark(cond())] PREV:[]
@@ -347,4 +347,4 @@ error:
<ERROR> PREV:[jmp(error)]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
=====================
@@ -0,0 +1,40 @@
== test ==
fun test(b: Boolean) {
do {
if (b) break;
continue;
} while (true);
}
---------------------
L0:
1 <START>
v(b: Boolean)
magic[FAKE_INITIALIZER](b: Boolean) -> <v0>
w(b|<v0>)
2 mark({ do { if (b) break; continue; } while (true); })
3 mark(do { if (b) break; continue; } while (true))
L2 [loop entry point]:
L4 [body entry point]:
mark({ if (b) break; continue; }) PREV:[mark(do { if (b) break; continue; } while (true)), jt(L2 [loop entry point]|<v5>)]
mark(if (b) break)
r(b) -> <v1>
jf(L6 [else branch]|<v1>) NEXT:[read (Unit), jmp(L3 [loop exit point])]
jmp(L3 [loop exit point]) NEXT:[read (Unit)]
- jmp(L7 ['if' expression result]) NEXT:[merge(if (b) break|!<v2>) -> <v3>] PREV:[]
L6 [else branch]:
read (Unit) PREV:[jf(L6 [else branch]|<v1>)]
L7 ['if' expression result]:
merge(if (b) break|!<v2>) -> <v3>
jmp(L5 [condition entry point])
L5 [condition entry point]:
r(true) -> <v5>
jt(L2 [loop entry point]|<v5>) NEXT:[read (Unit), mark({ if (b) break; continue; })]
L3 [loop exit point]:
read (Unit) PREV:[jmp(L3 [loop exit point]), jt(L2 [loop entry point]|<v5>)]
L1:
1 <END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
@@ -0,0 +1,6 @@
fun test(b: Boolean) {
do {
if (b) break;
continue;
} while (true);
}
@@ -0,0 +1,18 @@
== test ==
fun test(b: Boolean) {
do {
if (b) break;
continue;
} while (true);
}
---------------------
<v0>: Boolean NEW: magic[FAKE_INITIALIZER](b: Boolean) -> <v0>
b <v1>: Boolean NEW: r(b) -> <v1>
break !<v2>: *
if (b) break <v3>: * NEW: merge(if (b) break|!<v2>) -> <v3>
continue !<v4>: *
{ if (b) break; continue; } !<v4>: * COPY
true <v5>: Boolean NEW: r(true) -> <v5>
do { if (b) break; continue; } while (true) !<v6>: *
{ do { if (b) break; continue; } while (true); } !<v6>: * COPY
=====================
@@ -0,0 +1,47 @@
== test ==
fun test(b: Boolean) {
for (i in 1..10) {
if (b) break;
continue;
}
}
---------------------
L0:
1 <START>
v(b: Boolean)
magic[FAKE_INITIALIZER](b: Boolean) -> <v0>
w(b|<v0>)
2 mark({ for (i in 1..10) { if (b) break; continue; } })
3 r(1) -> <v1>
r(10) -> <v2>
mark(1..10)
call(1..10, rangeTo|<v1>, <v2>) -> <v3>
v(i)
L3 ['for' loop condition entry point]:
jmp?(L2 ['for' loop exit point]) NEXT:[read (Unit), magic[LOOP_RANGE_ITERATION](1..10|<v3>) -> <v4>] PREV:[v(i), jmp(L3 ['for' loop condition entry point])]
L4 [loop entry point]:
L5 [body entry point]:
magic[LOOP_RANGE_ITERATION](1..10|<v3>) -> <v4>
w(i|<v4>)
mark(for (i in 1..10) { if (b) break; continue; })
4 mark({ if (b) break; continue; })
mark(if (b) break)
r(b) -> <v5>
jf(L6 [else branch]|<v5>) NEXT:[read (Unit), jmp(L2 ['for' loop exit point])]
jmp(L2 ['for' loop exit point]) NEXT:[read (Unit)]
- jmp(L7 ['if' expression result]) NEXT:[merge(if (b) break|!<v6>) -> <v7>] PREV:[]
L6 [else branch]:
read (Unit) PREV:[jf(L6 [else branch]|<v5>)]
L7 ['if' expression result]:
merge(if (b) break|!<v6>) -> <v7>
jmp(L3 ['for' loop condition entry point]) NEXT:[jmp?(L2 ['for' loop exit point])]
- 3 jmp?(L4 [loop entry point]) NEXT:[magic[LOOP_RANGE_ITERATION](1..10|<v3>) -> <v4>, read (Unit)] PREV:[]
L2 ['for' loop exit point]:
read (Unit) PREV:[jmp?(L2 ['for' loop exit point]), jmp(L2 ['for' loop exit point])]
L1:
1 <END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
@@ -0,0 +1,6 @@
fun test(b: Boolean) {
for (i in 1..10) {
if (b) break;
continue;
}
}
@@ -0,0 +1,21 @@
== test ==
fun test(b: Boolean) {
for (i in 1..10) {
if (b) break;
continue;
}
}
---------------------
<v0>: Boolean NEW: magic[FAKE_INITIALIZER](b: Boolean) -> <v0>
<v4>: Int NEW: magic[LOOP_RANGE_ITERATION](1..10|<v3>) -> <v4>
1 <v1>: Int NEW: r(1) -> <v1>
10 <v2>: Int NEW: r(10) -> <v2>
1..10 <v3>: {<: Iterable<Int>} NEW: call(1..10, rangeTo|<v1>, <v2>) -> <v3>
b <v5>: Boolean NEW: r(b) -> <v5>
break !<v6>: *
if (b) break <v7>: * NEW: merge(if (b) break|!<v6>) -> <v7>
continue !<v8>: *
{ if (b) break; continue; } !<v8>: * COPY
for (i in 1..10) { if (b) break; continue; } !<v9>: *
{ for (i in 1..10) { if (b) break; continue; } } !<v9>: * COPY
=====================
@@ -0,0 +1,41 @@
== test ==
fun test(b: Boolean) {
while (true) {
if (b) break;
continue;
}
}
---------------------
L0:
1 <START>
v(b: Boolean)
magic[FAKE_INITIALIZER](b: Boolean) -> <v0>
w(b|<v0>)
2 mark({ while (true) { if (b) break; continue; } })
L2 [loop entry point]:
L5 [condition entry point]:
r(true) -> <v1> PREV:[mark({ while (true) { if (b) break; continue; } }), jmp(L5 [condition entry point])]
mark(while (true) { if (b) break; continue; })
magic[VALUE_CONSUMER](true|<v1>) -> <v2>
L4 [body entry point]:
3 mark({ if (b) break; continue; })
mark(if (b) break)
r(b) -> <v3>
jf(L6 [else branch]|<v3>) NEXT:[read (Unit), jmp(L3 [loop exit point])]
jmp(L3 [loop exit point]) NEXT:[read (Unit)]
- jmp(L7 ['if' expression result]) NEXT:[merge(if (b) break|!<v4>) -> <v5>] PREV:[]
L6 [else branch]:
read (Unit) PREV:[jf(L6 [else branch]|<v3>)]
L7 ['if' expression result]:
merge(if (b) break|!<v4>) -> <v5>
jmp(L5 [condition entry point]) NEXT:[r(true) -> <v1>]
- 2 jmp(L2 [loop entry point]) NEXT:[r(true) -> <v1>] PREV:[]
L3 [loop exit point]:
read (Unit) PREV:[jmp(L3 [loop exit point])]
L1:
1 <END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
@@ -0,0 +1,6 @@
fun test(b: Boolean) {
while (true) {
if (b) break;
continue;
}
}
@@ -0,0 +1,19 @@
== test ==
fun test(b: Boolean) {
while (true) {
if (b) break;
continue;
}
}
---------------------
<v0>: Boolean NEW: magic[FAKE_INITIALIZER](b: Boolean) -> <v0>
<v2>: * NEW: magic[VALUE_CONSUMER](true|<v1>) -> <v2>
true <v1>: Boolean NEW: r(true) -> <v1>
b <v3>: Boolean NEW: r(b) -> <v3>
break !<v4>: *
if (b) break <v5>: * NEW: merge(if (b) break|!<v4>) -> <v5>
continue !<v6>: *
{ if (b) break; continue; } !<v6>: * COPY
while (true) { if (b) break; continue; } !<v7>: *
{ while (true) { if (b) break; continue; } } !<v7>: * COPY
=====================
@@ -114,6 +114,21 @@ public class ControlFlowTestGenerated extends AbstractControlFlowTest {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/cfg/controlStructures"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("continueInDoWhile.kt")
public void testContinueInDoWhile() throws Exception {
doTest("compiler/testData/cfg/controlStructures/continueInDoWhile.kt");
}
@TestMetadata("continueInFor.kt")
public void testContinueInFor() throws Exception {
doTest("compiler/testData/cfg/controlStructures/continueInFor.kt");
}
@TestMetadata("continueInWhile.kt")
public void testContinueInWhile() throws Exception {
doTest("compiler/testData/cfg/controlStructures/continueInWhile.kt");
}
@TestMetadata("Finally.kt")
public void testFinally() throws Exception {
doTest("compiler/testData/cfg/controlStructures/Finally.kt");
@@ -116,6 +116,21 @@ public class PseudoValueTestGenerated extends AbstractPseudoValueTest {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/cfg/controlStructures"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("continueInDoWhile.kt")
public void testContinueInDoWhile() throws Exception {
doTest("compiler/testData/cfg/controlStructures/continueInDoWhile.kt");
}
@TestMetadata("continueInFor.kt")
public void testContinueInFor() throws Exception {
doTest("compiler/testData/cfg/controlStructures/continueInFor.kt");
}
@TestMetadata("continueInWhile.kt")
public void testContinueInWhile() throws Exception {
doTest("compiler/testData/cfg/controlStructures/continueInWhile.kt");
}
@TestMetadata("Finally.kt")
public void testFinally() throws Exception {
doTest("compiler/testData/cfg/controlStructures/Finally.kt");