Constants.h 11.6 KB
Newer Older
1
/* ----------------------------------------------------------------------------
2
 *
3
 * (c) The GHC Team, 1998-2009
4 5 6
 *
 * Constants
 *
7 8 9 10 11 12 13
 * NOTE: this information is used by both the compiler and the RTS.
 * Some of it is tweakable, and some of it must be kept up to date
 * with various other parts of the system.
 *
 * Constants which are derived automatically from other definitions in
 * the system (eg. structure sizes) are generated into the file
 * DerivedConstants.h by a C program (mkDerivedConstantsHdr).
14
 *
15
 * To understand the structure of the RTS headers, see the wiki:
16
 *   https://gitlab.haskell.org/ghc/ghc/wikis/commentary/source-tree/includes
17
 *
18 19
 * -------------------------------------------------------------------------- */

20
#pragma once
21 22 23 24

/* -----------------------------------------------------------------------------
   Minimum closure sizes

25 26 27 28 29
   This is the minimum number of words in the payload of a heap-allocated
   closure, so that the closure has two bits in the bitmap for mark-compact
   collection.

   See Note [Mark bits in mark-compact collector] in rts/sm/Compact.h
30 31
   -------------------------------------------------------------------------- */

32
#define MIN_PAYLOAD_SIZE 1
33 34 35 36 37

/* -----------------------------------------------------------------------------
   Constants to do with specialised closure types.
   -------------------------------------------------------------------------- */

sof's avatar
sof committed
38
/* We have some pre-compiled selector thunks defined in rts/StgStdThunks.hc.
dterei's avatar
dterei committed
39
 * This constant defines the highest selectee index that we can replace with a
sof's avatar
sof committed
40
 * reference to the pre-compiled code.
41 42 43 44 45 46 47
 */

#define MAX_SPEC_SELECTEE_SIZE 15

/* Vector-apply thunks.  These thunks just push their free variables
 * on the stack and enter the first one.  They're a bit like PAPs, but
 * don't have a dynamic size.  We've pre-compiled a few to save
dterei's avatar
dterei committed
48
 * space.
49 50
 */

51
#define MAX_SPEC_AP_SIZE       7
52

53 54 55 56 57 58
/* Specialised FUN/THUNK/CONSTR closure types */

#define MAX_SPEC_THUNK_SIZE    2
#define MAX_SPEC_FUN_SIZE      2
#define MAX_SPEC_CONSTR_SIZE   2

dterei's avatar
dterei committed
59 60
/* Range of built-in table of static small int-like and char-like closures.
 *
61 62 63
 *   NB. This corresponds with the number of actual INTLIKE/CHARLIKE
 *   closures defined in rts/StgMiscClosures.cmm.
 */
64
#define MAX_INTLIKE             255
dterei's avatar
dterei committed
65
#define MIN_INTLIKE             (-16)
66

dterei's avatar
dterei committed
67 68
#define MAX_CHARLIKE            255
#define MIN_CHARLIKE            0
69

70 71 72 73 74 75 76
/* Each byte in the card table for an StgMutaArrPtrs covers
 * (1<<MUT_ARR_PTRS_CARD_BITS) elements in the array.  To find a good
 * value for this, I used the benchmarks nofib/gc/hash,
 * nofib/gc/graph, and nofib/gc/gc_bench.
 */
#define MUT_ARR_PTRS_CARD_BITS 7

77 78 79 80 81 82 83
/* -----------------------------------------------------------------------------
   STG Registers.

   Note that in MachRegs.h we define how many of these registers are
   *real* machine registers, and not just offsets in the Register Table.
   -------------------------------------------------------------------------- */

84
#define MAX_VANILLA_REG 10
85 86
#define MAX_FLOAT_REG   6
#define MAX_DOUBLE_REG  6
87
#define MAX_LONG_REG    1
88
#define MAX_XMM_REG     6
89 90 91 92 93 94 95 96

/* -----------------------------------------------------------------------------
   Semi-Tagging constants

   Old Comments about this stuff:

   Tags for indirection nodes and ``other'' (probably unevaluated) nodes;
   normal-form values of algebraic data types will have tags 0, 1, ...
dterei's avatar
dterei committed
97

98 99 100 101 102 103
   @INFO_IND_TAG@ is different from @INFO_OTHER_TAG@ just so we can count
   how often we bang into indirection nodes; that's all.  (WDP 95/11)

   ToDo: find out if we need any of this.
   -------------------------------------------------------------------------- */

dterei's avatar
dterei committed
104 105 106
#define INFO_OTHER_TAG          (-1)
#define INFO_IND_TAG            (-2)
#define INFO_FIRST_TAG          0
107 108 109

/* -----------------------------------------------------------------------------
   How much C stack to reserve for local temporaries when in the STG
sof's avatar
sof committed
110
   world.  Used in StgCRun.c.
111 112
   -------------------------------------------------------------------------- */

