Debian Codegraph @

src/vmcall.c

raw
1 /*
2 * This code fragment implements the core of procedure and generic function
3 * calling sequence in Gauche VM. It is included by vm.c twice, with slight
4 * difference switched by preprocessor macros.
5 */
6
7 /* ADJUST_ARGUMENT_FRAME
8 *
9 * Checks the argument count is OK for call to PROC. if PROC takes &rest
10 * args, fold those arguments to the list. Modifies ARGC to hold the
11 * adjusted size of the argument frame.
12 *
13 * There's two cases in how the arguments are pushed in the VM stack
14 * (normal call and apply call), and there's two cases in how the arguments
15 * should be prepared (fixed-argument call and variable-argument call), so
16 * there are four cases in total.
17 *
18 * Input:
19 * In normal call, arguments at the call site are pushed in the stack
20 * in left-to-right order. For example, if the call site is (f x y z),
21 * the top 3 stack entries of the stack are like this ('|' indicates
22 * stack top; stack grows to right):
23 *
24 * ... x y z|
25 *
26 * in this case, input ARGC is 3.
27 *
28 * In apply call, the stack top is a list of tail of argument list.
29 * If the call site is (apply f x (list y z)), input ARGC is 2 and the
30 * stack top has these two entries:
31 *
32 * ... x (y z)|
33 *
34 * Output:
35 * Suppose proc->required == N and proc->optional == M.
36 * If M == 0, it is a fixed-argument procedure. The stack must contain
37 * N values, and ARGC must be N.
38 *
39 * If M > 0, the stack must contain between N+1 to N+M values, where
40 * the last value is the tail of the argument list. For standard Scheme
41 * variable argument procedure, M is always 1 and the stack contains
42 * N required arguments plus one list of 'rest' argument.
43 */
44
45 #undef ADJUST_ARGUMENT_FRAME
46 #if !defined(APPLY_CALL)
47 #define ADJUST_ARGUMENT_FRAME(proc, argc) \
48 do { \
49 int reqargs = SCM_PROCEDURE_REQUIRED(proc); \
50 int optargs = SCM_PROCEDURE_OPTIONAL(proc); \
51 if (optargs) { \
52 ScmObj p = SCM_NIL; \
53 if (argc < reqargs) { \
54 wna(vm, VAL0, argc, -1); RETURN_OP(); NEXT; \
55 } \
56 /* fold &rest args */ \
57 while (argc > reqargs+optargs-1) { \
58 ScmObj a; \
59 POP_ARG(a); \
60 p = Scm_Cons(a, p); \
61 argc--; \
62 } \
63 PUSH_ARG(p); \
64 argc++; \
65 } else { \
66 if (argc != reqargs) { \
67 wna(vm, VAL0, argc, -1); RETURN_OP(); NEXT; \
68 } \
69 } \
70 } while (0)
71 #else /*APPLY_CALL*/
72 #define ADJUST_ARGUMENT_FRAME(proc, argc) \
73 do { \
74 ScmObj p, a; \
75 int reqargs = SCM_PROCEDURE_REQUIRED(proc); \
76 int optargs = SCM_PROCEDURE_OPTIONAL(proc); \
77 if (optargs) { \
78 int rargc = check_arglist_tail_for_apply(vm, *(vm->sp - 1), -1); \
79 if ((rargc+argc-1) < reqargs) { \
80 wna(vm, VAL0, rargc+argc-1, rargc); RETURN_OP(); NEXT; \
81 } \
82 POP_ARG(p); /* tail of arglist */ \
83 if (argc > reqargs+optargs) { \
84 /* fold rest args. */ \
85 p = Scm_CopyList(p); \
86 for (int c=argc; c>reqargs+optargs; c--) { \
87 POP_ARG(a); \
88 p = Scm_Cons(a, p); \
89 } \
90 PUSH_ARG(p); \
91 } else { \
92 /* 'unfold' rest arg */ \
93 CHECK_STACK(reqargs + optargs - argc + 1); \
94 for (int c=argc; \
95 SCM_PAIRP(p) && c<reqargs+optargs; \
96 c++) { \
97 PUSH_ARG(SCM_CAR(p)); \
98 p = SCM_CDR(p); \
99 } \
100 p = Scm_CopyList(p); \
101 PUSH_ARG(p); \
102 } \
103 } else { \
104 /* no optargs */ \
105 int max_rargc = reqargs-argc+1+1; \
106 int rargc = check_arglist_tail_for_apply(vm, *(vm->sp - 1), max_rargc); \
107 if ((rargc+argc-1) != reqargs) { \
108 if (rargc >= max_rargc) { \
109 rargc = check_arglist_tail_for_apply(vm, *(vm->sp - 1), -1); \
110 } \
111 wna(vm, VAL0, rargc+argc-1, rargc); RETURN_OP(); NEXT; \
112 } \
113 POP_ARG(p); /* tail of arglist */ \
114 if (rargc > 0) { \
115 /* 'unfold' rest arg */ \
116 CHECK_STACK(rargc); \
117 do { \
118 PUSH_ARG(SCM_CAR(p)); \
119 p = SCM_CDR(p); \
120 } while (--rargc > 0); \
121 } \
122 } \
123 argc = SP-ARGP; \
124 } while (0)
125 #endif /*APPLY_CALL*/
126
127 /* avoid duplication of labels */
128 #undef GENERIC_ENTRY
129 #undef DO_METHOD_CALL
130 #undef APP
131 #if !defined(APPLY_CALL)
132 #define GENERIC_ENTRY generic_entry
133 #define DO_METHOD_CALL do_method_call
134 #define APP FALSE
135 #else
136 #define GENERIC_ENTRY generic_entry_app
137 #define DO_METHOD_CALL do_method_call_app
138 #define APP TRUE
139 #endif
140
141 /*
142 * The code fragment begins here. It is included in run_loop,
143 * so the VM registers are fully accessible via macros (SP, ARGP, etc.)
144 *
145 * NB: argc, proctype and nm are defined in outer scope.
146 */
147
148 {
149 ScmObj mm, *fp;
150
151 argc = (int)(SP - ARGP);
152 vm->numVals = 1; /* default */
153
154 /* object-apply hook. shift args, and insert val0 into
155 the fist arg slot, then call GenericObjectApply. */
156 if (MOSTLY_FALSE(!SCM_PROCEDUREP(VAL0))) {
157 int i;
158 CHECK_STACK_PARANOIA(1);
159 for (i=0; i<argc; i++) {
160 *(SP-i) = *(SP-i-1);
161 }
162 *(SP-argc) = VAL0;
163 SP++; argc++;
164 VAL0 = SCM_OBJ(&Scm_GenericObjectApply);
165 proctype = SCM_PROC_GENERIC;
166 nm = SCM_FALSE;
167 goto GENERIC_ENTRY;
168 }
169
170 /*
171 * We process the common cases first
172 */
173 proctype = SCM_PROCEDURE_TYPE(VAL0);
174 if (proctype == SCM_PROC_SUBR) {
175 /* We don't need to complete environment frame. Just need to
176 adjust sp, so that stack-operating procs called from subr
177 won't be confused. */
178 ADJUST_ARGUMENT_FRAME(VAL0, argc);
179 SP = ARGP;
180 PC = PC_TO_RETURN;
181 #if GAUCHE_FFX
182 /* We can't pass FLONUM_REGs unless the subr is flagged as
183 IMMEDIATE_ARG. */
184 if (!(SCM_SUBR_FLAGS(VAL0)&SCM_SUBR_IMMEDIATE_ARG)) {
185 ScmObj *ap = SP;
186 int i = 0;
187 for (; i<argc; i++, ap++) SCM_FLONUM_ENSURE_MEM(*ap);
188 }
189 #endif
190
191 do {
192 SCM_PROF_COUNT_CALL(vm, VAL0);
193 vm->trampoline = -1;
194 VAL0 = SCM_SUBR(VAL0)->func(ARGP, argc, SCM_SUBR(VAL0)->data);
195 } while ((argc = vm->trampoline) >= 0);
196 /* the subr may have substituted pc, so we need to check
197 if we can pop the continuation immediately. */
198 if (TAIL_POS()) RETURN_OP();
199 CHECK_INTR;
200 NEXT;
201 }
202 if (proctype == SCM_PROC_CLOSURE) {
203 ADJUST_ARGUMENT_FRAME(VAL0, argc);
204 if (argc) {
205 FINISH_ENV(SCM_PROCEDURE_INFO(VAL0), SCM_CLOSURE(VAL0)->env);
206 } else {
207 ENV = SCM_CLOSURE(VAL0)->env;
208 ARGP = SP;
209 }
210 vm->base = SCM_COMPILED_CODE(SCM_CLOSURE(VAL0)->code);
211 PC = vm->base->code;
212 CHECK_STACK(vm->base->maxstack);
213 SCM_PROF_COUNT_CALL(vm, SCM_OBJ(vm->base));
214 VAL0 = SCM_MAKE_INT(argc); /* keep argc to VAL0. */
215 NEXT;
216 }
217
218 /*
219 * Generic function application
220 */
221
222 /* First, compute methods */
223 nm = SCM_FALSE;
224 if (proctype == SCM_PROC_GENERIC) {
225 if (!SCM_GENERICP(VAL0)) {
226 /* use scheme-defined MOP. we modify the stack frame so
227 that it is converted to an application of pure generic
228 fn apply-generic. */
229 ScmObj args, arg;
230 #if !defined(APPLY_CALL)
231 if (argc < 2) CHECK_STACK(2);
232 args = SCM_NIL;
233 for (int i=0; i<argc; i++) {
234 POP_ARG(arg);
235 args = Scm_Cons(arg, args);
236 }
237 ARGP = SP;
238 argc = 2;
239 PUSH_ARG(VAL0);
240 PUSH_ARG(args);
241 #else /* APPLY_CALL */
242 if (argc < 3) CHECK_STACK(3);
243 POP_ARG(args);
244 argc--;
245 for (int i=0; i<argc; i++) {
246 POP_ARG(arg);
247 args = Scm_Cons(arg, args);
248 }
249 ARGP = SP;
250 argc = 3;
251 PUSH_ARG(VAL0);
252 PUSH_ARG(args);
253 PUSH_ARG(SCM_NIL); /* applyargs */
254 #endif /* APPLY_CALL */
255 VAL0 = SCM_OBJ(&Scm_GenericApplyGeneric);
256 }
257 GENERIC_ENTRY:
258 /* pure generic application. we implement MOP in C. */
259 mm = Scm_ComputeApplicableMethods(SCM_GENERIC(VAL0), ARGP, argc, APP);
260 if (!SCM_NULLP(mm)) {
261 /* sort methods. we only need as many args as
262 gf->maxReqargs to order methods, so we only unfold that
263 many args if applyargs.
264 */
265 #if defined(APPLY_CALL)
266 if (argc-1<SCM_GENERIC(VAL0)->maxReqargs) {
267 ScmObj args;
268 POP_ARG(args);
269 CHECK_STACK(SCM_GENERIC(VAL0)->maxReqargs - argc);
270 while (argc <= SCM_GENERIC(VAL0)->maxReqargs
271 && SCM_PAIRP(args)) {
272 PUSH_ARG(SCM_CAR(args));
273 args = SCM_CDR(args);
274 argc++;
275 }
276 PUSH_ARG(args);
277 }
278 #endif /*APPLY_CALL*/
279 #if GAUCHE_FFX
280 {
281 ScmObj *ap = ARGP;
282 for (int i=0;i<argc; i++, ap++) SCM_FLONUM_ENSURE_MEM(*ap);
283 }
284 #endif /*GAUCHE_FFX*/
285 if (SCM_PAIRP(SCM_CDR(mm))) {
286 mm = Scm_SortMethods(mm, ARGP, argc);
287 }
288 if (SCM_METHOD_LEAF_P(SCM_CAR(mm))) {
289 nm = SCM_TRUE; /* Dummy */
290 } else {
291 nm = Scm_MakeNextMethod(SCM_GENERIC(VAL0), SCM_CDR(mm),
292 ARGP, argc, TRUE, APP);
293 }
294 VAL0 = SCM_CAR(mm);
295 proctype = SCM_PROC_METHOD;
296 }
297 } else if (proctype == SCM_PROC_NEXT_METHOD) {
298 ScmNextMethod *n = SCM_NEXT_METHOD(VAL0);
299 int use_saved_args = FALSE;
300 int apply_call_p = APP;
301 /* If no arguments are given to next-method, we use the args
302 saved in the next-method. */
303 #if !defined(APPLY_CALL)
304 use_saved_args = (argc == 0);
305 #else /*APPLY_CALL*/
306 use_saved_args = (argc == 1 && SCM_NULLP(*(SP-1)));
307 #endif /*APPLY_CALL*/
308 if (use_saved_args) {
309 CHECK_STACK(n->argc+1);
310 memcpy(SP, n->argv, sizeof(ScmObj)*n->argc);
311 SP += n->argc;
312 argc = n->argc;
313 apply_call_p = n->applyargs;
314 }
315 if (SCM_NULLP(n->methods)) {
316 VAL0 = SCM_OBJ(n->generic);
317 proctype = SCM_PROC_GENERIC;
318 } else {
319 if (SCM_METHOD_LEAF_P(SCM_CAR(n->methods))) {
320 nm = SCM_TRUE; /* Dummy */
321 } else {
322 nm = Scm_MakeNextMethod(n->generic, SCM_CDR(n->methods),
323 ARGP, argc, TRUE, apply_call_p);
324 }
325 VAL0 = SCM_CAR(n->methods);
326 proctype = SCM_PROC_METHOD;
327 }
328 if (use_saved_args) {
329 #if !defined(APPLY_CALL)
330 if (apply_call_p) goto do_method_call_app;
331 #else /*APPLY_CALL*/
332 if (!apply_call_p) goto do_method_call;
333 #endif /*APPLY_CALL*/
334 }
335 } else if (proctype == SCM_PROC_METHOD) {
336 VM_ERR(("Attempt to call a method %S without using a generic function.",
337 VAL0));
338 } else {
339 Scm_Panic("something's wrong.");
340 }
341
342 DO_METHOD_CALL:
343 fp = ARGP;
344 if (proctype == SCM_PROC_GENERIC) {
345 /* we have no applicable methods. call fallback fn. */
346 #if defined(APPLY_CALL)
347 /* TEMPORARY - we need to fix the protocol of calling fallback
348 generic, so that it can accept large number of arguments
349 without unfolding all of them on the VM stack. */
350 ScmObj args;
351
352 POP_ARG(args);
353 argc--;
354 while (SCM_PAIRP(args)) {
355 PUSH_ARG(SCM_CAR(args));
356 args = SCM_CDR(args);
357 argc++;
358 }
359 #endif /*APPLY_CALL*/
360 FINISH_ENV(SCM_PROCEDURE_INFO(VAL0), NULL);
361 PC = PC_TO_RETURN;
362 SCM_PROF_COUNT_CALL(vm, VAL0);
363 VAL0 = SCM_GENERIC(VAL0)->fallback(fp, argc, SCM_GENERIC(VAL0));
364 /* The fallback may substituted pc, so we need to check if we
365 can pop the continuation immediately. */
366 if (TAIL_POS()) RETURN_OP();
367 NEXT;
368 }
369
370 /*
371 * Now, apply method
372 */
373 ADJUST_ARGUMENT_FRAME(VAL0, argc);
374
375 VM_ASSERT(proctype == SCM_PROC_METHOD);
376 VM_ASSERT(!SCM_FALSEP(nm));
377 if (SCM_METHOD(VAL0)->func) {
378 /* C-defined method */
379 FINISH_ENV(SCM_PROCEDURE_INFO(VAL0), NULL);
380 PC = PC_TO_RETURN;
381 SCM_PROF_COUNT_CALL(vm, VAL0);
382 VAL0 = SCM_METHOD(VAL0)->func(SCM_NEXT_METHOD(nm), fp, argc,
383 SCM_METHOD(VAL0)->data);
384 /* the func may substituted pc, so we need to check if we can
385 pop the continuation immediately. */
386 if (TAIL_POS()) RETURN_OP();
387 } else {
388 /* Scheme-defined method. next-method arg is passed as the
389 last arg (note that rest arg is already folded). */
390 PUSH_ARG(SCM_OBJ(nm));
391 FINISH_ENV(SCM_PROCEDURE_INFO(VAL0),
392 SCM_METHOD(VAL0)->env);
393 VM_ASSERT(SCM_COMPILED_CODE_P(SCM_METHOD(VAL0)->data));
394 vm->base = SCM_COMPILED_CODE(SCM_METHOD(VAL0)->data);
395 PC = vm->base->code;
396 CHECK_STACK(vm->base->maxstack);
397 SCM_PROF_COUNT_CALL(vm, SCM_OBJ(vm->base));
398 VAL0 = SCM_MAKE_INT(argc); /* keep argc to VAL0. */
399 }
400 NEXT;
401 }