Debian Codegraph @

src/c/cfun.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * cfun.c - compiled functions
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <ecl/ecl.h>
16 #include <string.h> /* for memmove() */
17
18 #include "cfun_dispatch.d"
19
20 cl_object
21 ecl_make_cfun(cl_objectfn_fixed c_function, cl_object name, cl_object cblock, int narg)
22 {
23 cl_object cf;
24
25 if (ecl_unlikely(narg < 0 || narg > ECL_C_ARGUMENTS_LIMIT)) {
26 FEprogram_error("ecl_make_cfun: ~a", 1,
27 (narg < 0)
28 ? @"number of arguments must be greater than 0."
29 : @"function requires too many arguments.");
30 }
31
32
33 cf = ecl_alloc_object(t_cfunfixed);
34 cf->cfunfixed.entry = fixed_dispatch_table[narg];
35 cf->cfunfixed.entry_fixed = c_function;
36 cf->cfunfixed.name = name;
37 cf->cfunfixed.block = cblock;
38 cf->cfunfixed.file = ECL_NIL;
39 cf->cfunfixed.file_position = ecl_make_fixnum(-1);
40 cf->cfunfixed.narg = narg;
41 return cf;
42 }
43
44 cl_object
45 ecl_make_cfun_va(cl_objectfn c_function, cl_object name, cl_object cblock, int narg_fixed)
46 {
47 cl_object cf;
48
49 if (ecl_unlikely(narg_fixed < 0 || narg_fixed > ECL_C_ARGUMENTS_LIMIT)) {
50 FEprogram_error("ecl_make_cfun_va: ~a", 1,
51 (narg_fixed < 0)
52 ? @"number of arguments must be greater than 0."
53 : @"function requires too many arguments.");
54 }
55
56 cf = ecl_alloc_object(t_cfun);
57 #ifdef ECL_C_COMPATIBLE_VARIADIC_DISPATCH
58 if (narg_fixed == 0) {
59 cf->cfun.entry = c_function;
60 } else {
61 cf->cfun.entry = variadic_dispatch_table[narg_fixed];
62 cf->cfun.entry_variadic = c_function;
63 }
64 #else
65 cf->cfun.entry = c_function;
66 #endif
67 cf->cfun.name = name;
68 cf->cfun.block = cblock;
69 cf->cfun.file = ECL_NIL;
70 cf->cfun.file_position = ecl_make_fixnum(-1);
71 cf->cfun.narg = narg_fixed;
72 return cf;
73 }
74
75 cl_object
76 ecl_make_cclosure_va(cl_objectfn c_function, cl_object env, cl_object block, int narg_fixed)
77 {
78 cl_object cc;
79
80 if (ecl_unlikely(narg_fixed < 0 || narg_fixed > ECL_C_ARGUMENTS_LIMIT)) {
81 FEprogram_error("ecl_make_cclosure_va: ~a", 1,
82 (narg_fixed < 0)
83 ? @"number of arguments must be greater than 0."
84 : @"function requires too many arguments.");
85 }
86
87 cc = ecl_alloc_object(t_cclosure);
88 #ifdef ECL_C_COMPATIBLE_VARIADIC_DISPATCH
89 if (narg_fixed == 0) {
90 cc->cclosure.entry = c_function;
91 } else {
92 cc->cclosure.entry = variadic_dispatch_table[narg_fixed];
93 cc->cclosure.entry_variadic = c_function;
94 }
95 #else
96 cc->cclosure.entry = c_function;
97 #endif
98 cc->cclosure.env = env;
99 cc->cclosure.block = block;
100 cc->cclosure.file = ECL_NIL;
101 cc->cclosure.file_position = ecl_make_fixnum(-1);
102 cc->cclosure.narg = narg_fixed;
103 return cc;
104 }
105
106 void
107 ecl_def_c_function(cl_object sym, cl_objectfn_fixed c_function, int narg)
108 {
109 si_fset(2, sym,
110 ecl_make_cfun(c_function, sym, ecl_symbol_value(@'si::*cblock*'), narg));
111 }
112
113 void
114 ecl_def_c_macro(cl_object sym, cl_objectfn_fixed c_function, int narg)
115 {
116 si_fset(3, sym,
117 ecl_make_cfun(c_function, sym, ecl_symbol_value(@'si::*cblock*'), 2),
118 ECL_T);
119 }
120
121 void
122 ecl_def_c_macro_va(cl_object sym, cl_objectfn c_function, int narg_fixed)
123 {
124 si_fset(3, sym,
125 ecl_make_cfun_va(c_function, sym, ecl_symbol_value(@'si::*cblock*'), narg_fixed),
126 ECL_T);
127 }
128
129 void
130 ecl_def_c_function_va(cl_object sym, cl_objectfn c_function, int narg_fixed)
131 {
132 si_fset(2, sym,
133 ecl_make_cfun_va(c_function, sym, ecl_symbol_value(@'si::*cblock*'), narg_fixed));
134 }
135
136 cl_object
137 si_compiled_function_name(cl_object fun)
138 {
139 cl_env_ptr the_env = ecl_process_env();
140 cl_object output;
141
142 switch(ecl_t_of(fun)) {
143 case t_bclosure:
144 fun = fun->bclosure.code;
145 case t_bytecodes:
146 output = fun->bytecodes.name; break;
147 case t_cfun:
148 case t_cfunfixed:
149 output = fun->cfun.name; break;
150 case t_cclosure:
151 output = ECL_NIL; break;
152 default:
153 FEinvalid_function(fun);
154 }
155 ecl_return1(the_env, output);
156 }
157
158 cl_object
159 cl_function_lambda_expression(cl_object fun)
160 {
161 cl_env_ptr the_env = ecl_process_env();
162 cl_object output, name = ECL_NIL, lex = ECL_NIL;
163
164 switch(ecl_t_of(fun)) {
165 case t_bclosure:
166 lex = fun->bclosure.lex;
167 fun = fun->bclosure.code;
168 /* fallthrough */
169 case t_bytecodes:
170 name = fun->bytecodes.name;
171 output = fun->bytecodes.definition;
172 if (name == ECL_NIL && output != ECL_NIL)
173 output = cl_cons(@'lambda', output);
174 else if (name != @'si::bytecodes' && output != ECL_NIL)
175 output = @list*(3, @'ext::lambda-block', name, output);
176 break;
177 case t_cfun:
178 case t_cfunfixed:
179 name = fun->cfun.name;
180 lex = ECL_NIL;
181 output = ECL_NIL;
182 break;
183 case t_cclosure:
184 name = ECL_NIL;
185 lex = ECL_T;
186 output = ECL_NIL;
187 break;
188 case t_instance:
189 if (fun->instance.isgf) {
190 name = ECL_NIL;
191 lex = ECL_NIL;
192 output = ECL_NIL;
193 break;
194 }
195 default:
196 FEinvalid_function(fun);
197 }
198 ecl_return3(the_env, output, lex, name);
199 }
200
201 cl_object
202 si_compiled_function_block(cl_object fun)
203 {
204 cl_object output;
205
206 switch(ecl_t_of(fun)) {
207 case t_cfun:
208 output = fun->cfun.block; break;
209 case t_cfunfixed:
210 output = fun->cfunfixed.block; break;
211 case t_cclosure:
212 output = fun->cclosure.block; break;
213 default:
214 FEerror("~S is not a C compiled function.", 1, fun);
215 }
216 @(return output);
217 }
218
219 cl_object
220 si_compiled_function_file(cl_object b)
221 {
222 cl_env_ptr the_env = ecl_process_env();
223 BEGIN:
224 switch (ecl_t_of(b)) {
225 case t_bclosure:
226 b = b->bclosure.code;
227 goto BEGIN;
228 case t_bytecodes:
229 ecl_return2(the_env, b->bytecodes.file, b->bytecodes.file_position);
230 case t_cfun:
231 ecl_return2(the_env, b->cfun.file, b->cfun.file_position);
232 case t_cfunfixed:
233 ecl_return2(the_env, b->cfunfixed.file, b->cfunfixed.file_position);
234 case t_cclosure:
235 ecl_return2(the_env, b->cclosure.file, b->cclosure.file_position);
236 default:
237 ecl_return2(the_env, ECL_NIL, ECL_NIL);
238 }
239 }
240
241 void
242 ecl_set_function_source_file_info(cl_object b, cl_object source, cl_object position)
243 {
244 BEGIN:
245 switch (ecl_t_of(b)) {
246 case t_bclosure:
247 b = b->bclosure.code;
248 goto BEGIN;
249 case t_bytecodes:
250 b->bytecodes.file = source;
251 b->bytecodes.file_position = position;
252 break;
253 case t_cfun:
254 b->cfun.file = source;
255 b->cfun.file_position = position;
256 break;
257 case t_cfunfixed:
258 b->cfunfixed.file = source;
259 b->cfunfixed.file_position = position;
260 break;
261 case t_cclosure:
262 b->cclosure.file = source;
263 b->cclosure.file_position = position;
264 break;
265 default:
266 FEerror("~S is not a compiled function.", 1, b);
267 }
268 }
269
270 void
271 ecl_cmp_defmacro(cl_object fun)
272 {
273 si_fset(3, fun->cfun.name, fun, ECL_T);
274 }
275
276 void
277 ecl_cmp_defun(cl_object fun)
278 {
279 si_fset(2, fun->cfun.name, fun);
280 }