Remove interpreter’s byte stack
[emacs.git] / src / bytecode.c
blob51546ca474d906760824e0d46b960f18d612b3c5
1 /* Execution of byte code produced by bytecomp.el.
2 Copyright (C) 1985-1988, 1993, 2000-2016 Free Software Foundation,
3 Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
22 #include "lisp.h"
23 #include "blockinput.h"
24 #include "character.h"
25 #include "buffer.h"
26 #include "keyboard.h"
27 #include "syntax.h"
28 #include "window.h"
30 /* Work around GCC bug 54561. */
31 #if GNUC_PREREQ (4, 3, 0)
32 # pragma GCC diagnostic ignored "-Wclobbered"
33 #endif
35 /* Define BYTE_CODE_SAFE true to enable some minor sanity checking,
36 useful for debugging the byte compiler. It defaults to false. */
38 #ifndef BYTE_CODE_SAFE
39 # define BYTE_CODE_SAFE false
40 #endif
42 /* Define BYTE_CODE_METER to generate a byte-op usage histogram. */
43 /* #define BYTE_CODE_METER */
45 /* If BYTE_CODE_THREADED is defined, then the interpreter will be
46 indirect threaded, using GCC's computed goto extension. This code,
47 as currently implemented, is incompatible with BYTE_CODE_SAFE and
48 BYTE_CODE_METER. */
49 #if (defined __GNUC__ && !defined __STRICT_ANSI__ && !defined __CHKP__ \
50 && !BYTE_CODE_SAFE && !defined BYTE_CODE_METER)
51 #define BYTE_CODE_THREADED
52 #endif
55 #ifdef BYTE_CODE_METER
57 #define METER_2(code1, code2) \
58 (*aref_addr (AREF (Vbyte_code_meter, code1), code2))
59 #define METER_1(code) METER_2 (0, code)
61 #define METER_CODE(last_code, this_code) \
62 { \
63 if (byte_metering_on) \
64 { \
65 if (XFASTINT (METER_1 (this_code)) < MOST_POSITIVE_FIXNUM) \
66 XSETFASTINT (METER_1 (this_code), \
67 XFASTINT (METER_1 (this_code)) + 1); \
68 if (last_code \
69 && (XFASTINT (METER_2 (last_code, this_code)) \
70 < MOST_POSITIVE_FIXNUM)) \
71 XSETFASTINT (METER_2 (last_code, this_code), \
72 XFASTINT (METER_2 (last_code, this_code)) + 1); \
73 } \
76 #endif /* BYTE_CODE_METER */
79 /* Byte codes: */
81 #define BYTE_CODES \
82 DEFINE (Bstack_ref, 0) /* Actually, Bstack_ref+0 is not implemented: use dup. */ \
83 DEFINE (Bstack_ref1, 1) \
84 DEFINE (Bstack_ref2, 2) \
85 DEFINE (Bstack_ref3, 3) \
86 DEFINE (Bstack_ref4, 4) \
87 DEFINE (Bstack_ref5, 5) \
88 DEFINE (Bstack_ref6, 6) \
89 DEFINE (Bstack_ref7, 7) \
90 DEFINE (Bvarref, 010) \
91 DEFINE (Bvarref1, 011) \
92 DEFINE (Bvarref2, 012) \
93 DEFINE (Bvarref3, 013) \
94 DEFINE (Bvarref4, 014) \
95 DEFINE (Bvarref5, 015) \
96 DEFINE (Bvarref6, 016) \
97 DEFINE (Bvarref7, 017) \
98 DEFINE (Bvarset, 020) \
99 DEFINE (Bvarset1, 021) \
100 DEFINE (Bvarset2, 022) \
101 DEFINE (Bvarset3, 023) \
102 DEFINE (Bvarset4, 024) \
103 DEFINE (Bvarset5, 025) \
104 DEFINE (Bvarset6, 026) \
105 DEFINE (Bvarset7, 027) \
106 DEFINE (Bvarbind, 030) \
107 DEFINE (Bvarbind1, 031) \
108 DEFINE (Bvarbind2, 032) \
109 DEFINE (Bvarbind3, 033) \
110 DEFINE (Bvarbind4, 034) \
111 DEFINE (Bvarbind5, 035) \
112 DEFINE (Bvarbind6, 036) \
113 DEFINE (Bvarbind7, 037) \
114 DEFINE (Bcall, 040) \
115 DEFINE (Bcall1, 041) \
116 DEFINE (Bcall2, 042) \
117 DEFINE (Bcall3, 043) \
118 DEFINE (Bcall4, 044) \
119 DEFINE (Bcall5, 045) \
120 DEFINE (Bcall6, 046) \
121 DEFINE (Bcall7, 047) \
122 DEFINE (Bunbind, 050) \
123 DEFINE (Bunbind1, 051) \
124 DEFINE (Bunbind2, 052) \
125 DEFINE (Bunbind3, 053) \
126 DEFINE (Bunbind4, 054) \
127 DEFINE (Bunbind5, 055) \
128 DEFINE (Bunbind6, 056) \
129 DEFINE (Bunbind7, 057) \
131 DEFINE (Bpophandler, 060) \
132 DEFINE (Bpushconditioncase, 061) \
133 DEFINE (Bpushcatch, 062) \
135 DEFINE (Bnth, 070) \
136 DEFINE (Bsymbolp, 071) \
137 DEFINE (Bconsp, 072) \
138 DEFINE (Bstringp, 073) \
139 DEFINE (Blistp, 074) \
140 DEFINE (Beq, 075) \
141 DEFINE (Bmemq, 076) \
142 DEFINE (Bnot, 077) \
143 DEFINE (Bcar, 0100) \
144 DEFINE (Bcdr, 0101) \
145 DEFINE (Bcons, 0102) \
146 DEFINE (Blist1, 0103) \
147 DEFINE (Blist2, 0104) \
148 DEFINE (Blist3, 0105) \
149 DEFINE (Blist4, 0106) \
150 DEFINE (Blength, 0107) \
151 DEFINE (Baref, 0110) \
152 DEFINE (Baset, 0111) \
153 DEFINE (Bsymbol_value, 0112) \
154 DEFINE (Bsymbol_function, 0113) \
155 DEFINE (Bset, 0114) \
156 DEFINE (Bfset, 0115) \
157 DEFINE (Bget, 0116) \
158 DEFINE (Bsubstring, 0117) \
159 DEFINE (Bconcat2, 0120) \
160 DEFINE (Bconcat3, 0121) \
161 DEFINE (Bconcat4, 0122) \
162 DEFINE (Bsub1, 0123) \
163 DEFINE (Badd1, 0124) \
164 DEFINE (Beqlsign, 0125) \
165 DEFINE (Bgtr, 0126) \
166 DEFINE (Blss, 0127) \
167 DEFINE (Bleq, 0130) \
168 DEFINE (Bgeq, 0131) \
169 DEFINE (Bdiff, 0132) \
170 DEFINE (Bnegate, 0133) \
171 DEFINE (Bplus, 0134) \
172 DEFINE (Bmax, 0135) \
173 DEFINE (Bmin, 0136) \
174 DEFINE (Bmult, 0137) \
176 DEFINE (Bpoint, 0140) \
177 /* Was Bmark in v17. */ \
178 DEFINE (Bsave_current_buffer, 0141) /* Obsolete. */ \
179 DEFINE (Bgoto_char, 0142) \
180 DEFINE (Binsert, 0143) \
181 DEFINE (Bpoint_max, 0144) \
182 DEFINE (Bpoint_min, 0145) \
183 DEFINE (Bchar_after, 0146) \
184 DEFINE (Bfollowing_char, 0147) \
185 DEFINE (Bpreceding_char, 0150) \
186 DEFINE (Bcurrent_column, 0151) \
187 DEFINE (Bindent_to, 0152) \
188 DEFINE (Beolp, 0154) \
189 DEFINE (Beobp, 0155) \
190 DEFINE (Bbolp, 0156) \
191 DEFINE (Bbobp, 0157) \
192 DEFINE (Bcurrent_buffer, 0160) \
193 DEFINE (Bset_buffer, 0161) \
194 DEFINE (Bsave_current_buffer_1, 0162) /* Replacing Bsave_current_buffer. */ \
195 DEFINE (Binteractive_p, 0164) /* Obsolete since Emacs-24.1. */ \
197 DEFINE (Bforward_char, 0165) \
198 DEFINE (Bforward_word, 0166) \
199 DEFINE (Bskip_chars_forward, 0167) \
200 DEFINE (Bskip_chars_backward, 0170) \
201 DEFINE (Bforward_line, 0171) \
202 DEFINE (Bchar_syntax, 0172) \
203 DEFINE (Bbuffer_substring, 0173) \
204 DEFINE (Bdelete_region, 0174) \
205 DEFINE (Bnarrow_to_region, 0175) \
206 DEFINE (Bwiden, 0176) \
207 DEFINE (Bend_of_line, 0177) \
209 DEFINE (Bconstant2, 0201) \
210 DEFINE (Bgoto, 0202) \
211 DEFINE (Bgotoifnil, 0203) \
212 DEFINE (Bgotoifnonnil, 0204) \
213 DEFINE (Bgotoifnilelsepop, 0205) \
214 DEFINE (Bgotoifnonnilelsepop, 0206) \
215 DEFINE (Breturn, 0207) \
216 DEFINE (Bdiscard, 0210) \
217 DEFINE (Bdup, 0211) \
219 DEFINE (Bsave_excursion, 0212) \
220 DEFINE (Bsave_window_excursion, 0213) /* Obsolete since Emacs-24.1. */ \
221 DEFINE (Bsave_restriction, 0214) \
222 DEFINE (Bcatch, 0215) \
224 DEFINE (Bunwind_protect, 0216) \
225 DEFINE (Bcondition_case, 0217) \
226 DEFINE (Btemp_output_buffer_setup, 0220) /* Obsolete since Emacs-24.1. */ \
227 DEFINE (Btemp_output_buffer_show, 0221) /* Obsolete since Emacs-24.1. */ \
229 DEFINE (Bunbind_all, 0222) /* Obsolete. Never used. */ \
231 DEFINE (Bset_marker, 0223) \
232 DEFINE (Bmatch_beginning, 0224) \
233 DEFINE (Bmatch_end, 0225) \
234 DEFINE (Bupcase, 0226) \
235 DEFINE (Bdowncase, 0227) \
237 DEFINE (Bstringeqlsign, 0230) \
238 DEFINE (Bstringlss, 0231) \
239 DEFINE (Bequal, 0232) \
240 DEFINE (Bnthcdr, 0233) \
241 DEFINE (Belt, 0234) \
242 DEFINE (Bmember, 0235) \
243 DEFINE (Bassq, 0236) \
244 DEFINE (Bnreverse, 0237) \
245 DEFINE (Bsetcar, 0240) \
246 DEFINE (Bsetcdr, 0241) \
247 DEFINE (Bcar_safe, 0242) \
248 DEFINE (Bcdr_safe, 0243) \
249 DEFINE (Bnconc, 0244) \
250 DEFINE (Bquo, 0245) \
251 DEFINE (Brem, 0246) \
252 DEFINE (Bnumberp, 0247) \
253 DEFINE (Bintegerp, 0250) \
255 DEFINE (BRgoto, 0252) \
256 DEFINE (BRgotoifnil, 0253) \
257 DEFINE (BRgotoifnonnil, 0254) \
258 DEFINE (BRgotoifnilelsepop, 0255) \
259 DEFINE (BRgotoifnonnilelsepop, 0256) \
261 DEFINE (BlistN, 0257) \
262 DEFINE (BconcatN, 0260) \
263 DEFINE (BinsertN, 0261) \
265 /* Bstack_ref is code 0. */ \
266 DEFINE (Bstack_set, 0262) \
267 DEFINE (Bstack_set2, 0263) \
268 DEFINE (BdiscardN, 0266) \
270 DEFINE (Bconstant, 0300)
272 enum byte_code_op
274 #define DEFINE(name, value) name = value,
275 BYTE_CODES
276 #undef DEFINE
278 #if BYTE_CODE_SAFE
279 Bscan_buffer = 0153, /* No longer generated as of v18. */
280 Bset_mark = 0163, /* this loser is no longer generated as of v18 */
281 #endif
284 /* Fetch the next byte from the bytecode stream. */
286 #define FETCH (*pc++)
288 /* Fetch two bytes from the bytecode stream and make a 16-bit number
289 out of them. */
291 #define FETCH2 (op = FETCH, op + (FETCH << 8))
293 /* Push X onto the execution stack. The expression X should not
294 contain TOP, to avoid competing side effects. */
296 #define PUSH(x) (*++top = (x))
298 /* Pop a value off the execution stack. */
300 #define POP (*top--)
302 /* Discard n values from the execution stack. */
304 #define DISCARD(n) (top -= (n))
306 /* Get the value which is at the top of the execution stack, but don't
307 pop it. */
309 #define TOP (*top)
311 DEFUN ("byte-code", Fbyte_code, Sbyte_code, 3, 3, 0,
312 doc: /* Function used internally in byte-compiled code.
313 The first argument, BYTESTR, is a string of byte code;
314 the second, VECTOR, a vector of constants;
315 the third, MAXDEPTH, the maximum stack depth used in this function.
316 If the third argument is incorrect, Emacs may crash. */)
317 (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth)
319 return exec_byte_code (bytestr, vector, maxdepth, Qnil, 0, NULL);
322 static void
323 bcall0 (Lisp_Object f)
325 Ffuncall (1, &f);
328 /* Execute the byte-code in BYTESTR. VECTOR is the constant vector, and
329 MAXDEPTH is the maximum stack depth used (if MAXDEPTH is incorrect,
330 emacs may crash!). If ARGS_TEMPLATE is non-nil, it should be a lisp
331 argument list (including &rest, &optional, etc.), and ARGS, of size
332 NARGS, should be a vector of the actual arguments. The arguments in
333 ARGS are pushed on the stack according to ARGS_TEMPLATE before
334 executing BYTESTR. */
336 Lisp_Object
337 exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
338 Lisp_Object args_template, ptrdiff_t nargs, Lisp_Object *args)
340 #ifdef BYTE_CODE_METER
341 int volatile this_op = 0;
342 #endif
344 CHECK_STRING (bytestr);
345 CHECK_VECTOR (vector);
346 CHECK_NATNUM (maxdepth);
348 ptrdiff_t const_length = ASIZE (vector);
350 if (STRING_MULTIBYTE (bytestr))
351 /* BYTESTR must have been produced by Emacs 20.2 or the earlier
352 because they produced a raw 8-bit string for byte-code and now
353 such a byte-code string is loaded as multibyte while raw 8-bit
354 characters converted to multibyte form. Thus, now we must
355 convert them back to the originally intended unibyte form. */
356 bytestr = Fstring_as_unibyte (bytestr);
358 ptrdiff_t bytestr_length = SBYTES (bytestr);
359 Lisp_Object *vectorp = XVECTOR (vector)->contents;
361 unsigned char quitcounter = 1;
362 EMACS_INT stack_items = XFASTINT (maxdepth) + 1;
363 USE_SAFE_ALLOCA;
364 Lisp_Object *stack_base;
365 SAFE_ALLOCA_LISP_EXTRA (stack_base, stack_items, bytestr_length);
366 Lisp_Object *stack_lim = stack_base + stack_items;
367 Lisp_Object *top = stack_base;
368 memcpy (stack_lim, SDATA (bytestr), bytestr_length);
369 void *void_stack_lim = stack_lim;
370 unsigned char const *bytestr_data = void_stack_lim;
371 unsigned char const *pc = bytestr_data;
372 ptrdiff_t count = SPECPDL_INDEX ();
374 if (!NILP (args_template))
376 eassert (INTEGERP (args_template));
377 ptrdiff_t at = XINT (args_template);
378 bool rest = (at & 128) != 0;
379 int mandatory = at & 127;
380 ptrdiff_t nonrest = at >> 8;
381 ptrdiff_t maxargs = rest ? PTRDIFF_MAX : nonrest;
382 if (! (mandatory <= nargs && nargs <= maxargs))
383 Fsignal (Qwrong_number_of_arguments,
384 list2 (Fcons (make_number (mandatory), make_number (nonrest)),
385 make_number (nargs)));
386 ptrdiff_t pushedargs = min (nonrest, nargs);
387 for (ptrdiff_t i = 0; i < pushedargs; i++, args++)
388 PUSH (*args);
389 if (nonrest < nargs)
390 PUSH (Flist (nargs - nonrest, args));
391 else
392 for (ptrdiff_t i = nargs - rest; i < nonrest; i++)
393 PUSH (Qnil);
396 while (true)
398 int op;
399 enum handlertype type;
401 if (BYTE_CODE_SAFE && ! (stack_base <= top && top < stack_lim))
402 emacs_abort ();
404 #ifdef BYTE_CODE_METER
405 int prev_op = this_op;
406 this_op = op = FETCH;
407 METER_CODE (prev_op, op);
408 #elif !defined BYTE_CODE_THREADED
409 op = FETCH;
410 #endif
412 /* The interpreter can be compiled one of two ways: as an
413 ordinary switch-based interpreter, or as a threaded
414 interpreter. The threaded interpreter relies on GCC's
415 computed goto extension, so it is not available everywhere.
416 Threading provides a performance boost. These macros are how
417 we allow the code to be compiled both ways. */
418 #ifdef BYTE_CODE_THREADED
419 /* The CASE macro introduces an instruction's body. It is
420 either a label or a case label. */
421 #define CASE(OP) insn_ ## OP
422 /* NEXT is invoked at the end of an instruction to go to the
423 next instruction. It is either a computed goto, or a
424 plain break. */
425 #define NEXT goto *(targets[op = FETCH])
426 /* FIRST is like NEXT, but is only used at the start of the
427 interpreter body. In the switch-based interpreter it is the
428 switch, so the threaded definition must include a semicolon. */
429 #define FIRST NEXT;
430 /* Most cases are labeled with the CASE macro, above.
431 CASE_DEFAULT is one exception; it is used if the interpreter
432 being built requires a default case. The threaded
433 interpreter does not, because the dispatch table is
434 completely filled. */
435 #define CASE_DEFAULT
436 /* This introduces an instruction that is known to call abort. */
437 #define CASE_ABORT CASE (Bstack_ref): CASE (default)
438 #else
439 /* See above for the meaning of the various defines. */
440 #define CASE(OP) case OP
441 #define NEXT break
442 #define FIRST switch (op)
443 #define CASE_DEFAULT case 255: default:
444 #define CASE_ABORT case 0
445 #endif
447 #ifdef BYTE_CODE_THREADED
449 /* A convenience define that saves us a lot of typing and makes
450 the table clearer. */
451 #define LABEL(OP) [OP] = &&insn_ ## OP
453 #if GNUC_PREREQ (4, 6, 0)
454 # pragma GCC diagnostic push
455 # pragma GCC diagnostic ignored "-Woverride-init"
456 #elif defined __clang__
457 # pragma GCC diagnostic push
458 # pragma GCC diagnostic ignored "-Winitializer-overrides"
459 #endif
461 /* This is the dispatch table for the threaded interpreter. */
462 static const void *const targets[256] =
464 [0 ... (Bconstant - 1)] = &&insn_default,
465 [Bconstant ... 255] = &&insn_Bconstant,
467 #define DEFINE(name, value) LABEL (name) ,
468 BYTE_CODES
469 #undef DEFINE
472 #if GNUC_PREREQ (4, 6, 0) || defined __clang__
473 # pragma GCC diagnostic pop
474 #endif
476 #endif
479 FIRST
481 CASE (Bvarref7):
482 op = FETCH2;
483 goto varref;
485 CASE (Bvarref):
486 CASE (Bvarref1):
487 CASE (Bvarref2):
488 CASE (Bvarref3):
489 CASE (Bvarref4):
490 CASE (Bvarref5):
491 op -= Bvarref;
492 goto varref;
494 /* This seems to be the most frequently executed byte-code
495 among the Bvarref's, so avoid a goto here. */
496 CASE (Bvarref6):
497 op = FETCH;
498 varref:
500 Lisp_Object v1 = vectorp[op], v2;
501 if (!SYMBOLP (v1)
502 || XSYMBOL (v1)->redirect != SYMBOL_PLAINVAL
503 || (v2 = SYMBOL_VAL (XSYMBOL (v1)), EQ (v2, Qunbound)))
504 v2 = Fsymbol_value (v1);
505 PUSH (v2);
506 NEXT;
509 CASE (Bgotoifnil):
511 Lisp_Object v1;
512 op = FETCH2;
513 v1 = POP;
514 if (NILP (v1))
515 goto op_branch;
516 NEXT;
519 CASE (Bcar):
520 if (CONSP (TOP))
521 TOP = XCAR (TOP);
522 else if (!NILP (TOP))
523 wrong_type_argument (Qlistp, TOP);
524 NEXT;
526 CASE (Beq):
528 Lisp_Object v1 = POP;
529 TOP = EQ (v1, TOP) ? Qt : Qnil;
530 NEXT;
533 CASE (Bmemq):
535 Lisp_Object v1 = POP;
536 TOP = Fmemq (TOP, v1);
537 NEXT;
540 CASE (Bcdr):
542 if (CONSP (TOP))
543 TOP = XCDR (TOP);
544 else if (!NILP (TOP))
545 wrong_type_argument (Qlistp, TOP);
546 NEXT;
549 CASE (Bvarset):
550 CASE (Bvarset1):
551 CASE (Bvarset2):
552 CASE (Bvarset3):
553 CASE (Bvarset4):
554 CASE (Bvarset5):
555 op -= Bvarset;
556 goto varset;
558 CASE (Bvarset7):
559 op = FETCH2;
560 goto varset;
562 CASE (Bvarset6):
563 op = FETCH;
564 varset:
566 Lisp_Object sym = vectorp[op];
567 Lisp_Object val = POP;
569 /* Inline the most common case. */
570 if (SYMBOLP (sym)
571 && !EQ (val, Qunbound)
572 && !XSYMBOL (sym)->redirect
573 && !SYMBOL_TRAPPED_WRITE_P (sym))
574 SET_SYMBOL_VAL (XSYMBOL (sym), val);
575 else
576 set_internal (sym, val, Qnil, SET_INTERNAL_SET);
578 NEXT;
580 CASE (Bdup):
582 Lisp_Object v1 = TOP;
583 PUSH (v1);
584 NEXT;
587 /* ------------------ */
589 CASE (Bvarbind6):
590 op = FETCH;
591 goto varbind;
593 CASE (Bvarbind7):
594 op = FETCH2;
595 goto varbind;
597 CASE (Bvarbind):
598 CASE (Bvarbind1):
599 CASE (Bvarbind2):
600 CASE (Bvarbind3):
601 CASE (Bvarbind4):
602 CASE (Bvarbind5):
603 op -= Bvarbind;
604 varbind:
605 /* Specbind can signal and thus GC. */
606 specbind (vectorp[op], POP);
607 NEXT;
609 CASE (Bcall6):
610 op = FETCH;
611 goto docall;
613 CASE (Bcall7):
614 op = FETCH2;
615 goto docall;
617 CASE (Bcall):
618 CASE (Bcall1):
619 CASE (Bcall2):
620 CASE (Bcall3):
621 CASE (Bcall4):
622 CASE (Bcall5):
623 op -= Bcall;
624 docall:
626 DISCARD (op);
627 #ifdef BYTE_CODE_METER
628 if (byte_metering_on && SYMBOLP (TOP))
630 Lisp_Object v1 = TOP;
631 Lisp_Object v2 = Fget (v1, Qbyte_code_meter);
632 if (INTEGERP (v2)
633 && XINT (v2) < MOST_POSITIVE_FIXNUM)
635 XSETINT (v2, XINT (v2) + 1);
636 Fput (v1, Qbyte_code_meter, v2);
639 #endif
640 TOP = Ffuncall (op + 1, &TOP);
641 NEXT;
644 CASE (Bunbind6):
645 op = FETCH;
646 goto dounbind;
648 CASE (Bunbind7):
649 op = FETCH2;
650 goto dounbind;
652 CASE (Bunbind):
653 CASE (Bunbind1):
654 CASE (Bunbind2):
655 CASE (Bunbind3):
656 CASE (Bunbind4):
657 CASE (Bunbind5):
658 op -= Bunbind;
659 dounbind:
660 unbind_to (SPECPDL_INDEX () - op, Qnil);
661 NEXT;
663 CASE (Bunbind_all): /* Obsolete. Never used. */
664 /* To unbind back to the beginning of this frame. Not used yet,
665 but will be needed for tail-recursion elimination. */
666 unbind_to (count, Qnil);
667 NEXT;
669 CASE (Bgoto):
670 op = FETCH2;
671 op_branch:
672 op -= pc - bytestr_data;
673 op_relative_branch:
674 if (BYTE_CODE_SAFE
675 && ! (bytestr_data - pc <= op
676 && op < bytestr_data + bytestr_length - pc))
677 emacs_abort ();
678 quitcounter += op < 0;
679 if (!quitcounter)
681 quitcounter = 1;
682 maybe_gc ();
683 QUIT;
685 pc += op;
686 NEXT;
688 CASE (Bgotoifnonnil):
690 op = FETCH2;
691 Lisp_Object v1 = POP;
692 if (!NILP (v1))
693 goto op_branch;
694 NEXT;
697 CASE (Bgotoifnilelsepop):
698 op = FETCH2;
699 if (NILP (TOP))
700 goto op_branch;
701 DISCARD (1);
702 NEXT;
704 CASE (Bgotoifnonnilelsepop):
705 op = FETCH2;
706 if (!NILP (TOP))
707 goto op_branch;
708 DISCARD (1);
709 NEXT;
711 CASE (BRgoto):
712 op = FETCH - 128;
713 goto op_relative_branch;
715 CASE (BRgotoifnil):
717 Lisp_Object v1 = POP;
718 op = FETCH - 128;
719 if (NILP (v1))
720 goto op_relative_branch;
721 NEXT;
724 CASE (BRgotoifnonnil):
726 Lisp_Object v1 = POP;
727 op = FETCH - 128;
728 if (!NILP (v1))
729 goto op_relative_branch;
730 NEXT;
733 CASE (BRgotoifnilelsepop):
734 op = FETCH - 128;
735 if (NILP (TOP))
736 goto op_relative_branch;
737 DISCARD (1);
738 NEXT;
740 CASE (BRgotoifnonnilelsepop):
741 op = FETCH - 128;
742 if (!NILP (TOP))
743 goto op_relative_branch;
744 DISCARD (1);
745 NEXT;
747 CASE (Breturn):
748 goto exit;
750 CASE (Bdiscard):
751 DISCARD (1);
752 NEXT;
754 CASE (Bconstant2):
755 PUSH (vectorp[FETCH2]);
756 NEXT;
758 CASE (Bsave_excursion):
759 record_unwind_protect (save_excursion_restore,
760 save_excursion_save ());
761 NEXT;
763 CASE (Bsave_current_buffer): /* Obsolete since ??. */
764 CASE (Bsave_current_buffer_1):
765 record_unwind_current_buffer ();
766 NEXT;
768 CASE (Bsave_window_excursion): /* Obsolete since 24.1. */
770 ptrdiff_t count1 = SPECPDL_INDEX ();
771 record_unwind_protect (restore_window_configuration,
772 Fcurrent_window_configuration (Qnil));
773 TOP = Fprogn (TOP);
774 unbind_to (count1, TOP);
775 NEXT;
778 CASE (Bsave_restriction):
779 record_unwind_protect (save_restriction_restore,
780 save_restriction_save ());
781 NEXT;
783 CASE (Bcatch): /* Obsolete since 24.4. */
785 Lisp_Object v1 = POP;
786 TOP = internal_catch (TOP, eval_sub, v1);
787 NEXT;
790 CASE (Bpushcatch): /* New in 24.4. */
791 type = CATCHER;
792 goto pushhandler;
793 CASE (Bpushconditioncase): /* New in 24.4. */
794 type = CONDITION_CASE;
795 pushhandler:
797 struct handler *c = push_handler (POP, type);
798 c->bytecode_dest = FETCH2;
799 c->bytecode_top = top;
801 if (sys_setjmp (c->jmp))
803 struct handler *c = handlerlist;
804 top = c->bytecode_top;
805 op = c->bytecode_dest;
806 handlerlist = c->next;
807 PUSH (c->val);
808 goto op_branch;
811 NEXT;
814 CASE (Bpophandler): /* New in 24.4. */
815 handlerlist = handlerlist->next;
816 NEXT;
818 CASE (Bunwind_protect): /* FIXME: avoid closure for lexbind. */
820 Lisp_Object handler = POP;
821 /* Support for a function here is new in 24.4. */
822 record_unwind_protect (FUNCTIONP (handler) ? bcall0 : unwind_body,
823 handler);
824 NEXT;
827 CASE (Bcondition_case): /* Obsolete since 24.4. */
829 Lisp_Object handlers = POP, body = POP;
830 TOP = internal_lisp_condition_case (TOP, body, handlers);
831 NEXT;
834 CASE (Btemp_output_buffer_setup): /* Obsolete since 24.1. */
835 CHECK_STRING (TOP);
836 temp_output_buffer_setup (SSDATA (TOP));
837 TOP = Vstandard_output;
838 NEXT;
840 CASE (Btemp_output_buffer_show): /* Obsolete since 24.1. */
842 Lisp_Object v1 = POP;
843 temp_output_buffer_show (TOP);
844 TOP = v1;
845 /* pop binding of standard-output */
846 unbind_to (SPECPDL_INDEX () - 1, Qnil);
847 NEXT;
850 CASE (Bnth):
852 Lisp_Object v2 = POP, v1 = TOP;
853 CHECK_NUMBER (v1);
854 EMACS_INT n = XINT (v1);
855 immediate_quit = true;
856 while (--n >= 0 && CONSP (v2))
857 v2 = XCDR (v2);
858 immediate_quit = false;
859 TOP = CAR (v2);
860 NEXT;
863 CASE (Bsymbolp):
864 TOP = SYMBOLP (TOP) ? Qt : Qnil;
865 NEXT;
867 CASE (Bconsp):
868 TOP = CONSP (TOP) ? Qt : Qnil;
869 NEXT;
871 CASE (Bstringp):
872 TOP = STRINGP (TOP) ? Qt : Qnil;
873 NEXT;
875 CASE (Blistp):
876 TOP = CONSP (TOP) || NILP (TOP) ? Qt : Qnil;
877 NEXT;
879 CASE (Bnot):
880 TOP = NILP (TOP) ? Qt : Qnil;
881 NEXT;
883 CASE (Bcons):
885 Lisp_Object v1 = POP;
886 TOP = Fcons (TOP, v1);
887 NEXT;
890 CASE (Blist1):
891 TOP = list1 (TOP);
892 NEXT;
894 CASE (Blist2):
896 Lisp_Object v1 = POP;
897 TOP = list2 (TOP, v1);
898 NEXT;
901 CASE (Blist3):
902 DISCARD (2);
903 TOP = Flist (3, &TOP);
904 NEXT;
906 CASE (Blist4):
907 DISCARD (3);
908 TOP = Flist (4, &TOP);
909 NEXT;
911 CASE (BlistN):
912 op = FETCH;
913 DISCARD (op - 1);
914 TOP = Flist (op, &TOP);
915 NEXT;
917 CASE (Blength):
918 TOP = Flength (TOP);
919 NEXT;
921 CASE (Baref):
923 Lisp_Object v1 = POP;
924 TOP = Faref (TOP, v1);
925 NEXT;
928 CASE (Baset):
930 Lisp_Object v2 = POP, v1 = POP;
931 TOP = Faset (TOP, v1, v2);
932 NEXT;
935 CASE (Bsymbol_value):
936 TOP = Fsymbol_value (TOP);
937 NEXT;
939 CASE (Bsymbol_function):
940 TOP = Fsymbol_function (TOP);
941 NEXT;
943 CASE (Bset):
945 Lisp_Object v1 = POP;
946 TOP = Fset (TOP, v1);
947 NEXT;
950 CASE (Bfset):
952 Lisp_Object v1 = POP;
953 TOP = Ffset (TOP, v1);
954 NEXT;
957 CASE (Bget):
959 Lisp_Object v1 = POP;
960 TOP = Fget (TOP, v1);
961 NEXT;
964 CASE (Bsubstring):
966 Lisp_Object v2 = POP, v1 = POP;
967 TOP = Fsubstring (TOP, v1, v2);
968 NEXT;
971 CASE (Bconcat2):
972 DISCARD (1);
973 TOP = Fconcat (2, &TOP);
974 NEXT;
976 CASE (Bconcat3):
977 DISCARD (2);
978 TOP = Fconcat (3, &TOP);
979 NEXT;
981 CASE (Bconcat4):
982 DISCARD (3);
983 TOP = Fconcat (4, &TOP);
984 NEXT;
986 CASE (BconcatN):
987 op = FETCH;
988 DISCARD (op - 1);
989 TOP = Fconcat (op, &TOP);
990 NEXT;
992 CASE (Bsub1):
993 TOP = INTEGERP (TOP) ? make_number (XINT (TOP) - 1) : Fsub1 (TOP);
994 NEXT;
996 CASE (Badd1):
997 TOP = INTEGERP (TOP) ? make_number (XINT (TOP) + 1) : Fadd1 (TOP);
998 NEXT;
1000 CASE (Beqlsign):
1002 Lisp_Object v2 = POP, v1 = TOP;
1003 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v1);
1004 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v2);
1005 bool equal;
1006 if (FLOATP (v1) || FLOATP (v2))
1008 double f1 = FLOATP (v1) ? XFLOAT_DATA (v1) : XINT (v1);
1009 double f2 = FLOATP (v2) ? XFLOAT_DATA (v2) : XINT (v2);
1010 equal = f1 == f2;
1012 else
1013 equal = XINT (v1) == XINT (v2);
1014 TOP = equal ? Qt : Qnil;
1015 NEXT;
1018 CASE (Bgtr):
1020 Lisp_Object v1 = POP;
1021 TOP = arithcompare (TOP, v1, ARITH_GRTR);
1022 NEXT;
1025 CASE (Blss):
1027 Lisp_Object v1 = POP;
1028 TOP = arithcompare (TOP, v1, ARITH_LESS);
1029 NEXT;
1032 CASE (Bleq):
1034 Lisp_Object v1 = POP;
1035 TOP = arithcompare (TOP, v1, ARITH_LESS_OR_EQUAL);
1036 NEXT;
1039 CASE (Bgeq):
1041 Lisp_Object v1 = POP;
1042 TOP = arithcompare (TOP, v1, ARITH_GRTR_OR_EQUAL);
1043 NEXT;
1046 CASE (Bdiff):
1047 DISCARD (1);
1048 TOP = Fminus (2, &TOP);
1049 NEXT;
1051 CASE (Bnegate):
1052 TOP = INTEGERP (TOP) ? make_number (- XINT (TOP)) : Fminus (1, &TOP);
1053 NEXT;
1055 CASE (Bplus):
1056 DISCARD (1);
1057 TOP = Fplus (2, &TOP);
1058 NEXT;
1060 CASE (Bmax):
1061 DISCARD (1);
1062 TOP = Fmax (2, &TOP);
1063 NEXT;
1065 CASE (Bmin):
1066 DISCARD (1);
1067 TOP = Fmin (2, &TOP);
1068 NEXT;
1070 CASE (Bmult):
1071 DISCARD (1);
1072 TOP = Ftimes (2, &TOP);
1073 NEXT;
1075 CASE (Bquo):
1076 DISCARD (1);
1077 TOP = Fquo (2, &TOP);
1078 NEXT;
1080 CASE (Brem):
1082 Lisp_Object v1 = POP;
1083 TOP = Frem (TOP, v1);
1084 NEXT;
1087 CASE (Bpoint):
1088 PUSH (make_natnum (PT));
1089 NEXT;
1091 CASE (Bgoto_char):
1092 TOP = Fgoto_char (TOP);
1093 NEXT;
1095 CASE (Binsert):
1096 TOP = Finsert (1, &TOP);
1097 NEXT;
1099 CASE (BinsertN):
1100 op = FETCH;
1101 DISCARD (op - 1);
1102 TOP = Finsert (op, &TOP);
1103 NEXT;
1105 CASE (Bpoint_max):
1107 Lisp_Object v1;
1108 XSETFASTINT (v1, ZV);
1109 PUSH (v1);
1110 NEXT;
1113 CASE (Bpoint_min):
1114 PUSH (make_natnum (BEGV));
1115 NEXT;
1117 CASE (Bchar_after):
1118 TOP = Fchar_after (TOP);
1119 NEXT;
1121 CASE (Bfollowing_char):
1122 PUSH (Ffollowing_char ());
1123 NEXT;
1125 CASE (Bpreceding_char):
1126 PUSH (Fprevious_char ());
1127 NEXT;
1129 CASE (Bcurrent_column):
1130 PUSH (make_natnum (current_column ()));
1131 NEXT;
1133 CASE (Bindent_to):
1134 TOP = Findent_to (TOP, Qnil);
1135 NEXT;
1137 CASE (Beolp):
1138 PUSH (Feolp ());
1139 NEXT;
1141 CASE (Beobp):
1142 PUSH (Feobp ());
1143 NEXT;
1145 CASE (Bbolp):
1146 PUSH (Fbolp ());
1147 NEXT;
1149 CASE (Bbobp):
1150 PUSH (Fbobp ());
1151 NEXT;
1153 CASE (Bcurrent_buffer):
1154 PUSH (Fcurrent_buffer ());
1155 NEXT;
1157 CASE (Bset_buffer):
1158 TOP = Fset_buffer (TOP);
1159 NEXT;
1161 CASE (Binteractive_p): /* Obsolete since 24.1. */
1162 PUSH (call0 (intern ("interactive-p")));
1163 NEXT;
1165 CASE (Bforward_char):
1166 TOP = Fforward_char (TOP);
1167 NEXT;
1169 CASE (Bforward_word):
1170 TOP = Fforward_word (TOP);
1171 NEXT;
1173 CASE (Bskip_chars_forward):
1175 Lisp_Object v1 = POP;
1176 TOP = Fskip_chars_forward (TOP, v1);
1177 NEXT;
1180 CASE (Bskip_chars_backward):
1182 Lisp_Object v1 = POP;
1183 TOP = Fskip_chars_backward (TOP, v1);
1184 NEXT;
1187 CASE (Bforward_line):
1188 TOP = Fforward_line (TOP);
1189 NEXT;
1191 CASE (Bchar_syntax):
1193 CHECK_CHARACTER (TOP);
1194 int c = XFASTINT (TOP);
1195 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1196 MAKE_CHAR_MULTIBYTE (c);
1197 XSETFASTINT (TOP, syntax_code_spec[SYNTAX (c)]);
1199 NEXT;
1201 CASE (Bbuffer_substring):
1203 Lisp_Object v1 = POP;
1204 TOP = Fbuffer_substring (TOP, v1);
1205 NEXT;
1208 CASE (Bdelete_region):
1210 Lisp_Object v1 = POP;
1211 TOP = Fdelete_region (TOP, v1);
1212 NEXT;
1215 CASE (Bnarrow_to_region):
1217 Lisp_Object v1 = POP;
1218 TOP = Fnarrow_to_region (TOP, v1);
1219 NEXT;
1222 CASE (Bwiden):
1223 PUSH (Fwiden ());
1224 NEXT;
1226 CASE (Bend_of_line):
1227 TOP = Fend_of_line (TOP);
1228 NEXT;
1230 CASE (Bset_marker):
1232 Lisp_Object v2 = POP, v1 = POP;
1233 TOP = Fset_marker (TOP, v1, v2);
1234 NEXT;
1237 CASE (Bmatch_beginning):
1238 TOP = Fmatch_beginning (TOP);
1239 NEXT;
1241 CASE (Bmatch_end):
1242 TOP = Fmatch_end (TOP);
1243 NEXT;
1245 CASE (Bupcase):
1246 TOP = Fupcase (TOP);
1247 NEXT;
1249 CASE (Bdowncase):
1250 TOP = Fdowncase (TOP);
1251 NEXT;
1253 CASE (Bstringeqlsign):
1255 Lisp_Object v1 = POP;
1256 TOP = Fstring_equal (TOP, v1);
1257 NEXT;
1260 CASE (Bstringlss):
1262 Lisp_Object v1 = POP;
1263 TOP = Fstring_lessp (TOP, v1);
1264 NEXT;
1267 CASE (Bequal):
1269 Lisp_Object v1 = POP;
1270 TOP = Fequal (TOP, v1);
1271 NEXT;
1274 CASE (Bnthcdr):
1276 Lisp_Object v1 = POP;
1277 TOP = Fnthcdr (TOP, v1);
1278 NEXT;
1281 CASE (Belt):
1283 if (CONSP (TOP))
1285 /* Exchange args and then do nth. */
1286 Lisp_Object v2 = POP, v1 = TOP;
1287 CHECK_NUMBER (v2);
1288 EMACS_INT n = XINT (v2);
1289 immediate_quit = true;
1290 while (--n >= 0 && CONSP (v1))
1291 v1 = XCDR (v1);
1292 immediate_quit = false;
1293 TOP = CAR (v1);
1295 else
1297 Lisp_Object v1 = POP;
1298 TOP = Felt (TOP, v1);
1300 NEXT;
1303 CASE (Bmember):
1305 Lisp_Object v1 = POP;
1306 TOP = Fmember (TOP, v1);
1307 NEXT;
1310 CASE (Bassq):
1312 Lisp_Object v1 = POP;
1313 TOP = Fassq (TOP, v1);
1314 NEXT;
1317 CASE (Bnreverse):
1318 TOP = Fnreverse (TOP);
1319 NEXT;
1321 CASE (Bsetcar):
1323 Lisp_Object v1 = POP;
1324 TOP = Fsetcar (TOP, v1);
1325 NEXT;
1328 CASE (Bsetcdr):
1330 Lisp_Object v1 = POP;
1331 TOP = Fsetcdr (TOP, v1);
1332 NEXT;
1335 CASE (Bcar_safe):
1336 TOP = CAR_SAFE (TOP);
1337 NEXT;
1339 CASE (Bcdr_safe):
1340 TOP = CDR_SAFE (TOP);
1341 NEXT;
1343 CASE (Bnconc):
1344 DISCARD (1);
1345 TOP = Fnconc (2, &TOP);
1346 NEXT;
1348 CASE (Bnumberp):
1349 TOP = NUMBERP (TOP) ? Qt : Qnil;
1350 NEXT;
1352 CASE (Bintegerp):
1353 TOP = INTEGERP (TOP) ? Qt : Qnil;
1354 NEXT;
1356 #if BYTE_CODE_SAFE
1357 /* These are intentionally written using 'case' syntax,
1358 because they are incompatible with the threaded
1359 interpreter. */
1361 case Bset_mark:
1362 error ("set-mark is an obsolete bytecode");
1363 break;
1364 case Bscan_buffer:
1365 error ("scan-buffer is an obsolete bytecode");
1366 break;
1367 #endif
1369 CASE_ABORT:
1370 /* Actually this is Bstack_ref with offset 0, but we use Bdup
1371 for that instead. */
1372 /* CASE (Bstack_ref): */
1373 call3 (Qerror,
1374 build_string ("Invalid byte opcode: op=%s, ptr=%d"),
1375 make_number (op),
1376 make_number (pc - 1 - bytestr_data));
1378 /* Handy byte-codes for lexical binding. */
1379 CASE (Bstack_ref1):
1380 CASE (Bstack_ref2):
1381 CASE (Bstack_ref3):
1382 CASE (Bstack_ref4):
1383 CASE (Bstack_ref5):
1385 Lisp_Object v1 = top[Bstack_ref - op];
1386 PUSH (v1);
1387 NEXT;
1389 CASE (Bstack_ref6):
1391 Lisp_Object v1 = top[- FETCH];
1392 PUSH (v1);
1393 NEXT;
1395 CASE (Bstack_ref7):
1397 Lisp_Object v1 = top[- FETCH2];
1398 PUSH (v1);
1399 NEXT;
1401 CASE (Bstack_set):
1402 /* stack-set-0 = discard; stack-set-1 = discard-1-preserve-tos. */
1404 Lisp_Object *ptr = top - FETCH;
1405 *ptr = POP;
1406 NEXT;
1408 CASE (Bstack_set2):
1410 Lisp_Object *ptr = top - FETCH2;
1411 *ptr = POP;
1412 NEXT;
1414 CASE (BdiscardN):
1415 op = FETCH;
1416 if (op & 0x80)
1418 op &= 0x7F;
1419 top[-op] = TOP;
1421 DISCARD (op);
1422 NEXT;
1424 CASE_DEFAULT
1425 CASE (Bconstant):
1426 if (BYTE_CODE_SAFE
1427 && ! (Bconstant <= op && op < Bconstant + const_length))
1428 emacs_abort ();
1429 PUSH (vectorp[op - Bconstant]);
1430 NEXT;
1434 exit:
1436 /* Binds and unbinds are supposed to be compiled balanced. */
1437 if (SPECPDL_INDEX () != count)
1439 if (SPECPDL_INDEX () > count)
1440 unbind_to (count, Qnil);
1441 error ("binding stack not balanced (serious byte compiler bug)");
1444 Lisp_Object result = TOP;
1445 SAFE_FREE ();
1446 return result;
1449 /* `args_template' has the same meaning as in exec_byte_code() above. */
1450 Lisp_Object
1451 get_byte_code_arity (Lisp_Object args_template)
1453 eassert (NATNUMP (args_template));
1454 EMACS_INT at = XINT (args_template);
1455 bool rest = (at & 128) != 0;
1456 int mandatory = at & 127;
1457 EMACS_INT nonrest = at >> 8;
1459 return Fcons (make_number (mandatory),
1460 rest ? Qmany : make_number (nonrest));
1463 void
1464 syms_of_bytecode (void)
1466 defsubr (&Sbyte_code);
1468 #ifdef BYTE_CODE_METER
1470 DEFVAR_LISP ("byte-code-meter", Vbyte_code_meter,
1471 doc: /* A vector of vectors which holds a histogram of byte-code usage.
1472 \(aref (aref byte-code-meter 0) CODE) indicates how many times the byte
1473 opcode CODE has been executed.
1474 \(aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,
1475 indicates how many times the byte opcodes CODE1 and CODE2 have been
1476 executed in succession. */);
1478 DEFVAR_BOOL ("byte-metering-on", byte_metering_on,
1479 doc: /* If non-nil, keep profiling information on byte code usage.
1480 The variable byte-code-meter indicates how often each byte opcode is used.
1481 If a symbol has a property named `byte-code-meter' whose value is an
1482 integer, it is incremented each time that symbol's function is called. */);
1484 byte_metering_on = false;
1485 Vbyte_code_meter = Fmake_vector (make_number (256), make_number (0));
1486 DEFSYM (Qbyte_code_meter, "byte-code-meter");
1488 int i = 256;
1489 while (i--)
1490 ASET (Vbyte_code_meter, i,
1491 Fmake_vector (make_number (256), make_number (0)));
1493 #endif