test-disassemble.scm 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ;;; Copyright (C) 2023 David Thompson <dave@spritely.institute>
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. ;;; Commentary:
  15. ;;;
  16. ;;; Disassembler tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (test utils)
  21. (wasm wat))
  22. (test-begin "test-disassemble")
  23. (test-equal "disassemble"
  24. '(module
  25. (import "math" "cos" (func $cos (param f64) (result f64)))
  26. (export "factorial" (func $factorial))
  27. (global $the-answer i32 (i32.const 42))
  28. (global $mutable (mut i32) (i32.const 0))
  29. (table $objs 10 (ref eq))
  30. (memory $main 1)
  31. (elem $stuff (table $objs) (offset (i32.const 0)) (ref i31)
  32. (item (i32.const 42) (ref.i31)))
  33. (data $bytes $main (i32.const 0) #vu8(1 2 3 4))
  34. (func $factorial (param $n i32) (result i32)
  35. (local $result i32)
  36. (i32.const 1)
  37. (local.set $result)
  38. (loop $loop (result i32)
  39. (local.get $n)
  40. (i32.const 1)
  41. (i32.eq)
  42. (if (result i32)
  43. (then (local.get $result))
  44. (else (local.get $n)
  45. (local.get $result)
  46. (i32.mul)
  47. (local.set $result)
  48. (local.get $n)
  49. (i32.const 1)
  50. (i32.sub)
  51. (local.set $n)
  52. (br $loop)))))
  53. (func $init (i32.const 1337) (global.set $mutable)))
  54. (wasm->wat
  55. (wat->wasm
  56. '(module
  57. (import "math" "cos" (func $cos (param f64) (result f64)))
  58. (global $the-answer i32 (i32.const 42))
  59. (global $mutable (mut i32) (i32.const 0))
  60. (table $objs 10 (ref eq))
  61. (memory $main 1)
  62. (elem $stuff (table $objs) (offset (i32.const 0)) (ref i31)
  63. (item (ref.i31 (i32.const 42))))
  64. (data $bytes (memory $main) (i32.const 0) #vu8(1 2 3 4))
  65. (func $factorial (export "factorial") (param $n i32) (result i32)
  66. (local $result i32)
  67. (local.set $result (i32.const 1))
  68. (loop $loop (result i32)
  69. (if (result i32)
  70. (i32.eq (local.get $n) (i32.const 1))
  71. (then (local.get $result))
  72. (else (local.set $result (i32.mul (local.get $n)
  73. (local.get $result)))
  74. (local.set $n (i32.sub (local.get $n)
  75. (i32.const 1)))
  76. (br $loop)))))
  77. (func $init
  78. (global.set $mutable (i32.const 1337)))))))
  79. (test-end* "test-disassemble")