]> code.delx.au - gnu-emacs/blob - src/lisp.h
Merge from origin/emacs-25
[gnu-emacs] / src / lisp.h
1 /* Fundamental definitions for GNU Emacs Lisp interpreter. -*- coding: utf-8 -*-
2
3 Copyright (C) 1985-1987, 1993-1995, 1997-2016 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 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/>. */
20
21 #ifndef EMACS_LISP_H
22 #define EMACS_LISP_H
23
24 #include <setjmp.h>
25 #include <stdalign.h>
26 #include <stdarg.h>
27 #include <stddef.h>
28 #include <float.h>
29 #include <inttypes.h>
30 #include <limits.h>
31
32 #include <intprops.h>
33 #include <verify.h>
34
35 INLINE_HEADER_BEGIN
36
37 /* Define a TYPE constant ID as an externally visible name. Use like this:
38
39 DEFINE_GDB_SYMBOL_BEGIN (TYPE, ID)
40 # define ID (some integer preprocessor expression of type TYPE)
41 DEFINE_GDB_SYMBOL_END (ID)
42
43 This hack is for the benefit of compilers that do not make macro
44 definitions or enums visible to the debugger. It's used for symbols
45 that .gdbinit needs. */
46
47 #define DECLARE_GDB_SYM(type, id) type const id EXTERNALLY_VISIBLE
48 #ifdef MAIN_PROGRAM
49 # define DEFINE_GDB_SYMBOL_BEGIN(type, id) DECLARE_GDB_SYM (type, id)
50 # define DEFINE_GDB_SYMBOL_END(id) = id;
51 #else
52 # define DEFINE_GDB_SYMBOL_BEGIN(type, id) extern DECLARE_GDB_SYM (type, id)
53 # define DEFINE_GDB_SYMBOL_END(val) ;
54 #endif
55
56 /* The ubiquitous max and min macros. */
57 #undef min
58 #undef max
59 #define max(a, b) ((a) > (b) ? (a) : (b))
60 #define min(a, b) ((a) < (b) ? (a) : (b))
61
62 /* Number of elements in an array. */
63 #define ARRAYELTS(arr) (sizeof (arr) / sizeof (arr)[0])
64
65 /* Number of bits in a Lisp_Object tag. */
66 DEFINE_GDB_SYMBOL_BEGIN (int, GCTYPEBITS)
67 #define GCTYPEBITS 3
68 DEFINE_GDB_SYMBOL_END (GCTYPEBITS)
69
70 /* EMACS_INT - signed integer wide enough to hold an Emacs value
71 EMACS_INT_MAX - maximum value of EMACS_INT; can be used in #if
72 pI - printf length modifier for EMACS_INT
73 EMACS_UINT - unsigned variant of EMACS_INT */
74 #ifndef EMACS_INT_MAX
75 # if INTPTR_MAX <= 0
76 # error "INTPTR_MAX misconfigured"
77 # elif INTPTR_MAX <= INT_MAX && !defined WIDE_EMACS_INT
78 typedef int EMACS_INT;
79 typedef unsigned int EMACS_UINT;
80 # define EMACS_INT_MAX INT_MAX
81 # define pI ""
82 # elif INTPTR_MAX <= LONG_MAX && !defined WIDE_EMACS_INT
83 typedef long int EMACS_INT;
84 typedef unsigned long EMACS_UINT;
85 # define EMACS_INT_MAX LONG_MAX
86 # define pI "l"
87 # elif INTPTR_MAX <= LLONG_MAX
88 typedef long long int EMACS_INT;
89 typedef unsigned long long int EMACS_UINT;
90 # define EMACS_INT_MAX LLONG_MAX
91 # define pI "ll"
92 # else
93 # error "INTPTR_MAX too large"
94 # endif
95 #endif
96
97 /* Number of bits to put in each character in the internal representation
98 of bool vectors. This should not vary across implementations. */
99 enum { BOOL_VECTOR_BITS_PER_CHAR =
100 #define BOOL_VECTOR_BITS_PER_CHAR 8
101 BOOL_VECTOR_BITS_PER_CHAR
102 };
103
104 /* An unsigned integer type representing a fixed-length bit sequence,
105 suitable for bool vector words, GC mark bits, etc. Normally it is size_t
106 for speed, but it is unsigned char on weird platforms. */
107 #if BOOL_VECTOR_BITS_PER_CHAR == CHAR_BIT
108 typedef size_t bits_word;
109 # define BITS_WORD_MAX SIZE_MAX
110 enum { BITS_PER_BITS_WORD = CHAR_BIT * sizeof (bits_word) };
111 #else
112 typedef unsigned char bits_word;
113 # define BITS_WORD_MAX ((1u << BOOL_VECTOR_BITS_PER_CHAR) - 1)
114 enum { BITS_PER_BITS_WORD = BOOL_VECTOR_BITS_PER_CHAR };
115 #endif
116 verify (BITS_WORD_MAX >> (BITS_PER_BITS_WORD - 1) == 1);
117
118 /* Number of bits in some machine integer types. */
119 enum
120 {
121 BITS_PER_CHAR = CHAR_BIT,
122 BITS_PER_SHORT = CHAR_BIT * sizeof (short),
123 BITS_PER_LONG = CHAR_BIT * sizeof (long int),
124 BITS_PER_EMACS_INT = CHAR_BIT * sizeof (EMACS_INT)
125 };
126
127 /* printmax_t and uprintmax_t are types for printing large integers.
128 These are the widest integers that are supported for printing.
129 pMd etc. are conversions for printing them.
130 On C99 hosts, there's no problem, as even the widest integers work.
131 Fall back on EMACS_INT on pre-C99 hosts. */
132 #ifdef PRIdMAX
133 typedef intmax_t printmax_t;
134 typedef uintmax_t uprintmax_t;
135 # define pMd PRIdMAX
136 # define pMu PRIuMAX
137 #else
138 typedef EMACS_INT printmax_t;
139 typedef EMACS_UINT uprintmax_t;
140 # define pMd pI"d"
141 # define pMu pI"u"
142 #endif
143
144 /* Use pD to format ptrdiff_t values, which suffice for indexes into
145 buffers and strings. Emacs never allocates objects larger than
146 PTRDIFF_MAX bytes, as they cause problems with pointer subtraction.
147 In C99, pD can always be "t"; configure it here for the sake of
148 pre-C99 libraries such as glibc 2.0 and Solaris 8. */
149 #if PTRDIFF_MAX == INT_MAX
150 # define pD ""
151 #elif PTRDIFF_MAX == LONG_MAX
152 # define pD "l"
153 #elif PTRDIFF_MAX == LLONG_MAX
154 # define pD "ll"
155 #else
156 # define pD "t"
157 #endif
158
159 /* Extra internal type checking? */
160
161 /* Define Emacs versions of <assert.h>'s 'assert (COND)' and <verify.h>'s
162 'assume (COND)'. COND should be free of side effects, as it may or
163 may not be evaluated.
164
165 'eassert (COND)' checks COND at runtime if ENABLE_CHECKING is
166 defined and suppress_checking is false, and does nothing otherwise.
167 Emacs dies if COND is checked and is false. The suppress_checking
168 variable is initialized to 0 in alloc.c. Set it to 1 using a
169 debugger to temporarily disable aborting on detected internal
170 inconsistencies or error conditions.
171
172 In some cases, a good compiler may be able to optimize away the
173 eassert macro even if ENABLE_CHECKING is true, e.g., if XSTRING (x)
174 uses eassert to test STRINGP (x), but a particular use of XSTRING
175 is invoked only after testing that STRINGP (x) is true, making the
176 test redundant.
177
178 eassume is like eassert except that it also causes the compiler to
179 assume that COND is true afterwards, regardless of whether runtime
180 checking is enabled. This can improve performance in some cases,
181 though it can degrade performance in others. It's often suboptimal
182 for COND to call external functions or access volatile storage. */
183
184 #ifndef ENABLE_CHECKING
185 # define eassert(cond) ((void) (false && (cond))) /* Check COND compiles. */
186 # define eassume(cond) assume (cond)
187 #else /* ENABLE_CHECKING */
188
189 extern _Noreturn void die (const char *, const char *, int);
190
191 extern bool suppress_checking EXTERNALLY_VISIBLE;
192
193 # define eassert(cond) \
194 (suppress_checking || (cond) \
195 ? (void) 0 \
196 : die (# cond, __FILE__, __LINE__))
197 # define eassume(cond) \
198 (suppress_checking \
199 ? assume (cond) \
200 : (cond) \
201 ? (void) 0 \
202 : die (# cond, __FILE__, __LINE__))
203 #endif /* ENABLE_CHECKING */
204
205 \f
206 /* Use the configure flag --enable-check-lisp-object-type to make
207 Lisp_Object use a struct type instead of the default int. The flag
208 causes CHECK_LISP_OBJECT_TYPE to be defined. */
209
210 /***** Select the tagging scheme. *****/
211 /* The following option controls the tagging scheme:
212 - USE_LSB_TAG means that we can assume the least 3 bits of pointers are
213 always 0, and we can thus use them to hold tag bits, without
214 restricting our addressing space.
215
216 If ! USE_LSB_TAG, then use the top 3 bits for tagging, thus
217 restricting our possible address range.
218
219 USE_LSB_TAG not only requires the least 3 bits of pointers returned by
220 malloc to be 0 but also needs to be able to impose a mult-of-8 alignment
221 on the few static Lisp_Objects used: lispsym, all the defsubr, and
222 the two special buffers buffer_defaults and buffer_local_symbols. */
223
224 enum Lisp_Bits
225 {
226 /* 2**GCTYPEBITS. This must be a macro that expands to a literal
227 integer constant, for MSVC. */
228 #define GCALIGNMENT 8
229
230 /* Number of bits in a Lisp_Object value, not counting the tag. */
231 VALBITS = BITS_PER_EMACS_INT - GCTYPEBITS,
232
233 /* Number of bits in a Lisp fixnum tag. */
234 INTTYPEBITS = GCTYPEBITS - 1,
235
236 /* Number of bits in a Lisp fixnum value, not counting the tag. */
237 FIXNUM_BITS = VALBITS + 1
238 };
239
240 #if GCALIGNMENT != 1 << GCTYPEBITS
241 # error "GCALIGNMENT and GCTYPEBITS are inconsistent"
242 #endif
243
244 /* The maximum value that can be stored in a EMACS_INT, assuming all
245 bits other than the type bits contribute to a nonnegative signed value.
246 This can be used in #if, e.g., '#if USE_LSB_TAG' below expands to an
247 expression involving VAL_MAX. */
248 #define VAL_MAX (EMACS_INT_MAX >> (GCTYPEBITS - 1))
249
250 /* Whether the least-significant bits of an EMACS_INT contain the tag.
251 On hosts where pointers-as-ints do not exceed VAL_MAX / 2, USE_LSB_TAG is:
252 a. unnecessary, because the top bits of an EMACS_INT are unused, and
253 b. slower, because it typically requires extra masking.
254 So, USE_LSB_TAG is true only on hosts where it might be useful. */
255 DEFINE_GDB_SYMBOL_BEGIN (bool, USE_LSB_TAG)
256 #define USE_LSB_TAG (VAL_MAX / 2 < INTPTR_MAX)
257 DEFINE_GDB_SYMBOL_END (USE_LSB_TAG)
258
259 #if !USE_LSB_TAG && !defined WIDE_EMACS_INT
260 # error "USE_LSB_TAG not supported on this platform; please report this." \
261 "Try 'configure --with-wide-int' to work around the problem."
262 error !;
263 #endif
264
265 #ifdef HAVE_STRUCT_ATTRIBUTE_ALIGNED
266 # define GCALIGNED __attribute__ ((aligned (GCALIGNMENT)))
267 #else
268 # define GCALIGNED /* empty */
269 #endif
270
271 /* Some operations are so commonly executed that they are implemented
272 as macros, not functions, because otherwise runtime performance would
273 suffer too much when compiling with GCC without optimization.
274 There's no need to inline everything, just the operations that
275 would otherwise cause a serious performance problem.
276
277 For each such operation OP, define a macro lisp_h_OP that contains
278 the operation's implementation. That way, OP can be implemented
279 via a macro definition like this:
280
281 #define OP(x) lisp_h_OP (x)
282
283 and/or via a function definition like this:
284
285 Lisp_Object (OP) (Lisp_Object x) { return lisp_h_OP (x); }
286
287 without worrying about the implementations diverging, since
288 lisp_h_OP defines the actual implementation. The lisp_h_OP macros
289 are intended to be private to this include file, and should not be
290 used elsewhere.
291
292 FIXME: Remove the lisp_h_OP macros, and define just the inline OP
293 functions, once most developers have access to GCC 4.8 or later and
294 can use "gcc -Og" to debug. Maybe in the year 2016. See
295 Bug#11935.
296
297 Commentary for these macros can be found near their corresponding
298 functions, below. */
299
300 #if CHECK_LISP_OBJECT_TYPE
301 # define lisp_h_XLI(o) ((o).i)
302 # define lisp_h_XIL(i) ((Lisp_Object) { i })
303 #else
304 # define lisp_h_XLI(o) (o)
305 # define lisp_h_XIL(i) (i)
306 #endif
307 #define lisp_h_CHECK_LIST_CONS(x, y) CHECK_TYPE (CONSP (x), Qlistp, y)
308 #define lisp_h_CHECK_NUMBER(x) CHECK_TYPE (INTEGERP (x), Qintegerp, x)
309 #define lisp_h_CHECK_SYMBOL(x) CHECK_TYPE (SYMBOLP (x), Qsymbolp, x)
310 #define lisp_h_CHECK_TYPE(ok, predicate, x) \
311 ((ok) ? (void) 0 : (void) wrong_type_argument (predicate, x))
312 #define lisp_h_CONSP(x) (XTYPE (x) == Lisp_Cons)
313 #define lisp_h_EQ(x, y) (XLI (x) == XLI (y))
314 #define lisp_h_FLOATP(x) (XTYPE (x) == Lisp_Float)
315 #define lisp_h_INTEGERP(x) ((XTYPE (x) & (Lisp_Int0 | ~Lisp_Int1)) == Lisp_Int0)
316 #define lisp_h_MARKERP(x) (MISCP (x) && XMISCTYPE (x) == Lisp_Misc_Marker)
317 #define lisp_h_MISCP(x) (XTYPE (x) == Lisp_Misc)
318 #define lisp_h_NILP(x) EQ (x, Qnil)
319 #define lisp_h_SET_SYMBOL_VAL(sym, v) \
320 (eassert ((sym)->redirect == SYMBOL_PLAINVAL), (sym)->val.value = (v))
321 #define lisp_h_SYMBOL_CONSTANT_P(sym) (XSYMBOL (sym)->constant)
322 #define lisp_h_SYMBOL_VAL(sym) \
323 (eassert ((sym)->redirect == SYMBOL_PLAINVAL), (sym)->val.value)
324 #define lisp_h_SYMBOLP(x) (XTYPE (x) == Lisp_Symbol)
325 #define lisp_h_VECTORLIKEP(x) (XTYPE (x) == Lisp_Vectorlike)
326 #define lisp_h_XCAR(c) XCONS (c)->car
327 #define lisp_h_XCDR(c) XCONS (c)->u.cdr
328 #define lisp_h_XCONS(a) \
329 (eassert (CONSP (a)), (struct Lisp_Cons *) XUNTAG (a, Lisp_Cons))
330 #define lisp_h_XHASH(a) XUINT (a)
331 #ifndef GC_CHECK_CONS_LIST
332 # define lisp_h_check_cons_list() ((void) 0)
333 #endif
334 #if USE_LSB_TAG
335 # define lisp_h_make_number(n) \
336 XIL ((EMACS_INT) (((EMACS_UINT) (n) << INTTYPEBITS) + Lisp_Int0))
337 # define lisp_h_XFASTINT(a) XINT (a)
338 # define lisp_h_XINT(a) (XLI (a) >> INTTYPEBITS)
339 # define lisp_h_XSYMBOL(a) \
340 (eassert (SYMBOLP (a)), \
341 (struct Lisp_Symbol *) ((intptr_t) XLI (a) - Lisp_Symbol \
342 + (char *) lispsym))
343 # define lisp_h_XTYPE(a) ((enum Lisp_Type) (XLI (a) & ~VALMASK))
344 # define lisp_h_XUNTAG(a, type) ((void *) (intptr_t) (XLI (a) - (type)))
345 #endif
346
347 /* When compiling via gcc -O0, define the key operations as macros, as
348 Emacs is too slow otherwise. To disable this optimization, compile
349 with -DINLINING=false. */
350 #if (defined __NO_INLINE__ \
351 && ! defined __OPTIMIZE__ && ! defined __OPTIMIZE_SIZE__ \
352 && ! (defined INLINING && ! INLINING))
353 # define DEFINE_KEY_OPS_AS_MACROS true
354 #else
355 # define DEFINE_KEY_OPS_AS_MACROS false
356 #endif
357
358 #if DEFINE_KEY_OPS_AS_MACROS
359 # define XLI(o) lisp_h_XLI (o)
360 # define XIL(i) lisp_h_XIL (i)
361 # define CHECK_LIST_CONS(x, y) lisp_h_CHECK_LIST_CONS (x, y)
362 # define CHECK_NUMBER(x) lisp_h_CHECK_NUMBER (x)
363 # define CHECK_SYMBOL(x) lisp_h_CHECK_SYMBOL (x)
364 # define CHECK_TYPE(ok, predicate, x) lisp_h_CHECK_TYPE (ok, predicate, x)
365 # define CONSP(x) lisp_h_CONSP (x)
366 # define EQ(x, y) lisp_h_EQ (x, y)
367 # define FLOATP(x) lisp_h_FLOATP (x)
368 # define INTEGERP(x) lisp_h_INTEGERP (x)
369 # define MARKERP(x) lisp_h_MARKERP (x)
370 # define MISCP(x) lisp_h_MISCP (x)
371 # define NILP(x) lisp_h_NILP (x)
372 # define SET_SYMBOL_VAL(sym, v) lisp_h_SET_SYMBOL_VAL (sym, v)
373 # define SYMBOL_CONSTANT_P(sym) lisp_h_SYMBOL_CONSTANT_P (sym)
374 # define SYMBOL_VAL(sym) lisp_h_SYMBOL_VAL (sym)
375 # define SYMBOLP(x) lisp_h_SYMBOLP (x)
376 # define VECTORLIKEP(x) lisp_h_VECTORLIKEP (x)
377 # define XCAR(c) lisp_h_XCAR (c)
378 # define XCDR(c) lisp_h_XCDR (c)
379 # define XCONS(a) lisp_h_XCONS (a)
380 # define XHASH(a) lisp_h_XHASH (a)
381 # ifndef GC_CHECK_CONS_LIST
382 # define check_cons_list() lisp_h_check_cons_list ()
383 # endif
384 # if USE_LSB_TAG
385 # define make_number(n) lisp_h_make_number (n)
386 # define XFASTINT(a) lisp_h_XFASTINT (a)
387 # define XINT(a) lisp_h_XINT (a)
388 # define XSYMBOL(a) lisp_h_XSYMBOL (a)
389 # define XTYPE(a) lisp_h_XTYPE (a)
390 # define XUNTAG(a, type) lisp_h_XUNTAG (a, type)
391 # endif
392 #endif
393
394
395 /* Define the fundamental Lisp data structures. */
396
397 /* This is the set of Lisp data types. If you want to define a new
398 data type, read the comments after Lisp_Fwd_Type definition
399 below. */
400
401 /* Lisp integers use 2 tags, to give them one extra bit, thus
402 extending their range from, e.g., -2^28..2^28-1 to -2^29..2^29-1. */
403 #define INTMASK (EMACS_INT_MAX >> (INTTYPEBITS - 1))
404 #define case_Lisp_Int case Lisp_Int0: case Lisp_Int1
405
406 /* Idea stolen from GDB. Pedantic GCC complains about enum bitfields,
407 MSVC doesn't support them, and xlc and Oracle Studio c99 complain
408 vociferously about them. */
409 #if (defined __STRICT_ANSI__ || defined _MSC_VER || defined __IBMC__ \
410 || (defined __SUNPRO_C && __STDC__))
411 #define ENUM_BF(TYPE) unsigned int
412 #else
413 #define ENUM_BF(TYPE) enum TYPE
414 #endif
415
416
417 enum Lisp_Type
418 {
419 /* Symbol. XSYMBOL (object) points to a struct Lisp_Symbol. */
420 Lisp_Symbol = 0,
421
422 /* Miscellaneous. XMISC (object) points to a union Lisp_Misc,
423 whose first member indicates the subtype. */
424 Lisp_Misc = 1,
425
426 /* Integer. XINT (obj) is the integer value. */
427 Lisp_Int0 = 2,
428 Lisp_Int1 = USE_LSB_TAG ? 6 : 3,
429
430 /* String. XSTRING (object) points to a struct Lisp_String.
431 The length of the string, and its contents, are stored therein. */
432 Lisp_String = 4,
433
434 /* Vector of Lisp objects, or something resembling it.
435 XVECTOR (object) points to a struct Lisp_Vector, which contains
436 the size and contents. The size field also contains the type
437 information, if it's not a real vector object. */
438 Lisp_Vectorlike = 5,
439
440 /* Cons. XCONS (object) points to a struct Lisp_Cons. */
441 Lisp_Cons = USE_LSB_TAG ? 3 : 6,
442
443 Lisp_Float = 7
444 };
445
446 /* This is the set of data types that share a common structure.
447 The first member of the structure is a type code from this set.
448 The enum values are arbitrary, but we'll use large numbers to make it
449 more likely that we'll spot the error if a random word in memory is
450 mistakenly interpreted as a Lisp_Misc. */
451 enum Lisp_Misc_Type
452 {
453 Lisp_Misc_Free = 0x5eab,
454 Lisp_Misc_Marker,
455 Lisp_Misc_Overlay,
456 Lisp_Misc_Save_Value,
457 Lisp_Misc_Finalizer,
458 #ifdef HAVE_MODULES
459 Lisp_Misc_User_Ptr,
460 #endif
461 /* Currently floats are not a misc type,
462 but let's define this in case we want to change that. */
463 Lisp_Misc_Float,
464 /* This is not a type code. It is for range checking. */
465 Lisp_Misc_Limit
466 };
467
468 /* These are the types of forwarding objects used in the value slot
469 of symbols for special built-in variables whose value is stored in
470 C variables. */
471 enum Lisp_Fwd_Type
472 {
473 Lisp_Fwd_Int, /* Fwd to a C `int' variable. */
474 Lisp_Fwd_Bool, /* Fwd to a C boolean var. */
475 Lisp_Fwd_Obj, /* Fwd to a C Lisp_Object variable. */
476 Lisp_Fwd_Buffer_Obj, /* Fwd to a Lisp_Object field of buffers. */
477 Lisp_Fwd_Kboard_Obj /* Fwd to a Lisp_Object field of kboards. */
478 };
479
480 /* If you want to define a new Lisp data type, here are some
481 instructions. See the thread at
482 http://lists.gnu.org/archive/html/emacs-devel/2012-10/msg00561.html
483 for more info.
484
485 First, there are already a couple of Lisp types that can be used if
486 your new type does not need to be exposed to Lisp programs nor
487 displayed to users. These are Lisp_Save_Value, a Lisp_Misc
488 subtype; and PVEC_OTHER, a kind of vectorlike object. The former
489 is suitable for temporarily stashing away pointers and integers in
490 a Lisp object. The latter is useful for vector-like Lisp objects
491 that need to be used as part of other objects, but which are never
492 shown to users or Lisp code (search for PVEC_OTHER in xterm.c for
493 an example).
494
495 These two types don't look pretty when printed, so they are
496 unsuitable for Lisp objects that can be exposed to users.
497
498 To define a new data type, add one more Lisp_Misc subtype or one
499 more pseudovector subtype. Pseudovectors are more suitable for
500 objects with several slots that need to support fast random access,
501 while Lisp_Misc types are for everything else. A pseudovector object
502 provides one or more slots for Lisp objects, followed by struct
503 members that are accessible only from C. A Lisp_Misc object is a
504 wrapper for a C struct that can contain anything you like.
505
506 Explicit freeing is discouraged for Lisp objects in general. But if
507 you really need to exploit this, use Lisp_Misc (check free_misc in
508 alloc.c to see why). There is no way to free a vectorlike object.
509
510 To add a new pseudovector type, extend the pvec_type enumeration;
511 to add a new Lisp_Misc, extend the Lisp_Misc_Type enumeration.
512
513 For a Lisp_Misc, you will also need to add your entry to union
514 Lisp_Misc (but make sure the first word has the same structure as
515 the others, starting with a 16-bit member of the Lisp_Misc_Type
516 enumeration and a 1-bit GC markbit) and make sure the overall size
517 of the union is not increased by your addition.
518
519 For a new pseudovector, it's highly desirable to limit the size
520 of your data type by VBLOCK_BYTES_MAX bytes (defined in alloc.c).
521 Otherwise you will need to change sweep_vectors (also in alloc.c).
522
523 Then you will need to add switch branches in print.c (in
524 print_object, to print your object, and possibly also in
525 print_preprocess) and to alloc.c, to mark your object (in
526 mark_object) and to free it (in gc_sweep). The latter is also the
527 right place to call any code specific to your data type that needs
528 to run when the object is recycled -- e.g., free any additional
529 resources allocated for it that are not Lisp objects. You can even
530 make a pointer to the function that frees the resources a slot in
531 your object -- this way, the same object could be used to represent
532 several disparate C structures. */
533
534 #ifdef CHECK_LISP_OBJECT_TYPE
535
536 typedef struct { EMACS_INT i; } Lisp_Object;
537
538 #define LISP_INITIALLY(i) {i}
539
540 #undef CHECK_LISP_OBJECT_TYPE
541 enum CHECK_LISP_OBJECT_TYPE { CHECK_LISP_OBJECT_TYPE = true };
542 #else /* CHECK_LISP_OBJECT_TYPE */
543
544 /* If a struct type is not wanted, define Lisp_Object as just a number. */
545
546 typedef EMACS_INT Lisp_Object;
547 #define LISP_INITIALLY(i) (i)
548 enum CHECK_LISP_OBJECT_TYPE { CHECK_LISP_OBJECT_TYPE = false };
549 #endif /* CHECK_LISP_OBJECT_TYPE */
550
551 #define LISP_INITIALLY_ZERO LISP_INITIALLY (0)
552 \f
553 /* Forward declarations. */
554
555 /* Defined in this file. */
556 union Lisp_Fwd;
557 INLINE bool BOOL_VECTOR_P (Lisp_Object);
558 INLINE bool BUFFER_OBJFWDP (union Lisp_Fwd *);
559 INLINE bool BUFFERP (Lisp_Object);
560 INLINE bool CHAR_TABLE_P (Lisp_Object);
561 INLINE Lisp_Object CHAR_TABLE_REF_ASCII (Lisp_Object, ptrdiff_t);
562 INLINE bool (CONSP) (Lisp_Object);
563 INLINE bool (FLOATP) (Lisp_Object);
564 INLINE bool functionp (Lisp_Object);
565 INLINE bool (INTEGERP) (Lisp_Object);
566 INLINE bool (MARKERP) (Lisp_Object);
567 INLINE bool (MISCP) (Lisp_Object);
568 INLINE bool (NILP) (Lisp_Object);
569 INLINE bool OVERLAYP (Lisp_Object);
570 INLINE bool PROCESSP (Lisp_Object);
571 INLINE bool PSEUDOVECTORP (Lisp_Object, int);
572 INLINE bool SAVE_VALUEP (Lisp_Object);
573 INLINE bool FINALIZERP (Lisp_Object);
574
575 #ifdef HAVE_MODULES
576 INLINE bool USER_PTRP (Lisp_Object);
577 INLINE struct Lisp_User_Ptr *(XUSER_PTR) (Lisp_Object);
578 #endif
579
580 INLINE void set_sub_char_table_contents (Lisp_Object, ptrdiff_t,
581 Lisp_Object);
582 INLINE bool STRINGP (Lisp_Object);
583 INLINE bool SUB_CHAR_TABLE_P (Lisp_Object);
584 INLINE bool SUBRP (Lisp_Object);
585 INLINE bool (SYMBOLP) (Lisp_Object);
586 INLINE bool (VECTORLIKEP) (Lisp_Object);
587 INLINE bool WINDOWP (Lisp_Object);
588 INLINE bool TERMINALP (Lisp_Object);
589 INLINE struct Lisp_Save_Value *XSAVE_VALUE (Lisp_Object);
590 INLINE struct Lisp_Finalizer *XFINALIZER (Lisp_Object);
591 INLINE struct Lisp_Symbol *(XSYMBOL) (Lisp_Object);
592 INLINE void *(XUNTAG) (Lisp_Object, int);
593
594 /* Defined in chartab.c. */
595 extern Lisp_Object char_table_ref (Lisp_Object, int);
596 extern void char_table_set (Lisp_Object, int, Lisp_Object);
597
598 /* Defined in data.c. */
599 extern _Noreturn Lisp_Object wrong_type_argument (Lisp_Object, Lisp_Object);
600 extern _Noreturn void wrong_choice (Lisp_Object, Lisp_Object);
601
602 /* Defined in emacs.c. */
603 #ifdef DOUG_LEA_MALLOC
604 extern bool might_dump;
605 #endif
606 /* True means Emacs has already been initialized.
607 Used during startup to detect startup of dumped Emacs. */
608 extern bool initialized;
609
610 /* Defined in floatfns.c. */
611 extern double extract_float (Lisp_Object);
612
613 \f
614 /* Interned state of a symbol. */
615
616 enum symbol_interned
617 {
618 SYMBOL_UNINTERNED = 0,
619 SYMBOL_INTERNED = 1,
620 SYMBOL_INTERNED_IN_INITIAL_OBARRAY = 2
621 };
622
623 enum symbol_redirect
624 {
625 SYMBOL_PLAINVAL = 4,
626 SYMBOL_VARALIAS = 1,
627 SYMBOL_LOCALIZED = 2,
628 SYMBOL_FORWARDED = 3
629 };
630
631 struct Lisp_Symbol
632 {
633 bool_bf gcmarkbit : 1;
634
635 /* Indicates where the value can be found:
636 0 : it's a plain var, the value is in the `value' field.
637 1 : it's a varalias, the value is really in the `alias' symbol.
638 2 : it's a localized var, the value is in the `blv' object.
639 3 : it's a forwarding variable, the value is in `forward'. */
640 ENUM_BF (symbol_redirect) redirect : 3;
641
642 /* Non-zero means symbol is constant, i.e. changing its value
643 should signal an error. If the value is 3, then the var
644 can be changed, but only by `defconst'. */
645 unsigned constant : 2;
646
647 /* Interned state of the symbol. This is an enumerator from
648 enum symbol_interned. */
649 unsigned interned : 2;
650
651 /* True means that this variable has been explicitly declared
652 special (with `defvar' etc), and shouldn't be lexically bound. */
653 bool_bf declared_special : 1;
654
655 /* True if pointed to from purespace and hence can't be GC'd. */
656 bool_bf pinned : 1;
657
658 /* The symbol's name, as a Lisp string. */
659 Lisp_Object name;
660
661 /* Value of the symbol or Qunbound if unbound. Which alternative of the
662 union is used depends on the `redirect' field above. */
663 union {
664 Lisp_Object value;
665 struct Lisp_Symbol *alias;
666 struct Lisp_Buffer_Local_Value *blv;
667 union Lisp_Fwd *fwd;
668 } val;
669
670 /* Function value of the symbol or Qnil if not fboundp. */
671 Lisp_Object function;
672
673 /* The symbol's property list. */
674 Lisp_Object plist;
675
676 /* Next symbol in obarray bucket, if the symbol is interned. */
677 struct Lisp_Symbol *next;
678 };
679
680 /* Declare a Lisp-callable function. The MAXARGS parameter has the same
681 meaning as in the DEFUN macro, and is used to construct a prototype. */
682 /* We can use the same trick as in the DEFUN macro to generate the
683 appropriate prototype. */
684 #define EXFUN(fnname, maxargs) \
685 extern Lisp_Object fnname DEFUN_ARGS_ ## maxargs
686
687 /* Note that the weird token-substitution semantics of ANSI C makes
688 this work for MANY and UNEVALLED. */
689 #define DEFUN_ARGS_MANY (ptrdiff_t, Lisp_Object *)
690 #define DEFUN_ARGS_UNEVALLED (Lisp_Object)
691 #define DEFUN_ARGS_0 (void)
692 #define DEFUN_ARGS_1 (Lisp_Object)
693 #define DEFUN_ARGS_2 (Lisp_Object, Lisp_Object)
694 #define DEFUN_ARGS_3 (Lisp_Object, Lisp_Object, Lisp_Object)
695 #define DEFUN_ARGS_4 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object)
696 #define DEFUN_ARGS_5 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, \
697 Lisp_Object)
698 #define DEFUN_ARGS_6 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, \
699 Lisp_Object, Lisp_Object)
700 #define DEFUN_ARGS_7 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, \
701 Lisp_Object, Lisp_Object, Lisp_Object)
702 #define DEFUN_ARGS_8 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, \
703 Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object)
704
705 /* Yield a signed integer that contains TAG along with PTR.
706
707 Sign-extend pointers when USE_LSB_TAG (this simplifies emacs-module.c),
708 and zero-extend otherwise (that’s a bit faster here).
709 Sign extension matters only when EMACS_INT is wider than a pointer. */
710 #define TAG_PTR(tag, ptr) \
711 (USE_LSB_TAG \
712 ? (intptr_t) (ptr) + (tag) \
713 : (EMACS_INT) (((EMACS_UINT) (tag) << VALBITS) + (uintptr_t) (ptr)))
714
715 /* Yield an integer that contains a symbol tag along with OFFSET.
716 OFFSET should be the offset in bytes from 'lispsym' to the symbol. */
717 #define TAG_SYMOFFSET(offset) TAG_PTR (Lisp_Symbol, offset)
718
719 /* XLI_BUILTIN_LISPSYM (iQwhatever) is equivalent to
720 XLI (builtin_lisp_symbol (Qwhatever)),
721 except the former expands to an integer constant expression. */
722 #define XLI_BUILTIN_LISPSYM(iname) TAG_SYMOFFSET ((iname) * sizeof *lispsym)
723
724 /* LISPSYM_INITIALLY (Qfoo) is equivalent to Qfoo except it is
725 designed for use as an initializer, even for a constant initializer. */
726 #define LISPSYM_INITIALLY(name) LISP_INITIALLY (XLI_BUILTIN_LISPSYM (i##name))
727
728 /* Declare extern constants for Lisp symbols. These can be helpful
729 when using a debugger like GDB, on older platforms where the debug
730 format does not represent C macros. */
731 #define DEFINE_LISP_SYMBOL(name) \
732 DEFINE_GDB_SYMBOL_BEGIN (Lisp_Object, name) \
733 DEFINE_GDB_SYMBOL_END (LISPSYM_INITIALLY (name))
734
735 /* By default, define macros for Qt, etc., as this leads to a bit
736 better performance in the core Emacs interpreter. A plugin can
737 define DEFINE_NON_NIL_Q_SYMBOL_MACROS to be false, to be portable to
738 other Emacs instances that assign different values to Qt, etc. */
739 #ifndef DEFINE_NON_NIL_Q_SYMBOL_MACROS
740 # define DEFINE_NON_NIL_Q_SYMBOL_MACROS true
741 #endif
742
743 #include "globals.h"
744
745 /* Convert a Lisp_Object to the corresponding EMACS_INT and vice versa.
746 At the machine level, these operations are no-ops. */
747
748 INLINE EMACS_INT
749 (XLI) (Lisp_Object o)
750 {
751 return lisp_h_XLI (o);
752 }
753
754 INLINE Lisp_Object
755 (XIL) (EMACS_INT i)
756 {
757 return lisp_h_XIL (i);
758 }
759
760 /* In the size word of a vector, this bit means the vector has been marked. */
761
762 DEFINE_GDB_SYMBOL_BEGIN (ptrdiff_t, ARRAY_MARK_FLAG)
763 # define ARRAY_MARK_FLAG PTRDIFF_MIN
764 DEFINE_GDB_SYMBOL_END (ARRAY_MARK_FLAG)
765
766 /* In the size word of a struct Lisp_Vector, this bit means it's really
767 some other vector-like object. */
768 DEFINE_GDB_SYMBOL_BEGIN (ptrdiff_t, PSEUDOVECTOR_FLAG)
769 # define PSEUDOVECTOR_FLAG (PTRDIFF_MAX - PTRDIFF_MAX / 2)
770 DEFINE_GDB_SYMBOL_END (PSEUDOVECTOR_FLAG)
771
772 /* In a pseudovector, the size field actually contains a word with one
773 PSEUDOVECTOR_FLAG bit set, and one of the following values extracted
774 with PVEC_TYPE_MASK to indicate the actual type. */
775 enum pvec_type
776 {
777 PVEC_NORMAL_VECTOR,
778 PVEC_FREE,
779 PVEC_PROCESS,
780 PVEC_FRAME,
781 PVEC_WINDOW,
782 PVEC_BOOL_VECTOR,
783 PVEC_BUFFER,
784 PVEC_HASH_TABLE,
785 PVEC_TERMINAL,
786 PVEC_WINDOW_CONFIGURATION,
787 PVEC_SUBR,
788 PVEC_OTHER,
789 PVEC_XWIDGET,
790 PVEC_XWIDGET_VIEW,
791
792 /* These should be last, check internal_equal to see why. */
793 PVEC_COMPILED,
794 PVEC_CHAR_TABLE,
795 PVEC_SUB_CHAR_TABLE,
796 PVEC_FONT /* Should be last because it's used for range checking. */
797 };
798
799 enum More_Lisp_Bits
800 {
801 /* For convenience, we also store the number of elements in these bits.
802 Note that this size is not necessarily the memory-footprint size, but
803 only the number of Lisp_Object fields (that need to be traced by GC).
804 The distinction is used, e.g., by Lisp_Process, which places extra
805 non-Lisp_Object fields at the end of the structure. */
806 PSEUDOVECTOR_SIZE_BITS = 12,
807 PSEUDOVECTOR_SIZE_MASK = (1 << PSEUDOVECTOR_SIZE_BITS) - 1,
808
809 /* To calculate the memory footprint of the pseudovector, it's useful
810 to store the size of non-Lisp area in word_size units here. */
811 PSEUDOVECTOR_REST_BITS = 12,
812 PSEUDOVECTOR_REST_MASK = (((1 << PSEUDOVECTOR_REST_BITS) - 1)
813 << PSEUDOVECTOR_SIZE_BITS),
814
815 /* Used to extract pseudovector subtype information. */
816 PSEUDOVECTOR_AREA_BITS = PSEUDOVECTOR_SIZE_BITS + PSEUDOVECTOR_REST_BITS,
817 PVEC_TYPE_MASK = 0x3f << PSEUDOVECTOR_AREA_BITS
818 };
819 \f
820 /* These functions extract various sorts of values from a Lisp_Object.
821 For example, if tem is a Lisp_Object whose type is Lisp_Cons,
822 XCONS (tem) is the struct Lisp_Cons * pointing to the memory for
823 that cons. */
824
825 /* Mask for the value (as opposed to the type bits) of a Lisp object. */
826 DEFINE_GDB_SYMBOL_BEGIN (EMACS_INT, VALMASK)
827 # define VALMASK (USE_LSB_TAG ? - (1 << GCTYPEBITS) : VAL_MAX)
828 DEFINE_GDB_SYMBOL_END (VALMASK)
829
830 /* Largest and smallest representable fixnum values. These are the C
831 values. They are macros for use in static initializers. */
832 #define MOST_POSITIVE_FIXNUM (EMACS_INT_MAX >> INTTYPEBITS)
833 #define MOST_NEGATIVE_FIXNUM (-1 - MOST_POSITIVE_FIXNUM)
834
835 #if USE_LSB_TAG
836
837 INLINE Lisp_Object
838 (make_number) (EMACS_INT n)
839 {
840 return lisp_h_make_number (n);
841 }
842
843 INLINE EMACS_INT
844 (XINT) (Lisp_Object a)
845 {
846 return lisp_h_XINT (a);
847 }
848
849 INLINE EMACS_INT
850 (XFASTINT) (Lisp_Object a)
851 {
852 EMACS_INT n = lisp_h_XFASTINT (a);
853 eassume (0 <= n);
854 return n;
855 }
856
857 INLINE struct Lisp_Symbol *
858 (XSYMBOL) (Lisp_Object a)
859 {
860 return lisp_h_XSYMBOL (a);
861 }
862
863 INLINE enum Lisp_Type
864 (XTYPE) (Lisp_Object a)
865 {
866 return lisp_h_XTYPE (a);
867 }
868
869 INLINE void *
870 (XUNTAG) (Lisp_Object a, int type)
871 {
872 return lisp_h_XUNTAG (a, type);
873 }
874
875 #else /* ! USE_LSB_TAG */
876
877 /* Although compiled only if ! USE_LSB_TAG, the following functions
878 also work when USE_LSB_TAG; this is to aid future maintenance when
879 the lisp_h_* macros are eventually removed. */
880
881 /* Make a Lisp integer representing the value of the low order
882 bits of N. */
883 INLINE Lisp_Object
884 make_number (EMACS_INT n)
885 {
886 EMACS_INT int0 = Lisp_Int0;
887 if (USE_LSB_TAG)
888 {
889 EMACS_UINT u = n;
890 n = u << INTTYPEBITS;
891 n += int0;
892 }
893 else
894 {
895 n &= INTMASK;
896 n += (int0 << VALBITS);
897 }
898 return XIL (n);
899 }
900
901 /* Extract A's value as a signed integer. */
902 INLINE EMACS_INT
903 XINT (Lisp_Object a)
904 {
905 EMACS_INT i = XLI (a);
906 if (! USE_LSB_TAG)
907 {
908 EMACS_UINT u = i;
909 i = u << INTTYPEBITS;
910 }
911 return i >> INTTYPEBITS;
912 }
913
914 /* Like XINT (A), but may be faster. A must be nonnegative.
915 If ! USE_LSB_TAG, this takes advantage of the fact that Lisp
916 integers have zero-bits in their tags. */
917 INLINE EMACS_INT
918 XFASTINT (Lisp_Object a)
919 {
920 EMACS_INT int0 = Lisp_Int0;
921 EMACS_INT n = USE_LSB_TAG ? XINT (a) : XLI (a) - (int0 << VALBITS);
922 eassume (0 <= n);
923 return n;
924 }
925
926 /* Extract A's type. */
927 INLINE enum Lisp_Type
928 XTYPE (Lisp_Object a)
929 {
930 EMACS_UINT i = XLI (a);
931 return USE_LSB_TAG ? i & ~VALMASK : i >> VALBITS;
932 }
933
934 /* Extract A's value as a symbol. */
935 INLINE struct Lisp_Symbol *
936 XSYMBOL (Lisp_Object a)
937 {
938 eassert (SYMBOLP (a));
939 intptr_t i = (intptr_t) XUNTAG (a, Lisp_Symbol);
940 void *p = (char *) lispsym + i;
941 return p;
942 }
943
944 /* Extract A's pointer value, assuming A's type is TYPE. */
945 INLINE void *
946 XUNTAG (Lisp_Object a, int type)
947 {
948 intptr_t i = USE_LSB_TAG ? XLI (a) - type : XLI (a) & VALMASK;
949 return (void *) i;
950 }
951
952 #endif /* ! USE_LSB_TAG */
953
954 /* Extract A's value as an unsigned integer. */
955 INLINE EMACS_UINT
956 XUINT (Lisp_Object a)
957 {
958 EMACS_UINT i = XLI (a);
959 return USE_LSB_TAG ? i >> INTTYPEBITS : i & INTMASK;
960 }
961
962 /* Return A's (Lisp-integer sized) hash. Happens to be like XUINT
963 right now, but XUINT should only be applied to objects we know are
964 integers. */
965
966 INLINE EMACS_INT
967 (XHASH) (Lisp_Object a)
968 {
969 return lisp_h_XHASH (a);
970 }
971
972 /* Like make_number (N), but may be faster. N must be in nonnegative range. */
973 INLINE Lisp_Object
974 make_natnum (EMACS_INT n)
975 {
976 eassert (0 <= n && n <= MOST_POSITIVE_FIXNUM);
977 EMACS_INT int0 = Lisp_Int0;
978 return USE_LSB_TAG ? make_number (n) : XIL (n + (int0 << VALBITS));
979 }
980
981 /* Return true if X and Y are the same object. */
982
983 INLINE bool
984 (EQ) (Lisp_Object x, Lisp_Object y)
985 {
986 return lisp_h_EQ (x, y);
987 }
988
989 /* Value is true if I doesn't fit into a Lisp fixnum. It is
990 written this way so that it also works if I is of unsigned
991 type or if I is a NaN. */
992
993 #define FIXNUM_OVERFLOW_P(i) \
994 (! ((0 <= (i) || MOST_NEGATIVE_FIXNUM <= (i)) && (i) <= MOST_POSITIVE_FIXNUM))
995
996 INLINE ptrdiff_t
997 clip_to_bounds (ptrdiff_t lower, EMACS_INT num, ptrdiff_t upper)
998 {
999 return num < lower ? lower : num <= upper ? num : upper;
1000 }
1001 \f
1002
1003 /* Extract a value or address from a Lisp_Object. */
1004
1005 INLINE struct Lisp_Cons *
1006 (XCONS) (Lisp_Object a)
1007 {
1008 return lisp_h_XCONS (a);
1009 }
1010
1011 INLINE struct Lisp_Vector *
1012 XVECTOR (Lisp_Object a)
1013 {
1014 eassert (VECTORLIKEP (a));
1015 return XUNTAG (a, Lisp_Vectorlike);
1016 }
1017
1018 INLINE struct Lisp_String *
1019 XSTRING (Lisp_Object a)
1020 {
1021 eassert (STRINGP (a));
1022 return XUNTAG (a, Lisp_String);
1023 }
1024
1025 /* The index of the C-defined Lisp symbol SYM.
1026 This can be used in a static initializer. */
1027 #define SYMBOL_INDEX(sym) i##sym
1028
1029 INLINE struct Lisp_Float *
1030 XFLOAT (Lisp_Object a)
1031 {
1032 eassert (FLOATP (a));
1033 return XUNTAG (a, Lisp_Float);
1034 }
1035
1036 /* Pseudovector types. */
1037
1038 INLINE struct Lisp_Process *
1039 XPROCESS (Lisp_Object a)
1040 {
1041 eassert (PROCESSP (a));
1042 return XUNTAG (a, Lisp_Vectorlike);
1043 }
1044
1045 INLINE struct window *
1046 XWINDOW (Lisp_Object a)
1047 {
1048 eassert (WINDOWP (a));
1049 return XUNTAG (a, Lisp_Vectorlike);
1050 }
1051
1052 INLINE struct terminal *
1053 XTERMINAL (Lisp_Object a)
1054 {
1055 eassert (TERMINALP (a));
1056 return XUNTAG (a, Lisp_Vectorlike);
1057 }
1058
1059 INLINE struct Lisp_Subr *
1060 XSUBR (Lisp_Object a)
1061 {
1062 eassert (SUBRP (a));
1063 return XUNTAG (a, Lisp_Vectorlike);
1064 }
1065
1066 INLINE struct buffer *
1067 XBUFFER (Lisp_Object a)
1068 {
1069 eassert (BUFFERP (a));
1070 return XUNTAG (a, Lisp_Vectorlike);
1071 }
1072
1073 INLINE struct Lisp_Char_Table *
1074 XCHAR_TABLE (Lisp_Object a)
1075 {
1076 eassert (CHAR_TABLE_P (a));
1077 return XUNTAG (a, Lisp_Vectorlike);
1078 }
1079
1080 INLINE struct Lisp_Sub_Char_Table *
1081 XSUB_CHAR_TABLE (Lisp_Object a)
1082 {
1083 eassert (SUB_CHAR_TABLE_P (a));
1084 return XUNTAG (a, Lisp_Vectorlike);
1085 }
1086
1087 INLINE struct Lisp_Bool_Vector *
1088 XBOOL_VECTOR (Lisp_Object a)
1089 {
1090 eassert (BOOL_VECTOR_P (a));
1091 return XUNTAG (a, Lisp_Vectorlike);
1092 }
1093
1094 /* Construct a Lisp_Object from a value or address. */
1095
1096 INLINE Lisp_Object
1097 make_lisp_ptr (void *ptr, enum Lisp_Type type)
1098 {
1099 Lisp_Object a = XIL (TAG_PTR (type, ptr));
1100 eassert (XTYPE (a) == type && XUNTAG (a, type) == ptr);
1101 return a;
1102 }
1103
1104 INLINE Lisp_Object
1105 make_lisp_symbol (struct Lisp_Symbol *sym)
1106 {
1107 Lisp_Object a = XIL (TAG_SYMOFFSET ((char *) sym - (char *) lispsym));
1108 eassert (XSYMBOL (a) == sym);
1109 return a;
1110 }
1111
1112 INLINE Lisp_Object
1113 builtin_lisp_symbol (int index)
1114 {
1115 return make_lisp_symbol (lispsym + index);
1116 }
1117
1118 #define XSETINT(a, b) ((a) = make_number (b))
1119 #define XSETFASTINT(a, b) ((a) = make_natnum (b))
1120 #define XSETCONS(a, b) ((a) = make_lisp_ptr (b, Lisp_Cons))
1121 #define XSETVECTOR(a, b) ((a) = make_lisp_ptr (b, Lisp_Vectorlike))
1122 #define XSETSTRING(a, b) ((a) = make_lisp_ptr (b, Lisp_String))
1123 #define XSETSYMBOL(a, b) ((a) = make_lisp_symbol (b))
1124 #define XSETFLOAT(a, b) ((a) = make_lisp_ptr (b, Lisp_Float))
1125 #define XSETMISC(a, b) ((a) = make_lisp_ptr (b, Lisp_Misc))
1126
1127 /* Pseudovector types. */
1128
1129 #define XSETPVECTYPE(v, code) \
1130 ((v)->header.size |= PSEUDOVECTOR_FLAG | ((code) << PSEUDOVECTOR_AREA_BITS))
1131 #define XSETPVECTYPESIZE(v, code, lispsize, restsize) \
1132 ((v)->header.size = (PSEUDOVECTOR_FLAG \
1133 | ((code) << PSEUDOVECTOR_AREA_BITS) \
1134 | ((restsize) << PSEUDOVECTOR_SIZE_BITS) \
1135 | (lispsize)))
1136
1137 /* The cast to struct vectorlike_header * avoids aliasing issues. */
1138 #define XSETPSEUDOVECTOR(a, b, code) \
1139 XSETTYPED_PSEUDOVECTOR (a, b, \
1140 (((struct vectorlike_header *) \
1141 XUNTAG (a, Lisp_Vectorlike)) \
1142 ->size), \
1143 code)
1144 #define XSETTYPED_PSEUDOVECTOR(a, b, size, code) \
1145 (XSETVECTOR (a, b), \
1146 eassert ((size & (PSEUDOVECTOR_FLAG | PVEC_TYPE_MASK)) \
1147 == (PSEUDOVECTOR_FLAG | (code << PSEUDOVECTOR_AREA_BITS))))
1148
1149 #define XSETWINDOW_CONFIGURATION(a, b) \
1150 (XSETPSEUDOVECTOR (a, b, PVEC_WINDOW_CONFIGURATION))
1151 #define XSETPROCESS(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_PROCESS))
1152 #define XSETWINDOW(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_WINDOW))
1153 #define XSETTERMINAL(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_TERMINAL))
1154 #define XSETSUBR(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_SUBR))
1155 #define XSETCOMPILED(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_COMPILED))
1156 #define XSETBUFFER(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_BUFFER))
1157 #define XSETCHAR_TABLE(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_CHAR_TABLE))
1158 #define XSETBOOL_VECTOR(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_BOOL_VECTOR))
1159 #define XSETSUB_CHAR_TABLE(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_SUB_CHAR_TABLE))
1160
1161 /* Efficiently convert a pointer to a Lisp object and back. The
1162 pointer is represented as a Lisp integer, so the garbage collector
1163 does not know about it. The pointer should not have both Lisp_Int1
1164 bits set, which makes this conversion inherently unportable. */
1165
1166 INLINE void *
1167 XINTPTR (Lisp_Object a)
1168 {
1169 return XUNTAG (a, Lisp_Int0);
1170 }
1171
1172 INLINE Lisp_Object
1173 make_pointer_integer (void *p)
1174 {
1175 Lisp_Object a = XIL (TAG_PTR (Lisp_Int0, p));
1176 eassert (INTEGERP (a) && XINTPTR (a) == p);
1177 return a;
1178 }
1179
1180 /* Type checking. */
1181
1182 INLINE void
1183 (CHECK_TYPE) (int ok, Lisp_Object predicate, Lisp_Object x)
1184 {
1185 lisp_h_CHECK_TYPE (ok, predicate, x);
1186 }
1187
1188 /* See the macros in intervals.h. */
1189
1190 typedef struct interval *INTERVAL;
1191
1192 struct GCALIGNED Lisp_Cons
1193 {
1194 /* Car of this cons cell. */
1195 Lisp_Object car;
1196
1197 union
1198 {
1199 /* Cdr of this cons cell. */
1200 Lisp_Object cdr;
1201
1202 /* Used to chain conses on a free list. */
1203 struct Lisp_Cons *chain;
1204 } u;
1205 };
1206
1207 /* Take the car or cdr of something known to be a cons cell. */
1208 /* The _addr functions shouldn't be used outside of the minimal set
1209 of code that has to know what a cons cell looks like. Other code not
1210 part of the basic lisp implementation should assume that the car and cdr
1211 fields are not accessible. (What if we want to switch to
1212 a copying collector someday? Cached cons cell field addresses may be
1213 invalidated at arbitrary points.) */
1214 INLINE Lisp_Object *
1215 xcar_addr (Lisp_Object c)
1216 {
1217 return &XCONS (c)->car;
1218 }
1219 INLINE Lisp_Object *
1220 xcdr_addr (Lisp_Object c)
1221 {
1222 return &XCONS (c)->u.cdr;
1223 }
1224
1225 /* Use these from normal code. */
1226
1227 INLINE Lisp_Object
1228 (XCAR) (Lisp_Object c)
1229 {
1230 return lisp_h_XCAR (c);
1231 }
1232
1233 INLINE Lisp_Object
1234 (XCDR) (Lisp_Object c)
1235 {
1236 return lisp_h_XCDR (c);
1237 }
1238
1239 /* Use these to set the fields of a cons cell.
1240
1241 Note that both arguments may refer to the same object, so 'n'
1242 should not be read after 'c' is first modified. */
1243 INLINE void
1244 XSETCAR (Lisp_Object c, Lisp_Object n)
1245 {
1246 *xcar_addr (c) = n;
1247 }
1248 INLINE void
1249 XSETCDR (Lisp_Object c, Lisp_Object n)
1250 {
1251 *xcdr_addr (c) = n;
1252 }
1253
1254 /* Take the car or cdr of something whose type is not known. */
1255 INLINE Lisp_Object
1256 CAR (Lisp_Object c)
1257 {
1258 return (CONSP (c) ? XCAR (c)
1259 : NILP (c) ? Qnil
1260 : wrong_type_argument (Qlistp, c));
1261 }
1262 INLINE Lisp_Object
1263 CDR (Lisp_Object c)
1264 {
1265 return (CONSP (c) ? XCDR (c)
1266 : NILP (c) ? Qnil
1267 : wrong_type_argument (Qlistp, c));
1268 }
1269
1270 /* Take the car or cdr of something whose type is not known. */
1271 INLINE Lisp_Object
1272 CAR_SAFE (Lisp_Object c)
1273 {
1274 return CONSP (c) ? XCAR (c) : Qnil;
1275 }
1276 INLINE Lisp_Object
1277 CDR_SAFE (Lisp_Object c)
1278 {
1279 return CONSP (c) ? XCDR (c) : Qnil;
1280 }
1281
1282 /* In a string or vector, the sign bit of the `size' is the gc mark bit. */
1283
1284 struct GCALIGNED Lisp_String
1285 {
1286 ptrdiff_t size;
1287 ptrdiff_t size_byte;
1288 INTERVAL intervals; /* Text properties in this string. */
1289 unsigned char *data;
1290 };
1291
1292 /* True if STR is a multibyte string. */
1293 INLINE bool
1294 STRING_MULTIBYTE (Lisp_Object str)
1295 {
1296 return 0 <= XSTRING (str)->size_byte;
1297 }
1298
1299 /* An upper bound on the number of bytes in a Lisp string, not
1300 counting the terminating null. This a tight enough bound to
1301 prevent integer overflow errors that would otherwise occur during
1302 string size calculations. A string cannot contain more bytes than
1303 a fixnum can represent, nor can it be so long that C pointer
1304 arithmetic stops working on the string plus its terminating null.
1305 Although the actual size limit (see STRING_BYTES_MAX in alloc.c)
1306 may be a bit smaller than STRING_BYTES_BOUND, calculating it here
1307 would expose alloc.c internal details that we'd rather keep
1308 private.
1309
1310 This is a macro for use in static initializers. The cast to
1311 ptrdiff_t ensures that the macro is signed. */
1312 #define STRING_BYTES_BOUND \
1313 ((ptrdiff_t) min (MOST_POSITIVE_FIXNUM, min (SIZE_MAX, PTRDIFF_MAX) - 1))
1314
1315 /* Mark STR as a unibyte string. */
1316 #define STRING_SET_UNIBYTE(STR) \
1317 do { \
1318 if (XSTRING (STR)->size == 0) \
1319 (STR) = empty_unibyte_string; \
1320 else \
1321 XSTRING (STR)->size_byte = -1; \
1322 } while (false)
1323
1324 /* Mark STR as a multibyte string. Assure that STR contains only
1325 ASCII characters in advance. */
1326 #define STRING_SET_MULTIBYTE(STR) \
1327 do { \
1328 if (XSTRING (STR)->size == 0) \
1329 (STR) = empty_multibyte_string; \
1330 else \
1331 XSTRING (STR)->size_byte = XSTRING (STR)->size; \
1332 } while (false)
1333
1334 /* Convenience functions for dealing with Lisp strings. */
1335
1336 INLINE unsigned char *
1337 SDATA (Lisp_Object string)
1338 {
1339 return XSTRING (string)->data;
1340 }
1341 INLINE char *
1342 SSDATA (Lisp_Object string)
1343 {
1344 /* Avoid "differ in sign" warnings. */
1345 return (char *) SDATA (string);
1346 }
1347 INLINE unsigned char
1348 SREF (Lisp_Object string, ptrdiff_t index)
1349 {
1350 return SDATA (string)[index];
1351 }
1352 INLINE void
1353 SSET (Lisp_Object string, ptrdiff_t index, unsigned char new)
1354 {
1355 SDATA (string)[index] = new;
1356 }
1357 INLINE ptrdiff_t
1358 SCHARS (Lisp_Object string)
1359 {
1360 return XSTRING (string)->size;
1361 }
1362
1363 #ifdef GC_CHECK_STRING_BYTES
1364 extern ptrdiff_t string_bytes (struct Lisp_String *);
1365 #endif
1366 INLINE ptrdiff_t
1367 STRING_BYTES (struct Lisp_String *s)
1368 {
1369 #ifdef GC_CHECK_STRING_BYTES
1370 return string_bytes (s);
1371 #else
1372 return s->size_byte < 0 ? s->size : s->size_byte;
1373 #endif
1374 }
1375
1376 INLINE ptrdiff_t
1377 SBYTES (Lisp_Object string)
1378 {
1379 return STRING_BYTES (XSTRING (string));
1380 }
1381 INLINE void
1382 STRING_SET_CHARS (Lisp_Object string, ptrdiff_t newsize)
1383 {
1384 XSTRING (string)->size = newsize;
1385 }
1386
1387 /* Header of vector-like objects. This documents the layout constraints on
1388 vectors and pseudovectors (objects of PVEC_xxx subtype). It also prevents
1389 compilers from being fooled by Emacs's type punning: XSETPSEUDOVECTOR
1390 and PSEUDOVECTORP cast their pointers to struct vectorlike_header *,
1391 because when two such pointers potentially alias, a compiler won't
1392 incorrectly reorder loads and stores to their size fields. See
1393 Bug#8546. */
1394 struct vectorlike_header
1395 {
1396 /* The only field contains various pieces of information:
1397 - The MSB (ARRAY_MARK_FLAG) holds the gcmarkbit.
1398 - The next bit (PSEUDOVECTOR_FLAG) indicates whether this is a plain
1399 vector (0) or a pseudovector (1).
1400 - If PSEUDOVECTOR_FLAG is 0, the rest holds the size (number
1401 of slots) of the vector.
1402 - If PSEUDOVECTOR_FLAG is 1, the rest is subdivided into three fields:
1403 - a) pseudovector subtype held in PVEC_TYPE_MASK field;
1404 - b) number of Lisp_Objects slots at the beginning of the object
1405 held in PSEUDOVECTOR_SIZE_MASK field. These objects are always
1406 traced by the GC;
1407 - c) size of the rest fields held in PSEUDOVECTOR_REST_MASK and
1408 measured in word_size units. Rest fields may also include
1409 Lisp_Objects, but these objects usually needs some special treatment
1410 during GC.
1411 There are some exceptions. For PVEC_FREE, b) is always zero. For
1412 PVEC_BOOL_VECTOR and PVEC_SUBR, both b) and c) are always zero.
1413 Current layout limits the pseudovectors to 63 PVEC_xxx subtypes,
1414 4095 Lisp_Objects in GC-ed area and 4095 word-sized other slots. */
1415 ptrdiff_t size;
1416 };
1417
1418 /* A regular vector is just a header plus an array of Lisp_Objects. */
1419
1420 struct Lisp_Vector
1421 {
1422 struct vectorlike_header header;
1423 Lisp_Object contents[FLEXIBLE_ARRAY_MEMBER];
1424 };
1425
1426 /* C11 prohibits alignof (struct Lisp_Vector), so compute it manually. */
1427 enum
1428 {
1429 ALIGNOF_STRUCT_LISP_VECTOR
1430 = alignof (union { struct vectorlike_header a; Lisp_Object b; })
1431 };
1432
1433 /* A boolvector is a kind of vectorlike, with contents like a string. */
1434
1435 struct Lisp_Bool_Vector
1436 {
1437 /* HEADER.SIZE is the vector's size field. It doesn't have the real size,
1438 just the subtype information. */
1439 struct vectorlike_header header;
1440 /* This is the size in bits. */
1441 EMACS_INT size;
1442 /* The actual bits, packed into bytes.
1443 Zeros fill out the last word if needed.
1444 The bits are in little-endian order in the bytes, and
1445 the bytes are in little-endian order in the words. */
1446 bits_word data[FLEXIBLE_ARRAY_MEMBER];
1447 };
1448
1449 INLINE EMACS_INT
1450 bool_vector_size (Lisp_Object a)
1451 {
1452 EMACS_INT size = XBOOL_VECTOR (a)->size;
1453 eassume (0 <= size);
1454 return size;
1455 }
1456
1457 INLINE bits_word *
1458 bool_vector_data (Lisp_Object a)
1459 {
1460 return XBOOL_VECTOR (a)->data;
1461 }
1462
1463 INLINE unsigned char *
1464 bool_vector_uchar_data (Lisp_Object a)
1465 {
1466 return (unsigned char *) bool_vector_data (a);
1467 }
1468
1469 /* The number of data words and bytes in a bool vector with SIZE bits. */
1470
1471 INLINE EMACS_INT
1472 bool_vector_words (EMACS_INT size)
1473 {
1474 eassume (0 <= size && size <= EMACS_INT_MAX - (BITS_PER_BITS_WORD - 1));
1475 return (size + BITS_PER_BITS_WORD - 1) / BITS_PER_BITS_WORD;
1476 }
1477
1478 INLINE EMACS_INT
1479 bool_vector_bytes (EMACS_INT size)
1480 {
1481 eassume (0 <= size && size <= EMACS_INT_MAX - (BITS_PER_BITS_WORD - 1));
1482 return (size + BOOL_VECTOR_BITS_PER_CHAR - 1) / BOOL_VECTOR_BITS_PER_CHAR;
1483 }
1484
1485 /* True if A's Ith bit is set. */
1486
1487 INLINE bool
1488 bool_vector_bitref (Lisp_Object a, EMACS_INT i)
1489 {
1490 eassume (0 <= i && i < bool_vector_size (a));
1491 return !! (bool_vector_uchar_data (a)[i / BOOL_VECTOR_BITS_PER_CHAR]
1492 & (1 << (i % BOOL_VECTOR_BITS_PER_CHAR)));
1493 }
1494
1495 INLINE Lisp_Object
1496 bool_vector_ref (Lisp_Object a, EMACS_INT i)
1497 {
1498 return bool_vector_bitref (a, i) ? Qt : Qnil;
1499 }
1500
1501 /* Set A's Ith bit to B. */
1502
1503 INLINE void
1504 bool_vector_set (Lisp_Object a, EMACS_INT i, bool b)
1505 {
1506 unsigned char *addr;
1507
1508 eassume (0 <= i && i < bool_vector_size (a));
1509 addr = &bool_vector_uchar_data (a)[i / BOOL_VECTOR_BITS_PER_CHAR];
1510
1511 if (b)
1512 *addr |= 1 << (i % BOOL_VECTOR_BITS_PER_CHAR);
1513 else
1514 *addr &= ~ (1 << (i % BOOL_VECTOR_BITS_PER_CHAR));
1515 }
1516
1517 /* Some handy constants for calculating sizes
1518 and offsets, mostly of vectorlike objects. */
1519
1520 enum
1521 {
1522 header_size = offsetof (struct Lisp_Vector, contents),
1523 bool_header_size = offsetof (struct Lisp_Bool_Vector, data),
1524 word_size = sizeof (Lisp_Object)
1525 };
1526
1527 /* Conveniences for dealing with Lisp arrays. */
1528
1529 INLINE Lisp_Object
1530 AREF (Lisp_Object array, ptrdiff_t idx)
1531 {
1532 return XVECTOR (array)->contents[idx];
1533 }
1534
1535 INLINE Lisp_Object *
1536 aref_addr (Lisp_Object array, ptrdiff_t idx)
1537 {
1538 return & XVECTOR (array)->contents[idx];
1539 }
1540
1541 INLINE ptrdiff_t
1542 ASIZE (Lisp_Object array)
1543 {
1544 ptrdiff_t size = XVECTOR (array)->header.size;
1545 eassume (0 <= size);
1546 return size;
1547 }
1548
1549 INLINE ptrdiff_t
1550 gc_asize (Lisp_Object array)
1551 {
1552 /* Like ASIZE, but also can be used in the garbage collector. */
1553 return XVECTOR (array)->header.size & ~ARRAY_MARK_FLAG;
1554 }
1555
1556 INLINE void
1557 ASET (Lisp_Object array, ptrdiff_t idx, Lisp_Object val)
1558 {
1559 eassert (0 <= idx && idx < ASIZE (array));
1560 XVECTOR (array)->contents[idx] = val;
1561 }
1562
1563 INLINE void
1564 gc_aset (Lisp_Object array, ptrdiff_t idx, Lisp_Object val)
1565 {
1566 /* Like ASET, but also can be used in the garbage collector:
1567 sweep_weak_table calls set_hash_key etc. while the table is marked. */
1568 eassert (0 <= idx && idx < gc_asize (array));
1569 XVECTOR (array)->contents[idx] = val;
1570 }
1571
1572 /* True, since Qnil's representation is zero. Every place in the code
1573 that assumes Qnil is zero should verify (NIL_IS_ZERO), to make it easy
1574 to find such assumptions later if we change Qnil to be nonzero. */
1575 enum { NIL_IS_ZERO = XLI_BUILTIN_LISPSYM (iQnil) == 0 };
1576
1577 /* Clear the object addressed by P, with size NBYTES, so that all its
1578 bytes are zero and all its Lisp values are nil. */
1579 INLINE void
1580 memclear (void *p, ptrdiff_t nbytes)
1581 {
1582 eassert (0 <= nbytes);
1583 verify (NIL_IS_ZERO);
1584 /* Since Qnil is zero, memset suffices. */
1585 memset (p, 0, nbytes);
1586 }
1587
1588 /* If a struct is made to look like a vector, this macro returns the length
1589 of the shortest vector that would hold that struct. */
1590
1591 #define VECSIZE(type) \
1592 ((sizeof (type) - header_size + word_size - 1) / word_size)
1593
1594 /* Like VECSIZE, but used when the pseudo-vector has non-Lisp_Object fields
1595 at the end and we need to compute the number of Lisp_Object fields (the
1596 ones that the GC needs to trace). */
1597
1598 #define PSEUDOVECSIZE(type, nonlispfield) \
1599 ((offsetof (type, nonlispfield) - header_size) / word_size)
1600
1601 /* Compute A OP B, using the unsigned comparison operator OP. A and B
1602 should be integer expressions. This is not the same as
1603 mathematical comparison; for example, UNSIGNED_CMP (0, <, -1)
1604 returns true. For efficiency, prefer plain unsigned comparison if A
1605 and B's sizes both fit (after integer promotion). */
1606 #define UNSIGNED_CMP(a, op, b) \
1607 (max (sizeof ((a) + 0), sizeof ((b) + 0)) <= sizeof (unsigned) \
1608 ? ((a) + (unsigned) 0) op ((b) + (unsigned) 0) \
1609 : ((a) + (uintmax_t) 0) op ((b) + (uintmax_t) 0))
1610
1611 /* True iff C is an ASCII character. */
1612 #define ASCII_CHAR_P(c) UNSIGNED_CMP (c, <, 0x80)
1613
1614 /* A char-table is a kind of vectorlike, with contents are like a
1615 vector but with a few other slots. For some purposes, it makes
1616 sense to handle a char-table with type struct Lisp_Vector. An
1617 element of a char table can be any Lisp objects, but if it is a sub
1618 char-table, we treat it a table that contains information of a
1619 specific range of characters. A sub char-table is like a vector but
1620 with two integer fields between the header and Lisp data, which means
1621 that it has to be marked with some precautions (see mark_char_table
1622 in alloc.c). A sub char-table appears only in an element of a char-table,
1623 and there's no way to access it directly from Emacs Lisp program. */
1624
1625 enum CHARTAB_SIZE_BITS
1626 {
1627 CHARTAB_SIZE_BITS_0 = 6,
1628 CHARTAB_SIZE_BITS_1 = 4,
1629 CHARTAB_SIZE_BITS_2 = 5,
1630 CHARTAB_SIZE_BITS_3 = 7
1631 };
1632
1633 extern const int chartab_size[4];
1634
1635 struct Lisp_Char_Table
1636 {
1637 /* HEADER.SIZE is the vector's size field, which also holds the
1638 pseudovector type information. It holds the size, too.
1639 The size counts the defalt, parent, purpose, ascii,
1640 contents, and extras slots. */
1641 struct vectorlike_header header;
1642
1643 /* This holds a default value,
1644 which is used whenever the value for a specific character is nil. */
1645 Lisp_Object defalt;
1646
1647 /* This points to another char table, which we inherit from when the
1648 value for a specific character is nil. The `defalt' slot takes
1649 precedence over this. */
1650 Lisp_Object parent;
1651
1652 /* This is a symbol which says what kind of use this char-table is
1653 meant for. */
1654 Lisp_Object purpose;
1655
1656 /* The bottom sub char-table for characters of the range 0..127. It
1657 is nil if none of ASCII character has a specific value. */
1658 Lisp_Object ascii;
1659
1660 Lisp_Object contents[(1 << CHARTAB_SIZE_BITS_0)];
1661
1662 /* These hold additional data. It is a vector. */
1663 Lisp_Object extras[FLEXIBLE_ARRAY_MEMBER];
1664 };
1665
1666 struct Lisp_Sub_Char_Table
1667 {
1668 /* HEADER.SIZE is the vector's size field, which also holds the
1669 pseudovector type information. It holds the size, too. */
1670 struct vectorlike_header header;
1671
1672 /* Depth of this sub char-table. It should be 1, 2, or 3. A sub
1673 char-table of depth 1 contains 16 elements, and each element
1674 covers 4096 (128*32) characters. A sub char-table of depth 2
1675 contains 32 elements, and each element covers 128 characters. A
1676 sub char-table of depth 3 contains 128 elements, and each element
1677 is for one character. */
1678 int depth;
1679
1680 /* Minimum character covered by the sub char-table. */
1681 int min_char;
1682
1683 /* Use set_sub_char_table_contents to set this. */
1684 Lisp_Object contents[FLEXIBLE_ARRAY_MEMBER];
1685 };
1686
1687 INLINE Lisp_Object
1688 CHAR_TABLE_REF_ASCII (Lisp_Object ct, ptrdiff_t idx)
1689 {
1690 struct Lisp_Char_Table *tbl = NULL;
1691 Lisp_Object val;
1692 do
1693 {
1694 tbl = tbl ? XCHAR_TABLE (tbl->parent) : XCHAR_TABLE (ct);
1695 val = (! SUB_CHAR_TABLE_P (tbl->ascii) ? tbl->ascii
1696 : XSUB_CHAR_TABLE (tbl->ascii)->contents[idx]);
1697 if (NILP (val))
1698 val = tbl->defalt;
1699 }
1700 while (NILP (val) && ! NILP (tbl->parent));
1701
1702 return val;
1703 }
1704
1705 /* Almost equivalent to Faref (CT, IDX) with optimization for ASCII
1706 characters. Do not check validity of CT. */
1707 INLINE Lisp_Object
1708 CHAR_TABLE_REF (Lisp_Object ct, int idx)
1709 {
1710 return (ASCII_CHAR_P (idx)
1711 ? CHAR_TABLE_REF_ASCII (ct, idx)
1712 : char_table_ref (ct, idx));
1713 }
1714
1715 /* Equivalent to Faset (CT, IDX, VAL) with optimization for ASCII and
1716 8-bit European characters. Do not check validity of CT. */
1717 INLINE void
1718 CHAR_TABLE_SET (Lisp_Object ct, int idx, Lisp_Object val)
1719 {
1720 if (ASCII_CHAR_P (idx) && SUB_CHAR_TABLE_P (XCHAR_TABLE (ct)->ascii))
1721 set_sub_char_table_contents (XCHAR_TABLE (ct)->ascii, idx, val);
1722 else
1723 char_table_set (ct, idx, val);
1724 }
1725
1726 /* This structure describes a built-in function.
1727 It is generated by the DEFUN macro only.
1728 defsubr makes it into a Lisp object. */
1729
1730 struct Lisp_Subr
1731 {
1732 struct vectorlike_header header;
1733 union {
1734 Lisp_Object (*a0) (void);
1735 Lisp_Object (*a1) (Lisp_Object);
1736 Lisp_Object (*a2) (Lisp_Object, Lisp_Object);
1737 Lisp_Object (*a3) (Lisp_Object, Lisp_Object, Lisp_Object);
1738 Lisp_Object (*a4) (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object);
1739 Lisp_Object (*a5) (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object);
1740 Lisp_Object (*a6) (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object);
1741 Lisp_Object (*a7) (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object);
1742 Lisp_Object (*a8) (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object);
1743 Lisp_Object (*aUNEVALLED) (Lisp_Object args);
1744 Lisp_Object (*aMANY) (ptrdiff_t, Lisp_Object *);
1745 } function;
1746 short min_args, max_args;
1747 const char *symbol_name;
1748 const char *intspec;
1749 const char *doc;
1750 };
1751
1752 enum char_table_specials
1753 {
1754 /* This is the number of slots that every char table must have. This
1755 counts the ordinary slots and the top, defalt, parent, and purpose
1756 slots. */
1757 CHAR_TABLE_STANDARD_SLOTS = PSEUDOVECSIZE (struct Lisp_Char_Table, extras),
1758
1759 /* This is an index of first Lisp_Object field in Lisp_Sub_Char_Table
1760 when the latter is treated as an ordinary Lisp_Vector. */
1761 SUB_CHAR_TABLE_OFFSET = PSEUDOVECSIZE (struct Lisp_Sub_Char_Table, contents)
1762 };
1763
1764 /* Return the number of "extra" slots in the char table CT. */
1765
1766 INLINE int
1767 CHAR_TABLE_EXTRA_SLOTS (struct Lisp_Char_Table *ct)
1768 {
1769 return ((ct->header.size & PSEUDOVECTOR_SIZE_MASK)
1770 - CHAR_TABLE_STANDARD_SLOTS);
1771 }
1772
1773 /* Make sure that sub char-table contents slot is where we think it is. */
1774 verify (offsetof (struct Lisp_Sub_Char_Table, contents)
1775 == (offsetof (struct Lisp_Vector, contents)
1776 + SUB_CHAR_TABLE_OFFSET * sizeof (Lisp_Object)));
1777
1778 /***********************************************************************
1779 Symbols
1780 ***********************************************************************/
1781
1782 /* Value is name of symbol. */
1783
1784 INLINE Lisp_Object
1785 (SYMBOL_VAL) (struct Lisp_Symbol *sym)
1786 {
1787 return lisp_h_SYMBOL_VAL (sym);
1788 }
1789
1790 INLINE struct Lisp_Symbol *
1791 SYMBOL_ALIAS (struct Lisp_Symbol *sym)
1792 {
1793 eassert (sym->redirect == SYMBOL_VARALIAS);
1794 return sym->val.alias;
1795 }
1796 INLINE struct Lisp_Buffer_Local_Value *
1797 SYMBOL_BLV (struct Lisp_Symbol *sym)
1798 {
1799 eassert (sym->redirect == SYMBOL_LOCALIZED);
1800 return sym->val.blv;
1801 }
1802 INLINE union Lisp_Fwd *
1803 SYMBOL_FWD (struct Lisp_Symbol *sym)
1804 {
1805 eassert (sym->redirect == SYMBOL_FORWARDED);
1806 return sym->val.fwd;
1807 }
1808
1809 INLINE void
1810 (SET_SYMBOL_VAL) (struct Lisp_Symbol *sym, Lisp_Object v)
1811 {
1812 lisp_h_SET_SYMBOL_VAL (sym, v);
1813 }
1814
1815 INLINE void
1816 SET_SYMBOL_ALIAS (struct Lisp_Symbol *sym, struct Lisp_Symbol *v)
1817 {
1818 eassert (sym->redirect == SYMBOL_VARALIAS);
1819 sym->val.alias = v;
1820 }
1821 INLINE void
1822 SET_SYMBOL_BLV (struct Lisp_Symbol *sym, struct Lisp_Buffer_Local_Value *v)
1823 {
1824 eassert (sym->redirect == SYMBOL_LOCALIZED);
1825 sym->val.blv = v;
1826 }
1827 INLINE void
1828 SET_SYMBOL_FWD (struct Lisp_Symbol *sym, union Lisp_Fwd *v)
1829 {
1830 eassert (sym->redirect == SYMBOL_FORWARDED);
1831 sym->val.fwd = v;
1832 }
1833
1834 INLINE Lisp_Object
1835 SYMBOL_NAME (Lisp_Object sym)
1836 {
1837 return XSYMBOL (sym)->name;
1838 }
1839
1840 /* Value is true if SYM is an interned symbol. */
1841
1842 INLINE bool
1843 SYMBOL_INTERNED_P (Lisp_Object sym)
1844 {
1845 return XSYMBOL (sym)->interned != SYMBOL_UNINTERNED;
1846 }
1847
1848 /* Value is true if SYM is interned in initial_obarray. */
1849
1850 INLINE bool
1851 SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (Lisp_Object sym)
1852 {
1853 return XSYMBOL (sym)->interned == SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
1854 }
1855
1856 /* Value is non-zero if symbol is considered a constant, i.e. its
1857 value cannot be changed (there is an exception for keyword symbols,
1858 whose value can be set to the keyword symbol itself). */
1859
1860 INLINE int
1861 (SYMBOL_CONSTANT_P) (Lisp_Object sym)
1862 {
1863 return lisp_h_SYMBOL_CONSTANT_P (sym);
1864 }
1865
1866 /* Placeholder for make-docfile to process. The actual symbol
1867 definition is done by lread.c's defsym. */
1868 #define DEFSYM(sym, name) /* empty */
1869
1870 \f
1871 /***********************************************************************
1872 Hash Tables
1873 ***********************************************************************/
1874
1875 /* The structure of a Lisp hash table. */
1876
1877 struct hash_table_test
1878 {
1879 /* Name of the function used to compare keys. */
1880 Lisp_Object name;
1881
1882 /* User-supplied hash function, or nil. */
1883 Lisp_Object user_hash_function;
1884
1885 /* User-supplied key comparison function, or nil. */
1886 Lisp_Object user_cmp_function;
1887
1888 /* C function to compare two keys. */
1889 bool (*cmpfn) (struct hash_table_test *t, Lisp_Object, Lisp_Object);
1890
1891 /* C function to compute hash code. */
1892 EMACS_UINT (*hashfn) (struct hash_table_test *t, Lisp_Object);
1893 };
1894
1895 struct Lisp_Hash_Table
1896 {
1897 /* This is for Lisp; the hash table code does not refer to it. */
1898 struct vectorlike_header header;
1899
1900 /* Nil if table is non-weak. Otherwise a symbol describing the
1901 weakness of the table. */
1902 Lisp_Object weak;
1903
1904 /* When the table is resized, and this is an integer, compute the
1905 new size by adding this to the old size. If a float, compute the
1906 new size by multiplying the old size with this factor. */
1907 Lisp_Object rehash_size;
1908
1909 /* Resize hash table when number of entries/ table size is >= this
1910 ratio, a float. */
1911 Lisp_Object rehash_threshold;
1912
1913 /* Vector of hash codes. If hash[I] is nil, this means that the
1914 I-th entry is unused. */
1915 Lisp_Object hash;
1916
1917 /* Vector used to chain entries. If entry I is free, next[I] is the
1918 entry number of the next free item. If entry I is non-free,
1919 next[I] is the index of the next entry in the collision chain. */
1920 Lisp_Object next;
1921
1922 /* Index of first free entry in free list. */
1923 Lisp_Object next_free;
1924
1925 /* Bucket vector. A non-nil entry is the index of the first item in
1926 a collision chain. This vector's size can be larger than the
1927 hash table size to reduce collisions. */
1928 Lisp_Object index;
1929
1930 /* Only the fields above are traced normally by the GC. The ones below
1931 `count' are special and are either ignored by the GC or traced in
1932 a special way (e.g. because of weakness). */
1933
1934 /* Number of key/value entries in the table. */
1935 ptrdiff_t count;
1936
1937 /* Vector of keys and values. The key of item I is found at index
1938 2 * I, the value is found at index 2 * I + 1.
1939 This is gc_marked specially if the table is weak. */
1940 Lisp_Object key_and_value;
1941
1942 /* The comparison and hash functions. */
1943 struct hash_table_test test;
1944
1945 /* Next weak hash table if this is a weak hash table. The head
1946 of the list is in weak_hash_tables. */
1947 struct Lisp_Hash_Table *next_weak;
1948 };
1949
1950
1951 INLINE bool
1952 HASH_TABLE_P (Lisp_Object a)
1953 {
1954 return PSEUDOVECTORP (a, PVEC_HASH_TABLE);
1955 }
1956
1957 INLINE struct Lisp_Hash_Table *
1958 XHASH_TABLE (Lisp_Object a)
1959 {
1960 eassert (HASH_TABLE_P (a));
1961 return XUNTAG (a, Lisp_Vectorlike);
1962 }
1963
1964 #define XSET_HASH_TABLE(VAR, PTR) \
1965 (XSETPSEUDOVECTOR (VAR, PTR, PVEC_HASH_TABLE))
1966
1967 /* Value is the key part of entry IDX in hash table H. */
1968 INLINE Lisp_Object
1969 HASH_KEY (struct Lisp_Hash_Table *h, ptrdiff_t idx)
1970 {
1971 return AREF (h->key_and_value, 2 * idx);
1972 }
1973
1974 /* Value is the value part of entry IDX in hash table H. */
1975 INLINE Lisp_Object
1976 HASH_VALUE (struct Lisp_Hash_Table *h, ptrdiff_t idx)
1977 {
1978 return AREF (h->key_and_value, 2 * idx + 1);
1979 }
1980
1981 /* Value is the index of the next entry following the one at IDX
1982 in hash table H. */
1983 INLINE Lisp_Object
1984 HASH_NEXT (struct Lisp_Hash_Table *h, ptrdiff_t idx)
1985 {
1986 return AREF (h->next, idx);
1987 }
1988
1989 /* Value is the hash code computed for entry IDX in hash table H. */
1990 INLINE Lisp_Object
1991 HASH_HASH (struct Lisp_Hash_Table *h, ptrdiff_t idx)
1992 {
1993 return AREF (h->hash, idx);
1994 }
1995
1996 /* Value is the index of the element in hash table H that is the
1997 start of the collision list at index IDX in the index vector of H. */
1998 INLINE Lisp_Object
1999 HASH_INDEX (struct Lisp_Hash_Table *h, ptrdiff_t idx)
2000 {
2001 return AREF (h->index, idx);
2002 }
2003
2004 /* Value is the size of hash table H. */
2005 INLINE ptrdiff_t
2006 HASH_TABLE_SIZE (struct Lisp_Hash_Table *h)
2007 {
2008 return ASIZE (h->next);
2009 }
2010
2011 /* Default size for hash tables if not specified. */
2012
2013 enum DEFAULT_HASH_SIZE { DEFAULT_HASH_SIZE = 65 };
2014
2015 /* Default threshold specifying when to resize a hash table. The
2016 value gives the ratio of current entries in the hash table and the
2017 size of the hash table. */
2018
2019 static double const DEFAULT_REHASH_THRESHOLD = 0.8;
2020
2021 /* Default factor by which to increase the size of a hash table. */
2022
2023 static double const DEFAULT_REHASH_SIZE = 1.5;
2024
2025 /* Combine two integers X and Y for hashing. The result might not fit
2026 into a Lisp integer. */
2027
2028 INLINE EMACS_UINT
2029 sxhash_combine (EMACS_UINT x, EMACS_UINT y)
2030 {
2031 return (x << 4) + (x >> (BITS_PER_EMACS_INT - 4)) + y;
2032 }
2033
2034 /* Hash X, returning a value that fits into a fixnum. */
2035
2036 INLINE EMACS_UINT
2037 SXHASH_REDUCE (EMACS_UINT x)
2038 {
2039 return (x ^ x >> (BITS_PER_EMACS_INT - FIXNUM_BITS)) & INTMASK;
2040 }
2041
2042 /* These structures are used for various misc types. */
2043
2044 struct Lisp_Misc_Any /* Supertype of all Misc types. */
2045 {
2046 ENUM_BF (Lisp_Misc_Type) type : 16; /* = Lisp_Misc_??? */
2047 bool_bf gcmarkbit : 1;
2048 unsigned spacer : 15;
2049 };
2050
2051 struct Lisp_Marker
2052 {
2053 ENUM_BF (Lisp_Misc_Type) type : 16; /* = Lisp_Misc_Marker */
2054 bool_bf gcmarkbit : 1;
2055 unsigned spacer : 13;
2056 /* This flag is temporarily used in the functions
2057 decode/encode_coding_object to record that the marker position
2058 must be adjusted after the conversion. */
2059 bool_bf need_adjustment : 1;
2060 /* True means normal insertion at the marker's position
2061 leaves the marker after the inserted text. */
2062 bool_bf insertion_type : 1;
2063 /* This is the buffer that the marker points into, or 0 if it points nowhere.
2064 Note: a chain of markers can contain markers pointing into different
2065 buffers (the chain is per buffer_text rather than per buffer, so it's
2066 shared between indirect buffers). */
2067 /* This is used for (other than NULL-checking):
2068 - Fmarker_buffer
2069 - Fset_marker: check eq(oldbuf, newbuf) to avoid unchain+rechain.
2070 - unchain_marker: to find the list from which to unchain.
2071 - Fkill_buffer: to only unchain the markers of current indirect buffer.
2072 */
2073 struct buffer *buffer;
2074
2075 /* The remaining fields are meaningless in a marker that
2076 does not point anywhere. */
2077
2078 /* For markers that point somewhere,
2079 this is used to chain of all the markers in a given buffer. */
2080 /* We could remove it and use an array in buffer_text instead.
2081 That would also allow us to preserve it ordered. */
2082 struct Lisp_Marker *next;
2083 /* This is the char position where the marker points. */
2084 ptrdiff_t charpos;
2085 /* This is the byte position.
2086 It's mostly used as a charpos<->bytepos cache (i.e. it's not directly
2087 used to implement the functionality of markers, but rather to (ab)use
2088 markers as a cache for char<->byte mappings). */
2089 ptrdiff_t bytepos;
2090 };
2091
2092 /* START and END are markers in the overlay's buffer, and
2093 PLIST is the overlay's property list. */
2094 struct Lisp_Overlay
2095 /* An overlay's real data content is:
2096 - plist
2097 - buffer (really there are two buffer pointers, one per marker,
2098 and both points to the same buffer)
2099 - insertion type of both ends (per-marker fields)
2100 - start & start byte (of start marker)
2101 - end & end byte (of end marker)
2102 - next (singly linked list of overlays)
2103 - next fields of start and end markers (singly linked list of markers).
2104 I.e. 9words plus 2 bits, 3words of which are for external linked lists.
2105 */
2106 {
2107 ENUM_BF (Lisp_Misc_Type) type : 16; /* = Lisp_Misc_Overlay */
2108 bool_bf gcmarkbit : 1;
2109 unsigned spacer : 15;
2110 struct Lisp_Overlay *next;
2111 Lisp_Object start;
2112 Lisp_Object end;
2113 Lisp_Object plist;
2114 };
2115
2116 /* Types of data which may be saved in a Lisp_Save_Value. */
2117
2118 enum
2119 {
2120 SAVE_UNUSED,
2121 SAVE_INTEGER,
2122 SAVE_FUNCPOINTER,
2123 SAVE_POINTER,
2124 SAVE_OBJECT
2125 };
2126
2127 /* Number of bits needed to store one of the above values. */
2128 enum { SAVE_SLOT_BITS = 3 };
2129
2130 /* Number of slots in a save value where save_type is nonzero. */
2131 enum { SAVE_VALUE_SLOTS = 4 };
2132
2133 /* Bit-width and values for struct Lisp_Save_Value's save_type member. */
2134
2135 enum { SAVE_TYPE_BITS = SAVE_VALUE_SLOTS * SAVE_SLOT_BITS + 1 };
2136
2137 enum Lisp_Save_Type
2138 {
2139 SAVE_TYPE_INT_INT = SAVE_INTEGER + (SAVE_INTEGER << SAVE_SLOT_BITS),
2140 SAVE_TYPE_INT_INT_INT
2141 = (SAVE_INTEGER + (SAVE_TYPE_INT_INT << SAVE_SLOT_BITS)),
2142 SAVE_TYPE_OBJ_OBJ = SAVE_OBJECT + (SAVE_OBJECT << SAVE_SLOT_BITS),
2143 SAVE_TYPE_OBJ_OBJ_OBJ = SAVE_OBJECT + (SAVE_TYPE_OBJ_OBJ << SAVE_SLOT_BITS),
2144 SAVE_TYPE_OBJ_OBJ_OBJ_OBJ
2145 = SAVE_OBJECT + (SAVE_TYPE_OBJ_OBJ_OBJ << SAVE_SLOT_BITS),
2146 SAVE_TYPE_PTR_INT = SAVE_POINTER + (SAVE_INTEGER << SAVE_SLOT_BITS),
2147 SAVE_TYPE_PTR_OBJ = SAVE_POINTER + (SAVE_OBJECT << SAVE_SLOT_BITS),
2148 SAVE_TYPE_PTR_PTR = SAVE_POINTER + (SAVE_POINTER << SAVE_SLOT_BITS),
2149 SAVE_TYPE_FUNCPTR_PTR_OBJ
2150 = SAVE_FUNCPOINTER + (SAVE_TYPE_PTR_OBJ << SAVE_SLOT_BITS),
2151
2152 /* This has an extra bit indicating it's raw memory. */
2153 SAVE_TYPE_MEMORY = SAVE_TYPE_PTR_INT + (1 << (SAVE_TYPE_BITS - 1))
2154 };
2155
2156 /* Special object used to hold a different values for later use.
2157
2158 This is mostly used to package C integers and pointers to call
2159 record_unwind_protect when two or more values need to be saved.
2160 For example:
2161
2162 ...
2163 struct my_data *md = get_my_data ();
2164 ptrdiff_t mi = get_my_integer ();
2165 record_unwind_protect (my_unwind, make_save_ptr_int (md, mi));
2166 ...
2167
2168 Lisp_Object my_unwind (Lisp_Object arg)
2169 {
2170 struct my_data *md = XSAVE_POINTER (arg, 0);
2171 ptrdiff_t mi = XSAVE_INTEGER (arg, 1);
2172 ...
2173 }
2174
2175 If ENABLE_CHECKING is in effect, XSAVE_xxx macros do type checking of the
2176 saved objects and raise eassert if type of the saved object doesn't match
2177 the type which is extracted. In the example above, XSAVE_INTEGER (arg, 2)
2178 and XSAVE_OBJECT (arg, 0) are wrong because nothing was saved in slot 2 and
2179 slot 0 is a pointer. */
2180
2181 typedef void (*voidfuncptr) (void);
2182
2183 struct Lisp_Save_Value
2184 {
2185 ENUM_BF (Lisp_Misc_Type) type : 16; /* = Lisp_Misc_Save_Value */
2186 bool_bf gcmarkbit : 1;
2187 unsigned spacer : 32 - (16 + 1 + SAVE_TYPE_BITS);
2188
2189 /* V->data may hold up to SAVE_VALUE_SLOTS entries. The type of
2190 V's data entries are determined by V->save_type. E.g., if
2191 V->save_type == SAVE_TYPE_PTR_OBJ, V->data[0] is a pointer,
2192 V->data[1] is an integer, and V's other data entries are unused.
2193
2194 If V->save_type == SAVE_TYPE_MEMORY, V->data[0].pointer is the address of
2195 a memory area containing V->data[1].integer potential Lisp_Objects. */
2196 ENUM_BF (Lisp_Save_Type) save_type : SAVE_TYPE_BITS;
2197 union {
2198 void *pointer;
2199 voidfuncptr funcpointer;
2200 ptrdiff_t integer;
2201 Lisp_Object object;
2202 } data[SAVE_VALUE_SLOTS];
2203 };
2204
2205 /* Return the type of V's Nth saved value. */
2206 INLINE int
2207 save_type (struct Lisp_Save_Value *v, int n)
2208 {
2209 eassert (0 <= n && n < SAVE_VALUE_SLOTS);
2210 return (v->save_type >> (SAVE_SLOT_BITS * n) & ((1 << SAVE_SLOT_BITS) - 1));
2211 }
2212
2213 /* Get and set the Nth saved pointer. */
2214
2215 INLINE void *
2216 XSAVE_POINTER (Lisp_Object obj, int n)
2217 {
2218 eassert (save_type (XSAVE_VALUE (obj), n) == SAVE_POINTER);
2219 return XSAVE_VALUE (obj)->data[n].pointer;
2220 }
2221 INLINE void
2222 set_save_pointer (Lisp_Object obj, int n, void *val)
2223 {
2224 eassert (save_type (XSAVE_VALUE (obj), n) == SAVE_POINTER);
2225 XSAVE_VALUE (obj)->data[n].pointer = val;
2226 }
2227 INLINE voidfuncptr
2228 XSAVE_FUNCPOINTER (Lisp_Object obj, int n)
2229 {
2230 eassert (save_type (XSAVE_VALUE (obj), n) == SAVE_FUNCPOINTER);
2231 return XSAVE_VALUE (obj)->data[n].funcpointer;
2232 }
2233
2234 /* Likewise for the saved integer. */
2235
2236 INLINE ptrdiff_t
2237 XSAVE_INTEGER (Lisp_Object obj, int n)
2238 {
2239 eassert (save_type (XSAVE_VALUE (obj), n) == SAVE_INTEGER);
2240 return XSAVE_VALUE (obj)->data[n].integer;
2241 }
2242 INLINE void
2243 set_save_integer (Lisp_Object obj, int n, ptrdiff_t val)
2244 {
2245 eassert (save_type (XSAVE_VALUE (obj), n) == SAVE_INTEGER);
2246 XSAVE_VALUE (obj)->data[n].integer = val;
2247 }
2248
2249 /* Extract Nth saved object. */
2250
2251 INLINE Lisp_Object
2252 XSAVE_OBJECT (Lisp_Object obj, int n)
2253 {
2254 eassert (save_type (XSAVE_VALUE (obj), n) == SAVE_OBJECT);
2255 return XSAVE_VALUE (obj)->data[n].object;
2256 }
2257
2258 #ifdef HAVE_MODULES
2259 struct Lisp_User_Ptr
2260 {
2261 ENUM_BF (Lisp_Misc_Type) type : 16; /* = Lisp_Misc_User_Ptr */
2262 bool_bf gcmarkbit : 1;
2263 unsigned spacer : 15;
2264
2265 void (*finalizer) (void *);
2266 void *p;
2267 };
2268 #endif
2269
2270 /* A finalizer sentinel. */
2271 struct Lisp_Finalizer
2272 {
2273 struct Lisp_Misc_Any base;
2274
2275 /* Circular list of all active weak references. */
2276 struct Lisp_Finalizer *prev;
2277 struct Lisp_Finalizer *next;
2278
2279 /* Call FUNCTION when the finalizer becomes unreachable, even if
2280 FUNCTION contains a reference to the finalizer; i.e., call
2281 FUNCTION when it is reachable _only_ through finalizers. */
2282 Lisp_Object function;
2283 };
2284
2285 /* A miscellaneous object, when it's on the free list. */
2286 struct Lisp_Free
2287 {
2288 ENUM_BF (Lisp_Misc_Type) type : 16; /* = Lisp_Misc_Free */
2289 bool_bf gcmarkbit : 1;
2290 unsigned spacer : 15;
2291 union Lisp_Misc *chain;
2292 };
2293
2294 /* To get the type field of a union Lisp_Misc, use XMISCTYPE.
2295 It uses one of these struct subtypes to get the type field. */
2296
2297 union Lisp_Misc
2298 {
2299 struct Lisp_Misc_Any u_any; /* Supertype of all Misc types. */
2300 struct Lisp_Free u_free;
2301 struct Lisp_Marker u_marker;
2302 struct Lisp_Overlay u_overlay;
2303 struct Lisp_Save_Value u_save_value;
2304 struct Lisp_Finalizer u_finalizer;
2305 #ifdef HAVE_MODULES
2306 struct Lisp_User_Ptr u_user_ptr;
2307 #endif
2308 };
2309
2310 INLINE union Lisp_Misc *
2311 XMISC (Lisp_Object a)
2312 {
2313 return XUNTAG (a, Lisp_Misc);
2314 }
2315
2316 INLINE struct Lisp_Misc_Any *
2317 XMISCANY (Lisp_Object a)
2318 {
2319 eassert (MISCP (a));
2320 return & XMISC (a)->u_any;
2321 }
2322
2323 INLINE enum Lisp_Misc_Type
2324 XMISCTYPE (Lisp_Object a)
2325 {
2326 return XMISCANY (a)->type;
2327 }
2328
2329 INLINE struct Lisp_Marker *
2330 XMARKER (Lisp_Object a)
2331 {
2332 eassert (MARKERP (a));
2333 return & XMISC (a)->u_marker;
2334 }
2335
2336 INLINE struct Lisp_Overlay *
2337 XOVERLAY (Lisp_Object a)
2338 {
2339 eassert (OVERLAYP (a));
2340 return & XMISC (a)->u_overlay;
2341 }
2342
2343 INLINE struct Lisp_Save_Value *
2344 XSAVE_VALUE (Lisp_Object a)
2345 {
2346 eassert (SAVE_VALUEP (a));
2347 return & XMISC (a)->u_save_value;
2348 }
2349
2350 INLINE struct Lisp_Finalizer *
2351 XFINALIZER (Lisp_Object a)
2352 {
2353 eassert (FINALIZERP (a));
2354 return & XMISC (a)->u_finalizer;
2355 }
2356
2357 #ifdef HAVE_MODULES
2358 INLINE struct Lisp_User_Ptr *
2359 XUSER_PTR (Lisp_Object a)
2360 {
2361 eassert (USER_PTRP (a));
2362 return & XMISC (a)->u_user_ptr;
2363 }
2364 #endif
2365
2366 \f
2367 /* Forwarding pointer to an int variable.
2368 This is allowed only in the value cell of a symbol,
2369 and it means that the symbol's value really lives in the
2370 specified int variable. */
2371 struct Lisp_Intfwd
2372 {
2373 enum Lisp_Fwd_Type type; /* = Lisp_Fwd_Int */
2374 EMACS_INT *intvar;
2375 };
2376
2377 /* Boolean forwarding pointer to an int variable.
2378 This is like Lisp_Intfwd except that the ostensible
2379 "value" of the symbol is t if the bool variable is true,
2380 nil if it is false. */
2381 struct Lisp_Boolfwd
2382 {
2383 enum Lisp_Fwd_Type type; /* = Lisp_Fwd_Bool */
2384 bool *boolvar;
2385 };
2386
2387 /* Forwarding pointer to a Lisp_Object variable.
2388 This is allowed only in the value cell of a symbol,
2389 and it means that the symbol's value really lives in the
2390 specified variable. */
2391 struct Lisp_Objfwd
2392 {
2393 enum Lisp_Fwd_Type type; /* = Lisp_Fwd_Obj */
2394 Lisp_Object *objvar;
2395 };
2396
2397 /* Like Lisp_Objfwd except that value lives in a slot in the
2398 current buffer. Value is byte index of slot within buffer. */
2399 struct Lisp_Buffer_Objfwd
2400 {
2401 enum Lisp_Fwd_Type type; /* = Lisp_Fwd_Buffer_Obj */
2402 int offset;
2403 /* One of Qnil, Qintegerp, Qsymbolp, Qstringp, Qfloatp or Qnumberp. */
2404 Lisp_Object predicate;
2405 };
2406
2407 /* struct Lisp_Buffer_Local_Value is used in a symbol value cell when
2408 the symbol has buffer-local or frame-local bindings. (Exception:
2409 some buffer-local variables are built-in, with their values stored
2410 in the buffer structure itself. They are handled differently,
2411 using struct Lisp_Buffer_Objfwd.)
2412
2413 The `realvalue' slot holds the variable's current value, or a
2414 forwarding pointer to where that value is kept. This value is the
2415 one that corresponds to the loaded binding. To read or set the
2416 variable, you must first make sure the right binding is loaded;
2417 then you can access the value in (or through) `realvalue'.
2418
2419 `buffer' and `frame' are the buffer and frame for which the loaded
2420 binding was found. If those have changed, to make sure the right
2421 binding is loaded it is necessary to find which binding goes with
2422 the current buffer and selected frame, then load it. To load it,
2423 first unload the previous binding, then copy the value of the new
2424 binding into `realvalue' (or through it). Also update
2425 LOADED-BINDING to point to the newly loaded binding.
2426
2427 `local_if_set' indicates that merely setting the variable creates a
2428 local binding for the current buffer. Otherwise the latter, setting
2429 the variable does not do that; only make-local-variable does that. */
2430
2431 struct Lisp_Buffer_Local_Value
2432 {
2433 /* True means that merely setting the variable creates a local
2434 binding for the current buffer. */
2435 bool_bf local_if_set : 1;
2436 /* True means this variable can have frame-local bindings, otherwise, it is
2437 can have buffer-local bindings. The two cannot be combined. */
2438 bool_bf frame_local : 1;
2439 /* True means that the binding now loaded was found.
2440 Presumably equivalent to (defcell!=valcell). */
2441 bool_bf found : 1;
2442 /* If non-NULL, a forwarding to the C var where it should also be set. */
2443 union Lisp_Fwd *fwd; /* Should never be (Buffer|Kboard)_Objfwd. */
2444 /* The buffer or frame for which the loaded binding was found. */
2445 Lisp_Object where;
2446 /* A cons cell that holds the default value. It has the form
2447 (SYMBOL . DEFAULT-VALUE). */
2448 Lisp_Object defcell;
2449 /* The cons cell from `where's parameter alist.
2450 It always has the form (SYMBOL . VALUE)
2451 Note that if `forward' is non-nil, VALUE may be out of date.
2452 Also if the currently loaded binding is the default binding, then
2453 this is `eq'ual to defcell. */
2454 Lisp_Object valcell;
2455 };
2456
2457 /* Like Lisp_Objfwd except that value lives in a slot in the
2458 current kboard. */
2459 struct Lisp_Kboard_Objfwd
2460 {
2461 enum Lisp_Fwd_Type type; /* = Lisp_Fwd_Kboard_Obj */
2462 int offset;
2463 };
2464
2465 union Lisp_Fwd
2466 {
2467 struct Lisp_Intfwd u_intfwd;
2468 struct Lisp_Boolfwd u_boolfwd;
2469 struct Lisp_Objfwd u_objfwd;
2470 struct Lisp_Buffer_Objfwd u_buffer_objfwd;
2471 struct Lisp_Kboard_Objfwd u_kboard_objfwd;
2472 };
2473
2474 INLINE enum Lisp_Fwd_Type
2475 XFWDTYPE (union Lisp_Fwd *a)
2476 {
2477 return a->u_intfwd.type;
2478 }
2479
2480 INLINE struct Lisp_Buffer_Objfwd *
2481 XBUFFER_OBJFWD (union Lisp_Fwd *a)
2482 {
2483 eassert (BUFFER_OBJFWDP (a));
2484 return &a->u_buffer_objfwd;
2485 }
2486 \f
2487 /* Lisp floating point type. */
2488 struct Lisp_Float
2489 {
2490 union
2491 {
2492 double data;
2493 struct Lisp_Float *chain;
2494 } u;
2495 };
2496
2497 INLINE double
2498 XFLOAT_DATA (Lisp_Object f)
2499 {
2500 return XFLOAT (f)->u.data;
2501 }
2502
2503 /* Most hosts nowadays use IEEE floating point, so they use IEC 60559
2504 representations, have infinities and NaNs, and do not trap on
2505 exceptions. Define IEEE_FLOATING_POINT if this host is one of the
2506 typical ones. The C11 macro __STDC_IEC_559__ is close to what is
2507 wanted here, but is not quite right because Emacs does not require
2508 all the features of C11 Annex F (and does not require C11 at all,
2509 for that matter). */
2510 enum
2511 {
2512 IEEE_FLOATING_POINT
2513 = (FLT_RADIX == 2 && FLT_MANT_DIG == 24
2514 && FLT_MIN_EXP == -125 && FLT_MAX_EXP == 128)
2515 };
2516
2517 /* A character, declared with the following typedef, is a member
2518 of some character set associated with the current buffer. */
2519 #ifndef _UCHAR_T /* Protect against something in ctab.h on AIX. */
2520 #define _UCHAR_T
2521 typedef unsigned char UCHAR;
2522 #endif
2523
2524 /* Meanings of slots in a Lisp_Compiled: */
2525
2526 enum Lisp_Compiled
2527 {
2528 COMPILED_ARGLIST = 0,
2529 COMPILED_BYTECODE = 1,
2530 COMPILED_CONSTANTS = 2,
2531 COMPILED_STACK_DEPTH = 3,
2532 COMPILED_DOC_STRING = 4,
2533 COMPILED_INTERACTIVE = 5
2534 };
2535
2536 /* Flag bits in a character. These also get used in termhooks.h.
2537 Richard Stallman <rms@gnu.ai.mit.edu> thinks that MULE
2538 (MUlti-Lingual Emacs) might need 22 bits for the character value
2539 itself, so we probably shouldn't use any bits lower than 0x0400000. */
2540 enum char_bits
2541 {
2542 CHAR_ALT = 0x0400000,
2543 CHAR_SUPER = 0x0800000,
2544 CHAR_HYPER = 0x1000000,
2545 CHAR_SHIFT = 0x2000000,
2546 CHAR_CTL = 0x4000000,
2547 CHAR_META = 0x8000000,
2548
2549 CHAR_MODIFIER_MASK =
2550 CHAR_ALT | CHAR_SUPER | CHAR_HYPER | CHAR_SHIFT | CHAR_CTL | CHAR_META,
2551
2552 /* Actually, the current Emacs uses 22 bits for the character value
2553 itself. */
2554 CHARACTERBITS = 22
2555 };
2556 \f
2557 /* Data type checking. */
2558
2559 INLINE bool
2560 (NILP) (Lisp_Object x)
2561 {
2562 return lisp_h_NILP (x);
2563 }
2564
2565 INLINE bool
2566 NUMBERP (Lisp_Object x)
2567 {
2568 return INTEGERP (x) || FLOATP (x);
2569 }
2570 INLINE bool
2571 NATNUMP (Lisp_Object x)
2572 {
2573 return INTEGERP (x) && 0 <= XINT (x);
2574 }
2575
2576 INLINE bool
2577 RANGED_INTEGERP (intmax_t lo, Lisp_Object x, intmax_t hi)
2578 {
2579 return INTEGERP (x) && lo <= XINT (x) && XINT (x) <= hi;
2580 }
2581
2582 #define TYPE_RANGED_INTEGERP(type, x) \
2583 (INTEGERP (x) \
2584 && (TYPE_SIGNED (type) ? TYPE_MINIMUM (type) <= XINT (x) : 0 <= XINT (x)) \
2585 && XINT (x) <= TYPE_MAXIMUM (type))
2586
2587 INLINE bool
2588 (CONSP) (Lisp_Object x)
2589 {
2590 return lisp_h_CONSP (x);
2591 }
2592 INLINE bool
2593 (FLOATP) (Lisp_Object x)
2594 {
2595 return lisp_h_FLOATP (x);
2596 }
2597 INLINE bool
2598 (MISCP) (Lisp_Object x)
2599 {
2600 return lisp_h_MISCP (x);
2601 }
2602 INLINE bool
2603 (SYMBOLP) (Lisp_Object x)
2604 {
2605 return lisp_h_SYMBOLP (x);
2606 }
2607 INLINE bool
2608 (INTEGERP) (Lisp_Object x)
2609 {
2610 return lisp_h_INTEGERP (x);
2611 }
2612 INLINE bool
2613 (VECTORLIKEP) (Lisp_Object x)
2614 {
2615 return lisp_h_VECTORLIKEP (x);
2616 }
2617 INLINE bool
2618 (MARKERP) (Lisp_Object x)
2619 {
2620 return lisp_h_MARKERP (x);
2621 }
2622
2623 INLINE bool
2624 STRINGP (Lisp_Object x)
2625 {
2626 return XTYPE (x) == Lisp_String;
2627 }
2628 INLINE bool
2629 VECTORP (Lisp_Object x)
2630 {
2631 return VECTORLIKEP (x) && ! (ASIZE (x) & PSEUDOVECTOR_FLAG);
2632 }
2633 INLINE bool
2634 OVERLAYP (Lisp_Object x)
2635 {
2636 return MISCP (x) && XMISCTYPE (x) == Lisp_Misc_Overlay;
2637 }
2638 INLINE bool
2639 SAVE_VALUEP (Lisp_Object x)
2640 {
2641 return MISCP (x) && XMISCTYPE (x) == Lisp_Misc_Save_Value;
2642 }
2643
2644 INLINE bool
2645 FINALIZERP (Lisp_Object x)
2646 {
2647 return MISCP (x) && XMISCTYPE (x) == Lisp_Misc_Finalizer;
2648 }
2649
2650 #ifdef HAVE_MODULES
2651 INLINE bool
2652 USER_PTRP (Lisp_Object x)
2653 {
2654 return MISCP (x) && XMISCTYPE (x) == Lisp_Misc_User_Ptr;
2655 }
2656 #endif
2657
2658 INLINE bool
2659 AUTOLOADP (Lisp_Object x)
2660 {
2661 return CONSP (x) && EQ (Qautoload, XCAR (x));
2662 }
2663
2664 INLINE bool
2665 BUFFER_OBJFWDP (union Lisp_Fwd *a)
2666 {
2667 return XFWDTYPE (a) == Lisp_Fwd_Buffer_Obj;
2668 }
2669
2670 INLINE bool
2671 PSEUDOVECTOR_TYPEP (struct vectorlike_header *a, int code)
2672 {
2673 return ((a->size & (PSEUDOVECTOR_FLAG | PVEC_TYPE_MASK))
2674 == (PSEUDOVECTOR_FLAG | (code << PSEUDOVECTOR_AREA_BITS)));
2675 }
2676
2677 /* True if A is a pseudovector whose code is CODE. */
2678 INLINE bool
2679 PSEUDOVECTORP (Lisp_Object a, int code)
2680 {
2681 if (! VECTORLIKEP (a))
2682 return false;
2683 else
2684 {
2685 /* Converting to struct vectorlike_header * avoids aliasing issues. */
2686 struct vectorlike_header *h = XUNTAG (a, Lisp_Vectorlike);
2687 return PSEUDOVECTOR_TYPEP (h, code);
2688 }
2689 }
2690
2691
2692 /* Test for specific pseudovector types. */
2693
2694 INLINE bool
2695 WINDOW_CONFIGURATIONP (Lisp_Object a)
2696 {
2697 return PSEUDOVECTORP (a, PVEC_WINDOW_CONFIGURATION);
2698 }
2699
2700 INLINE bool
2701 PROCESSP (Lisp_Object a)
2702 {
2703 return PSEUDOVECTORP (a, PVEC_PROCESS);
2704 }
2705
2706 INLINE bool
2707 WINDOWP (Lisp_Object a)
2708 {
2709 return PSEUDOVECTORP (a, PVEC_WINDOW);
2710 }
2711
2712 INLINE bool
2713 TERMINALP (Lisp_Object a)
2714 {
2715 return PSEUDOVECTORP (a, PVEC_TERMINAL);
2716 }
2717
2718 INLINE bool
2719 SUBRP (Lisp_Object a)
2720 {
2721 return PSEUDOVECTORP (a, PVEC_SUBR);
2722 }
2723
2724 INLINE bool
2725 COMPILEDP (Lisp_Object a)
2726 {
2727 return PSEUDOVECTORP (a, PVEC_COMPILED);
2728 }
2729
2730 INLINE bool
2731 BUFFERP (Lisp_Object a)
2732 {
2733 return PSEUDOVECTORP (a, PVEC_BUFFER);
2734 }
2735
2736 INLINE bool
2737 CHAR_TABLE_P (Lisp_Object a)
2738 {
2739 return PSEUDOVECTORP (a, PVEC_CHAR_TABLE);
2740 }
2741
2742 INLINE bool
2743 SUB_CHAR_TABLE_P (Lisp_Object a)
2744 {
2745 return PSEUDOVECTORP (a, PVEC_SUB_CHAR_TABLE);
2746 }
2747
2748 INLINE bool
2749 BOOL_VECTOR_P (Lisp_Object a)
2750 {
2751 return PSEUDOVECTORP (a, PVEC_BOOL_VECTOR);
2752 }
2753
2754 INLINE bool
2755 FRAMEP (Lisp_Object a)
2756 {
2757 return PSEUDOVECTORP (a, PVEC_FRAME);
2758 }
2759
2760 /* Test for image (image . spec) */
2761 INLINE bool
2762 IMAGEP (Lisp_Object x)
2763 {
2764 return CONSP (x) && EQ (XCAR (x), Qimage);
2765 }
2766
2767 /* Array types. */
2768 INLINE bool
2769 ARRAYP (Lisp_Object x)
2770 {
2771 return VECTORP (x) || STRINGP (x) || CHAR_TABLE_P (x) || BOOL_VECTOR_P (x);
2772 }
2773 \f
2774 INLINE void
2775 CHECK_LIST (Lisp_Object x)
2776 {
2777 CHECK_TYPE (CONSP (x) || NILP (x), Qlistp, x);
2778 }
2779
2780 INLINE void
2781 (CHECK_LIST_CONS) (Lisp_Object x, Lisp_Object y)
2782 {
2783 lisp_h_CHECK_LIST_CONS (x, y);
2784 }
2785
2786 INLINE void
2787 (CHECK_SYMBOL) (Lisp_Object x)
2788 {
2789 lisp_h_CHECK_SYMBOL (x);
2790 }
2791
2792 INLINE void
2793 (CHECK_NUMBER) (Lisp_Object x)
2794 {
2795 lisp_h_CHECK_NUMBER (x);
2796 }
2797
2798 INLINE void
2799 CHECK_STRING (Lisp_Object x)
2800 {
2801 CHECK_TYPE (STRINGP (x), Qstringp, x);
2802 }
2803 INLINE void
2804 CHECK_STRING_CAR (Lisp_Object x)
2805 {
2806 CHECK_TYPE (STRINGP (XCAR (x)), Qstringp, XCAR (x));
2807 }
2808 INLINE void
2809 CHECK_CONS (Lisp_Object x)
2810 {
2811 CHECK_TYPE (CONSP (x), Qconsp, x);
2812 }
2813 INLINE void
2814 CHECK_VECTOR (Lisp_Object x)
2815 {
2816 CHECK_TYPE (VECTORP (x), Qvectorp, x);
2817 }
2818 INLINE void
2819 CHECK_BOOL_VECTOR (Lisp_Object x)
2820 {
2821 CHECK_TYPE (BOOL_VECTOR_P (x), Qbool_vector_p, x);
2822 }
2823 /* This is a bit special because we always need size afterwards. */
2824 INLINE ptrdiff_t
2825 CHECK_VECTOR_OR_STRING (Lisp_Object x)
2826 {
2827 if (VECTORP (x))
2828 return ASIZE (x);
2829 if (STRINGP (x))
2830 return SCHARS (x);
2831 wrong_type_argument (Qarrayp, x);
2832 }
2833 INLINE void
2834 CHECK_ARRAY (Lisp_Object x, Lisp_Object predicate)
2835 {
2836 CHECK_TYPE (ARRAYP (x), predicate, x);
2837 }
2838 INLINE void
2839 CHECK_BUFFER (Lisp_Object x)
2840 {
2841 CHECK_TYPE (BUFFERP (x), Qbufferp, x);
2842 }
2843 INLINE void
2844 CHECK_WINDOW (Lisp_Object x)
2845 {
2846 CHECK_TYPE (WINDOWP (x), Qwindowp, x);
2847 }
2848 #ifdef subprocesses
2849 INLINE void
2850 CHECK_PROCESS (Lisp_Object x)
2851 {
2852 CHECK_TYPE (PROCESSP (x), Qprocessp, x);
2853 }
2854 #endif
2855 INLINE void
2856 CHECK_NATNUM (Lisp_Object x)
2857 {
2858 CHECK_TYPE (NATNUMP (x), Qwholenump, x);
2859 }
2860
2861 #define CHECK_RANGED_INTEGER(x, lo, hi) \
2862 do { \
2863 CHECK_NUMBER (x); \
2864 if (! ((lo) <= XINT (x) && XINT (x) <= (hi))) \
2865 args_out_of_range_3 \
2866 (x, \
2867 make_number ((lo) < 0 && (lo) < MOST_NEGATIVE_FIXNUM \
2868 ? MOST_NEGATIVE_FIXNUM \
2869 : (lo)), \
2870 make_number (min (hi, MOST_POSITIVE_FIXNUM))); \
2871 } while (false)
2872 #define CHECK_TYPE_RANGED_INTEGER(type, x) \
2873 do { \
2874 if (TYPE_SIGNED (type)) \
2875 CHECK_RANGED_INTEGER (x, TYPE_MINIMUM (type), TYPE_MAXIMUM (type)); \
2876 else \
2877 CHECK_RANGED_INTEGER (x, 0, TYPE_MAXIMUM (type)); \
2878 } while (false)
2879
2880 #define CHECK_NUMBER_COERCE_MARKER(x) \
2881 do { \
2882 if (MARKERP ((x))) \
2883 XSETFASTINT (x, marker_position (x)); \
2884 else \
2885 CHECK_TYPE (INTEGERP (x), Qinteger_or_marker_p, x); \
2886 } while (false)
2887
2888 INLINE double
2889 XFLOATINT (Lisp_Object n)
2890 {
2891 return extract_float (n);
2892 }
2893
2894 INLINE void
2895 CHECK_NUMBER_OR_FLOAT (Lisp_Object x)
2896 {
2897 CHECK_TYPE (NUMBERP (x), Qnumberp, x);
2898 }
2899
2900 #define CHECK_NUMBER_OR_FLOAT_COERCE_MARKER(x) \
2901 do { \
2902 if (MARKERP (x)) \
2903 XSETFASTINT (x, marker_position (x)); \
2904 else \
2905 CHECK_TYPE (NUMBERP (x), Qnumber_or_marker_p, x); \
2906 } while (false)
2907
2908 /* Since we can't assign directly to the CAR or CDR fields of a cons
2909 cell, use these when checking that those fields contain numbers. */
2910 INLINE void
2911 CHECK_NUMBER_CAR (Lisp_Object x)
2912 {
2913 Lisp_Object tmp = XCAR (x);
2914 CHECK_NUMBER (tmp);
2915 XSETCAR (x, tmp);
2916 }
2917
2918 INLINE void
2919 CHECK_NUMBER_CDR (Lisp_Object x)
2920 {
2921 Lisp_Object tmp = XCDR (x);
2922 CHECK_NUMBER (tmp);
2923 XSETCDR (x, tmp);
2924 }
2925 \f
2926 /* Define a built-in function for calling from Lisp.
2927 `lname' should be the name to give the function in Lisp,
2928 as a null-terminated C string.
2929 `fnname' should be the name of the function in C.
2930 By convention, it starts with F.
2931 `sname' should be the name for the C constant structure
2932 that records information on this function for internal use.
2933 By convention, it should be the same as `fnname' but with S instead of F.
2934 It's too bad that C macros can't compute this from `fnname'.
2935 `minargs' should be a number, the minimum number of arguments allowed.
2936 `maxargs' should be a number, the maximum number of arguments allowed,
2937 or else MANY or UNEVALLED.
2938 MANY means pass a vector of evaluated arguments,
2939 in the form of an integer number-of-arguments
2940 followed by the address of a vector of Lisp_Objects
2941 which contains the argument values.
2942 UNEVALLED means pass the list of unevaluated arguments
2943 `intspec' says how interactive arguments are to be fetched.
2944 If the string starts with a `(', `intspec' is evaluated and the resulting
2945 list is the list of arguments.
2946 If it's a string that doesn't start with `(', the value should follow
2947 the one of the doc string for `interactive'.
2948 A null string means call interactively with no arguments.
2949 `doc' is documentation for the user. */
2950
2951 /* This version of DEFUN declares a function prototype with the right
2952 arguments, so we can catch errors with maxargs at compile-time. */
2953 #ifdef _MSC_VER
2954 #define DEFUN(lname, fnname, sname, minargs, maxargs, intspec, doc) \
2955 Lisp_Object fnname DEFUN_ARGS_ ## maxargs ; \
2956 static struct Lisp_Subr alignas (GCALIGNMENT) sname = \
2957 { { (PVEC_SUBR << PSEUDOVECTOR_AREA_BITS) \
2958 | (sizeof (struct Lisp_Subr) / sizeof (EMACS_INT)) }, \
2959 { (Lisp_Object (__cdecl *)(void))fnname }, \
2960 minargs, maxargs, lname, intspec, 0}; \
2961 Lisp_Object fnname
2962 #else /* not _MSC_VER */
2963 #define DEFUN(lname, fnname, sname, minargs, maxargs, intspec, doc) \
2964 static struct Lisp_Subr alignas (GCALIGNMENT) sname = \
2965 { { PVEC_SUBR << PSEUDOVECTOR_AREA_BITS }, \
2966 { .a ## maxargs = fnname }, \
2967 minargs, maxargs, lname, intspec, 0}; \
2968 Lisp_Object fnname
2969 #endif
2970
2971 /* True if OBJ is a Lisp function. */
2972 INLINE bool
2973 FUNCTIONP (Lisp_Object obj)
2974 {
2975 return functionp (obj);
2976 }
2977
2978 /* defsubr (Sname);
2979 is how we define the symbol for function `name' at start-up time. */
2980 extern void defsubr (struct Lisp_Subr *);
2981
2982 enum maxargs
2983 {
2984 MANY = -2,
2985 UNEVALLED = -1
2986 };
2987
2988 /* Call a function F that accepts many args, passing it ARRAY's elements. */
2989 #define CALLMANY(f, array) (f) (ARRAYELTS (array), array)
2990
2991 /* Call a function F that accepts many args, passing it the remaining args,
2992 E.g., 'return CALLN (Fformat, fmt, text);' is less error-prone than
2993 '{ Lisp_Object a[2]; a[0] = fmt; a[1] = text; return Fformat (2, a); }'.
2994 CALLN is overkill for simple usages like 'Finsert (1, &text);'. */
2995 #define CALLN(f, ...) CALLMANY (f, ((Lisp_Object []) {__VA_ARGS__}))
2996
2997 extern void defvar_lisp (struct Lisp_Objfwd *, const char *, Lisp_Object *);
2998 extern void defvar_lisp_nopro (struct Lisp_Objfwd *, const char *, Lisp_Object *);
2999 extern void defvar_bool (struct Lisp_Boolfwd *, const char *, bool *);
3000 extern void defvar_int (struct Lisp_Intfwd *, const char *, EMACS_INT *);
3001 extern void defvar_kboard (struct Lisp_Kboard_Objfwd *, const char *, int);
3002
3003 /* Macros we use to define forwarded Lisp variables.
3004 These are used in the syms_of_FILENAME functions.
3005
3006 An ordinary (not in buffer_defaults, per-buffer, or per-keyboard)
3007 lisp variable is actually a field in `struct emacs_globals'. The
3008 field's name begins with "f_", which is a convention enforced by
3009 these macros. Each such global has a corresponding #define in
3010 globals.h; the plain name should be used in the code.
3011
3012 E.g., the global "cons_cells_consed" is declared as "int
3013 f_cons_cells_consed" in globals.h, but there is a define:
3014
3015 #define cons_cells_consed globals.f_cons_cells_consed
3016
3017 All C code uses the `cons_cells_consed' name. This is all done
3018 this way to support indirection for multi-threaded Emacs. */
3019
3020 #define DEFVAR_LISP(lname, vname, doc) \
3021 do { \
3022 static struct Lisp_Objfwd o_fwd; \
3023 defvar_lisp (&o_fwd, lname, &globals.f_ ## vname); \
3024 } while (false)
3025 #define DEFVAR_LISP_NOPRO(lname, vname, doc) \
3026 do { \
3027 static struct Lisp_Objfwd o_fwd; \
3028 defvar_lisp_nopro (&o_fwd, lname, &globals.f_ ## vname); \
3029 } while (false)
3030 #define DEFVAR_BOOL(lname, vname, doc) \
3031 do { \
3032 static struct Lisp_Boolfwd b_fwd; \
3033 defvar_bool (&b_fwd, lname, &globals.f_ ## vname); \
3034 } while (false)
3035 #define DEFVAR_INT(lname, vname, doc) \
3036 do { \
3037 static struct Lisp_Intfwd i_fwd; \
3038 defvar_int (&i_fwd, lname, &globals.f_ ## vname); \
3039 } while (false)
3040
3041 #define DEFVAR_BUFFER_DEFAULTS(lname, vname, doc) \
3042 do { \
3043 static struct Lisp_Objfwd o_fwd; \
3044 defvar_lisp_nopro (&o_fwd, lname, &BVAR (&buffer_defaults, vname)); \
3045 } while (false)
3046
3047 #define DEFVAR_KBOARD(lname, vname, doc) \
3048 do { \
3049 static struct Lisp_Kboard_Objfwd ko_fwd; \
3050 defvar_kboard (&ko_fwd, lname, offsetof (KBOARD, vname ## _)); \
3051 } while (false)
3052 \f
3053 /* Save and restore the instruction and environment pointers,
3054 without affecting the signal mask. */
3055
3056 #ifdef HAVE__SETJMP
3057 typedef jmp_buf sys_jmp_buf;
3058 # define sys_setjmp(j) _setjmp (j)
3059 # define sys_longjmp(j, v) _longjmp (j, v)
3060 #elif defined HAVE_SIGSETJMP
3061 typedef sigjmp_buf sys_jmp_buf;
3062 # define sys_setjmp(j) sigsetjmp (j, 0)
3063 # define sys_longjmp(j, v) siglongjmp (j, v)
3064 #else
3065 /* A platform that uses neither _longjmp nor siglongjmp; assume
3066 longjmp does not affect the sigmask. */
3067 typedef jmp_buf sys_jmp_buf;
3068 # define sys_setjmp(j) setjmp (j)
3069 # define sys_longjmp(j, v) longjmp (j, v)
3070 #endif
3071
3072 \f
3073 /* Elisp uses several stacks:
3074 - the C stack.
3075 - the bytecode stack: used internally by the bytecode interpreter.
3076 Allocated from the C stack.
3077 - The specpdl stack: keeps track of active unwind-protect and
3078 dynamic-let-bindings. Allocated from the `specpdl' array, a manually
3079 managed stack.
3080 - The handler stack: keeps track of active catch tags and condition-case
3081 handlers. Allocated in a manually managed stack implemented by a
3082 doubly-linked list allocated via xmalloc and never freed. */
3083
3084 /* Structure for recording Lisp call stack for backtrace purposes. */
3085
3086 /* The special binding stack holds the outer values of variables while
3087 they are bound by a function application or a let form, stores the
3088 code to be executed for unwind-protect forms.
3089
3090 NOTE: The specbinding union is defined here, because SPECPDL_INDEX is
3091 used all over the place, needs to be fast, and needs to know the size of
3092 union specbinding. But only eval.c should access it. */
3093
3094 enum specbind_tag {
3095 SPECPDL_UNWIND, /* An unwind_protect function on Lisp_Object. */
3096 SPECPDL_UNWIND_PTR, /* Likewise, on void *. */
3097 SPECPDL_UNWIND_INT, /* Likewise, on int. */
3098 SPECPDL_UNWIND_VOID, /* Likewise, with no arg. */
3099 SPECPDL_BACKTRACE, /* An element of the backtrace. */
3100 SPECPDL_LET, /* A plain and simple dynamic let-binding. */
3101 /* Tags greater than SPECPDL_LET must be "subkinds" of LET. */
3102 SPECPDL_LET_LOCAL, /* A buffer-local let-binding. */
3103 SPECPDL_LET_DEFAULT /* A global binding for a localized var. */
3104 };
3105
3106 union specbinding
3107 {
3108 ENUM_BF (specbind_tag) kind : CHAR_BIT;
3109 struct {
3110 ENUM_BF (specbind_tag) kind : CHAR_BIT;
3111 void (*func) (Lisp_Object);
3112 Lisp_Object arg;
3113 } unwind;
3114 struct {
3115 ENUM_BF (specbind_tag) kind : CHAR_BIT;
3116 void (*func) (void *);
3117 void *arg;
3118 } unwind_ptr;
3119 struct {
3120 ENUM_BF (specbind_tag) kind : CHAR_BIT;
3121 void (*func) (int);
3122 int arg;
3123 } unwind_int;
3124 struct {
3125 ENUM_BF (specbind_tag) kind : CHAR_BIT;
3126 void (*func) (void);
3127 } unwind_void;
3128 struct {
3129 ENUM_BF (specbind_tag) kind : CHAR_BIT;
3130 /* `where' is not used in the case of SPECPDL_LET. */
3131 Lisp_Object symbol, old_value, where;
3132 } let;
3133 struct {
3134 ENUM_BF (specbind_tag) kind : CHAR_BIT;
3135 bool_bf debug_on_exit : 1;
3136 Lisp_Object function;
3137 Lisp_Object *args;
3138 ptrdiff_t nargs;
3139 } bt;
3140 };
3141
3142 extern union specbinding *specpdl;
3143 extern union specbinding *specpdl_ptr;
3144 extern ptrdiff_t specpdl_size;
3145
3146 INLINE ptrdiff_t
3147 SPECPDL_INDEX (void)
3148 {
3149 return specpdl_ptr - specpdl;
3150 }
3151
3152 /* This structure helps implement the `catch/throw' and `condition-case/signal'
3153 control structures. A struct handler contains all the information needed to
3154 restore the state of the interpreter after a non-local jump.
3155
3156 handler structures are chained together in a doubly linked list; the `next'
3157 member points to the next outer catchtag and the `nextfree' member points in
3158 the other direction to the next inner element (which is typically the next
3159 free element since we mostly use it on the deepest handler).
3160
3161 A call like (throw TAG VAL) searches for a catchtag whose `tag_or_ch'
3162 member is TAG, and then unbinds to it. The `val' member is used to
3163 hold VAL while the stack is unwound; `val' is returned as the value
3164 of the catch form. If there is a handler of type CATCHER_ALL, it will
3165 be treated as a handler for all invocations of `throw'; in this case
3166 `val' will be set to (TAG . VAL).
3167
3168 All the other members are concerned with restoring the interpreter
3169 state.
3170
3171 Members are volatile if their values need to survive _longjmp when
3172 a 'struct handler' is a local variable. */
3173
3174 enum handlertype { CATCHER, CONDITION_CASE, CATCHER_ALL };
3175
3176 struct handler
3177 {
3178 enum handlertype type;
3179 Lisp_Object tag_or_ch;
3180 Lisp_Object val;
3181 struct handler *next;
3182 struct handler *nextfree;
3183
3184 /* The bytecode interpreter can have several handlers active at the same
3185 time, so when we longjmp to one of them, it needs to know which handler
3186 this was and what was the corresponding internal state. This is stored
3187 here, and when we longjmp we make sure that handlerlist points to the
3188 proper handler. */
3189 Lisp_Object *bytecode_top;
3190 int bytecode_dest;
3191
3192 /* Most global vars are reset to their value via the specpdl mechanism,
3193 but a few others are handled by storing their value here. */
3194 sys_jmp_buf jmp;
3195 EMACS_INT lisp_eval_depth;
3196 ptrdiff_t pdlcount;
3197 int poll_suppress_count;
3198 int interrupt_input_blocked;
3199 struct byte_stack *byte_stack;
3200 };
3201
3202 extern Lisp_Object memory_signal_data;
3203
3204 /* An address near the bottom of the stack.
3205 Tells GC how to save a copy of the stack. */
3206 extern char *stack_bottom;
3207
3208 /* Check quit-flag and quit if it is non-nil.
3209 Typing C-g does not directly cause a quit; it only sets Vquit_flag.
3210 So the program needs to do QUIT at times when it is safe to quit.
3211 Every loop that might run for a long time or might not exit
3212 ought to do QUIT at least once, at a safe place.
3213 Unless that is impossible, of course.
3214 But it is very desirable to avoid creating loops where QUIT is impossible.
3215
3216 Exception: if you set immediate_quit to true,
3217 then the handler that responds to the C-g does the quit itself.
3218 This is a good thing to do around a loop that has no side effects
3219 and (in particular) cannot call arbitrary Lisp code.
3220
3221 If quit-flag is set to `kill-emacs' the SIGINT handler has received
3222 a request to exit Emacs when it is safe to do. */
3223
3224 extern void process_pending_signals (void);
3225 extern bool volatile pending_signals;
3226
3227 extern void process_quit_flag (void);
3228 #define QUIT \
3229 do { \
3230 if (!NILP (Vquit_flag) && NILP (Vinhibit_quit)) \
3231 process_quit_flag (); \
3232 else if (pending_signals) \
3233 process_pending_signals (); \
3234 } while (false)
3235
3236
3237 /* True if ought to quit now. */
3238
3239 #define QUITP (!NILP (Vquit_flag) && NILP (Vinhibit_quit))
3240 \f
3241 extern Lisp_Object Vascii_downcase_table;
3242 extern Lisp_Object Vascii_canon_table;
3243 \f
3244 /* Call staticpro (&var) to protect static variable `var'. */
3245
3246 void staticpro (Lisp_Object *);
3247 \f
3248 /* Forward declarations for prototypes. */
3249 struct window;
3250 struct frame;
3251
3252 /* Copy COUNT Lisp_Objects from ARGS to contents of V starting from OFFSET. */
3253
3254 INLINE void
3255 vcopy (Lisp_Object v, ptrdiff_t offset, Lisp_Object *args, ptrdiff_t count)
3256 {
3257 eassert (0 <= offset && 0 <= count && offset + count <= ASIZE (v));
3258 memcpy (XVECTOR (v)->contents + offset, args, count * sizeof *args);
3259 }
3260
3261 /* Functions to modify hash tables. */
3262
3263 INLINE void
3264 set_hash_key_slot (struct Lisp_Hash_Table *h, ptrdiff_t idx, Lisp_Object val)
3265 {
3266 gc_aset (h->key_and_value, 2 * idx, val);
3267 }
3268
3269 INLINE void
3270 set_hash_value_slot (struct Lisp_Hash_Table *h, ptrdiff_t idx, Lisp_Object val)
3271 {
3272 gc_aset (h->key_and_value, 2 * idx + 1, val);
3273 }
3274
3275 /* Use these functions to set Lisp_Object
3276 or pointer slots of struct Lisp_Symbol. */
3277
3278 INLINE void
3279 set_symbol_function (Lisp_Object sym, Lisp_Object function)
3280 {
3281 XSYMBOL (sym)->function = function;
3282 }
3283
3284 INLINE void
3285 set_symbol_plist (Lisp_Object sym, Lisp_Object plist)
3286 {
3287 XSYMBOL (sym)->plist = plist;
3288 }
3289
3290 INLINE void
3291 set_symbol_next (Lisp_Object sym, struct Lisp_Symbol *next)
3292 {
3293 XSYMBOL (sym)->next = next;
3294 }
3295
3296 /* Buffer-local (also frame-local) variable access functions. */
3297
3298 INLINE int
3299 blv_found (struct Lisp_Buffer_Local_Value *blv)
3300 {
3301 eassert (blv->found == !EQ (blv->defcell, blv->valcell));
3302 return blv->found;
3303 }
3304
3305 /* Set overlay's property list. */
3306
3307 INLINE void
3308 set_overlay_plist (Lisp_Object overlay, Lisp_Object plist)
3309 {
3310 XOVERLAY (overlay)->plist = plist;
3311 }
3312
3313 /* Get text properties of S. */
3314
3315 INLINE INTERVAL
3316 string_intervals (Lisp_Object s)
3317 {
3318 return XSTRING (s)->intervals;
3319 }
3320
3321 /* Set text properties of S to I. */
3322
3323 INLINE void
3324 set_string_intervals (Lisp_Object s, INTERVAL i)
3325 {
3326 XSTRING (s)->intervals = i;
3327 }
3328
3329 /* Set a Lisp slot in TABLE to VAL. Most code should use this instead
3330 of setting slots directly. */
3331
3332 INLINE void
3333 set_char_table_defalt (Lisp_Object table, Lisp_Object val)
3334 {
3335 XCHAR_TABLE (table)->defalt = val;
3336 }
3337 INLINE void
3338 set_char_table_purpose (Lisp_Object table, Lisp_Object val)
3339 {
3340 XCHAR_TABLE (table)->purpose = val;
3341 }
3342
3343 /* Set different slots in (sub)character tables. */
3344
3345 INLINE void
3346 set_char_table_extras (Lisp_Object table, ptrdiff_t idx, Lisp_Object val)
3347 {
3348 eassert (0 <= idx && idx < CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (table)));
3349 XCHAR_TABLE (table)->extras[idx] = val;
3350 }
3351
3352 INLINE void
3353 set_char_table_contents (Lisp_Object table, ptrdiff_t idx, Lisp_Object val)
3354 {
3355 eassert (0 <= idx && idx < (1 << CHARTAB_SIZE_BITS_0));
3356 XCHAR_TABLE (table)->contents[idx] = val;
3357 }
3358
3359 INLINE void
3360 set_sub_char_table_contents (Lisp_Object table, ptrdiff_t idx, Lisp_Object val)
3361 {
3362 XSUB_CHAR_TABLE (table)->contents[idx] = val;
3363 }
3364
3365 /* Defined in data.c. */
3366 extern Lisp_Object indirect_function (Lisp_Object);
3367 extern Lisp_Object find_symbol_value (Lisp_Object);
3368 enum Arith_Comparison {
3369 ARITH_EQUAL,
3370 ARITH_NOTEQUAL,
3371 ARITH_LESS,
3372 ARITH_GRTR,
3373 ARITH_LESS_OR_EQUAL,
3374 ARITH_GRTR_OR_EQUAL
3375 };
3376 extern Lisp_Object arithcompare (Lisp_Object num1, Lisp_Object num2,
3377 enum Arith_Comparison comparison);
3378
3379 /* Convert the integer I to an Emacs representation, either the integer
3380 itself, or a cons of two or three integers, or if all else fails a float.
3381 I should not have side effects. */
3382 #define INTEGER_TO_CONS(i) \
3383 (! FIXNUM_OVERFLOW_P (i) \
3384 ? make_number (i) \
3385 : EXPR_SIGNED (i) ? intbig_to_lisp (i) : uintbig_to_lisp (i))
3386 extern Lisp_Object intbig_to_lisp (intmax_t);
3387 extern Lisp_Object uintbig_to_lisp (uintmax_t);
3388
3389 /* Convert the Emacs representation CONS back to an integer of type
3390 TYPE, storing the result the variable VAR. Signal an error if CONS
3391 is not a valid representation or is out of range for TYPE. */
3392 #define CONS_TO_INTEGER(cons, type, var) \
3393 (TYPE_SIGNED (type) \
3394 ? ((var) = cons_to_signed (cons, TYPE_MINIMUM (type), TYPE_MAXIMUM (type))) \
3395 : ((var) = cons_to_unsigned (cons, TYPE_MAXIMUM (type))))
3396 extern intmax_t cons_to_signed (Lisp_Object, intmax_t, intmax_t);
3397 extern uintmax_t cons_to_unsigned (Lisp_Object, uintmax_t);
3398
3399 extern struct Lisp_Symbol *indirect_variable (struct Lisp_Symbol *);
3400 extern _Noreturn void args_out_of_range (Lisp_Object, Lisp_Object);
3401 extern _Noreturn void args_out_of_range_3 (Lisp_Object, Lisp_Object,
3402 Lisp_Object);
3403 extern Lisp_Object do_symval_forwarding (union Lisp_Fwd *);
3404 extern void set_internal (Lisp_Object, Lisp_Object, Lisp_Object, bool);
3405 extern void syms_of_data (void);
3406 extern void swap_in_global_binding (struct Lisp_Symbol *);
3407
3408 /* Defined in cmds.c */
3409 extern void syms_of_cmds (void);
3410 extern void keys_of_cmds (void);
3411
3412 /* Defined in coding.c. */
3413 extern Lisp_Object detect_coding_system (const unsigned char *, ptrdiff_t,
3414 ptrdiff_t, bool, bool, Lisp_Object);
3415 extern void init_coding (void);
3416 extern void init_coding_once (void);
3417 extern void syms_of_coding (void);
3418
3419 /* Defined in character.c. */
3420 extern ptrdiff_t chars_in_text (const unsigned char *, ptrdiff_t);
3421 extern ptrdiff_t multibyte_chars_in_text (const unsigned char *, ptrdiff_t);
3422 extern void syms_of_character (void);
3423
3424 /* Defined in charset.c. */
3425 extern void init_charset (void);
3426 extern void init_charset_once (void);
3427 extern void syms_of_charset (void);
3428 /* Structure forward declarations. */
3429 struct charset;
3430
3431 /* Defined in syntax.c. */
3432 extern void init_syntax_once (void);
3433 extern void syms_of_syntax (void);
3434
3435 /* Defined in fns.c. */
3436 enum { NEXT_ALMOST_PRIME_LIMIT = 11 };
3437 extern EMACS_INT next_almost_prime (EMACS_INT) ATTRIBUTE_CONST;
3438 extern Lisp_Object larger_vector (Lisp_Object, ptrdiff_t, ptrdiff_t);
3439 extern void sweep_weak_hash_tables (void);
3440 EMACS_UINT hash_string (char const *, ptrdiff_t);
3441 EMACS_UINT sxhash (Lisp_Object, int);
3442 Lisp_Object make_hash_table (struct hash_table_test, Lisp_Object, Lisp_Object,
3443 Lisp_Object, Lisp_Object);
3444 ptrdiff_t hash_lookup (struct Lisp_Hash_Table *, Lisp_Object, EMACS_UINT *);
3445 ptrdiff_t hash_put (struct Lisp_Hash_Table *, Lisp_Object, Lisp_Object,
3446 EMACS_UINT);
3447 void hash_remove_from_table (struct Lisp_Hash_Table *, Lisp_Object);
3448 extern struct hash_table_test const hashtest_eq, hashtest_eql, hashtest_equal;
3449 extern void validate_subarray (Lisp_Object, Lisp_Object, Lisp_Object,
3450 ptrdiff_t, ptrdiff_t *, ptrdiff_t *);
3451 extern Lisp_Object substring_both (Lisp_Object, ptrdiff_t, ptrdiff_t,
3452 ptrdiff_t, ptrdiff_t);
3453 extern Lisp_Object merge (Lisp_Object, Lisp_Object, Lisp_Object);
3454 extern Lisp_Object do_yes_or_no_p (Lisp_Object);
3455 extern Lisp_Object concat2 (Lisp_Object, Lisp_Object);
3456 extern Lisp_Object concat3 (Lisp_Object, Lisp_Object, Lisp_Object);
3457 extern Lisp_Object nconc2 (Lisp_Object, Lisp_Object);
3458 extern Lisp_Object assq_no_quit (Lisp_Object, Lisp_Object);
3459 extern Lisp_Object assoc_no_quit (Lisp_Object, Lisp_Object);
3460 extern void clear_string_char_byte_cache (void);
3461 extern ptrdiff_t string_char_to_byte (Lisp_Object, ptrdiff_t);
3462 extern ptrdiff_t string_byte_to_char (Lisp_Object, ptrdiff_t);
3463 extern Lisp_Object string_to_multibyte (Lisp_Object);
3464 extern Lisp_Object string_make_unibyte (Lisp_Object);
3465 extern void syms_of_fns (void);
3466
3467 /* Defined in floatfns.c. */
3468 extern void syms_of_floatfns (void);
3469 extern Lisp_Object fmod_float (Lisp_Object x, Lisp_Object y);
3470
3471 /* Defined in fringe.c. */
3472 extern void syms_of_fringe (void);
3473 extern void init_fringe (void);
3474 #ifdef HAVE_WINDOW_SYSTEM
3475 extern void mark_fringe_data (void);
3476 extern void init_fringe_once (void);
3477 #endif /* HAVE_WINDOW_SYSTEM */
3478
3479 /* Defined in image.c. */
3480 extern int x_bitmap_mask (struct frame *, ptrdiff_t);
3481 extern void reset_image_types (void);
3482 extern void syms_of_image (void);
3483
3484 /* Defined in insdel.c. */
3485 extern void move_gap_both (ptrdiff_t, ptrdiff_t);
3486 extern _Noreturn void buffer_overflow (void);
3487 extern void make_gap (ptrdiff_t);
3488 extern void make_gap_1 (struct buffer *, ptrdiff_t);
3489 extern ptrdiff_t copy_text (const unsigned char *, unsigned char *,
3490 ptrdiff_t, bool, bool);
3491 extern int count_combining_before (const unsigned char *,
3492 ptrdiff_t, ptrdiff_t, ptrdiff_t);
3493 extern int count_combining_after (const unsigned char *,
3494 ptrdiff_t, ptrdiff_t, ptrdiff_t);
3495 extern void insert (const char *, ptrdiff_t);
3496 extern void insert_and_inherit (const char *, ptrdiff_t);
3497 extern void insert_1_both (const char *, ptrdiff_t, ptrdiff_t,
3498 bool, bool, bool);
3499 extern void insert_from_gap (ptrdiff_t, ptrdiff_t, bool text_at_gap_tail);
3500 extern void insert_from_string (Lisp_Object, ptrdiff_t, ptrdiff_t,
3501 ptrdiff_t, ptrdiff_t, bool);
3502 extern void insert_from_buffer (struct buffer *, ptrdiff_t, ptrdiff_t, bool);
3503 extern void insert_char (int);
3504 extern void insert_string (const char *);
3505 extern void insert_before_markers (const char *, ptrdiff_t);
3506 extern void insert_before_markers_and_inherit (const char *, ptrdiff_t);
3507 extern void insert_from_string_before_markers (Lisp_Object, ptrdiff_t,
3508 ptrdiff_t, ptrdiff_t,
3509 ptrdiff_t, bool);
3510 extern void del_range (ptrdiff_t, ptrdiff_t);
3511 extern Lisp_Object del_range_1 (ptrdiff_t, ptrdiff_t, bool, bool);
3512 extern void del_range_byte (ptrdiff_t, ptrdiff_t, bool);
3513 extern void del_range_both (ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t, bool);
3514 extern Lisp_Object del_range_2 (ptrdiff_t, ptrdiff_t,
3515 ptrdiff_t, ptrdiff_t, bool);
3516 extern void modify_text (ptrdiff_t, ptrdiff_t);
3517 extern void prepare_to_modify_buffer (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
3518 extern void prepare_to_modify_buffer_1 (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
3519 extern void invalidate_buffer_caches (struct buffer *, ptrdiff_t, ptrdiff_t);
3520 extern void signal_after_change (ptrdiff_t, ptrdiff_t, ptrdiff_t);
3521 extern void adjust_after_insert (ptrdiff_t, ptrdiff_t, ptrdiff_t,
3522 ptrdiff_t, ptrdiff_t);
3523 extern void adjust_markers_for_delete (ptrdiff_t, ptrdiff_t,
3524 ptrdiff_t, ptrdiff_t);
3525 extern void replace_range (ptrdiff_t, ptrdiff_t, Lisp_Object, bool, bool, bool);
3526 extern void replace_range_2 (ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t,
3527 const char *, ptrdiff_t, ptrdiff_t, bool);
3528 extern void syms_of_insdel (void);
3529
3530 /* Defined in dispnew.c. */
3531 #if (defined PROFILING \
3532 && (defined __FreeBSD__ || defined GNU_LINUX || defined __MINGW32__))
3533 _Noreturn void __executable_start (void);
3534 #endif
3535 extern Lisp_Object Vwindow_system;
3536 extern Lisp_Object sit_for (Lisp_Object, bool, int);
3537
3538 /* Defined in xdisp.c. */
3539 extern bool noninteractive_need_newline;
3540 extern Lisp_Object echo_area_buffer[2];
3541 extern void add_to_log (char const *, ...);
3542 extern void vadd_to_log (char const *, va_list);
3543 extern void check_message_stack (void);
3544 extern void setup_echo_area_for_printing (bool);
3545 extern bool push_message (void);
3546 extern void pop_message_unwind (void);
3547 extern Lisp_Object restore_message_unwind (Lisp_Object);
3548 extern void restore_message (void);
3549 extern Lisp_Object current_message (void);
3550 extern void clear_message (bool, bool);
3551 extern void message (const char *, ...) ATTRIBUTE_FORMAT_PRINTF (1, 2);
3552 extern void message1 (const char *);
3553 extern void message1_nolog (const char *);
3554 extern void message3 (Lisp_Object);
3555 extern void message3_nolog (Lisp_Object);
3556 extern void message_dolog (const char *, ptrdiff_t, bool, bool);
3557 extern void message_with_string (const char *, Lisp_Object, bool);
3558 extern void message_log_maybe_newline (void);
3559 extern void update_echo_area (void);
3560 extern void truncate_echo_area (ptrdiff_t);
3561 extern void redisplay (void);
3562
3563 void set_frame_cursor_types (struct frame *, Lisp_Object);
3564 extern void syms_of_xdisp (void);
3565 extern void init_xdisp (void);
3566 extern Lisp_Object safe_eval (Lisp_Object);
3567 extern bool pos_visible_p (struct window *, ptrdiff_t, int *,
3568 int *, int *, int *, int *, int *);
3569
3570 /* Defined in xsettings.c. */
3571 extern void syms_of_xsettings (void);
3572
3573 /* Defined in vm-limit.c. */
3574 extern void memory_warnings (void *, void (*warnfun) (const char *));
3575
3576 /* Defined in character.c. */
3577 extern void parse_str_as_multibyte (const unsigned char *, ptrdiff_t,
3578 ptrdiff_t *, ptrdiff_t *);
3579
3580 /* Defined in alloc.c. */
3581 extern void *my_heap_start (void);
3582 extern void check_pure_size (void);
3583 extern void free_misc (Lisp_Object);
3584 extern void allocate_string_data (struct Lisp_String *, EMACS_INT, EMACS_INT);
3585 extern void malloc_warning (const char *);
3586 extern _Noreturn void memory_full (size_t);
3587 extern _Noreturn void buffer_memory_full (ptrdiff_t);
3588 extern bool survives_gc_p (Lisp_Object);
3589 extern void mark_object (Lisp_Object);
3590 #if defined REL_ALLOC && !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
3591 extern void refill_memory_reserve (void);
3592 #endif
3593 extern void alloc_unexec_pre (void);
3594 extern void alloc_unexec_post (void);
3595 extern const char *pending_malloc_warning;
3596 extern Lisp_Object zero_vector;
3597 extern Lisp_Object *stack_base;
3598 extern EMACS_INT consing_since_gc;
3599 extern EMACS_INT gc_relative_threshold;
3600 extern EMACS_INT memory_full_cons_threshold;
3601 extern Lisp_Object list1 (Lisp_Object);
3602 extern Lisp_Object list2 (Lisp_Object, Lisp_Object);
3603 extern Lisp_Object list3 (Lisp_Object, Lisp_Object, Lisp_Object);
3604 extern Lisp_Object list4 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object);
3605 extern Lisp_Object list5 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object,
3606 Lisp_Object);
3607 enum constype {CONSTYPE_HEAP, CONSTYPE_PURE};
3608 extern Lisp_Object listn (enum constype, ptrdiff_t, Lisp_Object, ...);
3609
3610 /* Build a frequently used 2/3/4-integer lists. */
3611
3612 INLINE Lisp_Object
3613 list2i (EMACS_INT x, EMACS_INT y)
3614 {
3615 return list2 (make_number (x), make_number (y));
3616 }
3617
3618 INLINE Lisp_Object
3619 list3i (EMACS_INT x, EMACS_INT y, EMACS_INT w)
3620 {
3621 return list3 (make_number (x), make_number (y), make_number (w));
3622 }
3623
3624 INLINE Lisp_Object
3625 list4i (EMACS_INT x, EMACS_INT y, EMACS_INT w, EMACS_INT h)
3626 {
3627 return list4 (make_number (x), make_number (y),
3628 make_number (w), make_number (h));
3629 }
3630
3631 extern Lisp_Object make_uninit_bool_vector (EMACS_INT);
3632 extern Lisp_Object bool_vector_fill (Lisp_Object, Lisp_Object);
3633 extern _Noreturn void string_overflow (void);
3634 extern Lisp_Object make_string (const char *, ptrdiff_t);
3635 extern Lisp_Object make_formatted_string (char *, const char *, ...)
3636 ATTRIBUTE_FORMAT_PRINTF (2, 3);
3637 extern Lisp_Object make_unibyte_string (const char *, ptrdiff_t);
3638
3639 /* Make unibyte string from C string when the length isn't known. */
3640
3641 INLINE Lisp_Object
3642 build_unibyte_string (const char *str)
3643 {
3644 return make_unibyte_string (str, strlen (str));
3645 }
3646
3647 extern Lisp_Object make_multibyte_string (const char *, ptrdiff_t, ptrdiff_t);
3648 extern Lisp_Object make_event_array (ptrdiff_t, Lisp_Object *);
3649 extern Lisp_Object make_uninit_string (EMACS_INT);
3650 extern Lisp_Object make_uninit_multibyte_string (EMACS_INT, EMACS_INT);
3651 extern Lisp_Object make_string_from_bytes (const char *, ptrdiff_t, ptrdiff_t);
3652 extern Lisp_Object make_specified_string (const char *,
3653 ptrdiff_t, ptrdiff_t, bool);
3654 extern Lisp_Object make_pure_string (const char *, ptrdiff_t, ptrdiff_t, bool);
3655 extern Lisp_Object make_pure_c_string (const char *, ptrdiff_t);
3656
3657 /* Make a string allocated in pure space, use STR as string data. */
3658
3659 INLINE Lisp_Object
3660 build_pure_c_string (const char *str)
3661 {
3662 return make_pure_c_string (str, strlen (str));
3663 }
3664
3665 /* Make a string from the data at STR, treating it as multibyte if the
3666 data warrants. */
3667
3668 INLINE Lisp_Object
3669 build_string (const char *str)
3670 {
3671 return make_string (str, strlen (str));
3672 }
3673
3674 extern Lisp_Object pure_cons (Lisp_Object, Lisp_Object);
3675 extern void make_byte_code (struct Lisp_Vector *);
3676 extern struct Lisp_Vector *allocate_vector (EMACS_INT);
3677
3678 /* Make an uninitialized vector for SIZE objects. NOTE: you must
3679 be sure that GC cannot happen until the vector is completely
3680 initialized. E.g. the following code is likely to crash:
3681
3682 v = make_uninit_vector (3);
3683 ASET (v, 0, obj0);
3684 ASET (v, 1, Ffunction_can_gc ());
3685 ASET (v, 2, obj1); */
3686
3687 INLINE Lisp_Object
3688 make_uninit_vector (ptrdiff_t size)
3689 {
3690 Lisp_Object v;
3691 struct Lisp_Vector *p;
3692
3693 p = allocate_vector (size);
3694 XSETVECTOR (v, p);
3695 return v;
3696 }
3697
3698 /* Like above, but special for sub char-tables. */
3699
3700 INLINE Lisp_Object
3701 make_uninit_sub_char_table (int depth, int min_char)
3702 {
3703 int slots = SUB_CHAR_TABLE_OFFSET + chartab_size[depth];
3704 Lisp_Object v = make_uninit_vector (slots);
3705
3706 XSETPVECTYPE (XVECTOR (v), PVEC_SUB_CHAR_TABLE);
3707 XSUB_CHAR_TABLE (v)->depth = depth;
3708 XSUB_CHAR_TABLE (v)->min_char = min_char;
3709 return v;
3710 }
3711
3712 extern struct Lisp_Vector *allocate_pseudovector (int, int, int,
3713 enum pvec_type);
3714
3715 /* Allocate partially initialized pseudovector where all Lisp_Object
3716 slots are set to Qnil but the rest (if any) is left uninitialized. */
3717
3718 #define ALLOCATE_PSEUDOVECTOR(type, field, tag) \
3719 ((type *) allocate_pseudovector (VECSIZE (type), \
3720 PSEUDOVECSIZE (type, field), \
3721 PSEUDOVECSIZE (type, field), tag))
3722
3723 /* Allocate fully initialized pseudovector where all Lisp_Object
3724 slots are set to Qnil and the rest (if any) is zeroed. */
3725
3726 #define ALLOCATE_ZEROED_PSEUDOVECTOR(type, field, tag) \
3727 ((type *) allocate_pseudovector (VECSIZE (type), \
3728 PSEUDOVECSIZE (type, field), \
3729 VECSIZE (type), tag))
3730
3731 extern bool gc_in_progress;
3732 extern bool abort_on_gc;
3733 extern Lisp_Object make_float (double);
3734 extern void display_malloc_warning (void);
3735 extern ptrdiff_t inhibit_garbage_collection (void);
3736 extern Lisp_Object make_save_int_int_int (ptrdiff_t, ptrdiff_t, ptrdiff_t);
3737 extern Lisp_Object make_save_obj_obj_obj_obj (Lisp_Object, Lisp_Object,
3738 Lisp_Object, Lisp_Object);
3739 extern Lisp_Object make_save_ptr (void *);
3740 extern Lisp_Object make_save_ptr_int (void *, ptrdiff_t);
3741 extern Lisp_Object make_save_ptr_ptr (void *, void *);
3742 extern Lisp_Object make_save_funcptr_ptr_obj (void (*) (void), void *,
3743 Lisp_Object);
3744 extern Lisp_Object make_save_memory (Lisp_Object *, ptrdiff_t);
3745 extern void free_save_value (Lisp_Object);
3746 extern Lisp_Object build_overlay (Lisp_Object, Lisp_Object, Lisp_Object);
3747 extern void free_marker (Lisp_Object);
3748 extern void free_cons (struct Lisp_Cons *);
3749 extern void init_alloc_once (void);
3750 extern void init_alloc (void);
3751 extern void syms_of_alloc (void);
3752 extern struct buffer * allocate_buffer (void);
3753 extern int valid_lisp_object_p (Lisp_Object);
3754 #ifdef GC_CHECK_CONS_LIST
3755 extern void check_cons_list (void);
3756 #else
3757 INLINE void (check_cons_list) (void) { lisp_h_check_cons_list (); }
3758 #endif
3759
3760 /* Defined in gmalloc.c. */
3761 #if !defined DOUG_LEA_MALLOC && !defined HYBRID_MALLOC && !defined SYSTEM_MALLOC
3762 extern size_t __malloc_extra_blocks;
3763 #endif
3764 #if !HAVE_DECL_ALIGNED_ALLOC
3765 extern void *aligned_alloc (size_t, size_t) ATTRIBUTE_MALLOC_SIZE ((2));
3766 #endif
3767 extern void malloc_enable_thread (void);
3768
3769 #ifdef REL_ALLOC
3770 /* Defined in ralloc.c. */
3771 extern void *r_alloc (void **, size_t) ATTRIBUTE_ALLOC_SIZE ((2));
3772 extern void r_alloc_free (void **);
3773 extern void *r_re_alloc (void **, size_t) ATTRIBUTE_ALLOC_SIZE ((2));
3774 extern void r_alloc_reset_variable (void **, void **);
3775 extern void r_alloc_inhibit_buffer_relocation (int);
3776 #endif
3777
3778 /* Defined in chartab.c. */
3779 extern Lisp_Object copy_char_table (Lisp_Object);
3780 extern Lisp_Object char_table_ref_and_range (Lisp_Object, int,
3781 int *, int *);
3782 extern void char_table_set_range (Lisp_Object, int, int, Lisp_Object);
3783 extern void map_char_table (void (*) (Lisp_Object, Lisp_Object,
3784 Lisp_Object),
3785 Lisp_Object, Lisp_Object, Lisp_Object);
3786 extern void map_char_table_for_charset (void (*c_function) (Lisp_Object, Lisp_Object),
3787 Lisp_Object, Lisp_Object,
3788 Lisp_Object, struct charset *,
3789 unsigned, unsigned);
3790 extern Lisp_Object uniprop_table (Lisp_Object);
3791 extern void syms_of_chartab (void);
3792
3793 /* Defined in print.c. */
3794 extern Lisp_Object Vprin1_to_string_buffer;
3795 extern void debug_print (Lisp_Object) EXTERNALLY_VISIBLE;
3796 extern void temp_output_buffer_setup (const char *);
3797 extern int print_level;
3798 extern void write_string (const char *);
3799 extern void print_error_message (Lisp_Object, Lisp_Object, const char *,
3800 Lisp_Object);
3801 extern Lisp_Object internal_with_output_to_temp_buffer
3802 (const char *, Lisp_Object (*) (Lisp_Object), Lisp_Object);
3803 #define FLOAT_TO_STRING_BUFSIZE 350
3804 extern int float_to_string (char *, double);
3805 extern void init_print_once (void);
3806 extern void syms_of_print (void);
3807
3808 /* Defined in doprnt.c. */
3809 extern ptrdiff_t doprnt (char *, ptrdiff_t, const char *, const char *,
3810 va_list);
3811 extern ptrdiff_t esprintf (char *, char const *, ...)
3812 ATTRIBUTE_FORMAT_PRINTF (2, 3);
3813 extern ptrdiff_t exprintf (char **, ptrdiff_t *, char const *, ptrdiff_t,
3814 char const *, ...)
3815 ATTRIBUTE_FORMAT_PRINTF (5, 6);
3816 extern ptrdiff_t evxprintf (char **, ptrdiff_t *, char const *, ptrdiff_t,
3817 char const *, va_list)
3818 ATTRIBUTE_FORMAT_PRINTF (5, 0);
3819
3820 /* Defined in lread.c. */
3821 extern Lisp_Object check_obarray (Lisp_Object);
3822 extern Lisp_Object intern_1 (const char *, ptrdiff_t);
3823 extern Lisp_Object intern_c_string_1 (const char *, ptrdiff_t);
3824 extern Lisp_Object intern_driver (Lisp_Object, Lisp_Object, Lisp_Object);
3825 extern void init_symbol (Lisp_Object, Lisp_Object);
3826 extern Lisp_Object oblookup (Lisp_Object, const char *, ptrdiff_t, ptrdiff_t);
3827 INLINE void
3828 LOADHIST_ATTACH (Lisp_Object x)
3829 {
3830 if (initialized)
3831 Vcurrent_load_list = Fcons (x, Vcurrent_load_list);
3832 }
3833 extern int openp (Lisp_Object, Lisp_Object, Lisp_Object,
3834 Lisp_Object *, Lisp_Object, bool);
3835 extern Lisp_Object string_to_number (char const *, int, bool);
3836 extern void map_obarray (Lisp_Object, void (*) (Lisp_Object, Lisp_Object),
3837 Lisp_Object);
3838 extern void dir_warning (const char *, Lisp_Object);
3839 extern void init_obarray (void);
3840 extern void init_lread (void);
3841 extern void syms_of_lread (void);
3842
3843 INLINE Lisp_Object
3844 intern (const char *str)
3845 {
3846 return intern_1 (str, strlen (str));
3847 }
3848
3849 INLINE Lisp_Object
3850 intern_c_string (const char *str)
3851 {
3852 return intern_c_string_1 (str, strlen (str));
3853 }
3854
3855 /* Defined in eval.c. */
3856 extern Lisp_Object Vautoload_queue;
3857 extern Lisp_Object Vrun_hooks;
3858 extern Lisp_Object Vsignaling_function;
3859 extern Lisp_Object inhibit_lisp_code;
3860 extern struct handler *handlerlist;
3861
3862 /* To run a normal hook, use the appropriate function from the list below.
3863 The calling convention:
3864
3865 if (!NILP (Vrun_hooks))
3866 call1 (Vrun_hooks, Qmy_funny_hook);
3867
3868 should no longer be used. */
3869 extern void run_hook (Lisp_Object);
3870 extern void run_hook_with_args_2 (Lisp_Object, Lisp_Object, Lisp_Object);
3871 extern Lisp_Object run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
3872 Lisp_Object (*funcall)
3873 (ptrdiff_t nargs, Lisp_Object *args));
3874 extern _Noreturn void xsignal (Lisp_Object, Lisp_Object);
3875 extern _Noreturn void xsignal0 (Lisp_Object);
3876 extern _Noreturn void xsignal1 (Lisp_Object, Lisp_Object);
3877 extern _Noreturn void xsignal2 (Lisp_Object, Lisp_Object, Lisp_Object);
3878 extern _Noreturn void xsignal3 (Lisp_Object, Lisp_Object, Lisp_Object,
3879 Lisp_Object);
3880 extern _Noreturn void signal_error (const char *, Lisp_Object);
3881 extern Lisp_Object eval_sub (Lisp_Object form);
3882 extern Lisp_Object apply1 (Lisp_Object, Lisp_Object);
3883 extern Lisp_Object call0 (Lisp_Object);
3884 extern Lisp_Object call1 (Lisp_Object, Lisp_Object);
3885 extern Lisp_Object call2 (Lisp_Object, Lisp_Object, Lisp_Object);
3886 extern Lisp_Object call3 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object);
3887 extern Lisp_Object call4 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object);
3888 extern Lisp_Object call5 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object);
3889 extern Lisp_Object call6 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object);
3890 extern Lisp_Object call7 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object);
3891 extern Lisp_Object internal_catch (Lisp_Object, Lisp_Object (*) (Lisp_Object), Lisp_Object);
3892 extern Lisp_Object internal_lisp_condition_case (Lisp_Object, Lisp_Object, Lisp_Object);
3893 extern Lisp_Object internal_condition_case (Lisp_Object (*) (void), Lisp_Object, Lisp_Object (*) (Lisp_Object));
3894 extern Lisp_Object internal_condition_case_1 (Lisp_Object (*) (Lisp_Object), Lisp_Object, Lisp_Object, Lisp_Object (*) (Lisp_Object));
3895 extern Lisp_Object internal_condition_case_2 (Lisp_Object (*) (Lisp_Object, Lisp_Object), Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object (*) (Lisp_Object));
3896 extern Lisp_Object internal_condition_case_n
3897 (Lisp_Object (*) (ptrdiff_t, Lisp_Object *), ptrdiff_t, Lisp_Object *,
3898 Lisp_Object, Lisp_Object (*) (Lisp_Object, ptrdiff_t, Lisp_Object *));
3899 extern struct handler *push_handler (Lisp_Object, enum handlertype);
3900 extern struct handler *push_handler_nosignal (Lisp_Object, enum handlertype);
3901 extern void specbind (Lisp_Object, Lisp_Object);
3902 extern void record_unwind_protect (void (*) (Lisp_Object), Lisp_Object);
3903 extern void record_unwind_protect_ptr (void (*) (void *), void *);
3904 extern void record_unwind_protect_int (void (*) (int), int);
3905 extern void record_unwind_protect_void (void (*) (void));
3906 extern void record_unwind_protect_nothing (void);
3907 extern void clear_unwind_protect (ptrdiff_t);
3908 extern void set_unwind_protect (ptrdiff_t, void (*) (Lisp_Object), Lisp_Object);
3909 extern void set_unwind_protect_ptr (ptrdiff_t, void (*) (void *), void *);
3910 extern Lisp_Object unbind_to (ptrdiff_t, Lisp_Object);
3911 extern _Noreturn void error (const char *, ...) ATTRIBUTE_FORMAT_PRINTF (1, 2);
3912 extern _Noreturn void verror (const char *, va_list)
3913 ATTRIBUTE_FORMAT_PRINTF (1, 0);
3914 extern Lisp_Object vformat_string (const char *, va_list)
3915 ATTRIBUTE_FORMAT_PRINTF (1, 0);
3916 extern void un_autoload (Lisp_Object);
3917 extern Lisp_Object call_debugger (Lisp_Object arg);
3918 extern void *near_C_stack_top (void);
3919 extern void init_eval_once (void);
3920 extern Lisp_Object safe_call (ptrdiff_t, Lisp_Object, ...);
3921 extern Lisp_Object safe_call1 (Lisp_Object, Lisp_Object);
3922 extern Lisp_Object safe_call2 (Lisp_Object, Lisp_Object, Lisp_Object);
3923 extern void init_eval (void);
3924 extern void syms_of_eval (void);
3925 extern void unwind_body (Lisp_Object);
3926 extern ptrdiff_t record_in_backtrace (Lisp_Object, Lisp_Object *, ptrdiff_t);
3927 extern void mark_specpdl (void);
3928 extern void get_backtrace (Lisp_Object array);
3929 Lisp_Object backtrace_top_function (void);
3930 extern bool let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol);
3931 extern bool let_shadows_global_binding_p (Lisp_Object symbol);
3932
3933 #ifdef HAVE_MODULES
3934 /* Defined in alloc.c. */
3935 extern Lisp_Object make_user_ptr (void (*finalizer) (void *), void *p);
3936
3937 /* Defined in emacs-module.c. */
3938 extern void module_init (void);
3939 extern void syms_of_module (void);
3940 #endif
3941
3942 /* Defined in editfns.c. */
3943 extern void insert1 (Lisp_Object);
3944 extern Lisp_Object save_excursion_save (void);
3945 extern Lisp_Object save_restriction_save (void);
3946 extern void save_excursion_restore (Lisp_Object);
3947 extern void save_restriction_restore (Lisp_Object);
3948 extern _Noreturn void time_overflow (void);
3949 extern Lisp_Object make_buffer_string (ptrdiff_t, ptrdiff_t, bool);
3950 extern Lisp_Object make_buffer_string_both (ptrdiff_t, ptrdiff_t, ptrdiff_t,
3951 ptrdiff_t, bool);
3952 extern void init_editfns (bool);
3953 extern void syms_of_editfns (void);
3954
3955 /* Defined in buffer.c. */
3956 extern bool mouse_face_overlay_overlaps (Lisp_Object);
3957 extern _Noreturn void nsberror (Lisp_Object);
3958 extern void adjust_overlays_for_insert (ptrdiff_t, ptrdiff_t);
3959 extern void adjust_overlays_for_delete (ptrdiff_t, ptrdiff_t);
3960 extern void fix_start_end_in_overlays (ptrdiff_t, ptrdiff_t);
3961 extern void report_overlay_modification (Lisp_Object, Lisp_Object, bool,
3962 Lisp_Object, Lisp_Object, Lisp_Object);
3963 extern bool overlay_touches_p (ptrdiff_t);
3964 extern Lisp_Object other_buffer_safely (Lisp_Object);
3965 extern Lisp_Object get_truename_buffer (Lisp_Object);
3966 extern void init_buffer_once (void);
3967 extern void init_buffer (int);
3968 extern void syms_of_buffer (void);
3969 extern void keys_of_buffer (void);
3970
3971 /* Defined in marker.c. */
3972
3973 extern ptrdiff_t marker_position (Lisp_Object);
3974 extern ptrdiff_t marker_byte_position (Lisp_Object);
3975 extern void clear_charpos_cache (struct buffer *);
3976 extern ptrdiff_t buf_charpos_to_bytepos (struct buffer *, ptrdiff_t);
3977 extern ptrdiff_t buf_bytepos_to_charpos (struct buffer *, ptrdiff_t);
3978 extern void unchain_marker (struct Lisp_Marker *marker);
3979 extern Lisp_Object set_marker_restricted (Lisp_Object, Lisp_Object, Lisp_Object);
3980 extern Lisp_Object set_marker_both (Lisp_Object, Lisp_Object, ptrdiff_t, ptrdiff_t);
3981 extern Lisp_Object set_marker_restricted_both (Lisp_Object, Lisp_Object,
3982 ptrdiff_t, ptrdiff_t);
3983 extern Lisp_Object build_marker (struct buffer *, ptrdiff_t, ptrdiff_t);
3984 extern void syms_of_marker (void);
3985
3986 /* Defined in fileio.c. */
3987
3988 extern Lisp_Object expand_and_dir_to_file (Lisp_Object, Lisp_Object);
3989 extern Lisp_Object write_region (Lisp_Object, Lisp_Object, Lisp_Object,
3990 Lisp_Object, Lisp_Object, Lisp_Object,
3991 Lisp_Object, int);
3992 extern void close_file_unwind (int);
3993 extern void fclose_unwind (void *);
3994 extern void restore_point_unwind (Lisp_Object);
3995 extern _Noreturn void report_file_errno (const char *, Lisp_Object, int);
3996 extern _Noreturn void report_file_error (const char *, Lisp_Object);
3997 extern _Noreturn void report_file_notify_error (const char *, Lisp_Object);
3998 extern bool internal_delete_file (Lisp_Object);
3999 extern Lisp_Object emacs_readlinkat (int, const char *);
4000 extern bool file_directory_p (const char *);
4001 extern bool file_accessible_directory_p (Lisp_Object);
4002 extern void init_fileio (void);
4003 extern void syms_of_fileio (void);
4004 extern Lisp_Object make_temp_name (Lisp_Object, bool);
4005
4006 /* Defined in search.c. */
4007 extern void shrink_regexp_cache (void);
4008 extern void restore_search_regs (void);
4009 extern void record_unwind_save_match_data (void);
4010 struct re_registers;
4011 extern struct re_pattern_buffer *compile_pattern (Lisp_Object,
4012 struct re_registers *,
4013 Lisp_Object, bool, bool);
4014 extern ptrdiff_t fast_string_match_internal (Lisp_Object, Lisp_Object,
4015 Lisp_Object);
4016
4017 INLINE ptrdiff_t
4018 fast_string_match (Lisp_Object regexp, Lisp_Object string)
4019 {
4020 return fast_string_match_internal (regexp, string, Qnil);
4021 }
4022
4023 INLINE ptrdiff_t
4024 fast_string_match_ignore_case (Lisp_Object regexp, Lisp_Object string)
4025 {
4026 return fast_string_match_internal (regexp, string, Vascii_canon_table);
4027 }
4028
4029 extern ptrdiff_t fast_c_string_match_ignore_case (Lisp_Object, const char *,
4030 ptrdiff_t);
4031 extern ptrdiff_t fast_looking_at (Lisp_Object, ptrdiff_t, ptrdiff_t,
4032 ptrdiff_t, ptrdiff_t, Lisp_Object);
4033 extern ptrdiff_t find_newline (ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t,
4034 ptrdiff_t, ptrdiff_t *, ptrdiff_t *, bool);
4035 extern ptrdiff_t scan_newline (ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t,
4036 ptrdiff_t, bool);
4037 extern ptrdiff_t scan_newline_from_point (ptrdiff_t, ptrdiff_t *, ptrdiff_t *);
4038 extern ptrdiff_t find_newline_no_quit (ptrdiff_t, ptrdiff_t,
4039 ptrdiff_t, ptrdiff_t *);
4040 extern ptrdiff_t find_before_next_newline (ptrdiff_t, ptrdiff_t,
4041 ptrdiff_t, ptrdiff_t *);
4042 extern void syms_of_search (void);
4043 extern void clear_regexp_cache (void);
4044
4045 /* Defined in minibuf.c. */
4046
4047 extern Lisp_Object Vminibuffer_list;
4048 extern Lisp_Object last_minibuf_string;
4049 extern Lisp_Object get_minibuffer (EMACS_INT);
4050 extern void init_minibuf_once (void);
4051 extern void syms_of_minibuf (void);
4052
4053 /* Defined in callint.c. */
4054
4055 extern void syms_of_callint (void);
4056
4057 /* Defined in casefiddle.c. */
4058
4059 extern void syms_of_casefiddle (void);
4060 extern void keys_of_casefiddle (void);
4061
4062 /* Defined in casetab.c. */
4063
4064 extern void init_casetab_once (void);
4065 extern void syms_of_casetab (void);
4066
4067 /* Defined in keyboard.c. */
4068
4069 extern Lisp_Object echo_message_buffer;
4070 extern struct kboard *echo_kboard;
4071 extern void cancel_echoing (void);
4072 extern bool input_pending;
4073 #ifdef HAVE_STACK_OVERFLOW_HANDLING
4074 extern sigjmp_buf return_to_command_loop;
4075 #endif
4076 extern Lisp_Object menu_bar_items (Lisp_Object);
4077 extern Lisp_Object tool_bar_items (Lisp_Object, int *);
4078 extern void discard_mouse_events (void);
4079 #ifdef USABLE_SIGIO
4080 void handle_input_available_signal (int);
4081 #endif
4082 extern Lisp_Object pending_funcalls;
4083 extern bool detect_input_pending (void);
4084 extern bool detect_input_pending_ignore_squeezables (void);
4085 extern bool detect_input_pending_run_timers (bool);
4086 extern void safe_run_hooks (Lisp_Object);
4087 extern void cmd_error_internal (Lisp_Object, const char *);
4088 extern Lisp_Object command_loop_1 (void);
4089 extern Lisp_Object read_menu_command (void);
4090 extern Lisp_Object recursive_edit_1 (void);
4091 extern void record_auto_save (void);
4092 extern void force_auto_save_soon (void);
4093 extern void init_keyboard (void);
4094 extern void syms_of_keyboard (void);
4095 extern void keys_of_keyboard (void);
4096
4097 /* Defined in indent.c. */
4098 extern ptrdiff_t current_column (void);
4099 extern void invalidate_current_column (void);
4100 extern bool indented_beyond_p (ptrdiff_t, ptrdiff_t, EMACS_INT);
4101 extern void syms_of_indent (void);
4102
4103 /* Defined in frame.c. */
4104 extern void store_frame_param (struct frame *, Lisp_Object, Lisp_Object);
4105 extern void store_in_alist (Lisp_Object *, Lisp_Object, Lisp_Object);
4106 extern Lisp_Object do_switch_frame (Lisp_Object, int, int, Lisp_Object);
4107 extern Lisp_Object get_frame_param (struct frame *, Lisp_Object);
4108 extern void frames_discard_buffer (Lisp_Object);
4109 extern void syms_of_frame (void);
4110
4111 /* Defined in emacs.c. */
4112 extern char **initial_argv;
4113 extern int initial_argc;
4114 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NS)
4115 extern bool display_arg;
4116 #endif
4117 extern Lisp_Object decode_env_path (const char *, const char *, bool);
4118 extern Lisp_Object empty_unibyte_string, empty_multibyte_string;
4119 extern _Noreturn void terminate_due_to_signal (int, int);
4120 #ifdef WINDOWSNT
4121 extern Lisp_Object Vlibrary_cache;
4122 #endif
4123 #if HAVE_SETLOCALE
4124 void fixup_locale (void);
4125 void synchronize_system_messages_locale (void);
4126 void synchronize_system_time_locale (void);
4127 #else
4128 INLINE void fixup_locale (void) {}
4129 INLINE void synchronize_system_messages_locale (void) {}
4130 INLINE void synchronize_system_time_locale (void) {}
4131 #endif
4132 extern void shut_down_emacs (int, Lisp_Object);
4133
4134 /* True means don't do interactive redisplay and don't change tty modes. */
4135 extern bool noninteractive;
4136
4137 /* True means remove site-lisp directories from load-path. */
4138 extern bool no_site_lisp;
4139
4140 /* True means put details like time stamps into builds. */
4141 extern bool build_details;
4142
4143 /* Pipe used to send exit notification to the daemon parent at
4144 startup. On Windows, we use a kernel event instead. */
4145 #ifndef WINDOWSNT
4146 extern int daemon_pipe[2];
4147 #define IS_DAEMON (daemon_pipe[1] != 0)
4148 #define DAEMON_RUNNING (daemon_pipe[1] >= 0)
4149 #else /* WINDOWSNT */
4150 extern void *w32_daemon_event;
4151 #define IS_DAEMON (w32_daemon_event != NULL)
4152 #define DAEMON_RUNNING (w32_daemon_event != INVALID_HANDLE_VALUE)
4153 #endif
4154
4155 /* True if handling a fatal error already. */
4156 extern bool fatal_error_in_progress;
4157
4158 /* True means don't do use window-system-specific display code. */
4159 extern bool inhibit_window_system;
4160 /* True means that a filter or a sentinel is running. */
4161 extern bool running_asynch_code;
4162
4163 /* Defined in process.c. */
4164 extern void kill_buffer_processes (Lisp_Object);
4165 extern int wait_reading_process_output (intmax_t, int, int, bool, Lisp_Object,
4166 struct Lisp_Process *, int);
4167 /* Max value for the first argument of wait_reading_process_output. */
4168 #if __GNUC__ == 3 || (__GNUC__ == 4 && __GNUC_MINOR__ <= 5)
4169 /* Work around a bug in GCC 3.4.2, known to be fixed in GCC 4.6.3.
4170 The bug merely causes a bogus warning, but the warning is annoying. */
4171 # define WAIT_READING_MAX min (TYPE_MAXIMUM (time_t), INTMAX_MAX)
4172 #else
4173 # define WAIT_READING_MAX INTMAX_MAX
4174 #endif
4175 #ifdef HAVE_TIMERFD
4176 extern void add_timer_wait_descriptor (int);
4177 #endif
4178 extern void add_keyboard_wait_descriptor (int);
4179 extern void delete_keyboard_wait_descriptor (int);
4180 #ifdef HAVE_GPM
4181 extern void add_gpm_wait_descriptor (int);
4182 extern void delete_gpm_wait_descriptor (int);
4183 #endif
4184 extern void init_process_emacs (void);
4185 extern void syms_of_process (void);
4186 extern void setup_process_coding_systems (Lisp_Object);
4187
4188 /* Defined in callproc.c. */
4189 #ifndef DOS_NT
4190 _Noreturn
4191 #endif
4192 extern int child_setup (int, int, int, char **, bool, Lisp_Object);
4193 extern void init_callproc_1 (void);
4194 extern void init_callproc (void);
4195 extern void set_initial_environment (void);
4196 extern void syms_of_callproc (void);
4197
4198 /* Defined in doc.c. */
4199 enum text_quoting_style
4200 {
4201 /* Use curved single quotes ‘like this’. */
4202 CURVE_QUOTING_STYLE,
4203
4204 /* Use grave accent and apostrophe `like this'. */
4205 GRAVE_QUOTING_STYLE,
4206
4207 /* Use apostrophes 'like this'. */
4208 STRAIGHT_QUOTING_STYLE
4209 };
4210 extern enum text_quoting_style text_quoting_style (void);
4211 extern Lisp_Object read_doc_string (Lisp_Object);
4212 extern Lisp_Object get_doc_string (Lisp_Object, bool, bool);
4213 extern void syms_of_doc (void);
4214 extern int read_bytecode_char (bool);
4215
4216 /* Defined in bytecode.c. */
4217 extern void syms_of_bytecode (void);
4218 extern struct byte_stack *byte_stack_list;
4219 extern void relocate_byte_stack (void);
4220 extern Lisp_Object exec_byte_code (Lisp_Object, Lisp_Object, Lisp_Object,
4221 Lisp_Object, ptrdiff_t, Lisp_Object *);
4222 extern Lisp_Object get_byte_code_arity (Lisp_Object);
4223
4224 /* Defined in macros.c. */
4225 extern void init_macros (void);
4226 extern void syms_of_macros (void);
4227
4228 /* Defined in undo.c. */
4229 extern void truncate_undo_list (struct buffer *);
4230 extern void record_insert (ptrdiff_t, ptrdiff_t);
4231 extern void record_delete (ptrdiff_t, Lisp_Object, bool);
4232 extern void record_first_change (void);
4233 extern void record_change (ptrdiff_t, ptrdiff_t);
4234 extern void record_property_change (ptrdiff_t, ptrdiff_t,
4235 Lisp_Object, Lisp_Object,
4236 Lisp_Object);
4237 extern void syms_of_undo (void);
4238
4239 /* Defined in textprop.c. */
4240 extern void report_interval_modification (Lisp_Object, Lisp_Object);
4241
4242 /* Defined in menu.c. */
4243 extern void syms_of_menu (void);
4244
4245 /* Defined in xmenu.c. */
4246 extern void syms_of_xmenu (void);
4247
4248 /* Defined in termchar.h. */
4249 struct tty_display_info;
4250
4251 /* Defined in termhooks.h. */
4252 struct terminal;
4253
4254 /* Defined in sysdep.c. */
4255 extern void init_standard_fds (void);
4256 extern char *emacs_get_current_dir_name (void);
4257 extern void stuff_char (char c);
4258 extern void init_foreground_group (void);
4259 extern void sys_subshell (void);
4260 extern void sys_suspend (void);
4261 extern void discard_tty_input (void);
4262 extern void init_sys_modes (struct tty_display_info *);
4263 extern void reset_sys_modes (struct tty_display_info *);
4264 extern void init_all_sys_modes (void);
4265 extern void reset_all_sys_modes (void);
4266 extern void child_setup_tty (int);
4267 extern void setup_pty (int);
4268 extern int set_window_size (int, int, int);
4269 extern EMACS_INT get_random (void);
4270 extern void seed_random (void *, ptrdiff_t);
4271 extern void init_random (void);
4272 extern void emacs_backtrace (int);
4273 extern _Noreturn void emacs_abort (void) NO_INLINE;
4274 extern int emacs_open (const char *, int, int);
4275 extern int emacs_pipe (int[2]);
4276 extern int emacs_close (int);
4277 extern ptrdiff_t emacs_read (int, void *, ptrdiff_t);
4278 extern ptrdiff_t emacs_write (int, void const *, ptrdiff_t);
4279 extern ptrdiff_t emacs_write_sig (int, void const *, ptrdiff_t);
4280 extern void emacs_perror (char const *);
4281
4282 extern void unlock_all_files (void);
4283 extern void lock_file (Lisp_Object);
4284 extern void unlock_file (Lisp_Object);
4285 extern void unlock_buffer (struct buffer *);
4286 extern void syms_of_filelock (void);
4287 extern int str_collate (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object);
4288
4289 /* Defined in sound.c. */
4290 extern void syms_of_sound (void);
4291
4292 /* Defined in category.c. */
4293 extern void init_category_once (void);
4294 extern Lisp_Object char_category_set (int);
4295 extern void syms_of_category (void);
4296
4297 /* Defined in ccl.c. */
4298 extern void syms_of_ccl (void);
4299
4300 /* Defined in dired.c. */
4301 extern void syms_of_dired (void);
4302 extern Lisp_Object directory_files_internal (Lisp_Object, Lisp_Object,
4303 Lisp_Object, Lisp_Object,
4304 bool, Lisp_Object);
4305
4306 /* Defined in term.c. */
4307 extern int *char_ins_del_vector;
4308 extern void syms_of_term (void);
4309 extern _Noreturn void fatal (const char *msgid, ...)
4310 ATTRIBUTE_FORMAT_PRINTF (1, 2);
4311
4312 /* Defined in terminal.c. */
4313 extern void syms_of_terminal (void);
4314
4315 /* Defined in font.c. */
4316 extern void syms_of_font (void);
4317 extern void init_font (void);
4318
4319 #ifdef HAVE_WINDOW_SYSTEM
4320 /* Defined in fontset.c. */
4321 extern void syms_of_fontset (void);
4322 #endif
4323
4324 /* Defined in inotify.c */
4325 #ifdef HAVE_INOTIFY
4326 extern void syms_of_inotify (void);
4327 #endif
4328
4329 /* Defined in kqueue.c */
4330 #ifdef HAVE_KQUEUE
4331 extern void globals_of_kqueue (void);
4332 extern void syms_of_kqueue (void);
4333 #endif
4334
4335 /* Defined in gfilenotify.c */
4336 #ifdef HAVE_GFILENOTIFY
4337 extern void globals_of_gfilenotify (void);
4338 extern void syms_of_gfilenotify (void);
4339 #endif
4340
4341 #ifdef HAVE_W32NOTIFY
4342 /* Defined on w32notify.c. */
4343 extern void syms_of_w32notify (void);
4344 #endif
4345
4346 /* Defined in xfaces.c. */
4347 extern Lisp_Object Vface_alternative_font_family_alist;
4348 extern Lisp_Object Vface_alternative_font_registry_alist;
4349 extern void syms_of_xfaces (void);
4350
4351 #ifdef HAVE_X_WINDOWS
4352 /* Defined in xfns.c. */
4353 extern void syms_of_xfns (void);
4354
4355 /* Defined in xsmfns.c. */
4356 extern void syms_of_xsmfns (void);
4357
4358 /* Defined in xselect.c. */
4359 extern void syms_of_xselect (void);
4360
4361 /* Defined in xterm.c. */
4362 extern void init_xterm (void);
4363 extern void syms_of_xterm (void);
4364 #endif /* HAVE_X_WINDOWS */
4365
4366 #ifdef HAVE_WINDOW_SYSTEM
4367 /* Defined in xterm.c, nsterm.m, w32term.c. */
4368 extern char *x_get_keysym_name (int);
4369 #endif /* HAVE_WINDOW_SYSTEM */
4370
4371 #ifdef HAVE_LIBXML2
4372 /* Defined in xml.c. */
4373 extern void syms_of_xml (void);
4374 extern void xml_cleanup_parser (void);
4375 #endif
4376
4377 #ifdef HAVE_ZLIB
4378 /* Defined in decompress.c. */
4379 extern void syms_of_decompress (void);
4380 #endif
4381
4382 #ifdef HAVE_DBUS
4383 /* Defined in dbusbind.c. */
4384 void init_dbusbind (void);
4385 void syms_of_dbusbind (void);
4386 #endif
4387
4388
4389 /* Defined in profiler.c. */
4390 extern bool profiler_memory_running;
4391 extern void malloc_probe (size_t);
4392 extern void syms_of_profiler (void);
4393
4394
4395 #ifdef DOS_NT
4396 /* Defined in msdos.c, w32.c. */
4397 extern char *emacs_root_dir (void);
4398 #endif /* DOS_NT */
4399
4400 /* Defined in lastfile.c. */
4401 extern char my_edata[];
4402 extern char my_endbss[];
4403 extern char *my_endbss_static;
4404
4405 /* True means ^G can quit instantly. */
4406 extern bool immediate_quit;
4407
4408 extern void *xmalloc (size_t) ATTRIBUTE_MALLOC_SIZE ((1));
4409 extern void *xzalloc (size_t) ATTRIBUTE_MALLOC_SIZE ((1));
4410 extern void *xrealloc (void *, size_t) ATTRIBUTE_ALLOC_SIZE ((2));
4411 extern void xfree (void *);
4412 extern void *xnmalloc (ptrdiff_t, ptrdiff_t) ATTRIBUTE_MALLOC_SIZE ((1,2));
4413 extern void *xnrealloc (void *, ptrdiff_t, ptrdiff_t)
4414 ATTRIBUTE_ALLOC_SIZE ((2,3));
4415 extern void *xpalloc (void *, ptrdiff_t *, ptrdiff_t, ptrdiff_t, ptrdiff_t);
4416
4417 extern char *xstrdup (const char *) ATTRIBUTE_MALLOC;
4418 extern char *xlispstrdup (Lisp_Object) ATTRIBUTE_MALLOC;
4419 extern void dupstring (char **, char const *);
4420
4421 /* Make DEST a copy of STRING's data. Return a pointer to DEST's terminating
4422 null byte. This is like stpcpy, except the source is a Lisp string. */
4423
4424 INLINE char *
4425 lispstpcpy (char *dest, Lisp_Object string)
4426 {
4427 ptrdiff_t len = SBYTES (string);
4428 memcpy (dest, SDATA (string), len + 1);
4429 return dest + len;
4430 }
4431
4432 extern void xputenv (const char *);
4433
4434 extern char *egetenv_internal (const char *, ptrdiff_t);
4435
4436 INLINE char *
4437 egetenv (const char *var)
4438 {
4439 /* When VAR is a string literal, strlen can be optimized away. */
4440 return egetenv_internal (var, strlen (var));
4441 }
4442
4443 /* Set up the name of the machine we're running on. */
4444 extern void init_system_name (void);
4445
4446 /* Return the absolute value of X. X should be a signed integer
4447 expression without side effects, and X's absolute value should not
4448 exceed the maximum for its promoted type. This is called 'eabs'
4449 because 'abs' is reserved by the C standard. */
4450 #define eabs(x) ((x) < 0 ? -(x) : (x))
4451
4452 /* Return a fixnum or float, depending on whether VAL fits in a Lisp
4453 fixnum. */
4454
4455 #define make_fixnum_or_float(val) \
4456 (FIXNUM_OVERFLOW_P (val) ? make_float (val) : make_number (val))
4457
4458 /* SAFE_ALLOCA normally allocates memory on the stack, but if size is
4459 larger than MAX_ALLOCA, use xmalloc to avoid overflowing the stack. */
4460
4461 enum MAX_ALLOCA { MAX_ALLOCA = 16 * 1024 };
4462
4463 extern void *record_xmalloc (size_t) ATTRIBUTE_ALLOC_SIZE ((1));
4464
4465 #define USE_SAFE_ALLOCA \
4466 ptrdiff_t sa_avail = MAX_ALLOCA; \
4467 ptrdiff_t sa_count = SPECPDL_INDEX (); bool sa_must_free = false
4468
4469 #define AVAIL_ALLOCA(size) (sa_avail -= (size), alloca (size))
4470
4471 /* SAFE_ALLOCA allocates a simple buffer. */
4472
4473 #define SAFE_ALLOCA(size) ((size) <= sa_avail \
4474 ? AVAIL_ALLOCA (size) \
4475 : (sa_must_free = true, record_xmalloc (size)))
4476
4477 /* SAFE_NALLOCA sets BUF to a newly allocated array of MULTIPLIER *
4478 NITEMS items, each of the same type as *BUF. MULTIPLIER must
4479 positive. The code is tuned for MULTIPLIER being a constant. */
4480
4481 #define SAFE_NALLOCA(buf, multiplier, nitems) \
4482 do { \
4483 if ((nitems) <= sa_avail / sizeof *(buf) / (multiplier)) \
4484 (buf) = AVAIL_ALLOCA (sizeof *(buf) * (multiplier) * (nitems)); \
4485 else \
4486 { \
4487 (buf) = xnmalloc (nitems, sizeof *(buf) * (multiplier)); \
4488 sa_must_free = true; \
4489 record_unwind_protect_ptr (xfree, buf); \
4490 } \
4491 } while (false)
4492
4493 /* SAFE_ALLOCA_STRING allocates a C copy of a Lisp string. */
4494
4495 #define SAFE_ALLOCA_STRING(ptr, string) \
4496 do { \
4497 (ptr) = SAFE_ALLOCA (SBYTES (string) + 1); \
4498 memcpy (ptr, SDATA (string), SBYTES (string) + 1); \
4499 } while (false)
4500
4501 /* SAFE_FREE frees xmalloced memory and enables GC as needed. */
4502
4503 #define SAFE_FREE() \
4504 do { \
4505 if (sa_must_free) { \
4506 sa_must_free = false; \
4507 unbind_to (sa_count, Qnil); \
4508 } \
4509 } while (false)
4510
4511 /* SAFE_ALLOCA_LISP allocates an array of Lisp_Objects. */
4512
4513 #define SAFE_ALLOCA_LISP(buf, nelt) \
4514 do { \
4515 ptrdiff_t alloca_nbytes; \
4516 if (INT_MULTIPLY_WRAPV (nelt, word_size, &alloca_nbytes) \
4517 || SIZE_MAX < alloca_nbytes) \
4518 memory_full (SIZE_MAX); \
4519 else if (alloca_nbytes <= sa_avail) \
4520 (buf) = AVAIL_ALLOCA (alloca_nbytes); \
4521 else \
4522 { \
4523 Lisp_Object arg_; \
4524 (buf) = xmalloc (alloca_nbytes); \
4525 arg_ = make_save_memory (buf, nelt); \
4526 sa_must_free = true; \
4527 record_unwind_protect (free_save_value, arg_); \
4528 } \
4529 } while (false)
4530
4531
4532 /* If USE_STACK_LISP_OBJECTS, define macros that and functions that allocate
4533 block-scoped conses and strings. These objects are not
4534 managed by the garbage collector, so they are dangerous: passing them
4535 out of their scope (e.g., to user code) results in undefined behavior.
4536 Conversely, they have better performance because GC is not involved.
4537
4538 This feature is experimental and requires careful debugging.
4539 Build with CPPFLAGS='-DUSE_STACK_LISP_OBJECTS=0' to disable it. */
4540
4541 #if (!defined USE_STACK_LISP_OBJECTS \
4542 && defined __GNUC__ && !defined __clang__ \
4543 && !(4 < __GNUC__ + (3 < __GNUC_MINOR__ + (2 <= __GNUC_PATCHLEVEL__))))
4544 /* Work around GCC bugs 36584 and 35271, which were fixed in GCC 4.3.2. */
4545 # define USE_STACK_LISP_OBJECTS false
4546 #endif
4547 #ifndef USE_STACK_LISP_OBJECTS
4548 # define USE_STACK_LISP_OBJECTS true
4549 #endif
4550
4551 #ifdef GC_CHECK_STRING_BYTES
4552 enum { defined_GC_CHECK_STRING_BYTES = true };
4553 #else
4554 enum { defined_GC_CHECK_STRING_BYTES = false };
4555 #endif
4556
4557 /* Struct inside unions that are typically no larger and aligned enough. */
4558
4559 union Aligned_Cons
4560 {
4561 struct Lisp_Cons s;
4562 double d; intmax_t i; void *p;
4563 };
4564
4565 union Aligned_String
4566 {
4567 struct Lisp_String s;
4568 double d; intmax_t i; void *p;
4569 };
4570
4571 /* True for stack-based cons and string implementations, respectively.
4572 Use stack-based strings only if stack-based cons also works.
4573 Otherwise, STACK_CONS would create heap-based cons cells that
4574 could point to stack-based strings, which is a no-no. */
4575
4576 enum
4577 {
4578 USE_STACK_CONS = (USE_STACK_LISP_OBJECTS
4579 && alignof (union Aligned_Cons) % GCALIGNMENT == 0),
4580 USE_STACK_STRING = (USE_STACK_CONS
4581 && !defined_GC_CHECK_STRING_BYTES
4582 && alignof (union Aligned_String) % GCALIGNMENT == 0)
4583 };
4584
4585 /* Auxiliary macros used for auto allocation of Lisp objects. Please
4586 use these only in macros like AUTO_CONS that declare a local
4587 variable whose lifetime will be clear to the programmer. */
4588 #define STACK_CONS(a, b) \
4589 make_lisp_ptr (&(union Aligned_Cons) { { a, { b } } }.s, Lisp_Cons)
4590 #define AUTO_CONS_EXPR(a, b) \
4591 (USE_STACK_CONS ? STACK_CONS (a, b) : Fcons (a, b))
4592
4593 /* Declare NAME as an auto Lisp cons or short list if possible, a
4594 GC-based one otherwise. This is in the sense of the C keyword
4595 'auto'; i.e., the object has the lifetime of the containing block.
4596 The resulting object should not be made visible to user Lisp code. */
4597
4598 #define AUTO_CONS(name, a, b) Lisp_Object name = AUTO_CONS_EXPR (a, b)
4599 #define AUTO_LIST1(name, a) \
4600 Lisp_Object name = (USE_STACK_CONS ? STACK_CONS (a, Qnil) : list1 (a))
4601 #define AUTO_LIST2(name, a, b) \
4602 Lisp_Object name = (USE_STACK_CONS \
4603 ? STACK_CONS (a, STACK_CONS (b, Qnil)) \
4604 : list2 (a, b))
4605 #define AUTO_LIST3(name, a, b, c) \
4606 Lisp_Object name = (USE_STACK_CONS \
4607 ? STACK_CONS (a, STACK_CONS (b, STACK_CONS (c, Qnil))) \
4608 : list3 (a, b, c))
4609 #define AUTO_LIST4(name, a, b, c, d) \
4610 Lisp_Object name \
4611 = (USE_STACK_CONS \
4612 ? STACK_CONS (a, STACK_CONS (b, STACK_CONS (c, \
4613 STACK_CONS (d, Qnil)))) \
4614 : list4 (a, b, c, d))
4615
4616 /* Declare NAME as an auto Lisp string if possible, a GC-based one if not.
4617 Take its unibyte value from the null-terminated string STR,
4618 an expression that should not have side effects.
4619 STR's value is not necessarily copied. The resulting Lisp string
4620 should not be modified or made visible to user code. */
4621
4622 #define AUTO_STRING(name, str) \
4623 AUTO_STRING_WITH_LEN (name, str, strlen (str))
4624
4625 /* Declare NAME as an auto Lisp string if possible, a GC-based one if not.
4626 Take its unibyte value from the null-terminated string STR with length LEN.
4627 STR may have side effects and may contain null bytes.
4628 STR's value is not necessarily copied. The resulting Lisp string
4629 should not be modified or made visible to user code. */
4630
4631 #define AUTO_STRING_WITH_LEN(name, str, len) \
4632 Lisp_Object name = \
4633 (USE_STACK_STRING \
4634 ? (make_lisp_ptr \
4635 ((&(union Aligned_String) \
4636 {{len, -1, 0, (unsigned char *) (str)}}.s), \
4637 Lisp_String)) \
4638 : make_unibyte_string (str, len))
4639
4640 /* Loop over all tails of a list, checking for cycles.
4641 FIXME: Make tortoise and n internal declarations.
4642 FIXME: Unroll the loop body so we don't need `n'. */
4643 #define FOR_EACH_TAIL(hare, list, tortoise, n) \
4644 for ((tortoise) = (hare) = (list), (n) = true; \
4645 CONSP (hare); \
4646 (hare = XCDR (hare), (n) = !(n), \
4647 ((n) \
4648 ? (EQ (hare, tortoise) \
4649 ? xsignal1 (Qcircular_list, list) \
4650 : (void) 0) \
4651 /* Move tortoise before the next iteration, in case */ \
4652 /* the next iteration does an Fsetcdr. */ \
4653 : (void) ((tortoise) = XCDR (tortoise)))))
4654
4655 /* Do a `for' loop over alist values. */
4656
4657 #define FOR_EACH_ALIST_VALUE(head_var, list_var, value_var) \
4658 for ((list_var) = (head_var); \
4659 (CONSP (list_var) && ((value_var) = XCDR (XCAR (list_var)), true)); \
4660 (list_var) = XCDR (list_var))
4661
4662 /* Check whether it's time for GC, and run it if so. */
4663
4664 INLINE void
4665 maybe_gc (void)
4666 {
4667 if ((consing_since_gc > gc_cons_threshold
4668 && consing_since_gc > gc_relative_threshold)
4669 || (!NILP (Vmemory_full)
4670 && consing_since_gc > memory_full_cons_threshold))
4671 Fgarbage_collect ();
4672 }
4673
4674 INLINE bool
4675 functionp (Lisp_Object object)
4676 {
4677 if (SYMBOLP (object) && !NILP (Ffboundp (object)))
4678 {
4679 object = Findirect_function (object, Qt);
4680
4681 if (CONSP (object) && EQ (XCAR (object), Qautoload))
4682 {
4683 /* Autoloaded symbols are functions, except if they load
4684 macros or keymaps. */
4685 int i;
4686 for (i = 0; i < 4 && CONSP (object); i++)
4687 object = XCDR (object);
4688
4689 return ! (CONSP (object) && !NILP (XCAR (object)));
4690 }
4691 }
4692
4693 if (SUBRP (object))
4694 return XSUBR (object)->max_args != UNEVALLED;
4695 else if (COMPILEDP (object))
4696 return true;
4697 else if (CONSP (object))
4698 {
4699 Lisp_Object car = XCAR (object);
4700 return EQ (car, Qlambda) || EQ (car, Qclosure);
4701 }
4702 else
4703 return false;
4704 }
4705
4706 INLINE_HEADER_END
4707
4708 #endif /* EMACS_LISP_H */