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