From 3e184b0b47d203cd37089296e0c49c9219c83b26 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Mon, 27 Feb 2023 20:40:26 +0100 Subject: Move instruction implementation in sub module and introduce insn prelude --- src/insn.rs | 4 ++ src/insn/mov.rs | 105 +++++++++++++++++++++++++++++++++++++++++++++++++ src/insn/prelude.rs | 6 +++ src/lib.rs | 110 +--------------------------------------------------- 4 files changed, 117 insertions(+), 108 deletions(-) create mode 100644 src/insn/mov.rs create mode 100644 src/insn/prelude.rs (limited to 'src') diff --git a/src/insn.rs b/src/insn.rs index bb1a380..7c74dd9 100644 --- a/src/insn.rs +++ b/src/insn.rs @@ -1,3 +1,7 @@ +mod prelude; + +mod mov; + pub trait Mov { fn mov(&mut self, op1: T, op2: U); } diff --git a/src/insn/mov.rs b/src/insn/mov.rs new file mode 100644 index 0000000..d930ade --- /dev/null +++ b/src/insn/mov.rs @@ -0,0 +1,105 @@ +use super::prelude::*; + +// -- MOV : reg reg + +impl Mov for Asm { + fn mov(&mut self, op1: Reg64, op2: Reg64) { + self.encode_rr(0x89, op1, op2); + } +} + +impl Mov for Asm { + fn mov(&mut self, op1: Reg32, op2: Reg32) { + self.encode_rr(0x89, op1, op2); + } +} + +impl Mov for Asm { + fn mov(&mut self, op1: Reg16, op2: Reg16) { + self.encode_rr(0x89, op1, op2); + } +} + +impl Mov for Asm { + fn mov(&mut self, op1: Reg8, op2: Reg8) { + self.encode_rr(0x88, op1, op2); + } +} + +// -- MOV : mem reg + +impl Mov for Asm { + fn mov(&mut self, op1: MemOp, op2: Reg64) { + self.encode_mr(0x89, op1, op2); + } +} + +impl Mov for Asm { + fn mov(&mut self, op1: MemOp, op2: Reg32) { + self.encode_mr(0x89, op1, op2); + } +} + +impl Mov for Asm { + fn mov(&mut self, op1: MemOp, op2: Reg16) { + self.encode_mr(0x89, op1, op2); + } +} + +impl Mov for Asm { + fn mov(&mut self, op1: MemOp, op2: Reg8) { + self.encode_mr(0x88, op1, op2); + } +} + +// -- MOV : reg mem + +impl Mov for Asm { + fn mov(&mut self, op1: Reg64, op2: MemOp) { + self.encode_rm(0x8b, op1, op2); + } +} + +impl Mov for Asm { + fn mov(&mut self, op1: Reg32, op2: MemOp) { + self.encode_rm(0x8b, op1, op2); + } +} + +impl Mov for Asm { + fn mov(&mut self, op1: Reg16, op2: MemOp) { + self.encode_rm(0x8b, op1, op2); + } +} + +impl Mov for Asm { + fn mov(&mut self, op1: Reg8, op2: MemOp) { + self.encode_rm(0x8a, op1, op2); + } +} + +// -- MOV : reg imm + +impl Mov for Asm { + fn mov(&mut self, op1: Reg64, op2: Imm64) { + self.encode_oi(0xb8, op1, op2); + } +} + +impl Mov for Asm { + fn mov(&mut self, op1: Reg32, op2: Imm32) { + self.encode_oi(0xb8, op1, op2); + } +} + +impl Mov for Asm { + fn mov(&mut self, op1: Reg16, op2: Imm16) { + self.encode_oi(0xb8, op1, op2); + } +} + +impl Mov for Asm { + fn mov(&mut self, op1: Reg8, op2: Imm8) { + self.encode_oi(0xb0, op1, op2); + } +} diff --git a/src/insn/prelude.rs b/src/insn/prelude.rs new file mode 100644 index 0000000..703417a --- /dev/null +++ b/src/insn/prelude.rs @@ -0,0 +1,6 @@ +pub use crate::Asm; +pub use crate::MemOp; +pub use crate::{Imm16, Imm32, Imm64, Imm8}; +pub use crate::{Reg16, Reg32, Reg64, Reg8}; + +pub use crate::insn::Mov; diff --git a/src/lib.rs b/src/lib.rs index 23c9ca0..9eb2583 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -106,7 +106,7 @@ impl Asm { self.emit_optional(&[prefix, rex]); self.emit(&[opc]); - self.emit(&op2.bytes()); + self.emit(op2.bytes()); } fn encode_ri(&mut self, opc: u8, opc_ext: u8, op1: T, op2: U) @@ -127,7 +127,7 @@ impl Asm { self.emit_optional(&[prefix, rex]); self.emit(&[opc, modrm]); - self.emit(&op2.bytes()); + self.emit(op2.bytes()); } fn encode_mr(&mut self, opc: u8, op1: MemOp, op2: T) @@ -244,109 +244,3 @@ impl EncodeMR for Asm { } impl EncodeMR for Asm {} impl EncodeMR for Asm {} - -// -- Instruction implementations. - -// -- MOV : reg reg - -impl Mov for Asm { - fn mov(&mut self, op1: Reg64, op2: Reg64) { - self.encode_rr(0x89, op1, op2); - } -} - -impl Mov for Asm { - fn mov(&mut self, op1: Reg32, op2: Reg32) { - self.encode_rr(0x89, op1, op2); - } -} - -impl Mov for Asm { - fn mov(&mut self, op1: Reg16, op2: Reg16) { - self.encode_rr(0x89, op1, op2); - } -} - -impl Mov for Asm { - fn mov(&mut self, op1: Reg8, op2: Reg8) { - self.encode_rr(0x88, op1, op2); - } -} - -// -- MOV : mem reg - -impl Mov for Asm { - fn mov(&mut self, op1: MemOp, op2: Reg64) { - self.encode_mr(0x89, op1, op2); - } -} - -impl Mov for Asm { - fn mov(&mut self, op1: MemOp, op2: Reg32) { - self.encode_mr(0x89, op1, op2); - } -} - -impl Mov for Asm { - fn mov(&mut self, op1: MemOp, op2: Reg16) { - self.encode_mr(0x89, op1, op2); - } -} - -impl Mov for Asm { - fn mov(&mut self, op1: MemOp, op2: Reg8) { - self.encode_mr(0x88, op1, op2); - } -} - -// -- MOV : reg mem - -impl Mov for Asm { - fn mov(&mut self, op1: Reg64, op2: MemOp) { - self.encode_rm(0x8b, op1, op2); - } -} - -impl Mov for Asm { - fn mov(&mut self, op1: Reg32, op2: MemOp) { - self.encode_rm(0x8b, op1, op2); - } -} - -impl Mov for Asm { - fn mov(&mut self, op1: Reg16, op2: MemOp) { - self.encode_rm(0x8b, op1, op2); - } -} - -impl Mov for Asm { - fn mov(&mut self, op1: Reg8, op2: MemOp) { - self.encode_rm(0x8a, op1, op2); - } -} - -// -- MOV : reg imm - -impl Mov for Asm { - fn mov(&mut self, op1: Reg64, op2: Imm64) { - self.encode_oi(0xb8, op1, op2); - } -} - -impl Mov for Asm { - fn mov(&mut self, op1: Reg32, op2: Imm32) { - self.encode_oi(0xb8, op1, op2); - } -} - -impl Mov for Asm { - fn mov(&mut self, op1: Reg16, op2: Imm16) { - self.encode_oi(0xb8, op1, op2); - } -} - -impl Mov for Asm { - fn mov(&mut self, op1: Reg8, op2: Imm8) { - self.encode_oi(0xb0, op1, op2); - } -} -- cgit v1.2.3