c3724eb8c0cdfa426c9081a53c1ab8600a73be04
[sbcl.git] / src / runtime / funcall.c
1 /* funcall0 -- funcall3: we can get by with just two sets of these:
2  * for platforms where the control stack is the C-stack, and all others.
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 <stdio.h>
17
18 #include "sbcl.h"
19 #include "runtime.h"
20 #include "globals.h"
21 #include "os.h"
22 #include "interrupt.h"
23
24 /* This is implemented in assembly language and called from C: */
25 extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
26
27 static inline lispobj
28 safe_call_into_lisp(lispobj fun, lispobj *args, int nargs)
29 {
30     /* SIG_STOP_FOR_GC needs to be enabled before we can call lisp:
31      * otherwise two threads racing here may deadlock: the other will
32      * wait on the GC lock, and the other cannot stop the first
33      * one... */
34     check_gc_signals_unblocked_or_lose(0);
35     return call_into_lisp(fun, args, nargs);
36 }
37
38 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
39 /* These functions are an interface to the Lisp call-in facility.
40  * Since this is C we can know nothing about the calling environment.
41  * The control stack might be the C stack if called from the monitor
42  * or the Lisp stack if called as a result of an interrupt or maybe
43  * even a separate stack. The args are most likely on that stack but
44  * could be in registers depending on what the compiler likes. So we
45  * copy the args into a portable vector and let the assembly language
46  * call-in function figure it out. */
47
48 lispobj
49 funcall0(lispobj function)
50 {
51     lispobj *args = NULL;
52
53     FSHOW((stderr, "/entering funcall0(0x%lx)\n", (long)function));
54     return safe_call_into_lisp(function, args, 0);
55 }
56 lispobj
57 funcall1(lispobj function, lispobj arg0)
58 {
59     lispobj args[1];
60     args[0] = arg0;
61     return safe_call_into_lisp(function, args, 1);
62 }
63
64 lispobj
65 funcall2(lispobj function, lispobj arg0, lispobj arg1)
66 {
67     lispobj args[2];
68     args[0] = arg0;
69     args[1] = arg1;
70     return safe_call_into_lisp(function, args, 2);
71 }
72
73 lispobj
74 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
75 {
76     lispobj args[3];
77     args[0] = arg0;
78     args[1] = arg1;
79     args[2] = arg2;
80     return safe_call_into_lisp(function, args, 3);
81 }
82
83 #else
84
85 lispobj
86 funcall0(lispobj function)
87 {
88     lispobj **stack_pointer
89         = &access_control_stack_pointer(arch_os_get_current_thread());
90     lispobj *args = *stack_pointer;
91
92     return safe_call_into_lisp(function, args, 0);
93 }
94
95 lispobj
96 funcall1(lispobj function, lispobj arg0)
97 {
98     lispobj **stack_pointer
99         = &access_control_stack_pointer(arch_os_get_current_thread());
100     lispobj *args = *stack_pointer;
101
102     *stack_pointer += 1;
103     args[0] = arg0;
104
105     return safe_call_into_lisp(function, args, 1);
106 }
107
108 lispobj
109 funcall2(lispobj function, lispobj arg0, lispobj arg1)
110 {
111     lispobj **stack_pointer
112         = &access_control_stack_pointer(arch_os_get_current_thread());
113     lispobj *args = *stack_pointer;
114
115     *stack_pointer += 2;
116     args[0] = arg0;
117     args[1] = arg1;
118
119     return safe_call_into_lisp(function, args, 2);
120 }
121
122 lispobj
123 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
124 {
125     lispobj **stack_pointer
126         = &access_control_stack_pointer(arch_os_get_current_thread());
127     lispobj *args = *stack_pointer;
128
129     *stack_pointer += 3;
130     args[0] = arg0;
131     args[1] = arg1;
132     args[2] = arg2;
133
134     return safe_call_into_lisp(function, args, 3);
135 }
136 #endif