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