|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (language tree-il peval) |
|
#:use-module (language tree-il) |
|
#:use-module (language tree-il primitives) |
|
#:use-module (language tree-il effects) |
|
#:use-module (ice-9 vlist) |
|
#:use-module (ice-9 match) |
|
#:use-module (srfi srfi-1) |
|
#:use-module (srfi srfi-9) |
|
#:use-module (srfi srfi-11) |
|
#:use-module (srfi srfi-26) |
|
#:use-module (system base target) |
|
#:use-module (ice-9 control) |
|
#:export (peval)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax *logging* (identifier-syntax #f)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax log |
|
(syntax-rules (quote) |
|
((log 'event arg ...) |
|
(if (and *logging* |
|
(or (eq? *logging* #t) |
|
(memq 'event *logging*))) |
|
(log* 'event arg ...))))) |
|
|
|
(define (log* event . args) |
|
(let ((pp (module-ref (resolve-interface '(ice-9 pretty-print)) |
|
'pretty-print))) |
|
(pp `(log ,event . ,args)) |
|
(newline) |
|
(values))) |
|
|
|
(define (tree-il-any proc exp) |
|
(let/ec k |
|
(tree-il-fold (lambda (exp res) |
|
(let ((res (proc exp))) |
|
(if res (k res) #f))) |
|
(lambda (exp res) #f) |
|
#f exp))) |
|
|
|
(define (vlist-any proc vlist) |
|
(let ((len (vlist-length vlist))) |
|
(let lp ((i 0)) |
|
(and (< i len) |
|
(or (proc (vlist-ref vlist i)) |
|
(lp (1+ i))))))) |
|
|
|
(define (singly-valued-expression? exp) |
|
(match exp |
|
(($ <const>) #t) |
|
(($ <void>) #t) |
|
(($ <lexical-ref>) #t) |
|
(($ <primitive-ref>) #t) |
|
(($ <module-ref>) #t) |
|
(($ <toplevel-ref>) #t) |
|
(($ <primcall> _ (? singly-valued-primitive?)) #t) |
|
(($ <primcall> _ 'values (val)) #t) |
|
(($ <lambda>) #t) |
|
(($ <conditional> _ test consequent alternate) |
|
(and (singly-valued-expression? consequent) |
|
(singly-valued-expression? alternate))) |
|
(else #f))) |
|
|
|
(define (truncate-values x) |
|
"Discard all but the first value of X." |
|
(if (singly-valued-expression? x) |
|
x |
|
(make-primcall (tree-il-srcv x) 'values (list x)))) |
|
|
|
|
|
|
|
|
|
|
|
(define-record-type <var> |
|
(make-var name gensym refcount set?) |
|
var? |
|
(name var-name) |
|
(gensym var-gensym) |
|
(refcount var-refcount set-var-refcount!) |
|
(set? var-set? set-var-set?!)) |
|
|
|
(define* (build-var-table exp #:optional (table vlist-null)) |
|
(tree-il-fold |
|
(lambda (exp res) |
|
(match exp |
|
(($ <lexical-ref> src name gensym) |
|
(let ((var (cdr (vhash-assq gensym res)))) |
|
(set-var-refcount! var (1+ (var-refcount var))) |
|
res)) |
|
(($ <lambda-case> src req opt rest kw init gensyms body alt) |
|
(fold (lambda (name sym res) |
|
(vhash-consq sym (make-var name sym 0 #f) res)) |
|
res |
|
(append req (or opt '()) (if rest (list rest) '()) |
|
(match kw |
|
((aok? (kw name sym) ...) name) |
|
(_ '()))) |
|
gensyms)) |
|
(($ <let> src names gensyms vals body) |
|
(fold (lambda (name sym res) |
|
(vhash-consq sym (make-var name sym 0 #f) res)) |
|
res names gensyms)) |
|
(($ <letrec>) |
|
(error "unexpected letrec")) |
|
(($ <fix> src names gensyms vals body) |
|
(fold (lambda (name sym res) |
|
(vhash-consq sym (make-var name sym 0 #f) res)) |
|
res names gensyms)) |
|
(($ <lexical-set> src name gensym exp) |
|
(set-var-set?! (cdr (vhash-assq gensym res)) #t) |
|
res) |
|
(_ res))) |
|
(lambda (exp res) res) |
|
table exp)) |
|
|
|
(define (augment-var-table-with-externally-introduced-lexicals exp table) |
|
"Take the previously computed var table TABLE and the term EXP and |
|
return a table augmented with the lexicals bound in EXP which are not |
|
present in TABLE. This is used for the result of `expand-primcalls`, |
|
which may introduce new lexicals if a subexpression needs to be |
|
referenced multiple times." |
|
(define (maybe-add-var name sym table) |
|
|
|
(define refcount 2) |
|
(define assigned? #f) |
|
(if (vhash-assq sym table) |
|
table |
|
(vhash-consq sym (make-var name sym refcount assigned?) table))) |
|
(tree-il-fold |
|
(lambda (exp table) |
|
(match exp |
|
(($ <lambda-case> src req opt rest kw init gensyms body alt) |
|
(fold maybe-add-var table |
|
(append req (or opt '()) (if rest (list rest) '()) |
|
(match kw |
|
((aok? (kw name sym) ...) name) |
|
(_ '()))) |
|
gensyms)) |
|
(($ <let> src names gensyms vals body) |
|
(fold maybe-add-var table names gensyms)) |
|
(($ <letrec>) |
|
(error "unexpected letrec")) |
|
(($ <fix> src names gensyms vals body) |
|
(fold maybe-add-var table names gensyms)) |
|
(_ table))) |
|
(lambda (exp table) table) |
|
table exp)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-record-type <counter> |
|
(%make-counter effort size continuation recursive? data prev) |
|
counter? |
|
(effort effort-counter) |
|
(size size-counter) |
|
(continuation counter-continuation) |
|
(recursive? counter-recursive? set-counter-recursive?!) |
|
(data counter-data) |
|
(prev counter-prev)) |
|
|
|
(define (abort-counter c) |
|
((counter-continuation c))) |
|
|
|
(define (record-effort! c) |
|
(let ((e (effort-counter c))) |
|
(if (zero? (variable-ref e)) |
|
(abort-counter c) |
|
(variable-set! e (1- (variable-ref e)))))) |
|
|
|
(define (record-size! c) |
|
(let ((s (size-counter c))) |
|
(if (zero? (variable-ref s)) |
|
(abort-counter c) |
|
(variable-set! s (1- (variable-ref s)))))) |
|
|
|
(define (find-counter data counter) |
|
(and counter |
|
(if (eq? data (counter-data counter)) |
|
counter |
|
(find-counter data (counter-prev counter))))) |
|
|
|
(define* (transfer! from to #:optional |
|
(effort (variable-ref (effort-counter from))) |
|
(size (variable-ref (size-counter from)))) |
|
(define (transfer-counter! from-v to-v amount) |
|
(let* ((from-balance (variable-ref from-v)) |
|
(to-balance (variable-ref to-v)) |
|
(amount (min amount from-balance))) |
|
(variable-set! from-v (- from-balance amount)) |
|
(variable-set! to-v (+ to-balance amount)))) |
|
|
|
(transfer-counter! (effort-counter from) (effort-counter to) effort) |
|
(transfer-counter! (size-counter from) (size-counter to) size)) |
|
|
|
(define (make-top-counter effort-limit size-limit continuation data) |
|
(%make-counter (make-variable effort-limit) |
|
(make-variable size-limit) |
|
continuation |
|
#t |
|
data |
|
#f)) |
|
|
|
(define (make-nested-counter continuation data current) |
|
(let ((c (%make-counter (make-variable 0) |
|
(make-variable 0) |
|
continuation |
|
#f |
|
data |
|
current))) |
|
(transfer! current c) |
|
c)) |
|
|
|
(define (make-recursive-counter effort-limit size-limit orig current) |
|
(let ((c (%make-counter (make-variable 0) |
|
(make-variable 0) |
|
(counter-continuation orig) |
|
#t |
|
(counter-data orig) |
|
current))) |
|
(transfer! current c effort-limit size-limit) |
|
c)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-record-type <operand> |
|
(%make-operand var sym visit source visit-count use-count |
|
copyable? residual-value constant-value alias) |
|
operand? |
|
(var operand-var) |
|
(sym operand-sym) |
|
(visit %operand-visit) |
|
(source operand-source) |
|
(visit-count operand-visit-count set-operand-visit-count!) |
|
(use-count operand-use-count set-operand-use-count!) |
|
(copyable? operand-copyable? set-operand-copyable?!) |
|
(residual-value operand-residual-value %set-operand-residual-value!) |
|
(constant-value operand-constant-value set-operand-constant-value!) |
|
(alias operand-alias set-operand-alias!)) |
|
|
|
(define* (make-operand var sym #:optional source visit alias) |
|
|
|
|
|
|
|
|
|
(let ((source (and=> source truncate-values))) |
|
(%make-operand var sym visit source 0 0 |
|
(and source (not (var-set? var))) #f #f |
|
(and (not (var-set? var)) alias)))) |
|
|
|
(define* (make-bound-operands vars syms sources visit #:optional aliases) |
|
(if aliases |
|
(map (lambda (name sym source alias) |
|
(make-operand name sym source visit alias)) |
|
vars syms sources aliases) |
|
(map (lambda (name sym source) |
|
(make-operand name sym source visit #f)) |
|
vars syms sources))) |
|
|
|
(define (make-unbound-operands vars syms) |
|
(map make-operand vars syms)) |
|
|
|
(define (set-operand-residual-value! op val) |
|
(%set-operand-residual-value! |
|
op |
|
(match val |
|
(($ <primcall> src 'values (first)) |
|
|
|
|
|
first) |
|
(else |
|
val)))) |
|
|
|
(define* (visit-operand op counter ctx #:optional effort-limit size-limit) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(and (zero? (operand-visit-count op)) |
|
(dynamic-wind |
|
(lambda () |
|
(set-operand-visit-count! op (1+ (operand-visit-count op)))) |
|
(lambda () |
|
(and (operand-source op) |
|
(if (or counter (and (not effort-limit) (not size-limit))) |
|
((%operand-visit op) (operand-source op) counter ctx) |
|
(let/ec k |
|
(define (abort) |
|
|
|
|
|
|
|
(set-operand-copyable?! op #f) |
|
(k #f)) |
|
((%operand-visit op) |
|
(operand-source op) |
|
(make-top-counter effort-limit size-limit abort op) |
|
ctx))))) |
|
(lambda () |
|
(set-operand-visit-count! op (1- (operand-visit-count op))))))) |
|
|
|
|
|
|
|
(define (types-check? primitive-name args) |
|
(case primitive-name |
|
((values) #t) |
|
((not pair? null? list? symbol? vector? struct?) |
|
(= (length args) 1)) |
|
((eq? eqv? equal?) |
|
(= (length args) 2)) |
|
|
|
(else #f))) |
|
|
|
(define* (peval exp #:optional (cenv (current-module)) (env vlist-null) |
|
#:key |
|
(operator-size-limit 40) |
|
(operand-size-limit 20) |
|
(value-size-limit 10) |
|
(effort-limit 500) |
|
(recursive-effort-limit 100) |
|
(cross-module-inlining? #f)) |
|
"Partially evaluate EXP in compilation environment CENV, with |
|
top-level bindings from ENV and return the resulting expression." |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define local-toplevel-env |
|
|
|
(let () |
|
(define (env-folder x env) |
|
(match x |
|
(($ <toplevel-define> _ _ name) |
|
(vhash-consq name #t env)) |
|
(($ <seq> _ head tail) |
|
(env-folder tail (env-folder head env))) |
|
(_ env))) |
|
(env-folder exp vlist-null))) |
|
|
|
(define (local-toplevel? name) |
|
(vhash-assq name local-toplevel-env)) |
|
|
|
|
|
|
|
|
|
(define store (build-var-table exp)) |
|
|
|
(define (record-new-temporary! name sym refcount) |
|
(set! store (vhash-consq sym (make-var name sym refcount #f) store))) |
|
|
|
(define (lookup-var sym) |
|
(let ((v (vhash-assq sym store))) |
|
(if v (cdr v) (error "unbound var" sym (vlist->list store))))) |
|
|
|
(define (fresh-gensyms vars) |
|
(map (lambda (var) |
|
(let ((new (gensym (string-append (symbol->string (var-name var)) |
|
" ")))) |
|
(set! store (vhash-consq new var store)) |
|
new)) |
|
vars)) |
|
|
|
(define (fresh-temporaries ls) |
|
(map (lambda (elt) |
|
(let ((new (gensym "tmp "))) |
|
(record-new-temporary! 'tmp new 1) |
|
new)) |
|
ls)) |
|
|
|
(define (assigned-lexical? sym) |
|
(var-set? (lookup-var sym))) |
|
|
|
(define (lexical-refcount sym) |
|
(var-refcount (lookup-var sym))) |
|
|
|
(define (splice-expression exp) |
|
(define vars (make-hash-table)) |
|
(define (rename! old*) |
|
(match old* |
|
(() '()) |
|
((old . old*) |
|
(cons (let ((new (gensym "t"))) |
|
(hashq-set! vars old new) |
|
new) |
|
(rename! old*))))) |
|
(define (new-name old) (hashq-ref vars old)) |
|
(define renamed |
|
(pre-order |
|
(match-lambda |
|
(($ <lexical-ref> src name gensym) |
|
(make-lexical-ref src name (new-name gensym))) |
|
(($ <lexical-set> src name gensym exp) |
|
(make-lexical-set src name (new-name gensym) exp)) |
|
(($ <lambda-case> src req opt rest kw init gensyms body alt) |
|
(let ((gensyms (rename! gensyms))) |
|
(make-lambda-case src req opt rest |
|
(match kw |
|
((aok? (kw name sym) ...) |
|
(cons aok? |
|
(map (lambda (kw name sym) |
|
(list kw name (new-name sym))) |
|
kw name sym))) |
|
(#f #f)) |
|
init gensyms body alt))) |
|
(($ <let> src names gensyms vals body) |
|
(make-let src names (rename! gensyms) vals body)) |
|
(($ <letrec>) |
|
(error "unexpected letrec")) |
|
(($ <fix> src names gensyms vals body) |
|
(make-fix src names (rename! gensyms) vals body)) |
|
(exp exp)) |
|
exp)) |
|
(set! store (build-var-table renamed store)) |
|
renamed) |
|
|
|
(define (with-temporaries src exps refcount can-copy? k) |
|
(let* ((pairs (map (match-lambda |
|
((and exp (? can-copy?)) |
|
(cons #f exp)) |
|
(exp |
|
(let ((sym (gensym "tmp "))) |
|
(record-new-temporary! 'tmp sym refcount) |
|
(cons sym exp)))) |
|
exps)) |
|
(tmps (filter car pairs))) |
|
(match tmps |
|
(() (k exps)) |
|
(tmps |
|
(make-let src |
|
(make-list (length tmps) 'tmp) |
|
(map car tmps) |
|
(map cdr tmps) |
|
(k (map (match-lambda |
|
((#f . val) val) |
|
((sym . _) |
|
(make-lexical-ref #f 'tmp sym))) |
|
pairs))))))) |
|
|
|
(define (make-begin0 src first second) |
|
(make-let-values |
|
src |
|
first |
|
(let ((vals (gensym "vals "))) |
|
(record-new-temporary! 'vals vals 1) |
|
(make-lambda-case |
|
#f |
|
'() #f 'vals #f '() (list vals) |
|
(make-seq |
|
src |
|
second |
|
(make-primcall #f 'apply |
|
(list |
|
(make-primitive-ref #f 'values) |
|
(make-lexical-ref #f 'vals vals)))) |
|
#f)))) |
|
|
|
|
|
|
|
|
|
(define (record-source-expression! orig new) |
|
(set! store (vhash-consq new (source-expression orig) store)) |
|
new) |
|
|
|
|
|
|
|
|
|
(define (source-expression new) |
|
(let ((x (vhash-assq new store))) |
|
(if x (cdr x) new))) |
|
|
|
(define (record-operand-use op) |
|
(set-operand-use-count! op (1+ (operand-use-count op)))) |
|
|
|
(define (unrecord-operand-uses op n) |
|
(let ((count (- (operand-use-count op) n))) |
|
(when (zero? count) |
|
(set-operand-residual-value! op #f)) |
|
(set-operand-use-count! op count))) |
|
|
|
(define* (residualize-lexical op #:optional ctx val) |
|
(log 'residualize op) |
|
(record-operand-use op) |
|
(if (memq ctx '(value values)) |
|
(set-operand-residual-value! op val)) |
|
(make-lexical-ref #f (var-name (operand-var op)) (operand-sym op))) |
|
|
|
(define (fold-constants src name args ctx) |
|
(define (apply-primitive name args) |
|
|
|
(catch #t |
|
(lambda () |
|
(define mod (resolve-interface (primitive-module name))) |
|
(call-with-values |
|
(lambda () |
|
(apply (module-ref mod name) args)) |
|
(lambda results |
|
(values #t results)))) |
|
(lambda _ |
|
(values #f '())))) |
|
(define (make-values src values) |
|
(match values |
|
((single) single) |
|
((_ ...) |
|
(make-primcall src 'values values)))) |
|
(define (residualize-call) |
|
(make-primcall src name args)) |
|
(cond |
|
((every const? args) |
|
(let-values (((success? values) |
|
(apply-primitive name (map const-exp args)))) |
|
(log 'fold success? values name args) |
|
(if success? |
|
(case ctx |
|
((effect) (make-void src)) |
|
((test) |
|
|
|
|
|
(if (pair? values) |
|
(make-const src (car values)) |
|
(make-values src '()))) |
|
(else |
|
(make-values src (map (cut make-const src <>) values)))) |
|
(residualize-call)))) |
|
((and (eq? ctx 'effect) (types-check? name args)) |
|
(make-void #f)) |
|
(else |
|
(residualize-call)))) |
|
|
|
(define (inline-values src exp nmin nmax consumer) |
|
(let loop ((exp exp)) |
|
(match exp |
|
|
|
((or ($ <const>) |
|
($ <void>) |
|
($ <lambda>) |
|
($ <lexical-ref>) |
|
($ <toplevel-ref>) |
|
($ <module-ref>) |
|
($ <primitive-ref>) |
|
($ <lexical-set>) |
|
($ <toplevel-set>) |
|
($ <toplevel-define>) |
|
($ <module-set>) |
|
($ <primcall> src (? singly-valued-primitive?))) |
|
(and (<= nmin 1) (or (not nmax) (>= nmax 1)) |
|
(make-call src (make-lambda #f '() consumer) (list exp)))) |
|
|
|
|
|
(($ <primcall> src 'values vals) |
|
(and (<= nmin (length vals)) (or (not nmax) (>= nmax (length vals))) |
|
(make-call src (make-lambda #f '() consumer) vals))) |
|
|
|
|
|
(($ <conditional>) #f) |
|
|
|
|
|
(($ <call>) #f) |
|
(($ <primcall>) #f) |
|
|
|
|
|
(($ <prompt>) #f) |
|
(($ <abort>) #f) |
|
|
|
|
|
(($ <let> src names gensyms vals body) |
|
(let ((body (loop body))) |
|
(and body |
|
(make-let src names gensyms vals body)))) |
|
(($ <fix> src names gensyms vals body) |
|
(let ((body (loop body))) |
|
(and body |
|
(make-fix src names gensyms vals body)))) |
|
(($ <let-values> src exp |
|
($ <lambda-case> src2 req opt rest kw inits gensyms body #f)) |
|
(let ((body (loop body))) |
|
(and body |
|
(make-let-values src exp |
|
(make-lambda-case src2 req opt rest kw |
|
inits gensyms body #f))))) |
|
(($ <seq> src head tail) |
|
(let ((tail (loop tail))) |
|
(and tail (make-seq src head tail))))))) |
|
|
|
(define compute-effects |
|
(make-effects-analyzer assigned-lexical?)) |
|
|
|
(define (constant-expression? x) |
|
|
|
|
|
|
|
|
|
(constant? (compute-effects x))) |
|
|
|
(define (prune-bindings ops in-order? body counter ctx build-result) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (referenced? op) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(or (eq? ctx 'operator) |
|
(not (zero? (operand-use-count op))))) |
|
|
|
|
|
|
|
(define (residualize values effects) |
|
|
|
(cond |
|
(in-order? |
|
(let ((values (filter operand-residual-value ops))) |
|
(if (null? values) |
|
body |
|
(build-result (map (compose var-name operand-var) values) |
|
(map operand-sym values) |
|
(map operand-residual-value values) |
|
body)))) |
|
(else |
|
(let ((body |
|
(if (null? effects) |
|
body |
|
(let ((effect-vals (map operand-residual-value effects))) |
|
(list->seq #f (reverse (cons body effect-vals))))))) |
|
(if (null? values) |
|
body |
|
(let ((values (reverse values))) |
|
(build-result (map (compose var-name operand-var) values) |
|
(map operand-sym values) |
|
(map operand-residual-value values) |
|
body))))))) |
|
|
|
|
|
|
|
|
|
(let prune ((old (map referenced? ops)) (values '()) (effects '())) |
|
(let lp ((ops* ops) (values values) (effects effects)) |
|
(cond |
|
((null? ops*) |
|
(let ((new (map referenced? ops))) |
|
(if (not (equal? new old)) |
|
(prune new values '()) |
|
(residualize values |
|
(map (lambda (op val) |
|
(set-operand-residual-value! op val) |
|
op) |
|
(map car effects) (map cdr effects)))))) |
|
(else |
|
(let ((op (car ops*))) |
|
(cond |
|
((memq op values) |
|
(lp (cdr ops*) values effects)) |
|
((operand-residual-value op) |
|
(lp (cdr ops*) (cons op values) effects)) |
|
((referenced? op) |
|
(set-operand-residual-value! op (visit-operand op counter 'value)) |
|
(lp (cdr ops*) (cons op values) effects)) |
|
(else |
|
(lp (cdr ops*) |
|
values |
|
(let ((effect (visit-operand op counter 'effect))) |
|
(if (void? effect) |
|
effects |
|
(acons op effect effects)))))))))))) |
|
|
|
(define (small-expression? x limit) |
|
(let/ec k |
|
(tree-il-fold |
|
(lambda (x res) |
|
(1+ res)) |
|
(lambda (x res) |
|
(if (< res limit) |
|
res |
|
(k #f))) |
|
0 x) |
|
#t)) |
|
|
|
(define (extend-env sym op env) |
|
(vhash-consq (operand-sym op) op (vhash-consq sym op env))) |
|
|
|
(let loop ((exp exp) |
|
(env vlist-null) |
|
(counter #f) |
|
(ctx 'values)) |
|
(define (lookup var) |
|
(cond |
|
((vhash-assq var env) => cdr) |
|
(else (error "unbound var" var)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (find-definition x n-aliases) |
|
(cond |
|
((lexical-ref? x) |
|
(cond |
|
((lookup (lexical-ref-gensym x)) |
|
=> (lambda (op) |
|
(if (var-set? (operand-var op)) |
|
(values #f #f) |
|
(let ((y (or (operand-residual-value op) |
|
(visit-operand op counter 'value 10 10) |
|
(operand-source op)))) |
|
(cond |
|
((and (lexical-ref? y) |
|
(= (lexical-refcount (lexical-ref-gensym x)) 1)) |
|
|
|
|
|
(find-definition y n-aliases)) |
|
((= (lexical-refcount (lexical-ref-gensym x)) n-aliases) |
|
|
|
|
|
|
|
(values (find-definition y 1) |
|
op)) |
|
(else |
|
|
|
(values #f #f))))))) |
|
(else |
|
|
|
(values #f #f)))) |
|
((= n-aliases 1) |
|
|
|
|
|
(values x #f)) |
|
(else (values #f #f)))) |
|
|
|
(define (visit exp ctx) |
|
(loop exp env counter ctx)) |
|
|
|
(define (for-value exp) (visit exp 'value)) |
|
(define (for-values exp) (visit exp 'values)) |
|
(define (for-test exp) (visit exp 'test)) |
|
(define (for-effect exp) (visit exp 'effect)) |
|
(define (for-call exp) (visit exp 'call)) |
|
(define (for-tail exp) (visit exp ctx)) |
|
|
|
(if counter |
|
(record-effort! counter)) |
|
|
|
(log 'visit ctx (and=> counter effort-counter) |
|
(unparse-tree-il exp)) |
|
|
|
(match exp |
|
(($ <const>) |
|
(case ctx |
|
((effect) (make-void #f)) |
|
(else exp))) |
|
(($ <void>) |
|
(case ctx |
|
((test) (make-const #f #t)) |
|
(else exp))) |
|
(($ <lexical-ref> _ _ gensym) |
|
(log 'begin-copy gensym) |
|
(let lp ((op (lookup gensym))) |
|
(cond |
|
((eq? ctx 'effect) |
|
(log 'lexical-for-effect gensym) |
|
(make-void #f)) |
|
((operand-alias op) |
|
|
|
|
|
|
|
=> lp) |
|
((eq? ctx 'call) |
|
|
|
(log 'residualize-lexical-call gensym op) |
|
(residualize-lexical op)) |
|
((var-set? (operand-var op)) |
|
|
|
(log 'assigned-var gensym op) |
|
(residualize-lexical op)) |
|
((not (operand-copyable? op)) |
|
|
|
(log 'not-copyable gensym op) |
|
(residualize-lexical op)) |
|
((and=> (operand-constant-value op) |
|
(lambda (x) (or (const? x) (void? x) (primitive-ref? x)))) |
|
|
|
(let ((val (operand-constant-value op))) |
|
(log 'memoized-constant gensym val) |
|
(for-tail val))) |
|
((visit-operand op counter (if (eq? ctx 'values) 'value ctx) |
|
recursive-effort-limit operand-size-limit) |
|
=> |
|
|
|
|
|
(lambda (val) |
|
(cond |
|
((not (constant-expression? val)) |
|
(log 'not-constant gensym op) |
|
|
|
|
|
|
|
|
|
(set-operand-copyable?! op #f) |
|
(residualize-lexical op ctx val)) |
|
((or (const? val) |
|
(void? val) |
|
(primitive-ref? val)) |
|
|
|
|
|
(log 'copy-simple gensym val) |
|
|
|
|
|
|
|
(if (or (eq? ctx 'value) (eq? ctx 'values)) |
|
(begin |
|
(log 'memoize-constant gensym val) |
|
(set-operand-constant-value! op val))) |
|
val) |
|
((= 1 (var-refcount (operand-var op))) |
|
|
|
(log 'copy-single gensym val) |
|
val) |
|
|
|
|
|
((eq? ctx 'operator) |
|
|
|
|
|
(if (and (lambda? val) |
|
(small-expression? val operator-size-limit)) |
|
(begin |
|
(log 'copy-operator gensym val) |
|
val) |
|
(begin |
|
(log 'too-big-for-operator gensym val) |
|
(residualize-lexical op ctx val)))) |
|
(else |
|
|
|
|
|
|
|
(if (and (small-expression? val value-size-limit) |
|
(not (tree-il-any lambda? val))) |
|
(begin |
|
(log 'copy-value gensym val) |
|
val) |
|
(begin |
|
(log 'too-big-or-has-lambda gensym val) |
|
(residualize-lexical op ctx val))))))) |
|
(else |
|
|
|
|
|
(log 'unbound-or-aborted gensym op) |
|
(residualize-lexical op))))) |
|
(($ <lexical-set> src name gensym exp) |
|
(let ((op (lookup gensym))) |
|
(if (zero? (var-refcount (operand-var op))) |
|
(let ((exp (for-effect exp))) |
|
(if (void? exp) |
|
exp |
|
(make-seq src exp (make-void #f)))) |
|
(begin |
|
(record-operand-use op) |
|
(make-lexical-set src name (operand-sym op) (for-value exp)))))) |
|
(($ <let> src |
|
(names ... rest) |
|
(gensyms ... rest-sym) |
|
(vals ... ($ <primcall> _ 'list rest-args)) |
|
($ <primcall> asrc 'apply |
|
(proc args ... |
|
($ <lexical-ref> _ |
|
(? (cut eq? <> rest)) |
|
(? (lambda (sym) |
|
(and (eq? sym rest-sym) |
|
(= (lexical-refcount sym) 1)))))))) |
|
(let* ((tmps (make-list (length rest-args) 'tmp)) |
|
(tmp-syms (fresh-temporaries tmps))) |
|
(for-tail |
|
(make-let src |
|
(append names tmps) |
|
(append gensyms tmp-syms) |
|
(append vals rest-args) |
|
(make-call |
|
asrc |
|
proc |
|
(append args |
|
(map (cut make-lexical-ref #f <> <>) |
|
tmps tmp-syms))))))) |
|
(($ <let> src names gensyms vals body) |
|
(define (lookup-alias exp) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(match exp |
|
(($ <lexical-ref> _ _ sym) |
|
(let ((op (lookup sym))) |
|
(and (not (var-set? (operand-var op))) op))) |
|
(_ #f))) |
|
|
|
(let* ((vars (map lookup-var gensyms)) |
|
(new (fresh-gensyms vars)) |
|
(ops (make-bound-operands vars new vals |
|
(lambda (exp counter ctx) |
|
(loop exp env counter ctx)) |
|
(map lookup-alias vals))) |
|
(env (fold extend-env env gensyms ops)) |
|
(body (loop body env counter ctx))) |
|
(match body |
|
(($ <const>) |
|
(for-tail (list->seq src (append vals (list body))))) |
|
(($ <lexical-ref> _ _ (? (lambda (sym) (memq sym new)) sym)) |
|
(let ((pairs (map cons new vals))) |
|
|
|
(for-tail |
|
(list->seq |
|
src |
|
(append (map cdr (alist-delete sym pairs eq?)) |
|
(list (assq-ref pairs sym))))))) |
|
((and ($ <conditional> src* |
|
($ <lexical-ref> _ _ sym) ($ <lexical-ref> _ _ sym) alt) |
|
(? (lambda (_) |
|
(case ctx |
|
((test effect) |
|
(and (equal? (list sym) new) |
|
(= (lexical-refcount sym) 2))) |
|
(else #f))))) |
|
|
|
(make-conditional src* (visit-operand (car ops) counter 'test) |
|
(make-const src* #t) alt)) |
|
(_ |
|
|
|
|
|
(prune-bindings ops #f body counter ctx |
|
(lambda (names gensyms vals body) |
|
(if (null? names) (error "what!" names)) |
|
(make-let src names gensyms vals body))))))) |
|
(($ <fix> src names gensyms vals body) |
|
|
|
|
|
|
|
|
|
|
|
(letrec* ((visit (lambda (exp counter ctx) |
|
(loop exp env* counter ctx))) |
|
(vars (map lookup-var gensyms)) |
|
(new (fresh-gensyms vars)) |
|
(ops (make-bound-operands vars new vals visit)) |
|
(env* (fold extend-env env gensyms ops)) |
|
(body* (visit body counter ctx))) |
|
(if (const? body*) |
|
body* |
|
(prune-bindings ops #f body* counter ctx |
|
(lambda (names gensyms vals body) |
|
(make-fix src names gensyms vals body)))))) |
|
(($ <let-values> lv-src producer consumer) |
|
|
|
|
|
|
|
(let ((producer (for-values producer))) |
|
(or (match consumer |
|
((and ($ <lambda-case> src () #f rest #f () (rest-sym) body #f) |
|
(? (lambda _ (singly-valued-expression? producer)))) |
|
(let ((tmp (gensym "tmp "))) |
|
(record-new-temporary! 'tmp tmp 1) |
|
(for-tail |
|
(make-let |
|
src (list 'tmp) (list tmp) (list producer) |
|
(make-let |
|
src (list rest) (list rest-sym) |
|
(list |
|
(make-primcall #f 'list |
|
(list (make-lexical-ref #f 'tmp tmp)))) |
|
body))))) |
|
(($ <lambda-case> src req opt rest #f inits gensyms body #f) |
|
(let* ((nmin (length req)) |
|
(nmax (and (not rest) (+ nmin (if opt (length opt) 0))))) |
|
(cond |
|
((inline-values lv-src producer nmin nmax consumer) |
|
=> for-tail) |
|
(else #f)))) |
|
(_ #f)) |
|
(make-let-values lv-src producer (for-tail consumer))))) |
|
(($ <toplevel-ref> src mod (? effect-free-primitive? name)) |
|
exp) |
|
(($ <toplevel-ref>) |
|
|
|
exp) |
|
(($ <module-ref> src module (? effect-free-primitive? name) #f) |
|
(let ((module (false-if-exception |
|
(resolve-module module #:ensure #f)))) |
|
(if (module? module) |
|
(let ((var (module-variable module name))) |
|
(if (eq? var (module-variable the-scm-module name)) |
|
(make-primitive-ref src name) |
|
exp)) |
|
exp))) |
|
(($ <module-ref> src module name public?) |
|
(cond |
|
((and cross-module-inlining? |
|
public? |
|
(and=> (resolve-module module #:ensure #f) |
|
(lambda (module) |
|
(and=> (module-public-interface module) |
|
(lambda (iface) |
|
(and=> (module-inlinable-exports iface) |
|
(lambda (proc) (proc name)))))))) |
|
=> (lambda (inlined) |
|
|
|
|
|
(log 'begin-xm-copy exp inlined) |
|
(cond |
|
((eq? ctx 'effect) |
|
(log 'xm-effect) |
|
(make-void #f)) |
|
((eq? ctx 'call) |
|
|
|
(log 'residualize-xm-call exp) |
|
exp) |
|
((or (const? inlined) (void? inlined) (primitive-ref? inlined)) |
|
|
|
|
|
(log 'copy-xm-const) |
|
(for-tail inlined)) |
|
|
|
|
|
|
|
|
|
((and (eq? ctx 'operator) (lambda? inlined) |
|
(small-expression? inlined operator-size-limit)) |
|
(log 'copy-xm-operator exp inlined) |
|
(splice-expression inlined)) |
|
(else |
|
(log 'xm-copy-failed) |
|
|
|
|
|
exp)))) |
|
(else exp))) |
|
(($ <module-set> src mod name public? exp) |
|
(make-module-set src mod name public? (for-value exp))) |
|
(($ <toplevel-define> src mod name exp) |
|
(make-toplevel-define src mod name (for-value exp))) |
|
(($ <toplevel-set> src mod name exp) |
|
(make-toplevel-set src mod name (for-value exp))) |
|
(($ <primitive-ref>) |
|
(case ctx |
|
((effect) (make-void #f)) |
|
((test) (make-const #f #t)) |
|
(else exp))) |
|
(($ <conditional> src condition subsequent alternate) |
|
(define (call-with-failure-thunk exp proc) |
|
(match exp |
|
(($ <call> _ _ ()) (proc exp)) |
|
(($ <primcall> _ _ ()) (proc exp)) |
|
(($ <const>) (proc exp)) |
|
(($ <void>) (proc exp)) |
|
(($ <lexical-ref>) (proc exp)) |
|
(_ |
|
(let ((t (gensym "failure-"))) |
|
(record-new-temporary! 'failure t 2) |
|
(make-let |
|
src (list 'failure) (list t) |
|
(list |
|
(make-lambda |
|
#f '() |
|
(make-lambda-case #f '() #f #f #f '() '() exp #f))) |
|
(proc (make-call #f (make-lexical-ref #f 'failure t) |
|
'()))))))) |
|
(define (simplify-conditional c) |
|
(match c |
|
|
|
(($ <conditional> src ($ <primcall> _ 'not (pred)) |
|
subsequent alternate) |
|
(simplify-conditional |
|
(make-conditional src pred alternate subsequent))) |
|
|
|
|
|
|
|
(($ <conditional> src ($ <let> src* names vars vals body) |
|
subsequent alternate) |
|
(make-let src* names vars vals |
|
(simplify-conditional |
|
(make-conditional src body subsequent alternate)))) |
|
(($ <conditional> src ($ <fix> src* names vars vals body) |
|
subsequent alternate) |
|
(make-fix src* names vars vals |
|
(simplify-conditional |
|
(make-conditional src body subsequent alternate)))) |
|
(($ <conditional> src ($ <seq> src* head tail) |
|
subsequent alternate) |
|
(make-seq src* head |
|
(simplify-conditional |
|
(make-conditional src tail subsequent alternate)))) |
|
|
|
|
|
(($ <conditional> src |
|
($ <conditional> src* outer-test inner-test ($ <const> _ #f)) |
|
inner-subsequent |
|
alternate) |
|
(let lp ((alternate alternate)) |
|
(match alternate |
|
|
|
|
|
(($ <conditional> _ (? (cut tree-il=? outer-test <>)) |
|
other-subsequent alternate) |
|
(make-conditional |
|
src outer-test |
|
(simplify-conditional |
|
(make-conditional src* inner-test inner-subsequent |
|
other-subsequent)) |
|
alternate)) |
|
|
|
|
|
(($ <let> let-src (name) (sym) ((and thunk ($ <lambda>))) body) |
|
(make-let |
|
let-src (list name) (list sym) (list thunk) |
|
(lp body))) |
|
|
|
|
|
|
|
|
|
|
|
(_ |
|
(call-with-failure-thunk |
|
alternate |
|
(lambda (failure) |
|
(make-conditional |
|
src outer-test |
|
(simplify-conditional |
|
(make-conditional src* inner-test inner-subsequent failure)) |
|
failure))))))) |
|
(_ c))) |
|
(match (for-test condition) |
|
(($ <const> _ val) |
|
(if val |
|
(for-tail subsequent) |
|
(for-tail alternate))) |
|
(c |
|
(simplify-conditional |
|
(make-conditional src c (for-tail subsequent) |
|
(for-tail alternate)))))) |
|
(($ <primcall> src 'call-with-values |
|
(producer |
|
($ <lambda> _ _ |
|
(and consumer |
|
|
|
($ <lambda-case> |
|
_ req #f rest #f () gensyms body #f))))) |
|
(for-tail (make-let-values src (make-call src producer '()) |
|
consumer))) |
|
(($ <primcall> src 'dynamic-wind (w thunk u)) |
|
(for-tail |
|
(with-temporaries |
|
src (list w u) 2 constant-expression? |
|
(match-lambda |
|
((w u) |
|
(make-seq |
|
src |
|
(make-seq |
|
src |
|
(make-conditional |
|
src |
|
|
|
(make-primcall src 'thunk? (list u)) |
|
(make-call src w '()) |
|
(make-primcall src 'raise-type-error |
|
(list (make-const #f #("dynamic-wind" 3 "thunk")) |
|
u))) |
|
(make-primcall src 'wind (list w u))) |
|
(make-begin0 src |
|
(make-call src thunk '()) |
|
(make-seq src |
|
(make-primcall src 'unwind '()) |
|
(make-call src u '()))))))))) |
|
|
|
(($ <primcall> src 'with-fluid* (f v thunk)) |
|
(for-tail |
|
(with-temporaries |
|
src (list f v thunk) 1 constant-expression? |
|
(match-lambda |
|
((f v thunk) |
|
(make-seq src |
|
(make-primcall src 'push-fluid (list f v)) |
|
(make-begin0 src |
|
(make-call src thunk '()) |
|
(make-primcall src 'pop-fluid '())))))))) |
|
|
|
(($ <primcall> src 'with-dynamic-state (state thunk)) |
|
(for-tail |
|
(with-temporaries |
|
src (list state thunk) 1 constant-expression? |
|
(match-lambda |
|
((state thunk) |
|
(make-seq src |
|
(make-primcall src 'push-dynamic-state (list state)) |
|
(make-begin0 src |
|
(make-call src thunk '()) |
|
(make-primcall src 'pop-dynamic-state |
|
'())))))))) |
|
|
|
(($ <primcall> src 'values exps) |
|
(match exps |
|
(() |
|
(case ctx |
|
((effect) (make-void #f)) |
|
((values) exp) |
|
|
|
|
|
(else (make-primcall src 'values (list exp))))) |
|
((($ <primcall> _ 'values ())) exp) |
|
(_ |
|
(let ((vals (map for-value exps))) |
|
(if (and (case ctx |
|
((value test effect) #t) |
|
(else (null? (cdr vals)))) |
|
(every singly-valued-expression? vals)) |
|
(for-tail (list->seq src (append (cdr vals) (list (car vals))))) |
|
(make-primcall src 'values vals)))))) |
|
|
|
(($ <primcall> src 'apply (proc args ... tail)) |
|
(let lp ((tail* (find-definition tail 1)) (speculative? #t)) |
|
(define (copyable? x) |
|
|
|
|
|
|
|
(or (not speculative?) (constant-expression? x))) |
|
(match tail* |
|
(($ <const> _ (args* ...)) |
|
(let ((args* (map (cut make-const #f <>) args*))) |
|
(for-tail (make-call src proc (append args args*))))) |
|
(($ <primcall> _ 'cons |
|
((and head (? copyable?)) (and tail (? copyable?)))) |
|
(for-tail (make-primcall src 'apply |
|
(cons proc |
|
(append args (list head tail)))))) |
|
(($ <primcall> _ 'list |
|
(and args* ((? copyable?) ...))) |
|
(for-tail (make-call src proc (append args args*)))) |
|
(tail* |
|
(if speculative? |
|
(lp (for-value tail) #f) |
|
(let ((args (append (map for-value args) (list tail*)))) |
|
(make-primcall src 'apply |
|
(cons (for-value proc) args)))))))) |
|
|
|
(($ <primcall> src 'append (x z)) |
|
(let ((x (for-value x))) |
|
(match x |
|
((or ($ <const> _ ()) |
|
($ <primcall> _ 'list ())) |
|
(for-value z)) |
|
((or ($ <const> _ (_ . _)) |
|
($ <primcall> _ 'cons) |
|
($ <primcall> _ 'list)) |
|
(for-tail |
|
(let lp ((x x)) |
|
(match x |
|
((or ($ <const> csrc ()) |
|
($ <primcall> csrc 'list ())) |
|
|
|
z) |
|
(($ <const> csrc (x . y)) |
|
(let ((x (make-const csrc x)) |
|
(y (make-const csrc y))) |
|
(make-primcall src 'cons (list x (lp y))))) |
|
(($ <primcall> csrc 'cons (x y)) |
|
(make-primcall src 'cons (list x (lp y)))) |
|
(($ <primcall> csrc 'list (x . y)) |
|
(let ((y (make-primcall csrc 'list y))) |
|
(make-primcall src 'cons (list x (lp y))))) |
|
(x (make-primcall src 'append (list x z))))))) |
|
(else |
|
(make-primcall src 'append (list x (for-value z))))))) |
|
|
|
(($ <primcall> src (? constructor-primitive? name) args) |
|
(cond |
|
((and (memq ctx '(effect test)) |
|
(match (cons name args) |
|
((or ('cons _ _) |
|
('list . _) |
|
('vector . _) |
|
('make-prompt-tag) |
|
('make-prompt-tag ($ <const> _ (? string?)))) |
|
#t) |
|
(_ #f))) |
|
(let ((res (if (eq? ctx 'effect) |
|
(make-void #f) |
|
(make-const #f #t)))) |
|
(for-tail (list->seq src (append (map for-value args) |
|
(list res)))))) |
|
(else |
|
(match (cons name (map for-value args)) |
|
(('cons x ($ <const> _ (? (cut eq? <> '())))) |
|
(make-primcall src 'list (list x))) |
|
(('cons x ($ <primcall> _ 'list elts)) |
|
(make-primcall src 'list (cons x elts))) |
|
(('list) |
|
(make-const src '())) |
|
(('vector) |
|
(make-const src '#())) |
|
((name . args) |
|
(make-primcall src name args)))))) |
|
|
|
(($ <primcall> src 'thunk? (proc)) |
|
(case ctx |
|
((effect) |
|
(for-tail (make-seq src proc (make-void src)))) |
|
(else |
|
(match (for-value proc) |
|
(($ <lambda> _ _ ($ <lambda-case> _ req)) |
|
(for-tail (make-const src (null? req)))) |
|
(proc |
|
(match (find-definition proc 2) |
|
(($ <lambda> _ _ ($ <lambda-case> _ req)) |
|
(for-tail (make-const src (null? req)))) |
|
(_ |
|
(make-primcall src 'thunk? (list proc))))))))) |
|
|
|
(($ <primcall> src name args) |
|
(match (cons name (map for-value args)) |
|
|
|
|
|
(('car ($ <primcall> src 'cons (head tail))) |
|
(for-tail (make-seq src tail head))) |
|
(('cdr ($ <primcall> src 'cons (head tail))) |
|
(for-tail (make-seq src head tail))) |
|
(('car ($ <primcall> src 'list (head . tail))) |
|
(for-tail (list->seq src (append tail (list head))))) |
|
(('cdr ($ <primcall> src 'list (head . tail))) |
|
(for-tail (make-seq src head (make-primcall #f 'list tail)))) |
|
|
|
(('car ($ <const> src (head . tail))) |
|
(for-tail (make-const src head))) |
|
(('cdr ($ <const> src (head . tail))) |
|
(for-tail (make-const src tail))) |
|
(((or 'memq 'memv) k ($ <const> _ (elts ...))) |
|
|
|
(case ctx |
|
((effect) |
|
(for-tail |
|
(make-seq src k (make-void #f)))) |
|
((test) |
|
(cond |
|
((const? k) |
|
|
|
|
|
(let ((member (case name ((memq) memq) ((memv) memv)))) |
|
(make-const #f (and (member (const-exp k) elts) #t)))) |
|
((null? elts) |
|
(for-tail |
|
(make-seq src k (make-const #f #f)))) |
|
(else |
|
(let ((t (gensym "t ")) |
|
(eq (if (eq? name 'memq) 'eq? 'eqv?))) |
|
(record-new-temporary! 't t (length elts)) |
|
(for-tail |
|
(make-let |
|
src (list 't) (list t) (list k) |
|
(let lp ((elts elts)) |
|
(define test |
|
(make-primcall #f eq |
|
(list (make-lexical-ref #f 't t) |
|
(make-const #f (car elts))))) |
|
(if (null? (cdr elts)) |
|
test |
|
(make-conditional src test |
|
(make-const #f #t) |
|
(lp (cdr elts))))))))))) |
|
(else |
|
(cond |
|
((const? k) |
|
(let ((member (case name ((memq) memq) ((memv) memv)))) |
|
(make-const #f (member (const-exp k) elts)))) |
|
((null? elts) |
|
(for-tail (make-seq src k (make-const #f #f)))) |
|
(else |
|
(make-primcall src name (list k (make-const #f elts)))))))) |
|
|
|
(((? equality-primitive?) a (and b ($ <const> _ v))) |
|
(cond |
|
((const? a) |
|
|
|
|
|
|
|
(for-tail (make-const src (equal? (const-exp a) v)))) |
|
((eq? name 'eq?) |
|
|
|
(make-primcall src 'eq? (list a b))) |
|
((or (memq v '(#f #t () #nil)) (symbol? v) (char? v) |
|
|
|
|
|
|
|
(and (exact-integer? v) |
|
(<= (max (target-most-negative-fixnum) |
|
most-negative-fixnum) |
|
v |
|
(min (target-most-positive-fixnum) |
|
most-positive-fixnum)))) |
|
|
|
|
|
(make-primcall src 'eq? (list a b))) |
|
((number? v) |
|
|
|
|
|
(make-primcall src 'eqv? (list a b))) |
|
((eq? name 'eqv?) |
|
|
|
(make-primcall src 'eq? (list a b))) |
|
(else |
|
|
|
|
|
(make-primcall src name (list a b))))) |
|
(((? equality-primitive?) (and a ($ <const>)) b) |
|
(for-tail (make-primcall src name (list b a)))) |
|
(((? equality-primitive?) ($ <lexical-ref> _ _ sym) |
|
($ <lexical-ref> _ _ sym)) |
|
(for-tail (make-const src #t))) |
|
|
|
(('logbit? ($ <const> src2 |
|
(? (lambda (bit) |
|
(and (exact-integer? bit) |
|
(<= 0 bit (logcount most-positive-fixnum)))) |
|
bit)) |
|
val) |
|
(for-tail |
|
(make-primcall src 'logtest |
|
(list (make-const src2 (ash 1 bit)) val)))) |
|
|
|
(('logtest a b) |
|
(for-tail |
|
(make-primcall |
|
src |
|
'not |
|
(list |
|
(make-primcall src 'eq? |
|
(list (make-primcall src 'logand (list a b)) |
|
(make-const src 0))))))) |
|
|
|
(((? effect-free-primitive?) . args) |
|
(fold-constants src name args ctx)) |
|
|
|
((name . args) |
|
(if (and (eq? ctx 'effect) (effect-free-primcall? name args)) |
|
(if (null? args) |
|
(make-void src) |
|
(for-tail (list->seq src args))) |
|
(make-primcall src name args))))) |
|
|
|
(($ <call> src orig-proc orig-args) |
|
(define (residualize-call) |
|
(make-call src (for-call orig-proc) (map for-value orig-args))) |
|
|
|
(define (singly-referenced-lambda? proc) |
|
(match proc |
|
(($ <lambda>) #t) |
|
(($ <lexical-ref> _ _ sym) |
|
(and (not (assigned-lexical? sym)) |
|
(= (lexical-refcount sym) 1) |
|
(singly-referenced-lambda? |
|
(operand-source (lookup sym))))) |
|
(_ #f))) |
|
|
|
(define (attempt-inlining proc names syms vals body) |
|
(define inline-key (source-expression proc)) |
|
(define existing-counter (find-counter inline-key counter)) |
|
(define inlined-exp (make-let src names syms vals body)) |
|
|
|
(cond |
|
((and=> existing-counter counter-recursive?) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(let lp ((counter counter)) |
|
(unless (eq? counter existing-counter) |
|
(set-counter-recursive?! counter #t) |
|
(lp (counter-prev counter)))) |
|
|
|
(log 'inline-recurse inline-key) |
|
(loop inlined-exp env counter ctx)) |
|
((singly-referenced-lambda? orig-proc) |
|
|
|
|
|
(log 'inline-beta inline-key) |
|
(loop inlined-exp env counter ctx)) |
|
(else |
|
|
|
|
|
|
|
|
|
(log 'inline-begin exp) |
|
(let/ec k |
|
(define (abort) |
|
(log 'inline-abort exp) |
|
(k (residualize-call))) |
|
(define new-counter |
|
(cond |
|
|
|
|
|
(existing-counter |
|
(make-recursive-counter recursive-effort-limit |
|
operand-size-limit |
|
existing-counter counter)) |
|
(counter |
|
(make-nested-counter abort inline-key counter)) |
|
|
|
|
|
|
|
(else |
|
(make-top-counter effort-limit operand-size-limit |
|
abort inline-key)))) |
|
(define result |
|
(loop inlined-exp env new-counter ctx)) |
|
|
|
(when counter |
|
|
|
|
|
|
|
(transfer! new-counter counter)) |
|
|
|
(log 'inline-end result exp) |
|
result)))) |
|
|
|
(let revisit-proc ((proc (visit orig-proc 'operator))) |
|
(match proc |
|
(($ <primitive-ref> _ name) |
|
(let ((exp (expand-primcall (make-primcall src name orig-args)))) |
|
(set! store |
|
(augment-var-table-with-externally-introduced-lexicals |
|
exp store)) |
|
(for-tail exp))) |
|
|
|
(($ <lambda> _ _ clause) |
|
|
|
|
|
(define (inline-clause req opt rest kw inits gensyms body |
|
arity-mismatch) |
|
(define (bind name sym val binds) |
|
(cons (vector name sym val) binds)) |
|
(define (has-binding? binds sym) |
|
(match binds |
|
(() #f) |
|
((#(n s v) . binds) |
|
(or (eq? s sym) (has-binding? binds sym))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (process-req req syms args pbinds sbinds) |
|
(match req |
|
(() (process-opt (or opt '()) syms inits args pbinds sbinds)) |
|
((name . req) |
|
(match syms |
|
((sym . syms) |
|
(match args |
|
(() (arity-mismatch)) |
|
((arg . args) |
|
(process-req req syms args |
|
(bind name sym arg pbinds) |
|
sbinds)))))))) |
|
|
|
(define (keyword-arg? exp) |
|
(match exp |
|
(($ <const> _ (? keyword?)) #t) |
|
(_ #f))) |
|
(define (not-keyword-arg? exp) |
|
(match exp |
|
((or ($ <const> _ (not (? keyword?))) |
|
($ <void>) |
|
($ <primitive-ref>) |
|
($ <lambda>)) |
|
#t) |
|
(_ #f))) |
|
|
|
(define (process-opt opt syms inits args pbinds sbinds) |
|
(match opt |
|
(() (process-rest syms inits args pbinds sbinds)) |
|
((name . opt) |
|
(match inits |
|
((init . inits) |
|
(match syms |
|
((sym . syms) |
|
(cond |
|
(kw |
|
(match args |
|
((or () ((? keyword-arg?) . _)) |
|
|
|
|
|
(process-opt opt syms inits args pbinds |
|
(bind name sym init sbinds))) |
|
(((? not-keyword-arg? arg) . args) |
|
|
|
|
|
(process-opt opt syms inits args |
|
(bind name sym arg pbinds) |
|
sbinds)) |
|
(_ |
|
|
|
|
|
(residualize-call)))) |
|
(else |
|
|
|
(match args |
|
(() |
|
(process-opt opt syms inits args pbinds |
|
(bind name sym init sbinds))) |
|
((arg . args) |
|
(process-opt opt syms inits args |
|
(bind name sym arg pbinds) |
|
sbinds)))))))))))) |
|
|
|
(define (process-rest syms inits args pbinds sbinds) |
|
(match rest |
|
(#f |
|
(match kw |
|
((#f . kw) |
|
(process-kw kw syms inits args pbinds sbinds)) |
|
(#f |
|
(unless (and (null? syms) (null? inits)) |
|
(error "internal error")) |
|
(match args |
|
(() (finish pbinds sbinds body)) |
|
(_ (arity-mismatch)))))) |
|
(rest |
|
(match syms |
|
((sym . syms) |
|
(let ((rest-val (make-primcall src 'list args))) |
|
(unless (and (null? syms) (null? inits)) |
|
(error "internal error")) |
|
(finish pbinds (bind rest sym rest-val sbinds) |
|
body))))))) |
|
|
|
(define (process-kw kw syms inits args pbinds sbinds) |
|
|
|
|
|
|
|
|
|
|
|
(unless (equal? syms (match kw (((k name sym) ...) sym))) |
|
(error "internal error: unexpected kwarg syms" kw syms)) |
|
|
|
(define (process-kw-args positional? args pbinds) |
|
(match args |
|
(() |
|
(process-kw-inits kw inits pbinds sbinds)) |
|
((($ <const> _ (? keyword? keyword)) arg . args) |
|
(match (assq keyword kw) |
|
((keyword name sym) |
|
|
|
|
|
|
|
(if (has-binding? pbinds sym) |
|
(residualize-call) |
|
(process-kw-args #f args |
|
(bind name sym arg pbinds)))) |
|
(#f (residualize-call)))) |
|
(((? not-keyword-arg?) . args) |
|
(if positional? |
|
(arity-mismatch) |
|
(residualize-call))) |
|
(_ (residualize-call)))) |
|
|
|
(define (process-kw-inits kw inits pbinds sbinds) |
|
(match kw |
|
(() |
|
(unless (null? inits) (error "internal error")) |
|
(finish pbinds sbinds body)) |
|
(((keyword name sym) . kw) |
|
(match inits |
|
((init . inits) |
|
(process-kw-inits kw inits pbinds |
|
(if (has-binding? pbinds sym) |
|
sbinds |
|
(bind name sym init sbinds)))))))) |
|
|
|
(process-kw-args #t args pbinds)) |
|
|
|
(define (finish pbinds sbinds body) |
|
(match sbinds |
|
(() |
|
(match (reverse pbinds) |
|
((#(name sym val) ...) |
|
(attempt-inlining proc name sym val body)))) |
|
((#(name sym val) . sbinds) |
|
(finish pbinds sbinds |
|
(make-let src (list name) (list sym) (list val) |
|
body))))) |
|
|
|
|
|
|
|
|
|
|
|
(cond |
|
((and kw (or rest (match kw ((aok? . _) aok?)))) |
|
(residualize-call)) |
|
(else |
|
(process-req req gensyms orig-args '() '())))) |
|
|
|
(let lp ((clause clause)) |
|
(match clause |
|
|
|
(#f (residualize-call)) |
|
(($ <lambda-case> src req opt rest kw inits gensyms body alt) |
|
(inline-clause req opt rest kw inits gensyms body |
|
(lambda () (lp alt))))))) |
|
|
|
(($ <let> _ _ _ vals _) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(if (or (and-map constant-expression? vals) |
|
(and-map constant-expression? orig-args)) |
|
|
|
(match (for-value orig-proc) |
|
(($ <let> lsrc names syms vals body) |
|
(log 'inline-let orig-proc) |
|
(for-tail |
|
(make-let lsrc names syms vals |
|
(make-call src body orig-args)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(proc (revisit-proc proc))) |
|
(residualize-call))) |
|
|
|
(_ (residualize-call))))) |
|
|
|
(($ <lambda> src meta body) |
|
(case ctx |
|
((effect) (make-void #f)) |
|
((test) (make-const #f #t)) |
|
((operator) exp) |
|
(else (record-source-expression! |
|
exp |
|
(make-lambda src meta (and body (for-values body))))))) |
|
(($ <lambda-case> src req opt rest kw inits gensyms body alt) |
|
(define (lift-applied-lambda body gensyms) |
|
(and (not opt) rest (not kw) |
|
(match body |
|
(($ <primcall> _ 'apply |
|
(($ <lambda> _ _ (and lcase ($ <lambda-case> _ req1))) |
|
($ <lexical-ref> _ _ sym) |
|
...)) |
|
(and (equal? sym gensyms) |
|
(not (lambda-case-alternate lcase)) |
|
(<= (length req) (length req1)) |
|
(every (lambda (s) |
|
(= (lexical-refcount s) 1)) |
|
sym) |
|
lcase)) |
|
(_ #f)))) |
|
(let* ((vars (map lookup-var gensyms)) |
|
(new (fresh-gensyms vars)) |
|
(env (fold extend-env env gensyms |
|
(make-unbound-operands vars new))) |
|
(new-sym (lambda (old) |
|
(operand-sym (cdr (vhash-assq old env))))) |
|
(body (loop body env counter ctx))) |
|
(or |
|
|
|
(lift-applied-lambda body new) |
|
(make-lambda-case src req opt rest |
|
(match kw |
|
((aok? (kw name old) ...) |
|
(cons aok? (map list kw name (map new-sym old)))) |
|
(_ #f)) |
|
(map (cut loop <> env counter 'value) inits) |
|
new |
|
body |
|
(and alt (for-tail alt)))))) |
|
(($ <seq> src head tail) |
|
(let ((head (for-effect head)) |
|
(tail (for-tail tail))) |
|
(if (void? head) |
|
tail |
|
(make-seq src |
|
(if (and (seq? head) |
|
(void? (seq-tail head))) |
|
(seq-head head) |
|
head) |
|
tail)))) |
|
(($ <prompt> src escape-only? tag body handler) |
|
(define (make-prompt-tag? x) |
|
(match x |
|
(($ <primcall> _ 'make-prompt-tag (or () ((? constant-expression?)))) |
|
#t) |
|
(_ #f))) |
|
|
|
(let ((tag (for-value tag)) |
|
(body (if escape-only? (for-tail body) (for-value body)))) |
|
(cond |
|
((find-definition tag 1) |
|
(lambda (val op) |
|
(make-prompt-tag? val)) |
|
=> (lambda (val op) |
|
|
|
|
|
|
|
(when op (unrecord-operand-uses op 1)) |
|
(for-tail (if escape-only? body (make-call src body '()))))) |
|
(else |
|
(let ((handler (for-value handler))) |
|
(define (escape-only-handler? handler) |
|
(match handler |
|
(($ <lambda> _ _ |
|
($ <lambda-case> _ (_ . _) _ _ _ _ (k . _) body #f)) |
|
(not (tree-il-any |
|
(match-lambda |
|
(($ <lexical-ref> _ _ (? (cut eq? <> k))) #t) |
|
(_ #f)) |
|
body))) |
|
(else #f))) |
|
(if (and (not escape-only?) (escape-only-handler? handler)) |
|
|
|
|
|
(for-tail |
|
(make-prompt src #t tag (make-call #f body '()) handler)) |
|
(make-prompt src escape-only? tag body handler))))))) |
|
|
|
(($ <abort> src tag args tail) |
|
(make-abort src (for-value tag) (map for-value args) |
|
(for-value tail)))))) |
|
|