Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Optimizes emit_ins(X86Instruction::load_immediate()).
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Dec 5, 2024
1 parent 3fae1aa commit 46624f4
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,30 @@ impl X86Instruction {
/// Load destination from immediate
#[inline]
pub const fn load_immediate(destination: u8, immediate: i64) -> Self {
let mut size = OperandSize::S64;
if immediate >= 0 {
if immediate <= u32::MAX as i64 {
// Zero extend u32 imm to u64 reg
size = OperandSize::S32;
}
} else if immediate >= i32::MIN as i64 {
// Sign extend i32 imm to i64 reg
return Self {
size: OperandSize::S64,
opcode: 0xc7,
second_operand: destination,
immediate_size: OperandSize::S32,
immediate,
..Self::DEFAULT
};
}
// Load full u64 imm into u64 reg
Self {
size: OperandSize::S64,
size,
opcode: 0xb8 | (destination & 0b111),
modrm: false,
second_operand: destination,
immediate_size: OperandSize::S64,
immediate_size: size,
immediate,
..Self::DEFAULT
}
Expand Down

0 comments on commit 46624f4

Please sign in to comment.