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

3 4
Copyright (C) 1985-1987, 1993-1995, 1997-2013 Free Software Foundation,
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 <stdbool.h>
28
#include <stddef.h>
29
#include <float.h>
30
#include <inttypes.h>
31
#include <limits.h>
32

33 34
#include <intprops.h>

35 36 37 38 39
INLINE_HEADER_BEGIN
#ifndef LISP_INLINE
# define LISP_INLINE INLINE
#endif

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

/* 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)
  };
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/* 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

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/* 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

111
/* Extra internal type checking?  */
112

113 114 115 116 117 118
/* 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 */
119

120
extern _Noreturn void die (const char *, const char *, int);
121

122 123 124 125 126
/* 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
127
   eassert macro altogether, e.g., if XSTRING (x) uses eassert to test
128 129
   STRINGP (x), but a particular use of XSTRING is invoked only after
   testing that STRINGP (x) is true, making the test redundant.  */
130
extern bool suppress_checking EXTERNALLY_VISIBLE;
131

132 133 134 135
# define eassert(cond)						\
   ((cond) || suppress_checking					\
    ? (void) 0							\
    : die ("assertion failed: " # cond, __FILE__, __LINE__))
136
#endif /* ENABLE_CHECKING */
137

138 139 140
/* 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
141

142
/***** Select the tagging scheme.  *****/
143
/* The following option controls the tagging scheme:
144 145 146 147
   - 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.

148 149
   If ! USE_LSB_TAG, then use the top 3 bits for tagging, thus
   restricting our possible address range.
150 151 152 153 154 155

   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
156 157 158 159 160
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 =
161
#define GCTYPEBITS 3
Paul Eggert's avatar
Paul Eggert committed
162 163
	GCTYPEBITS,

164 165
    /* 2**GCTYPEBITS.  This must be a macro that expands to a literal
       integer constant, for MSVC.  */
166 167
#define GCALIGNMENT 8

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

Paul Eggert's avatar
Paul Eggert committed
171 172 173 174 175 176
    /* 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
177

178 179 180 181
#if GCALIGNMENT != 1 << GCTYPEBITS
# error "GCALIGNMENT and GCTYPEBITS are inconsistent"
#endif

Paul Eggert's avatar
Paul Eggert committed
182 183 184 185
/* 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))
186

187 188 189 190 191 192
/* 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.  */
193
#  ifdef alignas
194 195
/* 3.  Pointers-as-ints exceed VAL_MAX.
   On hosts where pointers-as-ints do not exceed VAL_MAX, USE_LSB_TAG is:
196 197
    a. unnecessary, because the top bits of an EMACS_INT are unused, and
    b. slower, because it typically requires extra masking.
198 199 200 201
   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
202
#  endif
203 204
# endif
#endif
Paul Eggert's avatar
Paul Eggert committed
205 206
#ifdef USE_LSB_TAG
# undef USE_LSB_TAG
Paul Eggert's avatar
Paul Eggert committed
207
enum enum_USE_LSB_TAG { USE_LSB_TAG = 1 };
Paul Eggert's avatar
Paul Eggert committed
208 209
# define USE_LSB_TAG 1
#else
Paul Eggert's avatar
Paul Eggert committed
210
enum enum_USE_LSB_TAG { USE_LSB_TAG = 0 };
211 212
# define USE_LSB_TAG 0
#endif
213

214 215
#ifndef alignas
# define alignas(alignment) /* empty */
216
# if USE_LSB_TAG
217
#  error "USE_LSB_TAG requires alignas"
218 219 220
# endif
#endif

221

222
/* Define the fundamental Lisp data structures.  */
Jim Blandy's avatar
Jim Blandy committed
223

224 225 226
/* 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
227

228 229
/* 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.  */
230
static EMACS_INT const INTMASK = EMACS_INT_MAX >> (INTTYPEBITS - 1);
231
#define case_Lisp_Int case Lisp_Int0: case Lisp_Int1
232
#define LISP_INT_TAG_P(x) (((x) & ~Lisp_Int1) == 0)
233

234 235 236 237 238 239 240 241 242
/* 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
243 244
enum Lisp_Type
  {
245
    /* Integer.  XINT (obj) is the integer value.  */
246
    Lisp_Int0 = 0,
247
    Lisp_Int1 = USE_LSB_TAG ? 1 << INTTYPEBITS : 1,
Jim Blandy's avatar
Jim Blandy committed
248

249
    /* Symbol.  XSYMBOL (object) points to a struct Lisp_Symbol.  */
250
    Lisp_Symbol = 2,
Jim Blandy's avatar
Jim Blandy committed
251

252 253
    /* Miscellaneous.  XMISC (object) points to a union Lisp_Misc,
       whose first member indicates the subtype.  */
254
    Lisp_Misc = 3,
Jim Blandy's avatar
Jim Blandy committed
255 256

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

260
    /* Vector of Lisp objects, or something resembling it.
261
       XVECTOR (object) points to a struct Lisp_Vector, which contains
262 263
       the size and contents.  The size field also contains the type
       information, if it's not a real vector object.  */
264
    Lisp_Vectorlike = 5,
Jim Blandy's avatar
Jim Blandy committed
265

266
    /* Cons.  XCONS (object) points to a struct Lisp_Cons.  */
267
    Lisp_Cons = 6,
268

269
    Lisp_Float = 7,
Jim Blandy's avatar
Jim Blandy committed
270 271
  };

272
/* This is the set of data types that share a common structure.
273 274 275 276
   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
277 278
enum Lisp_Misc_Type
  {
279
    Lisp_Misc_Free = 0x5eab,
280
    Lisp_Misc_Marker,
281
    Lisp_Misc_Overlay,
282
    Lisp_Misc_Save_Value,
283 284 285 286 287
    /* 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
288 289
  };

290 291 292 293 294 295 296 297 298 299 300 301
/* 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.  */
  };

