1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2009
5 * Top-level include file for everything required when compiling .hc
6 * code. NOTE: in .hc files, Stg.h must be included *before* any
7 * other headers, because we define some register variables which must
8 * be done before any inline functions are defined (some system
9 * headers have been known to define the odd inline function).
11 * We generally try to keep as little visible as possible when
12 * compiling .hc files. So for example the definitions of the
13 * InfoTable structs, closure structs and other RTS types are not
14 * visible here. The compiler knows enough about the representations
15 * of these types to generate code which manipulates them directly
16 * with pointer arithmetic.
18 * In ordinary C code, do not #include this file directly: #include
21 * To understand the structure of the RTS headers, see the wiki:
22 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/SourceTree/Includes
24 * ---------------------------------------------------------------------------*/
30 * If we are compiling a .hc file, then we want all the register
31 * variables. This is the what happens if you #include "Stg.h" first:
32 * we assume this is a .hc file, and set IN_STG_CODE==1, which later
33 * causes the register variables to be enabled in stg/Regs.h.
35 * If instead "Rts.h" is included first, then we are compiling a
36 * vanilla C file. Everything from Stg.h is provided, except that
37 * IN_STG_CODE is not defined, and the register variables will not be
41 # define IN_STG_CODE 1
43 // Turn on C99 for .hc code. This gives us the INFINITY and NAN
44 // constants from math.h, which we occasionally need to use in .hc (#1861)
45 # define _ISOC99_SOURCE
47 // We need _BSD_SOURCE so that math.h defines things like gamma
53 # define NO_GLOBAL_REG_DECLS /* don't define fixed registers */
57 #include "ghcconfig.h"
59 /* The code generator calls the math functions directly in .hc code.
60 NB. after configuration stuff above, because this sets #defines
61 that depend on config info, such as __USE_FILE_OFFSET64 */
64 /* -----------------------------------------------------------------------------
66 -------------------------------------------------------------------------- */
69 * The C backend likes to refer to labels by just mentioning their
70 * names. Howevver, when a symbol is declared as a variable in C, the
71 * C compiler will implicitly dereference it when it occurs in source.
72 * So we must subvert this behaviour for .hc files by declaring
73 * variables as arrays, which eliminates the implicit dereference.
76 #define RTS_VAR(x) (x)[]
77 #define RTS_DEREF(x) (*(x))
80 #define RTS_DEREF(x) x
85 #define BITS_PER_BYTE 8
86 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
88 /* Compute offsets of struct fields
90 #define STG_FIELD_OFFSET(s_type, field) ((StgWord)&(((s_type*)0)->field))
93 * 'Portable' inlining:
94 * INLINE_HEADER is for inline functions in header files (macros)
95 * STATIC_INLINE is for inline functions in source files
96 * EXTERN_INLINE is for functions that we want to inline sometimes
97 * (we also compile a static version of the function; see Inlines.c)
99 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
101 # define INLINE_HEADER static inline
102 # define INLINE_ME inline
103 # define STATIC_INLINE INLINE_HEADER
105 // The special "extern inline" behaviour is now only supported by gcc
106 // when _GNUC_GNU_INLINE__ is defined, and you have to use
107 // __attribute__((gnu_inline)). So when we don't have this, we use
108 // ordinary static inline.
110 // Apple's gcc defines __GNUC_GNU_INLINE__ without providing
111 // gnu_inline, so we exclude MacOS X and fall through to the safe
114 #if defined(__GNUC_GNU_INLINE__) && !defined(__APPLE__)
115 # if defined(KEEP_INLINES)
116 # define EXTERN_INLINE inline
118 # define EXTERN_INLINE extern inline __attribute__((gnu_inline))
121 # if defined(KEEP_INLINES)
122 # define EXTERN_INLINE
124 # define EXTERN_INLINE INLINE_HEADER
128 #elif defined(_MSC_VER)
130 # define INLINE_HEADER __inline static
131 # define INLINE_ME __inline
132 # define STATIC_INLINE INLINE_HEADER
134 # if defined(KEEP_INLINES)
135 # define EXTERN_INLINE __inline
137 # define EXTERN_INLINE __inline extern
142 # error "Don't know how to inline functions with your C compiler."
150 #if defined(__GNUC__)
151 #define GNU_ATTRIBUTE(at) __attribute__((at))
153 #define GNU_ATTRIBUTE(at)
157 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
159 #define GNUC3_ATTRIBUTE(at)
162 #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3
163 #define GNUC_ATTR_HOT __attribute__((hot))
165 #define GNUC_ATTR_HOT /* nothing */
168 #define STG_UNUSED GNUC3_ATTRIBUTE(__unused__)
170 /* -----------------------------------------------------------------------------
171 Global type definitions
172 -------------------------------------------------------------------------- */
174 #include "MachDeps.h"
175 #include "stg/Types.h"
177 /* -----------------------------------------------------------------------------
179 -------------------------------------------------------------------------- */
185 typedef StgWord StgWordArray[];
186 typedef StgFunPtr F_;
188 #define EI_(X) extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
189 #define II_(X) static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
190 #define IF_(f) static StgFunPtr GNUC3_ATTRIBUTE(used) f(void)
191 #define FN_(f) StgFunPtr f(void)
192 #define EF_(f) extern StgFunPtr f(void)
194 /* -----------------------------------------------------------------------------
197 This needs to be up near the top as the register line on alpha needs
198 to be before all procedures (inline & out-of-line).
199 -------------------------------------------------------------------------- */
201 #include "stg/TailCalls.h"
203 /* -----------------------------------------------------------------------------
205 -------------------------------------------------------------------------- */
208 #include "stg/MachRegs.h"
209 #include "stg/Regs.h"
210 #include "stg/Ticky.h"
214 * This is included later for RTS sources, after definitions of
215 * StgInfoTable, StgClosure and so on.
217 #include "stg/MiscClosures.h"
220 #include "stg/SMP.h" // write_barrier() inline is required
222 /* -----------------------------------------------------------------------------
223 Moving Floats and Doubles
225 ASSIGN_FLT is for assigning a float to memory (usually the
226 stack/heap). The memory address is guaranteed to be
227 StgWord aligned (currently == sizeof(void *)).
229 PK_FLT is for pulling a float out of memory. The memory is
230 guaranteed to be StgWord aligned.
231 -------------------------------------------------------------------------- */
233 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
234 INLINE_HEADER StgFloat PK_FLT (W_ []);
236 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
238 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
239 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
241 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
243 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
250 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
257 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
259 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
261 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
262 INLINE_HEADER StgDouble PK_DBL (W_ []);
264 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
265 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
267 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
269 /* Sparc uses two floating point registers to hold a double. We can
270 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
271 * independently - unfortunately this code isn't writable in C, we
272 * have to use inline assembler.
276 #define ASSIGN_DBL(dst0,src) \
277 { StgPtr dst = (StgPtr)(dst0); \
278 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
279 "=m" (((P_)(dst))[1]) : "f" (src)); \
282 #define PK_DBL(src0) \
283 ( { StgPtr src = (StgPtr)(src0); \
285 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
286 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
289 #else /* ! sparc_HOST_ARCH */
291 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
292 INLINE_HEADER StgDouble PK_DBL (W_ []);
304 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
308 p_dest[0] = y.du.dhi;
309 p_dest[1] = y.du.dlo;
312 /* GCC also works with this version, but it generates
313 the same code as the previous one, and is not ANSI
315 #define ASSIGN_DBL( p_dest, src ) \
316 *p_dest = ((double_thing) src).du.dhi; \
317 *(p_dest+1) = ((double_thing) src).du.dlo \
320 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
328 #endif /* ! sparc_HOST_ARCH */
330 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
333 /* -----------------------------------------------------------------------------
334 Moving 64-bit quantities around
336 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
337 PK_Word64 load an StgWord64/StgInt64 from a amemory location
339 In both cases the memory location might not be 64-bit aligned.
340 -------------------------------------------------------------------------- */
342 #if SIZEOF_HSWORD == 4
347 } unpacked_double_word;
351 unpacked_double_word iu;
356 unpacked_double_word wu;
359 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
363 p_dest[0] = y.wu.dhi;
364 p_dest[1] = y.wu.dlo;
367 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
375 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
379 p_dest[0] = y.iu.dhi;
380 p_dest[1] = y.iu.dlo;
383 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
391 #elif SIZEOF_VOID_P == 8
393 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
398 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
403 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
408 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
413 #endif /* SIZEOF_HSWORD == 4 */
415 /* -----------------------------------------------------------------------------
417 -------------------------------------------------------------------------- */
419 #if defined(USE_SPLIT_MARKERS)
420 #if defined(LEADING_UNDERSCORE)
421 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
423 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
426 #define __STG_SPLIT_MARKER /* nothing */
429 /* -----------------------------------------------------------------------------
430 Write-combining store
431 -------------------------------------------------------------------------- */
434 wcStore (StgPtr p, StgWord w)
436 #ifdef x86_64_HOST_ARCH
447 /* -----------------------------------------------------------------------------
448 Integer multiply with overflow
449 -------------------------------------------------------------------------- */
451 /* Multiply with overflow checking.
453 * This is tricky - the usual sign rules for add/subtract don't apply.
455 * On 32-bit machines we use gcc's 'long long' types, finding
456 * overflow with some careful bit-twiddling.
458 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
459 * we use a crude approximation, testing whether either operand is
460 * larger than 32-bits; if neither is, then we go ahead with the
463 * Return non-zero if there is any possibility that the signed multiply
464 * of a and b might overflow. Return zero only if you are absolutely sure
465 * that it won't overflow. If in doubt, return non-zero.
468 #if SIZEOF_VOID_P == 4
470 #ifdef WORDS_BIGENDIAN
471 #define RTS_CARRY_IDX__ 0
472 #define RTS_REM_IDX__ 1
474 #define RTS_CARRY_IDX__ 1
475 #define RTS_REM_IDX__ 0
483 #define mulIntMayOflo(a,b) \
487 z.l = (StgInt64)a * (StgInt64)b; \
488 r = z.i[RTS_REM_IDX__]; \
489 c = z.i[RTS_CARRY_IDX__]; \
490 if (c == 0 || c == -1) { \
491 c = ((StgWord)((a^b) ^ r)) \
492 >> (BITS_IN (I_) - 1); \
497 /* Careful: the carry calculation above is extremely delicate. Make sure
498 * you test it thoroughly after changing it.
503 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
505 /* If we have n-bit words then we have n-1 bits after accounting for the
506 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
507 #define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
508 #define HALF_NEG_INT (-HALF_POS_INT)
510 #define mulIntMayOflo(a,b) \
513 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
514 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\