* alloc.c (pure_bytes_used_lisp, pure_bytes_used_non_lisp):
[bpt/emacs.git] / src / bytecode.c
index d887668..b6ac6c5 100644 (file)
@@ -51,7 +51,7 @@ by Hallvard:
  *
  * define BYTE_CODE_METER to enable generation of a byte-op usage histogram.
  */
-#define BYTE_CODE_SAFE 1
+/* #define BYTE_CODE_SAFE */
 /* #define BYTE_CODE_METER */
 
 \f
@@ -80,7 +80,6 @@ Lisp_Object Qbyte_code_meter;
 \f
 
 Lisp_Object Qbytecode;
-extern Lisp_Object Qand_optional, Qand_rest;
 
 /*  Byte codes: */
 
@@ -145,7 +144,7 @@ extern Lisp_Object Qand_optional, Qand_rest;
 #define Bcurrent_column 0151
 #define Bindent_to 0152
 #ifdef BYTE_CODE_SAFE
-#define Bscan_buffer 0153 /* No longer generated as of v18 */
+#define Bscan_buffer 0153 /* No longer generated as of v18 */
 #endif
 #define Beolp 0154
 #define Beobp 0155
@@ -160,7 +159,7 @@ extern Lisp_Object Qand_optional, Qand_rest;
 #ifdef BYTE_CODE_SAFE
 #define Bset_mark 0163 /* this loser is no longer generated as of v18 */
 #endif
-#define Binteractive_p 0164 /* Obsolete.  */
+#define Binteractive_p 0164 /* Obsolete since Emacs-24.1.  */
 
 #define Bforward_char 0165
 #define Bforward_word 0166
@@ -185,16 +184,16 @@ extern Lisp_Object Qand_optional, Qand_rest;
 #define Bdup 0211
 
 #define Bsave_excursion 0212
-#define Bsave_window_excursion 0213 /* Obsolete.  */
+#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.  */
-#define Btemp_output_buffer_show 0221  /* Obsolete.  */
+#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.  */
+#define Bunbind_all 0222       /* Obsolete.  Never used.  */
 
 #define Bset_marker 0223
 #define Bmatch_beginning 0224
@@ -413,24 +412,15 @@ unmark_byte_stack (void)
   } while (0)
 
 
-DEFUN ("byte-code", Fbyte_code, Sbyte_code, 3, MANY, 0,
+DEFUN ("byte-code", Fbyte_code, Sbyte_code, 3, 3, 0,
        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.
-
-If ARGS-TEMPLATE is specified, it is an argument list specification,
-according to which any remaining arguments are pushed on the stack
-before executing BYTESTR.
-
-usage: (byte-code BYTESTR VECTOR MAXDEP &optional ARGS-TEMPLATE &rest ARGS) */)
-     (int nargs, Lisp_Object *args)
+If the third argument is incorrect, Emacs may crash.  */)
+  (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth)
 {
-  Lisp_Object args_tmpl = nargs >= 4 ? args[3] : Qnil;
-  int pnargs = nargs >= 4 ? nargs - 4 : 0;
-  Lisp_Object *pargs = nargs >= 4 ? args + 4 : 0;
-  return exec_byte_code (args[0], args[1], args[2], args_tmpl, pnargs, pargs);
+  return exec_byte_code (bytestr, vector, maxdepth, Qnil, 0, NULL);
 }
 
 /* Execute the byte-code in BYTESTR.  VECTOR is the constant vector, and
@@ -443,9 +433,9 @@ usage: (byte-code BYTESTR VECTOR MAXDEP &optional ARGS-TEMPLATE &rest ARGS) */)
 
 Lisp_Object
 exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
