diff options
| author | Henry Gressmann <mail@henrygressmann.de> | 2024-01-26 12:03:59 +0100 |
|---|---|---|
| committer | Henry Gressmann <mail@henrygressmann.de> | 2024-01-26 12:03:59 +0100 |
| commit | f3d890a36d6299dbd70377ce57b0bd9d61de5c1b (patch) | |
| tree | 36b51022af641c0f464fd0c5d4456b722e5f735b /examples/wasm | |
| parent | 461126ce309f99e4ad2a6ddcbc175ea01c5e8fee (diff) | |
docs: working selfhosted example
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'examples/wasm')
| -rw-r--r-- | examples/wasm/add.wasm | bin | 65 -> 0 bytes | |||
| -rw-r--r-- | examples/wasm/add.wat | 16 | ||||
| -rw-r--r-- | examples/wasm/call.wat | 42 | ||||
| -rw-r--r-- | examples/wasm/global.wat | 1 | ||||
| -rw-r--r-- | examples/wasm/helloworld.wasm | bin | 115 -> 0 bytes | |||
| -rw-r--r-- | examples/wasm/helloworld.wat | 15 | ||||
| -rw-r--r-- | examples/wasm/loop.wat | 84 | ||||
| -rw-r--r-- | examples/wasm/test.wat | 7 |
8 files changed, 0 insertions, 165 deletions
diff --git a/examples/wasm/add.wasm b/examples/wasm/add.wasm Binary files differdeleted file mode 100644 index 92e3432..0000000 --- a/examples/wasm/add.wasm +++ /dev/null diff --git a/examples/wasm/add.wat b/examples/wasm/add.wat deleted file mode 100644 index 4976689..0000000 --- a/examples/wasm/add.wat +++ /dev/null @@ -1,16 +0,0 @@ -(module - (func $add (export "add") (param $a i32) (param $b i32) (result i32) - local.get $a - local.get $b - i32.add) - - (func $sub (export "sub") (param $a i32) (param $b i32) (result i32) - local.get $a - local.get $b - i32.sub) - - (func $add_64 (export "add_64") (param $a i64) (param $b i64) (result i64) - local.get $a - local.get $b - i64.add) -) diff --git a/examples/wasm/call.wat b/examples/wasm/call.wat deleted file mode 100644 index d515e78..0000000 --- a/examples/wasm/call.wat +++ /dev/null @@ -1,42 +0,0 @@ -(module - (func (export "check") (param i32) (result i32) - i64.const 0 ;; Set 0 to the stack - local.get 0 - i32.const 10 - i32.lt_s ;; Check if input is less than 10 - if (param i64) (result i32) ;; If so, - i32.const 1 ;; Set 1 to the stack - return ;; And return immediately - else ;; Otherwise, - i32.const 0 ;; Set 0 to the stack - return ;; And return immediately - end) ;; End of the if/else block - - (func (export "simple_block") (result i32) - (block (result i32) - (i32.const 0) - (i32.const 1) - (i32.add) - ) - ) - - (func (export "checkloop") (result i32) - (block (result i32) - (i32.const 0) - (loop (param i32) - (block (br 2 (i32.const 18))) - (br 0 (i32.const 20)) - ) - (i32.const 19) - ) - ) - - - (func (export "param") (result i32) - (i32.const 1) - (loop (param i32) (result i32) - (i32.const 2) - (i32.add) - ) - ) -)
\ No newline at end of file diff --git a/examples/wasm/global.wat b/examples/wasm/global.wat deleted file mode 100644 index 22bde45..0000000 --- a/examples/wasm/global.wat +++ /dev/null @@ -1 +0,0 @@ -(module (global i32 (i32.const 0)))
\ No newline at end of file diff --git a/examples/wasm/helloworld.wasm b/examples/wasm/helloworld.wasm Binary files differdeleted file mode 100644 index a5c95d0..0000000 --- a/examples/wasm/helloworld.wasm +++ /dev/null diff --git a/examples/wasm/helloworld.wat b/examples/wasm/helloworld.wat deleted file mode 100644 index b74c98f..0000000 --- a/examples/wasm/helloworld.wat +++ /dev/null @@ -1,15 +0,0 @@ -(module - ;; Imports from JavaScript namespace - (import "console" "log" (func $log (param i32 i32))) ;; Import log function - (import "js" "mem" (memory 1)) ;; Import 1 page of memory (54kb) - - ;; Data section of our module - (data (i32.const 0) "Hello World from WebAssembly!") - - ;; Function declaration: Exported as helloWorld(), no arguments - (func (export "helloWorld") - i32.const 0 ;; pass offset 0 to log - i32.const 29 ;; pass length 29 to log (strlen of sample text) - call $log - ) -)
\ No newline at end of file diff --git a/examples/wasm/loop.wat b/examples/wasm/loop.wat deleted file mode 100644 index 0dcd191..0000000 --- a/examples/wasm/loop.wat +++ /dev/null @@ -1,84 +0,0 @@ -(module - (func $loop_test (export "loop_test") (result i32) - (local i32) ;; Local 0: Counter - - ;; Initialize the counter - (local.set 0 (i32.const 0)) - - ;; Loop starts here - (loop $my_loop - ;; Increment the counter - (local.set 0 (i32.add (local.get 0) (i32.const 1))) - - ;; Exit condition: break out of the loop if counter >= 10 - (br_if $my_loop (i32.lt_s (local.get 0) (i32.const 10))) - ) - - ;; Return the counter value - (local.get 0) - ) - - (func $loop_test3 (export "loop_test3") (result i32) - (local i32) ;; Local 0: Counter - - ;; Initialize the counter - (local.set 0 (i32.const 0)) - - ;; Loop starts here - (block $exit_loop ;; Label for exiting the loop - (loop $my_loop - ;; Increment the counter - (local.set 0 (i32.add (local.get 0) (i32.const 1))) - - ;; Prepare an index for br_table - ;; Here, we use the counter, but you could modify this - ;; For simplicity, 0 will continue the loop, any other value will exit - (local.get 0) - (i32.const 10) - (i32.lt_s) - (br_table $my_loop $exit_loop) - ) - ) - - ;; Return the counter value - (local.get 0) - ) - - (func $calculate (export "loop_test2") (result i32) - (local i32) ;; Local 0: Counter for the outer loop - (local i32) ;; Local 1: Counter for the inner loop - (local i32) ;; Local 2: Result variable - - ;; Initialize variables - (local.set 0 (i32.const 0)) ;; Initialize outer loop counter - (local.set 1 (i32.const 0)) ;; Initialize inner loop counter - (local.set 2 (i32.const 0)) ;; Initialize result variable - - (block $outer ;; Outer loop label - (loop $outer_loop - (local.set 1 (i32.const 5)) ;; Reset inner loop counter for each iteration of the outer loop - - (block $inner ;; Inner loop label - (loop $inner_loop - (br_if $inner (i32.eqz (local.get 1))) ;; Break to $inner if inner loop counter is zero - - ;; Computation: Adding product of counters to the result - (local.set 2 (i32.add (local.get 2) (i32.mul (local.get 0) (local.get 1)))) - - ;; Decrement inner loop counter - (local.set 1 (i32.sub (local.get 1) (i32.const 1))) - ) - ) - - ;; Increment outer loop counter - (local.set 0 (i32.add (local.get 0) (i32.const 1))) - - ;; Break condition for outer loop: break if outer loop counter >= 5 - (br_if $outer (i32.ge_s (local.get 0) (i32.const 5))) - ) - ) - - ;; Return the result - (local.get 2) - ) -)
\ No newline at end of file diff --git a/examples/wasm/test.wat b/examples/wasm/test.wat deleted file mode 100644 index 563f382..0000000 --- a/examples/wasm/test.wat +++ /dev/null @@ -1,7 +0,0 @@ -(module - (func (export "test") (result i32) - (i32.const 1) - ;; comment - (return (i32.const 2)) - ) -)
\ No newline at end of file |
