1 /* Execution of byte code produced by bytecomp.el.
2 Copyright (C) 1985, 1986, 1987, 1988, 1992 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 (XTYPE (vector
) != Lisp_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:
339 if (XTYPE (v1
) != Lisp_Symbol
)
340 v2
= Fsymbol_value (v1
);
343 v2
= XSYMBOL (v1
)->value
;
344 #ifdef SWITCH_ENUM_BUG
345 switch ((int) XTYPE (v2
))
351 if (!EQ (v2
, Qunbound
))
356 case Lisp_Buffer_Local_Value
:
357 case Lisp_Some_Buffer_Local_Value
:
358 case Lisp_Buffer_Objfwd
:
360 v2
= Fsymbol_value (v1
);
374 case Bvarset
: case Bvarset
+1: case Bvarset
+2: case Bvarset
+3:
375 case Bvarset
+4: case Bvarset
+5:
378 Fset (vectorp
[op
], POP
);
389 case Bvarbind
: case Bvarbind
+1: case Bvarbind
+2: case Bvarbind
+3:
390 case Bvarbind
+4: case Bvarbind
+5:
393 specbind (vectorp
[op
], POP
);
404 case Bcall
: case Bcall
+1: case Bcall
+2: case Bcall
+3:
405 case Bcall
+4: case Bcall
+5:
409 #ifdef BYTE_CODE_METER
410 if (byte_metering_on
&& XTYPE (TOP
) == Lisp_Symbol
)
413 v2
= Fget (v1
, Qbyte_code_meter
);
414 if (XTYPE (v2
) == Lisp_Int
415 && XINT (v2
) != ((1<<VALBITS
)-1))
417 XSETINT (v2
, XINT (v2
) + 1);
418 Fput (v1
, Qbyte_code_meter
, v2
);
422 TOP
= Ffuncall (op
+ 1, &TOP
);
433 case Bunbind
: case Bunbind
+1: case Bunbind
+2: case Bunbind
+3:
434 case Bunbind
+4: case Bunbind
+5:
437 unbind_to (specpdl_ptr
- specpdl
- op
, Qnil
);
441 /* To unbind back to the beginning of this frame. Not used yet,
442 but will be needed for tail-recursion elimination. */
443 unbind_to (count
, Qnil
);
448 op
= FETCH2
; /* pc = FETCH2 loses since FETCH2 contains pc++ */
449 pc
= XSTRING (string_saved
)->data
+ op
;
457 pc
= XSTRING (string_saved
)->data
+ op
;
466 pc
= XSTRING (string_saved
)->data
+ op
;
470 case Bgotoifnilelsepop
:
475 pc
= XSTRING (string_saved
)->data
+ op
;
480 case Bgotoifnonnilelsepop
:
485 pc
= XSTRING (string_saved
)->data
+ op
;
513 case BRgotoifnilelsepop
:
523 case BRgotoifnonnilelsepop
:
547 PUSH (vectorp
[FETCH2
]);
550 case Bsave_excursion
:
551 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
554 case Bsave_window_excursion
:
555 TOP
= Fsave_window_excursion (TOP
);
558 case Bsave_restriction
:
559 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
564 TOP
= internal_catch (TOP
, Feval
, v1
);
567 case Bunwind_protect
:
568 record_unwind_protect (0, POP
);
569 (specpdl_ptr
- 1)->symbol
= Qnil
;
572 case Bcondition_case
:
574 v1
= Fcons (POP
, v1
);
575 TOP
= Fcondition_case (Fcons (TOP
, v1
));
578 case Btemp_output_buffer_setup
:
579 temp_output_buffer_setup (XSTRING (TOP
)->data
);
580 TOP
= Vstandard_output
;
583 case Btemp_output_buffer_show
:
585 temp_output_buffer_show (TOP
);
587 /* pop binding of standard-output */
588 unbind_to (specpdl_ptr
- specpdl
- 1, Qnil
);
595 CHECK_NUMBER (v2
, 0);
601 v1
= XCONS (v1
)->cdr
;
605 v1
= wrong_type_argument (Qlistp
, v1
);
614 TOP
= XTYPE (TOP
) == Lisp_Symbol
? Qt
: Qnil
;
618 TOP
= CONSP (TOP
) ? Qt
: Qnil
;
622 TOP
= XTYPE (TOP
) == Lisp_String
? Qt
: Qnil
;
626 TOP
= CONSP (TOP
) || NILP (TOP
) ? Qt
: Qnil
;
631 TOP
= EQ (v1
, TOP
) ? Qt
: Qnil
;
636 TOP
= Fmemq (TOP
, v1
);
640 TOP
= NILP (TOP
) ? Qt
: Qnil
;
646 if (CONSP (v1
)) TOP
= XCONS (v1
)->car
;
647 else if (NILP (v1
)) TOP
= Qnil
;
648 else Fcar (wrong_type_argument (Qlistp
, v1
));
653 if (CONSP (v1
)) TOP
= XCONS (v1
)->cdr
;
654 else if (NILP (v1
)) TOP
= Qnil
;
655 else Fcdr (wrong_type_argument (Qlistp
, v1
));
660 TOP
= Fcons (TOP
, v1
);
664 TOP
= Fcons (TOP
, Qnil
);
669 TOP
= Fcons (TOP
, Fcons (v1
, Qnil
));
674 TOP
= Flist (3, &TOP
);
679 TOP
= Flist (4, &TOP
);
685 TOP
= Flist (op
, &TOP
);
694 TOP
= Faref (TOP
, v1
);
699 TOP
= Faset (TOP
, v1
, v2
);
703 TOP
= Fsymbol_value (TOP
);
706 case Bsymbol_function
:
707 TOP
= Fsymbol_function (TOP
);
712 TOP
= Fset (TOP
, v1
);
717 TOP
= Ffset (TOP
, v1
);
722 TOP
= Fget (TOP
, v1
);
727 TOP
= Fsubstring (TOP
, v1
, v2
);
732 TOP
= Fconcat (2, &TOP
);
737 TOP
= Fconcat (3, &TOP
);
742 TOP
= Fconcat (4, &TOP
);
748 TOP
= Fconcat (op
, &TOP
);
753 if (XTYPE (v1
) == Lisp_Int
)
755 XSETINT (v1
, XINT (v1
) - 1);
764 if (XTYPE (v1
) == Lisp_Int
)
766 XSETINT (v1
, XINT (v1
) + 1);
775 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v1
, 0);
776 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v2
, 0);
777 TOP
= (XFLOATINT (v1
) == XFLOATINT (v2
)) ? Qt
: Qnil
;
782 TOP
= Fgtr (TOP
, v1
);
787 TOP
= Flss (TOP
, v1
);
792 TOP
= Fleq (TOP
, v1
);
797 TOP
= Fgeq (TOP
, v1
);
802 TOP
= Fminus (2, &TOP
);
807 if (XTYPE (v1
) == Lisp_Int
)
809 XSETINT (v1
, - XINT (v1
));
813 TOP
= Fminus (1, &TOP
);
818 TOP
= Fplus (2, &TOP
);
823 TOP
= Fmax (2, &TOP
);
828 TOP
= Fmin (2, &TOP
);
833 TOP
= Ftimes (2, &TOP
);
838 TOP
= Fquo (2, &TOP
);
843 TOP
= Frem (TOP
, v1
);
847 XFASTINT (v1
) = point
;
852 TOP
= Fgoto_char (TOP
);
856 TOP
= Finsert (1, &TOP
);
862 TOP
= Finsert (op
, &TOP
);
871 XFASTINT (v1
) = BEGV
;
876 TOP
= Fchar_after (TOP
);
879 case Bfollowing_char
:
880 XFASTINT (v1
) = PT
== ZV
? 0 : FETCH_CHAR (point
);
884 case Bpreceding_char
:
885 XFASTINT (v1
) = point
<= BEGV
? 0 : FETCH_CHAR (point
- 1);
889 case Bcurrent_column
:
890 XFASTINT (v1
) = current_column ();
895 TOP
= Findent_to (TOP
, Qnil
);
914 case Bcurrent_buffer
:
915 PUSH (Fcurrent_buffer ());
919 TOP
= Fset_buffer (TOP
);
923 PUSH (Fread_char ());
928 PUSH (Finteractive_p ());
932 TOP
= Fforward_char (TOP
);
936 TOP
= Fforward_word (TOP
);
939 case Bskip_chars_forward
:
941 TOP
= Fskip_chars_forward (TOP
, v1
);
944 case Bskip_chars_backward
:
946 TOP
= Fskip_chars_backward (TOP
, v1
);
950 TOP
= Fforward_line (TOP
);
954 CHECK_NUMBER (TOP
, 0);
955 XFASTINT (TOP
) = syntax_code_spec
[(int) SYNTAX (0xFF & XINT (TOP
))];
958 case Bbuffer_substring
:
960 TOP
= Fbuffer_substring (TOP
, v1
);
965 TOP
= Fdelete_region (TOP
, v1
);
968 case Bnarrow_to_region
:
970 TOP
= Fnarrow_to_region (TOP
, v1
);
978 TOP
= Fend_of_line (TOP
);
984 TOP
= Fset_marker (TOP
, v2
, v1
);
987 case Bmatch_beginning
:
988 TOP
= Fmatch_beginning (TOP
);
992 TOP
= Fmatch_end (TOP
);
1000 TOP
= Fdowncase (TOP
);
1003 case Bstringeqlsign
:
1005 TOP
= Fstring_equal (TOP
, v1
);
1010 TOP
= Fstring_lessp (TOP
, v1
);
1015 TOP
= Fequal (TOP
, v1
);
1020 TOP
= Fnthcdr (TOP
, v1
);
1024 if (XTYPE (TOP
) == Lisp_Cons
)
1026 /* Exchange args and then do nth. */
1032 TOP
= Felt (TOP
, v1
);
1037 TOP
= Fmember (TOP
, v1
);
1042 TOP
= Fassq (TOP
, v1
);
1046 TOP
= Fnreverse (TOP
);
1051 TOP
= Fsetcar (TOP
, v1
);
1056 TOP
= Fsetcdr (TOP
, v1
);
1061 if (XTYPE (v1
) == Lisp_Cons
)
1062 TOP
= XCONS (v1
)->car
;
1069 if (XTYPE (v1
) == Lisp_Cons
)
1070 TOP
= XCONS (v1
)->cdr
;
1077 TOP
= Fnconc (2, &TOP
);
1081 TOP
= (NUMBERP (TOP
) ? Qt
: Qnil
);
1085 TOP
= XTYPE (TOP
) == Lisp_Int
? Qt
: Qnil
;
1088 #ifdef BYTE_CODE_SAFE
1090 error ("set-mark is an obsolete bytecode");
1093 error ("scan-buffer is an obsolete bytecode");
1096 error ("mark is an obsolete bytecode");
1101 #ifdef BYTE_CODE_SAFE
1103 error ("unknown bytecode %d (byte compiler bug)", op
);
1104 if ((op
-= Bconstant
) >= const_length
)
1105 error ("no constant number %d (byte compiler bug)", op
);
1108 PUSH (vectorp
[op
- Bconstant
]);
1115 /* Binds and unbinds are supposed to be compiled balanced. */
1116 if (specpdl_ptr
- specpdl
!= count
)
1117 #ifdef BYTE_CODE_SAFE
1118 error ("binding stack not balanced (serious byte compiler bug)");
1127 Qbytecode
= intern ("byte-code");
1128 staticpro (&Qbytecode
);
1130 defsubr (&Sbyte_code
);
1132 #ifdef BYTE_CODE_METER
1134 DEFVAR_LISP ("byte-code-meter", &Vbyte_code_meter
,
1135 "A vector of vectors which holds a histogram of byte-code usage.\n\
1136 (aref (aref byte-code-meter 0) CODE) indicates how many times the byte\n\
1137 opcode CODE has been executed.\n\
1138 (aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,\n\
1139 indicates how many times the byte opcodes CODE1 and CODE2 have been\n\
1140 executed in succession.");
1141 DEFVAR_BOOL ("byte-metering-on", &byte_metering_on
,
1142 "If non-nil, keep profiling information on byte code usage.\n\
1143 The variable byte-code-meter indicates how often each byte opcode is used.\n\
1144 If a symbol has a property named `byte-code-meter' whose value is an\n\
1145 integer, it is incremented each time that symbol's function is called.");
1147 byte_metering_on
= 0;
1148 Vbyte_code_meter
= Fmake_vector (make_number (256), make_number (0));
1149 Qbyte_code_meter
= intern ("byte-code-meter");
1150 staticpro (&Qbyte_code_meter
);
1154 XVECTOR (Vbyte_code_meter
)->contents
[i
] =
1155 Fmake_vector (make_number (256), make_number (0));