Skip to content

Commit

Permalink
Merge branch 'main' into refactor/exceptions-to-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
stuxnot committed Nov 6, 2023
2 parents 3c00c9b + 5f690dd commit c21193d
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 506 deletions.
285 changes: 0 additions & 285 deletions bindings/rust/Cargo.lock

This file was deleted.

1 change: 0 additions & 1 deletion bindings/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ categories = ["api-bindings", "compilers"]

[dependencies]
anyhow = { version = "1.0.68", default-features = true }
derive_builder = { version = "0.12.0" }
cxx = "1.0.94"

[build-dependencies]
Expand Down
15 changes: 8 additions & 7 deletions bindings/rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ extern crate anyhow;
extern crate nyxstone;

use anyhow::Result;
use nyxstone::{IntegerBase, LabelDefinition, NyxstoneBuilder};
use nyxstone::{IntegerBase, LabelDefinition, Nyxstone, NyxstoneConfig};

fn main() -> Result<()> {
// Creating a nyxstone instance can fail, for example if the triple is invalid.
let nyxstone = NyxstoneBuilder::default().with_triple("x86_64").build()?;
let nyxstone = Nyxstone::new("x86_64", NyxstoneConfig::default())?;

let instructions = nyxstone.assemble_to_instructions(
"mov rax, rbx; cmp rax, rdx; jne .label",
Expand All @@ -55,10 +55,11 @@ fn main() -> Result<()> {

assert_eq!(disassembly, "xor eax, ebx\n".to_owned());

let nyxstone = NyxstoneBuilder::default()
.with_triple("x86_64")
.with_immediate_style(IntegerBase::HexPrefix)
.build()?;
let config = NyxstoneConfig {
immediate_style: IntegerBase::HexPrefix,
..Default::default()
};
let nyxstone = Nyxstone::new("x86_64", config)?;

assert_eq!(
nyxstone.disassemble_to_text(&[0x83, 0xc0, 0x01], 0, 0)?,
Expand All @@ -79,4 +80,4 @@ The binding class `NyxstoneFFI` currently copies a lot of data between the rust

## Acknowledgements

The build script of the rust bindings borrow heavily from the [llvm-sys](https://gitlab.com/taricorp/llvm-sys.rs) build script.
The build script of the rust bindings borrow heavily from the [llvm-sys](https://gitlab.com/taricorp/llvm-sys.rs) build script.
45 changes: 45 additions & 0 deletions bindings/rust/examples/usage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
extern crate anyhow;
extern crate nyxstone;

use anyhow::Result;
use nyxstone::{IntegerBase, LabelDefinition, Nyxstone, NyxstoneConfig};

fn main() -> Result<()> {
// Creating a nyxstone instance can fail, for example if the triple is invalid.
let nyxstone = Nyxstone::new("x86_64", NyxstoneConfig::default())?;

let instructions = nyxstone.assemble_to_instructions(
"mov rax, rbx; cmp rax, rdx; jne .label",
0x100,
&[LabelDefinition {
name: ".label",
address: 0x1200,
}],
)?;

println!("Assembled: ");
for instr in instructions {
println!("0x{:04x}: {:15} - {:02x?}", instr.address, instr.assembly, instr.bytes);
}

let disassembly = nyxstone.disassemble_to_text(
&[0x31, 0xd8],
/* address= */ 0x0,
/* #instructions= (0 = all)*/ 0,
)?;

assert_eq!(disassembly, "xor eax, ebx\n".to_owned());

let config = NyxstoneConfig {
immediate_style: IntegerBase::HexPrefix,
..Default::default()
};
let nyxstone = Nyxstone::new("x86_64", config)?;

assert_eq!(
nyxstone.disassemble_to_text(&[0x83, 0xc0, 0x01], 0, 0)?,
"add eax, 0x1\n".to_owned()
);

Ok(())
}
Loading

0 comments on commit c21193d

Please sign in to comment.