-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples): introduce select example
- Loading branch information
Showing
7 changed files
with
121 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# AddTwo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |