Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
refactor: use DisassembledProgram instead of Executable
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Jun 11, 2024
1 parent aa22985 commit 9e2357c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
20 changes: 11 additions & 9 deletions src/disassembler/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use super::error::DisassemblerError;
use super::parser;
use crate::x86::Executable;
use super::{error::DisassemblerError, DisassembledProgram};
use crate::{
minix::Program,
x86::{Instruction, IR},
};

pub trait Disassembler {
fn disassemble(&self) -> Result<Executable, DisassemblerError>;
pub trait Disassemblable {
fn disassemble(&self) -> Result<DisassembledProgram, DisassemblerError>;
}

impl Disassembler for Program {
fn disassemble(&self) -> Result<Executable, DisassemblerError> {
impl Disassemblable for Program {
fn disassemble(&self) -> Result<DisassembledProgram, DisassemblerError> {
let mut instructions = Vec::new();
let mut text = self.text_segment.as_slice();

Expand All @@ -30,7 +29,10 @@ impl Disassembler for Program {
text = &text[bytes_consumed..];
}

Ok(Executable::new(instructions))
Ok(DisassembledProgram::new(
instructions,
self.data_segment.data.clone(),
))
}
}

Expand All @@ -42,8 +44,8 @@ pub fn decode(args: Vec<String>) -> Result<String, DisassemblerError> {
let file = std::fs::File::open(&args[1]).map_err(|_| DisassemblerError::InvalidArgs)?;
let program = Program::from_file(file).map_err(|_| DisassemblerError::InvalidArgs)?;

let executable = program.disassemble()?;
Ok(executable.to_string())
let disassembled = program.disassemble()?;
Ok(disassembled.to_string())
}

#[cfg(test)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use super::{Instruction, IR};
use crate::x86::{Instruction, IR};

pub struct Executable {
pub struct DisassembledProgram {
pub instructions: Vec<Instruction>,
}

impl Executable {
pub fn new(instructions: Vec<Instruction>) -> Self {
Executable { instructions }
impl DisassembledProgram {
pub fn new(instructions: Vec<Instruction>, data: Vec<u8>) -> Self {
DisassembledProgram { instructions }
}
}

impl From<Vec<IR>> for Executable {
impl From<Vec<IR>> for DisassembledProgram {
fn from(ir: Vec<IR>) -> Self {
let instructions = ir.into_iter().map(|ir| ir.into()).collect();
Executable::new(instructions)
DisassembledProgram::new(instructions, vec![])
}
}

impl std::fmt::Display for Executable {
impl std::fmt::Display for DisassembledProgram {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut bytes_count = 0;
for instruction in &self.instructions {
Expand Down
5 changes: 4 additions & 1 deletion src/disassembler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
mod decoder;
mod disassembled_program;
mod error;
mod parser;

pub use self::decoder::{decode, Disassembler};
pub use self::decoder::{decode, Disassemblable};
pub use self::disassembled_program::DisassembledProgram;
pub use self::parser::parse_instruction;
2 changes: 0 additions & 2 deletions src/x86/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
mod address;
mod displacement;
mod executable;
mod instruction;
mod operand;
mod register;

pub use self::address::Address;
pub use self::displacement::Displacement;
pub use self::executable::Executable;
pub use self::instruction::{Instruction, IR};
pub use self::operand::Operand;
pub use self::register::Register;

0 comments on commit 9e2357c

Please sign in to comment.