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