* __scm.h (SCM_TICK): Oops! Forgot to put SCM_ASYNC_TICK here...
[bpt/guile.git] / libguile / __scm.h
1 /* classes: h_files */
2
3 #ifndef __SCMH
4 #define __SCMH
5 /* Copyright (C) 1995, 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this software; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307 USA
21 *
22 * As a special exception, the Free Software Foundation gives permission
23 * for additional uses of the text contained in its release of GUILE.
24 *
25 * The exception is that, if you link the GUILE library with other files
26 * to produce an executable, this does not by itself cause the
27 * resulting executable to be covered by the GNU General Public License.
28 * Your use of that executable is in no way restricted on account of
29 * linking the GUILE library code into it.
30 *
31 * This exception does not however invalidate any other reasons why
32 * the executable file might be covered by the GNU General Public License.
33 *
34 * This exception applies only to the code released by the
35 * Free Software Foundation under the name GUILE. If you copy
36 * code from other Free Software Foundation releases into a copy of
37 * GUILE, as the General Public License permits, the exception does
38 * not apply to the code that you add in this way. To avoid misleading
39 * anyone as to the status of such modified files, you must delete
40 * this exception notice from them.
41 *
42 * If you write modifications of your own for GUILE, it is your choice
43 * whether to permit this exception to apply to your modifications.
44 * If you do not wish that, delete this exception notice. */
45 \f
46 /* {Supported Options}
47 *
48 * These may be defined or undefined.
49 */
50
51 /* #define GUILE_DEBUG_FREELIST */
52
53 /* If the compile FLAG `SCM_CAUTIOUS' is #defined then the number of
54 * arguments is always checked for application of closures. If the
55 * compile FLAG `SCM_RECKLESS' is #defined then they are not checked.
56 * Otherwise, number of argument checks for closures are made only when
57 * the function position (whose value is the closure) of a combination is
58 * not an ILOC or GLOC. When the function position of a combination is a
59 * symbol it will be checked only the first time it is evaluated because
60 * it will then be replaced with an ILOC or GLOC.
61 */
62 #undef SCM_RECKLESS
63 #define SCM_CAUTIOUS
64
65 /* After looking up a local for the first time, rewrite the
66 * code graph, caching its position.
67 */
68 #define MEMOIZE_LOCALS
69
70 /* All the number support there is.
71 */
72 #define SCM_FLOATS
73 #define BIGNUMS
74
75 /* GC should relinquish empty cons-pair arenas.
76 */
77 #define GC_FREE_SEGMENTS
78
79 /* Provide a scheme-accessible count-down timer that
80 * generates a pseudo-interrupt.
81 */
82 #define TICKS
83
84
85 /* Use engineering notation when converting numbers strings?
86 */
87 #undef ENGNOT
88
89 #undef SCM_CAREFUL_INTS
90 \f
91 /* {Unsupported Options}
92 *
93 * These must be defined as given here.
94 */
95
96
97 #define CCLO
98
99 /* Guile Scheme supports the #f/() distinction; Guile Lisp won't. We
100 have horrible plans for their unification. */
101 #undef SICP
102
103 \f
104
105 /* Random options (not yet supported or in final form). */
106
107 #define STACK_CHECKING
108 #undef NO_CEVAL_STACK_CHECKING
109
110 \f
111 /* What did the configure script discover about the outside world? */
112 #include "libguile/scmconfig.h"
113
114 #ifdef HAVE_LONG_LONGS
115
116 /* Some auto-generated .h files contain unused prototypes
117 * that need these typedefs.
118 */
119 typedef long long long_long;
120 typedef unsigned long long ulong_long;
121
122 #endif /* HAVE_LONG_LONGS */
123
124 \f
125
126 /* Define
127 *
128 * SCM_CHAR_CODE_LIMIT == UCHAR_MAX + 1
129 * SCM_MOST_POSITIVE_FIXNUM (LONG_MAX>>2)
130 * SCM_MOST_NEGATIVE_FIXNUM == SCM_SRS((long)LONG_MIN, 2)
131 */
132
133 #ifdef HAVE_LIMITS_H
134 # include <limits.h>
135 # ifdef UCHAR_MAX
136 # define SCM_CHAR_CODE_LIMIT (UCHAR_MAX+1L)
137 # else
138 # define SCM_CHAR_CODE_LIMIT 256L
139 # endif /* def UCHAR_MAX */
140 # define SCM_MOST_POSITIVE_FIXNUM (LONG_MAX>>2)
141 # ifdef _UNICOS /* Stupid cray bug */
142 # define SCM_MOST_NEGATIVE_FIXNUM ((long)LONG_MIN/4)
143 # else
144 # define SCM_MOST_NEGATIVE_FIXNUM SCM_SRS((long)LONG_MIN, 2)
145 # endif /* UNICOS */
146 #else
147 # define SCM_CHAR_CODE_LIMIT 256L
148 # define SCM_MOST_POSITIVE_FIXNUM ((long)((unsigned long)~0L>>3))
149 # if (0 != ~0)
150 # define SCM_MOST_NEGATIVE_FIXNUM (-SCM_MOST_POSITIVE_FIXNUM-1)
151 # else
152 # define SCM_MOST_NEGATIVE_FIXNUM (-SCM_MOST_POSITIVE_FIXNUM)
153 # endif /* (0 != ~0) */
154 #endif /* def HAVE_LIMITS_H */
155 \f
156
157 #ifdef STDC_HEADERS
158 # include <stdlib.h>
159 # ifdef AMIGA
160 # include <stddef.h>
161 # endif /* def AMIGA */
162 # define scm_sizet size_t
163 #else
164 # ifdef _SIZE_T
165 # define scm_sizet size_t
166 # else
167 # define scm_sizet unsigned int
168 # endif /* def _SIZE_T */
169 #endif /* def STDC_HEADERS */
170
171 \f
172
173 #include "libguile/tags.h"
174
175 \f
176 #ifdef vms
177 # ifndef CHEAP_CONTINUATIONS
178 typedef int jmp_buf[17];
179 extern int setjump(jmp_buf env);
180 extern int longjump(jmp_buf env, int ret);
181 # define setjmp setjump
182 # define longjmp longjump
183 # else
184 # include <setjmp.h>
185 # endif
186 #else /* ndef vms */
187 # ifdef _CRAY1
188 typedef int jmp_buf[112];
189 extern int setjump(jmp_buf env);
190 extern int longjump(jmp_buf env, int ret);
191 # define setjmp setjump
192 # define longjmp longjump
193 # else /* ndef _CRAY1 */
194 # include <setjmp.h>
195 # endif /* ndef _CRAY1 */
196 #endif /* ndef vms */
197
198 /* James Clark came up with this neat one instruction fix for
199 * continuations on the SPARC. It flushes the register windows so
200 * that all the state of the process is contained in the stack.
201 */
202
203 #ifdef sparc
204 # define SCM_FLUSH_REGISTER_WINDOWS asm("ta 3")
205 #else
206 # define SCM_FLUSH_REGISTER_WINDOWS /* empty */
207 #endif
208
209 /* If stack is not longword aligned then
210 */
211
212 /* #define SHORT_ALIGN */
213 #ifdef THINK_C
214 # define SHORT_ALIGN
215 #endif
216 #ifdef MSDOS
217 # define SHORT_ALIGN
218 #endif
219 #ifdef atarist
220 # define SHORT_ALIGN
221 #endif
222
223 #ifdef SHORT_ALIGN
224 typedef short SCM_STACKITEM;
225 #else
226 typedef long SCM_STACKITEM;
227 #endif
228 \f
229
230 #ifndef USE_THREADS
231 #define SCM_THREAD_DEFER
232 #define SCM_THREAD_ALLOW
233 #define SCM_THREAD_REDEFER
234 #define SCM_THREAD_SWITCHING_CODE
235 #endif
236
237 extern unsigned int scm_async_clock;
238
239 #define SCM_ASYNC_TICK \
240 do { \
241 if (0 == --scm_async_clock) \
242 scm_async_click (); \
243 } while(0)
244
245 #ifdef SCM_CAREFUL_INTS
246 #define SCM_CHECK_NOT_DISABLED \
247 if (scm_ints_disabled) \
248 fputs("ints already disabled\n", stderr); \
249
250 #define SCM_CHECK_NOT_ENABLED \
251 if (!scm_ints_disabled) \
252 fputs("ints already enabled\n", stderr); \
253
254 #else
255 #define SCM_CHECK_NOT_DISABLED
256 #define SCM_CHECK_NOT_ENABLED
257 #endif
258
259
260 /* Anthony Green writes:
261 When the compiler sees...
262 DEFER_INTS;
263 [critical code here]
264 ALLOW_INTS;
265 ...it doesn't actually promise to keep the critical code within the
266 boundries of the DEFER/ALLOW_INTS instructions. It may very well
267 schedule it outside of the magic defined in those macros.
268
269 However, GCC's volatile asm feature forms a barrier over which code is
270 never moved. So if you add...
271 asm ("");
272 ...to each of the DEFER_INTS and ALLOW_INTS macros, the critical
273 code will always remain in place. asm's without inputs or outputs
274 are implicitly volatile. */
275 #ifdef __GNUC__
276 #define SCM_FENCE asm /* volatile */ ("")
277 #else
278 #define SCM_FENCE
279 #endif
280
281 #define SCM_DEFER_INTS \
282 do { \
283 SCM_FENCE; \
284 SCM_CHECK_NOT_DISABLED; \
285 SCM_THREAD_DEFER; \
286 SCM_FENCE; \
287 scm_ints_disabled = 1; \
288 SCM_FENCE; \
289 } while (0)
290
291
292 #define SCM_ALLOW_INTS_ONLY \
293 do { \
294 SCM_THREAD_ALLOW; \
295 scm_ints_disabled = 0; \
296 } while (0)
297
298
299 #define SCM_ALLOW_INTS \
300 do { \
301 SCM_FENCE; \
302 SCM_CHECK_NOT_ENABLED; \
303 SCM_THREAD_SWITCHING_CODE; \
304 SCM_FENCE; \
305 scm_ints_disabled = 0; \
306 SCM_FENCE; \
307 SCM_THREAD_ALLOW; \
308 SCM_FENCE; \
309 } while (0)
310
311
312 #define SCM_REDEFER_INTS \
313 do { \
314 SCM_FENCE; \
315 SCM_THREAD_REDEFER; \
316 ++scm_ints_disabled; \
317 SCM_FENCE; \
318 } while (0)
319
320
321 #define SCM_REALLOW_INTS \
322 do { \
323 SCM_FENCE; \
324 SCM_THREAD_SWITCHING_CODE; \
325 SCM_FENCE; \
326 --scm_ints_disabled; \
327 SCM_FENCE; \
328 } while (0)
329
330
331 #define SCM_TICK \
332 do { \
333 SCM_DEFER_INTS; \
334 SCM_ALLOW_INTS; \
335 SCM_ASYNC_TICK; \
336 } while (0)
337
338 \f
339
340 /* Classification of critical sections
341 *
342 * When Guile moves to POSIX threads, it won't be possible to prevent
343 * context switching. In fact, the whole idea of context switching is
344 * bogus if threads are run by different processors. Therefore, we
345 * must ultimately eliminate all critical sections or enforce them by
346 * use of mutecis.
347 *
348 * All instances of SCM_DEFER_INTS and SCM_ALLOW_INTS should therefore
349 * be classified and replaced by one of the delimiters below. If you
350 * understand what this is all about, I'd like to encourage you to
351 * help with this task. The set of classes below must of course be
352 * incrementally augmented.
353 *
354 * MDJ 980419 <djurfeldt@nada.kth.se>
355 */
356
357 /* A sections
358 *
359 * Allocation of a cell with type tag in the CAR.
360 *
361 * With POSIX threads, each thread will have a private pool of free
362 * cells. Therefore, this type of section can be removed. But! It
363 * is important that the CDR is initialized first (with the CAR still
364 * indicating a free cell) so that we can guarantee a consistent heap
365 * at all times.
366 */
367
368 #ifdef SCM_POSIX_THREADS
369 #define SCM_ENTER_A_SECTION
370 #define SCM_EXIT_A_SECTION
371 #else
372 #define SCM_ENTER_A_SECTION SCM_DEFER_INTS
373 #define SCM_EXIT_A_SECTION SCM_ALLOW_INTS
374 #endif
375
376 \f
377
378 /** SCM_ASSERT
379 **
380 **/
381
382
383 #ifdef SCM_RECKLESS
384 #define SCM_ASSERT(_cond, _arg, _pos, _subr)
385 #define SCM_ASSERT_TYPE(_cond, _arg, _pos, _subr)
386 #define SCM_ASRTGO(_cond, _label)
387 #else
388 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
389 if (!(_cond)) \
390 scm_wta(_arg, (char *)(_pos), _subr)
391 #define SCM_ASSERT_TYPE(_cond, _arg, _pos, _subr, _msg) \
392 if (!(_cond)) \
393 scm_wrong_type_arg_msg(_subr, _pos, _arg, _msg)
394 #define SCM_ASRTGO(_cond, _label) \
395 if (!(_cond)) \
396 goto _label
397 #endif
398
399 /*
400 * SCM_WTA_DISPATCH
401 */
402
403 extern SCM scm_call_generic_0 (SCM gf);
404
405 #define SCM_WTA_DISPATCH_0(gf, arg, pos, subr) \
406 return ((gf) \
407 ? scm_call_generic_0 ((gf)) \
408 : scm_wta ((arg), (char *) (pos), (subr)))
409 #define SCM_GASSERT0(cond, gf, arg, pos, subr) \
410 if (!(cond)) SCM_WTA_DISPATCH_0((gf), (arg), (pos), (subr))
411
412 extern SCM scm_call_generic_1 (SCM gf, SCM a1);
413
414 #define SCM_WTA_DISPATCH_1(gf, a1, pos, subr) \
415 return ((gf) \
416 ? scm_call_generic_1 ((gf), (a1)) \
417 : scm_wta ((a1), (char *) (pos), (subr)))
418 #define SCM_GASSERT1(cond, gf, a1, pos, subr) \
419 if (!(cond)) SCM_WTA_DISPATCH_1((gf), (a1), (pos), (subr))
420
421 extern SCM scm_call_generic_2 (SCM gf, SCM a1, SCM a2);
422
423 #define SCM_WTA_DISPATCH_2(gf, a1, a2, pos, subr) \
424 return ((gf) \
425 ? scm_call_generic_2 ((gf), (a1), (a2)) \
426 : scm_wta ((pos) == SCM_ARG1 ? (a1) : (a2), (char *) (pos), (subr)))
427 #define SCM_GASSERT2(cond, gf, a1, a2, pos, subr) \
428 if (!(cond)) SCM_WTA_DISPATCH_2((gf), (a1), (a2), (pos), (subr))
429
430 extern SCM scm_apply_generic (SCM gf, SCM args);
431
432 #define SCM_WTA_DISPATCH_n(gf, args, pos, subr) \
433 return ((gf) \
434 ? scm_apply_generic ((gf), (args)) \
435 : scm_wta (scm_list_ref ((args), SCM_MAKINUM ((pos) - 1)), \
436 (char *) (pos), \
437 (subr)))
438 #define SCM_GASSERTn(cond, gf, args, pos, subr) \
439 if (!(cond)) SCM_WTA_DISPATCH_n((gf), (args), (pos), (subr))
440
441 #ifndef SCM_MAGIC_SNARFER
442 /* Let these macros pass through if
443 we are snarfing; thus we can tell the
444 difference between the use of an actual
445 number vs. the use of one of these macros --
446 actual numbers in SCM_VALIDATE_* and SCM_ASSERT
447 constructs must match the formal argument name,
448 but using SCM_ARG* avoids the test */
449
450 #define SCM_ARGn 0
451 #define SCM_ARG1 1
452 #define SCM_ARG2 2
453 #define SCM_ARG3 3
454 #define SCM_ARG4 4
455 #define SCM_ARG5 5
456 #define SCM_ARG6 6
457 #define SCM_ARG7 7
458 /* #define SCM_ARGERR(X) ((X) < SCM_WNA \
459 ? (char *)(X) \
460 : "wrong type argument")
461 */
462
463 /* Following must match entry indexes in scm_errmsgs[].
464 * Also, SCM_WNA must follow the last SCM_ARGn in sequence.
465 */
466 #define SCM_WNA 8
467 /* #define SCM_OVSCM_FLOW 9 */
468 #define SCM_OUTOFRANGE 10
469 #define SCM_NALLOC 11
470 /* #define SCM_STACK_OVFLOW 12 */
471 /* #define SCM_EXIT 13 */
472
473 #endif /* SCM_MAGIC_SNARFER */
474
475 /* (...still matching scm_errmsgs) These
476 * are signals. Signals may become errors
477 * but are distinguished because they first
478 * try to invoke a handler that can resume
479 * the interrupted routine.
480 */
481 #define SCM_HUP_SIGNAL 14
482 #define SCM_INT_SIGNAL 15
483 #define SCM_FPE_SIGNAL 16
484 #define SCM_BUS_SIGNAL 17
485 #define SCM_SEGV_SIGNAL 18
486 #define SCM_ALRM_SIGNAL 19
487 #define SCM_GC_SIGNAL 20
488 #define SCM_TICK_SIGNAL 21
489
490 #define SCM_SIG_ORD(X) ((X) - SCM_HUP_SIGNAL)
491 #define SCM_ORD_SIG(X) ((X) + SCM_HUP_SIGNAL)
492 #define SCM_NUM_SIGS (SCM_SIG_ORD (SCM_TICK_SIGNAL) + 1)
493
494 #if 0
495 struct errdesc
496 {
497 char *msg;
498 char *s_response;
499 short parent_err;
500 };
501
502
503 extern struct errdesc scm_errmsgs[];
504 #endif
505
506 \f
507
508 /* SCM_EXIT_SUCCESS is the default code to return from SCM if no errors
509 * were encountered. SCM_EXIT_FAILURE is the default code to return from
510 * SCM if errors were encountered. The return code can be explicitly
511 * specified in a SCM program with (scm_quit <n>).
512 */
513
514 #ifndef SCM_EXIT_SUCCESS
515 #ifdef vms
516 #define SCM_EXIT_SUCCESS 1
517 #else
518 #define SCM_EXIT_SUCCESS 0
519 #endif /* def vms */
520 #endif /* ndef SCM_EXIT_SUCCESS */
521 #ifndef SCM_EXIT_FAILURE
522 #ifdef vms
523 #define SCM_EXIT_FAILURE 2
524 #else
525 #define SCM_EXIT_FAILURE 1
526 #endif /* def vms */
527 #endif /* ndef SCM_EXIT_FAILURE */
528
529
530
531 \f
532
533 #endif /* __SCMH */