Fix -Wimplicit warnings.
[bpt/emacs.git] / src / bytecode.c
index d8de7eb..84d9ec6 100644 (file)
@@ -1,11 +1,11 @@
 /* Execution of byte code produced by bytecomp.el.
-   Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc.
+   Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
 GNU Emacs is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 1, or (at your option)
+the Free Software Foundation; either version 2, or (at your option)
 any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
@@ -15,7 +15,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.
 
 hacked on by jwz@lucid.com 17-jun-91
   o  added a compile-time switch to turn on simple sanity checking;
@@ -32,7 +33,7 @@ by Hallvard:
   o  all conditionals now only do QUIT if they jump.
  */
 
-#include "config.h"
+#include <config.h>
 #include "lisp.h"
 #include "buffer.h"
 #include "syntax.h"
@@ -65,7 +66,7 @@ int byte_metering_on;
       if (METER_1 (this_code) != ((1<<VALBITS)-1))             \
         METER_1 (this_code)++;                                 \
       if (last_code                                            \
-          && METER_2 (last_code, this_code) != ((1<<VALBITS)-1))       \
+         && METER_2 (last_code, this_code) != ((1<<VALBITS)-1))\
         METER_2 (last_code, this_code)++;                      \
     }                                                          \
 }
@@ -106,9 +107,9 @@ Lisp_Object Qbytecode;
 #define Baref 0110
 #define Baset 0111
 #define Bsymbol_value 0112
-#define Bsymbol_function 0113 /* no longer generated as of v19 */
+#define Bsymbol_function 0113
 #define Bset 0114
-#define Bfset 0115 /* no longer generated as of v19 */
+#define Bfset 0115
 #define Bget 0116
 #define Bsubstring 0117
 #define Bconcat2 0120
@@ -129,7 +130,8 @@ Lisp_Object Qbytecode;
 #define Bmult 0137
 
 #define Bpoint 0140
-#define Bmark 0141 /* no longer generated as of v18 */
+/* Was Bmark in v17.  */
+#define Bsave_current_buffer 0141
 #define Bgoto_char 0142
 #define Binsert 0143
 #define Bpoint_max 0144
@@ -146,6 +148,7 @@ Lisp_Object Qbytecode;
 #define Bbobp 0157
 #define Bcurrent_buffer 0160
 #define Bset_buffer 0161
+#define Bsave_current_buffer_1 0162 /* Replacing Bsave_current_buffer.  */
 #define Bread_char 0162 /* No longer generated as of v19 */
 #define Bset_mark 0163 /* this loser is no longer generated as of v18 */
 #define Binteractive_p 0164 /* Needed since interactive-p takes unevalled args */
@@ -249,10 +252,35 @@ Lisp_Object Qbytecode;
 
 #define TOP (*stackp)
 
+/* Garbage collect if we have consed enough since the last time.
+   We do this at every branch, to avoid loops that never GC.  */
+
+#define MAYBE_GC()                             \
+  if (consing_since_gc > gc_cons_threshold)    \
+    {                                          \
+      Fgarbage_collect ();                     \
+      HANDLE_RELOCATION ();                    \
+    }                                          \
+  else
+
+/* Relocate BYTESTR if there has been a GC recently.  */
+#define HANDLE_RELOCATION()                                            \
+  if (! EQ (string_saved, bytestr))                                    \
+    {                                                                  \
+      pc = pc - XSTRING (string_saved)->data + XSTRING (bytestr)->data;        \
+      string_saved = bytestr;                                          \
+    }                                                                  \
+  else
+
+/* Check for jumping out of range.  */
+#define CHECK_RANGE(ARG)                       \
+  if (ARG >= bytestr_length) abort ()
+
 DEFUN ("byte-code", Fbyte_code, Sbyte_code, 3, 3, 0,
   "Function used internally in byte-compiled code.\n\
-The first argument is a string of byte code; the second, a vector of constants;\n\
-the third, the maximum stack depth used in this function.\n\
+The first argument, BYTESTR, is a string of byte code;\n\
+the second, VECTOR, a vector of constants;\n\
+the third, MAXDEPTH, the maximum stack depth used in this function.\n\
 If the third argument is incorrect, Emacs may crash.")
   (bytestr, vector, maxdepth)
      Lisp_Object bytestr, vector, maxdepth;
