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 /* The code generator calls the math functions directly in .hc code.
45 NB. after configuration stuff above, because this sets #defines
46 that depend on config info, such as __USE_FILE_OFFSET64 */
49 /* -----------------------------------------------------------------------------
51 -------------------------------------------------------------------------- */
54 * The C backend like to refer to labels by just mentioning their
55 * names. Howevver, when a symbol is declared as a variable in C, the
56 * C compiler will implicitly dereference it when it occurs in source.
57 * So we must subvert this behaviour for .hc files by declaring
58 * variables as arrays, which eliminates the implicit dereference.
61 #define RTS_VAR(x) (x)[]
62 #define RTS_DEREF(x) (*(x))
65 #define RTS_DEREF(x) x
70 #define BITS_PER_BYTE 8
71 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
74 * 'Portable' inlining:
75 * INLINE_HEADER is for inline functions in header files
76 * STATIC_INLINE is for inline functions in source files
78 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
79 # define INLINE_HEADER static inline
80 # define INLINE_ME inline
81 # define STATIC_INLINE INLINE_HEADER
82 #elif defined(_MSC_VER)
83 # define INLINE_HEADER __inline static
84 # define INLINE_ME __inline
85 # define STATIC_INLINE INLINE_HEADER
87 # error "Don't know how to inline functions with your C compiler."
94 #define GNU_ATTRIBUTE(at) __attribute__((at))
96 #define GNU_ATTRIBUTE(at)
100 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
102 #define GNUC3_ATTRIBUTE(at)
105 #define STG_UNUSED GNUC3_ATTRIBUTE(__unused__)
107 /* -----------------------------------------------------------------------------
108 Global type definitions
109 -------------------------------------------------------------------------- */
111 #include "MachDeps.h"
112 #include "StgTypes.h"
114 /* -----------------------------------------------------------------------------
116 -------------------------------------------------------------------------- */
124 typedef const StgWord* D_;
125 typedef StgFunPtr F_;
126 typedef StgByteArray B_;
127 typedef StgClosurePtr L_;
129 typedef StgInt64 LI_;
130 typedef StgWord64 LW_;
132 #define IF_(f) static F_ GNUC3_ATTRIBUTE(used) f(void)
133 #define FN_(f) F_ f(void)
134 #define EF_(f) extern F_ f(void)
136 typedef StgWord StgWordArray[];
137 #define EI_(X) extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
138 #define II_(X) static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
140 /* -----------------------------------------------------------------------------
143 This needs to be up near the top as the register line on alpha needs
144 to be before all procedures (inline & out-of-line).
145 -------------------------------------------------------------------------- */
147 #include "TailCalls.h"
149 /* -----------------------------------------------------------------------------
151 -------------------------------------------------------------------------- */
154 #include "MachRegs.h"
159 * This is included later for RTS sources, after definitions of
160 * StgInfoTable, StgClosure and so on.
162 #include "StgMiscClosures.h"
165 #include "SMP.h" // write_barrier() inline is required
167 /* -----------------------------------------------------------------------------
168 Moving Floats and Doubles
170 ASSIGN_FLT is for assigning a float to memory (usually the
171 stack/heap). The memory address is guaranteed to be
172 StgWord aligned (currently == sizeof(void *)).
174 PK_FLT is for pulling a float out of memory. The memory is
175 guaranteed to be StgWord aligned.
176 -------------------------------------------------------------------------- */
178 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
179 INLINE_HEADER StgFloat PK_FLT (W_ []);
181 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
183 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
184 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
186 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
188 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
195 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
202 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
204 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
206 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
207 INLINE_HEADER StgDouble PK_DBL (W_ []);
209 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
210 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
212 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
214 /* Sparc uses two floating point registers to hold a double. We can
215 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
216 * independently - unfortunately this code isn't writable in C, we
217 * have to use inline assembler.
221 #define ASSIGN_DBL(dst0,src) \
222 { StgPtr dst = (StgPtr)(dst0); \
223 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
224 "=m" (((P_)(dst))[1]) : "f" (src)); \
227 #define PK_DBL(src0) \
228 ( { StgPtr src = (StgPtr)(src0); \
230 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
231 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
234 #else /* ! sparc_HOST_ARCH */
236 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
237 INLINE_HEADER StgDouble PK_DBL (W_ []);
249 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
253 p_dest[0] = y.du.dhi;
254 p_dest[1] = y.du.dlo;
257 /* GCC also works with this version, but it generates
258 the same code as the previous one, and is not ANSI
260 #define ASSIGN_DBL( p_dest, src ) \
261 *p_dest = ((double_thing) src).du.dhi; \
262 *(p_dest+1) = ((double_thing) src).du.dlo \
265 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
273 #endif /* ! sparc_HOST_ARCH */
275 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
278 /* -----------------------------------------------------------------------------
279 Moving 64-bit quantities around
281 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
282 PK_Word64 load an StgWord64/StgInt64 from a amemory location
284 In both cases the memory location might not be 64-bit aligned.
285 -------------------------------------------------------------------------- */
287 #ifdef SUPPORT_LONG_LONGS
292 } unpacked_double_word;
296 unpacked_double_word iu;
301 unpacked_double_word wu;
304 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
308 p_dest[0] = y.wu.dhi;
309 p_dest[1] = y.wu.dlo;
312 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
320 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
324 p_dest[0] = y.iu.dhi;
325 p_dest[1] = y.iu.dlo;
328 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
336 #elif SIZEOF_VOID_P == 8
338 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
343 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
348 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
353 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
360 /* -----------------------------------------------------------------------------
362 -------------------------------------------------------------------------- */
364 #if defined(USE_SPLIT_MARKERS)
365 #if defined(LEADING_UNDERSCORE)
366 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
368 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
371 #define __STG_SPLIT_MARKER /* nothing */
374 /* -----------------------------------------------------------------------------
375 Write-combining store
376 -------------------------------------------------------------------------- */
379 wcStore (StgPtr p, StgWord w)
381 #ifdef x86_64_HOST_ARCH
392 /* -----------------------------------------------------------------------------
393 Integer multiply with overflow
394 -------------------------------------------------------------------------- */
396 /* Multiply with overflow checking.
398 * This is tricky - the usual sign rules for add/subtract don't apply.
400 * On 32-bit machines we use gcc's 'long long' types, finding
401 * overflow with some careful bit-twiddling.
403 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
404 * we use a crude approximation, testing whether either operand is
405 * larger than 32-bits; if neither is, then we go ahead with the
408 * Return non-zero if there is any possibility that the signed multiply
409 * of a and b might overflow. Return zero only if you are absolutely sure
410 * that it won't overflow. If in doubt, return non-zero.
413 #if SIZEOF_VOID_P == 4
415 #ifdef WORDS_BIGENDIAN
416 #define RTS_CARRY_IDX__ 0
417 #define RTS_REM_IDX__ 1
419 #define RTS_CARRY_IDX__ 1
420 #define RTS_REM_IDX__ 0
428 #define mulIntMayOflo(a,b) \
432 z.l = (StgInt64)a * (StgInt64)b; \
433 r = z.i[RTS_REM_IDX__]; \
434 c = z.i[RTS_CARRY_IDX__]; \
435 if (c == 0 || c == -1) { \
436 c = ((StgWord)((a^b) ^ r)) \
437 >> (BITS_IN (I_) - 1); \
442 /* Careful: the carry calculation above is extremely delicate. Make sure
443 * you test it thoroughly after changing it.
448 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
450 /* If we have n-bit words then we have n-1 bits after accounting for the
451 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
452 #define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
453 #define HALF_NEG_INT (-HALF_POS_INT)
455 #define mulIntMayOflo(a,b) \
458 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
459 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\