From b5935c796ab0b2c478b4480a0dfdacb8e0dfe40e Mon Sep 17 00:00:00 2001 From: johannst Date: Thu, 19 Dec 2024 18:47:57 +0000 Subject: deploy: 8ca27aeeb99a0da43e92e39918aa07c0b1443c07 --- src/add/add.rs.html | 8 ++-- src/fib/fib.rs.html | 8 ++-- src/juicebox_asm/rt.rs.html | 90 +++++++++++++++++++++++++++++++++++++++------ 3 files changed, 84 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/add/add.rs.html b/src/add/add.rs.html index 580daa0..ba76c7d 100644 --- a/src/add/add.rs.html +++ b/src/add/add.rs.html @@ -36,8 +36,7 @@ 35 36 37 -38 -39
//! Add example.
+38
//! Add example.
 //!
 //! Jit compile a function at runtime (generate native host code) which calls a function defined in
 //! the example based on the SystemV abi to demonstrate the [`juicebox_asm`] crate.
@@ -69,9 +68,8 @@
     let mut rt = Runtime::new();
     let add42 = unsafe { rt.add_code::<extern "C" fn(u32) -> u32>(asm.into_code()) };
 
-    // Write out JIT code for visualization.
-    // Disassemble for example with `ndisasm -b 64 jit.asm`.
-    rt.dump();
+    // Disassemble JIT code and write to stdout.
+    rt.disasm();
 
     let res = add42(5);
     assert_eq!(res, 47);
diff --git a/src/fib/fib.rs.html b/src/fib/fib.rs.html
index c1a746f..99da282 100644
--- a/src/fib/fib.rs.html
+++ b/src/fib/fib.rs.html
@@ -75,8 +75,7 @@
 74
 75
 76
-77
-78
//! Fibonacci example.
+77
//! Fibonacci example.
 //!
 //! Jit compile a function at runtime (generate native host code) to compute the fibonacci sequence
 //! to demonstrate the [`juicebox_asm`] crate.
@@ -144,9 +143,8 @@
     let mut rt = Runtime::new();
     let fib = unsafe { rt.add_code::<extern "C" fn(u64) -> u64>(asm.into_code()) };
 
-    // Write out JIT code for visualization.
-    // Disassemble for example with `ndisasm -b 64 jit.asm`.
-    rt.dump();
+    // Disassemble JIT code and write to stdout.
+    rt.disasm();
 
     for n in 0..15 {
         let fib_jit = fib(n);
diff --git a/src/juicebox_asm/rt.rs.html b/src/juicebox_asm/rt.rs.html
index b0fad10..d138a8e 100644
--- a/src/juicebox_asm/rt.rs.html
+++ b/src/juicebox_asm/rt.rs.html
@@ -266,7 +266,40 @@
 265
 266
 267
-268
//! Simple `mmap`ed runtime.
+268
+269
+270
+271
+272
+273
+274
+275
+276
+277
+278
+279
+280
+281
+282
+283
+284
+285
+286
+287
+288
+289
+290
+291
+292
+293
+294
+295
+296
+297
+298
+299
+300
+301
//! Simple `mmap`ed runtime.
 //!
 //! This runtime supports adding code to executable pages and turn the added code into user
 //! specified function pointer.
@@ -424,22 +457,55 @@
         unsafe { Self::as_fn::<F>(fn_start) }
     }
 
-    /// Dump the code added so far to the runtime into a file called `jit.asm` in the processes
-    /// current working directory.
-    ///
-    /// The code can be inspected with a disassembler as for example `ndiasm` from
-    /// [nasm.us](https://nasm.us/index.php).
-    /// ```sh
-    /// ndisasm -b 64 jit.asm
-    /// ```
+    /// Disassemble the code currently added to the runtime, using
+    /// [`ndisasm`](https://nasm.us/index.php) and print it to _stdout_. If
+    /// `ndisasm` is not available on the system this prints a warning and
+    /// becomes a nop.
     ///
     /// # Panics
     ///
-    /// Panics if writing the file failed.
-    pub fn dump(&self) {
+    /// Panics if anything goes wrong with spawning, writing to or reading from
+    /// the `ndisasm` child process.
+    pub fn disasm(&self) {
         assert!(self.idx <= self.len);
         let code = unsafe { core::slice::from_raw_parts(self.buf, self.idx) };
-        std::fs::write("jit.asm", code).expect("Failed to write file");
+
+        // Create ndisasm process, which expects input on stdin.
+        let mut child = match std::process::Command::new("ndisasm")
+            .args(["-b64", "-"])
+            .stdin(std::process::Stdio::piped())
+            .stdout(std::process::Stdio::piped())
+            .spawn()
+        {
+            Ok(child) => child,
+            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
+                println!("Runtime::disasm: ndisasm not found, skipping!");
+                return;
+            }
+            Err(err) => {
+                panic!("{:?}", err);
+            }
+        };
+
+        // Write code to stdin of ndisasm.
+        use std::io::Write;
+        child
+            .stdin
+            .take()
+            .expect("failed to take stdin")
+            .write_all(code)
+            .expect("failed to write bytes to stdin");
+
+        // Wait for output from ndisasm and print to stdout.
+        println!(
+            "{}",
+            String::from_utf8_lossy(
+                &child
+                    .wait_with_output()
+                    .expect("failed to get stdout")
+                    .stdout
+            )
+        );
     }
 
     /// Reinterpret the block of code pointed to by `fn_start` as `F`.
-- 
cgit v1.2.3