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

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

This file is part of GNU Emacs.

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

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

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

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

23
#include <stdalign.h>
24
#include <stdarg.h>
25
#include <stddef.h>
26
#include <inttypes.h>
27
#include <limits.h>
28

29 30
#include <intprops.h>

31 32 33 34 35 36
/* 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))

Paul Eggert's avatar
Paul Eggert committed
37 38 39 40
/* 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 */
41
#ifndef EMACS_INT_MAX
Paul Eggert's avatar
Paul Eggert committed
42
# if LONG_MAX < LLONG_MAX && defined WIDE_EMACS_INT
43 44
typedef long long int EMACS_INT;
typedef unsigned long long int EMACS_UINT;
Paul Eggert's avatar
Paul Eggert committed
45
#  define EMACS_INT_MAX LLONG_MAX
46
#  define pI "ll"
Paul Eggert's avatar
Paul Eggert committed
47
# elif INT_MAX < LONG_MAX
48 49
typedef long int EMACS_INT;
typedef unsigned long int EMACS_UINT;
Paul Eggert's avatar
Paul Eggert committed
50
#  define EMACS_INT_MAX LONG_MAX
51 52
#  define pI "l"
# else
53 54
typedef int EMACS_INT;
typedef unsigned int EMACS_UINT;
Paul Eggert's avatar
Paul Eggert committed
55
#  define EMACS_INT_MAX INT_MAX
56 57
#  define pI ""
# endif
58
#endif
Paul Eggert's avatar
Paul Eggert committed
59 60 61 62 63 64 65 66 67 68

/* Number of bits in some machine integer types.  */
enum
  {
    BITS_PER_CHAR      = CHAR_BIT,
    BITS_PER_SHORT     = CHAR_BIT * sizeof (short),
    BITS_PER_INT       = CHAR_BIT * sizeof (int),
    BITS_PER_LONG      = CHAR_BIT * sizeof (long int),
    BITS_PER_EMACS_INT = CHAR_BIT * sizeof (EMACS_INT)
  };
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/* 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

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
/* 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

102
/* Extra internal type checking?  */
103

104 105 106 107 108 109
/* Define an Emacs version of 'assert (COND)', since some
   system-defined 'assert's are flaky.  COND should be free of side
   effects; it may or may not be evaluated.  */
#ifndef ENABLE_CHECKING
# define eassert(X) ((void) (0 && (X))) /* Check that X compiles.  */
#else /* ENABLE_CHECKING */
110

111
extern _Noreturn void die (const char *, const char *, int);
112

113 114 115 116 117
/* 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
118
   eassert macro altogether, e.g., if XSTRING (x) uses eassert to test
119 120
   STRINGP (x), but a particular use of XSTRING is invoked only after
   testing that STRINGP (x) is true, making the test redundant.  */
121 122
extern int suppress_checking EXTERNALLY_VISIBLE;

