Skip to content

Instantly share code, notes, and snippets.

@sliminality
Created May 25, 2018 22:13
Show Gist options
  • Save sliminality/9eaed7137e62b490e0b554c7355bc9e4 to your computer and use it in GitHub Desktop.
Save sliminality/9eaed7137e62b490e0b554c7355bc9e4 to your computer and use it in GitHub Desktop.
Breaking down the wast format
#[no_mangle]
pub extern "C" fn double_subtract5_add1(x: i32) -> i32 {
let result = double(x) + negate(5) + 1;
return result;
}
#[no_mangle]
pub fn double(x: i32) -> i32 {
return x * 2;
}
#[no_mangle]
pub fn negate(x: i32) -> i32 {
return -1 * x;
}
;; https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#known-sections
(module
;; Types section
(type $0 (func (param i32) (result i32)))
(type $1 (func (param i32 i32) (result i32)))
(type $2 (func (param i32 i32 i32)))
;; The rest are for Rust stdlib functions (omitted).
(type $3 (func (param i32 i32 i32 i32) (result i32)))
;; ...
;; Looks like this is used in allocation.
(global $global$0 (mut i32) (i32.const 1156384))
;; Table section?
(table 259 259 anyfunc)
;; Linear memory section?
(memory $0 18)
;; Omitting some statically-allocated Rust stdlib stuff...
;; ...
(data (i32.const 107756) "\01\00\00\00\00\00\00\00g\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00h\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00i\00\00\00\00\00\00\00\00")
;; Export section
(export "memory" (memory $0))
(export "double_subtract5_add1" (func $double_subtract5_add1))
(export "double" (func $double))
(export "negate" (func $negate))
;; Function section
;; (x: i32) -> i32
;; Computes `double(x) + negate(5) + 1`.
(func $double_subtract5_add1 (; 0 ;) (type $0) (param $var$0 i32) (result i32)
(i32.add ; (+
(i32.add ; (+
(call $double ; (double x)
(get_local $var$0)
)
(call $negate ; (negate 5))
(i32.const 5)
)
)
(i32.const 1) ; 1)
)
)
;; (x: i32) -> i32
;; Returns `x * 2`.
(func $double (; 1 ;) (type $0) (param $var$0 i32) (result i32)
(i32.shl ; shift left once
(get_local $var$0)
(i32.const 1)
)
)
;; (x: i32) -> i32
;; Computes `x * -1`.
(func $negate (; 2 ;) (type $0) (param $var$0 i32) (result i32)
(i32.sub
(i32.const 0)
(get_local $var$0)
)
)
;; Skipping a rather large # of Rust stdlib functions here...
;; All of this variety:
(func $__rust_alloc (; 3 ;) (type $1) (param $var$0 i32) (param $var$1 i32) (result i32)
(call $__rdl_alloc
(get_local $var$0)
(get_local $var$1)
)
)
;; ...
;; custom section "linking", size 5
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment