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