Commit | Line | Data |
---|---|---|
16ea9620 MV |
1 | /* classes: h_files */ |
2 | ||
3 | #ifndef SCM_INLINE_H | |
4 | #define SCM_INLINE_H | |
5 | ||
452c5ad9 LC |
6 | /* Copyright (C) 2001, 2002, 2003, 2004, 2006, 2008, 2009, 2010, |
7 | * 2011 Free Software Foundation, Inc. | |
16ea9620 | 8 | * |
73be1d9e | 9 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
10 | * modify it under the terms of the GNU Lesser General Public License |
11 | * as published by the Free Software Foundation; either version 3 of | |
12 | * the License, or (at your option) any later version. | |
16ea9620 | 13 | * |
53befeb7 NJ |
14 | * This library is distributed in the hope that it will be useful, but |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | * Lesser General Public License for more details. | |
16ea9620 | 18 | * |
73be1d9e MV |
19 | * You should have received a copy of the GNU Lesser General Public |
20 | * License along with this library; if not, write to the Free Software | |
53befeb7 NJ |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 | * 02110-1301 USA | |
73be1d9e | 23 | */ |
16ea9620 MV |
24 | |
25 | /* This file is for inline functions. On platforms that don't support | |
26 | inlining functions, they are turned into ordinary functions. See | |
27 | "inline.c". | |
28 | */ | |
29 | ||
1e71eafb | 30 | #include <stdio.h> |
f5c2af4b LC |
31 | #include <string.h> |
32 | ||
33 | #include "libguile/__scm.h" | |
1e71eafb | 34 | |
16ea9620 MV |
35 | #include "libguile/pairs.h" |
36 | #include "libguile/gc.h" | |
9bc4701c | 37 | #include "libguile/threads.h" |
2a610be5 | 38 | #include "libguile/array-handle.h" |
f5c2af4b | 39 | #include "libguile/ports.h" |
2a610be5 | 40 | #include "libguile/numbers.h" |
f5c2af4b | 41 | #include "libguile/error.h" |
16ea9620 | 42 | |
c8a1bdc4 | 43 | |
979eade6 LC |
44 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H |
45 | ||
46 | /* GCC has `__inline__' in all modes, including strict ansi. GCC 4.3 and | |
47 | above with `-std=c99' or `-std=gnu99' implements ISO C99 inline semantics, | |
48 | unless `-fgnu89-inline' is used. Here we want GNU "extern inline" | |
49 | semantics, hence the `__gnu_inline__' attribute, in accordance with: | |
50 | http://gcc.gnu.org/gcc-4.3/porting_to.html . | |
51 | ||
52 | With GCC 4.2, `__GNUC_STDC_INLINE__' is never defined (because C99 inline | |
53 | semantics are not supported), but a warning is issued in C99 mode if | |
7dc9ae71 | 54 | `__gnu_inline__' is not used. |
979eade6 | 55 | |
7dc9ae71 LC |
56 | Apple's GCC build >5400 (since Xcode 3.0) doesn't support GNU inline in |
57 | C99 mode and doesn't define `__GNUC_STDC_INLINE__'. Fall back to "static | |
58 | inline" in that case. */ | |
59 | ||
95a040cd | 60 | # if (defined __GNUC__) && (!(((defined __APPLE_CC__) && (__APPLE_CC__ > 5400)) && __STDC_VERSION__ >= 199901L)) |
07db6fcd | 61 | # define SCM_C_USE_EXTERN_INLINE 1 |
979eade6 LC |
62 | # if (defined __GNUC_STDC_INLINE__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 2) |
63 | # define SCM_C_EXTERN_INLINE \ | |
64 | extern __inline__ __attribute__ ((__gnu_inline__)) | |
65 | # else | |
66 | # define SCM_C_EXTERN_INLINE extern __inline__ | |
67 | # endif | |
68 | # elif (defined SCM_C_INLINE) | |
69 | # define SCM_C_EXTERN_INLINE static SCM_C_INLINE | |
70 | # endif | |
71 | ||
72 | #endif /* SCM_INLINE_C_INCLUDING_INLINE_H */ | |
73 | ||
74 | ||
07db6fcd LC |
75 | #if (!defined SCM_C_INLINE) || (defined SCM_INLINE_C_INCLUDING_INLINE_H) \ |
76 | || (defined SCM_C_USE_EXTERN_INLINE) | |
16ea9620 | 77 | |
3f520967 | 78 | /* The `extern' declarations. They should only appear when used from |
07db6fcd LC |
79 | "inline.c", when `inline' is not supported at all or when "extern inline" |
80 | is used. */ | |
c8a1bdc4 | 81 | |
1c44468d | 82 | #include "libguile/bdw-gc.h" |
26224b3f LC |
83 | |
84 | ||
c8a1bdc4 | 85 | SCM_API SCM scm_cell (scm_t_bits car, scm_t_bits cdr); |
53ea4fdf | 86 | SCM_API SCM scm_immutable_cell (scm_t_bits car, scm_t_bits cdr); |
c8a1bdc4 HWN |
87 | SCM_API SCM scm_double_cell (scm_t_bits car, scm_t_bits cbr, |
88 | scm_t_bits ccr, scm_t_bits cdr); | |
737219dd LC |
89 | SCM_API SCM scm_immutable_double_cell (scm_t_bits car, scm_t_bits cbr, |
90 | scm_t_bits ccr, scm_t_bits cdr); | |
e809758a AW |
91 | SCM_API SCM scm_words (scm_t_bits car, scm_t_uint16 n_words); |
92 | /* no immutable words for now, would require initialization at the same time as | |
93 | allocation */ | |
c8a1bdc4 | 94 | |
9598a406 MV |
95 | SCM_API SCM scm_array_handle_ref (scm_t_array_handle *h, ssize_t pos); |
96 | SCM_API void scm_array_handle_set (scm_t_array_handle *h, ssize_t pos, SCM val); | |
16ea9620 | 97 | |
3f520967 | 98 | SCM_API int scm_is_pair (SCM x); |
6bf623ca | 99 | SCM_API int scm_is_string (SCM x); |
60e7529a | 100 | |
889975e5 | 101 | SCM_API int scm_get_byte_or_eof (SCM port); |
452c5ad9 | 102 | SCM_API int scm_peek_byte_or_eof (SCM port); |
f5c2af4b LC |
103 | SCM_API void scm_putc (char c, SCM port); |
104 | SCM_API void scm_puts (const char *str_data, SCM port); | |
105 | ||
3f520967 LC |
106 | #endif |
107 | ||
60e7529a | 108 | |
9dca8935 | 109 | #if defined SCM_C_EXTERN_INLINE || defined SCM_INLINE_C_INCLUDING_INLINE_H |
60e7529a RB |
110 | /* either inlining, or being included from inline.c. We use (and |
111 | repeat) this long #if test here and below so that we don't have to | |
112 | introduce any extraneous symbols into the public namespace. We | |
113 | only need SCM_C_INLINE to be seen publically . */ | |
c8a1bdc4 HWN |
114 | |
115 | extern unsigned scm_newcell2_count; | |
116 | extern unsigned scm_newcell_count; | |
117 | ||
979eade6 | 118 | |
9dca8935 | 119 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H |
979eade6 | 120 | SCM_C_EXTERN_INLINE |
60e7529a | 121 | #endif |
26224b3f | 122 | |
c8a1bdc4 | 123 | SCM |
228a24ef | 124 | scm_cell (scm_t_bits car, scm_t_bits cdr) |
16ea9620 | 125 | { |
378f2625 | 126 | SCM cell = SCM_PACK ((scm_t_bits) (GC_MALLOC (sizeof (scm_t_cell)))); |
c8a1bdc4 | 127 | |
c812243b LC |
128 | /* Initialize the type slot last so that the cell is ignored by the GC |
129 | until it is completely initialized. This is only relevant when the GC | |
130 | can actually run during this code, which it can't since the GC only runs | |
131 | when all other threads are stopped. */ | |
26224b3f | 132 | SCM_GC_SET_CELL_WORD (cell, 1, cdr); |
c812243b | 133 | SCM_GC_SET_CELL_WORD (cell, 0, car); |
16ea9620 | 134 | |
26224b3f | 135 | return cell; |
16ea9620 MV |
136 | } |
137 | ||
53ea4fdf LC |
138 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H |
139 | SCM_C_EXTERN_INLINE | |
140 | #endif | |
141 | SCM | |
142 | scm_immutable_cell (scm_t_bits car, scm_t_bits cdr) | |
143 | { | |
144 | SCM cell = SCM_PACK ((scm_t_bits) (GC_MALLOC_STUBBORN (sizeof (scm_t_cell)))); | |
145 | ||
146 | /* Initialize the type slot last so that the cell is ignored by the GC | |
147 | until it is completely initialized. This is only relevant when the GC | |
148 | can actually run during this code, which it can't since the GC only runs | |
149 | when all other threads are stopped. */ | |
150 | SCM_GC_SET_CELL_WORD (cell, 1, cdr); | |
151 | SCM_GC_SET_CELL_WORD (cell, 0, car); | |
152 | ||
153 | GC_END_STUBBORN_CHANGE ((void *) cell); | |
154 | ||
155 | return cell; | |
156 | } | |
157 | ||
9dca8935 | 158 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H |
979eade6 | 159 | SCM_C_EXTERN_INLINE |
60e7529a | 160 | #endif |
c8a1bdc4 | 161 | SCM |
228a24ef DH |
162 | scm_double_cell (scm_t_bits car, scm_t_bits cbr, |
163 | scm_t_bits ccr, scm_t_bits cdr) | |
16ea9620 | 164 | { |
5e1e20c8 | 165 | SCM z; |
c8a1bdc4 | 166 | |
e7bca227 | 167 | z = SCM_PACK ((scm_t_bits) (GC_MALLOC (2 * sizeof (scm_t_cell)))); |
16ea9620 MV |
168 | /* Initialize the type slot last so that the cell is ignored by the |
169 | GC until it is completely initialized. This is only relevant | |
b2a339f6 MV |
170 | when the GC can actually run during this code, which it can't |
171 | since the GC only runs when all other threads are stopped. | |
16ea9620 | 172 | */ |
1fc8902f DH |
173 | SCM_GC_SET_CELL_WORD (z, 1, cbr); |
174 | SCM_GC_SET_CELL_WORD (z, 2, ccr); | |
175 | SCM_GC_SET_CELL_WORD (z, 3, cdr); | |
176 | SCM_GC_SET_CELL_WORD (z, 0, car); | |
16ea9620 | 177 | |
737219dd LC |
178 | /* When this function is inlined, it's possible that the last |
179 | SCM_GC_SET_CELL_WORD above will be adjacent to a following | |
180 | initialization of z. E.g., it occurred in scm_make_real. GCC | |
181 | from around version 3 (e.g., certainly 3.2) began taking | |
182 | advantage of strict C aliasing rules which say that it's OK to | |
183 | interchange the initialization above and the one below when the | |
184 | pointer types appear to differ sufficiently. We don't want that, | |
185 | of course. GCC allows this behaviour to be disabled with the | |
186 | -fno-strict-aliasing option, but would also need to be supplied | |
187 | by Guile users. Instead, the following statements prevent the | |
188 | reordering. | |
189 | */ | |
190 | #ifdef __GNUC__ | |
191 | __asm__ volatile ("" : : : "memory"); | |
192 | #else | |
193 | /* portable version, just in case any other compiler does the same | |
194 | thing. */ | |
195 | scm_remember_upto_here_1 (z); | |
196 | #endif | |
197 | ||
198 | return z; | |
199 | } | |
200 | ||
201 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H | |
202 | SCM_C_EXTERN_INLINE | |
203 | #endif | |
204 | SCM | |
205 | scm_immutable_double_cell (scm_t_bits car, scm_t_bits cbr, | |
206 | scm_t_bits ccr, scm_t_bits cdr) | |
207 | { | |
208 | SCM z; | |
209 | ||
210 | z = SCM_PACK ((scm_t_bits) (GC_MALLOC_STUBBORN (2 * sizeof (scm_t_cell)))); | |
211 | /* Initialize the type slot last so that the cell is ignored by the | |
212 | GC until it is completely initialized. This is only relevant | |
213 | when the GC can actually run during this code, which it can't | |
214 | since the GC only runs when all other threads are stopped. | |
215 | */ | |
216 | SCM_GC_SET_CELL_WORD (z, 1, cbr); | |
217 | SCM_GC_SET_CELL_WORD (z, 2, ccr); | |
218 | SCM_GC_SET_CELL_WORD (z, 3, cdr); | |
219 | SCM_GC_SET_CELL_WORD (z, 0, car); | |
220 | ||
221 | GC_END_STUBBORN_CHANGE ((void *) z); | |
222 | ||
3553e1d1 GH |
223 | /* When this function is inlined, it's possible that the last |
224 | SCM_GC_SET_CELL_WORD above will be adjacent to a following | |
225 | initialization of z. E.g., it occurred in scm_make_real. GCC | |
226 | from around version 3 (e.g., certainly 3.2) began taking | |
227 | advantage of strict C aliasing rules which say that it's OK to | |
228 | interchange the initialization above and the one below when the | |
229 | pointer types appear to differ sufficiently. We don't want that, | |
230 | of course. GCC allows this behaviour to be disabled with the | |
231 | -fno-strict-aliasing option, but would also need to be supplied | |
232 | by Guile users. Instead, the following statements prevent the | |
233 | reordering. | |
234 | */ | |
235 | #ifdef __GNUC__ | |
e809758a AW |
236 | __asm__ volatile ("" : : : "memory"); |
237 | #else | |
238 | /* portable version, just in case any other compiler does the same | |
239 | thing. */ | |
240 | scm_remember_upto_here_1 (z); | |
241 | #endif | |
242 | ||
243 | return z; | |
244 | } | |
245 | ||
246 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H | |
247 | SCM_C_EXTERN_INLINE | |
248 | #endif | |
249 | SCM | |
250 | scm_words (scm_t_bits car, scm_t_uint16 n_words) | |
251 | { | |
252 | SCM z; | |
253 | ||
254 | z = SCM_PACK ((scm_t_bits) (GC_MALLOC (sizeof (scm_t_bits) * n_words))); | |
255 | SCM_GC_SET_CELL_WORD (z, 0, car); | |
256 | ||
257 | /* FIXME: is the following concern even relevant with BDW-GC? */ | |
258 | ||
259 | /* When this function is inlined, it's possible that the last | |
260 | SCM_GC_SET_CELL_WORD above will be adjacent to a following | |
261 | initialization of z. E.g., it occurred in scm_make_real. GCC | |
262 | from around version 3 (e.g., certainly 3.2) began taking | |
263 | advantage of strict C aliasing rules which say that it's OK to | |
264 | interchange the initialization above and the one below when the | |
265 | pointer types appear to differ sufficiently. We don't want that, | |
266 | of course. GCC allows this behaviour to be disabled with the | |
267 | -fno-strict-aliasing option, but would also need to be supplied | |
268 | by Guile users. Instead, the following statements prevent the | |
269 | reordering. | |
270 | */ | |
271 | #ifdef __GNUC__ | |
cb975c21 | 272 | __asm__ volatile ("" : : : "memory"); |
3553e1d1 GH |
273 | #else |
274 | /* portable version, just in case any other compiler does the same | |
275 | thing. */ | |
276 | scm_remember_upto_here_1 (z); | |
c8a1bdc4 | 277 | #endif |
6253f3f1 | 278 | |
c8a1bdc4 HWN |
279 | return z; |
280 | } | |
6253f3f1 | 281 | |
9dca8935 | 282 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H |
979eade6 | 283 | SCM_C_EXTERN_INLINE |
9598a406 MV |
284 | #endif |
285 | SCM | |
286 | scm_array_handle_ref (scm_t_array_handle *h, ssize_t p) | |
287 | { | |
6d7c4402 | 288 | if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base)) |
2a610be5 AW |
289 | /* catch overflow */ |
290 | scm_out_of_range (NULL, scm_from_ssize_t (p)); | |
291 | /* perhaps should catch overflow here too */ | |
292 | return h->impl->vref (h, h->base + p); | |
9598a406 | 293 | } |
eab1b259 | 294 | |
9dca8935 | 295 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H |
979eade6 | 296 | SCM_C_EXTERN_INLINE |
9598a406 MV |
297 | #endif |
298 | void | |
299 | scm_array_handle_set (scm_t_array_handle *h, ssize_t p, SCM v) | |
300 | { | |
3245c0fb | 301 | if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base)) |
2a610be5 AW |
302 | /* catch overflow */ |
303 | scm_out_of_range (NULL, scm_from_ssize_t (p)); | |
304 | /* perhaps should catch overflow here too */ | |
305 | h->impl->vset (h, h->base + p, v); | |
9598a406 | 306 | } |
eab1b259 | 307 | |
9dca8935 | 308 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H |
979eade6 | 309 | SCM_C_EXTERN_INLINE |
d5ad4aa6 MV |
310 | #endif |
311 | int | |
312 | scm_is_pair (SCM x) | |
313 | { | |
23f2b9a3 KR |
314 | /* The following "workaround_for_gcc_295" avoids bad code generated by |
315 | i386 gcc 2.95.4 (the Debian packaged 2.95.4-24 at least). | |
316 | ||
317 | Under the default -O2 the inlined SCM_I_CONSP test gets "optimized" so | |
318 | the fetch of the tag word from x is done before confirming it's a | |
319 | non-immediate (SCM_NIMP). Needless to say that bombs badly if x is a | |
320 | immediate. This was seen to afflict scm_srfi1_split_at and something | |
321 | deep in the bowels of ceval(). In both cases segvs resulted from | |
322 | deferencing a random immediate value. srfi-1.test exposes the problem | |
323 | through a short list, the immediate being SCM_EOL in that case. | |
324 | Something in syntax.test exposed the ceval() problem. | |
325 | ||
326 | Just "volatile SCM workaround_for_gcc_295 = lst" is enough to avoid the | |
327 | problem, without even using that variable. The "w=w" is just to | |
328 | prevent a warning about it being unused. | |
329 | */ | |
330 | #if defined (__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ == 95 | |
331 | volatile SCM workaround_for_gcc_295 = x; | |
332 | workaround_for_gcc_295 = workaround_for_gcc_295; | |
333 | #endif | |
334 | ||
d5ad4aa6 MV |
335 | return SCM_I_CONSP (x); |
336 | } | |
337 | ||
183f7849 LC |
338 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H |
339 | SCM_C_EXTERN_INLINE | |
340 | #endif | |
341 | int | |
342 | scm_is_string (SCM x) | |
343 | { | |
344 | return SCM_NIMP (x) && (SCM_TYP7 (x) == scm_tc7_string); | |
345 | } | |
f5c2af4b LC |
346 | |
347 | /* Port I/O. */ | |
348 | ||
349 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H | |
350 | SCM_C_EXTERN_INLINE | |
351 | #endif | |
352 | int | |
889975e5 | 353 | scm_get_byte_or_eof (SCM port) |
f5c2af4b LC |
354 | { |
355 | int c; | |
356 | scm_t_port *pt = SCM_PTAB_ENTRY (port); | |
357 | ||
358 | if (pt->rw_active == SCM_PORT_WRITE) | |
359 | /* may be marginally faster than calling scm_flush. */ | |
360 | scm_ptobs[SCM_PTOBNUM (port)].flush (port); | |
361 | ||
362 | if (pt->rw_random) | |
363 | pt->rw_active = SCM_PORT_READ; | |
364 | ||
365 | if (pt->read_pos >= pt->read_end) | |
366 | { | |
452c5ad9 | 367 | if (SCM_UNLIKELY (scm_fill_input (port) == EOF)) |
f5c2af4b LC |
368 | return EOF; |
369 | } | |
370 | ||
371 | c = *(pt->read_pos++); | |
372 | ||
f5c2af4b LC |
373 | return c; |
374 | } | |
375 | ||
452c5ad9 LC |
376 | /* Like `scm_get_byte_or_eof' but does not change PORT's `read_pos'. */ |
377 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H | |
378 | SCM_C_EXTERN_INLINE | |
379 | #endif | |
380 | int | |
381 | scm_peek_byte_or_eof (SCM port) | |
382 | { | |
383 | int c; | |
384 | scm_t_port *pt = SCM_PTAB_ENTRY (port); | |
385 | ||
386 | if (pt->rw_active == SCM_PORT_WRITE) | |
387 | /* may be marginally faster than calling scm_flush. */ | |
388 | scm_ptobs[SCM_PTOBNUM (port)].flush (port); | |
389 | ||
390 | if (pt->rw_random) | |
391 | pt->rw_active = SCM_PORT_READ; | |
392 | ||
393 | if (pt->read_pos >= pt->read_end) | |
394 | { | |
395 | if (SCM_UNLIKELY (scm_fill_input (port) == EOF)) | |
396 | return EOF; | |
397 | } | |
398 | ||
399 | c = *pt->read_pos; | |
400 | ||
401 | return c; | |
402 | } | |
403 | ||
f5c2af4b LC |
404 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H |
405 | SCM_C_EXTERN_INLINE | |
406 | #endif | |
407 | void | |
408 | scm_putc (char c, SCM port) | |
409 | { | |
410 | SCM_ASSERT_TYPE (SCM_OPOUTPORTP (port), port, 0, NULL, "output port"); | |
411 | scm_lfwrite (&c, 1, port); | |
412 | } | |
413 | ||
414 | #ifndef SCM_INLINE_C_INCLUDING_INLINE_H | |
415 | SCM_C_EXTERN_INLINE | |
416 | #endif | |
417 | void | |
418 | scm_puts (const char *s, SCM port) | |
419 | { | |
420 | SCM_ASSERT_TYPE (SCM_OPOUTPORTP (port), port, 0, NULL, "output port"); | |
421 | scm_lfwrite (s, strlen (s), port); | |
422 | } | |
423 | ||
424 | ||
16ea9620 | 425 | #endif |
16ea9620 | 426 | #endif |