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 are compiling a .hc file, then we want all the register
28 * variables. This is the what happens if you #include "Stg.h" first:
29 * we assume this is a .hc file, and set IN_STG_CODE==1, which later
30 * causes the register variables to be enabled in stg/Regs.h.
32 * If instead "Rts.h" is included first, then we are compiling a
33 * vanilla C file. Everything from Stg.h is provided, except that
34 * IN_STG_CODE is not defined, and the register variables will not be
38 # define IN_STG_CODE 1
40 // Turn on C99 for .hc code. This gives us the INFINITY and NAN
41 // constants from math.h, which we occasionally need to use in .hc (#1861)
42 # define _ISOC99_SOURCE
44 // We need _BSD_SOURCE so that math.h defines things like gamma
50 # define NO_GLOBAL_REG_DECLS /* don't define fixed registers */
54 #include "ghcconfig.h"
56 /* The code generator calls the math functions directly in .hc code.
57 NB. after configuration stuff above, because this sets #defines
58 that depend on config info, such as __USE_FILE_OFFSET64 */
61 /* -----------------------------------------------------------------------------
63 -------------------------------------------------------------------------- */
66 * The C backend likes to refer to labels by just mentioning their
67 * names. Howevver, when a symbol is declared as a variable in C, the
68 * C compiler will implicitly dereference it when it occurs in source.
69 * So we must subvert this behaviour for .hc files by declaring
70 * variables as arrays, which eliminates the implicit dereference.
73 #define RTS_VAR(x) (x)[]
74 #define RTS_DEREF(x) (*(x))
77 #define RTS_DEREF(x) x
82 #define BITS_PER_BYTE 8
83 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
85 /* Compute offsets of struct fields
87 #define STG_FIELD_OFFSET(s_type, field) ((StgWord)&(((s_type*)0)->field))
90 * 'Portable' inlining:
91 * INLINE_HEADER is for inline functions in header files (macros)
92 * STATIC_INLINE is for inline functions in source files
93 * EXTERN_INLINE is for functions that we want to inline sometimes
94 * (we also compile a static version of the function; see Inlines.c)
96 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
98 # define INLINE_HEADER static inline
99 # define INLINE_ME inline
100 # define STATIC_INLINE INLINE_HEADER
102 // The special "extern inline" behaviour is now only supported by gcc
103 // when _GNUC_GNU_INLINE__ is defined, and you have to use
104 // __attribute__((gnu_inline)). So when we don't have this, we use
105 // ordinary static inline.
107 // Apple's gcc defines __GNUC_GNU_INLINE__ without providing
108 // gnu_inline, so we exclude MacOS X and fall through to the safe
111 #if defined(__GNUC_GNU_INLINE__) && !defined(__APPLE__)
112 # if defined(KEEP_INLINES)
113 # define EXTERN_INLINE inline
115 # define EXTERN_INLINE extern inline __attribute__((gnu_inline))
118 # if defined(KEEP_INLINES)
119 # define EXTERN_INLINE
121 # define EXTERN_INLINE INLINE_HEADER
125 #elif defined(_MSC_VER)
127 # define INLINE_HEADER __inline static
128 # define INLINE_ME __inline
129 # define STATIC_INLINE INLINE_HEADER
131 # if defined(KEEP_INLINES)
132 # define EXTERN_INLINE __inline
134 # define EXTERN_INLINE __inline extern
139 # error "Don't know how to inline functions with your C compiler."
147 #if defined(__GNUC__)
148 #define GNU_ATTRIBUTE(at) __attribute__((at))
150 #define GNU_ATTRIBUTE(at)
154 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
156 #define GNUC3_ATTRIBUTE(at)
159 #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3
160 #define GNUC_ATTR_HOT __attribute__((hot))
162 #define GNUC_ATTR_HOT /* nothing */
165 #define STG_UNUSED GNUC3_ATTRIBUTE(__unused__)
167 /* -----------------------------------------------------------------------------
168 Global type definitions
169 -------------------------------------------------------------------------- */
171 #include "MachDeps.h"
172 #include "stg/Types.h"
174 /* -----------------------------------------------------------------------------
176 -------------------------------------------------------------------------- */
182 typedef StgWord StgWordArray[];
184 #define EI_(X) extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
185 #define II_(X) static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
186 #define IF_(f) static StgFunPtr GNUC3_ATTRIBUTE(used) f(void)
187 #define FN_(f) StgFunPtr f(void)
188 #define EF_(f) extern StgFunPtr f(void)
190 /* -----------------------------------------------------------------------------
193 This needs to be up near the top as the register line on alpha needs
194 to be before all procedures (inline & out-of-line).
195 -------------------------------------------------------------------------- */
197 #include "stg/TailCalls.h"
199 /* -----------------------------------------------------------------------------
201 -------------------------------------------------------------------------- */
204 #include "stg/MachRegs.h"
205 #include "stg/Regs.h"
206 #include "stg/Ticky.h"
210 * This is included later for RTS sources, after definitions of
211 * StgInfoTable, StgClosure and so on.
213 #include "stg/MiscClosures.h"
216 #include "stg/SMP.h" // write_barrier() inline is required
218 /* -----------------------------------------------------------------------------
219 Moving Floats and Doubles
221 ASSIGN_FLT is for assigning a float to memory (usually the
222 stack/heap). The memory address is guaranteed to be
223 StgWord aligned (currently == sizeof(void *)).
225 PK_FLT is for pulling a float out of memory. The memory is
226 guaranteed to be StgWord aligned.
227 -------------------------------------------------------------------------- */
229 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
230 INLINE_HEADER StgFloat PK_FLT (W_ []);
232 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
234 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
235 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
237 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
239 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
246 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
253 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
255 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
257 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
258 INLINE_HEADER StgDouble PK_DBL (W_ []);
260 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
261 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
263 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
265 /* Sparc uses two floating point registers to hold a double. We can
266 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
267 * independently - unfortunately this code isn't writable in C, we
268 * have to use inline assembler.
272 #define ASSIGN_DBL(dst0,src) \
273 { StgPtr dst = (StgPtr)(dst0); \
274 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
275 "=m" (((P_)(dst))[1]) : "f" (src)); \
278 #define PK_DBL(src0) \
279 ( { StgPtr src = (StgPtr)(src0); \
281 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
282 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
285 #else /* ! sparc_HOST_ARCH */
287 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
288 INLINE_HEADER StgDouble PK_DBL (W_ []);
300 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
304 p_dest[0] = y.du.dhi;
305 p_dest[1] = y.du.dlo;
308 /* GCC also works with this version, but it generates
309 the same code as the previous one, and is not ANSI
311 #define ASSIGN_DBL( p_dest, src ) \
312 *p_dest = ((double_thing) src).du.dhi; \
313 *(p_dest+1) = ((double_thing) src).du.dlo \
316 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
324 #endif /* ! sparc_HOST_ARCH */
326 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
329 /* -----------------------------------------------------------------------------
330 Moving 64-bit quantities around
332 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
333 PK_Word64 load an StgWord64/StgInt64 from a amemory location
335 In both cases the memory location might not be 64-bit aligned.
336 -------------------------------------------------------------------------- */
338 #if SIZEOF_HSWORD == 4
343 } unpacked_double_word;
347 unpacked_double_word iu;
352 unpacked_double_word wu;
355 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
359 p_dest[0] = y.wu.dhi;
360 p_dest[1] = y.wu.dlo;
363 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
371 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
375 p_dest[0] = y.iu.dhi;
376 p_dest[1] = y.iu.dlo;
379 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
387 #elif SIZEOF_VOID_P == 8
389 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
394 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
399 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
404 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
409 #endif /* SIZEOF_HSWORD == 4 */
411 /* -----------------------------------------------------------------------------
413 -------------------------------------------------------------------------- */
415 #if defined(USE_SPLIT_MARKERS)
416 #if defined(LEADING_UNDERSCORE)
417 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
419 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
422 #define __STG_SPLIT_MARKER /* nothing */
425 /* -----------------------------------------------------------------------------
426 Write-combining store
427 -------------------------------------------------------------------------- */
430 wcStore (StgPtr p, StgWord w)
432 #ifdef x86_64_HOST_ARCH
443 /* -----------------------------------------------------------------------------
444 Integer multiply with overflow
445 -------------------------------------------------------------------------- */
447 /* Multiply with overflow checking.
449 * This is tricky - the usual sign rules for add/subtract don't apply.
451 * On 32-bit machines we use gcc's 'long long' types, finding
452 * overflow with some careful bit-twiddling.
454 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
455 * we use a crude approximation, testing whether either operand is
456 * larger than 32-bits; if neither is, then we go ahead with the
459 * Return non-zero if there is any possibility that the signed multiply
460 * of a and b might overflow. Return zero only if you are absolutely sure
461 * that it won't overflow. If in doubt, return non-zero.
464 #if SIZEOF_VOID_P == 4
466 #ifdef WORDS_BIGENDIAN
467 #define RTS_CARRY_IDX__ 0
468 #define RTS_REM_IDX__ 1
470 #define RTS_CARRY_IDX__ 1
471 #define RTS_REM_IDX__ 0
479 #define mulIntMayOflo(a,b) \
483 z.l = (StgInt64)a * (StgInt64)b; \
484 r = z.i[RTS_REM_IDX__]; \
485 c = z.i[RTS_CARRY_IDX__]; \
486 if (c == 0 || c == -1) { \
487 c = ((StgWord)((a^b) ^ r)) \
488 >> (BITS_IN (I_) - 1); \
493 /* Careful: the carry calculation above is extremely delicate. Make sure
494 * you test it thoroughly after changing it.
499 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
501 /* If we have n-bit words then we have n-1 bits after accounting for the
502 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
503 #define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
504 #define HALF_NEG_INT (-HALF_POS_INT)
506 #define mulIntMayOflo(a,b) \
509 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
510 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\