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