Add file-local-name
[emacs.git] / src / bytecode.c
blobe2d8ab706c7960a8565b4c2282ad66a328239cf8
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__ \
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 = POP;
512 op = FETCH2;
513 if (NILP (v1))
514 goto op_branch;
515 NEXT;
518 CASE (Bcar):
519 if (CONSP (TOP))
520 TOP = XCAR (TOP);
521 else if (!NILP (TOP))
522 wrong_type_argument (Qlistp, TOP);
523 NEXT;
525 CASE (Beq):
527 Lisp_Object v1 = POP;
528 TOP = EQ (v1, TOP) ? Qt : Qnil;
529 NEXT;
532 CASE (Bmemq):
534 Lisp_Object v1 = POP;
535 TOP = Fmemq (TOP, v1);
536 NEXT;
539 CASE (Bcdr):
541 if (CONSP (TOP))
542 TOP = XCDR (TOP);
543 else if (!NILP (TOP))
544 wrong_type_argument (Qlistp, TOP);
545 NEXT;
548 CASE (Bvarset):
549 CASE (Bvarset1):
550 CASE (Bvarset2):
551 CASE (Bvarset3):
552 CASE (Bvarset4):
553 CASE (Bvarset5):
554 op -= Bvarset;
555 goto varset;
557 CASE (Bvarset7):
558 op = FETCH2;
559 goto varset;
561 CASE (Bvarset6):
562 op = FETCH;
563 varset:
565 Lisp_Object sym = vectorp[op];
566 Lisp_Object val = POP;
568 /* Inline the most common case. */
569 if (SYMBOLP (sym)
570 && !EQ (val, Qunbound)
571 && !XSYMBOL (sym)->redirect
572 && !SYMBOL_CONSTANT_P (sym))
573 SET_SYMBOL_VAL (XSYMBOL (sym), val);
574 else
575 set_internal (sym, val, Qnil, false);
577 NEXT;
579 CASE (Bdup):
581 Lisp_Object v1 = TOP;
582 PUSH (v1);
583 NEXT;
586 /* ------------------ */
588 CASE (Bvarbind6):
589 op = FETCH;
590 goto varbind;
592 CASE (Bvarbind7):
593 op = FETCH2;
594 goto varbind;
596 CASE (Bvarbind):
597 CASE (Bvarbind1):
598 CASE (Bvarbind2):
599 CASE (Bvarbind3):
600 CASE (Bvarbind4):
601 CASE (Bvarbind5):
602 op -= Bvarbind;
603 varbind:
604 /* Specbind can signal and thus GC. */
605 specbind (vectorp[op], POP);
606 NEXT;
608 CASE (Bcall6):
609 op = FETCH;
610 goto docall;
612 CASE (Bcall7):
613 op = FETCH2;
614 goto docall;
616 CASE (Bcall):
617 CASE (Bcall1):
618 CASE (Bcall2):
619 CASE (Bcall3):
620 CASE (Bcall4):
621 CASE (Bcall5):
622 op -= Bcall;
623 docall:
625 DISCARD (op);
626 #ifdef BYTE_CODE_METER
627 if (byte_metering_on && SYMBOLP (TOP))
629 Lisp_Object v1 = TOP;
630 Lisp_Object v2 = Fget (v1, Qbyte_code_meter);
631 if (INTEGERP (v2)
632 && XINT (v2) < MOST_POSITIVE_FIXNUM)
634 XSETINT (v2, XINT (v2) + 1);
635 Fput (v1, Qbyte_code_meter, v2);
638 #endif
639 TOP = Ffuncall (op + 1, &TOP);
640 NEXT;
643 CASE (Bunbind6):
644 op = FETCH;
645 goto dounbind;
647 CASE (Bunbind7):
648 op = FETCH2;
649 goto dounbind;
651 CASE (Bunbind):
652 CASE (Bunbind1):
653 CASE (Bunbind2):
654 CASE (Bunbind3):
655 CASE (Bunbind4):
656 CASE (Bunbind5):
657 op -= Bunbind;
658 dounbind:
659 unbind_to (SPECPDL_INDEX () - op, Qnil);
660 NEXT;
662 CASE (Bunbind_all): /* Obsolete. Never used. */
663 /* To unbind back to the beginning of this frame. Not used yet,
664 but will be needed for tail-recursion elimination. */
665 unbind_to (count, Qnil);
666 NEXT;
668 CASE (Bgoto):
669 op = FETCH2;
670 op_branch:
671 op -= pc - bytestr_data;
672 op_relative_branch:
673 if (BYTE_CODE_SAFE
674 && ! (bytestr_data - pc <= op
675 && op < bytestr_data + bytestr_length - pc))
676 emacs_abort ();
677 quitcounter += op < 0;
678 if (!quitcounter)
680 quitcounter = 1;
681 maybe_gc ();
682 QUIT;
684 pc += op;
685 NEXT;
687 CASE (Bgotoifnonnil):
688 op = FETCH2;
689 if (!NILP (POP))
690 goto op_branch;
691 NEXT;
693 CASE (Bgotoifnilelsepop):
694 op = FETCH2;
695 if (NILP (TOP))
696 goto op_branch;
697 DISCARD (1);
698 NEXT;
700 CASE (Bgotoifnonnilelsepop):
701 op = FETCH2;
702 if (!NILP (TOP))
703 goto op_branch;
704 DISCARD (1);
705 NEXT;
707 CASE (BRgoto):
708 op = FETCH - 128;
709 goto op_relative_branch;
711 CASE (BRgotoifnil):
712 op = FETCH - 128;
713 if (NILP (POP))
714 goto op_relative_branch;
715 NEXT;
717 CASE (BRgotoifnonnil):
718 op = FETCH - 128;
719 if (!NILP (POP))
720 goto op_relative_branch;
721 NEXT;
723 CASE (BRgotoifnilelsepop):
724 op = FETCH - 128;
725 if (NILP (TOP))
726 goto op_relative_branch;
727 DISCARD (1);
728 NEXT;
730 CASE (BRgotoifnonnilelsepop):
731 op = FETCH - 128;
732 if (!NILP (TOP))
733 goto op_relative_branch;
734 DISCARD (1);
735 NEXT;
737 CASE (Breturn):
738 goto exit;
740 CASE (Bdiscard):
741 DISCARD (1);
742 NEXT;
744 CASE (Bconstant2):
745 PUSH (vectorp[FETCH2]);
746 NEXT;
748 CASE (Bsave_excursion):
749 record_unwind_protect (save_excursion_restore,
750 save_excursion_save ());
751 NEXT;
753 CASE (Bsave_current_buffer): /* Obsolete since ??. */
754 CASE (Bsave_current_buffer_1):
755 record_unwind_current_buffer ();
756 NEXT;
758 CASE (Bsave_window_excursion): /* Obsolete since 24.1. */
760 ptrdiff_t count1 = SPECPDL_INDEX ();
761 record_unwind_protect (restore_window_configuration,
762 Fcurrent_window_configuration (Qnil));
763 TOP = Fprogn (TOP);
764 unbind_to (count1, TOP);
765 NEXT;
768 CASE (Bsave_restriction):
769 record_unwind_protect (save_restriction_restore,
770 save_restriction_save ());
771 NEXT;
773 CASE (Bcatch): /* Obsolete since 24.4. */
775 Lisp_Object v1 = POP;
776 TOP = internal_catch (TOP, eval_sub, v1);
777 NEXT;
780 CASE (Bpushcatch): /* New in 24.4. */
781 type = CATCHER;
782 goto pushhandler;
783 CASE (Bpushconditioncase): /* New in 24.4. */
784 type = CONDITION_CASE;
785 pushhandler:
787 struct handler *c = push_handler (POP, type);
788 c->bytecode_dest = FETCH2;
789 c->bytecode_top = top;
791 if (sys_setjmp (c->jmp))
793 struct handler *c = handlerlist;
794 top = c->bytecode_top;
795 op = c->bytecode_dest;
796 handlerlist = c->next;
797 PUSH (c->val);
798 goto op_branch;
801 NEXT;
804 CASE (Bpophandler): /* New in 24.4. */
805 handlerlist = handlerlist->next;
806 NEXT;
808 CASE (Bunwind_protect): /* FIXME: avoid closure for lexbind. */
810 Lisp_Object handler = POP;
811 /* Support for a function here is new in 24.4. */
812 record_unwind_protect ((NILP (Ffunctionp (handler))
813 ? unwind_body : bcall0),
814 handler);
815 NEXT;
818 CASE (Bcondition_case): /* Obsolete since 24.4. */
820 Lisp_Object handlers = POP, body = POP;
821 TOP = internal_lisp_condition_case (TOP, body, handlers);
822 NEXT;
825 CASE (Btemp_output_buffer_setup): /* Obsolete since 24.1. */
826 CHECK_STRING (TOP);
827 temp_output_buffer_setup (SSDATA (TOP));
828 TOP = Vstandard_output;
829 NEXT;
831 CASE (Btemp_output_buffer_show): /* Obsolete since 24.1. */
833 Lisp_Object v1 = POP;
834 temp_output_buffer_show (TOP);
835 TOP = v1;
836 /* pop binding of standard-output */
837 unbind_to (SPECPDL_INDEX () - 1, Qnil);
838 NEXT;
841 CASE (Bnth):
843 Lisp_Object v2 = POP, v1 = TOP;
844 CHECK_NUMBER (v1);
845 EMACS_INT n = XINT (v1);
846 immediate_quit = true;
847 while (--n >= 0 && CONSP (v2))
848 v2 = XCDR (v2);
849 immediate_quit = false;
850 TOP = CAR (v2);
851 NEXT;
854 CASE (Bsymbolp):
855 TOP = SYMBOLP (TOP) ? Qt : Qnil;
856 NEXT;
858 CASE (Bconsp):
859 TOP = CONSP (TOP) ? Qt : Qnil;
860 NEXT;
862 CASE (Bstringp):
863 TOP = STRINGP (TOP) ? Qt : Qnil;
864 NEXT;
866 CASE (Blistp):
867 TOP = CONSP (TOP) || NILP (TOP) ? Qt : Qnil;
868 NEXT;
870 CASE (Bnot):
871 TOP = NILP (TOP) ? Qt : Qnil;
872 NEXT;
874 CASE (Bcons):
876 Lisp_Object v1 = POP;
877 TOP = Fcons (TOP, v1);
878 NEXT;
881 CASE (Blist1):
882 TOP = list1 (TOP);
883 NEXT;
885 CASE (Blist2):
887 Lisp_Object v1 = POP;
888 TOP = list2 (TOP, v1);
889 NEXT;
892 CASE (Blist3):
893 DISCARD (2);
894 TOP = Flist (3, &TOP);
895 NEXT;
897 CASE (Blist4):
898 DISCARD (3);
899 TOP = Flist (4, &TOP);
900 NEXT;
902 CASE (BlistN):
903 op = FETCH;
904 DISCARD (op - 1);
905 TOP = Flist (op, &TOP);
906 NEXT;
908 CASE (Blength):
909 TOP = Flength (TOP);
910 NEXT;
912 CASE (Baref):
914 Lisp_Object v1 = POP;
915 TOP = Faref (TOP, v1);
916 NEXT;
919 CASE (Baset):
921 Lisp_Object v2 = POP, v1 = POP;
922 TOP = Faset (TOP, v1, v2);
923 NEXT;
926 CASE (Bsymbol_value):
927 TOP = Fsymbol_value (TOP);
928 NEXT;
930 CASE (Bsymbol_function):
931 TOP = Fsymbol_function (TOP);
932 NEXT;
934 CASE (Bset):
936 Lisp_Object v1 = POP;
937 TOP = Fset (TOP, v1);
938 NEXT;
941 CASE (Bfset):
943 Lisp_Object v1 = POP;
944 TOP = Ffset (TOP, v1);
945 NEXT;
948 CASE (Bget):
950 Lisp_Object v1 = POP;
951 TOP = Fget (TOP, v1);
952 NEXT;
955 CASE (Bsubstring):
957 Lisp_Object v2 = POP, v1 = POP;
958 TOP = Fsubstring (TOP, v1, v2);
959 NEXT;
962 CASE (Bconcat2):
963 DISCARD (1);
964 TOP = Fconcat (2, &TOP);
965 NEXT;
967 CASE (Bconcat3):
968 DISCARD (2);
969 TOP = Fconcat (3, &TOP);
970 NEXT;
972 CASE (Bconcat4):
973 DISCARD (3);
974 TOP = Fconcat (4, &TOP);
975 NEXT;
977 CASE (BconcatN):
978 op = FETCH;
979 DISCARD (op - 1);
980 TOP = Fconcat (op, &TOP);
981 NEXT;
983 CASE (Bsub1):
984 TOP = INTEGERP (TOP) ? make_number (XINT (TOP) - 1) : Fsub1 (TOP);
985 NEXT;
987 CASE (Badd1):
988 TOP = INTEGERP (TOP) ? make_number (XINT (TOP) + 1) : Fadd1 (TOP);
989 NEXT;
991 CASE (Beqlsign):
993 Lisp_Object v2 = POP, v1 = TOP;
994 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v1);
995 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v2);
996 bool equal;
997 if (FLOATP (v1) || FLOATP (v2))
999 double f1 = FLOATP (v1) ? XFLOAT_DATA (v1) : XINT (v1);
1000 double f2 = FLOATP (v2) ? XFLOAT_DATA (v2) : XINT (v2);
1001 equal = f1 == f2;
1003 else
1004 equal = XINT (v1) == XINT (v2);
1005 TOP = equal ? Qt : Qnil;
1006 NEXT;
1009 CASE (Bgtr):
1011 Lisp_Object v1 = POP;
1012 TOP = arithcompare (TOP, v1, ARITH_GRTR);
1013 NEXT;
1016 CASE (Blss):
1018 Lisp_Object v1 = POP;
1019 TOP = arithcompare (TOP, v1, ARITH_LESS);
1020 NEXT;
1023 CASE (Bleq):
1025 Lisp_Object v1 = POP;
1026 TOP = arithcompare (TOP, v1, ARITH_LESS_OR_EQUAL);
1027 NEXT;
1030 CASE (Bgeq):
1032 Lisp_Object v1 = POP;
1033 TOP = arithcompare (TOP, v1, ARITH_GRTR_OR_EQUAL);
1034 NEXT;
1037 CASE (Bdiff):
1038 DISCARD (1);
1039 TOP = Fminus (2, &TOP);
1040 NEXT;
1042 CASE (Bnegate):
1043 TOP = INTEGERP (TOP) ? make_number (- XINT (TOP)) : Fminus (1, &TOP);
1044 NEXT;
1046 CASE (Bplus):
1047 DISCARD (1);
1048 TOP = Fplus (2, &TOP);
1049 NEXT;
1051 CASE (Bmax):
1052 DISCARD (1);
1053 TOP = Fmax (2, &TOP);
1054 NEXT;
1056 CASE (Bmin):
1057 DISCARD (1);
1058 TOP = Fmin (2, &TOP);
1059 NEXT;
1061 CASE (Bmult):
1062 DISCARD (1);
1063 TOP = Ftimes (2, &TOP);
1064 NEXT;
1066 CASE (Bquo):
1067 DISCARD (1);
1068 TOP = Fquo (2, &TOP);
1069 NEXT;
1071 CASE (Brem):
1073 Lisp_Object v1 = POP;
1074 TOP = Frem (TOP, v1);
1075 NEXT;
1078 CASE (Bpoint):
1079 PUSH (make_natnum (PT));
1080 NEXT;
1082 CASE (Bgoto_char):
1083 TOP = Fgoto_char (TOP);
1084 NEXT;
1086 CASE (Binsert):
1087 TOP = Finsert (1, &TOP);
1088 NEXT;
1090 CASE (BinsertN):
1091 op = FETCH;
1092 DISCARD (op - 1);
1093 TOP = Finsert (op, &TOP);
1094 NEXT;
1096 CASE (Bpoint_max):
1098 Lisp_Object v1;
1099 XSETFASTINT (v1, ZV);
1100 PUSH (v1);
1101 NEXT;
1104 CASE (Bpoint_min):
1105 PUSH (make_natnum (BEGV));
1106 NEXT;
1108 CASE (Bchar_after):
1109 TOP = Fchar_after (TOP);
1110 NEXT;
1112 CASE (Bfollowing_char):
1113 PUSH (Ffollowing_char ());
1114 NEXT;
1116 CASE (Bpreceding_char):
1117 PUSH (Fprevious_char ());
1118 NEXT;
1120 CASE (Bcurrent_column):
1121 PUSH (make_natnum (current_column ()));
1122 NEXT;
1124 CASE (Bindent_to):
1125 TOP = Findent_to (TOP, Qnil);
1126 NEXT;
1128 CASE (Beolp):
1129 PUSH (Feolp ());
1130 NEXT;
1132 CASE (Beobp):
1133 PUSH (Feobp ());
1134 NEXT;
1136 CASE (Bbolp):
1137 PUSH (Fbolp ());
1138 NEXT;
1140 CASE (Bbobp):
1141 PUSH (Fbobp ());
1142 NEXT;
1144 CASE (Bcurrent_buffer):
1145 PUSH (Fcurrent_buffer ());
1146 NEXT;
1148 CASE (Bset_buffer):
1149 TOP = Fset_buffer (TOP);
1150 NEXT;
1152 CASE (Binteractive_p): /* Obsolete since 24.1. */
1153 PUSH (call0 (intern ("interactive-p")));
1154 NEXT;
1156 CASE (Bforward_char):
1157 TOP = Fforward_char (TOP);
1158 NEXT;
1160 CASE (Bforward_word):
1161 TOP = Fforward_word (TOP);
1162 NEXT;
1164 CASE (Bskip_chars_forward):
1166 Lisp_Object v1 = POP;
1167 TOP = Fskip_chars_forward (TOP, v1);
1168 NEXT;
1171 CASE (Bskip_chars_backward):
1173 Lisp_Object v1 = POP;
1174 TOP = Fskip_chars_backward (TOP, v1);
1175 NEXT;
1178 CASE (Bforward_line):
1179 TOP = Fforward_line (TOP);
1180 NEXT;
1182 CASE (Bchar_syntax):
1184 CHECK_CHARACTER (TOP);
1185 int c = XFASTINT (TOP);
1186 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1187 MAKE_CHAR_MULTIBYTE (c);
1188 XSETFASTINT (TOP, syntax_code_spec[SYNTAX (c)]);
1190 NEXT;
1192 CASE (Bbuffer_substring):
1194 Lisp_Object v1 = POP;
1195 TOP = Fbuffer_substring (TOP, v1);
1196 NEXT;
1199 CASE (Bdelete_region):
1201 Lisp_Object v1 = POP;
1202 TOP = Fdelete_region (TOP, v1);
1203 NEXT;
1206 CASE (Bnarrow_to_region):
1208 Lisp_Object v1 = POP;
1209 TOP = Fnarrow_to_region (TOP, v1);
1210 NEXT;
1213 CASE (Bwiden):
1214 PUSH (Fwiden ());
1215 NEXT;
1217 CASE (Bend_of_line):
1218 TOP = Fend_of_line (TOP);
1219 NEXT;
1221 CASE (Bset_marker):
1223 Lisp_Object v2 = POP, v1 = POP;
1224 TOP = Fset_marker (TOP, v1, v2);
1225 NEXT;
1228 CASE (Bmatch_beginning):
1229 TOP = Fmatch_beginning (TOP);
1230 NEXT;
1232 CASE (Bmatch_end):
1233 TOP = Fmatch_end (TOP);
1234 NEXT;
1236 CASE (Bupcase):
1237 TOP = Fupcase (TOP);
1238 NEXT;
1240 CASE (Bdowncase):
1241 TOP = Fdowncase (TOP);
1242 NEXT;
1244 CASE (Bstringeqlsign):
1246 Lisp_Object v1 = POP;
1247 TOP = Fstring_equal (TOP, v1);
1248 NEXT;
1251 CASE (Bstringlss):
1253 Lisp_Object v1 = POP;
1254 TOP = Fstring_lessp (TOP, v1);
1255 NEXT;
1258 CASE (Bequal):
1260 Lisp_Object v1 = POP;
1261 TOP = Fequal (TOP, v1);
1262 NEXT;
1265 CASE (Bnthcdr):
1267 Lisp_Object v1 = POP;
1268 TOP = Fnthcdr (TOP, v1);
1269 NEXT;
1272 CASE (Belt):
1274 if (CONSP (TOP))
1276 /* Exchange args and then do nth. */
1277 Lisp_Object v2 = POP, v1 = TOP;
1278 CHECK_NUMBER (v2);
1279 EMACS_INT n = XINT (v2);
1280 immediate_quit = true;
1281 while (--n >= 0 && CONSP (v1))
1282 v1 = XCDR (v1);
1283 immediate_quit = false;
1284 TOP = CAR (v1);
1286 else
1288 Lisp_Object v1 = POP;
1289 TOP = Felt (TOP, v1);
1291 NEXT;
1294 CASE (Bmember):
1296 Lisp_Object v1 = POP;
1297 TOP = Fmember (TOP, v1);
1298 NEXT;
1301 CASE (Bassq):
1303 Lisp_Object v1 = POP;
1304 TOP = Fassq (TOP, v1);
1305 NEXT;
1308 CASE (Bnreverse):
1309 TOP = Fnreverse (TOP);
1310 NEXT;
1312 CASE (Bsetcar):
1314 Lisp_Object v1 = POP;
1315 TOP = Fsetcar (TOP, v1);
1316 NEXT;
1319 CASE (Bsetcdr):
1321 Lisp_Object v1 = POP;
1322 TOP = Fsetcdr (TOP, v1);
1323 NEXT;
1326 CASE (Bcar_safe):
1327 TOP = CAR_SAFE (TOP);
1328 NEXT;
1330 CASE (Bcdr_safe):
1331 TOP = CDR_SAFE (TOP);
1332 NEXT;
1334 CASE (Bnconc):
1335 DISCARD (1);
1336 TOP = Fnconc (2, &TOP);
1337 NEXT;
1339 CASE (Bnumberp):
1340 TOP = NUMBERP (TOP) ? Qt : Qnil;
1341 NEXT;
1343 CASE (Bintegerp):
1344 TOP = INTEGERP (TOP) ? Qt : Qnil;
1345 NEXT;
1347 #if BYTE_CODE_SAFE
1348 /* These are intentionally written using 'case' syntax,
1349 because they are incompatible with the threaded
1350 interpreter. */
1352 case Bset_mark:
1353 error ("set-mark is an obsolete bytecode");
1354 break;
1355 case Bscan_buffer:
1356 error ("scan-buffer is an obsolete bytecode");
1357 break;
1358 #endif
1360 CASE_ABORT:
1361 /* Actually this is Bstack_ref with offset 0, but we use Bdup
1362 for that instead. */
1363 /* CASE (Bstack_ref): */
1364 call3 (Qerror,
1365 build_string ("Invalid byte opcode: op=%s, ptr=%d"),
1366 make_number (op),
1367 make_number (pc - 1 - bytestr_data));
1369 /* Handy byte-codes for lexical binding. */
1370 CASE (Bstack_ref1):
1371 CASE (Bstack_ref2):
1372 CASE (Bstack_ref3):
1373 CASE (Bstack_ref4):
1374 CASE (Bstack_ref5):
1376 Lisp_Object v1 = top[Bstack_ref - op];
1377 PUSH (v1);
1378 NEXT;
1380 CASE (Bstack_ref6):
1382 Lisp_Object v1 = top[- FETCH];
1383 PUSH (v1);
1384 NEXT;
1386 CASE (Bstack_ref7):
1388 Lisp_Object v1 = top[- FETCH2];
1389 PUSH (v1);
1390 NEXT;
1392 CASE (Bstack_set):
1393 /* stack-set-0 = discard; stack-set-1 = discard-1-preserve-tos. */
1395 Lisp_Object *ptr = top - FETCH;
1396 *ptr = POP;
1397 NEXT;
1399 CASE (Bstack_set2):
1401 Lisp_Object *ptr = top - FETCH2;
1402 *ptr = POP;
1403 NEXT;
1405 CASE (BdiscardN):
1406 op = FETCH;
1407 if (op & 0x80)
1409 op &= 0x7F;
1410 top[-op] = TOP;
1412 DISCARD (op);
1413 NEXT;
1415 CASE_DEFAULT
1416 CASE (Bconstant):
1417 if (BYTE_CODE_SAFE
1418 && ! (Bconstant <= op && op < Bconstant + const_length))
1419 emacs_abort ();
1420 PUSH (vectorp[op - Bconstant]);
1421 NEXT;
1425 exit:
1427 /* Binds and unbinds are supposed to be compiled balanced. */
1428 if (SPECPDL_INDEX () != count)
1430 if (SPECPDL_INDEX () > count)
1431 unbind_to (count, Qnil);
1432 error ("binding stack not balanced (serious byte compiler bug)");
1435 Lisp_Object result = TOP;
1436 SAFE_FREE ();
1437 return result;
1440 /* `args_template' has the same meaning as in exec_byte_code() above. */
1441 Lisp_Object
1442 get_byte_code_arity (Lisp_Object args_template)
1444 eassert (NATNUMP (args_template));
1445 EMACS_INT at = XINT (args_template);
1446 bool rest = (at & 128) != 0;
1447 int mandatory = at & 127;
1448 EMACS_INT nonrest = at >> 8;
1450 return Fcons (make_number (mandatory),
1451 rest ? Qmany : make_number (nonrest));
1454 void
1455 syms_of_bytecode (void)
1457 defsubr (&Sbyte_code);
1459 #ifdef BYTE_CODE_METER
1461 DEFVAR_LISP ("byte-code-meter", Vbyte_code_meter,
1462 doc: /* A vector of vectors which holds a histogram of byte-code usage.
1463 \(aref (aref byte-code-meter 0) CODE) indicates how many times the byte
1464 opcode CODE has been executed.
1465 \(aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,
1466 indicates how many times the byte opcodes CODE1 and CODE2 have been
1467 executed in succession. */);
1469 DEFVAR_BOOL ("byte-metering-on", byte_metering_on,
1470 doc: /* If non-nil, keep profiling information on byte code usage.
1471 The variable byte-code-meter indicates how often each byte opcode is used.
1472 If a symbol has a property named `byte-code-meter' whose value is an
1473 integer, it is incremented each time that symbol's function is called. */);
1475 byte_metering_on = false;
1476 Vbyte_code_meter = Fmake_vector (make_number (256), make_number (0));
1477 DEFSYM (Qbyte_code_meter, "byte-code-meter");
1479 int i = 256;
1480 while (i--)
1481 ASET (Vbyte_code_meter, i,
1482 Fmake_vector (make_number (256), make_number (0)));
1484 #endif