302 303 304 305 306 307 308 309
/* 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
310
   subtype; and PVEC_OTHER, a kind of vectorlike object.  The former
311 312 313 314 315 316 317 318 319 320 321 322 323
   is suitable for temporarily stashing away pointers and integers in
   a Lisp object (see the existing uses of make_save_value and
   XSAVE_VALUE).  The latter is useful for vector-like Lisp objects
   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
324
   while Lisp_Misc types are for everything else.  A pseudovector object
325 326 327 328
   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.

329 330 331 332
   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.

333 334 335 336 337 338 339 340 341
   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.

342 343 344 345
   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).

346 347 348 349 350 351 352 353 354 355 356
   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.  */

357
#ifdef CHECK_LISP_OBJECT_TYPE
Jim Blandy's avatar
Jim Blandy committed
358

359 360 361
typedef struct { EMACS_INT i; } Lisp_Object;

#define XLI(o) (o).i
362
LISP_INLINE Lisp_Object
363 364 365 366 367
XIL (EMACS_INT i)
{
  Lisp_Object o = { i };
  return o;
}
Jim Blandy's avatar
Jim Blandy committed
368

369
LISP_INLINE Lisp_Object
370 371 372 373
LISP_MAKE_RVALUE (Lisp_Object o)
{
    return o;
}
374 375

#define LISP_INITIALLY_ZERO {0}
376

Paul Eggert's avatar
Paul Eggert committed
377 378
#undef CHECK_LISP_OBJECT_TYPE
enum CHECK_LISP_OBJECT_TYPE { CHECK_LISP_OBJECT_TYPE = 1 };
379
#else /* CHECK_LISP_OBJECT_TYPE */
Jim Blandy's avatar
Jim Blandy committed
380

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

383
typedef EMACS_INT Lisp_Object;
384 385
#define XLI(o) (o)
#define XIL(i) (i)
386
#define LISP_MAKE_RVALUE(o) (0 + (o))
387
#define LISP_INITIALLY_ZERO 0
Paul Eggert's avatar
Paul Eggert committed
388
enum CHECK_LISP_OBJECT_TYPE { CHECK_LISP_OBJECT_TYPE = 0 };
389
#endif /* CHECK_LISP_OBJECT_TYPE */
Jim Blandy's avatar
Jim Blandy committed
390

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

