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