123 124 125 126
# define eassert(cond)						\
   ((cond) || suppress_checking					\
    ? (void) 0							\
    : die ("assertion failed: " # cond, __FILE__, __LINE__))
127
#endif /* ENABLE_CHECKING */
128

129 130 131
/* 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
132

133
/***** Select the tagging scheme.  *****/
134
/* The following option controls the tagging scheme:
135 136 137 138
   - 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.

139 140
   If ! USE_LSB_TAG, then use the top 3 bits for tagging, thus
   restricting our possible address range.
141 142 143 144 145 146

   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
   on the few static Lisp_Objects used: all the defsubr as well
   as the two special buffers buffer_defaults and buffer_local_symbols.  */

Paul Eggert's avatar
Paul Eggert committed
147 148 149 150 151
enum Lisp_Bits
  {
    /* Number of bits in a Lisp_Object tag.  This can be used in #if,
       and for GDB's sake also as a regular symbol.  */
    GCTYPEBITS =
152
#define GCTYPEBITS 3
Paul Eggert's avatar
Paul Eggert committed
153 154
	GCTYPEBITS,

155 156 157 158 159 160
    /* 2**GCTYPEBITS.  This must also be a macro that expands to a
       literal integer constant, for MSVC.  */
    GCALIGNMENT =
#define GCALIGNMENT 8
	GCALIGNMENT,

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

Paul Eggert's avatar
Paul Eggert committed
164 165 166 167 168 169
    /* 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
170

171 172 173 174
#if GCALIGNMENT != 1 << GCTYPEBITS
# error "GCALIGNMENT and GCTYPEBITS are inconsistent"
#endif

Paul Eggert's avatar
Paul Eggert committed
175 176 177 178
/* 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.
   This can be used in #if, e.g., '#if VAL_MAX < UINTPTR_MAX' below.  */
#define VAL_MAX (EMACS_INT_MAX >> (GCTYPEBITS - 1))
179

180 181 182 183 184 185
/* Unless otherwise specified, use USE_LSB_TAG on systems where:  */
#ifndef USE_LSB_TAG
/* 1.  We know malloc returns a multiple of 8.  */
# if (defined GNU_MALLOC || defined DOUG_LEA_MALLOC || defined __GLIBC__ \
      || defined DARWIN_OS || defined __sun)
/* 2.  We can specify multiple-of-8 alignment on static variables.  */
186
#  ifdef alignas
187 188
/* 3.  Pointers-as-ints exceed VAL_MAX.
   On hosts where pointers-as-ints do not exceed VAL_MAX, USE_LSB_TAG is:
189 190
    a. unnecessary, because the top bits of an EMACS_INT are unused, and
    b. slower, because it typically requires extra masking.
191 192 193 194
   So, default USE_LSB_TAG to 1 only on hosts where it might be useful.  */
#   if VAL_MAX < UINTPTR_MAX
#    define USE_LSB_TAG 1
#   endif
195
#  endif
196 197
# endif
#endif
Paul Eggert's avatar
Paul Eggert committed
198 199
#ifdef USE_LSB_TAG
# undef USE_LSB_TAG
Paul Eggert's avatar
Paul Eggert committed
200
enum enum_USE_LSB_TAG { USE_LSB_TAG = 1 };
Paul Eggert's avatar
Paul Eggert committed
201 202
# define USE_LSB_TAG 1
#else
Paul Eggert's avatar
Paul Eggert committed
203
enum enum_USE_LSB_TAG { USE_LSB_TAG = 0 };
204 205
# define USE_LSB_TAG 0
#endif
206

207 208
#ifndef alignas
# define alignas(alignment) /* empty */
209
# if USE_LSB_TAG
210
#  error "USE_LSB_TAG requires alignas"
211 212 213
# endif
#endif

214

215
/* Define the fundamental Lisp data structures.  */
Jim Blandy's avatar
Jim Blandy committed
216

217
/* This is the set of Lisp data types.  */
Jim Blandy's avatar
Jim Blandy committed
218

219 220
/* 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.  */
221
static EMACS_INT const INTMASK = EMACS_INT_MAX >> (INTTYPEBITS - 1);
222
#define case_Lisp_Int case Lisp_Int0: case Lisp_Int1
223
#define LISP_INT_TAG_P(x) (((x) & ~Lisp_Int1) == 0)
224

225 226 227 228 229 230 231 232 233
/* Stolen from GDB.  The only known compiler that doesn't support
   enums in bitfields is MSVC.  */
#ifdef _MSC_VER
#define ENUM_BF(TYPE) unsigned int
#else
#define ENUM_BF(TYPE) enum TYPE
#endif


Jim Blandy's avatar
Jim Blandy committed
234 235
enum Lisp_Type
  {
236
    /* Integer.  XINT (obj) is the integer value.  */
237
    Lisp_Int0 = 0,
238
    Lisp_Int1 = USE_LSB_TAG ? 1 << INTTYPEBITS : 1,
Jim Blandy's avatar
Jim Blandy committed
239

240
    /* Symbol.  XSYMBOL (object) points to a struct Lisp_Symbol.  */
241
    Lisp_Symbol = 2,
Jim Blandy's avatar
Jim Blandy committed
242

243 244
    /* Miscellaneous.  XMISC (object) points to a union Lisp_Misc,
       whose first member indicates the subtype.  */
245
    Lisp_Misc = 3,
Jim Blandy's avatar
Jim Blandy committed
246 247

    /* String.  XSTRING (object) points to a struct Lisp_String.
248
       The length of the string, and its contents, are stored therein.  */
249
    Lisp_String = USE_LSB_TAG ? 1 : 1 << INTTYPEBITS,
Jim Blandy's avatar
Jim Blandy committed
250

251
    /* Vector of Lisp objects, or something resembling it.
252
       XVECTOR (object) points to a struct Lisp_Vector, which contains
253 254
       the size and contents.  The size field also contains the type
       information, if it's not a real vector object.  */
255
    Lisp_Vectorlike = 5,
Jim Blandy's avatar
Jim Blandy committed
256

257
    /* Cons.  XCONS (object) points to a struct Lisp_Cons.  */
258
    Lisp_Cons = 6,
259

260
    Lisp_Float = 7,
Jim Blandy's avatar
Jim Blandy committed
261 262
  };

263
/* This is the set of data types that share a common structure.
264 265 266 267
   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
268 269
enum Lisp_Misc_Type
  {
270
    Lisp_Misc_Free = 0x5eab,
271
    Lisp_Misc_Marker,
272
    Lisp_Misc_Overlay,
273
    Lisp_Misc_Save_Value,
274 275 276 277 278
    /* 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
279 280
  };

281 282 283 284 285 286 287 288 289 290 291 292
/* 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.  */
    Lisp_Fwd_Kboard_Obj,	/* Fwd to a Lisp_Object field of kboards.  */
  };

293
#ifdef CHECK_LISP_OBJECT_TYPE
Jim Blandy's avatar
Jim Blandy committed
294

295 296 297 298 299 300 301 302 303
typedef struct { EMACS_INT i; } Lisp_Object;

#define XLI(o) (o).i
static inline Lisp_Object
XIL (EMACS_INT i)
{
  Lisp_Object o = { i };
  return o;
}
Jim Blandy's avatar
Jim Blandy committed
304

Paul Eggert's avatar
Paul Eggert committed
305
static inline Lisp_Object
306 307 308 309
LISP_MAKE_RVALUE (Lisp_Object o)
{
    return o;
}
310 311

#define LISP_INITIALLY_ZERO {0}
312

Paul Eggert's avatar
Paul Eggert committed
313 314
#undef CHECK_LISP_OBJECT_TYPE
enum CHECK_LISP_OBJECT_TYPE { CHECK_LISP_OBJECT_TYPE = 1 };
315
#else /* CHECK_LISP_OBJECT_TYPE */
Jim Blandy's avatar
Jim Blandy committed
316

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

319
typedef EMACS_INT Lisp_Object;
320 321
#define XLI(o) (o)
#define XIL(i) (i)
322
#define LISP_MAKE_RVALUE(o) (0+(o))
323
#define LISP_INITIALLY_ZERO 0
Paul Eggert's avatar
Paul Eggert committed
324
enum CHECK_LISP_OBJECT_TYPE { CHECK_LISP_OBJECT_TYPE = 0 };
325
#endif /* CHECK_LISP_OBJECT_TYPE */
Jim Blandy's avatar
Jim Blandy committed
326

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

329
static ptrdiff_t const ARRAY_MARK_FLAG = PTRDIFF_MIN;
330

331 332
/* In the size word of a struct Lisp_Vector, this bit means it's really
   some other vector-like object.  */
333
static ptrdiff_t const PSEUDOVECTOR_FLAG = PTRDIFF_MAX - PTRDIFF_MAX / 2;
334

335
/* In a pseudovector, the size field actually contains a word with one
336
   PSEUDOVECTOR_FLAG bit set, and exactly one of the following bits to
337
   indicate the actual type.
338 339 340 341
   We use a bitset, even tho only one of the bits can be set at any
   particular time just so as to be able to use micro-optimizations such as
   testing membership of a particular subset of pseudovectors in Fequal.
   It is not crucial, but there are plenty of bits here, so why not do it?  */
342 343
enum pvec_type
{
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
  PVEC_NORMAL_VECTOR = 0,	/* Unused!  */
  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,
  /* These last 4 are special because we OR them in fns.c:internal_equal,
     so they have to use a disjoint bit pattern:
     if (!(size & (PVEC_COMPILED | PVEC_CHAR_TABLE
                   | PVEC_SUB_CHAR_TABLE | PVEC_FONT))) */
  PVEC_COMPILED			= 0x10,
  PVEC_CHAR_TABLE		= 0x20,
  PVEC_SUB_CHAR_TABLE		= 0x30,
  PVEC_FONT			= 0x40
364
};
365

Paul Eggert's avatar
Paul Eggert committed
366 367 368 369 370 371 372 373 374
/* DATA_SEG_BITS forces extra bits to be or'd in with any pointers
   which were stored in a Lisp_Object.  */
#ifndef DATA_SEG_BITS
# define DATA_SEG_BITS 0
#endif
enum { gdb_DATA_SEG_BITS = DATA_SEG_BITS };
#undef DATA_SEG_BITS

enum More_Lisp_Bits
375
  {
Paul Eggert's avatar
Paul Eggert committed
376 377 378 379 380 381 382
    DATA_SEG_BITS = gdb_DATA_SEG_BITS,

    /* 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.  */
383 384
    PSEUDOVECTOR_SIZE_BITS = 16,
    PSEUDOVECTOR_SIZE_MASK = (1 << PSEUDOVECTOR_SIZE_BITS) - 1,
Paul Eggert's avatar
Paul Eggert committed
385
    PVEC_TYPE_MASK = 0x0fff << PSEUDOVECTOR_SIZE_BITS,
386

Paul Eggert's avatar
Paul Eggert committed
387 388 389 390
    /* Number of bits to put in each character in the internal representation
       of bool vectors.  This should not vary across implementations.  */
    BOOL_VECTOR_BITS_PER_CHAR = 8
  };
Jim Blandy's avatar
Jim Blandy committed
391 392 393

/* These macros extract various sorts of values from a Lisp_Object.
 For example, if tem is a Lisp_Object whose type is Lisp_Cons,
394
 XCONS (tem) is the struct Lisp_Cons * pointing to the memory for that cons.  */
Jim Blandy's avatar
Jim Blandy committed
395

396
/* Return a perfect hash of the Lisp_Object representation.  */
397
#define XHASH(a) XLI (a)
398

399
#if USE_LSB_TAG
Kenichi Handa's avatar
Kenichi Handa committed
400

401
static int const VALMASK = -1 << GCTYPEBITS;
402 403 404 405 406
#define TYPEMASK ((1 << GCTYPEBITS) - 1)
#define XTYPE(a) ((enum Lisp_Type) (XLI (a) & TYPEMASK))
#define XINT(a) (XLI (a) >> INTTYPEBITS)
#define XUINT(a) ((EMACS_UINT) XLI (a) >> INTTYPEBITS)
#define make_number(N) XIL ((EMACS_INT) (N) << INTTYPEBITS)
407
#define XSET(var, type, ptr)						   \
408 409
  (eassert (XTYPE (XIL ((intptr_t) (ptr))) == 0), /* Check alignment.  */  \
   (var) = XIL ((type) | (intptr_t) (ptr)))
Kenichi Handa's avatar
Kenichi Handa committed
410

411 412
#define XPNTR(a) ((intptr_t) (XLI (a) & ~TYPEMASK))
#define XUNTAG(a, type) ((intptr_t) (XLI (a) - (type)))
Kenichi Handa's avatar
Kenichi Handa committed
413 414 415

#else  /* not USE_LSB_TAG */

416
static EMACS_INT const VALMASK = VAL_MAX;
Kenichi Handa's avatar
Kenichi Handa committed
417

418
#define XTYPE(a) ((enum Lisp_Type) ((EMACS_UINT) XLI (a) >> VALBITS))
Jim Blandy's avatar
Jim Blandy committed
419

Karl Heuer's avatar
Karl Heuer committed
420 421
/* For integers known to be positive, XFASTINT provides fast retrieval
   and XSETFASTINT provides fast storage.  This takes advantage of the
422 423 424
   fact that Lisp integers have zero-bits in their tags.  */
#define XFASTINT(a) (XLI (a) + 0)
#define XSETFASTINT(a, b) ((a) = XIL (b))
Jim Blandy's avatar
Jim Blandy committed
425

426
/* Extract the value of a Lisp_Object as a (un)signed integer.  */
Jim Blandy's avatar
Jim Blandy committed
427

428 429 430
#define XINT(a) (XLI (a) << INTTYPEBITS >> INTTYPEBITS)
#define XUINT(a) ((EMACS_UINT) (XLI (a) & INTMASK))
#define make_number(N) XIL ((EMACS_INT) (N) & INTMASK)
Jim Blandy's avatar
Jim Blandy committed
431

432 433
#define XSET(var, type, ptr)				      \
  ((var) = XIL ((EMACS_INT) ((EMACS_UINT) (type) << VALBITS)  \
434
		+ ((intptr_t) (ptr) & VALMASK)))
435

436
#if DATA_SEG_BITS
437 438
/* DATA_SEG_BITS forces extra bits to be or'd in with any pointers
   which were stored in a Lisp_Object */
439
#define XPNTR(a) ((uintptr_t) ((XLI (a) & VALMASK)) | DATA_SEG_BITS))
440
#else
441
#define XPNTR(a) ((uintptr_t) (XLI (a) & VALMASK))
442
#endif
443

Kenichi Handa's avatar
Kenichi Handa committed
444
#endif /* not USE_LSB_TAG */
Jim Blandy's avatar
Jim Blandy committed
445

446 447 448 449 450 451 452 453
/* For integers known to be positive, XFASTINT sometimes provides
   faster retrieval and XSETFASTINT provides faster storage.
   If not, fallback on the non-accelerated path.  */
#ifndef XFASTINT
# define XFASTINT(a) (XINT (a))
# define XSETFASTINT(a, b) (XSETINT (a, b))
#endif

454 455 456 457 458 459 460
/* Extract the pointer value of the Lisp object A, under the
   assumption that A's type is TYPE.  This is a fallback
   implementation if nothing faster is available.  */
#ifndef XUNTAG
# define XUNTAG(a, type) XPNTR (a)
#endif

461 462
#define EQ(x, y) (XHASH (x) == XHASH (y))

463
/* Largest and smallest representable fixnum values.  These are the C
464 465 466
   values.  They are macros for use in static initializers, and
   constants for visibility to GDB.  */
static EMACS_INT const MOST_POSITIVE_FIXNUM =
467
#define MOST_POSITIVE_FIXNUM (EMACS_INT_MAX >> INTTYPEBITS)
468 469
	MOST_POSITIVE_FIXNUM;
static EMACS_INT const MOST_NEGATIVE_FIXNUM =
470
#define MOST_NEGATIVE_FIXNUM (-1 - MOST_POSITIVE_FIXNUM)
471
	MOST_NEGATIVE_FIXNUM;
472

473 474
/* Value is non-zero if I doesn't fit into a Lisp fixnum.  It is
   written this way so that it also works if I is of unsigned
475
   type or if I is a NaN.  */
476 477

#define FIXNUM_OVERFLOW_P(i) \
478
  (! ((0 <= (i) || MOST_NEGATIVE_FIXNUM <= (i)) && (i) <= MOST_POSITIVE_FIXNUM))
479

480 481 482 483 484 485
static inline ptrdiff_t
clip_to_bounds (ptrdiff_t lower, EMACS_INT num, ptrdiff_t upper)
{
  return num < lower ? lower : num <= upper ? num : upper;
}

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

488 489 490 491 492 493 494 495 496 497
#define XCONS(a)   (eassert (CONSP (a)), \
		    (struct Lisp_Cons *) XUNTAG (a, Lisp_Cons))
