12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741 |
- ;;; WebAssembly compiler
- ;;; Copyright (C) 2023, 2024 Igalia, S.L.
- ;;; Copyright (C) 2023 Robin Templeton <robin@spritely.institute>
- ;;; Copyright (C) 2023 David Thompson <dave@spritely.institute>
- ;;;
- ;;; Licensed under the Apache License, Version 2.0 (the "License");
- ;;; you may not use this file except in compliance with the License.
- ;;; You may obtain a copy of the License at
- ;;;
- ;;; http://www.apache.org/licenses/LICENSE-2.0
- ;;;
- ;;; Unless required by applicable law or agreed to in writing, software
- ;;; distributed under the License is distributed on an "AS IS" BASIS,
- ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- ;;; See the License for the specific language governing permissions and
- ;;; limitations under the License.
- ;;; Commentary:
- ;;;
- ;;; Scheme to WebAssembly compiler.
- ;;;
- ;;; Code:
- (define-module (hoot backend)
- #:use-module (ice-9 format)
- #:use-module (ice-9 match)
- #:use-module ((srfi srfi-1) #:select (append-map alist-delete))
- #:use-module (srfi srfi-9)
- #:use-module (system base language)
- #:use-module (language cps)
- #:use-module (language cps intset)
- #:use-module (language cps intmap)
- #:use-module (language cps dump)
- #:use-module (language cps utils)
- #:use-module ((language cps hoot)
- #:select (hoot-primcall-raw-representations
- target-hash
- target-symbol-hash
- target-keyword-hash))
- #:use-module (rnrs bytevectors)
- #:use-module (hoot stdlib)
- #:use-module (wasm dump)
- #:use-module (wasm link)
- #:use-module (wasm lower)
- #:use-module (wasm types)
- #:export (high-level-cps->wasm))
- (define (invert-tree parents)
- (intmap-fold
- (lambda (child parent tree)
- (let ((tree (intmap-add tree child empty-intset intset-union)))
- (match parent
- (-1 tree)
- (_ (intmap-add tree parent (intset child) intset-union)))))
- parents empty-intmap))
- (define (intset-filter pred set)
- (persistent-intset
- (intset-fold (lambda (i out)
- (if (pred i) (intset-add! out i) out))
- set empty-intset)))
- (define (intset-pop-right set)
- (match (intset-prev set)
- (#f (values set #f))
- (i (values (intset-remove set i) i))))
- (define (intmap-map->list f map)
- (intmap-fold-right (lambda (k v out) (cons (f k v) out)) map '()))
- (define void-block-type (make-type-use #f (make-func-sig '() '())))
- (define i32-block-type (make-type-use #f (make-func-sig '() '(i32))))
- (define i64-block-type (make-type-use #f (make-func-sig '() '(i64))))
- ;; Codegen improvements:
- ;;
- ;; 1. Eliminating directly-used locals. Compute a set of variables that
- ;; are used once, right after they are defined, in such a way that
- ;; they can just flow to their use sites on the stack. Avoid
- ;; creating locals for these.
- ;;
- ;; 2. Instruction selection. Could be that some instructions could be
- ;; combined or rescheduled. Perhaps this is something on the CPS
- ;; level though.
- ;;
- ;; 3. Optimised local allocation. Do graph coloring to map variables to
- ;; a smaller set of locals, to avoid emitting one local per variable.
- (define scm-type (make-ref-type #f 'eq))
- (define scm-block-type (make-type-use #f (make-func-sig '() (list scm-type))))
- (define i31-type (make-ref-type #f 'i31))
- (define kvarargs-sig
- (make-func-sig (list (make-param '$nargs 'i32)
- (make-param '$arg0 scm-type)
- (make-param '$arg1 scm-type)
- (make-param '$arg2 scm-type))'()))
- (define (export-abi wasm)
- (define abi-exports
- (list (make-export "$arg3" 'global '$arg3)
- (make-export "$arg4" 'global '$arg4)
- (make-export "$arg5" 'global '$arg5)
- (make-export "$arg6" 'global '$arg6)
- (make-export "$arg7" 'global '$arg7)
- (make-export "$argv" 'table '$argv)
- (make-export "$raw-sp" 'global '$raw-sp)
- (make-export "$scm-sp" 'global '$scm-sp)
- (make-export "$ret-sp" 'global '$ret-sp)
- (make-export "$dyn-sp" 'global '$dyn-sp)
- (make-export "$current-fluids" 'global '$current-fluids)
- (make-export "$raise-exception" 'global '$raise-exception)
- (make-export "$with-exception-handler" 'global '$with-exception-handler)
- (make-export "$current-input-port" 'global '$current-input-port)
- (make-export "$current-output-port" 'global '$current-output-port)
- (make-export "$current-error-port" 'global '$current-error-port)
- (make-export "$default-prompt-tag" 'global '$default-prompt-tag)
- (make-export "$raw-stack" 'memory '$raw-stack)
- (make-export "$scm-stack" 'table '$scm-stack)
- (make-export "$ret-stack" 'table '$ret-stack)
- (make-export "$dyn-stack" 'table '$dyn-stack)
- (make-export "$intern-symbol!" 'func '$intern-symbol!)
- (make-export "$intern-keyword!" 'func '$intern-keyword!)
- (make-export "$make-size-error" 'global '$make-size-error)
- (make-export "$make-index-error" 'global '$make-index-error)
- (make-export "$make-range-error" 'global '$make-range-error)
- (make-export "$make-start-offset-error" 'global '$make-start-offset-error)
- (make-export "$make-end-offset-error" 'global '$make-end-offset-error)
- (make-export "$make-type-error" 'global '$make-type-error)
- (make-export "$make-unimplemented-error" 'global '$make-unimplemented-error)
- (make-export "$make-assertion-error" 'global '$make-assertion-error)
- (make-export "$make-not-seekable-error" 'global '$make-not-seekable-error)
- (make-export "$make-runtime-error-with-message" 'global '$make-runtime-error-with-message)
- (make-export "$make-runtime-error-with-message+irritants" 'global '$make-runtime-error-with-message+irritants)
- (make-export "$make-match-error" 'global '$make-match-error)
- (make-export "$make-arity-error" 'global '$make-arity-error)))
- (define (add-export export exports)
- (cons export exports))
- (match wasm
- (($ <wasm> id types imports funcs tables memories globals exports
- start elems datas tags strings custom)
- (make-wasm id types imports funcs tables memories globals
- (reverse (fold1 add-export abi-exports (reverse exports)))
- start elems datas tags strings custom))))
- (define (target-hashq x)
- (target-hash x))
- (define-record-type <static-procedure>
- (make-static-procedure code)
- static-procedure?
- (code static-procedure-code))
- (define (compute-used-vars cps)
- (define (adjoin var used)
- (intset-add! used var))
- (define (adjoin* vars used)
- (fold1 adjoin vars used))
- (persistent-intset
- (intmap-fold
- (lambda (k cont used)
- ;; Only a term can use a var.
- (match cont
- (($ $kargs names syms term)
- (match term
- (($ $continue k src exp)
- (match exp
- (($ $call proc args)
- (adjoin* args (adjoin proc used)))
- (($ $callk k proc args)
- (adjoin* args (if proc (adjoin proc used) used)))
- (($ $calli args callee)
- (adjoin* args (adjoin callee used)))
- (($ $primcall name param args)
- (adjoin* args used))
- (($ $values args)
- (adjoin* args used))
- ((or ($ $const) ($ $const-fun) ($ $prim) ($ $code)) used)))
- (($ $branch kf kt src op param args)
- (adjoin* args used))
- (($ $switch kf kt* src arg)
- (adjoin arg used))
- (($ $prompt k kh src escape? tag)
- (adjoin tag used))
- (($ $throw src op param args)
- (adjoin* args used))))
- (_ used)))
- cps empty-intset)))
- (define (compute-join-vars cps preds)
- (define (adjoin var joins)
- (intset-add! joins var))
- (define (adjoin* vars joins)
- (fold1 adjoin vars joins))
- (define (join-cont? k)
- (< 1 (fold1 (lambda (pred count)
- (if (< pred k) (1+ count) count))
- (intmap-ref preds k)
- 0)))
- (persistent-intset
- (intmap-fold
- (lambda (k cont joins)
- (match cont
- (($ $kargs names syms)
- (if (and (pair? syms) (join-cont? k))
- (adjoin* syms joins)
- joins))
- (_ joins)))
- cps empty-intset)))
- (define* (lower-to-wasm cps #:key import-abi?)
- (define max-args 0)
- ;; interning constants into constant table
- ;; finalizing constant table
- ;; setting init function.
- (define imports '())
- (define datas '())
- (define data-count 0)
- (define (intern-data! bv)
- (let ((name (string->symbol
- (format #f "$data~a" data-count))))
- (set! data-count (1+ data-count))
- (set! datas (cons (make-data name 'passive #f #f bv) datas))
- name))
- (define heap-constants '())
- (define heap-constant-count 0)
- (define heap-constant-names (make-hash-table))
- (define (intern-heap-constant! x)
- (define* (intern! type init-expr #:optional make-reloc)
- (let ((name (string->symbol
- (format #f "$constant~a" heap-constant-count))))
- (set! heap-constant-count (1+ heap-constant-count))
- (hash-set! heap-constant-names x name)
- (define entry (vector name type init-expr make-reloc))
- (set! heap-constants (cons entry heap-constants))
- name))
- (match x
- ((car . cdr)
- (intern! (make-ref-type #f '$pair)
- `((i32.const ,(target-hashq x))
- ,@(compile-constant car)
- ,@(compile-constant cdr)
- (struct.new $pair))))
- (#(elt ...)
- (intern! (make-ref-type #f '$vector)
- `((i32.const ,(target-hashq x))
- ,@(append-map compile-constant elt)
- (array.new_fixed $raw-scmvector ,(vector-length x))
- (struct.new $vector))))
- ((? bytevector?)
- (let ((name (intern-data! x)))
- (intern! (make-ref-type #f '$bytevector)
- `((i32.const ,(target-hashq x))
- (i32.const 0)
- (i32.const ,(bytevector-length x))
- (array.new_data $raw-bytevector ,name)
- (struct.new $bytevector)))))
- ((? bitvector?)
- ;; FIXME: Probably we should put the initializers in the data
- ;; section instead of using new_fixed.
- (intern! (make-ref-type #f '$bitvector)
- `((i32.const ,(target-hashq x))
- (i32.const ,(bitvector-length x))
- ,@(let* ((u32v (uniform-array->bytevector x))
- (u32len (/ (bytevector-length u32v) 4)))
- (unless (eq? (native-endianness) (endianness little))
- (error "unsupported"))
- (let lp ((i 0))
- (if (< i u32len)
- (cons `(i32.const
- ,(bytevector-s32-native-ref u32v (* i 4)))
- (lp (1+ i)))
- '())))
- (array.new_fixed $raw-bitvector
- ,(ash (+ 31 (bitvector-length x)) -5))
- (struct.new $bitvector))))
- ((? string?)
- (intern! (make-ref-type #f '$string)
- `((i32.const ,(target-hashq x))
- (string.const ,x)
- (struct.new $string))))
- (($ <static-procedure> code)
- (intern! (make-ref-type #f '$proc)
- `((i32.const 0)
- (ref.func ,code)
- (struct.new $proc))))
- ((? symbol?)
- ;; We need to add the symbol to the symbol table. In the case
- ;; where we're not importing ABI, the actual global is known to
- ;; be constant at compile-time, and we just emit a call to
- ;; $intern-symbol!. Otherwise we call $intern-symbol! as part of
- ;; the initializer, and rely on lower-globals pass to move this
- ;; non-constant initialization to start.
- (intern! (make-ref-type #f '$symbol)
- `((i32.const ,(target-symbol-hash (symbol->string x)))
- ,@(compile-constant (symbol->string x))
- (struct.new $symbol)
- . ,(if import-abi? `((call $intern-symbol!)) '()))
- (and (not import-abi?)
- (lambda (name)
- `((global.get ,name)
- (call $intern-symbol!)
- (drop))))))
- ((? keyword?)
- ;; Same run-time interning considerations as symbols.
- (intern! (make-ref-type #f '$keyword)
- `((i32.const ,(target-keyword-hash
- (symbol->string (keyword->symbol x))))
- ,@(compile-constant (keyword->symbol x))
- (struct.new $keyword)
- . ,(if import-abi? `((call $intern-keyword!)) '()))
- (and (not import-abi?)
- (lambda (name)
- `((global.get ,name)
- (call $intern-keyword!)
- (drop))))))
- ((? number?)
- (if (exact? x)
- (if (integer? x)
- (intern! (make-ref-type #f '$bignum)
- `((i32.const 0)
- (string.const ,(number->string x))
- (call $string->bignum)
- (struct.new $bignum)))
- (intern! (make-ref-type #f '$fraction)
- `((i32.const 0)
- ,@(compile-constant (numerator x))
- ,@(compile-constant (denominator x))
- (struct.new $fraction))))
- (if (real? x)
- (intern! (make-ref-type #f '$flonum)
- `((i32.const 0)
- (f64.const ,x)
- (struct.new $flonum)))
- (intern! (make-ref-type #f '$complex)
- `((i32.const 0)
- (f64.const ,(real-part x))
- (f64.const ,(imag-part x))
- (struct.new $complex))))))
- (_ (error "unrecognized constant" x))))
- (define (compile-heap-constant val)
- (let ((name (or (hash-ref heap-constant-names val)
- (intern-heap-constant! val))))
- `((global.get ,name))))
- (define (compile-immediate-constant val)
- (define (fixnum? val)
- (and (exact-integer? val)
- (<= (ash -1 29) val (1- (ash 1 29)))))
- (match val
- ((? fixnum?) `((i32.const ,(ash val 1))
- (ref.i31)))
- (#f `((i32.const 1) (ref.i31)))
- ((? (lambda (x) (eq? x #nil))) `((i32.const 5) (ref.i31)))
- ((? (lambda (x) (eq? x '()))) `((i32.const 13) (ref.i31)))
- (#t `((i32.const 17) (ref.i31)))
- ((? unspecified?) `((i32.const 33) (ref.i31)))
- ((? eof-object?) `((i32.const 41) (ref.i31)))
- ((? char?) `((i32.const ,(logior (ash (char->integer val) 2)
- #b11))
- (ref.i31)))
- (_ #f)))
- (define (compile-constant val)
- (or (compile-immediate-constant val)
- (compile-heap-constant val)))
- (define closure-types empty-intmap)
- (define (closure-type nfree)
- (if (zero? nfree)
- '$proc
- (or (intmap-ref closure-types nfree (lambda (_) #f))
- (let ((name (string->symbol (format #f "$closure~a" nfree))))
- (set! closure-types (intmap-add closure-types nfree name))
- name))))
- (define (make-closure-type nfree name)
- (define fields (make-list nfree (make-field #f #t scm-type)))
- (define struct-type
- (make-struct-type (cons* (make-field '$hash #t 'i32)
- (make-field '$proc #f
- (make-ref-type #f '$kvarargs))
- fields)))
- (make-type name (make-sub-type #t '($proc) struct-type)))
- (define max-struct-nfields 0)
- (define (struct-type-name nfields)
- (cond
- ((zero? nfields) '$struct)
- (else
- (when (< max-struct-nfields nfields)
- (set! max-struct-nfields nfields))
- (string->symbol (format #f "$struct/~a" nfields)))))
- (define (make-struct-types)
- ;; The first few struct types are part of the rec block, because
- ;; vtables are structs. See vtable-fields in stdlib.scm.
- (define vtable-nfields 8)
- (let lp ((n (1+ vtable-nfields)))
- (if (<= n max-struct-nfields)
- (let ((name (struct-type-name n))
- (parent (struct-type-name (1- n))))
- (define fields (make-list n (make-field #f #t scm-type)))
- (define struct-type
- (make-struct-type (cons* (make-field '$hash #t 'i32)
- (make-field '$vtable #t
- (make-ref-type #t '$vtable))
- fields)))
- (define sub-type
- (make-sub-type #f (list parent) struct-type))
- (cons (make-type name sub-type) (lp (1+ n))))
- '())))
- (define (func-label k) (string->symbol (format #f "$f~a" k)))
- (define (known-arity k)
- (match (intmap-ref cps k)
- (($ $kfun src meta self ktail kentry)
- (match (intmap-ref cps kentry)
- (($ $kclause) #f)
- (($ $kargs names vars) (if self (cons self vars) vars))))))
- (define (lower-func kfun body)
- (let ((cps (intmap-select cps body)))
- (define has-closure?
- (match (intmap-ref cps kfun)
- (($ $kfun src meta self ktail kentry) self)))
- (define elide-arity-check?
- (match (intmap-ref cps kfun)
- (($ $kfun src meta self ktail kentry)
- (assq-ref meta 'elide-arity-check?))))
- (define func-name
- (match (intmap-ref cps kfun)
- (($ $kfun src meta self ktail kentry)
- (assq-ref meta 'name))))
- (define used-vars (compute-used-vars cps))
- (define (var-used? var) (intset-ref used-vars var))
- (define preds (compute-predecessors cps kfun))
- (define join-vars (compute-join-vars cps preds))
- (define idoms (compute-idoms cps kfun))
- (define dom-children (invert-tree idoms))
- (define (merge-cont? label)
- (let lp ((preds (intmap-ref preds label))
- (has-forward-in-edge? #f))
- (match preds
- (() #f)
- ((pred . preds)
- (if (< pred label)
- (or has-forward-in-edge?
- (lp preds #t))
- (lp preds has-forward-in-edge?))))))
- (define (loop-cont? label)
- (or-map (lambda (pred) (<= label pred))
- (intmap-ref preds label)))
- (define (loop-label label)
- (string->symbol (format #f "$l~a" label)))
- (define (wrap-loop expr label)
- (if (loop-cont? label)
- `(loop ,(loop-label label) ,void-block-type ,expr)
- expr))
- (define (var-label var) (string->symbol (format #f "$v~a" var)))
- (define (local.get var) `(local.get ,(var-label var)))
- (define (local.set var) `(local.set ,(var-label var)))
- (define (local-arg-label idx) (string->symbol (format #f "$arg~a" idx)))
- (define (global-arg-label idx) (string->symbol (format #f "$arg~a" idx)))
- (define (arg-ref idx)
- (cond
- ((< idx 3) `((local.get ,(local-arg-label idx))))
- ((< idx 8) `((global.get ,(global-arg-label idx))))
- (else `((i32.const ,(- idx 8))
- (table.get $argv)
- ref.as_non_null))))
- (define (compile-tail exp)
- (define (pass-abi-arguments args)
- (cons
- `(i32.const ,(length args))
- (let lp ((args args) (idx 0))
- (match args
- (()
- (if (< idx 3)
- (append '((i32.const 0)
- (ref.i31))
- (lp args (1+ idx)))
- '()))
- ((arg . args)
- (append (cond
- ((< idx 3)
- `(,(local.get arg)))
- ((< idx 8)
- `(,(local.get arg)
- (global.set ,(global-arg-label idx))))
- (else
- `((i32.const ,(- idx 8))
- ,(local.get arg)
- (table.set $argv))))
- (lp args (1+ idx))))))))
- ;; Keep track of the maximum number of arguments passed in a
- ;; single call so that we can size the $argv table
- ;; appropriately in the start function.
- (define (update-max-args args)
- (set! max-args (max max-args (length args))))
- (match exp
- (($ $call proc args)
- (update-max-args args)
- `(,@(pass-abi-arguments (cons proc args))
- ,(local.get proc)
- (ref.cast ,(make-ref-type #f '$proc))
- (struct.get $proc 1)
- (return_call_ref $kvarargs)))
- (($ $calli args callee)
- (update-max-args args)
- ;; This is a return.
- `(,@(pass-abi-arguments args)
- ,(local.get callee)
- (return_call_ref $kvarargs)))
- (($ $callk k proc args)
- (update-max-args args)
- (let ((args (if proc (cons proc args) args)))
- `(,@(if (known-arity k)
- (map local.get args)
- (pass-abi-arguments args))
- (return_call ,(func-label k)))))))
- (define-syntax-rule (match-primcall name param args
- ((%name %param . %args) . body) ...)
- (match name
- (%name (match (cons param args) ((%param . %args) . body)))
- ...))
- (define (analyze-saved-vars reprs)
- (define (save/raw memory sp maybe-grow-stack idx store-inst alignment)
- (lambda (var sizes)
- (define (prepare-save)
- `((global.get ,sp)
- (local.tee ,sp)
- (i32.const ,(assq-ref sizes sp))
- (i32.add)
- (global.set ,sp)
- (call ,maybe-grow-stack)))
- `(,@(if (zero? idx) (prepare-save) '())
- (local.get ,sp)
- (local.get ,var)
- (,store-inst ,(make-mem-arg memory idx alignment)))))
- (define (save/ref table sp maybe-grow-stack idx)
- (lambda (var sizes)
- (define (prepare-save)
- `((global.get ,sp)
- (local.tee ,sp)
- (i32.const ,(assq-ref sizes sp))
- (i32.add)
- (global.set ,sp)
- (call ,maybe-grow-stack)))
- `(,@(if (zero? idx) (prepare-save) '())
- (local.get ,sp)
- ,@(if (zero? idx) '() `((i32.const ,idx) (i32.add)))
- (local.get ,var)
- (table.set ,table))))
- (define (restore sp idx code)
- (lambda (sizes)
- (define (prepare-restore)
- `((global.get ,sp)
- (i32.const ,(assq-ref sizes sp))
- (i32.sub)
- (local.tee ,sp)
- (global.set ,sp)))
- `(,@(if (zero? idx) (prepare-restore) '())
- (local.get ,sp)
- ,@code)))
- (define (restore/raw memory sp idx load-inst alignment)
- (restore sp idx `((,load-inst ,(make-mem-arg memory idx alignment)))))
- (define (restore/ref table sp idx cast)
- (restore sp idx `(,@(if (zero? idx) '() `((i32.const ,idx) (i32.add)))
- (table.get ,table)
- ,@cast)))
- (define (visit/raw idx store-inst load-inst alignment)
- (cons (save/raw '$raw-stack '$raw-sp '$maybe-grow-raw-stack
- idx store-inst alignment)
- (restore/raw '$raw-stack '$raw-sp idx load-inst alignment)))
- (define (visit/ref table sp grow idx restore-cast)
- (cons (save/ref table sp grow idx)
- (restore/ref table sp idx restore-cast)))
- (define (visit-i64 idx) (visit/raw idx 'i64.store 'i64.load 3))
- (define (visit-f64 idx) (visit/raw idx 'f64.store 'f64.load 3))
- (define (visit-scm idx)
- (visit/ref '$scm-stack '$scm-sp '$maybe-grow-scm-stack idx
- '((ref.as_non_null))))
- (define (visit-raw-bytevector idx)
- (visit/ref '$scm-stack '$scm-sp '$maybe-grow-scm-stack idx
- `((ref.cast ,(make-ref-type #f '$raw-bytevector)))))
- (define (visit-ret idx)
- (visit/ref '$ret-stack '$ret-sp '$maybe-grow-ret-stack idx
- '((ref.as_non_null))))
- (let lp ((reprs reprs) (out '())
- (raw-size 0) (scm-size 0) (ret-size 0))
- (match reprs
- (()
- (values (reverse out)
- `(($raw-sp . ,raw-size)
- ($scm-sp . ,scm-size)
- ($ret-sp . ,ret-size))))
- ((r . reprs)
- (match r
- ((or 'u64 's64)
- (lp reprs
- (cons (visit-i64 raw-size) out)
- (+ raw-size 8) scm-size ret-size))
- ('f64
- (lp reprs
- (cons (visit-f64 raw-size) out)
- (+ raw-size 8) scm-size ret-size))
- ('scm
- (lp reprs
- (cons (visit-scm scm-size) out)
- raw-size (1+ scm-size) ret-size))
- ('bv-contents
- (lp reprs
- (cons (visit-raw-bytevector scm-size) out)
- raw-size (1+ scm-size) ret-size))
- ('code
- (lp reprs
- (cons (visit-ret ret-size) out)
- raw-size scm-size (1+ ret-size))))))))
- (define (compile-fixnum-fast-path a block-type
- fast-expr slow-expr)
- `((block #f ,block-type
- ((block #f ,void-block-type
- (,(local.get a)
- (ref.test ,i31-type)
- (i32.eqz)
- (br_if 0)
- ,(local.get a)
- (ref.cast ,i31-type)
- (i31.get_s)
- (local.tee $i0)
- (i32.const 1)
- (i32.and)
- (br_if 0)
- ,@fast-expr
- (br 1)))
- ,(local.get a)
- ,@slow-expr))))
- (define* (compile-fixnum-fixnum-fast-path a b block-type
- fast-expr slow-expr
- #:key (fast-checks '()))
- `((block #f ,block-type
- ((block #f ,void-block-type
- (,(local.get a)
- (ref.test ,i31-type)
- (i32.eqz)
- (br_if 0)
- ,(local.get b)
- (ref.test ,i31-type)
- (i32.eqz)
- (br_if 0)
- ,(local.get a)
- (ref.cast ,i31-type)
- (i31.get_s)
- (local.tee $i0)
- ,(local.get b)
- (ref.cast ,i31-type)
- (i31.get_s)
- (local.tee $i1)
- (i32.or)
- (i32.const 1)
- (i32.and)
- (br_if 0)
- ,@(append-map (lambda (checks)
- `(,@checks (br_if 0)))
- fast-checks)
- ,@fast-expr
- (br 1)))
- ,(local.get a)
- ,(local.get b)
- ,@slow-expr))))
- (define* (compile-fixnum-u64-fast-path a b block-type
- fast-expr slow-expr
- #:key (fast-checks '()))
- `((block #f ,block-type
- ((block #f ,void-block-type
- (,(local.get a)
- (ref.test ,i31-type)
- (i32.eqz)
- (br_if 0)
- ,(local.get a)
- (ref.cast ,i31-type)
- (i31.get_s)
- (local.tee $i0)
- (i32.const 1)
- (i32.and)
- (br_if 0)
- ,@(append-map (lambda (checks)
- `(,@checks (br_if 0)))
- fast-checks)
- ,@fast-expr
- (br 1)))
- ,(local.get a)
- ,(local.get b)
- ,@slow-expr))))
- (define (compile-values exp)
- (match exp
- (($ $const val) (compile-constant val))
- (($ $const-fun k) (compile-constant
- (make-static-procedure (func-label k))))
- (($ $values vals)
- (map local.get vals))
- (($ $code k)
- `((ref.func ,(func-label k))))
- (($ $prim name)
- (match name
- ('values `((global.get $values-primitive)))
- ('apply `((global.get $apply-primitive)))
- ('append `((global.get $append-primitive)))
- ('abort-to-prompt '((global.get $abort-to-prompt-primitive)))
- ('raise-exception '((global.get $raise-exception)))
- (_ (error "unhandled $prim" name))))
- (($ $primcall name param args)
- (match-primcall
- name param args
- (('inline-wasm
- ($ <func> id
- ($ <type-use> #f ($ <func-sig> params results))
- locals body)
- . args)
- (define named-locals
- (map
- (lambda (param arg)
- (match param
- (($ <param> id type)
- (cons id (match type
- (($ <ref-type> #f 'string)
- `(,(local.get arg)
- (ref.cast ,(make-ref-type #f '$string))
- (struct.get $string $str)))
- (($ <ref-type> #t 'extern)
- `(,(local.get arg)
- (ref.cast ,(make-ref-type #f '$extern-ref))
- (struct.get $extern-ref $val)))
- (($ <ref-type> #f 'eq)
- `(,(local.get arg)))
- (($ <ref-type>)
- `(,(local.get arg)
- (ref.cast ,type)))
- ('i32 `(,(local.get arg) (i32.wrap_i64)))
- ('i64 `(,(local.get arg)))
- ('f32 `(,(local.get arg) (f32.demote_f64)))
- ('f64 `(,(local.get arg)))
- (_ (error "unexpected param type" type)))))))
- params args))
- (define temp-locals
- (map (lambda (local)
- (match local
- (($ <local> id type)
- (unless (equal? type (type-for-named-temporary id))
- (error "unexpected local type for name" id type))
- id)))
- locals))
- (define (rename-inst inst)
- (match inst
- (((and inst (or 'block 'loop)) label type body)
- `((,inst ,label ,type ,(rename-expr body))))
- (('if label type consequent alternate)
- `((if ,label ,type
- ,(rename-expr consequent) ,(rename-expr alternate))))
- (('try label type body catches catch-all)
- `((try ,label ,type ,(rename-expr body)
- ,(map rename-expr catches)
- ,(and catch-all (rename-expr catch-all)))))
- (('try_delegate label type body handler)
- `((try_delegate ,label ,type ,(rename-expr body) ,handler)))
- (('local.get id)
- (or (assq-ref named-locals id)
- (begin
- (unless (memq id temp-locals)
- (error "unexpected local" id))
- `((local.get ,id)))))
- (((or 'local.set 'local.tee) id)
- ;; Only temporary locals are assignable from inline wasm.
- (unless (memq id temp-locals)
- (error "unexpected local" id))
- (list inst))
- (inst (list inst))))
- (define (rename-expr expr)
- (append-map rename-inst expr))
- (rename-expr body))
- (('wasm-import (? import? import))
- (set! imports (cons import imports))
- '())
- ;; These are the primcalls inserted by tailification to
- ;; handle stack-allocated return continuations.
- (('save reprs . args)
- (call-with-values (lambda () (analyze-saved-vars reprs))
- (lambda (analyzed spill-sizes)
- (define (save-var analyzed var)
- (match analyzed
- ((save . restore)
- (save (var-label var) spill-sizes))))
- (append-map save-var analyzed args))))
- (('drop reprs)
- (call-with-values (lambda () (analyze-saved-vars reprs))
- (lambda (analyzed spill-sizes)
- (define pop-sp
- (match-lambda
- ((sp . size)
- (if (zero? size)
- '()
- `((global.get ,sp)
- (i32.const ,size)
- (i32.sub)
- (global.set ,sp))))))
- (append-map pop-sp spill-sizes))))
- (('restore reprs)
- ;; Precondition: the order of the vars (and associated
- ;; reprs) is the same as for the corresponding save.
- (call-with-values (lambda () (analyze-saved-vars reprs))
- (lambda (analyzed spill-sizes)
- (define restore-var
- (match-lambda
- ((save . restore)
- (restore spill-sizes))))
- (append-map restore-var analyzed))))
- ;; Primcalls related to the module system. Not sure if we
- ;; will need these, at least in this form; the whole-program
- ;; compiler may take a different approach to modules.
- (('current-module #f)
- (error "unimplemented" exp))
- (('current-thread #f)
- (error "unimplemented" exp))
- (('define! #f mod sym)
- (error "unimplemented" exp))
- (('resolve (bound?) (name))
- (error "unimplemented" exp))
- (('cache-ref key)
- (error "unimplemented" exp))
- (('cache-set! key val)
- (error "unimplemented" exp))
- (('resolve-module public? (name))
- (error "unimplemented" exp))
- (('module-variable #f mod name)
- (error "unimplemented" exp))
- (('lookup #f mod name)
- (error "unimplemented" exp))
- (('lookup-bound #f mod name)
- (error "unimplemented" exp))
- (('lookup-bound-public (mod name))
- (error "unimplemented" exp))
- (('lookup-bound-private (mod name))
- (error "unimplemented" exp))
- ;; Object allocation. Probably most of these need to be
- ;; replaced with `cons` et al; see log.md.
- (('cons #f head tail)
- `((i32.const 0)
- ,(local.get head)
- ,(local.get tail)
- (struct.new $mutable-pair)))
- (('car #f pair)
- `(,(local.get pair)
- (ref.cast ,(make-ref-type #f '$pair))
- (struct.get $pair $car)))
- (('cdr #f pair)
- `(,(local.get pair)
- (ref.cast ,(make-ref-type #f '$pair))
- (struct.get $pair $cdr)))
- (('set-car! #f pair val)
- `(,(local.get pair)
- (ref.cast ,(make-ref-type #f '$pair))
- ,(local.get val)
- (struct.set $pair $car)))
- (('set-cdr! #f pair val)
- `(,(local.get pair)
- (ref.cast ,(make-ref-type #f '$pair))
- ,(local.get val)
- (struct.set $pair $cdr)))
- (('allocate-vector/immediate len)
- `((i32.const 0)
- ,@(compile-constant #f)
- (i32.const ,len)
- (array.new $raw-scmvector)
- (struct.new $mutable-vector)))
- (('allocate-vector #f len)
- `((i32.const 0)
- ,@(compile-constant #f)
- ,(local.get len)
- (i32.wrap_i64)
- (array.new $raw-scmvector)
- (struct.new $mutable-vector)))
- (('vector-length #f v)
- `(,(local.get v)
- (ref.cast ,(make-ref-type #f '$vector))
- (struct.get $vector $vals)
- (array.len)
- (i64.extend_i32_u)))
- (('vector-ref #f v idx)
- `(,(local.get v)
- (ref.cast ,(make-ref-type #f '$vector))
- (struct.get $vector $vals)
- ,(local.get idx)
- (i32.wrap_i64)
- (array.get $raw-scmvector)))
- (('vector-ref/immediate idx v)
- `(,(local.get v)
- (ref.cast ,(make-ref-type #f '$vector))
- (struct.get $vector $vals)
- (i32.const ,idx)
- (array.get $raw-scmvector)))
- (('vector-set! #f v idx val)
- `(,(local.get v)
- (ref.cast ,(make-ref-type #f '$vector))
- (struct.get $vector $vals)
- ,(local.get idx)
- (i32.wrap_i64)
- ,(local.get val)
- (array.set $raw-scmvector)))
- (('vector-set!/immediate idx v val)
- `(,(local.get v)
- (ref.cast ,(make-ref-type #f '$vector))
- (struct.get $vector $vals)
- (i32.const ,idx)
- ,(local.get val)
- (array.set $raw-scmvector)))
- (('box #f val)
- `((i32.const 0)
- ,(local.get val)
- (struct.new $variable)))
- (('box-ref #f v)
- `(,(local.get v)
- (ref.cast ,(make-ref-type #f '$variable))
- (struct.get $variable $val)))
- (('box-set! #f v val)
- `(,(local.get v)
- (ref.cast ,(make-ref-type #f '$variable))
- ,(local.get val)
- (struct.set $variable $val)))
- (('bv-length #f bv)
- `(,(local.get bv)
- (ref.cast ,(make-ref-type #f '$bytevector))
- (struct.get $bytevector $vals)
- (array.len)
- (i64.extend_i32_u)))
- (('bv-contents #f bv)
- `(,(local.get bv)
- (ref.cast ,(make-ref-type #f '$bytevector))
- (struct.get $bytevector $vals)))
- (('make-closure nfree code)
- `((i32.const 0)
- ,(local.get code)
- ,@(append-map (lambda (_) '((i32.const 0) (ref.i31)))
- (iota nfree))
- (struct.new ,(closure-type nfree))))
- (('closure-ref (idx . nfree) closure)
- (let ((type (closure-type nfree))
- ;; Two header fields: hash and proc.
- (pos (+ idx 2)))
- `(,(local.get closure)
- (ref.cast ,(make-ref-type #f type))
- (struct.get ,type ,pos))))
- (('closure-set! (idx . nfree) closure val)
- (let ((type (closure-type nfree))
- ;; Two header fields: hash and proc.
- (pos (+ idx 2)))
- `(,(local.get closure)
- (ref.cast ,(make-ref-type #f type))
- ,(local.get val)
- (struct.set ,type ,pos))))
- (('struct-vtable #f struct)
- `(,(local.get struct)
- (ref.cast ,(make-ref-type #f '$struct))
- (struct.get $struct $vtable)
- (ref.as_non_null)))
- (('vtable-size #f struct)
- `(,(local.get struct)
- (ref.cast ,(make-ref-type #f '$vtable))
- (struct.get $vtable $nfields)
- (ref.cast ,i31-type)
- (i31.get_s)
- (i32.const 1)
- (i32.shr_s)
- (i64.extend_i32_u)))
- (('allocate-struct nfields vtable)
- `((i32.const 0)
- ,(local.get vtable)
- (ref.cast ,(make-ref-type #f '$vtable))
- ,@(append-map (lambda (_) '((i32.const 0) (ref.i31)))
- (iota nfields))
- (struct.new ,(struct-type-name nfields))))
- (('struct-ref idx struct)
- (let ((type (struct-type-name (1+ idx)))
- ;; Two header fields: hash and vtable.
- (pos (+ idx 2)))
- `(,(local.get struct)
- (ref.cast ,(make-ref-type #f type))
- (struct.get ,type ,pos))))
- (('struct-set! idx struct val)
- (let ((type (struct-type-name (1+ idx)))
- ;; Two header fields: hash and vtable.
- (pos (+ idx 2)))
- `(,(local.get struct)
- (ref.cast ,(make-ref-type #f type))
- ,(local.get val)
- (struct.set ,type ,pos))))
- (('string-length #f str)
- `(,(local.get str)
- (ref.cast ,(make-ref-type #f '$string))
- (struct.get $string $str)
- (string.as_iter)
- (i32.const #x7fffffff)
- (stringview_iter.advance)
- (i64.extend_i32_u)))
- (('string-ref #f str idx)
- `(,(local.get str)
- (ref.cast ,(make-ref-type #f '$string))
- (struct.get $string $str)
- (string.as_iter)
- (local.tee $str_iter)
- ,(local.get idx)
- (i32.wrap_i64)
- (stringview_iter.advance)
- (drop)
- (local.get $str_iter)
- (stringview_iter.next)
- (i64.extend_i32_u)))
- (('string-set! #f str idx val)
- `(,(local.get str)
- ,(local.get idx)
- (i32.wrap_i64)
- ,(local.get val)
- (i32.wrap_i64)
- (call $string-set!)))
- (('symbol-hash #f sym)
- `(,(local.get sym)
- (ref.cast ,(make-ref-type #f '$symbol))
- (struct.get $symbol $hash)
- (i64.extend_i32_u)))
- ;; For the time being, atomic boxes are basically the same
- ;; as regular boxes.
- (('make-atomic-box #f val)
- `((i32.const 0)
- ,(local.get val)
- (struct.new $atomic-box)))
- (('atomic-box-ref #f box)
- `(,(local.get box)
- (ref.cast ,(make-ref-type #f '$atomic-box))
- (struct.get $atomic-box $val)))
- (('atomic-box-set! #f box val)
- `(,(local.get box)
- (ref.cast ,(make-ref-type #f '$atomic-box))
- ,(local.get val)
- (struct.set $atomic-box $val)))
- (('atomic-box-swap! #f box val)
- `(,(local.get box)
- (ref.cast ,(make-ref-type #f '$atomic-box))
- (struct.get $atomic-box $val)
- ,(local.get box)
- (ref.cast ,(make-ref-type #f '$atomic-box))
- ,(local.get val)
- (struct.set $atomic-box $val)))
- (('atomic-box-compare-and-swap! #f box expected desired)
- `(,(local.get box)
- (ref.cast ,(make-ref-type #f '$atomic-box))
- (struct.get $atomic-box $val)
- (local.tee $s0)
- ,(local.get expected)
- (ref.eq)
- (if #f ,void-block-type
- (,(local.get box)
- (ref.cast ,(make-ref-type #f '$atomic-box))
- ,(local.get desired)
- (struct.set $atomic-box $val))
- ())
- (local.get $s0)))
- (('f64->scm #f f64)
- `((i32.const 0)
- ,(local.get f64)
- (struct.new $flonum)))
- ;; Generic arithmetic. Emit a fixnum fast-path and a
- ;; callout to runtime functions for the slow path.
- (('add #f x y)
- (compile-fixnum-fixnum-fast-path
- x y scm-block-type
- '((local.get $i0) (local.get $i1) (call $fixnum-add))
- '((call $add))))
- (('sub #f x y)
- (compile-fixnum-fixnum-fast-path
- x y scm-block-type
- '((local.get $i0) (local.get $i1) (call $fixnum-sub))
- '((call $sub))))
- ;; FIXME: Reimplement fast paths
- (('add/immediate y x)
- (compile-fixnum-fast-path
- x scm-block-type
- `((local.get $i0) (i32.const ,(ash y 1)) (call $fixnum-add))
- `((i32.const ,(ash y 1))
- (ref.i31)
- (call $add))))
- (('sub/immediate y x)
- (compile-fixnum-fast-path
- x scm-block-type
- `((local.get $i0) (i32.const ,(ash y 1)) (call $fixnum-sub))
- `((i32.const ,(ash y 1))
- (ref.i31)
- (call $sub))))
- (('mul #f x y)
- (compile-fixnum-fixnum-fast-path
- x y scm-block-type
- '((local.get $i0) (local.get $i1) (call $fixnum-mul))
- '((call $mul))))
- (('mul/immediate y x)
- (compile-fixnum-fast-path
- x scm-block-type
- `((local.get $i0) (i32.const ,(ash y 1)) (call $fixnum-mul))
- `((i32.const ,(ash y 1))
- (ref.i31)
- (call $mul))))
- (('div #f x y)
- `(,(local.get x)
- ,(local.get y)
- (call $div)))
- (('quo #f x y)
- (compile-fixnum-fixnum-fast-path
- x y scm-block-type
- '((local.get $i0) (i32.const 1) (i32.shr_s)
- (local.get $i1) (i32.const 1) (i32.shr_s)
- (i32.div_s)
- (i32.const 1) (i32.shl)
- (ref.i31))
- '((call $quo))
- #:fast-checks '(((local.get $i1) (i32.eqz))
- ((local.get $i1) (i32.const -2) (i32.eq)))))
- (('rem #f x y)
- (compile-fixnum-fixnum-fast-path
- x y scm-block-type
- '((local.get $i0) (i32.const 1) (i32.shr_s)
- (local.get $i1) (i32.const 1) (i32.shr_s)
- (i32.rem_s)
- (i32.const 1) (i32.shl)
- (ref.i31))
- '((call $rem))
- #:fast-checks '(((local.get $i1) (i32.eqz)))))
- (('mod #f x y)
- (compile-fixnum-fixnum-fast-path
- x y scm-block-type
- `((local.get $i0) (i32.const 1) (i32.shr_s)
- (local.get $i1) (i32.const 1) (i32.shr_s)
- (i32.rem_s)
- ;; Tag as fixnum.
- (i32.const 1) (i32.shl)
- ;; Adjust if y and r have opposite signs.
- (local.tee $i0)
- (local.get $i1)
- (i32.const 0)
- (i32.gt_s)
- (if #f ,i32-block-type
- ((local.get $i0) (i32.const 0) (i32.lt_s))
- ((local.get $i0) (i32.const 0) (i32.gt_s)))
- (if #f ,i32-block-type
- ;; Adjust by adding tagged $i1.
- ((local.get $i0) (local.get $i1) (i32.add))
- ((local.get $i0)))
- (ref.i31))
- '((call $mod))
- #:fast-checks '(((local.get $i1) (i32.eqz)))))
- ;; Integer bitwise operations. Fast path for fixnums and
- ;; callout otherwise.
- (('logand #f x y)
- (compile-fixnum-fixnum-fast-path
- x y scm-block-type
- '((local.get $i0) (local.get $i1) (i32.and) (ref.i31))
- '((call $logand))))
- (('logand/immediate y x)
- (compile-fixnum-fast-path
- x scm-block-type
- `((local.get $i0) (i32.const ,(ash y 1)) (i32.and) (ref.i31))
- `((i32.const ,(ash y 1))
- (ref.i31)
- (call $logand))))
- (('logior #f x y)
- (compile-fixnum-fixnum-fast-path
- x y scm-block-type
- '((local.get $i0) (local.get $i1) (i32.or) (ref.i31))
- '((call $logior))))
- (('logxor #f x y)
- (compile-fixnum-fixnum-fast-path
- x y scm-block-type
- '((local.get $i0) (local.get $i1) (i32.xor) (ref.i31))
- '((call $logxor))))
- (('logsub #f x y)
- (compile-fixnum-fixnum-fast-path
- x y scm-block-type
- '((local.get $i0)
- (local.get $i1)
- (i32.const -1)
- (i32.xor)
- (i32.and)
- (ref.i31))
- '((call $logsub))))
- (('rsh #f x y)
- (compile-fixnum-u64-fast-path
- x y scm-block-type
- `((local.get $i0)
- ;; Clamp shift to 31 bits; fixnum is smaller than that.
- (i64.const 31)
- ,(local.get y)
- (i64.lt_u)
- (if #f ,i32-block-type
- ((i32.const 31))
- (,(local.get y)
- (i32.wrap_i64)))
- (i32.shr_s)
- ;; Mask off fixnum bit.
- (i32.const -2)
- (i32.and)
- (ref.i31))
- '((call $rsh))))
- (('lsh #f x y)
- (compile-fixnum-u64-fast-path
- x y scm-block-type
- `((local.get $i0)
- (i32.const 1)
- (i32.shr_s)
- (i64.extend_i32_s)
- ,(local.get y)
- (i64.shl)
- (call $s64->scm))
- '((call $lsh))
- #:fast-checks `((,(local.get y) (i64.const 32) (i64.gt_u)))))
- (('rsh/immediate y x)
- (compile-fixnum-fast-path
- x scm-block-type
- (if (< y 31)
- `((local.get $i0)
- (i32.const ,y)
- (i32.shr_s)
- (i32.const -2)
- (i32.and)
- (ref.i31))
- `((i32.const 0)
- (ref.i31)))
- `((i64.const ,y)
- (call $rsh))))
- (('lsh/immediate y x)
- (if (<= y 32)
- (compile-fixnum-fast-path
- x scm-block-type
- `((local.get $i0)
- (i32.const 1)
- (i32.shr_s)
- (i64.extend_i32_s)
- (i64.const ,y)
- (i64.shl)
- (call $s64->scm))
- `((i64.const ,y)
- (call $lsh)))
- `((i64.const ,y)
- (call $lsh))))
- ;; Arithmetic on real numbers.
- (('inexact #f x)
- `(,(local.get x)
- (call $inexact)))
- (('abs #f x)
- `(,(local.get x)
- (call $abs)))
- (('sqrt #f x)
- `(,(local.get x)
- (call $sqrt)))
- (('floor #f x)
- `(,(local.get x)
- (call $floor)))
- (('ceiling #f x)
- `(,(local.get x)
- (call $ceiling)))
- ;; Trig functions. Probably just call out for now.
- (('sin #f x)
- `(,(local.get x)
- (call $sin)))
- (('cos #f x)
- `(,(local.get x)
- (call $cos)))
- (('tan #f x)
- `(,(local.get x)
- (call $tan)))
- (('asin #f x)
- `(,(local.get x)
- (call $asin)))
- (('acos #f x)
- `(,(local.get x)
- (call $acos)))
- (('atan #f x)
- `(,(local.get x)
- (call $atan)))
- (('atan2 #f x y)
- `(,(local.get x)
- ,(local.get y)
- (call $atan2)))
- ;; Unboxed integer arithmetic and logical operations.
- (((or 'load-u64 'load-s64) s64)
- `((i64.const ,s64)))
- (((or 's64->u64 'u64->s64) #f arg)
- `(,(local.get arg)))
- (((or 'uadd 'sadd) #f x y)
- `(,(local.get x)
- ,(local.get y)
- (i64.add)))
- (((or 'usub 'ssub) #f x y)
- `(,(local.get x)
- ,(local.get y)
- (i64.sub)))
- (((or 'umul 'smul) #f x y)
- `(,(local.get x)
- ,(local.get y)
- (i64.mul)))
- (((or 'sadd/immediate 'uadd/immediate) y x)
- `(,(local.get x)
- (i64.const ,y)
- (i64.add)))
- (((or 'usub/immediate 'ssub/immediate) y x)
- `(,(local.get x)
- (i64.const ,y)
- (i64.sub)))
- (((or 'umul/immediate 'smul/immediate) y x)
- `(,(local.get x)
- (i64.const ,y)
- (i64.mul)))
- (('ulogand #f x y)
- `(,(local.get x)
- ,(local.get y)
- (i64.and)))
- (('ulogand/immediate y x)
- `(,(local.get x)
- (i64.const ,y)
- (i64.and)))
- (('ulogior #f x y)
- `(,(local.get x)
- ,(local.get y)
- (i64.or)))
- (('ulogsub #f x y)
- `(,(local.get x)
- ,(local.get y)
- (i64.not)
- (i64.or)))
- (('ulogxor #f x y)
- `(,(local.get x)
- ,(local.get y)
- (i64.xor)))
- (('ursh #f x y)
- `(,(local.get x)
- ,(local.get y)
- (i64.shr_u)))
- (('srsh #f x y)
- `(,(local.get x)
- ,(local.get y)
- (i64.shr_s)))
- (('ulsh #f x y)
- `(,(local.get x)
- ,(local.get y)
- (i64.shl)))
- (('ursh/immediate y x)
- `(,(local.get x)
- (i64.const ,y)
- (i64.shr_u)))
- (('srsh/immediate y x)
- `(,(local.get x)
- (i64.const ,y)
- (i64.shr_s)))
- (('ulsh/immediate y x)
- `(,(local.get x)
- (i64.const ,y)
- (i64.shl)))
- (('slsh/immediate y x)
- `(,(local.get x)
- (i64.const ,y)
- (i64.shl)))
- ;; Unboxed floating-point arithmetic and trig.
- (('load-f64 f64)
- `((f64.const ,f64)))
- (('s64->f64 #f arg)
- `(,(local.get arg)
- (f64.reinterpret_i64)))
- (('fadd #f x y)
- `(,(local.get x)
- ,(local.get y)
- (f64.add)))
- (('fadd/immediate y x)
- `(,(local.get x)
- (f64.const ,y)
- (f64.add)))
- (('fsub #f x y)
- `(,(local.get x)
- ,(local.get y)
- (f64.sub)))
- (('fmul #f x y)
- `(,(local.get x)
- ,(local.get y)
- (f64.mul)))
- (('fdiv #f x y)
- `(,(local.get x)
- ,(local.get y)
- (f64.div)))
- (('fabs #f x)
- `(,(local.get x)
- (f64.abs)))
- (('fsqrt #f x)
- `(,(local.get x)
- (f64.sqrt)))
- (('ffloor #f x)
- `(,(local.get x)
- (f64.floor)))
- (('fceiling #f x)
- `(,(local.get x)
- (f64.ceil)))
- (('fsin #f x)
- `(,(local.get x)
- (call $fsin)))
- (('fcos #f x)
- `(,(local.get x)
- (call $fcos)))
- (('ftan #f x)
- `(,(local.get x)
- (call $ftan)))
- (('fasin #f x)
- `(,(local.get x)
- (call $fasin)))
- (('facos #f x)
- `(,(local.get x)
- (call $facos)))
- (('fatan #f x)
- `(,(local.get x)
- (call $fatan)))
- (('fatan2 #f x y)
- `(,(local.get x)
- ,(local.get y)
- (call $fatan2)))
- ;; Misc.
- (('string->bignum #f x)
- `((i32.const 0)
- ,(local.get x)
- (ref.cast ,(make-ref-type #f '$string))
- (struct.get $string $str)
- (call $string->bignum)
- (struct.new $bignum)))
- (('string->symbol #f x)
- `(,(local.get x)
- (ref.cast ,(make-ref-type #f '$string))
- (call $string->symbol)))
- (('symbol->string #f x)
- `(,(local.get x)
- (ref.cast ,(make-ref-type #f '$symbol))
- (struct.get $symbol $name)))
- (('symbol->keyword #f x)
- `(,(local.get x)
- (ref.cast ,(make-ref-type #f '$symbol))
- (call $symbol->keyword)))
- (('keyword->symbol #f x)
- `(,(local.get x)
- (ref.cast ,(make-ref-type #f '$keyword))
- (struct.get $keyword $name)))
- (('string->utf8 #f str)
- `((i32.const 0)
- ,(local.get str)
- (ref.cast ,(make-ref-type #f '$string))
- (struct.get $string $str)
- (call $string->wtf8)
- (struct.new $bytevector)))
- (('string-utf8-length #f str)
- `(,(local.get str)
- (ref.cast ,(make-ref-type #f '$string))
- (struct.get $string $str)
- (string.measure_wtf8)))
- (('utf8->string #f bv)
- `((i32.const 0)
- ,(local.get bv)
- (ref.cast ,(make-ref-type #f '$bytevector))
- (struct.get $bytevector $vals)
- (call $wtf8->string)
- (struct.new $mutable-string)))
- ;; Unboxing and boxing numbers.
- (('scm->f64 #f src)
- `(,(local.get src)
- (call $scm->f64)))
- (('flonum->f64 #f src)
- `(,(local.get src)
- (ref.cast ,(make-ref-type #f '$flonum))
- (struct.get $flonum $val)))
- (('compnum-real #f src)
- `(,(local.get src)
- (ref.cast ,(make-ref-type #f '$complex))
- (struct.get $complex $real)))
- (('compnum-imag #f src)
- `(,(local.get src)
- (ref.cast ,(make-ref-type #f '$complex))
- (struct.get $complex $imag)))
- (('scm->s64 #f src)
- (compile-fixnum-fast-path
- src i64-block-type
- `((local.get $i0)
- (i32.const 1)
- (i32.shr_s)
- (i64.extend_i32_s))
- `((call $scm->s64))))
- (('scm->u64 #f src)
- `(,(local.get src)
- (call $scm->u64)))
- (('scm->u64/truncate #f src)
- (compile-fixnum-fast-path
- src i64-block-type
- `((local.get $i0)
- (i32.const 1)
- (i32.shr_s)
- (i64.extend_i32_s))
- '((call $scm->u64/truncate))))
- (('u64->scm #f src)
- `(,(local.get src)
- (i64.const ,(ash 1 29))
- (i64.lt_u)
- (if #f ,scm-block-type
- (,(local.get src)
- (i32.wrap_i64)
- (i32.const 1)
- (i32.shl)
- (ref.i31))
- (,(local.get src)
- (call $u64->bignum)))))
- (('s64->scm #f src)
- `(,(local.get src)
- (call $s64->scm)))
- ;; For native Guile, these bytevector accesses take three
- ;; parameters: the object itself, which is unused but keeps
- ;; the pointer alive; a pointer to the actual bytes; and an
- ;; index. For WebAssembly we pass the $raw-bytevector as
- ;; the ptr and not reference the object.
- (('u8-ref ann obj ptr idx)
- `(,(local.get ptr)
- ,(local.get idx)
- (i32.wrap_i64)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u)))
- (('s8-ref ann obj ptr idx)
- `(,(local.get ptr)
- ,(local.get idx)
- (i32.wrap_i64)
- (array.get_s $raw-bytevector)
- (i64.extend_i32_s)))
- ;; Annoyingly there are no native multi-byte accesses to i8
- ;; arrays.
- (('u16-ref ann obj ptr idx)
- `(,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64)
- (array.get_u $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
- (array.get_u $raw-bytevector)
- (i32.const 8) (i32.shl)
- (i32.or)
- (i64.extend_i32_u)))
- (('s16-ref ann obj ptr idx)
- `(,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64)
- (array.get_u $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
- (array.get_s $raw-bytevector)
- (i32.const 8) (i32.shl)
- (i32.or)
- (i64.extend_i32_s)))
- (('u32-ref ann obj ptr idx)
- `(,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64)
- (array.get_u $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
- (array.get_u $raw-bytevector)
- (i32.const 8) (i32.shl)
- (i32.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
- (array.get_u $raw-bytevector)
- (i32.const 16) (i32.shl)
- (i32.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
- (array.get_u $raw-bytevector)
- (i32.const 24) (i32.shl)
- (i32.or)
- (i64.extend_i32_u)))
- (('s32-ref ann obj ptr idx)
- `(,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64)
- (array.get_u $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
- (array.get_u $raw-bytevector)
- (i32.const 8) (i32.shl)
- (i32.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
- (array.get_u $raw-bytevector)
- (i32.const 16) (i32.shl)
- (i32.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
- (array.get_s $raw-bytevector)
- (i32.const 24) (i32.shl)
- (i32.or)
- (i64.extend_i32_s)))
- (((or 'u64-ref 's64-ref) ann obj ptr idx)
- `(,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 8) (i64.shl)
- (i64.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 16) (i64.shl)
- (i64.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 24) (i64.shl)
- (i64.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 4) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 32) (i64.shl)
- (i64.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 5) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 40) (i64.shl)
- (i64.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 6) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 48) (i64.shl)
- (i64.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 7) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 56) (i64.shl)
- (i64.or)))
- (('f32-ref ann obj ptr idx)
- `(,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64)
- (array.get_u $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
- (array.get_u $raw-bytevector)
- (i32.const 8) (i32.shl)
- (i32.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
- (array.get_u $raw-bytevector)
- (i32.const 16) (i32.shl)
- (i32.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
- (array.get_s $raw-bytevector)
- (i32.const 24) (i32.shl)
- (i32.or)
- (f32.reinterpret_i32)
- (f64.promote_f32)))
- (('f64-ref ann obj ptr idx)
- `(,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 8) (i64.shl)
- (i64.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 16) (i64.shl)
- (i64.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 24) (i64.shl)
- (i64.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 4) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 32) (i64.shl)
- (i64.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 5) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 40) (i64.shl)
- (i64.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 6) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 48) (i64.shl)
- (i64.or)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 7) (i32.add)
- (array.get_u $raw-bytevector)
- (i64.extend_i32_u) (i64.const 56) (i64.shl)
- (i64.or)
- (f64.reinterpret_i64)))
- (((or 'u8-set! 's8-set!) ann obj ptr idx val)
- `(,(local.get ptr)
- ,(local.get idx)
- (i32.wrap_i64)
- ,(local.get val)
- (i32.wrap_i64)
- (array.set $raw-bytevector)))
- (((or 'u16-set! 's16-set!) ann obj ptr idx val)
- `(,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64)
- ,(local.get val) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
- ,(local.get val) (i64.const 8) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)))
- (((or 'u32-set! 's32-set!) ann obj ptr idx val)
- `(,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64)
- ,(local.get val) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
- ,(local.get val) (i64.const 8) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
- ,(local.get val) (i64.const 16) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
- ,(local.get val) (i64.const 24) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)))
- (((or 'u64-set! 's64-set!) ann obj ptr idx val)
- `(,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64)
- ,(local.get val) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
- ,(local.get val) (i64.const 8) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
- ,(local.get val) (i64.const 16) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
- ,(local.get val) (i64.const 24) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 4) (i32.add)
- ,(local.get val) (i64.const 32) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 5) (i32.add)
- ,(local.get val) (i64.const 40) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 6) (i32.add)
- ,(local.get val) (i64.const 48) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 7) (i32.add)
- ,(local.get val) (i64.const 56) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)))
- (('f32-set! ann obj ptr idx val)
- `(,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64)
- ,(local.get val) (f32.demote_f64) (i32.reinterpret_f32)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
- ,(local.get val) (f32.demote_f64) (i32.reinterpret_f32) (i32.const 8) (i32.shr_u)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
- ,(local.get val) (f32.demote_f64) (i32.reinterpret_f32) (i32.const 16) (i32.shr_u)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
- ,(local.get val) (f32.demote_f64) (i32.reinterpret_f32) (i32.const 24) (i32.shr_u)
- (array.set $raw-bytevector)))
- (('f64-set! ann obj ptr idx val)
- `(,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64)
- ,(local.get val) (i64.reinterpret_f64) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 1) (i32.add)
- ,(local.get val) (i64.reinterpret_f64) (i64.const 8) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 2) (i32.add)
- ,(local.get val) (i64.reinterpret_f64) (i64.const 16) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 3) (i32.add)
- ,(local.get val) (i64.reinterpret_f64) (i64.const 24) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 4) (i32.add)
- ,(local.get val) (i64.reinterpret_f64) (i64.const 32) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 5) (i32.add)
- ,(local.get val) (i64.reinterpret_f64) (i64.const 40) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 6) (i32.add)
- ,(local.get val) (i64.reinterpret_f64) (i64.const 48) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)
- ,(local.get ptr)
- ,(local.get idx) (i32.wrap_i64) (i32.const 7) (i32.add)
- ,(local.get val) (i64.reinterpret_f64) (i64.const 56) (i64.shr_u) (i32.wrap_i64)
- (array.set $raw-bytevector)))
- ;; Infallible unboxing of fixnums and chars.
- (('untag-fixnum #f src)
- `(,(local.get src)
- (ref.cast ,i31-type)
- (i31.get_s)
- (i32.const 1)
- (i32.shr_s)
- (i64.extend_i32_s)))
- (('tag-fixnum #f src)
- `(,(local.get src)
- (i32.wrap_i64)
- (i32.const 1)
- (i32.shl)
- (ref.i31)))
- (('untag-char #f src)
- `(,(local.get src)
- (ref.cast ,i31-type)
- (i31.get_s)
- (i32.const 2)
- (i32.shr_u)
- (i64.extend_i32_u)))
- (('tag-char #f src)
- `(,(local.get src)
- (i32.wrap_i64)
- (i32.const 2)
- (i32.shl)
- (i32.const #b11)
- (i32.or)
- (ref.i31)))
- ;; The dynamic state. Implement using runtime.
- (('push-prompt escape-only? tag handler)
- `((global.get $raw-sp)
- (global.get $scm-sp)
- (global.get $ret-sp)
- (i32.const ,(if escape-only? 1 0))
- ,(local.get tag)
- ,(local.get handler)
- (struct.new $dynprompt)
- (call $push-dyn)))
- (('push-fluid #f fluid val)
- ;; Going to assume (via the ref.cast) that someone checked
- ;; that fluid is in fact a fluid. Not the case currently
- ;; in Guile; this should be fixed.
- `(,(local.get fluid)
- (ref.cast ,(make-ref-type #f '$fluid))
- ,(local.get val)
- (call $push-fluid)))
- (('pop-fluid #f)
- `((call $pop-fluid)))
- (('fluid-ref #f fluid)
- `(,(local.get fluid)
- (ref.cast ,(make-ref-type #f '$fluid))
- (call $fluid-ref)))
- (('fluid-set! #f fluid value)
- `(,(local.get fluid)
- (ref.cast ,(make-ref-type #f '$fluid))
- ,(local.get value)
- (call $fluid-set!)))
- (('push-dynamic-state #f state)
- ;; Assuming that it's a $dynamic-state, as with fluids.
- `(,(local.get state)
- (ref.cast ,(make-ref-type #f '$dynamic-state))
- (call $push-dynamic-state)))
- (('pop-dynamic-state #f)
- `((call $pop-dynamic-state)))
- (('wind #f winder unwinder)
- `(,(local.get winder)
- (ref.cast ,(make-ref-type #f '$proc))
- ,(local.get unwinder)
- (ref.cast ,(make-ref-type #f '$proc))
- (struct.new $dynwind)
- (call $push-dyn)))
- (('unwind #f)
- `((global.get $dyn-sp)
- (i32.const 1)
- (i32.sub)
- (global.set $dyn-sp)))
- ((name param . args)
- (error "unexpected primcall" name param args))))
- (_ (error "unexpected expression" exp))))
- (define (compile-test op param args)
- (match (vector op param args)
- ;; Immediate type tag predicates.
- (#('fixnum? #f (a))
- `(,(local.get a)
- (ref.test ,i31-type)
- (if #f ,i32-block-type
- (,(local.get a)
- (ref.cast ,i31-type)
- (i31.get_s)
- (i32.const #b1)
- (i32.and)
- (i32.eqz))
- ((i32.const 0)))))
- ;; FIXME: elide heap-object? tests if they just guard a more
- ;; specific test.
- (#('heap-object? #f (a))
- `(,(local.get a) (ref.test ,(make-ref-type #f '$heap-object))))
- (#('char? #f (a))
- `(,(local.get a)
- (ref.test ,i31-type)
- (if #f ,i32-block-type
- (,(local.get a)
- (ref.cast ,i31-type)
- (i31.get_u)
- (i32.const #b11)
- (i32.and)
- (i32.const #b11)
- (i32.eq))
- ((i32.const 0)))))
- (#('eq-constant? imm (a))
- `(,(local.get a)
- ,@(compile-constant imm)
- (ref.eq)))
- (#('undefined? #f (a))
- `(,(local.get a)
- (i32.const 57)
- (ref.i31)
- (ref.eq)))
- (#('null? #f (a))
- `(,(local.get a)
- (ref.test ,i31-type)
- (if #f ,i32-block-type
- (,(local.get a)
- (ref.cast ,i31-type)
- (i31.get_s)
- (i32.const #b110111)
- (i32.and)
- (i32.const #b000101)
- (i32.eq))
- ((i32.const 0)))))
- (#('false? #f (a))
- `(,(local.get a)
- (ref.test ,i31-type)
- (if #f ,i32-block-type
- (,(local.get a)
- (ref.cast ,i31-type)
- (i31.get_s)
- (i32.const #b111011)
- (i32.and)
- (i32.const #b000001)
- (i32.eq))
- ((i32.const 0)))))
- (#('nil? #f (a))
- `(,(local.get a)
- (ref.test ,i31-type)
- (if #f ,i32-block-type
- (,(local.get a)
- (ref.cast ,i31-type)
- (i31.get_s)
- (i32.const #b110011)
- (i32.and)
- (i32.const #b000001)
- (i32.eq))
- ((i32.const 0)))))
- (#('pair? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$pair))))
- (#('struct? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$struct))))
- (#('symbol? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$symbol))))
- (#('variable? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$variable))))
- (#('vector? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$vector))))
- (#('mutable-vector? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$mutable-vector))))
- (#('immutable-vector? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$vector))))
- (#('string? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$string))))
- (#('heap-number? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$heap-number))))
- (#('hash-table? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$hash-table))))
- (#('fluid? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$fluid))))
- (#('dynamic-state? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$dynamic-state))))
- (#('keyword? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$keyword))))
- (#('atomic-box? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$atomic-box))))
- (#('syntax? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$syntax))))
- (#('procedure? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$proc))))
- (#('bytevector? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$bytevector))))
- (#('weak-table? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$weak-table))))
- (#('bitvector? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$bitvector))))
- (#('port? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$port))))
- (#('bignum? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$bignum))))
- (#('flonum? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$flonum))))
- (#('compnum? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$complex))))
- (#('fracnum? #f (a)) `(,(local.get a) (ref.test ,(make-ref-type #f '$fraction))))
- (#('eq? #f (a b)) `(,(local.get a) ,(local.get b) (ref.eq)))
- (#('vtable-vtable? #f (vtable))
- `(,(local.get vtable)
- (ref.cast ,(make-ref-type #f '$vtable))
- (struct.get $vtable $vtable)
- (ref.test ,(make-ref-type #f '$vtable-vtable))))
- (#('heap-numbers-equal? #f (a b))
- `(,(local.get a) ,(local.get b) (call $heap-numbers-equal?)))
- (#('< #f (a b))
- (compile-fixnum-fixnum-fast-path a b i32-block-type
- '((local.get $i0)
- (local.get $i1)
- (i32.lt_s))
- '((call $slow-<))))
- (#('<= #f (a b))
- (compile-fixnum-fixnum-fast-path a b i32-block-type
- '((local.get $i0)
- (local.get $i1)
- (i32.le_s))
- '((call $slow-<=))))
- (#('= #f (a b))
- (compile-fixnum-fixnum-fast-path a b i32-block-type
- '((local.get $i0)
- (local.get $i1)
- (i32.eq))
- '((call $slow-=))))
- (#('u64-< #f (a b))
- `(,(local.get a)
- ,(local.get b)
- (i64.lt_u)))
- (#('u64-imm-< b (a))
- `(,(local.get a)
- (i64.const ,b)
- (i64.lt_u)))
- (#('imm-u64-< b (a))
- `((i64.const ,b)
- ,(local.get a)
- (i64.lt_u)))
- (#((or 'u64-= 's64-=) #f (a b))
- `(,(local.get a)
- ,(local.get b)
- (i64.eq)))
- (#((or 'u64-imm-= 's64-imm-=) b (a))
- `(,(local.get a)
- (i64.const ,b)
- (i64.eq)))
- (#('s64-< #f (a b))
- `(,(local.get a)
- ,(local.get b)
- (i64.lt_s)))
- (#('s64-imm-< b (a))
- `(,(local.get a)
- (i64.const ,b)
- (i64.lt_s)))
- (#('imm-s64-< b (a))
- `((i64.const ,b)
- ,(local.get a)
- (i64.lt_s)))
- (#('f64-< #f (a b))
- `(,(local.get a)
- ,(local.get b)
- (f64.lt)))
- (#('f64-<= #f (a b))
- `(,(local.get a)
- ,(local.get b)
- (f64.le)))
- (#('f64-= #f (a b))
- `(,(local.get a)
- ,(local.get b)
- (f64.eq)))
- (#('f64-finite? #f (a))
- `(,(local.get a)
- ,(local.get a)
- (f64.sub)
- (f64.const 0)
- (f64.eq)))
- (#('f64-int? #f (a))
- `(,(local.get a)
- (f64.trunc)
- ,(local.get a)
- (f64.sub)
- (f64.const 0)
- (f64.eq)))))
- (define (compile-throw src op param args)
- ;; FIXME: Instead of dying, we need to implement exception
- ;; handling, as in Guile.
- (match (vector op param args)
- (#('raise-type-error #(subr pos what) (val))
- `((string.const ,subr)
- (string.const ,what)
- ,(local.get val)
- (return_call $raise-type-error)))
- (#('raise-range-error #(subr pos) (val))
- `((string.const ,subr)
- ,(local.get val)
- (return_call $raise-range-error)))
- (#('raise-arity-error #(subr) (val))
- `(,(if subr
- `(string.const ,subr)
- '(ref.null string))
- ,(local.get val)
- (return_call $raise-arity-error)))
- (#('raise-unbound-error #(subr) (val))
- `((string.const ,subr)
- ,(local.get val)
- (return_call $raise-unbound-error)))
- (#('raise-exception #f (exn))
- `(,(local.get exn)
- (return_call $raise-exception)))))
- ;; See "Beyond Relooper: Recursive Translation of Unstructured
- ;; Control Flow to Structured Control Flow", Norman Ramsey, ICFP
- ;; 2022.
- (define (make-ctx next-label stack) (cons next-label stack))
- (define (push-loop label ctx)
- (match ctx
- ((next-label . stack)
- (make-ctx next-label
- (acons 'loop-headed-by label stack)))))
- (define (push-block label ctx)
- (match ctx
- ((next-label . stack)
- (make-ctx label
- (acons 'block-followed-by label stack)))))
- (define (push-if ctx)
- (match ctx
- ((next-label . stack)
- (make-ctx next-label (cons 'if-then-else stack)))))
- (define (lookup-label k ctx)
- (match ctx
- ((next-label . stack)
- (let lp ((stack stack) (depth 0))
- (match stack
- (('if-then-else . stack) (lp stack (1+ depth)))
- ((((or 'loop-headed-by 'block-followed-by) . label) . stack)
- (if (eqv? label k)
- depth
- (lp stack (1+ depth))))
- (_ (error "block label not found" k ctx)))))))
- (define (do-tree label ctx)
- (define (code-for-label ctx)
- (let ((children (intmap-ref dom-children label)))
- (node-within
- label
- (match (intmap-ref cps label)
- (($ $kargs names vars ($ $switch))
- children)
- (_
- (intset-filter merge-cont? children)))
- ctx)))
- (if (loop-cont? label)
- `((loop #f ,void-block-type
- ,(code-for-label (push-loop label ctx))))
- (code-for-label ctx)))
- (define (do-branch pred succ ctx)
- (cond
- ((or (<= succ pred)
- (merge-cont? succ))
- ;; Backward branch or branch to merge: jump.
- (match ctx
- ((next-label . stack)
- (if (eqv? succ next-label)
- '()
- `((br ,(lookup-label succ ctx)))))))
- (else
- ;; Otherwise render successor inline.
- (do-tree succ ctx))))
- (define (node-within label ys ctx)
- (call-with-values (lambda () (intset-pop-right ys))
- (lambda (ys y)
- (match y
- (#f (emit-cont label ctx))
- (y `((block #f ,void-block-type
- ,(node-within label ys (push-block y ctx)))
- ,@(do-tree y ctx)))))))
- (define (emit-term label term ctx)
- ;; could change to bind names at continuation?
- (match term
- (($ $continue k src exp)
- (match (intmap-ref cps k)
- (($ $ktail)
- (compile-tail exp))
- (($ $kargs _ vars)
- `(,@(compile-values exp)
- ,@(reverse (map local.set vars))
- ,@(do-branch label k ctx)))
- (($ $kreceive ($ $arity req () rest () #f) kargs)
- (error "kreceive should be tailified away"))))
- (($ $branch kf kt src op param args)
- `(,@(compile-test op param args)
- (if #f ,void-block-type
- ,(do-branch label kt (push-if ctx))
- ,(do-branch label kf (push-if ctx)))))
- (($ $switch kf kt* src arg)
- `(,(local.get arg)
- (i32.wrap_i64)
- (br_table ,(map (lambda (k) (lookup-label k ctx)) kt*)
- ,(lookup-label kf ctx))))
- (($ $prompt k kh src escape? tag)
- (error "prompts should be removed by tailification?"))
- (($ $throw src op param args)
- (compile-throw src op param args))))
- (define (emit-fun/dispatch label self kentry ctx)
- ;; An arity-checking function; let the clause check
- ;; the arity.
- (let ((tail (do-branch label kentry ctx)))
- (if (and self (var-used? self))
- `(,@(arg-ref 0)
- ,(local.set self)
- . ,tail)
- tail)))
- (define (emit-fun/direct label vars ctx)
- ;; A function whose callers all pass the expected
- ;; number of arguments.
- ;; FIXME: no need to rebind vars.
- `(,@(append-map (lambda (var idx)
- `((local.get ,idx)
- ,(local.set var)))
- vars (iota (length vars)))
- ,@(do-tree label ctx)))
- (define (emit-clause label arity kbody kalt ctx)
- (match arity
- (($ $arity req opt rest kw allow-other-keys?)
- (match (intmap-ref cps kbody)
- (($ $kargs names vars)
- (define nreq (length req))
- (define nopt (length opt))
- (define (add-closure n) (if has-closure? (1+ n) n))
- (define req-vars (list-head vars nreq))
- (define opt-vars (list-head (list-tail vars nreq) nopt))
- (define rest-var
- (and rest (car (list-tail vars (+ nreq nopt)))))
- (define strict? rest)
- (define (check-nargs count test)
- `((local.get $nargs)
- (i32.const ,(add-closure count))
- (,test)))
- (define (branch-on-arity-mismatch checks)
- `(,@checks
- (if #f ,void-block-type
- ,(if kalt
- (do-branch label kalt ctx)
- `(,(match func-name
- (#f '(ref.null string))
- (name
- `(string.const
- ,(symbol->string func-name))))
- (local.get $arg0)
- (return_call $raise-arity-error)))
- ())))
- (define compute-npos
- (cond
- ((and (not allow-other-keys?) (null? kw))
- '())
- ((and (zero? nopt) (not rest))
- `((i32.const ,(add-closure nreq))
- (local.set $npos)))
- (else
- `((local.get $nargs)
- (local.get $arg0)
- (local.get $arg1)
- (local.get $arg2)
- (i32.const ,(add-closure nreq))
- (call $compute-npositional/kwargs)
- (local.set $npos)))))
- (define check-arity
- (cond
- (elide-arity-check?
- (unless (zero? nopt)
- (error "unexpected optargs with unchecked arity"))
- (unless (null? kw)
- (error "unexpected kwargs with unchecked arity"))
- '())
- ((or allow-other-keys? (pair? kw))
- (define (maybe-branch-on-arity-mismatch checks)
- (if (null? checks)
- '()
- (branch-on-arity-mismatch checks)))
- (define (prepend-nreq-check checks)
- (if (zero? nreq)
- checks
- `(,@(check-nargs nreq 'i32.lt_u)
- ,@(if (null? checks)
- '()
- `((if #f ,i32-block-type
- ((i32.const 1))
- ,checks))))))
- (maybe-branch-on-arity-mismatch
- (prepend-nreq-check
- (cond
- (rest
- '())
- (kalt
- `((local.get $npos)
- (i32.const ,(add-closure (+ nreq nopt)))
- (i32.gt_u)))
- (else
- ;; If there is no next clause, we should
- ;; fall through to issue an "expected a
- ;; keyword" error if there are too many
- ;; positional args instead of a more generic
- ;; "arity mismatch" error.
- '())))))
- (rest
- (if (zero? nreq)
- '()
- (branch-on-arity-mismatch
- (check-nargs nreq 'i32.lt_u))))
- ((zero? nopt)
- (branch-on-arity-mismatch
- (check-nargs nreq 'i32.ne)))
- (else
- (branch-on-arity-mismatch
- `(,@(check-nargs nreq 'i32.lt_u)
- ,@(check-nargs (+ nreq nopt) 'i32.gt_u)
- (i32.or))))))
- (define clamp-npos
- (if (and (or allow-other-keys? (pair? kw))
- (not (zero? nopt)))
- `((local.get $npos)
- (i32.const ,(add-closure (+ nreq nopt)))
- (i32.gt_u)
- (if #f ,void-block-type
- ((i32.const ,(add-closure (+ nreq nopt)))
- (local.set $npos))
- ()))
- '()))
- (define init-req
- (append-map
- (lambda (arg idx)
- (if (var-used? arg)
- `(,@(arg-ref idx)
- ,(local.set arg))
- '()))
- req-vars (iota nreq (add-closure 0))))
- (define init-opt
- (append-map
- (lambda (arg idx)
- (if (var-used? arg)
- `(,(if (or allow-other-keys? (pair? kw))
- '(local.get $npos)
- '(local.get $nargs))
- (i32.const ,idx)
- (i32.gt_u)
- (if #f ,scm-block-type
- (,@(arg-ref idx))
- ((i32.const 57) ; undefined
- (ref.i31)))
- ,(local.set arg))
- '()))
- opt-vars (iota nopt (add-closure nreq))))
- (define init-rest
- (if (and rest-var (var-used? rest-var))
- `((local.get $nargs)
- (local.get $arg0)
- (local.get $arg1)
- (local.get $arg2)
- ,(if (or allow-other-keys? (pair? kw))
- `(local.get $npos)
- `(i32.const ,(add-closure (+ nreq nopt))))
- (call $collect-rest-args)
- ,(local.set rest-var))
- '()))
- (define init-kw
- (append-map
- (match-lambda
- ((kw name sym)
- (if (var-used? sym)
- `((i32.const 57) ; undefined
- (ref.i31)
- ,(local.set sym))
- '())))
- kw))
- (define parse-kw
- (match kw
- (() '())
- (((kw name sym) ...)
- `(;; Loop through arguments.
- (loop $next
- ,void-block-type
- ((local.get $npos)
- (local.get $nargs)
- (i32.lt_u)
- (if #f ,void-block-type
- ;; If there are arguments remaining, fetch
- ;; the next arg.
- ((local.get $npos)
- (local.get $arg0)
- (local.get $arg1)
- (local.get $arg2)
- (call $arg-ref)
- (local.tee $s0) ; the keyword
- ;; Advance npos.
- (local.get $npos)
- (i32.const 1)
- (i32.add)
- (local.set $npos)
- (ref.test ,(make-ref-type #f '$keyword))
- (i32.eqz)
- ;; If $s0 is not a keyword, raise an error,
- ;; unless we are collecting a rest
- ;; argument, in which case you do you,
- ;; sloppy joe.
- (if #f ,void-block-type
- ,(if rest
- '((br $next))
- '((local.get $s0)
- (call $raise-invalid-keyword-error)))
- ())
- ;; Determine which keyword it corresponds
- ;; to.
- (local.get $s0)
- ,@(compile-constant kw)
- (call $keyword->idx)
- (local.set $i0) ; the keyword index, or -1
- ;; Raise an error if the keyword is
- ;; unrecognized, unless allow-other-keys?.
- ,@(cond
- (allow-other-keys?
- ;; Consume the next argument (if any;
- ;; may bump past the end) and loop.
- '((local.get $npos)
- (i32.const 1)
- (i32.add)
- (local.set $npos)
- (br $next)))
- (else
- `((local.get $i0)
- (i32.const -1)
- (i32.eq)
- (if #f ,void-block-type
- ((local.get $s0)
- (call $raise-unrecognized-keyword-error))
- ()))))
- ;; At this point the kw is recognized.
- ;; Require an argument.
- (local.get $npos) ; Flow to arg-ref.
- (local.get $npos) ; Flow to test.
- (local.get $npos)
- (i32.const 1)
- (i32.add)
- (local.set $npos)
- (local.get $nargs)
- (i32.eq)
- (if #f ,void-block-type
- ((local.get $s0)
- (call $raise-missing-keyword-argument-error))
- ())
- ;; Get the argument.
- (local.get $arg0)
- (local.get $arg1)
- (local.get $arg2)
- (call $arg-ref)
- ;; The value flows into this dispatch
- ;; block, which figures out which local
- ;; variable to assign the value to.
- ,@(let* ((params (list (make-param #f scm-type)))
- (results (list scm-type))
- (sig (make-func-sig params results))
- (block-type (make-type-use #f sig)))
- (define nkw (length kw))
- (let lp ((sym sym))
- (match sym
- (()
- `((local.get $i0)
- (br_table ,(reverse (iota (1- nkw) 1))
- 0)))
- ((s . sym)
- `((block
- #f ,block-type
- ,(lp sym))
- ,(local.set s)
- ;; Break directly to the loop.
- (br $next)))))))
- ;; Otherwise, fall through: we're done.
- ())))))))
- `(,@compute-npos
- ,@check-arity
- ,@clamp-npos
- ,@init-req
- ,@init-opt
- ,@init-rest
- ,@init-kw
- ,@parse-kw
- ,@(do-branch label kbody ctx)))))))
- (define (emit-cont label ctx)
- (match (intmap-ref cps label)
- (($ $kargs names vars term)
- (emit-term label term ctx))
- (($ $kreceive)
- (error "kreceive should be tailified away"))
- (($ $kfun src meta self ktail kentry)
- (match (intmap-ref cps kentry)
- (($ $kclause)
- (emit-fun/dispatch label self kentry ctx))
- (($ $kargs names vars _)
- (let ((vars (if self (cons self vars) vars)))
- (emit-fun/direct kentry vars ctx)))))
- (($ $kclause arity kbody kalt)
- (emit-clause label arity kbody kalt ctx))
- (($ $ktail)
- '())))
- (define code
- (do-tree kfun (make-ctx #f '())))
- (define (type-for-repr repr)
- (match repr
- ('scm scm-type)
- ('f64 'f64)
- ((or 's64 'u64) 'i64)
- ('bv-contents (make-ref-type #f '$raw-bytevector))
- ('code (make-ref-type #f '$kvarargs))))
- (define (type-for-named-temporary name)
- (match name
- ((or '$raw-sp '$scm-sp '$ret-sp '$dyn-sp) 'i32)
- ('$npos 'i32)
- ((or '$i0 '$i1 '$i2) 'i32)
- ((or '$f0 '$f1 '$f2) 'f64)
- ('$str_iter (make-ref-type #f 'stringview_iter))
- ('$s0 scm-type)
- ('$vu0 (make-ref-type #f '$raw-bytevector))
- ('$v0 (make-ref-type #f '$raw-scmvector))
- (_ #f)))
- (define (add-locals-from-code code)
- (define locals (make-hash-table))
- (define (visit-inst inst)
- (match inst
- (((or 'block 'loop) label type body)
- (visit-expr body))
- (('if label type consequent alternate)
- (visit-expr consequent)
- (visit-expr alternate))
- (('try label type body catches catch-all)
- (visit-expr body)
- (for-each visit-expr catches)
- (when catch-all (visit-expr catch-all)))
- (('try_delegate label type body handler)
- (visit-expr body))
- (((or 'local.set 'local.tee) label)
- (let ((type (type-for-named-temporary label)))
- (when type
- (hashq-set! locals label type))))
- (_ #f)))
- (define (visit-expr expr)
- (match expr
- (() (values))
- ((inst . expr)
- (visit-inst inst)
- (visit-expr expr))))
- (visit-expr code)
- (sort (hash-map->list make-local locals)
- (lambda (a b)
- (string<? (match a (($ <local> id) (symbol->string id)))
- (match b (($ <local> id) (symbol->string id)))))))
- (define reprs
- (compute-var-representations cps #:primcall-raw-representations
- hoot-primcall-raw-representations))
- (define locals
- (append
- (add-locals-from-code code)
- (intmap-fold-right (lambda (var repr out)
- (cons (make-local (var-label var)
- (type-for-repr repr))
- out))
- reprs '())))
- (define (init-joins code)
- (intset-fold (lambda (var code)
- (if (var-used? var)
- (match (intmap-ref reprs var)
- ((or 'f64 's64 'u64) code)
- ((or 'code 'bv-contents)
- (error "unexpected join repr" var))
- ('scm
- `((i32.const 57)
- (ref.i31)
- ,(local.set var)
- . ,code)))
- code))
- join-vars
- code))
- ;; FIXME: Here attach a name, other debug info to the function
- (make-func (func-label kfun)
- (match (known-arity kfun)
- (#f (make-type-use '$kvarargs kvarargs-sig))
- (vars (make-type-use
- #f
- (make-func-sig
- (map (lambda (var)
- (define repr (intmap-ref reprs var))
- (define type (type-for-repr repr))
- (make-param #f type))
- vars)
- '()))))
- locals
- (init-joins code))))
- (define (compute-globals)
- (fold1 (lambda (entry globals)
- (match entry
- (#(name type init-expr reloc-expr)
- (cons (make-global name
- (make-global-type #f type)
- init-expr)
- globals))))
- heap-constants
- (list (make-global '$load
- (make-global-type #t scm-type)
- `((i32.const 0) (ref.i31))))))
- (define (compute-exports)
- (list (make-export "$load" 'global '$load)))
- (define (compute-start-function funcs)
- (define load-function-id
- (match funcs
- ;; Assume that the first function is the "load" function.
- ((($ <func> id) . _) id)))
- (define relocs
- (fold1 (lambda (entry relocs)
- (match entry
- (#(name type init-expr #f) relocs)
- (#(name type init-expr make-reloc)
- (append (make-reloc name) relocs))))
- heap-constants
- '()))
- (define maybe-grow-argv
- (if (> max-args 8)
- `((i32.const ,(- max-args 8))
- (call $maybe-grow-argv))
- '()))
- (make-func '$start
- void-block-type
- '()
- `(,@maybe-grow-argv
- (i32.const 0)
- (ref.func ,load-function-id)
- (struct.new $proc)
- (global.set $load)
- . ,relocs)))
- (let* ((funcs
- (intmap-map->list lower-func (compute-reachable-functions cps 0)))
- (start-func (compute-start-function funcs))
- (types (append (intmap-map->list make-closure-type closure-types)
- (make-struct-types)))
- (tables '())
- (memories '())
- (globals (compute-globals))
- (exports (compute-exports))
- (start '$start)
- (elems '())
- (tags '())
- (datas (reverse datas))
- (strings '())
- (custom '()))
- (make-wasm '$scheme types imports
- (cons start-func funcs)
- tables memories globals exports
- start elems datas tags strings custom)))
- (define* (high-level-cps->wasm cps #:key
- (import-abi? #f)
- (export-abi? #t)
- (env #f)
- (optimization-level #f)
- (warning-level #f)
- (dump-cps? #f)
- (dump-wasm? #f)
- (emit-names? #f)
- (opts '()))
- (define (lower-and-tailify cps)
- (define lower-cps
- (let ((make-lower (language-lowerer (lookup-language 'cps))))
- (make-lower optimization-level opts)))
- (lower-cps cps env))
- (let ((cps (lower-and-tailify cps)))
- (when dump-cps?
- (dump cps))
- (let* ((wasm (lower-to-wasm cps #:import-abi? import-abi?))
- (wasm (if export-abi? (export-abi wasm) wasm))
- (wasm (add-stdlib wasm (compute-stdlib import-abi?)))
- (wasm (lower-wasm wasm #:emit-names? emit-names?)))
- (when dump-wasm?
- (format #t "\n\nThe wasm we are going to emit:\n")
- (dump-wasm wasm))
- wasm)))
- (define-syntax-rule (with-hoot-target . body)
- (with-target "wasm32-unknown-hoot"
- (lambda ()
- (parameterize ((target-runtime 'hoot))
- . body))))
|