1 /* -----------------------------------------------------------------------------
3 * (c) The University of Glasgow 2004
5 * Application-related bits.
7 * This file is written in a subset of C--, extended with various
8 * features specific to GHC. It is compiled by GHC directly. For the
9 * syntax of .cmm files, see the parser in ghc/compiler/cmm/CmmParse.y.
11 * -------------------------------------------------------------------------- */
15 /* ----------------------------------------------------------------------------
16 * Evaluate a closure and return it.
18 * There isn't an info table / return address version of stg_ap_0, because
19 * everything being returned is guaranteed evaluated, so it would be a no-op.
22 STRING(stg_ap_0_ret_str,"stg_ap_0_ret... ")
26 // fn is in R1, no args on the stack
29 foreign "C" debugBelch(stg_ap_0_ret_str) [R1];
30 foreign "C" printClosure(R1 "ptr") [R1]);
33 foreign "C" checkStackFrame(Sp "ptr") [R1]);
38 /* -----------------------------------------------------------------------------
41 This entry code is *only* called by one of the stg_ap functions.
42 On entry: Sp points to the remaining arguments on the stack. If
43 the stack check fails, we can just push the PAP on the stack and
44 return to the scheduler.
46 On entry: R1 points to the PAP. The rest of the function's
47 arguments (apart from those that are already in the PAP) are on the
48 stack, starting at Sp(0). R2 contains an info table which
49 describes these arguments, which is used in the event that the
50 stack check in the entry code below fails. The info table is
51 currently one of the stg_ap_*_ret family, as this code is always
52 entered from those functions.
54 The idea is to copy the chunk of stack from the PAP object onto the
55 stack / into registers, and enter the function.
56 -------------------------------------------------------------------------- */
58 INFO_TABLE(stg_PAP,/*special layout*/0,0,PAP,"PAP","PAP")
59 { foreign "C" barf("PAP object entered!") never returns; }
68 Words = TO_W_(StgPAP_n_args(pap));
71 // Check for stack overflow and bump the stack pointer.
72 // We have a hand-rolled stack check fragment here, because none of
73 // the canned ones suit this situation.
75 if ((Sp - WDS(Words)) < SpLim) {
76 // there is a return address in R2 in the event of a
77 // stack check failure. The various stg_apply functions arrange
78 // this before calling stg_PAP_entry.
88 // Enter PAP cost centre
89 ENTER_CCS_PAP_CL(pap);
94 p = pap + SIZEOF_StgHeader + OFFSET_StgPAP_payload;
104 R1 = StgPAP_fun(pap);
106 /* DEBUGGING CODE, ensures that arity 1 and 2 functions are entered tagged
107 if (TO_W_(StgFunInfoExtra_arity(%FUN_INFO(%INFO_PTR(UNTAG(R1))))) == 1 ) {
113 if (TO_W_(StgFunInfoExtra_arity(%FUN_INFO(%INFO_PTR(UNTAG(R1))))) == 2 ) {
124 jump %GET_ENTRY(UNTAG(R1));
127 info = %GET_FUN_INFO(UNTAG(R1));
129 type = TO_W_(StgFunInfoExtra_fun_type(info));
130 if (type == ARG_GEN) {
131 jump StgFunInfoExtra_slow_apply(info);
133 if (type == ARG_GEN_BIG) {
134 jump StgFunInfoExtra_slow_apply(info);
136 if (type == ARG_BCO) {
139 Sp(0) = stg_apply_interp_info;
140 jump stg_yield_to_interpreter;
142 jump W_[stg_ap_stack_entries +
143 WDS(TO_W_(StgFunInfoExtra_fun_type(info)))];
147 /* -----------------------------------------------------------------------------
148 Entry Code for an AP (a PAP with arity zero).
150 The entry code is very similar to a PAP, except there are no
151 further arguments on the stack to worry about, so the stack check
152 is simpler. We must also push an update frame on the stack before
153 applying the function.
154 -------------------------------------------------------------------------- */
156 INFO_TABLE(stg_AP,/*special layout*/0,0,AP,"AP","AP")
163 Words = TO_W_(StgAP_n_args(ap));
166 * Check for stack overflow. IMPORTANT: use a _NP check here,
167 * because if the check fails, we might end up blackholing this very
168 * closure, in which case we must enter the blackhole on return rather
169 * than continuing to evaluate the now-defunct closure.
171 STK_CHK_NP(WDS(Words) + SIZEOF_StgUpdateFrame);
173 PUSH_UPD_FRAME(Sp - SIZEOF_StgUpdateFrame, R1);
174 Sp = Sp - SIZEOF_StgUpdateFrame - WDS(Words);
179 // Enter PAP cost centre
180 ENTER_CCS_PAP_CL(ap); // ToDo: ENTER_CC_AP_CL
185 p = ap + SIZEOF_StgHeader + OFFSET_StgAP_payload;
201 jump %GET_ENTRY(UNTAG(R1));
204 info = %GET_FUN_INFO(UNTAG(R1));
206 type = TO_W_(StgFunInfoExtra_fun_type(info));
207 if (type == ARG_GEN) {
208 jump StgFunInfoExtra_slow_apply(info);
210 if (type == ARG_GEN_BIG) {
211 jump StgFunInfoExtra_slow_apply(info);
213 if (type == ARG_BCO) {
216 Sp(0) = stg_apply_interp_info;
217 jump stg_yield_to_interpreter;
219 jump W_[stg_ap_stack_entries +
220 WDS(TO_W_(StgFunInfoExtra_fun_type(info)))];
224 /* AP_NOUPD is exactly like AP, except that no update frame is pushed.
225 Use for thunks that are guaranteed to be entered once only, such as
226 those generated by the byte-code compiler for inserting breakpoints. */
228 INFO_TABLE(stg_AP_NOUPD,/*special layout*/0,0,AP,"AP_NOUPD","AP_NOUPD")
235 Words = TO_W_(StgAP_n_args(ap));
238 * Check for stack overflow. IMPORTANT: use a _NP check here,
239 * because if the check fails, we might end up blackholing this very
240 * closure, in which case we must enter the blackhole on return rather
241 * than continuing to evaluate the now-defunct closure.
243 STK_CHK_NP(WDS(Words));
244 Sp = Sp - WDS(Words);
249 // Enter PAP cost centre
250 ENTER_CCS_PAP_CL(ap); // ToDo: ENTER_CC_AP_CL
255 p = ap + SIZEOF_StgHeader + OFFSET_StgAP_payload;
271 jump %GET_ENTRY(UNTAG(R1));
274 info = %GET_FUN_INFO(UNTAG(R1));
276 type = TO_W_(StgFunInfoExtra_fun_type(info));
277 if (type == ARG_GEN) {
278 jump StgFunInfoExtra_slow_apply(info);
280 if (type == ARG_GEN_BIG) {
281 jump StgFunInfoExtra_slow_apply(info);
283 if (type == ARG_BCO) {
286 Sp(0) = stg_apply_interp_info;
287 jump stg_yield_to_interpreter;
289 jump W_[stg_ap_stack_entries +
290 WDS(TO_W_(StgFunInfoExtra_fun_type(info)))];
294 /* -----------------------------------------------------------------------------
295 Entry Code for an AP_STACK.
297 Very similar to a PAP and AP. The layout is the same as PAP
298 and AP, except that the payload is a chunk of stack instead of
299 being described by the function's info table. Like an AP,
300 there are no further arguments on the stack to worry about.
301 However, the function closure (ap->fun) does not necessarily point
302 directly to a function, so we have to enter it using stg_ap_0.
303 -------------------------------------------------------------------------- */
305 INFO_TABLE(stg_AP_STACK,/*special layout*/0,0,AP_STACK,"AP_STACK","AP_STACK")
312 Words = StgAP_STACK_size(ap);
315 * Check for stack overflow. IMPORTANT: use a _NP check here,
316 * because if the check fails, we might end up blackholing this very
317 * closure, in which case we must enter the blackhole on return rather
318 * than continuing to evaluate the now-defunct closure.
320 STK_CHK_NP(WDS(Words) + SIZEOF_StgUpdateFrame + WDS(AP_STACK_SPLIM));
321 /* ensure there is at least AP_STACK_SPLIM words of headroom available
322 * after unpacking the AP_STACK. See bug #1466 */
324 PUSH_UPD_FRAME(Sp - SIZEOF_StgUpdateFrame, R1);
325 Sp = Sp - SIZEOF_StgUpdateFrame - WDS(Words);
330 // Enter PAP cost centre
331 ENTER_CCS_PAP_CL(ap); // ToDo: ENTER_CC_AP_CL
336 p = ap + SIZEOF_StgHeader + OFFSET_StgAP_STACK_payload;
349 R1 = StgAP_STACK_fun(ap);
354 /* -----------------------------------------------------------------------------
355 AP_STACK_NOUPD - exactly like AP_STACK, but doesn't push an update frame.
356 -------------------------------------------------------------------------- */
358 INFO_TABLE(stg_AP_STACK_NOUPD,/*special layout*/0,0,AP_STACK,
359 "AP_STACK_NOUPD","AP_STACK_NOUPD")
366 Words = StgAP_STACK_size(ap);
369 * Check for stack overflow. IMPORTANT: use a _NP check here,
370 * because if the check fails, we might end up blackholing this very
371 * closure, in which case we must enter the blackhole on return rather
372 * than continuing to evaluate the now-defunct closure.
374 STK_CHK_NP(WDS(Words) + WDS(AP_STACK_SPLIM));
375 /* ensure there is at least AP_STACK_SPLIM words of headroom available
376 * after unpacking the AP_STACK. See bug #1466 */
378 Sp = Sp - WDS(Words);
383 // Enter PAP cost centre
384 ENTER_CCS_PAP_CL(ap); // ToDo: ENTER_CC_AP_CL
389 p = ap + SIZEOF_StgHeader + OFFSET_StgAP_STACK_payload;
402 R1 = StgAP_STACK_fun(ap);