#define XVECTOR(a) (eassert (VECTORLIKEP (a)), \
		    (struct Lisp_Vector *) XUNTAG (a, Lisp_Vectorlike))
#define XSTRING(a) (eassert (STRINGP (a)), \
		    (struct Lisp_String *) XUNTAG (a, Lisp_String))
#define XSYMBOL(a) (eassert (SYMBOLP (a)), \
		    (struct Lisp_Symbol *) XUNTAG (a, Lisp_Symbol))
#define XFLOAT(a)  (eassert (FLOATP (a)), \
		    (struct Lisp_Float *) XUNTAG (a, Lisp_Float))
498 499

/* Misc types.  */
500

501
#define XMISC(a)	((union Lisp_Misc *) XUNTAG (a, Lisp_Misc))
Juanma Barranquero's avatar
Juanma Barranquero committed
502
#define XMISCANY(a)	(eassert (MISCP (a)), &(XMISC (a)->u_any))
503
#define XMISCTYPE(a)   (XMISCANY (a)->type)
Juanma Barranquero's avatar
Juanma Barranquero committed
504 505 506
#define XMARKER(a)	(eassert (MARKERP (a)), &(XMISC (a)->u_marker))
#define XOVERLAY(a)	(eassert (OVERLAYP (a)), &(XMISC (a)->u_overlay))
#define XSAVE_VALUE(a)	(eassert (SAVE_VALUEP (a)), &(XMISC (a)->u_save_value))
507 508 509 510 511 512 513

