mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 11:16:42 +01:00
add simple rust example
This commit is contained in:
2
examples/rust/build.sh
Executable file
2
examples/rust/build.sh
Executable file
@@ -0,0 +1,2 @@
|
|||||||
|
rustc --target=wasm32-unknown-unknown --crate-type cdylib -C opt-level="z" -C "link-args=--import-memory --initial-memory=262144 -zstack-size=65536" -o tunnel.wasm tunnel.rs && \
|
||||||
|
wasm-opt -Oz -o tunnel.wasm tunnel.wasm
|
||||||
25
examples/rust/readme.txt
Normal file
25
examples/rust/readme.txt
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
A small example how to produce somewhat reasonably small MicroW8
|
||||||
|
carts in rust.
|
||||||
|
|
||||||
|
A nightly rust compiler is needed for the unstable sqrtf32
|
||||||
|
intrinsic.
|
||||||
|
|
||||||
|
Simply compiling with rustc as shown in build.sh results in a
|
||||||
|
339 byte tunnel.wasm. Using wasm-opt this can be reduced to
|
||||||
|
244 bytes.
|
||||||
|
|
||||||
|
When you disassemble this wasm file using wasm2wat you can see
|
||||||
|
these globals and exports:
|
||||||
|
|
||||||
|
(global (;0;) i32 (i32.const 65536))
|
||||||
|
(global (;1;) i32 (i32.const 65536))
|
||||||
|
(export "__data_end" (global 0))
|
||||||
|
(export "__heap_base" (global 1))
|
||||||
|
|
||||||
|
They are meant to be used for heap allocations and stack for any
|
||||||
|
values that are not simple scalars (i32, f32, etc.). Since our
|
||||||
|
code doesn't actually use any of that, we can just delete them
|
||||||
|
in a text editor and assemble the code again with wat2wasm.
|
||||||
|
|
||||||
|
This gives us a 200 byte wasm file. Running this through
|
||||||
|
uw8-tool pack brings us to the final size of 137 bytes.
|
||||||
31
examples/rust/tunnel.rs
Normal file
31
examples/rust/tunnel.rs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![feature(core_intrinsics)]
|
||||||
|
|
||||||
|
#[link(wasm_import_module = "math")]
|
||||||
|
extern "C" {
|
||||||
|
fn atan2(x: f32, y: f32) -> f32;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sqrt(v: f32) -> f32 {
|
||||||
|
unsafe { core::intrinsics::sqrtf32(v) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn tic(time: i32) {
|
||||||
|
unsafe {
|
||||||
|
for i in 0..320 * 256 {
|
||||||
|
let t = time as f32 / 10 as f32;
|
||||||
|
let x = (i % 320 - 160) as f32;
|
||||||
|
let y = (i / 320 - 128) as f32;
|
||||||
|
let d = 20000 as f32 / sqrt(x * x + y * y + 1 as f32);
|
||||||
|
let u = atan2(x, y) * 512f32 / 3.141;
|
||||||
|
let c = ((d + t).to_int_unchecked::<i32>() ^ (u + t).to_int_unchecked::<i32>()) as u8;
|
||||||
|
*((120 + i) as *mut u8) = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[panic_handler]
|
||||||
|
fn panic(_: &core::panic::PanicInfo) -> ! {
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
BIN
examples/rust/tunnel.uw8
Normal file
BIN
examples/rust/tunnel.uw8
Normal file
Binary file not shown.
@@ -440,10 +440,10 @@ fn remap_function(
|
|||||||
De::BrTable { .. } => todo!(),
|
De::BrTable { .. } => todo!(),
|
||||||
De::Return => En::Return,
|
De::Return => En::Return,
|
||||||
De::Call { function_index } => En::Call(
|
De::Call { function_index } => En::Call(
|
||||||
dbg!(*function_map
|
*function_map
|
||||||
.get(&function_index)
|
.get(&function_index)
|
||||||
.ok_or_else(|| anyhow!("Function index out of range: {}", function_index))?,
|
.ok_or_else(|| anyhow!("Function index out of range: {}", function_index))?,
|
||||||
)),
|
),
|
||||||
De::CallIndirect { .. }
|
De::CallIndirect { .. }
|
||||||
| De::ReturnCall { .. }
|
| De::ReturnCall { .. }
|
||||||
| De::ReturnCallIndirect { .. }
|
| De::ReturnCallIndirect { .. }
|
||||||
|
|||||||
Reference in New Issue
Block a user