@@ -278,9 +306,10 @@ If the third argument is incorrect, Emacs may crash.")
   /* Cached address of beginning of string,
      valid if BYTESTR equals STRING_SAVED.  */
   register unsigned char *strbeg;
+  int bytestr_length = STRING_BYTES (XSTRING (bytestr));
 
   CHECK_STRING (bytestr, 0);
-  if (XTYPE (vector) != Lisp_Vector)
+  if (!VECTORP (vector))
     vector = wrong_type_argument (Qvectorp, vector);
   CHECK_NUMBER (maxdepth, 2);
 
@@ -308,11 +337,8 @@ If the third argument is incorrect, Emacs may crash.")
               pc - XSTRING (string_saved)->data);
 #endif
 
-      if (string_saved != bytestr)
-       {
-         pc = pc - XSTRING (string_saved)->data + XSTRING (bytestr)->data;
-         string_saved = bytestr;
-       }
+      /* Update BYTESTR if we had a garbage collection.  */
+      HANDLE_RELOCATION ();
 
 #ifdef BYTE_CODE_METER
       prev_op = this_op;
@@ -336,29 +362,13 @@ If the third argument is incorrect, Emacs may crash.")
          op = op - Bvarref;
        varref:
          v1 = vectorp[op];
-         if (XTYPE (v1) != Lisp_Symbol)
+         if (!SYMBOLP (v1))
            v2 = Fsymbol_value (v1);
          else
            {
              v2 = XSYMBOL (v1)->value;
-#ifdef SWITCH_ENUM_BUG
-             switch ((int) XTYPE (v2))
-#else
-             switch (XTYPE (v2))
-#endif
-               {
-               case Lisp_Symbol:
-                 if (!EQ (v2, Qunbound))
-                   break;
-               case Lisp_Intfwd:
-               case Lisp_Boolfwd:
-               case Lisp_Objfwd:
-               case Lisp_Buffer_Local_Value:
-               case Lisp_Some_Buffer_Local_Value:
-               case Lisp_Buffer_Objfwd:
-               case Lisp_Void:
-                 v2 = Fsymbol_value (v1);
-               }
+             if (MISCP (v2) || EQ (v2, Qunbound))
+               v2 = Fsymbol_value (v1);
            }
          PUSH (v2);
          break;
@@ -407,25 +417,19 @@ If the third argument is incorrect, Emacs may crash.")
        docall:
          DISCARD (op);
 #ifdef BYTE_CODE_METER
-         if (byte_metering_on && XTYPE (TOP) == Lisp_Symbol)
+         if (byte_metering_on && SYMBOLP (TOP))
            {
              v1 = TOP;
              v2 = Fget (v1, Qbyte_code_meter);
-             if (XTYPE (v2) == Lisp_Int)
+             if (INTEGERP (v2)
+                 && XINT (v2) != ((1<<VALBITS)-1))
                {
                  XSETINT (v2, XINT (v2) + 1);
                  Fput (v1, Qbyte_code_meter, v2);
                }
            }
 #endif
-         /* The frobbing of gcpro3 was lost by jwz's changes in June 91
-            and then reinserted by jwz in Nov 91.  */
-         /* Remove protection from the args we are giving to Ffuncall.
-            FFuncall will protect them, and double protection would
-            cause disasters.  */
-         gcpro3.nvars = &TOP - stack - 1;
          TOP = Ffuncall (op + 1, &TOP);
-         gcpro3.nvars = XFASTINT (maxdepth);
          break;
 
        case Bunbind+6:
@@ -450,75 +454,89 @@ If the third argument is incorrect, Emacs may crash.")
          break;
 
        case Bgoto:
+         MAYBE_GC ();
          QUIT;
          op = FETCH2;    /* pc = FETCH2 loses since FETCH2 contains pc++ */
+         CHECK_RANGE (op);
          pc = XSTRING (string_saved)->data + op;
          break;
 
        case Bgotoifnil:
