Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / inline.h
1 /* classes: h_files */
2
3 #ifndef SCM_INLINE_H
4 #define SCM_INLINE_H
5
6 /* Copyright (C) 2001, 2002, 2003, 2004, 2006, 2008 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 /* 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
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "libguile/__scm.h"
32
33 #include "libguile/pairs.h"
34 #include "libguile/gc.h"
35 #include "libguile/threads.h"
36 #include "libguile/unif.h"
37 #include "libguile/ports.h"
38 #include "libguile/error.h"
39
40
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
51 `__gnu_inline__' is not used.
52
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__) && (!(((defined __APPLE_CC__) && (__APPLE_CC__ > 5400)) && __STDC_VERSION__ >= 199901L))
58 # define SCM_C_USE_EXTERN_INLINE 1
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
72 #if (!defined SCM_C_INLINE) || (defined SCM_INLINE_C_INCLUDING_INLINE_H) \
73 || (defined SCM_C_USE_EXTERN_INLINE)
74
75 /* The `extern' declarations. They should only appear when used from
76 "inline.c", when `inline' is not supported at all or when "extern inline"
77 is used. */
78
79 #include "libguile/boehm-gc.h"
80
81
82 SCM_API SCM scm_cell (scm_t_bits car, scm_t_bits cdr);
83 SCM_API SCM scm_immutable_cell (scm_t_bits car, scm_t_bits cdr);
84 SCM_API SCM scm_double_cell (scm_t_bits car, scm_t_bits cbr,
85 scm_t_bits ccr, scm_t_bits cdr);
86 SCM_API SCM scm_immutable_double_cell (scm_t_bits car, scm_t_bits cbr,
87 scm_t_bits ccr, scm_t_bits cdr);
88
89 SCM_API SCM scm_array_handle_ref (scm_t_array_handle *h, ssize_t pos);
90 SCM_API void scm_array_handle_set (scm_t_array_handle *h, ssize_t pos, SCM val);
91
92 SCM_API int scm_is_pair (SCM x);
93
94 SCM_API int scm_getc (SCM port);
95 SCM_API void scm_putc (char c, SCM port);
96 SCM_API void scm_puts (const char *str_data, SCM port);
97
98 #endif
99
100
101 #if defined SCM_C_EXTERN_INLINE || defined SCM_INLINE_C_INCLUDING_INLINE_H
102 /* either inlining, or being included from inline.c. We use (and
103 repeat) this long #if test here and below so that we don't have to
104 introduce any extraneous symbols into the public namespace. We
105 only need SCM_C_INLINE to be seen publically . */
106
107 extern unsigned scm_newcell2_count;
108 extern unsigned scm_newcell_count;
109
110
111 #ifndef SCM_INLINE_C_INCLUDING_INLINE_H
112 SCM_C_EXTERN_INLINE
113 #endif
114
115 SCM
116 scm_cell (scm_t_bits car, scm_t_bits cdr)
117 {
118 SCM cell = SCM_PACK ((scm_t_bits) (GC_MALLOC (sizeof (scm_t_cell))));
119
120 /* Initialize the type slot last so that the cell is ignored by the GC
121 until it is completely initialized. This is only relevant when the GC
122 can actually run during this code, which it can't since the GC only runs
123 when all other threads are stopped. */
124 SCM_GC_SET_CELL_WORD (cell, 1, cdr);
125 SCM_GC_SET_CELL_WORD (cell, 0, car);
126
127 return cell;
128 }
129
130 #ifndef SCM_INLINE_C_INCLUDING_INLINE_H
131 SCM_C_EXTERN_INLINE
132 #endif
133 SCM
134 scm_immutable_cell (scm_t_bits car, scm_t_bits cdr)
135 {
136 SCM cell = SCM_PACK ((scm_t_bits) (GC_MALLOC_STUBBORN (sizeof (scm_t_cell))));
137
138 /* Initialize the type slot last so that the cell is ignored by the GC
139 until it is completely initialized. This is only relevant when the GC
140 can actually run during this code, which it can't since the GC only runs
141 when all other threads are stopped. */
142 SCM_GC_SET_CELL_WORD (cell, 1, cdr);
143 SCM_GC_SET_CELL_WORD (cell, 0, car);
144
145 GC_END_STUBBORN_CHANGE ((void *) cell);
146
147 return cell;
148 }
149
150 #ifndef SCM_INLINE_C_INCLUDING_INLINE_H
151 SCM_C_EXTERN_INLINE
152 #endif
153 SCM
154 scm_double_cell (scm_t_bits car, scm_t_bits cbr,
155 scm_t_bits ccr, scm_t_bits cdr)
156 {
157 SCM z;
158
159 z = SCM_PACK ((scm_t_bits) (GC_MALLOC (2 * sizeof (scm_t_cell))));
160 /* Initialize the type slot last so that the cell is ignored by the
161 GC until it is completely initialized. This is only relevant
162 when the GC can actually run during this code, which it can't
163 since the GC only runs when all other threads are stopped.
164 */
165 SCM_GC_SET_CELL_WORD (z, 1, cbr);
166 SCM_GC_SET_CELL_WORD (z, 2, ccr);
167 SCM_GC_SET_CELL_WORD (z, 3, cdr);
168 SCM_GC_SET_CELL_WORD (z, 0, car);
169
170 /* When this function is inlined, it's possible that the last
171 SCM_GC_SET_CELL_WORD above will be adjacent to a following
172 initialization of z. E.g., it occurred in scm_make_real. GCC
173 from around version 3 (e.g., certainly 3.2) began taking
174 advantage of strict C aliasing rules which say that it's OK to
175 interchange the initialization above and the one below when the
176 pointer types appear to differ sufficiently. We don't want that,
177 of course. GCC allows this behaviour to be disabled with the
178 -fno-strict-aliasing option, but would also need to be supplied
179 by Guile users. Instead, the following statements prevent the
180 reordering.
181 */
182 #ifdef __GNUC__
183 __asm__ volatile ("" : : : "memory");
184 #else
185 /* portable version, just in case any other compiler does the same
186 thing. */
187 scm_remember_upto_here_1 (z);
188 #endif
189
190 return z;
191 }
192
193 #ifndef SCM_INLINE_C_INCLUDING_INLINE_H
194 SCM_C_EXTERN_INLINE
195 #endif
196 SCM
197 scm_immutable_double_cell (scm_t_bits car, scm_t_bits cbr,
198 scm_t_bits ccr, scm_t_bits cdr)
199 {
200 SCM z;
201
202 z = SCM_PACK ((scm_t_bits) (GC_MALLOC_STUBBORN (2 * sizeof (scm_t_cell))));
203 /* Initialize the type slot last so that the cell is ignored by the
204 GC until it is completely initialized. This is only relevant
205 when the GC can actually run during this code, which it can't
206 since the GC only runs when all other threads are stopped.
207 */
208 SCM_GC_SET_CELL_WORD (z, 1, cbr);
209 SCM_GC_SET_CELL_WORD (z, 2, ccr);
210 SCM_GC_SET_CELL_WORD (z, 3, cdr);
211 SCM_GC_SET_CELL_WORD (z, 0, car);
212
213 GC_END_STUBBORN_CHANGE ((void *) z);
214
215 /* When this function is inlined, it's possible that the last
216 SCM_GC_SET_CELL_WORD above will be adjacent to a following
217 initialization of z. E.g., it occurred in scm_make_real. GCC
218 from around version 3 (e.g., certainly 3.2) began taking
219 advantage of strict C aliasing rules which say that it's OK to
220 interchange the initialization above and the one below when the
221 pointer types appear to differ sufficiently. We don't want that,
222 of course. GCC allows this behaviour to be disabled with the
223 -fno-strict-aliasing option, but would also need to be supplied
224 by Guile users. Instead, the following statements prevent the
225 reordering.
226 */
227 #ifdef __GNUC__
228 __asm__ volatile ("" : : : "memory");
229 #else
230 /* portable version, just in case any other compiler does the same
231 thing. */
232 scm_remember_upto_here_1 (z);
233 #endif
234
235 return z;
236 }
237
238 #ifndef SCM_INLINE_C_INCLUDING_INLINE_H
239 SCM_C_EXTERN_INLINE
240 #endif
241 SCM
242 scm_array_handle_ref (scm_t_array_handle *h, ssize_t p)
243 {
244 return h->ref (h, p);
245 }
246
247 #ifndef SCM_INLINE_C_INCLUDING_INLINE_H
248 SCM_C_EXTERN_INLINE
249 #endif
250 void
251 scm_array_handle_set (scm_t_array_handle *h, ssize_t p, SCM v)
252 {
253 h->set (h, p, v);
254 }
255
256 #ifndef SCM_INLINE_C_INCLUDING_INLINE_H
257 SCM_C_EXTERN_INLINE
258 #endif
259 int
260 scm_is_pair (SCM x)
261 {
262 /* The following "workaround_for_gcc_295" avoids bad code generated by
263 i386 gcc 2.95.4 (the Debian packaged 2.95.4-24 at least).
264
265 Under the default -O2 the inlined SCM_I_CONSP test gets "optimized" so
266 the fetch of the tag word from x is done before confirming it's a
267 non-immediate (SCM_NIMP). Needless to say that bombs badly if x is a
268 immediate. This was seen to afflict scm_srfi1_split_at and something
269 deep in the bowels of ceval(). In both cases segvs resulted from
270 deferencing a random immediate value. srfi-1.test exposes the problem
271 through a short list, the immediate being SCM_EOL in that case.
272 Something in syntax.test exposed the ceval() problem.
273
274 Just "volatile SCM workaround_for_gcc_295 = lst" is enough to avoid the
275 problem, without even using that variable. The "w=w" is just to
276 prevent a warning about it being unused.
277 */
278 #if defined (__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ == 95
279 volatile SCM workaround_for_gcc_295 = x;
280 workaround_for_gcc_295 = workaround_for_gcc_295;
281 #endif
282
283 return SCM_I_CONSP (x);
284 }
285
286
287 /* Port I/O. */
288
289 #ifndef SCM_INLINE_C_INCLUDING_INLINE_H
290 SCM_C_EXTERN_INLINE
291 #endif
292 int
293 scm_getc (SCM port)
294 {
295 int c;
296 scm_t_port *pt = SCM_PTAB_ENTRY (port);
297
298 if (pt->rw_active == SCM_PORT_WRITE)
299 /* may be marginally faster than calling scm_flush. */
300 scm_ptobs[SCM_PTOBNUM (port)].flush (port);
301
302 if (pt->rw_random)
303 pt->rw_active = SCM_PORT_READ;
304
305 if (pt->read_pos >= pt->read_end)
306 {
307 if (scm_fill_input (port) == EOF)
308 return EOF;
309 }
310
311 c = *(pt->read_pos++);
312
313 switch (c)
314 {
315 case '\a':
316 break;
317 case '\b':
318 SCM_DECCOL (port);
319 break;
320 case '\n':
321 SCM_INCLINE (port);
322 break;
323 case '\r':
324 SCM_ZEROCOL (port);
325 break;
326 case '\t':
327 SCM_TABCOL (port);
328 break;
329 default:
330 SCM_INCCOL (port);
331 break;
332 }
333
334 return c;
335 }
336
337 #ifndef SCM_INLINE_C_INCLUDING_INLINE_H
338 SCM_C_EXTERN_INLINE
339 #endif
340 void
341 scm_putc (char c, SCM port)
342 {
343 SCM_ASSERT_TYPE (SCM_OPOUTPORTP (port), port, 0, NULL, "output port");
344 scm_lfwrite (&c, 1, port);
345 }
346
347 #ifndef SCM_INLINE_C_INCLUDING_INLINE_H
348 SCM_C_EXTERN_INLINE
349 #endif
350 void
351 scm_puts (const char *s, SCM port)
352 {
353 SCM_ASSERT_TYPE (SCM_OPOUTPORTP (port), port, 0, NULL, "output port");
354 scm_lfwrite (s, strlen (s), port);
355 }
356
357
358 #endif
359 #endif