Skip to main content

x86_64_assembler/
lib.rs

1#![deny(missing_debug_implementations, missing_copy_implementations)]
2#![warn(missing_docs, rustdoc::missing_crate_level_docs)]
3#![doc = include_str!("readme.md")]
4#![doc(html_logo_url = "https://raw.githubusercontent.com/oovm/shape-rs/dev/projects/images/Trapezohedron.svg")]
5#![doc(html_favicon_url = "https://raw.githubusercontent.com/oovm/shape-rs/dev/projects/images/Trapezohedron.svg")]
6
7use gaia_types::{helpers::Architecture, GaiaError, Result};
8
9pub mod builder;
10pub mod decoder;
11pub mod encoder;
12pub mod instruction;
13
14use instruction::Instruction;
15
16/// x86_64汇编器主结构体
17#[derive(Debug, Clone)]
18pub struct X86_64Assembler {
19    architecture: Architecture,
20}
21
22impl X86_64Assembler {
23    /// 创建新的x86_64汇编器
24    pub fn new(architecture: Architecture) -> Result<Self> {
25        match architecture {
26            Architecture::X86 | Architecture::X86_64 => Ok(Self { architecture }),
27            _ => Err(GaiaError::unsupported_architecture(architecture)),
28        }
29    }
30
31    /// 编码指令为字节码
32    pub fn encode(&self, instruction: &Instruction) -> Result<Vec<u8>> {
33        let encoder = encoder::InstructionEncoder::new(self.architecture.clone());
34        encoder.encode(instruction)
35    }
36
37    /// 解码字节码为指令序列
38    pub fn decode(&self, bytes: &[u8]) -> Result<Vec<Instruction>> {
39        let decoder = decoder::InstructionDecoder::new(self.architecture.clone());
40        decoder.decode(bytes)
41    }
42
43    /// 获取当前汇编器的架构
44    pub fn architecture(&self) -> Architecture {
45        self.architecture.clone()
46    }
47
48    /// 设置汇编器的架构
49    pub fn set_architecture(&mut self, architecture: Architecture) -> Result<()> {
50        match architecture {
51            Architecture::X86 | Architecture::X86_64 => {
52                self.architecture = architecture;
53                Ok(())
54            }
55            _ => Err(GaiaError::unsupported_architecture(architecture)),
56        }
57    }
58}