bytecode.c 37.9 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Execution of byte code produced by bytecomp.el.
Paul Eggert's avatar
Paul Eggert committed
2
   Copyright (C) 1985-1988, 1993, 2000-2016 Free Software Foundation,
3
   Inc.
Jim Blandy's avatar
Jim Blandy committed
4 5 6

This file is part of GNU Emacs.

7
GNU Emacs is free software: you can redistribute it and/or modify
Jim Blandy's avatar
Jim Blandy committed
8
it under the terms of the GNU General Public License as published by
9 10
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
Jim Blandy's avatar
Jim Blandy committed
11 12 13 14 15 16 17

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
18
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
19

20
#include <config.h>
21

Jim Blandy's avatar
Jim Blandy committed
22
#include "lisp.h"
Paul Eggert's avatar
Paul Eggert committed
23
#include "blockinput.h"
24
#include "character.h"
25
#include "buffer.h"
Paul Eggert's avatar
Paul Eggert committed
26
#include "keyboard.h"
Jim Blandy's avatar
Jim Blandy committed
27
#include "syntax.h"
Stefan Monnier's avatar
Stefan Monnier committed
28
#include "window.h"
Jim Blandy's avatar
Jim Blandy committed
29

30 31 32 33 34
#ifdef CHECK_FRAME_FONT
#include "frame.h"
#include "xterm.h"
#endif

35
/*
36
 * define BYTE_CODE_SAFE to enable some minor sanity checking (useful for
37 38
 * debugging the byte compiler...)
 *
39
 * define BYTE_CODE_METER to enable generation of a byte-op usage histogram.
Jim Blandy's avatar
Jim Blandy committed
40
 */
41 42
/* #define BYTE_CODE_SAFE */
/* #define BYTE_CODE_METER */
Jim Blandy's avatar
Jim Blandy committed
43

Tom Tromey's avatar
Tom Tromey committed
44 45 46 47
/* If BYTE_CODE_THREADED is defined, then the interpreter will be
   indirect threaded, using GCC's computed goto extension.  This code,
   as currently implemented, is incompatible with BYTE_CODE_SAFE and
   BYTE_CODE_METER.  */
Paul Eggert's avatar
Paul Eggert committed
48 49
#if (defined __GNUC__ && !defined __STRICT_ANSI__ \
     && !defined BYTE_CODE_SAFE && !defined BYTE_CODE_METER)
Tom Tromey's avatar
Tom Tromey committed
50 51 52
#define BYTE_CODE_THREADED
#endif

Jim Blandy's avatar
Jim Blandy committed
53 54 55

#ifdef BYTE_CODE_METER

56 57
#define METER_2(code1, code2) AREF (AREF (Vbyte_code_meter, code1), code2)
#define METER_1(code) METER_2 (0, code)
58

59 60 61 62
#define METER_CODE(last_code, this_code)				\
{									\
  if (byte_metering_on)							\
    {									\
63 64 65
      if (XFASTINT (METER_1 (this_code)) < MOST_POSITIVE_FIXNUM)	\
        XSETFASTINT (METER_1 (this_code),				\
		     XFASTINT (METER_1 (this_code)) + 1);		\
66
      if (last_code							\
67 68 69 70
	  && (XFASTINT (METER_2 (last_code, this_code))			\
	      < MOST_POSITIVE_FIXNUM))					\
        XSETFASTINT (METER_2 (last_code, this_code),			\
		     XFASTINT (METER_2 (last_code, this_code)) + 1);	\
71
    }									\
72
}
Jim Blandy's avatar
Jim Blandy committed
73

74
#endif /* BYTE_CODE_METER */
Jim Blandy's avatar
Jim Blandy committed
75 76 77 78


/*  Byte codes: */

Tom Tromey's avatar
Tom Tromey committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#define BYTE_CODES							\
DEFINE (Bstack_ref, 0) /* Actually, Bstack_ref+0 is not implemented: use dup.  */ \
DEFINE (Bstack_ref1, 1)							\
DEFINE (Bstack_ref2, 2)							\
DEFINE (Bstack_ref3, 3)							\
DEFINE (Bstack_ref4, 4)							\
DEFINE (Bstack_ref5, 5)							\
DEFINE (Bstack_ref6, 6)							\
DEFINE (Bstack_ref7, 7)							\
DEFINE (Bvarref, 010)							\
DEFINE (Bvarref1, 011)							\
DEFINE (Bvarref2, 012)							\
DEFINE (Bvarref3, 013)							\
DEFINE (Bvarref4, 014)							\
DEFINE (Bvarref5, 015)							\
DEFINE (Bvarref6, 016)							\
DEFINE (Bvarref7, 017)							\
DEFINE (Bvarset, 020)							\
DEFINE (Bvarset1, 021)							\
DEFINE (Bvarset2, 022)							\
DEFINE (Bvarset3, 023)							\
DEFINE (Bvarset4, 024)							\
DEFINE (Bvarset5, 025)							\
DEFINE (Bvarset6, 026)							\
DEFINE (Bvarset7, 027)							\
DEFINE (Bvarbind, 030)							\
DEFINE (Bvarbind1, 031)							\
DEFINE (Bvarbind2, 032)							\
DEFINE (Bvarbind3, 033)							\
DEFINE (Bvarbind4, 034)							\
DEFINE (Bvarbind5, 035)							\
DEFINE (Bvarbind6, 036)							\
DEFINE (Bvarbind7, 037)							\
DEFINE (Bcall, 040)							\
DEFINE (Bcall1, 041)							\
DEFINE (Bcall2, 042)							\
DEFINE (Bcall3, 043)							\
DEFINE (Bcall4, 044)							\
DEFINE (Bcall5, 045)							\
DEFINE (Bcall6, 046)							\
DEFINE (Bcall7, 047)							\
DEFINE (Bunbind, 050)							\
DEFINE (Bunbind1, 051)							\
DEFINE (Bunbind2, 052)							\
DEFINE (Bunbind3, 053)							\
DEFINE (Bunbind4, 054)							\
DEFINE (Bunbind5, 055)							\
DEFINE (Bunbind6, 056)							\
DEFINE (Bunbind7, 057)							\
									\
