aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bytecode.c')
-rw-r--r--src/bytecode.c163
1 files changed, 143 insertions, 20 deletions
diff --git a/src/bytecode.c b/src/bytecode.c
index a7be8e26f27..5d94cb0fb39 100644
--- a/src/bytecode.c
+++ b/src/bytecode.c
@@ -80,9 +80,11 @@ Lisp_Object Qbyte_code_meter;
80 80
81 81
82Lisp_Object Qbytecode; 82Lisp_Object Qbytecode;
83extern Lisp_Object Qand_optional, Qand_rest;
83 84
84/* Byte codes: */ 85/* Byte codes: */
85 86
87#define Bstack_ref 0 /* Actually, Bstack_ref+0 is not implemented: use dup. */
86#define Bvarref 010 88#define Bvarref 010
87#define Bvarset 020 89#define Bvarset 020
88#define Bvarbind 030 90#define Bvarbind 030
@@ -132,7 +134,7 @@ Lisp_Object Qbytecode;
132 134
133#define Bpoint 0140 135#define Bpoint 0140
134/* Was Bmark in v17. */ 136/* Was Bmark in v17. */
135#define Bsave_current_buffer 0141 137#define Bsave_current_buffer 0141 /* Obsolete. */
136#define Bgoto_char 0142 138#define Bgoto_char 0142
137#define Binsert 0143 139#define Binsert 0143
138#define Bpoint_max 0144 140#define Bpoint_max 0144
@@ -158,7 +160,7 @@ Lisp_Object Qbytecode;
158#ifdef BYTE_CODE_SAFE 160#ifdef BYTE_CODE_SAFE
159#define Bset_mark 0163 /* this loser is no longer generated as of v18 */ 161#define Bset_mark 0163 /* this loser is no longer generated as of v18 */
160#endif 162#endif
161#define Binteractive_p 0164 /* Needed since interactive-p takes unevalled args */ 163#define Binteractive_p 0164 /* Obsolete since Emacs-24.1. */
162 164
163#define Bforward_char 0165 165#define Bforward_char 0165
164#define Bforward_word 0166 166#define Bforward_word 0166
@@ -183,16 +185,16 @@ Lisp_Object Qbytecode;
183#define Bdup 0211 185#define Bdup 0211
184 186
185#define Bsave_excursion 0212 187#define Bsave_excursion 0212
186#define Bsave_window_excursion 0213 188#define Bsave_window_excursion 0213 /* Obsolete since Emacs-24.1. */
187#define Bsave_restriction 0214 189#define Bsave_restriction 0214
188#define Bcatch 0215 190#define Bcatch 0215
189 191
190#define Bunwind_protect 0216 192#define Bunwind_protect 0216
191#define Bcondition_case 0217 193#define Bcondition_case 0217
192#define Btemp_output_buffer_setup 0220 194#define Btemp_output_buffer_setup 0220 /* Obsolete since Emacs-24.1. */
193#define Btemp_output_buffer_show 0221 195#define Btemp_output_buffer_show 0221 /* Obsolete since Emacs-24.1. */
194 196
195#define Bunbind_all 0222 197#define Bunbind_all 0222 /* Obsolete. Never used. */
196 198
197#define Bset_marker 0223 199#define Bset_marker 0223
198#define Bmatch_beginning 0224 200#define Bmatch_beginning 0224
@@ -228,6 +230,11 @@ Lisp_Object Qbytecode;
228#define BconcatN 0260 230#define BconcatN 0260
229#define BinsertN 0261 231#define BinsertN 0261
230 232
233/* Bstack_ref is code 0. */
234#define Bstack_set 0262
235#define Bstack_set2 0263
236#define BdiscardN 0266
237
231#define Bconstant 0300 238#define Bconstant 0300
232 239
233/* Whether to maintain a `top' and `bottom' field in the stack frame. */ 240/* Whether to maintain a `top' and `bottom' field in the stack frame. */
@@ -414,6 +421,21 @@ the third, MAXDEPTH, the maximum stack depth used in this function.
414If the third argument is incorrect, Emacs may crash. */) 421If the third argument is incorrect, Emacs may crash. */)
415 (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth) 422 (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth)
416{ 423{
424 return exec_byte_code (bytestr, vector, maxdepth, Qnil, 0, NULL);
425}
426
427/* Execute the byte-code in BYTESTR. VECTOR is the constant vector, and
428 MAXDEPTH is the maximum stack depth used (if MAXDEPTH is incorrect,
429 emacs may crash!). If ARGS_TEMPLATE is non-nil, it should be a lisp
430 argument list (including &rest, &optional, etc.), and ARGS, of size
431 NARGS, should be a vector of the actual arguments. The arguments in
432 ARGS are pushed on the stack according to ARGS_TEMPLATE before
433 executing BYTESTR. */
434
435Lisp_Object
436exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
437 Lisp_Object args_template, int nargs, Lisp_Object *args)
438{
417 int count = SPECPDL_INDEX (); 439 int count = SPECPDL_INDEX ();
418#ifdef BYTE_CODE_METER 440#ifdef BYTE_CODE_METER
419 int this_op = 0; 441 int this_op = 0;
@@ -473,6 +495,52 @@ If the third argument is incorrect, Emacs may crash. */)
473 stacke = stack.bottom - 1 + XFASTINT (maxdepth); 495 stacke = stack.bottom - 1 + XFASTINT (maxdepth);
474#endif 496#endif
475 497
498 if (INTEGERP (args_template))
499 {
500 int at = XINT (args_template);
501 int rest = at & 128;
502 int mandatory = at & 127;
503 int nonrest = at >> 8;
504 eassert (mandatory <= nonrest);
505 if (nargs <= nonrest)
506 {
507 int i;
508 for (i = 0 ; i < nargs; i++, args++)
509 PUSH (*args);
510 if (nargs < mandatory)
511 /* Too few arguments. */
512 Fsignal (Qwrong_number_of_arguments,
513 Fcons (Fcons (make_number (mandatory),
514 rest ? Qand_rest : make_number (nonrest)),
515 Fcons (make_number (nargs), Qnil)));
516 else
517 {
518 for (; i < nonrest; i++)
519 PUSH (Qnil);
520 if (rest)
521 PUSH (Qnil);
522 }
523 }
524 else if (rest)
525 {
526 int i;
527 for (i = 0 ; i < nonrest; i++, args++)
528 PUSH (*args);
529 PUSH (Flist (nargs - nonrest, args));
530 }
531 else
532 /* Too many arguments. */
533 Fsignal (Qwrong_number_of_arguments,
534 Fcons (Fcons (make_number (mandatory),
535 make_number (nonrest)),
536 Fcons (make_number (nargs), Qnil)));
537 }
538 else if (! NILP (args_template))
539 /* We should push some arguments on the stack. */
540 {
541 error ("Unknown args template!");
542 }
543
476 while (1) 544 while (1)
477 { 545 {
478#ifdef BYTE_CODE_SAFE 546#ifdef BYTE_CODE_SAFE
@@ -733,7 +801,7 @@ If the third argument is incorrect, Emacs may crash. */)
733 AFTER_POTENTIAL_GC (); 801 AFTER_POTENTIAL_GC ();
734 break; 802 break;
735 803
736 case Bunbind_all: 804 case Bunbind_all: /* Obsolete. Never used. */
737 /* To unbind back to the beginning of this frame. Not used yet, 805 /* To unbind back to the beginning of this frame. Not used yet,
738 but will be needed for tail-recursion elimination. */ 806 but will be needed for tail-recursion elimination. */
739 BEFORE_POTENTIAL_GC (); 807 BEFORE_POTENTIAL_GC ();
@@ -861,37 +929,43 @@ If the third argument is incorrect, Emacs may crash. */)
861 save_excursion_save ()); 929 save_excursion_save ());
862 break; 930 break;
863 931
864 case Bsave_current_buffer: 932 case Bsave_current_buffer: /* Obsolete since ??. */
865 case Bsave_current_buffer_1: 933 case Bsave_current_buffer_1:
866 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ()); 934 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
867 break; 935 break;
868 936
869 case Bsave_window_excursion: 937 case Bsave_window_excursion: /* Obsolete since 24.1. */
870 BEFORE_POTENTIAL_GC (); 938 {
871 TOP = Fsave_window_excursion (TOP); 939 register int count = SPECPDL_INDEX ();
872 AFTER_POTENTIAL_GC (); 940 record_unwind_protect (Fset_window_configuration,
873 break; 941 Fcurrent_window_configuration (Qnil));
942 BEFORE_POTENTIAL_GC ();
943 TOP = Fprogn (TOP);
944 unbind_to (count, TOP);
945 AFTER_POTENTIAL_GC ();
946 break;
947 }
874 948
875 case Bsave_restriction: 949 case Bsave_restriction:
876 record_unwind_protect (save_restriction_restore, 950 record_unwind_protect (save_restriction_restore,
877 save_restriction_save ()); 951 save_restriction_save ());
878 break; 952 break;
879 953
880 case Bcatch: 954 case Bcatch: /* FIXME: ill-suited for lexbind */
881 { 955 {
882 Lisp_Object v1; 956 Lisp_Object v1;
883 BEFORE_POTENTIAL_GC (); 957 BEFORE_POTENTIAL_GC ();
884 v1 = POP; 958 v1 = POP;
885 TOP = internal_catch (TOP, Feval, v1); 959 TOP = internal_catch (TOP, eval_sub, v1);
886 AFTER_POTENTIAL_GC (); 960 AFTER_POTENTIAL_GC ();
887 break; 961 break;
888 } 962 }
889 963
890 case Bunwind_protect: 964 case Bunwind_protect: /* FIXME: avoid closure for lexbind */
891 record_unwind_protect (Fprogn, POP); 965 record_unwind_protect (Fprogn, POP);
892 break; 966 break;
893 967
894 case Bcondition_case: 968 case Bcondition_case: /* FIXME: ill-suited for lexbind */
895 { 969 {
896 Lisp_Object handlers, body; 970 Lisp_Object handlers, body;
897 handlers = POP; 971 handlers = POP;
@@ -902,7 +976,7 @@ If the third argument is incorrect, Emacs may crash. */)
902 break; 976 break;
903 } 977 }
904 978
905 case Btemp_output_buffer_setup: 979 case Btemp_output_buffer_setup: /* Obsolete since 24.1. */
906 BEFORE_POTENTIAL_GC (); 980 BEFORE_POTENTIAL_GC ();
907 CHECK_STRING (TOP); 981 CHECK_STRING (TOP);
908 temp_output_buffer_setup (SSDATA (TOP)); 982 temp_output_buffer_setup (SSDATA (TOP));
@@ -910,7 +984,7 @@ If the third argument is incorrect, Emacs may crash. */)
910 TOP = Vstandard_output; 984 TOP = Vstandard_output;
911 break; 985 break;
912 986
913 case Btemp_output_buffer_show: 987 case Btemp_output_buffer_show: /* Obsolete since 24.1. */
914 { 988 {
915 Lisp_Object v1; 989 Lisp_Object v1;
916 BEFORE_POTENTIAL_GC (); 990 BEFORE_POTENTIAL_GC ();
@@ -1382,7 +1456,7 @@ If the third argument is incorrect, Emacs may crash. */)
1382 AFTER_POTENTIAL_GC (); 1456 AFTER_POTENTIAL_GC ();
1383 break; 1457 break;
1384 1458
1385 case Binteractive_p: 1459 case Binteractive_p: /* Obsolete since 24.1. */
1386 PUSH (Finteractive_p ()); 1460 PUSH (Finteractive_p ());
1387 break; 1461 break;
1388 1462
@@ -1672,8 +1746,57 @@ If the third argument is incorrect, Emacs may crash. */)
1672#endif 1746#endif
1673 1747
1674 case 0: 1748 case 0:
1749 /* Actually this is Bstack_ref with offset 0, but we use Bdup
1750 for that instead. */
1751 /* case Bstack_ref: */
1675 abort (); 1752 abort ();
1676 1753
1754 /* Handy byte-codes for lexical binding. */
1755 case Bstack_ref+1:
1756 case Bstack_ref+2:
1757 case Bstack_ref+3:
1758 case Bstack_ref+4:
1759 case Bstack_ref+5:
1760 {
1761 Lisp_Object *ptr = top - (op - Bstack_ref);
1762 PUSH (*ptr);
1763 break;
1764 }
1765 case Bstack_ref+6:
1766 {
1767 Lisp_Object *ptr = top - (FETCH);
1768 PUSH (*ptr);
1769 break;
1770 }
1771 case Bstack_ref+7:
1772 {
1773 Lisp_Object *ptr = top - (FETCH2);
1774 PUSH (*ptr);
1775 break;
1776 }
1777 /* stack-set-0 = discard; stack-set-1 = discard-1-preserve-tos. */
1778 case Bstack_set:
1779 {
1780 Lisp_Object *ptr = top - (FETCH);
1781 *ptr = POP;
1782 break;
1783 }
1784 case Bstack_set2:
1785 {
1786 Lisp_Object *ptr = top - (FETCH2);
1787 *ptr = POP;
1788 break;
1789 }
1790 case BdiscardN:
1791 op = FETCH;
1792 if (op & 0x80)
1793 {
1794 op &= 0x7F;
1795 top[-op] = TOP;
1796 }
1797 DISCARD (op);
1798 break;
1799
1677 case 255: 1800 case 255:
1678 default: 1801 default:
1679#ifdef BYTE_CODE_SAFE 1802#ifdef BYTE_CODE_SAFE