wat.scm 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. ;;; WebAssembly text format parser and unparser
  2. ;;; Copyright (C) 2023 Igalia, S.L.
  3. ;;; Copyright (C) 2023 Robin Templeton <robin@spritely.institute>
  4. ;;; Copyright (C) 2023, 2024 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. ;;; Converts WAT to WASM and vice versa.
  20. ;;;
  21. ;;; Code:
  22. (define-module (wasm wat)
  23. #:use-module (ice-9 binary-ports)
  24. #:use-module (ice-9 match)
  25. #:use-module (rnrs bytevectors)
  26. #:use-module ((srfi srfi-1) #:select (append-map filter-map))
  27. #:use-module (srfi srfi-11)
  28. #:use-module (wasm types)
  29. #:export (wat->wasm wasm->wat))
  30. ;; to-do:
  31. ;; - support reftypes
  32. ;; - support bulk memory instructions
  33. ;; - tail calls
  34. ;; - stringref
  35. ;; differences from standard: scheme comments / no block comments.
  36. ;; strings have guile string syntax; bytevectors also for data. could
  37. ;; write standard-compliant parser instead (port from wassemble).
  38. (define (natural-alignment inst)
  39. (case inst
  40. ((i32.load8_s
  41. i32.load8_u
  42. i64.load8_s
  43. i64.load8_u
  44. i32.store8
  45. i64.store8)
  46. 0)
  47. ((i32.load16_s
  48. i32.load16_u
  49. i64.load16_s
  50. i64.load16_u
  51. i32.store16
  52. i64.store16)
  53. 1)
  54. ((i32.load
  55. f32.load
  56. i64.load32_s
  57. i64.load32_u
  58. i32.store
  59. f32.store
  60. i64.store32)
  61. 2)
  62. ((i64.load
  63. f64.load
  64. i64.store
  65. f64.store)
  66. 3)
  67. (else (error "unrecognized instruction" inst))))
  68. (define (wat->wasm expr)
  69. (define (id? x)
  70. (and (symbol? x) (eqv? (string-ref (symbol->string x) 0) #\$)))
  71. (define (kw? x)
  72. (and (symbol? x) (not (id? x))))
  73. (define (u32? x)
  74. (and (exact-integer? x) (<= 0 x (1- (ash 1 32)))))
  75. (define (s32? x)
  76. (and (exact-integer? x) (<= (ash -1 31) x (1- (ash 1 31)))))
  77. (define (u64? x)
  78. (and (exact-integer? x) (<= 0 x (1- (ash 1 64)))))
  79. (define (s64? x)
  80. (and (exact-integer? x) (<= (ash -1 63) x (1- (ash 1 63)))))
  81. (define (id-or-idx? x) (or (id? x) (u32? x)))
  82. (define (assert-true x msg)
  83. (or x (error msg)))
  84. ;; Identifiers with a space can't be in the source program.
  85. (define fresh-idx 0)
  86. (define (fresh-id!)
  87. (let ((id (string->symbol
  88. (string-append "$fresh " (number->string fresh-idx)))))
  89. (set! fresh-idx (1+ fresh-idx))
  90. id))
  91. (define (partition-clauses x)
  92. (define id #f)
  93. (define types '())
  94. (define imports '())
  95. (define funcs '())
  96. (define tables '())
  97. (define memories '())
  98. (define globals '())
  99. (define exports '())
  100. (define start #f)
  101. (define elems '())
  102. (define datas '())
  103. (define tags '())
  104. (define strings '())
  105. (define (collect-raw x)
  106. (match x
  107. (('type . x) (set! types (cons x types)))
  108. (('import . x) (set! imports (cons x imports)))
  109. (('func . x) (set! funcs (cons x funcs)))
  110. (('table . x) (set! tables (cons x tables)))
  111. (('memory . x) (set! memories (cons x memories)))
  112. (('global . x) (set! globals (cons x globals)))
  113. (('export . x) (set! exports (cons x exports)))
  114. (('start . x) (begin
  115. (when start (error "multiple start clauses"))
  116. (set! start x)))
  117. (('elem . x) (set! elems (cons x elems)))
  118. (('data . x) (set! datas (cons x datas)))
  119. (('tag . x) (set! tags (cons x tags)))
  120. (('strings . x) (set! strings (append strings x)))
  121. (('rec . _) (set! types (cons x types)))
  122. (_ (error "unexpected form in module" x))))
  123. (match x
  124. (('module (? id? mod-id) . clauses)
  125. (set! id mod-id)
  126. (for-each collect-raw clauses))
  127. (('module . clauses) (for-each collect-raw clauses))
  128. ((clauses ...) (for-each collect-raw clauses))
  129. (_ (error "unexpected module" x)))
  130. (make-wasm id (reverse types) (reverse imports) (reverse funcs)
  131. (reverse tables) (reverse memories) (reverse globals)
  132. (reverse exports) start (reverse elems)
  133. (reverse datas) (reverse tags) strings '()))
  134. (define (valid-heap-type? x)
  135. (match x
  136. ((or 'func 'extern 'any 'none 'noextern 'nofunc 'eq 'struct 'array 'i31
  137. 'string 'stringview_iter 'stringview_wtf8 'stringview_wtf16
  138. (? id-or-idx?))
  139. #t)
  140. (_ #f)))
  141. (define (parse-heap-type x)
  142. (if (valid-heap-type? x)
  143. x
  144. (error "bad heaptype" x)))
  145. (define* (parse-ref-type x #:key (error-message "bad reftype"))
  146. (match x
  147. (('ref 'null ht) (make-ref-type #t (parse-heap-type ht)))
  148. (('ref ht) (make-ref-type #f (parse-heap-type ht)))
  149. ('funcref (make-ref-type #t 'func))
  150. ('externref (make-ref-type #t 'extern))
  151. ('anyref (make-ref-type #t 'any))
  152. ('nullref (make-ref-type #t 'none))
  153. ('nullexternref (make-ref-type #t 'noextern))
  154. ('nullfuncref (make-ref-type #t 'nofunc))
  155. ('eqref (make-ref-type #t 'eq))
  156. ('structref (make-ref-type #t 'struct))
  157. ('arrayref (make-ref-type #t 'array))
  158. ('i31ref (make-ref-type #t 'i31))
  159. (_ (error error-message x))))
  160. (define (parse-val-type x)
  161. (match x
  162. ((or 'i32 'i64 'f32 'f64 'v128) x)
  163. (_ (parse-ref-type x #:error-message "bad valtype"))))
  164. (define (parse-storage-type x)
  165. (match x
  166. ((or 'i8 'i16) x)
  167. (_ (parse-val-type x))))
  168. (define (parse-params x)
  169. (match x
  170. (((? id? id) vt) (list (make-param id (parse-val-type vt))))
  171. ((vt ...) (map (lambda (vt) (make-param #f (parse-val-type vt))) vt))))
  172. (define (parse-results x)
  173. (match x
  174. ((vt ...) (map parse-val-type vt))))
  175. (define (parse-func-sig x)
  176. (let lp ((x x) (params '()))
  177. (match x
  178. ((('param . tail) . x)
  179. (lp x (append params (parse-params tail))))
  180. (_
  181. (let lp ((x x) (results '()))
  182. (match x
  183. ((('result . tail) . x)
  184. (lp x (append results (parse-results tail))))
  185. (_ (values (make-func-sig params results) x))))))))
  186. (define (parse-func-type x)
  187. (let-values (((sig tail) (parse-func-sig x)))
  188. (unless (null? tail) "unexpected tail on func type" tail)
  189. sig))
  190. (define (parse-type-use x)
  191. (define (parse-sig idx x)
  192. (let-values (((sig x) (parse-func-sig x)))
  193. (values (make-type-use idx sig) x)))
  194. (match x
  195. ((('type idx) . sig)
  196. (parse-sig idx sig))
  197. (sig (parse-sig #f sig))))
  198. (define (parse-block-type x)
  199. (match x
  200. ;; cf. emit-block-type in (@ (wasm assemble) assemble-wasm)
  201. (((and t (or (? symbol?) ('ref . _))) . tail)
  202. (values
  203. (make-type-use #f (make-func-sig '() (list (parse-val-type t))))
  204. tail))
  205. (_ (parse-type-use x))))
  206. (define (parse-limits x)
  207. (match x
  208. (((? u32? min) (? u32? max) . tail)
  209. (values (make-limits min max) tail))
  210. (((? u32? min) . tail)
  211. (values (make-limits min #f) tail))))
  212. (define (parse-elem-type x)
  213. (match x
  214. (('funcref) 'funcref)
  215. ((x) (parse-ref-type x #:error-message "bad elem type"))
  216. (_ (error "bad elem type"))))
  217. (define (parse-table-type x)
  218. (let-values (((limits x) (parse-limits x)))
  219. (make-table-type limits (parse-elem-type x))))
  220. (define (parse-mem-type x)
  221. (let-values (((limits x) (parse-limits x)))
  222. (match x
  223. (() (make-mem-type limits)))))
  224. (define (parse-global-type x)
  225. (match x
  226. (('mut vt) (make-global-type #t (parse-val-type vt)))
  227. (vt (make-global-type #f (parse-val-type vt)))))
  228. (define (parse-id-or-idx x)
  229. (match x
  230. (((? id-or-idx? id) . x) (values id x))
  231. (_ (values #f x))))
  232. (define (parse-id x)
  233. (match x
  234. (((? id? id) . x) (values id x))
  235. (_ (values #f x))))
  236. (define (parse-array-type x)
  237. (match x
  238. (('mut t) (make-array-type #t (parse-storage-type t)))
  239. (t (make-array-type #f (parse-storage-type t)))))
  240. (define (parse-field x)
  241. (match x
  242. (('field (? id? id) ('mut t))
  243. (make-field id #t (parse-storage-type t)))
  244. (('field (? id? id) t)
  245. (make-field id #f (parse-storage-type t)))
  246. (('mut t)
  247. (make-field #f #t (parse-storage-type t)))
  248. (t
  249. (make-field #f #f (parse-storage-type t)))))
  250. (define (parse-struct-type x)
  251. (make-struct-type (map parse-field x)))
  252. (define (parse-sub-type type)
  253. (match type
  254. (('sub 'final (? id-or-idx? super) ... type)
  255. (make-sub-type #t super (parse-prim-type type)))
  256. (('sub (? id-or-idx? super) ... type)
  257. (make-sub-type #f super (parse-prim-type type)))
  258. (type
  259. (parse-prim-type type))))
  260. (define (parse-prim-type x)
  261. (match x
  262. (('func . sig) (parse-func-sig sig))
  263. (('array sig) (parse-array-type sig))
  264. (('struct . sig) (parse-struct-type sig))))
  265. (define (parse-type x)
  266. (match x
  267. (('sub id sub)
  268. (make-sub-type #f (list id) (parse-prim-type sub)))
  269. (_ (parse-prim-type x))))
  270. (define (parse-type-def def)
  271. (define (parse-def def)
  272. (match def
  273. (((? id-or-idx? id) type) (make-type id (parse-sub-type type)))
  274. ((type) (make-type #f (parse-sub-type type)))))
  275. (match def
  276. (('rec ('type . def) ...) (make-rec-group (map parse-def def)))
  277. (def (parse-def def))))
  278. (define (parse-import x)
  279. (define (parse-inner mod name kind id tail)
  280. (match kind
  281. ('func (make-import mod name 'func id (parse-type-use tail)))
  282. ('table (make-import mod name 'table id (parse-table-type tail)))
  283. ('memory (make-import mod name 'memory id (parse-mem-type tail)))
  284. ('global (make-import mod name 'global id
  285. (match tail
  286. ((type) (parse-global-type type)))))))
  287. (match x
  288. (((? string? mod) (? string? name) desc)
  289. (match desc
  290. ((kind (? id? id) . tail)
  291. (parse-inner mod name kind id tail))
  292. ((kind . tail)
  293. (parse-inner mod name kind #f tail))))))
  294. (define (parse-export x)
  295. (match x
  296. (((? string? name) ((and kind (or 'func 'table 'memory 'global)) idx))
  297. (make-export name kind idx))))
  298. (define (parse-mem-arg x inst)
  299. (define (symbol-with-prefix prefix)
  300. (lambda (x)
  301. (and (symbol? x)
  302. (string-prefix? prefix (symbol->string x)))))
  303. (define (symbol-suffix x prefix)
  304. (substring (symbol->string x) (string-length prefix)))
  305. (define (parse-arg prefix x)
  306. (match x
  307. (((? (symbol-with-prefix prefix) arg) . x)
  308. (values
  309. (or (string->number (symbol-suffix arg prefix))
  310. (error "bad mem arg" arg))
  311. x))
  312. (_ (values #f x))))
  313. (let*-values (((idx x) (parse-id-or-idx x))
  314. ((offset x) (parse-arg "offset=" x))
  315. ((align x) (parse-arg "align=" x)))
  316. (values (make-mem-arg (or idx 0)
  317. (or offset 0)
  318. (or align (natural-alignment inst)))
  319. x)))
  320. (define (unfold-instruction inst)
  321. (define (unparse-val-type type)
  322. (match type
  323. ((? symbol?) type)
  324. (($ <ref-type> #f ht) `(ref ,ht))
  325. (($ <ref-type> #t ht) `(ref null ,ht))))
  326. (define (unfold-func-sig sig)
  327. (match sig
  328. (($ <func-sig> params results)
  329. `(,@(map (match-lambda
  330. ((#f . vt) `(param ,(unparse-val-type vt)))
  331. ((id . vt) `(param ,id ,(unparse-val-type vt))))
  332. params)
  333. (result ,@(map unparse-val-type results))))))
  334. (define (unfold-type-use type)
  335. (match type
  336. (($ <type-use> #f sig)
  337. (unfold-func-sig sig))
  338. (($ <type-use> idx sig)
  339. `((type ,idx) ,@(unfold-func-sig sig)))))
  340. (define (unfold-mem-arg arg)
  341. (match arg
  342. (($ <mem-arg> id offset align)
  343. `(,@(if (eqv? id 0)
  344. '()
  345. (list id))
  346. ,@(if offset
  347. (list (string->symbol (format #f "offset=~a" offset)))
  348. '())
  349. ,@(if align
  350. (list (string->symbol (format #f "align=~a" align)))
  351. '())))))
  352. (match inst
  353. (((and tag (or 'loop 'block)) body ...)
  354. (cons tag (append body (list 'end))))
  355. (('if . body)
  356. (let*-values (((label body) (parse-id body))
  357. ((type body) (parse-block-type body)))
  358. (define (finish test consequent alternate)
  359. `(,@test
  360. if ,@(if label `(,label) '()) ,@(unfold-type-use type)
  361. ,@consequent
  362. else ,@alternate
  363. end))
  364. (match body
  365. ((test ... ('then consequent ...))
  366. (finish test consequent '()))
  367. ((test ... ('then consequent ...) ('else alternate ...))
  368. (finish test consequent alternate)))))
  369. (((and tag (or 'br 'br_if 'call 'local.get 'local.set 'local.tee
  370. 'global.get 'global.set))
  371. idx
  372. . args)
  373. `(,@args ,tag ,idx))
  374. (((and tag (or 'br_on_cast 'br_on_cast_fail)) target rt1 rt2
  375. . args)
  376. `(,@args ,tag ,target ,rt1 ,rt2))
  377. (('br_table . args)
  378. (let lp ((args args) (targets '()))
  379. (match args
  380. (((? id-or-idx? target) . args)
  381. (lp args (cons target targets)))
  382. (_ `(,@args br_table ,@(reverse targets))))))
  383. (('call_indirect . args)
  384. (let*-values (((table args) (parse-id-or-idx args))
  385. ((type args) (parse-type-use args)))
  386. `(,@args call_indirect ,table ,@(unfold-type-use type))))
  387. (((and tag (or 'i32.load
  388. 'i64.load
  389. 'f32.load
  390. 'f64.load
  391. 'i32.load8_s
  392. 'i32.load8_u
  393. 'i32.load16_s
  394. 'i32.load16_u
  395. 'i64.load8_s
  396. 'i64.load8_u
  397. 'i64.load16_s
  398. 'i64.load16_u
  399. 'i64.load32_s
  400. 'i64.load32_u
  401. 'i32.store
  402. 'i64.store
  403. 'f32.store
  404. 'f64.store
  405. 'i32.store8
  406. 'i32.store16
  407. 'i64.store8
  408. 'i64.store16
  409. 'i64.store32))
  410. . args)
  411. (let-values (((mem-arg args) (parse-mem-arg args tag)))
  412. `(,@args ,tag ,@(unfold-mem-arg mem-arg))))
  413. (((and tag (or 'i32.const 'i64.const 'f32.const 'f64.const)) val . insts)
  414. `(,@insts ,tag ,val))
  415. (('ref.func (? id-or-idx? id) . args)
  416. `(,@args ref.func ,id))
  417. (((and tag (or 'call_ref 'return_call_ref)) (? id-or-idx? id) . args)
  418. `(,@args ,tag ,id))
  419. (((and tag 'ref.null) (? valid-heap-type? id) . args)
  420. `(,@args ,tag ,id))
  421. (((and tag (or 'table.set 'table.get 'table.size 'table.grow
  422. 'table.fill 'elem.drop
  423. 'memory.size 'memory.grow 'memory.fill))
  424. (? id-or-idx? id) . args)
  425. `(,@args ,tag ,id))
  426. (((and tag (or 'memory.init 'table.init))
  427. (? id-or-idx? id) (? id-or-idx? eid) . args)
  428. `(,@args ,tag ,id ,eid))
  429. (((and tag (or 'memory.init 'table.init)) (? id-or-idx? eid) . args)
  430. `(,@args ,tag 0 ,eid))
  431. (((and tag (or 'memory.copy 'table.copy))
  432. (? id-or-idx? a) (? id-or-idx? b) . args)
  433. `(,@args ,tag ,a ,b))
  434. (((and tag (or 'memory.copy 'table.copy)) . args)
  435. `(,@args ,tag 0 0))
  436. (((and tag (or 'struct.new 'struct.new_default)) (? id-or-idx? id) . args)
  437. `(,@args ,tag ,id))
  438. (((and tag (or 'struct.set 'struct.get 'struct.get_s 'struct.get_u))
  439. (? id-or-idx? ti)
  440. (? id-or-idx? fi) . args)
  441. `(,@args ,tag ,ti ,fi))
  442. (((and tag (or 'ref.test 'ref.cast)) 'null (? valid-heap-type? id) . args)
  443. `(,@args ,tag null ,id))
  444. (((and tag (or 'ref.test 'ref.cast)) (? valid-heap-type? id) . args)
  445. `(,@args ,tag ,id))
  446. (((and tag 'string.const) (? string? str) . args)
  447. `(,@args ,tag ,str))
  448. (((and tag (or 'array.new 'array.new_default 'array.fill
  449. 'array.get 'array.set 'array.get_u 'array.get_s))
  450. (? id-or-idx? ti) . args)
  451. `(,@args ,tag ,ti))
  452. (((and tag 'array.new_fixed) (? id-or-idx? ti) (? s32? k) . args)
  453. `(,@args ,tag ,ti ,k))
  454. (((and tag (or 'array.copy
  455. 'array.new_data 'array.new_elem
  456. 'array.init_data 'array.init_elem))
  457. (? id-or-idx? ti1) (? id-or-idx? ti2) . args)
  458. `(,@args ,tag ,ti1 ,ti2))
  459. (((and tag 'return_call) (? id-or-idx? id) . args)
  460. `(,@args ,tag ,id))
  461. ((tag . args)
  462. `(,@args ,tag))))
  463. (define (parse-block x block-kind)
  464. (let lp ((in x) (out '()))
  465. (define (lp/inst in parsed)
  466. (lp in (cons parsed out)))
  467. (define (lp/block block-label in parsed)
  468. ;; Skip end label.
  469. (let-values (((label in) (parse-id in)))
  470. (when label
  471. (unless (eq? label block-label) (error "bad end label" label)))
  472. (lp/inst in parsed)))
  473. (match in
  474. (()
  475. (unless (eq? block-kind 'body)
  476. (error "unexpected end of instruction sequence"))
  477. (values (reverse out) '()))
  478. (((folded ...) . in)
  479. (lp (append (unfold-instruction folded) in) out))
  480. (((? kw? inst) . in)
  481. (match inst
  482. ((or 'block 'loop)
  483. (let*-values (((label in) (parse-id in))
  484. ((type in) (parse-block-type in))
  485. ((insts in) (parse-block in inst)))
  486. (lp/block label in `(,inst ,label ,type ,insts))))
  487. ('if
  488. (let*-values (((label in) (parse-id in))
  489. ((type in) (parse-block-type in))
  490. ((consequent in) (parse-block in 'then)))
  491. (match in
  492. (('else . in)
  493. (let-values (((alternate in) (parse-block in 'else)))
  494. (lp/block label in
  495. `(,inst ,label ,type ,consequent ,alternate))))
  496. (_
  497. (lp/block label in `(,inst ,label ,type ,consequent ()))))))
  498. ('else
  499. (unless (eq? block-kind 'then)
  500. (error "unexpected 'else'"))
  501. (values (reverse out) (cons 'else in)))
  502. ('end
  503. (when (eq? block-kind 'body)
  504. (error "unexpected 'end'"))
  505. (values (reverse out) in))
  506. ('ref.func
  507. (match in
  508. (((? id-or-idx? id) . in)
  509. (lp/inst in `(,inst ,id)))))
  510. ((or 'br 'br_if
  511. 'call 'local.get 'local.set 'local.tee 'global.get
  512. 'global.set)
  513. (let-values (((idx in) (parse-id-or-idx in)))
  514. (unless idx (error "missing idx" inst in))
  515. (lp/inst in `(,inst ,idx))))
  516. ((or 'br_on_cast 'br_on_cast_fail)
  517. (match in
  518. (((? id-or-idx? target) rt1 rt2 . in)
  519. (lp/inst
  520. in
  521. `(,inst ,target ,(parse-ref-type rt1) ,(parse-ref-type rt2))))))
  522. ('br_table
  523. (let lp ((in in) (targets '()))
  524. (match in
  525. (((? id-or-idx? target) . in)
  526. (lp in (cons target targets)))
  527. (_
  528. (match (reverse targets)
  529. ((target ... default)
  530. (lp/inst in `(,inst ,target ,default)))
  531. (_ (error "no targets for br_table")))))))
  532. ('call_indirect
  533. (let*-values (((table in) (parse-id-or-idx in))
  534. ((type in) (parse-type-use in)))
  535. (lp/inst in `(,inst ,(or table 0) ,type))))
  536. ((or 'i32.load 'i64.load
  537. 'f32.load 'f64.load
  538. 'i32.load8_s 'i32.load8_u 'i32.load16_s 'i32.load16_u
  539. 'i64.load8_s 'i64.load8_u 'i64.load16_s 'i64.load16_u
  540. 'i64.load32_s 'i64.load32_u
  541. 'i32.store 'i64.store
  542. 'f32.store 'f64.store
  543. 'i32.store8 'i32.store16
  544. 'i64.store8 'i64.store16 'i64.store32)
  545. (let-values (((mem-arg in) (parse-mem-arg in inst)))
  546. (lp/inst in `(,inst ,mem-arg))))
  547. ('i32.const
  548. (match in
  549. (((? s32? const) . in)
  550. (lp/inst in `(,inst ,const)))))
  551. ('i64.const
  552. (match in
  553. (((? s64? const) . in)
  554. (lp/inst in `(,inst ,const)))))
  555. ((or 'f32.const 'f64.const)
  556. (match in
  557. (((? real? const) . in)
  558. (lp/inst in `(,inst ,(exact->inexact const))))))
  559. ((or 'call_ref 'return_call_ref)
  560. (match in
  561. (((? id-or-idx? id) . in)
  562. (lp/inst in `(,inst ,id)))))
  563. ('ref.null
  564. (match in
  565. ((id . in)
  566. (lp/inst in `(,inst ,(parse-heap-type id))))))
  567. ((or 'table.set 'table.get 'table.size 'table.grow
  568. 'table.fill 'elem.drop
  569. 'memory.size 'memory.grow 'memory.fill)
  570. (match in
  571. (((? id-or-idx? id) . in)
  572. (lp/inst in `(,inst ,id)))
  573. (_
  574. (lp/inst in `(,inst 0)))))
  575. ((or 'table.copy 'memory.copy)
  576. (match in
  577. (((? id-or-idx? a) (? id-or-idx? b) . in)
  578. (lp/inst in `(,inst ,a ,b)))
  579. (_
  580. (lp/inst in `(,inst 0 0)))))
  581. ((or 'table.init 'memory.init)
  582. (match in
  583. (((? id-or-idx? tid) (? id-or-idx? eid) . in)
  584. (lp/inst in `(,inst ,tid ,eid)))
  585. (((? id-or-idx? eid) . in)
  586. (lp/inst in `(,inst 0 ,eid)))))
  587. ((or 'struct.new 'struct.new_default)
  588. (match in
  589. (((? id-or-idx? id) . in)
  590. (lp/inst in `(,inst ,id)))))
  591. ((or 'struct.set 'struct.get 'struct.get_s 'struct.get_u)
  592. (match in
  593. (((? id-or-idx? ti) (? id-or-idx? fi) . in)
  594. (lp/inst in `(,inst ,ti ,fi)))))
  595. ((or 'ref.test 'ref.cast)
  596. (match in
  597. (('null ht . in)
  598. (lp/inst in `(,inst ,(make-ref-type #t (parse-heap-type ht)))))
  599. ((ht . in)
  600. (lp/inst in `(,inst ,(make-ref-type #f (parse-heap-type ht)))))))
  601. ('string.const
  602. (match in
  603. (((? string? str) . in)
  604. (lp/inst in `(,inst ,str)))))
  605. ((or 'array.new 'array.new_default 'array.fill
  606. 'array.get 'array.set 'array.get_u 'array.get_s)
  607. (match in
  608. (((? id-or-idx? ti) . in)
  609. (lp/inst in `(,inst ,ti)))))
  610. ('array.new_fixed
  611. (match in
  612. (((? id-or-idx? ti) (? s32? k) . in)
  613. (lp/inst in `(,inst ,ti ,k)))))
  614. ((or 'array.copy
  615. 'array.new_data 'array.new_elem
  616. 'array.init_data 'array.init_elem)
  617. (match in
  618. (((? id-or-idx? idx1) (? id-or-idx? idx2) . in)
  619. (lp/inst in `(,inst ,idx1 ,idx2)))))
  620. ('return_call
  621. (match in
  622. (((? id-or-idx? id) . in)
  623. (lp/inst in `(,inst ,id)))))
  624. (_
  625. (lp/inst in (list inst))))))))
  626. (define (parse-offset x)
  627. (parse-block (match x
  628. (('offset . offset) offset)
  629. (offset (list offset)))
  630. 'body))
  631. (define (parse-init init)
  632. (parse-block (match init
  633. (('item . init) init)
  634. (init (list init)))
  635. 'body))
  636. (define (parse-elem x)
  637. (define (parse-table-use x)
  638. (match x
  639. ((('table (? id-or-idx? table)) . x) (values table x))
  640. (_ (values #f x))))
  641. (define (parse-elemlist elemlist)
  642. (match elemlist
  643. (('func (? id-or-idx? id) ...)
  644. (values 'funcref (map (lambda (id) `((ref.func ,id))) id)))
  645. (((? id-or-idx? id) ...)
  646. (values 'funcref (map (lambda (id) `((ref.func ,id))) id)))
  647. ((type init ...)
  648. (values (parse-ref-type type) (map parse-init init)))))
  649. (let-values (((id x) (parse-id x)))
  650. (match x
  651. (('declare . elemlist)
  652. ;; Declarative element segment.
  653. (let-values (((type inits) (parse-elemlist elemlist)))
  654. (make-elem id 'declarative #f type #f inits)))
  655. ((('table table) offset . elemlist)
  656. ;; Active element segment with explicit table.
  657. (let-values (((type inits) (parse-elemlist elemlist)))
  658. (make-elem id 'active table type (parse-offset offset) inits)))
  659. (((and offset (or ('offset . _) ('i32.const _))) . elemlist)
  660. ;; Active element segment for table 0.
  661. (let-values (((type inits) (parse-elemlist elemlist)))
  662. (make-elem id 'active 0 type (parse-offset offset) inits)))
  663. (elemlist
  664. (let-values (((type inits) (parse-elemlist elemlist)))
  665. (make-elem id 'passive #f type #f inits))))))
  666. (define (bytevector-concatenate bvs)
  667. (call-with-output-bytevector
  668. (lambda (p)
  669. (for-each (lambda (bv) (put-bytevector p bv)) bvs))))
  670. (define (parse-data x)
  671. (let-values (((id x) (parse-id x)))
  672. (match x
  673. ((('memory (? id-or-idx? mem)) offset . init)
  674. ;; Active data segment with explicit memory.
  675. (make-data id 'active mem (parse-offset offset)
  676. (bytevector-concatenate init)))
  677. (((and offset (or ('offset . _) ('i32.const _))) . init)
  678. ;; Active data segment for memory 0.
  679. (make-data id 'active 0 (parse-offset offset)
  680. (bytevector-concatenate init)))
  681. (init
  682. ;; Passive data segment.
  683. (make-data id 'passive #f #f (bytevector-concatenate init))))))
  684. (define (parse-tag tag)
  685. (let*-values (((id tag) (parse-id tag))
  686. ((type tag) (parse-type-use tag)))
  687. (match tag
  688. (() (make-tag id type))
  689. (_ (error "bad tag" tag)))))
  690. (define (parse-start start)
  691. (let-values (((idx x) (parse-id-or-idx start)))
  692. (match x
  693. (() (or idx 0))
  694. (_ (error "bad start" x)))))
  695. (match (partition-clauses expr)
  696. (($ <wasm> id types imports funcs tables memories globals exports start
  697. elems datas tags strings custom)
  698. (let ((types (map parse-type-def types))
  699. (imports (map parse-import imports))
  700. (exports (map parse-export exports))
  701. (elems (map parse-elem elems))
  702. (datas (map parse-data datas))
  703. (tags (map parse-tag tags))
  704. (start (and start (parse-start start))))
  705. (define-syntax-rule (push! id val)
  706. (set! id (append! id (list val))))
  707. (define (visit-func x)
  708. (let*-values (((id x) (parse-id x))
  709. ((id) (or id (fresh-id!))))
  710. (let lp ((x x))
  711. (match x
  712. ((('export name) . x)
  713. (push! exports (make-export name 'func id))
  714. (lp x))
  715. ((('import mod name) . x)
  716. (let-values (((type x) (parse-type-use x)))
  717. (match x
  718. (()
  719. (push! imports (make-import mod name 'func id type))
  720. #f)
  721. (_
  722. (error "bad import" x)))))
  723. (_
  724. (let-values (((type x) (parse-type-use x)))
  725. (let lp ((x x) (locals '()))
  726. (match x
  727. ((('local (? id? id) vt) . x)
  728. (lp x (cons (make-local id (parse-val-type vt)) locals)))
  729. (_
  730. (make-func id type (reverse locals)
  731. (parse-block x 'body)))))))))))
  732. (define (visit-table x)
  733. (let*-values (((id x) (parse-id x))
  734. ((id) (or id (fresh-id!))))
  735. (let lp ((x x))
  736. (match x
  737. ((('export name) . x)
  738. (push! exports (make-export name 'table id))
  739. (lp x))
  740. ((('import mod name) . x)
  741. (let ((type (parse-table-type x)))
  742. (push! imports (make-import mod name 'table id type))
  743. #f))
  744. ((elemtype ... ('elem . segment))
  745. (let ((elemtype (parse-elem-type elemtype))
  746. (len (length segment))
  747. (offset '((i32.const 0))))
  748. (push!
  749. elems
  750. (match segment
  751. (((? id-or-idx? id) ...)
  752. (make-elem #f 'active id 'funcref offset
  753. (map (lambda (id) `((ref.func ,id))) id)))
  754. (((_ ...) ...)
  755. (make-elem #f 'active id 'funcref offset
  756. (map (lambda (init)
  757. (parse-block init 'body))
  758. segment)))))
  759. (make-table id
  760. (make-table-type (make-limits len len)
  761. elemtype)
  762. #f)))
  763. (_
  764. (make-table id (parse-table-type x) #f))))))
  765. (define (visit-memory x)
  766. (let*-values (((id x) (parse-id x))
  767. ((id) (or id (fresh-id!))))
  768. (let lp ((x x))
  769. (match x
  770. ((('export name) . x)
  771. (push! exports (make-export name 'memory id))
  772. (lp x))
  773. ((('import mod name) . x)
  774. (let ((type (parse-mem-type x)))
  775. (push! imports (make-import mod name 'memory id type))
  776. #f))
  777. ((('data . data))
  778. (let* ((init (bytevector-concatenate data))
  779. (len (bytevector-length init)))
  780. (push! data (make-data id 'active 0 '((i32.const 0)) init))
  781. (make-memory id (make-mem-type (make-limits len len)))))
  782. (_
  783. (make-memory id (parse-mem-type x)))))))
  784. (define (visit-global x)
  785. (let*-values (((id x) (parse-id x))
  786. ((id) (or id (fresh-id!))))
  787. (let lp ((x x))
  788. (match x
  789. ((('export name) . x)
  790. (push! exports (make-export name 'global id))
  791. (lp x))
  792. ((('import mod name) type)
  793. (let ((type (parse-global-type type)))
  794. (push! imports (make-import mod name 'global id type))
  795. #f))
  796. ((type . init)
  797. (make-global id
  798. (parse-global-type type)
  799. (parse-block init 'body)))))))
  800. (let ((funcs (filter-map visit-func funcs))
  801. (tables (filter-map visit-table tables))
  802. (memories (filter-map visit-memory memories))
  803. (globals (filter-map visit-global globals)))
  804. (make-wasm id types imports funcs tables memories globals exports
  805. start elems datas tags strings custom))))))
  806. (define (wasm->wat mod)
  807. (match mod
  808. (($ <wasm> id types imports funcs tables memories globals exports start
  809. elems datas tags strings custom)
  810. ;; Imports
  811. (define (select-imports kind)
  812. (filter (lambda (import)
  813. (eq? (import-kind import) kind))
  814. imports))
  815. (define func-imports (select-imports 'func))
  816. (define global-imports (select-imports 'global))
  817. (define memory-imports (select-imports 'memory))
  818. (define table-imports (select-imports 'table))
  819. ;; Exports
  820. (define (select-exports kind)
  821. (filter-map (match-lambda
  822. (($ <export> name kind* idx)
  823. (and (eq? kind* kind)
  824. (cons idx name))))
  825. exports))
  826. (define func-exports (select-exports 'func))
  827. (define global-exports (select-exports 'global))
  828. (define memory-exports (select-exports 'memory))
  829. (define table-exports (select-exports 'table))
  830. ;; WAT representations
  831. (define (heap-type-repr ht)
  832. (match ht
  833. ((? symbol?) ht)
  834. ((? exact-integer?) ht)))
  835. (define (val-type-repr vt)
  836. (match vt
  837. ((? symbol?) vt)
  838. ((? exact-integer?) vt)
  839. (($ <ref-type> #t ht)
  840. `(ref null ,(heap-type-repr ht)))
  841. (($ <ref-type> #f ht)
  842. `(ref ,(heap-type-repr ht)))))
  843. (define (params-repr params)
  844. (map (match-lambda
  845. (($ <param> id type)
  846. `(param ,@(if id `(,id) '())
  847. ,(val-type-repr type))))
  848. params))
  849. (define (locals-repr locals)
  850. (map (match-lambda
  851. (($ <local> id type)
  852. `(local ,id ,(val-type-repr type))))
  853. locals))
  854. (define (results-repr results)
  855. (map (lambda (type) `(result ,(val-type-repr type))) results))
  856. (define (type-repr type)
  857. (match type
  858. (($ <func-sig> params results)
  859. `(func ,@(params-repr params)
  860. ,@(results-repr results)))
  861. (($ <sub-type> final? supers type)
  862. `(sub ,@(if final? '(final) '())
  863. ,@supers
  864. ,(type-repr type)))
  865. (($ <struct-type> fields)
  866. `(struct ,@(map (match-lambda
  867. (($ <field> #f mutable? type)
  868. (if mutable?
  869. `(mut ,(val-type-repr type))
  870. (val-type-repr type)))
  871. (($ <field> id mutable? type)
  872. `(field ,id
  873. ,(if mutable?
  874. `(mut ,(val-type-repr type))
  875. (val-type-repr type)))))
  876. fields)))
  877. (($ <array-type> mutable? type)
  878. `(array ,(if mutable?
  879. `(mut ,(val-type-repr type))
  880. (val-type-repr type))))))
  881. (define (block-type-repr bt)
  882. (match bt
  883. (#f '())
  884. ((? symbol?) `(,bt))
  885. (($ <type-use> _ ($ <func-sig> _ results))
  886. (results-repr results))
  887. ((? ref-type? rt)
  888. `((result ,(val-type-repr rt))))))
  889. (define (instr-repr instr)
  890. (define (make-prefix-arg prefix x)
  891. (if (zero? x)
  892. '()
  893. (list (string->symbol (string-append prefix (number->string x))))))
  894. (match instr
  895. ;; Instructions that need special handling:
  896. (((and op (or 'i32.load 'i32.load16_s 'i32.load16_u 'i32.load8_s
  897. 'i32.load8_u 'i32.store 'i32.store16 'i32.store8
  898. 'i64.load 'i64.load32_s 'i64.load32_u 'i64.load16_s
  899. 'i64.load16_u 'i64.load8_s 'i64.load8_u 'f32.load
  900. 'f64.load 'i64.store 'i64.store32 'i64.store16
  901. 'i64.store8 'f32.store 'f64.store))
  902. ($ <mem-arg> id offset align))
  903. `(,op ,id
  904. ,@(make-prefix-arg "offset=" offset)
  905. ,@(make-prefix-arg "align=" align)))
  906. (((and op (or 'ref.cast 'ref.test)) ($ <ref-type> null? ht))
  907. `(,op ,@(if null? '(null) '())
  908. ,(heap-type-repr ht)))
  909. (('string.const (? number? id))
  910. `(string.const ,(list-ref strings id)))
  911. (('if label bt consequent alternate)
  912. `(if ,@(block-type-repr bt)
  913. ,@(if label `(,label) '())
  914. (then ,@(instrs-repr consequent))
  915. (else ,@(instrs-repr alternate))))
  916. (((and op (or 'block 'loop)) label bt body)
  917. `(,op ,@(if label `(,label) '())
  918. ,@(block-type-repr bt)
  919. ,@(instrs-repr body)))
  920. (_ instr)))
  921. (define (instrs-repr instrs)
  922. (map instr-repr instrs))
  923. (define (limits-repr limits)
  924. (match limits
  925. (($ <limits> min max)
  926. (if max (list min max) (list min)))))
  927. (define (elem-item-repr init)
  928. (match init
  929. ((instr)
  930. (instr-repr instr))
  931. ((instrs ...)
  932. `(item ,@(instrs-repr init)))))
  933. `(module
  934. ,@(if id `(,id) '())
  935. ;; Types
  936. ,@(filter-map (match-lambda
  937. (($ <type> #f (? func-sig?))
  938. #f)
  939. (($ <type> id val)
  940. `(type ,id ,(type-repr val)))
  941. (($ <rec-group> types)
  942. `(rec
  943. ,@(map (match-lambda
  944. (($ <type> id val)
  945. `(type ,id ,(type-repr val))))
  946. types))))
  947. types)
  948. ;; Imported functions
  949. ,@(map (match-lambda
  950. (($ <import> mod name 'func id
  951. ($ <type-use> _ ($ <func-sig> params results)))
  952. `(func ,id (import ,mod ,name)
  953. ,@(params-repr params)
  954. ,@(results-repr results))))
  955. func-imports)
  956. ;; Imported tables
  957. ,@(map (match-lambda
  958. (($ <import> mod name 'table id
  959. ($ <table-type> limits elem-type))
  960. `(table ,id (import ,mod ,name)
  961. ,@(limits-repr limits)
  962. ,(val-type-repr elem-type))))
  963. table-imports)
  964. ;; Imported memories
  965. ,@(map (match-lambda
  966. (($ <import> mod name 'memory id
  967. ($ <mem-type> limits))
  968. `(memory ,id (import ,mod ,name) ,@(limits-repr limits))))
  969. memory-imports)
  970. ;; Imported globals
  971. ,@(map (match-lambda
  972. (($ <import> mod name 'global id ($ <global-type> mutable? type))
  973. `(global ,id (import ,mod ,name)
  974. ,(if mutable?
  975. `(mut ,(val-type-repr type))
  976. (val-type-repr type)))))
  977. global-imports)
  978. ;; Tables
  979. ,@(map (match-lambda
  980. (($ <table> id ($ <table-type> limits elem-type) init)
  981. `(table ,id
  982. ,@(match (assq-ref table-exports id)
  983. (#f '())
  984. (name `((export ,name))))
  985. ,@(limits-repr limits)
  986. ,(val-type-repr elem-type)
  987. ,@(if init
  988. `((elem ,@(map instrs-repr init)))
  989. '()))))
  990. tables)
  991. ;; Memories
  992. ,@(map (match-lambda
  993. (($ <memory> id ($ <mem-type> limits))
  994. `(memory ,id
  995. ,@(match (assq-ref memory-exports id)
  996. (#f '())
  997. (name `((export ,name))))
  998. ,@(limits-repr limits))))
  999. memories)
  1000. ;; Globals
  1001. ,@(map (match-lambda
  1002. (($ <global> id ($ <global-type> mutable? type) init)
  1003. `(global ,id
  1004. ,@(match (assq-ref global-exports id)
  1005. (#f '())
  1006. (name `((export ,name))))
  1007. ,(if mutable?
  1008. `(mut ,(val-type-repr type))
  1009. (val-type-repr type))
  1010. ,@(instrs-repr init))))
  1011. globals)
  1012. ;; Element segments
  1013. ,@(map (match-lambda
  1014. (($ <elem> id mode table type offset items)
  1015. (match mode
  1016. ('passive
  1017. `(elem ,id ,(val-type-repr type)
  1018. ,@(map elem-item-repr items)))
  1019. ('active
  1020. `(elem ,id (table ,table)
  1021. (offset ,@(instrs-repr offset))
  1022. ,(val-type-repr type)
  1023. ,@(map elem-item-repr items)))
  1024. ('declarative
  1025. `(elem ,id declare
  1026. ,(val-type-repr type)
  1027. ,@(map elem-item-repr items))))))
  1028. elems)
  1029. ;; Data segments
  1030. ,@(map (match-lambda
  1031. (($ <data> id mode mem offset init)
  1032. (match mode
  1033. ('active `(data ,id ,mem ,@(instrs-repr offset) ,init))
  1034. ('passive `(data ,id ,init)))))
  1035. datas)
  1036. ;; Functions
  1037. ,@(map (match-lambda
  1038. (($ <func> id ($ <type-use> _ ($ <func-sig> params results))
  1039. locals body)
  1040. `(func ,id
  1041. ,@(match (assq-ref func-exports id)
  1042. (#f '())
  1043. (name `((export ,name))))
  1044. ,@(params-repr params)
  1045. ,@(results-repr results)
  1046. ,@(locals-repr locals)
  1047. ,@(instrs-repr body))))
  1048. funcs)
  1049. ;; Start function
  1050. ,@(if start `((start ,start)) '())))))