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