6ddf17a0a07e201dee8791f0c945dc8bf18870e6
[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 #if IN_STG_CODE
158 /*
159  * This is included later for RTS sources, after definitions of
160  * StgInfoTable, StgClosure and so on. 
161  */
162 #include "StgMiscClosures.h"
163 #endif
164
165 #include "SMP.h" // write_barrier() inline is required 
166
167 /* -----------------------------------------------------------------------------
168    Moving Floats and Doubles
169
170    ASSIGN_FLT is for assigning a float to memory (usually the
171               stack/heap).  The memory address is guaranteed to be
172               StgWord aligned (currently == sizeof(void *)).
173
174    PK_FLT     is for pulling a float out of memory.  The memory is
175               guaranteed to be StgWord aligned.
176    -------------------------------------------------------------------------- */
177
178 INLINE_HEADER void        ASSIGN_FLT (W_ [], StgFloat);
179 INLINE_HEADER StgFloat    PK_FLT     (W_ []);
180
181 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
182
183 INLINE_HEADER void     ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
184 INLINE_HEADER StgFloat PK_FLT    (W_ p_src[])                { return *(StgFloat *)p_src; }
185
186 #else  /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
187
188 INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
189 {
190     float_thing y;
191     y.f = src;
192     *p_dest = y.fu;
193 }
194
195 INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
196 {
197     float_thing y;
198     y.fu = *p_src;
199     return(y.f);
200 }
201
202 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
203
204 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
205
206 INLINE_HEADER void        ASSIGN_DBL (W_ [], StgDouble);
207 INLINE_HEADER StgDouble   PK_DBL     (W_ []);
208
209 INLINE_HEADER void      ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
210 INLINE_HEADER StgDouble PK_DBL    (W_ p_src[])                 { return *(StgDouble *)p_src; }
211
212 #else   /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
213
214 /* Sparc uses two floating point registers to hold a double.  We can
215  * write ASSIGN_DBL and PK_DBL by directly accessing the registers
216  * independently - unfortunately this code isn't writable in C, we
217  * have to use inline assembler.
218  */
219 #if sparc_HOST_ARCH
220
221 #define ASSIGN_DBL(dst0,src) \
222     { StgPtr dst = (StgPtr)(dst0); \
223       __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
224         "=m" (((P_)(dst))[1]) : "f" (src)); \
225     }
226
227 #define PK_DBL(src0) \
228     ( { StgPtr src = (StgPtr)(src0); \
229         register double d; \
230       __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
231         "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
232     } )
233
234 #else /* ! sparc_HOST_ARCH */
235
236 INLINE_HEADER void        ASSIGN_DBL (W_ [], StgDouble);
237 INLINE_HEADER StgDouble   PK_DBL     (W_ []);
238
239 typedef struct
240   { StgWord dhi;
241     StgWord dlo;
242   } unpacked_double;
243
244 typedef union
245   { StgDouble d;
246     unpacked_double du;
247   } double_thing;
248
249 INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
250 {
251     double_thing y;
252     y.d = src;
253     p_dest[0] = y.du.dhi;
254     p_dest[1] = y.du.dlo;
255 }
256
257 /* GCC also works with this version, but it generates
258    the same code as the previous one, and is not ANSI
259
260 #define ASSIGN_DBL( p_dest, src ) \
261         *p_dest = ((double_thing) src).du.dhi; \
262         *(p_dest+1) = ((double_thing) src).du.dlo \
263 */
264
265 INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
266 {
267     double_thing y;
268     y.du.dhi = p_src[0];
269     y.du.dlo = p_src[1];
270     return(y.d);
271 }
272
273 #endif /* ! sparc_HOST_ARCH */
274
275 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
276
277
278 /* -----------------------------------------------------------------------------
279    Moving 64-bit quantities around
280
281    ASSIGN_Word64      assign an StgWord64/StgInt64 to a memory location
282    PK_Word64          load an StgWord64/StgInt64 from a amemory location
283
284    In both cases the memory location might not be 64-bit aligned.
285    -------------------------------------------------------------------------- */
286
287 #ifdef SUPPORT_LONG_LONGS
288
289 typedef struct
290   { StgWord dhi;
291     StgWord dlo;
292   } unpacked_double_word;
293
294 typedef union
295   { StgInt64 i;
296     unpacked_double_word iu;
297   } int64_thing;
298
299 typedef union
300   { StgWord64 w;
301     unpacked_double_word wu;
302   } word64_thing;
303
304 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
305 {
306     word64_thing y;
307     y.w = src;
308     p_dest[0] = y.wu.dhi;
309     p_dest[1] = y.wu.dlo;
310 }
311
312 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
313 {
314     word64_thing y;
315     y.wu.dhi = p_src[0];
316     y.wu.dlo = p_src[1];
317     return(y.w);
318 }
319
320 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
321 {
322     int64_thing y;
323     y.i = src;
324     p_dest[0] = y.iu.dhi;
325     p_dest[1] = y.iu.dlo;
326 }
327
328 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
329 {
330     int64_thing y;
331     y.iu.dhi = p_src[0];
332     y.iu.dlo = p_src[1];
333     return(y.i);
334 }
335
336 #elif SIZEOF_VOID_P == 8
337
338 INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
339 {
340         p_dest[0] = src;
341 }
342
343 INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
344 {
345     return p_src[0];
346 }
347
348 INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
349 {
350     p_dest[0] = src;
351 }
352
353 INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
354 {
355     return p_src[0];
356 }
357
358 #endif
359
360 /* -----------------------------------------------------------------------------
361    Split markers
362    -------------------------------------------------------------------------- */
363
364 #if defined(USE_SPLIT_MARKERS)
365 #if defined(LEADING_UNDERSCORE)
366 #define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
367 #else
368 #define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
369 #endif
370 #else
371 #define __STG_SPLIT_MARKER /* nothing */
372 #endif
373
374 /* -----------------------------------------------------------------------------
375    Write-combining store
376    -------------------------------------------------------------------------- */
377
378 INLINE_HEADER void
379 wcStore (StgPtr p, StgWord w)
380 {
381 #ifdef x86_64_HOST_ARCH    
382     __asm__(
383         "movnti\t%1, %0"
384         : "=m" (*p)
385         : "r" (w)
386         );
387 #else
388       *p = w;
389 #endif
390 }
391
392 /* -----------------------------------------------------------------------------
393    Integer multiply with overflow
394    -------------------------------------------------------------------------- */
395
396 /* Multiply with overflow checking.
397  *
398  * This is tricky - the usual sign rules for add/subtract don't apply.  
399  *
400  * On 32-bit machines we use gcc's 'long long' types, finding
401  * overflow with some careful bit-twiddling.
402  *
403  * On 64-bit machines where gcc's 'long long' type is also 64-bits,
404  * we use a crude approximation, testing whether either operand is
405  * larger than 32-bits; if neither is, then we go ahead with the
406  * multiplication.
407  *
408  * Return non-zero if there is any possibility that the signed multiply
409  * of a and b might overflow.  Return zero only if you are absolutely sure
410  * that it won't overflow.  If in doubt, return non-zero.
411  */
412
413 #if SIZEOF_VOID_P == 4
414
415 #ifdef WORDS_BIGENDIAN
416 #define RTS_CARRY_IDX__ 0
417 #define RTS_REM_IDX__  1
418 #else
419 #define RTS_CARRY_IDX__ 1
420 #define RTS_REM_IDX__ 0
421 #endif
422
423 typedef union {
424     StgInt64 l;
425     StgInt32 i[2];
426 } long_long_u ;
427
428 #define mulIntMayOflo(a,b)                      \
429 ({                                              \
430   StgInt32 r, c;                                \
431   long_long_u z;                                \
432   z.l = (StgInt64)a * (StgInt64)b;              \
433   r = z.i[RTS_REM_IDX__];                       \
434   c = z.i[RTS_CARRY_IDX__];                     \
435   if (c == 0 || c == -1) {                      \
436     c = ((StgWord)((a^b) ^ r))                  \
437       >> (BITS_IN (I_) - 1);                    \
438   }                                             \
439   c;                                            \
440 })
441
442 /* Careful: the carry calculation above is extremely delicate.  Make sure
443  * you test it thoroughly after changing it.
444  */
445
446 #else
447
448 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
449
450 /* If we have n-bit words then we have n-1 bits after accounting for the
451  * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
452 #define HALF_POS_INT  (((I_)1) << ((BITS_IN (I_) - 1) / 2))
453 #define HALF_NEG_INT  (-HALF_POS_INT)
454
455 #define mulIntMayOflo(a,b)                      \
456 ({                                              \
457   I_ c;                                         \
458   if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT    \
459       || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\
460     c = 1;                                      \
461   } else {                                      \
462     c = 0;                                      \
463   }                                             \
464   c;                                            \
465 })
466 #endif
467
468 #endif /* STG_H */