From 50228fa085b120560fba6c09dc8d9299d17985b8 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Mon, 23 Jun 2014 13:53:08 +0400 Subject: [PATCH] Pseudocode: Generate correct values for postfix increment/decrement --- .../jet/lang/cfg/JetControlFlowProcessor.java | 3 + compiler/testData/cfg/arrays/arrayInc.values | 4 +- .../cfg/conventions/incrementAtTheEnd.values | 4 +- .../cfg/expressions/incdec.instructions | 65 +++++++++++++++++++ compiler/testData/cfg/expressions/incdec.kt | 11 ++++ .../testData/cfg/expressions/incdec.values | 30 +++++++++ .../jet/cfg/ControlFlowTestGenerated.java | 5 ++ .../jet/cfg/PseudoValueTestGenerated.java | 5 ++ 8 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/cfg/expressions/incdec.instructions create mode 100644 compiler/testData/cfg/expressions/incdec.kt create mode 100644 compiler/testData/cfg/expressions/incdec.values diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetControlFlowProcessor.java b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetControlFlowProcessor.java index 3fd84976263..03197059a86 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetControlFlowProcessor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetControlFlowProcessor.java @@ -585,6 +585,9 @@ public class JetControlFlowProcessor { if (incrementOrDecrement) { visitAssignment(baseExpression, getValueAsFunction(rhsValue), expression); + if (expression instanceof JetPostfixExpression) { + copyValue(baseExpression, expression); + } } } diff --git a/compiler/testData/cfg/arrays/arrayInc.values b/compiler/testData/cfg/arrays/arrayInc.values index 6f116496e1d..0b078b11564 100644 --- a/compiler/testData/cfg/arrays/arrayInc.values +++ b/compiler/testData/cfg/arrays/arrayInc.values @@ -6,6 +6,6 @@ fun foo(a: Array) { a NEW() 0 NEW() a[0] NEW(, ) -a[0]++ NEW(, , ) -{ a[0]++ } COPY +a[0]++ COPY +{ a[0]++ } COPY ===================== diff --git a/compiler/testData/cfg/conventions/incrementAtTheEnd.values b/compiler/testData/cfg/conventions/incrementAtTheEnd.values index c25eae8f5a4..af35d135232 100644 --- a/compiler/testData/cfg/conventions/incrementAtTheEnd.values +++ b/compiler/testData/cfg/conventions/incrementAtTheEnd.values @@ -6,6 +6,6 @@ fun foo() { --------------------- 1 NEW() i NEW() -i++ NEW() -{ var i = 1 i++ } COPY +i++ COPY +{ var i = 1 i++ } COPY ===================== diff --git a/compiler/testData/cfg/expressions/incdec.instructions b/compiler/testData/cfg/expressions/incdec.instructions new file mode 100644 index 00000000000..5fa70363fa8 --- /dev/null +++ b/compiler/testData/cfg/expressions/incdec.instructions @@ -0,0 +1,65 @@ +== bar == +fun bar(n: Int) { + +} +--------------------- +L0: + 1 + v(n: Int) + magic(n: Int) -> + w(n|) + 2 mark({ }) + read (Unit) +L1: + 1 NEXT:[] +error: + PREV:[] +sink: + PREV:[, ] +===================== +== foo == +fun foo() { + var a = 1 + bar(a++) + bar(a--) + bar(++a) + bar(--a) +} +--------------------- +L0: + 1 + 2 mark({ var a = 1 bar(a++) bar(a--) bar(++a) bar(--a) }) + v(var a = 1) + r(1) -> + w(a|) + r(a) -> + mark(a++) + call(++, inc|) -> + w(a|) + mark(bar(a++)) + call(bar, bar|) -> + r(a) -> + mark(a--) + call(--, dec|) -> + w(a|) + mark(bar(a--)) + call(bar, bar|) -> + r(a) -> + mark(++a) + call(++, inc|) -> + w(a|) + mark(bar(++a)) + call(bar, bar|) -> + r(a) -> + mark(--a) + call(--, dec|) -> + w(a|) + mark(bar(--a)) + call(bar, bar|) -> +L1: + 1 NEXT:[] +error: + PREV:[] +sink: + PREV:[, ] +===================== \ No newline at end of file diff --git a/compiler/testData/cfg/expressions/incdec.kt b/compiler/testData/cfg/expressions/incdec.kt new file mode 100644 index 00000000000..55a90b5c61a --- /dev/null +++ b/compiler/testData/cfg/expressions/incdec.kt @@ -0,0 +1,11 @@ +fun bar(n: Int) { + +} + +fun foo() { + var a = 1 + bar(a++) + bar(a--) + bar(++a) + bar(--a) +} \ No newline at end of file diff --git a/compiler/testData/cfg/expressions/incdec.values b/compiler/testData/cfg/expressions/incdec.values new file mode 100644 index 00000000000..f593461f745 --- /dev/null +++ b/compiler/testData/cfg/expressions/incdec.values @@ -0,0 +1,30 @@ +== bar == +fun bar(n: Int) { + +} +--------------------- +===================== +== foo == +fun foo() { + var a = 1 + bar(a++) + bar(a--) + bar(++a) + bar(--a) +} +--------------------- +1 NEW() +a NEW() +a++ COPY +bar(a++) NEW() +a NEW() +a-- COPY +bar(a--) NEW() +a NEW() +++a NEW() +bar(++a) NEW() +a NEW() +--a NEW() +bar(--a) NEW() +{ var a = 1 bar(a++) bar(a--) bar(++a) bar(--a) } COPY +===================== \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/cfg/ControlFlowTestGenerated.java b/compiler/tests/org/jetbrains/jet/cfg/ControlFlowTestGenerated.java index 597bde87fe3..4ea1536a2f6 100644 --- a/compiler/tests/org/jetbrains/jet/cfg/ControlFlowTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/cfg/ControlFlowTestGenerated.java @@ -359,6 +359,11 @@ public class ControlFlowTestGenerated extends AbstractControlFlowTest { doTest("compiler/testData/cfg/expressions/expressionAsFunction.kt"); } + @TestMetadata("incdec.kt") + public void testIncdec() throws Exception { + doTest("compiler/testData/cfg/expressions/incdec.kt"); + } + @TestMetadata("LazyBooleans.kt") public void testLazyBooleans() throws Exception { doTest("compiler/testData/cfg/expressions/LazyBooleans.kt"); diff --git a/compiler/tests/org/jetbrains/jet/cfg/PseudoValueTestGenerated.java b/compiler/tests/org/jetbrains/jet/cfg/PseudoValueTestGenerated.java index 4a9f13962dc..326aedc5a82 100644 --- a/compiler/tests/org/jetbrains/jet/cfg/PseudoValueTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/cfg/PseudoValueTestGenerated.java @@ -361,6 +361,11 @@ public class PseudoValueTestGenerated extends AbstractPseudoValueTest { doTest("compiler/testData/cfg/expressions/expressionAsFunction.kt"); } + @TestMetadata("incdec.kt") + public void testIncdec() throws Exception { + doTest("compiler/testData/cfg/expressions/incdec.kt"); + } + @TestMetadata("LazyBooleans.kt") public void testLazyBooleans() throws Exception { doTest("compiler/testData/cfg/expressions/LazyBooleans.kt");