Stg.h 13.8 KB
Newer Older
1
/* -----------------------------------------------------------------------------
2
 *
3
 * (c) The GHC Team, 1998-2004
4
5
6
7
8
 *
 * Top-level include file for everything STG-ish.  
 *
 * This file is included *automatically* by all .hc files.
 *
9
10
11
12
13
 * NOTE: always include Stg.h *before* any other headers, because we
 * define some register variables which must be done before any inline
 * functions are defined (some system headers have been known to
 * define the odd inline function).
 *
14
15
16
17
18
19
20
 * We generally try to keep as little visible as possible when
 * compiling .hc files.  So for example the definitions of the
 * InfoTable structs, closure structs and other RTS types are not
 * visible here.  The compiler knows enough about the representations
 * of these types to generate code which manipulates them directly
 * with pointer arithmetic.
 *
21
22
23
24
25
 * ---------------------------------------------------------------------------*/

#ifndef STG_H
#define STG_H

26

27
28
29
30
31
/* If we include "Stg.h" directly, we're in STG code, and we therefore
 * get all the global register variables, macros etc. that go along
 * with that.  If "Stg.h" is included via "Rts.h", we're assumed to
 * be in vanilla C.
 */
32
33
#ifndef IN_STG_CODE
# define IN_STG_CODE 1
Ian Lynagh's avatar
Ian Lynagh committed
34

35
36
// Turn on C99 for .hc code.  This gives us the INFINITY and NAN
// constants from math.h, which we occasionally need to use in .hc (#1861)
Ian Lynagh's avatar
Ian Lynagh committed
37
# define _ISOC99_SOURCE
38
39
40
#endif

#if IN_STG_CODE == 0
41
# define NO_GLOBAL_REG_DECLS	/* don't define fixed registers */
42
43
#endif

44
/* Configuration */
45
46
#include "ghcconfig.h"
#include "RtsConfig.h"
sof's avatar
sof committed
47

48
49
50
51
52
/* The code generator calls the math functions directly in .hc code.
   NB. after configuration stuff above, because this sets #defines
   that depend on config info, such as __USE_FILE_OFFSET64 */
#include <math.h>

53
54
55
/* -----------------------------------------------------------------------------
   Useful definitions
   -------------------------------------------------------------------------- */
56

57
58
59
60
61
62
/*
 * The C backend like to refer to labels by just mentioning their
 * names.  Howevver, when a symbol is declared as a variable in C, the
 * C compiler will implicitly dereference it when it occurs in source.
 * So we must subvert this behaviour for .hc files by declaring
 * variables as arrays, which eliminates the implicit dereference.
63
 */
64
65
66
#if IN_STG_CODE
#define RTS_VAR(x) (x)[]
#define RTS_DEREF(x) (*(x))
67
#else
68
69
#define RTS_VAR(x) x
#define RTS_DEREF(x) x
70
71
#endif

72
/* bit macros
sof's avatar
sof committed
73
 */
74
75
#define BITS_PER_BYTE 8
#define BITS_IN(x) (BITS_PER_BYTE * sizeof(x))
sof's avatar
sof committed
76

77
78
79
80
/* Compute offsets of struct fields
 */
#define FIELD_OFFSET(s_type, field) ((StgWord)&(((s_type*)0)->field))

sof's avatar
sof committed
81
/*
82
 * 'Portable' inlining:
Simon Marlow's avatar
Simon Marlow committed
83
 * INLINE_HEADER is for inline functions in header files (macros)
84
 * STATIC_INLINE is for inline functions in source files
85
86
 * EXTERN_INLINE is for functions that we want to inline sometimes 
 * (we also compile a static version of the function; see Inlines.c)
sof's avatar
sof committed
87
 */
88
#if defined(__GNUC__) || defined( __INTEL_COMPILER)
Simon Marlow's avatar
Simon Marlow committed
89

sof's avatar
sof committed
90
91
92
# define INLINE_HEADER static inline
# define INLINE_ME inline
# define STATIC_INLINE INLINE_HEADER
Simon Marlow's avatar
Simon Marlow committed
93

