* etc/AUTHORS: Update.
[emacs.git] / src / bytecode.c
bloba5c7576269f40910224e7227cdf7621a8abab37a
1 /* Execution of byte code produced by bytecomp.el.
2 Copyright (C) 1985-1988, 1993, 2000-2019 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 <https://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 (Bswitch, 0267) \
272 DEFINE (Bconstant, 0300)
274 enum byte_code_op
276 #define DEFINE(name, value) name = value,
277 BYTE_CODES
278 #undef DEFINE
280 #if BYTE_CODE_SAFE
281 Bscan_buffer = 0153, /* No longer generated as of v18. */
282 Bset_mark = 0163, /* this loser is no longer generated as of v18 */
283 #endif
286 /* Fetch the next byte from the bytecode stream. */
288 #define FETCH (*pc++)
290 /* Fetch two bytes from the bytecode stream and make a 16-bit number
291 out of them. */
293 #define FETCH2 (op = FETCH, op + (FETCH << 8))
295 /* Push X onto the execution stack. The expression X should not
296 contain TOP, to avoid competing side effects. */
298 #define PUSH(x) (*++top = (x))
300 /* Pop a value off the execution stack. */
302 #define POP (*top--)
304 /* Discard n values from the execution stack. */
306 #define DISCARD(n) (top -= (n))
308 /* Get the value which is at the top of the execution stack, but don't
309 pop it. */
311 #define TOP (*top)
313 DEFUN ("byte-code", Fbyte_code, Sbyte_code, 3, 3, 0,
314 doc: /* Function used internally in byte-compiled code.
315 The first argument, BYTESTR, is a string of byte code;
316 the second, VECTOR, a vector of constants;
317 the third, MAXDEPTH, the maximum stack depth used in this function.
318 If the third argument is incorrect, Emacs may crash. */)
319 (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth)
321 return exec_byte_code (bytestr, vector, maxdepth, Qnil, 0, NULL);
324 static void
325 bcall0 (Lisp_Object f)
327 Ffuncall (1, &f);
330 /* Execute the byte-code in BYTESTR. VECTOR is the constant vector, and
331 MAXDEPTH is the maximum stack depth used (if MAXDEPTH is incorrect,
332 emacs may crash!). If ARGS_TEMPLATE is non-nil, it should be a lisp
333 argument list (including &rest, &optional, etc.), and ARGS, of size
334 NARGS, should be a vector of the actual arguments. The arguments in
335 ARGS are pushed on the stack according to ARGS_TEMPLATE before
336 executing BYTESTR. */
338 Lisp_Object
339 exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
340 Lisp_Object args_template, ptrdiff_t nargs, Lisp_Object *args)
342 #ifdef BYTE_CODE_METER
343 int volatile this_op = 0;
344 #endif
346 CHECK_STRING (bytestr);
347 CHECK_VECTOR (vector);
348 CHECK_NATNUM (maxdepth);
350 ptrdiff_t const_length = ASIZE (vector);
352 if (STRING_MULTIBYTE (bytestr))
353 /* BYTESTR must have been produced by Emacs 20.2 or the earlier
354 because they produced a raw 8-bit string for byte-code and now
355 such a byte-code string is loaded as multibyte while raw 8-bit
356 characters converted to multibyte form. Thus, now we must
357 convert them back to the originally intended unibyte form. */
358 bytestr = Fstring_as_unibyte (bytestr);
360 ptrdiff_t bytestr_length = SBYTES (bytestr);
361 Lisp_Object *vectorp = XVECTOR (vector)->contents;
363 unsigned char quitcounter = 1;
364 EMACS_INT stack_items = XFASTINT (maxdepth) + 1;
365 USE_SAFE_ALLOCA;
366 Lisp_Object *stack_base;
367 SAFE_ALLOCA_LISP_EXTRA (stack_base, stack_items, bytestr_length);
368 Lisp_Object *stack_lim = stack_base + stack_items;
369 Lisp_Object *top = stack_base;
370 *top = vector; /* Ensure VECTOR survives GC (Bug#33014). */
371 memcpy (stack_lim, SDATA (bytestr), bytestr_length);
372 void *void_stack_lim = stack_lim;
373 unsigned char const *bytestr_data = void_stack_lim;
374 unsigned char const *pc = bytestr_data;
375 ptrdiff_t count = SPECPDL_INDEX ();
377 if (!NILP (args_template))
379 eassert (INTEGERP (args_template));
380 ptrdiff_t at = XINT (args_template);
381 bool rest = (at & 128) != 0;
382 int mandatory = at & 127;
383 ptrdiff_t nonrest = at >> 8;
384 ptrdiff_t maxargs = rest ? PTRDIFF_MAX : nonrest;
385 if (! (mandatory <= nargs && nargs <= maxargs))
386 Fsignal (Qwrong_number_of_arguments,
387 list2 (Fcons (make_number (mandatory), make_number (nonrest)),
388 make_number (nargs)));
389 ptrdiff_t pushedargs = min (nonrest, nargs);
390 for (ptrdiff_t i = 0; i < pushedargs; i++, args++)
391 PUSH (*args);
392 if (nonrest < nargs)
393 PUSH (Flist (nargs - nonrest, args));
394 else
395 for (ptrdiff_t i = nargs - rest; i < nonrest; i++)
396 PUSH (Qnil);
399 while (true)
401 int op;
402 enum handlertype type;
404 if (BYTE_CODE_SAFE && ! (stack_base <= top && top < stack_lim))
405 emacs_abort ();
407 #ifdef BYTE_CODE_METER
408 int prev_op = this_op;
409 this_op = op = FETCH;
410 METER_CODE (prev_op, op);
411 #elif !defined BYTE_CODE_THREADED
412 op = FETCH;
413 #endif
415 /* The interpreter can be compiled one of two ways: as an
416 ordinary switch-based interpreter, or as a threaded
417 interpreter. The threaded interpreter relies on GCC's
418 computed goto extension, so it is not available everywhere.
419 Threading provides a performance boost. These macros are how
420 we allow the code to be compiled both ways. */
421 #ifdef BYTE_CODE_THREADED
422 /* The CASE macro introduces an instruction's body. It is
423 either a label or a case label. */
424 #define CASE(OP) insn_ ## OP
425 /* NEXT is invoked at the end of an instruction to go to the
426 next instruction. It is either a computed goto, or a
427 plain break. */
428 #define NEXT goto *(targets[op = FETCH])
429 /* FIRST is like NEXT, but is only used at the start of the
430 interpreter body. In the switch-based interpreter it is the
431 switch, so the threaded definition must include a semicolon. */
432 #define FIRST NEXT;
433 /* Most cases are labeled with the CASE macro, above.
434 CASE_DEFAULT is one exception; it is used if the interpreter
435 being built requires a default case. The threaded
436 interpreter does not, because the dispatch table is
437 completely filled. */
438 #define CASE_DEFAULT
439 /* This introduces an instruction that is known to call abort. */
440 #define CASE_ABORT CASE (Bstack_ref): CASE (default)
441 #else
442 /* See above for the meaning of the various defines. */
443 #define CASE(OP) case OP
444 #define NEXT break
445 #define FIRST switch (op)
446 #define CASE_DEFAULT case 255: default:
447 #define CASE_ABORT case 0
448 #endif
450 #ifdef BYTE_CODE_THREADED
452 /* A convenience define that saves us a lot of typing and makes
453 the table clearer. */
454 #define LABEL(OP) [OP] = &&insn_ ## OP
456 /* This is the dispatch table for the threaded interpreter. */
457 static const void *const targets[256] =
459 [0 ... (Bconstant - 1)] = &&insn_default,
460 [Bconstant ... 255] = &&insn_Bconstant,
462 #define DEFINE(name, value) LABEL (name) ,
463 BYTE_CODES
464 #undef DEFINE
467 #endif
470 FIRST
472 CASE (Bvarref7):
473 op = FETCH2;
474 goto varref;
476 CASE (Bvarref):
477 CASE (Bvarref1):
478 CASE (Bvarref2):
479 CASE (Bvarref3):
480 CASE (Bvarref4):
481 CASE (Bvarref5):
482 op -= Bvarref;
483 goto varref;
485 /* This seems to be the most frequently executed byte-code
486 among the Bvarref's, so avoid a goto here. */
487 CASE (Bvarref6):
488 op = FETCH;
489 varref:
491 Lisp_Object v1 = vectorp[op], v2;
492 if (!SYMBOLP (v1)
493 || XSYMBOL (v1)->u.s.redirect != SYMBOL_PLAINVAL
494 || (v2 = SYMBOL_VAL (XSYMBOL (v1)), EQ (v2, Qunbound)))
495 v2 = Fsymbol_value (v1);
496 PUSH (v2);
497 NEXT;
500 CASE (Bgotoifnil):
502 Lisp_Object v1 = POP;
503 op = FETCH2;
504 if (NILP (v1))
505 goto op_branch;
506 NEXT;
509 CASE (Bcar):
510 if (CONSP (TOP))
511 TOP = XCAR (TOP);
512 else if (!NILP (TOP))
513 wrong_type_argument (Qlistp, TOP);
514 NEXT;
516 CASE (Beq):
518 Lisp_Object v1 = POP;
519 TOP = EQ (v1, TOP) ? Qt : Qnil;
520 NEXT;
523 CASE (Bmemq):
525 Lisp_Object v1 = POP;
526 TOP = Fmemq (TOP, v1);
527 NEXT;
530 CASE (Bcdr):
532 if (CONSP (TOP))
533 TOP = XCDR (TOP);
534 else if (!NILP (TOP))
535 wrong_type_argument (Qlistp, TOP);
536 NEXT;
539 CASE (Bvarset):
540 CASE (Bvarset1):
541 CASE (Bvarset2):
542 CASE (Bvarset3):
543 CASE (Bvarset4):
544 CASE (Bvarset5):
545 op -= Bvarset;
546 goto varset;
548 CASE (Bvarset7):
549 op = FETCH2;
550 goto varset;
552 CASE (Bvarset6):
553 op = FETCH;
554 varset:
556 Lisp_Object sym = vectorp[op];
557 Lisp_Object val = POP;
559 /* Inline the most common case. */
560 if (SYMBOLP (sym)
561 && !EQ (val, Qunbound)
562 && !XSYMBOL (sym)->u.s.redirect
563 && !SYMBOL_TRAPPED_WRITE_P (sym))
564 SET_SYMBOL_VAL (XSYMBOL (sym), val);
565 else
566 set_internal (sym, val, Qnil, SET_INTERNAL_SET);
568 NEXT;
570 CASE (Bdup):
572 Lisp_Object v1 = TOP;
573 PUSH (v1);
574 NEXT;
577 /* ------------------ */
579 CASE (Bvarbind6):
580 op = FETCH;
581 goto varbind;
583 CASE (Bvarbind7):
584 op = FETCH2;
585 goto varbind;
587 CASE (Bvarbind):
588 CASE (Bvarbind1):
589 CASE (Bvarbind2):
590 CASE (Bvarbind3):
591 CASE (Bvarbind4):
592 CASE (Bvarbind5):
593 op -= Bvarbind;
594 varbind:
595 /* Specbind can signal and thus GC. */
596 specbind (vectorp[op], POP);
597 NEXT;
599 CASE (Bcall6):
600 op = FETCH;
601 goto docall;
603 CASE (Bcall7):
604 op = FETCH2;
605 goto docall;
607 CASE (Bcall):
608 CASE (Bcall1):
609 CASE (Bcall2):
610 CASE (Bcall3):
611 CASE (Bcall4):
612 CASE (Bcall5):
613 op -= Bcall;
614 docall:
616 DISCARD (op);
617 #ifdef BYTE_CODE_METER
618 if (byte_metering_on && SYMBOLP (TOP))
620 Lisp_Object v1 = TOP;
621 Lisp_Object v2 = Fget (v1, Qbyte_code_meter);
622 if (INTEGERP (v2)
623 && XINT (v2) < MOST_POSITIVE_FIXNUM)
625 XSETINT (v2, XINT (v2) + 1);
626 Fput (v1, Qbyte_code_meter, v2);
629 #endif
630 TOP = Ffuncall (op + 1, &TOP);
631 NEXT;
634 CASE (Bunbind6):
635 op = FETCH;
636 goto dounbind;
638 CASE (Bunbind7):
639 op = FETCH2;
640 goto dounbind;
642 CASE (Bunbind):
643 CASE (Bunbind1):
644 CASE (Bunbind2):
645 CASE (Bunbind3):
646 CASE (Bunbind4):
647 CASE (Bunbind5):
648 op -= Bunbind;
649 dounbind:
650 unbind_to (SPECPDL_INDEX () - op, Qnil);
651 NEXT;
653 CASE (Bunbind_all): /* Obsolete. Never used. */
654 /* To unbind back to the beginning of this frame. Not used yet,
655 but will be needed for tail-recursion elimination. */
656 unbind_to (count, Qnil);
657 NEXT;
659 CASE (Bgoto):
660 op = FETCH2;
661 op_branch:
662 op -= pc - bytestr_data;
663 op_relative_branch:
664 if (BYTE_CODE_SAFE
665 && ! (bytestr_data - pc <= op
666 && op < bytestr_data + bytestr_length - pc))
667 emacs_abort ();
668 quitcounter += op < 0;
669 if (!quitcounter)
671 quitcounter = 1;
672 maybe_gc ();
673 maybe_quit ();
675 pc += op;
676 NEXT;
678 CASE (Bgotoifnonnil):
679 op = FETCH2;
680 if (!NILP (POP))
681 goto op_branch;
682 NEXT;
684 CASE (Bgotoifnilelsepop):
685 op = FETCH2;
686 if (NILP (TOP))
687 goto op_branch;
688 DISCARD (1);
689 NEXT;
691 CASE (Bgotoifnonnilelsepop):
692 op = FETCH2;
693 if (!NILP (TOP))
694 goto op_branch;
695 DISCARD (1);
696 NEXT;
698 CASE (BRgoto):
699 op = FETCH - 128;
700 goto op_relative_branch;
702 CASE (BRgotoifnil):
703 op = FETCH - 128;
704 if (NILP (POP))
705 goto op_relative_branch;
706 NEXT;
708 CASE (BRgotoifnonnil):
709 op = FETCH - 128;
710 if (!NILP (POP))
711 goto op_relative_branch;
712 NEXT;
714 CASE (BRgotoifnilelsepop):
715 op = FETCH - 128;
716 if (NILP (TOP))
717 goto op_relative_branch;
718 DISCARD (1);
719 NEXT;
721 CASE (BRgotoifnonnilelsepop):
722 op = FETCH - 128;
723 if (!NILP (TOP))
724 goto op_relative_branch;
725 DISCARD (1);
726 NEXT;
728 CASE (Breturn):
729 goto exit;
731 CASE (Bdiscard):
732 DISCARD (1);
733 NEXT;
735 CASE (Bconstant2):
736 PUSH (vectorp[FETCH2]);
737 NEXT;
739 CASE (Bsave_excursion):
740 record_unwind_protect (save_excursion_restore,
741 save_excursion_save ());
742 NEXT;
744 CASE (Bsave_current_buffer): /* Obsolete since ??. */
745 CASE (Bsave_current_buffer_1):
746 record_unwind_current_buffer ();
747 NEXT;
749 CASE (Bsave_window_excursion): /* Obsolete since 24.1. */
751 ptrdiff_t count1 = SPECPDL_INDEX ();
752 record_unwind_protect (restore_window_configuration,
753 Fcurrent_window_configuration (Qnil));
754 TOP = Fprogn (TOP);
755 unbind_to (count1, TOP);
756 NEXT;
759 CASE (Bsave_restriction):
760 record_unwind_protect (save_restriction_restore,
761 save_restriction_save ());
762 NEXT;
764 CASE (Bcatch): /* Obsolete since 24.4. */
766 Lisp_Object v1 = POP;
767 TOP = internal_catch (TOP, eval_sub, v1);
768 NEXT;
771 CASE (Bpushcatch): /* New in 24.4. */
772 type = CATCHER;
773 goto pushhandler;
774 CASE (Bpushconditioncase): /* New in 24.4. */
775 type = CONDITION_CASE;
776 pushhandler:
778 struct handler *c = push_handler (POP, type);
779 c->bytecode_dest = FETCH2;
780 c->bytecode_top = top;
782 if (sys_setjmp (c->jmp))
784 struct handler *c = handlerlist;
785 top = c->bytecode_top;
786 op = c->bytecode_dest;
787 handlerlist = c->next;
788 PUSH (c->val);
789 goto op_branch;
792 NEXT;
795 CASE (Bpophandler): /* New in 24.4. */
796 handlerlist = handlerlist->next;
797 NEXT;
799 CASE (Bunwind_protect): /* FIXME: avoid closure for lexbind. */
801 Lisp_Object handler = POP;
802 /* Support for a function here is new in 24.4. */
803 record_unwind_protect (FUNCTIONP (handler) ? bcall0 : prog_ignore,
804 handler);
805 NEXT;
808 CASE (Bcondition_case): /* Obsolete since 24.4. */
810 Lisp_Object handlers = POP, body = POP;
811 TOP = internal_lisp_condition_case (TOP, body, handlers);
812 NEXT;
815 CASE (Btemp_output_buffer_setup): /* Obsolete since 24.1. */
816 CHECK_STRING (TOP);
817 temp_output_buffer_setup (SSDATA (TOP));
818 TOP = Vstandard_output;
819 NEXT;
821 CASE (Btemp_output_buffer_show): /* Obsolete since 24.1. */
823 Lisp_Object v1 = POP;
824 temp_output_buffer_show (TOP);
825 TOP = v1;
826 /* pop binding of standard-output */
827 unbind_to (SPECPDL_INDEX () - 1, Qnil);
828 NEXT;
831 CASE (Bnth):
833 Lisp_Object v2 = POP, v1 = TOP;
834 CHECK_NUMBER (v1);
835 for (EMACS_INT n = XINT (v1); 0 < n && CONSP (v2); n--)
837 v2 = XCDR (v2);
838 rarely_quit (n);
840 TOP = CAR (v2);
841 NEXT;
844 CASE (Bsymbolp):
845 TOP = SYMBOLP (TOP) ? Qt : Qnil;
846 NEXT;
848 CASE (Bconsp):
849 TOP = CONSP (TOP) ? Qt : Qnil;
850 NEXT;
852 CASE (Bstringp):
853 TOP = STRINGP (TOP) ? Qt : Qnil;
854 NEXT;
856 CASE (Blistp):
857 TOP = CONSP (TOP) || NILP (TOP) ? Qt : Qnil;
858 NEXT;
860 CASE (Bnot):
861 TOP = NILP (TOP) ? Qt : Qnil;
862 NEXT;
864 CASE (Bcons):
866 Lisp_Object v1 = POP;
867 TOP = Fcons (TOP, v1);
868 NEXT;
871 CASE (Blist1):
872 TOP = list1 (TOP);
873 NEXT;
875 CASE (Blist2):
877 Lisp_Object v1 = POP;
878 TOP = list2 (TOP, v1);
879 NEXT;
882 CASE (Blist3):
883 DISCARD (2);
884 TOP = Flist (3, &TOP);
885 NEXT;
887 CASE (Blist4):
888 DISCARD (3);
889 TOP = Flist (4, &TOP);
890 NEXT;
892 CASE (BlistN):
893 op = FETCH;
894 DISCARD (op - 1);
895 TOP = Flist (op, &TOP);
896 NEXT;
898 CASE (Blength):
899 TOP = Flength (TOP);
900 NEXT;
902 CASE (Baref):
904 Lisp_Object v1 = POP;
905 TOP = Faref (TOP, v1);
906 NEXT;
909 CASE (Baset):
911 Lisp_Object v2 = POP, v1 = POP;
912 TOP = Faset (TOP, v1, v2);
913 NEXT;
916 CASE (Bsymbol_value):
917 TOP = Fsymbol_value (TOP);
918 NEXT;
920 CASE (Bsymbol_function):
921 TOP = Fsymbol_function (TOP);
922 NEXT;
924 CASE (Bset):
926 Lisp_Object v1 = POP;
927 TOP = Fset (TOP, v1);
928 NEXT;
931 CASE (Bfset):
933 Lisp_Object v1 = POP;
934 TOP = Ffset (TOP, v1);
935 NEXT;
938 CASE (Bget):
940 Lisp_Object v1 = POP;
941 TOP = Fget (TOP, v1);
942 NEXT;
945 CASE (Bsubstring):
947 Lisp_Object v2 = POP, v1 = POP;
948 TOP = Fsubstring (TOP, v1, v2);
949 NEXT;
952 CASE (Bconcat2):
953 DISCARD (1);
954 TOP = Fconcat (2, &TOP);
955 NEXT;
957 CASE (Bconcat3):
958 DISCARD (2);
959 TOP = Fconcat (3, &TOP);
960 NEXT;
962 CASE (Bconcat4):
963 DISCARD (3);
964 TOP = Fconcat (4, &TOP);
965 NEXT;
967 CASE (BconcatN):
968 op = FETCH;
969 DISCARD (op - 1);
970 TOP = Fconcat (op, &TOP);
971 NEXT;
973 CASE (Bsub1):
974 TOP = INTEGERP (TOP) ? make_number (XINT (TOP) - 1) : Fsub1 (TOP);
975 NEXT;
977 CASE (Badd1):
978 TOP = INTEGERP (TOP) ? make_number (XINT (TOP) + 1) : Fadd1 (TOP);
979 NEXT;
981 CASE (Beqlsign):
983 Lisp_Object v2 = POP, v1 = TOP;
984 if (FLOATP (v1) || FLOATP (v2))
985 TOP = arithcompare (v1, v2, ARITH_EQUAL);
986 else
988 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v1);
989 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v2);
990 TOP = EQ (v1, v2) ? Qt : Qnil;
992 NEXT;
995 CASE (Bgtr):
997 Lisp_Object v1 = POP;
998 TOP = arithcompare (TOP, v1, ARITH_GRTR);
999 NEXT;
1002 CASE (Blss):
1004 Lisp_Object v1 = POP;
1005 TOP = arithcompare (TOP, v1, ARITH_LESS);
1006 NEXT;
1009 CASE (Bleq):
1011 Lisp_Object v1 = POP;
1012 TOP = arithcompare (TOP, v1, ARITH_LESS_OR_EQUAL);
1013 NEXT;
1016 CASE (Bgeq):
1018 Lisp_Object v1 = POP;
1019 TOP = arithcompare (TOP, v1, ARITH_GRTR_OR_EQUAL);
1020 NEXT;
1023 CASE (Bdiff):
1024 DISCARD (1);
1025 TOP = Fminus (2, &TOP);
1026 NEXT;
1028 CASE (Bnegate):
1029 TOP = INTEGERP (TOP) ? make_number (- XINT (TOP)) : Fminus (1, &TOP);
1030 NEXT;
1032 CASE (Bplus):
1033 DISCARD (1);
1034 TOP = Fplus (2, &TOP);
1035 NEXT;
1037 CASE (Bmax):
1038 DISCARD (1);
1039 TOP = Fmax (2, &TOP);
1040 NEXT;
1042 CASE (Bmin):
1043 DISCARD (1);
1044 TOP = Fmin (2, &TOP);
1045 NEXT;
1047 CASE (Bmult):
1048 DISCARD (1);
1049 TOP = Ftimes (2, &TOP);
1050 NEXT;
1052 CASE (Bquo):
1053 DISCARD (1);
1054 TOP = Fquo (2, &TOP);
1055 NEXT;
1057 CASE (Brem):
1059 Lisp_Object v1 = POP;
1060 TOP = Frem (TOP, v1);
1061 NEXT;
1064 CASE (Bpoint):
1065 PUSH (make_natnum (PT));
1066 NEXT;
1068 CASE (Bgoto_char):
1069 TOP = Fgoto_char (TOP);
1070 NEXT;
1072 CASE (Binsert):
1073 TOP = Finsert (1, &TOP);
1074 NEXT;
1076 CASE (BinsertN):
1077 op = FETCH;
1078 DISCARD (op - 1);
1079 TOP = Finsert (op, &TOP);
1080 NEXT;
1082 CASE (Bpoint_max):
1084 Lisp_Object v1;
1085 XSETFASTINT (v1, ZV);
1086 PUSH (v1);
1087 NEXT;
1090 CASE (Bpoint_min):
1091 PUSH (make_natnum (BEGV));
1092 NEXT;
1094 CASE (Bchar_after):
1095 TOP = Fchar_after (TOP);
1096 NEXT;
1098 CASE (Bfollowing_char):
1099 PUSH (Ffollowing_char ());
1100 NEXT;
1102 CASE (Bpreceding_char):
1103 PUSH (Fprevious_char ());
1104 NEXT;
1106 CASE (Bcurrent_column):
1107 PUSH (make_natnum (current_column ()));
1108 NEXT;
1110 CASE (Bindent_to):
1111 TOP = Findent_to (TOP, Qnil);
1112 NEXT;
1114 CASE (Beolp):
1115 PUSH (Feolp ());
1116 NEXT;
1118 CASE (Beobp):
1119 PUSH (Feobp ());
1120 NEXT;
1122 CASE (Bbolp):
1123 PUSH (Fbolp ());
1124 NEXT;
1126 CASE (Bbobp):
1127 PUSH (Fbobp ());
1128 NEXT;
1130 CASE (Bcurrent_buffer):
1131 PUSH (Fcurrent_buffer ());
1132 NEXT;
1134 CASE (Bset_buffer):
1135 TOP = Fset_buffer (TOP);
1136 NEXT;
1138 CASE (Binteractive_p): /* Obsolete since 24.1. */
1139 PUSH (call0 (intern ("interactive-p")));
1140 NEXT;
1142 CASE (Bforward_char):
1143 TOP = Fforward_char (TOP);
1144 NEXT;
1146 CASE (Bforward_word):
1147 TOP = Fforward_word (TOP);
1148 NEXT;
1150 CASE (Bskip_chars_forward):
1152 Lisp_Object v1 = POP;
1153 TOP = Fskip_chars_forward (TOP, v1);
1154 NEXT;
1157 CASE (Bskip_chars_backward):
1159 Lisp_Object v1 = POP;
1160 TOP = Fskip_chars_backward (TOP, v1);
1161 NEXT;
1164 CASE (Bforward_line):
1165 TOP = Fforward_line (TOP);
1166 NEXT;
1168 CASE (Bchar_syntax):
1170 CHECK_CHARACTER (TOP);
1171 int c = XFASTINT (TOP);
1172 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1173 MAKE_CHAR_MULTIBYTE (c);
1174 XSETFASTINT (TOP, syntax_code_spec[SYNTAX (c)]);
1176 NEXT;
1178 CASE (Bbuffer_substring):
1180 Lisp_Object v1 = POP;
1181 TOP = Fbuffer_substring (TOP, v1);
1182 NEXT;
1185 CASE (Bdelete_region):
1187 Lisp_Object v1 = POP;
1188 TOP = Fdelete_region (TOP, v1);
1189 NEXT;
1192 CASE (Bnarrow_to_region):
1194 Lisp_Object v1 = POP;
1195 TOP = Fnarrow_to_region (TOP, v1);
1196 NEXT;
1199 CASE (Bwiden):
1200 PUSH (Fwiden ());
1201 NEXT;
1203 CASE (Bend_of_line):
1204 TOP = Fend_of_line (TOP);
1205 NEXT;
1207 CASE (Bset_marker):
1209 Lisp_Object v2 = POP, v1 = POP;
1210 TOP = Fset_marker (TOP, v1, v2);
1211 NEXT;
1214 CASE (Bmatch_beginning):
1215 TOP = Fmatch_beginning (TOP);
1216 NEXT;
1218 CASE (Bmatch_end):
1219 TOP = Fmatch_end (TOP);
1220 NEXT;
1222 CASE (Bupcase):
1223 TOP = Fupcase (TOP);
1224 NEXT;
1226 CASE (Bdowncase):
1227 TOP = Fdowncase (TOP);
1228 NEXT;
1230 CASE (Bstringeqlsign):
1232 Lisp_Object v1 = POP;
1233 TOP = Fstring_equal (TOP, v1);
1234 NEXT;
1237 CASE (Bstringlss):
1239 Lisp_Object v1 = POP;
1240 TOP = Fstring_lessp (TOP, v1);
1241 NEXT;
1244 CASE (Bequal):
1246 Lisp_Object v1 = POP;
1247 TOP = Fequal (TOP, v1);
1248 NEXT;
1251 CASE (Bnthcdr):
1253 Lisp_Object v1 = POP;
1254 TOP = Fnthcdr (TOP, v1);
1255 NEXT;
1258 CASE (Belt):
1260 if (CONSP (TOP))
1262 /* Exchange args and then do nth. */
1263 Lisp_Object v2 = POP, v1 = TOP;
1264 CHECK_NUMBER (v2);
1265 for (EMACS_INT n = XINT (v2); 0 < n && CONSP (v1); n--)
1267 v1 = XCDR (v1);
1268 rarely_quit (n);
1270 TOP = CAR (v1);
1272 else
1274 Lisp_Object v1 = POP;
1275 TOP = Felt (TOP, v1);
1277 NEXT;
1280 CASE (Bmember):
1282 Lisp_Object v1 = POP;
1283 TOP = Fmember (TOP, v1);
1284 NEXT;
1287 CASE (Bassq):
1289 Lisp_Object v1 = POP;
1290 TOP = Fassq (TOP, v1);
1291 NEXT;
1294 CASE (Bnreverse):
1295 TOP = Fnreverse (TOP);
1296 NEXT;
1298 CASE (Bsetcar):
1300 Lisp_Object v1 = POP;
1301 TOP = Fsetcar (TOP, v1);
1302 NEXT;
1305 CASE (Bsetcdr):
1307 Lisp_Object v1 = POP;
1308 TOP = Fsetcdr (TOP, v1);
1309 NEXT;
1312 CASE (Bcar_safe):
1313 TOP = CAR_SAFE (TOP);
1314 NEXT;
1316 CASE (Bcdr_safe):
1317 TOP = CDR_SAFE (TOP);
1318 NEXT;
1320 CASE (Bnconc):
1321 DISCARD (1);
1322 TOP = Fnconc (2, &TOP);
1323 NEXT;
1325 CASE (Bnumberp):
1326 TOP = NUMBERP (TOP) ? Qt : Qnil;
1327 NEXT;
1329 CASE (Bintegerp):
1330 TOP = INTEGERP (TOP) ? Qt : Qnil;
1331 NEXT;
1333 #if BYTE_CODE_SAFE
1334 /* These are intentionally written using 'case' syntax,
1335 because they are incompatible with the threaded
1336 interpreter. */
1338 case Bset_mark:
1339 error ("set-mark is an obsolete bytecode");
1340 break;
1341 case Bscan_buffer:
1342 error ("scan-buffer is an obsolete bytecode");
1343 break;
1344 #endif
1346 CASE_ABORT:
1347 /* Actually this is Bstack_ref with offset 0, but we use Bdup
1348 for that instead. */
1349 /* CASE (Bstack_ref): */
1350 error ("Invalid byte opcode: op=%d, ptr=%"pD"d",
1351 op, pc - 1 - bytestr_data);
1353 /* Handy byte-codes for lexical binding. */
1354 CASE (Bstack_ref1):
1355 CASE (Bstack_ref2):
1356 CASE (Bstack_ref3):
1357 CASE (Bstack_ref4):
1358 CASE (Bstack_ref5):
1360 Lisp_Object v1 = top[Bstack_ref - op];
1361 PUSH (v1);
1362 NEXT;
1364 CASE (Bstack_ref6):
1366 Lisp_Object v1 = top[- FETCH];
1367 PUSH (v1);
1368 NEXT;
1370 CASE (Bstack_ref7):
1372 Lisp_Object v1 = top[- FETCH2];
1373 PUSH (v1);
1374 NEXT;
1376 CASE (Bstack_set):
1377 /* stack-set-0 = discard; stack-set-1 = discard-1-preserve-tos. */
1379 Lisp_Object *ptr = top - FETCH;
1380 *ptr = POP;
1381 NEXT;
1383 CASE (Bstack_set2):
1385 Lisp_Object *ptr = top - FETCH2;
1386 *ptr = POP;
1387 NEXT;
1389 CASE (BdiscardN):
1390 op = FETCH;
1391 if (op & 0x80)
1393 op &= 0x7F;
1394 top[-op] = TOP;
1396 DISCARD (op);
1397 NEXT;
1399 CASE (Bswitch):
1401 /* TODO: Perhaps introduce another byte-code for switch when the
1402 number of cases is less, which uses a simple vector for linear
1403 search as the jump table. */
1404 Lisp_Object jmp_table = POP;
1405 if (BYTE_CODE_SAFE && !HASH_TABLE_P (jmp_table))
1406 emacs_abort ();
1407 Lisp_Object v1 = POP;
1408 ptrdiff_t i;
1409 struct Lisp_Hash_Table *h = XHASH_TABLE (jmp_table);
1411 /* h->count is a faster approximation for HASH_TABLE_SIZE (h)
1412 here. */
1413 if (h->count <= 5)
1414 { /* Do a linear search if there are not many cases
1415 FIXME: 5 is arbitrarily chosen. */
1416 Lisp_Object hash_code = h->test.cmpfn
1417 ? make_number (h->test.hashfn (&h->test, v1)) : Qnil;
1419 for (i = h->count; 0 <= --i; )
1420 if (EQ (v1, HASH_KEY (h, i))
1421 || (h->test.cmpfn
1422 && EQ (hash_code, HASH_HASH (h, i))
1423 && h->test.cmpfn (&h->test, v1, HASH_KEY (h, i))))
1424 break;
1427 else
1428 i = hash_lookup (h, v1, NULL);
1430 if (i >= 0)
1432 Lisp_Object val = HASH_VALUE (h, i);
1433 if (BYTE_CODE_SAFE && !INTEGERP (val))
1434 emacs_abort ();
1435 op = XINT (val);
1436 goto op_branch;
1439 NEXT;
1441 CASE_DEFAULT
1442 CASE (Bconstant):
1443 if (BYTE_CODE_SAFE
1444 && ! (Bconstant <= op && op < Bconstant + const_length))
1445 emacs_abort ();
1446 PUSH (vectorp[op - Bconstant]);
1447 NEXT;
1451 exit:
1453 /* Binds and unbinds are supposed to be compiled balanced. */
1454 if (SPECPDL_INDEX () != count)
1456 if (SPECPDL_INDEX () > count)
1457 unbind_to (count, Qnil);
1458 error ("binding stack not balanced (serious byte compiler bug)");
1461 Lisp_Object result = TOP;
1462 SAFE_FREE ();
1463 return result;
1466 /* `args_template' has the same meaning as in exec_byte_code() above. */
1467 Lisp_Object
1468 get_byte_code_arity (Lisp_Object args_template)
1470 eassert (NATNUMP (args_template));
1471 EMACS_INT at = XINT (args_template);
1472 bool rest = (at & 128) != 0;
1473 int mandatory = at & 127;
1474 EMACS_INT nonrest = at >> 8;
1476 return Fcons (make_number (mandatory),
1477 rest ? Qmany : make_number (nonrest));
1480 void
1481 syms_of_bytecode (void)
1483 defsubr (&Sbyte_code);
1485 #ifdef BYTE_CODE_METER
1487 DEFVAR_LISP ("byte-code-meter", Vbyte_code_meter,
1488 doc: /* A vector of vectors which holds a histogram of byte-code usage.
1489 \(aref (aref byte-code-meter 0) CODE) indicates how many times the byte
1490 opcode CODE has been executed.
1491 \(aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,
1492 indicates how many times the byte opcodes CODE1 and CODE2 have been
1493 executed in succession. */);
1495 DEFVAR_BOOL ("byte-metering-on", byte_metering_on,
1496 doc: /* If non-nil, keep profiling information on byte code usage.
1497 The variable byte-code-meter indicates how often each byte opcode is used.
1498 If a symbol has a property named `byte-code-meter' whose value is an
1499 integer, it is incremented each time that symbol's function is called. */);
1501 byte_metering_on = false;
1502 Vbyte_code_meter = Fmake_vector (make_number (256), make_number (0));
1503 DEFSYM (Qbyte_code_meter, "byte-code-meter");
1505 int i = 256;
1506 while (i--)
1507 ASET (Vbyte_code_meter, i,
1508 Fmake_vector (make_number (256), make_number (0)));
1510 #endif