compile.scm 110 KB

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