lisp.h 156 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Fundamental definitions for GNU Emacs Lisp interpreter.
2

Paul Eggert's avatar
Paul Eggert committed
3
Copyright (C) 1985-1987, 1993-1995, 1997-2015 Free Software Foundation,
4
Inc.
Jim Blandy's avatar
Jim Blandy committed
5 6 7

This file is part of GNU Emacs.

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

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
19
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
20

Kenichi Handa's avatar
Kenichi Handa committed
21 22 23
#ifndef EMACS_LISP_H
#define EMACS_LISP_H

24
#include <setjmp.h>
25
#include <stdalign.h>
26
#include <stdarg.h>
27
#include <stddef.h>
28
#include <float.h>
29
#include <inttypes.h>
30
#include <limits.h>
31

32
#include <intprops.h>
Paul Eggert's avatar
Paul Eggert committed
33
#include <verify.h>
34

35 36
INLINE_HEADER_BEGIN

37 38 39
/* Define a TYPE constant ID as an externally visible name.  Use like this:

      DEFINE_GDB_SYMBOL_BEGIN (TYPE, ID)
40
      # define ID (some integer preprocessor expression of type TYPE)
41 42 43
      DEFINE_GDB_SYMBOL_END (ID)

   This hack is for the benefit of compilers that do not make macro
44 45
   definitions or enums visible to the debugger.  It's used for symbols
   that .gdbinit needs.  */
46

47
#define DECLARE_GDB_SYM(type, id) type const id EXTERNALLY_VISIBLE
48
#ifdef MAIN_PROGRAM
49
# define DEFINE_GDB_SYMBOL_BEGIN(type, id) DECLARE_GDB_SYM (type, id)
50 51
# define DEFINE_GDB_SYMBOL_END(id) = id;
#else
52 53
# define DEFINE_GDB_SYMBOL_BEGIN(type, id) extern DECLARE_GDB_SYM (type, id)
# define DEFINE_GDB_SYMBOL_END(val) ;
54 55
#endif

56 57 58 59 60 61
/* The ubiquitous max and min macros.  */
#undef min
#undef max
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

62 63
/* Number of elements in an array.  */
#define ARRAYELTS(arr) (sizeof (arr) / sizeof (arr)[0])
64

Paul Eggert's avatar
Paul Eggert committed
65 66 67 68 69 70 71 72 73 74 75
/* Number of bits in a Lisp_Object tag.  */
DEFINE_GDB_SYMBOL_BEGIN (int, GCTYPEBITS)
#define GCTYPEBITS 3
DEFINE_GDB_SYMBOL_END (GCTYPEBITS)

/* The number of bits needed in an EMACS_INT over and above the number
   of bits in a pointer.  This is 0 on systems where:
   1.  We can specify multiple-of-8 alignment on static variables.
   2.  We know malloc returns a multiple of 8.  */
#if (defined alignas \
     && (defined GNU_MALLOC || defined DOUG_LEA_MALLOC || defined __GLIBC__ \
76 77
	 || defined DARWIN_OS || defined __sun || defined __MINGW32__ \
	 || defined CYGWIN))
Paul Eggert's avatar
Paul Eggert committed
78 79 80 81 82
# define NONPOINTER_BITS 0
#else
# define NONPOINTER_BITS GCTYPEBITS
#endif

Paul Eggert's avatar
Paul Eggert committed
83 84 85 86
/* EMACS_INT - signed integer wide enough to hold an Emacs value
   EMACS_INT_MAX - maximum value of EMACS_INT; can be used in #if
   pI - printf length modifier for EMACS_INT
   EMACS_UINT - unsigned variant of EMACS_INT */
87
#ifndef EMACS_INT_MAX
88 89
# if INTPTR_MAX <= 0
#  error "INTPTR_MAX misconfigured"
Paul Eggert's avatar
Paul Eggert committed
90
# elif INTPTR_MAX <= INT_MAX >> NONPOINTER_BITS && !defined WIDE_EMACS_INT
91 92 93 94
typedef int EMACS_INT;
typedef unsigned int EMACS_UINT;
#  define EMACS_INT_MAX INT_MAX
#  define pI ""
Paul Eggert's avatar
Paul Eggert committed
95
# elif INTPTR_MAX <= LONG_MAX >> NONPOINTER_BITS && !defined WIDE_EMACS_INT
96
typedef long int EMACS_INT;
97
typedef unsigned long EMACS_UINT;
Paul Eggert's avatar
Paul Eggert committed
98
#  define EMACS_INT_MAX LONG_MAX
99
#  define pI "l"
Paul Eggert's avatar
Paul Eggert committed
100 101
/* Check versus LLONG_MAX, not LLONG_MAX >> NONPOINTER_BITS.
   In theory this is not safe, but in practice it seems to be OK.  */
102 103 104 105 106
# elif INTPTR_MAX <= LLONG_MAX
typedef long long int EMACS_INT;
typedef unsigned long long int EMACS_UINT;
#  define EMACS_INT_MAX LLONG_MAX
#  define pI "ll"
107
# else
108
#  error "INTPTR_MAX too large"
109
# endif
110
#endif
Paul Eggert's avatar
Paul Eggert committed
111

112 113 114 115 116 117 118
/* Number of bits to put in each character in the internal representation
   of bool vectors.  This should not vary across implementations.  */
enum {  BOOL_VECTOR_BITS_PER_CHAR =
#define BOOL_VECTOR_BITS_PER_CHAR 8
        BOOL_VECTOR_BITS_PER_CHAR
};

119
/* An unsigned integer type representing a fixed-length bit sequence,
Paul Eggert's avatar
Paul Eggert committed
120
   suitable for bool vector words, GC mark bits, etc.  Normally it is size_t
121
   for speed, but it is unsigned char on weird platforms.  */
122
#if BOOL_VECTOR_BITS_PER_CHAR == CHAR_BIT
123
typedef size_t bits_word;
124
# define BITS_WORD_MAX SIZE_MAX
125 126 127
enum { BITS_PER_BITS_WORD = CHAR_BIT * sizeof (bits_word) };
#else
typedef unsigned char bits_word;
128
# define BITS_WORD_MAX ((1u << BOOL_VECTOR_BITS_PER_CHAR) - 1)
129 130
enum { BITS_PER_BITS_WORD = BOOL_VECTOR_BITS_PER_CHAR };
#endif
131
verify (BITS_WORD_MAX >> (BITS_PER_BITS_WORD - 1) == 1);
132

Paul Eggert's avatar
Paul Eggert committed
133 134 135 136 137 138 139 140
/* Number of bits in some machine integer types.  */
enum
  {
    BITS_PER_CHAR      = CHAR_BIT,
    BITS_PER_SHORT     = CHAR_BIT * sizeof (short),
    BITS_PER_LONG      = CHAR_BIT * sizeof (long int),
    BITS_PER_EMACS_INT = CHAR_BIT * sizeof (EMACS_INT)
  };
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/* printmax_t and uprintmax_t are types for printing large integers.
   These are the widest integers that are supported for printing.
   pMd etc. are conversions for printing them.
   On C99 hosts, there's no problem, as even the widest integers work.
   Fall back on EMACS_INT on pre-C99 hosts.  */
#ifdef PRIdMAX
typedef intmax_t printmax_t;
typedef uintmax_t uprintmax_t;
# define pMd PRIdMAX
# define pMu PRIuMAX
#else
typedef EMACS_INT printmax_t;
typedef EMACS_UINT uprintmax_t;
# define pMd pI"d"
# define pMu pI"u"
#endif

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
/* Use pD to format ptrdiff_t values, which suffice for indexes into
   buffers and strings.  Emacs never allocates objects larger than
   PTRDIFF_MAX bytes, as they cause problems with pointer subtraction.
   In C99, pD can always be "t"; configure it here for the sake of
   pre-C99 libraries such as glibc 2.0 and Solaris 8.  */
#if PTRDIFF_MAX == INT_MAX
# define pD ""
#elif PTRDIFF_MAX == LONG_MAX
# define pD "l"
#elif PTRDIFF_MAX == LLONG_MAX
# define pD "ll"
#else
# define pD "t"
#endif

174
/* Extra internal type checking?  */
175

Paul Eggert's avatar
Paul Eggert committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
/* Define Emacs versions of <assert.h>'s 'assert (COND)' and <verify.h>'s
   'assume (COND)'.  COND should be free of side effects, as it may or
   may not be evaluated.

   'eassert (COND)' checks COND at runtime if ENABLE_CHECKING is
   defined and suppress_checking is false, and does nothing otherwise.
   Emacs dies if COND is checked and is false.  The suppress_checking
   variable is initialized to 0 in alloc.c.  Set it to 1 using a
   debugger to temporarily disable aborting on detected internal
   inconsistencies or error conditions.

   In some cases, a good compiler may be able to optimize away the
   eassert macro even if ENABLE_CHECKING is true, e.g., if XSTRING (x)
   uses eassert to test STRINGP (x), but a particular use of XSTRING
   is invoked only after testing that STRINGP (x) is true, making the
   test redundant.

   eassume is like eassert except that it also causes the compiler to
   assume that COND is true afterwards, regardless of whether runtime
   checking is enabled.  This can improve performance in some cases,
   though it can degrade performance in others.  It's often suboptimal
   for COND to call external functions or access volatile storage.  */

199
#ifndef ENABLE_CHECKING
200
# define eassert(cond) ((void) (false && (cond))) /* Check COND compiles.  */
Paul Eggert's avatar
Paul Eggert committed
201
# define eassume(cond) assume (cond)
202
#else /* ENABLE_CHECKING */
203

204
extern _Noreturn void die (const char *, const char *, int);
205

206
extern bool suppress_checking EXTERNALLY_VISIBLE;
207

208
# define eassert(cond)						\
209
   (suppress_checking || (cond) 				\
210
    ? (void) 0							\
211
    : die (# cond, __FILE__, __LINE__))
Paul Eggert's avatar
Paul Eggert committed
212 213 214 215 216 217
# define eassume(cond)						\
   (suppress_checking						\
    ? assume (cond)						\
    : (cond)							\
    ? (void) 0							\
    : die (# cond, __FILE__, __LINE__))
218
#endif /* ENABLE_CHECKING */
219

220

221 222 223
/* Use the configure flag --enable-check-lisp-object-type to make
   Lisp_Object use a struct type instead of the default int.  The flag
   causes CHECK_LISP_OBJECT_TYPE to be defined.  */
Eli Zaretskii's avatar
Eli Zaretskii committed
224

225
/***** Select the tagging scheme.  *****/
226
/* The following option controls the tagging scheme:
227 228 229 230
   - USE_LSB_TAG means that we can assume the least 3 bits of pointers are
     always 0, and we can thus use them to hold tag bits, without
     restricting our addressing space.

231 232
   If ! USE_LSB_TAG, then use the top 3 bits for tagging, thus
   restricting our possible address range.
233 234 235

   USE_LSB_TAG not only requires the least 3 bits of pointers returned by
   malloc to be 0 but also needs to be able to impose a mult-of-8 alignment
Paul Eggert's avatar
Paul Eggert committed
236 237
   on the few static Lisp_Objects used: lispsym, all the defsubr, and
   the two special buffers buffer_defaults and buffer_local_symbols.  */
238

Paul Eggert's avatar
Paul Eggert committed
239 240
enum Lisp_Bits
  {
241 242
    /* 2**GCTYPEBITS.  This must be a macro that expands to a literal
       integer constant, for MSVC.  */
243 244
#define GCALIGNMENT 8

Paul Eggert's avatar
Paul Eggert committed
245 246
    /* Number of bits in a Lisp_Object value, not counting the tag.  */
    VALBITS = BITS_PER_EMACS_INT - GCTYPEBITS,
247

Paul Eggert's avatar
Paul Eggert committed
248 249 250 251 252 253
    /* Number of bits in a Lisp fixnum tag.  */
    INTTYPEBITS = GCTYPEBITS - 1,

    /* Number of bits in a Lisp fixnum value, not counting the tag.  */
    FIXNUM_BITS = VALBITS + 1
  };
Paul Eggert's avatar
Paul Eggert committed
254

255 256 257 258
#if GCALIGNMENT != 1 << GCTYPEBITS
# error "GCALIGNMENT and GCTYPEBITS are inconsistent"
#endif

Paul Eggert's avatar
Paul Eggert committed
259 260
/* The maximum value that can be stored in a EMACS_INT, assuming all
   bits other than the type bits contribute to a nonnegative signed value.
261 262
   This can be used in #if, e.g., '#if USB_TAG' below expands to an
   expression involving VAL_MAX.  */
Paul Eggert's avatar
Paul Eggert committed
263
#define VAL_MAX (EMACS_INT_MAX >> (GCTYPEBITS - 1))
264

Paul Eggert's avatar
Paul Eggert committed
265
/* Whether the least-significant bits of an EMACS_INT contain the tag.
266
   On hosts where pointers-as-ints do not exceed VAL_MAX / 2, USE_LSB_TAG is:
267 268
    a. unnecessary, because the top bits of an EMACS_INT are unused, and
    b. slower, because it typically requires extra masking.
Paul Eggert's avatar
Paul Eggert committed
269 270
   So, USE_LSB_TAG is true only on hosts where it might be useful.  */
DEFINE_GDB_SYMBOL_BEGIN (bool, USE_LSB_TAG)
271
#define USE_LSB_TAG (VAL_MAX / 2 < INTPTR_MAX)
Paul Eggert's avatar
Paul Eggert committed
272 273 274 275 276 277
DEFINE_GDB_SYMBOL_END (USE_LSB_TAG)

#if !USE_LSB_TAG && !defined WIDE_EMACS_INT
# error "USE_LSB_TAG not supported on this platform; please report this." \
	"Try 'configure --with-wide-int' to work around the problem."
error !;
278
#endif
279

280 281
#ifndef alignas
# define alignas(alignment) /* empty */
282
# if USE_LSB_TAG
283
#  error "USE_LSB_TAG requires alignas"
284 285 286
# endif
#endif

287
#ifdef HAVE_STRUCT_ATTRIBUTE_ALIGNED
288
# define GCALIGNED __attribute__ ((aligned (GCALIGNMENT)))
289
#else
290
# define GCALIGNED /* empty */
291
#endif
292

293 294 295 296 297 298 299
/* Some operations are so commonly executed that they are implemented
   as macros, not functions, because otherwise runtime performance would
   suffer too much when compiling with GCC without optimization.
   There's no need to inline everything, just the operations that
   would otherwise cause a serious performance problem.

   For each such operation OP, define a macro lisp_h_OP that contains
Paul Eggert's avatar
Paul Eggert committed
300
   the operation's implementation.  That way, OP can be implemented
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
   via a macro definition like this:

     #define OP(x) lisp_h_OP (x)

   and/or via a function definition like this:

     LISP_MACRO_DEFUN (OP, Lisp_Object, (Lisp_Object x), (x))

   which macro-expands to this:

     Lisp_Object (OP) (Lisp_Object x) { return lisp_h_OP (x); }

   without worrying about the implementations diverging, since
   lisp_h_OP defines the actual implementation.  The lisp_h_OP macros
   are intended to be private to this include file, and should not be
   used elsewhere.

   FIXME: Remove the lisp_h_OP macros, and define just the inline OP
   functions, once most developers have access to GCC 4.8 or later and
   can use "gcc -Og" to debug.  Maybe in the year 2016.  See
   Bug#11935.

   Commentary for these macros can be found near their corresponding
   functions, below.  */

#if CHECK_LISP_OBJECT_TYPE
# define lisp_h_XLI(o) ((o).i)
# define lisp_h_XIL(i) ((Lisp_Object) { i })
#else
# define lisp_h_XLI(o) (o)
# define lisp_h_XIL(i) (i)
#endif
#define lisp_h_CHECK_LIST_CONS(x, y) CHECK_TYPE (CONSP (x), Qlistp, y)
#define lisp_h_CHECK_NUMBER(x) CHECK_TYPE (INTEGERP (x), Qintegerp, x)
#define lisp_h_CHECK_SYMBOL(x) CHECK_TYPE (SYMBOLP (x), Qsymbolp, x)
336 337
#define lisp_h_CHECK_TYPE(ok, predicate, x) \
   ((ok) ? (void) 0 : (void) wrong_type_argument (predicate, x))
338 339 340
#define lisp_h_CONSP(x) (XTYPE (x) == Lisp_Cons)
#define lisp_h_EQ(x, y) (XLI (x) == XLI (y))
#define lisp_h_FLOATP(x) (XTYPE (x) == Lisp_Float)
341
#define lisp_h_INTEGERP(x) ((XTYPE (x) & (Lisp_Int0 | ~Lisp_Int1)) == Lisp_Int0)
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
#define lisp_h_MARKERP(x) (MISCP (x) && XMISCTYPE (x) == Lisp_Misc_Marker)
#define lisp_h_MISCP(x) (XTYPE (x) == Lisp_Misc)
#define lisp_h_NILP(x) EQ (x, Qnil)
#define lisp_h_SET_SYMBOL_VAL(sym, v) \
   (eassert ((sym)->redirect == SYMBOL_PLAINVAL), (sym)->val.value = (v))
#define lisp_h_SYMBOL_CONSTANT_P(sym) (XSYMBOL (sym)->constant)
#define lisp_h_SYMBOL_VAL(sym) \
   (eassert ((sym)->redirect == SYMBOL_PLAINVAL), (sym)->val.value)
#define lisp_h_SYMBOLP(x) (XTYPE (x) == Lisp_Symbol)
#define lisp_h_VECTORLIKEP(x) (XTYPE (x) == Lisp_Vectorlike)
#define lisp_h_XCAR(c) XCONS (c)->car
#define lisp_h_XCDR(c) XCONS (c)->u.cdr
#define lisp_h_XCONS(a) \
   (eassert (CONSP (a)), (struct Lisp_Cons *) XUNTAG (a, Lisp_Cons))
#define lisp_h_XHASH(a) XUINT (a)
Paul Eggert's avatar
Paul Eggert committed
357 358
#define lisp_h_XPNTR(a) \
   (SYMBOLP (a) ? XSYMBOL (a) : (void *) ((intptr_t) (XLI (a) & VALMASK)))
359 360 361 362
#ifndef GC_CHECK_CONS_LIST
# define lisp_h_check_cons_list() ((void) 0)
#endif
#if USE_LSB_TAG
363
# define lisp_h_make_number(n) \
364
    XIL ((EMACS_INT) (((EMACS_UINT) (n) << INTTYPEBITS) + Lisp_Int0))
365 366
# define lisp_h_XFASTINT(a) XINT (a)
# define lisp_h_XINT(a) (XLI (a) >> INTTYPEBITS)
367 368 369 370
# define lisp_h_XSYMBOL(a) \
    (eassert (SYMBOLP (a)), \
     (struct Lisp_Symbol *) ((uintptr_t) XLI (a) - Lisp_Symbol \
			     + (char *) lispsym))
371
# define lisp_h_XTYPE(a) ((enum Lisp_Type) (XLI (a) & ~VALMASK))
Paul Eggert's avatar
Paul Eggert committed
372
# define lisp_h_XUNTAG(a, type) ((void *) (intptr_t) (XLI (a) - (type)))
373 374 375 376
#endif

/* When compiling via gcc -O0, define the key operations as macros, as
   Emacs is too slow otherwise.  To disable this optimization, compile
377
   with -DINLINING=false.  */
378 379 380 381 382 383 384 385
#if (defined __NO_INLINE__ \
     && ! defined __OPTIMIZE__ && ! defined __OPTIMIZE_SIZE__ \
     && ! (defined INLINING && ! INLINING))
# define XLI(o) lisp_h_XLI (o)
# define XIL(i) lisp_h_XIL (i)
# define CHECK_LIST_CONS(x, y) lisp_h_CHECK_LIST_CONS (x, y)
# define CHECK_NUMBER(x) lisp_h_CHECK_NUMBER (x)
# define CHECK_SYMBOL(x) lisp_h_CHECK_SYMBOL (x)
386
# define CHECK_TYPE(ok, predicate, x) lisp_h_CHECK_TYPE (ok, predicate, x)
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
# define CONSP(x) lisp_h_CONSP (x)
# define EQ(x, y) lisp_h_EQ (x, y)
# define FLOATP(x) lisp_h_FLOATP (x)
# define INTEGERP(x) lisp_h_INTEGERP (x)
# define MARKERP(x) lisp_h_MARKERP (x)
# define MISCP(x) lisp_h_MISCP (x)
# define NILP(x) lisp_h_NILP (x)
# define SET_SYMBOL_VAL(sym, v) lisp_h_SET_SYMBOL_VAL (sym, v)
# define SYMBOL_CONSTANT_P(sym) lisp_h_SYMBOL_CONSTANT_P (sym)
# define SYMBOL_VAL(sym) lisp_h_SYMBOL_VAL (sym)
# define SYMBOLP(x) lisp_h_SYMBOLP (x)
# define VECTORLIKEP(x) lisp_h_VECTORLIKEP (x)
# define XCAR(c) lisp_h_XCAR (c)
# define XCDR(c) lisp_h_XCDR (c)
# define XCONS(a) lisp_h_XCONS (a)
# define XHASH(a) lisp_h_XHASH (a)
# define XPNTR(a) lisp_h_XPNTR (a)
# ifndef GC_CHECK_CONS_LIST
#  define check_cons_list() lisp_h_check_cons_list ()
# endif
# if USE_LSB_TAG
#  define make_number(n) lisp_h_make_number (n)
#  define XFASTINT(a) lisp_h_XFASTINT (a)
#  define XINT(a) lisp_h_XINT (a)
411
#  define XSYMBOL(a) lisp_h_XSYMBOL (a)
412 413 414 415 416 417 418 419 420 421
#  define XTYPE(a) lisp_h_XTYPE (a)
#  define XUNTAG(a, type) lisp_h_XUNTAG (a, type)
# endif
#endif

/* Define NAME as a lisp.h inline function that returns TYPE and has
   arguments declared as ARGDECLS and passed as ARGS.  ARGDECLS and
   ARGS should be parenthesized.  Implement the function by calling
   lisp_h_NAME ARGS.  */
#define LISP_MACRO_DEFUN(name, type, argdecls, args) \
Paul Eggert's avatar
Paul Eggert committed
422
  INLINE type (name) argdecls { return lisp_h_##name args; }
423 424 425

/* like LISP_MACRO_DEFUN, except NAME returns void.  */
#define LISP_MACRO_DEFUN_VOID(name, argdecls, args) \
Paul Eggert's avatar
Paul Eggert committed
426
  INLINE void (name) argdecls { lisp_h_##name args; }
427 428


429
/* Define the fundamental Lisp data structures.  */
Jim Blandy's avatar
Jim Blandy committed
430

431 432 433
/* This is the set of Lisp data types.  If you want to define a new
   data type, read the comments after Lisp_Fwd_Type definition
   below.  */
Jim Blandy's avatar
Jim Blandy committed
434

435 436
/* Lisp integers use 2 tags, to give them one extra bit, thus
   extending their range from, e.g., -2^28..2^28-1 to -2^29..2^29-1.  */
Paul Eggert's avatar
Paul Eggert committed
437
#define INTMASK (EMACS_INT_MAX >> (INTTYPEBITS - 1))
438
#define case_Lisp_Int case Lisp_Int0: case Lisp_Int1
439

Paul Eggert's avatar
Paul Eggert committed
440
/* Idea stolen from GDB.  Pedantic GCC complains about enum bitfields,
Paul Eggert's avatar
Paul Eggert committed
441 442 443 444
   MSVC doesn't support them, and xlc and Oracle Studio c99 complain
   vociferously about them.  */
#if (defined __STRICT_ANSI__ || defined _MSC_VER || defined __IBMC__ \
     || (defined __SUNPRO_C && __STDC__))
445 446 447 448 449 450
#define ENUM_BF(TYPE) unsigned int
#else
#define ENUM_BF(TYPE) enum TYPE
#endif


Jim Blandy's avatar
Jim Blandy committed
451 452
enum Lisp_Type
  {
453
    /* Symbol.  XSYMBOL (object) points to a struct Lisp_Symbol.  */
Paul Eggert's avatar
Paul Eggert committed
454
    Lisp_Symbol = 0,
Jim Blandy's avatar
Jim Blandy committed
455

456 457
    /* Miscellaneous.  XMISC (object) points to a union Lisp_Misc,
       whose first member indicates the subtype.  */
Paul Eggert's avatar
Paul Eggert committed
458 459 460 461 462
    Lisp_Misc = 1,

    /* Integer.  XINT (obj) is the integer value.  */
    Lisp_Int0 = 2,
    Lisp_Int1 = USE_LSB_TAG ? 6 : 3,
Jim Blandy's avatar
Jim Blandy committed
463 464

    /* String.  XSTRING (object) points to a struct Lisp_String.
465
       The length of the string, and its contents, are stored therein.  */
Paul Eggert's avatar
Paul Eggert committed
466
    Lisp_String = 4,
Jim Blandy's avatar
Jim Blandy committed
467

468
    /* Vector of Lisp objects, or something resembling it.
469
       XVECTOR (object) points to a struct Lisp_Vector, which contains
470 471
       the size and contents.  The size field also contains the type
       information, if it's not a real vector object.  */
472
    Lisp_Vectorlike = 5,
Jim Blandy's avatar
Jim Blandy committed
473

474
    /* Cons.  XCONS (object) points to a struct Lisp_Cons.  */
Paul Eggert's avatar
Paul Eggert committed
475
    Lisp_Cons = USE_LSB_TAG ? 3 : 6,
476

Paul Eggert's avatar
Paul Eggert committed
477
    Lisp_Float = 7
Jim Blandy's avatar
Jim Blandy committed
478 479
  };

480
/* This is the set of data types that share a common structure.
481 482 483 484
   The first member of the structure is a type code from this set.
   The enum values are arbitrary, but we'll use large numbers to make it
   more likely that we'll spot the error if a random word in memory is
   mistakenly interpreted as a Lisp_Misc.  */
Karl Heuer's avatar
Karl Heuer committed
485 486
enum Lisp_Misc_Type
  {
487
    Lisp_Misc_Free = 0x5eab,
488
    Lisp_Misc_Marker,
489
    Lisp_Misc_Overlay,
490
    Lisp_Misc_Save_Value,
491 492 493 494 495
    /* Currently floats are not a misc type,
       but let's define this in case we want to change that.  */
    Lisp_Misc_Float,
    /* This is not a type code.  It is for range checking.  */
    Lisp_Misc_Limit
Karl Heuer's avatar
Karl Heuer committed
496 497
  };

498 499 500 501 502 503 504 505 506
/* These are the types of forwarding objects used in the value slot
   of symbols for special built-in variables whose value is stored in
   C variables.  */
enum Lisp_Fwd_Type
  {
    Lisp_Fwd_Int,		/* Fwd to a C `int' variable.  */
    Lisp_Fwd_Bool,		/* Fwd to a C boolean var.  */
    Lisp_Fwd_Obj,		/* Fwd to a C Lisp_Object variable.  */
    Lisp_Fwd_Buffer_Obj,	/* Fwd to a Lisp_Object field of buffers.  */
Paul Eggert's avatar
Paul Eggert committed
507
    Lisp_Fwd_Kboard_Obj		/* Fwd to a Lisp_Object field of kboards.  */
508 509
  };

510 511 512 513 514 515 516 517
/* If you want to define a new Lisp data type, here are some
   instructions.  See the thread at
   http://lists.gnu.org/archive/html/emacs-devel/2012-10/msg00561.html
   for more info.

   First, there are already a couple of Lisp types that can be used if
   your new type does not need to be exposed to Lisp programs nor
   displayed to users.  These are Lisp_Save_Value, a Lisp_Misc
Paul Eggert's avatar
Paul Eggert committed
518
   subtype; and PVEC_OTHER, a kind of vectorlike object.  The former
519
   is suitable for temporarily stashing away pointers and integers in
520
   a Lisp object.  The latter is useful for vector-like Lisp objects
521 522 523 524 525 526 527 528 529 530
   that need to be used as part of other objects, but which are never
   shown to users or Lisp code (search for PVEC_OTHER in xterm.c for
   an example).

   These two types don't look pretty when printed, so they are
   unsuitable for Lisp objects that can be exposed to users.

   To define a new data type, add one more Lisp_Misc subtype or one
   more pseudovector subtype.  Pseudovectors are more suitable for
   objects with several slots that need to support fast random access,
Paul Eggert's avatar
Paul Eggert committed
531
   while Lisp_Misc types are for everything else.  A pseudovector object
532 533 534 535
   provides one or more slots for Lisp objects, followed by struct
   members that are accessible only from C.  A Lisp_Misc object is a
   wrapper for a C struct that can contain anything you like.

536 537 538 539
   Explicit freeing is discouraged for Lisp objects in general.  But if
   you really need to exploit this, use Lisp_Misc (check free_misc in
   alloc.c to see why).  There is no way to free a vectorlike object.

540 541 542 543 544 545 546 547 548
   To add a new pseudovector type, extend the pvec_type enumeration;
   to add a new Lisp_Misc, extend the Lisp_Misc_Type enumeration.

   For a Lisp_Misc, you will also need to add your entry to union
   Lisp_Misc (but make sure the first word has the same structure as
   the others, starting with a 16-bit member of the Lisp_Misc_Type
   enumeration and a 1-bit GC markbit) and make sure the overall size
   of the union is not increased by your addition.

549 550 551 552
   For a new pseudovector, it's highly desirable to limit the size
   of your data type by VBLOCK_BYTES_MAX bytes (defined in alloc.c).
   Otherwise you will need to change sweep_vectors (also in alloc.c).

553 554 555 556 557 558 559 560 561 562 563
   Then you will need to add switch branches in print.c (in
   print_object, to print your object, and possibly also in
   print_preprocess) and to alloc.c, to mark your object (in
   mark_object) and to free it (in gc_sweep).  The latter is also the
   right place to call any code specific to your data type that needs
   to run when the object is recycled -- e.g., free any additional
   resources allocated for it that are not Lisp objects.  You can even
   make a pointer to the function that frees the resources a slot in
   your object -- this way, the same object could be used to represent
   several disparate C structures.  */

564
#ifdef CHECK_LISP_OBJECT_TYPE
Jim Blandy's avatar
Jim Blandy committed
565

566 567
typedef struct { EMACS_INT i; } Lisp_Object;

568
#define LISP_INITIALLY(i) {i}
569

Paul Eggert's avatar
Paul Eggert committed
570
#undef CHECK_LISP_OBJECT_TYPE
571
enum CHECK_LISP_OBJECT_TYPE { CHECK_LISP_OBJECT_TYPE = true };
572
#else /* CHECK_LISP_OBJECT_TYPE */
Jim Blandy's avatar
Jim Blandy committed
573

574
/* If a struct type is not wanted, define Lisp_Object as just a number.  */
Jim Blandy's avatar
Jim Blandy committed
575

576
typedef EMACS_INT Lisp_Object;
577
#define LISP_INITIALLY(i) (i)
578
enum CHECK_LISP_OBJECT_TYPE { CHECK_LISP_OBJECT_TYPE = false };
579
#endif /* CHECK_LISP_OBJECT_TYPE */
580 581

#define LISP_INITIALLY_ZERO LISP_INITIALLY (0)
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610

/* Forward declarations.  */

/* Defined in this file.  */
union Lisp_Fwd;
INLINE bool BOOL_VECTOR_P (Lisp_Object);
INLINE bool BUFFER_OBJFWDP (union Lisp_Fwd *);
INLINE bool BUFFERP (Lisp_Object);
INLINE bool CHAR_TABLE_P (Lisp_Object);
INLINE Lisp_Object CHAR_TABLE_REF_ASCII (Lisp_Object, ptrdiff_t);
INLINE bool (CONSP) (Lisp_Object);
INLINE bool (FLOATP) (Lisp_Object);
INLINE bool functionp (Lisp_Object);
INLINE bool (INTEGERP) (Lisp_Object);
INLINE bool (MARKERP) (Lisp_Object);
INLINE bool (MISCP) (Lisp_Object);
INLINE bool (NILP) (Lisp_Object);
INLINE bool OVERLAYP (Lisp_Object);
INLINE bool PROCESSP (Lisp_Object);
INLINE bool PSEUDOVECTORP (Lisp_Object, int);
INLINE bool SAVE_VALUEP (Lisp_Object);
INLINE void set_sub_char_table_contents (Lisp_Object, ptrdiff_t,
					      Lisp_Object);
INLINE bool STRINGP (Lisp_Object);
INLINE bool SUB_CHAR_TABLE_P (Lisp_Object);
INLINE bool SUBRP (Lisp_Object);
INLINE bool (SYMBOLP) (Lisp_Object);
INLINE bool (VECTORLIKEP) (Lisp_Object);
INLINE bool WINDOWP (Lisp_Object);
Dmitry Antipov's avatar
Dmitry Antipov committed
611
INLINE bool TERMINALP (Lisp_Object);
612
INLINE struct Lisp_Save_Value *XSAVE_VALUE (Lisp_Object);
613
INLINE struct Lisp_Symbol *(XSYMBOL) (Lisp_Object);
614
INLINE void *(XUNTAG) (Lisp_Object, int);
615 616 617 618

/* Defined in chartab.c.  */
extern Lisp_Object char_table_ref (Lisp_Object, int);
extern void char_table_set (Lisp_Object, int, Lisp_Object);
Jim Blandy's avatar
Jim Blandy committed
619

620 621 622 623 624 625 626 627 628 629 630 631 632
/* Defined in data.c.  */
extern _Noreturn Lisp_Object wrong_type_argument (Lisp_Object, Lisp_Object);
extern _Noreturn void wrong_choice (Lisp_Object, Lisp_Object);

/* Defined in emacs.c.  */
extern bool might_dump;
/* True means Emacs has already been initialized.
   Used during startup to detect startup of dumped Emacs.  */
extern bool initialized;

/* Defined in floatfns.c.  */
extern double extract_float (Lisp_Object);

633 634

/* Interned state of a symbol.  */
635

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
enum symbol_interned
{
  SYMBOL_UNINTERNED = 0,
  SYMBOL_INTERNED = 1,
  SYMBOL_INTERNED_IN_INITIAL_OBARRAY = 2
};

enum symbol_redirect
{
  SYMBOL_PLAINVAL  = 4,
  SYMBOL_VARALIAS  = 1,
  SYMBOL_LOCALIZED = 2,
  SYMBOL_FORWARDED = 3
};

struct Lisp_Symbol
{
  bool_bf gcmarkbit : 1;

  /* Indicates where the value can be found:
     0 : it's a plain var, the value is in the `value' field.
     1 : it's a varalias, the value is really in the `alias' symbol.
     2 : it's a localized var, the value is in the `blv' object.
     3 : it's a forwarding variable, the value is in `forward'.  */
  ENUM_BF (symbol_redirect) redirect : 3;

  /* Non-zero means symbol is constant, i.e. changing its value
     should signal an error.  If the value is 3, then the var
     can be changed, but only by `defconst'.  */
  unsigned constant : 2;

  /* Interned state of the symbol.  This is an enumerator from
     enum symbol_interned.  */
  unsigned interned : 2;

  /* True means that this variable has been explicitly declared
     special (with `defvar' etc), and shouldn't be lexically bound.  */
  bool_bf declared_special : 1;

  /* True if pointed to from purespace and hence can't be GC'd.  */
  bool_bf pinned : 1;

  /* The symbol's name, as a Lisp string.  */
  Lisp_Object name;

  /* Value of the symbol or Qunbound if unbound.  Which alternative of the
     union is used depends on the `redirect' field above.  */
  union {
    Lisp_Object value;
    struct Lisp_Symbol *alias;
    struct Lisp_Buffer_Local_Value *blv;
    union Lisp_Fwd *fwd;
  } val;

  /* Function value of the symbol or Qnil if not fboundp.  */
  Lisp_Object function;

  /* The symbol's property list.  */
  Lisp_Object plist;

  /* Next symbol in obarray bucket, if the symbol is interned.  */
  struct Lisp_Symbol *next;
};

/* Declare a Lisp-callable function.  The MAXARGS parameter has the same
   meaning as in the DEFUN macro, and is used to construct a prototype.  */
/* We can use the same trick as in the DEFUN macro to generate the
   appropriate prototype.  */
#define EXFUN(fnname, maxargs) \
  extern Lisp_Object fnname DEFUN_ARGS_ ## maxargs

/* Note that the weird token-substitution semantics of ANSI C makes
   this work for MANY and UNEVALLED.  */
#define DEFUN_ARGS_MANY		(ptrdiff_t, Lisp_Object *)
#define DEFUN_ARGS_UNEVALLED	(Lisp_Object)
#define DEFUN_ARGS_0	(void)
#define DEFUN_ARGS_1	(Lisp_Object)
#define DEFUN_ARGS_2	(Lisp_Object, Lisp_Object)
#define DEFUN_ARGS_3	(Lisp_Object, Lisp_Object, Lisp_Object)
#define DEFUN_ARGS_4	(Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object)
#define DEFUN_ARGS_5	(Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, \
			 Lisp_Object)
#define DEFUN_ARGS_6	(Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, \
			 Lisp_Object, Lisp_Object)
#define DEFUN_ARGS_7	(Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, \
			 Lisp_Object, Lisp_Object, Lisp_Object)
#define DEFUN_ARGS_8	(Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, \
			 Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object)

/* Yield an integer that contains TAG along with PTR.  */
#define TAG_PTR(tag, ptr) \
  ((USE_LSB_TAG ? (tag) : (EMACS_UINT) (tag) << VALBITS) + (uintptr_t) (ptr))

Paul Eggert's avatar
Paul Eggert committed
729 730 731
/* Yield an integer that contains a symbol tag along with OFFSET.
   OFFSET should be the offset in bytes from 'lispsym' to the symbol.  */
#define TAG_SYMOFFSET(offset)				    \
732
  TAG_PTR (Lisp_Symbol,					    \
Paul Eggert's avatar
Paul Eggert committed
733
	   ((uintptr_t) (offset) >> (USE_LSB_TAG ? 0 : GCTYPEBITS)))
Paul Eggert's avatar
Paul Eggert committed
734

735 736 737 738 739
/* XLI_BUILTIN_LISPSYM (iQwhatever) is equivalent to
   XLI (builtin_lisp_symbol (Qwhatever)),
   except the former expands to an integer constant expression.  */
#define XLI_BUILTIN_LISPSYM(iname) TAG_SYMOFFSET ((iname) * sizeof *lispsym)

740 741
/* Declare extern constants for Lisp symbols.  These can be helpful
   when using a debugger like GDB, on older platforms where the debug
Paul Eggert's avatar
Paul Eggert committed
742 743
   format does not represent C macros.  */
#define DEFINE_LISP_SYMBOL_BEGIN(name) \
744
  DEFINE_GDB_SYMBOL_BEGIN (Lisp_Object, name)
Paul Eggert's avatar
Paul Eggert committed
745
#define DEFINE_LISP_SYMBOL_END(name) \
746
  DEFINE_GDB_SYMBOL_END (LISP_INITIALLY (XLI_BUILTIN_LISPSYM (i##name)))
747 748

#include "globals.h"
749

750 751 752 753 754
/* Convert a Lisp_Object to the corresponding EMACS_INT and vice versa.
   At the machine level, these operations are no-ops.  */
LISP_MACRO_DEFUN (XLI, EMACS_INT, (Lisp_Object o), (o))
LISP_MACRO_DEFUN (XIL, Lisp_Object, (EMACS_INT i), (i))

755
/* In the size word of a vector, this bit means the vector has been marked.  */
756

757
DEFINE_GDB_SYMBOL_BEGIN (ptrdiff_t, ARRAY_MARK_FLAG)
758
# define ARRAY_MARK_FLAG PTRDIFF_MIN
759
DEFINE_GDB_SYMBOL_END (ARRAY_MARK_FLAG)
760

761 762
/* In the size word of a struct Lisp_Vector, this bit means it's really
   some other vector-like object.  */
763
DEFINE_GDB_SYMBOL_BEGIN (ptrdiff_t, PSEUDOVECTOR_FLAG)
764
# define PSEUDOVECTOR_FLAG (PTRDIFF_MAX - PTRDIFF_MAX / 2)
765
DEFINE_GDB_SYMBOL_END (PSEUDOVECTOR_FLAG)
766

767
/* In a pseudovector, the size field actually contains a word with one
768 769
   PSEUDOVECTOR_FLAG bit set, and one of the following values extracted
   with PVEC_TYPE_MASK to indicate the actual type.  */
770 771
enum pvec_type
{
772
  PVEC_NORMAL_VECTOR,
773 774 775 776 777 778 779 780 781 782 783
  PVEC_FREE,
  PVEC_PROCESS,
  PVEC_FRAME,
  PVEC_WINDOW,
  PVEC_BOOL_VECTOR,
  PVEC_BUFFER,
  PVEC_HASH_TABLE,
  PVEC_TERMINAL,
  PVEC_WINDOW_CONFIGURATION,
  PVEC_SUBR,
  PVEC_OTHER,
784 785 786 787 788
  /* These should be last, check internal_equal to see why.  */
  PVEC_COMPILED,
  PVEC_CHAR_TABLE,
  PVEC_SUB_CHAR_TABLE,
  PVEC_FONT /* Should be last because it's used for range checking.  */
789
};
790

Paul Eggert's avatar
Paul Eggert committed
791
enum More_Lisp_Bits
792
  {
Paul Eggert's avatar
Paul Eggert committed
793 794 795 796 797
    /* For convenience, we also store the number of elements in these bits.
       Note that this size is not necessarily the memory-footprint size, but
       only the number of Lisp_Object fields (that need to be traced by GC).
       The distinction is used, e.g., by Lisp_Process, which places extra
       non-Lisp_Object fields at the end of the structure.  */
798
    PSEUDOVECTOR_SIZE_BITS = 12,
799
    PSEUDOVECTOR_SIZE_MASK = (1 << PSEUDOVECTOR_SIZE_BITS) - 1,
800 801 802 803

    /* To calculate the memory footprint of the pseudovector, it's useful
       to store the size of non-Lisp area in word_size units here.  */
    PSEUDOVECTOR_REST_BITS = 12,
804
    PSEUDOVECTOR_REST_MASK = (((1 << PSEUDOVECTOR_REST_BITS) - 1)
805 806 807 808
			      << PSEUDOVECTOR_SIZE_BITS),

    /* Used to extract pseudovector subtype information.  */
    PSEUDOVECTOR_AREA_BITS = PSEUDOVECTOR_SIZE_BITS + PSEUDOVECTOR_REST_BITS,
Paul Eggert's avatar
Paul Eggert committed
809
    PVEC_TYPE_MASK = 0x3f << PSEUDOVECTOR_AREA_BITS
Paul Eggert's avatar
Paul Eggert committed
810
  };
Jim Blandy's avatar
Jim Blandy committed
811

812
/* These functions extract various sorts of values from a Lisp_Object.
813 814 815
   For example, if tem is a Lisp_Object whose type is Lisp_Cons,
   XCONS (tem) is the struct Lisp_Cons * pointing to the memory for
   that cons.  */
Jim Blandy's avatar
Jim Blandy committed
816

817
/* Mask for the value (as opposed to the type bits) of a Lisp object.  */
818
DEFINE_GDB_SYMBOL_BEGIN (EMACS_INT, VALMASK)
819
# define VALMASK (USE_LSB_TAG ? - (1 << GCTYPEBITS) : VAL_MAX)
820
DEFINE_GDB_SYMBOL_END (VALMASK)
Kenichi Handa's avatar
Kenichi Handa committed
821

822 823 824 825
/* Largest and smallest representable fixnum values.  These are the C
   values.  They are macros for use in static initializers.  */
#define MOST_POSITIVE_FIXNUM (EMACS_INT_MAX >> INTTYPEBITS)
#define MOST_NEGATIVE_FIXNUM (-1 - MOST_POSITIVE_FIXNUM)
Kenichi Handa's avatar
Kenichi Handa committed
826

827
#if USE_LSB_TAG
Kenichi Handa's avatar
Kenichi Handa committed
828

829 830 831
LISP_MACRO_DEFUN (make_number, Lisp_Object, (EMACS_INT n), (n))
LISP_MACRO_DEFUN (XINT, EMACS_INT, (Lisp_Object a), (a))
LISP_MACRO_DEFUN (XFASTINT, EMACS_INT, (Lisp_Object a), (a))
832
LISP_MACRO_DEFUN (XSYMBOL, struct Lisp_Symbol *, (Lisp_Object a), (a))
833 834
LISP_MACRO_DEFUN (XTYPE, enum Lisp_Type, (Lisp_Object a), (a))
LISP_MACRO_DEFUN (XUNTAG, void *, (Lisp_Object a, int type), (a, type))
Kenichi Handa's avatar
Kenichi Handa committed
835

836
#else /* ! USE_LSB_TAG */
Jim Blandy's avatar
Jim Blandy committed
837

838 839 840
/* Although compiled only if ! USE_LSB_TAG, the following functions
   also work when USE_LSB_TAG; this is to aid future maintenance when
   the lisp_h_* macros are eventually removed.  */
Jim Blandy's avatar
Jim Blandy committed
841

842 843
/* Make a Lisp integer representing the value of the low order
   bits of N.  */
Paul Eggert's avatar
Paul Eggert committed
844
INLINE Lisp_Object
845 846
make_number (EMACS_INT n)
{
847
  EMACS_INT int0 = Lisp_Int0;
848 849 850 851
  if (USE_LSB_TAG)
    {
      EMACS_UINT u = n;
      n = u << INTTYPEBITS;
852
      n += int0;
853 854
    }
  else
855 856 857 858
    {
      n &= INTMASK;
      n += (int0 << VALBITS);
    }
859
  return XIL (n);
860
}
Jim Blandy's avatar
Jim Blandy committed
861

862
/* Extract A's value as a signed integer.  */
Paul Eggert's avatar
Paul Eggert committed
863
INLINE EMACS_INT
864 865 866
XINT (Lisp_Object a)
{
  EMACS_INT i = XLI (a);
867 868 869 870 871 872
  if (! USE_LSB_TAG)
    {
      EMACS_UINT u = i;
      i = u << INTTYPEBITS;
    }
  return i >> INTTYPEBITS;
873
}
Jim Blandy's avatar
Jim Blandy committed
874

875 876 877
/* Like XINT (A), but may be faster.  A must be nonnegative.
   If ! USE_LSB_TAG, this takes advantage of the fact that Lisp
   integers have zero-bits in their tags.  */
Paul Eggert's avatar
Paul Eggert committed
878
INLINE EMACS_INT
879 880
XFASTINT (Lisp_Object a)
{
881 882
  EMACS_INT int0 = Lisp_Int0;
  EMACS_INT n = USE_LSB_TAG ? XINT (a) : XLI (a) - (int0 << VALBITS);
883 884 885
  eassert (0 <= n);
  return n;
}
886

887 888 889 890 891 892 893 894 895 896 897
/* Extract A's value as a symbol.  */
INLINE struct Lisp_Symbol *
XSYMBOL (Lisp_Object a)
{
  uintptr_t i = (uintptr_t) XUNTAG (a, Lisp_Symbol);
  if (! USE_LSB_TAG)
    i <<= GCTYPEBITS;
  void *p = (char *) lispsym + i;
  return p;
}

898
/* Extract A's type.  */
Paul Eggert's avatar
Paul Eggert committed
899
INLINE enum Lisp_Type
900 901 902 903 904
XTYPE (Lisp_Object a)
{
  EMACS_UINT i = XLI (a);
  return USE_LSB_TAG ? i & ~VALMASK : i >> VALBITS;
}
905

906
/* Extract A's pointer value, assuming A's type is TYPE.  */
Paul Eggert's avatar
Paul Eggert committed
907
INLINE void *
908 909
XUNTAG (Lisp_Object a, int type)
{
Paul Eggert's avatar
Paul Eggert committed
910 911
  intptr_t i = USE_LSB_TAG ? XLI (a) - type : XLI (a) & VALMASK;
  return (void *) i;
912
}
Jim Blandy's avatar
Jim Blandy committed
913

914
#endif /* ! USE_LSB_TAG */
915

916 917 918
/* Extract the pointer hidden within A.  */
LISP_MACRO_DEFUN (XPNTR, void *, (Lisp_Object a), (a))

919
/* Extract A's value as an unsigned integer.  */
Paul Eggert's avatar
Paul Eggert committed
920
INLINE EMACS_UINT
921 922 923 924 925
XUINT (Lisp_Object a)
{
  EMACS_UINT i = XLI (a);
  return USE_LSB_TAG ? i >> INTTYPEBITS : i & INTMASK;
}
926

927 928 929 930
/* Return A's (Lisp-integer sized) hash.  Happens to be like XUINT
   right now, but XUINT should only be applied to objects we know are
   integers.  */
LISP_MACRO_DEFUN (XHASH, EMACS_INT, (Lisp_Object a), (a))
931

932
/* Like make_number (N), but may be faster.  N must be in nonnegative range.  */
Paul Eggert's avatar
Paul Eggert committed
933
INLINE Lisp_Object
934 935 936
make_natnum (EMACS_INT n)
{
  eassert (0 <= n && n <= MOST_POSITIVE_FIXNUM);
937 938
  EMACS_INT int0 = Lisp_Int0;
  return USE_LSB_TAG ? make_number (n) : XIL (n + (int0 << VALBITS));
939
}
940

941 942
/* Return true if X and Y are the same object.  */
LISP_MACRO_DEFUN (EQ, bool, (Lisp_Object x, Lisp_Object y), (x, y))
943

944
/* Value is true if I doesn't fit into a Lisp fixnum.  It is
945
   written this way so that it also works if I is of unsigned
946
   type or if I is a NaN.  */
947 948

#define FIXNUM_OVERFLOW_P(i) \
Paul Eggert's avatar
Paul Eggert committed
949
  (! ((0 <= (i) || MOST_NEGATIVE_FIXNUM <= (i)) && (i) <= MOST_POSITIVE_FIXNUM))
950

Paul Eggert's avatar
Paul Eggert committed
951
INLINE ptrdiff_t
952 953 954 955
clip_to_bounds (ptrdiff_t lower, EMACS_INT num, ptrdiff_t upper)
{
  return num < lower ? lower : num <= upper ? num : upper;
}
956 957


958
/* Extract a value or address from a Lisp_Object.  */
Jim Blandy's avatar
Jim Blandy committed
959

960
LISP_MACRO_DEFUN (XCONS, struct Lisp_Cons *, (Lisp_Object a), (a))
961

Paul Eggert's avatar
Paul Eggert committed
962
INLINE struct Lisp_Vector *
963 964 965 966 967
XVECTOR (Lisp_Object a)
{
  eassert (VECTORLIKEP (a));
  return XUNTAG (a, Lisp_Vectorlike);
}
968

Paul Eggert's avatar
Paul Eggert committed
969
INLINE struct Lisp_String *
970 971 972 973 974
XSTRING (Lisp_Object a)
{
  eassert (STRINGP (a));
  return XUNTAG (a, Lisp_String);
}
975

Paul Eggert's avatar
Paul Eggert committed
976 977 978
/* The index of the C-defined Lisp symbol SYM.
   This can be used in a static initializer.  */
#define SYMBOL_INDEX(sym) i##sym
979

Paul Eggert's avatar
Paul Eggert committed
980
INLINE struct Lisp_Float *
981 982 983 984 985
XFLOAT (Lisp_Object a)
{
  eassert (FLOATP (a));
  return XUNTAG (a, Lisp_Float);
}
Jim Blandy's avatar
Jim Blandy committed
986

987
/* Pseudovector types.  */
988

Paul Eggert's avatar
Paul Eggert committed
989
INLINE struct Lisp_Process *
990 991 992 993 994 995
XPROCESS (Lisp_Object a)
{
  eassert (PROCESSP (a));
  return XUNTAG (a, Lisp_Vectorlike);
}

Paul Eggert's avatar
Paul Eggert committed
996
INLINE struct window *
997 998 999 1000 1001 1002
XWINDOW (Lisp_Object a)
{
  eassert (WINDOWP (a));
  return XUNTAG (a, Lisp_Vectorlike);
}

Paul Eggert's avatar
Paul Eggert committed
1003
INLINE struct terminal *
1004 1