From ccdc80d1fee4845eceddebf554c65cd563f1a706 Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Tue, 20 Dec 2011 11:23:13 +0200 Subject: [PATCH] fix for while-loop-ending-with-if bug --- .../jet/codegen/ExpressionCodegen.java | 3 ++ .../codegen/controlStructures/ifInWhile.jet | 14 +++++++ .../jet/codegen/ControlStructuresTest.java | 6 +++ examples/src/benchmarks/LockPerf.kt | 42 +++++++++---------- 4 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 compiler/testData/codegen/controlStructures/ifInWhile.jet diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index 4885242db44..8dafa6b2690 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -233,6 +233,9 @@ public class ExpressionCodegen extends JetVisitor { condition.condJump(elseLabel, true, v); // == 0, i.e. false Label end = continueLabel == null ? new Label() : continueLabel; + + if(continueLabel != null) + asmType = Type.VOID_TYPE; gen(thenExpression, asmType); diff --git a/compiler/testData/codegen/controlStructures/ifInWhile.jet b/compiler/testData/codegen/controlStructures/ifInWhile.jet new file mode 100644 index 00000000000..834c71f2b6f --- /dev/null +++ b/compiler/testData/codegen/controlStructures/ifInWhile.jet @@ -0,0 +1,14 @@ +import java.lang.Runtime + +fun box() : String { + val processors = Runtime.getRuntime().sure().availableProcessors() + var threadNum = 1 + while(threadNum <= 1024) { + System.out?.println(threadNum) + if(threadNum < 2 * processors) + threadNum += 1 + else + threadNum *= 2 + } + return "OK" +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java b/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java index ec218e38024..2869aa145b4 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java @@ -244,6 +244,12 @@ public class ControlStructuresTest extends CodegenTestCase { public void testSynchronized() throws Exception { createEnvironmentWithFullJdk(); blackBoxFile("controlStructures/sync.jet"); +// System.out.println(generateToText()); + } + + public void testIfInWhile() throws Exception { + createEnvironmentWithFullJdk(); + blackBoxFile("controlStructures/ifInWhile.jet"); // System.out.println(generateToText()); } } diff --git a/examples/src/benchmarks/LockPerf.kt b/examples/src/benchmarks/LockPerf.kt index 064280780db..fd1559f0574 100644 --- a/examples/src/benchmarks/LockPerf.kt +++ b/examples/src/benchmarks/LockPerf.kt @@ -1,5 +1,7 @@ namespace lockperformance +import std.io.* +import std.util.* import std.concurrent.* import java.util.concurrent.atomic.AtomicInteger; @@ -13,41 +15,37 @@ fun Int.latch(op: fun CountDownLatch.() : T) : T { return res } -fun Int.times(action: fun():Unit) { - for(i in 0..this-1) - action() -} - fun main(args: Array) { val processors = Runtime.getRuntime().sure().availableProcessors() var threadNum = 1 while(threadNum <= 1024) { val counter = AtomicInteger() - val start = System.currentTimeMillis() - threadNum.latch{ - val lock = ReentrantLock() - threadNum.times { - thread { - while(true) { - lock.lock() - try { - if (counter.get() == 100000000) { - countDown(); - break; - } else { - counter.incrementAndGet(); + val duration = millisTime { + threadNum.latch{ + val lock = ReentrantLock() + for(i in 0..threadNum-1) { + thread { + while(true) { + lock.lock() + try { + if (counter.get() == 100000000) { + countDown(); + break; + } else { + counter.incrementAndGet(); + } + } + finally { + lock.unlock() } - } - finally { - lock.unlock() } } } } } - System.out?.println(threadNum.toString() + " " + (System.currentTimeMillis() - start)); + println(threadNum.toString() + " " + duration) if(threadNum < 2 * processors) threadNum = threadNum + 1