94
95
96
97
98
// The special "extern inline" behaviour is now only supported by gcc
// when _GNUC_GNU_INLINE__ is defined, and you have to use
// __attribute__((gnu_inline)).  So when we don't have this, we use
// ordinary static inline.
//
99
100
101
102
103
// Apple's gcc defines __GNUC_GNU_INLINE__ without providing
// gnu_inline, so we exclude MacOS X and fall through to the safe
// version.
//
#if defined(__GNUC_GNU_INLINE__) && !defined(__APPLE__)
104
105
106
107
108
109
110
111
112
113
114
115
#  if defined(KEEP_INLINES)
#    define EXTERN_INLINE inline
#  else
#    define EXTERN_INLINE extern inline __attribute__((gnu_inline))
#  endif
#else
#  if defined(KEEP_INLINES)
#    define EXTERN_INLINE
#  else
#    define EXTERN_INLINE INLINE_HEADER
#  endif
#endif
Simon Marlow's avatar
Simon Marlow committed
116

sof's avatar
sof committed
117
#elif defined(_MSC_VER)
Simon Marlow's avatar
Simon Marlow committed
118

sof's avatar
sof committed
119
120
121
# define INLINE_HEADER __inline static
# define INLINE_ME __inline
# define STATIC_INLINE INLINE_HEADER
Simon Marlow's avatar
Simon Marlow committed
122
123
124
125
126
127
128

# if defined(KEEP_INLINES)
#  define EXTERN_INLINE __inline
# else
#  define EXTERN_INLINE __inline extern
# endif

sof's avatar
sof committed
129
#else
Simon Marlow's avatar
Simon Marlow committed
130

sof's avatar
sof committed
131
# error "Don't know how to inline functions with your C compiler."
Simon Marlow's avatar
Simon Marlow committed
132

sof's avatar
sof committed
133
134
#endif

Simon Marlow's avatar
Simon Marlow committed
135

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * GCC attributes
 */
#if defined(__GNUC__)
#define GNU_ATTRIBUTE(at) __attribute__((at))
#else
#define GNU_ATTRIBUTE(at)
#endif

#if __GNUC__ >= 3 
#define GNUC3_ATTRIBUTE(at) __attribute__((at))
#else
#define GNUC3_ATTRIBUTE(at)
#endif

Simon Marlow's avatar
Simon Marlow committed
151
152
153
154
155
156
#if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3
#define GNUC_ATTR_HOT __attribute__((hot))
#else
#define GNUC_ATTR_HOT /* nothing */
#endif

157
158
#define STG_UNUSED    GNUC3_ATTRIBUTE(__unused__)

159
/* -----------------------------------------------------------------------------
160
   Global type definitions
161
162
   -------------------------------------------------------------------------- */

163
#include "MachDeps.h"
164
165
#include "StgTypes.h"

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* -----------------------------------------------------------------------------
   Shorthand forms
   -------------------------------------------------------------------------- */

typedef StgChar		C_;
typedef StgWord		W_;
typedef StgWord*	P_;
typedef P_*		PP_;
typedef StgInt		I_;
typedef StgAddr	        A_;
typedef const StgWord*  D_;
typedef StgFunPtr       F_;
typedef StgByteArray    B_;
typedef StgClosurePtr   L_;

typedef StgInt64        LI_;
typedef StgWord64       LW_;

184
#define IF_(f)		static F_ GNUC3_ATTRIBUTE(used) f(void) 
185
186
#define FN_(f)		F_ f(void)
#define EF_(f)		extern F_ f(void)
187

188
typedef StgWord StgWordArray[];
189
190
#define EI_(X)          extern StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
#define II_(X)          static StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
191

192
193
194
195
196
197
/* -----------------------------------------------------------------------------
   Tail calls

   This needs to be up near the top as the register line on alpha needs
   to be before all procedures (inline & out-of-line).
   -------------------------------------------------------------------------- */
198

199
#include "TailCalls.h"
200

201
202
203
204
205
206
207
208
/* -----------------------------------------------------------------------------
   Other Stg stuff...
   -------------------------------------------------------------------------- */

#include "StgDLL.h"
#include "MachRegs.h"
#include "Regs.h"

Ian Lynagh's avatar
Ian Lynagh committed
209
210
211
212
#ifdef TICKY_TICKY
#include "TickyCounters.h"
#endif

213
214
215
216
217
218
219
220
#if IN_STG_CODE
/*
 * This is included later for RTS sources, after definitions of
 * StgInfoTable, StgClosure and so on. 
 */
#include "StgMiscClosures.h"
#endif

221
#include "SMP.h" // write_barrier() inline is required 
222

