Ghidra is a software reverse engineering (SRE) framework
GP-2913 reflect calc_mask cpp change in java
James6a75d6c2abae
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/utils/Utils.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/utils/Utils.javaindex 2617a9159c..2fd0bd2933 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/utils/Utils.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/utils/Utils.java@@ -16,31 +16,29 @@*/package ghidra.pcodeCPort.utils;-import ghidra.pcodeCPort.context.*;+import ghidra.pcodeCPort.context.SleighError;import ghidra.sleigh.grammar.Location;public class Utils {- public static final String endl = System.getProperty( "line.separator" );-- private static long[] uintbmasks = {- 0, 0xff, 0xffff, 0xffffff, 0xffffffffL, 0xffffffffffL, 0xffffffffffffL,- 0xffffffffffffffL, 0xffffffffffffffffL- };+ public static final String endl = System.getProperty("line.separator");- public static long calc_mask( int size ) {- return uintbmasks[(size < 8) ? size : 8];+ private static long[] uintbmasks = { 0, 0xff, 0xffff, 0xffffff, 0xffffffffL, 0xffffffffffL,+ 0xffffffffffffL, 0xffffffffffffffL, 0xffffffffffffffffL };++ public static long calc_mask(int size) {+ return uintbmasks[(Integer.compareUnsigned(size, 8) < 0) ? size : 8];}- public static long pcode_right( long val, int sa ) {- if ( sa >= 64 ) {+ public static long pcode_right(long val, int sa) {+ if (sa >= 64) {return 0;}return val >>> sa;}- public static long pcode_left( long val, int sa ) {- if ( sa >= 64 ) {+ public static long pcode_left(long val, int sa) {+ if (sa >= 64) {return 0;}return val << sa;@@ -61,30 +59,30 @@ public class Utils {// return s;// }- public static boolean signbit_negative( long val, int size ) { // Return true if signbit is set- // (negative)+ public static boolean signbit_negative(long val, int size) { // Return true if signbit is set+ // (negative)long mask = 0x80;mask <<= 8 * (size - 1);return ((val & mask) != 0);}- public static long uintb_negate( long in, int size ) { // Invert bits- return ((~in) & calc_mask( size ));+ public static long uintb_negate(long in, int size) { // Invert bits+ return ((~in) & calc_mask(size));}- public static long sign_extend( long in, int sizein, int sizeout )+ public static long sign_extend(long in, int sizein, int sizeout){int signbit;long mask;signbit = sizein * 8 - 1;- in &= calc_mask( sizein );- if ( sizein >= sizeout ) {+ in &= calc_mask(sizein);+ if (sizein >= sizeout) {return in;}- if ( (in >>> signbit) != 0 ) {- mask = calc_mask( sizeout );+ if ((in >>> signbit) != 0) {+ mask = calc_mask(sizeout);long tmp = mask << signbit; // Split shift into two piecestmp = (tmp << 1) & mask; // In case, everything is shifted outin |= tmp;@@ -93,22 +91,23 @@ public class Utils {}// this used to void and changed the parameter val - can't do it in java- public static long zzz_sign_extend( long val, int bit )+ public static long zzz_sign_extend(long val, int bit){ // Sign extend -val- above -bit-long mask = 0;mask = (~mask) << bit;- if ( ((val >>> bit) & 1) != 0 ) {+ if (((val >>> bit) & 1) != 0) {val |= mask;- } else {+ }+ else {val &= (~mask);}return val;}// this used to void and changed the parameter val - can't do it in java- public static long zzz_zero_extend( long val, int bit ) { // Clear all bits in -val- above- // -bit-+ public static long zzz_zero_extend(long val, int bit) { // Clear all bits in -val- above+ // -bit-long mask = 0;mask = (~mask) << bit;mask <<= 1;@@ -117,9 +116,9 @@ public class Utils {}// this used to void and changed the parameter val - can't do it in java- public static long byte_swap( long val, int size ) { // Swap the least sig -size- bytes in val+ public static long byte_swap(long val, int size) { // Swap the least sig -size- bytes in vallong res = 0;- while ( size > 0 ) {+ while (size > 0) {res <<= 8;res |= (val & 0xff);val >>>= 8;@@ -128,9 +127,9 @@ public class Utils {return res;}- long byte_swap( int val ) { // Swap the bytes for the whole machine int+ long byte_swap(int val) { // Swap the bytes for the whole machine intlong res = 0;- for ( int i = 0; i < 4; ++i ) {+ for (int i = 0; i < 4; ++i) {res <<= 8;res |= (val & 0xff);val >>>= 8;@@ -138,21 +137,21 @@ public class Utils {return res;}- public static boolean isprint(int c) {- if (c >= 32 && c <= 126)- return true;- return false;- }+ public static boolean isprint(int c) {+ if (c >= 32 && c <= 126)+ return true;+ return false;+ }- public static boolean isascii(int c) {- if (c >= 0 && c <= 127)- return true;- return false;- }+ public static boolean isascii(int c) {+ if (c >= 0 && c <= 127)+ return true;+ return false;+ }- public static int leastsigbit_set( long val ) { // Return bit number (0=lsb)+ public static int leastsigbit_set(long val) { // Return bit number (0=lsb)// of the least signifigant bit set or -1 if none set- if ( val == 0 ) {+ if (val == 0) {return -1;}int res = 0;@@ -160,18 +159,19 @@ public class Utils {long mask = -1;do {mask >>>= sz;- if ( (mask & val) == 0 ) {+ if ((mask & val) == 0) {res += sz;val >>>= sz;}sz >>= 1;- } while ( sz != 0 );+ }+ while (sz != 0);return res;}- public static int mostsigbit_set( long val ) { // Return bit number (0=lsb)+ public static int mostsigbit_set(long val) { // Return bit number (0=lsb)// of the most signifigant bit set or -1 if none set- if ( val == 0 ) {+ if (val == 0) {return -1;}@@ -180,103 +180,108 @@ public class Utils {long mask = -1;do {mask <<= sz;- if ( (mask & val) == 0 ) {+ if ((mask & val) == 0) {res -= sz;val <<= sz;}sz >>= 1;- } while ( sz != 0 );+ }+ while (sz != 0);return res;}- public static long coveringmask( long val )+ public static long coveringmask(long val){ // Return smallest number of form 2^n-1, bigger or equal to vallong res = val;int sz = 1;- while ( sz < 64 ) {+ while (sz < 64) {res = res | (res >>> sz);sz <<= 1;}return res;}- public static String paddedHexString( long value, int padLength ) {- String decodedString = Long.toString( value, 16 );- if ( decodedString.length() >= padLength ) {+ public static String paddedHexString(long value, int padLength) {+ String decodedString = Long.toString(value, 16);+ if (decodedString.length() >= padLength) {return decodedString;}StringBuffer buffer = new StringBuffer();int missingLength = padLength - decodedString.length();- for ( int i = 0; i < missingLength; i++ ) {- buffer.append( "0" );+ for (int i = 0; i < missingLength; i++) {+ buffer.append("0");}- buffer.append( decodedString );+ buffer.append(decodedString);return buffer.toString();}- public static int unsignedCompare( long v1, long v2 ) {- if ( v1 == v2 ) {+ public static int unsignedCompare(long v1, long v2) {+ if (v1 == v2) {return 0;}- if ( v1 >= 0 && v2 >= 0 ) {+ if (v1 >= 0 && v2 >= 0) {return v1 < v2 ? -1 : 1;}- if ( v1 < 0 && v2 < 0 ) {+ if (v1 < 0 && v2 < 0) {return v1 < v2 ? -1 : 1;}- if ( v1 < 0 ) {+ if (v1 < 0) {return 1;}return -1;}- public static int unsignedCompare( int v1, int v2 ) {- if ( v1 == v2 ) {++ public static int unsignedCompare(int v1, int v2) {+ if (v1 == v2) {return 0;}- if ( v1 >= 0 && v2 >= 0 ) {+ if (v1 >= 0 && v2 >= 0) {return v1 < v2 ? -1 : 1;}- if ( v1 < 0 && v2 < 0 ) {+ if (v1 < 0 && v2 < 0) {return v1 < v2 ? -1 : 1;}- if ( v1 < 0 ) {+ if (v1 < 0) {return 1;}return -1;}- public static void calc_maskword( Location location, int sbit, int ebit, MutableInt num, MutableInt shift, MutableInt mask ) {- num.set(unsignedDivide(sbit, 8 * 4));- if(num.get() != unsignedDivide(ebit, 8 * 4)) {- throw new SleighError( "Context field not contained within one machine int", location );- }- sbit -= (int) (unsignedInt(num.get()) * 8 * 4);- ebit -= (int) (unsignedInt(num.get()) * 8 * 4);-- shift.set(8 * 4 - ebit - 1);- int m = (-1) >>> (sbit + shift.get());- m <<= shift.get();- mask.set(m);- }- public static int bytesToInt( byte[] bytes, boolean bigEndian ) {++ public static void calc_maskword(Location location, int sbit, int ebit, MutableInt num,+ MutableInt shift, MutableInt mask) {+ num.set(unsignedDivide(sbit, 8 * 4));+ if (num.get() != unsignedDivide(ebit, 8 * 4)) {+ throw new SleighError("Context field not contained within one machine int", location);+ }+ sbit -= (int) (unsignedInt(num.get()) * 8 * 4);+ ebit -= (int) (unsignedInt(num.get()) * 8 * 4);++ shift.set(8 * 4 - ebit - 1);+ int m = (-1) >>> (sbit + shift.get());+ m <<= shift.get();+ mask.set(m);+ }++ public static int bytesToInt(byte[] bytes, boolean bigEndian) {int result = 0;if (bigEndian) {- for(int i=0;i<4;i++) {+ for (int i = 0; i < 4; i++) {result <<= 8;result |= (bytes[i] & 0xFF);}}else {- for(int i=3;i>=0;i--) {+ for (int i = 3; i >= 0; i--) {result <<= 8;result |= (bytes[i] & 0xFF);}-+}return result;}-+public static long shiftLeft(long a, long b) {b &= 0xff;if (b >= 64) {@@ -284,6 +289,7 @@ public class Utils {}return a << b;}+public static long ashiftRight(long a, long b) {b &= 0xff;if (b >= 64) {@@ -291,6 +297,7 @@ public class Utils {}return a >> b;}+public static long lshiftRight(long a, long b) {b &= 0xff;if (b >= 64) {@@ -298,36 +305,41 @@ public class Utils {}return a >>> b;}+public static long unsignedInt(int a) {- long result = a;- if (result < 0) {- result += 4294967296L;- }- return result;+ long result = a;+ if (result < 0) {+ result += 4294967296L;+ }+ return result;}- public static int unsignedDivide(int a, int b) {- long la = unsignedInt(a);- long lb = unsignedInt(b);- long result = la/lb;- return (int) result;- }- public static int unsignedModulo(int a, int b) {- long la = unsignedInt(a);- long lb = unsignedInt(b);- long result = la%lb;- return (int) result;- }++ public static int unsignedDivide(int a, int b) {+ long la = unsignedInt(a);+ long lb = unsignedInt(b);+ long result = la / lb;+ return (int) result;+ }++ public static int unsignedModulo(int a, int b) {+ long la = unsignedInt(a);+ long lb = unsignedInt(b);+ long result = la % lb;+ return (int) result;+ }+public static void main(String[] args) {- System.out.println(unsignedDivide(-8, 32));- System.out.println(unsignedModulo(-8, 32));+ System.out.println(unsignedDivide(-8, 32));+ System.out.println(unsignedModulo(-8, 32));}+public static String toUnsignedIntHex(int n) {- return Long.toHexString(unsignedInt(n));+ return Long.toHexString(unsignedInt(n));}public static long bytesToLong(byte[] byteBuf) {long value = 0;- for(int i=0;i<8;i++) {+ for (int i = 0; i < 8; i++) {value = value << 8 | byteBuf[i];}return value;