129 130 131 132
DEFINE (Bpophandler, 060)						\
DEFINE (Bpushconditioncase, 061)					\
DEFINE (Bpushcatch, 062)						\
									\
Tom Tromey's avatar
Tom Tromey committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
DEFINE (Bnth, 070)							\
DEFINE (Bsymbolp, 071)							\
DEFINE (Bconsp, 072)							\
DEFINE (Bstringp, 073)							\
DEFINE (Blistp, 074)							\
DEFINE (Beq, 075)							\
DEFINE (Bmemq, 076)							\
DEFINE (Bnot, 077)							\
DEFINE (Bcar, 0100)							\
DEFINE (Bcdr, 0101)							\
DEFINE (Bcons, 0102)							\
DEFINE (Blist1, 0103)							\
DEFINE (Blist2, 0104)							\
DEFINE (Blist3, 0105)							\
DEFINE (Blist4, 0106)							\
DEFINE (Blength, 0107)							\
DEFINE (Baref, 0110)							\
DEFINE (Baset, 0111)							\
DEFINE (Bsymbol_value, 0112)						\
DEFINE (Bsymbol_function, 0113)						\
DEFINE (Bset, 0114)							\
DEFINE (Bfset, 0115)							\
DEFINE (Bget, 0116)							\
DEFINE (Bsubstring, 0117)						\
DEFINE (Bconcat2, 0120)							\
DEFINE (Bconcat3, 0121)							\
DEFINE (Bconcat4, 0122)							\
DEFINE (Bsub1, 0123)							\
DEFINE (Badd1, 0124)							\
DEFINE (Beqlsign, 0125)							\
DEFINE (Bgtr, 0126)							\
DEFINE (Blss, 0127)							\
DEFINE (Bleq, 0130)							\
DEFINE (Bgeq, 0131)							\
DEFINE (Bdiff, 0132)							\
DEFINE (Bnegate, 0133)							\
DEFINE (Bplus, 0134)							\
DEFINE (Bmax, 0135)							\
DEFINE (Bmin, 0136)							\
DEFINE (Bmult, 0137)							\
									\
DEFINE (Bpoint, 0140)							\
/* Was Bmark in v17.  */						\
DEFINE (Bsave_current_buffer, 0141) /* Obsolete.  */			\
DEFINE (Bgoto_char, 0142)						\
DEFINE (Binsert, 0143)							\
DEFINE (Bpoint_max, 0144)						\
DEFINE (Bpoint_min, 0145)						\
DEFINE (Bchar_after, 0146)						\
DEFINE (Bfollowing_char, 0147)						\
DEFINE (Bpreceding_char, 0150)						\
DEFINE (Bcurrent_column, 0151)						\
DEFINE (Bindent_to, 0152)						\
DEFINE (Beolp, 0154)							\
DEFINE (Beobp, 0155)							\
DEFINE (Bbolp, 0156)							\
DEFINE (Bbobp, 0157)							\
DEFINE (Bcurrent_buffer, 0160)						\
DEFINE (Bset_buffer, 0161)						\
DEFINE (Bsave_current_buffer_1, 0162) /* Replacing Bsave_current_buffer.  */ \
DEFINE (Binteractive_p, 0164) /* Obsolete since Emacs-24.1.  */		\
									\
DEFINE (Bforward_char, 0165)						\
DEFINE (Bforward_word, 0166)						\
DEFINE (Bskip_chars_forward, 0167)					\
DEFINE (Bskip_chars_backward, 0170)					\
DEFINE (Bforward_line, 0171)						\
DEFINE (Bchar_syntax, 0172)						\
DEFINE (Bbuffer_substring, 0173)					\
DEFINE (Bdelete_region, 0174)						\
DEFINE (Bnarrow_to_region, 0175)					\
DEFINE (Bwiden, 0176)							\
DEFINE (Bend_of_line, 0177)						\
									\
DEFINE (Bconstant2, 0201)						\
DEFINE (Bgoto, 0202)							\
DEFINE (Bgotoifnil, 0203)						\
DEFINE (Bgotoifnonnil, 0204)						\
DEFINE (Bgotoifnilelsepop, 0205)					\
DEFINE (Bgotoifnonnilelsepop, 0206)					\
DEFINE (Breturn, 0207)							\
DEFINE (Bdiscard, 0210)							\
DEFINE (Bdup, 0211)							\
									\
DEFINE (Bsave_excursion, 0212)						\
DEFINE (Bsave_window_excursion, 0213) /* Obsolete since Emacs-24.1.  */	\
DEFINE (Bsave_restriction, 0214)					\
DEFINE (Bcatch, 0215)							\
									\
DEFINE (Bunwind_protect, 0216)						\
DEFINE (Bcondition_case, 0217)						\
DEFINE (Btemp_output_buffer_setup, 0220) /* Obsolete since Emacs-24.1.  */ \
DEFINE (Btemp_output_buffer_show, 0221)  /* Obsolete since Emacs-24.1.  */ \
									\
DEFINE (Bunbind_all, 0222)	/* Obsolete.  Never used.  */		\
									\
DEFINE (Bset_marker, 0223)						\
DEFINE (Bmatch_beginning, 0224)						\
DEFINE (Bmatch_end, 0225)						\
DEFINE (Bupcase, 0226)							\
DEFINE (Bdowncase, 0227)						\
									\
DEFINE (Bstringeqlsign, 0230)						\
DEFINE (Bstringlss, 0231)						\
DEFINE (Bequal, 0232)							\
DEFINE (Bnthcdr, 0233)							\
DEFINE (Belt, 0234)							\
DEFINE (Bmember, 0235)							\
DEFINE (Bassq, 0236)							\
DEFINE (Bnreverse, 0237)						\
DEFINE (Bsetcar, 0240)							\
DEFINE (Bsetcdr, 0241)							\
DEFINE (Bcar_safe, 0242)						\
DEFINE (Bcdr_safe, 0243)						\
DEFINE (Bnconc, 0244)							\
DEFINE (Bquo, 0245)							\
DEFINE (Brem, 0246)							\
DEFINE (Bnumberp, 0247)							\
DEFINE (Bintegerp, 0250)						\
									\
DEFINE (BRgoto, 0252)							\
DEFINE (BRgotoifnil, 0253)						\
DEFINE (BRgotoifnonnil, 0254)						\
DEFINE (BRgotoifnilelsepop, 0255)					\
DEFINE (BRgotoifnonnilelsepop, 0256)					\
									\
DEFINE (BlistN, 0257)							\
DEFINE (BconcatN, 0260)							\
DEFINE (BinsertN, 0261)							\
									\
/* Bstack_ref is code 0.  */						\
DEFINE (Bstack_set,  0262)						\
DEFINE (Bstack_set2, 0263)						\
DEFINE (BdiscardN,   0266)						\
									\
DEFINE (Bconstant, 0300)

enum byte_code_op
{
#define DEFINE(name, value) name = value,
    BYTE_CODES
#undef DEFINE

276
#ifdef BYTE_CODE_SAFE
Tom Tromey's avatar
Tom Tromey committed
277
    Bscan_buffer = 0153, /* No longer generated as of v18.  */
Paul Eggert's avatar
Paul Eggert committed
278
    Bset_mark = 0163, /* this loser is no longer generated as of v18 */
279
#endif
Tom Tromey's avatar
Tom Tromey committed
280
};
281

Gerd Moellmann's avatar
Gerd Moellmann committed
282 283 284 285 286 287 288
/* Structure describing a value stack used during byte-code execution
   in Fbyte_code.  */

struct byte_stack
{
  /* Program counter.  This points into the byte_string below
     and is relocated when that string is relocated.  */
289
  const unsigned char *pc;
Gerd Moellmann's avatar
Gerd Moellmann committed
290 291

  /* The string containing the byte-code, and its current address.
292
     Storing this here protects it from GC.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
293
  Lisp_Object byte_string;
294
  const unsigned char *byte_string_start;
Gerd Moellmann's avatar
Gerd Moellmann committed
295 296 297 298 299 300 301

  /* Next entry in byte_stack_list.  */
  struct byte_stack *next;
};

/* A list of currently active byte-code execution value stacks.
   Fbyte_code adds an entry to the head of this list before it starts
302
   processing byte-code, and it removes the entry again when it is
303
   done.  Signaling an error truncates the list.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
304 305 306

struct byte_stack *byte_stack_list;

307

308 309
/* Relocate program counters in the stacks on byte_stack_list.  Called
   when GC has completed.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
310

311
void
312
relocate_byte_stack (void)
Gerd Moellmann's avatar
Gerd Moellmann committed
313 314 315 316
{
  struct byte_stack *stack;

  for (stack = byte_stack_list; stack; stack = stack->next)
317
    {
318
      if (stack->byte_string_start != SDATA (stack->byte_string))
319
	{
320
	  ptrdiff_t offset = stack->pc - stack->byte_string_start;
321
	  stack->byte_string_start = SDATA (stack->byte_string);
322 323 324
	  stack->pc = stack->byte_string_start + offset;
	}
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
325 326
}

Jim Blandy's avatar
Jim Blandy committed
327

328
/* Fetch the next byte from the bytecode stream.  */
Jim Blandy's avatar
Jim Blandy committed
329

330 331 332
#ifdef BYTE_CODE_SAFE
#define FETCH (eassert (stack.byte_string_start == SDATA (stack.byte_string)), *stack.pc++)
#else
Gerd Moellmann's avatar
Gerd Moellmann committed
333
#define FETCH *stack.pc++
334
#endif
Jim Blandy's avatar
Jim Blandy committed
335

336
/* Fetch two bytes from the bytecode stream and make a 16-bit number
337
   out of them.  */
Jim Blandy's avatar
Jim Blandy committed
338 339 340

#define FETCH2 (op = FETCH, op + (FETCH << 8))

341 342 343 344
/* Push x onto the execution stack.  This used to be #define PUSH(x)
   (*++stackp = (x)) This oddity is necessary because Alliant can't be
   bothered to compile the preincrement operator properly, as of 4/91.
   -JimB */
Gerd Moellmann's avatar
Gerd Moellmann committed
345 346

#define PUSH(x) (top++, *top = (x))
Jim Blandy's avatar
Jim Blandy committed
347 348 349

/* Pop a value off the execution stack.  */

Gerd Moellmann's avatar
Gerd Moellmann committed
350
#define POP (*top--)
Jim Blandy's avatar
Jim Blandy committed
351 352 353

/* Discard n values from the execution stack.  */

Gerd Moellmann's avatar
Gerd Moellmann committed
354 355 356
#define DISCARD(n) (top -= (n))

/* Get the value which is at the top of the execution stack, but don't
357
   pop it.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
358 359

#define TOP (*top)
Jim Blandy's avatar
Jim Blandy committed
360

361
/* Check for jumping out of range.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
362 363 364

#ifdef BYTE_CODE_SAFE

365
#define CHECK_RANGE(ARG) \
366
  if (ARG >= bytestr_length) emacs_abort ()
367

368
#else /* not BYTE_CODE_SAFE */
Gerd Moellmann's avatar
Gerd Moellmann committed
369 370 371

#define CHECK_RANGE(ARG)

372
#endif /* not BYTE_CODE_SAFE */
Gerd Moellmann's avatar
Gerd Moellmann committed
373

Gerd Moellmann's avatar
Gerd Moellmann committed
374 375 376 377 378 379 380
/* A version of the QUIT macro which makes sure that the stack top is
   set before signaling `quit'.  */