393 394 395
static ptrdiff_t const ARRAY_MARK_FLAG
#define ARRAY_MARK_FLAG PTRDIFF_MIN
      = ARRAY_MARK_FLAG;
396

397 398
/* In the size word of a struct Lisp_Vector, this bit means it's really
   some other vector-like object.  */
399 400 401
static ptrdiff_t const PSEUDOVECTOR_FLAG
#define PSEUDOVECTOR_FLAG (PTRDIFF_MAX - PTRDIFF_MAX / 2)
      = PSEUDOVECTOR_FLAG;
402

403
/* In a pseudovector, the size field actually contains a word with one
404 405
   PSEUDOVECTOR_FLAG bit set, and one of the following values extracted
   with PVEC_TYPE_MASK to indicate the actual type.  */
406 407
enum pvec_type
{
408
  PVEC_NORMAL_VECTOR,
409 410 411 412 413 414 415 416 417 418 419
  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,
420 421 422 423 424
  /* 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.  */
425
};
426

Paul Eggert's avatar
Paul Eggert committed
427 428 429 430 431 432 433 434 435
/* 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
436
  {
Paul Eggert's avatar
Paul Eggert committed
437 438 439 440 441 442 443
    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.  */
444
    PSEUDOVECTOR_SIZE_BITS = 12,
445
    PSEUDOVECTOR_SIZE_MASK = (1 << PSEUDOVECTOR_SIZE_BITS) - 1,
446 447 448 449

    /* 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,
450
    PSEUDOVECTOR_REST_MASK = (((1 << PSEUDOVECTOR_REST_BITS) - 1)
451 452 453 454 455
			      << PSEUDOVECTOR_SIZE_BITS),

    /* Used to extract pseudovector subtype information.  */
    PSEUDOVECTOR_AREA_BITS = PSEUDOVECTOR_SIZE_BITS + PSEUDOVECTOR_REST_BITS,
    PVEC_TYPE_MASK = 0x3f << PSEUDOVECTOR_AREA_BITS,
456

Paul Eggert's avatar
Paul Eggert committed
457 458 459 460
    /* 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
461 462 463

/* These macros extract various sorts of values from a Lisp_Object.
 For example, if tem is a Lisp_Object whose type is Lisp_Cons,
464
 XCONS (tem) is the struct Lisp_Cons * pointing to the memory for that cons.  */
Jim Blandy's avatar
Jim Blandy committed
465

466
#if USE_LSB_TAG
Kenichi Handa's avatar
Kenichi Handa committed
467

468 469 470 471 472
enum lsb_bits
  {
    TYPEMASK = (1 << GCTYPEBITS) - 1,
    VALMASK = ~ TYPEMASK
  };
473 474 475 476
#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)
477
#define make_lisp_ptr(ptr, type) \
478
  (eassert (XTYPE (XIL ((intptr_t) (ptr))) == 0), /* Check alignment.  */  \
479
   XIL ((type) | (intptr_t) (ptr)))
Kenichi Handa's avatar
Kenichi Handa committed
480

481 482
#define XPNTR(a) ((intptr_t) (XLI (a) & ~TYPEMASK))
#define XUNTAG(a, type) ((intptr_t) (XLI (a) - (type)))
Kenichi Handa's avatar
Kenichi Handa committed
483 484 485

#else  /* not USE_LSB_TAG */

486 487 488
static EMACS_INT const VALMASK
#define VALMASK VAL_MAX
      = VALMASK;
Kenichi Handa's avatar
Kenichi Handa committed
489

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

Karl Heuer's avatar
Karl Heuer committed
492 493
/* For integers known to be positive, XFASTINT provides fast retrieval
   and XSETFASTINT provides fast storage.  This takes advantage of the
494 495 496
   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
497

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

500 501 502
#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
503

504 505 506
#define make_lisp_ptr(ptr, type) \
  (XIL ((EMACS_INT) ((EMACS_UINT) (type) << VALBITS)  \
	+ ((intptr_t) (ptr) & VALMASK)))
507

508
#if DATA_SEG_BITS
509
/* DATA_SEG_BITS forces extra bits to be or'd in with any pointers
510
   which were stored in a Lisp_Object.  */
