* keyboard.c (parse_modifiers_uncached, parse_modifiers):
[emacs.git] / src / bytecode.c
blobce2f06dfccbb502bd7b8ae09398cfbd82b063434
1 /* Execution of byte code produced by bytecomp.el.
2 Copyright (C) 1985-1988, 1993, 2000-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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/>. */
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.
30 by Hallvard:
31 o added relative jump instructions;
32 o all conditionals now only do QUIT if they jump.
35 #include <config.h>
36 #include <setjmp.h>
37 #include "lisp.h"
38 #include "buffer.h"
39 #include "character.h"
40 #include "syntax.h"
41 #include "window.h"
43 #ifdef CHECK_FRAME_FONT
44 #include "frame.h"
45 #include "xterm.h"
46 #endif
49 * define BYTE_CODE_SAFE to enable some minor sanity checking (useful for
50 * debugging the byte compiler...)
52 * define BYTE_CODE_METER to enable generation of a byte-op usage histogram.
54 /* #define BYTE_CODE_SAFE */
55 /* #define BYTE_CODE_METER */
58 #ifdef BYTE_CODE_METER
60 Lisp_Object Qbyte_code_meter;
61 #define METER_2(code1, code2) \
62 XFASTINT (XVECTOR (XVECTOR (Vbyte_code_meter)->contents[(code1)]) \
63 ->contents[(code2)])
65 #define METER_1(code) METER_2 (0, (code))
67 #define METER_CODE(last_code, this_code) \
68 { \
69 if (byte_metering_on) \
70 { \
71 if (METER_1 (this_code) < MOST_POSITIVE_FIXNUM) \
72 METER_1 (this_code)++; \
73 if (last_code \
74 && METER_2 (last_code, this_code) < MOST_POSITIVE_FIXNUM) \
75 METER_2 (last_code, this_code)++; \
76 } \
79 #endif /* BYTE_CODE_METER */
82 Lisp_Object Qbytecode;
84 /* Byte codes: */
86 #define Bvarref 010
87 #define Bvarset 020
88 #define Bvarbind 030
89 #define Bcall 040
90 #define Bunbind 050
92 #define Bnth 070
93 #define Bsymbolp 071
94 #define Bconsp 072
95 #define Bstringp 073
96 #define Blistp 074
97 #define Beq 075
98 #define Bmemq 076
99 #define Bnot 077
100 #define Bcar 0100
101 #define Bcdr 0101
102 #define Bcons 0102
103 #define Blist1 0103
104 #define Blist2 0104
105 #define Blist3 0105
106 #define Blist4 0106
107 #define Blength 0107
108 #define Baref 0110
109 #define Baset 0111
110 #define Bsymbol_value 0112
111 #define Bsymbol_function 0113
112 #define Bset 0114
113 #define Bfset 0115
114 #define Bget 0116
115 #define Bsubstring 0117
116 #define Bconcat2 0120
117 #define Bconcat3 0121
118 #define Bconcat4 0122
119 #define Bsub1 0123
120 #define Badd1 0124
121 #define Beqlsign 0125
122 #define Bgtr 0126
123 #define Blss 0127
124 #define Bleq 0130
125 #define Bgeq 0131
126 #define Bdiff 0132
127 #define Bnegate 0133
128 #define Bplus 0134
129 #define Bmax 0135
130 #define Bmin 0136
131 #define Bmult 0137
133 #define Bpoint 0140
134 /* Was Bmark in v17. */
135 #define Bsave_current_buffer 0141
136 #define Bgoto_char 0142
137 #define Binsert 0143
138 #define Bpoint_max 0144
139 #define Bpoint_min 0145
140 #define Bchar_after 0146
141 #define Bfollowing_char 0147
142 #define Bpreceding_char 0150
143 #define Bcurrent_column 0151
144 #define Bindent_to 0152
145 #ifdef BYTE_CODE_SAFE
146 #define Bscan_buffer 0153 /* No longer generated as of v18 */
147 #endif
148 #define Beolp 0154
149 #define Beobp 0155
150 #define Bbolp 0156
151 #define Bbobp 0157
152 #define Bcurrent_buffer 0160
153 #define Bset_buffer 0161
154 #define Bsave_current_buffer_1 0162 /* Replacing Bsave_current_buffer. */
155 #if 0
156 #define Bread_char 0162 /* No longer generated as of v19 */
157 #endif
158 #ifdef BYTE_CODE_SAFE
159 #define Bset_mark 0163 /* this loser is no longer generated as of v18 */
160 #endif
161 #define Binteractive_p 0164 /* Needed since interactive-p takes unevalled args */
163 #define Bforward_char 0165
164 #define Bforward_word 0166
165 #define Bskip_chars_forward 0167
166 #define Bskip_chars_backward 0170
167 #define Bforward_line 0171
168 #define Bchar_syntax 0172
169 #define Bbuffer_substring 0173
170 #define Bdelete_region 0174
171 #define Bnarrow_to_region 0175
172 #define Bwiden 0176
173 #define Bend_of_line 0177
175 #define Bconstant2 0201
176 #define Bgoto 0202
177 #define Bgotoifnil 0203
178 #define Bgotoifnonnil 0204
179 #define Bgotoifnilelsepop 0205
180 #define Bgotoifnonnilelsepop 0206
181 #define Breturn 0207
182 #define Bdiscard 0210
183 #define Bdup 0211
185 #define Bsave_excursion 0212
186 #define Bsave_window_excursion 0213
187 #define Bsave_restriction 0214
188 #define Bcatch 0215
190 #define Bunwind_protect 0216
191 #define Bcondition_case 0217
192 #define Btemp_output_buffer_setup 0220
193 #define Btemp_output_buffer_show 0221
195 #define Bunbind_all 0222
197 #define Bset_marker 0223
198 #define Bmatch_beginning 0224
199 #define Bmatch_end 0225
200 #define Bupcase 0226
201 #define Bdowncase 0227
203 #define Bstringeqlsign 0230
204 #define Bstringlss 0231
205 #define Bequal 0232
206 #define Bnthcdr 0233
207 #define Belt 0234
208 #define Bmember 0235
209 #define Bassq 0236
210 #define Bnreverse 0237
211 #define Bsetcar 0240
212 #define Bsetcdr 0241
213 #define Bcar_safe 0242
214 #define Bcdr_safe 0243
215 #define Bnconc 0244
216 #define Bquo 0245
217 #define Brem 0246
218 #define Bnumberp 0247
219 #define Bintegerp 0250
221 #define BRgoto 0252
222 #define BRgotoifnil 0253
223 #define BRgotoifnonnil 0254
224 #define BRgotoifnilelsepop 0255
225 #define BRgotoifnonnilelsepop 0256
227 #define BlistN 0257
228 #define BconcatN 0260
229 #define BinsertN 0261
231 #define Bconstant 0300
233 /* Whether to maintain a `top' and `bottom' field in the stack frame. */
234 #define BYTE_MAINTAIN_TOP (BYTE_CODE_SAFE || BYTE_MARK_STACK)
236 /* Structure describing a value stack used during byte-code execution
237 in Fbyte_code. */
239 struct byte_stack
241 /* Program counter. This points into the byte_string below
242 and is relocated when that string is relocated. */
243 const unsigned char *pc;
245 /* Top and bottom of stack. The bottom points to an area of memory
246 allocated with alloca in Fbyte_code. */
247 #if BYTE_MAINTAIN_TOP
248 Lisp_Object *top, *bottom;
249 #endif
251 /* The string containing the byte-code, and its current address.
252 Storing this here protects it from GC because mark_byte_stack
253 marks it. */
254 Lisp_Object byte_string;
255 const unsigned char *byte_string_start;
257 /* The vector of constants used during byte-code execution. Storing
258 this here protects it from GC because mark_byte_stack marks it. */
259 Lisp_Object constants;
261 /* Next entry in byte_stack_list. */
262 struct byte_stack *next;
265 /* A list of currently active byte-code execution value stacks.
266 Fbyte_code adds an entry to the head of this list before it starts
267 processing byte-code, and it removed the entry again when it is
268 done. Signalling an error truncates the list analoguous to
269 gcprolist. */
271 struct byte_stack *byte_stack_list;
274 /* Mark objects on byte_stack_list. Called during GC. */
276 #if BYTE_MARK_STACK
277 void
278 mark_byte_stack (void)
280 struct byte_stack *stack;
281 Lisp_Object *obj;
283 for (stack = byte_stack_list; stack; stack = stack->next)
285 /* If STACK->top is null here, this means there's an opcode in
286 Fbyte_code that wasn't expected to GC, but did. To find out
287 which opcode this is, record the value of `stack', and walk
288 up the stack in a debugger, stopping in frames of Fbyte_code.
289 The culprit is found in the frame of Fbyte_code where the
290 address of its local variable `stack' is equal to the
291 recorded value of `stack' here. */
292 eassert (stack->top);
294 for (obj = stack->bottom; obj <= stack->top; ++obj)
295 mark_object (*obj);
297 mark_object (stack->byte_string);
298 mark_object (stack->constants);
301 #endif
303 /* Unmark objects in the stacks on byte_stack_list. Relocate program
304 counters. Called when GC has completed. */
306 void
307 unmark_byte_stack (void)
309 struct byte_stack *stack;
311 for (stack = byte_stack_list; stack; stack = stack->next)
313 if (stack->byte_string_start != SDATA (stack->byte_string))
315 int offset = stack->pc - stack->byte_string_start;
316 stack->byte_string_start = SDATA (stack->byte_string);
317 stack->pc = stack->byte_string_start + offset;
323 /* Fetch the next byte from the bytecode stream */
325 #define FETCH *stack.pc++
327 /* Fetch two bytes from the bytecode stream and make a 16-bit number
328 out of them */
330 #define FETCH2 (op = FETCH, op + (FETCH << 8))
332 /* Push x onto the execution stack. This used to be #define PUSH(x)
333 (*++stackp = (x)) This oddity is necessary because Alliant can't be
334 bothered to compile the preincrement operator properly, as of 4/91.
335 -JimB */
337 #define PUSH(x) (top++, *top = (x))
339 /* Pop a value off the execution stack. */
341 #define POP (*top--)
343 /* Discard n values from the execution stack. */
345 #define DISCARD(n) (top -= (n))
347 /* Get the value which is at the top of the execution stack, but don't
348 pop it. */
350 #define TOP (*top)
352 /* Actions that must be performed before and after calling a function
353 that might GC. */
355 #if !BYTE_MAINTAIN_TOP
356 #define BEFORE_POTENTIAL_GC() ((void)0)
357 #define AFTER_POTENTIAL_GC() ((void)0)
358 #else
359 #define BEFORE_POTENTIAL_GC() stack.top = top
360 #define AFTER_POTENTIAL_GC() stack.top = NULL
361 #endif
363 /* Garbage collect if we have consed enough since the last time.
364 We do this at every branch, to avoid loops that never GC. */
366 #define MAYBE_GC() \
367 do { \
368 if (consing_since_gc > gc_cons_threshold \
369 && consing_since_gc > gc_relative_threshold) \
371 BEFORE_POTENTIAL_GC (); \
372 Fgarbage_collect (); \
373 AFTER_POTENTIAL_GC (); \
375 } while (0)
377 /* Check for jumping out of range. */
379 #ifdef BYTE_CODE_SAFE
381 #define CHECK_RANGE(ARG) \
382 if (ARG >= bytestr_length) abort ()
384 #else /* not BYTE_CODE_SAFE */
386 #define CHECK_RANGE(ARG)
388 #endif /* not BYTE_CODE_SAFE */
390 /* A version of the QUIT macro which makes sure that the stack top is
391 set before signaling `quit'. */
393 #define BYTE_CODE_QUIT \
394 do { \
395 if (!NILP (Vquit_flag) && NILP (Vinhibit_quit)) \
397 Lisp_Object flag = Vquit_flag; \
398 Vquit_flag = Qnil; \
399 BEFORE_POTENTIAL_GC (); \
400 if (EQ (Vthrow_on_input, flag)) \
401 Fthrow (Vthrow_on_input, Qt); \
402 Fsignal (Qquit, Qnil); \
403 AFTER_POTENTIAL_GC (); \
405 ELSE_PENDING_SIGNALS \
406 } while (0)
409 DEFUN ("byte-code", Fbyte_code, Sbyte_code, 3, 3, 0,
410 doc: /* Function used internally in byte-compiled code.
411 The first argument, BYTESTR, is a string of byte code;
412 the second, VECTOR, a vector of constants;
413 the third, MAXDEPTH, the maximum stack depth used in this function.
414 If the third argument is incorrect, Emacs may crash. */)
415 (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth)
417 int count = SPECPDL_INDEX ();
418 #ifdef BYTE_CODE_METER
419 int this_op = 0;
420 int prev_op;
421 #endif
422 int op;
423 /* Lisp_Object v1, v2; */
424 Lisp_Object *vectorp;
425 #ifdef BYTE_CODE_SAFE
426 int const_length = XVECTOR (vector)->size;
427 Lisp_Object *stacke;
428 #endif
429 int bytestr_length;
430 struct byte_stack stack;
431 Lisp_Object *top;
432 Lisp_Object result;
434 #if 0 /* CHECK_FRAME_FONT */
436 struct frame *f = SELECTED_FRAME ();
437 if (FRAME_X_P (f)
438 && FRAME_FONT (f)->direction != 0
439 && FRAME_FONT (f)->direction != 1)
440 abort ();
442 #endif
444 CHECK_STRING (bytestr);
445 CHECK_VECTOR (vector);
446 CHECK_NUMBER (maxdepth);
448 if (STRING_MULTIBYTE (bytestr))
449 /* BYTESTR must have been produced by Emacs 20.2 or the earlier
450 because they produced a raw 8-bit string for byte-code and now
451 such a byte-code string is loaded as multibyte while raw 8-bit
452 characters converted to multibyte form. Thus, now we must
453 convert them back to the originally intended unibyte form. */
454 bytestr = Fstring_as_unibyte (bytestr);
456 bytestr_length = SBYTES (bytestr);
457 vectorp = XVECTOR (vector)->contents;
459 stack.byte_string = bytestr;
460 stack.pc = stack.byte_string_start = SDATA (bytestr);
461 stack.constants = vector;
462 top = (Lisp_Object *) alloca (XFASTINT (maxdepth)
463 * sizeof (Lisp_Object));
464 #if BYTE_MAINTAIN_TOP
465 stack.bottom = top;
466 stack.top = NULL;
467 #endif
468 top -= 1;
469 stack.next = byte_stack_list;
470 byte_stack_list = &stack;
472 #ifdef BYTE_CODE_SAFE
473 stacke = stack.bottom - 1 + XFASTINT (maxdepth);
474 #endif
476 while (1)
478 #ifdef BYTE_CODE_SAFE
479 if (top > stacke)
480 abort ();
481 else if (top < stack.bottom - 1)
482 abort ();
483 #endif
485 #ifdef BYTE_CODE_METER
486 prev_op = this_op;
487 this_op = op = FETCH;
488 METER_CODE (prev_op, op);
489 #else
490 op = FETCH;
491 #endif
493 switch (op)
495 case Bvarref + 7:
496 op = FETCH2;
497 goto varref;
499 case Bvarref:
500 case Bvarref + 1:
501 case Bvarref + 2:
502 case Bvarref + 3:
503 case Bvarref + 4:
504 case Bvarref + 5:
505 op = op - Bvarref;
506 goto varref;
508 /* This seems to be the most frequently executed byte-code
509 among the Bvarref's, so avoid a goto here. */
510 case Bvarref+6:
511 op = FETCH;
512 varref:
514 Lisp_Object v1, v2;
516 v1 = vectorp[op];
517 if (SYMBOLP (v1))
519 if (XSYMBOL (v1)->redirect != SYMBOL_PLAINVAL
520 || (v2 = SYMBOL_VAL (XSYMBOL (v1)),
521 EQ (v2, Qunbound)))
523 BEFORE_POTENTIAL_GC ();
524 v2 = Fsymbol_value (v1);
525 AFTER_POTENTIAL_GC ();
528 else
530 BEFORE_POTENTIAL_GC ();
531 v2 = Fsymbol_value (v1);
532 AFTER_POTENTIAL_GC ();
534 PUSH (v2);
535 break;
538 case Bgotoifnil:
540 Lisp_Object v1;
541 MAYBE_GC ();
542 op = FETCH2;
543 v1 = POP;
544 if (NILP (v1))
546 BYTE_CODE_QUIT;
547 CHECK_RANGE (op);
548 stack.pc = stack.byte_string_start + op;
550 break;
553 case Bcar:
555 Lisp_Object v1;
556 v1 = TOP;
557 TOP = CAR (v1);
558 break;
561 case Beq:
563 Lisp_Object v1;
564 v1 = POP;
565 TOP = EQ (v1, TOP) ? Qt : Qnil;
566 break;
569 case Bmemq:
571 Lisp_Object v1;
572 BEFORE_POTENTIAL_GC ();
573 v1 = POP;
574 TOP = Fmemq (TOP, v1);
575 AFTER_POTENTIAL_GC ();
576 break;
579 case Bcdr:
581 Lisp_Object v1;
582 v1 = TOP;
583 TOP = CDR (v1);
584 break;
587 case Bvarset:
588 case Bvarset+1:
589 case Bvarset+2:
590 case Bvarset+3:
591 case Bvarset+4:
592 case Bvarset+5:
593 op -= Bvarset;
594 goto varset;
596 case Bvarset+7:
597 op = FETCH2;
598 goto varset;
600 case Bvarset+6:
601 op = FETCH;
602 varset:
604 Lisp_Object sym, val;
606 sym = vectorp[op];
607 val = TOP;
609 /* Inline the most common case. */
610 if (SYMBOLP (sym)
611 && !EQ (val, Qunbound)
612 && !XSYMBOL (sym)->redirect
613 && !SYMBOL_CONSTANT_P (sym))
614 XSYMBOL (sym)->val.value = val;
615 else
617 BEFORE_POTENTIAL_GC ();
618 set_internal (sym, val, Qnil, 0);
619 AFTER_POTENTIAL_GC ();
622 (void) POP;
623 break;
625 case Bdup:
627 Lisp_Object v1;
628 v1 = TOP;
629 PUSH (v1);
630 break;
633 /* ------------------ */
635 case Bvarbind+6:
636 op = FETCH;
637 goto varbind;
639 case Bvarbind+7:
640 op = FETCH2;
641 goto varbind;
643 case Bvarbind:
644 case Bvarbind+1:
645 case Bvarbind+2:
646 case Bvarbind+3:
647 case Bvarbind+4:
648 case Bvarbind+5:
649 op -= Bvarbind;
650 varbind:
651 /* Specbind can signal and thus GC. */
652 BEFORE_POTENTIAL_GC ();
653 specbind (vectorp[op], POP);
654 AFTER_POTENTIAL_GC ();
655 break;
657 case Bcall+6:
658 op = FETCH;
659 goto docall;
661 case Bcall+7:
662 op = FETCH2;
663 goto docall;
665 case Bcall:
666 case Bcall+1:
667 case Bcall+2:
668 case Bcall+3:
669 case Bcall+4:
670 case Bcall+5:
671 op -= Bcall;
672 docall:
674 BEFORE_POTENTIAL_GC ();
675 DISCARD (op);
676 #ifdef BYTE_CODE_METER
677 if (byte_metering_on && SYMBOLP (TOP))
679 Lisp_Object v1, v2;
681 v1 = TOP;
682 v2 = Fget (v1, Qbyte_code_meter);
683 if (INTEGERP (v2)
684 && XINT (v2) < MOST_POSITIVE_FIXNUM)
686 XSETINT (v2, XINT (v2) + 1);
687 Fput (v1, Qbyte_code_meter, v2);
690 #endif
691 TOP = Ffuncall (op + 1, &TOP);
692 AFTER_POTENTIAL_GC ();
693 break;
696 case Bunbind+6:
697 op = FETCH;
698 goto dounbind;
700 case Bunbind+7:
701 op = FETCH2;
702 goto dounbind;
704 case Bunbind:
705 case Bunbind+1:
706 case Bunbind+2:
707 case Bunbind+3:
708 case Bunbind+4:
709 case Bunbind+5:
710 op -= Bunbind;
711 dounbind:
712 BEFORE_POTENTIAL_GC ();
713 unbind_to (SPECPDL_INDEX () - op, Qnil);
714 AFTER_POTENTIAL_GC ();
715 break;
717 case Bunbind_all:
718 /* To unbind back to the beginning of this frame. Not used yet,
719 but will be needed for tail-recursion elimination. */
720 BEFORE_POTENTIAL_GC ();
721 unbind_to (count, Qnil);
722 AFTER_POTENTIAL_GC ();
723 break;
725 case Bgoto:
726 MAYBE_GC ();
727 BYTE_CODE_QUIT;
728 op = FETCH2; /* pc = FETCH2 loses since FETCH2 contains pc++ */
729 CHECK_RANGE (op);
730 stack.pc = stack.byte_string_start + op;
731 break;
733 case Bgotoifnonnil:
735 Lisp_Object v1;
736 MAYBE_GC ();
737 op = FETCH2;
738 v1 = POP;
739 if (!NILP (v1))
741 BYTE_CODE_QUIT;
742 CHECK_RANGE (op);
743 stack.pc = stack.byte_string_start + op;
745 break;
748 case Bgotoifnilelsepop:
749 MAYBE_GC ();
750 op = FETCH2;
751 if (NILP (TOP))
753 BYTE_CODE_QUIT;
754 CHECK_RANGE (op);
755 stack.pc = stack.byte_string_start + op;
757 else DISCARD (1);
758 break;
760 case Bgotoifnonnilelsepop:
761 MAYBE_GC ();
762 op = FETCH2;
763 if (!NILP (TOP))
765 BYTE_CODE_QUIT;
766 CHECK_RANGE (op);
767 stack.pc = stack.byte_string_start + op;
769 else DISCARD (1);
770 break;
772 case BRgoto:
773 MAYBE_GC ();
774 BYTE_CODE_QUIT;
775 stack.pc += (int) *stack.pc - 127;
776 break;
778 case BRgotoifnil:
780 Lisp_Object v1;
781 MAYBE_GC ();
782 v1 = POP;
783 if (NILP (v1))
785 BYTE_CODE_QUIT;
786 stack.pc += (int) *stack.pc - 128;
788 stack.pc++;
789 break;
792 case BRgotoifnonnil:
794 Lisp_Object v1;
795 MAYBE_GC ();
796 v1 = POP;
797 if (!NILP (v1))
799 BYTE_CODE_QUIT;
800 stack.pc += (int) *stack.pc - 128;
802 stack.pc++;
803 break;
806 case BRgotoifnilelsepop:
807 MAYBE_GC ();
808 op = *stack.pc++;
809 if (NILP (TOP))
811 BYTE_CODE_QUIT;
812 stack.pc += op - 128;
814 else DISCARD (1);
815 break;
817 case BRgotoifnonnilelsepop:
818 MAYBE_GC ();
819 op = *stack.pc++;
820 if (!NILP (TOP))
822 BYTE_CODE_QUIT;
823 stack.pc += op - 128;
825 else DISCARD (1);
826 break;
828 case Breturn:
829 result = POP;
830 goto exit;
832 case Bdiscard:
833 DISCARD (1);
834 break;
836 case Bconstant2:
837 PUSH (vectorp[FETCH2]);
838 break;
840 case Bsave_excursion:
841 record_unwind_protect (save_excursion_restore,
842 save_excursion_save ());
843 break;
845 case Bsave_current_buffer:
846 case Bsave_current_buffer_1:
847 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
848 break;
850 case Bsave_window_excursion:
851 BEFORE_POTENTIAL_GC ();
852 TOP = Fsave_window_excursion (TOP);
853 AFTER_POTENTIAL_GC ();
854 break;
856 case Bsave_restriction:
857 record_unwind_protect (save_restriction_restore,
858 save_restriction_save ());
859 break;
861 case Bcatch:
863 Lisp_Object v1;
864 BEFORE_POTENTIAL_GC ();
865 v1 = POP;
866 TOP = internal_catch (TOP, Feval, v1);
867 AFTER_POTENTIAL_GC ();
868 break;
871 case Bunwind_protect:
872 record_unwind_protect (Fprogn, POP);
873 break;
875 case Bcondition_case:
877 Lisp_Object handlers, body;
878 handlers = POP;
879 body = POP;
880 BEFORE_POTENTIAL_GC ();
881 TOP = internal_lisp_condition_case (TOP, body, handlers);
882 AFTER_POTENTIAL_GC ();
883 break;
886 case Btemp_output_buffer_setup:
887 BEFORE_POTENTIAL_GC ();
888 CHECK_STRING (TOP);
889 temp_output_buffer_setup (SSDATA (TOP));
890 AFTER_POTENTIAL_GC ();
891 TOP = Vstandard_output;
892 break;
894 case Btemp_output_buffer_show:
896 Lisp_Object v1;
897 BEFORE_POTENTIAL_GC ();
898 v1 = POP;
899 temp_output_buffer_show (TOP);
900 TOP = v1;
901 /* pop binding of standard-output */
902 unbind_to (SPECPDL_INDEX () - 1, Qnil);
903 AFTER_POTENTIAL_GC ();
904 break;
907 case Bnth:
909 Lisp_Object v1, v2;
910 BEFORE_POTENTIAL_GC ();
911 v1 = POP;
912 v2 = TOP;
913 CHECK_NUMBER (v2);
914 AFTER_POTENTIAL_GC ();
915 op = XINT (v2);
916 immediate_quit = 1;
917 while (--op >= 0 && CONSP (v1))
918 v1 = XCDR (v1);
919 immediate_quit = 0;
920 TOP = CAR (v1);
921 break;
924 case Bsymbolp:
925 TOP = SYMBOLP (TOP) ? Qt : Qnil;
926 break;
928 case Bconsp:
929 TOP = CONSP (TOP) ? Qt : Qnil;
930 break;
932 case Bstringp:
933 TOP = STRINGP (TOP) ? Qt : Qnil;
934 break;
936 case Blistp:
937 TOP = CONSP (TOP) || NILP (TOP) ? Qt : Qnil;
938 break;
940 case Bnot:
941 TOP = NILP (TOP) ? Qt : Qnil;
942 break;
944 case Bcons:
946 Lisp_Object v1;
947 v1 = POP;
948 TOP = Fcons (TOP, v1);
949 break;
952 case Blist1:
953 TOP = Fcons (TOP, Qnil);
954 break;
956 case Blist2:
958 Lisp_Object v1;
959 v1 = POP;
960 TOP = Fcons (TOP, Fcons (v1, Qnil));
961 break;
964 case Blist3:
965 DISCARD (2);
966 TOP = Flist (3, &TOP);
967 break;
969 case Blist4:
970 DISCARD (3);
971 TOP = Flist (4, &TOP);
972 break;
974 case BlistN:
975 op = FETCH;
976 DISCARD (op - 1);
977 TOP = Flist (op, &TOP);
978 break;
980 case Blength:
981 BEFORE_POTENTIAL_GC ();
982 TOP = Flength (TOP);
983 AFTER_POTENTIAL_GC ();
984 break;
986 case Baref:
988 Lisp_Object v1;
989 BEFORE_POTENTIAL_GC ();
990 v1 = POP;
991 TOP = Faref (TOP, v1);
992 AFTER_POTENTIAL_GC ();
993 break;
996 case Baset:
998 Lisp_Object v1, v2;
999 BEFORE_POTENTIAL_GC ();
1000 v2 = POP; v1 = POP;
1001 TOP = Faset (TOP, v1, v2);
1002 AFTER_POTENTIAL_GC ();
1003 break;
1006 case Bsymbol_value:
1007 BEFORE_POTENTIAL_GC ();
1008 TOP = Fsymbol_value (TOP);
1009 AFTER_POTENTIAL_GC ();
1010 break;
1012 case Bsymbol_function:
1013 BEFORE_POTENTIAL_GC ();
1014 TOP = Fsymbol_function (TOP);
1015 AFTER_POTENTIAL_GC ();
1016 break;
1018 case Bset:
1020 Lisp_Object v1;
1021 BEFORE_POTENTIAL_GC ();
1022 v1 = POP;
1023 TOP = Fset (TOP, v1);
1024 AFTER_POTENTIAL_GC ();
1025 break;
1028 case Bfset:
1030 Lisp_Object v1;
1031 BEFORE_POTENTIAL_GC ();
1032 v1 = POP;
1033 TOP = Ffset (TOP, v1);
1034 AFTER_POTENTIAL_GC ();
1035 break;
1038 case Bget:
1040 Lisp_Object v1;
1041 BEFORE_POTENTIAL_GC ();
1042 v1 = POP;
1043 TOP = Fget (TOP, v1);
1044 AFTER_POTENTIAL_GC ();
1045 break;
1048 case Bsubstring:
1050 Lisp_Object v1, v2;
1051 BEFORE_POTENTIAL_GC ();
1052 v2 = POP; v1 = POP;
1053 TOP = Fsubstring (TOP, v1, v2);
1054 AFTER_POTENTIAL_GC ();
1055 break;
1058 case Bconcat2:
1059 BEFORE_POTENTIAL_GC ();
1060 DISCARD (1);
1061 TOP = Fconcat (2, &TOP);
1062 AFTER_POTENTIAL_GC ();
1063 break;
1065 case Bconcat3:
1066 BEFORE_POTENTIAL_GC ();
1067 DISCARD (2);
1068 TOP = Fconcat (3, &TOP);
1069 AFTER_POTENTIAL_GC ();
1070 break;
1072 case Bconcat4:
1073 BEFORE_POTENTIAL_GC ();
1074 DISCARD (3);
1075 TOP = Fconcat (4, &TOP);
1076 AFTER_POTENTIAL_GC ();
1077 break;
1079 case BconcatN:
1080 op = FETCH;
1081 BEFORE_POTENTIAL_GC ();
1082 DISCARD (op - 1);
1083 TOP = Fconcat (op, &TOP);
1084 AFTER_POTENTIAL_GC ();
1085 break;
1087 case Bsub1:
1089 Lisp_Object v1;
1090 v1 = TOP;
1091 if (INTEGERP (v1))
1093 XSETINT (v1, XINT (v1) - 1);
1094 TOP = v1;
1096 else
1098 BEFORE_POTENTIAL_GC ();
1099 TOP = Fsub1 (v1);
1100 AFTER_POTENTIAL_GC ();
1102 break;
1105 case Badd1:
1107 Lisp_Object v1;
1108 v1 = TOP;
1109 if (INTEGERP (v1))
1111 XSETINT (v1, XINT (v1) + 1);
1112 TOP = v1;
1114 else
1116 BEFORE_POTENTIAL_GC ();
1117 TOP = Fadd1 (v1);
1118 AFTER_POTENTIAL_GC ();
1120 break;
1123 case Beqlsign:
1125 Lisp_Object v1, v2;
1126 BEFORE_POTENTIAL_GC ();
1127 v2 = POP; v1 = TOP;
1128 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v1);
1129 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v2);
1130 AFTER_POTENTIAL_GC ();
1131 if (FLOATP (v1) || FLOATP (v2))
1133 double f1, f2;
1135 f1 = (FLOATP (v1) ? XFLOAT_DATA (v1) : XINT (v1));
1136 f2 = (FLOATP (v2) ? XFLOAT_DATA (v2) : XINT (v2));
1137 TOP = (f1 == f2 ? Qt : Qnil);
1139 else
1140 TOP = (XINT (v1) == XINT (v2) ? Qt : Qnil);
1141 break;
1144 case Bgtr:
1146 Lisp_Object v1;
1147 BEFORE_POTENTIAL_GC ();
1148 v1 = POP;
1149 TOP = Fgtr (TOP, v1);
1150 AFTER_POTENTIAL_GC ();
1151 break;
1154 case Blss:
1156 Lisp_Object v1;
1157 BEFORE_POTENTIAL_GC ();
1158 v1 = POP;
1159 TOP = Flss (TOP, v1);
1160 AFTER_POTENTIAL_GC ();
1161 break;
1164 case Bleq:
1166 Lisp_Object v1;
1167 BEFORE_POTENTIAL_GC ();
1168 v1 = POP;
1169 TOP = Fleq (TOP, v1);
1170 AFTER_POTENTIAL_GC ();
1171 break;
1174 case Bgeq:
1176 Lisp_Object v1;
1177 BEFORE_POTENTIAL_GC ();
1178 v1 = POP;
1179 TOP = Fgeq (TOP, v1);
1180 AFTER_POTENTIAL_GC ();
1181 break;
1184 case Bdiff:
1185 BEFORE_POTENTIAL_GC ();
1186 DISCARD (1);
1187 TOP = Fminus (2, &TOP);
1188 AFTER_POTENTIAL_GC ();
1189 break;
1191 case Bnegate:
1193 Lisp_Object v1;
1194 v1 = TOP;
1195 if (INTEGERP (v1))
1197 XSETINT (v1, - XINT (v1));
1198 TOP = v1;
1200 else
1202 BEFORE_POTENTIAL_GC ();
1203 TOP = Fminus (1, &TOP);
1204 AFTER_POTENTIAL_GC ();
1206 break;
1209 case Bplus:
1210 BEFORE_POTENTIAL_GC ();
1211 DISCARD (1);
1212 TOP = Fplus (2, &TOP);
1213 AFTER_POTENTIAL_GC ();
1214 break;
1216 case Bmax:
1217 BEFORE_POTENTIAL_GC ();
1218 DISCARD (1);
1219 TOP = Fmax (2, &TOP);
1220 AFTER_POTENTIAL_GC ();
1221 break;
1223 case Bmin:
1224 BEFORE_POTENTIAL_GC ();
1225 DISCARD (1);
1226 TOP = Fmin (2, &TOP);
1227 AFTER_POTENTIAL_GC ();
1228 break;
1230 case Bmult:
1231 BEFORE_POTENTIAL_GC ();
1232 DISCARD (1);
1233 TOP = Ftimes (2, &TOP);
1234 AFTER_POTENTIAL_GC ();
1235 break;
1237 case Bquo:
1238 BEFORE_POTENTIAL_GC ();
1239 DISCARD (1);
1240 TOP = Fquo (2, &TOP);
1241 AFTER_POTENTIAL_GC ();
1242 break;
1244 case Brem:
1246 Lisp_Object v1;
1247 BEFORE_POTENTIAL_GC ();
1248 v1 = POP;
1249 TOP = Frem (TOP, v1);
1250 AFTER_POTENTIAL_GC ();
1251 break;
1254 case Bpoint:
1256 Lisp_Object v1;
1257 XSETFASTINT (v1, PT);
1258 PUSH (v1);
1259 break;
1262 case Bgoto_char:
1263 BEFORE_POTENTIAL_GC ();
1264 TOP = Fgoto_char (TOP);
1265 AFTER_POTENTIAL_GC ();
1266 break;
1268 case Binsert:
1269 BEFORE_POTENTIAL_GC ();
1270 TOP = Finsert (1, &TOP);
1271 AFTER_POTENTIAL_GC ();
1272 break;
1274 case BinsertN:
1275 op = FETCH;
1276 BEFORE_POTENTIAL_GC ();
1277 DISCARD (op - 1);
1278 TOP = Finsert (op, &TOP);
1279 AFTER_POTENTIAL_GC ();
1280 break;
1282 case Bpoint_max:
1284 Lisp_Object v1;
1285 XSETFASTINT (v1, ZV);
1286 PUSH (v1);
1287 break;
1290 case Bpoint_min:
1292 Lisp_Object v1;
1293 XSETFASTINT (v1, BEGV);
1294 PUSH (v1);
1295 break;
1298 case Bchar_after:
1299 BEFORE_POTENTIAL_GC ();
1300 TOP = Fchar_after (TOP);
1301 AFTER_POTENTIAL_GC ();
1302 break;
1304 case Bfollowing_char:
1306 Lisp_Object v1;
1307 BEFORE_POTENTIAL_GC ();
1308 v1 = Ffollowing_char ();
1309 AFTER_POTENTIAL_GC ();
1310 PUSH (v1);
1311 break;
1314 case Bpreceding_char:
1316 Lisp_Object v1;
1317 BEFORE_POTENTIAL_GC ();
1318 v1 = Fprevious_char ();
1319 AFTER_POTENTIAL_GC ();
1320 PUSH (v1);
1321 break;
1324 case Bcurrent_column:
1326 Lisp_Object v1;
1327 BEFORE_POTENTIAL_GC ();
1328 XSETFASTINT (v1, current_column ());
1329 AFTER_POTENTIAL_GC ();
1330 PUSH (v1);
1331 break;
1334 case Bindent_to:
1335 BEFORE_POTENTIAL_GC ();
1336 TOP = Findent_to (TOP, Qnil);
1337 AFTER_POTENTIAL_GC ();
1338 break;
1340 case Beolp:
1341 PUSH (Feolp ());
1342 break;
1344 case Beobp:
1345 PUSH (Feobp ());
1346 break;
1348 case Bbolp:
1349 PUSH (Fbolp ());
1350 break;
1352 case Bbobp:
1353 PUSH (Fbobp ());
1354 break;
1356 case Bcurrent_buffer:
1357 PUSH (Fcurrent_buffer ());
1358 break;
1360 case Bset_buffer:
1361 BEFORE_POTENTIAL_GC ();
1362 TOP = Fset_buffer (TOP);
1363 AFTER_POTENTIAL_GC ();
1364 break;
1366 case Binteractive_p:
1367 PUSH (Finteractive_p ());
1368 break;
1370 case Bforward_char:
1371 BEFORE_POTENTIAL_GC ();
1372 TOP = Fforward_char (TOP);
1373 AFTER_POTENTIAL_GC ();
1374 break;
1376 case Bforward_word:
1377 BEFORE_POTENTIAL_GC ();
1378 TOP = Fforward_word (TOP);
1379 AFTER_POTENTIAL_GC ();
1380 break;
1382 case Bskip_chars_forward:
1384 Lisp_Object v1;
1385 BEFORE_POTENTIAL_GC ();
1386 v1 = POP;
1387 TOP = Fskip_chars_forward (TOP, v1);
1388 AFTER_POTENTIAL_GC ();
1389 break;
1392 case Bskip_chars_backward:
1394 Lisp_Object v1;
1395 BEFORE_POTENTIAL_GC ();
1396 v1 = POP;
1397 TOP = Fskip_chars_backward (TOP, v1);
1398 AFTER_POTENTIAL_GC ();
1399 break;
1402 case Bforward_line:
1403 BEFORE_POTENTIAL_GC ();
1404 TOP = Fforward_line (TOP);
1405 AFTER_POTENTIAL_GC ();
1406 break;
1408 case Bchar_syntax:
1410 int c;
1412 BEFORE_POTENTIAL_GC ();
1413 CHECK_CHARACTER (TOP);
1414 AFTER_POTENTIAL_GC ();
1415 c = XFASTINT (TOP);
1416 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1417 MAKE_CHAR_MULTIBYTE (c);
1418 XSETFASTINT (TOP, syntax_code_spec[(int) SYNTAX (c)]);
1420 break;
1422 case Bbuffer_substring:
1424 Lisp_Object v1;
1425 BEFORE_POTENTIAL_GC ();
1426 v1 = POP;
1427 TOP = Fbuffer_substring (TOP, v1);
1428 AFTER_POTENTIAL_GC ();
1429 break;
1432 case Bdelete_region:
1434 Lisp_Object v1;
1435 BEFORE_POTENTIAL_GC ();
1436 v1 = POP;
1437 TOP = Fdelete_region (TOP, v1);
1438 AFTER_POTENTIAL_GC ();
1439 break;
1442 case Bnarrow_to_region:
1444 Lisp_Object v1;
1445 BEFORE_POTENTIAL_GC ();
1446 v1 = POP;
1447 TOP = Fnarrow_to_region (TOP, v1);
1448 AFTER_POTENTIAL_GC ();
1449 break;
1452 case Bwiden:
1453 BEFORE_POTENTIAL_GC ();
1454 PUSH (Fwiden ());
1455 AFTER_POTENTIAL_GC ();
1456 break;
1458 case Bend_of_line:
1459 BEFORE_POTENTIAL_GC ();
1460 TOP = Fend_of_line (TOP);
1461 AFTER_POTENTIAL_GC ();
1462 break;
1464 case Bset_marker:
1466 Lisp_Object v1, v2;
1467 BEFORE_POTENTIAL_GC ();
1468 v1 = POP;
1469 v2 = POP;
1470 TOP = Fset_marker (TOP, v2, v1);
1471 AFTER_POTENTIAL_GC ();
1472 break;
1475 case Bmatch_beginning:
1476 BEFORE_POTENTIAL_GC ();
1477 TOP = Fmatch_beginning (TOP);
1478 AFTER_POTENTIAL_GC ();
1479 break;
1481 case Bmatch_end:
1482 BEFORE_POTENTIAL_GC ();
1483 TOP = Fmatch_end (TOP);
1484 AFTER_POTENTIAL_GC ();
1485 break;
1487 case Bupcase:
1488 BEFORE_POTENTIAL_GC ();
1489 TOP = Fupcase (TOP);
1490 AFTER_POTENTIAL_GC ();
1491 break;
1493 case Bdowncase:
1494 BEFORE_POTENTIAL_GC ();
1495 TOP = Fdowncase (TOP);
1496 AFTER_POTENTIAL_GC ();
1497 break;
1499 case Bstringeqlsign:
1501 Lisp_Object v1;
1502 BEFORE_POTENTIAL_GC ();
1503 v1 = POP;
1504 TOP = Fstring_equal (TOP, v1);
1505 AFTER_POTENTIAL_GC ();
1506 break;
1509 case Bstringlss:
1511 Lisp_Object v1;
1512 BEFORE_POTENTIAL_GC ();
1513 v1 = POP;
1514 TOP = Fstring_lessp (TOP, v1);
1515 AFTER_POTENTIAL_GC ();
1516 break;
1519 case Bequal:
1521 Lisp_Object v1;
1522 v1 = POP;
1523 TOP = Fequal (TOP, v1);
1524 break;
1527 case Bnthcdr:
1529 Lisp_Object v1;
1530 BEFORE_POTENTIAL_GC ();
1531 v1 = POP;
1532 TOP = Fnthcdr (TOP, v1);
1533 AFTER_POTENTIAL_GC ();
1534 break;
1537 case Belt:
1539 Lisp_Object v1, v2;
1540 if (CONSP (TOP))
1542 /* Exchange args and then do nth. */
1543 BEFORE_POTENTIAL_GC ();
1544 v2 = POP;
1545 v1 = TOP;
1546 CHECK_NUMBER (v2);
1547 AFTER_POTENTIAL_GC ();
1548 op = XINT (v2);
1549 immediate_quit = 1;
1550 while (--op >= 0 && CONSP (v1))
1551 v1 = XCDR (v1);
1552 immediate_quit = 0;
1553 TOP = CAR (v1);
1555 else
1557 BEFORE_POTENTIAL_GC ();
1558 v1 = POP;
1559 TOP = Felt (TOP, v1);
1560 AFTER_POTENTIAL_GC ();
1562 break;
1565 case Bmember:
1567 Lisp_Object v1;
1568 BEFORE_POTENTIAL_GC ();
1569 v1 = POP;
1570 TOP = Fmember (TOP, v1);
1571 AFTER_POTENTIAL_GC ();
1572 break;
1575 case Bassq:
1577 Lisp_Object v1;
1578 BEFORE_POTENTIAL_GC ();
1579 v1 = POP;
1580 TOP = Fassq (TOP, v1);
1581 AFTER_POTENTIAL_GC ();
1582 break;
1585 case Bnreverse:
1586 BEFORE_POTENTIAL_GC ();
1587 TOP = Fnreverse (TOP);
1588 AFTER_POTENTIAL_GC ();
1589 break;
1591 case Bsetcar:
1593 Lisp_Object v1;
1594 BEFORE_POTENTIAL_GC ();
1595 v1 = POP;
1596 TOP = Fsetcar (TOP, v1);
1597 AFTER_POTENTIAL_GC ();
1598 break;
1601 case Bsetcdr:
1603 Lisp_Object v1;
1604 BEFORE_POTENTIAL_GC ();
1605 v1 = POP;
1606 TOP = Fsetcdr (TOP, v1);
1607 AFTER_POTENTIAL_GC ();
1608 break;
1611 case Bcar_safe:
1613 Lisp_Object v1;
1614 v1 = TOP;
1615 TOP = CAR_SAFE (v1);
1616 break;
1619 case Bcdr_safe:
1621 Lisp_Object v1;
1622 v1 = TOP;
1623 TOP = CDR_SAFE (v1);
1624 break;
1627 case Bnconc:
1628 BEFORE_POTENTIAL_GC ();
1629 DISCARD (1);
1630 TOP = Fnconc (2, &TOP);
1631 AFTER_POTENTIAL_GC ();
1632 break;
1634 case Bnumberp:
1635 TOP = (NUMBERP (TOP) ? Qt : Qnil);
1636 break;
1638 case Bintegerp:
1639 TOP = INTEGERP (TOP) ? Qt : Qnil;
1640 break;
1642 #ifdef BYTE_CODE_SAFE
1643 case Bset_mark:
1644 BEFORE_POTENTIAL_GC ();
1645 error ("set-mark is an obsolete bytecode");
1646 AFTER_POTENTIAL_GC ();
1647 break;
1648 case Bscan_buffer:
1649 BEFORE_POTENTIAL_GC ();
1650 error ("scan-buffer is an obsolete bytecode");
1651 AFTER_POTENTIAL_GC ();
1652 break;
1653 #endif
1655 case 0:
1656 abort ();
1658 case 255:
1659 default:
1660 #ifdef BYTE_CODE_SAFE
1661 if (op < Bconstant)
1663 abort ();
1665 if ((op -= Bconstant) >= const_length)
1667 abort ();
1669 PUSH (vectorp[op]);
1670 #else
1671 PUSH (vectorp[op - Bconstant]);
1672 #endif
1676 exit:
1678 byte_stack_list = byte_stack_list->next;
1680 /* Binds and unbinds are supposed to be compiled balanced. */
1681 if (SPECPDL_INDEX () != count)
1682 #ifdef BYTE_CODE_SAFE
1683 error ("binding stack not balanced (serious byte compiler bug)");
1684 #else
1685 abort ();
1686 #endif
1688 return result;
1691 void
1692 syms_of_bytecode (void)
1694 Qbytecode = intern_c_string ("byte-code");
1695 staticpro (&Qbytecode);
1697 defsubr (&Sbyte_code);
1699 #ifdef BYTE_CODE_METER
1701 DEFVAR_LISP ("byte-code-meter", Vbyte_code_meter,
1702 doc: /* A vector of vectors which holds a histogram of byte-code usage.
1703 \(aref (aref byte-code-meter 0) CODE) indicates how many times the byte
1704 opcode CODE has been executed.
1705 \(aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,
1706 indicates how many times the byte opcodes CODE1 and CODE2 have been
1707 executed in succession. */);
1709 DEFVAR_BOOL ("byte-metering-on", byte_metering_on,
1710 doc: /* If non-nil, keep profiling information on byte code usage.
1711 The variable byte-code-meter indicates how often each byte opcode is used.
1712 If a symbol has a property named `byte-code-meter' whose value is an
1713 integer, it is incremented each time that symbol's function is called. */);
1715 byte_metering_on = 0;
1716 Vbyte_code_meter = Fmake_vector (make_number (256), make_number (0));
1717 Qbyte_code_meter = intern_c_string ("byte-code-meter");
1718 staticpro (&Qbyte_code_meter);
1720 int i = 256;
1721 while (i--)
1722 XVECTOR (Vbyte_code_meter)->contents[i] =
1723 Fmake_vector (make_number (256), make_number (0));
1725 #endif