#define BYTE_CODE_QUIT					\
  do {							\
    if (!NILP (Vquit_flag) && NILP (Vinhibit_quit))	\
      {							\
381
        Lisp_Object flag = Vquit_flag;			\
Gerd Moellmann's avatar
Gerd Moellmann committed
382
	Vquit_flag = Qnil;				\
383
	if (EQ (Vthrow_on_input, flag))			\
384
	  Fthrow (Vthrow_on_input, Qt);			\
Gerd Moellmann's avatar
Gerd Moellmann committed
385 386
	Fsignal (Qquit, Qnil);				\
      }							\
387 388
    else if (pending_signals)				\
      process_pending_signals ();			\
Gerd Moellmann's avatar
Gerd Moellmann committed
389 390
  } while (0)

Gerd Moellmann's avatar
Gerd Moellmann committed
391

Jim Blandy's avatar
Jim Blandy committed
392
DEFUN ("byte-code", Fbyte_code, Sbyte_code, 3, 3, 0,
393 394 395 396 397
       doc: /* Function used internally in byte-compiled code.
The first argument, BYTESTR, is a string of byte code;
the second, VECTOR, a vector of constants;
the third, MAXDEPTH, the maximum stack depth used in this function.
If the third argument is incorrect, Emacs may crash.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
398
  (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth)
399
{
400
  return exec_byte_code (bytestr, vector, maxdepth, Qnil, 0, NULL);
401 402
}

403 404 405 406 407 408
static void
bcall0 (Lisp_Object f)
{
  Ffuncall (1, &f);
}

409 410 411 412 413 414 415 416 417
/* Execute the byte-code in BYTESTR.  VECTOR is the constant vector, and
   MAXDEPTH is the maximum stack depth used (if MAXDEPTH is incorrect,
   emacs may crash!).  If ARGS_TEMPLATE is non-nil, it should be a lisp
   argument list (including &rest, &optional, etc.), and ARGS, of size
   NARGS, should be a vector of the actual arguments.  The arguments in
   ARGS are pushed on the stack according to ARGS_TEMPLATE before
   executing BYTESTR.  */

Lisp_Object
Stefan Monnier's avatar
Stefan Monnier committed
418
exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
419
		Lisp_Object args_template, ptrdiff_t nargs, Lisp_Object *args)
