mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-05 10:49:34 +02:00
Merge remote-tracking branch
'origin/GP-2913_ghidracadabra_PR-4595_kkaempf_improve_sleigh_error_reporting' (Closes #4595)
This commit is contained in:
commit
93d3b41d01
4 changed files with 147 additions and 116 deletions
|
@ -479,7 +479,7 @@ inline bool Range::contains(const Address &addr) const {
|
||||||
|
|
||||||
/// \param size is the desired size in bytes
|
/// \param size is the desired size in bytes
|
||||||
/// \return a value appropriate for masking off the first \e size bytes
|
/// \return a value appropriate for masking off the first \e size bytes
|
||||||
inline uintb calc_mask(int4 size) { return uintbmasks[(size<8)? size : 8]; }
|
inline uintb calc_mask(int4 size) { return uintbmasks[((uint4)size) < 8 ? size : 8]; }
|
||||||
|
|
||||||
/// Perform a CPUI_INT_RIGHT on the given val
|
/// Perform a CPUI_INT_RIGHT on the given val
|
||||||
/// \param val is the value to shift
|
/// \param val is the value to shift
|
||||||
|
|
|
@ -1945,8 +1945,10 @@ void SleighCompile::buildPatterns(void)
|
||||||
errors += 1;
|
errors += 1;
|
||||||
}
|
}
|
||||||
for(int4 i=0;i<tables.size();++i) {
|
for(int4 i=0;i<tables.size();++i) {
|
||||||
if (tables[i]->isError())
|
if (tables[i]->isError()) {
|
||||||
|
reportError(getLocation(tables[i]), "Problem in table '"+tables[i]->getName() + "':" + msg.str());
|
||||||
errors += 1;
|
errors += 1;
|
||||||
|
}
|
||||||
if (tables[i]->getPattern() == (TokenPattern *)0) {
|
if (tables[i]->getPattern() == (TokenPattern *)0) {
|
||||||
reportWarning(getLocation(tables[i]), "Unreferenced table '"+tables[i]->getName() + "'");
|
reportWarning(getLocation(tables[i]), "Unreferenced table '"+tables[i]->getName() + "'");
|
||||||
}
|
}
|
||||||
|
@ -2133,8 +2135,11 @@ void SleighCompile::checkCaseSensitivity(void)
|
||||||
SleighSymbol *oldsym = (*check.first).second;
|
SleighSymbol *oldsym = (*check.first).second;
|
||||||
ostringstream s;
|
ostringstream s;
|
||||||
s << "Name collision: " << sym->getName() << " --- ";
|
s << "Name collision: " << sym->getName() << " --- ";
|
||||||
|
s << "Duplicate symbol " << oldsym->getName();
|
||||||
const Location *oldLocation = getLocation(oldsym);
|
const Location *oldLocation = getLocation(oldsym);
|
||||||
s << "Duplicate symbol " << oldsym->getName() << " defined at " << oldLocation->format();
|
if (oldLocation != (Location *) 0x0) {
|
||||||
|
s << " defined at " << oldLocation->format();
|
||||||
|
}
|
||||||
const Location *location = getLocation(sym);
|
const Location *location = getLocation(sym);
|
||||||
reportError(location,s.str());
|
reportError(location,s.str());
|
||||||
}
|
}
|
||||||
|
@ -2188,11 +2193,15 @@ const Location *SleighCompile::getLocation(Constructor *ctor) const
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \param sym is the given symbol
|
/// \param sym is the given symbol
|
||||||
/// \return the filename and line number
|
/// \return the filename and line number or null if location not found
|
||||||
const Location *SleighCompile::getLocation(SleighSymbol *sym) const
|
const Location *SleighCompile::getLocation(SleighSymbol *sym) const
|
||||||
|
|
||||||
{
|
{
|
||||||
return &symbolLocationMap.at(sym);
|
try {
|
||||||
|
return &symbolLocationMap.at(sym);
|
||||||
|
} catch (const out_of_range &e) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The current filename and line number are placed into a Location object
|
/// The current filename and line number are placed into a Location object
|
||||||
|
@ -2241,7 +2250,7 @@ void SleighCompile::reportError(const Location* loc, const string &msg)
|
||||||
void SleighCompile::reportError(const string &msg)
|
void SleighCompile::reportError(const string &msg)
|
||||||
|
|
||||||
{
|
{
|
||||||
cerr << "ERROR " << msg << endl;
|
cerr << filename.back() << ":" << lineno.back() << " - ERROR " << msg << endl;
|
||||||
errors += 1;
|
errors += 1;
|
||||||
if (errors > 1000000) {
|
if (errors > 1000000) {
|
||||||
cerr << "Too many errors: Aborting" << endl;
|
cerr << "Too many errors: Aborting" << endl;
|
||||||
|
@ -2670,7 +2679,10 @@ void SleighCompile::attachValues(vector<SleighSymbol *> *symlist,vector<intb> *n
|
||||||
if (sym == (ValueSymbol *)0) continue;
|
if (sym == (ValueSymbol *)0) continue;
|
||||||
PatternValue *patval = sym->getPatternValue();
|
PatternValue *patval = sym->getPatternValue();
|
||||||
if (patval->maxValue() + 1 != numlist->size()) {
|
if (patval->maxValue() + 1 != numlist->size()) {
|
||||||
reportError(getCurrentLocation(), "Attach value '" + sym->getName() + "' is wrong size for list");
|
ostringstream msg;
|
||||||
|
msg << "Attach value '" + sym->getName();
|
||||||
|
msg << "' (range 0.." << patval->maxValue() << ") is wrong size for list (of " << numlist->size() << " entries)";
|
||||||
|
reportError(getCurrentLocation(), msg.str());
|
||||||
}
|
}
|
||||||
symtab.replaceSymbol(sym, new ValueMapSymbol(sym->getName(),patval,*numlist));
|
symtab.replaceSymbol(sym, new ValueMapSymbol(sym->getName(),patval,*numlist));
|
||||||
}
|
}
|
||||||
|
@ -2695,7 +2707,10 @@ void SleighCompile::attachNames(vector<SleighSymbol *> *symlist,vector<string> *
|
||||||
if (sym == (ValueSymbol *)0) continue;
|
if (sym == (ValueSymbol *)0) continue;
|
||||||
PatternValue *patval = sym->getPatternValue();
|
PatternValue *patval = sym->getPatternValue();
|
||||||
if (patval->maxValue() + 1 != names->size()) {
|
if (patval->maxValue() + 1 != names->size()) {
|
||||||
reportError(getCurrentLocation(), "Attach name '" + sym->getName() + "' is wrong size for list");
|
ostringstream msg;
|
||||||
|
msg << "Attach name '" + sym->getName();
|
||||||
|
msg << "' (range 0.." << patval->maxValue() << ") is wrong size for list (of " << names->size() << " entries)";
|
||||||
|
reportError(getCurrentLocation(), msg.str());
|
||||||
}
|
}
|
||||||
symtab.replaceSymbol(sym,new NameSymbol(sym->getName(),patval,*names));
|
symtab.replaceSymbol(sym,new NameSymbol(sym->getName(),patval,*names));
|
||||||
}
|
}
|
||||||
|
@ -2720,7 +2735,10 @@ void SleighCompile::attachVarnodes(vector<SleighSymbol *> *symlist,vector<Sleigh
|
||||||
if (sym == (ValueSymbol *)0) continue;
|
if (sym == (ValueSymbol *)0) continue;
|
||||||
PatternValue *patval = sym->getPatternValue();
|
PatternValue *patval = sym->getPatternValue();
|
||||||
if (patval->maxValue() + 1 != varlist->size()) {
|
if (patval->maxValue() + 1 != varlist->size()) {
|
||||||
reportError(getCurrentLocation(), "Attach varnode '" + sym->getName() + "' is wrong size for list");
|
ostringstream msg;
|
||||||
|
msg << "Attach varnode '" + sym->getName();
|
||||||
|
msg << "' (range 0.." << patval->maxValue() << ") is wrong size for list (of " << varlist->size() << " entries)";
|
||||||
|
reportError(getCurrentLocation(), msg.str());
|
||||||
}
|
}
|
||||||
int4 sz = 0;
|
int4 sz = 0;
|
||||||
for(int4 j=0;j<varlist->size();++j) {
|
for(int4 j=0;j<varlist->size();++j) {
|
||||||
|
|
|
@ -488,6 +488,8 @@ public class SleighCompile extends SleighBase {
|
||||||
}
|
}
|
||||||
for (int i = 0; i < tables.size(); ++i) {
|
for (int i = 0; i < tables.size(); ++i) {
|
||||||
if (tables.get(i).isError()) {
|
if (tables.get(i).isError()) {
|
||||||
|
reportError(tables.get(i).getLocation(),
|
||||||
|
"Problem in table: '" + tables.get(i).getName());
|
||||||
errors += 1;
|
errors += 1;
|
||||||
}
|
}
|
||||||
if (tables.get(i).getPattern() == null) {
|
if (tables.get(i).getPattern() == null) {
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
/* ###
|
/* ###
|
||||||
* IP: GHIDRA
|
* IP: GHIDRA
|
||||||
* REVIEWED: YES
|
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,31 +15,29 @@
|
||||||
*/
|
*/
|
||||||
package ghidra.pcodeCPort.utils;
|
package ghidra.pcodeCPort.utils;
|
||||||
|
|
||||||
import ghidra.pcodeCPort.context.*;
|
import ghidra.pcodeCPort.context.SleighError;
|
||||||
import ghidra.sleigh.grammar.Location;
|
import ghidra.sleigh.grammar.Location;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
|
|
||||||
public static final String endl = System.getProperty( "line.separator" );
|
public static final String endl = System.getProperty("line.separator");
|
||||||
|
|
||||||
private static long[] uintbmasks = {
|
private static long[] uintbmasks = { 0, 0xff, 0xffff, 0xffffff, 0xffffffffL, 0xffffffffffL,
|
||||||
0, 0xff, 0xffff, 0xffffff, 0xffffffffL, 0xffffffffffL, 0xffffffffffffL,
|
0xffffffffffffL, 0xffffffffffffffL, 0xffffffffffffffffL };
|
||||||
0xffffffffffffffL, 0xffffffffffffffffL
|
|
||||||
};
|
|
||||||
|
|
||||||
public static long calc_mask( int size ) {
|
public static long calc_mask(int size) {
|
||||||
return uintbmasks[(size < 8) ? size : 8];
|
return uintbmasks[(Integer.compareUnsigned(size, 8) < 0) ? size : 8];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long pcode_right( long val, int sa ) {
|
public static long pcode_right(long val, int sa) {
|
||||||
if ( sa >= 64 ) {
|
if (sa >= 64) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return val >>> sa;
|
return val >>> sa;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long pcode_left( long val, int sa ) {
|
public static long pcode_left(long val, int sa) {
|
||||||
if ( sa >= 64 ) {
|
if (sa >= 64) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return val << sa;
|
return val << sa;
|
||||||
|
@ -61,30 +58,30 @@ public class Utils {
|
||||||
// return s;
|
// return s;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public static boolean signbit_negative( long val, int size ) { // Return true if signbit is set
|
public static boolean signbit_negative(long val, int size) { // Return true if signbit is set
|
||||||
// (negative)
|
// (negative)
|
||||||
long mask = 0x80;
|
long mask = 0x80;
|
||||||
mask <<= 8 * (size - 1);
|
mask <<= 8 * (size - 1);
|
||||||
return ((val & mask) != 0);
|
return ((val & mask) != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long uintb_negate( long in, int size ) { // Invert bits
|
public static long uintb_negate(long in, int size) { // Invert bits
|
||||||
return ((~in) & calc_mask( size ));
|
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;
|
int signbit;
|
||||||
long mask;
|
long mask;
|
||||||
|
|
||||||
signbit = sizein * 8 - 1;
|
signbit = sizein * 8 - 1;
|
||||||
in &= calc_mask( sizein );
|
in &= calc_mask(sizein);
|
||||||
if ( sizein >= sizeout ) {
|
if (sizein >= sizeout) {
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
if ( (in >>> signbit) != 0 ) {
|
if ((in >>> signbit) != 0) {
|
||||||
mask = calc_mask( sizeout );
|
mask = calc_mask(sizeout);
|
||||||
long tmp = mask << signbit; // Split shift into two pieces
|
long tmp = mask << signbit; // Split shift into two pieces
|
||||||
tmp = (tmp << 1) & mask; // In case, everything is shifted out
|
tmp = (tmp << 1) & mask; // In case, everything is shifted out
|
||||||
in |= tmp;
|
in |= tmp;
|
||||||
|
@ -93,22 +90,23 @@ public class Utils {
|
||||||
}
|
}
|
||||||
|
|
||||||
// this used to void and changed the parameter val - can't do it in java
|
// 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-
|
{ // Sign extend -val- above -bit-
|
||||||
long mask = 0;
|
long mask = 0;
|
||||||
mask = (~mask) << bit;
|
mask = (~mask) << bit;
|
||||||
if ( ((val >>> bit) & 1) != 0 ) {
|
if (((val >>> bit) & 1) != 0) {
|
||||||
val |= mask;
|
val |= mask;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
val &= (~mask);
|
val &= (~mask);
|
||||||
}
|
}
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
// this used to void and changed the parameter val - can't do it in java
|
// 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
|
public static long zzz_zero_extend(long val, int bit) { // Clear all bits in -val- above
|
||||||
// -bit-
|
// -bit-
|
||||||
long mask = 0;
|
long mask = 0;
|
||||||
mask = (~mask) << bit;
|
mask = (~mask) << bit;
|
||||||
mask <<= 1;
|
mask <<= 1;
|
||||||
|
@ -117,9 +115,9 @@ public class Utils {
|
||||||
}
|
}
|
||||||
|
|
||||||
// this used to void and changed the parameter val - can't do it in java
|
// 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 val
|
||||||
long res = 0;
|
long res = 0;
|
||||||
while ( size > 0 ) {
|
while (size > 0) {
|
||||||
res <<= 8;
|
res <<= 8;
|
||||||
res |= (val & 0xff);
|
res |= (val & 0xff);
|
||||||
val >>>= 8;
|
val >>>= 8;
|
||||||
|
@ -128,9 +126,9 @@ public class Utils {
|
||||||
return res;
|
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 int
|
||||||
long res = 0;
|
long res = 0;
|
||||||
for ( int i = 0; i < 4; ++i ) {
|
for (int i = 0; i < 4; ++i) {
|
||||||
res <<= 8;
|
res <<= 8;
|
||||||
res |= (val & 0xff);
|
res |= (val & 0xff);
|
||||||
val >>>= 8;
|
val >>>= 8;
|
||||||
|
@ -138,21 +136,21 @@ public class Utils {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isprint(int c) {
|
public static boolean isprint(int c) {
|
||||||
if (c >= 32 && c <= 126)
|
if (c >= 32 && c <= 126)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isascii(int c) {
|
public static boolean isascii(int c) {
|
||||||
if (c >= 0 && c <= 127)
|
if (c >= 0 && c <= 127)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
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
|
// of the least signifigant bit set or -1 if none set
|
||||||
if ( val == 0 ) {
|
if (val == 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
@ -160,18 +158,19 @@ public class Utils {
|
||||||
long mask = -1;
|
long mask = -1;
|
||||||
do {
|
do {
|
||||||
mask >>>= sz;
|
mask >>>= sz;
|
||||||
if ( (mask & val) == 0 ) {
|
if ((mask & val) == 0) {
|
||||||
res += sz;
|
res += sz;
|
||||||
val >>>= sz;
|
val >>>= sz;
|
||||||
}
|
}
|
||||||
sz >>= 1;
|
sz >>= 1;
|
||||||
} while ( sz != 0 );
|
}
|
||||||
|
while (sz != 0);
|
||||||
return res;
|
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
|
// of the most signifigant bit set or -1 if none set
|
||||||
if ( val == 0 ) {
|
if (val == 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,95 +179,100 @@ public class Utils {
|
||||||
long mask = -1;
|
long mask = -1;
|
||||||
do {
|
do {
|
||||||
mask <<= sz;
|
mask <<= sz;
|
||||||
if ( (mask & val) == 0 ) {
|
if ((mask & val) == 0) {
|
||||||
res -= sz;
|
res -= sz;
|
||||||
val <<= sz;
|
val <<= sz;
|
||||||
}
|
}
|
||||||
sz >>= 1;
|
sz >>= 1;
|
||||||
} while ( sz != 0 );
|
}
|
||||||
|
while (sz != 0);
|
||||||
return res;
|
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 val
|
{ // Return smallest number of form 2^n-1, bigger or equal to val
|
||||||
long res = val;
|
long res = val;
|
||||||
int sz = 1;
|
int sz = 1;
|
||||||
while ( sz < 64 ) {
|
while (sz < 64) {
|
||||||
res = res | (res >>> sz);
|
res = res | (res >>> sz);
|
||||||
sz <<= 1;
|
sz <<= 1;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String paddedHexString( long value, int padLength ) {
|
public static String paddedHexString(long value, int padLength) {
|
||||||
String decodedString = Long.toString( value, 16 );
|
String decodedString = Long.toString(value, 16);
|
||||||
if ( decodedString.length() >= padLength ) {
|
if (decodedString.length() >= padLength) {
|
||||||
return decodedString;
|
return decodedString;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuffer buffer = new StringBuffer();
|
StringBuffer buffer = new StringBuffer();
|
||||||
int missingLength = padLength - decodedString.length();
|
int missingLength = padLength - decodedString.length();
|
||||||
for ( int i = 0; i < missingLength; i++ ) {
|
for (int i = 0; i < missingLength; i++) {
|
||||||
buffer.append( "0" );
|
buffer.append("0");
|
||||||
}
|
}
|
||||||
buffer.append( decodedString );
|
buffer.append(decodedString);
|
||||||
return buffer.toString();
|
return buffer.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int unsignedCompare( long v1, long v2 ) {
|
public static int unsignedCompare(long v1, long v2) {
|
||||||
if ( v1 == v2 ) {
|
if (v1 == v2) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if ( v1 >= 0 && v2 >= 0 ) {
|
if (v1 >= 0 && v2 >= 0) {
|
||||||
return v1 < v2 ? -1 : 1;
|
return v1 < v2 ? -1 : 1;
|
||||||
}
|
}
|
||||||
if ( v1 < 0 && v2 < 0 ) {
|
if (v1 < 0 && v2 < 0) {
|
||||||
return v1 < v2 ? -1 : 1;
|
return v1 < v2 ? -1 : 1;
|
||||||
}
|
}
|
||||||
if ( v1 < 0 ) {
|
if (v1 < 0) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
public static int unsignedCompare( int v1, int v2 ) {
|
|
||||||
if ( v1 == v2 ) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if ( v1 >= 0 && v2 >= 0 ) {
|
|
||||||
return v1 < v2 ? -1 : 1;
|
|
||||||
}
|
|
||||||
if ( v1 < 0 && v2 < 0 ) {
|
|
||||||
return v1 < v2 ? -1 : 1;
|
|
||||||
}
|
|
||||||
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);
|
public static int unsignedCompare(int v1, int v2) {
|
||||||
int m = (-1) >>> (sbit + shift.get());
|
if (v1 == v2) {
|
||||||
m <<= shift.get();
|
return 0;
|
||||||
mask.set(m);
|
}
|
||||||
}
|
if (v1 >= 0 && v2 >= 0) {
|
||||||
public static int bytesToInt( byte[] bytes, boolean bigEndian ) {
|
return v1 < v2 ? -1 : 1;
|
||||||
|
}
|
||||||
|
if (v1 < 0 && v2 < 0) {
|
||||||
|
return v1 < v2 ? -1 : 1;
|
||||||
|
}
|
||||||
|
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) {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
if (bigEndian) {
|
if (bigEndian) {
|
||||||
for(int i=0;i<4;i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
result <<= 8;
|
result <<= 8;
|
||||||
result |= (bytes[i] & 0xFF);
|
result |= (bytes[i] & 0xFF);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for(int i=3;i>=0;i--) {
|
for (int i = 3; i >= 0; i--) {
|
||||||
result <<= 8;
|
result <<= 8;
|
||||||
result |= (bytes[i] & 0xFF);
|
result |= (bytes[i] & 0xFF);
|
||||||
}
|
}
|
||||||
|
@ -284,6 +288,7 @@ public class Utils {
|
||||||
}
|
}
|
||||||
return a << b;
|
return a << b;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long ashiftRight(long a, long b) {
|
public static long ashiftRight(long a, long b) {
|
||||||
b &= 0xff;
|
b &= 0xff;
|
||||||
if (b >= 64) {
|
if (b >= 64) {
|
||||||
|
@ -291,6 +296,7 @@ public class Utils {
|
||||||
}
|
}
|
||||||
return a >> b;
|
return a >> b;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long lshiftRight(long a, long b) {
|
public static long lshiftRight(long a, long b) {
|
||||||
b &= 0xff;
|
b &= 0xff;
|
||||||
if (b >= 64) {
|
if (b >= 64) {
|
||||||
|
@ -298,36 +304,41 @@ public class Utils {
|
||||||
}
|
}
|
||||||
return a >>> b;
|
return a >>> b;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long unsignedInt(int a) {
|
public static long unsignedInt(int a) {
|
||||||
long result = a;
|
long result = a;
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
result += 4294967296L;
|
result += 4294967296L;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
public static int unsignedDivide(int a, int b) {
|
|
||||||
long la = unsignedInt(a);
|
public static int unsignedDivide(int a, int b) {
|
||||||
long lb = unsignedInt(b);
|
long la = unsignedInt(a);
|
||||||
long result = la/lb;
|
long lb = unsignedInt(b);
|
||||||
return (int) result;
|
long result = la / lb;
|
||||||
}
|
return (int) result;
|
||||||
public static int unsignedModulo(int a, int b) {
|
}
|
||||||
long la = unsignedInt(a);
|
|
||||||
long lb = unsignedInt(b);
|
public static int unsignedModulo(int a, int b) {
|
||||||
long result = la%lb;
|
long la = unsignedInt(a);
|
||||||
return (int) result;
|
long lb = unsignedInt(b);
|
||||||
}
|
long result = la % lb;
|
||||||
|
return (int) result;
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println(unsignedDivide(-8, 32));
|
System.out.println(unsignedDivide(-8, 32));
|
||||||
System.out.println(unsignedModulo(-8, 32));
|
System.out.println(unsignedModulo(-8, 32));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String toUnsignedIntHex(int n) {
|
public static String toUnsignedIntHex(int n) {
|
||||||
return Long.toHexString(unsignedInt(n));
|
return Long.toHexString(unsignedInt(n));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long bytesToLong(byte[] byteBuf) {
|
public static long bytesToLong(byte[] byteBuf) {
|
||||||
long value = 0;
|
long value = 0;
|
||||||
for(int i=0;i<8;i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
value = value << 8 | byteBuf[i];
|
value = value << 8 | byteBuf[i];
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue