From a676a0e39498e2130498dcff2f015bcaadf7ec9f Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 12 May 2019 15:59:22 +0900 Subject: [PATCH 1/5] Add mnemonics --- src/Kernel/Mnemonics/_baload.php | 8 +++++++- src/Kernel/Mnemonics/_bastore.php | 17 ++++++++++++++++- src/Kernel/Mnemonics/_caload.php | 8 +++++++- src/Kernel/Mnemonics/_daload.php | 4 +++- src/Kernel/Mnemonics/_dastore.php | 10 ++++++++++ src/Kernel/Mnemonics/_dstore.php | 9 +++++++-- src/Kernel/Mnemonics/_faload.php | 9 ++++++++- src/Kernel/Mnemonics/_fastore.php | 17 ++++++++++++++++- src/Kernel/Mnemonics/_saload.php | 8 +++++++- src/Kernel/Mnemonics/_sastore.php | 17 ++++++++++++++++- 10 files changed, 97 insertions(+), 10 deletions(-) diff --git a/src/Kernel/Mnemonics/_baload.php b/src/Kernel/Mnemonics/_baload.php index 87b84e98..4e5808db 100644 --- a/src/Kernel/Mnemonics/_baload.php +++ b/src/Kernel/Mnemonics/_baload.php @@ -2,6 +2,7 @@ namespace PHPJava\Kernel\Mnemonics; use PHPJava\Exceptions\NotImplementedException; +use PHPJava\Utilities\Extractor; final class _baload implements OperationInterface { @@ -10,6 +11,11 @@ final class _baload implements OperationInterface public function execute(): void { - throw new NotImplementedException(__CLASS__); + $index = Extractor::getRealValue($this->popFromOperandStack()); + $arrayref = $this->popFromOperandStack(); + + $this->pushToOperandStack( + $arrayref[$index] + ); } } diff --git a/src/Kernel/Mnemonics/_bastore.php b/src/Kernel/Mnemonics/_bastore.php index 163059bc..29273300 100644 --- a/src/Kernel/Mnemonics/_bastore.php +++ b/src/Kernel/Mnemonics/_bastore.php @@ -2,6 +2,8 @@ namespace PHPJava\Kernel\Mnemonics; use PHPJava\Exceptions\NotImplementedException; +use PHPJava\Kernel\Types\Type; +use PHPJava\Utilities\Extractor; final class _bastore implements OperationInterface { @@ -10,6 +12,19 @@ final class _bastore implements OperationInterface public function execute(): void { - throw new NotImplementedException(__CLASS__); + $value = $this->popFromOperandStack(); + $index = Extractor::getRealValue($this->popFromOperandStack()); + + /** + * @var Type $arrayref + */ + $arrayref = $this->popFromOperandStack(); + + // The value is a ref. + $arrayref[$index] = $value; + + $this->pushToOperandStack( + $arrayref + ); } } diff --git a/src/Kernel/Mnemonics/_caload.php b/src/Kernel/Mnemonics/_caload.php index 7bd06609..48d5804d 100644 --- a/src/Kernel/Mnemonics/_caload.php +++ b/src/Kernel/Mnemonics/_caload.php @@ -2,6 +2,7 @@ namespace PHPJava\Kernel\Mnemonics; use PHPJava\Exceptions\NotImplementedException; +use PHPJava\Utilities\Extractor; final class _caload implements OperationInterface { @@ -10,6 +11,11 @@ final class _caload implements OperationInterface public function execute(): void { - throw new NotImplementedException(__CLASS__); + $index = Extractor::getRealValue($this->popFromOperandStack()); + $arrayref = $this->popFromOperandStack(); + + $this->pushToOperandStack( + $arrayref[$index] + ); } } diff --git a/src/Kernel/Mnemonics/_daload.php b/src/Kernel/Mnemonics/_daload.php index cba13ec3..25ed5f25 100644 --- a/src/Kernel/Mnemonics/_daload.php +++ b/src/Kernel/Mnemonics/_daload.php @@ -16,6 +16,8 @@ public function execute(): void $index = Extractor::getRealValue($this->popFromOperandStack()); $arrayref = $this->popFromOperandStack(); - $this->pushToOperandStack($arrayref[$index]); + $this->pushToOperandStack( + $arrayref[$index] + ); } } diff --git a/src/Kernel/Mnemonics/_dastore.php b/src/Kernel/Mnemonics/_dastore.php index d7d066fe..702cdf41 100644 --- a/src/Kernel/Mnemonics/_dastore.php +++ b/src/Kernel/Mnemonics/_dastore.php @@ -1,6 +1,7 @@ popFromOperandStack(); $index = Extractor::getRealValue($this->popFromOperandStack()); + + /** + * @var Type $arrayref + */ $arrayref = $this->popFromOperandStack(); + // The value is a ref. $arrayref[$index] = $value; + + $this->pushToOperandStack( + $arrayref + ); } } diff --git a/src/Kernel/Mnemonics/_dstore.php b/src/Kernel/Mnemonics/_dstore.php index c86581f6..562bf1fe 100644 --- a/src/Kernel/Mnemonics/_dstore.php +++ b/src/Kernel/Mnemonics/_dstore.php @@ -1,7 +1,9 @@ readUnsignedByte(); - $value = $this->popFromOperandStack(); + $value = Extractor::getRealValue($this->popFromOperandStack()); - $this->setLocalStorage($index, BinaryTool::convertDoubleToIEEE754($value)); + $this->setLocalStorage( + $index, + $value + ); } } diff --git a/src/Kernel/Mnemonics/_faload.php b/src/Kernel/Mnemonics/_faload.php index 2855aaed..0ccf1f8c 100644 --- a/src/Kernel/Mnemonics/_faload.php +++ b/src/Kernel/Mnemonics/_faload.php @@ -2,6 +2,8 @@ namespace PHPJava\Kernel\Mnemonics; use PHPJava\Exceptions\NotImplementedException; +use PHPJava\Kernel\Types\Type; +use PHPJava\Utilities\Extractor; final class _faload implements OperationInterface { @@ -10,6 +12,11 @@ final class _faload implements OperationInterface public function execute(): void { - throw new NotImplementedException(__CLASS__); + $index = Extractor::getRealValue($this->popFromOperandStack()); + $arrayref = $this->popFromOperandStack(); + + $this->pushToOperandStack( + $arrayref[$index] + ); } } diff --git a/src/Kernel/Mnemonics/_fastore.php b/src/Kernel/Mnemonics/_fastore.php index a726b0d9..6fb9ea6e 100644 --- a/src/Kernel/Mnemonics/_fastore.php +++ b/src/Kernel/Mnemonics/_fastore.php @@ -2,6 +2,8 @@ namespace PHPJava\Kernel\Mnemonics; use PHPJava\Exceptions\NotImplementedException; +use PHPJava\Kernel\Types\Type; +use PHPJava\Utilities\Extractor; final class _fastore implements OperationInterface { @@ -10,6 +12,19 @@ final class _fastore implements OperationInterface public function execute(): void { - throw new NotImplementedException(__CLASS__); + $value = $this->popFromOperandStack(); + $index = Extractor::getRealValue($this->popFromOperandStack()); + + /** + * @var Type $arrayref + */ + $arrayref = $this->popFromOperandStack(); + + // The value is a ref. + $arrayref[$index] = $value; + + $this->pushToOperandStack( + $arrayref + ); } } diff --git a/src/Kernel/Mnemonics/_saload.php b/src/Kernel/Mnemonics/_saload.php index e3382fc6..f079435c 100644 --- a/src/Kernel/Mnemonics/_saload.php +++ b/src/Kernel/Mnemonics/_saload.php @@ -2,6 +2,7 @@ namespace PHPJava\Kernel\Mnemonics; use PHPJava\Exceptions\NotImplementedException; +use PHPJava\Utilities\Extractor; final class _saload implements OperationInterface { @@ -10,6 +11,11 @@ final class _saload implements OperationInterface public function execute(): void { - throw new NotImplementedException(__CLASS__); + $index = Extractor::getRealValue($this->popFromOperandStack()); + $arrayref = $this->popFromOperandStack(); + + $this->pushToOperandStack( + $arrayref[$index] + ); } } diff --git a/src/Kernel/Mnemonics/_sastore.php b/src/Kernel/Mnemonics/_sastore.php index 95d9e149..df5e6810 100644 --- a/src/Kernel/Mnemonics/_sastore.php +++ b/src/Kernel/Mnemonics/_sastore.php @@ -2,6 +2,8 @@ namespace PHPJava\Kernel\Mnemonics; use PHPJava\Exceptions\NotImplementedException; +use PHPJava\Kernel\Types\Type; +use PHPJava\Utilities\Extractor; final class _sastore implements OperationInterface { @@ -10,6 +12,19 @@ final class _sastore implements OperationInterface public function execute(): void { - throw new NotImplementedException(__CLASS__); + $value = $this->popFromOperandStack(); + $index = Extractor::getRealValue($this->popFromOperandStack()); + + /** + * @var Type $arrayref + */ + $arrayref = $this->popFromOperandStack(); + + // The value is a ref. + $arrayref[$index] = $value; + + $this->pushToOperandStack( + $arrayref + ); } } From 60c27627366d44e740e6f2f9dc316f8876c38f55 Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 12 May 2019 16:30:53 +0900 Subject: [PATCH 2/5] Add array tests --- src/Kernel/Mnemonics/_baload.php | 1 - src/Kernel/Mnemonics/_bastore.php | 1 - src/Kernel/Mnemonics/_caload.php | 1 - src/Kernel/Mnemonics/_dstore.php | 2 - src/Kernel/Mnemonics/_faload.php | 2 - src/Kernel/Mnemonics/_fastore.php | 1 - src/Kernel/Mnemonics/_saload.php | 1 - src/Kernel/Mnemonics/_sastore.php | 1 - tests/ArrayTest.php | 60 ++++++++++++++++++++++++++++++ tests/fixtures/java/ArrayTest.java | 36 +++++++++++++++++- 10 files changed, 94 insertions(+), 12 deletions(-) diff --git a/src/Kernel/Mnemonics/_baload.php b/src/Kernel/Mnemonics/_baload.php index 4e5808db..05fc02ef 100644 --- a/src/Kernel/Mnemonics/_baload.php +++ b/src/Kernel/Mnemonics/_baload.php @@ -1,7 +1,6 @@ assertEquals('bar', $actual->offsetGet(1)); $this->assertEquals('baz', $actual->offsetGet(2)); } + + public function testCreateLongArray() + { + $actual = $this->call('createLongArray'); + + $this->assertEquals(3, $actual->count()); + $this->assertEquals('1', $actual->offsetGet(0)); + $this->assertEquals('2', $actual->offsetGet(1)); + $this->assertEquals('3', $actual->offsetGet(2)); + } + + public function testCreateFloatArray() + { + $actual = $this->call('createFloatArray'); + + $this->assertEquals(3, $actual->count()); + $this->assertEquals('1.5', $actual->offsetGet(0)); + $this->assertEquals('2.5', $actual->offsetGet(1)); + $this->assertEquals('3.5', $actual->offsetGet(2)); + } + + public function testCreateDoubleArray() + { + $actual = $this->call('createDoubleArray'); + + $this->assertEquals(3, $actual->count()); + $this->assertEquals('1.5', $actual->offsetGet(0)); + $this->assertEquals('2.5', $actual->offsetGet(1)); + $this->assertEquals('3.5', $actual->offsetGet(2)); + } + + public function testCreateBooleanArray() + { + $actual = $this->call('createBooleanArray'); + + $this->assertEquals(3, $actual->count()); + $this->assertEquals('1', $actual->offsetGet(0)); + $this->assertEquals('0', $actual->offsetGet(1)); + $this->assertEquals('1', $actual->offsetGet(2)); + } + + public function testCreateCharArray() + { + $actual = $this->call('createCharArray'); + + $this->assertEquals(3, $actual->count()); + $this->assertEquals('A', $actual->offsetGet(0)); + $this->assertEquals('B', $actual->offsetGet(1)); + $this->assertEquals('C', $actual->offsetGet(2)); + } + + public function testCreateByteArray() + { + $actual = $this->call('createByteArray'); + + $this->assertEquals(3, $actual->count()); + $this->assertEquals('1', $actual->offsetGet(0)); + $this->assertEquals('2', $actual->offsetGet(1)); + $this->assertEquals('3', $actual->offsetGet(2)); + } } diff --git a/tests/fixtures/java/ArrayTest.java b/tests/fixtures/java/ArrayTest.java index 17a63612..bf27e58b 100644 --- a/tests/fixtures/java/ArrayTest.java +++ b/tests/fixtures/java/ArrayTest.java @@ -2,11 +2,43 @@ class ArrayTest { public static int[] createIntArray() { - return new int[] { 1, 2, 3 }; + return new int[] { 1, 2, 3 }; } public static String[] createStringArray() { - return new String[] { "foo", "bar", "baz" }; + return new String[] { "foo", "bar", "baz" }; + } + + public static long[] createLongArray() + { + return new long[] { 1, 2, 3 }; + } + + public static float[] createFloatArray() + { + return new float[] { 1.5F, 2.5F, 3.5F }; + } + + public static double[] createDoubleArray() + { + return new double[] { 1.5, 2.5, 3.5 }; + } + + public static boolean[] createBooleanArray() + { + return new boolean[] { true, false, true }; + } + + public static char[] createCharArray() + { + return new char[] { 'A', 'B', 'C' }; + } + + + public static byte[] createByteArray() + { + return new byte[] { 0x01, 0x02, 0x03 }; + } } From 280f39eac4200119aac3529a3bb90a93e8e34c5f Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 12 May 2019 18:24:07 +0900 Subject: [PATCH 3/5] Add negation tests --- src/Kernel/Mnemonics/_dneg.php | 14 ++- src/Kernel/Mnemonics/_fneg.php | 14 ++- src/Kernel/Mnemonics/_ineg.php | 12 ++- src/Kernel/Mnemonics/_lneg.php | 14 ++- src/Utilities/BinaryTool.php | 13 +-- tests/NegationTest.php | 136 ++++++++++++++++++++++++++ tests/fixtures/java/NegationTest.java | 22 +++++ 7 files changed, 206 insertions(+), 19 deletions(-) create mode 100644 tests/NegationTest.php create mode 100644 tests/fixtures/java/NegationTest.java diff --git a/src/Kernel/Mnemonics/_dneg.php b/src/Kernel/Mnemonics/_dneg.php index 32612550..7646add1 100644 --- a/src/Kernel/Mnemonics/_dneg.php +++ b/src/Kernel/Mnemonics/_dneg.php @@ -1,7 +1,9 @@ popFromOperandStack() + ); + + $this->pushToOperandStack( + _Double::get( + BinaryTool::negate($value) + ) + ); } } diff --git a/src/Kernel/Mnemonics/_fneg.php b/src/Kernel/Mnemonics/_fneg.php index d5baf26b..6de3753a 100644 --- a/src/Kernel/Mnemonics/_fneg.php +++ b/src/Kernel/Mnemonics/_fneg.php @@ -1,7 +1,9 @@ popFromOperandStack() + ); + + $this->pushToOperandStack( + _Float::get( + BinaryTool::negate($value) + ) + ); } } diff --git a/src/Kernel/Mnemonics/_ineg.php b/src/Kernel/Mnemonics/_ineg.php index b362a472..eff1c517 100644 --- a/src/Kernel/Mnemonics/_ineg.php +++ b/src/Kernel/Mnemonics/_ineg.php @@ -1,7 +1,9 @@ popFromOperandStack(); + $value = Extractor::getRealValue( + $this->popFromOperandStack() + ); - $this->pushToOperandStack(BinaryTool::negate($value, 4)); + $this->pushToOperandStack( + _Int::get( + BinaryTool::negate($value) + ) + ); } } diff --git a/src/Kernel/Mnemonics/_lneg.php b/src/Kernel/Mnemonics/_lneg.php index d6996902..51cfbf1f 100644 --- a/src/Kernel/Mnemonics/_lneg.php +++ b/src/Kernel/Mnemonics/_lneg.php @@ -1,7 +1,9 @@ popFromOperandStack() + ); + + $this->pushToOperandStack( + _Long::get( + BinaryTool::negate($value) + ) + ); } } diff --git a/src/Utilities/BinaryTool.php b/src/Utilities/BinaryTool.php index f993bba5..786176fd 100644 --- a/src/Utilities/BinaryTool.php +++ b/src/Utilities/BinaryTool.php @@ -52,18 +52,9 @@ final public static function toSigned($value, $bytes) return '-' . base_convert(self::addOneBit(self::reverseBits($convert)), 2, 10); } - final public static function negate($value, $bytes) + final public static function negate($value) { - $value = (int) $value; - $value = base_convert((string) $value, 10, 2); - - if (sprintf('%0' . $bytes . 's', $value) === str_repeat('0', $bytes)) { - // zero number was overflow - return '0'; - } - - $convert = self::addOneBit(self::reverseBits($value)); - return ($convert[0] === '1' ? '-' : '') . base_convert($convert, 2, 10); + return $value * -1; } final public static function multiply($value1, $value2) diff --git a/tests/NegationTest.php b/tests/NegationTest.php new file mode 100644 index 00000000..60813343 --- /dev/null +++ b/tests/NegationTest.php @@ -0,0 +1,136 @@ +initiatedJavaClasses['NegationTest'] + ->getInvoker() + ->getStatic() + ->getMethods() + ->call( + $method, + ...$arguments + ); + + return $calculatedValue->getValue(); + } + + public function testNegateIntPattern1() + { + $actual = $this->call( + 'negateInt', + _Int::get(123) + ); + $this->assertEquals('-123', $actual); + } + + public function testNegateIntPattern2() + { + $actual = $this->call( + 'negateInt', + _Int::get(-123) + ); + $this->assertEquals('123', $actual); + } + + public function testNegateDoublePattern1() + { + $actual = $this->call( + 'negateDouble', + _Double::get(123) + ); + $this->assertEquals('-123', $actual); + } + + public function testNegateDoublePattern2() + { + $actual = $this->call( + 'negateDouble', + _Double::get(-123) + ); + $this->assertEquals('123', $actual); + } + + public function testNegateDoublePattern3() + { + $actual = $this->call( + 'negateDouble', + _Double::get(123.5) + ); + $this->assertEquals('-123.5', $actual); + } + + public function testNegateDoublePattern4() + { + $actual = $this->call( + 'negateDouble', + _Double::get(-123.5) + ); + $this->assertEquals('123.5', $actual); + } + + public function testNegateFloatPattern1() + { + $actual = $this->call( + 'negateFloat', + _Float::get(123) + ); + $this->assertEquals('-123', $actual); + } + + public function testNegateFloatPattern2() + { + $actual = $this->call( + 'negateFloat', + _Float::get(-123) + ); + $this->assertEquals('123', $actual); + } + + public function testNegateFloatPattern3() + { + $actual = $this->call( + 'negateFloat', + _Float::get(123.5) + ); + $this->assertEquals('-123.5', $actual); + } + + public function testNegateFloatPattern4() + { + $actual = $this->call( + 'negateFloat', + _Float::get(-123.5) + ); + $this->assertEquals('123.5', $actual); + } + + public function testNegateLongPattern1() + { + $actual = $this->call( + 'negateLong', + _Long::get(123) + ); + $this->assertEquals('-123', $actual); + } + + public function testNegateLongPattern2() + { + $actual = $this->call( + 'negateLong', + _Long::get(-123) + ); + $this->assertEquals('123', $actual); + } +} diff --git a/tests/fixtures/java/NegationTest.java b/tests/fixtures/java/NegationTest.java new file mode 100644 index 00000000..99f4e264 --- /dev/null +++ b/tests/fixtures/java/NegationTest.java @@ -0,0 +1,22 @@ +class NegationTest +{ + public static int negateInt(int a) + { + return -a; + } + + public static double negateDouble(double a) + { + return -a; + } + + public static float negateFloat(float a) + { + return -a; + } + + public static long negateLong(long a) + { + return -a; + } +} From 14916f878328b9ab16f8ebaf712ad00b6d4c700b Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 12 May 2019 18:30:53 +0900 Subject: [PATCH 4/5] Add lstore_ --- src/Kernel/Mnemonics/_lstore_0.php | 4 +--- src/Kernel/Mnemonics/_lstore_1.php | 4 +--- src/Kernel/Mnemonics/_lstore_2.php | 4 +--- src/Kernel/Mnemonics/_lstore_3.php | 4 +--- 4 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Kernel/Mnemonics/_lstore_0.php b/src/Kernel/Mnemonics/_lstore_0.php index 2c8ec341..96caece7 100644 --- a/src/Kernel/Mnemonics/_lstore_0.php +++ b/src/Kernel/Mnemonics/_lstore_0.php @@ -1,8 +1,6 @@ setLocalStorage(0, $this->popFromOperandStack()); } } diff --git a/src/Kernel/Mnemonics/_lstore_1.php b/src/Kernel/Mnemonics/_lstore_1.php index e3e65dbc..34c2c6e9 100644 --- a/src/Kernel/Mnemonics/_lstore_1.php +++ b/src/Kernel/Mnemonics/_lstore_1.php @@ -1,8 +1,6 @@ setLocalStorage(1, $this->popFromOperandStack()); } } diff --git a/src/Kernel/Mnemonics/_lstore_2.php b/src/Kernel/Mnemonics/_lstore_2.php index 13c4c568..1907a806 100644 --- a/src/Kernel/Mnemonics/_lstore_2.php +++ b/src/Kernel/Mnemonics/_lstore_2.php @@ -1,8 +1,6 @@ setLocalStorage(2, $this->popFromOperandStack()); } } diff --git a/src/Kernel/Mnemonics/_lstore_3.php b/src/Kernel/Mnemonics/_lstore_3.php index 2898f836..421a0050 100644 --- a/src/Kernel/Mnemonics/_lstore_3.php +++ b/src/Kernel/Mnemonics/_lstore_3.php @@ -1,8 +1,6 @@ setLocalStorage(3, $this->popFromOperandStack()); } } From e35b8cdde177f9da3b60c661ccdc211aafca1dfc Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 12 May 2019 18:32:41 +0900 Subject: [PATCH 5/5] Add lxor --- src/Kernel/Mnemonics/_lxor.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Kernel/Mnemonics/_lxor.php b/src/Kernel/Mnemonics/_lxor.php index 5779ba74..f848ddd9 100644 --- a/src/Kernel/Mnemonics/_lxor.php +++ b/src/Kernel/Mnemonics/_lxor.php @@ -1,7 +1,8 @@ popFromOperandStack(); + $value1 = $this->popFromOperandStack(); + + $this->pushToOperandStack( + _Long::get( + BinaryTool::xorBits( + Extractor::getRealValue($value1), + Extractor::getRealValue($value2), + 8 + ) + ) + ); } }