1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.54 2000/08/25 13:12:07 simonmar Exp $
4 * (c) The GHC Team, 1998-2000
6 * Primitive functions / data
8 * ---------------------------------------------------------------------------*/
13 #include "StgStartup.h"
18 #include "BlockAlloc.h" /* tmp */
19 #include "StablePriv.h"
20 #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)
190 #define PUSH_N(o,x) Sp[1-o] = (W_)(x); Sp[-o] = ARG_TAG(1);
192 #define PUSH_N(o,x) Sp[1-o] = (W_)(x);
195 #define PUSHED(m) Sp -= (m); JMP_(ENTRY_CODE(Sp[m]));
197 /* Here's how to construct these macros:
199 * N = number of N's in the name;
200 * P = number of P's in the name;
202 * while (nonNull(name)) {
203 * if (nextChar == 'P') {
214 # define RET_P(a) PUSH_P(1,a); PUSHED(1)
215 # define RET_N(a) PUSH_N(2,a); PUSHED(2)
217 # define RET_PP(a,b) PUSH_P(2,a); PUSH_P(1,b); PUSHED(2)
218 # define RET_NN(a,b) PUSH_N(4,a); PUSH_N(2,b); PUSHED(4)
219 # define RET_NP(a,b) PUSH_N(3,a); PUSH_P(1,b); PUSHED(3)
221 # define RET_PPP(a,b,c) PUSH_P(3,a); PUSH_P(2,b); PUSH_P(1,c); PUSHED(3)
222 # define RET_NNP(a,b,c) PUSH_N(5,a); PUSH_N(3,b); PUSH_P(1,c); PUSHED(5)
224 # 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)
225 # 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)
226 # 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)
230 /*-----------------------------------------------------------------------------
233 Basically just new*Array - the others are all inline macros.
235 The size arg is always passed in R1, and the result returned in R1.
237 The slow entry point is for returning from a heap check, the saved
238 size argument must be re-loaded from the stack.
239 -------------------------------------------------------------------------- */
241 /* for objects that are *less* than the size of a word, make sure we
242 * round up to the nearest word for the size of the array.
245 #define BYTES_TO_STGWORDS(n) ((n) + sizeof(W_) - 1)/sizeof(W_)
247 #define newByteArray(ty,scale) \
248 FN_(new##ty##Arrayzh_fast) \
250 W_ stuff_size, size, n; \
253 MAYBE_GC(NO_PTRS,new##ty##Arrayzh_fast); \
255 stuff_size = BYTES_TO_STGWORDS(n*scale); \
256 size = sizeofW(StgArrWords)+ stuff_size; \
257 p = (StgArrWords *)RET_STGCALL1(P_,allocate,size); \
258 TICK_ALLOC_PRIM(sizeofW(StgArrWords),stuff_size,0); \
259 SET_HDR(p, &ARR_WORDS_info, CCCS); \
260 p->words = stuff_size; \
261 TICK_RET_UNBOXED_TUP(1) \
266 newByteArray(Char, 1)
267 /* Char arrays really contain only 8-bit bytes for compatibility. */
268 newByteArray(Int, sizeof(I_))
269 newByteArray(Word, sizeof(W_))
270 newByteArray(Addr, sizeof(P_))
271 newByteArray(Float, sizeof(StgFloat))
272 newByteArray(Double, sizeof(StgDouble))
273 newByteArray(StablePtr, sizeof(StgStablePtr))
283 MAYBE_GC(R2_PTR,newArrayzh_fast);
285 size = sizeofW(StgMutArrPtrs) + n;
286 arr = (StgMutArrPtrs *)RET_STGCALL1(P_, allocate, size);
287 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
289 SET_HDR(arr,&MUT_ARR_PTRS_info,CCCS);
293 for (p = (P_)arr + sizeofW(StgMutArrPtrs);
294 p < (P_)arr + size; p++) {
298 TICK_RET_UNBOXED_TUP(1);
303 FN_(newMutVarzh_fast)
306 /* Args: R1.p = initialisation value */
309 HP_CHK_GEN_TICKY(sizeofW(StgMutVar), R1_PTR, newMutVarzh_fast,);
310 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1,1, 0); /* hack, dependent on rep. */
311 CCS_ALLOC(CCCS,sizeofW(StgMutVar));
313 mv = (StgMutVar *)(Hp-sizeofW(StgMutVar)+1);
314 SET_HDR(mv,&MUT_VAR_info,CCCS);
317 TICK_RET_UNBOXED_TUP(1);
322 /* -----------------------------------------------------------------------------
323 Foreign Object Primitives
325 -------------------------------------------------------------------------- */
328 FN_(mkForeignObjzh_fast)
330 /* R1.p = ptr to foreign object,
332 StgForeignObj *result;
335 HP_CHK_GEN_TICKY(sizeofW(StgForeignObj), NO_PTRS, mkForeignObjzh_fast,);
336 TICK_ALLOC_PRIM(sizeofW(StgHeader),
337 sizeofW(StgForeignObj)-sizeofW(StgHeader), 0);
338 CCS_ALLOC(CCCS,sizeofW(StgForeignObj)); /* ccs prof */
340 result = (StgForeignObj *) (Hp + 1 - sizeofW(StgForeignObj));
341 SET_HDR(result,&FOREIGN_info,CCCS);
344 /* returns (# s#, ForeignObj# #) */
345 TICK_RET_UNBOXED_TUP(1);
351 /* These two are out-of-line for the benefit of the NCG */
352 FN_(unsafeThawArrayzh_fast)
355 SET_INFO((StgClosure *)R1.cl,&MUT_ARR_PTRS_info);
356 recordMutable((StgMutClosure*)R1.cl);
358 TICK_RET_UNBOXED_TUP(1);
363 /* -----------------------------------------------------------------------------
364 Weak Pointer Primitives
365 -------------------------------------------------------------------------- */
373 R3.p = finalizer (or NULL)
379 R3.cl = &NO_FINALIZER_closure;
382 HP_CHK_GEN_TICKY(sizeofW(StgWeak),R1_PTR|R2_PTR|R3_PTR, mkWeakzh_fast,);
383 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1, // +1 is for the link field
384 sizeofW(StgWeak)-sizeofW(StgHeader)-1, 0);
385 CCS_ALLOC(CCCS,sizeofW(StgWeak)); /* ccs prof */
387 w = (StgWeak *) (Hp + 1 - sizeofW(StgWeak));
388 SET_HDR(w, &WEAK_info, CCCS);
392 w->finalizer = R3.cl;
394 w->link = weak_ptr_list;
396 IF_DEBUG(weak, fprintf(stderr,"New weak pointer at %p\n",w));
398 TICK_RET_UNBOXED_TUP(1);
403 FN_(finalizzeWeakzh_fast)
410 TICK_RET_UNBOXED_TUP(0);
411 w = (StgDeadWeak *)R1.p;
414 if (w->header.info == &DEAD_WEAK_info) {
415 RET_NP(0,&NO_FINALIZER_closure);
419 w->header.info = &DEAD_WEAK_info;
420 f = ((StgWeak *)w)->finalizer;
421 w->link = ((StgWeak *)w)->link;
423 /* return the finalizer */
424 if (f == &NO_FINALIZER_closure) {
425 RET_NP(0,&NO_FINALIZER_closure);
434 /* -----------------------------------------------------------------------------
435 Arbitrary-precision Integer operations.
436 -------------------------------------------------------------------------- */
438 FN_(int2Integerzh_fast)
440 /* arguments: R1 = Int# */
442 I_ val, s; /* to avoid aliasing */
443 StgArrWords* p; /* address of array result */
447 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, int2Integerzh_fast,);
448 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
449 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
451 p = (StgArrWords *)Hp - 1;
452 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, 1);
454 /* mpz_set_si is inlined here, makes things simpler */
458 } else if (val > 0) {
465 /* returns (# size :: Int#,
469 TICK_RET_UNBOXED_TUP(2);
474 FN_(word2Integerzh_fast)
476 /* arguments: R1 = Word# */
478 W_ val; /* to avoid aliasing */
480 StgArrWords* p; /* address of array result */
484 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, word2Integerzh_fast,)
485 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
486 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
488 p = (StgArrWords *)Hp - 1;
489 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, 1);
498 /* returns (# size :: Int#,
502 TICK_RET_UNBOXED_TUP(2);
507 FN_(addr2Integerzh_fast)
513 MAYBE_GC(NO_PTRS,addr2Integerzh_fast);
515 /* args: R1 :: Addr# */
518 /* Perform the operation */
519 if (RET_STGCALL3(int, mpz_init_set_str,&result,(str),/*base*/10))
522 /* returns (# size :: Int#,
526 TICK_RET_UNBOXED_TUP(2);
527 RET_NP(result._mp_size,
528 result._mp_d - sizeofW(StgArrWords));
533 * 'long long' primops for converting to/from Integers.
536 #ifdef SUPPORT_LONG_LONGS
538 FN_(int64ToIntegerzh_fast)
540 /* arguments: L1 = Int64# */
542 StgInt64 val; /* to avoid aliasing */
544 I_ s, neg, words_needed;
545 StgArrWords* p; /* address of array result */
551 if ( val >= 0x100000000LL || val <= -0x100000000LL ) {
554 /* minimum is one word */
557 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, int64ToIntegerzh_fast,)
558 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
559 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
561 p = (StgArrWords *)(Hp-words_needed+1) - 1;
562 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, words_needed);
569 hi = (W_)((LW_)val / 0x100000000ULL);
571 if ( words_needed == 2 ) {
575 } else if ( val != 0 ) {
578 } else /* val==0 */ {
581 s = ( neg ? -s : s );
583 /* returns (# size :: Int#,
587 TICK_RET_UNBOXED_TUP(2);
592 FN_(word64ToIntegerzh_fast)
594 /* arguments: L1 = Word64# */
596 StgWord64 val; /* to avoid aliasing */
599 StgArrWords* p; /* address of array result */
603 if ( val >= 0x100000000ULL ) {
608 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, word64ToIntegerzh_fast,)
609 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
610 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
612 p = (StgArrWords *)(Hp-words_needed+1) - 1;
613 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, words_needed);
615 hi = (W_)((LW_)val / 0x100000000ULL);
616 if ( val >= 0x100000000ULL ) {
620 } else if ( val != 0 ) {
623 } else /* val==0 */ {
627 /* returns (# size :: Int#,
631 TICK_RET_UNBOXED_TUP(2);
637 #endif /* HAVE_LONG_LONG */
639 /* ToDo: this is shockingly inefficient */
641 #define GMP_TAKE2_RET1(name,mp_fun) \
644 MP_INT arg1, arg2, result; \
650 /* call doYouWantToGC() */ \
651 MAYBE_GC(R2_PTR | R4_PTR, name); \
653 d1 = (StgArrWords *)R2.p; \
655 d2 = (StgArrWords *)R4.p; \
658 arg1._mp_alloc = d1->words; \
659 arg1._mp_size = (s1); \
660 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
661 arg2._mp_alloc = d2->words; \
662 arg2._mp_size = (s2); \
663 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
665 STGCALL1(mpz_init,&result); \
667 /* Perform the operation */ \
668 STGCALL3(mp_fun,&result,&arg1,&arg2); \
670 TICK_RET_UNBOXED_TUP(2); \
671 RET_NP(result._mp_size, \
672 result._mp_d-sizeofW(StgArrWords)); \
676 #define GMP_TAKE2_RET2(name,mp_fun) \
679 MP_INT arg1, arg2, result1, result2; \
685 /* call doYouWantToGC() */ \
686 MAYBE_GC(R2_PTR | R4_PTR, name); \
688 d1 = (StgArrWords *)R2.p; \
690 d2 = (StgArrWords *)R4.p; \
693 arg1._mp_alloc = d1->words; \
694 arg1._mp_size = (s1); \
695 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
696 arg2._mp_alloc = d2->words; \
697 arg2._mp_size = (s2); \
698 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
700 STGCALL1(mpz_init,&result1); \
701 STGCALL1(mpz_init,&result2); \
703 /* Perform the operation */ \
704 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
706 TICK_RET_UNBOXED_TUP(4); \
707 RET_NPNP(result1._mp_size, \
708 result1._mp_d-sizeofW(StgArrWords), \
710 result2._mp_d-sizeofW(StgArrWords)); \
714 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add);
715 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub);
716 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul);
717 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd);
718 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q);
719 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r);
720 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact);
722 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr);
723 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr);
725 #ifndef FLOATS_AS_DOUBLES
726 FN_(decodeFloatzh_fast)
734 /* arguments: F1 = Float# */
737 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, decodeFloatzh_fast,);
738 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
739 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
741 /* Be prepared to tell Lennart-coded __decodeFloat */
742 /* where mantissa._mp_d can be put (it does not care about the rest) */
743 p = (StgArrWords *)Hp - 1;
744 SET_ARR_HDR(p,&ARR_WORDS_info,CCCS,1)
745 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
747 /* Perform the operation */
748 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
750 /* returns: (Int# (expn), Int#, ByteArray#) */
751 TICK_RET_UNBOXED_TUP(3);
752 RET_NNP(exponent,mantissa._mp_size,p);
755 #endif /* !FLOATS_AS_DOUBLES */
757 #define DOUBLE_MANTISSA_SIZE (sizeofW(StgDouble))
758 #define ARR_SIZE (sizeofW(StgArrWords) + DOUBLE_MANTISSA_SIZE)
760 FN_(decodeDoublezh_fast)
767 /* arguments: D1 = Double# */
770 HP_CHK_GEN_TICKY(ARR_SIZE, NO_PTRS, decodeDoublezh_fast,);
771 TICK_ALLOC_PRIM(sizeofW(StgArrWords),DOUBLE_MANTISSA_SIZE,0);
772 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
774 /* Be prepared to tell Lennart-coded __decodeDouble */
775 /* where mantissa.d can be put (it does not care about the rest) */
776 p = (StgArrWords *)(Hp-ARR_SIZE+1);
777 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
778 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
780 /* Perform the operation */
781 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
783 /* returns: (Int# (expn), Int#, ByteArray#) */
784 TICK_RET_UNBOXED_TUP(3);
785 RET_NNP(exponent,mantissa._mp_size,p);
789 /* -----------------------------------------------------------------------------
790 * Concurrency primitives
791 * -------------------------------------------------------------------------- */
796 /* args: R1 = closure to spark */
798 MAYBE_GC(R1_PTR, forkzh_fast);
800 /* create it right now, return ThreadID in R1 */
801 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
802 RtsFlags.GcFlags.initialStkSize, R1.cl);
803 STGCALL1(scheduleThread, R1.t);
805 /* switch at the earliest opportunity */
808 JMP_(ENTRY_CODE(Sp[0]));
815 JMP_(stg_yield_noregs);
826 HP_CHK_GEN_TICKY(sizeofW(StgMVar), NO_PTRS, newMVarzh_fast,);
827 TICK_ALLOC_PRIM(sizeofW(StgMutVar)-1, // consider head,tail,link as admin wds
829 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
831 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
832 SET_HDR(mvar,&EMPTY_MVAR_info,CCCS);
833 mvar->head = mvar->tail = (StgTSO *)&END_TSO_QUEUE_closure;
834 mvar->value = (StgClosure *)&END_TSO_QUEUE_closure;
836 TICK_RET_UNBOXED_TUP(1);
845 const StgInfoTable *info;
848 /* args: R1 = MVar closure */
850 mvar = (StgMVar *)R1.p;
853 info = LOCK_CLOSURE(mvar);
855 info = GET_INFO(mvar);
858 /* If the MVar is empty, put ourselves on its blocking queue,
859 * and wait until we're woken up.
861 if (info == &EMPTY_MVAR_info) {
862 if (mvar->head == (StgTSO *)&END_TSO_QUEUE_closure) {
863 mvar->head = CurrentTSO;
865 mvar->tail->link = CurrentTSO;
867 CurrentTSO->link = (StgTSO *)&END_TSO_QUEUE_closure;
868 CurrentTSO->why_blocked = BlockedOnMVar;
869 CurrentTSO->block_info.closure = (StgClosure *)mvar;
870 mvar->tail = CurrentTSO;
873 /* unlock the MVar */
874 mvar->header.info = &EMPTY_MVAR_info;
876 BLOCK(R1_PTR, takeMVarzh_fast);
880 mvar->value = (StgClosure *)&END_TSO_QUEUE_closure;
882 /* do this last... we might have locked the MVar in the SMP case,
883 * and writing the info pointer will unlock it.
885 SET_INFO(mvar,&EMPTY_MVAR_info);
887 TICK_RET_UNBOXED_TUP(1);
892 FN_(tryTakeMVarzh_fast)
896 const StgInfoTable *info;
899 /* args: R1 = MVar closure */
901 mvar = (StgMVar *)R1.p;
904 info = LOCK_CLOSURE(mvar);
906 info = GET_INFO(mvar);
909 if (info == &EMPTY_MVAR_info) {
912 /* unlock the MVar */
913 mvar->header.info = &EMPTY_MVAR_info;
916 /* HACK: we need a pointer to pass back, so we abuse NO_FINALIZER_closure */
917 RET_NP(0, &NO_FINALIZER_closure);
921 mvar->value = (StgClosure *)&END_TSO_QUEUE_closure;
923 /* do this last... we might have locked the MVar in the SMP case,
924 * and writing the info pointer will unlock it.
926 SET_INFO(mvar,&EMPTY_MVAR_info);
928 TICK_RET_UNBOXED_TUP(1);
936 const StgInfoTable *info;
939 /* args: R1 = MVar, R2 = value */
941 mvar = (StgMVar *)R1.p;
944 info = LOCK_CLOSURE(mvar);
946 info = GET_INFO(mvar);
949 if (info == &FULL_MVAR_info) {
951 fprintf(stderr, "fatal: put on a full MVar in Hugs; aborting\n" );
954 R1.cl = (StgClosure *)PutFullMVar_closure;
961 /* wake up the first thread on the queue, it will continue with the
962 * takeMVar operation and mark the MVar empty again.
964 if (mvar->head != (StgTSO *)&END_TSO_QUEUE_closure) {
965 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
967 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
969 // ToDo: check 2nd arg (mvar) is right
970 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
972 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
974 if (mvar->head == (StgTSO *)&END_TSO_QUEUE_closure) {
975 mvar->tail = (StgTSO *)&END_TSO_QUEUE_closure;
979 /* unlocks the MVar in the SMP case */
980 SET_INFO(mvar,&FULL_MVAR_info);
982 /* ToDo: yield here for better communication performance? */
983 JMP_(ENTRY_CODE(Sp[0]));
987 /* -----------------------------------------------------------------------------
988 Stable pointer primitives
989 ------------------------------------------------------------------------- */
991 FN_(makeStableNamezh_fast)
994 StgStableName *sn_obj;
997 HP_CHK_GEN_TICKY(sizeofW(StgStableName), R1_PTR, makeStableNamezh_fast,);
998 TICK_ALLOC_PRIM(sizeofW(StgHeader),
999 sizeofW(StgStableName)-sizeofW(StgHeader), 0);
1000 CCS_ALLOC(CCCS,sizeofW(StgStableName)); /* ccs prof */
1002 index = RET_STGCALL1(StgWord,lookupStableName,R1.p);
1004 /* Is there already a StableName for this heap object? */
1005 if (stable_ptr_table[index].sn_obj == NULL) {
1006 sn_obj = (StgStableName *) (Hp - sizeofW(StgStableName) + 1);
1007 sn_obj->header.info = &STABLE_NAME_info;
1009 stable_ptr_table[index].sn_obj = (StgClosure *)sn_obj;
1011 (StgClosure *)sn_obj = stable_ptr_table[index].sn_obj;
1014 TICK_RET_UNBOXED_TUP(1);
1018 /* -----------------------------------------------------------------------------
1019 Thread I/O blocking primitives
1020 -------------------------------------------------------------------------- */
1022 FN_(waitReadzh_fast)
1026 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1027 CurrentTSO->why_blocked = BlockedOnRead;
1028 CurrentTSO->block_info.fd = R1.i;
1029 ACQUIRE_LOCK(&sched_mutex);
1030 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1031 RELEASE_LOCK(&sched_mutex);
1032 JMP_(stg_block_noregs);
1036 FN_(waitWritezh_fast)
1040 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1041 CurrentTSO->why_blocked = BlockedOnWrite;
1042 CurrentTSO->block_info.fd = R1.i;
1043 ACQUIRE_LOCK(&sched_mutex);
1044 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1045 RELEASE_LOCK(&sched_mutex);
1046 JMP_(stg_block_noregs);
1056 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1057 CurrentTSO->why_blocked = BlockedOnDelay;
1059 ACQUIRE_LOCK(&sched_mutex);
1061 target = (R1.i / (TICK_MILLISECS*1000)) + timestamp + ticks_since_timestamp;
1062 CurrentTSO->block_info.target = target;
1064 /* Insert the new thread in the sleeping queue. */
1067 while (t != END_TSO_QUEUE && t->block_info.target < target) {
1072 CurrentTSO->link = t;
1074 sleeping_queue = CurrentTSO;
1076 prev->link = CurrentTSO;
1079 RELEASE_LOCK(&sched_mutex);
1080 JMP_(stg_block_noregs);