1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.83 2001/08/08 10:50:37 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) \
266 FN_(newPinnedByteArrayzh_fast) \
268 W_ size, stuff_size, n; \
271 MAYBE_GC(NO_PTRS,newPinnedByteArrayzh_fast); \
273 stuff_size = BYTES_TO_STGWORDS(n); \
274 size = sizeofW(StgArrWords)+ stuff_size; \
275 p = (StgArrWords *)RET_STGCALL1(P_,allocatePinned,size); \
276 TICK_ALLOC_PRIM(sizeofW(StgArrWords),stuff_size,0); \
277 SET_HDR(p, &stg_ARR_WORDS_info, CCCS); \
278 p->words = stuff_size; \
279 TICK_RET_UNBOXED_TUP(1) \
292 MAYBE_GC(R2_PTR,newArrayzh_fast);
294 size = sizeofW(StgMutArrPtrs) + n;
295 arr = (StgMutArrPtrs *)RET_STGCALL1(P_, allocate, size);
296 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
298 SET_HDR(arr,&stg_MUT_ARR_PTRS_info,CCCS);
302 for (p = (P_)arr + sizeofW(StgMutArrPtrs);
303 p < (P_)arr + size; p++) {
307 TICK_RET_UNBOXED_TUP(1);
312 FN_(newMutVarzh_fast)
315 /* Args: R1.p = initialisation value */
318 HP_CHK_GEN_TICKY(sizeofW(StgMutVar), R1_PTR, newMutVarzh_fast,);
319 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1,1, 0); /* hack, dependent on rep. */
320 CCS_ALLOC(CCCS,sizeofW(StgMutVar));
322 mv = (StgMutVar *)(Hp-sizeofW(StgMutVar)+1);
323 SET_HDR(mv,&stg_MUT_VAR_info,CCCS);
326 TICK_RET_UNBOXED_TUP(1);
331 /* -----------------------------------------------------------------------------
332 Foreign Object Primitives
334 -------------------------------------------------------------------------- */
336 FN_(mkForeignObjzh_fast)
338 /* R1.p = ptr to foreign object,
340 StgForeignObj *result;
343 HP_CHK_GEN_TICKY(sizeofW(StgForeignObj), NO_PTRS, mkForeignObjzh_fast,);
344 TICK_ALLOC_PRIM(sizeofW(StgHeader),
345 sizeofW(StgForeignObj)-sizeofW(StgHeader), 0);
346 CCS_ALLOC(CCCS,sizeofW(StgForeignObj)); /* ccs prof */
348 result = (StgForeignObj *) (Hp + 1 - sizeofW(StgForeignObj));
349 SET_HDR(result,&stg_FOREIGN_info,CCCS);
352 /* returns (# s#, ForeignObj# #) */
353 TICK_RET_UNBOXED_TUP(1);
358 /* These two are out-of-line for the benefit of the NCG */
359 FN_(unsafeThawArrayzh_fast)
362 SET_INFO((StgClosure *)R1.cl,&stg_MUT_ARR_PTRS_info);
363 recordMutable((StgMutClosure*)R1.cl);
365 TICK_RET_UNBOXED_TUP(1);
370 /* -----------------------------------------------------------------------------
371 Weak Pointer Primitives
372 -------------------------------------------------------------------------- */
378 R3.p = finalizer (or NULL)
384 R3.cl = &stg_NO_FINALIZER_closure;
387 HP_CHK_GEN_TICKY(sizeofW(StgWeak),R1_PTR|R2_PTR|R3_PTR, mkWeakzh_fast,);
388 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1, // +1 is for the link field
389 sizeofW(StgWeak)-sizeofW(StgHeader)-1, 0);
390 CCS_ALLOC(CCCS,sizeofW(StgWeak)); /* ccs prof */
392 w = (StgWeak *) (Hp + 1 - sizeofW(StgWeak));
393 SET_HDR(w, &stg_WEAK_info, CCCS);
397 w->finalizer = R3.cl;
399 w->link = weak_ptr_list;
401 IF_DEBUG(weak, fprintf(stderr,"New weak pointer at %p\n",w));
403 TICK_RET_UNBOXED_TUP(1);
408 FN_(finalizzeWeakzh_fast)
415 TICK_RET_UNBOXED_TUP(0);
416 w = (StgDeadWeak *)R1.p;
419 if (w->header.info == &stg_DEAD_WEAK_info) {
420 RET_NP(0,&stg_NO_FINALIZER_closure);
424 w->header.info = &stg_DEAD_WEAK_info;
425 f = ((StgWeak *)w)->finalizer;
426 w->link = ((StgWeak *)w)->link;
428 /* return the finalizer */
429 if (f == &stg_NO_FINALIZER_closure) {
430 RET_NP(0,&stg_NO_FINALIZER_closure);
437 /* -----------------------------------------------------------------------------
438 Arbitrary-precision Integer operations.
439 -------------------------------------------------------------------------- */
441 FN_(int2Integerzh_fast)
443 /* arguments: R1 = Int# */
445 I_ val, s; /* to avoid aliasing */
446 StgArrWords* p; /* address of array result */
450 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, int2Integerzh_fast,);
451 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
452 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
454 p = (StgArrWords *)Hp - 1;
455 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
457 /* mpz_set_si is inlined here, makes things simpler */
461 } else if (val > 0) {
468 /* returns (# size :: Int#,
472 TICK_RET_UNBOXED_TUP(2);
477 FN_(word2Integerzh_fast)
479 /* arguments: R1 = Word# */
481 W_ val; /* to avoid aliasing */
483 StgArrWords* p; /* address of array result */
487 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, word2Integerzh_fast,)
488 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
489 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
491 p = (StgArrWords *)Hp - 1;
492 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
501 /* returns (# size :: Int#,
505 TICK_RET_UNBOXED_TUP(2);
512 * 'long long' primops for converting to/from Integers.
515 #ifdef SUPPORT_LONG_LONGS
517 FN_(int64ToIntegerzh_fast)
519 /* arguments: L1 = Int64# */
521 StgInt64 val; /* to avoid aliasing */
523 I_ s, neg, words_needed;
524 StgArrWords* p; /* address of array result */
530 if ( val >= 0x100000000LL || val <= -0x100000000LL ) {
533 /* minimum is one word */
536 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, int64ToIntegerzh_fast,)
537 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
538 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
540 p = (StgArrWords *)(Hp-words_needed+1) - 1;
541 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
548 hi = (W_)((LW_)val / 0x100000000ULL);
550 if ( words_needed == 2 ) {
554 } else if ( val != 0 ) {
557 } else /* val==0 */ {
560 s = ( neg ? -s : s );
562 /* returns (# size :: Int#,
566 TICK_RET_UNBOXED_TUP(2);
571 FN_(word64ToIntegerzh_fast)
573 /* arguments: L1 = Word64# */
575 StgWord64 val; /* to avoid aliasing */
578 StgArrWords* p; /* address of array result */
582 if ( val >= 0x100000000ULL ) {
587 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, word64ToIntegerzh_fast,)
588 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
589 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
591 p = (StgArrWords *)(Hp-words_needed+1) - 1;
592 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
594 hi = (W_)((LW_)val / 0x100000000ULL);
595 if ( val >= 0x100000000ULL ) {
599 } else if ( val != 0 ) {
602 } else /* val==0 */ {
606 /* returns (# size :: Int#,
610 TICK_RET_UNBOXED_TUP(2);
616 #endif /* SUPPORT_LONG_LONGS */
618 /* ToDo: this is shockingly inefficient */
620 #define GMP_TAKE2_RET1(name,mp_fun) \
623 MP_INT arg1, arg2, result; \
629 /* call doYouWantToGC() */ \
630 MAYBE_GC(R2_PTR | R4_PTR, name); \
632 d1 = (StgArrWords *)R2.p; \
634 d2 = (StgArrWords *)R4.p; \
637 arg1._mp_alloc = d1->words; \
638 arg1._mp_size = (s1); \
639 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
640 arg2._mp_alloc = d2->words; \
641 arg2._mp_size = (s2); \
642 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
644 STGCALL1(mpz_init,&result); \
646 /* Perform the operation */ \
647 STGCALL3(mp_fun,&result,&arg1,&arg2); \
649 TICK_RET_UNBOXED_TUP(2); \
650 RET_NP(result._mp_size, \
651 result._mp_d-sizeofW(StgArrWords)); \
655 #define GMP_TAKE1_RET1(name,mp_fun) \
658 MP_INT arg1, result; \
663 /* call doYouWantToGC() */ \
664 MAYBE_GC(R2_PTR, name); \
666 d1 = (StgArrWords *)R2.p; \
669 arg1._mp_alloc = d1->words; \
670 arg1._mp_size = (s1); \
671 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
673 STGCALL1(mpz_init,&result); \
675 /* Perform the operation */ \
676 STGCALL2(mp_fun,&result,&arg1); \
678 TICK_RET_UNBOXED_TUP(2); \
679 RET_NP(result._mp_size, \
680 result._mp_d-sizeofW(StgArrWords)); \
684 #define GMP_TAKE2_RET2(name,mp_fun) \
687 MP_INT arg1, arg2, result1, result2; \
693 /* call doYouWantToGC() */ \
694 MAYBE_GC(R2_PTR | R4_PTR, name); \
696 d1 = (StgArrWords *)R2.p; \
698 d2 = (StgArrWords *)R4.p; \
701 arg1._mp_alloc = d1->words; \
702 arg1._mp_size = (s1); \
703 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
704 arg2._mp_alloc = d2->words; \
705 arg2._mp_size = (s2); \
706 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
708 STGCALL1(mpz_init,&result1); \
709 STGCALL1(mpz_init,&result2); \
711 /* Perform the operation */ \
712 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
714 TICK_RET_UNBOXED_TUP(4); \
715 RET_NPNP(result1._mp_size, \
716 result1._mp_d-sizeofW(StgArrWords), \
718 result2._mp_d-sizeofW(StgArrWords)); \
722 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add);
723 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub);
724 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul);
725 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd);
726 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q);
727 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r);
728 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact);
729 GMP_TAKE2_RET1(andIntegerzh_fast, mpz_and);
730 GMP_TAKE2_RET1(orIntegerzh_fast, mpz_ior);
731 GMP_TAKE2_RET1(xorIntegerzh_fast, mpz_xor);
732 GMP_TAKE1_RET1(complementIntegerzh_fast, mpz_com);
734 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr);
735 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr);
737 FN_(decodeFloatzh_fast)
745 /* arguments: F1 = Float# */
748 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, decodeFloatzh_fast,);
749 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
750 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
752 /* Be prepared to tell Lennart-coded __decodeFloat */
753 /* where mantissa._mp_d can be put (it does not care about the rest) */
754 p = (StgArrWords *)Hp - 1;
755 SET_ARR_HDR(p,&stg_ARR_WORDS_info,CCCS,1)
756 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
758 /* Perform the operation */
759 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
761 /* returns: (Int# (expn), Int#, ByteArray#) */
762 TICK_RET_UNBOXED_TUP(3);
763 RET_NNP(exponent,mantissa._mp_size,p);
767 #define DOUBLE_MANTISSA_SIZE (sizeofW(StgDouble))
768 #define ARR_SIZE (sizeofW(StgArrWords) + DOUBLE_MANTISSA_SIZE)
770 FN_(decodeDoublezh_fast)
777 /* arguments: D1 = Double# */
780 HP_CHK_GEN_TICKY(ARR_SIZE, NO_PTRS, decodeDoublezh_fast,);
781 TICK_ALLOC_PRIM(sizeofW(StgArrWords),DOUBLE_MANTISSA_SIZE,0);
782 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
784 /* Be prepared to tell Lennart-coded __decodeDouble */
785 /* where mantissa.d can be put (it does not care about the rest) */
786 p = (StgArrWords *)(Hp-ARR_SIZE+1);
787 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
788 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
790 /* Perform the operation */
791 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
793 /* returns: (Int# (expn), Int#, ByteArray#) */
794 TICK_RET_UNBOXED_TUP(3);
795 RET_NNP(exponent,mantissa._mp_size,p);
799 /* -----------------------------------------------------------------------------
800 * Concurrency primitives
801 * -------------------------------------------------------------------------- */
806 /* args: R1 = closure to spark */
808 MAYBE_GC(R1_PTR, forkzh_fast);
810 /* create it right now, return ThreadID in R1 */
811 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
812 RtsFlags.GcFlags.initialStkSize, R1.cl);
813 STGCALL1(scheduleThread, R1.t);
815 /* switch at the earliest opportunity */
818 JMP_(ENTRY_CODE(Sp[0]));
825 JMP_(stg_yield_noregs);
829 /* -----------------------------------------------------------------------------
832 * take & putMVar work as follows. Firstly, an important invariant:
834 * If the MVar is full, then the blocking queue contains only
835 * threads blocked on putMVar, and if the MVar is empty then the
836 * blocking queue contains only threads blocked on takeMVar.
839 * MVar empty : then add ourselves to the blocking queue
840 * MVar full : remove the value from the MVar, and
841 * blocking queue empty : return
842 * blocking queue non-empty : perform the first blocked putMVar
843 * from the queue, and wake up the
844 * thread (MVar is now full again)
846 * putMVar is just the dual of the above algorithm.
848 * How do we "perform a putMVar"? Well, we have to fiddle around with
849 * the stack of the thread waiting to do the putMVar. See
850 * stg_block_putmvar and stg_block_takemvar in HeapStackCheck.c for
851 * the stack layout, and the PerformPut and PerformTake macros below.
853 * It is important that a blocked take or put is woken up with the
854 * take/put already performed, because otherwise there would be a
855 * small window of vulnerability where the thread could receive an
856 * exception and never perform its take or put, and we'd end up with a
859 * -------------------------------------------------------------------------- */
868 HP_CHK_GEN_TICKY(sizeofW(StgMVar), NO_PTRS, newMVarzh_fast,);
869 TICK_ALLOC_PRIM(sizeofW(StgMutVar)-1, // consider head,tail,link as admin wds
871 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
873 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
874 SET_HDR(mvar,&stg_EMPTY_MVAR_info,CCCS);
875 mvar->head = mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
876 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
878 TICK_RET_UNBOXED_TUP(1);
883 #define PerformTake(tso, value) ({ \
884 (tso)->sp[1] = (W_)value; \
885 (tso)->sp[0] = (W_)&stg_gc_unpt_r1_ret_info; \
888 #define PerformPut(tso) ({ \
889 StgClosure *val = (StgClosure *)(tso)->sp[2]; \
890 (tso)->sp[2] = (W_)&stg_gc_noregs_ret_info; \
899 const StgInfoTable *info;
902 /* args: R1 = MVar closure */
904 mvar = (StgMVar *)R1.p;
907 info = LOCK_CLOSURE(mvar);
909 info = GET_INFO(mvar);
912 /* If the MVar is empty, put ourselves on its blocking queue,
913 * and wait until we're woken up.
915 if (info == &stg_EMPTY_MVAR_info) {
916 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
917 mvar->head = CurrentTSO;
919 mvar->tail->link = CurrentTSO;
921 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
922 CurrentTSO->why_blocked = BlockedOnMVar;
923 CurrentTSO->block_info.closure = (StgClosure *)mvar;
924 mvar->tail = CurrentTSO;
927 /* unlock the MVar */
928 mvar->header.info = &stg_EMPTY_MVAR_info;
930 JMP_(stg_block_takemvar);
933 /* we got the value... */
936 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
937 /* There are putMVar(s) waiting...
938 * wake up the first thread on the queue
940 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
942 /* actually perform the putMVar for the thread that we just woke up */
943 mvar->value = PerformPut(mvar->head);
945 #if defined(GRAN) || defined(PAR)
946 /* ToDo: check 2nd arg (mvar) is right */
947 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
949 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
951 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
952 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
955 /* unlock in the SMP case */
956 SET_INFO(mvar,&stg_FULL_MVAR_info);
958 TICK_RET_UNBOXED_TUP(1);
961 /* No further putMVars, MVar is now empty */
963 /* do this last... we might have locked the MVar in the SMP case,
964 * and writing the info pointer will unlock it.
966 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
967 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
968 TICK_RET_UNBOXED_TUP(1);
974 FN_(tryTakeMVarzh_fast)
978 const StgInfoTable *info;
981 /* args: R1 = MVar closure */
983 mvar = (StgMVar *)R1.p;
986 info = LOCK_CLOSURE(mvar);
988 info = GET_INFO(mvar);
991 if (info == &stg_EMPTY_MVAR_info) {
994 /* unlock the MVar */
995 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
998 /* HACK: we need a pointer to pass back,
999 * so we abuse NO_FINALIZER_closure
1001 RET_NP(0, &stg_NO_FINALIZER_closure);
1004 /* we got the value... */
1007 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1008 /* There are putMVar(s) waiting...
1009 * wake up the first thread on the queue
1011 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1013 /* actually perform the putMVar for the thread that we just woke up */
1014 mvar->value = PerformPut(mvar->head);
1016 #if defined(GRAN) || defined(PAR)
1017 /* ToDo: check 2nd arg (mvar) is right */
1018 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1020 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1022 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1023 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1026 /* unlock in the SMP case */
1027 SET_INFO(mvar,&stg_FULL_MVAR_info);
1029 TICK_RET_UNBOXED_TUP(1);
1032 /* No further putMVars, MVar is now empty */
1034 /* do this last... we might have locked the MVar in the SMP case,
1035 * and writing the info pointer will unlock it.
1037 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1038 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1039 TICK_RET_UNBOXED_TUP(1);
1048 const StgInfoTable *info;
1051 /* args: R1 = MVar, R2 = value */
1053 mvar = (StgMVar *)R1.p;
1056 info = LOCK_CLOSURE(mvar);
1058 info = GET_INFO(mvar);
1061 if (info == &stg_FULL_MVAR_info) {
1062 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1063 mvar->head = CurrentTSO;
1065 mvar->tail->link = CurrentTSO;
1067 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1068 CurrentTSO->why_blocked = BlockedOnMVar;
1069 CurrentTSO->block_info.closure = (StgClosure *)mvar;
1070 mvar->tail = CurrentTSO;
1073 /* unlock the MVar */
1074 SET_INFO(mvar,&stg_FULL_MVAR_info);
1076 JMP_(stg_block_putmvar);
1079 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1080 /* There are takeMVar(s) waiting: wake up the first one
1082 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1084 /* actually perform the takeMVar */
1085 PerformTake(mvar->head, R2.cl);
1087 #if defined(GRAN) || defined(PAR)
1088 /* ToDo: check 2nd arg (mvar) is right */
1089 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1091 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1093 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1094 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1097 /* unlocks the MVar in the SMP case */
1098 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1100 JMP_(ENTRY_CODE(Sp[0]));
1102 /* No further takes, the MVar is now full. */
1103 mvar->value = R2.cl;
1104 /* unlocks the MVar in the SMP case */
1105 SET_INFO(mvar,&stg_FULL_MVAR_info);
1106 JMP_(ENTRY_CODE(Sp[0]));
1109 /* ToDo: yield afterward for better communication performance? */
1113 FN_(tryPutMVarzh_fast)
1116 const StgInfoTable *info;
1119 /* args: R1 = MVar, R2 = value */
1121 mvar = (StgMVar *)R1.p;
1124 info = LOCK_CLOSURE(mvar);
1126 info = GET_INFO(mvar);
1129 if (info == &stg_FULL_MVAR_info) {
1132 /* unlock the MVar */
1133 mvar->header.info = &stg_FULL_MVAR_info;
1139 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1140 /* There are takeMVar(s) waiting: wake up the first one
1142 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1144 /* actually perform the takeMVar */
1145 PerformTake(mvar->head, R2.cl);
1147 #if defined(GRAN) || defined(PAR)
1148 /* ToDo: check 2nd arg (mvar) is right */
1149 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1151 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1153 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1154 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1157 /* unlocks the MVar in the SMP case */
1158 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1160 JMP_(ENTRY_CODE(Sp[0]));
1162 /* No further takes, the MVar is now full. */
1163 mvar->value = R2.cl;
1164 /* unlocks the MVar in the SMP case */
1165 SET_INFO(mvar,&stg_FULL_MVAR_info);
1166 JMP_(ENTRY_CODE(Sp[0]));
1169 /* ToDo: yield afterward for better communication performance? */
1173 /* -----------------------------------------------------------------------------
1174 Stable pointer primitives
1175 ------------------------------------------------------------------------- */
1177 FN_(makeStableNamezh_fast)
1180 StgStableName *sn_obj;
1183 HP_CHK_GEN_TICKY(sizeofW(StgStableName), R1_PTR, makeStableNamezh_fast,);
1184 TICK_ALLOC_PRIM(sizeofW(StgHeader),
1185 sizeofW(StgStableName)-sizeofW(StgHeader), 0);
1186 CCS_ALLOC(CCCS,sizeofW(StgStableName)); /* ccs prof */
1188 index = RET_STGCALL1(StgWord,lookupStableName,R1.p);
1190 /* Is there already a StableName for this heap object? */
1191 if (stable_ptr_table[index].sn_obj == NULL) {
1192 sn_obj = (StgStableName *) (Hp - sizeofW(StgStableName) + 1);
1193 SET_HDR(sn_obj,&stg_STABLE_NAME_info,CCCS);
1195 stable_ptr_table[index].sn_obj = (StgClosure *)sn_obj;
1197 (StgClosure *)sn_obj = stable_ptr_table[index].sn_obj;
1200 TICK_RET_UNBOXED_TUP(1);
1204 /* -----------------------------------------------------------------------------
1205 Bytecode object primitives
1206 ------------------------------------------------------------------------- */
1218 HP_CHK_GEN_TICKY(sizeofW(StgBCO),R1_PTR|R2_PTR|R3_PTR|R4_PTR, newBCOzh_fast,);
1219 TICK_ALLOC_PRIM(sizeofW(StgHeader), sizeofW(StgBCO)-sizeofW(StgHeader), 0);
1220 CCS_ALLOC(CCCS,sizeofW(StgBCO)); /* ccs prof */
1221 bco = (StgBCO *) (Hp + 1 - sizeofW(StgBCO));
1222 SET_HDR(bco, &stg_BCO_info, CCCS);
1224 bco->instrs = (StgArrWords*)R1.cl;
1225 bco->literals = (StgArrWords*)R2.cl;
1226 bco->ptrs = (StgMutArrPtrs*)R3.cl;
1227 bco->itbls = (StgArrWords*)R4.cl;
1229 TICK_RET_UNBOXED_TUP(1);
1234 FN_(mkApUpd0zh_fast)
1236 /* R1.p = the fn for the AP_UPD
1240 HP_CHK_GEN_TICKY(AP_sizeW(0), R1_PTR, mkApUpd0zh_fast,);
1241 TICK_ALLOC_PRIM(sizeofW(StgHeader), AP_sizeW(0)-sizeofW(StgHeader), 0);
1242 CCS_ALLOC(CCCS,AP_sizeW(0)); /* ccs prof */
1243 ap = (StgAP_UPD *) (Hp + 1 - AP_sizeW(0));
1244 SET_HDR(ap, &stg_AP_UPD_info, CCCS);
1249 TICK_RET_UNBOXED_TUP(1);
1254 /* -----------------------------------------------------------------------------
1255 Thread I/O blocking primitives
1256 -------------------------------------------------------------------------- */
1258 FN_(waitReadzh_fast)
1262 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1263 CurrentTSO->why_blocked = BlockedOnRead;
1264 CurrentTSO->block_info.fd = R1.i;
1265 ACQUIRE_LOCK(&sched_mutex);
1266 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1267 RELEASE_LOCK(&sched_mutex);
1268 JMP_(stg_block_noregs);
1272 FN_(waitWritezh_fast)
1276 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1277 CurrentTSO->why_blocked = BlockedOnWrite;
1278 CurrentTSO->block_info.fd = R1.i;
1279 ACQUIRE_LOCK(&sched_mutex);
1280 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1281 RELEASE_LOCK(&sched_mutex);
1282 JMP_(stg_block_noregs);
1292 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1293 CurrentTSO->why_blocked = BlockedOnDelay;
1295 ACQUIRE_LOCK(&sched_mutex);
1297 target = (R1.i / (TICK_MILLISECS*1000)) + getourtimeofday();
1298 CurrentTSO->block_info.target = target;
1300 /* Insert the new thread in the sleeping queue. */
1303 while (t != END_TSO_QUEUE && t->block_info.target < target) {
1308 CurrentTSO->link = t;
1310 sleeping_queue = CurrentTSO;
1312 prev->link = CurrentTSO;
1315 RELEASE_LOCK(&sched_mutex);
1316 JMP_(stg_block_noregs);