1 /* Execution of byte code produced by bytecomp.el.
2 Implementation of compiled-function objects.
3 Copyright (C) 1992, 1993 Free Software Foundation, Inc.
5 This file is part of SXEmacs
7 SXEmacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 SXEmacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Synched up with: Mule 2.0, FSF 19.30. */
23 /* This file has been Mule-ized. */
29 hacked on by jwz@jwz.org 1991-06
30 o added a compile-time switch to turn on simple sanity checking;
31 o put back the obsolete byte-codes for error-detection;
32 o added a new instruction, unbind_all, which I will use for
33 tail-recursion elimination;
34 o made temp_output_buffer_show be called with the right number
36 o made the new bytecodes be called with args in the right order;
37 o added metering support.
40 o added relative jump instructions;
41 o all conditionals now only do QUIT if they jump.
43 Ben Wing: some changes for Mule, 1995-06.
45 Martin Buchholz: performance hacking, 1998-09.
46 See Internals Manual, Evaluation.
51 #include "backtrace.h"
58 EXFUN(Ffetch_bytecode, 1);
60 Lisp_Object Qbyte_code, Qcompiled_functionp, Qinvalid_byte_code;
62 enum Opcode { /* Byte codes */
88 Bsymbol_function = 0113,
111 Beq = 0141, /* was Bmark,
112 but no longer generated as of v18 */
118 Bfollowing_char = 0147,
119 Bpreceding_char = 0150,
120 Bcurrent_column = 0151,
122 Bequal = 0153, /* was Bscan_buffer,
123 but no longer generated as of v18 */
128 Bcurrent_buffer = 0160,
130 Bsave_current_buffer = 0162, /* was Bread_char,
131 but no longer generated as of v19 */
132 Bmemq = 0163, /* was Bset_mark,
133 but no longer generated as of v18 */
134 Binteractive_p = 0164, /* Needed since interactive-p takes
136 Bforward_char = 0165,
137 Bforward_word = 0166,
138 Bskip_chars_forward = 0167,
139 Bskip_chars_backward = 0170,
140 Bforward_line = 0171,
142 Bbuffer_substring = 0173,
143 Bdelete_region = 0174,
144 Bnarrow_to_region = 0175,
151 Bgotoifnonnil = 0204,
152 Bgotoifnilelsepop = 0205,
153 Bgotoifnonnilelsepop = 0206,
158 Bsave_excursion = 0212,
159 Bsave_window_excursion = 0213,
160 Bsave_restriction = 0214,
163 Bunwind_protect = 0216,
164 Bcondition_case = 0217,
165 Btemp_output_buffer_setup = 0220,
166 Btemp_output_buffer_show = 0221,
171 Bmatch_beginning = 0224,
176 Bstring_equal = 0230,
177 Bstring_lessp = 0231,
196 BRgotoifnonnil = 0254,
197 BRgotoifnilelsepop = 0255,
198 BRgotoifnonnilelsepop = 0256,
203 Bmember = 0266, /* new in v20 */
204 Bassq = 0267, /* new in v20 */
206 Bcl_macro = 0270, /* only if modules/cl is there */
208 BLAST_BEFORE_THREE_O_O = Bcl_macro,
212 typedef enum Opcode Opcode;
213 typedef unsigned char Opbyte;
215 static void check_opcode(Opcode opcode);
216 static void invalid_byte_code_error(char *error_message, ...);
219 execute_rare_opcode(Lisp_Object *stk, const Opbyte *prg, Opcode opcode)
220 __attribute__((noinline));
222 static Lisp_Object execute_optimized_program(const Opbyte * program,
224 Lisp_Object * constants_data);
226 extern Lisp_Object Qand_rest, Qand_optional;
228 /* Define BYTE_CODE_METER to enable generation of a byte-op usage histogram.
229 This isn't defined in FSF Emacs and isn't defined in XEmacs v19. */
230 /* #define BYTE_CODE_METER */
232 #ifdef BYTE_CODE_METER
234 Lisp_Object Vbyte_code_meter, Qbyte_code_meter;
235 int byte_metering_on;
237 static void meter_code(Opcode prev_opcode, Opcode this_opcode)
239 if (byte_metering_on) {
241 XVECTOR_DATA(XVECTOR_DATA(Vbyte_code_meter)[this_opcode]);
242 p[0] = INT_PLUS1(p[0]);
244 p[prev_opcode] = INT_PLUS1(p[prev_opcode]);
248 #endif /* BYTE_CODE_METER */
250 static Lisp_Object bytecode_nreverse(Lisp_Object list)
252 REGISTER Lisp_Object prev = Qnil;
253 REGISTER Lisp_Object tail = list;
255 while (!NILP(tail)) {
256 REGISTER Lisp_Object next;
266 /* Apply compiled-function object FUN to the NARGS evaluated arguments
267 in ARGS, and return the result of evaluation. */
269 funcall_compiled_function(Lisp_Object fun, int nargs, Lisp_Object args[])
271 /* This function can GC */
272 int speccount = specpdl_depth();
274 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION(fun);
277 if (!OPAQUEP(f->instructions))
278 /* Lazily munge the instructions into a more efficient form */
279 optimize_compiled_function(fun);
281 /* optimize_compiled_function() guaranteed that f->specpdl_depth is
282 the required space on the specbinding stack for binding the args
283 and local variables of fun. So just reserve it once. */
284 SPECPDL_RESERVE(f->specpdl_depth);
287 /* Fmake_byte_code() guaranteed that f->arglist is a valid list
288 containing only non-constant symbols. */
289 LIST_LOOP_3(symbol, f->arglist, tail) {
290 if (EQ(symbol, Qand_rest)) {
293 SPECBIND_FAST_UNSAFE(symbol,
297 } else if (EQ(symbol, Qand_optional))
299 else if (i == nargs && !optional)
300 goto wrong_number_of_arguments;
302 SPECBIND_FAST_UNSAFE(symbol,
304 nargs ? args[i++] : Qnil);
309 goto wrong_number_of_arguments;
315 execute_optimized_program((Opbyte *)
316 XOPAQUE_DATA(f->instructions),
318 XVECTOR_DATA(f->constants));
320 /* The attempt to optimize this by only unbinding variables failed
321 because using buffer-local variables as function parameters
322 leads to specpdl_ptr->func != 0 */
323 /* UNBIND_TO_GCPRO_VARIABLES_ONLY (speccount, value); */
324 UNBIND_TO_GCPRO(speccount, value);
328 wrong_number_of_arguments:
329 /* The actual printed compiled_function object is incomprehensible.
330 Check the backtrace to see if we can get a more meaningful symbol. */
331 if (EQ(fun, indirect_function(*backtrace_list->function, 0)))
332 fun = *backtrace_list->function;
333 return Fsignal(Qwrong_number_of_arguments, list2(fun, make_int(nargs)));
336 /* Read next uint8 from the instruction stream. */
337 #define READ_UINT_1 ((unsigned int) (unsigned char) *program_ptr++)
339 /* Read next uint16 from the instruction stream. */
340 #define READ_UINT_2 \
342 (((unsigned int) (unsigned char) program_ptr[-1]) * 256 + \
343 ((unsigned int) (unsigned char) program_ptr[-2])))
345 /* Read next int8 from the instruction stream. */
346 #define READ_INT_1 ((int) (signed char) *program_ptr++)
348 /* Read next int16 from the instruction stream. */
351 (((int) ( signed char) program_ptr[-1]) * 256 + \
352 ((int) (unsigned char) program_ptr[-2])))
354 /* Read next int8 from instruction stream; don't advance program_pointer */
355 #define PEEK_INT_1 ((int) (signed char) program_ptr[0])
357 /* Read next int16 from instruction stream; don't advance program_pointer */
359 ((((int) ( signed char) program_ptr[1]) * 256) | \
360 ((int) (unsigned char) program_ptr[0]))
362 /* Do relative jumps from the current location.
363 We only do a QUIT if we jump backwards, for efficiency.
364 No infloops without backward jumps! */
365 #define JUMP_RELATIVE(jump) do { \
366 int JR_jump = (jump); \
367 if (JR_jump < 0) QUIT; \
368 program_ptr += JR_jump; \
371 #define JUMP JUMP_RELATIVE (PEEK_INT_2)
372 #define JUMPR JUMP_RELATIVE (PEEK_INT_1)
374 #define JUMP_NEXT ((void) (program_ptr += 2))
375 #define JUMPR_NEXT ((void) (program_ptr += 1))
377 /* Push x onto the execution stack. */
378 #define PUSH(x) (*++stack_ptr = (x))
380 /* Pop a value off the execution stack. */
381 #define POP (*stack_ptr--)
383 /* Discard n values from the execution stack. */
384 #define DISCARD(n) (stack_ptr -= (n))
386 /* Get the value which is at the top of the execution stack,
388 #define TOP (*stack_ptr)
390 /* See comment before the big switch in execute_optimized_program(). */
391 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
394 #define GCPRO_STACK (gcpro1.nvars = stack_ptr - stack_beg)
397 /* The actual interpreter for byte code.
398 This function has been seriously optimized for performance.
399 Don't change the constructs unless you are willing to do
400 real benchmarking and profiling work -- martin */
403 execute_optimized_program(const Opbyte *program,
404 int stack_depth, Lisp_Object *constants_data)
406 /* This function can GC */
407 REGISTER const Opbyte *program_ptr = program;
408 /* C99 here we come */
409 Lisp_Object stack_beg[stack_depth + 1];
410 REGISTER Lisp_Object *stack_ptr = stack_beg;
411 int speccount = specpdl_depth();
414 #ifdef BYTE_CODE_METER
415 Opcode this_opcode = 0;
419 #ifdef ERROR_CHECK_BYTE_CODE
420 Lisp_Object *stack_end = stack_beg + stack_depth;
423 /* We used to GCPRO the whole interpreter stack before entering this while
424 loop (21.5.14 and before), but that interferes with collection of weakly
425 referenced objects. Although strictly speaking there's no promise that
426 weak references will disappear by any given point in time, they should
427 be collected at the first opportunity. Waiting until exit from the
428 function caused test failures because "stale" objects "above" the top of
429 the stack were still GCPROed, and they were not getting collected until
430 after exit from the (byte-compiled) test!
432 Now the idea is to dynamically adjust the array of GCPROed objects to
433 include only the "active" region of the stack.
435 We use the "GCPRO1 the array base and set the nvars member" method. It
436 would be slightly inefficient but correct to use GCPRO1_ARRAY here. It
437 would just redundantly set nvars.
438 #### Maybe it would be clearer to use GCPRO1_ARRAY and do GCPRO_STACK
441 GCPRO_STACK is something of a misnomer, because it suggests that a
442 struct gcpro is initialized each time. This is false; only the nvars
443 member of a single struct gcpro is being adjusted. This works because
444 each time a new object is assigned to a stack location, the old object
445 loses its reference and is effectively UNGCPROed, and the new object is
446 automatically GCPROed as long as nvars is correct. Only when we
447 return from the interpreter do we need to finalize the struct gcpro
448 itself, and that's done at case Breturn.
450 GCPRO1 (stack_ptr[1]);
453 REGISTER Opcode opcode = READ_UINT_1;
455 /* Get nvars right before maybe signaling. */
457 #ifdef ERROR_CHECK_BYTE_CODE
458 if (stack_ptr > stack_end)
459 invalid_byte_code_error("byte code stack overflow");
460 if (stack_ptr < stack_beg)
461 invalid_byte_code_error("byte code stack underflow");
462 check_opcode(opcode);
465 #ifdef BYTE_CODE_METER
466 prev_opcode = this_opcode;
467 this_opcode = opcode;
468 meter_code(prev_opcode, this_opcode);
471 switch ((unsigned int)opcode) {
475 if (opcode >= Bconstant)
476 PUSH(constants_data[opcode - Bconstant]);
478 stack_ptr = execute_rare_opcode(
479 stack_ptr, program_ptr, opcode);
488 n = opcode - Bvarref;
494 n = READ_UINT_1; /* most common */
497 Lisp_Object symbol = constants_data[n];
498 Lisp_Object value = XSYMBOL(symbol)->value;
499 if (SYMBOL_VALUE_MAGIC_P(value))
500 value = Fsymbol_value(symbol);
511 n = opcode - Bvarset;
517 n = READ_UINT_1; /* most common */
520 Lisp_Object symbol = constants_data[n];
521 Lisp_Symbol *symbol_ptr = XSYMBOL(symbol);
522 Lisp_Object old_value = symbol_ptr->value;
523 Lisp_Object new_value = POP;
524 if (!SYMBOL_VALUE_MAGIC_P(old_value)
525 || UNBOUNDP(old_value))
526 symbol_ptr->value = new_value;
528 Fset(symbol, new_value);
538 n = opcode - Bvarbind;
544 n = READ_UINT_1; /* most common */
547 Lisp_Object symbol = constants_data[n];
548 Lisp_Symbol *symbol_ptr = XSYMBOL(symbol);
549 Lisp_Object old_value = symbol_ptr->value;
550 Lisp_Object new_value = POP;
551 if (!SYMBOL_VALUE_MAGIC_P(old_value)
552 || UNBOUNDP(old_value)) {
553 specpdl_ptr->symbol = symbol;
554 specpdl_ptr->old_value = old_value;
555 specpdl_ptr->func = 0;
557 specpdl_depth_counter++;
559 symbol_ptr->value = new_value;
561 #ifdef ERROR_CHECK_CATCH
562 check_specbind_stack_sanity ();
565 specbind_magic(symbol, new_value);
578 n = (opcode < Bcall + 6 ? opcode - Bcall :
579 opcode == Bcall + 6 ? READ_UINT_1 : READ_UINT_2);
581 #ifdef BYTE_CODE_METER
582 if (byte_metering_on && SYMBOLP(TOP)) {
584 Fget(TOP, Qbyte_code_meter, Qnil);
586 Fput(TOP, Qbyte_code_meter,
587 make_int(XINT(val) + 1));
590 TOP = Ffuncall(n + 1, &TOP);
601 UNBIND_TO(specpdl_depth() -
602 (opcode < Bunbind + 6 ? opcode - Bunbind :
604 Bunbind + 6 ? READ_UINT_1 : READ_UINT_2));
625 case Bgotoifnilelsepop:
634 case Bgotoifnonnilelsepop:
661 case BRgotoifnilelsepop:
670 case BRgotoifnonnilelsepop:
681 #ifdef ERROR_CHECK_BYTE_CODE
682 /* Binds and unbinds are supposed to be compiled balanced. */
683 if (specpdl_depth() != speccount)
684 invalid_byte_code_error
685 ("unbalanced specbinding stack");
695 Lisp_Object arg = TOP;
701 PUSH(constants_data[READ_UINT_2]);
705 TOP = CONSP(TOP) ? XCAR(TOP) : Fcar(TOP);
709 TOP = CONSP(TOP) ? XCDR(TOP) : Fcdr(TOP);
713 /* To unbind back to the beginning of this frame. Not
714 used yet, but will be needed for tail-recursion
716 unbind_to(speccount, Qnil);
720 Lisp_Object arg = POP;
721 TOP = Fcar(Fnthcdr(TOP, arg));
726 TOP = SYMBOLP(TOP) ? Qt : Qnil;
730 TOP = CONSP(TOP) ? Qt : Qnil;
734 TOP = STRINGP(TOP) ? Qt : Qnil;
738 TOP = LISTP(TOP) ? Qt : Qnil;
742 TOP = NUMBERP(TOP) ? Qt : Qnil;
746 #if defined HAVE_MPZ && (defined WITH_GMP || defined WITH_MP)
747 TOP = INTEGERP(TOP) ? Qt : Qnil;
749 TOP = INTP(TOP) ? Qt : Qnil;
754 Lisp_Object arg = POP;
755 TOP = EQ_WITH_EBOLA_NOTICE(TOP, arg) ? Qt : Qnil;
760 TOP = NILP(TOP) ? Qt : Qnil;
764 Lisp_Object arg = POP;
765 TOP = Fcons(TOP, arg);
770 TOP = Fcons(TOP, Qnil);
781 n = opcode - (Blist1 - 1);
784 Lisp_Object list = Qnil;
786 list = Fcons(TOP, list);
798 n = opcode - (Bconcat2 - 2);
806 TOP = Fconcat(n, &TOP);
814 Lisp_Object arg2 = POP;
815 Lisp_Object arg1 = POP;
816 TOP = Faset(TOP, arg1, arg2);
821 TOP = Fsymbol_value(TOP);
824 case Bsymbol_function:
825 TOP = Fsymbol_function(TOP);
829 Lisp_Object arg = POP;
830 TOP = Fget(TOP, arg, Qnil);
843 Lisp_Object arg = POP;
844 if (ent_binrel(ASE_BINARY_REL_EQUALP, TOP, arg))
852 Lisp_Object arg = POP;
853 if (ent_binrel(ASE_BINARY_REL_GREATERP, TOP, arg))
861 Lisp_Object arg = POP;
862 if (ent_binrel(ASE_BINARY_REL_LESSP, TOP, arg))
870 Lisp_Object arg = POP;
871 if (ent_binrel2(ASE_BINARY_REL_LESSP,
872 ASE_BINARY_REL_EQUALP, TOP, arg))
880 Lisp_Object arg = POP;
881 if (ent_binrel2(ASE_BINARY_REL_GREATERP,
882 ASE_BINARY_REL_EQUALP, TOP, arg))
890 TOP = ent_unop_neg(TOP);
895 TOP = bytecode_nconc2(&TOP);
899 Lisp_Object arg = POP;
900 TOP = ent_binop(ASE_BINARY_OP_SUM, TOP, arg);
904 Lisp_Object arg = POP;
905 TOP = ent_binop(ASE_BINARY_OP_DIFF, TOP, arg);
909 Lisp_Object arg = POP;
910 TOP = ent_binop(ASE_BINARY_OP_PROD, TOP, arg);
914 Lisp_Object arg = POP;
915 TOP = ent_binop(ASE_BINARY_OP_DIV, TOP, arg);
919 Lisp_Object arg = POP;
920 if (!ent_binrel(ASE_BINARY_REL_GREATERP, TOP, arg))
925 Lisp_Object arg = POP;
926 if (!ent_binrel(ASE_BINARY_REL_LESSP, TOP, arg))
932 PUSH(make_int(BUF_PT(current_buffer)));
936 TOP = Finsert(1, &TOP);
942 TOP = Finsert(n, &TOP);
946 Lisp_Object arg = POP;
947 TOP = Faref(TOP, arg);
952 Lisp_Object arg = POP;
953 TOP = Fmemq(TOP, arg);
958 Lisp_Object arg = POP;
959 TOP = Fset(TOP, arg);
964 Lisp_Object arg = POP;
965 TOP = Fequal(TOP, arg);
970 Lisp_Object arg = POP;
971 TOP = Fnthcdr(TOP, arg);
976 Lisp_Object arg = POP;
977 TOP = Felt(TOP, arg);
982 Lisp_Object arg = POP;
983 TOP = Fmember(TOP, arg);
988 TOP = Fgoto_char(TOP, Qnil);
991 case Bcurrent_buffer: {
993 XSETBUFFER(buffer, current_buffer);
999 TOP = Fset_buffer(TOP);
1003 PUSH(make_int(BUF_ZV(current_buffer)));
1007 PUSH(make_int(BUF_BEGV(current_buffer)));
1010 case Bskip_chars_forward: {
1011 Lisp_Object arg = POP;
1012 TOP = Fskip_chars_forward(TOP, arg, Qnil);
1017 Lisp_Object arg = POP;
1018 TOP = Fassq(TOP, arg);
1023 Lisp_Object arg = POP;
1024 TOP = Fsetcar(TOP, arg);
1029 Lisp_Object arg = POP;
1030 TOP = Fsetcdr(TOP, arg);
1035 TOP = bytecode_nreverse(TOP);
1039 TOP = CONSP(TOP) ? XCAR(TOP) : Qnil;
1043 TOP = CONSP(TOP) ? XCDR(TOP) : Qnil;
1047 Lisp_Object op = TOP;
1048 Lisp_Object orig_fun, fun;
1051 orig_fun = fun = XCAR(op);
1053 if (SYMBOLP (fun) && !EQ(fun, Qunbound) &&
1054 (fun = XSYMBOL(fun)->function, SYMBOLP(fun)))
1055 fun = indirect_function(fun, 1);
1056 if (SUBRP(fun) && XSUBR(fun)->max_args == UNEVALLED) {
1057 Lisp_Object(*subr)(Lisp_Object) =
1058 (Lisp_Object(*)(Lisp_Object))
1059 subr_function(XSUBR(fun));
1062 fprintf(stderr, "Uh-oh!\nSuicide?\n");
1064 fprintf(stderr, "YESSSSSS!\n");
1067 fprintf(stderr, "tomorrow maybe\n");
1075 /* It makes a worthwhile performance difference (5%) to shunt
1076 lesser-used opcodes off to a subroutine, to keep the switch in
1077 execute_optimized_program small. If you REALLY care about
1078 performance, you want to keep your heavily executed code away from
1079 rarely executed code, to minimize cache misses.
1081 Don't make this function static, since then the compiler might inline it.
1083 How about __attribute__((noinline)) then? -hrop */
1085 execute_rare_opcode(Lisp_Object *stack_ptr,
1086 const Opbyte *program_ptr, Opcode opcode)
1088 switch ((unsigned int)opcode) {
1090 case Bsave_excursion:
1091 record_unwind_protect(save_excursion_restore,
1092 save_excursion_save());
1095 case Bsave_window_excursion: {
1096 int count = specpdl_depth();
1097 record_unwind_protect(save_window_excursion_unwind,
1098 Fcurrent_window_configuration
1101 unbind_to(count, Qnil);
1105 case Bsave_restriction:
1106 record_unwind_protect(save_restriction_restore,
1107 save_restriction_save());
1111 Lisp_Object arg = POP;
1112 TOP = internal_catch(TOP, Feval, arg, 0);
1116 case Bskip_chars_backward: {
1117 Lisp_Object arg = POP;
1118 TOP = Fskip_chars_backward(TOP, arg, Qnil);
1122 case Bunwind_protect:
1123 record_unwind_protect(Fprogn, POP);
1126 case Bcondition_case: {
1127 Lisp_Object arg2 = POP; /* handlers */
1128 Lisp_Object arg1 = POP; /* bodyform */
1129 TOP = condition_case_3(arg1, TOP, arg2);
1134 Lisp_Object arg2 = POP;
1135 Lisp_Object arg1 = POP;
1136 TOP = Fset_marker(TOP, arg1, arg2);
1141 Lisp_Object arg = POP;
1142 TOP = ent_binop(ASE_BINARY_OP_REM, TOP, arg);
1146 case Bmatch_beginning:
1147 TOP = Fmatch_beginning(TOP);
1151 TOP = Fmatch_end(TOP);
1155 TOP = Fupcase(TOP, Qnil);
1159 TOP = Fdowncase(TOP, Qnil);
1163 Lisp_Object arg = POP;
1164 TOP = Ffset(TOP, arg);
1168 case Bstring_equal: {
1169 Lisp_Object arg = POP;
1170 TOP = Fstring_equal(TOP, arg);
1174 case Bstring_lessp: {
1175 Lisp_Object arg = POP;
1176 TOP = Fstring_lessp(TOP, arg);
1181 Lisp_Object arg2 = POP;
1182 Lisp_Object arg1 = POP;
1183 TOP = Fsubstring(TOP, arg1, arg2);
1187 case Bcurrent_column:
1188 PUSH(make_int(current_column(current_buffer)));
1192 TOP = Fchar_after(TOP, Qnil);
1196 TOP = Findent_to(TOP, Qnil, Qnil);
1203 case Bfollowing_char:
1204 PUSH(Ffollowing_char(Qnil));
1207 case Bpreceding_char:
1208 PUSH(Fpreceding_char(Qnil));
1227 case Bsave_current_buffer:
1228 record_unwind_protect(save_current_buffer_restore,
1232 case Binteractive_p:
1233 PUSH(Finteractive_p());
1237 TOP = Fforward_char(TOP, Qnil);
1241 TOP = Fforward_word(TOP, Qnil);
1245 TOP = Fforward_line(TOP, Qnil);
1249 TOP = Fchar_syntax(TOP, Qnil);
1252 case Bbuffer_substring: {
1253 Lisp_Object arg = POP;
1254 TOP = Fbuffer_substring(TOP, arg, Qnil);
1258 case Bdelete_region: {
1259 Lisp_Object arg = POP;
1260 TOP = Fdelete_region(TOP, arg, Qnil);
1264 case Bnarrow_to_region: {
1265 Lisp_Object arg = POP;
1266 TOP = Fnarrow_to_region(TOP, arg, Qnil);
1271 TOP = Fend_of_line(TOP, Qnil);
1274 case Btemp_output_buffer_setup:
1275 temp_output_buffer_setup(TOP);
1276 TOP = Vstandard_output;
1279 case Btemp_output_buffer_show: {
1280 Lisp_Object arg = POP;
1281 temp_output_buffer_show(TOP, Qnil);
1284 /* pop binding of standard-output */
1285 unbind_to(specpdl_depth() - 1, Qnil);
1291 Lisp_Object arg = POP;
1292 TOP = HACKEQ_UNSAFE(TOP, arg) ? Qt : Qnil;
1297 Lisp_Object arg = POP;
1298 TOP = Fold_memq(TOP, arg);
1303 Lisp_Object arg = POP;
1304 TOP = Fold_equal(TOP, arg);
1309 Lisp_Object arg = POP;
1310 TOP = Fold_member(TOP, arg);
1315 Lisp_Object arg = POP;
1316 TOP = Fold_assq(TOP, arg);
1328 static void invalid_byte_code_error(char *error_message, ...)
1332 int maxsz = strlen(error_message) + 128;
1333 char *buf = alloca_array(char, maxsz);
1335 int sz=snprintf(buf, maxsz, "%s", error_message);
1336 assert(sz>=0 && sz<maxsz);
1337 va_start(args, error_message);
1338 obj = emacs_doprnt_string_va((const Bufbyte *)GETTEXT(buf), Qnil, -1,
1342 signal_error(Qinvalid_byte_code, list1(obj));
1345 /* Check for valid opcodes. Change this when adding new opcodes. */
1346 static void check_opcode(Opcode opcode)
1348 if ((opcode < Bvarref) ||
1350 (opcode > BLAST_BEFORE_THREE_O_O && opcode < Bconstant))
1351 invalid_byte_code_error
1352 ("invalid opcode %d in instruction stream", opcode);
1355 /* Check that IDX is a valid offset into the `constants' vector */
1356 static void check_constants_index(int idx, Lisp_Object constants)
1358 if (idx < 0 || idx >= XVECTOR_LENGTH(constants))
1359 invalid_byte_code_error
1360 ("reference %d to constants array out of range 0, %d",
1361 idx, XVECTOR_LENGTH(constants) - 1);
1364 /* Get next character from Lisp instructions string. */
1365 #define READ_INSTRUCTION_CHAR(lvalue) do { \
1366 (lvalue) = charptr_emchar (ptr); \
1367 INC_CHARPTR (ptr); \
1368 *icounts_ptr++ = program_ptr - program; \
1369 if (lvalue > UCHAR_MAX) \
1370 invalid_byte_code_error \
1371 ("Invalid character %c in byte code string"); \
1374 /* Get opcode from Lisp instructions string. */
1375 #define READ_OPCODE do { \
1377 READ_INSTRUCTION_CHAR (c); \
1378 opcode = (Opcode) c; \
1381 /* Get next operand, a uint8, from Lisp instructions string. */
1382 #define READ_OPERAND_1 do { \
1383 READ_INSTRUCTION_CHAR (arg); \
1387 /* Get next operand, a uint16, from Lisp instructions string. */
1388 #define READ_OPERAND_2 do { \
1389 unsigned int arg1, arg2; \
1390 READ_INSTRUCTION_CHAR (arg1); \
1391 READ_INSTRUCTION_CHAR (arg2); \
1392 arg = arg1 + (arg2 << 8); \
1396 /* Write 1 byte to PTR, incrementing PTR */
1397 #define WRITE_INT8(value, ptr) do { \
1398 *((ptr)++) = (value); \
1401 /* Write 2 bytes to PTR, incrementing PTR */
1402 #define WRITE_INT16(value, ptr) do { \
1403 WRITE_INT8 (((unsigned) (value)) & 0x00ff, (ptr)); \
1404 WRITE_INT8 (((unsigned) (value)) >> 8 , (ptr)); \
1407 /* We've changed our minds about the opcode we've already written. */
1408 #define REWRITE_OPCODE(new_opcode) ((void) (program_ptr[-1] = new_opcode))
1410 /* Encode an op arg within the opcode, or as a 1 or 2-byte operand. */
1411 #define WRITE_NARGS(base_opcode) do { \
1414 REWRITE_OPCODE (base_opcode + arg); \
1416 else if (arg <= UCHAR_MAX) \
1418 REWRITE_OPCODE (base_opcode + 6); \
1419 WRITE_INT8 (arg, program_ptr); \
1423 REWRITE_OPCODE (base_opcode + 7); \
1424 WRITE_INT16 (arg, program_ptr); \
1428 /* Encode a constants reference within the opcode, or as a 2-byte operand. */
1429 #define WRITE_CONSTANT do { \
1430 check_constants_index(arg, constants); \
1431 if (arg <= UCHAR_MAX - Bconstant) \
1433 REWRITE_OPCODE (Bconstant + arg); \
1437 REWRITE_OPCODE (Bconstant2); \
1438 WRITE_INT16 (arg, program_ptr); \
1442 #define WRITE_OPCODE WRITE_INT8 (opcode, program_ptr)
1444 /* Compile byte code instructions into free space provided by caller, with
1445 size >= (2 * string_char_length (instructions) + 1) * sizeof (Opbyte).
1446 Returns length of compiled code. */
1447 static void optimize_byte_code(
1449 Lisp_Object instructions,
1450 Lisp_Object constants,
1452 Opbyte * const program,
1453 int *const program_length,
1454 int *const varbind_count)
1456 size_t instructions_length = XSTRING_LENGTH(instructions);
1457 size_t comfy_size = 2 * instructions_length;
1459 int *const icounts = alloca_array(int, comfy_size);
1460 int *icounts_ptr = icounts;
1462 /* We maintain a table of jumps in the source code. */
1467 struct jump *const jumps = alloca_array(struct jump, comfy_size);
1468 struct jump *jumps_ptr = jumps;
1470 Opbyte *program_ptr = program;
1472 /* const means constant! */
1473 Bufbyte *ptr = XSTRING_DATA(instructions);
1474 const Bufbyte *const end = ptr + instructions_length;
1485 switch ((unsigned int)opcode) {
1500 arg = opcode - Bvarref;
1502 check_constants_index(arg, constants);
1503 val = XVECTOR_DATA(constants)[arg];
1505 invalid_byte_code_error
1506 ("variable reference to non-symbol %S",
1508 if (EQ(val, Qnil) || EQ(val, Qt)
1509 || (SYMBOL_IS_KEYWORD(val)))
1510 invalid_byte_code_error
1511 ("variable reference to constant symbol %s",
1512 string_data(XSYMBOL(val)->name));
1513 WRITE_NARGS(Bvarref);
1528 arg = opcode - Bvarset;
1530 check_constants_index(arg, constants);
1531 val = XVECTOR_DATA(constants)[arg];
1533 invalid_byte_code_error
1534 ("attempt to set non-symbol %S", val);
1535 if (EQ(val, Qnil) || EQ(val, Qt))
1536 invalid_byte_code_error
1537 ("attempt to set constant symbol %s",
1538 string_data(XSYMBOL(val)->name));
1539 /* Ignore assignments to keywords by converting to Bdiscard.
1540 For backward compatibility only - we'd like to make this an error. */
1541 if (SYMBOL_IS_KEYWORD(val))
1542 REWRITE_OPCODE(Bdiscard);
1544 WRITE_NARGS(Bvarset);
1559 arg = opcode - Bvarbind;
1562 check_constants_index(arg, constants);
1563 val = XVECTOR_DATA(constants)[arg];
1565 invalid_byte_code_error
1566 ("attempt to let-bind non-symbol %S", val);
1567 if (EQ(val, Qnil) || EQ(val, Qt)
1568 || (SYMBOL_IS_KEYWORD(val)))
1569 invalid_byte_code_error
1570 ("attempt to let-bind constant symbol %s",
1571 string_data(XSYMBOL(val)->name));
1572 WRITE_NARGS(Bvarbind);
1587 arg = opcode - Bcall;
1604 arg = opcode - Bunbind;
1606 WRITE_NARGS(Bunbind);
1612 case Bgotoifnilelsepop:
1613 case Bgotoifnonnilelsepop:
1615 /* Make program_ptr-relative */
1616 arg += icounts - (icounts_ptr - argsize);
1621 case BRgotoifnonnil:
1622 case BRgotoifnilelsepop:
1623 case BRgotoifnonnilelsepop:
1625 /* Make program_ptr-relative */
1628 /* Record program-relative goto addresses in `jumps' table */
1629 jumps_ptr->from = icounts_ptr - icounts - argsize;
1630 jumps_ptr->to = jumps_ptr->from + arg;
1632 if (arg >= -1 && arg <= argsize)
1633 invalid_byte_code_error
1634 ("goto instruction is its own target");
1635 if (arg <= SCHAR_MIN || arg > SCHAR_MAX) {
1637 REWRITE_OPCODE(opcode + Bgoto - BRgoto);
1638 WRITE_INT16(arg, program_ptr);
1641 REWRITE_OPCODE(opcode + BRgoto - Bgoto);
1642 WRITE_INT8(arg, program_ptr);
1655 WRITE_INT8(arg, program_ptr);
1659 if (opcode < Bconstant)
1660 check_opcode(opcode);
1662 arg = opcode - Bconstant;
1669 /* Fix up jumps table to refer to NEW offsets. */
1670 for (struct jump *j = jumps; j < jumps_ptr; j++) {
1671 #ifdef ERROR_CHECK_BYTE_CODE
1672 assert(j->from < icounts_ptr - icounts);
1673 assert(j->to < icounts_ptr - icounts);
1675 j->from = icounts[j->from];
1676 j->to = icounts[j->to];
1677 #ifdef ERROR_CHECK_BYTE_CODE
1678 assert(j->from < program_ptr - program);
1679 assert(j->to < program_ptr - program);
1680 check_opcode((Opcode) (program[j->from - 1]));
1682 check_opcode((Opcode) (program[j->to]));
1685 /* Fixup jumps in byte-code until no more fixups needed */
1686 for (bool more_fixups_needed = true; more_fixups_needed; ) {
1689 /* assume we don't need more hiccups */
1690 more_fixups_needed = false;
1691 for (j = jumps; j < jumps_ptr; j++) {
1694 int jump = to - from;
1695 Opbyte *p = program + from;
1696 Opcode opcode = (Opcode)p[-1];
1698 if (!more_fixups_needed) {
1699 check_opcode((Opcode) p[jump]);
1701 assert(to >= 0 && program + to < program_ptr);
1703 switch ((unsigned int)opcode) {
1707 case Bgotoifnilelsepop:
1708 case Bgotoifnonnilelsepop:
1709 WRITE_INT16(jump, p);
1714 case BRgotoifnonnil:
1715 case BRgotoifnilelsepop:
1716 case BRgotoifnonnilelsepop:
1717 if (jump > SCHAR_MIN && jump <= SCHAR_MAX) {
1718 WRITE_INT8(jump, p);
1723 for (jj = jumps; jj < jumps_ptr; jj++) {
1725 program_ptr - program);
1727 program_ptr - program);
1728 if (jj->from > from) {
1731 if (jj->to > from) {
1735 p[-1] += Bgoto - BRgoto;
1736 more_fixups_needed = true;
1737 memmove(p + 1, p, program_ptr++ - p);
1738 WRITE_INT16(jump, p);
1749 /* *program_ptr++ = 0; */
1750 *program_length = program_ptr - program;
1753 /* Optimize the byte code and store the optimized program, only
1754 understood by bytecode.c, in an opaque object in the
1755 instructions slot of the Compiled_Function object. */
1756 void optimize_compiled_function(Lisp_Object compiled_function)
1758 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION(compiled_function);
1763 /* If we have not actually read the bytecode string
1764 and constants vector yet, fetch them from the file. */
1765 if (CONSP(f->instructions))
1766 Ffetch_bytecode(compiled_function);
1768 if (STRINGP(f->instructions)) {
1769 /* XSTRING_LENGTH() is more efficient than XSTRING_CHAR_LENGTH(),
1770 which would be slightly more `proper' */
1772 alloca_array(Opbyte,
1773 1 + 2 * XSTRING_LENGTH(f->instructions));
1774 optimize_byte_code(f->instructions, f->constants, program,
1775 &program_length, &varbind_count);
1776 f->specpdl_depth = XINT(Flength(f->arglist)) + varbind_count;
1778 make_opaque(program, program_length * sizeof(Opbyte));
1781 assert(OPAQUEP(f->instructions));
1784 /************************************************************************/
1785 /* The compiled-function object type */
1786 /************************************************************************/
1788 print_compiled_function(Lisp_Object obj, Lisp_Object printcharfun,
1791 /* This function can GC */
1792 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION(obj); /* GC doesn't relocate */
1793 int docp = f->flags.documentationp;
1794 int intp = f->flags.interactivep;
1795 struct gcpro gcpro1, gcpro2;
1796 GCPRO2(obj, printcharfun);
1798 write_c_string(print_readably ? "#[" : "#<compiled-function ",
1800 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
1801 if (!print_readably) {
1802 Lisp_Object ann = compiled_function_annotation(f);
1804 write_c_string("(from ", printcharfun);
1805 print_internal(ann, printcharfun, 1);
1806 write_c_string(") ", printcharfun);
1809 #endif /* COMPILED_FUNCTION_ANNOTATION_HACK */
1810 /* COMPILED_ARGLIST = 0 */
1811 print_internal(compiled_function_arglist(f), printcharfun, escapeflag);
1813 /* COMPILED_INSTRUCTIONS = 1 */
1814 write_c_string(" ", printcharfun);
1816 struct gcpro ngcpro1;
1817 Lisp_Object instructions = compiled_function_instructions(f);
1818 NGCPRO1(instructions);
1819 if (STRINGP(instructions) && !print_readably) {
1820 /* We don't usually want to see that junk in the bytecode. */
1821 write_fmt_str(printcharfun, "\"...(%ld)\"",
1822 (long)XSTRING_CHAR_LENGTH(instructions));
1824 print_internal(instructions, printcharfun, escapeflag);
1828 /* COMPILED_CONSTANTS = 2 */
1829 write_c_string(" ", printcharfun);
1830 print_internal(compiled_function_constants(f), printcharfun,
1833 /* COMPILED_STACK_DEPTH = 3 */
1834 write_fmt_str(printcharfun, " %d", compiled_function_stack_depth(f));
1836 /* COMPILED_DOC_STRING = 4 */
1838 write_c_string(" ", printcharfun);
1839 print_internal(compiled_function_documentation(f), printcharfun,
1843 /* COMPILED_INTERACTIVE = 5 */
1845 write_c_string(" ", printcharfun);
1846 print_internal(compiled_function_interactive(f), printcharfun,
1851 write_c_string(print_readably ? "]" : ">", printcharfun);
1854 static Lisp_Object mark_compiled_function(Lisp_Object obj)
1856 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION(obj);
1858 mark_object(f->instructions);
1859 mark_object(f->arglist);
1860 mark_object(f->doc_and_interactive);
1861 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
1862 mark_object(f->annotated);
1864 /* tail-recurse on constants */
1865 return f->constants;
1869 compiled_function_equal(Lisp_Object obj1, Lisp_Object obj2, int depth)
1871 Lisp_Compiled_Function *f1 = XCOMPILED_FUNCTION(obj1);
1872 Lisp_Compiled_Function *f2 = XCOMPILED_FUNCTION(obj2);
1873 return (f1->flags.documentationp == f2->flags.documentationp && f1->flags.interactivep == f2->flags.interactivep && f1->flags.domainp == f2->flags.domainp && /* I18N3 */
1874 internal_equal(compiled_function_instructions(f1),
1875 compiled_function_instructions(f2), depth + 1) &&
1876 internal_equal(f1->constants, f2->constants, depth + 1) &&
1877 internal_equal(f1->arglist, f2->arglist, depth + 1) &&
1878 internal_equal(f1->doc_and_interactive,
1879 f2->doc_and_interactive, depth + 1));
1882 static unsigned long compiled_function_hash(Lisp_Object obj, int depth)
1884 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION(obj);
1885 return HASH3((f->flags.documentationp << 2) +
1886 (f->flags.interactivep << 1) +
1888 internal_hash(f->instructions, depth + 1),
1889 internal_hash(f->constants, depth + 1));
1892 static const struct lrecord_description compiled_function_description[] = {
1893 {XD_LISP_OBJECT, offsetof(Lisp_Compiled_Function, instructions)},
1894 {XD_LISP_OBJECT, offsetof(Lisp_Compiled_Function, constants)},
1895 {XD_LISP_OBJECT, offsetof(Lisp_Compiled_Function, arglist)},
1896 {XD_LISP_OBJECT, offsetof(Lisp_Compiled_Function, doc_and_interactive)},
1897 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
1898 {XD_LISP_OBJECT, offsetof(Lisp_Compiled_Function, annotated)},
1903 DEFINE_BASIC_LRECORD_IMPLEMENTATION("compiled-function", compiled_function,
1904 mark_compiled_function,
1905 print_compiled_function, 0,
1906 compiled_function_equal,
1907 compiled_function_hash,
1908 compiled_function_description,
1909 Lisp_Compiled_Function);
1911 DEFUN("compiled-function-p", Fcompiled_function_p, 1, 1, 0, /*
1912 Return t if OBJECT is a byte-compiled function object.
1916 return COMPILED_FUNCTIONP(object) ? Qt : Qnil;
1919 /************************************************************************/
1920 /* compiled-function object accessor functions */
1921 /************************************************************************/
1923 Lisp_Object compiled_function_arglist(Lisp_Compiled_Function * f)
1928 Lisp_Object compiled_function_instructions(Lisp_Compiled_Function * f)
1930 if (!OPAQUEP(f->instructions))
1931 return f->instructions;
1934 /* Invert action performed by optimize_byte_code() */
1935 Lisp_Opaque *opaque = XOPAQUE(f->instructions);
1937 Bufbyte *const buffer =
1938 alloca_array(Bufbyte, OPAQUE_SIZE(opaque) * MAX_EMCHAR_LEN);
1939 Bufbyte *bp = buffer;
1941 const Opbyte *const program =
1942 (const Opbyte *)OPAQUE_DATA(opaque);
1943 const Opbyte *program_ptr = program;
1944 const Opbyte *const program_end =
1945 program_ptr + OPAQUE_SIZE(opaque);
1947 while (program_ptr < program_end) {
1948 Opcode opcode = (Opcode) READ_UINT_1;
1949 bp += set_charptr_emchar(bp, opcode);
1951 switch ((unsigned int)opcode) {
1958 bp += set_charptr_emchar(bp, READ_UINT_1);
1959 bp += set_charptr_emchar(bp, READ_UINT_1);
1970 bp += set_charptr_emchar(bp, READ_UINT_1);
1976 case Bgotoifnilelsepop:
1977 case Bgotoifnonnilelsepop:
1979 int jump = READ_INT_2;
1981 Opbyte *buf2p = buf2;
1982 /* Convert back to program-relative address */
1984 (program_ptr - 2 - program),
1986 bp += set_charptr_emchar(bp, buf2[0]);
1987 bp += set_charptr_emchar(bp, buf2[1]);
1993 case BRgotoifnonnil:
1994 case BRgotoifnilelsepop:
1995 case BRgotoifnonnilelsepop:
1996 bp += set_charptr_emchar(bp, READ_INT_1 + 127);
2003 return make_string(buffer, bp - buffer);
2007 Lisp_Object compiled_function_constants(Lisp_Compiled_Function * f)
2009 return f->constants;
2012 int compiled_function_stack_depth(Lisp_Compiled_Function * f)
2014 return f->stack_depth;
2017 /* The compiled_function->doc_and_interactive slot uses the minimal
2018 number of conses, based on compiled_function->flags; it may take
2019 any of the following forms:
2026 (interactive . domain)
2027 (doc . (interactive . domain))
2030 /* Caller must check flags.interactivep first */
2031 Lisp_Object compiled_function_interactive(Lisp_Compiled_Function * f)
2033 assert(f->flags.interactivep);
2034 if (f->flags.documentationp && f->flags.domainp)
2035 return XCAR(XCDR(f->doc_and_interactive));
2036 else if (f->flags.documentationp)
2037 return XCDR(f->doc_and_interactive);
2038 else if (f->flags.domainp)
2039 return XCAR(f->doc_and_interactive);
2041 return f->doc_and_interactive;
2044 /* Caller need not check flags.documentationp first */
2045 Lisp_Object compiled_function_documentation(Lisp_Compiled_Function * f)
2047 if (!f->flags.documentationp)
2049 else if (f->flags.interactivep && f->flags.domainp)
2050 return XCAR(f->doc_and_interactive);
2051 else if (f->flags.interactivep)
2052 return XCAR(f->doc_and_interactive);
2053 else if (f->flags.domainp)
2054 return XCAR(f->doc_and_interactive);
2056 return f->doc_and_interactive;
2059 /* Caller need not check flags.domainp first */
2060 Lisp_Object compiled_function_domain(Lisp_Compiled_Function * f)
2062 if (!f->flags.domainp)
2064 else if (f->flags.documentationp && f->flags.interactivep)
2065 return XCDR(XCDR(f->doc_and_interactive));
2066 else if (f->flags.documentationp)
2067 return XCDR(f->doc_and_interactive);
2068 else if (f->flags.interactivep)
2069 return XCDR(f->doc_and_interactive);
2071 return f->doc_and_interactive;
2074 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
2076 Lisp_Object compiled_function_annotation(Lisp_Compiled_Function * f)
2078 return f->annotated;
2083 /* used only by Snarf-documentation; there must be doc already. */
2085 set_compiled_function_documentation(Lisp_Compiled_Function * f,
2086 Lisp_Object new_doc)
2088 assert(f->flags.documentationp);
2089 assert(INTP(new_doc) || STRINGP(new_doc));
2091 if (f->flags.interactivep && f->flags.domainp)
2092 XCAR(f->doc_and_interactive) = new_doc;
2093 else if (f->flags.interactivep)
2094 XCAR(f->doc_and_interactive) = new_doc;
2095 else if (f->flags.domainp)
2096 XCAR(f->doc_and_interactive) = new_doc;
2098 f->doc_and_interactive = new_doc;
2101 DEFUN("compiled-function-arglist", Fcompiled_function_arglist, 1, 1, 0, /*
2102 Return the argument list of the compiled-function object FUNCTION.
2106 CHECK_COMPILED_FUNCTION(function);
2107 return compiled_function_arglist(XCOMPILED_FUNCTION(function));
2110 DEFUN("compiled-function-instructions", Fcompiled_function_instructions, 1, 1, 0, /*
2111 Return the byte-opcode string of the compiled-function object FUNCTION.
2115 CHECK_COMPILED_FUNCTION(function);
2116 return compiled_function_instructions(XCOMPILED_FUNCTION(function));
2119 DEFUN("compiled-function-constants", Fcompiled_function_constants, 1, 1, 0, /*
2120 Return the constants vector of the compiled-function object FUNCTION.
2124 CHECK_COMPILED_FUNCTION(function);
2125 return compiled_function_constants(XCOMPILED_FUNCTION(function));
2128 DEFUN("compiled-function-stack-depth", Fcompiled_function_stack_depth, 1, 1, 0, /*
2129 Return the maximum stack depth of the compiled-function object FUNCTION.
2133 CHECK_COMPILED_FUNCTION(function);
2135 make_int(compiled_function_stack_depth
2136 (XCOMPILED_FUNCTION(function)));
2139 DEFUN("compiled-function-doc-string", Fcompiled_function_doc_string, 1, 1, 0, /*
2140 Return the doc string of the compiled-function object FUNCTION, if available.
2141 Functions that had their doc strings snarfed into the DOC file will have
2142 an integer returned instead of a string.
2146 CHECK_COMPILED_FUNCTION(function);
2147 return compiled_function_documentation(XCOMPILED_FUNCTION(function));
2150 DEFUN("compiled-function-interactive", Fcompiled_function_interactive, 1, 1, 0, /*
2151 Return the interactive spec of the compiled-function object FUNCTION, or nil.
2152 If non-nil, the return value will be a list whose first element is
2153 `interactive' and whose second element is the interactive spec.
2157 CHECK_COMPILED_FUNCTION(function);
2158 return XCOMPILED_FUNCTION(function)->flags.interactivep
2159 ? list2(Qinteractive,
2160 compiled_function_interactive(XCOMPILED_FUNCTION(function)))
2164 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
2166 /* Remove the `xx' if you wish to restore this feature */
2167 xxDEFUN("compiled-function-annotation", Fcompiled_function_annotation, 1, 1, 0, /*
2168 Return the annotation of the compiled-function object FUNCTION, or nil.
2169 The annotation is a piece of information indicating where this
2170 compiled-function object came from. Generally this will be
2171 a symbol naming a function; or a string naming a file, if the
2172 compiled-function object was not defined in a function; or nil,
2173 if the compiled-function object was not created as a result of
2177 CHECK_COMPILED_FUNCTION(function);
2178 return compiled_function_annotation(XCOMPILED_FUNCTION(function));
2181 #endif /* COMPILED_FUNCTION_ANNOTATION_HACK */
2183 DEFUN("compiled-function-domain", Fcompiled_function_domain, 1, 1, 0, /*
2184 Return the domain of the compiled-function object FUNCTION, or nil.
2185 This is only meaningful if I18N3 was enabled when emacs was compiled.
2189 CHECK_COMPILED_FUNCTION(function);
2190 return XCOMPILED_FUNCTION(function)->flags.domainp
2191 ? compiled_function_domain(XCOMPILED_FUNCTION(function))
2195 DEFUN("fetch-bytecode", Ffetch_bytecode, 1, 1, 0, /*
2196 If the byte code for compiled function FUNCTION is lazy-loaded, fetch it now.
2200 Lisp_Compiled_Function *f;
2201 CHECK_COMPILED_FUNCTION(function);
2202 f = XCOMPILED_FUNCTION(function);
2204 if (OPAQUEP(f->instructions) || STRINGP(f->instructions))
2207 if (CONSP(f->instructions)) {
2208 Lisp_Object tem = read_doc_string(f->instructions);
2210 signal_simple_error("Invalid lazy-loaded byte code",
2212 /* v18 or v19 bytecode file. Need to Ebolify. */
2213 if (f->flags.ebolified && VECTORP(XCDR(tem)))
2214 ebolify_bytecode_constants(XCDR(tem));
2215 f->instructions = XCAR(tem);
2216 f->constants = XCDR(tem);
2220 return Qnil; /* not reached */
2223 DEFUN("optimize-compiled-function", Foptimize_compiled_function, 1, 1, 0, /*
2224 Convert compiled function FUNCTION into an optimized internal form.
2228 Lisp_Compiled_Function *f;
2229 CHECK_COMPILED_FUNCTION(function);
2230 f = XCOMPILED_FUNCTION(function);
2232 if (OPAQUEP(f->instructions)) /* Already optimized? */
2235 optimize_compiled_function(function);
2239 DEFUN("byte-code", Fbyte_code, 3, 3, 0, /*
2240 Function used internally in byte-compiled code.
2241 First argument INSTRUCTIONS is a string of byte code.
2242 Second argument CONSTANTS is a vector of constants.
2243 Third argument STACK-DEPTH is the maximum stack depth used in this function.
2244 If STACK-DEPTH is incorrect, Emacs may crash.
2246 (instructions, constants, stack_depth))
2248 /* This function can GC */
2253 CHECK_STRING(instructions);
2254 CHECK_VECTOR(constants);
2255 CHECK_NATNUM(stack_depth);
2257 /* Optimize the `instructions' string, just like when executing a
2258 regular compiled function, but don't save it for later since this is
2259 likely to only be executed once. */
2260 program = alloca_array(Opbyte, 1 + 2 * XSTRING_LENGTH(instructions));
2261 optimize_byte_code(instructions, constants, program,
2262 &program_length, &varbind_count);
2263 SPECPDL_RESERVE(varbind_count);
2264 return execute_optimized_program(program,
2266 XVECTOR_DATA(constants));
2269 void syms_of_bytecode(void)
2271 INIT_LRECORD_IMPLEMENTATION(compiled_function);
2273 DEFERROR_STANDARD(Qinvalid_byte_code, Qinvalid_state);
2274 defsymbol(&Qbyte_code, "byte-code");
2275 defsymbol(&Qcompiled_functionp, "compiled-function-p");
2277 DEFSUBR(Fbyte_code);
2278 DEFSUBR(Ffetch_bytecode);
2279 DEFSUBR(Foptimize_compiled_function);
2281 DEFSUBR(Fcompiled_function_p);
2282 DEFSUBR(Fcompiled_function_instructions);
2283 DEFSUBR(Fcompiled_function_constants);
2284 DEFSUBR(Fcompiled_function_stack_depth);
2285 DEFSUBR(Fcompiled_function_arglist);
2286 DEFSUBR(Fcompiled_function_interactive);
2287 DEFSUBR(Fcompiled_function_doc_string);
2288 DEFSUBR(Fcompiled_function_domain);
2289 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
2290 DEFSUBR(Fcompiled_function_annotation);
2293 #ifdef BYTE_CODE_METER
2294 defsymbol(&Qbyte_code_meter, "byte-code-meter");
2298 void vars_of_bytecode(void)
2300 #ifdef BYTE_CODE_METER
2302 DEFVAR_LISP("byte-code-meter", &Vbyte_code_meter /*
2303 A vector of vectors which holds a histogram of byte code usage.
2304 \(aref (aref byte-code-meter 0) CODE) indicates how many times the byte
2305 opcode CODE has been executed.
2306 \(aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,
2307 indicates how many times the byte opcodes CODE1 and CODE2 have been
2308 executed in succession.
2310 DEFVAR_BOOL("byte-metering-on", &byte_metering_on /*
2311 If non-nil, keep profiling information on byte code usage.
2312 The variable `byte-code-meter' indicates how often each byte opcode is used.
2313 If a symbol has a property named `byte-code-meter' whose value is an
2314 integer, it is incremented each time that symbol's function is called.
2317 byte_metering_on = 0;
2318 Vbyte_code_meter = make_vector(256, Qzero);
2322 XVECTOR_DATA(Vbyte_code_meter)[i] =
2323 make_vector(256, Qzero);
2325 #endif /* BYTE_CODE_METER */