1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.85 2001/11/22 14:25:12 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);
425 // A weak pointer is inherently used, so we do not need to call
426 // LDV_recordDead_FILL_SLOP_DYNAMIC():
427 // LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)w);
428 // or, LDV_recordDead():
429 // LDV_recordDead((StgClosure *)w, sizeofW(StgWeak) - sizeofW(StgProfHeader));
430 // Furthermore, when PROFILING is turned on, dead weak pointers are exactly as
431 // large as weak pointers, so there is no need to fill the slop, either.
432 // See stg_DEAD_WEAK_info in StgMiscClosures.hc.
435 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
437 w->header.info = &stg_DEAD_WEAK_info;
440 LDV_recordCreate((StgClosure *)w);
442 f = ((StgWeak *)w)->finalizer;
443 w->link = ((StgWeak *)w)->link;
445 /* return the finalizer */
446 if (f == &stg_NO_FINALIZER_closure) {
447 RET_NP(0,&stg_NO_FINALIZER_closure);
454 /* -----------------------------------------------------------------------------
455 Arbitrary-precision Integer operations.
456 -------------------------------------------------------------------------- */
458 FN_(int2Integerzh_fast)
460 /* arguments: R1 = Int# */
462 I_ val, s; /* to avoid aliasing */
463 StgArrWords* p; /* address of array result */
467 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, int2Integerzh_fast,);
468 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
469 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
471 p = (StgArrWords *)Hp - 1;
472 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
474 /* mpz_set_si is inlined here, makes things simpler */
478 } else if (val > 0) {
485 /* returns (# size :: Int#,
489 TICK_RET_UNBOXED_TUP(2);
494 FN_(word2Integerzh_fast)
496 /* arguments: R1 = Word# */
498 W_ val; /* to avoid aliasing */
500 StgArrWords* p; /* address of array result */
504 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, word2Integerzh_fast,)
505 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
506 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
508 p = (StgArrWords *)Hp - 1;
509 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
518 /* returns (# size :: Int#,
522 TICK_RET_UNBOXED_TUP(2);
529 * 'long long' primops for converting to/from Integers.
532 #ifdef SUPPORT_LONG_LONGS
534 FN_(int64ToIntegerzh_fast)
536 /* arguments: L1 = Int64# */
538 StgInt64 val; /* to avoid aliasing */
540 I_ s, neg, words_needed;
541 StgArrWords* p; /* address of array result */
547 if ( val >= 0x100000000LL || val <= -0x100000000LL ) {
550 /* minimum is one word */
553 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, int64ToIntegerzh_fast,)
554 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
555 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
557 p = (StgArrWords *)(Hp-words_needed+1) - 1;
558 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
565 hi = (W_)((LW_)val / 0x100000000ULL);
567 if ( words_needed == 2 ) {
571 } else if ( val != 0 ) {
574 } else /* val==0 */ {
577 s = ( neg ? -s : s );
579 /* returns (# size :: Int#,
583 TICK_RET_UNBOXED_TUP(2);
588 FN_(word64ToIntegerzh_fast)
590 /* arguments: L1 = Word64# */
592 StgWord64 val; /* to avoid aliasing */
595 StgArrWords* p; /* address of array result */
599 if ( val >= 0x100000000ULL ) {
604 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, word64ToIntegerzh_fast,)
605 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
606 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
608 p = (StgArrWords *)(Hp-words_needed+1) - 1;
609 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
611 hi = (W_)((LW_)val / 0x100000000ULL);
612 if ( val >= 0x100000000ULL ) {
616 } else if ( val != 0 ) {
619 } else /* val==0 */ {
623 /* returns (# size :: Int#,
627 TICK_RET_UNBOXED_TUP(2);
633 #endif /* SUPPORT_LONG_LONGS */
635 /* ToDo: this is shockingly inefficient */
637 #define GMP_TAKE2_RET1(name,mp_fun) \
640 MP_INT arg1, arg2, result; \
646 /* call doYouWantToGC() */ \
647 MAYBE_GC(R2_PTR | R4_PTR, name); \
649 d1 = (StgArrWords *)R2.p; \
651 d2 = (StgArrWords *)R4.p; \
654 arg1._mp_alloc = d1->words; \
655 arg1._mp_size = (s1); \
656 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
657 arg2._mp_alloc = d2->words; \
658 arg2._mp_size = (s2); \
659 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
661 STGCALL1(mpz_init,&result); \
663 /* Perform the operation */ \
664 STGCALL3(mp_fun,&result,&arg1,&arg2); \
666 TICK_RET_UNBOXED_TUP(2); \
667 RET_NP(result._mp_size, \
668 result._mp_d-sizeofW(StgArrWords)); \
672 #define GMP_TAKE1_RET1(name,mp_fun) \
675 MP_INT arg1, result; \
680 /* call doYouWantToGC() */ \
681 MAYBE_GC(R2_PTR, name); \
683 d1 = (StgArrWords *)R2.p; \
686 arg1._mp_alloc = d1->words; \
687 arg1._mp_size = (s1); \
688 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
690 STGCALL1(mpz_init,&result); \
692 /* Perform the operation */ \
693 STGCALL2(mp_fun,&result,&arg1); \
695 TICK_RET_UNBOXED_TUP(2); \
696 RET_NP(result._mp_size, \
697 result._mp_d-sizeofW(StgArrWords)); \
701 #define GMP_TAKE2_RET2(name,mp_fun) \
704 MP_INT arg1, arg2, result1, result2; \
710 /* call doYouWantToGC() */ \
711 MAYBE_GC(R2_PTR | R4_PTR, name); \
713 d1 = (StgArrWords *)R2.p; \
715 d2 = (StgArrWords *)R4.p; \
718 arg1._mp_alloc = d1->words; \
719 arg1._mp_size = (s1); \
720 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
721 arg2._mp_alloc = d2->words; \
722 arg2._mp_size = (s2); \
723 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
725 STGCALL1(mpz_init,&result1); \
726 STGCALL1(mpz_init,&result2); \
728 /* Perform the operation */ \
729 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
731 TICK_RET_UNBOXED_TUP(4); \
732 RET_NPNP(result1._mp_size, \
733 result1._mp_d-sizeofW(StgArrWords), \
735 result2._mp_d-sizeofW(StgArrWords)); \
739 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add);
740 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub);
741 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul);
742 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd);
743 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q);
744 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r);
745 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact);
746 GMP_TAKE2_RET1(andIntegerzh_fast, mpz_and);
747 GMP_TAKE2_RET1(orIntegerzh_fast, mpz_ior);
748 GMP_TAKE2_RET1(xorIntegerzh_fast, mpz_xor);
749 GMP_TAKE1_RET1(complementIntegerzh_fast, mpz_com);
751 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr);
752 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr);
754 FN_(decodeFloatzh_fast)
762 /* arguments: F1 = Float# */
765 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, decodeFloatzh_fast,);
766 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
767 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
769 /* Be prepared to tell Lennart-coded __decodeFloat */
770 /* where mantissa._mp_d can be put (it does not care about the rest) */
771 p = (StgArrWords *)Hp - 1;
772 SET_ARR_HDR(p,&stg_ARR_WORDS_info,CCCS,1)
773 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
775 /* Perform the operation */
776 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
778 /* returns: (Int# (expn), Int#, ByteArray#) */
779 TICK_RET_UNBOXED_TUP(3);
780 RET_NNP(exponent,mantissa._mp_size,p);
784 #define DOUBLE_MANTISSA_SIZE (sizeofW(StgDouble))
785 #define ARR_SIZE (sizeofW(StgArrWords) + DOUBLE_MANTISSA_SIZE)
787 FN_(decodeDoublezh_fast)
794 /* arguments: D1 = Double# */
797 HP_CHK_GEN_TICKY(ARR_SIZE, NO_PTRS, decodeDoublezh_fast,);
798 TICK_ALLOC_PRIM(sizeofW(StgArrWords),DOUBLE_MANTISSA_SIZE,0);
799 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
801 /* Be prepared to tell Lennart-coded __decodeDouble */
802 /* where mantissa.d can be put (it does not care about the rest) */
803 p = (StgArrWords *)(Hp-ARR_SIZE+1);
804 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
805 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
807 /* Perform the operation */
808 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
810 /* returns: (Int# (expn), Int#, ByteArray#) */
811 TICK_RET_UNBOXED_TUP(3);
812 RET_NNP(exponent,mantissa._mp_size,p);
816 /* -----------------------------------------------------------------------------
817 * Concurrency primitives
818 * -------------------------------------------------------------------------- */
823 /* args: R1 = closure to spark */
825 MAYBE_GC(R1_PTR, forkzh_fast);
827 /* create it right now, return ThreadID in R1 */
828 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
829 RtsFlags.GcFlags.initialStkSize, R1.cl);
830 STGCALL1(scheduleThread, R1.t);
832 /* switch at the earliest opportunity */
835 JMP_(ENTRY_CODE(Sp[0]));
842 JMP_(stg_yield_noregs);
846 /* -----------------------------------------------------------------------------
849 * take & putMVar work as follows. Firstly, an important invariant:
851 * If the MVar is full, then the blocking queue contains only
852 * threads blocked on putMVar, and if the MVar is empty then the
853 * blocking queue contains only threads blocked on takeMVar.
856 * MVar empty : then add ourselves to the blocking queue
857 * MVar full : remove the value from the MVar, and
858 * blocking queue empty : return
859 * blocking queue non-empty : perform the first blocked putMVar
860 * from the queue, and wake up the
861 * thread (MVar is now full again)
863 * putMVar is just the dual of the above algorithm.
865 * How do we "perform a putMVar"? Well, we have to fiddle around with
866 * the stack of the thread waiting to do the putMVar. See
867 * stg_block_putmvar and stg_block_takemvar in HeapStackCheck.c for
868 * the stack layout, and the PerformPut and PerformTake macros below.
870 * It is important that a blocked take or put is woken up with the
871 * take/put already performed, because otherwise there would be a
872 * small window of vulnerability where the thread could receive an
873 * exception and never perform its take or put, and we'd end up with a
876 * -------------------------------------------------------------------------- */
885 HP_CHK_GEN_TICKY(sizeofW(StgMVar), NO_PTRS, newMVarzh_fast,);
886 TICK_ALLOC_PRIM(sizeofW(StgMutVar)-1, // consider head,tail,link as admin wds
888 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
890 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
891 SET_HDR(mvar,&stg_EMPTY_MVAR_info,CCCS);
892 mvar->head = mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
893 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
895 TICK_RET_UNBOXED_TUP(1);
900 #define PerformTake(tso, value) ({ \
901 (tso)->sp[1] = (W_)value; \
902 (tso)->sp[0] = (W_)&stg_gc_unpt_r1_ret_info; \
905 #define PerformPut(tso) ({ \
906 StgClosure *val = (StgClosure *)(tso)->sp[2]; \
907 (tso)->sp[2] = (W_)&stg_gc_noregs_ret_info; \
916 const StgInfoTable *info;
919 /* args: R1 = MVar closure */
921 mvar = (StgMVar *)R1.p;
924 info = LOCK_CLOSURE(mvar);
926 info = GET_INFO(mvar);
929 /* If the MVar is empty, put ourselves on its blocking queue,
930 * and wait until we're woken up.
932 if (info == &stg_EMPTY_MVAR_info) {
933 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
934 mvar->head = CurrentTSO;
936 mvar->tail->link = CurrentTSO;
938 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
939 CurrentTSO->why_blocked = BlockedOnMVar;
940 CurrentTSO->block_info.closure = (StgClosure *)mvar;
941 mvar->tail = CurrentTSO;
944 /* unlock the MVar */
945 mvar->header.info = &stg_EMPTY_MVAR_info;
947 JMP_(stg_block_takemvar);
950 /* we got the value... */
953 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
954 /* There are putMVar(s) waiting...
955 * wake up the first thread on the queue
957 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
959 /* actually perform the putMVar for the thread that we just woke up */
960 mvar->value = PerformPut(mvar->head);
962 #if defined(GRAN) || defined(PAR)
963 /* ToDo: check 2nd arg (mvar) is right */
964 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
966 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
968 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
969 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
972 /* unlock in the SMP case */
973 SET_INFO(mvar,&stg_FULL_MVAR_info);
975 TICK_RET_UNBOXED_TUP(1);
978 /* No further putMVars, MVar is now empty */
980 /* do this last... we might have locked the MVar in the SMP case,
981 * and writing the info pointer will unlock it.
983 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
984 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
985 TICK_RET_UNBOXED_TUP(1);
991 FN_(tryTakeMVarzh_fast)
995 const StgInfoTable *info;
998 /* args: R1 = MVar closure */
1000 mvar = (StgMVar *)R1.p;
1003 info = LOCK_CLOSURE(mvar);
1005 info = GET_INFO(mvar);
1008 if (info == &stg_EMPTY_MVAR_info) {
1011 /* unlock the MVar */
1012 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1015 /* HACK: we need a pointer to pass back,
1016 * so we abuse NO_FINALIZER_closure
1018 RET_NP(0, &stg_NO_FINALIZER_closure);
1021 /* we got the value... */
1024 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1025 /* There are putMVar(s) waiting...
1026 * wake up the first thread on the queue
1028 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1030 /* actually perform the putMVar for the thread that we just woke up */
1031 mvar->value = PerformPut(mvar->head);
1033 #if defined(GRAN) || defined(PAR)
1034 /* ToDo: check 2nd arg (mvar) is right */
1035 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1037 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1039 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1040 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1043 /* unlock in the SMP case */
1044 SET_INFO(mvar,&stg_FULL_MVAR_info);
1046 TICK_RET_UNBOXED_TUP(1);
1049 /* No further putMVars, MVar is now empty */
1051 /* do this last... we might have locked the MVar in the SMP case,
1052 * and writing the info pointer will unlock it.
1054 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1055 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1056 TICK_RET_UNBOXED_TUP(1);
1065 const StgInfoTable *info;
1068 /* args: R1 = MVar, R2 = value */
1070 mvar = (StgMVar *)R1.p;
1073 info = LOCK_CLOSURE(mvar);
1075 info = GET_INFO(mvar);
1078 if (info == &stg_FULL_MVAR_info) {
1079 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1080 mvar->head = CurrentTSO;
1082 mvar->tail->link = CurrentTSO;
1084 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1085 CurrentTSO->why_blocked = BlockedOnMVar;
1086 CurrentTSO->block_info.closure = (StgClosure *)mvar;
1087 mvar->tail = CurrentTSO;
1090 /* unlock the MVar */
1091 SET_INFO(mvar,&stg_FULL_MVAR_info);
1093 JMP_(stg_block_putmvar);
1096 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1097 /* There are takeMVar(s) waiting: wake up the first one
1099 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1101 /* actually perform the takeMVar */
1102 PerformTake(mvar->head, R2.cl);
1104 #if defined(GRAN) || defined(PAR)
1105 /* ToDo: check 2nd arg (mvar) is right */
1106 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1108 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1110 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1111 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1114 /* unlocks the MVar in the SMP case */
1115 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1117 JMP_(ENTRY_CODE(Sp[0]));
1119 /* No further takes, the MVar is now full. */
1120 mvar->value = R2.cl;
1121 /* unlocks the MVar in the SMP case */
1122 SET_INFO(mvar,&stg_FULL_MVAR_info);
1123 JMP_(ENTRY_CODE(Sp[0]));
1126 /* ToDo: yield afterward for better communication performance? */
1130 FN_(tryPutMVarzh_fast)
1133 const StgInfoTable *info;
1136 /* args: R1 = MVar, R2 = value */
1138 mvar = (StgMVar *)R1.p;
1141 info = LOCK_CLOSURE(mvar);
1143 info = GET_INFO(mvar);
1146 if (info == &stg_FULL_MVAR_info) {
1149 /* unlock the MVar */
1150 mvar->header.info = &stg_FULL_MVAR_info;
1156 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1157 /* There are takeMVar(s) waiting: wake up the first one
1159 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1161 /* actually perform the takeMVar */
1162 PerformTake(mvar->head, R2.cl);
1164 #if defined(GRAN) || defined(PAR)
1165 /* ToDo: check 2nd arg (mvar) is right */
1166 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1168 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1170 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1171 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1174 /* unlocks the MVar in the SMP case */
1175 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1177 JMP_(ENTRY_CODE(Sp[0]));
1179 /* No further takes, the MVar is now full. */
1180 mvar->value = R2.cl;
1181 /* unlocks the MVar in the SMP case */
1182 SET_INFO(mvar,&stg_FULL_MVAR_info);
1183 JMP_(ENTRY_CODE(Sp[0]));
1186 /* ToDo: yield afterward for better communication performance? */
1190 /* -----------------------------------------------------------------------------
1191 Stable pointer primitives
1192 ------------------------------------------------------------------------- */
1194 FN_(makeStableNamezh_fast)
1197 StgStableName *sn_obj;
1200 HP_CHK_GEN_TICKY(sizeofW(StgStableName), R1_PTR, makeStableNamezh_fast,);
1201 TICK_ALLOC_PRIM(sizeofW(StgHeader),
1202 sizeofW(StgStableName)-sizeofW(StgHeader), 0);
1203 CCS_ALLOC(CCCS,sizeofW(StgStableName)); /* ccs prof */
1205 index = RET_STGCALL1(StgWord,lookupStableName,R1.p);
1207 /* Is there already a StableName for this heap object? */
1208 if (stable_ptr_table[index].sn_obj == NULL) {
1209 sn_obj = (StgStableName *) (Hp - sizeofW(StgStableName) + 1);
1210 SET_HDR(sn_obj,&stg_STABLE_NAME_info,CCCS);
1212 stable_ptr_table[index].sn_obj = (StgClosure *)sn_obj;
1214 (StgClosure *)sn_obj = stable_ptr_table[index].sn_obj;
1217 TICK_RET_UNBOXED_TUP(1);
1221 /* -----------------------------------------------------------------------------
1222 Bytecode object primitives
1223 ------------------------------------------------------------------------- */
1235 HP_CHK_GEN_TICKY(sizeofW(StgBCO),R1_PTR|R2_PTR|R3_PTR|R4_PTR, newBCOzh_fast,);
1236 TICK_ALLOC_PRIM(sizeofW(StgHeader), sizeofW(StgBCO)-sizeofW(StgHeader), 0);
1237 CCS_ALLOC(CCCS,sizeofW(StgBCO)); /* ccs prof */
1238 bco = (StgBCO *) (Hp + 1 - sizeofW(StgBCO));
1239 SET_HDR(bco, &stg_BCO_info, CCCS);
1241 bco->instrs = (StgArrWords*)R1.cl;
1242 bco->literals = (StgArrWords*)R2.cl;
1243 bco->ptrs = (StgMutArrPtrs*)R3.cl;
1244 bco->itbls = (StgArrWords*)R4.cl;
1246 TICK_RET_UNBOXED_TUP(1);
1251 FN_(mkApUpd0zh_fast)
1253 /* R1.p = the fn for the AP_UPD
1257 HP_CHK_GEN_TICKY(AP_sizeW(0), R1_PTR, mkApUpd0zh_fast,);
1258 TICK_ALLOC_PRIM(sizeofW(StgHeader), AP_sizeW(0)-sizeofW(StgHeader), 0);
1259 CCS_ALLOC(CCCS,AP_sizeW(0)); /* ccs prof */
1260 ap = (StgAP_UPD *) (Hp + 1 - AP_sizeW(0));
1261 SET_HDR(ap, &stg_AP_UPD_info, CCCS);
1266 TICK_RET_UNBOXED_TUP(1);
1271 /* -----------------------------------------------------------------------------
1272 Thread I/O blocking primitives
1273 -------------------------------------------------------------------------- */
1275 FN_(waitReadzh_fast)
1279 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1280 CurrentTSO->why_blocked = BlockedOnRead;
1281 CurrentTSO->block_info.fd = R1.i;
1282 ACQUIRE_LOCK(&sched_mutex);
1283 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1284 RELEASE_LOCK(&sched_mutex);
1285 JMP_(stg_block_noregs);
1289 FN_(waitWritezh_fast)
1293 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1294 CurrentTSO->why_blocked = BlockedOnWrite;
1295 CurrentTSO->block_info.fd = R1.i;
1296 ACQUIRE_LOCK(&sched_mutex);
1297 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1298 RELEASE_LOCK(&sched_mutex);
1299 JMP_(stg_block_noregs);
1309 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1310 CurrentTSO->why_blocked = BlockedOnDelay;
1312 ACQUIRE_LOCK(&sched_mutex);
1314 target = (R1.i / (TICK_MILLISECS*1000)) + getourtimeofday();
1315 CurrentTSO->block_info.target = target;
1317 /* Insert the new thread in the sleeping queue. */
1320 while (t != END_TSO_QUEUE && t->block_info.target < target) {
1325 CurrentTSO->link = t;
1327 sleeping_queue = CurrentTSO;
1329 prev->link = CurrentTSO;
1332 RELEASE_LOCK(&sched_mutex);
1333 JMP_(stg_block_noregs);