put IntCompare on stack

This commit is contained in:
Dmitry Jemerov
2011-04-07 14:56:17 +02:00
parent 230987392b
commit 339dbbdc8c
3 changed files with 25 additions and 2 deletions
@@ -486,7 +486,7 @@ public class ExpressionCodegen extends JetVisitor {
if (op.getName().equals("equals")) {
final Type leftType = typeMapper.mapType(bindingContext.getExpressionType(expression.getLeft()));
final Type rightType = typeMapper.mapType(bindingContext.getExpressionType(expression.getRight()));
if (leftType == Type.INT_TYPE && rightType == Type.INT_TYPE) {
if (isIntLikePrimitive(leftType) && isIntLikePrimitive(rightType)) {
gen(expression.getLeft(), Type.INT_TYPE);
gen(expression.getRight(), Type.INT_TYPE);
myStack.push(StackValue.icmp(opToken));
@@ -505,6 +505,10 @@ public class ExpressionCodegen extends JetVisitor {
throw new UnsupportedOperationException("Don't know how to generate binary op " + expression);
}
private static boolean isIntLikePrimitive(Type type) {
return type == Type.INT_TYPE || type == Type.BYTE_TYPE;
}
private static int opcodeForMethod(final String name) {
if (name.equals("plus")) return Opcodes.IADD;
if (name.equals("minus")) return Opcodes.ISUB;
@@ -133,7 +133,14 @@ public abstract class StackValue {
@Override
public void put(Type type, InstructionAdapter v) {
throw new UnsupportedOperationException("don't know how to put an IntCompare on stack");
Label ifTrue = new Label();
Label end = new Label();
condJump(ifTrue, false, v);
v.iconst(0);
v.goTo(end);
v.mark(ifTrue);
v.iconst(1);
v.mark(end);
}
@Override
@@ -207,6 +207,13 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
assertEquals(10, main.invoke(null, "false"));
}
public void testReturnCmp() throws Exception {
loadText("fun foo(a: Int, b: Int): Boolean = a == b");
final Method main = generateFunction();
assertEquals(true, main.invoke(null, 1, 1));
assertEquals(false, main.invoke(null, 1, 2));
}
public void _testBoxedInt() throws Exception {
loadText("fun foo(a: Int?): Int = if (a != null) a else 239");
final Method main = generateFunction();
@@ -247,6 +254,11 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
Byte.valueOf((byte) 127), Byte.valueOf((byte) 127), 254);
}
public void testByteCmp() throws Exception {
binOpTest("fun foo(a: Byte, b: Byte): Int = if (a == b) 1 else 0",
Byte.valueOf((byte) 127), Byte.valueOf((byte) 127), 1);
}
private void binOpTest(final String text, final Object arg1, final Object arg2, final int expected) throws Exception {
loadText(text);
System.out.println(generateToText());