From 4b320e08172137bf8869d2a676a3c5617eb18ee0 Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Tue, 6 Aug 2024 19:13:06 +0300 Subject: [PATCH 1/5] Fix comment --- bddisasm/bdx86_decoder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bddisasm/bdx86_decoder.c b/bddisasm/bdx86_decoder.c index 3e04516..6fd057e 100644 --- a/bddisasm/bdx86_decoder.c +++ b/bddisasm/bdx86_decoder.c @@ -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; From b261dd447de7a925311fd4169730153214695a3f Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Tue, 6 Aug 2024 19:13:06 +0300 Subject: [PATCH 2/5] Only set bcstSize when it's required --- bddisasm/bdx86_decoder.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bddisasm/bdx86_decoder.c b/bddisasm/bdx86_decoder.c index 6fd057e..31f3ac8 100644 --- a/bddisasm/bdx86_decoder.c +++ b/bddisasm/bdx86_decoder.c @@ -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. @@ -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) From f967327ca48593e000c7d19eb95d8ac5a42b9afe Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Tue, 6 Aug 2024 19:13:06 +0300 Subject: [PATCH 3/5] Add benchmark script --- benchmark.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 benchmark.sh 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 From 8bc44ae14586c2dc1936b76c149b3281583d29ce Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Mon, 26 Aug 2024 17:28:32 +0300 Subject: [PATCH 4/5] Only call ND_SIGN_EX when instruction has displacement --- bddisasm/bdx86_decoder.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bddisasm/bdx86_decoder.c b/bddisasm/bdx86_decoder.c index 31f3ac8..d024910 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; } From 56bbcca4cfe00d968222492efda9bf8b797a2442 Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Tue, 6 Aug 2024 19:13:06 +0300 Subject: [PATCH 5/5] Replace ND_SIGN_EX with less branchy version --- inc/bdx86_core.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/inc/bdx86_core.h b/inc/bdx86_core.h index 75271e4..117c4ed 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.