1 /* -----------------------------------------------------------------------------
2 * $Id: PrimOps.h,v 1.28 1999/05/07 15:42:49 sof Exp $
4 * (c) The GHC Team, 1998-1999
6 * Macros for primitive operations in STG-ish C code.
8 * ---------------------------------------------------------------------------*/
13 /* -----------------------------------------------------------------------------
15 -------------------------------------------------------------------------- */
17 #define gtCharzh(r,a,b) r=(I_)((a)> (b))
18 #define geCharzh(r,a,b) r=(I_)((a)>=(b))
19 #define eqCharzh(r,a,b) r=(I_)((a)==(b))
20 #define neCharzh(r,a,b) r=(I_)((a)!=(b))
21 #define ltCharzh(r,a,b) r=(I_)((a)< (b))
22 #define leCharzh(r,a,b) r=(I_)((a)<=(b))
24 /* Int comparisons: >#, >=# etc */
25 #define zgzh(r,a,b) r=(I_)((I_)(a) >(I_)(b))
26 #define zgzezh(r,a,b) r=(I_)((I_)(a)>=(I_)(b))
27 #define zezezh(r,a,b) r=(I_)((I_)(a)==(I_)(b))
28 #define zszezh(r,a,b) r=(I_)((I_)(a)!=(I_)(b))
29 #define zlzh(r,a,b) r=(I_)((I_)(a) <(I_)(b))
30 #define zlzezh(r,a,b) r=(I_)((I_)(a)<=(I_)(b))
32 #define gtWordzh(r,a,b) r=(I_)((W_)(a) >(W_)(b))
33 #define geWordzh(r,a,b) r=(I_)((W_)(a)>=(W_)(b))
34 #define eqWordzh(r,a,b) r=(I_)((W_)(a)==(W_)(b))
35 #define neWordzh(r,a,b) r=(I_)((W_)(a)!=(W_)(b))
36 #define ltWordzh(r,a,b) r=(I_)((W_)(a) <(W_)(b))
37 #define leWordzh(r,a,b) r=(I_)((W_)(a)<=(W_)(b))
39 #define gtAddrzh(r,a,b) r=(I_)((a) >(b))
40 #define geAddrzh(r,a,b) r=(I_)((a)>=(b))
41 #define eqAddrzh(r,a,b) r=(I_)((a)==(b))
42 #define neAddrzh(r,a,b) r=(I_)((a)!=(b))
43 #define ltAddrzh(r,a,b) r=(I_)((a) <(b))
44 #define leAddrzh(r,a,b) r=(I_)((a)<=(b))
46 #define gtFloatzh(r,a,b) r=(I_)((a)> (b))
47 #define geFloatzh(r,a,b) r=(I_)((a)>=(b))
48 #define eqFloatzh(r,a,b) r=(I_)((a)==(b))
49 #define neFloatzh(r,a,b) r=(I_)((a)!=(b))
50 #define ltFloatzh(r,a,b) r=(I_)((a)< (b))
51 #define leFloatzh(r,a,b) r=(I_)((a)<=(b))
53 /* Double comparisons: >##, >=#@ etc */
54 #define zgzhzh(r,a,b) r=(I_)((a) >(b))
55 #define zgzezhzh(r,a,b) r=(I_)((a)>=(b))
56 #define zezezhzh(r,a,b) r=(I_)((a)==(b))
57 #define zszezhzh(r,a,b) r=(I_)((a)!=(b))
58 #define zlzhzh(r,a,b) r=(I_)((a) <(b))
59 #define zlzezhzh(r,a,b) r=(I_)((a)<=(b))
61 /* -----------------------------------------------------------------------------
63 -------------------------------------------------------------------------- */
65 #define ordzh(r,a) r=(I_)((W_) (a))
66 #define chrzh(r,a) r=(StgChar)((W_)(a))
68 /* -----------------------------------------------------------------------------
70 -------------------------------------------------------------------------- */
72 I_ stg_div (I_ a, I_ b);
74 #define zpzh(r,a,b) r=(a)+(b)
75 #define zmzh(r,a,b) r=(a)-(b)
76 #define ztzh(r,a,b) r=(a)*(b)
77 #define quotIntzh(r,a,b) r=(a)/(b)
78 #define zszh(r,a,b) r=ULTRASAFESTGCALL2(I_,(void *, I_, I_),stg_div,(a),(b))
79 #define remIntzh(r,a,b) r=(a)%(b)
80 #define negateIntzh(r,a) r=-(a)
82 /* -----------------------------------------------------------------------------
83 * Int operations with carry.
84 * -------------------------------------------------------------------------- */
86 /* With some bit-twiddling, we can define int{Add,Sub}Czh portably in
87 * C, and without needing any comparisons. This may not be the
88 * fastest way to do it - if you have better code, please send it! --SDM
90 * Return : r = a + b, c = 0 if no overflow, 1 on overflow.
92 * We currently don't make use of the r value if c is != 0 (i.e.
93 * overflow), we just convert to big integers and try again. This
94 * could be improved by making r and c the correct values for
95 * plugging into a new J#.
97 #define addIntCzh(r,c,a,b) \
99 c = ((StgWord)(~(a^b) & (a^r))) \
100 >> (BITS_PER_BYTE * sizeof(I_) - 1); \
104 #define subIntCzh(r,c,a,b) \
106 c = ((StgWord)((a^b) & (a^r))) \
107 >> (BITS_PER_BYTE * sizeof(I_) - 1); \
110 /* Multiply with overflow checking.
112 * This is slightly more tricky - the usual sign rules for add/subtract
115 * On x86 hardware we use a hand-crafted assembly fragment to do the job.
117 * On other 32-bit machines we use gcc's 'long long' types, finding
118 * overflow with some careful bit-twiddling.
120 * On 64-bit machines where gcc's 'long long' type is also 64-bits,
121 * we use a crude approximation, testing whether either operand is
122 * larger than 32-bits; if neither is, then we go ahead with the
128 #define mulIntCzh(r,c,a,b) \
130 __asm__("xor %1,%1\n\t \
135 : "=r" (r), "=r" (c) : "r" (a), "0" (b)); \
138 #elif SIZEOF_VOID_P == 4
140 #ifdef WORDS_BIGENDIAN
153 #define mulIntCzh(r,c,a,b) \
156 z.l = (StgInt64)a * (StgInt64)b; \
159 if (c == 0 || c == -1) { \
160 c = ((StgWord)((a^b) ^ r)) \
161 >> (BITS_PER_BYTE * sizeof(I_) - 1); \
164 /* Careful: the carry calculation above is extremely delicate. Make sure
165 * you test it thoroughly after changing it.
170 #define HALF_INT (1 << (BITS_PER_BYTE * sizeof(I_) / 2))
172 #define stg_abs(a) ((a) < 0 ? -(a) : (a))
174 #define mulIntCzh(r,c,a,b) \
176 if (stg_abs(a) >= HALF_INT \
177 stg_abs(b) >= HALF_INT) { \
186 /* -----------------------------------------------------------------------------
188 -------------------------------------------------------------------------- */
190 #define quotWordzh(r,a,b) r=((W_)a)/((W_)b)
191 #define remWordzh(r,a,b) r=((W_)a)%((W_)b)
193 #define andzh(r,a,b) r=(a)&(b)
194 #define orzh(r,a,b) r=(a)|(b)
195 #define xorzh(r,a,b) r=(a)^(b)
196 #define notzh(r,a) r=~(a)
198 #define shiftLzh(r,a,b) r=(a)<<(b)
199 #define shiftRLzh(r,a,b) r=(a)>>(b)
200 #define iShiftLzh(r,a,b) r=(a)<<(b)
201 /* Right shifting of signed quantities is not portable in C, so
202 the behaviour you'll get from using these primops depends
203 on the whatever your C compiler is doing. ToDo: fix/document. -- sof 8/98
205 #define iShiftRAzh(r,a,b) r=(a)>>(b)
206 #define iShiftRLzh(r,a,b) r=(a)>>(b)
208 #define int2Wordzh(r,a) r=(W_)(a)
209 #define word2Intzh(r,a) r=(I_)(a)
211 /* -----------------------------------------------------------------------------
213 -------------------------------------------------------------------------- */
215 #define int2Addrzh(r,a) r=(A_)(a)
216 #define addr2Intzh(r,a) r=(I_)(a)
218 #define indexCharOffAddrzh(r,a,i) r= ((C_ *)(a))[i]
219 #define indexIntOffAddrzh(r,a,i) r= ((I_ *)(a))[i]
220 #define indexAddrOffAddrzh(r,a,i) r= ((PP_)(a))[i]
221 #define indexFloatOffAddrzh(r,a,i) r= PK_FLT((P_) (((StgFloat *)(a)) + i))
222 #define indexDoubleOffAddrzh(r,a,i) r= PK_DBL((P_) (((StgDouble *)(a)) + i))
223 #define indexStablePtrOffAddrzh(r,a,i) r= ((StgStablePtr *)(a))[i]
224 #ifdef SUPPORT_LONG_LONGS
225 #define indexInt64OffAddrzh(r,a,i) r= ((LI_ *)(a))[i]
226 #define indexWord64OffAddrzh(r,a,i) r= ((LW_ *)(a))[i]
229 #define writeCharOffAddrzh(a,i,v) ((C_ *)(a))[i] = (v)
230 #define writeIntOffAddrzh(a,i,v) ((I_ *)(a))[i] = (v)
231 #define writeWordOffAddrzh(a,i,v) ((W_ *)(a))[i] = (v)
232 #define writeAddrOffAddrzh(a,i,v) ((PP_)(a))[i] = (v)
233 #define writeForeignObjOffAddrzh(a,i,v) ((PP_)(a))[i] = ForeignObj_CLOSURE_DATA(v)
234 #define writeFloatOffAddrzh(a,i,v) ASSIGN_FLT((P_) (((StgFloat *)(a)) + i),v)
235 #define writeDoubleOffAddrzh(a,i,v) ASSIGN_DBL((P_) (((StgDouble *)(a)) + i),v)
236 #define writeStablePtrOffAddrzh(a,i,v) ((StgStablePtr *)(a))[i] = (v)
237 #ifdef SUPPORT_LONG_LONGS
238 #define writeInt64OffAddrzh(a,i,v) ((LI_ *)(a))[i] = (v)
239 #define writeWord64OffAddrzh(a,i,v) ((LW_ *)(a))[i] = (v)
242 /* -----------------------------------------------------------------------------
244 -------------------------------------------------------------------------- */
246 #define plusFloatzh(r,a,b) r=(a)+(b)
247 #define minusFloatzh(r,a,b) r=(a)-(b)
248 #define timesFloatzh(r,a,b) r=(a)*(b)
249 #define divideFloatzh(r,a,b) r=(a)/(b)
250 #define negateFloatzh(r,a) r=-(a)
252 #define int2Floatzh(r,a) r=(StgFloat)(a)
253 #define float2Intzh(r,a) r=(I_)(a)
255 #define expFloatzh(r,a) r=(StgFloat) RET_PRIM_STGCALL1(StgDouble,exp,a)
256 #define logFloatzh(r,a) r=(StgFloat) RET_PRIM_STGCALL1(StgDouble,log,a)
257 #define sqrtFloatzh(r,a) r=(StgFloat) RET_PRIM_STGCALL1(StgDouble,sqrt,a)
258 #define sinFloatzh(r,a) r=(StgFloat) RET_PRIM_STGCALL1(StgDouble,sin,a)
259 #define cosFloatzh(r,a) r=(StgFloat) RET_PRIM_STGCALL1(StgDouble,cos,a)
260 #define tanFloatzh(r,a) r=(StgFloat) RET_PRIM_STGCALL1(StgDouble,tan,a)
261 #define asinFloatzh(r,a) r=(StgFloat) RET_PRIM_STGCALL1(StgDouble,asin,a)
262 #define acosFloatzh(r,a) r=(StgFloat) RET_PRIM_STGCALL1(StgDouble,acos,a)
263 #define atanFloatzh(r,a) r=(StgFloat) RET_PRIM_STGCALL1(StgDouble,atan,a)
264 #define sinhFloatzh(r,a) r=(StgFloat) RET_PRIM_STGCALL1(StgDouble,sinh,a)
265 #define coshFloatzh(r,a) r=(StgFloat) RET_PRIM_STGCALL1(StgDouble,cosh,a)
266 #define tanhFloatzh(r,a) r=(StgFloat) RET_PRIM_STGCALL1(StgDouble,tanh,a)
267 #define powerFloatzh(r,a,b) r=(StgFloat) RET_PRIM_STGCALL2(StgDouble,pow,a,b)
269 /* -----------------------------------------------------------------------------
271 -------------------------------------------------------------------------- */
273 #define zpzhzh(r,a,b) r=(a)+(b)
274 #define zmzhzh(r,a,b) r=(a)-(b)
275 #define ztzhzh(r,a,b) r=(a)*(b)
276 #define zszhzh(r,a,b) r=(a)/(b)
277 #define negateDoublezh(r,a) r=-(a)
279 #define int2Doublezh(r,a) r=(StgDouble)(a)
280 #define double2Intzh(r,a) r=(I_)(a)
282 #define float2Doublezh(r,a) r=(StgDouble)(a)
283 #define double2Floatzh(r,a) r=(StgFloat)(a)
285 #define expDoublezh(r,a) r=(StgDouble) RET_PRIM_STGCALL1(StgDouble,exp,a)
286 #define logDoublezh(r,a) r=(StgDouble) RET_PRIM_STGCALL1(StgDouble,log,a)
287 #define sqrtDoublezh(r,a) r=(StgDouble) RET_PRIM_STGCALL1(StgDouble,sqrt,a)
288 #define sinDoublezh(r,a) r=(StgDouble) RET_PRIM_STGCALL1(StgDouble,sin,a)
289 #define cosDoublezh(r,a) r=(StgDouble) RET_PRIM_STGCALL1(StgDouble,cos,a)
290 #define tanDoublezh(r,a) r=(StgDouble) RET_PRIM_STGCALL1(StgDouble,tan,a)
291 #define asinDoublezh(r,a) r=(StgDouble) RET_PRIM_STGCALL1(StgDouble,asin,a)
292 #define acosDoublezh(r,a) r=(StgDouble) RET_PRIM_STGCALL1(StgDouble,acos,a)
293 #define atanDoublezh(r,a) r=(StgDouble) RET_PRIM_STGCALL1(StgDouble,atan,a)
294 #define sinhDoublezh(r,a) r=(StgDouble) RET_PRIM_STGCALL1(StgDouble,sinh,a)
295 #define coshDoublezh(r,a) r=(StgDouble) RET_PRIM_STGCALL1(StgDouble,cosh,a)
296 #define tanhDoublezh(r,a) r=(StgDouble) RET_PRIM_STGCALL1(StgDouble,tanh,a)
298 #define ztztzhzh(r,a,b) r=(StgDouble) RET_PRIM_STGCALL2(StgDouble,pow,a,b)
300 /* -----------------------------------------------------------------------------
302 -------------------------------------------------------------------------- */
304 /* We can do integer2Int and cmpInteger inline, since they don't need
305 * to allocate any memory.
307 * integer2Int# is now modular.
310 #define integer2Intzh(r, sa,da) \
313 arg._mp_size = (sa); \
314 arg._mp_alloc = ((StgArrWords *)da)->words; \
315 arg._mp_d = (unsigned long int *) (BYTE_ARR_CTS(da)); \
318 ( arg._mp_size == 0 ) ? \
320 ( arg._mp_size < 0 && arg._mp_d[0] > 0x80000000 ) ? \
321 -(I_)arg._mp_d[0] : \
325 #define integer2Wordzh(r, sa,da) \
328 arg._mp_size = (sa); \
329 arg._mp_alloc = ((StgArrWords *)da)->words; \
330 arg._mp_d = (unsigned long int *) (BYTE_ARR_CTS(da)); \
332 (r) = RET_PRIM_STGCALL1(I_,mpz_get_ui,&arg); \
335 #define cmpIntegerzh(r, s1,d1, s2,d2) \
339 arg1._mp_size = (s1); \
340 arg1._mp_alloc= ((StgArrWords *)d1)->words; \
341 arg1._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d1)); \
342 arg2._mp_size = (s2); \
343 arg2._mp_alloc= ((StgArrWords *)d2)->words; \
344 arg2._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d2)); \
346 (r) = RET_PRIM_STGCALL2(I_,mpz_cmp,&arg1,&arg2); \
349 #define cmpIntegerIntzh(r, s,d, i) \
352 arg._mp_size = (s); \
353 arg._mp_alloc = ((StgArrWords *)d)->words; \
354 arg._mp_d = (unsigned long int *) (BYTE_ARR_CTS(d)); \
356 (r) = RET_PRIM_STGCALL2(I_,mpz_cmp_si,&arg,i); \
359 /* The rest are all out-of-line: -------- */
361 /* Integer arithmetic */
362 EF_(plusIntegerzh_fast);
363 EF_(minusIntegerzh_fast);
364 EF_(timesIntegerzh_fast);
365 EF_(gcdIntegerzh_fast);
366 EF_(quotRemIntegerzh_fast);
367 EF_(divModIntegerzh_fast);
370 EF_(int2Integerzh_fast);
371 EF_(word2Integerzh_fast);
372 EF_(addr2Integerzh_fast);
374 /* Floating-point decodings */
375 EF_(decodeFloatzh_fast);
376 EF_(decodeDoublezh_fast);
378 /* -----------------------------------------------------------------------------
380 -------------------------------------------------------------------------- */
382 #ifdef SUPPORT_LONG_LONGS
384 #define integerToWord64zh(r, sa,da) \
385 { unsigned long int* d; \
389 d = (unsigned long int *) (BYTE_ARR_CTS(da)); \
390 aa = ((StgArrWords *)da)->words; \
393 } else if ( (aa) == 1) { \
396 res = (LW_)d[0] + (LW_)d[1] * 0x100000000ULL; \
401 #define integerToInt64zh(r, sa,da) \
402 { unsigned long int* d; \
406 d = (unsigned long int *) (BYTE_ARR_CTS(da)); \
407 aa = ((StgArrWords *)da)->words; \
410 } else if ( (aa) == 1) { \
413 res = (LI_)d[0] + (LI_)d[1] * 0x100000000LL; \
422 EF_(int64ToIntegerzh_fast);
423 EF_(word64ToIntegerzh_fast);
425 /* The rest are (way!) out of line, implemented via C entry points.
427 I_ stg_gtWord64 (StgWord64, StgWord64);
428 I_ stg_geWord64 (StgWord64, StgWord64);
429 I_ stg_eqWord64 (StgWord64, StgWord64);
430 I_ stg_neWord64 (StgWord64, StgWord64);
431 I_ stg_ltWord64 (StgWord64, StgWord64);
432 I_ stg_leWord64 (StgWord64, StgWord64);
434 I_ stg_gtInt64 (StgInt64, StgInt64);
435 I_ stg_geInt64 (StgInt64, StgInt64);
436 I_ stg_eqInt64 (StgInt64, StgInt64);
437 I_ stg_neInt64 (StgInt64, StgInt64);
438 I_ stg_ltInt64 (StgInt64, StgInt64);
439 I_ stg_leInt64 (StgInt64, StgInt64);
441 LW_ stg_remWord64 (StgWord64, StgWord64);
442 LW_ stg_quotWord64 (StgWord64, StgWord64);
444 LI_ stg_remInt64 (StgInt64, StgInt64);
445 LI_ stg_quotInt64 (StgInt64, StgInt64);
446 LI_ stg_negateInt64 (StgInt64);
447 LI_ stg_plusInt64 (StgInt64, StgInt64);
448 LI_ stg_minusInt64 (StgInt64, StgInt64);
449 LI_ stg_timesInt64 (StgInt64, StgInt64);
451 LW_ stg_and64 (StgWord64, StgWord64);
452 LW_ stg_or64 (StgWord64, StgWord64);
453 LW_ stg_xor64 (StgWord64, StgWord64);
454 LW_ stg_not64 (StgWord64);
456 LW_ stg_shiftL64 (StgWord64, StgInt);
457 LW_ stg_shiftRL64 (StgWord64, StgInt);
458 LI_ stg_iShiftL64 (StgInt64, StgInt);
459 LI_ stg_iShiftRL64 (StgInt64, StgInt);
460 LI_ stg_iShiftRA64 (StgInt64, StgInt);
462 LI_ stg_intToInt64 (StgInt);
463 I_ stg_int64ToInt (StgInt64);
464 LW_ stg_int64ToWord64 (StgInt64);
466 LW_ stg_wordToWord64 (StgWord);
467 W_ stg_word64ToWord (StgWord64);
468 LI_ stg_word64ToInt64 (StgWord64);
471 /* -----------------------------------------------------------------------------
473 -------------------------------------------------------------------------- */
475 /* We cast to void* instead of StgChar* because this avoids a warning
476 * about increasing the alignment requirements.
478 #define REAL_BYTE_ARR_CTS(a) ((void *) (((StgArrWords *)(a))->payload))
479 #define REAL_PTRS_ARR_CTS(a) ((P_) (((StgMutArrPtrs *)(a))->payload))
482 #define BYTE_ARR_CTS(a) \
483 ({ ASSERT(GET_INFO(a) == &ARR_WORDS_info); \
484 REAL_BYTE_ARR_CTS(a); })
485 #define PTRS_ARR_CTS(a) \
486 ({ ASSERT((GET_INFO(a) == &ARR_PTRS_info) \
487 || (GET_INFO(a) == &MUT_ARR_PTRS_info)); \
488 REAL_PTRS_ARR_CTS(a); })
490 #define BYTE_ARR_CTS(a) REAL_BYTE_ARR_CTS(a)
491 #define PTRS_ARR_CTS(a) REAL_PTRS_ARR_CTS(a)
494 extern I_ genSymZh(void);
495 extern I_ resetGenSymZh(void);
497 /*--- everything except new*Array is done inline: */
499 #define sameMutableArrayzh(r,a,b) r=(I_)((a)==(b))
500 #define sameMutableByteArrayzh(r,a,b) r=(I_)((a)==(b))
502 #define readArrayzh(r,a,i) r=((PP_) PTRS_ARR_CTS(a))[(i)]
504 #define readCharArrayzh(r,a,i) indexCharOffAddrzh(r,BYTE_ARR_CTS(a),i)
505 #define readIntArrayzh(r,a,i) indexIntOffAddrzh(r,BYTE_ARR_CTS(a),i)
506 #define readWordArrayzh(r,a,i) indexWordOffAddrzh(r,BYTE_ARR_CTS(a),i)
507 #define readAddrArrayzh(r,a,i) indexAddrOffAddrzh(r,BYTE_ARR_CTS(a),i)
508 #define readFloatArrayzh(r,a,i) indexFloatOffAddrzh(r,BYTE_ARR_CTS(a),i)
509 #define readDoubleArrayzh(r,a,i) indexDoubleOffAddrzh(r,BYTE_ARR_CTS(a),i)
510 #define readStablePtrArrayzh(r,a,i) indexStablePtrOffAddrzh(r,BYTE_ARR_CTS(a),i)
511 #ifdef SUPPORT_LONG_LONGS
512 #define readInt64Arrayzh(r,a,i) indexInt64OffAddrzh(r,BYTE_ARR_CTS(a),i)
513 #define readWord64Arrayzh(r,a,i) indexWord64OffAddrzh(r,BYTE_ARR_CTS(a),i)
516 /* result ("r") arg ignored in write macros! */
517 #define writeArrayzh(a,i,v) ((PP_) PTRS_ARR_CTS(a))[(i)]=(v)
519 #define writeCharArrayzh(a,i,v) ((C_ *)(BYTE_ARR_CTS(a)))[i] = (v)
520 #define writeIntArrayzh(a,i,v) ((I_ *)(BYTE_ARR_CTS(a)))[i] = (v)
521 #define writeWordArrayzh(a,i,v) ((W_ *)(BYTE_ARR_CTS(a)))[i] = (v)
522 #define writeAddrArrayzh(a,i,v) ((PP_)(BYTE_ARR_CTS(a)))[i] = (v)
523 #define writeFloatArrayzh(a,i,v) \
524 ASSIGN_FLT((P_) (((StgFloat *)(BYTE_ARR_CTS(a))) + i),v)
525 #define writeDoubleArrayzh(a,i,v) \
526 ASSIGN_DBL((P_) (((StgDouble *)(BYTE_ARR_CTS(a))) + i),v)
527 #define writeStablePtrArrayzh(a,i,v) ((StgStablePtr *)(BYTE_ARR_CTS(a)))[i] = (v)
528 #ifdef SUPPORT_LONG_LONGS
529 #define writeInt64Arrayzh(a,i,v) ((LI_ *)(BYTE_ARR_CTS(a)))[i] = (v)
530 #define writeWord64Arrayzh(a,i,v) ((LW_ *)(BYTE_ARR_CTS(a)))[i] = (v)
533 #define indexArrayzh(r,a,i) r=((PP_) PTRS_ARR_CTS(a))[(i)]
535 #define indexCharArrayzh(r,a,i) indexCharOffAddrzh(r,BYTE_ARR_CTS(a),i)
536 #define indexIntArrayzh(r,a,i) indexIntOffAddrzh(r,BYTE_ARR_CTS(a),i)
537 #define indexWordArrayzh(r,a,i) indexWordOffAddrzh(r,BYTE_ARR_CTS(a),i)
538 #define indexAddrArrayzh(r,a,i) indexAddrOffAddrzh(r,BYTE_ARR_CTS(a),i)
539 #define indexFloatArrayzh(r,a,i) indexFloatOffAddrzh(r,BYTE_ARR_CTS(a),i)
540 #define indexDoubleArrayzh(r,a,i) indexDoubleOffAddrzh(r,BYTE_ARR_CTS(a),i)
541 #define indexStablePtrArrayzh(r,a,i) indexStablePtrOffAddrzh(r,BYTE_ARR_CTS(a),i)
542 #ifdef SUPPORT_LONG_LONGS
543 #define indexInt64Arrayzh(r,a,i) indexInt64OffAddrzh(r,BYTE_ARR_CTS(a),i)
544 #define indexWord64Arrayzh(r,a,i) indexWord64OffAddrzh(r,BYTE_ARR_CTS(a),i)
547 #define indexCharOffForeignObjzh(r,fo,i) indexCharOffAddrzh(r,ForeignObj_CLOSURE_DATA(fo),i)
548 #define indexIntOffForeignObjzh(r,fo,i) indexIntOffAddrzh(r,ForeignObj_CLOSURE_DATA(fo),i)
549 #define indexWordOffForeignObjzh(r,fo,i) indexWordOffAddrzh(r,ForeignObj_CLOSURE_DATA(fo),i)
550 #define indexAddrOffForeignObjzh(r,fo,i) indexAddrOffAddrzh(r,ForeignObj_CLOSURE_DATA(fo),i)
551 #define indexFloatOffForeignObjzh(r,fo,i) indexFloatOffAddrzh(r,ForeignObj_CLOSURE_DATA(fo),i)
552 #define indexDoubleOffForeignObjzh(r,fo,i) indexDoubleOffAddrzh(r,ForeignObj_CLOSURE_DATA(fo),i)
553 #define indexStablePtrOffForeignObjzh(r,fo,i) indexStablePtrOffAddrzh(r,ForeignObj_CLOSURE_DATA(fo),i)
554 #ifdef SUPPORT_LONG_LONGS
555 #define indexInt64OffForeignObjzh(r,fo,i) indexInt64OffAddrzh(r,ForeignObj_CLOSURE_DATA(fo),i)
556 #define indexWord64OffForeignObjzh(r,fo,i) indexWord64OffAddrzh(r,ForeignObj_CLOSURE_DATA(fo),i)
559 #define indexCharOffAddrzh(r,a,i) r= ((C_ *)(a))[i]
560 #define indexIntOffAddrzh(r,a,i) r= ((I_ *)(a))[i]
561 #define indexWordOffAddrzh(r,a,i) r= ((W_ *)(a))[i]
562 #define indexAddrOffAddrzh(r,a,i) r= ((PP_)(a))[i]
563 #define indexFloatOffAddrzh(r,a,i) r= PK_FLT((P_) (((StgFloat *)(a)) + i))
564 #define indexDoubleOffAddrzh(r,a,i) r= PK_DBL((P_) (((StgDouble *)(a)) + i))
565 #ifdef SUPPORT_LONG_LONGS
566 #define indexInt64OffAddrzh(r,a,i) r= ((LI_ *)(a))[i]
567 #define indexWord64OffAddrzh(r,a,i) r= ((LW_ *)(a))[i]
570 /* Freezing arrays-of-ptrs requires changing an info table, for the
571 benefit of the generational collector. It needs to scavenge mutable
572 objects, even if they are in old space. When they become immutable,
573 they can be removed from this scavenge list. */
575 #define unsafeFreezzeArrayzh(r,a) \
577 SET_INFO((StgClosure *)a,&MUT_ARR_PTRS_FROZEN_info); \
581 #define unsafeFreezzeByteArrayzh(r,a) r=(a)
582 #define unsafeThawByteArrayzh(r,a) r=(a)
584 EF_(unsafeThawArrayzh_fast);
586 #define sizzeofByteArrayzh(r,a) \
587 r = (((StgArrWords *)(a))->words * sizeof(W_))
588 #define sizzeofMutableByteArrayzh(r,a) \
589 r = (((StgArrWords *)(a))->words * sizeof(W_))
591 /* and the out-of-line ones... */
593 EF_(newCharArrayzh_fast);
594 EF_(newIntArrayzh_fast);
595 EF_(newWordArrayzh_fast);
596 EF_(newAddrArrayzh_fast);
597 EF_(newFloatArrayzh_fast);
598 EF_(newDoubleArrayzh_fast);
599 EF_(newStablePtrArrayzh_fast);
600 EF_(newArrayzh_fast);
602 /* encoding and decoding of floats/doubles. */
604 /* We only support IEEE floating point format */
605 #include "ieee-flpt.h"
607 /* The decode operations are out-of-line because they need to allocate
610 #ifdef FLOATS_AS_DOUBLES
611 #define decodeFloatzh_fast decodeDoublezh_fast
613 EF_(decodeFloatzh_fast);
616 EF_(decodeDoublezh_fast);
618 /* grimy low-level support functions defined in StgPrimFloat.c */
620 extern StgDouble __encodeDouble (I_ size, StgByteArray arr, I_ e);
621 extern StgDouble __int_encodeDouble (I_ j, I_ e);
622 #ifndef FLOATS_AS_DOUBLES
623 extern StgFloat __encodeFloat (I_ size, StgByteArray arr, I_ e);
624 extern StgFloat __int_encodeFloat (I_ j, I_ e);
626 extern void __decodeDouble (MP_INT *man, I_ *_exp, StgDouble dbl);
627 extern void __decodeFloat (MP_INT *man, I_ *_exp, StgFloat flt);
628 extern StgInt isDoubleNaN(StgDouble d);
629 extern StgInt isDoubleInfinite(StgDouble d);
630 extern StgInt isDoubleDenormalized(StgDouble d);
631 extern StgInt isDoubleNegativeZero(StgDouble d);
632 extern StgInt isFloatNaN(StgFloat f);
633 extern StgInt isFloatInfinite(StgFloat f);
634 extern StgInt isFloatDenormalized(StgFloat f);
635 extern StgInt isFloatNegativeZero(StgFloat f);
637 /* -----------------------------------------------------------------------------
640 newMutVar is out of line.
641 -------------------------------------------------------------------------- */
643 EF_(newMutVarzh_fast);
645 #define readMutVarzh(r,a) r=(P_)(((StgMutVar *)(a))->var)
646 #define writeMutVarzh(a,v) (P_)(((StgMutVar *)(a))->var)=(v)
647 #define sameMutVarzh(r,a,b) r=(I_)((a)==(b))
649 /* -----------------------------------------------------------------------------
652 All out of line, because they either allocate or may block.
653 -------------------------------------------------------------------------- */
654 #define sameMVarzh(r,a,b) r=(I_)((a)==(b))
656 /* Assume external decl of EMPTY_MVAR_info is in scope by now */
657 #define isEmptyMVarzh(r,a) r=(I_)((GET_INFO((StgMVar*)(a))) == &EMPTY_MVAR_info )
659 EF_(takeMVarzh_fast);
663 /* -----------------------------------------------------------------------------
665 -------------------------------------------------------------------------- */
667 /* Hmm, I'll think about these later. */
669 /* -----------------------------------------------------------------------------
670 Primitive I/O, error-handling PrimOps
671 -------------------------------------------------------------------------- */
676 extern void stg_exit(I_ n) __attribute__ ((noreturn));
678 /* -----------------------------------------------------------------------------
679 Stable Name / Stable Pointer PrimOps
680 -------------------------------------------------------------------------- */
684 EF_(makeStableNamezh_fast);
686 #define stableNameToIntzh(r,s) (r = ((StgStableName *)s)->sn)
688 #define eqStableNamezh(r,sn1,sn2) \
689 (r = (((StgStableName *)sn1)->sn == ((StgStableName *)sn2)->sn))
691 #define makeStablePtrzh(r,a) \
692 r = RET_STGCALL1(StgStablePtr,getStablePtr,a)
694 #define deRefStablePtrzh(r,sp) do { \
695 ASSERT(stable_ptr_table[sp & ~STABLEPTR_WEIGHT_MASK].weight > 0); \
696 r = stable_ptr_table[sp & ~STABLEPTR_WEIGHT_MASK].addr; \
699 #define eqStablePtrzh(r,sp1,sp2) \
700 (r = ((sp1 & ~STABLEPTR_WEIGHT_MASK) == (sp2 & ~STABLEPTR_WEIGHT_MASK)))
704 /* -----------------------------------------------------------------------------
706 -------------------------------------------------------------------------- */
710 EF_(killThreadzh_fast);
713 #define myThreadIdzh(t) (t = CurrentTSO)
715 /* Hmm, I'll think about these later. */
716 /* -----------------------------------------------------------------------------
718 -------------------------------------------------------------------------- */
720 /* warning: extremely non-referentially transparent, need to hide in
721 an appropriate monad.
723 ToDo: follow indirections.
726 #define reallyUnsafePtrEqualityzh(r,a,b) r=((StgPtr)(a) == (StgPtr)(b))
728 /* -----------------------------------------------------------------------------
729 Weak Pointer PrimOps.
730 -------------------------------------------------------------------------- */
735 EF_(finalizzeWeakzh_fast);
737 #define deRefWeakzh(code,val,w) \
738 if (((StgWeak *)w)->header.info == &WEAK_info) { \
740 val = (P_)((StgWeak *)w)->value; \
746 #define sameWeakzh(w1,w2) ((w1)==(w2))
750 /* -----------------------------------------------------------------------------
751 Foreign Object PrimOps.
752 -------------------------------------------------------------------------- */
756 #define ForeignObj_CLOSURE_DATA(c) (((StgForeignObj *)c)->data)
758 EF_(makeForeignObjzh_fast);
760 #define writeForeignObjzh(res,datum) \
761 (ForeignObj_CLOSURE_DATA(res) = (P_)(datum))
763 #define eqForeignObj(f1,f2) ((f1)==(f2))
767 /* -----------------------------------------------------------------------------
769 -------------------------------------------------------------------------- */
771 #define dataToTagzh(r,a) r=(GET_TAG(((StgClosure *)a)->header.info))
772 /* tagToEnum# is handled directly by the code generator. */
774 /* -----------------------------------------------------------------------------
775 Signal processing. Not really primops, but called directly from
777 -------------------------------------------------------------------------- */
779 #define STG_SIG_DFL (-1)
780 #define STG_SIG_IGN (-2)
781 #define STG_SIG_ERR (-3)
782 #define STG_SIG_HAN (-4)
784 extern StgInt sig_install (StgInt, StgInt, StgStablePtr, sigset_t *);
785 #define stg_sig_default(sig,mask) sig_install(sig,STG_SIG_DFL,0,(sigset_t *)mask)
786 #define stg_sig_ignore(sig,mask) sig_install(sig,STG_SIG_IGN,0,(sigset_t *)mask)
787 #define stg_sig_catch(sig,ptr,mask) sig_install(sig,STG_SIG_HAN,ptr,(sigset_t *)mask)