HeapStackCheck.hc 24.8 KB
Newer Older
1
/* -----------------------------------------------------------------------------
2
 * $Id: HeapStackCheck.hc,v 1.13 2000/03/17 13:30:24 simonmar Exp $
3 4
 *
 * (c) The GHC Team, 1998-1999
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Canned Heap-Check and Stack-Check sequences.
 *
 * ---------------------------------------------------------------------------*/

#include "Rts.h"
#include "Storage.h"   	/* for CurrentTSO */
#include "StgRun.h"	/* for StgReturn and register saving */
#include "Schedule.h"   /* for context_switch */
#include "HeapStackCheck.h"

/* Stack/Heap Check Failure
 * ------------------------
 *
 * On discovering that a stack or heap check has failed, we do the following:
 *
 *    - If the context_switch flag is set, indicating that there are more
 *      threads waiting to run, we yield to the scheduler 
23
 *	(return ThreadYielding).
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
 *
 *    - If Hp > HpLim, we've had a heap check failure.  This means we've
 *	come to the end of the current heap block, so we try to chain
 *	another block on with ExtendNursery().  
 *
 *	     - If this succeeds, we carry on without returning to the 
 *	       scheduler.  
 *
 *	     - If it fails, we return to the scheduler claiming HeapOverflow
 *	       so that a garbage collection can be performed.
 *
 *    - If Hp <= HpLim, it must have been a stack check that failed.  In
 *	which case, we return to the scheduler claiming StackOverflow, the
 *	scheduler will either increase the size of our stack, or flag
 *	an error if the stack is already too big.
 *
 * The effect of checking for context switch only in the heap/stack check
 * failure code is that we'll switch threads after the current thread has
 * reached the end of its heap block.  If a thread isn't allocating
 * at all, it won't yield.  Hopefully this won't be a problem in practice.
 */
 
/* Remember that the return address is *removed* when returning to a
 * ThreadRunGHC thread.
 */


#define GC_GENERIC					\
  if (Hp > HpLim) {					\
    if (ExtendNursery(Hp,HpLim)) {			\
	if (context_switch) {				\
	    R1.i = ThreadYielding;			\
	} else {					\
 	   Sp++;					\
	   JMP_(ENTRY_CODE(Sp[-1]));			\
	}						\
    } else {						\
      R1.i = HeapOverflow;				\
    }							\
  } else {						\
    R1.i = StackOverflow;				\
  }							\
  SaveThreadState();					\
67
  CurrentTSO->what_next = ThreadRunGHC;			\
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  JMP_(StgReturn);

#define GC_ENTER					\
  if (Hp > HpLim) {					\
    if (ExtendNursery(Hp,HpLim)) {			\
	if (context_switch) {				\
	    R1.i = ThreadYielding;			\
	} else {					\
 	   R1.w = *Sp;					\
  	   Sp++;					\
	   JMP_(ENTRY_CODE(*R1.p));			\
	}						\
    } else {						\
      R1.i = HeapOverflow;				\
    }							\
  } else {						\
    R1.i = StackOverflow;				\
  }							\
  SaveThreadState();					\
87
  CurrentTSO->what_next = ThreadEnterGHC;		\
88 89 90 91
  JMP_(StgReturn);

#define HP_GENERIC			\
  SaveThreadState();			\
92
  CurrentTSO->what_next = ThreadRunGHC;	\
93 94 95 96 97
  R1.i = HeapOverflow;			\
  JMP_(StgReturn);

#define STK_GENERIC 			\
  SaveThreadState();			\
98
  CurrentTSO->what_next = ThreadRunGHC;	\
99 100 101 102 103
  R1.i = StackOverflow;			\
  JMP_(StgReturn);

#define YIELD_GENERIC			\
  SaveThreadState();			\
104
  CurrentTSO->what_next = ThreadRunGHC;	\
105 106 107 108 109
  R1.i = ThreadYielding;		\
  JMP_(StgReturn);

#define YIELD_TO_HUGS			  \
  SaveThreadState();			  \
110
  CurrentTSO->what_next = ThreadEnterHugs; \
111 112 113 114 115
  R1.i = ThreadYielding;		  \
  JMP_(StgReturn);

#define BLOCK_GENERIC			\
  SaveThreadState();			\
116
  CurrentTSO->what_next = ThreadRunGHC;	\
