mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-12 06:41:13 +02:00
Expand pair-related primcalls
* module/language/tree-il/compile-cps.scm (ensure-pair): New helper. (cons, car, cdr, set-car!, set-cdr!): New expanders. * module/language/cps/closure-conversion.scm (convert-one): * module/language/cps/contification.scm (apply-contification): Emit lower-level instructions for making pairs.
This commit is contained in:
parent
9a1d2d8ec8
commit
49fa4980bb
3 changed files with 93 additions and 6 deletions
|
@ -528,9 +528,9 @@ term."
|
||||||
;; Well-known closure with two free variables; the closure is a
|
;; Well-known closure with two free variables; the closure is a
|
||||||
;; pair.
|
;; pair.
|
||||||
(with-cps cps
|
(with-cps cps
|
||||||
($ (with-cps-constants ((false #f))
|
(build-term
|
||||||
(build-term
|
($continue k src
|
||||||
($continue k src ($primcall 'cons #f (false false))))))))
|
($primcall 'allocate-words/immediate `(pair . 2) ())))))
|
||||||
;; Well-known callee with more than two free variables; the closure
|
;; Well-known callee with more than two free variables; the closure
|
||||||
;; is a vector.
|
;; is a vector.
|
||||||
(#(#t nfree)
|
(#(#t nfree)
|
||||||
|
|
|
@ -382,9 +382,20 @@ function set."
|
||||||
(build-term ($continue k src ($const '())))))
|
(build-term ($continue k src ($const '())))))
|
||||||
((v . vals)
|
((v . vals)
|
||||||
(with-cps cps
|
(with-cps cps
|
||||||
(letv tail)
|
(letv pair tail)
|
||||||
(letk ktail ($kargs ('tail) (tail)
|
(letk kdone ($kargs () () ($continue k src ($values (pair)))))
|
||||||
($continue k src ($primcall 'cons #f (v tail)))))
|
(letk ktail
|
||||||
|
($kargs () ()
|
||||||
|
($continue kdone src
|
||||||
|
($primcall 'scm-set!/immediate '(pair . 1) (pair tail)))))
|
||||||
|
(letk khead
|
||||||
|
($kargs ('pair) (pair)
|
||||||
|
($continue ktail src
|
||||||
|
($primcall 'scm-set!/immediate '(pair . 0) (pair v)))))
|
||||||
|
(letk ktail
|
||||||
|
($kargs ('tail) (tail)
|
||||||
|
($continue khead src
|
||||||
|
($primcall 'allocate-words/immediate '(pair . 2) ()))))
|
||||||
($ (build-list ktail src vals))))))
|
($ (build-list ktail src vals))))))
|
||||||
(cond
|
(cond
|
||||||
((and (not rest) (eqv? (length vals) nreq))
|
((and (not rest) (eqv? (length vals) nreq))
|
||||||
|
|
|
@ -425,6 +425,82 @@
|
||||||
($continue ktag0 src
|
($continue ktag0 src
|
||||||
($primcall 'allocate-words/immediate `(vector . ,nwords) ()))))))
|
($primcall 'allocate-words/immediate `(vector . ,nwords) ()))))))
|
||||||
|
|
||||||
|
(define (ensure-pair cps src op pred x is-pair)
|
||||||
|
(define msg
|
||||||
|
(match pred
|
||||||
|
('pair?
|
||||||
|
"Wrong type argument in position 1 (expecting pair): ~S")
|
||||||
|
('mutable-pair?
|
||||||
|
"Wrong type argument in position 1 (expecting mutable pair): ~S")))
|
||||||
|
(define not-pair (vector 'wrong-type-arg (symbol->string op) msg))
|
||||||
|
(with-cps cps
|
||||||
|
(letk knot-pair ($kargs () () ($throw src 'throw/value+data not-pair (x))))
|
||||||
|
(let$ body (is-pair))
|
||||||
|
(letk k ($kargs () () ,body))
|
||||||
|
(letk kheap-object ($kargs () () ($branch knot-pair k src pred #f (x))))
|
||||||
|
(build-term ($branch knot-pair kheap-object src 'heap-object? #f (x)))))
|
||||||
|
|
||||||
|
(define-primcall-converter cons
|
||||||
|
(lambda (cps k src op param head tail)
|
||||||
|
(with-cps cps
|
||||||
|
(letv pair)
|
||||||
|
(letk kdone
|
||||||
|
($kargs () ()
|
||||||
|
($continue k src ($values (pair)))))
|
||||||
|
(letk ktail
|
||||||
|
($kargs () ()
|
||||||
|
($continue kdone src
|
||||||
|
($primcall 'scm-set!/immediate '(pair . 1) (pair tail)))))
|
||||||
|
(letk khead
|
||||||
|
($kargs ('pair) (pair)
|
||||||
|
($continue ktail src
|
||||||
|
($primcall 'scm-set!/immediate '(pair . 0) (pair head)))))
|
||||||
|
(build-term
|
||||||
|
($continue khead src
|
||||||
|
($primcall 'allocate-words/immediate '(pair . 2) ()))))))
|
||||||
|
|
||||||
|
(define-primcall-converter car
|
||||||
|
(lambda (cps k src op param pair)
|
||||||
|
(ensure-pair
|
||||||
|
cps src 'car 'pair? pair
|
||||||
|
(lambda (cps)
|
||||||
|
(with-cps cps
|
||||||
|
(build-term
|
||||||
|
($continue k src
|
||||||
|
($primcall 'scm-ref/immediate '(pair . 0) (pair)))))))))
|
||||||
|
|
||||||
|
(define-primcall-converter cdr
|
||||||
|
(lambda (cps k src op param pair)
|
||||||
|
(ensure-pair
|
||||||
|
cps src 'cdr 'pair? pair
|
||||||
|
(lambda (cps)
|
||||||
|
(with-cps cps
|
||||||
|
(build-term
|
||||||
|
($continue k src
|
||||||
|
($primcall 'scm-ref/immediate '(pair . 1) (pair)))))))))
|
||||||
|
|
||||||
|
(define-primcall-converter set-car!
|
||||||
|
(lambda (cps k src op param pair val)
|
||||||
|
(ensure-pair
|
||||||
|
;; FIXME: Use mutable-pair? as predicate.
|
||||||
|
cps src 'set-car! 'pair? pair
|
||||||
|
(lambda (cps)
|
||||||
|
(with-cps cps
|
||||||
|
(build-term
|
||||||
|
($continue k src
|
||||||
|
($primcall 'scm-set!/immediate '(pair . 0) (pair val)))))))))
|
||||||
|
|
||||||
|
(define-primcall-converter set-cdr!
|
||||||
|
(lambda (cps k src op param pair val)
|
||||||
|
(ensure-pair
|
||||||
|
;; FIXME: Use mutable-pair? as predicate.
|
||||||
|
cps src 'set-cdr! 'pair? pair
|
||||||
|
(lambda (cps)
|
||||||
|
(with-cps cps
|
||||||
|
(build-term
|
||||||
|
($continue k src
|
||||||
|
($primcall 'scm-set!/immediate '(pair . 1) (pair val)))))))))
|
||||||
|
|
||||||
(define-primcall-converters
|
(define-primcall-converters
|
||||||
(char->integer scm >u64)
|
(char->integer scm >u64)
|
||||||
(integer->char u64 >scm)
|
(integer->char u64 >scm)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue