1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.100 2002/07/17 09:21:50 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"
25 #ifdef HAVE_SYS_TYPES_H
26 # include <sys/types.h>
33 classes CCallable and CReturnable don't really exist, but the
34 compiler insists on generating dictionaries containing references
35 to GHC_ZcCCallable_static_info etc., so we provide dummy symbols
36 for these. Some C compilers can't cope with zero-length static arrays,
37 so we have to make these one element long.
40 StgWord GHC_ZCCCallable_static_info[1];
41 StgWord GHC_ZCCReturnable_static_info[1];
43 /* -----------------------------------------------------------------------------
44 Macros for Hand-written primitives.
45 -------------------------------------------------------------------------- */
48 * Horrible macros for returning unboxed tuples.
50 * How an unboxed tuple is returned depends on two factors:
51 * - the number of real registers we have available
52 * - the boxedness of the returned fields.
54 * To return an unboxed tuple from a primitive operation, we have macros
55 * RET_<layout> where <layout> describes the boxedness of each field of the
56 * unboxed tuple: N indicates a non-pointer field, and P indicates a pointer.
58 * We only define the cases actually used, to avoid having too much
59 * garbage in this section. Warning: any bugs in here will be hard to
63 /*------ All Regs available */
65 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
66 # define RET_N(a) RET_P(a)
68 # define RET_PP(a,b) R1.w = (W_)(a); R2.w = (W_)(b); JMP_(ENTRY_CODE(Sp[0]));
69 # define RET_NN(a,b) RET_PP(a,b)
70 # define RET_NP(a,b) RET_PP(a,b)
72 # define RET_PPP(a,b,c) \
73 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); JMP_(ENTRY_CODE(Sp[0]));
74 # define RET_NNP(a,b,c) RET_PPP(a,b,c)
76 # define RET_NNNP(a,b,c,d) \
77 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); R4.w = (W_)d; \
78 JMP_(ENTRY_CODE(Sp[0]));
80 # define RET_NPNP(a,b,c,d) \
81 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); R4.w = (W_)(d); \
82 JMP_(ENTRY_CODE(Sp[0]));
84 # define RET_NNPNNP(a,b,c,d,e,f) \
85 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); \
86 R4.w = (W_)(d); R5.w = (W_)(e); R6.w = (W_)(f); \
87 JMP_(ENTRY_CODE(Sp[0]));
89 #elif defined(REG_R7) || defined(REG_R6) || defined(REG_R5) || \
90 defined(REG_R4) || defined(REG_R3)
91 # error RET_n macros not defined for this setup.
93 /*------ 2 Registers available */
96 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
97 # define RET_N(a) RET_P(a)
99 # define RET_PP(a,b) R1.w = (W_)(a); R2.w = (W_)(b); \
100 JMP_(ENTRY_CODE(Sp[0]));
101 # define RET_NN(a,b) RET_PP(a,b)
102 # define RET_NP(a,b) RET_PP(a,b)
104 # define RET_PPP(a,b,c) \
105 R1.w = (W_)(a); R2.w = (W_)(b); Sp[-1] = (W_)(c); Sp -= 1; \
106 JMP_(ENTRY_CODE(Sp[1]));
107 # define RET_NNP(a,b,c) \
108 R1.w = (W_)(a); R2.w = (W_)(b); Sp[-1] = (W_)(c); Sp -= 1; \
109 JMP_(ENTRY_CODE(Sp[1]));
111 # define RET_NNNP(a,b,c,d) \
114 /* Sp[-3] = ARGTAG(1); */ \
118 JMP_(ENTRY_CODE(Sp[3]));
120 # define RET_NPNP(a,b,c,d) \
123 /* Sp[-3] = ARGTAG(1); */ \
127 JMP_(ENTRY_CODE(Sp[3]));
129 # define RET_NNPNNP(a,b,c,d,e,f) \
133 /* Sp[-5] = ARGTAG(1); */ \
135 /* Sp[-3] = ARGTAG(1); */ \
139 JMP_(ENTRY_CODE(Sp[6]));
141 /*------ 1 Register available */
142 #elif defined(REG_R1)
143 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
144 # define RET_N(a) RET_P(a)
146 # define RET_PP(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 1; \
147 JMP_(ENTRY_CODE(Sp[1]));
148 # define RET_NN(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 2; \
149 JMP_(ENTRY_CODE(Sp[2]));
150 # define RET_NP(a,b) RET_PP(a,b)
152 # define RET_PPP(a,b,c) \
153 R1.w = (W_)(a); Sp[-2] = (W_)(b); Sp[-1] = (W_)(c); Sp -= 2; \
154 JMP_(ENTRY_CODE(Sp[2]));
155 # define RET_NNP(a,b,c) \
156 R1.w = (W_)(a); Sp[-2] = (W_)(b); Sp[-1] = (W_)(c); Sp -= 3; \
157 JMP_(ENTRY_CODE(Sp[3]));
159 # define RET_NNNP(a,b,c,d) \
161 /* Sp[-5] = ARGTAG(1); */ \
163 /* Sp[-3] = ARGTAG(1); */ \
167 JMP_(ENTRY_CODE(Sp[5]));
169 # define RET_NPNP(a,b,c,d) \
172 /* Sp[-3] = ARGTAG(1); */ \
176 JMP_(ENTRY_CODE(Sp[4]));
178 # define RET_NNPNNP(a,b,c,d,e,f) \
182 /* Sp[-3] = ARGTAG(1); */ \
184 /* Sp[-5] = ARGTAG(1); */ \
187 /* Sp[-8] = ARGTAG(1); */ \
189 JMP_(ENTRY_CODE(Sp[8]));
191 #else /* 0 Regs available */
193 #define PUSH_P(o,x) Sp[-o] = (W_)(x)
196 #define PUSH_N(o,x) Sp[1-o] = (W_)(x); Sp[-o] = ARG_TAG(1);
198 #define PUSH_N(o,x) Sp[1-o] = (W_)(x);
201 #define PUSHED(m) Sp -= (m); JMP_(ENTRY_CODE(Sp[m]));
203 /* Here's how to construct these macros:
205 * N = number of N's in the name;
206 * P = number of P's in the name;
208 * while (nonNull(name)) {
209 * if (nextChar == 'P') {
220 # define RET_P(a) PUSH_P(1,a); PUSHED(1)
221 # define RET_N(a) PUSH_N(2,a); PUSHED(2)
223 # define RET_PP(a,b) PUSH_P(2,a); PUSH_P(1,b); PUSHED(2)
224 # define RET_NN(a,b) PUSH_N(4,a); PUSH_N(2,b); PUSHED(4)
225 # define RET_NP(a,b) PUSH_N(3,a); PUSH_P(1,b); PUSHED(3)
227 # define RET_PPP(a,b,c) PUSH_P(3,a); PUSH_P(2,b); PUSH_P(1,c); PUSHED(3)
228 # define RET_NNP(a,b,c) PUSH_N(5,a); PUSH_N(3,b); PUSH_P(1,c); PUSHED(5)
230 # 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)
231 # 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)
232 # 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)
236 /*-----------------------------------------------------------------------------
239 Basically just new*Array - the others are all inline macros.
241 The size arg is always passed in R1, and the result returned in R1.
243 The slow entry point is for returning from a heap check, the saved
244 size argument must be re-loaded from the stack.
245 -------------------------------------------------------------------------- */
247 /* for objects that are *less* than the size of a word, make sure we
248 * round up to the nearest word for the size of the array.
251 #define BYTES_TO_STGWORDS(n) ((n) + sizeof(W_) - 1)/sizeof(W_)
253 FN_(newByteArrayzh_fast)
255 W_ size, stuff_size, n;
258 MAYBE_GC(NO_PTRS,newByteArrayzh_fast);
260 stuff_size = BYTES_TO_STGWORDS(n);
261 size = sizeofW(StgArrWords)+ stuff_size;
262 p = (StgArrWords *)RET_STGCALL1(P_,allocate,size);
263 TICK_ALLOC_PRIM(sizeofW(StgArrWords),stuff_size,0);
264 SET_HDR(p, &stg_ARR_WORDS_info, CCCS);
265 p->words = stuff_size;
266 TICK_RET_UNBOXED_TUP(1)
271 FN_(newPinnedByteArrayzh_fast)
273 W_ size, stuff_size, n;
276 MAYBE_GC(NO_PTRS,newPinnedByteArrayzh_fast);
278 stuff_size = BYTES_TO_STGWORDS(n);
280 // We want an 8-byte aligned array. allocatePinned() gives us
281 // 8-byte aligned memory by default, but we want to align the
282 // *goods* inside the ArrWords object, so we have to check the
283 // size of the ArrWords header and adjust our size accordingly.
284 size = sizeofW(StgArrWords)+ stuff_size;
285 if ((sizeof(StgArrWords) & 7) != 0) {
289 p = (StgArrWords *)RET_STGCALL1(P_,allocatePinned,size);
290 TICK_ALLOC_PRIM(sizeofW(StgArrWords),stuff_size,0);
292 // Again, if the ArrWords header isn't a multiple of 8 bytes, we
293 // have to push the object forward one word so that the goods
294 // fall on an 8-byte boundary.
295 if ((sizeof(StgArrWords) & 7) != 0) {
299 SET_HDR(p, &stg_ARR_WORDS_info, CCCS);
300 p->words = stuff_size;
301 TICK_RET_UNBOXED_TUP(1)
314 MAYBE_GC(R2_PTR,newArrayzh_fast);
316 size = sizeofW(StgMutArrPtrs) + n;
317 arr = (StgMutArrPtrs *)RET_STGCALL1(P_, allocate, size);
318 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
320 SET_HDR(arr,&stg_MUT_ARR_PTRS_info,CCCS);
324 for (p = (P_)arr + sizeofW(StgMutArrPtrs);
325 p < (P_)arr + size; p++) {
329 TICK_RET_UNBOXED_TUP(1);
334 FN_(newMutVarzh_fast)
337 /* Args: R1.p = initialisation value */
340 HP_CHK_GEN_TICKY(sizeofW(StgMutVar), R1_PTR, newMutVarzh_fast,);
341 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1,1, 0); /* hack, dependent on rep. */
342 CCS_ALLOC(CCCS,sizeofW(StgMutVar));
344 mv = (StgMutVar *)(Hp-sizeofW(StgMutVar)+1);
345 SET_HDR(mv,&stg_MUT_VAR_info,CCCS);
348 TICK_RET_UNBOXED_TUP(1);
353 /* -----------------------------------------------------------------------------
354 Foreign Object Primitives
355 -------------------------------------------------------------------------- */
357 FN_(mkForeignObjzh_fast)
359 /* R1.p = ptr to foreign object,
361 StgForeignObj *result;
364 HP_CHK_GEN_TICKY(sizeofW(StgForeignObj), NO_PTRS, mkForeignObjzh_fast,);
365 TICK_ALLOC_PRIM(sizeofW(StgHeader),
366 sizeofW(StgForeignObj)-sizeofW(StgHeader), 0);
367 CCS_ALLOC(CCCS,sizeofW(StgForeignObj)); /* ccs prof */
369 result = (StgForeignObj *) (Hp + 1 - sizeofW(StgForeignObj));
370 SET_HDR(result,&stg_FOREIGN_info,CCCS);
373 /* returns (# s#, ForeignObj# #) */
374 TICK_RET_UNBOXED_TUP(1);
379 /* These two are out-of-line for the benefit of the NCG */
380 FN_(unsafeThawArrayzh_fast)
383 SET_INFO((StgClosure *)R1.cl,&stg_MUT_ARR_PTRS_info);
384 recordMutable((StgMutClosure*)R1.cl);
386 TICK_RET_UNBOXED_TUP(1);
391 /* -----------------------------------------------------------------------------
392 Weak Pointer Primitives
393 -------------------------------------------------------------------------- */
399 R3.p = finalizer (or NULL)
405 R3.cl = &stg_NO_FINALIZER_closure;
408 HP_CHK_GEN_TICKY(sizeofW(StgWeak),R1_PTR|R2_PTR|R3_PTR, mkWeakzh_fast,);
409 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1, // +1 is for the link field
410 sizeofW(StgWeak)-sizeofW(StgHeader)-1, 0);
411 CCS_ALLOC(CCCS,sizeofW(StgWeak)); /* ccs prof */
413 w = (StgWeak *) (Hp + 1 - sizeofW(StgWeak));
414 SET_HDR(w, &stg_WEAK_info, CCCS);
418 w->finalizer = R3.cl;
420 w->link = weak_ptr_list;
422 IF_DEBUG(weak, fprintf(stderr,"New weak pointer at %p\n",w));
424 TICK_RET_UNBOXED_TUP(1);
429 FN_(finalizzeWeakzh_fast)
436 TICK_RET_UNBOXED_TUP(0);
437 w = (StgDeadWeak *)R1.p;
440 if (w->header.info == &stg_DEAD_WEAK_info) {
441 RET_NP(0,&stg_NO_FINALIZER_closure);
447 // A weak pointer is inherently used, so we do not need to call
448 // LDV_recordDead_FILL_SLOP_DYNAMIC():
449 // LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)w);
450 // or, LDV_recordDead():
451 // LDV_recordDead((StgClosure *)w, sizeofW(StgWeak) - sizeofW(StgProfHeader));
452 // Furthermore, when PROFILING is turned on, dead weak pointers are exactly as
453 // large as weak pointers, so there is no need to fill the slop, either.
454 // See stg_DEAD_WEAK_info in StgMiscClosures.hc.
457 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
459 w->header.info = &stg_DEAD_WEAK_info;
462 LDV_recordCreate((StgClosure *)w);
464 f = ((StgWeak *)w)->finalizer;
465 w->link = ((StgWeak *)w)->link;
467 /* return the finalizer */
468 if (f == &stg_NO_FINALIZER_closure) {
469 RET_NP(0,&stg_NO_FINALIZER_closure);
476 FN_(deRefWeakzh_fast)
478 /* R1.p = weak ptr */
484 if (w->header.info == &stg_WEAK_info) {
486 val = (P_)((StgWeak *)w)->value;
495 /* -----------------------------------------------------------------------------
496 Arbitrary-precision Integer operations.
497 -------------------------------------------------------------------------- */
499 FN_(int2Integerzh_fast)
501 /* arguments: R1 = Int# */
503 I_ val, s; /* to avoid aliasing */
504 StgArrWords* p; /* address of array result */
508 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, int2Integerzh_fast,);
509 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
510 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
512 p = (StgArrWords *)Hp - 1;
513 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
515 /* mpz_set_si is inlined here, makes things simpler */
519 } else if (val > 0) {
526 /* returns (# size :: Int#,
530 TICK_RET_UNBOXED_TUP(2);
535 FN_(word2Integerzh_fast)
537 /* arguments: R1 = Word# */
539 W_ val; /* to avoid aliasing */
541 StgArrWords* p; /* address of array result */
545 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, word2Integerzh_fast,)
546 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
547 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
549 p = (StgArrWords *)Hp - 1;
550 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
559 /* returns (# size :: Int#,
563 TICK_RET_UNBOXED_TUP(2);
570 * 'long long' primops for converting to/from Integers.
573 #ifdef SUPPORT_LONG_LONGS
575 FN_(int64ToIntegerzh_fast)
577 /* arguments: L1 = Int64# */
579 StgInt64 val; /* to avoid aliasing */
581 I_ s, neg, words_needed;
582 StgArrWords* p; /* address of array result */
588 if ( val >= 0x100000000LL || val <= -0x100000000LL ) {
591 /* minimum is one word */
594 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, int64ToIntegerzh_fast,)
595 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
596 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
598 p = (StgArrWords *)(Hp-words_needed+1) - 1;
599 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
606 hi = (W_)((LW_)val / 0x100000000ULL);
608 if ( words_needed == 2 ) {
612 } else if ( val != 0 ) {
615 } else /* val==0 */ {
618 s = ( neg ? -s : s );
620 /* returns (# size :: Int#,
624 TICK_RET_UNBOXED_TUP(2);
629 FN_(word64ToIntegerzh_fast)
631 /* arguments: L1 = Word64# */
633 StgWord64 val; /* to avoid aliasing */
636 StgArrWords* p; /* address of array result */
640 if ( val >= 0x100000000ULL ) {
645 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, word64ToIntegerzh_fast,)
646 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
647 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
649 p = (StgArrWords *)(Hp-words_needed+1) - 1;
650 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
652 hi = (W_)((LW_)val / 0x100000000ULL);
653 if ( val >= 0x100000000ULL ) {
657 } else if ( val != 0 ) {
660 } else /* val==0 */ {
664 /* returns (# size :: Int#,
668 TICK_RET_UNBOXED_TUP(2);
674 #endif /* SUPPORT_LONG_LONGS */
676 /* ToDo: this is shockingly inefficient */
678 #define GMP_TAKE2_RET1(name,mp_fun) \
681 MP_INT arg1, arg2, result; \
687 /* call doYouWantToGC() */ \
688 MAYBE_GC(R2_PTR | R4_PTR, name); \
690 d1 = (StgArrWords *)R2.p; \
692 d2 = (StgArrWords *)R4.p; \
695 arg1._mp_alloc = d1->words; \
696 arg1._mp_size = (s1); \
697 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
698 arg2._mp_alloc = d2->words; \
699 arg2._mp_size = (s2); \
700 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
702 STGCALL1(mpz_init,&result); \
704 /* Perform the operation */ \
705 STGCALL3(mp_fun,&result,&arg1,&arg2); \
707 TICK_RET_UNBOXED_TUP(2); \
708 RET_NP(result._mp_size, \
709 result._mp_d-sizeofW(StgArrWords)); \
713 #define GMP_TAKE1_RET1(name,mp_fun) \
716 MP_INT arg1, result; \
721 /* call doYouWantToGC() */ \
722 MAYBE_GC(R2_PTR, name); \
724 d1 = (StgArrWords *)R2.p; \
727 arg1._mp_alloc = d1->words; \
728 arg1._mp_size = (s1); \
729 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
731 STGCALL1(mpz_init,&result); \
733 /* Perform the operation */ \
734 STGCALL2(mp_fun,&result,&arg1); \
736 TICK_RET_UNBOXED_TUP(2); \
737 RET_NP(result._mp_size, \
738 result._mp_d-sizeofW(StgArrWords)); \
742 #define GMP_TAKE2_RET2(name,mp_fun) \
745 MP_INT arg1, arg2, result1, result2; \
751 /* call doYouWantToGC() */ \
752 MAYBE_GC(R2_PTR | R4_PTR, name); \
754 d1 = (StgArrWords *)R2.p; \
756 d2 = (StgArrWords *)R4.p; \
759 arg1._mp_alloc = d1->words; \
760 arg1._mp_size = (s1); \
761 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
762 arg2._mp_alloc = d2->words; \
763 arg2._mp_size = (s2); \
764 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
766 STGCALL1(mpz_init,&result1); \
767 STGCALL1(mpz_init,&result2); \
769 /* Perform the operation */ \
770 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
772 TICK_RET_UNBOXED_TUP(4); \
773 RET_NPNP(result1._mp_size, \
774 result1._mp_d-sizeofW(StgArrWords), \
776 result2._mp_d-sizeofW(StgArrWords)); \
780 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add);
781 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub);
782 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul);
783 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd);
784 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q);
785 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r);
786 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact);
787 GMP_TAKE2_RET1(andIntegerzh_fast, mpz_and);
788 GMP_TAKE2_RET1(orIntegerzh_fast, mpz_ior);
789 GMP_TAKE2_RET1(xorIntegerzh_fast, mpz_xor);
790 GMP_TAKE1_RET1(complementIntegerzh_fast, mpz_com);
792 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr);
793 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr);
798 /* R1 = the first Int#; R2 = the second Int# */
802 aa = (mp_limb_t)(R1.i);
803 r = RET_STGCALL3(StgInt, mpn_gcd_1, (mp_limb_t *)(&aa), 1, (mp_limb_t)(R2.i));
806 /* Result parked in R1, return via info-pointer at TOS */
807 JMP_(ENTRY_CODE(Sp[0]));
811 FN_(gcdIntegerIntzh_fast)
813 /* R1 = s1; R2 = d1; R3 = the int */
816 r = RET_STGCALL3(StgInt,mpn_gcd_1,(mp_limb_t *)(BYTE_ARR_CTS(R2.p)), R1.i, R3.i);
819 /* Result parked in R1, return via info-pointer at TOS */
820 JMP_(ENTRY_CODE(Sp[0]));
824 FN_(cmpIntegerIntzh_fast)
826 /* R1 = s1; R2 = d1; R3 = the int */
837 // paraphrased from mpz_cmp_si() in the GMP sources
840 } else if (v_digit < 0) {
845 if (usize != vsize) {
846 R1.i = usize - vsize; JMP_(ENTRY_CODE(Sp[0]));
850 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
853 u_digit = *(mp_limb_t *)(BYTE_ARR_CTS(R2.p));
855 if (u_digit == (mp_limb_t) (unsigned long) v_digit) {
856 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
859 if (u_digit > (mp_limb_t) (unsigned long) v_digit) {
865 JMP_(ENTRY_CODE(Sp[0]));
869 FN_(cmpIntegerzh_fast)
871 /* R1 = s1; R2 = d1; R3 = s2; R4 = d2 */
879 // paraphrased from mpz_cmp() in the GMP sources
883 if (usize != vsize) {
884 R1.i = usize - vsize; JMP_(ENTRY_CODE(Sp[0]));
888 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
893 up = BYTE_ARR_CTS(R2.p);
894 vp = BYTE_ARR_CTS(R4.p);
896 cmp = RET_STGCALL3(I_, mpn_cmp, (mp_limb_t *)up, (mp_limb_t *)vp, size);
899 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
902 if ((cmp < 0) == (usize < 0)) {
907 /* Result parked in R1, return via info-pointer at TOS */
908 JMP_(ENTRY_CODE(Sp[0]));
912 FN_(integer2Intzh_fast)
921 r = ((mp_limb_t *) (BYTE_ARR_CTS(R2.p)))[0];
924 /* Result parked in R1, return via info-pointer at TOS */
926 JMP_(ENTRY_CODE(Sp[0]));
930 FN_(integer2Wordzh_fast)
940 r = ((mp_limb_t *) (BYTE_ARR_CTS(R2.p)))[0];
943 /* Result parked in R1, return via info-pointer at TOS */
945 JMP_(ENTRY_CODE(Sp[0]));
950 FN_(decodeFloatzh_fast)
958 /* arguments: F1 = Float# */
961 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, decodeFloatzh_fast,);
962 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
963 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
965 /* Be prepared to tell Lennart-coded __decodeFloat */
966 /* where mantissa._mp_d can be put (it does not care about the rest) */
967 p = (StgArrWords *)Hp - 1;
968 SET_ARR_HDR(p,&stg_ARR_WORDS_info,CCCS,1)
969 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
971 /* Perform the operation */
972 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
974 /* returns: (Int# (expn), Int#, ByteArray#) */
975 TICK_RET_UNBOXED_TUP(3);
976 RET_NNP(exponent,mantissa._mp_size,p);
980 #define DOUBLE_MANTISSA_SIZE (sizeofW(StgDouble))
981 #define ARR_SIZE (sizeofW(StgArrWords) + DOUBLE_MANTISSA_SIZE)
983 FN_(decodeDoublezh_fast)
990 /* arguments: D1 = Double# */
993 HP_CHK_GEN_TICKY(ARR_SIZE, NO_PTRS, decodeDoublezh_fast,);
994 TICK_ALLOC_PRIM(sizeofW(StgArrWords),DOUBLE_MANTISSA_SIZE,0);
995 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
997 /* Be prepared to tell Lennart-coded __decodeDouble */
998 /* where mantissa.d can be put (it does not care about the rest) */
999 p = (StgArrWords *)(Hp-ARR_SIZE+1);
1000 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
1001 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
1003 /* Perform the operation */
1004 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
1006 /* returns: (Int# (expn), Int#, ByteArray#) */
1007 TICK_RET_UNBOXED_TUP(3);
1008 RET_NNP(exponent,mantissa._mp_size,p);
1012 /* -----------------------------------------------------------------------------
1013 * Concurrency primitives
1014 * -------------------------------------------------------------------------- */
1019 /* args: R1 = closure to spark */
1021 MAYBE_GC(R1_PTR, forkzh_fast);
1023 /* create it right now, return ThreadID in R1 */
1024 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
1025 RtsFlags.GcFlags.initialStkSize, R1.cl);
1026 STGCALL1(scheduleThread, R1.t);
1028 /* switch at the earliest opportunity */
1035 FN_(forkProcesszh_fast)
1043 R1.i = RET_STGCALL1(StgInt, forkProcess, CurrentTSO);
1045 JMP_(ENTRY_CODE(Sp[0]));
1053 JMP_(stg_yield_noregs);
1057 FN_(myThreadIdzh_fast)
1061 RET_P((P_)CurrentTSO);
1065 FN_(labelThreadzh_fast)
1072 STGCALL2(labelThread,(StgTSO *)R1.p,(char *)R2.p);
1074 JMP_(ENTRY_CODE(Sp[0]));
1079 /* -----------------------------------------------------------------------------
1082 * take & putMVar work as follows. Firstly, an important invariant:
1084 * If the MVar is full, then the blocking queue contains only
1085 * threads blocked on putMVar, and if the MVar is empty then the
1086 * blocking queue contains only threads blocked on takeMVar.
1089 * MVar empty : then add ourselves to the blocking queue
1090 * MVar full : remove the value from the MVar, and
1091 * blocking queue empty : return
1092 * blocking queue non-empty : perform the first blocked putMVar
1093 * from the queue, and wake up the
1094 * thread (MVar is now full again)
1096 * putMVar is just the dual of the above algorithm.
1098 * How do we "perform a putMVar"? Well, we have to fiddle around with
1099 * the stack of the thread waiting to do the putMVar. See
1100 * stg_block_putmvar and stg_block_takemvar in HeapStackCheck.c for
1101 * the stack layout, and the PerformPut and PerformTake macros below.
1103 * It is important that a blocked take or put is woken up with the
1104 * take/put already performed, because otherwise there would be a
1105 * small window of vulnerability where the thread could receive an
1106 * exception and never perform its take or put, and we'd end up with a
1109 * -------------------------------------------------------------------------- */
1111 FN_(isEmptyMVarzh_fast)
1113 /* args: R1 = MVar closure */
1116 r = (I_)((GET_INFO((StgMVar*)(R1.p))) == &stg_EMPTY_MVAR_info);
1129 HP_CHK_GEN_TICKY(sizeofW(StgMVar), NO_PTRS, newMVarzh_fast,);
1130 TICK_ALLOC_PRIM(sizeofW(StgMutVar)-1, // consider head,tail,link as admin wds
1132 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
1134 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
1135 SET_HDR(mvar,&stg_EMPTY_MVAR_info,CCCS);
1136 mvar->head = mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1137 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1139 TICK_RET_UNBOXED_TUP(1);
1144 /* If R1 isn't available, pass it on the stack */
1146 #define PerformTake(tso, value) ({ \
1147 (tso)->sp[1] = (W_)value; \
1148 (tso)->sp[0] = (W_)&stg_gc_unpt_r1_info; \
1151 #define PerformTake(tso, value) ({ \
1152 (tso)->sp[1] = (W_)value; \
1153 (tso)->sp[0] = (W_)&stg_ut_1_0_unreg_info; \
1158 #define PerformPut(tso) ({ \
1159 StgClosure *val = (StgClosure *)(tso)->sp[2]; \
1160 (tso)->sp[2] = (W_)&stg_gc_noregs_info; \
1165 FN_(takeMVarzh_fast)
1169 const StgInfoTable *info;
1172 /* args: R1 = MVar closure */
1174 mvar = (StgMVar *)R1.p;
1177 info = LOCK_CLOSURE(mvar);
1179 info = GET_INFO(mvar);
1182 /* If the MVar is empty, put ourselves on its blocking queue,
1183 * and wait until we're woken up.
1185 if (info == &stg_EMPTY_MVAR_info) {
1186 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1187 mvar->head = CurrentTSO;
1189 mvar->tail->link = CurrentTSO;
1191 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1192 CurrentTSO->why_blocked = BlockedOnMVar;
1193 CurrentTSO->block_info.closure = (StgClosure *)mvar;
1194 mvar->tail = CurrentTSO;
1197 /* unlock the MVar */
1198 mvar->header.info = &stg_EMPTY_MVAR_info;
1200 JMP_(stg_block_takemvar);
1203 /* we got the value... */
1206 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1207 /* There are putMVar(s) waiting...
1208 * wake up the first thread on the queue
1210 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1212 /* actually perform the putMVar for the thread that we just woke up */
1213 mvar->value = PerformPut(mvar->head);
1215 #if defined(GRAN) || defined(PAR)
1216 /* ToDo: check 2nd arg (mvar) is right */
1217 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1219 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1221 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1222 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1225 /* unlock in the SMP case */
1226 SET_INFO(mvar,&stg_FULL_MVAR_info);
1228 TICK_RET_UNBOXED_TUP(1);
1231 /* No further putMVars, MVar is now empty */
1233 /* do this last... we might have locked the MVar in the SMP case,
1234 * and writing the info pointer will unlock it.
1236 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1237 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1238 TICK_RET_UNBOXED_TUP(1);
1244 FN_(tryTakeMVarzh_fast)
1248 const StgInfoTable *info;
1251 /* args: R1 = MVar closure */
1253 mvar = (StgMVar *)R1.p;
1256 info = LOCK_CLOSURE(mvar);
1258 info = GET_INFO(mvar);
1261 if (info == &stg_EMPTY_MVAR_info) {
1264 /* unlock the MVar */
1265 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1268 /* HACK: we need a pointer to pass back,
1269 * so we abuse NO_FINALIZER_closure
1271 RET_NP(0, &stg_NO_FINALIZER_closure);
1274 /* we got the value... */
1277 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1278 /* There are putMVar(s) waiting...
1279 * wake up the first thread on the queue
1281 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1283 /* actually perform the putMVar for the thread that we just woke up */
1284 mvar->value = PerformPut(mvar->head);
1286 #if defined(GRAN) || defined(PAR)
1287 /* ToDo: check 2nd arg (mvar) is right */
1288 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1290 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1292 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1293 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1296 /* unlock in the SMP case */
1297 SET_INFO(mvar,&stg_FULL_MVAR_info);
1300 /* No further putMVars, MVar is now empty */
1301 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1303 /* do this last... we might have locked the MVar in the SMP case,
1304 * and writing the info pointer will unlock it.
1306 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1309 TICK_RET_UNBOXED_TUP(1);
1317 const StgInfoTable *info;
1320 /* args: R1 = MVar, R2 = value */
1322 mvar = (StgMVar *)R1.p;
1325 info = LOCK_CLOSURE(mvar);
1327 info = GET_INFO(mvar);
1330 if (info == &stg_FULL_MVAR_info) {
1331 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1332 mvar->head = CurrentTSO;
1334 mvar->tail->link = CurrentTSO;
1336 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1337 CurrentTSO->why_blocked = BlockedOnMVar;
1338 CurrentTSO->block_info.closure = (StgClosure *)mvar;
1339 mvar->tail = CurrentTSO;
1342 /* unlock the MVar */
1343 SET_INFO(mvar,&stg_FULL_MVAR_info);
1345 JMP_(stg_block_putmvar);
1348 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1349 /* There are takeMVar(s) waiting: wake up the first one
1351 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1353 /* actually perform the takeMVar */
1354 PerformTake(mvar->head, R2.cl);
1356 #if defined(GRAN) || defined(PAR)
1357 /* ToDo: check 2nd arg (mvar) is right */
1358 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1360 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1362 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1363 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1366 /* unlocks the MVar in the SMP case */
1367 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1369 JMP_(ENTRY_CODE(Sp[0]));
1371 /* No further takes, the MVar is now full. */
1372 mvar->value = R2.cl;
1373 /* unlocks the MVar in the SMP case */
1374 SET_INFO(mvar,&stg_FULL_MVAR_info);
1375 JMP_(ENTRY_CODE(Sp[0]));
1378 /* ToDo: yield afterward for better communication performance? */
1382 FN_(tryPutMVarzh_fast)
1385 const StgInfoTable *info;
1388 /* args: R1 = MVar, R2 = value */
1390 mvar = (StgMVar *)R1.p;
1393 info = LOCK_CLOSURE(mvar);
1395 info = GET_INFO(mvar);
1398 if (info == &stg_FULL_MVAR_info) {
1401 /* unlock the MVar */
1402 mvar->header.info = &stg_FULL_MVAR_info;
1408 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1409 /* There are takeMVar(s) waiting: wake up the first one
1411 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1413 /* actually perform the takeMVar */
1414 PerformTake(mvar->head, R2.cl);
1416 #if defined(GRAN) || defined(PAR)
1417 /* ToDo: check 2nd arg (mvar) is right */
1418 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1420 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1422 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1423 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1426 /* unlocks the MVar in the SMP case */
1427 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1429 JMP_(ENTRY_CODE(Sp[0]));
1431 /* No further takes, the MVar is now full. */
1432 mvar->value = R2.cl;
1433 /* unlocks the MVar in the SMP case */
1434 SET_INFO(mvar,&stg_FULL_MVAR_info);
1435 JMP_(ENTRY_CODE(Sp[0]));
1438 /* ToDo: yield afterward for better communication performance? */
1442 /* -----------------------------------------------------------------------------
1443 Stable pointer primitives
1444 ------------------------------------------------------------------------- */
1446 FN_(makeStableNamezh_fast)
1449 StgStableName *sn_obj;
1452 HP_CHK_GEN_TICKY(sizeofW(StgStableName), R1_PTR, makeStableNamezh_fast,);
1453 TICK_ALLOC_PRIM(sizeofW(StgHeader),
1454 sizeofW(StgStableName)-sizeofW(StgHeader), 0);
1455 CCS_ALLOC(CCCS,sizeofW(StgStableName)); /* ccs prof */
1457 index = RET_STGCALL1(StgWord,lookupStableName,R1.p);
1459 /* Is there already a StableName for this heap object? */
1460 if (stable_ptr_table[index].sn_obj == NULL) {
1461 sn_obj = (StgStableName *) (Hp - sizeofW(StgStableName) + 1);
1462 SET_HDR(sn_obj,&stg_STABLE_NAME_info,CCCS);
1464 stable_ptr_table[index].sn_obj = (StgClosure *)sn_obj;
1466 (StgClosure *)sn_obj = stable_ptr_table[index].sn_obj;
1469 TICK_RET_UNBOXED_TUP(1);
1474 FN_(makeStablePtrzh_fast)
1479 MAYBE_GC(R1_PTR, makeStablePtrzh_fast);
1480 sp = RET_STGCALL1(StgStablePtr,getStablePtr,R1.p);
1485 FN_(deRefStablePtrzh_fast)
1487 /* Args: R1 = the stable ptr */
1491 sp = (StgStablePtr)R1.w;
1492 r = stable_ptr_table[(StgWord)sp].addr;
1497 /* -----------------------------------------------------------------------------
1498 Bytecode object primitives
1499 ------------------------------------------------------------------------- */
1511 HP_CHK_GEN_TICKY(sizeofW(StgBCO),R1_PTR|R2_PTR|R3_PTR|R4_PTR, newBCOzh_fast,);
1512 TICK_ALLOC_PRIM(sizeofW(StgHeader), sizeofW(StgBCO)-sizeofW(StgHeader), 0);
1513 CCS_ALLOC(CCCS,sizeofW(StgBCO)); /* ccs prof */
1514 bco = (StgBCO *) (Hp + 1 - sizeofW(StgBCO));
1515 SET_HDR(bco, &stg_BCO_info, CCCS);
1517 bco->instrs = (StgArrWords*)R1.cl;
1518 bco->literals = (StgArrWords*)R2.cl;
1519 bco->ptrs = (StgMutArrPtrs*)R3.cl;
1520 bco->itbls = (StgArrWords*)R4.cl;
1522 TICK_RET_UNBOXED_TUP(1);
1527 FN_(mkApUpd0zh_fast)
1529 /* R1.p = the fn for the AP_UPD
1533 HP_CHK_GEN_TICKY(AP_sizeW(0), R1_PTR, mkApUpd0zh_fast,);
1534 TICK_ALLOC_PRIM(sizeofW(StgHeader), AP_sizeW(0)-sizeofW(StgHeader), 0);
1535 CCS_ALLOC(CCCS,AP_sizeW(0)); /* ccs prof */
1536 ap = (StgAP_UPD *) (Hp + 1 - AP_sizeW(0));
1537 SET_HDR(ap, &stg_AP_UPD_info, CCCS);
1542 TICK_RET_UNBOXED_TUP(1);
1547 /* -----------------------------------------------------------------------------
1548 Thread I/O blocking primitives
1549 -------------------------------------------------------------------------- */
1551 FN_(waitReadzh_fast)
1555 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1556 CurrentTSO->why_blocked = BlockedOnRead;
1557 CurrentTSO->block_info.fd = R1.i;
1558 ACQUIRE_LOCK(&sched_mutex);
1559 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1560 RELEASE_LOCK(&sched_mutex);
1561 JMP_(stg_block_noregs);
1565 FN_(waitWritezh_fast)
1569 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1570 CurrentTSO->why_blocked = BlockedOnWrite;
1571 CurrentTSO->block_info.fd = R1.i;
1572 ACQUIRE_LOCK(&sched_mutex);
1573 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1574 RELEASE_LOCK(&sched_mutex);
1575 JMP_(stg_block_noregs);
1585 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1586 CurrentTSO->why_blocked = BlockedOnDelay;
1588 ACQUIRE_LOCK(&sched_mutex);
1590 target = (R1.i / (TICK_MILLISECS*1000)) + getourtimeofday();
1591 CurrentTSO->block_info.target = target;
1593 /* Insert the new thread in the sleeping queue. */
1596 while (t != END_TSO_QUEUE && t->block_info.target < target) {
1601 CurrentTSO->link = t;
1603 sleeping_queue = CurrentTSO;
1605 prev->link = CurrentTSO;
1608 RELEASE_LOCK(&sched_mutex);
1609 JMP_(stg_block_noregs);