summaryrefslogtreecommitdiff
path: root/rts/HeapStackCheck.cmm
blob: e9ddf5b69e18cafe3767a965065457ee3cc6b254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
162
163
164
165
166
167
168
169
170
171
172
173
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
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
599
600
601
602
603
604
605
606
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
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
717
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
750
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
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
/* -----------------------------------------------------------------------------
 *
 * (c) The GHC Team, 1998-2004
 *
 * Canned Heap-Check and Stack-Check sequences.
 *
 * This file is written in a subset of C--, extended with various
 * features specific to GHC.  It is compiled by GHC directly.  For the
 * syntax of .cmm files, see the parser in ghc/compiler/cmm/CmmParse.y.
 *
 * ---------------------------------------------------------------------------*/

#include "Cmm.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 
 *	(return ThreadYielding).
 *
 *    - 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 raise
 *	an exception 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.
 */
 
#define PRE_RETURN(why,what_next)			\
  StgTSO_what_next(CurrentTSO) = what_next::I16;	\
  StgRegTable_rRet(BaseReg) = why;           	        \
  R1 = BaseReg;

/* Remember that the return address is *removed* when returning to a
 * ThreadRunGHC thread.
 */

#define GC_GENERIC						\
    DEBUG_ONLY(foreign "C" heapCheckFail());			\
    if (Hp > HpLim) {						\
        Hp = Hp - HpAlloc/*in bytes*/;				\
        if (HpAlloc <= BLOCK_SIZE				\
            && bdescr_link(CurrentNursery) != NULL) {		\
            CLOSE_NURSERY();					\
            CurrentNursery = bdescr_link(CurrentNursery);	\
            OPEN_NURSERY();					\
            if (CInt[context_switch] != 0 :: CInt) {		\
                R1 = ThreadYielding;				\
                goto sched;					\
            } else {						\
                jump %ENTRY_CODE(Sp(0));			\
            }							\
	} else {						\
            R1 = HeapOverflow;					\
            goto sched;						\
        }							\
    } else {							\
        R1 = StackOverflow;					\
    }								\
  sched:							\
    PRE_RETURN(R1,ThreadRunGHC);				\
    jump stg_returnToSched;

#define HP_GENERIC				\
   PRE_RETURN(HeapOverflow, ThreadRunGHC)	\
  jump stg_returnToSched;

#define BLOCK_GENERIC				\
   PRE_RETURN(ThreadBlocked,  ThreadRunGHC)	\
  jump stg_returnToSched;

#define YIELD_GENERIC				\
  PRE_RETURN(ThreadYielding, ThreadRunGHC)	\
  jump stg_returnToSched;

#define BLOCK_BUT_FIRST(c)			\
  PRE_RETURN(ThreadBlocked, ThreadRunGHC)	\
  R2 = c;					\
  jump stg_returnToSchedButFirst;

#define YIELD_TO_INTERPRETER			\
  PRE_RETURN(ThreadYielding, ThreadInterpret)	\
  jump stg_returnToSchedNotPaused;

/* -----------------------------------------------------------------------------
   Heap checks in 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.
   -------------------------------------------------------------------------- */

INFO_TABLE_RET( stg_enter, 1/*framesize*/, 0/*bitmap*/, RET_SMALL)
{
    R1 = Sp(1);
    Sp_adj(2);
    ENTER();
}

__stg_gc_enter_1
{
    Sp_adj(-2);
    Sp(1) = R1;
    Sp(0) = stg_enter_info;
    GC_GENERIC
}

#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
*/
gran_yield_0
{
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadYielding;
    jump StgReturn;
}

gran_yield_1
{
    Sp_adj(-1);
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadYielding;
    jump StgReturn;
}

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

gran_yield_2
{
    Sp_adj(-2);
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadYielding;
    jump StgReturn;
}

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

gran_yield_3
{
    Sp_adj(-3);
    Sp(2) = R3;
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadYielding;
    jump StgReturn;
}

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

