Use immutable cells for closures.
[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)
16ea9620 74
3f520967 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. */
c8a1bdc4 78
e7bca227 79#include "libguile/boehm-gc.h"
26224b3f
LC
80
81
c8a1bdc4 82SCM_API SCM scm_cell (scm_t_bits car, scm_t_bits cdr);
53ea4fdf 83SCM_API SCM scm_immutable_cell (scm_t_bits car, scm_t_bits cdr);
c8a1bdc4
HWN
84SCM_API SCM scm_double_cell (scm_t_bits car, scm_t_bits cbr,
85 scm_t_bits ccr, scm_t_bits cdr);
86
9598a406
MV
87SCM_API SCM scm_array_handle_ref (scm_t_array_handle *h, ssize_t pos);
88SCM_API void scm_array_handle_set (scm_t_array_handle *h, ssize_t pos, SCM val);
16ea9620 89
3f520967 90SCM_API int scm_is_pair (SCM x);
60e7529a 91
f5c2af4b
LC
92SCM_API int scm_getc (SCM port);
93SCM_API void scm_putc (char c, SCM port);
94SCM_API void scm_puts (const char *str_data, SCM port);
95
3f520967
LC
96#endif
97
60e7529a 98
9dca8935 99#if defined SCM_C_EXTERN_INLINE || defined SCM_INLINE_C_INCLUDING_INLINE_H
60e7529a
RB
100/* either inlining, or being included from inline.c. We use (and
101 repeat) this long #if test here and below so that we don't have to
102 introduce any extraneous symbols into the public namespace. We
103 only need SCM_C_INLINE to be seen publically . */
c8a1bdc4
HWN
104
105extern unsigned scm_newcell2_count;
106extern unsigned scm_newcell_count;
107
979eade6 108
9dca8935 109#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
979eade6 110SCM_C_EXTERN_INLINE
60e7529a 111#endif
26224b3f 112
c8a1bdc4 113SCM
228a24ef 114scm_cell (scm_t_bits car, scm_t_bits cdr)
16ea9620 115{
378f2625 116 SCM cell = SCM_PACK ((scm_t_bits) (GC_MALLOC (sizeof (scm_t_cell))));
c8a1bdc4 117
c812243b
LC
118 /* Initialize the type slot last so that the cell is ignored by the GC
119 until it is completely initialized. This is only relevant when the GC
120 can actually run during this code, which it can't since the GC only runs
121 when all other threads are stopped. */
26224b3f 122 SCM_GC_SET_CELL_WORD (cell, 1, cdr);
c812243b 123 SCM_GC_SET_CELL_WORD (cell, 0, car);
16ea9620 124
26224b3f 125 return cell;
16ea9620
MV
126}
127
53ea4fdf
LC
128#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
129SCM_C_EXTERN_INLINE
130#endif
131SCM
132scm_immutable_cell (scm_t_bits car, scm_t_bits cdr)
133{
134 SCM cell = SCM_PACK ((scm_t_bits) (GC_MALLOC_STUBBORN (sizeof (scm_t_cell))));
135
136 /* Initialize the type slot last so that the cell is ignored by the GC
137 until it is completely initialized. This is only relevant when the GC
138 can actually run during this code, which it can't since the GC only runs
139 when all other threads are stopped. */
140 SCM_GC_SET_CELL_WORD (cell, 1, cdr);
141 SCM_GC_SET_CELL_WORD (cell, 0, car);
142
143 GC_END_STUBBORN_CHANGE ((void *) cell);
144
145 return cell;
146}
147
9dca8935 148#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
979eade6 149SCM_C_EXTERN_INLINE
60e7529a 150#endif
c8a1bdc4 151SCM
228a24ef
DH
152scm_double_cell (scm_t_bits car, scm_t_bits cbr,
153 scm_t_bits ccr, scm_t_bits cdr)
16ea9620 154{
5e1e20c8 155 SCM z;
c8a1bdc4 156
e7bca227 157 z = SCM_PACK ((scm_t_bits) (GC_MALLOC (2 * sizeof (scm_t_cell))));
16ea9620
MV
158 /* Initialize the type slot last so that the cell is ignored by the
159 GC until it is completely initialized. This is only relevant
b2a339f6
MV
160 when the GC can actually run during this code, which it can't
161 since the GC only runs when all other threads are stopped.
16ea9620 162 */
1fc8902f
DH
163 SCM_GC_SET_CELL_WORD (z, 1, cbr);
164 SCM_GC_SET_CELL_WORD (z, 2, ccr);
165 SCM_GC_SET_CELL_WORD (z, 3, cdr);
166 SCM_GC_SET_CELL_WORD (z, 0, car);
16ea9620 167
3553e1d1
GH
168 /* When this function is inlined, it's possible that the last
169 SCM_GC_SET_CELL_WORD above will be adjacent to a following
170 initialization of z. E.g., it occurred in scm_make_real. GCC
171 from around version 3 (e.g., certainly 3.2) began taking
172 advantage of strict C aliasing rules which say that it's OK to
173 interchange the initialization above and the one below when the
174 pointer types appear to differ sufficiently. We don't want that,
175 of course. GCC allows this behaviour to be disabled with the
176 -fno-strict-aliasing option, but would also need to be supplied
177 by Guile users. Instead, the following statements prevent the
178 reordering.
179 */
180#ifdef __GNUC__
cb975c21 181 __asm__ volatile ("" : : : "memory");
3553e1d1
GH
182#else
183 /* portable version, just in case any other compiler does the same
184 thing. */
185 scm_remember_upto_here_1 (z);
c8a1bdc4 186#endif
6253f3f1 187
c8a1bdc4
HWN
188 return z;
189}
6253f3f1 190
9dca8935 191#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
979eade6 192SCM_C_EXTERN_INLINE
9598a406
MV
193#endif
194SCM
195scm_array_handle_ref (scm_t_array_handle *h, ssize_t p)
196{
197 return h->ref (h, p);
198}
eab1b259 199
9dca8935 200#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
979eade6 201SCM_C_EXTERN_INLINE
9598a406
MV
202#endif
203void
204scm_array_handle_set (scm_t_array_handle *h, ssize_t p, SCM v)
205{
206 h->set (h, p, v);
207}
eab1b259 208
9dca8935 209#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
979eade6 210SCM_C_EXTERN_INLINE
d5ad4aa6
MV
211#endif
212int
213scm_is_pair (SCM x)
214{
23f2b9a3
KR
215 /* The following "workaround_for_gcc_295" avoids bad code generated by
216 i386 gcc 2.95.4 (the Debian packaged 2.95.4-24 at least).
217
218 Under the default -O2 the inlined SCM_I_CONSP test gets "optimized" so
219 the fetch of the tag word from x is done before confirming it's a
220 non-immediate (SCM_NIMP). Needless to say that bombs badly if x is a
221 immediate. This was seen to afflict scm_srfi1_split_at and something
222 deep in the bowels of ceval(). In both cases segvs resulted from
223 deferencing a random immediate value. srfi-1.test exposes the problem
224 through a short list, the immediate being SCM_EOL in that case.
225 Something in syntax.test exposed the ceval() problem.
226
227 Just "volatile SCM workaround_for_gcc_295 = lst" is enough to avoid the
228 problem, without even using that variable. The "w=w" is just to
229 prevent a warning about it being unused.
230 */
231#if defined (__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ == 95
232 volatile SCM workaround_for_gcc_295 = x;
233 workaround_for_gcc_295 = workaround_for_gcc_295;
234#endif
235
d5ad4aa6
MV
236 return SCM_I_CONSP (x);
237}
238
f5c2af4b
LC
239
240/* Port I/O. */
241
242#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
243SCM_C_EXTERN_INLINE
244#endif
245int
246scm_getc (SCM port)
247{
248 int c;
249 scm_t_port *pt = SCM_PTAB_ENTRY (port);
250
251 if (pt->rw_active == SCM_PORT_WRITE)
252 /* may be marginally faster than calling scm_flush. */
253 scm_ptobs[SCM_PTOBNUM (port)].flush (port);
254
255 if (pt->rw_random)
256 pt->rw_active = SCM_PORT_READ;
257
258 if (pt->read_pos >= pt->read_end)
259 {
260 if (scm_fill_input (port) == EOF)
261 return EOF;
262 }
263
264 c = *(pt->read_pos++);
265
266 switch (c)
267 {
268 case '\a':
269 break;
270 case '\b':
271 SCM_DECCOL (port);
272 break;
273 case '\n':
274 SCM_INCLINE (port);
275 break;
276 case '\r':
277 SCM_ZEROCOL (port);
278 break;
279 case '\t':
280 SCM_TABCOL (port);
281 break;
282 default:
283 SCM_INCCOL (port);
284 break;
285 }
286
287 return c;
288}
289
290#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
291SCM_C_EXTERN_INLINE
292#endif
293void
294scm_putc (char c, SCM port)
295{
296 SCM_ASSERT_TYPE (SCM_OPOUTPORTP (port), port, 0, NULL, "output port");
297 scm_lfwrite (&c, 1, port);
298}
299
300#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
301SCM_C_EXTERN_INLINE
302#endif
303void
304scm_puts (const char *s, SCM port)
305{
306 SCM_ASSERT_TYPE (SCM_OPOUTPORTP (port), port, 0, NULL, "output port");
307 scm_lfwrite (s, strlen (s), port);
308}
309
310
16ea9620 311#endif
16ea9620 312#endif