condjump for boolean value on stack

This commit is contained in:
Dmitry Jemerov
2011-04-07 13:32:59 +02:00
parent b722300db7
commit 9a00e73e64
3 changed files with 27 additions and 5 deletions
@@ -402,6 +402,9 @@ public class ExpressionCodegen extends JetVisitor {
if (type == PsiType.LONG) { if (type == PsiType.LONG) {
return Type.LONG_TYPE; return Type.LONG_TYPE;
} }
if (type == PsiType.BOOLEAN) {
return Type.BOOLEAN_TYPE;
}
} }
if (type instanceof PsiClassType) { if (type instanceof PsiClassType) {
PsiClass psiClass = ((PsiClassType) type).resolve(); PsiClass psiClass = ((PsiClassType) type).resolve();
@@ -73,7 +73,17 @@ public abstract class StackValue {
@Override @Override
public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) { public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
throw new UnsupportedOperationException("don't know how to generate this condjump"); if (type == Type.BOOLEAN_TYPE) {
if (jumpIfFalse) {
v.ifeq(label);
}
else {
v.ifne(label);
}
}
else {
throw new UnsupportedOperationException("can't generate a cond jump for a non-boolean value on stack");
}
} }
} }
@@ -181,13 +181,15 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
} }
public void testNE() throws Exception { public void testNE() throws Exception {
binOpTest("fun foo(a: Int, b: Int): Int = if (a != b) 1 else 0", 5, 5, 0); final String code = "fun foo(a: Int, b: Int): Int = if (a != b) 1 else 0";
binOpTest("fun foo(a: Int, b: Int): Int = if (a != b) 1 else 0", 5, 3, 1); binOpTest(code, 5, 5, 0);
binOpTest(code, 5, 3, 1);
} }
public void testGE() throws Exception { public void testGE() throws Exception {
binOpTest("fun foo(a: Int, b: Int): Int = if (a >= b) 1 else 0", 5, 5, 1); final String code = "fun foo(a: Int, b: Int): Int = if (a >= b) 1 else 0";
binOpTest("fun foo(a: Int, b: Int): Int = if (a >= b) 1 else 0", 3, 5, 0); binOpTest(code, 5, 5, 1);
binOpTest(code, 3, 5, 0);
} }
public void testIfNoElse() throws Exception { public void testIfNoElse() throws Exception {
@@ -198,6 +200,13 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
assertEquals(10, main.invoke(null, 5, false)); assertEquals(10, main.invoke(null, 5, false));
} }
public void testCondJumpOnStack() throws Exception {
loadText("fun foo(a: String): Int = if (Boolean.parseBoolean(a)) 5 else 10");
final Method main = generateFunction();
assertEquals(5, main.invoke(null, "true"));
assertEquals(10, main.invoke(null, "false"));
}
private void binOpTest(final String text, final int arg1, final int arg2, final int expected) throws Exception { private void binOpTest(final String text, final int arg1, final int arg2, final int expected) throws Exception {
loadText(text); loadText(text);
System.out.println(generateToText()); System.out.println(generateToText());