From b27d3a088db2fac2324df2a05330801afee2e92b Mon Sep 17 00:00:00 2001 From: Rett Berg Date: Sat, 16 Nov 2019 12:38:14 -0700 Subject: add example of how stack might be managed --- wasm.html.markdown | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'wasm.html.markdown') diff --git a/wasm.html.markdown b/wasm.html.markdown index 98bfc000..92831e69 100644 --- a/wasm.html.markdown +++ b/wasm.html.markdown @@ -222,6 +222,92 @@ contributors: ) ) (export "apply_cos64" (func $apply_cos64)) + + ;; Demonstration of how this C code might be written by hand + ;; + ;; typedef struct { + ;; int a; + ;; int b; + ;; } sum_struct_t; + ;; + ;; sum_struct_t sum_struct_create(int a, int b) { + ;; return (sum_struct_t){a, b}; + ;; } + ;; + ;; int sum_local() { + ;; sum_struct_t s = sum_struct_create(40, 2); + ;; return s.a + s.b; + ;; } + ;; + ;; Wasm is a stack-based language, but for returning values more complicated + ;; than an int/float, a memory stack has to be manually managed. One ;; + ;; approach is to use a mutable global to store the stack_ptr. We give + ;; ourselves 1MiB of mem-stack and grow it downwards. + ;; + ;; Note: we are differentiating from the memstack (stack stored in memory) + ;; and the "stack", which wasm implicitly uses to to pass and return values. + (global $memstack_ptr (mut i32) (i32.const 65536)) + + ;; structs can only be returned by reference + (func $sum_struct_create + (param $sum_struct_ptr i32) + (param $var$a i32) + (param $var$b i32) + ;; c// sum_struct_ptr->a = a; + (i32.store + (get_local $sum_struct_ptr) + (get_local $var$a) + ) + + ;; c// sum_struct_ptr->b = b; + (i32.store offset=4 + (get_local $sum_struct_ptr) + (get_local $var$b) + ) + ) + + (func $sum_local (result i32) + (local $var$sum_struct$a i32) + (local $var$sum_struct$b i32) + (local $local_memstack_ptr i32) + + ;; reserve stack space + (i32.sub + (get_global $memstack_ptr) + (i32.const 8) + ) + tee_local $local_memstack_ptr ;; tee both stores and returns given value + set_global $memstack_ptr + + ;; call the function, storing the result in the stack + (call $sum_struct_create + ((;$sum_struct_ptr=;) get_local $local_memstack_ptr) + ((;$var$a=;) i32.const 40) + ((;$var$b=;) i32.const 2) + ) + + ;; retrieve values from struct + (set_local $var$sum_struct$a + (i32.load offset=0 (get_local $local_memstack_ptr)) + ) + (set_local $var$sum_struct$b + (i32.load offset=4 (get_local $local_memstack_ptr)) + ) + + ;; unreserve stack space + (set_global $memstack_ptr + (i32.add + (get_local $local_memstack_ptr) + (i32.const 8) + ) + ) + + (i32.add + (get_local $var$sum_struct$a) + (get_local $var$sum_struct$b) + ) + ) + (export "sum_local" (func $sum_local)) ) ``` -- cgit v1.2.3 From 4727925b1aef7f030adb8ae76bf7562a6280b54e Mon Sep 17 00:00:00 2001 From: Rett Berg Date: Sat, 16 Nov 2019 12:44:47 -0700 Subject: update comments a bit --- wasm.html.markdown | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'wasm.html.markdown') diff --git a/wasm.html.markdown b/wasm.html.markdown index 92831e69..d62539b8 100644 --- a/wasm.html.markdown +++ b/wasm.html.markdown @@ -223,7 +223,12 @@ contributors: ) (export "apply_cos64" (func $apply_cos64)) - ;; Demonstration of how this C code might be written by hand + ;; Wasm is a stack-based language, but for returning values more complicated + ;; than an int/float, a memory stack has to be manually managed. One + ;; approach is to use a mutable global to store the stack_ptr. We give + ;; ourselves 1MiB of mem-stack and grow it downwards. + ;; + ;; Below is a demonstration of how this C code **might** be written by hand ;; ;; typedef struct { ;; int a; @@ -238,17 +243,11 @@ contributors: ;; sum_struct_t s = sum_struct_create(40, 2); ;; return s.a + s.b; ;; } - ;; - ;; Wasm is a stack-based language, but for returning values more complicated - ;; than an int/float, a memory stack has to be manually managed. One ;; - ;; approach is to use a mutable global to store the stack_ptr. We give - ;; ourselves 1MiB of mem-stack and grow it downwards. - ;; - ;; Note: we are differentiating from the memstack (stack stored in memory) - ;; and the "stack", which wasm implicitly uses to to pass and return values. + + ;; Unlike C, we must manage our own memory stack (global $memstack_ptr (mut i32) (i32.const 65536)) - ;; structs can only be returned by reference + ;; Structs can only be returned by reference (func $sum_struct_create (param $sum_struct_ptr i32) (param $var$a i32) -- cgit v1.2.3 From 0427cb31681114e3d4c2bf664c37d17aa82a7f59 Mon Sep 17 00:00:00 2001 From: Rett Berg Date: Sat, 16 Nov 2019 12:47:32 -0700 Subject: more comment updates --- wasm.html.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'wasm.html.markdown') diff --git a/wasm.html.markdown b/wasm.html.markdown index d62539b8..aba2084f 100644 --- a/wasm.html.markdown +++ b/wasm.html.markdown @@ -224,9 +224,9 @@ contributors: (export "apply_cos64" (func $apply_cos64)) ;; Wasm is a stack-based language, but for returning values more complicated - ;; than an int/float, a memory stack has to be manually managed. One + ;; than an int/float, a separate memory stack has to be manually managed. One ;; approach is to use a mutable global to store the stack_ptr. We give - ;; ourselves 1MiB of mem-stack and grow it downwards. + ;; ourselves 1MiB of memstack and grow it downwards. ;; ;; Below is a demonstration of how this C code **might** be written by hand ;; @@ -244,7 +244,7 @@ contributors: ;; return s.a + s.b; ;; } - ;; Unlike C, we must manage our own memory stack + ;; Unlike C, we must manage our own memory stack. We reserve 1MiB (global $memstack_ptr (mut i32) (i32.const 65536)) ;; Structs can only be returned by reference @@ -270,7 +270,7 @@ contributors: (local $var$sum_struct$b i32) (local $local_memstack_ptr i32) - ;; reserve stack space + ;; reserve memstack space (i32.sub (get_global $memstack_ptr) (i32.const 8) @@ -278,7 +278,7 @@ contributors: tee_local $local_memstack_ptr ;; tee both stores and returns given value set_global $memstack_ptr - ;; call the function, storing the result in the stack + ;; call the function, storing the result in the memstack (call $sum_struct_create ((;$sum_struct_ptr=;) get_local $local_memstack_ptr) ((;$var$a=;) i32.const 40) @@ -293,7 +293,7 @@ contributors: (i32.load offset=4 (get_local $local_memstack_ptr)) ) - ;; unreserve stack space + ;; unreserve memstack space (set_global $memstack_ptr (i32.add (get_local $local_memstack_ptr) -- cgit v1.2.3