Fix inline machinery in C99 mode on MacOS X.
[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 "libguile/__scm.h"
29
30 #if (SCM_DEBUG_CELL_ACCESSES == 1)
31 #include <stdio.h>
32 #endif
33
34 #include "libguile/pairs.h"
35 #include "libguile/gc.h"
36 #include "libguile/threads.h"
37 #include "libguile/unif.h"
38 #include "libguile/pairs.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__) && (!(__APPLE_CC__ > 5400 && __STDC_VERSION__ >= 199901L))
58 #  if (defined __GNUC_STDC_INLINE__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 2)
59 #   define SCM_C_EXTERN_INLINE                                  \
60            extern __inline__ __attribute__ ((__gnu_inline__))
61 #  else
62 #   define SCM_C_EXTERN_INLINE extern __inline__
63 #  endif
64 # elif (defined SCM_C_INLINE)
65 #  define SCM_C_EXTERN_INLINE static SCM_C_INLINE
66 # endif
67
68 #endif /* SCM_INLINE_C_INCLUDING_INLINE_H */
69
70
71 #if ((!defined SCM_C_INLINE) && (!defined SCM_INLINE_C_INCLUDING_INLINE_H)) \
72      || (defined __GNUC__)
73
74 /* The `extern' declarations.  They should only appear when used from
75    "inline.c", when `inline' is not supported at all or when GCC's "extern
76    inline" is used.  */
77
78 SCM_API SCM scm_cell (scm_t_bits car, scm_t_bits cdr);
79 SCM_API SCM scm_double_cell (scm_t_bits car, scm_t_bits cbr,
80                              scm_t_bits ccr, scm_t_bits cdr);
81
82 SCM_API SCM scm_array_handle_ref (scm_t_array_handle *h, ssize_t pos);
83 SCM_API void scm_array_handle_set (scm_t_array_handle *h, ssize_t pos, SCM val);
84
85 SCM_API int scm_is_pair (SCM x);
86
87 #endif
88
89
90 #if defined SCM_C_INLINE || defined SCM_INLINE_C_INCLUDING_INLINE_H
91 /* either inlining, or being included from inline.c.  We use (and
92    repeat) this long #if test here and below so that we don't have to
93    introduce any extraneous symbols into the public namespace.  We
94    only need SCM_C_INLINE to be seen publically . */
95
96 extern unsigned scm_newcell2_count;
97 extern unsigned scm_newcell_count;
98
99
100 #if defined SCM_C_EXTERN_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
101 SCM_C_EXTERN_INLINE
102 #endif
103 SCM
104 scm_cell (scm_t_bits car, scm_t_bits cdr)
105 {
106   SCM z;
107   SCM *freelist = SCM_FREELIST_LOC (scm_i_freelist);
108
109   if (scm_is_null (*freelist))
110     z = scm_gc_for_newcell (&scm_i_master_freelist, freelist);
111   else
112     {
113       z = *freelist;
114       *freelist = SCM_FREE_CELL_CDR (*freelist);
115     }
116
117   /*
118     We update scm_cells_allocated from this function. If we don't
119     update this explicitly, we will have to walk a freelist somewhere
120     later on, which seems a lot more expensive.
121    */
122   scm_cells_allocated += 1;  
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     /*
140       Always set mark. Otherwise cells that are alloced before
141       scm_debug_cell_accesses_p is toggled seem invalid.
142     */
143     SCM_SET_GC_MARK (z);
144
145     /*
146       TODO: figure out if this use of mark bits is valid with
147       threading. What if another thread is doing GC at this point
148       ... ?
149      */
150       
151 #endif
152
153   
154   /* Initialize the type slot last so that the cell is ignored by the
155      GC until it is completely initialized.  This is only relevant
156      when the GC can actually run during this code, which it can't
157      since the GC only runs when all other threads are stopped.
158   */
159   SCM_GC_SET_CELL_WORD (z, 1, cdr);
160   SCM_GC_SET_CELL_WORD (z, 0, car);
161
162 #if (SCM_DEBUG_CELL_ACCESSES == 1)
163   if (scm_expensive_debug_cell_accesses_p )
164     scm_i_expensive_validation_check (z);
165 #endif
166   
167   return z;
168 }
169
170 #if defined SCM_C_EXTERN_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
171 SCM_C_EXTERN_INLINE
172 #endif
173 SCM
174 scm_double_cell (scm_t_bits car, scm_t_bits cbr,
175                  scm_t_bits ccr, scm_t_bits cdr)
176 {
177   SCM z;
178   SCM *freelist = SCM_FREELIST_LOC (scm_i_freelist2);
179
180   if (scm_is_null (*freelist))
181     z = scm_gc_for_newcell (&scm_i_master_freelist2, freelist);
182   else
183     {
184       z = *freelist;
185       *freelist = SCM_FREE_CELL_CDR (*freelist);
186     }
187
188   scm_cells_allocated += 2;
189
190   /* Initialize the type slot last so that the cell is ignored by the
191      GC until it is completely initialized.  This is only relevant
192      when the GC can actually run during this code, which it can't
193      since the GC only runs when all other threads are stopped.
194   */
195   SCM_GC_SET_CELL_WORD (z, 1, cbr);
196   SCM_GC_SET_CELL_WORD (z, 2, ccr);
197   SCM_GC_SET_CELL_WORD (z, 3, cdr);
198   SCM_GC_SET_CELL_WORD (z, 0, car);
199
200 #if (SCM_DEBUG_CELL_ACCESSES == 1)
201   if (scm_debug_cell_accesses_p)
202     {
203       if (SCM_GC_MARK_P (z))
204         {
205           fprintf(stderr,
206                   "scm_double_cell tried to allocate a marked cell.\n");
207           abort();
208         }
209     }
210
211   /* see above. */
212   SCM_SET_GC_MARK (z);
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 #if defined SCM_C_EXTERN_INLINE && ! defined 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   return h->ref (h, p);
246 }
247
248 #if defined SCM_C_EXTERN_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
249 SCM_C_EXTERN_INLINE
250 #endif
251 void
252 scm_array_handle_set (scm_t_array_handle *h, ssize_t p, SCM v)
253 {
254   h->set (h, p, v);
255 }
256
257 #if defined SCM_C_EXTERN_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
258 SCM_C_EXTERN_INLINE
259 #endif
260 int
261 scm_is_pair (SCM x)
262 {
263   /* The following "workaround_for_gcc_295" avoids bad code generated by
264      i386 gcc 2.95.4 (the Debian packaged 2.95.4-24 at least).
265
266      Under the default -O2 the inlined SCM_I_CONSP test gets "optimized" so
267      the fetch of the tag word from x is done before confirming it's a
268      non-immediate (SCM_NIMP).  Needless to say that bombs badly if x is a
269      immediate.  This was seen to afflict scm_srfi1_split_at and something
270      deep in the bowels of ceval().  In both cases segvs resulted from
271      deferencing a random immediate value.  srfi-1.test exposes the problem
272      through a short list, the immediate being SCM_EOL in that case.
273      Something in syntax.test exposed the ceval() problem.
274
275      Just "volatile SCM workaround_for_gcc_295 = lst" is enough to avoid the
276      problem, without even using that variable.  The "w=w" is just to
277      prevent a warning about it being unused.
278      */
279 #if defined (__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ == 95
280   volatile SCM workaround_for_gcc_295 = x;
281   workaround_for_gcc_295 = workaround_for_gcc_295;
282 #endif
283
284   return SCM_I_CONSP (x);
285 }
286
287 #endif
288 #endif