From 339dbbdc8c8e27588ae69dc6d145e284fb4d4357 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 7 Apr 2011 14:56:17 +0200 Subject: [PATCH] put IntCompare on stack --- .../org/jetbrains/jet/codegen/ExpressionCodegen.java | 6 +++++- idea/src/org/jetbrains/jet/codegen/StackValue.java | 9 ++++++++- .../org/jetbrains/jet/codegen/NamespaceGenTest.java | 12 ++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index e7336a4a2e4..71764de0f87 100644 --- a/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -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; diff --git a/idea/src/org/jetbrains/jet/codegen/StackValue.java b/idea/src/org/jetbrains/jet/codegen/StackValue.java index 3b46c0cc826..8dd0eafb09e 100644 --- a/idea/src/org/jetbrains/jet/codegen/StackValue.java +++ b/idea/src/org/jetbrains/jet/codegen/StackValue.java @@ -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 diff --git a/idea/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java b/idea/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java index fd8629dd4ca..2c24d89f65b 100644 --- a/idea/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java +++ b/idea/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java @@ -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());