1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-21 12:10:26 +02:00

Allow callk to continue to kargs

* module/language/cps/verify.scm (check-arities): If a callk continues
to kargs, the caller knows the number of return values that the callee
provides and no number-of-values check is needed.
* module/language/cps/contification.scm (apply-contification): Allow
contification of known-return-values calls.
* module/language/cps/reify-primitives.scm (uniquify-receive)
(reify-primitives): No need for uniquify-receive any more as receive
shuffles are attached to the call, not the continuation.
* module/language/cps/compile-bytecode.scm (compile-function): Add kargs
case.
This commit is contained in:
Andy Wingo 2021-11-15 10:39:04 +01:00
parent 4fcd643adb
commit 5c76381625
5 changed files with 23 additions and 30 deletions

View file

@ -135,6 +135,8 @@
(emit-fmov asm dst src) (emit-fmov asm dst src)
(lp moves reset-frame?))))))) (lp moves reset-frame?)))))))
(match cont (match cont
(($ $kargs)
(shuffle-results))
(($ $kreceive ($ $arity req () rest () #f) kargs) (($ $kreceive ($ $arity req () rest () #f) kargs)
(let ((nreq (length req)) (let ((nreq (length req))
(rest-var (and rest (rest-var (and rest

View file

@ -469,8 +469,9 @@ function set."
(if (eq? k k*) (if (eq? k k*)
(with-cps cps (build-term ($continue k src ,exp))) (with-cps cps (build-term ($continue k src ,exp)))
;; We are contifying this return. It must be a call or a ;; We are contifying this return. It must be a call or a
;; $values expression. k* will be either a $ktail or a ;; $values expression. k* will be a $ktail or a $kreceive
;; $kreceive continuation. ;; continuation, or a $kargs continuation for a
;; known-number-of-values return.
(match (intmap-ref conts k*) (match (intmap-ref conts k*)
(($ $kreceive ($ $arity req () rest () #f) kargs) (($ $kreceive ($ $arity req () rest () #f) kargs)
(match exp (match exp
@ -480,6 +481,10 @@ function set."
;; have to rewrite as a call to the 'values primitive. ;; have to rewrite as a call to the 'values primitive.
(($ $values vals) (($ $values vals)
(inline-return cps k* kargs src (length req) rest vals)))) (inline-return cps k* kargs src (length req) rest vals))))
(($ $kargs)
(match exp
((or ($ $callk) ($ $values))
(with-cps cps (build-term ($continue k* src ,exp))))))
(($ $ktail) (($ $ktail)
(with-cps cps (build-term ($continue k* src ,exp)))))))) (with-cps cps (build-term ($continue k* src ,exp))))))))
(define (contify-unchecked-function cps kfun) (define (contify-unchecked-function cps kfun)

View file

@ -102,16 +102,6 @@
(letk kclause ($kclause ('() '() #f '() #f) kbody #f)) (letk kclause ($kclause ('() '() #f '() #f) kbody #f))
kclause)) kclause))
;; A $kreceive continuation should have only one predecessor.
(define (uniquify-receive cps k)
(match (intmap-ref cps k)
(($ $kreceive ($ $arity req () rest () #f) kargs)
(with-cps cps
(letk k ($kreceive req rest kargs))
k))
(_
(with-cps cps k))))
(define (wrap-unary cps k src wrap unwrap op param a) (define (wrap-unary cps k src wrap unwrap op param a)
(with-cps cps (with-cps cps
(letv a* res*) (letv a* res*)
@ -619,16 +609,6 @@
((imm-s64-< (s12? a) b) load-s64 (s64-< a b)) ((imm-s64-< (s12? a) b) load-s64 (s64-< a b))
((eq-constant? (imm16? b) a) load-const (eq? a b)) ((eq-constant? (imm16? b) a) load-const (eq? a b))
(_ cps)))) (_ cps))))
(($ $kargs names vars ($ $continue k src ($ $call proc args)))
(with-cps cps
(let$ k (uniquify-receive k))
(setk label ($kargs names vars
($continue k src ($call proc args))))))
(($ $kargs names vars ($ $continue k src ($ $callk k* proc args)))
(with-cps cps
(let$ k (uniquify-receive k))
(setk label ($kargs names vars
($continue k src ($callk k* proc args))))))
(_ cps))) (_ cps)))
(with-fresh-name-state cps (with-fresh-name-state cps

View file

@ -389,6 +389,8 @@ by a label, respectively."
(($ $values (arg)) (($ $values (arg))
(intmap-add representations var (intmap-add representations var
(intmap-ref representations arg))) (intmap-ref representations arg)))
(($ $callk)
(intmap-add representations var 'scm))
(($ $primcall (or 'scm->f64 'load-f64 's64->f64 (($ $primcall (or 'scm->f64 'load-f64 's64->f64
'f32-ref 'f64-ref 'f32-ref 'f64-ref
'fadd 'fsub 'fmul 'fdiv 'fsqrt 'fabs 'fadd 'fsub 'fmul 'fdiv 'fsqrt 'fabs
@ -425,7 +427,11 @@ by a label, respectively."
(fold (lambda (arg var representations) (fold (lambda (arg var representations)
(intmap-add representations var (intmap-add representations var
(intmap-ref representations arg))) (intmap-ref representations arg)))
representations args vars)))))) representations args vars))
(($ $callk)
(fold1 (lambda (var representations)
(intmap-add representations var 'scm))
vars representations))))))
(($ $kargs _ _ (or ($ $branch) ($ $switch) ($ $prompt) ($ $throw))) (($ $kargs _ _ (or ($ $branch) ($ $switch) ($ $prompt) ($ $throw)))
representations) representations)
(($ $kfun src meta self tail entry) (($ $kfun src meta self tail entry)

View file

@ -1,5 +1,5 @@
;;; Diagnostic checker for CPS ;;; Diagnostic checker for CPS
;;; Copyright (C) 2014-2020 Free Software Foundation, Inc. ;;; Copyright (C) 2014-2021 Free Software Foundation, Inc.
;;; ;;;
;;; This library is free software: you can redistribute it and/or modify ;;; This library is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU Lesser General Public License as ;;; it under the terms of the GNU Lesser General Public License as
@ -271,10 +271,6 @@ definitions that are available at LABEL."
(unless (= (length vars) n) (unless (= (length vars) n)
(error "expected n-ary continuation" n cont))) (error "expected n-ary continuation" n cont)))
(_ (error "expected $kargs continuation" cont)))) (_ (error "expected $kargs continuation" cont))))
(define (assert-kreceive-or-ktail)
(match cont
((or ($ $kreceive) ($ $ktail)) #t)
(_ (error "expected $kreceive or $ktail continuation" cont))))
(match exp (match exp
((or ($ $const) ($ $prim) ($ $const-fun) ($ $code) ($ $fun)) ((or ($ $const) ($ $prim) ($ $const-fun) ($ $code) ($ $fun))
(assert-unary)) (assert-unary))
@ -291,9 +287,13 @@ definitions that are available at LABEL."
(($ $ktail) #t) (($ $ktail) #t)
(_ (assert-n-ary (length args))))) (_ (assert-n-ary (length args)))))
(($ $call proc args) (($ $call proc args)
(assert-kreceive-or-ktail)) (match cont
((or ($ $kreceive) ($ $ktail)) #t)
(_ (error "expected $kreceive or $ktail continuation" cont))))
(($ $callk k proc args) (($ $callk k proc args)
(assert-kreceive-or-ktail)) (match cont
((or ($ $kargs) ($ $kreceive) ($ $ktail)) #t)
(_ (error "expected $kargs, $kreceive or $ktail continuation" cont))))
(($ $primcall name param args) (($ $primcall name param args)
(match cont (match cont
(($ $kargs) #t) (($ $kargs) #t)