1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.78 2001/03/26 13:43:05 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);
598 #endif /* HAVE_LONG_LONG */
600 /* ToDo: this is shockingly inefficient */
602 #define GMP_TAKE2_RET1(name,mp_fun) \
605 MP_INT arg1, arg2, result; \
611 /* call doYouWantToGC() */ \
612 MAYBE_GC(R2_PTR | R4_PTR, name); \
614 d1 = (StgArrWords *)R2.p; \
616 d2 = (StgArrWords *)R4.p; \
619 arg1._mp_alloc = d1->words; \
620 arg1._mp_size = (s1); \
621 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
622 arg2._mp_alloc = d2->words; \
623 arg2._mp_size = (s2); \
624 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
626 STGCALL1(mpz_init,&result); \
628 /* Perform the operation */ \
629 STGCALL3(mp_fun,&result,&arg1,&arg2); \
631 TICK_RET_UNBOXED_TUP(2); \
632 RET_NP(result._mp_size, \
633 result._mp_d-sizeofW(StgArrWords)); \
637 #define GMP_TAKE1_RET1(name,mp_fun) \
640 MP_INT arg1, result; \
645 /* call doYouWantToGC() */ \
646 MAYBE_GC(R2_PTR, name); \
648 d1 = (StgArrWords *)R2.p; \
651 arg1._mp_alloc = d1->words; \
652 arg1._mp_size = (s1); \
653 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
655 STGCALL1(mpz_init,&result); \
657 /* Perform the operation */ \
658 STGCALL2(mp_fun,&result,&arg1); \
660 TICK_RET_UNBOXED_TUP(2); \
661 RET_NP(result._mp_size, \
662 result._mp_d-sizeofW(StgArrWords)); \
666 #define GMP_TAKE2_RET2(name,mp_fun) \
669 MP_INT arg1, arg2, result1, result2; \
675 /* call doYouWantToGC() */ \
676 MAYBE_GC(R2_PTR | R4_PTR, name); \
678 d1 = (StgArrWords *)R2.p; \
680 d2 = (StgArrWords *)R4.p; \
683 arg1._mp_alloc = d1->words; \
684 arg1._mp_size = (s1); \
685 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
686 arg2._mp_alloc = d2->words; \
687 arg2._mp_size = (s2); \
688 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
690 STGCALL1(mpz_init,&result1); \
691 STGCALL1(mpz_init,&result2); \
693 /* Perform the operation */ \
694 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
696 TICK_RET_UNBOXED_TUP(4); \
697 RET_NPNP(result1._mp_size, \
698 result1._mp_d-sizeofW(StgArrWords), \
700 result2._mp_d-sizeofW(StgArrWords)); \
704 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add);
705 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub);
706 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul);
707 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd);
708 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q);
709 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r);
710 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact);
711 GMP_TAKE2_RET1(andIntegerzh_fast, mpz_and);
712 GMP_TAKE2_RET1(orIntegerzh_fast, mpz_ior);
713 GMP_TAKE2_RET1(xorIntegerzh_fast, mpz_xor);
714 GMP_TAKE1_RET1(complementIntegerzh_fast, mpz_com);
716 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr);
717 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr);
719 FN_(decodeFloatzh_fast)
727 /* arguments: F1 = Float# */
730 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, decodeFloatzh_fast,);
731 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
732 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
734 /* Be prepared to tell Lennart-coded __decodeFloat */
735 /* where mantissa._mp_d can be put (it does not care about the rest) */
736 p = (StgArrWords *)Hp - 1;
737 SET_ARR_HDR(p,&stg_ARR_WORDS_info,CCCS,1)
738 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
740 /* Perform the operation */
741 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
743 /* returns: (Int# (expn), Int#, ByteArray#) */
744 TICK_RET_UNBOXED_TUP(3);
745 RET_NNP(exponent,mantissa._mp_size,p);
749 #define DOUBLE_MANTISSA_SIZE (sizeofW(StgDouble))
750 #define ARR_SIZE (sizeofW(StgArrWords) + DOUBLE_MANTISSA_SIZE)
752 FN_(decodeDoublezh_fast)
759 /* arguments: D1 = Double# */
762 HP_CHK_GEN_TICKY(ARR_SIZE, NO_PTRS, decodeDoublezh_fast,);
763 TICK_ALLOC_PRIM(sizeofW(StgArrWords),DOUBLE_MANTISSA_SIZE,0);
764 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
766 /* Be prepared to tell Lennart-coded __decodeDouble */
767 /* where mantissa.d can be put (it does not care about the rest) */
768 p = (StgArrWords *)(Hp-ARR_SIZE+1);
769 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
770 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
772 /* Perform the operation */
773 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
775 /* returns: (Int# (expn), Int#, ByteArray#) */
776 TICK_RET_UNBOXED_TUP(3);
777 RET_NNP(exponent,mantissa._mp_size,p);
781 /* -----------------------------------------------------------------------------
782 * Concurrency primitives
783 * -------------------------------------------------------------------------- */
788 /* args: R1 = closure to spark */
790 MAYBE_GC(R1_PTR, forkzh_fast);
792 /* create it right now, return ThreadID in R1 */
793 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
794 RtsFlags.GcFlags.initialStkSize, R1.cl);
795 STGCALL1(scheduleThread, R1.t);
797 /* switch at the earliest opportunity */
800 JMP_(ENTRY_CODE(Sp[0]));
807 JMP_(stg_yield_noregs);
818 HP_CHK_GEN_TICKY(sizeofW(StgMVar), NO_PTRS, newMVarzh_fast,);
819 TICK_ALLOC_PRIM(sizeofW(StgMutVar)-1, // consider head,tail,link as admin wds
821 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
823 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
824 SET_HDR(mvar,&stg_EMPTY_MVAR_info,CCCS);
825 mvar->head = mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
826 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
828 TICK_RET_UNBOXED_TUP(1);
837 const StgInfoTable *info;
840 /* args: R1 = MVar closure */
842 mvar = (StgMVar *)R1.p;
845 info = LOCK_CLOSURE(mvar);
847 info = GET_INFO(mvar);
850 /* If the MVar is empty, put ourselves on its blocking queue,
851 * and wait until we're woken up.
853 if (info == &stg_EMPTY_MVAR_info) {
854 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
855 mvar->head = CurrentTSO;
857 mvar->tail->link = CurrentTSO;
859 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
860 CurrentTSO->why_blocked = BlockedOnMVar;
861 CurrentTSO->block_info.closure = (StgClosure *)mvar;
862 mvar->tail = CurrentTSO;
865 /* unlock the MVar */
866 mvar->header.info = &stg_EMPTY_MVAR_info;
868 BLOCK(R1_PTR, takeMVarzh_fast);
872 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
874 /* wake up the first thread on the queue
876 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
877 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
878 #if defined(GRAN) || defined(PAR)
879 /* ToDo: check 2nd arg (mvar) is right */
880 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
882 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
884 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
885 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
889 /* do this last... we might have locked the MVar in the SMP case,
890 * and writing the info pointer will unlock it.
892 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
894 TICK_RET_UNBOXED_TUP(1);
899 FN_(tryTakeMVarzh_fast)
903 const StgInfoTable *info;
906 /* args: R1 = MVar closure */
908 mvar = (StgMVar *)R1.p;
911 info = LOCK_CLOSURE(mvar);
913 info = GET_INFO(mvar);
916 if (info == &stg_EMPTY_MVAR_info) {
919 /* unlock the MVar */
920 mvar->header.info = &stg_EMPTY_MVAR_info;
923 /* HACK: we need a pointer to pass back, so we abuse NO_FINALIZER_closure */
924 RET_NP(0, &stg_NO_FINALIZER_closure);
928 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
930 /* wake up the first thread on the queue
932 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
933 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
934 #if defined(GRAN) || defined(PAR)
935 /* ToDo: check 2nd arg (mvar) is right */
936 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
938 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
940 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
941 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
945 /* do this last... we might have locked the MVar in the SMP case,
946 * and writing the info pointer will unlock it.
948 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
950 TICK_RET_UNBOXED_TUP(1);
958 const StgInfoTable *info;
961 /* args: R1 = MVar, R2 = value */
963 mvar = (StgMVar *)R1.p;
966 info = LOCK_CLOSURE(mvar);
968 info = GET_INFO(mvar);
971 if (info == &stg_FULL_MVAR_info) {
972 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
973 mvar->head = CurrentTSO;
975 mvar->tail->link = CurrentTSO;
977 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
978 CurrentTSO->why_blocked = BlockedOnMVar;
979 CurrentTSO->block_info.closure = (StgClosure *)mvar;
980 mvar->tail = CurrentTSO;
983 /* unlock the MVar */
984 mvar->header.info = &stg_FULL_MVAR_info;
986 BLOCK( R1_PTR | R2_PTR, putMVarzh_fast );
991 /* wake up the first thread on the queue, it will continue with the
992 * takeMVar operation and mark the MVar empty again.
994 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
995 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
996 #if defined(GRAN) || defined(PAR)
997 /* ToDo: check 2nd arg (mvar) is right */
998 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1000 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1002 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1003 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1007 /* unlocks the MVar in the SMP case */
1008 SET_INFO(mvar,&stg_FULL_MVAR_info);
1010 /* ToDo: yield here for better communication performance? */
1011 JMP_(ENTRY_CODE(Sp[0]));
1015 FN_(tryPutMVarzh_fast)
1018 const StgInfoTable *info;
1021 /* args: R1 = MVar, R2 = value */
1023 mvar = (StgMVar *)R1.p;
1026 info = LOCK_CLOSURE(mvar);
1028 info = GET_INFO(mvar);
1031 if (info == &stg_FULL_MVAR_info) {
1034 /* unlock the MVar */
1035 mvar->header.info = &stg_FULL_MVAR_info;
1041 mvar->value = R2.cl;
1043 /* wake up the first thread on the queue, it will continue with the
1044 * takeMVar operation and mark the MVar empty again.
1046 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1047 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1048 #if defined(GRAN) || defined(PAR)
1049 /* ToDo: check 2nd arg (mvar) is right */
1050 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1052 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1054 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1055 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1059 /* unlocks the MVar in the SMP case */
1060 SET_INFO(mvar,&stg_FULL_MVAR_info);
1062 /* ToDo: yield here for better communication performance? */
1067 /* -----------------------------------------------------------------------------
1068 Stable pointer primitives
1069 ------------------------------------------------------------------------- */
1071 FN_(makeStableNamezh_fast)
1074 StgStableName *sn_obj;
1077 HP_CHK_GEN_TICKY(sizeofW(StgStableName), R1_PTR, makeStableNamezh_fast,);
1078 TICK_ALLOC_PRIM(sizeofW(StgHeader),
1079 sizeofW(StgStableName)-sizeofW(StgHeader), 0);
1080 CCS_ALLOC(CCCS,sizeofW(StgStableName)); /* ccs prof */
1082 index = RET_STGCALL1(StgWord,lookupStableName,R1.p);
1084 /* Is there already a StableName for this heap object? */
1085 if (stable_ptr_table[index].sn_obj == NULL) {
1086 sn_obj = (StgStableName *) (Hp - sizeofW(StgStableName) + 1);
1087 SET_HDR(sn_obj,&stg_STABLE_NAME_info,CCCS);
1089 stable_ptr_table[index].sn_obj = (StgClosure *)sn_obj;
1091 (StgClosure *)sn_obj = stable_ptr_table[index].sn_obj;
1094 TICK_RET_UNBOXED_TUP(1);
1098 /* -----------------------------------------------------------------------------
1099 Bytecode object primitives
1100 ------------------------------------------------------------------------- */
1112 HP_CHK_GEN_TICKY(sizeofW(StgBCO),R1_PTR|R2_PTR|R3_PTR|R4_PTR, newBCOzh_fast,);
1113 TICK_ALLOC_PRIM(sizeofW(StgHeader), sizeofW(StgBCO)-sizeofW(StgHeader), 0);
1114 CCS_ALLOC(CCCS,sizeofW(StgBCO)); /* ccs prof */
1115 bco = (StgBCO *) (Hp + 1 - sizeofW(StgBCO));
1116 SET_HDR(bco, &stg_BCO_info, CCCS);
1118 bco->instrs = (StgArrWords*)R1.cl;
1119 bco->literals = (StgArrWords*)R2.cl;
1120 bco->ptrs = (StgMutArrPtrs*)R3.cl;
1121 bco->itbls = (StgArrWords*)R4.cl;
1123 TICK_RET_UNBOXED_TUP(1);
1128 FN_(mkApUpd0zh_fast)
1130 /* R1.p = the fn for the AP_UPD
1134 HP_CHK_GEN_TICKY(AP_sizeW(0), R1_PTR, mkApUpd0zh_fast,);
1135 TICK_ALLOC_PRIM(sizeofW(StgHeader), AP_sizeW(0)-sizeofW(StgHeader), 0);
1136 CCS_ALLOC(CCCS,AP_sizeW(0)); /* ccs prof */
1137 ap = (StgAP_UPD *) (Hp + 1 - AP_sizeW(0));
1138 SET_HDR(ap, &stg_AP_UPD_info, CCCS);
1143 TICK_RET_UNBOXED_TUP(1);
1148 /* -----------------------------------------------------------------------------
1149 Thread I/O blocking primitives
1150 -------------------------------------------------------------------------- */
1152 FN_(waitReadzh_fast)
1156 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1157 CurrentTSO->why_blocked = BlockedOnRead;
1158 CurrentTSO->block_info.fd = R1.i;
1159 ACQUIRE_LOCK(&sched_mutex);
1160 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1161 RELEASE_LOCK(&sched_mutex);
1162 JMP_(stg_block_noregs);
1166 FN_(waitWritezh_fast)
1170 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1171 CurrentTSO->why_blocked = BlockedOnWrite;
1172 CurrentTSO->block_info.fd = R1.i;
1173 ACQUIRE_LOCK(&sched_mutex);
1174 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1175 RELEASE_LOCK(&sched_mutex);
1176 JMP_(stg_block_noregs);
1186 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1187 CurrentTSO->why_blocked = BlockedOnDelay;
1189 ACQUIRE_LOCK(&sched_mutex);
1191 target = (R1.i / (TICK_MILLISECS*1000)) + getourtimeofday();
1192 CurrentTSO->block_info.target = target;
1194 /* Insert the new thread in the sleeping queue. */
1197 while (t != END_TSO_QUEUE && t->block_info.target < target) {
1202 CurrentTSO->link = t;
1204 sleeping_queue = CurrentTSO;
1206 prev->link = CurrentTSO;
1209 RELEASE_LOCK(&sched_mutex);
1210 JMP_(stg_block_noregs);