|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (language tree-il letrectify) |
|
#:use-module ((srfi srfi-1) #:select (fold-right)) |
|
#:use-module (srfi srfi-11) |
|
#:use-module (ice-9 match) |
|
#:use-module (language tree-il) |
|
#:use-module (language tree-il effects) |
|
#:export (letrectify)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define for-each-fold (make-tree-il-folder)) |
|
(define (tree-il-for-each f x) |
|
(for-each-fold x (lambda (x) (f x) (values)) (lambda (x) (values)))) |
|
|
|
(define (compute-declarative-toplevels x) |
|
(define dynamic (make-hash-table)) |
|
(define defined (make-hash-table)) |
|
(define assigned (make-hash-table)) |
|
(tree-il-for-each |
|
(lambda (x) |
|
(match x |
|
(($ <toplevel-set> src mod name) |
|
(if mod |
|
(hash-set! assigned (cons mod name) #t) |
|
(hashq-set! dynamic name #t))) |
|
(($ <toplevel-define> src mod name expr) |
|
(if mod |
|
(hash-set! (if (hash-ref defined (cons mod name)) |
|
assigned |
|
defined) |
|
(cons mod name) expr) |
|
(hashq-set! dynamic name #t))) |
|
(_ (values)))) |
|
x) |
|
(let ((declarative (make-hash-table))) |
|
(define (declarative-module? mod) |
|
(let ((m (resolve-module mod #f #:ensure #f))) |
|
(and m (module-declarative? m)))) |
|
(hash-for-each (lambda (k expr) |
|
(match k |
|
((mod . name) |
|
(unless (or (hash-ref assigned k) |
|
(hashq-ref dynamic name) |
|
(not (declarative-module? mod))) |
|
(hash-set! declarative k expr))))) |
|
defined) |
|
declarative)) |
|
|
|
(define (compute-private-toplevels declarative) |
|
|
|
|
|
(define exports (make-hash-table)) |
|
|
|
|
|
|
|
(define exports-macro? (make-hash-table)) |
|
(hash-for-each |
|
(lambda (k _) |
|
(match k |
|
((mod . name) |
|
(unless (hash-get-handle exports-macro? mod) |
|
(hash-set! exports-macro? mod #f) |
|
(let ((i (module-public-interface (resolve-module mod)))) |
|
(when i |
|
(module-for-each |
|
(lambda (k v) |
|
(hashq-set! exports v k) |
|
(when (and (variable-bound? v) (macro? (variable-ref v))) |
|
(hash-set! exports-macro? mod #t))) |
|
i))))))) |
|
declarative) |
|
(let ((private (make-hash-table))) |
|
(hash-for-each |
|
(lambda (k _) |
|
(match k |
|
((mod . name) |
|
(unless (or (hash-ref exports-macro? mod) |
|
(hashq-ref exports |
|
(module-local-variable (resolve-module mod) name))) |
|
(hash-set! private k #t))))) |
|
declarative) |
|
private)) |
|
|
|
(define* (letrectify expr #:key (seal-private-bindings? #f)) |
|
(define declarative (compute-declarative-toplevels expr)) |
|
(define private |
|
(if seal-private-bindings? |
|
(compute-private-toplevels declarative) |
|
(make-hash-table))) |
|
(define declarative-box+value |
|
(let ((tab (make-hash-table))) |
|
(hash-for-each (lambda (key val) |
|
(let ((box (and (not (hash-ref private key)) |
|
(gensym))) |
|
(val (gensym))) |
|
(hash-set! tab key (cons box val)))) |
|
declarative) |
|
(lambda (mod name) |
|
(hash-ref tab (cons mod name))))) |
|
|
|
(define compute-effects |
|
|
|
|
|
(let ((assigned? (lambda (sym) #t))) |
|
(make-effects-analyzer assigned?))) |
|
|
|
(define (can-elide-statement? stmt) |
|
(let ((effects (compute-effects stmt))) |
|
(effect-free? |
|
(exclude-effects effects (logior &allocation &zero-values))))) |
|
|
|
(define (add-binding name var val tail) |
|
(match tail |
|
(($ <letrec> src #t names vars vals tail) |
|
(make-letrec src #t |
|
(cons name names) (cons var vars) (cons val vals) |
|
tail)) |
|
(_ |
|
(make-letrec (tree-il-srcv tail) #t |
|
(list name) (list var) (list val) |
|
tail)))) |
|
|
|
(define (add-statement src stmt tail) |
|
(if (can-elide-statement? stmt) |
|
tail |
|
(add-binding '_ (gensym "_") (make-seq src stmt (make-void src)) |
|
tail))) |
|
|
|
(define (visit-expr expr) |
|
(post-order |
|
(lambda (expr) |
|
(match expr |
|
(($ <toplevel-ref> src mod name) |
|
(match (declarative-box+value mod name) |
|
(#f expr) |
|
((box . value) |
|
(make-lexical-ref src name value)))) |
|
(_ expr))) |
|
expr)) |
|
|
|
(define (visit-top-level expr mod-vars) |
|
(match expr |
|
(($ <toplevel-define> src mod name exp) |
|
(match (declarative-box+value mod name) |
|
(#f (values (visit-expr expr) mod-vars)) |
|
((#f . value) |
|
(values (add-binding name value (visit-expr exp) (make-void src)) |
|
mod-vars)) |
|
((box . value) |
|
(match (assoc-ref mod-vars mod) |
|
(#f |
|
(let* ((mod-var (gensym "mod")) |
|
(mod-vars (acons mod mod-var mod-vars))) |
|
(call-with-values (lambda () (visit-top-level expr mod-vars)) |
|
(lambda (tail mod-vars) |
|
(values |
|
(add-binding 'mod |
|
mod-var |
|
(make-primcall src 'current-module '()) |
|
tail) |
|
mod-vars))))) |
|
(mod-var |
|
(let* ((loc |
|
(make-primcall src 'module-ensure-local-variable! |
|
(list (make-lexical-ref src 'mod mod-var) |
|
(make-const src name)))) |
|
(exp (visit-expr exp)) |
|
(ref (make-lexical-ref src name value)) |
|
(init |
|
(make-primcall src '%variable-set! |
|
(list (make-lexical-ref src name box) |
|
ref)))) |
|
(values |
|
(add-binding |
|
name box loc |
|
(add-binding |
|
name value exp |
|
(add-statement src init (make-void src)))) |
|
mod-vars))))))) |
|
|
|
(($ <let> src names vars vals body) |
|
(let lp ((names names) (vars vars) (vals vals) (mod-vars mod-vars)) |
|
(match (vector names vars vals) |
|
(#(() () ()) |
|
(values (visit-expr body) mod-vars)) |
|
(#((name . names) (var . vars) (val . vals)) |
|
(let* ((val (visit-expr val)) |
|
(mod-vars |
|
(match val |
|
(($ <call> _ |
|
($ <module-ref> _ '(guile) 'define-module* #f) |
|
(($ <const> _ mod) . args)) |
|
(acons mod var mod-vars)) |
|
(_ mod-vars)))) |
|
(let-values (((exp mod-vars) (lp names vars vals mod-vars))) |
|
(values (add-binding name var val exp) |
|
mod-vars))))))) |
|
|
|
(($ <seq> src head tail) |
|
(let*-values (((head mod-vars) (visit-top-level head mod-vars)) |
|
((tail mod-vars) (visit-top-level tail mod-vars))) |
|
|
|
(values (match head |
|
(($ <letrec> src2 #t names vars vals head) |
|
(fold-right add-binding (add-statement src head tail) |
|
names vars vals)) |
|
(else |
|
(add-statement src head tail))) |
|
mod-vars))) |
|
|
|
|
|
|
|
(_ (values (visit-expr expr) mod-vars)))) |
|
|
|
(values (visit-top-level expr '()))) |
|
|