1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.84 2001/11/08 12:46:31 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"
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
30 for these. Some C compilers can't cope with zero-length static arrays,
31 so we have to make these one element long.
34 StgWord GHC_ZCCCallable_static_info[1];
35 StgWord GHC_ZCCReturnable_static_info[1];
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 FN_(newByteArrayzh_fast) \
249 W_ size, stuff_size, n; \
252 MAYBE_GC(NO_PTRS,newByteArrayzh_fast); \
254 stuff_size = BYTES_TO_STGWORDS(n); \
255 size = sizeofW(StgArrWords)+ stuff_size; \
256 p = (StgArrWords *)RET_STGCALL1(P_,allocate,size); \
257 TICK_ALLOC_PRIM(sizeofW(StgArrWords),stuff_size,0); \
258 SET_HDR(p, &stg_ARR_WORDS_info, CCCS); \
259 p->words = stuff_size; \
260 TICK_RET_UNBOXED_TUP(1) \
265 FN_(newPinnedByteArrayzh_fast) \
267 W_ size, stuff_size, n; \
270 MAYBE_GC(NO_PTRS,newPinnedByteArrayzh_fast); \
272 stuff_size = BYTES_TO_STGWORDS(n); \
273 size = sizeofW(StgArrWords)+ stuff_size; \
274 p = (StgArrWords *)RET_STGCALL1(P_,allocatePinned,size); \
275 TICK_ALLOC_PRIM(sizeofW(StgArrWords),stuff_size,0); \
276 SET_HDR(p, &stg_ARR_WORDS_info, CCCS); \
277 p->words = stuff_size; \
278 TICK_RET_UNBOXED_TUP(1) \
291 MAYBE_GC(R2_PTR,newArrayzh_fast);
293 size = sizeofW(StgMutArrPtrs) + n;
294 arr = (StgMutArrPtrs *)RET_STGCALL1(P_, allocate, size);
295 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
297 SET_HDR(arr,&stg_MUT_ARR_PTRS_info,CCCS);
301 for (p = (P_)arr + sizeofW(StgMutArrPtrs);
302 p < (P_)arr + size; p++) {
306 TICK_RET_UNBOXED_TUP(1);
311 FN_(newMutVarzh_fast)
314 /* Args: R1.p = initialisation value */
317 HP_CHK_GEN_TICKY(sizeofW(StgMutVar), R1_PTR, newMutVarzh_fast,);
318 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1,1, 0); /* hack, dependent on rep. */
319 CCS_ALLOC(CCCS,sizeofW(StgMutVar));
321 mv = (StgMutVar *)(Hp-sizeofW(StgMutVar)+1);
322 SET_HDR(mv,&stg_MUT_VAR_info,CCCS);
325 TICK_RET_UNBOXED_TUP(1);
330 /* -----------------------------------------------------------------------------
331 Foreign Object Primitives
333 -------------------------------------------------------------------------- */
335 FN_(mkForeignObjzh_fast)
337 /* R1.p = ptr to foreign object,
339 StgForeignObj *result;
342 HP_CHK_GEN_TICKY(sizeofW(StgForeignObj), NO_PTRS, mkForeignObjzh_fast,);
343 TICK_ALLOC_PRIM(sizeofW(StgHeader),
344 sizeofW(StgForeignObj)-sizeofW(StgHeader), 0);
345 CCS_ALLOC(CCCS,sizeofW(StgForeignObj)); /* ccs prof */
347 result = (StgForeignObj *) (Hp + 1 - sizeofW(StgForeignObj));
348 SET_HDR(result,&stg_FOREIGN_info,CCCS);
351 /* returns (# s#, ForeignObj# #) */
352 TICK_RET_UNBOXED_TUP(1);
357 /* These two are out-of-line for the benefit of the NCG */
358 FN_(unsafeThawArrayzh_fast)
361 SET_INFO((StgClosure *)R1.cl,&stg_MUT_ARR_PTRS_info);
362 recordMutable((StgMutClosure*)R1.cl);
364 TICK_RET_UNBOXED_TUP(1);
369 /* -----------------------------------------------------------------------------
370 Weak Pointer Primitives
371 -------------------------------------------------------------------------- */
377 R3.p = finalizer (or NULL)
383 R3.cl = &stg_NO_FINALIZER_closure;
386 HP_CHK_GEN_TICKY(sizeofW(StgWeak),R1_PTR|R2_PTR|R3_PTR, mkWeakzh_fast,);
387 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1, // +1 is for the link field
388 sizeofW(StgWeak)-sizeofW(StgHeader)-1, 0);
389 CCS_ALLOC(CCCS,sizeofW(StgWeak)); /* ccs prof */
391 w = (StgWeak *) (Hp + 1 - sizeofW(StgWeak));
392 SET_HDR(w, &stg_WEAK_info, CCCS);
396 w->finalizer = R3.cl;
398 w->link = weak_ptr_list;
400 IF_DEBUG(weak, fprintf(stderr,"New weak pointer at %p\n",w));
402 TICK_RET_UNBOXED_TUP(1);
407 FN_(finalizzeWeakzh_fast)
414 TICK_RET_UNBOXED_TUP(0);
415 w = (StgDeadWeak *)R1.p;
418 if (w->header.info == &stg_DEAD_WEAK_info) {
419 RET_NP(0,&stg_NO_FINALIZER_closure);
423 w->header.info = &stg_DEAD_WEAK_info;
424 f = ((StgWeak *)w)->finalizer;
425 w->link = ((StgWeak *)w)->link;
427 /* return the finalizer */
428 if (f == &stg_NO_FINALIZER_closure) {
429 RET_NP(0,&stg_NO_FINALIZER_closure);
436 /* -----------------------------------------------------------------------------
437 Arbitrary-precision Integer operations.
438 -------------------------------------------------------------------------- */
440 FN_(int2Integerzh_fast)
442 /* arguments: R1 = Int# */
444 I_ val, s; /* to avoid aliasing */
445 StgArrWords* p; /* address of array result */
449 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, int2Integerzh_fast,);
450 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
451 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
453 p = (StgArrWords *)Hp - 1;
454 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
456 /* mpz_set_si is inlined here, makes things simpler */
460 } else if (val > 0) {
467 /* returns (# size :: Int#,
471 TICK_RET_UNBOXED_TUP(2);
476 FN_(word2Integerzh_fast)
478 /* arguments: R1 = Word# */
480 W_ val; /* to avoid aliasing */
482 StgArrWords* p; /* address of array result */
486 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, word2Integerzh_fast,)
487 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
488 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
490 p = (StgArrWords *)Hp - 1;
491 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
500 /* returns (# size :: Int#,
504 TICK_RET_UNBOXED_TUP(2);
511 * 'long long' primops for converting to/from Integers.
514 #ifdef SUPPORT_LONG_LONGS
516 FN_(int64ToIntegerzh_fast)
518 /* arguments: L1 = Int64# */
520 StgInt64 val; /* to avoid aliasing */
522 I_ s, neg, words_needed;
523 StgArrWords* p; /* address of array result */
529 if ( val >= 0x100000000LL || val <= -0x100000000LL ) {
532 /* minimum is one word */
535 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, int64ToIntegerzh_fast,)
536 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
537 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
539 p = (StgArrWords *)(Hp-words_needed+1) - 1;
540 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
547 hi = (W_)((LW_)val / 0x100000000ULL);
549 if ( words_needed == 2 ) {
553 } else if ( val != 0 ) {
556 } else /* val==0 */ {
559 s = ( neg ? -s : s );
561 /* returns (# size :: Int#,
565 TICK_RET_UNBOXED_TUP(2);
570 FN_(word64ToIntegerzh_fast)
572 /* arguments: L1 = Word64# */
574 StgWord64 val; /* to avoid aliasing */
577 StgArrWords* p; /* address of array result */
581 if ( val >= 0x100000000ULL ) {
586 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, word64ToIntegerzh_fast,)
587 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
588 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
590 p = (StgArrWords *)(Hp-words_needed+1) - 1;
591 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
593 hi = (W_)((LW_)val / 0x100000000ULL);
594 if ( val >= 0x100000000ULL ) {
598 } else if ( val != 0 ) {
601 } else /* val==0 */ {
605 /* returns (# size :: Int#,
609 TICK_RET_UNBOXED_TUP(2);
615 #endif /* SUPPORT_LONG_LONGS */
617 /* ToDo: this is shockingly inefficient */
619 #define GMP_TAKE2_RET1(name,mp_fun) \
622 MP_INT arg1, arg2, result; \
628 /* call doYouWantToGC() */ \
629 MAYBE_GC(R2_PTR | R4_PTR, name); \
631 d1 = (StgArrWords *)R2.p; \
633 d2 = (StgArrWords *)R4.p; \
636 arg1._mp_alloc = d1->words; \
637 arg1._mp_size = (s1); \
638 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
639 arg2._mp_alloc = d2->words; \
640 arg2._mp_size = (s2); \
641 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
643 STGCALL1(mpz_init,&result); \
645 /* Perform the operation */ \
646 STGCALL3(mp_fun,&result,&arg1,&arg2); \
648 TICK_RET_UNBOXED_TUP(2); \
649 RET_NP(result._mp_size, \
650 result._mp_d-sizeofW(StgArrWords)); \
654 #define GMP_TAKE1_RET1(name,mp_fun) \
657 MP_INT arg1, result; \
662 /* call doYouWantToGC() */ \
663 MAYBE_GC(R2_PTR, name); \
665 d1 = (StgArrWords *)R2.p; \
668 arg1._mp_alloc = d1->words; \
669 arg1._mp_size = (s1); \
670 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
672 STGCALL1(mpz_init,&result); \
674 /* Perform the operation */ \
675 STGCALL2(mp_fun,&result,&arg1); \
677 TICK_RET_UNBOXED_TUP(2); \
678 RET_NP(result._mp_size, \
679 result._mp_d-sizeofW(StgArrWords)); \
683 #define GMP_TAKE2_RET2(name,mp_fun) \
686 MP_INT arg1, arg2, result1, result2; \
692 /* call doYouWantToGC() */ \
693 MAYBE_GC(R2_PTR | R4_PTR, name); \
695 d1 = (StgArrWords *)R2.p; \
697 d2 = (StgArrWords *)R4.p; \
700 arg1._mp_alloc = d1->words; \
701 arg1._mp_size = (s1); \
702 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
703 arg2._mp_alloc = d2->words; \
704 arg2._mp_size = (s2); \
705 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
707 STGCALL1(mpz_init,&result1); \
708 STGCALL1(mpz_init,&result2); \
710 /* Perform the operation */ \
711 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
713 TICK_RET_UNBOXED_TUP(4); \
714 RET_NPNP(result1._mp_size, \
715 result1._mp_d-sizeofW(StgArrWords), \
717 result2._mp_d-sizeofW(StgArrWords)); \
721 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add);
722 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub);
723 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul);
724 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd);
725 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q);
726 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r);
727 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact);
728 GMP_TAKE2_RET1(andIntegerzh_fast, mpz_and);
729 GMP_TAKE2_RET1(orIntegerzh_fast, mpz_ior);
730 GMP_TAKE2_RET1(xorIntegerzh_fast, mpz_xor);
731 GMP_TAKE1_RET1(complementIntegerzh_fast, mpz_com);
733 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr);
734 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr);
736 FN_(decodeFloatzh_fast)
744 /* arguments: F1 = Float# */
747 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, decodeFloatzh_fast,);
748 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
749 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
751 /* Be prepared to tell Lennart-coded __decodeFloat */
752 /* where mantissa._mp_d can be put (it does not care about the rest) */
753 p = (StgArrWords *)Hp - 1;
754 SET_ARR_HDR(p,&stg_ARR_WORDS_info,CCCS,1)
755 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
757 /* Perform the operation */
758 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
760 /* returns: (Int# (expn), Int#, ByteArray#) */
761 TICK_RET_UNBOXED_TUP(3);
762 RET_NNP(exponent,mantissa._mp_size,p);
766 #define DOUBLE_MANTISSA_SIZE (sizeofW(StgDouble))
767 #define ARR_SIZE (sizeofW(StgArrWords) + DOUBLE_MANTISSA_SIZE)
769 FN_(decodeDoublezh_fast)
776 /* arguments: D1 = Double# */
779 HP_CHK_GEN_TICKY(ARR_SIZE, NO_PTRS, decodeDoublezh_fast,);
780 TICK_ALLOC_PRIM(sizeofW(StgArrWords),DOUBLE_MANTISSA_SIZE,0);
781 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
783 /* Be prepared to tell Lennart-coded __decodeDouble */
784 /* where mantissa.d can be put (it does not care about the rest) */
785 p = (StgArrWords *)(Hp-ARR_SIZE+1);
786 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
787 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
789 /* Perform the operation */
790 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
792 /* returns: (Int# (expn), Int#, ByteArray#) */
793 TICK_RET_UNBOXED_TUP(3);
794 RET_NNP(exponent,mantissa._mp_size,p);
798 /* -----------------------------------------------------------------------------
799 * Concurrency primitives
800 * -------------------------------------------------------------------------- */
805 /* args: R1 = closure to spark */
807 MAYBE_GC(R1_PTR, forkzh_fast);
809 /* create it right now, return ThreadID in R1 */
810 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
811 RtsFlags.GcFlags.initialStkSize, R1.cl);
812 STGCALL1(scheduleThread, R1.t);
814 /* switch at the earliest opportunity */
817 JMP_(ENTRY_CODE(Sp[0]));
824 JMP_(stg_yield_noregs);
828 /* -----------------------------------------------------------------------------
831 * take & putMVar work as follows. Firstly, an important invariant:
833 * If the MVar is full, then the blocking queue contains only
834 * threads blocked on putMVar, and if the MVar is empty then the
835 * blocking queue contains only threads blocked on takeMVar.
838 * MVar empty : then add ourselves to the blocking queue
839 * MVar full : remove the value from the MVar, and
840 * blocking queue empty : return
841 * blocking queue non-empty : perform the first blocked putMVar
842 * from the queue, and wake up the
843 * thread (MVar is now full again)
845 * putMVar is just the dual of the above algorithm.
847 * How do we "perform a putMVar"? Well, we have to fiddle around with
848 * the stack of the thread waiting to do the putMVar. See
849 * stg_block_putmvar and stg_block_takemvar in HeapStackCheck.c for
850 * the stack layout, and the PerformPut and PerformTake macros below.
852 * It is important that a blocked take or put is woken up with the
853 * take/put already performed, because otherwise there would be a
854 * small window of vulnerability where the thread could receive an
855 * exception and never perform its take or put, and we'd end up with a
858 * -------------------------------------------------------------------------- */
867 HP_CHK_GEN_TICKY(sizeofW(StgMVar), NO_PTRS, newMVarzh_fast,);
868 TICK_ALLOC_PRIM(sizeofW(StgMutVar)-1, // consider head,tail,link as admin wds
870 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
872 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
873 SET_HDR(mvar,&stg_EMPTY_MVAR_info,CCCS);
874 mvar->head = mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
875 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
877 TICK_RET_UNBOXED_TUP(1);
882 #define PerformTake(tso, value) ({ \
883 (tso)->sp[1] = (W_)value; \
884 (tso)->sp[0] = (W_)&stg_gc_unpt_r1_ret_info; \
887 #define PerformPut(tso) ({ \
888 StgClosure *val = (StgClosure *)(tso)->sp[2]; \
889 (tso)->sp[2] = (W_)&stg_gc_noregs_ret_info; \
898 const StgInfoTable *info;
901 /* args: R1 = MVar closure */
903 mvar = (StgMVar *)R1.p;
906 info = LOCK_CLOSURE(mvar);
908 info = GET_INFO(mvar);
911 /* If the MVar is empty, put ourselves on its blocking queue,
912 * and wait until we're woken up.
914 if (info == &stg_EMPTY_MVAR_info) {
915 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
916 mvar->head = CurrentTSO;
918 mvar->tail->link = CurrentTSO;
920 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
921 CurrentTSO->why_blocked = BlockedOnMVar;
922 CurrentTSO->block_info.closure = (StgClosure *)mvar;
923 mvar->tail = CurrentTSO;
926 /* unlock the MVar */
927 mvar->header.info = &stg_EMPTY_MVAR_info;
929 JMP_(stg_block_takemvar);
932 /* we got the value... */
935 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
936 /* There are putMVar(s) waiting...
937 * wake up the first thread on the queue
939 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
941 /* actually perform the putMVar for the thread that we just woke up */
942 mvar->value = PerformPut(mvar->head);
944 #if defined(GRAN) || defined(PAR)
945 /* ToDo: check 2nd arg (mvar) is right */
946 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
948 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
950 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
951 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
954 /* unlock in the SMP case */
955 SET_INFO(mvar,&stg_FULL_MVAR_info);
957 TICK_RET_UNBOXED_TUP(1);
960 /* No further putMVars, MVar is now empty */
962 /* do this last... we might have locked the MVar in the SMP case,
963 * and writing the info pointer will unlock it.
965 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
966 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
967 TICK_RET_UNBOXED_TUP(1);
973 FN_(tryTakeMVarzh_fast)
977 const StgInfoTable *info;
980 /* args: R1 = MVar closure */
982 mvar = (StgMVar *)R1.p;
985 info = LOCK_CLOSURE(mvar);
987 info = GET_INFO(mvar);
990 if (info == &stg_EMPTY_MVAR_info) {
993 /* unlock the MVar */
994 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
997 /* HACK: we need a pointer to pass back,
998 * so we abuse NO_FINALIZER_closure
1000 RET_NP(0, &stg_NO_FINALIZER_closure);
1003 /* we got the value... */
1006 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1007 /* There are putMVar(s) waiting...
1008 * wake up the first thread on the queue
1010 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1012 /* actually perform the putMVar for the thread that we just woke up */
1013 mvar->value = PerformPut(mvar->head);
1015 #if defined(GRAN) || defined(PAR)
1016 /* ToDo: check 2nd arg (mvar) is right */
1017 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1019 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1021 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1022 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1025 /* unlock in the SMP case */
1026 SET_INFO(mvar,&stg_FULL_MVAR_info);
1028 TICK_RET_UNBOXED_TUP(1);
1031 /* No further putMVars, MVar is now empty */
1033 /* do this last... we might have locked the MVar in the SMP case,
1034 * and writing the info pointer will unlock it.
1036 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1037 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1038 TICK_RET_UNBOXED_TUP(1);
1047 const StgInfoTable *info;
1050 /* args: R1 = MVar, R2 = value */
1052 mvar = (StgMVar *)R1.p;
1055 info = LOCK_CLOSURE(mvar);
1057 info = GET_INFO(mvar);
1060 if (info == &stg_FULL_MVAR_info) {
1061 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1062 mvar->head = CurrentTSO;
1064 mvar->tail->link = CurrentTSO;
1066 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1067 CurrentTSO->why_blocked = BlockedOnMVar;
1068 CurrentTSO->block_info.closure = (StgClosure *)mvar;
1069 mvar->tail = CurrentTSO;
1072 /* unlock the MVar */
1073 SET_INFO(mvar,&stg_FULL_MVAR_info);
1075 JMP_(stg_block_putmvar);
1078 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1079 /* There are takeMVar(s) waiting: wake up the first one
1081 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1083 /* actually perform the takeMVar */
1084 PerformTake(mvar->head, R2.cl);
1086 #if defined(GRAN) || defined(PAR)
1087 /* ToDo: check 2nd arg (mvar) is right */
1088 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1090 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1092 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1093 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1096 /* unlocks the MVar in the SMP case */
1097 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1099 JMP_(ENTRY_CODE(Sp[0]));
1101 /* No further takes, the MVar is now full. */
1102 mvar->value = R2.cl;
1103 /* unlocks the MVar in the SMP case */
1104 SET_INFO(mvar,&stg_FULL_MVAR_info);
1105 JMP_(ENTRY_CODE(Sp[0]));
1108 /* ToDo: yield afterward for better communication performance? */
1112 FN_(tryPutMVarzh_fast)
1115 const StgInfoTable *info;
1118 /* args: R1 = MVar, R2 = value */
1120 mvar = (StgMVar *)R1.p;
1123 info = LOCK_CLOSURE(mvar);
1125 info = GET_INFO(mvar);
1128 if (info == &stg_FULL_MVAR_info) {
1131 /* unlock the MVar */
1132 mvar->header.info = &stg_FULL_MVAR_info;
1138 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1139 /* There are takeMVar(s) waiting: wake up the first one
1141 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1143 /* actually perform the takeMVar */
1144 PerformTake(mvar->head, R2.cl);
1146 #if defined(GRAN) || defined(PAR)
1147 /* ToDo: check 2nd arg (mvar) is right */
1148 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1150 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1152 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1153 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1156 /* unlocks the MVar in the SMP case */
1157 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1159 JMP_(ENTRY_CODE(Sp[0]));
1161 /* No further takes, the MVar is now full. */
1162 mvar->value = R2.cl;
1163 /* unlocks the MVar in the SMP case */
1164 SET_INFO(mvar,&stg_FULL_MVAR_info);
1165 JMP_(ENTRY_CODE(Sp[0]));
1168 /* ToDo: yield afterward for better communication performance? */
1172 /* -----------------------------------------------------------------------------
1173 Stable pointer primitives
1174 ------------------------------------------------------------------------- */
1176 FN_(makeStableNamezh_fast)
1179 StgStableName *sn_obj;
1182 HP_CHK_GEN_TICKY(sizeofW(StgStableName), R1_PTR, makeStableNamezh_fast,);
1183 TICK_ALLOC_PRIM(sizeofW(StgHeader),
1184 sizeofW(StgStableName)-sizeofW(StgHeader), 0);
1185 CCS_ALLOC(CCCS,sizeofW(StgStableName)); /* ccs prof */
1187 index = RET_STGCALL1(StgWord,lookupStableName,R1.p);
1189 /* Is there already a StableName for this heap object? */
1190 if (stable_ptr_table[index].sn_obj == NULL) {
1191 sn_obj = (StgStableName *) (Hp - sizeofW(StgStableName) + 1);
1192 SET_HDR(sn_obj,&stg_STABLE_NAME_info,CCCS);
1194 stable_ptr_table[index].sn_obj = (StgClosure *)sn_obj;
1196 (StgClosure *)sn_obj = stable_ptr_table[index].sn_obj;
1199 TICK_RET_UNBOXED_TUP(1);
1203 /* -----------------------------------------------------------------------------
1204 Bytecode object primitives
1205 ------------------------------------------------------------------------- */
1217 HP_CHK_GEN_TICKY(sizeofW(StgBCO),R1_PTR|R2_PTR|R3_PTR|R4_PTR, newBCOzh_fast,);
1218 TICK_ALLOC_PRIM(sizeofW(StgHeader), sizeofW(StgBCO)-sizeofW(StgHeader), 0);
1219 CCS_ALLOC(CCCS,sizeofW(StgBCO)); /* ccs prof */
1220 bco = (StgBCO *) (Hp + 1 - sizeofW(StgBCO));
1221 SET_HDR(bco, &stg_BCO_info, CCCS);
1223 bco->instrs = (StgArrWords*)R1.cl;
1224 bco->literals = (StgArrWords*)R2.cl;
1225 bco->ptrs = (StgMutArrPtrs*)R3.cl;
1226 bco->itbls = (StgArrWords*)R4.cl;
1228 TICK_RET_UNBOXED_TUP(1);
1233 FN_(mkApUpd0zh_fast)
1235 /* R1.p = the fn for the AP_UPD
1239 HP_CHK_GEN_TICKY(AP_sizeW(0), R1_PTR, mkApUpd0zh_fast,);
1240 TICK_ALLOC_PRIM(sizeofW(StgHeader), AP_sizeW(0)-sizeofW(StgHeader), 0);
1241 CCS_ALLOC(CCCS,AP_sizeW(0)); /* ccs prof */
1242 ap = (StgAP_UPD *) (Hp + 1 - AP_sizeW(0));
1243 SET_HDR(ap, &stg_AP_UPD_info, CCCS);
1248 TICK_RET_UNBOXED_TUP(1);
1253 /* -----------------------------------------------------------------------------
1254 Thread I/O blocking primitives
1255 -------------------------------------------------------------------------- */
1257 FN_(waitReadzh_fast)
1261 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1262 CurrentTSO->why_blocked = BlockedOnRead;
1263 CurrentTSO->block_info.fd = R1.i;
1264 ACQUIRE_LOCK(&sched_mutex);
1265 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1266 RELEASE_LOCK(&sched_mutex);
1267 JMP_(stg_block_noregs);
1271 FN_(waitWritezh_fast)
1275 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1276 CurrentTSO->why_blocked = BlockedOnWrite;
1277 CurrentTSO->block_info.fd = R1.i;
1278 ACQUIRE_LOCK(&sched_mutex);
1279 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1280 RELEASE_LOCK(&sched_mutex);
1281 JMP_(stg_block_noregs);
1291 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1292 CurrentTSO->why_blocked = BlockedOnDelay;
1294 ACQUIRE_LOCK(&sched_mutex);
1296 target = (R1.i / (TICK_MILLISECS*1000)) + getourtimeofday();
1297 CurrentTSO->block_info.target = target;
1299 /* Insert the new thread in the sleeping queue. */
1302 while (t != END_TSO_QUEUE && t->block_info.target < target) {
1307 CurrentTSO->link = t;
1309 sleeping_queue = CurrentTSO;
1311 prev->link = CurrentTSO;
1314 RELEASE_LOCK(&sched_mutex);
1315 JMP_(stg_block_noregs);