From 2294180c3778d0fcfa877818e98c420fcd54bb8a Mon Sep 17 00:00:00 2001 From: johannst Date: Tue, 5 Dec 2023 22:08:06 +0000 Subject: deploy: 7e98f5def5942969f97f5f015e7fb8417793d132 --- src/add/add.rs.html | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/add/add.rs.html (limited to 'src/add/add.rs.html') diff --git a/src/add/add.rs.html b/src/add/add.rs.html new file mode 100644 index 0000000..112d0f1 --- /dev/null +++ b/src/add/add.rs.html @@ -0,0 +1,77 @@ +add.rs - source
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
+
//! 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.
+
+#[cfg(not(any(target_arch = "x86_64", target_os = "linux")))]
+compile_error!("Only supported on x86_64 with SystemV abi");
+
+use juicebox_asm::prelude::*;
+use juicebox_asm::Runtime;
+use Reg64::*;
+
+extern "C" fn add(a: u32, b: u32) -> u32 {
+    a + b
+}
+
+fn main() {
+    let mut asm = Asm::new();
+
+    // SystemV abi:
+    //   rdi -> first argument
+    //   rsi -> second argument
+    //   rax -> return value
+    //
+    asm.mov(rsi, Imm64::from(42));
+    asm.mov(rax, Imm64::from(add as u64));
+    asm.call(rax);
+    asm.ret();
+
+    let code = asm.into_code();
+    std::fs::write("jit.asm", &code).unwrap();
+
+    let mut rt = Runtime::new();
+    let add42 = unsafe { rt.add_code::<extern "C" fn(u32) -> u32>(code) };
+
+    let res = add42(5);
+    assert_eq!(res, 47);
+}
+
\ No newline at end of file -- cgit v1.2.3