Skip to content

Commit

Permalink
Merge pull request #391 from 0xPolygonMiden/greenhat/i390-add-fib-exa…
Browse files Browse the repository at this point in the history
…mple

chore: add fibonacci sequence example
  • Loading branch information
bitwalker authored Feb 10, 2025
2 parents 89e9607 + 8d13fe4 commit db59fd2
Show file tree
Hide file tree
Showing 27 changed files with 239 additions and 287 deletions.
5 changes: 0 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ exclude = [
"tests/rust-apps/fib",
"tests/rust-apps-wasm",
"cargo-ext/tests/data",
"examples",
]

[workspace.package]
Expand Down
6 changes: 4 additions & 2 deletions docs/usage/cargo-miden.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ As there is no panic infrastructure, `panic = "abort"` is set, and the panic han
to use the native WebAssembly `unreachable` intrinsic, so the compiler will strip out all of the
usual panic formatting code.

### Compiling to Miden package
## Compiling to Miden package

Now that you've created your project, compiling it to Miden package is as easy as running the
following command from the root of the project directory:
Expand All @@ -70,7 +70,7 @@ cargo miden build --release
This will emit the compiled artifacts to `target/miden/release/foo.masp`.


### Running a compiled Miden VM program
## Running a compiled Miden VM program


!!! warning
Expand All @@ -88,4 +88,6 @@ See `midenc run --help` for the inputs file format.



## Examples

