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