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