resolve.scm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. ;;; WebAssembly resolver
  2. ;;; Copyright (C) 2023 Igalia, S.L.
  3. ;;; Copyright (C) 2023 Robin Templeton <robin@spritely.institute>
  4. ;;; Copyright (C) 2023 David Thompson <dave@spritely.institute>
  5. ;;;
  6. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  7. ;;; you may not use this file except in compliance with the License.
  8. ;;; You may obtain a copy of the License at
  9. ;;;
  10. ;;; http://www.apache.org/licenses/LICENSE-2.0
  11. ;;;
  12. ;;; Unless required by applicable law or agreed to in writing, software
  13. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  14. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. ;;; See the License for the specific language governing permissions and
  16. ;;; limitations under the License.
  17. ;;; Commentary:
  18. ;;;
  19. ;;; Lowers WASM with human readable identifiers to WASM with only
  20. ;;; index references.
  21. ;;;
  22. ;;; Code:
  23. (define-module (wasm resolve)
  24. #:use-module (ice-9 match)
  25. #:use-module ((srfi srfi-1) #:select (list-index))
  26. #:use-module (srfi srfi-11)
  27. #:use-module (wasm types)
  28. #:export (resolve-wasm))
  29. (define (fold1 f l s0)
  30. (let lp ((l l) (s0 s0))
  31. (match l
  32. (() s0)
  33. ((elt . l) (lp l (f elt s0))))))
  34. (define (alist-sort alist)
  35. (sort alist (lambda (a b) (< (car a) (car b)))))
  36. (define (make-name-store)
  37. (let ((count 0)
  38. (ids (make-hash-table)))
  39. (values (lambda (id)
  40. (let ((idx count))
  41. (set! count (1+ count))
  42. (when id (hashq-set! ids id idx))
  43. idx))
  44. (lambda (id)
  45. (cond
  46. ((exact-integer? id) id)
  47. ((hashq-ref ids id))
  48. (else (error "unbound identifier" id))))
  49. (lambda ()
  50. (alist-sort
  51. (hash-fold (lambda (name idx result)
  52. (cons (cons idx name) result))
  53. '() ids))))))
  54. (define (make-indirect-name-store)
  55. (let ((table (make-hash-table)))
  56. (values (lambda (parent-id parent-idx id)
  57. (match (hashq-ref table parent-idx)
  58. (#f
  59. (let-values (((add-id! resolve-id name-map) (make-name-store)))
  60. (let ((procs (list add-id! resolve-id name-map)))
  61. (hashq-set! table parent-idx procs)
  62. (when parent-id
  63. (hashq-set! table parent-id procs)))
  64. (add-id! id)))
  65. ((add-id! resolve-id name-map)
  66. (add-id! id))))
  67. (lambda (parent-id-or-idx id-or-idx)
  68. (if (exact-integer? id-or-idx)
  69. id-or-idx
  70. (match (hashq-ref table parent-id-or-idx)
  71. ((add-id! resolve-id name-map)
  72. (resolve-id id-or-idx)))))
  73. (lambda ()
  74. (alist-sort
  75. (hash-fold
  76. (lambda (id-or-idx procs result)
  77. (if (exact-integer? id-or-idx)
  78. (match procs
  79. ((_ _ name-map)
  80. (match (name-map)
  81. ((name-map ..1) (cons (cons id-or-idx name-map) result))
  82. (_ result))))
  83. result))
  84. '()
  85. table))))))
  86. (define* (resolve-wasm mod #:key emit-names?)
  87. (define-values (add-type-id! resolve-type type-name-map) (make-name-store))
  88. (define-values (add-func-id! resolve-func func-name-map) (make-name-store))
  89. (define-values (add-table-id! resolve-table table-name-map) (make-name-store))
  90. (define-values (add-memory-id! resolve-memory memory-name-map) (make-name-store))
  91. (define-values (add-global-id! resolve-global global-name-map) (make-name-store))
  92. (define-values (add-elem-id! resolve-elem elem-name-map) (make-name-store))
  93. (define-values (add-data-id! resolve-data data-name-map) (make-name-store))
  94. (define-values (add-tag-id! resolve-tag tag-name-map) (make-name-store))
  95. (define-values (add-struct-field! resolve-struct-field struct-field-name-map)
  96. (make-indirect-name-store))
  97. (define-values (add-func-local! resolve-func-local func-local-name-map)
  98. (make-indirect-name-store))
  99. (define-values (add-func-label! resolve-func-label func-label-name-map)
  100. (make-indirect-name-store))
  101. (define (add-func-locals! func)
  102. (match func
  103. (($ <func> id ($ <type-use> _ type) locals)
  104. (let ((idx (resolve-func id)))
  105. (for-each (lambda (local-id)
  106. (add-func-local! id idx local-id))
  107. (append (map param-id (func-sig-params type))
  108. (map local-id locals)))))))
  109. (define (add-func-labels! func)
  110. (match func
  111. (($ <func> id _ _ body)
  112. (let ((idx (resolve-func id)))
  113. (let loop ((insts body))
  114. (match insts
  115. (() #t)
  116. ((((or 'block 'loop) label _ body) . rest)
  117. (add-func-label! id idx label)
  118. (loop body)
  119. (loop rest))
  120. ((('if label _ consequent alternate) . rest)
  121. (add-func-label! id idx label)
  122. (loop consequent)
  123. (loop alternate)
  124. (loop rest))
  125. ((_ . rest)
  126. (loop rest))))))))
  127. (define (resolve-memarg memarg)
  128. (match memarg
  129. (($ <mem-arg> id offset align)
  130. (make-mem-arg (resolve-memory id) offset align))))
  131. (define interned-strings (make-hash-table))
  132. (define interned-string-count 0)
  133. (define (intern-string string)
  134. (or (hash-ref interned-strings string)
  135. (let ((idx interned-string-count))
  136. (hash-set! interned-strings string idx)
  137. (set! interned-string-count (1+ idx))
  138. idx)))
  139. (define functions-used-as-values (make-hash-table))
  140. (define (record-function-used-as-value idx)
  141. (unless (exact-integer? idx) (error "expected resolved idx"))
  142. (hashv-set! functions-used-as-values idx #t)
  143. idx)
  144. (define (type-use-matcher params results)
  145. (define param-type (match-lambda (($ <param> id type) type)))
  146. (lambda (rec type-id type-idx supers type)
  147. (and (null? supers)
  148. (match type
  149. (($ <func-sig> params' results')
  150. (and (equal? (map param-type params)
  151. (map param-type params'))
  152. (equal? results results')
  153. (make-type-use type-idx (make-func-sig params results))))
  154. (_ #f)))))
  155. (define (adjoin-types-from-type-uses types funcs imports tags)
  156. (define (adjoin-type-use type types)
  157. (match type
  158. (($ <type-use> #f ($ <func-sig> params results))
  159. (if (find-type (type-use-matcher params results) types)
  160. types
  161. (append types
  162. (list (make-type #f (make-func-sig params results))))))
  163. (($ <type-use>) types)))
  164. (define (adjoin-type-uses-from-import import types)
  165. (match import
  166. (($ <import> mod name 'func id type)
  167. (adjoin-type-use type types))
  168. (($ <import>) types)))
  169. (define (adjoin-type-uses-from-tag tag types)
  170. (match tag
  171. (($ <tag> id type) (adjoin-type-use type types))))
  172. (define (adjoin-type-uses-from-func func types)
  173. (define (adjoin-type-use-for-block-type x types)
  174. (match x
  175. (($ <type-use> #f ($ <func-sig> () (or () (_))))
  176. types)
  177. (_ (adjoin-type-use x types))))
  178. (define (adjoin-type-uses-for-inst inst types)
  179. (match inst
  180. (((or 'block 'loop) label type body)
  181. (fold1 adjoin-type-uses-for-inst body
  182. (adjoin-type-use-for-block-type type types)))
  183. (('if label type consequent alternate)
  184. (adjoin-type-uses-from-body
  185. consequent
  186. (adjoin-type-uses-from-body
  187. alternate
  188. (adjoin-type-use-for-block-type type types))))
  189. (('try label type body catches catch-all)
  190. (fold1 adjoin-type-uses-from-body (append body catches)
  191. (adjoin-type-use-for-block-type
  192. type
  193. (if catch-all
  194. (adjoin-type-uses-from-body catch-all types)
  195. types))))
  196. (('try_delegate label type body handler)
  197. (adjoin-type-uses-from-body
  198. body
  199. (adjoin-type-use-for-block-type type types)))
  200. (((or 'call_indirect 'return_call_indirect) table type)
  201. (adjoin-type-use type types))
  202. (_ types)))
  203. (define (adjoin-type-uses-from-body insts types)
  204. (fold1 adjoin-type-uses-for-inst insts types))
  205. (match func
  206. (($ <func> id type locals body)
  207. (adjoin-type-uses-from-body body (adjoin-type-use type types)))))
  208. (fold1 adjoin-type-uses-from-func funcs
  209. (fold1 adjoin-type-uses-from-tag tags
  210. (fold1 adjoin-type-uses-from-import imports types))))
  211. (match mod
  212. (($ <wasm> id %types imports funcs tables memories globals exports start
  213. elems datas tags strings custom)
  214. (define (generate-names)
  215. (make-names id
  216. (func-name-map)
  217. (func-local-name-map)
  218. (func-label-name-map)
  219. (type-name-map)
  220. (table-name-map)
  221. (memory-name-map)
  222. (global-name-map)
  223. (elem-name-map)
  224. (data-name-map)
  225. (struct-field-name-map)
  226. (tag-name-map)))
  227. (define types (adjoin-types-from-type-uses %types funcs imports tags))
  228. (for-each (match-lambda (($ <type> id type) (add-type-id! id))
  229. (($ <rec-group> (($ <type> id type) ...))
  230. (for-each add-type-id! id)))
  231. types)
  232. (for-each (match-lambda (($ <import> mod name kind id type)
  233. (match kind
  234. ('func (add-func-id! id))
  235. ('global (add-global-id! id))
  236. ('table (add-table-id! id))
  237. ('memory (add-memory-id! id)))))
  238. imports)
  239. (for-each (match-lambda (($ <func> id type locals body)
  240. (add-func-id! id)))
  241. funcs)
  242. (for-each (match-lambda (($ <table> id type init)
  243. (add-table-id! id)))
  244. tables)
  245. (for-each (match-lambda (($ <memory> id type)
  246. (add-memory-id! id)))
  247. memories)
  248. (for-each (match-lambda (($ <global> id type init)
  249. (add-global-id! id)))
  250. globals)
  251. (for-each (match-lambda (($ <elem> id mode table type offset init)
  252. (add-elem-id! id)))
  253. elems)
  254. (for-each (match-lambda (($ <data> id mode mem offset init)
  255. (add-data-id! id)))
  256. datas)
  257. (for-each (match-lambda (($ <tag> id type)
  258. (add-tag-id! id)))
  259. tags)
  260. (for-each intern-string strings)
  261. (find-type (lambda (rec type-id type-idx supers type)
  262. (match type
  263. (($ <struct-type>
  264. (($ <field> field-id mutable? type) ...))
  265. (for-each
  266. (lambda (field-id)
  267. (add-struct-field! type-id type-idx field-id))
  268. field-id))
  269. (_ (values)))
  270. #f)
  271. types)
  272. (when emit-names?
  273. (for-each add-func-locals! funcs)
  274. (for-each add-func-labels! funcs))
  275. (define (type-by-idx idx)
  276. (or (find-type (lambda (rec type-id type-idx supers type)
  277. (and (eqv? type-idx idx)
  278. type))
  279. types)
  280. (error "unknown type" idx)))
  281. (define (resolve-heap-type ht)
  282. (match ht
  283. ((or 'func 'extern
  284. 'any 'eq 'i31 'noextern 'nofunc 'struct 'array 'none
  285. 'string 'stringview_wtf8 'stringview_wtf16 'stringview_iter)
  286. ht)
  287. (_ (resolve-type ht))))
  288. (define (resolve-val-type vt)
  289. (match vt
  290. ((or 'i32 'i64 'f32 'f64 'v128
  291. 'funcref 'externref 'anyref 'eqref 'i31ref
  292. 'nullexternref 'nullfuncref
  293. 'structref 'arrayref 'nullref
  294. 'stringref
  295. 'stringview_wtf8ref 'stringview_wtf16ref 'stringview_iterref)
  296. vt)
  297. (($ <ref-type> nullable? ht)
  298. (make-ref-type nullable? (resolve-heap-type ht)))))
  299. (define (resolve-ref-type rt)
  300. (resolve-val-type rt))
  301. (define (resolve-storage-type type)
  302. (match type
  303. ((or 'i8 'i16) type)
  304. (_ (resolve-val-type type))))
  305. (define (resolve-param param)
  306. (match param
  307. (($ <param> id type)
  308. (make-param id (resolve-val-type type)))))
  309. (define (resolve-type-use x)
  310. ;; Transform symbolic or anonymous type uses to indexed type
  311. ;; uses.
  312. (define (lookup-type-use params results)
  313. (or (find-type (type-use-matcher params results) types)
  314. (error "unreachable")))
  315. (match x
  316. (($ <type-use> idx (and use-sig ($ <func-sig> params results)))
  317. (if idx
  318. (let ((idx (resolve-type idx)))
  319. (let ((def-sig (type-by-idx idx)))
  320. (make-type-use idx
  321. (if (and (null? params) (null? results))
  322. def-sig
  323. use-sig))))
  324. (match (lookup-type-use params results)
  325. (($ <type-use> idx ($ <func-sig> params results))
  326. (let ((params (map resolve-param params))
  327. (results (map resolve-val-type results)))
  328. (make-type-use idx (make-func-sig params results)))))))))
  329. (define (resolve-type-use-as-idx x)
  330. (match (resolve-type-use x)
  331. (($ <type-use> idx func-sig)
  332. idx)))
  333. (define (resolve-block-type x)
  334. (match x
  335. (($ <type-use> #f ($ <func-sig> () ()))
  336. x)
  337. (($ <type-use> #f ($ <func-sig> () (ret)))
  338. (let ((ret (resolve-val-type ret)))
  339. (make-type-use #f (make-func-sig '() (list ret)))))
  340. (_ (resolve-type-use-as-idx x))))
  341. (define (resolve-instructions insts locals labels)
  342. (define (resolve-i32 x)
  343. (if (< x (ash 1 31)) x (- x (ash 1 32))))
  344. (define (resolve-i64 x)
  345. (if (< x (ash 1 63)) x (- x (ash 1 64))))
  346. (define (resolve-label label)
  347. (match label
  348. ((? exact-integer?) label)
  349. (_
  350. (or (list-index (lambda (x) (eqv? x label)) labels)
  351. (error "unbound label" label labels)))))
  352. (define (resolve-local id)
  353. (match id
  354. ((? exact-integer?) id)
  355. (_
  356. (let ((local (list-index
  357. (lambda (local)
  358. (match local
  359. (($ <local> id* _) (eqv? id id*))
  360. (($ <param> id* _) (eqv? id id*))
  361. (_ #f)))
  362. locals)))
  363. (unless local
  364. (error "unbound local" id locals))
  365. local))))
  366. (map
  367. (match-lambda
  368. (((and inst (or 'block 'loop)) label type body)
  369. (let ((labels (cons label labels)))
  370. `(,inst ,label ,(resolve-block-type type)
  371. ,(resolve-instructions body locals labels))))
  372. (('if label type consequent alternate)
  373. (let ((labels (cons label labels)))
  374. `(if ,label ,(resolve-block-type type)
  375. ,(resolve-instructions consequent locals labels)
  376. ,(resolve-instructions alternate locals labels))))
  377. (('try label type body catches catch-all)
  378. (let ((labels (cons label labels)))
  379. `(try ,label ,(resolve-block-type type)
  380. ,(resolve-instructions body locals labels)
  381. ,(map (lambda (body)
  382. (resolve-instructions body locals labels))
  383. catches)
  384. ,(and catch-all
  385. (resolve-instructions catch-all locals labels)))))
  386. (('try_delegate label type body handler)
  387. (let ((labels (cons label labels)))
  388. `(try_delegate ,label ,(resolve-block-type type)
  389. ,(resolve-instructions body locals labels)
  390. ,(resolve-label handler))))
  391. (((and inst (or 'throw 'rethrow)) tag) `(,inst ,(resolve-tag tag)))
  392. (((and inst (or 'br 'br_if)) label)
  393. `(,inst ,(resolve-label label)))
  394. (('br_table targets default)
  395. `(br_table ,(map resolve-label targets) ,(resolve-label default)))
  396. (((and inst (or 'call 'return_call)) label)
  397. `(,inst ,(resolve-func label)))
  398. (('call_indirect table type)
  399. `(call_indirect ,(resolve-table table) ,(resolve-type-use-as-idx type)))
  400. (((and inst (or 'call_ref 'return_call_ref)) type)
  401. `(,inst ,(resolve-type type)))
  402. (('select types) `(select ,(map resolve-val-type types)))
  403. (((and inst (or 'local.get 'local.set 'local.tee)) local)
  404. `(,inst ,(resolve-local local)))
  405. (((and inst (or 'global.get 'global.set)) global)
  406. `(,inst ,(resolve-global global)))
  407. (((and inst (or 'table.get 'table.set)) table)
  408. `(,inst ,(resolve-table table)))
  409. (((and inst (or 'memory.size 'memory.grow)) mem)
  410. `(,inst ,(resolve-memory mem)))
  411. (((and inst (or 'i32.load 'i64.load 'f32.load 'f64.load
  412. 'i32.load8_s 'i32.load8_u 'i32.load16_s 'i32.load16_u
  413. 'i64.load8_s 'i64.load8_u 'i64.load16_s 'i64.load16_u
  414. 'i64.load32_s 'i64.load32_u
  415. 'i32.store 'i64.store 'f32.store 'f64.store
  416. 'i32.store8 'i32.store16
  417. 'i64.store8 'i64.store16 'i64.store32))
  418. mem)
  419. `(,inst ,(resolve-memarg mem)))
  420. (('i32.const x) `(i32.const ,(resolve-i32 x)))
  421. (('i64.const x) `(i64.const ,(resolve-i64 x)))
  422. (('ref.null ht) `(ref.null ,(resolve-heap-type ht)))
  423. (('ref.func f) `(ref.func ,(record-function-used-as-value
  424. (resolve-func f))))
  425. ;; GC instructions.
  426. (((and inst (or 'ref.test 'ref.cast)) rt)
  427. `(,inst ,(resolve-ref-type rt)))
  428. (((and inst (or 'br_on_cast 'br_on_cast_fail)) label rt1 rt2)
  429. `(,inst ,(resolve-label label)
  430. ,(resolve-ref-type rt1) ,(resolve-ref-type rt2)))
  431. (((and inst (or 'struct.get 'struct.get_s 'struct.get_u 'struct.set))
  432. type field)
  433. `(,inst ,(resolve-type type) ,(resolve-struct-field type field)))
  434. (((and inst (or 'struct.new 'struct.new_default)) type)
  435. `(,inst ,(resolve-type type)))
  436. (((and inst (or 'array.get 'array.get_s 'array.get_u 'array.set)) type)
  437. `(,inst ,(resolve-type type)))
  438. (('array.new_fixed type len)
  439. `(array.new_fixed ,(resolve-type type) ,len))
  440. (((and inst (or 'array.new 'array.new_default)) type)
  441. `(,inst ,(resolve-type type)))
  442. (((and inst (or 'array.new_data 'array.init_data)) type data)
  443. `(,inst ,(resolve-type type) ,(resolve-data data)))
  444. (((and inst (or 'array.new_elem 'array.init_elem)) type elem)
  445. `(,inst ,(resolve-type type) ,(resolve-elem elem)))
  446. (('array.fill type)
  447. `(array.fill ,(resolve-type type)))
  448. (('array.copy dst src)
  449. `(array.copy ,(resolve-type dst) ,(resolve-type src)))
  450. ;; Stringref instructions.
  451. (('string.const (? string? str))
  452. `(string.const ,(intern-string str)))
  453. (((and inst (or 'string.new_utf8 'string.new_lossy_utf8 'string.new_wtf8
  454. 'string.new_wtf16
  455. 'string.encode_utf8 'string.encode_lossy_utf8
  456. 'string.encode_wtf8 'string.encode_wtf16
  457. 'stringview_wtf8.encode_utf8
  458. 'stringview_wtf8.encode_lossy_utf8
  459. 'stringview_wtf8.encode_wtf8
  460. 'stringview_wtf16.encode))
  461. mem)
  462. `(,inst ,(resolve-memarg mem)))
  463. ;; Misc instructions.
  464. (('memory.init data mem)
  465. `(memory.init ,(resolve-data data) ,(resolve-memory mem)))
  466. (('data.drop data)
  467. `(data.drop ,(resolve-data data)))
  468. (('memory.copy dst src)
  469. `(memory.copy ,(resolve-memory dst) ,(resolve-memory src)))
  470. (('memory.fill mem)
  471. `(memory.fill ,(resolve-memory mem)))
  472. (('table.init table elem)
  473. `(table.init ,(resolve-table table) ,(resolve-elem elem)))
  474. (('elem.drop elem)
  475. `(elem.drop ,(resolve-elem elem)))
  476. (('table.copy dst src)
  477. `(table.copy ,(resolve-table dst) ,(resolve-table src)))
  478. (((and inst (or 'table.grow 'table.size 'table.fill)) table)
  479. `(,inst ,(resolve-table table)))
  480. ;; Not yet implemented: simd mem ops, atomic mem ops.
  481. ((? symbol? op) `(,op))
  482. (inst inst))
  483. insts))
  484. (define (visit-type type)
  485. (define (resolve-field field)
  486. (match field
  487. (($ <field> id mutable? type)
  488. (make-field id mutable? (resolve-storage-type type)))))
  489. (define (resolve-base type)
  490. (match type
  491. (($ <func-sig> params results)
  492. (make-func-sig (map resolve-param params)
  493. (map resolve-val-type results)))
  494. (($ <array-type> mutable? type)
  495. (make-array-type mutable? (resolve-storage-type type)))
  496. (($ <struct-type> fields)
  497. (make-struct-type (map resolve-field fields)))))
  498. (define (resolve-sub type)
  499. (match type
  500. (($ <type> id type)
  501. (make-type id
  502. (match type
  503. (($ <sub-type> final? supers type)
  504. (make-sub-type final?
  505. (map resolve-heap-type supers)
  506. (resolve-base type)))
  507. (_ (resolve-base type)))))))
  508. (match type
  509. (($ <rec-group> sub-types)
  510. (make-rec-group (map resolve-sub sub-types)))
  511. (_ (resolve-sub type))))
  512. (define (visit-import import)
  513. (match import
  514. (($ <import> mod name 'func id type)
  515. (make-import mod name 'func id (resolve-type-use type)))
  516. (($ <import> mod name 'global id ($ <global-type> mutable? type))
  517. (make-import mod name 'global id
  518. (make-global-type mutable? (resolve-val-type type))))
  519. ((and import ($ <import> mod name 'memory))
  520. import)
  521. (($ <import> mod name 'table id ($ <table-type> limits type))
  522. (make-import mod name 'table id
  523. (make-table-type limits (resolve-val-type type))))))
  524. (define (visit-export export)
  525. (match export
  526. (($ <export> name 'func id)
  527. (make-export name 'func (resolve-func id)))
  528. (($ <export> name 'table id)
  529. (make-export name 'table (resolve-table id)))
  530. (($ <export> name 'memory id)
  531. (make-export name 'memory (resolve-memory id)))
  532. (($ <export> name 'global id)
  533. (make-export name 'global (resolve-global id)))))
  534. (define (strip-declarative-segments elems)
  535. (filter (match-lambda
  536. (($ <elem> id mode) (not (eq? mode 'declarative))))
  537. elems))
  538. (define (add-declarative-segment elems)
  539. (match (sort (hash-map->list (lambda (k v) k) functions-used-as-values)
  540. <)
  541. (() elems)
  542. (funcs
  543. (let ((declarative (make-elem #f 'declarative #f 'funcref #f
  544. (map (lambda (func-idx)
  545. `((ref.func ,func-idx)))
  546. funcs))))
  547. (append elems (list declarative))))))
  548. (define (visit-elem elem)
  549. (match elem
  550. (($ <elem> id mode table type offset init)
  551. (make-elem id mode (and table (resolve-table table))
  552. (resolve-val-type type)
  553. (and offset (resolve-instructions offset '() '()))
  554. (map (lambda (init)
  555. (resolve-instructions init '() '()))
  556. init)))))
  557. (define (visit-data data)
  558. (match data
  559. (($ <data> id mode mem offset init)
  560. (make-data id mode (and mem (resolve-memory mem))
  561. (and offset (resolve-instructions offset '() '()))
  562. init))))
  563. (define (visit-start start)
  564. (and start (resolve-func start)))
  565. (define (visit-func func)
  566. (define (visit-local local)
  567. (match local
  568. (($ <local> id type)
  569. (make-local id (resolve-val-type type)))))
  570. (match func
  571. (($ <func> id type locals body)
  572. (match (resolve-type-use type)
  573. ((and type ($ <type-use> idx ($ <func-sig> params _)))
  574. (make-func id type (map visit-local locals)
  575. (resolve-instructions body
  576. (append params locals)
  577. '())))))))
  578. (define (visit-table table)
  579. (match table
  580. (($ <table> id ($ <table-type> limits type) init)
  581. (make-table id
  582. (make-table-type limits (resolve-val-type type))
  583. (and init (resolve-instructions init '() '()))))))
  584. (define (visit-memory mem) mem)
  585. (define (visit-global global)
  586. (match global
  587. (($ <global> id ($ <global-type> mutable? type) init)
  588. (make-global id
  589. (make-global-type mutable? (resolve-val-type type))
  590. (resolve-instructions init '() '())))))
  591. (define (visit-tag tag)
  592. (match tag
  593. (($ <tag> id type)
  594. (make-tag id (resolve-type-use type)))))
  595. (let ((types (map visit-type types))
  596. (imports (map visit-import imports))
  597. (exports (map visit-export exports))
  598. (%elems (map visit-elem (strip-declarative-segments elems)))
  599. (datas (map visit-data datas))
  600. (start (visit-start start))
  601. (funcs (map visit-func funcs))
  602. (tables (map visit-table tables))
  603. (memories (map visit-memory memories))
  604. (globals (map visit-global globals))
  605. (tags (map visit-tag tags))
  606. (custom (if emit-names?
  607. (cons (generate-names) custom)
  608. custom)))
  609. (define strings
  610. (map car
  611. (sort (hash-map->list cons interned-strings)
  612. (match-lambda*
  613. (((s1 . idx1) (s2 . idx2)) (< idx1 idx2))))))
  614. (define elems (add-declarative-segment %elems))
  615. (make-wasm #f types imports funcs tables memories globals exports start
  616. elems datas tags strings custom)))))