1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * Top-level include file for everything STG-ish.
7 * This file is included *automatically* by all .hc files.
9 * NOTE: always include Stg.h *before* any other headers, because we
10 * define some register variables which must be done before any inline
11 * functions are defined (some system headers have been known to
12 * define the odd inline function).
14 * We generally try to keep as little visible as possible when
15 * compiling .hc files. So for example the definitions of the
16 * InfoTable structs, closure structs and other RTS types are not
17 * visible here. The compiler knows enough about the representations
18 * of these types to generate code which manipulates them directly
19 * with pointer arithmetic.
21 * ---------------------------------------------------------------------------*/
27 /* If we include "Stg.h" directly, we're in STG code, and we therefore
28 * get all the global register variables, macros etc. that go along
29 * with that. If "Stg.h" is included via "Rts.h", we're assumed to
33 # define IN_STG_CODE 1
35 // Turn on C99 for .hc code. This gives us the INFINITY and NAN
36 // constants from math.h, which we occasionally need to use in .hc (#1861)
37 # define _ISOC99_SOURCE
39 // We need _BSD_SOURCE so that math.h defines things like gamma
45 # define NO_GLOBAL_REG_DECLS /* don't define fixed registers */
49 #include "ghcconfig.h"
50 #include "RtsConfig.h"
52 /* The code generator calls the math functions directly in .hc code.
53 NB. after configuration stuff above, because this sets #defines
54 that depend on config info, such as __USE_FILE_OFFSET64 */
57 /* -----------------------------------------------------------------------------
59 -------------------------------------------------------------------------- */
62 * The C backend like to refer to labels by just mentioning their
63 * names. Howevver, when a symbol is declared as a variable in C, the
64 * C compiler will implicitly dereference it when it occurs in source.
65 * So we must subvert this behaviour for .hc files by declaring
66 * variables as arrays, which eliminates the implicit dereference.
69 #define RTS_VAR(x) (x)[]
70 #define RTS_DEREF(x) (*(x))
73 #define RTS_DEREF(x) x
78 #define BITS_PER_BYTE 8
79 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
81 /* Compute offsets of struct fields
83 #define STG_FIELD_OFFSET(s_type, field) ((StgWord)&(((s_type*)0)->field))
86 * 'Portable' inlining:
87 * INLINE_HEADER is for inline functions in header files (macros)
88 * STATIC_INLINE is for inline functions in source files
89 * EXTERN_INLINE is for functions that we want to inline sometimes
90 * (we also compile a static version of the function; see Inlines.c)
92 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
94 # define INLINE_HEADER static inline
95 # define INLINE_ME inline
96 # define STATIC_INLINE INLINE_HEADER
98 // The special "extern inline" behaviour is now only supported by gcc
99 // when _GNUC_GNU_INLINE__ is defined, and you have to use
100 // __attribute__((gnu_inline)). So when we don't have this, we use
101 // ordinary static inline.
103 // Apple's gcc defines __GNUC_GNU_INLINE__ without providing
104 // gnu_inline, so we exclude MacOS X and fall through to the safe
107 #if defined(__GNUC_GNU_INLINE__) && !defined(__APPLE__)
108 # if defined(KEEP_INLINES)
109 # define EXTERN_INLINE inline
111 # define EXTERN_INLINE extern inline __attribute__((gnu_inline))
114 # if defined(KEEP_INLINES)
115 # define EXTERN_INLINE
117 # define EXTERN_INLINE INLINE_HEADER
121 #elif defined(_MSC_VER)
123 # define INLINE_HEADER __inline static
124 # define INLINE_ME __inline
125 # define STATIC_INLINE INLINE_HEADER
127 # if defined(KEEP_INLINES)
128 # define EXTERN_INLINE __inline
130 # define EXTERN_INLINE __inline extern
135 # error "Don't know how to inline functions with your C compiler."
143 #if defined(__GNUC__)
144 #define GNU_ATTRIBUTE(at) __attribute__((at))
146 #define GNU_ATTRIBUTE(at)
150 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
152 #define GNUC3_ATTRIBUTE(at)
155 #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3
156 #define GNUC_ATTR_HOT __attribute__((hot))
158 #define GNUC_ATTR_HOT /* nothing */
161 #define STG_UNUSED GNUC3_ATTRIBUTE(__unused__)
163 /* -----------------------------------------------------------------------------
164 Global type definitions
165 -------------------------------------------------------------------------- */
167 #include "MachDeps.h"
168 #include "StgTypes.h"
170 /* -----------------------------------------------------------------------------
172 -------------------------------------------------------------------------- */
180 typedef const StgWord* D_;
181 typedef StgFunPtr F_;
182 typedef StgByteArray B_;
183 typedef StgClosurePtr L_;
185 typedef StgInt64 LI_;
186 typedef StgWord64 LW_;
188 #define IF_(f) static F_ GNUC3_ATTRIBUTE(used) f(void)
189 #define FN_(f) F_ f(void)
190 #define EF_(f) extern F_ f(void)
192 typedef StgWord StgWordArray[];
193 #define EI_(X) extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
194 #define II_(X) static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
196 /* -----------------------------------------------------------------------------
199 This needs to be up near the top as the register line on alpha needs
200 to be before all procedures (inline & out-of-line).
201 -------------------------------------------------------------------------- */
203 #include "TailCalls.h"
205 /* -----------------------------------------------------------------------------
207 -------------------------------------------------------------------------- */
210 #include "MachRegs.h"
213 #include "TickyCounters.h"
217 * This is included later for RTS sources, after definitions of
218 * StgInfoTable, StgClosure and so on.
220 #include "StgMiscClosures.h"
223 #include "SMP.h" // write_barrier() inline is required
225 /* -----------------------------------------------------------------------------
226 Moving Floats and Doubles
228 ASSIGN_FLT is for assigning a float to memory (usually the
229 stack/heap). The memory address is guaranteed to be
230 StgWord aligned (currently == sizeof(void *)).
232 PK_FLT is for pulling a float out of memory. The memory is
233 guaranteed to be StgWord aligned.
234 -------------------------------------------------------------------------- */
236 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
237 INLINE_HEADER StgFloat PK_FLT (W_ []);
239 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
241 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
242 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
244 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
246 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
253 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
260 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
262 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
264 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
265 INLINE_HEADER StgDouble PK_DBL (W_ []);
267 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
268 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
270 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
272 /* Sparc uses two floating point registers to hold a double. We can
273 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
274 * independently - unfortunately this code isn't writable in C, we
275 * have to use inline assembler.
279 #define ASSIGN_DBL(dst0,src) \
280 { StgPtr dst = (StgPtr)(dst0); \
281 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
282 "=m" (((P_)(dst))[1]) : "f" (src)); \
285 #define PK_DBL(src0) \
286 ( { StgPtr src = (StgPtr)(src0); \
288 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
289 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
292 #else /* ! sparc_HOST_ARCH */
294 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
295 INLINE_HEADER StgDouble PK_DBL (W_ []);
307 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
311 p_dest[0] = y.du.dhi;
312 p_dest[1] = y.du.dlo;
315 /* GCC also works with this version, but it generates
316 the same code as the previous one, and is not ANSI
318 #define ASSIGN_DBL( p_dest, src ) \
319 *p_dest = ((double_thing) src).du.dhi; \
320 *(p_dest+1) = ((double_thing) src).du.dlo \
323 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
331 #endif /* ! sparc_HOST_ARCH */
333 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
336 /* -----------------------------------------------------------------------------
337 Moving 64-bit quantities around
339 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
340 PK_Word64 load an StgWord64/StgInt64 from a amemory location
342 In both cases the memory location might not be 64-bit aligned.
343 -------------------------------------------------------------------------- */
345 #ifdef SUPPORT_LONG_LONGS
350 } unpacked_double_word;
354 unpacked_double_word iu;
359 unpacked_double_word wu;
362 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
366 p_dest[0] = y.wu.dhi;
367 p_dest[1] = y.wu.dlo;
370 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
378 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
382 p_dest[0] = y.iu.dhi;
383 p_dest[1] = y.iu.dlo;
386 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
394 #elif SIZEOF_VOID_P == 8
396 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
401 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
406 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
411 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
418 /* -----------------------------------------------------------------------------
420 -------------------------------------------------------------------------- */
422 #if defined(USE_SPLIT_MARKERS)
423 #if defined(LEADING_UNDERSCORE)
424 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
426 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
429 #define __STG_SPLIT_MARKER /* nothing */
432 /* -----------------------------------------------------------------------------
433 Write-combining store
434 -------------------------------------------------------------------------- */
437 wcStore (StgPtr p, StgWord w)
439 #ifdef x86_64_HOST_ARCH
450 /* -----------------------------------------------------------------------------
451 Integer multiply with overflow
452 -------------------------------------------------------------------------- */
454 /* Multiply with overflow checking.
456 * This is tricky - the usual sign rules for add/subtract don't apply.
458 * On 32-bit machines we use gcc's 'long long' types, finding
459 * overflow with some careful bit-twiddling.
461 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
462 * we use a crude approximation, testing whether either operand is
463 * larger than 32-bits; if neither is, then we go ahead with the
466 * Return non-zero if there is any possibility that the signed multiply
467 * of a and b might overflow. Return zero only if you are absolutely sure
468 * that it won't overflow. If in doubt, return non-zero.
471 #if SIZEOF_VOID_P == 4
473 #ifdef WORDS_BIGENDIAN
474 #define RTS_CARRY_IDX__ 0
475 #define RTS_REM_IDX__ 1
477 #define RTS_CARRY_IDX__ 1
478 #define RTS_REM_IDX__ 0
486 #define mulIntMayOflo(a,b) \
490 z.l = (StgInt64)a * (StgInt64)b; \
491 r = z.i[RTS_REM_IDX__]; \
492 c = z.i[RTS_CARRY_IDX__]; \
493 if (c == 0 || c == -1) { \
494 c = ((StgWord)((a^b) ^ r)) \
495 >> (BITS_IN (I_) - 1); \
500 /* Careful: the carry calculation above is extremely delicate. Make sure
501 * you test it thoroughly after changing it.
506 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
508 /* If we have n-bit words then we have n-1 bits after accounting for the
509 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
510 #define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
511 #define HALF_NEG_INT (-HALF_POS_INT)
513 #define mulIntMayOflo(a,b) \
516 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
517 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\