lisp.h 160 KB
Newer Older
1
/* Fundamental definitions for GNU Emacs Lisp interpreter. -*- coding: utf-8 -*-
2

Paul Eggert's avatar
Paul Eggert committed
3
Copyright (C) 1985-1987, 1993-1995, 1997-2019 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 <https://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

Paul Eggert's avatar
Paul Eggert committed
24
#include <alloca.h>
25
#include <setjmp.h>
26
#include <stdalign.h>
27
#include <stdarg.h>
28
#include <stddef.h>
Paul Eggert's avatar
Paul Eggert committed
29
#include <string.h>
30
#include <float.h>
31
#include <inttypes.h>
32
#include <limits.h>
33

34
#include <intprops.h>
Paul Eggert's avatar
Paul Eggert committed
35
#include <verify.h>
36

37 38
INLINE_HEADER_BEGIN

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

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

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

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

58 59 60 61 62
/* 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))
63

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

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

Paul Eggert's avatar
Paul Eggert committed
72
/* EMACS_INT - signed integer wide enough to hold an Emacs value
73
   EMACS_INT_WIDTH - width in bits of EMACS_INT
Paul Eggert's avatar
Paul Eggert committed
74 75 76
   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 */
77
#ifndef EMACS_INT_MAX
78 79
# if INTPTR_MAX <= 0
#  error "INTPTR_MAX misconfigured"
Paul Eggert's avatar
Paul Eggert committed
80
# elif INTPTR_MAX <= INT_MAX && !defined WIDE_EMACS_INT
81 82
typedef int EMACS_INT;
typedef unsigned int EMACS_UINT;
83
enum { EMACS_INT_WIDTH = INT_WIDTH, EMACS_UINT_WIDTH = UINT_WIDTH };
84 85
#  define EMACS_INT_MAX INT_MAX
#  define pI ""
Paul Eggert's avatar
Paul Eggert committed
86
# elif INTPTR_MAX <= LONG_MAX && !defined WIDE_EMACS_INT
87
typedef long int EMACS_INT;
88
typedef unsigned long EMACS_UINT;
89
enum { EMACS_INT_WIDTH = LONG_WIDTH, EMACS_UINT_WIDTH = ULONG_WIDTH };
Paul Eggert's avatar
Paul Eggert committed
90
#  define EMACS_INT_MAX LONG_MAX
91
#  define pI "l"
92
# elif INTPTR_MAX <= LLONG_MAX
93 94
typedef long long int EMACS_INT;
typedef unsigned long long int EMACS_UINT;
95
enum { EMACS_INT_WIDTH = LLONG_WIDTH, EMACS_UINT_WIDTH = ULLONG_WIDTH };
Paul Eggert's avatar
Paul Eggert committed
96
#  define EMACS_INT_MAX LLONG_MAX
97 98 99 100 101 102 103
/* MinGW supports %lld only if __USE_MINGW_ANSI_STDIO is non-zero,
   which is arranged by config.h, and (for mingw.org) if GCC is 6.0 or
   later and the runtime version is 5.0.0 or later.  Otherwise,
   printf-like functions are declared with __ms_printf__ attribute,
   which will cause a warning for %lld etc.  */
#  if defined __MINGW32__						\
  && (!defined __USE_MINGW_ANSI_STDIO					\
104 105
      || (!defined MINGW_W64						\
	  && !(GNUC_PREREQ (6, 0, 0) && __MINGW32_MAJOR_VERSION >= 5)))
106
#   define pI "I64"
107
#  else	 /* ! MinGW */
108 109
#   define pI "ll"
#  endif
110
# else
111
#  error "INTPTR_MAX too large"
112
# endif
113
#endif
Paul Eggert's avatar
Paul Eggert committed
114

115 116 117 118 119 120 121
/* 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
};

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

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/* 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

152 153 154
/* Convenience macro for rarely-used functions that do not return.  */
#define AVOID _Noreturn ATTRIBUTE_COLD void

155
/* Extra internal type checking?  */
156

Paul Eggert's avatar
Paul Eggert committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/* 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.  */

180
#ifndef ENABLE_CHECKING
181
# define eassert(cond) ((void) (false && (cond))) /* Check COND compiles.  */
Paul Eggert's avatar
Paul Eggert committed
182
# define eassume(cond) assume (cond)
183
#else /* ENABLE_CHECKING */
184

185
extern AVOID die (const char *, const char *, int);
186

187
extern bool suppress_checking EXTERNALLY_VISIBLE;
188

