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)
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
28 o made the new bytecodes be called with args in the right order;
29 o added metering support.
32 o added relative jump instructions;
33 o all conditionals now only do QUIT if they jump.
42 * define BYTE_CODE_SAFE to enable some minor sanity checking (useful for
43 * debugging the byte compiler...)
45 * define BYTE_CODE_METER to enable generation of a byte-op usage histogram.
47 /* #define BYTE_CODE_SAFE */
48 /* #define BYTE_CODE_METER */
51 #ifdef BYTE_CODE_METER
53 Lisp_Object Vbyte_code_meter
, Qbyte_code_meter
;
56 #define METER_2(code1, code2) \
57 XFASTINT (XVECTOR (XVECTOR (Vbyte_code_meter)->contents[(code1)]) \
60 #define METER_1(code) METER_2 (0, (code))
62 #define METER_CODE(last_code, this_code) \
64 if (byte_metering_on) \
66 if (METER_1 (this_code) != ((1<<VALBITS)-1)) \
67 METER_1 (this_code)++; \
69 && METER_2 (last_code, this_code) != ((1<<VALBITS)-1))\
70 METER_2 (last_code, this_code)++; \
74 #else /* no BYTE_CODE_METER */
76 #define METER_CODE(last_code, this_code)
78 #endif /* no BYTE_CODE_METER */
81 Lisp_Object Qbytecode
;
109 #define Bsymbol_value 0112
110 #define Bsymbol_function 0113
114 #define Bsubstring 0117
115 #define Bconcat2 0120
116 #define Bconcat3 0121
117 #define Bconcat4 0122
120 #define Beqlsign 0125
133 /* Was Bmark in v17. */
134 #define Bsave_current_buffer 0141
135 #define Bgoto_char 0142
137 #define Bpoint_max 0144
138 #define Bpoint_min 0145
139 #define Bchar_after 0146
140 #define Bfollowing_char 0147
141 #define Bpreceding_char 0150
142 #define Bcurrent_column 0151
143 #define Bindent_to 0152
144 #define Bscan_buffer 0153 /* No longer generated as of v18 */
149 #define Bcurrent_buffer 0160
150 #define Bset_buffer 0161
151 #define Bsave_current_buffer_1 0162 /* Replacing Bsave_current_buffer. */
152 #define Bread_char 0162 /* No longer generated as of v19 */
153 #define Bset_mark 0163 /* this loser is no longer generated as of v18 */
154 #define Binteractive_p 0164 /* Needed since interactive-p takes unevalled args */
156 #define Bforward_char 0165
157 #define Bforward_word 0166
158 #define Bskip_chars_forward 0167
159 #define Bskip_chars_backward 0170
160 #define Bforward_line 0171
161 #define Bchar_syntax 0172
162 #define Bbuffer_substring 0173
163 #define Bdelete_region 0174
164 #define Bnarrow_to_region 0175
166 #define Bend_of_line 0177
168 #define Bconstant2 0201
170 #define Bgotoifnil 0203
171 #define Bgotoifnonnil 0204
172 #define Bgotoifnilelsepop 0205
173 #define Bgotoifnonnilelsepop 0206
175 #define Bdiscard 0210
178 #define Bsave_excursion 0212
179 #define Bsave_window_excursion 0213
180 #define Bsave_restriction 0214
183 #define Bunwind_protect 0216
184 #define Bcondition_case 0217
185 #define Btemp_output_buffer_setup 0220
186 #define Btemp_output_buffer_show 0221
188 #define Bunbind_all 0222
190 #define Bset_marker 0223
191 #define Bmatch_beginning 0224
192 #define Bmatch_end 0225
194 #define Bdowncase 0227
196 #define Bstringeqlsign 0230
197 #define Bstringlss 0231
203 #define Bnreverse 0237
206 #define Bcar_safe 0242
207 #define Bcdr_safe 0243
211 #define Bnumberp 0247
212 #define Bintegerp 0250
215 #define BRgotoifnil 0253
216 #define BRgotoifnonnil 0254
217 #define BRgotoifnilelsepop 0255
218 #define BRgotoifnonnilelsepop 0256
221 #define BconcatN 0260
222 #define BinsertN 0261
224 #define Bconstant 0300
225 #define CONSTANTLIM 0100
227 /* Fetch the next byte from the bytecode stream */
231 /* Fetch two bytes from the bytecode stream
232 and make a 16-bit number out of them */
234 #define FETCH2 (op = FETCH, op + (FETCH << 8))
236 /* Push x onto the execution stack. */
238 /* This used to be #define PUSH(x) (*++stackp = (x))
239 This oddity is necessary because Alliant can't be bothered to
240 compile the preincrement operator properly, as of 4/91. -JimB */
241 #define PUSH(x) (stackp++, *stackp = (x))
243 /* Pop a value off the execution stack. */
245 #define POP (*stackp--)
247 /* Discard n values from the execution stack. */
249 #define DISCARD(n) (stackp -= (n))
251 /* Get the value which is at the top of the execution stack, but don't pop it. */
253 #define TOP (*stackp)
255 /* Garbage collect if we have consed enough since the last time.
256 We do this at every branch, to avoid loops that never GC. */
259 if (consing_since_gc > gc_cons_threshold) \
261 Fgarbage_collect (); \
262 HANDLE_RELOCATION (); \
266 /* Relocate BYTESTR if there has been a GC recently. */
267 #define HANDLE_RELOCATION() \
268 if (! EQ (string_saved, bytestr)) \
270 pc = pc - XSTRING (string_saved)->data + XSTRING (bytestr)->data; \
271 string_saved = bytestr; \
275 /* Check for jumping out of range. */
276 #define CHECK_RANGE(ARG) \
277 if (ARG >= bytestr_length) abort ()
279 DEFUN ("byte-code", Fbyte_code
, Sbyte_code
, 3, 3, 0,
280 "Function used internally in byte-compiled code.\n\
281 The first argument, BYTESTR, is a string of byte code;\n\
282 the second, VECTOR, a vector of constants;\n\
283 the third, MAXDEPTH, the maximum stack depth used in this function.\n\
284 If the third argument is incorrect, Emacs may crash.")
285 (bytestr
, vector
, maxdepth
)
286 Lisp_Object bytestr
, vector
, maxdepth
;
288 struct gcpro gcpro1
, gcpro2
, gcpro3
;
289 int count
= specpdl_ptr
- specpdl
;
290 #ifdef BYTE_CODE_METER
297 register Lisp_Object
*stackp
;
299 register Lisp_Object v1
, v2
;
300 register Lisp_Object
*vectorp
= XVECTOR (vector
)->contents
;
301 #ifdef BYTE_CODE_SAFE
302 register int const_length
= XVECTOR (vector
)->size
;
304 /* Copy of BYTESTR, saved so we can tell if BYTESTR was relocated. */
305 Lisp_Object string_saved
;
306 /* Cached address of beginning of string,
307 valid if BYTESTR equals STRING_SAVED. */
308 register unsigned char *strbeg
;
309 int bytestr_length
= STRING_BYTES (XSTRING (bytestr
));
311 CHECK_STRING (bytestr
, 0);
312 if (!VECTORP (vector
))
313 vector
= wrong_type_argument (Qvectorp
, vector
);
314 CHECK_NUMBER (maxdepth
, 2);
316 stackp
= (Lisp_Object
*) alloca (XFASTINT (maxdepth
) * sizeof (Lisp_Object
));
317 bzero (stackp
, XFASTINT (maxdepth
) * sizeof (Lisp_Object
));
318 GCPRO3 (bytestr
, vector
, *stackp
);
319 gcpro3
.nvars
= XFASTINT (maxdepth
);
323 stacke
= stackp
+ XFASTINT (maxdepth
);
325 /* Initialize the saved pc-pointer for fetching from the string. */
326 string_saved
= bytestr
;
327 pc
= XSTRING (string_saved
)->data
;
331 #ifdef BYTE_CODE_SAFE
333 error ("Byte code stack overflow (byte compiler bug), pc %d, depth %d",
334 pc
- XSTRING (string_saved
)->data
, stacke
- stackp
);
336 error ("Byte code stack underflow (byte compiler bug), pc %d",
337 pc
- XSTRING (string_saved
)->data
);
340 /* Update BYTESTR if we had a garbage collection. */
341 HANDLE_RELOCATION ();
343 #ifdef BYTE_CODE_METER
345 this_op
= op
= FETCH
;
346 METER_CODE (prev_op
, op
);
360 case Bvarref
: case Bvarref
+1: case Bvarref
+2: case Bvarref
+3:
361 case Bvarref
+4: case Bvarref
+5:
366 v2
= Fsymbol_value (v1
);
369 v2
= XSYMBOL (v1
)->value
;
370 if (MISCP (v2
) || EQ (v2
, Qunbound
))
371 v2
= Fsymbol_value (v1
);
384 case Bvarset
: case Bvarset
+1: case Bvarset
+2: case Bvarset
+3:
385 case Bvarset
+4: case Bvarset
+5:
388 Fset (vectorp
[op
], POP
);
399 case Bvarbind
: case Bvarbind
+1: case Bvarbind
+2: case Bvarbind
+3:
400 case Bvarbind
+4: case Bvarbind
+5:
403 specbind (vectorp
[op
], POP
);
414 case Bcall
: case Bcall
+1: case Bcall
+2: case Bcall
+3:
415 case Bcall
+4: case Bcall
+5:
419 #ifdef BYTE_CODE_METER
420 if (byte_metering_on
&& SYMBOLP (TOP
))
423 v2
= Fget (v1
, Qbyte_code_meter
);
425 && XINT (v2
) != ((1<<VALBITS
)-1))
427 XSETINT (v2
, XINT (v2
) + 1);
428 Fput (v1
, Qbyte_code_meter
, v2
);
432 TOP
= Ffuncall (op
+ 1, &TOP
);
443 case Bunbind
: case Bunbind
+1: case Bunbind
+2: case Bunbind
+3:
444 case Bunbind
+4: case Bunbind
+5:
447 unbind_to (specpdl_ptr
- specpdl
- op
, Qnil
);
451 /* To unbind back to the beginning of this frame. Not used yet,
452 but will be needed for tail-recursion elimination. */
453 unbind_to (count
, Qnil
);
459 op
= FETCH2
; /* pc = FETCH2 loses since FETCH2 contains pc++ */
461 pc
= XSTRING (string_saved
)->data
+ op
;
471 pc
= XSTRING (string_saved
)->data
+ op
;
482 pc
= XSTRING (string_saved
)->data
+ op
;
486 case Bgotoifnilelsepop
:
493 pc
= XSTRING (string_saved
)->data
+ op
;
498 case Bgotoifnonnilelsepop
:
505 pc
= XSTRING (string_saved
)->data
+ op
;
513 pc
+= (int) *pc
- 127;
521 pc
+= (int) *pc
- 128;
531 pc
+= (int) *pc
- 128;
536 case BRgotoifnilelsepop
:
547 case BRgotoifnonnilelsepop
:
572 PUSH (vectorp
[FETCH2
]);
575 case Bsave_excursion
:
576 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
579 case Bsave_current_buffer
:
580 case Bsave_current_buffer_1
:
581 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
584 case Bsave_window_excursion
:
585 TOP
= Fsave_window_excursion (TOP
);
588 case Bsave_restriction
:
589 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
594 TOP
= internal_catch (TOP
, Feval
, v1
);
597 case Bunwind_protect
:
598 record_unwind_protect (0, POP
);
599 (specpdl_ptr
- 1)->symbol
= Qnil
;
602 case Bcondition_case
:
604 v1
= Fcons (POP
, v1
);
605 TOP
= Fcondition_case (Fcons (TOP
, v1
));
608 case Btemp_output_buffer_setup
:
609 temp_output_buffer_setup (XSTRING (TOP
)->data
);
610 TOP
= Vstandard_output
;
613 case Btemp_output_buffer_show
:
615 temp_output_buffer_show (TOP
);
617 /* pop binding of standard-output */
618 unbind_to (specpdl_ptr
- specpdl
- 1, Qnil
);
625 CHECK_NUMBER (v2
, 0);
631 v1
= XCONS (v1
)->cdr
;
635 v1
= wrong_type_argument (Qlistp
, v1
);
644 TOP
= SYMBOLP (TOP
) ? Qt
: Qnil
;
648 TOP
= CONSP (TOP
) ? Qt
: Qnil
;
652 TOP
= STRINGP (TOP
) ? Qt
: Qnil
;
656 TOP
= CONSP (TOP
) || NILP (TOP
) ? Qt
: Qnil
;
661 TOP
= EQ (v1
, TOP
) ? Qt
: Qnil
;
666 TOP
= Fmemq (TOP
, v1
);
670 TOP
= NILP (TOP
) ? Qt
: Qnil
;
676 if (CONSP (v1
)) TOP
= XCONS (v1
)->car
;
677 else if (NILP (v1
)) TOP
= Qnil
;
678 else Fcar (wrong_type_argument (Qlistp
, v1
));
683 if (CONSP (v1
)) TOP
= XCONS (v1
)->cdr
;
684 else if (NILP (v1
)) TOP
= Qnil
;
685 else Fcdr (wrong_type_argument (Qlistp
, v1
));
690 TOP
= Fcons (TOP
, v1
);
694 TOP
= Fcons (TOP
, Qnil
);
699 TOP
= Fcons (TOP
, Fcons (v1
, Qnil
));
704 TOP
= Flist (3, &TOP
);
709 TOP
= Flist (4, &TOP
);
715 TOP
= Flist (op
, &TOP
);
724 TOP
= Faref (TOP
, v1
);
729 TOP
= Faset (TOP
, v1
, v2
);
733 TOP
= Fsymbol_value (TOP
);
736 case Bsymbol_function
:
737 TOP
= Fsymbol_function (TOP
);
742 TOP
= Fset (TOP
, v1
);
747 TOP
= Ffset (TOP
, v1
);
752 TOP
= Fget (TOP
, v1
);
757 TOP
= Fsubstring (TOP
, v1
, v2
);
762 TOP
= Fconcat (2, &TOP
);
767 TOP
= Fconcat (3, &TOP
);
772 TOP
= Fconcat (4, &TOP
);
778 TOP
= Fconcat (op
, &TOP
);
785 XSETINT (v1
, XINT (v1
) - 1);
796 XSETINT (v1
, XINT (v1
) + 1);
805 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v1
, 0);
806 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v2
, 0);
807 #ifdef LISP_FLOAT_TYPE
808 if (FLOATP (v1
) || FLOATP (v2
))
812 f1
= (FLOATP (v1
) ? XFLOAT (v1
)->data
: XINT (v1
));
813 f2
= (FLOATP (v2
) ? XFLOAT (v2
)->data
: XINT (v2
));
814 TOP
= (f1
== f2
? Qt
: Qnil
);
818 TOP
= (XINT (v1
) == XINT (v2
) ? Qt
: Qnil
);
823 TOP
= Fgtr (TOP
, v1
);
828 TOP
= Flss (TOP
, v1
);
833 TOP
= Fleq (TOP
, v1
);
838 TOP
= Fgeq (TOP
, v1
);
843 TOP
= Fminus (2, &TOP
);
850 XSETINT (v1
, - XINT (v1
));
854 TOP
= Fminus (1, &TOP
);
859 TOP
= Fplus (2, &TOP
);
864 TOP
= Fmax (2, &TOP
);
869 TOP
= Fmin (2, &TOP
);
874 TOP
= Ftimes (2, &TOP
);
879 TOP
= Fquo (2, &TOP
);
884 TOP
= Frem (TOP
, v1
);
888 XSETFASTINT (v1
, PT
);
893 TOP
= Fgoto_char (TOP
);
897 TOP
= Finsert (1, &TOP
);
903 TOP
= Finsert (op
, &TOP
);
907 XSETFASTINT (v1
, ZV
);
912 XSETFASTINT (v1
, BEGV
);
917 TOP
= Fchar_after (TOP
);
920 case Bfollowing_char
:
921 v1
= Ffollowing_char ();
925 case Bpreceding_char
:
926 v1
= Fprevious_char ();
930 case Bcurrent_column
:
931 XSETFASTINT (v1
, current_column ());
936 TOP
= Findent_to (TOP
, Qnil
);
955 case Bcurrent_buffer
:
956 PUSH (Fcurrent_buffer ());
960 TOP
= Fset_buffer (TOP
);
964 PUSH (Finteractive_p ());
968 TOP
= Fforward_char (TOP
);
972 TOP
= Fforward_word (TOP
);
975 case Bskip_chars_forward
:
977 TOP
= Fskip_chars_forward (TOP
, v1
);
980 case Bskip_chars_backward
:
982 TOP
= Fskip_chars_backward (TOP
, v1
);
986 TOP
= Fforward_line (TOP
);
990 CHECK_NUMBER (TOP
, 0);
992 syntax_code_spec
[(int) SYNTAX (XINT (TOP
))]);
995 case Bbuffer_substring
:
997 TOP
= Fbuffer_substring (TOP
, v1
);
1000 case Bdelete_region
:
1002 TOP
= Fdelete_region (TOP
, v1
);
1005 case Bnarrow_to_region
:
1007 TOP
= Fnarrow_to_region (TOP
, v1
);
1015 TOP
= Fend_of_line (TOP
);
1021 TOP
= Fset_marker (TOP
, v2
, v1
);
1024 case Bmatch_beginning
:
1025 TOP
= Fmatch_beginning (TOP
);
1029 TOP
= Fmatch_end (TOP
);
1033 TOP
= Fupcase (TOP
);
1037 TOP
= Fdowncase (TOP
);
1040 case Bstringeqlsign
:
1042 TOP
= Fstring_equal (TOP
, v1
);
1047 TOP
= Fstring_lessp (TOP
, v1
);
1052 TOP
= Fequal (TOP
, v1
);
1057 TOP
= Fnthcdr (TOP
, v1
);
1063 /* Exchange args and then do nth. */
1069 TOP
= Felt (TOP
, v1
);
1074 TOP
= Fmember (TOP
, v1
);
1079 TOP
= Fassq (TOP
, v1
);
1083 TOP
= Fnreverse (TOP
);
1088 TOP
= Fsetcar (TOP
, v1
);
1093 TOP
= Fsetcdr (TOP
, v1
);
1099 TOP
= XCONS (v1
)->car
;
1107 TOP
= XCONS (v1
)->cdr
;
1114 TOP
= Fnconc (2, &TOP
);
1118 TOP
= (NUMBERP (TOP
) ? Qt
: Qnil
);
1122 TOP
= INTEGERP (TOP
) ? Qt
: Qnil
;
1125 #ifdef BYTE_CODE_SAFE
1127 error ("set-mark is an obsolete bytecode");
1130 error ("scan-buffer is an obsolete bytecode");
1135 #ifdef BYTE_CODE_SAFE
1137 error ("unknown bytecode %d (byte compiler bug)", op
);
1138 if ((op
-= Bconstant
) >= const_length
)
1139 error ("no constant number %d (byte compiler bug)", op
);
1142 PUSH (vectorp
[op
- Bconstant
]);
1149 /* Binds and unbinds are supposed to be compiled balanced. */
1150 if (specpdl_ptr
- specpdl
!= count
)
1151 #ifdef BYTE_CODE_SAFE
1152 error ("binding stack not balanced (serious byte compiler bug)");
1162 Qbytecode
= intern ("byte-code");
1163 staticpro (&Qbytecode
);
1165 defsubr (&Sbyte_code
);
1167 #ifdef BYTE_CODE_METER
1169 DEFVAR_LISP ("byte-code-meter", &Vbyte_code_meter
,
1170 "A vector of vectors which holds a histogram of byte-code usage.\n\
1171 (aref (aref byte-code-meter 0) CODE) indicates how many times the byte\n\
1172 opcode CODE has been executed.\n\
1173 (aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,\n\
1174 indicates how many times the byte opcodes CODE1 and CODE2 have been\n\
1175 executed in succession.");
1176 DEFVAR_BOOL ("byte-metering-on", &byte_metering_on
,
1177 "If non-nil, keep profiling information on byte code usage.\n\
1178 The variable byte-code-meter indicates how often each byte opcode is used.\n\
1179 If a symbol has a property named `byte-code-meter' whose value is an\n\
1180 integer, it is incremented each time that symbol's function is called.");
1182 byte_metering_on
= 0;
1183 Vbyte_code_meter
= Fmake_vector (make_number (256), make_number (0));
1184 Qbyte_code_meter
= intern ("byte-code-meter");
1185 staticpro (&Qbyte_code_meter
);
1189 XVECTOR (Vbyte_code_meter
)->contents
[i
] =
1190 Fmake_vector (make_number (256), make_number (0));