gran_yield_4
{
    Sp_adj(-4);
    Sp(3) = R4;
    Sp(2) = R3;
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadYielding;
    jump StgReturn;
}

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

gran_yield_5
{
    Sp_adj(-5);
    Sp(4) = R5;
    Sp(3) = R4;
    Sp(2) = R3;
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadYielding;
    jump StgReturn;
}

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

gran_yield_6
{
    Sp_adj(-6);
    Sp(5) = R6;
    Sp(4) = R5;
    Sp(3) = R4;
    Sp(2) = R3;
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadYielding;
    jump StgReturn;
}

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

gran_yield_7
{
    Sp_adj(-7);
    Sp(6) = R7;
    Sp(5) = R6;
    Sp(4) = R5;
    Sp(3) = R4;
    Sp(2) = R3;
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadYielding;
    jump StgReturn;
}

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

gran_yield_8
{
    Sp_adj(-8);
    Sp(7) = R8;
    Sp(6) = R7;
    Sp(5) = R6;
    Sp(4) = R5;
    Sp(3) = R4;
    Sp(2) = R3;
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadYielding;
    jump StgReturn;
}

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

gran_block_1
{
    Sp_adj(-1);
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadBlocked;
    jump StgReturn;
}

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

gran_block_2
{
    Sp_adj(-2);
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadBlocked;
    jump StgReturn;
}

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

gran_block_3
{
    Sp_adj(-3);
    Sp(2) = R3;
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadBlocked;
    jump StgReturn;
}

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

gran_block_4
{
    Sp_adj(-4);
    Sp(3) = R4;
    Sp(2) = R3;
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadBlocked;
    jump StgReturn;
}

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

gran_block_5
{
    Sp_adj(-5);
    Sp(4) = R5;
    Sp(3) = R4;
    Sp(2) = R3;
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadBlocked;
    jump StgReturn;
}

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

gran_block_6
{
    Sp_adj(-6);
    Sp(5) = R6;
    Sp(4) = R5;
    Sp(3) = R4;
    Sp(2) = R3;
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadBlocked;
    jump StgReturn;
}

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

gran_block_7
{
    Sp_adj(-7);
    Sp(6) = R7;
    Sp(5) = R6;
    Sp(4) = R5;
    Sp(3) = R4;
    Sp(2) = R3;
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadBlocked;
    jump StgReturn;
}

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

gran_block_8
{
    Sp_adj(-8);
    Sp(7) = R8;
    Sp(6) = R7;
    Sp(5) = R6;
    Sp(4) = R5;
    Sp(3) = R4;
    Sp(2) = R3;
    Sp(1) = R2;
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadBlocked;
    jump StgReturn;
}

#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).
*/

par_block_1_no_jump
{
    Sp_adj(-1);
    Sp(0) = R1;
    SAVE_THREAD_STATE();					
}

par_jump
{
    TSO_what_next(CurrentTSO) = ThreadRunGHC;		
    R1 = ThreadBlocked;
    jump StgReturn;
}

#endif

/* -----------------------------------------------------------------------------
   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.
   -------------------------------------------------------------------------- */

/*-- No Registers live ------------------------------------------------------ */

stg_gc_noregs
{
    GC_GENERIC
}

/*-- void return ------------------------------------------------------------ */

INFO_TABLE_RET( stg_gc_void, 0/*framesize*/, 0/*bitmap*/, RET_SMALL)
{
    Sp_adj(1);
    jump %ENTRY_CODE(Sp(0));
}

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

INFO_TABLE_RET( stg_gc_unpt_r1, 1/*framesize*/, 0/*bitmap*/, RET_SMALL)
{
    R1 = Sp(1);
    Sp_adj(2);
    jump %ENTRY_CODE(Sp(0));
}

stg_gc_unpt_r1
{
    Sp_adj(-2);
    Sp(1) = R1;
    Sp(0) = stg_gc_unpt_r1_info;
    GC_GENERIC
}

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

