1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.80 2001/07/23 17:23:19 simonmar Exp $
4 * (c) The GHC Team, 1998-2000
6 * Primitive functions / data
8 * ---------------------------------------------------------------------------*/
14 #include "StgStartup.h"
19 #include "BlockAlloc.h" /* tmp */
20 #include "StablePriv.h"
21 #include "HeapStackCheck.h"
28 classes CCallable and CReturnable don't really exist, but the
29 compiler insists on generating dictionaries containing references
30 to GHC_ZcCCallable_static_info etc., so we provide dummy symbols
31 for these. Some C compilers can't cope with zero-length static arrays,
32 so we have to make these one element long.
35 StgWord GHC_ZCCCallable_static_info[1];
36 StgWord GHC_ZCCReturnable_static_info[1];
38 /* -----------------------------------------------------------------------------
39 Macros for Hand-written primitives.
40 -------------------------------------------------------------------------- */
43 * Horrible macros for returning unboxed tuples.
45 * How an unboxed tuple is returned depends on two factors:
46 * - the number of real registers we have available
47 * - the boxedness of the returned fields.
49 * To return an unboxed tuple from a primitive operation, we have macros
50 * RET_<layout> where <layout> describes the boxedness of each field of the
51 * unboxed tuple: N indicates a non-pointer field, and P indicates a pointer.
53 * We only define the cases actually used, to avoid having too much
54 * garbage in this section. Warning: any bugs in here will be hard to
58 /*------ All Regs available */
60 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
61 # define RET_N(a) RET_P(a)
63 # define RET_PP(a,b) R1.w = (W_)(a); R2.w = (W_)(b); JMP_(ENTRY_CODE(Sp[0]));
64 # define RET_NN(a,b) RET_PP(a,b)
65 # define RET_NP(a,b) RET_PP(a,b)
67 # define RET_PPP(a,b,c) \
68 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); JMP_(ENTRY_CODE(Sp[0]));
69 # define RET_NNP(a,b,c) RET_PPP(a,b,c)
71 # define RET_NNNP(a,b,c,d) \
72 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); R4.w = (W_)d; \
73 JMP_(ENTRY_CODE(Sp[0]));
75 # define RET_NPNP(a,b,c,d) \
76 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); R4.w = (W_)(d); \
77 JMP_(ENTRY_CODE(Sp[0]));
79 # define RET_NNPNNP(a,b,c,d,e,f) \
80 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); \
81 R4.w = (W_)(d); R5.w = (W_)(e); R6.w = (W_)(f); \
82 JMP_(ENTRY_CODE(Sp[0]));
84 #elif defined(REG_R7) || defined(REG_R6) || defined(REG_R5) || \
85 defined(REG_R4) || defined(REG_R3)
86 # error RET_n macros not defined for this setup.
88 /*------ 2 Registers available */
91 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
92 # define RET_N(a) RET_P(a)
94 # define RET_PP(a,b) R1.w = (W_)(a); R2.w = (W_)(b); \
95 JMP_(ENTRY_CODE(Sp[0]));
96 # define RET_NN(a,b) RET_PP(a,b)
97 # define RET_NP(a,b) RET_PP(a,b)
99 # define RET_PPP(a,b,c) \
100 R1.w = (W_)(a); R2.w = (W_)(b); Sp[-1] = (W_)(c); Sp -= 1; \
101 JMP_(ENTRY_CODE(Sp[1]));
102 # define RET_NNP(a,b,c) \
103 R1.w = (W_)(a); R2.w = (W_)(b); Sp[-1] = (W_)(c); Sp -= 1; \
104 JMP_(ENTRY_CODE(Sp[1]));
106 # define RET_NNNP(a,b,c,d) \
109 /* Sp[-3] = ARGTAG(1); */ \
113 JMP_(ENTRY_CODE(Sp[3]));
115 # define RET_NPNP(a,b,c,d) \
118 /* Sp[-3] = ARGTAG(1); */ \
122 JMP_(ENTRY_CODE(Sp[3]));
124 # define RET_NNPNNP(a,b,c,d,e,f) \
128 /* Sp[-5] = ARGTAG(1); */ \
130 /* Sp[-3] = ARGTAG(1); */ \
134 JMP_(ENTRY_CODE(Sp[6]));
136 /*------ 1 Register available */
137 #elif defined(REG_R1)
138 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
139 # define RET_N(a) RET_P(a)
141 # define RET_PP(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 1; \
142 JMP_(ENTRY_CODE(Sp[1]));
143 # define RET_NN(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 2; \
144 JMP_(ENTRY_CODE(Sp[2]));
145 # define RET_NP(a,b) RET_PP(a,b)
147 # define RET_PPP(a,b,c) \
148 R1.w = (W_)(a); Sp[-2] = (W_)(b); Sp[-1] = (W_)(c); Sp -= 2; \
149 JMP_(ENTRY_CODE(Sp[2]));
150 # define RET_NNP(a,b,c) \
151 R1.w = (W_)(a); Sp[-2] = (W_)(b); Sp[-1] = (W_)(c); Sp -= 3; \
152 JMP_(ENTRY_CODE(Sp[3]));
154 # define RET_NNNP(a,b,c,d) \
156 /* Sp[-5] = ARGTAG(1); */ \
158 /* Sp[-3] = ARGTAG(1); */ \
162 JMP_(ENTRY_CODE(Sp[5]));
164 # define RET_NPNP(a,b,c,d) \
167 /* Sp[-3] = ARGTAG(1); */ \
171 JMP_(ENTRY_CODE(Sp[4]));
173 # define RET_NNPNNP(a,b,c,d,e,f) \
177 /* Sp[-3] = ARGTAG(1); */ \
179 /* Sp[-5] = ARGTAG(1); */ \
182 /* Sp[-8] = ARGTAG(1); */ \
184 JMP_(ENTRY_CODE(Sp[8]));
186 #else /* 0 Regs available */
188 #define PUSH_P(o,x) Sp[-o] = (W_)(x)
191 #define PUSH_N(o,x) Sp[1-o] = (W_)(x); Sp[-o] = ARG_TAG(1);
193 #define PUSH_N(o,x) Sp[1-o] = (W_)(x);
196 #define PUSHED(m) Sp -= (m); JMP_(ENTRY_CODE(Sp[m]));
198 /* Here's how to construct these macros:
200 * N = number of N's in the name;
201 * P = number of P's in the name;
203 * while (nonNull(name)) {
204 * if (nextChar == 'P') {
215 # define RET_P(a) PUSH_P(1,a); PUSHED(1)
216 # define RET_N(a) PUSH_N(2,a); PUSHED(2)
218 # define RET_PP(a,b) PUSH_P(2,a); PUSH_P(1,b); PUSHED(2)
219 # define RET_NN(a,b) PUSH_N(4,a); PUSH_N(2,b); PUSHED(4)
220 # define RET_NP(a,b) PUSH_N(3,a); PUSH_P(1,b); PUSHED(3)
222 # define RET_PPP(a,b,c) PUSH_P(3,a); PUSH_P(2,b); PUSH_P(1,c); PUSHED(3)
223 # define RET_NNP(a,b,c) PUSH_N(5,a); PUSH_N(3,b); PUSH_P(1,c); PUSHED(5)
225 # 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)
226 # 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)
227 # 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)
231 /*-----------------------------------------------------------------------------
234 Basically just new*Array - the others are all inline macros.
236 The size arg is always passed in R1, and the result returned in R1.
238 The slow entry point is for returning from a heap check, the saved
239 size argument must be re-loaded from the stack.
240 -------------------------------------------------------------------------- */
242 /* for objects that are *less* than the size of a word, make sure we
243 * round up to the nearest word for the size of the array.
246 #define BYTES_TO_STGWORDS(n) ((n) + sizeof(W_) - 1)/sizeof(W_)
248 FN_(newByteArrayzh_fast) \
250 W_ size, stuff_size, n; \
253 MAYBE_GC(NO_PTRS,newByteArrayzh_fast); \
255 stuff_size = BYTES_TO_STGWORDS(n); \
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, &stg_ARR_WORDS_info, CCCS); \
260 p->words = stuff_size; \
261 TICK_RET_UNBOXED_TUP(1) \
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,&stg_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,&stg_MUT_VAR_info,CCCS);
308 TICK_RET_UNBOXED_TUP(1);
313 /* -----------------------------------------------------------------------------
314 Foreign Object Primitives
316 -------------------------------------------------------------------------- */
318 FN_(mkForeignObjzh_fast)
320 /* R1.p = ptr to foreign object,
322 StgForeignObj *result;
325 HP_CHK_GEN_TICKY(sizeofW(StgForeignObj), NO_PTRS, mkForeignObjzh_fast,);
326 TICK_ALLOC_PRIM(sizeofW(StgHeader),
327 sizeofW(StgForeignObj)-sizeofW(StgHeader), 0);
328 CCS_ALLOC(CCCS,sizeofW(StgForeignObj)); /* ccs prof */
330 result = (StgForeignObj *) (Hp + 1 - sizeofW(StgForeignObj));
331 SET_HDR(result,&stg_FOREIGN_info,CCCS);
334 /* returns (# s#, ForeignObj# #) */
335 TICK_RET_UNBOXED_TUP(1);
340 /* These two are out-of-line for the benefit of the NCG */
341 FN_(unsafeThawArrayzh_fast)
344 SET_INFO((StgClosure *)R1.cl,&stg_MUT_ARR_PTRS_info);
345 recordMutable((StgMutClosure*)R1.cl);
347 TICK_RET_UNBOXED_TUP(1);
352 /* -----------------------------------------------------------------------------
353 Weak Pointer Primitives
354 -------------------------------------------------------------------------- */
360 R3.p = finalizer (or NULL)
366 R3.cl = &stg_NO_FINALIZER_closure;
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, &stg_WEAK_info, CCCS);
379 w->finalizer = R3.cl;
381 w->link = weak_ptr_list;
383 IF_DEBUG(weak, fprintf(stderr,"New weak pointer at %p\n",w));
385 TICK_RET_UNBOXED_TUP(1);
390 FN_(finalizzeWeakzh_fast)
397 TICK_RET_UNBOXED_TUP(0);
398 w = (StgDeadWeak *)R1.p;
401 if (w->header.info == &stg_DEAD_WEAK_info) {
402 RET_NP(0,&stg_NO_FINALIZER_closure);
406 w->header.info = &stg_DEAD_WEAK_info;
407 f = ((StgWeak *)w)->finalizer;
408 w->link = ((StgWeak *)w)->link;
410 /* return the finalizer */
411 if (f == &stg_NO_FINALIZER_closure) {
412 RET_NP(0,&stg_NO_FINALIZER_closure);
419 /* -----------------------------------------------------------------------------
420 Arbitrary-precision Integer operations.
421 -------------------------------------------------------------------------- */
423 FN_(int2Integerzh_fast)
425 /* arguments: R1 = Int# */
427 I_ val, s; /* to avoid aliasing */
428 StgArrWords* p; /* address of array result */
432 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, int2Integerzh_fast,);
433 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
434 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
436 p = (StgArrWords *)Hp - 1;
437 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
439 /* mpz_set_si is inlined here, makes things simpler */
443 } else if (val > 0) {
450 /* returns (# size :: Int#,
454 TICK_RET_UNBOXED_TUP(2);
459 FN_(word2Integerzh_fast)
461 /* arguments: R1 = Word# */
463 W_ val; /* to avoid aliasing */
465 StgArrWords* p; /* address of array result */
469 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, word2Integerzh_fast,)
470 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
471 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
473 p = (StgArrWords *)Hp - 1;
474 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
483 /* returns (# size :: Int#,
487 TICK_RET_UNBOXED_TUP(2);
494 * 'long long' primops for converting to/from Integers.
497 #ifdef SUPPORT_LONG_LONGS
499 FN_(int64ToIntegerzh_fast)
501 /* arguments: L1 = Int64# */
503 StgInt64 val; /* to avoid aliasing */
505 I_ s, neg, words_needed;
506 StgArrWords* p; /* address of array result */
512 if ( val >= 0x100000000LL || val <= -0x100000000LL ) {
515 /* minimum is one word */
518 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, int64ToIntegerzh_fast,)
519 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
520 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
522 p = (StgArrWords *)(Hp-words_needed+1) - 1;
523 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
530 hi = (W_)((LW_)val / 0x100000000ULL);
532 if ( words_needed == 2 ) {
536 } else if ( val != 0 ) {
539 } else /* val==0 */ {
542 s = ( neg ? -s : s );
544 /* returns (# size :: Int#,
548 TICK_RET_UNBOXED_TUP(2);
553 FN_(word64ToIntegerzh_fast)
555 /* arguments: L1 = Word64# */
557 StgWord64 val; /* to avoid aliasing */
560 StgArrWords* p; /* address of array result */
564 if ( val >= 0x100000000ULL ) {
569 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, word64ToIntegerzh_fast,)
570 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
571 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
573 p = (StgArrWords *)(Hp-words_needed+1) - 1;
574 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
576 hi = (W_)((LW_)val / 0x100000000ULL);
577 if ( val >= 0x100000000ULL ) {
581 } else if ( val != 0 ) {
584 } else /* val==0 */ {
588 /* returns (# size :: Int#,
592 TICK_RET_UNBOXED_TUP(2);
597 #elif SIZEOF_VOID_P == 8
599 FN_(word64ToIntegerzh_fast)
602 JMP_(wordToIntegerzh_fast);
606 FN_(int64ToIntegerzh_fast)
609 JMP_(intToIntegerzh_fast);
613 #endif /* SUPPORT_LONG_LONGS || SIZEOF_VOID_P == 8 */
615 /* ToDo: this is shockingly inefficient */
617 #define GMP_TAKE2_RET1(name,mp_fun) \
620 MP_INT arg1, arg2, result; \
626 /* call doYouWantToGC() */ \
627 MAYBE_GC(R2_PTR | R4_PTR, name); \
629 d1 = (StgArrWords *)R2.p; \
631 d2 = (StgArrWords *)R4.p; \
634 arg1._mp_alloc = d1->words; \
635 arg1._mp_size = (s1); \
636 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
637 arg2._mp_alloc = d2->words; \
638 arg2._mp_size = (s2); \
639 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
641 STGCALL1(mpz_init,&result); \
643 /* Perform the operation */ \
644 STGCALL3(mp_fun,&result,&arg1,&arg2); \
646 TICK_RET_UNBOXED_TUP(2); \
647 RET_NP(result._mp_size, \
648 result._mp_d-sizeofW(StgArrWords)); \
652 #define GMP_TAKE1_RET1(name,mp_fun) \
655 MP_INT arg1, result; \
660 /* call doYouWantToGC() */ \
661 MAYBE_GC(R2_PTR, name); \
663 d1 = (StgArrWords *)R2.p; \
666 arg1._mp_alloc = d1->words; \
667 arg1._mp_size = (s1); \
668 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
670 STGCALL1(mpz_init,&result); \
672 /* Perform the operation */ \
673 STGCALL2(mp_fun,&result,&arg1); \
675 TICK_RET_UNBOXED_TUP(2); \
676 RET_NP(result._mp_size, \
677 result._mp_d-sizeofW(StgArrWords)); \
681 #define GMP_TAKE2_RET2(name,mp_fun) \
684 MP_INT arg1, arg2, result1, result2; \
690 /* call doYouWantToGC() */ \
691 MAYBE_GC(R2_PTR | R4_PTR, name); \
693 d1 = (StgArrWords *)R2.p; \
695 d2 = (StgArrWords *)R4.p; \
698 arg1._mp_alloc = d1->words; \
699 arg1._mp_size = (s1); \
700 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
701 arg2._mp_alloc = d2->words; \
702 arg2._mp_size = (s2); \
703 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
705 STGCALL1(mpz_init,&result1); \
706 STGCALL1(mpz_init,&result2); \
708 /* Perform the operation */ \
709 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
711 TICK_RET_UNBOXED_TUP(4); \
712 RET_NPNP(result1._mp_size, \
713 result1._mp_d-sizeofW(StgArrWords), \
715 result2._mp_d-sizeofW(StgArrWords)); \
719 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add);
720 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub);
721 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul);
722 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd);
723 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q);
724 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r);
725 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact);
726 GMP_TAKE2_RET1(andIntegerzh_fast, mpz_and);
727 GMP_TAKE2_RET1(orIntegerzh_fast, mpz_ior);
728 GMP_TAKE2_RET1(xorIntegerzh_fast, mpz_xor);
729 GMP_TAKE1_RET1(complementIntegerzh_fast, mpz_com);
731 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr);
732 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr);
734 FN_(decodeFloatzh_fast)
742 /* arguments: F1 = Float# */
745 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, decodeFloatzh_fast,);
746 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
747 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
749 /* Be prepared to tell Lennart-coded __decodeFloat */
750 /* where mantissa._mp_d can be put (it does not care about the rest) */
751 p = (StgArrWords *)Hp - 1;
752 SET_ARR_HDR(p,&stg_ARR_WORDS_info,CCCS,1)
753 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
755 /* Perform the operation */
756 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
758 /* returns: (Int# (expn), Int#, ByteArray#) */
759 TICK_RET_UNBOXED_TUP(3);
760 RET_NNP(exponent,mantissa._mp_size,p);
764 #define DOUBLE_MANTISSA_SIZE (sizeofW(StgDouble))
765 #define ARR_SIZE (sizeofW(StgArrWords) + DOUBLE_MANTISSA_SIZE)
767 FN_(decodeDoublezh_fast)
774 /* arguments: D1 = Double# */
777 HP_CHK_GEN_TICKY(ARR_SIZE, NO_PTRS, decodeDoublezh_fast,);
778 TICK_ALLOC_PRIM(sizeofW(StgArrWords),DOUBLE_MANTISSA_SIZE,0);
779 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
781 /* Be prepared to tell Lennart-coded __decodeDouble */
782 /* where mantissa.d can be put (it does not care about the rest) */
783 p = (StgArrWords *)(Hp-ARR_SIZE+1);
784 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
785 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
787 /* Perform the operation */
788 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
790 /* returns: (Int# (expn), Int#, ByteArray#) */
791 TICK_RET_UNBOXED_TUP(3);
792 RET_NNP(exponent,mantissa._mp_size,p);
796 /* -----------------------------------------------------------------------------
797 * Concurrency primitives
798 * -------------------------------------------------------------------------- */
803 /* args: R1 = closure to spark */
805 MAYBE_GC(R1_PTR, forkzh_fast);
807 /* create it right now, return ThreadID in R1 */
808 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
809 RtsFlags.GcFlags.initialStkSize, R1.cl);
810 STGCALL1(scheduleThread, R1.t);
812 /* switch at the earliest opportunity */
815 JMP_(ENTRY_CODE(Sp[0]));
822 JMP_(stg_yield_noregs);
826 /* -----------------------------------------------------------------------------
829 * take & putMVar work as follows. Firstly, an important invariant:
831 * If the MVar is full, then the blocking queue contains only
832 * threads blocked on putMVar, and if the MVar is empty then the
833 * blocking queue contains only threads blocked on takeMVar.
836 * MVar empty : then add ourselves to the blocking queue
837 * MVar full : remove the value from the MVar, and
838 * blocking queue empty : return
839 * blocking queue non-empty : perform the first blocked putMVar
840 * from the queue, and wake up the
841 * thread (MVar is now full again)
843 * putMVar is just the dual of the above algorithm.
845 * How do we "perform a putMVar"? Well, we have to fiddle around with
846 * the stack of the thread waiting to do the putMVar. See
847 * stg_block_putmvar and stg_block_takemvar in HeapStackCheck.c for
848 * the stack layout, and the PerformPut and PerformTake macros below.
850 * It is important that a blocked take or put is woken up with the
851 * take/put already performed, because otherwise there would be a
852 * small window of vulnerability where the thread could receive an
853 * exception and never perform its take or put, and we'd end up with a
856 * -------------------------------------------------------------------------- */
865 HP_CHK_GEN_TICKY(sizeofW(StgMVar), NO_PTRS, newMVarzh_fast,);
866 TICK_ALLOC_PRIM(sizeofW(StgMutVar)-1, // consider head,tail,link as admin wds
868 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
870 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
871 SET_HDR(mvar,&stg_EMPTY_MVAR_info,CCCS);
872 mvar->head = mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
873 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
875 TICK_RET_UNBOXED_TUP(1);
880 #define PerformTake(tso, value) ({ \
881 (tso)->sp[1] = (W_)value; \
882 (tso)->sp[0] = (W_)&stg_gc_unpt_r1_ret_info; \
885 #define PerformPut(tso) ({ \
886 StgClosure *val = (StgClosure *)(tso)->sp[2]; \
887 (tso)->sp[2] = (W_)&stg_gc_noregs_ret_info; \
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 the MVar is empty, put ourselves on its blocking queue,
910 * and wait until we're woken up.
912 if (info == &stg_EMPTY_MVAR_info) {
913 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
914 mvar->head = CurrentTSO;
916 mvar->tail->link = CurrentTSO;
918 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
919 CurrentTSO->why_blocked = BlockedOnMVar;
920 CurrentTSO->block_info.closure = (StgClosure *)mvar;
921 mvar->tail = CurrentTSO;
924 /* unlock the MVar */
925 mvar->header.info = &stg_EMPTY_MVAR_info;
927 JMP_(stg_block_takemvar);
930 /* we got the value... */
933 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
934 /* There are putMVar(s) waiting...
935 * wake up the first thread on the queue
937 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
939 /* actually perform the putMVar for the thread that we just woke up */
940 mvar->value = PerformPut(mvar->head);
942 #if defined(GRAN) || defined(PAR)
943 /* ToDo: check 2nd arg (mvar) is right */
944 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
946 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
948 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
949 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
952 /* unlock in the SMP case */
953 SET_INFO(mvar,&stg_FULL_MVAR_info);
955 TICK_RET_UNBOXED_TUP(1);
958 /* No further putMVars, MVar is now empty */
960 /* do this last... we might have locked the MVar in the SMP case,
961 * and writing the info pointer will unlock it.
963 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
964 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
965 TICK_RET_UNBOXED_TUP(1);
971 FN_(tryTakeMVarzh_fast)
975 const StgInfoTable *info;
978 /* args: R1 = MVar closure */
980 mvar = (StgMVar *)R1.p;
983 info = LOCK_CLOSURE(mvar);
985 info = GET_INFO(mvar);
988 if (info == &stg_EMPTY_MVAR_info) {
991 /* unlock the MVar */
992 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
995 /* HACK: we need a pointer to pass back,
996 * so we abuse NO_FINALIZER_closure
998 RET_NP(0, &stg_NO_FINALIZER_closure);
1001 /* we got the value... */
1004 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1005 /* There are putMVar(s) waiting...
1006 * wake up the first thread on the queue
1008 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1010 /* actually perform the putMVar for the thread that we just woke up */
1011 mvar->value = PerformPut(mvar->head);
1013 #if defined(GRAN) || defined(PAR)
1014 /* ToDo: check 2nd arg (mvar) is right */
1015 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1017 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1019 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1020 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1023 /* unlock in the SMP case */
1024 SET_INFO(mvar,&stg_FULL_MVAR_info);
1026 TICK_RET_UNBOXED_TUP(1);
1029 /* No further putMVars, MVar is now empty */
1031 /* do this last... we might have locked the MVar in the SMP case,
1032 * and writing the info pointer will unlock it.
1034 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1035 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1036 TICK_RET_UNBOXED_TUP(1);
1045 const StgInfoTable *info;
1048 /* args: R1 = MVar, R2 = value */
1050 mvar = (StgMVar *)R1.p;
1053 info = LOCK_CLOSURE(mvar);
1055 info = GET_INFO(mvar);
1058 if (info == &stg_FULL_MVAR_info) {
1059 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1060 mvar->head = CurrentTSO;
1062 mvar->tail->link = CurrentTSO;
1064 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1065 CurrentTSO->why_blocked = BlockedOnMVar;
1066 CurrentTSO->block_info.closure = (StgClosure *)mvar;
1067 mvar->tail = CurrentTSO;
1070 /* unlock the MVar */
1071 SET_INFO(mvar,&stg_FULL_MVAR_info);
1073 JMP_(stg_block_putmvar);
1076 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1077 /* There are takeMVar(s) waiting: wake up the first one
1079 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1081 /* actually perform the takeMVar */
1082 PerformTake(mvar->head, R2.cl);
1084 #if defined(GRAN) || defined(PAR)
1085 /* ToDo: check 2nd arg (mvar) is right */
1086 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1088 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1090 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1091 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1094 /* unlocks the MVar in the SMP case */
1095 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1097 JMP_(ENTRY_CODE(Sp[0]));
1099 /* No further takes, the MVar is now full. */
1100 mvar->value = R2.cl;
1101 /* unlocks the MVar in the SMP case */
1102 SET_INFO(mvar,&stg_FULL_MVAR_info);
1103 JMP_(ENTRY_CODE(Sp[0]));
1106 /* ToDo: yield afterward for better communication performance? */
1110 FN_(tryPutMVarzh_fast)
1113 const StgInfoTable *info;
1116 /* args: R1 = MVar, R2 = value */
1118 mvar = (StgMVar *)R1.p;
1121 info = LOCK_CLOSURE(mvar);
1123 info = GET_INFO(mvar);
1126 if (info == &stg_FULL_MVAR_info) {
1129 /* unlock the MVar */
1130 mvar->header.info = &stg_FULL_MVAR_info;
1136 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1137 /* There are takeMVar(s) waiting: wake up the first one
1139 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1141 /* actually perform the takeMVar */
1142 PerformTake(mvar->head, R2.cl);
1144 #if defined(GRAN) || defined(PAR)
1145 /* ToDo: check 2nd arg (mvar) is right */
1146 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1148 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1150 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1151 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1154 /* unlocks the MVar in the SMP case */
1155 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1157 JMP_(ENTRY_CODE(Sp[0]));
1159 /* No further takes, the MVar is now full. */
1160 mvar->value = R2.cl;
1161 /* unlocks the MVar in the SMP case */
1162 SET_INFO(mvar,&stg_FULL_MVAR_info);
1163 JMP_(ENTRY_CODE(Sp[0]));
1166 /* ToDo: yield afterward for better communication performance? */
1170 /* -----------------------------------------------------------------------------
1171 Stable pointer primitives
1172 ------------------------------------------------------------------------- */
1174 FN_(makeStableNamezh_fast)
1177 StgStableName *sn_obj;
1180 HP_CHK_GEN_TICKY(sizeofW(StgStableName), R1_PTR, makeStableNamezh_fast,);
1181 TICK_ALLOC_PRIM(sizeofW(StgHeader),
1182 sizeofW(StgStableName)-sizeofW(StgHeader), 0);
1183 CCS_ALLOC(CCCS,sizeofW(StgStableName)); /* ccs prof */
1185 index = RET_STGCALL1(StgWord,lookupStableName,R1.p);
1187 /* Is there already a StableName for this heap object? */
1188 if (stable_ptr_table[index].sn_obj == NULL) {
1189 sn_obj = (StgStableName *) (Hp - sizeofW(StgStableName) + 1);
1190 SET_HDR(sn_obj,&stg_STABLE_NAME_info,CCCS);
1192 stable_ptr_table[index].sn_obj = (StgClosure *)sn_obj;
1194 (StgClosure *)sn_obj = stable_ptr_table[index].sn_obj;
1197 TICK_RET_UNBOXED_TUP(1);
1201 /* -----------------------------------------------------------------------------
1202 Bytecode object primitives
1203 ------------------------------------------------------------------------- */
1215 HP_CHK_GEN_TICKY(sizeofW(StgBCO),R1_PTR|R2_PTR|R3_PTR|R4_PTR, newBCOzh_fast,);
1216 TICK_ALLOC_PRIM(sizeofW(StgHeader), sizeofW(StgBCO)-sizeofW(StgHeader), 0);
1217 CCS_ALLOC(CCCS,sizeofW(StgBCO)); /* ccs prof */
1218 bco = (StgBCO *) (Hp + 1 - sizeofW(StgBCO));
1219 SET_HDR(bco, &stg_BCO_info, CCCS);
1221 bco->instrs = (StgArrWords*)R1.cl;
1222 bco->literals = (StgArrWords*)R2.cl;
1223 bco->ptrs = (StgMutArrPtrs*)R3.cl;
1224 bco->itbls = (StgArrWords*)R4.cl;
1226 TICK_RET_UNBOXED_TUP(1);
1231 FN_(mkApUpd0zh_fast)
1233 /* R1.p = the fn for the AP_UPD
1237 HP_CHK_GEN_TICKY(AP_sizeW(0), R1_PTR, mkApUpd0zh_fast,);
1238 TICK_ALLOC_PRIM(sizeofW(StgHeader), AP_sizeW(0)-sizeofW(StgHeader), 0);
1239 CCS_ALLOC(CCCS,AP_sizeW(0)); /* ccs prof */
1240 ap = (StgAP_UPD *) (Hp + 1 - AP_sizeW(0));
1241 SET_HDR(ap, &stg_AP_UPD_info, CCCS);
1246 TICK_RET_UNBOXED_TUP(1);
1251 /* -----------------------------------------------------------------------------
1252 Thread I/O blocking primitives
1253 -------------------------------------------------------------------------- */
1255 FN_(waitReadzh_fast)
1259 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1260 CurrentTSO->why_blocked = BlockedOnRead;
1261 CurrentTSO->block_info.fd = R1.i;
1262 ACQUIRE_LOCK(&sched_mutex);
1263 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1264 RELEASE_LOCK(&sched_mutex);
1265 JMP_(stg_block_noregs);
1269 FN_(waitWritezh_fast)
1273 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1274 CurrentTSO->why_blocked = BlockedOnWrite;
1275 CurrentTSO->block_info.fd = R1.i;
1276 ACQUIRE_LOCK(&sched_mutex);
1277 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1278 RELEASE_LOCK(&sched_mutex);
1279 JMP_(stg_block_noregs);
1289 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1290 CurrentTSO->why_blocked = BlockedOnDelay;
1292 ACQUIRE_LOCK(&sched_mutex);
1294 target = (R1.i / (TICK_MILLISECS*1000)) + getourtimeofday();
1295 CurrentTSO->block_info.target = target;
1297 /* Insert the new thread in the sleeping queue. */
1300 while (t != END_TSO_QUEUE && t->block_info.target < target) {
1305 CurrentTSO->link = t;
1307 sleeping_queue = CurrentTSO;
1309 prev->link = CurrentTSO;
1312 RELEASE_LOCK(&sched_mutex);
1313 JMP_(stg_block_noregs);