aboutsummaryrefslogtreecommitdiffhomepage
path: root/juicebox_asm/index.html
blob: ae482cb7e64b12440aca693109d565905ad15c76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="A simple `x64` jit assembler with a minimal runtime to execute emitted code for fun."><title>juicebox_asm - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-b0742ba02757f159.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="juicebox_asm" data-themes="" data-resource-suffix="" data-rustdoc-version="1.83.0 (90b35a623 2024-11-26)" data-channel="1.83.0" data-search-js="search-f0d225181b97f9a4.js" data-settings-js="settings-805db61a62df4bd2.js" ><script src="../static.files/storage-1d39b6787ed640ff.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-f070b9041d14864c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-0111fcff984fae8f.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../juicebox_asm/index.html">juicebox_<wbr>asm</a><span class="version">0.1.0</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#modules">Crate Items</a></h3><ul class="block"><li><a href="#modules" title="Modules">Modules</a></li><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#enums" title="Enums">Enums</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>juicebox_asm</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/juicebox_asm/lib.rs.html#1-90">source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A simple <code>x64</code> jit assembler with a minimal runtime to execute emitted code for fun.</p>
<p>The following is an fibonacci example implementation.</p>

<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>juicebox_asm::{Asm, Reg64, Imm64, Label};
<span class="kw">use </span>juicebox_asm::insn::<span class="kw-2">*</span>;
<span class="kw">use </span>juicebox_asm::Runtime;

<span class="kw">const fn </span>fib_rs(n: u64) -&gt; u64 {
    <span class="kw">match </span>n {
        <span class="number">0 </span>=&gt; <span class="number">0</span>,
        <span class="number">1 </span>=&gt; <span class="number">1</span>,
        <span class="kw">_ </span>=&gt; fib_rs(n - <span class="number">2</span>) + fib_rs(n - <span class="number">1</span>),
    }
}

