0.7.13.5
[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 #include "genesis/symbol.h"
21 #include "genesis/binding.h"
22 #include "genesis/static-symbols.h"
23
24 #if defined(__i386__)
25 #define GetBSP() ((struct binding *)SymbolValue(BINDING_STACK_POINTER))
26 #define SetBSP(value) SetSymbolValue(BINDING_STACK_POINTER, (lispobj)(value))
27 #else
28 #define GetBSP() ((struct binding *)current_binding_stack_pointer)
29 #define SetBSP(value) (current_binding_stack_pointer=(lispobj *)(value))
30 #endif
31
32 void bind_variable(lispobj symbol, lispobj value)
33 {
34     lispobj old_value;
35     struct binding *binding;
36
37     old_value = SymbolValue(symbol);
38     binding = GetBSP();
39     SetBSP(binding+1);
40
41     binding->value = old_value;
42     binding->symbol = symbol;
43     SetSymbolValue(symbol, value);
44 }
45
46 void
47 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
64 unbind_to_here(lispobj *bsp)
65 {
66     struct binding *target = (struct binding *)bsp;
67     struct binding *binding = GetBSP();
68     lispobj symbol;
69
70     while (target < binding) {
71         binding--;
72
73         symbol = binding->symbol;
74
75         if (symbol) {
76             SetSymbolValue(symbol, binding->value);
77             binding->symbol = 0;
78         }
79
80     }
81     SetBSP(binding);
82 }