Check out the [examples](https://github.com/0xPolygonMiden/compiler/tree/next/examples) for some `cargo-miden` project examples.
6 changes: 6 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Rust program examples

These examples are built using `cargo-miden`.

`cargo miden` is a `cargo` cargo extension. Check out its [documentation](https://0xpolygonmiden.github.io/compiler/usage/cargo-miden/#compiling-to-miden-assembly)
for more details on how to build and run the compiled programs.
1 change: 1 addition & 0 deletions examples/fibonacci/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
38 changes: 38 additions & 0 deletions examples/fibonacci/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions examples/fibonacci/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "fibonacci"
version = "0.1.0"
edition = "2021"

[lib]
# Build this crate as a self-contained, C-style dynamic library
# This is required to emit the proper Wasm module type
crate-type = ["cdylib"]

[dependencies]
# Miden SDK consists of a stdlib (intrinsic functions for VM ops, stdlib functions and types)
# and transaction kernel API for the Miden rollup

miden = { git = "https://github.com/0xPolygonMiden/compiler" }


[profile.release]
# optimize the output for size
opt-level = "z"
# Explicitly disable panic infrastructure on Wasm, as
# there is no proper support for them anyway, and it
# ensures that panics do not pull in a bunch of standard
# library code unintentionally
panic = "abort"

[profile.dev]
# Explicitly disable panic infrastructure on Wasm, as
# there is no proper support for them anyway, and it
# ensures that panics do not pull in a bunch of standard
# library code unintentionally
panic = "abort"
opt-level = 1
debug-assertions = false
overflow-checks = false
debug = true
21 changes: 21 additions & 0 deletions examples/fibonacci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# fibonacci

## Useful commands

`fibonacci` is built using the [Miden compiler](https://github.com/0xPolygonMiden/compiler).

`cargo miden` is a `cargo` cargo extension. Check out its [documentation](https://0xpolygonmiden.github.io/compiler/usage/cargo-miden/#compiling-to-miden-assembly)
for more details on how to build and run the compiled programs.

## Compile

```bash
cargo miden build --release
```

## Run

```bash
midenc run target/miden/release/fibonacci.masp --inputs inputs.toml
```

2 changes: 2 additions & 0 deletions examples/fibonacci/inputs.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[inputs]
stack = [25]
5 changes: 5 additions & 0 deletions examples/fibonacci/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[toolchain]
channel = "nightly-2025-01-16"
components = ["rustfmt", "rust-src"]
targets = ["wasm32-wasi"]
profile = "minimal"
37 changes: 37 additions & 0 deletions examples/fibonacci/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Do not link against libstd (i.e. anything defined in `std::`)
#![no_std]

// However, we could still use some standard library types while
// remaining no-std compatible, if we uncommented the following lines:
//
// extern crate alloc;
// use alloc::vec::Vec;

// // Global allocator to use heap memory in no-std environment
// #[global_allocator]
// static ALLOC: BumpAlloc = miden::BumpAlloc::new();

// Required for no-std crates
#[panic_handler]
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

// Pass up to 16 u32 inputs as entrypoint function parameters.
// The output is temporarely limited to 1 u32 value
//
// NOTE:
// The name of the entrypoint function is expected to be `entrypoint`. Do not remove the
// `#[no_mangle]` attribute, otherwise, the rustc will mangle the name and it'll not be recognized
// by the Miden compiler.
#[no_mangle]
pub fn entrypoint(n: u32) -> u32 {
let mut a = 0;
let mut b = 1;
for _ in 0..n {
let c = a + b;
a = b;
b = c;
}
a
}
1 change: 0 additions & 1 deletion tests/integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,3 @@ blake3.workspace = true
concat-idents = "1.1"
env_logger.workspace = true
miden-core.workspace = true
miden-integration-tests-rust-fib = { path = "../rust-apps/fib" }
32 changes: 32 additions & 0 deletions tests/integration/expected/examples/fib.hir
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
builtin.component #[name = #root] #[namespace = #root_ns] #[version = 1.0.0] #[visibility = public] {

builtin.module #[name = #fibonacci] #[visibility = public] {

builtin.function public @entrypoint(v0: i32) -> i32 {
^block3(v0: i32):
v2 = hir.constant 0 : i32;
v3 = hir.constant 0 : i32;
v4 = hir.constant 1 : i32;
hir.br block5 v4, v0, v3;
^block4(v1: i32):

^block5(v6: i32, v7: i32, v10: i32):
v8 = hir.constant 0 : i32;
v9 = hir.neq v7, v8 : i1;
hir.cond_br block7, block8 v9;
^block6(v5: i32):

^block7:
v11 = hir.constant -1 : i32;
v12 = hir.add v7, v11 : i32 #[overflow = wrapping];
v13 = hir.add v10, v6 : i32 #[overflow = wrapping];
hir.br block5 v13, v12, v6;
^block8:
hir.ret v10;
};
builtin.global_variable #[name = #__stack_pointer] #[ty = i32] #[visibility = private] {

hir.ret_imm #[value = 1048576];
};
};
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# mod miden_integration_tests_rust_fib_wasm
# mod fibonacci

export.fib
export.entrypoint
push.0
push.1
movup.2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(module $miden_integration_tests_rust_fib_wasm.wasm
(module $fibonacci.wasm
(type (;0;) (func (param i32) (result i32)))
(func $fib (;0;) (type 0) (param i32) (result i32)
(func $entrypoint (;0;) (type 0) (param i32) (result i32)
(local i32 i32 i32)
i32.const 0
local.set 1
Expand Down Expand Up @@ -30,10 +30,6 @@
)
(memory (;0;) 16)
(global $__stack_pointer (;0;) (mut i32) i32.const 1048576)
(global (;1;) i32 i32.const 1048576)
(global (;2;) i32 i32.const 1048576)
(export "memory" (memory 0))
(export "fib" (func $fib))
(export "__data_end" (global 1))
(export "__heap_base" (global 2))
(export "entrypoint" (func $entrypoint))
)
39 changes: 0 additions & 39 deletions tests/integration/expected/fib.hir

This file was deleted.

40 changes: 0 additions & 40 deletions tests/integration/expected/fib_hir2.hir

This file was deleted.

39 changes: 0 additions & 39 deletions tests/integration/expected/fib_hir2.wat

This file was deleted.

Loading

0 comments on commit db59fd2

Please sign in to comment.