return-call-indirect.txt 792 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
;;; TOOL: run-interp
;;; ARGS*: --enable-tail-call
(module
  (type $iii_i (func (param i32 i32 i32)(result i32)))
  (table anyfunc (elem $facInd))

  (func (export "facInd10") (result i32)
    i32.const 10
    i32.const 1
    i32.const 0
    i32.const 0
    call_indirect (type $iii_i))

(;; Tail call version of factorial, using indirect call ;;)
(;; fac(Ix,So) => Ix==0?So:fac(Ix-1,So*Ix) ;;)
  (func $facInd (type $iii_i)
    get_local 0
    i32.const 0
    i32.gt_s
    if (result i32)
      get_local 0
      i32.const 1
      i32.sub
      get_local 1
      get_local 0
      i32.mul
      get_local 2
      get_local 2
      return_call_indirect (type $iii_i)
      unreachable
    else
      get_local 1
      return
    end)
)
(;; STDOUT ;;;
facInd10() => i32:3628800
;;; STDOUT ;;)