1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
|
; Copyright (c) 1993-2008 by Richard Kelsey. See file COPYING.
; temporary hack
;(define enqueue! enqueue)
;(define dequeue! dequeue)
(define-record-type form
(var ; variable being defined (if any)
(value) ; current value
;source ; one line of source code
(free) ; variables free in this form
)
(used? ; is the value used in the program
(exported? #f) ; true if the definition in this form is exported
(integrate 'okay) ; one of OKAY, YES, NO, PARTIAL
(aliases '()) ; variables that are aliases for this one
(shadowed '()) ; package variables that should be shadowed here
value-type ; value's type
(dependency-index #f) ; index of this form in the data dependent order
lambdas ; list of all non-cont lambdas in this form
(clients '()) ; forms that use this one's variable
(providers '()) ; forms that define a variable used by this one
(type #f) ; one of LAMBDA, INTEGRATE, INITIALIZE or
; #F for unfinished forms
merge ; slot used by form-merging code
temp ; handy slot
))
(define-record-discloser type/form
(lambda (form)
`(form ,(variable-name (form-var form)))))
(define (make-form var value free)
(let ((form (form-maker var value free)))
(if (maybe-variable->form var)
(error "more than one definition of ~S" (variable-name var)))
(set-variable-flags! var `((form . ,form) . ,(variable-flags var)))
form))
(define (pp-one-line x)
(call-with-string-output-port
(lambda (p)
(write-one-line p 70 (lambda (p) (write x p))))))
(define (form-node form)
(let ((value (form-value form)))
(if (node? value)
value
(bug "form's value is not a node ~S ~S" form value))))
(define (set-form-node! form node lambdas)
(set-node-flag! node form)
(set-form-value! form node)
(set-form-lambdas! form lambdas))
(define (node-form node)
(let ((form (node-flag (node-base node))))
(if (form? form)
form
(bug "node ~S (~S) not in any form" node (node-base node)))))
(define (suspend-form-use! form)
(set-form-lambdas! form (make-lambda-list))
(set-node-flag! (form-node form) form))
(define (use-this-form! form)
(initialize-lambdas)
(also-use-this-form! form))
(define (also-use-this-form! form)
(add-lambdas (form-lambdas form))
(set-node-flag! (form-node form) #f))
(define (form-name form)
(variable-name (form-var form)))
(define (make-form-unused! form)
(set-form-type! form 'unused)
(cond ((node? (form-value form))
(erase (form-value form))
(set-form-value! form #f)
(set-form-lambdas! form #f))))
; notes on writing and reading forms
; What we really need here are forms.
; What to do? Can read until there are no missing lambdas = end of form
; Need the variables as well.
; (form index type var source? clients providers integrate?)
; clients and providers are lists of indicies
; can get lambdas automatically
;(define (write-cps-file file forms)
; (let ((port (make-tracking-output-port (open-output-file file))))
; (reset-pp-cps)
; (walk (lambda (f)
; (write-form f port))
; (sort-list forms
; (lambda (f1 f2)
; (< (form-index f1) (form-index f2)))))
; (close-output-port port)))
;(define (write-form form port)
; (format port "(FORM ~D ~S ~S "
; (form-index form)
; (form-type form)
; (form-integrate form))
; (if (form-var form)
; (print-variable-name (form-var form) port)
; (format port "#f"))
; (format port "~% ~S" (map form-index (form-clients form)))
; (rereadable-pp-cps (form-value form) port)
; (format port ")~%~%"))
;------------------------------------------------------------------------------
; Put the forms that do not reference any other forms' variables in a queue.
; Every form gets a list of forms that use its variable and a list of forms
; whose variables it uses.
(define (sort-forms forms)
(let ((queue (make-queue)))
(for-each (lambda (f)
(set-variable-flag! (form-var f) f))
forms)
(let ((forms (really-remove-unreferenced-forms
forms
set-providers-using-free)))
(for-each (lambda (f)
(if (null? (form-providers f))
(enqueue! queue f)))
(reverse forms))
(for-each (lambda (f)
(set-variable-flag! (form-var f) #f))
forms)
(values forms (make-form-queue queue forms)))))
(define (set-providers-using-free form)
(let loop ((vars (form-free form)) (provs '()))
(cond ((null? vars)
(set-form-providers! form provs))
((variable-flag (car vars))
=> (lambda (prov)
(set-form-clients! prov (cons form (form-clients prov)))
(loop (cdr vars) (cons prov provs))))
(else
(loop (cdr vars) provs)))))
(define (make-form-queue ready forms)
(let ((index 0))
(lambda ()
(let loop ()
(cond ((not (queue-empty? ready))
(let ((form (dequeue! ready)))
(set-form-dependency-index! form index)
(for-each (lambda (f)
(set-form-providers! f (delq! form (form-providers f)))
(if (and (null? (form-providers f))
(not (form-dependency-index f))
(form-used? f))
(enqueue! ready f)))
(form-clients form))
(set! index (+ index 1))
form))
((find-dependency-loop ready forms)
=> (lambda (rest)
(set! forms rest)
(loop)))
(else #f))))))
; Find a circular dependence between the remaining forms.
(define (find-dependency-loop queue forms)
(let ((forms (do ((forms forms (cdr forms)))
((or (null? forms)
(not (form-dependency-index (car forms))))
forms))))
(cond ((null? forms)
#f)
(else
;;(format #t "Dependency loop!~%")
(let ((form (really-find-dependency-loop forms)))
(if (not (every? (lambda (f) (eq? 'no (form-integrate f)))
(form-providers form)))
(set-form-integrate! form 'no))
(set-form-providers! form '())
(enqueue! queue form)
forms)))))
(define (really-find-dependency-loop forms)
(for-each (lambda (f) (set-form-temp! f #f))
forms)
(let label ((form (car forms)))
(cond ((form-temp form)
(break-dependency-loop (filter (lambda (f)
(and (form-temp f) (form-var f)))
forms)))
(else
(set-form-temp! form #t)
(cond ((any-map label (form-providers form))
=> (lambda (res)
(set-form-temp! form #f)
res))
(else
(set-form-temp! form #f)
#f))))))
(define (any-map proc list)
(let loop ((list list))
(cond ((null? list)
#f)
((proc (car list))
=> identity)
(else
(loop (cdr list))))))
(define *loop-forms* #f)
(define (break-dependency-loop forms)
(or (first (lambda (f)
(or (every? (lambda (f)
(eq? 'no (form-integrate f)))
(form-providers f))
(memq? f (form-providers f))
(and (scheme-node? (form-value f))
(scheme-literal-node? (form-value f)))))
forms)
(begin (set! *loop-forms* forms)
(let ((f (breakpoint "Break dependency loop: *loop-forms* = ~S" forms)))
(set! *loop-forms* #f)
f))))
(define scheme-literal-node?
((structure-ref nodes node-predicate) 'literal))
(define scheme-node?
(structure-ref nodes node?))
;----------------------------------------------------------------
(define (variable-set!? var)
(memq 'set! (variable-flags var)))
(define (note-variable-set!! var)
(if (not (variable-set!? var))
(set-variable-flags! var (cons 'set! (variable-flags var)))))
;------------------------------------------------------------------------------
; Turn expression into nodes and simplify it.
; Still to do:
; Get representations of data values
; Need to constant fold vector slots, including detection of modifications
; and single uses.
(define (expand-and-simplify-form form)
(initialize-lambdas)
(let* ((value (form-value form))
(node (if (variable? value)
(make-reference-node value)
(x->cps (form-value form) (form-name form)))))
(cond ((variable-set!? (form-var form))
(set-form-type! form 'initialize)
(set-form-node! form node '())
"settable")
((reference-node? node)
(let ((var (reference-variable node)))
(add-known-form-value! form node)
(cond ((maybe-variable->form var)
=> (lambda (f)
(set-form-aliases! f
`(,(form-var form)
,@(form-aliases form)
. ,(form-aliases f))))))
(set-form-type! form 'alias)
(erase node)
(set-form-value! form var)
"alias"))
((literal-node? node)
(expand-and-simplify-literal node form))
((lambda-node? node)
(expand-and-simplify-lambda node form))
(else
(bug "funny form value ~S" node)))))
; This could pay attention to immutability.
(define (atomic? value)
(not (or (vector? value)
(pair? value))))
(define (expand-and-simplify-literal node form)
(let ((value (literal-value node)))
(cond ((unspecific? value)
(format #t "~%Warning: variable `~S' has no value and is not SET!~%"
(form-name form))
(set-form-value! form node)
(set-form-lambdas! form '())
(set-form-integrate! form 'no)
(set-form-type! form 'unused)
"constant")
((atomic? value)
(add-known-form-value! form node)
(set-form-value! form node)
(set-form-lambdas! form '())
"constant")
(else
(set-form-node! form (stob->node value) '())
(set-form-type! form 'stob)
"consed"))))
; Make a call node containing the contents of the stob so that any
; variables will be seen as referenced and any integrable values will
; be integrated.
; Only works for vectors at this point.
; MAKE-VECTOR is a randomly chosen primop, almost anything could be used.
(define (stob->node value)
(let* ((contents '())
(add! (lambda (x) (set! contents (cons x contents)))))
(cond ((vector? value)
(do ((i 0 (+ i 1)))
((>= i (vector-length value)))
(add! (vector-ref value i))))
(else
(error "unknown kind of stob value ~S" value)))
(let ((call (make-call-node (get-prescheme-primop 'make-vector)
(+ 1 (length contents))
0))
(node (make-lambda-node 'stob 'init '())))
(attach call 0 (make-literal-node value #f)) ; save for future use
(do ((i 1 (+ i 1))
(cs (reverse contents) (cdr cs)))
((null? cs))
(let ((x (car cs)))
(attach call i (if (variable? x)
(make-reference-node x)
(make-literal-node x type/unknown)))))
(attach-body node call)
(simplify-args call 1)
node)))
(define (add-known-form-value! form value)
(let ((node (if (variable? value)
(make-reference-node value)
value))
(var (form-var form)))
(set-form-type! form 'integrate)
(cond ((or (literal-node? node)
(reference-node? node)
(and (call-node? node)
(trivial? node)))
(add-variable-known-value! var (node->vector node))
(if (variable? value)
(erase node)))
((lambda-node? node)
(add-variable-simplifier! var (make-inliner (node->vector node))))
(else
(bug "form's value ~S is not a value" value)))))
(define (make-inliner vector)
(lambda (call)
(let ((proc (call-arg call 1)))
(replace proc (reconstruct-value vector proc call)))))
(define (reconstruct-value value proc call)
(let ((has-type (maybe-follow-uvar (variable-type (reference-variable proc))))
(node (vector->node value)))
(if (type-scheme? has-type)
(instantiate-type&value has-type node proc))
node))
(define (expand-and-simplify-lambda node form)
(simplify-all node (form-name form))
(let ((lambdas (make-lambda-list))
(status (duplicate-form? form node)))
(if status
(add-known-form-value! form node))
(set-form-node! form node lambdas)
(set-form-type! form 'lambda)
(set-form-free! form #f) ; old value no longer valid
status))
(define *duplicate-lambda-size* 10)
(define (set-duplicate-lambda-size! n)
(set! *duplicate-lambda-size* n))
(define (duplicate-form? form node)
(cond ((or (variable-set!? (form-var form))
(eq? 'no (form-integrate form)))
#f)
((small-node? node *duplicate-lambda-size*)
"small")
((eq? 'yes (form-integrate form))
"by request")
; ((called-arguments? node)
; "called arguments")
(else
#f)))
(define (called-arguments? node)
(any? (lambda (v)
(any? (lambda (n)
(eq? n (called-node (node-parent n))))
(variable-refs v)))
(cdr (lambda-variables node))))
;------------------------------------------------------------------------------
(define (integrate-stob-form form)
(if (and (eq? 'stob (form-type form))
(elide-aliases! form)
(not (form-exported? form))
(every? cell-use (variable-refs (form-var form))))
(let* ((var (form-var form))
(ref (car (variable-refs var)))
(call (lambda-body (form-value form))))
; could fold any fixed references - do it later
(cond ((and (null? (cdr (variable-refs var)))
(called-node? (cell-use ref)))
(format #t "computed-goto: ~S~%" (variable-name var))
(make-computed-goto form))))))
(define (cell-use node)
(let ((parent (node-parent node)))
(if (and (call-node? parent)
(eq? 'vector-ref (primop-id (call-primop parent))))
parent
#f)))
(define (elide-aliases! form)
(not (or-map (lambda (f)
(switch-references! (form-var f) (form-var form))
(form-exported? f))
(form-aliases form))))
(define (switch-references! from to)
(for-each (lambda (r)
(set-reference-variable! r to))
(variable-refs from))
(set-variable-refs! to (append (variable-refs from) (variable-refs to))))
;------------------------------------------------------------------------------
(define (resimplify-form form)
(let ((node (form-value form)))
(cond ((and (node? node)
(not (eq? 'stob (form-type form)))
(not (node-simplified? node)))
(use-this-form! form)
(simplify-node node)
(suspend-form-use! form)))))
;------------------------------------------------------------------------------
; This is removes all forms that are not ultimately referenced from some
; exported form.
(define (add-form-provider! form provider)
(if (not (memq? provider (form-providers form)))
(set-form-providers!
form
(cons provider (form-providers form)))))
(define (variable->form var)
(or (maybe-variable->form var)
(bug "variable ~S has no form" var)))
(define (maybe-variable->form var)
(cond ((flag-assq 'form (variable-flags var))
=> cdr)
(else
#f)))
(define (remove-unreferenced-forms forms)
(really-remove-unreferenced-forms forms set-form-providers))
(define (really-remove-unreferenced-forms forms set-form-providers)
(receive (exported others)
(partition-list form-exported? forms)
(for-each (lambda (f)
(set-form-providers! f '())
(set-form-clients! f '())
(set-form-used?! f (form-exported? f)))
forms)
(for-each set-form-providers forms)
(propogate-used?! exported)
(append (remove-unused-forms others) exported)))
(define (set-form-providers form)
(for-each (lambda (n)
(add-form-provider! (node-form n) form))
(variable-refs (form-var form)))
(if (eq? (form-type form) 'alias)
(add-form-provider! form (variable->form (form-value form)))))
(define (propogate-used?! forms)
(let loop ((to-do forms))
(if (not (null? to-do))
(let loop2 ((providers (form-providers (car to-do)))
(to-do (cdr to-do)))
(if (null? providers)
(loop to-do)
(loop2 (cdr providers)
(let ((p (car providers)))
(cond ((form-used? p)
to-do)
(else
(set-form-used?! p #t)
(cons p to-do))))))))))
; Actually remove forms that are not referenced.
(define (remove-unused-forms forms)
; (format #t "Removing unused forms~%")
(filter (lambda (f)
(cond ((or (not (form-used? f))
)
;(let ((value (form-value f)))
; (and (quote-exp? value)
; (external-value? (quote-exp-value value))))
; (format #t " ~S~%" (variable-name (form-var f)))
(erase-variable (form-var f))
(cond ((node? (form-value f))
(erase (form-value f))
(set-form-value! f #f)
(set-form-lambdas! f '())))
#f)
(else #t)))
forms))
;------------------------------------------------------------
; Total yucko.
; (unknown-call (lambda e-vars e-body)
; protocol
; (vector-ref x offset)
; . args)
; =>
; (let (lambda ,vars
; (computed-goto
; ...
; (lambda ()
; (unknown-call (lambda ,copied-evars
; (jump ,(car vars) ,copied-evars))
; ,(vector-ref proc-vector i)
; . ,(cdr vars)))
; ...
; '((offsets ...) ...) ; offsets for each continuation
; ,offset))
; ,exit
; . ,args)
(define (make-computed-goto form)
(let* ((ref (car (variable-refs (form-var form))))
(in-form (node-form ref))
(entries (vector->offset-map (call-args (lambda-body (form-node form))))))
(use-this-form! in-form)
(also-use-this-form! form)
(really-make-computed-goto (node-parent ref) entries)
(erase (form-node form))
(set-form-value! form #f)
(set-form-lambdas! form #f)
(simplify-node (form-node in-form))
(suspend-form-use! in-form)))
; Returns a list ((<node> . <offsets>) ...) where <offsets> are where <node>
; was found in VECTOR. The first element of VECTOR is a marker which we
; pretend isn't there.
;
; This would be more effective if done by a simplifier after the continuations
; had been simplified.
(define (vector->offset-map vector)
(let loop ((i 0) (res '()))
(if (= (+ i 1) (vector-length vector))
(reverse (map (lambda (p)
(cons (car p) (reverse (cdr p))))
res))
(let ((n (vector-ref vector (+ i 1))))
(loop (+ i 1)
(cond ((first (lambda (p)
(node-equal? n (car p)))
res)
=> (lambda (p)
(set-cdr! p (cons i (cdr p)))
res))
(else
(cons (list n i) res))))))))
(define (really-make-computed-goto vec-ref entries)
(let* ((exits (length entries))
(offset (call-arg vec-ref 1))
(vector-call (node-parent vec-ref))
(args (sub-vector->list (call-args vector-call) 3))
(call (make-call-node (get-prescheme-primop 'computed-goto)
(+ 2 exits)
exits))
(arg-vars (map (lambda (arg) (make-variable 't (node-type arg)))
args))
(protocol (literal-value (call-arg vector-call 2)))
(cont (call-arg vector-call 0)))
(for-each detach args)
(attach call exits (make-literal-node (map cdr entries) #f))
(attach call (+ exits 1) (detach offset))
(receive (top continuations)
(if (reference-node? cont)
(make-computed-goto-tail-conts call args arg-vars entries cont protocol)
(make-computed-goto-conts call args arg-vars entries cont protocol))
(do ((i 0 (+ i 1))
(l continuations (cdr l)))
((= i exits))
(attach call i (car l)))
(replace-body vector-call top))))
(define (make-computed-goto-tail-conts call args arg-vars entries cont protocol)
(let-nodes ((top (let 1 l1 . args))
(l1 arg-vars call))
(values top (map (lambda (p)
(computed-goto-tail-exit
(detach (car p))
protocol
(reference-variable cont)
arg-vars))
entries))))
(define (computed-goto-tail-exit node protocol cont-var arg-vars)
(let ((args (map make-reference-node arg-vars)))
(let-nodes ((l1 () (unknown-tail-call 0 (* cont-var)
node
'(protocol #f) . args)))
l1)))
(define (make-computed-goto-conts call args arg-vars entries cont protocol)
(let ((cont-vars (lambda-variables cont))
(cont-type (make-arrow-type (map variable-type
(lambda-variables cont))
type/null)))
(detach cont)
(change-lambda-type cont 'jump)
(let-nodes ((top (let 1 l1 cont . args))
(l1 ((j cont-type) . arg-vars) call))
(values top
(map (lambda (p)
(computed-goto-exit (detach (car p))
protocol
arg-vars
j
cont-vars))
entries)))))
(define (computed-goto-exit node protocol arg-vars cont-var cont-vars)
(let* ((cont-vars (map copy-variable cont-vars))
(cont-args (map make-reference-node cont-vars))
(args (map make-reference-node arg-vars)))
(let-nodes ((l1 () (unknown-call 1 l2 node '(protocol #f) . args))
(l2 cont-vars (jump 0 (* cont-var) . cont-args)))
l1)))
|