* admin/update_autogen (changelog_files): Remove gitlog-to-emacslog.
[emacs.git] / src / bytecode.c
blob55789b41ad994f051669b7208970de837731b92d
1 /* Execution of byte code produced by bytecomp.el.
2 Copyright (C) 1985-1988, 1993, 2000-2015 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
10 (at 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/>. */
21 hacked on by jwz@lucid.com 17-jun-91
22 o added a compile-time switch to turn on simple sanity checking;
23 o put back the obsolete byte-codes for error-detection;
24 o added a new instruction, unbind_all, which I will use for
25 tail-recursion elimination;
26 o made temp_output_buffer_show be called with the right number
27 of args;
28 o made the new bytecodes be called with args in the right order;
29 o added metering support.
31 by Hallvard:
32 o added relative jump instructions;
33 o all conditionals now only do QUIT if they jump.
36 #include <config.h>
38 #include "lisp.h"
39 #include "blockinput.h"
40 #include "character.h"
41 #include "buffer.h"
42 #include "keyboard.h"
43 #include "syntax.h"
44 #include "window.h"
46 #ifdef CHECK_FRAME_FONT
47 #include "frame.h"
48 #include "xterm.h"
49 #endif
52 * define BYTE_CODE_SAFE to enable some minor sanity checking (useful for
53 * debugging the byte compiler...)
55 * define BYTE_CODE_METER to enable generation of a byte-op usage histogram.
57 /* #define BYTE_CODE_SAFE */
58 /* #define BYTE_CODE_METER */
60 /* If BYTE_CODE_THREADED is defined, then the interpreter will be
61 indirect threaded, using GCC's computed goto extension. This code,
62 as currently implemented, is incompatible with BYTE_CODE_SAFE and
63 BYTE_CODE_METER. */
64 #if (defined __GNUC__ && !defined __STRICT_ANSI__ \
65 && !defined BYTE_CODE_SAFE && !defined BYTE_CODE_METER)
66 #define BYTE_CODE_THREADED
67 #endif
70 #ifdef BYTE_CODE_METER
72 #define METER_2(code1, code2) AREF (AREF (Vbyte_code_meter, code1), code2)
73 #define METER_1(code) METER_2 (0, code)
75 #define METER_CODE(last_code, this_code) \
76 { \
77 if (byte_metering_on) \
78 { \
79 if (XFASTINT (METER_1 (this_code)) < MOST_POSITIVE_FIXNUM) \
80 XSETFASTINT (METER_1 (this_code), \
81 XFASTINT (METER_1 (this_code)) + 1); \
82 if (last_code \
83 && (XFASTINT (METER_2 (last_code, this_code)) \
84 < MOST_POSITIVE_FIXNUM)) \
85 XSETFASTINT (METER_2 (last_code, this_code), \
86 XFASTINT (METER_2 (last_code, this_code)) + 1); \
87 } \
90 #endif /* BYTE_CODE_METER */
93 /* Byte codes: */
95 #define BYTE_CODES \
96 DEFINE (Bstack_ref, 0) /* Actually, Bstack_ref+0 is not implemented: use dup. */ \
97 DEFINE (Bstack_ref1, 1) \
98 DEFINE (Bstack_ref2, 2) \
99 DEFINE (Bstack_ref3, 3) \
100 DEFINE (Bstack_ref4, 4) \
101 DEFINE (Bstack_ref5, 5) \
102 DEFINE (Bstack_ref6, 6) \
103 DEFINE (Bstack_ref7, 7) \
104 DEFINE (Bvarref, 010) \
105 DEFINE (Bvarref1, 011) \
106 DEFINE (Bvarref2, 012) \
107 DEFINE (Bvarref3, 013) \
108 DEFINE (Bvarref4, 014) \
109 DEFINE (Bvarref5, 015) \
110 DEFINE (Bvarref6, 016) \
111 DEFINE (Bvarref7, 017) \
112 DEFINE (Bvarset, 020) \
113 DEFINE (Bvarset1, 021) \
114 DEFINE (Bvarset2, 022) \
115 DEFINE (Bvarset3, 023) \
116 DEFINE (Bvarset4, 024) \
117 DEFINE (Bvarset5, 025) \
118 DEFINE (Bvarset6, 026) \
119 DEFINE (Bvarset7, 027) \
120 DEFINE (Bvarbind, 030) \
121 DEFINE (Bvarbind1, 031) \
122 DEFINE (Bvarbind2, 032) \
123 DEFINE (Bvarbind3, 033) \
124 DEFINE (Bvarbind4, 034) \
125 DEFINE (Bvarbind5, 035) \
126 DEFINE (Bvarbind6, 036) \
127 DEFINE (Bvarbind7, 037) \
128 DEFINE (Bcall, 040) \
129 DEFINE (Bcall1, 041) \
130 DEFINE (Bcall2, 042) \
131 DEFINE (Bcall3, 043) \
132 DEFINE (Bcall4, 044) \
133 DEFINE (Bcall5, 045) \
134 DEFINE (Bcall6, 046) \
135 DEFINE (Bcall7, 047) \
136 DEFINE (Bunbind, 050) \
137 DEFINE (Bunbind1, 051) \
138 DEFINE (Bunbind2, 052) \
139 DEFINE (Bunbind3, 053) \
140 DEFINE (Bunbind4, 054) \
141 DEFINE (Bunbind5, 055) \
142 DEFINE (Bunbind6, 056) \
143 DEFINE (Bunbind7, 057) \
145 DEFINE (Bpophandler, 060) \
146 DEFINE (Bpushconditioncase, 061) \
147 DEFINE (Bpushcatch, 062) \
149 DEFINE (Bnth, 070) \
150 DEFINE (Bsymbolp, 071) \
151 DEFINE (Bconsp, 072) \
152 DEFINE (Bstringp, 073) \
153 DEFINE (Blistp, 074) \
154 DEFINE (Beq, 075) \
155 DEFINE (Bmemq, 076) \
156 DEFINE (Bnot, 077) \
157 DEFINE (Bcar, 0100) \
158 DEFINE (Bcdr, 0101) \
159 DEFINE (Bcons, 0102) \
160 DEFINE (Blist1, 0103) \
161 DEFINE (Blist2, 0104) \
162 DEFINE (Blist3, 0105) \
163 DEFINE (Blist4, 0106) \
164 DEFINE (Blength, 0107) \
165 DEFINE (Baref, 0110) \
166 DEFINE (Baset, 0111) \
167 DEFINE (Bsymbol_value, 0112) \
168 DEFINE (Bsymbol_function, 0113) \
169 DEFINE (Bset, 0114) \
170 DEFINE (Bfset, 0115) \
171 DEFINE (Bget, 0116) \
172 DEFINE (Bsubstring, 0117) \
173 DEFINE (Bconcat2, 0120) \
174 DEFINE (Bconcat3, 0121) \
175 DEFINE (Bconcat4, 0122) \
176 DEFINE (Bsub1, 0123) \
177 DEFINE (Badd1, 0124) \
178 DEFINE (Beqlsign, 0125) \
179 DEFINE (Bgtr, 0126) \
180 DEFINE (Blss, 0127) \
181 DEFINE (Bleq, 0130) \
182 DEFINE (Bgeq, 0131) \
183 DEFINE (Bdiff, 0132) \
184 DEFINE (Bnegate, 0133) \
185 DEFINE (Bplus, 0134) \
186 DEFINE (Bmax, 0135) \
187 DEFINE (Bmin, 0136) \
188 DEFINE (Bmult, 0137) \
190 DEFINE (Bpoint, 0140) \
191 /* Was Bmark in v17. */ \
192 DEFINE (Bsave_current_buffer, 0141) /* Obsolete. */ \
193 DEFINE (Bgoto_char, 0142) \
194 DEFINE (Binsert, 0143) \
195 DEFINE (Bpoint_max, 0144) \
196 DEFINE (Bpoint_min, 0145) \
197 DEFINE (Bchar_after, 0146) \
198 DEFINE (Bfollowing_char, 0147) \
199 DEFINE (Bpreceding_char, 0150) \
200 DEFINE (Bcurrent_column, 0151) \
201 DEFINE (Bindent_to, 0152) \
202 DEFINE (Beolp, 0154) \
203 DEFINE (Beobp, 0155) \
204 DEFINE (Bbolp, 0156) \
205 DEFINE (Bbobp, 0157) \
206 DEFINE (Bcurrent_buffer, 0160) \
207 DEFINE (Bset_buffer, 0161) \
208 DEFINE (Bsave_current_buffer_1, 0162) /* Replacing Bsave_current_buffer. */ \
209 DEFINE (Binteractive_p, 0164) /* Obsolete since Emacs-24.1. */ \
211 DEFINE (Bforward_char, 0165) \
212 DEFINE (Bforward_word, 0166) \
213 DEFINE (Bskip_chars_forward, 0167) \
214 DEFINE (Bskip_chars_backward, 0170) \
215 DEFINE (Bforward_line, 0171) \
216 DEFINE (Bchar_syntax, 0172) \
217 DEFINE (Bbuffer_substring, 0173) \
218 DEFINE (Bdelete_region, 0174) \
219 DEFINE (Bnarrow_to_region, 0175) \
220 DEFINE (Bwiden, 0176) \
221 DEFINE (Bend_of_line, 0177) \
223 DEFINE (Bconstant2, 0201) \
224 DEFINE (Bgoto, 0202) \
225 DEFINE (Bgotoifnil, 0203) \
226 DEFINE (Bgotoifnonnil, 0204) \
227 DEFINE (Bgotoifnilelsepop, 0205) \
228 DEFINE (Bgotoifnonnilelsepop, 0206) \
229 DEFINE (Breturn, 0207) \
230 DEFINE (Bdiscard, 0210) \
231 DEFINE (Bdup, 0211) \
233 DEFINE (Bsave_excursion, 0212) \
234 DEFINE (Bsave_window_excursion, 0213) /* Obsolete since Emacs-24.1. */ \
235 DEFINE (Bsave_restriction, 0214) \
236 DEFINE (Bcatch, 0215) \
238 DEFINE (Bunwind_protect, 0216) \
239 DEFINE (Bcondition_case, 0217) \
240 DEFINE (Btemp_output_buffer_setup, 0220) /* Obsolete since Emacs-24.1. */ \
241 DEFINE (Btemp_output_buffer_show, 0221) /* Obsolete since Emacs-24.1. */ \
243 DEFINE (Bunbind_all, 0222) /* Obsolete. Never used. */ \
245 DEFINE (Bset_marker, 0223) \
246 DEFINE (Bmatch_beginning, 0224) \
247 DEFINE (Bmatch_end, 0225) \
248 DEFINE (Bupcase, 0226) \
249 DEFINE (Bdowncase, 0227) \
251 DEFINE (Bstringeqlsign, 0230) \
252 DEFINE (Bstringlss, 0231) \
253 DEFINE (Bequal, 0232) \
254 DEFINE (Bnthcdr, 0233) \
255 DEFINE (Belt, 0234) \
256 DEFINE (Bmember, 0235) \
257 DEFINE (Bassq, 0236) \
258 DEFINE (Bnreverse, 0237) \
259 DEFINE (Bsetcar, 0240) \
260 DEFINE (Bsetcdr, 0241) \
261 DEFINE (Bcar_safe, 0242) \
262 DEFINE (Bcdr_safe, 0243) \
263 DEFINE (Bnconc, 0244) \
264 DEFINE (Bquo, 0245) \
265 DEFINE (Brem, 0246) \
266 DEFINE (Bnumberp, 0247) \
267 DEFINE (Bintegerp, 0250) \
269 DEFINE (BRgoto, 0252) \
270 DEFINE (BRgotoifnil, 0253) \
271 DEFINE (BRgotoifnonnil, 0254) \
272 DEFINE (BRgotoifnilelsepop, 0255) \
273 DEFINE (BRgotoifnonnilelsepop, 0256) \
275 DEFINE (BlistN, 0257) \
276 DEFINE (BconcatN, 0260) \
277 DEFINE (BinsertN, 0261) \
279 /* Bstack_ref is code 0. */ \
280 DEFINE (Bstack_set, 0262) \
281 DEFINE (Bstack_set2, 0263) \
282 DEFINE (BdiscardN, 0266) \
284 DEFINE (Bconstant, 0300)
286 enum byte_code_op
288 #define DEFINE(name, value) name = value,
289 BYTE_CODES
290 #undef DEFINE
292 #ifdef BYTE_CODE_SAFE
293 Bscan_buffer = 0153, /* No longer generated as of v18. */
294 Bset_mark = 0163, /* this loser is no longer generated as of v18 */
295 #endif
298 /* Whether to maintain a `top' and `bottom' field in the stack frame. */
299 #define BYTE_MAINTAIN_TOP (BYTE_CODE_SAFE || BYTE_MARK_STACK)
301 /* Structure describing a value stack used during byte-code execution
302 in Fbyte_code. */
304 struct byte_stack
306 /* Program counter. This points into the byte_string below
307 and is relocated when that string is relocated. */
308 const unsigned char *pc;
310 /* Top and bottom of stack. The bottom points to an area of memory
311 allocated with alloca in Fbyte_code. */
312 #if BYTE_MAINTAIN_TOP
313 Lisp_Object *top, *bottom;
314 #endif
316 /* The string containing the byte-code, and its current address.
317 Storing this here protects it from GC because mark_byte_stack
318 marks it. */
319 Lisp_Object byte_string;
320 const unsigned char *byte_string_start;
322 #if BYTE_MARK_STACK
323 /* The vector of constants used during byte-code execution. Storing
324 this here protects it from GC because mark_byte_stack marks it. */
325 Lisp_Object constants;
326 #endif
328 /* Next entry in byte_stack_list. */
329 struct byte_stack *next;
332 /* A list of currently active byte-code execution value stacks.
333 Fbyte_code adds an entry to the head of this list before it starts
334 processing byte-code, and it removes the entry again when it is
335 done. Signaling an error truncates the list analogous to
336 gcprolist. */
338 struct byte_stack *byte_stack_list;
341 /* Mark objects on byte_stack_list. Called during GC. */
343 #if BYTE_MARK_STACK
344 void
345 mark_byte_stack (void)
347 struct byte_stack *stack;
348 Lisp_Object *obj;
350 for (stack = byte_stack_list; stack; stack = stack->next)
352 /* If STACK->top is null here, this means there's an opcode in
353 Fbyte_code that wasn't expected to GC, but did. To find out
354 which opcode this is, record the value of `stack', and walk
355 up the stack in a debugger, stopping in frames of Fbyte_code.
356 The culprit is found in the frame of Fbyte_code where the
357 address of its local variable `stack' is equal to the
358 recorded value of `stack' here. */
359 eassert (stack->top);
361 for (obj = stack->bottom; obj <= stack->top; ++obj)
362 mark_object (*obj);
364 mark_object (stack->byte_string);
365 mark_object (stack->constants);
368 #endif
370 /* Unmark objects in the stacks on byte_stack_list. Relocate program
371 counters. Called when GC has completed. */
373 void
374 unmark_byte_stack (void)
376 struct byte_stack *stack;
378 for (stack = byte_stack_list; stack; stack = stack->next)
380 if (stack->byte_string_start != SDATA (stack->byte_string))
382 ptrdiff_t offset = stack->pc - stack->byte_string_start;
383 stack->byte_string_start = SDATA (stack->byte_string);
384 stack->pc = stack->byte_string_start + offset;
390 /* Fetch the next byte from the bytecode stream. */
392 #ifdef BYTE_CODE_SAFE
393 #define FETCH (eassert (stack.byte_string_start == SDATA (stack.byte_string)), *stack.pc++)
394 #else
395 #define FETCH *stack.pc++
396 #endif
398 /* Fetch two bytes from the bytecode stream and make a 16-bit number
399 out of them. */
401 #define FETCH2 (op = FETCH, op + (FETCH << 8))
403 /* Push x onto the execution stack. This used to be #define PUSH(x)
404 (*++stackp = (x)) This oddity is necessary because Alliant can't be
405 bothered to compile the preincrement operator properly, as of 4/91.
406 -JimB */
408 #define PUSH(x) (top++, *top = (x))
410 /* Pop a value off the execution stack. */
412 #define POP (*top--)
414 /* Discard n values from the execution stack. */
416 #define DISCARD(n) (top -= (n))
418 /* Get the value which is at the top of the execution stack, but don't
419 pop it. */
421 #define TOP (*top)
423 /* Actions that must be performed before and after calling a function
424 that might GC. */
426 #if !BYTE_MAINTAIN_TOP
427 #define BEFORE_POTENTIAL_GC() ((void)0)
428 #define AFTER_POTENTIAL_GC() ((void)0)
429 #else
430 #define BEFORE_POTENTIAL_GC() stack.top = top
431 #define AFTER_POTENTIAL_GC() stack.top = NULL
432 #endif
434 /* Garbage collect if we have consed enough since the last time.
435 We do this at every branch, to avoid loops that never GC. */
437 #define MAYBE_GC() \
438 do { \
439 BEFORE_POTENTIAL_GC (); \
440 maybe_gc (); \
441 AFTER_POTENTIAL_GC (); \
442 } while (0)
444 /* Check for jumping out of range. */
446 #ifdef BYTE_CODE_SAFE
448 #define CHECK_RANGE(ARG) \
449 if (ARG >= bytestr_length) emacs_abort ()
451 #else /* not BYTE_CODE_SAFE */
453 #define CHECK_RANGE(ARG)
455 #endif /* not BYTE_CODE_SAFE */
457 /* A version of the QUIT macro which makes sure that the stack top is
458 set before signaling `quit'. */
460 #define BYTE_CODE_QUIT \
461 do { \
462 if (!NILP (Vquit_flag) && NILP (Vinhibit_quit)) \
464 Lisp_Object flag = Vquit_flag; \
465 Vquit_flag = Qnil; \
466 BEFORE_POTENTIAL_GC (); \
467 if (EQ (Vthrow_on_input, flag)) \
468 Fthrow (Vthrow_on_input, Qt); \
469 Fsignal (Qquit, Qnil); \
470 AFTER_POTENTIAL_GC (); \
472 else if (pending_signals) \
473 process_pending_signals (); \
474 } while (0)
477 DEFUN ("byte-code", Fbyte_code, Sbyte_code, 3, 3, 0,
478 doc: /* Function used internally in byte-compiled code.
479 The first argument, BYTESTR, is a string of byte code;
480 the second, VECTOR, a vector of constants;
481 the third, MAXDEPTH, the maximum stack depth used in this function.
482 If the third argument is incorrect, Emacs may crash. */)
483 (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth)
485 return exec_byte_code (bytestr, vector, maxdepth, Qnil, 0, NULL);
488 static void
489 bcall0 (Lisp_Object f)
491 Ffuncall (1, &f);
494 /* Execute the byte-code in BYTESTR. VECTOR is the constant vector, and
495 MAXDEPTH is the maximum stack depth used (if MAXDEPTH is incorrect,
496 emacs may crash!). If ARGS_TEMPLATE is non-nil, it should be a lisp
497 argument list (including &rest, &optional, etc.), and ARGS, of size
498 NARGS, should be a vector of the actual arguments. The arguments in
499 ARGS are pushed on the stack according to ARGS_TEMPLATE before
500 executing BYTESTR. */
502 Lisp_Object
503 exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
504 Lisp_Object args_template, ptrdiff_t nargs, Lisp_Object *args)
506 ptrdiff_t count = SPECPDL_INDEX ();
507 #ifdef BYTE_CODE_METER
508 int volatile this_op = 0;
509 int prev_op;
510 #endif
511 int op;
512 /* Lisp_Object v1, v2; */
513 Lisp_Object *vectorp;
514 #ifdef BYTE_CODE_SAFE
515 ptrdiff_t const_length;
516 Lisp_Object *stacke;
517 ptrdiff_t bytestr_length;
518 #endif
519 struct byte_stack stack;
520 Lisp_Object *top;
521 Lisp_Object result;
522 enum handlertype type;
524 #if 0 /* CHECK_FRAME_FONT */
526 struct frame *f = SELECTED_FRAME ();
527 if (FRAME_X_P (f)
528 && FRAME_FONT (f)->direction != 0
529 && FRAME_FONT (f)->direction != 1)
530 emacs_abort ();
532 #endif
534 CHECK_STRING (bytestr);
535 CHECK_VECTOR (vector);
536 CHECK_NATNUM (maxdepth);
538 #ifdef BYTE_CODE_SAFE
539 const_length = ASIZE (vector);
540 #endif
542 if (STRING_MULTIBYTE (bytestr))
543 /* BYTESTR must have been produced by Emacs 20.2 or the earlier
544 because they produced a raw 8-bit string for byte-code and now
545 such a byte-code string is loaded as multibyte while raw 8-bit
546 characters converted to multibyte form. Thus, now we must
547 convert them back to the originally intended unibyte form. */
548 bytestr = Fstring_as_unibyte (bytestr);
550 #ifdef BYTE_CODE_SAFE
551 bytestr_length = SBYTES (bytestr);
552 #endif
553 vectorp = XVECTOR (vector)->contents;
555 stack.byte_string = bytestr;
556 stack.pc = stack.byte_string_start = SDATA (bytestr);
557 #if BYTE_MARK_STACK
558 stack.constants = vector;
559 #endif
560 if (MAX_ALLOCA / word_size <= XFASTINT (maxdepth))
561 memory_full (SIZE_MAX);
562 top = alloca ((XFASTINT (maxdepth) + 1) * sizeof *top);
563 #if BYTE_MAINTAIN_TOP
564 stack.bottom = top + 1;
565 stack.top = NULL;
566 #endif
567 stack.next = byte_stack_list;
568 byte_stack_list = &stack;
570 #ifdef BYTE_CODE_SAFE
571 stacke = stack.bottom - 1 + XFASTINT (maxdepth);
572 #endif
574 if (INTEGERP (args_template))
576 ptrdiff_t at = XINT (args_template);
577 bool rest = (at & 128) != 0;
578 int mandatory = at & 127;
579 ptrdiff_t nonrest = at >> 8;
580 eassert (mandatory <= nonrest);
581 if (nargs <= nonrest)
583 ptrdiff_t i;
584 for (i = 0 ; i < nargs; i++, args++)
585 PUSH (*args);
586 if (nargs < mandatory)
587 /* Too few arguments. */
588 Fsignal (Qwrong_number_of_arguments,
589 list2 (Fcons (make_number (mandatory),
590 rest ? Qand_rest : make_number (nonrest)),
591 make_number (nargs)));
592 else
594 for (; i < nonrest; i++)
595 PUSH (Qnil);
596 if (rest)
597 PUSH (Qnil);
600 else if (rest)
602 ptrdiff_t i;
603 for (i = 0 ; i < nonrest; i++, args++)
604 PUSH (*args);
605 PUSH (Flist (nargs - nonrest, args));
607 else
608 /* Too many arguments. */
609 Fsignal (Qwrong_number_of_arguments,
610 list2 (Fcons (make_number (mandatory), make_number (nonrest)),
611 make_number (nargs)));
613 else if (! NILP (args_template))
614 /* We should push some arguments on the stack. */
616 error ("Unknown args template!");
619 while (1)
621 #ifdef BYTE_CODE_SAFE
622 if (top > stacke)
623 emacs_abort ();
624 else if (top < stack.bottom - 1)
625 emacs_abort ();
626 #endif
628 #ifdef BYTE_CODE_METER
629 prev_op = this_op;
630 this_op = op = FETCH;
631 METER_CODE (prev_op, op);
632 #else
633 #ifndef BYTE_CODE_THREADED
634 op = FETCH;
635 #endif
636 #endif
638 /* The interpreter can be compiled one of two ways: as an
639 ordinary switch-based interpreter, or as a threaded
640 interpreter. The threaded interpreter relies on GCC's
641 computed goto extension, so it is not available everywhere.
642 Threading provides a performance boost. These macros are how
643 we allow the code to be compiled both ways. */
644 #ifdef BYTE_CODE_THREADED
645 /* The CASE macro introduces an instruction's body. It is
646 either a label or a case label. */
647 #define CASE(OP) insn_ ## OP
648 /* NEXT is invoked at the end of an instruction to go to the
649 next instruction. It is either a computed goto, or a
650 plain break. */
651 #define NEXT goto *(targets[op = FETCH])
652 /* FIRST is like NEXT, but is only used at the start of the
653 interpreter body. In the switch-based interpreter it is the
654 switch, so the threaded definition must include a semicolon. */
655 #define FIRST NEXT;
656 /* Most cases are labeled with the CASE macro, above.
657 CASE_DEFAULT is one exception; it is used if the interpreter
658 being built requires a default case. The threaded
659 interpreter does not, because the dispatch table is
660 completely filled. */
661 #define CASE_DEFAULT
662 /* This introduces an instruction that is known to call abort. */
663 #define CASE_ABORT CASE (Bstack_ref): CASE (default)
664 #else
665 /* See above for the meaning of the various defines. */
666 #define CASE(OP) case OP
667 #define NEXT break
668 #define FIRST switch (op)
669 #define CASE_DEFAULT case 255: default:
670 #define CASE_ABORT case 0
671 #endif
673 #ifdef BYTE_CODE_THREADED
675 /* A convenience define that saves us a lot of typing and makes
676 the table clearer. */
677 #define LABEL(OP) [OP] = &&insn_ ## OP
679 #if 4 < __GNUC__ + (6 <= __GNUC_MINOR__)
680 # pragma GCC diagnostic push
681 # pragma GCC diagnostic ignored "-Woverride-init"
682 #elif defined __clang__
683 # pragma GCC diagnostic push
684 # pragma GCC diagnostic ignored "-Winitializer-overrides"
685 #endif
687 /* This is the dispatch table for the threaded interpreter. */
688 static const void *const targets[256] =
690 [0 ... (Bconstant - 1)] = &&insn_default,
691 [Bconstant ... 255] = &&insn_Bconstant,
693 #define DEFINE(name, value) LABEL (name) ,
694 BYTE_CODES
695 #undef DEFINE
698 #if 4 < __GNUC__ + (6 <= __GNUC_MINOR__) || defined __clang__
699 # pragma GCC diagnostic pop
700 #endif
702 #endif
705 FIRST
707 CASE (Bvarref7):
708 op = FETCH2;
709 goto varref;
711 CASE (Bvarref):
712 CASE (Bvarref1):
713 CASE (Bvarref2):
714 CASE (Bvarref3):
715 CASE (Bvarref4):
716 CASE (Bvarref5):
717 op = op - Bvarref;
718 goto varref;
720 /* This seems to be the most frequently executed byte-code
721 among the Bvarref's, so avoid a goto here. */
722 CASE (Bvarref6):
723 op = FETCH;
724 varref:
726 Lisp_Object v1, v2;
728 v1 = vectorp[op];
729 if (SYMBOLP (v1))
731 if (XSYMBOL (v1)->redirect != SYMBOL_PLAINVAL
732 || (v2 = SYMBOL_VAL (XSYMBOL (v1)),
733 EQ (v2, Qunbound)))
735 BEFORE_POTENTIAL_GC ();
736 v2 = Fsymbol_value (v1);
737 AFTER_POTENTIAL_GC ();
740 else
742 BEFORE_POTENTIAL_GC ();
743 v2 = Fsymbol_value (v1);
744 AFTER_POTENTIAL_GC ();
746 PUSH (v2);
747 NEXT;
750 CASE (Bgotoifnil):
752 Lisp_Object v1;
753 MAYBE_GC ();
754 op = FETCH2;
755 v1 = POP;
756 if (NILP (v1))
758 BYTE_CODE_QUIT;
759 CHECK_RANGE (op);
760 stack.pc = stack.byte_string_start + op;
762 NEXT;
765 CASE (Bcar):
767 Lisp_Object v1;
768 v1 = TOP;
769 if (CONSP (v1))
770 TOP = XCAR (v1);
771 else if (NILP (v1))
772 TOP = Qnil;
773 else
775 BEFORE_POTENTIAL_GC ();
776 wrong_type_argument (Qlistp, v1);
778 NEXT;
781 CASE (Beq):
783 Lisp_Object v1;
784 v1 = POP;
785 TOP = EQ (v1, TOP) ? Qt : Qnil;
786 NEXT;
789 CASE (Bmemq):
791 Lisp_Object v1;
792 BEFORE_POTENTIAL_GC ();
793 v1 = POP;
794 TOP = Fmemq (TOP, v1);
795 AFTER_POTENTIAL_GC ();
796 NEXT;
799 CASE (Bcdr):
801 Lisp_Object v1;
802 v1 = TOP;
803 if (CONSP (v1))
804 TOP = XCDR (v1);
805 else if (NILP (v1))
806 TOP = Qnil;
807 else
809 BEFORE_POTENTIAL_GC ();
810 wrong_type_argument (Qlistp, v1);
812 NEXT;
815 CASE (Bvarset):
816 CASE (Bvarset1):
817 CASE (Bvarset2):
818 CASE (Bvarset3):
819 CASE (Bvarset4):
820 CASE (Bvarset5):
821 op -= Bvarset;
822 goto varset;
824 CASE (Bvarset7):
825 op = FETCH2;
826 goto varset;
828 CASE (Bvarset6):
829 op = FETCH;
830 varset:
832 Lisp_Object sym, val;
834 sym = vectorp[op];
835 val = TOP;
837 /* Inline the most common case. */
838 if (SYMBOLP (sym)
839 && !EQ (val, Qunbound)
840 && !XSYMBOL (sym)->redirect
841 && !SYMBOL_CONSTANT_P (sym))
842 SET_SYMBOL_VAL (XSYMBOL (sym), val);
843 else
845 BEFORE_POTENTIAL_GC ();
846 set_internal (sym, val, Qnil, 0);
847 AFTER_POTENTIAL_GC ();
850 (void) POP;
851 NEXT;
853 CASE (Bdup):
855 Lisp_Object v1;
856 v1 = TOP;
857 PUSH (v1);
858 NEXT;
861 /* ------------------ */
863 CASE (Bvarbind6):
864 op = FETCH;
865 goto varbind;
867 CASE (Bvarbind7):
868 op = FETCH2;
869 goto varbind;
871 CASE (Bvarbind):
872 CASE (Bvarbind1):
873 CASE (Bvarbind2):
874 CASE (Bvarbind3):
875 CASE (Bvarbind4):
876 CASE (Bvarbind5):
877 op -= Bvarbind;
878 varbind:
879 /* Specbind can signal and thus GC. */
880 BEFORE_POTENTIAL_GC ();
881 specbind (vectorp[op], POP);
882 AFTER_POTENTIAL_GC ();
883 NEXT;
885 CASE (Bcall6):
886 op = FETCH;
887 goto docall;
889 CASE (Bcall7):
890 op = FETCH2;
891 goto docall;
893 CASE (Bcall):
894 CASE (Bcall1):
895 CASE (Bcall2):
896 CASE (Bcall3):
897 CASE (Bcall4):
898 CASE (Bcall5):
899 op -= Bcall;
900 docall:
902 BEFORE_POTENTIAL_GC ();
903 DISCARD (op);
904 #ifdef BYTE_CODE_METER
905 if (byte_metering_on && SYMBOLP (TOP))
907 Lisp_Object v1, v2;
909 v1 = TOP;
910 v2 = Fget (v1, Qbyte_code_meter);
911 if (INTEGERP (v2)
912 && XINT (v2) < MOST_POSITIVE_FIXNUM)
914 XSETINT (v2, XINT (v2) + 1);
915 Fput (v1, Qbyte_code_meter, v2);
918 #endif
919 TOP = Ffuncall (op + 1, &TOP);
920 AFTER_POTENTIAL_GC ();
921 NEXT;
924 CASE (Bunbind6):
925 op = FETCH;
926 goto dounbind;
928 CASE (Bunbind7):
929 op = FETCH2;
930 goto dounbind;
932 CASE (Bunbind):
933 CASE (Bunbind1):
934 CASE (Bunbind2):
935 CASE (Bunbind3):
936 CASE (Bunbind4):
937 CASE (Bunbind5):
938 op -= Bunbind;
939 dounbind:
940 BEFORE_POTENTIAL_GC ();
941 unbind_to (SPECPDL_INDEX () - op, Qnil);
942 AFTER_POTENTIAL_GC ();
943 NEXT;
945 CASE (Bunbind_all): /* Obsolete. Never used. */
946 /* To unbind back to the beginning of this frame. Not used yet,
947 but will be needed for tail-recursion elimination. */
948 BEFORE_POTENTIAL_GC ();
949 unbind_to (count, Qnil);
950 AFTER_POTENTIAL_GC ();
951 NEXT;
953 CASE (Bgoto):
954 MAYBE_GC ();
955 BYTE_CODE_QUIT;
956 op = FETCH2; /* pc = FETCH2 loses since FETCH2 contains pc++ */
957 CHECK_RANGE (op);
958 stack.pc = stack.byte_string_start + op;
959 NEXT;
961 CASE (Bgotoifnonnil):
963 Lisp_Object v1;
964 MAYBE_GC ();
965 op = FETCH2;
966 v1 = POP;
967 if (!NILP (v1))
969 BYTE_CODE_QUIT;
970 CHECK_RANGE (op);
971 stack.pc = stack.byte_string_start + op;
973 NEXT;
976 CASE (Bgotoifnilelsepop):
977 MAYBE_GC ();
978 op = FETCH2;
979 if (NILP (TOP))
981 BYTE_CODE_QUIT;
982 CHECK_RANGE (op);
983 stack.pc = stack.byte_string_start + op;
985 else DISCARD (1);
986 NEXT;
988 CASE (Bgotoifnonnilelsepop):
989 MAYBE_GC ();
990 op = FETCH2;
991 if (!NILP (TOP))
993 BYTE_CODE_QUIT;
994 CHECK_RANGE (op);
995 stack.pc = stack.byte_string_start + op;
997 else DISCARD (1);
998 NEXT;
1000 CASE (BRgoto):
1001 MAYBE_GC ();
1002 BYTE_CODE_QUIT;
1003 stack.pc += (int) *stack.pc - 127;
1004 NEXT;
1006 CASE (BRgotoifnil):
1008 Lisp_Object v1;
1009 MAYBE_GC ();
1010 v1 = POP;
1011 if (NILP (v1))
1013 BYTE_CODE_QUIT;
1014 stack.pc += (int) *stack.pc - 128;
1016 stack.pc++;
1017 NEXT;
1020 CASE (BRgotoifnonnil):
1022 Lisp_Object v1;
1023 MAYBE_GC ();
1024 v1 = POP;
1025 if (!NILP (v1))
1027 BYTE_CODE_QUIT;
1028 stack.pc += (int) *stack.pc - 128;
1030 stack.pc++;
1031 NEXT;
1034 CASE (BRgotoifnilelsepop):
1035 MAYBE_GC ();
1036 op = *stack.pc++;
1037 if (NILP (TOP))
1039 BYTE_CODE_QUIT;
1040 stack.pc += op - 128;
1042 else DISCARD (1);
1043 NEXT;
1045 CASE (BRgotoifnonnilelsepop):
1046 MAYBE_GC ();
1047 op = *stack.pc++;
1048 if (!NILP (TOP))
1050 BYTE_CODE_QUIT;
1051 stack.pc += op - 128;
1053 else DISCARD (1);
1054 NEXT;
1056 CASE (Breturn):
1057 result = POP;
1058 goto exit;
1060 CASE (Bdiscard):
1061 DISCARD (1);
1062 NEXT;
1064 CASE (Bconstant2):
1065 PUSH (vectorp[FETCH2]);
1066 NEXT;
1068 CASE (Bsave_excursion):
1069 record_unwind_protect (save_excursion_restore,
1070 save_excursion_save ());
1071 NEXT;
1073 CASE (Bsave_current_buffer): /* Obsolete since ??. */
1074 CASE (Bsave_current_buffer_1):
1075 record_unwind_current_buffer ();
1076 NEXT;
1078 CASE (Bsave_window_excursion): /* Obsolete since 24.1. */
1080 ptrdiff_t count1 = SPECPDL_INDEX ();
1081 record_unwind_protect (restore_window_configuration,
1082 Fcurrent_window_configuration (Qnil));
1083 BEFORE_POTENTIAL_GC ();
1084 TOP = Fprogn (TOP);
1085 unbind_to (count1, TOP);
1086 AFTER_POTENTIAL_GC ();
1087 NEXT;
1090 CASE (Bsave_restriction):
1091 record_unwind_protect (save_restriction_restore,
1092 save_restriction_save ());
1093 NEXT;
1095 CASE (Bcatch): /* Obsolete since 24.4. */
1097 Lisp_Object v1;
1098 BEFORE_POTENTIAL_GC ();
1099 v1 = POP;
1100 TOP = internal_catch (TOP, eval_sub, v1);
1101 AFTER_POTENTIAL_GC ();
1102 NEXT;
1105 CASE (Bpushcatch): /* New in 24.4. */
1106 type = CATCHER;
1107 goto pushhandler;
1108 CASE (Bpushconditioncase): /* New in 24.4. */
1110 struct handler *c;
1111 Lisp_Object tag;
1112 int dest;
1114 type = CONDITION_CASE;
1115 pushhandler:
1116 tag = POP;
1117 dest = FETCH2;
1119 PUSH_HANDLER (c, tag, type);
1120 c->bytecode_dest = dest;
1121 c->bytecode_top = top;
1123 if (sys_setjmp (c->jmp))
1125 struct handler *c = handlerlist;
1126 int dest;
1127 top = c->bytecode_top;
1128 dest = c->bytecode_dest;
1129 handlerlist = c->next;
1130 PUSH (c->val);
1131 CHECK_RANGE (dest);
1132 /* Might have been re-set by longjmp! */
1133 stack.byte_string_start = SDATA (stack.byte_string);
1134 stack.pc = stack.byte_string_start + dest;
1137 NEXT;
1140 CASE (Bpophandler): /* New in 24.4. */
1142 handlerlist = handlerlist->next;
1143 NEXT;
1146 CASE (Bunwind_protect): /* FIXME: avoid closure for lexbind. */
1148 Lisp_Object handler = POP;
1149 /* Support for a function here is new in 24.4. */
1150 record_unwind_protect (NILP (Ffunctionp (handler))
1151 ? unwind_body : bcall0,
1152 handler);
1153 NEXT;
1156 CASE (Bcondition_case): /* Obsolete since 24.4. */
1158 Lisp_Object handlers, body;
1159 handlers = POP;
1160 body = POP;
1161 BEFORE_POTENTIAL_GC ();
1162 TOP = internal_lisp_condition_case (TOP, body, handlers);
1163 AFTER_POTENTIAL_GC ();
1164 NEXT;
1167 CASE (Btemp_output_buffer_setup): /* Obsolete since 24.1. */
1168 BEFORE_POTENTIAL_GC ();
1169 CHECK_STRING (TOP);
1170 temp_output_buffer_setup (SSDATA (TOP));
1171 AFTER_POTENTIAL_GC ();
1172 TOP = Vstandard_output;
1173 NEXT;
1175 CASE (Btemp_output_buffer_show): /* Obsolete since 24.1. */
1177 Lisp_Object v1;
1178 BEFORE_POTENTIAL_GC ();
1179 v1 = POP;
1180 temp_output_buffer_show (TOP);
1181 TOP = v1;
1182 /* pop binding of standard-output */
1183 unbind_to (SPECPDL_INDEX () - 1, Qnil);
1184 AFTER_POTENTIAL_GC ();
1185 NEXT;
1188 CASE (Bnth):
1190 Lisp_Object v1, v2;
1191 EMACS_INT n;
1192 BEFORE_POTENTIAL_GC ();
1193 v1 = POP;
1194 v2 = TOP;
1195 CHECK_NUMBER (v2);
1196 n = XINT (v2);
1197 immediate_quit = 1;
1198 while (--n >= 0 && CONSP (v1))
1199 v1 = XCDR (v1);
1200 immediate_quit = 0;
1201 TOP = CAR (v1);
1202 AFTER_POTENTIAL_GC ();
1203 NEXT;
1206 CASE (Bsymbolp):
1207 TOP = SYMBOLP (TOP) ? Qt : Qnil;
1208 NEXT;
1210 CASE (Bconsp):
1211 TOP = CONSP (TOP) ? Qt : Qnil;
1212 NEXT;
1214 CASE (Bstringp):
1215 TOP = STRINGP (TOP) ? Qt : Qnil;
1216 NEXT;
1218 CASE (Blistp):
1219 TOP = CONSP (TOP) || NILP (TOP) ? Qt : Qnil;
1220 NEXT;
1222 CASE (Bnot):
1223 TOP = NILP (TOP) ? Qt : Qnil;
1224 NEXT;
1226 CASE (Bcons):
1228 Lisp_Object v1;
1229 v1 = POP;
1230 TOP = Fcons (TOP, v1);
1231 NEXT;
1234 CASE (Blist1):
1235 TOP = list1 (TOP);
1236 NEXT;
1238 CASE (Blist2):
1240 Lisp_Object v1;
1241 v1 = POP;
1242 TOP = list2 (TOP, v1);
1243 NEXT;
1246 CASE (Blist3):
1247 DISCARD (2);
1248 TOP = Flist (3, &TOP);
1249 NEXT;
1251 CASE (Blist4):
1252 DISCARD (3);
1253 TOP = Flist (4, &TOP);
1254 NEXT;
1256 CASE (BlistN):
1257 op = FETCH;
1258 DISCARD (op - 1);
1259 TOP = Flist (op, &TOP);
1260 NEXT;
1262 CASE (Blength):
1263 BEFORE_POTENTIAL_GC ();
1264 TOP = Flength (TOP);
1265 AFTER_POTENTIAL_GC ();
1266 NEXT;
1268 CASE (Baref):
1270 Lisp_Object v1;
1271 BEFORE_POTENTIAL_GC ();
1272 v1 = POP;
1273 TOP = Faref (TOP, v1);
1274 AFTER_POTENTIAL_GC ();
1275 NEXT;
1278 CASE (Baset):
1280 Lisp_Object v1, v2;
1281 BEFORE_POTENTIAL_GC ();
1282 v2 = POP; v1 = POP;
1283 TOP = Faset (TOP, v1, v2);
1284 AFTER_POTENTIAL_GC ();
1285 NEXT;
1288 CASE (Bsymbol_value):
1289 BEFORE_POTENTIAL_GC ();
1290 TOP = Fsymbol_value (TOP);
1291 AFTER_POTENTIAL_GC ();
1292 NEXT;
1294 CASE (Bsymbol_function):
1295 BEFORE_POTENTIAL_GC ();
1296 TOP = Fsymbol_function (TOP);
1297 AFTER_POTENTIAL_GC ();
1298 NEXT;
1300 CASE (Bset):
1302 Lisp_Object v1;
1303 BEFORE_POTENTIAL_GC ();
1304 v1 = POP;
1305 TOP = Fset (TOP, v1);
1306 AFTER_POTENTIAL_GC ();
1307 NEXT;
1310 CASE (Bfset):
1312 Lisp_Object v1;
1313 BEFORE_POTENTIAL_GC ();
1314 v1 = POP;
1315 TOP = Ffset (TOP, v1);
1316 AFTER_POTENTIAL_GC ();
1317 NEXT;
1320 CASE (Bget):
1322 Lisp_Object v1;
1323 BEFORE_POTENTIAL_GC ();
1324 v1 = POP;
1325 TOP = Fget (TOP, v1);
1326 AFTER_POTENTIAL_GC ();
1327 NEXT;
1330 CASE (Bsubstring):
1332 Lisp_Object v1, v2;
1333 BEFORE_POTENTIAL_GC ();
1334 v2 = POP; v1 = POP;
1335 TOP = Fsubstring (TOP, v1, v2);
1336 AFTER_POTENTIAL_GC ();
1337 NEXT;
1340 CASE (Bconcat2):
1341 BEFORE_POTENTIAL_GC ();
1342 DISCARD (1);
1343 TOP = Fconcat (2, &TOP);
1344 AFTER_POTENTIAL_GC ();
1345 NEXT;
1347 CASE (Bconcat3):
1348 BEFORE_POTENTIAL_GC ();
1349 DISCARD (2);
1350 TOP = Fconcat (3, &TOP);
1351 AFTER_POTENTIAL_GC ();
1352 NEXT;
1354 CASE (Bconcat4):
1355 BEFORE_POTENTIAL_GC ();
1356 DISCARD (3);
1357 TOP = Fconcat (4, &TOP);
1358 AFTER_POTENTIAL_GC ();
1359 NEXT;
1361 CASE (BconcatN):
1362 op = FETCH;
1363 BEFORE_POTENTIAL_GC ();
1364 DISCARD (op - 1);
1365 TOP = Fconcat (op, &TOP);
1366 AFTER_POTENTIAL_GC ();
1367 NEXT;
1369 CASE (Bsub1):
1371 Lisp_Object v1;
1372 v1 = TOP;
1373 if (INTEGERP (v1))
1375 XSETINT (v1, XINT (v1) - 1);
1376 TOP = v1;
1378 else
1380 BEFORE_POTENTIAL_GC ();
1381 TOP = Fsub1 (v1);
1382 AFTER_POTENTIAL_GC ();
1384 NEXT;
1387 CASE (Badd1):
1389 Lisp_Object v1;
1390 v1 = TOP;
1391 if (INTEGERP (v1))
1393 XSETINT (v1, XINT (v1) + 1);
1394 TOP = v1;
1396 else
1398 BEFORE_POTENTIAL_GC ();
1399 TOP = Fadd1 (v1);
1400 AFTER_POTENTIAL_GC ();
1402 NEXT;
1405 CASE (Beqlsign):
1407 Lisp_Object v1, v2;
1408 BEFORE_POTENTIAL_GC ();
1409 v2 = POP; v1 = TOP;
1410 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v1);
1411 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v2);
1412 AFTER_POTENTIAL_GC ();
1413 if (FLOATP (v1) || FLOATP (v2))
1415 double f1, f2;
1417 f1 = (FLOATP (v1) ? XFLOAT_DATA (v1) : XINT (v1));
1418 f2 = (FLOATP (v2) ? XFLOAT_DATA (v2) : XINT (v2));
1419 TOP = (f1 == f2 ? Qt : Qnil);
1421 else
1422 TOP = (XINT (v1) == XINT (v2) ? Qt : Qnil);
1423 NEXT;
1426 CASE (Bgtr):
1428 Lisp_Object v1;
1429 BEFORE_POTENTIAL_GC ();
1430 v1 = POP;
1431 TOP = arithcompare (TOP, v1, ARITH_GRTR);
1432 AFTER_POTENTIAL_GC ();
1433 NEXT;
1436 CASE (Blss):
1438 Lisp_Object v1;
1439 BEFORE_POTENTIAL_GC ();
1440 v1 = POP;
1441 TOP = arithcompare (TOP, v1, ARITH_LESS);
1442 AFTER_POTENTIAL_GC ();
1443 NEXT;
1446 CASE (Bleq):
1448 Lisp_Object v1;
1449 BEFORE_POTENTIAL_GC ();
1450 v1 = POP;
1451 TOP = arithcompare (TOP, v1, ARITH_LESS_OR_EQUAL);
1452 AFTER_POTENTIAL_GC ();
1453 NEXT;
1456 CASE (Bgeq):
1458 Lisp_Object v1;
1459 BEFORE_POTENTIAL_GC ();
1460 v1 = POP;
1461 TOP = arithcompare (TOP, v1, ARITH_GRTR_OR_EQUAL);
1462 AFTER_POTENTIAL_GC ();
1463 NEXT;
1466 CASE (Bdiff):
1467 BEFORE_POTENTIAL_GC ();
1468 DISCARD (1);
1469 TOP = Fminus (2, &TOP);
1470 AFTER_POTENTIAL_GC ();
1471 NEXT;
1473 CASE (Bnegate):
1475 Lisp_Object v1;
1476 v1 = TOP;
1477 if (INTEGERP (v1))
1479 XSETINT (v1, - XINT (v1));
1480 TOP = v1;
1482 else
1484 BEFORE_POTENTIAL_GC ();
1485 TOP = Fminus (1, &TOP);
1486 AFTER_POTENTIAL_GC ();
1488 NEXT;
1491 CASE (Bplus):
1492 BEFORE_POTENTIAL_GC ();
1493 DISCARD (1);
1494 TOP = Fplus (2, &TOP);
1495 AFTER_POTENTIAL_GC ();
1496 NEXT;
1498 CASE (Bmax):
1499 BEFORE_POTENTIAL_GC ();
1500 DISCARD (1);
1501 TOP = Fmax (2, &TOP);
1502 AFTER_POTENTIAL_GC ();
1503 NEXT;
1505 CASE (Bmin):
1506 BEFORE_POTENTIAL_GC ();
1507 DISCARD (1);
1508 TOP = Fmin (2, &TOP);
1509 AFTER_POTENTIAL_GC ();
1510 NEXT;
1512 CASE (Bmult):
1513 BEFORE_POTENTIAL_GC ();
1514 DISCARD (1);
1515 TOP = Ftimes (2, &TOP);
1516 AFTER_POTENTIAL_GC ();
1517 NEXT;
1519 CASE (Bquo):
1520 BEFORE_POTENTIAL_GC ();
1521 DISCARD (1);
1522 TOP = Fquo (2, &TOP);
1523 AFTER_POTENTIAL_GC ();
1524 NEXT;
1526 CASE (Brem):
1528 Lisp_Object v1;
1529 BEFORE_POTENTIAL_GC ();
1530 v1 = POP;
1531 TOP = Frem (TOP, v1);
1532 AFTER_POTENTIAL_GC ();
1533 NEXT;
1536 CASE (Bpoint):
1538 Lisp_Object v1;
1539 XSETFASTINT (v1, PT);
1540 PUSH (v1);
1541 NEXT;
1544 CASE (Bgoto_char):
1545 BEFORE_POTENTIAL_GC ();
1546 TOP = Fgoto_char (TOP);
1547 AFTER_POTENTIAL_GC ();
1548 NEXT;
1550 CASE (Binsert):
1551 BEFORE_POTENTIAL_GC ();
1552 TOP = Finsert (1, &TOP);
1553 AFTER_POTENTIAL_GC ();
1554 NEXT;
1556 CASE (BinsertN):
1557 op = FETCH;
1558 BEFORE_POTENTIAL_GC ();
1559 DISCARD (op - 1);
1560 TOP = Finsert (op, &TOP);
1561 AFTER_POTENTIAL_GC ();
1562 NEXT;
1564 CASE (Bpoint_max):
1566 Lisp_Object v1;
1567 XSETFASTINT (v1, ZV);
1568 PUSH (v1);
1569 NEXT;
1572 CASE (Bpoint_min):
1574 Lisp_Object v1;
1575 XSETFASTINT (v1, BEGV);
1576 PUSH (v1);
1577 NEXT;
1580 CASE (Bchar_after):
1581 BEFORE_POTENTIAL_GC ();
1582 TOP = Fchar_after (TOP);
1583 AFTER_POTENTIAL_GC ();
1584 NEXT;
1586 CASE (Bfollowing_char):
1588 Lisp_Object v1;
1589 BEFORE_POTENTIAL_GC ();
1590 v1 = Ffollowing_char ();
1591 AFTER_POTENTIAL_GC ();
1592 PUSH (v1);
1593 NEXT;
1596 CASE (Bpreceding_char):
1598 Lisp_Object v1;
1599 BEFORE_POTENTIAL_GC ();
1600 v1 = Fprevious_char ();
1601 AFTER_POTENTIAL_GC ();
1602 PUSH (v1);
1603 NEXT;
1606 CASE (Bcurrent_column):
1608 Lisp_Object v1;
1609 BEFORE_POTENTIAL_GC ();
1610 XSETFASTINT (v1, current_column ());
1611 AFTER_POTENTIAL_GC ();
1612 PUSH (v1);
1613 NEXT;
1616 CASE (Bindent_to):
1617 BEFORE_POTENTIAL_GC ();
1618 TOP = Findent_to (TOP, Qnil);
1619 AFTER_POTENTIAL_GC ();
1620 NEXT;
1622 CASE (Beolp):
1623 PUSH (Feolp ());
1624 NEXT;
1626 CASE (Beobp):
1627 PUSH (Feobp ());
1628 NEXT;
1630 CASE (Bbolp):
1631 PUSH (Fbolp ());
1632 NEXT;
1634 CASE (Bbobp):
1635 PUSH (Fbobp ());
1636 NEXT;
1638 CASE (Bcurrent_buffer):
1639 PUSH (Fcurrent_buffer ());
1640 NEXT;
1642 CASE (Bset_buffer):
1643 BEFORE_POTENTIAL_GC ();
1644 TOP = Fset_buffer (TOP);
1645 AFTER_POTENTIAL_GC ();
1646 NEXT;
1648 CASE (Binteractive_p): /* Obsolete since 24.1. */
1649 BEFORE_POTENTIAL_GC ();
1650 PUSH (call0 (intern ("interactive-p")));
1651 AFTER_POTENTIAL_GC ();
1652 NEXT;
1654 CASE (Bforward_char):
1655 BEFORE_POTENTIAL_GC ();
1656 TOP = Fforward_char (TOP);
1657 AFTER_POTENTIAL_GC ();
1658 NEXT;
1660 CASE (Bforward_word):
1661 BEFORE_POTENTIAL_GC ();
1662 TOP = Fforward_word (TOP);
1663 AFTER_POTENTIAL_GC ();
1664 NEXT;
1666 CASE (Bskip_chars_forward):
1668 Lisp_Object v1;
1669 BEFORE_POTENTIAL_GC ();
1670 v1 = POP;
1671 TOP = Fskip_chars_forward (TOP, v1);
1672 AFTER_POTENTIAL_GC ();
1673 NEXT;
1676 CASE (Bskip_chars_backward):
1678 Lisp_Object v1;
1679 BEFORE_POTENTIAL_GC ();
1680 v1 = POP;
1681 TOP = Fskip_chars_backward (TOP, v1);
1682 AFTER_POTENTIAL_GC ();
1683 NEXT;
1686 CASE (Bforward_line):
1687 BEFORE_POTENTIAL_GC ();
1688 TOP = Fforward_line (TOP);
1689 AFTER_POTENTIAL_GC ();
1690 NEXT;
1692 CASE (Bchar_syntax):
1694 int c;
1696 BEFORE_POTENTIAL_GC ();
1697 CHECK_CHARACTER (TOP);
1698 AFTER_POTENTIAL_GC ();
1699 c = XFASTINT (TOP);
1700 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1701 MAKE_CHAR_MULTIBYTE (c);
1702 XSETFASTINT (TOP, syntax_code_spec[SYNTAX (c)]);
1704 NEXT;
1706 CASE (Bbuffer_substring):
1708 Lisp_Object v1;
1709 BEFORE_POTENTIAL_GC ();
1710 v1 = POP;
1711 TOP = Fbuffer_substring (TOP, v1);
1712 AFTER_POTENTIAL_GC ();
1713 NEXT;
1716 CASE (Bdelete_region):
1718 Lisp_Object v1;
1719 BEFORE_POTENTIAL_GC ();
1720 v1 = POP;
1721 TOP = Fdelete_region (TOP, v1);
1722 AFTER_POTENTIAL_GC ();
1723 NEXT;
1726 CASE (Bnarrow_to_region):
1728 Lisp_Object v1;
1729 BEFORE_POTENTIAL_GC ();
1730 v1 = POP;
1731 TOP = Fnarrow_to_region (TOP, v1);
1732 AFTER_POTENTIAL_GC ();
1733 NEXT;
1736 CASE (Bwiden):
1737 BEFORE_POTENTIAL_GC ();
1738 PUSH (Fwiden ());
1739 AFTER_POTENTIAL_GC ();
1740 NEXT;
1742 CASE (Bend_of_line):
1743 BEFORE_POTENTIAL_GC ();
1744 TOP = Fend_of_line (TOP);
1745 AFTER_POTENTIAL_GC ();
1746 NEXT;
1748 CASE (Bset_marker):
1750 Lisp_Object v1, v2;
1751 BEFORE_POTENTIAL_GC ();
1752 v1 = POP;
1753 v2 = POP;
1754 TOP = Fset_marker (TOP, v2, v1);
1755 AFTER_POTENTIAL_GC ();
1756 NEXT;
1759 CASE (Bmatch_beginning):
1760 BEFORE_POTENTIAL_GC ();
1761 TOP = Fmatch_beginning (TOP);
1762 AFTER_POTENTIAL_GC ();
1763 NEXT;
1765 CASE (Bmatch_end):
1766 BEFORE_POTENTIAL_GC ();
1767 TOP = Fmatch_end (TOP);
1768 AFTER_POTENTIAL_GC ();
1769 NEXT;
1771 CASE (Bupcase):
1772 BEFORE_POTENTIAL_GC ();
1773 TOP = Fupcase (TOP);
1774 AFTER_POTENTIAL_GC ();
1775 NEXT;
1777 CASE (Bdowncase):
1778 BEFORE_POTENTIAL_GC ();
1779 TOP = Fdowncase (TOP);
1780 AFTER_POTENTIAL_GC ();
1781 NEXT;
1783 CASE (Bstringeqlsign):
1785 Lisp_Object v1;
1786 BEFORE_POTENTIAL_GC ();
1787 v1 = POP;
1788 TOP = Fstring_equal (TOP, v1);
1789 AFTER_POTENTIAL_GC ();
1790 NEXT;
1793 CASE (Bstringlss):
1795 Lisp_Object v1;
1796 BEFORE_POTENTIAL_GC ();
1797 v1 = POP;
1798 TOP = Fstring_lessp (TOP, v1);
1799 AFTER_POTENTIAL_GC ();
1800 NEXT;
1803 CASE (Bequal):
1805 Lisp_Object v1;
1806 v1 = POP;
1807 TOP = Fequal (TOP, v1);
1808 NEXT;
1811 CASE (Bnthcdr):
1813 Lisp_Object v1;
1814 BEFORE_POTENTIAL_GC ();
1815 v1 = POP;
1816 TOP = Fnthcdr (TOP, v1);
1817 AFTER_POTENTIAL_GC ();
1818 NEXT;
1821 CASE (Belt):
1823 Lisp_Object v1, v2;
1824 if (CONSP (TOP))
1826 /* Exchange args and then do nth. */
1827 EMACS_INT n;
1828 BEFORE_POTENTIAL_GC ();
1829 v2 = POP;
1830 v1 = TOP;
1831 CHECK_NUMBER (v2);
1832 AFTER_POTENTIAL_GC ();
1833 n = XINT (v2);
1834 immediate_quit = 1;
1835 while (--n >= 0 && CONSP (v1))
1836 v1 = XCDR (v1);
1837 immediate_quit = 0;
1838 TOP = CAR (v1);
1840 else
1842 BEFORE_POTENTIAL_GC ();
1843 v1 = POP;
1844 TOP = Felt (TOP, v1);
1845 AFTER_POTENTIAL_GC ();
1847 NEXT;
1850 CASE (Bmember):
1852 Lisp_Object v1;
1853 BEFORE_POTENTIAL_GC ();
1854 v1 = POP;
1855 TOP = Fmember (TOP, v1);
1856 AFTER_POTENTIAL_GC ();
1857 NEXT;
1860 CASE (Bassq):
1862 Lisp_Object v1;
1863 BEFORE_POTENTIAL_GC ();
1864 v1 = POP;
1865 TOP = Fassq (TOP, v1);
1866 AFTER_POTENTIAL_GC ();
1867 NEXT;
1870 CASE (Bnreverse):
1871 BEFORE_POTENTIAL_GC ();
1872 TOP = Fnreverse (TOP);
1873 AFTER_POTENTIAL_GC ();
1874 NEXT;
1876 CASE (Bsetcar):
1878 Lisp_Object v1;
1879 BEFORE_POTENTIAL_GC ();
1880 v1 = POP;
1881 TOP = Fsetcar (TOP, v1);
1882 AFTER_POTENTIAL_GC ();
1883 NEXT;
1886 CASE (Bsetcdr):
1888 Lisp_Object v1;
1889 BEFORE_POTENTIAL_GC ();
1890 v1 = POP;
1891 TOP = Fsetcdr (TOP, v1);
1892 AFTER_POTENTIAL_GC ();
1893 NEXT;
1896 CASE (Bcar_safe):
1898 Lisp_Object v1;
1899 v1 = TOP;
1900 TOP = CAR_SAFE (v1);
1901 NEXT;
1904 CASE (Bcdr_safe):
1906 Lisp_Object v1;
1907 v1 = TOP;
1908 TOP = CDR_SAFE (v1);
1909 NEXT;
1912 CASE (Bnconc):
1913 BEFORE_POTENTIAL_GC ();
1914 DISCARD (1);
1915 TOP = Fnconc (2, &TOP);
1916 AFTER_POTENTIAL_GC ();
1917 NEXT;
1919 CASE (Bnumberp):
1920 TOP = (NUMBERP (TOP) ? Qt : Qnil);
1921 NEXT;
1923 CASE (Bintegerp):
1924 TOP = INTEGERP (TOP) ? Qt : Qnil;
1925 NEXT;
1927 #ifdef BYTE_CODE_SAFE
1928 /* These are intentionally written using 'case' syntax,
1929 because they are incompatible with the threaded
1930 interpreter. */
1932 case Bset_mark:
1933 BEFORE_POTENTIAL_GC ();
1934 error ("set-mark is an obsolete bytecode");
1935 AFTER_POTENTIAL_GC ();
1936 break;
1937 case Bscan_buffer:
1938 BEFORE_POTENTIAL_GC ();
1939 error ("scan-buffer is an obsolete bytecode");
1940 AFTER_POTENTIAL_GC ();
1941 break;
1942 #endif
1944 CASE_ABORT:
1945 /* Actually this is Bstack_ref with offset 0, but we use Bdup
1946 for that instead. */
1947 /* CASE (Bstack_ref): */
1948 call3 (Qerror,
1949 build_string ("Invalid byte opcode: op=%s, ptr=%d"),
1950 make_number (op),
1951 make_number ((stack.pc - 1) - stack.byte_string_start));
1953 /* Handy byte-codes for lexical binding. */
1954 CASE (Bstack_ref1):
1955 CASE (Bstack_ref2):
1956 CASE (Bstack_ref3):
1957 CASE (Bstack_ref4):
1958 CASE (Bstack_ref5):
1960 Lisp_Object *ptr = top - (op - Bstack_ref);
1961 PUSH (*ptr);
1962 NEXT;
1964 CASE (Bstack_ref6):
1966 Lisp_Object *ptr = top - (FETCH);
1967 PUSH (*ptr);
1968 NEXT;
1970 CASE (Bstack_ref7):
1972 Lisp_Object *ptr = top - (FETCH2);
1973 PUSH (*ptr);
1974 NEXT;
1976 CASE (Bstack_set):
1977 /* stack-set-0 = discard; stack-set-1 = discard-1-preserve-tos. */
1979 Lisp_Object *ptr = top - (FETCH);
1980 *ptr = POP;
1981 NEXT;
1983 CASE (Bstack_set2):
1985 Lisp_Object *ptr = top - (FETCH2);
1986 *ptr = POP;
1987 NEXT;
1989 CASE (BdiscardN):
1990 op = FETCH;
1991 if (op & 0x80)
1993 op &= 0x7F;
1994 top[-op] = TOP;
1996 DISCARD (op);
1997 NEXT;
1999 CASE_DEFAULT
2000 CASE (Bconstant):
2001 #ifdef BYTE_CODE_SAFE
2002 if (op < Bconstant)
2004 emacs_abort ();
2006 if ((op -= Bconstant) >= const_length)
2008 emacs_abort ();
2010 PUSH (vectorp[op]);
2011 #else
2012 PUSH (vectorp[op - Bconstant]);
2013 #endif
2014 NEXT;
2018 exit:
2020 byte_stack_list = byte_stack_list->next;
2022 /* Binds and unbinds are supposed to be compiled balanced. */
2023 if (SPECPDL_INDEX () != count)
2025 if (SPECPDL_INDEX () > count)
2026 unbind_to (count, Qnil);
2027 error ("binding stack not balanced (serious byte compiler bug)");
2030 return result;
2033 void
2034 syms_of_bytecode (void)
2036 defsubr (&Sbyte_code);
2038 #ifdef BYTE_CODE_METER
2040 DEFVAR_LISP ("byte-code-meter", Vbyte_code_meter,
2041 doc: /* A vector of vectors which holds a histogram of byte-code usage.
2042 \(aref (aref byte-code-meter 0) CODE) indicates how many times the byte
2043 opcode CODE has been executed.
2044 \(aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,
2045 indicates how many times the byte opcodes CODE1 and CODE2 have been
2046 executed in succession. */);
2048 DEFVAR_BOOL ("byte-metering-on", byte_metering_on,
2049 doc: /* If non-nil, keep profiling information on byte code usage.
2050 The variable byte-code-meter indicates how often each byte opcode is used.
2051 If a symbol has a property named `byte-code-meter' whose value is an
2052 integer, it is incremented each time that symbol's function is called. */);
2054 byte_metering_on = 0;
2055 Vbyte_code_meter = Fmake_vector (make_number (256), make_number (0));
2056 DEFSYM (Qbyte_code_meter, "byte-code-meter");
2058 int i = 256;
2059 while (i--)
2060 ASET (Vbyte_code_meter, i,
2061 Fmake_vector (make_number (256), make_number (0)));
2063 #endif