RTS tidyup sweep, first phase
[ghc-hetmet.git] / includes / Stg.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2004
4  *
5  * Top-level include file for everything STG-ish.  
6  *
7  * This file is included *automatically* by all .hc files.
8  *
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).
13  *
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.
20  *
21  * ---------------------------------------------------------------------------*/
22
23 #ifndef STG_H
24 #define STG_H
25
26 /*
27  * If we are compiling a .hc file, then we want all the register
28  * variables.  This is the what happens if you #include "Stg.h" first:
29  * we assume this is a .hc file, and set IN_STG_CODE==1, which later
30  * causes the register variables to be enabled in stg/Regs.h.
31  *
32  * If instead "Rts.h" is included first, then we are compiling a
33  * vanilla C file.  Everything from Stg.h is provided, except that
34  * IN_STG_CODE is not defined, and the register variables will not be
35  * active.
36  */
37 #ifndef IN_STG_CODE
38 # define IN_STG_CODE 1
39
40 // Turn on C99 for .hc code.  This gives us the INFINITY and NAN
41 // constants from math.h, which we occasionally need to use in .hc (#1861)
42 # define _ISOC99_SOURCE
43
44 // We need _BSD_SOURCE so that math.h defines things like gamma
45 // on Linux
46 # define _BSD_SOURCE
47 #endif
48
49 #if IN_STG_CODE == 0
50 # define NO_GLOBAL_REG_DECLS    /* don't define fixed registers */
51 #endif
52
53 /* Configuration */
54 #include "ghcconfig.h"
55
56 /* The code generator calls the math functions directly in .hc code.
57    NB. after configuration stuff above, because this sets #defines
58    that depend on config info, such as __USE_FILE_OFFSET64 */
59 #include <math.h>
60
61 /* -----------------------------------------------------------------------------
62    Useful definitions
63    -------------------------------------------------------------------------- */
64
65 /*
66  * The C backend likes to refer to labels by just mentioning their
67  * names.  Howevver, when a symbol is declared as a variable in C, the
68  * C compiler will implicitly dereference it when it occurs in source.
69  * So we must subvert this behaviour for .hc files by declaring
70  * variables as arrays, which eliminates the implicit dereference.
71  */
72 #if IN_STG_CODE
73 #define RTS_VAR(x) (x)[]
74 #define RTS_DEREF(x) (*(x))
75 #else
76 #define RTS_VAR(x) x
77 #define RTS_DEREF(x) x
78 #endif
79
80 /* bit macros
81  */
82 #define BITS_PER_BYTE 8
83 #define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
84
85 /* Compute offsets of struct fields
86  */
87 #define STG_FIELD_OFFSET(s_type, field) ((StgWord)&(((s_type*)0)->field))
88
89 /*
90  * 'Portable' inlining:
91  * INLINE_HEADER is for inline functions in header files (macros)
92  * STATIC_INLINE is for inline functions in source files
93  * EXTERN_INLINE is for functions that we want to inline sometimes 
94  * (we also compile a static version of the function; see Inlines.c)
95  */
96 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
97
98 # define INLINE_HEADER static inline
99 # define INLINE_ME inline
100 # define STATIC_INLINE INLINE_HEADER
101
102 // The special "extern inline" behaviour is now only supported by gcc
103 // when _GNUC_GNU_INLINE__ is defined, and you have to use
104 // __attribute__((gnu_inline)).  So when we don't have this, we use
105 // ordinary static inline.
106 //
107 // Apple's gcc defines __GNUC_GNU_INLINE__ without providing
108 // gnu_inline, so we exclude MacOS X and fall through to the safe
109 // version.
110 //
111 #if defined(__GNUC_GNU_INLINE__) && !defined(__APPLE__)
112 #  if defined(KEEP_INLINES)
113 #    define EXTERN_INLINE inline
114 #  else
115 #    define EXTERN_INLINE extern inline __attribute__((gnu_inline))
116 #  endif
117 #else
118 #  if defined(KEEP_INLINES)
119 #    define EXTERN_INLINE
120 #  else
121 #    define EXTERN_INLINE INLINE_HEADER
122 #  endif
123 #endif
124
125 #elif defined(_MSC_VER)
126
127 # define INLINE_HEADER __inline static
128 # define INLINE_ME __inline
129 # define STATIC_INLINE INLINE_HEADER
130
131 # if defined(KEEP_INLINES)
132 #  define EXTERN_INLINE __inline
133 # else
134 #  define EXTERN_INLINE __inline extern
135 # endif
136
137 #else
138
139 # error "Don't know how to inline functions with your C compiler."
140
141 #endif
142
143
144 /*
145  * GCC attributes
146  */
147 #if defined(__GNUC__)
148 #define GNU_ATTRIBUTE(at) __attribute__((at))
149 #else
150 #define GNU_ATTRIBUTE(at)
151 #endif
152
153 #if __GNUC__ >= 3 
154 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
155 #else
156 #define GNUC3_ATTRIBUTE(at)
157 #endif
158
159 #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3
160 #define GNUC_ATTR_HOT __attribute__((hot))
161 #else
162 #define GNUC_ATTR_HOT /* nothing */
163 #endif
164
165 #define STG_UNUSED    GNUC3_ATTRIBUTE(__unused__)
166
167 /* -----------------------------------------------------------------------------
168    Global type definitions
169    -------------------------------------------------------------------------- */
170
171 #include "MachDeps.h"
172 #include "stg/Types.h"
173
174 /* -----------------------------------------------------------------------------
175    Shorthand forms
176    -------------------------------------------------------------------------- */
177
178 typedef StgChar         C_;
179 typedef StgWord         W_;
180 typedef StgWord*        P_;
181 typedef StgInt          I_;
182 typedef StgWord StgWordArray[];
183
184 #define EI_(X)          extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
185 #define II_(X)          static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
186 #define IF_(f)          static StgFunPtr GNUC3_ATTRIBUTE(used) f(void) 
187 #define FN_(f)          StgFunPtr f(void)
188 #define EF_(f)          extern StgFunPtr f(void)
189
190 /* -----------------------------------------------------------------------------
191    Tail calls
192
193    This needs to be up near the top as the register line on alpha needs
194    to be before all procedures (inline & out-of-line).
195    -------------------------------------------------------------------------- */
196
197 #include "stg/TailCalls.h"
198
199 /* -----------------------------------------------------------------------------
200    Other Stg stuff...
201    -------------------------------------------------------------------------- */
202
203 #include "stg/DLL.h"
204 #include "stg/MachRegs.h"
205 #include "stg/Regs.h"
206 #include "stg/Ticky.h"
207
208 #if IN_STG_CODE
209 /*
210  * This is included later for RTS sources, after definitions of
211  * StgInfoTable, StgClosure and so on. 
212  */
213 #include "stg/MiscClosures.h"
214 #endif
215
216 #include "stg/SMP.h" // write_barrier() inline is required 
217
218 /* -----------------------------------------------------------------------------
219    Moving Floats and Doubles
220
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 *)).
224
225    PK_FLT     is for pulling a float out of memory.  The memory is
226               guaranteed to be StgWord aligned.
227    -------------------------------------------------------------------------- */
228
229 INLINE_HEADER void        ASSIGN_FLT (W_ [], StgFloat);
230 INLINE_HEADER StgFloat    PK_FLT     (W_ []);
231
232 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
233
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; }
236
237 #else  /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
238
239 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
240 {
241     float_thing y;
242     y.f = src;
243     *p_dest = y.fu;
244 }
245
246 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
247 {
248     float_thing y;
249     y.fu = *p_src;
250     return(y.f);
251 }
252
253 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
254
255 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
256
257 INLINE_HEADER void        ASSIGN_DBL (W_ [], StgDouble);
258 INLINE_HEADER StgDouble   PK_DBL     (W_ []);
259
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; }
262
263 #else   /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
264
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.
269  */
270 #if sparc_HOST_ARCH
271
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)); \
276     }
277
278 #define PK_DBL(src0) \
279     ( { StgPtr src = (StgPtr)(src0); \
280         register double d; \
281       __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
282         "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
283     } )
284
285 #else /* ! sparc_HOST_ARCH */
286
287 INLINE_HEADER void        ASSIGN_DBL (W_ [], StgDouble);
288 INLINE_HEADER StgDouble   PK_DBL     (W_ []);
289
290 typedef struct
291   { StgWord dhi;
292     StgWord dlo;
293   } unpacked_double;
294
295 typedef union
296   { StgDouble d;
297     unpacked_double du;
298   } double_thing;
299
300 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
301 {
302     double_thing y;
303     y.d = src;
304     p_dest[0] = y.du.dhi;
305     p_dest[1] = y.du.dlo;
306 }
307
308 /* GCC also works with this version, but it generates
309    the same code as the previous one, and is not ANSI
310
311 #define ASSIGN_DBL( p_dest, src ) \
312         *p_dest = ((double_thing) src).du.dhi; \
313         *(p_dest+1) = ((double_thing) src).du.dlo \
314 */
315
316 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
317 {
318     double_thing y;
319     y.du.dhi = p_src[0];
320     y.du.dlo = p_src[1];
321     return(y.d);
322 }
323
324 #endif /* ! sparc_HOST_ARCH */
325
326 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
327
328
329 /* -----------------------------------------------------------------------------
330    Moving 64-bit quantities around
331
332    ASSIGN_Word64      assign an StgWord64/StgInt64 to a memory location
333    PK_Word64          load an StgWord64/StgInt64 from a amemory location
334
335    In both cases the memory location might not be 64-bit aligned.
336    -------------------------------------------------------------------------- */
337
338 #if SIZEOF_HSWORD == 4
339
340 typedef struct
341   { StgWord dhi;
342     StgWord dlo;
343   } unpacked_double_word;
344
345 typedef union
346   { StgInt64 i;
347     unpacked_double_word iu;
348   } int64_thing;
349
350 typedef union
351   { StgWord64 w;
352     unpacked_double_word wu;
353   } word64_thing;
354
355 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
356 {
357     word64_thing y;
358     y.w = src;
359     p_dest[0] = y.wu.dhi;
360     p_dest[1] = y.wu.dlo;
361 }
362
363 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
364 {
365     word64_thing y;
366     y.wu.dhi = p_src[0];
367     y.wu.dlo = p_src[1];
368     return(y.w);
369 }
370
371 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
372 {
373     int64_thing y;
374     y.i = src;
375     p_dest[0] = y.iu.dhi;
376     p_dest[1] = y.iu.dlo;
377 }
378
379 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
380 {
381     int64_thing y;
382     y.iu.dhi = p_src[0];
383     y.iu.dlo = p_src[1];
384     return(y.i);
385 }
386
387 #elif SIZEOF_VOID_P == 8
388
389 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
390 {
391         p_dest[0] = src;
392 }
393
394 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
395 {
396     return p_src[0];
397 }
398
399 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
400 {
401     p_dest[0] = src;
402 }
403
404 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
405 {
406     return p_src[0];
407 }
408
409 #endif /* SIZEOF_HSWORD == 4 */
410
411 /* -----------------------------------------------------------------------------
412    Split markers
413    -------------------------------------------------------------------------- */
414
415 #if defined(USE_SPLIT_MARKERS)
416 #if defined(LEADING_UNDERSCORE)
417 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
418 #else
419 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
420 #endif
421 #else
422 #define __STG_SPLIT_MARKER /* nothing */
423 #endif
424
425 /* -----------------------------------------------------------------------------
426    Write-combining store
427    -------------------------------------------------------------------------- */
428
429 INLINE_HEADER void
430 wcStore (StgPtr p, StgWord w)
431 {
432 #ifdef x86_64_HOST_ARCH    
433     __asm__(
434         "movnti\t%1, %0"
435         : "=m" (*p)
436         : "r" (w)
437         );
438 #else
439       *p = w;
440 #endif
441 }
442
443 /* -----------------------------------------------------------------------------
444    Integer multiply with overflow
445    -------------------------------------------------------------------------- */
446
447 /* Multiply with overflow checking.
448  *
449  * This is tricky - the usual sign rules for add/subtract don't apply.  
450  *
451  * On 32-bit machines we use gcc's 'long long' types, finding
452  * overflow with some careful bit-twiddling.
453  *
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
457  * multiplication.
458  *
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.
462  */
463
464 #if SIZEOF_VOID_P == 4
465
466 #ifdef WORDS_BIGENDIAN
467 #define RTS_CARRY_IDX__ 0
468 #define RTS_REM_IDX__  1
469 #else
470 #define RTS_CARRY_IDX__ 1
471 #define RTS_REM_IDX__ 0
472 #endif
473
474 typedef union {
475     StgInt64 l;
476     StgInt32 i[2];
477 } long_long_u ;
478
479 #define mulIntMayOflo(a,b)                      \
480 ({                                              \
481   StgInt32 r, c;                                \
482   long_long_u z;                                \
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);                    \
489   }                                             \
490   c;                                            \
491 })
492
493 /* Careful: the carry calculation above is extremely delicate.  Make sure
494  * you test it thoroughly after changing it.
495  */
496
497 #else
498
499 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
500
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)
505
506 #define mulIntMayOflo(a,b)                      \
507 ({                                              \
508   I_ c;                                         \
509   if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT    \
510       || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\
511     c = 1;                                      \
512   } else {                                      \
513     c = 0;                                      \
514   }                                             \
515   c;                                            \
516 })
517 #endif
518
519 #endif /* STG_H */