1 /* -----------------------------------------------------------------------------
2 * $Id: Updates.hc,v 1.35 2001/11/08 12:46:31 simonmar Exp $
4 * (c) The GHC Team, 1998-1999
6 * Code to perform updates.
8 * ---------------------------------------------------------------------------*/
15 #if defined(GRAN) || defined(PAR)
20 The update frame return address must be *polymorphic*, that means
21 we have to cope with both vectored and non-vectored returns. This
22 is done by putting the return vector right before the info table, and
23 having a standard direct return address after the info table (pointed
24 to by the return address itself, as usual).
26 Each entry in the vector table points to a specialised entry code fragment
27 that knows how to return after doing the update. It would be possible to
28 use a single generic piece of code that simply entered the return value
29 to return, but it's quicker this way. The direct return code of course
30 just does another direct return when it's finished.
32 Why is there necessarily an activation underneath us on the stack?
33 Because if we're returning, that means we've got a constructor in
34 our hands. If there were any arguments to be applied to it, that
35 would be a type error. We don't ever return a PAP to an update frame,
36 the update is handled manually by stg_update_PAP.
39 /* on entry to the update code
40 (1) R1 points to the closure being returned
41 (2) R2 contains the tag (if we returned directly, non-vectored)
42 (3) Sp points to the update frame
45 /* Why updatee is placed in a temporary variable here: this helps
46 gcc's aliasing by indicating that the location of the updatee
47 doesn't change across assignments. Saves one instruction in the
52 #define UPD_FRAME_ENTRY_TEMPLATE(label,ret) \
58 Su = (StgUpdateFrame *)((StgUpdateFrame *)Sp)->updatee; \
60 /* Tick - it must be a con, all the paps are handled \
61 * in stg_upd_PAP and PAP_entry below \
63 TICK_UPD_CON_IN_NEW(sizeW_fromITBL(get_itbl(Su))); \
67 /* reset Su to the next update frame */ \
68 Su = ((StgUpdateFrame *)Sp)->link; \
70 /* remove the update frame from the stack */ \
71 Sp += sizeofW(StgUpdateFrame); \
78 #define UPD_FRAME_ENTRY_TEMPLATE(label,ret) \
82 StgClosure *updatee; \
85 updatee = ((StgUpdateFrame *)Sp)->updatee; \
87 /* Tick - it must be a con, all the paps are handled \
88 * in stg_upd_PAP and PAP_entry below \
90 TICK_UPD_CON_IN_NEW(sizeW_fromITBL(get_itbl(updatee))); \
92 UPD_IND(updatee, R1.cl); \
94 /* reset Su to the next update frame */ \
95 Su = ((StgUpdateFrame *)Sp)->link; \
97 /* remove the update frame from the stack */ \
98 Sp += sizeofW(StgUpdateFrame); \
105 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_entry,ENTRY_CODE(Sp[0]));
106 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_0_entry,RET_VEC(Sp[0],0));
107 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_1_entry,RET_VEC(Sp[0],1));
108 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_2_entry,RET_VEC(Sp[0],2));
109 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_3_entry,RET_VEC(Sp[0],3));
110 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_4_entry,RET_VEC(Sp[0],4));
111 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_5_entry,RET_VEC(Sp[0],5));
112 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_6_entry,RET_VEC(Sp[0],6));
113 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_7_entry,RET_VEC(Sp[0],7));
116 Make sure this table is big enough to handle the maximum vectored
121 #define UPD_FRAME_BITMAP 3
123 #define UPD_FRAME_BITMAP 1
126 /* this bitmap indicates that the first word of an update frame is a
127 * non-pointer - this is the update frame link. (for profiling,
128 * there's a cost-centre-stack in there too).
131 VEC_POLY_INFO_TABLE(stg_upd_frame,UPD_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, UPDATE_FRAME,, EF_);
133 /* -----------------------------------------------------------------------------
134 Entry Code for a PAP.
136 The idea is to copy the chunk of stack from the PAP object and then
137 re-enter the function closure that failed it's args check in the
140 In fact, we do a little optimisation too, by performing the updates
141 for any update frames sitting on top of the stack. (ToDo: is this
142 really an optimisation? --SDM)
143 -------------------------------------------------------------------------- */
145 INFO_TABLE(stg_PAP_info,stg_PAP_entry,/*special layout*/0,0,PAP,,EF_,"PAP","PAP");
146 STGFUN(stg_PAP_entry)
155 pap = (StgPAP *) R1.p;
158 * remove any update frames on the top of the stack, by just
159 * performing the update here.
161 while ((W_)Su - (W_)Sp == 0) {
163 switch (get_itbl(Su)->type) {
166 /* We're sitting on top of an update frame, so let's do the business */
167 UPD_IND(Su->updatee, pap);
169 #if defined(PROFILING)
171 * Restore the Cost Centre too (if required); again see Sansom
172 * thesis p 183. Take the CC out of the update frame if a
176 CCCS = Su->header.prof.ccs;
177 #endif /* PROFILING */
180 Sp += sizeofW(StgUpdateFrame);
184 /* Just pop the seq frame and return to the activation record
185 * underneath us - R1 already contains the address of the PAP.
187 Su = ((StgSeqFrame *)Su)->link;
188 Sp += sizeofW(StgSeqFrame);
189 JMP_(ENTRY_CODE(*Sp));
192 /* can't happen, see stg_update_PAP */
193 barf("PAP_entry: CATCH_FRAME");
196 barf("PAP_entry: strange activation record");
204 * Check for stack overflow.
206 STK_CHK_NP(Words,1,);
211 /* Enter PAP cost centre -- lexical scoping only */
212 ENTER_CCS_PAP_CL(pap);
215 p = (P_)(pap->payload);
217 /* Reload the stack */
218 for (i=0; i<Words; i++) Sp[i] = (W_) *p++;
222 JMP_(GET_ENTRY(R1.cl));
226 /* -----------------------------------------------------------------------------
227 stg_update_PAP: Update the current closure with a partial application.
229 This function is called whenever an argument satisfaction check fails.
230 -------------------------------------------------------------------------- */
232 EXTFUN(__stg_update_PAP)
236 CostCentreStack *CCS_pap;
239 StgClosure *Fun, *Updatee;
245 /* Save the pointer to the function closure that just failed the
246 * argument satisfaction check
250 /* Just copy the whole block of stack between the stack pointer
251 * and the update frame pointer.
253 Words = (P_)Su - (P_)Sp;
254 ASSERT((int)Words >= 0);
256 #if defined(PROFILING)
257 /* pretend we just entered the function closure */
265 * No arguments, only Node. Skip building the PAP and
266 * just plan to update with an indirection.
269 PapClosure = (StgPAP *)Fun;
274 PapSize = Words + sizeofW(StgPAP);
277 * First we need to do a heap check, which involves saving
278 * everything on the stack. We only have one live pointer:
279 * Fun, the function closure that was passed to us. If the
280 * heap check fails, we push the function closure on the stack
281 * and instruct the scheduler to try entering it again when
282 * the garbage collector has run.
284 * It's done this way because there's a possibility that the
285 * garbage collector might have messed around with the stack,
286 * such as removing the update frame.
288 if ((Hp += PapSize) > HpLim) {
291 JMP_(stg_gc_entertop);
294 TICK_ALLOC_UPD_PAP(1/*fun*/ + Words, 0);
296 CCS_ALLOC(CCS_pap, PapSize);
299 PapClosure = (StgPAP *)(Hp + 1 - PapSize); /* The new PapClosure */
301 SET_HDR(PapClosure,&stg_PAP_info,CCS_pap);
302 PapClosure->n_args = Words;
303 PapClosure->fun = Fun;
305 /* Now fill in the closure fields */
308 for (i = Words; --i >= 0; ) {
314 * Finished constructing PAP closure; now update the updatee.
317 /* ToDo: we'd like to just jump to the code for PAP_entry here,
318 * which deals with a stack of update frames in one go. What to
319 * do about the special ticky and profiling stuff here?
322 switch (get_itbl(Su)->type) {
325 /* Set Sp to just above the SEQ frame (should be an activation rec.)*/
326 Sp = (P_)Su + sizeofW(StgSeqFrame);
329 Su = ((StgSeqFrame *)Su)->link;
331 /* return to the activation record, with the address of the PAP in R1 */
332 R1.p = (P_)PapClosure;
333 JMP_(ENTRY_CODE(*Sp));
336 /* Set Sp to just above the CATCH frame (should be an activation rec.)*/
337 Sp = (P_)Su + sizeofW(StgCatchFrame);
340 Su = ((StgCatchFrame *)Su)->link;
342 /* restart by entering the PAP */
343 R1.p = (P_)PapClosure;
344 JMP_(GET_ENTRY(R1.cl));
348 * Now we have a standard update frame, so we update the updatee with
349 * either the new PAP or Node.
352 Updatee = Su->updatee;
354 #if defined(PROFILING)
356 UPD_IND(Updatee,PapClosure);
357 TICK_UPD_PAP_IN_NEW(Words+1);
359 /* Lexical scoping requires a *permanent* indirection, and we
360 * also have to set the cost centre for the indirection.
362 UPD_PERM_IND(Updatee,PapClosure);
363 TICK_UPD_PAP_IN_PLACE();
364 Updatee->header.prof.ccs = CCS_pap;
367 UPD_IND(Updatee,PapClosure);
369 TICK_UPD_PAP_IN_NEW(Words+1);
371 TICK_UPD_PAP_IN_PLACE();
375 #if defined(PROFILING)
376 CCCS = Su->header.prof.ccs;
377 ENTER_CCS_PAP(CCS_pap);
378 #endif /* PROFILING */
384 * Squeeze out update frame from stack.
386 for (i = Words; --i >= 0; ) {
387 Sp[i+(sizeofW(StgUpdateFrame))] = Sp[i];
389 Sp += sizeofW(StgUpdateFrame);
393 barf("stg_update_PAP: strange activation record");
397 * All done! Restart by re-entering Node
398 * Don't count this entry for ticky-ticky profiling.
400 JMP_(GET_ENTRY(R1.cl));
405 /* -----------------------------------------------------------------------------
406 Entry Code for an AP_UPD.
408 The idea is to copy the chunk of stack from the AP object and then
409 enter the function closure.
411 (This code is a simplified copy of the PAP code - with all the
412 update frame code stripped out.)
413 -------------------------------------------------------------------------- */
416 INFO_TABLE(stg_AP_UPD_info,stg_AP_UPD_entry,/*special layout*/0,0,AP_UPD,,EF_,"AP_UPD","AP_UPD");
417 STGFUN(stg_AP_UPD_entry)
426 ap = (StgAP_UPD *) R1.p;
431 * Check for stack overflow.
433 STK_CHK_GEN(Words+sizeofW(StgUpdateFrame), R1_PTR, stg_AP_UPD_entry, );
435 PUSH_UPD_FRAME(R1.p, 0);
436 Sp -= sizeofW(StgUpdateFrame) + Words;
440 /* Enter PAP cost centre -- lexical scoping only */
441 ENTER_CCS_PAP_CL(ap); /* ToDo: ENTER_CC_AP_UPD_CL */
444 p = (P_)(ap->payload);
446 /* Reload the stack */
447 for (i=0; i<Words; i++) Sp[i] = (W_) *p++;
451 JMP_(GET_ENTRY(R1.cl));
456 /*-----------------------------------------------------------------------------
459 We don't have a primitive seq# operator: it is just a 'case'
460 expression whose scrutinee has either a polymorphic or function type
461 (constructor types can be handled by normal 'case' expressions).
463 To handle a polymorphic/function typed seq, we push a SEQ_FRAME on
464 the stack. This is a polymorphic activation record that just pops
465 itself and returns when entered. The purpose of the SEQ_FRAME is to
466 act as a barrier in case the scrutinee is a partial application - in
467 this way it is just like an update frame, except that it doesn't
469 -------------------------------------------------------------------------- */
471 #define SEQ_FRAME_ENTRY_TEMPLATE(label,ret) \
475 Su = ((StgSeqFrame *)Sp)->link; \
476 Sp += sizeofW(StgSeqFrame); \
481 SEQ_FRAME_ENTRY_TEMPLATE(stg_seq_frame_entry, ENTRY_CODE(Sp[0]));
482 SEQ_FRAME_ENTRY_TEMPLATE(stg_seq_frame_0_entry,ENTRY_CODE(Sp[0]));
483 SEQ_FRAME_ENTRY_TEMPLATE(stg_seq_frame_1_entry,ENTRY_CODE(Sp[0]));
484 SEQ_FRAME_ENTRY_TEMPLATE(stg_seq_frame_2_entry,ENTRY_CODE(Sp[0]));
485 SEQ_FRAME_ENTRY_TEMPLATE(stg_seq_frame_3_entry,ENTRY_CODE(Sp[0]));
486 SEQ_FRAME_ENTRY_TEMPLATE(stg_seq_frame_4_entry,ENTRY_CODE(Sp[0]));
487 SEQ_FRAME_ENTRY_TEMPLATE(stg_seq_frame_5_entry,ENTRY_CODE(Sp[0]));
488 SEQ_FRAME_ENTRY_TEMPLATE(stg_seq_frame_6_entry,ENTRY_CODE(Sp[0]));
489 SEQ_FRAME_ENTRY_TEMPLATE(stg_seq_frame_7_entry,ENTRY_CODE(Sp[0]));
491 VEC_POLY_INFO_TABLE(stg_seq_frame, UPD_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, SEQ_FRAME,, EF_);
493 /* -----------------------------------------------------------------------------
496 * This closure takes one argument, which it evaluates and returns the
497 * result with a direct return (never a vectored return!) in R1. It
498 * does this by pushing a SEQ_FRAME on the stack and
499 * entering its argument.
501 * It is used in deleteThread when reverting blackholes.
502 * -------------------------------------------------------------------------- */
504 INFO_TABLE(stg_seq_info,stg_seq_entry,1,0,FUN,,EF_,0,0);
505 STGFUN(stg_seq_entry)
508 STK_CHK_GEN(sizeofW(StgSeqFrame), NO_PTRS, stg_seq_entry, );
509 Sp -= sizeofW(StgSeqFrame);
511 R1.cl = R1.cl->payload[0];
512 JMP_(ENTRY_CODE(*R1.p));