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" allocate(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" allocate(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 SET_INFO(R1,stg_MUT_ARR_PTRS_info);
123 // SUBTLETY TO DO WITH THE OLD GEN MUTABLE LIST
125 // A MUT_ARR_PTRS lives on the mutable list, but a MUT_ARR_PTRS_FROZEN
126 // normally doesn't. However, when we freeze a MUT_ARR_PTRS, we leave
127 // it on the mutable list for the GC to remove (removing something from
128 // the mutable list is not easy, because the mut_list is only singly-linked).
130 // So, when we thaw a MUT_ARR_PTRS_FROZEN, we must cope with two cases:
131 // either it is on a mut_list, or it isn't. We adopt the convention that
132 // the mut_link field is NULL if it isn't on a mut_list, and the GC
133 // maintains this invariant.
135 if (StgMutClosure_mut_link(R1) == NULL) {
136 foreign "C" recordMutable(R1 "ptr");
142 /* -----------------------------------------------------------------------------
144 -------------------------------------------------------------------------- */
149 /* Args: R1 = initialisation value */
151 ALLOC_PRIM( SIZEOF_StgMutVar, R1_PTR, newMutVarzh_fast);
153 mv = Hp - SIZEOF_StgMutVar + WDS(1);
154 SET_HDR(mv,stg_MUT_VAR_info,W_[CCCS]);
155 StgMutVar_var(mv) = R1;
160 atomicModifyMutVarzh_fast
163 /* Args: R1 :: MutVar#, R2 :: a -> (a,b) */
165 /* If x is the current contents of the MutVar#, then
166 We want to make the new contents point to
170 and the return value is
174 obviously we can share (f x).
176 z = [stg_ap_2 f x] (max (HS + 2) MIN_UPD_SIZE)
177 y = [stg_sel_0 z] (max (HS + 1) MIN_UPD_SIZE)
178 r = [stg_sel_1 z] (max (HS + 1) MIN_UPD_SIZE)
182 #define THUNK_1_SIZE (SIZEOF_StgHeader + WDS(MIN_UPD_SIZE))
183 #define TICK_ALLOC_THUNK_1() TICK_ALLOC_UP_THK(WDS(1),WDS(MIN_UPD_SIZE-1))
185 #define THUNK_1_SIZE (SIZEOF_StgHeader + WDS(1))
186 #define TICK_ALLOC_THUNK_1() TICK_ALLOC_UP_THK(WDS(1),0)
190 #define THUNK_2_SIZE (SIZEOF_StgHeader + WDS(MIN_UPD_SIZE))
191 #define TICK_ALLOC_THUNK_2() TICK_ALLOC_UP_THK(WDS(2),WDS(MIN_UPD_SIZE-2))
193 #define THUNK_2_SIZE (SIZEOF_StgHeader + WDS(2))
194 #define TICK_ALLOC_THUNK_2() TICK_ALLOC_UP_THK(WDS(2),0)
197 #define SIZE (THUNK_2_SIZE + THUNK_1_SIZE + THUNK_1_SIZE)
199 HP_CHK_GEN_TICKY(SIZE, R1_PTR & R2_PTR, atomicModifyMutVarzh_fast);
201 x = StgMutVar_var(R1);
203 TICK_ALLOC_THUNK_2();
204 CCCS_ALLOC(THUNK_2_SIZE);
205 z = Hp - THUNK_2_SIZE + WDS(1);
206 SET_HDR(z, stg_ap_2_upd_info, W_[CCCS]);
207 LDV_RECORD_CREATE(z);
208 StgClosure_payload(z,0) = R2;
209 StgClosure_payload(z,1) = x;
211 TICK_ALLOC_THUNK_1();
212 CCCS_ALLOC(THUNK_1_SIZE);
213 y = z - THUNK_1_SIZE;
214 SET_HDR(y, stg_sel_0_upd_info, W_[CCCS]);
215 LDV_RECORD_CREATE(y);
216 StgClosure_payload(y,0) = z;
218 StgMutVar_var(R1) = y;
220 TICK_ALLOC_THUNK_1();
221 CCCS_ALLOC(THUNK_1_SIZE);
222 r = y - THUNK_1_SIZE;
223 SET_HDR(r, stg_sel_1_upd_info, W_[CCCS]);
224 LDV_RECORD_CREATE(r);
225 StgClosure_payload(r,0) = z;
230 /* -----------------------------------------------------------------------------
231 Foreign Object Primitives
232 -------------------------------------------------------------------------- */
236 /* R1 = ptr to foreign object,
240 ALLOC_PRIM( SIZEOF_StgForeignObj, NO_PTRS, mkForeignObjzh_fast);
242 result = Hp - SIZEOF_StgForeignObj + WDS(1);
243 SET_HDR(result,stg_FOREIGN_info,W_[CCCS]);
244 StgForeignObj_data(result) = R1;
246 /* returns (# s#, ForeignObj# #) */
250 /* -----------------------------------------------------------------------------
251 Weak Pointer Primitives
252 -------------------------------------------------------------------------- */
254 STRING(stg_weak_msg,"New weak pointer at %p\n")
260 R3 = finalizer (or NULL)
265 R3 = stg_NO_FINALIZER_closure;
268 ALLOC_PRIM( SIZEOF_StgWeak, R1_PTR & R2_PTR & R3_PTR, mkWeakzh_fast );
270 w = Hp - SIZEOF_StgWeak + WDS(1);
271 SET_HDR(w, stg_WEAK_info, W_[CCCS]);
274 StgWeak_value(w) = R2;
275 StgWeak_finalizer(w) = R3;
277 StgWeak_link(w) = W_[weak_ptr_list];
278 W_[weak_ptr_list] = w;
280 IF_DEBUG(weak, foreign "C" fprintf(stderr,stg_weak_msg,w));
295 if (GET_INFO(w) == stg_DEAD_WEAK_info) {
296 RET_NP(0,stg_NO_FINALIZER_closure);
302 // A weak pointer is inherently used, so we do not need to call
303 // LDV_recordDead_FILL_SLOP_DYNAMIC():
304 // LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)w);
305 // or, LDV_recordDead():
306 // LDV_recordDead((StgClosure *)w, sizeofW(StgWeak) - sizeofW(StgProfHeader));
307 // Furthermore, when PROFILING is turned on, dead weak pointers are exactly as
308 // large as weak pointers, so there is no need to fill the slop, either.
309 // See stg_DEAD_WEAK_info in StgMiscClosures.hc.
313 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
315 SET_INFO(w,stg_DEAD_WEAK_info);
316 LDV_RECORD_CREATE(w);
318 f = StgWeak_finalizer(w);
319 StgDeadWeak_link(w) = StgWeak_link(w);
321 /* return the finalizer */
322 if (f == stg_NO_FINALIZER_closure) {
323 RET_NP(0,stg_NO_FINALIZER_closure);
335 if (GET_INFO(w) == stg_WEAK_info) {
337 val = StgWeak_value(w);
345 /* -----------------------------------------------------------------------------
346 Arbitrary-precision Integer operations.
348 There are some assumptions in this code that mp_limb_t == W_. This is
349 the case for all the platforms that GHC supports, currently.
350 -------------------------------------------------------------------------- */
354 /* arguments: R1 = Int# */
356 W_ val, s, p; /* to avoid aliasing */
359 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(1), NO_PTRS, int2Integerzh_fast );
361 p = Hp - SIZEOF_StgArrWords;
362 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
363 StgArrWords_words(p) = 1;
365 /* mpz_set_si is inlined here, makes things simpler */
378 /* returns (# size :: Int#,
387 /* arguments: R1 = Word# */
389 W_ val, s, p; /* to avoid aliasing */
393 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(1), NO_PTRS, word2Integerzh_fast);
395 p = Hp - SIZEOF_StgArrWords;
396 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
397 StgArrWords_words(p) = 1;
406 /* returns (# size :: Int#,
407 data :: ByteArray# #)
414 * 'long long' primops for converting to/from Integers.
417 #ifdef SUPPORT_LONG_LONGS
419 int64ToIntegerzh_fast
421 /* arguments: L1 = Int64# */
424 W_ hi, s, neg, words_needed, p;
429 if ( %ge(val,0x100000000::L_) || %le(val,-0x100000000::L_) ) {
432 // minimum is one word
436 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(words_needed),
437 NO_PTRS, int64ToIntegerzh_fast );
439 p = Hp - SIZEOF_StgArrWords - WDS(words_needed) + WDS(1);
440 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
441 StgArrWords_words(p) = words_needed;
443 if ( %lt(val,0::L_) ) {
448 hi = TO_W_(val >> 32);
450 if ( words_needed == 2 ) {
455 if ( val != 0::L_ ) {
458 } else /* val==0 */ {
466 /* returns (# size :: Int#,
467 data :: ByteArray# #)
472 word64ToIntegerzh_fast
474 /* arguments: L1 = Word64# */
477 W_ hi, s, words_needed, p;
480 if ( val >= 0x100000000::L_ ) {
486 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(words_needed),
487 NO_PTRS, word64ToIntegerzh_fast );
489 p = Hp - SIZEOF_StgArrWords - WDS(words_needed) + WDS(1);
490 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
491 StgArrWords_words(p) = words_needed;
493 hi = TO_W_(val >> 32);
494 if ( val >= 0x100000000::L_ ) {
499 if ( val != 0::L_ ) {
502 } else /* val==0 */ {
507 /* returns (# size :: Int#,
508 data :: ByteArray# #)
514 #endif /* SUPPORT_LONG_LONGS */
516 /* ToDo: this is shockingly inefficient */
520 bits8 [SIZEOF_MP_INT];
525 bits8 [SIZEOF_MP_INT];
530 bits8 [SIZEOF_MP_INT];
535 bits8 [SIZEOF_MP_INT];
538 #define GMP_TAKE2_RET1(name,mp_fun) \
544 /* call doYouWantToGC() */ \
545 MAYBE_GC(R2_PTR & R4_PTR, name); \
552 MP_INT__mp_alloc(mp_tmp1) = W_TO_INT(StgArrWords_words(d1)); \
553 MP_INT__mp_size(mp_tmp1) = (s1); \
554 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(d1); \
555 MP_INT__mp_alloc(mp_tmp2) = W_TO_INT(StgArrWords_words(d2)); \
556 MP_INT__mp_size(mp_tmp2) = (s2); \
557 MP_INT__mp_d(mp_tmp2) = BYTE_ARR_CTS(d2); \
559 foreign "C" mpz_init(result1); \
561 /* Perform the operation */ \
562 foreign "C" mp_fun(result1,mp_tmp1,mp_tmp2); \
564 RET_NP(TO_W_(MP_INT__mp_size(result1)), \
565 MP_INT__mp_d(result1) - SIZEOF_StgArrWords); \
568 #define GMP_TAKE1_RET1(name,mp_fun) \
574 /* call doYouWantToGC() */ \
575 MAYBE_GC(R2_PTR, name); \
580 MP_INT__mp_alloc(mp_tmp1) = W_TO_INT(StgArrWords_words(d1)); \
581 MP_INT__mp_size(mp_tmp1) = (s1); \
582 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(d1); \
584 foreign "C" mpz_init(result1); \
586 /* Perform the operation */ \
587 foreign "C" mp_fun(result1,mp_tmp1); \
589 RET_NP(TO_W_(MP_INT__mp_size(result1)), \
590 MP_INT__mp_d(result1) - SIZEOF_StgArrWords); \
593 #define GMP_TAKE2_RET2(name,mp_fun) \
599 /* call doYouWantToGC() */ \
600 MAYBE_GC(R2_PTR & R4_PTR, name); \
607 MP_INT__mp_alloc(mp_tmp1) = W_TO_INT(StgArrWords_words(d1)); \
608 MP_INT__mp_size(mp_tmp1) = (s1); \
609 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(d1); \
610 MP_INT__mp_alloc(mp_tmp2) = W_TO_INT(StgArrWords_words(d2)); \
611 MP_INT__mp_size(mp_tmp2) = (s2); \
612 MP_INT__mp_d(mp_tmp2) = BYTE_ARR_CTS(d2); \
614 foreign "C" mpz_init(result1); \
615 foreign "C" mpz_init(result2); \
617 /* Perform the operation */ \
618 foreign "C" mp_fun(result1,result2,mp_tmp1,mp_tmp2); \
620 RET_NPNP(TO_W_(MP_INT__mp_size(result1)), \
621 MP_INT__mp_d(result1) - SIZEOF_StgArrWords, \
622 TO_W_(MP_INT__mp_size(result2)), \
623 MP_INT__mp_d(result2) - SIZEOF_StgArrWords); \
626 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add)
627 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub)
628 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul)
629 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd)
630 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q)
631 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r)
632 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact)
633 GMP_TAKE2_RET1(andIntegerzh_fast, mpz_and)
634 GMP_TAKE2_RET1(orIntegerzh_fast, mpz_ior)
635 GMP_TAKE2_RET1(xorIntegerzh_fast, mpz_xor)
636 GMP_TAKE1_RET1(complementIntegerzh_fast, mpz_com)
638 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr)
639 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr)
642 aa: W_; // NB. aa is really an mp_limb_t
647 /* R1 = the first Int#; R2 = the second Int# */
651 r = foreign "C" mpn_gcd_1(aa, 1, R2);
654 /* Result parked in R1, return via info-pointer at TOS */
655 jump %ENTRY_CODE(Sp(0));
661 /* R1 = s1; R2 = d1; R3 = the int */
662 R1 = foreign "C" mpn_gcd_1( BYTE_ARR_CTS(R2) "ptr", R1, R3);
664 /* Result parked in R1, return via info-pointer at TOS */
665 jump %ENTRY_CODE(Sp(0));
671 /* R1 = s1; R2 = d1; R3 = the int */
672 W_ usize, vsize, v_digit, u_digit;
678 // paraphrased from mpz_cmp_si() in the GMP sources
679 if (%gt(v_digit,0)) {
682 if (%lt(v_digit,0)) {
688 if (usize != vsize) {
690 jump %ENTRY_CODE(Sp(0));
695 jump %ENTRY_CODE(Sp(0));
698 u_digit = W_[BYTE_ARR_CTS(R2)];
700 if (u_digit == v_digit) {
702 jump %ENTRY_CODE(Sp(0));
705 if (%gtu(u_digit,v_digit)) { // NB. unsigned: these are mp_limb_t's
711 jump %ENTRY_CODE(Sp(0));
716 /* R1 = s1; R2 = d1; R3 = s2; R4 = d2 */
717 W_ usize, vsize, size, up, vp;
720 // paraphrased from mpz_cmp() in the GMP sources
724 if (usize != vsize) {
726 jump %ENTRY_CODE(Sp(0));
731 jump %ENTRY_CODE(Sp(0));
734 if (%lt(usize,0)) { // NB. not <, which is unsigned
740 up = BYTE_ARR_CTS(R2);
741 vp = BYTE_ARR_CTS(R4);
743 cmp = foreign "C" mpn_cmp(up "ptr", vp "ptr", size);
745 if (cmp == 0 :: CInt) {
747 jump %ENTRY_CODE(Sp(0));
750 if (%lt(cmp,0 :: CInt) == %lt(usize,0)) {
755 /* Result parked in R1, return via info-pointer at TOS */
756 jump %ENTRY_CODE(Sp(0));
768 r = W_[R2 + SIZEOF_StgArrWords];
773 /* Result parked in R1, return via info-pointer at TOS */
775 jump %ENTRY_CODE(Sp(0));
787 r = W_[R2 + SIZEOF_StgArrWords];
792 /* Result parked in R1, return via info-pointer at TOS */
794 jump %ENTRY_CODE(Sp(0));
806 /* arguments: F1 = Float# */
809 ALLOC_PRIM( SIZEOF_StgArrWords + WDS(1), NO_PTRS, decodeFloatzh_fast );
811 /* Be prepared to tell Lennart-coded __decodeFloat
812 where mantissa._mp_d can be put (it does not care about the rest) */
813 p = Hp - SIZEOF_StgArrWords;
814 SET_HDR(p,stg_ARR_WORDS_info,W_[CCCS]);
815 StgArrWords_words(p) = 1;
816 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(p);
818 /* Perform the operation */
819 foreign "C" __decodeFloat(mp_tmp1,exponent,arg);
821 /* returns: (Int# (expn), Int#, ByteArray#) */
822 RET_NNP(W_[exponent], TO_W_(MP_INT__mp_size(mp_tmp1)), p);
825 #define DOUBLE_MANTISSA_SIZE SIZEOF_DOUBLE
826 #define ARR_SIZE (SIZEOF_StgArrWords + DOUBLE_MANTISSA_SIZE)
833 /* arguments: D1 = Double# */
836 ALLOC_PRIM( ARR_SIZE, NO_PTRS, decodeDoublezh_fast );
838 /* Be prepared to tell Lennart-coded __decodeDouble
839 where mantissa.d can be put (it does not care about the rest) */
840 p = Hp - ARR_SIZE + WDS(1);
841 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
842 StgArrWords_words(p) = BYTES_TO_WDS(DOUBLE_MANTISSA_SIZE);
843 MP_INT__mp_d(mp_tmp1) = BYTE_ARR_CTS(p);
845 /* Perform the operation */
846 foreign "C" __decodeDouble(mp_tmp1,exponent,arg);
848 /* returns: (Int# (expn), Int#, ByteArray#) */
849 RET_NNP(W_[exponent], TO_W_(MP_INT__mp_size(mp_tmp1)), p);
852 /* -----------------------------------------------------------------------------
853 * Concurrency primitives
854 * -------------------------------------------------------------------------- */
858 /* args: R1 = closure to spark */
860 MAYBE_GC(R1_PTR, forkzh_fast);
862 // create it right now, return ThreadID in R1
863 "ptr" R1 = foreign "C" createIOThread( RtsFlags_GcFlags_initialStkSize(RtsFlags),
865 foreign "C" scheduleThread(R1 "ptr");
867 // switch at the earliest opportunity
868 CInt[context_switch] = 1 :: CInt;
875 jump stg_yield_noregs;
890 foreign "C" labelThread(R1 "ptr", R2 "ptr");
892 jump %ENTRY_CODE(Sp(0));
895 isCurrentThreadBoundzh_fast
899 r = foreign "C" isThreadBound(CurrentTSO);
903 /* -----------------------------------------------------------------------------
906 * take & putMVar work as follows. Firstly, an important invariant:
908 * If the MVar is full, then the blocking queue contains only
909 * threads blocked on putMVar, and if the MVar is empty then the
910 * blocking queue contains only threads blocked on takeMVar.
913 * MVar empty : then add ourselves to the blocking queue
914 * MVar full : remove the value from the MVar, and
915 * blocking queue empty : return
916 * blocking queue non-empty : perform the first blocked putMVar
917 * from the queue, and wake up the
918 * thread (MVar is now full again)
920 * putMVar is just the dual of the above algorithm.
922 * How do we "perform a putMVar"? Well, we have to fiddle around with
923 * the stack of the thread waiting to do the putMVar. See
924 * stg_block_putmvar and stg_block_takemvar in HeapStackCheck.c for
925 * the stack layout, and the PerformPut and PerformTake macros below.
927 * It is important that a blocked take or put is woken up with the
928 * take/put already performed, because otherwise there would be a
929 * small window of vulnerability where the thread could receive an
930 * exception and never perform its take or put, and we'd end up with a
933 * -------------------------------------------------------------------------- */
937 /* args: R1 = MVar closure */
939 if (GET_INFO(R1) == stg_EMPTY_MVAR_info) {
951 ALLOC_PRIM ( SIZEOF_StgMVar, NO_PTRS, newMVarzh_fast );
953 mvar = Hp - SIZEOF_StgMVar + WDS(1);
954 SET_HDR(mvar,stg_EMPTY_MVAR_info,W_[CCCS]);
955 StgMVar_head(mvar) = stg_END_TSO_QUEUE_closure;
956 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
957 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
962 /* If R1 isn't available, pass it on the stack */
964 #define PerformTake(tso, value) \
965 W_[StgTSO_sp(tso) + WDS(1)] = value; \
966 W_[StgTSO_sp(tso) + WDS(0)] = stg_gc_unpt_r1_info;
968 #define PerformTake(tso, value) \
969 W_[StgTSO_sp(tso) + WDS(1)] = value; \
970 W_[StgTSO_sp(tso) + WDS(0)] = stg_ut_1_0_unreg_info;
973 #define PerformPut(tso,lval) \
974 StgTSO_sp(tso) = StgTSO_sp(tso) + WDS(3); \
975 lval = W_[StgTSO_sp(tso) - WDS(1)];
980 W_ mvar, val, info, tso;
982 /* args: R1 = MVar closure */
985 info = GET_INFO(mvar);
987 /* If the MVar is empty, put ourselves on its blocking queue,
988 * and wait until we're woken up.
990 if (info == stg_EMPTY_MVAR_info) {
991 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
992 StgMVar_head(mvar) = CurrentTSO;
994 StgTSO_link(StgMVar_tail(mvar)) = CurrentTSO;
996 StgTSO_link(CurrentTSO) = stg_END_TSO_QUEUE_closure;
997 StgTSO_why_blocked(CurrentTSO) = BlockedOnMVar::I16;
998 StgTSO_block_info(CurrentTSO) = mvar;
999 StgMVar_tail(mvar) = CurrentTSO;
1001 jump stg_block_takemvar;
1004 /* we got the value... */
1005 val = StgMVar_value(mvar);
1007 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure)
1009 /* There are putMVar(s) waiting...
1010 * wake up the first thread on the queue
1012 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1014 /* actually perform the putMVar for the thread that we just woke up */
1015 tso = StgMVar_head(mvar);
1016 PerformPut(tso,StgMVar_value(mvar));
1018 #if defined(GRAN) || defined(PAR)
1019 /* ToDo: check 2nd arg (mvar) is right */
1020 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar),mvar);
1021 StgMVar_head(mvar) = tso;
1023 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr");
1024 StgMVar_head(mvar) = tso;
1026 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1027 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1033 /* No further putMVars, MVar is now empty */
1035 /* do this last... we might have locked the MVar in the SMP case,
1036 * and writing the info pointer will unlock it.
1038 SET_INFO(mvar,stg_EMPTY_MVAR_info);
1039 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1047 W_ mvar, val, info, tso;
1049 /* args: R1 = MVar closure */
1053 info = GET_INFO(mvar);
1055 if (info == stg_EMPTY_MVAR_info) {
1056 /* HACK: we need a pointer to pass back,
1057 * so we abuse NO_FINALIZER_closure
1059 RET_NP(0, stg_NO_FINALIZER_closure);
1062 /* we got the value... */
1063 val = StgMVar_value(mvar);
1065 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1066 /* There are putMVar(s) waiting...
1067 * wake up the first thread on the queue
1069 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1071 /* actually perform the putMVar for the thread that we just woke up */
1072 tso = StgMVar_head(mvar);
1073 PerformPut(tso,StgMVar_value(mvar));
1075 #if defined(GRAN) || defined(PAR)
1076 /* ToDo: check 2nd arg (mvar) is right */
1077 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr", mvar "ptr");
1078 StgMVar_head(mvar) = tso;
1080 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr");
1081 StgMVar_head(mvar) = tso;
1084 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1085 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1090 /* No further putMVars, MVar is now empty */
1091 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1093 /* do this last... we might have locked the MVar in the SMP case,
1094 * and writing the info pointer will unlock it.
1096 SET_INFO(mvar,stg_EMPTY_MVAR_info);
1107 /* args: R1 = MVar, R2 = value */
1110 info = GET_INFO(mvar);
1112 if (info == stg_FULL_MVAR_info) {
1113 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1114 StgMVar_head(mvar) = CurrentTSO;
1116 StgTSO_link(StgMVar_tail(mvar)) = CurrentTSO;
1118 StgTSO_link(CurrentTSO) = stg_END_TSO_QUEUE_closure;
1119 StgTSO_why_blocked(CurrentTSO) = BlockedOnMVar::I16;
1120 StgTSO_block_info(CurrentTSO) = mvar;
1121 StgMVar_tail(mvar) = CurrentTSO;
1123 jump stg_block_putmvar;
1126 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1127 /* There are takeMVar(s) waiting: wake up the first one
1129 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1131 /* actually perform the takeMVar */
1132 tso = StgMVar_head(mvar);
1133 PerformTake(tso, R2);
1135 #if defined(GRAN) || defined(PAR)
1136 /* ToDo: check 2nd arg (mvar) is right */
1137 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr",mvar "ptr");
1138 StgMVar_head(mvar) = tso;
1140 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr");
1141 StgMVar_head(mvar) = tso;
1144 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1145 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1148 jump %ENTRY_CODE(Sp(0));
1152 /* No further takes, the MVar is now full. */
1153 StgMVar_value(mvar) = R2;
1154 /* unlocks the MVar in the SMP case */
1155 SET_INFO(mvar,stg_FULL_MVAR_info);
1156 jump %ENTRY_CODE(Sp(0));
1159 /* ToDo: yield afterward for better communication performance? */
1167 /* args: R1 = MVar, R2 = value */
1170 info = GET_INFO(mvar);
1172 if (info == stg_FULL_MVAR_info) {
1176 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1177 /* There are takeMVar(s) waiting: wake up the first one
1179 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1181 /* actually perform the takeMVar */
1182 tso = StgMVar_head(mvar);
1183 PerformTake(tso, R2);
1185 #if defined(GRAN) || defined(PAR)
1186 /* ToDo: check 2nd arg (mvar) is right */
1187 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr",mvar "ptr");
1188 StgMVar_head(mvar) = tso;
1190 "ptr" tso = foreign "C" unblockOne(StgMVar_head(mvar) "ptr");
1191 StgMVar_head(mvar) = tso;
1194 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1195 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1198 jump %ENTRY_CODE(Sp(0));
1202 /* No further takes, the MVar is now full. */
1203 StgMVar_value(mvar) = R2;
1204 /* unlocks the MVar in the SMP case */
1205 SET_INFO(mvar,stg_FULL_MVAR_info);
1206 jump %ENTRY_CODE(Sp(0));
1209 /* ToDo: yield afterward for better communication performance? */
1213 /* -----------------------------------------------------------------------------
1214 Stable pointer primitives
1215 ------------------------------------------------------------------------- */
1217 makeStableNamezh_fast
1221 ALLOC_PRIM( SIZEOF_StgStableName, R1_PTR, makeStableNamezh_fast );
1223 index = foreign "C" lookupStableName(R1 "ptr");
1225 /* Is there already a StableName for this heap object?
1226 * stable_ptr_table is an array of snEntry structs.
1228 if ( snEntry_sn_obj(stable_ptr_table + index*SIZEOF_snEntry) == NULL ) {
1229 sn_obj = Hp - SIZEOF_StgStableName + WDS(1);
1230 SET_HDR(sn_obj, stg_STABLE_NAME_info, W_[CCCS]);
1231 StgStableName_sn(sn_obj) = index;
1232 snEntry_sn_obj(stable_ptr_table + index*SIZEOF_snEntry) = sn_obj;
1234 sn_obj = snEntry_sn_obj(stable_ptr_table + index*SIZEOF_snEntry);
1241 makeStablePtrzh_fast
1245 MAYBE_GC(R1_PTR, makeStablePtrzh_fast);
1246 "ptr" sp = foreign "C" getStablePtr(R1 "ptr");
1250 deRefStablePtrzh_fast
1252 /* Args: R1 = the stable ptr */
1255 r = snEntry_addr(stable_ptr_table + sp*SIZEOF_snEntry);
1259 /* -----------------------------------------------------------------------------
1260 Bytecode object primitives
1261 ------------------------------------------------------------------------- */
1272 W_ bco, bitmap_arr, bytes, words;
1275 words = BYTES_TO_WDS(SIZEOF_StgBCO) + StgArrWords_words(bitmap_arr);
1278 ALLOC_PRIM( bytes, R1_PTR&R2_PTR&R3_PTR&R4_PTR&R6_PTR, newBCOzh_fast );
1280 bco = Hp - bytes + WDS(1);
1281 SET_HDR(bco, stg_BCO_info, W_[CCCS]);
1283 StgBCO_instrs(bco) = R1;
1284 StgBCO_literals(bco) = R2;
1285 StgBCO_ptrs(bco) = R3;
1286 StgBCO_itbls(bco) = R4;
1287 StgBCO_arity(bco) = HALF_W_(R5);
1288 StgBCO_size(bco) = HALF_W_(words);
1290 // Copy the arity/bitmap info into the BCO
1294 if (i < StgArrWords_words(bitmap_arr)) {
1295 StgBCO_bitmap(bco,i) = StgArrWords_payload(bitmap_arr,i);
1306 // R1 = the BCO# for the AP
1310 // This function is *only* used to wrap zero-arity BCOs in an
1311 // updatable wrapper (see ByteCodeLink.lhs). An AP thunk is always
1312 // saturated and always points directly to a FUN or BCO.
1313 ASSERT(%INFO_TYPE(%GET_STD_INFO(R1)) == HALF_W_(BCO) &&
1314 StgBCO_arity(R1) == HALF_W_(0));
1316 HP_CHK_GEN_TICKY(SIZEOF_StgAP, R1_PTR, mkApUpd0zh_fast);
1317 TICK_ALLOC_UP_THK(0, 0);
1318 CCCS_ALLOC(SIZEOF_StgAP);
1320 ap = Hp - SIZEOF_StgAP + WDS(1);
1321 SET_HDR(ap, stg_AP_info, W_[CCCS]);
1323 StgAP_n_args(ap) = HALF_W_(0);
1329 /* -----------------------------------------------------------------------------
1330 Thread I/O blocking primitives
1331 -------------------------------------------------------------------------- */
1333 /* Add a thread to the end of the blocked queue. (C-- version of the C
1334 * macro in Schedule.h).
1336 #define APPEND_TO_BLOCKED_QUEUE(tso) \
1337 ASSERT(StgTSO_link(tso) == END_TSO_QUEUE); \
1338 if (W_[blocked_queue_hd] == END_TSO_QUEUE) { \
1339 W_[blocked_queue_hd] = tso; \
1341 StgTSO_link(W_[blocked_queue_tl]) = tso; \
1343 W_[blocked_queue_tl] = tso;
1349 foreign "C" barf("waitRead# on threaded RTS");
1352 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1353 StgTSO_why_blocked(CurrentTSO) = BlockedOnRead::I16;
1354 StgTSO_block_info(CurrentTSO) = R1;
1355 // No locking - we're not going to use this interface in the
1356 // threaded RTS anyway.
1357 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1358 jump stg_block_noregs;
1365 foreign "C" barf("waitWrite# on threaded RTS");
1368 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1369 StgTSO_why_blocked(CurrentTSO) = BlockedOnWrite::I16;
1370 StgTSO_block_info(CurrentTSO) = R1;
1371 // No locking - we're not going to use this interface in the
1372 // threaded RTS anyway.
1373 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1374 jump stg_block_noregs;
1378 STRING(stg_delayzh_malloc_str, "delayzh_fast")
1381 #ifdef mingw32_TARGET_OS
1389 foreign "C" barf("delay# on threaded RTS");
1392 /* args: R1 (microsecond delay amount) */
1393 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1394 StgTSO_why_blocked(CurrentTSO) = BlockedOnDelay::I16;
1396 #ifdef mingw32_TARGET_OS
1398 /* could probably allocate this on the heap instead */
1399 "ptr" ares = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1400 stg_delayzh_malloc_str);
1401 reqID = foreign "C" addDelayRequest(R1);
1402 StgAsyncIOResult_reqID(ares) = reqID;
1403 StgAsyncIOResult_len(ares) = 0;
1404 StgAsyncIOResult_errCode(ares) = 0;
1405 StgTSO_block_info(CurrentTSO) = ares;
1407 /* Having all async-blocked threads reside on the blocked_queue
1408 * simplifies matters, so change the status to OnDoProc put the
1409 * delayed thread on the blocked_queue.
1411 StgTSO_why_blocked(CurrentTSO) = BlockedOnDoProc::I16;
1412 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1413 jump stg_block_async_void;
1418 time = foreign "C" getourtimeofday();
1419 target = (R1 / (TICK_MILLISECS*1000)) + time;
1420 StgTSO_block_info(CurrentTSO) = target;
1422 /* Insert the new thread in the sleeping queue. */
1424 t = W_[sleeping_queue];
1426 if (t != END_TSO_QUEUE && StgTSO_block_info(t) < target) {
1432 StgTSO_link(CurrentTSO) = t;
1434 W_[sleeping_queue] = CurrentTSO;
1436 StgTSO_link(prev) = CurrentTSO;
1438 jump stg_block_noregs;
1443 #ifdef mingw32_TARGET_OS
1444 STRING(stg_asyncReadzh_malloc_str, "asyncReadzh_fast")
1451 foreign "C" barf("asyncRead# on threaded RTS");
1454 /* args: R1 = fd, R2 = isSock, R3 = len, R4 = buf */
1455 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1456 StgTSO_why_blocked(CurrentTSO) = BlockedOnRead::I16;
1458 /* could probably allocate this on the heap instead */
1459 "ptr" ares = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1460 stg_asyncReadzh_malloc_str);
1461 reqID = foreign "C" addIORequest(R1, 0/*FALSE*/,R2,R3,R4 "ptr");
1462 StgAsyncIOResult_reqID(ares) = reqID;
1463 StgAsyncIOResult_len(ares) = 0;
1464 StgAsyncIOResult_errCode(ares) = 0;
1465 StgTSO_block_info(CurrentTSO) = ares;
1466 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1467 jump stg_block_async;
1470 STRING(stg_asyncWritezh_malloc_str, "asyncWritezh_fast")
1477 foreign "C" barf("asyncWrite# on threaded RTS");
1480 /* args: R1 = fd, R2 = isSock, R3 = len, R4 = buf */
1481 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1482 StgTSO_why_blocked(CurrentTSO) = BlockedOnWrite::I16;
1484 "ptr" ares = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1485 stg_asyncWritezh_malloc_str);
1486 reqID = foreign "C" addIORequest(R1, 1/*TRUE*/,R2,R3,R4 "ptr");
1488 StgAsyncIOResult_reqID(ares) = reqID;
1489 StgAsyncIOResult_len(ares) = 0;
1490 StgAsyncIOResult_errCode(ares) = 0;
1491 StgTSO_block_info(CurrentTSO) = ares;
1492 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1493 jump stg_block_async;
1496 STRING(stg_asyncDoProczh_malloc_str, "asyncDoProczh_fast")
1502 /* args: R1 = proc, R2 = param */
1503 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1504 StgTSO_why_blocked(CurrentTSO) = BlockedOnDoProc::I16;
1506 /* could probably allocate this on the heap instead */
1507 "ptr" ares = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1508 stg_asyncDoProczh_malloc_str);
1509 reqID = foreign "C" addDoProcRequest(R1 "ptr",R2 "ptr");
1510 StgAsyncIOResult_reqID(ares) = reqID;
1511 StgAsyncIOResult_len(ares) = 0;
1512 StgAsyncIOResult_errCode(ares) = 0;
1513 StgTSO_block_info(CurrentTSO) = ares;
1514 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1515 jump stg_block_async;
1519 /* -----------------------------------------------------------------------------
1522 classes CCallable and CReturnable don't really exist, but the
1523 compiler insists on generating dictionaries containing references
1524 to GHC_ZcCCallable_static_info etc., so we provide dummy symbols
1525 for these. Some C compilers can't cope with zero-length static arrays,
1526 so we have to make these one element long.
1527 --------------------------------------------------------------------------- */
1530 GHC_ZCCCallable_static_info: W_ 0;
1534 GHC_ZCCReturnable_static_info: W_ 0;