From bcec8858a8968a955ac2febe36e5a4657e6054f6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ludovic=20Court=C3=A8s?= Date: Tue, 6 Dec 2011 21:36:49 +0100 Subject: [PATCH] peval: Truncate multiple values when extending the environment. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Reported by Cédric Cellier . * module/language/tree-il/peval.scm (truncate-values): New procedure. (make-operand): Call `truncate-values' SOURCE. * test-suite/tests/tree-il.test ("partial evaluation"): New tests for multiple value truncation. --- module/language/tree-il/peval.scm | 45 ++++++++++++++++++++++++++++++- test-suite/tests/tree-il.test | 17 ++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/module/language/tree-il/peval.scm b/module/language/tree-il/peval.scm index 634c6c91c..0fd37fe6c 100644 --- a/module/language/tree-il/peval.scm +++ b/module/language/tree-il/peval.scm @@ -99,6 +99,47 @@ (or (proc (vlist-ref vlist i)) (lp (1+ i))))))) +(define (truncate-values x) + "Discard all but the first value of X." + (let loop ((x x)) + (match x + (($ ) x) + (($ ) x) + (($ ) x) + (($ ) x) + (($ ) x) + (($ ) x) + (($ ) x) + (($ src condition subsequent alternate) + (make-conditional src condition (loop subsequent) (loop alternate))) + (($ _ ($ _ 'values) (first _ ...)) + first) + (($ _ ($ _ 'values) (val)) + val) + (($ src + (and prim ($ _ (? singly-valued-primitive?))) + args) + (make-application src prim (map loop args))) + (($ src proc args) + (make-application src proc (map loop args))) + (($ src (exps ... last)) + (make-sequence src (append exps (list (loop last))))) + (($ ) x) + (($ src fluids vals body) + (make-dynlet src fluids vals (loop body))) + (($ src names gensyms vals body) + (make-let src names gensyms vals (loop body))) + (($ src in-order? names gensyms vals body) + (make-letrec src in-order? names gensyms vals (loop body))) + (($ src names gensyms vals body) + (make-fix src names gensyms vals body)) + (($ src exp body) + (make-let-values src exp (loop body))) + (else + (make-application (tree-il-src x) + (make-primitive-ref #f 'values) + (list x)))))) + ;; Peval will do a one-pass analysis on the source program to determine ;; the set of assigned lexicals, and to identify unreferenced and ;; singly-referenced lexicals. @@ -278,8 +319,10 @@ (constant-value operand-constant-value set-operand-constant-value!)) (define* (make-operand var sym #:optional source visit) + ;; Bind SYM to VAR, with value SOURCE. ;; Bound operands are considered copyable until we prove otherwise. - (%make-operand var sym visit source 0 #f (and source #t) #f #f)) + (let ((source (if source (truncate-values source) source))) + (%make-operand var sym visit source 0 #f (and source #t) #f #f))) (define (make-bound-operands vars syms sources visit) (map (lambda (x y z) (make-operand x y z visit)) vars syms sources)) diff --git a/test-suite/tests/tree-il.test b/test-suite/tests/tree-il.test index 5e02bd175..de82340a4 100644 --- a/test-suite/tests/tree-il.test +++ b/test-suite/tests/tree-il.test @@ -664,6 +664,23 @@ (+ a b)))) (const 3)) + (pass-if-peval resolve-primitives + ;; First order, multiple values. + (let ((x 1) (y 2)) + (values x y)) + (apply (primitive values) (const 1) (const 2))) + + (pass-if-peval resolve-primitives + ;; First order, multiple values truncated. + (let ((x (values 1 'a)) (y 2)) + (values x y)) + (apply (primitive values) (const 1) (const 2))) + + (pass-if-peval resolve-primitives + ;; First order, multiple values truncated. + (or (values 1 2) 3) + (const 1)) + (pass-if-peval ;; First order, coalesced, mutability preserved. (cons 0 (cons 1 (cons 2 (list 3 4 5)))) -- 2.20.1