GP-3155 Regenerating parsers, documentation. LZCOUNT tests.

This commit is contained in:
caheckman 2023-03-03 15:10:55 -05:00
parent e4ab760242
commit 945d91fc93
54 changed files with 3252 additions and 3211 deletions

View file

@ -42,7 +42,21 @@ public class OpBehaviorLzcount extends UnaryOpBehavior {
@Override
public BigInteger evaluateUnary(int sizeout, int sizein, BigInteger unsignedIn1) {
// TODO Auto-generated method stub
return null;
int bitcount = 0;
sizein = sizein * 8 - 1;
while (sizein >= 0) {
if (unsignedIn1.testBit(sizein)) {
break;
}
bitcount += 1;
sizein -= 1;
}
if (sizeout == 1) {
bitcount &= 0xff;
}
else if (sizeout == 2) {
bitcount &= 0xffff;
}
return BigInteger.valueOf(bitcount);
}
}

View file

@ -39,8 +39,26 @@ public class OpBehaviorPopcount extends UnaryOpBehavior {
@Override
public BigInteger evaluateUnary(int sizeout, int sizein, BigInteger unsignedIn1) {
// TODO Auto-generated method stub
return null;
int bitcount = 0;
while (sizein >= 8) {
bitcount += evaluateUnary(1, 8, unsignedIn1.longValue());
sizein -= 8;
if (sizein == 0) {
break;
}
unsignedIn1 = unsignedIn1.shiftRight(64);
}
if (sizein > 0) {
long mask = sizein * 8 - 1;
bitcount += evaluateUnary(1, 8, unsignedIn1.longValue() & mask);
}
if (sizeout == 1) {
bitcount &= 0xff;
}
else if (sizeout == 2) {
bitcount &= 0xffff;
}
return BigInteger.valueOf(bitcount);
}
}

View file

@ -0,0 +1,91 @@
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ghidra.pcode.opbehavior;
import java.math.BigInteger;
import org.junit.Assert;
import org.junit.Test;
import ghidra.pcode.utils.Utils;
public class OpBehaviorLzcountTest extends AbstractOpBehaviorTest {
@Test
public void testEvaluateUnaryLong() {
OpBehaviorLzcount op = new OpBehaviorLzcount();
Assert.assertEquals(8, op.evaluateUnary(1, 1, 0L));
Assert.assertEquals(16, op.evaluateUnary(1, 2, 0L));
Assert.assertEquals(32, op.evaluateUnary(1, 4, 0L));
Assert.assertEquals(64, op.evaluateUnary(1, 8, 0L));
Assert.assertEquals(0, op.evaluateUnary(1, 1, 0xffL));
Assert.assertEquals(0, op.evaluateUnary(1, 2, 0xffffL));
Assert.assertEquals(0, op.evaluateUnary(1, 4, 0xffffffffL));
Assert.assertEquals(0, op.evaluateUnary(1, 8, 0xffffffffffffffffL));
Assert.assertEquals(0, op.evaluateUnary(1, 1, 0x96L));
Assert.assertEquals(0, op.evaluateUnary(1, 2, 0xdbf4L));
Assert.assertEquals(1, op.evaluateUnary(1, 4, 0x460f457bL));
Assert.assertEquals(3, op.evaluateUnary(1, 8, 0x1fae97efca7d5759L));
Assert.assertEquals(4, op.evaluateUnary(1, 1, 0xaL));
Assert.assertEquals(6, op.evaluateUnary(1, 2, 0x2a5L));
Assert.assertEquals(9, op.evaluateUnary(1, 4, 0x60dfffL));
Assert.assertEquals(13, op.evaluateUnary(1, 8, 0x635017adefe4eL));
Assert.assertEquals(3, op.evaluateUnary(1, 1, 0x17L));
Assert.assertEquals(8, op.evaluateUnary(1, 2, 0xd1L));
Assert.assertEquals(22, op.evaluateUnary(1, 4, 0x39eL));
Assert.assertEquals(27, op.evaluateUnary(1, 8, 0x189c178d6aL));
Assert.assertEquals(4, op.evaluateUnary(1, 1, 0xfL));
Assert.assertEquals(4, op.evaluateUnary(1, 2, 0xff0L));
Assert.assertEquals(0, op.evaluateUnary(1, 4, 0xffff0000L));
Assert.assertEquals(24, op.evaluateUnary(1, 8, 0xff00ff00ffL));
}
@Test
public void testEvaluateUnaryBigInteger() {
OpBehaviorLzcount op = new OpBehaviorLzcount();
BigInteger NEGATIVE_ONE = Utils.convertToUnsignedValue(BigInteger.valueOf(-1), 16);
BigInteger BIG_POSITIVE = new BigInteger("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
BigInteger BIG_NEGATIVE = Utils
.convertToUnsignedValue(new BigInteger("80000000000000000000000000000000", 16), 16);
assertEquals(BigInteger.valueOf(128), op.evaluateUnary(1, 16, BigInteger.ZERO), 16);
assertEquals(BigInteger.ZERO, op.evaluateUnary(1, 16, NEGATIVE_ONE), 16);
assertEquals(BigInteger.ONE, op.evaluateUnary(1, 16, BIG_POSITIVE), 16);
assertEquals(BigInteger.ZERO, op.evaluateUnary(1, 16, BIG_NEGATIVE), 16);
BigInteger val = BigInteger.valueOf(0x35017adefe4eL);
val = val.shiftLeft(64);
val = val.or(Utils.convertToUnsignedValue(BigInteger.valueOf(0xd46223189c178d6aL), 8));
assertEquals(BigInteger.valueOf(18), op.evaluateUnary(1, 16, val), 16);
BigInteger FF = BigInteger.valueOf(0xff);
val = BigInteger.ZERO;
for (int i = 0; i < 20; ++i) {
val = val.shiftLeft(16);
val = val.add(FF);
}
assertEquals(BigInteger.valueOf(8), op.evaluateUnary(1, 40, val), 40);
}
}

View file

@ -0,0 +1,90 @@
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ghidra.pcode.opbehavior;
import java.math.BigInteger;
import org.junit.Assert;
import org.junit.Test;
import ghidra.pcode.utils.Utils;
public class OpBehaviorPopcountTest extends AbstractOpBehaviorTest {
@Test
public void testEvaluateUnaryLong() {
OpBehaviorPopcount op = new OpBehaviorPopcount();
Assert.assertEquals(0, op.evaluateUnary(1, 1, 0L));
Assert.assertEquals(0, op.evaluateUnary(1, 2, 0L));
Assert.assertEquals(0, op.evaluateUnary(1, 4, 0L));
Assert.assertEquals(0, op.evaluateUnary(1, 8, 0L));
Assert.assertEquals(8, op.evaluateUnary(1, 1, 0xffL));
Assert.assertEquals(16, op.evaluateUnary(1, 2, 0xffffL));
Assert.assertEquals(32, op.evaluateUnary(1, 4, 0xffffffffL));
Assert.assertEquals(64, op.evaluateUnary(1, 8, 0xffffffffffffffffL));
Assert.assertEquals(4, op.evaluateUnary(1, 1, 0x96L));
Assert.assertEquals(11, op.evaluateUnary(1, 2, 0xdbf4L));
Assert.assertEquals(16, op.evaluateUnary(1, 4, 0x460f457bL));
Assert.assertEquals(41, op.evaluateUnary(1, 8, 0x1fae97efca7d5759L));
Assert.assertEquals(5, op.evaluateUnary(1, 1, 0x7aL));
Assert.assertEquals(10, op.evaluateUnary(1, 2, 0xfca5L));
Assert.assertEquals(20, op.evaluateUnary(1, 4, 0x2660dfffL));
Assert.assertEquals(38, op.evaluateUnary(1, 8, 0x79f635017adefe4eL));
Assert.assertEquals(4, op.evaluateUnary(1, 1, 0x17L));
Assert.assertEquals(10, op.evaluateUnary(1, 2, 0x77d1L));
Assert.assertEquals(15, op.evaluateUnary(1, 4, 0x5758039eL));
Assert.assertEquals(28, op.evaluateUnary(1, 8, 0xd46223189c178d6aL));
Assert.assertEquals(7, op.evaluateUnary(1, 1, 0xbfL));
Assert.assertEquals(12, op.evaluateUnary(1, 2, 0xe3efL));
Assert.assertEquals(17, op.evaluateUnary(1, 4, 0xb2d7e134L));
Assert.assertEquals(34, op.evaluateUnary(1, 8, 0x69f7a0fa6eeda6L));
}
@Test
public void testEvaluateUnaryBigInteger() {
OpBehaviorPopcount op = new OpBehaviorPopcount();
BigInteger NEGATIVE_ONE = Utils.convertToUnsignedValue(BigInteger.valueOf(-1), 16);
BigInteger BIG_POSITIVE = new BigInteger("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
BigInteger BIG_NEGATIVE = Utils
.convertToUnsignedValue(new BigInteger("80000000000000000000000000000000", 16), 16);
assertEquals(BigInteger.ZERO, op.evaluateUnary(1, 16, BigInteger.ZERO), 16);
assertEquals(BigInteger.valueOf(128), op.evaluateUnary(1, 16, NEGATIVE_ONE), 16);
assertEquals(BigInteger.valueOf(127), op.evaluateUnary(1, 16, BIG_POSITIVE), 16);
assertEquals(BigInteger.ONE, op.evaluateUnary(1, 16, BIG_NEGATIVE), 16);
BigInteger val = BigInteger.valueOf(0x79f635017adefe4eL);
val = val.shiftLeft(64);
val = val.or(Utils.convertToUnsignedValue(BigInteger.valueOf(0xd46223189c178d6aL), 8));
assertEquals(BigInteger.valueOf(66), op.evaluateUnary(1, 16, val), 16);
BigInteger FF = BigInteger.valueOf(0xff);
val = BigInteger.ZERO;
for (int i = 0; i < 20; ++i) {
val = val.shiftLeft(16);
val = val.add(FF);
}
assertEquals(BigInteger.valueOf(160), op.evaluateUnary(1, 40, val), 40);
}
}