1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.99 2002/06/26 08:18:41 stolz 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);
274 // We want an 8-byte aligned array. allocatePinned() gives us
275 // 8-byte aligned memory by default, but we want to align the
276 // *goods* inside the ArrWords object, so we have to check the
277 // size of the ArrWords header and adjust our size accordingly.
278 size = sizeofW(StgArrWords)+ stuff_size;
279 if ((sizeof(StgArrWords) & 7) != 0) {
283 p = (StgArrWords *)RET_STGCALL1(P_,allocatePinned,size);
284 TICK_ALLOC_PRIM(sizeofW(StgArrWords),stuff_size,0);
286 // Again, if the ArrWords header isn't a multiple of 8 bytes, we
287 // have to push the object forward one word so that the goods
288 // fall on an 8-byte boundary.
289 if ((sizeof(StgArrWords) & 7) != 0) {
293 SET_HDR(p, &stg_ARR_WORDS_info, CCCS);
294 p->words = stuff_size;
295 TICK_RET_UNBOXED_TUP(1)
308 MAYBE_GC(R2_PTR,newArrayzh_fast);
310 size = sizeofW(StgMutArrPtrs) + n;
311 arr = (StgMutArrPtrs *)RET_STGCALL1(P_, allocate, size);
312 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
314 SET_HDR(arr,&stg_MUT_ARR_PTRS_info,CCCS);
318 for (p = (P_)arr + sizeofW(StgMutArrPtrs);
319 p < (P_)arr + size; p++) {
323 TICK_RET_UNBOXED_TUP(1);
328 FN_(newMutVarzh_fast)
331 /* Args: R1.p = initialisation value */
334 HP_CHK_GEN_TICKY(sizeofW(StgMutVar), R1_PTR, newMutVarzh_fast,);
335 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1,1, 0); /* hack, dependent on rep. */
336 CCS_ALLOC(CCCS,sizeofW(StgMutVar));
338 mv = (StgMutVar *)(Hp-sizeofW(StgMutVar)+1);
339 SET_HDR(mv,&stg_MUT_VAR_info,CCCS);
342 TICK_RET_UNBOXED_TUP(1);
347 /* -----------------------------------------------------------------------------
348 Foreign Object Primitives
349 -------------------------------------------------------------------------- */
351 FN_(mkForeignObjzh_fast)
353 /* R1.p = ptr to foreign object,
355 StgForeignObj *result;
358 HP_CHK_GEN_TICKY(sizeofW(StgForeignObj), NO_PTRS, mkForeignObjzh_fast,);
359 TICK_ALLOC_PRIM(sizeofW(StgHeader),
360 sizeofW(StgForeignObj)-sizeofW(StgHeader), 0);
361 CCS_ALLOC(CCCS,sizeofW(StgForeignObj)); /* ccs prof */
363 result = (StgForeignObj *) (Hp + 1 - sizeofW(StgForeignObj));
364 SET_HDR(result,&stg_FOREIGN_info,CCCS);
367 /* returns (# s#, ForeignObj# #) */
368 TICK_RET_UNBOXED_TUP(1);
373 /* These two are out-of-line for the benefit of the NCG */
374 FN_(unsafeThawArrayzh_fast)
377 SET_INFO((StgClosure *)R1.cl,&stg_MUT_ARR_PTRS_info);
378 recordMutable((StgMutClosure*)R1.cl);
380 TICK_RET_UNBOXED_TUP(1);
385 /* -----------------------------------------------------------------------------
386 Weak Pointer Primitives
387 -------------------------------------------------------------------------- */
393 R3.p = finalizer (or NULL)
399 R3.cl = &stg_NO_FINALIZER_closure;
402 HP_CHK_GEN_TICKY(sizeofW(StgWeak),R1_PTR|R2_PTR|R3_PTR, mkWeakzh_fast,);
403 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1, // +1 is for the link field
404 sizeofW(StgWeak)-sizeofW(StgHeader)-1, 0);
405 CCS_ALLOC(CCCS,sizeofW(StgWeak)); /* ccs prof */
407 w = (StgWeak *) (Hp + 1 - sizeofW(StgWeak));
408 SET_HDR(w, &stg_WEAK_info, CCCS);
412 w->finalizer = R3.cl;
414 w->link = weak_ptr_list;
416 IF_DEBUG(weak, fprintf(stderr,"New weak pointer at %p\n",w));
418 TICK_RET_UNBOXED_TUP(1);
423 FN_(finalizzeWeakzh_fast)
430 TICK_RET_UNBOXED_TUP(0);
431 w = (StgDeadWeak *)R1.p;
434 if (w->header.info == &stg_DEAD_WEAK_info) {
435 RET_NP(0,&stg_NO_FINALIZER_closure);
441 // A weak pointer is inherently used, so we do not need to call
442 // LDV_recordDead_FILL_SLOP_DYNAMIC():
443 // LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)w);
444 // or, LDV_recordDead():
445 // LDV_recordDead((StgClosure *)w, sizeofW(StgWeak) - sizeofW(StgProfHeader));
446 // Furthermore, when PROFILING is turned on, dead weak pointers are exactly as
447 // large as weak pointers, so there is no need to fill the slop, either.
448 // See stg_DEAD_WEAK_info in StgMiscClosures.hc.
451 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
453 w->header.info = &stg_DEAD_WEAK_info;
456 LDV_recordCreate((StgClosure *)w);
458 f = ((StgWeak *)w)->finalizer;
459 w->link = ((StgWeak *)w)->link;
461 /* return the finalizer */
462 if (f == &stg_NO_FINALIZER_closure) {
463 RET_NP(0,&stg_NO_FINALIZER_closure);
470 FN_(deRefWeakzh_fast)
472 /* R1.p = weak ptr */
478 if (w->header.info == &stg_WEAK_info) {
480 val = (P_)((StgWeak *)w)->value;
489 /* -----------------------------------------------------------------------------
490 Arbitrary-precision Integer operations.
491 -------------------------------------------------------------------------- */
493 FN_(int2Integerzh_fast)
495 /* arguments: R1 = Int# */
497 I_ val, s; /* to avoid aliasing */
498 StgArrWords* p; /* address of array result */
502 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, int2Integerzh_fast,);
503 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
504 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
506 p = (StgArrWords *)Hp - 1;
507 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
509 /* mpz_set_si is inlined here, makes things simpler */
513 } else if (val > 0) {
520 /* returns (# size :: Int#,
524 TICK_RET_UNBOXED_TUP(2);
529 FN_(word2Integerzh_fast)
531 /* arguments: R1 = Word# */
533 W_ val; /* to avoid aliasing */
535 StgArrWords* p; /* address of array result */
539 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, word2Integerzh_fast,)
540 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
541 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
543 p = (StgArrWords *)Hp - 1;
544 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
553 /* returns (# size :: Int#,
557 TICK_RET_UNBOXED_TUP(2);
564 * 'long long' primops for converting to/from Integers.
567 #ifdef SUPPORT_LONG_LONGS
569 FN_(int64ToIntegerzh_fast)
571 /* arguments: L1 = Int64# */
573 StgInt64 val; /* to avoid aliasing */
575 I_ s, neg, words_needed;
576 StgArrWords* p; /* address of array result */
582 if ( val >= 0x100000000LL || val <= -0x100000000LL ) {
585 /* minimum is one word */
588 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, int64ToIntegerzh_fast,)
589 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
590 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
592 p = (StgArrWords *)(Hp-words_needed+1) - 1;
593 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
600 hi = (W_)((LW_)val / 0x100000000ULL);
602 if ( words_needed == 2 ) {
606 } else if ( val != 0 ) {
609 } else /* val==0 */ {
612 s = ( neg ? -s : s );
614 /* returns (# size :: Int#,
618 TICK_RET_UNBOXED_TUP(2);
623 FN_(word64ToIntegerzh_fast)
625 /* arguments: L1 = Word64# */
627 StgWord64 val; /* to avoid aliasing */
630 StgArrWords* p; /* address of array result */
634 if ( val >= 0x100000000ULL ) {
639 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, word64ToIntegerzh_fast,)
640 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
641 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
643 p = (StgArrWords *)(Hp-words_needed+1) - 1;
644 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
646 hi = (W_)((LW_)val / 0x100000000ULL);
647 if ( val >= 0x100000000ULL ) {
651 } else if ( val != 0 ) {
654 } else /* val==0 */ {
658 /* returns (# size :: Int#,
662 TICK_RET_UNBOXED_TUP(2);
668 #endif /* SUPPORT_LONG_LONGS */
670 /* ToDo: this is shockingly inefficient */
672 #define GMP_TAKE2_RET1(name,mp_fun) \
675 MP_INT arg1, arg2, result; \
681 /* call doYouWantToGC() */ \
682 MAYBE_GC(R2_PTR | R4_PTR, name); \
684 d1 = (StgArrWords *)R2.p; \
686 d2 = (StgArrWords *)R4.p; \
689 arg1._mp_alloc = d1->words; \
690 arg1._mp_size = (s1); \
691 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
692 arg2._mp_alloc = d2->words; \
693 arg2._mp_size = (s2); \
694 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
696 STGCALL1(mpz_init,&result); \
698 /* Perform the operation */ \
699 STGCALL3(mp_fun,&result,&arg1,&arg2); \
701 TICK_RET_UNBOXED_TUP(2); \
702 RET_NP(result._mp_size, \
703 result._mp_d-sizeofW(StgArrWords)); \
707 #define GMP_TAKE1_RET1(name,mp_fun) \
710 MP_INT arg1, result; \
715 /* call doYouWantToGC() */ \
716 MAYBE_GC(R2_PTR, name); \
718 d1 = (StgArrWords *)R2.p; \
721 arg1._mp_alloc = d1->words; \
722 arg1._mp_size = (s1); \
723 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
725 STGCALL1(mpz_init,&result); \
727 /* Perform the operation */ \
728 STGCALL2(mp_fun,&result,&arg1); \
730 TICK_RET_UNBOXED_TUP(2); \
731 RET_NP(result._mp_size, \
732 result._mp_d-sizeofW(StgArrWords)); \
736 #define GMP_TAKE2_RET2(name,mp_fun) \
739 MP_INT arg1, arg2, result1, result2; \
745 /* call doYouWantToGC() */ \
746 MAYBE_GC(R2_PTR | R4_PTR, name); \
748 d1 = (StgArrWords *)R2.p; \
750 d2 = (StgArrWords *)R4.p; \
753 arg1._mp_alloc = d1->words; \
754 arg1._mp_size = (s1); \
755 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
756 arg2._mp_alloc = d2->words; \
757 arg2._mp_size = (s2); \
758 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
760 STGCALL1(mpz_init,&result1); \
761 STGCALL1(mpz_init,&result2); \
763 /* Perform the operation */ \
764 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
766 TICK_RET_UNBOXED_TUP(4); \
767 RET_NPNP(result1._mp_size, \
768 result1._mp_d-sizeofW(StgArrWords), \
770 result2._mp_d-sizeofW(StgArrWords)); \
774 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add);
775 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub);
776 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul);
777 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd);
778 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q);
779 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r);
780 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact);
781 GMP_TAKE2_RET1(andIntegerzh_fast, mpz_and);
782 GMP_TAKE2_RET1(orIntegerzh_fast, mpz_ior);
783 GMP_TAKE2_RET1(xorIntegerzh_fast, mpz_xor);
784 GMP_TAKE1_RET1(complementIntegerzh_fast, mpz_com);
786 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr);
787 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr);
792 /* R1 = the first Int#; R2 = the second Int# */
796 aa = (mp_limb_t)(R1.i);
797 r = RET_STGCALL3(StgInt, mpn_gcd_1, (mp_limb_t *)(&aa), 1, (mp_limb_t)(R2.i));
800 /* Result parked in R1, return via info-pointer at TOS */
801 JMP_(ENTRY_CODE(Sp[0]));
805 FN_(gcdIntegerIntzh_fast)
807 /* R1 = s1; R2 = d1; R3 = the int */
810 r = RET_STGCALL3(StgInt,mpn_gcd_1,(mp_limb_t *)(BYTE_ARR_CTS(R2.p)), R1.i, R3.i);
813 /* Result parked in R1, return via info-pointer at TOS */
814 JMP_(ENTRY_CODE(Sp[0]));
818 FN_(cmpIntegerIntzh_fast)
820 /* R1 = s1; R2 = d1; R3 = the int */
831 // paraphrased from mpz_cmp_si() in the GMP sources
834 } else if (v_digit < 0) {
839 if (usize != vsize) {
840 R1.i = usize - vsize; JMP_(ENTRY_CODE(Sp[0]));
844 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
847 u_digit = *(mp_limb_t *)(BYTE_ARR_CTS(R2.p));
849 if (u_digit == (mp_limb_t) (unsigned long) v_digit) {
850 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
853 if (u_digit > (mp_limb_t) (unsigned long) v_digit) {
859 JMP_(ENTRY_CODE(Sp[0]));
863 FN_(cmpIntegerzh_fast)
865 /* R1 = s1; R2 = d1; R3 = s2; R4 = d2 */
873 // paraphrased from mpz_cmp() in the GMP sources
877 if (usize != vsize) {
878 R1.i = usize - vsize; JMP_(ENTRY_CODE(Sp[0]));
882 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
887 up = BYTE_ARR_CTS(R2.p);
888 vp = BYTE_ARR_CTS(R4.p);
890 cmp = RET_STGCALL3(I_, mpn_cmp, (mp_limb_t *)up, (mp_limb_t *)vp, size);
893 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
896 if ((cmp < 0) == (usize < 0)) {
901 /* Result parked in R1, return via info-pointer at TOS */
902 JMP_(ENTRY_CODE(Sp[0]));
906 FN_(integer2Intzh_fast)
915 r = ((mp_limb_t *) (BYTE_ARR_CTS(R2.p)))[0];
918 /* Result parked in R1, return via info-pointer at TOS */
920 JMP_(ENTRY_CODE(Sp[0]));
924 FN_(integer2Wordzh_fast)
934 r = ((mp_limb_t *) (BYTE_ARR_CTS(R2.p)))[0];
937 /* Result parked in R1, return via info-pointer at TOS */
939 JMP_(ENTRY_CODE(Sp[0]));
944 FN_(decodeFloatzh_fast)
952 /* arguments: F1 = Float# */
955 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, decodeFloatzh_fast,);
956 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
957 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
959 /* Be prepared to tell Lennart-coded __decodeFloat */
960 /* where mantissa._mp_d can be put (it does not care about the rest) */
961 p = (StgArrWords *)Hp - 1;
962 SET_ARR_HDR(p,&stg_ARR_WORDS_info,CCCS,1)
963 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
965 /* Perform the operation */
966 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
968 /* returns: (Int# (expn), Int#, ByteArray#) */
969 TICK_RET_UNBOXED_TUP(3);
970 RET_NNP(exponent,mantissa._mp_size,p);
974 #define DOUBLE_MANTISSA_SIZE (sizeofW(StgDouble))
975 #define ARR_SIZE (sizeofW(StgArrWords) + DOUBLE_MANTISSA_SIZE)
977 FN_(decodeDoublezh_fast)
984 /* arguments: D1 = Double# */
987 HP_CHK_GEN_TICKY(ARR_SIZE, NO_PTRS, decodeDoublezh_fast,);
988 TICK_ALLOC_PRIM(sizeofW(StgArrWords),DOUBLE_MANTISSA_SIZE,0);
989 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
991 /* Be prepared to tell Lennart-coded __decodeDouble */
992 /* where mantissa.d can be put (it does not care about the rest) */
993 p = (StgArrWords *)(Hp-ARR_SIZE+1);
994 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
995 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
997 /* Perform the operation */
998 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
1000 /* returns: (Int# (expn), Int#, ByteArray#) */
1001 TICK_RET_UNBOXED_TUP(3);
1002 RET_NNP(exponent,mantissa._mp_size,p);
1006 /* -----------------------------------------------------------------------------
1007 * Concurrency primitives
1008 * -------------------------------------------------------------------------- */
1013 /* args: R1 = closure to spark */
1015 MAYBE_GC(R1_PTR, forkzh_fast);
1017 /* create it right now, return ThreadID in R1 */
1018 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
1019 RtsFlags.GcFlags.initialStkSize, R1.cl);
1020 STGCALL1(scheduleThread, R1.t);
1022 /* switch at the earliest opportunity */
1029 FN_(forkProcesszh_fast)
1037 R1.i = RET_STGCALL1(StgInt, forkProcess, CurrentTSO);
1039 JMP_(ENTRY_CODE(Sp[0]));
1047 JMP_(stg_yield_noregs);
1051 FN_(myThreadIdzh_fast)
1055 RET_P((P_)CurrentTSO);
1059 FN_(labelThreadzh_fast)
1066 STGCALL2(labelThread,(StgTSO *)R1.p,(char *)R2.p);
1068 JMP_(ENTRY_CODE(Sp[0]));
1073 /* -----------------------------------------------------------------------------
1076 * take & putMVar work as follows. Firstly, an important invariant:
1078 * If the MVar is full, then the blocking queue contains only
1079 * threads blocked on putMVar, and if the MVar is empty then the
1080 * blocking queue contains only threads blocked on takeMVar.
1083 * MVar empty : then add ourselves to the blocking queue
1084 * MVar full : remove the value from the MVar, and
1085 * blocking queue empty : return
1086 * blocking queue non-empty : perform the first blocked putMVar
1087 * from the queue, and wake up the
1088 * thread (MVar is now full again)
1090 * putMVar is just the dual of the above algorithm.
1092 * How do we "perform a putMVar"? Well, we have to fiddle around with
1093 * the stack of the thread waiting to do the putMVar. See
1094 * stg_block_putmvar and stg_block_takemvar in HeapStackCheck.c for
1095 * the stack layout, and the PerformPut and PerformTake macros below.
1097 * It is important that a blocked take or put is woken up with the
1098 * take/put already performed, because otherwise there would be a
1099 * small window of vulnerability where the thread could receive an
1100 * exception and never perform its take or put, and we'd end up with a
1103 * -------------------------------------------------------------------------- */
1105 FN_(isEmptyMVarzh_fast)
1107 /* args: R1 = MVar closure */
1110 r = (I_)((GET_INFO((StgMVar*)(R1.p))) == &stg_EMPTY_MVAR_info);
1123 HP_CHK_GEN_TICKY(sizeofW(StgMVar), NO_PTRS, newMVarzh_fast,);
1124 TICK_ALLOC_PRIM(sizeofW(StgMutVar)-1, // consider head,tail,link as admin wds
1126 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
1128 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
1129 SET_HDR(mvar,&stg_EMPTY_MVAR_info,CCCS);
1130 mvar->head = mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1131 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1133 TICK_RET_UNBOXED_TUP(1);
1138 /* If R1 isn't available, pass it on the stack */
1140 #define PerformTake(tso, value) ({ \
1141 (tso)->sp[1] = (W_)value; \
1142 (tso)->sp[0] = (W_)&stg_gc_unpt_r1_info; \
1145 #define PerformTake(tso, value) ({ \
1146 (tso)->sp[1] = (W_)value; \
1147 (tso)->sp[0] = (W_)&stg_ut_1_0_unreg_info; \
1152 #define PerformPut(tso) ({ \
1153 StgClosure *val = (StgClosure *)(tso)->sp[2]; \
1154 (tso)->sp[2] = (W_)&stg_gc_noregs_info; \
1159 FN_(takeMVarzh_fast)
1163 const StgInfoTable *info;
1166 /* args: R1 = MVar closure */
1168 mvar = (StgMVar *)R1.p;
1171 info = LOCK_CLOSURE(mvar);
1173 info = GET_INFO(mvar);
1176 /* If the MVar is empty, put ourselves on its blocking queue,
1177 * and wait until we're woken up.
1179 if (info == &stg_EMPTY_MVAR_info) {
1180 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1181 mvar->head = CurrentTSO;
1183 mvar->tail->link = CurrentTSO;
1185 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1186 CurrentTSO->why_blocked = BlockedOnMVar;
1187 CurrentTSO->block_info.closure = (StgClosure *)mvar;
1188 mvar->tail = CurrentTSO;
1191 /* unlock the MVar */
1192 mvar->header.info = &stg_EMPTY_MVAR_info;
1194 JMP_(stg_block_takemvar);
1197 /* we got the value... */
1200 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1201 /* There are putMVar(s) waiting...
1202 * wake up the first thread on the queue
1204 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1206 /* actually perform the putMVar for the thread that we just woke up */
1207 mvar->value = PerformPut(mvar->head);
1209 #if defined(GRAN) || defined(PAR)
1210 /* ToDo: check 2nd arg (mvar) is right */
1211 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1213 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1215 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1216 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1219 /* unlock in the SMP case */
1220 SET_INFO(mvar,&stg_FULL_MVAR_info);
1222 TICK_RET_UNBOXED_TUP(1);
1225 /* No further putMVars, MVar is now empty */
1227 /* do this last... we might have locked the MVar in the SMP case,
1228 * and writing the info pointer will unlock it.
1230 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1231 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1232 TICK_RET_UNBOXED_TUP(1);
1238 FN_(tryTakeMVarzh_fast)
1242 const StgInfoTable *info;
1245 /* args: R1 = MVar closure */
1247 mvar = (StgMVar *)R1.p;
1250 info = LOCK_CLOSURE(mvar);
1252 info = GET_INFO(mvar);
1255 if (info == &stg_EMPTY_MVAR_info) {
1258 /* unlock the MVar */
1259 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1262 /* HACK: we need a pointer to pass back,
1263 * so we abuse NO_FINALIZER_closure
1265 RET_NP(0, &stg_NO_FINALIZER_closure);
1268 /* we got the value... */
1271 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1272 /* There are putMVar(s) waiting...
1273 * wake up the first thread on the queue
1275 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1277 /* actually perform the putMVar for the thread that we just woke up */
1278 mvar->value = PerformPut(mvar->head);
1280 #if defined(GRAN) || defined(PAR)
1281 /* ToDo: check 2nd arg (mvar) is right */
1282 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1284 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1286 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1287 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1290 /* unlock in the SMP case */
1291 SET_INFO(mvar,&stg_FULL_MVAR_info);
1294 /* No further putMVars, MVar is now empty */
1295 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1297 /* do this last... we might have locked the MVar in the SMP case,
1298 * and writing the info pointer will unlock it.
1300 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1303 TICK_RET_UNBOXED_TUP(1);
1311 const StgInfoTable *info;
1314 /* args: R1 = MVar, R2 = value */
1316 mvar = (StgMVar *)R1.p;
1319 info = LOCK_CLOSURE(mvar);
1321 info = GET_INFO(mvar);
1324 if (info == &stg_FULL_MVAR_info) {
1325 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1326 mvar->head = CurrentTSO;
1328 mvar->tail->link = CurrentTSO;
1330 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1331 CurrentTSO->why_blocked = BlockedOnMVar;
1332 CurrentTSO->block_info.closure = (StgClosure *)mvar;
1333 mvar->tail = CurrentTSO;
1336 /* unlock the MVar */
1337 SET_INFO(mvar,&stg_FULL_MVAR_info);
1339 JMP_(stg_block_putmvar);
1342 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1343 /* There are takeMVar(s) waiting: wake up the first one
1345 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1347 /* actually perform the takeMVar */
1348 PerformTake(mvar->head, R2.cl);
1350 #if defined(GRAN) || defined(PAR)
1351 /* ToDo: check 2nd arg (mvar) is right */
1352 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1354 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1356 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1357 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1360 /* unlocks the MVar in the SMP case */
1361 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1363 JMP_(ENTRY_CODE(Sp[0]));
1365 /* No further takes, the MVar is now full. */
1366 mvar->value = R2.cl;
1367 /* unlocks the MVar in the SMP case */
1368 SET_INFO(mvar,&stg_FULL_MVAR_info);
1369 JMP_(ENTRY_CODE(Sp[0]));
1372 /* ToDo: yield afterward for better communication performance? */
1376 FN_(tryPutMVarzh_fast)
1379 const StgInfoTable *info;
1382 /* args: R1 = MVar, R2 = value */
1384 mvar = (StgMVar *)R1.p;
1387 info = LOCK_CLOSURE(mvar);
1389 info = GET_INFO(mvar);
1392 if (info == &stg_FULL_MVAR_info) {
1395 /* unlock the MVar */
1396 mvar->header.info = &stg_FULL_MVAR_info;
1402 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1403 /* There are takeMVar(s) waiting: wake up the first one
1405 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1407 /* actually perform the takeMVar */
1408 PerformTake(mvar->head, R2.cl);
1410 #if defined(GRAN) || defined(PAR)
1411 /* ToDo: check 2nd arg (mvar) is right */
1412 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1414 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1416 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1417 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1420 /* unlocks the MVar in the SMP case */
1421 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1423 JMP_(ENTRY_CODE(Sp[0]));
1425 /* No further takes, the MVar is now full. */
1426 mvar->value = R2.cl;
1427 /* unlocks the MVar in the SMP case */
1428 SET_INFO(mvar,&stg_FULL_MVAR_info);
1429 JMP_(ENTRY_CODE(Sp[0]));
1432 /* ToDo: yield afterward for better communication performance? */
1436 /* -----------------------------------------------------------------------------
1437 Stable pointer primitives
1438 ------------------------------------------------------------------------- */
1440 FN_(makeStableNamezh_fast)
1443 StgStableName *sn_obj;
1446 HP_CHK_GEN_TICKY(sizeofW(StgStableName), R1_PTR, makeStableNamezh_fast,);
1447 TICK_ALLOC_PRIM(sizeofW(StgHeader),
1448 sizeofW(StgStableName)-sizeofW(StgHeader), 0);
1449 CCS_ALLOC(CCCS,sizeofW(StgStableName)); /* ccs prof */
1451 index = RET_STGCALL1(StgWord,lookupStableName,R1.p);
1453 /* Is there already a StableName for this heap object? */
1454 if (stable_ptr_table[index].sn_obj == NULL) {
1455 sn_obj = (StgStableName *) (Hp - sizeofW(StgStableName) + 1);
1456 SET_HDR(sn_obj,&stg_STABLE_NAME_info,CCCS);
1458 stable_ptr_table[index].sn_obj = (StgClosure *)sn_obj;
1460 (StgClosure *)sn_obj = stable_ptr_table[index].sn_obj;
1463 TICK_RET_UNBOXED_TUP(1);
1468 FN_(makeStablePtrzh_fast)
1473 MAYBE_GC(R1_PTR, makeStablePtrzh_fast);
1474 sp = RET_STGCALL1(StgStablePtr,getStablePtr,R1.p);
1479 FN_(deRefStablePtrzh_fast)
1481 /* Args: R1 = the stable ptr */
1485 sp = (StgStablePtr)R1.w;
1486 r = stable_ptr_table[(StgWord)sp].addr;
1491 /* -----------------------------------------------------------------------------
1492 Bytecode object primitives
1493 ------------------------------------------------------------------------- */
1505 HP_CHK_GEN_TICKY(sizeofW(StgBCO),R1_PTR|R2_PTR|R3_PTR|R4_PTR, newBCOzh_fast,);
1506 TICK_ALLOC_PRIM(sizeofW(StgHeader), sizeofW(StgBCO)-sizeofW(StgHeader), 0);
1507 CCS_ALLOC(CCCS,sizeofW(StgBCO)); /* ccs prof */
1508 bco = (StgBCO *) (Hp + 1 - sizeofW(StgBCO));
1509 SET_HDR(bco, &stg_BCO_info, CCCS);
1511 bco->instrs = (StgArrWords*)R1.cl;
1512 bco->literals = (StgArrWords*)R2.cl;
1513 bco->ptrs = (StgMutArrPtrs*)R3.cl;
1514 bco->itbls = (StgArrWords*)R4.cl;
1516 TICK_RET_UNBOXED_TUP(1);
1521 FN_(mkApUpd0zh_fast)
1523 /* R1.p = the fn for the AP_UPD
1527 HP_CHK_GEN_TICKY(AP_sizeW(0), R1_PTR, mkApUpd0zh_fast,);
1528 TICK_ALLOC_PRIM(sizeofW(StgHeader), AP_sizeW(0)-sizeofW(StgHeader), 0);
1529 CCS_ALLOC(CCCS,AP_sizeW(0)); /* ccs prof */
1530 ap = (StgAP_UPD *) (Hp + 1 - AP_sizeW(0));
1531 SET_HDR(ap, &stg_AP_UPD_info, CCCS);
1536 TICK_RET_UNBOXED_TUP(1);
1541 /* -----------------------------------------------------------------------------
1542 Thread I/O blocking primitives
1543 -------------------------------------------------------------------------- */
1545 FN_(waitReadzh_fast)
1549 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1550 CurrentTSO->why_blocked = BlockedOnRead;
1551 CurrentTSO->block_info.fd = R1.i;
1552 ACQUIRE_LOCK(&sched_mutex);
1553 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1554 RELEASE_LOCK(&sched_mutex);
1555 JMP_(stg_block_noregs);
1559 FN_(waitWritezh_fast)
1563 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1564 CurrentTSO->why_blocked = BlockedOnWrite;
1565 CurrentTSO->block_info.fd = R1.i;
1566 ACQUIRE_LOCK(&sched_mutex);
1567 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1568 RELEASE_LOCK(&sched_mutex);
1569 JMP_(stg_block_noregs);
1579 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1580 CurrentTSO->why_blocked = BlockedOnDelay;
1582 ACQUIRE_LOCK(&sched_mutex);
1584 target = (R1.i / (TICK_MILLISECS*1000)) + getourtimeofday();
1585 CurrentTSO->block_info.target = target;
1587 /* Insert the new thread in the sleeping queue. */
1590 while (t != END_TSO_QUEUE && t->block_info.target < target) {
1595 CurrentTSO->link = t;
1597 sleeping_queue = CurrentTSO;
1599 prev->link = CurrentTSO;
1602 RELEASE_LOCK(&sched_mutex);
1603 JMP_(stg_block_noregs);