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.
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
;
57 #define METER_2(code1, code2) \
58 XFASTINT (XVECTOR (XVECTOR (Vbyte_code_meter)->contents[(code1)]) \
61 #define METER_1(code) METER_2 (0, (code))
63 #define METER_CODE(last_code, this_code) \
65 if (byte_metering_on) \
67 if (METER_1 (this_code) != ((1<<VALBITS)-1)) \
68 METER_1 (this_code)++; \
70 && METER_2 (last_code, this_code) != ((1<<VALBITS)-1))\
71 METER_2 (last_code, this_code)++; \
75 #else /* no BYTE_CODE_METER */
77 #define METER_CODE(last_code, this_code)
79 #endif /* no BYTE_CODE_METER */
82 Lisp_Object Qbytecode
;
110 #define Bsymbol_value 0112
111 #define Bsymbol_function 0113
115 #define Bsubstring 0117
116 #define Bconcat2 0120
117 #define Bconcat3 0121
118 #define Bconcat4 0122
121 #define Beqlsign 0125
134 /* Was Bmark in v17. */
135 #define Bsave_current_buffer 0141
136 #define Bgoto_char 0142
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 */
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
167 #define Bend_of_line 0177
169 #define Bconstant2 0201
171 #define Bgotoifnil 0203
172 #define Bgotoifnonnil 0204
173 #define Bgotoifnilelsepop 0205
174 #define Bgotoifnonnilelsepop 0206
176 #define Bdiscard 0210
179 #define Bsave_excursion 0212
180 #define Bsave_window_excursion 0213
181 #define Bsave_restriction 0214
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
195 #define Bdowncase 0227
197 #define Bstringeqlsign 0230
198 #define Bstringlss 0231
204 #define Bnreverse 0237
207 #define Bcar_safe 0242
208 #define Bcdr_safe 0243
212 #define Bnumberp 0247
213 #define Bintegerp 0250
216 #define BRgotoifnil 0253
217 #define BRgotoifnonnil 0254
218 #define BRgotoifnilelsepop 0255
219 #define BRgotoifnonnilelsepop 0256
222 #define BconcatN 0260
223 #define BinsertN 0261
225 #define Bconstant 0300
226 #define CONSTANTLIM 0100
228 /* Fetch the next byte from the bytecode stream */
232 /* Fetch two bytes from the bytecode stream
233 and make a 16-bit number out of them */
235 #define FETCH2 (op = FETCH, op + (FETCH << 8))
237 /* Push x onto the execution stack. */
239 /* This used to be #define PUSH(x) (*++stackp = (x))
240 This oddity is necessary because Alliant can't be bothered to
241 compile the preincrement operator properly, as of 4/91. -JimB */
242 #define PUSH(x) (stackp++, *stackp = (x))
244 /* Pop a value off the execution stack. */
246 #define POP (*stackp--)
248 /* Discard n values from the execution stack. */
250 #define DISCARD(n) (stackp -= (n))
252 /* Get the value which is at the top of the execution stack, but don't pop it. */
254 #define TOP (*stackp)
256 /* Garbage collect if we have consed enough since the last time.
257 We do this at every branch, to avoid loops that never GC. */
260 if (consing_since_gc > gc_cons_threshold) \
262 Fgarbage_collect (); \
263 HANDLE_RELOCATION (); \
267 /* Relocate BYTESTR if there has been a GC recently. */
268 #define HANDLE_RELOCATION() \
269 if (! EQ (string_saved, bytestr)) \
271 pc = pc - XSTRING (string_saved)->data + XSTRING (bytestr)->data; \
272 string_saved = bytestr; \
276 /* Check for jumping out of range. */
277 #define CHECK_RANGE(ARG) \
278 if (ARG >= bytestr_length) abort ()
280 DEFUN ("byte-code", Fbyte_code
, Sbyte_code
, 3, 3, 0,
281 "Function used internally in byte-compiled code.\n\
282 The first argument, BYTESTR, is a string of byte code;\n\
283 the second, VECTOR, a vector of constants;\n\
284 the third, MAXDEPTH, the maximum stack depth used in this function.\n\
285 If the third argument is incorrect, Emacs may crash.")
286 (bytestr
, vector
, maxdepth
)
287 Lisp_Object bytestr
, vector
, maxdepth
;
289 struct gcpro gcpro1
, gcpro2
, gcpro3
;
290 int count
= specpdl_ptr
- specpdl
;
291 #ifdef BYTE_CODE_METER
298 register Lisp_Object
*stackp
;
300 register Lisp_Object v1
, v2
;
301 register Lisp_Object
*vectorp
= XVECTOR (vector
)->contents
;
302 #ifdef BYTE_CODE_SAFE
303 register int const_length
= XVECTOR (vector
)->size
;
305 /* Copy of BYTESTR, saved so we can tell if BYTESTR was relocated. */
306 Lisp_Object string_saved
;
307 /* Cached address of beginning of string,
308 valid if BYTESTR equals STRING_SAVED. */
309 register unsigned char *strbeg
;
310 int bytestr_length
= STRING_BYTES (XSTRING (bytestr
));
312 CHECK_STRING (bytestr
, 0);
313 if (!VECTORP (vector
))
314 vector
= wrong_type_argument (Qvectorp
, vector
);
315 CHECK_NUMBER (maxdepth
, 2);
317 stackp
= (Lisp_Object
*) alloca (XFASTINT (maxdepth
) * sizeof (Lisp_Object
));
318 bzero (stackp
, XFASTINT (maxdepth
) * sizeof (Lisp_Object
));
319 GCPRO3 (bytestr
, vector
, *stackp
);
320 gcpro3
.nvars
= XFASTINT (maxdepth
);
324 stacke
= stackp
+ XFASTINT (maxdepth
);
326 /* Initialize the saved pc-pointer for fetching from the string. */
327 string_saved
= bytestr
;
328 pc
= XSTRING (string_saved
)->data
;
332 #ifdef BYTE_CODE_SAFE
334 error ("Byte code stack overflow (byte compiler bug), pc %d, depth %d",
335 pc
- XSTRING (string_saved
)->data
, stacke
- stackp
);
337 error ("Byte code stack underflow (byte compiler bug), pc %d",
338 pc
- XSTRING (string_saved
)->data
);
341 /* Update BYTESTR if we had a garbage collection. */
342 HANDLE_RELOCATION ();
344 #ifdef BYTE_CODE_METER
346 this_op
= op
= FETCH
;
347 METER_CODE (prev_op
, op
);
361 case Bvarref
: case Bvarref
+1: case Bvarref
+2: case Bvarref
+3:
362 case Bvarref
+4: case Bvarref
+5:
367 v2
= Fsymbol_value (v1
);
370 v2
= XSYMBOL (v1
)->value
;
371 if (MISCP (v2
) || EQ (v2
, Qunbound
))
372 v2
= Fsymbol_value (v1
);
385 case Bvarset
: case Bvarset
+1: case Bvarset
+2: case Bvarset
+3:
386 case Bvarset
+4: case Bvarset
+5:
389 Fset (vectorp
[op
], POP
);
400 case Bvarbind
: case Bvarbind
+1: case Bvarbind
+2: case Bvarbind
+3:
401 case Bvarbind
+4: case Bvarbind
+5:
404 specbind (vectorp
[op
], POP
);
415 case Bcall
: case Bcall
+1: case Bcall
+2: case Bcall
+3:
416 case Bcall
+4: case Bcall
+5:
420 #ifdef BYTE_CODE_METER
421 if (byte_metering_on
&& SYMBOLP (TOP
))
424 v2
= Fget (v1
, Qbyte_code_meter
);
426 && XINT (v2
) != ((1<<VALBITS
)-1))
428 XSETINT (v2
, XINT (v2
) + 1);
429 Fput (v1
, Qbyte_code_meter
, v2
);
433 TOP
= Ffuncall (op
+ 1, &TOP
);
444 case Bunbind
: case Bunbind
+1: case Bunbind
+2: case Bunbind
+3:
445 case Bunbind
+4: case Bunbind
+5:
448 unbind_to (specpdl_ptr
- specpdl
- op
, Qnil
);
452 /* To unbind back to the beginning of this frame. Not used yet,
453 but will be needed for tail-recursion elimination. */
454 unbind_to (count
, Qnil
);
460 op
= FETCH2
; /* pc = FETCH2 loses since FETCH2 contains pc++ */
462 pc
= XSTRING (string_saved
)->data
+ op
;
472 pc
= XSTRING (string_saved
)->data
+ op
;
483 pc
= XSTRING (string_saved
)->data
+ op
;
487 case Bgotoifnilelsepop
:
494 pc
= XSTRING (string_saved
)->data
+ op
;
499 case Bgotoifnonnilelsepop
:
506 pc
= XSTRING (string_saved
)->data
+ op
;
514 pc
+= (int) *pc
- 127;
522 pc
+= (int) *pc
- 128;
532 pc
+= (int) *pc
- 128;
537 case BRgotoifnilelsepop
:
548 case BRgotoifnonnilelsepop
:
573 PUSH (vectorp
[FETCH2
]);
576 case Bsave_excursion
:
577 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
580 case Bsave_current_buffer
:
581 case Bsave_current_buffer_1
:
582 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
585 case Bsave_window_excursion
:
586 TOP
= Fsave_window_excursion (TOP
);
589 case Bsave_restriction
:
590 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
595 TOP
= internal_catch (TOP
, Feval
, v1
);
598 case Bunwind_protect
:
599 record_unwind_protect (0, POP
);
600 (specpdl_ptr
- 1)->symbol
= Qnil
;
603 case Bcondition_case
:
605 v1
= Fcons (POP
, v1
);
606 TOP
= Fcondition_case (Fcons (TOP
, v1
));
609 case Btemp_output_buffer_setup
:
610 temp_output_buffer_setup (XSTRING (TOP
)->data
);
611 TOP
= Vstandard_output
;
614 case Btemp_output_buffer_show
:
616 temp_output_buffer_show (TOP
);
618 /* pop binding of standard-output */
619 unbind_to (specpdl_ptr
- specpdl
- 1, Qnil
);
626 CHECK_NUMBER (v2
, 0);
636 v1
= wrong_type_argument (Qlistp
, v1
);
645 TOP
= SYMBOLP (TOP
) ? Qt
: Qnil
;
649 TOP
= CONSP (TOP
) ? Qt
: Qnil
;
653 TOP
= STRINGP (TOP
) ? Qt
: Qnil
;
657 TOP
= CONSP (TOP
) || NILP (TOP
) ? Qt
: Qnil
;
662 TOP
= EQ (v1
, TOP
) ? Qt
: Qnil
;
667 TOP
= Fmemq (TOP
, v1
);
671 TOP
= NILP (TOP
) ? Qt
: Qnil
;
677 if (CONSP (v1
)) TOP
= XCAR (v1
);
678 else if (NILP (v1
)) TOP
= Qnil
;
679 else Fcar (wrong_type_argument (Qlistp
, v1
));
684 if (CONSP (v1
)) TOP
= XCDR (v1
);
685 else if (NILP (v1
)) TOP
= Qnil
;
686 else Fcdr (wrong_type_argument (Qlistp
, v1
));
691 TOP
= Fcons (TOP
, v1
);
695 TOP
= Fcons (TOP
, Qnil
);
700 TOP
= Fcons (TOP
, Fcons (v1
, Qnil
));
705 TOP
= Flist (3, &TOP
);
710 TOP
= Flist (4, &TOP
);
716 TOP
= Flist (op
, &TOP
);
725 TOP
= Faref (TOP
, v1
);
730 TOP
= Faset (TOP
, v1
, v2
);
734 TOP
= Fsymbol_value (TOP
);
737 case Bsymbol_function
:
738 TOP
= Fsymbol_function (TOP
);
743 TOP
= Fset (TOP
, v1
);
748 TOP
= Ffset (TOP
, v1
);
753 TOP
= Fget (TOP
, v1
);
758 TOP
= Fsubstring (TOP
, v1
, v2
);
763 TOP
= Fconcat (2, &TOP
);
768 TOP
= Fconcat (3, &TOP
);
773 TOP
= Fconcat (4, &TOP
);
779 TOP
= Fconcat (op
, &TOP
);
786 XSETINT (v1
, XINT (v1
) - 1);
797 XSETINT (v1
, XINT (v1
) + 1);
806 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v1
, 0);
807 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v2
, 0);
808 #ifdef LISP_FLOAT_TYPE
809 if (FLOATP (v1
) || FLOATP (v2
))
813 f1
= (FLOATP (v1
) ? XFLOAT_DATA (v1
) : XINT (v1
));
814 f2
= (FLOATP (v2
) ? XFLOAT_DATA (v2
) : XINT (v2
));
815 TOP
= (f1
== f2
? Qt
: Qnil
);
819 TOP
= (XINT (v1
) == XINT (v2
) ? Qt
: Qnil
);
824 TOP
= Fgtr (TOP
, v1
);
829 TOP
= Flss (TOP
, v1
);
834 TOP
= Fleq (TOP
, v1
);
839 TOP
= Fgeq (TOP
, v1
);
844 TOP
= Fminus (2, &TOP
);
851 XSETINT (v1
, - XINT (v1
));
855 TOP
= Fminus (1, &TOP
);
860 TOP
= Fplus (2, &TOP
);
865 TOP
= Fmax (2, &TOP
);
870 TOP
= Fmin (2, &TOP
);
875 TOP
= Ftimes (2, &TOP
);
880 TOP
= Fquo (2, &TOP
);
885 TOP
= Frem (TOP
, v1
);
889 XSETFASTINT (v1
, PT
);
894 TOP
= Fgoto_char (TOP
);
898 TOP
= Finsert (1, &TOP
);
904 TOP
= Finsert (op
, &TOP
);
908 XSETFASTINT (v1
, ZV
);
913 XSETFASTINT (v1
, BEGV
);
918 TOP
= Fchar_after (TOP
);
921 case Bfollowing_char
:
922 v1
= Ffollowing_char ();
926 case Bpreceding_char
:
927 v1
= Fprevious_char ();
931 case Bcurrent_column
:
932 XSETFASTINT (v1
, current_column ());
937 TOP
= Findent_to (TOP
, Qnil
);
956 case Bcurrent_buffer
:
957 PUSH (Fcurrent_buffer ());
961 TOP
= Fset_buffer (TOP
);
965 PUSH (Finteractive_p ());
969 TOP
= Fforward_char (TOP
);
973 TOP
= Fforward_word (TOP
);
976 case Bskip_chars_forward
:
978 TOP
= Fskip_chars_forward (TOP
, v1
);
981 case Bskip_chars_backward
:
983 TOP
= Fskip_chars_backward (TOP
, v1
);
987 TOP
= Fforward_line (TOP
);
991 CHECK_NUMBER (TOP
, 0);
993 syntax_code_spec
[(int) SYNTAX (XINT (TOP
))]);
996 case Bbuffer_substring
:
998 TOP
= Fbuffer_substring (TOP
, v1
);
1001 case Bdelete_region
:
1003 TOP
= Fdelete_region (TOP
, v1
);
1006 case Bnarrow_to_region
:
1008 TOP
= Fnarrow_to_region (TOP
, v1
);
1016 TOP
= Fend_of_line (TOP
);
1022 TOP
= Fset_marker (TOP
, v2
, v1
);
1025 case Bmatch_beginning
:
1026 TOP
= Fmatch_beginning (TOP
);
1030 TOP
= Fmatch_end (TOP
);
1034 TOP
= Fupcase (TOP
);
1038 TOP
= Fdowncase (TOP
);
1041 case Bstringeqlsign
:
1043 TOP
= Fstring_equal (TOP
, v1
);
1048 TOP
= Fstring_lessp (TOP
, v1
);
1053 TOP
= Fequal (TOP
, v1
);
1058 TOP
= Fnthcdr (TOP
, v1
);
1064 /* Exchange args and then do nth. */
1070 TOP
= Felt (TOP
, v1
);
1075 TOP
= Fmember (TOP
, v1
);
1080 TOP
= Fassq (TOP
, v1
);
1084 TOP
= Fnreverse (TOP
);
1089 TOP
= Fsetcar (TOP
, v1
);
1094 TOP
= Fsetcdr (TOP
, v1
);
1115 TOP
= Fnconc (2, &TOP
);
1119 TOP
= (NUMBERP (TOP
) ? Qt
: Qnil
);
1123 TOP
= INTEGERP (TOP
) ? Qt
: Qnil
;
1126 #ifdef BYTE_CODE_SAFE
1128 error ("set-mark is an obsolete bytecode");
1131 error ("scan-buffer is an obsolete bytecode");
1136 #ifdef BYTE_CODE_SAFE
1138 error ("unknown bytecode %d (byte compiler bug)", op
);
1139 if ((op
-= Bconstant
) >= const_length
)
1140 error ("no constant number %d (byte compiler bug)", op
);
1143 PUSH (vectorp
[op
- Bconstant
]);
1150 /* Binds and unbinds are supposed to be compiled balanced. */
1151 if (specpdl_ptr
- specpdl
!= count
)
1152 #ifdef BYTE_CODE_SAFE
1153 error ("binding stack not balanced (serious byte compiler bug)");
1163 Qbytecode
= intern ("byte-code");
1164 staticpro (&Qbytecode
);
1166 defsubr (&Sbyte_code
);
1168 #ifdef BYTE_CODE_METER
1170 DEFVAR_LISP ("byte-code-meter", &Vbyte_code_meter
,
1171 "A vector of vectors which holds a histogram of byte-code usage.\n\
1172 (aref (aref byte-code-meter 0) CODE) indicates how many times the byte\n\
1173 opcode CODE has been executed.\n\
1174 (aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,\n\
1175 indicates how many times the byte opcodes CODE1 and CODE2 have been\n\
1176 executed in succession.");
1177 DEFVAR_BOOL ("byte-metering-on", &byte_metering_on
,
1178 "If non-nil, keep profiling information on byte code usage.\n\
1179 The variable byte-code-meter indicates how often each byte opcode is used.\n\
1180 If a symbol has a property named `byte-code-meter' whose value is an\n\
1181 integer, it is incremented each time that symbol's function is called.");
1183 byte_metering_on
= 0;
1184 Vbyte_code_meter
= Fmake_vector (make_number (256), make_number (0));
1185 Qbyte_code_meter
= intern ("byte-code-meter");
1186 staticpro (&Qbyte_code_meter
);
1190 XVECTOR (Vbyte_code_meter
)->contents
[i
] =
1191 Fmake_vector (make_number (256), make_number (0));