/* Forwarding object types.  */

#define XFWDTYPE(a)     (a->u_intfwd.type)
#define XINTFWD(a)	(eassert (INTFWDP (a)), &((a)->u_intfwd))
#define XBOOLFWD(a)	(eassert (BOOLFWDP (a)), &((a)->u_boolfwd))
#define XOBJFWD(a)	(eassert (OBJFWDP (a)), &((a)->u_objfwd))
514
#define XBUFFER_OBJFWD(a) \
515
  (eassert (BUFFER_OBJFWDP (a)), &((a)->u_buffer_objfwd))
516
#define XKBOARD_OBJFWD(a) \
517
  (eassert (KBOARD_OBJFWDP (a)), &((a)->u_kboard_objfwd))
Jim Blandy's avatar
Jim Blandy committed
518

519
/* Pseudovector types.  */
520

521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
#define XPROCESS(a) (eassert (PROCESSP (a)), \
		     (struct Lisp_Process *) XUNTAG (a, Lisp_Vectorlike))
#define XWINDOW(a) (eassert (WINDOWP (a)), \
		    (struct window *) XUNTAG (a, Lisp_Vectorlike))
#define XTERMINAL(a) (eassert (TERMINALP (a)), \
		      (struct terminal *) XUNTAG (a, Lisp_Vectorlike))