511
#define XPNTR(a) ((uintptr_t) ((XLI (a) & VALMASK)) | DATA_SEG_BITS))
512
#else
513
#define XPNTR(a) ((uintptr_t) (XLI (a) & VALMASK))
514
#endif
515

Kenichi Handa's avatar
Kenichi Handa committed
516
#endif /* not USE_LSB_TAG */
Jim Blandy's avatar
Jim Blandy committed
517

518 519 520 521 522
/* Return a (Lisp-integer sized) hash of the Lisp_Object value.  Happens to be
   like XUINT right now, but XUINT should only be applied to objects we know
   are integers.  */
#define XHASH(a) XUINT (a)

523 524 525 526 527 528 529 530
/* 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

531 532 533 534 535 536 537
/* 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

538
#define EQ(x, y) (XLI (x) == XLI (y))
539

540
/* Largest and smallest representable fixnum values.  These are the C
541
   values.  They are macros for use in static initializers.  */
542
#define MOST_POSITIVE_FIXNUM (EMACS_INT_MAX >> INTTYPEBITS)
543 544
#define MOST_NEGATIVE_FIXNUM (-1 - MOST_POSITIVE_FIXNUM)

545 546
/* 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
547
   type or if I is a NaN.  */
548 549

#define FIXNUM_OVERFLOW_P(i) \
550
  (! ((0 <= (i) || MOST_NEGATIVE_FIXNUM <= (i)) && (i) <= MOST_POSITIVE_FIXNUM))
551

552
LISP_INLINE ptrdiff_t
553 554 555 556 557
clip_to_bounds (ptrdiff_t lower, EMACS_INT num, ptrdiff_t upper)
{
  return num < lower ? lower : num <= upper ? num : upper;
}

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

560 561 562 563 564 565 566 567 568 569
#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))
570 571

/* Misc types.  */
572

573
#define XMISC(a)	((union Lisp_Misc *) XUNTAG (a, Lisp_Misc))
Juanma Barranquero's avatar
Juanma Barranquero committed
574
#define XMISCANY(a)	(eassert (MISCP (a)), &(XMISC (a)->u_any))
575
#define XMISCTYPE(a)   (XMISCANY (a)->type)
Juanma Barranquero's avatar
Juanma Barranquero committed
576 577 578
#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))
579 580 581 582 583 584 585

/* 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))
586
#define XBUFFER_OBJFWD(a) \
587
  (eassert (BUFFER_OBJFWDP (a)), &((a)->u_buffer_objfwd))
588
#define XKBOARD_OBJFWD(a) \
589
  (eassert (KBOARD_OBJFWDP (a)), &((a)->u_kboard_objfwd))
Jim Blandy's avatar
Jim Blandy committed
590

591
/* Pseudovector types.  */
592

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
#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)))
611 612

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

Paul Eggert's avatar
Paul Eggert committed
614 615 616 617 618 619
#define XSETINT(a, b) ((a) = make_number (b))
#define XSETCONS(a, b) ((a) = make_lisp_ptr (b, Lisp_Cons))
#define XSETVECTOR(a, b) ((a) = make_lisp_ptr (b, Lisp_Vectorlike))
#define XSETSTRING(a, b) ((a) = make_lisp_ptr (b, Lisp_String))
#define XSETSYMBOL(a, b) ((a) = make_lisp_ptr (b, Lisp_Symbol))
#define XSETFLOAT(a, b) ((a) = make_lisp_ptr (b, Lisp_Float))
620 621

/* Misc types.  */
622

Paul Eggert's avatar
Paul Eggert committed
623
#define XSETMISC(a, b) ((a) = make_lisp_ptr (b, Lisp_Misc))
Richard M. Stallman's avatar
Richard M. Stallman committed
624
#define XSETMARKER(a, b) (XSETMISC (a, b), XMISCTYPE (a) = Lisp_Misc_Marker)
625 626

/* Pseudovector types.  */
627

