dump.scm 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ;;; WebAssembly dumper
  2. ;;; Copyright (C) 2023 Igalia, S.L.
  3. ;;; Copyright (C) 2023 David Thompson <dave@spritely.institute>
  4. ;;;
  5. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  6. ;;; you may not use this file except in compliance with the License.
  7. ;;; You may obtain a copy of the License at
  8. ;;;
  9. ;;; http://www.apache.org/licenses/LICENSE-2.0
  10. ;;;
  11. ;;; Unless required by applicable law or agreed to in writing, software
  12. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  13. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ;;; See the License for the specific language governing permissions and
  15. ;;; limitations under the License.
  16. ;;; Commentary:
  17. ;;;
  18. ;;; Print out details of WebAssembly modules.
  19. ;;;
  20. ;;; Code:
  21. (define-module (wasm dump)
  22. #:use-module (ice-9 format)
  23. #:use-module (ice-9 match)
  24. #:use-module (ice-9 pretty-print)
  25. #:use-module ((srfi srfi-1) #:select (count))
  26. #:use-module (wasm types)
  27. #:export (val-type-repr
  28. type-repr
  29. type-use-repr
  30. dump-wasm))
  31. (define (val-type-repr vt)
  32. (match vt
  33. (($ <ref-type> #t ht)
  34. `(ref null ,ht))
  35. (($ <ref-type> #f ht)
  36. `(ref ,ht))
  37. (_ vt)))
  38. (define (type-repr type)
  39. (define (params-repr params)
  40. (match params
  41. (() '())
  42. ((($ <param> #f type) ...)
  43. `((param ,@(map val-type-repr type))))
  44. ((($ <param> id type) . params)
  45. (cons `(param ,id ,(val-type-repr type))
  46. (params-repr params)))))
  47. (define (results-repr results)
  48. `((result ,@(map val-type-repr results))))
  49. (define (field-repr field)
  50. (define (wrap mutable? repr)
  51. (if mutable? `(mut ,repr) repr))
  52. (match field
  53. (($ <field> id mutable? type)
  54. (let ((repr (wrap mutable? (val-type-repr type))))
  55. (if id
  56. `(field ,id ,repr)
  57. repr)))))
  58. (match type
  59. (($ <func-sig> params results)
  60. `(func ,@(params-repr params) ,@(results-repr results)))
  61. (($ <sub-type> final? supers type)
  62. `(sub ,@(if final? '(final) '()) ,@supers ,(type-repr type)))
  63. (($ <struct-type> fields)
  64. `(struct ,@(map field-repr fields)))
  65. (($ <array-type> mutable? type)
  66. `(array ,(field-repr (make-field #f mutable? type))))))
  67. (define (type-use-repr type)
  68. (match type
  69. (($ <type-use> idx ($ <type> id type))
  70. (type-repr type))
  71. (($ <type-use> idx type)
  72. `(type ,(or idx "error: invalid type use!")))))
  73. (define (table-type-repr type)
  74. (match type
  75. (($ <table-type> ($ <limits> min max) elem-type)
  76. `(,min ,max ,(val-type-repr elem-type)))))
  77. (define (global-type-repr type)
  78. (match type
  79. (($ <global-type> mutable? type)
  80. (let ((t (val-type-repr type)))
  81. (if mutable? `(mut ,t) t)))))
  82. (define (memory-type-repr type)
  83. (match type
  84. (($ <mem-type> ($ <limits> min max))
  85. `(,min ,max))))
  86. (define* (dump-wasm mod #:key (port (current-output-port))
  87. (dump-func-defs? #t))
  88. (define (enumerate f items start)
  89. (let lp ((items items) (idx start))
  90. (match items
  91. (() (values))
  92. ((item . items)
  93. (f item idx)
  94. (lp items (1+ idx))))))
  95. (define* (dump-items header items #:optional (start 0))
  96. (unless (null? items)
  97. (format port "~a:\n" header)
  98. (enumerate (lambda (item idx)
  99. (format port " ~a: ~a\n" idx item))
  100. items start)
  101. (newline port)))
  102. (define (dump-types types)
  103. (define (dump-type type idx indent)
  104. (match type
  105. (($ <type> id type)
  106. (let ((repr (type-repr type)))
  107. (format port "~a~a~@[ (~a)~]: ~a\n" indent idx id repr)))))
  108. (unless (null? types)
  109. (format port "Types:\n")
  110. (let lp ((types types) (idx 0))
  111. (match types
  112. (() (values))
  113. ((($ <rec-group> rec-types) . types)
  114. (format port "Recursive type group:\n")
  115. (enumerate (lambda (type idx)
  116. (dump-type type idx " "))
  117. rec-types idx)
  118. (format port "Recursive type group end.\n")
  119. (lp types (+ idx (length rec-types))))
  120. ((type . types)
  121. (dump-type type idx " ")
  122. (lp types (1+ idx)))))
  123. (newline port)))
  124. (define (dump-imports imports)
  125. (dump-items "Imports"
  126. (map (match-lambda
  127. (($ <import> mod name 'func _ type)
  128. `(import ,mod ,name func ,(type-use-repr type)))
  129. (($ <import> mod name 'table _ type)
  130. `(import ,mod ,name table ,@(table-type-repr type)))
  131. (($ <import> mod name 'global _ type)
  132. `(import ,mod ,name global ,(global-type-repr type)))
  133. (($ <import> mod name 'memory _ type)
  134. `(import ,mod ,name memory ,@(memory-type-repr type))))
  135. imports)))
  136. (define (dump-func-decls funcs imported)
  137. (dump-items "Function declarations"
  138. (map (match-lambda (($ <func> id type locals body)
  139. (type-use-repr type)))
  140. funcs)
  141. imported))
  142. (define (dump-tables tables imported)
  143. (dump-items "Tables"
  144. (map (match-lambda
  145. (($ <table> id type init)
  146. `(table ,id ,@(table-type-repr type))))
  147. tables)
  148. imported))
  149. (define (dump-memories memories imported)
  150. (dump-items "Memories"
  151. (map (match-lambda
  152. (($ <memory> id type)
  153. `(memory ,id ,@(memory-type-repr type)))
  154. (type
  155. `(memory #f ,@(memory-type-repr type))))
  156. memories)))
  157. (define (dump-tags tags)
  158. (dump-items "Tags" tags))
  159. (define (dump-strings strings)
  160. (dump-items "Strings" strings))
  161. (define (dump-globals globals imported)
  162. (dump-items "Globals"
  163. (map (match-lambda
  164. (($ <global> id type init)
  165. (let ((t (val-type-repr type)))
  166. `(global ,(global-type-repr type) ,init))))
  167. globals)
  168. imported))
  169. (define (dump-exports exports)
  170. (dump-items "Exports"
  171. (map (match-lambda
  172. (($ <export> name kind idx)
  173. `(export ,name ,kind ,idx)))
  174. exports)))
  175. (define (dump-start start)
  176. (when start
  177. (format port "Start: #~a\n\n" start)))
  178. (define (dump-elems elems)
  179. (dump-items "Elems"
  180. (map (match-lambda
  181. (($ <elem> id mode table type offset inits)
  182. `(elem id ,mode ,table ,(val-type-repr type)
  183. ,offset ,inits)))
  184. elems)))
  185. (define (dump-data datas)
  186. (dump-items "Datas"
  187. (map (match-lambda
  188. (($ <data> id mode mem offset init)
  189. `(data ,id ,mode ,mem ,offset ,init)))
  190. datas)))
  191. (define (dump-func-defs funcs imported)
  192. (unless (null? funcs)
  193. (format port "Function definitions:\n")
  194. (enumerate
  195. (match-lambda*
  196. ((($ <func> id type locals body) idx)
  197. (format port " Function #~a:\n" idx)
  198. (when id (format port " Id: ~a\n" id))
  199. (format port " Type: ~a\n" (type-use-repr type))
  200. (match locals
  201. (() #t)
  202. ((($ <local> id vt) ...)
  203. (format port " Locals:~:{ ~@[~a:~]~a~}\n"
  204. (map list id (map val-type-repr vt)))))
  205. (format port " Body:\n")
  206. (pretty-print body #:port port #:per-line-prefix " ")))
  207. funcs
  208. imported)))
  209. (match mod
  210. (($ <wasm> id types imports funcs tables memories globals exports start
  211. elems datas tags strings custom)
  212. (define (import-has-kind kind)
  213. (match-lambda
  214. (($ <import> mod name kind' id type) (eq? kind kind'))))
  215. (let ((imported-funcs (count (import-has-kind 'func) imports))
  216. (imported-tables (count (import-has-kind 'table) imports))
  217. (imported-memories (count (import-has-kind 'memory) imports))
  218. (imported-globals (count (import-has-kind 'global) imports)))
  219. (dump-types types)
  220. (dump-imports imports)
  221. (dump-func-decls funcs imported-funcs)
  222. (dump-tables tables imported-tables)
  223. (dump-memories memories imported-memories)
  224. (dump-tags tags)
  225. (dump-strings strings)
  226. (dump-globals globals imported-globals)
  227. (dump-exports exports)
  228. (dump-start start)
  229. (dump-elems elems)
  230. (dump-data datas)
  231. (when dump-func-defs?
  232. (dump-func-defs funcs imported-funcs))))))