189
# define eassert(cond)						\
190
   (suppress_checking || (cond) 				\
191
    ? (void) 0							\
192
    : die (# cond, __FILE__, __LINE__))
Paul Eggert's avatar
Paul Eggert committed
193 194 195 196 197 198
# define eassume(cond)						\
   (suppress_checking						\
    ? assume (cond)						\
    : (cond)							\
    ? (void) 0							\
    : die (# cond, __FILE__, __LINE__))
199
#endif /* ENABLE_CHECKING */
200

201

202 203 204
/* 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
205

206
/***** Select the tagging scheme.  *****/
207
/* The following option controls the tagging scheme:
208 209 210 211
   - 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.

212 213
   If ! USE_LSB_TAG, then use the top 3 bits for tagging, thus
   restricting our possible address range.
214 215 216

   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
217
   on some non-GC Lisp_Objects, all of which are aligned via
218
   GCALIGNED_UNION_MEMBER.  */
219

Paul Eggert's avatar
Paul Eggert committed
220 221 222
enum Lisp_Bits
  {
    /* Number of bits in a Lisp_Object value, not counting the tag.  */
223
    VALBITS = EMACS_INT_WIDTH - GCTYPEBITS,
224

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

229
/* Number of bits in a fixnum tag; can be used in #if.  */
230 231 232 233
DEFINE_GDB_SYMBOL_BEGIN (int, INTTYPEBITS)
#define INTTYPEBITS (GCTYPEBITS - 1)
DEFINE_GDB_SYMBOL_END (INTTYPEBITS)

Paul Eggert's avatar
Paul Eggert committed
234 235
/* 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.
236
   This can be used in #if, e.g., '#if USE_LSB_TAG' below expands to an
237
   expression involving VAL_MAX.  */
Paul Eggert's avatar
Paul Eggert committed
238
#define VAL_MAX (EMACS_INT_MAX >> (GCTYPEBITS - 1))
239

Paul Eggert's avatar
Paul Eggert committed
240
/* Whether the least-significant bits of an EMACS_INT contain the tag.
241
   On hosts where pointers-as-ints do not exceed VAL_MAX / 2, USE_LSB_TAG is:
242 243
    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
244 245
   So, USE_LSB_TAG is true only on hosts where it might be useful.  */
DEFINE_GDB_SYMBOL_BEGIN (bool, USE_LSB_TAG)
246
#define USE_LSB_TAG (VAL_MAX / 2 < INTPTR_MAX)
Paul Eggert's avatar
Paul Eggert committed
247 248
DEFINE_GDB_SYMBOL_END (USE_LSB_TAG)

249 250 251 252 253
/* Mask for the value (as opposed to the type bits) of a Lisp object.  */
DEFINE_GDB_SYMBOL_BEGIN (EMACS_INT, VALMASK)
# define VALMASK (USE_LSB_TAG ? - (1 << GCTYPEBITS) : VAL_MAX)
DEFINE_GDB_SYMBOL_END (VALMASK)

Paul Eggert's avatar
Paul Eggert committed
254 255 256 257
#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 !;
258
#endif
259

260 261
/* Minimum alignment requirement for Lisp objects, imposed by the
   internal representation of tagged pointers.  It is 2**GCTYPEBITS if
262 263
   USE_LSB_TAG, 1 otherwise.  It must be a literal integer constant,
   for older versions of GCC (through at least 4.9).  */
264
#if USE_LSB_TAG
265 266 267 268
# define GCALIGNMENT 8
# if GCALIGNMENT != 1 << GCTYPEBITS
#  error "GCALIGNMENT and GCTYPEBITS are inconsistent"
# endif
269
#else
270
# define GCALIGNMENT 1
271 272
#endif

273 274
/* To cause a union to have alignment of at least GCALIGNMENT, put
   GCALIGNED_UNION_MEMBER in its member list.
Paul Eggert's avatar
Paul Eggert committed
275

276 277 278 279 280
   If a struct is always GC-aligned (either by the GC, or via
   allocation in a containing union that has GCALIGNED_UNION_MEMBER)
   and does not contain a GC-aligned struct or union, putting
   GCALIGNED_STRUCT after its closing '}' can help the compiler
   generate better code.
Paul Eggert's avatar
Paul Eggert committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

   Although these macros are reasonably portable, they are not
   guaranteed on non-GCC platforms, as C11 does not require support
   for alignment to GCALIGNMENT and older compilers may ignore
   alignment requests.  For any type T where garbage collection
   requires alignment, use verify (GCALIGNED (T)) to verify the
   requirement on the current platform.  Types need this check if
   their objects can be allocated outside the garbage collector.  For
   example, struct Lisp_Symbol needs the check because of lispsym and
   struct Lisp_Cons needs it because of STACK_CONS.  */

#define GCALIGNED_UNION_MEMBER char alignas (GCALIGNMENT) gcaligned;
#if HAVE_STRUCT_ATTRIBUTE_ALIGNED
# define GCALIGNED_STRUCT __attribute__ ((aligned (GCALIGNMENT)))
#else
# define GCALIGNED_STRUCT
#endif
#define GCALIGNED(type) (alignof (type) % GCALIGNMENT == 0)
299

300 301 302 303 304 305 306 307 308 309 310 311
/* Lisp_Word is a scalar word suitable for holding a tagged pointer or
   integer.  Usually it is a pointer to a deliberately-incomplete type
   'union Lisp_X'.  However, it is EMACS_INT when Lisp_Objects and
   pointers differ in width.  */

#define LISP_WORDS_ARE_POINTERS (EMACS_INT_MAX == INTPTR_MAX)
#if LISP_WORDS_ARE_POINTERS
typedef union Lisp_X *Lisp_Word;
#else
typedef EMACS_INT Lisp_Word;
#endif

312 313 314 315 316 317 318
/* 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
319
   the operation's implementation.  That way, OP can be implemented
320 321 322 323 324 325 326 327 328 329 330 331 332 333
   via a macro definition like this:

     #define OP(x) lisp_h_OP (x)

   and/or via a function definition like 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
334 335
   functions, once "gcc -Og" (new to GCC 4.8) works well enough for
   Emacs developers.  Maybe in the year 2020.  See Bug#11935.
336

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
   For the macros that have corresponding functions (defined later),
   see these functions for commentary.  */

/* Convert among the various Lisp-related types: I for EMACS_INT, L
   for Lisp_Object, P for void *.  */
#if !CHECK_LISP_OBJECT_TYPE
# if LISP_WORDS_ARE_POINTERS
#  define lisp_h_XLI(o) ((EMACS_INT) (o))
#  define lisp_h_XIL(i) ((Lisp_Object) (i))
#  define lisp_h_XLP(o) ((void *) (o))
#  define lisp_h_XPL(p) ((Lisp_Object) (p))
# else
#  define lisp_h_XLI(o) (o)
#  define lisp_h_XIL(i) (i)
#  define lisp_h_XLP(o) ((void *) (uintptr_t) (o))
#  define lisp_h_XPL(p) ((Lisp_Object) (uintptr_t) (p))
# endif
354
#else
355 356 357 358 359 360 361 362 363 364 365
# if LISP_WORDS_ARE_POINTERS
#  define lisp_h_XLI(o) ((EMACS_INT) (o).i)
#  define lisp_h_XIL(i) ((Lisp_Object) {(Lisp_Word) (i)})
#  define lisp_h_XLP(o) ((void *) (o).i)
#  define lisp_h_XPL(p) lisp_h_XIL (p)
# else
#  define lisp_h_XLI(o) ((o).i)
#  define lisp_h_XIL(i) ((Lisp_Object) {i})
#  define lisp_h_XLP(o) ((void *) (uintptr_t) (o).i)
#  define lisp_h_XPL(p) ((Lisp_Object) {(uintptr_t) (p)})
# endif
366
#endif
367

368
#define lisp_h_CHECK_FIXNUM(x) CHECK_TYPE (FIXNUMP (x), Qfixnump, x)
369
#define lisp_h_CHECK_SYMBOL(x) CHECK_TYPE (SYMBOLP (x), Qsymbolp, x)
370
#define lisp_h_CHECK_TYPE(ok, predicate, x) \
371
   ((ok) ? (void) 0 : wrong_type_argument (predicate, x))
372
#define lisp_h_CONSP(x) TAGGEDP (x, Lisp_Cons)
373
#define lisp_h_EQ(x, y) (XLI (x) == XLI (y))
374 375 376 377 378
#define lisp_h_FIXNUMP(x) \
   (! (((unsigned) (XLI (x) >> (USE_LSB_TAG ? 0 : FIXNUM_BITS)) \
	- (unsigned) (Lisp_Int0 >> !USE_LSB_TAG)) \
       & ((1 << INTTYPEBITS) - 1)))
#define lisp_h_FLOATP(x) TAGGEDP (x, Lisp_Float)
379 380
#define lisp_h_NILP(x) EQ (x, Qnil)
#define lisp_h_SET_SYMBOL_VAL(sym, v) \
381 382 383 384 385
   (eassert ((sym)->u.s.redirect == SYMBOL_PLAINVAL), \
    (sym)->u.s.val.value = (v))
#define lisp_h_SYMBOL_CONSTANT_P(sym) \
   (XSYMBOL (sym)->u.s.trapped_write == SYMBOL_NOWRITE)
#define lisp_h_SYMBOL_TRAPPED_WRITE_P(sym) (XSYMBOL (sym)->u.s.trapped_write)
386
#define lisp_h_SYMBOL_VAL(sym) \
387
   (eassert ((sym)->u.s.redirect == SYMBOL_PLAINVAL), (sym)->u.s.val.value)
388 389 390 391 392 393
#define lisp_h_SYMBOLP(x) TAGGEDP (x, Lisp_Symbol)
#define lisp_h_TAGGEDP(a, tag) \
   (! (((unsigned) (XLI (a) >> (USE_LSB_TAG ? 0 : VALBITS)) \
	- (unsigned) (tag)) \
       & ((1 << GCTYPEBITS) - 1)))
#define lisp_h_VECTORLIKEP(x) TAGGEDP (x, Lisp_Vectorlike)
394 395
#define lisp_h_XCAR(c) XCONS (c)->u.s.car
#define lisp_h_XCDR(c) XCONS (c)->u.s.u.cdr
396
#define lisp_h_XCONS(a) \
397
   (eassert (CONSP (a)), XUNTAG (a, Lisp_Cons, struct Lisp_Cons))
Paul Eggert's avatar
Paul Eggert committed
398
#define lisp_h_XHASH(a) XUFIXNUM_RAW (a)
399
#if USE_LSB_TAG
400
# define lisp_h_make_fixnum_wrap(n) \
401
    XIL ((EMACS_INT) (((EMACS_UINT) (n) << INTTYPEBITS) + Lisp_Int0))
402 403
# if defined HAVE_STATEMENT_EXPRESSIONS && defined HAVE_TYPEOF
#  define lisp_h_make_fixnum(n) \
404
     ({ typeof (+(n)) lisp_h_make_fixnum_n = n; \
405 406 407 408 409
	eassert (!FIXNUM_OVERFLOW_P (lisp_h_make_fixnum_n)); \
	lisp_h_make_fixnum_wrap (lisp_h_make_fixnum_n); })
# else
#  define lisp_h_make_fixnum(n) lisp_h_make_fixnum_wrap (n)
# endif
Paul Eggert's avatar
Paul Eggert committed
410
# define lisp_h_XFIXNUM_RAW(a) (XLI (a) >> INTTYPEBITS)
411 412 413 414 415
# define lisp_h_XTYPE(a) ((enum Lisp_Type) (XLI (a) & ~VALMASK))
#endif

/* When compiling via gcc -O0, define the key operations as macros, as
   Emacs is too slow otherwise.  To disable this optimization, compile
416
   with -DINLINING=false.  */
417 418 419
#if (defined __NO_INLINE__ \
     && ! defined __OPTIMIZE__ && ! defined __OPTIMIZE_SIZE__ \
     && ! (defined INLINING && ! INLINING))
420 421 422 423 424 425
# define DEFINE_KEY_OPS_AS_MACROS true
#else
# define DEFINE_KEY_OPS_AS_MACROS false
#endif

#if DEFINE_KEY_OPS_AS_MACROS
426 427
# define XLI(o) lisp_h_XLI (o)
# define XIL(i) lisp_h_XIL (i)
428 429
# define XLP(o) lisp_h_XLP (o)
# define XPL(p) lisp_h_XPL (p)
430
# define CHECK_FIXNUM(x) lisp_h_CHECK_FIXNUM (x)
431
# define CHECK_SYMBOL(x) lisp_h_CHECK_SYMBOL (x)
432
# define CHECK_TYPE(ok, predicate, x) lisp_h_CHECK_TYPE (ok, predicate, x)
433 434 435
# define CONSP(x) lisp_h_CONSP (x)
# define EQ(x, y) lisp_h_EQ (x, y)
# define FLOATP(x) lisp_h_FLOATP (x)
436
# define FIXNUMP(x) lisp_h_FIXNUMP (x)
437 438 439
# 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)
Noam Postavsky's avatar
Noam Postavsky committed
440
# define SYMBOL_TRAPPED_WRITE_P(sym) lisp_h_SYMBOL_TRAPPED_WRITE_P (sym)
441 442
# define SYMBOL_VAL(sym) lisp_h_SYMBOL_VAL (sym)
# define SYMBOLP(x) lisp_h_SYMBOLP (x)
443
# define TAGGEDP(a, tag) lisp_h_TAGGEDP (a, tag)
444 445 446 447 448 449
# 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)
# if USE_LSB_TAG
450
#  define make_fixnum(n) lisp_h_make_fixnum (n)
Paul Eggert's avatar
Paul Eggert committed
451
#  define XFIXNUM_RAW(a) lisp_h_XFIXNUM_RAW (a)
452 453 454 455 456
#  define XTYPE(a) lisp_h_XTYPE (a)
# endif
#endif


457
/* Define the fundamental Lisp data structures.  */
Jim Blandy's avatar
Jim Blandy committed
458

459 460 461
/* 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
462

463
/* Fixnums use 2 tags, to give them one extra bit, thus
464
   extending their range from, e.g., -2^28..2^28-1 to -2^29..2^29-1.  */
Paul Eggert's avatar
Paul Eggert committed
465
#define INTMASK (EMACS_INT_MAX >> (INTTYPEBITS - 1))
466
#define case_Lisp_Int case Lisp_Int0: case Lisp_Int1
467

Paul Eggert's avatar
Paul Eggert committed
468
/* Idea stolen from GDB.  Pedantic GCC complains about enum bitfields,
Paul Eggert's avatar
Paul Eggert committed
469 470
   and xlc and Oracle Studio c99 complain vociferously about them.  */
#if (defined __STRICT_ANSI__ || defined __IBMC__ \
Paul Eggert's avatar
Paul Eggert committed
471
     || (defined __SUNPRO_C && __STDC__))
472 473 474 475 476 477
#define ENUM_BF(TYPE) unsigned int
#else
#define ENUM_BF(TYPE) enum TYPE
#endif


Jim Blandy's avatar
Jim Blandy committed
478 479
enum Lisp_Type
  {
480
    /* Symbol.  XSYMBOL (object) points to a struct Lisp_Symbol.  */
Paul Eggert's avatar
Paul Eggert committed
481
    Lisp_Symbol = 0,
Jim Blandy's avatar
Jim Blandy committed
482

483
    /* Type 1 is currently unused.  */
Paul Eggert's avatar
Paul Eggert committed
484

485
    /* Fixnum.  XFIXNUM (obj) is the integer value.  */
Paul Eggert's avatar
Paul Eggert committed
486 487
    Lisp_Int0 = 2,
    Lisp_Int1 = USE_LSB_TAG ? 6 : 3,
Jim Blandy's avatar
Jim Blandy committed
488 489

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

493
    /* Vector of Lisp objects, or something resembling it.
494
       XVECTOR (object) points to a struct Lisp_Vector, which contains
495 496
       the size and contents.  The size field also contains the type
       information, if it's not a real vector object.  */
497
    Lisp_Vectorlike = 5,
Jim Blandy's avatar
Jim Blandy committed
498

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

Daniel Colascione's avatar
Daniel Colascione committed
502
    /* Must be last entry in Lisp_Type enumeration.  */
Paul Eggert's avatar
Paul Eggert committed
503
    Lisp_Float = 7
Jim Blandy's avatar
Jim Blandy committed
504 505
  };

506 507 508 509 510 511 512 513 514
/* 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
515
    Lisp_Fwd_Kboard_Obj		/* Fwd to a Lisp_Object field of kboards.  */
516 517
  };

518
/* If you want to define a new Lisp data type, here are some
519
   instructions.
520 521 522

   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
523 524
   displayed to users.  These are Lisp_Misc_Ptr and PVEC_OTHER,
   which are both vectorlike objects.  The former
Paul Eggert's avatar
Paul Eggert committed
525 526 527
   is suitable for stashing a pointer in a Lisp object; the pointer
   might be to some low-level C object that contains auxiliary
   information.  The latter is useful for vector-like Lisp objects
528 529 530 531 532 533 534
   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.

535 536 537 538
   To define a new data type, add a pseudovector subtype by extending
   the pvec_type enumeration.  A pseudovector provides one or more
   slots for Lisp objects, followed by struct members that are
   accessible only from C.
539

Paul Eggert's avatar
Paul Eggert committed
540 541
   There is no way to explicitly free a Lisp Object; only the garbage
   collector frees them.
542 543 544 545 546

   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).

547 548 549 550 551 552 553 554 555
   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
556 557 558 559
   several disparate C structures.

   You also need to add the new type to the constant
   `cl--typeof-types' in lisp/emacs-lisp/cl-preloaded.el.  */
560

Jim Blandy's avatar
Jim Blandy committed
561

562 563 564
/* A Lisp_Object is a tagged pointer or integer.  Ordinarily it is a
   Lisp_Word.  However, if CHECK_LISP_OBJECT_TYPE, it is a wrapper
   around Lisp_Word, to help catch thinkos like 'Lisp_Object x = 0;'.
Jim Blandy's avatar
Jim Blandy committed
565

566 567
   LISP_INITIALLY (W) initializes a Lisp object with a tagged value
   that is a Lisp_Word W.  It can be used in a static initializer.  */
Jim Blandy's avatar
Jim Blandy committed
568

569 570 571 572
#ifdef CHECK_LISP_OBJECT_TYPE
typedef struct Lisp_Object { Lisp_Word i; } Lisp_Object;
# define LISP_INITIALLY(w) {w}
# undef CHECK_LISP_OBJECT_TYPE
573
enum CHECK_LISP_OBJECT_TYPE { CHECK_LISP_OBJECT_TYPE = true };
574 575 576
#else
typedef Lisp_Word Lisp_Object;
# define LISP_INITIALLY(w) (w)
577
enum CHECK_LISP_OBJECT_TYPE { CHECK_LISP_OBJECT_TYPE = false };
578
#endif
579 580

/* Forward declarations.  */
Jim Blandy's avatar
Jim Blandy committed
581

582 583 584 585
/* Defined in this file.  */
INLINE void set_sub_char_table_contents (Lisp_Object, ptrdiff_t,
					      Lisp_Object);

Paul Eggert's avatar
Paul Eggert committed
586 587 588
/* Defined in bignum.c.  */
extern double bignum_to_double (Lisp_Object);
extern Lisp_Object make_bigint (intmax_t);
589
extern Lisp_Object make_biguint (uintmax_t);
Paul Eggert's avatar
Paul Eggert committed
590

591 592 593
/* 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
594

595
/* Defined in data.c.  */
596
extern AVOID wrong_type_argument (Lisp_Object, Lisp_Object);
Noam Postavsky's avatar
Noam Postavsky committed
597

598 599

/* Defined in emacs.c.  */
Daniel Colascione's avatar
Daniel Colascione committed
600 601 602 603

/* Set after Emacs has started up the first time.
   Prevents reinitialization of the Lisp world and keymaps on
   subsequent starts.  */
604 605
extern bool initialized;

606 607
extern struct gflags
{
Daniel Colascione's avatar
Daniel Colascione committed
608
  /* True means this Emacs instance was born to dump.  */
609
#if defined HAVE_PDUMPER || defined HAVE_UNEXEC
Daniel Colascione's avatar
Daniel Colascione committed
610 611 612
  bool will_dump_ : 1;
  bool will_bootstrap_ : 1;
#endif
613
#ifdef HAVE_PDUMPER
Daniel Colascione's avatar
Daniel Colascione committed
614 615 616 617 618 619 620
  /* Set in an Emacs process that will likely dump with pdumper; all
     Emacs processes may dump with pdumper, however.  */
  bool will_dump_with_pdumper_ : 1;
  /* Set in an Emacs process that has been restored from a portable
     dump.  */
  bool dumped_with_pdumper_ : 1;
#endif
621
#ifdef HAVE_UNEXEC
Daniel Colascione's avatar
Daniel Colascione committed
622 623 624 625 626 627 628 629 630 631 632 633
  bool will_dump_with_unexec_ : 1;
  /* Set in an Emacs process that has been restored from an unexec
     dump.  */
  bool dumped_with_unexec_ : 1;
  /* We promise not to unexec: useful for hybrid malloc.  */
  bool will_not_unexec_ : 1;
#endif
} gflags;

INLINE bool
will_dump_p (void)
{
634
#if HAVE_PDUMPER || defined HAVE_UNEXEC
Daniel Colascione's avatar
Daniel Colascione committed
635 636 637 638 639 640 641 642 643
  return gflags.will_dump_;
#else
  return false;
#endif
}

INLINE bool
will_bootstrap_p (void)
{
644
#if HAVE_PDUMPER || defined HAVE_UNEXEC
Daniel Colascione's avatar
Daniel Colascione committed
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
  return gflags.will_bootstrap_;
#else
  return false;
#endif
}

INLINE bool
will_dump_with_pdumper_p (void)
{
#if HAVE_PDUMPER
  return gflags.will_dump_with_pdumper_;
#else
  return false;
#endif
}

INLINE bool
dumped_with_pdumper_p (void)
{
#if HAVE_PDUMPER
  return gflags.dumped_with_pdumper_;
#else
  return false;
#endif
}

INLINE bool
will_dump_with_unexec_p (void)
{
674
#ifdef HAVE_UNEXEC
Daniel Colascione's avatar
Daniel Colascione committed
675
  return gflags.will_dump_with_unexec_;
676 677
#else
  return false;
Daniel Colascione's avatar
Daniel Colascione committed
678 679 680 681 682 683
#endif
}

INLINE bool
dumped_with_unexec_p (void)
{
684
#ifdef HAVE_UNEXEC
Daniel Colascione's avatar
Daniel Colascione committed
685
  return gflags.dumped_with_unexec_;
686 687
#else
  return false;
Daniel Colascione's avatar
Daniel Colascione committed
688 689 690 691 692 693 694 695 696
#endif
}

/* This function is the opposite of will_dump_with_unexec_p(), except
   that it returns false before main runs.  It's important to use
   gmalloc for any pre-main allocations if we're going to unexec.  */
INLINE bool
definitely_will_not_unexec_p (void)
{
697
#ifdef HAVE_UNEXEC
Daniel Colascione's avatar
Daniel Colascione committed
698
  return gflags.will_not_unexec_;
699 700
#else
  return true;
Daniel Colascione's avatar
Daniel Colascione committed
701 702 703
#endif
}

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

707 708 709

/* Low-level conversion and type checking.  */

710 711 712 713
/* Convert among various types use to implement Lisp_Object.  At the
   machine level, these operations may widen or narrow their arguments
   if pointers differ in width from EMACS_INT; otherwise they are
   no-ops.  */
714 715 716 717 718 719 720 721 722 723 724 725 726

INLINE EMACS_INT
(XLI) (Lisp_Object o)
{
  return lisp_h_XLI (o);
}

INLINE Lisp_Object
(XIL) (EMACS_INT i)
{
  return lisp_h_XIL (i);
}

727 728 729 730 731 732 733 734 735 736 737 738
INLINE void *
(XLP) (Lisp_Object o)
{
  return lisp_h_XLP (o);
}

INLINE Lisp_Object
(XPL) (void *p)
{
  return lisp_h_XPL (p);
}

739 740 741 742 743 744 745 746 747 748 749 750 751
/* Extract A's type.  */

INLINE enum Lisp_Type
(XTYPE) (Lisp_Object a)
{
#if USE_LSB_TAG
  return lisp_h_XTYPE (a);
#else
  EMACS_UINT i = XLI (a);
  return USE_LSB_TAG ? i & ~VALMASK : i >> VALBITS;
#endif
}

752 753 754 755 756 757 758 759 760
/* True if A has type tag TAG.
   Equivalent to XTYPE (a) == TAG, but often faster.  */

INLINE bool
(TAGGEDP) (Lisp_Object a, enum Lisp_Type tag)
{
  return lisp_h_TAGGEDP (a, tag);
}

761 762 763 764 765 766
INLINE void
(CHECK_TYPE) (int ok, Lisp_Object predicate, Lisp_Object x)
{
  lisp_h_CHECK_TYPE (ok, predicate, x);
}

767 768
/* Extract A's pointer value, assuming A's Lisp type is TYPE and the
   extracted pointer's type is CTYPE *.  */
769

770 771
#define XUNTAG(a, type, ctype) ((ctype *) \
				((char *) XLP (a) - LISP_WORD_TAG (type)))
772 773 774 775 776 777

/* A forwarding pointer to a value.  It uses a generic pointer to
   avoid alignment bugs that could occur if it used a pointer to a
   union of the possible values (struct Lisp_Objfwd, struct
   Lisp_Intfwd, etc.).  The pointer is packaged inside a struct to
   help static checking.  */
778
typedef struct { void const *fwdptr; } lispfwd;
779 780

/* Interned state of a symbol.  */
781

782 783 784 785 786 787
enum symbol_interned
{
  SYMBOL_UNINTERNED = 0,
  SYMBOL_INTERNED = 1,
  SYMBOL_INTERNED_IN_INITIAL_OBARRAY = 2
};
788

789 790 791 792 793 794 795 796
enum symbol_redirect
{
  SYMBOL_PLAINVAL  = 4,
  SYMBOL_VARALIAS  = 1,
  SYMBOL_LOCALIZED = 2,
  SYMBOL_FORWARDED = 3
};

Noam Postavsky's avatar
Noam Postavsky committed
797 798 799 800 801 802 803
enum symbol_trapped_write
{
  SYMBOL_UNTRAPPED_WRITE = 0,
  SYMBOL_NOWRITE = 1,
  SYMBOL_TRAPPED_WRITE = 2
};

804 805
struct Lisp_Symbol
{
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
  union
  {
    struct
    {
      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;

      /* 0 : normal case, just set the value
	 1 : constant, cannot set, e.g. nil, t, :keywords.
	 2 : trap the write, call watcher functions.  */
      ENUM_BF (symbol_trapped_write) trapped_write : 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;
844
	lispfwd fwd;
845 846 847 848
      } val;

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

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

853 854 855
      /* Next symbol in obarray bucket, if the symbol is interned.  */
      struct Lisp_Symbol *next;
    } s;
Paul Eggert's avatar
Paul Eggert committed
856
    GCALIGNED_UNION_MEMBER
857
  } u;
858
};
Paul Eggert's avatar
Paul Eggert committed
859
verify (GCALIGNED (struct Lisp_Symbol));
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885

