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
41 # define NO_GLOBAL_REG_DECLS /* don't define fixed registers */
45 #include "ghcconfig.h"
46 #include "RtsConfig.h"
48 /* The code generator calls the math functions directly in .hc code.
49 NB. after configuration stuff above, because this sets #defines
50 that depend on config info, such as __USE_FILE_OFFSET64 */
53 /* -----------------------------------------------------------------------------
55 -------------------------------------------------------------------------- */
58 * The C backend like to refer to labels by just mentioning their
59 * names. Howevver, when a symbol is declared as a variable in C, the
60 * C compiler will implicitly dereference it when it occurs in source.
61 * So we must subvert this behaviour for .hc files by declaring
62 * variables as arrays, which eliminates the implicit dereference.
65 #define RTS_VAR(x) (x)[]
66 #define RTS_DEREF(x) (*(x))
69 #define RTS_DEREF(x) x
74 #define BITS_PER_BYTE 8
75 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
77 /* Compute offsets of struct fields
79 #define STG_FIELD_OFFSET(s_type, field) ((StgWord)&(((s_type*)0)->field))
82 * 'Portable' inlining:
83 * INLINE_HEADER is for inline functions in header files (macros)
84 * STATIC_INLINE is for inline functions in source files
85 * EXTERN_INLINE is for functions that we want to inline sometimes
86 * (we also compile a static version of the function; see Inlines.c)
88 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
90 # define INLINE_HEADER static inline
91 # define INLINE_ME inline
92 # define STATIC_INLINE INLINE_HEADER
94 // The special "extern inline" behaviour is now only supported by gcc
95 // when _GNUC_GNU_INLINE__ is defined, and you have to use
96 // __attribute__((gnu_inline)). So when we don't have this, we use
97 // ordinary static inline.
99 // Apple's gcc defines __GNUC_GNU_INLINE__ without providing
100 // gnu_inline, so we exclude MacOS X and fall through to the safe
103 #if defined(__GNUC_GNU_INLINE__) && !defined(__APPLE__)
104 # if defined(KEEP_INLINES)
105 # define EXTERN_INLINE inline
107 # define EXTERN_INLINE extern inline __attribute__((gnu_inline))
110 # if defined(KEEP_INLINES)
111 # define EXTERN_INLINE
113 # define EXTERN_INLINE INLINE_HEADER
117 #elif defined(_MSC_VER)
119 # define INLINE_HEADER __inline static
120 # define INLINE_ME __inline
121 # define STATIC_INLINE INLINE_HEADER
123 # if defined(KEEP_INLINES)
124 # define EXTERN_INLINE __inline
126 # define EXTERN_INLINE __inline extern
131 # error "Don't know how to inline functions with your C compiler."
139 #if defined(__GNUC__)
140 #define GNU_ATTRIBUTE(at) __attribute__((at))
142 #define GNU_ATTRIBUTE(at)
146 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
148 #define GNUC3_ATTRIBUTE(at)
151 #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3
152 #define GNUC_ATTR_HOT __attribute__((hot))
154 #define GNUC_ATTR_HOT /* nothing */
157 #define STG_UNUSED GNUC3_ATTRIBUTE(__unused__)
159 /* -----------------------------------------------------------------------------
160 Global type definitions
161 -------------------------------------------------------------------------- */
163 #include "MachDeps.h"
164 #include "StgTypes.h"
166 /* -----------------------------------------------------------------------------
168 -------------------------------------------------------------------------- */
176 typedef const StgWord* D_;
177 typedef StgFunPtr F_;
178 typedef StgByteArray B_;
179 typedef StgClosurePtr L_;
181 typedef StgInt64 LI_;
182 typedef StgWord64 LW_;
184 #define IF_(f) static F_ GNUC3_ATTRIBUTE(used) f(void)
185 #define FN_(f) F_ f(void)
186 #define EF_(f) extern F_ f(void)
188 typedef StgWord StgWordArray[];
189 #define EI_(X) extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
190 #define II_(X) static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
192 /* -----------------------------------------------------------------------------
195 This needs to be up near the top as the register line on alpha needs
196 to be before all procedures (inline & out-of-line).
197 -------------------------------------------------------------------------- */
199 #include "TailCalls.h"
201 /* -----------------------------------------------------------------------------
203 -------------------------------------------------------------------------- */
206 #include "MachRegs.h"
210 #include "TickyCounters.h"
215 * This is included later for RTS sources, after definitions of
216 * StgInfoTable, StgClosure and so on.
218 #include "StgMiscClosures.h"
221 #include "SMP.h" // write_barrier() inline is required
223 /* -----------------------------------------------------------------------------
224 Moving Floats and Doubles
226 ASSIGN_FLT is for assigning a float to memory (usually the
227 stack/heap). The memory address is guaranteed to be
228 StgWord aligned (currently == sizeof(void *)).
230 PK_FLT is for pulling a float out of memory. The memory is
231 guaranteed to be StgWord aligned.
232 -------------------------------------------------------------------------- */
234 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
235 INLINE_HEADER StgFloat PK_FLT (W_ []);
237 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
239 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
240 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
242 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
244 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
251 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
258 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
260 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
262 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
263 INLINE_HEADER StgDouble PK_DBL (W_ []);
265 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
266 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
268 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
270 /* Sparc uses two floating point registers to hold a double. We can
271 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
272 * independently - unfortunately this code isn't writable in C, we
273 * have to use inline assembler.
277 #define ASSIGN_DBL(dst0,src) \
278 { StgPtr dst = (StgPtr)(dst0); \
279 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
280 "=m" (((P_)(dst))[1]) : "f" (src)); \
283 #define PK_DBL(src0) \
284 ( { StgPtr src = (StgPtr)(src0); \
286 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
287 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
290 #else /* ! sparc_HOST_ARCH */
292 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
293 INLINE_HEADER StgDouble PK_DBL (W_ []);
305 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
309 p_dest[0] = y.du.dhi;
310 p_dest[1] = y.du.dlo;
313 /* GCC also works with this version, but it generates
314 the same code as the previous one, and is not ANSI
316 #define ASSIGN_DBL( p_dest, src ) \
317 *p_dest = ((double_thing) src).du.dhi; \
318 *(p_dest+1) = ((double_thing) src).du.dlo \
321 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
329 #endif /* ! sparc_HOST_ARCH */
331 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
334 /* -----------------------------------------------------------------------------
335 Moving 64-bit quantities around
337 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
338 PK_Word64 load an StgWord64/StgInt64 from a amemory location
340 In both cases the memory location might not be 64-bit aligned.
341 -------------------------------------------------------------------------- */
343 #ifdef SUPPORT_LONG_LONGS
348 } unpacked_double_word;
352 unpacked_double_word iu;
357 unpacked_double_word wu;
360 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
364 p_dest[0] = y.wu.dhi;
365 p_dest[1] = y.wu.dlo;
368 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
376 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
380 p_dest[0] = y.iu.dhi;
381 p_dest[1] = y.iu.dlo;
384 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
392 #elif SIZEOF_VOID_P == 8
394 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
399 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
404 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
409 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
416 /* -----------------------------------------------------------------------------
418 -------------------------------------------------------------------------- */
420 #if defined(USE_SPLIT_MARKERS)
421 #if defined(LEADING_UNDERSCORE)
422 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
424 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
427 #define __STG_SPLIT_MARKER /* nothing */
430 /* -----------------------------------------------------------------------------
431 Write-combining store
432 -------------------------------------------------------------------------- */
435 wcStore (StgPtr p, StgWord w)
437 #ifdef x86_64_HOST_ARCH
448 /* -----------------------------------------------------------------------------
449 Integer multiply with overflow
450 -------------------------------------------------------------------------- */
452 /* Multiply with overflow checking.
454 * This is tricky - the usual sign rules for add/subtract don't apply.
456 * On 32-bit machines we use gcc's 'long long' types, finding
457 * overflow with some careful bit-twiddling.
459 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
460 * we use a crude approximation, testing whether either operand is
461 * larger than 32-bits; if neither is, then we go ahead with the
464 * Return non-zero if there is any possibility that the signed multiply
465 * of a and b might overflow. Return zero only if you are absolutely sure
466 * that it won't overflow. If in doubt, return non-zero.
469 #if SIZEOF_VOID_P == 4
471 #ifdef WORDS_BIGENDIAN
472 #define RTS_CARRY_IDX__ 0
473 #define RTS_REM_IDX__ 1
475 #define RTS_CARRY_IDX__ 1
476 #define RTS_REM_IDX__ 0
484 #define mulIntMayOflo(a,b) \
488 z.l = (StgInt64)a * (StgInt64)b; \
489 r = z.i[RTS_REM_IDX__]; \
490 c = z.i[RTS_CARRY_IDX__]; \
491 if (c == 0 || c == -1) { \
492 c = ((StgWord)((a^b) ^ r)) \
493 >> (BITS_IN (I_) - 1); \
498 /* Careful: the carry calculation above is extremely delicate. Make sure
499 * you test it thoroughly after changing it.
504 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
506 /* If we have n-bit words then we have n-1 bits after accounting for the
507 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
508 #define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
509 #define HALF_NEG_INT (-HALF_POS_INT)
511 #define mulIntMayOflo(a,b) \
514 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
515 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\