backend.scm 110 KB

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