home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / t3_1 / sources.lha / sources / comp / front_end / assign.t < prev    next >
Encoding:
Text File  |  1988-02-05  |  5.2 KB  |  130 lines

  1. (herald (front_end assign)
  2.   (env t (orbit_top defs)))
  3.  
  4. ;;; Copyright (c) 1985 Yale University
  5. ;;;     Authors: N Adams, R Kelsey, D Kranz, J Philbin, J Rees.
  6. ;;; This material was developed by the T Project at the Yale University Computer 
  7. ;;; Science Department.  Permission to copy this software, to redistribute it, 
  8. ;;; and to use it for any purpose is granted, subject to the following restric-
  9. ;;; tions and understandings.
  10. ;;; 1. Any copy made of this software must include this copyright notice in full.
  11. ;;; 2. Users of this software agree to make their best efforts (a) to return
  12. ;;;    to the T Project at Yale any improvements or extensions that they make,
  13. ;;;    so that these may be included in future releases; and (b) to inform
  14. ;;;    the T Project of noteworthy uses of this software.
  15. ;;; 3. All materials developed as a consequence of the use of this software
  16. ;;;    shall duly acknowledge such use, in accordance with the usual standards
  17. ;;;    of acknowledging credit in academic research.
  18. ;;; 4. Yale has made no warrantee or representation that the operation of
  19. ;;;    this software will be error-free, and Yale is under no obligation to
  20. ;;;    provide any services, by way of maintenance, update, or otherwise.
  21. ;;; 5. In conjunction with products arising from the use of this material,
  22. ;;;    there shall be no use of the name of the Yale University nor of any
  23. ;;;    adaptation thereof in any advertising, promotional, or sales literature
  24. ;;;    without prior written consent from Yale in each case.
  25. ;;;
  26.  
  27. ;;;         ADDING CELLS FOR SIDE-AFFECTED LEXICAL VARIABLES
  28. ;;;============================================================================
  29. ;;;  There are two versions of this depending on whether or not MAKE-CELL
  30. ;;; takes an argument.
  31. ;;;
  32. ;;; (lambda (x) ... x ... (set x ...) ... (locative x) ...)
  33. ;;;   ==>
  34. ;;; (lambda (x)
  35. ;;;   (let ((x' (make-cell)))
  36. ;;;     (set-location x' x)
  37. ;;;      ... (location x') ... (set-location x' ...) ... x' ...))
  38. ;;;
  39. ;;; A warning is issued if a variable is set but never used.
  40.  
  41. (define (introduce-cell var)
  42.   (let ((node (variable-binder var))
  43.         (new-var (create-variable (variable-name var))))
  44.     (hack-references var new-var)
  45.     (let-nodes ((call (($ primop/make-cell) 1 (^ cont1)))
  46.                  (cont1 (() (v new-var))
  47.                    (($ primop/set-location) 1
  48.                     (^ cont2) ($ primop/cell-value) (* var) (* new-var)))
  49.                   (cont2 (#f) ()))
  50.       (insert-call call cont2 node))))
  51.  
  52. ;;; (lambda (x) ... x ... (set x ...) ... (locative x) ...)
  53. ;;;   ==>
  54. ;;; (lambda (x)
  55. ;;;   (let ((x' (make-cell x)))
  56. ;;;      ... (location x') ... (set-location x' ...) ... x' ...))
  57.  
  58. (define (hack-references var new-var)
  59.   (if (not (any? true? (map-refs-safely (lambda (ref)
  60.                                           (hack-reference ref new-var))
  61.                                         var)))
  62.       (user-message 'warning
  63.                     "variable ~S is set but never referenced"
  64.                     nil
  65.                     (variable-name var))))
  66.  
  67. ;;; Replace references to variables with indirections through locatives.
  68. ;;;   x              ==>  (contents x')
  69. ;;;   (set x y)      ==>  (set-contents x' y)
  70. ;;;   (locative x)   ==>  x'
  71. ;;; Returns T if the variable is being used, NIL if it is being set.
  72.  
  73. (define (hack-reference ref new-var)
  74.   (let* ((parent (node-parent ref)) 
  75.          (proc (call-proc parent)))
  76.     (cond ((or (neq? (node-role ref) (call-arg 2))
  77.                (not (primop-node? proc)))
  78.            (dereference parent ref new-var)
  79.            t)
  80.           ((eq? (primop-value proc) primop/*set-var)
  81.            (replace parent (assigner parent new-var))
  82.            nil)
  83.           ((eq? (primop-value proc) primop/*locative)
  84.            (replace-call-with-value parent 
  85.                                     (create-reference-node new-var))
  86.            t)
  87.           (else
  88.            (dereference parent ref new-var)
  89.            t))))
  90.  
  91. ;;;  x ==> (contents-location cell-value x')
  92.  
  93. (define (dereference call ref new-var)
  94.   (let ((new-v (create-variable (variable-name new-var))))
  95.     (let-nodes ((new-call (($ primop/contents-location)
  96.                            1
  97.                            (^ l-node)
  98.                            ($ primop/cell-value)
  99.                            (* new-var)))
  100.                  (l-node (() (v new-v)) ()))
  101.       (insert-call new-call l-node (node-parent call))
  102.       (replace ref (create-reference-node new-v)))))
  103.  
  104. ;;;  (set x y) ==> (set-location cell-value y x')
  105.  
  106. (define (assigner call new-var)
  107.   (let ((node (create-call-node 5 1)))
  108.     (relate call-proc node (create-primop-node primop/set-location))
  109.     (relate-four-call-args node
  110.                            (detach ((call-arg 1) call))
  111.                            (create-primop-node primop/cell-value)
  112.                            (detach ((call-arg 3) call))
  113.                            (create-reference-node new-var)) 
  114.     node))
  115.  
  116. ;;; This stuff is here to keep all of the MAKE-CELL calls in the same file.
  117.  
  118. ;;; Make a thunk that creates a new cell.
  119.  
  120. (define (labels-make-cell-thunk)
  121.   (let-nodes ((l1 (#f c1) (($ primop/make-cell) 1 (^ l2)))
  122.                (l2 (#f v) (($ primop/set-location) 1
  123.                            (^ l3) ($ primop/cell-value)
  124.                            ''uninitialized-labels (* v)))
  125.                 (l3 (#f) ((* c1) 0 (* v))))
  126.     l1))
  127.  
  128.  
  129.  
  130.