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 / rtlgen / fndblk.scm next >
Text File  |  1999-01-02  |  6KB  |  150 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Id: fndblk.scm,v 4.12 1999/01/02 06:06:43 cph Exp $
  4.  
  5. Copyright (c) 1988, 1990, 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. ;;;; RTL Generation: Environment Locatives
  23. ;;; package: (compiler rtl-generator find-block)
  24.  
  25. (declare (usual-integrations))
  26.  
  27. (define (find-block context extra-offset end-block?)
  28.   (find-block/loop (reference-context/block context)
  29.            context
  30.            end-block?
  31.            (find-block/initial context extra-offset)))
  32.  
  33. (define (find-block/initial context extra-offset)
  34.   (let ((block (reference-context/block context)))
  35.     (if (not block)
  36.     (error "find-block/initial: Null block!" block))
  37.     (enumeration-case block-type (block-type block)
  38.      ((STACK)
  39.       (stack-locative-offset (rtl:make-fetch register:stack-pointer)
  40.                  (+ extra-offset
  41.                 (reference-context/offset context))))
  42.      ((IC)
  43.       (rtl:make-fetch register:environment))
  44.      (else
  45.       (error "Illegal initial block type" block)))))
  46.  
  47. (define (find-block/loop block context end-block? locative)
  48.   (cond ((null? block)
  49.      (error "find-block/loop: Null block!" block)
  50.      (values block locative))
  51.     ((or (end-block? block)
  52.          (ic-block? block))
  53.      (values block locative))
  54.     (else
  55.      (find-block/loop
  56.       (block-parent block)
  57.       context
  58.       end-block?
  59.       ((find-block/parent-procedure block) block context locative)))))
  60.  
  61. (define (find-block/parent-procedure block)
  62.   (enumeration-case block-type (block-type block)
  63.     ((STACK)
  64.      (let ((parent (block-parent block)))
  65.        (cond ((not (procedure/closure? (block-procedure block)))
  66.           (if parent
  67.           (enumeration-case block-type (block-type parent)
  68.            ((STACK) internal-block/parent-locative)
  69.            ((IC) stack-block/static-link-locative)
  70.            ((CLOSURE) (error "Closure parent of open procedure" block))
  71.            (else (error "Illegal procedure parent" parent)))
  72.           (error "Block has no parent" block)))
  73.          ((procedure/trivial-closure? (block-procedure block))
  74. #|
  75.           ;; This case cannot signal an error because of the way that
  76.           ;; find-block/loop is written.  The locative for the
  77.           ;; parent is needed, although it will be ignored by the
  78.           ;; receiver once it finds out that the block is
  79.           ;; ic/non-existent.  The references are found by using
  80.           ;; the variable caches.
  81.           (error "Block corresponds to trivial closure")
  82. |#
  83.           trivial-closure/bogus-locative)
  84.          ((not parent)
  85.           (error "Block has no parent" block))
  86.          (else
  87.           (enumeration-case
  88.            block-type (block-type parent)
  89.            ((STACK) (error "Closure has a stack parent" block))
  90.            ((IC) stack-block/parent-of-dummy-closure-locative)
  91.            ((CLOSURE) stack-block/closure-parent-locative)
  92.            (else (error "Illegal procedure parent" parent)))))))
  93.     ((CLOSURE) closure-block/parent-locative)
  94.     ((CONTINUATION) continuation-block/parent-locative)
  95.     (else (error "Illegal parent block type" block))))
  96.  
  97. (define (internal-block/parent-locative block context locative)
  98.   (let ((link (block-stack-link block)))
  99.     (if link
  100.     (let ((end-block?
  101.            (let ((end-block (block-parent block)))
  102.          (lambda (block) (eq? block end-block)))))
  103.       (with-values
  104.           (lambda ()
  105.         (find-block/loop
  106.          link
  107.          context
  108.          end-block?
  109.          (stack-locative-offset locative (block-frame-size block))))
  110.         (lambda (end-block locative)
  111.           (if (not (end-block? end-block))
  112.           (error "Couldn't find internal block parent!" block))
  113.           locative)))
  114.     (stack-block/static-link-locative block context locative))))
  115.  
  116. (define (continuation-block/parent-locative block context locative)
  117.   context
  118.   (stack-locative-offset locative
  119.              (+ (block-frame-size block)
  120.                 (continuation/offset (block-procedure block)))))
  121.  
  122. (define (stack-block/static-link-locative block context locative)
  123.   (if (reference-context/adjacent-parent? context block)
  124.       (stack-locative-offset locative (block-frame-size block))
  125.       (rtl:make-fetch
  126.        (stack-locative-offset locative (-1+ (block-frame-size block))))))
  127.  
  128. (define (stack-block/closure-parent-locative block context locative)
  129.   context
  130.   (rtl:make-fetch
  131.    (stack-locative-offset
  132.     locative
  133.     (procedure-closure-offset (block-procedure block)))))
  134.  
  135. (define (trivial-closure/bogus-locative block context locative)
  136.   block context locative
  137.   ;; This value should make anyone trying to look at it crash.
  138.   'TRIVIAL-CLOSURE-BOGUS-LOCATIVE)
  139.  
  140. (define (closure-block/parent-locative block context locative)
  141.   context
  142.   (rtl:make-fetch
  143.    (rtl:locative-offset locative
  144.             (closure-block-first-offset block))))
  145.  
  146. (define (stack-block/parent-of-dummy-closure-locative block context locative)
  147.   (closure-block/parent-locative
  148.    block
  149.    context
  150.    (stack-block/closure-parent-locative block context locative)))