1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.hc,v 1.2 1998/12/02 13:28:32 simonm Exp $
4 * Primitive functions / data
6 * ---------------------------------------------------------------------------*/
13 #include "StgStartup.h"
18 #include "BlockAlloc.h" /* tmp */
19 #include "StablePtr.h"
23 classes CCallable and CReturnable don't really exist, but the
24 compiler insists on generating dictionaries containing references
25 to GHC_ZcCCallable_static_info etc., so we provide dummy symbols
29 W_ GHC_ZcCCallable_static_info[0];
30 W_ GHC_ZcCReturnable_static_info[0];
32 #ifndef aix_TARGET_OS /* AIX gives link errors with this as a const (RO assembler section) */
35 StgClosure *PrelBase_Bool_closure_tbl[] = {
40 /* -----------------------------------------------------------------------------
41 Macros for Hand-written primitives.
42 -------------------------------------------------------------------------- */
45 * Horrible macros for returning unboxed tuples.
47 * How an unboxed tuple is returned depends on two factors:
48 * - the number of real registers we have available
49 * - the boxedness of the returned fields.
51 * To return an unboxed tuple from a primitive operation, we have macros
52 * RET_<layout> where <layout> describes the boxedness of each field of the
53 * unboxed tuple: N indicates a non-pointer field, and P indicates a pointer.
55 * We only define the cases actually used, to avoid having too much
56 * garbage in this section. Warning: any bugs in here will be hard to
60 /*------ All Regs available */
62 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
63 # define RET_N(a) RET_P(a)
65 # define RET_PP(a,b) R1.w = (W_)(a); R2.w = (W_)(b); JMP_(ENTRY_CODE(Sp[0]));
66 # define RET_NN(a,b) RET_PP(a,b)
67 # define RET_NP(a,b) RET_PP(a,b)
69 # define RET_PPP(a,b,c) \
70 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); JMP_(ENTRY_CODE(Sp[0]));
71 # define RET_NNP(a,b,c) RET_PPP(a,b,c)
73 # define RET_NNNP(a,b,c,d) \
74 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); R4.w = (W_)d; \
75 JMP_(ENTRY_CODE(Sp[0]));
77 # define RET_NNPNNP(a,b,c,d,e,f) \
78 R1.w = (W_)(a); R2.w = (W_)(b); R3.w = (W_)(c); \
79 R4.w = (W_)(d); R5.w = (W_)(e); R6.w = (W_)(f); \
80 JMP_(ENTRY_CODE(Sp[0]));
84 #if defined(REG_R7) || defined(REG_R6) || defined(REG_R5) || \
85 defined(REG_R4) || defined(REG_R3) || defined(REG_R2)
86 # error RET_n macros not defined for this setup.
89 /*------ 1 Register available */
91 # define RET_P(a) R1.w = (W_)(a); JMP_(ENTRY_CODE(Sp[0]));
92 # define RET_N(a) RET_P(a)
94 # define RET_PP(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 1; \
95 JMP_(ENTRY_CODE(Sp[1]));
96 # define RET_NN(a,b) R1.w = (W_)(a); Sp[-1] = (W_)(b); Sp -= 2; \
97 JMP_(ENTRY_CODE(Sp[2]));
98 # define RET_NP(a,b) RET_PP(a,b)
100 # define RET_PPP(a,b,c) \
101 R1.w = (W_)(a); Sp[-2] = (W_)(b); Sp[-1] = (W_)(c); Sp -= 2; \
102 JMP_(ENTRY_CODE(Sp[2]));
103 # define RET_NNP(a,b,c) \
104 R1.w = (W_)(a); Sp[-2] = (W_)(b); Sp[-1] = (W_)(c); Sp -= 3; \
105 JMP_(ENTRY_CODE(Sp[3]));
107 # define RET_NNNP(a,b,c,d) \
109 /* Sp[-5] = ARGTAG(1); */ \
111 /* Sp[-3] = ARGTAG(1); */ \
115 JMP_(ENTRY_CODE(Sp[5]));
117 # define RET_NNPNNP(a,b,c,d,e,f) \
121 /* Sp[-3] = ARGTAG(1); */ \
123 /* Sp[-5] = ARGTAG(1); */ \
126 /* Sp[-8] = ARGTAG(1); */ \
128 JMP_(ENTRY_CODE(Sp[8]));
130 #else /* 0 Regs available */
132 #define PUSH_P(o,x) Sp[-o] = (W_)(x)
133 #define PUSH_N(o,x) Sp[1-o] = (W_)(x); /* Sp[-o] = ARGTAG(1) */
134 #define PUSHED(m) Sp -= (m); JMP_(ENTRY_CODE(Sp[m]));
136 /* Here's how to construct these macros:
138 * N = number of N's in the name;
139 * P = number of P's in the name;
141 * while (nonNull(name)) {
142 * if (nextChar == 'P') {
153 # define RET_P(a) PUSH_P(1,a); PUSHED(1)
154 # define RET_N(a) PUSH_N(2,a); PUSHED(2)
156 # define RET_PP(a,b) PUSH_P(2,a); PUSH_P(1,b); PUSHED(2)
157 # define RET_NN(a,b) PUSH_N(4,a); PUSH_N(2,b); PUSHED(4)
158 # define RET_NP(a,b) PUSH_N(3,a); PUSH_P(1,b); PUSHED(3)
160 # define RET_PPP(a,b,c) PUSH_P(3,a); PUSH_P(2,b); PUSH_P(1,c); PUSHED(3)
161 # define RET_NNP(a,b,c) PUSH_N(6,a); PUSH_N(4,b); PUSH_N(2,c); PUSHED(6)
163 # 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)
164 # 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)
171 /*-----------------------------------------------------------------------------
174 Basically just new*Array - the others are all inline macros.
176 The size arg is always passed in R1, and the result returned in R1.
178 The slow entry point is for returning from a heap check, the saved
179 size argument must be re-loaded from the stack.
180 -------------------------------------------------------------------------- */
182 /* for objects that are *less* than the size of a word, make sure we
183 * round up to the nearest word for the size of the array.
186 #define BYTES_TO_STGWORDS(n) ((n) + sizeof(W_) - 1)/sizeof(W_)
188 #define newByteArray(ty,scale) \
189 FN_(new##ty##ArrayZh_fast) \
191 W_ stuff_size, size, n; \
194 MAYBE_GC(NO_PTRS,new##ty##ArrayZh_fast); \
196 stuff_size = BYTES_TO_STGWORDS(n*scale); \
197 size = sizeofW(StgArrWords)+ stuff_size; \
198 p = (StgArrWords *)allocate(size); \
199 SET_HDR(p, &MUT_ARR_WORDS_info, CCCS); \
200 p->words = stuff_size; \
205 newByteArray(Char, sizeof(C_))
206 newByteArray(Int, sizeof(I_));
207 newByteArray(Word, sizeof(W_));
208 newByteArray(Addr, sizeof(P_));
209 newByteArray(Float, sizeof(StgFloat));
210 newByteArray(Double, sizeof(StgDouble));
211 newByteArray(StablePtr, sizeof(StgStablePtr));
221 MAYBE_GC(R2_PTR,newArrayZh_fast);
223 size = sizeofW(StgArrPtrs) + n;
224 arr = (StgArrPtrs *)allocate(size);
226 SET_HDR(arr,&MUT_ARR_PTRS_info,CCCS);
230 for (p = (P_)arr + sizeofW(StgArrPtrs);
231 p < (P_)arr + size; p++) {
239 FN_(newMutVarZh_fast)
242 /* Args: R1.p = initialisation value */
245 HP_CHK_GEN(sizeofW(StgMutVar), R1_PTR, newMutVarZh_fast,);
246 TICK_ALLOC_PRIM(sizeofW(StgMutVar),wibble,wibble,wibble)
247 CCS_ALLOC(CCCS,sizeofW(StgMutVar));
249 mv = stgCast(StgMutVar*,Hp-sizeofW(StgMutVar)+1);
250 SET_HDR(mv,&MUT_VAR_info,CCCS);
258 /* -----------------------------------------------------------------------------
259 Foreign Object Primitives
261 -------------------------------------------------------------------------- */
264 FN_(makeForeignObjZh_fast)
266 /* R1.p = ptr to foreign object,
268 StgForeignObj *result;
271 HP_CHK_GEN(sizeofW(StgForeignObj), NO_PTRS, makeForeignObjZh_fast,);
272 TICK_ALLOC_PRIM(sizeofW(StgForeignObj),wibble,wibble,wibble)
273 CCS_ALLOC(CCCS,sizeofW(StgForeignObj)); /* ccs prof */
275 result = (StgForeignObj *) (Hp + 1 - sizeofW(StgForeignObj));
276 SET_HDR(result,&FOREIGN_info,CCCS);
279 /* returns (# s#, ForeignObj# #) */
285 /* -----------------------------------------------------------------------------
286 Weak Pointer Primitives
287 -------------------------------------------------------------------------- */
300 HP_CHK_GEN(sizeofW(StgWeak), R1_PTR|R2_PTR|R3_PTR, mkWeakZh_fast,);
301 TICK_ALLOC_PRIM(sizeofW(StgWeak),wibble,wibble,wibble);
302 CCS_ALLOC(CCCS,sizeofW(StgWeak)); /* ccs prof */
304 w = (StgWeak *) (Hp + 1 - sizeofW(StgWeak));
305 SET_HDR(w, &WEAK_info, CCCS);
309 w->finaliser = R3.cl;
311 w->link = weak_ptr_list;
313 IF_DEBUG(weak, fprintf(stderr,"New weak pointer at %p\n",w));
319 FN_(deRefWeakZh_fast)
327 if (w->header.info == &WEAK_info) {
337 /* -----------------------------------------------------------------------------
338 Arbitrary-precision Integer operations.
339 -------------------------------------------------------------------------- */
341 FN_(int2IntegerZh_fast)
343 /* arguments: R1 = Int# */
345 I_ val, s; /* to avoid aliasing */
346 StgArrWords* p; /* address of array result */
350 HP_CHK_GEN(sizeofW(StgArrWords)+1, NO_PTRS, int2IntegerZh_fast,)
351 TICK_ALLOC_PRIM(sizeofW(StgArrWords)+1,wibble,wibble,wibble)
352 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
354 p = stgCast(StgArrWords*,Hp)-1;
355 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, 1);
357 /* mpz_set_si is inlined here, makes things simpler */
361 } else if (val > 0) {
368 /* returns (# alloc :: Int#,
377 FN_(word2IntegerZh_fast)
379 /* arguments: R1 = Word# */
381 W_ val; /* to avoid aliasing */
383 StgArrWords* p; /* address of array result */
387 HP_CHK_GEN(sizeofW(StgArrWords)+1, NO_PTRS, word2IntegerZh_fast,)
388 TICK_ALLOC_PRIM(sizeofW(StgArrWords)+1,wibble,wibble,wibble)
389 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
391 p = stgCast(StgArrWords*,Hp)-1;
392 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, 1);
401 /* returns (# alloc :: Int#,
410 FN_(addr2IntegerZh_fast)
416 MAYBE_GC(NO_PTRS,addr2IntegerZh_fast);
418 /* args: R1 :: Addr# */
421 /* Perform the operation */
422 if (RET_STGCALL3(int, mpz_init_set_str,&result,(str),/*base*/10))
425 RET_NNP(result._mp_alloc, result._mp_size,
426 result._mp_d - sizeofW(StgArrWords));
431 * 'long long' primops for converting to/from Integers.
434 #ifdef SUPPORT_LONG_LONGS
436 FN_(int64ToIntegerZh_fast)
438 /* arguments: L1 = Int64# */
440 StgInt64 val; /* to avoid aliasing */
442 I_ s,a, neg, words_needed;
443 StgArrWords* p; /* address of array result */
446 /* ToDo: extend StgUnion?? */
449 if ((LW_)(val) >= 0x100000000ULL) {
452 /* minimum is one word */
455 HP_CHK_GEN(sizeofW(StgArrWords)+words_needed, NO_PTRS, int64ToIntegerZh_fast,)
456 TICK_ALLOC_PRIM(sizeofW(StgArrWords)+words_needed,wibble,wibble,wibble)
457 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
459 p = stgCast(StgArrWords*,Hp)-1;
460 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, words_needed);
466 hi = (W_)((LW_)val / 0x100000000ULL);
467 if ((LW_)(val) >= 0x100000000ULL) {
472 } else if ( val != 0 ) {
476 } else /* val==0 */ {
480 s = ( neg ? -s : s );
482 /* returns (# alloc :: Int#,
491 FN_(word64ToIntegerZh_fast)
493 /* arguments: L1 = Word64# */
495 StgNat64 val; /* to avoid aliasing */
498 StgArrWords* p; /* address of array result */
502 if ( val >= 0x100000000ULL ) {
507 HP_CHK_GEN(sizeofW(StgArrWords)+words_needed, NO_PTRS, word64ToIntegerZh_fast,)
508 TICK_ALLOC_PRIM(sizeofW(StgArrWords)+words_needed,wibble,wibble,wibble)
509 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+words_needed); /* ccs prof */
511 p = stgCast(StgArrWords*,Hp)-1;
512 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, words_needed);
514 hi = (W_)((LW_)val / 0x100000000ULL);
515 if ( val >= 0x100000000ULL ) {
520 } else if ( val != 0 ) {
524 } else /* val==0 */ {
529 /* returns (# alloc :: Int#,
539 #endif /* HAVE_LONG_LONG */
541 /* ToDo: this is shockingly inefficient */
543 #define GMP_TAKE2_RET1(name,mp_fun) \
546 MP_INT arg1, arg2, result; \
552 /* call doYouWantToGC() */ \
553 MAYBE_GC(R3_PTR | R6_PTR, name); \
557 d1 = stgCast(StgArrWords*,R3.p); \
560 d2 = stgCast(StgArrWords*,R6.p); \
562 arg1._mp_alloc = (a1); \
563 arg1._mp_size = (s1); \
564 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
565 arg2._mp_alloc = (a2); \
566 arg2._mp_size = (s2); \
567 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
569 STGCALL1(mpz_init,&result); \
571 /* Perform the operation */ \
572 STGCALL3(mp_fun,&result,&arg1,&arg2); \
574 RET_NNP(result._mp_alloc, \
576 result._mp_d-sizeofW(StgArrWords)); \
580 #define GMP_TAKE2_RET2(name,mp_fun) \
583 MP_INT arg1, arg2, result1, result2; \
589 /* call doYouWantToGC() */ \
590 MAYBE_GC(R3_PTR | R6_PTR, name); \
594 d1 = stgCast(StgArrWords*,R3.p); \
597 d2 = stgCast(StgArrWords*,R6.p); \
599 arg1._mp_alloc = (a1); \
600 arg1._mp_size = (s1); \
601 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
602 arg2._mp_alloc = (a2); \
603 arg2._mp_size = (s2); \
604 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
606 STGCALL1(mpz_init,&result1); \
607 STGCALL1(mpz_init,&result2); \
609 /* Perform the operation */ \
610 STGCALL4(mp_fun,&result1,&result2,&arg1,&arg2); \
612 RET_NNPNNP(result1._mp_alloc, \
614 result1._mp_d-sizeofW(StgArrWords), \
617 result2._mp_d-sizeofW(StgArrWords)); \
621 GMP_TAKE2_RET1(plusIntegerZh_fast, mpz_add);
622 GMP_TAKE2_RET1(minusIntegerZh_fast, mpz_sub);
623 GMP_TAKE2_RET1(timesIntegerZh_fast, mpz_mul);
624 GMP_TAKE2_RET1(gcdIntegerZh_fast, mpz_gcd);
626 GMP_TAKE2_RET2(quotRemIntegerZh_fast, mpz_tdiv_qr);
627 GMP_TAKE2_RET2(divModIntegerZh_fast, mpz_fdiv_qr);
629 #ifndef FLOATS_AS_DOUBLES
630 FN_(decodeFloatZh_fast)
638 /* arguments: F1 = Float# */
641 HP_CHK_GEN(sizeof(StgArrWords)+1, NO_PTRS, decodeFloatZh_fast,);
642 TICK_ALLOC_PRIM(sizeofW(StgArrWords)+1,wibble,wibble,wibble)
643 CCS_ALLOC(CCCS,sizeofW(StgArrWords)+1); /* ccs prof */
645 /* Be prepared to tell Lennart-coded __decodeFloat */
646 /* where mantissa._mp_d can be put (it does not care about the rest) */
647 p = stgCast(StgArrWords*,Hp)-1;
648 SET_ARR_HDR(p,&ARR_WORDS_info,CCCS,1)
649 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
651 /* Perform the operation */
652 STGCALL3(__decodeFloat,&mantissa,&exponent,arg);
654 /* returns: (R1 = Int# (expn), R2 = Int#, R3 = Int#, R4 = ByteArray#) */
655 RET_NNNP(exponent,mantissa._mp_alloc,mantissa._mp_size,p);
658 #endif /* !FLOATS_AS_DOUBLES */
660 #define DOUBLE_MANTISSA_SIZE (sizeof(StgDouble)/sizeof(W_))
661 #define ARR_SIZE (sizeof(StgArrWords) + DOUBLE_MANTISSA_SIZE)
663 FN_(decodeDoubleZh_fast)
670 /* arguments: D1 = Double# */
673 HP_CHK_GEN(ARR_SIZE, NO_PTRS, decodeDoubleZh_fast,);
674 TICK_ALLOC_PRIM(ARR_SIZE,wibble,wibble,wibble)
675 CCS_ALLOC(CCCS,ARR_SIZE); /* ccs prof */
677 /* Be prepared to tell Lennart-coded __decodeDouble */
678 /* where mantissa.d can be put (it does not care about the rest) */
679 p = stgCast(StgArrWords*,Hp-ARR_SIZE+1);
680 SET_ARR_HDR(p, &ARR_WORDS_info, CCCS, DOUBLE_MANTISSA_SIZE);
681 mantissa._mp_d = (void *)BYTE_ARR_CTS(p);
683 /* Perform the operation */
684 STGCALL3(__decodeDouble,&mantissa,&exponent,arg);
686 /* returns: (R1 = Int# (expn), R2 = Int#, R3 = Int#, R4 = ByteArray#) */
687 RET_NNNP(exponent,mantissa._mp_alloc,mantissa._mp_size,p);
691 /* -----------------------------------------------------------------------------
692 * Concurrency primitives
693 * -------------------------------------------------------------------------- */
698 /* args: R1 = closure to spark */
700 if (closure_SHOULD_SPARK(stgCast(StgClosure*,R1.p))) {
702 MAYBE_GC(R1_PTR, forkZh_fast);
704 /* create it right now, return ThreadID in R1 */
705 R1.t = RET_STGCALL2(StgTSO *, createIOThread,
706 RtsFlags.GcFlags.initialStkSize, R1.cl);
708 /* switch at the earliest opportunity */
717 FN_(killThreadZh_fast)
720 /* args: R1.p = TSO to kill */
722 /* The thread is dead, but the TSO sticks around for a while. That's why
723 * we don't have to explicitly remove it from any queues it might be on.
725 STGCALL1(deleteThread, (StgTSO *)R1.p);
727 /* We might have killed ourselves. In which case, better return to the
730 if ((StgTSO *)R1.p == CurrentTSO) {
731 JMP_(stg_stop_thread_entry); /* leave semi-gracefully */
734 JMP_(ENTRY_CODE(Sp[0]));
745 HP_CHK_GEN(sizeofW(StgMVar), NO_PTRS, newMVarZh_fast,);
746 TICK_ALLOC_PRIM(sizeofW(StgMVar),wibble,wibble,wibble)
747 CCS_ALLOC(CCCS,sizeofW(StgMVar)); /* ccs prof */
749 mvar = (StgMVar *) (Hp - sizeofW(StgMVar) + 1);
750 SET_INFO(mvar,&EMPTY_MVAR_info);
751 mvar->head = mvar->tail = (StgTSO *)&END_TSO_QUEUE_closure;
752 mvar->value = (StgClosure *)&END_TSO_QUEUE_closure;
756 JMP_(ENTRY_CODE(Sp[0]));
765 /* args: R1 = MVar closure */
767 mvar = (StgMVar *)R1.p;
769 /* If the MVar is empty, put ourselves on its blocking queue,
770 * and wait until we're woken up.
772 if (GET_INFO(mvar) != &FULL_MVAR_info) {
773 if (mvar->head == (StgTSO *)&END_TSO_QUEUE_closure) {
774 mvar->head = CurrentTSO;
776 mvar->tail->link = CurrentTSO;
778 CurrentTSO->link = (StgTSO *)&END_TSO_QUEUE_closure;
779 mvar->tail = CurrentTSO;
781 BLOCK(R1_PTR, takeMVarZh_fast);
784 SET_INFO(mvar,&EMPTY_MVAR_info);
786 mvar->value = (StgClosure *)&END_TSO_QUEUE_closure;
788 JMP_(ENTRY_CODE(Sp[0]));
798 /* args: R1 = MVar, R2 = value */
800 mvar = (StgMVar *)R1.p;
801 if (GET_INFO(mvar) == &FULL_MVAR_info) {
803 fprintf(stderr, "putMVar#: MVar already full.\n");
804 stg_exit(EXIT_FAILURE);
807 SET_INFO(mvar,&FULL_MVAR_info);
810 /* wake up the first thread on the queue,
811 * it will continue with the takeMVar operation and mark the MVar
815 if (tso != (StgTSO *)&END_TSO_QUEUE_closure) {
816 PUSH_ON_RUN_QUEUE(tso);
817 mvar->head = tso->link;
818 tso->link = (StgTSO *)&END_TSO_QUEUE_closure;
819 if (mvar->head == (StgTSO *)&END_TSO_QUEUE_closure) {
820 mvar->tail = (StgTSO *)&END_TSO_QUEUE_closure;
824 /* ToDo: yield here for better communication performance? */
825 JMP_(ENTRY_CODE(*Sp));
829 /* -----------------------------------------------------------------------------
830 Stable pointer primitives
831 ------------------------------------------------------------------------- */
833 FN_(makeStablePtrZh_fast)
838 if (stable_ptr_free == NULL) {
839 enlargeStablePtrTable();
842 stable_ptr = stable_ptr_free - stable_ptr_table;
843 (P_)stable_ptr_free = *stable_ptr_free;
844 stable_ptr_table[stable_ptr] = R1.p;
847 JMP_(ENTRY_CODE(Sp[0]));
851 #endif /* COMPILER */