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