Skip to content

Commit

Permalink
docs(examples): introduce select example
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Aug 8, 2023
1 parent abc938c commit e26c608
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 9 deletions.
10 changes: 5 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 docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [Compiling Zink Project](./tutorial/compile-zink-project.md)
- [Examples](./examples/README.md)
- [AddTwo](./examples/add-two.md)
- [Select](./examples/select.md)
- [Fibonacci](./examples/fibonacci.md)
- [Command Line Tool](./cli/README.md)
- [elko](./cli/elko.md)
Expand Down
10 changes: 6 additions & 4 deletions docs/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

This chapter provides various zink examples in rust:

| name | knowledges | description |
| ------------------------ | ------------------------------------------ | ------------------------ |
| [`add-two`][add-two] | `params` | basic program in zink |
| [`fibonacci`][fibonacci] | `params`, `calls`, `recursion`, `if-block` | recursion implementation |
| name | knowledges | description |
|--------------------------|--------------------------------------------|---------------------------------------------------|
| [`add-two`][add-two] | `params` | basic program in zink |
| [`if-else`][if-else] | `params`, `code-section` | program with extra instruction `select` from WASM |
| [`fibonacci`][fibonacci] | `params`, `calls`, `recursion`, `if-block` | recursion implementation |

[add-two]: /examples/add-two.md
[if-else]: /examples/if-else.md
[fibonacci]: /examples/fibonacci.md
21 changes: 21 additions & 0 deletions docs/examples/add-two.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,24 @@ pub extern "C" fn addition(x: u64, y: u64) -> u64 {
x + y
}
```

A basic addition program in zink

```wasm
(module
(func (param i32) (param i32) (result i32)
(local.get 0)
(local.get 1)
(i32.add)
)
)
```

Requires:
- Get params from locals
- Process basic operand
- Return data from the result type

```text
6000356020350160005260206000f3
```
43 changes: 43 additions & 0 deletions docs/examples/fibonacci.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,46 @@ pub extern "C" fn recursion(n: usize) -> usize {
}
}
```

A recursion example, complex in bytecode

```wasm
(module
(type (;0;) (func (param i32) (result i32)))
(func (;0;) (type 0) (param i32) (result i32)
local.get 0
call 1)
(func (;1;) (type 0) (param i32) (result i32)
(local i32)
local.get 0
i32.const 2
i32.ge_u
if ;; label = @1
loop ;; label = @2
local.get 0 ;; 1
i32.const 1 ;; 2
i32.sub ;; 1
call 1 ;; 1
local.get 1 ;; 2
i32.add ;; 1
local.set 1 ;; 0
local.get 0 ;; 1
i32.const 2 ;; 2
i32.sub ;; 1
local.tee 0 ;; 1
i32.const 1 ;; 2
i32.gt_u ;; 1
br_if 0 (;@2;) ;; 2 -> 0
end
end
local.get 0
local.get 1
i32.add))
```

A more complex implementation of locals ( params + local variables) is introduced in this example,
control flow `br_if` and `loop` are compiled as well.

```
600035586010565b60005260206000f35b906000816002600190031015603d575b8160019003586010565b8101905081600290038092506001106020575b8181019150509060040156
```
1 change: 1 addition & 0 deletions docs/examples/if-else.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# AddTwo
44 changes: 44 additions & 0 deletions docs/examples/select.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Select


```rust
//! if-else example.
#![no_std]

// for the panic handler.
#[cfg(not(test))]
extern crate zink;

/// Simple if-else condition
#[no_mangle]
pub extern "C" fn if_else(x: u64, y: u64) -> u64 {
if x > y {
x
} else {
y
}
}

```

Code block selecting value with if-else will be compiled to instruction `select` in WASM

```wasm
(module
(type (;0;) (func (param i64 i64) (result i64)))
(func $if_else (type 0) (param i64 i64) (result i64)
local.get 0
local.get 1
local.get 0
local.get 1
i64.gt_u
select))
```

Since EVM bytecode doesn't have similar instruction, we have to implement it ourselves, the solution
is introduce a `select` function in the extra code section provided by zink compiler, jump to there
and jump back just like calling a real function.

```
60003560203560003560203510589190601c575b60005260206000f35b5060060156
```

0 comments on commit e26c608

Please sign in to comment.