223
/* -----------------------------------------------------------------------------
224
225
226
227
228
229
230
231
   Moving Floats and Doubles

   ASSIGN_FLT is for assigning a float to memory (usually the
              stack/heap).  The memory address is guaranteed to be
	      StgWord aligned (currently == sizeof(void *)).

   PK_FLT     is for pulling a float out of memory.  The memory is
              guaranteed to be StgWord aligned.
232
233
   -------------------------------------------------------------------------- */

234
235
INLINE_HEADER void	  ASSIGN_FLT (W_ [], StgFloat);
INLINE_HEADER StgFloat    PK_FLT     (W_ []);
236

237
#if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
238

239
240
INLINE_HEADER void     ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
INLINE_HEADER StgFloat PK_FLT    (W_ p_src[])                { return *(StgFloat *)p_src; }
241

242
#else  /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
243

244
245
246
247
248
249
INLINE_HEADER void ASSIGN_FLT(W_ p_dest[], StgFloat src)
{
    float_thing y;
    y.f = src;
    *p_dest = y.fu;
}
250

251
252
253
254
255
256
257
258
259
260
261
262
263
INLINE_HEADER StgFloat PK_FLT(W_ p_src[])
{
    float_thing y;
    y.fu = *p_src;
    return(y.f);
}

#endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */

#if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG

INLINE_HEADER void	  ASSIGN_DBL (W_ [], StgDouble);
INLINE_HEADER StgDouble   PK_DBL     (W_ []);
264

265
266
INLINE_HEADER void      ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
INLINE_HEADER StgDouble PK_DBL    (W_ p_src[])                 { return *(StgDouble *)p_src; }
sof's avatar
sof committed
267

268
269
270
271
272
273
#else	/* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */

/* Sparc uses two floating point registers to hold a double.  We can
 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
 * independently - unfortunately this code isn't writable in C, we
 * have to use inline assembler.
274
 */
275
#if sparc_HOST_ARCH
276
277
278
279
280
281
282
283
284
285
286
287
288
289

#define ASSIGN_DBL(dst0,src) \
    { StgPtr dst = (StgPtr)(dst0); \
      __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
	"=m" (((P_)(dst))[1]) : "f" (src)); \
    }

#define PK_DBL(src0) \
    ( { StgPtr src = (StgPtr)(src0); \
        register double d; \
      __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
	"m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
    } )

290
#else /* ! sparc_HOST_ARCH */
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

INLINE_HEADER void	  ASSIGN_DBL (W_ [], StgDouble);
INLINE_HEADER StgDouble   PK_DBL     (W_ []);

typedef struct
  { StgWord dhi;
    StgWord dlo;
  } unpacked_double;

typedef union
  { StgDouble d;
    unpacked_double du;
  } double_thing;

INLINE_HEADER void ASSIGN_DBL(W_ p_dest[], StgDouble src)
{
    double_thing y;
    y.d = src;
    p_dest[0] = y.du.dhi;
    p_dest[1] = y.du.dlo;
}

/* GCC also works with this version, but it generates
   the same code as the previous one, and is not ANSI

#define ASSIGN_DBL( p_dest, src ) \
	*p_dest = ((double_thing) src).du.dhi; \
	*(p_dest+1) = ((double_thing) src).du.dlo \
*/
320

321
322
323
324
325
326
327
INLINE_HEADER StgDouble PK_DBL(W_ p_src[])
{
    double_thing y;
    y.du.dhi = p_src[0];
    y.du.dlo = p_src[1];
    return(y.d);
}
328

329
#endif /* ! sparc_HOST_ARCH */
330

331
#endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
332
333


334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* -----------------------------------------------------------------------------
   Moving 64-bit quantities around

   ASSIGN_Word64      assign an StgWord64/StgInt64 to a memory location
   PK_Word64          load an StgWord64/StgInt64 from a amemory location

   In both cases the memory location might not be 64-bit aligned.
   -------------------------------------------------------------------------- */

#ifdef SUPPORT_LONG_LONGS

typedef struct
  { StgWord dhi;
    StgWord dlo;
  } unpacked_double_word;

typedef union
  { StgInt64 i;
    unpacked_double_word iu;
  } int64_thing;

typedef union
  { StgWord64 w;
    unpacked_double_word wu;
  } word64_thing;

INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
{
    word64_thing y;
    y.w = src;
    p_dest[0] = y.wu.dhi;
    p_dest[1] = y.wu.dlo;
}

INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
{
    word64_thing y;
    y.wu.dhi = p_src[0];
    y.wu.dlo = p_src[1];
    return(y.w);
}

INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
{
    int64_thing y;
    y.i = src;
    p_dest[0] = y.iu.dhi;
    p_dest[1] = y.iu.dlo;
}

INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
{
    int64_thing y;
    y.iu.dhi = p_src[0];
    y.iu.dlo = p_src[1];
    return(y.i);
}

#elif SIZEOF_VOID_P == 8

INLINE_HEADER void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
{
	p_dest[0] = src;
}

INLINE_HEADER StgWord64 PK_Word64(W_ p_src[])
{
    return p_src[0];
}

INLINE_HEADER void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
{
    p_dest[0] = src;
}

INLINE_HEADER StgInt64 PK_Int64(W_ p_src[])
{
    return p_src[0];
}

#endif

/* -----------------------------------------------------------------------------
   Split markers
   -------------------------------------------------------------------------- */
419

420
421
422
423
424
425
426
427
428
429
#if defined(USE_SPLIT_MARKERS)
#if defined(LEADING_UNDERSCORE)
#define __STG_SPLIT_MARKER __asm__("\n___stg_split_marker:");
#else
#define __STG_SPLIT_MARKER __asm__("\n__stg_split_marker:");
#endif
#else
#define __STG_SPLIT_MARKER /* nothing */
#endif

430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* -----------------------------------------------------------------------------
   Write-combining store
   -------------------------------------------------------------------------- */

INLINE_HEADER void
wcStore (StgPtr p, StgWord w)
{
#ifdef x86_64_HOST_ARCH    
    __asm__(
	"movnti\t%1, %0"
	: "=m" (*p)
	: "r" (w)
	);
#else
      *p = w;
#endif
}

448
449
450
/* -----------------------------------------------------------------------------
   Integer multiply with overflow
   -------------------------------------------------------------------------- */
451

452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* Multiply with overflow checking.
 *
 * This is tricky - the usual sign rules for add/subtract don't apply.  
 *
 * On 32-bit machines we use gcc's 'long long' types, finding
 * overflow with some careful bit-twiddling.
 *
 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
 * we use a crude approximation, testing whether either operand is
 * larger than 32-bits; if neither is, then we go ahead with the
 * multiplication.
 *
 * Return non-zero if there is any possibility that the signed multiply
 * of a and b might overflow.  Return zero only if you are absolutely sure
 * that it won't overflow.  If in doubt, return non-zero.
 */
468

469
#if SIZEOF_VOID_P == 4
470

471
472
473
474
475
476
#ifdef WORDS_BIGENDIAN
#define RTS_CARRY_IDX__ 0
#define RTS_REM_IDX__  1
#else
#define RTS_CARRY_IDX__ 1
#define RTS_REM_IDX__ 0
sof's avatar
sof committed
477
478
#endif

479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
typedef union {
    StgInt64 l;
    StgInt32 i[2];
} long_long_u ;

#define mulIntMayOflo(a,b)			\
({                                              \
  StgInt32 r, c;				\
  long_long_u z;				\
  z.l = (StgInt64)a * (StgInt64)b;		\
  r = z.i[RTS_REM_IDX__];			\
  c = z.i[RTS_CARRY_IDX__];			\
  if (c == 0 || c == -1) {			\
    c = ((StgWord)((a^b) ^ r))			\
      >> (BITS_IN (I_) - 1);			\
  }						\
  c;                                            \
})

/* Careful: the carry calculation above is extremely delicate.  Make sure
 * you test it thoroughly after changing it.
 */

#else

504
/* Approximate version when we don't have long arithmetic (on 64-bit archs) */
505

506
507
508
/* If we have n-bit words then we have n-1 bits after accounting for the
 * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
#define HALF_POS_INT  (((I_)1) << ((BITS_IN (I_) - 1) / 2))
509
#define HALF_NEG_INT  (-HALF_POS_INT)
510
511
512
513

#define mulIntMayOflo(a,b)			\
({                                              \
  I_ c; 					\
514
515
  if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT    \
      || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\
516
517
518
519
520
521
522
    c = 1;					\
  } else {					\
    c = 0;					\
  }						\
  c;                                            \
})
#endif
sof's avatar
sof committed
523

524
#endif /* STG_H */