1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.94 2002/03/02 17:40:24 sof 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
332 -------------------------------------------------------------------------- */
334 FN_(mkForeignObjzh_fast)
336 /* R1.p = ptr to foreign object,
338 StgForeignObj *result;
341 HP_CHK_GEN_TICKY(sizeofW(StgForeignObj), NO_PTRS, mkForeignObjzh_fast,);
342 TICK_ALLOC_PRIM(sizeofW(StgHeader),
343 sizeofW(StgForeignObj)-sizeofW(StgHeader), 0);
344 CCS_ALLOC(CCCS,sizeofW(StgForeignObj)); /* ccs prof */
346 result = (StgForeignObj *) (Hp + 1 - sizeofW(StgForeignObj));
347 SET_HDR(result,&stg_FOREIGN_info,CCCS);
350 /* returns (# s#, ForeignObj# #) */
351 TICK_RET_UNBOXED_TUP(1);
356 /* These two are out-of-line for the benefit of the NCG */
357 FN_(unsafeThawArrayzh_fast)
360 SET_INFO((StgClosure *)R1.cl,&stg_MUT_ARR_PTRS_info);
361 recordMutable((StgMutClosure*)R1.cl);
363 TICK_RET_UNBOXED_TUP(1);
368 /* -----------------------------------------------------------------------------
369 Weak Pointer Primitives
370 -------------------------------------------------------------------------- */
376 R3.p = finalizer (or NULL)
382 R3.cl = &stg_NO_FINALIZER_closure;
385 HP_CHK_GEN_TICKY(sizeofW(StgWeak),R1_PTR|R2_PTR|R3_PTR, mkWeakzh_fast,);
386 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1, // +1 is for the link field
387 sizeofW(StgWeak)-sizeofW(StgHeader)-1, 0);
388 CCS_ALLOC(CCCS,sizeofW(StgWeak)); /* ccs prof */
390 w = (StgWeak *) (Hp + 1 - sizeofW(StgWeak));
391 SET_HDR(w, &stg_WEAK_info, CCCS);
395 w->finalizer = R3.cl;
397 w->link = weak_ptr_list;
399 IF_DEBUG(weak, fprintf(stderr,"New weak pointer at %p\n",w));
401 TICK_RET_UNBOXED_TUP(1);
406 FN_(finalizzeWeakzh_fast)
413 TICK_RET_UNBOXED_TUP(0);
414 w = (StgDeadWeak *)R1.p;
417 if (w->header.info == &stg_DEAD_WEAK_info) {
418 RET_NP(0,&stg_NO_FINALIZER_closure);
424 // A weak pointer is inherently used, so we do not need to call
425 // LDV_recordDead_FILL_SLOP_DYNAMIC():
426 // LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)w);
427 // or, LDV_recordDead():
428 // LDV_recordDead((StgClosure *)w, sizeofW(StgWeak) - sizeofW(StgProfHeader));
429 // Furthermore, when PROFILING is turned on, dead weak pointers are exactly as
430 // large as weak pointers, so there is no need to fill the slop, either.
431 // See stg_DEAD_WEAK_info in StgMiscClosures.hc.
434 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
436 w->header.info = &stg_DEAD_WEAK_info;
439 LDV_recordCreate((StgClosure *)w);
441 f = ((StgWeak *)w)->finalizer;
442 w->link = ((StgWeak *)w)->link;
444 /* return the finalizer */
445 if (f == &stg_NO_FINALIZER_closure) {
446 RET_NP(0,&stg_NO_FINALIZER_closure);
453 FN_(deRefWeakzh_fast)
455 /* R1.p = weak ptr */
461 if (w->header.info == &stg_WEAK_info) {
463 val = (P_)((StgWeak *)w)->value;
472 /* -----------------------------------------------------------------------------
473 Arbitrary-precision Integer operations.
474 -------------------------------------------------------------------------- */
476 FN_(int2Integerzh_fast)
478 /* arguments: R1 = Int# */
480 I_ val, s; /* to avoid aliasing */
481 StgArrWords* p; /* address of array result */
485 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, int2Integerzh_fast,);
486 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
487 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
489 p = (StgArrWords *)Hp - 1;
490 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
492 /* mpz_set_si is inlined here, makes things simpler */
496 } else if (val > 0) {
503 /* returns (# size :: Int#,
507 TICK_RET_UNBOXED_TUP(2);
512 FN_(word2Integerzh_fast)
514 /* arguments: R1 = Word# */
516 W_ val; /* to avoid aliasing */
518 StgArrWords* p; /* address of array result */
522 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, word2Integerzh_fast,)
523 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
524 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
526 p = (StgArrWords *)Hp - 1;
527 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
536 /* returns (# size :: Int#,
540 TICK_RET_UNBOXED_TUP(2);
547 * 'long long' primops for converting to/from Integers.
550 #ifdef SUPPORT_LONG_LONGS
552 FN_(int64ToIntegerzh_fast)
554 /* arguments: L1 = Int64# */
556 StgInt64 val; /* to avoid aliasing */
558 I_ s, neg, words_needed;
559 StgArrWords* p; /* address of array result */
565 if ( val >= 0x100000000LL || val <= -0x100000000LL ) {
568 /* minimum is one word */
571 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, int64ToIntegerzh_fast,)
572 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
573 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
575 p = (StgArrWords *)(Hp-words_needed+1) - 1;
576 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
583 hi = (W_)((LW_)val / 0x100000000ULL);
585 if ( words_needed == 2 ) {
589 } else if ( val != 0 ) {
592 } else /* val==0 */ {
595 s = ( neg ? -s : s );
597 /* returns (# size :: Int#,
601 TICK_RET_UNBOXED_TUP(2);
606 FN_(word64ToIntegerzh_fast)
608 /* arguments: L1 = Word64# */
610 StgWord64 val; /* to avoid aliasing */
613 StgArrWords* p; /* address of array result */
617 if ( val >= 0x100000000ULL ) {
622 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, word64ToIntegerzh_fast,)
623 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
624 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
626 p = (StgArrWords *)(Hp-words_needed+1) - 1;
627 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
629 hi = (W_)((LW_)val / 0x100000000ULL);
630 if ( val >= 0x100000000ULL ) {
634 } else if ( val != 0 ) {
637 } else /* val==0 */ {
641 /* returns (# size :: Int#,
645 TICK_RET_UNBOXED_TUP(2);
651 #endif /* SUPPORT_LONG_LONGS */
653 /* ToDo: this is shockingly inefficient */
655 #define GMP_TAKE2_RET1(name,mp_fun) \
658 MP_INT arg1, arg2, result; \
664 /* call doYouWantToGC() */ \
665 MAYBE_GC(R2_PTR | R4_PTR, name); \
667 d1 = (StgArrWords *)R2.p; \
669 d2 = (StgArrWords *)R4.p; \
672 arg1._mp_alloc = d1->words; \
673 arg1._mp_size = (s1); \
674 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
675 arg2._mp_alloc = d2->words; \
676 arg2._mp_size = (s2); \
677 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
679 STGCALL1(mpz_init,&result); \
681 /* Perform the operation */ \
682 STGCALL3(mp_fun,&result,&arg1,&arg2); \
684 TICK_RET_UNBOXED_TUP(2); \
685 RET_NP(result._mp_size, \
686 result._mp_d-sizeofW(StgArrWords)); \
690 #define GMP_TAKE1_RET1(name,mp_fun) \
693 MP_INT arg1, result; \
698 /* call doYouWantToGC() */ \
699 MAYBE_GC(R2_PTR, name); \
701 d1 = (StgArrWords *)R2.p; \
704 arg1._mp_alloc = d1->words; \
705 arg1._mp_size = (s1); \
706 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
708 STGCALL1(mpz_init,&result); \
710 /* Perform the operation */ \
711 STGCALL2(mp_fun,&result,&arg1); \
713 TICK_RET_UNBOXED_TUP(2); \
714 RET_NP(result._mp_size, \
715 result._mp_d-sizeofW(StgArrWords)); \
719 #define GMP_TAKE2_RET2(name,mp_fun) \
722 MP_INT arg1, arg2, result1, result2; \
728 /* call doYouWantToGC() */ \
729 MAYBE_GC(R2_PTR | R4_PTR, name); \
731 d1 = (StgArrWords *)R2.p; \
733 d2 = (StgArrWords *)R4.p; \
736 arg1._mp_alloc = d1->words; \
737 arg1._mp_size = (s1); \
738 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
739 arg2._mp_alloc = d2->words; \
740 arg2._mp_size = (s2); \
741 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
743 STGCALL1(mpz_init,&result1); \
744 STGCALL1(mpz_init,&result2); \
746 /* Perform the operation */ \
747 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
749 TICK_RET_UNBOXED_TUP(4); \
750 RET_NPNP(result1._mp_size, \
751 result1._mp_d-sizeofW(StgArrWords), \
753 result2._mp_d-sizeofW(StgArrWords)); \
757 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add);
758 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub);
759 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul);
760 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd);
761 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q);
762 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r);
763 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact);
764 GMP_TAKE2_RET1(andIntegerzh_fast, mpz_and);
765 GMP_TAKE2_RET1(orIntegerzh_fast, mpz_ior);
766 GMP_TAKE2_RET1(xorIntegerzh_fast, mpz_xor);
767 GMP_TAKE1_RET1(complementIntegerzh_fast, mpz_com);
769 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr);
770 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr);
775 /* R1 = the first Int#; R2 = the second Int# */
779 aa = (mp_limb_t)(R1.i);
780 r = RET_STGCALL3(StgInt, mpn_gcd_1, (mp_limb_t *)(&aa), 1, (mp_limb_t)(R2.i));
783 /* Result parked in R1, return via info-pointer at TOS */
784 JMP_(ENTRY_CODE(Sp[0]));
788 FN_(gcdIntegerIntzh_fast)
790 /* R1 = s1; R2 = d1; R3 = the int */
793 r = RET_STGCALL3(StgInt,mpn_gcd_1,(mp_limb_t *)(BYTE_ARR_CTS(R2.p)), R1.i, R3.i);
796 /* Result parked in R1, return via info-pointer at TOS */
797 JMP_(ENTRY_CODE(Sp[0]));
801 FN_(cmpIntegerIntzh_fast)
803 /* R1 = s1; R2 = d1; R3 = the int */
814 // paraphrased from mpz_cmp_si() in the GMP sources
817 } else if (v_digit < 0) {
822 if (usize != vsize) {
823 R1.i = usize - vsize; JMP_(ENTRY_CODE(Sp[0]));
827 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
830 u_digit = *(mp_limb_t *)(BYTE_ARR_CTS(R2.p));
832 if (u_digit == (mp_limb_t) (unsigned long) v_digit) {
833 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
836 if (u_digit > (mp_limb_t) (unsigned long) v_digit) {
842 JMP_(ENTRY_CODE(Sp[0]));
846 FN_(cmpIntegerzh_fast)
848 /* R1 = s1; R2 = d1; R3 = s2; R4 = d2 */
856 // paraphrased from mpz_cmp() in the GMP sources
860 if (usize != vsize) {
861 R1.i = usize - vsize; JMP_(ENTRY_CODE(Sp[0]));
865 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
870 up = BYTE_ARR_CTS(R2.p);
871 vp = BYTE_ARR_CTS(R4.p);
873 cmp = RET_STGCALL3(I_, mpn_cmp, (mp_limb_t *)up, (mp_limb_t *)vp, size);
876 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
879 if ((cmp < 0) == (usize < 0)) {
884 /* Result parked in R1, return via info-pointer at TOS */
885 JMP_(ENTRY_CODE(Sp[0]));
889 FN_(integer2Intzh_fast)
898 r = ((mp_limb_t *) (BYTE_ARR_CTS(R2.p)))[0];
901 /* Result parked in R1, return via info-pointer at TOS */
903 JMP_(ENTRY_CODE(Sp[0]));
907 FN_(integer2Wordzh_fast)
917 r = ((mp_limb_t *) (BYTE_ARR_CTS(R2.p)))[0];
920 /* Result parked in R1, return via info-pointer at TOS */
922 JMP_(ENTRY_CODE(Sp[0]));
927 FN_(decodeFloatzh_fast)
935 /* arguments: F1 = Float# */
938 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, decodeFloatzh_fast,);
939 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
940 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
942 /* Be prepared to tell Lennart-coded __decodeFloat */
943 /* where mantissa._mp_d can be put (it does not care about the rest) */
944 p = (StgArrWords *)Hp - 1;
945 SET_ARR_HDR(p,&stg_ARR_WORDS_info,CCCS,1)
946 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
948 /* Perform the operation */
949 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
951 /* returns: (Int# (expn), Int#, ByteArray#) */
952 TICK_RET_UNBOXED_TUP(3);
953 RET_NNP(exponent,mantissa._mp_size,p);
957 #define DOUBLE_MANTISSA_SIZE (sizeofW(StgDouble))
958 #define ARR_SIZE (sizeofW(StgArrWords) + DOUBLE_MANTISSA_SIZE)
960 FN_(decodeDoublezh_fast)
967 /* arguments: D1 = Double# */
970 HP_CHK_GEN_TICKY(ARR_SIZE, NO_PTRS, decodeDoublezh_fast,);
971 TICK_ALLOC_PRIM(sizeofW(StgArrWords),DOUBLE_MANTISSA_SIZE,0);
972 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
974 /* Be prepared to tell Lennart-coded __decodeDouble */
975 /* where mantissa.d can be put (it does not care about the rest) */
976 p = (StgArrWords *)(Hp-ARR_SIZE+1);
977 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
978 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
980 /* Perform the operation */
981 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
983 /* returns: (Int# (expn), Int#, ByteArray#) */
984 TICK_RET_UNBOXED_TUP(3);
985 RET_NNP(exponent,mantissa._mp_size,p);
989 /* -----------------------------------------------------------------------------
990 * Concurrency primitives
991 * -------------------------------------------------------------------------- */
996 /* args: R1 = closure to spark */
998 MAYBE_GC(R1_PTR, forkzh_fast);
1000 /* create it right now, return ThreadID in R1 */
1001 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
1002 RtsFlags.GcFlags.initialStkSize, R1.cl);
1003 STGCALL1(scheduleThread, R1.t);
1005 /* switch at the earliest opportunity */
1015 JMP_(stg_yield_noregs);
1019 FN_(myThreadIdzh_fast)
1023 RET_P((P_)CurrentTSO);
1030 /* -----------------------------------------------------------------------------
1033 * take & putMVar work as follows. Firstly, an important invariant:
1035 * If the MVar is full, then the blocking queue contains only
1036 * threads blocked on putMVar, and if the MVar is empty then the
1037 * blocking queue contains only threads blocked on takeMVar.
1040 * MVar empty : then add ourselves to the blocking queue
1041 * MVar full : remove the value from the MVar, and
1042 * blocking queue empty : return
1043 * blocking queue non-empty : perform the first blocked putMVar
1044 * from the queue, and wake up the
1045 * thread (MVar is now full again)
1047 * putMVar is just the dual of the above algorithm.
1049 * How do we "perform a putMVar"? Well, we have to fiddle around with
1050 * the stack of the thread waiting to do the putMVar. See
1051 * stg_block_putmvar and stg_block_takemvar in HeapStackCheck.c for
1052 * the stack layout, and the PerformPut and PerformTake macros below.
1054 * It is important that a blocked take or put is woken up with the
1055 * take/put already performed, because otherwise there would be a
1056 * small window of vulnerability where the thread could receive an
1057 * exception and never perform its take or put, and we'd end up with a
1060 * -------------------------------------------------------------------------- */
1062 FN_(isEmptyMVarzh_fast)
1064 /* args: R1 = MVar closure */
1067 r = (I_)((GET_INFO((StgMVar*)(R1.p))) == &stg_EMPTY_MVAR_info);
1080 HP_CHK_GEN_TICKY(sizeofW(StgMVar), NO_PTRS, newMVarzh_fast,);
1081 TICK_ALLOC_PRIM(sizeofW(StgMutVar)-1, // consider head,tail,link as admin wds
1083 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
1085 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
1086 SET_HDR(mvar,&stg_EMPTY_MVAR_info,CCCS);
1087 mvar->head = mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1088 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1090 TICK_RET_UNBOXED_TUP(1);
1095 /* If R1 isn't available, pass it on the stack */
1097 #define PerformTake(tso, value) ({ \
1098 (tso)->sp[1] = (W_)value; \
1099 (tso)->sp[0] = (W_)&stg_gc_unpt_r1_info; \
1102 #define PerformTake(tso, value) ({ \
1103 (tso)->sp[1] = (W_)value; \
1104 (tso)->sp[0] = (W_)&stg_ut_1_0_unreg_info; \
1109 #define PerformPut(tso) ({ \
1110 StgClosure *val = (StgClosure *)(tso)->sp[2]; \
1111 (tso)->sp[2] = (W_)&stg_gc_noregs_info; \
1116 FN_(takeMVarzh_fast)
1120 const StgInfoTable *info;
1123 /* args: R1 = MVar closure */
1125 mvar = (StgMVar *)R1.p;
1128 info = LOCK_CLOSURE(mvar);
1130 info = GET_INFO(mvar);
1133 /* If the MVar is empty, put ourselves on its blocking queue,
1134 * and wait until we're woken up.
1136 if (info == &stg_EMPTY_MVAR_info) {
1137 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1138 mvar->head = CurrentTSO;
1140 mvar->tail->link = CurrentTSO;
1142 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1143 CurrentTSO->why_blocked = BlockedOnMVar;
1144 CurrentTSO->block_info.closure = (StgClosure *)mvar;
1145 mvar->tail = CurrentTSO;
1148 /* unlock the MVar */
1149 mvar->header.info = &stg_EMPTY_MVAR_info;
1151 JMP_(stg_block_takemvar);
1154 /* we got the value... */
1157 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1158 /* There are putMVar(s) waiting...
1159 * wake up the first thread on the queue
1161 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1163 /* actually perform the putMVar for the thread that we just woke up */
1164 mvar->value = PerformPut(mvar->head);
1166 #if defined(GRAN) || defined(PAR)
1167 /* ToDo: check 2nd arg (mvar) is right */
1168 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1170 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1172 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1173 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1176 /* unlock in the SMP case */
1177 SET_INFO(mvar,&stg_FULL_MVAR_info);
1179 TICK_RET_UNBOXED_TUP(1);
1182 /* No further putMVars, MVar is now empty */
1184 /* do this last... we might have locked the MVar in the SMP case,
1185 * and writing the info pointer will unlock it.
1187 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1188 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1189 TICK_RET_UNBOXED_TUP(1);
1195 FN_(tryTakeMVarzh_fast)
1199 const StgInfoTable *info;
1202 /* args: R1 = MVar closure */
1204 mvar = (StgMVar *)R1.p;
1207 info = LOCK_CLOSURE(mvar);
1209 info = GET_INFO(mvar);
1212 if (info == &stg_EMPTY_MVAR_info) {
1215 /* unlock the MVar */
1216 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1219 /* HACK: we need a pointer to pass back,
1220 * so we abuse NO_FINALIZER_closure
1222 RET_NP(0, &stg_NO_FINALIZER_closure);
1225 /* we got the value... */
1228 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1229 /* There are putMVar(s) waiting...
1230 * wake up the first thread on the queue
1232 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1234 /* actually perform the putMVar for the thread that we just woke up */
1235 mvar->value = PerformPut(mvar->head);
1237 #if defined(GRAN) || defined(PAR)
1238 /* ToDo: check 2nd arg (mvar) is right */
1239 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1241 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1243 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1244 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1247 /* unlock in the SMP case */
1248 SET_INFO(mvar,&stg_FULL_MVAR_info);
1251 /* No further putMVars, MVar is now empty */
1252 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1254 /* do this last... we might have locked the MVar in the SMP case,
1255 * and writing the info pointer will unlock it.
1257 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1260 TICK_RET_UNBOXED_TUP(1);
1268 const StgInfoTable *info;
1271 /* args: R1 = MVar, R2 = value */
1273 mvar = (StgMVar *)R1.p;
1276 info = LOCK_CLOSURE(mvar);
1278 info = GET_INFO(mvar);
1281 if (info == &stg_FULL_MVAR_info) {
1282 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1283 mvar->head = CurrentTSO;
1285 mvar->tail->link = CurrentTSO;
1287 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1288 CurrentTSO->why_blocked = BlockedOnMVar;
1289 CurrentTSO->block_info.closure = (StgClosure *)mvar;
1290 mvar->tail = CurrentTSO;
1293 /* unlock the MVar */
1294 SET_INFO(mvar,&stg_FULL_MVAR_info);
1296 JMP_(stg_block_putmvar);
1299 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1300 /* There are takeMVar(s) waiting: wake up the first one
1302 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1304 /* actually perform the takeMVar */
1305 PerformTake(mvar->head, R2.cl);
1307 #if defined(GRAN) || defined(PAR)
1308 /* ToDo: check 2nd arg (mvar) is right */
1309 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1311 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1313 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1314 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1317 /* unlocks the MVar in the SMP case */
1318 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1320 JMP_(ENTRY_CODE(Sp[0]));
1322 /* No further takes, the MVar is now full. */
1323 mvar->value = R2.cl;
1324 /* unlocks the MVar in the SMP case */
1325 SET_INFO(mvar,&stg_FULL_MVAR_info);
1326 JMP_(ENTRY_CODE(Sp[0]));
1329 /* ToDo: yield afterward for better communication performance? */
1333 FN_(tryPutMVarzh_fast)
1336 const StgInfoTable *info;
1339 /* args: R1 = MVar, R2 = value */
1341 mvar = (StgMVar *)R1.p;
1344 info = LOCK_CLOSURE(mvar);
1346 info = GET_INFO(mvar);
1349 if (info == &stg_FULL_MVAR_info) {
1352 /* unlock the MVar */
1353 mvar->header.info = &stg_FULL_MVAR_info;
1359 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1360 /* There are takeMVar(s) waiting: wake up the first one
1362 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1364 /* actually perform the takeMVar */
1365 PerformTake(mvar->head, R2.cl);
1367 #if defined(GRAN) || defined(PAR)
1368 /* ToDo: check 2nd arg (mvar) is right */
1369 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1371 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1373 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1374 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1377 /* unlocks the MVar in the SMP case */
1378 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1380 JMP_(ENTRY_CODE(Sp[0]));
1382 /* No further takes, the MVar is now full. */
1383 mvar->value = R2.cl;
1384 /* unlocks the MVar in the SMP case */
1385 SET_INFO(mvar,&stg_FULL_MVAR_info);
1386 JMP_(ENTRY_CODE(Sp[0]));
1389 /* ToDo: yield afterward for better communication performance? */
1393 /* -----------------------------------------------------------------------------
1394 Stable pointer primitives
1395 ------------------------------------------------------------------------- */
1397 FN_(makeStableNamezh_fast)
1400 StgStableName *sn_obj;
1403 HP_CHK_GEN_TICKY(sizeofW(StgStableName), R1_PTR, makeStableNamezh_fast,);
1404 TICK_ALLOC_PRIM(sizeofW(StgHeader),
1405 sizeofW(StgStableName)-sizeofW(StgHeader), 0);
1406 CCS_ALLOC(CCCS,sizeofW(StgStableName)); /* ccs prof */
1408 index = RET_STGCALL1(StgWord,lookupStableName,R1.p);
1410 /* Is there already a StableName for this heap object? */
1411 if (stable_ptr_table[index].sn_obj == NULL) {
1412 sn_obj = (StgStableName *) (Hp - sizeofW(StgStableName) + 1);
1413 SET_HDR(sn_obj,&stg_STABLE_NAME_info,CCCS);
1415 stable_ptr_table[index].sn_obj = (StgClosure *)sn_obj;
1417 (StgClosure *)sn_obj = stable_ptr_table[index].sn_obj;
1420 TICK_RET_UNBOXED_TUP(1);
1425 FN_(makeStablePtrzh_fast)
1430 MAYBE_GC(R1_PTR, makeStablePtrzh_fast);
1431 sp = RET_STGCALL1(StgStablePtr,getStablePtr,R1.p);
1436 FN_(deRefStablePtrzh_fast)
1438 /* Args: R1 = the stable ptr */
1442 sp = (StgStablePtr)R1.w;
1443 r = stable_ptr_table[(StgWord)sp].addr;
1448 /* -----------------------------------------------------------------------------
1449 Bytecode object primitives
1450 ------------------------------------------------------------------------- */
1462 HP_CHK_GEN_TICKY(sizeofW(StgBCO),R1_PTR|R2_PTR|R3_PTR|R4_PTR, newBCOzh_fast,);
1463 TICK_ALLOC_PRIM(sizeofW(StgHeader), sizeofW(StgBCO)-sizeofW(StgHeader), 0);
1464 CCS_ALLOC(CCCS,sizeofW(StgBCO)); /* ccs prof */
1465 bco = (StgBCO *) (Hp + 1 - sizeofW(StgBCO));
1466 SET_HDR(bco, &stg_BCO_info, CCCS);
1468 bco->instrs = (StgArrWords*)R1.cl;
1469 bco->literals = (StgArrWords*)R2.cl;
1470 bco->ptrs = (StgMutArrPtrs*)R3.cl;
1471 bco->itbls = (StgArrWords*)R4.cl;
1473 TICK_RET_UNBOXED_TUP(1);
1478 FN_(mkApUpd0zh_fast)
1480 /* R1.p = the fn for the AP_UPD
1484 HP_CHK_GEN_TICKY(AP_sizeW(0), R1_PTR, mkApUpd0zh_fast,);
1485 TICK_ALLOC_PRIM(sizeofW(StgHeader), AP_sizeW(0)-sizeofW(StgHeader), 0);
1486 CCS_ALLOC(CCCS,AP_sizeW(0)); /* ccs prof */
1487 ap = (StgAP_UPD *) (Hp + 1 - AP_sizeW(0));
1488 SET_HDR(ap, &stg_AP_UPD_info, CCCS);
1493 TICK_RET_UNBOXED_TUP(1);
1498 /* -----------------------------------------------------------------------------
1499 Thread I/O blocking primitives
1500 -------------------------------------------------------------------------- */
1502 FN_(waitReadzh_fast)
1506 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1507 CurrentTSO->why_blocked = BlockedOnRead;
1508 CurrentTSO->block_info.fd = R1.i;
1509 ACQUIRE_LOCK(&sched_mutex);
1510 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1511 RELEASE_LOCK(&sched_mutex);
1512 JMP_(stg_block_noregs);
1516 FN_(waitWritezh_fast)
1520 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1521 CurrentTSO->why_blocked = BlockedOnWrite;
1522 CurrentTSO->block_info.fd = R1.i;
1523 ACQUIRE_LOCK(&sched_mutex);
1524 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1525 RELEASE_LOCK(&sched_mutex);
1526 JMP_(stg_block_noregs);
1536 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1537 CurrentTSO->why_blocked = BlockedOnDelay;
1539 ACQUIRE_LOCK(&sched_mutex);
1541 target = (R1.i / (TICK_MILLISECS*1000)) + getourtimeofday();
1542 CurrentTSO->block_info.target = target;
1544 /* Insert the new thread in the sleeping queue. */
1547 while (t != END_TSO_QUEUE && t->block_info.target < target) {
1552 CurrentTSO->link = t;
1554 sleeping_queue = CurrentTSO;
1556 prev->link = CurrentTSO;
1559 RELEASE_LOCK(&sched_mutex);
1560 JMP_(stg_block_noregs);