117 118 119 120 121
  R1.i = ThreadBlocked;			\
  JMP_(StgReturn);

#define BLOCK_ENTER			\
  SaveThreadState();			\
122
  CurrentTSO->what_next = ThreadEnterGHC;\
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  R1.i = ThreadBlocked;			\
  JMP_(StgReturn);

/* -----------------------------------------------------------------------------
   Heap Checks
   -------------------------------------------------------------------------- */

/*
 * This one is used when we want to *enter* the top thing on the stack
 * when we return, instead of the just returning to an address.  See
 * UpdatePAP for an example.
 */

EXTFUN(stg_gc_entertop)
{
  FB_
  GC_ENTER
  FE_
}

/* -----------------------------------------------------------------------------
   Heap checks in non-top-level thunks/functions.

   In these cases, node always points to the function closure.  This gives
   us an easy way to return to the function: just leave R1 on the top of
   the stack, and have the scheduler enter it to return.

   There are canned sequences for 'n' pointer values in registers.
   -------------------------------------------------------------------------- */

EXTFUN(stg_gc_enter_1)
{
  FB_
  Sp -= 1;
  Sp[0] = R1.w;
  GC_ENTER
  FE_
}

162 163 164 165 166 167 168
EXTFUN(stg_gc_enter_1_hponly)
{
  FB_
  Sp -= 1;
  Sp[0] = R1.w;
  R1.i = HeapOverflow;
  SaveThreadState();
169
  CurrentTSO->what_next = ThreadEnterGHC;
170 171 172 173
  JMP_(StgReturn);
  FE_
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
/*- 2 Regs--------------------------------------------------------------------*/

EXTFUN(stg_gc_enter_2)
{
  FB_
  Sp -= 2;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  GC_ENTER;
  FE_
}

/*- 3 Regs -------------------------------------------------------------------*/

EXTFUN(stg_gc_enter_3)
{
  FB_
  Sp -= 3;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  GC_ENTER;
  FE_
}

/*- 4 Regs -------------------------------------------------------------------*/

EXTFUN(stg_gc_enter_4)
{
  FB_
  Sp -= 4;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  GC_ENTER;
  FE_
}

/*- 5 Regs -------------------------------------------------------------------*/

EXTFUN(stg_gc_enter_5)
{
  FB_
  Sp -= 5;
  Sp[4] = R5.w;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  GC_ENTER;
  FE_
}

/*- 6 Regs -------------------------------------------------------------------*/

EXTFUN(stg_gc_enter_6)
{
  FB_
  Sp -= 6;
  Sp[5] = R6.w;
  Sp[4] = R5.w;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  GC_ENTER;
  FE_
}

/*- 7 Regs -------------------------------------------------------------------*/

EXTFUN(stg_gc_enter_7)
{
  FB_
  Sp -= 7;
  Sp[6] = R7.w;
  Sp[5] = R6.w;
  Sp[4] = R5.w;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  GC_ENTER;
  FE_
}

/*- 8 Regs -------------------------------------------------------------------*/

EXTFUN(stg_gc_enter_8)
{
  FB_
  Sp -= 8;
  Sp[7] = R8.w;
  Sp[6] = R7.w;
  Sp[5] = R6.w;
  Sp[4] = R5.w;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  GC_ENTER;
  FE_
}

279 280 281 282 283 284 285 286 287 288 289 290 291
#if defined(GRAN)
/*
  ToDo: merge the block and yield macros, calling something like BLOCK(N)
        at the end;
*/

/* 
   Should we actually ever do a yield in such a case?? -- HWL
*/
EXTFUN(gran_yield_0)
{
  FB_
  SaveThreadState();					
292
  CurrentTSO->what_next = ThreadEnterGHC;		
293 294 295 296 297 298 299 300 301 302 303
  R1.i = ThreadYielding;
  JMP_(StgReturn);
  FE_
}

EXTFUN(gran_yield_1)
{
  FB_
  Sp -= 1;
  Sp[0] = R1.w;
  SaveThreadState();					
304
  CurrentTSO->what_next = ThreadEnterGHC;		
305 306 307 308 309 310 311 312 313 314 315 316 317 318
  R1.i = ThreadYielding;
  JMP_(StgReturn);
  FE_
}

/*- 2 Regs--------------------------------------------------------------------*/

EXTFUN(gran_yield_2)
{
  FB_
  Sp -= 2;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
319
  CurrentTSO->what_next = ThreadEnterGHC;		
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  R1.i = ThreadYielding;
  JMP_(StgReturn);
  FE_
}

/*- 3 Regs -------------------------------------------------------------------*/

EXTFUN(gran_yield_3)
{
  FB_
  Sp -= 3;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
335
  CurrentTSO->what_next = ThreadEnterGHC;		
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  R1.i = ThreadYielding;
  JMP_(StgReturn);
  FE_
}

/*- 4 Regs -------------------------------------------------------------------*/

EXTFUN(gran_yield_4)
{
  FB_
  Sp -= 4;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
352
  CurrentTSO->what_next = ThreadEnterGHC;		
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
  R1.i = ThreadYielding;
  JMP_(StgReturn);
  FE_
}

/*- 5 Regs -------------------------------------------------------------------*/

EXTFUN(gran_yield_5)
{
  FB_
  Sp -= 5;
  Sp[4] = R5.w;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
370
  CurrentTSO->what_next = ThreadEnterGHC;		
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  R1.i = ThreadYielding;
  JMP_(StgReturn);
  FE_
}

/*- 6 Regs -------------------------------------------------------------------*/

EXTFUN(gran_yield_6)
{
  FB_
  Sp -= 6;
  Sp[5] = R6.w;
  Sp[4] = R5.w;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
389
  CurrentTSO->what_next = ThreadEnterGHC;		
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
  R1.i = ThreadYielding;
  JMP_(StgReturn);
  FE_
}

/*- 7 Regs -------------------------------------------------------------------*/

EXTFUN(gran_yield_7)
{
  FB_
  Sp -= 7;
  Sp[6] = R7.w;
  Sp[5] = R6.w;
  Sp[4] = R5.w;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
409
  CurrentTSO->what_next = ThreadEnterGHC;		
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
  R1.i = ThreadYielding;
  JMP_(StgReturn);
  FE_
}

/*- 8 Regs -------------------------------------------------------------------*/

EXTFUN(gran_yield_8)
{
  FB_
  Sp -= 8;
  Sp[7] = R8.w;
  Sp[6] = R7.w;
  Sp[5] = R6.w;
  Sp[4] = R5.w;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
430
  CurrentTSO->what_next = ThreadEnterGHC;		
431 432 433 434 435 436 437 438 439 440 441 442 443
  R1.i = ThreadYielding;
  JMP_(StgReturn);
  FE_
}

// the same routines but with a block rather than a yield

EXTFUN(gran_block_1)
{
  FB_
  Sp -= 1;
  Sp[0] = R1.w;
  SaveThreadState();					
444
  CurrentTSO->what_next = ThreadEnterGHC;		
445 446 447 448 449 450 451 452 453 454 455 456 457 458
  R1.i = ThreadBlocked;
  JMP_(StgReturn);
  FE_
}

/*- 2 Regs--------------------------------------------------------------------*/

EXTFUN(gran_block_2)
{
  FB_
  Sp -= 2;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
459
  CurrentTSO->what_next = ThreadEnterGHC;		
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
  R1.i = ThreadBlocked;
  JMP_(StgReturn);
  FE_
}

/*- 3 Regs -------------------------------------------------------------------*/

EXTFUN(gran_block_3)
{
  FB_
  Sp -= 3;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
475
  CurrentTSO->what_next = ThreadEnterGHC;		
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
  R1.i = ThreadBlocked;
  JMP_(StgReturn);
  FE_
}

/*- 4 Regs -------------------------------------------------------------------*/

EXTFUN(gran_block_4)
{
  FB_
  Sp -= 4;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
492
  CurrentTSO->what_next = ThreadEnterGHC;		
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
  R1.i = ThreadBlocked;
  JMP_(StgReturn);
  FE_
}

/*- 5 Regs -------------------------------------------------------------------*/

EXTFUN(gran_block_5)
{
  FB_
  Sp -= 5;
  Sp[4] = R5.w;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
510
  CurrentTSO->what_next = ThreadEnterGHC;		
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
  R1.i = ThreadBlocked;
  JMP_(StgReturn);
  FE_
}

/*- 6 Regs -------------------------------------------------------------------*/

EXTFUN(gran_block_6)
{
  FB_
  Sp -= 6;
  Sp[5] = R6.w;
  Sp[4] = R5.w;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
529
  CurrentTSO->what_next = ThreadEnterGHC;		
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
  R1.i = ThreadBlocked;
  JMP_(StgReturn);
  FE_
}

/*- 7 Regs -------------------------------------------------------------------*/

EXTFUN(gran_block_7)
{
  FB_
  Sp -= 7;
  Sp[6] = R7.w;
  Sp[5] = R6.w;
  Sp[4] = R5.w;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
549
  CurrentTSO->what_next = ThreadEnterGHC;		
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
  R1.i = ThreadBlocked;
  JMP_(StgReturn);
  FE_
}

/*- 8 Regs -------------------------------------------------------------------*/

EXTFUN(gran_block_8)
{
  FB_
  Sp -= 8;
  Sp[7] = R8.w;
  Sp[6] = R7.w;
  Sp[5] = R6.w;
  Sp[4] = R5.w;
  Sp[3] = R4.w;
  Sp[2] = R3.w;
  Sp[1] = R2.w;
  Sp[0] = R1.w;
  SaveThreadState();					
570
  CurrentTSO->what_next = ThreadEnterGHC;		
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
  R1.i = ThreadBlocked;
  JMP_(StgReturn);
  FE_
}

#endif

#if 0 && defined(PAR)

/*
  Similar to stg_block_1 (called via StgMacro BLOCK_NP) but separates the
  saving of the thread state from the actual jump via an StgReturn.
  We need this separation because we call RTS routines in blocking entry codes
  before jumping back into the RTS (see parallel/FetchMe.hc).
*/

EXTFUN(par_block_1_no_jump)
{
  FB_
  Sp -= 1;
  Sp[0] = R1.w;
  SaveThreadState();					
  FE_
}

EXTFUN(par_jump)
{
  FB_
599
  CurrentTSO->what_next = ThreadEnterGHC;		
600 601 602 603 604 605 606
  R1.i = ThreadBlocked;
  JMP_(StgReturn);
  FE_
}

#endif

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
/* -----------------------------------------------------------------------------
   For a case expression on a polymorphic or function-typed object, if
   the default branch (there can only be one branch) of the case fails
   a heap-check, instead of using stg_gc_enter_1 as normal, we must
   push a new SEQ frame on the stack, followed by the object returned.  

   Otherwise, if the object is a function, it won't return to the
   correct activation record on returning from garbage collection.  It will
   assume it has some arguments and apply itself.
   -------------------------------------------------------------------------- */

EXTFUN(stg_gc_seq_1)
{
  FB_
  Sp -= 1 + sizeofW(StgSeqFrame);
  PUSH_SEQ_FRAME(Sp+1);
  *Sp = R1.w;
  GC_ENTER;
  FE_
}

/* -----------------------------------------------------------------------------
   Heap checks in Primitive case alternatives

   A primitive case alternative is entered with a value either in 
   R1, FloatReg1 or D1 depending on the return convention.  All the
   cases are covered below.
   -------------------------------------------------------------------------- */

636 637
/*-- No regsiters live (probably a void return) ----------------------------- */

638 639 640 641
/* If we change the policy for thread startup to *not* remove the
 * return address from the stack, we can get rid of this little
 * function/info table...  
 */
642 643 644 645 646 647 648 649 650 651
INFO_TABLE_SRT_BITMAP(stg_gc_noregs_ret_info, stg_gc_noregs_ret, 0/*BITMAP*/, 
		      0/*SRT*/, 0/*SRT_OFF*/, 0/*SRT_LEN*/, 
		      RET_SMALL,, EF_, 0, 0);

EXTFUN(stg_gc_noregs_ret)
{
  FB_
  JMP_(ENTRY_CODE(Sp[0]));
  FE_
}
652 653 654 655

EXTFUN(stg_gc_noregs)
{
  FB_
656 657
  Sp -= 1;
  Sp[0] = (W_)&stg_gc_noregs_ret_info;
658 659 660 661 662 663 664 665
  GC_GENERIC
  FE_
}

/*-- R1 is boxed/unpointed -------------------------------------------------- */

INFO_TABLE_SRT_BITMAP(stg_gc_unpt_r1_info, stg_gc_unpt_r1_entry, 0/*BITMAP*/, 
		      0/*SRT*/, 0/*SRT_OFF*/, 0/*SRT_LEN*/, 
666
		      RET_SMALL,, EF_, 0, 0);
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690

EXTFUN(stg_gc_unpt_r1_entry)
{
  FB_
  R1.w = Sp[0];
  Sp += 1;
  JMP_(ENTRY_CODE(Sp[0]));
  FE_
}

EXTFUN(stg_gc_unpt_r1)
{
  FB_
  Sp -= 2;
  Sp[1] = R1.w;
  Sp[0] = (W_)&stg_gc_unpt_r1_info;
  GC_GENERIC
  FE_
}

/*-- R1 is unboxed -------------------------------------------------- */

INFO_TABLE_SRT_BITMAP(stg_gc_unbx_r1_info, stg_gc_unbx_r1_entry, 1/*BITMAP*/,
		      0/*SRT*/, 0/*SRT_OFF*/, 0/*SRT_LEN*/, 
691
		      RET_SMALL,, EF_, 0, 0);
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
/* the 1 is a bitmap - i.e. 1 non-pointer word on the stack. */

EXTFUN(stg_gc_unbx_r1_entry)
{
  FB_
  R1.w = Sp[0];
  Sp += 1;
  JMP_(ENTRY_CODE(Sp[0]));
  FE_
}

EXTFUN(stg_gc_unbx_r1)
{
  FB_
  Sp -= 2;
  Sp[1] = R1.w;
  Sp[0] = (W_)&stg_gc_unbx_r1_info;
  GC_GENERIC
  FE_
}

/*-- F1 contains a float ------------------------------------------------- */

INFO_TABLE_SRT_BITMAP(stg_gc_f1_info, stg_gc_f1_entry, 1/*BITMAP*/,
		      0/*SRT*/, 0/*SRT_OFF*/, 0/*SRT_LEN*/, 
717
		      RET_SMALL,, EF_, 0, 0);
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749

EXTFUN(stg_gc_f1_entry)
{
  FB_
  F1 = PK_FLT(Sp);
  Sp += 1;
  JMP_(ENTRY_CODE(Sp[0]));
  FE_
}

EXTFUN(stg_gc_f1)
{
  FB_
  Sp -= 2;
  ASSIGN_FLT(Sp+1, F1);
  Sp[0] = (W_)&stg_gc_f1_info;
  GC_GENERIC
  FE_
}

/*-- D1 contains a double ------------------------------------------------- */

/* we support doubles of either 1 or 2 words in size */

#if SIZEOF_DOUBLE == SIZEOF_VOID_P
#  define DBL_BITMAP 1
#else
#  define DBL_BITMAP 3
#endif 

INFO_TABLE_SRT_BITMAP(stg_gc_d1_info, stg_gc_d1_entry, DBL_BITMAP,
		      0/*SRT*/, 0/*SRT_OFF*/, 0/*SRT_LEN*/, 
750
		      RET_SMALL,, EF_, 0, 0);
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802

EXTFUN(stg_gc_d1_entry)
{
  FB_
  D1 = PK_DBL(Sp);
  Sp += sizeofW(StgDouble);
  JMP_(ENTRY_CODE(Sp[0]));
  FE_
}

EXTFUN(stg_gc_d1)
{
  FB_
  Sp -= 1 + sizeofW(StgDouble);
  ASSIGN_DBL(Sp+1,D1);
  Sp[0] = (W_)&stg_gc_d1_info;
  GC_GENERIC
  FE_
}

/* -----------------------------------------------------------------------------
   Heap checks for unboxed tuple case alternatives

   The story is: 

      - for an unboxed tuple with n components, we rearrange the components
	with pointers first followed by non-pointers. (NB: not done yet)
 
      - The first k components are allocated registers, where k is the
        number of components that will fit in real registers.

      - The rest are placed on the stack, with space left for tagging
        of the non-pointer block if necessary.

      - On failure of a heap check:
		- the tag is filled in if necessary,
		- we load Ri with the address of the continuation,
		  where i is the lowest unused vanilla register.
		- jump to 'stg_gc_ut_x_y' where x is the number of pointer
		  registers and y the number of non-pointers.
		- if the required canned sequence isn't available, it will
		  have to be generated at compile-time by the code
		  generator (this will probably happen if there are
		  floating-point values, for instance).
  
   For now, just deal with R1, hence R2 contains the sequel address.
   -------------------------------------------------------------------------- */

/*---- R1 contains a pointer: ------ */

INFO_TABLE_SRT_BITMAP(stg_gc_ut_1_0_info, stg_gc_ut_1_0_entry, 1/*BITMAP*/, 
		      0/*SRT*/, 0/*SRT_OFF*/, 0/*SRT_LEN*/, 
803
		      RET_SMALL,, EF_, 0, 0);
804 805 806 807 808 809

EXTFUN(stg_gc_ut_1_0_entry)
{
  FB_
  R1.w = Sp[1];
  Sp += 2;
810
  JMP_(ENTRY_CODE(Sp[-2]));
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
  FE_
}

EXTFUN(stg_gc_ut_1_0)
{
  FB_
  Sp -= 3;
  Sp[2] = R1.w;
  Sp[1] = R2.w;
  Sp[0] = (W_)&stg_gc_ut_1_0_info;
  GC_GENERIC
  FE_
}

/*---- R1 contains a non-pointer: ------ */

INFO_TABLE_SRT_BITMAP(stg_gc_ut_0_1_info, stg_gc_ut_0_1_entry, 3/*BITMAP*/, 
		      0/*SRT*/, 0/*SRT_OFF*/, 0/*SRT_LEN*/, 
829
		      RET_SMALL,, EF_, 0, 0);
830 831 832 833 834 835

EXTFUN(stg_gc_ut_0_1_entry)
{
  FB_
  R1.w = Sp[1];
  Sp += 2;
836
  JMP_(ENTRY_CODE(Sp[-2]));
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
  FE_
}

EXTFUN(stg_gc_ut_0_1)
{
  FB_
  Sp -= 3;
  Sp[0] = (W_)&stg_gc_ut_0_1_info;
  Sp[1] = R2.w;
  Sp[2] = R1.w;
  GC_GENERIC
  FE_
}

/* -----------------------------------------------------------------------------
   Standard top-level fast-entry heap checks.

   - we want to make the stack look like it should at the slow entry
     point for the function.  That way we can just push the slow
     entry point on the stack and return using ThreadRunGHC.

   - The compiler will generate code to fill in any tags on the stack,
     in case we arrived directly at the fast entry point and these tags
     aren't present.

   - The rest is hopefully handled by jumping to a canned sequence.
     We currently have canned sequences for 0-8 pointer registers.  If
     any registers contain non-pointers, we must reduce to an all-pointers
     situation by pushing as many registers on the stack as necessary.

     eg. if R1, R2 contain pointers and R3 contains a word, the heap check
         failure sequence looks like this:

		Sp[-1] = R3.w;
	 	Sp[-2] = WORD_TAG;
		Sp -= 2;
		JMP_(stg_chk_2)

	  after pushing R3, we have pointers in R1 and R2 which corresponds
	  to the 2-pointer canned sequence.

  -------------------------------------------------------------------------- */

/*- 0 Regs -------------------------------------------------------------------*/

EXTFUN(stg_chk_0)
{
  FB_
  Sp -= 1;
  Sp[0] = R1.w;
  GC_GENERIC;
  FE_
}

/*- 1 Reg --------------------------------------------------------------------*/

EXTFUN(stg_chk_1)
{
  FB_
  Sp -= 2;
  Sp[1] = R1.w;
  Sp[0] = R2.w;
  GC_GENERIC;
  FE_
}

/*- 1 Reg (non-ptr) ----------------------------------------------------------*/

EXTFUN(stg_chk_1n)
{
  FB_
  Sp -= 3;
  Sp[2] = R1.w;
  Sp[1] = WORD_TAG; /* ToDo: or maybe its an int? */
  Sp[0] = R2.w;
  GC_GENERIC;
  FE_
}

/*- 2 Regs--------------------------------------------------------------------*/

EXTFUN(stg_chk_2)
{
  FB_
  Sp -= 3;
  Sp[2] = R2.w;
  Sp[1] = R1.w;
  Sp[0] = R3.w;
  GC_GENERIC;
  FE_
}

/*- 3 Regs -------------------------------------------------------------------*/

EXTFUN(stg_chk_3)
{
  FB_
  Sp -= 4;
  Sp[3] = R3.w;
  Sp[2] = R2.w;
  Sp[1] = R1.w;
  Sp[0] = R4.w;
  GC_GENERIC;
  FE_
}

/*- 4 Regs -------------------------------------------------------------------*/

EXTFUN(stg_chk_4)
{
  FB_
  Sp -= 5;
  Sp[4] = R4.w;
  Sp[3] = R3.w;
  Sp[2] = R2.w;
  Sp[1] = R1.w;
  Sp[0] = R5.w;
  GC_GENERIC;
  FE_
}

/*- 5 Regs -------------------------------------------------------------------*/

EXTFUN(stg_chk_5)
{
  FB_
  Sp -= 6;
  Sp[5] = R5.w;
  Sp[4] = R4.w;
  Sp[3] = R3.w;
  Sp[2] = R2.w;
  Sp[1] = R1.w;
  Sp[0] = R6.w;
  GC_GENERIC;
  FE_
}

/*- 6 Regs -------------------------------------------------------------------*/

EXTFUN(stg_chk_6)
{
  FB_
  Sp -= 7;
  Sp[6] = R6.w;
  Sp[5] = R5.w;
  Sp[4] = R4.w;
  Sp[3] = R3.w;
  Sp[2] = R2.w;
  Sp[1] = R1.w;
  Sp[0] = R7.w;
  GC_GENERIC;
  FE_
}

/*- 7 Regs -------------------------------------------------------------------*/

EXTFUN(stg_chk_7)
{
  FB_
  Sp -= 8;
  Sp[7] = R7.w;
  Sp[6] = R6.w;
  Sp[5] = R5.w;
  Sp[4] = R4.w;
  Sp[3] = R3.w;
  Sp[2] = R2.w;
  Sp[1] = R1.w;
  Sp[0] = R8.w;
  GC_GENERIC;
  FE_
}

/*- 8 Regs -------------------------------------------------------------------*/

EXTFUN(stg_chk_8)
{
  FB_
  Sp -= 9;
  Sp[8] = R8.w;
  Sp[7] = R7.w;
  Sp[6] = R6.w;
  Sp[5] = R5.w;
  Sp[4] = R4.w;
  Sp[3] = R3.w;
  Sp[2] = R2.w;
  Sp[1] = R1.w;
  Sp[0] = R9.w;
  GC_GENERIC;
  FE_
}

/* -----------------------------------------------------------------------------
   Generic Heap Check Code.

   Called with Liveness mask in R9,  Return address in R10.
   Stack must be consistent (tagged, and containing all necessary info pointers
   to relevant SRTs).

   We also define an stg_gen_yield here, because it's very similar.
   -------------------------------------------------------------------------- */

#if SIZEOF_DOUBLE > SIZEOF_VOID_P

#define RESTORE_EVERYTHING			\
    D2   = PK_DBL(Sp+16);			\
    D1   = PK_DBL(Sp+14);			\
    F4   = PK_FLT(Sp+13);			\
    F3   = PK_FLT(Sp+12);			\
    F2   = PK_FLT(Sp+11);			\
    F1   = PK_FLT(Sp+10);			\
    R8.w = Sp[9];				\
    R7.w = Sp[8];				\
    R6.w = Sp[7];				\
    R5.w = Sp[6];				\
    R4.w = Sp[5];				\
    R3.w = Sp[4];				\
    R2.w = Sp[3];				\
    R1.w = Sp[2];				\
    Sp += 18;

#define RET_OFFSET (-17)

#define SAVE_EVERYTHING				\
    ASSIGN_DBL(Sp-2,D2);			\
    ASSIGN_DBL(Sp-4,D1);			\
    ASSIGN_FLT(Sp-5,F4);			\
    ASSIGN_FLT(Sp-6,F3);			\
    ASSIGN_FLT(Sp-7,F2);			\
    ASSIGN_FLT(Sp-8,F1);			\
    Sp[-9]  = R8.w;				\
    Sp[-10] = R7.w;				\
    Sp[-11] = R6.w;				\
    Sp[-12] = R5.w;				\
    Sp[-13] = R4.w;				\
    Sp[-14] = R3.w;				\
    Sp[-15] = R2.w;				\
    Sp[-16] = R1.w;				\
    Sp[-17] = R10.w;    /* return address */	\
    Sp[-18] = R9.w;     /* liveness mask  */	\
    Sp[-19] = (W_)&stg_gen_chk_info;		\
    Sp -= 19;

#else

#define RESTORE_EVERYTHING			\
    D2   = PK_DBL(Sp+15);			\
    D1   = PK_DBL(Sp+14);			\
    F4   = PK_FLT(Sp+13);			\
    F3   = PK_FLT(Sp+12);			\
    F2   = PK_FLT(Sp+11);			\
    F1   = PK_FLT(Sp+10);			\
    R8.w = Sp[9];				\
    R7.w = Sp[8];				\
    R6.w = Sp[7];				\
    R5.w = Sp[6];				\
    R4.w = Sp[5];				\
    R3.w = Sp[4];				\
    R2.w = Sp[3];				\
    R1.w = Sp[2];				\
    Sp += 16;

#define RET_OFFSET (-15)

#define SAVE_EVERYTHING				\
    ASSIGN_DBL(Sp-1,D2);			\
    ASSIGN_DBL(Sp-2,D1);			\
    ASSIGN_FLT(Sp-3,F4);			\
    ASSIGN_FLT(Sp-4,F3);			\
    ASSIGN_FLT(Sp-5,F2);			\
    ASSIGN_FLT(Sp-6,F1);			\
    Sp[-7]  = R8.w;				\
    Sp[-8]  = R7.w;				\
    Sp[-9]  = R6.w;				\
    Sp[-10] = R5.w;				\
    Sp[-11] = R4.w;				\
    Sp[-12] = R3.w;				\
    Sp[-13] = R2.w;				\
    Sp[-14] = R1.w;				\
    Sp[-15] = R10.w;    /* return address */	\
    Sp[-16] = R9.w;     /* liveness mask  */	\
    Sp[-17] = (W_)&stg_gen_chk_info;		\
    Sp -= 17;

#endif

INFO_TABLE_SRT_BITMAP(stg_gen_chk_info, stg_gen_chk_ret, 0,
		      0/*SRT*/, 0/*SRT_OFF*/, 0/*SRT_LEN*/, 
1124
	              RET_DYN,, EF_, 0, 0);
1125 1126 1127 1128 1129 1130 1131 1132

/* bitmap in the above info table is unused, the real one is on the stack. 
 */

FN_(stg_gen_chk_ret)
{
  FB_
  RESTORE_EVERYTHING;
1133
  JMP_(Sp[RET_OFFSET]); /* NO ENTRY_CODE() - this is a direct ret address */
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
  FE_
}

FN_(stg_gen_chk)
{
  FB_
  SAVE_EVERYTHING;
  GC_GENERIC
  FE_
}	  

/*
 * stg_gen_hp is used by MAYBE_GC, where we can't use GC_GENERIC
 * because we've just failed doYouWantToGC(), not a standard heap
 * check.  GC_GENERIC would end up returning StackOverflow.
 */
FN_(stg_gen_hp)
{
  FB_
  SAVE_EVERYTHING;
  HP_GENERIC
  FE_
}	  

1158 1159 1160 1161
/* -----------------------------------------------------------------------------
   Yields
   -------------------------------------------------------------------------- */

1162 1163 1164 1165 1166 1167 1168 1169
FN_(stg_gen_yield)
{
  FB_
  SAVE_EVERYTHING;
  YIELD_GENERIC
  FE_
}

1170 1171 1172
FN_(stg_yield_noregs)
{
  FB_
1173
  Sp--;
1174
  Sp[0] = (W_)&stg_gc_noregs_ret_info;
1175
  YIELD_GENERIC;
1176 1177 1178
  FE_
}

1179 1180 1181 1182 1183 1184 1185 1186
FN_(stg_yield_to_Hugs)
{
  FB_
  /* No need to save everything - no live registers */
  YIELD_TO_HUGS
  FE_
}

1187 1188 1189 1190
/* -----------------------------------------------------------------------------
   Blocks
   -------------------------------------------------------------------------- */

1191 1192 1193 1194 1195 1196 1197 1198
FN_(stg_gen_block)
{
  FB_
  SAVE_EVERYTHING;
  BLOCK_GENERIC
  FE_
}

1199 1200 1201 1202 1203 1204 1205 1206 1207
FN_(stg_block_noregs)
{
  FB_
  Sp--;
  Sp[0] = (W_)&stg_gc_noregs_ret_info;
  BLOCK_GENERIC;
  FE_
}

1208 1209 1210 1211 1212 1213 1214 1215
FN_(stg_block_1)
{
  FB_
  Sp--;
  Sp[0] = R1.w;
  BLOCK_ENTER;
  FE_
}