diff --git a/bddisasm/bdx86_decoder.c b/bddisasm/bdx86_decoder.c index 3fd7e17..6e43f24 100644 --- a/bddisasm/bdx86_decoder.c +++ b/bddisasm/bdx86_decoder.c @@ -1298,7 +1298,7 @@ NdParseMemoryOperand16( // Store the displacement. Operand->Info.Memory.HasDisp = !!Instrux->HasDisp; Operand->Info.Memory.DispSize = Instrux->DispLength; - Operand->Info.Memory.Disp = ND_SIGN_EX(Instrux->DispLength, Instrux->Displacement); + Operand->Info.Memory.Disp = Instrux->HasDisp ? ND_SIGN_EX(Instrux->DispLength, Instrux->Displacement) : 0; return ND_STATUS_SUCCESS; } @@ -1416,7 +1416,7 @@ NdParseMemoryOperand3264( Operand->Info.Memory.HasDisp = Instrux->HasDisp; Operand->Info.Memory.DispSize = Instrux->DispLength; - Operand->Info.Memory.Disp = ND_SIGN_EX(Instrux->DispLength, Instrux->Displacement); + Operand->Info.Memory.Disp = Instrux->HasDisp ? ND_SIGN_EX(Instrux->DispLength, Instrux->Displacement) : 0; return ND_STATUS_SUCCESS; } @@ -1441,14 +1441,14 @@ NdParseOperand( ND_UINT8 opt, ops, opf, opa, opd, opb; ND_REG_SIZE vsibRegSize; ND_UINT8 vsibIndexSize, vsibIndexCount; - ND_OPERAND_SIZE size, bcstSize; + ND_OPERAND_SIZE size; ND_BOOL width; // pre-init status = ND_STATUS_SUCCESS; vsibRegSize = 0; vsibIndexSize = vsibIndexCount = 0; - size = bcstSize = 0; + size = 0; // Get actual width. width = Instrux->Exs.w && !(Instrux->Attributes & ND_FLAG_WIG); @@ -1877,7 +1877,7 @@ NdParseOperand( } // Store operand info. - operand->Size = bcstSize = size; + operand->Size = size; // // Fill in the operand type. @@ -2242,7 +2242,7 @@ NdParseOperand( break; case ND_OPT_LSTAR: - // The operand is implicit and is the IA32_STAR. + // The operand is implicit and is the IA32_LSTAR. operand->Type = ND_OP_REG; operand->Info.Register.Type = ND_REG_MSR; operand->Info.Register.Size = ND_SIZE_64BIT; @@ -2778,6 +2778,7 @@ memory: // bcstSize / rawSize. if (Instrux->HasBroadcast) { + ND_OPERAND_SIZE bcstSize = size; operand->Info.Memory.HasBroadcast = ND_TRUE; if (opd & ND_OPD_B32) diff --git a/benchmark.sh b/benchmark.sh new file mode 100755 index 0000000..0a273fa --- /dev/null +++ b/benchmark.sh @@ -0,0 +1,60 @@ +#!/bin/sh +set -e + +if [ "$#" -ne 4 ] ; then + echo "Compare the speed of two different disasmtool versions" + echo "Usage $0 " + exit 0 +fi + +FIRST=$1 +SECOND=$2 +INPUT=$3 +COUNT=$4 + +if [ ! -x "$FIRST" ] ; then + echo "First program $FIRST does not exist or is not executable" + exit 1 +fi + +if [ ! -x "$SECOND" ] ; then + echo "Second program $SECOND does not exist or is not executable" + exit 1 +fi + +if [ ! -f "$INPUT" ] ; then + echo "Input file $INPUT does not exist" + exit 1 +fi + +case $COUNT in + ''|*[!0-9]*) echo "Iteration count $COUNT is not a number" ; exit 1 ;; + *) ;; +esac + +if [ "$COUNT" -lt 3 ] ; then + echo "ministat requires at least 3 samples" + exit 1 +fi + +FIRSTRESULT="$FIRST.result" +SECONDRESULT="$SECOND.result" + +truncate -s 0 $FIRSTRESULT +truncate -s 0 $SECONDRESULT + +# Make sure all necessary files are in cache +$FIRST -f $INPUT -nv > /dev/null +$SECOND -f $INPUT -nv > /dev/null + +for n in `seq 1 $COUNT` ; do + echo "$n" + $FIRST -f $INPUT -nv -iv | tee -a $FIRSTRESULT + $SECOND -f $INPUT -nv -iv | tee -a $SECONDRESULT +done + +ministat -C 6 $FIRSTRESULT $SECONDRESULT +echo 'Instructions/second, higher is better' +if [ "$COUNT" -lt 30 ] ; then + echo "Sample count $COUNT was less than 30, results might be unreliable" +fi diff --git a/inc/bdx86_core.h b/inc/bdx86_core.h index 5c6173d..53f2036 100644 --- a/inc/bdx86_core.h +++ b/inc/bdx86_core.h @@ -353,15 +353,14 @@ typedef ND_UINT32 ND_REG_SIZE; #define ND_SIGN_EX_16(x) (((x) & 0x00008000) ? (0xFFFFFFFFFFFF0000 | (x)) : ((x) & 0xFFFF)) // Sign extend 32 bit to 64 bit. #define ND_SIGN_EX_32(x) (((x) & 0x80000000) ? (0xFFFFFFFF00000000 | (x)) : ((x) & 0xFFFFFFFF)) -// Wrapper for for ND_SIGN_EX_8/ND_SIGN_EX_16/ND_SIGN_EX_32. Sign extend sz bytes to 64 bits. -#define ND_SIGN_EX(sz, x) ((sz) == 1 ? ND_SIGN_EX_8(x) : (sz) == 2 ? ND_SIGN_EX_16(x) : \ - (sz) == 4 ? ND_SIGN_EX_32(x) : (x)) +// Sign extend to 64 bit, with minimal branches +#define ND_SIGN_EX(sz, x) (((x) & ND_SIZE_TO_MASK(sz)) | (~ND_SIZE_TO_MASK(sz) * ND_GET_SIGN(sz, x))) + // Trim 64 bits to sz bytes. #define ND_TRIM(sz, x) ((sz) == 1 ? (x) & 0xFF : (sz) == 2 ? (x) & 0xFFFF : \ (sz) == 4 ? (x) & 0xFFFFFFFF : (x)) // Returns most significant bit, given size in bytes sz. -#define ND_MSB(sz, x) ((sz) == 1 ? ((x) >> 7) & 1 : (sz) == 2 ? ((x) >> 15) & 1 : \ - (sz) == 4 ? ((x) >> 31) & 1 : ((x) >> 63) & 1) +#define ND_MSB(sz, x) (((x) >> ( (sz) * 8 - 1)) & 1) // Returns least significant bit. #define ND_LSB(sz, x) ((x) & 1) // Convert a size in bytes to a bitmask.