better handling of empty if/else blocks
This commit is contained in:
@@ -155,6 +155,19 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
|||||||
return genQualified(receiver, expression.getBaseExpression());
|
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
|
@Override
|
||||||
public StackValue visitIfExpression(JetIfExpression expression, StackValue receiver) {
|
public StackValue visitIfExpression(JetIfExpression expression, StackValue receiver) {
|
||||||
Type asmType = expressionType(expression);
|
Type asmType = expressionType(expression);
|
||||||
@@ -167,27 +180,36 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
|||||||
throw new CompilationException();
|
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);
|
return generateSingleBranchIf(condition, elseExpression, false);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
if (elseExpression == null) {
|
if (isEmptyExpression(elseExpression)) {
|
||||||
return generateSingleBranchIf(condition, thenExpression, true);
|
return generateSingleBranchIf(condition, thenExpression, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Label elseLabel = new Label();
|
Label elseLabel = new Label();
|
||||||
condition.condJump(elseLabel, true, v); // == 0, i.e. false
|
condition.condJump(elseLabel, true, v); // == 0, i.e. false
|
||||||
|
|
||||||
|
Label end = continueLabel == null ? new Label() : continueLabel;
|
||||||
|
|
||||||
gen(thenExpression, asmType);
|
gen(thenExpression, asmType);
|
||||||
|
|
||||||
Label endLabel = new Label();
|
v.goTo(end);
|
||||||
v.goTo(endLabel);
|
|
||||||
v.mark(elseLabel);
|
v.mark(elseLabel);
|
||||||
|
|
||||||
gen(elseExpression, asmType);
|
gen(elseExpression, asmType);
|
||||||
|
|
||||||
v.mark(endLabel);
|
if(end != continueLabel)
|
||||||
|
v.mark(end);
|
||||||
|
else
|
||||||
|
v.goTo(end);
|
||||||
|
|
||||||
return StackValue.onStack(asmType);
|
return StackValue.onStack(asmType);
|
||||||
}
|
}
|
||||||
@@ -198,16 +220,21 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
|||||||
myContinueTargets.push(condition);
|
myContinueTargets.push(condition);
|
||||||
v.mark(condition);
|
v.mark(condition);
|
||||||
|
|
||||||
Label end = new Label();
|
Label end = continueLabel != null ? continueLabel : new Label();
|
||||||
myBreakTargets.push(end);
|
myBreakTargets.push(end);
|
||||||
|
|
||||||
|
Label savedContinueLabel = continueLabel;
|
||||||
|
continueLabel = condition;
|
||||||
|
|
||||||
final StackValue conditionValue = gen(expression.getCondition());
|
final StackValue conditionValue = gen(expression.getCondition());
|
||||||
conditionValue.condJump(end, true, v);
|
conditionValue.condJump(end, true, v);
|
||||||
|
|
||||||
gen(expression.getBody(), Type.VOID_TYPE);
|
gen(expression.getBody(), Type.VOID_TYPE);
|
||||||
v.goTo(condition);
|
v.goTo(condition);
|
||||||
|
|
||||||
v.mark(end);
|
continueLabel = savedContinueLabel;
|
||||||
|
if(end != continueLabel)
|
||||||
|
v.mark(end);
|
||||||
myBreakTargets.pop();
|
myBreakTargets.pop();
|
||||||
myContinueTargets.pop();
|
myContinueTargets.pop();
|
||||||
|
|
||||||
@@ -519,13 +546,14 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private StackValue generateSingleBranchIf(StackValue condition, JetExpression expression, boolean inverse) {
|
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);
|
gen(expression, Type.VOID_TYPE);
|
||||||
|
|
||||||
v.mark(endLabel);
|
if(continueLabel != end)
|
||||||
|
v.mark(end);
|
||||||
return StackValue.none();
|
return StackValue.none();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -655,6 +683,8 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
|||||||
return StackValue.onStack(Type.getObjectType(closure.getClassname()));
|
return StackValue.onStack(Type.getObjectType(closure.getClassname()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Label continueLabel;
|
||||||
|
|
||||||
private StackValue generateBlock(List<JetElement> statements) {
|
private StackValue generateBlock(List<JetElement> statements) {
|
||||||
Label blockStart = new Label();
|
Label blockStart = new Label();
|
||||||
v.mark(blockStart);
|
v.mark(blockStart);
|
||||||
@@ -671,9 +701,12 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
StackValue answer = StackValue.none();
|
StackValue answer = StackValue.none();
|
||||||
|
Label savedContinueLabel = continueLabel;
|
||||||
|
continueLabel = null;
|
||||||
for (int i = 0, statementsSize = statements.size(); i < statementsSize; i++) {
|
for (int i = 0, statementsSize = statements.size(); i < statementsSize; i++) {
|
||||||
JetElement statement = statements.get(i);
|
JetElement statement = statements.get(i);
|
||||||
if (i == statements.size() - 1 /*&& statement instanceof JetExpression && !bindingContext.get(BindingContext.STATEMENT, statement)*/) {
|
if (i == statements.size() - 1 /*&& statement instanceof JetExpression && !bindingContext.get(BindingContext.STATEMENT, statement)*/) {
|
||||||
|
continueLabel = savedContinueLabel;
|
||||||
answer = gen(statement);
|
answer = gen(statement);
|
||||||
}
|
}
|
||||||
else {
|
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
|
val j = i?.plus(3) //verify error
|
||||||
|
return "OK"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -220,4 +220,9 @@ public class ControlStructuresTest extends CodegenTestCase {
|
|||||||
createEnvironmentWithFullJdk();
|
createEnvironmentWithFullJdk();
|
||||||
blackBoxFile("regressions/kt434.jet");
|
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 {
|
public void testKt239 () throws Exception {
|
||||||
blackBoxFile("regressions/kt242.jet");
|
blackBoxFile("regressions/kt239.jet");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testKt243 () throws Exception {
|
public void testKt243 () throws Exception {
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ public class Quicksort {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
// Sample data
|
// Sample data
|
||||||
int[] a = new int[100000000];
|
int[] a = new int[100000000];
|
||||||
for (int i = 0; i < a.length; i++) {
|
for (int i = 0; i < a.length; i++) {
|
||||||
@@ -38,8 +40,6 @@ public class Quicksort {
|
|||||||
a[i] = -a[i];
|
a[i] = -a[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
|
|
||||||
quicksort(a);
|
quicksort(a);
|
||||||
|
|
||||||
long total = System.currentTimeMillis() - start;
|
long total = System.currentTimeMillis() - start;
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ fun IntArray.quicksort(L: Int, R:Int) {
|
|||||||
i++
|
i++
|
||||||
while (this[j] > m)
|
while (this[j] > m)
|
||||||
j--
|
j--
|
||||||
if (i <= j) { KT-5
|
if (i <= j) {
|
||||||
swap(i++, j--)
|
swap(i++, j--)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,6 +28,8 @@ fun IntArray.quicksort(L: Int, R:Int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun main(array: Array<String>) {
|
fun main(array: Array<String>) {
|
||||||
|
val start = System.currentTimeMillis()
|
||||||
|
|
||||||
val a = IntArray(100000000)
|
val a = IntArray(100000000)
|
||||||
var i = 0
|
var i = 0
|
||||||
val len = a.size
|
val len = a.size
|
||||||
@@ -38,8 +40,6 @@ fun main(array: Array<String>) {
|
|||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
val start = System.currentTimeMillis()
|
|
||||||
|
|
||||||
a.quicksort()
|
a.quicksort()
|
||||||
|
|
||||||
val total = System.currentTimeMillis() - start
|
val total = System.currentTimeMillis() - start
|
||||||
|
|||||||
Reference in New Issue
Block a user