-               Lisp_Object args_template, int nargs, Lisp_Object *args)
+               Lisp_Object args_template, ptrdiff_t nargs, Lisp_Object *args)
 {
-  int count = SPECPDL_INDEX ();
+  ptrdiff_t count = SPECPDL_INDEX ();
 #ifdef BYTE_CODE_METER
   int this_op = 0;
   int prev_op;
@@ -454,10 +444,10 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
   /* Lisp_Object v1, v2; */
   Lisp_Object *vectorp;
 #ifdef BYTE_CODE_SAFE
-  int const_length = XVECTOR (vector)->size;
+  ptrdiff_t const_length;
   Lisp_Object *stacke;
-#endif
   int bytestr_length;
+#endif
   struct byte_stack stack;
   Lisp_Object *top;
   Lisp_Object result;
@@ -474,7 +464,11 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
 
   CHECK_STRING (bytestr);
   CHECK_VECTOR (vector);
-  CHECK_NUMBER (maxdepth);
+  CHECK_NATNUM (maxdepth);
+
+#ifdef BYTE_CODE_SAFE
+  const_length = ASIZE (vector);
+#endif
 
   if (STRING_MULTIBYTE (bytestr))
     /* BYTESTR must have been produced by Emacs 20.2 or the earlier
@@ -484,7 +478,9 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
        convert them back to the originally intended unibyte form.  */
     bytestr = Fstring_as_unibyte (bytestr);
 
+#ifdef BYTE_CODE_SAFE
   bytestr_length = SBYTES (bytestr);
+#endif
   vectorp = XVECTOR (vector)->contents;
 
   stack.byte_string = bytestr;
@@ -506,14 +502,14 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
 
   if (INTEGERP (args_template))
     {
-      int at = XINT (args_template);
+      EMACS_INT at = XINT (args_template);
       int rest = at & 128;
       int mandatory = at & 127;
-      int nonrest = at >> 8;
+      EMACS_INT nonrest = at >> 8;
       eassert (mandatory <= nonrest);
       if (nargs <= nonrest)
        {
-         int i;
+         EMACS_INT i;
          for (i = 0 ; i < nargs; i++, args++)
            PUSH (*args);
          if (nargs < mandatory)
@@ -532,7 +528,7 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
        }
       else if (rest)
        {
-         int i;
+         ptrdiff_t i;
          for (i = 0 ; i < nonrest; i++, args++)
            PUSH (*args);
          PUSH (Flist (nargs - nonrest, args));
@@ -631,7 +627,16 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
          {
            Lisp_Object v1;
            v1 = TOP;
-           TOP = CAR (v1);
+           if (CONSP (v1))
+             TOP = XCAR (v1);
+           else if (NILP (v1))
+             TOP = Qnil;
+           else
+             {
+               BEFORE_POTENTIAL_GC ();
+               wrong_type_argument (Qlistp, v1);
+               AFTER_POTENTIAL_GC ();
+             }
            break;
          }
 
@@ -657,7 +662,17 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
          {
            Lisp_Object v1;
            v1 = TOP;
-           TOP = CDR (v1);
+           if (CONSP (v1))
+             TOP = XCDR (v1);
+           else if (NILP (v1))
+             TOP = Qnil;
+           else
+             {
+               BEFORE_POTENTIAL_GC ();
+               wrong_type_argument (Qlistp, v1);
+               AFTER_POTENTIAL_GC ();
+             }
+           break;
            break;
          }
 
@@ -791,7 +806,7 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
          AFTER_POTENTIAL_GC ();
          break;
 
-       case Bunbind_all:       /* Obsolete.  */
+       case Bunbind_all:       /* Obsolete.  Never used.  */
          /* To unbind back to the beginning of this frame.  Not used yet,
             but will be needed for tail-recursion elimination.  */
          BEFORE_POTENTIAL_GC ();
@@ -919,19 +934,19 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
                                 save_excursion_save ());
          break;
 
-       case Bsave_current_buffer: /* Obsolete.  */
+       case Bsave_current_buffer: /* Obsolete since ??.  */
        case Bsave_current_buffer_1:
          record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
          break;
 
-       case Bsave_window_excursion: /* Obsolete.  */
+       case Bsave_window_excursion: /* Obsolete since 24.1.  */
          {
-           register int count = SPECPDL_INDEX ();
+           register ptrdiff_t count1 = SPECPDL_INDEX ();
            record_unwind_protect (Fset_window_configuration,
                                   Fcurrent_window_configuration (Qnil));
            BEFORE_POTENTIAL_GC ();
            TOP = Fprogn (TOP);
-           unbind_to (count, TOP);
+           unbind_to (count1, TOP);
            AFTER_POTENTIAL_GC ();
            break;
          }
@@ -941,7 +956,7 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
                                 save_restriction_save ());
          break;
 
-       case Bcatch:            /* FIXME: ill-suited for lexbind */
+       case Bcatch:            /* FIXME: ill-suited for lexbind */
          {
            Lisp_Object v1;
            BEFORE_POTENTIAL_GC ();
@@ -951,11 +966,11 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
            break;
          }
 
-       case Bunwind_protect:   /* FIXME: avoid closure for lexbind */
+       case Bunwind_protect:   /* FIXME: avoid closure for lexbind */
          record_unwind_protect (Fprogn, POP);
          break;
 
-       case Bcondition_case:   /* FIXME: ill-suited for lexbind */
+       case Bcondition_case:   /* FIXME: ill-suited for lexbind */
          {
            Lisp_Object handlers, body;
            handlers = POP;
@@ -966,7 +981,7 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
            break;
          }
 
-       case Btemp_output_buffer_setup: /* Obsolete.  */
+       case Btemp_output_buffer_setup: /* Obsolete since 24.1.  */
          BEFORE_POTENTIAL_GC ();
          CHECK_STRING (TOP);
          temp_output_buffer_setup (SSDATA (TOP));
@@ -974,7 +989,7 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
          TOP = Vstandard_output;
          break;
 
-       case Btemp_output_buffer_show: /* Obsolete.  */
+       case Btemp_output_buffer_show: /* Obsolete since 24.1.  */
          {
            Lisp_Object v1;
            BEFORE_POTENTIAL_GC ();
@@ -990,17 +1005,18 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
        case Bnth:
          {
            Lisp_Object v1, v2;
+           EMACS_INT n;
            BEFORE_POTENTIAL_GC ();
            v1 = POP;
            v2 = TOP;
            CHECK_NUMBER (v2);
-           AFTER_POTENTIAL_GC ();
-           op = XINT (v2);
+           n = XINT (v2);
            immediate_quit = 1;
-           while (--op >= 0 && CONSP (v1))
+           while (--n >= 0 && CONSP (v1))
              v1 = XCDR (v1);
            immediate_quit = 0;
            TOP = CAR (v1);
+           AFTER_POTENTIAL_GC ();
            break;
          }
 
@@ -1446,7 +1462,7 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
          AFTER_POTENTIAL_GC ();
          break;
 
-       case Binteractive_p:    /* Obsolete.  */
+       case Binteractive_p:    /* Obsolete since 24.1.  */
          PUSH (Finteractive_p ());
          break;
 
@@ -1623,14 +1639,15 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
            if (CONSP (TOP))
              {
                /* Exchange args and then do nth.  */
+               EMACS_INT n;
                BEFORE_POTENTIAL_GC ();
                v2 = POP;
                v1 = TOP;
                CHECK_NUMBER (v2);
                AFTER_POTENTIAL_GC ();
-               op = XINT (v2);
+               n = XINT (v2);
                immediate_quit = 1;
-               while (--op >= 0 && CONSP (v1))
+               while (--n >= 0 && CONSP (v1))
                  v1 = XCDR (v1);
                immediate_quit = 0;
                TOP = CAR (v1);
@@ -1764,8 +1781,8 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
            PUSH (*ptr);
            break;
          }
-         /* stack-set-0 = discard; stack-set-1 = discard-1-preserve-tos.  */
        case Bstack_set:
+         /* stack-set-0 = discard; stack-set-1 = discard-1-preserve-tos.  */
          {
            Lisp_Object *ptr = top - (FETCH);
            *ptr = POP;
@@ -1823,8 +1840,7 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
 void
 syms_of_bytecode (void)
 {
-  Qbytecode = intern_c_string ("byte-code");
-  staticpro (&Qbytecode);
+  DEFSYM (Qbytecode, "byte-code");
 
   defsubr (&Sbyte_code);
 
@@ -1846,8 +1862,7 @@ integer, it is incremented each time that symbol's function is called.  */);
 
   byte_metering_on = 0;
   Vbyte_code_meter = Fmake_vector (make_number (256), make_number (0));
-  Qbyte_code_meter = intern_c_string ("byte-code-meter");
-  staticpro (&Qbyte_code_meter);
+  DEFSYM (Qbyte_code_meter, "byte-code-meter");
   {
     int i = 256;
     while (i--)