From 1d4b7c2dad97694a6980d95287519ba4c3d582ff Mon Sep 17 00:00:00 2001 From: iximeow Date: Fri, 2 Jul 2021 00:39:35 -0700 Subject: consolidate length/extension checks to help compilers DCE --- src/long_mode/mod.rs | 57 ++++++++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 33 deletions(-) (limited to 'src/long_mode/mod.rs') diff --git a/src/long_mode/mod.rs b/src/long_mode/mod.rs index 861160a..65bda54 100644 --- a/src/long_mode/mod.rs +++ b/src/long_mode/mod.rs @@ -4094,11 +4094,32 @@ impl Default for InstDecoder { impl Decoder for InstDecoder { fn decode::Address, ::Word>>(&self, words: &mut T) -> Result::DecodeError> { let mut instr = Instruction::invalid(); - read_instr(self, words, &mut instr) - .map(|_: ()| instr) + read_instr(self, words, &mut instr)?; + + instr.length = words.offset() as u8; + if words.offset() > 15 { + return Err(DecodeError::TooLong); + } + + if self != &InstDecoder::default() { + self.revise_instruction(&mut instr)?; + } + + Ok(instr) } fn decode_into::Address, ::Word>>(&self, instr: &mut Instruction, words: &mut T) -> Result<(), ::DecodeError> { - read_instr(self, words, instr) + read_instr(self, words, instr)?; + + instr.length = words.offset() as u8; + if words.offset() > 15 { + return Err(DecodeError::TooLong); + } + + if self != &InstDecoder::default() { + self.revise_instruction(instr)?; + } + + Ok(()) } } @@ -7019,13 +7040,6 @@ fn read_instr::Address, 15 { - return Err(DecodeError::TooLong); - } - if decoder != &InstDecoder::default() { - decoder.revise_instruction(instruction)?; - } return Ok(()); } } else if b == 0xc4 { @@ -7035,13 +7049,6 @@ fn read_instr::Address, 15 { - return Err(DecodeError::TooLong); - } - if decoder != &InstDecoder::default() { - decoder.revise_instruction(instruction)?; - } return Ok(()); } } else if b == 0x62 { @@ -7052,13 +7059,6 @@ fn read_instr::Address, 15 { - return Err(DecodeError::TooLong); - } - if decoder != &InstDecoder::default() { - decoder.revise_instruction(instruction)?; - } return Ok(()); } } @@ -7106,10 +7106,6 @@ fn read_instr::Address, 15 { - return Err(DecodeError::TooLong); - } if instruction.prefixes.lock() { if !LOCKABLE_INSTRUCTIONS.contains(&instruction.opcode) || !instruction.operands[0].is_memory() { @@ -7117,11 +7113,6 @@ fn read_instr::Address,