diff options
author | johannst <johannst@users.noreply.github.com> | 2024-12-13 22:47:47 +0000 |
---|---|---|
committer | johannst <johannst@users.noreply.github.com> | 2024-12-13 22:47:47 +0000 |
commit | 550a1794e9460467c474d56554b62942bf911a9c (patch) | |
tree | 58215b8e6267b7d4b9b690d031bdad07ab34f6c4 /src/bf | |
parent | dd973aa72be71aac88e5449df3c82d5eeededbb5 (diff) | |
download | juicebox-asm-550a1794e9460467c474d56554b62942bf911a9c.tar.gz juicebox-asm-550a1794e9460467c474d56554b62942bf911a9c.zip |
deploy: 869761849ff64669244b6cbb79cac41f66654041
Diffstat (limited to 'src/bf')
-rw-r--r-- | src/bf/bf.rs.html | 44 |
1 files changed, 29 insertions, 15 deletions
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 @@ <a href="#401" id="401">401</a> <a href="#402" id="402">402</a> <a href="#403" id="403">403</a> -<a href="#404" id="404">404</a></pre></div><pre class="rust"><code><span class="doccomment">//! Brainfuck VM. +<a href="#404" id="404">404</a> +<a href="#405" id="405">405</a> +<a href="#406" id="406">406</a> +<a href="#407" id="407">407</a> +<a href="#408" id="408">408</a> +<a href="#409" id="409">409</a> +<a href="#410" id="410">410</a> +<a href="#411" id="411">411</a></pre></div><pre class="rust"><code><span class="doccomment">//! 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! </span><span class="kw">use </span>std::collections::HashMap; <span class="kw">use </span>std::io::Write; @@ -439,7 +453,7 @@ <span class="kw">fn </span>new(prog: <span class="kw-2">&</span>str) -> <span class="prelude-ty">Result</span><<span class="self">Self</span>, String> { <span class="comment">// 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. </span><span class="kw">let </span>(imem, branches) = { <span class="comment">// Instruction memory holding the final bf program. </span><span class="kw">let </span><span class="kw-2">mut </span>imem = Vec::new(); @@ -591,8 +605,8 @@ <span class="comment">// Move data memory pointer (argument on jit entry) into correct register. </span>asm.mov(dmem_base, Reg64::rdi); - <span class="comment">// Move data memory size into correct register. - </span>asm.mov(dmem_size, Reg64::rsi); + <span class="comment">// Move data memory size (compile time constant) into correct register. + </span>asm.mov(dmem_size, Imm64::from(vm.dmem.len())); <span class="comment">// Clear data memory index. </span>asm.xor(dmem_idx, dmem_idx); @@ -720,11 +734,11 @@ </span>pc += <span class="number">1</span>; } - <span class="kw">let </span><span class="kw-2">mut </span>ret_epilogue = Label::new(); + <span class="kw">let </span><span class="kw-2">mut </span>epilogue = Label::new(); <span class="comment">// Successful return from bf program. </span>asm.xor(Reg64::rax, Reg64::rax); - asm.bind(<span class="kw-2">&mut </span>ret_epilogue); + asm.bind(<span class="kw-2">&mut </span>epilogue); <span class="comment">// Restore callee saved registers before returning from jit. </span>asm.pop(dmem_idx); asm.pop(dmem_size); @@ -734,12 +748,12 @@ <span class="comment">// Return because of data pointer overflow. </span>asm.bind(<span class="kw-2">&mut </span>oob_ov); asm.mov(Reg64::rax, Imm64::from(<span class="number">1</span>)); - asm.jmp(<span class="kw-2">&mut </span>ret_epilogue); + asm.jmp(<span class="kw-2">&mut </span>epilogue); <span class="comment">// Return because of data pointer underflow. </span>asm.bind(<span class="kw-2">&mut </span>oob_uv); asm.mov(Reg64::rax, Imm64::from(<span class="number">2</span>)); - asm.jmp(<span class="kw-2">&mut </span>ret_epilogue); + asm.jmp(<span class="kw-2">&mut </span>epilogue); <span class="kw">if </span>!label_stack.is_empty() { <span class="macro">panic!</span>(<span class="string">"encountered un-balanced brackets, left-over '[' after jitting bf program"</span>) @@ -747,11 +761,11 @@ <span class="comment">// Get function pointer to jitted bf program. </span><span class="kw">let </span><span class="kw-2">mut </span>rt = Runtime::new(); - <span class="kw">let </span>bf_entry = <span class="kw">unsafe </span>{ rt.add_code::<<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>(<span class="kw-2">*mut </span>u8, usize) -> u64>(asm.into_code()) }; + <span class="kw">let </span>bf_entry = <span class="kw">unsafe </span>{ rt.add_code::<<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>(<span class="kw-2">*mut </span>u8) -> u64>(asm.into_code()) }; <span class="comment">// Execute jitted bf program. - </span><span class="kw">match </span>bf_entry(<span class="kw-2">&mut </span>vm.dmem <span class="kw">as </span><span class="kw-2">*mut </span>u8, vm.dmem.len()) { - <span class="number">0 </span>=> {} + </span><span class="kw">match </span>bf_entry(<span class="kw-2">&mut </span>vm.dmem <span class="kw">as </span><span class="kw-2">*mut </span>u8) { + <span class="number">0 </span>=> { <span class="comment">/* success */ </span>} <span class="number">1 </span>=> <span class="macro">panic!</span>(<span class="string">"oob: data pointer overflow"</span>), <span class="number">2 </span>=> <span class="macro">panic!</span>(<span class="string">"oob: data pointer underflow"</span>), <span class="kw">_ </span>=> <span class="macro">unreachable!</span>(), |