home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / compiler / base / subprb.scm < prev    next >
Text File  |  1999-01-02  |  6KB  |  157 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Id: subprb.scm,v 4.8 1999/01/02 06:06:43 cph Exp $
  4.  
  5. Copyright (c) 1988, 1989, 1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. |#
  21.  
  22. ;;;; Subproblem Type
  23.  
  24. (declare (usual-integrations))
  25.  
  26. #|
  27.  
  28. Subproblems come in two forms, canonical and non-canonical.  In a
  29. canonical subproblem, the `prefix' is always exited by a return
  30. statement whose operator is the subproblem's `continuation'.  The
  31. `rvalue' is always the parameter of the `continuation'.
  32.  
  33. In a non-canonical subproblem, there is no `continuation' -- the
  34. `rvalue' is sufficiently simple that no complex computation is
  35. required to compute its value.  Instead, the `prefix' is some setup
  36. code that must be executed for effect, while the value of the
  37. subproblem is just `rvalue'.
  38.  
  39. The non-canonical subproblem is used as an optimization by several
  40. parts of the compiler, where better code can be generated if it is
  41. known that the continuation need not be used.
  42.  
  43. |#
  44.  
  45. (define-structure (subproblem
  46.            (constructor make-subproblem
  47.                 (prefix continuation rvalue)))
  48.   (prefix false read-only true)
  49.   (continuation false read-only true)
  50.   (rvalue false read-only true)
  51.   (simple? 'UNKNOWN)
  52.   (free-variables 'UNKNOWN))
  53.  
  54. (define-integrable (subproblem-entry-node subproblem)
  55.   (cfg-entry-node (subproblem-prefix subproblem)))
  56.  
  57. (define-integrable (subproblem-canonical? subproblem)
  58.   (procedure? (subproblem-continuation subproblem)))
  59.  
  60. (define-integrable (subproblem-type subproblem)
  61.   (continuation*/type (subproblem-continuation subproblem)))
  62.  
  63. (define-integrable (set-subproblem-type! subproblem type)
  64.   (set-continuation*/type! (subproblem-continuation subproblem) type))
  65.  
  66. (define-integrable (subproblem-register subproblem)
  67.   (continuation*/register (subproblem-continuation subproblem)))
  68.  
  69. (define (subproblem-context subproblem)
  70.   (continuation*/context (subproblem-continuation subproblem)))
  71.  
  72. (define (continuation*/type continuation)
  73.   (if (procedure? continuation)
  74.       (continuation/type continuation)
  75.       (virtual-continuation/type continuation)))
  76.  
  77. (define (set-continuation*/type! continuation type)
  78.   (if (procedure? continuation)
  79.       (set-continuation/type! continuation type)
  80.       (set-virtual-continuation/type! continuation type)))
  81.  
  82. (define (continuation*/register continuation)
  83.   (if (procedure? continuation)
  84.       (continuation/register continuation)
  85.       (virtual-continuation/register continuation)))
  86.  
  87. (define (continuation*/context continuation)
  88.   (let ((continuation/context
  89.      (lambda (continuation)
  90.        (make-reference-context
  91.         (block-parent (continuation/block continuation))))))
  92.     (cond ((procedure? continuation)
  93.        (continuation/context continuation))
  94.       ((virtual-continuation/reified? continuation)
  95.        (continuation/context
  96.         (virtual-continuation/reification continuation)))
  97.       (else
  98.        (virtual-continuation/context continuation)))))
  99.  
  100. ;;;; Virtual Continuations
  101.  
  102. ;;; These are constructed in the FG generation phase for the purpose
  103. ;;; of delaying generation of real continuations until the last
  104. ;;; possible moment.  After the FG generation, non-reified virtual
  105. ;;; continuations are used to hold several values that normally would
  106. ;;; have resided in the real continuation.
  107.  
  108. (define-structure (virtual-continuation
  109.            (constructor virtual-continuation/%make)
  110.            (conc-name virtual-continuation/)
  111.            (print-procedure
  112.             (standard-unparser (symbol->string 'VIRTUAL-CONTINUATION)
  113.               (lambda (state continuation)
  114.             (let ((type (virtual-continuation/type continuation)))
  115.               (if type
  116.                   (unparse-object
  117.                    state
  118.                    (enumeration/index->name continuation-types
  119.                             type))))))))
  120.   context
  121.   parent
  122.   type
  123.   debugging)
  124.  
  125. (define-integrable (virtual-continuation/make block type)
  126.   ;; Used exclusively after FG generation.
  127.   (virtual-continuation/%make block false type false))
  128.  
  129. (define-integrable (virtual-continuation/reified? continuation)
  130.   (not (virtual-continuation/type continuation)))
  131.  
  132. (define-integrable virtual-continuation/reification
  133.   virtual-continuation/context)
  134.  
  135. (define (virtual-continuation/reify! continuation)
  136.   ;; This is used only during FG generation when it is decided that we
  137.   ;; need a real continuation to handle a subproblem.
  138.   (if (virtual-continuation/type continuation)
  139.       (let ((reification
  140.          (make-continuation
  141.           (virtual-continuation/context continuation)
  142.           (virtual-continuation/parent continuation)
  143.           (virtual-continuation/type continuation))))
  144.     (set-continuation/debugging-info!
  145.      reification
  146.      (virtual-continuation/debugging continuation))
  147.     (set-virtual-continuation/context! continuation reification)
  148.     (set-virtual-continuation/parent! continuation false)
  149.     (set-virtual-continuation/type! continuation false)
  150.     reification)
  151.       (virtual-continuation/context continuation)))
  152.  
  153. (define (virtual-continuation/register continuation)
  154.   (or (virtual-continuation/parent continuation)
  155.       (let ((register (rtl:make-pseudo-register)))
  156.     (set-virtual-continuation/parent! continuation register)
  157.     register)))