628 629 630
#define XSETPVECTYPE(v, code)						\
  ((v)->header.size |= PSEUDOVECTOR_FLAG | ((code) << PSEUDOVECTOR_AREA_BITS))
#define XSETPVECTYPESIZE(v, code, lispsize, restsize)		\
631
  ((v)->header.size = (PSEUDOVECTOR_FLAG			\
632 633 634
		       | ((code) << PSEUDOVECTOR_AREA_BITS)	\
		       | ((restsize) << PSEUDOVECTOR_SIZE_BITS) \
		       | (lispsize)))
635 636

/* The cast to struct vectorlike_header * avoids aliasing issues.  */
637
#define XSETPSEUDOVECTOR(a, b, code) \
638 639 640 641 642
  XSETTYPED_PSEUDOVECTOR (a, b,					\
			  (((struct vectorlike_header *)	\
			    XUNTAG (a, Lisp_Vectorlike))	\
			   ->size),				\
			  code)
643
#define XSETTYPED_PSEUDOVECTOR(a, b, size, code)			\
644
  (XSETVECTOR (a, b),							\
645
   eassert ((size & (PSEUDOVECTOR_FLAG | PVEC_TYPE_MASK))		\
646
	    == (PSEUDOVECTOR_FLAG | (code << PSEUDOVECTOR_AREA_BITS))))
647

648 649 650 651
#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))
652
#define XSETTERMINAL(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_TERMINAL))
653
#define XSETSUBR(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_SUBR))
654
#define XSETCOMPILED(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_COMPILED))
655
#define XSETBUFFER(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_BUFFER))
656 657
#define XSETCHAR_TABLE(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_CHAR_TABLE))
#define XSETBOOL_VECTOR(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_BOOL_VECTOR))
658
#define XSETSUB_CHAR_TABLE(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_SUB_CHAR_TABLE))
659 660 661

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

662
#define AREF(ARRAY, IDX)	XVECTOR ((ARRAY))->contents[IDX]
663
#define ASIZE(ARRAY)		XVECTOR ((ARRAY))->header.size
Stefan Monnier's avatar
Stefan Monnier committed
664
#define ASET(ARRAY, IDX, VAL)	\
665
  (eassert (0 <= (IDX) && (IDX) < ASIZE (ARRAY)),	\
666
   XVECTOR (ARRAY)->contents[IDX] = (VAL))
667

668 669
/* Convenience macros for dealing with Lisp strings.  */

Ken Raeburn's avatar
Ken Raeburn committed
670
#define SDATA(string)		(XSTRING (string)->data + 0)
671 672
#define SREF(string, index)	(SDATA (string)[index] + 0)
#define SSET(string, index, new) (SDATA (string)[index] = (new))
673 674 675
#define SCHARS(string)		(XSTRING (string)->size + 0)
#define SBYTES(string)		(STRING_BYTES (XSTRING (string)) + 0)

676 677 678
/* Avoid "differ in sign" warnings.  */
#define SSDATA(x)  ((char *) SDATA (x))

679 680
#define STRING_SET_CHARS(string, newsize) \
    (XSTRING (string)->size = (newsize))
681

Ken Raeburn's avatar
Ken Raeburn committed
682
#define STRING_COPYIN(string, index, new, count) \
683
    memcpy (SDATA (string) + index, new, count)
Ken Raeburn's avatar
Ken Raeburn committed
684

685 686 687 688 689
/* Type checking.  */

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

690
/* Deprecated and will be removed soon.  */
691 692

#define INTERNAL_FIELD(field) field ## _
693

694
/* See the macros in intervals.h.  */
695 696 697

typedef struct interval *INTERVAL;

698
/* Complain if object is not string or buffer type.  */
699
#define CHECK_STRING_OR_BUFFER(x) \
700 701
  CHECK_TYPE (STRINGP (x) || BUFFERP (x), Qbuffer_or_string_p, x)

Jim Blandy's avatar
Jim Blandy committed
702 703
struct Lisp_Cons
  {
704
    /* Car of this cons cell.  */
705
    Lisp_Object car;
706

707 708
    union
    {
709
      /* Cdr of this cons cell.  */
710
      Lisp_Object cdr;
711 712

      /* Used to chain conses on a free list.  */
713 714
      struct Lisp_Cons *chain;
    } u;
Jim Blandy's avatar
Jim Blandy committed
715 716
  };