#define XSUBR(a) (eassert (SUBRP (a)), \
		  (struct Lisp_Subr *) XUNTAG (a, Lisp_Vectorlike))
#define XBUFFER(a) (eassert (BUFFERP (a)), \
		    (struct buffer *) XUNTAG (a, Lisp_Vectorlike))
#define XCHAR_TABLE(a) (eassert (CHAR_TABLE_P (a)), \
			(struct Lisp_Char_Table *) XUNTAG (a, Lisp_Vectorlike))
#define XSUB_CHAR_TABLE(a) (eassert (SUB_CHAR_TABLE_P (a)), \
			    ((struct Lisp_Sub_Char_Table *) \
			     XUNTAG (a, Lisp_Vectorlike)))
#define XBOOL_VECTOR(a) (eassert (BOOL_VECTOR_P (a)), \
			 ((struct Lisp_Bool_Vector *) \
			  XUNTAG (a, Lisp_Vectorlike)))
539 540

/* Construct a Lisp_Object from a value or address.  */
541

542
#define XSETINT(a, b) (a) = make_number (b)
543
#define XSETCONS(a, b) XSET (a, Lisp_Cons, b)
544
#define XSETVECTOR(a, b) XSET (a, Lisp_Vectorlike, b)
545 546 547
#define XSETSTRING(a, b) XSET (a, Lisp_String, b)
#define XSETSYMBOL(a, b) XSET (a, Lisp_Symbol, b)
#define XSETFLOAT(a, b) XSET (a, Lisp_Float, b)
548 549

/* Misc types.  */
550

551
#define XSETMISC(a, b) XSET (a, Lisp_Misc, b)
Richard M. Stallman's avatar
Richard M. Stallman committed
552
#define XSETMARKER(a, b) (XSETMISC (a, b), XMISCTYPE (a) = Lisp_Misc_Marker)
553 554

/* Pseudovector types.  */
555

Juanma Barranquero's avatar
Juanma Barranquero committed
556
#define XSETPVECTYPE(v, code) XSETTYPED_PVECTYPE (v, header.size, code)
557
#define XSETTYPED_PVECTYPE(v, size_member, code) \
558
  ((v)->size_member |= PSEUDOVECTOR_FLAG | ((code) << PSEUDOVECTOR_SIZE_BITS))
559
#define XSETPVECTYPESIZE(v, code, sizeval) \
560 561 562
  ((v)->header.size = (PSEUDOVECTOR_FLAG			\
		       | ((code) << PSEUDOVECTOR_SIZE_BITS)	\
		       | (sizeval)))
563 564

/* The cast to struct vectorlike_header * avoids aliasing issues.  */
565
#define XSETPSEUDOVECTOR(a, b, code) \
566 567 568 569 570
  XSETTYPED_PSEUDOVECTOR (a, b,					\
			  (((struct vectorlike_header *)	\
			    XUNTAG (a, Lisp_Vectorlike))	\
			   ->size),				\
			  code)
571
#define XSETTYPED_PSEUDOVECTOR(a, b, size, code)			\
572
  (XSETVECTOR (a, b),							\
573
   eassert ((size & (PSEUDOVECTOR_FLAG | PVEC_TYPE_MASK))		\
574
	    == (PSEUDOVECTOR_FLAG | (code << PSEUDOVECTOR_SIZE_BITS))))
575

576 577 578 579
#define XSETWINDOW_CONFIGURATION(a, b) \
  (XSETPSEUDOVECTOR (a, b, PVEC_WINDOW_CONFIGURATION))
#define XSETPROCESS(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_PROCESS))
#define XSETWINDOW(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_WINDOW))
580
#define XSETTERMINAL(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_TERMINAL))
581
/* XSETSUBR is special since Lisp_Subr lacks struct vectorlike_header.  */
582 583
#define XSETSUBR(a, b) \
  XSETTYPED_PSEUDOVECTOR (a, b, XSUBR (a)->size, PVEC_SUBR)
584
#define XSETCOMPILED(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_COMPILED))
585
#define XSETBUFFER(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_BUFFER))
586 587
#define XSETCHAR_TABLE(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_CHAR_TABLE))
#define XSETBOOL_VECTOR(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_BOOL_VECTOR))
588
#define XSETSUB_CHAR_TABLE(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_SUB_CHAR_TABLE))
589 590 591

/* Convenience macros for dealing with Lisp arrays.  */

592
#define AREF(ARRAY, IDX)	XVECTOR ((ARRAY))->contents[IDX]
593
#define ASIZE(ARRAY)		XVECTOR ((ARRAY))->header.size
594
/* The IDX==IDX tries to detect when the macro argument is side-effecting.  */
Stefan Monnier's avatar
Stefan Monnier committed
595 596 597
#define ASET(ARRAY, IDX, VAL)	\
  (eassert ((IDX) == (IDX)),				\
   eassert ((IDX) >= 0 && (IDX) < ASIZE (ARRAY)),	\
598
   AREF ((ARRAY), (IDX)) = (VAL))
599

600 601
/* Convenience macros for dealing with Lisp strings.  */

Ken Raeburn's avatar
Ken Raeburn committed
602
#define SDATA(string)		(XSTRING (string)->data + 0)
603 604
#define SREF(string, index)	(SDATA (string)[index] + 0)
#define SSET(string, index, new) (SDATA (string)[index] = (new))
605 606 607
#define SCHARS(string)		(XSTRING (string)->size + 0)
#define SBYTES(string)		(STRING_BYTES (XSTRING (string)) + 0)

