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