717
/* Take the car or cdr of something known to be a cons cell.  */
718 719 720 721 722 723
/* 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.)  */
724 725
#define XCAR_AS_LVALUE(c) (XCONS (c)->car)
#define XCDR_AS_LVALUE(c) (XCONS (c)->u.cdr)
726

727
/* Use these from normal code.  */
Juanma Barranquero's avatar
Juanma Barranquero committed
728 729
#define XCAR(c)	LISP_MAKE_RVALUE (XCAR_AS_LVALUE (c))
#define XCDR(c) LISP_MAKE_RVALUE (XCDR_AS_LVALUE (c))
730 731 732 733 734 735 736

/* 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
737 738
#define XSETCAR(c,n) (XCAR_AS_LVALUE (c) = (n))
#define XSETCDR(c,n) (XCDR_AS_LVALUE (c) = (n))
739

740 741 742 743
/* Take the car or cdr of something whose type is not known.  */
#define CAR(c)					\
 (CONSP ((c)) ? XCAR ((c))			\
  : NILP ((c)) ? Qnil				\
744
  : wrong_type_argument (Qlistp, (c)))
745 746 747 748

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

751 752 753 754 755 756 757
/* 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)

758
/* True if STR is a multibyte string.  */
Kenichi Handa's avatar
Kenichi Handa committed
759 760 761 762
#define STRING_MULTIBYTE(STR)  \
  (XSTRING (STR)->size_byte >= 0)

/* Return the length in bytes of STR.  */
763 764 765 766

#ifdef GC_CHECK_STRING_BYTES

struct Lisp_String;
767
extern ptrdiff_t string_bytes (struct Lisp_String *);
768 769 770 771
#define STRING_BYTES(S) string_bytes ((S))

#else /* not GC_CHECK_STRING_BYTES */

Kenichi Handa's avatar
Kenichi Handa committed
772 773 774
#define STRING_BYTES(STR)  \
  ((STR)->size_byte < 0 ? (STR)->size : (STR)->size_byte)

775 776
#endif /* not GC_CHECK_STRING_BYTES */

777 778 779 780 781 782 783 784 785
/* 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
786 787
   private.

788
   This is a macro for use in static initializers, and a constant for
789
   visibility to GDB.  The cast to ptrdiff_t ensures that
790
   the macro is signed.  */
791
static ptrdiff_t const STRING_BYTES_BOUND =
792
#define STRING_BYTES_BOUND  \
793
  ((ptrdiff_t) min (MOST_POSITIVE_FIXNUM, min (SIZE_MAX, PTRDIFF_MAX) - 1))
794
	STRING_BYTES_BOUND;
795

796
/* Mark STR as a unibyte string.  */
797 798 799 800
#define STRING_SET_UNIBYTE(STR)  \
  do { if (EQ (STR, empty_multibyte_string))  \
      (STR) = empty_unibyte_string;  \
    else XSTRING (STR)->size_byte = -1; } while (0)
801

802 803 804 805 806 807 808
/* 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)

809
/* In a string or vector, the sign bit of the `size' is the gc mark bit.  */
Jim Blandy's avatar
Jim Blandy committed
810 811 812

struct Lisp_String
  {
813 814
    ptrdiff_t size;
    ptrdiff_t size_byte;
815
    INTERVAL intervals;		/* Text properties in this string.  */
816
    unsigned char *data;
Jim Blandy's avatar
Jim Blandy committed
817 818
  };

819
/* Header of vector-like objects.  This documents the layout constraints on
820
   vectors and pseudovectors (objects of PVEC_xxx subtype).  It also prevents
821 822 823 824 825
   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>.  */
826
struct vectorlike_header
Jim Blandy's avatar
Jim Blandy committed
827
  {
828
    /* The only field contains various pieces of information:
829 830 831 832 833
       - 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.
834 835 836 837 838 839 840 841 842 843 844 845 846
       - 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.  */
847
    ptrdiff_t size;
848 849
  };