608 609 610
/* Avoid "differ in sign" warnings.  */
#define SSDATA(x)  ((char *) SDATA (x))

611 612
#define STRING_SET_CHARS(string, newsize) \
    (XSTRING (string)->size = (newsize))
613

Ken Raeburn's avatar
Ken Raeburn committed
614
#define STRING_COPYIN(string, index, new, count) \
615
    memcpy (SDATA (string) + index, new, count)
Ken Raeburn's avatar
Ken Raeburn committed
616

617 618 619 620 621 622
/* Type checking.  */

#define CHECK_TYPE(ok, Qxxxp, x) \
  do { if (!(ok)) wrong_type_argument (Qxxxp, (x)); } while (0)


Jim Blandy's avatar
Jim Blandy committed
623

624
/* See the macros in intervals.h.  */
625 626 627 628

typedef struct interval *INTERVAL;

/* Complain if object is not string or buffer type */
629
#define CHECK_STRING_OR_BUFFER(x) \
630 631
  CHECK_TYPE (STRINGP (x) || BUFFERP (x), Qbuffer_or_string_p, x)

632

Jim Blandy's avatar
Jim Blandy committed
633 634 635 636
/* In a cons, the markbit of the car is the gc mark bit */

struct Lisp_Cons
  {
637 638
    /* Please do not use the names of these elements in code other
       than the core lisp implementation.  Use XCAR and XCDR below.  */
639 640 641 642 643 644
    Lisp_Object car;
    union
    {
      Lisp_Object cdr;
      struct Lisp_Cons *chain;
    } u;
Jim Blandy's avatar
Jim Blandy committed
645 646
  };

647
/* Take the car or cdr of something known to be a cons cell.  */
648 649 650 651 652 653 654
/* The _AS_LVALUE macros shouldn't be used outside of the minimal set
   of code that has to know what a cons cell looks like.  Other code not
   part of the basic lisp implementation should assume that the car and cdr
   fields are not accessible as lvalues.  (What if we want to switch to
   a copying collector someday?  Cached cons cell field addresses may be
   invalidated at arbitrary points.)  */
#define XCAR_AS_LVALUE(c) (XCONS ((c))->car)
655
#define XCDR_AS_LVALUE(c) (XCONS ((c))->u.cdr)
656

657
/* Use these from normal code.  */
Juanma Barranquero's avatar
Juanma Barranquero committed
658 659
#define XCAR(c)	LISP_MAKE_RVALUE (XCAR_AS_LVALUE (c))
#define XCDR(c) LISP_MAKE_RVALUE (XCDR_AS_LVALUE (c))
660 661 662 663 664 665 666

/* Use these to set the fields of a cons cell.

   Note that both arguments may refer to the same object, so 'n'
   should not be read after 'c' is first modified.  Also, neither
   argument should be evaluated more than once; side effects are
   especially common in the second argument.  */
Juanma Barranquero's avatar
Juanma Barranquero committed
667 668
#define XSETCAR(c,n) (XCAR_AS_LVALUE (c) = (n))
#define XSETCDR(c,n) (XCDR_AS_LVALUE (c) = (n))
669

670 671 672 673
/* Take the car or cdr of something whose type is not known.  */
#define CAR(c)					\
 (CONSP ((c)) ? XCAR ((c))			\
  : NILP ((c)) ? Qnil				\
674
  : wrong_type_argument (Qlistp, (c)))
675 676 677 678

#define CDR(c)					\
 (CONSP ((c)) ? XCDR ((c))			\
  : NILP ((c)) ? Qnil				\
679
  : wrong_type_argument (Qlistp, (c)))
680

681 682 683 684 685 686 687
/* Take the car or cdr of something whose type is not known.  */
#define CAR_SAFE(c)				\
  (CONSP ((c)) ? XCAR ((c)) : Qnil)

#define CDR_SAFE(c)				\
  (CONSP ((c)) ? XCDR ((c)) : Qnil)

Kenichi Handa's avatar
Kenichi Handa committed
688 689 690 691 692
/* Nonzero if STR is a multibyte string.  */
#define STRING_MULTIBYTE(STR)  \
  (XSTRING (STR)->size_byte >= 0)

/* Return the length in bytes of STR.  */
693 694 695 696

#ifdef GC_CHECK_STRING_BYTES

struct Lisp_String;
697
extern ptrdiff_t string_bytes (struct Lisp_String *);
698 699 700 701
#define STRING_BYTES(S) string_bytes ((S))

#else /* not GC_CHECK_STRING_BYTES */

Kenichi Handa's avatar
Kenichi Handa committed
702 703 704
#define STRING_BYTES(STR)  \
  ((STR)->size_byte < 0 ? (STR)->size : (STR)->size_byte)

705 706
#endif /* not GC_CHECK_STRING_BYTES */

707 708 709 710 711 712 713 714 715
/* An upper bound on the number of bytes in a Lisp string, not
   counting the terminating null.  This a tight enough bound to
   prevent integer overflow errors that would otherwise occur during
   string size calculations.  A string cannot contain more bytes than
   a fixnum can represent, nor can it be so long that C pointer
   arithmetic stops working on the string plus its terminating null.
   Although the actual size limit (see STRING_BYTES_MAX in alloc.c)
   may be a bit smaller than STRING_BYTES_BOUND, calculating it here
   would expose alloc.c internal details that we'd rather keep
716 717 718 719 720 721
   private.

   This is a macros for use in static initializers, and a constant for
   visibility to GDB.  The cast to ptrdiff_t ensures that
   STRING_BYTES_BOUND is signed.  */