Jim Blandy's avatar
Jim Blandy committed
420
{
421
  ptrdiff_t count = SPECPDL_INDEX ();
Jim Blandy's avatar
Jim Blandy committed
422
#ifdef BYTE_CODE_METER
423
  int volatile this_op = 0;
Jim Blandy's avatar
Jim Blandy committed
424 425
  int prev_op;
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
426
  int op;
427
  /* Lisp_Object v1, v2; */
428
  Lisp_Object *vectorp;
Jim Blandy's avatar
Jim Blandy committed
429
#ifdef BYTE_CODE_SAFE
430 431 432
  ptrdiff_t const_length;
  Lisp_Object *stacke;
  ptrdiff_t bytestr_length;
433
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
434 435
  struct byte_stack stack;
  Lisp_Object *top;
436
  Lisp_Object result;
437
  enum handlertype type;
Jim Blandy's avatar
Jim Blandy committed
438

439
#if 0 /* CHECK_FRAME_FONT */
440 441 442 443 444
 {
   struct frame *f = SELECTED_FRAME ();
   if (FRAME_X_P (f)
       && FRAME_FONT (f)->direction != 0
       && FRAME_FONT (f)->direction != 1)
445
     emacs_abort ();
446 447 448
 }
#endif

449
  CHECK_STRING (bytestr);
Kim F. Storm's avatar
Kim F. Storm committed
450
  CHECK_VECTOR (vector);
451
  CHECK_NATNUM (maxdepth);
Jim Blandy's avatar
Jim Blandy committed
452

453
#ifdef BYTE_CODE_SAFE
454
  const_length = ASIZE (vector);
455 456
#endif

457 458 459 460 461
  if (STRING_MULTIBYTE (bytestr))
    /* BYTESTR must have been produced by Emacs 20.2 or the earlier
       because they produced a raw 8-bit string for byte-code and now
       such a byte-code string is loaded as multibyte while raw 8-bit
       characters converted to multibyte form.  Thus, now we must
462
       convert them back to the originally intended unibyte form.  */
463
    bytestr = Fstring_as_unibyte (bytestr);
464

465
#ifdef BYTE_CODE_SAFE
466
  bytestr_length = SBYTES (bytestr);
467
#endif
Paul Eggert's avatar
Paul Eggert committed
468
  vectorp = XVECTOR (vector)->contents;
469

Gerd Moellmann's avatar
Gerd Moellmann committed
470
  stack.byte_string = bytestr;
471
  stack.pc = stack.byte_string_start = SDATA (bytestr);
472
  if (MAX_ALLOCA / word_size <= XFASTINT (maxdepth))
473
    memory_full (SIZE_MAX);
474
  top = alloca ((XFASTINT (maxdepth) + 1) * sizeof *top);
Gerd Moellmann's avatar
Gerd Moellmann committed
475 476
  stack.next = byte_stack_list;
  byte_stack_list = &stack;
Jim Blandy's avatar
Jim Blandy committed
477

Gerd Moellmann's avatar
Gerd Moellmann committed
478 479 480
#ifdef BYTE_CODE_SAFE
  stacke = stack.bottom - 1 + XFASTINT (maxdepth);
#endif
481

482
  if (INTEGERP (args_template))
483
    {
484
      ptrdiff_t at = XINT (args_template);
485
      bool rest = (at & 128) != 0;
486
      int mandatory = at & 127;
487
      ptrdiff_t nonrest = at >> 8;
488 489 490
      eassert (mandatory <= nonrest);
      if (nargs <= nonrest)
	{
491
	  ptrdiff_t i;
492 493 494 495 496
	  for (i = 0 ; i < nargs; i++, args++)
	    PUSH (*args);
	  if (nargs < mandatory)
	    /* Too few arguments.  */
	    Fsignal (Qwrong_number_of_arguments,
497
		     list2 (Fcons (make_number (mandatory),
498
				   rest ? Qand_rest : make_number (nonrest)),
499
			    make_number (nargs)));
500 501 502 503 504 505 506 507 508 509
	  else
	    {
	      for (; i < nonrest; i++)
		PUSH (Qnil);
	      if (rest)
		PUSH (Qnil);
	    }
	}
      else if (rest)
	{
510
	  ptrdiff_t i;
511 512 513 514 515 516
	  for (i = 0 ; i < nonrest; i++, args++)
	    PUSH (*args);
	  PUSH (Flist (nargs - nonrest, args));
	}
      else
	/* Too many arguments.  */
517
	Fsignal (Qwrong_number_of_arguments,
518 519
		 list2 (Fcons (make_number (mandatory), make_number (nonrest)),
			make_number (nargs)));
520 521 522 523 524
    }
  else if (! NILP (args_template))
    /* We should push some arguments on the stack.  */
    {
      error ("Unknown args template!");
525 526
    }

Jim Blandy's avatar
Jim Blandy committed
527 528 529
  while (1)
    {
#ifdef BYTE_CODE_SAFE
530
      if (top > stacke)
531
	emacs_abort ();
Gerd Moellmann's avatar
Gerd Moellmann committed
532
      else if (top < stack.bottom - 1)
533
	emacs_abort ();
Jim Blandy's avatar
Jim Blandy committed
534 535 536 537 538 539 540
#endif

#ifdef BYTE_CODE_METER
      prev_op = this_op;
      this_op = op = FETCH;
      METER_CODE (prev_op, op);
#else
Tom Tromey's avatar
Tom Tromey committed
541
#ifndef BYTE_CODE_THREADED
542
      op = FETCH;
Jim Blandy's avatar
Jim Blandy committed
543
#endif
Tom Tromey's avatar
Tom Tromey committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
#endif

      /* The interpreter can be compiled one of two ways: as an
	 ordinary switch-based interpreter, or as a threaded
	 interpreter.  The threaded interpreter relies on GCC's
	 computed goto extension, so it is not available everywhere.
	 Threading provides a performance boost.  These macros are how
	 we allow the code to be compiled both ways.  */
#ifdef BYTE_CODE_THREADED
      /* The CASE macro introduces an instruction's body.  It is
	 either a label or a case label.  */
#define CASE(OP) insn_ ## OP
      /* NEXT is invoked at the end of an instruction to go to the
	 next instruction.  It is either a computed goto, or a
	 plain break.  */
#define NEXT goto *(targets[op = FETCH])
      /* FIRST is like NEXT, but is only used at the start of the
	 interpreter body.  In the switch-based interpreter it is the
	 switch, so the threaded definition must include a semicolon.  */
#define FIRST NEXT;
      /* Most cases are labeled with the CASE macro, above.
	 CASE_DEFAULT is one exception; it is used if the interpreter
	 being built requires a default case.  The threaded
	 interpreter does not, because the dispatch table is
	 completely filled.  */
#define CASE_DEFAULT
      /* This introduces an instruction that is known to call abort.  */
#define CASE_ABORT CASE (Bstack_ref): CASE (default)
#else
      /* See above for the meaning of the various defines.  */
#define CASE(OP) case OP
#define NEXT break
#define FIRST switch (op)
#define CASE_DEFAULT case 255: default:
#define CASE_ABORT case 0
#endif

#ifdef BYTE_CODE_THREADED

      /* A convenience define that saves us a lot of typing and makes
	 the table clearer.  */
#define LABEL(OP) [OP] = &&insn_ ## OP
Jim Blandy's avatar
Jim Blandy committed
586

587
#if GNUC_PREREQ (4, 6, 0)
588 589
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Woverride-init"
590 591 592
#elif defined __clang__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Winitializer-overrides"
593 594
#endif

Tom Tromey's avatar
Tom Tromey committed
595 596
      /* This is the dispatch table for the threaded interpreter.  */
      static const void *const targets[256] =
597
	{
Tom Tromey's avatar
Tom Tromey committed
598 599 600 601 602 603 604
	  [0 ... (Bconstant - 1)] = &&insn_default,
	  [Bconstant ... 255] = &&insn_Bconstant,

#define DEFINE(name, value) LABEL (name) ,
	  BYTE_CODES
#undef DEFINE
	};
605

606
#if GNUC_PREREQ (4, 6, 0) || defined __clang__
607 608 609
# pragma GCC diagnostic pop
#endif

Tom Tromey's avatar
Tom Tromey committed
610 611 612 613 614 615
#endif


      FIRST
	{
	CASE (Bvarref7):
Jim Blandy's avatar
Jim Blandy committed
616 617 618
	  op = FETCH2;
	  goto varref;

Tom Tromey's avatar
Tom Tromey committed
619 620 621 622 623 624
	CASE (Bvarref):
	CASE (Bvarref1):
	CASE (Bvarref2):
	CASE (Bvarref3):
	CASE (Bvarref4):
	CASE (Bvarref5):
Jim Blandy's avatar
Jim Blandy committed
625
	  op = op - Bvarref;
626 627 628 629
	  goto varref;

	/* This seems to be the most frequently executed byte-code
	   among the Bvarref's, so avoid a goto here.  */
Tom Tromey's avatar
Tom Tromey committed
630
	CASE (Bvarref6):
631
	  op = FETCH;
Jim Blandy's avatar
Jim Blandy committed
632
	varref:
633 634 635 636 637 638
	  {
	    Lisp_Object v1, v2;

	    v1 = vectorp[op];
	    if (SYMBOLP (v1))
	      {
639 640 641
		if (XSYMBOL (v1)->redirect != SYMBOL_PLAINVAL
		    || (v2 = SYMBOL_VAL (XSYMBOL (v1)),
			EQ (v2, Qunbound)))
642 643 644
		  {
		    v2 = Fsymbol_value (v1);
		  }
645 646
	      }
	    else
647 648 649
	      {
		v2 = Fsymbol_value (v1);
	      }
650
	    PUSH (v2);
Tom Tromey's avatar
Tom Tromey committed
651
	    NEXT;
652 653
	  }

Tom Tromey's avatar
Tom Tromey committed
654
	CASE (Bgotoifnil):
655 656
	  {
	    Lisp_Object v1;
Paul Eggert's avatar
Paul Eggert committed
657
	    maybe_gc ();
658 659 660 661 662 663 664 665
	    op = FETCH2;
	    v1 = POP;
	    if (NILP (v1))
	      {
		BYTE_CODE_QUIT;
		CHECK_RANGE (op);
		stack.pc = stack.byte_string_start + op;
	      }
Tom Tromey's avatar
Tom Tromey committed
666
	    NEXT;
667
	  }
Jim Blandy's avatar
Jim Blandy committed
668

Tom Tromey's avatar
Tom Tromey committed
669
	CASE (Bcar):
670 671 672
	  {
	    Lisp_Object v1;
	    v1 = TOP;
673 674 675 676 677 678 679 680
	    if (CONSP (v1))
	      TOP = XCAR (v1);
	    else if (NILP (v1))
	      TOP = Qnil;
	    else
	      {
		wrong_type_argument (Qlistp, v1);
	      }
Tom Tromey's avatar
Tom Tromey committed
681
	    NEXT;
682 683
	  }

Tom Tromey's avatar
Tom Tromey committed
684
	CASE (Beq):
685 686 687 688
	  {
	    Lisp_Object v1;
	    v1 = POP;
	    TOP = EQ (v1, TOP) ? Qt : Qnil;
Tom Tromey's avatar
Tom Tromey committed
689
	    NEXT;
690 691
	  }

Tom Tromey's avatar
Tom Tromey committed
692
	CASE (Bmemq):
693 694 695 696
	  {
	    Lisp_Object v1;
	    v1 = POP;
	    TOP = Fmemq (TOP, v1);
Tom Tromey's avatar
Tom Tromey committed
697
	    NEXT;
698 699
	  }

Tom Tromey's avatar
Tom Tromey committed
700
	CASE (Bcdr):
701 702 703
	  {
	    Lisp_Object v1;
	    v1 = TOP;
704 705 706 707 708 709 710 711
	    if (CONSP (v1))
	      TOP = XCDR (v1);
	    else if (NILP (v1))
	      TOP = Qnil;
	    else
	      {
		wrong_type_argument (Qlistp, v1);
	      }
Tom Tromey's avatar
Tom Tromey committed
712
	    NEXT;
713
	  }
Jim Blandy's avatar
Jim Blandy committed
714

Tom Tromey's avatar
Tom Tromey committed
715 716 717 718 719 720
	CASE (Bvarset):
	CASE (Bvarset1):
	CASE (Bvarset2):
	CASE (Bvarset3):
	CASE (Bvarset4):
	CASE (Bvarset5):
721
	  op -= Bvarset;
Jim Blandy's avatar
Jim Blandy committed
722 723
	  goto varset;

Tom Tromey's avatar
Tom Tromey committed
724
	CASE (Bvarset7):
725
	  op = FETCH2;
726 727
	  goto varset;

Tom Tromey's avatar
Tom Tromey committed
728
	CASE (Bvarset6):
729
	  op = FETCH;
Jim Blandy's avatar
Jim Blandy committed
730
	varset:
731 732
	  {
	    Lisp_Object sym, val;
733

734
	    sym = vectorp[op];
735
	    val = TOP;
736 737 738 739

	    /* Inline the most common case.  */
	    if (SYMBOLP (sym)
		&& !EQ (val, Qunbound)
740 741
		&& !XSYMBOL (sym)->redirect
		&& !SYMBOL_CONSTANT_P (sym))
742
	      SET_SYMBOL_VAL (XSYMBOL (sym), val);
743
	    else
744
	      {
745
		set_internal (sym, val, Qnil, 0);
746
	      }
747
	  }
748
	  (void) POP;
Tom Tromey's avatar
Tom Tromey committed
749
	  NEXT;
Jim Blandy's avatar
Jim Blandy committed
750

Tom Tromey's avatar
Tom Tromey committed
751
	CASE (Bdup):
752 753 754 755
	  {
	    Lisp_Object v1;
	    v1 = TOP;
	    PUSH (v1);
Tom Tromey's avatar
Tom Tromey committed
756
	    NEXT;
757 758 759 760
	  }

	/* ------------------ */

Tom Tromey's avatar
Tom Tromey committed
761
	CASE (Bvarbind6):
Jim Blandy's avatar
Jim Blandy committed
762 763 764
	  op = FETCH;
	  goto varbind;

Tom Tromey's avatar
Tom Tromey committed
765
	CASE (Bvarbind7):
Jim Blandy's avatar
Jim Blandy committed
766 767 768
	  op = FETCH2;
	  goto varbind;

Tom Tromey's avatar
Tom Tromey committed
769 770 771 772 773 774
	CASE (Bvarbind):
	CASE (Bvarbind1):
	CASE (Bvarbind2):
	CASE (Bvarbind3):
	CASE (Bvarbind4):
	CASE (Bvarbind5):
Jim Blandy's avatar
Jim Blandy committed
775 776
	  op -= Bvarbind;
	varbind:
777
	  /* Specbind can signal and thus GC.  */
Jim Blandy's avatar
Jim Blandy committed
778
	  specbind (vectorp[op], POP);
Tom Tromey's avatar
Tom Tromey committed
779
	  NEXT;
Jim Blandy's avatar
Jim Blandy committed
780

Tom Tromey's avatar
Tom Tromey committed
781
	CASE (Bcall6):
Jim Blandy's avatar
Jim Blandy committed
782 783 784
	  op = FETCH;
	  goto docall;

Tom Tromey's avatar
Tom Tromey committed
785
	CASE (Bcall7):
Jim Blandy's avatar
Jim Blandy committed
786 787 788
	  op = FETCH2;
	  goto docall;

Tom Tromey's avatar
Tom Tromey committed
789 790 791 792 793 794
	CASE (Bcall):
	CASE (Bcall1):
	CASE (Bcall2):
	CASE (Bcall3):
	CASE (Bcall4):
	CASE (Bcall5):
Jim Blandy's avatar
Jim Blandy committed
795 796
	  op -= Bcall;
	docall:
797 798
	  {
	    DISCARD (op);
799
#ifdef BYTE_CODE_METER
800 801 802 803 804 805 806
	    if (byte_metering_on && SYMBOLP (TOP))
	      {
		Lisp_Object v1, v2;

		v1 = TOP;
		v2 = Fget (v1, Qbyte_code_meter);
		if (INTEGERP (v2)
807
		    && XINT (v2) < MOST_POSITIVE_FIXNUM)
808 809 810 811 812
		  {
		    XSETINT (v2, XINT (v2) + 1);
		    Fput (v1, Qbyte_code_meter, v2);
		  }
	      }
813
#endif
814
	    TOP = Ffuncall (op + 1, &TOP);
Tom Tromey's avatar
Tom Tromey committed
815
	    NEXT;
816
	  }
Jim Blandy's avatar
Jim Blandy committed
817

Tom Tromey's avatar
Tom Tromey committed
818
	CASE (Bunbind6):
Jim Blandy's avatar
Jim Blandy committed
819 820 821
	  op = FETCH;
	  goto dounbind;

Tom Tromey's avatar
Tom Tromey committed
822
	CASE (Bunbind7):
Jim Blandy's avatar
Jim Blandy committed
823 824 825
	  op = FETCH2;
	  goto dounbind;

Tom Tromey's avatar
Tom Tromey committed
826 827 828 829 830 831
	CASE (Bunbind):
	CASE (Bunbind1):
	CASE (Bunbind2):
	CASE (Bunbind3):
	CASE (Bunbind4):
	CASE (Bunbind5):
Jim Blandy's avatar
Jim Blandy committed
832 833
	  op -= Bunbind;
	dounbind:
Juanma Barranquero's avatar
Juanma Barranquero committed
834
	  unbind_to (SPECPDL_INDEX () - op, Qnil);
Tom Tromey's avatar
Tom Tromey committed
835
	  NEXT;
Jim Blandy's avatar
Jim Blandy committed
836

Tom Tromey's avatar
Tom Tromey committed
837
	CASE (Bunbind_all):	/* Obsolete.  Never used.  */
Jim Blandy's avatar
Jim Blandy committed
838
	  /* To unbind back to the beginning of this frame.  Not used yet,
839
	     but will be needed for tail-recursion elimination.  */
Jim Blandy's avatar
Jim Blandy committed
840
	  unbind_to (count, Qnil);
Tom Tromey's avatar
Tom Tromey committed
841
	  NEXT;
Jim Blandy's avatar
Jim Blandy committed
842

Tom Tromey's avatar
Tom Tromey committed
843
	CASE (Bgoto):
Paul Eggert's avatar
Paul Eggert committed
844
	  maybe_gc ();
Gerd Moellmann's avatar
Gerd Moellmann committed
845
	  BYTE_CODE_QUIT;
Jim Blandy's avatar
Jim Blandy committed
846
	  op = FETCH2;    /* pc = FETCH2 loses since FETCH2 contains pc++ */
847
	  CHECK_RANGE (op);
Gerd Moellmann's avatar
Gerd Moellmann committed
848
	  stack.pc = stack.byte_string_start + op;
Tom Tromey's avatar
Tom Tromey committed
849
	  NEXT;
Jim Blandy's avatar
Jim Blandy committed
850

Tom Tromey's avatar
Tom Tromey committed
851
	CASE (Bgotoifnonnil):
852 853
	  {
	    Lisp_Object v1;
Paul Eggert's avatar
Paul Eggert committed
854
	    maybe_gc ();
855 856 857 858 859 860 861 862
	    op = FETCH2;
	    v1 = POP;
	    if (!NILP (v1))
	      {
		BYTE_CODE_QUIT;
		CHECK_RANGE (op);
		stack.pc = stack.byte_string_start + op;
	      }
Tom Tromey's avatar
Tom Tromey committed
863
	    NEXT;
864
	  }
Jim Blandy's avatar
Jim Blandy committed
865

Tom Tromey's avatar
Tom Tromey committed
866
	CASE (Bgotoifnilelsepop):
Paul Eggert's avatar
Paul Eggert committed
867
	  maybe_gc ();
Jim Blandy's avatar
Jim Blandy committed
868
	  op = FETCH2;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
869
	  if (NILP (TOP))
Jim Blandy's avatar
Jim Blandy committed
870
	    {
Gerd Moellmann's avatar
Gerd Moellmann committed
871
	      BYTE_CODE_QUIT;
872
	      CHECK_RANGE (op);
Gerd Moellmann's avatar
Gerd Moellmann committed
873
	      stack.pc = stack.byte_string_start + op;
Jim Blandy's avatar
Jim Blandy committed
874
	    }
875
	  else DISCARD (1);
Tom Tromey's avatar
Tom Tromey committed
876
	  NEXT;
Jim Blandy's avatar
Jim Blandy committed
877

Tom Tromey's avatar
Tom Tromey committed
878
	CASE (Bgotoifnonnilelsepop):
Paul Eggert's avatar
Paul Eggert committed
879
	  maybe_gc ();
Jim Blandy's avatar
Jim Blandy committed
880
	  op = FETCH2;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
881
	  if (!NILP (TOP))
Jim Blandy's avatar
Jim Blandy committed
882
	    {
Gerd Moellmann's avatar
Gerd Moellmann committed
883
	      BYTE_CODE_QUIT;
884
	      CHECK_RANGE (op);
Gerd Moellmann's avatar
Gerd Moellmann committed
885
	      stack.pc = stack.byte_string_start + op;
Jim Blandy's avatar
Jim Blandy committed
886
	    }
887
	  else DISCARD (1);
Tom Tromey's avatar
Tom Tromey committed
888
	  NEXT;
889

Tom Tromey's avatar
Tom Tromey committed
890
	CASE (BRgoto):
Paul Eggert's avatar
Paul Eggert committed
891
	  maybe_gc ();
Gerd Moellmann's avatar
Gerd Moellmann committed
892
	  BYTE_CODE_QUIT;
Gerd Moellmann's avatar
Gerd Moellmann committed
893
	  stack.pc += (int) *stack.pc - 127;
Tom Tromey's avatar
Tom Tromey committed
894
	  NEXT;
895

Tom Tromey's avatar
Tom Tromey committed
896
	CASE (BRgotoifnil):
897 898
	  {
	    Lisp_Object v1;
Paul Eggert's avatar
Paul Eggert committed
899
	    maybe_gc ();
900 901 902 903 904 905 906
	    v1 = POP;
	    if (NILP (v1))
	      {
		BYTE_CODE_QUIT;
		stack.pc += (int) *stack.pc - 128;
	      }
	    stack.pc++;
Tom Tromey's avatar
Tom Tromey committed
907
	    NEXT;
908
	  }
909

Tom Tromey's avatar
Tom Tromey committed
910
	CASE (BRgotoifnonnil):
911 912
	  {
	    Lisp_Object v1;
Paul Eggert's avatar
Paul Eggert committed
913
	    maybe_gc ();
914 915 916 917 918 919 920
	    v1 = POP;
	    if (!NILP (v1))
	      {
		BYTE_CODE_QUIT;
		stack.pc += (int) *stack.pc - 128;
	      }
	    stack.pc++;
Tom Tromey's avatar
Tom Tromey committed
921
	    NEXT;
922
	  }
923

Tom Tromey's avatar
Tom Tromey committed
924
	CASE (BRgotoifnilelsepop):
Paul Eggert's avatar
Paul Eggert committed
925
	  maybe_gc ();
Gerd Moellmann's avatar
Gerd Moellmann committed
926
	  op = *stack.pc++;
927 928
	  if (NILP (TOP))
	    {
Gerd Moellmann's avatar
Gerd Moellmann committed
929
	      BYTE_CODE_QUIT;
Gerd Moellmann's avatar
Gerd Moellmann committed
930
	      stack.pc += op - 128;
931 932
	    }
	  else DISCARD (1);
Tom Tromey's avatar
Tom Tromey committed
933
	  NEXT;
934

Tom Tromey's avatar
Tom Tromey committed
935
	CASE (BRgotoifnonnilelsepop):
Paul Eggert's avatar
Paul Eggert committed
936
	  maybe_gc ();
Gerd Moellmann's avatar
Gerd Moellmann committed
937
	  op = *stack.pc++;
938 939
	  if (!NILP (TOP))
	    {
Gerd Moellmann's avatar
Gerd Moellmann committed
940
	      BYTE_CODE_QUIT;
Gerd Moellmann's avatar
Gerd Moellmann committed
941
	      stack.pc += op - 128;
942 943
	    }
	  else DISCARD (1);
Tom Tromey's avatar
Tom Tromey committed
944
	  NEXT;
Jim Blandy's avatar
Jim Blandy committed
945

Tom Tromey's avatar
Tom Tromey committed
946
	CASE (Breturn):
947
	  result = POP;
Jim Blandy's avatar
Jim Blandy committed
948 949
	  goto exit;

Tom Tromey's avatar
Tom Tromey committed
950
	CASE (Bdiscard):
951
	  DISCARD (1);
Tom Tromey's avatar
Tom Tromey committed
952
	  NEXT;
Jim Blandy's avatar
Jim Blandy committed
953

Tom Tromey's avatar
Tom Tromey committed
954
	CASE (Bconstant2):
Jim Blandy's avatar
Jim Blandy committed
955
	  PUSH (vectorp[FETCH2]);
Tom Tromey's avatar
Tom Tromey committed
956
	  NEXT;
Jim Blandy's avatar
Jim Blandy committed
957

Tom Tromey's avatar
Tom Tromey committed
958
	CASE (Bsave_excursion):
959 960
	  record_unwind_protect (save_excursion_restore,
				 save_excursion_save ());
Tom Tromey's avatar
Tom Tromey committed
961
	  NEXT;
Jim Blandy's avatar
Jim Blandy committed
962

Tom Tromey's avatar
Tom Tromey committed
963 964
	CASE (Bsave_current_buffer): /* Obsolete since ??.  */
	CASE (Bsave_current_buffer_1):
965
	  record_unwind_current_buffer ();
Tom Tromey's avatar
Tom Tromey committed
966
	  NEXT;
967

Tom Tromey's avatar
Tom Tromey committed
968
	CASE (Bsave_window_excursion): /* Obsolete since 24.1.  */
969
	  {
970 971
	    ptrdiff_t count1 = SPECPDL_INDEX ();
	    record_unwind_protect (restore_window_configuration,
972 973
				   Fcurrent_window_configuration (Qnil));
	    TOP = Fprogn (TOP);
974
	    unbind_to (count1, TOP);
Tom Tromey's avatar
Tom Tromey committed
975
	    NEXT;
976
	  }
Jim Blandy's avatar
Jim Blandy committed
977

Tom Tromey's avatar
Tom Tromey committed
978
	CASE (Bsave_restriction):
979 980
	  record_unwind_protect (save_restriction_restore,
				 save_restriction_save ());
Tom Tromey's avatar
Tom Tromey committed
981
	  NEXT;
Jim Blandy's avatar
Jim Blandy committed
982

983
	CASE (Bcatch):		/* Obsolete since 24.4.  */
984 985
	  {
	    Lisp_Object v1;
986
	    v1 = POP;
987
	    TOP = internal_catch (TOP, eval_sub, v1);
Tom Tromey's avatar
Tom Tromey committed
988
	    NEXT;
989
	  }
Jim Blandy's avatar
Jim Blandy committed
990

991 992 993 994
	CASE (Bpushcatch):	/* New in 24.4.  */
	  type = CATCHER;
	  goto pushhandler;
	CASE (Bpushconditioncase): /* New in 24.4.  */
995 996
	  type = CONDITION_CASE;
	pushhandler:
997
	  {
998 999
	    Lisp_Object tag = POP;
	    int dest = FETCH2;
1000

1001
	    struct handler *c = push_handler (tag, type);
1002 1003
	    c->bytecode_dest = dest;
	    c->bytecode_top = top;
1004

1005 1006