/* the 1 is a bitmap - i.e. 1 non-pointer word on the stack. */
INFO_TABLE_RET(	stg_gc_unbx_r1, 1/*framesize*/, 1/*bitmap*/, RET_SMALL )
{
    R1 = Sp(1);
    Sp_adj(2);
    jump %ENTRY_CODE(Sp(0));
}

stg_gc_unbx_r1
{
    Sp_adj(-2);
    Sp(1) = R1;
    Sp(0) = stg_gc_unbx_r1_info;
    GC_GENERIC
}

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

INFO_TABLE_RET(	stg_gc_f1, 1/*framesize*/, 1/*bitmap*/, RET_SMALL )
{
    F1 = F_[Sp+WDS(1)];
    Sp_adj(2);
    jump %ENTRY_CODE(Sp(0));
}

stg_gc_f1
{
    Sp_adj(-2);
    F_[Sp + WDS(1)] = F1;
    Sp(0) = stg_gc_f1_info;
    GC_GENERIC
}

/*-- 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
#  define DBL_WORDS  1
#else
#  define DBL_BITMAP 3
#  define DBL_WORDS  2
#endif 

INFO_TABLE_RET(	stg_gc_d1, DBL_WORDS/*framesize*/, DBL_BITMAP/*bitmap*/, RET_SMALL )
{
    D1 = D_[Sp + WDS(1)];
    Sp = Sp + WDS(1) + SIZEOF_StgDouble;
    jump %ENTRY_CODE(Sp(0));
}

stg_gc_d1
{
    Sp = Sp - WDS(1) - SIZEOF_StgDouble;
    D_[Sp + WDS(1)] = D1;
    Sp(0) = stg_gc_d1_info;
    GC_GENERIC
}


/*-- L1 contains an int64 ------------------------------------------------- */

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

#if SIZEOF_VOID_P == 8
#  define LLI_BITMAP 1
#  define LLI_WORDS  1
#else
#  define LLI_BITMAP 3
#  define LLI_WORDS  2
#endif 

INFO_TABLE_RET( stg_gc_l1, LLI_WORDS/*framesize*/, LLI_BITMAP/*bitmap*/, RET_SMALL )
{
    L1 = L_[Sp + WDS(1)];
    Sp_adj(1) + SIZEOF_StgWord64;
    jump %ENTRY_CODE(Sp(0));
}

stg_gc_l1
{
    Sp_adj(-1) - SIZEOF_StgWord64;
    L_[Sp + WDS(1)] = L1;
    Sp(0) = stg_gc_l1_info;
    GC_GENERIC
}

/*-- Unboxed tuple return, one pointer (unregisterised build only) ---------- */

INFO_TABLE_RET( stg_ut_1_0_unreg, 1/*size*/, 0/*BITMAP*/, RET_SMALL )
{
    Sp_adj(1);
    // one ptr is on the stack (Sp(0))
    jump %ENTRY_CODE(Sp(1));
}

/* -----------------------------------------------------------------------------
   Generic function entry heap check code.

   At a function entry point, the arguments are as per the calling convention,
   i.e. some in regs and some on the stack.  There may or may not be 
   a pointer to the function closure in R1 - if there isn't, then the heap
   check failure code in the function will arrange to load it.

   The function's argument types are described in its info table, so we
   can just jump to this bit of generic code to save away all the
   registers and return to the scheduler.

   This code arranges the stack like this:
	 
         |        ....         |
         |        args         |
	 +---------------------+
         |      f_closure      |
	 +---------------------+
         |        size         |
	 +---------------------+
         |   stg_gc_fun_info   |
	 +---------------------+

   The size is the number of words of arguments on the stack, and is cached
   in the frame in order to simplify stack walking: otherwise the size of
   this stack frame would have to be calculated by looking at f's info table.

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

__stg_gc_fun
{
    W_ size;
    W_ info;
    W_ type;

    info = %GET_FUN_INFO(R1);

    // cache the size
    type = TO_W_(StgFunInfoExtra_fun_type(info));
    if (type == ARG_GEN) {
	size = BITMAP_SIZE(StgFunInfoExtra_bitmap(info));
    } else { 
	if (type == ARG_GEN_BIG) {
#ifdef TABLES_NEXT_TO_CODE
            // bitmap field holds an offset
            size = StgLargeBitmap_size( StgFunInfoExtra_bitmap(info)
                                        + %GET_ENTRY(R1) /* ### */ );
