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