1 /* -----------------------------------------------------------------------------
2 * $Id: Updates.hc,v 1.17 1999/05/13 17:31:14 simonm Exp $
4 * (c) The GHC Team, 1998-1999
6 * Code to perform updates.
8 * ---------------------------------------------------------------------------*/
13 #include "HeapStackCheck.h"
18 The update frame return address must be *polymorphic*, that means
19 we have to cope with both vectored and non-vectored returns. This
20 is done by putting the return vector right before the info table, and
21 having a standard direct return address after the info table (pointed
22 to by the return address itself, as usual).
24 Each entry in the vector table points to a specialised entry code fragment
25 that knows how to return after doing the update. It would be possible to
26 use a single generic piece of code that simply entered the return value
27 to return, but it's quicker this way. The direct return code of course
28 just does another direct return when it's finished.
30 Why is there necessarily an activation underneath us on the stack?
31 Because if we're returning, that means we've got a constructor in
32 our hands. If there were any arguments to be applied to it, that
33 would be a type error. We don't ever return a PAP to an update frame,
34 the update is handled manually by stg_update_PAP.
37 /* on entry to the update code
38 (1) R1 points to the closure being returned
39 (2) R2 contains the tag (if we returned directly, non-vectored)
40 (3) Sp points to the update frame
43 /* Why updatee is placed in a temporary variable here: this helps
44 gcc's aliasing by indicating that the location of the updatee
45 doesn't change across assignments. Saves one instruction in the
49 #define UPD_FRAME_ENTRY_TEMPLATE(label,ret) \
53 StgClosure *updatee; \
55 /* tick - ToDo: check this is right */ \
56 TICK_UPD_EXISTING(); \
58 updatee = ((StgUpdateFrame *)Sp)->updatee; \
60 /* update the updatee with an indirection to the return value */\
61 UPD_IND(updatee,R1.p); \
63 /* reset Su to the next update frame */ \
64 Su = ((StgUpdateFrame *)Sp)->link; \
66 /* remove the update frame from the stack */ \
67 Sp += sizeofW(StgUpdateFrame); \
73 UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_entry,ENTRY_CODE(Sp[0]));
74 UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_0_entry,RET_VEC(Sp[0],0));
75 UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_1_entry,RET_VEC(Sp[0],1));
76 UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_2_entry,RET_VEC(Sp[0],2));
77 UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_3_entry,RET_VEC(Sp[0],3));
78 UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_4_entry,RET_VEC(Sp[0],4));
79 UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_5_entry,RET_VEC(Sp[0],5));
80 UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_6_entry,RET_VEC(Sp[0],6));
81 UPD_FRAME_ENTRY_TEMPLATE(Upd_frame_7_entry,RET_VEC(Sp[0],7));
84 Make sure this table is big enough to handle the maximum vectored
89 #define UPD_FRAME_BITMAP 3
91 #define UPD_FRAME_BITMAP 1
94 /* this bitmap indicates that the first word of an update frame is a
95 * non-pointer - this is the update frame link. (for profiling,
96 * there's a cost-centre-stack in there too).
99 VEC_POLY_INFO_TABLE(Upd_frame,UPD_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, UPDATE_FRAME,, EF_);
101 /* -----------------------------------------------------------------------------
102 Entry Code for a PAP.
104 The idea is to copy the chunk of stack from the PAP object and then
105 re-enter the function closure that failed it's args check in the
108 In fact, we do a little optimisation too, by performing the updates
109 for any update frames sitting on top of the stack. (ToDo: is this
110 really an optimisation? --SDM)
111 -------------------------------------------------------------------------- */
113 INFO_TABLE(PAP_info,PAP_entry,/*special layout*/0,0,PAP,,EF_,0,0);
123 pap = (StgPAP *) R1.p;
126 * remove any update frames on the top of the stack, by just
127 * performing the update here.
129 while ((W_)Su - (W_)Sp == 0) {
131 switch (get_itbl(Su)->type) {
134 /* We're sitting on top of an update frame, so let's do the business */
135 UPD_IND(Su->updatee, pap);
137 #if defined(PROFILING)
139 * Restore the Cost Centre too (if required); again see Sansom
140 * thesis p 183. Take the CC out of the update frame if a
144 CCCS = Su->header.prof.ccs;
145 #endif /* PROFILING */
148 Sp += sizeofW(StgUpdateFrame);
152 /* Just pop the seq frame and return to the activation record
153 * underneath us - R1 already contains the address of the PAP.
155 Su = ((StgSeqFrame *)Su)->link;
156 Sp += sizeofW(StgSeqFrame);
157 JMP_(ENTRY_CODE(*Sp));
160 /* can't happen, see stg_update_PAP */
161 barf("PAP_entry: CATCH_FRAME");
164 barf("PAP_entry: strange activation record");
172 * Check for stack overflow.
174 STK_CHK_NP(Words,1,);
179 /* Enter PAP cost centre -- lexical scoping only */
180 ENTER_CCS_PAP_CL(pap);
183 p = (P_)(pap->payload);
185 /* Reload the stack */
186 for (i=0; i<Words; i++) Sp[i] = (W_) *p++;
190 JMP_(GET_ENTRY(R1.cl));
194 /* -----------------------------------------------------------------------------
195 stg_update_PAP: Update the current closure with a partial application.
197 This function is called whenever an argument satisfaction check fails.
198 -------------------------------------------------------------------------- */
200 EXTFUN(stg_update_PAP)
204 CostCentreStack *CCS_pap;
207 StgClosure *Fun, *Updatee;
213 /* Save the pointer to the function closure that just failed the
214 * argument satisfaction check
218 #if defined(GRAN_COUNT)
223 /* Just copy the whole block of stack between the stack pointer
224 * and the update frame pointer.
226 Words = (P_)Su - (P_)Sp;
227 ASSERT((int)Words >= 0);
229 #if defined(PROFILING)
230 /* pretend we just entered the function closure */
238 * No arguments, only Node. Skip building the PAP and
239 * just plan to update with an indirection.
242 PapClosure = (StgPAP *)Fun;
247 PapSize = Words + sizeofW(StgPAP);
250 * First we need to do a heap check, which involves saving
251 * everything on the stack. We only have one live pointer:
252 * Fun, the function closure that was passed to us. If the
253 * heap check fails, we push the function closure on the stack
254 * and instruct the scheduler to try entering it again when
255 * the garbage collector has run.
257 * It's done this way because there's a possibility that the
258 * garbage collector might have messed around with the stack,
259 * such as removing the update frame.
261 if ((Hp += PapSize) > HpLim) {
264 JMP_(stg_gc_entertop);
267 TICK_ALLOC_UPD_PAP(1/*fun*/ + Words, 0);
269 CCS_ALLOC(CCS_pap, PapSize);
272 PapClosure = (StgPAP *)(Hp + 1 - PapSize); /* The new PapClosure */
274 SET_HDR(PapClosure,&PAP_info,CCS_pap);
275 PapClosure->n_args = Words;
276 PapClosure->fun = Fun;
278 /* Now fill in the closure fields */
281 for (i = Words-1; i >= 0; i--) {
287 * Finished constructing PAP closure; now update the updatee.
290 /* ToDo: we'd like to just jump to the code for PAP_entry here,
291 * which deals with a stack of update frames in one go. What to
292 * do about the special ticky and profiling stuff here?
295 switch (get_itbl(Su)->type) {
298 /* Set Sp to just above the SEQ frame (should be an activation rec.)*/
299 Sp = (P_)Su + sizeofW(StgSeqFrame);
302 Su = ((StgSeqFrame *)Su)->link;
304 /* return to the activation record, with the address of the PAP in R1 */
305 R1.p = (P_)PapClosure;
306 JMP_(ENTRY_CODE(*Sp));
309 /* Set Sp to just above the CATCH frame (should be an activation rec.)*/
310 Sp = (P_)Su + sizeofW(StgCatchFrame);
313 Su = ((StgCatchFrame *)Su)->link;
315 /* restart by entering the PAP */
316 R1.p = (P_)PapClosure;
317 JMP_(GET_ENTRY(R1.cl));
321 * Now we have a standard update frame, so we update the updatee with
322 * either the new PAP or Node.
325 Updatee = Su->updatee;
327 #if defined(PROFILING)
329 UPD_IND(Updatee,PapClosure);
330 TICK_UPD_PAP_IN_NEW(Words+1);
332 /* Lexical scoping requires a *permanent* indirection, and we
333 * also have to set the cost centre for the indirection.
335 UPD_PERM_IND(Updatee,PapClosure);
336 TICK_UPD_PAP_IN_PLACE();
337 Updatee->header.prof.ccs = CCS_pap;
340 UPD_IND(Updatee,PapClosure);
342 TICK_UPD_PAP_IN_NEW(Words+1);
344 TICK_UPD_PAP_IN_PLACE();
348 #if defined(PROFILING)
349 CCCS = Su->header.prof.ccs;
350 ENTER_CCS_PAP(CCS_pap);
351 #endif /* PROFILING */
357 * Squeeze out update frame from stack.
359 for (i = Words-1; i >= 0; i--) {
360 Sp[i+(sizeofW(StgUpdateFrame))] = Sp[i];
362 Sp += sizeofW(StgUpdateFrame);
366 barf("stg_update_PAP: strange activation record");
370 * All done! Restart by re-entering Node
371 * Don't count this entry for ticky-ticky profiling.
373 JMP_(GET_ENTRY(R1.cl));
378 /* -----------------------------------------------------------------------------
379 Entry Code for an AP_UPD.
381 The idea is to copy the chunk of stack from the AP object and then
382 enter the function closure.
384 (This code is a simplified copy of the PAP code - with all the
385 update frame code stripped out.)
386 -------------------------------------------------------------------------- */
389 INFO_TABLE(AP_UPD_info,AP_UPD_entry,/*special layout*/0,0,AP_UPD,,EF_,0,0);
399 ap = (StgAP_UPD *) R1.p;
404 * Check for stack overflow.
406 STK_CHK(Words+sizeofW(StgUpdateFrame),AP_UPD_entry,R2.p,1,);
408 PUSH_UPD_FRAME(R1.p, 0);
409 Sp -= sizeofW(StgUpdateFrame) + Words;
413 /* Enter PAP cost centre -- lexical scoping only */
414 ENTER_CCS_PAP_CL(ap); /* ToDo: ENTER_CC_AP_UPD_CL */
417 p = (P_)(ap->payload);
419 /* Reload the stack */
420 for (i=0; i<Words; i++) Sp[i] = (W_) *p++;
424 JMP_(GET_ENTRY(R1.cl));
429 /*-----------------------------------------------------------------------------
432 We don't have a primitive seq# operator: it is just a 'case'
433 expression whose scrutinee has either a polymorphic or function type
434 (constructor types can be handled by normal 'case' expressions).
436 To handle a polymorphic/function typed seq, we push a SEQ_FRAME on
437 the stack. This is a polymorphic activation record that just pops
438 itself and returns when entered. The purpose of the SEQ_FRAME is to
439 act as a barrier in case the scrutinee is a partial application - in
440 this way it is just like an update frame, except that it doesn't
442 -------------------------------------------------------------------------- */
444 #define SEQ_FRAME_ENTRY_TEMPLATE(label,ret) \
448 Su = ((StgSeqFrame *)Sp)->link; \
449 Sp += sizeofW(StgSeqFrame); \
454 SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_entry, ENTRY_CODE(Sp[0]));
455 SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_0_entry,ENTRY_CODE(Sp[0]));
456 SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_1_entry,ENTRY_CODE(Sp[0]));
457 SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_2_entry,ENTRY_CODE(Sp[0]));
458 SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_3_entry,ENTRY_CODE(Sp[0]));
459 SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_4_entry,ENTRY_CODE(Sp[0]));
460 SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_5_entry,ENTRY_CODE(Sp[0]));
461 SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_6_entry,ENTRY_CODE(Sp[0]));
462 SEQ_FRAME_ENTRY_TEMPLATE(seq_frame_7_entry,ENTRY_CODE(Sp[0]));
464 VEC_POLY_INFO_TABLE(seq_frame, UPD_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, SEQ_FRAME,, EF_);
466 /* -----------------------------------------------------------------------------
469 * This closure takes one argument, which it evaluates and returns the
470 * result with a direct return (never a vectored return!) in R1. It
471 * does this by pushing a SEQ_FRAME on the stack and
472 * entering its argument.
474 * It is used in deleteThread when reverting blackholes.
475 * -------------------------------------------------------------------------- */
477 INFO_TABLE(seq_info,seq_entry,1,0,FUN,,EF_,0,0);
481 STK_CHK_GEN(sizeofW(StgSeqFrame), NO_PTRS, seq_entry, );
482 Sp -= sizeofW(StgSeqFrame);
484 R1.cl = R1.cl->payload[0];
485 JMP_(ENTRY_CODE(*R1.p));
490 /* -----------------------------------------------------------------------------
492 -------------------------------------------------------------------------- */
497 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
502 Su = ((StgCatchFrame *)Sp)->link; \
503 Sp += sizeofW(StgCatchFrame); \
508 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_entry,ENTRY_CODE(Sp[0]));
509 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_0_entry,RET_VEC(Sp[0],0));
510 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_1_entry,RET_VEC(Sp[0],1));
511 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_2_entry,RET_VEC(Sp[0],2));
512 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_3_entry,RET_VEC(Sp[0],3));
513 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_4_entry,RET_VEC(Sp[0],4));
514 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_5_entry,RET_VEC(Sp[0],5));
515 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_6_entry,RET_VEC(Sp[0],6));
516 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_7_entry,RET_VEC(Sp[0],7));
519 #define CATCH_FRAME_BITMAP 3
521 #define CATCH_FRAME_BITMAP 1
524 /* Catch frames are very similar to update frames, but when entering
525 * one we just pop the frame off the stack and perform the correct
526 * kind of return to the activation record underneath us on the stack.
529 VEC_POLY_INFO_TABLE(catch_frame, CATCH_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, CATCH_FRAME,, EF_);
531 /* -----------------------------------------------------------------------------
532 * The catch infotable
534 * This should be exactly the same as would be generated by this STG code
536 * catch = {x,h} \n {} -> catch#{x,h}
538 * It is used in deleteThread when reverting blackholes.
539 * -------------------------------------------------------------------------- */
541 INFO_TABLE(catch_info,catch_entry,2,0,FUN,,EF_,0,0);
545 R2.cl = payloadCPtr(R1.cl,1); /* h */
546 R1.cl = payloadCPtr(R1.cl,0); /* x */
556 /* args: R1 = m, R2 = k */
557 STK_CHK_GEN(sizeofW(StgCatchFrame), R1_PTR | R2_PTR, catchzh_fast, );
558 Sp -= sizeofW(StgCatchFrame);
559 fp = (StgCatchFrame *)Sp;
560 SET_HDR(fp,(StgInfoTable *)&catch_frame_info,CCCS);
561 fp -> handler = R2.cl;
563 Su = (StgUpdateFrame *)fp;
564 TICK_CATCHF_PUSHED();
566 JMP_(ENTRY_CODE(*R1.p));
571 /* -----------------------------------------------------------------------------
572 * The raise infotable
574 * This should be exactly the same as would be generated by this STG code
576 * raise = {err} \n {} -> raise#{err}
578 * It is used in raisezh_fast to update thunks on the update list
579 * -------------------------------------------------------------------------- */
581 INFO_TABLE(raise_info,raise_entry,1,0,FUN,,EF_,0,0);
585 R1.cl = R1.cl->payload[0];
594 StgClosure *raise_closure;
596 /* args : R1 = error */
600 /* This closure represents the expression 'raise# E' where E
601 * is the exception raise. It is used to overwrite all the
602 * thunks which are currently under evaluataion.
604 raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
605 sizeofW(StgClosure)+1);
606 raise_closure->header.info = &raise_info;
607 raise_closure->payload[0] = R1.cl;
611 switch (get_itbl(p)->type) {
614 UPD_IND(p->updatee,raise_closure);
619 p = ((StgSeqFrame *)p)->link;
627 barf("raisezh_fast: STOP_FRAME");
630 barf("raisezh_fast: weird activation record");
637 /* Ok, p points to the enclosing CATCH_FRAME. Pop everything down to
638 * and including this frame, update Su, push R1, and enter the handler.
640 Su = ((StgCatchFrame *)p)->link;
641 handler = ((StgCatchFrame *)p)->handler;
643 Sp = (P_)p + sizeofW(StgCatchFrame) - 1;
648 JMP_(ENTRY_CODE(handler->header.info));