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
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)
37 # define _ISOC99_SOURCE
39 // Turning on _ISOC99_SOURCE means S_ISSOCK gets defined on Linux
44 # define NO_GLOBAL_REG_DECLS /* don't define fixed registers */
48 #include "ghcconfig.h"
49 #include "RtsConfig.h"
51 /* The code generator calls the math functions directly in .hc code.
52 NB. after configuration stuff above, because this sets #defines
53 that depend on config info, such as __USE_FILE_OFFSET64 */
56 /* -----------------------------------------------------------------------------
58 -------------------------------------------------------------------------- */
61 * The C backend like to refer to labels by just mentioning their
62 * names. Howevver, when a symbol is declared as a variable in C, the
63 * C compiler will implicitly dereference it when it occurs in source.
64 * So we must subvert this behaviour for .hc files by declaring
65 * variables as arrays, which eliminates the implicit dereference.
68 #define RTS_VAR(x) (x)[]
69 #define RTS_DEREF(x) (*(x))
72 #define RTS_DEREF(x) x
77 #define BITS_PER_BYTE 8
78 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
81 * 'Portable' inlining:
82 * INLINE_HEADER is for inline functions in header files (macros)
83 * STATIC_INLINE is for inline functions in source files
84 * EXTERN_INLINE is for functions that we want to inline sometimes
85 * (we also compile a static version of the function; see Inlines.c)
87 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
89 # define INLINE_HEADER static inline
90 # define INLINE_ME inline
91 # define STATIC_INLINE INLINE_HEADER
93 // The special "extern inline" behaviour is now only supported by gcc
94 // when _GNUC_GNU_INLINE__ is defined, and you have to use
95 // __attribute__((gnu_inline)). So when we don't have this, we use
96 // ordinary static inline.
98 #if defined(__GNUC_GNU_INLINE__)
99 # if defined(KEEP_INLINES)
100 # define EXTERN_INLINE inline
102 # define EXTERN_INLINE extern inline __attribute__((gnu_inline))
105 # if defined(KEEP_INLINES)
106 # define EXTERN_INLINE
108 # define EXTERN_INLINE INLINE_HEADER
112 #elif defined(_MSC_VER)
114 # define INLINE_HEADER __inline static
115 # define INLINE_ME __inline
116 # define STATIC_INLINE INLINE_HEADER
118 # if defined(KEEP_INLINES)
119 # define EXTERN_INLINE __inline
121 # define EXTERN_INLINE __inline extern
126 # error "Don't know how to inline functions with your C compiler."
134 #if defined(__GNUC__)
135 #define GNU_ATTRIBUTE(at) __attribute__((at))
137 #define GNU_ATTRIBUTE(at)
141 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
143 #define GNUC3_ATTRIBUTE(at)
146 #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3
147 #define GNUC_ATTR_HOT __attribute__((hot))
149 #define GNUC_ATTR_HOT /* nothing */
152 #define STG_UNUSED GNUC3_ATTRIBUTE(__unused__)
154 /* -----------------------------------------------------------------------------
155 Global type definitions
156 -------------------------------------------------------------------------- */
158 #include "MachDeps.h"
159 #include "StgTypes.h"
161 /* -----------------------------------------------------------------------------
163 -------------------------------------------------------------------------- */
171 typedef const StgWord* D_;
172 typedef StgFunPtr F_;
173 typedef StgByteArray B_;
174 typedef StgClosurePtr L_;
176 typedef StgInt64 LI_;
177 typedef StgWord64 LW_;
179 #define IF_(f) static F_ GNUC3_ATTRIBUTE(used) f(void)
180 #define FN_(f) F_ f(void)
181 #define EF_(f) extern F_ f(void)
183 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))
187 /* -----------------------------------------------------------------------------
190 This needs to be up near the top as the register line on alpha needs
191 to be before all procedures (inline & out-of-line).
192 -------------------------------------------------------------------------- */
194 #include "TailCalls.h"
196 /* -----------------------------------------------------------------------------
198 -------------------------------------------------------------------------- */
201 #include "MachRegs.h"
205 #include "TickyCounters.h"
210 * This is included later for RTS sources, after definitions of
211 * StgInfoTable, StgClosure and so on.
213 #include "StgMiscClosures.h"
216 #include "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 #ifdef SUPPORT_LONG_LONGS
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[])
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) {\