Merge from emacs-24; up to 2012-05-05T02:50:20Z!monnier@iro.umontreal.ca
[bpt/emacs.git] / src / bytecode.c
1 /* Execution of byte code produced by bytecomp.el.
2 Copyright (C) 1985-1988, 1993, 2000-2012 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19 /*
20 hacked on by jwz@lucid.com 17-jun-91
21 o added a compile-time switch to turn on simple sanity checking;
22 o put back the obsolete byte-codes for error-detection;
23 o added a new instruction, unbind_all, which I will use for
24 tail-recursion elimination;
25 o made temp_output_buffer_show be called with the right number
26 of args;
27 o made the new bytecodes be called with args in the right order;
28 o added metering support.
29
30 by Hallvard:
31 o added relative jump instructions;
32 o all conditionals now only do QUIT if they jump.
33 */
34
35 #include <config.h>
36 #include <setjmp.h>
37 #include "lisp.h"
38 #include "character.h"
39 #include "buffer.h"
40 #include "syntax.h"
41 #include "window.h"
42
43 #ifdef CHECK_FRAME_FONT
44 #include "frame.h"
45 #include "xterm.h"
46 #endif
47
48 /*
49 * define BYTE_CODE_SAFE to enable some minor sanity checking (useful for
50 * debugging the byte compiler...)
51 *
52 * define BYTE_CODE_METER to enable generation of a byte-op usage histogram.
53 */
54 /* #define BYTE_CODE_SAFE */
55 /* #define BYTE_CODE_METER */
56
57 /* If BYTE_CODE_THREADED is defined, then the interpreter will be
58 indirect threaded, using GCC's computed goto extension. This code,
59 as currently implemented, is incompatible with BYTE_CODE_SAFE and
60 BYTE_CODE_METER. */
61 #if defined (__GNUC__) && !defined (BYTE_CODE_SAFE) && !defined (BYTE_CODE_METER)
62 #define BYTE_CODE_THREADED
63 #endif
64
65 \f
66 #ifdef BYTE_CODE_METER
67
68 Lisp_Object Qbyte_code_meter;
69 #define METER_2(code1, code2) AREF (AREF (Vbyte_code_meter, code1), code2)
70 #define METER_1(code) METER_2 (0, code)
71
72 #define METER_CODE(last_code, this_code) \
73 { \
74 if (byte_metering_on) \
75 { \
76 if (XFASTINT (METER_1 (this_code)) < MOST_POSITIVE_FIXNUM) \
77 XSETFASTINT (METER_1 (this_code), \
78 XFASTINT (METER_1 (this_code)) + 1); \
79 if (last_code \
80 && (XFASTINT (METER_2 (last_code, this_code)) \
81 < MOST_POSITIVE_FIXNUM)) \
82 XSETFASTINT (METER_2 (last_code, this_code), \
83 XFASTINT (METER_2 (last_code, this_code)) + 1); \
84 } \
85 }
86
87 #endif /* BYTE_CODE_METER */
88 \f
89
90 Lisp_Object Qbytecode;
91
92 /* Byte codes: */
93
94 #define BYTE_CODES \
95 DEFINE (Bstack_ref, 0) /* Actually, Bstack_ref+0 is not implemented: use dup. */ \
96 DEFINE (Bstack_ref1, 1) \
97 DEFINE (Bstack_ref2, 2) \
98 DEFINE (Bstack_ref3, 3) \
99 DEFINE (Bstack_ref4, 4) \
100 DEFINE (Bstack_ref5, 5) \
101 DEFINE (Bstack_ref6, 6) \
102 DEFINE (Bstack_ref7, 7) \
103 DEFINE (Bvarref, 010) \
104 DEFINE (Bvarref1, 011) \
105 DEFINE (Bvarref2, 012) \
106 DEFINE (Bvarref3, 013) \
107 DEFINE (Bvarref4, 014) \
108 DEFINE (Bvarref5, 015) \
109 DEFINE (Bvarref6, 016) \
110 DEFINE (Bvarref7, 017) \
111 DEFINE (Bvarset, 020) \
112 DEFINE (Bvarset1, 021) \
113 DEFINE (Bvarset2, 022) \
114 DEFINE (Bvarset3, 023) \
115 DEFINE (Bvarset4, 024) \
116 DEFINE (Bvarset5, 025) \
117 DEFINE (Bvarset6, 026) \
118 DEFINE (Bvarset7, 027) \
119 DEFINE (Bvarbind, 030) \
120 DEFINE (Bvarbind1, 031) \
121 DEFINE (Bvarbind2, 032) \
122 DEFINE (Bvarbind3, 033) \
123 DEFINE (Bvarbind4, 034) \
124 DEFINE (Bvarbind5, 035) \
125 DEFINE (Bvarbind6, 036) \
126 DEFINE (Bvarbind7, 037) \
127 DEFINE (Bcall, 040) \
128 DEFINE (Bcall1, 041) \
129 DEFINE (Bcall2, 042) \
130 DEFINE (Bcall3, 043) \
131 DEFINE (Bcall4, 044) \
132 DEFINE (Bcall5, 045) \
133 DEFINE (Bcall6, 046) \
134 DEFINE (Bcall7, 047) \
135 DEFINE (Bunbind, 050) \
136 DEFINE (Bunbind1, 051) \
137 DEFINE (Bunbind2, 052) \
138 DEFINE (Bunbind3, 053) \
139 DEFINE (Bunbind4, 054) \
140 DEFINE (Bunbind5, 055) \
141 DEFINE (Bunbind6, 056) \
142 DEFINE (Bunbind7, 057) \
143 \
144 DEFINE (Bnth, 070) \
145 DEFINE (Bsymbolp, 071) \
146 DEFINE (Bconsp, 072) \
147 DEFINE (Bstringp, 073) \
148 DEFINE (Blistp, 074) \
149 DEFINE (Beq, 075) \
150 DEFINE (Bmemq, 076) \
151 DEFINE (Bnot, 077) \
152 DEFINE (Bcar, 0100) \
153 DEFINE (Bcdr, 0101) \
154 DEFINE (Bcons, 0102) \
155 DEFINE (Blist1, 0103) \
156 DEFINE (Blist2, 0104) \
157 DEFINE (Blist3, 0105) \
158 DEFINE (Blist4, 0106) \
159 DEFINE (Blength, 0107) \
160 DEFINE (Baref, 0110) \
161 DEFINE (Baset, 0111) \
162 DEFINE (Bsymbol_value, 0112) \
163 DEFINE (Bsymbol_function, 0113) \
164 DEFINE (Bset, 0114) \
165 DEFINE (Bfset, 0115) \
166 DEFINE (Bget, 0116) \
167 DEFINE (Bsubstring, 0117) \
168 DEFINE (Bconcat2, 0120) \
169 DEFINE (Bconcat3, 0121) \
170 DEFINE (Bconcat4, 0122) \
171 DEFINE (Bsub1, 0123) \
172 DEFINE (Badd1, 0124) \
173 DEFINE (Beqlsign, 0125) \
174 DEFINE (Bgtr, 0126) \
175 DEFINE (Blss, 0127) \
176 DEFINE (Bleq, 0130) \
177 DEFINE (Bgeq, 0131) \
178 DEFINE (Bdiff, 0132) \
179 DEFINE (Bnegate, 0133) \
180 DEFINE (Bplus, 0134) \
181 DEFINE (Bmax, 0135) \
182 DEFINE (Bmin, 0136) \
183 DEFINE (Bmult, 0137) \
184 \
185 DEFINE (Bpoint, 0140) \
186 /* Was Bmark in v17. */ \
187 DEFINE (Bsave_current_buffer, 0141) /* Obsolete. */ \
188 DEFINE (Bgoto_char, 0142) \
189 DEFINE (Binsert, 0143) \
190 DEFINE (Bpoint_max, 0144) \
191 DEFINE (Bpoint_min, 0145) \
192 DEFINE (Bchar_after, 0146) \
193 DEFINE (Bfollowing_char, 0147) \
194 DEFINE (Bpreceding_char, 0150) \
195 DEFINE (Bcurrent_column, 0151) \
196 DEFINE (Bindent_to, 0152) \
197 DEFINE (Beolp, 0154) \
198 DEFINE (Beobp, 0155) \
199 DEFINE (Bbolp, 0156) \
200 DEFINE (Bbobp, 0157) \
201 DEFINE (Bcurrent_buffer, 0160) \
202 DEFINE (Bset_buffer, 0161) \
203 DEFINE (Bsave_current_buffer_1, 0162) /* Replacing Bsave_current_buffer. */ \
204 DEFINE (Binteractive_p, 0164) /* Obsolete since Emacs-24.1. */ \
205 \
206 DEFINE (Bforward_char, 0165) \
207 DEFINE (Bforward_word, 0166) \
208 DEFINE (Bskip_chars_forward, 0167) \
209 DEFINE (Bskip_chars_backward, 0170) \
210 DEFINE (Bforward_line, 0171) \
211 DEFINE (Bchar_syntax, 0172) \
212 DEFINE (Bbuffer_substring, 0173) \
213 DEFINE (Bdelete_region, 0174) \
214 DEFINE (Bnarrow_to_region, 0175) \
215 DEFINE (Bwiden, 0176) \
216 DEFINE (Bend_of_line, 0177) \
217 \
218 DEFINE (Bconstant2, 0201) \
219 DEFINE (Bgoto, 0202) \
220 DEFINE (Bgotoifnil, 0203) \
221 DEFINE (Bgotoifnonnil, 0204) \
222 DEFINE (Bgotoifnilelsepop, 0205) \
223 DEFINE (Bgotoifnonnilelsepop, 0206) \
224 DEFINE (Breturn, 0207) \
225 DEFINE (Bdiscard, 0210) \
226 DEFINE (Bdup, 0211) \
227 \
228 DEFINE (Bsave_excursion, 0212) \
229 DEFINE (Bsave_window_excursion, 0213) /* Obsolete since Emacs-24.1. */ \
230 DEFINE (Bsave_restriction, 0214) \
231 DEFINE (Bcatch, 0215) \
232 \
233 DEFINE (Bunwind_protect, 0216) \
234 DEFINE (Bcondition_case, 0217) \
235 DEFINE (Btemp_output_buffer_setup, 0220) /* Obsolete since Emacs-24.1. */ \
236 DEFINE (Btemp_output_buffer_show, 0221) /* Obsolete since Emacs-24.1. */ \
237 \
238 DEFINE (Bunbind_all, 0222) /* Obsolete. Never used. */ \
239 \
240 DEFINE (Bset_marker, 0223) \
241 DEFINE (Bmatch_beginning, 0224) \
242 DEFINE (Bmatch_end, 0225) \
243 DEFINE (Bupcase, 0226) \
244 DEFINE (Bdowncase, 0227) \
245 \
246 DEFINE (Bstringeqlsign, 0230) \
247 DEFINE (Bstringlss, 0231) \
248 DEFINE (Bequal, 0232) \
249 DEFINE (Bnthcdr, 0233) \
250 DEFINE (Belt, 0234) \
251 DEFINE (Bmember, 0235) \
252 DEFINE (Bassq, 0236) \
253 DEFINE (Bnreverse, 0237) \
254 DEFINE (Bsetcar, 0240) \
255 DEFINE (Bsetcdr, 0241) \
256 DEFINE (Bcar_safe, 0242) \
257 DEFINE (Bcdr_safe, 0243) \
258 DEFINE (Bnconc, 0244) \
259 DEFINE (Bquo, 0245) \
260 DEFINE (Brem, 0246) \
261 DEFINE (Bnumberp, 0247) \
262 DEFINE (Bintegerp, 0250) \
263 \
264 DEFINE (BRgoto, 0252) \
265 DEFINE (BRgotoifnil, 0253) \
266 DEFINE (BRgotoifnonnil, 0254) \
267 DEFINE (BRgotoifnilelsepop, 0255) \
268 DEFINE (BRgotoifnonnilelsepop, 0256) \
269 \
270 DEFINE (BlistN, 0257) \
271 DEFINE (BconcatN, 0260) \
272 DEFINE (BinsertN, 0261) \
273 \
274 /* Bstack_ref is code 0. */ \
275 DEFINE (Bstack_set, 0262) \
276 DEFINE (Bstack_set2, 0263) \
277 DEFINE (BdiscardN, 0266) \
278 \
279 DEFINE (Bconstant, 0300)
280
281 enum byte_code_op
282 {
283 #define DEFINE(name, value) name = value,
284 BYTE_CODES
285 #undef DEFINE
286
287 #ifdef BYTE_CODE_SAFE
288 Bscan_buffer = 0153, /* No longer generated as of v18. */
289 Bset_mark = 0163 /* this loser is no longer generated as of v18 */
290 #endif
291 };
292
293 /* Whether to maintain a `top' and `bottom' field in the stack frame. */
294 #define BYTE_MAINTAIN_TOP (BYTE_CODE_SAFE || BYTE_MARK_STACK)
295 \f
296 /* Structure describing a value stack used during byte-code execution
297 in Fbyte_code. */
298
299 struct byte_stack
300 {
301 /* Program counter. This points into the byte_string below
302 and is relocated when that string is relocated. */
303 const unsigned char *pc;
304
305 /* Top and bottom of stack. The bottom points to an area of memory
306 allocated with alloca in Fbyte_code. */
307 #if BYTE_MAINTAIN_TOP
308 Lisp_Object *top, *bottom;
309 #endif
310
311 /* The string containing the byte-code, and its current address.
312 Storing this here protects it from GC because mark_byte_stack
313 marks it. */
314 Lisp_Object byte_string;
315 const unsigned char *byte_string_start;
316
317 /* The vector of constants used during byte-code execution. Storing
318 this here protects it from GC because mark_byte_stack marks it. */
319 Lisp_Object constants;
320
321 /* Next entry in byte_stack_list. */
322 struct byte_stack *next;
323 };
324
325 /* A list of currently active byte-code execution value stacks.
326 Fbyte_code adds an entry to the head of this list before it starts
327 processing byte-code, and it removed the entry again when it is
328 done. Signaling an error truncates the list analogous to
329 gcprolist. */
330
331 struct byte_stack *byte_stack_list;
332
333 \f
334 /* Mark objects on byte_stack_list. Called during GC. */
335
336 #if BYTE_MARK_STACK
337 void
338 mark_byte_stack (void)
339 {
340 struct byte_stack *stack;
341 Lisp_Object *obj;
342
343 for (stack = byte_stack_list; stack; stack = stack->next)
344 {
345 /* If STACK->top is null here, this means there's an opcode in
346 Fbyte_code that wasn't expected to GC, but did. To find out
347 which opcode this is, record the value of `stack', and walk
348 up the stack in a debugger, stopping in frames of Fbyte_code.
349 The culprit is found in the frame of Fbyte_code where the
350 address of its local variable `stack' is equal to the
351 recorded value of `stack' here. */
352 eassert (stack->top);
353
354 for (obj = stack->bottom; obj <= stack->top; ++obj)
355 mark_object (*obj);
356
357 mark_object (stack->byte_string);
358 mark_object (stack->constants);
359 }
360 }
361 #endif
362
363 /* Unmark objects in the stacks on byte_stack_list. Relocate program
364 counters. Called when GC has completed. */
365
366 void
367 unmark_byte_stack (void)
368 {
369 struct byte_stack *stack;
370
371 for (stack = byte_stack_list; stack; stack = stack->next)
372 {
373 if (stack->byte_string_start != SDATA (stack->byte_string))
374 {
375 ptrdiff_t offset = stack->pc - stack->byte_string_start;
376 stack->byte_string_start = SDATA (stack->byte_string);
377 stack->pc = stack->byte_string_start + offset;
378 }
379 }
380 }
381
382 \f
383 /* Fetch the next byte from the bytecode stream */
384
385 #define FETCH *stack.pc++
386
387 /* Fetch two bytes from the bytecode stream and make a 16-bit number
388 out of them */
389
390 #define FETCH2 (op = FETCH, op + (FETCH << 8))
391
392 /* Push x onto the execution stack. This used to be #define PUSH(x)
393 (*++stackp = (x)) This oddity is necessary because Alliant can't be
394 bothered to compile the preincrement operator properly, as of 4/91.
395 -JimB */
396
397 #define PUSH(x) (top++, *top = (x))
398
399 /* Pop a value off the execution stack. */
400
401 #define POP (*top--)
402
403 /* Discard n values from the execution stack. */
404
405 #define DISCARD(n) (top -= (n))
406
407 /* Get the value which is at the top of the execution stack, but don't
408 pop it. */
409
410 #define TOP (*top)
411
412 /* Actions that must be performed before and after calling a function
413 that might GC. */
414
415 #if !BYTE_MAINTAIN_TOP
416 #define BEFORE_POTENTIAL_GC() ((void)0)
417 #define AFTER_POTENTIAL_GC() ((void)0)
418 #else
419 #define BEFORE_POTENTIAL_GC() stack.top = top
420 #define AFTER_POTENTIAL_GC() stack.top = NULL
421 #endif
422
423 /* Garbage collect if we have consed enough since the last time.
424 We do this at every branch, to avoid loops that never GC. */
425
426 #define MAYBE_GC() \
427 do { \
428 BEFORE_POTENTIAL_GC (); \
429 maybe_gc (); \
430 AFTER_POTENTIAL_GC (); \
431 } while (0)
432
433 /* Check for jumping out of range. */
434
435 #ifdef BYTE_CODE_SAFE
436
437 #define CHECK_RANGE(ARG) \
438 if (ARG >= bytestr_length) emacs_abort ()
439
440 #else /* not BYTE_CODE_SAFE */
441
442 #define CHECK_RANGE(ARG)
443
444 #endif /* not BYTE_CODE_SAFE */
445
446 /* A version of the QUIT macro which makes sure that the stack top is
447 set before signaling `quit'. */
448
449 #define BYTE_CODE_QUIT \
450 do { \
451 if (!NILP (Vquit_flag) && NILP (Vinhibit_quit)) \
452 { \
453 Lisp_Object flag = Vquit_flag; \
454 Vquit_flag = Qnil; \
455 BEFORE_POTENTIAL_GC (); \
456 if (EQ (Vthrow_on_input, flag)) \
457 Fthrow (Vthrow_on_input, Qt); \
458 Fsignal (Qquit, Qnil); \
459 AFTER_POTENTIAL_GC (); \
460 } \
461 ELSE_PENDING_SIGNALS \
462 } while (0)
463
464
465 DEFUN ("byte-code", Fbyte_code, Sbyte_code, 3, 3, 0,
466 doc: /* Function used internally in byte-compiled code.
467 The first argument, BYTESTR, is a string of byte code;
468 the second, VECTOR, a vector of constants;
469 the third, MAXDEPTH, the maximum stack depth used in this function.
470 If the third argument is incorrect, Emacs may crash. */)
471 (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth)
472 {
473 return exec_byte_code (bytestr, vector, maxdepth, Qnil, 0, NULL);
474 }
475
476 /* Execute the byte-code in BYTESTR. VECTOR is the constant vector, and
477 MAXDEPTH is the maximum stack depth used (if MAXDEPTH is incorrect,
478 emacs may crash!). If ARGS_TEMPLATE is non-nil, it should be a lisp
479 argument list (including &rest, &optional, etc.), and ARGS, of size
480 NARGS, should be a vector of the actual arguments. The arguments in
481 ARGS are pushed on the stack according to ARGS_TEMPLATE before
482 executing BYTESTR. */
483
484 Lisp_Object
485 exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
486 Lisp_Object args_template, ptrdiff_t nargs, Lisp_Object *args)
487 {
488 ptrdiff_t count = SPECPDL_INDEX ();
489 #ifdef BYTE_CODE_METER
490 int this_op = 0;
491 int prev_op;
492 #endif
493 int op;
494 /* Lisp_Object v1, v2; */
495 Lisp_Object *vectorp;
496 #ifdef BYTE_CODE_SAFE
497 ptrdiff_t const_length;
498 Lisp_Object *stacke;
499 ptrdiff_t bytestr_length;
500 #endif
501 struct byte_stack stack;
502 Lisp_Object *top;
503 Lisp_Object result;
504
505 #if 0 /* CHECK_FRAME_FONT */
506 {
507 struct frame *f = SELECTED_FRAME ();
508 if (FRAME_X_P (f)
509 && FRAME_FONT (f)->direction != 0
510 && FRAME_FONT (f)->direction != 1)
511 emacs_abort ();
512 }
513 #endif
514
515 CHECK_STRING (bytestr);
516 CHECK_VECTOR (vector);
517 CHECK_NATNUM (maxdepth);
518
519 #ifdef BYTE_CODE_SAFE
520 const_length = ASIZE (vector);
521 #endif
522
523 if (STRING_MULTIBYTE (bytestr))
524 /* BYTESTR must have been produced by Emacs 20.2 or the earlier
525 because they produced a raw 8-bit string for byte-code and now
526 such a byte-code string is loaded as multibyte while raw 8-bit
527 characters converted to multibyte form. Thus, now we must
528 convert them back to the originally intended unibyte form. */
529 bytestr = Fstring_as_unibyte (bytestr);
530
531 #ifdef BYTE_CODE_SAFE
532 bytestr_length = SBYTES (bytestr);
533 #endif
534 vectorp = XVECTOR (vector)->contents;
535
536 stack.byte_string = bytestr;
537 stack.pc = stack.byte_string_start = SDATA (bytestr);
538 stack.constants = vector;
539 if (MAX_ALLOCA / word_size <= XFASTINT (maxdepth))
540 memory_full (SIZE_MAX);
541 top = alloca ((XFASTINT (maxdepth) + 1) * sizeof *top);
542 #if BYTE_MAINTAIN_TOP
543 stack.bottom = top + 1;
544 stack.top = NULL;
545 #endif
546 stack.next = byte_stack_list;
547 byte_stack_list = &stack;
548
549 #ifdef BYTE_CODE_SAFE
550 stacke = stack.bottom - 1 + XFASTINT (maxdepth);
551 #endif
552
553 if (INTEGERP (args_template))
554 {
555 ptrdiff_t at = XINT (args_template);
556 bool rest = (at & 128) != 0;
557 int mandatory = at & 127;
558 ptrdiff_t nonrest = at >> 8;
559 eassert (mandatory <= nonrest);
560 if (nargs <= nonrest)
561 {
562 ptrdiff_t i;
563 for (i = 0 ; i < nargs; i++, args++)
564 PUSH (*args);
565 if (nargs < mandatory)
566 /* Too few arguments. */
567 Fsignal (Qwrong_number_of_arguments,
568 Fcons (Fcons (make_number (mandatory),
569 rest ? Qand_rest : make_number (nonrest)),
570 Fcons (make_number (nargs), Qnil)));
571 else
572 {
573 for (; i < nonrest; i++)
574 PUSH (Qnil);
575 if (rest)
576 PUSH (Qnil);
577 }
578 }
579 else if (rest)
580 {
581 ptrdiff_t i;
582 for (i = 0 ; i < nonrest; i++, args++)
583 PUSH (*args);
584 PUSH (Flist (nargs - nonrest, args));
585 }
586 else
587 /* Too many arguments. */
588 Fsignal (Qwrong_number_of_arguments,
589 Fcons (Fcons (make_number (mandatory),
590 make_number (nonrest)),
591 Fcons (make_number (nargs), Qnil)));
592 }
593 else if (! NILP (args_template))
594 /* We should push some arguments on the stack. */
595 {
596 error ("Unknown args template!");
597 }
598
599 while (1)
600 {
601 #ifdef BYTE_CODE_SAFE
602 if (top > stacke)
603 emacs_abort ();
604 else if (top < stack.bottom - 1)
605 emacs_abort ();
606 #endif
607
608 #ifdef BYTE_CODE_METER
609 prev_op = this_op;
610 this_op = op = FETCH;
611 METER_CODE (prev_op, op);
612 #else
613 #ifndef BYTE_CODE_THREADED
614 op = FETCH;
615 #endif
616 #endif
617
618 /* The interpreter can be compiled one of two ways: as an
619 ordinary switch-based interpreter, or as a threaded
620 interpreter. The threaded interpreter relies on GCC's
621 computed goto extension, so it is not available everywhere.
622 Threading provides a performance boost. These macros are how
623 we allow the code to be compiled both ways. */
624 #ifdef BYTE_CODE_THREADED
625 /* The CASE macro introduces an instruction's body. It is
626 either a label or a case label. */
627 #define CASE(OP) insn_ ## OP
628 /* NEXT is invoked at the end of an instruction to go to the
629 next instruction. It is either a computed goto, or a
630 plain break. */
631 #define NEXT goto *(targets[op = FETCH])
632 /* FIRST is like NEXT, but is only used at the start of the
633 interpreter body. In the switch-based interpreter it is the
634 switch, so the threaded definition must include a semicolon. */
635 #define FIRST NEXT;
636 /* Most cases are labeled with the CASE macro, above.
637 CASE_DEFAULT is one exception; it is used if the interpreter
638 being built requires a default case. The threaded
639 interpreter does not, because the dispatch table is
640 completely filled. */
641 #define CASE_DEFAULT
642 /* This introduces an instruction that is known to call abort. */
643 #define CASE_ABORT CASE (Bstack_ref): CASE (default)
644 #else
645 /* See above for the meaning of the various defines. */
646 #define CASE(OP) case OP
647 #define NEXT break
648 #define FIRST switch (op)
649 #define CASE_DEFAULT case 255: default:
650 #define CASE_ABORT case 0
651 #endif
652
653 #ifdef BYTE_CODE_THREADED
654
655 /* A convenience define that saves us a lot of typing and makes
656 the table clearer. */
657 #define LABEL(OP) [OP] = &&insn_ ## OP
658
659 #if (__GNUC__ == 4 && 6 <= __GNUC_MINOR__) || 4 < __GNUC__
660 # pragma GCC diagnostic push
661 # pragma GCC diagnostic ignored "-Woverride-init"
662 #endif
663
664 /* This is the dispatch table for the threaded interpreter. */
665 static const void *const targets[256] =
666 {
667 [0 ... (Bconstant - 1)] = &&insn_default,
668 [Bconstant ... 255] = &&insn_Bconstant,
669
670 #define DEFINE(name, value) LABEL (name) ,
671 BYTE_CODES
672 #undef DEFINE
673 };
674
675 #if (__GNUC__ == 4 && 6 <= __GNUC_MINOR__) || 4 < __GNUC__
676 # pragma GCC diagnostic pop
677 #endif
678
679 #endif
680
681
682 FIRST
683 {
684 CASE (Bvarref7):
685 op = FETCH2;
686 goto varref;
687
688 CASE (Bvarref):
689 CASE (Bvarref1):
690 CASE (Bvarref2):
691 CASE (Bvarref3):
692 CASE (Bvarref4):
693 CASE (Bvarref5):
694 op = op - Bvarref;
695 goto varref;
696
697 /* This seems to be the most frequently executed byte-code
698 among the Bvarref's, so avoid a goto here. */
699 CASE (Bvarref6):
700 op = FETCH;
701 varref:
702 {
703 Lisp_Object v1, v2;
704
705 v1 = vectorp[op];
706 if (SYMBOLP (v1))
707 {
708 if (XSYMBOL (v1)->redirect != SYMBOL_PLAINVAL
709 || (v2 = SYMBOL_VAL (XSYMBOL (v1)),
710 EQ (v2, Qunbound)))
711 {
712 BEFORE_POTENTIAL_GC ();
713 v2 = Fsymbol_value (v1);
714 AFTER_POTENTIAL_GC ();
715 }
716 }
717 else
718 {
719 BEFORE_POTENTIAL_GC ();
720 v2 = Fsymbol_value (v1);
721 AFTER_POTENTIAL_GC ();
722 }
723 PUSH (v2);
724 NEXT;
725 }
726
727 CASE (Bgotoifnil):
728 {
729 Lisp_Object v1;
730 MAYBE_GC ();
731 op = FETCH2;
732 v1 = POP;
733 if (NILP (v1))
734 {
735 BYTE_CODE_QUIT;
736 CHECK_RANGE (op);
737 stack.pc = stack.byte_string_start + op;
738 }
739 NEXT;
740 }
741
742 CASE (Bcar):
743 {
744 Lisp_Object v1;
745 v1 = TOP;
746 if (CONSP (v1))
747 TOP = XCAR (v1);
748 else if (NILP (v1))
749 TOP = Qnil;
750 else
751 {
752 BEFORE_POTENTIAL_GC ();
753 wrong_type_argument (Qlistp, v1);
754 AFTER_POTENTIAL_GC ();
755 }
756 NEXT;
757 }
758
759 CASE (Beq):
760 {
761 Lisp_Object v1;
762 v1 = POP;
763 TOP = EQ (v1, TOP) ? Qt : Qnil;
764 NEXT;
765 }
766
767 CASE (Bmemq):
768 {
769 Lisp_Object v1;
770 BEFORE_POTENTIAL_GC ();
771 v1 = POP;
772 TOP = Fmemq (TOP, v1);
773 AFTER_POTENTIAL_GC ();
774 NEXT;
775 }
776
777 CASE (Bcdr):
778 {
779 Lisp_Object v1;
780 v1 = TOP;
781 if (CONSP (v1))
782 TOP = XCDR (v1);
783 else if (NILP (v1))
784 TOP = Qnil;
785 else
786 {
787 BEFORE_POTENTIAL_GC ();
788 wrong_type_argument (Qlistp, v1);
789 AFTER_POTENTIAL_GC ();
790 }
791 NEXT;
792 }
793
794 CASE (Bvarset):
795 CASE (Bvarset1):
796 CASE (Bvarset2):
797 CASE (Bvarset3):
798 CASE (Bvarset4):
799 CASE (Bvarset5):
800 op -= Bvarset;
801 goto varset;
802
803 CASE (Bvarset7):
804 op = FETCH2;
805 goto varset;
806
807 CASE (Bvarset6):
808 op = FETCH;
809 varset:
810 {
811 Lisp_Object sym, val;
812
813 sym = vectorp[op];
814 val = TOP;
815
816 /* Inline the most common case. */
817 if (SYMBOLP (sym)
818 && !EQ (val, Qunbound)
819 && !XSYMBOL (sym)->redirect
820 && !SYMBOL_CONSTANT_P (sym))
821 SET_SYMBOL_VAL (XSYMBOL (sym), val);
822 else
823 {
824 BEFORE_POTENTIAL_GC ();
825 set_internal (sym, val, Qnil, 0);
826 AFTER_POTENTIAL_GC ();
827 }
828 }
829 (void) POP;
830 NEXT;
831
832 CASE (Bdup):
833 {
834 Lisp_Object v1;
835 v1 = TOP;
836 PUSH (v1);
837 NEXT;
838 }
839
840 /* ------------------ */
841
842 CASE (Bvarbind6):
843 op = FETCH;
844 goto varbind;
845
846 CASE (Bvarbind7):
847 op = FETCH2;
848 goto varbind;
849
850 CASE (Bvarbind):
851 CASE (Bvarbind1):
852 CASE (Bvarbind2):
853 CASE (Bvarbind3):
854 CASE (Bvarbind4):
855 CASE (Bvarbind5):
856 op -= Bvarbind;
857 varbind:
858 /* Specbind can signal and thus GC. */
859 BEFORE_POTENTIAL_GC ();
860 specbind (vectorp[op], POP);
861 AFTER_POTENTIAL_GC ();
862 NEXT;
863
864 CASE (Bcall6):
865 op = FETCH;
866 goto docall;
867
868 CASE (Bcall7):
869 op = FETCH2;
870 goto docall;
871
872 CASE (Bcall):
873 CASE (Bcall1):
874 CASE (Bcall2):
875 CASE (Bcall3):
876 CASE (Bcall4):
877 CASE (Bcall5):
878 op -= Bcall;
879 docall:
880 {
881 BEFORE_POTENTIAL_GC ();
882 DISCARD (op);
883 #ifdef BYTE_CODE_METER
884 if (byte_metering_on && SYMBOLP (TOP))
885 {
886 Lisp_Object v1, v2;
887
888 v1 = TOP;
889 v2 = Fget (v1, Qbyte_code_meter);
890 if (INTEGERP (v2)
891 && XINT (v2) < MOST_POSITIVE_FIXNUM)
892 {
893 XSETINT (v2, XINT (v2) + 1);
894 Fput (v1, Qbyte_code_meter, v2);
895 }
896 }
897 #endif
898 TOP = Ffuncall (op + 1, &TOP);
899 AFTER_POTENTIAL_GC ();
900 NEXT;
901 }
902
903 CASE (Bunbind6):
904 op = FETCH;
905 goto dounbind;
906
907 CASE (Bunbind7):
908 op = FETCH2;
909 goto dounbind;
910
911 CASE (Bunbind):
912 CASE (Bunbind1):
913 CASE (Bunbind2):
914 CASE (Bunbind3):
915 CASE (Bunbind4):
916 CASE (Bunbind5):
917 op -= Bunbind;
918 dounbind:
919 BEFORE_POTENTIAL_GC ();
920 unbind_to (SPECPDL_INDEX () - op, Qnil);
921 AFTER_POTENTIAL_GC ();
922 NEXT;
923
924 CASE (Bunbind_all): /* Obsolete. Never used. */
925 /* To unbind back to the beginning of this frame. Not used yet,
926 but will be needed for tail-recursion elimination. */
927 BEFORE_POTENTIAL_GC ();
928 unbind_to (count, Qnil);
929 AFTER_POTENTIAL_GC ();
930 NEXT;
931
932 CASE (Bgoto):
933 MAYBE_GC ();
934 BYTE_CODE_QUIT;
935 op = FETCH2; /* pc = FETCH2 loses since FETCH2 contains pc++ */
936 CHECK_RANGE (op);
937 stack.pc = stack.byte_string_start + op;
938 NEXT;
939
940 CASE (Bgotoifnonnil):
941 {
942 Lisp_Object v1;
943 MAYBE_GC ();
944 op = FETCH2;
945 v1 = POP;
946 if (!NILP (v1))
947 {
948 BYTE_CODE_QUIT;
949 CHECK_RANGE (op);
950 stack.pc = stack.byte_string_start + op;
951 }
952 NEXT;
953 }
954
955 CASE (Bgotoifnilelsepop):
956 MAYBE_GC ();
957 op = FETCH2;
958 if (NILP (TOP))
959 {
960 BYTE_CODE_QUIT;
961 CHECK_RANGE (op);
962 stack.pc = stack.byte_string_start + op;
963 }
964 else DISCARD (1);
965 NEXT;
966
967 CASE (Bgotoifnonnilelsepop):
968 MAYBE_GC ();
969 op = FETCH2;
970 if (!NILP (TOP))
971 {
972 BYTE_CODE_QUIT;
973 CHECK_RANGE (op);
974 stack.pc = stack.byte_string_start + op;
975 }
976 else DISCARD (1);
977 NEXT;
978
979 CASE (BRgoto):
980 MAYBE_GC ();
981 BYTE_CODE_QUIT;
982 stack.pc += (int) *stack.pc - 127;
983 NEXT;
984
985 CASE (BRgotoifnil):
986 {
987 Lisp_Object v1;
988 MAYBE_GC ();
989 v1 = POP;
990 if (NILP (v1))
991 {
992 BYTE_CODE_QUIT;
993 stack.pc += (int) *stack.pc - 128;
994 }
995 stack.pc++;
996 NEXT;
997 }
998
999 CASE (BRgotoifnonnil):
1000 {
1001 Lisp_Object v1;
1002 MAYBE_GC ();
1003 v1 = POP;
1004 if (!NILP (v1))
1005 {
1006 BYTE_CODE_QUIT;
1007 stack.pc += (int) *stack.pc - 128;
1008 }
1009 stack.pc++;
1010 NEXT;
1011 }
1012
1013 CASE (BRgotoifnilelsepop):
1014 MAYBE_GC ();
1015 op = *stack.pc++;
1016 if (NILP (TOP))
1017 {
1018 BYTE_CODE_QUIT;
1019 stack.pc += op - 128;
1020 }
1021 else DISCARD (1);
1022 NEXT;
1023
1024 CASE (BRgotoifnonnilelsepop):
1025 MAYBE_GC ();
1026 op = *stack.pc++;
1027 if (!NILP (TOP))
1028 {
1029 BYTE_CODE_QUIT;
1030 stack.pc += op - 128;
1031 }
1032 else DISCARD (1);
1033 NEXT;
1034
1035 CASE (Breturn):
1036 result = POP;
1037 goto exit;
1038
1039 CASE (Bdiscard):
1040 DISCARD (1);
1041 NEXT;
1042
1043 CASE (Bconstant2):
1044 PUSH (vectorp[FETCH2]);
1045 NEXT;
1046
1047 CASE (Bsave_excursion):
1048 record_unwind_protect (save_excursion_restore,
1049 save_excursion_save ());
1050 NEXT;
1051
1052 CASE (Bsave_current_buffer): /* Obsolete since ??. */
1053 CASE (Bsave_current_buffer_1):
1054 record_unwind_current_buffer ();
1055 NEXT;
1056
1057 CASE (Bsave_window_excursion): /* Obsolete since 24.1. */
1058 {
1059 register ptrdiff_t count1 = SPECPDL_INDEX ();
1060 record_unwind_protect (Fset_window_configuration,
1061 Fcurrent_window_configuration (Qnil));
1062 BEFORE_POTENTIAL_GC ();
1063 TOP = Fprogn (TOP);
1064 unbind_to (count1, TOP);
1065 AFTER_POTENTIAL_GC ();
1066 NEXT;
1067 }
1068
1069 CASE (Bsave_restriction):
1070 record_unwind_protect (save_restriction_restore,
1071 save_restriction_save ());
1072 NEXT;
1073
1074 CASE (Bcatch): /* FIXME: ill-suited for lexbind. */
1075 {
1076 Lisp_Object v1;
1077 BEFORE_POTENTIAL_GC ();
1078 v1 = POP;
1079 TOP = internal_catch (TOP, eval_sub, v1);
1080 AFTER_POTENTIAL_GC ();
1081 NEXT;
1082 }
1083
1084 CASE (Bunwind_protect): /* FIXME: avoid closure for lexbind. */
1085 record_unwind_protect (Fprogn, POP);
1086 NEXT;
1087
1088 CASE (Bcondition_case): /* FIXME: ill-suited for lexbind. */
1089 {
1090 Lisp_Object handlers, body;
1091 handlers = POP;
1092 body = POP;
1093 BEFORE_POTENTIAL_GC ();
1094 TOP = internal_lisp_condition_case (TOP, body, handlers);
1095 AFTER_POTENTIAL_GC ();
1096 NEXT;
1097 }
1098
1099 CASE (Btemp_output_buffer_setup): /* Obsolete since 24.1. */
1100 BEFORE_POTENTIAL_GC ();
1101 CHECK_STRING (TOP);
1102 temp_output_buffer_setup (SSDATA (TOP));
1103 AFTER_POTENTIAL_GC ();
1104 TOP = Vstandard_output;
1105 NEXT;
1106
1107 CASE (Btemp_output_buffer_show): /* Obsolete since 24.1. */
1108 {
1109 Lisp_Object v1;
1110 BEFORE_POTENTIAL_GC ();
1111 v1 = POP;
1112 temp_output_buffer_show (TOP);
1113 TOP = v1;
1114 /* pop binding of standard-output */
1115 unbind_to (SPECPDL_INDEX () - 1, Qnil);
1116 AFTER_POTENTIAL_GC ();
1117 NEXT;
1118 }
1119
1120 CASE (Bnth):
1121 {
1122 Lisp_Object v1, v2;
1123 EMACS_INT n;
1124 BEFORE_POTENTIAL_GC ();
1125 v1 = POP;
1126 v2 = TOP;
1127 CHECK_NUMBER (v2);
1128 n = XINT (v2);
1129 immediate_quit = 1;
1130 while (--n >= 0 && CONSP (v1))
1131 v1 = XCDR (v1);
1132 immediate_quit = 0;
1133 TOP = CAR (v1);
1134 AFTER_POTENTIAL_GC ();
1135 NEXT;
1136 }
1137
1138 CASE (Bsymbolp):
1139 TOP = SYMBOLP (TOP) ? Qt : Qnil;
1140 NEXT;
1141
1142 CASE (Bconsp):
1143 TOP = CONSP (TOP) ? Qt : Qnil;
1144 NEXT;
1145
1146 CASE (Bstringp):
1147 TOP = STRINGP (TOP) ? Qt : Qnil;
1148 NEXT;
1149
1150 CASE (Blistp):
1151 TOP = CONSP (TOP) || NILP (TOP) ? Qt : Qnil;
1152 NEXT;
1153
1154 CASE (Bnot):
1155 TOP = NILP (TOP) ? Qt : Qnil;
1156 NEXT;
1157
1158 CASE (Bcons):
1159 {
1160 Lisp_Object v1;
1161 v1 = POP;
1162 TOP = Fcons (TOP, v1);
1163 NEXT;
1164 }
1165
1166 CASE (Blist1):
1167 TOP = Fcons (TOP, Qnil);
1168 NEXT;
1169
1170 CASE (Blist2):
1171 {
1172 Lisp_Object v1;
1173 v1 = POP;
1174 TOP = Fcons (TOP, Fcons (v1, Qnil));
1175 NEXT;
1176 }
1177
1178 CASE (Blist3):
1179 DISCARD (2);
1180 TOP = Flist (3, &TOP);
1181 NEXT;
1182
1183 CASE (Blist4):
1184 DISCARD (3);
1185 TOP = Flist (4, &TOP);
1186 NEXT;
1187
1188 CASE (BlistN):
1189 op = FETCH;
1190 DISCARD (op - 1);
1191 TOP = Flist (op, &TOP);
1192 NEXT;
1193
1194 CASE (Blength):
1195 BEFORE_POTENTIAL_GC ();
1196 TOP = Flength (TOP);
1197 AFTER_POTENTIAL_GC ();
1198 NEXT;
1199
1200 CASE (Baref):
1201 {
1202 Lisp_Object v1;
1203 BEFORE_POTENTIAL_GC ();
1204 v1 = POP;
1205 TOP = Faref (TOP, v1);
1206 AFTER_POTENTIAL_GC ();
1207 NEXT;
1208 }
1209
1210 CASE (Baset):
1211 {
1212 Lisp_Object v1, v2;
1213 BEFORE_POTENTIAL_GC ();
1214 v2 = POP; v1 = POP;
1215 TOP = Faset (TOP, v1, v2);
1216 AFTER_POTENTIAL_GC ();
1217 NEXT;
1218 }
1219
1220 CASE (Bsymbol_value):
1221 BEFORE_POTENTIAL_GC ();
1222 TOP = Fsymbol_value (TOP);
1223 AFTER_POTENTIAL_GC ();
1224 NEXT;
1225
1226 CASE (Bsymbol_function):
1227 BEFORE_POTENTIAL_GC ();
1228 TOP = Fsymbol_function (TOP);
1229 AFTER_POTENTIAL_GC ();
1230 NEXT;
1231
1232 CASE (Bset):
1233 {
1234 Lisp_Object v1;
1235 BEFORE_POTENTIAL_GC ();
1236 v1 = POP;
1237 TOP = Fset (TOP, v1);
1238 AFTER_POTENTIAL_GC ();
1239 NEXT;
1240 }
1241
1242 CASE (Bfset):
1243 {
1244 Lisp_Object v1;
1245 BEFORE_POTENTIAL_GC ();
1246 v1 = POP;
1247 TOP = Ffset (TOP, v1);
1248 AFTER_POTENTIAL_GC ();
1249 NEXT;
1250 }
1251
1252 CASE (Bget):
1253 {
1254 Lisp_Object v1;
1255 BEFORE_POTENTIAL_GC ();
1256 v1 = POP;
1257 TOP = Fget (TOP, v1);
1258 AFTER_POTENTIAL_GC ();
1259 NEXT;
1260 }
1261
1262 CASE (Bsubstring):
1263 {
1264 Lisp_Object v1, v2;
1265 BEFORE_POTENTIAL_GC ();
1266 v2 = POP; v1 = POP;
1267 TOP = Fsubstring (TOP, v1, v2);
1268 AFTER_POTENTIAL_GC ();
1269 NEXT;
1270 }
1271
1272 CASE (Bconcat2):
1273 BEFORE_POTENTIAL_GC ();
1274 DISCARD (1);
1275 TOP = Fconcat (2, &TOP);
1276 AFTER_POTENTIAL_GC ();
1277 NEXT;
1278
1279 CASE (Bconcat3):
1280 BEFORE_POTENTIAL_GC ();
1281 DISCARD (2);
1282 TOP = Fconcat (3, &TOP);
1283 AFTER_POTENTIAL_GC ();
1284 NEXT;
1285
1286 CASE (Bconcat4):
1287 BEFORE_POTENTIAL_GC ();
1288 DISCARD (3);
1289 TOP = Fconcat (4, &TOP);
1290 AFTER_POTENTIAL_GC ();
1291 NEXT;
1292
1293 CASE (BconcatN):
1294 op = FETCH;
1295 BEFORE_POTENTIAL_GC ();
1296 DISCARD (op - 1);
1297 TOP = Fconcat (op, &TOP);
1298 AFTER_POTENTIAL_GC ();
1299 NEXT;
1300
1301 CASE (Bsub1):
1302 {
1303 Lisp_Object v1;
1304 v1 = TOP;
1305 if (INTEGERP (v1))
1306 {
1307 XSETINT (v1, XINT (v1) - 1);
1308 TOP = v1;
1309 }
1310 else
1311 {
1312 BEFORE_POTENTIAL_GC ();
1313 TOP = Fsub1 (v1);
1314 AFTER_POTENTIAL_GC ();
1315 }
1316 NEXT;
1317 }
1318
1319 CASE (Badd1):
1320 {
1321 Lisp_Object v1;
1322 v1 = TOP;
1323 if (INTEGERP (v1))
1324 {
1325 XSETINT (v1, XINT (v1) + 1);
1326 TOP = v1;
1327 }
1328 else
1329 {
1330 BEFORE_POTENTIAL_GC ();
1331 TOP = Fadd1 (v1);
1332 AFTER_POTENTIAL_GC ();
1333 }
1334 NEXT;
1335 }
1336
1337 CASE (Beqlsign):
1338 {
1339 Lisp_Object v1, v2;
1340 BEFORE_POTENTIAL_GC ();
1341 v2 = POP; v1 = TOP;
1342 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v1);
1343 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v2);
1344 AFTER_POTENTIAL_GC ();
1345 if (FLOATP (v1) || FLOATP (v2))
1346 {
1347 double f1, f2;
1348
1349 f1 = (FLOATP (v1) ? XFLOAT_DATA (v1) : XINT (v1));
1350 f2 = (FLOATP (v2) ? XFLOAT_DATA (v2) : XINT (v2));
1351 TOP = (f1 == f2 ? Qt : Qnil);
1352 }
1353 else
1354 TOP = (XINT (v1) == XINT (v2) ? Qt : Qnil);
1355 NEXT;
1356 }
1357
1358 CASE (Bgtr):
1359 {
1360 Lisp_Object v1;
1361 BEFORE_POTENTIAL_GC ();
1362 v1 = POP;
1363 TOP = Fgtr (TOP, v1);
1364 AFTER_POTENTIAL_GC ();
1365 NEXT;
1366 }
1367
1368 CASE (Blss):
1369 {
1370 Lisp_Object v1;
1371 BEFORE_POTENTIAL_GC ();
1372 v1 = POP;
1373 TOP = Flss (TOP, v1);
1374 AFTER_POTENTIAL_GC ();
1375 NEXT;
1376 }
1377
1378 CASE (Bleq):
1379 {
1380 Lisp_Object v1;
1381 BEFORE_POTENTIAL_GC ();
1382 v1 = POP;
1383 TOP = Fleq (TOP, v1);
1384 AFTER_POTENTIAL_GC ();
1385 NEXT;
1386 }
1387
1388 CASE (Bgeq):
1389 {
1390 Lisp_Object v1;
1391 BEFORE_POTENTIAL_GC ();
1392 v1 = POP;
1393 TOP = Fgeq (TOP, v1);
1394 AFTER_POTENTIAL_GC ();
1395 NEXT;
1396 }
1397
1398 CASE (Bdiff):
1399 BEFORE_POTENTIAL_GC ();
1400 DISCARD (1);
1401 TOP = Fminus (2, &TOP);
1402 AFTER_POTENTIAL_GC ();
1403 NEXT;
1404
1405 CASE (Bnegate):
1406 {
1407 Lisp_Object v1;
1408 v1 = TOP;
1409 if (INTEGERP (v1))
1410 {
1411 XSETINT (v1, - XINT (v1));
1412 TOP = v1;
1413 }
1414 else
1415 {
1416 BEFORE_POTENTIAL_GC ();
1417 TOP = Fminus (1, &TOP);
1418 AFTER_POTENTIAL_GC ();
1419 }
1420 NEXT;
1421 }
1422
1423 CASE (Bplus):
1424 BEFORE_POTENTIAL_GC ();
1425 DISCARD (1);
1426 TOP = Fplus (2, &TOP);
1427 AFTER_POTENTIAL_GC ();
1428 NEXT;
1429
1430 CASE (Bmax):
1431 BEFORE_POTENTIAL_GC ();
1432 DISCARD (1);
1433 TOP = Fmax (2, &TOP);
1434 AFTER_POTENTIAL_GC ();
1435 NEXT;
1436
1437 CASE (Bmin):
1438 BEFORE_POTENTIAL_GC ();
1439 DISCARD (1);
1440 TOP = Fmin (2, &TOP);
1441 AFTER_POTENTIAL_GC ();
1442 NEXT;
1443
1444 CASE (Bmult):
1445 BEFORE_POTENTIAL_GC ();
1446 DISCARD (1);
1447 TOP = Ftimes (2, &TOP);
1448 AFTER_POTENTIAL_GC ();
1449 NEXT;
1450
1451 CASE (Bquo):
1452 BEFORE_POTENTIAL_GC ();
1453 DISCARD (1);
1454 TOP = Fquo (2, &TOP);
1455 AFTER_POTENTIAL_GC ();
1456 NEXT;
1457
1458 CASE (Brem):
1459 {
1460 Lisp_Object v1;
1461 BEFORE_POTENTIAL_GC ();
1462 v1 = POP;
1463 TOP = Frem (TOP, v1);
1464 AFTER_POTENTIAL_GC ();
1465 NEXT;
1466 }
1467
1468 CASE (Bpoint):
1469 {
1470 Lisp_Object v1;
1471 XSETFASTINT (v1, PT);
1472 PUSH (v1);
1473 NEXT;
1474 }
1475
1476 CASE (Bgoto_char):
1477 BEFORE_POTENTIAL_GC ();
1478 TOP = Fgoto_char (TOP);
1479 AFTER_POTENTIAL_GC ();
1480 NEXT;
1481
1482 CASE (Binsert):
1483 BEFORE_POTENTIAL_GC ();
1484 TOP = Finsert (1, &TOP);
1485 AFTER_POTENTIAL_GC ();
1486 NEXT;
1487
1488 CASE (BinsertN):
1489 op = FETCH;
1490 BEFORE_POTENTIAL_GC ();
1491 DISCARD (op - 1);
1492 TOP = Finsert (op, &TOP);
1493 AFTER_POTENTIAL_GC ();
1494 NEXT;
1495
1496 CASE (Bpoint_max):
1497 {
1498 Lisp_Object v1;
1499 XSETFASTINT (v1, ZV);
1500 PUSH (v1);
1501 NEXT;
1502 }
1503
1504 CASE (Bpoint_min):
1505 {
1506 Lisp_Object v1;
1507 XSETFASTINT (v1, BEGV);
1508 PUSH (v1);
1509 NEXT;
1510 }
1511
1512 CASE (Bchar_after):
1513 BEFORE_POTENTIAL_GC ();
1514 TOP = Fchar_after (TOP);
1515 AFTER_POTENTIAL_GC ();
1516 NEXT;
1517
1518 CASE (Bfollowing_char):
1519 {
1520 Lisp_Object v1;
1521 BEFORE_POTENTIAL_GC ();
1522 v1 = Ffollowing_char ();
1523 AFTER_POTENTIAL_GC ();
1524 PUSH (v1);
1525 NEXT;
1526 }
1527
1528 CASE (Bpreceding_char):
1529 {
1530 Lisp_Object v1;
1531 BEFORE_POTENTIAL_GC ();
1532 v1 = Fprevious_char ();
1533 AFTER_POTENTIAL_GC ();
1534 PUSH (v1);
1535 NEXT;
1536 }
1537
1538 CASE (Bcurrent_column):
1539 {
1540 Lisp_Object v1;
1541 BEFORE_POTENTIAL_GC ();
1542 XSETFASTINT (v1, current_column ());
1543 AFTER_POTENTIAL_GC ();
1544 PUSH (v1);
1545 NEXT;
1546 }
1547
1548 CASE (Bindent_to):
1549 BEFORE_POTENTIAL_GC ();
1550 TOP = Findent_to (TOP, Qnil);
1551 AFTER_POTENTIAL_GC ();
1552 NEXT;
1553
1554 CASE (Beolp):
1555 PUSH (Feolp ());
1556 NEXT;
1557
1558 CASE (Beobp):
1559 PUSH (Feobp ());
1560 NEXT;
1561
1562 CASE (Bbolp):
1563 PUSH (Fbolp ());
1564 NEXT;
1565
1566 CASE (Bbobp):
1567 PUSH (Fbobp ());
1568 NEXT;
1569
1570 CASE (Bcurrent_buffer):
1571 PUSH (Fcurrent_buffer ());
1572 NEXT;
1573
1574 CASE (Bset_buffer):
1575 BEFORE_POTENTIAL_GC ();
1576 TOP = Fset_buffer (TOP);
1577 AFTER_POTENTIAL_GC ();
1578 NEXT;
1579
1580 CASE (Binteractive_p): /* Obsolete since 24.1. */
1581 PUSH (Finteractive_p ());
1582 NEXT;
1583
1584 CASE (Bforward_char):
1585 BEFORE_POTENTIAL_GC ();
1586 TOP = Fforward_char (TOP);
1587 AFTER_POTENTIAL_GC ();
1588 NEXT;
1589
1590 CASE (Bforward_word):
1591 BEFORE_POTENTIAL_GC ();
1592 TOP = Fforward_word (TOP);
1593 AFTER_POTENTIAL_GC ();
1594 NEXT;
1595
1596 CASE (Bskip_chars_forward):
1597 {
1598 Lisp_Object v1;
1599 BEFORE_POTENTIAL_GC ();
1600 v1 = POP;
1601 TOP = Fskip_chars_forward (TOP, v1);
1602 AFTER_POTENTIAL_GC ();
1603 NEXT;
1604 }
1605
1606 CASE (Bskip_chars_backward):
1607 {
1608 Lisp_Object v1;
1609 BEFORE_POTENTIAL_GC ();
1610 v1 = POP;
1611 TOP = Fskip_chars_backward (TOP, v1);
1612 AFTER_POTENTIAL_GC ();
1613 NEXT;
1614 }
1615
1616 CASE (Bforward_line):
1617 BEFORE_POTENTIAL_GC ();
1618 TOP = Fforward_line (TOP);
1619 AFTER_POTENTIAL_GC ();
1620 NEXT;
1621
1622 CASE (Bchar_syntax):
1623 {
1624 int c;
1625
1626 BEFORE_POTENTIAL_GC ();
1627 CHECK_CHARACTER (TOP);
1628 AFTER_POTENTIAL_GC ();
1629 c = XFASTINT (TOP);
1630 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1631 MAKE_CHAR_MULTIBYTE (c);
1632 XSETFASTINT (TOP, syntax_code_spec[(int) SYNTAX (c)]);
1633 }
1634 NEXT;
1635
1636 CASE (Bbuffer_substring):
1637 {
1638 Lisp_Object v1;
1639 BEFORE_POTENTIAL_GC ();
1640 v1 = POP;
1641 TOP = Fbuffer_substring (TOP, v1);
1642 AFTER_POTENTIAL_GC ();
1643 NEXT;
1644 }
1645
1646 CASE (Bdelete_region):
1647 {
1648 Lisp_Object v1;
1649 BEFORE_POTENTIAL_GC ();
1650 v1 = POP;
1651 TOP = Fdelete_region (TOP, v1);
1652 AFTER_POTENTIAL_GC ();
1653 NEXT;
1654 }
1655
1656 CASE (Bnarrow_to_region):
1657 {
1658 Lisp_Object v1;
1659 BEFORE_POTENTIAL_GC ();
1660 v1 = POP;
1661 TOP = Fnarrow_to_region (TOP, v1);
1662 AFTER_POTENTIAL_GC ();
1663 NEXT;
1664 }
1665
1666 CASE (Bwiden):
1667 BEFORE_POTENTIAL_GC ();
1668 PUSH (Fwiden ());
1669 AFTER_POTENTIAL_GC ();
1670 NEXT;
1671
1672 CASE (Bend_of_line):
1673 BEFORE_POTENTIAL_GC ();
1674 TOP = Fend_of_line (TOP);
1675 AFTER_POTENTIAL_GC ();
1676 NEXT;
1677
1678 CASE (Bset_marker):
1679 {
1680 Lisp_Object v1, v2;
1681 BEFORE_POTENTIAL_GC ();
1682 v1 = POP;
1683 v2 = POP;
1684 TOP = Fset_marker (TOP, v2, v1);
1685 AFTER_POTENTIAL_GC ();
1686 NEXT;
1687 }
1688
1689 CASE (Bmatch_beginning):
1690 BEFORE_POTENTIAL_GC ();
1691 TOP = Fmatch_beginning (TOP);
1692 AFTER_POTENTIAL_GC ();
1693 NEXT;
1694
1695 CASE (Bmatch_end):
1696 BEFORE_POTENTIAL_GC ();
1697 TOP = Fmatch_end (TOP);
1698 AFTER_POTENTIAL_GC ();
1699 NEXT;
1700
1701 CASE (Bupcase):
1702 BEFORE_POTENTIAL_GC ();
1703 TOP = Fupcase (TOP);
1704 AFTER_POTENTIAL_GC ();
1705 NEXT;
1706
1707 CASE (Bdowncase):
1708 BEFORE_POTENTIAL_GC ();
1709 TOP = Fdowncase (TOP);
1710 AFTER_POTENTIAL_GC ();
1711 NEXT;
1712
1713 CASE (Bstringeqlsign):
1714 {
1715 Lisp_Object v1;
1716 BEFORE_POTENTIAL_GC ();
1717 v1 = POP;
1718 TOP = Fstring_equal (TOP, v1);
1719 AFTER_POTENTIAL_GC ();
1720 NEXT;
1721 }
1722
1723 CASE (Bstringlss):
1724 {
1725 Lisp_Object v1;
1726 BEFORE_POTENTIAL_GC ();
1727 v1 = POP;
1728 TOP = Fstring_lessp (TOP, v1);
1729 AFTER_POTENTIAL_GC ();
1730 NEXT;
1731 }
1732
1733 CASE (Bequal):
1734 {
1735 Lisp_Object v1;
1736 v1 = POP;
1737 TOP = Fequal (TOP, v1);
1738 NEXT;
1739 }
1740
1741 CASE (Bnthcdr):
1742 {
1743 Lisp_Object v1;
1744 BEFORE_POTENTIAL_GC ();
1745 v1 = POP;
1746 TOP = Fnthcdr (TOP, v1);
1747 AFTER_POTENTIAL_GC ();
1748 NEXT;
1749 }
1750
1751 CASE (Belt):
1752 {
1753 Lisp_Object v1, v2;
1754 if (CONSP (TOP))
1755 {
1756 /* Exchange args and then do nth. */
1757 EMACS_INT n;
1758 BEFORE_POTENTIAL_GC ();
1759 v2 = POP;
1760 v1 = TOP;
1761 CHECK_NUMBER (v2);
1762 AFTER_POTENTIAL_GC ();
1763 n = XINT (v2);
1764 immediate_quit = 1;
1765 while (--n >= 0 && CONSP (v1))
1766 v1 = XCDR (v1);
1767 immediate_quit = 0;
1768 TOP = CAR (v1);
1769 }
1770 else
1771 {
1772 BEFORE_POTENTIAL_GC ();
1773 v1 = POP;
1774 TOP = Felt (TOP, v1);
1775 AFTER_POTENTIAL_GC ();
1776 }
1777 NEXT;
1778 }
1779
1780 CASE (Bmember):
1781 {
1782 Lisp_Object v1;
1783 BEFORE_POTENTIAL_GC ();
1784 v1 = POP;
1785 TOP = Fmember (TOP, v1);
1786 AFTER_POTENTIAL_GC ();
1787 NEXT;
1788 }
1789
1790 CASE (Bassq):
1791 {
1792 Lisp_Object v1;
1793 BEFORE_POTENTIAL_GC ();
1794 v1 = POP;
1795 TOP = Fassq (TOP, v1);
1796 AFTER_POTENTIAL_GC ();
1797 NEXT;
1798 }
1799
1800 CASE (Bnreverse):
1801 BEFORE_POTENTIAL_GC ();
1802 TOP = Fnreverse (TOP);
1803 AFTER_POTENTIAL_GC ();
1804 NEXT;
1805
1806 CASE (Bsetcar):
1807 {
1808 Lisp_Object v1;
1809 BEFORE_POTENTIAL_GC ();
1810 v1 = POP;
1811 TOP = Fsetcar (TOP, v1);
1812 AFTER_POTENTIAL_GC ();
1813 NEXT;
1814 }
1815
1816 CASE (Bsetcdr):
1817 {
1818 Lisp_Object v1;
1819 BEFORE_POTENTIAL_GC ();
1820 v1 = POP;
1821 TOP = Fsetcdr (TOP, v1);
1822 AFTER_POTENTIAL_GC ();
1823 NEXT;
1824 }
1825
1826 CASE (Bcar_safe):
1827 {
1828 Lisp_Object v1;
1829 v1 = TOP;
1830 TOP = CAR_SAFE (v1);
1831 NEXT;
1832 }
1833
1834 CASE (Bcdr_safe):
1835 {
1836 Lisp_Object v1;
1837 v1 = TOP;
1838 TOP = CDR_SAFE (v1);
1839 NEXT;
1840 }
1841
1842 CASE (Bnconc):
1843 BEFORE_POTENTIAL_GC ();
1844 DISCARD (1);
1845 TOP = Fnconc (2, &TOP);
1846 AFTER_POTENTIAL_GC ();
1847 NEXT;
1848
1849 CASE (Bnumberp):
1850 TOP = (NUMBERP (TOP) ? Qt : Qnil);
1851 NEXT;
1852
1853 CASE (Bintegerp):
1854 TOP = INTEGERP (TOP) ? Qt : Qnil;
1855 NEXT;
1856
1857 #ifdef BYTE_CODE_SAFE
1858 /* These are intentionally written using 'case' syntax,
1859 because they are incompatible with the threaded
1860 interpreter. */
1861
1862 case Bset_mark:
1863 BEFORE_POTENTIAL_GC ();
1864 error ("set-mark is an obsolete bytecode");
1865 AFTER_POTENTIAL_GC ();
1866 break;
1867 case Bscan_buffer:
1868 BEFORE_POTENTIAL_GC ();
1869 error ("scan-buffer is an obsolete bytecode");
1870 AFTER_POTENTIAL_GC ();
1871 break;
1872 #endif
1873
1874 CASE_ABORT:
1875 /* Actually this is Bstack_ref with offset 0, but we use Bdup
1876 for that instead. */
1877 /* CASE (Bstack_ref): */
1878 emacs_abort ();
1879
1880 /* Handy byte-codes for lexical binding. */
1881 CASE (Bstack_ref1):
1882 CASE (Bstack_ref2):
1883 CASE (Bstack_ref3):
1884 CASE (Bstack_ref4):
1885 CASE (Bstack_ref5):
1886 {
1887 Lisp_Object *ptr = top - (op - Bstack_ref);
1888 PUSH (*ptr);
1889 NEXT;
1890 }
1891 CASE (Bstack_ref6):
1892 {
1893 Lisp_Object *ptr = top - (FETCH);
1894 PUSH (*ptr);
1895 NEXT;
1896 }
1897 CASE (Bstack_ref7):
1898 {
1899 Lisp_Object *ptr = top - (FETCH2);
1900 PUSH (*ptr);
1901 NEXT;
1902 }
1903 CASE (Bstack_set):
1904 /* stack-set-0 = discard; stack-set-1 = discard-1-preserve-tos. */
1905 {
1906 Lisp_Object *ptr = top - (FETCH);
1907 *ptr = POP;
1908 NEXT;
1909 }
1910 CASE (Bstack_set2):
1911 {
1912 Lisp_Object *ptr = top - (FETCH2);
1913 *ptr = POP;
1914 NEXT;
1915 }
1916 CASE (BdiscardN):
1917 op = FETCH;
1918 if (op & 0x80)
1919 {
1920 op &= 0x7F;
1921 top[-op] = TOP;
1922 }
1923 DISCARD (op);
1924 NEXT;
1925
1926 CASE_DEFAULT
1927 CASE (Bconstant):
1928 #ifdef BYTE_CODE_SAFE
1929 if (op < Bconstant)
1930 {
1931 emacs_abort ();
1932 }
1933 if ((op -= Bconstant) >= const_length)
1934 {
1935 emacs_abort ();
1936 }
1937 PUSH (vectorp[op]);
1938 #else
1939 PUSH (vectorp[op - Bconstant]);
1940 #endif
1941 NEXT;
1942 }
1943 }
1944
1945 exit:
1946
1947 byte_stack_list = byte_stack_list->next;
1948
1949 /* Binds and unbinds are supposed to be compiled balanced. */
1950 if (SPECPDL_INDEX () != count)
1951 #ifdef BYTE_CODE_SAFE
1952 error ("binding stack not balanced (serious byte compiler bug)");
1953 #else
1954 emacs_abort ();
1955 #endif
1956
1957 return result;
1958 }
1959
1960 void
1961 syms_of_bytecode (void)
1962 {
1963 DEFSYM (Qbytecode, "byte-code");
1964
1965 defsubr (&Sbyte_code);
1966
1967 #ifdef BYTE_CODE_METER
1968
1969 DEFVAR_LISP ("byte-code-meter", Vbyte_code_meter,
1970 doc: /* A vector of vectors which holds a histogram of byte-code usage.
1971 \(aref (aref byte-code-meter 0) CODE) indicates how many times the byte
1972 opcode CODE has been executed.
1973 \(aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,
1974 indicates how many times the byte opcodes CODE1 and CODE2 have been
1975 executed in succession. */);
1976
1977 DEFVAR_BOOL ("byte-metering-on", byte_metering_on,
1978 doc: /* If non-nil, keep profiling information on byte code usage.
1979 The variable byte-code-meter indicates how often each byte opcode is used.
1980 If a symbol has a property named `byte-code-meter' whose value is an
1981 integer, it is incremented each time that symbol's function is called. */);
1982
1983 byte_metering_on = 0;
1984 Vbyte_code_meter = Fmake_vector (make_number (256), make_number (0));
1985 DEFSYM (Qbyte_code_meter, "byte-code-meter");
1986 {
1987 int i = 256;
1988 while (i--)
1989 ASET (Vbyte_code_meter, i,
1990 Fmake_vector (make_number (256), make_number (0)));
1991 }
1992 #endif
1993 }