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