850 851
/* Regular vector is just a header plus array of Lisp_Objects.  */

852 853
struct Lisp_Vector
  {
854
    struct vectorlike_header header;
Jim Blandy's avatar
Jim Blandy committed
855 856 857
    Lisp_Object contents[1];
  };

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
/* A boolvector is a kind of vectorlike, with contents are like a string.  */

struct Lisp_Bool_Vector
  {
    /* HEADER.SIZE is the vector's size field.  It doesn't have the real size,
       just the subtype information.  */
    struct vectorlike_header header;
    /* This is the size in bits.  */
    EMACS_INT size;
    /* This contains the actual bits, packed into bytes.  */
    unsigned char data[1];
  };

/* Some handy constants for calculating sizes
   and offsets, mostly of vectorlike objects.   */

enum
  {
    header_size = offsetof (struct Lisp_Vector, contents),
    bool_header_size = offsetof (struct Lisp_Bool_Vector, data),
    word_size = sizeof (Lisp_Object)
  };

Karl Heuer's avatar
Karl Heuer committed
881
/* If a struct is made to look like a vector, this macro returns the length
882
   of the shortest vector that would hold that struct.  */
883 884 885

#define VECSIZE(type)						\
  ((sizeof (type) - header_size + word_size - 1) / word_size)
Karl Heuer's avatar
Karl Heuer committed
886

887 888 889
/* 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).  */
890 891 892

#define PSEUDOVECSIZE(type, nonlispfield)			\
  ((offsetof (type, nonlispfield) - header_size) / word_size)
893

894
/* A char-table is a kind of vectorlike, with contents are like a
895
   vector but with a few other slots.  For some purposes, it makes
896
   sense to handle a char-table with type struct Lisp_Vector.  An
897 898
   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
899 900 901 902
   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.  */
903

904 905 906 907 908 909 910 911 912 913 914 915 916
#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; })
917

918 919 920 921 922 923 924 925 926 927 928 929 930
#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__ */

931 932
/* 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
933
   mathematical comparison; for example, UNSIGNED_CMP (0, <, -1)
934 935 936 937 938 939 940
   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))

941
/* Nonzero iff C is an ASCII character.  */
942
#define ASCII_CHAR_P(c) UNSIGNED_CMP (c, <, 0x80)
943

944
/* Almost equivalent to Faref (CT, IDX) with optimization for ASCII
945
   characters.  Do not check validity of CT.  */
946 947
#define CHAR_TABLE_REF(CT, IDX)					\
  (ASCII_CHAR_P (IDX) ? CHAR_TABLE_REF_ASCII ((CT), (IDX))	\
948
   : char_table_ref ((CT), (IDX)))
949 950

/* Equivalent to Faset (CT, IDX, VAL) with optimization for ASCII and
Dave Love's avatar
Dave Love committed
951
   8-bit European characters.  Do not check validity of CT.  */
952
#define CHAR_TABLE_SET(CT, IDX, VAL)					\
953
  (ASCII_CHAR_P (IDX) && SUB_CHAR_TABLE_P (XCHAR_TABLE (CT)->ascii)	\
954
   ? set_sub_char_table_contents (XCHAR_TABLE (CT)->ascii, IDX, VAL)	\
955 956
   : char_table_set (CT, IDX, VAL))

957 958 959 960 961 962 963
enum CHARTAB_SIZE_BITS
  {
    CHARTAB_SIZE_BITS_0 = 6,
    CHARTAB_SIZE_BITS_1 = 4,
    CHARTAB_SIZE_BITS_2 = 5,
    CHARTAB_SIZE_BITS_3 = 7
  };
964 965 966

extern const int chartab_size[4];

967 968
struct Lisp_Char_Table
  {
969
    /* HEADER.SIZE is the vector's size field, which also holds the
970
       pseudovector type information.  It holds the size, too.
Miles Bader's avatar
Miles Bader committed
971 972
       The size counts the defalt, parent, purpose, ascii,
       contents, and extras slots.  */
973
    struct vectorlike_header header;
974

975 976 <