Merge branch 'boehm-demers-weiser-gc' into bdw-gc-static-alloc
[bpt/guile.git] / libguile / snarf.h
1 /* classes: h_files */
2
3 #ifndef SCM_SNARF_H
4 #define SCM_SNARF_H
5
6 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2009 Free Software Foundation, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 \f
24
25 /* Macros for snarfing initialization actions from C source. */
26
27 #if defined(__cplusplus) || defined(GUILE_CPLUSPLUS_SNARF)
28
29 /* This used to be "SCM (*)(...)" but GCC on RedHat 7.1 doesn't seem
30 to like it.
31 */
32 #define SCM_FUNC_CAST_ARBITRARY_ARGS SCM (*)()
33
34 #else
35 #define SCM_FUNC_CAST_ARBITRARY_ARGS SCM (*)()
36 #endif
37
38 #if (defined SCM_ALIGNED) && (SCM_DEBUG_TYPING_STRICTNESS <= 1)
39 /* We support static allocation of some `SCM' objects. */
40 # define SCM_SUPPORT_STATIC_ALLOCATION
41 #endif
42
43 /* C preprocessor token concatenation. */
44 #define scm_i_paste(x, y) x ## y
45 #define scm_i_paste3(a, b, c) a ## b ## c
46
47
48 \f
49 /* Generic macros to be used in user macro definitions.
50 *
51 * For example, in order to define a macro which creates ints and
52 * initializes them to the result of foo (), do:
53 *
54 * #define SCM_FOO(NAME) \
55 * SCM_SNARF_HERE (int NAME) \
56 * SCM_SNARF_INIT (NAME = foo ())
57 *
58 * The SCM_SNARF_INIT text goes into the corresponding .x file
59 * up through the first occurrence of SCM_SNARF_DOC_START on that
60 * line, if any.
61 */
62
63 #ifdef SCM_MAGIC_SNARF_INITS
64 # define SCM_SNARF_HERE(X)
65 # define SCM_SNARF_INIT(X) ^^ X ^:^
66 # define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
67 #else
68 # ifdef SCM_MAGIC_SNARF_DOCS
69 # define SCM_SNARF_HERE(X)
70 # define SCM_SNARF_INIT(X)
71 # define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING) \
72 ^^ { \
73 cname CNAME ^^ \
74 fname FNAME ^^ \
75 type TYPE ^^ \
76 location __FILE__ __LINE__ ^^ \
77 arglist ARGLIST ^^ \
78 argsig REQ OPT VAR ^^ \
79 DOCSTRING ^^ }
80 # else
81 # define SCM_SNARF_HERE(X) X
82 # define SCM_SNARF_INIT(X)
83 # define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
84 # endif
85 #endif
86
87 #define SCM_DEFINE_GSUBR(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
88 SCM_SNARF_HERE(\
89 static const char s_ ## FNAME [] = PRIMNAME; \
90 SCM FNAME ARGLIST\
91 )\
92 SCM_SNARF_INIT(\
93 scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
94 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
95 )\
96 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
97
98 #ifdef SCM_SUPPORT_STATIC_ALLOCATION
99
100 /* Regular "subrs", i.e., few arguments. */
101 #define SCM_DEFINE_SUBR(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
102 SCM_SYMBOL (scm_i_paste (FNAME, __name), PRIMNAME); \
103 SCM_SNARF_HERE( \
104 static const char scm_i_paste (s_, FNAME) [] = PRIMNAME; \
105 SCM_IMMUTABLE_SUBR (scm_i_paste (FNAME, __subr), \
106 scm_i_paste (FNAME, __name), \
107 REQ, OPT, VAR, &FNAME); \
108 SCM FNAME ARGLIST \
109 ) \
110 SCM_SNARF_INIT( \
111 /* Initialize the procedure name (an interned symbol). */ \
112 scm_i_paste (FNAME, __subr_meta_info)[0] = scm_i_paste (FNAME, __name); \
113 \
114 /* Define the subr. */ \
115 scm_c_define (scm_i_paste (s_, FNAME), scm_i_paste (FNAME, __subr)); \
116 ) \
117 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
118
119 /* XXX: Eventually, we could statically allocate gsubrs as well. */
120
121 /* These are the subrs whose arity makes it possible to define them as "raw
122 subrs" (as opposed to "gsubrs"). This has to be consistent with
123 `SCM_SUBR_ARITY_TO_TYPE ()' and `create_gsubr ()'. */
124 #define SCM_DEFINE_SUBR_req0_opt0_rst0 SCM_DEFINE_SUBR
125 #define SCM_DEFINE_SUBR_req1_opt0_rst0 SCM_DEFINE_SUBR
126 #define SCM_DEFINE_SUBR_req0_opt1_rst0 SCM_DEFINE_SUBR
127 #define SCM_DEFINE_SUBR_req1_opt1_rst0 SCM_DEFINE_SUBR
128 #define SCM_DEFINE_SUBR_req2_opt0_rst0 SCM_DEFINE_SUBR
129 #define SCM_DEFINE_SUBR_req3_opt0_rst0 SCM_DEFINE_SUBR
130 #define SCM_DEFINE_SUBR_req0_opt0_rst1 SCM_DEFINE_SUBR
131 #define SCM_DEFINE_SUBR_req2_opt0_rst1 SCM_DEFINE_SUBR
132
133 /* For any other combination of required/optional/rest arguments, use
134 `SCM_DEFINE_GSUBR (). */
135 #include "libguile/snarf-gsubr.h"
136
137 /* The generic subr definition macro. This macro dispatches to either
138 `SCM_DEFINE_SUBR ()' or `SCM_DEFINE_GSUBR ()' depending on the arity of
139 the subr being defined. */
140 #define SCM_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
141 SCM_DEFINE_SUBR_req ## REQ ## _opt ## OPT ## _rst ## VAR \
142 (FNAME, PRIMNAME, \
143 REQ, OPT, VAR, \
144 ARGLIST, DOCSTRING)
145
146
147 #else /* !SCM_SUPPORT_STATIC_ALLOCATION */
148
149 /* Always use the generic subr case. */
150 #define SCM_DEFINE SCM_DEFINE_GSUBR
151
152 #endif /* !SCM_SUPPORT_STATIC_ALLOCATION */
153
154
155 #define SCM_PRIMITIVE_GENERIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
156 SCM_SNARF_HERE(\
157 static const char s_ ## FNAME [] = PRIMNAME; \
158 static SCM g_ ## FNAME; \
159 SCM FNAME ARGLIST\
160 )\
161 SCM_SNARF_INIT(\
162 g_ ## FNAME = SCM_PACK (0); \
163 scm_c_define_gsubr_with_generic (s_ ## FNAME, REQ, OPT, VAR, \
164 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME, \
165 &g_ ## FNAME); \
166 )\
167 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
168
169 #define SCM_DEFINE_PUBLIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
170 SCM_SNARF_HERE(\
171 static const char s_ ## FNAME [] = PRIMNAME; \
172 SCM FNAME ARGLIST\
173 )\
174 SCM_SNARF_INIT(\
175 scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
176 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
177 scm_c_export (s_ ## FNAME, NULL); \
178 )\
179 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
180
181 #define SCM_DEFINE1(FNAME, PRIMNAME, TYPE, ARGLIST, DOCSTRING) \
182 SCM_SNARF_HERE(\
183 static const char s_ ## FNAME [] = PRIMNAME; \
184 SCM FNAME ARGLIST\
185 )\
186 SCM_SNARF_INIT(scm_c_define_subr (s_ ## FNAME, TYPE, FNAME); ) \
187 SCM_SNARF_DOCS(1, FNAME, PRIMNAME, ARGLIST, 2, 0, 0, DOCSTRING)
188
189 #define SCM_PRIMITIVE_GENERIC_1(FNAME, PRIMNAME, TYPE, ARGLIST, DOCSTRING) \
190 SCM_SNARF_HERE(\
191 static const char s_ ## FNAME [] = PRIMNAME; \
192 static SCM g_ ## FNAME; \
193 SCM FNAME ARGLIST\
194 )\
195 SCM_SNARF_INIT(\
196 g_ ## FNAME = SCM_PACK (0); \
197 scm_c_define_subr_with_generic (s_ ## FNAME, TYPE, FNAME, &g_ ## FNAME); \
198 )\
199 SCM_SNARF_DOCS(1, FNAME, PRIMNAME, ARGLIST, 2, 0, 0, DOCSTRING)
200
201 #define SCM_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
202 SCM_SNARF_HERE(static const char RANAME[]=STR) \
203 SCM_SNARF_INIT(scm_c_define_gsubr (RANAME, REQ, OPT, VAR, \
204 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN))
205
206 #define SCM_REGISTER_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
207 SCM_SNARF_HERE(static const char RANAME[]=STR) \
208 SCM_SNARF_INIT(scm_c_define_gsubr (RANAME, REQ, OPT, VAR, \
209 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN);) \
210 SCM_SNARF_DOCS(register, CFN, STR, (), REQ, OPT, VAR, \
211 "implemented by the C function \"" #CFN "\"")
212
213 #define SCM_GPROC(RANAME, STR, REQ, OPT, VAR, CFN, GF) \
214 SCM_SNARF_HERE(\
215 static const char RANAME[]=STR;\
216 static SCM GF \
217 )SCM_SNARF_INIT(\
218 GF = SCM_PACK (0); /* Dirk:FIXME:: Can we safely use #f instead of 0? */ \
219 scm_c_define_gsubr_with_generic (RANAME, REQ, OPT, VAR, \
220 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN, &GF) \
221 )
222
223 #define SCM_PROC1(RANAME, STR, TYPE, CFN) \
224 SCM_SNARF_HERE(static const char RANAME[]=STR) \
225 SCM_SNARF_INIT(\
226 scm_c_define_subr (RANAME, TYPE, (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN) \
227 )
228
229
230 #define SCM_GPROC1(RANAME, STR, TYPE, CFN, GF) \
231 SCM_SNARF_HERE(\
232 static const char RANAME[]=STR; \
233 static SCM GF \
234 )SCM_SNARF_INIT(\
235 GF = SCM_PACK (0); /* Dirk:FIXME:: Can we safely use #f instead of 0? */ \
236 scm_c_define_subr_with_generic (RANAME, TYPE, \
237 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN, &GF) \
238 )
239
240 #define SCM_SYNTAX(RANAME, STR, TYPE, CFN) \
241 SCM_SNARF_HERE(static const char RANAME[]=STR)\
242 SCM_SNARF_INIT(scm_make_synt (RANAME, TYPE, CFN))
243
244 #ifdef SCM_SUPPORT_STATIC_ALLOCATION
245
246 # define SCM_SYMBOL(c_name, scheme_name) \
247 SCM_SNARF_HERE( \
248 SCM_IMMUTABLE_STRING (scm_i_paste (c_name, _string), scheme_name); \
249 static SCM c_name) \
250 SCM_SNARF_INIT( \
251 c_name = scm_string_to_symbol (scm_i_paste (c_name, _string)) \
252 )
253
254 # define SCM_GLOBAL_SYMBOL(c_name, scheme_name) \
255 SCM_SNARF_HERE( \
256 SCM_IMMUTABLE_STRING (scm_i_paste (c_name, _string), scheme_name); \
257 SCM c_name) \
258 SCM_SNARF_INIT( \
259 c_name = scm_string_to_symbol (scm_i_paste (c_name, _string)) \
260 )
261
262 #else /* !SCM_SUPPORT_STATIC_ALLOCATION */
263
264 # define SCM_SYMBOL(c_name, scheme_name) \
265 SCM_SNARF_HERE(static SCM c_name) \
266 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_from_locale_symbol (scheme_name)))
267
268 # define SCM_GLOBAL_SYMBOL(c_name, scheme_name) \
269 SCM_SNARF_HERE(SCM c_name) \
270 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_from_locale_symbol (scheme_name)))
271
272 #endif /* !SCM_SUPPORT_STATIC_ALLOCATION */
273
274 #define SCM_KEYWORD(c_name, scheme_name) \
275 SCM_SNARF_HERE(static SCM c_name) \
276 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_from_locale_keyword (scheme_name)))
277
278 #define SCM_GLOBAL_KEYWORD(c_name, scheme_name) \
279 SCM_SNARF_HERE(SCM c_name) \
280 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_from_locale_keyword (scheme_name)))
281
282 #define SCM_VARIABLE(c_name, scheme_name) \
283 SCM_SNARF_HERE(static SCM c_name) \
284 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_c_define (scheme_name, SCM_BOOL_F));)
285
286 #define SCM_GLOBAL_VARIABLE(c_name, scheme_name) \
287 SCM_SNARF_HERE(SCM c_name) \
288 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_c_define (scheme_name, SCM_BOOL_F));)
289
290 #define SCM_VARIABLE_INIT(c_name, scheme_name, init_val) \
291 SCM_SNARF_HERE(static SCM c_name) \
292 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_c_define (scheme_name, init_val));)
293
294 #define SCM_GLOBAL_VARIABLE_INIT(c_name, scheme_name, init_val) \
295 SCM_SNARF_HERE(SCM c_name) \
296 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_c_define (scheme_name, init_val));)
297
298 #define SCM_MUTEX(c_name) \
299 SCM_SNARF_HERE(static scm_t_mutex c_name) \
300 SCM_SNARF_INIT(scm_i_plugin_mutex_init (&c_name, &scm_i_plugin_mutex))
301
302 #define SCM_GLOBAL_MUTEX(c_name) \
303 SCM_SNARF_HERE(scm_t_mutex c_name) \
304 SCM_SNARF_INIT(scm_i_plugin_mutex_init (&c_name, &scm_i_plugin_mutex))
305
306 #define SCM_REC_MUTEX(c_name) \
307 SCM_SNARF_HERE(static scm_t_rec_mutex c_name) \
308 SCM_SNARF_INIT(scm_i_plugin_rec_mutex_init (&c_name, &scm_i_plugin_rec_mutex))
309
310 #define SCM_GLOBAL_REC_MUTEX(c_name) \
311 SCM_SNARF_HERE(scm_t_rec_mutex c_name) \
312 SCM_SNARF_INIT(scm_i_plugin_rec_mutex_init (&c_name, &scm_i_plugin_rec_mutex))
313
314 #define SCM_SMOB(tag, scheme_name, size) \
315 SCM_SNARF_HERE(static scm_t_bits tag) \
316 SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
317
318 #define SCM_GLOBAL_SMOB(tag, scheme_name, size) \
319 SCM_SNARF_HERE(scm_t_bits tag) \
320 SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
321
322 #define SCM_SMOB_MARK(tag, c_name, arg) \
323 SCM_SNARF_HERE(static SCM c_name(SCM arg)) \
324 SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
325
326 #define SCM_GLOBAL_SMOB_MARK(tag, c_name, arg) \
327 SCM_SNARF_HERE(SCM c_name(SCM arg)) \
328 SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
329
330 #define SCM_SMOB_FREE(tag, c_name, arg) \
331 SCM_SNARF_HERE(static size_t c_name(SCM arg)) \
332 SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
333
334 #define SCM_GLOBAL_SMOB_FREE(tag, c_name, arg) \
335 SCM_SNARF_HERE(size_t c_name(SCM arg)) \
336 SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
337
338 #define SCM_SMOB_PRINT(tag, c_name, obj, port, pstate) \
339 SCM_SNARF_HERE(static int c_name(SCM obj, SCM port, scm_print_state* pstate)) \
340 SCM_SNARF_INIT(scm_set_smob_print((tag), (c_name));)
341
342 #define SCM_GLOBAL_SMOB_PRINT(tag, c_name, obj, port, pstate) \
343 SCM_SNARF_HERE(int c_name(SCM obj, SCM port, scm_print_state* pstate)) \
344 SCM_SNARF_INIT(scm_set_smob_print((tag), (c_name));)
345
346 #define SCM_SMOB_EQUALP(tag, c_name, obj1, obj2) \
347 SCM_SNARF_HERE(static SCM c_name(SCM obj1, SCM obj2)) \
348 SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
349
350 #define SCM_GLOBAL_SMOB_EQUALP(tag, c_name, obj1, obj2) \
351 SCM_SNARF_HERE(SCM c_name(SCM obj1, SCM obj2)) \
352 SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
353
354 #define SCM_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
355 SCM_SNARF_HERE(static SCM c_name arglist) \
356 SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
357
358 #define SCM_GLOBAL_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
359 SCM_SNARF_HERE(SCM c_name arglist) \
360 SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
361
362 \f
363 /* Low-level snarfing for static memory allocation. */
364
365 #ifdef SCM_SUPPORT_STATIC_ALLOCATION
366
367 #define SCM_IMMUTABLE_DOUBLE_CELL(c_name, car, cbr, ccr, cdr) \
368 static SCM_ALIGNED (8) SCM_UNUSED const scm_t_cell \
369 c_name ## _raw_cell [2] = \
370 { \
371 { SCM_PACK (car), SCM_PACK (cbr) }, \
372 { SCM_PACK (ccr), SCM_PACK (cdr) } \
373 }; \
374 static SCM_UNUSED const SCM c_name = SCM_PACK (& c_name ## _raw_cell)
375
376 #define SCM_IMMUTABLE_STRINGBUF(c_name, contents) \
377 SCM_IMMUTABLE_DOUBLE_CELL (c_name, \
378 scm_tc7_stringbuf | SCM_I_STRINGBUF_F_SHARED, \
379 (scm_t_bits) (contents), \
380 (scm_t_bits) sizeof (contents) - 1, \
381 (scm_t_bits) 0)
382
383 #define SCM_IMMUTABLE_STRING(c_name, contents) \
384 SCM_IMMUTABLE_STRINGBUF (scm_i_paste (c_name, _stringbuf), contents); \
385 SCM_IMMUTABLE_DOUBLE_CELL (c_name, \
386 scm_tc7_ro_string, \
387 (scm_t_bits) &scm_i_paste (c_name, \
388 _stringbuf_raw_cell), \
389 (scm_t_bits) 0, \
390 (scm_t_bits) sizeof (contents) - 1)
391
392 #define SCM_IMMUTABLE_SUBR(c_name, name, req, opt, rest, fcn) \
393 static SCM_UNUSED SCM scm_i_paste (c_name, _meta_info)[2] = \
394 { \
395 SCM_BOOL_F, /* The name, initialized at run-time. */ \
396 SCM_EOL /* The procedure properties. */ \
397 }; \
398 SCM_IMMUTABLE_DOUBLE_CELL (c_name, \
399 SCM_SUBR_ARITY_TO_TYPE (req, opt, rest), \
400 (scm_t_bits) fcn, \
401 (scm_t_bits) 0 /* no generic */, \
402 (scm_t_bits) & scm_i_paste (c_name, _meta_info));
403
404 #endif /* SCM_SUPPORT_STATIC_ALLOCATION */
405
406 \f
407 /* Documentation. */
408
409 #ifdef SCM_MAGIC_SNARF_DOCS
410 #undef SCM_ASSERT
411 #define SCM_ASSERT(_cond, _arg, _pos, _subr) ^^ argpos _arg _pos __LINE__ ^^
412 #endif /* SCM_MAGIC_SNARF_DOCS */
413
414 #endif /* SCM_SNARF_H */
415
416 /*
417 Local Variables:
418 c-file-style: "gnu"
419 End:
420 */