/* 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)

886 887 888
/* untagged_ptr represents a pointer before tagging, and Lisp_Word_tag
   contains a possibly-shifted tag to be added to an untagged_ptr to
   convert it to a Lisp_Word.  */
889 890 891 892 893 894 895 896 897 898 899 900 901
#if LISP_WORDS_ARE_POINTERS
/* untagged_ptr is a pointer so that the compiler knows that TAG_PTR
   yields a pointer; this can help with gcc -fcheck-pointer-bounds.
   It is char * so that adding a tag uses simple machine addition.  */
typedef char *untagged_ptr;
typedef uintptr_t Lisp_Word_tag;
#else
/* untagged_ptr is an unsigned integer instead of a pointer, so that
   it can be added to the possibly-wider Lisp_Word_tag type without
   losing information.  */
typedef uintptr_t untagged_ptr;
typedef EMACS_UINT Lisp_Word_tag;
#endif
902

903 904 905 906
/* A integer value tagged with TAG, and otherwise all zero.  */
#define LISP_WORD_TAG(tag) \
  ((Lisp_Word_tag) (tag) << (USE_LSB_TAG ? 0 : VALBITS))

907
/* An initializer for a Lisp_Object that contains TAG along with PTR.  */
908
#define TAG_PTR(tag, ptr) \
909
  LISP_INITIALLY ((Lisp_Word) ((untagged_ptr) (ptr) + LISP_WORD_TAG (tag)))
