1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * Out-of-line primitive operations
7 * This file contains the implementations of all the primitive
8 * operations ("primops") which are not expanded inline. See
9 * ghc/compiler/prelude/primops.txt.pp for a list of all the primops;
10 * this file contains code for most of those with the attribute
13 * Entry convention: the entry convention for a primop is that all the
14 * args are in Stg registers (R1, R2, etc.). This is to make writing
15 * the primops easier. (see compiler/codeGen/CgCallConv.hs).
17 * Return convention: results from a primop are generally returned
18 * using the ordinary unboxed tuple return convention. The C-- parser
19 * implements the RET_xxxx() macros to perform unboxed-tuple returns
20 * based on the prevailing return convention.
22 * This file is written in a subset of C--, extended with various
23 * features specific to GHC. It is compiled by GHC directly. For the
24 * syntax of .cmm files, see the parser in ghc/compiler/cmm/CmmParse.y.
26 * ---------------------------------------------------------------------------*/
30 /*-----------------------------------------------------------------------------
33 Basically just new*Array - the others are all inline macros.
35 The size arg is always passed in R1, and the result returned in R1.
37 The slow entry point is for returning from a heap check, the saved
38 size argument must be re-loaded from the stack.
39 -------------------------------------------------------------------------- */
41 /* for objects that are *less* than the size of a word, make sure we
42 * round up to the nearest word for the size of the array.
47 W_ words, payload_words, n, p;
48 MAYBE_GC(NO_PTRS,newByteArrayzh_fast);
50 payload_words = ROUNDUP_BYTES_TO_WDS(n);
51 words = BYTES_TO_WDS(SIZEOF_StgArrWords) + payload_words;
52 "ptr" p = foreign "C" allocateLocal(BaseReg "ptr",words) [];
53 TICK_ALLOC_PRIM(SIZEOF_StgArrWords,WDS(payload_words),0);
54 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
55 StgArrWords_words(p) = payload_words;
59 newPinnedByteArrayzh_fast
61 W_ words, payload_words, n, p;
63 MAYBE_GC(NO_PTRS,newPinnedByteArrayzh_fast);
65 payload_words = ROUNDUP_BYTES_TO_WDS(n);
67 // We want an 8-byte aligned array. allocatePinned() gives us
68 // 8-byte aligned memory by default, but we want to align the
69 // *goods* inside the ArrWords object, so we have to check the
70 // size of the ArrWords header and adjust our size accordingly.
71 words = BYTES_TO_WDS(SIZEOF_StgArrWords) + payload_words;
72 if ((SIZEOF_StgArrWords & 7) != 0) {
76 "ptr" p = foreign "C" allocatePinned(words) [];
77 TICK_ALLOC_PRIM(SIZEOF_StgArrWords,WDS(payload_words),0);
79 // Again, if the ArrWords header isn't a multiple of 8 bytes, we
80 // have to push the object forward one word so that the goods
81 // fall on an 8-byte boundary.
82 if ((SIZEOF_StgArrWords & 7) != 0) {
86 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
87 StgArrWords_words(p) = payload_words;
93 W_ words, n, init, arr, p;
94 /* Args: R1 = words, R2 = initialisation value */
97 MAYBE_GC(R2_PTR,newArrayzh_fast);
99 words = BYTES_TO_WDS(SIZEOF_StgMutArrPtrs) + n;
100 "ptr" arr = foreign "C" allocateLocal(BaseReg "ptr",words) [];
101 TICK_ALLOC_PRIM(SIZEOF_StgMutArrPtrs, WDS(n), 0);
103 SET_HDR(arr, stg_MUT_ARR_PTRS_info, W_[CCCS]);
104 StgMutArrPtrs_ptrs(arr) = n;
106 // Initialise all elements of the the array with the value in R2
108 p = arr + SIZEOF_StgMutArrPtrs;
110 if (p < arr + WDS(words)) {
119 unsafeThawArrayzh_fast
121 // SUBTLETY TO DO WITH THE OLD GEN MUTABLE LIST
123 // A MUT_ARR_PTRS lives on the mutable list, but a MUT_ARR_PTRS_FROZEN
124 // normally doesn't. However, when we freeze a MUT_ARR_PTRS, we leave
125 // it on the mutable list for the GC to remove (removing something from
126 // the mutable list is not easy, because the mut_list is only singly-linked).
128 // So that we can tell whether a MUT_ARR_PTRS_FROZEN is on the mutable list,
129 // when we freeze it we set the info ptr to be MUT_ARR_PTRS_FROZEN0 to indicate
130 // that it is still on the mutable list.
132 // So, when we thaw a MUT_ARR_PTRS_FROZEN, we must cope with two cases:
133 // either it is on a mut_list, or it isn't. We adopt the convention that
134 // the closure type is MUT_ARR_PTRS_FROZEN0 if it is on the mutable list,
135 // and MUT_ARR_PTRS_FROZEN otherwise.
137 if (%INFO_TYPE(%GET_STD_INFO(R1)) != HALF_W_(MUT_ARR_PTRS_FROZEN0)) {
138 foreign "C" recordMutableLock(R1 "ptr") [R1];
141 SET_INFO(R1,stg_MUT_ARR_PTRS_info);
146 /* -----------------------------------------------------------------------------
148 -------------------------------------------------------------------------- */
153 /* Args: R1 = initialisation value */
155 ALLOC_PRIM( SIZEOF_StgMutVar, R1_PTR, newMutVarzh_fast);
157 mv = Hp - SIZEOF_StgMutVar + WDS(1);
158 SET_HDR(mv,stg_MUT_VAR_info,W_[CCCS]);
159 StgMutVar_var(mv) = R1;
164 atomicModifyMutVarzh_fast
167 /* Args: R1 :: MutVar#, R2 :: a -> (a,b) */
169 /* If x is the current contents of the MutVar#, then
170 We want to make the new contents point to
174 and the return value is
178 obviously we can share (f x).
180 z = [stg_ap_2 f x] (max (HS + 2) MIN_UPD_SIZE)
181 y = [stg_sel_0 z] (max (HS + 1) MIN_UPD_SIZE)
182 r = [stg_sel_1 z] (max (HS + 1) MIN_UPD_SIZE)
186 #define THUNK_1_SIZE (SIZEOF_StgThunkHeader + WDS(MIN_UPD_SIZE))
187 #define TICK_ALLOC_THUNK_1() TICK_ALLOC_UP_THK(WDS(1),WDS(MIN_UPD_SIZE-1))
189 #define THUNK_1_SIZE (SIZEOF_StgThunkHeader + WDS(1))
190 #define TICK_ALLOC_THUNK_1() TICK_ALLOC_UP_THK(WDS(1),0)
194 #define THUNK_2_SIZE (SIZEOF_StgThunkHeader + WDS(MIN_UPD_SIZE))
195 #define TICK_ALLOC_THUNK_2() TICK_ALLOC_UP_THK(WDS(2),WDS(MIN_UPD_SIZE-2))
197 #define THUNK_2_SIZE (SIZEOF_StgThunkHeader + WDS(2))
198 #define TICK_ALLOC_THUNK_2() TICK_ALLOC_UP_THK(WDS(2),0)
201 #define SIZE (THUNK_2_SIZE + THUNK_1_SIZE + THUNK_1_SIZE)
203 HP_CHK_GEN_TICKY(SIZE, R1_PTR & R2_PTR, atomicModifyMutVarzh_fast);
206 foreign "C" ACQUIRE_LOCK(sm_mutex "ptr");
209 x = StgMutVar_var(R1);
211 TICK_ALLOC_THUNK_2();
212 CCCS_ALLOC(THUNK_2_SIZE);
213 z = Hp - THUNK_2_SIZE + WDS(1);
214 SET_HDR(z, stg_ap_2_upd_info, W_[CCCS]);
215 LDV_RECORD_CREATE(z);
216 StgThunk_payload(z,0) = R2;
217 StgThunk_payload(z,1) = x;
219 TICK_ALLOC_THUNK_1();
220 CCCS_ALLOC(THUNK_1_SIZE);
221 y = z - THUNK_1_SIZE;
222 SET_HDR(y, stg_sel_0_upd_info, W_[CCCS]);
223 LDV_RECORD_CREATE(y);
224 StgThunk_payload(y,0) = z;
226 StgMutVar_var(R1) = y;
228 TICK_ALLOC_THUNK_1();
229 CCCS_ALLOC(THUNK_1_SIZE);
230 r = y - THUNK_1_SIZE;
231 SET_HDR(r, stg_sel_1_upd_info, W_[CCCS]);
232 LDV_RECORD_CREATE(r);
233 StgThunk_payload(r,0) = z;
236 foreign "C" RELEASE_LOCK(sm_mutex "ptr") [];
242 /* -----------------------------------------------------------------------------
243 Weak Pointer Primitives
244 -------------------------------------------------------------------------- */
246 STRING(stg_weak_msg,"New weak pointer at %p\n")
252 R3 = finalizer (or NULL)
257 R3 = stg_NO_FINALIZER_closure;
260 ALLOC_PRIM( SIZEOF_StgWeak, R1_PTR & R2_PTR & R3_PTR, mkWeakzh_fast );
262 w = Hp - SIZEOF_StgWeak + WDS(1);
263 SET_HDR(w, stg_WEAK_info, W_[CCCS]);
266 StgWeak_value(w) = R2;
267 StgWeak_finalizer(w) = R3;
269 StgWeak_link(w) = W_[weak_ptr_list];
270 W_[weak_ptr_list] = w;
272 IF_DEBUG(weak, foreign "C" debugBelch(stg_weak_msg,w) []);
287 if (GET_INFO(w) == stg_DEAD_WEAK_info) {
288 RET_NP(0,stg_NO_FINALIZER_closure);
294 // A weak pointer is inherently used, so we do not need to call
295 // LDV_recordDead_FILL_SLOP_DYNAMIC():
296 // LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)w);
297 // or, LDV_recordDead():
298 // LDV_recordDead((StgClosure *)w, sizeofW(StgWeak) - sizeofW(StgProfHeader));
299 // Furthermore, when PROFILING is turned on, dead weak pointers are exactly as
300 // large as weak pointers, so there is no need to fill the slop, either.
301 // See stg_DEAD_WEAK_info in StgMiscClosures.hc.
305 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
307 SET_INFO(w,stg_DEAD_WEAK_info);
308 LDV_RECORD_CREATE(w);
310 f = StgWeak_finalizer(w);
311 StgDeadWeak_link(w) = StgWeak_link(w);
313 /* return the finalizer */
314 if (f == stg_NO_FINALIZER_closure) {
315 RET_NP(0,stg_NO_FINALIZER_closure);
327 if (GET_INFO(w) == stg_WEAK_info) {
329 val = StgWeak_value(w);
337 /* -----------------------------------------------------------------------------
338 Arbitrary-precision Integer operations.
340 There are some assumptions in this code that mp_limb_t == W_. This is
341 the case for all the platforms that GHC supports, currently.
342 -------------------------------------------------------------------------- */
346 /* arguments: R1 = Int# */
348 W_ val, s, p; /* to avoid aliasing */
351 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(1), NO_PTRS, int2Integerzh_fast );
353 p = Hp - SIZEOF_StgArrWords;
354 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
355 StgArrWords_words(p) = 1;
357 /* mpz_set_si is inlined here, makes things simpler */
370 /* returns (# size :: Int#,
379 /* arguments: R1 = Word# */
381 W_ val, s, p; /* to avoid aliasing */
385 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(1), NO_PTRS, word2Integerzh_fast);
387 p = Hp - SIZEOF_StgArrWords;
388 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
389 StgArrWords_words(p) = 1;
398 /* returns (# size :: Int#,
399 data :: ByteArray# #)
406 * 'long long' primops for converting to/from Integers.
409 #ifdef SUPPORT_LONG_LONGS
411 int64ToIntegerzh_fast
413 /* arguments: L1 = Int64# */
416 W_ hi, s, neg, words_needed, p;
421 if ( %ge(val,0x100000000::L_) || %le(val,-0x100000000::L_) ) {
424 // minimum is one word
428 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(words_needed),
429 NO_PTRS, int64ToIntegerzh_fast );
431 p = Hp - SIZEOF_StgArrWords - WDS(words_needed) + WDS(1);
432 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
433 StgArrWords_words(p) = words_needed;
435 if ( %lt(val,0::L_) ) {
440 hi = TO_W_(val >> 32);
442 if ( words_needed == 2 ) {
447 if ( val != 0::L_ ) {
450 } else /* val==0 */ {
458 /* returns (# size :: Int#,
459 data :: ByteArray# #)
464 word64ToIntegerzh_fast
466 /* arguments: L1 = Word64# */
469 W_ hi, s, words_needed, p;
472 if ( val >= 0x100000000::L_ ) {
478 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(words_needed),
479 NO_PTRS, word64ToIntegerzh_fast );
481 p = Hp - SIZEOF_StgArrWords - WDS(words_needed) + WDS(1);
482 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
483 StgArrWords_words(p) = words_needed;
485 hi = TO_W_(val >> 32);
486 if ( val >= 0x100000000::L_ ) {
491 if ( val != 0::L_ ) {
494 } else /* val==0 */ {
499 /* returns (# size :: Int#,
500 data :: ByteArray# #)
506 #endif /* SUPPORT_LONG_LONGS */
508 /* ToDo: this is shockingly inefficient */
513 bits8 [SIZEOF_MP_INT];
518 bits8 [SIZEOF_MP_INT];
523 bits8 [SIZEOF_MP_INT];
528 bits8 [SIZEOF_MP_INT];
533 #define FETCH_MP_TEMP(X) \
535 X = BaseReg + (OFFSET_StgRegTable_r ## X);
537 #define FETCH_MP_TEMP(X) /* Nothing */
540 #define GMP_TAKE2_RET1(name,mp_fun) \
545 FETCH_MP_TEMP(mp_tmp1); \
546 FETCH_MP_TEMP(mp_tmp2); \
547 FETCH_MP_TEMP(mp_result1) \
548 FETCH_MP_TEMP(mp_result2); \
550 /* call doYouWantToGC() */ \
551 MAYBE_GC(R2_PTR & R4_PTR, name); \
558 MP_INT__mp_alloc(mp_tmp1) = W_TO_INT(StgArrWords_words(d1)); \
559 MP_INT__mp_size(mp_tmp1) = (s1); \
560 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(d1); \
561 MP_INT__mp_alloc(mp_tmp2) = W_TO_INT(StgArrWords_words(d2)); \
562 MP_INT__mp_size(mp_tmp2) = (s2); \
563 MP_INT__mp_d(mp_tmp2) = BYTE_ARR_CTS(d2); \
565 foreign "C" mpz_init(mp_result1 "ptr") []; \
567 /* Perform the operation */ \
568 foreign "C" mp_fun(mp_result1 "ptr",mp_tmp1 "ptr",mp_tmp2 "ptr") []; \
570 RET_NP(TO_W_(MP_INT__mp_size(mp_result1)), \
571 MP_INT__mp_d(mp_result1) - SIZEOF_StgArrWords); \
574 #define GMP_TAKE1_RET1(name,mp_fun) \
579 FETCH_MP_TEMP(mp_tmp1); \
580 FETCH_MP_TEMP(mp_result1) \
582 /* call doYouWantToGC() */ \
583 MAYBE_GC(R2_PTR, name); \
588 MP_INT__mp_alloc(mp_tmp1) = W_TO_INT(StgArrWords_words(d1)); \
589 MP_INT__mp_size(mp_tmp1) = (s1); \
590 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(d1); \
592 foreign "C" mpz_init(mp_result1 "ptr") []; \
594 /* Perform the operation */ \
595 foreign "C" mp_fun(mp_result1 "ptr",mp_tmp1 "ptr") []; \
597 RET_NP(TO_W_(MP_INT__mp_size(mp_result1)), \
598 MP_INT__mp_d(mp_result1) - SIZEOF_StgArrWords); \
601 #define GMP_TAKE2_RET2(name,mp_fun) \
606 FETCH_MP_TEMP(mp_tmp1); \
607 FETCH_MP_TEMP(mp_tmp2); \
608 FETCH_MP_TEMP(mp_result1) \
609 FETCH_MP_TEMP(mp_result2) \
611 /* call doYouWantToGC() */ \
612 MAYBE_GC(R2_PTR & R4_PTR, name); \
619 MP_INT__mp_alloc(mp_tmp1) = W_TO_INT(StgArrWords_words(d1)); \
620 MP_INT__mp_size(mp_tmp1) = (s1); \
621 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(d1); \
622 MP_INT__mp_alloc(mp_tmp2) = W_TO_INT(StgArrWords_words(d2)); \
623 MP_INT__mp_size(mp_tmp2) = (s2); \
624 MP_INT__mp_d(mp_tmp2) = BYTE_ARR_CTS(d2); \
626 foreign "C" mpz_init(mp_result1 "ptr") []; \
627 foreign "C" mpz_init(mp_result2 "ptr") []; \
629 /* Perform the operation */ \
630 foreign "C" mp_fun(mp_result1 "ptr",mp_result2 "ptr",mp_tmp1 "ptr",mp_tmp2 "ptr") []; \
632 RET_NPNP(TO_W_(MP_INT__mp_size(mp_result1)), \
633 MP_INT__mp_d(mp_result1) - SIZEOF_StgArrWords, \
634 TO_W_(MP_INT__mp_size(mp_result2)), \
635 MP_INT__mp_d(mp_result2) - SIZEOF_StgArrWords); \
638 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add)
639 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub)
640 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul)
641 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd)
642 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q)
643 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r)
644 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact)
645 GMP_TAKE2_RET1(andIntegerzh_fast, mpz_and)
646 GMP_TAKE2_RET1(orIntegerzh_fast, mpz_ior)
647 GMP_TAKE2_RET1(xorIntegerzh_fast, mpz_xor)
648 GMP_TAKE1_RET1(complementIntegerzh_fast, mpz_com)
650 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr)
651 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr)
655 mp_tmp_w: W_; // NB. mp_tmp_w is really an here mp_limb_t
661 /* R1 = the first Int#; R2 = the second Int# */
663 FETCH_MP_TEMP(mp_tmp_w);
666 r = foreign "C" mpn_gcd_1(mp_tmp_w "ptr", 1, R2) [];
669 /* Result parked in R1, return via info-pointer at TOS */
670 jump %ENTRY_CODE(Sp(0));
676 /* R1 = s1; R2 = d1; R3 = the int */
677 R1 = foreign "C" mpn_gcd_1( BYTE_ARR_CTS(R2) "ptr", R1, R3) [];
679 /* Result parked in R1, return via info-pointer at TOS */
680 jump %ENTRY_CODE(Sp(0));
686 /* R1 = s1; R2 = d1; R3 = the int */
687 W_ usize, vsize, v_digit, u_digit;
693 // paraphrased from mpz_cmp_si() in the GMP sources
694 if (%gt(v_digit,0)) {
697 if (%lt(v_digit,0)) {
703 if (usize != vsize) {
705 jump %ENTRY_CODE(Sp(0));
710 jump %ENTRY_CODE(Sp(0));
713 u_digit = W_[BYTE_ARR_CTS(R2)];
715 if (u_digit == v_digit) {
717 jump %ENTRY_CODE(Sp(0));
720 if (%gtu(u_digit,v_digit)) { // NB. unsigned: these are mp_limb_t's
726 jump %ENTRY_CODE(Sp(0));
731 /* R1 = s1; R2 = d1; R3 = s2; R4 = d2 */
732 W_ usize, vsize, size, up, vp;
735 // paraphrased from mpz_cmp() in the GMP sources
739 if (usize != vsize) {
741 jump %ENTRY_CODE(Sp(0));
746 jump %ENTRY_CODE(Sp(0));
749 if (%lt(usize,0)) { // NB. not <, which is unsigned
755 up = BYTE_ARR_CTS(R2);
756 vp = BYTE_ARR_CTS(R4);
758 cmp = foreign "C" mpn_cmp(up "ptr", vp "ptr", size) [];
760 if (cmp == 0 :: CInt) {
762 jump %ENTRY_CODE(Sp(0));
765 if (%lt(cmp,0 :: CInt) == %lt(usize,0)) {
770 /* Result parked in R1, return via info-pointer at TOS */
771 jump %ENTRY_CODE(Sp(0));
783 r = W_[R2 + SIZEOF_StgArrWords];
788 /* Result parked in R1, return via info-pointer at TOS */
790 jump %ENTRY_CODE(Sp(0));
802 r = W_[R2 + SIZEOF_StgArrWords];
807 /* Result parked in R1, return via info-pointer at TOS */
809 jump %ENTRY_CODE(Sp(0));
816 FETCH_MP_TEMP(mp_tmp1);
817 FETCH_MP_TEMP(mp_tmp_w);
819 /* arguments: F1 = Float# */
822 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(1), NO_PTRS, decodeFloatzh_fast );
824 /* Be prepared to tell Lennart-coded __decodeFloat
825 where mantissa._mp_d can be put (it does not care about the rest) */
826 p = Hp - SIZEOF_StgArrWords;
827 SET_HDR(p,stg_ARR_WORDS_info,W_[CCCS]);
828 StgArrWords_words(p) = 1;
829 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(p);
831 /* Perform the operation */
832 foreign "C" __decodeFloat(mp_tmp1 "ptr",mp_tmp_w "ptr" ,arg) [];
834 /* returns: (Int# (expn), Int#, ByteArray#) */
835 RET_NNP(W_[mp_tmp_w], TO_W_(MP_INT__mp_size(mp_tmp1)), p);
838 #define DOUBLE_MANTISSA_SIZE SIZEOF_DOUBLE
839 #define ARR_SIZE (SIZEOF_StgArrWords + DOUBLE_MANTISSA_SIZE)
845 FETCH_MP_TEMP(mp_tmp1);
846 FETCH_MP_TEMP(mp_tmp_w);
848 /* arguments: D1 = Double# */
851 ALLOC_PRIM( ARR_SIZE, NO_PTRS, decodeDoublezh_fast );
853 /* Be prepared to tell Lennart-coded __decodeDouble
854 where mantissa.d can be put (it does not care about the rest) */
855 p = Hp - ARR_SIZE + WDS(1);
856 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
857 StgArrWords_words(p) = BYTES_TO_WDS(DOUBLE_MANTISSA_SIZE);
858 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(p);
860 /* Perform the operation */
861 foreign "C" __decodeDouble(mp_tmp1 "ptr", mp_tmp_w "ptr",arg) [];
863 /* returns: (Int# (expn), Int#, ByteArray#) */
864 RET_NNP(W_[mp_tmp_w], TO_W_(MP_INT__mp_size(mp_tmp1)), p);
867 /* -----------------------------------------------------------------------------
868 * Concurrency primitives
869 * -------------------------------------------------------------------------- */
873 /* args: R1 = closure to spark */
875 MAYBE_GC(R1_PTR, forkzh_fast);
877 foreign "C" ACQUIRE_LOCK(sched_mutex "ptr");
879 // create it right now, return ThreadID in R1
880 "ptr" R1 = foreign "C" createIOThread( RtsFlags_GcFlags_initialStkSize(RtsFlags),
882 foreign "C" scheduleThreadLocked(R1 "ptr");
884 foreign "C" RELEASE_LOCK(sched_mutex "ptr");
886 // switch at the earliest opportunity
887 CInt[context_switch] = 1 :: CInt;
894 jump stg_yield_noregs;
909 foreign "C" labelThread(R1 "ptr", R2 "ptr");
911 jump %ENTRY_CODE(Sp(0));
914 isCurrentThreadBoundzh_fast
918 r = foreign "C" isThreadBound(CurrentTSO) [];
923 /* -----------------------------------------------------------------------------
925 * -------------------------------------------------------------------------- */
929 #define IF_NOT_REG_R1(x)
932 #define IF_NOT_REG_R1(x) x
935 // Catch retry frame ------------------------------------------------------------
937 #define CATCH_RETRY_FRAME_ERROR(label) \
938 label { foreign "C" barf("catch_retry_frame incorrectly entered!"); }
940 CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_0_ret)
941 CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_1_ret)
942 CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_2_ret)
943 CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_3_ret)
944 CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_4_ret)
945 CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_5_ret)
946 CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_6_ret)
947 CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_7_ret)
949 #if MAX_VECTORED_RTN > 8
950 #error MAX_VECTORED_RTN has changed: please modify stg_catch_retry_frame too.
953 #if defined(PROFILING)
954 #define CATCH_RETRY_FRAME_BITMAP 7
955 #define CATCH_RETRY_FRAME_WORDS 6
957 #define CATCH_RETRY_FRAME_BITMAP 1
958 #define CATCH_RETRY_FRAME_WORDS 4
961 INFO_TABLE_RET(stg_catch_retry_frame,
962 CATCH_RETRY_FRAME_WORDS, CATCH_RETRY_FRAME_BITMAP,
964 stg_catch_retry_frame_0_ret,
965 stg_catch_retry_frame_1_ret,
966 stg_catch_retry_frame_2_ret,
967 stg_catch_retry_frame_3_ret,
968 stg_catch_retry_frame_4_ret,
969 stg_catch_retry_frame_5_ret,
970 stg_catch_retry_frame_6_ret,
971 stg_catch_retry_frame_7_ret)
973 W_ r, frame, trec, outer;
974 IF_NOT_REG_R1(W_ rval; rval = Sp(0); Sp_adj(1); )
977 trec = StgTSO_trec(CurrentTSO);
978 "ptr" outer = foreign "C" stmGetEnclosingTRec(trec "ptr") [];
979 r = foreign "C" stmCommitNestedTransaction(BaseReg "ptr", trec "ptr") [];
981 /* Succeeded (either first branch or second branch) */
982 StgTSO_trec(CurrentTSO) = outer;
983 Sp = Sp + SIZEOF_StgCatchRetryFrame;
984 IF_NOT_REG_R1(Sp_adj(-1); Sp(0) = rval;)
985 jump %ENTRY_CODE(Sp(SP_OFF));
987 /* Did not commit: retry */
989 "ptr" new_trec = foreign "C" stmStartTransaction(BaseReg "ptr", outer "ptr") [];
990 StgTSO_trec(CurrentTSO) = new_trec;
991 if (StgCatchRetryFrame_running_alt_code(frame)) {
992 R1 = StgCatchRetryFrame_alt_code(frame);
994 R1 = StgCatchRetryFrame_first_code(frame);
995 StgCatchRetryFrame_first_code_trec(frame) = new_trec;
998 jump RET_LBL(stg_ap_v);
1003 // Atomically frame -------------------------------------------------------------
1006 #define ATOMICALLY_FRAME_ERROR(label) \
1007 label { foreign "C" barf("atomically_frame incorrectly entered!"); }
1009 ATOMICALLY_FRAME_ERROR(stg_atomically_frame_0_ret)
1010 ATOMICALLY_FRAME_ERROR(stg_atomically_frame_1_ret)
1011 ATOMICALLY_FRAME_ERROR(stg_atomically_frame_2_ret)
1012 ATOMICALLY_FRAME_ERROR(stg_atomically_frame_3_ret)
1013 ATOMICALLY_FRAME_ERROR(stg_atomically_frame_4_ret)
1014 ATOMICALLY_FRAME_ERROR(stg_atomically_frame_5_ret)
1015 ATOMICALLY_FRAME_ERROR(stg_atomically_frame_6_ret)
1016 ATOMICALLY_FRAME_ERROR(stg_atomically_frame_7_ret)
1018 #if MAX_VECTORED_RTN > 8
1019 #error MAX_VECTORED_RTN has changed: please modify stg_atomically_frame too.
1022 #if defined(PROFILING)
1023 #define ATOMICALLY_FRAME_BITMAP 7
1024 #define ATOMICALLY_FRAME_WORDS 4
1026 #define ATOMICALLY_FRAME_BITMAP 1
1027 #define ATOMICALLY_FRAME_WORDS 2
1031 INFO_TABLE_RET(stg_atomically_frame,
1032 ATOMICALLY_FRAME_WORDS, ATOMICALLY_FRAME_BITMAP,
1034 stg_atomically_frame_0_ret,
1035 stg_atomically_frame_1_ret,
1036 stg_atomically_frame_2_ret,
1037 stg_atomically_frame_3_ret,
1038 stg_atomically_frame_4_ret,
1039 stg_atomically_frame_5_ret,
1040 stg_atomically_frame_6_ret,
1041 stg_atomically_frame_7_ret)
1043 W_ frame, trec, valid;
1044 IF_NOT_REG_R1(W_ rval; rval = Sp(0); Sp_adj(1); )
1047 trec = StgTSO_trec(CurrentTSO);
1048 if (StgAtomicallyFrame_waiting(frame)) {
1049 /* The TSO is currently waiting: should we stop waiting? */
1050 valid = foreign "C" stmReWait(CurrentTSO "ptr");
1052 /* Previous attempt is still valid: no point trying again yet */
1053 IF_NOT_REG_R1(Sp_adj(-2);
1054 Sp(1) = stg_NO_FINALIZER_closure;
1055 Sp(0) = stg_ut_1_0_unreg_info;)
1056 jump stg_block_noregs;
1058 /* Previous attempt is no longer valid: try again */
1059 "ptr" trec = foreign "C" stmStartTransaction(BaseReg "ptr", NO_TREC "ptr");
1060 StgTSO_trec(CurrentTSO) = trec;
1061 StgAtomicallyFrame_waiting(frame) = 0 :: CInt; /* false; */
1062 R1 = StgAtomicallyFrame_code(frame);
1064 jump RET_LBL(stg_ap_v);
1067 /* The TSO is not currently waiting: try to commit the transaction */
1068 valid = foreign "C" stmCommitTransaction(BaseReg "ptr", trec "ptr");
1070 /* Transaction was valid: commit succeeded */
1071 StgTSO_trec(CurrentTSO) = NO_TREC;
1072 Sp = Sp + SIZEOF_StgAtomicallyFrame;
1073 IF_NOT_REG_R1(Sp_adj(-1); Sp(0) = rval;)
1074 jump %ENTRY_CODE(Sp(SP_OFF));
1076 /* Transaction was not valid: try again */
1077 "ptr" trec = foreign "C" stmStartTransaction(BaseReg "ptr", NO_TREC "ptr");
1078 StgTSO_trec(CurrentTSO) = trec;
1079 R1 = StgAtomicallyFrame_code(frame);
1081 jump RET_LBL(stg_ap_v);
1087 // STM catch frame --------------------------------------------------------------
1089 #define CATCH_STM_FRAME_ENTRY_TEMPLATE(label,ret) \
1092 IF_NOT_REG_R1(W_ rval; rval = Sp(0); Sp_adj(1); ) \
1093 Sp = Sp + SIZEOF_StgCatchSTMFrame; \
1094 IF_NOT_REG_R1(Sp_adj(-1); Sp(0) = rval;) \
1104 CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_0_ret,%RET_VEC(Sp(SP_OFF),0))
1105 CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_1_ret,%RET_VEC(Sp(SP_OFF),1))
1106 CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_2_ret,%RET_VEC(Sp(SP_OFF),2))
1107 CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_3_ret,%RET_VEC(Sp(SP_OFF),3))
1108 CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_4_ret,%RET_VEC(Sp(SP_OFF),4))
1109 CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_5_ret,%RET_VEC(Sp(SP_OFF),5))
1110 CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_6_ret,%RET_VEC(Sp(SP_OFF),6))
1111 CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_7_ret,%RET_VEC(Sp(SP_OFF),7))
1113 #if MAX_VECTORED_RTN > 8
1114 #error MAX_VECTORED_RTN has changed: please modify stg_catch_stm_frame too.
1117 #if defined(PROFILING)
1118 #define CATCH_STM_FRAME_BITMAP 3
1119 #define CATCH_STM_FRAME_WORDS 3
1121 #define CATCH_STM_FRAME_BITMAP 0
1122 #define CATCH_STM_FRAME_WORDS 1
1125 /* Catch frames are very similar to update frames, but when entering
1126 * one we just pop the frame off the stack and perform the correct
1127 * kind of return to the activation record underneath us on the stack.
1130 INFO_TABLE_RET(stg_catch_stm_frame,
1131 CATCH_STM_FRAME_WORDS, CATCH_STM_FRAME_BITMAP,
1133 stg_catch_stm_frame_0_ret,
1134 stg_catch_stm_frame_1_ret,
1135 stg_catch_stm_frame_2_ret,
1136 stg_catch_stm_frame_3_ret,
1137 stg_catch_stm_frame_4_ret,
1138 stg_catch_stm_frame_5_ret,
1139 stg_catch_stm_frame_6_ret,
1140 stg_catch_stm_frame_7_ret)
1141 CATCH_STM_FRAME_ENTRY_TEMPLATE(,%ENTRY_CODE(Sp(SP_OFF)))
1144 // Primop definition ------------------------------------------------------------
1152 // stmStartTransaction may allocate
1153 MAYBE_GC (R1_PTR, atomicallyzh_fast);
1155 /* Args: R1 = m :: STM a */
1156 STK_CHK_GEN(SIZEOF_StgAtomicallyFrame + WDS(1), R1_PTR, atomicallyzh_fast);
1158 /* Set up the atomically frame */
1159 Sp = Sp - SIZEOF_StgAtomicallyFrame;
1162 SET_HDR(frame,stg_atomically_frame_info, W_[CCCS]);
1163 StgAtomicallyFrame_waiting(frame) = 0 :: CInt; // False
1164 StgAtomicallyFrame_code(frame) = R1;
1166 /* Start the memory transcation */
1167 old_trec = StgTSO_trec(CurrentTSO);
1168 ASSERT(old_trec == NO_TREC);
1169 "ptr" new_trec = foreign "C" stmStartTransaction(BaseReg "ptr", old_trec "ptr");
1170 StgTSO_trec(CurrentTSO) = new_trec;
1172 /* Apply R1 to the realworld token */
1174 jump RET_LBL(stg_ap_v);
1182 /* Args: R1 :: STM a */
1183 /* Args: R2 :: Exception -> STM a */
1184 STK_CHK_GEN(SIZEOF_StgCatchSTMFrame + WDS(1), R1_PTR & R2_PTR, catchSTMzh_fast);
1186 /* Set up the catch frame */
1187 Sp = Sp - SIZEOF_StgCatchSTMFrame;
1190 SET_HDR(frame, stg_catch_stm_frame_info, W_[CCCS]);
1191 StgCatchSTMFrame_handler(frame) = R2;
1193 /* Apply R1 to the realworld token */
1195 jump RET_LBL(stg_ap_v);
1205 // stmStartTransaction may allocate
1206 MAYBE_GC (R1_PTR & R2_PTR, catchRetryzh_fast);
1208 /* Args: R1 :: STM a */
1209 /* Args: R2 :: STM a */
1210 STK_CHK_GEN(SIZEOF_StgCatchRetryFrame + WDS(1), R1_PTR & R2_PTR, catchRetryzh_fast);
1212 /* Start a nested transaction within which to run the first code */
1213 trec = StgTSO_trec(CurrentTSO);
1214 "ptr" new_trec = foreign "C" stmStartTransaction(BaseReg "ptr", trec "ptr");
1215 StgTSO_trec(CurrentTSO) = new_trec;
1217 /* Set up the catch-retry frame */
1218 Sp = Sp - SIZEOF_StgCatchRetryFrame;
1221 SET_HDR(frame, stg_catch_retry_frame_info, W_[CCCS]);
1222 StgCatchRetryFrame_running_alt_code(frame) = 0 :: CInt; // false;
1223 StgCatchRetryFrame_first_code(frame) = R1;
1224 StgCatchRetryFrame_alt_code(frame) = R2;
1225 StgCatchRetryFrame_first_code_trec(frame) = new_trec;
1227 /* Apply R1 to the realworld token */
1229 jump RET_LBL(stg_ap_v);
1241 MAYBE_GC (NO_PTRS, retryzh_fast); // STM operations may allocate
1243 // Find the enclosing ATOMICALLY_FRAME or CATCH_RETRY_FRAME
1245 trec = StgTSO_trec(CurrentTSO);
1246 "ptr" outer = foreign "C" stmGetEnclosingTRec(trec "ptr");
1247 StgTSO_sp(CurrentTSO) = Sp;
1248 frame_type = foreign "C" findRetryFrameHelper(CurrentTSO "ptr");
1249 Sp = StgTSO_sp(CurrentTSO);
1252 if (frame_type == CATCH_RETRY_FRAME) {
1253 // The retry reaches a CATCH_RETRY_FRAME before the atomic frame
1254 ASSERT(outer != NO_TREC);
1255 if (!StgCatchRetryFrame_running_alt_code(frame)) {
1256 // Retry in the first code: try the alternative
1257 "ptr" trec = foreign "C" stmStartTransaction(BaseReg "ptr", outer "ptr");
1258 StgTSO_trec(CurrentTSO) = trec;
1259 StgCatchRetryFrame_running_alt_code(frame) = 1 :: CInt; // true;
1260 R1 = StgCatchRetryFrame_alt_code(frame);
1262 jump RET_LBL(stg_ap_v);
1264 // Retry in the alternative code: propagate
1266 other_trec = StgCatchRetryFrame_first_code_trec(frame);
1267 r = foreign "C" stmCommitNestedTransaction(BaseReg "ptr", other_trec "ptr");
1269 r = foreign "C" stmCommitNestedTransaction(BaseReg "ptr", trec "ptr");
1272 // Merge between siblings succeeded: commit it back to enclosing transaction
1273 // and then propagate the retry
1274 StgTSO_trec(CurrentTSO) = outer;
1275 Sp = Sp + SIZEOF_StgCatchRetryFrame;
1276 goto retry_pop_stack;
1278 // Merge failed: we musn't propagate the retry. Try both paths again.
1279 "ptr" trec = foreign "C" stmStartTransaction(BaseReg "ptr", outer "ptr");
1280 StgCatchRetryFrame_first_code_trec(frame) = trec;
1281 StgCatchRetryFrame_running_alt_code(frame) = 0 :: CInt; // false;
1282 StgTSO_trec(CurrentTSO) = trec;
1283 R1 = StgCatchRetryFrame_first_code(frame);
1285 jump RET_LBL(stg_ap_v);
1290 // We've reached the ATOMICALLY_FRAME: attempt to wait
1291 ASSERT(frame_type == ATOMICALLY_FRAME);
1292 ASSERT(outer == NO_TREC);
1293 r = foreign "C" stmWait(BaseReg "ptr", CurrentTSO "ptr", trec "ptr");
1295 // Transaction was valid: stmWait put us on the TVars' queues, we now block
1296 StgAtomicallyFrame_waiting(frame) = 1 :: CInt; // true
1298 // Fix up the stack in the unregisterised case: the return convention is different.
1299 IF_NOT_REG_R1(Sp_adj(-2);
1300 Sp(1) = stg_NO_FINALIZER_closure;
1301 Sp(0) = stg_ut_1_0_unreg_info;)
1302 jump stg_block_noregs;
1304 // Transaction was not valid: retry immediately
1305 "ptr" trec = foreign "C" stmStartTransaction(BaseReg "ptr", outer "ptr");
1306 StgTSO_trec(CurrentTSO) = trec;
1307 R1 = StgAtomicallyFrame_code(frame);
1310 jump RET_LBL(stg_ap_v);
1320 /* Args: R1 = initialisation value */
1322 MAYBE_GC (R1_PTR, newTVarzh_fast);
1324 tv = foreign "C" stmNewTVar(BaseReg "ptr", new_value "ptr");
1335 /* Args: R1 = TVar closure */
1337 MAYBE_GC (R1_PTR, readTVarzh_fast); // Call to stmReadTVar may allocate
1338 trec = StgTSO_trec(CurrentTSO);
1340 "ptr" result = foreign "C" stmReadTVar(BaseReg "ptr", trec "ptr", tvar "ptr") [];
1352 /* Args: R1 = TVar closure */
1353 /* R2 = New value */
1355 MAYBE_GC (R1_PTR & R2_PTR, writeTVarzh_fast); // Call to stmWriteTVar may allocate
1356 trec = StgTSO_trec(CurrentTSO);
1359 foreign "C" stmWriteTVar(BaseReg "ptr", trec "ptr", tvar "ptr", new_value "ptr") [];
1361 jump %ENTRY_CODE(Sp(0));
1365 /* -----------------------------------------------------------------------------
1368 * take & putMVar work as follows. Firstly, an important invariant:
1370 * If the MVar is full, then the blocking queue contains only
1371 * threads blocked on putMVar, and if the MVar is empty then the
1372 * blocking queue contains only threads blocked on takeMVar.
1375 * MVar empty : then add ourselves to the blocking queue
1376 * MVar full : remove the value from the MVar, and
1377 * blocking queue empty : return
1378 * blocking queue non-empty : perform the first blocked putMVar
1379 * from the queue, and wake up the
1380 * thread (MVar is now full again)
1382 * putMVar is just the dual of the above algorithm.
1384 * How do we "perform a putMVar"? Well, we have to fiddle around with
1385 * the stack of the thread waiting to do the putMVar. See
1386 * stg_block_putmvar and stg_block_takemvar in HeapStackCheck.c for
1387 * the stack layout, and the PerformPut and PerformTake macros below.
1389 * It is important that a blocked take or put is woken up with the
1390 * take/put already performed, because otherwise there would be a
1391 * small window of vulnerability where the thread could receive an
1392 * exception and never perform its take or put, and we'd end up with a
1395 * -------------------------------------------------------------------------- */
1399 /* args: R1 = MVar closure */
1401 if (GET_INFO(R1) == stg_EMPTY_MVAR_info) {
1413 ALLOC_PRIM ( SIZEOF_StgMVar, NO_PTRS, newMVarzh_fast );
1415 mvar = Hp - SIZEOF_StgMVar + WDS(1);
1416 SET_HDR(mvar,stg_EMPTY_MVAR_info,W_[CCCS]);
1417 StgMVar_head(mvar) = stg_END_TSO_QUEUE_closure;
1418 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1419 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1424 /* If R1 isn't available, pass it on the stack */
1426 #define PerformTake(tso, value) \
1427 W_[StgTSO_sp(tso) + WDS(1)] = value; \
1428 W_[StgTSO_sp(tso) + WDS(0)] = stg_gc_unpt_r1_info;
1430 #define PerformTake(tso, value) \
1431 W_[StgTSO_sp(tso) + WDS(1)] = value; \
1432 W_[StgTSO_sp(tso) + WDS(0)] = stg_ut_1_0_unreg_info;
1435 #define PerformPut(tso,lval) \
1436 StgTSO_sp(tso) = StgTSO_sp(tso) + WDS(3); \
1437 lval = W_[StgTSO_sp(tso) - WDS(1)];
1441 W_ mvar, val, info, tso;
1443 /* args: R1 = MVar closure */
1447 "ptr" info = foreign "C" lockClosure(mvar "ptr");
1449 info = GET_INFO(mvar);
1452 /* If the MVar is empty, put ourselves on its blocking queue,
1453 * and wait until we're woken up.
1455 if (info == stg_EMPTY_MVAR_info) {
1456 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1457 StgMVar_head(mvar) = CurrentTSO;
1459 StgTSO_link(StgMVar_tail(mvar)) = CurrentTSO;
1461 StgTSO_link(CurrentTSO) = stg_END_TSO_QUEUE_closure;
1462 StgTSO_why_blocked(CurrentTSO) = BlockedOnMVar::I16;
1463 StgTSO_block_info(CurrentTSO) = mvar;
1464 StgMVar_tail(mvar) = CurrentTSO;
1466 jump stg_block_takemvar;
1469 /* we got the value... */
1470 val = StgMVar_value(mvar);
1472 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure)
1474 /* There are putMVar(s) waiting...
1475 * wake up the first thread on the queue
1477 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1479 /* actually perform the putMVar for the thread that we just woke up */
1480 tso = StgMVar_head(mvar);
1481 PerformPut(tso,StgMVar_value(mvar));
1483 #if defined(GRAN) || defined(PAR)
1484 /* ToDo: check 2nd arg (mvar) is right */
1485 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar),mvar) [];
1486 StgMVar_head(mvar) = tso;
1488 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr") [];
1489 StgMVar_head(mvar) = tso;
1492 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1493 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1497 foreign "C" unlockClosure(mvar "ptr", stg_FULL_MVAR_info);
1503 /* No further putMVars, MVar is now empty */
1504 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1507 foreign "C" unlockClosure(mvar "ptr", stg_EMPTY_MVAR_info);
1509 SET_INFO(mvar,stg_EMPTY_MVAR_info);
1519 W_ mvar, val, info, tso;
1521 /* args: R1 = MVar closure */
1526 "ptr" info = foreign "C" lockClosure(mvar "ptr");
1528 info = GET_INFO(mvar);
1531 if (info == stg_EMPTY_MVAR_info) {
1533 foreign "C" unlockClosure(mvar "ptr", stg_EMPTY_MVAR_info);
1535 /* HACK: we need a pointer to pass back,
1536 * so we abuse NO_FINALIZER_closure
1538 RET_NP(0, stg_NO_FINALIZER_closure);
1541 /* we got the value... */
1542 val = StgMVar_value(mvar);
1544 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1546 /* There are putMVar(s) waiting...
1547 * wake up the first thread on the queue
1549 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1551 /* actually perform the putMVar for the thread that we just woke up */
1552 tso = StgMVar_head(mvar);
1553 PerformPut(tso,StgMVar_value(mvar));
1555 #if defined(GRAN) || defined(PAR)
1556 /* ToDo: check 2nd arg (mvar) is right */
1557 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr", mvar "ptr") [];
1558 StgMVar_head(mvar) = tso;
1560 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr") [];
1561 StgMVar_head(mvar) = tso;
1564 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1565 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1568 foreign "C" unlockClosure(mvar "ptr", stg_FULL_MVAR_info);
1573 /* No further putMVars, MVar is now empty */
1574 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1576 foreign "C" unlockClosure(mvar "ptr", stg_EMPTY_MVAR_info);
1578 SET_INFO(mvar,stg_EMPTY_MVAR_info);
1590 /* args: R1 = MVar, R2 = value */
1594 "ptr" info = foreign "C" lockClosure(mvar "ptr");
1596 info = GET_INFO(mvar);
1599 if (info == stg_FULL_MVAR_info) {
1600 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1601 StgMVar_head(mvar) = CurrentTSO;
1603 StgTSO_link(StgMVar_tail(mvar)) = CurrentTSO;
1605 StgTSO_link(CurrentTSO) = stg_END_TSO_QUEUE_closure;
1606 StgTSO_why_blocked(CurrentTSO) = BlockedOnMVar::I16;
1607 StgTSO_block_info(CurrentTSO) = mvar;
1608 StgMVar_tail(mvar) = CurrentTSO;
1610 jump stg_block_putmvar;
1613 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1615 /* There are takeMVar(s) waiting: wake up the first one
1617 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1619 /* actually perform the takeMVar */
1620 tso = StgMVar_head(mvar);
1621 PerformTake(tso, R2);
1623 #if defined(GRAN) || defined(PAR)
1624 /* ToDo: check 2nd arg (mvar) is right */
1625 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr",mvar "ptr") [];
1626 StgMVar_head(mvar) = tso;
1628 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr") [];
1629 StgMVar_head(mvar) = tso;
1632 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1633 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1637 foreign "C" unlockClosure(mvar "ptr", stg_EMPTY_MVAR_info);
1639 jump %ENTRY_CODE(Sp(0));
1643 /* No further takes, the MVar is now full. */
1644 StgMVar_value(mvar) = R2;
1647 foreign "C" unlockClosure(mvar "ptr", stg_FULL_MVAR_info);
1649 SET_INFO(mvar,stg_FULL_MVAR_info);
1651 jump %ENTRY_CODE(Sp(0));
1654 /* ToDo: yield afterward for better communication performance? */
1662 /* args: R1 = MVar, R2 = value */
1666 "ptr" info = foreign "C" lockClosure(mvar "ptr");
1668 info = GET_INFO(mvar);
1671 if (info == stg_FULL_MVAR_info) {
1673 foreign "C" unlockClosure(mvar "ptr", stg_FULL_MVAR_info);
1678 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1680 /* There are takeMVar(s) waiting: wake up the first one
1682 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1684 /* actually perform the takeMVar */
1685 tso = StgMVar_head(mvar);
1686 PerformTake(tso, R2);
1688 #if defined(GRAN) || defined(PAR)
1689 /* ToDo: check 2nd arg (mvar) is right */
1690 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr",mvar "ptr") [];
1691 StgMVar_head(mvar) = tso;
1693 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr") [];
1694 StgMVar_head(mvar) = tso;
1697 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1698 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1702 foreign "C" unlockClosure(mvar "ptr", stg_EMPTY_MVAR_info);
1704 jump %ENTRY_CODE(Sp(0));
1708 /* No further takes, the MVar is now full. */
1709 StgMVar_value(mvar) = R2;
1712 foreign "C" unlockClosure(mvar "ptr", stg_FULL_MVAR_info);
1714 SET_INFO(mvar,stg_FULL_MVAR_info);
1716 jump %ENTRY_CODE(Sp(0));
1719 /* ToDo: yield afterward for better communication performance? */
1723 /* -----------------------------------------------------------------------------
1724 Stable pointer primitives
1725 ------------------------------------------------------------------------- */
1727 makeStableNamezh_fast
1731 ALLOC_PRIM( SIZEOF_StgStableName, R1_PTR, makeStableNamezh_fast );
1733 index = foreign "C" lookupStableName(R1 "ptr") [];
1735 /* Is there already a StableName for this heap object?
1736 * stable_ptr_table is a pointer to an array of snEntry structs.
1738 if ( snEntry_sn_obj(W_[stable_ptr_table] + index*SIZEOF_snEntry) == NULL ) {
1739 sn_obj = Hp - SIZEOF_StgStableName + WDS(1);
1740 SET_HDR(sn_obj, stg_STABLE_NAME_info, W_[CCCS]);
1741 StgStableName_sn(sn_obj) = index;
1742 snEntry_sn_obj(W_[stable_ptr_table] + index*SIZEOF_snEntry) = sn_obj;
1744 sn_obj = snEntry_sn_obj(W_[stable_ptr_table] + index*SIZEOF_snEntry);
1751 makeStablePtrzh_fast
1755 MAYBE_GC(R1_PTR, makeStablePtrzh_fast);
1756 "ptr" sp = foreign "C" getStablePtr(R1 "ptr") [];
1760 deRefStablePtrzh_fast
1762 /* Args: R1 = the stable ptr */
1765 r = snEntry_addr(W_[stable_ptr_table] + sp*SIZEOF_snEntry);
1769 /* -----------------------------------------------------------------------------
1770 Bytecode object primitives
1771 ------------------------------------------------------------------------- */
1782 W_ bco, bitmap_arr, bytes, words;
1785 words = BYTES_TO_WDS(SIZEOF_StgBCO) + StgArrWords_words(bitmap_arr);
1788 ALLOC_PRIM( bytes, R1_PTR&R2_PTR&R3_PTR&R4_PTR&R6_PTR, newBCOzh_fast );
1790 bco = Hp - bytes + WDS(1);
1791 SET_HDR(bco, stg_BCO_info, W_[CCCS]);
1793 StgBCO_instrs(bco) = R1;
1794 StgBCO_literals(bco) = R2;
1795 StgBCO_ptrs(bco) = R3;
1796 StgBCO_itbls(bco) = R4;
1797 StgBCO_arity(bco) = HALF_W_(R5);
1798 StgBCO_size(bco) = HALF_W_(words);
1800 // Copy the arity/bitmap info into the BCO
1804 if (i < StgArrWords_words(bitmap_arr)) {
1805 StgBCO_bitmap(bco,i) = StgArrWords_payload(bitmap_arr,i);
1816 // R1 = the BCO# for the AP
1820 // This function is *only* used to wrap zero-arity BCOs in an
1821 // updatable wrapper (see ByteCodeLink.lhs). An AP thunk is always
1822 // saturated and always points directly to a FUN or BCO.
1823 ASSERT(%INFO_TYPE(%GET_STD_INFO(R1)) == HALF_W_(BCO) &&
1824 StgBCO_arity(R1) == HALF_W_(0));
1826 HP_CHK_GEN_TICKY(SIZEOF_StgAP, R1_PTR, mkApUpd0zh_fast);
1827 TICK_ALLOC_UP_THK(0, 0);
1828 CCCS_ALLOC(SIZEOF_StgAP);
1830 ap = Hp - SIZEOF_StgAP + WDS(1);
1831 SET_HDR(ap, stg_AP_info, W_[CCCS]);
1833 StgAP_n_args(ap) = HALF_W_(0);
1839 /* -----------------------------------------------------------------------------
1840 Thread I/O blocking primitives
1841 -------------------------------------------------------------------------- */
1843 /* Add a thread to the end of the blocked queue. (C-- version of the C
1844 * macro in Schedule.h).
1846 #define APPEND_TO_BLOCKED_QUEUE(tso) \
1847 ASSERT(StgTSO_link(tso) == END_TSO_QUEUE); \
1848 if (W_[blocked_queue_hd] == END_TSO_QUEUE) { \
1849 W_[blocked_queue_hd] = tso; \
1851 StgTSO_link(W_[blocked_queue_tl]) = tso; \
1853 W_[blocked_queue_tl] = tso;
1859 foreign "C" barf("waitRead# on threaded RTS");
1862 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1863 StgTSO_why_blocked(CurrentTSO) = BlockedOnRead::I16;
1864 StgTSO_block_info(CurrentTSO) = R1;
1865 // No locking - we're not going to use this interface in the
1866 // threaded RTS anyway.
1867 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1868 jump stg_block_noregs;
1875 foreign "C" barf("waitWrite# on threaded RTS");
1878 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1879 StgTSO_why_blocked(CurrentTSO) = BlockedOnWrite::I16;
1880 StgTSO_block_info(CurrentTSO) = R1;
1881 // No locking - we're not going to use this interface in the
1882 // threaded RTS anyway.
1883 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1884 jump stg_block_noregs;
1888 STRING(stg_delayzh_malloc_str, "delayzh_fast")
1891 #ifdef mingw32_HOST_OS
1899 foreign "C" barf("delay# on threaded RTS");
1902 /* args: R1 (microsecond delay amount) */
1903 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1904 StgTSO_why_blocked(CurrentTSO) = BlockedOnDelay::I16;
1906 #ifdef mingw32_HOST_OS
1908 /* could probably allocate this on the heap instead */
1909 "ptr" ares = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1910 stg_delayzh_malloc_str);
1911 reqID = foreign "C" addDelayRequest(R1);
1912 StgAsyncIOResult_reqID(ares) = reqID;
1913 StgAsyncIOResult_len(ares) = 0;
1914 StgAsyncIOResult_errCode(ares) = 0;
1915 StgTSO_block_info(CurrentTSO) = ares;
1917 /* Having all async-blocked threads reside on the blocked_queue
1918 * simplifies matters, so change the status to OnDoProc put the
1919 * delayed thread on the blocked_queue.
1921 StgTSO_why_blocked(CurrentTSO) = BlockedOnDoProc::I16;
1922 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1923 jump stg_block_async_void;
1928 time = foreign "C" getourtimeofday();
1929 target = (R1 / (TICK_MILLISECS*1000)) + time;
1930 StgTSO_block_info(CurrentTSO) = target;
1932 /* Insert the new thread in the sleeping queue. */
1934 t = W_[sleeping_queue];
1936 if (t != END_TSO_QUEUE && StgTSO_block_info(t) < target) {
1942 StgTSO_link(CurrentTSO) = t;
1944 W_[sleeping_queue] = CurrentTSO;
1946 StgTSO_link(prev) = CurrentTSO;
1948 jump stg_block_noregs;
1953 #ifdef mingw32_HOST_OS
1954 STRING(stg_asyncReadzh_malloc_str, "asyncReadzh_fast")
1961 foreign "C" barf("asyncRead# on threaded RTS");
1964 /* args: R1 = fd, R2 = isSock, R3 = len, R4 = buf */
1965 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1966 StgTSO_why_blocked(CurrentTSO) = BlockedOnRead::I16;
1968 /* could probably allocate this on the heap instead */
1969 "ptr" ares = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1970 stg_asyncReadzh_malloc_str);
1971 reqID = foreign "C" addIORequest(R1, 0/*FALSE*/,R2,R3,R4 "ptr");
1972 StgAsyncIOResult_reqID(ares) = reqID;
1973 StgAsyncIOResult_len(ares) = 0;
1974 StgAsyncIOResult_errCode(ares) = 0;
1975 StgTSO_block_info(CurrentTSO) = ares;
1976 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1977 jump stg_block_async;
1980 STRING(stg_asyncWritezh_malloc_str, "asyncWritezh_fast")
1987 foreign "C" barf("asyncWrite# on threaded RTS");
1990 /* args: R1 = fd, R2 = isSock, R3 = len, R4 = buf */
1991 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1992 StgTSO_why_blocked(CurrentTSO) = BlockedOnWrite::I16;
1994 "ptr" ares = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1995 stg_asyncWritezh_malloc_str);
1996 reqID = foreign "C" addIORequest(R1, 1/*TRUE*/,R2,R3,R4 "ptr");
1998 StgAsyncIOResult_reqID(ares) = reqID;
1999 StgAsyncIOResult_len(ares) = 0;
2000 StgAsyncIOResult_errCode(ares) = 0;
2001 StgTSO_block_info(CurrentTSO) = ares;
2002 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
2003 jump stg_block_async;
2006 STRING(stg_asyncDoProczh_malloc_str, "asyncDoProczh_fast")
2012 /* args: R1 = proc, R2 = param */
2013 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
2014 StgTSO_why_blocked(CurrentTSO) = BlockedOnDoProc::I16;
2016 /* could probably allocate this on the heap instead */
2017 "ptr" ares = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
2018 stg_asyncDoProczh_malloc_str);
2019 reqID = foreign "C" addDoProcRequest(R1 "ptr",R2 "ptr");
2020 StgAsyncIOResult_reqID(ares) = reqID;
2021 StgAsyncIOResult_len(ares) = 0;
2022 StgAsyncIOResult_errCode(ares) = 0;
2023 StgTSO_block_info(CurrentTSO) = ares;
2024 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
2025 jump stg_block_async;
2029 /* -----------------------------------------------------------------------------
2032 classes CCallable and CReturnable don't really exist, but the
2033 compiler insists on generating dictionaries containing references
2034 to GHC_ZcCCallable_static_info etc., so we provide dummy symbols
2035 for these. Some C compilers can't cope with zero-length static arrays,
2036 so we have to make these one element long.
2037 --------------------------------------------------------------------------- */
2040 GHC_ZCCCallable_static_info: W_ 0;
2044 GHC_ZCCReturnable_static_info: W_ 0;