<span class="kw">fn </span>main() {
    <span class="kw">let </span><span class="kw-2">mut </span>asm = Asm::new();

    <span class="kw">let </span><span class="kw-2">mut </span>lp = Label::new();
    <span class="kw">let </span><span class="kw-2">mut </span>end = Label::new();

    <span class="comment">// Reference implementation:
    //
    // int fib(int n) {
    //   int tmp = 0;
    //   int prv = 1;
    //   int sum = 0;
    // loop:
    //   if (n == 0) goto end;
    //   tmp = sum;
    //   sum += prv;
    //   prv = tmp;
    //   --n;
    //   goto loop;
    // end:
    //   return sum;
    // }

    // SystemV abi:
    //   rdi -&gt; first argument
    //   rax -&gt; return value
    </span><span class="kw">let </span>n = Reg64::rdi;
    <span class="kw">let </span>sum = Reg64::rax;

    <span class="kw">let </span>tmp = Reg64::rcx;
    <span class="kw">let </span>prv = Reg64::rbx;

    asm.mov(tmp, Imm64::from(<span class="number">0</span>));
    asm.mov(prv, Imm64::from(<span class="number">1</span>));
    asm.mov(sum, Imm64::from(<span class="number">0</span>));

    asm.bind(<span class="kw-2">&amp;mut </span>lp);
    asm.test(n, n);
    asm.jz(<span class="kw-2">&amp;mut </span>end);
    asm.mov(tmp, sum);
    asm.add(sum, prv);
    asm.mov(prv, tmp);
    asm.dec(n);
    asm.jmp(<span class="kw-2">&amp;mut </span>lp);
    asm.bind(<span class="kw-2">&amp;mut </span>end);
    asm.ret();

    <span class="comment">// Move code into executable page and get function pointer to it.
    </span><span class="kw">let </span><span class="kw-2">mut </span>rt = Runtime::new();
    <span class="kw">let </span>fib = <span class="kw">unsafe </span>{ rt.add_code::&lt;<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>(u64) -&gt; u64&gt;(<span class="kw-2">&amp;</span>asm.into_code()) };

    <span class="kw">for </span>n <span class="kw">in </span><span class="number">0</span>..<span class="number">15 </span>{
        <span class="kw">let </span>fib_jit = fib(n);
        <span class="macro">println!</span>(<span class="string">"fib({}) = {}"</span>, n, fib_jit);
        <span class="macro">assert_eq!</span>(fib_jit, fib_rs(n));
    }
}</code></pre></div>
</div></details><h2 id="modules" class="section-header">Modules<a href="#modules" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="mod" href="insn/index.html" title="mod juicebox_asm::insn">insn</a></div><div class="desc docblock-short">Trait definitions of various instructions.</div></li></ul><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.Asm.html" title="struct juicebox_asm::Asm">Asm</a></div><div class="desc docblock-short"><code>x64</code> jit assembler.</div></li><li><div class="item-name"><a class="struct" href="struct.Imm8.html" title="struct juicebox_asm::Imm8">Imm8</a></div><div class="desc docblock-short">Type representing an 8 bit immediate.</div></li><li><div class="item-name"><a class="struct" href="struct.Imm16.html" title="struct juicebox_asm::Imm16">Imm16</a></div><div class="desc docblock-short">Type representing a 16 bit immediate.</div></li><li><div class="item-name"><a class="struct" href="struct.Imm32.html" title="struct juicebox_asm::Imm32">Imm32</a></div><div class="desc docblock-short">Type representing a 32 bit immediate.</div></li><li><div class="item-name"><a class="struct" href="struct.Imm64.html" title="struct juicebox_asm::Imm64">Imm64</a></div><div class="desc docblock-short">Type representing a 64 bit immediate.</div></li><li><div class="item-name"><a class="struct" href="struct.Label.html" title="struct juicebox_asm::Label">Label</a></div><div class="desc docblock-short">A label which is used as target for jump instructions.</div></li><li><div class="item-name"><a class="struct" href="struct.Mem8.html" title="struct juicebox_asm::Mem8">Mem8</a></div><div class="desc docblock-short">A memory operand with <code>byte</code> size (8 bit).</div></li><li><div class="item-name"><a class="struct" href="struct.Mem16.html" title="struct juicebox_asm::Mem16">Mem16</a></div><div class="desc docblock-short">A memory operand with <code>word</code> size (16 bit).</div></li><li><div class="item-name"><a class="struct" href="struct.Mem32.html" title="struct juicebox_asm::Mem32">Mem32</a></div><div class="desc docblock-short">A memory operand with <code>dword</code> size (32 bit).</div></li><li><div class="item-name"><a class="struct" href="struct.Mem64.html" title="struct juicebox_asm::Mem64">Mem64</a></div><div class="desc docblock-short">A memory operand with <code>qword</code> size (64 bit).</div></li><li><div class="item-name"><a class="struct" href="struct.Runtime.html" title="struct juicebox_asm::Runtime">Runtime</a></div><div class="desc docblock-short">A simple <code>mmap</code>ed runtime with executable pages.</div></li></ul><h2 id="enums" class="section-header">Enums<a href="#enums" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="enum" href="enum.Reg8.html" title="enum juicebox_asm::Reg8">Reg8</a></div><div class="desc docblock-short">Definition of 8 bit registers.</div></li><li><div class="item-name"><a class="enum" href="enum.Reg16.html" title="enum juicebox_asm::Reg16">Reg16</a></div><div class="desc docblock-short">Definition of 16 bit registers.</div></li><li><div class="item-name"><a class="enum" href="enum.Reg32.html" title="enum juicebox_asm::Reg32">Reg32</a></div><div class="desc docblock-short">Definition of 32 bit registers.</div></li><li><div class="item-name"><a class="enum" href="enum.Reg64.html" title="enum juicebox_asm::Reg64">Reg64</a></div><div class="desc docblock-short">Definition of 64 bit registers.</div></li></ul></section></div></main></body></html>