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