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
|
/* -----------------------------------------------------------------------------
* $Id: Updates.hc,v 1.18 1999/07/06 16:40:28 sewardj Exp $
*
* (c) The GHC Team, 1998-1999
*
* Code to perform updates.
*
* ---------------------------------------------------------------------------*/
#include "Rts.h"
#include "RtsUtils.h"
#include "RtsFlags.h"
#include "HeapStackCheck.h"
#include "Storage.h"
#include "ProfRts.h"
/*
The update frame return address must be *polymorphic*, that means
we have to cope with both vectored and non-vectored returns. This
is done by putting the return vector right before the info table, and
having a standard direct return address after the info table (pointed
to by the return address itself, as usual).
Each entry in the vector table points to a specialised entry code fragment
that knows how to return after doing the update. It would be possible to
use a single generic piece of code that simply entered the return value
to return, but it's quicker this way. The direct return code of course
just does another direct return when it's finished.
Why is there necessarily an activation underneath us on the stack?
Because if we're returning, that means we've got a constructor in
our hands. If there were any arguments to be applied to it, that
would be a type error. We don't ever return a PAP to an update frame,
the update is handled manually by stg_update_PAP.
*/
/* on entry to the update code
(1) R1 points to the closure being returned
(2) R2 contains the tag (if we returned directly, non-vectored)
(3) Sp points to the update frame
*/
/* Why updatee is placed in a temporary variable here: this helps
gcc's aliasing by indicating that the location of the updatee
doesn't change across assignments. Saves one instruction in the
update code.
*/
#define UPD_FRAME_ENTRY_TEMPLATE(label,ret) \
STGFUN(label); \
STGFUN(label) \
{ \
StgClosure *updatee; \
FB_ \
/* tick - ToDo: check this is right */ \
TICK_UPD_EXISTING(); \
\
updatee = ((StgUpdateFrame *)Sp)->updatee; \
\
/* update the updatee with an indirection to the return value */\
UPD_IND(updatee,R1.p); \
\
/* reset Su to the next update frame */ \
Su = ((StgUpdateFrame *)Sp)->link; \
\
/* remove the update frame from the stack */ \
Sp += sizeofW(StgUpdateFrame); \
\
JMP_(ret); \
FE_ \
}
//UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_entry,ENTRY_CODE(Sp[0]));
STGFUN(Upd_frame_entry);
STGFUN(Upd_frame_entry)
{
StgClosure *updatee;
FB_
/* tick - ToDo: check this is right */
TICK_UPD_EXISTING();
updatee = ((StgUpdateFrame *)Sp)->updatee;
/* update the updatee with an indirection to the return value */
UPD_IND(updatee,R1.p);
/* reset Su to the next update frame */
Su = ((StgUpdateFrame *)Sp)->link;
/* remove the update frame from the stack */
Sp += sizeofW(StgUpdateFrame);
JMP_(ENTRY_CODE(Sp[0]));
FE_
}
UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_0_entry,RET_VEC(Sp[0],0));
UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_1_entry,RET_VEC(Sp[0],1));
UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_2_entry,RET_VEC(Sp[0],2));
UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_3_entry,RET_VEC(Sp[0],3));
UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_4_entry,RET_VEC(Sp[0],4));
UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_5_entry,RET_VEC(Sp[0],5));
UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_6_entry,RET_VEC(Sp[0],6));
UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_7_entry,RET_VEC(Sp[0],7));
/*
Make sure this table is big enough to handle the maximum vectored
return size!
*/
#ifdef PROFILING
#define UPD_FRAME_BITMAP 3
#else
#define UPD_FRAME_BITMAP 1
#endif
/* this bitmap indicates that the first word of an update frame is a
* non-pointer - this is the update frame link. (for profiling,
* there's a cost-centre-stack in there too).
*/
VEC_POLY_INFO_TABLE(Upd_frame,UPD_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, UPDATE_FRAME,, EF_);
/* -----------------------------------------------------------------------------
Entry Code for a PAP.
The idea is to copy the chunk of stack from the PAP object and then
re-enter the function closure that failed it's args check in the
first place.
In fact, we do a little optimisation too, by performing the updates
for any update frames sitting on top of the stack. (ToDo: is this
really an optimisation? --SDM)
-------------------------------------------------------------------------- */
INFO_TABLE(PAP_info,PAP_entry,/*special layout*/0,0,PAP,,EF_,0,0);
STGFUN(PAP_entry)
{
nat Words;
P_ p;
nat i;
StgPAP *pap;
FB_
pap = (StgPAP *) R1.p;
/*
* remove any update frames on the top of the stack, by just
* performing the update here.
*/
while ((W_)Su - (W_)Sp == 0) {
switch (get_itbl(Su)->type) {
case UPDATE_FRAME:
/* We're sitting on top of an update frame, so let's do the business */
UPD_IND(Su->updatee, pap);
#if defined(PROFILING)
/*
* Restore the Cost Centre too (if required); again see Sansom
* thesis p 183. Take the CC out of the update frame if a
* CAF/DICT.
*/
CCCS = Su->header.prof.ccs;
#endif /* PROFILING */
Su = Su->link;
Sp += sizeofW(StgUpdateFrame);
continue;
case SEQ_FRAME:
/* Just pop the seq frame and return to the activation record
* underneath us - R1 already contains the address of the PAP.
*/
Su = ((StgSeqFrame *)Su)->link;
Sp += sizeofW(StgSeqFrame);
JMP_(ENTRY_CODE(*Sp));
case CATCH_FRAME:
/* can't happen, see stg_update_PAP */
barf("PAP_entry: CATCH_FRAME");
default:
barf("PAP_entry: strange activation record");
}
}
Words = pap->n_args;
/*
* Check for stack overflow.
*/
STK_CHK_NP(Words,1,);
Sp -= Words;
TICK_ENT_PAP(pap);
/* Enter PAP cost centre -- lexical scoping only */
ENTER_CCS_PAP_CL(pap);
R1.cl = pap->fun;
p = (P_)(pap->payload);
/* Reload the stack */
for (i=0; i<Words; i++) Sp[i] = (W_) *p++;
/* Off we go! */
TICK_ENT_VIA_NODE();
JMP_(GET_ENTRY(R1.cl));
FE_
}
/* -----------------------------------------------------------------------------
stg_update_PAP: Update the current closure with a partial application.
This function is called whenever an argument satisfaction check fails.
-------------------------------------------------------------------------- */
EXTFUN(stg_update_PAP)
{
nat Words, PapSize;
#ifdef PROFILING
CostCentreStack *CCS_pap;
#endif
StgPAP* PapClosure;
StgClosure *Fun, *Updatee;
P_ p;
I_ i;
FB_
/* Save the pointer to the function closure that just failed the
* argument satisfaction check
*/
Fun = R1.cl;
#if defined(GRAN_COUNT)
#error Fixme.
++nPAPs;
#endif
/* Just copy the whole block of stack between the stack pointer
* and the update frame pointer.
*/
Words = (P_)Su - (P_)Sp;
ASSERT((int)Words >= 0);
#if defined(PROFILING)
/* pretend we just entered the function closure */
ENTER_CCS_FCL(Fun);
CCS_pap = CCCS;
#endif
if (Words == 0) {
/*
* No arguments, only Node. Skip building the PAP and
* just plan to update with an indirection.
*/
PapClosure = (StgPAP *)Fun;
} else {
/* Build the PAP */
PapSize = Words + sizeofW(StgPAP);
/*
* First we need to do a heap check, which involves saving
* everything on the stack. We only have one live pointer:
* Fun, the function closure that was passed to us. If the
* heap check fails, we push the function closure on the stack
* and instruct the scheduler to try entering it again when
* the garbage collector has run.
*
* It's done this way because there's a possibility that the
* garbage collector might have messed around with the stack,
* such as removing the update frame.
*/
if ((Hp += PapSize) > HpLim) {
Sp -= 1;
Sp[0] = (W_)Fun;
JMP_(stg_gc_entertop);
}
TICK_ALLOC_UPD_PAP(1/*fun*/ + Words, 0);
#ifdef PROFILING
CCS_ALLOC(CCS_pap, PapSize);
#endif
PapClosure = (StgPAP *)(Hp + 1 - PapSize); /* The new PapClosure */
SET_HDR(PapClosure,&PAP_info,CCS_pap);
PapClosure->n_args = Words;
PapClosure->fun = Fun;
/* Now fill in the closure fields */
p = Hp;
for (i = Words-1; i >= 0; i--) {
*p-- = (W_) Sp[i];
}
}
/*
* Finished constructing PAP closure; now update the updatee.
*/
/* ToDo: we'd like to just jump to the code for PAP_entry here,
* which deals with a stack of update frames in one go. What to
* do about the special ticky and profiling stuff here?
*/
switch (get_itbl(Su)->type) {
case SEQ_FRAME:
/* Set Sp to just above the SEQ frame (should be an activation rec.)*/
Sp = (P_)Su + sizeofW(StgSeqFrame);
/* restore Su */
Su = ((StgSeqFrame *)Su)->link;
/* return to the activation record, with the address of the PAP in R1 */
R1.p = (P_)PapClosure;
JMP_(ENTRY_CODE(*Sp));
case CATCH_FRAME:
/* Set Sp to just above the CATCH frame (should be an activation rec.)*/
Sp = (P_)Su + sizeofW(StgCatchFrame);
/* restore Su */
Su = ((StgCatchFrame *)Su)->link;
/* restart by entering the PAP */
R1.p = (P_)PapClosure;
JMP_(GET_ENTRY(R1.cl));
case UPDATE_FRAME:
/*
* Now we have a standard update frame, so we update the updatee with
* either the new PAP or Node.
*/
Updatee = Su->updatee;
#if defined(PROFILING)
if (Words != 0) {
UPD_IND(Updatee,PapClosure);
TICK_UPD_PAP_IN_NEW(Words+1);
} else {
/* Lexical scoping requires a *permanent* indirection, and we
* also have to set the cost centre for the indirection.
*/
UPD_PERM_IND(Updatee,PapClosure);
TICK_UPD_PAP_IN_PLACE();
Updatee->header.prof.ccs = CCS_pap;
}
#else
UPD_IND(Updatee,PapClosure);
if (Words != 0) {
TICK_UPD_PAP_IN_NEW(Words+1);
} else {
TICK_UPD_PAP_IN_PLACE();
}
#endif
#if defined(PROFILING)
CCCS = Su->header.prof.ccs;
ENTER_CCS_PAP(CCS_pap);
#endif /* PROFILING */
/* Restore Su */
Su = Su->link;
/*
* Squeeze out update frame from stack.
*/
for (i = Words-1; i >= 0; i--) {
Sp[i+(sizeofW(StgUpdateFrame))] = Sp[i];
}
Sp += sizeofW(StgUpdateFrame);
break;
default:
barf("stg_update_PAP: strange activation record");
}
/*
* All done! Restart by re-entering Node
* Don't count this entry for ticky-ticky profiling.
*/
JMP_(GET_ENTRY(R1.cl));
FE_
}
/* -----------------------------------------------------------------------------
Entry Code for an AP_UPD.
The idea is to copy the chunk of stack from the AP object and then
enter the function closure.
(This code is a simplified copy of the PAP code - with all the
update frame code stripped out.)
-------------------------------------------------------------------------- */
INFO_TABLE(AP_UPD_info,AP_UPD_entry,/*special layout*/0,0,AP_UPD,,EF_,0,0);
STGFUN(AP_UPD_entry)
{
nat Words;
P_ p;
nat i;
StgAP_UPD *ap;
FB_
ap = (StgAP_UPD *) R1.p;
Words = ap->n_args;
/*
* Check for stack overflow.
*/
STK_CHK(Words+sizeofW(StgUpdateFrame),AP_UPD_entry,R2.p,1,);
PUSH_UPD_FRAME(R1.p, 0);
Sp -= sizeofW(StgUpdateFrame) + Words;
TICK_ENT_AP_UPD(ap);
/* Enter PAP cost centre -- lexical scoping only */
ENTER_CCS_PAP_CL(ap); /* ToDo: ENTER_CC_AP_UPD_CL */
R1.cl = ap->fun;
p = (P_)(ap->payload);
/* Reload the stack */
for (i=0; i<Words; i++) Sp[i] = (W_) *p++;
/* Off we go! */
TICK_ENT_VIA_NODE();
JMP_(GET_ENTRY(R1.cl));
FE_
}
/*-----------------------------------------------------------------------------
Seq frames
We don't have a primitive seq# operator: it is just a 'case'
expression whose scrutinee has either a polymorphic or function type
(constructor types can be handled by normal 'case' expressions).
To handle a polymorphic/function typed seq, we push a SEQ_FRAME on
the stack. This is a polymorphic activation record that just pops
itself and returns when entered. The purpose of the SEQ_FRAME is to
act as a barrier in case the scrutinee is a partial application - in
this way it is just like an update frame, except that it doesn't
update anything.
-------------------------------------------------------------------------- */
#define SEQ_FRAME_ENTRY_TEMPLATE(label,ret) \
IFN_(label) \
{ \
FB_ \
Su = ((StgSeqFrame *)Sp)->link; \
Sp += sizeofW(StgSeqFrame); \
JMP_(ret); \
FE_ \
}
SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_entry, ENTRY_CODE(Sp[0]));
SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_0_entry,ENTRY_CODE(Sp[0]));
SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_1_entry,ENTRY_CODE(Sp[0]));
SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_2_entry,ENTRY_CODE(Sp[0]));
SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_3_entry,ENTRY_CODE(Sp[0]));
SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_4_entry,ENTRY_CODE(Sp[0]));
SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_5_entry,ENTRY_CODE(Sp[0]));
SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_6_entry,ENTRY_CODE(Sp[0]));
SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_7_entry,ENTRY_CODE(Sp[0]));
VEC_POLY_INFO_TABLE(seq_frame, UPD_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, SEQ_FRAME,, EF_);
/* -----------------------------------------------------------------------------
* The seq infotable
*
* This closure takes one argument, which it evaluates and returns the
* result with a direct return (never a vectored return!) in R1. It
* does this by pushing a SEQ_FRAME on the stack and
* entering its argument.
*
* It is used in deleteThread when reverting blackholes.
* -------------------------------------------------------------------------- */
INFO_TABLE(seq_info,seq_entry,1,0,FUN,,EF_,0,0);
STGFUN(seq_entry)
{
FB_
STK_CHK_GEN(sizeofW(StgSeqFrame), NO_PTRS, seq_entry, );
Sp -= sizeofW(StgSeqFrame);
PUSH_SEQ_FRAME(Sp);
R1.cl = R1.cl->payload[0];
JMP_(ENTRY_CODE(*R1.p));
FE_
}
/* -----------------------------------------------------------------------------
Exception Primitives
-------------------------------------------------------------------------- */
FN_(catchzh_fast);
FN_(raisezh_fast);
#define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
FN_(label); \
FN_(label) \
{ \
FB_ \
Su = ((StgCatchFrame *)Sp)->link; \
Sp += sizeofW(StgCatchFrame); \
JMP_(ret); \
FE_ \
}
CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_entry,ENTRY_CODE(Sp[0]));
CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_0_entry,RET_VEC(Sp[0],0));
CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_1_entry,RET_VEC(Sp[0],1));
CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_2_entry,RET_VEC(Sp[0],2));
CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_3_entry,RET_VEC(Sp[0],3));
CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_4_entry,RET_VEC(Sp[0],4));
CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_5_entry,RET_VEC(Sp[0],5));
CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_6_entry,RET_VEC(Sp[0],6));
CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_7_entry,RET_VEC(Sp[0],7));
#ifdef PROFILING
#define CATCH_FRAME_BITMAP 3
#else
#define CATCH_FRAME_BITMAP 1
#endif
/* Catch frames are very similar to update frames, but when entering
* one we just pop the frame off the stack and perform the correct
* kind of return to the activation record underneath us on the stack.
*/
VEC_POLY_INFO_TABLE(catch_frame, CATCH_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, CATCH_FRAME,, EF_);
/* -----------------------------------------------------------------------------
* The catch infotable
*
* This should be exactly the same as would be generated by this STG code
*
* catch = {x,h} \n {} -> catch#{x,h}
*
* It is used in deleteThread when reverting blackholes.
* -------------------------------------------------------------------------- */
INFO_TABLE(catch_info,catch_entry,2,0,FUN,,EF_,0,0);
STGFUN(catch_entry)
{
FB_
R2.cl = payloadCPtr(R1.cl,1); /* h */
R1.cl = payloadCPtr(R1.cl,0); /* x */
JMP_(catchzh_fast);
FE_
}
FN_(catchzh_fast)
{
StgCatchFrame *fp;
FB_
/* args: R1 = m, R2 = k */
STK_CHK_GEN(sizeofW(StgCatchFrame), R1_PTR | R2_PTR, catchzh_fast, );
Sp -= sizeofW(StgCatchFrame);
fp = (StgCatchFrame *)Sp;
SET_HDR(fp,(StgInfoTable *)&catch_frame_info,CCCS);
fp -> handler = R2.cl;
fp -> link = Su;
Su = (StgUpdateFrame *)fp;
TICK_CATCHF_PUSHED();
TICK_ENT_VIA_NODE();
JMP_(ENTRY_CODE(*R1.p));
FE_
}
/* -----------------------------------------------------------------------------
* The raise infotable
*
* This should be exactly the same as would be generated by this STG code
*
* raise = {err} \n {} -> raise#{err}
*
* It is used in raisezh_fast to update thunks on the update list
* -------------------------------------------------------------------------- */
INFO_TABLE(raise_info,raise_entry,1,0,FUN,,EF_,0,0);
STGFUN(raise_entry)
{
FB_
R1.cl = R1.cl->payload[0];
JMP_(raisezh_fast);
FE_
}
FN_(raisezh_fast)
{
StgClosure *handler;
StgUpdateFrame *p;
StgClosure *raise_closure;
FB_
/* args : R1 = error */
p = Su;
/* This closure represents the expression 'raise# E' where E
* is the exception raise. It is used to overwrite all the
* thunks which are currently under evaluataion.
*/
raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
sizeofW(StgClosure)+1);
raise_closure->header.info = &raise_info;
raise_closure->payload[0] = R1.cl;
while (1) {
switch (get_itbl(p)->type) {
case UPDATE_FRAME:
UPD_IND(p->updatee,raise_closure);
p = p->link;
continue;
case SEQ_FRAME:
p = ((StgSeqFrame *)p)->link;
continue;
case CATCH_FRAME:
/* found it! */
break;
case STOP_FRAME:
barf("raisezh_fast: STOP_FRAME");
default:
barf("raisezh_fast: weird activation record");
}
break;
}
/* Ok, p points to the enclosing CATCH_FRAME. Pop everything down to
* and including this frame, update Su, push R1, and enter the handler.
*/
Su = ((StgCatchFrame *)p)->link;
handler = ((StgCatchFrame *)p)->handler;
Sp = (P_)p + sizeofW(StgCatchFrame) - 1;
*Sp = R1.w;
TICK_ENT_VIA_NODE();
R1.cl = handler;
JMP_(ENTRY_CODE(handler->header.info));
FE_
}
|