#else
	    size = StgLargeBitmap_size( StgFunInfoExtra_bitmap(info) );
#endif
	} else {
	    size = BITMAP_SIZE(W_[stg_arg_bitmaps + WDS(type)]);
	}
    }
    
#ifdef NO_ARG_REGS
    // we don't have to save any registers away
    Sp_adj(-3);
    Sp(2) = R1;
    Sp(1) = size;
    Sp(0) = stg_gc_fun_info;
    GC_GENERIC
#else
    W_ type;
    type = TO_W_(StgFunInfoExtra_fun_type(info));
    // cache the size
    if (type == ARG_GEN || type == ARG_GEN_BIG) {
        // regs already saved by the heap check code
        Sp_adj(-3);
        Sp(2) = R1;
        Sp(1) = size;
        Sp(0) = stg_gc_fun_info;
        // DEBUG_ONLY(foreign "C" debugBelch("stg_fun_gc_gen(ARG_GEN)"););
        GC_GENERIC
    } else { 
	jump W_[stg_stack_save_entries + WDS(type)];
	    // jumps to stg_gc_noregs after saving stuff
    }
#endif /* !NO_ARG_REGS */
}

/* -----------------------------------------------------------------------------
   Generic Apply (return point)

   The dual to stg_fun_gc_gen (above): this fragment returns to the
   function, passing arguments in the stack and in registers
   appropriately.  The stack layout is given above.
   -------------------------------------------------------------------------- */

INFO_TABLE_RET( stg_gc_fun, 0/*framesize*/, 0/*bitmap*/, RET_FUN )
{
    R1 = Sp(2);
    Sp_adj(3);
#ifdef NO_ARG_REGS
    // Minor optimisation: there are no argument registers to load up,
    // so we can just jump straight to the function's entry point.
    jump %GET_ENTRY(R1);
#else
    W_ info;
    W_ type;
    
    info = %GET_FUN_INFO(R1);
    type = TO_W_(StgFunInfoExtra_fun_type(info));
    if (type == ARG_GEN || type == ARG_GEN_BIG) {
	jump StgFunInfoExtra_slow_apply(info);
    } else { 
	if (type == ARG_BCO) {
	    // cover this case just to be on the safe side
	    Sp_adj(-2);
	    Sp(1) = R1;
	    Sp(0) = stg_apply_interp_info;
	    jump stg_yield_to_interpreter;
	} else {
	    jump W_[stg_ap_stack_entries + WDS(type)];
	}
    }
#endif
}

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

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

   See StgMacros.h for a description of the RET_DYN stack frame.

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

// For simplicity, we assume that SIZEOF_DOUBLE == 2*SIZEOF_VOID_P
// on a 64-bit machine, we'll end up wasting a couple of words, but
// it's not a big deal.

#define RESTORE_EVERYTHING			\
    L1   = L_[Sp + WDS(19)];			\
    D2   = D_[Sp + WDS(17)];			\
    D1   = D_[Sp + WDS(15)];			\
    F4   = F_[Sp + WDS(14)];			\
    F3   = F_[Sp + WDS(13)];			\
    F2   = F_[Sp + WDS(12)];			\
    F1   = F_[Sp + WDS(11)];			\
    R8 = Sp(10);				\
    R7 = Sp(9);					\
    R6 = Sp(8);					\
    R5 = Sp(7);					\
    R4 = Sp(6);					\
    R3 = Sp(5);					\
    R2 = Sp(4);					\
    R1 = Sp(3);					\
    Sp_adj(21);

#define RET_OFFSET (-19)

