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  2000)
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    (while (wgreaterp bndstkptr ptr)
97      (setf (symval (inf (bndstkid bndstkptr))) (bndstkval bndstkptr))
98      (setq bndstkptr (adjustbndstkptr bndstkptr -1)))))
99
100(de clearbindings ()
101  %. Restore bindings to top level
102  (progn (restoreenvironment bndstklowerbound)
103         (!%clear-catch-stack)))
104
105(de unbindn (n)
106  %. Support for Lambda and Prog interp
107  (restoreenvironment (adjustbndstkptr bndstkptr (wminus (intinf n)))))
108
109(de lbind1 (idname valuetobind)
110  %. Support for Lambda
111  (cond ((not (idp idname)) (noniderror idname "binding"))
112        ((or (null idname) (weq idname 't))
113         (stderror '"T and NIL cannot be rebound"))
114        (t (setq bndstkptr (adjustbndstkptr bndstkptr 1)) (if (wgreaterp
115                                                               bndstkptr
116                                                               bndstkupperbound)
117             (bstackoverflow)
118             (progn (setq idname (idinf idname))
119                    (setf (bndstkid bndstkptr) idname)
120                    (setf (bndstkval bndstkptr) (symval idname))
121                    (setf (symval idname) valuetobind))))))
122
123(de pbind1 (idname)
124  %. Support for PROG
125  (lbind1 idname nil))
126
127(compiletime (progn (remprop 'bndstksize 'constant?)
128                    (fluid '(bndstksize))
129                    (flag '(update-catchstk) 'internalfunction)
130                    (setq *syslisp t)))
131
132(de set-bndstk-size (newsize)
133  (prog (diff from to)
134       (setq diff (wdifference newsize bndstksize))
135       (cond ((wgreaterp diff 0)
136                (setq to (gtwarray newsize))
137                (setq from bndstk)
138                (copywarray to from
139                        (wplus2 2 (quotient (difference bndstkptr bndstk)
140                                               addressingunitsperitem)))
141                (setf bndstklowerbound to)
142                (setf bndstk           to)
143                (setf bndstkupperbound
144                     (wplus2 to
145                        (wtimes2 addressingunitsperitem (wplus2 -3 newsize)) ))
146                (setf bndstkptr (wplus2 to (wdifference bndstkptr from)))
147                (update-catchstk (wdifference to from) 3)
148                (setq bndstksize newsize)
149                (return newsize)))))
150
151(de update-catchstk (diff posi)   % if stack or bndstk have been moved
152    (prog (actcatch)      % update sp entries by diff
153          (setq actcatch catchstackptr)
154    lupe  (setf (wgetv actcatch posi) (wplus2 (wgetv actcatch posi)
155                                               diff))
156          (setq actcatch
157            (wdifference actcatch (times2 4 addressingunitsperitem)))
158          (cond ((wgreaterp actcatch catchstack ) (go lupe)))
159          (return nil)
160)   )
161