comparison src/eval.c @ 70:131b0175ea99 r20-0b30

Import from CVS: tag r20-0b30
author cvs
date Mon, 13 Aug 2007 09:02:59 +0200
parents 8d2a9b52c682
children 54cc21c15cbb
comparison
equal deleted inserted replaced
69:804d1389bcd6 70:131b0175ea99
38 #include "buffer.h" 38 #include "buffer.h"
39 #include "console.h" 39 #include "console.h"
40 #include "opaque.h" 40 #include "opaque.h"
41 41
42 struct backtrace *backtrace_list; 42 struct backtrace *backtrace_list;
43
44 /* Note you must always fill all of the fields in a backtrace structure
45 before pushing them on the backtrace_list. The profiling code depends
46 on this. */
47
48 #define PUSH_BACKTRACE(bt) \
49 do { (bt).next = backtrace_list; backtrace_list = &(bt); } while (0)
50
51 #define POP_BACKTRACE(bt) \
52 do { backtrace_list = (bt).next; } while (0)
53 43
54 /* This is the list of current catches (and also condition-cases). 44 /* This is the list of current catches (and also condition-cases).
55 This is a stack: the most recent catch is at the head of the 45 This is a stack: the most recent catch is at the head of the
56 list. Catches are created by declaring a 'struct catchtag' 46 list. Catches are created by declaring a 'struct catchtag'
57 locally, filling the .TAG field in with the tag, and doing 47 locally, filling the .TAG field in with the tag, and doing
226 static Lisp_Object Vcondition_handlers; 216 static Lisp_Object Vcondition_handlers;
227 217
228 /* Used for error catching purposes by throw_or_bomb_out */ 218 /* Used for error catching purposes by throw_or_bomb_out */
229 static int throw_level; 219 static int throw_level;
230 220
231 static Lisp_Object primitive_funcall (lisp_fn_t fn, int nargs, 221 static Lisp_Object primitive_funcall (Lisp_Object (*fn) (), int nargs,
232 Lisp_Object args[]); 222 Lisp_Object args[]);
233 223
234 224
235 /**********************************************************************/ 225 /**********************************************************************/
236 /* The subr and compiled-function types */ 226 /* The subr and compiled-function types */
411 401
412 static Lisp_Object 402 static Lisp_Object
413 do_debug_on_exit (Lisp_Object val) 403 do_debug_on_exit (Lisp_Object val)
414 { 404 {
415 /* This is falsified by call_debugger */ 405 /* This is falsified by call_debugger */
406 int old_debug_on_next_call = debug_on_next_call;
416 Lisp_Object v = call_debugger (list2 (Qexit, val)); 407 Lisp_Object v = call_debugger (list2 (Qexit, val));
417 408 debug_on_next_call = old_debug_on_next_call;
418 return ((!UNBOUNDP (v)) ? v : val); 409 return ((!UNBOUNDP (v)) ? v : val);
419 } 410 }
420 411
421 /* Called when debug-on-call behavior is called for. Enter the debugger 412 /* Called when debug-on-call behavior is called for. Enter the debugger
422 with the appropriate args for this. VAL is either t for a call 413 with the appropriate args for this. VAL is either t for a call
662 (if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE... 653 (if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...
663 Returns the value of THEN or the value of the last of the ELSE's. 654 Returns the value of THEN or the value of the last of the ELSE's.
664 THEN must be one expression, but ELSE... can be zero or more expressions. 655 THEN must be one expression, but ELSE... can be zero or more expressions.
665 If COND yields nil, and there are no ELSE's, the value is nil. 656 If COND yields nil, and there are no ELSE's, the value is nil.
666 */ 657 */
667 (args)) 658 (args))
668 { 659 {
669 /* This function can GC */ 660 /* This function can GC */
670 Lisp_Object cond; 661 Lisp_Object cond;
671 struct gcpro gcpro1; 662 struct gcpro gcpro1;
672 663
924 DEFUN ("while", Fwhile, 1, UNEVALLED, 0, /* 915 DEFUN ("while", Fwhile, 1, UNEVALLED, 0, /*
925 (while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat. 916 (while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.
926 The order of execution is thus TEST, BODY, TEST, BODY and so on 917 The order of execution is thus TEST, BODY, TEST, BODY and so on
927 until TEST returns nil. 918 until TEST returns nil.
928 */ 919 */
929 (args)) 920 (args))
930 { 921 {
931 /* This function can GC */ 922 /* This function can GC */
932 Lisp_Object test, body, tem; 923 Lisp_Object test, body, tem;
933 struct gcpro gcpro1, gcpro2; 924 struct gcpro gcpro1, gcpro2;
934 925
1755 1746
1756 If you want to establish an error handler that is called with the 1747 If you want to establish an error handler that is called with the
1757 Lisp stack, bindings, etc. as they were when `signal' was called, 1748 Lisp stack, bindings, etc. as they were when `signal' was called,
1758 rather than when the handler was set, use `call-with-condition-handler'. 1749 rather than when the handler was set, use `call-with-condition-handler'.
1759 */ 1750 */
1760 (args)) 1751 (args))
1761 { 1752 {
1762 /* This function can GC */ 1753 /* This function can GC */
1763 return Fcondition_case_3 (Fcar (Fcdr (args)), 1754 return Fcondition_case_3 (Fcar (Fcdr (args)),
1764 Fcar (args), 1755 Fcar (args),
1765 Fcdr (Fcdr (args))); 1756 Fcdr (Fcdr (args)));
1779 you to specify which errors are trapped). If the handler function 1770 you to specify which errors are trapped). If the handler function
1780 returns, `signal' continues as if the handler were never invoked. 1771 returns, `signal' continues as if the handler were never invoked.
1781 (It continues to look for handlers established earlier than this one, 1772 (It continues to look for handlers established earlier than this one,
1782 and invokes the standard error-handler if none is found.) 1773 and invokes the standard error-handler if none is found.)
1783 */ 1774 */
1784 (int nargs, Lisp_Object *args)) /* Note! Args side-effected! */ 1775 (int nargs, Lisp_Object *args)) /* Note! Args side-effected! */
1785 { 1776 {
1786 /* This function can GC */ 1777 /* This function can GC */
1787 int speccount = specpdl_depth_counter; 1778 int speccount = specpdl_depth_counter;
1788 Lisp_Object tem; 1779 Lisp_Object tem;
1789 1780
2061 2052
2062 static Lisp_Object 2053 static Lisp_Object
2063 call_with_suspended_errors_1 (Lisp_Object opaque_arg) 2054 call_with_suspended_errors_1 (Lisp_Object opaque_arg)
2064 { 2055 {
2065 Lisp_Object *kludgy_args = (Lisp_Object *) get_opaque_ptr (opaque_arg); 2056 Lisp_Object *kludgy_args = (Lisp_Object *) get_opaque_ptr (opaque_arg);
2066 return (primitive_funcall ((lisp_fn_t) get_opaque_ptr (kludgy_args[0]), 2057 return (primitive_funcall
2067 XINT (kludgy_args[1]), kludgy_args + 2)); 2058 ((Lisp_Object (*)()) get_opaque_ptr (kludgy_args[0]),
2059 XINT (kludgy_args[1]), kludgy_args + 2));
2068 } 2060 }
2069 2061
2070 static Lisp_Object 2062 static Lisp_Object
2071 restore_current_warning_class (Lisp_Object warning_class) 2063 restore_current_warning_class (Lisp_Object warning_class)
2072 { 2064 {
2095 Wrap this around any function in which you might want errors 2087 Wrap this around any function in which you might want errors
2096 to not be errors. 2088 to not be errors.
2097 */ 2089 */
2098 2090
2099 Lisp_Object 2091 Lisp_Object
2100 call_with_suspended_errors (lisp_fn_t fun, Lisp_Object retval, 2092 call_with_suspended_errors (Lisp_Object (*fun)(), Lisp_Object retval,
2101 Lisp_Object class, Error_behavior errb, 2093 Lisp_Object class, Error_behavior errb,
2102 int nargs, ...) 2094 int nargs, ...)
2103 { 2095 {
2104 va_list vargs; 2096 va_list vargs;
2105 int speccount; 2097 int speccount;
2590 if (CONSP (final) || SUBRP (final) || COMPILED_FUNCTIONP (final)) 2582 if (CONSP (final) || SUBRP (final) || COMPILED_FUNCTIONP (final))
2591 { 2583 {
2592 #ifdef EMACS_BTL 2584 #ifdef EMACS_BTL
2593 backtrace.id_number = 0; 2585 backtrace.id_number = 0;
2594 #endif 2586 #endif
2587 backtrace.next = backtrace_list;
2588 backtrace_list = &backtrace;
2595 backtrace.function = &Qcall_interactively; 2589 backtrace.function = &Qcall_interactively;
2596 backtrace.args = &cmd; 2590 backtrace.args = &cmd;
2597 backtrace.nargs = 1; 2591 backtrace.nargs = 1;
2598 backtrace.evalargs = 0; 2592 backtrace.evalargs = 0;
2599 backtrace.pdlcount = specpdl_depth (); 2593 backtrace.pdlcount = specpdl_depth ();
2600 backtrace.debug_on_exit = 0; 2594 backtrace.debug_on_exit = 0;
2601 PUSH_BACKTRACE (backtrace);
2602 2595
2603 final = Fcall_interactively (cmd, record, keys); 2596 final = Fcall_interactively (cmd, record, keys);
2604 2597
2605 POP_BACKTRACE (backtrace); 2598 backtrace_list = backtrace.next;
2606 return (final); 2599 return (final);
2607 } 2600 }
2608 else if (STRINGP (final) || VECTORP (final)) 2601 else if (STRINGP (final) || VECTORP (final))
2609 { 2602 {
2610 return Fexecute_kbd_macro (final, prefixarg); 2603 return Fexecute_kbd_macro (final, prefixarg);
2924 2917
2925 #ifdef EMACS_BTL 2918 #ifdef EMACS_BTL
2926 backtrace.id_number = 0; 2919 backtrace.id_number = 0;
2927 #endif 2920 #endif
2928 backtrace.pdlcount = specpdl_depth_counter; 2921 backtrace.pdlcount = specpdl_depth_counter;
2922 backtrace.next = backtrace_list;
2923 backtrace_list = &backtrace;
2929 backtrace.function = &original_fun; /* This also protects them from gc */ 2924 backtrace.function = &original_fun; /* This also protects them from gc */
2930 backtrace.args = &original_args; 2925 backtrace.args = &original_args;
2931 backtrace.nargs = UNEVALLED; 2926 backtrace.nargs = UNEVALLED;
2932 backtrace.evalargs = 1; 2927 backtrace.evalargs = 1;
2933 backtrace.debug_on_exit = 0; 2928 backtrace.debug_on_exit = 0;
2934 PUSH_BACKTRACE (backtrace);
2935 2929
2936 if (debug_on_next_call) 2930 if (debug_on_next_call)
2937 do_debug_on_call (Qt); 2931 do_debug_on_call (Qt);
2938 2932
2939 /* At this point, only original_fun and original_args 2933 /* At this point, only original_fun and original_args
2985 } 2979 }
2986 2980
2987 backtrace.args = vals; 2981 backtrace.args = vals;
2988 backtrace.nargs = nargs; 2982 backtrace.nargs = nargs;
2989 2983
2990 val = ((Lisp_Object (*) (int, Lisp_Object *)) (subr_function (subr))) 2984 val = ((subr_function (subr)) (nargs, vals));
2991 (nargs, vals);
2992 2985
2993 /* Have to duplicate this code because if the 2986 /* Have to duplicate this code because if the
2994 * debugger is called it must be in a scope in 2987 * debugger is called it must be in a scope in
2995 * which the `alloca'-ed data in vals is still valid. 2988 * which the `alloca'-ed data in vals is still valid.
2996 * (And GC-protected.) 2989 * (And GC-protected.)
3005 val = make_int (1); 2998 val = make_int (1);
3006 } 2999 }
3007 #endif 3000 #endif
3008 if (backtrace.debug_on_exit) 3001 if (backtrace.debug_on_exit)
3009 val = do_debug_on_exit (val); 3002 val = do_debug_on_exit (val);
3010 POP_BACKTRACE (backtrace); 3003 backtrace_list = backtrace.next;
3011 UNGCPRO; 3004 UNGCPRO;
3012 return (val); 3005 return (val);
3013 } 3006 }
3014 3007
3015 else 3008 else
3078 val = make_int (1); 3071 val = make_int (1);
3079 } 3072 }
3080 #endif 3073 #endif
3081 if (backtrace.debug_on_exit) 3074 if (backtrace.debug_on_exit)
3082 val = do_debug_on_exit (val); 3075 val = do_debug_on_exit (val);
3083 POP_BACKTRACE (backtrace); 3076 backtrace_list = backtrace.next;
3084 return (val); 3077 return (val);
3085 } 3078 }
3086 3079
3087 3080
3088 Lisp_Object 3081 Lisp_Object
3113 3106
3114 #ifdef EMACS_BTL 3107 #ifdef EMACS_BTL
3115 backtrace.id_number = 0; 3108 backtrace.id_number = 0;
3116 #endif 3109 #endif
3117 backtrace.pdlcount = specpdl_depth_counter; 3110 backtrace.pdlcount = specpdl_depth_counter;
3111 backtrace.next = backtrace_list;
3118 backtrace.function = &args[0]; 3112 backtrace.function = &args[0];
3119 backtrace.args = &args[1]; 3113 backtrace.args = &args[1];
3120 backtrace.nargs = nargs; 3114 backtrace.nargs = nargs;
3121 backtrace.evalargs = 0; 3115 backtrace.evalargs = 0;
3122 backtrace.debug_on_exit = 0; 3116 backtrace.debug_on_exit = 0;
3123 PUSH_BACKTRACE (backtrace); 3117 /* XEmacs: make sure this is done last so we don't get race
3118 conditions in the profiling code. */
3119 backtrace_list = &backtrace;
3124 3120
3125 if (debug_on_next_call) 3121 if (debug_on_next_call)
3126 do_debug_on_call (Qlambda); 3122 do_debug_on_call (Qlambda);
3127 3123
3128 retry: 3124 retry:
3156 list2 (fun, make_int (nargs))); 3152 list2 (fun, make_int (nargs)));
3157 } 3153 }
3158 3154
3159 if (max_args == MANY) 3155 if (max_args == MANY)
3160 { 3156 {
3161 val = ((Lisp_Object (*) (int, Lisp_Object *)) (subr_function (subr))) 3157 val = ((subr_function (subr)) (nargs, args + 1));
3162 (nargs, args + 1);
3163 } 3158 }
3164 3159
3165 else if (max_args > nargs) 3160 else if (max_args > nargs)
3166 { 3161 {
3167 Lisp_Object argvals[SUBR_MAX_ARGS]; 3162 Lisp_Object argvals[SUBR_MAX_ARGS];
3207 } 3202 }
3208 } 3203 }
3209 lisp_eval_depth--; 3204 lisp_eval_depth--;
3210 if (backtrace.debug_on_exit) 3205 if (backtrace.debug_on_exit)
3211 val = do_debug_on_exit (val); 3206 val = do_debug_on_exit (val);
3212 POP_BACKTRACE (backtrace); 3207 backtrace_list = backtrace.next;
3213 return val; 3208 return val;
3214 } 3209 }
3215 3210
3216 DEFUN ("funcall", Ffuncall, 1, MANY, 0, /* 3211 DEFUN ("funcall", Ffuncall, 1, MANY, 0, /*
3217 Call first argument as a function, passing remaining arguments to it. 3212 Call first argument as a function, passing remaining arguments to it.
3434 RETURN_UNGCPRO (Ffuncall (funcall_nargs, funcall_args)); 3429 RETURN_UNGCPRO (Ffuncall (funcall_nargs, funcall_args));
3435 } 3430 }
3436 } 3431 }
3437 3432
3438 3433
3439 /* Define proper types and argument lists simultaneously */
3440 #define PRIMITIVE_FUNCALL(n) ((Lisp_Object (*) (PRIMITIVE_FUNCALL_##n)
3441 #define PRIMITIVE_FUNCALL_0 void)) (fn)) (
3442 #define PRIMITIVE_FUNCALL_1 Lisp_Object)) (fn)) (args[0]
3443 #define PRIMITIVE_FUNCALL_2 Lisp_Object, PRIMITIVE_FUNCALL_1, args[1]
3444 #define PRIMITIVE_FUNCALL_3 Lisp_Object, PRIMITIVE_FUNCALL_2, args[2]
3445 #define PRIMITIVE_FUNCALL_4 Lisp_Object, PRIMITIVE_FUNCALL_3, args[3]
3446 #define PRIMITIVE_FUNCALL_5 Lisp_Object, PRIMITIVE_FUNCALL_4, args[4]
3447 #define PRIMITIVE_FUNCALL_6 Lisp_Object, PRIMITIVE_FUNCALL_5, args[5]
3448 #define PRIMITIVE_FUNCALL_7 Lisp_Object, PRIMITIVE_FUNCALL_6, args[6]
3449 #define PRIMITIVE_FUNCALL_8 Lisp_Object, PRIMITIVE_FUNCALL_7, args[7]
3450 #define PRIMITIVE_FUNCALL_9 Lisp_Object, PRIMITIVE_FUNCALL_8, args[8]
3451 #define PRIMITIVE_FUNCALL_10 Lisp_Object, PRIMITIVE_FUNCALL_9, args[9]
3452 #define PRIMITIVE_FUNCALL_11 Lisp_Object, PRIMITIVE_FUNCALL_10, args[10]
3453 #define PRIMITIVE_FUNCALL_12 Lisp_Object, PRIMITIVE_FUNCALL_11, args[11]
3454
3455 static Lisp_Object 3434 static Lisp_Object
3456 primitive_funcall (lisp_fn_t fn, int nargs, Lisp_Object args[]) 3435 primitive_funcall (Lisp_Object (*fn) (), int nargs, Lisp_Object args[])
3457 { 3436 {
3458 switch (nargs) 3437 switch (nargs)
3459 { 3438 {
3460 case 0: return PRIMITIVE_FUNCALL(0); 3439 case 0:
3461 case 1: return PRIMITIVE_FUNCALL(1); 3440 return ((*fn) ());
3462 case 2: return PRIMITIVE_FUNCALL(2); 3441 case 1:
3463 case 3: return PRIMITIVE_FUNCALL(3); 3442 return ((*fn) (args[0]));
3464 case 4: return PRIMITIVE_FUNCALL(4); 3443 case 2:
3465 case 5: return PRIMITIVE_FUNCALL(5); 3444 return ((*fn) (args[0], args[1]));
3466 case 6: return PRIMITIVE_FUNCALL(6); 3445 case 3:
3467 case 7: return PRIMITIVE_FUNCALL(7); 3446 return ((*fn) (args[0], args[1], args[2]));
3468 case 8: return PRIMITIVE_FUNCALL(8); 3447 case 4:
3469 case 9: return PRIMITIVE_FUNCALL(9); 3448 return ((*fn) (args[0], args[1], args[2], args[3]));
3470 case 10: return PRIMITIVE_FUNCALL(10); 3449 case 5:
3471 case 11: return PRIMITIVE_FUNCALL(11); 3450 return ((*fn) (args[0], args[1], args[2], args[3], args[4]));
3472 case 12: return PRIMITIVE_FUNCALL(12); 3451 case 6:
3473 } 3452 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5]));
3474 3453 case 7:
3475 /* Someone has created a subr that takes more arguments than is 3454 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
3476 supported by this code. We need to either rewrite the subr to 3455 args[6]));
3477 use a different argument protocol, or add more cases to this 3456 case 8:
3478 switch. */ 3457 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
3479 abort (); 3458 args[6], args[7]));
3459 case 9:
3460 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
3461 args[6], args[7], args[8]));
3462 case 10:
3463 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
3464 args[6], args[7], args[8], args[9]));
3465 case 11:
3466 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
3467 args[6], args[7], args[8], args[9], args[10]));
3468 case 12:
3469 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
3470 args[6], args[7], args[8], args[9], args[10], args[11]));
3471 default:
3472 /* Someone has created a subr that takes more arguments than
3473 is supported by this code. We need to either rewrite the
3474 subr to use a different argument protocol, or add more
3475 cases to this switch. */
3476 abort ();
3477 }
3480 return Qnil; /* suppress compiler warning */ 3478 return Qnil; /* suppress compiler warning */
3481 } 3479 }
3482 3480
3483 static Lisp_Object 3481 static Lisp_Object
3484 funcall_subr (struct Lisp_Subr *subr, Lisp_Object args[]) 3482 funcall_subr (struct Lisp_Subr *subr, Lisp_Object args[])
3611 && CONSP (XCOMPILED_FUNCTION (object)->bytecodes)) 3609 && CONSP (XCOMPILED_FUNCTION (object)->bytecodes))
3612 { 3610 {
3613 tem = read_doc_string (XCOMPILED_FUNCTION (object)->bytecodes); 3611 tem = read_doc_string (XCOMPILED_FUNCTION (object)->bytecodes);
3614 if (!CONSP (tem)) 3612 if (!CONSP (tem))
3615 signal_simple_error ("invalid lazy-loaded byte code", tem); 3613 signal_simple_error ("invalid lazy-loaded byte code", tem);
3614 /* v18 or v19 bytecode file. Need to Ebolify. */
3615 if (XCOMPILED_FUNCTION (object)->flags.ebolified
3616 && VECTORP (XCDR (tem)))
3617 ebolify_bytecode_constants (XCDR (tem));
3616 /* VERY IMPORTANT to purecopy here!!!!! 3618 /* VERY IMPORTANT to purecopy here!!!!!
3617 See load_force_doc_string_unwind. */ 3619 See load_force_doc_string_unwind. */
3618 XCOMPILED_FUNCTION (object)->bytecodes = Fpurecopy (XCAR (tem)); 3620 XCOMPILED_FUNCTION (object)->bytecodes = Fpurecopy (XCAR (tem));
3619 XCOMPILED_FUNCTION (object)->constants = Fpurecopy (XCDR (tem)); 3621 XCOMPILED_FUNCTION (object)->constants = Fpurecopy (XCDR (tem));
3620 } 3622 }