2d9f47e3e3685f7b01d6c7b8713ecd3b40509652
[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 #include "runtime.h"
17 #include "sbcl.h"
18 #include "globals.h"
19 #include "dynbind.h"
20
21 #if defined(__i386__)
22 #define GetBSP() ((struct binding *)SymbolValue(BINDING_STACK_POINTER))
23 #define SetBSP(value) SetSymbolValue(BINDING_STACK_POINTER, (lispobj)(value))
24 #else
25 #define GetBSP() ((struct binding *)current_binding_stack_pointer)
26 #define SetBSP(value) (current_binding_stack_pointer=(lispobj *)(value))
27 #endif
28
29 void bind_variable(lispobj symbol, lispobj value)
30 {
31     lispobj old_value;
32     struct binding *binding;
33
34     old_value = SymbolValue(symbol);
35     binding = GetBSP();
36     SetBSP(binding+1);
37
38     binding->value = old_value;
39     binding->symbol = symbol;
40     SetSymbolValue(symbol, value);
41 }
42
43 void
44 unbind(void)
45 {
46     struct binding *binding;
47     lispobj symbol;
48         
49     binding = GetBSP() - 1;
50                 
51     symbol = binding->symbol;
52
53     SetSymbolValue(symbol, binding->value);
54
55     binding->symbol = 0;
56
57     SetBSP(binding);
58 }
59
60 void
61 unbind_to_here(lispobj *bsp)
62 {
63     struct binding *target = (struct binding *)bsp;
64     struct binding *binding = GetBSP();
65     lispobj symbol;
66
67     while (target < binding) {
68         binding--;
69
70         symbol = binding->symbol;
71
72         if (symbol) {
73             SetSymbolValue(symbol, binding->value);
74             binding->symbol = 0;
75         }
76
77     }
78     SetBSP(binding);
79 }