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
34 # define _ISOC99_SOURCE
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)
40 # define NO_GLOBAL_REG_DECLS /* don't define fixed registers */
44 #include "ghcconfig.h"
45 #include "RtsConfig.h"
47 /* The code generator calls the math functions directly in .hc code.
48 NB. after configuration stuff above, because this sets #defines
49 that depend on config info, such as __USE_FILE_OFFSET64 */
52 /* -----------------------------------------------------------------------------
54 -------------------------------------------------------------------------- */
57 * The C backend like to refer to labels by just mentioning their
58 * names. Howevver, when a symbol is declared as a variable in C, the
59 * C compiler will implicitly dereference it when it occurs in source.
60 * So we must subvert this behaviour for .hc files by declaring
61 * variables as arrays, which eliminates the implicit dereference.
64 #define RTS_VAR(x) (x)[]
65 #define RTS_DEREF(x) (*(x))
68 #define RTS_DEREF(x) x
73 #define BITS_PER_BYTE 8
74 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
77 * 'Portable' inlining:
78 * INLINE_HEADER is for inline functions in header files (macros)
79 * STATIC_INLINE is for inline functions in source files
80 * EXTERN_INLINE is for functions that we want to inline sometimes
82 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
84 # define INLINE_HEADER static inline
85 # define INLINE_ME inline
86 # define STATIC_INLINE INLINE_HEADER
88 # if defined(KEEP_INLINES)
89 # define EXTERN_INLINE inline
91 # define EXTERN_INLINE extern inline
94 #elif defined(_MSC_VER)
96 # define INLINE_HEADER __inline static
97 # define INLINE_ME __inline
98 # define STATIC_INLINE INLINE_HEADER
100 # if defined(KEEP_INLINES)
101 # define EXTERN_INLINE __inline
103 # define EXTERN_INLINE __inline extern
108 # error "Don't know how to inline functions with your C compiler."
116 #if defined(__GNUC__)
117 #define GNU_ATTRIBUTE(at) __attribute__((at))
119 #define GNU_ATTRIBUTE(at)
123 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
125 #define GNUC3_ATTRIBUTE(at)
128 #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3
129 #define GNUC_ATTR_HOT __attribute__((hot))
131 #define GNUC_ATTR_HOT /* nothing */
134 #define STG_UNUSED GNUC3_ATTRIBUTE(__unused__)
136 /* -----------------------------------------------------------------------------
137 Global type definitions
138 -------------------------------------------------------------------------- */
140 #include "MachDeps.h"
141 #include "StgTypes.h"
143 /* -----------------------------------------------------------------------------
145 -------------------------------------------------------------------------- */
153 typedef const StgWord* D_;
154 typedef StgFunPtr F_;
155 typedef StgByteArray B_;
156 typedef StgClosurePtr L_;
158 typedef StgInt64 LI_;
159 typedef StgWord64 LW_;
161 #define IF_(f) static F_ GNUC3_ATTRIBUTE(used) f(void)
162 #define FN_(f) F_ f(void)
163 #define EF_(f) extern F_ f(void)
165 typedef StgWord StgWordArray[];
166 #define EI_(X) extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
167 #define II_(X) static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
169 /* -----------------------------------------------------------------------------
172 This needs to be up near the top as the register line on alpha needs
173 to be before all procedures (inline & out-of-line).
174 -------------------------------------------------------------------------- */
176 #include "TailCalls.h"
178 /* -----------------------------------------------------------------------------
180 -------------------------------------------------------------------------- */
183 #include "MachRegs.h"
187 #include "TickyCounters.h"
192 * This is included later for RTS sources, after definitions of
193 * StgInfoTable, StgClosure and so on.
195 #include "StgMiscClosures.h"
198 #include "SMP.h" // write_barrier() inline is required
200 /* -----------------------------------------------------------------------------
201 Moving Floats and Doubles
203 ASSIGN_FLT is for assigning a float to memory (usually the
204 stack/heap). The memory address is guaranteed to be
205 StgWord aligned (currently == sizeof(void *)).
207 PK_FLT is for pulling a float out of memory. The memory is
208 guaranteed to be StgWord aligned.
209 -------------------------------------------------------------------------- */
211 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
212 INLINE_HEADER StgFloat PK_FLT (W_ []);
214 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
216 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
217 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
219 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
221 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
228 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
235 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
237 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
239 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
240 INLINE_HEADER StgDouble PK_DBL (W_ []);
242 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
243 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
245 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
247 /* Sparc uses two floating point registers to hold a double. We can
248 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
249 * independently - unfortunately this code isn't writable in C, we
250 * have to use inline assembler.
254 #define ASSIGN_DBL(dst0,src) \
255 { StgPtr dst = (StgPtr)(dst0); \
256 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
257 "=m" (((P_)(dst))[1]) : "f" (src)); \
260 #define PK_DBL(src0) \
261 ( { StgPtr src = (StgPtr)(src0); \
263 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
264 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
267 #else /* ! sparc_HOST_ARCH */
269 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
270 INLINE_HEADER StgDouble PK_DBL (W_ []);
282 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
286 p_dest[0] = y.du.dhi;
287 p_dest[1] = y.du.dlo;
290 /* GCC also works with this version, but it generates
291 the same code as the previous one, and is not ANSI
293 #define ASSIGN_DBL( p_dest, src ) \
294 *p_dest = ((double_thing) src).du.dhi; \
295 *(p_dest+1) = ((double_thing) src).du.dlo \
298 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
306 #endif /* ! sparc_HOST_ARCH */
308 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
311 /* -----------------------------------------------------------------------------
312 Moving 64-bit quantities around
314 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
315 PK_Word64 load an StgWord64/StgInt64 from a amemory location
317 In both cases the memory location might not be 64-bit aligned.
318 -------------------------------------------------------------------------- */
320 #ifdef SUPPORT_LONG_LONGS
325 } unpacked_double_word;
329 unpacked_double_word iu;
334 unpacked_double_word wu;
337 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
341 p_dest[0] = y.wu.dhi;
342 p_dest[1] = y.wu.dlo;
345 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
353 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
357 p_dest[0] = y.iu.dhi;
358 p_dest[1] = y.iu.dlo;
361 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
369 #elif SIZEOF_VOID_P == 8
371 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
376 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
381 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
386 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
393 /* -----------------------------------------------------------------------------
395 -------------------------------------------------------------------------- */
397 #if defined(USE_SPLIT_MARKERS)
398 #if defined(LEADING_UNDERSCORE)
399 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
401 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
404 #define __STG_SPLIT_MARKER /* nothing */
407 /* -----------------------------------------------------------------------------
408 Write-combining store
409 -------------------------------------------------------------------------- */
412 wcStore (StgPtr p, StgWord w)
414 #ifdef x86_64_HOST_ARCH
425 /* -----------------------------------------------------------------------------
426 Integer multiply with overflow
427 -------------------------------------------------------------------------- */
429 /* Multiply with overflow checking.
431 * This is tricky - the usual sign rules for add/subtract don't apply.
433 * On 32-bit machines we use gcc's 'long long' types, finding
434 * overflow with some careful bit-twiddling.
436 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
437 * we use a crude approximation, testing whether either operand is
438 * larger than 32-bits; if neither is, then we go ahead with the
441 * Return non-zero if there is any possibility that the signed multiply
442 * of a and b might overflow. Return zero only if you are absolutely sure
443 * that it won't overflow. If in doubt, return non-zero.
446 #if SIZEOF_VOID_P == 4
448 #ifdef WORDS_BIGENDIAN
449 #define RTS_CARRY_IDX__ 0
450 #define RTS_REM_IDX__ 1
452 #define RTS_CARRY_IDX__ 1
453 #define RTS_REM_IDX__ 0
461 #define mulIntMayOflo(a,b) \
465 z.l = (StgInt64)a * (StgInt64)b; \
466 r = z.i[RTS_REM_IDX__]; \
467 c = z.i[RTS_CARRY_IDX__]; \
468 if (c == 0 || c == -1) { \
469 c = ((StgWord)((a^b) ^ r)) \
470 >> (BITS_IN (I_) - 1); \
475 /* Careful: the carry calculation above is extremely delicate. Make sure
476 * you test it thoroughly after changing it.
481 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
483 /* If we have n-bit words then we have n-1 bits after accounting for the
484 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
485 #define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
486 #define HALF_NEG_INT (-HALF_POS_INT)
488 #define mulIntMayOflo(a,b) \
491 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
492 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\