aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bf/bf.rs.html44
-rw-r--r--src/juicebox_asm/rt.rs.html42
2 files changed, 49 insertions, 37 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].
+//! ```
+//! ++++++++[&gt;++++[&gt;++&gt;+++&gt;+++&gt;+&lt;&lt;&lt;&lt;-]&gt;+&gt;+&gt;-&gt;&gt;+[&lt;]&lt;-]&gt;&gt;.&gt;---.+++++++..+++.&gt;&gt;.&lt;-.&lt;.+++.------.--------.&gt;&gt;+.&gt;++.
+//! ```
+//!
+//! [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">&amp;</span>str) -&gt; <span class="prelude-ty">Result</span>&lt;<span class="self">Self</span>, String&gt; {
<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">&amp;mut </span>ret_epilogue);
+ asm.bind(<span class="kw-2">&amp;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">&amp;mut </span>oob_ov);
asm.mov(Reg64::rax, Imm64::from(<span class="number">1</span>));
- asm.jmp(<span class="kw-2">&amp;mut </span>ret_epilogue);
+ asm.jmp(<span class="kw-2">&amp;mut </span>epilogue);
<span class="comment">// Return because of data pointer underflow.
</span>asm.bind(<span class="kw-2">&amp;mut </span>oob_uv);
asm.mov(Reg64::rax, Imm64::from(<span class="number">2</span>));
- asm.jmp(<span class="kw-2">&amp;mut </span>ret_epilogue);
+ asm.jmp(<span class="kw-2">&amp;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::&lt;<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>(<span class="kw-2">*mut </span>u8, usize) -&gt; u64&gt;(asm.into_code()) };
+ <span class="kw">let </span>bf_entry = <span class="kw">unsafe </span>{ rt.add_code::&lt;<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>(<span class="kw-2">*mut </span>u8) -&gt; u64&gt;(asm.into_code()) };
<span class="comment">// Execute jitted bf program.
- </span><span class="kw">match </span>bf_entry(<span class="kw-2">&amp;mut </span>vm.dmem <span class="kw">as </span><span class="kw-2">*mut </span>u8, vm.dmem.len()) {
- <span class="number">0 </span>=&gt; {}
+ </span><span class="kw">match </span>bf_entry(<span class="kw-2">&amp;mut </span>vm.dmem <span class="kw">as </span><span class="kw-2">*mut </span>u8) {
+ <span class="number">0 </span>=&gt; { <span class="comment">/* success */ </span>}
<span class="number">1 </span>=&gt; <span class="macro">panic!</span>(<span class="string">"oob: data pointer overflow"</span>),
<span class="number">2 </span>=&gt; <span class="macro">panic!</span>(<span class="string">"oob: data pointer underflow"</span>),
<span class="kw">_ </span>=&gt; <span class="macro">unreachable!</span>(),
diff --git a/src/juicebox_asm/rt.rs.html b/src/juicebox_asm/rt.rs.html
index 039fa77..b0fad10 100644
--- a/src/juicebox_asm/rt.rs.html
+++ b/src/juicebox_asm/rt.rs.html
@@ -266,8 +266,7 @@
<a href="#265" id="265">265</a>
<a href="#266" id="266">266</a>
<a href="#267" id="267">267</a>
-<a href="#268" id="268">268</a>
-<a href="#269" id="269">269</a></pre></div><pre class="rust"><code><span class="doccomment">//! Simple `mmap`ed runtime.
+<a href="#268" id="268">268</a></pre></div><pre class="rust"><code><span class="doccomment">//! Simple `mmap`ed runtime.
//!
//! This runtime supports adding code to executable pages and turn the added code into user
//! specified function pointer.
@@ -275,8 +274,6 @@
</span><span class="attr">#[cfg(not(target_os = <span class="string">"linux"</span>))]
</span><span class="macro">compile_error!</span>(<span class="string">"This runtime is only supported on linux"</span>);
-<span class="kw">use </span>nix::sys::mman::{mmap, mprotect, munmap, MapFlags, ProtFlags};
-
<span class="kw">mod </span>perf {
<span class="kw">use </span>std::fs;
<span class="kw">use </span>std::io::Write;
@@ -298,7 +295,7 @@
<span class="kw">impl </span>PerfMap {
<span class="doccomment">/// Create an empty perf map file.
</span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>new() -&gt; <span class="self">Self </span>{
- <span class="kw">let </span>name = <span class="macro">format!</span>(<span class="string">"/tmp/perf-{}.map"</span>, nix::unistd::getpid());
+ <span class="kw">let </span>name = <span class="macro">format!</span>(<span class="string">"/tmp/perf-{}.map"</span>, <span class="kw">unsafe </span>{ libc::getpid() });
<span class="kw">let </span>file = fs::OpenOptions::new()
.truncate(<span class="bool-val">true</span>)
.create(<span class="bool-val">true</span>)
@@ -338,22 +335,26 @@
/// Panics if the `mmap` call fails.
</span><span class="kw">pub fn </span>new() -&gt; Runtime {
<span class="comment">// Allocate a single page.
- </span><span class="kw">let </span>len = core::num::NonZeroUsize::new(<span class="number">4096</span>).expect(<span class="string">"Value is non zero"</span>);
+ </span><span class="kw">let </span>len = <span class="number">4096</span>;
<span class="kw">let </span>buf = <span class="kw">unsafe </span>{
- mmap(
- <span class="prelude-val">None</span>,
+ libc::mmap(
+ std::ptr::null_mut(),
len,
- ProtFlags::PROT_NONE,
- MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS,
+ libc::PROT_NONE,
+ libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
<span class="number">0</span>, <span class="comment">/* fd */
</span><span class="number">0</span>, <span class="comment">/* off */
- </span>)
- .expect(<span class="string">"Failed to mmap runtime code page"</span>) <span class="kw">as </span><span class="kw-2">*mut </span>u8
+ </span>) <span class="kw">as </span><span class="kw-2">*mut </span>u8
};
+ <span class="macro">assert_ne!</span>(
+ buf.cast(),
+ libc::MAP_FAILED,
+ <span class="string">"Failed to mmap runtime code page"
+ </span>);
Runtime {
buf,
- len: len.get(),
+ len,
idx: <span class="number">0</span>,
perf: <span class="prelude-val">None</span>,
}
@@ -455,12 +456,8 @@
</span><span class="kw">fn </span>protect(<span class="kw-2">&amp;mut </span><span class="self">self</span>) {
<span class="kw">unsafe </span>{
<span class="comment">// Remove write permissions from code page and allow to read-execute from it.
- </span>mprotect(
- <span class="self">self</span>.buf.cast(),
- <span class="self">self</span>.len,
- ProtFlags::PROT_READ | ProtFlags::PROT_EXEC,
- )
- .expect(<span class="string">"Failed to RX mprotect runtime code page"</span>);
+ </span><span class="kw">let </span>ret = libc::mprotect(<span class="self">self</span>.buf.cast(), <span class="self">self</span>.len, libc::PROT_READ | libc::PROT_EXEC);
+ <span class="macro">assert_eq!</span>(ret, <span class="number">0</span>, <span class="string">"Failed to RX mprotect runtime code page"</span>);
}
}
@@ -472,8 +469,8 @@
</span><span class="kw">fn </span>unprotect(<span class="kw-2">&amp;mut </span><span class="self">self</span>) {
<span class="kw">unsafe </span>{
<span class="comment">// Add write permissions to code page.
- </span>mprotect(<span class="self">self</span>.buf.cast(), <span class="self">self</span>.len, ProtFlags::PROT_WRITE)
- .expect(<span class="string">"Failed to W mprotect runtime code page"</span>);
+ </span><span class="kw">let </span>ret = libc::mprotect(<span class="self">self</span>.buf.cast(), <span class="self">self</span>.len, libc::PROT_WRITE);
+ <span class="macro">assert_eq!</span>(ret, <span class="number">0</span>, <span class="string">"Failed to W mprotect runtime code page"</span>);
}
}
}
@@ -483,7 +480,8 @@
/// [`Runtime::add_code`].
</span><span class="kw">fn </span>drop(<span class="kw-2">&amp;mut </span><span class="self">self</span>) {
<span class="kw">unsafe </span>{
- munmap(<span class="self">self</span>.buf.cast(), <span class="self">self</span>.len).expect(<span class="string">"Failed to munmap runtime"</span>);
+ <span class="kw">let </span>ret = libc::munmap(<span class="self">self</span>.buf.cast(), <span class="self">self</span>.len);
+ <span class="macro">assert_eq!</span>(ret, <span class="number">0</span>, <span class="string">"Failed to munmap runtime"</span>);
}
}
}