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 * ---------------------------------------------------------------------------*/
31 #ifndef mingw32_HOST_OS
41 import __gmpz_tdiv_qr;
42 import __gmpz_fdiv_qr;
43 import __gmpz_divexact;
49 import pthread_mutex_lock;
50 import pthread_mutex_unlock;
52 import base_GHCziIOBase_NestedAtomically_closure;
53 import EnterCriticalSection;
54 import LeaveCriticalSection;
56 /*-----------------------------------------------------------------------------
59 Basically just new*Array - the others are all inline macros.
61 The size arg is always passed in R1, and the result returned in R1.
63 The slow entry point is for returning from a heap check, the saved
64 size argument must be re-loaded from the stack.
65 -------------------------------------------------------------------------- */
67 /* for objects that are *less* than the size of a word, make sure we
68 * round up to the nearest word for the size of the array.
73 W_ words, payload_words, n, p;
74 MAYBE_GC(NO_PTRS,newByteArrayzh_fast);
76 payload_words = ROUNDUP_BYTES_TO_WDS(n);
77 words = BYTES_TO_WDS(SIZEOF_StgArrWords) + payload_words;
78 ("ptr" p) = foreign "C" allocateLocal(MyCapability() "ptr",words) [];
79 TICK_ALLOC_PRIM(SIZEOF_StgArrWords,WDS(payload_words),0);
80 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
81 StgArrWords_words(p) = payload_words;
85 newPinnedByteArrayzh_fast
87 W_ words, payload_words, n, p;
89 MAYBE_GC(NO_PTRS,newPinnedByteArrayzh_fast);
91 payload_words = ROUNDUP_BYTES_TO_WDS(n);
93 // We want an 8-byte aligned array. allocatePinned() gives us
94 // 8-byte aligned memory by default, but we want to align the
95 // *goods* inside the ArrWords object, so we have to check the
96 // size of the ArrWords header and adjust our size accordingly.
97 words = BYTES_TO_WDS(SIZEOF_StgArrWords) + payload_words;
98 if ((SIZEOF_StgArrWords & 7) != 0) {
102 ("ptr" p) = foreign "C" allocatePinned(words) [];
103 TICK_ALLOC_PRIM(SIZEOF_StgArrWords,WDS(payload_words),0);
105 // Again, if the ArrWords header isn't a multiple of 8 bytes, we
106 // have to push the object forward one word so that the goods
107 // fall on an 8-byte boundary.
108 if ((SIZEOF_StgArrWords & 7) != 0) {
112 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
113 StgArrWords_words(p) = payload_words;
119 W_ words, n, init, arr, p;
120 /* Args: R1 = words, R2 = initialisation value */
123 MAYBE_GC(R2_PTR,newArrayzh_fast);
125 words = BYTES_TO_WDS(SIZEOF_StgMutArrPtrs) + n;
126 ("ptr" arr) = foreign "C" allocateLocal(MyCapability() "ptr",words) [R2];
127 TICK_ALLOC_PRIM(SIZEOF_StgMutArrPtrs, WDS(n), 0);
129 SET_HDR(arr, stg_MUT_ARR_PTRS_DIRTY_info, W_[CCCS]);
130 StgMutArrPtrs_ptrs(arr) = n;
132 // Initialise all elements of the the array with the value in R2
134 p = arr + SIZEOF_StgMutArrPtrs;
136 if (p < arr + WDS(words)) {
145 unsafeThawArrayzh_fast
147 // SUBTLETY TO DO WITH THE OLD GEN MUTABLE LIST
149 // A MUT_ARR_PTRS lives on the mutable list, but a MUT_ARR_PTRS_FROZEN
150 // normally doesn't. However, when we freeze a MUT_ARR_PTRS, we leave
151 // it on the mutable list for the GC to remove (removing something from
152 // the mutable list is not easy, because the mut_list is only singly-linked).
154 // So that we can tell whether a MUT_ARR_PTRS_FROZEN is on the mutable list,
155 // when we freeze it we set the info ptr to be MUT_ARR_PTRS_FROZEN0
156 // to indicate that it is still on the mutable list.
158 // So, when we thaw a MUT_ARR_PTRS_FROZEN, we must cope with two cases:
159 // either it is on a mut_list, or it isn't. We adopt the convention that
160 // the closure type is MUT_ARR_PTRS_FROZEN0 if it is on the mutable list,
161 // and MUT_ARR_PTRS_FROZEN otherwise. In fact it wouldn't matter if
162 // we put it on the mutable list more than once, but it would get scavenged
163 // multiple times during GC, which would be unnecessarily slow.
165 if (StgHeader_info(R1) != stg_MUT_ARR_PTRS_FROZEN0_info) {
166 SET_INFO(R1,stg_MUT_ARR_PTRS_DIRTY_info);
167 recordMutable(R1, R1);
168 // must be done after SET_INFO, because it ASSERTs closure_MUTABLE()
171 SET_INFO(R1,stg_MUT_ARR_PTRS_DIRTY_info);
176 /* -----------------------------------------------------------------------------
178 -------------------------------------------------------------------------- */
183 /* Args: R1 = initialisation value */
185 ALLOC_PRIM( SIZEOF_StgMutVar, R1_PTR, newMutVarzh_fast);
187 mv = Hp - SIZEOF_StgMutVar + WDS(1);
188 SET_HDR(mv,stg_MUT_VAR_DIRTY_info,W_[CCCS]);
189 StgMutVar_var(mv) = R1;
194 atomicModifyMutVarzh_fast
197 /* Args: R1 :: MutVar#, R2 :: a -> (a,b) */
199 /* If x is the current contents of the MutVar#, then
200 We want to make the new contents point to
204 and the return value is
208 obviously we can share (f x).
210 z = [stg_ap_2 f x] (max (HS + 2) MIN_UPD_SIZE)
211 y = [stg_sel_0 z] (max (HS + 1) MIN_UPD_SIZE)
212 r = [stg_sel_1 z] (max (HS + 1) MIN_UPD_SIZE)
216 #define THUNK_1_SIZE (SIZEOF_StgThunkHeader + WDS(MIN_UPD_SIZE))
217 #define TICK_ALLOC_THUNK_1() TICK_ALLOC_UP_THK(WDS(1),WDS(MIN_UPD_SIZE-1))
219 #define THUNK_1_SIZE (SIZEOF_StgThunkHeader + WDS(1))
220 #define TICK_ALLOC_THUNK_1() TICK_ALLOC_UP_THK(WDS(1),0)
224 #define THUNK_2_SIZE (SIZEOF_StgThunkHeader + WDS(MIN_UPD_SIZE))
225 #define TICK_ALLOC_THUNK_2() TICK_ALLOC_UP_THK(WDS(2),WDS(MIN_UPD_SIZE-2))
227 #define THUNK_2_SIZE (SIZEOF_StgThunkHeader + WDS(2))
228 #define TICK_ALLOC_THUNK_2() TICK_ALLOC_UP_THK(WDS(2),0)
231 #define SIZE (THUNK_2_SIZE + THUNK_1_SIZE + THUNK_1_SIZE)
233 HP_CHK_GEN_TICKY(SIZE, R1_PTR & R2_PTR, atomicModifyMutVarzh_fast);
235 #if defined(THREADED_RTS)
236 ACQUIRE_LOCK(atomic_modify_mutvar_mutex "ptr") [R1,R2];
239 x = StgMutVar_var(R1);
241 TICK_ALLOC_THUNK_2();
242 CCCS_ALLOC(THUNK_2_SIZE);
243 z = Hp - THUNK_2_SIZE + WDS(1);
244 SET_HDR(z, stg_ap_2_upd_info, W_[CCCS]);
245 LDV_RECORD_CREATE(z);
246 StgThunk_payload(z,0) = R2;
247 StgThunk_payload(z,1) = x;
249 TICK_ALLOC_THUNK_1();
250 CCCS_ALLOC(THUNK_1_SIZE);
251 y = z - THUNK_1_SIZE;
252 SET_HDR(y, stg_sel_0_upd_info, W_[CCCS]);
253 LDV_RECORD_CREATE(y);
254 StgThunk_payload(y,0) = z;
256 StgMutVar_var(R1) = y;
257 foreign "C" dirty_MUT_VAR(BaseReg "ptr", R1 "ptr") [R1];
259 TICK_ALLOC_THUNK_1();
260 CCCS_ALLOC(THUNK_1_SIZE);
261 r = y - THUNK_1_SIZE;
262 SET_HDR(r, stg_sel_1_upd_info, W_[CCCS]);
263 LDV_RECORD_CREATE(r);
264 StgThunk_payload(r,0) = z;
266 #if defined(THREADED_RTS)
267 RELEASE_LOCK(atomic_modify_mutvar_mutex "ptr") [];
273 /* -----------------------------------------------------------------------------
274 Weak Pointer Primitives
275 -------------------------------------------------------------------------- */
277 STRING(stg_weak_msg,"New weak pointer at %p\n")
283 R3 = finalizer (or NULL)
288 R3 = stg_NO_FINALIZER_closure;
291 ALLOC_PRIM( SIZEOF_StgWeak, R1_PTR & R2_PTR & R3_PTR, mkWeakzh_fast );
293 w = Hp - SIZEOF_StgWeak + WDS(1);
294 SET_HDR(w, stg_WEAK_info, W_[CCCS]);
297 StgWeak_value(w) = R2;
298 StgWeak_finalizer(w) = R3;
300 StgWeak_link(w) = W_[weak_ptr_list];
301 W_[weak_ptr_list] = w;
303 IF_DEBUG(weak, foreign "C" debugBelch(stg_weak_msg,w) []);
318 if (GET_INFO(w) == stg_DEAD_WEAK_info) {
319 RET_NP(0,stg_NO_FINALIZER_closure);
325 // A weak pointer is inherently used, so we do not need to call
326 // LDV_recordDead_FILL_SLOP_DYNAMIC():
327 // LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)w);
328 // or, LDV_recordDead():
329 // LDV_recordDead((StgClosure *)w, sizeofW(StgWeak) - sizeofW(StgProfHeader));
330 // Furthermore, when PROFILING is turned on, dead weak pointers are exactly as
331 // large as weak pointers, so there is no need to fill the slop, either.
332 // See stg_DEAD_WEAK_info in StgMiscClosures.hc.
336 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
338 SET_INFO(w,stg_DEAD_WEAK_info);
339 LDV_RECORD_CREATE(w);
341 f = StgWeak_finalizer(w);
342 StgDeadWeak_link(w) = StgWeak_link(w);
344 /* return the finalizer */
345 if (f == stg_NO_FINALIZER_closure) {
346 RET_NP(0,stg_NO_FINALIZER_closure);
358 if (GET_INFO(w) == stg_WEAK_info) {
360 val = StgWeak_value(w);
368 /* -----------------------------------------------------------------------------
369 Arbitrary-precision Integer operations.
371 There are some assumptions in this code that mp_limb_t == W_. This is
372 the case for all the platforms that GHC supports, currently.
373 -------------------------------------------------------------------------- */
377 /* arguments: R1 = Int# */
379 W_ val, s, p; /* to avoid aliasing */
382 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(1), NO_PTRS, int2Integerzh_fast );
384 p = Hp - SIZEOF_StgArrWords;
385 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
386 StgArrWords_words(p) = 1;
388 /* mpz_set_si is inlined here, makes things simpler */
401 /* returns (# size :: Int#,
410 /* arguments: R1 = Word# */
412 W_ val, s, p; /* to avoid aliasing */
416 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(1), NO_PTRS, word2Integerzh_fast);
418 p = Hp - SIZEOF_StgArrWords;
419 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
420 StgArrWords_words(p) = 1;
429 /* returns (# size :: Int#,
430 data :: ByteArray# #)
437 * 'long long' primops for converting to/from Integers.
440 #ifdef SUPPORT_LONG_LONGS
442 int64ToIntegerzh_fast
444 /* arguments: L1 = Int64# */
447 W_ hi, lo, s, neg, words_needed, p;
452 hi = TO_W_(val >> 32);
455 if ( hi != 0 && hi != 0xFFFFFFFF ) {
458 // minimum is one word
462 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(words_needed),
463 NO_PTRS, int64ToIntegerzh_fast );
465 p = Hp - SIZEOF_StgArrWords - WDS(words_needed) + WDS(1);
466 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
467 StgArrWords_words(p) = words_needed;
479 if ( words_needed == 2 ) {
487 } else /* val==0 */ {
495 /* returns (# size :: Int#,
496 data :: ByteArray# #)
500 word64ToIntegerzh_fast
502 /* arguments: L1 = Word64# */
505 W_ hi, lo, s, words_needed, p;
508 hi = TO_W_(val >> 32);
517 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(words_needed),
518 NO_PTRS, word64ToIntegerzh_fast );
520 p = Hp - SIZEOF_StgArrWords - WDS(words_needed) + WDS(1);
521 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
522 StgArrWords_words(p) = words_needed;
532 } else /* val==0 */ {
537 /* returns (# size :: Int#,
538 data :: ByteArray# #)
545 #endif /* SUPPORT_LONG_LONGS */
547 /* ToDo: this is shockingly inefficient */
552 bits8 [SIZEOF_MP_INT];
557 bits8 [SIZEOF_MP_INT];
562 bits8 [SIZEOF_MP_INT];
567 bits8 [SIZEOF_MP_INT];
572 #define FETCH_MP_TEMP(X) \
574 X = BaseReg + (OFFSET_StgRegTable_r ## X);
576 #define FETCH_MP_TEMP(X) /* Nothing */
579 #define GMP_TAKE2_RET1(name,mp_fun) \
584 FETCH_MP_TEMP(mp_tmp1); \
585 FETCH_MP_TEMP(mp_tmp2); \
586 FETCH_MP_TEMP(mp_result1) \
587 FETCH_MP_TEMP(mp_result2); \
589 /* call doYouWantToGC() */ \
590 MAYBE_GC(R2_PTR & R4_PTR, name); \
597 MP_INT__mp_alloc(mp_tmp1) = W_TO_INT(StgArrWords_words(d1)); \
598 MP_INT__mp_size(mp_tmp1) = (s1); \
599 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(d1); \
600 MP_INT__mp_alloc(mp_tmp2) = W_TO_INT(StgArrWords_words(d2)); \
601 MP_INT__mp_size(mp_tmp2) = (s2); \
602 MP_INT__mp_d(mp_tmp2) = BYTE_ARR_CTS(d2); \
604 foreign "C" __gmpz_init(mp_result1 "ptr") []; \
606 /* Perform the operation */ \
607 foreign "C" mp_fun(mp_result1 "ptr",mp_tmp1 "ptr",mp_tmp2 "ptr") []; \
609 RET_NP(TO_W_(MP_INT__mp_size(mp_result1)), \
610 MP_INT__mp_d(mp_result1) - SIZEOF_StgArrWords); \
613 #define GMP_TAKE1_RET1(name,mp_fun) \
618 FETCH_MP_TEMP(mp_tmp1); \
619 FETCH_MP_TEMP(mp_result1) \
621 /* call doYouWantToGC() */ \
622 MAYBE_GC(R2_PTR, name); \
627 MP_INT__mp_alloc(mp_tmp1) = W_TO_INT(StgArrWords_words(d1)); \
628 MP_INT__mp_size(mp_tmp1) = (s1); \
629 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(d1); \
631 foreign "C" __gmpz_init(mp_result1 "ptr") []; \
633 /* Perform the operation */ \
634 foreign "C" mp_fun(mp_result1 "ptr",mp_tmp1 "ptr") []; \
636 RET_NP(TO_W_(MP_INT__mp_size(mp_result1)), \
637 MP_INT__mp_d(mp_result1) - SIZEOF_StgArrWords); \
640 #define GMP_TAKE2_RET2(name,mp_fun) \
645 FETCH_MP_TEMP(mp_tmp1); \
646 FETCH_MP_TEMP(mp_tmp2); \
647 FETCH_MP_TEMP(mp_result1) \
648 FETCH_MP_TEMP(mp_result2) \
650 /* call doYouWantToGC() */ \
651 MAYBE_GC(R2_PTR & R4_PTR, name); \
658 MP_INT__mp_alloc(mp_tmp1) = W_TO_INT(StgArrWords_words(d1)); \
659 MP_INT__mp_size(mp_tmp1) = (s1); \
660 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(d1); \
661 MP_INT__mp_alloc(mp_tmp2) = W_TO_INT(StgArrWords_words(d2)); \
662 MP_INT__mp_size(mp_tmp2) = (s2); \
663 MP_INT__mp_d(mp_tmp2) = BYTE_ARR_CTS(d2); \
665 foreign "C" __gmpz_init(mp_result1 "ptr") []; \
666 foreign "C" __gmpz_init(mp_result2 "ptr") []; \
668 /* Perform the operation */ \
669 foreign "C" mp_fun(mp_result1 "ptr",mp_result2 "ptr",mp_tmp1 "ptr",mp_tmp2 "ptr") []; \
671 RET_NPNP(TO_W_(MP_INT__mp_size(mp_result1)), \
672 MP_INT__mp_d(mp_result1) - SIZEOF_StgArrWords, \
673 TO_W_(MP_INT__mp_size(mp_result2)), \
674 MP_INT__mp_d(mp_result2) - SIZEOF_StgArrWords); \
677 GMP_TAKE2_RET1(plusIntegerzh_fast, __gmpz_add)
678 GMP_TAKE2_RET1(minusIntegerzh_fast, __gmpz_sub)
679 GMP_TAKE2_RET1(timesIntegerzh_fast, __gmpz_mul)
680 GMP_TAKE2_RET1(gcdIntegerzh_fast, __gmpz_gcd)
681 GMP_TAKE2_RET1(quotIntegerzh_fast, __gmpz_tdiv_q)
682 GMP_TAKE2_RET1(remIntegerzh_fast, __gmpz_tdiv_r)
683 GMP_TAKE2_RET1(divExactIntegerzh_fast, __gmpz_divexact)
684 GMP_TAKE2_RET1(andIntegerzh_fast, __gmpz_and)
685 GMP_TAKE2_RET1(orIntegerzh_fast, __gmpz_ior)
686 GMP_TAKE2_RET1(xorIntegerzh_fast, __gmpz_xor)
687 GMP_TAKE1_RET1(complementIntegerzh_fast, __gmpz_com)
689 GMP_TAKE2_RET2(quotRemIntegerzh_fast, __gmpz_tdiv_qr)
690 GMP_TAKE2_RET2(divModIntegerzh_fast, __gmpz_fdiv_qr)
694 mp_tmp_w: W_; // NB. mp_tmp_w is really an here mp_limb_t
700 /* R1 = the first Int#; R2 = the second Int# */
702 FETCH_MP_TEMP(mp_tmp_w);
705 (r) = foreign "C" __gmpn_gcd_1(mp_tmp_w "ptr", 1, R2) [];
708 /* Result parked in R1, return via info-pointer at TOS */
709 jump %ENTRY_CODE(Sp(0));
715 /* R1 = s1; R2 = d1; R3 = the int */
717 (s1) = foreign "C" __gmpn_gcd_1( BYTE_ARR_CTS(R2) "ptr", R1, R3) [];
720 /* Result parked in R1, return via info-pointer at TOS */
721 jump %ENTRY_CODE(Sp(0));
727 /* R1 = s1; R2 = d1; R3 = the int */
728 W_ usize, vsize, v_digit, u_digit;
734 // paraphrased from __gmpz_cmp_si() in the GMP sources
735 if (%gt(v_digit,0)) {
738 if (%lt(v_digit,0)) {
744 if (usize != vsize) {
746 jump %ENTRY_CODE(Sp(0));
751 jump %ENTRY_CODE(Sp(0));
754 u_digit = W_[BYTE_ARR_CTS(R2)];
756 if (u_digit == v_digit) {
758 jump %ENTRY_CODE(Sp(0));
761 if (%gtu(u_digit,v_digit)) { // NB. unsigned: these are mp_limb_t's
767 jump %ENTRY_CODE(Sp(0));
772 /* R1 = s1; R2 = d1; R3 = s2; R4 = d2 */
773 W_ usize, vsize, size, up, vp;
776 // paraphrased from __gmpz_cmp() in the GMP sources
780 if (usize != vsize) {
782 jump %ENTRY_CODE(Sp(0));
787 jump %ENTRY_CODE(Sp(0));
790 if (%lt(usize,0)) { // NB. not <, which is unsigned
796 up = BYTE_ARR_CTS(R2);
797 vp = BYTE_ARR_CTS(R4);
799 (cmp) = foreign "C" __gmpn_cmp(up "ptr", vp "ptr", size) [];
801 if (cmp == 0 :: CInt) {
803 jump %ENTRY_CODE(Sp(0));
806 if (%lt(cmp,0 :: CInt) == %lt(usize,0)) {
811 /* Result parked in R1, return via info-pointer at TOS */
812 jump %ENTRY_CODE(Sp(0));
824 r = W_[R2 + SIZEOF_StgArrWords];
829 /* Result parked in R1, return via info-pointer at TOS */
831 jump %ENTRY_CODE(Sp(0));
843 r = W_[R2 + SIZEOF_StgArrWords];
848 /* Result parked in R1, return via info-pointer at TOS */
850 jump %ENTRY_CODE(Sp(0));
857 FETCH_MP_TEMP(mp_tmp1);
858 FETCH_MP_TEMP(mp_tmp_w);
860 /* arguments: F1 = Float# */
863 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(1), NO_PTRS, decodeFloatzh_fast );
865 /* Be prepared to tell Lennart-coded __decodeFloat
866 where mantissa._mp_d can be put (it does not care about the rest) */
867 p = Hp - SIZEOF_StgArrWords;
868 SET_HDR(p,stg_ARR_WORDS_info,W_[CCCS]);
869 StgArrWords_words(p) = 1;
870 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(p);
872 /* Perform the operation */
873 foreign "C" __decodeFloat(mp_tmp1 "ptr",mp_tmp_w "ptr" ,arg) [];
875 /* returns: (Int# (expn), Int#, ByteArray#) */
876 RET_NNP(W_[mp_tmp_w], TO_W_(MP_INT__mp_size(mp_tmp1)), p);
879 decodeFloatzuIntzh_fast
883 FETCH_MP_TEMP(mp_tmp1);
884 FETCH_MP_TEMP(mp_tmp_w);
886 /* arguments: F1 = Float# */
889 /* Perform the operation */
890 foreign "C" __decodeFloat_Int(mp_tmp1 "ptr", mp_tmp_w "ptr", arg) [];
892 /* returns: (Int# (mantissa), Int# (exponent)) */
893 RET_NN(W_[mp_tmp1], W_[mp_tmp_w]);
896 #define DOUBLE_MANTISSA_SIZE SIZEOF_DOUBLE
897 #define ARR_SIZE (SIZEOF_StgArrWords + DOUBLE_MANTISSA_SIZE)
903 FETCH_MP_TEMP(mp_tmp1);
904 FETCH_MP_TEMP(mp_tmp_w);
906 /* arguments: D1 = Double# */
909 ALLOC_PRIM( ARR_SIZE, NO_PTRS, decodeDoublezh_fast );
911 /* Be prepared to tell Lennart-coded __decodeDouble
912 where mantissa.d can be put (it does not care about the rest) */
913 p = Hp - ARR_SIZE + WDS(1);
914 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
915 StgArrWords_words(p) = BYTES_TO_WDS(DOUBLE_MANTISSA_SIZE);
916 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(p);
918 /* Perform the operation */
919 foreign "C" __decodeDouble(mp_tmp1 "ptr", mp_tmp_w "ptr",arg) [];
921 /* returns: (Int# (expn), Int#, ByteArray#) */
922 RET_NNP(W_[mp_tmp_w], TO_W_(MP_INT__mp_size(mp_tmp1)), p);
925 decodeDoublezu2Intzh_fast
929 FETCH_MP_TEMP(mp_tmp1);
930 FETCH_MP_TEMP(mp_tmp2);
931 FETCH_MP_TEMP(mp_tmp_w);
933 /* arguments: D1 = Double# */
936 /* Perform the operation */
937 foreign "C" __decodeDouble_2Int(mp_tmp1 "ptr", mp_tmp2 "ptr", mp_tmp_w "ptr", arg) [];
939 /* returns: (Int# (mant high), Int# (mant low), Int# (expn)) */
940 RET_NNN(W_[mp_tmp1], W_[mp_tmp2], W_[mp_tmp_w]);
943 /* -----------------------------------------------------------------------------
944 * Concurrency primitives
945 * -------------------------------------------------------------------------- */
949 /* args: R1 = closure to spark */
951 MAYBE_GC(R1_PTR, forkzh_fast);
957 ("ptr" threadid) = foreign "C" createIOThread( MyCapability() "ptr",
958 RtsFlags_GcFlags_initialStkSize(RtsFlags),
961 /* start blocked if the current thread is blocked */
962 StgTSO_flags(threadid) =
963 StgTSO_flags(threadid) | (StgTSO_flags(CurrentTSO) &
964 (TSO_BLOCKEX::I32 | TSO_INTERRUPTIBLE::I32));
966 foreign "C" scheduleThread(MyCapability() "ptr", threadid "ptr") [];
968 // switch at the earliest opportunity
969 CInt[context_switch] = 1 :: CInt;
976 /* args: R1 = cpu, R2 = closure to spark */
978 MAYBE_GC(R2_PTR, forkOnzh_fast);
986 ("ptr" threadid) = foreign "C" createIOThread( MyCapability() "ptr",
987 RtsFlags_GcFlags_initialStkSize(RtsFlags),
990 /* start blocked if the current thread is blocked */
991 StgTSO_flags(threadid) =
992 StgTSO_flags(threadid) | (StgTSO_flags(CurrentTSO) &
993 (TSO_BLOCKEX::I32 | TSO_INTERRUPTIBLE::I32));
995 foreign "C" scheduleThreadOn(MyCapability() "ptr", cpu, threadid "ptr") [];
997 // switch at the earliest opportunity
998 CInt[context_switch] = 1 :: CInt;
1005 jump stg_yield_noregs;
1020 foreign "C" labelThread(R1 "ptr", R2 "ptr") [];
1022 jump %ENTRY_CODE(Sp(0));
1025 isCurrentThreadBoundzh_fast
1029 (r) = foreign "C" isThreadBound(CurrentTSO) [];
1034 /* -----------------------------------------------------------------------------
1036 * -------------------------------------------------------------------------- */
1040 #define IF_NOT_REG_R1(x)
1043 #define IF_NOT_REG_R1(x) x
1046 // Catch retry frame ------------------------------------------------------------
1048 INFO_TABLE_RET(stg_catch_retry_frame, CATCH_RETRY_FRAME,
1049 #if defined(PROFILING)
1050 W_ unused1, W_ unused2,
1052 W_ unused3, "ptr" W_ unused4, "ptr" W_ unused5)
1054 W_ r, frame, trec, outer;
1055 IF_NOT_REG_R1(W_ rval; rval = Sp(0); Sp_adj(1); )
1058 trec = StgTSO_trec(CurrentTSO);
1059 ("ptr" outer) = foreign "C" stmGetEnclosingTRec(trec "ptr") [];
1060 (r) = foreign "C" stmCommitNestedTransaction(MyCapability() "ptr", trec "ptr") [];
1062 /* Succeeded (either first branch or second branch) */
1063 StgTSO_trec(CurrentTSO) = outer;
1064 Sp = Sp + SIZEOF_StgCatchRetryFrame;
1065 IF_NOT_REG_R1(Sp_adj(-1); Sp(0) = rval;)
1066 jump %ENTRY_CODE(Sp(SP_OFF));
1068 /* Did not commit: re-execute */
1070 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];
1071 StgTSO_trec(CurrentTSO) = new_trec;
1072 if (StgCatchRetryFrame_running_alt_code(frame) != 0::I32) {
1073 R1 = StgCatchRetryFrame_alt_code(frame);
1075 R1 = StgCatchRetryFrame_first_code(frame);
1082 // Atomically frame ------------------------------------------------------------
1084 INFO_TABLE_RET(stg_atomically_frame, ATOMICALLY_FRAME,
1085 #if defined(PROFILING)
1086 W_ unused1, W_ unused2,
1088 "ptr" W_ unused3, "ptr" W_ unused4)
1090 W_ frame, trec, valid, next_invariant, q, outer;
1091 IF_NOT_REG_R1(W_ rval; rval = Sp(0); Sp_adj(1); )
1094 trec = StgTSO_trec(CurrentTSO);
1095 ("ptr" outer) = foreign "C" stmGetEnclosingTRec(trec "ptr") [];
1097 if (outer == NO_TREC) {
1098 /* First time back at the atomically frame -- pick up invariants */
1099 ("ptr" q) = foreign "C" stmGetInvariantsToCheck(MyCapability() "ptr", trec "ptr") [];
1100 StgAtomicallyFrame_next_invariant_to_check(frame) = q;
1103 /* Second/subsequent time back at the atomically frame -- abort the
1104 * tx that's checking the invariant and move on to the next one */
1105 StgTSO_trec(CurrentTSO) = outer;
1106 q = StgAtomicallyFrame_next_invariant_to_check(frame);
1107 StgInvariantCheckQueue_my_execution(q) = trec;
1108 foreign "C" stmAbortTransaction(MyCapability() "ptr", trec "ptr") [];
1109 /* Don't free trec -- it's linked from q and will be stashed in the
1110 * invariant if we eventually commit. */
1111 q = StgInvariantCheckQueue_next_queue_entry(q);
1112 StgAtomicallyFrame_next_invariant_to_check(frame) = q;
1116 q = StgAtomicallyFrame_next_invariant_to_check(frame);
1118 if (q != END_INVARIANT_CHECK_QUEUE) {
1119 /* We can't commit yet: another invariant to check */
1120 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", trec "ptr") [];
1121 StgTSO_trec(CurrentTSO) = trec;
1123 next_invariant = StgInvariantCheckQueue_invariant(q);
1124 R1 = StgAtomicInvariant_code(next_invariant);
1129 /* We've got no more invariants to check, try to commit */
1130 (valid) = foreign "C" stmCommitTransaction(MyCapability() "ptr", trec "ptr") [];
1132 /* Transaction was valid: commit succeeded */
1133 StgTSO_trec(CurrentTSO) = NO_TREC;
1134 Sp = Sp + SIZEOF_StgAtomicallyFrame;
1135 IF_NOT_REG_R1(Sp_adj(-1); Sp(0) = rval;)
1136 jump %ENTRY_CODE(Sp(SP_OFF));
1138 /* Transaction was not valid: try again */
1139 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", NO_TREC "ptr") [];
1140 StgTSO_trec(CurrentTSO) = trec;
1141 StgAtomicallyFrame_next_invariant_to_check(frame) = END_INVARIANT_CHECK_QUEUE;
1142 R1 = StgAtomicallyFrame_code(frame);
1148 INFO_TABLE_RET(stg_atomically_waiting_frame, ATOMICALLY_FRAME,
1149 #if defined(PROFILING)
1150 W_ unused1, W_ unused2,
1152 "ptr" W_ unused3, "ptr" W_ unused4)
1154 W_ frame, trec, valid;
1155 IF_NOT_REG_R1(W_ rval; rval = Sp(0); Sp_adj(1); )
1159 /* The TSO is currently waiting: should we stop waiting? */
1160 (valid) = foreign "C" stmReWait(MyCapability() "ptr", CurrentTSO "ptr") [];
1162 /* Previous attempt is still valid: no point trying again yet */
1163 IF_NOT_REG_R1(Sp_adj(-2);
1164 Sp(1) = stg_NO_FINALIZER_closure;
1165 Sp(0) = stg_ut_1_0_unreg_info;)
1166 jump stg_block_noregs;
1168 /* Previous attempt is no longer valid: try again */
1169 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", NO_TREC "ptr") [];
1170 StgTSO_trec(CurrentTSO) = trec;
1171 StgHeader_info(frame) = stg_atomically_frame_info;
1172 R1 = StgAtomicallyFrame_code(frame);
1177 // STM catch frame --------------------------------------------------------------
1185 /* Catch frames are very similar to update frames, but when entering
1186 * one we just pop the frame off the stack and perform the correct
1187 * kind of return to the activation record underneath us on the stack.
1190 INFO_TABLE_RET(stg_catch_stm_frame, CATCH_STM_FRAME,
1191 #if defined(PROFILING)
1192 W_ unused1, W_ unused2,
1194 "ptr" W_ unused3, "ptr" W_ unused4)
1196 IF_NOT_REG_R1(W_ rval; rval = Sp(0); Sp_adj(1); )
1197 W_ r, frame, trec, outer;
1199 trec = StgTSO_trec(CurrentTSO);
1200 ("ptr" outer) = foreign "C" stmGetEnclosingTRec(trec "ptr") [];
1201 (r) = foreign "C" stmCommitNestedTransaction(MyCapability() "ptr", trec "ptr") [];
1203 /* Commit succeeded */
1204 StgTSO_trec(CurrentTSO) = outer;
1205 Sp = Sp + SIZEOF_StgCatchSTMFrame;
1206 IF_NOT_REG_R1(Sp_adj(-1); Sp(0) = rval;)
1211 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];
1212 StgTSO_trec(CurrentTSO) = new_trec;
1213 R1 = StgCatchSTMFrame_code(frame);
1219 // Primop definition ------------------------------------------------------------
1227 // stmStartTransaction may allocate
1228 MAYBE_GC (R1_PTR, atomicallyzh_fast);
1230 /* Args: R1 = m :: STM a */
1231 STK_CHK_GEN(SIZEOF_StgAtomicallyFrame + WDS(1), R1_PTR, atomicallyzh_fast);
1233 old_trec = StgTSO_trec(CurrentTSO);
1235 /* Nested transactions are not allowed; raise an exception */
1236 if (old_trec != NO_TREC) {
1237 R1 = base_GHCziIOBase_NestedAtomically_closure;
1241 /* Set up the atomically frame */
1242 Sp = Sp - SIZEOF_StgAtomicallyFrame;
1245 SET_HDR(frame,stg_atomically_frame_info, W_[CCCS]);
1246 StgAtomicallyFrame_code(frame) = R1;
1247 StgAtomicallyFrame_next_invariant_to_check(frame) = END_INVARIANT_CHECK_QUEUE;
1249 /* Start the memory transcation */
1250 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", old_trec "ptr") [R1];
1251 StgTSO_trec(CurrentTSO) = new_trec;
1253 /* Apply R1 to the realworld token */
1262 /* Args: R1 :: STM a */
1263 /* Args: R2 :: Exception -> STM a */
1264 STK_CHK_GEN(SIZEOF_StgCatchSTMFrame + WDS(1), R1_PTR & R2_PTR, catchSTMzh_fast);
1266 /* Set up the catch frame */
1267 Sp = Sp - SIZEOF_StgCatchSTMFrame;
1270 SET_HDR(frame, stg_catch_stm_frame_info, W_[CCCS]);
1271 StgCatchSTMFrame_handler(frame) = R2;
1272 StgCatchSTMFrame_code(frame) = R1;
1274 /* Start a nested transaction to run the body of the try block in */
1277 cur_trec = StgTSO_trec(CurrentTSO);
1278 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", cur_trec "ptr");
1279 StgTSO_trec(CurrentTSO) = new_trec;
1281 /* Apply R1 to the realworld token */
1292 // stmStartTransaction may allocate
1293 MAYBE_GC (R1_PTR & R2_PTR, catchRetryzh_fast);
1295 /* Args: R1 :: STM a */
1296 /* Args: R2 :: STM a */
1297 STK_CHK_GEN(SIZEOF_StgCatchRetryFrame + WDS(1), R1_PTR & R2_PTR, catchRetryzh_fast);
1299 /* Start a nested transaction within which to run the first code */
1300 trec = StgTSO_trec(CurrentTSO);
1301 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", trec "ptr") [R1,R2];
1302 StgTSO_trec(CurrentTSO) = new_trec;
1304 /* Set up the catch-retry frame */
1305 Sp = Sp - SIZEOF_StgCatchRetryFrame;
1308 SET_HDR(frame, stg_catch_retry_frame_info, W_[CCCS]);
1309 StgCatchRetryFrame_running_alt_code(frame) = 0 :: CInt; // false;
1310 StgCatchRetryFrame_first_code(frame) = R1;
1311 StgCatchRetryFrame_alt_code(frame) = R2;
1313 /* Apply R1 to the realworld token */
1326 MAYBE_GC (NO_PTRS, retryzh_fast); // STM operations may allocate
1328 // Find the enclosing ATOMICALLY_FRAME or CATCH_RETRY_FRAME
1330 StgTSO_sp(CurrentTSO) = Sp;
1331 (frame_type) = foreign "C" findRetryFrameHelper(CurrentTSO "ptr") [];
1332 Sp = StgTSO_sp(CurrentTSO);
1334 trec = StgTSO_trec(CurrentTSO);
1335 ("ptr" outer) = foreign "C" stmGetEnclosingTRec(trec "ptr") [];
1337 if (frame_type == CATCH_RETRY_FRAME) {
1338 // The retry reaches a CATCH_RETRY_FRAME before the atomic frame
1339 ASSERT(outer != NO_TREC);
1340 // Abort the transaction attempting the current branch
1341 foreign "C" stmAbortTransaction(MyCapability() "ptr", trec "ptr") [];
1342 foreign "C" stmFreeAbortedTRec(MyCapability() "ptr", trec "ptr") [];
1343 if (!StgCatchRetryFrame_running_alt_code(frame) != 0::I32) {
1344 // Retry in the first branch: try the alternative
1345 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];
1346 StgTSO_trec(CurrentTSO) = trec;
1347 StgCatchRetryFrame_running_alt_code(frame) = 1 :: CInt; // true;
1348 R1 = StgCatchRetryFrame_alt_code(frame);
1351 // Retry in the alternative code: propagate the retry
1352 StgTSO_trec(CurrentTSO) = outer;
1353 Sp = Sp + SIZEOF_StgCatchRetryFrame;
1354 goto retry_pop_stack;
1358 // We've reached the ATOMICALLY_FRAME: attempt to wait
1359 ASSERT(frame_type == ATOMICALLY_FRAME);
1360 if (outer != NO_TREC) {
1361 // We called retry while checking invariants, so abort the current
1362 // invariant check (merging its TVar accesses into the parents read
1363 // set so we'll wait on them)
1364 foreign "C" stmAbortTransaction(MyCapability() "ptr", trec "ptr") [];
1365 foreign "C" stmFreeAbortedTRec(MyCapability() "ptr", trec "ptr") [];
1367 StgTSO_trec(CurrentTSO) = trec;
1368 ("ptr" outer) = foreign "C" stmGetEnclosingTRec(trec "ptr") [];
1370 ASSERT(outer == NO_TREC);
1372 (r) = foreign "C" stmWait(MyCapability() "ptr", CurrentTSO "ptr", trec "ptr") [];
1374 // Transaction was valid: stmWait put us on the TVars' queues, we now block
1375 StgHeader_info(frame) = stg_atomically_waiting_frame_info;
1377 // Fix up the stack in the unregisterised case: the return convention is different.
1378 IF_NOT_REG_R1(Sp_adj(-2);
1379 Sp(1) = stg_NO_FINALIZER_closure;
1380 Sp(0) = stg_ut_1_0_unreg_info;)
1381 R3 = trec; // passing to stmWaitUnblock()
1382 jump stg_block_stmwait;
1384 // Transaction was not valid: retry immediately
1385 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];
1386 StgTSO_trec(CurrentTSO) = trec;
1387 R1 = StgAtomicallyFrame_code(frame);
1398 /* Args: R1 = invariant closure */
1399 MAYBE_GC (R1_PTR, checkzh_fast);
1401 trec = StgTSO_trec(CurrentTSO);
1403 foreign "C" stmAddInvariantToCheck(MyCapability() "ptr",
1407 jump %ENTRY_CODE(Sp(0));
1416 /* Args: R1 = initialisation value */
1418 MAYBE_GC (R1_PTR, newTVarzh_fast);
1420 ("ptr" tv) = foreign "C" stmNewTVar(MyCapability() "ptr", new_value "ptr") [];
1431 /* Args: R1 = TVar closure */
1433 MAYBE_GC (R1_PTR, readTVarzh_fast); // Call to stmReadTVar may allocate
1434 trec = StgTSO_trec(CurrentTSO);
1436 ("ptr" result) = foreign "C" stmReadTVar(MyCapability() "ptr", trec "ptr", tvar "ptr") [];
1448 /* Args: R1 = TVar closure */
1449 /* R2 = New value */
1451 MAYBE_GC (R1_PTR & R2_PTR, writeTVarzh_fast); // Call to stmWriteTVar may allocate
1452 trec = StgTSO_trec(CurrentTSO);
1455 foreign "C" stmWriteTVar(MyCapability() "ptr", trec "ptr", tvar "ptr", new_value "ptr") [];
1457 jump %ENTRY_CODE(Sp(0));
1461 /* -----------------------------------------------------------------------------
1464 * take & putMVar work as follows. Firstly, an important invariant:
1466 * If the MVar is full, then the blocking queue contains only
1467 * threads blocked on putMVar, and if the MVar is empty then the
1468 * blocking queue contains only threads blocked on takeMVar.
1471 * MVar empty : then add ourselves to the blocking queue
1472 * MVar full : remove the value from the MVar, and
1473 * blocking queue empty : return
1474 * blocking queue non-empty : perform the first blocked putMVar
1475 * from the queue, and wake up the
1476 * thread (MVar is now full again)
1478 * putMVar is just the dual of the above algorithm.
1480 * How do we "perform a putMVar"? Well, we have to fiddle around with
1481 * the stack of the thread waiting to do the putMVar. See
1482 * stg_block_putmvar and stg_block_takemvar in HeapStackCheck.c for
1483 * the stack layout, and the PerformPut and PerformTake macros below.
1485 * It is important that a blocked take or put is woken up with the
1486 * take/put already performed, because otherwise there would be a
1487 * small window of vulnerability where the thread could receive an
1488 * exception and never perform its take or put, and we'd end up with a
1491 * -------------------------------------------------------------------------- */
1495 /* args: R1 = MVar closure */
1497 if (StgMVar_value(R1) == stg_END_TSO_QUEUE_closure) {
1509 ALLOC_PRIM ( SIZEOF_StgMVar, NO_PTRS, newMVarzh_fast );
1511 mvar = Hp - SIZEOF_StgMVar + WDS(1);
1512 SET_HDR(mvar,stg_MVAR_DIRTY_info,W_[CCCS]);
1513 // MVARs start dirty: generation 0 has no mutable list
1514 StgMVar_head(mvar) = stg_END_TSO_QUEUE_closure;
1515 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1516 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1521 /* If R1 isn't available, pass it on the stack */
1523 #define PerformTake(tso, value) \
1524 W_[StgTSO_sp(tso) + WDS(1)] = value; \
1525 W_[StgTSO_sp(tso) + WDS(0)] = stg_gc_unpt_r1_info;
1527 #define PerformTake(tso, value) \
1528 W_[StgTSO_sp(tso) + WDS(1)] = value; \
1529 W_[StgTSO_sp(tso) + WDS(0)] = stg_ut_1_0_unreg_info;
1532 #define PerformPut(tso,lval) \
1533 StgTSO_sp(tso) = StgTSO_sp(tso) + WDS(3); \
1534 lval = W_[StgTSO_sp(tso) - WDS(1)];
1538 W_ mvar, val, info, tso;
1540 /* args: R1 = MVar closure */
1543 #if defined(THREADED_RTS)
1544 ("ptr" info) = foreign "C" lockClosure(mvar "ptr") [];
1546 info = GET_INFO(mvar);
1549 if (info == stg_MVAR_CLEAN_info) {
1550 foreign "C" dirty_MVAR(BaseReg "ptr", mvar "ptr");
1553 /* If the MVar is empty, put ourselves on its blocking queue,
1554 * and wait until we're woken up.
1556 if (StgMVar_value(mvar) == stg_END_TSO_QUEUE_closure) {
1557 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1558 StgMVar_head(mvar) = CurrentTSO;
1560 StgTSO_link(StgMVar_tail(mvar)) = CurrentTSO;
1562 StgTSO_link(CurrentTSO) = stg_END_TSO_QUEUE_closure;
1563 StgTSO_why_blocked(CurrentTSO) = BlockedOnMVar::I16;
1564 StgTSO_block_info(CurrentTSO) = mvar;
1565 StgMVar_tail(mvar) = CurrentTSO;
1567 jump stg_block_takemvar;
1570 /* we got the value... */
1571 val = StgMVar_value(mvar);
1573 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure)
1575 /* There are putMVar(s) waiting...
1576 * wake up the first thread on the queue
1578 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1580 /* actually perform the putMVar for the thread that we just woke up */
1581 tso = StgMVar_head(mvar);
1582 PerformPut(tso,StgMVar_value(mvar));
1585 #if defined(GRAN) || defined(PAR)
1586 /* ToDo: check 2nd arg (mvar) is right */
1587 ("ptr" tso) = foreign "C" unblockOne(StgMVar_head(mvar),mvar) [];
1588 StgMVar_head(mvar) = tso;
1590 ("ptr" tso) = foreign "C" unblockOne(MyCapability() "ptr",
1591 StgMVar_head(mvar) "ptr") [];
1592 StgMVar_head(mvar) = tso;
1595 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1596 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1599 #if defined(THREADED_RTS)
1600 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1602 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1608 /* No further putMVars, MVar is now empty */
1609 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1611 #if defined(THREADED_RTS)
1612 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1614 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1624 W_ mvar, val, info, tso;
1626 /* args: R1 = MVar closure */
1630 #if defined(THREADED_RTS)
1631 ("ptr" info) = foreign "C" lockClosure(mvar "ptr") [];
1633 info = GET_INFO(mvar);
1636 if (StgMVar_value(mvar) == stg_END_TSO_QUEUE_closure) {
1637 #if defined(THREADED_RTS)
1638 unlockClosure(mvar, info);
1640 /* HACK: we need a pointer to pass back,
1641 * so we abuse NO_FINALIZER_closure
1643 RET_NP(0, stg_NO_FINALIZER_closure);
1646 if (info == stg_MVAR_CLEAN_info) {
1647 foreign "C" dirty_MVAR(BaseReg "ptr", mvar "ptr");
1650 /* we got the value... */
1651 val = StgMVar_value(mvar);
1653 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1655 /* There are putMVar(s) waiting...
1656 * wake up the first thread on the queue
1658 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1660 /* actually perform the putMVar for the thread that we just woke up */
1661 tso = StgMVar_head(mvar);
1662 PerformPut(tso,StgMVar_value(mvar));
1665 #if defined(GRAN) || defined(PAR)
1666 /* ToDo: check 2nd arg (mvar) is right */
1667 ("ptr" tso) = foreign "C" unblockOne(StgMVar_head(mvar) "ptr", mvar "ptr") [];
1668 StgMVar_head(mvar) = tso;
1670 ("ptr" tso) = foreign "C" unblockOne(MyCapability() "ptr",
1671 StgMVar_head(mvar) "ptr") [];
1672 StgMVar_head(mvar) = tso;
1675 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1676 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1678 #if defined(THREADED_RTS)
1679 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1681 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1686 /* No further putMVars, MVar is now empty */
1687 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1688 #if defined(THREADED_RTS)
1689 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1691 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1703 /* args: R1 = MVar, R2 = value */
1706 #if defined(THREADED_RTS)
1707 ("ptr" info) = foreign "C" lockClosure(mvar "ptr") [R2];
1709 info = GET_INFO(mvar);
1712 if (info == stg_MVAR_CLEAN_info) {
1713 foreign "C" dirty_MVAR(BaseReg "ptr", mvar "ptr");
1716 if (StgMVar_value(mvar) != stg_END_TSO_QUEUE_closure) {
1717 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1718 StgMVar_head(mvar) = CurrentTSO;
1720 StgTSO_link(StgMVar_tail(mvar)) = CurrentTSO;
1722 StgTSO_link(CurrentTSO) = stg_END_TSO_QUEUE_closure;
1723 StgTSO_why_blocked(CurrentTSO) = BlockedOnMVar::I16;
1724 StgTSO_block_info(CurrentTSO) = mvar;
1725 StgMVar_tail(mvar) = CurrentTSO;
1727 jump stg_block_putmvar;
1730 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1732 /* There are takeMVar(s) waiting: wake up the first one
1734 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1736 /* actually perform the takeMVar */
1737 tso = StgMVar_head(mvar);
1738 PerformTake(tso, R2);
1741 #if defined(GRAN) || defined(PAR)
1742 /* ToDo: check 2nd arg (mvar) is right */
1743 ("ptr" tso) = foreign "C" unblockOne(MyCapability() "ptr", StgMVar_head(mvar) "ptr",mvar "ptr") [];
1744 StgMVar_head(mvar) = tso;
1746 ("ptr" tso) = foreign "C" unblockOne(MyCapability() "ptr", StgMVar_head(mvar) "ptr") [];
1747 StgMVar_head(mvar) = tso;
1750 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1751 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1754 #if defined(THREADED_RTS)
1755 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1757 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1759 jump %ENTRY_CODE(Sp(0));
1763 /* No further takes, the MVar is now full. */
1764 StgMVar_value(mvar) = R2;
1766 #if defined(THREADED_RTS)
1767 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1769 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1771 jump %ENTRY_CODE(Sp(0));
1774 /* ToDo: yield afterward for better communication performance? */
1782 /* args: R1 = MVar, R2 = value */
1785 #if defined(THREADED_RTS)
1786 ("ptr" info) = foreign "C" lockClosure(mvar "ptr") [R2];
1788 info = GET_INFO(mvar);
1791 if (StgMVar_value(mvar) != stg_END_TSO_QUEUE_closure) {
1792 #if defined(THREADED_RTS)
1793 unlockClosure(mvar, info);
1798 if (info == stg_MVAR_CLEAN_info) {
1799 foreign "C" dirty_MVAR(BaseReg "ptr", mvar "ptr");
1802 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1804 /* There are takeMVar(s) waiting: wake up the first one
1806 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1808 /* actually perform the takeMVar */
1809 tso = StgMVar_head(mvar);
1810 PerformTake(tso, R2);
1813 #if defined(GRAN) || defined(PAR)
1814 /* ToDo: check 2nd arg (mvar) is right */
1815 ("ptr" tso) = foreign "C" unblockOne(MyCapability() "ptr", StgMVar_head(mvar) "ptr",mvar "ptr") [];
1816 StgMVar_head(mvar) = tso;
1818 ("ptr" tso) = foreign "C" unblockOne(MyCapability() "ptr", StgMVar_head(mvar) "ptr") [];
1819 StgMVar_head(mvar) = tso;
1822 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1823 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1826 #if defined(THREADED_RTS)
1827 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1829 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1834 /* No further takes, the MVar is now full. */
1835 StgMVar_value(mvar) = R2;
1837 #if defined(THREADED_RTS)
1838 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1840 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1845 /* ToDo: yield afterward for better communication performance? */
1849 /* -----------------------------------------------------------------------------
1850 Stable pointer primitives
1851 ------------------------------------------------------------------------- */
1853 makeStableNamezh_fast
1857 ALLOC_PRIM( SIZEOF_StgStableName, R1_PTR, makeStableNamezh_fast );
1859 (index) = foreign "C" lookupStableName(R1 "ptr") [];
1861 /* Is there already a StableName for this heap object?
1862 * stable_ptr_table is a pointer to an array of snEntry structs.
1864 if ( snEntry_sn_obj(W_[stable_ptr_table] + index*SIZEOF_snEntry) == NULL ) {
1865 sn_obj = Hp - SIZEOF_StgStableName + WDS(1);
1866 SET_HDR(sn_obj, stg_STABLE_NAME_info, W_[CCCS]);
1867 StgStableName_sn(sn_obj) = index;
1868 snEntry_sn_obj(W_[stable_ptr_table] + index*SIZEOF_snEntry) = sn_obj;
1870 sn_obj = snEntry_sn_obj(W_[stable_ptr_table] + index*SIZEOF_snEntry);
1877 makeStablePtrzh_fast
1881 MAYBE_GC(R1_PTR, makeStablePtrzh_fast);
1882 ("ptr" sp) = foreign "C" getStablePtr(R1 "ptr") [];
1886 deRefStablePtrzh_fast
1888 /* Args: R1 = the stable ptr */
1891 r = snEntry_addr(W_[stable_ptr_table] + sp*SIZEOF_snEntry);
1895 /* -----------------------------------------------------------------------------
1896 Bytecode object primitives
1897 ------------------------------------------------------------------------- */
1907 W_ bco, bitmap_arr, bytes, words;
1911 words = BYTES_TO_WDS(SIZEOF_StgBCO) + StgArrWords_words(bitmap_arr);
1914 ALLOC_PRIM( bytes, R1_PTR&R2_PTR&R3_PTR&R5_PTR, newBCOzh_fast );
1916 bco = Hp - bytes + WDS(1);
1917 SET_HDR(bco, stg_BCO_info, W_[CCCS]);
1919 StgBCO_instrs(bco) = R1;
1920 StgBCO_literals(bco) = R2;
1921 StgBCO_ptrs(bco) = R3;
1922 StgBCO_arity(bco) = HALF_W_(R4);
1923 StgBCO_size(bco) = HALF_W_(words);
1925 // Copy the arity/bitmap info into the BCO
1929 if (i < StgArrWords_words(bitmap_arr)) {
1930 StgBCO_bitmap(bco,i) = StgArrWords_payload(bitmap_arr,i);
1941 // R1 = the BCO# for the AP
1945 // This function is *only* used to wrap zero-arity BCOs in an
1946 // updatable wrapper (see ByteCodeLink.lhs). An AP thunk is always
1947 // saturated and always points directly to a FUN or BCO.
1948 ASSERT(%INFO_TYPE(%GET_STD_INFO(R1)) == HALF_W_(BCO) &&
1949 StgBCO_arity(R1) == HALF_W_(0));
1951 HP_CHK_GEN_TICKY(SIZEOF_StgAP, R1_PTR, mkApUpd0zh_fast);
1952 TICK_ALLOC_UP_THK(0, 0);
1953 CCCS_ALLOC(SIZEOF_StgAP);
1955 ap = Hp - SIZEOF_StgAP + WDS(1);
1956 SET_HDR(ap, stg_AP_info, W_[CCCS]);
1958 StgAP_n_args(ap) = HALF_W_(0);
1964 unpackClosurezh_fast
1966 /* args: R1 = closure to analyze */
1967 // TODO: Consider the absence of ptrs or nonptrs as a special case ?
1969 W_ info, ptrs, nptrs, p, ptrs_arr, nptrs_arr;
1970 info = %GET_STD_INFO(UNTAG(R1));
1972 // Some closures have non-standard layout, so we omit those here.
1974 type = TO_W_(%INFO_TYPE(info));
1975 switch [0 .. N_CLOSURE_TYPES] type {
1976 case THUNK_SELECTOR : {
1981 case THUNK, THUNK_1_0, THUNK_0_1, THUNK_2_0, THUNK_1_1,
1982 THUNK_0_2, THUNK_STATIC, AP, PAP, AP_STACK, BCO : {
1988 ptrs = TO_W_(%INFO_PTRS(info));
1989 nptrs = TO_W_(%INFO_NPTRS(info));
1994 W_ ptrs_arr_sz, nptrs_arr_sz;
1995 nptrs_arr_sz = SIZEOF_StgArrWords + WDS(nptrs);
1996 ptrs_arr_sz = SIZEOF_StgMutArrPtrs + WDS(ptrs);
1998 ALLOC_PRIM (ptrs_arr_sz + nptrs_arr_sz, R1_PTR, unpackClosurezh_fast);
2003 ptrs_arr = Hp - nptrs_arr_sz - ptrs_arr_sz + WDS(1);
2004 nptrs_arr = Hp - nptrs_arr_sz + WDS(1);
2006 SET_HDR(ptrs_arr, stg_MUT_ARR_PTRS_FROZEN_info, W_[CCCS]);
2007 StgMutArrPtrs_ptrs(ptrs_arr) = ptrs;
2011 W_[ptrs_arr + SIZEOF_StgMutArrPtrs + WDS(p)] = StgClosure_payload(clos,p);
2016 SET_HDR(nptrs_arr, stg_ARR_WORDS_info, W_[CCCS]);
2017 StgArrWords_words(nptrs_arr) = nptrs;
2021 W_[BYTE_ARR_CTS(nptrs_arr) + WDS(p)] = StgClosure_payload(clos, p+ptrs);
2025 RET_NPP(info, ptrs_arr, nptrs_arr);
2028 /* -----------------------------------------------------------------------------
2029 Thread I/O blocking primitives
2030 -------------------------------------------------------------------------- */
2032 /* Add a thread to the end of the blocked queue. (C-- version of the C
2033 * macro in Schedule.h).
2035 #define APPEND_TO_BLOCKED_QUEUE(tso) \
2036 ASSERT(StgTSO_link(tso) == END_TSO_QUEUE); \
2037 if (W_[blocked_queue_hd] == END_TSO_QUEUE) { \
2038 W_[blocked_queue_hd] = tso; \
2040 StgTSO_link(W_[blocked_queue_tl]) = tso; \
2042 W_[blocked_queue_tl] = tso;
2048 foreign "C" barf("waitRead# on threaded RTS") never returns;
2051 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
2052 StgTSO_why_blocked(CurrentTSO) = BlockedOnRead::I16;
2053 StgTSO_block_info(CurrentTSO) = R1;
2054 // No locking - we're not going to use this interface in the
2055 // threaded RTS anyway.
2056 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
2057 jump stg_block_noregs;
2065 foreign "C" barf("waitWrite# on threaded RTS") never returns;
2068 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
2069 StgTSO_why_blocked(CurrentTSO) = BlockedOnWrite::I16;
2070 StgTSO_block_info(CurrentTSO) = R1;
2071 // No locking - we're not going to use this interface in the
2072 // threaded RTS anyway.
2073 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
2074 jump stg_block_noregs;
2079 STRING(stg_delayzh_malloc_str, "delayzh_fast")
2082 #ifdef mingw32_HOST_OS
2090 foreign "C" barf("delay# on threaded RTS") never returns;
2093 /* args: R1 (microsecond delay amount) */
2094 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
2095 StgTSO_why_blocked(CurrentTSO) = BlockedOnDelay::I16;
2097 #ifdef mingw32_HOST_OS
2099 /* could probably allocate this on the heap instead */
2100 ("ptr" ares) = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
2101 stg_delayzh_malloc_str);
2102 (reqID) = foreign "C" addDelayRequest(R1);
2103 StgAsyncIOResult_reqID(ares) = reqID;
2104 StgAsyncIOResult_len(ares) = 0;
2105 StgAsyncIOResult_errCode(ares) = 0;
2106 StgTSO_block_info(CurrentTSO) = ares;
2108 /* Having all async-blocked threads reside on the blocked_queue
2109 * simplifies matters, so change the status to OnDoProc put the
2110 * delayed thread on the blocked_queue.
2112 StgTSO_why_blocked(CurrentTSO) = BlockedOnDoProc::I16;
2113 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
2114 jump stg_block_async_void;
2120 (time) = foreign "C" getourtimeofday() [R1];
2121 divisor = TO_W_(RtsFlags_MiscFlags_tickInterval(RtsFlags))*1000;
2122 target = ((R1 + divisor - 1) / divisor) /* divide rounding up */
2123 + time + 1; /* Add 1 as getourtimeofday rounds down */
2124 StgTSO_block_info(CurrentTSO) = target;
2126 /* Insert the new thread in the sleeping queue. */
2128 t = W_[sleeping_queue];
2130 if (t != END_TSO_QUEUE && StgTSO_block_info(t) < target) {
2136 StgTSO_link(CurrentTSO) = t;
2138 W_[sleeping_queue] = CurrentTSO;
2140 StgTSO_link(prev) = CurrentTSO;
2142 jump stg_block_noregs;
2144 #endif /* !THREADED_RTS */
2148 #ifdef mingw32_HOST_OS
2149 STRING(stg_asyncReadzh_malloc_str, "asyncReadzh_fast")
2156 foreign "C" barf("asyncRead# on threaded RTS") never returns;
2159 /* args: R1 = fd, R2 = isSock, R3 = len, R4 = buf */
2160 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
2161 StgTSO_why_blocked(CurrentTSO) = BlockedOnRead::I16;
2163 /* could probably allocate this on the heap instead */
2164 ("ptr" ares) = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
2165 stg_asyncReadzh_malloc_str)
2167 (reqID) = foreign "C" addIORequest(R1, 0/*FALSE*/,R2,R3,R4 "ptr") [];
2168 StgAsyncIOResult_reqID(ares) = reqID;
2169 StgAsyncIOResult_len(ares) = 0;
2170 StgAsyncIOResult_errCode(ares) = 0;
2171 StgTSO_block_info(CurrentTSO) = ares;
2172 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
2173 jump stg_block_async;
2177 STRING(stg_asyncWritezh_malloc_str, "asyncWritezh_fast")
2184 foreign "C" barf("asyncWrite# on threaded RTS") never returns;
2187 /* args: R1 = fd, R2 = isSock, R3 = len, R4 = buf */
2188 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
2189 StgTSO_why_blocked(CurrentTSO) = BlockedOnWrite::I16;
2191 ("ptr" ares) = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
2192 stg_asyncWritezh_malloc_str)
2194 (reqID) = foreign "C" addIORequest(R1, 1/*TRUE*/,R2,R3,R4 "ptr") [];
2196 StgAsyncIOResult_reqID(ares) = reqID;
2197 StgAsyncIOResult_len(ares) = 0;
2198 StgAsyncIOResult_errCode(ares) = 0;
2199 StgTSO_block_info(CurrentTSO) = ares;
2200 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
2201 jump stg_block_async;
2205 STRING(stg_asyncDoProczh_malloc_str, "asyncDoProczh_fast")
2212 foreign "C" barf("asyncDoProc# on threaded RTS") never returns;
2215 /* args: R1 = proc, R2 = param */
2216 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
2217 StgTSO_why_blocked(CurrentTSO) = BlockedOnDoProc::I16;
2219 /* could probably allocate this on the heap instead */
2220 ("ptr" ares) = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
2221 stg_asyncDoProczh_malloc_str)
2223 (reqID) = foreign "C" addDoProcRequest(R1 "ptr",R2 "ptr") [];
2224 StgAsyncIOResult_reqID(ares) = reqID;
2225 StgAsyncIOResult_len(ares) = 0;
2226 StgAsyncIOResult_errCode(ares) = 0;
2227 StgTSO_block_info(CurrentTSO) = ares;
2228 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
2229 jump stg_block_async;
2234 // noDuplicate# tries to ensure that none of the thunks under
2235 // evaluation by the current thread are also under evaluation by
2236 // another thread. It relies on *both* threads doing noDuplicate#;
2237 // the second one will get blocked if they are duplicating some work.
2240 SAVE_THREAD_STATE();
2241 ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
2242 foreign "C" threadPaused (MyCapability() "ptr", CurrentTSO "ptr") [];
2244 if (StgTSO_what_next(CurrentTSO) == ThreadKilled::I16) {
2245 jump stg_threadFinished;
2247 LOAD_THREAD_STATE();
2248 ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
2249 jump %ENTRY_CODE(Sp(0));
2253 getApStackValzh_fast
2255 W_ ap_stack, offset, val, ok;
2257 /* args: R1 = AP_STACK, R2 = offset */
2261 if (%INFO_PTR(ap_stack) == stg_AP_STACK_info) {
2263 val = StgAP_STACK_payload(ap_stack,offset);