From 550a1794e9460467c474d56554b62942bf911a9c Mon Sep 17 00:00:00 2001 From: johannst Date: Fri, 13 Dec 2024 22:47:47 +0000 Subject: deploy: 869761849ff64669244b6cbb79cac41f66654041 --- src/bf/bf.rs.html | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) (limited to 'src/bf/bf.rs.html') diff --git a/src/bf/bf.rs.html b/src/bf/bf.rs.html index 556b131..8efe643 100644 --- a/src/bf/bf.rs.html +++ b/src/bf/bf.rs.html @@ -402,10 +402,16 @@ 401 402 403 -404
//! Brainfuck VM.
+404
+405
+406
+407
+408
+409
+410
+411
//! Brainfuck VM.
 //!
-//! This example implements a simple
-//! [brainfuck](https://en.wikipedia.org/wiki/Brainfuck) interpreter
+//! This example implements a simple [brainfuck][bf] interpreter
 //! [`BrainfuckInterp`] and a jit compiler [`BrainfuckJit`].
 //!
 //! Brainfuck is an esoteric programming languge existing of 8 commands.
@@ -415,8 +421,16 @@
 //! - `-` decrement data at current data pointer.
 //! - `.` output data at current data pointer.
 //! - `,` read input and store at current data pointer.
-//! - `[` jump behind matching ']' if data at data pointer is zero.
-//! - `]` jump behind matching '[' if data at data pointer is non-zero.
+//! - `[` jump behind matching `]` if data at data pointer is zero.
+//! - `]` jump behind matching `[` if data at data pointer is non-zero.
+//!
+//! The following is the `hello-world` program from [wikipedia][hw].
+//! ```
+//! ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
+//! ```
+//!
+//! [bf]: https://en.wikipedia.org/wiki/Brainfuck
+//! [hw]: https://en.wikipedia.org/wiki/Brainfuck#Hello_World!
 
 use std::collections::HashMap;
 use std::io::Write;
@@ -439,7 +453,7 @@
     fn new(prog: &str) -> Result<Self, String> {
         // Do a first pass over the bf program to filter whitespace and detect
         // invalid tokens. Additionally validate all conditional branches, and
-        // compute their branch target.
+        // compute their branch targets.
         let (imem, branches) = {
             // Instruction memory holding the final bf program.
             let mut imem = Vec::new();
@@ -591,8 +605,8 @@
 
     // Move data memory pointer (argument on jit entry) into correct register.
     asm.mov(dmem_base, Reg64::rdi);
-    // Move data memory size into correct register.
-    asm.mov(dmem_size, Reg64::rsi);
+    // Move data memory size (compile time constant) into correct register.
+    asm.mov(dmem_size, Imm64::from(vm.dmem.len()));
     // Clear data memory index.
     asm.xor(dmem_idx, dmem_idx);
 
@@ -720,11 +734,11 @@
         pc += 1;
     }
 
-    let mut ret_epilogue = Label::new();
+    let mut epilogue = Label::new();
 
     // Successful return from bf program.
     asm.xor(Reg64::rax, Reg64::rax);
-    asm.bind(&mut ret_epilogue);
+    asm.bind(&mut epilogue);
     // Restore callee saved registers before returning from jit.
     asm.pop(dmem_idx);
     asm.pop(dmem_size);
@@ -734,12 +748,12 @@
     // Return because of data pointer overflow.
     asm.bind(&mut oob_ov);
     asm.mov(Reg64::rax, Imm64::from(1));
-    asm.jmp(&mut ret_epilogue);
+    asm.jmp(&mut epilogue);
 
     // Return because of data pointer underflow.
     asm.bind(&mut oob_uv);
     asm.mov(Reg64::rax, Imm64::from(2));
-    asm.jmp(&mut ret_epilogue);
+    asm.jmp(&mut epilogue);
 
     if !label_stack.is_empty() {
         panic!("encountered un-balanced brackets, left-over '[' after jitting bf program")
@@ -747,11 +761,11 @@
 
     // Get function pointer to jitted bf program.
     let mut rt = Runtime::new();
-    let bf_entry = unsafe { rt.add_code::<extern "C" fn(*mut u8, usize) -> u64>(asm.into_code()) };
+    let bf_entry = unsafe { rt.add_code::<extern "C" fn(*mut u8) -> u64>(asm.into_code()) };
 
     // Execute jitted bf program.
-    match bf_entry(&mut vm.dmem as *mut u8, vm.dmem.len()) {
-        0 => {}
+    match bf_entry(&mut vm.dmem as *mut u8) {
+        0 => { /* success */ }
         1 => panic!("oob: data pointer overflow"),
         2 => panic!("oob: data pointer underflow"),
         _ => unreachable!(),
-- 
cgit v1.2.3