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, 675 Mass Ave, Cambridge, MA 02139, USA.
20 hacked on by jwz@lucid.com 17-jun-91
21 o added a compile-time switch to turn on simple sanity checking;
22 o put back the obsolete byte-codes for error-detection;
23 o added a new instruction, unbind_all, which I will use for
24 tail-recursion elimination;
25 o made temp_output_buffer_show be called with the right number
27 o made the new bytecodes be called with args in the right order;
28 o added metering support.
31 o added relative jump instructions;
32 o all conditionals now only do QUIT if they jump.
41 * define BYTE_CODE_SAFE to enable some minor sanity checking (useful for
42 * debugging the byte compiler...)
44 * define BYTE_CODE_METER to enable generation of a byte-op usage histogram.
46 /* #define BYTE_CODE_SAFE */
47 /* #define BYTE_CODE_METER */
50 #ifdef BYTE_CODE_METER
52 Lisp_Object Vbyte_code_meter
, Qbyte_code_meter
;
55 #define METER_2(code1, code2) \
56 XFASTINT (XVECTOR (XVECTOR (Vbyte_code_meter)->contents[(code1)]) \
59 #define METER_1(code) METER_2 (0, (code))
61 #define METER_CODE(last_code, this_code) \
63 if (byte_metering_on) \
65 if (METER_1 (this_code) != ((1<<VALBITS)-1)) \
66 METER_1 (this_code)++; \
68 && METER_2 (last_code, this_code) != ((1<<VALBITS)-1))\
69 METER_2 (last_code, this_code)++; \
73 #else /* no BYTE_CODE_METER */
75 #define METER_CODE(last_code, this_code)
77 #endif /* no BYTE_CODE_METER */
80 Lisp_Object Qbytecode
;
108 #define Bsymbol_value 0112
109 #define Bsymbol_function 0113
113 #define Bsubstring 0117
114 #define Bconcat2 0120
115 #define Bconcat3 0121
116 #define Bconcat4 0122
119 #define Beqlsign 0125
132 #define Bmark 0141 /* no longer generated as of v18 */
133 #define Bgoto_char 0142
135 #define Bpoint_max 0144
136 #define Bpoint_min 0145
137 #define Bchar_after 0146
138 #define Bfollowing_char 0147
139 #define Bpreceding_char 0150
140 #define Bcurrent_column 0151
141 #define Bindent_to 0152
142 #define Bscan_buffer 0153 /* No longer generated as of v18 */
147 #define Bcurrent_buffer 0160
148 #define Bset_buffer 0161
149 #define Bread_char 0162 /* No longer generated as of v19 */
150 #define Bset_mark 0163 /* this loser is no longer generated as of v18 */
151 #define Binteractive_p 0164 /* Needed since interactive-p takes unevalled args */
153 #define Bforward_char 0165
154 #define Bforward_word 0166
155 #define Bskip_chars_forward 0167
156 #define Bskip_chars_backward 0170
157 #define Bforward_line 0171
158 #define Bchar_syntax 0172
159 #define Bbuffer_substring 0173
160 #define Bdelete_region 0174
161 #define Bnarrow_to_region 0175
163 #define Bend_of_line 0177
165 #define Bconstant2 0201
167 #define Bgotoifnil 0203
168 #define Bgotoifnonnil 0204
169 #define Bgotoifnilelsepop 0205
170 #define Bgotoifnonnilelsepop 0206
172 #define Bdiscard 0210
175 #define Bsave_excursion 0212
176 #define Bsave_window_excursion 0213
177 #define Bsave_restriction 0214
180 #define Bunwind_protect 0216
181 #define Bcondition_case 0217
182 #define Btemp_output_buffer_setup 0220
183 #define Btemp_output_buffer_show 0221
185 #define Bunbind_all 0222
187 #define Bset_marker 0223
188 #define Bmatch_beginning 0224
189 #define Bmatch_end 0225
191 #define Bdowncase 0227
193 #define Bstringeqlsign 0230
194 #define Bstringlss 0231
200 #define Bnreverse 0237
203 #define Bcar_safe 0242
204 #define Bcdr_safe 0243
208 #define Bnumberp 0247
209 #define Bintegerp 0250
212 #define BRgotoifnil 0253
213 #define BRgotoifnonnil 0254
214 #define BRgotoifnilelsepop 0255
215 #define BRgotoifnonnilelsepop 0256
218 #define BconcatN 0260
219 #define BinsertN 0261
221 #define Bconstant 0300
222 #define CONSTANTLIM 0100
224 /* Fetch the next byte from the bytecode stream */
228 /* Fetch two bytes from the bytecode stream
229 and make a 16-bit number out of them */
231 #define FETCH2 (op = FETCH, op + (FETCH << 8))
233 /* Push x onto the execution stack. */
235 /* This used to be #define PUSH(x) (*++stackp = (x))
236 This oddity is necessary because Alliant can't be bothered to
237 compile the preincrement operator properly, as of 4/91. -JimB */
238 #define PUSH(x) (stackp++, *stackp = (x))
240 /* Pop a value off the execution stack. */
242 #define POP (*stackp--)
244 /* Discard n values from the execution stack. */
246 #define DISCARD(n) (stackp -= (n))
248 /* Get the value which is at the top of the execution stack, but don't pop it. */
250 #define TOP (*stackp)
252 DEFUN ("byte-code", Fbyte_code
, Sbyte_code
, 3, 3, 0,
253 "Function used internally in byte-compiled code.\n\
254 The first argument is a string of byte code; the second, a vector of constants;\n\
255 the third, the maximum stack depth used in this function.\n\
256 If the third argument is incorrect, Emacs may crash.")
257 (bytestr
, vector
, maxdepth
)
258 Lisp_Object bytestr
, vector
, maxdepth
;
260 struct gcpro gcpro1
, gcpro2
, gcpro3
;
261 int count
= specpdl_ptr
- specpdl
;
262 #ifdef BYTE_CODE_METER
269 register Lisp_Object
*stackp
;
271 register Lisp_Object v1
, v2
;
272 register Lisp_Object
*vectorp
= XVECTOR (vector
)->contents
;
273 #ifdef BYTE_CODE_SAFE
274 register int const_length
= XVECTOR (vector
)->size
;
276 /* Copy of BYTESTR, saved so we can tell if BYTESTR was relocated. */
277 Lisp_Object string_saved
;
278 /* Cached address of beginning of string,
279 valid if BYTESTR equals STRING_SAVED. */
280 register unsigned char *strbeg
;
282 CHECK_STRING (bytestr
, 0);
283 if (!VECTORP (vector
))
284 vector
= wrong_type_argument (Qvectorp
, vector
);
285 CHECK_NUMBER (maxdepth
, 2);
287 stackp
= (Lisp_Object
*) alloca (XFASTINT (maxdepth
) * sizeof (Lisp_Object
));
288 bzero (stackp
, XFASTINT (maxdepth
) * sizeof (Lisp_Object
));
289 GCPRO3 (bytestr
, vector
, *stackp
);
290 gcpro3
.nvars
= XFASTINT (maxdepth
);
294 stacke
= stackp
+ XFASTINT (maxdepth
);
296 /* Initialize the saved pc-pointer for fetching from the string. */
297 string_saved
= bytestr
;
298 pc
= XSTRING (string_saved
)->data
;
302 #ifdef BYTE_CODE_SAFE
304 error ("Byte code stack overflow (byte compiler bug), pc %d, depth %d",
305 pc
- XSTRING (string_saved
)->data
, stacke
- stackp
);
307 error ("Byte code stack underflow (byte compiler bug), pc %d",
308 pc
- XSTRING (string_saved
)->data
);
311 if (! EQ (string_saved
, bytestr
))
313 pc
= pc
- XSTRING (string_saved
)->data
+ XSTRING (bytestr
)->data
;
314 string_saved
= bytestr
;
317 #ifdef BYTE_CODE_METER
319 this_op
= op
= FETCH
;
320 METER_CODE (prev_op
, op
);
334 case Bvarref
: case Bvarref
+1: case Bvarref
+2: case Bvarref
+3:
335 case Bvarref
+4: case Bvarref
+5:
340 v2
= Fsymbol_value (v1
);
343 v2
= XSYMBOL (v1
)->value
;
344 if (MISCP (v2
) || EQ (v2
, Qunbound
))
345 v2
= Fsymbol_value (v1
);
358 case Bvarset
: case Bvarset
+1: case Bvarset
+2: case Bvarset
+3:
359 case Bvarset
+4: case Bvarset
+5:
362 Fset (vectorp
[op
], POP
);
373 case Bvarbind
: case Bvarbind
+1: case Bvarbind
+2: case Bvarbind
+3:
374 case Bvarbind
+4: case Bvarbind
+5:
377 specbind (vectorp
[op
], POP
);
388 case Bcall
: case Bcall
+1: case Bcall
+2: case Bcall
+3:
389 case Bcall
+4: case Bcall
+5:
393 #ifdef BYTE_CODE_METER
394 if (byte_metering_on
&& SYMBOLP (TOP
))
397 v2
= Fget (v1
, Qbyte_code_meter
);
399 && XINT (v2
) != ((1<<VALBITS
)-1))
401 XSETINT (v2
, XINT (v2
) + 1);
402 Fput (v1
, Qbyte_code_meter
, v2
);
406 TOP
= Ffuncall (op
+ 1, &TOP
);
417 case Bunbind
: case Bunbind
+1: case Bunbind
+2: case Bunbind
+3:
418 case Bunbind
+4: case Bunbind
+5:
421 unbind_to (specpdl_ptr
- specpdl
- op
, Qnil
);
425 /* To unbind back to the beginning of this frame. Not used yet,
426 but will be needed for tail-recursion elimination. */
427 unbind_to (count
, Qnil
);
432 op
= FETCH2
; /* pc = FETCH2 loses since FETCH2 contains pc++ */
433 pc
= XSTRING (string_saved
)->data
+ op
;
441 pc
= XSTRING (string_saved
)->data
+ op
;
450 pc
= XSTRING (string_saved
)->data
+ op
;
454 case Bgotoifnilelsepop
:
459 pc
= XSTRING (string_saved
)->data
+ op
;
464 case Bgotoifnonnilelsepop
:
469 pc
= XSTRING (string_saved
)->data
+ op
;
497 case BRgotoifnilelsepop
:
507 case BRgotoifnonnilelsepop
:
531 PUSH (vectorp
[FETCH2
]);
534 case Bsave_excursion
:
535 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
538 case Bsave_window_excursion
:
539 TOP
= Fsave_window_excursion (TOP
);
542 case Bsave_restriction
:
543 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
548 TOP
= internal_catch (TOP
, Feval
, v1
);
551 case Bunwind_protect
:
552 record_unwind_protect (0, POP
);
553 (specpdl_ptr
- 1)->symbol
= Qnil
;
556 case Bcondition_case
:
558 v1
= Fcons (POP
, v1
);
559 TOP
= Fcondition_case (Fcons (TOP
, v1
));
562 case Btemp_output_buffer_setup
:
563 temp_output_buffer_setup (XSTRING (TOP
)->data
);
564 TOP
= Vstandard_output
;
567 case Btemp_output_buffer_show
:
569 temp_output_buffer_show (TOP
);
571 /* pop binding of standard-output */
572 unbind_to (specpdl_ptr
- specpdl
- 1, Qnil
);
579 CHECK_NUMBER (v2
, 0);
585 v1
= XCONS (v1
)->cdr
;
589 v1
= wrong_type_argument (Qlistp
, v1
);
598 TOP
= SYMBOLP (TOP
) ? Qt
: Qnil
;
602 TOP
= CONSP (TOP
) ? Qt
: Qnil
;
606 TOP
= STRINGP (TOP
) ? Qt
: Qnil
;
610 TOP
= CONSP (TOP
) || NILP (TOP
) ? Qt
: Qnil
;
615 TOP
= EQ (v1
, TOP
) ? Qt
: Qnil
;
620 TOP
= Fmemq (TOP
, v1
);
624 TOP
= NILP (TOP
) ? Qt
: Qnil
;
630 if (CONSP (v1
)) TOP
= XCONS (v1
)->car
;
631 else if (NILP (v1
)) TOP
= Qnil
;
632 else Fcar (wrong_type_argument (Qlistp
, v1
));
637 if (CONSP (v1
)) TOP
= XCONS (v1
)->cdr
;
638 else if (NILP (v1
)) TOP
= Qnil
;
639 else Fcdr (wrong_type_argument (Qlistp
, v1
));
644 TOP
= Fcons (TOP
, v1
);
648 TOP
= Fcons (TOP
, Qnil
);
653 TOP
= Fcons (TOP
, Fcons (v1
, Qnil
));
658 TOP
= Flist (3, &TOP
);
663 TOP
= Flist (4, &TOP
);
669 TOP
= Flist (op
, &TOP
);
678 TOP
= Faref (TOP
, v1
);
683 TOP
= Faset (TOP
, v1
, v2
);
687 TOP
= Fsymbol_value (TOP
);
690 case Bsymbol_function
:
691 TOP
= Fsymbol_function (TOP
);
696 TOP
= Fset (TOP
, v1
);
701 TOP
= Ffset (TOP
, v1
);
706 TOP
= Fget (TOP
, v1
);
711 TOP
= Fsubstring (TOP
, v1
, v2
);
716 TOP
= Fconcat (2, &TOP
);
721 TOP
= Fconcat (3, &TOP
);
726 TOP
= Fconcat (4, &TOP
);
732 TOP
= Fconcat (op
, &TOP
);
739 XSETINT (v1
, XINT (v1
) - 1);
750 XSETINT (v1
, XINT (v1
) + 1);
759 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v1
, 0);
760 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v2
, 0);
761 #ifdef LISP_FLOAT_TYPE
762 if (FLOATP (v1
) || FLOATP (v2
))
766 f1
= (FLOATP (v1
) ? XFLOAT (v1
)->data
: XINT (v1
));
767 f2
= (FLOATP (v2
) ? XFLOAT (v2
)->data
: XINT (v2
));
768 TOP
= (f1
== f2
? Qt
: Qnil
);
772 TOP
= (XINT (v1
) == XINT (v2
) ? Qt
: Qnil
);
777 TOP
= Fgtr (TOP
, v1
);
782 TOP
= Flss (TOP
, v1
);
787 TOP
= Fleq (TOP
, v1
);
792 TOP
= Fgeq (TOP
, v1
);
797 TOP
= Fminus (2, &TOP
);
804 XSETINT (v1
, - XINT (v1
));
808 TOP
= Fminus (1, &TOP
);
813 TOP
= Fplus (2, &TOP
);
818 TOP
= Fmax (2, &TOP
);
823 TOP
= Fmin (2, &TOP
);
828 TOP
= Ftimes (2, &TOP
);
833 TOP
= Fquo (2, &TOP
);
838 TOP
= Frem (TOP
, v1
);
842 XSETFASTINT (v1
, point
);
847 TOP
= Fgoto_char (TOP
);
851 TOP
= Finsert (1, &TOP
);
857 TOP
= Finsert (op
, &TOP
);
861 XSETFASTINT (v1
, ZV
);
866 XSETFASTINT (v1
, BEGV
);
871 TOP
= Fchar_after (TOP
);
874 case Bfollowing_char
:
875 v1
= Ffollowing_char ();
879 case Bpreceding_char
:
880 v1
= Fprevious_char ();
884 case Bcurrent_column
:
885 XSETFASTINT (v1
, current_column ());
890 TOP
= Findent_to (TOP
, Qnil
);
909 case Bcurrent_buffer
:
910 PUSH (Fcurrent_buffer ());
914 TOP
= Fset_buffer (TOP
);
918 PUSH (Fread_char ());
923 PUSH (Finteractive_p ());
927 TOP
= Fforward_char (TOP
);
931 TOP
= Fforward_word (TOP
);
934 case Bskip_chars_forward
:
936 TOP
= Fskip_chars_forward (TOP
, v1
);
939 case Bskip_chars_backward
:
941 TOP
= Fskip_chars_backward (TOP
, v1
);
945 TOP
= Fforward_line (TOP
);
949 CHECK_NUMBER (TOP
, 0);
951 syntax_code_spec
[(int) SYNTAX (XINT (TOP
))]);
954 case Bbuffer_substring
:
956 TOP
= Fbuffer_substring (TOP
, v1
);
961 TOP
= Fdelete_region (TOP
, v1
);
964 case Bnarrow_to_region
:
966 TOP
= Fnarrow_to_region (TOP
, v1
);
974 TOP
= Fend_of_line (TOP
);
980 TOP
= Fset_marker (TOP
, v2
, v1
);
983 case Bmatch_beginning
:
984 TOP
= Fmatch_beginning (TOP
);
988 TOP
= Fmatch_end (TOP
);
996 TOP
= Fdowncase (TOP
);
1001 TOP
= Fstring_equal (TOP
, v1
);
1006 TOP
= Fstring_lessp (TOP
, v1
);
1011 TOP
= Fequal (TOP
, v1
);
1016 TOP
= Fnthcdr (TOP
, v1
);
1022 /* Exchange args and then do nth. */
1028 TOP
= Felt (TOP
, v1
);
1033 TOP
= Fmember (TOP
, v1
);
1038 TOP
= Fassq (TOP
, v1
);
1042 TOP
= Fnreverse (TOP
);
1047 TOP
= Fsetcar (TOP
, v1
);
1052 TOP
= Fsetcdr (TOP
, v1
);
1058 TOP
= XCONS (v1
)->car
;
1066 TOP
= XCONS (v1
)->cdr
;
1073 TOP
= Fnconc (2, &TOP
);
1077 TOP
= (NUMBERP (TOP
) ? Qt
: Qnil
);
1081 TOP
= INTEGERP (TOP
) ? Qt
: Qnil
;
1084 #ifdef BYTE_CODE_SAFE
1086 error ("set-mark is an obsolete bytecode");
1089 error ("scan-buffer is an obsolete bytecode");
1092 error ("mark is an obsolete bytecode");
1097 #ifdef BYTE_CODE_SAFE
1099 error ("unknown bytecode %d (byte compiler bug)", op
);
1100 if ((op
-= Bconstant
) >= const_length
)
1101 error ("no constant number %d (byte compiler bug)", op
);
1104 PUSH (vectorp
[op
- Bconstant
]);
1111 /* Binds and unbinds are supposed to be compiled balanced. */
1112 if (specpdl_ptr
- specpdl
!= count
)
1113 #ifdef BYTE_CODE_SAFE
1114 error ("binding stack not balanced (serious byte compiler bug)");
1123 Qbytecode
= intern ("byte-code");
1124 staticpro (&Qbytecode
);
1126 defsubr (&Sbyte_code
);
1128 #ifdef BYTE_CODE_METER
1130 DEFVAR_LISP ("byte-code-meter", &Vbyte_code_meter
,
1131 "A vector of vectors which holds a histogram of byte-code usage.\n\
1132 (aref (aref byte-code-meter 0) CODE) indicates how many times the byte\n\
1133 opcode CODE has been executed.\n\
1134 (aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,\n\
1135 indicates how many times the byte opcodes CODE1 and CODE2 have been\n\
1136 executed in succession.");
1137 DEFVAR_BOOL ("byte-metering-on", &byte_metering_on
,
1138 "If non-nil, keep profiling information on byte code usage.\n\
1139 The variable byte-code-meter indicates how often each byte opcode is used.\n\
1140 If a symbol has a property named `byte-code-meter' whose value is an\n\
1141 integer, it is incremented each time that symbol's function is called.");
1143 byte_metering_on
= 0;
1144 Vbyte_code_meter
= Fmake_vector (make_number (256), make_number (0));
1145 Qbyte_code_meter
= intern ("byte-code-meter");
1146 staticpro (&Qbyte_code_meter
);
1150 XVECTOR (Vbyte_code_meter
)->contents
[i
] =
1151 Fmake_vector (make_number (256), make_number (0));