A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
;;! target = "x86_64"
;;! test = "winch"
(module
(func $fibonacci8 (param $n i32) (result i32)
(if (result i32) (i32.le_s (local.get $n) (i32.const 1))
(then
;; If n <= 1, return n (base case)
(local.get $n)
)
(else
;; Else, return fibonacci(n - 1) + fibonacci(n - 2)
(i32.add
(call $fibonacci8
(i32.sub (local.get $n) (i32.const 1)) ;; Calculate n - 1
)
(call $fibonacci8
(i32.sub (local.get $n) (i32.const 2)) ;; Calculate n - 2
)
)
)
)
)
(export "fib" (func $fibonacci8))
)
;; wasm[0]::function[0]::fibonacci8:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
;; movq 0x18(%r11), %r11
;; addq $0x30, %r11
;; cmpq %rsp, %r11
;; ja 0xe0
;; 1c: movq %rdi, %r14
;; subq $0x20, %rsp
;; movq %rdi, 0x18(%rsp)
;; movq %rsi, 0x10(%rsp)
;; movl %edx, 0xc(%rsp)
;; movl 0xc(%rsp), %eax
;; cmpl $1, %eax
;; movl $0, %eax
;; setle %al
;; testl %eax, %eax
;; je 0x54
;; 4b: movl 0xc(%rsp), %eax
;; jmp 0xd7
;; 54: movl 0xc(%rsp), %eax
;; subl $1, %eax
;; subq $4, %rsp
;; movl %eax, (%rsp)
;; subq $0xc, %rsp
;; movq %r14, %rdi
;; movq %r14, %rsi
;; movl 0xc(%rsp), %edx
;; callq 0
;; addq $0x10, %rsp
;; movq 0x18(%rsp), %r14
;; movl 0xc(%rsp), %ecx
;; subl $2, %ecx
;; subq $4, %rsp
;; movl %eax, (%rsp)
;; subq $4, %rsp
;; movl %ecx, (%rsp)
;; subq $8, %rsp
;; movq %r14, %rdi
;; movq %r14, %rsi
;; movl 8(%rsp), %edx
;; callq 0
;; addq $0xc, %rsp
;; movq 0x1c(%rsp), %r14
;; movl (%rsp), %ecx
;; addq $4, %rsp
;; addl %eax, %ecx
;; movl %ecx, %eax
;; addq $0x20, %rsp
;; popq %rbp
;; retq
;; e0: ud2