backend.scm 111 KB

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