#define SAVE_EVERYTHING				\
    Sp_adj(-21);				\
    L_[Sp + WDS(19)] = L1;			\
    D_[Sp + WDS(17)] = D2;			\
    D_[Sp + WDS(15)] = D1;			\
    F_[Sp + WDS(14)] = F4;			\
    F_[Sp + WDS(13)] = F3;			\
    F_[Sp + WDS(12)] = F2;			\
    F_[Sp + WDS(11)] = F1;			\
    Sp(10) = R8;				\
    Sp(9) = R7;					\
    Sp(8) = R6;					\
    Sp(7) = R5;					\
    Sp(6) = R4;					\
    Sp(5) = R3;					\
    Sp(4) = R2;					\
    Sp(3) = R1;					\
    Sp(2) = R10;    /* return address */	\
    Sp(1) = R9;     /* liveness mask  */	\
    Sp(0) = stg_gc_gen_info;

INFO_TABLE_RET( stg_gc_gen, 0/*framesize*/, 0/*bitmap*/, RET_DYN )
/* bitmap in the above info table is unused, the real one is on the stack. */
{
    RESTORE_EVERYTHING;
    jump Sp(RET_OFFSET); /* No %ENTRY_CODE( - this is an actual code ptr */
}

stg_gc_gen
{
    SAVE_EVERYTHING;
    GC_GENERIC
}	  

// A heap check at an unboxed tuple return point.  The return address
// is on the stack, and we can find it by using the offsets given
// to us in the liveness mask.
stg_gc_ut
{
    R10 = %ENTRY_CODE(Sp(RET_DYN_NONPTRS(R9) + RET_DYN_PTRS(R9)));
    SAVE_EVERYTHING;
    GC_GENERIC
}

/*
 * 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.
 */
stg_gc_gen_hp
{
    SAVE_EVERYTHING;
    HP_GENERIC
}	  

/* -----------------------------------------------------------------------------
   Yields
   -------------------------------------------------------------------------- */

stg_gen_yield
{
    SAVE_EVERYTHING;
    YIELD_GENERIC
}

stg_yield_noregs
{
    YIELD_GENERIC;
}

/* -----------------------------------------------------------------------------
   Yielding to the interpreter... top of stack says what to do next.
   -------------------------------------------------------------------------- */

stg_yield_to_interpreter
{
    YIELD_TO_INTERPRETER;
}

/* -----------------------------------------------------------------------------
   Blocks
   -------------------------------------------------------------------------- */

stg_gen_block
{
    SAVE_EVERYTHING;
    BLOCK_GENERIC;
}

stg_block_noregs
{
    BLOCK_GENERIC;
}

stg_block_1
{
    Sp_adj(-2);
    Sp(1) = R1;
    Sp(0) = stg_enter_info;
    BLOCK_GENERIC;
}

/* -----------------------------------------------------------------------------
 * takeMVar/putMVar-specific blocks
 *
 * Stack layout for a thread blocked in takeMVar:
 *      
 *       ret. addr
 *       ptr to MVar   (R1)
 *       stg_block_takemvar_info
 *
 * Stack layout for a thread blocked in putMVar:
 *      
 *       ret. addr
 *       ptr to Value  (R2)
 *       ptr to MVar   (R1)
 *       stg_block_putmvar_info
 *
 * See PrimOps.hc for a description of the workings of take/putMVar.
 * 
 * -------------------------------------------------------------------------- */

INFO_TABLE_RET( stg_block_takemvar, 1/*framesize*/, 0/*bitmap*/, RET_SMALL )
{
    R1 = Sp(1);
    Sp_adj(2);
    jump takeMVarzh_fast;
}

// code fragment executed just before we return to the scheduler
stg_block_takemvar_finally
{
#ifdef THREADED_RTS
    unlockClosure(R3, stg_EMPTY_MVAR_info);
#endif
    jump StgReturn;
}

