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