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