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
37 # define NO_GLOBAL_REG_DECLS /* don't define fixed registers */
41 #include "ghcconfig.h"
42 #include "RtsConfig.h"
44 /* -----------------------------------------------------------------------------
46 -------------------------------------------------------------------------- */
49 * The C backend like to refer to labels by just mentioning their
50 * names. Howevver, when a symbol is declared as a variable in C, the
51 * C compiler will implicitly dereference it when it occurs in source.
52 * So we must subvert this behaviour for .hc files by declaring
53 * variables as arrays, which eliminates the implicit dereference.
56 #define RTS_VAR(x) (x)[]
57 #define RTS_DEREF(x) (*(x))
60 #define RTS_DEREF(x) x
65 #define BITS_PER_BYTE 8
66 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
69 * 'Portable' inlining:
70 * INLINE_HEADER is for inline functions in header files
71 * STATIC_INLINE is for inline functions in source files
73 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
74 # define INLINE_HEADER static inline
75 # define INLINE_ME inline
76 # define STATIC_INLINE INLINE_HEADER
77 #elif defined(_MSC_VER)
78 # define INLINE_HEADER __inline static
79 # define INLINE_ME __inline
80 # define STATIC_INLINE INLINE_HEADER
82 # error "Don't know how to inline functions with your C compiler."
89 #define GNU_ATTRIBUTE(at) __attribute__((at))
91 #define GNU_ATTRIBUTE(at)
95 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
97 #define GNUC3_ATTRIBUTE(at)
100 #define STG_UNUSED GNUC3_ATTRIBUTE(__unused__)
102 /* -----------------------------------------------------------------------------
103 Global type definitions
104 -------------------------------------------------------------------------- */
106 #include "MachDeps.h"
107 #include "StgTypes.h"
109 /* -----------------------------------------------------------------------------
111 -------------------------------------------------------------------------- */
119 typedef const StgWord* D_;
120 typedef StgFunPtr F_;
121 typedef StgByteArray B_;
122 typedef StgClosurePtr L_;
124 typedef StgInt64 LI_;
125 typedef StgWord64 LW_;
127 #define IF_(f) static F_ GNUC3_ATTRIBUTE(used) f(void)
128 #define FN_(f) F_ f(void)
129 #define EF_(f) extern F_ f(void)
131 typedef StgWord StgWordArray[];
132 #define EI_(X) extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
133 #define II_(X) static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
135 /* -----------------------------------------------------------------------------
138 This needs to be up near the top as the register line on alpha needs
139 to be before all procedures (inline & out-of-line).
140 -------------------------------------------------------------------------- */
142 #include "TailCalls.h"
144 /* -----------------------------------------------------------------------------
146 -------------------------------------------------------------------------- */
149 #include "MachRegs.h"
151 #include "StgProf.h" /* ToDo: separate out RTS-only stuff from here */
155 * This is included later for RTS sources, after definitions of
156 * StgInfoTable, StgClosure and so on.
158 #include "StgMiscClosures.h"
161 /* RTS external interface */
162 #include "RtsExternal.h"
164 /* -----------------------------------------------------------------------------
165 Moving Floats and Doubles
167 ASSIGN_FLT is for assigning a float to memory (usually the
168 stack/heap). The memory address is guaranteed to be
169 StgWord aligned (currently == sizeof(void *)).
171 PK_FLT is for pulling a float out of memory. The memory is
172 guaranteed to be StgWord aligned.
173 -------------------------------------------------------------------------- */
175 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
176 INLINE_HEADER StgFloat PK_FLT (W_ []);
178 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
180 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
181 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
183 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
185 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
192 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
199 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
201 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
203 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
204 INLINE_HEADER StgDouble PK_DBL (W_ []);
206 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
207 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
209 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
211 /* Sparc uses two floating point registers to hold a double. We can
212 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
213 * independently - unfortunately this code isn't writable in C, we
214 * have to use inline assembler.
218 #define ASSIGN_DBL(dst0,src) \
219 { StgPtr dst = (StgPtr)(dst0); \
220 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
221 "=m" (((P_)(dst))[1]) : "f" (src)); \
224 #define PK_DBL(src0) \
225 ( { StgPtr src = (StgPtr)(src0); \
227 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
228 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
231 #else /* ! sparc_HOST_ARCH */
233 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
234 INLINE_HEADER StgDouble PK_DBL (W_ []);
246 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
250 p_dest[0] = y.du.dhi;
251 p_dest[1] = y.du.dlo;
254 /* GCC also works with this version, but it generates
255 the same code as the previous one, and is not ANSI
257 #define ASSIGN_DBL( p_dest, src ) \
258 *p_dest = ((double_thing) src).du.dhi; \
259 *(p_dest+1) = ((double_thing) src).du.dlo \
262 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
270 #endif /* ! sparc_HOST_ARCH */
272 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
275 /* -----------------------------------------------------------------------------
276 Moving 64-bit quantities around
278 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
279 PK_Word64 load an StgWord64/StgInt64 from a amemory location
281 In both cases the memory location might not be 64-bit aligned.
282 -------------------------------------------------------------------------- */
284 #ifdef SUPPORT_LONG_LONGS
289 } unpacked_double_word;
293 unpacked_double_word iu;
298 unpacked_double_word wu;
301 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
305 p_dest[0] = y.wu.dhi;
306 p_dest[1] = y.wu.dlo;
309 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
317 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
321 p_dest[0] = y.iu.dhi;
322 p_dest[1] = y.iu.dlo;
325 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
333 #elif SIZEOF_VOID_P == 8
335 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
340 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
345 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
350 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
357 /* -----------------------------------------------------------------------------
359 -------------------------------------------------------------------------- */
361 #if defined(USE_SPLIT_MARKERS)
362 #if defined(LEADING_UNDERSCORE)
363 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
365 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
368 #define __STG_SPLIT_MARKER /* nothing */
371 /* -----------------------------------------------------------------------------
372 Write-combining store
373 -------------------------------------------------------------------------- */
376 wcStore (StgPtr p, StgWord w)
378 #ifdef x86_64_HOST_ARCH
389 /* -----------------------------------------------------------------------------
390 Integer multiply with overflow
391 -------------------------------------------------------------------------- */
393 /* Multiply with overflow checking.
395 * This is tricky - the usual sign rules for add/subtract don't apply.
397 * On 32-bit machines we use gcc's 'long long' types, finding
398 * overflow with some careful bit-twiddling.
400 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
401 * we use a crude approximation, testing whether either operand is
402 * larger than 32-bits; if neither is, then we go ahead with the
405 * Return non-zero if there is any possibility that the signed multiply
406 * of a and b might overflow. Return zero only if you are absolutely sure
407 * that it won't overflow. If in doubt, return non-zero.
410 #if SIZEOF_VOID_P == 4
412 #ifdef WORDS_BIGENDIAN
413 #define RTS_CARRY_IDX__ 0
414 #define RTS_REM_IDX__ 1
416 #define RTS_CARRY_IDX__ 1
417 #define RTS_REM_IDX__ 0
425 #define mulIntMayOflo(a,b) \
429 z.l = (StgInt64)a * (StgInt64)b; \
430 r = z.i[RTS_REM_IDX__]; \
431 c = z.i[RTS_CARRY_IDX__]; \
432 if (c == 0 || c == -1) { \
433 c = ((StgWord)((a^b) ^ r)) \
434 >> (BITS_IN (I_) - 1); \
439 /* Careful: the carry calculation above is extremely delicate. Make sure
440 * you test it thoroughly after changing it.
445 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
447 /* If we have n-bit words then we have n-1 bits after accounting for the
448 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
449 #define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
450 #define HALF_NEG_INT (-HALF_POS_INT)
452 #define mulIntMayOflo(a,b) \
455 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
456 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\