stg_block_takemvar
{
    Sp_adj(-2);
    Sp(1) = R1;
    Sp(0) = stg_block_takemvar_info;
    R3 = R1;
    BLOCK_BUT_FIRST(stg_block_takemvar_finally);
}

INFO_TABLE_RET( stg_block_putmvar, 2/*framesize*/, 0/*bitmap*/, RET_SMALL )
{
    R2 = Sp(2);
    R1 = Sp(1);
    Sp_adj(3);
    jump putMVarzh_fast;
}

// code fragment executed just before we return to the scheduler
stg_block_putmvar_finally
{
#ifdef THREADED_RTS
    unlockClosure(R3, stg_FULL_MVAR_info);
#endif
    jump StgReturn;
}

stg_block_putmvar
{
    Sp_adj(-3);
    Sp(2) = R2;
    Sp(1) = R1;
    Sp(0) = stg_block_putmvar_info;
    R3 = R1;
    BLOCK_BUT_FIRST(stg_block_putmvar_finally);
}

// code fragment executed just before we return to the scheduler
stg_block_blackhole_finally
{
#if defined(THREADED_RTS)
    // The last thing we do is release sched_lock, which is
    // preventing other threads from accessing blackhole_queue and
    // picking up this thread before we are finished with it.
    foreign "C" RELEASE_LOCK(sched_mutex "ptr");
#endif
    jump StgReturn;
}

stg_block_blackhole
{
    Sp_adj(-2);
    Sp(1) = R1;
    Sp(0) = stg_enter_info;
    BLOCK_BUT_FIRST(stg_block_blackhole_finally);
}

INFO_TABLE_RET( stg_block_throwto, 2/*framesize*/, 0/*bitmap*/, RET_SMALL )
{
    R2 = Sp(2);
    R1 = Sp(1);
    Sp_adj(3);
    jump killThreadzh_fast;
}

stg_block_throwto_finally
{
#ifdef THREADED_RTS
    foreign "C" throwToReleaseTarget (R3 "ptr");
#endif
    jump StgReturn;
}

stg_block_throwto
{
    Sp_adj(-3);
    Sp(2) = R2;
    Sp(1) = R1;
    Sp(0) = stg_block_throwto_info;
    BLOCK_BUT_FIRST(stg_block_throwto_finally);
}

#ifdef mingw32_HOST_OS
INFO_TABLE_RET( stg_block_async, 0/*framesize*/, 0/*bitmap*/, RET_SMALL )
{
    W_ ares;
    W_ len, errC;

    ares = StgTSO_block_info(CurrentTSO);
    len = StgAsyncIOResult_len(ares);
    errC = StgAsyncIOResult_errCode(ares);
    StgTSO_block_info(CurrentTSO) = NULL;
    foreign "C" free(ares "ptr");
    R1 = len;
    Sp(0) = errC;
    jump %ENTRY_CODE(Sp(1));
}

stg_block_async
{
    Sp_adj(-1);
    Sp(0) = stg_block_async_info;
    BLOCK_GENERIC;
}

/* Used by threadDelay implementation; it would be desirable to get rid of
 * this free()'ing void return continuation.
 */
INFO_TABLE_RET( stg_block_async_void, 0/*framesize*/, 0/*bitmap*/, RET_SMALL )
{
    W_ ares;

    ares = StgTSO_block_info(CurrentTSO);
    StgTSO_block_info(CurrentTSO) = NULL;
    foreign "C" free(ares "ptr");
    Sp_adj(1);
    jump %ENTRY_CODE(Sp(0));
}

stg_block_async_void
{
    Sp_adj(-1);
    Sp(0) = stg_block_async_void_info;
    BLOCK_GENERIC;
}

#endif

/* -----------------------------------------------------------------------------
   STM-specific waiting
   -------------------------------------------------------------------------- */

stg_block_stmwait_finally
{
    foreign "C" stmWaitUnlock(MyCapability() "ptr", R3 "ptr");
    jump StgReturn;
}

stg_block_stmwait
{
    BLOCK_BUT_FIRST(stg_block_stmwait_finally);
}