diff --git a/src/Kernel/Mnemonics/_dadd.php b/src/Kernel/Mnemonics/_dadd.php index ba9437e6..dd6461a8 100644 --- a/src/Kernel/Mnemonics/_dadd.php +++ b/src/Kernel/Mnemonics/_dadd.php @@ -11,8 +11,8 @@ final class _dadd implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::add($value1, $value2)); } diff --git a/src/Kernel/Mnemonics/_dmul.php b/src/Kernel/Mnemonics/_dmul.php index 988346bb..326d400d 100644 --- a/src/Kernel/Mnemonics/_dmul.php +++ b/src/Kernel/Mnemonics/_dmul.php @@ -11,8 +11,8 @@ final class _dmul implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::multiply($value1, $value2, 8)); } diff --git a/src/Kernel/Mnemonics/_dsub.php b/src/Kernel/Mnemonics/_dsub.php index 9e1b3751..85cc06e4 100644 --- a/src/Kernel/Mnemonics/_dsub.php +++ b/src/Kernel/Mnemonics/_dsub.php @@ -11,8 +11,8 @@ final class _dsub implements OperationInterface public function execute(): void { - $leftValue = $this->getStack(); $rightValue = $this->getStack(); + $leftValue = $this->getStack(); $this->pushStack(BinaryTool::sub($leftValue, $rightValue, 8)); } diff --git a/src/Kernel/Mnemonics/_iadd.php b/src/Kernel/Mnemonics/_iadd.php index 08789c60..412bb9f3 100644 --- a/src/Kernel/Mnemonics/_iadd.php +++ b/src/Kernel/Mnemonics/_iadd.php @@ -11,8 +11,8 @@ final class _iadd implements OperationInterface public function execute(): void { - $leftValue = $this->getStack(); $rightValue = $this->getStack(); + $leftValue = $this->getStack(); $this->pushStack(BinaryTool::add($leftValue, $rightValue)); } diff --git a/src/Kernel/Mnemonics/_iand.php b/src/Kernel/Mnemonics/_iand.php index fb7dc93f..bd26bf78 100644 --- a/src/Kernel/Mnemonics/_iand.php +++ b/src/Kernel/Mnemonics/_iand.php @@ -11,8 +11,8 @@ final class _iand implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::andBits($value1, $value2, 4)); } diff --git a/src/Kernel/Mnemonics/_if_acmpeq.php b/src/Kernel/Mnemonics/_if_acmpeq.php index 276c5526..aefa5360 100644 --- a/src/Kernel/Mnemonics/_if_acmpeq.php +++ b/src/Kernel/Mnemonics/_if_acmpeq.php @@ -13,8 +13,8 @@ public function execute(): void { $offset = $this->readShort(); - $leftOperand = $this->getStack(); $rightOperand = $this->getStack(); + $leftOperand = $this->getStack(); if ($leftOperand === $rightOperand) { $this->setOffset($this->getProgramCounter() + $offset); diff --git a/src/Kernel/Mnemonics/_if_acmpne.php b/src/Kernel/Mnemonics/_if_acmpne.php index 75d2361b..84c47987 100644 --- a/src/Kernel/Mnemonics/_if_acmpne.php +++ b/src/Kernel/Mnemonics/_if_acmpne.php @@ -13,8 +13,8 @@ public function execute(): void { $offset = $this->readShort(); - $leftOperand = $this->getStack(); $rightOperand = $this->getStack(); + $leftOperand = $this->getStack(); if ($leftOperand !== $rightOperand) { $this->setOffset($this->getProgramCounter() + $offset); diff --git a/src/Kernel/Mnemonics/_if_icmpge.php b/src/Kernel/Mnemonics/_if_icmpge.php index f29d4a9c..8b942630 100644 --- a/src/Kernel/Mnemonics/_if_icmpge.php +++ b/src/Kernel/Mnemonics/_if_icmpge.php @@ -13,10 +13,10 @@ public function execute(): void { $offset = $this->readShort(); - $leftOperand = $this->getStack(); $rightOperand = $this->getStack(); + $leftOperand = $this->getStack(); - if ($leftOperand <= $rightOperand) { + if ($leftOperand >= $rightOperand) { $this->setOffset($this->getProgramCounter() + $offset); } } diff --git a/src/Kernel/Mnemonics/_if_icmpgt.php b/src/Kernel/Mnemonics/_if_icmpgt.php index 7ce1f603..968f0577 100644 --- a/src/Kernel/Mnemonics/_if_icmpgt.php +++ b/src/Kernel/Mnemonics/_if_icmpgt.php @@ -13,10 +13,10 @@ public function execute(): void { $offset = $this->readShort(); - $leftOperand = $this->getStack(); $rightOperand = $this->getStack(); + $leftOperand = $this->getStack(); - if ($leftOperand < $rightOperand) { + if ($leftOperand > $rightOperand) { $this->setOffset($this->getProgramCounter() + $offset); } } diff --git a/src/Kernel/Mnemonics/_if_icmplt.php b/src/Kernel/Mnemonics/_if_icmplt.php index 2b762c6f..242c47a8 100644 --- a/src/Kernel/Mnemonics/_if_icmplt.php +++ b/src/Kernel/Mnemonics/_if_icmplt.php @@ -13,10 +13,10 @@ public function execute(): void { $offset = $this->readShort(); - $leftOperand = $this->getStack(); $rightOperand = $this->getStack(); + $leftOperand = $this->getStack(); - if ($rightOperand < $leftOperand) { + if ($leftOperand < $rightOperand) { $this->setOffset($this->getProgramCounter() + $offset); } } diff --git a/src/Kernel/Mnemonics/_if_icmpne.php b/src/Kernel/Mnemonics/_if_icmpne.php index 1d5ef93a..076baaa5 100644 --- a/src/Kernel/Mnemonics/_if_icmpne.php +++ b/src/Kernel/Mnemonics/_if_icmpne.php @@ -13,8 +13,8 @@ public function execute(): void { $offset = $this->readShort(); - $leftOperand = $this->getStack(); $rightOperand = $this->getStack(); + $leftOperand = $this->getStack(); if ($leftOperand != $rightOperand) { $this->setOffset($this->getProgramCounter() + $offset); diff --git a/src/Kernel/Mnemonics/_imul.php b/src/Kernel/Mnemonics/_imul.php index 39294b1a..ff6df708 100644 --- a/src/Kernel/Mnemonics/_imul.php +++ b/src/Kernel/Mnemonics/_imul.php @@ -11,8 +11,8 @@ final class _imul implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::multiply($value1, $value2, 4)); } diff --git a/src/Kernel/Mnemonics/_ior.php b/src/Kernel/Mnemonics/_ior.php index c6a53306..dff008e0 100644 --- a/src/Kernel/Mnemonics/_ior.php +++ b/src/Kernel/Mnemonics/_ior.php @@ -11,8 +11,8 @@ final class _ior implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::orBits($value1, $value2, 4)); } diff --git a/src/Kernel/Mnemonics/_ishl.php b/src/Kernel/Mnemonics/_ishl.php index a344c67c..75702a0c 100644 --- a/src/Kernel/Mnemonics/_ishl.php +++ b/src/Kernel/Mnemonics/_ishl.php @@ -11,8 +11,8 @@ final class _ishl implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::shiftLeft($value1, $value2)); } diff --git a/src/Kernel/Mnemonics/_ishr.php b/src/Kernel/Mnemonics/_ishr.php index 7d791e2b..eb54519c 100644 --- a/src/Kernel/Mnemonics/_ishr.php +++ b/src/Kernel/Mnemonics/_ishr.php @@ -11,8 +11,8 @@ final class _ishr implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::shiftRight($value1, $value2, 4)); } diff --git a/src/Kernel/Mnemonics/_isub.php b/src/Kernel/Mnemonics/_isub.php index b4bafb4a..8c200a7e 100644 --- a/src/Kernel/Mnemonics/_isub.php +++ b/src/Kernel/Mnemonics/_isub.php @@ -11,8 +11,8 @@ final class _isub implements OperationInterface public function execute(): void { - $leftValue = $this->getStack(); $rightValue = $this->getStack(); + $leftValue = $this->getStack(); $this->pushStack(BinaryTool::sub($leftValue, $rightValue, 4)); } diff --git a/src/Kernel/Mnemonics/_iushr.php b/src/Kernel/Mnemonics/_iushr.php index 6c620cbc..91f3cedb 100644 --- a/src/Kernel/Mnemonics/_iushr.php +++ b/src/Kernel/Mnemonics/_iushr.php @@ -11,8 +11,8 @@ final class _iushr implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::unsignedShiftRight($value1, $value2, 4)); } diff --git a/src/Kernel/Mnemonics/_ixor.php b/src/Kernel/Mnemonics/_ixor.php index c7d8057e..2173f8eb 100644 --- a/src/Kernel/Mnemonics/_ixor.php +++ b/src/Kernel/Mnemonics/_ixor.php @@ -11,8 +11,8 @@ final class _ixor implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::xorBits($value1, $value2, 4)); } diff --git a/src/Kernel/Mnemonics/_ladd.php b/src/Kernel/Mnemonics/_ladd.php index 9e33cf16..a28d7abd 100644 --- a/src/Kernel/Mnemonics/_ladd.php +++ b/src/Kernel/Mnemonics/_ladd.php @@ -11,8 +11,8 @@ final class _ladd implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::add($value1, $value2)); } diff --git a/src/Kernel/Mnemonics/_land.php b/src/Kernel/Mnemonics/_land.php index 92206336..2daec345 100644 --- a/src/Kernel/Mnemonics/_land.php +++ b/src/Kernel/Mnemonics/_land.php @@ -11,8 +11,8 @@ final class _land implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::andBits($value1, $value2, 8)); } diff --git a/src/Kernel/Mnemonics/_lmul.php b/src/Kernel/Mnemonics/_lmul.php index c848f55e..61ca4c9c 100644 --- a/src/Kernel/Mnemonics/_lmul.php +++ b/src/Kernel/Mnemonics/_lmul.php @@ -11,8 +11,8 @@ final class _lmul implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::multiply($value1, $value2, 8)); } diff --git a/src/Kernel/Mnemonics/_lor.php b/src/Kernel/Mnemonics/_lor.php index bb66a443..f09a3e3e 100644 --- a/src/Kernel/Mnemonics/_lor.php +++ b/src/Kernel/Mnemonics/_lor.php @@ -11,8 +11,8 @@ final class _lor implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::orBits($value1, $value2, 8)); } diff --git a/src/Kernel/Mnemonics/_lshl.php b/src/Kernel/Mnemonics/_lshl.php index 54ed1257..5db4b44f 100644 --- a/src/Kernel/Mnemonics/_lshl.php +++ b/src/Kernel/Mnemonics/_lshl.php @@ -11,8 +11,8 @@ final class _lshl implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::shiftLeft($value1, $value2)); } diff --git a/src/Kernel/Mnemonics/_lshr.php b/src/Kernel/Mnemonics/_lshr.php index 164d443c..8322f1be 100644 --- a/src/Kernel/Mnemonics/_lshr.php +++ b/src/Kernel/Mnemonics/_lshr.php @@ -11,8 +11,8 @@ final class _lshr implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::shiftRight($value1, $value2, 8)); } diff --git a/src/Kernel/Mnemonics/_lsub.php b/src/Kernel/Mnemonics/_lsub.php index e32d8a27..4d26883c 100644 --- a/src/Kernel/Mnemonics/_lsub.php +++ b/src/Kernel/Mnemonics/_lsub.php @@ -11,8 +11,8 @@ final class _lsub implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::sub($value1, $value2, 8)); } diff --git a/src/Kernel/Mnemonics/_lushr.php b/src/Kernel/Mnemonics/_lushr.php index ff715a03..66b27bb2 100644 --- a/src/Kernel/Mnemonics/_lushr.php +++ b/src/Kernel/Mnemonics/_lushr.php @@ -11,8 +11,8 @@ final class _lushr implements OperationInterface public function execute(): void { - $value1 = $this->getStack(); $value2 = $this->getStack(); + $value1 = $this->getStack(); $this->pushStack(BinaryTool::unsignedShiftRight($value1, $value2, 8)); } diff --git a/src/Utilities/BinaryTool.php b/src/Utilities/BinaryTool.php index 6d2df9b6..e62d3d0c 100644 --- a/src/Utilities/BinaryTool.php +++ b/src/Utilities/BinaryTool.php @@ -135,9 +135,9 @@ final public static function unsignedShiftRight($value1, $value2, $bytes) { $value1 = (int) $value1; $value2 = (int) $value2; - $bits = sprintf('%0' . ($bytes * 8) . 's', base_convert($value2, 10, 2)); + $bits = sprintf('%0' . ($bytes * 8) . 's', base_convert($value1, 10, 2)); - $bits = sprintf('%0' . ($bytes * 8) . 's', substr($bits, 0, strlen($bits) - $value1)); + $bits = sprintf('%0' . ($bytes * 8) . 's', substr($bits, 0, strlen($bits) - $value2)); if ($bits === '') { $bits = '0'; diff --git a/tests/BinaryOperatorTest.php b/tests/BinaryOperatorTest.php new file mode 100644 index 00000000..03970f4c --- /dev/null +++ b/tests/BinaryOperatorTest.php @@ -0,0 +1,85 @@ +initiatedJavaClasses['BinaryOperatorTest'] + ->getInvoker() + ->getStatic() + ->getMethods() + ->call($method, $value1, $value2); + + return $calculatedValue->getValue(); + } + + public function testIntAdd() + { + $actual = $this->call('intAdd', 5, 3); + $this->assertEquals(8, $actual); + } + + public function testIntSub() + { + $actual = $this->call('intSub', 5, 3); + $this->assertEquals(2, $actual); + } + + public function testIntMul() + { + $actual = $this->call('intMul', 5, 3); + $this->assertEquals(15, $actual); + } + + public function testIntShl() + { + $actual = $this->call('intShl', 3, 2); + $this->assertEquals(12, $actual); + } + + public function testIntShr() + { + $actual = $this->call('intShr', 12, 2); + $this->assertEquals(3, $actual); + } + + public function testIntUshr() + { + $actual = $this->call('intUshr', 12, 2); + $this->assertEquals(3, $actual); + } + + public function testIntAnd() + { + $value1 = (int) base_convert('0011', 2, 10); + $value2 = (int) base_convert('0101', 2, 10); + $expect = (int) base_convert('0001', 2, 10); + $actual = $this->call('intAnd', $value1, $value2); + $this->assertEquals($expect, $actual); + } + + public function testIntOr() + { + $value1 = (int) base_convert('0011', 2, 10); + $value2 = (int) base_convert('0101', 2, 10); + $expect = (int) base_convert('0111', 2, 10); + $actual = $this->call('intOr', $value1, $value2); + $this->assertEquals($expect, $actual); + } + + public function testIntXor() + { + $value1 = (int) base_convert('0011', 2, 10); + $value2 = (int) base_convert('0101', 2, 10); + $expect = (int) base_convert('0110', 2, 10); + $actual = $this->call('intXor', $value1, $value2); + $this->assertEquals($expect, $actual); + } +} diff --git a/tests/BranchIfTest.php b/tests/BranchIfTest.php new file mode 100644 index 00000000..6febbdc4 --- /dev/null +++ b/tests/BranchIfTest.php @@ -0,0 +1,70 @@ +initiatedJavaClasses['BranchIfTest'] + ->getInvoker() + ->getStatic() + ->getMethods() + ->call($method, $value1, $value2); + + return $calculatedValue->getValue(); + } + + public function testIfAcmpEq() + { + $actual = $this->call('ifAcmpEq', 'value1', 'value2'); + $this->assertEquals(0, $actual); + } + + public function testIfAcmpNe() + { + $actual = $this->call('ifAcmpNe', 'value1', 'value2'); + $this->assertEquals(1, $actual); + } + + // public function testIfIcmpEq() + // { + // $actual = $this->call('ifIcmpEq', 5, 3); + // $this->assertEquals(0, $actual); + // } + + public function testIfIcmpNe() + { + $actual = $this->call('ifIcmpNe', 5, 3); + $this->assertEquals(1, $actual); + } + + public function testIfIcmpLt() + { + $actual = $this->call('ifIcmpLt', 5, 3); + $this->assertEquals(0, $actual); + } + + public function testIfIcmpGe() + { + $actual = $this->call('ifIcmpGe', 5, 3); + $this->assertEquals(1, $actual); + } + + public function testIfIcmpGt() + { + $actual = $this->call('ifIcmpGt', 5, 3); + $this->assertEquals(1, $actual); + } + + // public function testIfIcmpLe() + // { + // $actual = $this->call('ifIcmpLe', 5, 3); + // $this->assertEquals(0, $actual); + // } +} diff --git a/tests/fixtures/java/BinaryOperatorTest.java b/tests/fixtures/java/BinaryOperatorTest.java new file mode 100644 index 00000000..553b4912 --- /dev/null +++ b/tests/fixtures/java/BinaryOperatorTest.java @@ -0,0 +1,47 @@ +class BinaryOperatorTest +{ + public static int intAdd(int value1, int value2) + { + return value1 + value2; + } + + public static int intSub(int value1, int value2) + { + return value1 - value2; + } + + public static int intMul(int value1, int value2) + { + return value1 * value2; + } + + public static int intShl(int value1, int value2) + { + return value1 << value2; + } + + public static int intShr(int value1, int value2) + { + return value1 >> value2; + } + + public static int intUshr(int value1, int value2) + { + return value1 >>> value2; + } + + public static int intAnd(int value1, int value2) + { + return value1 & value2; + } + + public static int intOr(int value1, int value2) + { + return value1 | value2; + } + + public static int intXor(int value1, int value2) + { + return value1 ^ value2; + } +} diff --git a/tests/fixtures/java/BranchIfTest.java b/tests/fixtures/java/BranchIfTest.java new file mode 100644 index 00000000..b5f2dcee --- /dev/null +++ b/tests/fixtures/java/BranchIfTest.java @@ -0,0 +1,42 @@ +class BranchIfTest +{ + public static int ifAcmpEq(String value1, String value2) + { + return value1 != value2 ? 0 : 1; + } + + public static int ifAcmpNe(String value1, String value2) + { + return value1 == value2 ? 0 : 1; + } + + public static int ifIcmpEq(int value1, int value2) + { + return value1 != value2 ? 0 : 1; + } + + public static int ifIcmpNe(int value1, int value2) + { + return value1 == value2 ? 0 : 1; + } + + public static int ifIcmpLt(int value1, int value2) + { + return value1 >= value2 ? 0 : 1; + } + + public static int ifIcmpGe(int value1, int value2) + { + return value1 < value2 ? 0 : 1; + } + + public static int ifIcmpGt(int value1, int value2) + { + return value1 <= value2 ? 0 : 1; + } + + public static int ifIcmpLe(int value1, int value2) + { + return value1 > value2 ? 0 : 1; + } +}