910

911 912
/* LISPSYM_INITIALLY (Qfoo) is equivalent to Qfoo except it is
   designed for use as an initializer, even for a constant initializer.  */
913 914
#define LISPSYM_INITIALLY(name) \
  TAG_PTR (Lisp_Symbol, (char *) (intptr_t) ((i##name) * sizeof *lispsym))
915

916 917
/* Declare extern constants for Lisp symbols.  These can be helpful
   when using a debugger like GDB, on older platforms where the debug
918 919 920 921 922 923 924 925 926
   format does not represent C macros.  However, they are unbounded
   and would just be asking for trouble if checking pointer bounds.  */
#ifdef __CHKP__
# define DEFINE_LISP_SYMBOL(name)
#else
# define DEFINE_LISP_SYMBOL(name) \
   DEFINE_GDB_SYMBOL_BEGIN (Lisp_Object, name) \
   DEFINE_GDB_SYMBOL_END (LISPSYM_INITIALLY (name))
#endif
927

928 929 930 931
/* The index of the C-defined Lisp symbol SYM.
   This can be used in a static initializer.  */
#define SYMBOL_INDEX(sym) i##sym

932 933
/* By default, define macros for Qt, etc., as this leads to a bit
   better performance in the core Emacs interpreter.  A plugin can
Paul Eggert's avatar
Paul Eggert committed
934
   define DEFINE_NON_NIL_Q_SYMBOL_MACROS to be false, to be portable to
935
   other Emacs instances that assign different values to Qt, etc.  */
Paul Eggert's avatar
Paul Eggert committed
936 937
#ifndef DEFINE_NON_NIL_Q_SYMBOL_MACROS
# define DEFINE_NON_NIL_Q_SYMBOL_MACROS true
938 939
#endif

Daniel Colascione's avatar
Daniel Colascione committed
940 941 942 943 944 945 946 947 948 949 950 951 952
/* True if N is a power of 2.  N should be positive.  */

#define POWER_OF_2(n) (((n) & ((n) - 1)) == 0)

/* Return X rounded to the next multiple of Y.  Y should be positive,
   and Y - 1 + X should not overflow.  Arguments should not have side
   effects, as they are evaluated more than once.  Tune for Y being a
   power of 2.  */

#define ROUNDUP(x, y) (POWER_OF_2 (y)					\
                       ? ((y) - 1 + (x)) & ~ ((y) - 1)			\
                       : ((y) - 1 + (x)) - ((y) - 1 + (x)) % (y))

953
#include "globals.h"
954

Tom Tromey's avatar
Tom Tromey committed
955 956 957
/* Header of vector-like objects.  This documents the layout constraints on
   vectors and pseudovectors (objects of PVEC_xxx subtype).  It also prevents
   compilers from being fooled by Emacs's type punning: XSETPSEUDOVECTOR
958
   and PSEUDOVECTORP cast their pointers to union vectorlike_header *,
Tom Tromey's avatar
Tom Tromey committed
959 960
   because when two such pointers potentially alias, a compiler won't
   incorrectly reorder loads and stores to their size fields.  See
Paul Eggert's avatar
Paul Eggert committed
961 962 963
   Bug#8546.  This union formerly contained more members, and there's
   no compelling reason to change it to a struct merely because the
   number of members has been reduced to one.  */
964
union vectorlike_header
Tom Tromey's avatar
Tom Tromey committed
965
  {
966
    /* The main member contains various pieces of information:
Tom Tromey's avatar
Tom Tromey committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
       - The MSB (ARRAY_MARK_FLAG) holds the gcmarkbit.
       - The next bit (PSEUDOVECTOR_FLAG) indicates whether this is a plain
         vector (0) or a pseudovector (1).
       - If PSEUDOVECTOR_FLAG is 0, the rest holds the size (number
         of slots) of the vector.
       - If PSEUDOVECTOR_FLAG is 1, the rest is subdivided into three fields:
	 - a) pseudovector subtype held in PVEC_TYPE_MASK field;
	 - b) number of Lisp_Objects slots at the beginning of the object
	   held in PSEUDOVECTOR_SIZE_MASK field.  These objects are always
	   traced by the GC;
	 - c) size of the rest fields held in PSEUDOVECTOR_REST_MASK and
	   measured in word_size units.  Rest fields may also include
	   Lisp_Objects, but these objects usually needs some special treatment
	   during GC.
	 There are some exceptions.  For PVEC_FREE, b) is always zero.  For
	 PVEC_BOOL_VECTOR and PVEC_SUBR, both b) and c) are always zero.
	 Current layout limits the pseudovectors to 63 PVEC_xxx subtypes,
	 4095 Lisp_Objects in GC-ed area and 4095 word-sized other slots.  */
    ptrdiff_t size;
  };

988 989 990 991 992
INLINE bool
(SYMBOLP) (Lisp_Object x)
{
  return lisp_h_SYMBOLP (x);
}
Tom Tromey's avatar
Tom Tromey committed
993

994
INLINE struct Lisp_Symbol * ATTRIBUTE_NO_SANITIZE_UNDEFINED
995
XSYMBOL (Lisp_Object a)
996 997
{
  eassert (SYMBOLP (a));
998
  intptr_t i = (intptr_t) XUNTAG (a, Lisp_Symbol, struct Lisp_Symbol);
999
  void *p = (char *) lispsym + i;
1000
#ifdef __CHKP__
1001 1002 1003
  /* Bypass pointer checking.  Although this could be improved it is
     probably not worth the trouble.  */
  p = __builtin___bnd_set_ptr_bounds (p, sizeof (struct Lisp_Symbol));
1004
#endif
1005
  return p;
1006
}
1007

1008 1009
INLINE Lisp_Object
make_lisp_symbol (struct Lisp_Symbol *sym)
1010
{
1011
#ifdef __CHKP__
1012 1013 1014 1015 1016
  /* Although '__builtin___bnd_narrow_ptr_bounds (sym, sym, sizeof *sym)'
     should be more efficient, it runs afoul of GCC bug 83251
     <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83251>.
     Also, attempting to call __builtin___bnd_chk_ptr_bounds (sym, sizeof *sym)
     here seems to trigger a GCC bug, as yet undiagnosed.  */
1017 1018
  char *addr = __builtin___bnd_set_ptr_bounds (sym, sizeof *sym);
  char *symoffset = addr - (intptr_t) lispsym;
1019
#else
1020 1021
  /* If !__CHKP__, GCC 7 x86-64 generates faster code if lispsym is
     cast to char * rather than to intptr_t.  */
1022 1023 1024
  char *symoffset = (char *) ((char *) sym - (char *) lispsym);
#endif
  Lisp_Object a = TAG_PTR (Lisp_Symbol, symoffset);
1025 1026
  eassert (XSYMBOL (a) == sym);
  return a;
1027 1028 1029
}

INLINE Lisp_Object
1030
builtin_lisp_symbol (int index)
1031
{
1032
  return make_lisp_symbol (&lispsym[index]);
1033 1034
}

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
INLINE bool
c_symbol_p (struct Lisp_Symbol *sym)
{
  char *bp = (char *) lispsym;
  char *sp = (char *) sym;
  if (PTRDIFF_MAX < INTPTR_MAX)
    return bp <= sp && sp < bp + sizeof lispsym;
  else
    {
      ptrdiff_t offset = sp - bp;
      return 0 <= offset && offset < sizeof lispsym;
    }
}

1049 1050 1051
INLINE void
(CHECK_SYMBOL) (Lisp_Object x)
{
Andreas Schwab's avatar
Andreas Schwab committed
1052
  lisp_h_CHECK_SYMBOL (x);
1053
}
1054

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

1057
DEFINE_GDB_SYMBOL_BEGIN (ptrdiff_t, ARRAY_MARK_FLAG)
1058
# define ARRAY_MARK_FLAG PTRDIFF_MIN
1059
DEFINE_GDB_SYMBOL_END (ARRAY_MARK_FLAG)
1060

1061 1062
/* In the size word of a struct Lisp_Vector, this bit means it's really
   some other vector-like object.  */
1063
DEFINE_GDB_SYMBOL_BEGIN (ptrdiff_t, PSEUDOVECTOR_FLAG)
1064
# define PSEUDOVECTOR_FLAG (PTRDIFF_MAX - PTRDIFF_MAX / 2)
1065
DEFINE_GDB_SYMBOL_END (PSEUDOVECTOR_FLAG)
1066

1067
/* In a pseudovector, the size field actually contains a word with one
1068 1069
   PSEUDOVECTOR_FLAG bit set, and one of the following values extracted
   with PVEC_TYPE_MASK to indicate the actual type.  */
1070 1071
enum pvec_type
{
1072
  PVEC_NORMAL_VECTOR,
1073
  PVEC_FREE,
1074 1075 1076 1077 1078 1079
  PVEC_BIGNUM,
  PVEC_MARKER,
  PVEC_OVERLAY,
  PVEC_FINALIZER,
  PVEC_MISC_PTR,
  PVEC_USER_PTR,
1080 1081 1082 1083 1084 1085 1086 1087 1088
  PVEC_PROCESS,
  PVEC_FRAME,
  PVEC_WINDOW,
  PVEC_BOOL_VECTOR,
  PVEC_BUFFER,
  PVEC_HASH_TABLE,
  PVEC_TERMINAL,
  PVEC_WINDOW_CONFIGURATION,
  PVEC_SUBR,
1089
  PVEC_OTHER,            /* Should never be visible to Elisp code.  */
1090 1091
  PVEC_XWIDGET,
  PVEC_XWIDGET_VIEW,
1092
  PVEC_THREAD,
1093
  PVEC_MUTEX,
Tom Tromey's avatar
Tom Tromey committed
1094
  PVEC_CONDVAR,