backend.scm 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566
  1. ;;; WebAssembly compiler
  2. ;;; Copyright (C) 2023, 2024 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. ;;; Scheme to WebAssembly compiler.
  20. ;;;
  21. ;;; Code:
  22. (define-module (hoot backend)
  23. #:use-module (ice-9 format)
  24. #:use-module (ice-9 match)
  25. #:use-module ((srfi srfi-1) #:select (append-map alist-delete))
  26. #:use-module (srfi srfi-9)
  27. #:use-module (system base language)
  28. #:use-module (language cps)
  29. #:use-module (language cps intset)
  30. #:use-module (language cps intmap)
  31. #:use-module (language cps dump)
  32. #:use-module (language cps utils)
  33. #:use-module ((language cps hoot)
  34. #:select (hoot-primcall-raw-representations
  35. target-hash
  36. target-symbol-hash
  37. target-keyword-hash))
  38. #:use-module (rnrs bytevectors)
  39. #:use-module (hoot stdlib)
  40. #:use-module (wasm dump)
  41. #:use-module (wasm link)
  42. #:use-module (wasm lower)
  43. #:use-module (wasm types)
  44. #:export (high-level-cps->wasm))
  45. (define (invert-tree parents)
  46. (intmap-fold
  47. (lambda (child parent tree)
  48. (let ((tree (intmap-add tree child empty-intset intset-union)))
  49. (match parent
  50. (-1 tree)
  51. (_ (intmap-add tree parent (intset child) intset-union)))))
  52. parents empty-intmap))
  53. (define (intset-filter pred set)
  54. (persistent-intset
  55. (intset-fold (lambda (i out)
  56. (if (pred i) (intset-add! out i) out))
  57. set empty-intset)))
  58. (define (intset-pop-right set)
  59. (match (intset-prev set)
  60. (#f (values set #f))
  61. (i (values (intset-remove set i) i))))
  62. (define (intmap-map->list f map)
  63. (intmap-fold-right (lambda (k v out) (cons (f k v) out)) map '()))
  64. (define void-block-type (make-type-use #f (make-func-sig '() '())))
  65. (define i32-block-type (make-type-use #f (make-func-sig '() '(i32))))
  66. (define i64-block-type (make-type-use #f (make-func-sig '() '(i64))))
  67. ;; Codegen improvements:
  68. ;;
  69. ;; 1. Eliminating directly-used locals. Compute a set of variables that
  70. ;; are used once, right after they are defined, in such a way that
  71. ;; they can just flow to their use sites on the stack. Avoid
  72. ;; creating locals for these.
  73. ;;
  74. ;; 2. Instruction selection. Could be that some instructions could be
  75. ;; combined or rescheduled. Perhaps this is something on the CPS
  76. ;; level though.
  77. ;;
  78. ;; 3. Optimised local allocation. Do graph coloring to map variables to
  79. ;; a smaller set of locals, to avoid emitting one local per variable.
  80. (define scm-type (make-ref-type #f 'eq))
  81. (define scm-block-type (make-type-use #f (make-func-sig '() (list scm-type))))
  82. (define i31-type (make-ref-type #f 'i31))
  83. (define kvarargs-sig
  84. (make-func-sig (list (make-param '$nargs 'i32)
  85. (make-param '$arg0 scm-type)
  86. (make-param '$arg1 scm-type)
  87. (make-param '$arg2 scm-type))'()))
  88. (define (export-abi wasm)
  89. (define abi-exports
  90. (list (make-export "$arg3" 'global '$arg3)
  91. (make-export "$arg4" 'global '$arg4)
  92. (make-export "$arg5" 'global '$arg5)
  93. (make-export "$arg6" 'global '$arg6)
  94. (make-export "$arg7" 'global '$arg7)
  95. (make-export "$argv" 'table '$argv)
  96. (make-export "$raw-sp" 'global '$raw-sp)
  97. (make-export "$scm-sp" 'global '$scm-sp)
  98. (make-export "$ret-sp" 'global '$ret-sp)
  99. (make-export "$dyn-sp" 'global '$dyn-sp)
  100. (make-export "$current-fluids" 'global '$current-fluids)
  101. (make-export "$raise-exception" 'global '$raise-exception)
  102. (make-export "$with-exception-handler" 'global '$with-exception-handler)
  103. (make-export "$current-input-port" 'global '$current-input-port)
  104. (make-export "$current-output-port" 'global '$current-output-port)
  105. (make-export "$current-error-port" 'global '$current-error-port)
  106. (make-export "$default-prompt-tag" 'global '$default-prompt-tag)
  107. (make-export "$raw-stack" 'memory '$raw-stack)
  108. (make-export "$scm-stack" 'table '$scm-stack)
  109. (make-export "$ret-stack" 'table '$ret-stack)
  110. (make-export "$dyn-stack" 'table '$dyn-stack)
  111. (make-export "$intern-symbol!" 'func '$intern-symbol!)
  112. (make-export "$intern-keyword!" 'func '$intern-keyword!)
  113. (make-export "$make-size-error" 'global '$make-size-error)
  114. (make-export "$make-index-error" 'global '$make-index-error)
  115. (make-export "$make-range-error" 'global '$make-range-error)
  116. (make-export "$make-start-offset-error" 'global '$make-start-offset-error)
  117. (make-export "$make-end-offset-error" 'global '$make-end-offset-error)
  118. (make-export "$make-type-error" 'global '$make-type-error)
  119. (make-export "$make-unimplemented-error" 'global '$make-unimplemented-error)
  120. (make-export "$make-assertion-error" 'global '$make-assertion-error)
  121. (make-export "$make-not-seekable-error" 'global '$make-not-seekable-error)
  122. (make-export "$make-runtime-error-with-message" 'global '$make-runtime-error-with-message)
  123. (make-export "$make-runtime-error-with-message+irritants" 'global '$make-runtime-error-with-message+irritants)
  124. (make-export "$make-match-error" 'global '$make-match-error)
  125. (make-export "$make-arity-error" 'global '$make-arity-error)))
  126. (define (add-export export exports)
  127. (cons export exports))
  128. (match wasm
  129. (($ <wasm> id types imports funcs tables memories globals exports
  130. start elems datas tags strings custom)
  131. (make-wasm id types imports funcs tables memories globals
  132. (reverse (fold1 add-export abi-exports (reverse exports)))
  133. start elems datas tags strings custom))))
  134. (define (target-hashq x)
  135. (target-hash x))
  136. (define-record-type <static-procedure>
  137. (make-static-procedure code)
  138. static-procedure?
  139. (code static-procedure-code))
  140. (define (compute-used-vars cps)
  141. (define (adjoin var used)
  142. (intset-add! used var))
  143. (define (adjoin* vars used)
  144. (fold1 adjoin vars used))
  145. (persistent-intset
  146. (intmap-fold
  147. (lambda (k cont used)
  148. ;; Only a term can use a var.
  149. (match cont
  150. (($ $kargs names syms term)
  151. (match term
  152. (($ $continue k src exp)
  153. (match exp
  154. (($ $call proc args)
  155. (adjoin* args (adjoin proc used)))
  156. (($ $callk k proc args)
  157. (adjoin* args (if proc (adjoin proc used) used)))
  158. (($ $calli args callee)
  159. (adjoin* args (adjoin callee used)))
  160. (($ $primcall name param args)
  161. (adjoin* args used))
  162. (($ $values args)
  163. (adjoin* args used))
  164. ((or ($ $const) ($ $const-fun) ($ $prim) ($ $code)) used)))
  165. (($ $branch kf kt src op param args)
  166. (adjoin* args used))
  167. (($ $switch kf kt* src arg)
  168. (adjoin arg used))
  169. (($ $prompt k kh src escape? tag)
  170. (adjoin tag used))
  171. (($ $throw src op param args)
  172. (adjoin* args used))))
  173. (_ used)))
  174. cps empty-intset)))
  175. (define (compute-join-vars cps preds)
  176. (define (adjoin var joins)
  177. (intset-add! joins var))
  178. (define (adjoin* vars joins)
  179. (fold1 adjoin vars joins))
  180. (define (join-cont? k)
  181. (< 1 (fold1 (lambda (pred count)
  182. (if (< pred k) (1+ count) count))
  183. (intmap-ref preds k)
  184. 0)))
  185. (persistent-intset
  186. (intmap-fold
  187. (lambda (k cont joins)
  188. (match cont
  189. (($ $kargs names syms)
  190. (if (and (pair? syms) (join-cont? k))
  191. (adjoin* syms joins)
  192. joins))
  193. (_ joins)))
  194. cps empty-intset)))
  195. (define* (lower-to-wasm cps #:key import-abi?)
  196. (define max-args 0)
  197. ;; interning constants into constant table
  198. ;; finalizing constant table
  199. ;; setting init function.
  200. (define imports '())
  201. (define datas '())
  202. (define data-count 0)
  203. (define (intern-data! bv)
  204. (let ((name (string->symbol
  205. (format #f "$data~a" data-count))))
  206. (set! data-count (1+ data-count))
  207. (set! datas (cons (make-data name 'passive #f #f bv) datas))
  208. name))
  209. (define heap-constants '())
  210. (define heap-constant-count 0)
  211. (define heap-constant-names (make-hash-table))
  212. (define (intern-heap-constant! x)
  213. (define* (intern! type init-expr #:optional make-reloc)
  214. (let ((name (string->symbol
  215. (format #f "$constant~a" heap-constant-count))))
  216. (set! heap-constant-count (1+ heap-constant-count))
  217. (hash-set! heap-constant-names x name)
  218. (define entry (vector name type init-expr make-reloc))
  219. (set! heap-constants (cons entry heap-constants))
  220. name))
  221. (match x
  222. ((car . cdr)
  223. (intern! (make-ref-type #f '$pair)
  224. `((i32.const ,(target-hashq x))
  225. ,@(compile-constant car)
  226. ,@(compile-constant cdr)
  227. (struct.new $pair))))
  228. (#(elt ...)
  229. (intern! (make-ref-type #f '$vector)
  230. `((i32.const ,(target-hashq x))
  231. ,@(append-map compile-constant elt)
  232. (array.new_fixed $raw-scmvector ,(vector-length x))
  233. (struct.new $vector))))
  234. ((? bytevector?)
  235. (let ((name (intern-data! x)))
  236. (intern! (make-ref-type #f '$bytevector)
  237. `((i32.const ,(target-hashq x))
  238. (i32.const 0)
  239. (i32.const ,(bytevector-length x))
  240. (array.new_data $raw-bytevector ,name)
  241. (struct.new $bytevector)))))
  242. ((? bitvector?)
  243. ;; FIXME: Probably we should put the initializers in the data
  244. ;; section instead of using new_fixed.
  245. (intern! (make-ref-type #f '$bitvector)
  246. `((i32.const ,(target-hashq x))
  247. (i32.const ,(bitvector-length x))
  248. ,@(let* ((u32v (uniform-array->bytevector x))
  249. (u32len (/ (bytevector-length u32v) 4)))
  250. (unless (eq? (native-endianness) (endianness little))
  251. (error "unsupported"))
  252. (let lp ((i 0))
  253. (if (< i u32len)
  254. (cons `(i32.const
  255. ,(bytevector-s32-native-ref u32v (* i 4)))
  256. (lp (1+ i)))
  257. '())))
  258. (array.new_fixed $raw-bitvector
  259. ,(ash (+ 31 (bitvector-length x)) -5))
  260. (struct.new $bitvector))))
  261. ((? string?)
  262. (intern! (make-ref-type #f '$string)
  263. `((i32.const ,(target-hashq x))
  264. (string.const ,x)
  265. (struct.new $string))))
  266. (($ <static-procedure> code)
  267. (intern! (make-ref-type #f '$proc)
  268. `((i32.const 0)
  269. (ref.func ,code)
  270. (struct.new $proc))))
  271. ((? symbol?)
  272. ;; We need to add the symbol to the symbol table. In the case
  273. ;; where we're not importing ABI, the actual global is known to
  274. ;; be constant at compile-time, and we just emit a call to
  275. ;; $intern-symbol!. Otherwise we call $intern-symbol! as part of
  276. ;; the initializer, and rely on lower-globals pass to move this
  277. ;; non-constant initialization to start.
  278. (intern! (make-ref-type #f '$symbol)
  279. `((i32.const ,(target-symbol-hash (symbol->string x)))
  280. ,@(compile-constant (symbol->string x))
  281. (struct.new $symbol)
  282. . ,(if import-abi? `((call $intern-symbol!)) '()))
  283. (and (not import-abi?)
  284. (lambda (name)
  285. `((global.get ,name)
  286. (call $intern-symbol!)
  287. (drop))))))
  288. ((? keyword?)
  289. ;; Same run-time interning considerations as symbols.
  290. (intern! (make-ref-type #f '$keyword)
  291. `((i32.const ,(target-keyword-hash
  292. (symbol->string (keyword->symbol x))))
  293. ,@(compile-constant (keyword->symbol x))
  294. (struct.new $keyword)
  295. . ,(if import-abi? `((call $intern-keyword!)) '()))
  296. (and (not import-abi?)
  297. (lambda (name)
  298. `((global.get ,name)
  299. (call $intern-keyword!)
  300. (drop))))))
  301. ((? number?)
  302. (if (exact? x)
  303. (if (integer? x)
  304. (intern! (make-ref-type #f '$bignum)
  305. `((i32.const 0)
  306. (string.const ,(number->string x))
  307. (call $string->bignum)
  308. (struct.new $bignum)))
  309. (intern! (make-ref-type #f '$fraction)
  310. `((i32.const 0)
  311. ,@(compile-constant (numerator x))
  312. ,@(compile-constant (denominator x))
  313. (struct.new $fraction))))
  314. (if (real? x)
  315. (intern! (make-ref-type #f '$flonum)
  316. `((i32.const 0)
  317. (f64.const ,x)
  318. (struct.new $flonum)))
  319. (intern! (make-ref-type #f '$complex)
  320. `((i32.const 0)
  321. (f64.const ,(real-part x))
  322. (f64.const ,(imag-part x))
  323. (struct.new $complex))))))
  324. (_ (error "unrecognized constant" x))))
  325. (define (compile-heap-constant val)
  326. (let ((name (or (hash-ref heap-constant-names val)
  327. (intern-heap-constant! val))))
  328. `((global.get ,name))))
  329. (define (compile-immediate-constant val)
  330. (define (fixnum? val)
  331. (and (exact-integer? val)
  332. (<= (ash -1 29) val (1- (ash 1 29)))))
  333. (match val
  334. ((? fixnum?) `((i32.const ,(ash val 1))
  335. (ref.i31)))
  336. (#f `((i32.const 1) (ref.i31)))
  337. ((? (lambda (x) (eq? x #nil))) `((i32.const 5) (ref.i31)))
  338. ((? (lambda (x) (eq? x '()))) `((i32.const 13) (ref.i31)))
  339. (#t `((i32.const 17) (ref.i31)))
  340. ((? unspecified?) `((i32.const 33) (ref.i31)))
  341. ((? eof-object?) `((i32.const 41) (ref.i31)))
  342. ((? char?) `((i32.const ,(logior (ash (char->integer val) 2)
  343. #b11))
  344. (ref.i31)))
  345. (_ #f)))
  346. (define (compile-constant val)
  347. (or (compile-immediate-constant val)
  348. (compile-heap-constant val)))
  349. (define closure-types empty-intmap)
  350. (define (closure-type nfree)
  351. (if (zero? nfree)
  352. '$proc
  353. (or (intmap-ref closure-types nfree (lambda (_) #f))
  354. (let ((name (string->symbol (format #f "$closure~a" nfree))))
  355. (set! closure-types (intmap-add closure-types nfree name))
  356. name))))
  357. (define (make-closure-type nfree name)
  358. (define fields (make-list nfree (make-field #f #t scm-type)))
  359. (define struct-type
  360. (make-struct-type (cons* (make-field '$hash #t 'i32)
  361. (make-field '$proc #f
  362. (make-ref-type #f '$kvarargs))
  363. fields)))
  364. (make-type name (make-sub-type #t '($proc) struct-type)))
  365. (define max-struct-nfields 0)
  366. (define (struct-type-name nfields)
  367. (cond
  368. ((zero? nfields) '$struct)
  369. (else
  370. (when (< max-struct-nfields nfields)
  371. (set! max-struct-nfields nfields))
  372. (string->symbol (format #f "$struct/~a" nfields)))))
  373. (define (make-struct-types)
  374. ;; The first few struct types are part of the rec block, because
  375. ;; vtables are structs. See vtable-fields in stdlib.scm.
  376. (define vtable-nfields 8)
  377. (let lp ((n (1+ vtable-nfields)))
  378. (if (<= n max-struct-nfields)
  379. (let ((name (struct-type-name n))
  380. (parent (struct-type-name (1- n))))
  381. (define fields (make-list n (make-field #f #t scm-type)))
  382. (define struct-type
  383. (make-struct-type (cons* (make-field '$hash #t 'i32)
  384. (make-field '$vtable #t
  385. (make-ref-type #t '$vtable))
  386. fields)))
  387. (define sub-type
  388. (make-sub-type #f (list parent) struct-type))
  389. (cons (make-type name sub-type) (lp (1+ n))))
  390. '())))
  391. (define (func-label k) (string->symbol (format #f "$f~a" k)))
  392. (define (known-arity k)
  393. (match (intmap-ref cps k)
  394. (($ $kfun src meta self ktail kentry)
  395. (match (intmap-ref cps kentry)
  396. (($ $kclause) #f)
  397. (($ $kargs names vars) (if self (cons self vars) vars))))))
  398. (define (lower-func kfun body)
  399. (let ((cps (intmap-select cps body)))
  400. (define has-closure?
  401. (match (intmap-ref cps kfun)
  402. (($ $kfun src meta self ktail kentry) self)))
  403. (define elide-arity-check?
  404. (match (intmap-ref cps kfun)
  405. (($ $kfun src meta self ktail kentry)
  406. (assq-ref meta 'elide-arity-check?))))
  407. (define func-name
  408. (match (intmap-ref cps kfun)
  409. (($ $kfun src meta self ktail kentry)
  410. (assq-ref meta 'name))))
  411. (define used-vars (compute-used-vars cps))
  412. (define (var-used? var) (intset-ref used-vars var))
  413. (define preds (compute-predecessors cps kfun))
  414. (define join-vars (compute-join-vars cps preds))
  415. (define idoms (compute-idoms cps kfun))
  416. (define dom-children (invert-tree idoms))
  417. (define (merge-cont? label)
  418. (let lp ((preds (intmap-ref preds label))
  419. (has-forward-in-edge? #f))
  420. (match preds
  421. (() #f)
  422. ((pred . preds)
  423. (if (< pred label)
  424. (or has-forward-in-edge?
  425. (lp preds #t))
  426. (lp preds has-forward-in-edge?))))))
  427. (define (loop-cont? label)
  428. (or-map (lambda (pred) (<= label pred))
  429. (intmap-ref preds label)))
  430. (define (loop-label label)
  431. (string->symbol (format #f "$l~a" label)))
  432. (define (wrap-loop expr label)
  433. (if (loop-cont? label)
  434. `(loop ,(loop-label label) ,void-block-type ,expr)
  435. expr))
  436. (define (var-label var) (string->symbol (format #f "$v~a" var)))
  437. (define (local.get var) `(local.get ,(var-label var)))
  438. (define (local.set var) `(local.set ,(var-label var)))
  439. (define (local-arg-label idx) (string->symbol (format #f "$arg~a" idx)))
  440. (define (global-arg-label idx) (string->symbol (format #f "$arg~a" idx)))
  441. (define (arg-ref idx)
  442. (cond
  443. ((< idx 3) `((local.get ,(local-arg-label idx))))
  444. ((< idx 8) `((global.get ,(global-arg-label idx))))
  445. (else `((i32.const ,(- idx 8))
  446. (table.get $argv)
  447. ref.as_non_null))))
  448. (define (compile-tail exp)
  449. (define (pass-abi-arguments args)
  450. (cons
  451. `(i32.const ,(length args))
  452. (let lp ((args args) (idx 0))
  453. (match args
  454. (()
  455. (if (< idx 3)
  456. (append '((i32.const 0)
  457. (ref.i31))
  458. (lp args (1+ idx)))
  459. '()))
  460. ((arg . args)
  461. (append (cond
  462. ((< idx 3)
  463. `(,(local.get arg)))
  464. ((< idx 8)
  465. `(,(local.get arg)
  466. (global.set ,(global-arg-label idx))))
  467. (else
  468. `((i32.const ,(- idx 8))
  469. ,(local.get arg)
  470. (table.set $argv))))
  471. (lp args (1+ idx))))))))
  472. ;; Keep track of the maximum number of arguments passed in a
  473. ;; single call so that we can size the $argv table
  474. ;; appropriately in the start function.
  475. (define (update-max-args args)
  476. (set! max-args (max max-args (length args))))
  477. (match exp
  478. (($ $call proc args)
  479. (update-max-args args)
  480. `(,@(pass-abi-arguments (cons proc args))
  481. ,(local.get proc)
  482. (ref.cast ,(make-ref-type #f '$proc))
  483. (struct.get $proc 1)
  484. (return_call_ref $kvarargs)))
  485. (($ $calli args callee)
  486. (update-max-args args)
  487. ;; This is a return.
  488. `(,@(pass-abi-arguments args)
  489. ,(local.get callee)
  490. (return_call_ref $kvarargs)))
  491. (($ $callk k proc args)
  492. (update-max-args args)
  493. (let ((args (if proc (cons proc args) args)))
  494. `(,@(if (known-arity k)
  495. (map local.get args)
  496. (pass-abi-arguments args))
  497. (return_call ,(func-label k)))))))
  498. (define-syntax-rule (match-primcall name param args
  499. ((%name %param . %args) . body) ...)
  500. (match name
  501. (%name (match (cons param args) ((%param . %args) . body)))
  502. ...))
  503. (define (analyze-saved-vars reprs)
  504. (define (save/raw memory sp maybe-grow-stack idx store-inst alignment)
  505. (lambda (var sizes)
  506. (define (prepare-save)
  507. `((global.get ,sp)
  508. (local.tee ,sp)
  509. (i32.const ,(assq-ref sizes sp))
  510. (i32.add)
  511. (global.set ,sp)
  512. (call ,maybe-grow-stack)))
  513. `(,@(if (zero? idx) (prepare-save) '())
  514. (local.get ,sp)
  515. (local.get ,var)
  516. (,store-inst ,(make-mem-arg memory idx alignment)))))
  517. (define (save/ref table sp maybe-grow-stack idx)
  518. (lambda (var sizes)
  519. (define (prepare-save)
  520. `((global.get ,sp)
  521. (local.tee ,sp)
  522. (i32.const ,(assq-ref sizes sp))
  523. (i32.add)
  524. (global.set ,sp)
  525. (call ,maybe-grow-stack)))
  526. `(,@(if (zero? idx) (prepare-save) '())
  527. (local.get ,sp)
  528. ,@(if (zero? idx) '() `((i32.const ,idx) (i32.add)))
  529. (local.get ,var)
  530. (table.set ,table))))
  531. (define (restore sp idx code)
  532. (lambda (sizes)
  533. (define (prepare-restore)
  534. `((global.get ,sp)
  535. (i32.const ,(assq-ref sizes sp))
  536. (i32.sub)
  537. (local.tee ,sp)
  538. (global.set ,sp)))
  539. `(,@(if (zero? idx) (prepare-restore) '())
  540. (local.get ,sp)
  541. ,@code)))
  542. (define (restore/raw memory sp idx load-inst alignment)
  543. (restore sp idx `((,load-inst ,(make-mem-arg memory idx alignment)))))
  544. (define (restore/ref table sp idx cast)
  545. (restore sp idx `(,@(if (zero? idx) '() `((i32.const ,idx) (i32.add)))
  546. (table.get ,table)
  547. ,@cast)))
  548. (define (visit/raw idx store-inst load-inst alignment)
  549. (cons (save/raw '$raw-stack '$raw-sp '$maybe-grow-raw-stack
  550. idx store-inst alignment)
  551. (restore/raw '$raw-stack '$raw-sp idx load-inst alignment)))
  552. (define (visit/ref table sp grow idx restore-cast)
  553. (cons (save/ref table sp grow idx)
  554. (restore/ref table sp idx restore-cast)))
  555. (define (visit-i64 idx) (visit/raw idx 'i64.store 'i64.load 3))
  556. (define (visit-f64 idx) (visit/raw idx 'f64.store 'f64.load 3))
  557. (define (visit-scm idx)
  558. (visit/ref '$scm-stack '$scm-sp '$maybe-grow-scm-stack idx
  559. '((ref.as_non_null))))
  560. (define (visit-raw-bytevector idx)
  561. (visit/ref '$scm-stack '$scm-sp '$maybe-grow-scm-stack idx
  562. `((ref.cast ,(make-ref-type #f '$raw-bytevector)))))
  563. (define (visit-ret idx)
  564. (visit/ref '$ret-stack '$ret-sp '$maybe-grow-ret-stack idx
  565. '((ref.as_non_null))))
  566. (let lp ((reprs reprs) (out '())
  567. (raw-size 0) (scm-size 0) (ret-size 0))
  568. (match reprs
  569. (()
  570. (values (reverse out)
  571. `(($raw-sp . ,raw-size)
  572. ($scm-sp . ,scm-size)
  573. ($ret-sp . ,ret-size))))
  574. ((r . reprs)
  575. (match r
  576. ((or 'u64 's64)
  577. (lp reprs
  578. (cons (visit-i64 raw-size) out)
  579. (+ raw-size 8) scm-size ret-size))
  580. ('f64
  581. (lp reprs
  582. (cons (visit-f64 raw-size) out)
  583. (+ raw-size 8) scm-size ret-size))
  584. ('scm
  585. (lp reprs
  586. (cons (visit-scm scm-size) out)
  587. raw-size (1+ scm-size) ret-size))
  588. ('bv-contents
  589. (lp reprs
  590. (cons (visit-raw-bytevector scm-size) out)
  591. raw-size (1+ scm-size) ret-size))
  592. ('code
  593. (lp reprs
  594. (cons (visit-ret ret-size) out)
  595. raw-size scm-size (1+ ret-size))))))))
  596. (define (compile-fixnum-fast-path a block-type
  597. fast-expr slow-expr)
  598. `((block #f ,block-type
  599. ((block #f ,void-block-type
  600. (,(local.get a)
  601. (ref.test ,i31-type)
  602. (i32.eqz)
  603. (br_if 0)
  604. ,(local.get a)
  605. (ref.cast ,i31-type)
  606. (i31.get_s)
  607. (local.tee $i0)
  608. (i32.const 1)
  609. (i32.and)
  610. (br_if 0)
  611. ,@fast-expr
  612. (br 1)))
  613. ,(local.get a)
  614. ,@slow-expr))))
  615. (define* (compile-fixnum-fixnum-fast-path a b block-type
  616. fast-expr slow-expr
  617. #:key (fast-checks '()))
  618. `((block #f ,block-type
  619. ((block #f ,void-block-type
  620. (,(local.get a)
  621. (ref.test ,i31-type)
  622. (i32.eqz)
  623. (br_if 0)
  624. ,(local.get b)
  625. (ref.test ,i31-type)
  626. (i32.eqz)
  627. (br_if 0)
  628. ,(local.get a)
  629. (ref.cast ,i31-type)
  630. (i31.get_s)
  631. (local.tee $i0)
  632. ,(local.get b)
  633. (ref.cast ,i31-type)
  634. (i31.get_s)
  635. (local.tee $i1)
  636. (i32.or)
  637. (i32.const 1)
  638. (i32.and)
  639. (br_if 0)
  640. ,@(append-map (lambda (checks)
  641. `(,@checks (br_if 0)))
  642. fast-checks)
  643. ,@fast-expr
  644. (br 1)))
  645. ,(local.get a)
  646. ,(local.get b)
  647. ,@slow-expr))))
  648. (define* (compile-fixnum-u64-fast-path a b block-type
  649. fast-expr slow-expr
  650. #:key (fast-checks '()))
  651. `((block #f ,block-type
  652. ((block #f ,void-block-type
  653. (,(local.get a)
  654. (ref.test ,i31-type)
  655. (i32.eqz)
  656. (br_if 0)
  657. ,(local.get a)
  658. (ref.cast ,i31-type)
  659. (i31.get_s)
  660. (local.tee $i0)
  661. (i32.const 1)
  662. (i32.and)
  663. (br_if 0)
  664. ,@(append-map (lambda (checks)
  665. `(,@checks (br_if 0)))
  666. fast-checks)
  667. ,@fast-expr
  668. (br 1)))
  669. ,(local.get a)
  670. ,(local.get b)
  671. ,@slow-expr))))
  672. (define (compile-values exp)
  673. (match exp
  674. (($ $const val) (compile-constant val))
  675. (($ $const-fun k) (compile-constant
  676. (make-static-procedure (func-label k))))
  677. (($ $values vals)
  678. (map local.get vals))
  679. (($ $code k)
  680. `((ref.func ,(func-label k))))
  681. (($ $prim name)
  682. (match name
  683. ('values `((global.get $values-primitive)))
  684. ('apply `((global.get $apply-primitive)))
  685. ('append `((global.get $append-primitive)))
  686. ('abort-to-prompt '((global.get $abort-to-prompt-primitive)))
  687. ('raise-exception '((global.get $raise-exception)))
  688. (_ (error "unhandled $prim" name))))
  689. (($ $primcall name param args)
  690. (match-primcall
  691. name param args
  692. (('inline-wasm
  693. ($ <func> id
  694. ($ <type-use> #f ($ <func-sig> params results))
  695. locals body)
  696. . args)
  697. (define named-locals
  698. (map
  699. (lambda (param arg)
  700. (match param
  701. (($ <param> id type)
  702. (cons id (match type
  703. (($ <ref-type> #f 'string)
  704. `(,(local.get arg)
  705. (ref.cast ,(make-ref-type #f '$string))
  706. (struct.get $string $str)))
  707. (($ <ref-type> #t 'extern)
  708. `(,(local.get arg)
  709. (ref.cast ,(make-ref-type #f '$extern-ref))
  710. (struct.get $extern-ref $val)))
  711. (($ <ref-type> #f 'eq)
  712. `(,(local.get arg)))
  713. (($ <ref-type>)
  714. `(,(local.get arg)
  715. (ref.cast ,type)))
  716. ('i32 `(,(local.get arg) (i32.wrap_i64)))
  717. ('i64 `(,(local.get arg)))
  718. ('f32 `(,(local.get arg) (f32.demote_f64)))
  719. ('f64 `(,(local.get arg)))
  720. (_ (error "unexpected param type" type)))))))
  721. params args))
  722. (define temp-locals
  723. (map (lambda (local)
  724. (match local
  725. (($ <local> id type)
  726. (unless (equal? type (type-for-named-temporary id))
  727. (error "unexpected local type for name" id type))
  728. id)))
  729. locals))
  730. (define (rename-inst inst)
  731. (match inst
  732. (((and inst (or 'block 'loop)) label type body)
  733. `((,inst ,label ,type ,(rename-expr body))))
  734. (('if label type consequent alternate)
  735. `((if ,label ,type
  736. ,(rename-expr consequent) ,(rename-expr alternate))))
  737. (('try label type body catches catch-all)
  738. `((try ,label ,type ,(rename-expr body)
  739. ,(map rename-expr catches)
  740. ,(and catch-all (rename-expr catch-all)))))
  741. (('try_delegate label type body handler)
  742. `((try_delegate ,label ,type ,(rename-expr body) ,handler)))
  743. (('local.get id)
  744. (or (assq-ref named-locals id)
  745. (begin
  746. (unless (memq id temp-locals)
  747. (error "unexpected local" id))
  748. `((local.get ,id)))))
  749. (((or 'local.set 'local.tee) id)
  750. ;; Only temporary locals are assignable from inline wasm.
  751. (unless (memq id temp-locals)
  752. (error "unexpected local" id))
  753. (list inst))
  754. (inst (list inst))))
  755. (define (rename-expr expr)
  756. (append-map rename-inst expr))
  757. (rename-expr body))
  758. (('wasm-import (? import? import))
  759. (set! imports (cons import imports))
  760. '())
  761. ;; These are the primcalls inserted by tailification to
  762. ;; handle stack-allocated return continuations.
  763. (('save reprs . args)
  764. (call-with-values (lambda () (analyze-saved-vars reprs))
  765. (lambda (analyzed spill-sizes)
  766. (define (save-var analyzed var)
  767. (match analyzed
  768. ((save . restore)
  769. (save (var-label var) spill-sizes))))
  770. (append-map save-var analyzed args))))
  771. (('drop reprs)
  772. (call-with-values (lambda () (analyze-saved-vars reprs))
  773. (lambda (analyzed spill-sizes)
  774. (define pop-sp
  775. (match-lambda
  776. ((sp . size)
  777. (if (zero? size)
  778. '()
  779. `((global.get ,sp)
  780. (i32.const ,size)
  781. (i32.sub)
  782. (global.set ,sp))))))
  783. (append-map pop-sp spill-sizes))))
  784. (('restore reprs)
  785. ;; Precondition: the order of the vars (and associated
  786. ;; reprs) is the same as for the corresponding save.
  787. (call-with-values (lambda () (analyze-saved-vars reprs))
  788. (lambda (analyzed spill-sizes)
  789. (define restore-var
  790. (match-lambda
  791. ((save . restore)
  792. (restore spill-sizes))))
  793. (append-map restore-var analyzed))))
  794. ;; Primcalls related to the module system. Not sure if we
  795. ;; will need these, at least in this form; the whole-program
  796. ;; compiler may take a different approach to modules.
  797. (('current-module #f)
  798. (error "unimplemented" exp))
  799. (('current-thread #f)
  800. (error "unimplemented" exp))
  801. (('define! #f mod sym)
  802. (error "unimplemented" exp))
  803. (('resolve (bound?) (name))
  804. (error "unimplemented" exp))
  805. (('cache-ref key)
  806. (error "unimplemented" exp))
  807. (('cache-set! key val)
  808. (error "unimplemented" exp))
  809. (('resolve-module public? (name))
  810. (error "unimplemented" exp))
  811. (('module-variable #f mod name)
  812. (error "unimplemented" exp))
  813. (('lookup #f mod name)
  814. (error "unimplemented" exp))
  815. (('lookup-bound #f mod name)
  816. (error "unimplemented" exp))
  817. (('lookup-bound-public (mod name))
  818. (error "unimplemented" exp))
  819. (('lookup-bound-private (mod name))
  820. (error "unimplemented" exp))
  821. ;; Object allocation. Probably most of these need to be
  822. ;; replaced with `cons` et al; see log.md.
  823. (('cons #f head tail)
  824. `((i32.const 0)
  825. ,(local.get head)
  826. ,(local.get tail)
  827. (struct.new $mutable-pair)))
  828. (('car #f pair)
  829. `(,(local.get pair)
  830. (ref.cast ,(make-ref-type #f '$pair))
  831. (struct.get $pair $car)))
  832. (('cdr #f pair)
  833. `(,(local.get pair)
  834. (ref.cast ,(make-ref-type #f '$pair))
  835. (struct.get $pair $cdr)))
  836. (('set-car! #f pair val)
  837. `(,(local.get pair)
  838. (ref.cast ,(make-ref-type #f '$pair))
  839. ,(local.get val)
  840. (struct.set $pair $car)))
  841. (('set-cdr! #f pair val)
  842. `(,(local.get pair)
  843. (ref.cast ,(make-ref-type #f '$pair))
  844. ,(local.get val)
  845. (struct.set $pair $cdr)))
  846. (('allocate-vector/immediate len)
  847. `((i32.const 0)
  848. ,@(compile-constant #f)
  849. (i32.const ,len)
  850. (array.new $raw-scmvector)
  851. (struct.new $mutable-vector)))
  852. (('allocate-vector #f len)
  853. `((i32.const 0)
  854. ,@(compile-constant #f)
  855. ,(local.get len)
  856. (i32.wrap_i64)
  857. (array.new $raw-scmvector)
  858. (struct.new $mutable-vector)))
  859. (('vector-length #f v)
  860. `(,(local.get v)
  861. (ref.cast ,(make-ref-type #f '$vector))
  862. (struct.get $vector $vals)
  863. (array.len)
  864. (i64.extend_i32_u)))
  865. (('vector-ref #f v idx)
  866. `(,(local.get v)
  867. (ref.cast ,(make-ref-type #f '$vector))
  868. (struct.get $vector $vals)
  869. ,(local.get idx)
  870. (i32.wrap_i64)
  871. (array.get $raw-scmvector)))
  872. (('vector-ref/immediate idx v)
  873. `(,(local.get v)
  874. (ref.cast ,(make-ref-type #f '$vector))
  875. (struct.get $vector $vals)
  876. (i32.const ,idx)
  877. (array.get $raw-scmvector)))
  878. (('vector-set! #f v idx val)
  879. `(,(local.get v)
  880. (ref.cast ,(make-ref-type #f '$vector))
  881. (struct.get $vector $vals)
  882. ,(local.get idx)
  883. (i32.wrap_i64)
  884. ,(local.get val)
  885. (array.set $raw-scmvector)))
  886. (('vector-set!/immediate idx v val)
  887. `(,(local.get v)
  888. (ref.cast ,(make-ref-type #f '$vector))
  889. (struct.get $vector $vals)
  890. (i32.const ,idx)
  891. ,(local.get val)
  892. (array.set $raw-scmvector)))
  893. (('box #f val)
  894. `((i32.const 0)
  895. ,(local.get val)
  896. (struct.new $variable)))
  897. (('box-ref #f v)
  898. `(,(local.get v)
  899. (ref.cast ,(make-ref-type #f '$variable))
  900. (struct.get $variable $val)))
  901. (('box-set! #f v val)
  902. `(,(local.get v)
  903. (ref.cast ,(make-ref-type #f '$variable))
  904. ,(local.get val)
  905. (struct.set $variable $val)))
  906. (('bv-length #f bv)
  907. `(,(local.get bv)
  908. (ref.cast ,(make-ref-type #f '$bytevector))
  909. (struct.get $bytevector $vals)
  910. (array.len)
  911. (i64.extend_i32_u)))
  912. (('bv-contents #f bv)
  913. `(,(local.get bv)
  914. (ref.cast ,(make-ref-type #f '$bytevector))
  915. (struct.get $bytevector $vals)))
  916. (('make-closure nfree code)
  917. `((i32.const 0)
  918. ,(local.get code)
  919. ,@(append-map (lambda (_) '((i32.const 0) (ref.i31)))
  920. (iota nfree))
  921. (struct.new ,(closure-type nfree))))
  922. (('closure-ref (idx . nfree) closure)
  923. (let ((type (closure-type nfree))
  924. ;; Two header fields: hash and proc.
  925. (pos (+ idx 2)))
  926. `(,(local.get closure)
  927. (ref.cast ,(make-ref-type #f type))
  928. (struct.get ,type ,pos))))
  929. (('closure-set! (idx . nfree) closure val)
  930. (let ((type (closure-type nfree))
  931. ;; Two header fields: hash and proc.
  932. (pos (+ idx 2)))
  933. `(,(local.get closure)
  934. (ref.cast ,(make-ref-type #f type))
  935. ,(local.get val)
  936. (struct.set ,type ,pos))))
  937. (('struct-vtable #f struct)
  938. `(,(local.get struct)
  939. (ref.cast ,(make-ref-type #f '$struct))
  940. (struct.get $struct $vtable)
  941. (ref.as_non_null)))
  942. (('vtable-size #f struct)
  943. `(,(local.get struct)
  944. (ref.cast ,(make-ref-type #f '$vtable))
  945. (struct.get $vtable $nfields)
  946. (ref.cast ,i31-type)
  947. (i31.get_s)
  948. (i32.const 1)
  949. (i32.shr_s)
  950. (i64.extend_i32_u)))
  951. (('allocate-struct nfields vtable)
  952. `((i32.const 0)
  953. ,(local.get vtable)
  954. (ref.cast ,(make-ref-type #f '$vtable))
  955. ,@(append-map (lambda (_) '((i32.const 0) (ref.i31)))
  956. (iota nfields))
  957. (struct.new ,(struct-type-name nfields))))
  958. (('struct-ref idx struct)
  959. (let ((type (struct-type-name (1+ idx)))
  960. ;; Two header fields: hash and vtable.
  961. (pos (+ idx 2)))
  962. `(,(local.get struct)
  963. (ref.cast ,(make-ref-type #f type))
  964. (struct.get ,type ,pos))))
  965. (('struct-set! idx struct val)
  966. (let ((type (struct-type-name (1+ idx)))
  967. ;; Two header fields: hash and vtable.
  968. (pos (+ idx 2)))
  969. `(,(local.get struct)
  970. (ref.cast ,(make-ref-type #f type))
  971. ,(local.get val)
  972. (struct.set ,type ,pos))))
  973. (('string-length #f str)
  974. `(,(local.get str)
  975. (ref.cast ,(make-ref-type #f '$string))
  976. (struct.get $string $str)
  977. (string.as_iter)
  978. (i32.const #x7fffffff)
  979. (stringview_iter.advance)
  980. (i64.extend_i32_u)))
  981. (('string-ref #f str idx)
  982. `(,(local.get str)
  983. (ref.cast ,(make-ref-type #f '$string))
  984. (struct.get $string $str)
  985. (string.as_iter)
  986. (local.tee $str_iter)
  987. ,(local.get idx)
  988. (i32.wrap_i64)
  989. (stringview_iter.advance)
  990. (drop)
  991. (local.get $str_iter)
  992. (stringview_iter.next)
  993. (i64.extend_i32_u)))
  994. (('string-set! #f str idx val)
  995. `(,(local.get str)
  996. ,(local.get idx)
  997. (i32.wrap_i64)
  998. ,(local.get val)
  999. (i32.wrap_i64)
  1000. (call $string-set!)))
  1001. (('symbol-hash #f sym)
  1002. `(,(local.get sym)
  1003. (ref.cast ,(make-ref-type #f '$symbol))
  1004. (struct.get $symbol $hash)
  1005. (i64.extend_i32_u)))
  1006. ;; For the time being, atomic boxes are basically the same
  1007. ;; as regular boxes.
  1008. (('make-atomic-box #f val)
  1009. `((i32.const 0)
  1010. ,(local.get val)
  1011. (struct.new $atomic-box)))
  1012. (('atomic-box-ref #f box)
  1013. `(,(local.get box)
  1014. (ref.cast ,(make-ref-type #f '$atomic-box))
  1015. (struct.get $atomic-box $val)))
  1016. (('atomic-box-set! #f box val)
  1017. `(,(local.get box)
  1018. (ref.cast ,(make-ref-type #f '$atomic-box))
  1019. ,(local.get val)
  1020. (struct.set $atomic-box $val)))
  1021. (('atomic-box-swap! #f box val)
  1022. `(,(local.get box)
  1023. (ref.cast ,(make-ref-type #f '$atomic-box))
  1024. (struct.get $atomic-box $val)
  1025. ,(local.get box)
  1026. (ref.cast ,(make-ref-type #f '$atomic-box))
  1027. ,(local.get val)
  1028. (struct.set $atomic-box $val)))
  1029. (('atomic-box-compare-and-swap! #f box expected desired)
  1030. `(,(local.get box)
  1031. (ref.cast ,(make-ref-type #f '$atomic-box))
  1032. (struct.get $atomic-box $val)
  1033. (local.tee $s0)
  1034. ,(local.get expected)
  1035. (ref.eq)
  1036. (if #f ,void-block-type
  1037. (,(local.get box)
  1038. (ref.cast ,(make-ref-type #f '$atomic-box))
  1039. ,(local.get desired)
  1040. (struct.set $atomic-box $val))
  1041. ())
  1042. (local.get $s0)))
  1043. (('f64->scm #f f64)
  1044. `((i32.const 0)
  1045. ,(local.get f64)
  1046. (struct.new $flonum)))
  1047. ;; Generic arithmetic. Emit a fixnum fast-path and a
  1048. ;; callout to runtime functions for the slow path.
  1049. (('add #f x y)
  1050. (compile-fixnum-fixnum-fast-path
  1051. x y scm-block-type
  1052. '((local.get $i0) (local.get $i1) (call $fixnum-add))
  1053. '((call $add))))
  1054. (('sub #f x y)
  1055. (compile-fixnum-fixnum-fast-path
  1056. x y scm-block-type
  1057. '((local.get $i0) (local.get $i1) (call $fixnum-sub))
  1058. '((call $sub))))
  1059. ;; FIXME: Reimplement fast paths
  1060. (('add/immediate y x)
  1061. (compile-fixnum-fast-path
  1062. x scm-block-type
  1063. `((local.get $i0) (i32.const ,(ash y 1)) (call $fixnum-add))
  1064. `((i32.const ,(ash y 1))
  1065. (ref.i31)
  1066. (call $add))))
  1067. (('sub/immediate y x)
  1068. (compile-fixnum-fast-path
  1069. x scm-block-type
  1070. `((local.get $i0) (i32.const ,(ash y 1)) (call $fixnum-sub))
  1071. `((i32.const ,(ash y 1))
  1072. (ref.i31)
  1073. (call $sub))))
  1074. (('mul #f x y)
  1075. (compile-fixnum-fixnum-fast-path
  1076. x y scm-block-type
  1077. '((local.get $i0) (local.get $i1) (call $fixnum-mul))
  1078. '((call $mul))))
  1079. (('mul/immediate y x)
  1080. (compile-fixnum-fast-path
  1081. x scm-block-type
  1082. `((local.get $i0) (i32.const ,(ash y 1)) (call $fixnum-mul))
  1083. `((i32.const ,(ash y 1))
  1084. (ref.i31)
  1085. (call $mul))))
  1086. (('div #f x y)
  1087. `(,(local.get x)
  1088. ,(local.get y)
  1089. (call $div)))
  1090. (('quo #f x y)
  1091. (compile-fixnum-fixnum-fast-path
  1092. x y scm-block-type
  1093. '((local.get $i0) (i32.const 1) (i32.shr_s)
  1094. (local.get $i1) (i32.const 1) (i32.shr_s)
  1095. (i32.div_s)
  1096. (i32.const 1) (i32.shl)
  1097. (ref.i31))
  1098. '((call $quo))
  1099. #:fast-checks '(((local.get $i1) (i32.eqz))
  1100. ((local.get $i1) (i32.const -2) (i32.eq)))))
  1101. (('rem #f x y)
  1102. (compile-fixnum-fixnum-fast-path
  1103. x y scm-block-type
  1104. '((local.get $i0) (i32.const 1) (i32.shr_s)
  1105. (local.get $i1) (i32.const 1) (i32.shr_s)
  1106. (i32.rem_s)
  1107. (i32.const 1) (i32.shl)
  1108. (ref.i31))
  1109. '((call $rem))
  1110. #:fast-checks '(((local.get $i1) (i32.eqz)))))
  1111. (('mod #f x y)
  1112. (compile-fixnum-fixnum-fast-path
  1113. x y scm-block-type
  1114. `((local.get $i0) (i32.const 1) (i32.shr_s)
  1115. (local.get $i1) (i32.const 1) (i32.shr_s)
  1116. (i32.rem_s)
  1117. ;; Tag as fixnum.
  1118. (i32.const 1) (i32.shl)
  1119. ;; Adjust if y and r have opposite signs.
  1120. (local.tee $i0)
  1121. (local.get $i1)
  1122. (i32.const 0)
  1123. (i32.gt_s)
  1124. (if #f ,i32-block-type
  1125. ((local.get $i0) (i32.const 0) (i32.lt_s))
  1126. ((local.get $i0) (i32.const 0) (i32.gt_s)))
  1127. (if #f ,i32-block-type
  1128. ;; Adjust by adding tagged $i1.
  1129. ((local.get $i0) (local.get $i1) (i32.add))
  1130. ((local.get $i0)))
  1131. (ref.i31))
  1132. '((call $mod))
  1133. #:fast-checks '(((local.get $i1) (i32.eqz)))))
  1134. ;; Integer bitwise operations. Fast path for fixnums and
  1135. ;; callout otherwise.
  1136. (('logand #f x y)
  1137. (compile-fixnum-fixnum-fast-path
  1138. x y scm-block-type
  1139. '((local.get $i0) (local.get $i1) (i32.and) (ref.i31))
  1140. '((call $logand))))
  1141. (('logand/immediate y x)
  1142. (compile-fixnum-fast-path
  1143. x scm-block-type
  1144. `((local.get $i0) (i32.const ,(ash y 1)) (i32.and) (ref.i31))
  1145. `((i32.const ,(ash y 1))
  1146. (ref.i31)
  1147. (call $logand))))
  1148. (('logior #f x y)
  1149. (compile-fixnum-fixnum-fast-path
  1150. x y scm-block-type
  1151. '((local.get $i0) (local.get $i1) (i32.or) (ref.i31))
  1152. '((call $logior))))
  1153. (('logxor #f x y)
  1154. (compile-fixnum-fixnum-fast-path
  1155. x y scm-block-type
  1156. '((local.get $i0) (local.get $i1) (i32.xor) (ref.i31))
  1157. '((call $logxor))))
  1158. (('logsub #f x y)
  1159. (compile-fixnum-fixnum-fast-path
  1160. x y scm-block-type
  1161. '((local.get $i0)
  1162. (local.get $i1)
  1163. (i32.const -1)
  1164. (i32.xor)
  1165. (i32.and)
  1166. (ref.i31))
  1167. '((call $logsub))))
  1168. (('rsh #f x y)
  1169. (compile-fixnum-u64-fast-path
  1170. x y scm-block-type
  1171. `((local.get $i0)
  1172. ;; Clamp shift to 31 bits; fixnum is smaller than that.
  1173. (i64.const 31)
  1174. ,(local.get y)
  1175. (i64.lt_u)
  1176. (if #f ,i32-block-type
  1177. ((i32.const 31))
  1178. (,(local.get y)
  1179. (i32.wrap_i64)))
  1180. (i32.shr_s)
  1181. ;; Mask off fixnum bit.
  1182. (i32.const -2)
  1183. (i32.and)
  1184. (ref.i31))
  1185. '((call $rsh))))
  1186. (('lsh #f x y)
  1187. (compile-fixnum-u64-fast-path
  1188. x y scm-block-type
  1189. `((local.get $i0)
  1190. (i32.const 1)
  1191. (i32.shr_s)
  1192. (i64.extend_i32_s)
  1193. ,(local.get y)
  1194. (i64.shl)
  1195. (call $s64->scm))
  1196. '((call $lsh))
  1197. #:fast-checks `((,(local.get y) (i64.const 32) (i64.gt_u)))))
  1198. (('rsh/immediate y x)
  1199. (compile-fixnum-fast-path
  1200. x scm-block-type
  1201. (if (< y 31)
  1202. `((local.get $i0)
  1203. (i32.const ,y)
  1204. (i32.shr_s)
  1205. (i32.const -2)
  1206. (i32.and)
  1207. (ref.i31))
  1208. `((i32.const 0)
  1209. (ref.i31)))
  1210. `((i64.const ,y)
  1211. (call $rsh))))
  1212. (('lsh/immediate y x)
  1213. (if (<= y 32)
  1214. (compile-fixnum-fast-path
  1215. x scm-block-type
  1216. `((local.get $i0)
  1217. (i32.const 1)
  1218. (i32.shr_s)
  1219. (i64.extend_i32_s)
  1220. (i64.const ,y)
  1221. (i64.shl)
  1222. (call $s64->scm))
  1223. `((i64.const ,y)
  1224. (call $lsh)))
  1225. `((i64.const ,y)
  1226. (call $lsh))))
  1227. ;; Arithmetic on real numbers.
  1228. (('inexact #f x)
  1229. `(,(local.get x)
  1230. (call $inexact)))
  1231. (('abs #f x)
  1232. `(,(local.get x)
  1233. (call $abs)))
  1234. (('sqrt #f x)
  1235. `(,(local.get x)
  1236. (call $sqrt)))
  1237. (('floor #f x)
  1238. `(,(local.get x)
  1239. (call $floor)))
  1240. (('ceiling #f x)
  1241. `(,(local.get x)
  1242. (call $ceiling)))
  1243. ;; Trig functions. Probably just call out for now.
  1244. (('sin #f x)
  1245. `(,(local.get x)
  1246. (call $sin)))
  1247. (('cos #f x)
  1248. `(,(local.get x)
  1249. (call $cos)))
  1250. (('tan #f x)
  1251. `(,(local.get x)
  1252. (call $tan)))
  1253. (('asin #f x)
  1254. `(,(local.get x)
  1255. (call $asin)))
  1256. (('acos #f x)
  1257. `(,(local.get x)
  1258. (call $acos)))
  1259. (('atan #f x)
  1260. `(,(local.get x)
  1261. (call $atan)))
  1262. (('atan2 #f x y)
  1263. `(,(local.get x)
  1264. ,(local.get y)
  1265. (call $atan2)))
  1266. ;; Unboxed integer arithmetic and logical operations.
  1267. (((or 'load-u64 'load-s64) s64)
  1268. `((i64.const ,s64)))
  1269. (((or 's64->u64 'u64->s64) #f arg)
  1270. `(,(local.get arg)))
  1271. (((or 'uadd 'sadd) #f x y)
  1272. `(,(local.get x)
  1273. ,(local.get y)
  1274. (i64.add)))
  1275. (((or 'usub 'ssub) #f x y)
  1276. `(,(local.get x)
  1277. ,(local.get y)
  1278. (i64.sub)))
  1279. (((or 'umul 'smul) #f x y)
  1280. `(,(local.get x)
  1281. ,(local.get y)
  1282. (i64.mul)))
  1283. (((or 'sadd/immediate 'uadd/immediate) y x)
  1284. `(,(local.get x)
  1285. (i64.const ,y)
  1286. (i64.add)))
  1287. (((or 'usub/immediate 'ssub/immediate) y x)
  1288. `(,(local.get x)
  1289. (i64.const ,y)
  1290. (i64.sub)))
  1291. (((or 'umul/immediate 'smul/immediate) y x)
  1292. `(,(local.get x)
  1293. (i64.const ,y)
  1294. (i64.mul)))
  1295. (('ulogand #f x y)
  1296. `(,(local.get x)
  1297. ,(local.get y)
  1298. (i64.and)))
  1299. (('ulogand/immediate y x)
  1300. `(,(local.get x)
  1301. (i64.const ,y)
  1302. (i64.and)))
  1303. (('ulogior #f x y)
  1304. `(,(local.get x)
  1305. ,(local.get y)
  1306. (i64.or)))
  1307. (('ulogsub #f x y)
  1308. `(,(local.get x)
  1309. ,(local.get y)
  1310. (i64.not)
  1311. (i64.or)))
  1312. (('ulogxor #f x y)
  1313. `(,(local.get x)
  1314. ,(local.get y)
  1315. (i64.xor)))
  1316. (('ursh #f x y)
  1317. `(,(local.get x)
  1318. ,(local.get y)
  1319. (i64.shr_u)))
  1320. (('srsh #f x y)
  1321. `(,(local.get x)
  1322. ,(local.get y)
  1323. (i64.shr_s)))
  1324. (('ulsh #f x y)
  1325. `(,(local.get x)
  1326. ,(local.get y)
  1327. (i64.shl)))
  1328. (('ursh/immediate y x)
  1329. `(,(local.get x)
  1330. (i64.const ,y)
  1331. (i64.shr_u)))
  1332. (('srsh/immediate y x)
  1333. `(,(local.get x)
  1334. (i64.const ,y)
  1335. (i64.shr_s)))
  1336. (('ulsh/immediate y x)
  1337. `(,(local.get x)
  1338. (i64.const ,y)
  1339. (i64.shl)))
  1340. (('slsh/immediate y x)
  1341. `(,(local.get x)
  1342. (i64.const ,y)
  1343. (i64.shl)))
  1344. ;; Unboxed floating-point arithmetic and trig.
  1345. (('load-f64 f64)
  1346. `((f64.const ,f64)))
  1347. (('s64->f64 #f arg)
  1348. `(,(local.get arg)
  1349. (f64.reinterpret_i64)))
  1350. (('fadd #f x y)
  1351. `(,(local.get x)
  1352. ,(local.get y)
  1353. (f64.add)))
  1354. (('fadd/immediate y x)
  1355. `(,(local.get x)
  1356. (f64.const ,y)
  1357. (f64.add)))
  1358. (('fsub #f x y)
  1359. `(,(local.get x)
  1360. ,(local.get y)
  1361. (f64.sub)))
  1362. (('fmul #f x y)
  1363. `(,(local.get x)
  1364. ,(local.get y)
  1365. (f64.mul)))
  1366. (('fdiv #f x y)
  1367. `(,(local.get x)
  1368. ,(local.get y)
  1369. (f64.div)))
  1370. (('fabs #f x)
  1371. `(,(local.get x)
  1372. (f64.abs)))
  1373. (('fsqrt #f x)
  1374. `(,(local.get x)
  1375. (f64.sqrt)))
  1376. (('ffloor #f x)
  1377. `(,(local.get x)
  1378. (f64.floor)))
  1379. (('fceiling #f x)
  1380. `(,(local.get x)
  1381. (f64.ceil)))
  1382. (('fsin #f x)
  1383. `(,(local.get x)
  1384. (call $fsin)))
  1385. (('fcos #f x)
  1386. `(,(local.get x)
  1387. (call $fcos)))
  1388. (('ftan #f x)
  1389. `(,(local.get x)
  1390. (call $ftan)))
  1391. (('fasin #f x)
  1392. `(,(local.get x)
  1393. (call $fasin)))
  1394. (('facos #f x)
  1395. `(,(local.get x)
  1396. (call $facos)))
  1397. (('fatan #f x)
  1398. `(,(local.get x)
  1399. (call $fatan)))
  1400. (('fatan2 #f x y)
  1401. `(,(local.get x)
  1402. ,(local.get y)
  1403. (call $fatan2)))
  1404. ;; Misc.
  1405. (('string->bignum #f x)
  1406. `((i32.const 0)
  1407. ,(local.get x)
  1408. (ref.cast ,(make-ref-type #f '$string))
  1409. (struct.get $string $str)
  1410. (call $string->bignum)
  1411. (struct.new $bignum)))
  1412. (('string->symbol #f x)
  1413. `(,(local.get x)
  1414. (ref.cast ,(make-ref-type #f '$string))
  1415. (call $string->symbol)))
  1416. (('symbol->string #f x)
  1417. `(,(local.get x)
  1418. (ref.cast ,(make-ref-type #f '$symbol))
  1419. (struct.get $symbol $name)))
  1420. (('symbol->keyword #f x)
  1421. `(,(local.get x)
  1422. (ref.cast ,(make-ref-type #f '$symbol))
  1423. (call $symbol->keyword)))
  1424. (('keyword->symbol #f x)
  1425. `(,(local.get x)
  1426. (ref.cast ,(make-ref-type #f '$keyword))
  1427. (struct.get $keyword $name)))
  1428. (('string->utf8 #f str)
  1429. `((i32.const 0)
  1430. ,(local.get str)
  1431. (ref.cast ,(make-ref-type #f '$string))
  1432. (struct.get $string $str)
  1433. (call $string->wtf8)
  1434. (struct.new $bytevector)))
  1435. (('string-utf8-length #f str)
  1436. `(,(local.get str)
  1437. (ref.cast ,(make-ref-type #f '$string))
  1438. (struct.get $string $str)
  1439. (string.measure_wtf8)))
  1440. (('utf8->string #f bv)
  1441. `((i32.const 0)
  1442. ,(local.get bv)
  1443. (ref.cast ,(make-ref-type #f '$bytevector))
  1444. (struct.get $bytevector $vals)
  1445. (call $wtf8->string)
  1446. (struct.new $mutable-string)))
  1447. ;; Unboxing and boxing numbers.
  1448. (('scm->f64 #f src)
  1449. `(,(local.get src)
  1450. (call $scm->f64)))
  1451. (('flonum->f64 #f src)
  1452. `(,(local.get src)
  1453. (ref.cast ,(make-ref-type #f '$flonum))
  1454. (struct.get $flonum $val)))
  1455. (('compnum-real #f src)
  1456. `(,(local.get src)
  1457. (ref.cast ,(make-ref-type #f '$complex))
  1458. (struct.get $complex $real)))
  1459. (('compnum-imag #f src)
  1460. `(,(local.get src)
  1461. (ref.cast ,(make-ref-type #f '$complex))
  1462. (struct.get $complex $imag)))
  1463. (('scm->s64 #f src)
  1464. (compile-fixnum-fast-path
  1465. src i64-block-type
  1466. `((local.get $i0)
  1467. (i32.const 1)
  1468. (i32.shr_s)
  1469. (i64.extend_i32_s))
  1470. `((call $scm->s64))))
  1471. (('scm->u64 #f src)
  1472. `(,(local.get src)
  1473. (call $scm->u64)))
  1474. (('scm->u64/truncate #f src)
  1475. (compile-fixnum-fast-path
  1476. src i64-block-type
  1477. `((local.get $i0)
  1478. (i32.const 1)
  1479. (i32.shr_s)
  1480. (i64.extend_i32_s))
  1481. '((call $scm->u64/truncate))))
  1482. (('u64->scm #f src)
  1483. `(,(local.get src)
  1484. (i64.const ,(ash 1 29))
  1485. (i64.lt_u)
  1486. (if #f ,scm-block-type
  1487. (,(local.get src)
  1488. (i32.wrap_i64)
  1489. (i32.const 1)
  1490. (i32.shl)
  1491. (ref.i31))
  1492. (,(local.get src)
  1493. (call $u64->bignum)))))
  1494. (('s64->scm #f src)
  1495. `(,(local.get src)
  1496. (call $s64->scm)))
  1497. ;; For native Guile, these bytevector accesses take three
  1498. ;; parameters: the object itself, which is unused but keeps
  1499. ;; the pointer alive; a pointer to the actual bytes; and an
  1500. ;; index. For WebAssembly we pass the $raw-bytevector as
  1501. ;; the ptr and not reference the object.
  1502. (('u8-ref ann obj ptr idx)
  1503. `(,(local.get ptr)
  1504. ,(local.get idx)
  1505. (i32.wrap_i64)
  1506. (array.get_u $raw-bytevector)
  1507. (i64.extend_i32_u)))
  1508. (('s8-ref ann obj ptr idx)
  1509. `(,(local.get ptr)
  1510. ,(local.get idx)
  1511. (i32.wrap_i64)
  1512. (array.get_s $raw-bytevector)
  1513. (i64.extend_i32_s)))
  1514. ;; Annoyingly there are no native multi-byte accesses to i8
  1515. ;; arrays.
  1516. (('u16-ref ann obj ptr idx)
  1517. `(,(local.get ptr)
  1518. ,(local.get idx) (i32.wrap_i64)
  1519. (array.get_u $raw-bytevector)
  1520. ,(local.get ptr)
  1521. ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
  1522. (array.get_u $raw-bytevector)
  1523. (i32.const 8) (i32.shl)
  1524. (i32.or)
  1525. (i64.extend_i32_u)))
  1526. (('s16-ref ann obj ptr idx)
  1527. `(,(local.get ptr)
  1528. ,(local.get idx) (i32.wrap_i64)
  1529. (array.get_u $raw-bytevector)
  1530. ,(local.get ptr)
  1531. ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
  1532. (array.get_s $raw-bytevector)
  1533. (i32.const 8) (i32.shl)
  1534. (i32.or)
  1535. (i64.extend_i32_s)))
  1536. (('u32-ref ann obj ptr idx)
  1537. `(,(local.get ptr)
  1538. ,(local.get idx) (i32.wrap_i64)
  1539. (array.get_u $raw-bytevector)
  1540. ,(local.get ptr)
  1541. ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
  1542. (array.get_u $raw-bytevector)
  1543. (i32.const 8) (i32.shl)
  1544. (i32.or)
  1545. ,(local.get ptr)
  1546. ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
  1547. (array.get_u $raw-bytevector)
  1548. (i32.const 16) (i32.shl)
  1549. (i32.or)
  1550. ,(local.get ptr)
  1551. ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
  1552. (array.get_u $raw-bytevector)
  1553. (i32.const 24) (i32.shl)
  1554. (i32.or)
  1555. (i64.extend_i32_u)))
  1556. (('s32-ref ann obj ptr idx)
  1557. `(,(local.get ptr)
  1558. ,(local.get idx) (i32.wrap_i64)
  1559. (array.get_u $raw-bytevector)
  1560. ,(local.get ptr)
  1561. ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
  1562. (array.get_u $raw-bytevector)
  1563. (i32.const 8) (i32.shl)
  1564. (i32.or)
  1565. ,(local.get ptr)
  1566. ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
  1567. (array.get_u $raw-bytevector)
  1568. (i32.const 16) (i32.shl)
  1569. (i32.or)
  1570. ,(local.get ptr)
  1571. ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
  1572. (array.get_s $raw-bytevector)
  1573. (i32.const 24) (i32.shl)
  1574. (i32.or)
  1575. (i64.extend_i32_s)))
  1576. (((or 'u64-ref 's64-ref) ann obj ptr idx)
  1577. `(,(local.get ptr)
  1578. ,(local.get idx) (i32.wrap_i64)
  1579. (array.get_u $raw-bytevector)
  1580. (i64.extend_i32_u)
  1581. ,(local.get ptr)
  1582. ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
  1583. (array.get_u $raw-bytevector)
  1584. (i64.extend_i32_u) (i64.const 8) (i64.shl)
  1585. (i64.or)
  1586. ,(local.get ptr)
  1587. ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
  1588. (array.get_u $raw-bytevector)
  1589. (i64.extend_i32_u) (i64.const 16) (i64.shl)
  1590. (i64.or)
  1591. ,(local.get ptr)
  1592. ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
  1593. (array.get_u $raw-bytevector)
  1594. (i64.extend_i32_u) (i64.const 24) (i64.shl)
  1595. (i64.or)
  1596. ,(local.get ptr)
  1597. ,(local.get idx) (i32.wrap_i64) (i32.const 4) (i32.add)
  1598. (array.get_u $raw-bytevector)
  1599. (i64.extend_i32_u) (i64.const 32) (i64.shl)
  1600. (i64.or)
  1601. ,(local.get ptr)
  1602. ,(local.get idx) (i32.wrap_i64) (i32.const 5) (i32.add)
  1603. (array.get_u $raw-bytevector)
  1604. (i64.extend_i32_u) (i64.const 40) (i64.shl)
  1605. (i64.or)
  1606. ,(local.get ptr)
  1607. ,(local.get idx) (i32.wrap_i64) (i32.const 6) (i32.add)
  1608. (array.get_u $raw-bytevector)
  1609. (i64.extend_i32_u) (i64.const 48) (i64.shl)
  1610. (i64.or)
  1611. ,(local.get ptr)
  1612. ,(local.get idx) (i32.wrap_i64) (i32.const 7) (i32.add)
  1613. (array.get_u $raw-bytevector)
  1614. (i64.extend_i32_u) (i64.const 56) (i64.shl)
  1615. (i64.or)))
  1616. (('f32-ref ann obj ptr idx)
  1617. `(,(local.get ptr)
  1618. ,(local.get idx) (i32.wrap_i64)
  1619. (array.get_u $raw-bytevector)
  1620. ,(local.get ptr)
  1621. ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
  1622. (array.get_u $raw-bytevector)
  1623. (i32.const 8) (i32.shl)
  1624. (i32.or)
  1625. ,(local.get ptr)
  1626. ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
  1627. (array.get_u $raw-bytevector)
  1628. (i32.const 16) (i32.shl)
  1629. (i32.or)
  1630. ,(local.get ptr)
  1631. ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
  1632. (array.get_s $raw-bytevector)
  1633. (i32.const 24) (i32.shl)
  1634. (i32.or)
  1635. (f32.reinterpret_i32)
  1636. (f64.promote_f32)))
  1637. (('f64-ref ann obj ptr idx)
  1638. `(,(local.get ptr)
  1639. ,(local.get idx) (i32.wrap_i64)
  1640. (array.get_u $raw-bytevector)
  1641. (i64.extend_i32_u)
  1642. ,(local.get ptr)
  1643. ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
  1644. (array.get_u $raw-bytevector)
  1645. (i64.extend_i32_u) (i64.const 8) (i64.shl)
  1646. (i64.or)
  1647. ,(local.get ptr)
  1648. ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
  1649. (array.get_u $raw-bytevector)
  1650. (i64.extend_i32_u) (i64.const 16) (i64.shl)
  1651. (i64.or)
  1652. ,(local.get ptr)
  1653. ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
  1654. (array.get_u $raw-bytevector)
  1655. (i64.extend_i32_u) (i64.const 24) (i64.shl)
  1656. (i64.or)
  1657. ,(local.get ptr)
  1658. ,(local.get idx) (i32.wrap_i64) (i32.const 4) (i32.add)
  1659. (array.get_u $raw-bytevector)
  1660. (i64.extend_i32_u) (i64.const 32) (i64.shl)
  1661. (i64.or)
  1662. ,(local.get ptr)
  1663. ,(local.get idx) (i32.wrap_i64) (i32.const 5) (i32.add)
  1664. (array.get_u $raw-bytevector)
  1665. (i64.extend_i32_u) (i64.const 40) (i64.shl)
  1666. (i64.or)
  1667. ,(local.get ptr)
  1668. ,(local.get idx) (i32.wrap_i64) (i32.const 6) (i32.add)
  1669. (array.get_u $raw-bytevector)
  1670. (i64.extend_i32_u) (i64.const 48) (i64.shl)
  1671. (i64.or)
  1672. ,(local.get ptr)
  1673. ,(local.get idx) (i32.wrap_i64) (i32.const 7) (i32.add)
  1674. (array.get_u $raw-bytevector)
  1675. (i64.extend_i32_u) (i64.const 56) (i64.shl)
  1676. (i64.or)
  1677. (f64.reinterpret_i64)))
  1678. (((or 'u8-set! 's8-set!) ann obj ptr idx val)
  1679. `(,(local.get ptr)
  1680. ,(local.get idx)
  1681. (i32.wrap_i64)
  1682. ,(local.get val)
  1683. (i32.wrap_i64)
  1684. (array.set $raw-bytevector)))
  1685. (((or 'u16-set! 's16-set!) ann obj ptr idx val)
  1686. `(,(local.get ptr)
  1687. ,(local.get idx) (i32.wrap_i64)
  1688. ,(local.get val) (i32.wrap_i64)
  1689. (array.set $raw-bytevector)
  1690. ,(local.get ptr)
  1691. ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
  1692. ,(local.get val) (i64.const 8) (i64.shr_u) (i32.wrap_i64)
  1693. (array.set $raw-bytevector)))
  1694. (((or 'u32-set! 's32-set!) ann obj ptr idx val)
  1695. `(,(local.get ptr)
  1696. ,(local.get idx) (i32.wrap_i64)
  1697. ,(local.get val) (i32.wrap_i64)
  1698. (array.set $raw-bytevector)
  1699. ,(local.get ptr)
  1700. ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
  1701. ,(local.get val) (i64.const 8) (i64.shr_u) (i32.wrap_i64)
  1702. (array.set $raw-bytevector)
  1703. ,(local.get ptr)
  1704. ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
  1705. ,(local.get val) (i64.const 16) (i64.shr_u) (i32.wrap_i64)
  1706. (array.set $raw-bytevector)
  1707. ,(local.get ptr)
  1708. ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
  1709. ,(local.get val) (i64.const 24) (i64.shr_u) (i32.wrap_i64)
  1710. (array.set $raw-bytevector)))
  1711. (((or 'u64-set! 's64-set!) ann obj ptr idx val)
  1712. `(,(local.get ptr)
  1713. ,(local.get idx) (i32.wrap_i64)
  1714. ,(local.get val) (i32.wrap_i64)
  1715. (array.set $raw-bytevector)
  1716. ,(local.get ptr)
  1717. ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
  1718. ,(local.get val) (i64.const 8) (i64.shr_u) (i32.wrap_i64)
  1719. (array.set $raw-bytevector)
  1720. ,(local.get ptr)
  1721. ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
  1722. ,(local.get val) (i64.const 16) (i64.shr_u) (i32.wrap_i64)
  1723. (array.set $raw-bytevector)
  1724. ,(local.get ptr)
  1725. ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
  1726. ,(local.get val) (i64.const 24) (i64.shr_u) (i32.wrap_i64)
  1727. (array.set $raw-bytevector)
  1728. ,(local.get ptr)
  1729. ,(local.get idx) (i32.wrap_i64) (i32.const 4) (i32.add)
  1730. ,(local.get val) (i64.const 32) (i64.shr_u) (i32.wrap_i64)
  1731. (array.set $raw-bytevector)
  1732. ,(local.get ptr)
  1733. ,(local.get idx) (i32.wrap_i64) (i32.const 5) (i32.add)
  1734. ,(local.get val) (i64.const 40) (i64.shr_u) (i32.wrap_i64)
  1735. (array.set $raw-bytevector)
  1736. ,(local.get ptr)
  1737. ,(local.get idx) (i32.wrap_i64) (i32.const 6) (i32.add)
  1738. ,(local.get val) (i64.const 48) (i64.shr_u) (i32.wrap_i64)
  1739. (array.set $raw-bytevector)
  1740. ,(local.get ptr)
  1741. ,(local.get idx) (i32.wrap_i64) (i32.const 7) (i32.add)
  1742. ,(local.get val) (i64.const 56) (i64.shr_u) (i32.wrap_i64)
  1743. (array.set $raw-bytevector)))
  1744. (('f32-set! ann obj ptr idx val)
  1745. `(,(local.get ptr)
  1746. ,(local.get idx) (i32.wrap_i64)
  1747. ,(local.get val) (f32.demote_f64) (i32.reinterpret_f32)
  1748. (array.set $raw-bytevector)
  1749. ,(local.get ptr)
  1750. ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
  1751. ,(local.get val) (f32.demote_f64) (i32.reinterpret_f32) (i32.const 8) (i32.shr_u)
  1752. (array.set $raw-bytevector)
  1753. ,(local.get ptr)
  1754. ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
  1755. ,(local.get val) (f32.demote_f64) (i32.reinterpret_f32) (i32.const 16) (i32.shr_u)
  1756. (array.set $raw-bytevector)
  1757. ,(local.get ptr)
  1758. ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
  1759. ,(local.get val) (f32.demote_f64) (i32.reinterpret_f32) (i32.const 24) (i32.shr_u)
  1760. (array.set $raw-bytevector)))
  1761. (('f64-set! ann obj ptr idx val)
  1762. `(,(local.get ptr)
  1763. ,(local.get idx) (i32.wrap_i64)
  1764. ,(local.get val) (i64.reinterpret_f64) (i32.wrap_i64)
  1765. (array.set $raw-bytevector)
  1766. ,(local.get ptr)
  1767. ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
  1768. ,(local.get val) (i64.reinterpret_f64) (i64.const 8) (i64.shr_u) (i32.wrap_i64)
  1769. (array.set $raw-bytevector)
  1770. ,(local.get ptr)
  1771. ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
  1772. ,(local.get val) (i64.reinterpret_f64) (i64.const 16) (i64.shr_u) (i32.wrap_i64)
  1773. (array.set $raw-bytevector)
  1774. ,(local.get ptr)
  1775. ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
  1776. ,(local.get val) (i64.reinterpret_f64) (i64.const 24) (i64.shr_u) (i32.wrap_i64)
  1777. (array.set $raw-bytevector)
  1778. ,(local.get ptr)
  1779. ,(local.get idx) (i32.wrap_i64) (i32.const 4) (i32.add)
  1780. ,(local.get val) (i64.reinterpret_f64) (i64.const 32) (i64.shr_u) (i32.wrap_i64)
  1781. (array.set $raw-bytevector)
  1782. ,(local.get ptr)
  1783. ,(local.get idx) (i32.wrap_i64) (i32.const 5) (i32.add)
  1784. ,(local.get val) (i64.reinterpret_f64) (i64.const 40) (i64.shr_u) (i32.wrap_i64)
  1785. (array.set $raw-bytevector)
  1786. ,(local.get ptr)
  1787. ,(local.get idx) (i32.wrap_i64) (i32.const 6) (i32.add)
  1788. ,(local.get val) (i64.reinterpret_f64) (i64.const 48) (i64.shr_u) (i32.wrap_i64)
  1789. (array.set $raw-bytevector)
  1790. ,(local.get ptr)
  1791. ,(local.get idx) (i32.wrap_i64) (i32.const 7) (i32.add)
  1792. ,(local.get val) (i64.reinterpret_f64) (i64.const 56) (i64.shr_u) (i32.wrap_i64)
  1793. (array.set $raw-bytevector)))
  1794. ;; Infallible unboxing of fixnums and chars.
  1795. (('untag-fixnum #f src)
  1796. `(,(local.get src)
  1797. (ref.cast ,i31-type)
  1798. (i31.get_s)
  1799. (i32.const 1)
  1800. (i32.shr_s)
  1801. (i64.extend_i32_s)))
  1802. (('tag-fixnum #f src)
  1803. `(,(local.get src)
  1804. (i32.wrap_i64)
  1805. (i32.const 1)
  1806. (i32.shl)
  1807. (ref.i31)))
  1808. (('untag-char #f src)
  1809. `(,(local.get src)
  1810. (ref.cast ,i31-type)
  1811. (i31.get_s)
  1812. (i32.const 2)
  1813. (i32.shr_u)
  1814. (i64.extend_i32_u)))
  1815. (('tag-char #f src)
  1816. `(,(local.get src)
  1817. (i32.wrap_i64)
  1818. (i32.const 2)
  1819. (i32.shl)
  1820. (i32.const #b11)
  1821. (i32.or)
  1822. (ref.i31)))
  1823. ;; The dynamic state. Implement using runtime.
  1824. (('push-prompt escape-only? tag handler)
  1825. `((global.get $raw-sp)
  1826. (global.get $scm-sp)
  1827. (global.get $ret-sp)
  1828. (i32.const ,(if escape-only? 1 0))
  1829. ,(local.get tag)
  1830. ,(local.get handler)
  1831. (struct.new $dynprompt)
  1832. (call $push-dyn)))
  1833. (('push-fluid #f fluid val)
  1834. ;; Going to assume (via the ref.cast) that someone checked
  1835. ;; that fluid is in fact a fluid. Not the case currently
  1836. ;; in Guile; this should be fixed.
  1837. `(,(local.get fluid)
  1838. (ref.cast ,(make-ref-type #f '$fluid))
  1839. ,(local.get val)
  1840. (call $push-fluid)))
  1841. (('pop-fluid #f)
  1842. `((call $pop-fluid)))
  1843. (('fluid-ref #f fluid)
  1844. `(,(local.get fluid)
  1845. (ref.cast ,(make-ref-type #f '$fluid))
  1846. (call $fluid-ref)))
  1847. (('fluid-set! #f fluid value)
  1848. `(,(local.get fluid)
  1849. (ref.cast ,(make-ref-type #f '$fluid))
  1850. ,(local.get value)
  1851. (call $fluid-set!)))
  1852. (('push-dynamic-state #f state)
  1853. ;; Assuming that it's a $dynamic-state, as with fluids.
  1854. `(,(local.get state)
  1855. (ref.cast ,(make-ref-type #f '$dynamic-state))
  1856. (call $push-dynamic-state)))
  1857. (('pop-dynamic-state #f)
  1858. `((call $pop-dynamic-state)))
  1859. (('wind #f winder unwinder)
  1860. `(,(local.get winder)
  1861. (ref.cast ,(make-ref-type #f '$proc))
  1862. ,(local.get unwinder)
  1863. (ref.cast ,(make-ref-type #f '$proc))
  1864. (struct.new $dynwind)
  1865. (call $push-dyn)))
  1866. (('unwind #f)
  1867. `((global.get $dyn-sp)
  1868. (i32.const 1)
  1869. (i32.sub)
  1870. (global.set $dyn-sp)))
  1871. ((name param . args)
  1872. (error "unexpected primcall" name param args))))
  1873. (_ (error "unexpected expression" exp))))
  1874. (define (compile-test op param args)
  1875. (match (vector op param args)
  1876. ;; Immediate type tag predicates.
  1877. (#('fixnum? #f (a))
  1878. `(,(local.get a)
  1879. (ref.test ,i31-type)
  1880. (if #f ,i32-block-type
  1881. (,(local.get a)
  1882. (ref.cast ,i31-type)
  1883. (i31.get_s)
  1884. (i32.const #b1)
  1885. (i32.and)
  1886. (i32.eqz))
  1887. ((i32.const 0)))))
  1888. ;; FIXME: elide heap-object? tests if they just guard a more
  1889. ;; specific test.
  1890. (#('heap-object? #f (a))
  1891. `(,(local.get a) (ref.test ,(make-ref-type #f '$heap-object))))
  1892. (#('char? #f (a))
  1893. `(,(local.get a)
  1894. (ref.test ,i31-type)
  1895. (if #f ,i32-block-type
  1896. (,(local.get a)
  1897. (ref.cast ,i31-type)
  1898. (i31.get_u)
  1899. (i32.const #b11)
  1900. (i32.and)
  1901. (i32.const #b11)
  1902. (i32.eq))
  1903. ((i32.const 0)))))
  1904. (#('eq-constant? imm (a))
  1905. `(,(local.get a)
  1906. ,@(compile-constant imm)
  1907. (ref.eq)))
  1908. (#('undefined? #f (a))
  1909. `(,(local.get a)
  1910. (i32.const 57)
  1911. (ref.i31)
  1912. (ref.eq)))
  1913. (#('null? #f (a))
  1914. `(,(local.get a)
  1915. (ref.test ,i31-type)
  1916. (if #f ,i32-block-type
  1917. (,(local.get a)
  1918. (ref.cast ,i31-type)
  1919. (i31.get_s)
  1920. (i32.const #b110111)
  1921. (i32.and)
  1922. (i32.const #b000101)
  1923. (i32.eq))
  1924. ((i32.const 0)))))
  1925. (#('false? #f (a))
  1926. `(,(local.get a)
  1927. (ref.test ,i31-type)
  1928. (if #f ,i32-block-type
  1929. (,(local.get a)
  1930. (ref.cast ,i31-type)
  1931. (i31.get_s)
  1932. (i32.const #b111011)
  1933. (i32.and)
  1934. (i32.const #b000001)
  1935. (i32.eq))
  1936. ((i32.const 0)))))
  1937. (#('nil? #f (a))
  1938. `(,(local.get a)
  1939. (ref.test ,i31-type)
  1940. (if #f ,i32-block-type
  1941. (,(local.get a)
  1942. (ref.cast ,i31-type)
  1943. (i31.get_s)
  1944. (i32.const #b110011)
  1945. (i32.and)
  1946. (i32.const #b000001)
  1947. (i32.eq))
  1948. ((i32.const 0)))))
  1949. (#('pair? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$pair))))
  1950. (#('struct? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$struct))))
  1951. (#('symbol? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$symbol))))
  1952. (#('variable? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$variable))))
  1953. (#('vector? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$vector))))
  1954. (#('mutable-vector? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$mutable-vector))))
  1955. (#('immutable-vector? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$vector))))
  1956. (#('string? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$string))))
  1957. (#('heap-number? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$heap-number))))
  1958. (#('hash-table? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$hash-table))))
  1959. (#('fluid? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$fluid))))
  1960. (#('dynamic-state? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$dynamic-state))))
  1961. (#('keyword? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$keyword))))
  1962. (#('atomic-box? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$atomic-box))))
  1963. (#('syntax? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$syntax))))
  1964. (#('procedure? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$proc))))
  1965. (#('bytevector? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$bytevector))))
  1966. (#('weak-table? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$weak-table))))
  1967. (#('bitvector? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$bitvector))))
  1968. (#('port? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$port))))
  1969. (#('bignum? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$bignum))))
  1970. (#('flonum? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$flonum))))
  1971. (#('compnum? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$complex))))
  1972. (#('fracnum? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$fraction))))
  1973. (#('eq? #f (a b)) `(,(local.get a) ,(local.get b) (ref.eq)))
  1974. (#('vtable-vtable? #f (vtable))
  1975. `(,(local.get vtable)
  1976. (ref.cast ,(make-ref-type #f '$vtable))
  1977. (struct.get $vtable $vtable)
  1978. (ref.test ,(make-ref-type #f '$vtable-vtable))))
  1979. (#('heap-numbers-equal? #f (a b))
  1980. `(,(local.get a) ,(local.get b) (call $heap-numbers-equal?)))
  1981. (#('< #f (a b))
  1982. (compile-fixnum-fixnum-fast-path a b i32-block-type
  1983. '((local.get $i0)
  1984. (local.get $i1)
  1985. (i32.lt_s))
  1986. '((call $slow-<))))
  1987. (#('<= #f (a b))
  1988. (compile-fixnum-fixnum-fast-path a b i32-block-type
  1989. '((local.get $i0)
  1990. (local.get $i1)
  1991. (i32.le_s))
  1992. '((call $slow-<=))))
  1993. (#('= #f (a b))
  1994. (compile-fixnum-fixnum-fast-path a b i32-block-type
  1995. '((local.get $i0)
  1996. (local.get $i1)
  1997. (i32.eq))
  1998. '((call $slow-=))))
  1999. (#('u64-< #f (a b))
  2000. `(,(local.get a)
  2001. ,(local.get b)
  2002. (i64.lt_u)))
  2003. (#('u64-imm-< b (a))
  2004. `(,(local.get a)
  2005. (i64.const ,b)
  2006. (i64.lt_u)))
  2007. (#('imm-u64-< b (a))
  2008. `((i64.const ,b)
  2009. ,(local.get a)
  2010. (i64.lt_u)))
  2011. (#((or 'u64-= 's64-=) #f (a b))
  2012. `(,(local.get a)
  2013. ,(local.get b)
  2014. (i64.eq)))
  2015. (#((or 'u64-imm-= 's64-imm-=) b (a))
  2016. `(,(local.get a)
  2017. (i64.const ,b)
  2018. (i64.eq)))
  2019. (#('s64-< #f (a b))
  2020. `(,(local.get a)
  2021. ,(local.get b)
  2022. (i64.lt_s)))
  2023. (#('s64-imm-< b (a))
  2024. `(,(local.get a)
  2025. (i64.const ,b)
  2026. (i64.lt_s)))
  2027. (#('imm-s64-< b (a))
  2028. `((i64.const ,b)
  2029. ,(local.get a)
  2030. (i64.lt_s)))
  2031. (#('f64-< #f (a b))
  2032. `(,(local.get a)
  2033. ,(local.get b)
  2034. (f64.lt)))
  2035. (#('f64-<= #f (a b))
  2036. `(,(local.get a)
  2037. ,(local.get b)
  2038. (f64.le)))
  2039. (#('f64-= #f (a b))
  2040. `(,(local.get a)
  2041. ,(local.get b)
  2042. (f64.eq)))
  2043. (#('f64-finite? #f (a))
  2044. `(,(local.get a)
  2045. ,(local.get a)
  2046. (f64.sub)
  2047. (f64.const 0)
  2048. (f64.eq)))
  2049. (#('f64-int? #f (a))
  2050. `(,(local.get a)
  2051. (f64.trunc)
  2052. ,(local.get a)
  2053. (f64.sub)
  2054. (f64.const 0)
  2055. (f64.eq)))))
  2056. (define (compile-throw src op param args)
  2057. ;; FIXME: Instead of dying, we need to implement exception
  2058. ;; handling, as in Guile.
  2059. (match (vector op param args)
  2060. (#('raise-type-error #(subr pos what) (val))
  2061. `((string.const ,subr)
  2062. (string.const ,what)
  2063. ,(local.get val)
  2064. (return_call $raise-type-error)))
  2065. (#('raise-range-error #(subr pos) (val))
  2066. `((string.const ,subr)
  2067. ,(local.get val)
  2068. (return_call $raise-range-error)))
  2069. (#('raise-arity-error #(subr) (val))
  2070. `(,(if subr
  2071. `(string.const ,subr)
  2072. '(ref.null string))
  2073. ,(local.get val)
  2074. (return_call $raise-arity-error)))
  2075. (#('raise-unbound-error #(subr) (val))
  2076. `((string.const ,subr)
  2077. ,(local.get val)
  2078. (return_call $raise-unbound-error)))
  2079. (#('raise-exception #f (exn))
  2080. `(,(local.get exn)
  2081. (return_call $raise-exception)))))
  2082. ;; See "Beyond Relooper: Recursive Translation of Unstructured
  2083. ;; Control Flow to Structured Control Flow", Norman Ramsey, ICFP
  2084. ;; 2022.
  2085. (define (make-ctx next-label stack) (cons next-label stack))
  2086. (define (push-loop label ctx)
  2087. (match ctx
  2088. ((next-label . stack)
  2089. (make-ctx next-label
  2090. (acons 'loop-headed-by label stack)))))
  2091. (define (push-block label ctx)
  2092. (match ctx
  2093. ((next-label . stack)
  2094. (make-ctx label
  2095. (acons 'block-followed-by label stack)))))
  2096. (define (push-if ctx)
  2097. (match ctx
  2098. ((next-label . stack)
  2099. (make-ctx next-label (cons 'if-then-else stack)))))
  2100. (define (lookup-label k ctx)
  2101. (match ctx
  2102. ((next-label . stack)
  2103. (let lp ((stack stack) (depth 0))
  2104. (match stack
  2105. (('if-then-else . stack) (lp stack (1+ depth)))
  2106. ((((or 'loop-headed-by 'block-followed-by) . label) . stack)
  2107. (if (eqv? label k)
  2108. depth
  2109. (lp stack (1+ depth))))
  2110. (_ (error "block label not found" k ctx)))))))
  2111. (define (do-tree label ctx)
  2112. (define (code-for-label ctx)
  2113. (let ((children (intmap-ref dom-children label)))
  2114. (node-within
  2115. label
  2116. (match (intmap-ref cps label)
  2117. (($ $kargs names vars ($ $switch))
  2118. children)
  2119. (_
  2120. (intset-filter merge-cont? children)))
  2121. ctx)))
  2122. (if (loop-cont? label)
  2123. `((loop #f ,void-block-type
  2124. ,(code-for-label (push-loop label ctx))))
  2125. (code-for-label ctx)))
  2126. (define (do-branch pred succ ctx)
  2127. (cond
  2128. ((or (<= succ pred)
  2129. (merge-cont? succ))
  2130. ;; Backward branch or branch to merge: jump.
  2131. (match ctx
  2132. ((next-label . stack)
  2133. (if (eqv? succ next-label)
  2134. '()
  2135. `((br ,(lookup-label succ ctx)))))))
  2136. (else
  2137. ;; Otherwise render successor inline.
  2138. (do-tree succ ctx))))
  2139. (define (node-within label ys ctx)
  2140. (call-with-values (lambda () (intset-pop-right ys))
  2141. (lambda (ys y)
  2142. (match y
  2143. (#f
  2144. (match (intmap-ref cps label)
  2145. (($ $kargs names vars term)
  2146. ;; could change to bind names at continuation?
  2147. (match term
  2148. (($ $continue k src exp)
  2149. (match (intmap-ref cps k)
  2150. (($ $ktail)
  2151. (compile-tail exp))
  2152. (($ $kargs _ vars)
  2153. `(,@(compile-values exp)
  2154. ,@(reverse (map local.set vars))
  2155. ,@(do-branch label k ctx)))
  2156. (($ $kreceive ($ $arity req () rest () #f) kargs)
  2157. (error "kreceive should be tailified away"))))
  2158. (($ $branch kf kt src op param args)
  2159. `(,@(compile-test op param args)
  2160. (if #f ,void-block-type
  2161. ,(do-branch label kt (push-if ctx))
  2162. ,(do-branch label kf (push-if ctx)))))
  2163. (($ $switch kf kt* src arg)
  2164. `(,(local.get arg)
  2165. (i32.wrap_i64)
  2166. (br_table ,(map (lambda (k) (lookup-label k ctx)) kt*)
  2167. ,(lookup-label kf ctx))))
  2168. (($ $prompt k kh src escape? tag)
  2169. (error "prompts should be removed by tailification?"))
  2170. (($ $throw src op param args)
  2171. (compile-throw src op param args))))
  2172. (($ $kreceive)
  2173. (error "kreceive should be tailified away"))
  2174. (($ $kfun src meta self ktail kentry)
  2175. (match (intmap-ref cps kentry)
  2176. (($ $kclause)
  2177. ;; An arity-checking function; let the clause check
  2178. ;; the arity.
  2179. (let ((tail (do-branch label kentry ctx)))
  2180. (if (and self (var-used? self))
  2181. `(,@(arg-ref 0)
  2182. ,(local.set self)
  2183. . ,tail)
  2184. tail)))
  2185. (($ $kargs names vars _)
  2186. ;; A function whose callers all pass the expected
  2187. ;; number of arguments.
  2188. (let ((vars (if self (cons self vars) vars)))
  2189. ;; FIXME: no need to rebind vars.
  2190. `(,@(append-map (lambda (var idx)
  2191. `((local.get ,idx)
  2192. ,(local.set var)))
  2193. vars (iota (length vars)))
  2194. ,@(do-tree kentry ctx))))))
  2195. (($ $kclause ($ $arity req opt rest kw allow-other-keys?)
  2196. kbody kalt)
  2197. (when allow-other-keys? (error "allow-other-keys? unimplemented"))
  2198. (when (not (null? kw)) (error "kwargs unimplemented"))
  2199. (match (intmap-ref cps kbody)
  2200. (($ $kargs names vars)
  2201. (let ((nreq (length req))
  2202. (nopt (length opt)))
  2203. (define req-vars (list-head vars nreq))
  2204. (define opt-vars (list-head (list-tail vars nreq) nopt))
  2205. (define rest-var
  2206. (and rest (car (list-tail vars (+ nreq nopt)))))
  2207. (define (check-positional min max strict?)
  2208. (define (check count test)
  2209. (let ((count (if has-closure? (1+ count) count)))
  2210. `((local.get $nargs)
  2211. (i32.const ,count)
  2212. ,test)))
  2213. (let ((check-req
  2214. (if (or elide-arity-check?
  2215. (and (not strict?) (zero? min)))
  2216. '()
  2217. (check min
  2218. (if (and strict? (eqv? min max))
  2219. '(i32.ne)
  2220. '(i32.lt_u)))))
  2221. (check-opt
  2222. (if (or elide-arity-check?
  2223. (eqv? min max) (not strict?))
  2224. '()
  2225. (check max '(i32.gt_u)))))
  2226. (define (combine-checks a b)
  2227. (cond
  2228. ((null? a) b)
  2229. ((null? b) a)
  2230. (else `(,@a ,@b (i32.or)))))
  2231. (match (combine-checks check-req check-opt)
  2232. (() '())
  2233. (checks
  2234. `(,@checks
  2235. (if #f ,void-block-type
  2236. ,(if kalt
  2237. (do-branch label kalt ctx)
  2238. `(,(match func-name
  2239. (#f '(ref.null string))
  2240. (name
  2241. `(string.const
  2242. ,(symbol->string func-name))))
  2243. (local.get $arg0)
  2244. (return_call $raise-arity-error)))
  2245. ()))))))
  2246. (define (init-positional min max)
  2247. (define (add-closure n) (if has-closure? (1+ n) n))
  2248. (define init-req
  2249. (append-map
  2250. (lambda (arg idx)
  2251. (if (var-used? arg)
  2252. `(,@(arg-ref idx)
  2253. ,(local.set arg))
  2254. '()))
  2255. req-vars (iota min (add-closure 0))))
  2256. (define init-opt
  2257. (append-map
  2258. (lambda (arg idx)
  2259. (if (var-used? arg)
  2260. `((local.get $nargs)
  2261. (i32.const ,idx)
  2262. (i32.gt_u)
  2263. (if #f ,scm-block-type
  2264. (,@(arg-ref idx))
  2265. ((i32.const 57) ; undefined
  2266. (ref.i31)))
  2267. ,(local.set arg))
  2268. '()))
  2269. opt-vars (iota (- max min) (add-closure min))))
  2270. (define init-rest
  2271. (if (and rest-var (var-used? rest-var))
  2272. `((local.get $nargs)
  2273. (local.get $arg0)
  2274. (local.get $arg1)
  2275. (local.get $arg2)
  2276. (i32.const ,(add-closure max))
  2277. (call $collect-rest-args)
  2278. ,(local.set rest-var))
  2279. '()))
  2280. `(,@init-req ,@init-opt ,@init-rest))
  2281. (define (positional-prelude)
  2282. `(,@(check-positional nreq (+ nreq nopt) (not rest))
  2283. ,@(init-positional nreq (+ nreq nopt))))
  2284. (define (kw-prelude)
  2285. (error "keyword args unimplemented"))
  2286. (define prelude
  2287. (cond
  2288. ((or allow-other-keys? (pair? kw))
  2289. (kw-prelude))
  2290. (else
  2291. (positional-prelude))))
  2292. `(,@prelude
  2293. ,@(do-branch label kbody ctx))))))
  2294. (($ $ktail)
  2295. '())))
  2296. (y
  2297. `((block #f ,void-block-type
  2298. ,(node-within label ys (push-block y ctx)))
  2299. ,@(do-tree y ctx)))))))
  2300. (define code
  2301. (do-tree kfun (make-ctx #f '())))
  2302. (define (type-for-repr repr)
  2303. (match repr
  2304. ('scm scm-type)
  2305. ('f64 'f64)
  2306. ((or 's64 'u64) 'i64)
  2307. ('bv-contents (make-ref-type #f '$raw-bytevector))
  2308. ('code (make-ref-type #f '$kvarargs))))
  2309. (define (type-for-named-temporary name)
  2310. (match name
  2311. ((or '$raw-sp '$scm-sp '$ret-sp '$dyn-sp) 'i32)
  2312. ((or '$i0 '$i1 '$i2) 'i32)
  2313. ((or '$f0 '$f1 '$f2) 'f64)
  2314. ('$str_iter (make-ref-type #f 'stringview_iter))
  2315. ('$s0 scm-type)
  2316. ('$vu0 (make-ref-type #f '$raw-bytevector))
  2317. ('$v0 (make-ref-type #f '$raw-scmvector))
  2318. (_ #f)))
  2319. (define (add-locals-from-code code)
  2320. (define locals (make-hash-table))
  2321. (define (visit-inst inst)
  2322. (match inst
  2323. (((or 'block 'loop) label type body)
  2324. (visit-expr body))
  2325. (('if label type consequent alternate)
  2326. (visit-expr consequent)
  2327. (visit-expr alternate))
  2328. (('try label type body catches catch-all)
  2329. (visit-expr body)
  2330. (for-each visit-expr catches)
  2331. (when catch-all (visit-expr catch-all)))
  2332. (('try_delegate label type body handler)
  2333. (visit-expr body))
  2334. (((or 'local.set 'local.tee) label)
  2335. (let ((type (type-for-named-temporary label)))
  2336. (when type
  2337. (hashq-set! locals label type))))
  2338. (_ #f)))
  2339. (define (visit-expr expr)
  2340. (match expr
  2341. (() (values))
  2342. ((inst . expr)
  2343. (visit-inst inst)
  2344. (visit-expr expr))))
  2345. (visit-expr code)
  2346. (sort (hash-map->list make-local locals)
  2347. (lambda (a b)
  2348. (string<? (match a (($ <local> id) (symbol->string id)))
  2349. (match b (($ <local> id) (symbol->string id)))))))
  2350. (define reprs
  2351. (compute-var-representations cps #:primcall-raw-representations
  2352. hoot-primcall-raw-representations))
  2353. (define locals
  2354. (append
  2355. (add-locals-from-code code)
  2356. (intmap-fold-right (lambda (var repr out)
  2357. (cons (make-local (var-label var)
  2358. (type-for-repr repr))
  2359. out))
  2360. reprs '())))
  2361. (define (init-joins code)
  2362. (intset-fold (lambda (var code)
  2363. (if (var-used? var)
  2364. (match (intmap-ref reprs var)
  2365. ((or 'f64 's64 'u64) code)
  2366. ((or 'code 'bv-contents)
  2367. (error "unexpected join repr" var))
  2368. ('scm
  2369. `((i32.const 57)
  2370. (ref.i31)
  2371. ,(local.set var)
  2372. . ,code)))
  2373. code))
  2374. join-vars
  2375. code))
  2376. ;; FIXME: Here attach a name, other debug info to the function
  2377. (make-func (func-label kfun)
  2378. (match (known-arity kfun)
  2379. (#f (make-type-use '$kvarargs kvarargs-sig))
  2380. (vars (make-type-use
  2381. #f
  2382. (make-func-sig
  2383. (map (lambda (var)
  2384. (define repr (intmap-ref reprs var))
  2385. (define type (type-for-repr repr))
  2386. (make-param #f type))
  2387. vars)
  2388. '()))))
  2389. locals
  2390. (init-joins code))))
  2391. (define (compute-globals)
  2392. (fold1 (lambda (entry globals)
  2393. (match entry
  2394. (#(name type init-expr reloc-expr)
  2395. (cons (make-global name
  2396. (make-global-type #f type)
  2397. init-expr)
  2398. globals))))
  2399. heap-constants
  2400. (list (make-global '$load
  2401. (make-global-type #t scm-type)
  2402. `((i32.const 0) (ref.i31))))))
  2403. (define (compute-exports)
  2404. (list (make-export "$load" 'global '$load)))
  2405. (define (compute-start-function funcs)
  2406. (define load-function-id
  2407. (match funcs
  2408. ;; Assume that the first function is the "load" function.
  2409. ((($ <func> id) . _) id)))
  2410. (define relocs
  2411. (fold1 (lambda (entry relocs)
  2412. (match entry
  2413. (#(name type init-expr #f) relocs)
  2414. (#(name type init-expr make-reloc)
  2415. (append (make-reloc name) relocs))))
  2416. heap-constants
  2417. '()))
  2418. (define maybe-grow-argv
  2419. (if (> max-args 8)
  2420. `((i32.const ,(- max-args 8))
  2421. (call $maybe-grow-argv))
  2422. '()))
  2423. (make-func '$start
  2424. void-block-type
  2425. '()
  2426. `(,@maybe-grow-argv
  2427. (i32.const 0)
  2428. (ref.func ,load-function-id)
  2429. (struct.new $proc)
  2430. (global.set $load)
  2431. . ,relocs)))
  2432. (let* ((funcs
  2433. (intmap-map->list lower-func (compute-reachable-functions cps 0)))
  2434. (start-func (compute-start-function funcs))
  2435. (types (append (intmap-map->list make-closure-type closure-types)
  2436. (make-struct-types)))
  2437. (tables '())
  2438. (memories '())
  2439. (globals (compute-globals))
  2440. (exports (compute-exports))
  2441. (start '$start)
  2442. (elems '())
  2443. (tags '())
  2444. (datas (reverse datas))
  2445. (strings '())
  2446. (custom '()))
  2447. (make-wasm '$scheme types imports
  2448. (cons start-func funcs)
  2449. tables memories globals exports
  2450. start elems datas tags strings custom)))
  2451. (define* (high-level-cps->wasm cps #:key
  2452. (import-abi? #f)
  2453. (export-abi? #t)
  2454. (env #f)
  2455. (optimization-level #f)
  2456. (warning-level #f)
  2457. (dump-cps? #f)
  2458. (dump-wasm? #f)
  2459. (emit-names? #f)
  2460. (opts '()))
  2461. (define (lower-and-tailify cps)
  2462. (define lower-cps
  2463. (let ((make-lower (language-lowerer (lookup-language 'cps))))
  2464. (make-lower optimization-level opts)))
  2465. (lower-cps cps env))
  2466. (let ((cps (lower-and-tailify cps)))
  2467. (when dump-cps?
  2468. (dump cps))
  2469. (let* ((wasm (lower-to-wasm cps #:import-abi? import-abi?))
  2470. (wasm (if export-abi? (export-abi wasm) wasm))
  2471. (wasm (add-stdlib wasm (compute-stdlib import-abi?)))
  2472. (wasm (lower-wasm wasm #:emit-names? emit-names?)))
  2473. (when dump-wasm?
  2474. (format #t "\n\nThe wasm we are going to emit:\n")
  2475. (dump-wasm wasm))
  2476. wasm)))
  2477. (define-syntax-rule (with-hoot-target . body)
  2478. (with-target "wasm32-unknown-hoot"
  2479. (lambda ()
  2480. (parameterize ((target-runtime 'hoot))
  2481. . body))))