1 /* Execution of byte code produced by bytecomp.el.
2 Copyright (C) 1992, 1993 Free Software Foundation, Inc.
4 This file is part of XEmacs.
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; 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 /* Synched up with: Mule 2.0, FSF 19.30. */
23 /* This file has been Mule-ized. */
30 hacked on by jwz@netscape.com 17-jun-91
31 o added a compile-time switch to turn on simple sanity checking;
32 o put back the obsolete byte-codes for error-detection;
33 o added a new instruction, unbind_all, which I will use for
34 tail-recursion elimination;
35 o made temp_output_buffer_show be called with the right number
37 o made the new bytecodes be called with args in the right order;
38 o added metering support.
41 o added relative jump instructions;
42 o all conditionals now only do QUIT if they jump.
44 Ben Wing: some changes for Mule, June 1995.
53 * define BYTE_CODE_SAFE to enable some minor sanity checking (useful for
54 * debugging the byte compiler...) Somewhat surprisingly, defining this
55 * makes Fbyte_code about 8% slower.
57 * define BYTE_CODE_METER to enable generation of a byte-op usage histogram.
59 /* This isn't defined in FSF Emacs and isn't defined in XEmacs v19 */
61 #define BYTE_CODE_SAFE
63 /* #define BYTE_CODE_METER */
66 #ifdef BYTE_CODE_METER
68 Lisp_Object Vbyte_code_meter, Qbyte_code_meter;
71 #define METER_2(code1, code2) \
72 XINT (XVECTOR_DATA (XVECTOR_DATA (Vbyte_code_meter)[(code1)])[(code2)])
74 #define METER_1(code) METER_2 (0, (code))
76 #define METER_CODE(last_code, this_code) \
78 if (byte_metering_on) \
80 if (METER_1 (this_code) != ((1<<VALBITS)-1)) \
81 METER_1 (this_code)++; \
83 && METER_2 (last_code, this_code) != ((1<<VALBITS)-1))\
84 METER_2 (last_code, this_code)++; \
88 #endif /* no BYTE_CODE_METER */
91 Lisp_Object Qbyte_code;
107 #define Bold_memq 076
119 #define Bsymbol_value 0112
120 #define Bsymbol_function 0113
124 #define Bsubstring 0117
125 #define Bconcat2 0120
126 #define Bconcat3 0121
127 #define Bconcat4 0122
130 #define Beqlsign 0125
143 #define Beq 0141 /* was Bmark, but no longer generated as of v18 */
144 #define Bgoto_char 0142
146 #define Bpoint_max 0144
147 #define Bpoint_min 0145
148 #define Bchar_after 0146
149 #define Bfollowing_char 0147
150 #define Bpreceding_char 0150
151 #define Bcurrent_column 0151
152 #define Bindent_to 0152
153 #define Bequal 0153 /* was Bscan_buffer, but no longer generated as of v18 */
158 #define Bcurrent_buffer 0160
159 #define Bset_buffer 0161
160 #define Bsave_current_buffer 0162 /* was Bread_char, but no longer
161 generated as of v19 */
162 #define Bmemq 0163 /* was Bset_mark, but no longer generated as of v18 */
163 #define Binteractive_p 0164 /* Needed since interactive-p takes unevalled args */
165 #define Bforward_char 0165
166 #define Bforward_word 0166
167 #define Bskip_chars_forward 0167
168 #define Bskip_chars_backward 0170
169 #define Bforward_line 0171
170 #define Bchar_syntax 0172
171 #define Bbuffer_substring 0173
172 #define Bdelete_region 0174
173 #define Bnarrow_to_region 0175
175 #define Bend_of_line 0177
177 #define Bconstant2 0201
179 #define Bgotoifnil 0203
180 #define Bgotoifnonnil 0204
181 #define Bgotoifnilelsepop 0205
182 #define Bgotoifnonnilelsepop 0206
184 #define Bdiscard 0210
187 #define Bsave_excursion 0212
188 #define Bsave_window_excursion 0213
189 #define Bsave_restriction 0214
192 #define Bunwind_protect 0216
193 #define Bcondition_case 0217
194 #define Btemp_output_buffer_setup 0220
195 #define Btemp_output_buffer_show 0221
197 #define Bunbind_all 0222
199 #define Bset_marker 0223
200 #define Bmatch_beginning 0224
201 #define Bmatch_end 0225
203 #define Bdowncase 0227
205 #define Bstringeqlsign 0230
206 #define Bstringlss 0231
207 #define Bold_equal 0232
210 #define Bold_member 0235
211 #define Bold_assq 0236
212 #define Bnreverse 0237
215 #define Bcar_safe 0242
216 #define Bcdr_safe 0243
220 #define Bnumberp 0247
221 #define Bintegerp 0250
224 #define BRgotoifnil 0253
225 #define BRgotoifnonnil 0254
226 #define BRgotoifnilelsepop 0255
227 #define BRgotoifnonnilelsepop 0256
230 #define BconcatN 0260
231 #define BinsertN 0261
232 #define Bmember 0266 /* new in v20 */
233 #define Bassq 0267 /* new in v20 */
235 #define Bconstant 0300
236 #define CONSTANTLIM 0100
238 /* Fetch the next byte from the bytecode stream */
240 #define FETCH (massaged_code[pc++])
242 /* Fetch two bytes from the bytecode stream
243 and make a 16-bit number out of them */
245 #define FETCH2 (op = FETCH, op + (FETCH << 8))
247 /* Push x onto the execution stack. */
249 #define PUSH(x) (*++stackp = (x))
251 /* Pop a value off the execution stack. */
253 #define POP (*stackp--)
255 /* Discard n values from the execution stack. */
257 #define DISCARD(n) (stackp -= (n))
259 /* Get the value which is at the top of the execution stack,
262 #define TOP (*stackp)
264 DEFUN ("byte-code", Fbyte_code, 3, 3, 0, /*
265 Function used internally in byte-compiled code.
266 The first argument is a string of byte code; the second, a vector of constants;
267 the third, the maximum stack depth used in this function.
268 If the third argument is incorrect, Emacs may crash.
270 (bytestr, vector, maxdepth))
272 /* This function can GC */
273 struct gcpro gcpro1, gcpro2, gcpro3;
274 int speccount = specpdl_depth ();
275 #ifdef BYTE_CODE_METER
282 REGISTER Lisp_Object *stackp;
284 REGISTER Lisp_Object v1, v2;
285 REGISTER Lisp_Object *vectorp = XVECTOR_DATA (vector);
286 #ifdef BYTE_CODE_SAFE
287 REGISTER int const_length = XVECTOR_LENGTH (vector);
289 REGISTER Emchar *massaged_code;
290 int massaged_code_len;
292 CHECK_STRING (bytestr);
293 if (!VECTORP (vector))
294 vector = wrong_type_argument (Qvectorp, vector);
295 CHECK_NATNUM (maxdepth);
297 stackp = alloca_array (Lisp_Object, XINT (maxdepth));
298 memset (stackp, 0, XINT (maxdepth) * sizeof (Lisp_Object));
299 GCPRO3 (bytestr, vector, *stackp);
300 gcpro3.nvars = XINT (maxdepth);
304 stacke = stackp + XINT (maxdepth);
306 /* Initialize the pc-register and convert the string into a fixed-width
307 format for easier processing. */
308 massaged_code = alloca_array (Emchar, 1 + XSTRING_CHAR_LENGTH (bytestr));
310 convert_bufbyte_string_into_emchar_string (XSTRING_DATA (bytestr),
311 XSTRING_LENGTH (bytestr),
313 massaged_code[massaged_code_len] = 0;
318 #ifdef BYTE_CODE_SAFE
320 error ("Byte code stack overflow (byte compiler bug), pc %d, depth %ld",
321 pc, (long) (stacke - stackp));
323 error ("Byte code stack underflow (byte compiler bug), pc %d",
327 #ifdef BYTE_CODE_METER
329 this_op = op = FETCH;
330 METER_CODE (prev_op, op);
344 case Bvarref: case Bvarref+1: case Bvarref+2: case Bvarref+3:
345 case Bvarref+4: case Bvarref+5:
350 v2 = Fsymbol_value (v1);
353 v2 = XSYMBOL (v1)->value;
354 if (SYMBOL_VALUE_MAGIC_P (v2))
355 v2 = Fsymbol_value (v1);
368 case Bvarset: case Bvarset+1: case Bvarset+2: case Bvarset+3:
369 case Bvarset+4: case Bvarset+5:
372 Fset (vectorp[op], POP);
383 case Bvarbind: case Bvarbind+1: case Bvarbind+2: case Bvarbind+3:
384 case Bvarbind+4: case Bvarbind+5:
387 specbind (vectorp[op], POP);
398 case Bcall: case Bcall+1: case Bcall+2: case Bcall+3:
399 case Bcall+4: case Bcall+5:
403 #ifdef BYTE_CODE_METER
404 if (byte_metering_on && SYMBOLP (TOP))
407 v2 = Fget (v1, Qbyte_code_meter, Qnil);
409 && XINT (v2) != ((1<<VALBITS)-1))
411 XSETINT (v2, XINT (v2) + 1);
412 Fput (v1, Qbyte_code_meter, v2);
415 #endif /* BYTE_CODE_METER */
416 TOP = Ffuncall (op + 1, &TOP);
427 case Bunbind: case Bunbind+1: case Bunbind+2: case Bunbind+3:
428 case Bunbind+4: case Bunbind+5:
431 unbind_to (specpdl_depth () - op, Qnil);
435 /* To unbind back to the beginning of this frame. Not used yet,
436 but will be needed for tail-recursion elimination. */
437 unbind_to (speccount, Qnil);
442 op = FETCH2; /* pc = FETCH2 loses since FETCH2 contains pc++ */
464 case Bgotoifnilelsepop:
474 case Bgotoifnonnilelsepop:
486 pc += massaged_code[pc] - 127;
493 pc += massaged_code[pc] - 128;
502 pc += massaged_code[pc] - 128;
507 case BRgotoifnilelsepop:
517 case BRgotoifnonnilelsepop:
541 PUSH (vectorp[FETCH2]);
544 case Bsave_excursion:
545 record_unwind_protect (save_excursion_restore,
546 save_excursion_save ());
549 case Bsave_window_excursion:
551 int count = specpdl_depth ();
552 record_unwind_protect (save_window_excursion_unwind,
553 Fcurrent_window_configuration (Qnil));
555 unbind_to (count, Qnil);
559 case Bsave_restriction:
560 record_unwind_protect (save_restriction_restore,
561 save_restriction_save ());
566 TOP = internal_catch (TOP, Feval, v1, 0);
569 case Bunwind_protect:
570 record_unwind_protect (Fprogn, POP);
573 case Bcondition_case:
574 v1 = POP; /* handlers */
575 v2 = POP; /* bodyform */
576 TOP = condition_case_3 (v2, TOP, v1);
579 case Btemp_output_buffer_setup:
580 temp_output_buffer_setup ((char *) XSTRING_DATA (TOP));
581 TOP = Vstandard_output;
584 case Btemp_output_buffer_show:
586 temp_output_buffer_show (TOP, Qnil);
589 /* pop binding of standard-output */
590 unbind_to (specpdl_depth() - 1, Qnil);
598 for (op = XINT (v2); op; op--)
609 v1 = wrong_type_argument (Qlistp, v1);
618 TOP = SYMBOLP (TOP) ? Qt : Qnil;
622 TOP = CONSP (TOP) ? Qt : Qnil;
626 TOP = STRINGP (TOP) ? Qt : Qnil;
630 TOP = LISTP (TOP) ? Qt : Qnil;
635 TOP = EQ_WITH_EBOLA_NOTICE (v1, TOP) ? Qt : Qnil;
640 TOP = HACKEQ_UNSAFE (v1, TOP) ? Qt : Qnil;
645 TOP = Fmemq (TOP, v1);
650 TOP = Fold_memq (TOP, v1);
654 TOP = NILP (TOP) ? Qt : Qnil;
660 if (CONSP (v1)) TOP = XCAR (v1);
661 else if (NILP (v1)) TOP = Qnil;
664 TOP = wrong_type_argument (Qlistp, v1);
672 if (CONSP (v1)) TOP = XCDR (v1);
673 else if (NILP (v1)) TOP = Qnil;
676 TOP = wrong_type_argument (Qlistp, v1);
683 TOP = Fcons (TOP, v1);
687 TOP = Fcons (TOP, Qnil);
692 TOP = Fcons (TOP, Fcons (v1, Qnil));
697 TOP = Flist (3, &TOP);
702 TOP = Flist (4, &TOP);
708 TOP = Flist (op, &TOP);
717 TOP = Faref (TOP, v1);
722 TOP = Faset (TOP, v1, v2);
726 TOP = Fsymbol_value (TOP);
729 case Bsymbol_function:
730 TOP = Fsymbol_function (TOP);
735 TOP = Fset (TOP, v1);
740 TOP = Ffset (TOP, v1);
745 TOP = Fget (TOP, v1, Qnil);
750 TOP = Fsubstring (TOP, v1, v2);
755 TOP = Fconcat (2, &TOP);
760 TOP = Fconcat (3, &TOP);
765 TOP = Fconcat (4, &TOP);
771 TOP = Fconcat (op, &TOP);
778 XSETINT (v1, XINT (v1) - 1);
789 XSETINT (v1, XINT (v1) + 1);
798 CHECK_INT_OR_FLOAT_COERCE_CHAR_OR_MARKER (v1);
799 CHECK_INT_OR_FLOAT_COERCE_CHAR_OR_MARKER (v2);
800 #ifdef LISP_FLOAT_TYPE
801 if (FLOATP (v1) || FLOATP (v2))
803 double f1 = (FLOATP (v1) ? float_data (XFLOAT (v1)) : XINT (v1));
804 double f2 = (FLOATP (v2) ? float_data (XFLOAT (v2)) : XINT (v2));
805 TOP = (f1 == f2 ? Qt : Qnil);
808 #endif /* LISP_FLOAT_TYPE */
809 TOP = (XINT (v1) == XINT (v2) ? Qt : Qnil);
814 TOP = arithcompare (TOP, v1, arith_grtr);
819 TOP = arithcompare (TOP, v1, arith_less);
824 TOP = arithcompare (TOP, v1, arith_less_or_equal);
829 TOP = arithcompare (TOP, v1, arith_grtr_or_equal);
834 TOP = Fminus (2, &TOP);
841 XSETINT (v1, - XINT (v1));
845 TOP = Fminus (1, &TOP);
850 TOP = Fplus (2, &TOP);
855 TOP = Fmax (2, &TOP);
860 TOP = Fmin (2, &TOP);
865 TOP = Ftimes (2, &TOP);
870 TOP = Fquo (2, &TOP);
875 TOP = Frem (TOP, v1);
879 v1 = make_int (BUF_PT (current_buffer));
884 TOP = Fgoto_char (TOP, Qnil);
888 TOP = Finsert (1, &TOP);
894 TOP = Finsert (op, &TOP);
898 v1 = make_int (BUF_ZV (current_buffer));
903 v1 = make_int (BUF_BEGV (current_buffer));
908 TOP = Fchar_after (TOP, Qnil);
911 case Bfollowing_char:
912 v1 = Ffollowing_char (Qnil);
916 case Bpreceding_char:
917 v1 = Fpreceding_char (Qnil);
921 case Bcurrent_column:
922 v1 = make_int (current_column (current_buffer));
927 TOP = Findent_to (TOP, Qnil, Qnil);
946 case Bcurrent_buffer:
947 PUSH (Fcurrent_buffer ());
951 TOP = Fset_buffer (TOP);
954 case Bsave_current_buffer:
955 record_unwind_protect (save_current_buffer_restore,
960 PUSH (Finteractive_p ());
964 TOP = Fforward_char (TOP, Qnil);
968 TOP = Fforward_word (TOP, Qnil);
971 case Bskip_chars_forward:
973 TOP = Fskip_chars_forward (TOP, v1, Qnil);
976 case Bskip_chars_backward:
978 TOP = Fskip_chars_backward (TOP, v1, Qnil);
982 TOP = Fforward_line (TOP, Qnil);
987 CHECK_CHAR_COERCE_INT (TOP);
988 TOP = make_char (syntax_code_spec
991 (current_buffer->mirror_syntax_table),
995 TOP = Fchar_syntax(TOP, Qnil);
998 case Bbuffer_substring:
1000 TOP = Fbuffer_substring (TOP, v1, Qnil);
1003 case Bdelete_region:
1005 TOP = Fdelete_region (TOP, v1, Qnil);
1008 case Bnarrow_to_region:
1010 TOP = Fnarrow_to_region (TOP, v1, Qnil);
1014 PUSH (Fwiden (Qnil));
1018 TOP = Fend_of_line (TOP, Qnil);
1024 TOP = Fset_marker (TOP, v2, v1);
1027 case Bmatch_beginning:
1028 TOP = Fmatch_beginning (TOP);
1032 TOP = Fmatch_end (TOP);
1036 TOP = Fupcase (TOP, Qnil);
1040 TOP = Fdowncase (TOP, Qnil);
1043 case Bstringeqlsign:
1045 TOP = Fstring_equal (TOP, v1);
1050 TOP = Fstring_lessp (TOP, v1);
1055 TOP = Fequal (TOP, v1);
1060 TOP = Fold_equal (TOP, v1);
1067 for (op = XINT (v2); op; op--)
1075 v1 = wrong_type_argument (Qlistp, v1);
1084 /* probably this code is OK, but nth_entry is commented
1086 /* #### will not work if cons type is an lrecord. */
1087 if (XTYPE (TOP) == Lisp_Type_Cons)
1089 /* Exchange args and then do nth. */
1096 TOP = Felt (TOP, v1);
1101 TOP = Fmember (TOP, v1);
1106 TOP = Fold_member (TOP, v1);
1111 TOP = Fassq (TOP, v1);
1116 TOP = Fold_assq (TOP, v1);
1120 TOP = Fnreverse (TOP);
1125 TOP = Fsetcar (TOP, v1);
1130 TOP = Fsetcdr (TOP, v1);
1151 TOP = Fnconc (2, &TOP);
1155 TOP = INT_OR_FLOATP (TOP) ? Qt : Qnil;
1159 TOP = INTP (TOP) ? Qt : Qnil;
1163 #ifdef BYTE_CODE_SAFE
1165 error ("unknown bytecode %d (byte compiler bug)", op);
1166 if ((op -= Bconstant) >= const_length)
1167 error ("no constant number %d (byte compiler bug)", op);
1170 PUSH (vectorp[op - Bconstant]);
1177 /* Binds and unbinds are supposed to be compiled balanced. */
1178 if (specpdl_depth() != speccount)
1179 /* FSF: abort() if BYTE_CODE_SAFE not defined */
1180 error ("binding stack not balanced (serious byte compiler bug)");
1185 syms_of_bytecode (void)
1187 defsymbol (&Qbyte_code, "byte-code");
1188 DEFSUBR (Fbyte_code);
1189 #ifdef BYTE_CODE_METER
1190 defsymbol (&Qbyte_code_meter, "byte-code-meter");
1195 vars_of_bytecode (void)
1197 #ifdef BYTE_CODE_METER
1199 DEFVAR_LISP ("byte-code-meter", &Vbyte_code_meter /*
1200 A vector of vectors which holds a histogram of byte-code usage.
1201 \(aref (aref byte-code-meter 0) CODE) indicates how many times the byte
1202 opcode CODE has been executed.
1203 \(aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,
1204 indicates how many times the byte opcodes CODE1 and CODE2 have been
1205 executed in succession.
1207 DEFVAR_BOOL ("byte-metering-on", &byte_metering_on /*
1208 If non-nil, keep profiling information on byte code usage.
1209 The variable byte-code-meter indicates how often each byte opcode is used.
1210 If a symbol has a property named `byte-code-meter' whose value is an
1211 integer, it is incremented each time that symbol's function is called.
1214 byte_metering_on = 0;
1215 Vbyte_code_meter = make_vector (256, Qzero);
1219 XVECTOR_DATA (Vbyte_code_meter)[i] =
1220 make_vector (256, Qzero);