wasker 0.2.2

Wasm compiler for running Wasm on your favorite kernel
Documentation
;; hello_world.wat

(module

  ;; Import our myprint function
  (import "myenv" "print" (func $print (param i64 i32)))

  (data (i32.const 40) "Test Passed\n")
  (data (i32.const 56) "Test Failed\n")

  ;; Define a single page memory of 64KB.
  (memory $0 1)

  ;; Debug function
  (func $printd (param $len i32)
    i64.const 0
    (local.get $len)
    (call $print)
  )

  (func $printSuccess
    i64.const 40
    i32.const 12
    (call $print)
  )

  (func $printFail
    i64.const 56
    i32.const 12
    (call $print)
  )

  (func $assert_test_i32 (param $expected i32) (param $result i32)
    local.get $expected
    local.get $result
    i32.eq
    (if
      (then
        (call $printSuccess)
      )
      (else
        (call $printFail)
      )
    )
  )

  (func $assert_test_i64 (param $expected i64) (param $result i64)
    local.get $expected
    local.get $result
    i64.eq
    (if
      (then
        (call $printSuccess)
      )
      (else
        (call $printFail)
      )
    )
  )
  (func $assert_test_f32 (param $expected f32) (param $result f32)
    local.get $expected
    local.get $result
    f32.eq
    (if
      (then
        (call $printSuccess)
      )
      (else
        (call $printFail)
      )
    )
  )
  (func $assert_test_f64 (param $expected f64) (param $result f64)
    local.get $expected
    local.get $result
    f64.eq
    (if
      (then
        (call $printSuccess)
      )
      (else
        (call $printFail)
      )
    )
  )
  (func $dummy)
  (func $empty
    (loop)
    (loop $l)
  )
  (func $singular (result i32)
    (loop (nop))
    (loop (result i32) (i32.const 7))
  )
  (func $nested (result i32)
    (loop (result i32)
      (loop (call $dummy) (block) (nop))
      (loop (result i32) (call $dummy) (i32.const 9))
    )
  )
   (func $deep (result i32)
    (loop (result i32) (block (result i32)
      (loop (result i32) (block (result i32)
        (loop (result i32) (block (result i32)
          (loop (result i32) (block (result i32)
            (loop (result i32) (block (result i32)
              (loop (result i32) (block (result i32)
                (loop (result i32) (block (result i32)
                  (loop (result i32) (block (result i32)
                    (loop (result i32) (block (result i32)
                      (loop (result i32) (block (result i32)
                        (loop (result i32) (block (result i32)
                          (loop (result i32) (block (result i32)
                            (loop (result i32) (block (result i32)
                              (loop (result i32) (block (result i32)
                                (loop (result i32) (block (result i32)
                                  (loop (result i32) (block (result i32)
                                    (loop (result i32) (block (result i32)
                                      (loop (result i32) (block (result i32)
                                        (loop (result i32) (block (result i32)
                                          (loop (result i32) (block (result i32)
                                            (call $dummy) (i32.const 150)
                                          ))
                                        ))
                                      ))
                                    ))
                                  ))
                                ))
                              ))
                            ))
                          ))
                        ))
                      ))
                    ))
                  ))
                ))
              ))
            ))
          ))
        ))
      ))
    ))
  )

  (func $as-select-first (result i32)
    (select (loop (result i32) (i32.const 1)) (i32.const 2) (i32.const 3))
  )
  (func $as-select-mid (result i32)
    (select (i32.const 2) (loop (result i32) (i32.const 1)) (i32.const 3))
  )
  (func $as-select-last (result i32)
    (select (i32.const 2) (i32.const 3) (loop (result i32) (i32.const 1)))
  )

  (func $as-if-condition
    (loop (result i32) (i32.const 1)) (if (then (call $dummy)))
  )
  (func $as-if-then (result i32)
    (if (result i32) (i32.const 1) (then (loop (result i32) (i32.const 1))) (else (i32.const 2)))
  )
  (func $as-if-else (result i32)
    (if (result i32) (i32.const 1) (then (i32.const 2)) (else (loop (result i32) (i32.const 1))))
  )

  (func $as-br_if-first (result i32)
    (block (result i32) (br_if 0 (loop (result i32) (i32.const 1)) (i32.const 2)))
  )
  (func $as-br_if-last (result i32)
    (block (result i32) (br_if 0 (i32.const 2) (loop (result i32) (i32.const 1))))
  )

  (func $as-br_table-first (result i32)
    (block (result i32) (loop (result i32) (i32.const 1)) (i32.const 2) (br_table 0 0))
  )
  (func $as-br_table-last (result i32)
    (block (result i32) (i32.const 2) (loop (result i32) (i32.const 1)) (br_table 0 0))
  )
  (func $func (param i32 i32) (result i32) (local.get 0))
  (type $check (func (param i32 i32) (result i32)))
  (table funcref (elem $func))
  (func $as-call_indirect-first (result i32)
    (block (result i32)
      (call_indirect (type $check)
        (loop (result i32) (i32.const 1)) (i32.const 2) (i32.const 0)
      )
    )
  )
  (func $as-call_indirect-mid (result i32)
    (block (result i32)
      (call_indirect (type $check)
        (i32.const 2) (loop (result i32) (i32.const 1)) (i32.const 0)
      )
    )
  )
  (func $as-call_indirect-last (result i32)
    (block (result i32)
      (call_indirect (type $check)
        (i32.const 1) (i32.const 2) (loop (result i32) (i32.const 0))
      )
    )
  )

  (func $as-store-first
    (loop (result i32) (i32.const 1)) (i32.const 1) (i32.store)
  )
  (func $as-store-last
    (i32.const 10) (loop (result i32) (i32.const 1)) (i32.store)
  )

  (func $as-memory.grow-value (result i32)
    (memory.grow (loop (result i32) (i32.const 1)))
  )

  (func $f (param i32) (result i32) (local.get 0))

  (func $as-call-value (result i32)
    (call $f (loop (result i32) (i32.const 1)))
  )
  (func $as-return-value (result i32)
    (loop (result i32) (i32.const 1)) (return)
  )
  (func $as-drop-operand
    (drop (loop (result i32) (i32.const 1)))
  )
  (func $as-br-value (result i32)
    (block (result i32) (br 0 (loop (result i32) (i32.const 1))))
  )
  (func $as-local.set-value (result i32)
    (local i32) (local.set 0 (loop (result i32) (i32.const 1))) (local.get 0)
  )
  (func $as-local.tee-value (result i32)
    (local i32) (local.tee 0 (loop (result i32) (i32.const 1)))
  )
  (global $a (mut i32) (i32.const 0))
  (func $as-global.set-value (result i32)
    (global.set $a (loop (result i32) (i32.const 1)))
    (global.get $a)
  )
  (func $as-load-operand (result i32)
    (i32.load (loop (result i32) (i32.const 1)))
  )

  (func $as-unary-operand (result i32)
    (i32.ctz (loop (result i32) (call $dummy) (i32.const 13)))
  )
  (func $as-binary-operand (result i32)
    (i32.mul
      (loop (result i32) (call $dummy) (i32.const 3))
      (loop (result i32) (call $dummy) (i32.const 4))
    )
  )
  (func $as-test-operand (result i32)
    (i32.eqz (loop (result i32) (call $dummy) (i32.const 13)))
  )
  (func $break-bare (result i32)
    (block (loop (br 1) (br 0) (unreachable)))
    (block (loop (br_if 1 (i32.const 1)) (unreachable)))
    (block (loop (br_table 1 (i32.const 0)) (unreachable)))
    (block (loop (br_table 1 1 1 (i32.const 1)) (unreachable)))
    (i32.const 19)
  )
  (func $break-repeated (result i32)
    (block (result i32)
      (loop (result i32)
        (br 1 (i32.const 18))
        (br 1 (i32.const 19))
        (drop (br_if 1 (i32.const 20) (i32.const 0)))
        (drop (br_if 1 (i32.const 20) (i32.const 1)))
        (br 1 (i32.const 21))
        (br_table 1 (i32.const 22) (i32.const 0))
        (br_table 1 1 1 (i32.const 23) (i32.const 1))
        (i32.const 21)
      )
    )
  )
  (func $break-inner (result i32)
    (local i32)
    (local.set 0 (i32.const 0))
    (local.set 0 (i32.add (local.get 0) (block (result i32) (loop (result i32) (block (result i32) (br 2 (i32.const 0x1)))))))
    (local.set 0 (i32.add (local.get 0) (block (result i32) (loop (result i32) (loop (result i32) (br 2 (i32.const 0x2)))))))
    (local.set 0 (i32.add (local.get 0) (block (result i32) (loop (result i32) (block (result i32) (loop (result i32) (br 1 (i32.const 0x4))))))))
    (local.set 0 (i32.add (local.get 0) (block (result i32) (loop (result i32) (i32.ctz (br 1 (i32.const 0x8)))))))
    (local.set 0 (i32.add (local.get 0) (block (result i32) (loop (result i32) (i32.ctz (loop (result i32) (br 2 (i32.const 0x10))))))))
    (local.get 0)
  )
  (func $cont-inner (result i32)
    (local i32)
    (local.set 0 (i32.const 0))
    (local.set 0 (i32.add (local.get 0) (loop (result i32) (loop (result i32) (br 1)))))
    (local.set 0 (i32.add (local.get 0) (loop (result i32) (i32.ctz (br 0)))))
    (local.set 0 (i32.add (local.get 0) (loop (result i32) (i32.ctz (loop (result i32) (br 1))))))
    (local.get 0)
  )
  (func $effects (result i32)
    (local i32)
    (block
      (loop
        (local.set 0 (i32.const 1))
        (local.set 0 (i32.mul (local.get 0) (i32.const 3)))
        (local.set 0 (i32.sub (local.get 0) (i32.const 5)))
        (local.set 0 (i32.mul (local.get 0) (i32.const 7)))
        (br 1)
        (local.set 0 (i32.mul (local.get 0) (i32.const 100)))
      )
    )
    (i32.eq (local.get 0) (i32.const -14))
  )
  (func $while (param i64) (result i64)
    (local i64)
    (local.set 1 (i64.const 1))
    (block
      (loop
        (br_if 1 (i64.eqz (local.get 0)))
        (local.set 1 (i64.mul (local.get 0) (local.get 1)))
        (local.set 0 (i64.sub (local.get 0) (i64.const 1)))
        (br 0)
      )
    )
    (local.get 1)
  )
  (func $for (param i64) (result i64)
    (local i64 i64)
    (local.set 1 (i64.const 1))
    (local.set 2 (i64.const 2))
    (block
      (loop
        (br_if 1 (i64.gt_u (local.get 2) (local.get 0)))
        (local.set 1 (i64.mul (local.get 1) (local.get 2)))
        (local.set 2 (i64.add (local.get 2) (i64.const 1)))
        (br 0)
      )
    )
    (local.get 1)
  )

  ;; Entrypoint
	(func (export "_start")
    (call $empty)
    (call $assert_test_i32 (call $singular) (i32.const 7))
    (call $assert_test_i32 (call $nested) (i32.const 9))
    (call $assert_test_i32 (call $deep) (i32.const 150))
    (call $assert_test_i32 (call $as-select-first) (i32.const 1))
    (call $assert_test_i32 (call $as-select-mid) (i32.const 2))
    (call $assert_test_i32 (call $as-select-last) (i32.const 2))
    (call $as-if-condition)
    (call $assert_test_i32 (call $as-if-then) (i32.const 1))
    (call $assert_test_i32 (call $as-if-else) (i32.const 2))
    (call $assert_test_i32 (call $as-br_if-first) (i32.const 1))
    (call $assert_test_i32 (call $as-br_if-last) (i32.const 2))
    (call $assert_test_i32 (call $as-br_table-first) (i32.const 1))
    (call $assert_test_i32 (call $as-br_table-last) (i32.const 2))
    (call $assert_test_i32 (call $as-call_indirect-first) (i32.const 1))
    (call $assert_test_i32 (call $as-call_indirect-mid) (i32.const 2))
    (call $assert_test_i32 (call $as-call_indirect-last) (i32.const 1))
    (call $as-store-first)
    (call $as-store-last)
    (call $assert_test_i32 (call $as-memory.grow-value) (i32.const 1))
    (call $assert_test_i32 (call $as-call-value) (i32.const 1))
    (call $assert_test_i32 (call $as-return-value) (i32.const 1))
    (call $as-drop-operand)
    (call $assert_test_i32 (call $as-br-value) (i32.const 1))
    (call $assert_test_i32 (call $as-local.set-value) (i32.const 1))
    (call $assert_test_i32 (call $as-local.tee-value) (i32.const 1))
    (call $assert_test_i32 (call $as-global.set-value) (i32.const 1))
    (call $assert_test_i32 (call $as-load-operand) (i32.const 1))
    (call $assert_test_i32 (call $as-unary-operand) (i32.const 0))
    (call $assert_test_i32 (call $as-binary-operand) (i32.const 12))
    (call $assert_test_i32 (call $as-test-operand) (i32.const 0))
    (call $assert_test_i32 (call $break-bare) (i32.const 19))
    (call $assert_test_i32 (call $break-repeated) (i32.const 18))
    (call $assert_test_i32 (call $break-inner) (i32.const 0x1f))
    (call $assert_test_i32 (call $effects) (i32.const 1))
    (call $assert_test_i64 (call $while (i64.const 0)) (i64.const 1))
    (call $assert_test_i64 (call $while (i64.const 1)) (i64.const 1))
    (call $assert_test_i64 (call $while (i64.const 2)) (i64.const 2))
    (call $assert_test_i64 (call $while (i64.const 3)) (i64.const 6))
    (call $assert_test_i64 (call $while (i64.const 5)) (i64.const 120))
    (call $assert_test_i64 (call $while (i64.const 20)) (i64.const 2432902008176640000))
    (call $assert_test_i64 (call $for (i64.const 0)) (i64.const 1))
    (call $assert_test_i64 (call $for (i64.const 1)) (i64.const 1))
    (call $assert_test_i64 (call $for (i64.const 2)) (i64.const 2))
    (call $assert_test_i64 (call $for (i64.const 3)) (i64.const 6))
    (call $assert_test_i64 (call $for (i64.const 5)) (i64.const 120))
    (call $assert_test_i64 (call $for (i64.const 20)) (i64.const 2432902008176640000))
	)
)