better handling of empty if/else blocks

This commit is contained in:
Alex Tkachman
2011-11-29 11:56:30 +02:00
parent 84cb0a179a
commit ce1718e518
7 changed files with 101 additions and 19 deletions
@@ -155,6 +155,19 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
return genQualified(receiver, expression.getBaseExpression());
}
private static boolean isEmptyExpression(JetElement expr) {
if(expr == null)
return true;
if(expr instanceof JetBlockExpression) {
JetBlockExpression blockExpression = (JetBlockExpression) expr;
List<JetElement> statements = blockExpression.getStatements();
if(statements.size() == 0 || statements.size() == 1 && isEmptyExpression(statements.get(0))) {
return true;
}
}
return false;
}
@Override
public StackValue visitIfExpression(JetIfExpression expression, StackValue receiver) {
Type asmType = expressionType(expression);
@@ -167,27 +180,36 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
throw new CompilationException();
}
if (thenExpression == null) {
if (isEmptyExpression(thenExpression)) {
if (isEmptyExpression(elseExpression)) {
condition.put(asmType, v);
return StackValue.onStack(asmType);
}
return generateSingleBranchIf(condition, elseExpression, false);
}
if (elseExpression == null) {
return generateSingleBranchIf(condition, thenExpression, true);
else {
if (isEmptyExpression(elseExpression)) {
return generateSingleBranchIf(condition, thenExpression, true);
}
}
Label elseLabel = new Label();
condition.condJump(elseLabel, true, v); // == 0, i.e. false
Label end = continueLabel == null ? new Label() : continueLabel;
gen(thenExpression, asmType);
Label endLabel = new Label();
v.goTo(endLabel);
v.goTo(end);
v.mark(elseLabel);
gen(elseExpression, asmType);
v.mark(endLabel);
if(end != continueLabel)
v.mark(end);
else
v.goTo(end);
return StackValue.onStack(asmType);
}
@@ -198,16 +220,21 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
myContinueTargets.push(condition);
v.mark(condition);
Label end = new Label();
Label end = continueLabel != null ? continueLabel : new Label();
myBreakTargets.push(end);
Label savedContinueLabel = continueLabel;
continueLabel = condition;
final StackValue conditionValue = gen(expression.getCondition());
conditionValue.condJump(end, true, v);
gen(expression.getBody(), Type.VOID_TYPE);
v.goTo(condition);
v.mark(end);
continueLabel = savedContinueLabel;
if(end != continueLabel)
v.mark(end);
myBreakTargets.pop();
myContinueTargets.pop();
@@ -519,13 +546,14 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
}
private StackValue generateSingleBranchIf(StackValue condition, JetExpression expression, boolean inverse) {
Label endLabel = new Label();
Label end = continueLabel != null ? continueLabel : new Label();
condition.condJump(endLabel, inverse, v);
condition.condJump(end, inverse, v);
gen(expression, Type.VOID_TYPE);
v.mark(endLabel);
if(continueLabel != end)
v.mark(end);
return StackValue.none();
}
@@ -655,6 +683,8 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
return StackValue.onStack(Type.getObjectType(closure.getClassname()));
}
private Label continueLabel;
private StackValue generateBlock(List<JetElement> statements) {
Label blockStart = new Label();
v.mark(blockStart);
@@ -671,9 +701,12 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
}
StackValue answer = StackValue.none();
Label savedContinueLabel = continueLabel;
continueLabel = null;
for (int i = 0, statementsSize = statements.size(); i < statementsSize; i++) {
JetElement statement = statements.get(i);
if (i == statements.size() - 1 /*&& statement instanceof JetExpression && !bindingContext.get(BindingContext.STATEMENT, statement)*/) {
continueLabel = savedContinueLabel;
answer = gen(statement);
}
else {
@@ -0,0 +1,42 @@
fun IntArray.swap(i:Int, j:Int) {
val temp = this[i]
this[i] = this[j]
this[j] = temp
}
fun IntArray.quicksort() = quicksort(0, size-1)
fun IntArray.quicksort(L: Int, R:Int) {
val m = this[(L + R) / 2]
var i = L
var j = R
while (i <= j) {
while (this[i] < m)
i++
while (this[j] > m)
j--
if (i <= j) {
swap(i++, j--)
}
else {
}
}
if (L < j)
quicksort(L, j)
if (R > i)
quicksort(i, R)
}
fun box() : String {
val a = IntArray(10)
for(i in 0..4) {
a[2*i] = 2*i
a[2*i+1] = -2*i-1
}
a.quicksort()
for(i in 0..9) {
System.out?.print(a[i])
System.out?.print(' ')
}
return "OK"
}
@@ -1,3 +1,5 @@
fun box(i: Int?) {
fun box() : String {
val i : Int? = 0
val j = i?.plus(3) //verify error
return "OK"
}
@@ -220,4 +220,9 @@ public class ControlStructuresTest extends CodegenTestCase {
createEnvironmentWithFullJdk();
blackBoxFile("regressions/kt434.jet");
}
public void testQuicksort() throws Exception {
blackBoxFile("controlStructures/quicksort.jet");
// System.out.println(generateToText());
}
}
@@ -266,7 +266,7 @@ public class PrimitiveTypesTest extends CodegenTestCase {
}
public void testKt239 () throws Exception {
blackBoxFile("regressions/kt242.jet");
blackBoxFile("regressions/kt239.jet");
}
public void testKt243 () throws Exception {
+2 -2
View File
@@ -30,6 +30,8 @@ public class Quicksort {
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
// Sample data
int[] a = new int[100000000];
for (int i = 0; i < a.length; i++) {
@@ -38,8 +40,6 @@ public class Quicksort {
a[i] = -a[i];
}
long start = System.currentTimeMillis();
quicksort(a);
long total = System.currentTimeMillis() - start;
+3 -3
View File
@@ -17,7 +17,7 @@ fun IntArray.quicksort(L: Int, R:Int) {
i++
while (this[j] > m)
j--
if (i <= j) { KT-5
if (i <= j) {
swap(i++, j--)
}
}
@@ -28,6 +28,8 @@ fun IntArray.quicksort(L: Int, R:Int) {
}
fun main(array: Array<String>) {
val start = System.currentTimeMillis()
val a = IntArray(100000000)
var i = 0
val len = a.size
@@ -38,8 +40,6 @@ fun main(array: Array<String>) {
i++
}
val start = System.currentTimeMillis()
a.quicksort()
val total = System.currentTimeMillis() - start