+         MAYBE_GC ();
          op = FETCH2;
-         if (NULL (POP))
+         if (NILP (POP))
            {
              QUIT;
+             CHECK_RANGE (op);
              pc = XSTRING (string_saved)->data + op;
            }
          break;
 
        case Bgotoifnonnil:
+         MAYBE_GC ();
          op = FETCH2;
-         if (!NULL (POP))
+         if (!NILP (POP))
            {
              QUIT;
+             CHECK_RANGE (op);
              pc = XSTRING (string_saved)->data + op;
            }
          break;
 
        case Bgotoifnilelsepop:
+         MAYBE_GC ();
          op = FETCH2;
-         if (NULL (TOP))
+         if (NILP (TOP))
            {
              QUIT;
+             CHECK_RANGE (op);
              pc = XSTRING (string_saved)->data + op;
            }
          else DISCARD (1);
          break;
 
        case Bgotoifnonnilelsepop:
+         MAYBE_GC ();
          op = FETCH2;
-         if (!NULL (TOP))
+         if (!NILP (TOP))
            {
              QUIT;
+             CHECK_RANGE (op);
              pc = XSTRING (string_saved)->data + op;
            }
          else DISCARD (1);
          break;
 
        case BRgoto:
+         MAYBE_GC ();
          QUIT;
-         pc += *pc - 127;
+         pc += (int) *pc - 127;
          break;
 
        case BRgotoifnil:
-         if (NULL (POP))
+         MAYBE_GC ();
+         if (NILP (POP))
            {
              QUIT;
-             pc += *pc - 128;
+             pc += (int) *pc - 128;
            }
          pc++;
          break;
 
        case BRgotoifnonnil:
-         if (!NULL (POP))
+         MAYBE_GC ();
+         if (!NILP (POP))
            {
              QUIT;
-             pc += *pc - 128;
+             pc += (int) *pc - 128;
            }
          pc++;
          break;
 
        case BRgotoifnilelsepop:
+         MAYBE_GC ();
          op = *pc++;
