1 /* -----------------------------------------------------------------------------
3 * (c) The University of Glasgow 2004
5 * This file is included at the top of all .cmm source files (and
6 * *only* .cmm files). It defines a collection of useful macros for
7 * making .cmm code a bit less error-prone to write, and a bit easier
8 * on the eye for the reader.
10 * For the syntax of .cmm files, see the parser in ghc/compiler/cmm/CmmParse.y.
12 * If you're used to the old HC file syntax, here's a quick cheat sheet
13 * for converting HC code:
16 * - Remove all type casts
18 * - STGFUN(foo) { ... } ==> foo { ... }
19 * - FN_(foo) { ... } ==> foo { ... }
20 * - JMP_(e) ==> jump e;
21 * - Remove EXTFUN(foo)
24 * - Sp += n ==> Sp_adj(n)
25 * - Hp += n ==> Hp_adj(n)
26 * - R1.i ==> R1 (similarly for R1.w, R1.cl etc.)
27 * - You need to explicitly dereference variables; eg.
28 * context_switch ==> CInt[context_switch]
29 * - convert all word offsets into byte offsets:
31 * - sizeofW(StgFoo) ==> SIZEOF_StgFoo
32 * - ENTRY_CODE(e) ==> %ENTRY_CODE(e)
33 * - get_itbl(c) ==> %GET_STD_INFO(c)
34 * - Change liveness masks in STK_CHK_GEN, HP_CHK_GEN:
35 * R1_PTR | R2_PTR ==> R1_PTR & R2_PTR
37 * - Declarations like 'StgPtr p;' become just 'W_ p;'
38 * - e->payload[n] ==> PAYLOAD(e,n)
39 * - Be very careful with comparisons: the infix versions (>, >=, etc.)
40 * are unsigned, so use %lt(a,b) to get signed less-than for example.
42 * Accessing fields of structures defined in the RTS header files is
43 * done via automatically-generated macros in DerivedConstants.h. For
44 * example, where previously we used
46 * CurrentTSO->what_next = x
50 * StgTSO_what_next(CurrentTSO) = x
52 * where the StgTSO_what_next() macro is automatically generated by
53 * mkDerivedConstnants.c. If you need to access a field that doesn't
54 * already have a macro, edit that file (it's pretty self-explanatory).
56 * -------------------------------------------------------------------------- */
62 * In files that are included into both C and C-- (and perhaps
63 * Haskell) sources, we sometimes need to conditionally compile bits
64 * depending on the language. CMINUSMINUS==1 in .cmm sources:
68 #include "ghcconfig.h"
69 #include "RtsConfig.h"
71 /* -----------------------------------------------------------------------------
74 The following synonyms for C-- types are declared here:
76 I8, I16, I32, I64 MachRep-style names for convenience
78 W_ is shorthand for the word type (== StgWord)
79 F_ shorthand for float (F_ == StgFloat == C's float)
80 D_ shorthand for double (D_ == StgDouble == C's double)
82 CInt has the same size as an int in C on this platform
83 CLong has the same size as a long in C on this platform
85 --------------------------------------------------------------------------- */
92 #if SIZEOF_VOID_P == 4
94 #elif SIZEOF_VOID_P == 8
97 #error Unknown word size
102 #elif SIZEOF_INT == 8
105 #error Unknown int size
110 #elif SIZEOF_LONG == 8
113 #error Unknown long size
120 #define SIZEOF_StgDouble 8
121 #define SIZEOF_StgWord64 8
123 /* -----------------------------------------------------------------------------
125 -------------------------------------------------------------------------- */
129 #define STRING(name,str) \
131 name : bits8[] str; \
134 /* -----------------------------------------------------------------------------
137 Everything in C-- is in byte offsets (well, most things). We use
138 some macros to allow us to express offsets in words and to try to
139 avoid byte/word confusion.
140 -------------------------------------------------------------------------- */
142 #define SIZEOF_W SIZEOF_VOID_P
143 #define W_MASK (SIZEOF_W-1)
151 /* Converting quantities of words to bytes */
152 #define WDS(n) ((n)*SIZEOF_W)
155 * Converting quantities of bytes to words
156 * NB. these work on *unsigned* values only
158 #define BYTES_TO_WDS(n) ((n) / SIZEOF_W)
159 #define ROUNDUP_BYTES_TO_WDS(n) (((n) + SIZEOF_W - 1) / SIZEOF_W)
161 /* TO_W_(n) converts n to W_ type from a smaller type */
163 #define TO_W_(x) %sx32(x)
164 #define HALF_W_(x) %lobits16(x)
166 #define TO_W_(x) %sx64(x)
167 #define HALF_W_(x) %lobits32(x)
170 #if SIZEOF_INT == 4 && SIZEOF_W == 8
171 #define W_TO_INT(x) %lobits32(x)
172 #elif SIZEOF_INT == SIZEOF_W
173 #define W_TO_INT(x) (x)
176 /* -----------------------------------------------------------------------------
177 Heap/stack access, and adjusting the heap/stack pointers.
178 -------------------------------------------------------------------------- */
180 #define Sp(n) W_[Sp + WDS(n)]
181 #define Hp(n) W_[Hp + WDS(n)]
183 #define Sp_adj(n) Sp = Sp + WDS(n)
184 #define Hp_adj(n) Hp = Hp + WDS(n)
186 /* -----------------------------------------------------------------------------
187 Assertions and Debuggery
188 -------------------------------------------------------------------------- */
191 #define ASSERT(predicate) \
195 foreign "C" _assertFail(NULL, __LINE__); \
198 #define ASSERT(p) /* nothing */
202 #define DEBUG_ONLY(s) s
204 #define DEBUG_ONLY(s) /* nothing */
208 * The IF_DEBUG macro is useful for debug messages that depend on one
209 * of the RTS debug options. For example:
211 * IF_DEBUG(RtsFlags_DebugFlags_apply,
212 * foreign "C" fprintf(stderr, stg_ap_0_ret_str));
214 * Note the syntax is slightly different to the C version of this macro.
217 #define IF_DEBUG(c,s) if (RtsFlags_DebugFlags_##c(RtsFlags)) { s; }
219 #define IF_DEBUG(c,s) /* nothing */
222 /* -----------------------------------------------------------------------------
225 It isn't safe to "enter" every closure. Functions in particular
226 have no entry code as such; their entry point contains the code to
229 ToDo: range should end in N_CLOSURE_TYPES-1, not N_CLOSURE_TYPES,
230 but switch doesn't allow us to use exprs there yet.
231 -------------------------------------------------------------------------- */
235 switch [INVALID_OBJECT .. N_CLOSURE_TYPES] \
236 (TO_W_( %INFO_TYPE(%GET_STD_INFO(R1)) )) { \
244 R1 = StgInd_indirectee(R1); \
258 jump %ENTRY_CODE(Sp(0)); \
262 jump %GET_ENTRY(R1); \
266 /* -----------------------------------------------------------------------------
268 -------------------------------------------------------------------------- */
270 #include "Constants.h"
271 #include "DerivedConstants.h"
272 #include "ClosureTypes.h"
276 * Need MachRegs, because some of the RTS code is conditionally
277 * compiled based on REG_R1, REG_R2, etc.
279 #define STOLEN_X86_REGS 4
280 #include "MachRegs.h"
282 #include "Liveness.h"
283 #include "StgLdvProf.h"
287 #include "Block.h" /* For Bdescr() */
290 /* Can't think of a better place to put this. */
291 #if SIZEOF_mp_limb_t != SIZEOF_VOID_P
292 #error mp_limb_t != StgWord: assumptions in PrimOps.cmm are now false
295 #define MyCapability() (BaseReg - OFFSET_Capability_r)
297 /* -------------------------------------------------------------------------
298 Allocation and garbage collection
299 ------------------------------------------------------------------------- */
302 * ALLOC_PRIM is for allocating memory on the heap for a primitive
303 * object. It is used all over PrimOps.cmm.
305 * We make the simplifying assumption that the "admin" part of a
306 * primitive closure is just the header when calculating sizes for
307 * ticky-ticky. It's not clear whether eg. the size field of an array
308 * should be counted as "admin", or the various fields of a BCO.
310 #define ALLOC_PRIM(bytes,liveness,reentry) \
311 HP_CHK_GEN_TICKY(bytes,liveness,reentry); \
312 TICK_ALLOC_PRIM(SIZEOF_StgHeader,bytes-SIZEOF_StgHeader,0); \
315 /* CCS_ALLOC wants the size in words, because ccs->mem_alloc is in words */
316 #define CCCS_ALLOC(__alloc) CCS_ALLOC(BYTES_TO_WDS(__alloc), W_[CCCS])
318 #define HP_CHK_GEN_TICKY(alloc,liveness,reentry) \
319 HP_CHK_GEN(alloc,liveness,reentry); \
320 TICK_ALLOC_HEAP_NOCTR(alloc);
322 // allocateLocal() allocates from the nursery, so we check to see
323 // whether the nursery is nearly empty in any function that uses
324 // allocateLocal() - this includes many of the primops.
325 #define MAYBE_GC(liveness,reentry) \
326 if (bdescr_link(CurrentNursery) == NULL) { \
329 jump stg_gc_gen_hp; \
332 /* -----------------------------------------------------------------------------
334 -------------------------------------------------------------------------- */
337 * This is really ugly, since we don't do the rest of StgHeader this
338 * way. The problem is that values from DerivedConstants.h cannot be
339 * dependent on the way (SMP, PROF etc.). For SIZEOF_StgHeader we get
340 * the value from GHC, but it seems like too much trouble to do that
341 * for StgThunkHeader.
344 #define SIZEOF_StgThunkHeader SIZEOF_StgHeader+SIZEOF_StgSMPThunkHeader
346 #define SIZEOF_StgThunkHeader SIZEOF_StgHeader
349 #define StgThunk_payload(__ptr__,__ix__) \
350 W_[__ptr__+SIZEOF_StgThunkHeader+ WDS(__ix__)]
352 /* -----------------------------------------------------------------------------
354 -------------------------------------------------------------------------- */
356 /* The offset of the payload of an array */
357 #define BYTE_ARR_CTS(arr) ((arr) + SIZEOF_StgArrWords)
359 /* Getting/setting the info pointer of a closure */
360 #define SET_INFO(p,info) StgHeader_info(p) = info
361 #define GET_INFO(p) StgHeader_info(p)
363 /* Determine the size of an ordinary closure from its info table */
364 #define sizeW_fromITBL(itbl) \
365 SIZEOF_StgHeader + WDS(%INFO_PTRS(itbl)) + WDS(%INFO_NPTRS(itbl))
367 /* NB. duplicated from InfoTables.h! */
368 #define BITMAP_SIZE(bitmap) ((bitmap) & BITMAP_SIZE_MASK)
369 #define BITMAP_BITS(bitmap) ((bitmap) >> BITMAP_BITS_SHIFT)
371 /* Debugging macros */
372 #define LOOKS_LIKE_INFO_PTR(p) \
374 (TO_W_(%INFO_TYPE(%STD_INFO(p))) != INVALID_OBJECT) && \
375 (TO_W_(%INFO_TYPE(%STD_INFO(p))) < N_CLOSURE_TYPES))
377 #define LOOKS_LIKE_CLOSURE_PTR(p) (LOOKS_LIKE_INFO_PTR(GET_INFO(p)))
380 * The layout of the StgFunInfoExtra part of an info table changes
381 * depending on TABLES_NEXT_TO_CODE. So we define field access
382 * macros which use the appropriate version here:
384 #ifdef TABLES_NEXT_TO_CODE
386 * when TABLES_NEXT_TO_CODE, slow_apply is stored as an offset
387 * instead of the normal pointer.
390 #define StgFunInfoExtra_slow_apply(fun_info) \
391 (TO_W_(StgFunInfoExtraRev_slow_apply_offset(fun_info)) \
392 + (fun_info) + SIZEOF_StgFunInfoExtraRev + SIZEOF_StgInfoTable)
394 #define StgFunInfoExtra_fun_type(i) StgFunInfoExtraRev_fun_type(i)
395 #define StgFunInfoExtra_arity(i) StgFunInfoExtraRev_arity(i)
396 #define StgFunInfoExtra_bitmap(i) StgFunInfoExtraRev_bitmap(i)
398 #define StgFunInfoExtra_slow_apply(i) StgFunInfoExtraFwd_slow_apply(i)
399 #define StgFunInfoExtra_fun_type(i) StgFunInfoExtraFwd_fun_type(i)
400 #define StgFunInfoExtra_arity(i) StgFunInfoExtraFwd_arity(i)
401 #define StgFunInfoExtra_bitmap(i) StgFunInfoExtraFwd_bitmap(i)
404 /* -----------------------------------------------------------------------------
405 Voluntary Yields/Blocks
407 We only have a generic version of this at the moment - if it turns
408 out to be slowing us down we can make specialised ones.
409 -------------------------------------------------------------------------- */
411 #define YIELD(liveness,reentry) \
416 #define BLOCK(liveness,reentry) \
421 /* -----------------------------------------------------------------------------
423 -------------------------------------------------------------------------- */
426 #define TICK_BUMP_BY(ctr,n) CLong[ctr] = CLong[ctr] + n
428 #define TICK_BUMP_BY(ctr,n) /* nothing */
431 #define TICK_BUMP(ctr) TICK_BUMP_BY(ctr,1)
433 #define TICK_ENT_DYN_IND() TICK_BUMP(ENT_DYN_IND_ctr)
434 #define TICK_ENT_DYN_THK() TICK_BUMP(ENT_DYN_THK_ctr)
435 #define TICK_ENT_VIA_NODE() TICK_BUMP(ENT_VIA_NODE_ctr)
436 #define TICK_ENT_STATIC_IND() TICK_BUMP(ENT_STATIC_IND_ctr)
437 #define TICK_ENT_PERM_IND() TICK_BUMP(ENT_PERM_IND_ctr)
438 #define TICK_ENT_PAP() TICK_BUMP(ENT_PAP_ctr)
439 #define TICK_ENT_AP() TICK_BUMP(ENT_AP_ctr)
440 #define TICK_ENT_AP_STACK() TICK_BUMP(ENT_AP_STACK_ctr)
441 #define TICK_ENT_BH() TICK_BUMP(ENT_BH_ctr)
442 #define TICK_UNKNOWN_CALL() TICK_BUMP(UNKNOWN_CALL_ctr)
443 #define TICK_UPDF_PUSHED() TICK_BUMP(UPDF_PUSHED_ctr)
444 #define TICK_CATCHF_PUSHED() TICK_BUMP(CATCHF_PUSHED_ctr)
445 #define TICK_UPDF_OMITTED() TICK_BUMP(UPDF_OMITTED_ctr)
446 #define TICK_UPD_NEW_IND() TICK_BUMP(UPD_NEW_IND_ctr)
447 #define TICK_UPD_NEW_PERM_IND() TICK_BUMP(UPD_NEW_PERM_IND_ctr)
448 #define TICK_UPD_OLD_IND() TICK_BUMP(UPD_OLD_IND_ctr)
449 #define TICK_UPD_OLD_PERM_IND() TICK_BUMP(UPD_OLD_PERM_IND_ctr)
451 #define TICK_SLOW_CALL_FUN_TOO_FEW() TICK_BUMP(SLOW_CALL_FUN_TOO_FEW_ctr)
452 #define TICK_SLOW_CALL_FUN_CORRECT() TICK_BUMP(SLOW_CALL_FUN_CORRECT_ctr)
453 #define TICK_SLOW_CALL_FUN_TOO_MANY() TICK_BUMP(SLOW_CALL_FUN_TOO_MANY_ctr)
454 #define TICK_SLOW_CALL_PAP_TOO_FEW() TICK_BUMP(SLOW_CALL_PAP_TOO_FEW_ctr)
455 #define TICK_SLOW_CALL_PAP_CORRECT() TICK_BUMP(SLOW_CALL_PAP_CORRECT_ctr)
456 #define TICK_SLOW_CALL_PAP_TOO_MANY() TICK_BUMP(SLOW_CALL_PAP_TOO_MANY_ctr)
458 #define TICK_SLOW_CALL_v() TICK_BUMP(SLOW_CALL_v_ctr)
459 #define TICK_SLOW_CALL_p() TICK_BUMP(SLOW_CALL_p_ctr)
460 #define TICK_SLOW_CALL_pv() TICK_BUMP(SLOW_CALL_pv_ctr)
461 #define TICK_SLOW_CALL_pp() TICK_BUMP(SLOW_CALL_pp_ctr)
462 #define TICK_SLOW_CALL_ppp() TICK_BUMP(SLOW_CALL_ppp_ctr)
463 #define TICK_SLOW_CALL_pppp() TICK_BUMP(SLOW_CALL_pppp_ctr)
464 #define TICK_SLOW_CALL_ppppp() TICK_BUMP(SLOW_CALL_ppppp_ctr)
465 #define TICK_SLOW_CALL_pppppp() TICK_BUMP(SLOW_CALL_pppppp_ctr)
468 #define TICK_HISTO_BY(histo,n,i) \
474 CLong[histo##_hst + _idx*SIZEOF_LONG] \
475 = histo##_hst + __idx*SIZEOF_LONG] + i;
477 #define TICK_HISTO_BY(histo,n,i) /* nothing */
480 #define TICK_HISTO(histo,n) TICK_HISTO_BY(histo,n,1)
482 /* An unboxed tuple with n components. */
483 #define TICK_RET_UNBOXED_TUP(n) \
484 TICK_BUMP(RET_UNBOXED_TUP_ctr++); \
485 TICK_HISTO(RET_UNBOXED_TUP,n)
488 * A slow call with n arguments. In the unevald case, this call has
489 * already been counted once, so don't count it again.
491 #define TICK_SLOW_CALL(n) \
492 TICK_BUMP(SLOW_CALL_ctr); \
493 TICK_HISTO(SLOW_CALL,n)
496 * This slow call was found to be to an unevaluated function; undo the
497 * ticks we did in TICK_SLOW_CALL.
499 #define TICK_SLOW_CALL_UNEVALD(n) \
500 TICK_BUMP(SLOW_CALL_UNEVALD_ctr); \
501 TICK_BUMP_BY(SLOW_CALL_ctr,-1); \
502 TICK_HISTO_BY(SLOW_CALL,n,-1);
504 /* Updating a closure with a new CON */
505 #define TICK_UPD_CON_IN_NEW(n) \
506 TICK_BUMP(UPD_CON_IN_NEW_ctr); \
507 TICK_HISTO(UPD_CON_IN_NEW,n)
509 #define TICK_ALLOC_HEAP_NOCTR(n) \
510 TICK_BUMP(ALLOC_HEAP_ctr); \
511 TICK_BUMP_BY(ALLOC_HEAP_tot,n)
513 /* -----------------------------------------------------------------------------
515 -------------------------------------------------------------------------- */
517 #define TICK_MILLISECS (1000/TICK_FREQUENCY) /* ms per tick */