1 /* -----------------------------------------------------------------------------
2 * $Id: Stg.h,v 1.66 2005/01/28 12:55:52 simonmar Exp $
4 * (c) The GHC Team, 1998-2004
6 * Top-level include file for everything STG-ish.
8 * This file is included *automatically* by all .hc files.
10 * NOTE: always include Stg.h *before* any other headers, because we
11 * define some register variables which must be done before any inline
12 * functions are defined (some system headers have been known to
13 * define the odd inline function).
15 * We generally try to keep as little visible as possible when
16 * compiling .hc files. So for example the definitions of the
17 * InfoTable structs, closure structs and other RTS types are not
18 * visible here. The compiler knows enough about the representations
19 * of these types to generate code which manipulates them directly
20 * with pointer arithmetic.
22 * ---------------------------------------------------------------------------*/
28 /* If we include "Stg.h" directly, we're in STG code, and we therefore
29 * get all the global register variables, macros etc. that go along
30 * with that. If "Stg.h" is included via "Rts.h", we're assumed to
34 # define IN_STG_CODE 1
38 # define NO_GLOBAL_REG_DECLS /* don't define fixed registers */
42 #include "ghcconfig.h"
43 #include "RtsConfig.h"
45 /* -----------------------------------------------------------------------------
47 -------------------------------------------------------------------------- */
50 * The C backend like to refer to labels by just mentioning their
51 * names. Howevver, when a symbol is declared as a variable in C, the
52 * C compiler will implicitly dereference it when it occurs in source.
53 * So we must subvert this behaviour for .hc files by declaring
54 * variables as arrays, which eliminates the implicit dereference.
57 #define RTS_VAR(x) (x)[]
58 #define RTS_DEREF(x) (*(x))
61 #define RTS_DEREF(x) x
66 #define BITS_PER_BYTE 8
67 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
72 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
73 # define INLINE_HEADER static inline
74 # define INLINE_ME inline
75 # define STATIC_INLINE INLINE_HEADER
76 #elif defined(_MSC_VER)
77 # define INLINE_HEADER __inline static
78 # define INLINE_ME __inline
79 # define STATIC_INLINE INLINE_HEADER
81 # error "Don't know how to inline functions with your C compiler."
84 /* -----------------------------------------------------------------------------
85 Global type definitions
86 -------------------------------------------------------------------------- */
91 /* -----------------------------------------------------------------------------
93 -------------------------------------------------------------------------- */
101 typedef const StgWord* D_;
102 typedef StgFunPtr F_;
103 typedef StgByteArray B_;
104 typedef StgClosurePtr L_;
106 typedef StgInt64 LI_;
107 typedef StgWord64 LW_;
109 #define IF_(f) static F_ f(void)
110 #define FN_(f) F_ f(void)
111 #define EF_(f) extern F_ f(void)
113 typedef StgWord StgWordArray[];
114 #define EI_ extern StgWordArray
115 #define II_ static StgWordArray
117 /* -----------------------------------------------------------------------------
120 This needs to be up near the top as the register line on alpha needs
121 to be before all procedures (inline & out-of-line).
122 -------------------------------------------------------------------------- */
124 #include "TailCalls.h"
126 /* -----------------------------------------------------------------------------
128 -------------------------------------------------------------------------- */
131 #include "MachRegs.h"
133 #include "StgProf.h" /* ToDo: separate out RTS-only stuff from here */
137 * This is included later for RTS sources, after definitions of
138 * StgInfoTable, StgClosure and so on.
140 #include "StgMiscClosures.h"
143 /* RTS external interface */
144 #include "RtsExternal.h"
146 /* -----------------------------------------------------------------------------
147 Moving Floats and Doubles
149 ASSIGN_FLT is for assigning a float to memory (usually the
150 stack/heap). The memory address is guaranteed to be
151 StgWord aligned (currently == sizeof(void *)).
153 PK_FLT is for pulling a float out of memory. The memory is
154 guaranteed to be StgWord aligned.
155 -------------------------------------------------------------------------- */
157 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
158 INLINE_HEADER StgFloat PK_FLT (W_ []);
160 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
162 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
163 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
165 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
167 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
174 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
181 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
183 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
185 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
186 INLINE_HEADER StgDouble PK_DBL (W_ []);
188 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
189 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
191 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
193 /* Sparc uses two floating point registers to hold a double. We can
194 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
195 * independently - unfortunately this code isn't writable in C, we
196 * have to use inline assembler.
200 #define ASSIGN_DBL(dst0,src) \
201 { StgPtr dst = (StgPtr)(dst0); \
202 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
203 "=m" (((P_)(dst))[1]) : "f" (src)); \
206 #define PK_DBL(src0) \
207 ( { StgPtr src = (StgPtr)(src0); \
209 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
210 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
213 #else /* ! sparc_HOST_ARCH */
215 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
216 INLINE_HEADER StgDouble PK_DBL (W_ []);
228 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
232 p_dest[0] = y.du.dhi;
233 p_dest[1] = y.du.dlo;
236 /* GCC also works with this version, but it generates
237 the same code as the previous one, and is not ANSI
239 #define ASSIGN_DBL( p_dest, src ) \
240 *p_dest = ((double_thing) src).du.dhi; \
241 *(p_dest+1) = ((double_thing) src).du.dlo \
244 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
252 #endif /* ! sparc_HOST_ARCH */
254 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
257 /* -----------------------------------------------------------------------------
258 Moving 64-bit quantities around
260 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
261 PK_Word64 load an StgWord64/StgInt64 from a amemory location
263 In both cases the memory location might not be 64-bit aligned.
264 -------------------------------------------------------------------------- */
266 #ifdef SUPPORT_LONG_LONGS
271 } unpacked_double_word;
275 unpacked_double_word iu;
280 unpacked_double_word wu;
283 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
287 p_dest[0] = y.wu.dhi;
288 p_dest[1] = y.wu.dlo;
291 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
299 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
303 p_dest[0] = y.iu.dhi;
304 p_dest[1] = y.iu.dlo;
307 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
315 #elif SIZEOF_VOID_P == 8
317 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
322 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
327 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
332 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
339 /* -----------------------------------------------------------------------------
341 -------------------------------------------------------------------------- */
343 #if defined(USE_SPLIT_MARKERS)
344 #if defined(LEADING_UNDERSCORE)
345 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
347 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
350 #define __STG_SPLIT_MARKER /* nothing */
353 /* -----------------------------------------------------------------------------
354 Integer multiply with overflow
355 -------------------------------------------------------------------------- */
357 /* Multiply with overflow checking.
359 * This is tricky - the usual sign rules for add/subtract don't apply.
361 * On 32-bit machines we use gcc's 'long long' types, finding
362 * overflow with some careful bit-twiddling.
364 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
365 * we use a crude approximation, testing whether either operand is
366 * larger than 32-bits; if neither is, then we go ahead with the
369 * Return non-zero if there is any possibility that the signed multiply
370 * of a and b might overflow. Return zero only if you are absolutely sure
371 * that it won't overflow. If in doubt, return non-zero.
374 #if SIZEOF_VOID_P == 4
376 #ifdef WORDS_BIGENDIAN
377 #define RTS_CARRY_IDX__ 0
378 #define RTS_REM_IDX__ 1
380 #define RTS_CARRY_IDX__ 1
381 #define RTS_REM_IDX__ 0
389 #define mulIntMayOflo(a,b) \
393 z.l = (StgInt64)a * (StgInt64)b; \
394 r = z.i[RTS_REM_IDX__]; \
395 c = z.i[RTS_CARRY_IDX__]; \
396 if (c == 0 || c == -1) { \
397 c = ((StgWord)((a^b) ^ r)) \
398 >> (BITS_IN (I_) - 1); \
403 /* Careful: the carry calculation above is extremely delicate. Make sure
404 * you test it thoroughly after changing it.
409 #define HALF_INT (((I_)1) << (BITS_IN (I_) / 2))
411 #define stg_abs(a) (((I_)(a)) < 0 ? -((I_)(a)) : ((I_)(a)))
413 #define mulIntMayOflo(a,b) \
416 if (stg_abs(a) >= HALF_INT || \
417 stg_abs(b) >= HALF_INT) { \