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 #define Bmark 0141 /* no longer generated as of v18 */
134 #define Bgoto_char 0142
136 #define Bpoint_max 0144
137 #define Bpoint_min 0145
138 #define Bchar_after 0146
139 #define Bfollowing_char 0147
140 #define Bpreceding_char 0150
141 #define Bcurrent_column 0151
142 #define Bindent_to 0152
143 #define Bscan_buffer 0153 /* No longer generated as of v18 */
148 #define Bcurrent_buffer 0160
149 #define Bset_buffer 0161
150 #define Bread_char 0162 /* No longer generated as of v19 */
151 #define Bset_mark 0163 /* this loser is no longer generated as of v18 */
152 #define Binteractive_p 0164 /* Needed since interactive-p takes unevalled args */
154 #define Bforward_char 0165
155 #define Bforward_word 0166
156 #define Bskip_chars_forward 0167
157 #define Bskip_chars_backward 0170
158 #define Bforward_line 0171
159 #define Bchar_syntax 0172
160 #define Bbuffer_substring 0173
161 #define Bdelete_region 0174
162 #define Bnarrow_to_region 0175
164 #define Bend_of_line 0177
166 #define Bconstant2 0201
168 #define Bgotoifnil 0203
169 #define Bgotoifnonnil 0204
170 #define Bgotoifnilelsepop 0205
171 #define Bgotoifnonnilelsepop 0206
173 #define Bdiscard 0210
176 #define Bsave_excursion 0212
177 #define Bsave_window_excursion 0213
178 #define Bsave_restriction 0214
181 #define Bunwind_protect 0216
182 #define Bcondition_case 0217
183 #define Btemp_output_buffer_setup 0220
184 #define Btemp_output_buffer_show 0221
186 #define Bunbind_all 0222
188 #define Bset_marker 0223
189 #define Bmatch_beginning 0224
190 #define Bmatch_end 0225
192 #define Bdowncase 0227
194 #define Bstringeqlsign 0230
195 #define Bstringlss 0231
201 #define Bnreverse 0237
204 #define Bcar_safe 0242
205 #define Bcdr_safe 0243
209 #define Bnumberp 0247
210 #define Bintegerp 0250
213 #define BRgotoifnil 0253
214 #define BRgotoifnonnil 0254
215 #define BRgotoifnilelsepop 0255
216 #define BRgotoifnonnilelsepop 0256
219 #define BconcatN 0260
220 #define BinsertN 0261
222 #define Bconstant 0300
223 #define CONSTANTLIM 0100
225 /* Fetch the next byte from the bytecode stream */
229 /* Fetch two bytes from the bytecode stream
230 and make a 16-bit number out of them */
232 #define FETCH2 (op = FETCH, op + (FETCH << 8))
234 /* Push x onto the execution stack. */
236 /* This used to be #define PUSH(x) (*++stackp = (x))
237 This oddity is necessary because Alliant can't be bothered to
238 compile the preincrement operator properly, as of 4/91. -JimB */
239 #define PUSH(x) (stackp++, *stackp = (x))
241 /* Pop a value off the execution stack. */
243 #define POP (*stackp--)
245 /* Discard n values from the execution stack. */
247 #define DISCARD(n) (stackp -= (n))
249 /* Get the value which is at the top of the execution stack, but don't pop it. */
251 #define TOP (*stackp)
253 DEFUN ("byte-code", Fbyte_code
, Sbyte_code
, 3, 3, 0,
254 "Function used internally in byte-compiled code.\n\
255 The first argument, BYTESTR, is a string of byte code;\n\
256 the second, VECTOR, a vector of constants;\n\
257 the third, MAXDEPTH, the maximum stack depth used in this function.\n\
258 If the third argument is incorrect, Emacs may crash.")
259 (bytestr
, vector
, maxdepth
)
260 Lisp_Object bytestr
, vector
, maxdepth
;
262 struct gcpro gcpro1
, gcpro2
, gcpro3
;
263 int count
= specpdl_ptr
- specpdl
;
264 #ifdef BYTE_CODE_METER
271 register Lisp_Object
*stackp
;
273 register Lisp_Object v1
, v2
;
274 register Lisp_Object
*vectorp
= XVECTOR (vector
)->contents
;
275 #ifdef BYTE_CODE_SAFE
276 register int const_length
= XVECTOR (vector
)->size
;
278 /* Copy of BYTESTR, saved so we can tell if BYTESTR was relocated. */
279 Lisp_Object string_saved
;
280 /* Cached address of beginning of string,
281 valid if BYTESTR equals STRING_SAVED. */
282 register unsigned char *strbeg
;
284 CHECK_STRING (bytestr
, 0);
285 if (!VECTORP (vector
))
286 vector
= wrong_type_argument (Qvectorp
, vector
);
287 CHECK_NUMBER (maxdepth
, 2);
289 stackp
= (Lisp_Object
*) alloca (XFASTINT (maxdepth
) * sizeof (Lisp_Object
));
290 bzero (stackp
, XFASTINT (maxdepth
) * sizeof (Lisp_Object
));
291 GCPRO3 (bytestr
, vector
, *stackp
);
292 gcpro3
.nvars
= XFASTINT (maxdepth
);
296 stacke
= stackp
+ XFASTINT (maxdepth
);
298 /* Initialize the saved pc-pointer for fetching from the string. */
299 string_saved
= bytestr
;
300 pc
= XSTRING (string_saved
)->data
;
304 #ifdef BYTE_CODE_SAFE
306 error ("Byte code stack overflow (byte compiler bug), pc %d, depth %d",
307 pc
- XSTRING (string_saved
)->data
, stacke
- stackp
);
309 error ("Byte code stack underflow (byte compiler bug), pc %d",
310 pc
- XSTRING (string_saved
)->data
);
313 if (! EQ (string_saved
, bytestr
))
315 pc
= pc
- XSTRING (string_saved
)->data
+ XSTRING (bytestr
)->data
;
316 string_saved
= bytestr
;
319 #ifdef BYTE_CODE_METER
321 this_op
= op
= FETCH
;
322 METER_CODE (prev_op
, op
);
336 case Bvarref
: case Bvarref
+1: case Bvarref
+2: case Bvarref
+3:
337 case Bvarref
+4: case Bvarref
+5:
342 v2
= Fsymbol_value (v1
);
345 v2
= XSYMBOL (v1
)->value
;
346 if (MISCP (v2
) || EQ (v2
, Qunbound
))
347 v2
= Fsymbol_value (v1
);
360 case Bvarset
: case Bvarset
+1: case Bvarset
+2: case Bvarset
+3:
361 case Bvarset
+4: case Bvarset
+5:
364 Fset (vectorp
[op
], POP
);
375 case Bvarbind
: case Bvarbind
+1: case Bvarbind
+2: case Bvarbind
+3:
376 case Bvarbind
+4: case Bvarbind
+5:
379 specbind (vectorp
[op
], POP
);
390 case Bcall
: case Bcall
+1: case Bcall
+2: case Bcall
+3:
391 case Bcall
+4: case Bcall
+5:
395 #ifdef BYTE_CODE_METER
396 if (byte_metering_on
&& SYMBOLP (TOP
))
399 v2
= Fget (v1
, Qbyte_code_meter
);
401 && XINT (v2
) != ((1<<VALBITS
)-1))
403 XSETINT (v2
, XINT (v2
) + 1);
404 Fput (v1
, Qbyte_code_meter
, v2
);
408 TOP
= Ffuncall (op
+ 1, &TOP
);
419 case Bunbind
: case Bunbind
+1: case Bunbind
+2: case Bunbind
+3:
420 case Bunbind
+4: case Bunbind
+5:
423 unbind_to (specpdl_ptr
- specpdl
- op
, Qnil
);
427 /* To unbind back to the beginning of this frame. Not used yet,
428 but will be needed for tail-recursion elimination. */
429 unbind_to (count
, Qnil
);
434 op
= FETCH2
; /* pc = FETCH2 loses since FETCH2 contains pc++ */
435 pc
= XSTRING (string_saved
)->data
+ op
;
443 pc
= XSTRING (string_saved
)->data
+ op
;
452 pc
= XSTRING (string_saved
)->data
+ op
;
456 case Bgotoifnilelsepop
:
461 pc
= XSTRING (string_saved
)->data
+ op
;
466 case Bgotoifnonnilelsepop
:
471 pc
= XSTRING (string_saved
)->data
+ op
;
478 pc
+= (int) *pc
- 127;
485 pc
+= (int) *pc
- 128;
494 pc
+= (int) *pc
- 128;
499 case BRgotoifnilelsepop
:
509 case BRgotoifnonnilelsepop
:
533 PUSH (vectorp
[FETCH2
]);
536 case Bsave_excursion
:
537 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
540 case Bsave_window_excursion
:
541 TOP
= Fsave_window_excursion (TOP
);
544 case Bsave_restriction
:
545 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
550 TOP
= internal_catch (TOP
, Feval
, v1
);
553 case Bunwind_protect
:
554 record_unwind_protect (0, POP
);
555 (specpdl_ptr
- 1)->symbol
= Qnil
;
558 case Bcondition_case
:
560 v1
= Fcons (POP
, v1
);
561 TOP
= Fcondition_case (Fcons (TOP
, v1
));
564 case Btemp_output_buffer_setup
:
565 temp_output_buffer_setup (XSTRING (TOP
)->data
);
566 TOP
= Vstandard_output
;
569 case Btemp_output_buffer_show
:
571 temp_output_buffer_show (TOP
);
573 /* pop binding of standard-output */
574 unbind_to (specpdl_ptr
- specpdl
- 1, Qnil
);
581 CHECK_NUMBER (v2
, 0);
587 v1
= XCONS (v1
)->cdr
;
591 v1
= wrong_type_argument (Qlistp
, v1
);
600 TOP
= SYMBOLP (TOP
) ? Qt
: Qnil
;
604 TOP
= CONSP (TOP
) ? Qt
: Qnil
;
608 TOP
= STRINGP (TOP
) ? Qt
: Qnil
;
612 TOP
= CONSP (TOP
) || NILP (TOP
) ? Qt
: Qnil
;
617 TOP
= EQ (v1
, TOP
) ? Qt
: Qnil
;
622 TOP
= Fmemq (TOP
, v1
);
626 TOP
= NILP (TOP
) ? Qt
: Qnil
;
632 if (CONSP (v1
)) TOP
= XCONS (v1
)->car
;
633 else if (NILP (v1
)) TOP
= Qnil
;
634 else Fcar (wrong_type_argument (Qlistp
, v1
));
639 if (CONSP (v1
)) TOP
= XCONS (v1
)->cdr
;
640 else if (NILP (v1
)) TOP
= Qnil
;
641 else Fcdr (wrong_type_argument (Qlistp
, v1
));
646 TOP
= Fcons (TOP
, v1
);
650 TOP
= Fcons (TOP
, Qnil
);
655 TOP
= Fcons (TOP
, Fcons (v1
, Qnil
));
660 TOP
= Flist (3, &TOP
);
665 TOP
= Flist (4, &TOP
);
671 TOP
= Flist (op
, &TOP
);
680 TOP
= Faref (TOP
, v1
);
685 TOP
= Faset (TOP
, v1
, v2
);
689 TOP
= Fsymbol_value (TOP
);
692 case Bsymbol_function
:
693 TOP
= Fsymbol_function (TOP
);
698 TOP
= Fset (TOP
, v1
);
703 TOP
= Ffset (TOP
, v1
);
708 TOP
= Fget (TOP
, v1
);
713 TOP
= Fsubstring (TOP
, v1
, v2
);
718 TOP
= Fconcat (2, &TOP
);
723 TOP
= Fconcat (3, &TOP
);
728 TOP
= Fconcat (4, &TOP
);
734 TOP
= Fconcat (op
, &TOP
);
741 XSETINT (v1
, XINT (v1
) - 1);
752 XSETINT (v1
, XINT (v1
) + 1);
761 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v1
, 0);
762 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v2
, 0);
763 #ifdef LISP_FLOAT_TYPE
764 if (FLOATP (v1
) || FLOATP (v2
))
768 f1
= (FLOATP (v1
) ? XFLOAT (v1
)->data
: XINT (v1
));
769 f2
= (FLOATP (v2
) ? XFLOAT (v2
)->data
: XINT (v2
));
770 TOP
= (f1
== f2
? Qt
: Qnil
);
774 TOP
= (XINT (v1
) == XINT (v2
) ? Qt
: Qnil
);
779 TOP
= Fgtr (TOP
, v1
);
784 TOP
= Flss (TOP
, v1
);
789 TOP
= Fleq (TOP
, v1
);
794 TOP
= Fgeq (TOP
, v1
);
799 TOP
= Fminus (2, &TOP
);
806 XSETINT (v1
, - XINT (v1
));
810 TOP
= Fminus (1, &TOP
);
815 TOP
= Fplus (2, &TOP
);
820 TOP
= Fmax (2, &TOP
);
825 TOP
= Fmin (2, &TOP
);
830 TOP
= Ftimes (2, &TOP
);
835 TOP
= Fquo (2, &TOP
);
840 TOP
= Frem (TOP
, v1
);
844 XSETFASTINT (v1
, point
);
849 TOP
= Fgoto_char (TOP
);
853 TOP
= Finsert (1, &TOP
);
859 TOP
= Finsert (op
, &TOP
);
863 XSETFASTINT (v1
, ZV
);
868 XSETFASTINT (v1
, BEGV
);
873 TOP
= Fchar_after (TOP
);
876 case Bfollowing_char
:
877 v1
= Ffollowing_char ();
881 case Bpreceding_char
:
882 v1
= Fprevious_char ();
886 case Bcurrent_column
:
887 XSETFASTINT (v1
, current_column ());
892 TOP
= Findent_to (TOP
, Qnil
);
911 case Bcurrent_buffer
:
912 PUSH (Fcurrent_buffer ());
916 TOP
= Fset_buffer (TOP
);
920 PUSH (Fread_char ());
925 PUSH (Finteractive_p ());
929 TOP
= Fforward_char (TOP
);
933 TOP
= Fforward_word (TOP
);
936 case Bskip_chars_forward
:
938 TOP
= Fskip_chars_forward (TOP
, v1
);
941 case Bskip_chars_backward
:
943 TOP
= Fskip_chars_backward (TOP
, v1
);
947 TOP
= Fforward_line (TOP
);
951 CHECK_NUMBER (TOP
, 0);
953 syntax_code_spec
[(int) SYNTAX (XINT (TOP
))]);
956 case Bbuffer_substring
:
958 TOP
= Fbuffer_substring (TOP
, v1
);
963 TOP
= Fdelete_region (TOP
, v1
);
966 case Bnarrow_to_region
:
968 TOP
= Fnarrow_to_region (TOP
, v1
);
976 TOP
= Fend_of_line (TOP
);
982 TOP
= Fset_marker (TOP
, v2
, v1
);
985 case Bmatch_beginning
:
986 TOP
= Fmatch_beginning (TOP
);
990 TOP
= Fmatch_end (TOP
);
998 TOP
= Fdowncase (TOP
);
1001 case Bstringeqlsign
:
1003 TOP
= Fstring_equal (TOP
, v1
);
1008 TOP
= Fstring_lessp (TOP
, v1
);
1013 TOP
= Fequal (TOP
, v1
);
1018 TOP
= Fnthcdr (TOP
, v1
);
1024 /* Exchange args and then do nth. */
1030 TOP
= Felt (TOP
, v1
);
1035 TOP
= Fmember (TOP
, v1
);
1040 TOP
= Fassq (TOP
, v1
);
1044 TOP
= Fnreverse (TOP
);
1049 TOP
= Fsetcar (TOP
, v1
);
1054 TOP
= Fsetcdr (TOP
, v1
);
1060 TOP
= XCONS (v1
)->car
;
1068 TOP
= XCONS (v1
)->cdr
;
1075 TOP
= Fnconc (2, &TOP
);
1079 TOP
= (NUMBERP (TOP
) ? Qt
: Qnil
);
1083 TOP
= INTEGERP (TOP
) ? Qt
: Qnil
;
1086 #ifdef BYTE_CODE_SAFE
1088 error ("set-mark is an obsolete bytecode");
1091 error ("scan-buffer is an obsolete bytecode");
1094 error ("mark is an obsolete bytecode");
1099 #ifdef BYTE_CODE_SAFE
1101 error ("unknown bytecode %d (byte compiler bug)", op
);
1102 if ((op
-= Bconstant
) >= const_length
)
1103 error ("no constant number %d (byte compiler bug)", op
);
1106 PUSH (vectorp
[op
- Bconstant
]);
1113 /* Binds and unbinds are supposed to be compiled balanced. */
1114 if (specpdl_ptr
- specpdl
!= count
)
1115 #ifdef BYTE_CODE_SAFE
1116 error ("binding stack not balanced (serious byte compiler bug)");
1125 Qbytecode
= intern ("byte-code");
1126 staticpro (&Qbytecode
);
1128 defsubr (&Sbyte_code
);
1130 #ifdef BYTE_CODE_METER
1132 DEFVAR_LISP ("byte-code-meter", &Vbyte_code_meter
,
1133 "A vector of vectors which holds a histogram of byte-code usage.\n\
1134 (aref (aref byte-code-meter 0) CODE) indicates how many times the byte\n\
1135 opcode CODE has been executed.\n\
1136 (aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,\n\
1137 indicates how many times the byte opcodes CODE1 and CODE2 have been\n\
1138 executed in succession.");
1139 DEFVAR_BOOL ("byte-metering-on", &byte_metering_on
,
1140 "If non-nil, keep profiling information on byte code usage.\n\
1141 The variable byte-code-meter indicates how often each byte opcode is used.\n\
1142 If a symbol has a property named `byte-code-meter' whose value is an\n\
1143 integer, it is incremented each time that symbol's function is called.");
1145 byte_metering_on
= 0;
1146 Vbyte_code_meter
= Fmake_vector (make_number (256), make_number (0));
1147 Qbyte_code_meter
= intern ("byte-code-meter");
1148 staticpro (&Qbyte_code_meter
);
1152 XVECTOR (Vbyte_code_meter
)->contents
[i
] =
1153 Fmake_vector (make_number (256), make_number (0));