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"
158 #include "TickyCounters.h"
163 * This is included later for RTS sources, after definitions of
164 * StgInfoTable, StgClosure and so on.
166 #include "StgMiscClosures.h"
169 #include "SMP.h" // write_barrier() inline is required
171 /* -----------------------------------------------------------------------------
172 Moving Floats and Doubles
174 ASSIGN_FLT is for assigning a float to memory (usually the
175 stack/heap). The memory address is guaranteed to be
176 StgWord aligned (currently == sizeof(void *)).
178 PK_FLT is for pulling a float out of memory. The memory is
179 guaranteed to be StgWord aligned.
180 -------------------------------------------------------------------------- */
182 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
183 INLINE_HEADER StgFloat PK_FLT (W_ []);
185 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
187 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
188 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
190 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
192 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
199 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
206 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
208 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
210 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
211 INLINE_HEADER StgDouble PK_DBL (W_ []);
213 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
214 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
216 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
218 /* Sparc uses two floating point registers to hold a double. We can
219 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
220 * independently - unfortunately this code isn't writable in C, we
221 * have to use inline assembler.
225 #define ASSIGN_DBL(dst0,src) \
226 { StgPtr dst = (StgPtr)(dst0); \
227 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
228 "=m" (((P_)(dst))[1]) : "f" (src)); \
231 #define PK_DBL(src0) \
232 ( { StgPtr src = (StgPtr)(src0); \
234 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
235 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
238 #else /* ! sparc_HOST_ARCH */
240 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
241 INLINE_HEADER StgDouble PK_DBL (W_ []);
253 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
257 p_dest[0] = y.du.dhi;
258 p_dest[1] = y.du.dlo;
261 /* GCC also works with this version, but it generates
262 the same code as the previous one, and is not ANSI
264 #define ASSIGN_DBL( p_dest, src ) \
265 *p_dest = ((double_thing) src).du.dhi; \
266 *(p_dest+1) = ((double_thing) src).du.dlo \
269 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
277 #endif /* ! sparc_HOST_ARCH */
279 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
282 /* -----------------------------------------------------------------------------
283 Moving 64-bit quantities around
285 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
286 PK_Word64 load an StgWord64/StgInt64 from a amemory location
288 In both cases the memory location might not be 64-bit aligned.
289 -------------------------------------------------------------------------- */
291 #ifdef SUPPORT_LONG_LONGS
296 } unpacked_double_word;
300 unpacked_double_word iu;
305 unpacked_double_word wu;
308 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
312 p_dest[0] = y.wu.dhi;
313 p_dest[1] = y.wu.dlo;
316 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
324 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
328 p_dest[0] = y.iu.dhi;
329 p_dest[1] = y.iu.dlo;
332 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
340 #elif SIZEOF_VOID_P == 8
342 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
347 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
352 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
357 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
364 /* -----------------------------------------------------------------------------
366 -------------------------------------------------------------------------- */
368 #if defined(USE_SPLIT_MARKERS)
369 #if defined(LEADING_UNDERSCORE)
370 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
372 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
375 #define __STG_SPLIT_MARKER /* nothing */
378 /* -----------------------------------------------------------------------------
379 Write-combining store
380 -------------------------------------------------------------------------- */
383 wcStore (StgPtr p, StgWord w)
385 #ifdef x86_64_HOST_ARCH
396 /* -----------------------------------------------------------------------------
397 Integer multiply with overflow
398 -------------------------------------------------------------------------- */
400 /* Multiply with overflow checking.
402 * This is tricky - the usual sign rules for add/subtract don't apply.
404 * On 32-bit machines we use gcc's 'long long' types, finding
405 * overflow with some careful bit-twiddling.
407 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
408 * we use a crude approximation, testing whether either operand is
409 * larger than 32-bits; if neither is, then we go ahead with the
412 * Return non-zero if there is any possibility that the signed multiply
413 * of a and b might overflow. Return zero only if you are absolutely sure
414 * that it won't overflow. If in doubt, return non-zero.
417 #if SIZEOF_VOID_P == 4
419 #ifdef WORDS_BIGENDIAN
420 #define RTS_CARRY_IDX__ 0
421 #define RTS_REM_IDX__ 1
423 #define RTS_CARRY_IDX__ 1
424 #define RTS_REM_IDX__ 0
432 #define mulIntMayOflo(a,b) \
436 z.l = (StgInt64)a * (StgInt64)b; \
437 r = z.i[RTS_REM_IDX__]; \
438 c = z.i[RTS_CARRY_IDX__]; \
439 if (c == 0 || c == -1) { \
440 c = ((StgWord)((a^b) ^ r)) \
441 >> (BITS_IN (I_) - 1); \
446 /* Careful: the carry calculation above is extremely delicate. Make sure
447 * you test it thoroughly after changing it.
452 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
454 /* If we have n-bit words then we have n-1 bits after accounting for the
455 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
456 #define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
457 #define HALF_NEG_INT (-HALF_POS_INT)
459 #define mulIntMayOflo(a,b) \
462 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
463 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\