113
#define RESERVED_C_STACK_BYTES (2048 * SIZEOF_LONG)
114

115 116 117
/* -----------------------------------------------------------------------------
   How large is the stack frame saved by StgRun?
   world.  Used in StgCRun.c.
118 119 120 121 122

   The size has to be enough to save the registers (see StgCRun)
   plus padding if the result is not 16 byte aligned.
   See the Note [Stack Alignment on X86] in StgCRun.c for details.

123 124 125
   -------------------------------------------------------------------------- */
#if defined(x86_64_HOST_ARCH)
#  if defined(mingw32_HOST_OS)
126
#    define STG_RUN_STACK_FRAME_SIZE 144
127 128 129 130 131
#  else
#    define STG_RUN_STACK_FRAME_SIZE 48
#  endif
#endif

132 133 134 135 136 137 138 139 140 141 142 143 144
/* -----------------------------------------------------------------------------
   StgRun related labels shared between StgCRun.c and StgStartup.cmm.
   -------------------------------------------------------------------------- */

#if defined(LEADING_UNDERSCORE)
#define STG_RUN "_StgRun"
#define STG_RUN_JMP _StgRunJmp
#define STG_RETURN "_StgReturn"
#else
#define STG_RUN "StgRun"
#define STG_RUN_JMP StgRunJmp
#define STG_RETURN "StgReturn"
#endif
145

146 147 148
/* -----------------------------------------------------------------------------
   How much Haskell stack space to reserve for the saving of registers
   etc. in the case of a stack/heap overflow.
dterei's avatar
dterei committed
149

Herbert Valerio Riedel's avatar
Herbert Valerio Riedel committed
150
   This must be large enough to accommodate the largest stack frame
151
   pushed in one of the heap check fragments in HeapStackCheck.hc
152
   (ie. currently the generic heap checks - 3 words for StgRetDyn,
dterei's avatar
dterei committed
153
   18 words for the saved registers, see StgMacros.h).
154 155
   -------------------------------------------------------------------------- */

156
#define RESERVED_STACK_WORDS 21
157

158 159 160 161 162 163 164
/* -----------------------------------------------------------------------------
   The limit on the size of the stack check performed when we enter an
   AP_STACK, in words.  See raiseAsync() and bug #1466.
   -------------------------------------------------------------------------- */

#define AP_STACK_SPLIM 1024

165 166 167 168
/* -----------------------------------------------------------------------------
   Storage manager constants
   -------------------------------------------------------------------------- */

169
/* The size of a block (2^BLOCK_SHIFT bytes) */
170
#define BLOCK_SHIFT  12
171

172
/* The size of a megablock (2^MBLOCK_SHIFT bytes) */
173 174
#define MBLOCK_SHIFT   20

175 176 177
/* -----------------------------------------------------------------------------
   Bitmap/size fields (used in info tables)
   -------------------------------------------------------------------------- */
178

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
/* In a 32-bit bitmap field, we use 5 bits for the size, and 27 bits
 * for the bitmap.  If the bitmap requires more than 27 bits, then we
 * store it in a separate array, and leave a pointer in the bitmap
 * field.  On a 64-bit machine, the sizes are extended accordingly.
 */
#if SIZEOF_VOID_P == 4
#define BITMAP_SIZE_MASK     0x1f
#define BITMAP_BITS_SHIFT    5
#elif SIZEOF_VOID_P == 8
#define BITMAP_SIZE_MASK     0x3f
#define BITMAP_BITS_SHIFT    6
#else
#error unknown SIZEOF_VOID_P
#endif

194 195 196 197 198
/* -----------------------------------------------------------------------------
   Lag/Drag/Void constants
   -------------------------------------------------------------------------- */

/*
dterei's avatar
dterei committed
199 200
  An LDV word is divided into 3 parts: state bits (LDV_STATE_MASK), creation
  time bits (LDV_CREATE_MASK), and last use time bits (LDV_LAST_MASK).
201 202 203 204 205 206 207 208 209 210
 */
#if SIZEOF_VOID_P == 8
#define LDV_SHIFT               30
#define LDV_STATE_MASK          0x1000000000000000
#define LDV_CREATE_MASK         0x0FFFFFFFC0000000
#define LDV_LAST_MASK           0x000000003FFFFFFF
#define LDV_STATE_CREATE        0x0000000000000000
#define LDV_STATE_USE           0x1000000000000000
#else
#define LDV_SHIFT               15
dterei's avatar
dterei committed
211
#define LDV_STATE_MASK          0x40000000
212 213 214 215
#define LDV_CREATE_MASK         0x3FFF8000
#define LDV_LAST_MASK           0x00007FFF
#define LDV_STATE_CREATE        0x00000000
#define LDV_STATE_USE           0x40000000
216
#endif /* SIZEOF_VOID_P */
217 218 219 220 221 222 223 224 225

