1 /* -----------------------------------------------------------------------------
2 * $Id: StgMacros.h,v 1.10 1999/05/11 16:47:41 keithw Exp $
4 * (c) The GHC Team, 1998-1999
6 * Macros used for writing STG-ish C code.
8 * ---------------------------------------------------------------------------*/
13 /* -----------------------------------------------------------------------------
14 The following macros create function headers.
16 Each basic block is represented by a C function with no arguments.
17 We therefore always begin with either
25 The macros can be used either to define the function itself, or to provide
26 prototypes (by following with a ';').
28 Note: the various I*_ shorthands in the second block below are used to
29 declare forward references to local symbols. These shorthands *have* to
30 use the 'extern' type specifier and not 'static'. The reason for this is
31 that 'static' declares a reference as being a static/local variable,
32 and *not* as a forward reference to a static variable.
34 This might seem obvious, but it had me stumped as to why my info tables
35 were suddenly all filled with 0s.
39 --------------------------------------------------------------------------- */
41 #define STGFUN(f) StgFunPtr f(void)
42 #define EXTFUN(f) extern StgFunPtr f(void)
43 #define EXTFUN_RTS(f) extern DLL_IMPORT_RTS StgFunPtr f(void)
44 #define FN_(f) F_ f(void)
45 #define IFN_(f) static F_ f(void)
46 #define IF_(f) static F_ f(void)
47 #define EF_(f) extern F_ f(void)
48 #define EDF_(f) extern DLLIMPORT F_ f(void)
51 #define EDD_ extern DLLIMPORT
52 #define ED_RO_ extern const
54 #define ID_RO_ extern const
55 #define EI_ extern const StgInfoTable
56 #define EDI_ extern DLLIMPORT const StgInfoTable
57 #define II_ extern const StgInfoTable
58 #define EC_ extern StgClosure
59 #define EDC_ extern DLLIMPORT StgClosure
60 #define IC_ extern StgClosure
61 #define ECP_(x) extern const StgClosure *(x)[]
62 #define EDCP_(x) extern DLLIMPORT StgClosure *(x)[]
63 #define ICP_(x) extern const StgClosure *(x)[]
65 /* -----------------------------------------------------------------------------
68 For a block of non-pointer words on the stack, we precede the
69 block with a small-integer tag giving the number of non-pointer
71 -------------------------------------------------------------------------- */
74 #define ARGTAG_MAX 16 /* probably arbitrary */
75 #define ARG_TAG(n) (n)
76 #define ARG_SIZE(n) stgCast(StgWord,n)
80 INT_TAG = sizeofW(StgInt),
81 INT64_TAG = sizeofW(StgInt64),
82 WORD_TAG = sizeofW(StgWord),
83 ADDR_TAG = sizeofW(StgAddr),
84 CHAR_TAG = sizeofW(StgChar),
85 FLOAT_TAG = sizeofW(StgFloat),
86 DOUBLE_TAG = sizeofW(StgDouble),
87 STABLE_TAG = sizeofW(StgWord),
90 #else /* DEBUG_EXTRA */
103 ARGTAG_MAX = DOUBLE_TAG
106 /* putting this in a .h file generates many copies - but its only a
109 static StgWord stg_arg_size[] = {
111 [INT_TAG ] = sizeofW(StgInt),
112 [INT64_TAG ] = sizeofW(StgInt64),
113 [WORD_TAG ] = sizeofW(StgWord),
114 [ADDR_TAG ] = sizeofW(StgAddr),
115 [CHAR_TAG ] = sizeofW(StgChar),
116 [FLOAT_TAG ] = sizeofW(StgFloat),
117 [DOUBLE_TAG] = sizeofW(StgDouble),
118 [STABLE_TAG] = sizeofW(StgWord)
121 #define ARG_SIZE(tag) stg_arg_size[stgCast(StgWord,tag)]
123 #endif /* DEBUG_EXTRA */
125 static inline int IS_ARG_TAG( StgWord p );
126 static inline int IS_ARG_TAG( StgWord p ) { return p <= ARGTAG_MAX; }
128 /* -----------------------------------------------------------------------------
131 If (Sp + <n_args>) > Su { JMP_(stg_update_PAP); }
133 Sp points to the topmost used word on the stack, and Su points to
134 the most recently pushed update frame.
136 Remember that <n_args> must include any tagging of unboxed values.
138 ARGS_CHK_LOAD_NODE is for top-level functions, whose entry
139 convention doesn't require that Node is loaded with a pointer to
140 the closure. Thus we must load node before calling stg_updatePAP if
141 the argument check fails.
142 -------------------------------------------------------------------------- */
144 #define ARGS_CHK(n) \
145 if ((P_)(Sp + (n)) > (P_)Su) { \
146 JMP_(stg_update_PAP); \
149 #define ARGS_CHK_LOAD_NODE(n,closure) \
150 if ((P_)(Sp + (n)) > (P_)Su) { \
151 R1.p = (P_)closure; \
152 JMP_(stg_update_PAP); \
155 /* -----------------------------------------------------------------------------
158 When failing a check, we save a return address on the stack and
159 jump to a pre-compiled code fragment that saves the live registers
160 and returns to the scheduler.
162 The return address in most cases will be the beginning of the basic
163 block in which the check resides, since we need to perform the check
164 again on re-entry because someone else might have stolen the resource
166 ------------------------------------------------------------------------- */
168 #define STK_CHK(headroom,ret,r,layout,tag_assts) \
169 if (Sp - headroom < SpLim) { \
170 EXTFUN_RTS(stg_chk_##layout); \
173 JMP_(stg_chk_##layout); \
176 #define HP_CHK(headroom,ret,r,layout,tag_assts) \
177 if ((Hp += headroom) > HpLim) { \
178 EXTFUN_RTS(stg_chk_##layout); \
181 JMP_(stg_chk_##layout); \
183 TICK_ALLOC_HEAP(headroom);
185 #define HP_STK_CHK(stk_headroom,hp_headroom,ret,r,layout,tag_assts) \
186 if (Sp - stk_headroom < SpLim || (Hp += hp_headroom) > HpLim) { \
187 EXTFUN_RTS(stg_chk_##layout); \
190 JMP_(stg_chk_##layout); \
192 TICK_ALLOC_HEAP(hp_headroom);
194 /* -----------------------------------------------------------------------------
195 A Heap Check in a case alternative are much simpler: everything is
196 on the stack and covered by a liveness mask already, and there is
197 even a return address with an SRT info table there as well.
199 Just push R1 and return to the scheduler saying 'EnterGHC'
201 {STK,HP,HP_STK}_CHK_NP are the various checking macros for
202 bog-standard case alternatives, thunks, and non-top-level
203 functions. In all these cases, node points to a closure that we
204 can just enter to restart the heap check (the NP stands for 'node points').
206 HpLim points to the LAST WORD of valid allocation space.
207 -------------------------------------------------------------------------- */
209 #define STK_CHK_NP(headroom,ptrs,tag_assts) \
210 if ((Sp - (headroom)) < SpLim) { \
211 EXTFUN_RTS(stg_gc_enter_##ptrs); \
213 JMP_(stg_gc_enter_##ptrs); \
216 #define HP_CHK_NP(headroom,ptrs,tag_assts) \
217 if ((Hp += (headroom)) > HpLim) { \
218 EXTFUN_RTS(stg_gc_enter_##ptrs); \
220 JMP_(stg_gc_enter_##ptrs); \
222 TICK_ALLOC_HEAP(headroom);
224 #define HP_CHK_SEQ_NP(headroom,ptrs,tag_assts) \
225 if ((Hp += (headroom)) > HpLim) { \
226 EXTFUN_RTS(stg_gc_seq_##ptrs); \
228 JMP_(stg_gc_seq_##ptrs); \
230 TICK_ALLOC_HEAP(headroom);
232 #define HP_STK_CHK_NP(stk_headroom, hp_headroom, ptrs, tag_assts) \
233 if ((Sp - (stk_headroom)) < SpLim || (Hp += (hp_headroom)) > HpLim) { \
234 EXTFUN_RTS(stg_gc_enter_##ptrs); \
236 JMP_(stg_gc_enter_##ptrs); \
238 TICK_ALLOC_HEAP(hp_headroom);
240 /* Heap checks for branches of a primitive case / unboxed tuple return */
242 #define GEN_HP_CHK_ALT(headroom,lbl,tag_assts) \
243 if ((Hp += (headroom)) > HpLim) { \
248 TICK_ALLOC_HEAP(headroom);
250 #define HP_CHK_NOREGS(headroom,tag_assts) \
251 GEN_HP_CHK_ALT(headroom,stg_gc_noregs,tag_assts);
252 #define HP_CHK_UNPT_R1(headroom,tag_assts) \
253 GEN_HP_CHK_ALT(headroom,stg_gc_unpt_r1,tag_assts);
254 #define HP_CHK_UNBX_R1(headroom,tag_assts) \
255 GEN_HP_CHK_ALT(headroom,stg_gc_unbx_r1,tag_assts);
256 #define HP_CHK_F1(headroom,tag_assts) \
257 GEN_HP_CHK_ALT(headroom,stg_gc_f1,tag_assts);
258 #define HP_CHK_D1(headroom,tag_assts) \
259 GEN_HP_CHK_ALT(headroom,stg_gc_d1,tag_assts);
261 #define HP_CHK_L1(headroom,tag_assts) \
262 GEN_HP_CHK_ALT(headroom,stg_gc_d1,tag_assts);
264 #define HP_CHK_UT_ALT(headroom, ptrs, nptrs, r, ret, tag_assts) \
265 GEN_HP_CHK_ALT(headroom, stg_gc_ut_##ptrs##_##nptrs, \
266 tag_assts r = (P_)ret;)
268 /* -----------------------------------------------------------------------------
271 These are slow, but have the advantage of being usable in a variety
274 The one restriction is that any relevant SRTs must already be pointed
275 to from the stack. The return address doesn't need to have an info
276 table attached: hence it can be any old code pointer.
278 The liveness mask is a logical 'XOR' of NO_PTRS and zero or more
279 Rn_PTR constants defined below. All registers will be saved, but
280 the garbage collector needs to know which ones contain pointers.
282 Good places to use a generic heap check:
284 - case alternatives (the return address with an SRT is already
287 - primitives (no SRT required).
289 The stack layout is like this:
298 so the liveness mask depends on the size of an StgDouble (FltRegs
299 and R<n> are guaranteed to be 1 word in size).
301 -------------------------------------------------------------------------- */
303 /* VERY MAGIC CONSTANTS!
304 * must agree with code in HeapStackCheck.c, stg_gen_chk
307 #if SIZEOF_DOUBLE > SIZEOF_VOID_P
308 #define ALL_NON_PTRS 0xffff
309 #else /* SIZEOF_DOUBLE == SIZEOF_VOID_P */
310 #define ALL_NON_PTRS 0x3fff
313 #define LIVENESS_MASK(ptr_regs) (ALL_NON_PTRS ^ (ptr_regs))
324 #define HP_CHK_GEN(headroom,liveness,reentry,tag_assts) \
325 if ((Hp += (headroom)) > HpLim ) { \
328 R9.w = (W_)LIVENESS_MASK(liveness); \
329 R10.w = (W_)reentry; \
332 TICK_ALLOC_HEAP(headroom);
334 #define STK_CHK_GEN(headroom,liveness,reentry,tag_assts) \
335 if ((Sp - (headroom)) < SpLim) { \
338 R9.w = (W_)LIVENESS_MASK(liveness); \
339 R10.w = (W_)reentry; \
343 #define MAYBE_GC(liveness,reentry) \
344 if (doYouWantToGC()) { \
346 R9.w = (W_)LIVENESS_MASK(liveness); \
347 R10.w = (W_)reentry; \
351 /* -----------------------------------------------------------------------------
352 Voluntary Yields/Blocks
354 We only have a generic version of this at the moment - if it turns
355 out to be slowing us down we can make specialised ones.
356 -------------------------------------------------------------------------- */
361 #define YIELD(liveness,reentry) \
363 R9.w = (W_)LIVENESS_MASK(liveness); \
364 R10.w = (W_)reentry; \
365 JMP_(stg_gen_yield); \
368 #define BLOCK(liveness,reentry) \
370 R9.w = (W_)LIVENESS_MASK(liveness); \
371 R10.w = (W_)reentry; \
372 JMP_(stg_gen_block); \
375 #define BLOCK_NP(ptrs) \
377 EF_(stg_block_##ptrs); \
378 JMP_(stg_block_##ptrs); \
381 /* -----------------------------------------------------------------------------
382 CCall_GC needs to push a dummy stack frame containing the contents
383 of volatile registers and variables.
385 We use a RET_DYN frame the same as for a dynamic heap check.
386 ------------------------------------------------------------------------- */
389 EI_(stg_gen_chk_info);
391 EDI_(stg_gen_chk_info);
393 /* -----------------------------------------------------------------------------
396 RETVEC(p,t) where 'p' is a pointer to the info table for a
397 vectored return address, returns the address of the return code for
400 Return vectors are placed in *reverse order* immediately before the info
401 table for the return address. Hence the formula for computing the
402 actual return address is (addr - sizeof(InfoTable) - tag - 1).
403 The extra subtraction of one word is because tags start at zero.
404 -------------------------------------------------------------------------- */
406 #ifdef USE_MINIINTERPRETER
407 #define RET_VEC(p,t) ((*(stgCast(StgInfoTable*,p)->vector))[t])
409 #define RET_VEC(p,t) (*((P_)(p) - sizeofW(StgInfoTable) - t - 1))
412 /* -----------------------------------------------------------------------------
414 -------------------------------------------------------------------------- */
416 /* set the tag register (if we have one) */
417 #define SET_TAG(t) /* nothing */
419 #ifdef EAGER_BLACKHOLING
420 # define UPD_BH_UPDATABLE(thunk) \
421 TICK_UPD_BH_UPDATABLE(); \
422 SET_INFO((StgClosure *)thunk,&BLACKHOLE_info)
423 # define UPD_BH_SINGLE_ENTRY(thunk) \
424 TICK_UPD_BH_SINGLE_ENTRY(); \
425 SET_INFO((StgClosure *)thunk,&SE_BLACKHOLE_info)
426 #else /* !EAGER_BLACKHOLING */
427 # define UPD_BH_UPDATABLE(thunk) /* nothing */
428 # define UPD_BH_SINGLE_ENTRY(thunk) /* nothing */
429 #endif /* EAGER_BLACKHOLING */
431 /* -----------------------------------------------------------------------------
432 Moving Floats and Doubles
434 ASSIGN_FLT is for assigning a float to memory (usually the
435 stack/heap). The memory address is guaranteed to be
436 StgWord aligned (currently == sizeof(long)).
438 PK_FLT is for pulling a float out of memory. The memory is
439 guaranteed to be StgWord aligned.
440 -------------------------------------------------------------------------- */
442 static inline void ASSIGN_FLT (W_ [], StgFloat);
443 static inline StgFloat PK_FLT (W_ []);
445 #if ALIGNMENT_FLOAT <= ALIGNMENT_LONG
447 static inline void ASSIGN_FLT(W_ p_dest[], StgFloat src) { *(StgFloat *)p_dest = src; }
448 static inline StgFloat PK_FLT (W_ p_src[]) { return *(StgFloat *)p_src; }
450 #else /* ALIGNMENT_FLOAT > ALIGNMENT_UNSIGNED_INT */
452 static inline void ASSIGN_FLT(W_ p_dest[], StgFloat src)
459 static inline StgFloat PK_FLT(W_ p_src[])
466 #endif /* ALIGNMENT_FLOAT > ALIGNMENT_LONG */
468 #if ALIGNMENT_DOUBLE <= ALIGNMENT_LONG
470 static inline void ASSIGN_DBL (W_ [], StgDouble);
471 static inline StgDouble PK_DBL (W_ []);
473 static inline void ASSIGN_DBL(W_ p_dest[], StgDouble src) { *(StgDouble *)p_dest = src; }
474 static inline StgDouble PK_DBL (W_ p_src[]) { return *(StgDouble *)p_src; }
476 #else /* ALIGNMENT_DOUBLE > ALIGNMENT_LONG */
478 /* Sparc uses two floating point registers to hold a double. We can
479 * write ASSIGN_DBL and PK_DBL by directly accessing the registers
480 * independently - unfortunately this code isn't writable in C, we
481 * have to use inline assembler.
483 #if sparc_TARGET_ARCH
485 #define ASSIGN_DBL(dst0,src) \
486 { StgPtr dst = (StgPtr)(dst0); \
487 __asm__("st %2,%0\n\tst %R2,%1" : "=m" (((P_)(dst))[0]), \
488 "=m" (((P_)(dst))[1]) : "f" (src)); \
491 #define PK_DBL(src0) \
492 ( { StgPtr src = (StgPtr)(src0); \
494 __asm__("ld %1,%0\n\tld %2,%R0" : "=f" (d) : \
495 "m" (((P_)(src))[0]), "m" (((P_)(src))[1])); d; \
498 #else /* ! sparc_TARGET_ARCH */
500 static inline void ASSIGN_DBL (W_ [], StgDouble);
501 static inline StgDouble PK_DBL (W_ []);
513 static inline void ASSIGN_DBL(W_ p_dest[], StgDouble src)
517 p_dest[0] = y.du.dhi;
518 p_dest[1] = y.du.dlo;
521 /* GCC also works with this version, but it generates
522 the same code as the previous one, and is not ANSI
524 #define ASSIGN_DBL( p_dest, src ) \
525 *p_dest = ((double_thing) src).du.dhi; \
526 *(p_dest+1) = ((double_thing) src).du.dlo \
529 static inline StgDouble PK_DBL(W_ p_src[])
537 #endif /* ! sparc_TARGET_ARCH */
539 #endif /* ALIGNMENT_DOUBLE > ALIGNMENT_UNSIGNED_INT */
541 #ifdef SUPPORT_LONG_LONGS
546 } unpacked_double_word;
550 unpacked_double_word iu;
555 unpacked_double_word wu;
558 static inline void ASSIGN_Word64(W_ p_dest[], StgWord64 src)
562 p_dest[0] = y.wu.dhi;
563 p_dest[1] = y.wu.dlo;
566 static inline StgWord64 PK_Word64(W_ p_src[])
574 static inline void ASSIGN_Int64(W_ p_dest[], StgInt64 src)
578 p_dest[0] = y.iu.dhi;
579 p_dest[1] = y.iu.dlo;
582 static inline StgInt64 PK_Int64(W_ p_src[])
591 /* -----------------------------------------------------------------------------
593 -------------------------------------------------------------------------- */
595 extern DLL_IMPORT_DATA const StgPolyInfoTable catch_frame_info;
597 /* -----------------------------------------------------------------------------
600 A seq frame is very like an update frame, except that it doesn't do
602 -------------------------------------------------------------------------- */
604 extern DLL_IMPORT_DATA const StgPolyInfoTable seq_frame_info;
606 #define PUSH_SEQ_FRAME(sp) \
608 StgSeqFrame *__frame; \
609 TICK_SEQF_PUSHED(); \
610 __frame = (StgSeqFrame *)(sp); \
611 SET_HDR_(__frame,&seq_frame_info,CCCS); \
612 __frame->link = Su; \
613 Su = (StgUpdateFrame *)__frame; \
616 /* -----------------------------------------------------------------------------
618 -------------------------------------------------------------------------- */
620 #if defined(USE_SPLIT_MARKERS)
621 #define __STG_SPLIT_MARKER(n) FN_(__stg_split_marker##n) { }
623 #define __STG_SPLIT_MARKER(n) /* nothing */
626 /* -----------------------------------------------------------------------------
627 Closure and Info Macros with casting.
629 We don't want to mess around with casts in the generated C code, so
630 we use these casting versions of the closure/info tables macros.
631 -------------------------------------------------------------------------- */
633 #define SET_HDR_(c,info,ccs) \
634 SET_HDR((StgClosure *)(c),(StgInfoTable *)(info),ccs)
636 /* -----------------------------------------------------------------------------
637 Saving context for exit from the STG world, and loading up context
638 on entry to STG code.
640 We save all the STG registers (that is, the ones that are mapped to
641 machine registers) in their places in the TSO.
643 The stack registers go into the current stack object, and the heap
644 registers are saved in global locations.
645 -------------------------------------------------------------------------- */
647 static __inline__ void
648 SaveThreadState(void)
650 /* Don't need to save REG_Base, it won't have changed. */
654 CurrentTSO->splim = SpLim;
657 #if defined(PROFILING)
658 CurrentTSO->prof.CCCS = CCCS;
662 static __inline__ void
663 LoadThreadState (void)
666 BaseReg = (StgRegTable*)&MainRegTable;
671 SpLim = CurrentTSO->splim;
672 OpenNursery(Hp,HpLim);
674 # if defined(PROFILING)
675 CCCS = CurrentTSO->prof.CCCS;
679 #endif /* STGMACROS_H */