| 1 | ;;; Tree-IL partial evaluator |
| 2 | |
| 3 | ;; Copyright (C) 2011-2014 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;;;; This library is free software; you can redistribute it and/or |
| 6 | ;;;; modify it under the terms of the GNU Lesser General Public |
| 7 | ;;;; License as published by the Free Software Foundation; either |
| 8 | ;;;; version 3 of the License, or (at your option) any later version. |
| 9 | ;;;; |
| 10 | ;;;; This library is distributed in the hope that it will be useful, |
| 11 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | ;;;; Lesser General Public License for more details. |
| 14 | ;;;; |
| 15 | ;;;; You should have received a copy of the GNU Lesser General Public |
| 16 | ;;;; License along with this library; if not, write to the Free Software |
| 17 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | |
| 19 | (define-module (language tree-il peval) |
| 20 | #:use-module (language tree-il) |
| 21 | #:use-module (language tree-il primitives) |
| 22 | #:use-module (language tree-il effects) |
| 23 | #:use-module (ice-9 vlist) |
| 24 | #:use-module (ice-9 match) |
| 25 | #:use-module (srfi srfi-1) |
| 26 | #:use-module (srfi srfi-9) |
| 27 | #:use-module (srfi srfi-11) |
| 28 | #:use-module (srfi srfi-26) |
| 29 | #:use-module (ice-9 control) |
| 30 | #:export (peval)) |
| 31 | |
| 32 | ;;; |
| 33 | ;;; Partial evaluation is Guile's most important source-to-source |
| 34 | ;;; optimization pass. It performs copy propagation, dead code |
| 35 | ;;; elimination, inlining, and constant folding, all while preserving |
| 36 | ;;; the order of effects in the residual program. |
| 37 | ;;; |
| 38 | ;;; For more on partial evaluation, see William Cook’s excellent |
| 39 | ;;; tutorial on partial evaluation at DSL 2011, called “Build your own |
| 40 | ;;; partial evaluator in 90 minutes”[0]. |
| 41 | ;;; |
| 42 | ;;; Our implementation of this algorithm was heavily influenced by |
| 43 | ;;; Waddell and Dybvig's paper, "Fast and Effective Procedure Inlining", |
| 44 | ;;; IU CS Dept. TR 484. |
| 45 | ;;; |
| 46 | ;;; [0] http://www.cs.utexas.edu/~wcook/tutorial/. |
| 47 | ;;; |
| 48 | |
| 49 | ;; First, some helpers. |
| 50 | ;; |
| 51 | (define-syntax *logging* (identifier-syntax #f)) |
| 52 | |
| 53 | ;; For efficiency we define *logging* to inline to #f, so that the call |
| 54 | ;; to log* gets optimized out. If you want to log, uncomment these |
| 55 | ;; lines: |
| 56 | ;; |
| 57 | ;; (define %logging #f) |
| 58 | ;; (define-syntax *logging* (identifier-syntax %logging)) |
| 59 | ;; |
| 60 | ;; Then you can change %logging at runtime. |
| 61 | |
| 62 | (define-syntax log |
| 63 | (syntax-rules (quote) |
| 64 | ((log 'event arg ...) |
| 65 | (if (and *logging* |
| 66 | (or (eq? *logging* #t) |
| 67 | (memq 'event *logging*))) |
| 68 | (log* 'event arg ...))))) |
| 69 | |
| 70 | (define (log* event . args) |
| 71 | (let ((pp (module-ref (resolve-interface '(ice-9 pretty-print)) |
| 72 | 'pretty-print))) |
| 73 | (pp `(log ,event . ,args)) |
| 74 | (newline) |
| 75 | (values))) |
| 76 | |
| 77 | (define (tree-il-any proc exp) |
| 78 | (let/ec k |
| 79 | (tree-il-fold (lambda (exp res) |
| 80 | (let ((res (proc exp))) |
| 81 | (if res (k res) #f))) |
| 82 | (lambda (exp res) #f) |
| 83 | #f exp))) |
| 84 | |
| 85 | (define (vlist-any proc vlist) |
| 86 | (let ((len (vlist-length vlist))) |
| 87 | (let lp ((i 0)) |
| 88 | (and (< i len) |
| 89 | (or (proc (vlist-ref vlist i)) |
| 90 | (lp (1+ i))))))) |
| 91 | |
| 92 | (define (singly-valued-expression? exp) |
| 93 | (match exp |
| 94 | (($ <const>) #t) |
| 95 | (($ <lexical-ref>) #t) |
| 96 | (($ <void>) #t) |
| 97 | (($ <lexical-ref>) #t) |
| 98 | (($ <primitive-ref>) #t) |
| 99 | (($ <module-ref>) #t) |
| 100 | (($ <toplevel-ref>) #t) |
| 101 | (($ <primcall> _ (? singly-valued-primitive?)) #t) |
| 102 | (($ <primcall> _ 'values (val)) #t) |
| 103 | (($ <lambda>) #t) |
| 104 | (($ <conditional> _ test consequent alternate) |
| 105 | (and (singly-valued-expression? consequent) |
| 106 | (singly-valued-expression? alternate))) |
| 107 | (else #f))) |
| 108 | |
| 109 | (define (truncate-values x) |
| 110 | "Discard all but the first value of X." |
| 111 | (if (singly-valued-expression? x) |
| 112 | x |
| 113 | (make-primcall (tree-il-src x) 'values (list x)))) |
| 114 | |
| 115 | ;; Peval will do a one-pass analysis on the source program to determine |
| 116 | ;; the set of assigned lexicals, and to identify unreferenced and |
| 117 | ;; singly-referenced lexicals. |
| 118 | ;; |
| 119 | (define-record-type <var> |
| 120 | (make-var name gensym refcount set?) |
| 121 | var? |
| 122 | (name var-name) |
| 123 | (gensym var-gensym) |
| 124 | (refcount var-refcount set-var-refcount!) |
| 125 | (set? var-set? set-var-set?!)) |
| 126 | |
| 127 | (define* (build-var-table exp #:optional (table vlist-null)) |
| 128 | (tree-il-fold |
| 129 | (lambda (exp res) |
| 130 | (match exp |
| 131 | (($ <lexical-ref> src name gensym) |
| 132 | (let ((var (cdr (vhash-assq gensym res)))) |
| 133 | (set-var-refcount! var (1+ (var-refcount var))) |
| 134 | res)) |
| 135 | (($ <lambda-case> src req opt rest kw init gensyms body alt) |
| 136 | (fold (lambda (name sym res) |
| 137 | (vhash-consq sym (make-var name sym 0 #f) res)) |
| 138 | res |
| 139 | (append req (or opt '()) (if rest (list rest) '()) |
| 140 | (match kw |
| 141 | ((aok? (kw name sym) ...) name) |
| 142 | (_ '()))) |
| 143 | gensyms)) |
| 144 | (($ <let> src names gensyms vals body) |
| 145 | (fold (lambda (name sym res) |
| 146 | (vhash-consq sym (make-var name sym 0 #f) res)) |
| 147 | res names gensyms)) |
| 148 | (($ <letrec> src in-order? names gensyms vals body) |
| 149 | (fold (lambda (name sym res) |
| 150 | (vhash-consq sym (make-var name sym 0 #f) res)) |
| 151 | res names gensyms)) |
| 152 | (($ <fix> src names gensyms vals body) |
| 153 | (fold (lambda (name sym res) |
| 154 | (vhash-consq sym (make-var name sym 0 #f) res)) |
| 155 | res names gensyms)) |
| 156 | (($ <lexical-set> src name gensym exp) |
| 157 | (set-var-set?! (cdr (vhash-assq gensym res)) #t) |
| 158 | res) |
| 159 | (_ res))) |
| 160 | (lambda (exp res) res) |
| 161 | table exp)) |
| 162 | |
| 163 | ;; Counters are data structures used to limit the effort that peval |
| 164 | ;; spends on particular inlining attempts. Each call site in the source |
| 165 | ;; program is allocated some amount of effort. If peval exceeds the |
| 166 | ;; effort counter while attempting to inline a call site, it aborts the |
| 167 | ;; inlining attempt and residualizes a call instead. |
| 168 | ;; |
| 169 | ;; As there is a fixed number of call sites, that makes `peval' O(N) in |
| 170 | ;; the number of call sites in the source program. |
| 171 | ;; |
| 172 | ;; Counters should limit the size of the residual program as well, but |
| 173 | ;; currently this is not implemented. |
| 174 | ;; |
| 175 | ;; At the top level, before seeing any peval call, there is no counter, |
| 176 | ;; because inlining will terminate as there is no recursion. When peval |
| 177 | ;; sees a call at the top level, it will make a new counter, allocating |
| 178 | ;; it some amount of effort and size. |
| 179 | ;; |
| 180 | ;; This top-level effort counter effectively "prints money". Within a |
| 181 | ;; toplevel counter, no more effort is printed ex nihilo; for a nested |
| 182 | ;; inlining attempt to proceed, effort must be transferred from the |
| 183 | ;; toplevel counter to the nested counter. |
| 184 | ;; |
| 185 | ;; Via `data' and `prev', counters form a linked list, terminating in a |
| 186 | ;; toplevel counter. In practice `data' will be the a pointer to the |
| 187 | ;; source expression of the procedure being inlined. |
| 188 | ;; |
| 189 | ;; In this way peval can detect a recursive inlining attempt, by walking |
| 190 | ;; back on the `prev' links looking for matching `data'. Recursive |
| 191 | ;; counters receive a more limited effort allocation, as we don't want |
| 192 | ;; to spend all of the effort for a toplevel inlining site on loops. |
| 193 | ;; Also, recursive counters don't need a prompt at each inlining site: |
| 194 | ;; either the call chain folds entirely, or it will be residualized at |
| 195 | ;; its original call. |
| 196 | ;; |
| 197 | (define-record-type <counter> |
| 198 | (%make-counter effort size continuation recursive? data prev) |
| 199 | counter? |
| 200 | (effort effort-counter) |
| 201 | (size size-counter) |
| 202 | (continuation counter-continuation) |
| 203 | (recursive? counter-recursive? set-counter-recursive?!) |
| 204 | (data counter-data) |
| 205 | (prev counter-prev)) |
| 206 | |
| 207 | (define (abort-counter c) |
| 208 | ((counter-continuation c))) |
| 209 | |
| 210 | (define (record-effort! c) |
| 211 | (let ((e (effort-counter c))) |
| 212 | (if (zero? (variable-ref e)) |
| 213 | (abort-counter c) |
| 214 | (variable-set! e (1- (variable-ref e)))))) |
| 215 | |
| 216 | (define (record-size! c) |
| 217 | (let ((s (size-counter c))) |
| 218 | (if (zero? (variable-ref s)) |
| 219 | (abort-counter c) |
| 220 | (variable-set! s (1- (variable-ref s)))))) |
| 221 | |
| 222 | (define (find-counter data counter) |
| 223 | (and counter |
| 224 | (if (eq? data (counter-data counter)) |
| 225 | counter |
| 226 | (find-counter data (counter-prev counter))))) |
| 227 | |
| 228 | (define* (transfer! from to #:optional |
| 229 | (effort (variable-ref (effort-counter from))) |
| 230 | (size (variable-ref (size-counter from)))) |
| 231 | (define (transfer-counter! from-v to-v amount) |
| 232 | (let* ((from-balance (variable-ref from-v)) |
| 233 | (to-balance (variable-ref to-v)) |
| 234 | (amount (min amount from-balance))) |
| 235 | (variable-set! from-v (- from-balance amount)) |
| 236 | (variable-set! to-v (+ to-balance amount)))) |
| 237 | |
| 238 | (transfer-counter! (effort-counter from) (effort-counter to) effort) |
| 239 | (transfer-counter! (size-counter from) (size-counter to) size)) |
| 240 | |
| 241 | (define (make-top-counter effort-limit size-limit continuation data) |
| 242 | (%make-counter (make-variable effort-limit) |
| 243 | (make-variable size-limit) |
| 244 | continuation |
| 245 | #t |
| 246 | data |
| 247 | #f)) |
| 248 | |
| 249 | (define (make-nested-counter continuation data current) |
| 250 | (let ((c (%make-counter (make-variable 0) |
| 251 | (make-variable 0) |
| 252 | continuation |
| 253 | #f |
| 254 | data |
| 255 | current))) |
| 256 | (transfer! current c) |
| 257 | c)) |
| 258 | |
| 259 | (define (make-recursive-counter effort-limit size-limit orig current) |
| 260 | (let ((c (%make-counter (make-variable 0) |
| 261 | (make-variable 0) |
| 262 | (counter-continuation orig) |
| 263 | #t |
| 264 | (counter-data orig) |
| 265 | current))) |
| 266 | (transfer! current c effort-limit size-limit) |
| 267 | c)) |
| 268 | |
| 269 | ;; Operand structures allow bindings to be processed lazily instead of |
| 270 | ;; eagerly. By doing so, hopefully we can get process them in a way |
| 271 | ;; appropriate to their use contexts. Operands also prevent values from |
| 272 | ;; being visited multiple times, wasting effort. |
| 273 | ;; |
| 274 | ;; TODO: Record value size in operand structure? |
| 275 | ;; |
| 276 | (define-record-type <operand> |
| 277 | (%make-operand var sym visit source visit-count use-count |
| 278 | copyable? residual-value constant-value alias-value) |
| 279 | operand? |
| 280 | (var operand-var) |
| 281 | (sym operand-sym) |
| 282 | (visit %operand-visit) |
| 283 | (source operand-source) |
| 284 | (visit-count operand-visit-count set-operand-visit-count!) |
| 285 | (use-count operand-use-count set-operand-use-count!) |
| 286 | (copyable? operand-copyable? set-operand-copyable?!) |
| 287 | (residual-value operand-residual-value %set-operand-residual-value!) |
| 288 | (constant-value operand-constant-value set-operand-constant-value!) |
| 289 | (alias-value operand-alias-value set-operand-alias-value!)) |
| 290 | |
| 291 | (define* (make-operand var sym #:optional source visit alias) |
| 292 | ;; Bind SYM to VAR, with value SOURCE. Unassigned bound operands are |
| 293 | ;; considered copyable until we prove otherwise. If we have a source |
| 294 | ;; expression, truncate it to one value. Copy propagation does not |
| 295 | ;; work on multiply-valued expressions. |
| 296 | (let ((source (and=> source truncate-values))) |
| 297 | (%make-operand var sym visit source 0 0 |
| 298 | (and source (not (var-set? var))) #f #f |
| 299 | (and (not (var-set? var)) alias)))) |
| 300 | |
| 301 | (define* (make-bound-operands vars syms sources visit #:optional aliases) |
| 302 | (if aliases |
| 303 | (map (lambda (name sym source alias) |
| 304 | (make-operand name sym source visit alias)) |
| 305 | vars syms sources aliases) |
| 306 | (map (lambda (name sym source) |
| 307 | (make-operand name sym source visit #f)) |
| 308 | vars syms sources))) |
| 309 | |
| 310 | (define (make-unbound-operands vars syms) |
| 311 | (map make-operand vars syms)) |
| 312 | |
| 313 | (define (set-operand-residual-value! op val) |
| 314 | (%set-operand-residual-value! |
| 315 | op |
| 316 | (match val |
| 317 | (($ <primcall> src 'values (first)) |
| 318 | ;; The continuation of a residualized binding does not need the |
| 319 | ;; introduced `values' node, so undo the effects of truncation. |
| 320 | first) |
| 321 | (else |
| 322 | val)))) |
| 323 | |
| 324 | (define* (visit-operand op counter ctx #:optional effort-limit size-limit) |
| 325 | ;; Peval is O(N) in call sites of the source program. However, |
| 326 | ;; visiting an operand can introduce new call sites. If we visit an |
| 327 | ;; operand outside a counter -- i.e., outside an inlining attempt -- |
| 328 | ;; this can lead to divergence. So, if we are visiting an operand to |
| 329 | ;; try to copy it, and there is no counter, make a new one. |
| 330 | ;; |
| 331 | ;; This will only happen at most as many times as there are lexical |
| 332 | ;; references in the source program. |
| 333 | (and (zero? (operand-visit-count op)) |
| 334 | (dynamic-wind |
| 335 | (lambda () |
| 336 | (set-operand-visit-count! op (1+ (operand-visit-count op)))) |
| 337 | (lambda () |
| 338 | (and (operand-source op) |
| 339 | (if (or counter (and (not effort-limit) (not size-limit))) |
| 340 | ((%operand-visit op) (operand-source op) counter ctx) |
| 341 | (let/ec k |
| 342 | (define (abort) |
| 343 | ;; If we abort when visiting the value in a |
| 344 | ;; fresh context, we won't succeed in any future |
| 345 | ;; attempt, so don't try to copy it again. |
| 346 | (set-operand-copyable?! op #f) |
| 347 | (k #f)) |
| 348 | ((%operand-visit op) |
| 349 | (operand-source op) |
| 350 | (make-top-counter effort-limit size-limit abort op) |
| 351 | ctx))))) |
| 352 | (lambda () |
| 353 | (set-operand-visit-count! op (1- (operand-visit-count op))))))) |
| 354 | |
| 355 | ;; A helper for constant folding. |
| 356 | ;; |
| 357 | (define (types-check? primitive-name args) |
| 358 | (case primitive-name |
| 359 | ((values) #t) |
| 360 | ((not pair? null? list? symbol? vector? struct?) |
| 361 | (= (length args) 1)) |
| 362 | ((eq? eqv? equal?) |
| 363 | (= (length args) 2)) |
| 364 | ;; FIXME: add more cases? |
| 365 | (else #f))) |
| 366 | |
| 367 | (define* (peval exp #:optional (cenv (current-module)) (env vlist-null) |
| 368 | #:key |
| 369 | (operator-size-limit 40) |
| 370 | (operand-size-limit 20) |
| 371 | (value-size-limit 10) |
| 372 | (effort-limit 500) |
| 373 | (recursive-effort-limit 100)) |
| 374 | "Partially evaluate EXP in compilation environment CENV, with |
| 375 | top-level bindings from ENV and return the resulting expression." |
| 376 | |
| 377 | ;; This is a simple partial evaluator. It effectively performs |
| 378 | ;; constant folding, copy propagation, dead code elimination, and |
| 379 | ;; inlining. |
| 380 | |
| 381 | ;; TODO: |
| 382 | ;; |
| 383 | ;; Propagate copies across toplevel bindings, if we can prove the |
| 384 | ;; bindings to be immutable. |
| 385 | ;; |
| 386 | ;; Specialize lambda expressions with invariant arguments. |
| 387 | |
| 388 | (define local-toplevel-env |
| 389 | ;; The top-level environment of the module being compiled. |
| 390 | (let () |
| 391 | (define (env-folder x env) |
| 392 | (match x |
| 393 | (($ <toplevel-define> _ name) |
| 394 | (vhash-consq name #t env)) |
| 395 | (($ <seq> _ head tail) |
| 396 | (env-folder tail (env-folder head env))) |
| 397 | (_ env))) |
| 398 | (env-folder exp vlist-null))) |
| 399 | |
| 400 | (define (local-toplevel? name) |
| 401 | (vhash-assq name local-toplevel-env)) |
| 402 | |
| 403 | ;; gensym -> <var> |
| 404 | ;; renamed-term -> original-term |
| 405 | ;; |
| 406 | (define store (build-var-table exp)) |
| 407 | |
| 408 | (define (record-new-temporary! name sym refcount) |
| 409 | (set! store (vhash-consq sym (make-var name sym refcount #f) store))) |
| 410 | |
| 411 | (define (lookup-var sym) |
| 412 | (let ((v (vhash-assq sym store))) |
| 413 | (if v (cdr v) (error "unbound var" sym (vlist->list store))))) |
| 414 | |
| 415 | (define (fresh-gensyms vars) |
| 416 | (map (lambda (var) |
| 417 | (let ((new (gensym (string-append (symbol->string (var-name var)) |
| 418 | " ")))) |
| 419 | (set! store (vhash-consq new var store)) |
| 420 | new)) |
| 421 | vars)) |
| 422 | |
| 423 | (define (fresh-temporaries ls) |
| 424 | (map (lambda (elt) |
| 425 | (let ((new (gensym "tmp "))) |
| 426 | (record-new-temporary! 'tmp new 1) |
| 427 | new)) |
| 428 | ls)) |
| 429 | |
| 430 | (define (assigned-lexical? sym) |
| 431 | (var-set? (lookup-var sym))) |
| 432 | |
| 433 | (define (lexical-refcount sym) |
| 434 | (var-refcount (lookup-var sym))) |
| 435 | |
| 436 | (define (with-temporaries src exps refcount can-copy? k) |
| 437 | (let* ((pairs (map (match-lambda |
| 438 | ((and exp (? can-copy?)) |
| 439 | (cons #f exp)) |
| 440 | (exp |
| 441 | (let ((sym (gensym "tmp "))) |
| 442 | (record-new-temporary! 'tmp sym refcount) |
| 443 | (cons sym exp)))) |
| 444 | exps)) |
| 445 | (tmps (filter car pairs))) |
| 446 | (match tmps |
| 447 | (() (k exps)) |
| 448 | (tmps |
| 449 | (make-let src |
| 450 | (make-list (length tmps) 'tmp) |
| 451 | (map car tmps) |
| 452 | (map cdr tmps) |
| 453 | (k (map (match-lambda |
| 454 | ((#f . val) val) |
| 455 | ((sym . _) |
| 456 | (make-lexical-ref #f 'tmp sym))) |
| 457 | pairs))))))) |
| 458 | |
| 459 | (define (make-begin0 src first second) |
| 460 | (make-let-values |
| 461 | src |
| 462 | first |
| 463 | (let ((vals (gensym "vals "))) |
| 464 | (record-new-temporary! 'vals vals 1) |
| 465 | (make-lambda-case |
| 466 | #f |
| 467 | '() #f 'vals #f '() (list vals) |
| 468 | (make-seq |
| 469 | src |
| 470 | second |
| 471 | (make-primcall #f 'apply |
| 472 | (list |
| 473 | (make-primitive-ref #f 'values) |
| 474 | (make-lexical-ref #f 'vals vals)))) |
| 475 | #f)))) |
| 476 | |
| 477 | ;; ORIG has been alpha-renamed to NEW. Analyze NEW and record a link |
| 478 | ;; from it to ORIG. |
| 479 | ;; |
| 480 | (define (record-source-expression! orig new) |
| 481 | (set! store (vhash-consq new (source-expression orig) store)) |
| 482 | new) |
| 483 | |
| 484 | ;; Find the source expression corresponding to NEW. Used to detect |
| 485 | ;; recursive inlining attempts. |
| 486 | ;; |
| 487 | (define (source-expression new) |
| 488 | (let ((x (vhash-assq new store))) |
| 489 | (if x (cdr x) new))) |
| 490 | |
| 491 | (define (record-operand-use op) |
| 492 | (set-operand-use-count! op (1+ (operand-use-count op)))) |
| 493 | |
| 494 | (define (unrecord-operand-uses op n) |
| 495 | (let ((count (- (operand-use-count op) n))) |
| 496 | (when (zero? count) |
| 497 | (set-operand-residual-value! op #f)) |
| 498 | (set-operand-use-count! op count))) |
| 499 | |
| 500 | (define* (residualize-lexical op #:optional ctx val) |
| 501 | (log 'residualize op) |
| 502 | (record-operand-use op) |
| 503 | (if (memq ctx '(value values)) |
| 504 | (set-operand-residual-value! op val)) |
| 505 | (make-lexical-ref #f (var-name (operand-var op)) (operand-sym op))) |
| 506 | |
| 507 | (define (fold-constants src name args ctx) |
| 508 | (define (apply-primitive name args) |
| 509 | ;; todo: further optimize commutative primitives |
| 510 | (catch #t |
| 511 | (lambda () |
| 512 | (call-with-values |
| 513 | (lambda () |
| 514 | (apply (module-ref the-scm-module name) args)) |
| 515 | (lambda results |
| 516 | (values #t results)))) |
| 517 | (lambda _ |
| 518 | (values #f '())))) |
| 519 | (define (make-values src values) |
| 520 | (match values |
| 521 | ((single) single) ; 1 value |
| 522 | ((_ ...) ; 0, or 2 or more values |
| 523 | (make-primcall src 'values values)))) |
| 524 | (define (residualize-call) |
| 525 | (make-primcall src name args)) |
| 526 | (cond |
| 527 | ((every const? args) |
| 528 | (let-values (((success? values) |
| 529 | (apply-primitive name (map const-exp args)))) |
| 530 | (log 'fold success? values name args) |
| 531 | (if success? |
| 532 | (case ctx |
| 533 | ((effect) (make-void src)) |
| 534 | ((test) |
| 535 | ;; Values truncation: only take the first |
| 536 | ;; value. |
| 537 | (if (pair? values) |
| 538 | (make-const src (car values)) |
| 539 | (make-values src '()))) |
| 540 | (else |
| 541 | (make-values src (map (cut make-const src <>) values)))) |
| 542 | (residualize-call)))) |
| 543 | ((and (eq? ctx 'effect) (types-check? name args)) |
| 544 | (make-void #f)) |
| 545 | (else |
| 546 | (residualize-call)))) |
| 547 | |
| 548 | (define (inline-values src exp nmin nmax consumer) |
| 549 | (let loop ((exp exp)) |
| 550 | (match exp |
| 551 | ;; Some expression types are always singly-valued. |
| 552 | ((or ($ <const>) |
| 553 | ($ <void>) |
| 554 | ($ <lambda>) |
| 555 | ($ <lexical-ref>) |
| 556 | ($ <toplevel-ref>) |
| 557 | ($ <module-ref>) |
| 558 | ($ <primitive-ref>) |
| 559 | ($ <lexical-set>) ; FIXME: these set! expressions |
| 560 | ($ <toplevel-set>) ; could return zero values in |
| 561 | ($ <toplevel-define>) ; the future |
| 562 | ($ <module-set>) ; |
| 563 | ($ <primcall> src (? singly-valued-primitive?))) |
| 564 | (and (<= nmin 1) (or (not nmax) (>= nmax 1)) |
| 565 | (make-call src (make-lambda #f '() consumer) (list exp)))) |
| 566 | |
| 567 | ;; Statically-known number of values. |
| 568 | (($ <primcall> src 'values vals) |
| 569 | (and (<= nmin (length vals)) (or (not nmax) (>= nmax (length vals))) |
| 570 | (make-call src (make-lambda #f '() consumer) vals))) |
| 571 | |
| 572 | ;; Not going to copy code into both branches. |
| 573 | (($ <conditional>) #f) |
| 574 | |
| 575 | ;; Bail on other applications. |
| 576 | (($ <call>) #f) |
| 577 | (($ <primcall>) #f) |
| 578 | |
| 579 | ;; Bail on prompt and abort. |
| 580 | (($ <prompt>) #f) |
| 581 | (($ <abort>) #f) |
| 582 | |
| 583 | ;; Propagate to tail positions. |
| 584 | (($ <let> src names gensyms vals body) |
| 585 | (let ((body (loop body))) |
| 586 | (and body |
| 587 | (make-let src names gensyms vals body)))) |
| 588 | (($ <letrec> src in-order? names gensyms vals body) |
| 589 | (let ((body (loop body))) |
| 590 | (and body |
| 591 | (make-letrec src in-order? names gensyms vals body)))) |
| 592 | (($ <fix> src names gensyms vals body) |
| 593 | (let ((body (loop body))) |
| 594 | (and body |
| 595 | (make-fix src names gensyms vals body)))) |
| 596 | (($ <let-values> src exp |
| 597 | ($ <lambda-case> src2 req opt rest kw inits gensyms body #f)) |
| 598 | (let ((body (loop body))) |
| 599 | (and body |
| 600 | (make-let-values src exp |
| 601 | (make-lambda-case src2 req opt rest kw |
| 602 | inits gensyms body #f))))) |
| 603 | (($ <seq> src head tail) |
| 604 | (let ((tail (loop tail))) |
| 605 | (and tail (make-seq src head tail))))))) |
| 606 | |
| 607 | (define compute-effects |
| 608 | (make-effects-analyzer assigned-lexical?)) |
| 609 | |
| 610 | (define (constant-expression? x) |
| 611 | ;; Return true if X is constant, for the purposes of copying or |
| 612 | ;; elision---i.e., if it is known to have no effects, does not |
| 613 | ;; allocate storage for a mutable object, and does not access |
| 614 | ;; mutable data (like `car' or toplevel references). |
| 615 | (constant? (compute-effects x))) |
| 616 | |
| 617 | (define (prune-bindings ops in-order? body counter ctx build-result) |
| 618 | ;; This helper handles both `let' and `letrec'/`fix'. In the latter |
| 619 | ;; cases we need to make sure that if referenced binding A needs |
| 620 | ;; as-yet-unreferenced binding B, that B is processed for value. |
| 621 | ;; Likewise if C, when processed for effect, needs otherwise |
| 622 | ;; unreferenced D, then D needs to be processed for value too. |
| 623 | ;; |
| 624 | (define (referenced? op) |
| 625 | ;; When we visit lambdas in operator context, we just copy them, |
| 626 | ;; as we will process their body later. However this does have |
| 627 | ;; the problem that any free var referenced by the lambda is not |
| 628 | ;; marked as needing residualization. Here we hack around this |
| 629 | ;; and treat all bindings as referenced if we are in operator |
| 630 | ;; context. |
| 631 | (or (eq? ctx 'operator) |
| 632 | (not (zero? (operand-use-count op))))) |
| 633 | |
| 634 | ;; values := (op ...) |
| 635 | ;; effects := (op ...) |
| 636 | (define (residualize values effects) |
| 637 | ;; Note, values and effects are reversed. |
| 638 | (cond |
| 639 | (in-order? |
| 640 | (let ((values (filter operand-residual-value ops))) |
| 641 | (if (null? values) |
| 642 | body |
| 643 | (build-result (map (compose var-name operand-var) values) |
| 644 | (map operand-sym values) |
| 645 | (map operand-residual-value values) |
| 646 | body)))) |
| 647 | (else |
| 648 | (let ((body |
| 649 | (if (null? effects) |
| 650 | body |
| 651 | (let ((effect-vals (map operand-residual-value effects))) |
| 652 | (list->seq #f (reverse (cons body effect-vals))))))) |
| 653 | (if (null? values) |
| 654 | body |
| 655 | (let ((values (reverse values))) |
| 656 | (build-result (map (compose var-name operand-var) values) |
| 657 | (map operand-sym values) |
| 658 | (map operand-residual-value values) |
| 659 | body))))))) |
| 660 | |
| 661 | ;; old := (bool ...) |
| 662 | ;; values := (op ...) |
| 663 | ;; effects := ((op . value) ...) |
| 664 | (let prune ((old (map referenced? ops)) (values '()) (effects '())) |
| 665 | (let lp ((ops* ops) (values values) (effects effects)) |
| 666 | (cond |
| 667 | ((null? ops*) |
| 668 | (let ((new (map referenced? ops))) |
| 669 | (if (not (equal? new old)) |
| 670 | (prune new values '()) |
| 671 | (residualize values |
| 672 | (map (lambda (op val) |
| 673 | (set-operand-residual-value! op val) |
| 674 | op) |
| 675 | (map car effects) (map cdr effects)))))) |
| 676 | (else |
| 677 | (let ((op (car ops*))) |
| 678 | (cond |
| 679 | ((memq op values) |
| 680 | (lp (cdr ops*) values effects)) |
| 681 | ((operand-residual-value op) |
| 682 | (lp (cdr ops*) (cons op values) effects)) |
| 683 | ((referenced? op) |
| 684 | (set-operand-residual-value! op (visit-operand op counter 'value)) |
| 685 | (lp (cdr ops*) (cons op values) effects)) |
| 686 | (else |
| 687 | (lp (cdr ops*) |
| 688 | values |
| 689 | (let ((effect (visit-operand op counter 'effect))) |
| 690 | (if (void? effect) |
| 691 | effects |
| 692 | (acons op effect effects)))))))))))) |
| 693 | |
| 694 | (define (small-expression? x limit) |
| 695 | (let/ec k |
| 696 | (tree-il-fold |
| 697 | (lambda (x res) ; down |
| 698 | (1+ res)) |
| 699 | (lambda (x res) ; up |
| 700 | (if (< res limit) |
| 701 | res |
| 702 | (k #f))) |
| 703 | 0 x) |
| 704 | #t)) |
| 705 | |
| 706 | (define (extend-env sym op env) |
| 707 | (vhash-consq (operand-sym op) op (vhash-consq sym op env))) |
| 708 | |
| 709 | (let loop ((exp exp) |
| 710 | (env vlist-null) ; vhash of gensym -> <operand> |
| 711 | (counter #f) ; inlined call stack |
| 712 | (ctx 'values)) ; effect, value, values, test, operator, or call |
| 713 | (define (lookup var) |
| 714 | (cond |
| 715 | ((vhash-assq var env) => cdr) |
| 716 | (else (error "unbound var" var)))) |
| 717 | |
| 718 | ;; Find a value referenced a specific number of times. This is a hack |
| 719 | ;; that's used for propagating fresh data structures like rest lists and |
| 720 | ;; prompt tags. Usually we wouldn't copy consed data, but we can do so in |
| 721 | ;; some special cases like `apply' or prompts if we can account |
| 722 | ;; for all of its uses. |
| 723 | ;; |
| 724 | ;; You don't want to use this in general because it introduces a slight |
| 725 | ;; nonlinearity by running peval again (though with a small effort and size |
| 726 | ;; counter). |
| 727 | ;; |
| 728 | (define (find-definition x n-aliases) |
| 729 | (cond |
| 730 | ((lexical-ref? x) |
| 731 | (cond |
| 732 | ((lookup (lexical-ref-gensym x)) |
| 733 | => (lambda (op) |
| 734 | (if (var-set? (operand-var op)) |
| 735 | (values #f #f) |
| 736 | (let ((y (or (operand-residual-value op) |
| 737 | (visit-operand op counter 'value 10 10) |
| 738 | (operand-source op)))) |
| 739 | (cond |
| 740 | ((and (lexical-ref? y) |
| 741 | (= (lexical-refcount (lexical-ref-gensym x)) 1)) |
| 742 | ;; X is a simple alias for Y. Recurse, regardless of |
| 743 | ;; the number of aliases we were expecting. |
| 744 | (find-definition y n-aliases)) |
| 745 | ((= (lexical-refcount (lexical-ref-gensym x)) n-aliases) |
| 746 | ;; We found a definition that is aliased the right |
| 747 | ;; number of times. We still recurse in case it is a |
| 748 | ;; lexical. |
| 749 | (values (find-definition y 1) |
| 750 | op)) |
| 751 | (else |
| 752 | ;; We can't account for our aliases. |
| 753 | (values #f #f))))))) |
| 754 | (else |
| 755 | ;; A formal parameter. Can't say anything about that. |
| 756 | (values #f #f)))) |
| 757 | ((= n-aliases 1) |
| 758 | ;; Not a lexical: success, but only if we are looking for an |
| 759 | ;; unaliased value. |
| 760 | (values x #f)) |
| 761 | (else (values #f #f)))) |
| 762 | |
| 763 | (define (visit exp ctx) |
| 764 | (loop exp env counter ctx)) |
| 765 | |
| 766 | (define (for-value exp) (visit exp 'value)) |
| 767 | (define (for-values exp) (visit exp 'values)) |
| 768 | (define (for-test exp) (visit exp 'test)) |
| 769 | (define (for-effect exp) (visit exp 'effect)) |
| 770 | (define (for-call exp) (visit exp 'call)) |
| 771 | (define (for-tail exp) (visit exp ctx)) |
| 772 | |
| 773 | (if counter |
| 774 | (record-effort! counter)) |
| 775 | |
| 776 | (log 'visit ctx (and=> counter effort-counter) |
| 777 | (unparse-tree-il exp)) |
| 778 | |
| 779 | (match exp |
| 780 | (($ <const>) |
| 781 | (case ctx |
| 782 | ((effect) (make-void #f)) |
| 783 | (else exp))) |
| 784 | (($ <void>) |
| 785 | (case ctx |
| 786 | ((test) (make-const #f #t)) |
| 787 | (else exp))) |
| 788 | (($ <lexical-ref> _ _ gensym) |
| 789 | (log 'begin-copy gensym) |
| 790 | (let ((op (lookup gensym))) |
| 791 | (cond |
| 792 | ((eq? ctx 'effect) |
| 793 | (log 'lexical-for-effect gensym) |
| 794 | (make-void #f)) |
| 795 | ((operand-alias-value op) |
| 796 | ;; This is an unassigned operand that simply aliases some |
| 797 | ;; other operand. Recurse to avoid residualizing the leaf |
| 798 | ;; binding. |
| 799 | => for-tail) |
| 800 | ((eq? ctx 'call) |
| 801 | ;; Don't propagate copies if we are residualizing a call. |
| 802 | (log 'residualize-lexical-call gensym op) |
| 803 | (residualize-lexical op)) |
| 804 | ((var-set? (operand-var op)) |
| 805 | ;; Assigned lexicals don't copy-propagate. |
| 806 | (log 'assigned-var gensym op) |
| 807 | (residualize-lexical op)) |
| 808 | ((not (operand-copyable? op)) |
| 809 | ;; We already know that this operand is not copyable. |
| 810 | (log 'not-copyable gensym op) |
| 811 | (residualize-lexical op)) |
| 812 | ((and=> (operand-constant-value op) |
| 813 | (lambda (x) (or (const? x) (void? x) (primitive-ref? x)))) |
| 814 | ;; A cache hit. |
| 815 | (let ((val (operand-constant-value op))) |
| 816 | (log 'memoized-constant gensym val) |
| 817 | (for-tail val))) |
| 818 | ((visit-operand op counter (if (eq? ctx 'values) 'value ctx) |
| 819 | recursive-effort-limit operand-size-limit) |
| 820 | => |
| 821 | ;; If we end up deciding to residualize this value instead of |
| 822 | ;; copying it, save that residualized value. |
| 823 | (lambda (val) |
| 824 | (cond |
| 825 | ((not (constant-expression? val)) |
| 826 | (log 'not-constant gensym op) |
| 827 | ;; At this point, ctx is operator, test, or value. A |
| 828 | ;; value that is non-constant in one context will be |
| 829 | ;; non-constant in the others, so it's safe to record |
| 830 | ;; that here, and avoid future visits. |
| 831 | (set-operand-copyable?! op #f) |
| 832 | (residualize-lexical op ctx val)) |
| 833 | ((or (const? val) |
| 834 | (void? val) |
| 835 | (primitive-ref? val)) |
| 836 | ;; Always propagate simple values that cannot lead to |
| 837 | ;; code bloat. |
| 838 | (log 'copy-simple gensym val) |
| 839 | ;; It could be this constant is the result of folding. |
| 840 | ;; If that is the case, cache it. This helps loop |
| 841 | ;; unrolling get farther. |
| 842 | (if (or (eq? ctx 'value) (eq? ctx 'values)) |
| 843 | (begin |
| 844 | (log 'memoize-constant gensym val) |
| 845 | (set-operand-constant-value! op val))) |
| 846 | val) |
| 847 | ((= 1 (var-refcount (operand-var op))) |
| 848 | ;; Always propagate values referenced only once. |
| 849 | (log 'copy-single gensym val) |
| 850 | val) |
| 851 | ;; FIXME: do demand-driven size accounting rather than |
| 852 | ;; these heuristics. |
| 853 | ((eq? ctx 'operator) |
| 854 | ;; A pure expression in the operator position. Inline |
| 855 | ;; if it's a lambda that's small enough. |
| 856 | (if (and (lambda? val) |
| 857 | (small-expression? val operator-size-limit)) |
| 858 | (begin |
| 859 | (log 'copy-operator gensym val) |
| 860 | val) |
| 861 | (begin |
| 862 | (log 'too-big-for-operator gensym val) |
| 863 | (residualize-lexical op ctx val)))) |
| 864 | (else |
| 865 | ;; A pure expression, processed for call or for value. |
| 866 | ;; Don't inline lambdas, because they will probably won't |
| 867 | ;; fold because we don't know the operator. |
| 868 | (if (and (small-expression? val value-size-limit) |
| 869 | (not (tree-il-any lambda? val))) |
| 870 | (begin |
| 871 | (log 'copy-value gensym val) |
| 872 | val) |
| 873 | (begin |
| 874 | (log 'too-big-or-has-lambda gensym val) |
| 875 | (residualize-lexical op ctx val))))))) |
| 876 | (else |
| 877 | ;; Visit failed. Either the operand isn't bound, as in |
| 878 | ;; lambda formal parameters, or the copy was aborted. |
| 879 | (log 'unbound-or-aborted gensym op) |
| 880 | (residualize-lexical op))))) |
| 881 | (($ <lexical-set> src name gensym exp) |
| 882 | (let ((op (lookup gensym))) |
| 883 | (if (zero? (var-refcount (operand-var op))) |
| 884 | (let ((exp (for-effect exp))) |
| 885 | (if (void? exp) |
| 886 | exp |
| 887 | (make-seq src exp (make-void #f)))) |
| 888 | (begin |
| 889 | (record-operand-use op) |
| 890 | (make-lexical-set src name (operand-sym op) (for-value exp)))))) |
| 891 | (($ <let> src |
| 892 | (names ... rest) |
| 893 | (gensyms ... rest-sym) |
| 894 | (vals ... ($ <primcall> _ 'list rest-args)) |
| 895 | ($ <primcall> asrc 'apply |
| 896 | (proc args ... |
| 897 | ($ <lexical-ref> _ |
| 898 | (? (cut eq? <> rest)) |
| 899 | (? (lambda (sym) |
| 900 | (and (eq? sym rest-sym) |
| 901 | (= (lexical-refcount sym) 1)))))))) |
| 902 | (let* ((tmps (make-list (length rest-args) 'tmp)) |
| 903 | (tmp-syms (fresh-temporaries tmps))) |
| 904 | (for-tail |
| 905 | (make-let src |
| 906 | (append names tmps) |
| 907 | (append gensyms tmp-syms) |
| 908 | (append vals rest-args) |
| 909 | (make-call |
| 910 | asrc |
| 911 | proc |
| 912 | (append args |
| 913 | (map (cut make-lexical-ref #f <> <>) |
| 914 | tmps tmp-syms))))))) |
| 915 | (($ <let> src names gensyms vals body) |
| 916 | (define (compute-alias exp) |
| 917 | ;; It's very common for macros to introduce something like: |
| 918 | ;; |
| 919 | ;; ((lambda (x y) ...) x-exp y-exp) |
| 920 | ;; |
| 921 | ;; In that case you might end up trying to inline something like: |
| 922 | ;; |
| 923 | ;; (let ((x x-exp) (y y-exp)) ...) |
| 924 | ;; |
| 925 | ;; But if x-exp is itself a lexical-ref that aliases some much |
| 926 | ;; larger expression, perhaps it will fail to inline due to |
| 927 | ;; size. However we don't want to introduce a useless alias |
| 928 | ;; (in this case, x). So if the RHS of a let expression is a |
| 929 | ;; lexical-ref, we record that expression. If we end up having |
| 930 | ;; to residualize X, then instead we residualize X-EXP, as long |
| 931 | ;; as it isn't assigned. |
| 932 | ;; |
| 933 | (match exp |
| 934 | (($ <lexical-ref> _ _ sym) |
| 935 | (let ((op (lookup sym))) |
| 936 | (and (not (var-set? (operand-var op))) |
| 937 | (or (operand-alias-value op) |
| 938 | exp)))) |
| 939 | (_ #f))) |
| 940 | |
| 941 | (let* ((vars (map lookup-var gensyms)) |
| 942 | (new (fresh-gensyms vars)) |
| 943 | (ops (make-bound-operands vars new vals |
| 944 | (lambda (exp counter ctx) |
| 945 | (loop exp env counter ctx)) |
| 946 | (map compute-alias vals))) |
| 947 | (env (fold extend-env env gensyms ops)) |
| 948 | (body (loop body env counter ctx))) |
| 949 | (cond |
| 950 | ((const? body) |
| 951 | (for-tail (list->seq src (append vals (list body))))) |
| 952 | ((and (lexical-ref? body) |
| 953 | (memq (lexical-ref-gensym body) new)) |
| 954 | (let ((sym (lexical-ref-gensym body)) |
| 955 | (pairs (map cons new vals))) |
| 956 | ;; (let ((x foo) (y bar) ...) x) => (begin bar ... foo) |
| 957 | (for-tail |
| 958 | (list->seq |
| 959 | src |
| 960 | (append (map cdr (alist-delete sym pairs eq?)) |
| 961 | (list (assq-ref pairs sym))))))) |
| 962 | (else |
| 963 | ;; Only include bindings for which lexical references |
| 964 | ;; have been residualized. |
| 965 | (prune-bindings ops #f body counter ctx |
| 966 | (lambda (names gensyms vals body) |
| 967 | (if (null? names) (error "what!" names)) |
| 968 | (make-let src names gensyms vals body))))))) |
| 969 | (($ <letrec> src in-order? names gensyms vals body) |
| 970 | ;; Note the difference from the `let' case: here we use letrec* |
| 971 | ;; so that the `visit' procedure for the new operands closes over |
| 972 | ;; an environment that includes the operands. Also we don't try |
| 973 | ;; to elide aliases, because we can't sensibly reduce something |
| 974 | ;; like (letrec ((a b) (b a)) a). |
| 975 | (letrec* ((visit (lambda (exp counter ctx) |
| 976 | (loop exp env* counter ctx))) |
| 977 | (vars (map lookup-var gensyms)) |
| 978 | (new (fresh-gensyms vars)) |
| 979 | (ops (make-bound-operands vars new vals visit)) |
| 980 | (env* (fold extend-env env gensyms ops)) |
| 981 | (body* (visit body counter ctx))) |
| 982 | (if (and (const? body*) (every constant-expression? vals)) |
| 983 | ;; We may have folded a loop completely, even though there |
| 984 | ;; might be cyclical references between the bound values. |
| 985 | ;; Handle this degenerate case specially. |
| 986 | body* |
| 987 | (prune-bindings ops in-order? body* counter ctx |
| 988 | (lambda (names gensyms vals body) |
| 989 | (make-letrec src in-order? |
| 990 | names gensyms vals body)))))) |
| 991 | (($ <fix> src names gensyms vals body) |
| 992 | (letrec* ((visit (lambda (exp counter ctx) |
| 993 | (loop exp env* counter ctx))) |
| 994 | (vars (map lookup-var gensyms)) |
| 995 | (new (fresh-gensyms vars)) |
| 996 | (ops (make-bound-operands vars new vals visit)) |
| 997 | (env* (fold extend-env env gensyms ops)) |
| 998 | (body* (visit body counter ctx))) |
| 999 | (if (const? body*) |
| 1000 | body* |
| 1001 | (prune-bindings ops #f body* counter ctx |
| 1002 | (lambda (names gensyms vals body) |
| 1003 | (make-fix src names gensyms vals body)))))) |
| 1004 | (($ <let-values> lv-src producer consumer) |
| 1005 | ;; Peval the producer, then try to inline the consumer into |
| 1006 | ;; the producer. If that succeeds, peval again. Otherwise |
| 1007 | ;; reconstruct the let-values, pevaling the consumer. |
| 1008 | (let ((producer (for-values producer))) |
| 1009 | (or (match consumer |
| 1010 | (($ <lambda-case> src (req-name) #f #f #f () (req-sym) body #f) |
| 1011 | (for-tail |
| 1012 | (make-let src (list req-name) (list req-sym) (list producer) |
| 1013 | body))) |
| 1014 | ((and ($ <lambda-case> src () #f rest #f () (rest-sym) body #f) |
| 1015 | (? (lambda _ (singly-valued-expression? producer)))) |
| 1016 | (let ((tmp (gensym "tmp "))) |
| 1017 | (record-new-temporary! 'tmp tmp 1) |
| 1018 | (for-tail |
| 1019 | (make-let |
| 1020 | src (list 'tmp) (list tmp) (list producer) |
| 1021 | (make-let |
| 1022 | src (list rest) (list rest-sym) |
| 1023 | (list |
| 1024 | (make-primcall #f 'list |
| 1025 | (list (make-lexical-ref #f 'tmp tmp)))) |
| 1026 | body))))) |
| 1027 | (($ <lambda-case> src req opt rest #f inits gensyms body #f) |
| 1028 | (let* ((nmin (length req)) |
| 1029 | (nmax (and (not rest) (+ nmin (if opt (length opt) 0))))) |
| 1030 | (cond |
| 1031 | ((inline-values lv-src producer nmin nmax consumer) |
| 1032 | => for-tail) |
| 1033 | (else #f)))) |
| 1034 | (_ #f)) |
| 1035 | (make-let-values lv-src producer (for-tail consumer))))) |
| 1036 | (($ <toplevel-ref> src (? effect-free-primitive? name)) |
| 1037 | exp) |
| 1038 | (($ <toplevel-ref>) |
| 1039 | ;; todo: open private local bindings. |
| 1040 | exp) |
| 1041 | (($ <module-ref> src module (? effect-free-primitive? name) #f) |
| 1042 | (let ((module (false-if-exception |
| 1043 | (resolve-module module #:ensure #f)))) |
| 1044 | (if (module? module) |
| 1045 | (let ((var (module-variable module name))) |
| 1046 | (if (eq? var (module-variable the-scm-module name)) |
| 1047 | (make-primitive-ref src name) |
| 1048 | exp)) |
| 1049 | exp))) |
| 1050 | (($ <module-ref>) |
| 1051 | exp) |
| 1052 | (($ <module-set> src mod name public? exp) |
| 1053 | (make-module-set src mod name public? (for-value exp))) |
| 1054 | (($ <toplevel-define> src name exp) |
| 1055 | (make-toplevel-define src name (for-value exp))) |
| 1056 | (($ <toplevel-set> src name exp) |
| 1057 | (make-toplevel-set src name (for-value exp))) |
| 1058 | (($ <primitive-ref>) |
| 1059 | (case ctx |
| 1060 | ((effect) (make-void #f)) |
| 1061 | ((test) (make-const #f #t)) |
| 1062 | (else exp))) |
| 1063 | (($ <conditional> src condition subsequent alternate) |
| 1064 | (define (call-with-failure-thunk exp proc) |
| 1065 | (match exp |
| 1066 | (($ <call> _ _ ()) (proc exp)) |
| 1067 | (($ <primcall> _ _ ()) (proc exp)) |
| 1068 | (($ <const>) (proc exp)) |
| 1069 | (($ <void>) (proc exp)) |
| 1070 | (($ <lexical-ref>) (proc exp)) |
| 1071 | (_ |
| 1072 | (let ((t (gensym "failure-"))) |
| 1073 | (record-new-temporary! 'failure t 2) |
| 1074 | (make-let |
| 1075 | src (list 'failure) (list t) |
| 1076 | (list |
| 1077 | (make-lambda |
| 1078 | #f '() |
| 1079 | (make-lambda-case #f '() #f #f #f '() '() exp #f))) |
| 1080 | (proc (make-call #f (make-lexical-ref #f 'failure t) |
| 1081 | '()))))))) |
| 1082 | (define (simplify-conditional c) |
| 1083 | (match c |
| 1084 | ;; Swap the arms of (if (not FOO) A B), to simplify. |
| 1085 | (($ <conditional> src ($ <primcall> _ 'not (pred)) |
| 1086 | subsequent alternate) |
| 1087 | (simplify-conditional |
| 1088 | (make-conditional src pred alternate subsequent))) |
| 1089 | ;; Special cases for common tests in the predicates of chains |
| 1090 | ;; of if expressions. |
| 1091 | (($ <conditional> src |
| 1092 | ($ <conditional> src* outer-test inner-test ($ <const> _ #f)) |
| 1093 | inner-subsequent |
| 1094 | alternate) |
| 1095 | (let lp ((alternate alternate)) |
| 1096 | (match alternate |
| 1097 | ;; Lift a common repeated test out of a chain of if |
| 1098 | ;; expressions. |
| 1099 | (($ <conditional> _ (? (cut tree-il=? outer-test <>)) |
| 1100 | other-subsequent alternate) |
| 1101 | (make-conditional |
| 1102 | src outer-test |
| 1103 | (simplify-conditional |
| 1104 | (make-conditional src* inner-test inner-subsequent |
| 1105 | other-subsequent)) |
| 1106 | alternate)) |
| 1107 | ;; Likewise, but punching through any surrounding |
| 1108 | ;; failure continuations. |
| 1109 | (($ <let> let-src (name) (sym) ((and thunk ($ <lambda>))) body) |
| 1110 | (make-let |
| 1111 | let-src (list name) (list sym) (list thunk) |
| 1112 | (lp body))) |
| 1113 | ;; Otherwise, rotate AND tests to expose a simple |
| 1114 | ;; condition in the front. Although this may result in |
| 1115 | ;; lexically binding failure thunks, the thunks will be |
| 1116 | ;; compiled to labels allocation, so there's no actual |
| 1117 | ;; code growth. |
| 1118 | (_ |
| 1119 | (call-with-failure-thunk |
| 1120 | alternate |
| 1121 | (lambda (failure) |
| 1122 | (make-conditional |
| 1123 | src outer-test |
| 1124 | (simplify-conditional |
| 1125 | (make-conditional src* inner-test inner-subsequent failure)) |
| 1126 | failure))))))) |
| 1127 | (_ c))) |
| 1128 | (match (for-test condition) |
| 1129 | (($ <const> _ val) |
| 1130 | (if val |
| 1131 | (for-tail subsequent) |
| 1132 | (for-tail alternate))) |
| 1133 | (c |
| 1134 | (simplify-conditional |
| 1135 | (make-conditional src c (for-tail subsequent) |
| 1136 | (for-tail alternate)))))) |
| 1137 | (($ <primcall> src 'call-with-values |
| 1138 | (producer |
| 1139 | ($ <lambda> _ _ |
| 1140 | (and consumer |
| 1141 | ;; No optional or kwargs. |
| 1142 | ($ <lambda-case> |
| 1143 | _ req #f rest #f () gensyms body #f))))) |
| 1144 | (for-tail (make-let-values src (make-call src producer '()) |
| 1145 | consumer))) |
| 1146 | (($ <primcall> src 'dynamic-wind (w thunk u)) |
| 1147 | (for-tail |
| 1148 | (with-temporaries |
| 1149 | src (list w u) 2 constant-expression? |
| 1150 | (match-lambda |
| 1151 | ((w u) |
| 1152 | (make-seq |
| 1153 | src |
| 1154 | (make-seq |
| 1155 | src |
| 1156 | (make-conditional |
| 1157 | src |
| 1158 | ;; fixme: introduce logic to fold thunk? |
| 1159 | (make-primcall src 'thunk? (list u)) |
| 1160 | (make-call src w '()) |
| 1161 | (make-primcall |
| 1162 | src 'scm-error |
| 1163 | (list |
| 1164 | (make-const #f 'wrong-type-arg) |
| 1165 | (make-const #f "dynamic-wind") |
| 1166 | (make-const #f "Wrong type (expecting thunk): ~S") |
| 1167 | (make-primcall #f 'list (list u)) |
| 1168 | (make-primcall #f 'list (list u))))) |
| 1169 | (make-primcall src 'wind (list w u))) |
| 1170 | (make-begin0 src |
| 1171 | (make-call src thunk '()) |
| 1172 | (make-seq src |
| 1173 | (make-primcall src 'unwind '()) |
| 1174 | (make-call src u '()))))))))) |
| 1175 | |
| 1176 | (($ <primcall> src 'with-fluid* (f v thunk)) |
| 1177 | (for-tail |
| 1178 | (with-temporaries |
| 1179 | src (list f v thunk) 1 constant-expression? |
| 1180 | (match-lambda |
| 1181 | ((f v thunk) |
| 1182 | (make-seq src |
| 1183 | (make-primcall src 'push-fluid (list f v)) |
| 1184 | (make-begin0 src |
| 1185 | (make-call src thunk '()) |
| 1186 | (make-primcall src 'pop-fluid '())))))))) |
| 1187 | |
| 1188 | (($ <primcall> src 'values exps) |
| 1189 | (cond |
| 1190 | ((null? exps) |
| 1191 | (if (eq? ctx 'effect) |
| 1192 | (make-void #f) |
| 1193 | exp)) |
| 1194 | (else |
| 1195 | (let ((vals (map for-value exps))) |
| 1196 | (if (and (case ctx |
| 1197 | ((value test effect) #t) |
| 1198 | (else (null? (cdr vals)))) |
| 1199 | (every singly-valued-expression? vals)) |
| 1200 | (for-tail (list->seq src (append (cdr vals) (list (car vals))))) |
| 1201 | (make-primcall src 'values vals)))))) |
| 1202 | |
| 1203 | (($ <primcall> src 'apply (proc args ... tail)) |
| 1204 | (let lp ((tail* (find-definition tail 1)) (speculative? #t)) |
| 1205 | (define (copyable? x) |
| 1206 | ;; Inlining a result from find-definition effectively copies it, |
| 1207 | ;; relying on the let-pruning to remove its original binding. We |
| 1208 | ;; shouldn't copy non-constant expressions. |
| 1209 | (or (not speculative?) (constant-expression? x))) |
| 1210 | (match tail* |
| 1211 | (($ <const> _ (args* ...)) |
| 1212 | (let ((args* (map (cut make-const #f <>) args*))) |
| 1213 | (for-tail (make-call src proc (append args args*))))) |
| 1214 | (($ <primcall> _ 'cons |
| 1215 | ((and head (? copyable?)) (and tail (? copyable?)))) |
| 1216 | (for-tail (make-primcall src 'apply |
| 1217 | (cons proc |
| 1218 | (append args (list head tail)))))) |
| 1219 | (($ <primcall> _ 'list |
| 1220 | (and args* ((? copyable?) ...))) |
| 1221 | (for-tail (make-call src proc (append args args*)))) |
| 1222 | (tail* |
| 1223 | (if speculative? |
| 1224 | (lp (for-value tail) #f) |
| 1225 | (let ((args (append (map for-value args) (list tail*)))) |
| 1226 | (make-primcall src 'apply |
| 1227 | (cons (for-value proc) args)))))))) |
| 1228 | |
| 1229 | (($ <primcall> src (? constructor-primitive? name) args) |
| 1230 | (cond |
| 1231 | ((and (memq ctx '(effect test)) |
| 1232 | (match (cons name args) |
| 1233 | ((or ('cons _ _) |
| 1234 | ('list . _) |
| 1235 | ('vector . _) |
| 1236 | ('make-prompt-tag) |
| 1237 | ('make-prompt-tag ($ <const> _ (? string?)))) |
| 1238 | #t) |
| 1239 | (_ #f))) |
| 1240 | ;; Some expressions can be folded without visiting the |
| 1241 | ;; arguments for value. |
| 1242 | (let ((res (if (eq? ctx 'effect) |
| 1243 | (make-void #f) |
| 1244 | (make-const #f #t)))) |
| 1245 | (for-tail (list->seq src (append args (list res)))))) |
| 1246 | (else |
| 1247 | (match (cons name (map for-value args)) |
| 1248 | (('cons x ($ <const> _ (? (cut eq? <> '())))) |
| 1249 | (make-primcall src 'list (list x))) |
| 1250 | (('cons x ($ <primcall> _ 'list elts)) |
| 1251 | (make-primcall src 'list (cons x elts))) |
| 1252 | (('list) |
| 1253 | (make-const src '())) |
| 1254 | (('vector) |
| 1255 | (make-const src '#())) |
| 1256 | ((name . args) |
| 1257 | (make-primcall src name args)))))) |
| 1258 | |
| 1259 | (($ <primcall> src 'thunk? (proc)) |
| 1260 | (case ctx |
| 1261 | ((effect) |
| 1262 | (for-tail (make-seq src proc (make-void src)))) |
| 1263 | (else |
| 1264 | (match (for-value proc) |
| 1265 | (($ <lambda> _ _ ($ <lambda-case> _ req)) |
| 1266 | (for-tail (make-const src (null? req)))) |
| 1267 | (proc |
| 1268 | (match (find-definition proc 2) |
| 1269 | (($ <lambda> _ _ ($ <lambda-case> _ req)) |
| 1270 | (for-tail (make-const src (null? req)))) |
| 1271 | (_ |
| 1272 | (make-primcall src 'thunk? (list proc))))))))) |
| 1273 | |
| 1274 | (($ <primcall> src name args) |
| 1275 | (match (cons name (map for-value args)) |
| 1276 | ;; FIXME: these for-tail recursions could take place outside |
| 1277 | ;; an effort counter. |
| 1278 | (('car ($ <primcall> src 'cons (head tail))) |
| 1279 | (for-tail (make-seq src tail head))) |
| 1280 | (('cdr ($ <primcall> src 'cons (head tail))) |
| 1281 | (for-tail (make-seq src head tail))) |
| 1282 | (('car ($ <primcall> src 'list (head . tail))) |
| 1283 | (for-tail (list->seq src (append tail (list head))))) |
| 1284 | (('cdr ($ <primcall> src 'list (head . tail))) |
| 1285 | (for-tail (make-seq src head (make-primcall #f 'list tail)))) |
| 1286 | |
| 1287 | (('car ($ <const> src (head . tail))) |
| 1288 | (for-tail (make-const src head))) |
| 1289 | (('cdr ($ <const> src (head . tail))) |
| 1290 | (for-tail (make-const src tail))) |
| 1291 | (((or 'memq 'memv) k ($ <const> _ (elts ...))) |
| 1292 | ;; FIXME: factor |
| 1293 | (case ctx |
| 1294 | ((effect) |
| 1295 | (for-tail |
| 1296 | (make-seq src k (make-void #f)))) |
| 1297 | ((test) |
| 1298 | (cond |
| 1299 | ((const? k) |
| 1300 | ;; A shortcut. The `else' case would handle it, but |
| 1301 | ;; this way is faster. |
| 1302 | (let ((member (case name ((memq) memq) ((memv) memv)))) |
| 1303 | (make-const #f (and (member (const-exp k) elts) #t)))) |
| 1304 | ((null? elts) |
| 1305 | (for-tail |
| 1306 | (make-seq src k (make-const #f #f)))) |
| 1307 | (else |
| 1308 | (let ((t (gensym "t ")) |
| 1309 | (eq (if (eq? name 'memq) 'eq? 'eqv?))) |
| 1310 | (record-new-temporary! 't t (length elts)) |
| 1311 | (for-tail |
| 1312 | (make-let |
| 1313 | src (list 't) (list t) (list k) |
| 1314 | (let lp ((elts elts)) |
| 1315 | (define test |
| 1316 | (make-primcall #f eq |
| 1317 | (list (make-lexical-ref #f 't t) |
| 1318 | (make-const #f (car elts))))) |
| 1319 | (if (null? (cdr elts)) |
| 1320 | test |
| 1321 | (make-conditional src test |
| 1322 | (make-const #f #t) |
| 1323 | (lp (cdr elts))))))))))) |
| 1324 | (else |
| 1325 | (cond |
| 1326 | ((const? k) |
| 1327 | (let ((member (case name ((memq) memq) ((memv) memv)))) |
| 1328 | (make-const #f (member (const-exp k) elts)))) |
| 1329 | ((null? elts) |
| 1330 | (for-tail (make-seq src k (make-const #f #f)))) |
| 1331 | (else |
| 1332 | (make-primcall src name (list k (make-const #f elts)))))))) |
| 1333 | (((? equality-primitive?) |
| 1334 | ($ <lexical-ref> _ _ sym) ($ <lexical-ref> _ _ sym)) |
| 1335 | (for-tail (make-const #f #t))) |
| 1336 | |
| 1337 | (('= ($ <primcall> src2 'logand (a b)) ($ <const> _ 0)) |
| 1338 | (let ((src (or src src2))) |
| 1339 | (make-primcall src 'not |
| 1340 | (list (make-primcall src 'logtest (list a b)))))) |
| 1341 | |
| 1342 | (('logbit? ($ <const> src2 |
| 1343 | (? (lambda (bit) |
| 1344 | (and (exact-integer? bit) (not (negative? bit)))) |
| 1345 | bit)) |
| 1346 | val) |
| 1347 | (fold-constants src 'logtest |
| 1348 | (list (make-const (or src2 src) (ash 1 bit)) val) |
| 1349 | ctx)) |
| 1350 | |
| 1351 | (((? effect-free-primitive?) . args) |
| 1352 | (fold-constants src name args ctx)) |
| 1353 | |
| 1354 | ((name . args) |
| 1355 | (make-primcall src name args)))) |
| 1356 | |
| 1357 | (($ <call> src orig-proc orig-args) |
| 1358 | ;; todo: augment the global env with specialized functions |
| 1359 | (let revisit-proc ((proc (visit orig-proc 'operator))) |
| 1360 | (match proc |
| 1361 | (($ <primitive-ref> _ name) |
| 1362 | (for-tail (make-primcall src name orig-args))) |
| 1363 | (($ <lambda> _ _ |
| 1364 | ($ <lambda-case> _ req opt rest #f inits gensyms body #f)) |
| 1365 | ;; Simple case: no keyword arguments. |
| 1366 | ;; todo: handle the more complex cases |
| 1367 | (let* ((nargs (length orig-args)) |
| 1368 | (nreq (length req)) |
| 1369 | (opt (or opt '())) |
| 1370 | (rest (if rest (list rest) '())) |
| 1371 | (nopt (length opt)) |
| 1372 | (key (source-expression proc))) |
| 1373 | (define (singly-referenced-lambda? orig-proc) |
| 1374 | (match orig-proc |
| 1375 | (($ <lambda>) #t) |
| 1376 | (($ <lexical-ref> _ _ sym) |
| 1377 | (and (not (assigned-lexical? sym)) |
| 1378 | (= (lexical-refcount sym) 1) |
| 1379 | (singly-referenced-lambda? |
| 1380 | (operand-source (lookup sym))))) |
| 1381 | (_ #f))) |
| 1382 | (define (inlined-call) |
| 1383 | (let ((req-vals (list-head orig-args nreq)) |
| 1384 | (opt-vals (let lp ((args (drop orig-args nreq)) |
| 1385 | (inits inits) |
| 1386 | (out '())) |
| 1387 | (match inits |
| 1388 | (() (reverse out)) |
| 1389 | ((init . inits) |
| 1390 | (match args |
| 1391 | (() |
| 1392 | (lp '() inits (cons init out))) |
| 1393 | ((arg . args) |
| 1394 | (lp args inits (cons arg out)))))))) |
| 1395 | (rest-vals (cond |
| 1396 | ((> nargs (+ nreq nopt)) |
| 1397 | (list (make-primcall |
| 1398 | #f 'list |
| 1399 | (drop orig-args (+ nreq nopt))))) |
| 1400 | (rest (list (make-const #f '()))) |
| 1401 | (else '())))) |
| 1402 | (if (>= nargs (+ nreq nopt)) |
| 1403 | (make-let src |
| 1404 | (append req opt rest) |
| 1405 | gensyms |
| 1406 | (append req-vals opt-vals rest-vals) |
| 1407 | body) |
| 1408 | ;; The default initializers of optional arguments |
| 1409 | ;; may refer to earlier arguments, so in the general |
| 1410 | ;; case we must expand into a series of nested let |
| 1411 | ;; expressions. |
| 1412 | ;; |
| 1413 | ;; In the generated code, the outermost let |
| 1414 | ;; expression will bind all required arguments, as |
| 1415 | ;; well as the empty rest argument, if any. Each |
| 1416 | ;; optional argument will be bound within an inner |
| 1417 | ;; let. |
| 1418 | (make-let src |
| 1419 | (append req rest) |
| 1420 | (append (list-head gensyms nreq) |
| 1421 | (last-pair gensyms)) |
| 1422 | (append req-vals rest-vals) |
| 1423 | (fold-right (lambda (var gensym val body) |
| 1424 | (make-let src |
| 1425 | (list var) |
| 1426 | (list gensym) |
| 1427 | (list val) |
| 1428 | body)) |
| 1429 | body |
| 1430 | opt |
| 1431 | (list-head (drop gensyms nreq) nopt) |
| 1432 | opt-vals))))) |
| 1433 | |
| 1434 | (cond |
| 1435 | ((or (< nargs nreq) (and (not rest) (> nargs (+ nreq nopt)))) |
| 1436 | ;; An error, or effecting arguments. |
| 1437 | (make-call src (for-call orig-proc) (map for-value orig-args))) |
| 1438 | ((or (and=> (find-counter key counter) counter-recursive?) |
| 1439 | (singly-referenced-lambda? orig-proc)) |
| 1440 | ;; A recursive call, or a lambda in the operator |
| 1441 | ;; position of the source expression. Process again in |
| 1442 | ;; tail context. |
| 1443 | ;; |
| 1444 | ;; In the recursive case, mark intervening counters as |
| 1445 | ;; recursive, so we can handle a toplevel counter that |
| 1446 | ;; recurses mutually with some other procedure. |
| 1447 | ;; Otherwise, the next time we see the other procedure, |
| 1448 | ;; the effort limit would be clamped to 100. |
| 1449 | ;; |
| 1450 | (let ((found (find-counter key counter))) |
| 1451 | (if (and found (counter-recursive? found)) |
| 1452 | (let lp ((counter counter)) |
| 1453 | (if (not (eq? counter found)) |
| 1454 | (begin |
| 1455 | (set-counter-recursive?! counter #t) |
| 1456 | (lp (counter-prev counter))))))) |
| 1457 | |
| 1458 | (log 'inline-recurse key) |
| 1459 | (loop (inlined-call) env counter ctx)) |
| 1460 | (else |
| 1461 | ;; An integration at the top-level, the first |
| 1462 | ;; recursion of a recursive procedure, or a nested |
| 1463 | ;; integration of a procedure that hasn't been seen |
| 1464 | ;; yet. |
| 1465 | (log 'inline-begin exp) |
| 1466 | (let/ec k |
| 1467 | (define (abort) |
| 1468 | (log 'inline-abort exp) |
| 1469 | (k (make-call src (for-call orig-proc) |
| 1470 | (map for-value orig-args)))) |
| 1471 | (define new-counter |
| 1472 | (cond |
| 1473 | ;; These first two cases will transfer effort |
| 1474 | ;; from the current counter into the new |
| 1475 | ;; counter. |
| 1476 | ((find-counter key counter) |
| 1477 | => (lambda (prev) |
| 1478 | (make-recursive-counter recursive-effort-limit |
| 1479 | operand-size-limit |
| 1480 | prev counter))) |
| 1481 | (counter |
| 1482 | (make-nested-counter abort key counter)) |
| 1483 | ;; This case opens a new account, effectively |
| 1484 | ;; printing money. It should only do so once |
| 1485 | ;; for each call site in the source program. |
| 1486 | (else |
| 1487 | (make-top-counter effort-limit operand-size-limit |
| 1488 | abort key)))) |
| 1489 | (define result |
| 1490 | (loop (inlined-call) env new-counter ctx)) |
| 1491 | |
| 1492 | (if counter |
| 1493 | ;; The nested inlining attempt succeeded. |
| 1494 | ;; Deposit the unspent effort and size back |
| 1495 | ;; into the current counter. |
| 1496 | (transfer! new-counter counter)) |
| 1497 | |
| 1498 | (log 'inline-end result exp) |
| 1499 | result))))) |
| 1500 | (($ <let> _ _ _ vals _) |
| 1501 | ;; Attempt to inline `let' in the operator position. |
| 1502 | ;; |
| 1503 | ;; We have to re-visit the proc in value mode, since the |
| 1504 | ;; `let' bindings might have been introduced or renamed, |
| 1505 | ;; whereas the lambda (if any) in operator position has not |
| 1506 | ;; been renamed. |
| 1507 | (if (or (and-map constant-expression? vals) |
| 1508 | (and-map constant-expression? orig-args)) |
| 1509 | ;; The arguments and the let-bound values commute. |
| 1510 | (match (for-value orig-proc) |
| 1511 | (($ <let> lsrc names syms vals body) |
| 1512 | (log 'inline-let orig-proc) |
| 1513 | (for-tail |
| 1514 | (make-let lsrc names syms vals |
| 1515 | (make-call src body orig-args)))) |
| 1516 | ;; It's possible for a `let' to go away after the |
| 1517 | ;; visit due to the fact that visiting a procedure in |
| 1518 | ;; value context will prune unused bindings, whereas |
| 1519 | ;; visiting in operator mode can't because it doesn't |
| 1520 | ;; traverse through lambdas. In that case re-visit |
| 1521 | ;; the procedure. |
| 1522 | (proc (revisit-proc proc))) |
| 1523 | (make-call src (for-call orig-proc) |
| 1524 | (map for-value orig-args)))) |
| 1525 | (_ |
| 1526 | (make-call src (for-call orig-proc) (map for-value orig-args)))))) |
| 1527 | (($ <lambda> src meta body) |
| 1528 | (case ctx |
| 1529 | ((effect) (make-void #f)) |
| 1530 | ((test) (make-const #f #t)) |
| 1531 | ((operator) exp) |
| 1532 | (else (record-source-expression! |
| 1533 | exp |
| 1534 | (make-lambda src meta (and body (for-values body))))))) |
| 1535 | (($ <lambda-case> src req opt rest kw inits gensyms body alt) |
| 1536 | (define (lift-applied-lambda body gensyms) |
| 1537 | (and (not opt) rest (not kw) |
| 1538 | (match body |
| 1539 | (($ <primcall> _ 'apply |
| 1540 | (($ <lambda> _ _ (and lcase ($ <lambda-case>))) |
| 1541 | ($ <lexical-ref> _ _ sym) |
| 1542 | ...)) |
| 1543 | (and (equal? sym gensyms) |
| 1544 | (not (lambda-case-alternate lcase)) |
| 1545 | lcase)) |
| 1546 | (_ #f)))) |
| 1547 | (let* ((vars (map lookup-var gensyms)) |
| 1548 | (new (fresh-gensyms vars)) |
| 1549 | (env (fold extend-env env gensyms |
| 1550 | (make-unbound-operands vars new))) |
| 1551 | (new-sym (lambda (old) |
| 1552 | (operand-sym (cdr (vhash-assq old env))))) |
| 1553 | (body (loop body env counter ctx))) |
| 1554 | (or |
| 1555 | ;; (lambda args (apply (lambda ...) args)) => (lambda ...) |
| 1556 | (lift-applied-lambda body new) |
| 1557 | (make-lambda-case src req opt rest |
| 1558 | (match kw |
| 1559 | ((aok? (kw name old) ...) |
| 1560 | (cons aok? (map list kw name (map new-sym old)))) |
| 1561 | (_ #f)) |
| 1562 | (map (cut loop <> env counter 'value) inits) |
| 1563 | new |
| 1564 | body |
| 1565 | (and alt (for-tail alt)))))) |
| 1566 | (($ <seq> src head tail) |
| 1567 | (let ((head (for-effect head)) |
| 1568 | (tail (for-tail tail))) |
| 1569 | (if (void? head) |
| 1570 | tail |
| 1571 | (make-seq src |
| 1572 | (if (and (seq? head) |
| 1573 | (void? (seq-tail head))) |
| 1574 | (seq-head head) |
| 1575 | head) |
| 1576 | tail)))) |
| 1577 | (($ <prompt> src escape-only? tag body handler) |
| 1578 | (define (make-prompt-tag? x) |
| 1579 | (match x |
| 1580 | (($ <primcall> _ 'make-prompt-tag (or () ((? constant-expression?)))) |
| 1581 | #t) |
| 1582 | (_ #f))) |
| 1583 | |
| 1584 | (let ((tag (for-value tag)) |
| 1585 | (body (if escape-only? (for-tail body) (for-value body)))) |
| 1586 | (cond |
| 1587 | ((find-definition tag 1) |
| 1588 | (lambda (val op) |
| 1589 | (make-prompt-tag? val)) |
| 1590 | => (lambda (val op) |
| 1591 | ;; There is no way that an <abort> could know the tag |
| 1592 | ;; for this <prompt>, so we can elide the <prompt> |
| 1593 | ;; entirely. |
| 1594 | (unrecord-operand-uses op 1) |
| 1595 | (for-tail (if escape-only? body (make-call src body '()))))) |
| 1596 | (else |
| 1597 | (let ((handler (for-value handler))) |
| 1598 | (define (escape-only-handler? handler) |
| 1599 | (match handler |
| 1600 | (($ <lambda> _ _ |
| 1601 | ($ <lambda-case> _ (_ . _) _ _ _ _ (k . _) body #f)) |
| 1602 | (not (tree-il-any |
| 1603 | (match-lambda |
| 1604 | (($ <lexical-ref> _ _ (? (cut eq? <> k))) #t) |
| 1605 | (_ #f)) |
| 1606 | body))) |
| 1607 | (else #f))) |
| 1608 | (if (and (not escape-only?) (escape-only-handler? handler)) |
| 1609 | ;; Prompt transitioning to escape-only; transition body |
| 1610 | ;; to be an expression. |
| 1611 | (for-tail |
| 1612 | (make-prompt src #t tag (make-call #f body '()) handler)) |
| 1613 | (make-prompt src escape-only? tag body handler))))))) |
| 1614 | |
| 1615 | (($ <abort> src tag args tail) |
| 1616 | (make-abort src (for-value tag) (map for-value args) |
| 1617 | (for-value tail)))))) |