1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2%
3% File:         PNK:BINDING.SL
4% Title:        Primitives to support Lambda binding
5% Author:       Eric Benson
6% Created:      18 August 1981
7% Modified:     23-May-84 10:30:23 (Brian Beach)
8% Status:       Open Source: BSD License
9% Mode:         Lisp
10% Package:      Kernel
11% Compiletime:
12% Runtime:
13%
14% (c) Copyright 1983, Hewlett-Packard Company, see the file
15%            HP_disclaimer at the root of the PSL file tree
16%
17% (c) Copyright 1982, University of Utah
18%
19% Redistribution and use in source and binary forms, with or without
20% modification, are permitted provided that the following conditions are met:
21%
22%    * Redistributions of source code must retain the relevant copyright
23%      notice, this list of conditions and the following disclaimer.
24%    * Redistributions in binary form must reproduce the above copyright
25%      notice, this list of conditions and the following disclaimer in the
26%      documentation and/or other materials provided with the distribution.
27%
28% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30% THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31% PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR
32% CONTRIBUTORS
33% BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39% POSSIBILITY OF SUCH DAMAGE.
40%
41%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42%
43% Revisions:
44%
45% 31-Jan-1990 (Winfried Neun)
46% added set-bndstk-size feature
47% 31-Aug-88 (Julian Padget)
48%  Bnd... declarations changed from fluid to global.
49% 06-Mar-84 17:11:53 (Jim Ambras/CRC)
50%  Uppded binding stack size from 4000 to 8000 for REDUCE.
51% 02-Mar-84 09:29:17 (Jim Ambras/CRC)
52%  Corrected file header.
53% 16-Jan-84 14:11:00 (Tim Tillson)
54%  Upped binding stack size from 2000 to 4000 for unknown (and possibly buggy?)
55%  reasons to build NMODE successfully.
56% 01-Dec-83 14:16:18 (Brian Beach)
57%   Translated from Rlisp to Lisp.
58%
59%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60
61(global '(bndstksize))
62
63(setq bndstksize  20000)
64
65(global '(bndstk bndstkptr bndstkupperbound bndstklowerbound))
66
67% Binding stack is initialized in the kernel.
68%
69%(setq bndstkptr (loc (wgetv bndstk 0)))%
70%
71%(setq bndstkupperbound (loc (wgetv bndstk (wdifference bndstksize 1))))
72%
73%(setq bndstklowerbound (loc (wgetv bndstk 0)))
74
75% Only the macros BndStkID, BndStkVal and AdjustBndStkPtr will be used
76% to access or modify the binding stack and pointer.
77
78(de bstackoverflow ()
79  (channelprin2 errout* "***** Binding stack overflow, restarting...")
80  (channelwritechar errout* (char eol))
81  (reset))
82
83(de bstackunderflow ()
84  (channelprin2 errout* "***** Binding stack underflow, restarting...")
85  (channelwritechar errout* (char eol))
86  (reset))
87
88(de captureenvironment ()
89  bndstkptr
90  )
91
92(de restoreenvironment (ptr)
93  % Restore old bindings
94  (if (wlessp ptr bndstklowerbound)
95    (bstackunderflow)
96
97    (while (wgreaterp bndstkptr ptr)
98      (setf (symval (inf (bndstkid bndstkptr))) (bndstkval bndstkptr))
99        (setq bndstkptr (adjustbndstkptr bndstkptr -1)))))
100
101(de clearbindings ()
102  %. Restore bindings to top level
103  (progn (restoreenvironment bndstklowerbound)
104         (!%clear-catch-stack)))
105
106(de unbindn (n)
107  %. Support for Lambda and Prog interp
108  (restoreenvironment (adjustbndstkptr bndstkptr (wminus (intinf n)))))
109
110(de lbind1 (idname valuetobind)
111  %. Support for Lambda
112  (cond ((not (idp idname)) (noniderror idname "binding"))
113        ((or (null idname) (weq idname 't))
114         (stderror '"T and NIL cannot be rebound"))
115        (t (setq bndstkptr (adjustbndstkptr bndstkptr 1)) (if (wgreaterp
116                                                               bndstkptr
117                                                               bndstkupperbound)
118             (bstackoverflow)
119             (progn (setq idname (idinf idname))
120                    (setf (bndstkid bndstkptr) idname)
121                    (setf (bndstkval bndstkptr) (symval idname))
122                    (setf (symval idname) valuetobind))))))
123
124(de pbind1 (idname)
125  %. Support for PROG
126  (lbind1 idname nil))
127
128(compiletime (progn (remprop 'bndstksize 'constant?)
129                    (remprop 'bndstksize 'vartype)
130                    (fluid '(bndstksize))
131                    (flag '(update-catchstk) 'internalfunction)
132                    (setq *syslisp t)))
133
134(de set-bndstk-size (newsize)
135  (prog (diff from to)
136       (setq diff (wdifference newsize bndstksize))
137       (cond ((wgreaterp diff 0)
138                (setq to (gtwarray newsize))
139                (setq from bndstk)
140                (copywarray to from
141                        (wplus2 2 (quotient (difference bndstkptr bndstk)
142                                               addressingunitsperitem)))
143                (setf bndstklowerbound to)
144                (setf bndstk           to)
145                (setf bndstkupperbound
146                     (wplus2 to
147                        (wtimes2 addressingunitsperitem (wplus2 -3 newsize)) ))
148                (setf bndstkptr (wplus2 to (wdifference bndstkptr from)))
149                (update-catchstk (wdifference to from) 3)
150                (setq bndstksize newsize)
151                (return newsize)))))
152
153(de update-catchstk (diff posi)   % if stack or bndstk have been moved
154    (prog (actcatch)      % update sp entries by diff
155          (setq actcatch catchstackptr)
156    lupe  (setf (wgetv actcatch posi) (wplus2 (wgetv actcatch posi)
157                                               diff))
158          (setq actcatch
159            (wdifference actcatch (times2 4 addressingunitsperitem)))
160          (cond ((wgreaterp actcatch catchstack ) (go lupe)))
161          (return nil)
162)   )
163