1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.111 2003/09/02 09:20:05 simonmar Exp $
4 * (c) The GHC Team, 1998-2002
6 * Primitive functions / data
8 * ---------------------------------------------------------------------------*/
14 #include "StgStartup.h"
19 #include "BlockAlloc.h" /* tmp */
20 #include "StablePriv.h"
22 #include "Timer.h" /* TICK_MILLISECS */
24 #ifndef mingw32_TARGET_OS
25 #include "Itimer.h" /* getourtimeofday() */
28 #ifdef HAVE_SYS_TYPES_H
29 # include <sys/types.h>
34 #ifdef mingw32_TARGET_OS
36 #include "win32/AsyncIO.h"
41 classes CCallable and CReturnable don't really exist, but the
42 compiler insists on generating dictionaries containing references
43 to GHC_ZcCCallable_static_info etc., so we provide dummy symbols
44 for these. Some C compilers can't cope with zero-length static arrays,
45 so we have to make these one element long.
48 StgWord GHC_ZCCCallable_static_info[1];
49 StgWord GHC_ZCCReturnable_static_info[1];
51 /* -----------------------------------------------------------------------------
52 Macros for Hand-written primitives.
53 -------------------------------------------------------------------------- */
56 * Horrible macros for returning unboxed tuples.
58 * How an unboxed tuple is returned depends on two factors:
59 * - the number of real registers we have available
60 * - the boxedness of the returned fields.
62 * To return an unboxed tuple from a primitive operation, we have macros
63 * RET_<layout> where <layout> describes the boxedness of each field of the
64 * unboxed tuple: N indicates a non-pointer field, and P indicates a pointer.
66 * We only define the cases actually used, to avoid having too much
67 * garbage in this section. Warning: any bugs in here will be hard to
70 * The return convention for an unboxed tuple is as follows:
71 * - fit as many fields as possible in registers (as per the
72 * function fast-entry point calling convention).
73 * - sort the rest of the fields into pointers and non-pointers.
74 * push the pointers on the stack, followed by the non-pointers.
75 * (so the pointers have higher addresses).
78 /*------ All Regs available */
79 #if MAX_REAL_VANILLA_REG == 8
80 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
81 # define RET_N(a) RET_P(a)
83 # define RET_PP(a,b) R1.w = (W_)(a); R2.w = (W_)(b); JMP_(ENTRY_CODE(Sp[0]));
84 # define RET_NN(a,b) RET_PP(a,b)
85 # define RET_NP(a,b) RET_PP(a,b)
87 # define RET_PPP(a,b,c) \
88 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); JMP_(ENTRY_CODE(Sp[0]));
89 # define RET_NNP(a,b,c) RET_PPP(a,b,c)
91 # define RET_NNNP(a,b,c,d) \
92 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); R4.w = (W_)d; \
93 JMP_(ENTRY_CODE(Sp[0]));
95 # define RET_NPNP(a,b,c,d) \
96 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); R4.w = (W_)(d); \
97 JMP_(ENTRY_CODE(Sp[0]));
99 #elif MAX_REAL_VANILLA_REG > 2 && MAX_REAL_VANILLA_REG < 8
100 # error RET_n macros not defined for this setup.
102 /*------ 2 Registers available */
103 #elif MAX_REAL_VANILLA_REG == 2
105 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
106 # define RET_N(a) RET_P(a)
108 # define RET_PP(a,b) R1.w = (W_)(a); R2.w = (W_)(b); \
109 JMP_(ENTRY_CODE(Sp[0]));
110 # define RET_NN(a,b) RET_PP(a,b)
111 # define RET_NP(a,b) RET_PP(a,b)
113 # define RET_PPP(a,b,c) \
118 JMP_(ENTRY_CODE(Sp[1]));
120 # define RET_NNP(a,b,c) \
125 JMP_(ENTRY_CODE(Sp[1]));
127 # define RET_NNNP(a,b,c,d) \
133 JMP_(ENTRY_CODE(Sp[2]));
135 # define RET_NPNP(a,b,c,d) \
141 JMP_(ENTRY_CODE(Sp[2]));
143 /*------ 1 Register available */
144 #elif MAX_REAL_VANILLA_REG == 1
145 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
146 # define RET_N(a) RET_P(a)
148 # define RET_PP(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 1; \
149 JMP_(ENTRY_CODE(Sp[1]));
150 # define RET_NN(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 2; \
151 JMP_(ENTRY_CODE(Sp[2]));
152 # define RET_NP(a,b) RET_PP(a,b)
154 # define RET_PPP(a,b,c) \
159 JMP_(ENTRY_CODE(Sp[2]));
161 # define RET_NNP(a,b,c) \
166 JMP_(ENTRY_CODE(Sp[2]));
168 # define RET_NNNP(a,b,c,d) \
174 JMP_(ENTRY_CODE(Sp[3]));
176 # define RET_NPNP(a,b,c,d) \
182 JMP_(ENTRY_CODE(Sp[3]));
184 #else /* 0 Regs available */
186 #define PUSH(o,x) Sp[-o] = (W_)(x)
188 #define PUSHED(m) Sp -= (m); JMP_(ENTRY_CODE(Sp[m]));
190 # define RET_P(a) PUSH(1,a); PUSHED(1)
191 # define RET_N(a) PUSH(1,a); PUSHED(1)
193 # define RET_PP(a,b) PUSH(2,a); PUSH(1,b); PUSHED(2)
194 # define RET_NN(a,b) PUSH(2,a); PUSH(1,b); PUSHED(2)
195 # define RET_NP(a,b) PUSH(2,a); PUSH(1,b); PUSHED(2)
197 # define RET_PPP(a,b,c) PUSH(3,a); PUSH(2,b); PUSH(1,c); PUSHED(3)
198 # define RET_NNP(a,b,c) PUSH(3,a); PUSH(2,b); PUSH(1,c); PUSHED(3)
200 # define RET_NNNP(a,b,c,d) PUSH(4,a); PUSH(3,b); PUSH(2,c); PUSH(1,d); PUSHED(4)
201 # define RET_NPNP(a,b,c,d) PUSH(4,a); PUSH(3,c); PUSH(2,b); PUSH(1,d); PUSHED(4)
204 /*-----------------------------------------------------------------------------
207 Basically just new*Array - the others are all inline macros.
209 The size arg is always passed in R1, and the result returned in R1.
211 The slow entry point is for returning from a heap check, the saved
212 size argument must be re-loaded from the stack.
213 -------------------------------------------------------------------------- */
215 /* for objects that are *less* than the size of a word, make sure we
216 * round up to the nearest word for the size of the array.
219 #define BYTES_TO_STGWORDS(n) ((n) + sizeof(W_) - 1)/sizeof(W_)
221 FN_(newByteArrayzh_fast)
223 W_ size, stuff_size, n;
226 MAYBE_GC(NO_PTRS,newByteArrayzh_fast);
228 stuff_size = BYTES_TO_STGWORDS(n);
229 size = sizeofW(StgArrWords)+ stuff_size;
230 p = (StgArrWords *)RET_STGCALL1(P_,allocate,size);
231 TICK_ALLOC_PRIM(sizeofW(StgArrWords),stuff_size,0);
232 SET_HDR(p, &stg_ARR_WORDS_info, CCCS);
233 p->words = stuff_size;
234 TICK_RET_UNBOXED_TUP(1)
239 FN_(newPinnedByteArrayzh_fast)
241 W_ size, stuff_size, n;
244 MAYBE_GC(NO_PTRS,newPinnedByteArrayzh_fast);
246 stuff_size = BYTES_TO_STGWORDS(n);
248 // We want an 8-byte aligned array. allocatePinned() gives us
249 // 8-byte aligned memory by default, but we want to align the
250 // *goods* inside the ArrWords object, so we have to check the
251 // size of the ArrWords header and adjust our size accordingly.
252 size = sizeofW(StgArrWords)+ stuff_size;
253 if ((sizeof(StgArrWords) & 7) != 0) {
257 p = (StgArrWords *)RET_STGCALL1(P_,allocatePinned,size);
258 TICK_ALLOC_PRIM(sizeofW(StgArrWords),stuff_size,0);
260 // Again, if the ArrWords header isn't a multiple of 8 bytes, we
261 // have to push the object forward one word so that the goods
262 // fall on an 8-byte boundary.
263 if ((sizeof(StgArrWords) & 7) != 0) {
267 SET_HDR(p, &stg_ARR_WORDS_info, CCCS);
268 p->words = stuff_size;
269 TICK_RET_UNBOXED_TUP(1)
282 MAYBE_GC(R2_PTR,newArrayzh_fast);
284 size = sizeofW(StgMutArrPtrs) + n;
285 arr = (StgMutArrPtrs *)RET_STGCALL1(P_, allocate, size);
286 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
288 SET_HDR(arr,&stg_MUT_ARR_PTRS_info,CCCS);
292 for (p = (P_)arr + sizeofW(StgMutArrPtrs);
293 p < (P_)arr + size; p++) {
297 TICK_RET_UNBOXED_TUP(1);
302 FN_(newMutVarzh_fast)
305 /* Args: R1.p = initialisation value */
308 HP_CHK_GEN_TICKY(sizeofW(StgMutVar), R1_PTR, newMutVarzh_fast);
309 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1,1, 0); /* hack, dependent on rep. */
310 CCS_ALLOC(CCCS,sizeofW(StgMutVar));
312 mv = (StgMutVar *)(Hp-sizeofW(StgMutVar)+1);
313 SET_HDR(mv,&stg_MUT_VAR_info,CCCS);
316 TICK_RET_UNBOXED_TUP(1);
321 FN_(atomicModifyMutVarzh_fast)
324 StgClosure *z, *x, *y, *r;
326 /* Args: R1.p :: MutVar#, R2.p :: a -> (a,b) */
328 /* If x is the current contents of the MutVar#, then
329 We want to make the new contents point to
333 and the return value is
337 obviously we can share (f x).
339 z = [stg_ap_2 f x] (max (HS + 2) MIN_UPD_SIZE)
340 y = [stg_sel_0 z] (max (HS + 1) MIN_UPD_SIZE)
341 r = [stg_sel_1 z] (max (HS + 1) MIN_UPD_SIZE)
344 #define THUNK_SIZE(n) (sizeofW(StgHeader) + stg_max((n), MIN_UPD_SIZE))
345 #define SIZE (THUNK_SIZE(2) + THUNK_SIZE(1) + THUNK_SIZE(1))
347 HP_CHK_GEN_TICKY(SIZE, R1_PTR|R2_PTR, atomicModifyMutVarzh_fast);
348 CCS_ALLOC(CCCS,SIZE);
350 x = ((StgMutVar *)R1.cl)->var;
352 TICK_ALLOC_UP_THK(2,0); // XXX
353 z = (StgClosure *) Hp - THUNK_SIZE(2) + 1;
354 SET_HDR(z, (StgInfoTable *)&stg_ap_2_upd_info, CCCS);
355 z->payload[0] = R2.cl;
358 TICK_ALLOC_UP_THK(1,1); // XXX
359 y = (StgClosure *) (StgPtr)z - THUNK_SIZE(1);
360 SET_HDR(y, &stg_sel_0_upd_info, CCCS);
363 ((StgMutVar *)R1.cl)->var = y;
365 TICK_ALLOC_UP_THK(1,1); // XXX
366 r = (StgClosure *) (StgPtr)y - THUNK_SIZE(1);
367 SET_HDR(r, &stg_sel_1_upd_info, CCCS);
374 /* -----------------------------------------------------------------------------
375 Foreign Object Primitives
376 -------------------------------------------------------------------------- */
378 FN_(mkForeignObjzh_fast)
380 /* R1.p = ptr to foreign object,
382 StgForeignObj *result;
385 HP_CHK_GEN_TICKY(sizeofW(StgForeignObj), NO_PTRS, mkForeignObjzh_fast);
386 TICK_ALLOC_PRIM(sizeofW(StgHeader),
387 sizeofW(StgForeignObj)-sizeofW(StgHeader), 0);
388 CCS_ALLOC(CCCS,sizeofW(StgForeignObj)); /* ccs prof */
390 result = (StgForeignObj *) (Hp + 1 - sizeofW(StgForeignObj));
391 SET_HDR(result,&stg_FOREIGN_info,CCCS);
394 /* returns (# s#, ForeignObj# #) */
395 TICK_RET_UNBOXED_TUP(1);
400 /* These two are out-of-line for the benefit of the NCG */
401 FN_(unsafeThawArrayzh_fast)
404 SET_INFO((StgClosure *)R1.cl,&stg_MUT_ARR_PTRS_info);
405 recordMutable((StgMutClosure*)R1.cl);
407 TICK_RET_UNBOXED_TUP(1);
412 /* -----------------------------------------------------------------------------
413 Weak Pointer Primitives
414 -------------------------------------------------------------------------- */
420 R3.p = finalizer (or NULL)
426 R3.cl = &stg_NO_FINALIZER_closure;
429 HP_CHK_GEN_TICKY(sizeofW(StgWeak),R1_PTR|R2_PTR|R3_PTR, mkWeakzh_fast);
430 TICK_ALLOC_PRIM(sizeofW(StgHeader)+1, // +1 is for the link field
431 sizeofW(StgWeak)-sizeofW(StgHeader)-1, 0);
432 CCS_ALLOC(CCCS,sizeofW(StgWeak)); /* ccs prof */
434 w = (StgWeak *) (Hp + 1 - sizeofW(StgWeak));
435 SET_HDR(w, &stg_WEAK_info, CCCS);
439 w->finalizer = R3.cl;
441 w->link = weak_ptr_list;
443 IF_DEBUG(weak, fprintf(stderr,"New weak pointer at %p\n",w));
445 TICK_RET_UNBOXED_TUP(1);
450 FN_(finalizzeWeakzh_fast)
457 TICK_RET_UNBOXED_TUP(0);
458 w = (StgDeadWeak *)R1.p;
461 if (w->header.info == &stg_DEAD_WEAK_info) {
462 RET_NP(0,&stg_NO_FINALIZER_closure);
468 // A weak pointer is inherently used, so we do not need to call
469 // LDV_recordDead_FILL_SLOP_DYNAMIC():
470 // LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)w);
471 // or, LDV_recordDead():
472 // LDV_recordDead((StgClosure *)w, sizeofW(StgWeak) - sizeofW(StgProfHeader));
473 // Furthermore, when PROFILING is turned on, dead weak pointers are exactly as
474 // large as weak pointers, so there is no need to fill the slop, either.
475 // See stg_DEAD_WEAK_info in StgMiscClosures.hc.
478 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
480 w->header.info = &stg_DEAD_WEAK_info;
483 LDV_recordCreate((StgClosure *)w);
485 f = ((StgWeak *)w)->finalizer;
486 w->link = ((StgWeak *)w)->link;
488 /* return the finalizer */
489 if (f == &stg_NO_FINALIZER_closure) {
490 RET_NP(0,&stg_NO_FINALIZER_closure);
497 FN_(deRefWeakzh_fast)
499 /* R1.p = weak ptr */
505 if (w->header.info == &stg_WEAK_info) {
507 val = (P_)((StgWeak *)w)->value;
516 /* -----------------------------------------------------------------------------
517 Arbitrary-precision Integer operations.
518 -------------------------------------------------------------------------- */
520 FN_(int2Integerzh_fast)
522 /* arguments: R1 = Int# */
524 I_ val, s; /* to avoid aliasing */
525 StgArrWords* p; /* address of array result */
529 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, int2Integerzh_fast);
530 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
531 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
533 p = (StgArrWords *)Hp - 1;
534 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
536 /* mpz_set_si is inlined here, makes things simpler */
540 } else if (val > 0) {
547 /* returns (# size :: Int#,
551 TICK_RET_UNBOXED_TUP(2);
556 FN_(word2Integerzh_fast)
558 /* arguments: R1 = Word# */
560 W_ val; /* to avoid aliasing */
562 StgArrWords* p; /* address of array result */
566 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, word2Integerzh_fast)
567 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
568 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
570 p = (StgArrWords *)Hp - 1;
571 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, 1);
580 /* returns (# size :: Int#,
584 TICK_RET_UNBOXED_TUP(2);
591 * 'long long' primops for converting to/from Integers.
594 #ifdef SUPPORT_LONG_LONGS
596 FN_(int64ToIntegerzh_fast)
598 /* arguments: L1 = Int64# */
600 StgInt64 val; /* to avoid aliasing */
602 I_ s, neg, words_needed;
603 StgArrWords* p; /* address of array result */
609 if ( val >= 0x100000000LL || val <= -0x100000000LL ) {
612 /* minimum is one word */
615 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, int64ToIntegerzh_fast)
616 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
617 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
619 p = (StgArrWords *)(Hp-words_needed+1) - 1;
620 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
627 hi = (W_)((LW_)val / 0x100000000ULL);
629 if ( words_needed == 2 ) {
633 } else if ( val != 0 ) {
636 } else /* val==0 */ {
639 s = ( neg ? -s : s );
641 /* returns (# size :: Int#,
645 TICK_RET_UNBOXED_TUP(2);
650 FN_(word64ToIntegerzh_fast)
652 /* arguments: L1 = Word64# */
654 StgWord64 val; /* to avoid aliasing */
657 StgArrWords* p; /* address of array result */
661 if ( val >= 0x100000000ULL ) {
666 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+words_needed, NO_PTRS, word64ToIntegerzh_fast)
667 TICK_ALLOC_PRIM(sizeofW(StgArrWords),words_needed,0);
668 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
670 p = (StgArrWords *)(Hp-words_needed+1) - 1;
671 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, words_needed);
673 hi = (W_)((LW_)val / 0x100000000ULL);
674 if ( val >= 0x100000000ULL ) {
678 } else if ( val != 0 ) {
681 } else /* val==0 */ {
685 /* returns (# size :: Int#,
689 TICK_RET_UNBOXED_TUP(2);
695 #endif /* SUPPORT_LONG_LONGS */
697 /* ToDo: this is shockingly inefficient */
699 #define GMP_TAKE2_RET1(name,mp_fun) \
702 MP_INT arg1, arg2, result; \
708 /* call doYouWantToGC() */ \
709 MAYBE_GC(R2_PTR | R4_PTR, name); \
711 d1 = (StgArrWords *)R2.p; \
713 d2 = (StgArrWords *)R4.p; \
716 arg1._mp_alloc = d1->words; \
717 arg1._mp_size = (s1); \
718 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
719 arg2._mp_alloc = d2->words; \
720 arg2._mp_size = (s2); \
721 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
723 STGCALL1(mpz_init,&result); \
725 /* Perform the operation */ \
726 STGCALL3(mp_fun,&result,&arg1,&arg2); \
728 TICK_RET_UNBOXED_TUP(2); \
729 RET_NP(result._mp_size, \
730 result._mp_d-sizeofW(StgArrWords)); \
734 #define GMP_TAKE1_RET1(name,mp_fun) \
737 MP_INT arg1, result; \
742 /* call doYouWantToGC() */ \
743 MAYBE_GC(R2_PTR, name); \
745 d1 = (StgArrWords *)R2.p; \
748 arg1._mp_alloc = d1->words; \
749 arg1._mp_size = (s1); \
750 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
752 STGCALL1(mpz_init,&result); \
754 /* Perform the operation */ \
755 STGCALL2(mp_fun,&result,&arg1); \
757 TICK_RET_UNBOXED_TUP(2); \
758 RET_NP(result._mp_size, \
759 result._mp_d-sizeofW(StgArrWords)); \
763 #define GMP_TAKE2_RET2(name,mp_fun) \
766 MP_INT arg1, arg2, result1, result2; \
772 /* call doYouWantToGC() */ \
773 MAYBE_GC(R2_PTR | R4_PTR, name); \
775 d1 = (StgArrWords *)R2.p; \
777 d2 = (StgArrWords *)R4.p; \
780 arg1._mp_alloc = d1->words; \
781 arg1._mp_size = (s1); \
782 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
783 arg2._mp_alloc = d2->words; \
784 arg2._mp_size = (s2); \
785 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
787 STGCALL1(mpz_init,&result1); \
788 STGCALL1(mpz_init,&result2); \
790 /* Perform the operation */ \
791 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
793 TICK_RET_UNBOXED_TUP(4); \
794 RET_NPNP(result1._mp_size, \
795 result1._mp_d-sizeofW(StgArrWords), \
797 result2._mp_d-sizeofW(StgArrWords)); \
801 GMP_TAKE2_RET1(plusIntegerzh_fast, mpz_add);
802 GMP_TAKE2_RET1(minusIntegerzh_fast, mpz_sub);
803 GMP_TAKE2_RET1(timesIntegerzh_fast, mpz_mul);
804 GMP_TAKE2_RET1(gcdIntegerzh_fast, mpz_gcd);
805 GMP_TAKE2_RET1(quotIntegerzh_fast, mpz_tdiv_q);
806 GMP_TAKE2_RET1(remIntegerzh_fast, mpz_tdiv_r);
807 GMP_TAKE2_RET1(divExactIntegerzh_fast, mpz_divexact);
808 GMP_TAKE2_RET1(andIntegerzh_fast, mpz_and);
809 GMP_TAKE2_RET1(orIntegerzh_fast, mpz_ior);
810 GMP_TAKE2_RET1(xorIntegerzh_fast, mpz_xor);
811 GMP_TAKE1_RET1(complementIntegerzh_fast, mpz_com);
813 GMP_TAKE2_RET2(quotRemIntegerzh_fast, mpz_tdiv_qr);
814 GMP_TAKE2_RET2(divModIntegerzh_fast, mpz_fdiv_qr);
819 /* R1 = the first Int#; R2 = the second Int# */
823 aa = (mp_limb_t)(R1.i);
824 r = RET_STGCALL3(StgInt, mpn_gcd_1, (mp_limb_t *)(&aa), 1, (mp_limb_t)(R2.i));
827 /* Result parked in R1, return via info-pointer at TOS */
828 JMP_(ENTRY_CODE(Sp[0]));
832 FN_(gcdIntegerIntzh_fast)
834 /* R1 = s1; R2 = d1; R3 = the int */
837 r = RET_STGCALL3(StgInt,mpn_gcd_1,(mp_limb_t *)(BYTE_ARR_CTS(R2.p)), R1.i, R3.i);
840 /* Result parked in R1, return via info-pointer at TOS */
841 JMP_(ENTRY_CODE(Sp[0]));
845 FN_(cmpIntegerIntzh_fast)
847 /* R1 = s1; R2 = d1; R3 = the int */
858 // paraphrased from mpz_cmp_si() in the GMP sources
861 } else if (v_digit < 0) {
866 if (usize != vsize) {
867 R1.i = usize - vsize; JMP_(ENTRY_CODE(Sp[0]));
871 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
874 u_digit = *(mp_limb_t *)(BYTE_ARR_CTS(R2.p));
876 if (u_digit == (mp_limb_t) (unsigned long) v_digit) {
877 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
880 if (u_digit > (mp_limb_t) (unsigned long) v_digit) {
886 JMP_(ENTRY_CODE(Sp[0]));
890 FN_(cmpIntegerzh_fast)
892 /* R1 = s1; R2 = d1; R3 = s2; R4 = d2 */
900 // paraphrased from mpz_cmp() in the GMP sources
904 if (usize != vsize) {
905 R1.i = usize - vsize; JMP_(ENTRY_CODE(Sp[0]));
909 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
914 up = BYTE_ARR_CTS(R2.p);
915 vp = BYTE_ARR_CTS(R4.p);
917 cmp = RET_STGCALL3(I_, mpn_cmp, (mp_limb_t *)up, (mp_limb_t *)vp, size);
920 R1.i = 0; JMP_(ENTRY_CODE(Sp[0]));
923 if ((cmp < 0) == (usize < 0)) {
928 /* Result parked in R1, return via info-pointer at TOS */
929 JMP_(ENTRY_CODE(Sp[0]));
933 FN_(integer2Intzh_fast)
942 r = ((mp_limb_t *) (BYTE_ARR_CTS(R2.p)))[0];
945 /* Result parked in R1, return via info-pointer at TOS */
947 JMP_(ENTRY_CODE(Sp[0]));
951 FN_(integer2Wordzh_fast)
961 r = ((mp_limb_t *) (BYTE_ARR_CTS(R2.p)))[0];
964 /* Result parked in R1, return via info-pointer at TOS */
966 JMP_(ENTRY_CODE(Sp[0]));
971 FN_(decodeFloatzh_fast)
979 /* arguments: F1 = Float# */
982 HP_CHK_GEN_TICKY(sizeofW(StgArrWords)+1, NO_PTRS, decodeFloatzh_fast);
983 TICK_ALLOC_PRIM(sizeofW(StgArrWords),1,0);
984 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
986 /* Be prepared to tell Lennart-coded __decodeFloat */
987 /* where mantissa._mp_d can be put (it does not care about the rest) */
988 p = (StgArrWords *)Hp - 1;
989 SET_ARR_HDR(p,&stg_ARR_WORDS_info,CCCS,1)
990 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
992 /* Perform the operation */
993 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
995 /* returns: (Int# (expn), Int#, ByteArray#) */
996 TICK_RET_UNBOXED_TUP(3);
997 RET_NNP(exponent,mantissa._mp_size,p);
1001 #define DOUBLE_MANTISSA_SIZE (sizeofW(StgDouble))
1002 #define ARR_SIZE (sizeofW(StgArrWords) + DOUBLE_MANTISSA_SIZE)
1004 FN_(decodeDoublezh_fast)
1011 /* arguments: D1 = Double# */
1014 HP_CHK_GEN_TICKY(ARR_SIZE, NO_PTRS, decodeDoublezh_fast);
1015 TICK_ALLOC_PRIM(sizeofW(StgArrWords),DOUBLE_MANTISSA_SIZE,0);
1016 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
1018 /* Be prepared to tell Lennart-coded __decodeDouble */
1019 /* where mantissa.d can be put (it does not care about the rest) */
1020 p = (StgArrWords *)(Hp-ARR_SIZE+1);
1021 SET_ARR_HDR(p, &stg_ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
1022 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
1024 /* Perform the operation */
1025 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
1027 /* returns: (Int# (expn), Int#, ByteArray#) */
1028 TICK_RET_UNBOXED_TUP(3);
1029 RET_NNP(exponent,mantissa._mp_size,p);
1033 /* -----------------------------------------------------------------------------
1034 * Concurrency primitives
1035 * -------------------------------------------------------------------------- */
1040 /* args: R1 = closure to spark */
1042 MAYBE_GC(R1_PTR, forkzh_fast);
1044 /* create it right now, return ThreadID in R1 */
1045 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
1046 RtsFlags.GcFlags.initialStkSize, R1.cl);
1047 STGCALL1(scheduleThread, R1.t);
1049 /* switch at the earliest opportunity */
1056 FN_(forkProcesszh_fast)
1064 R1.i = RET_STGCALL1(StgInt, forkProcess, CurrentTSO);
1073 JMP_(stg_yield_noregs);
1077 FN_(myThreadIdzh_fast)
1081 RET_P((P_)CurrentTSO);
1085 FN_(labelThreadzh_fast)
1092 STGCALL2(labelThread,R1.p,(char *)R2.p);
1094 JMP_(ENTRY_CODE(Sp[0]));
1099 /* -----------------------------------------------------------------------------
1102 * take & putMVar work as follows. Firstly, an important invariant:
1104 * If the MVar is full, then the blocking queue contains only
1105 * threads blocked on putMVar, and if the MVar is empty then the
1106 * blocking queue contains only threads blocked on takeMVar.
1109 * MVar empty : then add ourselves to the blocking queue
1110 * MVar full : remove the value from the MVar, and
1111 * blocking queue empty : return
1112 * blocking queue non-empty : perform the first blocked putMVar
1113 * from the queue, and wake up the
1114 * thread (MVar is now full again)
1116 * putMVar is just the dual of the above algorithm.
1118 * How do we "perform a putMVar"? Well, we have to fiddle around with
1119 * the stack of the thread waiting to do the putMVar. See
1120 * stg_block_putmvar and stg_block_takemvar in HeapStackCheck.c for
1121 * the stack layout, and the PerformPut and PerformTake macros below.
1123 * It is important that a blocked take or put is woken up with the
1124 * take/put already performed, because otherwise there would be a
1125 * small window of vulnerability where the thread could receive an
1126 * exception and never perform its take or put, and we'd end up with a
1129 * -------------------------------------------------------------------------- */
1131 FN_(isEmptyMVarzh_fast)
1133 /* args: R1 = MVar closure */
1136 r = (I_)((GET_INFO((StgMVar*)(R1.p))) == &stg_EMPTY_MVAR_info);
1149 HP_CHK_GEN_TICKY(sizeofW(StgMVar), NO_PTRS, newMVarzh_fast);
1150 TICK_ALLOC_PRIM(sizeofW(StgMutVar)-1, // consider head,tail,link as admin wds
1152 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
1154 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
1155 SET_HDR(mvar,&stg_EMPTY_MVAR_info,CCCS);
1156 mvar->head = mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1157 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1159 TICK_RET_UNBOXED_TUP(1);
1164 /* If R1 isn't available, pass it on the stack */
1166 #define PerformTake(tso, value) ({ \
1167 (tso)->sp[1] = (W_)value; \
1168 (tso)->sp[0] = (W_)&stg_gc_unpt_r1_info; \
1171 #define PerformTake(tso, value) ({ \
1172 (tso)->sp[1] = (W_)value; \
1173 (tso)->sp[0] = (W_)&stg_ut_1_0_unreg_info; \
1178 #define PerformPut(tso) ({ \
1179 StgClosure *val = (StgClosure *)(tso)->sp[2]; \
1184 FN_(takeMVarzh_fast)
1188 const StgInfoTable *info;
1191 /* args: R1 = MVar closure */
1193 mvar = (StgMVar *)R1.p;
1196 info = LOCK_CLOSURE(mvar);
1198 info = GET_INFO(mvar);
1201 /* If the MVar is empty, put ourselves on its blocking queue,
1202 * and wait until we're woken up.
1204 if (info == &stg_EMPTY_MVAR_info) {
1205 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1206 mvar->head = CurrentTSO;
1208 mvar->tail->link = CurrentTSO;
1210 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1211 CurrentTSO->why_blocked = BlockedOnMVar;
1212 CurrentTSO->block_info.closure = (StgClosure *)mvar;
1213 mvar->tail = CurrentTSO;
1216 /* unlock the MVar */
1217 mvar->header.info = &stg_EMPTY_MVAR_info;
1219 JMP_(stg_block_takemvar);
1222 /* we got the value... */
1225 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1226 /* There are putMVar(s) waiting...
1227 * wake up the first thread on the queue
1229 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1231 /* actually perform the putMVar for the thread that we just woke up */
1232 mvar->value = PerformPut(mvar->head);
1234 #if defined(GRAN) || defined(PAR)
1235 /* ToDo: check 2nd arg (mvar) is right */
1236 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1238 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1240 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1241 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1244 /* unlock in the SMP case */
1245 SET_INFO(mvar,&stg_FULL_MVAR_info);
1247 TICK_RET_UNBOXED_TUP(1);
1250 /* No further putMVars, MVar is now empty */
1252 /* do this last... we might have locked the MVar in the SMP case,
1253 * and writing the info pointer will unlock it.
1255 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1256 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1257 TICK_RET_UNBOXED_TUP(1);
1263 FN_(tryTakeMVarzh_fast)
1267 const StgInfoTable *info;
1270 /* args: R1 = MVar closure */
1272 mvar = (StgMVar *)R1.p;
1275 info = LOCK_CLOSURE(mvar);
1277 info = GET_INFO(mvar);
1280 if (info == &stg_EMPTY_MVAR_info) {
1283 /* unlock the MVar */
1284 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1287 /* HACK: we need a pointer to pass back,
1288 * so we abuse NO_FINALIZER_closure
1290 RET_NP(0, &stg_NO_FINALIZER_closure);
1293 /* we got the value... */
1296 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1297 /* There are putMVar(s) waiting...
1298 * wake up the first thread on the queue
1300 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1302 /* actually perform the putMVar for the thread that we just woke up */
1303 mvar->value = PerformPut(mvar->head);
1305 #if defined(GRAN) || defined(PAR)
1306 /* ToDo: check 2nd arg (mvar) is right */
1307 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1309 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1311 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1312 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1315 /* unlock in the SMP case */
1316 SET_INFO(mvar,&stg_FULL_MVAR_info);
1319 /* No further putMVars, MVar is now empty */
1320 mvar->value = (StgClosure *)&stg_END_TSO_QUEUE_closure;
1322 /* do this last... we might have locked the MVar in the SMP case,
1323 * and writing the info pointer will unlock it.
1325 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1328 TICK_RET_UNBOXED_TUP(1);
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) {
1350 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1351 mvar->head = CurrentTSO;
1353 mvar->tail->link = CurrentTSO;
1355 CurrentTSO->link = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1356 CurrentTSO->why_blocked = BlockedOnMVar;
1357 CurrentTSO->block_info.closure = (StgClosure *)mvar;
1358 mvar->tail = CurrentTSO;
1361 /* unlock the MVar */
1362 SET_INFO(mvar,&stg_FULL_MVAR_info);
1364 JMP_(stg_block_putmvar);
1367 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1368 /* There are takeMVar(s) waiting: wake up the first one
1370 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1372 /* actually perform the takeMVar */
1373 PerformTake(mvar->head, R2.cl);
1375 #if defined(GRAN) || defined(PAR)
1376 /* ToDo: check 2nd arg (mvar) is right */
1377 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1379 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1381 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1382 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1385 /* unlocks the MVar in the SMP case */
1386 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1388 JMP_(ENTRY_CODE(Sp[0]));
1390 /* No further takes, the MVar is now full. */
1391 mvar->value = R2.cl;
1392 /* unlocks the MVar in the SMP case */
1393 SET_INFO(mvar,&stg_FULL_MVAR_info);
1394 JMP_(ENTRY_CODE(Sp[0]));
1397 /* ToDo: yield afterward for better communication performance? */
1401 FN_(tryPutMVarzh_fast)
1404 const StgInfoTable *info;
1407 /* args: R1 = MVar, R2 = value */
1409 mvar = (StgMVar *)R1.p;
1412 info = LOCK_CLOSURE(mvar);
1414 info = GET_INFO(mvar);
1417 if (info == &stg_FULL_MVAR_info) {
1420 /* unlock the MVar */
1421 mvar->header.info = &stg_FULL_MVAR_info;
1427 if (mvar->head != (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1428 /* There are takeMVar(s) waiting: wake up the first one
1430 ASSERT(mvar->head->why_blocked == BlockedOnMVar);
1432 /* actually perform the takeMVar */
1433 PerformTake(mvar->head, R2.cl);
1435 #if defined(GRAN) || defined(PAR)
1436 /* ToDo: check 2nd arg (mvar) is right */
1437 mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
1439 mvar->head = RET_STGCALL1(StgTSO *,unblockOne,mvar->head);
1441 if (mvar->head == (StgTSO *)&stg_END_TSO_QUEUE_closure) {
1442 mvar->tail = (StgTSO *)&stg_END_TSO_QUEUE_closure;
1445 /* unlocks the MVar in the SMP case */
1446 SET_INFO(mvar,&stg_EMPTY_MVAR_info);
1448 JMP_(ENTRY_CODE(Sp[0]));
1450 /* No further takes, the MVar is now full. */
1451 mvar->value = R2.cl;
1452 /* unlocks the MVar in the SMP case */
1453 SET_INFO(mvar,&stg_FULL_MVAR_info);
1454 JMP_(ENTRY_CODE(Sp[0]));
1457 /* ToDo: yield afterward for better communication performance? */
1461 /* -----------------------------------------------------------------------------
1462 Stable pointer primitives
1463 ------------------------------------------------------------------------- */
1465 FN_(makeStableNamezh_fast)
1468 StgStableName *sn_obj;
1471 HP_CHK_GEN_TICKY(sizeofW(StgStableName), R1_PTR, makeStableNamezh_fast);
1472 TICK_ALLOC_PRIM(sizeofW(StgHeader),
1473 sizeofW(StgStableName)-sizeofW(StgHeader), 0);
1474 CCS_ALLOC(CCCS,sizeofW(StgStableName)); /* ccs prof */
1476 index = RET_STGCALL1(StgWord,lookupStableName,R1.p);
1478 /* Is there already a StableName for this heap object? */
1479 if (stable_ptr_table[index].sn_obj == NULL) {
1480 sn_obj = (StgStableName *) (Hp - sizeofW(StgStableName) + 1);
1481 SET_HDR(sn_obj,&stg_STABLE_NAME_info,CCCS);
1483 stable_ptr_table[index].sn_obj = (StgClosure *)sn_obj;
1485 (StgClosure *)sn_obj = stable_ptr_table[index].sn_obj;
1488 TICK_RET_UNBOXED_TUP(1);
1493 FN_(makeStablePtrzh_fast)
1498 MAYBE_GC(R1_PTR, makeStablePtrzh_fast);
1499 sp = RET_STGCALL1(StgStablePtr,getStablePtr,R1.p);
1504 FN_(deRefStablePtrzh_fast)
1506 /* Args: R1 = the stable ptr */
1510 sp = (StgStablePtr)R1.w;
1511 r = stable_ptr_table[(StgWord)sp].addr;
1516 /* -----------------------------------------------------------------------------
1517 Bytecode object primitives
1518 ------------------------------------------------------------------------- */
1531 StgArrWords *bitmap_arr;
1534 bitmap_arr = (StgArrWords *)R6.cl;
1535 size = sizeofW(StgBCO) + bitmap_arr->words;
1536 HP_CHK_GEN_TICKY(size,R1_PTR|R2_PTR|R3_PTR|R4_PTR|R6_PTR, newBCOzh_fast);
1537 TICK_ALLOC_PRIM(size, size-sizeofW(StgHeader), 0);
1538 CCS_ALLOC(CCCS,size); /* ccs prof */
1539 bco = (StgBCO *) (Hp + 1 - size);
1540 SET_HDR(bco, (const StgInfoTable *)&stg_BCO_info, CCCS);
1542 bco->instrs = (StgArrWords*)R1.cl;
1543 bco->literals = (StgArrWords*)R2.cl;
1544 bco->ptrs = (StgMutArrPtrs*)R3.cl;
1545 bco->itbls = (StgArrWords*)R4.cl;
1549 // Copy the arity/bitmap info into the BCO
1552 for (i = 0; i < bitmap_arr->words; i++) {
1553 bco->bitmap[i] = bitmap_arr->payload[i];
1557 TICK_RET_UNBOXED_TUP(1);
1562 FN_(mkApUpd0zh_fast)
1564 // R1.p = the BCO# for the AP
1569 // This function is *only* used to wrap zero-arity BCOs in an
1570 // updatable wrapper (see ByteCodeLink.lhs). An AP thunk is always
1571 // saturated and always points directly to a FUN or BCO.
1572 ASSERT(get_itbl(R1.cl)->type == BCO && ((StgBCO *)R1.p)->arity == 0);
1574 HP_CHK_GEN_TICKY(PAP_sizeW(0), R1_PTR, mkApUpd0zh_fast);
1575 TICK_ALLOC_PRIM(sizeofW(StgHeader), PAP_sizeW(0)-sizeofW(StgHeader), 0);
1576 CCS_ALLOC(CCCS,PAP_sizeW(0)); /* ccs prof */
1577 ap = (StgPAP *) (Hp + 1 - PAP_sizeW(0));
1578 SET_HDR(ap, &stg_AP_info, CCCS);
1583 TICK_RET_UNBOXED_TUP(1);
1588 /* -----------------------------------------------------------------------------
1589 Thread I/O blocking primitives
1590 -------------------------------------------------------------------------- */
1592 FN_(waitReadzh_fast)
1596 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1597 CurrentTSO->why_blocked = BlockedOnRead;
1598 CurrentTSO->block_info.fd = R1.i;
1599 ACQUIRE_LOCK(&sched_mutex);
1600 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1601 RELEASE_LOCK(&sched_mutex);
1602 JMP_(stg_block_noregs);
1606 FN_(waitWritezh_fast)
1610 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1611 CurrentTSO->why_blocked = BlockedOnWrite;
1612 CurrentTSO->block_info.fd = R1.i;
1613 ACQUIRE_LOCK(&sched_mutex);
1614 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1615 RELEASE_LOCK(&sched_mutex);
1616 JMP_(stg_block_noregs);
1622 #ifdef mingw32_TARGET_OS
1623 StgAsyncIOResult* ares;
1631 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1632 CurrentTSO->why_blocked = BlockedOnDelay;
1634 ACQUIRE_LOCK(&sched_mutex);
1635 #ifdef mingw32_TARGET_OS
1636 /* could probably allocate this on the heap instead */
1637 ares = (StgAsyncIOResult*)RET_STGCALL2(P_,stgMallocBytes,sizeof(StgAsyncIOResult), "delayzh_fast");
1638 reqID = RET_STGCALL1(W_,addDelayRequest,R1.i);
1639 ares->reqID = reqID;
1642 CurrentTSO->block_info.async_result = ares;
1643 /* Having all async-blocked threads reside on the blocked_queue simplifies matters, so
1644 * change the status to OnDoProc & put the delayed thread on the blocked_queue.
1646 CurrentTSO->why_blocked = BlockedOnDoProc;
1647 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1649 target = (R1.i / (TICK_MILLISECS*1000)) + getourtimeofday();
1650 CurrentTSO->block_info.target = target;
1652 /* Insert the new thread in the sleeping queue. */
1655 while (t != END_TSO_QUEUE && t->block_info.target < target) {
1660 CurrentTSO->link = t;
1662 sleeping_queue = CurrentTSO;
1664 prev->link = CurrentTSO;
1667 RELEASE_LOCK(&sched_mutex);
1668 JMP_(stg_block_noregs);
1672 #ifdef mingw32_TARGET_OS
1673 FN_(asyncReadzh_fast)
1675 StgAsyncIOResult* ares;
1678 /* args: R1.i = fd, R2.i = isSock, R3.i = len, R4.p = buf */
1679 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1680 CurrentTSO->why_blocked = BlockedOnRead;
1681 ACQUIRE_LOCK(&sched_mutex);
1682 /* could probably allocate this on the heap instead */
1683 ares = (StgAsyncIOResult*)RET_STGCALL2(P_,stgMallocBytes,sizeof(StgAsyncIOResult), "asyncReadzh_fast");
1684 reqID = RET_STGCALL5(W_,addIORequest,R1.i,FALSE,R2.i,R3.i,(char*)R4.p);
1685 ares->reqID = reqID;
1688 CurrentTSO->block_info.async_result = ares;
1689 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1690 RELEASE_LOCK(&sched_mutex);
1691 JMP_(stg_block_async);
1695 FN_(asyncWritezh_fast)
1697 StgAsyncIOResult* ares;
1701 /* args: R1.i = fd, R2.i = isSock, R3.i = len, R4.p = buf */
1702 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1703 CurrentTSO->why_blocked = BlockedOnWrite;
1704 ACQUIRE_LOCK(&sched_mutex);
1705 ares = (StgAsyncIOResult*)RET_STGCALL2(P_,stgMallocBytes,sizeof(StgAsyncIOResult), "asyncWritezh_fast");
1706 reqID = RET_STGCALL5(W_,addIORequest,R1.i,TRUE,R2.i,R3.i,(char*)R4.p);
1707 ares->reqID = reqID;
1710 CurrentTSO->block_info.async_result = ares;
1711 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1712 RELEASE_LOCK(&sched_mutex);
1713 JMP_(stg_block_async);
1717 FN_(asyncDoProczh_fast)
1719 StgAsyncIOResult* ares;
1722 /* args: R1.i = proc, R2.i = param */
1723 ASSERT(CurrentTSO->why_blocked == NotBlocked);
1724 CurrentTSO->why_blocked = BlockedOnDoProc;
1725 ACQUIRE_LOCK(&sched_mutex);
1726 /* could probably allocate this on the heap instead */
1727 ares = (StgAsyncIOResult*)RET_STGCALL2(P_,stgMallocBytes,sizeof(StgAsyncIOResult), "asyncDoProczh_fast");
1728 reqID = RET_STGCALL2(W_,addDoProcRequest,R1.p,R2.p);
1729 ares->reqID = reqID;
1732 CurrentTSO->block_info.async_result = ares;
1733 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1734 RELEASE_LOCK(&sched_mutex);
1735 JMP_(stg_block_async);