450c63ca03a8e2841ef0a2ec3a4454d863b01fdb
[sbcl.git] / src / runtime / dynbind.c
1 /*
2  * support for dynamic binding from C
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
9  * This software is derived from the CMU CL system, which was
10  * written at Carnegie Mellon University and released into the
11  * public domain. The software is in the public domain and is
12  * provided with absolutely no warranty. See the COPYING and CREDITS
13  * files for more information.
14  */
15
16 /*
17  * $Header$
18  */
19
20 #include "runtime.h"
21 #include "sbcl.h"
22 #include "globals.h"
23 #include "dynbind.h"
24
25 #if defined(ibmrt) || defined(__i386__)
26 #define GetBSP() ((struct binding *)SymbolValue(BINDING_STACK_POINTER))
27 #define SetBSP(value) SetSymbolValue(BINDING_STACK_POINTER, (lispobj)(value))
28 #else
29 #define GetBSP() ((struct binding *)current_binding_stack_pointer)
30 #define SetBSP(value) (current_binding_stack_pointer=(lispobj *)(value))
31 #endif
32
33 void bind_variable(lispobj symbol, lispobj value)
34 {
35         lispobj old_value;
36         struct binding *binding;
37
38         old_value = SymbolValue(symbol);
39         binding = GetBSP();
40         SetBSP(binding+1);
41
42         binding->value = old_value;
43         binding->symbol = symbol;
44         SetSymbolValue(symbol, value);
45 }
46
47 void unbind(void)
48 {
49         struct binding *binding;
50         lispobj symbol;
51         
52         binding = GetBSP() - 1;
53                 
54         symbol = binding->symbol;
55
56         SetSymbolValue(symbol, binding->value);
57
58         binding->symbol = 0;
59
60         SetBSP(binding);
61 }
62
63 void unbind_to_here(lispobj *bsp)
64 {
65     struct binding *target = (struct binding *)bsp;
66     struct binding *binding = GetBSP();
67     lispobj symbol;
68
69     while (target < binding) {
70         binding--;
71
72         symbol = binding->symbol;
73
74         if (symbol) {
75             SetSymbolValue(symbol, binding->value);
76             binding->symbol = 0;
77         }
78
79     }
80     SetBSP(binding);
81 }