static ptrdiff_t const STRING_BYTES_BOUND =
722 723
#define STRING_BYTES_BOUND  \
  min (MOST_POSITIVE_FIXNUM, (ptrdiff_t) min (SIZE_MAX, PTRDIFF_MAX) - 1)
724
	STRING_BYTES_BOUND;
725

726
/* Mark STR as a unibyte string.  */
727 728 729 730
#define STRING_SET_UNIBYTE(STR)  \
  do { if (EQ (STR, empty_multibyte_string))  \
      (STR) = empty_unibyte_string;  \
    else XSTRING (STR)->size_byte = -1; } while (0)
731

732 733 734 735 736 737 738
/* Mark STR as a multibyte string.  Assure that STR contains only
   ASCII characters in advance.  */
#define STRING_SET_MULTIBYTE(STR)  \
  do { if (EQ (STR, empty_unibyte_string))  \
      (STR) = empty_multibyte_string;  \
    else XSTRING (STR)->size_byte = XSTRING (STR)->size; } while (0)

739
/* Get text properties.  */
740 741 742 743
#define STRING_INTERVALS(STR)  (XSTRING (STR)->intervals + 0)

/* Set text properties.  */
#define STRING_SET_INTERVALS(STR, INT) (XSTRING (STR)->intervals = (INT))
Kenichi Handa's avatar
Kenichi Handa committed
744

745
/* In a string or vector, the sign bit of the `size' is the gc mark bit.  */
Jim Blandy's avatar
Jim Blandy committed
746 747 748

struct Lisp_String
  {
749 750
    ptrdiff_t size;
    ptrdiff_t size_byte;
751
    INTERVAL intervals;		/* Text properties in this string.  */
752
    unsigned char *data;
Jim Blandy's avatar
Jim Blandy committed
753 754
  };

755 756 757 758 759 760 761
/* Header of vector-like objects.  This documents the layout constraints on
   vectors and pseudovectors other than struct Lisp_Subr.  It also prevents
   compilers from being fooled by Emacs's type punning: the XSETPSEUDOVECTOR
   and PSEUDOVECTORP macros cast their pointers to struct vectorlike_header *,
   because when two such pointers potentially alias, a compiler won't
   incorrectly reorder loads and stores to their size fields.  See
   <http://debbugs.gnu.org/cgi/bugreport.cgi?bug=8546>.  */
762
struct vectorlike_header
Jim Blandy's avatar
Jim Blandy committed
763
  {
764 765 766 767 768 769 770 771 772 773 774 775 776 777
    /* This field contains various pieces of information:
       - 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
         a "pvec type" tag held in PVEC_TYPE_MASK and a size held in the lowest
         PSEUDOVECTOR_SIZE_BITS.  That size normally indicates the number of
         Lisp_Object slots at the beginning of the object that need to be
         traced by the GC, tho some types use it slightly differently.
       - E.g. if the pvec type is PVEC_FREE it means this is an unallocated
         vector on a free-list and PSEUDOVECTOR_SIZE_BITS indicates its size
         in bytes.  */
778
    ptrdiff_t size;
779

780 781 782
    /* When the vector is allocated from a vector block, NBYTES is used
       if the vector is not on a free list, and VECTOR is used otherwise.
       For large vector-like objects, BUFFER or VECTOR is used as a pointer
783
       to the next vector-like object.  It is generally a buffer or a
784 785 786
       Lisp_Vector alias, so for convenience it is a union instead of a
       pointer: this way, one can write P->next.vector instead of ((struct
       Lisp_Vector *) P->next).  */
787
    union {
788 789 790 791 792
      /* This is only needed for small vectors that are not free because the
	 `size' field only gives us the number of Lisp_Object slots, whereas we
	 need to know the total size, including non-Lisp_Object data.
	 FIXME: figure out a way to store this info elsewhere so we can
	 finally get rid of this extra word of overhead.  */
793
      ptrdiff_t nbytes;
794
      struct buffer *buffer;
795 796 797 798
      /* FIXME: This can be removed: For large vectors, this field could be
	 placed *before* the vector itself.  And for small vectors on a free
	 list, this field could be stored in the vector's bytes, since the
	 empty vector is handled specially anyway.  */
799 800 801 802 803 804
      struct Lisp_Vector *vector;
    } next;
  };

struct Lisp_Vector
  {
805
    struct vectorlike_header header;
Jim Blandy's avatar
Jim Blandy committed
806 807 808
    Lisp_Object contents[1];
  };

Karl Heuer's avatar
Karl Heuer committed
809
/* If a struct is made to look like a vector, this macro returns the length
810
   of the shortest vector that would hold that struct.  */
Andreas Schwab's avatar
Andreas Schwab committed
811
#define VECSIZE(type) ((sizeof (type)					  \
812
			- offsetof (struct Lisp_Vector, contents[0])      \
813
                        + sizeof (Lisp_Object) - 1) /* Round up.  */	  \
Karl Heuer's avatar
Karl Heuer committed
814 815
		       / sizeof (Lisp_Object))

816 817 818 819
/* Like VECSIZE, but used when the pseudo-vector has non-Lisp_Object fields
   at the end and we need to compute the number of Lisp_Object fields (the
   ones that the GC needs to trace).  */
#define PSEUDOVECSIZE(type, nonlispfield) \
Juanma Barranquero's avatar
Juanma Barranquero committed
820
  ((offsetof (type, nonlispfield) - offsetof (struct Lisp_Vector, contents[0])) \
821 822
   / sizeof (Lisp_Object))

