bitwise operations

This commit is contained in:
Dmitry Jemerov
2011-04-07 18:51:03 +02:00
parent 2ce07a5ee2
commit 6b1f435b99
2 changed files with 39 additions and 0 deletions
@@ -546,6 +546,12 @@ public class ExpressionCodegen extends JetVisitor {
if (name.equals("times")) return Opcodes.IMUL;
if (name.equals("div")) return Opcodes.IDIV;
if (name.equals("mod")) return Opcodes.IREM;
if (name.equals("shl")) return Opcodes.ISHL;
if (name.equals("shr")) return Opcodes.ISHR;
if (name.equals("ushr")) return Opcodes.IUSHR;
if (name.equals("and")) return Opcodes.IAND;
if (name.equals("or")) return Opcodes.IOR;
if (name.equals("xor")) return Opcodes.IXOR;
throw new UnsupportedOperationException("Don't know how to generate binary op method " + name);
}
@@ -606,6 +612,13 @@ public class ExpressionCodegen extends JetVisitor {
myStack.push(StackValue.onStack(asmType));
return true;
}
else if (op.getName().equals("inv")) {
gen(operand, asmType);
v.aconst(-1);
v.xor(asmType);
myStack.push(StackValue.onStack(asmType));
return true;
}
else if (op.getName().equals("inc") || op.getName().equals("dec")) {
if (!(operand instanceof JetReferenceExpression)) {
throw new UnsupportedOperationException("cannot increment or decrement a non-lvalue");
@@ -378,6 +378,32 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
assertEquals(2.0, main.invoke(null, 1.0));
}
public void testShl() throws Exception {
binOpTest("fun foo(a: Int, b: Int): Int = a shl b", 1, 3, 8);
}
public void testShr() throws Exception {
binOpTest("fun foo(a: Int, b: Int): Int = a shr b", 8, 3, 1);
}
public void testBitAnd() throws Exception {
binOpTest("fun foo(a: Int, b: Int): Int = a and b", 0x77, 0x1f, 0x17);
}
public void testBitOr() throws Exception {
binOpTest("fun foo(a: Int, b: Int): Int = a or b", 0x77, 0x1f, 0x7f);
}
public void testBitXor() throws Exception {
binOpTest("fun foo(a: Int, b: Int): Int = a xor b", 0x70, 0x1f, 0x6f);
}
public void _testBitInv() throws Exception {
loadText("fun foo(a: Int): Int = ~a");
final Method main = generateFunction();
assertEquals(0xffff0000, main.invoke(null, 0x0000ffff));
}
private void binOpTest(final String text, final Object arg1, final Object arg2, final Object expected) throws Exception {
loadText(text);
System.out.println(generateToText());