| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
;;; Work in progress |
| 18 |
|
| 19 |
;;; Oscar Waddell. "Extending the Scope of Syntactic Abstraction". PhD. |
| 20 |
;;; Thesis. Indiana University Computer Science Department. August 1999. |
| 21 |
;;; Available online: |
| 22 |
;;; http://www.cs.indiana.edu/~owaddell/papers/thesis.ps.gz |
| 23 |
|
| 24 |
(module (source-optimize optimize-level cp0-effort-limit cp0-size-limit) |
| 25 |
(define who 'source-optimize) |
| 26 |
;;; |
| 27 |
(define-structure (app rand* ctxt) |
| 28 |
([inlined #f])) |
| 29 |
;;; |
| 30 |
(define-structure (operand expr env ec) |
| 31 |
([value #f] |
| 32 |
[residualize-for-effect #f] |
| 33 |
[size 0] |
| 34 |
[inner-pending #f] |
| 35 |
[outer-pending #f])) |
| 36 |
;;; |
| 37 |
(define-structure (counter value ctxt k)) |
| 38 |
;;; |
| 39 |
(define (passive-counter) |
| 40 |
(make-counter (greatest-fixnum) #f |
| 41 |
(lambda args |
| 42 |
(error 'passive-counter "invalid abort")))) |
| 43 |
;;; |
| 44 |
(define (passive-counter-value x) |
| 45 |
(- (greatest-fixnum) (counter-value x))) |
| 46 |
;;; |
| 47 |
(define (active-counter? x) |
| 48 |
(and (counter? x) (counter-ctxt x))) |
| 49 |
;;; |
| 50 |
(define (decrement x amt) |
| 51 |
(let ([n (- (counter-value x) amt)]) |
| 52 |
(set-counter-value! x n) |
| 53 |
(when (< n 0) |
| 54 |
(reset-integrated! (counter-ctxt x)) |
| 55 |
((counter-k x) #f)))) |
| 56 |
;;; |
| 57 |
(define (abort-counter! x) |
| 58 |
(reset-integrated! (counter-ctxt x)) |
| 59 |
((counter-k x) #f)) |
| 60 |
;;; |
| 61 |
(define (reset-integrated! ctxt) |
| 62 |
(set-app-inlined! ctxt #f) |
| 63 |
(let ([ctxt (app-ctxt ctxt)]) |
| 64 |
(when (app? ctxt) |
| 65 |
(reset-integrated! ctxt)))) |
| 66 |
;;; |
| 67 |
;;; |
| 68 |
(module (with-extended-env copy-var) |
| 69 |
(define (copy-var x) |
| 70 |
(let ([y (make-prelex (prelex-name x) #f)]) |
| 71 |
(set-prelex-source-referenced?! y |
| 72 |
(prelex-source-referenced? x)) |
| 73 |
(set-prelex-source-assigned?! y |
| 74 |
(prelex-source-assigned? x)) |
| 75 |
(let ([loc (prelex-global-location x)]) |
| 76 |
(when loc |
| 77 |
(set-prelex-global-location! y loc) |
| 78 |
(set-prelex-source-referenced?! y #t) |
| 79 |
(set-prelex-residual-referenced?! y #t))) |
| 80 |
y)) |
| 81 |
(define (extend env lhs* rands) |
| 82 |
(if (null? lhs*) |
| 83 |
(values env '()) |
| 84 |
(let ([nlhs* (map copy-var lhs*)]) |
| 85 |
(when rands |
| 86 |
(for-each |
| 87 |
(lambda (lhs rhs) |
| 88 |
(set-prelex-operand! lhs rhs)) |
| 89 |
nlhs* rands)) |
| 90 |
(values (vector lhs* nlhs* env) nlhs*)))) |
| 91 |
(define (copy-back ls) |
| 92 |
(for-each |
| 93 |
(lambda (x) |
| 94 |
(set-prelex-source-assigned?! x |
| 95 |
(prelex-residual-assigned? x)) |
| 96 |
(set-prelex-source-referenced?! x |
| 97 |
(prelex-residual-referenced? x))) |
| 98 |
ls)) |
| 99 |
(define-syntax with-extended-env |
| 100 |
(syntax-rules () |
| 101 |
[(_ ((e2 args2) (e1 args1 rands)) b b* ...) |
| 102 |
(let-values ([(e2 args2) (extend e1 args1 rands)]) |
| 103 |
(let ([v (let () b b* ...)]) |
| 104 |
(copy-back args2) |
| 105 |
v))]))) |
| 106 |
|
| 107 |
(define cp0-effort-limit (make-parameter 50)) |
| 108 |
(define cp0-size-limit (make-parameter 8)) |
| 109 |
|
| 110 |
(define primitive-info-list |
| 111 |
'( |
| 112 |
[(cons _ _) effect-free result-true] |
| 113 |
[(cons* _) foldable effect-free ] |
| 114 |
[(cons* _ . _) effect-free result-true] |
| 115 |
[(list) foldable effect-free result-true] |
| 116 |
[(list . _) effect-free result-true] |
| 117 |
[(reverse ()) foldable effect-free result-true] |
| 118 |
[(string) foldable effect-free result-true] |
| 119 |
[(string . _) result-true] |
| 120 |
[(make-string 0) foldable effect-free result-true] |
| 121 |
[(make-string 0 _) foldable effect-free result-true] |
| 122 |
[(make-string . _) result-true] |
| 123 |
[(make-bytevector 0) foldable effect-free result-true] |
| 124 |
[(make-bytevector 0 _) foldable result-true] |
| 125 |
[(make-bytevector . _) result-true] |
| 126 |
[(string-length _) foldable result-true] |
| 127 |
[(string-ref _ _) foldable result-true] |
| 128 |
[(vector) foldable effect-free result-true] |
| 129 |
[(vector . _) effect-free result-true] |
| 130 |
[(make-vector 0) foldable effect-free result-true] |
| 131 |
[(make-vector 0 _) foldable effect-free result-true] |
| 132 |
[(make-vector . _) result-true] |
| 133 |
[(vector-length _) foldable result-true] |
| 134 |
[(vector-ref _ _) foldable ] |
| 135 |
[(eq? _ _) foldable effect-free ] |
| 136 |
[(eqv? _ _) foldable effect-free ] |
| 137 |
[(assq _ _) foldable ] |
| 138 |
[(assv _ _) foldable ] |
| 139 |
[(assoc _ _) foldable ] |
| 140 |
[(not _) foldable effect-free ] |
| 141 |
[(null? _) foldable effect-free ] |
| 142 |
[(pair? _) foldable effect-free ] |
| 143 |
[(fixnum? _) foldable effect-free ] |
| 144 |
[(vector? _) foldable effect-free ] |
| 145 |
[(string? _) foldable effect-free ] |
| 146 |
[(char? _) foldable effect-free ] |
| 147 |
[(symbol? _) foldable effect-free ] |
| 148 |
[(procedure? _) foldable effect-free ] |
| 149 |
[(eof-object? _) foldable effect-free ] |
| 150 |
[(flonum? _) foldable effect-free ] |
| 151 |
[(cflonum? _) foldable effect-free ] |
| 152 |
[(compnum? _) foldable effect-free ] |
| 153 |
[(integer? _) foldable effect-free ] |
| 154 |
[(bignum? _) foldable effect-free ] |
| 155 |
[(ratnum? _) foldable effect-free ] |
| 156 |
[(void) foldable effect-free result-true] |
| 157 |
[(car _) foldable ] |
| 158 |
[(cdr _) foldable ] |
| 159 |
[(caar _) foldable ] |
| 160 |
[(cadr _) foldable ] |
| 161 |
[(cdar _) foldable ] |
| 162 |
[(cddr _) foldable ] |
| 163 |
[(caaar _) foldable ] |
| 164 |
[(caadr _) foldable ] |
| 165 |
[(cadar _) foldable ] |
| 166 |
[(caddr _) foldable ] |
| 167 |
[(cdaar _) foldable ] |
| 168 |
[(cdadr _) foldable ] |
| 169 |
[(cddar _) foldable ] |
| 170 |
[(cdddr _) foldable ] |
| 171 |
[(caaaar _) foldable ] |
| 172 |
[(caaadr _) foldable ] |
| 173 |
[(caadar _) foldable ] |
| 174 |
[(caaddr _) foldable ] |
| 175 |
[(cadaar _) foldable ] |
| 176 |
[(cadadr _) foldable ] |
| 177 |
[(caddar _) foldable ] |
| 178 |
[(cadddr _) foldable ] |
| 179 |
[(cdaaar _) foldable ] |
| 180 |
[(cdaadr _) foldable ] |
| 181 |
[(cdadar _) foldable ] |
| 182 |
[(cdaddr _) foldable ] |
| 183 |
[(cddaar _) foldable ] |
| 184 |
[(cddadr _) foldable ] |
| 185 |
[(cdddar _) foldable ] |
| 186 |
[(cddddr _) foldable ] |
| 187 |
[(memq _ _) foldable ] |
| 188 |
[(memv _ _) foldable ] |
| 189 |
[(length _) foldable result-true] |
| 190 |
[(+ . _) foldable result-true] |
| 191 |
[(* . _) foldable result-true] |
| 192 |
[(/ _ . _) foldable result-true] |
| 193 |
[(- _ . _) foldable result-true] |
| 194 |
[(fx+ _ _) foldable result-true] |
| 195 |
[(fx- _ _) foldable result-true] |
| 196 |
[(fx* _ _) foldable result-true] |
| 197 |
[(fxior . _) foldable result-true] |
| 198 |
[(fxlogor . _) foldable result-true] |
| 199 |
[(fxnot _) foldable result-true] |
| 200 |
[(fxadd1 _) foldable result-true] |
| 201 |
[(fxsub1 _) foldable result-true] |
| 202 |
[(fxzero? _) foldable ] |
| 203 |
[(fx=? _ . _) foldable ] |
| 204 |
[(fx<? _ . _) foldable ] |
| 205 |
[(fx<=? _ . _) foldable ] |
| 206 |
[(fx>? _ . _) foldable ] |
| 207 |
[(fx>=? _ . _) foldable ] |
| 208 |
[(fx= _ . _) foldable ] |
| 209 |
[(fx< _ . _) foldable ] |
| 210 |
[(fx<= _ . _) foldable ] |
| 211 |
[(fx> _ . _) foldable ] |
| 212 |
[(fx>= _ . _) foldable ] |
| 213 |
[(real-part _) foldable result-true] |
| 214 |
[(imag-part _) foldable result-true] |
| 215 |
[(fxsll _ _) foldable result-true] |
| 216 |
[(fxsra _ _) foldable result-true] |
| 217 |
[(fxremainder _ _) foldable result-true] |
| 218 |
[(fxquotient _ _) foldable result-true] |
| 219 |
[(greatest-fixnum) foldable effect-free result-true] |
| 220 |
[(least-fixnum) foldable effect-free result-true] |
| 221 |
[(fixnum-width) foldable effect-free result-true] |
| 222 |
[(char->integer _) foldable result-true] |
| 223 |
[(integer->char _) foldable result-true] |
| 224 |
[(eof-object) foldable effect-free result-true] |
| 225 |
[(zero? _) foldable ] |
| 226 |
[(= _ . _) foldable ] |
| 227 |
[(< _ . _) foldable ] |
| 228 |
[(<= _ . _) foldable ] |
| 229 |
[(> _ . _) foldable ] |
| 230 |
[(>= _ . _) foldable ] |
| 231 |
[(expt _ _) foldable result-true] |
| 232 |
[(log _) foldable result-true] |
| 233 |
[(sll _ _) foldable result-true] |
| 234 |
[(sra _ _) foldable result-true] |
| 235 |
[(inexact _) foldable result-true] |
| 236 |
[(exact _) foldable result-true] |
| 237 |
[(add1 _) foldable result-true] |
| 238 |
[(sub1 _) foldable result-true] |
| 239 |
[(bitwise-and _ _) foldable result-true] |
| 240 |
[(make-rectangular _ _) foldable result-true] |
| 241 |
[(sin _) foldable result-true] |
| 242 |
[(cos _) foldable result-true] |
| 243 |
[(tan _) foldable result-true] |
| 244 |
[(asin _) foldable result-true] |
| 245 |
[(acos _) foldable result-true] |
| 246 |
[(atan _) foldable result-true] |
| 247 |
[(make-eq-hashtable) effect-free result-true] |
| 248 |
[(string->number _) foldable ] |
| 249 |
[(string->number _ _) foldable ] |
| 250 |
[($fixnum->flonum _) foldable effect-free result-true] |
| 251 |
[($char->fixnum _) foldable effect-free result-true] |
| 252 |
[($fixnum->char _) foldable effect-free result-true] |
| 253 |
[($fxzero? _) foldable effect-free ] |
| 254 |
[($fx+ _ _) foldable effect-free result-true] |
| 255 |
[($fx* _ _) foldable effect-free result-true] |
| 256 |
[($fx- _ _) foldable effect-free result-true] |
| 257 |
[($fx= _ _) foldable effect-free ] |
| 258 |
[($fx>= _ _) foldable effect-free ] |
| 259 |
[($fx> _ _) foldable effect-free ] |
| 260 |
[($fx<= _ _) foldable effect-free ] |
| 261 |
[($fx< _ _) foldable effect-free ] |
| 262 |
[($car _) foldable effect-free ] |
| 263 |
[($cdr _) foldable effect-free ] |
| 264 |
[($struct-ref _ _) foldable effect-free ] |
| 265 |
[($struct/rtd? _ _) foldable effect-free ] |
| 266 |
[($fxsll _ _) foldable effect-free result-true] |
| 267 |
[($fxsra _ _) foldable effect-free result-true] |
| 268 |
[($fxlogor _ _) foldable effect-free result-true] |
| 269 |
[($fxlogand _ _) foldable effect-free result-true] |
| 270 |
[($fxadd1 _) foldable effect-free result-true] |
| 271 |
[($fxsub1 _) foldable effect-free result-true] |
| 272 |
[($vector-length _) foldable effect-free result-true] |
| 273 |
[($vector-ref _ _) foldable effect-free result-true] |
| 274 |
[($make-bytevector 0) foldable effect-free result-true] |
| 275 |
[($make-bytevector 0 _) foldable effect-free result-true] |
| 276 |
[($make-bytevector . _) effect-free result-true] |
| 277 |
[($bytevector-u8-ref _ _) foldable effect-free result-true] |
| 278 |
[($bytevector-length _) foldable effect-free result-true] |
| 279 |
;;; |
| 280 |
[(annotation? #f) foldable effect-free result-false] |
| 281 |
[(annotation-stripped #f) foldable effect-free result-false] |
| 282 |
;;; unoptimizable |
| 283 |
[(condition . _)] |
| 284 |
[($make-flonum . _)] |
| 285 |
[(top-level-value . _)] |
| 286 |
[($struct . _)] |
| 287 |
[(make-message-condition . _)] |
| 288 |
[(make-lexical-violation . _)] |
| 289 |
[(make-who-condition . _)] |
| 290 |
[(make-error . _)] |
| 291 |
[(make-i/o-error . _)] |
| 292 |
[(make-i/o-write-error . _)] |
| 293 |
[(make-i/o-read-error . _)] |
| 294 |
[(make-i/o-file-already-exists-error . _)] |
| 295 |
[(make-i/o-file-is-read-only-error . _)] |
| 296 |
[(make-i/o-file-protection-error . _)] |
| 297 |
[(make-i/o-file-does-not-exist-error . _)] |
| 298 |
[(make-undefined-violation . _)] |
| 299 |
[(die . _)] |
| 300 |
[(gensym . _)] |
| 301 |
[(values . _)] |
| 302 |
[(error . _)] |
| 303 |
[(assertion-violation . _)] |
| 304 |
[(console-input-port . _)] |
| 305 |
[(console-output-port . _)] |
| 306 |
[(console-error-port . _)] |
| 307 |
[(printf . _)] ;;; FIXME: reduce to display |
| 308 |
[(newline . _)] |
| 309 |
[(native-transcoder . _)] |
| 310 |
[(open-string-output-port . _)] |
| 311 |
[(open-string-input-port . _)] |
| 312 |
[(environment . _)] |
| 313 |
[(print-gensym . _)] |
| 314 |
[(exit . _)] |
| 315 |
[(interrupt-handler . _)] |
| 316 |
[(display . _)] |
| 317 |
[(write-char . _)] |
| 318 |
[(current-input-port . _)] |
| 319 |
[(current-output-port . _)] |
| 320 |
[(current-error-port . _)] |
| 321 |
[(standard-input-port . _)] |
| 322 |
[(standard-output-port . _)] |
| 323 |
[(standard-error-port . _)] |
| 324 |
[($current-frame . _)] |
| 325 |
[(pretty-width . _)] |
| 326 |
[($fp-at-base . _)] |
| 327 |
[(read-annotated . _)] |
| 328 |
[($collect-key . _)] |
| 329 |
[(make-non-continuable-violation . _)] |
| 330 |
[(format . _)] ;;; FIXME, reduce to string-copy |
| 331 |
[(uuid . _)] |
| 332 |
[(print-graph . _)] |
| 333 |
[(interaction-environment . _)] |
| 334 |
[(make-guardian)] |
| 335 |
[(command-line-arguments)] |
| 336 |
[(make-record-type-descriptor . _)] ;;; FIXME |
| 337 |
[(make-assertion-violation . _)] |
| 338 |
[(new-cafe . _)] |
| 339 |
[(getenv . _)] |
| 340 |
[(gensym-prefix . _)] |
| 341 |
[($arg-list . _)] |
| 342 |
[($make-symbol . _)] |
| 343 |
[(string->utf8 . _)] |
| 344 |
[($make-call-with-values-procedure . _)] |
| 345 |
[($make-values-procedure . _)] |
| 346 |
[($unset-interrupted! . _)] |
| 347 |
[(make-interrupted-condition . _)] |
| 348 |
[($interrupted? . _)] |
| 349 |
[($symbol-value . _)] |
| 350 |
[(library-extensions . _)] |
| 351 |
[(base-rtd . _)] |
| 352 |
[($data->transcoder . _)] |
| 353 |
[(current-time . _)] |
| 354 |
)) |
| 355 |
|
| 356 |
(module (primprop) |
| 357 |
(define-syntax ct-gensym |
| 358 |
(lambda (x) |
| 359 |
(with-syntax ([g (datum->syntax #'here (gensym))]) |
| 360 |
#'(quote g)))) |
| 361 |
(define g (ct-gensym)) |
| 362 |
(define (primprop p) |
| 363 |
(or (getprop p g) '())) |
| 364 |
(define (get prim ls) |
| 365 |
(cond |
| 366 |
[(null? ls) (values '() '())] |
| 367 |
[else |
| 368 |
(let ([a (car ls)]) |
| 369 |
(let ([cc (car a)]) |
| 370 |
(cond |
| 371 |
[(eq? (car cc) prim) |
| 372 |
(let-values ([(p* ls) (get prim (cdr ls))]) |
| 373 |
(values (cons (cons (cdr cc) (cdr a)) p*) ls))] |
| 374 |
[else (values '() ls)])))])) |
| 375 |
(let f ([ls primitive-info-list]) |
| 376 |
(unless (null? ls) |
| 377 |
(let ([a (car ls)]) |
| 378 |
(let ([cc (car a)] [cv (cdr a)]) |
| 379 |
(let ([prim (car cc)] [args (cdr cc)]) |
| 380 |
(let-values ([(p* ls) (get prim (cdr ls))]) |
| 381 |
(putprop prim g |
| 382 |
(cons (cons args cv) p*)) |
| 383 |
(f ls)))))))) |
| 384 |
(define (primitive-info op args) |
| 385 |
(define (matches? x) |
| 386 |
(let f ([args args] [params (car x)]) |
| 387 |
(cond |
| 388 |
[(pair? params) |
| 389 |
(and (pair? args) |
| 390 |
(case (car params) |
| 391 |
[(_) (f (cdr args) (cdr params))] |
| 392 |
[(#f 0 ()) |
| 393 |
(let ([v (value-visit-operand! (car args))]) |
| 394 |
(and (constant? v) |
| 395 |
(equal? (constant-value v) (car params)) |
| 396 |
(f (cdr args) (cdr params))))] |
| 397 |
[else |
| 398 |
(error 'primitive-info "cannot happen" op (car params))]))] |
| 399 |
[(eq? params '_) #t] |
| 400 |
[(null? params) (null? args)] |
| 401 |
[else (error 'primitive-info "cannot happen" op params)]))) |
| 402 |
(cond |
| 403 |
[(find matches? (primprop op))] |
| 404 |
[else '()])) |
| 405 |
|
| 406 |
(define (info-foldable? info) (memq 'foldable info)) |
| 407 |
(define (info-effect-free? info) (memq 'effect-free info)) |
| 408 |
(define (info-result-true? info) (memq 'result-true info)) |
| 409 |
(define (info-result-false? info) (memq 'result-false info)) |
| 410 |
|
| 411 |
(define-syntax ctxt-case |
| 412 |
(lambda (stx) |
| 413 |
(define (test x) |
| 414 |
(case (syntax->datum x) |
| 415 |
[(p) #'(eq? t 'p)] |
| 416 |
[(v) #'(eq? t 'v)] |
| 417 |
[(e) #'(eq? t 'e)] |
| 418 |
[(app) #'(app? t)] |
| 419 |
[else (syntax-violation stx "invalid ctxt" x)])) |
| 420 |
(define (extract cls*) |
| 421 |
(syntax-case cls* (else) |
| 422 |
[() #'(error 'extract "unmatched ctxt" t)] |
| 423 |
[([else e e* ...]) #'(begin e e* ...)] |
| 424 |
[([(t* ...) e e* ...] rest ...) |
| 425 |
(with-syntax ([(t* ...) (map test #'(t* ...))] |
| 426 |
[body (extract #'(rest ...))]) |
| 427 |
#'(if (or t* ...) |
| 428 |
(begin e e* ...) |
| 429 |
body))])) |
| 430 |
(syntax-case stx () |
| 431 |
[(_ expr cls* ...) |
| 432 |
(with-syntax ([body (extract #'(cls* ...))]) |
| 433 |
#'(let ([t expr]) |
| 434 |
body))]))) |
| 435 |
(define (mkseq e0 e1) |
| 436 |
;;; returns a (seq e0 e1) with a seq-less e1 if both |
| 437 |
;;; e0 and e1 are constructed properly. |
| 438 |
(if (simple? e0) |
| 439 |
e1 |
| 440 |
(let ([e0 (struct-case e0 |
| 441 |
[(seq e0a e0b) (if (simple? e0b) e0a e0)] |
| 442 |
[else e0])]) |
| 443 |
(struct-case e1 |
| 444 |
[(seq e1a e1b) (make-seq (make-seq e0 e1a) e1b)] |
| 445 |
[else (make-seq e0 e1)])))) |
| 446 |
;;; simple?: check quickly whether something is effect-free |
| 447 |
(define (simple? x) |
| 448 |
(struct-case x |
| 449 |
[(constant) #t] |
| 450 |
[(prelex) #t] |
| 451 |
[(primref) #t] |
| 452 |
[(clambda) #t] |
| 453 |
[else #f])) |
| 454 |
;;; result returns the "last" value of an expression |
| 455 |
(define (result-expr x) |
| 456 |
(struct-case x |
| 457 |
[(seq e0 e1) e1] |
| 458 |
[else x])) |
| 459 |
;;; |
| 460 |
(define (records-equal? x y ctxt) |
| 461 |
(struct-case x |
| 462 |
[(constant kx) |
| 463 |
(struct-case y |
| 464 |
[(constant ky) |
| 465 |
(ctxt-case ctxt |
| 466 |
[(e) #t] |
| 467 |
[(p) (if kx ky (not ky))] |
| 468 |
[else (eq? kx ky)])] |
| 469 |
[else #f])] |
| 470 |
[else #f])) |
| 471 |
;;; |
| 472 |
(define (residualize-operands e rand* sc) |
| 473 |
(cond |
| 474 |
[(null? rand*) e] |
| 475 |
[(not (operand-residualize-for-effect (car rand*))) |
| 476 |
(residualize-operands e (cdr rand*) sc)] |
| 477 |
[else |
| 478 |
(let ([opnd (car rand*)]) |
| 479 |
(let ([e1 (or (operand-value opnd) |
| 480 |
(struct-case opnd |
| 481 |
[(operand expr env ec) |
| 482 |
(E expr 'e env ec sc)]))]) |
| 483 |
(if (simple? e1) |
| 484 |
(residualize-operands e (cdr rand*) sc) |
| 485 |
(begin |
| 486 |
(decrement sc (operand-size opnd)) |
| 487 |
(mkseq e1 (residualize-operands e (cdr rand*) sc))))))])) |
| 488 |
(define (value-visit-operand! rand) |
| 489 |
(or (operand-value rand) |
| 490 |
(let ([sc (passive-counter)]) |
| 491 |
(let ([e (struct-case rand |
| 492 |
[(operand expr env ec) |
| 493 |
(E expr 'v env sc ec)])]) |
| 494 |
(set-operand-value! rand e) |
| 495 |
(set-operand-size! rand (passive-counter-value sc)) |
| 496 |
e)))) |
| 497 |
(define (score-value-visit-operand! rand sc) |
| 498 |
(let ([val (value-visit-operand! rand)]) |
| 499 |
(let ([score (operand-size rand)]) |
| 500 |
(decrement sc score)) |
| 501 |
val)) |
| 502 |
(define (E-call rator rand* env ctxt ec sc) |
| 503 |
(let ([ctxt (make-app rand* ctxt)]) |
| 504 |
(let ([rator (E rator ctxt env ec sc)]) |
| 505 |
(if (app-inlined ctxt) |
| 506 |
(residualize-operands rator rand* sc) |
| 507 |
(begin |
| 508 |
(decrement sc (if (primref? rator) 1 3)) |
| 509 |
(make-funcall rator |
| 510 |
(map (lambda (x) (score-value-visit-operand! x sc)) |
| 511 |
rand*))))))) |
| 512 |
;;; |
| 513 |
(define (E-debug-call ctxt ec sc) |
| 514 |
(let ([rand* (app-rand* ctxt)]) |
| 515 |
(cond |
| 516 |
[(< (length rand*) 2) |
| 517 |
(decrement sc 1) |
| 518 |
(make-primref 'debug-call)] |
| 519 |
[else |
| 520 |
(let ([src/expr (car rand*)] |
| 521 |
[rator (cadr rand*)] |
| 522 |
[rands (cddr rand*)]) |
| 523 |
(let ([ctxt2 (make-app rands (app-ctxt ctxt))]) |
| 524 |
(let ([rator (E (operand-expr rator) |
| 525 |
ctxt2 |
| 526 |
(operand-env rator) |
| 527 |
(operand-ec rator) |
| 528 |
sc)]) |
| 529 |
(if (app-inlined ctxt2) |
| 530 |
(begin |
| 531 |
(set-app-inlined! ctxt #t) |
| 532 |
(residualize-operands rator (cons src/expr rands) sc)) |
| 533 |
(begin |
| 534 |
(decrement sc 1) |
| 535 |
(make-primref 'debug-call))))))]))) |
| 536 |
;;; |
| 537 |
(define (E-var x ctxt env ec sc) |
| 538 |
(ctxt-case ctxt |
| 539 |
[(e) (make-constant (void))] |
| 540 |
[else |
| 541 |
(let ([x (lookup x env)]) |
| 542 |
(let ([opnd (prelex-operand x)]) |
| 543 |
(if (and opnd (not (operand-inner-pending opnd))) |
| 544 |
(begin |
| 545 |
(dynamic-wind |
| 546 |
(lambda () (set-operand-inner-pending! opnd #t)) |
| 547 |
(lambda () (value-visit-operand! opnd)) |
| 548 |
(lambda () (set-operand-inner-pending! opnd #f))) |
| 549 |
(if (prelex-source-assigned? x) |
| 550 |
(residualize-ref x sc) |
| 551 |
(copy x opnd ctxt ec sc))) |
| 552 |
(residualize-ref x sc))))])) |
| 553 |
;;; |
| 554 |
(define (copy x opnd ctxt ec sc) |
| 555 |
(let ([rhs (result-expr (operand-value opnd))]) |
| 556 |
(struct-case rhs |
| 557 |
[(constant) rhs] |
| 558 |
[(prelex) |
| 559 |
(if (prelex-source-assigned? rhs) |
| 560 |
(residualize-ref x sc) |
| 561 |
(let ([opnd (prelex-operand rhs)]) |
| 562 |
(if (and opnd (operand-value opnd)) |
| 563 |
(copy2 rhs opnd ctxt ec sc) |
| 564 |
(residualize-ref rhs sc))))] |
| 565 |
[else (copy2 x opnd ctxt ec sc)]))) |
| 566 |
;;; |
| 567 |
(define (copy2 x opnd ctxt ec sc) |
| 568 |
(let ([rhs (result-expr (operand-value opnd))]) |
| 569 |
(struct-case rhs |
| 570 |
[(clambda) |
| 571 |
(ctxt-case ctxt |
| 572 |
[(v) (residualize-ref x sc)] |
| 573 |
[(p) (make-constant #t)] |
| 574 |
[(e) (make-constant (void))] |
| 575 |
[(app) |
| 576 |
(or (and (not (operand-outer-pending opnd)) |
| 577 |
(dynamic-wind |
| 578 |
(lambda () (set-operand-outer-pending! opnd #t)) |
| 579 |
(lambda () |
| 580 |
(call/cc |
| 581 |
(lambda (abort) |
| 582 |
(inline rhs ctxt empty-env |
| 583 |
(if (active-counter? ec) |
| 584 |
ec |
| 585 |
(make-counter |
| 586 |
(cp0-effort-limit) |
| 587 |
ctxt abort)) |
| 588 |
(make-counter |
| 589 |
(if (active-counter? sc) |
| 590 |
(counter-value sc) |
| 591 |
(cp0-size-limit)) |
| 592 |
ctxt abort))))) |
| 593 |
(lambda () (set-operand-outer-pending! opnd #f)))) |
| 594 |
(residualize-ref x sc))])] |
| 595 |
[(primref p) |
| 596 |
(ctxt-case ctxt |
| 597 |
[(v) rhs] |
| 598 |
[(p) (make-constant #t)] |
| 599 |
[(e) (make-constant (void))] |
| 600 |
[(app) (fold-prim p ctxt ec sc)])] |
| 601 |
[else (residualize-ref x sc)]))) |
| 602 |
(define (inline proc ctxt env ec sc) |
| 603 |
(define (get-case cases rand*) |
| 604 |
(define (compatible? x) |
| 605 |
(struct-case (clambda-case-info x) |
| 606 |
[(case-info label args proper) |
| 607 |
(cond |
| 608 |
[proper (= (length rand*) (length args))] |
| 609 |
[else (>= (length rand*) (- (length args) 1))])])) |
| 610 |
(cond |
| 611 |
[(memp compatible? cases) => car] |
| 612 |
[else #f])) |
| 613 |
(define (partition args rand*) |
| 614 |
(cond |
| 615 |
[(null? (cdr args)) |
| 616 |
(let ([r (car args)]) |
| 617 |
(let ([t* (map (lambda (x) (copy-var r)) rand*)]) |
| 618 |
(values '() t* r)))] |
| 619 |
[else |
| 620 |
(let ([x (car args)]) |
| 621 |
(let-values ([(x* t* r) (partition (cdr args) (cdr rand*))]) |
| 622 |
(values (cons x x*) t* r)))])) |
| 623 |
(struct-case proc |
| 624 |
[(clambda g cases cp free name) |
| 625 |
(let ([rand* (app-rand* ctxt)]) |
| 626 |
(struct-case (get-case cases rand*) |
| 627 |
[(clambda-case info body) |
| 628 |
(struct-case info |
| 629 |
[(case-info label args proper) |
| 630 |
(cond |
| 631 |
[proper |
| 632 |
(with-extended-env ((env args) (env args rand*)) |
| 633 |
(let ([body (E body (app-ctxt ctxt) env ec sc)]) |
| 634 |
(let ([result (make-let-binding args rand* body sc)]) |
| 635 |
(set-app-inlined! ctxt #t) |
| 636 |
result)))] |
| 637 |
[else |
| 638 |
(let-values ([(x* t* r) (partition args rand*)]) |
| 639 |
(with-extended-env ((env a*) |
| 640 |
(env (append x* t*) rand*)) |
| 641 |
(let ([rarg (make-operand |
| 642 |
(make-funcall (make-primref 'list) t*) |
| 643 |
env ec)]) |
| 644 |
(with-extended-env ((env b*) |
| 645 |
(env (list r) (list rarg))) |
| 646 |
(let ([result |
| 647 |
(make-let-binding a* rand* |
| 648 |
(make-let-binding b* (list rarg) |
| 649 |
(E body (app-ctxt ctxt) env ec sc) |
| 650 |
sc) |
| 651 |
sc)]) |
| 652 |
(set-app-inlined! ctxt #t) |
| 653 |
result)))))])])] |
| 654 |
[else |
| 655 |
(E proc 'v env ec sc)]))])) |
| 656 |
;;; |
| 657 |
(define (do-bind lhs* rhs* body ctxt env ec sc) |
| 658 |
(let ([rand* (map (lambda (x) (make-operand x env ec)) rhs*)]) |
| 659 |
(with-extended-env ((env lhs*) (env lhs* rand*)) |
| 660 |
(residualize-operands |
| 661 |
(make-let-binding lhs* rand* |
| 662 |
(E body ctxt env ec sc) |
| 663 |
sc) |
| 664 |
rand* sc)))) |
| 665 |
;;; |
| 666 |
(define (make-let-binding var* rand* body sc) |
| 667 |
(define (process1 var rand lhs* rhs*) |
| 668 |
(cond |
| 669 |
[(prelex-residual-referenced? var) |
| 670 |
(values |
| 671 |
(cons var lhs*) |
| 672 |
(cons (score-value-visit-operand! rand sc) rhs*))] |
| 673 |
[(prelex-residual-assigned? var) |
| 674 |
(set-operand-residualize-for-effect! rand #t) |
| 675 |
(values |
| 676 |
(cons var lhs*) |
| 677 |
(cons (make-constant (void)) rhs*))] |
| 678 |
[else |
| 679 |
(set-operand-residualize-for-effect! rand #t) |
| 680 |
(values lhs* rhs*)])) |
| 681 |
(define (process var* rand*) |
| 682 |
(cond |
| 683 |
[(null? var*) (values '() '())] |
| 684 |
[else |
| 685 |
(let ([var (car var*)] [rand (car rand*)]) |
| 686 |
(let-values ([(lhs* rhs*) (process (cdr var*) (cdr rand*))]) |
| 687 |
(process1 var rand lhs* rhs*)))])) |
| 688 |
(let-values ([(lhs* rhs*) (process var* rand*)]) |
| 689 |
(if (null? lhs*) body (make-bind lhs* rhs* body)))) |
| 690 |
;;; |
| 691 |
(define (fold-prim p ctxt ec sc) |
| 692 |
(define (get-value p ls) |
| 693 |
(call/cc |
| 694 |
(lambda (k) |
| 695 |
(with-exception-handler |
| 696 |
(lambda (con) |
| 697 |
(decrement ec 10) |
| 698 |
(k #f)) |
| 699 |
(lambda () |
| 700 |
(make-constant (apply (system-value p) ls))))))) |
| 701 |
(let ([rand* (app-rand* ctxt)]) |
| 702 |
(let ([info (primitive-info p rand*)]) |
| 703 |
(let ([result |
| 704 |
(or (and (info-effect-free? info) |
| 705 |
(ctxt-case (app-ctxt ctxt) |
| 706 |
[(e) (make-constant (void))] |
| 707 |
[(p) |
| 708 |
(cond |
| 709 |
[(info-result-true? info) |
| 710 |
(make-constant #t)] |
| 711 |
[(info-result-false? info) |
| 712 |
(make-constant #f)] |
| 713 |
[else #f])] |
| 714 |
[else #f])) |
| 715 |
(and (info-foldable? info) |
| 716 |
(let ([val* |
| 717 |
(map (lambda (x) (value-visit-operand! x)) rand*)]) |
| 718 |
(cond |
| 719 |
[(andmap constant? val*) |
| 720 |
(get-value p (map constant-value val*))] |
| 721 |
[else #f]))))]) |
| 722 |
(if result |
| 723 |
(begin |
| 724 |
(decrement ec 1) |
| 725 |
(for-each |
| 726 |
(lambda (x) |
| 727 |
(set-operand-residualize-for-effect! x #t)) |
| 728 |
rand*) |
| 729 |
(set-app-inlined! ctxt #t) |
| 730 |
result) |
| 731 |
(begin |
| 732 |
(decrement sc 1) |
| 733 |
(make-primref p))))))) |
| 734 |
;;; |
| 735 |
(define (residualize-ref x sc) |
| 736 |
(decrement sc 1) |
| 737 |
(set-prelex-residual-referenced?! x #t) |
| 738 |
x) |
| 739 |
;;; |
| 740 |
(define (build-conditional e0 e1 e2) |
| 741 |
(or (struct-case e0 |
| 742 |
[(funcall rator rand*) |
| 743 |
(struct-case rator |
| 744 |
[(primref op) |
| 745 |
(and (eq? op 'not) |
| 746 |
(= (length rand*) 1) |
| 747 |
(build-conditional (car rand*) e2 e1))] |
| 748 |
[else #f])] |
| 749 |
[else #f]) |
| 750 |
(make-conditional e0 e1 e2))) |
| 751 |
|
| 752 |
(define (E x ctxt env ec sc) |
| 753 |
(decrement ec 1) |
| 754 |
(struct-case x |
| 755 |
[(constant) (decrement sc 1) x] |
| 756 |
[(prelex) (E-var x ctxt env ec sc)] |
| 757 |
[(seq e0 e1) |
| 758 |
(mkseq (E e0 'e env ec sc) (E e1 ctxt env ec sc))] |
| 759 |
[(conditional e0 e1 e2) |
| 760 |
(let ([e0 (E e0 'p env ec sc)]) |
| 761 |
(struct-case (result-expr e0) |
| 762 |
[(constant k) |
| 763 |
(mkseq e0 (E (if k e1 e2) ctxt env ec sc))] |
| 764 |
[else |
| 765 |
(let ([ctxt (ctxt-case ctxt [(app) 'v] [else ctxt])]) |
| 766 |
(let ([e1 (E e1 ctxt env ec sc)] |
| 767 |
[e2 (E e2 ctxt env ec sc)]) |
| 768 |
(if (records-equal? e1 e2 ctxt) |
| 769 |
(mkseq e0 e1) |
| 770 |
(begin |
| 771 |
(decrement sc 1) |
| 772 |
(build-conditional e0 e1 e2)))))]))] |
| 773 |
[(assign x v) |
| 774 |
(mkseq |
| 775 |
(let ([x (lookup x env)]) |
| 776 |
(cond |
| 777 |
[(not (prelex-source-referenced? x)) |
| 778 |
;;; dead on arrival |
| 779 |
(E v 'e env ec sc)] |
| 780 |
[else |
| 781 |
(decrement sc 1) |
| 782 |
(set-prelex-residual-assigned?! x |
| 783 |
(prelex-source-assigned? x)) |
| 784 |
(make-assign x (E v 'v env ec sc))])) |
| 785 |
(make-constant (void)))] |
| 786 |
[(funcall rator rand*) |
| 787 |
(E-call rator |
| 788 |
(map (lambda (x) (make-operand x env ec)) rand*) |
| 789 |
env ctxt ec sc)] |
| 790 |
[(forcall name rand*) |
| 791 |
(decrement sc 1) |
| 792 |
(make-forcall name (map (lambda (x) (E x 'v env ec sc)) rand*))] |
| 793 |
[(primref name) |
| 794 |
(ctxt-case ctxt |
| 795 |
[(app) |
| 796 |
(case name |
| 797 |
[(debug-call) (E-debug-call ctxt ec sc)] |
| 798 |
[else (fold-prim name ctxt ec sc)])] |
| 799 |
[(v) (decrement sc 1) x] |
| 800 |
[else (make-constant #t)])] |
| 801 |
[(clambda g cases cp free name) |
| 802 |
(ctxt-case ctxt |
| 803 |
[(app) (inline x ctxt env ec sc)] |
| 804 |
[(p e) (make-constant #t)] |
| 805 |
[else |
| 806 |
(decrement sc 2) |
| 807 |
(make-clambda (gensym) |
| 808 |
(map |
| 809 |
(lambda (x) |
| 810 |
(struct-case x |
| 811 |
[(clambda-case info body) |
| 812 |
(struct-case info |
| 813 |
[(case-info label args proper) |
| 814 |
(with-extended-env ((env args) (env args #f)) |
| 815 |
(make-clambda-case |
| 816 |
(make-case-info (gensym) args proper) |
| 817 |
(E body 'v env ec sc)))])])) |
| 818 |
cases) |
| 819 |
cp free name)])] |
| 820 |
[(bind lhs* rhs* body) |
| 821 |
(do-bind lhs* rhs* body ctxt env ec sc)] |
| 822 |
[(fix lhs* rhs* body) |
| 823 |
(with-extended-env ((env lhs*) (env lhs* #f)) |
| 824 |
(for-each |
| 825 |
(lambda (lhs rhs) |
| 826 |
(set-prelex-operand! lhs (make-operand rhs env ec))) |
| 827 |
lhs* rhs*) |
| 828 |
(let ([body (E body ctxt env ec sc)]) |
| 829 |
(let ([lhs* (remp |
| 830 |
(lambda (x) |
| 831 |
(not (prelex-residual-referenced? x))) |
| 832 |
lhs*)]) |
| 833 |
(cond |
| 834 |
[(null? lhs*) body] |
| 835 |
[else |
| 836 |
(decrement sc 1) |
| 837 |
(make-fix lhs* |
| 838 |
(map (lambda (x) |
| 839 |
(let ([opnd (prelex-operand x)]) |
| 840 |
(decrement sc (+ (operand-size opnd) 1)) |
| 841 |
(value-visit-operand! opnd))) |
| 842 |
lhs*) |
| 843 |
body)]))))] |
| 844 |
[else |
| 845 |
(error who "invalid expression" x)])) |
| 846 |
(define empty-env '()) |
| 847 |
(define (lookup x env) |
| 848 |
(cond |
| 849 |
[(vector? env) |
| 850 |
(let f ([lhs* (vector-ref env 0)] [rhs* (vector-ref env 1)]) |
| 851 |
(cond |
| 852 |
[(null? lhs*) (lookup x (vector-ref env 2))] |
| 853 |
[(eq? x (car lhs*)) (car rhs*)] |
| 854 |
[else (f (cdr lhs*) (cdr rhs*))]))] |
| 855 |
[else x])) |
| 856 |
(define optimize-level |
| 857 |
(make-parameter 1 |
| 858 |
(lambda (x) |
| 859 |
(if (memv x '(0 1 2)) |
| 860 |
x |
| 861 |
(die 'optimize-level "valid levels are 0, 1, and 2"))))) |
| 862 |
(define (source-optimize expr) |
| 863 |
(define (source-optimize expr) |
| 864 |
(E expr 'v empty-env (passive-counter) (passive-counter))) |
| 865 |
(case (optimize-level) |
| 866 |
[(2) (source-optimize expr)] |
| 867 |
[(1) |
| 868 |
(parameterize ([cp0-size-limit 0]) |
| 869 |
(source-optimize expr))] |
| 870 |
[else expr])) |
| 871 |
) |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|