Partially sync files.el from XEmacs 21.5 for wildcard support.
[sxemacs] / src / bytecode.c
1 /* Execution of byte code produced by bytecomp.el.
2    Implementation of compiled-function objects.
3    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
4
5 This file is part of SXEmacs
6
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.
11
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.
16
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/>. */
19
20
21 /* Synched up with: Mule 2.0, FSF 19.30. */
22
23 /* This file has been Mule-ized. */
24
25 /* Authorship:
26
27    FSF: long ago.
28
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
35      of args;
36   o  made the new bytecodes be called with args in the right order;
37   o  added metering support.
38
39 by Hallvard:
40   o  added relative jump instructions;
41   o  all conditionals now only do QUIT if they jump.
42
43    Ben Wing: some changes for Mule, 1995-06.
44
45    Martin Buchholz: performance hacking, 1998-09.
46    See Internals Manual, Evaluation.
47  */
48
49 #include <config.h>
50 #include "lisp.h"
51 #include "backtrace.h"
52 #include "buffer.h"
53 #include "bytecode.h"
54 #include "opaque.h"
55 #include "syntax.h"
56 #include "ent/ent.h"
57
58 EXFUN(Ffetch_bytecode, 1);
59
60 Lisp_Object Qbyte_code, Qcompiled_functionp, Qinvalid_byte_code;
61
62 enum Opcode {                   /* Byte codes */
63         Bvarref = 010,
64         Bvarset = 020,
65         Bvarbind = 030,
66         Bcall = 040,
67         Bunbind = 050,
68
69         Bnth = 070,
70         Bsymbolp = 071,
71         Bconsp = 072,
72         Bstringp = 073,
73         Blistp = 074,
74         Bold_eq = 075,
75         Bold_memq = 076,
76         Bnot = 077,
77         Bcar = 0100,
78         Bcdr = 0101,
79         Bcons = 0102,
80         Blist1 = 0103,
81         Blist2 = 0104,
82         Blist3 = 0105,
83         Blist4 = 0106,
84         Blength = 0107,
85         Baref = 0110,
86         Baset = 0111,
87         Bsymbol_value = 0112,
88         Bsymbol_function = 0113,
89         Bset = 0114,
90         Bfset = 0115,
91         Bget = 0116,
92         Bsubstring = 0117,
93         Bconcat2 = 0120,
94         Bconcat3 = 0121,
95         Bconcat4 = 0122,
96         Bsub1 = 0123,
97         Badd1 = 0124,
98         Beqlsign = 0125,
99         Bgtr = 0126,
100         Blss = 0127,
101         Bleq = 0130,
102         Bgeq = 0131,
103         Bdiff = 0132,
104         Bnegate = 0133,
105         Bplus = 0134,
106         Bmax = 0135,
107         Bmin = 0136,
108         Bmult = 0137,
109
110         Bpoint = 0140,
111         Beq = 0141,             /* was Bmark,
112                                    but no longer generated as of v18 */
113         Bgoto_char = 0142,
114         Binsert = 0143,
115         Bpoint_max = 0144,
116         Bpoint_min = 0145,
117         Bchar_after = 0146,
118         Bfollowing_char = 0147,
119         Bpreceding_char = 0150,
120         Bcurrent_column = 0151,
121         Bindent_to = 0152,
122         Bequal = 0153,          /* was Bscan_buffer,
123                                    but no longer generated as of v18 */
124         Beolp = 0154,
125         Beobp = 0155,
126         Bbolp = 0156,
127         Bbobp = 0157,
128         Bcurrent_buffer = 0160,
129         Bset_buffer = 0161,
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
135                                    unevalled args */
136         Bforward_char = 0165,
137         Bforward_word = 0166,
138         Bskip_chars_forward = 0167,
139         Bskip_chars_backward = 0170,
140         Bforward_line = 0171,
141         Bchar_syntax = 0172,
142         Bbuffer_substring = 0173,
143         Bdelete_region = 0174,
144         Bnarrow_to_region = 0175,
145         Bwiden = 0176,
146         Bend_of_line = 0177,
147
148         Bconstant2 = 0201,
149         Bgoto = 0202,
150         Bgotoifnil = 0203,
151         Bgotoifnonnil = 0204,
152         Bgotoifnilelsepop = 0205,
153         Bgotoifnonnilelsepop = 0206,
154         Breturn = 0207,
155         Bdiscard = 0210,
156         Bdup = 0211,
157
158         Bsave_excursion = 0212,
159         Bsave_window_excursion = 0213,
160         Bsave_restriction = 0214,
161         Bcatch = 0215,
162
163         Bunwind_protect = 0216,
164         Bcondition_case = 0217,
165         Btemp_output_buffer_setup = 0220,
166         Btemp_output_buffer_show = 0221,
167
168         Bunbind_all = 0222,
169
170         Bset_marker = 0223,
171         Bmatch_beginning = 0224,
172         Bmatch_end = 0225,
173         Bupcase = 0226,
174         Bdowncase = 0227,
175
176         Bstring_equal = 0230,
177         Bstring_lessp = 0231,
178         Bold_equal = 0232,
179         Bnthcdr = 0233,
180         Belt = 0234,
181         Bold_member = 0235,
182         Bold_assq = 0236,
183         Bnreverse = 0237,
184         Bsetcar = 0240,
185         Bsetcdr = 0241,
186         Bcar_safe = 0242,
187         Bcdr_safe = 0243,
188         Bnconc = 0244,
189         Bquo = 0245,
190         Brem = 0246,
191         Bnumberp = 0247,
192         Bintegerp = 0250,
193
194         BRgoto = 0252,
195         BRgotoifnil = 0253,
196         BRgotoifnonnil = 0254,
197         BRgotoifnilelsepop = 0255,
198         BRgotoifnonnilelsepop = 0256,
199
200         BlistN = 0257,
201         BconcatN = 0260,
202         BinsertN = 0261,
203         Bmember = 0266,         /* new in v20 */
204         Bassq = 0267,           /* new in v20 */
205
206         Bcl_macro = 0270,               /* only if modules/cl is there */
207
208         BLAST_BEFORE_THREE_O_O = Bcl_macro,
209
210         Bconstant = 0300
211 };
212 typedef enum Opcode Opcode;
213 typedef unsigned char Opbyte;
214 \f
215 static void check_opcode(Opcode opcode);
216 static void invalid_byte_code_error(char *error_message, ...);
217
218 static Lisp_Object*
219 execute_rare_opcode(Lisp_Object *stk, const Opbyte *prg, Opcode opcode)
220         __attribute__((noinline));
221
222 static Lisp_Object execute_optimized_program(const Opbyte * program,
223                                              int stack_depth,
224                                              Lisp_Object * constants_data);
225
226 extern Lisp_Object Qand_rest, Qand_optional;
227
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 */
231 \f
232 #ifdef BYTE_CODE_METER
233
234 Lisp_Object Vbyte_code_meter, Qbyte_code_meter;
235 int byte_metering_on;
236
237 static void meter_code(Opcode prev_opcode, Opcode this_opcode)
238 {
239         if (byte_metering_on) {
240                 Lisp_Object *p =
241                     XVECTOR_DATA(XVECTOR_DATA(Vbyte_code_meter)[this_opcode]);
242                 p[0] = INT_PLUS1(p[0]);
243                 if (prev_opcode)
244                         p[prev_opcode] = INT_PLUS1(p[prev_opcode]);
245         }
246 }
247
248 #endif                          /* BYTE_CODE_METER */
249 \f
250 static Lisp_Object bytecode_nreverse(Lisp_Object list)
251 {
252         REGISTER Lisp_Object prev = Qnil;
253         REGISTER Lisp_Object tail = list;
254
255         while (!NILP(tail)) {
256                 REGISTER Lisp_Object next;
257                 CHECK_CONS(tail);
258                 next = XCDR(tail);
259                 XCDR(tail) = prev;
260                 prev = tail;
261                 tail = next;
262         }
263         return prev;
264 }
265
266 /* Apply compiled-function object FUN to the NARGS evaluated arguments
267    in ARGS, and return the result of evaluation. */
268 Lisp_Object
269 funcall_compiled_function(Lisp_Object fun, int nargs, Lisp_Object args[])
270 {
271         /* This function can GC */
272         int speccount = specpdl_depth();
273         REGISTER int i = 0;
274         Lisp_Compiled_Function *f = XCOMPILED_FUNCTION(fun);
275         int optional = 0;
276
277         if (!OPAQUEP(f->instructions))
278                 /* Lazily munge the instructions into a more efficient form */
279                 optimize_compiled_function(fun);
280
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);
285
286         {
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)) {
291                                 tail = XCDR(tail);
292                                 symbol = XCAR(tail);
293                                 SPECBIND_FAST_UNSAFE(symbol,
294                                                      Flist(nargs - i,
295                                                            &args[i]));
296                                 goto run_code;
297                         } else if (EQ(symbol, Qand_optional))
298                                 optional = 1;
299                         else if (i == nargs && !optional)
300                                 goto wrong_number_of_arguments;
301                         else
302                                 SPECBIND_FAST_UNSAFE(symbol,
303                                                      i <
304                                                      nargs ? args[i++] : Qnil);
305                 }
306         }
307
308         if (i < nargs)
309                 goto wrong_number_of_arguments;
310
311       run_code:
312
313         {
314                 Lisp_Object value =
315                     execute_optimized_program((Opbyte *)
316                                               XOPAQUE_DATA(f->instructions),
317                                               f->stack_depth,
318                                               XVECTOR_DATA(f->constants));
319
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);
325                 return value;
326         }
327
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)));
334 }
335 \f
336 /* Read next uint8 from the instruction stream. */
337 #define READ_UINT_1 ((unsigned int) (unsigned char) *program_ptr++)
338
339 /* Read next uint16 from the instruction stream. */
340 #define READ_UINT_2                                             \
341   (program_ptr += 2,                                            \
342    (((unsigned int) (unsigned char) program_ptr[-1]) * 256 +    \
343     ((unsigned int) (unsigned char) program_ptr[-2])))
344
345 /* Read next int8 from the instruction stream. */
346 #define READ_INT_1 ((int) (signed char) *program_ptr++)
347
348 /* Read next int16 from the instruction stream. */
349 #define READ_INT_2                                      \
350   (program_ptr += 2,                                    \
351    (((int) (  signed char) program_ptr[-1]) * 256 +     \
352     ((int) (unsigned char) program_ptr[-2])))
353
354 /* Read next int8 from instruction stream; don't advance program_pointer */
355 #define PEEK_INT_1 ((int) (signed char) program_ptr[0])
356
357 /* Read next int16 from instruction stream; don't advance program_pointer */
358 #define PEEK_INT_2                                      \
359   ((((int) (  signed char) program_ptr[1]) * 256) |     \
360     ((int) (unsigned char) program_ptr[0]))
361
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;               \
369 } while (0)
370
371 #define JUMP  JUMP_RELATIVE (PEEK_INT_2)
372 #define JUMPR JUMP_RELATIVE (PEEK_INT_1)
373
374 #define JUMP_NEXT  ((void) (program_ptr += 2))
375 #define JUMPR_NEXT ((void) (program_ptr += 1))
376
377 /* Push x onto the execution stack. */
378 #define PUSH(x) (*++stack_ptr = (x))
379
380 /* Pop a value off the execution stack. */
381 #define POP (*stack_ptr--)
382
383 /* Discard n values from the execution stack.  */
384 #define DISCARD(n) (stack_ptr -= (n))
385
386 /* Get the value which is at the top of the execution stack,
387    but don't pop it. */
388 #define TOP (*stack_ptr)
389
390 /* See comment before the big switch in execute_optimized_program(). */
391 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
392 #define GCPRO_STACK
393 #else  /* !BDWGC */
394 #define GCPRO_STACK  (gcpro1.nvars = stack_ptr - stack_beg)
395 #endif  /* BDWGC */
396
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 */
401
402 static Lisp_Object
403 execute_optimized_program(const Opbyte *program,
404                           int stack_depth, Lisp_Object *constants_data)
405 {
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();
412         struct gcpro gcpro1;
413
414 #ifdef BYTE_CODE_METER
415         Opcode this_opcode = 0;
416         Opcode prev_opcode;
417 #endif
418
419 #ifdef ERROR_CHECK_BYTE_CODE
420         Lisp_Object *stack_end = stack_beg + stack_depth;
421 #endif
422
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!
431
432            Now the idea is to dynamically adjust the array of GCPROed objects to
433            include only the "active" region of the stack.
434
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
439            after the switch?
440
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.
449         */
450         GCPRO1 (stack_ptr[1]);
451
452         while (1) {
453                 REGISTER Opcode opcode = READ_UINT_1;
454
455                 /* Get nvars right before maybe signaling. */
456                 GCPRO_STACK;
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);
463 #endif
464
465 #ifdef BYTE_CODE_METER
466                 prev_opcode = this_opcode;
467                 this_opcode = opcode;
468                 meter_code(prev_opcode, this_opcode);
469 #endif
470
471                 switch ((unsigned int)opcode) {
472                         REGISTER int n;
473
474                 default:
475                         if (opcode >= Bconstant)
476                                 PUSH(constants_data[opcode - Bconstant]);
477                         else
478                                 stack_ptr = execute_rare_opcode(
479                                         stack_ptr, program_ptr, opcode);
480                         break;
481
482                 case Bvarref:
483                 case Bvarref + 1:
484                 case Bvarref + 2:
485                 case Bvarref + 3:
486                 case Bvarref + 4:
487                 case Bvarref + 5:
488                         n = opcode - Bvarref;
489                         goto do_varref;
490                 case Bvarref + 7:
491                         n = READ_UINT_2;
492                         goto do_varref;
493                 case Bvarref + 6:
494                         n = READ_UINT_1;        /* most common */
495                 do_varref:
496                         {
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);
501                                 PUSH(value);
502                                 break;
503                         }
504
505                 case Bvarset:
506                 case Bvarset + 1:
507                 case Bvarset + 2:
508                 case Bvarset + 3:
509                 case Bvarset + 4:
510                 case Bvarset + 5:
511                         n = opcode - Bvarset;
512                         goto do_varset;
513                 case Bvarset + 7:
514                         n = READ_UINT_2;
515                         goto do_varset;
516                 case Bvarset + 6:
517                         n = READ_UINT_1;        /* most common */
518                 do_varset:
519                         {
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;
527                                 else
528                                         Fset(symbol, new_value);
529                                 break;
530                         }
531
532                 case Bvarbind:
533                 case Bvarbind + 1:
534                 case Bvarbind + 2:
535                 case Bvarbind + 3:
536                 case Bvarbind + 4:
537                 case Bvarbind + 5:
538                         n = opcode - Bvarbind;
539                         goto do_varbind;
540                 case Bvarbind + 7:
541                         n = READ_UINT_2;
542                         goto do_varbind;
543                 case Bvarbind + 6:
544                         n = READ_UINT_1;        /* most common */
545                 do_varbind:
546                         {
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;
556                                         specpdl_ptr++;
557                                         specpdl_depth_counter++;
558
559                                         symbol_ptr->value = new_value;
560
561 #ifdef ERROR_CHECK_CATCH
562                                         check_specbind_stack_sanity ();
563 #endif
564                                 } else
565                                         specbind_magic(symbol, new_value);
566                                 break;
567                         }
568
569
570                 case Bcall:
571                 case Bcall + 1:
572                 case Bcall + 2:
573                 case Bcall + 3:
574                 case Bcall + 4:
575                 case Bcall + 5:
576                 case Bcall + 6:
577                 case Bcall + 7:
578                         n = (opcode < Bcall + 6 ? opcode - Bcall :
579                              opcode == Bcall + 6 ? READ_UINT_1 : READ_UINT_2);
580                         DISCARD(n);
581 #ifdef BYTE_CODE_METER
582                         if (byte_metering_on && SYMBOLP(TOP)) {
583                                 Lisp_Object val =
584                                         Fget(TOP, Qbyte_code_meter, Qnil);
585                                 if (INTP(val))
586                                         Fput(TOP, Qbyte_code_meter,
587                                              make_int(XINT(val) + 1));
588                         }
589 #endif
590                         TOP = Ffuncall(n + 1, &TOP);
591                         break;
592
593                 case Bunbind:
594                 case Bunbind + 1:
595                 case Bunbind + 2:
596                 case Bunbind + 3:
597                 case Bunbind + 4:
598                 case Bunbind + 5:
599                 case Bunbind + 6:
600                 case Bunbind + 7:
601                         UNBIND_TO(specpdl_depth() -
602                                   (opcode < Bunbind + 6 ? opcode - Bunbind :
603                                    opcode ==
604                                    Bunbind + 6 ? READ_UINT_1 : READ_UINT_2));
605                         break;
606
607                 case Bgoto:
608                         JUMP;
609                         break;
610
611                 case Bgotoifnil:
612                         if (NILP(POP))
613                                 JUMP;
614                         else
615                                 JUMP_NEXT;
616                         break;
617
618                 case Bgotoifnonnil:
619                         if (!NILP(POP))
620                                 JUMP;
621                         else
622                                 JUMP_NEXT;
623                         break;
624
625                 case Bgotoifnilelsepop:
626                         if (NILP(TOP))
627                                 JUMP;
628                         else {
629                                 DISCARD(1);
630                                 JUMP_NEXT;
631                         }
632                         break;
633
634                 case Bgotoifnonnilelsepop:
635                         if (!NILP(TOP))
636                                 JUMP;
637                         else {
638                                 DISCARD(1);
639                                 JUMP_NEXT;
640                         }
641                         break;
642
643                 case BRgoto:
644                         JUMPR;
645                         break;
646
647                 case BRgotoifnil:
648                         if (NILP(POP))
649                                 JUMPR;
650                         else
651                                 JUMPR_NEXT;
652                         break;
653
654                 case BRgotoifnonnil:
655                         if (!NILP(POP))
656                                 JUMPR;
657                         else
658                                 JUMPR_NEXT;
659                         break;
660
661                 case BRgotoifnilelsepop:
662                         if (NILP(TOP))
663                                 JUMPR;
664                         else {
665                                 DISCARD(1);
666                                 JUMPR_NEXT;
667                         }
668                         break;
669
670                 case BRgotoifnonnilelsepop:
671                         if (!NILP(TOP))
672                                 JUMPR;
673                         else {
674                                 DISCARD(1);
675                                 JUMPR_NEXT;
676                         }
677                         break;
678
679                 case Breturn:
680                         UNGCPRO;
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");
686 #endif
687                         return TOP;
688
689                 case Bdiscard:
690                         DISCARD(1);
691                         break;
692
693                 case Bdup:
694                 {
695                         Lisp_Object arg = TOP;
696                         PUSH(arg);
697                         break;
698                 }
699
700                 case Bconstant2:
701                         PUSH(constants_data[READ_UINT_2]);
702                         break;
703
704                 case Bcar:
705                         TOP = CONSP(TOP) ? XCAR(TOP) : Fcar(TOP);
706                         break;
707
708                 case Bcdr:
709                         TOP = CONSP(TOP) ? XCDR(TOP) : Fcdr(TOP);
710                         break;
711
712                 case Bunbind_all:
713                         /* To unbind back to the beginning of this frame.  Not
714                            used yet, but will be needed for tail-recursion
715                            elimination. */
716                         unbind_to(speccount, Qnil);
717                         break;
718
719                 case Bnth: {
720                         Lisp_Object arg = POP;
721                         TOP = Fcar(Fnthcdr(TOP, arg));
722                         break;
723                 }
724
725                 case Bsymbolp:
726                         TOP = SYMBOLP(TOP) ? Qt : Qnil;
727                         break;
728
729                 case Bconsp:
730                         TOP = CONSP(TOP) ? Qt : Qnil;
731                         break;
732
733                 case Bstringp:
734                         TOP = STRINGP(TOP) ? Qt : Qnil;
735                         break;
736
737                 case Blistp:
738                         TOP = LISTP(TOP) ? Qt : Qnil;
739                         break;
740
741                 case Bnumberp:
742                         TOP = NUMBERP(TOP) ? Qt : Qnil;
743                         break;
744
745                 case Bintegerp:
746 #if defined HAVE_MPZ && (defined WITH_GMP || defined WITH_MP)
747                         TOP = INTEGERP(TOP) ? Qt : Qnil;
748 #else
749                         TOP = INTP(TOP) ? Qt : Qnil;
750 #endif
751                         break;
752
753                 case Beq: {
754                         Lisp_Object arg = POP;
755                         TOP = EQ_WITH_EBOLA_NOTICE(TOP, arg) ? Qt : Qnil;
756                         break;
757                 }
758
759                 case Bnot:
760                         TOP = NILP(TOP) ? Qt : Qnil;
761                         break;
762
763                 case Bcons: {
764                         Lisp_Object arg = POP;
765                         TOP = Fcons(TOP, arg);
766                         break;
767                 }
768
769                 case Blist1:
770                         TOP = Fcons(TOP, Qnil);
771                         break;
772
773                 case BlistN:
774                         n = READ_UINT_1;
775                         goto do_list;
776
777                 case Blist2:
778                 case Blist3:
779                 case Blist4:
780                         /* common case */
781                         n = opcode - (Blist1 - 1);
782                 do_list:
783                         {
784                                 Lisp_Object list = Qnil;
785                         list_loop:
786                                 list = Fcons(TOP, list);
787                                 if (--n) {
788                                         DISCARD(1);
789                                         goto list_loop;
790                                 }
791                                 TOP = list;
792                                 break;
793                         }
794
795                 case Bconcat2:
796                 case Bconcat3:
797                 case Bconcat4:
798                         n = opcode - (Bconcat2 - 2);
799                         goto do_concat;
800
801                 case BconcatN:
802                         /* common case */
803                         n = READ_UINT_1;
804                 do_concat:
805                         DISCARD(n - 1);
806                         TOP = Fconcat(n, &TOP);
807                         break;
808
809                 case Blength:
810                         TOP = Flength(TOP);
811                         break;
812
813                 case Baset: {
814                         Lisp_Object arg2 = POP;
815                         Lisp_Object arg1 = POP;
816                         TOP = Faset(TOP, arg1, arg2);
817                         break;
818                 }
819
820                 case Bsymbol_value:
821                         TOP = Fsymbol_value(TOP);
822                         break;
823
824                 case Bsymbol_function:
825                         TOP = Fsymbol_function(TOP);
826                         break;
827
828                 case Bget: {
829                         Lisp_Object arg = POP;
830                         TOP = Fget(TOP, arg, Qnil);
831                         break;
832                 }
833
834                 case Bsub1:
835                         TOP = Fsub1(TOP);
836                         break;
837
838                 case Badd1:
839                         TOP = Fadd1(TOP);
840                         break;
841
842                 case Beqlsign: {
843                         Lisp_Object arg = POP;
844                         if (ent_binrel(ASE_BINARY_REL_EQUALP, TOP, arg))
845                                 TOP = Qt;
846                         else
847                                 TOP = Qnil;
848                         break;
849                 }
850
851                 case Bgtr: {
852                         Lisp_Object arg = POP;
853                         if (ent_binrel(ASE_BINARY_REL_GREATERP, TOP, arg))
854                                 TOP = Qt;
855                         else
856                                 TOP = Qnil;
857                         break;
858                 }
859
860                 case Blss: {
861                         Lisp_Object arg = POP;
862                         if (ent_binrel(ASE_BINARY_REL_LESSP, TOP, arg))
863                                 TOP = Qt;
864                         else
865                                 TOP = Qnil;
866                         break;
867                 }
868
869                 case Bleq: {
870                         Lisp_Object arg = POP;
871                         if (ent_binrel2(ASE_BINARY_REL_LESSP,
872                                         ASE_BINARY_REL_EQUALP, TOP, arg))
873                                 TOP = Qt;
874                         else
875                                 TOP = Qnil;
876                         break;
877                 }
878
879                 case Bgeq: {
880                         Lisp_Object arg = POP;
881                         if (ent_binrel2(ASE_BINARY_REL_GREATERP,
882                                         ASE_BINARY_REL_EQUALP, TOP, arg))
883                                 TOP = Qt;
884                         else
885                                 TOP = Qnil;
886                         break;
887                 }
888
889                 case Bnegate:
890                         TOP = ent_unop_neg(TOP);
891                         break;
892
893                 case Bnconc:
894                         DISCARD(1);
895                         TOP = bytecode_nconc2(&TOP);
896                         break;
897
898                 case Bplus: {
899                         Lisp_Object arg = POP;
900                         TOP = ent_binop(ASE_BINARY_OP_SUM, TOP, arg);
901                         break;
902                 }
903                 case Bdiff: {
904                         Lisp_Object arg = POP;
905                         TOP = ent_binop(ASE_BINARY_OP_DIFF, TOP, arg);
906                         break;
907                 }
908                 case Bmult: {
909                         Lisp_Object arg = POP;
910                         TOP = ent_binop(ASE_BINARY_OP_PROD, TOP, arg);
911                         break;
912                 }
913                 case Bquo: {
914                         Lisp_Object arg = POP;
915                         TOP = ent_binop(ASE_BINARY_OP_DIV, TOP, arg);
916                         break;
917                 }
918                 case Bmax: {
919                         Lisp_Object arg = POP;
920                         if (!ent_binrel(ASE_BINARY_REL_GREATERP, TOP, arg))
921                                 TOP = arg;
922                         break;
923                 }
924                 case Bmin: {
925                         Lisp_Object arg = POP;
926                         if (!ent_binrel(ASE_BINARY_REL_LESSP, TOP, arg))
927                                 TOP = arg;
928                         break;
929                 }
930
931                 case Bpoint:
932                         PUSH(make_int(BUF_PT(current_buffer)));
933                         break;
934
935                 case Binsert:
936                         TOP = Finsert(1, &TOP);
937                         break;
938
939                 case BinsertN:
940                         n = READ_UINT_1;
941                         DISCARD(n - 1);
942                         TOP = Finsert(n, &TOP);
943                         break;
944
945                 case Baref: {
946                         Lisp_Object arg = POP;
947                         TOP = Faref(TOP, arg);
948                         break;
949                 }
950
951                 case Bmemq: {
952                         Lisp_Object arg = POP;
953                         TOP = Fmemq(TOP, arg);
954                         break;
955                 }
956
957                 case Bset: {
958                         Lisp_Object arg = POP;
959                         TOP = Fset(TOP, arg);
960                         break;
961                 }
962
963                 case Bequal: {
964                         Lisp_Object arg = POP;
965                         TOP = Fequal(TOP, arg);
966                         break;
967                 }
968
969                 case Bnthcdr: {
970                         Lisp_Object arg = POP;
971                         TOP = Fnthcdr(TOP, arg);
972                         break;
973                 }
974
975                 case Belt: {
976                         Lisp_Object arg = POP;
977                         TOP = Felt(TOP, arg);
978                         break;
979                 }
980
981                 case Bmember: {
982                         Lisp_Object arg = POP;
983                         TOP = Fmember(TOP, arg);
984                         break;
985                 }
986
987                 case Bgoto_char:
988                         TOP = Fgoto_char(TOP, Qnil);
989                         break;
990
991                 case Bcurrent_buffer: {
992                         Lisp_Object buffer;
993                         XSETBUFFER(buffer, current_buffer);
994                         PUSH(buffer);
995                         break;
996                 }
997
998                 case Bset_buffer:
999                         TOP = Fset_buffer(TOP);
1000                         break;
1001
1002                 case Bpoint_max:
1003                         PUSH(make_int(BUF_ZV(current_buffer)));
1004                         break;
1005
1006                 case Bpoint_min:
1007                         PUSH(make_int(BUF_BEGV(current_buffer)));
1008                         break;
1009
1010                 case Bskip_chars_forward: {
1011                         Lisp_Object arg = POP;
1012                         TOP = Fskip_chars_forward(TOP, arg, Qnil);
1013                         break;
1014                 }
1015
1016                 case Bassq: {
1017                         Lisp_Object arg = POP;
1018                         TOP = Fassq(TOP, arg);
1019                         break;
1020                 }
1021
1022                 case Bsetcar: {
1023                         Lisp_Object arg = POP;
1024                         TOP = Fsetcar(TOP, arg);
1025                         break;
1026                 }
1027
1028                 case Bsetcdr: {
1029                         Lisp_Object arg = POP;
1030                         TOP = Fsetcdr(TOP, arg);
1031                         break;
1032                 }
1033
1034                 case Bnreverse:
1035                         TOP = bytecode_nreverse(TOP);
1036                         break;
1037
1038                 case Bcar_safe:
1039                         TOP = CONSP(TOP) ? XCAR(TOP) : Qnil;
1040                         break;
1041
1042                 case Bcdr_safe:
1043                         TOP = CONSP(TOP) ? XCDR(TOP) : Qnil;
1044                         break;
1045
1046                 case Bcl_macro: {
1047                         Lisp_Object op = TOP;
1048                         Lisp_Object orig_fun, fun;
1049                         Lisp_Object args;
1050
1051                         orig_fun = fun = XCAR(op);
1052                         args = XCDR(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));
1060                                 TOP = subr(args);
1061                         } else {
1062                                 fprintf(stderr, "Uh-oh!\nSuicide?\n");
1063                                 if (random() & 1) {
1064                                         fprintf(stderr, "YESSSSSS!\n");
1065                                         abort();
1066                                 }
1067                                 fprintf(stderr, "tomorrow maybe\n");
1068                         }
1069                         break;
1070                 }
1071                 }
1072         }
1073 }
1074
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.
1080
1081    Don't make this function static, since then the compiler might inline it.
1082
1083    How about __attribute__((noinline)) then? -hrop */
1084 static Lisp_Object*
1085 execute_rare_opcode(Lisp_Object *stack_ptr,
1086                     const Opbyte *program_ptr, Opcode opcode)
1087 {
1088         switch ((unsigned int)opcode) {
1089
1090         case Bsave_excursion:
1091                 record_unwind_protect(save_excursion_restore,
1092                                       save_excursion_save());
1093                 break;
1094
1095         case Bsave_window_excursion: {
1096                 int count = specpdl_depth();
1097                 record_unwind_protect(save_window_excursion_unwind,
1098                                       Fcurrent_window_configuration
1099                                       (Qnil));
1100                 TOP = Fprogn(TOP);
1101                 unbind_to(count, Qnil);
1102                 break;
1103         }
1104
1105         case Bsave_restriction:
1106                 record_unwind_protect(save_restriction_restore,
1107                                       save_restriction_save());
1108                 break;
1109
1110         case Bcatch: {
1111                 Lisp_Object arg = POP;
1112                 TOP = internal_catch(TOP, Feval, arg, 0);
1113                 break;
1114         }
1115
1116         case Bskip_chars_backward: {
1117                 Lisp_Object arg = POP;
1118                 TOP = Fskip_chars_backward(TOP, arg, Qnil);
1119                 break;
1120         }
1121
1122         case Bunwind_protect:
1123                 record_unwind_protect(Fprogn, POP);
1124                 break;
1125
1126         case Bcondition_case: {
1127                 Lisp_Object arg2 = POP; /* handlers */
1128                 Lisp_Object arg1 = POP; /* bodyform */
1129                 TOP = condition_case_3(arg1, TOP, arg2);
1130                 break;
1131         }
1132
1133         case Bset_marker: {
1134                 Lisp_Object arg2 = POP;
1135                 Lisp_Object arg1 = POP;
1136                 TOP = Fset_marker(TOP, arg1, arg2);
1137                 break;
1138         }
1139
1140         case Brem: {
1141                 Lisp_Object arg = POP;
1142                 TOP = ent_binop(ASE_BINARY_OP_REM, TOP, arg);
1143                 break;
1144         }
1145
1146         case Bmatch_beginning:
1147                 TOP = Fmatch_beginning(TOP);
1148                 break;
1149
1150         case Bmatch_end:
1151                 TOP = Fmatch_end(TOP);
1152                 break;
1153
1154         case Bupcase:
1155                 TOP = Fupcase(TOP, Qnil);
1156                 break;
1157
1158         case Bdowncase:
1159                 TOP = Fdowncase(TOP, Qnil);
1160                 break;
1161
1162         case Bfset: {
1163                 Lisp_Object arg = POP;
1164                 TOP = Ffset(TOP, arg);
1165                 break;
1166         }
1167
1168         case Bstring_equal: {
1169                 Lisp_Object arg = POP;
1170                 TOP = Fstring_equal(TOP, arg);
1171                 break;
1172         }
1173
1174         case Bstring_lessp: {
1175                 Lisp_Object arg = POP;
1176                 TOP = Fstring_lessp(TOP, arg);
1177                 break;
1178         }
1179
1180         case Bsubstring: {
1181                 Lisp_Object arg2 = POP;
1182                 Lisp_Object arg1 = POP;
1183                 TOP = Fsubstring(TOP, arg1, arg2);
1184                 break;
1185         }
1186
1187         case Bcurrent_column:
1188                 PUSH(make_int(current_column(current_buffer)));
1189                 break;
1190
1191         case Bchar_after:
1192                 TOP = Fchar_after(TOP, Qnil);
1193                 break;
1194
1195         case Bindent_to:
1196                 TOP = Findent_to(TOP, Qnil, Qnil);
1197                 break;
1198
1199         case Bwiden:
1200                 PUSH(Fwiden(Qnil));
1201                 break;
1202
1203         case Bfollowing_char:
1204                 PUSH(Ffollowing_char(Qnil));
1205                 break;
1206
1207         case Bpreceding_char:
1208                 PUSH(Fpreceding_char(Qnil));
1209                 break;
1210
1211         case Beolp:
1212                 PUSH(Feolp(Qnil));
1213                 break;
1214
1215         case Beobp:
1216                 PUSH(Feobp(Qnil));
1217                 break;
1218
1219         case Bbolp:
1220                 PUSH(Fbolp(Qnil));
1221                 break;
1222
1223         case Bbobp:
1224                 PUSH(Fbobp(Qnil));
1225                 break;
1226
1227         case Bsave_current_buffer:
1228                 record_unwind_protect(save_current_buffer_restore,
1229                                       Fcurrent_buffer());
1230                 break;
1231
1232         case Binteractive_p:
1233                 PUSH(Finteractive_p());
1234                 break;
1235
1236         case Bforward_char:
1237                 TOP = Fforward_char(TOP, Qnil);
1238                 break;
1239
1240         case Bforward_word:
1241                 TOP = Fforward_word(TOP, Qnil);
1242                 break;
1243
1244         case Bforward_line:
1245                 TOP = Fforward_line(TOP, Qnil);
1246                 break;
1247
1248         case Bchar_syntax:
1249                 TOP = Fchar_syntax(TOP, Qnil);
1250                 break;
1251
1252         case Bbuffer_substring: {
1253                 Lisp_Object arg = POP;
1254                 TOP = Fbuffer_substring(TOP, arg, Qnil);
1255                 break;
1256         }
1257
1258         case Bdelete_region: {
1259                 Lisp_Object arg = POP;
1260                 TOP = Fdelete_region(TOP, arg, Qnil);
1261                 break;
1262         }
1263
1264         case Bnarrow_to_region: {
1265                 Lisp_Object arg = POP;
1266                 TOP = Fnarrow_to_region(TOP, arg, Qnil);
1267                 break;
1268         }
1269
1270         case Bend_of_line:
1271                 TOP = Fend_of_line(TOP, Qnil);
1272                 break;
1273
1274         case Btemp_output_buffer_setup:
1275                 temp_output_buffer_setup(TOP);
1276                 TOP = Vstandard_output;
1277                 break;
1278
1279         case Btemp_output_buffer_show: {
1280                 Lisp_Object arg = POP;
1281                 temp_output_buffer_show(TOP, Qnil);
1282                 TOP = arg;
1283                 /* GAG ME!! */
1284                 /* pop binding of standard-output */
1285                 unbind_to(specpdl_depth() - 1, Qnil);
1286                 break;
1287         }
1288
1289
1290         case Bold_eq: {
1291                 Lisp_Object arg = POP;
1292                 TOP = HACKEQ_UNSAFE(TOP, arg) ? Qt : Qnil;
1293                 break;
1294         }
1295
1296         case Bold_memq: {
1297                 Lisp_Object arg = POP;
1298                 TOP = Fold_memq(TOP, arg);
1299                 break;
1300         }
1301
1302         case Bold_equal: {
1303                 Lisp_Object arg = POP;
1304                 TOP = Fold_equal(TOP, arg);
1305                 break;
1306         }
1307
1308         case Bold_member: {
1309                 Lisp_Object arg = POP;
1310                 TOP = Fold_member(TOP, arg);
1311                 break;
1312         }
1313
1314         case Bold_assq: {
1315                 Lisp_Object arg = POP;
1316                 TOP = Fold_assq(TOP, arg);
1317                 break;
1318         }
1319
1320         default:
1321                 abort();
1322                 break;
1323         }
1324         return stack_ptr;
1325 }
1326
1327 \f
1328 static void invalid_byte_code_error(char *error_message, ...)
1329 {
1330         Lisp_Object obj;
1331         va_list args;
1332         int maxsz = strlen(error_message) + 128;
1333         char *buf = alloca_array(char, maxsz);
1334
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,
1339                                      args);
1340         va_end(args);
1341
1342         signal_error(Qinvalid_byte_code, list1(obj));
1343 }
1344
1345 /* Check for valid opcodes.  Change this when adding new opcodes.  */
1346 static void check_opcode(Opcode opcode)
1347 {
1348         if ((opcode < Bvarref) ||
1349             (opcode == 0251) ||
1350             (opcode > BLAST_BEFORE_THREE_O_O && opcode < Bconstant))
1351                 invalid_byte_code_error
1352                     ("invalid opcode %d in instruction stream", opcode);
1353 }
1354
1355 /* Check that IDX is a valid offset into the `constants' vector */
1356 static void check_constants_index(int idx, Lisp_Object constants)
1357 {
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);
1362 }
1363
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");     \
1372 } while (0)
1373
1374 /* Get opcode from Lisp instructions string. */
1375 #define READ_OPCODE do {                \
1376   unsigned int c;                       \
1377   READ_INSTRUCTION_CHAR (c);            \
1378   opcode = (Opcode) c;                  \
1379 } while (0)
1380
1381 /* Get next operand, a uint8, from Lisp instructions string. */
1382 #define READ_OPERAND_1 do {             \
1383   READ_INSTRUCTION_CHAR (arg);          \
1384   argsize = 1;                          \
1385 } while (0)
1386
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);             \
1393   argsize = 2;                          \
1394 } while (0)
1395
1396 /* Write 1 byte to PTR, incrementing PTR */
1397 #define WRITE_INT8(value, ptr) do {     \
1398   *((ptr)++) = (value);                 \
1399 } while (0)
1400
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));    \
1405 } while (0)
1406
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))
1409
1410 /* Encode an op arg within the opcode, or as a 1 or 2-byte operand. */
1411 #define WRITE_NARGS(base_opcode) do {           \
1412   if (arg <= 5)                                 \
1413     {                                           \
1414       REWRITE_OPCODE (base_opcode + arg);       \
1415     }                                           \
1416   else if (arg <= UCHAR_MAX)                    \
1417     {                                           \
1418       REWRITE_OPCODE (base_opcode + 6);         \
1419       WRITE_INT8 (arg, program_ptr);            \
1420     }                                           \
1421   else                                          \
1422     {                                           \
1423       REWRITE_OPCODE (base_opcode + 7);         \
1424       WRITE_INT16 (arg, program_ptr);           \
1425     }                                           \
1426 } while (0)
1427
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)             \
1432     {                                           \
1433       REWRITE_OPCODE (Bconstant + arg);         \
1434     }                                           \
1435   else                                          \
1436     {                                           \
1437       REWRITE_OPCODE (Bconstant2);              \
1438       WRITE_INT16 (arg, program_ptr);           \
1439     }                                           \
1440 } while (0)
1441
1442 #define WRITE_OPCODE WRITE_INT8 (opcode, program_ptr)
1443
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(
1448         /* in */
1449         Lisp_Object instructions,
1450         Lisp_Object constants,
1451         /* out */
1452         Opbyte * const program,
1453         int *const program_length,
1454         int *const varbind_count)
1455 {
1456         size_t instructions_length = XSTRING_LENGTH(instructions);
1457         size_t comfy_size = 2 * instructions_length;
1458
1459         int *const icounts = alloca_array(int, comfy_size);
1460         int *icounts_ptr = icounts;
1461
1462         /* We maintain a table of jumps in the source code. */
1463         struct jump {
1464                 int from;
1465                 int to;
1466         };
1467         struct jump *const jumps = alloca_array(struct jump, comfy_size);
1468         struct jump *jumps_ptr = jumps;
1469
1470         Opbyte *program_ptr = program;
1471
1472         /* const means constant! */
1473         Bufbyte *ptr = XSTRING_DATA(instructions);
1474         const Bufbyte *const end = ptr + instructions_length;
1475
1476         *varbind_count = 0;
1477
1478         while (ptr < end) {
1479                 Opcode opcode;
1480                 int arg;
1481                 int argsize = 0;
1482                 READ_OPCODE;
1483                 WRITE_OPCODE;
1484
1485                 switch ((unsigned int)opcode) {
1486                         Lisp_Object val;
1487
1488                 case Bvarref + 7:
1489                         READ_OPERAND_2;
1490                         goto do_varref;
1491                 case Bvarref + 6:
1492                         READ_OPERAND_1;
1493                         goto do_varref;
1494                 case Bvarref:
1495                 case Bvarref + 1:
1496                 case Bvarref + 2:
1497                 case Bvarref + 3:
1498                 case Bvarref + 4:
1499                 case Bvarref + 5:
1500                         arg = opcode - Bvarref;
1501                       do_varref:
1502                         check_constants_index(arg, constants);
1503                         val = XVECTOR_DATA(constants)[arg];
1504                         if (!SYMBOLP(val))
1505                                 invalid_byte_code_error
1506                                     ("variable reference to non-symbol %S",
1507                                      val);
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);
1514                         break;
1515
1516                 case Bvarset + 7:
1517                         READ_OPERAND_2;
1518                         goto do_varset;
1519                 case Bvarset + 6:
1520                         READ_OPERAND_1;
1521                         goto do_varset;
1522                 case Bvarset:
1523                 case Bvarset + 1:
1524                 case Bvarset + 2:
1525                 case Bvarset + 3:
1526                 case Bvarset + 4:
1527                 case Bvarset + 5:
1528                         arg = opcode - Bvarset;
1529                       do_varset:
1530                         check_constants_index(arg, constants);
1531                         val = XVECTOR_DATA(constants)[arg];
1532                         if (!SYMBOLP(val))
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);
1543                         else
1544                                 WRITE_NARGS(Bvarset);
1545                         break;
1546
1547                 case Bvarbind + 7:
1548                         READ_OPERAND_2;
1549                         goto do_varbind;
1550                 case Bvarbind + 6:
1551                         READ_OPERAND_1;
1552                         goto do_varbind;
1553                 case Bvarbind:
1554                 case Bvarbind + 1:
1555                 case Bvarbind + 2:
1556                 case Bvarbind + 3:
1557                 case Bvarbind + 4:
1558                 case Bvarbind + 5:
1559                         arg = opcode - Bvarbind;
1560                       do_varbind:
1561                         (*varbind_count)++;
1562                         check_constants_index(arg, constants);
1563                         val = XVECTOR_DATA(constants)[arg];
1564                         if (!SYMBOLP(val))
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);
1573                         break;
1574
1575                 case Bcall + 7:
1576                         READ_OPERAND_2;
1577                         goto do_call;
1578                 case Bcall + 6:
1579                         READ_OPERAND_1;
1580                         goto do_call;
1581                 case Bcall:
1582                 case Bcall + 1:
1583                 case Bcall + 2:
1584                 case Bcall + 3:
1585                 case Bcall + 4:
1586                 case Bcall + 5:
1587                         arg = opcode - Bcall;
1588                       do_call:
1589                         WRITE_NARGS(Bcall);
1590                         break;
1591
1592                 case Bunbind + 7:
1593                         READ_OPERAND_2;
1594                         goto do_unbind;
1595                 case Bunbind + 6:
1596                         READ_OPERAND_1;
1597                         goto do_unbind;
1598                 case Bunbind:
1599                 case Bunbind + 1:
1600                 case Bunbind + 2:
1601                 case Bunbind + 3:
1602                 case Bunbind + 4:
1603                 case Bunbind + 5:
1604                         arg = opcode - Bunbind;
1605                       do_unbind:
1606                         WRITE_NARGS(Bunbind);
1607                         break;
1608
1609                 case Bgoto:
1610                 case Bgotoifnil:
1611                 case Bgotoifnonnil:
1612                 case Bgotoifnilelsepop:
1613                 case Bgotoifnonnilelsepop:
1614                         READ_OPERAND_2;
1615                         /* Make program_ptr-relative */
1616                         arg += icounts - (icounts_ptr - argsize);
1617                         goto do_jump;
1618
1619                 case BRgoto:
1620                 case BRgotoifnil:
1621                 case BRgotoifnonnil:
1622                 case BRgotoifnilelsepop:
1623                 case BRgotoifnonnilelsepop:
1624                         READ_OPERAND_1;
1625                         /* Make program_ptr-relative */
1626                         arg -= 127;
1627                       do_jump:
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;
1631                         jumps_ptr++;
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) {
1636                                 if (argsize == 1)
1637                                         REWRITE_OPCODE(opcode + Bgoto - BRgoto);
1638                                 WRITE_INT16(arg, program_ptr);
1639                         } else {
1640                                 if (argsize == 2)
1641                                         REWRITE_OPCODE(opcode + BRgoto - Bgoto);
1642                                 WRITE_INT8(arg, program_ptr);
1643                         }
1644                         break;
1645
1646                 case Bconstant2:
1647                         READ_OPERAND_2;
1648                         WRITE_CONSTANT;
1649                         break;
1650
1651                 case BlistN:
1652                 case BconcatN:
1653                 case BinsertN:
1654                         READ_OPERAND_1;
1655                         WRITE_INT8(arg, program_ptr);
1656                         break;
1657
1658                 default:
1659                         if (opcode < Bconstant)
1660                                 check_opcode(opcode);
1661                         else {
1662                                 arg = opcode - Bconstant;
1663                                 WRITE_CONSTANT;
1664                         }
1665                         break;
1666                 }
1667         }
1668
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);
1674 #endif
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]));
1681 #endif
1682                 check_opcode((Opcode) (program[j->to]));
1683         }
1684
1685         /* Fixup jumps in byte-code until no more fixups needed */
1686         for (bool more_fixups_needed = true; more_fixups_needed; ) {
1687                 struct jump *j;
1688
1689                 /* assume we don't need more hiccups */
1690                 more_fixups_needed = false;
1691                 for (j = jumps; j < jumps_ptr; j++) {
1692                         int from = j->from;
1693                         int to = j->to;
1694                         int jump = to - from;
1695                         Opbyte *p = program + from;
1696                         Opcode opcode = (Opcode)p[-1];
1697
1698                         if (!more_fixups_needed) {
1699                                 check_opcode((Opcode) p[jump]);
1700                         }
1701                         assert(to >= 0 && program + to < program_ptr);
1702
1703                         switch ((unsigned int)opcode) {
1704                         case Bgoto:
1705                         case Bgotoifnil:
1706                         case Bgotoifnonnil:
1707                         case Bgotoifnilelsepop:
1708                         case Bgotoifnonnilelsepop:
1709                                 WRITE_INT16(jump, p);
1710                                 break;
1711
1712                         case BRgoto:
1713                         case BRgotoifnil:
1714                         case BRgotoifnonnil:
1715                         case BRgotoifnilelsepop:
1716                         case BRgotoifnonnilelsepop:
1717                                 if (jump > SCHAR_MIN && jump <= SCHAR_MAX) {
1718                                         WRITE_INT8(jump, p);
1719                                 } else {
1720                                         /* barf */
1721                                         struct jump *jj;
1722
1723                                         for (jj = jumps; jj < jumps_ptr; jj++) {
1724                                                 assert(jj->from <
1725                                                        program_ptr - program);
1726                                                 assert(jj->to <
1727                                                        program_ptr - program);
1728                                                 if (jj->from > from) {
1729                                                         jj->from++;
1730                                                 }
1731                                                 if (jj->to > from) {
1732                                                         jj->to++;
1733                                                 }
1734                                         }
1735                                         p[-1] += Bgoto - BRgoto;
1736                                         more_fixups_needed = true;
1737                                         memmove(p + 1, p, program_ptr++ - p);
1738                                         WRITE_INT16(jump, p);
1739                                 }
1740                                 break;
1741
1742                         default:
1743                                 abort();
1744                                 break;
1745                         }
1746                 }
1747         }
1748
1749         /* *program_ptr++ = 0; */
1750         *program_length = program_ptr - program;
1751 }
1752
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)
1757 {
1758         Lisp_Compiled_Function *f = XCOMPILED_FUNCTION(compiled_function);
1759         int program_length;
1760         int varbind_count;
1761         Opbyte *program;
1762
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);
1767
1768         if (STRINGP(f->instructions)) {
1769                 /* XSTRING_LENGTH() is more efficient than XSTRING_CHAR_LENGTH(),
1770                    which would be slightly more `proper' */
1771                 program =
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;
1777                 f->instructions =
1778                     make_opaque(program, program_length * sizeof(Opbyte));
1779         }
1780
1781         assert(OPAQUEP(f->instructions));
1782 }
1783 \f
1784 /************************************************************************/
1785 /*              The compiled-function object type                       */
1786 /************************************************************************/
1787 static void
1788 print_compiled_function(Lisp_Object obj, Lisp_Object printcharfun,
1789                         int escapeflag)
1790 {
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);
1797
1798         write_c_string(print_readably ? "#[" : "#<compiled-function ",
1799                        printcharfun);
1800 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
1801         if (!print_readably) {
1802                 Lisp_Object ann = compiled_function_annotation(f);
1803                 if (!NILP(ann)) {
1804                         write_c_string("(from ", printcharfun);
1805                         print_internal(ann, printcharfun, 1);
1806                         write_c_string(") ", printcharfun);
1807                 }
1808         }
1809 #endif                          /* COMPILED_FUNCTION_ANNOTATION_HACK */
1810         /* COMPILED_ARGLIST = 0 */
1811         print_internal(compiled_function_arglist(f), printcharfun, escapeflag);
1812
1813         /* COMPILED_INSTRUCTIONS = 1 */
1814         write_c_string(" ", printcharfun);
1815         {
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));
1823                 } else
1824                         print_internal(instructions, printcharfun, escapeflag);
1825                 NUNGCPRO;
1826         }
1827
1828         /* COMPILED_CONSTANTS = 2 */
1829         write_c_string(" ", printcharfun);
1830         print_internal(compiled_function_constants(f), printcharfun,
1831                        escapeflag);
1832
1833         /* COMPILED_STACK_DEPTH = 3 */
1834         write_fmt_str(printcharfun, " %d", compiled_function_stack_depth(f));
1835
1836         /* COMPILED_DOC_STRING = 4 */
1837         if (docp || intp) {
1838                 write_c_string(" ", printcharfun);
1839                 print_internal(compiled_function_documentation(f), printcharfun,
1840                                escapeflag);
1841         }
1842
1843         /* COMPILED_INTERACTIVE = 5 */
1844         if (intp) {
1845                 write_c_string(" ", printcharfun);
1846                 print_internal(compiled_function_interactive(f), printcharfun,
1847                                escapeflag);
1848         }
1849
1850         UNGCPRO;
1851         write_c_string(print_readably ? "]" : ">", printcharfun);
1852 }
1853
1854 static Lisp_Object mark_compiled_function(Lisp_Object obj)
1855 {
1856         Lisp_Compiled_Function *f = XCOMPILED_FUNCTION(obj);
1857
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);
1863 #endif
1864         /* tail-recurse on constants */
1865         return f->constants;
1866 }
1867
1868 static int
1869 compiled_function_equal(Lisp_Object obj1, Lisp_Object obj2, int depth)
1870 {
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));
1880 }
1881
1882 static unsigned long compiled_function_hash(Lisp_Object obj, int depth)
1883 {
1884         Lisp_Compiled_Function *f = XCOMPILED_FUNCTION(obj);
1885         return HASH3((f->flags.documentationp << 2) +
1886                      (f->flags.interactivep << 1) +
1887                      f->flags.domainp,
1888                      internal_hash(f->instructions, depth + 1),
1889                      internal_hash(f->constants, depth + 1));
1890 }
1891
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)},
1899 #endif
1900         {XD_END}
1901 };
1902
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);
1910 \f
1911 DEFUN("compiled-function-p", Fcompiled_function_p, 1, 1, 0,     /*
1912 Return t if OBJECT is a byte-compiled function object.
1913 */
1914       (object))
1915 {
1916         return COMPILED_FUNCTIONP(object) ? Qt : Qnil;
1917 }
1918
1919 /************************************************************************/
1920 /*              compiled-function object accessor functions             */
1921 /************************************************************************/
1922
1923 Lisp_Object compiled_function_arglist(Lisp_Compiled_Function * f)
1924 {
1925         return f->arglist;
1926 }
1927
1928 Lisp_Object compiled_function_instructions(Lisp_Compiled_Function * f)
1929 {
1930         if (!OPAQUEP(f->instructions))
1931                 return f->instructions;
1932
1933         {
1934                 /* Invert action performed by optimize_byte_code() */
1935                 Lisp_Opaque *opaque = XOPAQUE(f->instructions);
1936
1937                 Bufbyte *const buffer =
1938                     alloca_array(Bufbyte, OPAQUE_SIZE(opaque) * MAX_EMCHAR_LEN);
1939                 Bufbyte *bp = buffer;
1940
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);
1946
1947                 while (program_ptr < program_end) {
1948                         Opcode opcode = (Opcode) READ_UINT_1;
1949                         bp += set_charptr_emchar(bp, opcode);
1950
1951                         switch ((unsigned int)opcode) {
1952                         case Bvarref + 7:
1953                         case Bvarset + 7:
1954                         case Bvarbind + 7:
1955                         case Bcall + 7:
1956                         case Bunbind + 7:
1957                         case Bconstant2:
1958                                 bp += set_charptr_emchar(bp, READ_UINT_1);
1959                                 bp += set_charptr_emchar(bp, READ_UINT_1);
1960                                 break;
1961
1962                         case Bvarref + 6:
1963                         case Bvarset + 6:
1964                         case Bvarbind + 6:
1965                         case Bcall + 6:
1966                         case Bunbind + 6:
1967                         case BlistN:
1968                         case BconcatN:
1969                         case BinsertN:
1970                                 bp += set_charptr_emchar(bp, READ_UINT_1);
1971                                 break;
1972
1973                         case Bgoto:
1974                         case Bgotoifnil:
1975                         case Bgotoifnonnil:
1976                         case Bgotoifnilelsepop:
1977                         case Bgotoifnonnilelsepop:
1978                                 {
1979                                         int jump = READ_INT_2;
1980                                         Opbyte buf2[2];
1981                                         Opbyte *buf2p = buf2;
1982                                         /* Convert back to program-relative address */
1983                                         WRITE_INT16(jump +
1984                                                     (program_ptr - 2 - program),
1985                                                     buf2p);
1986                                         bp += set_charptr_emchar(bp, buf2[0]);
1987                                         bp += set_charptr_emchar(bp, buf2[1]);
1988                                         break;
1989                                 }
1990
1991                         case BRgoto:
1992                         case BRgotoifnil:
1993                         case BRgotoifnonnil:
1994                         case BRgotoifnilelsepop:
1995                         case BRgotoifnonnilelsepop:
1996                                 bp += set_charptr_emchar(bp, READ_INT_1 + 127);
1997                                 break;
1998
1999                         default:
2000                                 break;
2001                         }
2002                 }
2003                 return make_string(buffer, bp - buffer);
2004         }
2005 }
2006
2007 Lisp_Object compiled_function_constants(Lisp_Compiled_Function * f)
2008 {
2009         return f->constants;
2010 }
2011
2012 int compiled_function_stack_depth(Lisp_Compiled_Function * f)
2013 {
2014         return f->stack_depth;
2015 }
2016
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:
2020
2021         doc
2022         interactive
2023         domain
2024         (doc . interactive)
2025         (doc . domain)
2026         (interactive . domain)
2027         (doc . (interactive . domain))
2028  */
2029
2030 /* Caller must check flags.interactivep first */
2031 Lisp_Object compiled_function_interactive(Lisp_Compiled_Function * f)
2032 {
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);
2040         else
2041                 return f->doc_and_interactive;
2042 }
2043
2044 /* Caller need not check flags.documentationp first */
2045 Lisp_Object compiled_function_documentation(Lisp_Compiled_Function * f)
2046 {
2047         if (!f->flags.documentationp)
2048                 return Qnil;
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);
2055         else
2056                 return f->doc_and_interactive;
2057 }
2058
2059 /* Caller need not check flags.domainp first */
2060 Lisp_Object compiled_function_domain(Lisp_Compiled_Function * f)
2061 {
2062         if (!f->flags.domainp)
2063                 return Qnil;
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);
2070         else
2071                 return f->doc_and_interactive;
2072 }
2073
2074 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
2075
2076 Lisp_Object compiled_function_annotation(Lisp_Compiled_Function * f)
2077 {
2078         return f->annotated;
2079 }
2080
2081 #endif
2082
2083 /* used only by Snarf-documentation; there must be doc already. */
2084 void
2085 set_compiled_function_documentation(Lisp_Compiled_Function * f,
2086                                     Lisp_Object new_doc)
2087 {
2088         assert(f->flags.documentationp);
2089         assert(INTP(new_doc) || STRINGP(new_doc));
2090
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;
2097         else
2098                 f->doc_and_interactive = new_doc;
2099 }
2100
2101 DEFUN("compiled-function-arglist", Fcompiled_function_arglist, 1, 1, 0, /*
2102 Return the argument list of the compiled-function object FUNCTION.
2103 */
2104       (function))
2105 {
2106         CHECK_COMPILED_FUNCTION(function);
2107         return compiled_function_arglist(XCOMPILED_FUNCTION(function));
2108 }
2109
2110 DEFUN("compiled-function-instructions", Fcompiled_function_instructions, 1, 1, 0,       /*
2111 Return the byte-opcode string of the compiled-function object FUNCTION.
2112 */
2113       (function))
2114 {
2115         CHECK_COMPILED_FUNCTION(function);
2116         return compiled_function_instructions(XCOMPILED_FUNCTION(function));
2117 }
2118
2119 DEFUN("compiled-function-constants", Fcompiled_function_constants, 1, 1, 0,     /*
2120 Return the constants vector of the compiled-function object FUNCTION.
2121 */
2122       (function))
2123 {
2124         CHECK_COMPILED_FUNCTION(function);
2125         return compiled_function_constants(XCOMPILED_FUNCTION(function));
2126 }
2127
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.
2130 */
2131       (function))
2132 {
2133         CHECK_COMPILED_FUNCTION(function);
2134         return
2135             make_int(compiled_function_stack_depth
2136                      (XCOMPILED_FUNCTION(function)));
2137 }
2138
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.
2143 */
2144       (function))
2145 {
2146         CHECK_COMPILED_FUNCTION(function);
2147         return compiled_function_documentation(XCOMPILED_FUNCTION(function));
2148 }
2149
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.
2154 */
2155       (function))
2156 {
2157         CHECK_COMPILED_FUNCTION(function);
2158         return XCOMPILED_FUNCTION(function)->flags.interactivep
2159             ? list2(Qinteractive,
2160                     compiled_function_interactive(XCOMPILED_FUNCTION(function)))
2161             : Qnil;
2162 }
2163
2164 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
2165
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
2174 a `load'.
2175                                                                                  */
2176         (function)) {
2177         CHECK_COMPILED_FUNCTION(function);
2178         return compiled_function_annotation(XCOMPILED_FUNCTION(function));
2179 }
2180
2181 #endif                          /* COMPILED_FUNCTION_ANNOTATION_HACK */
2182
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.
2186 */
2187       (function))
2188 {
2189         CHECK_COMPILED_FUNCTION(function);
2190         return XCOMPILED_FUNCTION(function)->flags.domainp
2191             ? compiled_function_domain(XCOMPILED_FUNCTION(function))
2192             : Qnil;
2193 }
2194 \f
2195 DEFUN("fetch-bytecode", Ffetch_bytecode, 1, 1, 0,       /*
2196 If the byte code for compiled function FUNCTION is lazy-loaded, fetch it now.
2197 */
2198       (function))
2199 {
2200         Lisp_Compiled_Function *f;
2201         CHECK_COMPILED_FUNCTION(function);
2202         f = XCOMPILED_FUNCTION(function);
2203
2204         if (OPAQUEP(f->instructions) || STRINGP(f->instructions))
2205                 return function;
2206
2207         if (CONSP(f->instructions)) {
2208                 Lisp_Object tem = read_doc_string(f->instructions);
2209                 if (!CONSP(tem))
2210                         signal_simple_error("Invalid lazy-loaded byte code",
2211                                             tem);
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);
2217                 return function;
2218         }
2219         abort();
2220         return Qnil;            /* not reached */
2221 }
2222
2223 DEFUN("optimize-compiled-function", Foptimize_compiled_function, 1, 1, 0,       /*
2224 Convert compiled function FUNCTION into an optimized internal form.
2225 */
2226       (function))
2227 {
2228         Lisp_Compiled_Function *f;
2229         CHECK_COMPILED_FUNCTION(function);
2230         f = XCOMPILED_FUNCTION(function);
2231
2232         if (OPAQUEP(f->instructions))   /* Already optimized? */
2233                 return Qnil;
2234
2235         optimize_compiled_function(function);
2236         return Qnil;
2237 }
2238
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.
2245 */
2246       (instructions, constants, stack_depth))
2247 {
2248         /* This function can GC */
2249         int varbind_count;
2250         int program_length;
2251         Opbyte *program;
2252
2253         CHECK_STRING(instructions);
2254         CHECK_VECTOR(constants);
2255         CHECK_NATNUM(stack_depth);
2256
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,
2265                                          XINT(stack_depth),
2266                                          XVECTOR_DATA(constants));
2267 }
2268 \f
2269 void syms_of_bytecode(void)
2270 {
2271         INIT_LRECORD_IMPLEMENTATION(compiled_function);
2272
2273         DEFERROR_STANDARD(Qinvalid_byte_code, Qinvalid_state);
2274         defsymbol(&Qbyte_code, "byte-code");
2275         defsymbol(&Qcompiled_functionp, "compiled-function-p");
2276
2277         DEFSUBR(Fbyte_code);
2278         DEFSUBR(Ffetch_bytecode);
2279         DEFSUBR(Foptimize_compiled_function);
2280
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);
2291 #endif
2292
2293 #ifdef BYTE_CODE_METER
2294         defsymbol(&Qbyte_code_meter, "byte-code-meter");
2295 #endif
2296 }
2297
2298 void vars_of_bytecode(void)
2299 {
2300 #ifdef BYTE_CODE_METER
2301
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.
2309                                                                  */ );
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.
2315                                                                  */ );
2316
2317         byte_metering_on = 0;
2318         Vbyte_code_meter = make_vector(256, Qzero);
2319         {
2320                 int i = 256;
2321                 while (i--)
2322                         XVECTOR_DATA(Vbyte_code_meter)[i] =
2323                             make_vector(256, Qzero);
2324         }
2325 #endif                          /* BYTE_CODE_METER */
2326 }