3 ms·
WebAssembly code consists of sequences of instructions. Its computational model is based on a stack machine in that instructions manipulate values on an implici
by CalChris 4y ago
WebAssembly code consists of sequences of instructions. Its computational model is based on a stack machine in that instructions manipulate values on an implicit operand stack, consuming (popping) argument values and producing or returning (pushing) result values.
https://webassembly.github.io/spec/core/syntax/instructions.html https://webassembly.github.io/spec/core/syntax/instructions....
(module
(import "console" "log" (func $log (param i32)))
(func $main
;; load `10` and `3` onto the stack
i32.const 10
i32.const 3
i32.add ;; add up both numbers
call $log ;; log the result
)
(start $main)
)
https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Numeric/Addition https://developer.mozilla.org/en-US/docs/WebAssembly/Referen...
Looks pretty stacky to me.
- amelius 4y agoLooks also very lispy.
- deleted 4y ago[deleted]
- Jasper_ 4y agoWebAssembly started as an AST dump, and Lisp-appearing syntax is a simple way of dumping any tree, ASTs included, so the WebAssembly Textual syntax (WAT) adopted it just because it was easy. Then function bodies went from post-encoded tree form to a linear set of instructions managing a stack, which means there's no reason for a tree encoding anymore. So the Lisp-y looking bits are mostly in the metadata structures around the function bodies. You could output a lot of formats in a Lisp-y looking way. The language itself has very little to do with actual Lisp.
- user5678 4y ago
- deleted 4y ago[deleted]
- Someone 4y agoThe article’s argument is that it’s not a stack machine because entry of a block is a barrier for accessing what’s on the stack. (AFAIK, exiting a block also is limited in that it can only return a single item) You can’t do what’s typical in forths: push intermediate values, enter a loop, use and/or change intermediate values, check for end of loop, loop or exit, read intermediate values. An example from http://www.murphywong.net/hello/simple.htm#L15 http://www.murphywong.net/hello/simple.htm#L15): : INTEGERS ( +n -- ) 1 \ Initialize i to 1 ( +n i=1 ) BEGIN \ Start loop: i is TOS ( +n i ) 2DUP \ Duplicate 2 items ( +n i +n i ) < \ Is +n less than i ? ( +n i flag ) IF \ Act on flag ( +n i ) 2DROP \ True: drop 2 items ( ) EXIT \ True: leave word ( ) THEN \ End IF ... THEN ( ) DUP \ DUPlicate TOS ( +n i i ) . \ Display TOS ( +n i ) 1+ \ Increment TOS ( +n i=i+1 ) AGAIN \ Loop back ( +n i ) ; \ End definition Here, both the value of n and the loop counter are on the stack when entering the loop, and the loop removes them before exiting (using 2DROP) Edit: you also can’t take multiple values from the stack in a loop, as in 111 108 108 101 72 5 0 DO EMIT LOOP which, if I didn’t mess things up, prints “Hello”.
- garaetjjte 4y ago>AFAIK, exiting a block also is limited in that it can only return a single item Multi-value returns were added after MVP but they are supported now.