/* -----------------------------------------------------------------------------
   TSO related constants
   -------------------------------------------------------------------------- */

/*
 * Constants for the what_next field of a TSO, which indicates how it
 * is to be run.
 */
dterei's avatar
dterei committed
226 227 228
#define ThreadRunGHC    1       /* return to address on top of stack */
#define ThreadInterpret 2       /* interpret this thread */
#define ThreadKilled    3       /* thread has died, don't run it */
229
#define ThreadComplete  4       /* thread has finished */
230 231 232

/*
 * Constants for the why_blocked field of a TSO
233
 * NB. keep these in sync with GHC/Conc/Sync.hs: threadStatus
234 235 236
 */
#define NotBlocked          0
#define BlockedOnMVar       1
237 238 239 240 241 242
#define BlockedOnMVarRead   14 /* TODO: renumber me, see #9003 */
#define BlockedOnBlackHole  2
#define BlockedOnRead       3
#define BlockedOnWrite      4
#define BlockedOnDelay      5
#define BlockedOnSTM        6
243 244

/* Win32 only: */
245
#define BlockedOnDoProc     7
246

247
/* Only relevant for THREADED_RTS: */
248 249
#define BlockedOnCCall      10
#define BlockedOnCCall_Interruptible 11
250
   /* same as above but permit killing the worker thread */
251

252
/* Involved in a message sent to tso->msg_cap */
253
#define BlockedOnMsgThrowTo 12
254

dterei's avatar
dterei committed
255
/* The thread is not on any run queues, but can be woken up
256
   by tryWakeupThread() */
257 258
#define ThreadMigrating     13

259
/* Lightweight non-deadlock checked version of MVar.  Used for the why_blocked
260 261 262
   field of a TSO. Threads blocked for this reason are not forcibly release by
   the GC, as we expect them to be unblocked in the future based on outstanding
   IO events.  */
263 264 265
#define BlockedOnIOCompletion  15

/* Next number is 16.  */
266

267 268 269 270 271
/*
 * These constants are returned to the scheduler by a thread that has
 * stopped for one reason or another.  See typedef StgThreadReturnCode
 * in TSO.h.
 */
272
#define HeapOverflow   1                /* might also be StackOverflow */
273 274 275 276 277
#define StackOverflow  2
#define ThreadYielding 3
#define ThreadBlocked  4
#define ThreadFinished 5

dterei's avatar
dterei committed
278
/*
279 280 281 282 283 284 285 286 287 288 289 290 291
 * Flags for the tso->flags field.
 */

/*
 * TSO_LOCKED is set when a TSO is locked to a particular Capability.
 */
#define TSO_LOCKED  2

/*
 * TSO_BLOCKEX: the TSO is blocking exceptions
 *
 * TSO_INTERRUPTIBLE: the TSO can be interrupted if it blocks
 * interruptibly (eg. with BlockedOnMVar).
292 293
 *
 * TSO_STOPPED_ON_BREAKPOINT: the thread is currently stopped in a breakpoint
294 295 296
 */
#define TSO_BLOCKEX       4
#define TSO_INTERRUPTIBLE 8
dterei's avatar
dterei committed
297
#define TSO_STOPPED_ON_BREAKPOINT 16
298

Simon Marlow's avatar
Simon Marlow committed
299 300 301 302
/*
 * Used by the sanity checker to check whether TSOs are on the correct
 * mutable list.
 */
303 304
#define TSO_MARKED 64

305 306 307 308 309 310 311
/*
 * Used to communicate between stackSqueeze() and
 * threadStackOverflow() that a thread's stack was squeezed and the
 * stack may not need to be expanded.
 */
#define TSO_SQUEEZED 128

312 313 314 315 316 317
/*
 * Enables the AllocationLimitExceeded exception when the thread's
 * allocation limit goes negative.
 */
#define TSO_ALLOC_LIMIT 256

318 319 320 321 322 323 324 325 326
/*
 * The number of times we spin in a spin lock before yielding (see
 * #3758).  To tune this value, use the benchmark in #3758: run the
 * server with -N2 and the client both on a dual-core.  Also make sure
 * that the chosen value doesn't slow down any of the parallel
 * benchmarks in nofib/parallel.
 */
#define SPIN_COUNT 1000

327 328 329 330 331 332 333 334 335
/* -----------------------------------------------------------------------------
   Spare workers per Capability in the threaded RTS

   No more than MAX_SPARE_WORKERS will be kept in the thread pool
   associated with each Capability.
   -------------------------------------------------------------------------- */

#define MAX_SPARE_WORKERS 6

Simon Marlow's avatar
Simon Marlow committed
336 337 338 339 340
/*
 * The maximum number of NUMA nodes we support.  This is a fixed limit so that
 * we can have static arrays of this size in the RTS for speed.
 */
#define MAX_NUMA_NODES 16