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:       Experimental
9% Mode:         Lisp
10% Package:      Kernel
11% Compiletime:
12% Runtime:
13%
14% (c) Copyright 1983, Hewlett-Packard Company, all rights reserved.
15% (c) Copyright 1981, University of Utah
16%
17%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
18%
19% Revisions:
20%
21% 31-Aug-88 (Julian Padget)
22%  Bnd... declarations changed from fluid to global.
23% 06-Mar-84 17:11:53 (Jim Ambras/CRC)
24%  Uppded binding stack size from 4000 to 8000 for REDUCE.
25% 02-Mar-84 09:29:17 (Jim Ambras/CRC)
26%  Corrected file header.
27% 16-Jan-84 14:11:00 (Tim Tillson)
28%  Upped binding stack size from 2000 to 4000 for unknown (and possibly buggy?)
29%  reasons to build NMODE successfully.
30% 01-Dec-83 14:16:18 (Brian Beach)
31%   Translated from Rlisp to Lisp.
32%
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34
35(define-constant bndstksize 2000)
36
37(global '(bndstk bndstkptr bndstkupperbound bndstklowerbound))
38
39% Binding stack is initialized in the kernel.
40%
41%(setq bndstkptr (loc (wgetv bndstk 0)))%
42%
43%(setq bndstkupperbound (loc (wgetv bndstk (wdifference bndstksize 1))))
44%
45%(setq bndstklowerbound (loc (wgetv bndstk 0)))
46
47% Only the macros BndStkID, BndStkVal and AdjustBndStkPtr will be used
48% to access or modify the binding stack and pointer.
49
50(de bstackoverflow ()
51  (channelprin2 errout* "***** Binding stack overflow, restarting...")
52  (channelwritechar errout* (char eol))
53  (reset))
54
55(de bstackunderflow ()
56  (channelprin2 errout* "***** Binding stack underflow, restarting...")
57  (channelwritechar errout* (char eol))
58  (reset))
59
60(de captureenvironment ()
61  bndstkptr
62  )
63
64(de restoreenvironment (ptr)
65  % Restore old bindings
66  (if (wlessp ptr bndstklowerbound)
67    (bstackunderflow)
68    (while (wgreaterp bndstkptr ptr)
69      (setf (symval (inf (bndstkid bndstkptr))) (bndstkval bndstkptr))
70      (setq bndstkptr (adjustbndstkptr bndstkptr -1)))))
71
72(de clearbindings ()
73  %. Restore bindings to top level
74  (progn (restoreenvironment bndstklowerbound)
75         (!%clear-catch-stack)))
76
77(de unbindn (n)
78  %. Support for Lambda and Prog interp
79  (restoreenvironment (adjustbndstkptr bndstkptr (wminus (intinf n)))))
80
81(de lbind1 (idname valuetobind)
82  %. Support for Lambda
83  (cond ((not (idp idname)) (noniderror idname "binding"))
84        ((or (null idname) (weq idname 't))
85         (stderror '"T and NIL cannot be rebound"))
86        (t (setq bndstkptr (adjustbndstkptr bndstkptr 1)) (if (wgreaterp
87                                                               bndstkptr
88                                                               bndstkupperbound)
89             (bstackoverflow)
90             (progn (setq idname (idinf idname))
91                    (setf (bndstkid bndstkptr) idname)
92                    (setf (bndstkval bndstkptr) (symval idname))
93                    (setf (symval idname) valuetobind))))))
94
95(de pbind1 (idname)
96  %. Support for PROG
97  (lbind1 idname nil))
98
99(compiletime (progn (remprop 'bndstksize 'constant?)
100                    (fluid '(bndstksize))
101                    (flag '(update-catchstk) 'internalfunction)
102                    (setq *syslisp t)))
103
104(de set-bndstk-size (newsize)
105  (prog (diff from to)
106       (setq diff (wdifference newsize bndstksize))
107       (cond ((wgreaterp diff 0)
108                (setq to (gtwarray newsize))
109                (setq from bndstk)
110                (copywarray to from
111                        (wplus2 2 (quotient (wdifference bndstkptr bndstk)
112                                               addressingunitsperitem)))
113                (setf bndstklowerbound to)
114                (setf bndstk           to)
115                (setf bndstkupperbound
116                     (wplus2 to
117                        (wtimes2 addressingunitsperitem (wplus2 -3 newsize)) ))
118                (setf bndstkptr (wplus2 to (wdifference bndstkptr from)))
119                (update-catchstk (wdifference to from) 3)
120                (setq bndstksize newsize)
121                (return newsize)))))
122
123(de update-catchstk (diff posi)   % if stack or bndstk have been moved
124    (prog (actcatch)      % update sp entries by diff
125          (setq actcatch catchstackptr)
126    lupe  (setf (wgetv actcatch posi) (wplus2 (wgetv actcatch posi)
127                                               diff))
128          (setq actcatch
129            (wdifference actcatch (times2 4 addressingunitsperitem)))
130          (cond ((wgreaterp actcatch catchstack ) (go lupe)))
131          (return nil)
132)   )
133
134