Fix MacOS X build: don't believe __GNUC_GNU_INLINE__ on MacOS X
[ghc.git] / includes / Stg.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2004
4 *
5 * Top-level include file for everything STG-ish.
6 *
7 * This file is included *automatically* by all .hc files.
8 *
9 * NOTE: always include Stg.h *before* any other headers, because we
10 * define some register variables which must be done before any inline
11 * functions are defined (some system headers have been known to
12 * define the odd inline function).
13 *
14 * We generally try to keep as little visible as possible when
15 * compiling .hc files. So for example the definitions of the
16 * InfoTable structs, closure structs and other RTS types are not
17 * visible here. The compiler knows enough about the representations
18 * of these types to generate code which manipulates them directly
19 * with pointer arithmetic.
20 *
21 * ---------------------------------------------------------------------------*/
22
23 #ifndef STG_H
24 #define STG_H
25
26
27 /* If we include "Stg.h" directly, we're in STG code, and we therefore
28 * get all the global register variables, macros etc. that go along
29 * with that. If "Stg.h" is included via "Rts.h", we're assumed to
30 * be in vanilla C.
31 */
32 #ifndef IN_STG_CODE
33 # define IN_STG_CODE 1
34
35 // Turn on C99 for .hc code. This gives us the INFINITY and NAN
36 // constants from math.h, which we occasionally need to use in .hc (#1861)
37 # define _ISOC99_SOURCE
38
39 // Turning on _ISOC99_SOURCE means S_ISSOCK gets defined on Linux
40 # define _BSD_SOURCE
41 #endif
42
43 #if IN_STG_CODE == 0
44 # define NO_GLOBAL_REG_DECLS /* don't define fixed registers */
45 #endif
46
47 /* Configuration */
48 #include "ghcconfig.h"
49 #include "RtsConfig.h"
50
51 /* The code generator calls the math functions directly in .hc code.
52 NB. after configuration stuff above, because this sets #defines
53 that depend on config info, such as __USE_FILE_OFFSET64 */
54 #include <math.h>
55
56 /* -----------------------------------------------------------------------------
57 Useful definitions
58 -------------------------------------------------------------------------- */
59
60 /*
61 * The C backend like to refer to labels by just mentioning their
62 * names. Howevver, when a symbol is declared as a variable in C, the
63 * C compiler will implicitly dereference it when it occurs in source.
64 * So we must subvert this behaviour for .hc files by declaring
65 * variables as arrays, which eliminates the implicit dereference.
66 */
67 #if IN_STG_CODE
68 #define RTS_VAR(x) (x)[]
69 #define RTS_DEREF(x) (*(x))
70 #else
71 #define RTS_VAR(x) x
72 #define RTS_DEREF(x) x
73 #endif
74
75 /* bit macros
76 */
77 #define BITS_PER_BYTE 8
78 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
79
80 /*
81 * 'Portable' inlining:
82 * INLINE_HEADER is for inline functions in header files (macros)
83 * STATIC_INLINE is for inline functions in source files
84 * EXTERN_INLINE is for functions that we want to inline sometimes
85 * (we also compile a static version of the function; see Inlines.c)
86 */
87 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
88
89 # define INLINE_HEADER static inline
90 # define INLINE_ME inline
91 # define STATIC_INLINE INLINE_HEADER
92
93 // The special "extern inline" behaviour is now only supported by gcc
94 // when _GNUC_GNU_INLINE__ is defined, and you have to use
95 // __attribute__((gnu_inline)). So when we don't have this, we use
96 // ordinary static inline.
97 //
98 // Apple's gcc defines __GNUC_GNU_INLINE__ without providing
99 // gnu_inline, so we exclude MacOS X and fall through to the safe
100 // version.
101 //
102 #if defined(__GNUC_GNU_INLINE__) && !defined(__APPLE__)
103 # if defined(KEEP_INLINES)
104 # define EXTERN_INLINE inline
105 # else
106 # define EXTERN_INLINE extern inline __attribute__((gnu_inline))
107 # endif
108 #else
109 # if defined(KEEP_INLINES)
110 # define EXTERN_INLINE
111 # else
112 # define EXTERN_INLINE INLINE_HEADER
113 # endif
114 #endif
115
116 #elif defined(_MSC_VER)
117
118 # define INLINE_HEADER __inline static
119 # define INLINE_ME __inline
120 # define STATIC_INLINE INLINE_HEADER
121
122 # if defined(KEEP_INLINES)
123 # define EXTERN_INLINE __inline
124 # else
125 # define EXTERN_INLINE __inline extern
126 # endif
127
128 #else
129
130 # error "Don't know how to inline functions with your C compiler."
131
132 #endif
133
134
135 /*
136 * GCC attributes
137 */
138 #if defined(__GNUC__)
139 #define GNU_ATTRIBUTE(at) __attribute__((at))
140 #else
141 #define GNU_ATTRIBUTE(at)
142 #endif
143
144 #if __GNUC__ >= 3
145 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
146 #else
147 #define GNUC3_ATTRIBUTE(at)
148 #endif
149
150 #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3
151 #define GNUC_ATTR_HOT __attribute__((hot))
152 #else
153 #define GNUC_ATTR_HOT /* nothing */
154 #endif
155
156 #define STG_UNUSED GNUC3_ATTRIBUTE(__unused__)
157
158 /* -----------------------------------------------------------------------------
159 Global type definitions
160 -------------------------------------------------------------------------- */
161
162 #include "MachDeps.h"
163 #include "StgTypes.h"
164
165 /* -----------------------------------------------------------------------------
166 Shorthand forms
167 -------------------------------------------------------------------------- */
168
169 typedef StgChar C_;
170 typedef StgWord W_;
171 typedef StgWord* P_;
172 typedef P_* PP_;
173 typedef StgInt I_;
174 typedef StgAddr A_;
175 typedef const StgWord* D_;
176 typedef StgFunPtr F_;
177 typedef StgByteArray B_;
178 typedef StgClosurePtr L_;
179
180 typedef StgInt64 LI_;
181 typedef StgWord64 LW_;
182
183 #define IF_(f) static F_ GNUC3_ATTRIBUTE(used) f(void)
184 #define FN_(f) F_ f(void)
185 #define EF_(f) extern F_ f(void)
186
187 typedef StgWord StgWordArray[];
188 #define EI_(X) extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
189 #define II_(X) static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
190
191 /* -----------------------------------------------------------------------------
192 Tail calls
193
194 This needs to be up near the top as the register line on alpha needs
195 to be before all procedures (inline & out-of-line).
196 -------------------------------------------------------------------------- */
197
198 #include "TailCalls.h"
199
200 /* -----------------------------------------------------------------------------
201 Other Stg stuff...
202 -------------------------------------------------------------------------- */
203
204 #include "StgDLL.h"
205 #include "MachRegs.h"
206 #include "Regs.h"
207
208 #ifdef TICKY_TICKY
209 #include "TickyCounters.h"
210 #endif
211
212 #if IN_STG_CODE
213 /*
214 * This is included later for RTS sources, after definitions of
215 * StgInfoTable, StgClosure and so on.
216 */
217 #include "StgMiscClosures.h"
218 #endif
219
220 #include "SMP.h" // write_barrier() inline is required
221
222 /* -----------------------------------------------------------------------------
223 Moving Floats and Doubles
224
225 ASSIGN_FLT is for assigning a float to memory (usually the
226 stack/heap). The memory address is guaranteed to be
227 StgWord aligned (currently == sizeof(void *)).
228
229 PK_FLT is for pulling a float out of memory. The memory is
230 guaranteed to be StgWord aligned.
231 -------------------------------------------------------------------------- */
232
233 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
234 INLINE_HEADER StgFloat PK_FLT (W_ []);
235
236 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
237
238 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
239 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
240
241 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
242
243 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
244 {
245 float_thing y;
246 y.f = src;
247 *p_dest = y.fu;
248 }
249
250 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
251 {
252 float_thing y;
253 y.fu = *p_src;
254 return(y.f);
255 }
256
257 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
258
259 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
260
261 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
262 INLINE_HEADER StgDouble PK_DBL (W_ []);
263
264 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
265 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
266
267 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
268
269 /* Sparc uses two floating point registers to hold a double. We can
270 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
271 * independently - unfortunately this code isn't writable in C, we
272 * have to use inline assembler.
273 */
274 #if sparc_HOST_ARCH
275
276 #define ASSIGN_DBL(dst0,src) \
277 { StgPtr dst = (StgPtr)(dst0); \
278 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
279 "=m" (((P_)(dst))[1]) : "f" (src)); \
280 }
281
282 #define PK_DBL(src0) \
283 ( { StgPtr src = (StgPtr)(src0); \
284 register double d; \
285 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
286 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
287 } )
288
289 #else /* ! sparc_HOST_ARCH */
290
291 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
292 INLINE_HEADER StgDouble PK_DBL (W_ []);
293
294 typedef struct
295 { StgWord dhi;
296 StgWord dlo;
297 } unpacked_double;
298
299 typedef union
300 { StgDouble d;
301 unpacked_double du;
302 } double_thing;
303
304 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
305 {
306 double_thing y;
307 y.d = src;
308 p_dest[0] = y.du.dhi;
309 p_dest[1] = y.du.dlo;
310 }
311
312 /* GCC also works with this version, but it generates
313 the same code as the previous one, and is not ANSI
314
315 #define ASSIGN_DBL( p_dest, src ) \
316 *p_dest = ((double_thing) src).du.dhi; \
317 *(p_dest+1) = ((double_thing) src).du.dlo \
318 */
319
320 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
321 {
322 double_thing y;
323 y.du.dhi = p_src[0];
324 y.du.dlo = p_src[1];
325 return(y.d);
326 }
327
328 #endif /* ! sparc_HOST_ARCH */
329
330 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
331
332
333 /* -----------------------------------------------------------------------------
334 Moving 64-bit quantities around
335
336 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
337 PK_Word64 load an StgWord64/StgInt64 from a amemory location
338
339 In both cases the memory location might not be 64-bit aligned.
340 -------------------------------------------------------------------------- */
341
342 #ifdef SUPPORT_LONG_LONGS
343
344 typedef struct
345 { StgWord dhi;
346 StgWord dlo;
347 } unpacked_double_word;
348
349 typedef union
350 { StgInt64 i;
351 unpacked_double_word iu;
352 } int64_thing;
353
354 typedef union
355 { StgWord64 w;
356 unpacked_double_word wu;
357 } word64_thing;
358
359 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
360 {
361 word64_thing y;
362 y.w = src;
363 p_dest[0] = y.wu.dhi;
364 p_dest[1] = y.wu.dlo;
365 }
366
367 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
368 {
369 word64_thing y;
370 y.wu.dhi = p_src[0];
371 y.wu.dlo = p_src[1];
372 return(y.w);
373 }
374
375 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
376 {
377 int64_thing y;
378 y.i = src;
379 p_dest[0] = y.iu.dhi;
380 p_dest[1] = y.iu.dlo;
381 }
382
383 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
384 {
385 int64_thing y;
386 y.iu.dhi = p_src[0];
387 y.iu.dlo = p_src[1];
388 return(y.i);
389 }
390
391 #elif SIZEOF_VOID_P == 8
392
393 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
394 {
395 p_dest[0] = src;
396 }
397
398 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
399 {
400 return p_src[0];
401 }
402
403 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
404 {
405 p_dest[0] = src;
406 }
407
408 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
409 {
410 return p_src[0];
411 }
412
413 #endif
414
415 /* -----------------------------------------------------------------------------
416 Split markers
417 -------------------------------------------------------------------------- */
418
419 #if defined(USE_SPLIT_MARKERS)
420 #if defined(LEADING_UNDERSCORE)
421 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
422 #else
423 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
424 #endif
425 #else
426 #define __STG_SPLIT_MARKER /* nothing */
427 #endif
428
429 /* -----------------------------------------------------------------------------
430 Write-combining store
431 -------------------------------------------------------------------------- */
432
433 INLINE_HEADER void
434 wcStore (StgPtr p, StgWord w)
435 {
436 #ifdef x86_64_HOST_ARCH
437 __asm__(
438 "movnti\t%1, %0"
439 : "=m" (*p)
440 : "r" (w)
441 );
442 #else
443 *p = w;
444 #endif
445 }
446
447 /* -----------------------------------------------------------------------------
448 Integer multiply with overflow
449 -------------------------------------------------------------------------- */
450
451 /* Multiply with overflow checking.
452 *
453 * This is tricky - the usual sign rules for add/subtract don't apply.
454 *
455 * On 32-bit machines we use gcc's 'long long' types, finding
456 * overflow with some careful bit-twiddling.
457 *
458 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
459 * we use a crude approximation, testing whether either operand is
460 * larger than 32-bits; if neither is, then we go ahead with the
461 * multiplication.
462 *
463 * Return non-zero if there is any possibility that the signed multiply
464 * of a and b might overflow. Return zero only if you are absolutely sure
465 * that it won't overflow. If in doubt, return non-zero.
466 */
467
468 #if SIZEOF_VOID_P == 4
469
470 #ifdef WORDS_BIGENDIAN
471 #define RTS_CARRY_IDX__ 0
472 #define RTS_REM_IDX__ 1
473 #else
474 #define RTS_CARRY_IDX__ 1
475 #define RTS_REM_IDX__ 0
476 #endif
477
478 typedef union {
479 StgInt64 l;
480 StgInt32 i[2];
481 } long_long_u ;
482
483 #define mulIntMayOflo(a,b) \
484 ({ \
485 StgInt32 r, c; \
486 long_long_u z; \
487 z.l = (StgInt64)a * (StgInt64)b; \
488 r = z.i[RTS_REM_IDX__]; \
489 c = z.i[RTS_CARRY_IDX__]; \
490 if (c == 0 || c == -1) { \
491 c = ((StgWord)((a^b) ^ r)) \
492 >> (BITS_IN (I_) - 1); \
493 } \
494 c; \
495 })
496
497 /* Careful: the carry calculation above is extremely delicate. Make sure
498 * you test it thoroughly after changing it.
499 */
500
501 #else
502
503 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
504
505 /* If we have n-bit words then we have n-1 bits after accounting for the
506 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
507 #define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
508 #define HALF_NEG_INT (-HALF_POS_INT)
509
510 #define mulIntMayOflo(a,b) \
511 ({ \
512 I_ c; \
513 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
514 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\
515 c = 1; \
516 } else { \
517 c = 0; \
518 } \
519 c; \
520 })
521 #endif
522
523 #endif /* STG_H */