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
37 # define NO_GLOBAL_REG_DECLS /* don't define fixed registers */
41 #include "ghcconfig.h"
42 #include "RtsConfig.h"
44 /* -----------------------------------------------------------------------------
46 -------------------------------------------------------------------------- */
49 * The C backend like to refer to labels by just mentioning their
50 * names. Howevver, when a symbol is declared as a variable in C, the
51 * C compiler will implicitly dereference it when it occurs in source.
52 * So we must subvert this behaviour for .hc files by declaring
53 * variables as arrays, which eliminates the implicit dereference.
56 #define RTS_VAR(x) (x)[]
57 #define RTS_DEREF(x) (*(x))
60 #define RTS_DEREF(x) x
65 #define BITS_PER_BYTE 8
66 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
71 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
72 # define INLINE_HEADER static inline
73 # define INLINE_ME inline
74 # define STATIC_INLINE INLINE_HEADER
75 #elif defined(_MSC_VER)
76 # define INLINE_HEADER __inline static
77 # define INLINE_ME __inline
78 # define STATIC_INLINE INLINE_HEADER
80 # error "Don't know how to inline functions with your C compiler."
87 #define GNU_ATTRIBUTE(at) __attribute__((at))
89 #define GNU_ATTRIBUTE(at)
93 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
95 #define GNUC3_ATTRIBUTE(at)
98 #define STG_UNUSED GNUC3_ATTRIBUTE(__unused__)
100 /* -----------------------------------------------------------------------------
101 Global type definitions
102 -------------------------------------------------------------------------- */
104 #include "MachDeps.h"
105 #include "StgTypes.h"
107 /* -----------------------------------------------------------------------------
109 -------------------------------------------------------------------------- */
117 typedef const StgWord* D_;
118 typedef StgFunPtr F_;
119 typedef StgByteArray B_;
120 typedef StgClosurePtr L_;
122 typedef StgInt64 LI_;
123 typedef StgWord64 LW_;
125 #define IF_(f) static F_ GNUC3_ATTRIBUTE(used) f(void)
126 #define FN_(f) F_ f(void)
127 #define EF_(f) extern F_ f(void)
129 typedef StgWord StgWordArray[];
130 #define EI_ extern StgWordArray
131 #define II_ static StgWordArray
133 /* -----------------------------------------------------------------------------
136 This needs to be up near the top as the register line on alpha needs
137 to be before all procedures (inline & out-of-line).
138 -------------------------------------------------------------------------- */
140 #include "TailCalls.h"
142 /* -----------------------------------------------------------------------------
144 -------------------------------------------------------------------------- */
147 #include "MachRegs.h"
149 #include "StgProf.h" /* ToDo: separate out RTS-only stuff from here */
153 * This is included later for RTS sources, after definitions of
154 * StgInfoTable, StgClosure and so on.
156 #include "StgMiscClosures.h"
159 /* RTS external interface */
160 #include "RtsExternal.h"
162 /* -----------------------------------------------------------------------------
163 Moving Floats and Doubles
165 ASSIGN_FLT is for assigning a float to memory (usually the
166 stack/heap). The memory address is guaranteed to be
167 StgWord aligned (currently == sizeof(void *)).
169 PK_FLT is for pulling a float out of memory. The memory is
170 guaranteed to be StgWord aligned.
171 -------------------------------------------------------------------------- */
173 INLINE_HEADER void ASSIGN_FLT (W_ [], StgFloat);
174 INLINE_HEADER StgFloat PK_FLT (W_ []);
176 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
178 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
179 INLINE_HEADER StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
181 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
183 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
190 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
197 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
199 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
201 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
202 INLINE_HEADER StgDouble PK_DBL (W_ []);
204 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
205 INLINE_HEADER StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
207 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
209 /* Sparc uses two floating point registers to hold a double. We can
210 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
211 * independently - unfortunately this code isn't writable in C, we
212 * have to use inline assembler.
216 #define ASSIGN_DBL(dst0,src) \
217 { StgPtr dst = (StgPtr)(dst0); \
218 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
219 "=m" (((P_)(dst))[1]) : "f" (src)); \
222 #define PK_DBL(src0) \
223 ( { StgPtr src = (StgPtr)(src0); \
225 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
226 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
229 #else /* ! sparc_HOST_ARCH */
231 INLINE_HEADER void ASSIGN_DBL (W_ [], StgDouble);
232 INLINE_HEADER StgDouble PK_DBL (W_ []);
244 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
248 p_dest[0] = y.du.dhi;
249 p_dest[1] = y.du.dlo;
252 /* GCC also works with this version, but it generates
253 the same code as the previous one, and is not ANSI
255 #define ASSIGN_DBL( p_dest, src ) \
256 *p_dest = ((double_thing) src).du.dhi; \
257 *(p_dest+1) = ((double_thing) src).du.dlo \
260 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
268 #endif /* ! sparc_HOST_ARCH */
270 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
273 /* -----------------------------------------------------------------------------
274 Moving 64-bit quantities around
276 ASSIGN_Word64 assign an StgWord64/StgInt64 to a memory location
277 PK_Word64 load an StgWord64/StgInt64 from a amemory location
279 In both cases the memory location might not be 64-bit aligned.
280 -------------------------------------------------------------------------- */
282 #ifdef SUPPORT_LONG_LONGS
287 } unpacked_double_word;
291 unpacked_double_word iu;
296 unpacked_double_word wu;
299 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
303 p_dest[0] = y.wu.dhi;
304 p_dest[1] = y.wu.dlo;
307 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
315 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
319 p_dest[0] = y.iu.dhi;
320 p_dest[1] = y.iu.dlo;
323 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
331 #elif SIZEOF_VOID_P == 8
333 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
338 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
343 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
348 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
355 /* -----------------------------------------------------------------------------
357 -------------------------------------------------------------------------- */
359 #if defined(USE_SPLIT_MARKERS)
360 #if defined(LEADING_UNDERSCORE)
361 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
363 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
366 #define __STG_SPLIT_MARKER /* nothing */
369 /* -----------------------------------------------------------------------------
370 Integer multiply with overflow
371 -------------------------------------------------------------------------- */
373 /* Multiply with overflow checking.
375 * This is tricky - the usual sign rules for add/subtract don't apply.
377 * On 32-bit machines we use gcc's 'long long' types, finding
378 * overflow with some careful bit-twiddling.
380 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
381 * we use a crude approximation, testing whether either operand is
382 * larger than 32-bits; if neither is, then we go ahead with the
385 * Return non-zero if there is any possibility that the signed multiply
386 * of a and b might overflow. Return zero only if you are absolutely sure
387 * that it won't overflow. If in doubt, return non-zero.
390 #if SIZEOF_VOID_P == 4
392 #ifdef WORDS_BIGENDIAN
393 #define RTS_CARRY_IDX__ 0
394 #define RTS_REM_IDX__ 1
396 #define RTS_CARRY_IDX__ 1
397 #define RTS_REM_IDX__ 0
405 #define mulIntMayOflo(a,b) \
409 z.l = (StgInt64)a * (StgInt64)b; \
410 r = z.i[RTS_REM_IDX__]; \
411 c = z.i[RTS_CARRY_IDX__]; \
412 if (c == 0 || c == -1) { \
413 c = ((StgWord)((a^b) ^ r)) \
414 >> (BITS_IN (I_) - 1); \
419 /* Careful: the carry calculation above is extremely delicate. Make sure
420 * you test it thoroughly after changing it.
425 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
427 #define HALF_POS_INT (((I_)1) << (BITS_IN (I_) / 2))
428 #define HALF_NEG_INT (-HALF_POS_INT)
430 #define mulIntMayOflo(a,b) \
433 if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
434 || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\