1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.34 1999/11/09 15:46:53 simonmar Exp $
4 * (c) The GHC Team, 1998-1999
6 * Primitive functions / data
8 * ---------------------------------------------------------------------------*/
15 #include "StgStartup.h"
20 #include "BlockAlloc.h" /* tmp */
21 #include "StablePriv.h"
22 #include "HeapStackCheck.h"
27 classes CCallable and CReturnable don't really exist, but the
28 compiler insists on generating dictionaries containing references
29 to GHC_ZcCCallable_static_info etc., so we provide dummy symbols
33 W_ GHC_ZCCCallable_static_info[0];
34 W_ GHC_ZCCReturnable_static_info[0];
37 /* -----------------------------------------------------------------------------
38 Macros for Hand-written primitives.
39 -------------------------------------------------------------------------- */
42 * Horrible macros for returning unboxed tuples.
44 * How an unboxed tuple is returned depends on two factors:
45 * - the number of real registers we have available
46 * - the boxedness of the returned fields.
48 * To return an unboxed tuple from a primitive operation, we have macros
49 * RET_<layout> where <layout> describes the boxedness of each field of the
50 * unboxed tuple: N indicates a non-pointer field, and P indicates a pointer.
52 * We only define the cases actually used, to avoid having too much
53 * garbage in this section. Warning: any bugs in here will be hard to
57 /*------ All Regs available */
59 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
60 # define RET_N(a) RET_P(a)
62 # define RET_PP(a,b) R1.w = (W_)(a); R2.w = (W_)(b); JMP_(ENTRY_CODE(Sp[0]));
63 # define RET_NN(a,b) RET_PP(a,b)
64 # define RET_NP(a,b) RET_PP(a,b)
66 # define RET_PPP(a,b,c) \
67 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); JMP_(ENTRY_CODE(Sp[0]));
68 # define RET_NNP(a,b,c) RET_PPP(a,b,c)
70 # define RET_NNNP(a,b,c,d) \
71 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); R4.w = (W_)d; \
72 JMP_(ENTRY_CODE(Sp[0]));
74 # define RET_NPNP(a,b,c,d) \
75 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); R4.w = (W_)(d); \
76 JMP_(ENTRY_CODE(Sp[0]));
78 # define RET_NNPNNP(a,b,c,d,e,f) \
79 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); \
80 R4.w = (W_)(d); R5.w = (W_)(e); R6.w = (W_)(f); \
81 JMP_(ENTRY_CODE(Sp[0]));
83 #elif defined(REG_R7) || defined(REG_R6) || defined(REG_R5) || \
84 defined(REG_R4) || defined(REG_R3)
85 # error RET_n macros not defined for this setup.
87 /*------ 2 Registers available */
90 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
91 # define RET_N(a) RET_P(a)
93 # define RET_PP(a,b) R1.w = (W_)(a); R2.w = (W_)(b); \
94 JMP_(ENTRY_CODE(Sp[0]));
95 # define RET_NN(a,b) RET_PP(a,b)
96 # define RET_NP(a,b) RET_PP(a,b)
98 # define RET_PPP(a,b,c) \
99 R1.w = (W_)(a); R2.w = (W_)(b); Sp[-1] = (W_)(c); Sp -= 1; \
100 JMP_(ENTRY_CODE(Sp[1]));
101 # define RET_NNP(a,b,c) \
102 R1.w = (W_)(a); R2.w = (W_)(b); Sp[-1] = (W_)(c); Sp -= 1; \
103 JMP_(ENTRY_CODE(Sp[1]));
105 # define RET_NNNP(a,b,c,d) \
108 /* Sp[-3] = ARGTAG(1); */ \
112 JMP_(ENTRY_CODE(Sp[3]));
114 # define RET_NPNP(a,b,c,d) \
117 /* Sp[-3] = ARGTAG(1); */ \
121 JMP_(ENTRY_CODE(Sp[3]));
123 # define RET_NNPNNP(a,b,c,d,e,f) \
127 /* Sp[-5] = ARGTAG(1); */ \
129 /* Sp[-3] = ARGTAG(1); */ \
133 JMP_(ENTRY_CODE(Sp[6]));
135 /*------ 1 Register available */
136 #elif defined(REG_R1)
137 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
138 # define RET_N(a) RET_P(a)
140 # define RET_PP(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 1; \
141 JMP_(ENTRY_CODE(Sp[1]));
142 # define RET_NN(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 2; \
143 JMP_(ENTRY_CODE(Sp[2]));
144 # define RET_NP(a,b) RET_PP(a,b)
146 # define RET_PPP(a,b,c) \
147 R1.w = (W_)(a); Sp[-2] = (W_)(b); Sp[-1] = (W_)(c); Sp -= 2; \
148 JMP_(ENTRY_CODE(Sp[2]));
149 # define RET_NNP(a,b,c) \
150 R1.w = (W_)(a); Sp[-2] = (W_)(b); Sp[-1] = (W_)(c); Sp -= 3; \
151 JMP_(ENTRY_CODE(Sp[3]));
153 # define RET_NNNP(a,b,c,d) \
155 /* Sp[-5] = ARGTAG(1); */ \
157 /* Sp[-3] = ARGTAG(1); */ \
161 JMP_(ENTRY_CODE(Sp[5]));
163 # define RET_NPNP(a,b,c,d) \
166 /* Sp[-3] = ARGTAG(1); */ \
170 JMP_(ENTRY_CODE(Sp[4]));
172 # define RET_NNPNNP(a,b,c,d,e,f) \
176 /* Sp[-3] = ARGTAG(1); */ \
178 /* Sp[-5] = ARGTAG(1); */ \
181 /* Sp[-8] = ARGTAG(1); */ \
183 JMP_(ENTRY_CODE(Sp[8]));
185 #else /* 0 Regs available */
187 #define PUSH_P(o,x) Sp[-o] = (W_)(x)
188 #define PUSH_N(o,x) Sp[1-o] = (W_)(x); /* Sp[-o] = ARGTAG(1) */
189 #define PUSHED(m) Sp -= (m); JMP_(ENTRY_CODE(Sp[m]));
191 /* Here's how to construct these macros:
193 * N = number of N's in the name;
194 * P = number of P's in the name;
196 * while (nonNull(name)) {
197 * if (nextChar == 'P') {
208 # define RET_P(a) PUSH_P(1,a); PUSHED(1)
209 # define RET_N(a) PUSH_N(2,a); PUSHED(2)
211 # define RET_PP(a,b) PUSH_P(2,a); PUSH_P(1,b); PUSHED(2)
212 # define RET_NN(a,b) PUSH_N(4,a); PUSH_N(2,b); PUSHED(4)
213 # define RET_NP(a,b) PUSH_N(3,a); PUSH_P(1,b); PUSHED(3)
215 # define RET_PPP(a,b,c) PUSH_P(3,a); PUSH_P(2,b); PUSH_P(1,c); PUSHED(3)
216 # define RET_NNP(a,b,c) PUSH_N(5,a); PUSH_N(3,b); PUSH_P(1,c); PUSHED(5)
218 # 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)
219 # 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)
220 # 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)
224 /*-----------------------------------------------------------------------------
227 Basically just new*Array - the others are all inline macros.
229 The size arg is always passed in R1, and the result returned in R1.
231 The slow entry point is for returning from a heap check, the saved
232 size argument must be re-loaded from the stack.
233 -------------------------------------------------------------------------- */
235 /* for objects that are *less* than the size of a word, make sure we
236 * round up to the nearest word for the size of the array.
239 #define BYTES_TO_STGWORDS(n) ((n) + sizeof(W_) - 1)/sizeof(W_)
241 #define newByteArray(ty,scale) \
242 FN_(new##ty##Arrayzh_fast) \
244 W_ stuff_size, size, n; \
247 MAYBE_GC(NO_PTRS,new##ty##Arrayzh_fast); \
249 stuff_size = BYTES_TO_STGWORDS(n*scale); \
250 size = sizeofW(StgArrWords)+ stuff_size; \
251 p = (StgArrWords *)RET_STGCALL1(P_,allocate,size); \
252 TICK_ALLOC_PRIM(sizeofW(StgArrWords),stuff_size,0); \
253 SET_HDR(p, &ARR_WORDS_info, CCCS); \
254 p->words = stuff_size; \
255 TICK_RET_UNBOXED_TUP(1) \
260 newByteArray(Char, sizeof(C_))
261 newByteArray(Int, sizeof(I_));
262 newByteArray(Word, sizeof(W_));
263 newByteArray(Addr, sizeof(P_));
264 newByteArray(Float, sizeof(StgFloat));
265 newByteArray(Double, sizeof(StgDouble));
266 newByteArray(StablePtr, sizeof(StgStablePtr));
276 MAYBE_GC(R2_PTR,newArrayzh_fast);
278 size = sizeofW(StgMutArrPtrs) + n;
279 arr = (StgMutArrPtrs *)RET_STGCALL1(P_, allocate, size);
280 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
282 SET_HDR(arr,&MUT_ARR_PTRS_info,CCCS);
286 for (p = (P_)arr + sizeofW(StgMutArrPtrs);
287 p < (P_)arr + size; p++) {
291 TICK_RET_UNBOXED_TUP(1);
296 FN_(newMutVarzh_fast)
299 /* Args: R1.p = initialisation value */
302 HP_CHK_GEN_TICKY(sizeofW(StgMutVar), R1_PTR, newMutVarzh_fast,);
303 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1,1, 0); /* hack, dependent on rep. */
304 CCS_ALLOC(CCCS,sizeofW(StgMutVar));
306 mv = (StgMutVar *)(Hp-sizeofW(StgMutVar)+1);
307 SET_HDR(mv,&MUT_VAR_info,CCCS);
310 TICK_RET_UNBOXED_TUP(1);
315 /* -----------------------------------------------------------------------------
316 Foreign Object Primitives
318 -------------------------------------------------------------------------- */
321 FN_(makeForeignObjzh_fast)
323 /* R1.p = ptr to foreign object,
325 StgForeignObj *result;
328 HP_CHK_GEN_TICKY(sizeofW(StgForeignObj), NO_PTRS, makeForeignObjzh_fast,);
329 TICK_ALLOC_PRIM(sizeofW(StgHeader),
330 sizeofW(StgForeignObj)-sizeofW(StgHeader), 0);
331 CCS_ALLOC(CCCS,sizeofW(StgForeignObj)); /* ccs prof */
333 result = (StgForeignObj *) (Hp + 1 - sizeofW(StgForeignObj));
334 SET_HDR(result,&FOREIGN_info,CCCS);
337 /* returns (# s#, ForeignObj# #) */
338 TICK_RET_UNBOXED_TUP(1);
344 /* These two are out-of-line for the benefit of the NCG */
345 FN_(unsafeThawArrayzh_fast)
348 SET_INFO((StgClosure *)R1.cl,&MUT_ARR_PTRS_info);
349 recordMutable((StgMutClosure*)R1.cl);
351 TICK_RET_UNBOXED_TUP(1);
356 /* -----------------------------------------------------------------------------
357 Weak Pointer Primitives
358 -------------------------------------------------------------------------- */
371 HP_CHK_GEN_TICKY(sizeofW(StgWeak), R1_PTR|R2_PTR|R3_PTR, mkWeakzh_fast,);
372 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1, // +1 is for the link field
373 sizeofW(StgWeak)-sizeofW(StgHeader)-1, 0);
374 CCS_ALLOC(CCCS,sizeofW(StgWeak)); /* ccs prof */
376 w = (StgWeak *) (Hp + 1 - sizeofW(StgWeak));
377 SET_HDR(w, &WEAK_info, CCCS);
382 w->finalizer = R3.cl;
384 w->finalizer = &NO_FINALIZER_closure;
387 w->link = weak_ptr_list;
389 IF_DEBUG(weak, fprintf(stderr,"New weak pointer at %p\n",w));
391 TICK_RET_UNBOXED_TUP(1);
396 FN_(finalizzeWeakzh_fast)
403 TICK_RET_UNBOXED_TUP(0);
404 w = (StgDeadWeak *)R1.p;
407 if (w->header.info == &DEAD_WEAK_info) {
408 RET_NP(0,&NO_FINALIZER_closure);
412 w->header.info = &DEAD_WEAK_info;
413 f = ((StgWeak *)w)->finalizer;
414 w->link = ((StgWeak *)w)->link;
416 /* return the finalizer */
417 if (f == &NO_FINALIZER_closure) {
418 RET_NP(0,&NO_FINALIZER_closure);
427 /* -----------------------------------------------------------------------------
428 Arbitrary-precision Integer operations.
429 -------------------------------------------------------------------------- */
431 FN_(int2Integerzh_fast)
433 /* arguments: R1 = Int# */
435 I_ val, s; /* to avoid aliasing */
436 StgArrWords* p; /* address of array result */
440 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, int2Integerzh_fast,);
441 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
442 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
444 p = (StgArrWords *)Hp - 1;
445 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, 1);
447 /* mpz_set_si is inlined here, makes things simpler */
451 } else if (val > 0) {
458 /* returns (# size :: Int#,
462 TICK_RET_UNBOXED_TUP(2);
467 FN_(word2Integerzh_fast)
469 /* arguments: R1 = Word# */
471 W_ val; /* to avoid aliasing */
473 StgArrWords* p; /* address of array result */
477 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, word2Integerzh_fast,)
478 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
479 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
481 p = (StgArrWords *)Hp - 1;
482 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, 1);
491 /* returns (# size :: Int#,
495 TICK_RET_UNBOXED_TUP(2);
500 FN_(addr2Integerzh_fast)
506 MAYBE_GC(NO_PTRS,addr2Integerzh_fast);
508 /* args: R1 :: Addr# */
511 /* Perform the operation */
512 if (RET_STGCALL3(int, mpz_init_set_str,&result,(str),/*base*/10))
515 /* returns (# size :: Int#,
519 TICK_RET_UNBOXED_TUP(2);
520 RET_NP(result._mp_size,
521 result._mp_d - sizeofW(StgArrWords));
526 * 'long long' primops for converting to/from Integers.
529 #ifdef SUPPORT_LONG_LONGS
531 FN_(int64ToIntegerzh_fast)
533 /* arguments: L1 = Int64# */
535 StgInt64 val; /* to avoid aliasing */
537 I_ s, neg, words_needed;
538 StgArrWords* p; /* address of array result */
544 if ( val >= 0x100000000LL || val <= -0x100000000LL ) {
547 /* minimum is one word */
550 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, int64ToIntegerzh_fast,)
551 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
552 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
554 p = (StgArrWords *)(Hp-words_needed+1) - 1;
555 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, words_needed);
562 hi = (W_)((LW_)val / 0x100000000ULL);
564 if ( words_needed == 2 ) {
568 } else if ( val != 0 ) {
571 } else /* val==0 */ {
574 s = ( neg ? -s : s );
576 /* returns (# size :: Int#,
580 TICK_RET_UNBOXED_TUP(2);
585 FN_(word64ToIntegerzh_fast)
587 /* arguments: L1 = Word64# */
589 StgWord64 val; /* to avoid aliasing */
592 StgArrWords* p; /* address of array result */
596 if ( val >= 0x100000000ULL ) {
601 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, word64ToIntegerzh_fast,)
602 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
603 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
605 p = (StgArrWords *)(Hp-words_needed+1) - 1;
606 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, words_needed);
608 hi = (W_)((LW_)val / 0x100000000ULL);
609 if ( val >= 0x100000000ULL ) {
613 } else if ( val != 0 ) {
616 } else /* val==0 */ {
620 /* returns (# size :: Int#,
624 TICK_RET_UNBOXED_TUP(2);
630 #endif /* HAVE_LONG_LONG */
632 /* ToDo: this is shockingly inefficient */
634 #define GMP_TAKE2_RET1(name,mp_fun) \
637 MP_INT arg1, arg2, result; \
643 /* call doYouWantToGC() */ \
644 MAYBE_GC(R2_PTR | R4_PTR, name); \
646 d1 = (StgArrWords *)R2.p; \
648 d2 = (StgArrWords *)R4.p; \
651 arg1._mp_alloc = d1->words; \
652 arg1._mp_size = (s1); \
653 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
654 arg2._mp_alloc = d2->words; \
655 arg2._mp_size = (s2); \
656 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
658 STGCALL1(mpz_init,&result); \
660 /* Perform the operation */ \
661 STGCALL3(mp_fun,&result,&arg1,&arg2); \
663 TICK_RET_UNBOXED_TUP(2); \
664 RET_NP(result._mp_size, \
665 result._mp_d-sizeofW(StgArrWords)); \
669 #define GMP_TAKE2_RET2(name,mp_fun) \
672 MP_INT arg1, arg2, result1, result2; \
678 /* call doYouWantToGC() */ \
679 MAYBE_GC(R2_PTR | R4_PTR, name); \
681 d1 = (StgArrWords *)R2.p; \
683 d2 = (StgArrWords *)R4.p; \
686 arg1._mp_alloc = d1->words; \
687 arg1._mp_size = (s1); \
688 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
689 arg2._mp_alloc = d2->words; \
690 arg2._mp_size = (s2); \
691 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
693 STGCALL1(mpz_init,&result1); \
694 STGCALL1(mpz_init,&result2); \
696 /* Perform the operation */ \
697 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
699 TICK_RET_UNBOXED_TUP(4); \
700 RET_NPNP(result1._mp_size, \
701 result1._mp_d-sizeofW(StgArrWords), \
703 result2._mp_d-sizeofW(StgArrWords)); \
707 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add);
708 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub);
709 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul);
710 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd);
712 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr);
713 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr);
715 #ifndef FLOATS_AS_DOUBLES
716 FN_(decodeFloatzh_fast)
724 /* arguments: F1 = Float# */
727 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, decodeFloatzh_fast,);
728 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
729 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
731 /* Be prepared to tell Lennart-coded __decodeFloat */
732 /* where mantissa._mp_d can be put (it does not care about the rest) */
733 p = (StgArrWords *)Hp - 1;
734 SET_ARR_HDR(p,&ARR_WORDS_info,CCCS,1)
735 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
737 /* Perform the operation */
738 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
740 /* returns: (Int# (expn), Int#, ByteArray#) */
741 TICK_RET_UNBOXED_TUP(3);
742 RET_NNP(exponent,mantissa._mp_size,p);
745 #endif /* !FLOATS_AS_DOUBLES */
747 #define DOUBLE_MANTISSA_SIZE (sizeofW(StgDouble))
748 #define ARR_SIZE (sizeofW(StgArrWords) + DOUBLE_MANTISSA_SIZE)
750 FN_(decodeDoublezh_fast)
757 /* arguments: D1 = Double# */
760 HP_CHK_GEN_TICKY(ARR_SIZE, NO_PTRS, decodeDoublezh_fast,);
761 TICK_ALLOC_PRIM(sizeofW(StgArrWords),DOUBLE_MANTISSA_SIZE,0);
762 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
764 /* Be prepared to tell Lennart-coded __decodeDouble */
765 /* where mantissa.d can be put (it does not care about the rest) */
766 p = (StgArrWords *)(Hp-ARR_SIZE+1);
767 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
768 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
770 /* Perform the operation */
771 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
773 /* returns: (Int# (expn), Int#, ByteArray#) */
774 TICK_RET_UNBOXED_TUP(3);
775 RET_NNP(exponent,mantissa._mp_size,p);
779 /* -----------------------------------------------------------------------------
780 * Concurrency primitives
781 * -------------------------------------------------------------------------- */
786 /* args: R1 = closure to spark */
788 MAYBE_GC(R1_PTR, forkzh_fast);
790 /* create it right now, return ThreadID in R1 */
791 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
792 RtsFlags.GcFlags.initialStkSize, R1.cl);
793 STGCALL1(scheduleThread, R1.t);
795 /* switch at the earliest opportunity */
798 JMP_(ENTRY_CODE(Sp[0]));
805 JMP_(stg_yield_noregs);
809 FN_(killThreadzh_fast)
812 /* args: R1.p = TSO to kill, R2.p = Exception */
814 /* The thread is dead, but the TSO sticks around for a while. That's why
815 * we don't have to explicitly remove it from any queues it might be on.
818 /* We might have killed ourselves. In which case, better be *very*
819 * careful. If the exception killed us, then return to the scheduler.
820 * If the exception went to a catch frame, we'll just continue from
823 if (R1.t == CurrentTSO) {
824 SaveThreadState(); /* inline! */
825 STGCALL2(raiseAsync, R1.t, R2.cl);
826 if (CurrentTSO->whatNext == ThreadKilled) {
827 R1.w = ThreadYielding;
831 if (CurrentTSO->whatNext == ThreadEnterGHC) {
834 JMP_(GET_ENTRY(R1.cl));
836 barf("killThreadzh_fast");
839 STGCALL2(raiseAsync, R1.t, R2.cl);
842 JMP_(ENTRY_CODE(Sp[0]));
853 HP_CHK_GEN_TICKY(sizeofW(StgMVar), NO_PTRS, newMVarzh_fast,);
854 TICK_ALLOC_PRIM(sizeofW(StgMutVar)-1, // consider head,tail,link as admin wds
856 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
858 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
859 SET_HDR(mvar,&EMPTY_MVAR_info,CCCS);
860 mvar->head = mvar->tail = (StgTSO *)&END_TSO_QUEUE_closure;
861 mvar->value = (StgClosure *)&END_TSO_QUEUE_closure;
863 TICK_RET_UNBOXED_TUP(1);
872 const StgInfoTable *info;
875 /* args: R1 = MVar closure */
877 mvar = (StgMVar *)R1.p;
880 info = LOCK_CLOSURE(mvar);
882 info = GET_INFO(mvar);
885 /* If the MVar is empty, put ourselves on its blocking queue,
886 * and wait until we're woken up.
888 if (info == &EMPTY_MVAR_info) {
889 if (mvar->head == (StgTSO *)&END_TSO_QUEUE_closure) {
890 mvar->head = CurrentTSO;
892 mvar->tail->link = CurrentTSO;
894 CurrentTSO->link = (StgTSO *)&END_TSO_QUEUE_closure;
895 CurrentTSO->why_blocked = BlockedOnMVar;
896 CurrentTSO->block_info.closure = (StgClosure *)mvar;
897 mvar->tail = CurrentTSO;
900 /* unlock the MVar */
901 mvar->header.info = &EMPTY_MVAR_info;
903 BLOCK(R1_PTR, takeMVarzh_fast);
907 mvar->value = (StgClosure *)&END_TSO_QUEUE_closure;
909 /* do this last... we might have locked the MVar in the SMP case,
910 * and writing the info pointer will unlock it.
912 SET_INFO(mvar,&EMPTY_MVAR_info);
914 TICK_RET_UNBOXED_TUP(1);
922 const StgInfoTable *info;
925 /* args: R1 = MVar, R2 = value */
927 mvar = (StgMVar *)R1.p;
930 info = LOCK_CLOSURE(mvar);
932 info = GET_INFO(mvar);
935 if (info == &FULL_MVAR_info) {
936 fprintf(stderr, "putMVar#: MVar already full.\n");
937 stg_exit(EXIT_FAILURE);
942 /* wake up the first thread on the queue, it will continue with the
943 * takeMVar operation and mark the MVar empty again.
945 if (mvar->head != (StgTSO *)&END_TSO_QUEUE_closure) {
946 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
947 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
948 if (mvar->head == (StgTSO *)&END_TSO_QUEUE_closure) {
949 mvar->tail = (StgTSO *)&END_TSO_QUEUE_closure;
953 /* unlocks the MVar in the SMP case */
954 SET_INFO(mvar,&FULL_MVAR_info);
956 /* ToDo: yield here for better communication performance? */
957 JMP_(ENTRY_CODE(Sp[0]));
961 /* -----------------------------------------------------------------------------
962 Stable pointer primitives
963 ------------------------------------------------------------------------- */
965 FN_(makeStableNamezh_fast)
968 StgStableName *sn_obj;
971 HP_CHK_GEN_TICKY(sizeofW(StgStableName), R1_PTR, makeStableNamezh_fast,);
972 TICK_ALLOC_PRIM(sizeofW(StgHeader),
973 sizeofW(StgStableName)-sizeofW(StgHeader), 0);
974 CCS_ALLOC(CCCS,sizeofW(StgStableName)); /* ccs prof */
976 index = RET_STGCALL1(StgWord,lookupStableName,R1.p);
978 /* Is there already a StableName for this heap object? */
979 if (stable_ptr_table[index].sn_obj == NULL) {
980 sn_obj = (StgStableName *) (Hp - sizeofW(StgStableName) + 1);
981 sn_obj->header.info = &STABLE_NAME_info;
983 stable_ptr_table[index].sn_obj = (StgClosure *)sn_obj;
985 (StgClosure *)sn_obj = stable_ptr_table[index].sn_obj;
988 TICK_RET_UNBOXED_TUP(1);
992 /* -----------------------------------------------------------------------------
993 Thread I/O blocking primitives
994 -------------------------------------------------------------------------- */
1000 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1001 CurrentTSO->why_blocked = BlockedOnRead;
1002 CurrentTSO->block_info.fd = R1.i;
1003 ACQUIRE_LOCK(&sched_mutex);
1004 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1005 RELEASE_LOCK(&sched_mutex);
1006 JMP_(stg_block_noregs);
1010 FN_(waitWritezh_fast)
1014 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1015 CurrentTSO->why_blocked = BlockedOnWrite;
1016 CurrentTSO->block_info.fd = R1.i;
1017 ACQUIRE_LOCK(&sched_mutex);
1018 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1019 RELEASE_LOCK(&sched_mutex);
1020 JMP_(stg_block_noregs);
1028 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1029 CurrentTSO->why_blocked = BlockedOnDelay;
1031 ACQUIRE_LOCK(&sched_mutex);
1033 /* Add on ticks_since_select, since these will be subtracted at
1034 * the next awaitEvent call.
1036 CurrentTSO->block_info.delay = R1.i + ticks_since_select;
1038 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1040 RELEASE_LOCK(&sched_mutex);
1041 JMP_(stg_block_noregs);
1045 #endif /* COMPILER */