Revert "(scm_shell_usage): Note need for subscription to bug-guile@gnu.org."
[bpt/guile.git] / libguile / inline.h
CommitLineData
16ea9620
MV
1/* classes: h_files */
2
3#ifndef SCM_INLINE_H
4#define SCM_INLINE_H
5
3f520967 6/* Copyright (C) 2001, 2002, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
16ea9620 7 *
73be1d9e
MV
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.
16ea9620 12 *
73be1d9e 13 * This library is distributed in the hope that it will be useful,
16ea9620 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
16ea9620 17 *
73be1d9e
MV
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
92205699 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 21 */
16ea9620
MV
22
23/* This file is for inline functions. On platforms that don't support
24 inlining functions, they are turned into ordinary functions. See
25 "inline.c".
26*/
27
1e71eafb 28#include <stdio.h>
f5c2af4b
LC
29#include <string.h>
30
31#include "libguile/__scm.h"
1e71eafb 32
16ea9620
MV
33#include "libguile/pairs.h"
34#include "libguile/gc.h"
9bc4701c 35#include "libguile/threads.h"
9598a406 36#include "libguile/unif.h"
f5c2af4b
LC
37#include "libguile/ports.h"
38#include "libguile/error.h"
16ea9620 39
c8a1bdc4 40
979eade6
LC
41#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
42
43/* GCC has `__inline__' in all modes, including strict ansi. GCC 4.3 and
44 above with `-std=c99' or `-std=gnu99' implements ISO C99 inline semantics,
45 unless `-fgnu89-inline' is used. Here we want GNU "extern inline"
46 semantics, hence the `__gnu_inline__' attribute, in accordance with:
47 http://gcc.gnu.org/gcc-4.3/porting_to.html .
48
49 With GCC 4.2, `__GNUC_STDC_INLINE__' is never defined (because C99 inline
50 semantics are not supported), but a warning is issued in C99 mode if
7dc9ae71 51 `__gnu_inline__' is not used.
979eade6 52
7dc9ae71
LC
53 Apple's GCC build >5400 (since Xcode 3.0) doesn't support GNU inline in
54 C99 mode and doesn't define `__GNUC_STDC_INLINE__'. Fall back to "static
55 inline" in that case. */
56
57# if (defined __GNUC__) && (!(__APPLE_CC__ > 5400 && __STDC_VERSION__ >= 199901L))
07db6fcd 58# define SCM_C_USE_EXTERN_INLINE 1
979eade6
LC
59# if (defined __GNUC_STDC_INLINE__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 2)
60# define SCM_C_EXTERN_INLINE \
61 extern __inline__ __attribute__ ((__gnu_inline__))
62# else
63# define SCM_C_EXTERN_INLINE extern __inline__
64# endif
65# elif (defined SCM_C_INLINE)
66# define SCM_C_EXTERN_INLINE static SCM_C_INLINE
67# endif
68
69#endif /* SCM_INLINE_C_INCLUDING_INLINE_H */
70
71
07db6fcd
LC
72#if (!defined SCM_C_INLINE) || (defined SCM_INLINE_C_INCLUDING_INLINE_H) \
73 || (defined SCM_C_USE_EXTERN_INLINE)
3f520967
LC
74
75/* The `extern' declarations. They should only appear when used from
07db6fcd
LC
76 "inline.c", when `inline' is not supported at all or when "extern inline"
77 is used. */
3f520967 78
c8a1bdc4
HWN
79SCM_API SCM scm_cell (scm_t_bits car, scm_t_bits cdr);
80SCM_API SCM scm_double_cell (scm_t_bits car, scm_t_bits cbr,
81 scm_t_bits ccr, scm_t_bits cdr);
82
9598a406
MV
83SCM_API SCM scm_array_handle_ref (scm_t_array_handle *h, ssize_t pos);
84SCM_API void scm_array_handle_set (scm_t_array_handle *h, ssize_t pos, SCM val);
16ea9620 85
3f520967
LC
86SCM_API int scm_is_pair (SCM x);
87
f5c2af4b
LC
88SCM_API int scm_getc (SCM port);
89SCM_API void scm_putc (char c, SCM port);
90SCM_API void scm_puts (const char *str_data, SCM port);
91
3f520967
LC
92#endif
93
60e7529a 94
9dca8935 95#if defined SCM_C_EXTERN_INLINE || defined SCM_INLINE_C_INCLUDING_INLINE_H
60e7529a
RB
96/* either inlining, or being included from inline.c. We use (and
97 repeat) this long #if test here and below so that we don't have to
98 introduce any extraneous symbols into the public namespace. We
99 only need SCM_C_INLINE to be seen publically . */
c8a1bdc4
HWN
100
101extern unsigned scm_newcell2_count;
102extern unsigned scm_newcell_count;
103
979eade6 104
9dca8935 105#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
979eade6 106SCM_C_EXTERN_INLINE
60e7529a 107#endif
c8a1bdc4 108SCM
228a24ef 109scm_cell (scm_t_bits car, scm_t_bits cdr)
16ea9620
MV
110{
111 SCM z;
9bc4701c 112 SCM *freelist = SCM_FREELIST_LOC (scm_i_freelist);
16ea9620 113
d2e53ed6 114 if (scm_is_null (*freelist))
9bc4701c 115 z = scm_gc_for_newcell (&scm_i_master_freelist, freelist);
c8a1bdc4 116 else
16ea9620 117 {
9bc4701c
MD
118 z = *freelist;
119 *freelist = SCM_FREE_CELL_CDR (*freelist);
16ea9620 120 }
c8a1bdc4 121
c8a1bdc4
HWN
122#if (SCM_DEBUG_CELL_ACCESSES == 1)
123 if (scm_debug_cell_accesses_p)
1e71eafb
HWN
124 {
125 if (SCM_GC_MARK_P (z))
126 {
127 fprintf(stderr, "scm_cell tried to allocate a marked cell.\n");
128 abort();
129 }
6b69393d 130 else if (SCM_GC_CELL_WORD(z, 0) != scm_tc_free_cell)
1e71eafb
HWN
131 {
132 fprintf(stderr, "cell from freelist is not a free cell.\n");
133 abort();
134 }
135 }
136
7ddb9baf 137#if (SCM_DEBUG_MARKING_API == 0)
1e71eafb
HWN
138 /*
139 Always set mark. Otherwise cells that are alloced before
140 scm_debug_cell_accesses_p is toggled seem invalid.
141 */
142 SCM_SET_GC_MARK (z);
7ddb9baf
HWN
143#endif /* SCM_DEBUG_MARKING_API */
144
1e71eafb
HWN
145 /*
146 TODO: figure out if this use of mark bits is valid with
147 threading. What if another thread is doing GC at this point
148 ... ?
149 */
c8a1bdc4 150#endif
16ea9620 151
c8a1bdc4 152
16ea9620
MV
153 /* Initialize the type slot last so that the cell is ignored by the
154 GC until it is completely initialized. This is only relevant
b2a339f6
MV
155 when the GC can actually run during this code, which it can't
156 since the GC only runs when all other threads are stopped.
16ea9620 157 */
1fc8902f
DH
158 SCM_GC_SET_CELL_WORD (z, 1, cdr);
159 SCM_GC_SET_CELL_WORD (z, 0, car);
16ea9620 160
eab1b259
HWN
161#if (SCM_DEBUG_CELL_ACCESSES == 1)
162 if (scm_expensive_debug_cell_accesses_p )
163 scm_i_expensive_validation_check (z);
164#endif
c8a1bdc4 165
16ea9620
MV
166 return z;
167}
168
9dca8935 169#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
979eade6 170SCM_C_EXTERN_INLINE
60e7529a 171#endif
c8a1bdc4 172SCM
228a24ef
DH
173scm_double_cell (scm_t_bits car, scm_t_bits cbr,
174 scm_t_bits ccr, scm_t_bits cdr)
16ea9620 175{
5e1e20c8
MV
176 SCM z;
177 SCM *freelist = SCM_FREELIST_LOC (scm_i_freelist2);
178
d2e53ed6 179 if (scm_is_null (*freelist))
9bc4701c 180 z = scm_gc_for_newcell (&scm_i_master_freelist2, freelist);
16ea9620
MV
181 else
182 {
9bc4701c
MD
183 z = *freelist;
184 *freelist = SCM_FREE_CELL_CDR (*freelist);
16ea9620
MV
185 }
186
187 /* Initialize the type slot last so that the cell is ignored by the
188 GC until it is completely initialized. This is only relevant
b2a339f6
MV
189 when the GC can actually run during this code, which it can't
190 since the GC only runs when all other threads are stopped.
16ea9620 191 */
1fc8902f
DH
192 SCM_GC_SET_CELL_WORD (z, 1, cbr);
193 SCM_GC_SET_CELL_WORD (z, 2, ccr);
194 SCM_GC_SET_CELL_WORD (z, 3, cdr);
195 SCM_GC_SET_CELL_WORD (z, 0, car);
16ea9620 196
c8a1bdc4
HWN
197#if (SCM_DEBUG_CELL_ACCESSES == 1)
198 if (scm_debug_cell_accesses_p)
199 {
200 if (SCM_GC_MARK_P (z))
201 {
202 fprintf(stderr,
203 "scm_double_cell tried to allocate a marked cell.\n");
204 abort();
205 }
c8a1bdc4 206 }
7ddb9baf 207#if (SCM_DEBUG_MARKING_API == 0)
4ad0814a
HWN
208 /* see above. */
209 SCM_SET_GC_MARK (z);
7ddb9baf
HWN
210#endif /* SCM_DEBUG_MARKING_API */
211
3553e1d1
GH
212#endif
213
214 /* When this function is inlined, it's possible that the last
215 SCM_GC_SET_CELL_WORD above will be adjacent to a following
216 initialization of z. E.g., it occurred in scm_make_real. GCC
217 from around version 3 (e.g., certainly 3.2) began taking
218 advantage of strict C aliasing rules which say that it's OK to
219 interchange the initialization above and the one below when the
220 pointer types appear to differ sufficiently. We don't want that,
221 of course. GCC allows this behaviour to be disabled with the
222 -fno-strict-aliasing option, but would also need to be supplied
223 by Guile users. Instead, the following statements prevent the
224 reordering.
225 */
226#ifdef __GNUC__
cb975c21 227 __asm__ volatile ("" : : : "memory");
3553e1d1
GH
228#else
229 /* portable version, just in case any other compiler does the same
230 thing. */
231 scm_remember_upto_here_1 (z);
c8a1bdc4 232#endif
6253f3f1 233
c8a1bdc4
HWN
234 return z;
235}
6253f3f1 236
9dca8935 237#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
979eade6 238SCM_C_EXTERN_INLINE
9598a406
MV
239#endif
240SCM
241scm_array_handle_ref (scm_t_array_handle *h, ssize_t p)
242{
243 return h->ref (h, p);
244}
eab1b259 245
9dca8935 246#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
979eade6 247SCM_C_EXTERN_INLINE
9598a406
MV
248#endif
249void
250scm_array_handle_set (scm_t_array_handle *h, ssize_t p, SCM v)
251{
252 h->set (h, p, v);
253}
eab1b259 254
9dca8935 255#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
979eade6 256SCM_C_EXTERN_INLINE
d5ad4aa6
MV
257#endif
258int
259scm_is_pair (SCM x)
260{
23f2b9a3
KR
261 /* The following "workaround_for_gcc_295" avoids bad code generated by
262 i386 gcc 2.95.4 (the Debian packaged 2.95.4-24 at least).
263
264 Under the default -O2 the inlined SCM_I_CONSP test gets "optimized" so
265 the fetch of the tag word from x is done before confirming it's a
266 non-immediate (SCM_NIMP). Needless to say that bombs badly if x is a
267 immediate. This was seen to afflict scm_srfi1_split_at and something
268 deep in the bowels of ceval(). In both cases segvs resulted from
269 deferencing a random immediate value. srfi-1.test exposes the problem
270 through a short list, the immediate being SCM_EOL in that case.
271 Something in syntax.test exposed the ceval() problem.
272
273 Just "volatile SCM workaround_for_gcc_295 = lst" is enough to avoid the
274 problem, without even using that variable. The "w=w" is just to
275 prevent a warning about it being unused.
276 */
277#if defined (__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ == 95
278 volatile SCM workaround_for_gcc_295 = x;
279 workaround_for_gcc_295 = workaround_for_gcc_295;
280#endif
281
d5ad4aa6
MV
282 return SCM_I_CONSP (x);
283}
284
f5c2af4b
LC
285
286/* Port I/O. */
287
288#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
289SCM_C_EXTERN_INLINE
290#endif
291int
292scm_getc (SCM port)
293{
294 int c;
295 scm_t_port *pt = SCM_PTAB_ENTRY (port);
296
297 if (pt->rw_active == SCM_PORT_WRITE)
298 /* may be marginally faster than calling scm_flush. */
299 scm_ptobs[SCM_PTOBNUM (port)].flush (port);
300
301 if (pt->rw_random)
302 pt->rw_active = SCM_PORT_READ;
303
304 if (pt->read_pos >= pt->read_end)
305 {
306 if (scm_fill_input (port) == EOF)
307 return EOF;
308 }
309
310 c = *(pt->read_pos++);
311
312 switch (c)
313 {
314 case '\a':
315 break;
316 case '\b':
317 SCM_DECCOL (port);
318 break;
319 case '\n':
320 SCM_INCLINE (port);
321 break;
322 case '\r':
323 SCM_ZEROCOL (port);
324 break;
325 case '\t':
326 SCM_TABCOL (port);
327 break;
328 default:
329 SCM_INCCOL (port);
330 break;
331 }
332
333 return c;
334}
335
336#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
337SCM_C_EXTERN_INLINE
338#endif
339void
340scm_putc (char c, SCM port)
341{
342 SCM_ASSERT_TYPE (SCM_OPOUTPORTP (port), port, 0, NULL, "output port");
343 scm_lfwrite (&c, 1, port);
344}
345
346#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
347SCM_C_EXTERN_INLINE
348#endif
349void
350scm_puts (const char *s, SCM port)
351{
352 SCM_ASSERT_TYPE (SCM_OPOUTPORTP (port), port, 0, NULL, "output port");
353 scm_lfwrite (s, strlen (s), port);
354}
355
356
16ea9620 357#endif
16ea9620 358#endif