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 // Turning on _ISOC99_SOURCE means S_ISSOCK gets defined on Linux
44 # define NO_GLOBAL_REG_DECLS /* don't define fixed registers */
48 #include "ghcconfig.h"
49 #include "RtsConfig.h"
51 /* The code generator calls the math functions directly in .hc code.
52 NB. after configuration stuff above, because this sets #defines
53 that depend on config info, such as __USE_FILE_OFFSET64 */
56 /* -----------------------------------------------------------------------------
58 -------------------------------------------------------------------------- */
61 * The C backend like to refer to labels by just mentioning their
62 * names. Howevver, when a symbol is declared as a variable in C, the
63 * C compiler will implicitly dereference it when it occurs in source.
64 * So we must subvert this behaviour for .hc files by declaring
65 * variables as arrays, which eliminates the implicit dereference.
68 #define RTS_VAR(x) (x)[]
69 #define RTS_DEREF(x) (*(x))
72 #define RTS_DEREF(x) x
77 #define BITS_PER_BYTE 8
78 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
81 * 'Portable' inlining:
82 * INLINE_HEADER is for inline functions in header files (macros)
83 * STATIC_INLINE is for inline functions in source files
84 * EXTERN_INLINE is for functions that we want to inline sometimes
86 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
88 # define INLINE_HEADER static inline
89 # define INLINE_ME inline
90 # define STATIC_INLINE INLINE_HEADER
92 # if defined(KEEP_INLINES)
93 # define EXTERN_INLINE inline
95 # define EXTERN_INLINE extern inline
98 #elif defined(_MSC_VER)
100 # define INLINE_HEADER __inline static
101 # define INLINE_ME __inline
102 # define STATIC_INLINE INLINE_HEADER
104 # if defined(KEEP_INLINES)
105 # define EXTERN_INLINE __inline
107 # define EXTERN_INLINE __inline extern
112 # error "Don't know how to inline functions with your C compiler."
120 #if defined(__GNUC__)
121 #define GNU_ATTRIBUTE(at) __attribute__((at))
123 #define GNU_ATTRIBUTE(at)
127 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
129 #define GNUC3_ATTRIBUTE(at)
132 #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3
133 #define GNUC_ATTR_HOT __attribute__((hot))
135 #define GNUC_ATTR_HOT /* nothing */
138 #define STG_UNUSED GNUC3_ATTRIBUTE(__unused__)
140 /* -----------------------------------------------------------------------------
141 Global type definitions
142 -------------------------------------------------------------------------- */
144 #include "MachDeps.h"
145 #include "StgTypes.h"
147 /* -----------------------------------------------------------------------------
149 -------------------------------------------------------------------------- */
157 typedef const StgWord* D_;
158 typedef StgFunPtr F_;
159 typedef StgByteArray B_;
160 typedef StgClosurePtr L_;
162 typedef StgInt64 LI_;
163 typedef StgWord64 LW_;
165 #define IF_(f) static F_ GNUC3_ATTRIBUTE(used) f(void)
166 #define FN_(f) F_ f(void)
167 #define EF_(f) extern F_ f(void)
169 typedef StgWord StgWordArray[];
170 #define EI_(X) extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
171 #define II_(X) static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
173 /* -----------------------------------------------------------------------------
176 This needs to be up near the top as the register line on alpha needs
177 to be before all procedures (inline & out-of-line).
178 -------------------------------------------------------------------------- */
180 #include "TailCalls.h"
182 /* -----------------------------------------------------------------------------
184 -------------------------------------------------------------------------- */
187 #include "MachRegs.h"
191 #include "TickyCounters.h"
196 * This is included later for RTS sources, after definitions of
197 * StgInfoTable, StgClosure and so on.
199 #include "StgMiscClosures.h"
202 #include "SMP.h" // write_barrier() inline is required
204 /* -----------------------------------------------------------------------------
205 Moving Floats and Doubles
207 ASSIGN_FLT is for assigning a float to memory (usually the
208 stack/heap). The memory address is guaranteed to be
209 StgWord aligned (currently == sizeof(void *)).
211 PK_FLT is for pulling a float out of memory. The memory is
212 guaranteed to be StgWord aligned.
213 -------------------------------------------------------------------------- */
215 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
216 INLINE_HEADER StgFloat PK_FLT (W_ []);
218 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
220 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
221 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
223 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
225 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
232 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
239 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
241 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
243 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
244 INLINE_HEADER StgDouble PK_DBL (W_ []);
246 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
247 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
249 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
251 /* Sparc uses two floating point registers to hold a double. We can
252 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
253 * independently - unfortunately this code isn't writable in C, we
254 * have to use inline assembler.
258 #define ASSIGN_DBL(dst0,src) \
259 { StgPtr dst = (StgPtr)(dst0); \
260 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
261 "=m" (((P_)(dst))[1]) : "f" (src)); \
264 #define PK_DBL(src0) \
265 ( { StgPtr src = (StgPtr)(src0); \
267 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
268 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
271 #else /* ! sparc_HOST_ARCH */
273 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
274 INLINE_HEADER StgDouble PK_DBL (W_ []);
286 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
290 p_dest[0] = y.du.dhi;
291 p_dest[1] = y.du.dlo;
294 /* GCC also works with this version, but it generates
295 the same code as the previous one, and is not ANSI
297 #define ASSIGN_DBL( p_dest, src ) \
298 *p_dest = ((double_thing) src).du.dhi; \
299 *(p_dest+1) = ((double_thing) src).du.dlo \
302 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
310 #endif /* ! sparc_HOST_ARCH */
312 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
315 /* -----------------------------------------------------------------------------
316 Moving 64-bit quantities around
318 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
319 PK_Word64 load an StgWord64/StgInt64 from a amemory location
321 In both cases the memory location might not be 64-bit aligned.
322 -------------------------------------------------------------------------- */
324 #ifdef SUPPORT_LONG_LONGS
329 } unpacked_double_word;
333 unpacked_double_word iu;
338 unpacked_double_word wu;
341 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
345 p_dest[0] = y.wu.dhi;
346 p_dest[1] = y.wu.dlo;
349 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
357 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
361 p_dest[0] = y.iu.dhi;
362 p_dest[1] = y.iu.dlo;
365 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
373 #elif SIZEOF_VOID_P == 8
375 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
380 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
385 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
390 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
397 /* -----------------------------------------------------------------------------
399 -------------------------------------------------------------------------- */
401 #if defined(USE_SPLIT_MARKERS)
402 #if defined(LEADING_UNDERSCORE)
403 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
405 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
408 #define __STG_SPLIT_MARKER /* nothing */
411 /* -----------------------------------------------------------------------------
412 Write-combining store
413 -------------------------------------------------------------------------- */
416 wcStore (StgPtr p, StgWord w)
418 #ifdef x86_64_HOST_ARCH
429 /* -----------------------------------------------------------------------------
430 Integer multiply with overflow
431 -------------------------------------------------------------------------- */
433 /* Multiply with overflow checking.
435 * This is tricky - the usual sign rules for add/subtract don't apply.
437 * On 32-bit machines we use gcc's 'long long' types, finding
438 * overflow with some careful bit-twiddling.
440 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
441 * we use a crude approximation, testing whether either operand is
442 * larger than 32-bits; if neither is, then we go ahead with the
445 * Return non-zero if there is any possibility that the signed multiply
446 * of a and b might overflow. Return zero only if you are absolutely sure
447 * that it won't overflow. If in doubt, return non-zero.
450 #if SIZEOF_VOID_P == 4
452 #ifdef WORDS_BIGENDIAN
453 #define RTS_CARRY_IDX__ 0
454 #define RTS_REM_IDX__ 1
456 #define RTS_CARRY_IDX__ 1
457 #define RTS_REM_IDX__ 0
465 #define mulIntMayOflo(a,b) \
469 z.l = (StgInt64)a * (StgInt64)b; \
470 r = z.i[RTS_REM_IDX__]; \
471 c = z.i[RTS_CARRY_IDX__]; \
472 if (c == 0 || c == -1) { \
473 c = ((StgWord)((a^b) ^ r)) \
474 >> (BITS_IN (I_) - 1); \
479 /* Careful: the carry calculation above is extremely delicate. Make sure
480 * you test it thoroughly after changing it.
485 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
487 /* If we have n-bit words then we have n-1 bits after accounting for the
488 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
489 #define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
490 #define HALF_NEG_INT (-HALF_POS_INT)
492 #define mulIntMayOflo(a,b) \
495 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
496 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\