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