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
79 * STATIC_INLINE is for inline functions in source files
81 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
82 # define INLINE_HEADER static inline
83 # define INLINE_ME inline
84 # define STATIC_INLINE INLINE_HEADER
85 #elif defined(_MSC_VER)
86 # define INLINE_HEADER __inline static
87 # define INLINE_ME __inline
88 # define STATIC_INLINE INLINE_HEADER
90 # error "Don't know how to inline functions with your C compiler."
97 #define GNU_ATTRIBUTE(at) __attribute__((at))
99 #define GNU_ATTRIBUTE(at)
103 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
105 #define GNUC3_ATTRIBUTE(at)
108 #define STG_UNUSED GNUC3_ATTRIBUTE(__unused__)
110 /* -----------------------------------------------------------------------------
111 Global type definitions
112 -------------------------------------------------------------------------- */
114 #include "MachDeps.h"
115 #include "StgTypes.h"
117 /* -----------------------------------------------------------------------------
119 -------------------------------------------------------------------------- */
127 typedef const StgWord* D_;
128 typedef StgFunPtr F_;
129 typedef StgByteArray B_;
130 typedef StgClosurePtr L_;
132 typedef StgInt64 LI_;
133 typedef StgWord64 LW_;
135 #define IF_(f) static F_ GNUC3_ATTRIBUTE(used) f(void)
136 #define FN_(f) F_ f(void)
137 #define EF_(f) extern F_ f(void)
139 typedef StgWord StgWordArray[];
140 #define EI_(X) extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
141 #define II_(X) static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
143 /* -----------------------------------------------------------------------------
146 This needs to be up near the top as the register line on alpha needs
147 to be before all procedures (inline & out-of-line).
148 -------------------------------------------------------------------------- */
150 #include "TailCalls.h"
152 /* -----------------------------------------------------------------------------
154 -------------------------------------------------------------------------- */
157 #include "MachRegs.h"
161 #include "TickyCounters.h"
166 * This is included later for RTS sources, after definitions of
167 * StgInfoTable, StgClosure and so on.
169 #include "StgMiscClosures.h"
172 #include "SMP.h" // write_barrier() inline is required
174 /* -----------------------------------------------------------------------------
175 Moving Floats and Doubles
177 ASSIGN_FLT is for assigning a float to memory (usually the
178 stack/heap). The memory address is guaranteed to be
179 StgWord aligned (currently == sizeof(void *)).
181 PK_FLT is for pulling a float out of memory. The memory is
182 guaranteed to be StgWord aligned.
183 -------------------------------------------------------------------------- */
185 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
186 INLINE_HEADER StgFloat PK_FLT (W_ []);
188 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
190 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
191 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
193 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
195 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
202 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
209 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
211 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
213 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
214 INLINE_HEADER StgDouble PK_DBL (W_ []);
216 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
217 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
219 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
221 /* Sparc uses two floating point registers to hold a double. We can
222 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
223 * independently - unfortunately this code isn't writable in C, we
224 * have to use inline assembler.
228 #define ASSIGN_DBL(dst0,src) \
229 { StgPtr dst = (StgPtr)(dst0); \
230 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
231 "=m" (((P_)(dst))[1]) : "f" (src)); \
234 #define PK_DBL(src0) \
235 ( { StgPtr src = (StgPtr)(src0); \
237 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
238 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
241 #else /* ! sparc_HOST_ARCH */
243 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
244 INLINE_HEADER StgDouble PK_DBL (W_ []);
256 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
260 p_dest[0] = y.du.dhi;
261 p_dest[1] = y.du.dlo;
264 /* GCC also works with this version, but it generates
265 the same code as the previous one, and is not ANSI
267 #define ASSIGN_DBL( p_dest, src ) \
268 *p_dest = ((double_thing) src).du.dhi; \
269 *(p_dest+1) = ((double_thing) src).du.dlo \
272 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
280 #endif /* ! sparc_HOST_ARCH */
282 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
285 /* -----------------------------------------------------------------------------
286 Moving 64-bit quantities around
288 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
289 PK_Word64 load an StgWord64/StgInt64 from a amemory location
291 In both cases the memory location might not be 64-bit aligned.
292 -------------------------------------------------------------------------- */
294 #ifdef SUPPORT_LONG_LONGS
299 } unpacked_double_word;
303 unpacked_double_word iu;
308 unpacked_double_word wu;
311 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
315 p_dest[0] = y.wu.dhi;
316 p_dest[1] = y.wu.dlo;
319 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
327 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
331 p_dest[0] = y.iu.dhi;
332 p_dest[1] = y.iu.dlo;
335 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
343 #elif SIZEOF_VOID_P == 8
345 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
350 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
355 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
360 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
367 /* -----------------------------------------------------------------------------
369 -------------------------------------------------------------------------- */
371 #if defined(USE_SPLIT_MARKERS)
372 #if defined(LEADING_UNDERSCORE)
373 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
375 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
378 #define __STG_SPLIT_MARKER /* nothing */
381 /* -----------------------------------------------------------------------------
382 Write-combining store
383 -------------------------------------------------------------------------- */
386 wcStore (StgPtr p, StgWord w)
388 #ifdef x86_64_HOST_ARCH
399 /* -----------------------------------------------------------------------------
400 Integer multiply with overflow
401 -------------------------------------------------------------------------- */
403 /* Multiply with overflow checking.
405 * This is tricky - the usual sign rules for add/subtract don't apply.
407 * On 32-bit machines we use gcc's 'long long' types, finding
408 * overflow with some careful bit-twiddling.
410 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
411 * we use a crude approximation, testing whether either operand is
412 * larger than 32-bits; if neither is, then we go ahead with the
415 * Return non-zero if there is any possibility that the signed multiply
416 * of a and b might overflow. Return zero only if you are absolutely sure
417 * that it won't overflow. If in doubt, return non-zero.
420 #if SIZEOF_VOID_P == 4
422 #ifdef WORDS_BIGENDIAN
423 #define RTS_CARRY_IDX__ 0
424 #define RTS_REM_IDX__ 1
426 #define RTS_CARRY_IDX__ 1
427 #define RTS_REM_IDX__ 0
435 #define mulIntMayOflo(a,b) \
439 z.l = (StgInt64)a * (StgInt64)b; \
440 r = z.i[RTS_REM_IDX__]; \
441 c = z.i[RTS_CARRY_IDX__]; \
442 if (c == 0 || c == -1) { \
443 c = ((StgWord)((a^b) ^ r)) \
444 >> (BITS_IN (I_) - 1); \
449 /* Careful: the carry calculation above is extremely delicate. Make sure
450 * you test it thoroughly after changing it.
455 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
457 /* If we have n-bit words then we have n-1 bits after accounting for the
458 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
459 #define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
460 #define HALF_NEG_INT (-HALF_POS_INT)
462 #define mulIntMayOflo(a,b) \
465 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
466 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\