includes/Stg.h: define _DEFAULT_SOURCE for glibc-2.20
[ghc.git] / includes / Stg.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2009
4 *
5 * Top-level include file for everything required when compiling .hc
6 * code. NOTE: in .hc files, Stg.h must be included *before* any
7 * other headers, because we define some register variables which must
8 * be done before any inline functions are defined (some system
9 * headers have been known to define the odd inline function).
10 *
11 * We generally try to keep as little visible as possible when
12 * compiling .hc files. So for example the definitions of the
13 * InfoTable structs, closure structs and other RTS types are not
14 * visible here. The compiler knows enough about the representations
15 * of these types to generate code which manipulates them directly
16 * with pointer arithmetic.
17 *
18 * In ordinary C code, do not #include this file directly: #include
19 * "Rts.h" instead.
20 *
21 * To understand the structure of the RTS headers, see the wiki:
22 * http://ghc.haskell.org/trac/ghc/wiki/Commentary/SourceTree/Includes
23 *
24 * ---------------------------------------------------------------------------*/
25
26 #ifndef STG_H
27 #define STG_H
28
29 /*
30 * If we are compiling a .hc file, then we want all the register
31 * variables. This is the what happens if you #include "Stg.h" first:
32 * we assume this is a .hc file, and set IN_STG_CODE==1, which later
33 * causes the register variables to be enabled in stg/Regs.h.
34 *
35 * If instead "Rts.h" is included first, then we are compiling a
36 * vanilla C file. Everything from Stg.h is provided, except that
37 * IN_STG_CODE is not defined, and the register variables will not be
38 * active.
39 */
40 #ifndef IN_STG_CODE
41 # define IN_STG_CODE 1
42
43 // Turn on C99 for .hc code. This gives us the INFINITY and NAN
44 // constants from math.h, which we occasionally need to use in .hc (#1861)
45 # define _ISOC99_SOURCE
46
47 // We need _BSD_SOURCE so that math.h defines things like gamma
48 // on Linux
49 # define _BSD_SOURCE
50
51 // '_BSD_SOURCE' is deprecated since glibc-2.20
52 // in favour of '_DEFAULT_SOURCE'
53 # define _DEFAULT_SOURCE
54 #endif
55
56 #if IN_STG_CODE == 0 || defined(llvm_CC_FLAVOR)
57 // C compilers that use an LLVM back end (clang or llvm-gcc) do not
58 // correctly support global register variables so we make sure that
59 // we do not declare them for these compilers.
60 # define NO_GLOBAL_REG_DECLS /* don't define fixed registers */
61 #endif
62
63 /* Configuration */
64 #include "ghcconfig.h"
65
66 /* The code generator calls the math functions directly in .hc code.
67 NB. after configuration stuff above, because this sets #defines
68 that depend on config info, such as __USE_FILE_OFFSET64 */
69 #include <math.h>
70
71 // On Solaris, we don't get the INFINITY and NAN constants unless we
72 // #define _STDC_C99, and we can't do that unless we also use -std=c99,
73 // because _STDC_C99 causes the headers to use C99 syntax (e.g. restrict).
74 // We aren't ready for -std=c99 yet, so define INFINITY/NAN by hand using
75 // the gcc builtins.
76 #if !defined(INFINITY)
77 #if defined(__GNUC__)
78 #define INFINITY __builtin_inf()
79 #else
80 #error No definition for INFINITY
81 #endif
82 #endif
83
84 #if !defined(NAN)
85 #if defined(__GNUC__)
86 #define NAN __builtin_nan("")
87 #else
88 #error No definition for NAN
89 #endif
90 #endif
91
92 /* -----------------------------------------------------------------------------
93 Useful definitions
94 -------------------------------------------------------------------------- */
95
96 /*
97 * The C backend likes to refer to labels by just mentioning their
98 * names. However, when a symbol is declared as a variable in C, the
99 * C compiler will implicitly dereference it when it occurs in source.
100 * So we must subvert this behaviour for .hc files by declaring
101 * variables as arrays, which eliminates the implicit dereference.
102 */
103 #if IN_STG_CODE
104 #define RTS_VAR(x) (x)[]
105 #define RTS_DEREF(x) (*(x))
106 #else
107 #define RTS_VAR(x) x
108 #define RTS_DEREF(x) x
109 #endif
110
111 /* bit macros
112 */
113 #define BITS_PER_BYTE 8
114 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
115
116 /* Compute offsets of struct fields
117 */
118 #define STG_FIELD_OFFSET(s_type, field) ((StgWord)&(((s_type*)0)->field))
119
120 /*
121 * 'Portable' inlining:
122 * INLINE_HEADER is for inline functions in header files (macros)
123 * STATIC_INLINE is for inline functions in source files
124 * EXTERN_INLINE is for functions that we want to inline sometimes
125 * (we also compile a static version of the function; see Inlines.c)
126 */
127 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
128
129 # define INLINE_HEADER static inline
130 # define INLINE_ME inline
131 # define STATIC_INLINE INLINE_HEADER
132
133 // The special "extern inline" behaviour is now only supported by gcc
134 // when _GNUC_GNU_INLINE__ is defined, and you have to use
135 // __attribute__((gnu_inline)). So when we don't have this, we use
136 // ordinary static inline.
137 //
138 // Apple's gcc defines __GNUC_GNU_INLINE__ without providing
139 // gnu_inline, so we exclude MacOS X and fall through to the safe
140 // version.
141 //
142 #if defined(__GNUC_GNU_INLINE__) && !defined(__APPLE__)
143 # if defined(KEEP_INLINES)
144 # define EXTERN_INLINE inline
145 # else
146 # define EXTERN_INLINE extern inline __attribute__((gnu_inline))
147 # endif
148 #else
149 # if defined(KEEP_INLINES)
150 # define EXTERN_INLINE
151 # else
152 # define EXTERN_INLINE INLINE_HEADER
153 # endif
154 #endif
155
156 #elif defined(_MSC_VER)
157
158 # define INLINE_HEADER __inline static
159 # define INLINE_ME __inline
160 # define STATIC_INLINE INLINE_HEADER
161
162 # if defined(KEEP_INLINES)
163 # define EXTERN_INLINE __inline
164 # else
165 # define EXTERN_INLINE __inline extern
166 # endif
167
168 #else
169
170 # error "Don't know how to inline functions with your C compiler."
171
172 #endif
173
174
175 /*
176 * GCC attributes
177 */
178 #if defined(__GNUC__)
179 #define GNU_ATTRIBUTE(at) __attribute__((at))
180 #else
181 #define GNU_ATTRIBUTE(at)
182 #endif
183
184 #if __GNUC__ >= 3
185 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
186 #else
187 #define GNUC3_ATTRIBUTE(at)
188 #endif
189
190 #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3
191 #define GNUC_ATTR_HOT __attribute__((hot))
192 #else
193 #define GNUC_ATTR_HOT /* nothing */
194 #endif
195
196 #define STG_UNUSED GNUC3_ATTRIBUTE(__unused__)
197
198 /* -----------------------------------------------------------------------------
199 Global type definitions
200 -------------------------------------------------------------------------- */
201
202 #include "MachDeps.h"
203 #include "stg/Types.h"
204
205 /* -----------------------------------------------------------------------------
206 Shorthand forms
207 -------------------------------------------------------------------------- */
208
209 typedef StgChar C_;
210 typedef StgWord W_;
211 typedef StgWord* P_;
212 typedef StgInt I_;
213 typedef StgWord StgWordArray[];
214 typedef StgFunPtr F_;
215
216 #define EI_(X) extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
217 #define II_(X) static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
218 #define IF_(f) static StgFunPtr GNUC3_ATTRIBUTE(used) f(void)
219 #define FN_(f) StgFunPtr f(void)
220 #define EF_(f) extern StgFunPtr f() /* See Note [External function prototypes] */
221
222 /* Note [External function prototypes] See Trac #8965
223 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
224 The external-function macro EF_(F) used to be defined as
225 extern StgFunPtr f(void)
226 i.e a function of zero arguments. On most platforms this doesn't
227 matter very much: calls to these functions put the parameters in the
228 usual places anyway, and (with the exception of varargs) things just
229 work.
230
231 However, the ELFv2 ABI on ppc64 optimises stack allocation
232 (http://gcc.gnu.org/ml/gcc-patches/2013-11/msg01149.html): a call to a
233 function that has a prototype, is not varargs, and receives all parameters
234 in registers rather than on the stack does not require the caller to
235 allocate an argument save area. The incorrect prototypes cause GCC to
236 believe that all functions declared this way can be called without an
237 argument save area, but if the callee has sufficiently many arguments then
238 it will expect that area to be present, and will thus corrupt the caller's
239 stack. This happens in particular with calls to runInteractiveProcess in
240 libraries/process/cbits/runProcess.c, and led to Trac #8965.
241
242 The simplest fix appears to be to declare these external functions with an
243 unspecified argument list rather than a void argument list. This is no
244 worse for platforms that don't care either way, and allows a successful
245 bootstrap of GHC 7.8 on little-endian Linux ppc64 (which uses the ELFv2
246 ABI).
247 */
248
249
250 /* -----------------------------------------------------------------------------
251 Tail calls
252 -------------------------------------------------------------------------- */
253
254 #define JMP_(cont) return((StgFunPtr)(cont))
255
256 /* -----------------------------------------------------------------------------
257 Other Stg stuff...
258 -------------------------------------------------------------------------- */
259
260 #include "stg/DLL.h"
261 #include "stg/RtsMachRegs.h"
262 #include "stg/Regs.h"
263 #include "stg/Ticky.h"
264
265 #if IN_STG_CODE
266 /*
267 * This is included later for RTS sources, after definitions of
268 * StgInfoTable, StgClosure and so on.
269 */
270 #include "stg/MiscClosures.h"
271 #endif
272
273 #include "stg/Prim.h" /* ghc-prim fallbacks */
274 #include "stg/SMP.h" // write_barrier() inline is required
275
276 /* -----------------------------------------------------------------------------
277 Moving Floats and Doubles
278
279 ASSIGN_FLT is for assigning a float to memory (usually the
280 stack/heap). The memory address is guaranteed to be
281 StgWord aligned (currently == sizeof(void *)).
282
283 PK_FLT is for pulling a float out of memory. The memory is
284 guaranteed to be StgWord aligned.
285 -------------------------------------------------------------------------- */
286
287 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
288 INLINE_HEADER StgFloat PK_FLT (W_ []);
289
290 #if ALIGNMENT_FLOAT <= ALIGNMENT_VOID_P
291
292 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
293 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
294
295 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
296
297 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
298 {
299 float_thing y;
300 y.f = src;
301 *p_dest = y.fu;
302 }
303
304 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
305 {
306 float_thing y;
307 y.fu = *p_src;
308 return(y.f);
309 }
310
311 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_VOID_P */
312
313 #if ALIGNMENT_DOUBLE <= ALIGNMENT_VOID_P
314
315 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
316 INLINE_HEADER StgDouble PK_DBL (W_ []);
317
318 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
319 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
320
321 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_VOID_P */
322
323 /* Sparc uses two floating point registers to hold a double. We can
324 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
325 * independently - unfortunately this code isn't writable in C, we
326 * have to use inline assembler.
327 */
328 #if sparc_HOST_ARCH
329
330 #define ASSIGN_DBL(dst0,src) \
331 { StgPtr dst = (StgPtr)(dst0); \
332 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
333 "=m" (((P_)(dst))[1]) : "f" (src)); \
334 }
335
336 #define PK_DBL(src0) \
337 ( { StgPtr src = (StgPtr)(src0); \
338 register double d; \
339 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
340 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
341 } )
342
343 #else /* ! sparc_HOST_ARCH */
344
345 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
346 INLINE_HEADER StgDouble PK_DBL (W_ []);
347
348 typedef struct
349 { StgWord dhi;
350 StgWord dlo;
351 } unpacked_double;
352
353 typedef union
354 { StgDouble d;
355 unpacked_double du;
356 } double_thing;
357
358 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
359 {
360 double_thing y;
361 y.d = src;
362 p_dest[0] = y.du.dhi;
363 p_dest[1] = y.du.dlo;
364 }
365
366 /* GCC also works with this version, but it generates
367 the same code as the previous one, and is not ANSI
368
369 #define ASSIGN_DBL( p_dest, src ) \
370 *p_dest = ((double_thing) src).du.dhi; \
371 *(p_dest+1) = ((double_thing) src).du.dlo \
372 */
373
374 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
375 {
376 double_thing y;
377 y.du.dhi = p_src[0];
378 y.du.dlo = p_src[1];
379 return(y.d);
380 }
381
382 #endif /* ! sparc_HOST_ARCH */
383
384 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
385
386
387 /* -----------------------------------------------------------------------------
388 Moving 64-bit quantities around
389
390 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
391 PK_Word64 load an StgWord64/StgInt64 from a amemory location
392
393 In both cases the memory location might not be 64-bit aligned.
394 -------------------------------------------------------------------------- */
395
396 #if SIZEOF_HSWORD == 4
397
398 typedef struct
399 { StgWord dhi;
400 StgWord dlo;
401 } unpacked_double_word;
402
403 typedef union
404 { StgInt64 i;
405 unpacked_double_word iu;
406 } int64_thing;
407
408 typedef union
409 { StgWord64 w;
410 unpacked_double_word wu;
411 } word64_thing;
412
413 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
414 {
415 word64_thing y;
416 y.w = src;
417 p_dest[0] = y.wu.dhi;
418 p_dest[1] = y.wu.dlo;
419 }
420
421 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
422 {
423 word64_thing y;
424 y.wu.dhi = p_src[0];
425 y.wu.dlo = p_src[1];
426 return(y.w);
427 }
428
429 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
430 {
431 int64_thing y;
432 y.i = src;
433 p_dest[0] = y.iu.dhi;
434 p_dest[1] = y.iu.dlo;
435 }
436
437 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
438 {
439 int64_thing y;
440 y.iu.dhi = p_src[0];
441 y.iu.dlo = p_src[1];
442 return(y.i);
443 }
444
445 #elif SIZEOF_VOID_P == 8
446
447 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
448 {
449 p_dest[0] = src;
450 }
451
452 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
453 {
454 return p_src[0];
455 }
456
457 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
458 {
459 p_dest[0] = src;
460 }
461
462 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
463 {
464 return p_src[0];
465 }
466
467 #endif /* SIZEOF_HSWORD == 4 */
468
469 /* -----------------------------------------------------------------------------
470 Split markers
471 -------------------------------------------------------------------------- */
472
473 #if defined(USE_SPLIT_MARKERS)
474 #if defined(LEADING_UNDERSCORE)
475 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
476 #else
477 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
478 #endif
479 #else
480 #define __STG_SPLIT_MARKER /* nothing */
481 #endif
482
483 /* -----------------------------------------------------------------------------
484 Integer multiply with overflow
485 -------------------------------------------------------------------------- */
486
487 /* Multiply with overflow checking.
488 *
489 * This is tricky - the usual sign rules for add/subtract don't apply.
490 *
491 * On 32-bit machines we use gcc's 'long long' types, finding
492 * overflow with some careful bit-twiddling.
493 *
494 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
495 * we use a crude approximation, testing whether either operand is
496 * larger than 32-bits; if neither is, then we go ahead with the
497 * multiplication.
498 *
499 * Return non-zero if there is any possibility that the signed multiply
500 * of a and b might overflow. Return zero only if you are absolutely sure
501 * that it won't overflow. If in doubt, return non-zero.
502 */
503
504 #if SIZEOF_VOID_P == 4
505
506 #ifdef WORDS_BIGENDIAN
507 #define RTS_CARRY_IDX__ 0
508 #define RTS_REM_IDX__ 1
509 #else
510 #define RTS_CARRY_IDX__ 1
511 #define RTS_REM_IDX__ 0
512 #endif
513
514 typedef union {
515 StgInt64 l;
516 StgInt32 i[2];
517 } long_long_u ;
518
519 #define mulIntMayOflo(a,b) \
520 ({ \
521 StgInt32 r, c; \
522 long_long_u z; \
523 z.l = (StgInt64)a * (StgInt64)b; \
524 r = z.i[RTS_REM_IDX__]; \
525 c = z.i[RTS_CARRY_IDX__]; \
526 if (c == 0 || c == -1) { \
527 c = ((StgWord)((a^b) ^ r)) \
528 >> (BITS_IN (I_) - 1); \
529 } \
530 c; \
531 })
532
533 /* Careful: the carry calculation above is extremely delicate. Make sure
534 * you test it thoroughly after changing it.
535 */
536
537 #else
538
539 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
540
541 /* If we have n-bit words then we have n-1 bits after accounting for the
542 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
543 #define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
544 #define HALF_NEG_INT (-HALF_POS_INT)
545
546 #define mulIntMayOflo(a,b) \
547 ({ \
548 I_ c; \
549 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
550 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\
551 c = 1; \
552 } else { \
553 c = 0; \
554 } \
555 c; \
556 })
557 #endif
558
559 #endif /* STG_H */