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