1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.41 2000/02/14 10:56:47 sewardj Exp $
4 * (c) The GHC Team, 1998-1999
6 * Primitive functions / data
8 * ---------------------------------------------------------------------------*/
13 #include "StgStartup.h"
18 #include "BlockAlloc.h" /* tmp */
19 #include "StablePriv.h"
20 #include "HeapStackCheck.h"
25 classes CCallable and CReturnable don't really exist, but the
26 compiler insists on generating dictionaries containing references
27 to GHC_ZcCCallable_static_info etc., so we provide dummy symbols
31 W_ GHC_ZCCCallable_static_info[0];
32 W_ GHC_ZCCReturnable_static_info[0];
35 /* -----------------------------------------------------------------------------
36 Macros for Hand-written primitives.
37 -------------------------------------------------------------------------- */
40 * Horrible macros for returning unboxed tuples.
42 * How an unboxed tuple is returned depends on two factors:
43 * - the number of real registers we have available
44 * - the boxedness of the returned fields.
46 * To return an unboxed tuple from a primitive operation, we have macros
47 * RET_<layout> where <layout> describes the boxedness of each field of the
48 * unboxed tuple: N indicates a non-pointer field, and P indicates a pointer.
50 * We only define the cases actually used, to avoid having too much
51 * garbage in this section. Warning: any bugs in here will be hard to
55 /*------ All Regs available */
57 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
58 # define RET_N(a) RET_P(a)
60 # define RET_PP(a,b) R1.w = (W_)(a); R2.w = (W_)(b); JMP_(ENTRY_CODE(Sp[0]));
61 # define RET_NN(a,b) RET_PP(a,b)
62 # define RET_NP(a,b) RET_PP(a,b)
64 # define RET_PPP(a,b,c) \
65 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); JMP_(ENTRY_CODE(Sp[0]));
66 # define RET_NNP(a,b,c) RET_PPP(a,b,c)
68 # define RET_NNNP(a,b,c,d) \
69 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); R4.w = (W_)d; \
70 JMP_(ENTRY_CODE(Sp[0]));
72 # define RET_NPNP(a,b,c,d) \
73 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); R4.w = (W_)(d); \
74 JMP_(ENTRY_CODE(Sp[0]));
76 # define RET_NNPNNP(a,b,c,d,e,f) \
77 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); \
78 R4.w = (W_)(d); R5.w = (W_)(e); R6.w = (W_)(f); \
79 JMP_(ENTRY_CODE(Sp[0]));
81 #elif defined(REG_R7) || defined(REG_R6) || defined(REG_R5) || \
82 defined(REG_R4) || defined(REG_R3)
83 # error RET_n macros not defined for this setup.
85 /*------ 2 Registers available */
88 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
89 # define RET_N(a) RET_P(a)
91 # define RET_PP(a,b) R1.w = (W_)(a); R2.w = (W_)(b); \
92 JMP_(ENTRY_CODE(Sp[0]));
93 # define RET_NN(a,b) RET_PP(a,b)
94 # define RET_NP(a,b) RET_PP(a,b)
96 # define RET_PPP(a,b,c) \
97 R1.w = (W_)(a); R2.w = (W_)(b); Sp[-1] = (W_)(c); Sp -= 1; \
98 JMP_(ENTRY_CODE(Sp[1]));
99 # define RET_NNP(a,b,c) \
100 R1.w = (W_)(a); R2.w = (W_)(b); Sp[-1] = (W_)(c); Sp -= 1; \
101 JMP_(ENTRY_CODE(Sp[1]));
103 # define RET_NNNP(a,b,c,d) \
106 /* Sp[-3] = ARGTAG(1); */ \
110 JMP_(ENTRY_CODE(Sp[3]));
112 # define RET_NPNP(a,b,c,d) \
115 /* Sp[-3] = ARGTAG(1); */ \
119 JMP_(ENTRY_CODE(Sp[3]));
121 # define RET_NNPNNP(a,b,c,d,e,f) \
125 /* Sp[-5] = ARGTAG(1); */ \
127 /* Sp[-3] = ARGTAG(1); */ \
131 JMP_(ENTRY_CODE(Sp[6]));
133 /*------ 1 Register available */
134 #elif defined(REG_R1)
135 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
136 # define RET_N(a) RET_P(a)
138 # define RET_PP(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 1; \
139 JMP_(ENTRY_CODE(Sp[1]));
140 # define RET_NN(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 2; \
141 JMP_(ENTRY_CODE(Sp[2]));
142 # define RET_NP(a,b) RET_PP(a,b)
144 # define RET_PPP(a,b,c) \
145 R1.w = (W_)(a); Sp[-2] = (W_)(b); Sp[-1] = (W_)(c); Sp -= 2; \
146 JMP_(ENTRY_CODE(Sp[2]));
147 # define RET_NNP(a,b,c) \
148 R1.w = (W_)(a); Sp[-2] = (W_)(b); Sp[-1] = (W_)(c); Sp -= 3; \
149 JMP_(ENTRY_CODE(Sp[3]));
151 # define RET_NNNP(a,b,c,d) \
153 /* Sp[-5] = ARGTAG(1); */ \
155 /* Sp[-3] = ARGTAG(1); */ \
159 JMP_(ENTRY_CODE(Sp[5]));
161 # define RET_NPNP(a,b,c,d) \
164 /* Sp[-3] = ARGTAG(1); */ \
168 JMP_(ENTRY_CODE(Sp[4]));
170 # define RET_NNPNNP(a,b,c,d,e,f) \
174 /* Sp[-3] = ARGTAG(1); */ \
176 /* Sp[-5] = ARGTAG(1); */ \
179 /* Sp[-8] = ARGTAG(1); */ \
181 JMP_(ENTRY_CODE(Sp[8]));
183 #else /* 0 Regs available */
185 #define PUSH_P(o,x) Sp[-o] = (W_)(x)
186 #define PUSH_N(o,x) Sp[1-o] = (W_)(x); Sp[-o] = ARG_TAG(1);
187 #define PUSHED(m) Sp -= (m); JMP_(ENTRY_CODE(Sp[m]));
189 /* Here's how to construct these macros:
191 * N = number of N's in the name;
192 * P = number of P's in the name;
194 * while (nonNull(name)) {
195 * if (nextChar == 'P') {
206 # define RET_P(a) PUSH_P(1,a); PUSHED(1)
207 # define RET_N(a) PUSH_N(2,a); PUSHED(2)
209 # define RET_PP(a,b) PUSH_P(2,a); PUSH_P(1,b); PUSHED(2)
210 # define RET_NN(a,b) PUSH_N(4,a); PUSH_N(2,b); PUSHED(4)
211 # define RET_NP(a,b) PUSH_N(3,a); PUSH_P(1,b); PUSHED(3)
213 # define RET_PPP(a,b,c) PUSH_P(3,a); PUSH_P(2,b); PUSH_P(1,c); PUSHED(3)
214 # define RET_NNP(a,b,c) PUSH_N(5,a); PUSH_N(3,b); PUSH_P(1,c); PUSHED(5)
216 # define RET_NNNP(a,b,c,d) PUSH_N(7,a); PUSH_N(5,b); PUSH_N(3,c); PUSH_P(1,d); PUSHED(7)
217 # define RET_NPNP(a,b,c,d) PUSH_N(6,a); PUSH_P(4,b); PUSH_N(3,c); PUSH_P(1,d); PUSHED(6)
218 # define RET_NNPNNP(a,b,c,d,e,f) PUSH_N(10,a); PUSH_N(8,b); PUSH_P(6,c); PUSH_N(5,d); PUSH_N(3,e); PUSH_P(1,f); PUSHED(10)
222 /*-----------------------------------------------------------------------------
225 Basically just new*Array - the others are all inline macros.
227 The size arg is always passed in R1, and the result returned in R1.
229 The slow entry point is for returning from a heap check, the saved
230 size argument must be re-loaded from the stack.
231 -------------------------------------------------------------------------- */
233 /* for objects that are *less* than the size of a word, make sure we
234 * round up to the nearest word for the size of the array.
237 #define BYTES_TO_STGWORDS(n) ((n) + sizeof(W_) - 1)/sizeof(W_)
239 #define newByteArray(ty,scale) \
240 FN_(new##ty##Arrayzh_fast) \
242 W_ stuff_size, size, n; \
245 MAYBE_GC(NO_PTRS,new##ty##Arrayzh_fast); \
247 stuff_size = BYTES_TO_STGWORDS(n*scale); \
248 size = sizeofW(StgArrWords)+ stuff_size; \
249 p = (StgArrWords *)RET_STGCALL1(P_,allocate,size); \
250 TICK_ALLOC_PRIM(sizeofW(StgArrWords),stuff_size,0); \
251 SET_HDR(p, &ARR_WORDS_info, CCCS); \
252 p->words = stuff_size; \
253 TICK_RET_UNBOXED_TUP(1) \
258 newByteArray(Char, sizeof(C_))
259 newByteArray(Int, sizeof(I_));
260 newByteArray(Word, sizeof(W_));
261 newByteArray(Addr, sizeof(P_));
262 newByteArray(Float, sizeof(StgFloat));
263 newByteArray(Double, sizeof(StgDouble));
264 newByteArray(StablePtr, sizeof(StgStablePtr));
274 MAYBE_GC(R2_PTR,newArrayzh_fast);
276 size = sizeofW(StgMutArrPtrs) + n;
277 arr = (StgMutArrPtrs *)RET_STGCALL1(P_, allocate, size);
278 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
280 SET_HDR(arr,&MUT_ARR_PTRS_info,CCCS);
284 for (p = (P_)arr + sizeofW(StgMutArrPtrs);
285 p < (P_)arr + size; p++) {
289 TICK_RET_UNBOXED_TUP(1);
294 FN_(newMutVarzh_fast)
297 /* Args: R1.p = initialisation value */
300 HP_CHK_GEN_TICKY(sizeofW(StgMutVar), R1_PTR, newMutVarzh_fast,);
301 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1,1, 0); /* hack, dependent on rep. */
302 CCS_ALLOC(CCCS,sizeofW(StgMutVar));
304 mv = (StgMutVar *)(Hp-sizeofW(StgMutVar)+1);
305 SET_HDR(mv,&MUT_VAR_info,CCCS);
308 TICK_RET_UNBOXED_TUP(1);
313 /* -----------------------------------------------------------------------------
314 Foreign Object Primitives
316 -------------------------------------------------------------------------- */
319 FN_(makeForeignObjzh_fast)
321 /* R1.p = ptr to foreign object,
323 StgForeignObj *result;
326 HP_CHK_GEN_TICKY(sizeofW(StgForeignObj), NO_PTRS, makeForeignObjzh_fast,);
327 TICK_ALLOC_PRIM(sizeofW(StgHeader),
328 sizeofW(StgForeignObj)-sizeofW(StgHeader), 0);
329 CCS_ALLOC(CCCS,sizeofW(StgForeignObj)); /* ccs prof */
331 result = (StgForeignObj *) (Hp + 1 - sizeofW(StgForeignObj));
332 SET_HDR(result,&FOREIGN_info,CCCS);
335 /* returns (# s#, ForeignObj# #) */
336 TICK_RET_UNBOXED_TUP(1);
342 /* These two are out-of-line for the benefit of the NCG */
343 FN_(unsafeThawArrayzh_fast)
346 SET_INFO((StgClosure *)R1.cl,&MUT_ARR_PTRS_info);
347 recordMutable((StgMutClosure*)R1.cl);
349 TICK_RET_UNBOXED_TUP(1);
354 /* -----------------------------------------------------------------------------
355 Weak Pointer Primitives
356 -------------------------------------------------------------------------- */
369 HP_CHK_GEN_TICKY(sizeofW(StgWeak), R1_PTR|R2_PTR|R3_PTR, mkWeakzh_fast,);
370 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1, // +1 is for the link field
371 sizeofW(StgWeak)-sizeofW(StgHeader)-1, 0);
372 CCS_ALLOC(CCCS,sizeofW(StgWeak)); /* ccs prof */
374 w = (StgWeak *) (Hp + 1 - sizeofW(StgWeak));
375 SET_HDR(w, &WEAK_info, CCCS);
380 w->finalizer = R3.cl;
382 w->finalizer = &NO_FINALIZER_closure;
385 w->link = weak_ptr_list;
387 IF_DEBUG(weak, fprintf(stderr,"New weak pointer at %p\n",w));
389 TICK_RET_UNBOXED_TUP(1);
394 FN_(finalizzeWeakzh_fast)
401 TICK_RET_UNBOXED_TUP(0);
402 w = (StgDeadWeak *)R1.p;
405 if (w->header.info == &DEAD_WEAK_info) {
406 RET_NP(0,&NO_FINALIZER_closure);
410 w->header.info = &DEAD_WEAK_info;
411 f = ((StgWeak *)w)->finalizer;
412 w->link = ((StgWeak *)w)->link;
414 /* return the finalizer */
415 if (f == &NO_FINALIZER_closure) {
416 RET_NP(0,&NO_FINALIZER_closure);
425 /* -----------------------------------------------------------------------------
426 Arbitrary-precision Integer operations.
427 -------------------------------------------------------------------------- */
429 FN_(int2Integerzh_fast)
431 /* arguments: R1 = Int# */
433 I_ val, s; /* to avoid aliasing */
434 StgArrWords* p; /* address of array result */
438 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, int2Integerzh_fast,);
439 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
440 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
442 p = (StgArrWords *)Hp - 1;
443 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, 1);
445 /* mpz_set_si is inlined here, makes things simpler */
449 } else if (val > 0) {
456 /* returns (# size :: Int#,
460 TICK_RET_UNBOXED_TUP(2);
465 FN_(word2Integerzh_fast)
467 /* arguments: R1 = Word# */
469 W_ val; /* to avoid aliasing */
471 StgArrWords* p; /* address of array result */
475 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, word2Integerzh_fast,)
476 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
477 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
479 p = (StgArrWords *)Hp - 1;
480 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, 1);
489 /* returns (# size :: Int#,
493 TICK_RET_UNBOXED_TUP(2);
498 FN_(addr2Integerzh_fast)
504 MAYBE_GC(NO_PTRS,addr2Integerzh_fast);
506 /* args: R1 :: Addr# */
509 /* Perform the operation */
510 if (RET_STGCALL3(int, mpz_init_set_str,&result,(str),/*base*/10))
513 /* returns (# size :: Int#,
517 TICK_RET_UNBOXED_TUP(2);
518 RET_NP(result._mp_size,
519 result._mp_d - sizeofW(StgArrWords));
524 * 'long long' primops for converting to/from Integers.
527 #ifdef SUPPORT_LONG_LONGS
529 FN_(int64ToIntegerzh_fast)
531 /* arguments: L1 = Int64# */
533 StgInt64 val; /* to avoid aliasing */
535 I_ s, neg, words_needed;
536 StgArrWords* p; /* address of array result */
542 if ( val >= 0x100000000LL || val <= -0x100000000LL ) {
545 /* minimum is one word */
548 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, int64ToIntegerzh_fast,)
549 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
550 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
552 p = (StgArrWords *)(Hp-words_needed+1) - 1;
553 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, words_needed);
560 hi = (W_)((LW_)val / 0x100000000ULL);
562 if ( words_needed == 2 ) {
566 } else if ( val != 0 ) {
569 } else /* val==0 */ {
572 s = ( neg ? -s : s );
574 /* returns (# size :: Int#,
578 TICK_RET_UNBOXED_TUP(2);
583 FN_(word64ToIntegerzh_fast)
585 /* arguments: L1 = Word64# */
587 StgWord64 val; /* to avoid aliasing */
590 StgArrWords* p; /* address of array result */
594 if ( val >= 0x100000000ULL ) {
599 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, word64ToIntegerzh_fast,)
600 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
601 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
603 p = (StgArrWords *)(Hp-words_needed+1) - 1;
604 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, words_needed);
606 hi = (W_)((LW_)val / 0x100000000ULL);
607 if ( val >= 0x100000000ULL ) {
611 } else if ( val != 0 ) {
614 } else /* val==0 */ {
618 /* returns (# size :: Int#,
622 TICK_RET_UNBOXED_TUP(2);
628 #endif /* HAVE_LONG_LONG */
630 /* ToDo: this is shockingly inefficient */
632 #define GMP_TAKE2_RET1(name,mp_fun) \
635 MP_INT arg1, arg2, result; \
641 /* call doYouWantToGC() */ \
642 MAYBE_GC(R2_PTR | R4_PTR, name); \
644 d1 = (StgArrWords *)R2.p; \
646 d2 = (StgArrWords *)R4.p; \
649 arg1._mp_alloc = d1->words; \
650 arg1._mp_size = (s1); \
651 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
652 arg2._mp_alloc = d2->words; \
653 arg2._mp_size = (s2); \
654 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
656 STGCALL1(mpz_init,&result); \
658 /* Perform the operation */ \
659 STGCALL3(mp_fun,&result,&arg1,&arg2); \
661 TICK_RET_UNBOXED_TUP(2); \
662 RET_NP(result._mp_size, \
663 result._mp_d-sizeofW(StgArrWords)); \
667 #define GMP_TAKE2_RET2(name,mp_fun) \
670 MP_INT arg1, arg2, result1, result2; \
676 /* call doYouWantToGC() */ \
677 MAYBE_GC(R2_PTR | R4_PTR, name); \
679 d1 = (StgArrWords *)R2.p; \
681 d2 = (StgArrWords *)R4.p; \
684 arg1._mp_alloc = d1->words; \
685 arg1._mp_size = (s1); \
686 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
687 arg2._mp_alloc = d2->words; \
688 arg2._mp_size = (s2); \
689 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
691 STGCALL1(mpz_init,&result1); \
692 STGCALL1(mpz_init,&result2); \
694 /* Perform the operation */ \
695 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
697 TICK_RET_UNBOXED_TUP(4); \
698 RET_NPNP(result1._mp_size, \
699 result1._mp_d-sizeofW(StgArrWords), \
701 result2._mp_d-sizeofW(StgArrWords)); \
705 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add);
706 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub);
707 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul);
708 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd);
709 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q);
710 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r);
711 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact);
713 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr);
714 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr);
716 #ifndef FLOATS_AS_DOUBLES
717 FN_(decodeFloatzh_fast)
725 /* arguments: F1 = Float# */
728 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, decodeFloatzh_fast,);
729 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
730 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
732 /* Be prepared to tell Lennart-coded __decodeFloat */
733 /* where mantissa._mp_d can be put (it does not care about the rest) */
734 p = (StgArrWords *)Hp - 1;
735 SET_ARR_HDR(p,&ARR_WORDS_info,CCCS,1)
736 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
738 /* Perform the operation */
739 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
741 /* returns: (Int# (expn), Int#, ByteArray#) */
742 TICK_RET_UNBOXED_TUP(3);
743 RET_NNP(exponent,mantissa._mp_size,p);
746 #endif /* !FLOATS_AS_DOUBLES */
748 #define DOUBLE_MANTISSA_SIZE (sizeofW(StgDouble))
749 #define ARR_SIZE (sizeofW(StgArrWords) + DOUBLE_MANTISSA_SIZE)
751 FN_(decodeDoublezh_fast)
758 /* arguments: D1 = Double# */
761 HP_CHK_GEN_TICKY(ARR_SIZE, NO_PTRS, decodeDoublezh_fast,);
762 TICK_ALLOC_PRIM(sizeofW(StgArrWords),DOUBLE_MANTISSA_SIZE,0);
763 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
765 /* Be prepared to tell Lennart-coded __decodeDouble */
766 /* where mantissa.d can be put (it does not care about the rest) */
767 p = (StgArrWords *)(Hp-ARR_SIZE+1);
768 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
769 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
771 /* Perform the operation */
772 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
774 /* returns: (Int# (expn), Int#, ByteArray#) */
775 TICK_RET_UNBOXED_TUP(3);
776 RET_NNP(exponent,mantissa._mp_size,p);
780 /* -----------------------------------------------------------------------------
781 * Concurrency primitives
782 * -------------------------------------------------------------------------- */
787 /* args: R1 = closure to spark */
789 MAYBE_GC(R1_PTR, forkzh_fast);
791 /* create it right now, return ThreadID in R1 */
792 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
793 RtsFlags.GcFlags.initialStkSize, R1.cl);
794 STGCALL1(scheduleThread, R1.t);
796 /* switch at the earliest opportunity */
799 JMP_(ENTRY_CODE(Sp[0]));
806 JMP_(stg_yield_noregs);
817 HP_CHK_GEN_TICKY(sizeofW(StgMVar), NO_PTRS, newMVarzh_fast,);
818 TICK_ALLOC_PRIM(sizeofW(StgMutVar)-1, // consider head,tail,link as admin wds
820 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
822 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
823 SET_HDR(mvar,&EMPTY_MVAR_info,CCCS);
824 mvar->head = mvar->tail = (StgTSO *)&END_TSO_QUEUE_closure;
825 mvar->value = (StgClosure *)&END_TSO_QUEUE_closure;
827 TICK_RET_UNBOXED_TUP(1);
836 const StgInfoTable *info;
839 /* args: R1 = MVar closure */
841 mvar = (StgMVar *)R1.p;
844 info = LOCK_CLOSURE(mvar);
846 info = GET_INFO(mvar);
849 /* If the MVar is empty, put ourselves on its blocking queue,
850 * and wait until we're woken up.
852 if (info == &EMPTY_MVAR_info) {
853 if (mvar->head == (StgTSO *)&END_TSO_QUEUE_closure) {
854 mvar->head = CurrentTSO;
856 mvar->tail->link = CurrentTSO;
858 CurrentTSO->link = (StgTSO *)&END_TSO_QUEUE_closure;
859 CurrentTSO->why_blocked = BlockedOnMVar;
860 CurrentTSO->block_info.closure = (StgClosure *)mvar;
861 mvar->tail = CurrentTSO;
864 /* unlock the MVar */
865 mvar->header.info = &EMPTY_MVAR_info;
867 BLOCK(R1_PTR, takeMVarzh_fast);
871 mvar->value = (StgClosure *)&END_TSO_QUEUE_closure;
873 /* do this last... we might have locked the MVar in the SMP case,
874 * and writing the info pointer will unlock it.
876 SET_INFO(mvar,&EMPTY_MVAR_info);
878 TICK_RET_UNBOXED_TUP(1);
886 const StgInfoTable *info;
889 /* args: R1 = MVar, R2 = value */
891 mvar = (StgMVar *)R1.p;
894 info = LOCK_CLOSURE(mvar);
896 info = GET_INFO(mvar);
899 if (info == &FULL_MVAR_info) {
900 barf("putMVar#: MVar already full");
905 /* wake up the first thread on the queue, it will continue with the
906 * takeMVar operation and mark the MVar empty again.
908 if (mvar->head != (StgTSO *)&END_TSO_QUEUE_closure) {
909 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
911 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
913 // ToDo: check 2nd arg (mvar) is right
914 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
916 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
918 if (mvar->head == (StgTSO *)&END_TSO_QUEUE_closure) {
919 mvar->tail = (StgTSO *)&END_TSO_QUEUE_closure;
923 /* unlocks the MVar in the SMP case */
924 SET_INFO(mvar,&FULL_MVAR_info);
926 /* ToDo: yield here for better communication performance? */
927 JMP_(ENTRY_CODE(Sp[0]));
931 /* -----------------------------------------------------------------------------
932 Stable pointer primitives
933 ------------------------------------------------------------------------- */
935 FN_(makeStableNamezh_fast)
938 StgStableName *sn_obj;
941 HP_CHK_GEN_TICKY(sizeofW(StgStableName), R1_PTR, makeStableNamezh_fast,);
942 TICK_ALLOC_PRIM(sizeofW(StgHeader),
943 sizeofW(StgStableName)-sizeofW(StgHeader), 0);
944 CCS_ALLOC(CCCS,sizeofW(StgStableName)); /* ccs prof */
946 index = RET_STGCALL1(StgWord,lookupStableName,R1.p);
948 /* Is there already a StableName for this heap object? */
949 if (stable_ptr_table[index].sn_obj == NULL) {
950 sn_obj = (StgStableName *) (Hp - sizeofW(StgStableName) + 1);
951 sn_obj->header.info = &STABLE_NAME_info;
953 stable_ptr_table[index].sn_obj = (StgClosure *)sn_obj;
955 (StgClosure *)sn_obj = stable_ptr_table[index].sn_obj;
958 TICK_RET_UNBOXED_TUP(1);
962 /* -----------------------------------------------------------------------------
963 Thread I/O blocking primitives
964 -------------------------------------------------------------------------- */
970 ASSERT(CurrentTSO->why_blocked == NotBlocked);
971 CurrentTSO->why_blocked = BlockedOnRead;
972 CurrentTSO->block_info.fd = R1.i;
973 ACQUIRE_LOCK(&sched_mutex);
974 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
975 RELEASE_LOCK(&sched_mutex);
976 JMP_(stg_block_noregs);
980 FN_(waitWritezh_fast)
984 ASSERT(CurrentTSO->why_blocked == NotBlocked);
985 CurrentTSO->why_blocked = BlockedOnWrite;
986 CurrentTSO->block_info.fd = R1.i;
987 ACQUIRE_LOCK(&sched_mutex);
988 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
989 RELEASE_LOCK(&sched_mutex);
990 JMP_(stg_block_noregs);
998 ASSERT(CurrentTSO->why_blocked == NotBlocked);
999 CurrentTSO->why_blocked = BlockedOnDelay;
1001 ACQUIRE_LOCK(&sched_mutex);
1003 /* Add on ticks_since_select, since these will be subtracted at
1004 * the next awaitEvent call.
1006 CurrentTSO->block_info.delay = R1.i + ticks_since_select;
1008 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1010 RELEASE_LOCK(&sched_mutex);
1011 JMP_(stg_block_noregs);