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 * stg_ap_0_info <--- Sp
20 * NOTE: this needs to be a polymorphic return point, because we can't
21 * be sure that the thing being evaluated is not a function.
24 #if MAX_VECTORED_RTN > 8
25 #error MAX_VECTORED_RTN has changed: please modify stg_ap_0 too.
28 STRING(stg_ap_0_ret_str,"stg_ap_0_ret... ")
30 INFO_TABLE_RET( stg_ap_0,
31 0/*framsize*/, 0/*bitmap*/, RET_SMALL,
41 // fn is in R1, no args on the stack
44 foreign "C" debugBelch(stg_ap_0_ret_str);
45 foreign "C" printClosure(R1 "ptr"));
48 foreign "C" checkStackChunk(Sp+WDS(1) "ptr",
49 CurrentTSO + OFFSET_StgTSO_stack +
50 WDS(StgTSO_stack_size(CurrentTSO)) "ptr"));
56 /* -----------------------------------------------------------------------------
59 This entry code is *only* called by one of the stg_ap functions.
60 On entry: Sp points to the remaining arguments on the stack. If
61 the stack check fails, we can just push the PAP on the stack and
62 return to the scheduler.
64 On entry: R1 points to the PAP. The rest of the function's
65 arguments (apart from those that are already in the PAP) are on the
66 stack, starting at Sp(0). R2 contains an info table which
67 describes these arguments, which is used in the event that the
68 stack check in the entry code below fails. The info table is
69 currently one of the stg_ap_*_ret family, as this code is always
70 entered from those functions.
72 The idea is to copy the chunk of stack from the PAP object onto the
73 stack / into registers, and enter the function.
74 -------------------------------------------------------------------------- */
76 INFO_TABLE(stg_PAP,/*special layout*/0,0,PAP,"PAP","PAP")
83 Words = TO_W_(StgPAP_n_args(pap));
86 // Check for stack overflow and bump the stack pointer.
87 // We have a hand-rolled stack check fragment here, because none of
88 // the canned ones suit this situation.
90 if ((Sp - WDS(Words)) < SpLim) {
91 // there is a return address in R2 in the event of a
92 // stack check failure. The various stg_apply functions arrange
93 // this before calling stg_PAP_entry.
103 // Enter PAP cost centre
104 ENTER_CCS_PAP_CL(pap);
106 R1 = StgPAP_fun(pap);
111 p = pap + SIZEOF_StgHeader + OFFSET_StgPAP_payload;
128 info = %GET_FUN_INFO(R1);
130 type = TO_W_(StgFunInfoExtra_fun_type(info));
131 if (type == ARG_GEN) {
132 jump StgFunInfoExtra_slow_apply(info);
134 if (type == ARG_GEN_BIG) {
135 jump StgFunInfoExtra_slow_apply(info);
137 if (type == ARG_BCO) {
140 Sp(0) = stg_apply_interp_info;
141 jump stg_yield_to_interpreter;
143 jump W_[stg_ap_stack_entries +
144 WDS(TO_W_(StgFunInfoExtra_fun_type(info)))];
148 /* -----------------------------------------------------------------------------
149 Entry Code for an AP (a PAP with arity zero).
151 The entry code is very similar to a PAP, except there are no
152 further arguments on the stack to worry about, so the stack check
153 is simpler. We must also push an update frame on the stack before
154 applying the function.
155 -------------------------------------------------------------------------- */
157 INFO_TABLE(stg_AP,/*special layout*/0,0,AP,"AP","AP")
164 Words = TO_W_(StgAP_n_args(ap));
167 * Check for stack overflow. IMPORTANT: use a _NP check here,
168 * because if the check fails, we might end up blackholing this very
169 * closure, in which case we must enter the blackhole on return rather
170 * than continuing to evaluate the now-defunct closure.
172 STK_CHK_NP(WDS(Words) + SIZEOF_StgUpdateFrame);
174 PUSH_UPD_FRAME(Sp - SIZEOF_StgUpdateFrame, R1);
175 Sp = Sp - SIZEOF_StgUpdateFrame - WDS(Words);
180 // Enter PAP cost centre
181 ENTER_CCS_PAP_CL(ap); // ToDo: ENTER_CC_AP_CL
188 p = ap + SIZEOF_StgHeader + OFFSET_StgAP_payload;
205 info = %GET_FUN_INFO(R1);
207 type = TO_W_(StgFunInfoExtra_fun_type(info));
208 if (type == ARG_GEN) {
209 jump StgFunInfoExtra_slow_apply(info);
211 if (type == ARG_GEN_BIG) {
212 jump StgFunInfoExtra_slow_apply(info);
214 if (type == ARG_BCO) {
217 Sp(0) = stg_apply_interp_info;
218 jump stg_yield_to_interpreter;
220 jump W_[stg_ap_stack_entries +
221 WDS(TO_W_(StgFunInfoExtra_fun_type(info)))];
225 /* -----------------------------------------------------------------------------
226 Entry Code for an AP_STACK.
228 Very similar to a PAP and AP. The layout is the same as PAP
229 and AP, except that the payload is a chunk of stack instead of
230 being described by the function's info table. Like an AP,
231 there are no further arguments on the stack to worry about.
232 However, the function closure (ap->fun) does not necessarily point
233 directly to a function, so we have to enter it using stg_ap_0.
234 -------------------------------------------------------------------------- */
236 INFO_TABLE(stg_AP_STACK,/*special layout*/0,0,AP_STACK,"AP_STACK","AP_STACK")
243 Words = StgAP_STACK_size(ap);
246 * Check for stack overflow. IMPORTANT: use a _NP check here,
247 * because if the check fails, we might end up blackholing this very
248 * closure, in which case we must enter the blackhole on return rather
249 * than continuing to evaluate the now-defunct closure.
251 STK_CHK_NP(WDS(Words) + SIZEOF_StgUpdateFrame);
253 PUSH_UPD_FRAME(Sp - SIZEOF_StgUpdateFrame, R1);
254 Sp = Sp - SIZEOF_StgUpdateFrame - WDS(Words);
259 // Enter PAP cost centre
260 ENTER_CCS_PAP_CL(ap); // ToDo: ENTER_CC_AP_CL
262 R1 = StgAP_STACK_fun(ap);
267 p = ap + SIZEOF_StgThunkHeader + OFFSET_StgAP_STACK_payload;