(kill-comment): Fixed by rewriting it with syntax-tables rather than regexps
[emacs.git] / src / bytecode.c
blobd2d74b0b59a4308c02cafd8ad1a621709e17d70f
1 /* Execution of byte code produced by bytecomp.el.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993 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 2, or (at your option)
9 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; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
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>
37 #include "lisp.h"
38 #include "buffer.h"
39 #include "charset.h"
40 #include "syntax.h"
43 * define BYTE_CODE_SAFE to enable some minor sanity checking (useful for
44 * debugging the byte compiler...)
46 * define BYTE_CODE_METER to enable generation of a byte-op usage histogram.
48 /* #define BYTE_CODE_SAFE */
49 /* #define BYTE_CODE_METER */
52 #ifdef BYTE_CODE_METER
54 Lisp_Object Vbyte_code_meter, Qbyte_code_meter;
55 int byte_metering_on;
57 #define METER_2(code1, code2) \
58 XFASTINT (XVECTOR (XVECTOR (Vbyte_code_meter)->contents[(code1)]) \
59 ->contents[(code2)])
61 #define METER_1(code) METER_2 (0, (code))
63 #define METER_CODE(last_code, this_code) \
64 { \
65 if (byte_metering_on) \
66 { \
67 if (METER_1 (this_code) != ((1<<VALBITS)-1)) \
68 METER_1 (this_code)++; \
69 if (last_code \
70 && METER_2 (last_code, this_code) != ((1<<VALBITS)-1))\
71 METER_2 (last_code, this_code)++; \
72 } \
75 #else /* no BYTE_CODE_METER */
77 #define METER_CODE(last_code, this_code)
79 #endif /* no 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 #define Bscan_buffer 0153 /* No longer generated as of v18 */
146 #define Beolp 0154
147 #define Beobp 0155
148 #define Bbolp 0156
149 #define Bbobp 0157
150 #define Bcurrent_buffer 0160
151 #define Bset_buffer 0161
152 #define Bsave_current_buffer_1 0162 /* Replacing Bsave_current_buffer. */
153 #define Bread_char 0162 /* No longer generated as of v19 */
154 #define Bset_mark 0163 /* this loser is no longer generated as of v18 */
155 #define Binteractive_p 0164 /* Needed since interactive-p takes unevalled args */
157 #define Bforward_char 0165
158 #define Bforward_word 0166
159 #define Bskip_chars_forward 0167
160 #define Bskip_chars_backward 0170
161 #define Bforward_line 0171
162 #define Bchar_syntax 0172
163 #define Bbuffer_substring 0173
164 #define Bdelete_region 0174
165 #define Bnarrow_to_region 0175
166 #define Bwiden 0176
167 #define Bend_of_line 0177
169 #define Bconstant2 0201
170 #define Bgoto 0202
171 #define Bgotoifnil 0203
172 #define Bgotoifnonnil 0204
173 #define Bgotoifnilelsepop 0205
174 #define Bgotoifnonnilelsepop 0206
175 #define Breturn 0207
176 #define Bdiscard 0210
177 #define Bdup 0211
179 #define Bsave_excursion 0212
180 #define Bsave_window_excursion 0213
181 #define Bsave_restriction 0214
182 #define Bcatch 0215
184 #define Bunwind_protect 0216
185 #define Bcondition_case 0217
186 #define Btemp_output_buffer_setup 0220
187 #define Btemp_output_buffer_show 0221
189 #define Bunbind_all 0222
191 #define Bset_marker 0223
192 #define Bmatch_beginning 0224
193 #define Bmatch_end 0225
194 #define Bupcase 0226
195 #define Bdowncase 0227
197 #define Bstringeqlsign 0230
198 #define Bstringlss 0231
199 #define Bequal 0232
200 #define Bnthcdr 0233
201 #define Belt 0234
202 #define Bmember 0235
203 #define Bassq 0236
204 #define Bnreverse 0237
205 #define Bsetcar 0240
206 #define Bsetcdr 0241
207 #define Bcar_safe 0242
208 #define Bcdr_safe 0243
209 #define Bnconc 0244
210 #define Bquo 0245
211 #define Brem 0246
212 #define Bnumberp 0247
213 #define Bintegerp 0250
215 #define BRgoto 0252
216 #define BRgotoifnil 0253
217 #define BRgotoifnonnil 0254
218 #define BRgotoifnilelsepop 0255
219 #define BRgotoifnonnilelsepop 0256
221 #define BlistN 0257
222 #define BconcatN 0260
223 #define BinsertN 0261
225 #define Bconstant 0300
226 #define CONSTANTLIM 0100
229 /* Structure describing a value stack used during byte-code execution
230 in Fbyte_code. */
232 struct byte_stack
234 /* Program counter. This points into the byte_string below
235 and is relocated when that string is relocated. */
236 unsigned char *pc;
238 /* Top and bottom of stack. The bottom points to an area of memory
239 allocated with alloca in Fbyte_code. */
240 Lisp_Object *top, *bottom;
242 /* The string containing the byte-code, and its current address.
243 Storing this here protects it from GC because mark_byte_stack
244 marks it. */
245 Lisp_Object byte_string;
246 unsigned char *byte_string_start;
248 /* The vector of constants used during byte-code execution. Storing
249 this here protects it from GC because mark_byte_stack marks it. */
250 Lisp_Object constants;
252 /* Next entry in byte_stack_list. */
253 struct byte_stack *next;
256 /* A list of currently active byte-code execution value stacks.
257 Fbyte_code adds an entry to the head of this list before it starts
258 processing byte-code, and it removed the entry again when it is
259 done. Signalling an error truncates the list analoguous to
260 gcprolist. */
262 struct byte_stack *byte_stack_list;
265 /* Mark objects on byte_stack_list. Called during GC. */
267 void
268 mark_byte_stack ()
270 struct byte_stack *stack;
271 Lisp_Object *obj;
273 for (stack = byte_stack_list; stack; stack = stack->next)
275 if (!stack->top)
276 abort ();
278 for (obj = stack->bottom; obj <= stack->top; ++obj)
279 if (!XMARKBIT (*obj))
281 mark_object (obj);
282 XMARK (*obj);
285 if (!XMARKBIT (stack->byte_string))
287 mark_object (&stack->byte_string);
288 XMARK (stack->byte_string);
291 if (!XMARKBIT (stack->constants))
293 mark_object (&stack->constants);
294 XMARK (stack->constants);
300 /* Unmark objects in the stacks on byte_stack_list. Relocate program
301 counters. Called when GC has completed. */
303 void
304 unmark_byte_stack ()
306 struct byte_stack *stack;
307 Lisp_Object *obj;
309 for (stack = byte_stack_list; stack; stack = stack->next)
311 for (obj = stack->bottom; obj <= stack->top; ++obj)
312 XUNMARK (*obj);
314 XUNMARK (stack->byte_string);
315 XUNMARK (stack->constants);
317 if (stack->byte_string_start != XSTRING (stack->byte_string)->data)
319 int offset = stack->pc - stack->byte_string_start;
320 stack->byte_string_start = XSTRING (stack->byte_string)->data;
321 stack->pc = stack->byte_string_start + offset;
327 /* Fetch the next byte from the bytecode stream */
329 #define FETCH *stack.pc++
331 /* Fetch two bytes from the bytecode stream and make a 16-bit number
332 out of them */
334 #define FETCH2 (op = FETCH, op + (FETCH << 8))
336 /* Push x onto the execution stack. This used to be #define PUSH(x)
337 (*++stackp = (x)) This oddity is necessary because Alliant can't be
338 bothered to compile the preincrement operator properly, as of 4/91.
339 -JimB */
341 #define PUSH(x) (top++, *top = (x))
343 /* Pop a value off the execution stack. */
345 #define POP (*top--)
347 /* Discard n values from the execution stack. */
349 #define DISCARD(n) (top -= (n))
351 /* Get the value which is at the top of the execution stack, but don't
352 pop it. */
354 #define TOP (*top)
356 /* Actions that must be performed before and after calling a function
357 that might GC. */
359 #define BEFORE_POTENTIAL_GC() stack.top = top
360 #define AFTER_POTENTIAL_GC() stack.top = NULL
362 /* Garbage collect if we have consed enough since the last time.
363 We do this at every branch, to avoid loops that never GC. */
365 #define MAYBE_GC() \
366 if (consing_since_gc > gc_cons_threshold) \
368 BEFORE_POTENTIAL_GC (); \
369 Fgarbage_collect (); \
370 AFTER_POTENTIAL_GC (); \
372 else
374 /* Check for jumping out of range. */
376 #ifdef BYTE_CODE_SAFE
378 #define CHECK_RANGE(ARG) \
379 if (ARG >= bytestr_length) abort ()
381 #else /* not BYTE_CODE_SAFE */
383 #define CHECK_RANGE(ARG)
385 #endif /* not BYTE_CODE_SAFE */
388 DEFUN ("byte-code", Fbyte_code, Sbyte_code, 3, 3, 0,
389 "Function used internally in byte-compiled code.\n\
390 The first argument, BYTESTR, is a string of byte code;\n\
391 the second, VECTOR, a vector of constants;\n\
392 the third, MAXDEPTH, the maximum stack depth used in this function.\n\
393 If the third argument is incorrect, Emacs may crash.")
394 (bytestr, vector, maxdepth)
395 Lisp_Object bytestr, vector, maxdepth;
397 int count = specpdl_ptr - specpdl;
398 #ifdef BYTE_CODE_METER
399 int this_op = 0;
400 int prev_op;
401 #endif
402 int op;
403 /* Lisp_Object v1, v2; */
404 Lisp_Object *vectorp = XVECTOR (vector)->contents;
405 #ifdef BYTE_CODE_SAFE
406 int const_length = XVECTOR (vector)->size;
407 Lisp_Object *stacke;
408 #endif
409 int bytestr_length = STRING_BYTES (XSTRING (bytestr));
410 struct byte_stack stack;
411 Lisp_Object *top;
412 Lisp_Object result;
414 CHECK_STRING (bytestr, 0);
415 if (!VECTORP (vector))
416 vector = wrong_type_argument (Qvectorp, vector);
417 CHECK_NUMBER (maxdepth, 2);
419 stack.byte_string = bytestr;
420 stack.pc = stack.byte_string_start = XSTRING (bytestr)->data;
421 stack.constants = vector;
422 stack.bottom = (Lisp_Object *) alloca (XFASTINT (maxdepth)
423 * sizeof (Lisp_Object));
424 top = stack.bottom - 1;
425 stack.top = NULL;
426 stack.next = byte_stack_list;
427 byte_stack_list = &stack;
429 #ifdef BYTE_CODE_SAFE
430 stacke = stack.bottom - 1 + XFASTINT (maxdepth);
431 #endif
433 while (1)
435 #ifdef BYTE_CODE_SAFE
436 if (top > stacks)
437 error ("Byte code stack overflow (byte compiler bug), pc %d, depth %d",
438 stack.pc - stack.byte_string_start, stacke - top);
439 else if (top < stack.bottom - 1)
440 error ("Byte code stack underflow (byte compiler bug), pc %d",
441 stack.pc - stack.byte_string_start);
442 #endif
444 #ifdef BYTE_CODE_METER
445 prev_op = this_op;
446 this_op = op = FETCH;
447 METER_CODE (prev_op, op);
448 #else
449 op = FETCH;
450 #endif
452 switch (op)
454 case Bvarref + 7:
455 op = FETCH2;
456 goto varref;
458 case Bvarref:
459 case Bvarref + 1:
460 case Bvarref + 2:
461 case Bvarref + 3:
462 case Bvarref + 4:
463 case Bvarref + 5:
464 op = op - Bvarref;
465 goto varref;
467 /* This seems to be the most frequently executed byte-code
468 among the Bvarref's, so avoid a goto here. */
469 case Bvarref+6:
470 op = FETCH;
471 varref:
473 Lisp_Object v1, v2;
475 v1 = vectorp[op];
476 if (SYMBOLP (v1))
478 v2 = XSYMBOL (v1)->value;
479 if (MISCP (v2) || EQ (v2, Qunbound))
480 v2 = Fsymbol_value (v1);
482 else
483 v2 = Fsymbol_value (v1);
484 PUSH (v2);
485 break;
488 case Bgotoifnil:
489 MAYBE_GC ();
490 op = FETCH2;
491 if (NILP (POP))
493 QUIT;
494 CHECK_RANGE (op);
495 stack.pc = stack.byte_string_start + op;
497 break;
499 case Bcar:
501 Lisp_Object v1;
502 v1 = TOP;
503 if (CONSP (v1))
504 TOP = XCAR (v1);
505 else if (NILP (v1))
506 TOP = Qnil;
507 else
508 Fcar (wrong_type_argument (Qlistp, v1));
509 break;
512 case Beq:
514 Lisp_Object v1;
515 v1 = POP;
516 TOP = EQ (v1, TOP) ? Qt : Qnil;
517 break;
520 case Bmemq:
522 Lisp_Object v1;
523 v1 = POP;
524 TOP = Fmemq (TOP, v1);
525 break;
528 case Bcdr:
530 Lisp_Object v1;
531 v1 = TOP;
532 if (CONSP (v1))
533 TOP = XCDR (v1);
534 else if (NILP (v1))
535 TOP = Qnil;
536 else
537 Fcdr (wrong_type_argument (Qlistp, v1));
538 break;
541 case Bvarset+7:
542 op = FETCH2;
543 goto varset;
545 case Bvarset: case Bvarset+1: case Bvarset+2: case Bvarset+3:
546 case Bvarset+4: case Bvarset+5:
547 op -= Bvarset;
548 goto varset;
550 case Bvarset+6:
551 op = FETCH;
552 varset:
553 set_internal (vectorp[op], POP, 0);
554 /* Fset (vectorp[op], POP); */
555 break;
557 case Bdup:
559 Lisp_Object v1;
560 v1 = TOP;
561 PUSH (v1);
562 break;
565 /* ------------------ */
567 case Bvarbind+6:
568 op = FETCH;
569 goto varbind;
571 case Bvarbind+7:
572 op = FETCH2;
573 goto varbind;
575 case Bvarbind:
576 case Bvarbind+1:
577 case Bvarbind+2:
578 case Bvarbind+3:
579 case Bvarbind+4:
580 case Bvarbind+5:
581 op -= Bvarbind;
582 varbind:
583 specbind (vectorp[op], POP);
584 break;
586 case Bcall+6:
587 op = FETCH;
588 goto docall;
590 case Bcall+7:
591 op = FETCH2;
592 goto docall;
594 case Bcall:
595 case Bcall+1:
596 case Bcall+2:
597 case Bcall+3:
598 case Bcall+4:
599 case Bcall+5:
600 op -= Bcall;
601 docall:
603 BEFORE_POTENTIAL_GC ();
604 DISCARD (op);
605 #ifdef BYTE_CODE_METER
606 if (byte_metering_on && SYMBOLP (TOP))
608 Lisp_Object v1, v2;
610 v1 = TOP;
611 v2 = Fget (v1, Qbyte_code_meter);
612 if (INTEGERP (v2)
613 && XINT (v2) != ((1<<VALBITS)-1))
615 XSETINT (v2, XINT (v2) + 1);
616 Fput (v1, Qbyte_code_meter, v2);
619 #endif
620 TOP = Ffuncall (op + 1, &TOP);
621 AFTER_POTENTIAL_GC ();
622 break;
625 case Bunbind+6:
626 op = FETCH;
627 goto dounbind;
629 case Bunbind+7:
630 op = FETCH2;
631 goto dounbind;
633 case Bunbind:
634 case Bunbind+1:
635 case Bunbind+2:
636 case Bunbind+3:
637 case Bunbind+4:
638 case Bunbind+5:
639 op -= Bunbind;
640 dounbind:
641 BEFORE_POTENTIAL_GC ();
642 unbind_to (specpdl_ptr - specpdl - op, Qnil);
643 AFTER_POTENTIAL_GC ();
644 break;
646 case Bunbind_all:
647 /* To unbind back to the beginning of this frame. Not used yet,
648 but will be needed for tail-recursion elimination. */
649 BEFORE_POTENTIAL_GC ();
650 unbind_to (count, Qnil);
651 AFTER_POTENTIAL_GC ();
652 break;
654 case Bgoto:
655 MAYBE_GC ();
656 QUIT;
657 op = FETCH2; /* pc = FETCH2 loses since FETCH2 contains pc++ */
658 CHECK_RANGE (op);
659 stack.pc = stack.byte_string_start + op;
660 break;
662 case Bgotoifnonnil:
663 MAYBE_GC ();
664 op = FETCH2;
665 if (!NILP (POP))
667 QUIT;
668 CHECK_RANGE (op);
669 stack.pc = stack.byte_string_start + op;
671 break;
673 case Bgotoifnilelsepop:
674 MAYBE_GC ();
675 op = FETCH2;
676 if (NILP (TOP))
678 QUIT;
679 CHECK_RANGE (op);
680 stack.pc = stack.byte_string_start + op;
682 else DISCARD (1);
683 break;
685 case Bgotoifnonnilelsepop:
686 MAYBE_GC ();
687 op = FETCH2;
688 if (!NILP (TOP))
690 QUIT;
691 CHECK_RANGE (op);
692 stack.pc = stack.byte_string_start + op;
694 else DISCARD (1);
695 break;
697 case BRgoto:
698 MAYBE_GC ();
699 QUIT;
700 stack.pc += (int) *stack.pc - 127;
701 break;
703 case BRgotoifnil:
704 MAYBE_GC ();
705 if (NILP (POP))
707 QUIT;
708 stack.pc += (int) *stack.pc - 128;
710 stack.pc++;
711 break;
713 case BRgotoifnonnil:
714 MAYBE_GC ();
715 if (!NILP (POP))
717 QUIT;
718 stack.pc += (int) *stack.pc - 128;
720 stack.pc++;
721 break;
723 case BRgotoifnilelsepop:
724 MAYBE_GC ();
725 op = *stack.pc++;
726 if (NILP (TOP))
728 QUIT;
729 stack.pc += op - 128;
731 else DISCARD (1);
732 break;
734 case BRgotoifnonnilelsepop:
735 MAYBE_GC ();
736 op = *stack.pc++;
737 if (!NILP (TOP))
739 QUIT;
740 stack.pc += op - 128;
742 else DISCARD (1);
743 break;
745 case Breturn:
746 result = POP;
747 goto exit;
749 case Bdiscard:
750 DISCARD (1);
751 break;
753 case Bconstant2:
754 PUSH (vectorp[FETCH2]);
755 break;
757 case Bsave_excursion:
758 record_unwind_protect (save_excursion_restore,
759 save_excursion_save ());
760 break;
762 case Bsave_current_buffer:
763 case Bsave_current_buffer_1:
764 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
765 break;
767 case Bsave_window_excursion:
768 BEFORE_POTENTIAL_GC ();
769 TOP = Fsave_window_excursion (TOP);
770 AFTER_POTENTIAL_GC ();
771 break;
773 case Bsave_restriction:
774 record_unwind_protect (save_restriction_restore,
775 save_restriction_save ());
776 break;
778 case Bcatch:
780 Lisp_Object v1;
781 v1 = POP;
782 BEFORE_POTENTIAL_GC ();
783 TOP = internal_catch (TOP, Feval, v1);
784 AFTER_POTENTIAL_GC ();
785 break;
788 case Bunwind_protect:
789 record_unwind_protect (0, POP);
790 (specpdl_ptr - 1)->symbol = Qnil;
791 break;
793 case Bcondition_case:
795 Lisp_Object v1;
796 v1 = POP;
797 v1 = Fcons (POP, v1);
798 BEFORE_POTENTIAL_GC ();
799 TOP = Fcondition_case (Fcons (TOP, v1));
800 AFTER_POTENTIAL_GC ();
801 break;
804 case Btemp_output_buffer_setup:
805 BEFORE_POTENTIAL_GC ();
806 temp_output_buffer_setup (XSTRING (TOP)->data);
807 AFTER_POTENTIAL_GC ();
808 TOP = Vstandard_output;
809 break;
811 case Btemp_output_buffer_show:
813 Lisp_Object v1;
814 v1 = POP;
815 BEFORE_POTENTIAL_GC ();
816 temp_output_buffer_show (TOP);
817 TOP = v1;
818 /* pop binding of standard-output */
819 unbind_to (specpdl_ptr - specpdl - 1, Qnil);
820 AFTER_POTENTIAL_GC ();
821 break;
824 case Bnth:
826 Lisp_Object v1, v2;
827 v1 = POP;
828 v2 = TOP;
829 CHECK_NUMBER (v2, 0);
830 op = XINT (v2);
831 immediate_quit = 1;
832 while (--op >= 0)
834 if (CONSP (v1))
835 v1 = XCDR (v1);
836 else if (!NILP (v1))
838 immediate_quit = 0;
839 v1 = wrong_type_argument (Qlistp, v1);
840 immediate_quit = 1;
841 op++;
844 immediate_quit = 0;
845 if (CONSP (v1))
846 TOP = XCAR (v1);
847 else if (NILP (v1))
848 TOP = Qnil;
849 else
850 Fcar (wrong_type_argument (Qlistp, v1));
851 break;
854 case Bsymbolp:
855 TOP = SYMBOLP (TOP) ? Qt : Qnil;
856 break;
858 case Bconsp:
859 TOP = CONSP (TOP) ? Qt : Qnil;
860 break;
862 case Bstringp:
863 TOP = STRINGP (TOP) ? Qt : Qnil;
864 break;
866 case Blistp:
867 TOP = CONSP (TOP) || NILP (TOP) ? Qt : Qnil;
868 break;
870 case Bnot:
871 TOP = NILP (TOP) ? Qt : Qnil;
872 break;
874 case Bcons:
876 Lisp_Object v1;
877 v1 = POP;
878 TOP = Fcons (TOP, v1);
879 break;
882 case Blist1:
883 TOP = Fcons (TOP, Qnil);
884 break;
886 case Blist2:
888 Lisp_Object v1;
889 v1 = POP;
890 TOP = Fcons (TOP, Fcons (v1, Qnil));
891 break;
894 case Blist3:
895 DISCARD (2);
896 TOP = Flist (3, &TOP);
897 break;
899 case Blist4:
900 DISCARD (3);
901 TOP = Flist (4, &TOP);
902 break;
904 case BlistN:
905 op = FETCH;
906 DISCARD (op - 1);
907 TOP = Flist (op, &TOP);
908 break;
910 case Blength:
911 TOP = Flength (TOP);
912 break;
914 case Baref:
916 Lisp_Object v1;
917 v1 = POP;
918 TOP = Faref (TOP, v1);
919 break;
922 case Baset:
924 Lisp_Object v1, v2;
925 v2 = POP; v1 = POP;
926 TOP = Faset (TOP, v1, v2);
927 break;
930 case Bsymbol_value:
931 TOP = Fsymbol_value (TOP);
932 break;
934 case Bsymbol_function:
935 TOP = Fsymbol_function (TOP);
936 break;
938 case Bset:
940 Lisp_Object v1;
941 v1 = POP;
942 TOP = Fset (TOP, v1);
943 break;
946 case Bfset:
948 Lisp_Object v1;
949 v1 = POP;
950 TOP = Ffset (TOP, v1);
951 break;
954 case Bget:
956 Lisp_Object v1;
957 v1 = POP;
958 TOP = Fget (TOP, v1);
959 break;
962 case Bsubstring:
964 Lisp_Object v1, v2;
965 v2 = POP; v1 = POP;
966 BEFORE_POTENTIAL_GC ();
967 TOP = Fsubstring (TOP, v1, v2);
968 AFTER_POTENTIAL_GC ();
969 break;
972 case Bconcat2:
973 DISCARD (1);
974 TOP = Fconcat (2, &TOP);
975 break;
977 case Bconcat3:
978 DISCARD (2);
979 TOP = Fconcat (3, &TOP);
980 break;
982 case Bconcat4:
983 DISCARD (3);
984 TOP = Fconcat (4, &TOP);
985 break;
987 case BconcatN:
988 op = FETCH;
989 DISCARD (op - 1);
990 TOP = Fconcat (op, &TOP);
991 break;
993 case Bsub1:
995 Lisp_Object v1;
996 v1 = TOP;
997 if (INTEGERP (v1))
999 XSETINT (v1, XINT (v1) - 1);
1000 TOP = v1;
1002 else
1003 TOP = Fsub1 (v1);
1004 break;
1007 case Badd1:
1009 Lisp_Object v1;
1010 v1 = TOP;
1011 if (INTEGERP (v1))
1013 XSETINT (v1, XINT (v1) + 1);
1014 TOP = v1;
1016 else
1017 TOP = Fadd1 (v1);
1018 break;
1021 case Beqlsign:
1023 Lisp_Object v1, v2;
1024 v2 = POP; v1 = TOP;
1025 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v1, 0);
1026 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v2, 0);
1027 #ifdef LISP_FLOAT_TYPE
1028 if (FLOATP (v1) || FLOATP (v2))
1030 double f1, f2;
1032 f1 = (FLOATP (v1) ? XFLOAT_DATA (v1) : XINT (v1));
1033 f2 = (FLOATP (v2) ? XFLOAT_DATA (v2) : XINT (v2));
1034 TOP = (f1 == f2 ? Qt : Qnil);
1036 else
1037 #endif
1038 TOP = (XINT (v1) == XINT (v2) ? Qt : Qnil);
1039 break;
1042 case Bgtr:
1044 Lisp_Object v1;
1045 v1 = POP;
1046 TOP = Fgtr (TOP, v1);
1047 break;
1050 case Blss:
1052 Lisp_Object v1;
1053 v1 = POP;
1054 TOP = Flss (TOP, v1);
1055 break;
1058 case Bleq:
1060 Lisp_Object v1;
1061 v1 = POP;
1062 TOP = Fleq (TOP, v1);
1063 break;
1066 case Bgeq:
1068 Lisp_Object v1;
1069 v1 = POP;
1070 TOP = Fgeq (TOP, v1);
1071 break;
1074 case Bdiff:
1075 DISCARD (1);
1076 TOP = Fminus (2, &TOP);
1077 break;
1079 case Bnegate:
1081 Lisp_Object v1;
1082 v1 = TOP;
1083 if (INTEGERP (v1))
1085 XSETINT (v1, - XINT (v1));
1086 TOP = v1;
1088 else
1089 TOP = Fminus (1, &TOP);
1090 break;
1093 case Bplus:
1094 DISCARD (1);
1095 TOP = Fplus (2, &TOP);
1096 break;
1098 case Bmax:
1099 DISCARD (1);
1100 TOP = Fmax (2, &TOP);
1101 break;
1103 case Bmin:
1104 DISCARD (1);
1105 TOP = Fmin (2, &TOP);
1106 break;
1108 case Bmult:
1109 DISCARD (1);
1110 TOP = Ftimes (2, &TOP);
1111 break;
1113 case Bquo:
1114 DISCARD (1);
1115 TOP = Fquo (2, &TOP);
1116 break;
1118 case Brem:
1120 Lisp_Object v1;
1121 v1 = POP;
1122 TOP = Frem (TOP, v1);
1123 break;
1126 case Bpoint:
1128 Lisp_Object v1;
1129 XSETFASTINT (v1, PT);
1130 PUSH (v1);
1131 break;
1134 case Bgoto_char:
1135 BEFORE_POTENTIAL_GC ();
1136 TOP = Fgoto_char (TOP);
1137 AFTER_POTENTIAL_GC ();
1138 break;
1140 case Binsert:
1141 BEFORE_POTENTIAL_GC ();
1142 TOP = Finsert (1, &TOP);
1143 AFTER_POTENTIAL_GC ();
1144 break;
1146 case BinsertN:
1147 op = FETCH;
1148 BEFORE_POTENTIAL_GC ();
1149 DISCARD (op - 1);
1150 TOP = Finsert (op, &TOP);
1151 AFTER_POTENTIAL_GC ();
1152 break;
1154 case Bpoint_max:
1156 Lisp_Object v1;
1157 XSETFASTINT (v1, ZV);
1158 PUSH (v1);
1159 break;
1162 case Bpoint_min:
1164 Lisp_Object v1;
1165 XSETFASTINT (v1, BEGV);
1166 PUSH (v1);
1167 break;
1170 case Bchar_after:
1171 TOP = Fchar_after (TOP);
1172 break;
1174 case Bfollowing_char:
1176 Lisp_Object v1;
1177 v1 = Ffollowing_char ();
1178 PUSH (v1);
1179 break;
1182 case Bpreceding_char:
1184 Lisp_Object v1;
1185 v1 = Fprevious_char ();
1186 PUSH (v1);
1187 break;
1190 case Bcurrent_column:
1192 Lisp_Object v1;
1193 XSETFASTINT (v1, current_column ());
1194 PUSH (v1);
1195 break;
1198 case Bindent_to:
1199 BEFORE_POTENTIAL_GC ();
1200 TOP = Findent_to (TOP, Qnil);
1201 AFTER_POTENTIAL_GC ();
1202 break;
1204 case Beolp:
1205 PUSH (Feolp ());
1206 break;
1208 case Beobp:
1209 PUSH (Feobp ());
1210 break;
1212 case Bbolp:
1213 PUSH (Fbolp ());
1214 break;
1216 case Bbobp:
1217 PUSH (Fbobp ());
1218 break;
1220 case Bcurrent_buffer:
1221 PUSH (Fcurrent_buffer ());
1222 break;
1224 case Bset_buffer:
1225 BEFORE_POTENTIAL_GC ();
1226 TOP = Fset_buffer (TOP);
1227 AFTER_POTENTIAL_GC ();
1228 break;
1230 case Binteractive_p:
1231 PUSH (Finteractive_p ());
1232 break;
1234 case Bforward_char:
1235 BEFORE_POTENTIAL_GC ();
1236 TOP = Fforward_char (TOP);
1237 AFTER_POTENTIAL_GC ();
1238 break;
1240 case Bforward_word:
1241 BEFORE_POTENTIAL_GC ();
1242 TOP = Fforward_word (TOP);
1243 AFTER_POTENTIAL_GC ();
1244 break;
1246 case Bskip_chars_forward:
1248 Lisp_Object v1;
1249 v1 = POP;
1250 BEFORE_POTENTIAL_GC ();
1251 TOP = Fskip_chars_forward (TOP, v1);
1252 AFTER_POTENTIAL_GC ();
1253 break;
1256 case Bskip_chars_backward:
1258 Lisp_Object v1;
1259 v1 = POP;
1260 BEFORE_POTENTIAL_GC ();
1261 TOP = Fskip_chars_backward (TOP, v1);
1262 AFTER_POTENTIAL_GC ();
1263 break;
1266 case Bforward_line:
1267 BEFORE_POTENTIAL_GC ();
1268 TOP = Fforward_line (TOP);
1269 AFTER_POTENTIAL_GC ();
1270 break;
1272 case Bchar_syntax:
1273 CHECK_NUMBER (TOP, 0);
1274 XSETFASTINT (TOP, syntax_code_spec[(int) SYNTAX (XINT (TOP))]);
1275 break;
1277 case Bbuffer_substring:
1279 Lisp_Object v1;
1280 v1 = POP;
1281 BEFORE_POTENTIAL_GC ();
1282 TOP = Fbuffer_substring (TOP, v1);
1283 AFTER_POTENTIAL_GC ();
1284 break;
1287 case Bdelete_region:
1289 Lisp_Object v1;
1290 v1 = POP;
1291 BEFORE_POTENTIAL_GC ();
1292 TOP = Fdelete_region (TOP, v1);
1293 AFTER_POTENTIAL_GC ();
1294 break;
1297 case Bnarrow_to_region:
1299 Lisp_Object v1;
1300 v1 = POP;
1301 BEFORE_POTENTIAL_GC ();
1302 TOP = Fnarrow_to_region (TOP, v1);
1303 AFTER_POTENTIAL_GC ();
1304 break;
1307 case Bwiden:
1308 BEFORE_POTENTIAL_GC ();
1309 PUSH (Fwiden ());
1310 AFTER_POTENTIAL_GC ();
1311 break;
1313 case Bend_of_line:
1314 BEFORE_POTENTIAL_GC ();
1315 TOP = Fend_of_line (TOP);
1316 AFTER_POTENTIAL_GC ();
1317 break;
1319 case Bset_marker:
1321 Lisp_Object v1, v2;
1322 v1 = POP;
1323 v2 = POP;
1324 TOP = Fset_marker (TOP, v2, v1);
1325 break;
1328 case Bmatch_beginning:
1329 TOP = Fmatch_beginning (TOP);
1330 break;
1332 case Bmatch_end:
1333 TOP = Fmatch_end (TOP);
1334 break;
1336 case Bupcase:
1337 TOP = Fupcase (TOP);
1338 break;
1340 case Bdowncase:
1341 TOP = Fdowncase (TOP);
1342 break;
1344 case Bstringeqlsign:
1346 Lisp_Object v1;
1347 v1 = POP;
1348 TOP = Fstring_equal (TOP, v1);
1349 break;
1352 case Bstringlss:
1354 Lisp_Object v1;
1355 v1 = POP;
1356 TOP = Fstring_lessp (TOP, v1);
1357 break;
1360 case Bequal:
1362 Lisp_Object v1;
1363 v1 = POP;
1364 TOP = Fequal (TOP, v1);
1365 break;
1368 case Bnthcdr:
1370 Lisp_Object v1;
1371 v1 = POP;
1372 TOP = Fnthcdr (TOP, v1);
1373 break;
1376 case Belt:
1378 Lisp_Object v1, v2;
1379 if (CONSP (TOP))
1381 /* Exchange args and then do nth. */
1382 v2 = POP;
1383 v1 = TOP;
1384 CHECK_NUMBER (v2, 0);
1385 op = XINT (v2);
1386 immediate_quit = 1;
1387 while (--op >= 0)
1389 if (CONSP (v1))
1390 v1 = XCDR (v1);
1391 else if (!NILP (v1))
1393 immediate_quit = 0;
1394 v1 = wrong_type_argument (Qlistp, v1);
1395 immediate_quit = 1;
1396 op++;
1399 immediate_quit = 0;
1400 if (CONSP (v1))
1401 TOP = XCAR (v1);
1402 else if (NILP (v1))
1403 TOP = Qnil;
1404 else
1405 Fcar (wrong_type_argument (Qlistp, v1));
1407 else
1409 v1 = POP;
1410 TOP = Felt (TOP, v1);
1412 break;
1415 case Bmember:
1417 Lisp_Object v1;
1418 v1 = POP;
1419 TOP = Fmember (TOP, v1);
1420 break;
1423 case Bassq:
1425 Lisp_Object v1;
1426 v1 = POP;
1427 TOP = Fassq (TOP, v1);
1428 break;
1431 case Bnreverse:
1432 TOP = Fnreverse (TOP);
1433 break;
1435 case Bsetcar:
1437 Lisp_Object v1;
1438 v1 = POP;
1439 TOP = Fsetcar (TOP, v1);
1440 break;
1443 case Bsetcdr:
1445 Lisp_Object v1;
1446 v1 = POP;
1447 TOP = Fsetcdr (TOP, v1);
1448 break;
1451 case Bcar_safe:
1453 Lisp_Object v1;
1454 v1 = TOP;
1455 if (CONSP (v1))
1456 TOP = XCAR (v1);
1457 else
1458 TOP = Qnil;
1459 break;
1462 case Bcdr_safe:
1464 Lisp_Object v1;
1465 v1 = TOP;
1466 if (CONSP (v1))
1467 TOP = XCDR (v1);
1468 else
1469 TOP = Qnil;
1470 break;
1473 case Bnconc:
1474 DISCARD (1);
1475 TOP = Fnconc (2, &TOP);
1476 break;
1478 case Bnumberp:
1479 TOP = (NUMBERP (TOP) ? Qt : Qnil);
1480 break;
1482 case Bintegerp:
1483 TOP = INTEGERP (TOP) ? Qt : Qnil;
1484 break;
1486 #ifdef BYTE_CODE_SAFE
1487 case Bset_mark:
1488 error ("set-mark is an obsolete bytecode");
1489 break;
1490 case Bscan_buffer:
1491 error ("scan-buffer is an obsolete bytecode");
1492 break;
1493 #endif
1495 default:
1496 #ifdef BYTE_CODE_SAFE
1497 if (op < Bconstant)
1498 error ("unknown bytecode %d (byte compiler bug)", op);
1499 if ((op -= Bconstant) >= const_length)
1500 error ("no constant number %d (byte compiler bug)", op);
1501 PUSH (vectorp[op]);
1502 #else
1503 PUSH (vectorp[op - Bconstant]);
1504 #endif
1508 exit:
1510 byte_stack_list = byte_stack_list->next;
1512 /* Binds and unbinds are supposed to be compiled balanced. */
1513 if (specpdl_ptr - specpdl != count)
1514 #ifdef BYTE_CODE_SAFE
1515 error ("binding stack not balanced (serious byte compiler bug)");
1516 #else
1517 abort ();
1518 #endif
1520 return result;
1523 void
1524 syms_of_bytecode ()
1526 Qbytecode = intern ("byte-code");
1527 staticpro (&Qbytecode);
1529 defsubr (&Sbyte_code);
1531 #ifdef BYTE_CODE_METER
1533 DEFVAR_LISP ("byte-code-meter", &Vbyte_code_meter,
1534 "A vector of vectors which holds a histogram of byte-code usage.\n\
1535 (aref (aref byte-code-meter 0) CODE) indicates how many times the byte\n\
1536 opcode CODE has been executed.\n\
1537 (aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,\n\
1538 indicates how many times the byte opcodes CODE1 and CODE2 have been\n\
1539 executed in succession.");
1540 DEFVAR_BOOL ("byte-metering-on", &byte_metering_on,
1541 "If non-nil, keep profiling information on byte code usage.\n\
1542 The variable byte-code-meter indicates how often each byte opcode is used.\n\
1543 If a symbol has a property named `byte-code-meter' whose value is an\n\
1544 integer, it is incremented each time that symbol's function is called.");
1546 byte_metering_on = 0;
1547 Vbyte_code_meter = Fmake_vector (make_number (256), make_number (0));
1548 Qbyte_code_meter = intern ("byte-code-meter");
1549 staticpro (&Qbyte_code_meter);
1551 int i = 256;
1552 while (i--)
1553 XVECTOR (Vbyte_code_meter)->contents[i] =
1554 Fmake_vector (make_number (256), make_number (0));
1556 #endif