1 /* -----------------------------------------------------------------------------
2 * $Id: Stg.h,v 1.64 2004/09/02 12:45:25 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
39 # define NO_REGS /* don't define fixed registers */
44 #include "ghcconfig.h"
45 #include "RtsConfig.h"
47 /* -----------------------------------------------------------------------------
49 -------------------------------------------------------------------------- */
52 * The C backend like to refer to labels by just mentioning their
53 * names. Howevver, when a symbol is declared as a variable in C, the
54 * C compiler will implicitly dereference it when it occurs in source.
55 * So we must subvert this behaviour for .hc files by declaring
56 * variables as arrays, which eliminates the implicit dereference.
59 #define RTS_VAR(x) (x)[]
60 #define RTS_DEREF(x) (*(x))
63 #define RTS_DEREF(x) x
68 #define BITS_PER_BYTE 8
69 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
74 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
75 # define INLINE_HEADER static inline
76 # define INLINE_ME inline
77 # define STATIC_INLINE INLINE_HEADER
78 #elif defined(_MSC_VER)
79 # define INLINE_HEADER __inline static
80 # define INLINE_ME __inline
81 # define STATIC_INLINE INLINE_HEADER
83 # error "Don't know how to inline functions with your C compiler."
86 /* -----------------------------------------------------------------------------
87 Global type definitions
88 -------------------------------------------------------------------------- */
93 /* -----------------------------------------------------------------------------
95 -------------------------------------------------------------------------- */
103 typedef const StgWord* D_;
104 typedef StgFunPtr F_;
105 typedef StgByteArray B_;
106 typedef StgClosurePtr L_;
108 typedef StgInt64 LI_;
109 typedef StgWord64 LW_;
111 #define IF_(f) static F_ f(void)
112 #define FN_(f) F_ f(void)
113 #define EF_(f) extern F_ f(void)
115 typedef StgWord StgWordArray[];
116 #define EI_ extern StgWordArray
117 #define II_ static StgWordArray
119 /* -----------------------------------------------------------------------------
122 This needs to be up near the top as the register line on alpha needs
123 to be before all procedures (inline & out-of-line).
124 -------------------------------------------------------------------------- */
126 #include "TailCalls.h"
128 /* -----------------------------------------------------------------------------
130 -------------------------------------------------------------------------- */
133 #include "MachRegs.h"
135 #include "StgProf.h" /* ToDo: separate out RTS-only stuff from here */
139 * This is included later for RTS sources, after definitions of
140 * StgInfoTable, StgClosure and so on.
142 #include "StgMiscClosures.h"
145 /* RTS external interface */
146 #include "RtsExternal.h"
148 /* -----------------------------------------------------------------------------
149 Moving Floats and Doubles
151 ASSIGN_FLT is for assigning a float to memory (usually the
152 stack/heap). The memory address is guaranteed to be
153 StgWord aligned (currently == sizeof(void *)).
155 PK_FLT is for pulling a float out of memory. The memory is
156 guaranteed to be StgWord aligned.
157 -------------------------------------------------------------------------- */
159 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
160 INLINE_HEADER StgFloat PK_FLT (W_ []);
162 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
164 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
165 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
167 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
169 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
176 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
183 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
185 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
187 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
188 INLINE_HEADER StgDouble PK_DBL (W_ []);
190 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
191 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
193 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
195 /* Sparc uses two floating point registers to hold a double. We can
196 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
197 * independently - unfortunately this code isn't writable in C, we
198 * have to use inline assembler.
200 #if sparc_TARGET_ARCH
202 #define ASSIGN_DBL(dst0,src) \
203 { StgPtr dst = (StgPtr)(dst0); \
204 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
205 "=m" (((P_)(dst))[1]) : "f" (src)); \
208 #define PK_DBL(src0) \
209 ( { StgPtr src = (StgPtr)(src0); \
211 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
212 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
215 #else /* ! sparc_TARGET_ARCH */
217 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
218 INLINE_HEADER StgDouble PK_DBL (W_ []);
230 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
234 p_dest[0] = y.du.dhi;
235 p_dest[1] = y.du.dlo;
238 /* GCC also works with this version, but it generates
239 the same code as the previous one, and is not ANSI
241 #define ASSIGN_DBL( p_dest, src ) \
242 *p_dest = ((double_thing) src).du.dhi; \
243 *(p_dest+1) = ((double_thing) src).du.dlo \
246 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
254 #endif /* ! sparc_TARGET_ARCH */
256 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
259 /* -----------------------------------------------------------------------------
260 Moving 64-bit quantities around
262 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
263 PK_Word64 load an StgWord64/StgInt64 from a amemory location
265 In both cases the memory location might not be 64-bit aligned.
266 -------------------------------------------------------------------------- */
268 #ifdef SUPPORT_LONG_LONGS
273 } unpacked_double_word;
277 unpacked_double_word iu;
282 unpacked_double_word wu;
285 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
289 p_dest[0] = y.wu.dhi;
290 p_dest[1] = y.wu.dlo;
293 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
301 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
305 p_dest[0] = y.iu.dhi;
306 p_dest[1] = y.iu.dlo;
309 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
317 #elif SIZEOF_VOID_P == 8
319 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
324 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
329 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
334 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
341 /* -----------------------------------------------------------------------------
343 -------------------------------------------------------------------------- */
345 #if defined(USE_SPLIT_MARKERS)
346 #if defined(LEADING_UNDERSCORE)
347 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
349 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
352 #define __STG_SPLIT_MARKER /* nothing */
355 /* -----------------------------------------------------------------------------
356 Integer multiply with overflow
357 -------------------------------------------------------------------------- */
359 /* Multiply with overflow checking.
361 * This is tricky - the usual sign rules for add/subtract don't apply.
363 * On 32-bit machines we use gcc's 'long long' types, finding
364 * overflow with some careful bit-twiddling.
366 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
367 * we use a crude approximation, testing whether either operand is
368 * larger than 32-bits; if neither is, then we go ahead with the
371 * Return non-zero if there is any possibility that the signed multiply
372 * of a and b might overflow. Return zero only if you are absolutely sure
373 * that it won't overflow. If in doubt, return non-zero.
376 #if SIZEOF_VOID_P == 4
378 #ifdef WORDS_BIGENDIAN
379 #define RTS_CARRY_IDX__ 0
380 #define RTS_REM_IDX__ 1
382 #define RTS_CARRY_IDX__ 1
383 #define RTS_REM_IDX__ 0
391 #define mulIntMayOflo(a,b) \
395 z.l = (StgInt64)a * (StgInt64)b; \
396 r = z.i[RTS_REM_IDX__]; \
397 c = z.i[RTS_CARRY_IDX__]; \
398 if (c == 0 || c == -1) { \
399 c = ((StgWord)((a^b) ^ r)) \
400 >> (BITS_IN (I_) - 1); \
405 /* Careful: the carry calculation above is extremely delicate. Make sure
406 * you test it thoroughly after changing it.
411 #define HALF_INT (((I_)1) << (BITS_IN (I_) / 2))
413 #define stg_abs(a) (((I_)(a)) < 0 ? -((I_)(a)) : ((I_)(a)))
415 #define mulIntMayOflo(a,b) \
418 if (stg_abs(a) >= HALF_INT || \
419 stg_abs(b) >= HALF_INT) { \