823
/* A char-table is a kind of vectorlike, with contents are like a
824
   vector but with a few other slots.  For some purposes, it makes
825
   sense to handle a char-table with type struct Lisp_Vector.  An
826 827
   element of a char table can be any Lisp objects, but if it is a sub
   char-table, we treat it a table that contains information of a
828 829 830 831
   specific range of characters.  A sub char-table has the same
   structure as a vector.  A sub char table appears only in an element
   of a char-table, and there's no way to access it directly from
   Emacs Lisp program.  */
832

833 834 835 836 837 838 839 840 841 842 843 844 845
#ifdef __GNUC__

#define CHAR_TABLE_REF_ASCII(CT, IDX)					\
  ({struct Lisp_Char_Table *_tbl = NULL;				\
    Lisp_Object _val;							\
    do {								\
      _tbl = _tbl ? XCHAR_TABLE (_tbl->parent) : XCHAR_TABLE (CT);	\
      _val = (! SUB_CHAR_TABLE_P (_tbl->ascii) ? _tbl->ascii		\
	      : XSUB_CHAR_TABLE (_tbl->ascii)->contents[IDX]);		\
      if (NILP (_val))							\
	_val = _tbl->defalt;						\
    } while (NILP (_val) && ! NILP (_tbl->parent));			\
    _val; })
846

847 848 849 850 851 852 853 854 855 856 857 858 859
#else  /* not __GNUC__ */

#define CHAR_TABLE_REF_ASCII(CT, IDX)					  \
  (! NILP (XCHAR_TABLE (CT)->ascii)					  \
   ? (! SUB_CHAR_TABLE_P (XCHAR_TABLE (CT)->ascii)			  \
      ? XCHAR_TABLE (CT)->ascii						  \
      : ! NILP (XSUB_CHAR_TABLE (XCHAR_TABLE (CT)->ascii)->contents[IDX]) \
      ? XSUB_CHAR_TABLE (XCHAR_TABLE (CT)->ascii)->contents[IDX]	  \
      : char_table_ref ((CT), (IDX)))					  \
   :  char_table_ref ((CT), (IDX)))

#endif	/* not __GNUC__ */

860 861
/* Compute A OP B, using the unsigned comparison operator OP.  A and B
   should be integer expressions.  This is not the same as
Paul Eggert's avatar
Paul Eggert committed
862
   mathematical comparison; for example, UNSIGNED_CMP (0, <, -1)
863 864 865 866 867 868 869
   returns 1.  For efficiency, prefer plain unsigned comparison if A
   and B's sizes both fit (after integer promotion).  */
#define UNSIGNED_CMP(a, op, b)						\
  (max (sizeof ((a) + 0), sizeof ((b) + 0)) <= sizeof (unsigned)	\
   ? ((a) + (unsigned) 0) op ((b) + (unsigned) 0)			\
   : ((a) + (uintmax_t) 0) op ((b) + (uintmax_t) 0))

870
/* Nonzero iff C is an ASCII character.  */
871
#define ASCII_CHAR_P(c) UNSIGNED_CMP (c, <, 0x80)
872

873
/* Almost equivalent to Faref (CT, IDX) with optimization for ASCII
874
   characters.  Do not check validity of CT.  */
875 876
#define CHAR_TABLE_REF(CT, IDX)					\
  (ASCII_CHAR_P (IDX) ? CHAR_TABLE_REF_ASCII ((CT), (IDX))	\
877
   : char_table_ref ((CT), (IDX)))
878

Kenichi Handa's avatar
Kenichi Handa committed
879 880
/* Almost equivalent to Faref (CT, IDX).  However, if the result is
   not a character, return IDX.
881 882 883

   For these characters, do not check validity of CT
   and do not follow parent.  */
884 885
#define CHAR_TABLE_TRANSLATE(CT, IDX)	\
  char_table_translate (CT, IDX)
886

887
/* Equivalent to Faset (CT, IDX, VAL) with optimization for ASCII and
Dave Love's avatar
Dave Love committed
888
   8-bit European characters.  Do not check validity of CT.  */
889
#define CHAR_TABLE_SET(CT, IDX, VAL)					\
890
  (ASCII_CHAR_P (IDX) && SUB_CHAR_TABLE_P (XCHAR_TABLE (CT)->ascii)	\
891 892 893
   ? XSUB_CHAR_TABLE (XCHAR_TABLE (CT)->ascii)->contents[IDX] = VAL	\
   : char_table_set (CT, IDX, VAL))

894 895 896 897 898 899 900
enum CHARTAB_SIZE_BITS
  {
    CHARTAB_SIZE_BITS_0 = 6,
    CHARTAB_SIZE_BITS_1 = 4,
    CHARTAB_SIZE_BITS_2 = 5,
    CHARTAB_SIZE_BITS_3 = 7
  };
901 902 903 904

extern const int chartab_size[4];

struct Lisp_Sub_Char_Table;
905

906 907
struct Lisp_Char_Table
  {
908
    /* HEADER.SIZE is the vector's size field, which also holds the
909
       pseudovector type information.  It holds the size, too.
Miles Bader's avatar
Miles Bader committed
910 911
       The size counts the defalt, parent, purpose, ascii,
       contents, and extras slots.  */
912
    struct vectorlike_header header;
913

914 915 916
    /* This holds a default value,
       which is used whenever the value for a specific character is nil.  */
    Lisp_Object defalt;
917

Kenichi Handa's avatar
Kenichi Handa committed
918 919 920
    /* This points to another char table, which we inherit from when the
       value for a specific character is nil.  The `defalt' slot takes
       precedence over this.  */
921
    Lisp_Object parent;
922

Kenichi Handa's avatar
Kenichi Handa committed
923 924
    /* This is a symbol which says what kind of use this char-table is
       meant for.  */
925
    Lisp_Object purpose;
Kenichi Handa's avatar