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