From 80458e3288852dbbf0a34de7dcaf57c2e1898f7a Mon Sep 17 00:00:00 2001 From: Alexis Engelke Date: Sun, 13 Jan 2019 14:26:26 +0100 Subject: [PATCH] Reduce space required by instruction width --- decode.c | 16 +++++++++------- decode.h | 4 ++-- format.c | 4 ++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/decode.c b/decode.c index dd9bae8..efa52e7 100644 --- a/decode.c +++ b/decode.c @@ -567,35 +567,37 @@ decode(const uint8_t* buffer, int len, DecodeMode mode, Instr* instr) instr->segment = RI_DS; } - uint8_t op_size = 0; + uint8_t op_size_log = 0; if (desc->gp_size_8) { - op_size = 1; + op_size_log = 1; } #if defined(ARCH_X86_64) else if (prefixes & PREFIX_REXW) { - op_size = 8; + op_size_log = 4; } #endif else if (prefixes & PREFIX_OPSZ) { - op_size = 2; + op_size_log = 2; } #if defined(ARCH_X86_64) else if (mode == DECODE_64 && desc->gp_size_def64) { - op_size = 8; + op_size_log = 4; } #endif else { - op_size = 4; + op_size_log = 3; } + uint8_t op_size = (1 << op_size_log) >> 1; + if (UNLIKELY(desc->gp_instr_width)) { - instr->width = op_size; + instr->width = op_size_log; } else { diff --git a/decode.h b/decode.h index d800904..43ac1bf 100644 --- a/decode.h +++ b/decode.h @@ -117,7 +117,7 @@ struct Instr uint16_t type; struct Operand operands[4]; uint8_t segment : 3; - uint8_t width : 5; + uint8_t width : 3; /** * Encoded as 1 << (scale - 1) **or** no scaled register at all if zero. @@ -136,7 +136,7 @@ struct Instr typedef struct Instr Instr; #define INSTR_SEGMENT(instr) ((instr)->segment) -#define INSTR_WIDTH(instr) ((instr)->width) +#define INSTR_WIDTH(instr) ((1 << (instr)->width) >> 1) #define INSTR_HAS_REP(instr) ((instr)->prefixes & PREFIX_REP) #define INSTR_HAS_REPNZ(instr) ((instr)->prefixes & PREFIX_REPNZ) #define INSTR_HAS_LOCK(instr) ((instr)->prefixes & PREFIX_LOCK) diff --git a/format.c b/format.c index 79325ad..9838dad 100644 --- a/format.c +++ b/format.c @@ -83,10 +83,10 @@ instr_format(const Instr* instr, char buffer[128]) *(cur++) = *(mnemonic++); } - if (instr->width != 0) + if (INSTR_WIDTH(instr)) { *(cur++) = '_'; - instr_format_decimal(&cur, instr->width); + instr_format_decimal(&cur, INSTR_WIDTH(instr)); } for (int i = 0; i < 4; i++)