1 /* ----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * Entry code for various built-in closure types.
7 * This file is written in a subset of C--, extended with various
8 * features specific to GHC. It is compiled by GHC directly. For the
9 * syntax of .cmm files, see the parser in ghc/compiler/cmm/CmmParse.y.
11 * --------------------------------------------------------------------------*/
15 import pthread_mutex_lock;
16 import ghczmprim_GHCziTypes_Czh_static_info;
17 import ghczmprim_GHCziTypes_Izh_static_info;
18 import EnterCriticalSection;
19 import LeaveCriticalSection;
21 /* ----------------------------------------------------------------------------
22 Support for the bytecode interpreter.
23 ------------------------------------------------------------------------- */
25 /* 9 bits of return code for constructors created by the interpreter. */
26 stg_interp_constr_entry
28 /* R1 points at the constructor */
29 jump %ENTRY_CODE(Sp(0));
32 /* Some info tables to be used when compiled code returns a value to
33 the interpreter, i.e. the interpreter pushes one of these onto the
34 stack before entering a value. What the code does is to
35 impedance-match the compiled return convention (in R1p/R1n/F1/D1 etc) to
36 the interpreter's convention (returned value is on top of stack),
37 and then cause the scheduler to enter the interpreter.
39 On entry, the stack (growing down) looks like this:
41 ptr to BCO holding return continuation
42 ptr to one of these info tables.
44 The info table code, both direct and vectored, must:
45 * push R1/F1/D1 on the stack, and its tag if necessary
46 * push the BCO (so it's now on the stack twice)
47 * Yield, ie, go to the scheduler.
49 Scheduler examines the t.o.s, discovers it is a BCO, and proceeds
50 directly to the bytecode interpreter. That pops the top element
51 (the BCO, containing the return continuation), and interprets it.
52 Net result: return continuation gets interpreted, with the
56 ptr to the info table just jumped thru
59 which is just what we want -- the "standard" return layout for the
62 Don't ask me how unboxed tuple returns are supposed to work. We
63 haven't got a good story about that yet.
66 INFO_TABLE_RET( stg_ctoi_R1p, RET_BCO)
70 Sp(0) = stg_enter_info;
71 jump stg_yield_to_interpreter;
75 * When the returned value is a pointer, but unlifted, in R1 ...
77 INFO_TABLE_RET( stg_ctoi_R1unpt, RET_BCO )
81 Sp(0) = stg_gc_unpt_r1_info;
82 jump stg_yield_to_interpreter;
86 * When the returned value is a non-pointer in R1 ...
88 INFO_TABLE_RET( stg_ctoi_R1n, RET_BCO )
92 Sp(0) = stg_gc_unbx_r1_info;
93 jump stg_yield_to_interpreter;
97 * When the returned value is in F1
99 INFO_TABLE_RET( stg_ctoi_F1, RET_BCO )
102 F_[Sp + WDS(1)] = F1;
103 Sp(0) = stg_gc_f1_info;
104 jump stg_yield_to_interpreter;
108 * When the returned value is in D1
110 INFO_TABLE_RET( stg_ctoi_D1, RET_BCO )
112 Sp_adj(-1) - SIZEOF_DOUBLE;
113 D_[Sp + WDS(1)] = D1;
114 Sp(0) = stg_gc_d1_info;
115 jump stg_yield_to_interpreter;
119 * When the returned value is in L1
121 INFO_TABLE_RET( stg_ctoi_L1, RET_BCO )
124 L_[Sp + WDS(1)] = L1;
125 Sp(0) = stg_gc_l1_info;
126 jump stg_yield_to_interpreter;
130 * When the returned value is a void
132 INFO_TABLE_RET( stg_ctoi_V, RET_BCO )
135 Sp(0) = stg_gc_void_info;
136 jump stg_yield_to_interpreter;
140 * Dummy info table pushed on the top of the stack when the interpreter
141 * should apply the BCO on the stack to its arguments, also on the
144 INFO_TABLE_RET( stg_apply_interp, RET_BCO )
146 /* Just in case we end up in here... (we shouldn't) */
147 jump stg_yield_to_interpreter;
150 /* ----------------------------------------------------------------------------
152 ------------------------------------------------------------------------- */
154 INFO_TABLE_FUN( stg_BCO, 4, 0, BCO, "BCO", "BCO", ARG_BCO )
156 /* entering a BCO means "apply it", same as a function */
159 Sp(0) = stg_apply_interp_info;
160 jump stg_yield_to_interpreter;
163 /* ----------------------------------------------------------------------------
164 Info tables for indirections.
166 SPECIALISED INDIRECTIONS: we have a specialised indirection for direct returns,
167 so that we can avoid entering
168 the object when we know it points directly to a value. The update
169 code (Updates.cmm) updates objects with the appropriate kind of
170 indirection. We only do this for young-gen indirections.
171 ------------------------------------------------------------------------- */
173 INFO_TABLE(stg_IND,1,0,IND,"IND","IND")
175 TICK_ENT_DYN_IND(); /* tick */
176 R1 = UNTAG(StgInd_indirectee(R1));
181 INFO_TABLE(stg_IND_direct,1,0,IND,"IND","IND")
183 TICK_ENT_DYN_IND(); /* tick */
184 R1 = StgInd_indirectee(R1);
186 jump %ENTRY_CODE(Sp(0));
189 INFO_TABLE(stg_IND_STATIC,1,0,IND_STATIC,"IND_STATIC","IND_STATIC")
191 TICK_ENT_STATIC_IND(); /* tick */
192 R1 = UNTAG(StgInd_indirectee(R1));
197 INFO_TABLE(stg_IND_PERM,1,0,IND_PERM,"IND_PERM","IND_PERM")
199 /* Don't add INDs to granularity cost */
201 /* Don't: TICK_ENT_STATIC_IND(Node); for ticky-ticky; this ind is
202 here only to help profiling */
204 #if defined(TICKY_TICKY) && !defined(PROFILING)
205 /* TICKY_TICKY && !PROFILING means PERM_IND *replaces* an IND, rather than
212 /* Enter PAP cost centre */
213 ENTER_CCS_PAP_CL(R1);
215 /* For ticky-ticky, change the perm_ind to a normal ind on first
216 * entry, so the number of ent_perm_inds is the number of *thunks*
217 * entered again, not the number of subsequent entries.
219 * Since this screws up cost centres, we die if profiling and
220 * ticky_ticky are on at the same time. KSW 1999-01.
224 # error Profiling and ticky-ticky do not mix at present!
225 # endif /* PROFILING */
226 StgHeader_info(R1) = stg_IND_info;
227 #endif /* TICKY_TICKY */
229 R1 = UNTAG(StgInd_indirectee(R1));
231 #if defined(TICKY_TICKY) && !defined(PROFILING)
239 INFO_TABLE(stg_IND_OLDGEN,1,0,IND_OLDGEN,"IND_OLDGEN","IND_OLDGEN")
241 TICK_ENT_STATIC_IND(); /* tick */
242 R1 = UNTAG(StgInd_indirectee(R1));
247 INFO_TABLE(stg_IND_OLDGEN_PERM,1,0,IND_OLDGEN_PERM,"IND_OLDGEN_PERM","IND_OLDGEN_PERM")
249 /* Don't: TICK_ENT_STATIC_IND(Node); for ticky-ticky;
250 this ind is here only to help profiling */
252 #if defined(TICKY_TICKY) && !defined(PROFILING)
253 /* TICKY_TICKY && !PROFILING means PERM_IND *replaces* an IND,
254 rather than being extra */
255 TICK_ENT_PERM_IND(); /* tick */
260 /* Enter PAP cost centre -- lexical scoping only */
261 ENTER_CCS_PAP_CL(R1);
263 /* see comment in IND_PERM */
266 # error Profiling and ticky-ticky do not mix at present!
267 # endif /* PROFILING */
268 StgHeader_info(R1) = stg_IND_OLDGEN_info;
269 #endif /* TICKY_TICKY */
271 R1 = UNTAG(StgInd_indirectee(R1));
277 /* ----------------------------------------------------------------------------
280 Entering a black hole normally causes a cyclic data dependency, but
281 in the concurrent world, black holes are synchronization points,
282 and they are turned into blocking queues when there are threads
283 waiting for the evaluation of the closure to finish.
284 ------------------------------------------------------------------------- */
286 INFO_TABLE(stg_BLACKHOLE,1,0,BLACKHOLE,"BLACKHOLE","BLACKHOLE")
288 W_ r, p, info, bq, msg, owner, bd;
290 TICK_ENT_DYN_IND(); /* tick */
293 p = StgInd_indirectee(R1);
294 if (GETTAG(p) != 0) {
296 jump %ENTRY_CODE(Sp(0));
299 info = StgHeader_info(p);
300 if (info == stg_IND_info) {
301 // This could happen, if e.g. we got a BLOCKING_QUEUE that has
302 // just been replaced with an IND by another thread in
303 // wakeBlockingQueue().
307 if (info == stg_TSO_info ||
308 info == stg_BLOCKING_QUEUE_CLEAN_info ||
309 info == stg_BLOCKING_QUEUE_DIRTY_info)
311 ("ptr" msg) = foreign "C" allocate(MyCapability() "ptr",
312 BYTES_TO_WDS(SIZEOF_MessageBlackHole)) [R1];
314 StgHeader_info(msg) = stg_MSG_BLACKHOLE_info;
315 MessageBlackHole_tso(msg) = CurrentTSO;
316 MessageBlackHole_bh(msg) = R1;
318 (r) = foreign "C" messageBlackHole(MyCapability() "ptr", msg "ptr") [R1];
323 StgTSO_why_blocked(CurrentTSO) = BlockedOnBlackHole::I16;
324 StgTSO_block_info(CurrentTSO) = msg;
325 jump stg_block_blackhole;
335 INFO_TABLE(__stg_EAGER_BLACKHOLE,1,0,BLACKHOLE,"BLACKHOLE","BLACKHOLE")
337 jump ENTRY_LBL(stg_BLACKHOLE);
340 // CAF_BLACKHOLE is allocated when entering a CAF. The reason it is
341 // distinct from BLACKHOLE is so that we can tell the difference
342 // between an update frame on the stack that points to a CAF under
343 // evaluation, and one that points to a closure that is under
344 // evaluation by another thread (a BLACKHOLE). See threadPaused().
346 INFO_TABLE(stg_CAF_BLACKHOLE,1,0,BLACKHOLE,"BLACKHOLE","BLACKHOLE")
348 jump ENTRY_LBL(stg_BLACKHOLE);
351 INFO_TABLE(stg_BLOCKING_QUEUE_CLEAN,4,0,BLOCKING_QUEUE,"BLOCKING_QUEUE","BLOCKING_QUEUE")
352 { foreign "C" barf("BLOCKING_QUEUE_CLEAN object entered!") never returns; }
355 INFO_TABLE(stg_BLOCKING_QUEUE_DIRTY,4,0,BLOCKING_QUEUE,"BLOCKING_QUEUE","BLOCKING_QUEUE")
356 { foreign "C" barf("BLOCKING_QUEUE_DIRTY object entered!") never returns; }
359 /* ----------------------------------------------------------------------------
360 Whiteholes are used for the "locked" state of a closure (see lockClosure())
361 ------------------------------------------------------------------------- */
363 INFO_TABLE(stg_WHITEHOLE, 0,0, WHITEHOLE, "WHITEHOLE", "WHITEHOLE")
365 #if defined(THREADED_RTS)
370 // spin until the WHITEHOLE is updated
371 info = StgHeader_info(R1);
372 if (info == stg_WHITEHOLE_info) {
374 if (i == SPIN_COUNT) {
376 foreign "C" yieldThread() [R1];
380 jump %ENTRY_CODE(info);
382 foreign "C" barf("WHITEHOLE object entered!") never returns;
386 /* ----------------------------------------------------------------------------
387 Some static info tables for things that don't get entered, and
388 therefore don't need entry code (i.e. boxed but unpointed objects)
389 NON_ENTERABLE_ENTRY_CODE now defined at the beginning of the file
390 ------------------------------------------------------------------------- */
392 INFO_TABLE(stg_TSO, 0,0,TSO, "TSO", "TSO")
393 { foreign "C" barf("TSO object entered!") never returns; }
395 /* ----------------------------------------------------------------------------
398 Live weak pointers have a special closure type. Dead ones are just
399 nullary constructors (although they live on the heap - we overwrite
400 live weak pointers with dead ones).
401 ------------------------------------------------------------------------- */
403 INFO_TABLE(stg_WEAK,1,4,WEAK,"WEAK","WEAK")
404 { foreign "C" barf("WEAK object entered!") never returns; }
407 * It's important when turning an existing WEAK into a DEAD_WEAK
408 * (which is what finalizeWeak# does) that we don't lose the link
409 * field and break the linked list of weak pointers. Hence, we give
410 * DEAD_WEAK 5 non-pointer fields.
412 INFO_TABLE_CONSTR(stg_DEAD_WEAK,0,5,0,CONSTR,"DEAD_WEAK","DEAD_WEAK")
413 { foreign "C" barf("DEAD_WEAK object entered!") never returns; }
415 /* ----------------------------------------------------------------------------
418 This is a static nullary constructor (like []) that we use to mark an empty
419 finalizer in a weak pointer object.
420 ------------------------------------------------------------------------- */
422 INFO_TABLE_CONSTR(stg_NO_FINALIZER,0,0,0,CONSTR_NOCAF_STATIC,"NO_FINALIZER","NO_FINALIZER")
423 { foreign "C" barf("NO_FINALIZER object entered!") never returns; }
425 CLOSURE(stg_NO_FINALIZER_closure,stg_NO_FINALIZER);
427 /* ----------------------------------------------------------------------------
428 Stable Names are unlifted too.
429 ------------------------------------------------------------------------- */
431 INFO_TABLE(stg_STABLE_NAME,0,1,PRIM,"STABLE_NAME","STABLE_NAME")
432 { foreign "C" barf("STABLE_NAME object entered!") never returns; }
434 /* ----------------------------------------------------------------------------
437 There are two kinds of these: full and empty. We need an info table
438 and entry code for each type.
439 ------------------------------------------------------------------------- */
441 INFO_TABLE(stg_MVAR_CLEAN,3,0,MVAR_CLEAN,"MVAR","MVAR")
442 { foreign "C" barf("MVAR object entered!") never returns; }
444 INFO_TABLE(stg_MVAR_DIRTY,3,0,MVAR_DIRTY,"MVAR","MVAR")
445 { foreign "C" barf("MVAR object entered!") never returns; }
447 /* -----------------------------------------------------------------------------
449 -------------------------------------------------------------------------- */
451 INFO_TABLE(stg_TVAR, 2, 1, MUT_PRIM, "TVAR", "TVAR")
452 { foreign "C" barf("TVAR object entered!") never returns; }
454 INFO_TABLE(stg_TVAR_WATCH_QUEUE, 3, 0, MUT_PRIM, "TVAR_WATCH_QUEUE", "TVAR_WATCH_QUEUE")
455 { foreign "C" barf("TVAR_WATCH_QUEUE object entered!") never returns; }
457 INFO_TABLE(stg_ATOMIC_INVARIANT, 2, 1, MUT_PRIM, "ATOMIC_INVARIANT", "ATOMIC_INVARIANT")
458 { foreign "C" barf("ATOMIC_INVARIANT object entered!") never returns; }
460 INFO_TABLE(stg_INVARIANT_CHECK_QUEUE, 3, 0, MUT_PRIM, "INVARIANT_CHECK_QUEUE", "INVARIANT_CHECK_QUEUE")
461 { foreign "C" barf("INVARIANT_CHECK_QUEUE object entered!") never returns; }
463 INFO_TABLE(stg_TREC_CHUNK, 0, 0, TREC_CHUNK, "TREC_CHUNK", "TREC_CHUNK")
464 { foreign "C" barf("TREC_CHUNK object entered!") never returns; }
466 INFO_TABLE(stg_TREC_HEADER, 3, 1, MUT_PRIM, "TREC_HEADER", "TREC_HEADER")
467 { foreign "C" barf("TREC_HEADER object entered!") never returns; }
469 INFO_TABLE_CONSTR(stg_END_STM_WATCH_QUEUE,0,0,0,CONSTR_NOCAF_STATIC,"END_STM_WATCH_QUEUE","END_STM_WATCH_QUEUE")
470 { foreign "C" barf("END_STM_WATCH_QUEUE object entered!") never returns; }
472 INFO_TABLE_CONSTR(stg_END_INVARIANT_CHECK_QUEUE,0,0,0,CONSTR_NOCAF_STATIC,"END_INVARIANT_CHECK_QUEUE","END_INVARIANT_CHECK_QUEUE")
473 { foreign "C" barf("END_INVARIANT_CHECK_QUEUE object entered!") never returns; }
475 INFO_TABLE_CONSTR(stg_END_STM_CHUNK_LIST,0,0,0,CONSTR_NOCAF_STATIC,"END_STM_CHUNK_LIST","END_STM_CHUNK_LIST")
476 { foreign "C" barf("END_STM_CHUNK_LIST object entered!") never returns; }
478 INFO_TABLE_CONSTR(stg_NO_TREC,0,0,0,CONSTR_NOCAF_STATIC,"NO_TREC","NO_TREC")
479 { foreign "C" barf("NO_TREC object entered!") never returns; }
481 CLOSURE(stg_END_STM_WATCH_QUEUE_closure,stg_END_STM_WATCH_QUEUE);
483 CLOSURE(stg_END_INVARIANT_CHECK_QUEUE_closure,stg_END_INVARIANT_CHECK_QUEUE);
485 CLOSURE(stg_END_STM_CHUNK_LIST_closure,stg_END_STM_CHUNK_LIST);
487 CLOSURE(stg_NO_TREC_closure,stg_NO_TREC);
489 /* ----------------------------------------------------------------------------
491 ------------------------------------------------------------------------- */
493 // PRIM rather than CONSTR, because PRIM objects cannot be duplicated by the GC.
494 INFO_TABLE_CONSTR(stg_MSG_WAKEUP,2,0,0,PRIM,"MSG_WAKEUP","MSG_WAKEUP")
495 { foreign "C" barf("MSG_WAKEUP object entered!") never returns; }
497 INFO_TABLE_CONSTR(stg_MSG_TRY_WAKEUP,2,0,0,PRIM,"MSG_TRY_WAKEUP","MSG_TRY_WAKEUP")
498 { foreign "C" barf("MSG_TRY_WAKEUP object entered!") never returns; }
500 INFO_TABLE_CONSTR(stg_MSG_THROWTO,4,0,0,PRIM,"MSG_THROWTO","MSG_THROWTO")
501 { foreign "C" barf("MSG_THROWTO object entered!") never returns; }
503 INFO_TABLE_CONSTR(stg_MSG_BLACKHOLE,3,0,0,PRIM,"MSG_BLACKHOLE","MSG_BLACKHOLE")
504 { foreign "C" barf("MSG_BLACKHOLE object entered!") never returns; }
506 /* ----------------------------------------------------------------------------
509 This is a static nullary constructor (like []) that we use to mark the
510 end of a linked TSO queue.
511 ------------------------------------------------------------------------- */
513 INFO_TABLE_CONSTR(stg_END_TSO_QUEUE,0,0,0,CONSTR_NOCAF_STATIC,"END_TSO_QUEUE","END_TSO_QUEUE")
514 { foreign "C" barf("END_TSO_QUEUE object entered!") never returns; }
516 CLOSURE(stg_END_TSO_QUEUE_closure,stg_END_TSO_QUEUE);
518 /* ----------------------------------------------------------------------------
521 These come in two basic flavours: arrays of data (StgArrWords) and arrays of
522 pointers (StgArrPtrs). They all have a similar layout:
524 ___________________________
525 | Info | No. of | data....
527 ---------------------------
529 These are *unpointed* objects: i.e. they cannot be entered.
531 ------------------------------------------------------------------------- */
533 INFO_TABLE(stg_ARR_WORDS, 0, 0, ARR_WORDS, "ARR_WORDS", "ARR_WORDS")
534 { foreign "C" barf("ARR_WORDS object entered!") never returns; }
536 INFO_TABLE(stg_MUT_ARR_PTRS_CLEAN, 0, 0, MUT_ARR_PTRS_CLEAN, "MUT_ARR_PTRS_CLEAN", "MUT_ARR_PTRS_CLEAN")
537 { foreign "C" barf("MUT_ARR_PTRS_CLEAN object entered!") never returns; }
539 INFO_TABLE(stg_MUT_ARR_PTRS_DIRTY, 0, 0, MUT_ARR_PTRS_DIRTY, "MUT_ARR_PTRS_DIRTY", "MUT_ARR_PTRS_DIRTY")
540 { foreign "C" barf("MUT_ARR_PTRS_DIRTY object entered!") never returns; }
542 INFO_TABLE(stg_MUT_ARR_PTRS_FROZEN, 0, 0, MUT_ARR_PTRS_FROZEN, "MUT_ARR_PTRS_FROZEN", "MUT_ARR_PTRS_FROZEN")
543 { foreign "C" barf("MUT_ARR_PTRS_FROZEN object entered!") never returns; }
545 INFO_TABLE(stg_MUT_ARR_PTRS_FROZEN0, 0, 0, MUT_ARR_PTRS_FROZEN0, "MUT_ARR_PTRS_FROZEN0", "MUT_ARR_PTRS_FROZEN0")
546 { foreign "C" barf("MUT_ARR_PTRS_FROZEN0 object entered!") never returns; }
548 /* ----------------------------------------------------------------------------
550 ------------------------------------------------------------------------- */
552 INFO_TABLE(stg_MUT_VAR_CLEAN, 1, 0, MUT_VAR_CLEAN, "MUT_VAR_CLEAN", "MUT_VAR_CLEAN")
553 { foreign "C" barf("MUT_VAR_CLEAN object entered!") never returns; }
554 INFO_TABLE(stg_MUT_VAR_DIRTY, 1, 0, MUT_VAR_DIRTY, "MUT_VAR_DIRTY", "MUT_VAR_DIRTY")
555 { foreign "C" barf("MUT_VAR_DIRTY object entered!") never returns; }
557 /* ----------------------------------------------------------------------------
560 Entering this closure will just return to the address on the top of the
561 stack. Useful for getting a thread in a canonical form where we can
562 just enter the top stack word to start the thread. (see deleteThread)
563 * ------------------------------------------------------------------------- */
565 INFO_TABLE( stg_dummy_ret, 0, 0, CONSTR_NOCAF_STATIC, "DUMMY_RET", "DUMMY_RET")
567 jump %ENTRY_CODE(Sp(0));
569 CLOSURE(stg_dummy_ret_closure,stg_dummy_ret);
571 /* ----------------------------------------------------------------------------
572 CHARLIKE and INTLIKE closures.
574 These are static representations of Chars and small Ints, so that
575 we can remove dynamic Chars and Ints during garbage collection and
576 replace them with references to the static objects.
577 ------------------------------------------------------------------------- */
579 #if defined(__PIC__) && defined(mingw32_TARGET_OS)
581 * When sticking the RTS in a Windows DLL, we delay populating the
582 * Charlike and Intlike tables until load-time, which is only
583 * when we've got the real addresses to the C# and I# closures.
585 * -- this is currently broken BL 2009/11/14.
586 * we don't rewrite to static closures at all with Windows DLLs.
588 // #warning Is this correct? _imp is a pointer!
589 #define Char_hash_static_info _imp__ghczmprim_GHCziTypes_Czh_static_info
590 #define Int_hash_static_info _imp__ghczmprim_GHCziTypes_Izh_static_info
592 #define Char_hash_static_info ghczmprim_GHCziTypes_Czh_static_info
593 #define Int_hash_static_info ghczmprim_GHCziTypes_Izh_static_info
597 #define CHARLIKE_HDR(n) CLOSURE(Char_hash_static_info, n)
598 #define INTLIKE_HDR(n) CLOSURE(Int_hash_static_info, n)
600 /* put these in the *data* section, since the garbage collector relies
601 * on the fact that static closures live in the data section.
604 /* end the name with _closure, to convince the mangler this is a closure */
606 #if !(defined(__PIC__) && defined(mingw32_HOST_OS))
608 stg_CHARLIKE_closure:
869 INTLIKE_HDR(-16) /* MIN_INTLIKE == -16 */
901 INTLIKE_HDR(16) /* MAX_INTLIKE == 16 */
904 #endif // !(defined(__PIC__) && defined(mingw32_HOST_OS))