-         if (NULL (TOP))
+         if (NILP (TOP))
            {
              QUIT;
              pc += op - 128;
@@ -527,8 +545,9 @@ If the third argument is incorrect, Emacs may crash.")
          break;
 
        case BRgotoifnonnilelsepop:
+         MAYBE_GC ();
          op = *pc++;
-         if (!NULL (TOP))
+         if (!NILP (TOP))
            {
              QUIT;
              pc += op - 128;
@@ -557,6 +576,11 @@ If the third argument is incorrect, Emacs may crash.")
          record_unwind_protect (save_excursion_restore, save_excursion_save ());
          break;
 
+       case Bsave_current_buffer:
+       case Bsave_current_buffer_1:
+         record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
+         break;
+
        case Bsave_window_excursion:
          TOP = Fsave_window_excursion (TOP);
          break;
@@ -588,7 +612,7 @@ If the third argument is incorrect, Emacs may crash.")
 
        case Btemp_output_buffer_show:
          v1 = POP;
-         temp_output_buffer_show (TOP, Qnil);
+         temp_output_buffer_show (TOP);
          TOP = v1;
          /* pop binding of standard-output */
          unbind_to (specpdl_ptr - specpdl - 1, Qnil);
@@ -605,7 +629,7 @@ If the third argument is incorrect, Emacs may crash.")
            {
              if (CONSP (v1))
                v1 = XCONS (v1)->cdr;
-             else if (!NULL (v1))
+             else if (!NILP (v1))
                {
                  immediate_quit = 0;
                  v1 = wrong_type_argument (Qlistp, v1);
@@ -617,7 +641,7 @@ If the third argument is incorrect, Emacs may crash.")
          goto docar;
 
        case Bsymbolp:
-         TOP = XTYPE (TOP) == Lisp_Symbol ? Qt : Qnil;
+         TOP = SYMBOLP (TOP) ? Qt : Qnil;
          break;
 
        case Bconsp:
@@ -625,11 +649,11 @@ If the third argument is incorrect, Emacs may crash.")
          break;
 
        case Bstringp:
-         TOP = XTYPE (TOP) == Lisp_String ? Qt : Qnil;
+         TOP = STRINGP (TOP) ? Qt : Qnil;
          break;
 
        case Blistp:
-         TOP = CONSP (TOP) || NULL (TOP) ? Qt : Qnil;
+         TOP = CONSP (TOP) || NILP (TOP) ? Qt : Qnil;
          break;
 
        case Beq:
@@ -643,21 +667,21 @@ If the third argument is incorrect, Emacs may crash.")
          break;
 
        case Bnot:
-         TOP = NULL (TOP) ? Qt : Qnil;
+         TOP = NILP (TOP) ? Qt : Qnil;
          break;
 
        case Bcar:
          v1 = TOP;
        docar:
          if (CONSP (v1)) TOP = XCONS (v1)->car;
-         else if (NULL (v1)) TOP = Qnil;
+         else if (NILP (v1)) TOP = Qnil;
          else Fcar (wrong_type_argument (Qlistp, v1));
          break;
 
        case Bcdr:
          v1 = TOP;
          if (CONSP (v1)) TOP = XCONS (v1)->cdr;
-         else if (NULL (v1)) TOP = Qnil;
+         else if (NILP (v1)) TOP = Qnil;
          else Fcdr (wrong_type_argument (Qlistp, v1));
          break;
 
@@ -756,7 +780,7 @@ If the third argument is incorrect, Emacs may crash.")
 
        case Bsub1:
          v1 = TOP;
-         if (XTYPE (v1) == Lisp_Int)
+         if (INTEGERP (v1))
            {
              XSETINT (v1, XINT (v1) - 1);
              TOP = v1;
@@ -767,7 +791,7 @@ If the third argument is incorrect, Emacs may crash.")
 
        case Badd1:
          v1 = TOP;
-         if (XTYPE (v1) == Lisp_Int)
+         if (INTEGERP (v1))
            {
              XSETINT (v1, XINT (v1) + 1);
              TOP = v1;
@@ -780,7 +804,18 @@ If the third argument is incorrect, Emacs may crash.")
          v2 = POP; v1 = TOP;
          CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v1, 0);
          CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (v2, 0);
-         TOP = (XFLOATINT (v1) == XFLOATINT (v2)) ? Qt : Qnil;
+#ifdef LISP_FLOAT_TYPE
+         if (FLOATP (v1) || FLOATP (v2))
+           {
+             double f1, f2;
+
+             f1 = (FLOATP (v1) ? XFLOAT (v1)->data : XINT (v1));
+             f2 = (FLOATP (v2) ? XFLOAT (v2)->data : XINT (v2));
+             TOP = (f1 == f2 ? Qt : Qnil);
+           }
+         else
+#endif
+           TOP = (XINT (v1) == XINT (v2) ? Qt : Qnil);
          break;
 
        case Bgtr:
@@ -810,7 +845,7 @@ If the third argument is incorrect, Emacs may crash.")
 
        case Bnegate:
          v1 = TOP;
-         if (XTYPE (v1) == Lisp_Int)
+         if (INTEGERP (v1))
            {
              XSETINT (v1, - XINT (v1));
              TOP = v1;
@@ -850,7 +885,7 @@ If the third argument is incorrect, Emacs may crash.")
          break;
 
        case Bpoint:
-         XFASTINT (v1) = point;
+         XSETFASTINT (v1, PT);
          PUSH (v1);
          break;
 
@@ -869,12 +904,12 @@ If the third argument is incorrect, Emacs may crash.")
          break;
 
        case Bpoint_max:
-         XFASTINT (v1) = ZV;
+         XSETFASTINT (v1, ZV);
          PUSH (v1);
          break;
 
        case Bpoint_min:
-         XFASTINT (v1) = BEGV;
+         XSETFASTINT (v1, BEGV);
          PUSH (v1);
          break;
 
@@ -883,17 +918,17 @@ If the third argument is incorrect, Emacs may crash.")
          break;
 
        case Bfollowing_char:
-         XFASTINT (v1) = PT == ZV ? 0 : FETCH_CHAR (point);
+         v1 = Ffollowing_char ();
          PUSH (v1);
          break;
 
        case Bpreceding_char:
-         XFASTINT (v1) = point <= BEGV ? 0 : FETCH_CHAR (point - 1);
+         v1 = Fprevious_char ();
          PUSH (v1);
          break;
 
        case Bcurrent_column:
-         XFASTINT (v1) = current_column ();
+         XSETFASTINT (v1, current_column ());
          PUSH (v1);
          break;
 
@@ -925,11 +960,6 @@ If the third argument is incorrect, Emacs may crash.")
          TOP = Fset_buffer (TOP);
          break;
 
-       case Bread_char:
-         PUSH (Fread_char ());
-         QUIT;
-         break;
-
        case Binteractive_p:
          PUSH (Finteractive_p ());
          break;
@@ -958,7 +988,8 @@ If the third argument is incorrect, Emacs may crash.")
 
        case Bchar_syntax:
          CHECK_NUMBER (TOP, 0);
-         XFASTINT (TOP) = syntax_code_spec[(int) SYNTAX (0xFF & XINT (TOP))];
+         XSETFASTINT (TOP,
+                      syntax_code_spec[(int) SYNTAX (XINT (TOP))]);
          break;
 
        case Bbuffer_substring:
@@ -1027,7 +1058,7 @@ If the third argument is incorrect, Emacs may crash.")
          break;
 
        case Belt:
-         if (XTYPE (TOP) == Lisp_Cons)
+         if (CONSP (TOP))
            {
              /* Exchange args and then do nth.  */
              v2 = POP;
@@ -1064,7 +1095,7 @@ If the third argument is incorrect, Emacs may crash.")
 
        case Bcar_safe:
          v1 = TOP;
-         if (XTYPE (v1) == Lisp_Cons)
+         if (CONSP (v1))
            TOP = XCONS (v1)->car;
          else
            TOP = Qnil;
@@ -1072,7 +1103,7 @@ If the third argument is incorrect, Emacs may crash.")
 
        case Bcdr_safe:
          v1 = TOP;
-         if (XTYPE (v1) == Lisp_Cons)
+         if (CONSP (v1))
            TOP = XCONS (v1)->cdr;
          else
            TOP = Qnil;
@@ -1084,12 +1115,11 @@ If the third argument is incorrect, Emacs may crash.")
          break;
 
        case Bnumberp:
-         TOP = (XTYPE (TOP) == Lisp_Int || XTYPE (TOP) == Lisp_Float
-                ? Qt : Qnil);
+         TOP = (NUMBERP (TOP) ? Qt : Qnil);
          break;
 
        case Bintegerp:
-         TOP = XTYPE (TOP) == Lisp_Int ? Qt : Qnil;
+         TOP = INTEGERP (TOP) ? Qt : Qnil;
          break;
 
 #ifdef BYTE_CODE_SAFE
@@ -1129,6 +1159,7 @@ If the third argument is incorrect, Emacs may crash.")
   return v1;
 }
 
+void
 syms_of_bytecode ()
 {
   Qbytecode = intern ("byte-code");
@@ -1139,8 +1170,17 @@ syms_of_bytecode ()
 #ifdef BYTE_CODE_METER
 
   DEFVAR_LISP ("byte-code-meter", &Vbyte_code_meter,
-   "A vector of vectors which holds a histogram of byte-code usage.");
-  DEFVAR_BOOL ("byte-metering-on", &byte_metering_on, "");
+   "A vector of vectors which holds a histogram of byte-code usage.\n\
+(aref (aref byte-code-meter 0) CODE) indicates how many times the byte\n\
+opcode CODE has been executed.\n\
+(aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,\n\
+indicates how many times the byte opcodes CODE1 and CODE2 have been\n\
+executed in succession.");
+  DEFVAR_BOOL ("byte-metering-on", &byte_metering_on,
+   "If non-nil, keep profiling information on byte code usage.\n\
+The variable byte-code-meter indicates how often each byte opcode is used.\n\
+If a symbol has a property named `byte-code-meter' whose value is an\n\
+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));