Split external symbol prototypes (EF_) (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) StgFunPtr f(void) /* External Cmm functions */
226 #define EFF_(f) void f() /* See Note [External function prototypes] */
227
228 /* Note [External function prototypes] See Trac #8965, #11395
229 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
230 In generated C code we need to distinct between two types
231 of external symbols:
232 1. Cmm functions declared by 'EF_' macro (External Functions)
233 2. C functions declared by 'EFF_' macro (External Foreign Functions)
234
235 Cmm functions are simple as they are internal to GHC.
236
237 C functions are trickier:
238
239 The external-function macro EFF_(F) used to be defined as
240 extern StgFunPtr f(void)
241 i.e a function of zero arguments. On most platforms this doesn't
242 matter very much: calls to these functions put the parameters in the
243 usual places anyway, and (with the exception of varargs) things just
244 work.
245
246 However, the ELFv2 ABI on ppc64 optimises stack allocation
247 (http://gcc.gnu.org/ml/gcc-patches/2013-11/msg01149.html): a call to a
248 function that has a prototype, is not varargs, and receives all parameters
249 in registers rather than on the stack does not require the caller to
250 allocate an argument save area. The incorrect prototypes cause GCC to
251 believe that all functions declared this way can be called without an
252 argument save area, but if the callee has sufficiently many arguments then
253 it will expect that area to be present, and will thus corrupt the caller's
254 stack. This happens in particular with calls to runInteractiveProcess in
255 libraries/process/cbits/runProcess.c, and led to Trac #8965.
256
257 The simplest fix appears to be to declare these external functions with an
258 unspecified argument list rather than a void argument list. This is no
259 worse for platforms that don't care either way, and allows a successful
260 bootstrap of GHC 7.8 on little-endian Linux ppc64 (which uses the ELFv2
261 ABI).
262
263 Another case is m68k ABI where 'void*' return type is returned by 'a0'
264 register while 'long' return type is returned by 'd0'. Thus we trick
265 external prototype return neither of these types to workaround #11395.
266 */
267
268
269 /* -----------------------------------------------------------------------------
270 Tail calls
271 -------------------------------------------------------------------------- */
272
273 #define JMP_(cont) return((StgFunPtr)(cont))
274
275 /* -----------------------------------------------------------------------------
276 Other Stg stuff...
277 -------------------------------------------------------------------------- */
278
279 #include "stg/DLL.h"
280 #include "stg/RtsMachRegs.h"
281 #include "stg/Regs.h"
282 #include "stg/Ticky.h"
283
284 #if IN_STG_CODE
285 /*
286 * This is included later for RTS sources, after definitions of
287 * StgInfoTable, StgClosure and so on.
288 */
289 #include "stg/MiscClosures.h"
290 #endif
291
292 #include "stg/Prim.h" /* ghc-prim fallbacks */
293 #include "stg/SMP.h" // write_barrier() inline is required
294
295 /* -----------------------------------------------------------------------------
296 Moving Floats and Doubles
297
298 ASSIGN_FLT is for assigning a float to memory (usually the
299 stack/heap). The memory address is guaranteed to be
300 StgWord aligned (currently == sizeof(void *)).
301
302 PK_FLT is for pulling a float out of memory. The memory is
303 guaranteed to be StgWord aligned.
304 -------------------------------------------------------------------------- */
305
306 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
307 INLINE_HEADER StgFloat PK_FLT (W_ []);
308
309 #if ALIGNMENT_FLOAT <= ALIGNMENT_VOID_P
310
311 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
312 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
313
314 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
315
316 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
317 {
318 float_thing y;
319 y.f = src;
320 *p_dest = y.fu;
321 }
322
323 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
324 {
325 float_thing y;
326 y.fu = *p_src;
327 return(y.f);
328 }
329
330 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_VOID_P */
331
332 #if ALIGNMENT_DOUBLE <= ALIGNMENT_VOID_P
333
334 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
335 INLINE_HEADER StgDouble PK_DBL (W_ []);
336
337 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
338 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
339
340 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_VOID_P */
341
342 /* Sparc uses two floating point registers to hold a double. We can
343 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
344 * independently - unfortunately this code isn't writable in C, we
345 * have to use inline assembler.
346 */
347 #if sparc_HOST_ARCH
348
349 #define ASSIGN_DBL(dst0,src) \
350 { StgPtr dst = (StgPtr)(dst0); \
351 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
352 "=m" (((P_)(dst))[1]) : "f" (src)); \
353 }
354
355 #define PK_DBL(src0) \
356 ( { StgPtr src = (StgPtr)(src0); \
357 register double d; \
358 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
359 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
360 } )
361
362 #else /* ! sparc_HOST_ARCH */
363
364 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
365 INLINE_HEADER StgDouble PK_DBL (W_ []);
366
367 typedef struct
368 { StgWord dhi;
369 StgWord dlo;
370 } unpacked_double;
371
372 typedef union
373 { StgDouble d;
374 unpacked_double du;
375 } double_thing;
376
377 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
378 {
379 double_thing y;
380 y.d = src;
381 p_dest[0] = y.du.dhi;
382 p_dest[1] = y.du.dlo;
383 }
384
385 /* GCC also works with this version, but it generates
386 the same code as the previous one, and is not ANSI
387
388 #define ASSIGN_DBL( p_dest, src ) \
389 *p_dest = ((double_thing) src).du.dhi; \
390 *(p_dest+1) = ((double_thing) src).du.dlo \
391 */
392
393 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
394 {
395 double_thing y;
396 y.du.dhi = p_src[0];
397 y.du.dlo = p_src[1];
398 return(y.d);
399 }
400
401 #endif /* ! sparc_HOST_ARCH */
402
403 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
404
405
406 /* -----------------------------------------------------------------------------
407 Moving 64-bit quantities around
408
409 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
410 PK_Word64 load an StgWord64/StgInt64 from a amemory location
411
412 In both cases the memory location might not be 64-bit aligned.
413 -------------------------------------------------------------------------- */
414
415 #if SIZEOF_HSWORD == 4
416
417 typedef struct
418 { StgWord dhi;
419 StgWord dlo;
420 } unpacked_double_word;
421
422 typedef union
423 { StgInt64 i;
424 unpacked_double_word iu;
425 } int64_thing;
426
427 typedef union
428 { StgWord64 w;
429 unpacked_double_word wu;
430 } word64_thing;
431
432 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
433 {
434 word64_thing y;
435 y.w = src;
436 p_dest[0] = y.wu.dhi;
437 p_dest[1] = y.wu.dlo;
438 }
439
440 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
441 {
442 word64_thing y;
443 y.wu.dhi = p_src[0];
444 y.wu.dlo = p_src[1];
445 return(y.w);
446 }
447
448 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
449 {
450 int64_thing y;
451 y.i = src;
452 p_dest[0] = y.iu.dhi;
453 p_dest[1] = y.iu.dlo;
454 }
455
456 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
457 {
458 int64_thing y;
459 y.iu.dhi = p_src[0];
460 y.iu.dlo = p_src[1];
461 return(y.i);
462 }
463
464 #elif SIZEOF_VOID_P == 8
465
466 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
467 {
468 p_dest[0] = src;
469 }
470
471 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
472 {
473 return p_src[0];
474 }
475
476 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
477 {
478 p_dest[0] = src;
479 }
480
481 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
482 {
483 return p_src[0];
484 }
485
486 #endif /* SIZEOF_HSWORD == 4 */
487
488 /* -----------------------------------------------------------------------------
489 Split markers
490 -------------------------------------------------------------------------- */
491
492 #if defined(USE_SPLIT_MARKERS)
493 #if defined(LEADING_UNDERSCORE)
494 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
495 #else
496 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
497 #endif
498 #else
499 #define __STG_SPLIT_MARKER /* nothing */
500 #endif
501
502 /* -----------------------------------------------------------------------------
503 Integer multiply with overflow
504 -------------------------------------------------------------------------- */
505
506 /* Multiply with overflow checking.
507 *
508 * This is tricky - the usual sign rules for add/subtract don't apply.
509 *
510 * On 32-bit machines we use gcc's 'long long' types, finding
511 * overflow with some careful bit-twiddling.
512 *
513 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
514 * we use a crude approximation, testing whether either operand is
515 * larger than 32-bits; if neither is, then we go ahead with the
516 * multiplication.
517 *
518 * Return non-zero if there is any possibility that the signed multiply
519 * of a and b might overflow. Return zero only if you are absolutely sure
520 * that it won't overflow. If in doubt, return non-zero.
521 */
522
523 #if SIZEOF_VOID_P == 4
524
525 #ifdef WORDS_BIGENDIAN
526 #define RTS_CARRY_IDX__ 0
527 #define RTS_REM_IDX__ 1
528 #else
529 #define RTS_CARRY_IDX__ 1
530 #define RTS_REM_IDX__ 0
531 #endif
532
533 typedef union {
534 StgInt64 l;
535 StgInt32 i[2];
536 } long_long_u ;
537
538 #define mulIntMayOflo(a,b) \
539 ({ \
540 StgInt32 r, c; \
541 long_long_u z; \
542 z.l = (StgInt64)a * (StgInt64)b; \
543 r = z.i[RTS_REM_IDX__]; \
544 c = z.i[RTS_CARRY_IDX__]; \
545 if (c == 0 || c == -1) { \
546 c = ((StgWord)((a^b) ^ r)) \
547 >> (BITS_IN (I_) - 1); \
548 } \
549 c; \
550 })
551
552 /* Careful: the carry calculation above is extremely delicate. Make sure
553 * you test it thoroughly after changing it.
554 */
555
556 #else
557
558 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
559
560 /* If we have n-bit words then we have n-1 bits after accounting for the
561 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
562 #define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
563 #define HALF_NEG_INT (-HALF_POS_INT)
564
565 #define mulIntMayOflo(a,b) \
566 ({ \
567 I_ c; \
568 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
569 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\
570 c = 1; \
571 } else { \
572 c = 0; \
573 } \
574 c; \
575 })
576 #endif
577
578 #endif /* STG_H */