2 * eval.i.c - actual evaluator code for GUILE
4 * Copyright (C) 2002, 03, 04, 05, 06, 07 Free Software Foundation, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #undef EVAL_DEBUGGING_P
32 This code is specific for the debugging support.
35 #define EVAL_DEBUGGING_P 1
36 #define CEVAL deval /* Substitute all uses of ceval */
37 #define SCM_APPLY scm_dapply
38 #define PREP_APPLY(p, l) \
39 { ++debug.info; debug.info->a.proc = p; debug.info->a.args = l; }
43 SCM_SET_ARGSREADY (debug);\
44 if (scm_check_apply_p && SCM_TRAPS_P)\
45 if (SCM_APPLY_FRAME_P || (SCM_TRACE_P && SCM_PROCTRACEP (proc)))\
47 SCM tmp, tail = scm_from_bool(SCM_TRACED_FRAME_P (debug)); \
48 SCM_SET_TRACED_FRAME (debug); \
50 tmp = scm_make_debugobj (&debug);\
51 scm_call_3 (SCM_APPLY_FRAME_HDLR, scm_sym_apply_frame, tmp, tail);\
56 #define RETURN(e) do { proc = (e); goto exit; } while (0)
59 # ifndef EVAL_STACK_CHECKING
60 # define EVAL_STACK_CHECKING
61 # endif /* EVAL_STACK_CHECKING */
62 #endif /* STACK_CHECKING */
68 deval_args (SCM l
, SCM env
, SCM proc
, SCM
*lloc
)
71 while (scm_is_pair (l
))
73 const SCM res
= SCM_I_XEVALCAR (l
, env
, 1);
75 *lloc
= scm_list_1 (res
);
76 lloc
= SCM_CDRLOC (*lloc
);
80 scm_wrong_num_args (proc
);
88 Code is specific to debugging-less support.
93 #define SCM_APPLY scm_apply
94 #define PREP_APPLY(proc, args)
96 #define RETURN(x) do { return x; } while (0)
97 #define EVAL_DEBUGGING_P 0
100 # ifndef NO_CEVAL_STACK_CHECKING
101 # define EVAL_STACK_CHECKING
109 ceval_letrec_inits (SCM env
, SCM init_forms
, SCM
**init_values_eol
)
112 int i
= 0, imax
= sizeof (argv
) / sizeof (SCM
);
114 while (!scm_is_null (init_forms
))
118 ceval_letrec_inits (env
, init_forms
, init_values_eol
);
121 argv
[i
++] = SCM_I_XEVALCAR (init_forms
, env
, 0);
122 init_forms
= SCM_CDR (init_forms
);
125 for (i
--; i
>= 0; i
--)
127 **init_values_eol
= scm_list_1 (argv
[i
]);
128 *init_values_eol
= SCM_CDRLOC (**init_values_eol
);
133 scm_ceval_args (SCM l
, SCM env
, SCM proc
)
135 SCM results
= SCM_EOL
, *lloc
= &results
, res
;
136 while (scm_is_pair (l
))
138 res
= EVALCAR (l
, env
);
140 *lloc
= scm_list_1 (res
);
141 lloc
= SCM_CDRLOC (*lloc
);
144 if (!scm_is_null (l
))
145 scm_wrong_num_args (proc
);
151 scm_eval_args (SCM l
, SCM env
, SCM proc
)
153 return scm_ceval_args (l
, env
, proc
);
163 #define EVAL(x, env) SCM_I_XEVAL(x, env, EVAL_DEBUGGING_P)
164 #define EVALCAR(x, env) SCM_I_XEVALCAR(x, env, EVAL_DEBUGGING_P)
168 /* Update the toplevel environment frame ENV so that it refers to the
170 #define UPDATE_TOPLEVEL_ENV(env) \
172 SCM p = scm_current_module_lookup_closure (); \
173 if (p != SCM_CAR (env)) \
174 env = scm_top_level_env (p); \
178 #define SCM_VALIDATE_NON_EMPTY_COMBINATION(x) \
179 ASSERT_SYNTAX (!scm_is_eq ((x), SCM_EOL), s_empty_combination, x)
182 /* This is the evaluator. Like any real monster, it has three heads:
184 * ceval is the non-debugging evaluator, deval is the debugging version. Both
185 * are implemented using a common code base, using the following mechanism:
186 * CEVAL is a macro, which is either defined to ceval or deval. Thus, there
187 * is no function CEVAL, but the code for CEVAL actually compiles to either
188 * ceval or deval. When CEVAL is defined to ceval, it is known that the macro
189 * DEVAL is not defined. When CEVAL is defined to deval, then the macro DEVAL
190 * is known to be defined. Thus, in CEVAL parts for the debugging evaluator
191 * are enclosed within #ifdef DEVAL ... #endif.
193 * All three (ceval, deval and their common implementation CEVAL) take two
194 * input parameters, x and env: x is a single expression to be evalutated.
195 * env is the environment in which bindings are searched.
197 * x is known to be a pair. Since x is a single expression, it is necessarily
198 * in a tail position. If x is just a call to another function like in the
199 * expression (foo exp1 exp2 ...), the realization of that call therefore
200 * _must_not_ increase stack usage (the evaluation of exp1, exp2 etc.,
201 * however, may do so). This is realized by making extensive use of 'goto'
202 * statements within the evaluator: The gotos replace recursive calls to
203 * CEVAL, thus re-using the same stack frame that CEVAL was already using.
204 * If, however, x represents some form that requires to evaluate a sequence of
205 * expressions like (begin exp1 exp2 ...), then recursive calls to CEVAL are
206 * performed for all but the last expression of that sequence. */
209 CEVAL (SCM x
, SCM env
)
213 scm_t_debug_frame debug
;
214 scm_t_debug_info
*debug_info_end
;
215 debug
.prev
= scm_i_last_debug_frame ();
218 * The debug.vect contains twice as much scm_t_debug_info frames as the
219 * user has specified with (debug-set! frames <n>).
221 * Even frames are eval frames, odd frames are apply frames.
223 debug
.vect
= (scm_t_debug_info
*) alloca (scm_debug_eframe_size
224 * sizeof (scm_t_debug_info
));
225 debug
.info
= debug
.vect
;
226 debug_info_end
= debug
.vect
+ scm_debug_eframe_size
;
227 scm_i_set_last_debug_frame (&debug
);
229 #ifdef EVAL_STACK_CHECKING
230 if (scm_stack_checking_enabled_p
&& SCM_STACK_OVERFLOW_P (&proc
))
233 debug
.info
->e
.exp
= x
;
234 debug
.info
->e
.env
= env
;
236 scm_report_stack_overflow ();
246 SCM_CLEAR_ARGSREADY (debug
);
247 if (SCM_OVERFLOWP (debug
))
250 * In theory, this should be the only place where it is necessary to
251 * check for space in debug.vect since both eval frames and
252 * available space are even.
254 * For this to be the case, however, it is necessary that primitive
255 * special forms which jump back to `loop', `begin' or some similar
256 * label call PREP_APPLY.
258 else if (++debug
.info
>= debug_info_end
)
260 SCM_SET_OVERFLOW (debug
);
265 debug
.info
->e
.exp
= x
;
266 debug
.info
->e
.env
= env
;
267 if (scm_check_entry_p
&& SCM_TRAPS_P
)
269 if (SCM_ENTER_FRAME_P
270 || (SCM_BREAKPOINTS_P
&& scm_c_source_property_breakpoint_p (x
)))
273 SCM tail
= scm_from_bool (SCM_TAILRECP (debug
));
274 SCM_SET_TAILREC (debug
);
275 stackrep
= scm_make_debugobj (&debug
);
277 stackrep
= scm_call_4 (SCM_ENTER_FRAME_HDLR
,
281 unmemoize_expression (x
, env
));
283 if (scm_is_pair (stackrep
) &&
284 scm_is_eq (SCM_CAR (stackrep
), sym_instead
))
286 /* This gives the possibility for the debugger to modify
287 the source expression before evaluation. */
288 x
= SCM_CDR (stackrep
);
297 if (SCM_ISYMP (SCM_CAR (x
)))
299 switch (ISYMNUM (SCM_CAR (x
)))
301 case (ISYMNUM (SCM_IM_AND
)):
303 while (!scm_is_null (SCM_CDR (x
)))
305 SCM test_result
= EVALCAR (x
, env
);
306 if (scm_is_false (test_result
) || SCM_NILP (test_result
))
311 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
314 case (ISYMNUM (SCM_IM_BEGIN
)):
317 RETURN (SCM_UNSPECIFIED
);
319 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
322 /* If we are on toplevel with a lookup closure, we need to sync
323 with the current module. */
324 if (scm_is_pair (env
) && !scm_is_pair (SCM_CAR (env
)))
326 UPDATE_TOPLEVEL_ENV (env
);
327 while (!scm_is_null (SCM_CDR (x
)))
330 UPDATE_TOPLEVEL_ENV (env
);
336 goto nontoplevel_begin
;
339 while (!scm_is_null (SCM_CDR (x
)))
341 const SCM form
= SCM_CAR (x
);
344 if (SCM_ISYMP (form
))
346 scm_dynwind_begin (0);
347 scm_i_dynwind_pthread_mutex_lock (&source_mutex
);
348 /* check for race condition */
349 if (SCM_ISYMP (SCM_CAR (x
)))
350 m_expand_body (x
, env
);
352 goto nontoplevel_begin
;
355 SCM_VALIDATE_NON_EMPTY_COMBINATION (form
);
358 (void) EVAL (form
, env
);
364 /* scm_eval last form in list */
365 const SCM last_form
= SCM_CAR (x
);
367 if (scm_is_pair (last_form
))
369 /* This is by far the most frequent case. */
371 goto loop
; /* tail recurse */
373 else if (SCM_IMP (last_form
))
374 RETURN (SCM_I_EVALIM (last_form
, env
));
375 else if (SCM_VARIABLEP (last_form
))
376 RETURN (SCM_VARIABLE_REF (last_form
));
377 else if (scm_is_symbol (last_form
))
378 RETURN (*scm_lookupcar (x
, env
, 1));
384 case (ISYMNUM (SCM_IM_CASE
)):
387 const SCM key
= EVALCAR (x
, env
);
389 while (!scm_is_null (x
))
391 const SCM clause
= SCM_CAR (x
);
392 SCM labels
= SCM_CAR (clause
);
393 if (scm_is_eq (labels
, SCM_IM_ELSE
))
395 x
= SCM_CDR (clause
);
396 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
399 while (!scm_is_null (labels
))
401 const SCM label
= SCM_CAR (labels
);
402 if (scm_is_eq (label
, key
)
403 || scm_is_true (scm_eqv_p (label
, key
)))
405 x
= SCM_CDR (clause
);
406 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
409 labels
= SCM_CDR (labels
);
414 RETURN (SCM_UNSPECIFIED
);
417 case (ISYMNUM (SCM_IM_COND
)):
419 while (!scm_is_null (x
))
421 const SCM clause
= SCM_CAR (x
);
422 if (scm_is_eq (SCM_CAR (clause
), SCM_IM_ELSE
))
424 x
= SCM_CDR (clause
);
425 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
430 arg1
= EVALCAR (clause
, env
);
431 /* SRFI 61 extended cond */
432 if (!scm_is_null (SCM_CDR (clause
))
433 && !scm_is_null (SCM_CDDR (clause
))
434 && scm_is_eq (SCM_CADDR (clause
), SCM_IM_ARROW
))
436 SCM xx
, guard_result
;
437 if (SCM_VALUESP (arg1
))
438 arg1
= scm_struct_ref (arg1
, SCM_INUM0
);
440 arg1
= scm_list_1 (arg1
);
441 xx
= SCM_CDR (clause
);
442 proc
= EVALCAR (xx
, env
);
443 guard_result
= SCM_APPLY (proc
, arg1
, SCM_EOL
);
444 if (scm_is_true (guard_result
)
445 && !SCM_NILP (guard_result
))
447 proc
= SCM_CDDR (xx
);
448 proc
= EVALCAR (proc
, env
);
449 PREP_APPLY (proc
, arg1
);
453 else if (scm_is_true (arg1
) && !SCM_NILP (arg1
))
455 x
= SCM_CDR (clause
);
458 else if (!scm_is_eq (SCM_CAR (x
), SCM_IM_ARROW
))
460 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
466 proc
= EVALCAR (proc
, env
);
467 PREP_APPLY (proc
, scm_list_1 (arg1
));
475 RETURN (SCM_UNSPECIFIED
);
478 case (ISYMNUM (SCM_IM_DO
)):
481 /* Compute the initialization values and the initial environment. */
482 SCM init_forms
= SCM_CAR (x
);
483 SCM init_values
= SCM_EOL
;
484 while (!scm_is_null (init_forms
))
486 init_values
= scm_cons (EVALCAR (init_forms
, env
), init_values
);
487 init_forms
= SCM_CDR (init_forms
);
490 env
= SCM_EXTEND_ENV (SCM_CAR (x
), init_values
, env
);
494 SCM test_form
= SCM_CAR (x
);
495 SCM body_forms
= SCM_CADR (x
);
496 SCM step_forms
= SCM_CDDR (x
);
498 SCM test_result
= EVALCAR (test_form
, env
);
500 while (scm_is_false (test_result
) || SCM_NILP (test_result
))
503 /* Evaluate body forms. */
505 for (temp_forms
= body_forms
;
506 !scm_is_null (temp_forms
);
507 temp_forms
= SCM_CDR (temp_forms
))
509 SCM form
= SCM_CAR (temp_forms
);
510 /* Dirk:FIXME: We only need to eval forms that may have
511 * a side effect here. This is only true for forms that
512 * start with a pair. All others are just constants.
513 * Since with the current memoizer 'form' may hold a
514 * constant, we call EVAL here to handle the constant
515 * cases. In the long run it would make sense to have
516 * the macro transformer of 'do' eliminate all forms
517 * that have no sideeffect. Then instead of EVAL we
518 * could call CEVAL directly here. */
519 (void) EVAL (form
, env
);
524 /* Evaluate the step expressions. */
526 SCM step_values
= SCM_EOL
;
527 for (temp_forms
= step_forms
;
528 !scm_is_null (temp_forms
);
529 temp_forms
= SCM_CDR (temp_forms
))
531 const SCM value
= EVALCAR (temp_forms
, env
);
532 step_values
= scm_cons (value
, step_values
);
534 env
= SCM_EXTEND_ENV (SCM_CAAR (env
),
539 test_result
= EVALCAR (test_form
, env
);
544 RETURN (SCM_UNSPECIFIED
);
545 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
546 goto nontoplevel_begin
;
549 case (ISYMNUM (SCM_IM_IF
)):
552 SCM test_result
= EVALCAR (x
, env
);
553 x
= SCM_CDR (x
); /* then expression */
554 if (scm_is_false (test_result
) || SCM_NILP (test_result
))
556 x
= SCM_CDR (x
); /* else expression */
558 RETURN (SCM_UNSPECIFIED
);
561 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
565 case (ISYMNUM (SCM_IM_LET
)):
568 SCM init_forms
= SCM_CADR (x
);
569 SCM init_values
= SCM_EOL
;
572 init_values
= scm_cons (EVALCAR (init_forms
, env
), init_values
);
573 init_forms
= SCM_CDR (init_forms
);
575 while (!scm_is_null (init_forms
));
576 env
= SCM_EXTEND_ENV (SCM_CAR (x
), init_values
, env
);
579 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
580 goto nontoplevel_begin
;
583 case (ISYMNUM (SCM_IM_LETREC
)):
585 env
= SCM_EXTEND_ENV (SCM_CAR (x
), undefineds
, env
);
588 SCM init_forms
= SCM_CAR (x
);
589 SCM init_values
= scm_list_1 (SCM_BOOL_T
);
590 SCM
*init_values_eol
= SCM_CDRLOC (init_values
);
591 ceval_letrec_inits (env
, init_forms
, &init_values_eol
);
592 SCM_SETCDR (SCM_CAR (env
), SCM_CDR (init_values
));
595 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
596 goto nontoplevel_begin
;
599 case (ISYMNUM (SCM_IM_LETSTAR
)):
602 SCM bindings
= SCM_CAR (x
);
603 if (!scm_is_null (bindings
))
607 SCM name
= SCM_CAR (bindings
);
608 SCM init
= SCM_CDR (bindings
);
609 env
= SCM_EXTEND_ENV (name
, EVALCAR (init
, env
), env
);
610 bindings
= SCM_CDR (init
);
612 while (!scm_is_null (bindings
));
616 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
617 goto nontoplevel_begin
;
620 case (ISYMNUM (SCM_IM_OR
)):
622 while (!scm_is_null (SCM_CDR (x
)))
624 SCM val
= EVALCAR (x
, env
);
625 if (scm_is_true (val
) && !SCM_NILP (val
))
630 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
634 case (ISYMNUM (SCM_IM_LAMBDA
)):
635 RETURN (scm_closure (SCM_CDR (x
), env
));
638 case (ISYMNUM (SCM_IM_QUOTE
)):
639 RETURN (SCM_CDR (x
));
642 case (ISYMNUM (SCM_IM_SET_X
)):
646 SCM variable
= SCM_CAR (x
);
647 if (SCM_ILOCP (variable
))
648 location
= scm_ilookup (variable
, env
);
649 else if (SCM_VARIABLEP (variable
))
650 location
= SCM_VARIABLE_LOC (variable
);
653 /* (scm_is_symbol (variable)) is known to be true */
654 variable
= lazy_memoize_variable (variable
, env
);
655 SCM_SETCAR (x
, variable
);
656 location
= SCM_VARIABLE_LOC (variable
);
659 *location
= EVALCAR (x
, env
);
661 RETURN (SCM_UNSPECIFIED
);
664 case (ISYMNUM (SCM_IM_APPLY
)):
665 /* Evaluate the procedure to be applied. */
667 proc
= EVALCAR (x
, env
);
668 PREP_APPLY (proc
, SCM_EOL
);
670 /* Evaluate the argument holding the list of arguments */
672 arg1
= EVALCAR (x
, env
);
675 /* Go here to tail-apply a procedure. PROC is the procedure and
676 * ARG1 is the list of arguments. PREP_APPLY must have been called
677 * before jumping to apply_proc. */
678 if (SCM_CLOSUREP (proc
))
680 SCM formals
= SCM_CLOSURE_FORMALS (proc
);
682 debug
.info
->a
.args
= arg1
;
684 if (SCM_UNLIKELY (scm_badargsp (formals
, arg1
)))
685 scm_wrong_num_args (proc
);
687 /* Copy argument list */
688 if (SCM_NULL_OR_NIL_P (arg1
))
689 env
= SCM_EXTEND_ENV (formals
, SCM_EOL
, SCM_ENV (proc
));
692 SCM args
= scm_list_1 (SCM_CAR (arg1
));
694 arg1
= SCM_CDR (arg1
);
695 while (!SCM_NULL_OR_NIL_P (arg1
))
697 SCM new_tail
= scm_list_1 (SCM_CAR (arg1
));
698 SCM_SETCDR (tail
, new_tail
);
700 arg1
= SCM_CDR (arg1
);
702 env
= SCM_EXTEND_ENV (formals
, args
, SCM_ENV (proc
));
705 x
= SCM_CLOSURE_BODY (proc
);
706 goto nontoplevel_begin
;
711 RETURN (SCM_APPLY (proc
, arg1
, SCM_EOL
));
715 case (ISYMNUM (SCM_IM_CONT
)):
718 SCM val
= scm_make_continuation (&first
);
726 proc
= EVALCAR (proc
, env
);
727 PREP_APPLY (proc
, scm_list_1 (arg1
));
734 case (ISYMNUM (SCM_IM_DELAY
)):
735 RETURN (scm_make_promise (scm_closure (SCM_CDR (x
), env
)));
738 /* See futures.h for a comment why futures are not enabled.
740 case (ISYMNUM (SCM_IM_FUTURE
)):
741 RETURN (scm_i_make_future (scm_closure (SCM_CDR (x
), env
)));
744 /* PLACEHOLDER for case (ISYMNUM (SCM_IM_DISPATCH)): The following
745 code (type_dispatch) is intended to be the tail of the case
746 clause for the internal macro SCM_IM_DISPATCH. Please don't
747 remove it from this location without discussing it with Mikael
748 <djurfeldt@nada.kth.se> */
750 /* The type dispatch code is duplicated below
751 * (c.f. objects.c:scm_mcache_compute_cmethod) since that
752 * cuts down execution time for type dispatch to 50%. */
753 type_dispatch
: /* inputs: x, arg1 */
754 /* Type dispatch means to determine from the types of the function
755 * arguments (i. e. the 'signature' of the call), which method from
756 * a generic function is to be called. This process of selecting
757 * the right method takes some time. To speed it up, guile uses
758 * caching: Together with the macro call to dispatch the signatures
759 * of some previous calls to that generic function from the same
760 * place are stored (in the code!) in a cache that we call the
761 * 'method cache'. This is done since it is likely, that
762 * consecutive calls to dispatch from that position in the code will
763 * have the same signature. Thus, the type dispatch works as
764 * follows: First, determine a hash value from the signature of the
765 * actual arguments. Second, use this hash value as an index to
766 * find that same signature in the method cache stored at this
767 * position in the code. If found, you have also found the
768 * corresponding method that belongs to that signature. If the
769 * signature is not found in the method cache, you have to perform a
770 * full search over all signatures stored with the generic
773 unsigned long int specializers
;
774 unsigned long int hash_value
;
775 unsigned long int cache_end_pos
;
776 unsigned long int mask
;
780 SCM z
= SCM_CDDR (x
);
781 SCM tmp
= SCM_CADR (z
);
782 specializers
= scm_to_ulong (SCM_CAR (z
));
784 /* Compute a hash value for searching the method cache. There
785 * are two variants for computing the hash value, a (rather)
786 * complicated one, and a simple one. For the complicated one
787 * explained below, tmp holds a number that is used in the
789 if (scm_is_simple_vector (tmp
))
791 /* This method of determining the hash value is much
792 * simpler: Set the hash value to zero and just perform a
793 * linear search through the method cache. */
795 mask
= (unsigned long int) ((long) -1);
797 cache_end_pos
= SCM_SIMPLE_VECTOR_LENGTH (method_cache
);
801 /* Use the signature of the actual arguments to determine
802 * the hash value. This is done as follows: Each class has
803 * an array of random numbers, that are determined when the
804 * class is created. The integer 'hashset' is an index into
805 * that array of random numbers. Now, from all classes that
806 * are part of the signature of the actual arguments, the
807 * random numbers at index 'hashset' are taken and summed
808 * up, giving the hash value. The value of 'hashset' is
809 * stored at the call to dispatch. This allows to have
810 * different 'formulas' for calculating the hash value at
811 * different places where dispatch is called. This allows
812 * to optimize the hash formula at every individual place
813 * where dispatch is called, such that hopefully the hash
814 * value that is computed will directly point to the right
815 * method in the method cache. */
816 unsigned long int hashset
= scm_to_ulong (tmp
);
817 unsigned long int counter
= specializers
+ 1;
820 while (!scm_is_null (tmp_arg
) && counter
!= 0)
822 SCM
class = scm_class_of (SCM_CAR (tmp_arg
));
823 hash_value
+= SCM_INSTANCE_HASH (class, hashset
);
824 tmp_arg
= SCM_CDR (tmp_arg
);
828 method_cache
= SCM_CADR (z
);
829 mask
= scm_to_ulong (SCM_CAR (z
));
831 cache_end_pos
= hash_value
;
836 /* Search the method cache for a method with a matching
837 * signature. Start the search at position 'hash_value'. The
838 * hashing implementation uses linear probing for conflict
839 * resolution, that is, if the signature in question is not
840 * found at the starting index in the hash table, the next table
841 * entry is tried, and so on, until in the worst case the whole
842 * cache has been searched, but still the signature has not been
847 SCM args
= arg1
; /* list of arguments */
848 z
= SCM_SIMPLE_VECTOR_REF (method_cache
, hash_value
);
849 while (!scm_is_null (args
))
851 /* More arguments than specifiers => CLASS != ENV */
852 SCM class_of_arg
= scm_class_of (SCM_CAR (args
));
853 if (!scm_is_eq (class_of_arg
, SCM_CAR (z
)))
855 args
= SCM_CDR (args
);
858 /* Fewer arguments than specifiers => CAR != CLASS */
859 if (!scm_is_pair (z
))
860 goto apply_vm_cmethod
;
861 else if (!SCM_CLASSP (SCM_CAR (z
))
862 && !scm_is_symbol (SCM_CAR (z
)))
863 goto apply_memoized_cmethod
;
865 hash_value
= (hash_value
+ 1) & mask
;
866 } while (hash_value
!= cache_end_pos
);
868 /* No appropriate method was found in the cache. */
869 z
= scm_memoize_method (x
, arg1
);
872 goto apply_memoized_cmethod
;
876 PREP_APPLY (proc
, arg1
);
879 apply_memoized_cmethod
: /* inputs: z, arg1 */
881 SCM formals
= SCM_CMETHOD_FORMALS (z
);
882 env
= SCM_EXTEND_ENV (formals
, arg1
, SCM_CMETHOD_ENV (z
));
883 x
= SCM_CMETHOD_BODY (z
);
884 goto nontoplevel_begin
;
890 case (ISYMNUM (SCM_IM_SLOT_REF
)):
893 SCM instance
= EVALCAR (x
, env
);
894 unsigned long int slot
= SCM_I_INUM (SCM_CDR (x
));
895 RETURN (SCM_PACK (SCM_STRUCT_DATA (instance
) [slot
]));
899 case (ISYMNUM (SCM_IM_SLOT_SET_X
)):
902 SCM instance
= EVALCAR (x
, env
);
903 unsigned long int slot
= SCM_I_INUM (SCM_CADR (x
));
904 SCM value
= EVALCAR (SCM_CDDR (x
), env
);
905 SCM_STRUCT_DATA (instance
) [slot
] = SCM_UNPACK (value
);
906 RETURN (SCM_UNSPECIFIED
);
912 case (ISYMNUM (SCM_IM_NIL_COND
)):
914 SCM test_form
= SCM_CDR (x
);
915 x
= SCM_CDR (test_form
);
916 while (!SCM_NULL_OR_NIL_P (x
))
918 SCM test_result
= EVALCAR (test_form
, env
);
919 if (!(scm_is_false (test_result
)
920 || SCM_NULL_OR_NIL_P (test_result
)))
922 if (scm_is_eq (SCM_CAR (x
), SCM_UNSPECIFIED
))
923 RETURN (test_result
);
924 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
929 test_form
= SCM_CDR (x
);
930 x
= SCM_CDR (test_form
);
934 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
938 #endif /* SCM_ENABLE_ELISP */
940 case (ISYMNUM (SCM_IM_BIND
)):
942 SCM vars
, exps
, vals
;
948 while (!scm_is_null (exps
))
950 vals
= scm_cons (EVALCAR (exps
, env
), vals
);
951 exps
= SCM_CDR (exps
);
954 scm_swap_bindings (vars
, vals
);
955 scm_i_set_dynwinds (scm_acons (vars
, vals
, scm_i_dynwinds ()));
957 /* Ignore all but the last evaluation result. */
958 for (x
= SCM_CDR (x
); !scm_is_null (SCM_CDR (x
)); x
= SCM_CDR (x
))
960 if (scm_is_pair (SCM_CAR (x
)))
961 CEVAL (SCM_CAR (x
), env
);
963 proc
= EVALCAR (x
, env
);
965 scm_i_set_dynwinds (SCM_CDR (scm_i_dynwinds ()));
966 scm_swap_bindings (vars
, vals
);
972 case (ISYMNUM (SCM_IM_CALL_WITH_VALUES
)):
977 producer
= EVALCAR (x
, env
);
979 proc
= EVALCAR (x
, env
); /* proc is the consumer. */
980 arg1
= SCM_APPLY (producer
, SCM_EOL
, SCM_EOL
);
981 if (SCM_VALUESP (arg1
))
983 /* The list of arguments is not copied. Rather, it is assumed
984 * that this has been done by the 'values' procedure. */
985 arg1
= scm_struct_ref (arg1
, SCM_INUM0
);
989 arg1
= scm_list_1 (arg1
);
991 PREP_APPLY (proc
, arg1
);
1002 if (SCM_VARIABLEP (SCM_CAR (x
)))
1003 proc
= SCM_VARIABLE_REF (SCM_CAR (x
));
1004 else if (SCM_ILOCP (SCM_CAR (x
)))
1005 proc
= *scm_ilookup (SCM_CAR (x
), env
);
1006 else if (scm_is_pair (SCM_CAR (x
)))
1007 proc
= CEVAL (SCM_CAR (x
), env
);
1008 else if (scm_is_symbol (SCM_CAR (x
)))
1010 SCM orig_sym
= SCM_CAR (x
);
1012 SCM
*location
= scm_lookupcar1 (x
, env
, 1);
1013 if (location
== NULL
)
1015 /* we have lost the race, start again. */
1020 if (scm_check_memoize_p
&& SCM_TRAPS_P
)
1022 SCM_CLEAR_TRACED_FRAME (debug
);
1023 SCM arg1
= scm_make_debugobj (&debug
);
1024 SCM retval
= SCM_BOOL_T
;
1026 retval
= scm_call_4 (SCM_MEMOIZE_HDLR
,
1027 scm_sym_memoize_symbol
,
1031 do something with retval?
1038 if (SCM_MACROP (proc
))
1040 SCM_SETCAR (x
, orig_sym
); /* Undo memoizing effect of
1042 handle_a_macro
: /* inputs: x, env, proc */
1044 /* Set a flag during macro expansion so that macro
1045 application frames can be deleted from the backtrace. */
1046 SCM_SET_MACROEXP (debug
);
1048 arg1
= SCM_APPLY (SCM_MACRO_CODE (proc
), x
,
1049 scm_cons (env
, scm_listofnull
));
1051 SCM_CLEAR_MACROEXP (debug
);
1053 switch (SCM_MACRO_TYPE (proc
))
1057 if (!scm_is_pair (arg1
))
1058 arg1
= scm_list_2 (SCM_IM_BEGIN
, arg1
);
1060 assert (!scm_is_eq (x
, SCM_CAR (arg1
))
1061 && !scm_is_eq (x
, SCM_CDR (arg1
)));
1064 if (!SCM_CLOSUREP (SCM_MACRO_CODE (proc
)))
1066 SCM_CRITICAL_SECTION_START
;
1067 SCM_SETCAR (x
, SCM_CAR (arg1
));
1068 SCM_SETCDR (x
, SCM_CDR (arg1
));
1069 SCM_CRITICAL_SECTION_END
;
1072 /* Prevent memoizing of debug info expression. */
1073 debug
.info
->e
.exp
= scm_cons_source (debug
.info
->e
.exp
,
1077 SCM_CRITICAL_SECTION_START
;
1078 SCM_SETCAR (x
, SCM_CAR (arg1
));
1079 SCM_SETCDR (x
, SCM_CDR (arg1
));
1080 SCM_CRITICAL_SECTION_END
;
1081 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
1083 #if SCM_ENABLE_DEPRECATED == 1
1088 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
1102 if (SCM_MACROP (proc
))
1103 goto handle_a_macro
;
1107 /* When reaching this part of the code, the following is granted: Variable x
1108 * holds the first pair of an expression of the form (<function> arg ...).
1109 * Variable proc holds the object that resulted from the evaluation of
1110 * <function>. In the following, the arguments (if any) will be evaluated,
1111 * and proc will be applied to them. If proc does not really hold a
1112 * function object, this will be signalled as an error on the scheme
1113 * level. If the number of arguments does not match the number of arguments
1114 * that are allowed to be passed to proc, also an error on the scheme level
1115 * will be signalled. */
1117 PREP_APPLY (proc
, SCM_EOL
);
1118 if (scm_is_null (SCM_CDR (x
))) {
1121 SCM_ASRTGO (!SCM_IMP (proc
), badfun
);
1122 switch (SCM_TYP7 (proc
))
1123 { /* no arguments given */
1124 case scm_tc7_subr_0
:
1125 RETURN (SCM_SUBRF (proc
) ());
1126 case scm_tc7_subr_1o
:
1127 RETURN (SCM_SUBRF (proc
) (SCM_UNDEFINED
));
1129 RETURN (SCM_SUBRF (proc
) (SCM_EOL
));
1130 case scm_tc7_rpsubr
:
1131 RETURN (SCM_BOOL_T
);
1133 RETURN (SCM_SUBRF (proc
) (SCM_UNDEFINED
, SCM_UNDEFINED
));
1135 if (!SCM_SMOB_APPLICABLE_P (proc
))
1137 RETURN (SCM_SMOB_APPLY_0 (proc
));
1140 proc
= SCM_CCLO_SUBR (proc
);
1142 debug
.info
->a
.proc
= proc
;
1143 debug
.info
->a
.args
= scm_list_1 (arg1
);
1147 proc
= SCM_PROCEDURE (proc
);
1149 debug
.info
->a
.proc
= proc
;
1151 if (!SCM_CLOSUREP (proc
))
1154 case scm_tcs_closures
:
1156 const SCM formals
= SCM_CLOSURE_FORMALS (proc
);
1157 if (SCM_UNLIKELY (scm_is_pair (formals
)))
1159 x
= SCM_CLOSURE_BODY (proc
);
1160 env
= SCM_EXTEND_ENV (formals
, SCM_EOL
, SCM_ENV (proc
));
1161 goto nontoplevel_begin
;
1163 case scm_tcs_struct
:
1164 if (SCM_OBJ_CLASS_FLAGS (proc
) & SCM_CLASSF_PURE_GENERIC
)
1166 x
= SCM_ENTITY_PROCEDURE (proc
);
1170 else if (SCM_I_OPERATORP (proc
))
1173 proc
= (SCM_I_ENTITYP (proc
)
1174 ? SCM_ENTITY_PROCEDURE (proc
)
1175 : SCM_OPERATOR_PROCEDURE (proc
));
1177 debug
.info
->a
.proc
= proc
;
1178 debug
.info
->a
.args
= scm_list_1 (arg1
);
1184 case scm_tc7_subr_1
:
1185 case scm_tc7_subr_2
:
1186 case scm_tc7_subr_2o
:
1189 case scm_tc7_subr_3
:
1190 case scm_tc7_lsubr_2
:
1192 scm_wrong_num_args (proc
);
1195 scm_misc_error (NULL
, "Wrong type to apply: ~S", scm_list_1 (proc
));
1199 /* must handle macros by here */
1201 if (SCM_LIKELY (scm_is_pair (x
)))
1202 arg1
= EVALCAR (x
, env
);
1204 scm_wrong_num_args (proc
);
1206 debug
.info
->a
.args
= scm_list_1 (arg1
);
1211 if (scm_is_null (x
))
1214 evap1
: /* inputs: proc, arg1 */
1215 SCM_ASRTGO (!SCM_IMP (proc
), badfun
);
1216 switch (SCM_TYP7 (proc
))
1217 { /* have one argument in arg1 */
1218 case scm_tc7_subr_2o
:
1219 RETURN (SCM_SUBRF (proc
) (arg1
, SCM_UNDEFINED
));
1220 case scm_tc7_subr_1
:
1221 case scm_tc7_subr_1o
:
1222 RETURN (SCM_SUBRF (proc
) (arg1
));
1224 if (SCM_I_INUMP (arg1
))
1226 RETURN (scm_from_double (SCM_DSUBRF (proc
) ((double) SCM_I_INUM (arg1
))));
1228 else if (SCM_REALP (arg1
))
1230 RETURN (scm_from_double (SCM_DSUBRF (proc
) (SCM_REAL_VALUE (arg1
))));
1232 else if (SCM_BIGP (arg1
))
1234 RETURN (scm_from_double (SCM_DSUBRF (proc
) (scm_i_big2dbl (arg1
))));
1236 else if (SCM_FRACTIONP (arg1
))
1238 RETURN (scm_from_double (SCM_DSUBRF (proc
) (scm_i_fraction2double (arg1
))));
1240 SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc
), arg1
,
1242 scm_i_symbol_chars (SCM_SNAME (proc
)));
1244 RETURN (scm_i_chase_pairs (arg1
, (scm_t_bits
) SCM_SUBRF (proc
)));
1245 case scm_tc7_rpsubr
:
1246 RETURN (SCM_BOOL_T
);
1248 RETURN (SCM_SUBRF (proc
) (arg1
, SCM_UNDEFINED
));
1251 RETURN (SCM_SUBRF (proc
) (debug
.info
->a
.args
));
1253 RETURN (SCM_SUBRF (proc
) (scm_list_1 (arg1
)));
1256 if (!SCM_SMOB_APPLICABLE_P (proc
))
1258 RETURN (SCM_SMOB_APPLY_1 (proc
, arg1
));
1262 proc
= SCM_CCLO_SUBR (proc
);
1264 debug
.info
->a
.args
= scm_cons (arg1
, debug
.info
->a
.args
);
1265 debug
.info
->a
.proc
= proc
;
1269 proc
= SCM_PROCEDURE (proc
);
1271 debug
.info
->a
.proc
= proc
;
1273 if (!SCM_CLOSUREP (proc
))
1276 case scm_tcs_closures
:
1279 const SCM formals
= SCM_CLOSURE_FORMALS (proc
);
1280 if (scm_is_null (formals
)
1281 || (scm_is_pair (formals
) && scm_is_pair (SCM_CDR (formals
))))
1283 x
= SCM_CLOSURE_BODY (proc
);
1285 env
= SCM_EXTEND_ENV (formals
,
1289 env
= SCM_EXTEND_ENV (formals
,
1293 goto nontoplevel_begin
;
1295 case scm_tcs_struct
:
1296 if (SCM_OBJ_CLASS_FLAGS (proc
) & SCM_CLASSF_PURE_GENERIC
)
1298 x
= SCM_ENTITY_PROCEDURE (proc
);
1300 arg1
= debug
.info
->a
.args
;
1302 arg1
= scm_list_1 (arg1
);
1306 else if (SCM_I_OPERATORP (proc
))
1310 proc
= (SCM_I_ENTITYP (proc
)
1311 ? SCM_ENTITY_PROCEDURE (proc
)
1312 : SCM_OPERATOR_PROCEDURE (proc
));
1314 debug
.info
->a
.args
= scm_cons (arg1
, debug
.info
->a
.args
);
1315 debug
.info
->a
.proc
= proc
;
1321 case scm_tc7_subr_2
:
1322 case scm_tc7_subr_0
:
1323 case scm_tc7_subr_3
:
1324 case scm_tc7_lsubr_2
:
1325 scm_wrong_num_args (proc
);
1330 if (SCM_LIKELY (scm_is_pair (x
)))
1331 arg2
= EVALCAR (x
, env
);
1333 scm_wrong_num_args (proc
);
1335 { /* have two or more arguments */
1337 debug
.info
->a
.args
= scm_list_2 (arg1
, arg2
);
1340 if (scm_is_null (x
)) {
1343 SCM_ASRTGO (!SCM_IMP (proc
), badfun
);
1344 switch (SCM_TYP7 (proc
))
1345 { /* have two arguments */
1346 case scm_tc7_subr_2
:
1347 case scm_tc7_subr_2o
:
1348 RETURN (SCM_SUBRF (proc
) (arg1
, arg2
));
1351 RETURN (SCM_SUBRF (proc
) (debug
.info
->a
.args
));
1353 RETURN (SCM_SUBRF (proc
) (scm_list_2 (arg1
, arg2
)));
1355 case scm_tc7_lsubr_2
:
1356 RETURN (SCM_SUBRF (proc
) (arg1
, arg2
, SCM_EOL
));
1357 case scm_tc7_rpsubr
:
1359 RETURN (SCM_SUBRF (proc
) (arg1
, arg2
));
1361 if (!SCM_SMOB_APPLICABLE_P (proc
))
1363 RETURN (SCM_SMOB_APPLY_2 (proc
, arg1
, arg2
));
1367 RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc
),
1368 scm_cons (proc
, debug
.info
->a
.args
),
1371 RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc
),
1372 scm_cons2 (proc
, arg1
,
1379 case scm_tcs_struct
:
1380 if (SCM_OBJ_CLASS_FLAGS (proc
) & SCM_CLASSF_PURE_GENERIC
)
1382 x
= SCM_ENTITY_PROCEDURE (proc
);
1384 arg1
= debug
.info
->a
.args
;
1386 arg1
= scm_list_2 (arg1
, arg2
);
1390 else if (SCM_I_OPERATORP (proc
))
1394 RETURN (SCM_APPLY (SCM_I_ENTITYP (proc
)
1395 ? SCM_ENTITY_PROCEDURE (proc
)
1396 : SCM_OPERATOR_PROCEDURE (proc
),
1397 scm_cons (proc
, debug
.info
->a
.args
),
1400 RETURN (SCM_APPLY (SCM_I_ENTITYP (proc
)
1401 ? SCM_ENTITY_PROCEDURE (proc
)
1402 : SCM_OPERATOR_PROCEDURE (proc
),
1403 scm_cons2 (proc
, arg1
,
1413 case scm_tc7_subr_0
:
1416 case scm_tc7_subr_1o
:
1417 case scm_tc7_subr_1
:
1418 case scm_tc7_subr_3
:
1419 scm_wrong_num_args (proc
);
1423 proc
= SCM_PROCEDURE (proc
);
1425 debug
.info
->a
.proc
= proc
;
1427 if (!SCM_CLOSUREP (proc
))
1430 case scm_tcs_closures
:
1433 const SCM formals
= SCM_CLOSURE_FORMALS (proc
);
1434 if (scm_is_null (formals
)
1435 || (scm_is_pair (formals
)
1436 && (scm_is_null (SCM_CDR (formals
))
1437 || (scm_is_pair (SCM_CDR (formals
))
1438 && scm_is_pair (SCM_CDDR (formals
))))))
1441 env
= SCM_EXTEND_ENV (formals
,
1445 env
= SCM_EXTEND_ENV (formals
,
1446 scm_list_2 (arg1
, arg2
),
1449 x
= SCM_CLOSURE_BODY (proc
);
1450 goto nontoplevel_begin
;
1454 if (SCM_UNLIKELY (!scm_is_pair (x
)))
1455 scm_wrong_num_args (proc
);
1457 debug
.info
->a
.args
= scm_cons2 (arg1
, arg2
,
1458 deval_args (x
, env
, proc
,
1459 SCM_CDRLOC (SCM_CDR (debug
.info
->a
.args
))));
1463 SCM_ASRTGO (!SCM_IMP (proc
), badfun
);
1464 switch (SCM_TYP7 (proc
))
1465 { /* have 3 or more arguments */
1467 case scm_tc7_subr_3
:
1468 if (!scm_is_null (SCM_CDR (x
)))
1469 scm_wrong_num_args (proc
);
1471 RETURN (SCM_SUBRF (proc
) (arg1
, arg2
,
1472 SCM_CADDR (debug
.info
->a
.args
)));
1474 arg1
= SCM_SUBRF(proc
)(arg1
, arg2
);
1475 arg2
= SCM_CDDR (debug
.info
->a
.args
);
1478 arg1
= SCM_SUBRF(proc
)(arg1
, SCM_CAR (arg2
));
1479 arg2
= SCM_CDR (arg2
);
1481 while (SCM_NIMP (arg2
));
1483 case scm_tc7_rpsubr
:
1484 if (scm_is_false (SCM_SUBRF (proc
) (arg1
, arg2
)))
1485 RETURN (SCM_BOOL_F
);
1486 arg1
= SCM_CDDR (debug
.info
->a
.args
);
1489 if (scm_is_false (SCM_SUBRF (proc
) (arg2
, SCM_CAR (arg1
))))
1490 RETURN (SCM_BOOL_F
);
1491 arg2
= SCM_CAR (arg1
);
1492 arg1
= SCM_CDR (arg1
);
1494 while (SCM_NIMP (arg1
));
1495 RETURN (SCM_BOOL_T
);
1496 case scm_tc7_lsubr_2
:
1497 RETURN (SCM_SUBRF (proc
) (arg1
, arg2
,
1498 SCM_CDDR (debug
.info
->a
.args
)));
1500 RETURN (SCM_SUBRF (proc
) (debug
.info
->a
.args
));
1502 if (!SCM_SMOB_APPLICABLE_P (proc
))
1504 RETURN (SCM_SMOB_APPLY_3 (proc
, arg1
, arg2
,
1505 SCM_CDDR (debug
.info
->a
.args
)));
1509 proc
= SCM_PROCEDURE (proc
);
1510 debug
.info
->a
.proc
= proc
;
1511 if (!SCM_CLOSUREP (proc
))
1514 case scm_tcs_closures
:
1516 const SCM formals
= SCM_CLOSURE_FORMALS (proc
);
1517 if (scm_is_null (formals
)
1518 || (scm_is_pair (formals
)
1519 && (scm_is_null (SCM_CDR (formals
))
1520 || (scm_is_pair (SCM_CDR (formals
))
1521 && scm_badargsp (SCM_CDDR (formals
), x
)))))
1523 SCM_SET_ARGSREADY (debug
);
1524 env
= SCM_EXTEND_ENV (formals
,
1527 x
= SCM_CLOSURE_BODY (proc
);
1528 goto nontoplevel_begin
;
1531 case scm_tc7_subr_3
:
1532 if (SCM_UNLIKELY (!scm_is_null (SCM_CDR (x
))))
1533 scm_wrong_num_args (proc
);
1535 RETURN (SCM_SUBRF (proc
) (arg1
, arg2
, EVALCAR (x
, env
)));
1537 arg1
= SCM_SUBRF (proc
) (arg1
, arg2
);
1540 arg1
= SCM_SUBRF(proc
)(arg1
, EVALCAR(x
, env
));
1543 while (!scm_is_null (x
));
1545 case scm_tc7_rpsubr
:
1546 if (scm_is_false (SCM_SUBRF (proc
) (arg1
, arg2
)))
1547 RETURN (SCM_BOOL_F
);
1550 arg1
= EVALCAR (x
, env
);
1551 if (scm_is_false (SCM_SUBRF (proc
) (arg2
, arg1
)))
1552 RETURN (SCM_BOOL_F
);
1556 while (!scm_is_null (x
));
1557 RETURN (SCM_BOOL_T
);
1558 case scm_tc7_lsubr_2
:
1559 RETURN (SCM_SUBRF (proc
) (arg1
, arg2
, scm_ceval_args (x
, env
, proc
)));
1561 RETURN (SCM_SUBRF (proc
) (scm_cons2 (arg1
,
1563 scm_ceval_args (x
, env
, proc
))));
1565 if (!SCM_SMOB_APPLICABLE_P (proc
))
1567 RETURN (SCM_SMOB_APPLY_3 (proc
, arg1
, arg2
,
1568 scm_ceval_args (x
, env
, proc
)));
1572 proc
= SCM_PROCEDURE (proc
);
1573 if (!SCM_CLOSUREP (proc
))
1576 case scm_tcs_closures
:
1578 const SCM formals
= SCM_CLOSURE_FORMALS (proc
);
1579 if (scm_is_null (formals
)
1580 || (scm_is_pair (formals
)
1581 && (scm_is_null (SCM_CDR (formals
))
1582 || (scm_is_pair (SCM_CDR (formals
))
1583 && scm_badargsp (SCM_CDDR (formals
), x
)))))
1585 env
= SCM_EXTEND_ENV (formals
,
1588 scm_ceval_args (x
, env
, proc
)),
1590 x
= SCM_CLOSURE_BODY (proc
);
1591 goto nontoplevel_begin
;
1594 case scm_tcs_struct
:
1595 if (SCM_OBJ_CLASS_FLAGS (proc
) & SCM_CLASSF_PURE_GENERIC
)
1598 arg1
= debug
.info
->a
.args
;
1600 arg1
= scm_cons2 (arg1
, arg2
, scm_ceval_args (x
, env
, proc
));
1602 x
= SCM_ENTITY_PROCEDURE (proc
);
1605 else if (SCM_I_OPERATORP (proc
))
1609 case scm_tc7_subr_2
:
1610 case scm_tc7_subr_1o
:
1611 case scm_tc7_subr_2o
:
1612 case scm_tc7_subr_0
:
1615 case scm_tc7_subr_1
:
1616 scm_wrong_num_args (proc
);
1624 if (scm_check_exit_p
&& SCM_TRAPS_P
)
1625 if (SCM_EXIT_FRAME_P
|| (SCM_TRACE_P
&& SCM_TRACED_FRAME_P (debug
)))
1627 SCM_CLEAR_TRACED_FRAME (debug
);
1628 arg1
= scm_make_debugobj (&debug
);
1630 arg1
= scm_call_3 (SCM_EXIT_FRAME_HDLR
, scm_sym_exit_frame
, arg1
, proc
);
1632 if (scm_is_pair (arg1
) && scm_is_eq (SCM_CAR (arg1
), sym_instead
))
1633 proc
= SCM_CDR (arg1
);
1635 scm_i_set_last_debug_frame (debug
.prev
);
1643 /* Apply a function to a list of arguments.
1645 This function is exported to the Scheme level as taking two
1646 required arguments and a tail argument, as if it were:
1647 (lambda (proc arg1 . args) ...)
1648 Thus, if you just have a list of arguments to pass to a procedure,
1649 pass the list as ARG1, and '() for ARGS. If you have some fixed
1650 args, pass the first as ARG1, then cons any remaining fixed args
1651 onto the front of your argument list, and pass that as ARGS. */
1654 SCM_APPLY (SCM proc
, SCM arg1
, SCM args
)
1657 scm_t_debug_frame debug
;
1658 scm_t_debug_info debug_vect_body
;
1659 debug
.prev
= scm_i_last_debug_frame ();
1660 debug
.status
= SCM_APPLYFRAME
;
1661 debug
.vect
= &debug_vect_body
;
1662 debug
.vect
[0].a
.proc
= proc
;
1663 debug
.vect
[0].a
.args
= SCM_EOL
;
1664 scm_i_set_last_debug_frame (&debug
);
1666 if (scm_debug_mode_p
)
1667 return scm_dapply (proc
, arg1
, args
);
1670 SCM_ASRTGO (SCM_NIMP (proc
), badproc
);
1672 /* If ARGS is the empty list, then we're calling apply with only two
1673 arguments --- ARG1 is the list of arguments for PROC. Whatever
1674 the case, futz with things so that ARG1 is the first argument to
1675 give to PROC (or SCM_UNDEFINED if no args), and ARGS contains the
1678 Setting the debug apply frame args this way is pretty messy.
1679 Perhaps we should store arg1 and args directly in the frame as
1680 received, and let scm_frame_arguments unpack them, because that's
1681 a relatively rare operation. This works for now; if the Guile
1682 developer archives are still around, see Mikael's post of
1684 if (scm_is_null (args
))
1686 if (scm_is_null (arg1
))
1688 arg1
= SCM_UNDEFINED
;
1690 debug
.vect
[0].a
.args
= SCM_EOL
;
1696 debug
.vect
[0].a
.args
= arg1
;
1698 args
= SCM_CDR (arg1
);
1699 arg1
= SCM_CAR (arg1
);
1704 args
= scm_nconc2last (args
);
1706 debug
.vect
[0].a
.args
= scm_cons (arg1
, args
);
1710 if (SCM_ENTER_FRAME_P
&& SCM_TRAPS_P
)
1712 SCM tmp
= scm_make_debugobj (&debug
);
1714 scm_call_2 (SCM_ENTER_FRAME_HDLR
, scm_sym_enter_frame
, tmp
);
1720 switch (SCM_TYP7 (proc
))
1722 case scm_tc7_subr_2o
:
1723 if (SCM_UNLIKELY (SCM_UNBNDP (arg1
)))
1724 scm_wrong_num_args (proc
);
1725 if (scm_is_null (args
))
1726 args
= SCM_UNDEFINED
;
1729 if (SCM_UNLIKELY (! scm_is_null (SCM_CDR (args
))))
1730 scm_wrong_num_args (proc
);
1731 args
= SCM_CAR (args
);
1733 RETURN (SCM_SUBRF (proc
) (arg1
, args
));
1734 case scm_tc7_subr_2
:
1735 if (SCM_UNLIKELY (scm_is_null (args
) ||
1736 !scm_is_null (SCM_CDR (args
))))
1737 scm_wrong_num_args (proc
);
1738 args
= SCM_CAR (args
);
1739 RETURN (SCM_SUBRF (proc
) (arg1
, args
));
1740 case scm_tc7_subr_0
:
1741 if (SCM_UNLIKELY (!SCM_UNBNDP (arg1
)))
1742 scm_wrong_num_args (proc
);
1744 RETURN (SCM_SUBRF (proc
) ());
1745 case scm_tc7_subr_1
:
1746 if (SCM_UNLIKELY (SCM_UNBNDP (arg1
)))
1747 scm_wrong_num_args (proc
);
1748 case scm_tc7_subr_1o
:
1749 if (SCM_UNLIKELY (!scm_is_null (args
)))
1750 scm_wrong_num_args (proc
);
1752 RETURN (SCM_SUBRF (proc
) (arg1
));
1754 if (SCM_UNLIKELY (SCM_UNBNDP (arg1
) || !scm_is_null (args
)))
1755 scm_wrong_num_args (proc
);
1756 if (SCM_I_INUMP (arg1
))
1758 RETURN (scm_from_double (SCM_DSUBRF (proc
) ((double) SCM_I_INUM (arg1
))));
1760 else if (SCM_REALP (arg1
))
1762 RETURN (scm_from_double (SCM_DSUBRF (proc
) (SCM_REAL_VALUE (arg1
))));
1764 else if (SCM_BIGP (arg1
))
1766 RETURN (scm_from_double (SCM_DSUBRF (proc
) (scm_i_big2dbl (arg1
))));
1768 else if (SCM_FRACTIONP (arg1
))
1770 RETURN (scm_from_double (SCM_DSUBRF (proc
) (scm_i_fraction2double (arg1
))));
1772 SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc
), arg1
,
1773 SCM_ARG1
, scm_i_symbol_chars (SCM_SNAME (proc
)));
1775 if (SCM_UNLIKELY (SCM_UNBNDP (arg1
) || !scm_is_null (args
)))
1776 scm_wrong_num_args (proc
);
1777 RETURN (scm_i_chase_pairs (arg1
, (scm_t_bits
) SCM_SUBRF (proc
)));
1778 case scm_tc7_subr_3
:
1779 if (SCM_UNLIKELY (scm_is_null (args
)
1780 || scm_is_null (SCM_CDR (args
))
1781 || !scm_is_null (SCM_CDDR (args
))))
1782 scm_wrong_num_args (proc
);
1784 RETURN (SCM_SUBRF (proc
) (arg1
, SCM_CAR (args
), SCM_CADR (args
)));
1787 RETURN (SCM_SUBRF (proc
) (SCM_UNBNDP (arg1
) ? SCM_EOL
: debug
.vect
[0].a
.args
));
1789 RETURN (SCM_SUBRF (proc
) (SCM_UNBNDP (arg1
) ? SCM_EOL
: scm_cons (arg1
, args
)));
1791 case scm_tc7_lsubr_2
:
1792 if (SCM_UNLIKELY (!scm_is_pair (args
)))
1793 scm_wrong_num_args (proc
);
1795 RETURN (SCM_SUBRF (proc
) (arg1
, SCM_CAR (args
), SCM_CDR (args
)));
1797 if (scm_is_null (args
))
1798 RETURN (SCM_SUBRF (proc
) (arg1
, SCM_UNDEFINED
));
1799 while (SCM_NIMP (args
))
1801 SCM_ASSERT (scm_is_pair (args
), args
, SCM_ARG2
, "apply");
1802 arg1
= SCM_SUBRF (proc
) (arg1
, SCM_CAR (args
));
1803 args
= SCM_CDR (args
);
1806 case scm_tc7_rpsubr
:
1807 if (scm_is_null (args
))
1808 RETURN (SCM_BOOL_T
);
1809 while (SCM_NIMP (args
))
1811 SCM_ASSERT (scm_is_pair (args
), args
, SCM_ARG2
, "apply");
1812 if (scm_is_false (SCM_SUBRF (proc
) (arg1
, SCM_CAR (args
))))
1813 RETURN (SCM_BOOL_F
);
1814 arg1
= SCM_CAR (args
);
1815 args
= SCM_CDR (args
);
1817 RETURN (SCM_BOOL_T
);
1818 case scm_tcs_closures
:
1820 arg1
= (SCM_UNBNDP (arg1
) ? SCM_EOL
: debug
.vect
[0].a
.args
);
1822 arg1
= (SCM_UNBNDP (arg1
) ? SCM_EOL
: scm_cons (arg1
, args
));
1824 if (SCM_UNLIKELY (scm_badargsp (SCM_CLOSURE_FORMALS (proc
), arg1
)))
1825 scm_wrong_num_args (proc
);
1827 /* Copy argument list */
1832 SCM tl
= args
= scm_cons (SCM_CAR (arg1
), SCM_UNSPECIFIED
);
1833 for (arg1
= SCM_CDR (arg1
); scm_is_pair (arg1
); arg1
= SCM_CDR (arg1
))
1835 SCM_SETCDR (tl
, scm_cons (SCM_CAR (arg1
), SCM_UNSPECIFIED
));
1838 SCM_SETCDR (tl
, arg1
);
1841 args
= SCM_EXTEND_ENV (SCM_CLOSURE_FORMALS (proc
),
1844 proc
= SCM_CLOSURE_BODY (proc
);
1846 arg1
= SCM_CDR (proc
);
1847 while (!scm_is_null (arg1
))
1849 if (SCM_IMP (SCM_CAR (proc
)))
1851 if (SCM_ISYMP (SCM_CAR (proc
)))
1853 scm_dynwind_begin (0);
1854 scm_i_dynwind_pthread_mutex_lock (&source_mutex
);
1855 /* check for race condition */
1856 if (SCM_ISYMP (SCM_CAR (proc
)))
1857 m_expand_body (proc
, args
);
1862 SCM_VALIDATE_NON_EMPTY_COMBINATION (SCM_CAR (proc
));
1865 (void) EVAL (SCM_CAR (proc
), args
);
1867 arg1
= SCM_CDR (proc
);
1869 RETURN (EVALCAR (proc
, args
));
1871 if (!SCM_SMOB_APPLICABLE_P (proc
))
1873 if (SCM_UNBNDP (arg1
))
1874 RETURN (SCM_SMOB_APPLY_0 (proc
));
1875 else if (scm_is_null (args
))
1876 RETURN (SCM_SMOB_APPLY_1 (proc
, arg1
));
1877 else if (scm_is_null (SCM_CDR (args
)))
1878 RETURN (SCM_SMOB_APPLY_2 (proc
, arg1
, SCM_CAR (args
)));
1880 RETURN (SCM_SMOB_APPLY_3 (proc
, arg1
, SCM_CAR (args
), SCM_CDR (args
)));
1883 args
= (SCM_UNBNDP(arg1
) ? SCM_EOL
: debug
.vect
[0].a
.args
);
1885 proc
= SCM_CCLO_SUBR (proc
);
1886 debug
.vect
[0].a
.proc
= proc
;
1887 debug
.vect
[0].a
.args
= scm_cons (arg1
, args
);
1889 args
= (SCM_UNBNDP(arg1
) ? SCM_EOL
: scm_cons (arg1
, args
));
1891 proc
= SCM_CCLO_SUBR (proc
);
1895 proc
= SCM_PROCEDURE (proc
);
1897 debug
.vect
[0].a
.proc
= proc
;
1900 case scm_tcs_struct
:
1901 if (SCM_OBJ_CLASS_FLAGS (proc
) & SCM_CLASSF_PURE_GENERIC
)
1904 args
= (SCM_UNBNDP(arg1
) ? SCM_EOL
: debug
.vect
[0].a
.args
);
1906 args
= (SCM_UNBNDP(arg1
) ? SCM_EOL
: scm_cons (arg1
, args
));
1908 RETURN (scm_apply_generic (proc
, args
));
1910 else if (SCM_I_OPERATORP (proc
))
1914 args
= (SCM_UNBNDP(arg1
) ? SCM_EOL
: debug
.vect
[0].a
.args
);
1916 args
= (SCM_UNBNDP(arg1
) ? SCM_EOL
: scm_cons (arg1
, args
));
1919 proc
= (SCM_I_ENTITYP (proc
)
1920 ? SCM_ENTITY_PROCEDURE (proc
)
1921 : SCM_OPERATOR_PROCEDURE (proc
));
1923 debug
.vect
[0].a
.proc
= proc
;
1924 debug
.vect
[0].a
.args
= scm_cons (arg1
, args
);
1926 if (SCM_NIMP (proc
))
1935 scm_wrong_type_arg ("apply", SCM_ARG1
, proc
);
1939 if (scm_check_exit_p
&& SCM_TRAPS_P
)
1940 if (SCM_EXIT_FRAME_P
|| (SCM_TRACE_P
&& SCM_TRACED_FRAME_P (debug
)))
1942 SCM_CLEAR_TRACED_FRAME (debug
);
1943 arg1
= scm_make_debugobj (&debug
);
1945 arg1
= scm_call_3 (SCM_EXIT_FRAME_HDLR
, scm_sym_exit_frame
, arg1
, proc
);
1947 if (scm_is_pair (arg1
) && scm_is_eq (SCM_CAR (arg1
), sym_instead
))
1948 proc
= SCM_CDR (arg1
);
1950 scm_i_set_last_debug_frame (debug
.prev
);