1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.45 2000/03/13 13:00:00 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"
26 classes CCallable and CReturnable don't really exist, but the
27 compiler insists on generating dictionaries containing references
28 to GHC_ZcCCallable_static_info etc., so we provide dummy symbols
32 W_ GHC_ZCCCallable_static_info[0];
33 W_ GHC_ZCCReturnable_static_info[0];
36 /* -----------------------------------------------------------------------------
37 Macros for Hand-written primitives.
38 -------------------------------------------------------------------------- */
41 * Horrible macros for returning unboxed tuples.
43 * How an unboxed tuple is returned depends on two factors:
44 * - the number of real registers we have available
45 * - the boxedness of the returned fields.
47 * To return an unboxed tuple from a primitive operation, we have macros
48 * RET_<layout> where <layout> describes the boxedness of each field of the
49 * unboxed tuple: N indicates a non-pointer field, and P indicates a pointer.
51 * We only define the cases actually used, to avoid having too much
52 * garbage in this section. Warning: any bugs in here will be hard to
56 /*------ All Regs available */
58 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
59 # define RET_N(a) RET_P(a)
61 # define RET_PP(a,b) R1.w = (W_)(a); R2.w = (W_)(b); JMP_(ENTRY_CODE(Sp[0]));
62 # define RET_NN(a,b) RET_PP(a,b)
63 # define RET_NP(a,b) RET_PP(a,b)
65 # define RET_PPP(a,b,c) \
66 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); JMP_(ENTRY_CODE(Sp[0]));
67 # define RET_NNP(a,b,c) RET_PPP(a,b,c)
69 # define RET_NNNP(a,b,c,d) \
70 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); R4.w = (W_)d; \
71 JMP_(ENTRY_CODE(Sp[0]));
73 # define RET_NPNP(a,b,c,d) \
74 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); R4.w = (W_)(d); \
75 JMP_(ENTRY_CODE(Sp[0]));
77 # define RET_NNPNNP(a,b,c,d,e,f) \
78 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); \
79 R4.w = (W_)(d); R5.w = (W_)(e); R6.w = (W_)(f); \
80 JMP_(ENTRY_CODE(Sp[0]));
82 #elif defined(REG_R7) || defined(REG_R6) || defined(REG_R5) || \
83 defined(REG_R4) || defined(REG_R3)
84 # error RET_n macros not defined for this setup.
86 /*------ 2 Registers available */
89 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
90 # define RET_N(a) RET_P(a)
92 # define RET_PP(a,b) R1.w = (W_)(a); R2.w = (W_)(b); \
93 JMP_(ENTRY_CODE(Sp[0]));
94 # define RET_NN(a,b) RET_PP(a,b)
95 # define RET_NP(a,b) RET_PP(a,b)
97 # define RET_PPP(a,b,c) \
98 R1.w = (W_)(a); R2.w = (W_)(b); Sp[-1] = (W_)(c); Sp -= 1; \
99 JMP_(ENTRY_CODE(Sp[1]));
100 # define RET_NNP(a,b,c) \
101 R1.w = (W_)(a); R2.w = (W_)(b); Sp[-1] = (W_)(c); Sp -= 1; \
102 JMP_(ENTRY_CODE(Sp[1]));
104 # define RET_NNNP(a,b,c,d) \
107 /* Sp[-3] = ARGTAG(1); */ \
111 JMP_(ENTRY_CODE(Sp[3]));
113 # define RET_NPNP(a,b,c,d) \
116 /* Sp[-3] = ARGTAG(1); */ \
120 JMP_(ENTRY_CODE(Sp[3]));
122 # define RET_NNPNNP(a,b,c,d,e,f) \
126 /* Sp[-5] = ARGTAG(1); */ \
128 /* Sp[-3] = ARGTAG(1); */ \
132 JMP_(ENTRY_CODE(Sp[6]));
134 /*------ 1 Register available */
135 #elif defined(REG_R1)
136 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
137 # define RET_N(a) RET_P(a)
139 # define RET_PP(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 1; \
140 JMP_(ENTRY_CODE(Sp[1]));
141 # define RET_NN(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 2; \
142 JMP_(ENTRY_CODE(Sp[2]));
143 # define RET_NP(a,b) RET_PP(a,b)
145 # define RET_PPP(a,b,c) \
146 R1.w = (W_)(a); Sp[-2] = (W_)(b); Sp[-1] = (W_)(c); Sp -= 2; \
147 JMP_(ENTRY_CODE(Sp[2]));
148 # define RET_NNP(a,b,c) \
149 R1.w = (W_)(a); Sp[-2] = (W_)(b); Sp[-1] = (W_)(c); Sp -= 3; \
150 JMP_(ENTRY_CODE(Sp[3]));
152 # define RET_NNNP(a,b,c,d) \
154 /* Sp[-5] = ARGTAG(1); */ \
156 /* Sp[-3] = ARGTAG(1); */ \
160 JMP_(ENTRY_CODE(Sp[5]));
162 # define RET_NPNP(a,b,c,d) \
165 /* Sp[-3] = ARGTAG(1); */ \
169 JMP_(ENTRY_CODE(Sp[4]));
171 # define RET_NNPNNP(a,b,c,d,e,f) \
175 /* Sp[-3] = ARGTAG(1); */ \
177 /* Sp[-5] = ARGTAG(1); */ \
180 /* Sp[-8] = ARGTAG(1); */ \
182 JMP_(ENTRY_CODE(Sp[8]));
184 #else /* 0 Regs available */
186 #define PUSH_P(o,x) Sp[-o] = (W_)(x)
189 #define PUSH_N(o,x) Sp[1-o] = (W_)(x); Sp[-o] = ARG_TAG(1);
191 #define PUSH_N(o,x) Sp[1-o] = (W_)(x);
194 #define PUSHED(m) Sp -= (m); JMP_(ENTRY_CODE(Sp[m]));
196 /* Here's how to construct these macros:
198 * N = number of N's in the name;
199 * P = number of P's in the name;
201 * while (nonNull(name)) {
202 * if (nextChar == 'P') {
213 # define RET_P(a) PUSH_P(1,a); PUSHED(1)
214 # define RET_N(a) PUSH_N(2,a); PUSHED(2)
216 # define RET_PP(a,b) PUSH_P(2,a); PUSH_P(1,b); PUSHED(2)
217 # define RET_NN(a,b) PUSH_N(4,a); PUSH_N(2,b); PUSHED(4)
218 # define RET_NP(a,b) PUSH_N(3,a); PUSH_P(1,b); PUSHED(3)
220 # define RET_PPP(a,b,c) PUSH_P(3,a); PUSH_P(2,b); PUSH_P(1,c); PUSHED(3)
221 # define RET_NNP(a,b,c) PUSH_N(5,a); PUSH_N(3,b); PUSH_P(1,c); PUSHED(5)
223 # 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)
224 # 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)
225 # 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)
229 /*-----------------------------------------------------------------------------
232 Basically just new*Array - the others are all inline macros.
234 The size arg is always passed in R1, and the result returned in R1.
236 The slow entry point is for returning from a heap check, the saved
237 size argument must be re-loaded from the stack.
238 -------------------------------------------------------------------------- */
240 /* for objects that are *less* than the size of a word, make sure we
241 * round up to the nearest word for the size of the array.
244 #define BYTES_TO_STGWORDS(n) ((n) + sizeof(W_) - 1)/sizeof(W_)
246 #define newByteArray(ty,scale) \
247 FN_(new##ty##Arrayzh_fast) \
249 W_ stuff_size, size, n; \
252 MAYBE_GC(NO_PTRS,new##ty##Arrayzh_fast); \
254 stuff_size = BYTES_TO_STGWORDS(n*scale); \
255 size = sizeofW(StgArrWords)+ stuff_size; \
256 p = (StgArrWords *)RET_STGCALL1(P_,allocate,size); \
257 TICK_ALLOC_PRIM(sizeofW(StgArrWords),stuff_size,0); \
258 SET_HDR(p, &ARR_WORDS_info, CCCS); \
259 p->words = stuff_size; \
260 TICK_RET_UNBOXED_TUP(1) \
265 newByteArray(Char, sizeof(C_))
266 newByteArray(Int, sizeof(I_));
267 newByteArray(Word, sizeof(W_));
268 newByteArray(Addr, sizeof(P_));
269 newByteArray(Float, sizeof(StgFloat));
270 newByteArray(Double, sizeof(StgDouble));
271 newByteArray(StablePtr, sizeof(StgStablePtr));
281 MAYBE_GC(R2_PTR,newArrayzh_fast);
283 size = sizeofW(StgMutArrPtrs) + n;
284 arr = (StgMutArrPtrs *)RET_STGCALL1(P_, allocate, size);
285 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
287 SET_HDR(arr,&MUT_ARR_PTRS_info,CCCS);
291 for (p = (P_)arr + sizeofW(StgMutArrPtrs);
292 p < (P_)arr + size; p++) {
296 TICK_RET_UNBOXED_TUP(1);
301 FN_(newMutVarzh_fast)
304 /* Args: R1.p = initialisation value */
307 HP_CHK_GEN_TICKY(sizeofW(StgMutVar), R1_PTR, newMutVarzh_fast,);
308 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1,1, 0); /* hack, dependent on rep. */
309 CCS_ALLOC(CCCS,sizeofW(StgMutVar));
311 mv = (StgMutVar *)(Hp-sizeofW(StgMutVar)+1);
312 SET_HDR(mv,&MUT_VAR_info,CCCS);
315 TICK_RET_UNBOXED_TUP(1);
320 /* -----------------------------------------------------------------------------
321 Foreign Object Primitives
323 -------------------------------------------------------------------------- */
326 FN_(makeForeignObjzh_fast)
328 /* R1.p = ptr to foreign object,
330 StgForeignObj *result;
333 HP_CHK_GEN_TICKY(sizeofW(StgForeignObj), NO_PTRS, makeForeignObjzh_fast,);
334 TICK_ALLOC_PRIM(sizeofW(StgHeader),
335 sizeofW(StgForeignObj)-sizeofW(StgHeader), 0);
336 CCS_ALLOC(CCCS,sizeofW(StgForeignObj)); /* ccs prof */
338 result = (StgForeignObj *) (Hp + 1 - sizeofW(StgForeignObj));
339 SET_HDR(result,&FOREIGN_info,CCCS);
342 /* returns (# s#, ForeignObj# #) */
343 TICK_RET_UNBOXED_TUP(1);
349 /* These two are out-of-line for the benefit of the NCG */
350 FN_(unsafeThawArrayzh_fast)
353 SET_INFO((StgClosure *)R1.cl,&MUT_ARR_PTRS_info);
354 recordMutable((StgMutClosure*)R1.cl);
356 TICK_RET_UNBOXED_TUP(1);
361 /* -----------------------------------------------------------------------------
362 Weak Pointer Primitives
363 -------------------------------------------------------------------------- */
371 R3.p = finalizer (or NULL)
377 R3.cl = &NO_FINALIZER_closure;
380 HP_CHK_GEN_TICKY(sizeofW(StgWeak),R1_PTR|R2_PTR|R3_PTR, mkWeakzh_fast,);
381 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1, // +1 is for the link field
382 sizeofW(StgWeak)-sizeofW(StgHeader)-1, 0);
383 CCS_ALLOC(CCCS,sizeofW(StgWeak)); /* ccs prof */
385 w = (StgWeak *) (Hp + 1 - sizeofW(StgWeak));
386 SET_HDR(w, &WEAK_info, CCCS);
390 w->finalizer = R3.cl;
392 w->link = weak_ptr_list;
394 IF_DEBUG(weak, fprintf(stderr,"New weak pointer at %p\n",w));
396 TICK_RET_UNBOXED_TUP(1);
401 FN_(finalizzeWeakzh_fast)
408 TICK_RET_UNBOXED_TUP(0);
409 w = (StgDeadWeak *)R1.p;
412 if (w->header.info == &DEAD_WEAK_info) {
413 RET_NP(0,&NO_FINALIZER_closure);
417 w->header.info = &DEAD_WEAK_info;
418 f = ((StgWeak *)w)->finalizer;
419 w->link = ((StgWeak *)w)->link;
421 /* return the finalizer */
422 if (f == &NO_FINALIZER_closure) {
423 RET_NP(0,&NO_FINALIZER_closure);
432 /* -----------------------------------------------------------------------------
433 Arbitrary-precision Integer operations.
434 -------------------------------------------------------------------------- */
436 FN_(int2Integerzh_fast)
438 /* arguments: R1 = Int# */
440 I_ val, s; /* to avoid aliasing */
441 StgArrWords* p; /* address of array result */
445 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, int2Integerzh_fast,);
446 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
447 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
449 p = (StgArrWords *)Hp - 1;
450 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, 1);
452 /* mpz_set_si is inlined here, makes things simpler */
456 } else if (val > 0) {
463 /* returns (# size :: Int#,
467 TICK_RET_UNBOXED_TUP(2);
472 FN_(word2Integerzh_fast)
474 /* arguments: R1 = Word# */
476 W_ val; /* to avoid aliasing */
478 StgArrWords* p; /* address of array result */
482 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, word2Integerzh_fast,)
483 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
484 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
486 p = (StgArrWords *)Hp - 1;
487 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, 1);
496 /* returns (# size :: Int#,
500 TICK_RET_UNBOXED_TUP(2);
505 FN_(addr2Integerzh_fast)
511 MAYBE_GC(NO_PTRS,addr2Integerzh_fast);
513 /* args: R1 :: Addr# */
516 /* Perform the operation */
517 if (RET_STGCALL3(int, mpz_init_set_str,&result,(str),/*base*/10))
520 /* returns (# size :: Int#,
524 TICK_RET_UNBOXED_TUP(2);
525 RET_NP(result._mp_size,
526 result._mp_d - sizeofW(StgArrWords));
531 * 'long long' primops for converting to/from Integers.
534 #ifdef SUPPORT_LONG_LONGS
536 FN_(int64ToIntegerzh_fast)
538 /* arguments: L1 = Int64# */
540 StgInt64 val; /* to avoid aliasing */
542 I_ s, neg, words_needed;
543 StgArrWords* p; /* address of array result */
549 if ( val >= 0x100000000LL || val <= -0x100000000LL ) {
552 /* minimum is one word */
555 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, int64ToIntegerzh_fast,)
556 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
557 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
559 p = (StgArrWords *)(Hp-words_needed+1) - 1;
560 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, words_needed);
567 hi = (W_)((LW_)val / 0x100000000ULL);
569 if ( words_needed == 2 ) {
573 } else if ( val != 0 ) {
576 } else /* val==0 */ {
579 s = ( neg ? -s : s );
581 /* returns (# size :: Int#,
585 TICK_RET_UNBOXED_TUP(2);
590 FN_(word64ToIntegerzh_fast)
592 /* arguments: L1 = Word64# */
594 StgWord64 val; /* to avoid aliasing */
597 StgArrWords* p; /* address of array result */
601 if ( val >= 0x100000000ULL ) {
606 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, word64ToIntegerzh_fast,)
607 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
608 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
610 p = (StgArrWords *)(Hp-words_needed+1) - 1;
611 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, words_needed);
613 hi = (W_)((LW_)val / 0x100000000ULL);
614 if ( val >= 0x100000000ULL ) {
618 } else if ( val != 0 ) {
621 } else /* val==0 */ {
625 /* returns (# size :: Int#,
629 TICK_RET_UNBOXED_TUP(2);
635 #endif /* HAVE_LONG_LONG */
637 /* ToDo: this is shockingly inefficient */
639 #define GMP_TAKE2_RET1(name,mp_fun) \
642 MP_INT arg1, arg2, result; \
648 /* call doYouWantToGC() */ \
649 MAYBE_GC(R2_PTR | R4_PTR, name); \
651 d1 = (StgArrWords *)R2.p; \
653 d2 = (StgArrWords *)R4.p; \
656 arg1._mp_alloc = d1->words; \
657 arg1._mp_size = (s1); \
658 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
659 arg2._mp_alloc = d2->words; \
660 arg2._mp_size = (s2); \
661 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
663 STGCALL1(mpz_init,&result); \
665 /* Perform the operation */ \
666 STGCALL3(mp_fun,&result,&arg1,&arg2); \
668 TICK_RET_UNBOXED_TUP(2); \
669 RET_NP(result._mp_size, \
670 result._mp_d-sizeofW(StgArrWords)); \
674 #define GMP_TAKE2_RET2(name,mp_fun) \
677 MP_INT arg1, arg2, result1, result2; \
683 /* call doYouWantToGC() */ \
684 MAYBE_GC(R2_PTR | R4_PTR, name); \
686 d1 = (StgArrWords *)R2.p; \
688 d2 = (StgArrWords *)R4.p; \
691 arg1._mp_alloc = d1->words; \
692 arg1._mp_size = (s1); \
693 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
694 arg2._mp_alloc = d2->words; \
695 arg2._mp_size = (s2); \
696 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
698 STGCALL1(mpz_init,&result1); \
699 STGCALL1(mpz_init,&result2); \
701 /* Perform the operation */ \
702 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
704 TICK_RET_UNBOXED_TUP(4); \
705 RET_NPNP(result1._mp_size, \
706 result1._mp_d-sizeofW(StgArrWords), \
708 result2._mp_d-sizeofW(StgArrWords)); \
712 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add);
713 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub);
714 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul);
715 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd);
716 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q);
717 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r);
718 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact);
720 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr);
721 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr);
723 #ifndef FLOATS_AS_DOUBLES
724 FN_(decodeFloatzh_fast)
732 /* arguments: F1 = Float# */
735 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, decodeFloatzh_fast,);
736 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
737 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
739 /* Be prepared to tell Lennart-coded __decodeFloat */
740 /* where mantissa._mp_d can be put (it does not care about the rest) */
741 p = (StgArrWords *)Hp - 1;
742 SET_ARR_HDR(p,&ARR_WORDS_info,CCCS,1)
743 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
745 /* Perform the operation */
746 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
748 /* returns: (Int# (expn), Int#, ByteArray#) */
749 TICK_RET_UNBOXED_TUP(3);
750 RET_NNP(exponent,mantissa._mp_size,p);
753 #endif /* !FLOATS_AS_DOUBLES */
755 #define DOUBLE_MANTISSA_SIZE (sizeofW(StgDouble))
756 #define ARR_SIZE (sizeofW(StgArrWords) + DOUBLE_MANTISSA_SIZE)
758 FN_(decodeDoublezh_fast)
765 /* arguments: D1 = Double# */
768 HP_CHK_GEN_TICKY(ARR_SIZE, NO_PTRS, decodeDoublezh_fast,);
769 TICK_ALLOC_PRIM(sizeofW(StgArrWords),DOUBLE_MANTISSA_SIZE,0);
770 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
772 /* Be prepared to tell Lennart-coded __decodeDouble */
773 /* where mantissa.d can be put (it does not care about the rest) */
774 p = (StgArrWords *)(Hp-ARR_SIZE+1);
775 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
776 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
778 /* Perform the operation */
779 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
781 /* returns: (Int# (expn), Int#, ByteArray#) */
782 TICK_RET_UNBOXED_TUP(3);
783 RET_NNP(exponent,mantissa._mp_size,p);
787 /* -----------------------------------------------------------------------------
788 * Concurrency primitives
789 * -------------------------------------------------------------------------- */
794 /* args: R1 = closure to spark */
796 MAYBE_GC(R1_PTR, forkzh_fast);
798 /* create it right now, return ThreadID in R1 */
799 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
800 RtsFlags.GcFlags.initialStkSize, R1.cl);
801 STGCALL1(scheduleThread, R1.t);
803 /* switch at the earliest opportunity */
806 JMP_(ENTRY_CODE(Sp[0]));
813 JMP_(stg_yield_noregs);
824 HP_CHK_GEN_TICKY(sizeofW(StgMVar), NO_PTRS, newMVarzh_fast,);
825 TICK_ALLOC_PRIM(sizeofW(StgMutVar)-1, // consider head,tail,link as admin wds
827 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
829 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
830 SET_HDR(mvar,&EMPTY_MVAR_info,CCCS);
831 mvar->head = mvar->tail = (StgTSO *)&END_TSO_QUEUE_closure;
832 mvar->value = (StgClosure *)&END_TSO_QUEUE_closure;
834 TICK_RET_UNBOXED_TUP(1);
843 const StgInfoTable *info;
846 /* args: R1 = MVar closure */
848 mvar = (StgMVar *)R1.p;
851 info = LOCK_CLOSURE(mvar);
853 info = GET_INFO(mvar);
856 /* If the MVar is empty, put ourselves on its blocking queue,
857 * and wait until we're woken up.
859 if (info == &EMPTY_MVAR_info) {
860 if (mvar->head == (StgTSO *)&END_TSO_QUEUE_closure) {
861 mvar->head = CurrentTSO;
863 mvar->tail->link = CurrentTSO;
865 CurrentTSO->link = (StgTSO *)&END_TSO_QUEUE_closure;
866 CurrentTSO->why_blocked = BlockedOnMVar;
867 CurrentTSO->block_info.closure = (StgClosure *)mvar;
868 mvar->tail = CurrentTSO;
871 /* unlock the MVar */
872 mvar->header.info = &EMPTY_MVAR_info;
874 BLOCK(R1_PTR, takeMVarzh_fast);
878 mvar->value = (StgClosure *)&END_TSO_QUEUE_closure;
880 /* do this last... we might have locked the MVar in the SMP case,
881 * and writing the info pointer will unlock it.
883 SET_INFO(mvar,&EMPTY_MVAR_info);
885 TICK_RET_UNBOXED_TUP(1);
893 const StgInfoTable *info;
896 /* args: R1 = MVar, R2 = value */
898 mvar = (StgMVar *)R1.p;
901 info = LOCK_CLOSURE(mvar);
903 info = GET_INFO(mvar);
906 if (info == &FULL_MVAR_info) {
908 fprintf(stderr, "fatal: put on a full MVar in Hugs; aborting\n" );
911 R1.cl = (StgClosure *)&PutFullMVar_closure;
918 /* wake up the first thread on the queue, it will continue with the
919 * takeMVar operation and mark the MVar empty again.
921 if (mvar->head != (StgTSO *)&END_TSO_QUEUE_closure) {
922 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
924 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
926 // ToDo: check 2nd arg (mvar) is right
927 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
929 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
931 if (mvar->head == (StgTSO *)&END_TSO_QUEUE_closure) {
932 mvar->tail = (StgTSO *)&END_TSO_QUEUE_closure;
936 /* unlocks the MVar in the SMP case */
937 SET_INFO(mvar,&FULL_MVAR_info);
939 /* ToDo: yield here for better communication performance? */
940 JMP_(ENTRY_CODE(Sp[0]));
944 /* -----------------------------------------------------------------------------
945 Stable pointer primitives
946 ------------------------------------------------------------------------- */
948 FN_(makeStableNamezh_fast)
951 StgStableName *sn_obj;
954 HP_CHK_GEN_TICKY(sizeofW(StgStableName), R1_PTR, makeStableNamezh_fast,);
955 TICK_ALLOC_PRIM(sizeofW(StgHeader),
956 sizeofW(StgStableName)-sizeofW(StgHeader), 0);
957 CCS_ALLOC(CCCS,sizeofW(StgStableName)); /* ccs prof */
959 index = RET_STGCALL1(StgWord,lookupStableName,R1.p);
961 /* Is there already a StableName for this heap object? */
962 if (stable_ptr_table[index].sn_obj == NULL) {
963 sn_obj = (StgStableName *) (Hp - sizeofW(StgStableName) + 1);
964 sn_obj->header.info = &STABLE_NAME_info;
966 stable_ptr_table[index].sn_obj = (StgClosure *)sn_obj;
968 (StgClosure *)sn_obj = stable_ptr_table[index].sn_obj;
971 TICK_RET_UNBOXED_TUP(1);
975 /* -----------------------------------------------------------------------------
976 Thread I/O blocking primitives
977 -------------------------------------------------------------------------- */
983 ASSERT(CurrentTSO->why_blocked == NotBlocked);
984 CurrentTSO->why_blocked = BlockedOnRead;
985 CurrentTSO->block_info.fd = R1.i;
986 ACQUIRE_LOCK(&sched_mutex);
987 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
988 RELEASE_LOCK(&sched_mutex);
989 JMP_(stg_block_noregs);
993 FN_(waitWritezh_fast)
997 ASSERT(CurrentTSO->why_blocked == NotBlocked);
998 CurrentTSO->why_blocked = BlockedOnWrite;
999 CurrentTSO->block_info.fd = R1.i;
1000 ACQUIRE_LOCK(&sched_mutex);
1001 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1002 RELEASE_LOCK(&sched_mutex);
1003 JMP_(stg_block_noregs);
1011 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1012 CurrentTSO->why_blocked = BlockedOnDelay;
1014 ACQUIRE_LOCK(&sched_mutex);
1016 /* Add on ticks_since_select, since these will be subtracted at
1017 * the next awaitEvent call.
1019 CurrentTSO->block_info.delay = R1.i + ticks_since_select;
1021 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1023 RELEASE_LOCK(&sched_mutex);
1024 JMP_(stg_block_noregs);