FIX #2469: sort out our static/extern inline story
[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  * (we also compile a static version of the function; see Inlines.c)
86  */
87 #if defined(__GNUC__) || defined( __INTEL_COMPILER)
88
89 # define INLINE_HEADER static inline
90 # define INLINE_ME inline
91 # define STATIC_INLINE INLINE_HEADER
92
93 // The special "extern inline" behaviour is now only supported by gcc
94 // when _GNUC_GNU_INLINE__ is defined, and you have to use
95 // __attribute__((gnu_inline)).  So when we don't have this, we use
96 // ordinary static inline.
97 //
98 #if defined(__GNUC_GNU_INLINE__)
99 #  if defined(KEEP_INLINES)
100 #    define EXTERN_INLINE inline
101 #  else
102 #    define EXTERN_INLINE extern inline __attribute__((gnu_inline))
103 #  endif
104 #else
105 #  if defined(KEEP_INLINES)
106 #    define EXTERN_INLINE
107 #  else
108 #    define EXTERN_INLINE INLINE_HEADER
109 #  endif
110 #endif
111
112 #elif defined(_MSC_VER)
113
114 # define INLINE_HEADER __inline static
115 # define INLINE_ME __inline
116 # define STATIC_INLINE INLINE_HEADER
117
118 # if defined(KEEP_INLINES)
119 #  define EXTERN_INLINE __inline
120 # else
121 #  define EXTERN_INLINE __inline extern
122 # endif
123
124 #else
125
126 # error "Don't know how to inline functions with your C compiler."
127
128 #endif
129
130
131 /*
132  * GCC attributes
133  */
134 #if defined(__GNUC__)
135 #define GNU_ATTRIBUTE(at) __attribute__((at))
136 #else
137 #define GNU_ATTRIBUTE(at)
138 #endif
139
140 #if __GNUC__ >= 3 
141 #define GNUC3_ATTRIBUTE(at) __attribute__((at))
142 #else
143 #define GNUC3_ATTRIBUTE(at)
144 #endif
145
146 #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3
147 #define GNUC_ATTR_HOT __attribute__((hot))
148 #else
149 #define GNUC_ATTR_HOT /* nothing */
150 #endif
151
152 #define STG_UNUSED    GNUC3_ATTRIBUTE(__unused__)
153
154 /* -----------------------------------------------------------------------------
155    Global type definitions
156    -------------------------------------------------------------------------- */
157
158 #include "MachDeps.h"
159 #include "StgTypes.h"
160
161 /* -----------------------------------------------------------------------------
162    Shorthand forms
163    -------------------------------------------------------------------------- */
164
165 typedef StgChar         C_;
166 typedef StgWord         W_;
167 typedef StgWord*        P_;
168 typedef P_*             PP_;
169 typedef StgInt          I_;
170 typedef StgAddr         A_;
171 typedef const StgWord*  D_;
172 typedef StgFunPtr       F_;
173 typedef StgByteArray    B_;
174 typedef StgClosurePtr   L_;
175
176 typedef StgInt64        LI_;
177 typedef StgWord64       LW_;
178
179 #define IF_(f)          static F_ GNUC3_ATTRIBUTE(used) f(void) 
180 #define FN_(f)          F_ f(void)
181 #define EF_(f)          extern F_ f(void)
182
183 typedef StgWord StgWordArray[];
184 #define EI_(X)          extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
185 #define II_(X)          static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
186
187 /* -----------------------------------------------------------------------------
188    Tail calls
189
190    This needs to be up near the top as the register line on alpha needs
191    to be before all procedures (inline & out-of-line).
192    -------------------------------------------------------------------------- */
193
194 #include "TailCalls.h"
195
196 /* -----------------------------------------------------------------------------
197    Other Stg stuff...
198    -------------------------------------------------------------------------- */
199
200 #include "StgDLL.h"
201 #include "MachRegs.h"
202 #include "Regs.h"
203
204 #ifdef TICKY_TICKY
205 #include "TickyCounters.h"
206 #endif
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 "StgMiscClosures.h"
214 #endif
215
216 #include "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 #ifdef SUPPORT_LONG_LONGS
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
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 */