428
|
1 /* Primitive operations on Lisp data types for XEmacs Lisp interpreter.
|
|
2 Copyright (C) 1985, 1986, 1988, 1992, 1993, 1994, 1995
|
|
3 Free Software Foundation, Inc.
|
1330
|
4 Copyright (C) 2000, 2001, 2002, 2003 Ben Wing.
|
428
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
|
23 /* Synched up with: Mule 2.0, FSF 19.30. Some of FSF's data.c is in
|
|
24 XEmacs' symbols.c. */
|
|
25
|
|
26 /* This file has been Mule-ized. */
|
|
27
|
|
28 #include <config.h>
|
|
29 #include "lisp.h"
|
|
30
|
|
31 #include "buffer.h"
|
|
32 #include "bytecode.h"
|
|
33 #include "syssignal.h"
|
771
|
34 #include "sysfloat.h"
|
428
|
35
|
|
36 Lisp_Object Qnil, Qt, Qquote, Qlambda, Qunbound;
|
|
37 Lisp_Object Qerror_conditions, Qerror_message;
|
442
|
38 Lisp_Object Qerror, Qquit, Qsyntax_error, Qinvalid_read_syntax;
|
563
|
39 Lisp_Object Qlist_formation_error, Qstructure_formation_error;
|
442
|
40 Lisp_Object Qmalformed_list, Qmalformed_property_list;
|
|
41 Lisp_Object Qcircular_list, Qcircular_property_list;
|
563
|
42 Lisp_Object Qinvalid_argument, Qinvalid_constant, Qwrong_type_argument;
|
|
43 Lisp_Object Qargs_out_of_range;
|
442
|
44 Lisp_Object Qwrong_number_of_arguments, Qinvalid_function, Qno_catch;
|
563
|
45 Lisp_Object Qinternal_error, Qinvalid_state, Qstack_overflow, Qout_of_memory;
|
428
|
46 Lisp_Object Qvoid_variable, Qcyclic_variable_indirection;
|
|
47 Lisp_Object Qvoid_function, Qcyclic_function_indirection;
|
563
|
48 Lisp_Object Qinvalid_operation, Qinvalid_change, Qprinting_unreadable_object;
|
442
|
49 Lisp_Object Qsetting_constant;
|
|
50 Lisp_Object Qediting_error;
|
|
51 Lisp_Object Qbeginning_of_buffer, Qend_of_buffer, Qbuffer_read_only;
|
563
|
52 Lisp_Object Qio_error, Qfile_error, Qconversion_error, Qend_of_file;
|
580
|
53 Lisp_Object Qtext_conversion_error;
|
428
|
54 Lisp_Object Qarith_error, Qrange_error, Qdomain_error;
|
|
55 Lisp_Object Qsingularity_error, Qoverflow_error, Qunderflow_error;
|
1983
|
56 Lisp_Object Qintegerp, Qnatnump, Qnonnegativep, Qsymbolp;
|
428
|
57 Lisp_Object Qlistp, Qtrue_list_p, Qweak_listp;
|
|
58 Lisp_Object Qconsp, Qsubrp;
|
|
59 Lisp_Object Qcharacterp, Qstringp, Qarrayp, Qsequencep, Qvectorp;
|
|
60 Lisp_Object Qchar_or_string_p, Qmarkerp, Qinteger_or_marker_p, Qbufferp;
|
|
61 Lisp_Object Qinteger_or_char_p, Qinteger_char_or_marker_p;
|
|
62 Lisp_Object Qnumberp, Qnumber_char_or_marker_p;
|
|
63 Lisp_Object Qbit_vectorp, Qbitp, Qcdr;
|
|
64
|
563
|
65 Lisp_Object Qerror_lacks_explanatory_string;
|
428
|
66 Lisp_Object Qfloatp;
|
|
67
|
|
68 #ifdef DEBUG_XEMACS
|
|
69
|
|
70 int debug_issue_ebola_notices;
|
|
71
|
458
|
72 Fixnum debug_ebola_backtrace_length;
|
428
|
73
|
|
74 int
|
|
75 eq_with_ebola_notice (Lisp_Object obj1, Lisp_Object obj2)
|
|
76 {
|
|
77 if (debug_issue_ebola_notices
|
|
78 && ((CHARP (obj1) && INTP (obj2)) || (CHARP (obj2) && INTP (obj1))))
|
|
79 {
|
|
80 /* #### It would be really nice if this were a proper warning
|
1551
|
81 instead of brain-dead print to Qexternal_debugging_output. */
|
826
|
82 write_c_string
|
|
83 (Qexternal_debugging_output,
|
|
84 "Comparison between integer and character is constant nil (");
|
428
|
85 Fprinc (obj1, Qexternal_debugging_output);
|
826
|
86 write_c_string (Qexternal_debugging_output, " and ");
|
428
|
87 Fprinc (obj2, Qexternal_debugging_output);
|
826
|
88 write_c_string (Qexternal_debugging_output, ")\n");
|
428
|
89 debug_short_backtrace (debug_ebola_backtrace_length);
|
|
90 }
|
|
91 return EQ (obj1, obj2);
|
|
92 }
|
|
93
|
|
94 #endif /* DEBUG_XEMACS */
|
|
95
|
|
96
|
|
97
|
|
98 Lisp_Object
|
|
99 wrong_type_argument (Lisp_Object predicate, Lisp_Object value)
|
|
100 {
|
|
101 /* This function can GC */
|
|
102 REGISTER Lisp_Object tem;
|
|
103 do
|
|
104 {
|
|
105 value = Fsignal (Qwrong_type_argument, list2 (predicate, value));
|
|
106 tem = call1 (predicate, value);
|
|
107 }
|
|
108 while (NILP (tem));
|
|
109 return value;
|
|
110 }
|
|
111
|
|
112 DOESNT_RETURN
|
|
113 dead_wrong_type_argument (Lisp_Object predicate, Lisp_Object value)
|
|
114 {
|
563
|
115 signal_error_1 (Qwrong_type_argument, list2 (predicate, value));
|
428
|
116 }
|
|
117
|
|
118 DEFUN ("wrong-type-argument", Fwrong_type_argument, 2, 2, 0, /*
|
|
119 Signal an error until the correct type value is given by the user.
|
|
120 This function loops, signalling a continuable `wrong-type-argument' error
|
|
121 with PREDICATE and VALUE as the data associated with the error and then
|
|
122 calling PREDICATE on the returned value, until the value gotten satisfies
|
|
123 PREDICATE. At that point, the gotten value is returned.
|
|
124 */
|
|
125 (predicate, value))
|
|
126 {
|
|
127 return wrong_type_argument (predicate, value);
|
|
128 }
|
|
129
|
|
130 DOESNT_RETURN
|
|
131 c_write_error (Lisp_Object obj)
|
|
132 {
|
563
|
133 signal_error (Qsetting_constant,
|
|
134 "Attempt to modify read-only object (c)", obj);
|
428
|
135 }
|
|
136
|
|
137 DOESNT_RETURN
|
|
138 lisp_write_error (Lisp_Object obj)
|
|
139 {
|
563
|
140 signal_error (Qsetting_constant,
|
|
141 "Attempt to modify read-only object (lisp)", obj);
|
428
|
142 }
|
|
143
|
|
144 DOESNT_RETURN
|
|
145 args_out_of_range (Lisp_Object a1, Lisp_Object a2)
|
|
146 {
|
563
|
147 signal_error_1 (Qargs_out_of_range, list2 (a1, a2));
|
428
|
148 }
|
|
149
|
|
150 DOESNT_RETURN
|
|
151 args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
|
|
152 {
|
563
|
153 signal_error_1 (Qargs_out_of_range, list3 (a1, a2, a3));
|
428
|
154 }
|
|
155
|
|
156 void
|
|
157 check_int_range (EMACS_INT val, EMACS_INT min, EMACS_INT max)
|
|
158 {
|
|
159 if (val < min || val > max)
|
|
160 args_out_of_range_3 (make_int (val), make_int (min), make_int (max));
|
|
161 }
|
|
162
|
|
163 /* On some machines, XINT needs a temporary location.
|
|
164 Here it is, in case it is needed. */
|
|
165
|
|
166 EMACS_INT sign_extend_temp;
|
|
167
|
|
168 /* On a few machines, XINT can only be done by calling this. */
|
|
169 /* XEmacs: only used by m/convex.h */
|
|
170 EMACS_INT sign_extend_lisp_int (EMACS_INT num);
|
|
171 EMACS_INT
|
|
172 sign_extend_lisp_int (EMACS_INT num)
|
|
173 {
|
|
174 if (num & (1L << (VALBITS - 1)))
|
|
175 return num | ((-1L) << VALBITS);
|
|
176 else
|
|
177 return num & ((1L << VALBITS) - 1);
|
|
178 }
|
|
179
|
|
180
|
|
181 /* Data type predicates */
|
|
182
|
|
183 DEFUN ("eq", Feq, 2, 2, 0, /*
|
|
184 Return t if the two args are the same Lisp object.
|
|
185 */
|
444
|
186 (object1, object2))
|
428
|
187 {
|
444
|
188 return EQ_WITH_EBOLA_NOTICE (object1, object2) ? Qt : Qnil;
|
428
|
189 }
|
|
190
|
|
191 DEFUN ("old-eq", Fold_eq, 2, 2, 0, /*
|
|
192 Return t if the two args are (in most cases) the same Lisp object.
|
|
193
|
|
194 Special kludge: A character is considered `old-eq' to its equivalent integer
|
|
195 even though they are not the same object and are in fact of different
|
|
196 types. This is ABSOLUTELY AND UTTERLY HORRENDOUS but is necessary to
|
|
197 preserve byte-code compatibility with v19. This kludge is known as the
|
|
198 \"char-int confoundance disease\" and appears in a number of other
|
|
199 functions with `old-foo' equivalents.
|
|
200
|
|
201 Do not use this function!
|
|
202 */
|
444
|
203 (object1, object2))
|
428
|
204 {
|
|
205 /* #### blasphemy */
|
444
|
206 return HACKEQ_UNSAFE (object1, object2) ? Qt : Qnil;
|
428
|
207 }
|
|
208
|
|
209 DEFUN ("null", Fnull, 1, 1, 0, /*
|
|
210 Return t if OBJECT is nil.
|
|
211 */
|
|
212 (object))
|
|
213 {
|
|
214 return NILP (object) ? Qt : Qnil;
|
|
215 }
|
|
216
|
|
217 DEFUN ("consp", Fconsp, 1, 1, 0, /*
|
|
218 Return t if OBJECT is a cons cell. `nil' is not a cons cell.
|
|
219 */
|
|
220 (object))
|
|
221 {
|
|
222 return CONSP (object) ? Qt : Qnil;
|
|
223 }
|
|
224
|
|
225 DEFUN ("atom", Fatom, 1, 1, 0, /*
|
|
226 Return t if OBJECT is not a cons cell. `nil' is not a cons cell.
|
|
227 */
|
|
228 (object))
|
|
229 {
|
|
230 return CONSP (object) ? Qnil : Qt;
|
|
231 }
|
|
232
|
|
233 DEFUN ("listp", Flistp, 1, 1, 0, /*
|
|
234 Return t if OBJECT is a list. `nil' is a list.
|
|
235 */
|
|
236 (object))
|
|
237 {
|
|
238 return LISTP (object) ? Qt : Qnil;
|
|
239 }
|
|
240
|
|
241 DEFUN ("nlistp", Fnlistp, 1, 1, 0, /*
|
|
242 Return t if OBJECT is not a list. `nil' is a list.
|
|
243 */
|
|
244 (object))
|
|
245 {
|
|
246 return LISTP (object) ? Qnil : Qt;
|
|
247 }
|
|
248
|
|
249 DEFUN ("true-list-p", Ftrue_list_p, 1, 1, 0, /*
|
1551
|
250 Return t if OBJECT is an acyclic, nil-terminated (ie, not dotted), list.
|
428
|
251 */
|
|
252 (object))
|
|
253 {
|
|
254 return TRUE_LIST_P (object) ? Qt : Qnil;
|
|
255 }
|
|
256
|
|
257 DEFUN ("symbolp", Fsymbolp, 1, 1, 0, /*
|
|
258 Return t if OBJECT is a symbol.
|
|
259 */
|
|
260 (object))
|
|
261 {
|
|
262 return SYMBOLP (object) ? Qt : Qnil;
|
|
263 }
|
|
264
|
|
265 DEFUN ("keywordp", Fkeywordp, 1, 1, 0, /*
|
|
266 Return t if OBJECT is a keyword.
|
|
267 */
|
|
268 (object))
|
|
269 {
|
|
270 return KEYWORDP (object) ? Qt : Qnil;
|
|
271 }
|
|
272
|
|
273 DEFUN ("vectorp", Fvectorp, 1, 1, 0, /*
|
|
274 Return t if OBJECT is a vector.
|
|
275 */
|
|
276 (object))
|
|
277 {
|
|
278 return VECTORP (object) ? Qt : Qnil;
|
|
279 }
|
|
280
|
|
281 DEFUN ("bit-vector-p", Fbit_vector_p, 1, 1, 0, /*
|
|
282 Return t if OBJECT is a bit vector.
|
|
283 */
|
|
284 (object))
|
|
285 {
|
|
286 return BIT_VECTORP (object) ? Qt : Qnil;
|
|
287 }
|
|
288
|
|
289 DEFUN ("stringp", Fstringp, 1, 1, 0, /*
|
|
290 Return t if OBJECT is a string.
|
|
291 */
|
|
292 (object))
|
|
293 {
|
|
294 return STRINGP (object) ? Qt : Qnil;
|
|
295 }
|
|
296
|
|
297 DEFUN ("arrayp", Farrayp, 1, 1, 0, /*
|
|
298 Return t if OBJECT is an array (string, vector, or bit vector).
|
|
299 */
|
|
300 (object))
|
|
301 {
|
|
302 return (VECTORP (object) ||
|
|
303 STRINGP (object) ||
|
|
304 BIT_VECTORP (object))
|
|
305 ? Qt : Qnil;
|
|
306 }
|
|
307
|
|
308 DEFUN ("sequencep", Fsequencep, 1, 1, 0, /*
|
|
309 Return t if OBJECT is a sequence (list or array).
|
|
310 */
|
|
311 (object))
|
|
312 {
|
|
313 return (LISTP (object) ||
|
|
314 VECTORP (object) ||
|
|
315 STRINGP (object) ||
|
|
316 BIT_VECTORP (object))
|
|
317 ? Qt : Qnil;
|
|
318 }
|
|
319
|
|
320 DEFUN ("markerp", Fmarkerp, 1, 1, 0, /*
|
|
321 Return t if OBJECT is a marker (editor pointer).
|
|
322 */
|
|
323 (object))
|
|
324 {
|
|
325 return MARKERP (object) ? Qt : Qnil;
|
|
326 }
|
|
327
|
|
328 DEFUN ("subrp", Fsubrp, 1, 1, 0, /*
|
|
329 Return t if OBJECT is a built-in function.
|
|
330 */
|
|
331 (object))
|
|
332 {
|
|
333 return SUBRP (object) ? Qt : Qnil;
|
|
334 }
|
|
335
|
|
336 DEFUN ("subr-min-args", Fsubr_min_args, 1, 1, 0, /*
|
|
337 Return minimum number of args built-in function SUBR may be called with.
|
|
338 */
|
|
339 (subr))
|
|
340 {
|
|
341 CHECK_SUBR (subr);
|
|
342 return make_int (XSUBR (subr)->min_args);
|
|
343 }
|
|
344
|
|
345 DEFUN ("subr-max-args", Fsubr_max_args, 1, 1, 0, /*
|
|
346 Return maximum number of args built-in function SUBR may be called with,
|
|
347 or nil if it takes an arbitrary number of arguments or is a special form.
|
|
348 */
|
|
349 (subr))
|
|
350 {
|
|
351 int nargs;
|
|
352 CHECK_SUBR (subr);
|
|
353 nargs = XSUBR (subr)->max_args;
|
|
354 if (nargs == MANY || nargs == UNEVALLED)
|
|
355 return Qnil;
|
|
356 else
|
|
357 return make_int (nargs);
|
|
358 }
|
|
359
|
|
360 DEFUN ("subr-interactive", Fsubr_interactive, 1, 1, 0, /*
|
444
|
361 Return the interactive spec of the subr object SUBR, or nil.
|
428
|
362 If non-nil, the return value will be a list whose first element is
|
|
363 `interactive' and whose second element is the interactive spec.
|
|
364 */
|
|
365 (subr))
|
|
366 {
|
867
|
367 const CIbyte *prompt;
|
428
|
368 CHECK_SUBR (subr);
|
|
369 prompt = XSUBR (subr)->prompt;
|
771
|
370 return prompt ? list2 (Qinteractive, build_msg_string (prompt)) : Qnil;
|
428
|
371 }
|
|
372
|
|
373
|
|
374 DEFUN ("characterp", Fcharacterp, 1, 1, 0, /*
|
|
375 Return t if OBJECT is a character.
|
|
376 Unlike in XEmacs v19 and FSF Emacs, a character is its own primitive type.
|
|
377 Any character can be converted into an equivalent integer using
|
|
378 `char-int'. To convert the other way, use `int-char'; however,
|
|
379 only some integers can be converted into characters. Such an integer
|
|
380 is called a `char-int'; see `char-int-p'.
|
|
381
|
|
382 Some functions that work on integers (e.g. the comparison functions
|
|
383 <, <=, =, /=, etc. and the arithmetic functions +, -, *, etc.)
|
|
384 accept characters and implicitly convert them into integers. In
|
|
385 general, functions that work on characters also accept char-ints and
|
|
386 implicitly convert them into characters. WARNING: Neither of these
|
|
387 behaviors is very desirable, and they are maintained for backward
|
|
388 compatibility with old E-Lisp programs that confounded characters and
|
|
389 integers willy-nilly. These behaviors may change in the future; therefore,
|
|
390 do not rely on them. Instead, use the character-specific functions such
|
|
391 as `char='.
|
|
392 */
|
|
393 (object))
|
|
394 {
|
|
395 return CHARP (object) ? Qt : Qnil;
|
|
396 }
|
|
397
|
|
398 DEFUN ("char-to-int", Fchar_to_int, 1, 1, 0, /*
|
444
|
399 Convert CHARACTER into an equivalent integer.
|
428
|
400 The resulting integer will always be non-negative. The integers in
|
|
401 the range 0 - 255 map to characters as follows:
|
|
402
|
|
403 0 - 31 Control set 0
|
|
404 32 - 127 ASCII
|
|
405 128 - 159 Control set 1
|
|
406 160 - 255 Right half of ISO-8859-1
|
|
407
|
|
408 If support for Mule does not exist, these are the only valid character
|
|
409 values. When Mule support exists, the values assigned to other characters
|
|
410 may vary depending on the particular version of XEmacs, the order in which
|
|
411 character sets were loaded, etc., and you should not depend on them.
|
|
412 */
|
444
|
413 (character))
|
428
|
414 {
|
444
|
415 CHECK_CHAR (character);
|
|
416 return make_int (XCHAR (character));
|
428
|
417 }
|
|
418
|
|
419 DEFUN ("int-to-char", Fint_to_char, 1, 1, 0, /*
|
444
|
420 Convert integer INTEGER into the equivalent character.
|
428
|
421 Not all integers correspond to valid characters; use `char-int-p' to
|
|
422 determine whether this is the case. If the integer cannot be converted,
|
|
423 nil is returned.
|
|
424 */
|
|
425 (integer))
|
|
426 {
|
|
427 CHECK_INT (integer);
|
|
428 if (CHAR_INTP (integer))
|
|
429 return make_char (XINT (integer));
|
|
430 else
|
|
431 return Qnil;
|
|
432 }
|
|
433
|
|
434 DEFUN ("char-int-p", Fchar_int_p, 1, 1, 0, /*
|
|
435 Return t if OBJECT is an integer that can be converted into a character.
|
|
436 See `char-int'.
|
|
437 */
|
|
438 (object))
|
|
439 {
|
|
440 return CHAR_INTP (object) ? Qt : Qnil;
|
|
441 }
|
|
442
|
|
443 DEFUN ("char-or-char-int-p", Fchar_or_char_int_p, 1, 1, 0, /*
|
|
444 Return t if OBJECT is a character or an integer that can be converted into one.
|
|
445 */
|
|
446 (object))
|
|
447 {
|
|
448 return CHAR_OR_CHAR_INTP (object) ? Qt : Qnil;
|
|
449 }
|
|
450
|
|
451 DEFUN ("char-or-string-p", Fchar_or_string_p, 1, 1, 0, /*
|
|
452 Return t if OBJECT is a character (or a char-int) or a string.
|
|
453 It is semi-hateful that we allow a char-int here, as it goes against
|
|
454 the name of this function, but it makes the most sense considering the
|
|
455 other steps we take to maintain compatibility with the old character/integer
|
|
456 confoundedness in older versions of E-Lisp.
|
|
457 */
|
|
458 (object))
|
|
459 {
|
|
460 return CHAR_OR_CHAR_INTP (object) || STRINGP (object) ? Qt : Qnil;
|
|
461 }
|
|
462
|
1983
|
463 #ifdef HAVE_BIGNUM
|
|
464 /* In this case, integerp is defined in number.c. */
|
|
465 DEFUN ("fixnump", Ffixnump, 1, 1, 0, /*
|
|
466 Return t if OBJECT is a fixnum.
|
|
467 */
|
|
468 (object))
|
|
469 {
|
|
470 return INTP (object) ? Qt : Qnil;
|
|
471 }
|
|
472 #else
|
428
|
473 DEFUN ("integerp", Fintegerp, 1, 1, 0, /*
|
|
474 Return t if OBJECT is an integer.
|
|
475 */
|
|
476 (object))
|
|
477 {
|
|
478 return INTP (object) ? Qt : Qnil;
|
|
479 }
|
1983
|
480 #endif
|
428
|
481
|
|
482 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, 1, 1, 0, /*
|
|
483 Return t if OBJECT is an integer or a marker (editor pointer).
|
|
484 */
|
|
485 (object))
|
|
486 {
|
|
487 return INTP (object) || MARKERP (object) ? Qt : Qnil;
|
|
488 }
|
|
489
|
|
490 DEFUN ("integer-or-char-p", Finteger_or_char_p, 1, 1, 0, /*
|
|
491 Return t if OBJECT is an integer or a character.
|
|
492 */
|
|
493 (object))
|
|
494 {
|
|
495 return INTP (object) || CHARP (object) ? Qt : Qnil;
|
|
496 }
|
|
497
|
|
498 DEFUN ("integer-char-or-marker-p", Finteger_char_or_marker_p, 1, 1, 0, /*
|
|
499 Return t if OBJECT is an integer, character or a marker (editor pointer).
|
|
500 */
|
|
501 (object))
|
|
502 {
|
|
503 return INTP (object) || CHARP (object) || MARKERP (object) ? Qt : Qnil;
|
|
504 }
|
|
505
|
|
506 DEFUN ("natnump", Fnatnump, 1, 1, 0, /*
|
|
507 Return t if OBJECT is a nonnegative integer.
|
|
508 */
|
|
509 (object))
|
|
510 {
|
1983
|
511 return NATNUMP (object)
|
|
512 #ifdef HAVE_BIGNUM
|
|
513 || (BIGNUMP (object) && bignum_sign (XBIGNUM_DATA (object)) >= 0)
|
|
514 #endif
|
|
515 ? Qt : Qnil;
|
|
516 }
|
|
517
|
|
518 DEFUN ("nonnegativep", Fnonnegativep, 1, 1, 0, /*
|
|
519 Return t if OBJECT is a nonnegative number.
|
|
520 */
|
|
521 (object))
|
|
522 {
|
|
523 return NATNUMP (object)
|
|
524 #ifdef HAVE_BIGNUM
|
|
525 || (BIGNUMP (object) && bignum_sign (XBIGNUM_DATA (object)) >= 0)
|
|
526 #endif
|
|
527 #ifdef HAVE_RATIO
|
|
528 || (RATIOP (object) && ratio_sign (XRATIO_DATA (object)) >= 0)
|
|
529 #endif
|
|
530 #ifdef HAVE_BIGFLOAT
|
|
531 || (BIGFLOATP (object) && bigfloat_sign (XBIGFLOAT_DATA (object)) >= 0)
|
|
532 #endif
|
|
533 ? Qt : Qnil;
|
428
|
534 }
|
|
535
|
|
536 DEFUN ("bitp", Fbitp, 1, 1, 0, /*
|
|
537 Return t if OBJECT is a bit (0 or 1).
|
|
538 */
|
|
539 (object))
|
|
540 {
|
|
541 return BITP (object) ? Qt : Qnil;
|
|
542 }
|
|
543
|
|
544 DEFUN ("numberp", Fnumberp, 1, 1, 0, /*
|
|
545 Return t if OBJECT is a number (floating point or integer).
|
|
546 */
|
|
547 (object))
|
|
548 {
|
1983
|
549 #ifdef WITH_NUMBER_TYPES
|
|
550 return NUMBERP (object) ? Qt : Qnil;
|
|
551 #else
|
428
|
552 return INT_OR_FLOATP (object) ? Qt : Qnil;
|
1983
|
553 #endif
|
428
|
554 }
|
|
555
|
|
556 DEFUN ("number-or-marker-p", Fnumber_or_marker_p, 1, 1, 0, /*
|
|
557 Return t if OBJECT is a number or a marker.
|
|
558 */
|
|
559 (object))
|
|
560 {
|
|
561 return INT_OR_FLOATP (object) || MARKERP (object) ? Qt : Qnil;
|
|
562 }
|
|
563
|
|
564 DEFUN ("number-char-or-marker-p", Fnumber_char_or_marker_p, 1, 1, 0, /*
|
|
565 Return t if OBJECT is a number, character or a marker.
|
|
566 */
|
|
567 (object))
|
|
568 {
|
|
569 return (INT_OR_FLOATP (object) ||
|
|
570 CHARP (object) ||
|
|
571 MARKERP (object))
|
|
572 ? Qt : Qnil;
|
|
573 }
|
|
574
|
|
575 DEFUN ("floatp", Ffloatp, 1, 1, 0, /*
|
|
576 Return t if OBJECT is a floating point number.
|
|
577 */
|
|
578 (object))
|
|
579 {
|
|
580 return FLOATP (object) ? Qt : Qnil;
|
|
581 }
|
|
582
|
|
583 DEFUN ("type-of", Ftype_of, 1, 1, 0, /*
|
|
584 Return a symbol representing the type of OBJECT.
|
|
585 */
|
|
586 (object))
|
|
587 {
|
|
588 switch (XTYPE (object))
|
|
589 {
|
|
590 case Lisp_Type_Record:
|
|
591 return intern (XRECORD_LHEADER_IMPLEMENTATION (object)->name);
|
|
592
|
|
593 case Lisp_Type_Char: return Qcharacter;
|
|
594
|
|
595 default: return Qinteger;
|
|
596 }
|
|
597 }
|
|
598
|
|
599
|
|
600 /* Extract and set components of lists */
|
|
601
|
|
602 DEFUN ("car", Fcar, 1, 1, 0, /*
|
|
603 Return the car of LIST. If arg is nil, return nil.
|
|
604 Error if arg is not nil and not a cons cell. See also `car-safe'.
|
|
605 */
|
|
606 (list))
|
|
607 {
|
|
608 while (1)
|
|
609 {
|
|
610 if (CONSP (list))
|
|
611 return XCAR (list);
|
|
612 else if (NILP (list))
|
|
613 return Qnil;
|
|
614 else
|
|
615 list = wrong_type_argument (Qlistp, list);
|
|
616 }
|
|
617 }
|
|
618
|
|
619 DEFUN ("car-safe", Fcar_safe, 1, 1, 0, /*
|
|
620 Return the car of OBJECT if it is a cons cell, or else nil.
|
|
621 */
|
|
622 (object))
|
|
623 {
|
|
624 return CONSP (object) ? XCAR (object) : Qnil;
|
|
625 }
|
|
626
|
|
627 DEFUN ("cdr", Fcdr, 1, 1, 0, /*
|
|
628 Return the cdr of LIST. If arg is nil, return nil.
|
|
629 Error if arg is not nil and not a cons cell. See also `cdr-safe'.
|
|
630 */
|
|
631 (list))
|
|
632 {
|
|
633 while (1)
|
|
634 {
|
|
635 if (CONSP (list))
|
|
636 return XCDR (list);
|
|
637 else if (NILP (list))
|
|
638 return Qnil;
|
|
639 else
|
|
640 list = wrong_type_argument (Qlistp, list);
|
|
641 }
|
|
642 }
|
|
643
|
|
644 DEFUN ("cdr-safe", Fcdr_safe, 1, 1, 0, /*
|
|
645 Return the cdr of OBJECT if it is a cons cell, else nil.
|
|
646 */
|
|
647 (object))
|
|
648 {
|
|
649 return CONSP (object) ? XCDR (object) : Qnil;
|
|
650 }
|
|
651
|
|
652 DEFUN ("setcar", Fsetcar, 2, 2, 0, /*
|
444
|
653 Set the car of CONS-CELL to be NEWCAR. Return NEWCAR.
|
428
|
654 */
|
444
|
655 (cons_cell, newcar))
|
428
|
656 {
|
444
|
657 if (!CONSP (cons_cell))
|
|
658 cons_cell = wrong_type_argument (Qconsp, cons_cell);
|
428
|
659
|
444
|
660 XCAR (cons_cell) = newcar;
|
428
|
661 return newcar;
|
|
662 }
|
|
663
|
|
664 DEFUN ("setcdr", Fsetcdr, 2, 2, 0, /*
|
444
|
665 Set the cdr of CONS-CELL to be NEWCDR. Return NEWCDR.
|
428
|
666 */
|
444
|
667 (cons_cell, newcdr))
|
428
|
668 {
|
444
|
669 if (!CONSP (cons_cell))
|
|
670 cons_cell = wrong_type_argument (Qconsp, cons_cell);
|
428
|
671
|
444
|
672 XCDR (cons_cell) = newcdr;
|
428
|
673 return newcdr;
|
|
674 }
|
|
675
|
|
676 /* Find the function at the end of a chain of symbol function indirections.
|
|
677
|
|
678 If OBJECT is a symbol, find the end of its function chain and
|
|
679 return the value found there. If OBJECT is not a symbol, just
|
|
680 return it. If there is a cycle in the function chain, signal a
|
|
681 cyclic-function-indirection error.
|
|
682
|
442
|
683 This is like Findirect_function when VOID_FUNCTION_ERRORP is true.
|
|
684 When VOID_FUNCTION_ERRORP is false, no error is signaled if the end
|
|
685 of the chain ends up being Qunbound. */
|
428
|
686 Lisp_Object
|
442
|
687 indirect_function (Lisp_Object object, int void_function_errorp)
|
428
|
688 {
|
|
689 #define FUNCTION_INDIRECTION_SUSPICION_LENGTH 16
|
|
690 Lisp_Object tortoise, hare;
|
|
691 int count;
|
|
692
|
|
693 for (hare = tortoise = object, count = 0;
|
|
694 SYMBOLP (hare);
|
|
695 hare = XSYMBOL (hare)->function, count++)
|
|
696 {
|
|
697 if (count < FUNCTION_INDIRECTION_SUSPICION_LENGTH) continue;
|
|
698
|
|
699 if (count & 1)
|
|
700 tortoise = XSYMBOL (tortoise)->function;
|
|
701 if (EQ (hare, tortoise))
|
|
702 return Fsignal (Qcyclic_function_indirection, list1 (object));
|
|
703 }
|
|
704
|
442
|
705 if (void_function_errorp && UNBOUNDP (hare))
|
436
|
706 return signal_void_function_error (object);
|
428
|
707
|
|
708 return hare;
|
|
709 }
|
|
710
|
|
711 DEFUN ("indirect-function", Findirect_function, 1, 1, 0, /*
|
|
712 Return the function at the end of OBJECT's function chain.
|
|
713 If OBJECT is a symbol, follow all function indirections and return
|
|
714 the final function binding.
|
|
715 If OBJECT is not a symbol, just return it.
|
|
716 Signal a void-function error if the final symbol is unbound.
|
|
717 Signal a cyclic-function-indirection error if there is a loop in the
|
|
718 function chain of symbols.
|
|
719 */
|
|
720 (object))
|
|
721 {
|
|
722 return indirect_function (object, 1);
|
|
723 }
|
|
724
|
|
725 /* Extract and set vector and string elements */
|
|
726
|
|
727 DEFUN ("aref", Faref, 2, 2, 0, /*
|
|
728 Return the element of ARRAY at index INDEX.
|
|
729 ARRAY may be a vector, bit vector, or string. INDEX starts at 0.
|
|
730 */
|
|
731 (array, index_))
|
|
732 {
|
|
733 EMACS_INT idx;
|
|
734
|
|
735 retry:
|
|
736
|
|
737 if (INTP (index_)) idx = XINT (index_);
|
|
738 else if (CHARP (index_)) idx = XCHAR (index_); /* yuck! */
|
|
739 else
|
|
740 {
|
|
741 index_ = wrong_type_argument (Qinteger_or_char_p, index_);
|
|
742 goto retry;
|
|
743 }
|
|
744
|
|
745 if (idx < 0) goto range_error;
|
|
746
|
|
747 if (VECTORP (array))
|
|
748 {
|
|
749 if (idx >= XVECTOR_LENGTH (array)) goto range_error;
|
|
750 return XVECTOR_DATA (array)[idx];
|
|
751 }
|
|
752 else if (BIT_VECTORP (array))
|
|
753 {
|
647
|
754 if (idx >= (EMACS_INT) bit_vector_length (XBIT_VECTOR (array)))
|
|
755 goto range_error;
|
428
|
756 return make_int (bit_vector_bit (XBIT_VECTOR (array), idx));
|
|
757 }
|
|
758 else if (STRINGP (array))
|
|
759 {
|
826
|
760 if (idx >= string_char_length (array)) goto range_error;
|
867
|
761 return make_char (string_ichar (array, idx));
|
428
|
762 }
|
|
763 #ifdef LOSING_BYTECODE
|
|
764 else if (COMPILED_FUNCTIONP (array))
|
|
765 {
|
|
766 /* Weird, gross compatibility kludge */
|
|
767 return Felt (array, index_);
|
|
768 }
|
|
769 #endif
|
|
770 else
|
|
771 {
|
|
772 check_losing_bytecode ("aref", array);
|
|
773 array = wrong_type_argument (Qarrayp, array);
|
|
774 goto retry;
|
|
775 }
|
|
776
|
|
777 range_error:
|
|
778 args_out_of_range (array, index_);
|
1204
|
779 RETURN_NOT_REACHED (Qnil);
|
428
|
780 }
|
|
781
|
|
782 DEFUN ("aset", Faset, 3, 3, 0, /*
|
|
783 Store into the element of ARRAY at index INDEX the value NEWVAL.
|
|
784 ARRAY may be a vector, bit vector, or string. INDEX starts at 0.
|
|
785 */
|
|
786 (array, index_, newval))
|
|
787 {
|
|
788 EMACS_INT idx;
|
|
789
|
|
790 retry:
|
|
791
|
|
792 if (INTP (index_)) idx = XINT (index_);
|
|
793 else if (CHARP (index_)) idx = XCHAR (index_); /* yuck! */
|
|
794 else
|
|
795 {
|
|
796 index_ = wrong_type_argument (Qinteger_or_char_p, index_);
|
|
797 goto retry;
|
|
798 }
|
|
799
|
|
800 if (idx < 0) goto range_error;
|
|
801
|
771
|
802 CHECK_LISP_WRITEABLE (array);
|
428
|
803 if (VECTORP (array))
|
|
804 {
|
|
805 if (idx >= XVECTOR_LENGTH (array)) goto range_error;
|
|
806 XVECTOR_DATA (array)[idx] = newval;
|
|
807 }
|
|
808 else if (BIT_VECTORP (array))
|
|
809 {
|
647
|
810 if (idx >= (EMACS_INT) bit_vector_length (XBIT_VECTOR (array)))
|
|
811 goto range_error;
|
428
|
812 CHECK_BIT (newval);
|
|
813 set_bit_vector_bit (XBIT_VECTOR (array), idx, !ZEROP (newval));
|
|
814 }
|
|
815 else if (STRINGP (array))
|
|
816 {
|
|
817 CHECK_CHAR_COERCE_INT (newval);
|
826
|
818 if (idx >= string_char_length (array)) goto range_error;
|
793
|
819 set_string_char (array, idx, XCHAR (newval));
|
428
|
820 bump_string_modiff (array);
|
|
821 }
|
|
822 else
|
|
823 {
|
|
824 array = wrong_type_argument (Qarrayp, array);
|
|
825 goto retry;
|
|
826 }
|
|
827
|
|
828 return newval;
|
|
829
|
|
830 range_error:
|
|
831 args_out_of_range (array, index_);
|
1204
|
832 RETURN_NOT_REACHED (Qnil);
|
428
|
833 }
|
|
834
|
|
835
|
|
836 /**********************************************************************/
|
|
837 /* Arithmetic functions */
|
|
838 /**********************************************************************/
|
2001
|
839 #ifndef WITH_NUMBER_TYPES
|
428
|
840 typedef struct
|
|
841 {
|
|
842 int int_p;
|
|
843 union
|
|
844 {
|
|
845 EMACS_INT ival;
|
|
846 double dval;
|
|
847 } c;
|
|
848 } int_or_double;
|
|
849
|
|
850 static void
|
|
851 number_char_or_marker_to_int_or_double (Lisp_Object obj, int_or_double *p)
|
|
852 {
|
|
853 retry:
|
|
854 p->int_p = 1;
|
|
855 if (INTP (obj)) p->c.ival = XINT (obj);
|
|
856 else if (CHARP (obj)) p->c.ival = XCHAR (obj);
|
|
857 else if (MARKERP (obj)) p->c.ival = marker_position (obj);
|
|
858 else if (FLOATP (obj)) p->c.dval = XFLOAT_DATA (obj), p->int_p = 0;
|
|
859 else
|
|
860 {
|
|
861 obj = wrong_type_argument (Qnumber_char_or_marker_p, obj);
|
|
862 goto retry;
|
|
863 }
|
|
864 }
|
|
865
|
|
866 static double
|
|
867 number_char_or_marker_to_double (Lisp_Object obj)
|
|
868 {
|
|
869 retry:
|
|
870 if (INTP (obj)) return (double) XINT (obj);
|
|
871 else if (CHARP (obj)) return (double) XCHAR (obj);
|
|
872 else if (MARKERP (obj)) return (double) marker_position (obj);
|
|
873 else if (FLOATP (obj)) return XFLOAT_DATA (obj);
|
|
874 else
|
|
875 {
|
|
876 obj = wrong_type_argument (Qnumber_char_or_marker_p, obj);
|
|
877 goto retry;
|
|
878 }
|
|
879 }
|
2001
|
880 #endif /* WITH_NUMBER_TYPES */
|
428
|
881
|
|
882 static EMACS_INT
|
|
883 integer_char_or_marker_to_int (Lisp_Object obj)
|
|
884 {
|
|
885 retry:
|
|
886 if (INTP (obj)) return XINT (obj);
|
|
887 else if (CHARP (obj)) return XCHAR (obj);
|
|
888 else if (MARKERP (obj)) return marker_position (obj);
|
|
889 else
|
|
890 {
|
|
891 obj = wrong_type_argument (Qinteger_char_or_marker_p, obj);
|
|
892 goto retry;
|
|
893 }
|
|
894 }
|
|
895
|
1983
|
896 #ifdef WITH_NUMBER_TYPES
|
|
897
|
|
898 #ifdef HAVE_BIGNUM
|
|
899 #define BIGNUM_CASE(op) \
|
|
900 case BIGNUM_T: \
|
|
901 if (!bignum_##op (XBIGNUM_DATA (obj1), XBIGNUM_DATA (obj2))) \
|
|
902 return Qnil; \
|
|
903 break;
|
|
904 #else
|
|
905 #define BIGNUM_CASE(op)
|
|
906 #endif /* HAVE_BIGNUM */
|
|
907
|
|
908 #ifdef HAVE_RATIO
|
|
909 #define RATIO_CASE(op) \
|
|
910 case RATIO_T: \
|
|
911 if (!ratio_##op (XRATIO_DATA (obj1), XRATIO_DATA (obj2))) \
|
|
912 return Qnil; \
|
|
913 break;
|
|
914 #else
|
|
915 #define RATIO_CASE(op)
|
|
916 #endif /* HAVE_RATIO */
|
|
917
|
|
918 #ifdef HAVE_BIGFLOAT
|
|
919 #define BIGFLOAT_CASE(op) \
|
|
920 case BIGFLOAT_T: \
|
|
921 if (!bigfloat_##op (XBIGFLOAT_DATA (obj1), XBIGFLOAT_DATA (obj2))) \
|
|
922 return Qnil; \
|
|
923 break;
|
|
924 #else
|
|
925 #define BIGFLOAT_CASE(op)
|
|
926 #endif /* HAVE_BIGFLOAT */
|
|
927
|
|
928 #define ARITHCOMPARE_MANY(c_op,op) \
|
|
929 { \
|
|
930 REGISTER int i; \
|
|
931 Lisp_Object obj1, obj2; \
|
|
932 \
|
|
933 for (i = 1; i < nargs; i++) \
|
|
934 { \
|
|
935 obj1 = args[i - 1]; \
|
|
936 obj2 = args[i]; \
|
|
937 switch (promote_args (&obj1, &obj2)) \
|
|
938 { \
|
|
939 case FIXNUM_T: \
|
|
940 if (!(XREALINT (obj1) c_op XREALINT (obj2))) \
|
|
941 return Qnil; \
|
|
942 break; \
|
|
943 BIGNUM_CASE (op) \
|
|
944 RATIO_CASE (op) \
|
|
945 case FLOAT_T: \
|
|
946 if (!(XFLOAT_DATA (obj1) c_op XFLOAT_DATA (obj2))) \
|
|
947 return Qnil; \
|
|
948 break; \
|
|
949 BIGFLOAT_CASE (op) \
|
|
950 } \
|
|
951 } \
|
|
952 return Qt; \
|
|
953 }
|
|
954 #else /* !WITH_NUMBER_TYPES */
|
|
955 #define ARITHCOMPARE_MANY(c_op,op) \
|
428
|
956 { \
|
|
957 int_or_double iod1, iod2, *p = &iod1, *q = &iod2; \
|
|
958 Lisp_Object *args_end = args + nargs; \
|
|
959 \
|
|
960 number_char_or_marker_to_int_or_double (*args++, p); \
|
|
961 \
|
|
962 while (args < args_end) \
|
|
963 { \
|
|
964 number_char_or_marker_to_int_or_double (*args++, q); \
|
|
965 \
|
|
966 if (!((p->int_p && q->int_p) ? \
|
1983
|
967 (p->c.ival c_op q->c.ival) : \
|
|
968 ((p->int_p ? (double) p->c.ival : p->c.dval) c_op \
|
428
|
969 (q->int_p ? (double) q->c.ival : q->c.dval)))) \
|
|
970 return Qnil; \
|
|
971 \
|
|
972 { /* swap */ int_or_double *r = p; p = q; q = r; } \
|
|
973 } \
|
|
974 return Qt; \
|
|
975 }
|
1983
|
976 #endif /* WITH_NUMBER_TYPES */
|
428
|
977
|
|
978 DEFUN ("=", Feqlsign, 1, MANY, 0, /*
|
|
979 Return t if all the arguments are numerically equal.
|
|
980 The arguments may be numbers, characters or markers.
|
|
981 */
|
|
982 (int nargs, Lisp_Object *args))
|
|
983 {
|
1983
|
984 ARITHCOMPARE_MANY (==, eql)
|
428
|
985 }
|
|
986
|
|
987 DEFUN ("<", Flss, 1, MANY, 0, /*
|
|
988 Return t if the sequence of arguments is monotonically increasing.
|
|
989 The arguments may be numbers, characters or markers.
|
|
990 */
|
|
991 (int nargs, Lisp_Object *args))
|
|
992 {
|
1983
|
993 ARITHCOMPARE_MANY (<, lt)
|
428
|
994 }
|
|
995
|
|
996 DEFUN (">", Fgtr, 1, MANY, 0, /*
|
|
997 Return t if the sequence of arguments is monotonically decreasing.
|
|
998 The arguments may be numbers, characters or markers.
|
|
999 */
|
|
1000 (int nargs, Lisp_Object *args))
|
|
1001 {
|
1983
|
1002 ARITHCOMPARE_MANY (>, gt)
|
428
|
1003 }
|
|
1004
|
|
1005 DEFUN ("<=", Fleq, 1, MANY, 0, /*
|
|
1006 Return t if the sequence of arguments is monotonically nondecreasing.
|
|
1007 The arguments may be numbers, characters or markers.
|
|
1008 */
|
|
1009 (int nargs, Lisp_Object *args))
|
|
1010 {
|
1983
|
1011 ARITHCOMPARE_MANY (<=, le)
|
428
|
1012 }
|
|
1013
|
|
1014 DEFUN (">=", Fgeq, 1, MANY, 0, /*
|
|
1015 Return t if the sequence of arguments is monotonically nonincreasing.
|
|
1016 The arguments may be numbers, characters or markers.
|
|
1017 */
|
|
1018 (int nargs, Lisp_Object *args))
|
|
1019 {
|
1983
|
1020 ARITHCOMPARE_MANY (>=, ge)
|
428
|
1021 }
|
|
1022
|
1983
|
1023 /* Unlike all the other comparisons, this is an O(N*N) algorithm. But who
|
|
1024 cares? Inspection of all elisp code distributed by xemacs.org shows that
|
|
1025 it is almost always called with 2 arguments, rarely with 3, and never with
|
|
1026 more than 3. The constant factors of algorithms with better asymptotic
|
|
1027 complexity are higher, which means that those algorithms will run SLOWER
|
|
1028 than this one in the common case. Optimize the common case! */
|
428
|
1029 DEFUN ("/=", Fneq, 1, MANY, 0, /*
|
|
1030 Return t if no two arguments are numerically equal.
|
|
1031 The arguments may be numbers, characters or markers.
|
|
1032 */
|
|
1033 (int nargs, Lisp_Object *args))
|
|
1034 {
|
1983
|
1035 #ifdef WITH_NUMBER_TYPES
|
|
1036 REGISTER int i, j;
|
|
1037 Lisp_Object obj1, obj2;
|
|
1038
|
|
1039 for (i = 0; i < nargs - 1; i++)
|
|
1040 {
|
|
1041 obj1 = args[i];
|
|
1042 for (j = i + 1; j < nargs; j++)
|
|
1043 {
|
|
1044 obj2 = args[j];
|
|
1045 switch (promote_args (&obj1, &obj2))
|
|
1046 {
|
|
1047 case FIXNUM_T:
|
|
1048 if (XREALINT (obj1) == XREALINT (obj2))
|
|
1049 return Qnil;
|
|
1050 break;
|
|
1051 #ifdef HAVE_BIGNUM
|
|
1052 case BIGNUM_T:
|
|
1053 if (bignum_eql (XBIGNUM_DATA (obj1), XBIGNUM_DATA (obj2)))
|
|
1054 return Qnil;
|
|
1055 break;
|
|
1056 #endif
|
|
1057 #ifdef HAVE_RATIO
|
|
1058 case RATIO_T:
|
|
1059 if (ratio_eql (XRATIO_DATA (obj1), XRATIO_DATA (obj2)))
|
|
1060 return Qnil;
|
|
1061 break;
|
|
1062 #endif
|
|
1063 case FLOAT_T:
|
|
1064 if (XFLOAT_DATA (obj1) == XFLOAT_DATA (obj2))
|
|
1065 return Qnil;
|
|
1066 break;
|
|
1067 #ifdef HAVE_BIGFLOAT
|
|
1068 case BIGFLOAT_T:
|
|
1069 if (bigfloat_eql (XBIGFLOAT_DATA (obj1), XBIGFLOAT_DATA (obj2)))
|
|
1070 return Qnil;
|
|
1071 break;
|
|
1072 #endif
|
|
1073 }
|
|
1074 }
|
|
1075 }
|
|
1076 return Qt;
|
|
1077 #else /* !WITH_NUMBER_TYPES */
|
428
|
1078 Lisp_Object *args_end = args + nargs;
|
|
1079 Lisp_Object *p, *q;
|
|
1080
|
|
1081 /* Unlike all the other comparisons, this is an N*N algorithm.
|
|
1082 We could use a hash table for nargs > 50 to make this linear. */
|
|
1083 for (p = args; p < args_end; p++)
|
|
1084 {
|
|
1085 int_or_double iod1, iod2;
|
|
1086 number_char_or_marker_to_int_or_double (*p, &iod1);
|
|
1087
|
|
1088 for (q = p + 1; q < args_end; q++)
|
|
1089 {
|
|
1090 number_char_or_marker_to_int_or_double (*q, &iod2);
|
|
1091
|
|
1092 if (!((iod1.int_p && iod2.int_p) ?
|
|
1093 (iod1.c.ival != iod2.c.ival) :
|
|
1094 ((iod1.int_p ? (double) iod1.c.ival : iod1.c.dval) !=
|
|
1095 (iod2.int_p ? (double) iod2.c.ival : iod2.c.dval))))
|
|
1096 return Qnil;
|
|
1097 }
|
|
1098 }
|
|
1099 return Qt;
|
1983
|
1100 #endif /* WITH_NUMBER_TYPES */
|
428
|
1101 }
|
|
1102
|
|
1103 DEFUN ("zerop", Fzerop, 1, 1, 0, /*
|
|
1104 Return t if NUMBER is zero.
|
|
1105 */
|
|
1106 (number))
|
|
1107 {
|
|
1108 retry:
|
|
1109 if (INTP (number))
|
|
1110 return EQ (number, Qzero) ? Qt : Qnil;
|
1983
|
1111 #ifdef HAVE_BIGNUM
|
|
1112 else if (BIGNUMP (number))
|
|
1113 return bignum_sign (XBIGNUM_DATA (number)) == 0 ? Qt : Qnil;
|
|
1114 #endif
|
|
1115 #ifdef HAVE_RATIO
|
|
1116 else if (RATIOP (number))
|
|
1117 return ratio_sign (XRATIO_DATA (number)) == 0 ? Qt : Qnil;
|
|
1118 #endif
|
428
|
1119 else if (FLOATP (number))
|
|
1120 return XFLOAT_DATA (number) == 0.0 ? Qt : Qnil;
|
1983
|
1121 #ifdef HAVE_BIGFLOAT
|
|
1122 else if (BIGFLOATP (number))
|
|
1123 return bigfloat_sign (XBIGFLOAT_DATA (number)) == 0 ? Qt : Qnil;
|
|
1124 #endif
|
428
|
1125 else
|
|
1126 {
|
|
1127 number = wrong_type_argument (Qnumberp, number);
|
|
1128 goto retry;
|
|
1129 }
|
|
1130 }
|
|
1131
|
|
1132 /* Convert between a 32-bit value and a cons of two 16-bit values.
|
|
1133 This is used to pass 32-bit integers to and from the user.
|
|
1134 Use time_to_lisp() and lisp_to_time() for time values.
|
|
1135
|
|
1136 If you're thinking of using this to store a pointer into a Lisp Object
|
|
1137 for internal purposes (such as when calling record_unwind_protect()),
|
|
1138 try using make_opaque_ptr()/get_opaque_ptr() instead. */
|
|
1139 Lisp_Object
|
|
1140 word_to_lisp (unsigned int item)
|
|
1141 {
|
|
1142 return Fcons (make_int (item >> 16), make_int (item & 0xffff));
|
|
1143 }
|
|
1144
|
|
1145 unsigned int
|
|
1146 lisp_to_word (Lisp_Object item)
|
|
1147 {
|
|
1148 if (INTP (item))
|
|
1149 return XINT (item);
|
|
1150 else
|
|
1151 {
|
|
1152 Lisp_Object top = Fcar (item);
|
|
1153 Lisp_Object bot = Fcdr (item);
|
|
1154 CHECK_INT (top);
|
|
1155 CHECK_INT (bot);
|
|
1156 return (XINT (top) << 16) | (XINT (bot) & 0xffff);
|
|
1157 }
|
|
1158 }
|
|
1159
|
|
1160
|
|
1161 DEFUN ("number-to-string", Fnumber_to_string, 1, 1, 0, /*
|
444
|
1162 Convert NUMBER to a string by printing it in decimal.
|
428
|
1163 Uses a minus sign if negative.
|
444
|
1164 NUMBER may be an integer or a floating point number.
|
1983
|
1165 If supported, it may also be a ratio.
|
428
|
1166 */
|
444
|
1167 (number))
|
428
|
1168 {
|
1983
|
1169 #ifdef WITH_NUMBER_TYPES
|
|
1170 CHECK_NUMBER (number);
|
|
1171 #else
|
444
|
1172 CHECK_INT_OR_FLOAT (number);
|
1983
|
1173 #endif
|
428
|
1174
|
444
|
1175 if (FLOATP (number))
|
428
|
1176 {
|
|
1177 char pigbuf[350]; /* see comments in float_to_string */
|
|
1178
|
444
|
1179 float_to_string (pigbuf, XFLOAT_DATA (number));
|
428
|
1180 return build_string (pigbuf);
|
|
1181 }
|
1983
|
1182 #ifdef HAVE_BIGNUM
|
|
1183 if (BIGNUMP (number))
|
|
1184 {
|
|
1185 char *str = bignum_to_string (XBIGNUM_DATA (number), 10);
|
|
1186 Lisp_Object retval = build_string (str);
|
|
1187 xfree (str, char *);
|
|
1188 return retval;
|
|
1189 }
|
|
1190 #endif
|
|
1191 #ifdef HAVE_RATIO
|
|
1192 if (RATIOP (number))
|
|
1193 {
|
|
1194 char *str = ratio_to_string (XRATIO_DATA (number), 10);
|
|
1195 Lisp_Object retval = build_string (str);
|
|
1196 xfree (str, char *);
|
|
1197 return retval;
|
|
1198 }
|
|
1199 #endif
|
|
1200 #ifdef HAVE_BIGFLOAT
|
|
1201 if (BIGFLOATP (number))
|
|
1202 {
|
|
1203 char *str = bigfloat_to_string (XBIGFLOAT_DATA (number), 10);
|
|
1204 Lisp_Object retval = build_string (str);
|
|
1205 xfree (str, char *);
|
|
1206 return retval;
|
|
1207 }
|
|
1208 #endif
|
428
|
1209
|
603
|
1210 {
|
|
1211 char buffer[DECIMAL_PRINT_SIZE (long)];
|
|
1212
|
|
1213 long_to_string (buffer, XINT (number));
|
|
1214 return build_string (buffer);
|
|
1215 }
|
428
|
1216 }
|
|
1217
|
2001
|
1218 #ifndef HAVE_BIGNUM
|
428
|
1219 static int
|
|
1220 digit_to_number (int character, int base)
|
|
1221 {
|
|
1222 /* Assumes ASCII */
|
|
1223 int digit = ((character >= '0' && character <= '9') ? character - '0' :
|
|
1224 (character >= 'a' && character <= 'z') ? character - 'a' + 10 :
|
|
1225 (character >= 'A' && character <= 'Z') ? character - 'A' + 10 :
|
|
1226 -1);
|
|
1227
|
|
1228 return digit >= base ? -1 : digit;
|
|
1229 }
|
2001
|
1230 #endif
|
428
|
1231
|
|
1232 DEFUN ("string-to-number", Fstring_to_number, 1, 2, 0, /*
|
444
|
1233 Convert STRING to a number by parsing it as a number in base BASE.
|
428
|
1234 This parses both integers and floating point numbers.
|
1983
|
1235 If they are supported, it also reads ratios.
|
428
|
1236 It ignores leading spaces and tabs.
|
|
1237
|
444
|
1238 If BASE is nil or omitted, base 10 is used.
|
|
1239 BASE must be an integer between 2 and 16 (inclusive).
|
428
|
1240 Floating point numbers always use base 10.
|
|
1241 */
|
|
1242 (string, base))
|
|
1243 {
|
1995
|
1244 Ibyte *p;
|
428
|
1245 int b;
|
|
1246
|
|
1247 CHECK_STRING (string);
|
|
1248
|
|
1249 if (NILP (base))
|
|
1250 b = 10;
|
|
1251 else
|
|
1252 {
|
|
1253 CHECK_INT (base);
|
|
1254 b = XINT (base);
|
|
1255 check_int_range (b, 2, 16);
|
|
1256 }
|
|
1257
|
1995
|
1258 p = XSTRING_DATA (string);
|
428
|
1259
|
|
1260 /* Skip any whitespace at the front of the number. Some versions of
|
|
1261 atoi do this anyway, so we might as well make Emacs lisp consistent. */
|
|
1262 while (*p == ' ' || *p == '\t')
|
|
1263 p++;
|
|
1264
|
1995
|
1265 if (isfloat_string ((const char *) p) && b == 10)
|
1983
|
1266 {
|
|
1267 #ifdef HAVE_BIGFLOAT
|
|
1268 if (ZEROP (Vdefault_float_precision))
|
|
1269 #endif
|
1995
|
1270 return make_float (atof ((const char *) p));
|
1983
|
1271 #ifdef HAVE_BIGFLOAT
|
|
1272 else
|
|
1273 {
|
2010
|
1274 /* GMP bigfloat_set_string returns random values with initial + */
|
|
1275 if (*p == '+')
|
|
1276 p++;
|
1983
|
1277 bigfloat_set_prec (scratch_bigfloat, bigfloat_get_default_prec ());
|
1995
|
1278 bigfloat_set_string (scratch_bigfloat, (const char *) p, b);
|
1983
|
1279 return make_bigfloat_bf (scratch_bigfloat);
|
|
1280 }
|
|
1281 #endif
|
|
1282 }
|
|
1283
|
|
1284 #ifdef HAVE_RATIO
|
|
1285 if (qxestrchr (p, '/') != NULL)
|
|
1286 {
|
2010
|
1287 /* GMP ratio_set_string returns random values with initial + sign */
|
|
1288 if (*p == '+')
|
|
1289 p++;
|
1995
|
1290 ratio_set_string (scratch_ratio, (const char *) p, b);
|
1983
|
1291 return make_ratio_rt (scratch_ratio);
|
|
1292 }
|
|
1293 #endif /* HAVE_RATIO */
|
|
1294
|
|
1295 #ifdef HAVE_BIGNUM
|
2010
|
1296 /* GMP bignum_set_string returns random values when the string starts with a
|
|
1297 plus sign */
|
|
1298 if (*p == '+')
|
|
1299 p++;
|
1983
|
1300 /* GMP bignum_set_string returns random values when fed an empty string */
|
|
1301 if (*p == '\0')
|
|
1302 return make_int (0);
|
1995
|
1303 bignum_set_string (scratch_bignum, (const char *) p, b);
|
1983
|
1304 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
1305 #else
|
428
|
1306 if (b == 10)
|
|
1307 {
|
|
1308 /* Use the system-provided functions for base 10. */
|
|
1309 #if SIZEOF_EMACS_INT == SIZEOF_INT
|
|
1310 return make_int (atoi (p));
|
|
1311 #elif SIZEOF_EMACS_INT == SIZEOF_LONG
|
|
1312 return make_int (atol (p));
|
|
1313 #elif SIZEOF_EMACS_INT == SIZEOF_LONG_LONG
|
|
1314 return make_int (atoll (p));
|
|
1315 #endif
|
|
1316 }
|
|
1317 else
|
|
1318 {
|
444
|
1319 int negative = 1;
|
428
|
1320 EMACS_INT v = 0;
|
|
1321
|
|
1322 if (*p == '-')
|
|
1323 {
|
|
1324 negative = -1;
|
|
1325 p++;
|
|
1326 }
|
|
1327 else if (*p == '+')
|
|
1328 p++;
|
|
1329 while (1)
|
|
1330 {
|
444
|
1331 int digit = digit_to_number (*p++, b);
|
428
|
1332 if (digit < 0)
|
|
1333 break;
|
|
1334 v = v * b + digit;
|
|
1335 }
|
|
1336 return make_int (negative * v);
|
|
1337 }
|
1983
|
1338 #endif /* HAVE_BIGNUM */
|
428
|
1339 }
|
|
1340
|
|
1341
|
|
1342 DEFUN ("+", Fplus, 0, MANY, 0, /*
|
|
1343 Return sum of any number of arguments.
|
|
1344 The arguments should all be numbers, characters or markers.
|
|
1345 */
|
|
1346 (int nargs, Lisp_Object *args))
|
|
1347 {
|
1983
|
1348 #ifdef WITH_NUMBER_TYPES
|
|
1349 REGISTER int i;
|
|
1350 Lisp_Object accum = make_int (0), addend;
|
|
1351
|
|
1352 for (i = 0; i < nargs; i++)
|
|
1353 {
|
|
1354 addend = args[i];
|
|
1355 switch (promote_args (&accum, &addend))
|
|
1356 {
|
|
1357 case FIXNUM_T:
|
|
1358 accum = make_integer (XREALINT (accum) + XREALINT (addend));
|
|
1359 break;
|
|
1360 #ifdef HAVE_BIGNUM
|
|
1361 case BIGNUM_T:
|
|
1362 bignum_add (scratch_bignum, XBIGNUM_DATA (accum),
|
|
1363 XBIGNUM_DATA (addend));
|
|
1364 accum = make_bignum_bg (scratch_bignum);
|
|
1365 break;
|
|
1366 #endif
|
|
1367 #ifdef HAVE_RATIO
|
|
1368 case RATIO_T:
|
|
1369 ratio_add (scratch_ratio, XRATIO_DATA (accum),
|
|
1370 XRATIO_DATA (addend));
|
|
1371 accum = make_ratio_rt (scratch_ratio);
|
|
1372 break;
|
|
1373 #endif
|
|
1374 case FLOAT_T:
|
|
1375 accum = make_float (XFLOAT_DATA (accum) + XFLOAT_DATA (addend));
|
|
1376 break;
|
|
1377 #ifdef HAVE_BIGFLOAT
|
|
1378 case BIGFLOAT_T:
|
|
1379 bigfloat_set_prec (scratch_bigfloat,
|
|
1380 max (XBIGFLOAT_GET_PREC (addend),
|
|
1381 XBIGFLOAT_GET_PREC (accum)));
|
|
1382 bigfloat_add (scratch_bigfloat, XBIGFLOAT_DATA (accum),
|
|
1383 XBIGFLOAT_DATA (addend));
|
|
1384 accum = make_bigfloat_bf (scratch_bigfloat);
|
|
1385 break;
|
|
1386 #endif
|
|
1387 }
|
|
1388 }
|
|
1389 return Fcanonicalize_number (accum);
|
|
1390 #else /* !WITH_NUMBER_TYPES */
|
428
|
1391 EMACS_INT iaccum = 0;
|
|
1392 Lisp_Object *args_end = args + nargs;
|
|
1393
|
|
1394 while (args < args_end)
|
|
1395 {
|
|
1396 int_or_double iod;
|
|
1397 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1398 if (iod.int_p)
|
|
1399 iaccum += iod.c.ival;
|
|
1400 else
|
|
1401 {
|
|
1402 double daccum = (double) iaccum + iod.c.dval;
|
|
1403 while (args < args_end)
|
|
1404 daccum += number_char_or_marker_to_double (*args++);
|
|
1405 return make_float (daccum);
|
|
1406 }
|
|
1407 }
|
|
1408
|
|
1409 return make_int (iaccum);
|
1983
|
1410 #endif /* WITH_NUMBER_TYPES */
|
428
|
1411 }
|
|
1412
|
|
1413 DEFUN ("-", Fminus, 1, MANY, 0, /*
|
|
1414 Negate number or subtract numbers, characters or markers.
|
|
1415 With one arg, negates it. With more than one arg,
|
|
1416 subtracts all but the first from the first.
|
|
1417 */
|
|
1418 (int nargs, Lisp_Object *args))
|
|
1419 {
|
1983
|
1420 #ifdef WITH_NUMBER_TYPES
|
|
1421 REGISTER int i;
|
|
1422 Lisp_Object accum = args[0], subtrahend;
|
|
1423
|
|
1424 if (nargs == 1)
|
|
1425 {
|
|
1426 if (CHARP (accum))
|
|
1427 accum = make_int (XCHAR (accum));
|
|
1428 else if (MARKERP (accum))
|
|
1429 accum = make_int (marker_position (accum));
|
|
1430
|
|
1431 /* Invert the sign of accum */
|
|
1432 CHECK_NUMBER (accum);
|
|
1433 switch (get_number_type (accum))
|
|
1434 {
|
|
1435 case FIXNUM_T:
|
|
1436 return make_integer (-XREALINT (accum));
|
|
1437 #ifdef HAVE_BIGNUM
|
|
1438 case BIGNUM_T:
|
|
1439 bignum_neg (scratch_bignum, XBIGNUM_DATA (accum));
|
|
1440 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
1441 #endif
|
|
1442 #ifdef HAVE_RATIO
|
|
1443 case RATIO_T:
|
|
1444 ratio_neg (scratch_ratio, XRATIO_DATA (accum));
|
|
1445 return make_ratio_rt (scratch_ratio);
|
|
1446 #endif
|
|
1447 case FLOAT_T:
|
|
1448 return make_float (-XFLOAT_DATA (accum));
|
|
1449 #ifdef HAVE_BIGFLOAT
|
|
1450 case BIGFLOAT_T:
|
|
1451 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (accum));
|
|
1452 bigfloat_neg (scratch_bigfloat, XBIGFLOAT_DATA (accum));
|
|
1453 return make_bigfloat_bf (scratch_bigfloat);
|
|
1454 #endif
|
|
1455 }
|
|
1456 }
|
|
1457 else
|
|
1458 {
|
|
1459 /* Subtrace the remaining arguments from accum */
|
|
1460 for (i = 1; i < nargs; i++)
|
|
1461 {
|
|
1462 subtrahend = args[i];
|
|
1463 switch (promote_args (&accum, &subtrahend))
|
|
1464 {
|
|
1465 case FIXNUM_T:
|
|
1466 accum = make_integer (XREALINT (accum) - XREALINT (subtrahend));
|
|
1467 break;
|
|
1468 #ifdef HAVE_BIGNUM
|
|
1469 case BIGNUM_T:
|
|
1470 bignum_sub (scratch_bignum, XBIGNUM_DATA (accum),
|
|
1471 XBIGNUM_DATA (subtrahend));
|
|
1472 accum = make_bignum_bg (scratch_bignum);
|
|
1473 break;
|
|
1474 #endif
|
|
1475 #ifdef HAVE_RATIO
|
|
1476 case RATIO_T:
|
|
1477 ratio_sub (scratch_ratio, XRATIO_DATA (accum),
|
|
1478 XRATIO_DATA (subtrahend));
|
|
1479 accum = make_ratio_rt (scratch_ratio);
|
|
1480 break;
|
|
1481 #endif
|
|
1482 case FLOAT_T:
|
|
1483 accum =
|
|
1484 make_float (XFLOAT_DATA (accum) - XFLOAT_DATA (subtrahend));
|
|
1485 break;
|
|
1486 #ifdef HAVE_BIGFLOAT
|
|
1487 case BIGFLOAT_T:
|
|
1488 bigfloat_set_prec (scratch_bigfloat,
|
|
1489 max (XBIGFLOAT_GET_PREC (subtrahend),
|
|
1490 XBIGFLOAT_GET_PREC (accum)));
|
|
1491 bigfloat_sub (scratch_bigfloat, XBIGFLOAT_DATA (accum),
|
|
1492 XBIGFLOAT_DATA (subtrahend));
|
|
1493 accum = make_bigfloat_bf (scratch_bigfloat);
|
|
1494 break;
|
|
1495 #endif
|
|
1496 }
|
|
1497 }
|
|
1498 }
|
|
1499 return Fcanonicalize_number (accum);
|
|
1500 #else /* !WITH_NUMBER_TYPES */
|
428
|
1501 EMACS_INT iaccum;
|
|
1502 double daccum;
|
|
1503 Lisp_Object *args_end = args + nargs;
|
|
1504 int_or_double iod;
|
|
1505
|
|
1506 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1507 if (iod.int_p)
|
|
1508 iaccum = nargs > 1 ? iod.c.ival : - iod.c.ival;
|
|
1509 else
|
|
1510 {
|
|
1511 daccum = nargs > 1 ? iod.c.dval : - iod.c.dval;
|
|
1512 goto do_float;
|
|
1513 }
|
|
1514
|
|
1515 while (args < args_end)
|
|
1516 {
|
|
1517 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1518 if (iod.int_p)
|
|
1519 iaccum -= iod.c.ival;
|
|
1520 else
|
|
1521 {
|
|
1522 daccum = (double) iaccum - iod.c.dval;
|
|
1523 goto do_float;
|
|
1524 }
|
|
1525 }
|
|
1526
|
|
1527 return make_int (iaccum);
|
|
1528
|
|
1529 do_float:
|
|
1530 for (; args < args_end; args++)
|
|
1531 daccum -= number_char_or_marker_to_double (*args);
|
|
1532 return make_float (daccum);
|
1983
|
1533 #endif /* WITH_NUMBER_TYPES */
|
428
|
1534 }
|
|
1535
|
|
1536 DEFUN ("*", Ftimes, 0, MANY, 0, /*
|
|
1537 Return product of any number of arguments.
|
|
1538 The arguments should all be numbers, characters or markers.
|
|
1539 */
|
|
1540 (int nargs, Lisp_Object *args))
|
|
1541 {
|
1983
|
1542 #ifdef WITH_NUMBER_TYPES
|
|
1543 REGISTER int i;
|
|
1544 /* Start with a bignum to avoid overflow */
|
|
1545 Lisp_Object accum = make_bignum (1L), multiplier;
|
|
1546
|
|
1547 for (i = 0; i < nargs; i++)
|
|
1548 {
|
|
1549 multiplier = args[i];
|
|
1550 switch (promote_args (&accum, &multiplier))
|
|
1551 {
|
|
1552 #ifdef HAVE_BIGNUM
|
|
1553 case BIGNUM_T:
|
|
1554 bignum_mul (scratch_bignum, XBIGNUM_DATA (accum),
|
|
1555 XBIGNUM_DATA (multiplier));
|
|
1556 accum = make_bignum_bg (scratch_bignum);
|
|
1557 break;
|
|
1558 #endif
|
|
1559 #ifdef HAVE_RATIO
|
|
1560 case RATIO_T:
|
|
1561 ratio_mul (scratch_ratio, XRATIO_DATA (accum),
|
|
1562 XRATIO_DATA (multiplier));
|
|
1563 accum = make_ratio_rt (scratch_ratio);
|
|
1564 break;
|
|
1565 #endif
|
|
1566 case FLOAT_T:
|
|
1567 accum = make_float (XFLOAT_DATA (accum) * XFLOAT_DATA (multiplier));
|
|
1568 break;
|
|
1569 #ifdef HAVE_BIGFLOAT
|
|
1570 case BIGFLOAT_T:
|
|
1571 bigfloat_set_prec (scratch_bigfloat,
|
|
1572 max (XBIGFLOAT_GET_PREC (multiplier),
|
|
1573 XBIGFLOAT_GET_PREC (accum)));
|
|
1574 bigfloat_mul (scratch_bigfloat, XBIGFLOAT_DATA (accum),
|
|
1575 XBIGFLOAT_DATA (multiplier));
|
|
1576 accum = make_bigfloat_bf (scratch_bigfloat);
|
|
1577 break;
|
|
1578 #endif
|
|
1579 }
|
|
1580 }
|
|
1581 return Fcanonicalize_number (accum);
|
|
1582 #else /* !WITH_NUMBER_TYPES */
|
428
|
1583 EMACS_INT iaccum = 1;
|
|
1584 Lisp_Object *args_end = args + nargs;
|
|
1585
|
|
1586 while (args < args_end)
|
|
1587 {
|
|
1588 int_or_double iod;
|
|
1589 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1590 if (iod.int_p)
|
|
1591 iaccum *= iod.c.ival;
|
|
1592 else
|
|
1593 {
|
|
1594 double daccum = (double) iaccum * iod.c.dval;
|
|
1595 while (args < args_end)
|
|
1596 daccum *= number_char_or_marker_to_double (*args++);
|
|
1597 return make_float (daccum);
|
|
1598 }
|
|
1599 }
|
|
1600
|
|
1601 return make_int (iaccum);
|
1983
|
1602 #endif /* WITH_NUMBER_TYPES */
|
428
|
1603 }
|
|
1604
|
1983
|
1605 #ifdef HAVE_RATIO
|
|
1606 DEFUN ("div", Fdiv, 1, MANY, 0, /*
|
|
1607 Same as `/', but dividing integers creates a ratio instead of truncating.
|
|
1608 Note that this is a departure from Common Lisp, where / creates ratios when
|
|
1609 dividing integers. Having a separate function lets us avoid breaking existing
|
|
1610 Emacs Lisp code that expects / to do integer division.
|
|
1611 */
|
|
1612 (int nargs, Lisp_Object *args))
|
|
1613 {
|
|
1614 REGISTER int i;
|
|
1615 Lisp_Object accum, divisor;
|
|
1616
|
|
1617 if (nargs == 1)
|
|
1618 {
|
|
1619 i = 0;
|
|
1620 accum = make_int (1);
|
|
1621 }
|
|
1622 else
|
|
1623 {
|
|
1624 i = 1;
|
|
1625 accum = args[0];
|
|
1626 }
|
|
1627 for (; i < nargs; i++)
|
|
1628 {
|
|
1629 divisor = args[i];
|
|
1630 switch (promote_args (&accum, &divisor))
|
|
1631 {
|
|
1632 case FIXNUM_T:
|
|
1633 if (XREALINT (divisor) == 0) goto divide_by_zero;
|
|
1634 bignum_set_long (scratch_bignum, XREALINT (accum));
|
|
1635 bignum_set_long (scratch_bignum2, XREALINT (divisor));
|
|
1636 accum = make_ratio_bg (scratch_bignum, scratch_bignum2);
|
|
1637 break;
|
|
1638 case BIGNUM_T:
|
|
1639 if (bignum_sign (XBIGNUM_DATA (divisor)) == 0) goto divide_by_zero;
|
|
1640 accum = make_ratio_bg (XBIGNUM_DATA (accum), XBIGNUM_DATA (divisor));
|
|
1641 break;
|
|
1642 case RATIO_T:
|
|
1643 if (ratio_sign (XRATIO_DATA (divisor)) == 0) goto divide_by_zero;
|
|
1644 ratio_div (scratch_ratio, XRATIO_DATA (accum),
|
|
1645 XRATIO_DATA (divisor));
|
|
1646 accum = make_ratio_rt (scratch_ratio);
|
|
1647 break;
|
|
1648 case FLOAT_T:
|
|
1649 if (XFLOAT_DATA (divisor) == 0.0) goto divide_by_zero;
|
|
1650 accum = make_float (XFLOAT_DATA (accum) / XFLOAT_DATA (divisor));
|
|
1651 break;
|
|
1652 #ifdef HAVE_BIGFLOAT
|
|
1653 case BIGFLOAT_T:
|
|
1654 if (bigfloat_sign (XBIGFLOAT_DATA (divisor)) == 0)
|
|
1655 goto divide_by_zero;
|
|
1656 bigfloat_set_prec (scratch_bigfloat,
|
|
1657 max (XBIGFLOAT_GET_PREC (divisor),
|
|
1658 XBIGFLOAT_GET_PREC (accum)));
|
|
1659 bigfloat_div (scratch_bigfloat, XBIGFLOAT_DATA (accum),
|
|
1660 XBIGFLOAT_DATA (divisor));
|
|
1661 accum = make_bigfloat_bf (scratch_bigfloat);
|
|
1662 break;
|
|
1663 #endif
|
|
1664 }
|
|
1665 }
|
|
1666 return Fcanonicalize_number (accum);
|
|
1667
|
|
1668 divide_by_zero:
|
|
1669 Fsignal (Qarith_error, Qnil);
|
|
1670 return Qnil; /* not (usually) reached */
|
|
1671 }
|
|
1672 #endif /* HAVE_RATIO */
|
|
1673
|
428
|
1674 DEFUN ("/", Fquo, 1, MANY, 0, /*
|
|
1675 Return first argument divided by all the remaining arguments.
|
|
1676 The arguments must be numbers, characters or markers.
|
|
1677 With one argument, reciprocates the argument.
|
|
1678 */
|
|
1679 (int nargs, Lisp_Object *args))
|
|
1680 {
|
1983
|
1681 #ifdef WITH_NUMBER_TYPES
|
|
1682 REGISTER int i;
|
|
1683 Lisp_Object accum, divisor;
|
|
1684
|
|
1685 if (nargs == 1)
|
|
1686 {
|
|
1687 i = 0;
|
|
1688 accum = make_int (1);
|
|
1689 }
|
|
1690 else
|
|
1691 {
|
|
1692 i = 1;
|
|
1693 accum = args[0];
|
|
1694 }
|
|
1695 for (; i < nargs; i++)
|
|
1696 {
|
|
1697 divisor = args[i];
|
|
1698 switch (promote_args (&accum, &divisor))
|
|
1699 {
|
|
1700 case FIXNUM_T:
|
|
1701 if (XREALINT (divisor) == 0) goto divide_by_zero;
|
|
1702 accum = make_integer (XREALINT (accum) / XREALINT (divisor));
|
|
1703 break;
|
|
1704 #ifdef HAVE_BIGNUM
|
|
1705 case BIGNUM_T:
|
|
1706 if (bignum_sign (XBIGNUM_DATA (divisor)) == 0) goto divide_by_zero;
|
|
1707 bignum_div (scratch_bignum, XBIGNUM_DATA (accum),
|
|
1708 XBIGNUM_DATA (divisor));
|
|
1709 accum = make_bignum_bg (scratch_bignum);
|
|
1710 break;
|
|
1711 #endif
|
|
1712 #ifdef HAVE_RATIO
|
|
1713 case RATIO_T:
|
|
1714 if (ratio_sign (XRATIO_DATA (divisor)) == 0) goto divide_by_zero;
|
|
1715 ratio_div (scratch_ratio, XRATIO_DATA (accum),
|
|
1716 XRATIO_DATA (divisor));
|
|
1717 accum = make_ratio_rt (scratch_ratio);
|
|
1718 break;
|
|
1719 #endif
|
|
1720 case FLOAT_T:
|
|
1721 if (XFLOAT_DATA (divisor) == 0.0) goto divide_by_zero;
|
|
1722 accum = make_float (XFLOAT_DATA (accum) / XFLOAT_DATA (divisor));
|
|
1723 break;
|
|
1724 #ifdef HAVE_BIGFLOAT
|
|
1725 case BIGFLOAT_T:
|
|
1726 if (bigfloat_sign (XBIGFLOAT_DATA (divisor)) == 0)
|
|
1727 goto divide_by_zero;
|
|
1728 bigfloat_set_prec (scratch_bigfloat,
|
|
1729 max (XBIGFLOAT_GET_PREC (divisor),
|
|
1730 XBIGFLOAT_GET_PREC (accum)));
|
|
1731 bigfloat_div (scratch_bigfloat, XBIGFLOAT_DATA (accum),
|
|
1732 XBIGFLOAT_DATA (divisor));
|
|
1733 accum = make_bigfloat_bf (scratch_bigfloat);
|
|
1734 break;
|
|
1735 #endif
|
|
1736 }
|
|
1737 }
|
|
1738 return Fcanonicalize_number (accum);
|
|
1739 #else /* !WITH_NUMBER_TYPES */
|
428
|
1740 EMACS_INT iaccum;
|
|
1741 double daccum;
|
|
1742 Lisp_Object *args_end = args + nargs;
|
|
1743 int_or_double iod;
|
|
1744
|
|
1745 if (nargs == 1)
|
|
1746 iaccum = 1;
|
|
1747 else
|
|
1748 {
|
|
1749 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1750 if (iod.int_p)
|
|
1751 iaccum = iod.c.ival;
|
|
1752 else
|
|
1753 {
|
|
1754 daccum = iod.c.dval;
|
|
1755 goto divide_floats;
|
|
1756 }
|
|
1757 }
|
|
1758
|
|
1759 while (args < args_end)
|
|
1760 {
|
|
1761 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1762 if (iod.int_p)
|
|
1763 {
|
|
1764 if (iod.c.ival == 0) goto divide_by_zero;
|
|
1765 iaccum /= iod.c.ival;
|
|
1766 }
|
|
1767 else
|
|
1768 {
|
|
1769 if (iod.c.dval == 0) goto divide_by_zero;
|
|
1770 daccum = (double) iaccum / iod.c.dval;
|
|
1771 goto divide_floats;
|
|
1772 }
|
|
1773 }
|
|
1774
|
|
1775 return make_int (iaccum);
|
|
1776
|
|
1777 divide_floats:
|
|
1778 for (; args < args_end; args++)
|
|
1779 {
|
|
1780 double dval = number_char_or_marker_to_double (*args);
|
|
1781 if (dval == 0) goto divide_by_zero;
|
|
1782 daccum /= dval;
|
|
1783 }
|
|
1784 return make_float (daccum);
|
1983
|
1785 #endif /* WITH_NUMBER_TYPES */
|
428
|
1786
|
|
1787 divide_by_zero:
|
|
1788 Fsignal (Qarith_error, Qnil);
|
801
|
1789 return Qnil; /* not (usually) reached */
|
428
|
1790 }
|
|
1791
|
|
1792 DEFUN ("max", Fmax, 1, MANY, 0, /*
|
|
1793 Return largest of all the arguments.
|
1983
|
1794 All arguments must be real numbers, characters or markers.
|
428
|
1795 The value is always a number; markers and characters are converted
|
|
1796 to numbers.
|
|
1797 */
|
|
1798 (int nargs, Lisp_Object *args))
|
|
1799 {
|
1983
|
1800 #ifdef WITH_NUMBER_TYPES
|
|
1801 REGISTER int i, maxindex = 0;
|
|
1802 Lisp_Object comp1, comp2;
|
|
1803
|
|
1804 while (!(CHARP (args[0]) || MARKERP (args[0]) || REALP (args[0])))
|
|
1805 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]);
|
|
1806 if (CHARP (args[0]))
|
|
1807 args[0] = make_int (XCHAR (args[0]));
|
|
1808 else if (MARKERP (args[0]))
|
|
1809 args[0] = make_int (marker_position (args[0]));
|
|
1810 for (i = 1; i < nargs; i++)
|
|
1811 {
|
|
1812 comp1 = args[maxindex];
|
|
1813 comp2 = args[i];
|
|
1814 switch (promote_args (&comp1, &comp2))
|
|
1815 {
|
|
1816 case FIXNUM_T:
|
|
1817 if (XREALINT (comp1) < XREALINT (comp2))
|
|
1818 maxindex = i;
|
|
1819 break;
|
|
1820 #ifdef HAVE_BIGNUM
|
|
1821 case BIGNUM_T:
|
|
1822 if (bignum_lt (XBIGNUM_DATA (comp1), XBIGNUM_DATA (comp2)))
|
|
1823 maxindex = i;
|
|
1824 break;
|
|
1825 #endif
|
|
1826 #ifdef HAVE_RATIO
|
|
1827 case RATIO_T:
|
|
1828 if (ratio_lt (XRATIO_DATA (comp1), XRATIO_DATA (comp2)))
|
|
1829 maxindex = i;
|
|
1830 break;
|
|
1831 #endif
|
|
1832 case FLOAT_T:
|
|
1833 if (XFLOAT_DATA (comp1) < XFLOAT_DATA (comp2))
|
|
1834 maxindex = i;
|
|
1835 break;
|
|
1836 #ifdef HAVE_BIGFLOAT
|
|
1837 case BIGFLOAT_T:
|
|
1838 if (bigfloat_lt (XBIGFLOAT_DATA (comp1), XBIGFLOAT_DATA (comp2)))
|
|
1839 maxindex = i;
|
|
1840 break;
|
|
1841 #endif
|
|
1842 }
|
|
1843 }
|
|
1844 return args[maxindex];
|
|
1845 #else /* !WITH_NUMBER_TYPES */
|
428
|
1846 EMACS_INT imax;
|
|
1847 double dmax;
|
|
1848 Lisp_Object *args_end = args + nargs;
|
|
1849 int_or_double iod;
|
|
1850
|
|
1851 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1852 if (iod.int_p)
|
|
1853 imax = iod.c.ival;
|
|
1854 else
|
|
1855 {
|
|
1856 dmax = iod.c.dval;
|
|
1857 goto max_floats;
|
|
1858 }
|
|
1859
|
|
1860 while (args < args_end)
|
|
1861 {
|
|
1862 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1863 if (iod.int_p)
|
|
1864 {
|
|
1865 if (imax < iod.c.ival) imax = iod.c.ival;
|
|
1866 }
|
|
1867 else
|
|
1868 {
|
|
1869 dmax = (double) imax;
|
|
1870 if (dmax < iod.c.dval) dmax = iod.c.dval;
|
|
1871 goto max_floats;
|
|
1872 }
|
|
1873 }
|
|
1874
|
|
1875 return make_int (imax);
|
|
1876
|
|
1877 max_floats:
|
|
1878 while (args < args_end)
|
|
1879 {
|
|
1880 double dval = number_char_or_marker_to_double (*args++);
|
|
1881 if (dmax < dval) dmax = dval;
|
|
1882 }
|
|
1883 return make_float (dmax);
|
1983
|
1884 #endif /* WITH_NUMBER_TYPES */
|
428
|
1885 }
|
|
1886
|
|
1887 DEFUN ("min", Fmin, 1, MANY, 0, /*
|
|
1888 Return smallest of all the arguments.
|
|
1889 All arguments must be numbers, characters or markers.
|
|
1890 The value is always a number; markers and characters are converted
|
|
1891 to numbers.
|
|
1892 */
|
|
1893 (int nargs, Lisp_Object *args))
|
|
1894 {
|
1983
|
1895 #ifdef WITH_NUMBER_TYPES
|
|
1896 REGISTER int i, minindex = 0;
|
|
1897 Lisp_Object comp1, comp2;
|
|
1898
|
|
1899 while (!(CHARP (args[0]) || MARKERP (args[0]) || REALP (args[0])))
|
|
1900 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]);
|
|
1901 if (CHARP (args[0]))
|
|
1902 args[0] = make_int (XCHAR (args[0]));
|
|
1903 else if (MARKERP (args[0]))
|
|
1904 args[0] = make_int (marker_position (args[0]));
|
|
1905 for (i = 1; i < nargs; i++)
|
|
1906 {
|
|
1907 comp1 = args[minindex];
|
|
1908 comp2 = args[i];
|
|
1909 switch (promote_args (&comp1, &comp2))
|
|
1910 {
|
|
1911 case FIXNUM_T:
|
|
1912 if (XREALINT (comp1) > XREALINT (comp2))
|
|
1913 minindex = i;
|
|
1914 break;
|
|
1915 #ifdef HAVE_BIGNUM
|
|
1916 case BIGNUM_T:
|
|
1917 if (bignum_gt (XBIGNUM_DATA (comp1), XBIGNUM_DATA (comp2)))
|
|
1918 minindex = i;
|
|
1919 break;
|
|
1920 #endif
|
|
1921 #ifdef HAVE_RATIO
|
|
1922 case RATIO_T:
|
|
1923 if (ratio_gt (XRATIO_DATA (comp1), XRATIO_DATA (comp2)))
|
|
1924 minindex = i;
|
|
1925 break;
|
|
1926 #endif
|
|
1927 case FLOAT_T:
|
|
1928 if (XFLOAT_DATA (comp1) > XFLOAT_DATA (comp2))
|
|
1929 minindex = i;
|
|
1930 break;
|
|
1931 #ifdef HAVE_BIGFLOAT
|
|
1932 case BIGFLOAT_T:
|
|
1933 if (bigfloat_gt (XBIGFLOAT_DATA (comp1), XBIGFLOAT_DATA (comp2)))
|
|
1934 minindex = i;
|
|
1935 break;
|
|
1936 #endif
|
|
1937 }
|
|
1938 }
|
|
1939 return args[minindex];
|
|
1940 #else /* !WITH_NUMBER_TYPES */
|
428
|
1941 EMACS_INT imin;
|
|
1942 double dmin;
|
|
1943 Lisp_Object *args_end = args + nargs;
|
|
1944 int_or_double iod;
|
|
1945
|
|
1946 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1947 if (iod.int_p)
|
|
1948 imin = iod.c.ival;
|
|
1949 else
|
|
1950 {
|
|
1951 dmin = iod.c.dval;
|
|
1952 goto min_floats;
|
|
1953 }
|
|
1954
|
|
1955 while (args < args_end)
|
|
1956 {
|
|
1957 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1958 if (iod.int_p)
|
|
1959 {
|
|
1960 if (imin > iod.c.ival) imin = iod.c.ival;
|
|
1961 }
|
|
1962 else
|
|
1963 {
|
|
1964 dmin = (double) imin;
|
|
1965 if (dmin > iod.c.dval) dmin = iod.c.dval;
|
|
1966 goto min_floats;
|
|
1967 }
|
|
1968 }
|
|
1969
|
|
1970 return make_int (imin);
|
|
1971
|
|
1972 min_floats:
|
|
1973 while (args < args_end)
|
|
1974 {
|
|
1975 double dval = number_char_or_marker_to_double (*args++);
|
|
1976 if (dmin > dval) dmin = dval;
|
|
1977 }
|
|
1978 return make_float (dmin);
|
1983
|
1979 #endif /* WITH_NUMBER_TYPES */
|
428
|
1980 }
|
|
1981
|
|
1982 DEFUN ("logand", Flogand, 0, MANY, 0, /*
|
|
1983 Return bitwise-and of all the arguments.
|
|
1984 Arguments may be integers, or markers or characters converted to integers.
|
|
1985 */
|
|
1986 (int nargs, Lisp_Object *args))
|
|
1987 {
|
1983
|
1988 #ifdef HAVE_BIGNUM
|
|
1989 REGISTER int i;
|
|
1990 Lisp_Object result, other;
|
|
1991
|
|
1992 if (nargs == 0)
|
|
1993 return make_int (~0);
|
|
1994
|
|
1995 while (!(CHARP (args[0]) || MARKERP (args[0]) || INTEGERP (args[0])))
|
|
1996 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]);
|
|
1997
|
|
1998 result = args[0];
|
|
1999 if (CHARP (result))
|
|
2000 result = make_int (XCHAR (result));
|
|
2001 else if (MARKERP (result))
|
|
2002 result = make_int (marker_position (result));
|
|
2003 for (i = 1; i < nargs; i++)
|
|
2004 {
|
|
2005 while (!(CHARP (args[i]) || MARKERP (args[i]) || INTEGERP (args[i])))
|
|
2006 args[i] = wrong_type_argument (Qnumber_char_or_marker_p, args[i]);
|
|
2007 other = args[i];
|
1995
|
2008 switch (promote_args (&result, &other))
|
1983
|
2009 {
|
|
2010 case FIXNUM_T:
|
1995
|
2011 result = make_int (XREALINT (result) & XREALINT (other));
|
1983
|
2012 break;
|
|
2013 case BIGNUM_T:
|
|
2014 bignum_and (scratch_bignum, XBIGNUM_DATA (result),
|
|
2015 XBIGNUM_DATA (other));
|
|
2016 result = make_bignum_bg (scratch_bignum);
|
|
2017 break;
|
|
2018 }
|
|
2019 }
|
|
2020 return Fcanonicalize_number (result);
|
|
2021 #else /* !HAVE_BIGNUM */
|
428
|
2022 EMACS_INT bits = ~0;
|
|
2023 Lisp_Object *args_end = args + nargs;
|
|
2024
|
|
2025 while (args < args_end)
|
|
2026 bits &= integer_char_or_marker_to_int (*args++);
|
|
2027
|
|
2028 return make_int (bits);
|
1983
|
2029 #endif /* HAVE_BIGNUM */
|
428
|
2030 }
|
|
2031
|
|
2032 DEFUN ("logior", Flogior, 0, MANY, 0, /*
|
|
2033 Return bitwise-or of all the arguments.
|
|
2034 Arguments may be integers, or markers or characters converted to integers.
|
|
2035 */
|
|
2036 (int nargs, Lisp_Object *args))
|
|
2037 {
|
1983
|
2038 #ifdef HAVE_BIGNUM
|
|
2039 REGISTER int i;
|
|
2040 Lisp_Object result, other;
|
|
2041
|
|
2042 if (nargs == 0)
|
|
2043 return make_int (0);
|
|
2044
|
|
2045 while (!(CHARP (args[0]) || MARKERP (args[0]) || INTEGERP (args[0])))
|
|
2046 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]);
|
|
2047
|
|
2048 result = args[0];
|
|
2049 if (CHARP (result))
|
|
2050 result = make_int (XCHAR (result));
|
|
2051 else if (MARKERP (result))
|
|
2052 result = make_int (marker_position (result));
|
|
2053 for (i = 1; i < nargs; i++)
|
|
2054 {
|
|
2055 while (!(CHARP (args[i]) || MARKERP (args[i]) || INTEGERP (args[i])))
|
|
2056 args[i] = wrong_type_argument (Qnumber_char_or_marker_p, args[i]);
|
|
2057 other = args[i];
|
|
2058 switch (promote_args (&result, &other))
|
|
2059 {
|
|
2060 case FIXNUM_T:
|
1992
|
2061 result = make_int (XREALINT (result) | XREALINT (other));
|
1983
|
2062 break;
|
|
2063 case BIGNUM_T:
|
|
2064 bignum_ior (scratch_bignum, XBIGNUM_DATA (result),
|
|
2065 XBIGNUM_DATA (other));
|
|
2066 result = make_bignum_bg (scratch_bignum);
|
|
2067 break;
|
|
2068 }
|
|
2069 }
|
|
2070 return Fcanonicalize_number (result);
|
|
2071 #else /* !HAVE_BIGNUM */
|
428
|
2072 EMACS_INT bits = 0;
|
|
2073 Lisp_Object *args_end = args + nargs;
|
|
2074
|
|
2075 while (args < args_end)
|
|
2076 bits |= integer_char_or_marker_to_int (*args++);
|
|
2077
|
|
2078 return make_int (bits);
|
1983
|
2079 #endif /* HAVE_BIGNUM */
|
428
|
2080 }
|
|
2081
|
|
2082 DEFUN ("logxor", Flogxor, 0, MANY, 0, /*
|
|
2083 Return bitwise-exclusive-or of all the arguments.
|
|
2084 Arguments may be integers, or markers or characters converted to integers.
|
|
2085 */
|
|
2086 (int nargs, Lisp_Object *args))
|
|
2087 {
|
1983
|
2088 #ifdef HAVE_BIGNUM
|
|
2089 REGISTER int i;
|
|
2090 Lisp_Object result, other;
|
|
2091
|
|
2092 if (nargs == 0)
|
|
2093 return make_int (0);
|
|
2094
|
|
2095 while (!(CHARP (args[0]) || MARKERP (args[0]) || INTEGERP (args[0])))
|
|
2096 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]);
|
|
2097
|
|
2098 result = args[0];
|
|
2099 if (CHARP (result))
|
|
2100 result = make_int (XCHAR (result));
|
|
2101 else if (MARKERP (result))
|
|
2102 result = make_int (marker_position (result));
|
|
2103 for (i = 1; i < nargs; i++)
|
|
2104 {
|
|
2105 while (!(CHARP (args[i]) || MARKERP (args[i]) || INTEGERP (args[i])))
|
|
2106 args[i] = wrong_type_argument (Qnumber_char_or_marker_p, args[i]);
|
|
2107 other = args[i];
|
|
2108 if (promote_args (&result, &other) == FIXNUM_T)
|
|
2109 {
|
|
2110 result = make_int (XREALINT (result) ^ XREALINT (other));
|
|
2111 }
|
|
2112 else
|
|
2113 {
|
|
2114 bignum_xor (scratch_bignum, XBIGNUM_DATA (result),
|
|
2115 XBIGNUM_DATA (other));
|
|
2116 result = make_bignum_bg (scratch_bignum);
|
|
2117 }
|
|
2118 }
|
|
2119 return Fcanonicalize_number (result);
|
|
2120 #else /* !HAVE_BIGNUM */
|
428
|
2121 EMACS_INT bits = 0;
|
|
2122 Lisp_Object *args_end = args + nargs;
|
|
2123
|
|
2124 while (args < args_end)
|
|
2125 bits ^= integer_char_or_marker_to_int (*args++);
|
|
2126
|
|
2127 return make_int (bits);
|
1983
|
2128 #endif /* !HAVE_BIGNUM */
|
428
|
2129 }
|
|
2130
|
|
2131 DEFUN ("lognot", Flognot, 1, 1, 0, /*
|
|
2132 Return the bitwise complement of NUMBER.
|
|
2133 NUMBER may be an integer, marker or character converted to integer.
|
|
2134 */
|
|
2135 (number))
|
|
2136 {
|
1983
|
2137 #ifdef HAVE_BIGNUM
|
|
2138 if (BIGNUMP (number))
|
|
2139 {
|
|
2140 bignum_not (scratch_bignum, XBIGNUM_DATA (number));
|
|
2141 return make_bignum_bg (scratch_bignum);
|
|
2142 }
|
|
2143 #endif /* HAVE_BIGNUM */
|
428
|
2144 return make_int (~ integer_char_or_marker_to_int (number));
|
|
2145 }
|
|
2146
|
|
2147 DEFUN ("%", Frem, 2, 2, 0, /*
|
|
2148 Return remainder of first arg divided by second.
|
|
2149 Both must be integers, characters or markers.
|
|
2150 */
|
444
|
2151 (number1, number2))
|
428
|
2152 {
|
1983
|
2153 #ifdef HAVE_BIGNUM
|
|
2154 while (!(CHARP (number1) || MARKERP (number1) || INTEGERP (number1)))
|
|
2155 number1 = wrong_type_argument (Qnumber_char_or_marker_p, number1);
|
|
2156 while (!(CHARP (number2) || MARKERP (number2) || INTEGERP (number2)))
|
|
2157 number2 = wrong_type_argument (Qnumber_char_or_marker_p, number2);
|
|
2158
|
|
2159 if (promote_args (&number1, &number2) == FIXNUM_T)
|
|
2160 {
|
|
2161 if (XREALINT (number2) == 0)
|
|
2162 Fsignal (Qarith_error, Qnil);
|
|
2163 return make_int (XREALINT (number1) % XREALINT (number2));
|
|
2164 }
|
|
2165 else
|
|
2166 {
|
|
2167 if (bignum_sign (XBIGNUM_DATA (number2)) == 0)
|
|
2168 Fsignal (Qarith_error, Qnil);
|
|
2169 bignum_mod (scratch_bignum, XBIGNUM_DATA (number1),
|
|
2170 XBIGNUM_DATA (number2));
|
|
2171 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
2172 }
|
|
2173 #else /* !HAVE_BIGNUM */
|
444
|
2174 EMACS_INT ival1 = integer_char_or_marker_to_int (number1);
|
|
2175 EMACS_INT ival2 = integer_char_or_marker_to_int (number2);
|
428
|
2176
|
|
2177 if (ival2 == 0)
|
|
2178 Fsignal (Qarith_error, Qnil);
|
|
2179
|
|
2180 return make_int (ival1 % ival2);
|
1983
|
2181 #endif /* HAVE_BIGNUM */
|
428
|
2182 }
|
|
2183
|
|
2184 /* Note, ANSI *requires* the presence of the fmod() library routine.
|
|
2185 If your system doesn't have it, complain to your vendor, because
|
|
2186 that is a bug. */
|
|
2187
|
|
2188 #ifndef HAVE_FMOD
|
|
2189 double
|
|
2190 fmod (double f1, double f2)
|
|
2191 {
|
|
2192 if (f2 < 0.0)
|
|
2193 f2 = -f2;
|
|
2194 return f1 - f2 * floor (f1/f2);
|
|
2195 }
|
|
2196 #endif /* ! HAVE_FMOD */
|
|
2197
|
|
2198
|
|
2199 DEFUN ("mod", Fmod, 2, 2, 0, /*
|
|
2200 Return X modulo Y.
|
|
2201 The result falls between zero (inclusive) and Y (exclusive).
|
|
2202 Both X and Y must be numbers, characters or markers.
|
|
2203 If either argument is a float, a float will be returned.
|
|
2204 */
|
|
2205 (x, y))
|
|
2206 {
|
1983
|
2207 #ifdef WITH_NUMBER_TYPES
|
|
2208 while (!(CHARP (x) || MARKERP (x) || REALP (x)))
|
|
2209 x = wrong_type_argument (Qnumber_char_or_marker_p, x);
|
|
2210 while (!(CHARP (y) || MARKERP (y) || REALP (y)))
|
|
2211 y = wrong_type_argument (Qnumber_char_or_marker_p, y);
|
|
2212 switch (promote_args (&x, &y))
|
|
2213 {
|
|
2214 case FIXNUM_T:
|
|
2215 {
|
|
2216 EMACS_INT ival;
|
|
2217 if (XREALINT (y) == 0) goto divide_by_zero;
|
|
2218 ival = XREALINT (x) % XREALINT (y);
|
|
2219 /* If the "remainder" comes out with the wrong sign, fix it. */
|
|
2220 if (XREALINT (y) < 0 ? ival > 0 : ival < 0)
|
|
2221 ival += XREALINT (y);
|
|
2222 return make_int (ival);
|
|
2223 }
|
|
2224 #ifdef HAVE_BIGNUM
|
|
2225 case BIGNUM_T:
|
|
2226 if (bignum_sign (XBIGNUM_DATA (y)) == 0) goto divide_by_zero;
|
|
2227 bignum_mod (scratch_bignum, XBIGNUM_DATA (x), XBIGNUM_DATA (y));
|
|
2228 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
2229 #endif
|
|
2230 #ifdef HAVE_RATIO
|
|
2231 case RATIO_T:
|
|
2232 if (ratio_sign (XRATIO_DATA (y)) == 0) goto divide_by_zero;
|
|
2233 ratio_div (scratch_ratio, XRATIO_DATA (x), XRATIO_DATA (y));
|
|
2234 bignum_div (scratch_bignum, ratio_numerator (scratch_ratio),
|
|
2235 ratio_denominator (scratch_ratio));
|
|
2236 ratio_set_bignum (scratch_ratio, scratch_bignum);
|
|
2237 ratio_mul (scratch_ratio, scratch_ratio, XRATIO_DATA (y));
|
|
2238 ratio_sub (scratch_ratio, XRATIO_DATA (x), scratch_ratio);
|
|
2239 return Fcanonicalize_number (make_ratio_rt (scratch_ratio));
|
|
2240 #endif
|
|
2241 case FLOAT_T:
|
|
2242 {
|
|
2243 double dval;
|
|
2244 if (XFLOAT_DATA (y) == 0.0) goto divide_by_zero;
|
|
2245 dval = fmod (XFLOAT_DATA (x), XFLOAT_DATA (y));
|
|
2246 /* If the "remainder" comes out with the wrong sign, fix it. */
|
|
2247 if (XFLOAT_DATA (y) < 0 ? dval > 0 : dval < 0)
|
|
2248 dval += XFLOAT_DATA (y);
|
|
2249 return make_float (dval);
|
|
2250 }
|
|
2251 #ifdef HAVE_BIGFLOAT
|
|
2252 case BIGFLOAT_T:
|
|
2253 bigfloat_set_prec (scratch_bigfloat,
|
|
2254 max (XBIGFLOAT_GET_PREC (x), XBIGFLOAT_GET_PREC (y)));
|
|
2255 bigfloat_div (scratch_bigfloat, XBIGFLOAT_DATA (x), XBIGFLOAT_DATA (y));
|
|
2256 bigfloat_trunc (scratch_bigfloat, scratch_bigfloat);
|
|
2257 bigfloat_mul (scratch_bigfloat, scratch_bigfloat, XBIGFLOAT_DATA (y));
|
|
2258 bigfloat_sub (scratch_bigfloat, XBIGFLOAT_DATA (x), scratch_bigfloat);
|
|
2259 return make_bigfloat_bf (scratch_bigfloat);
|
|
2260 #endif
|
|
2261 }
|
|
2262 #else /* !WITH_NUMBER_TYPES */
|
428
|
2263 int_or_double iod1, iod2;
|
|
2264 number_char_or_marker_to_int_or_double (x, &iod1);
|
|
2265 number_char_or_marker_to_int_or_double (y, &iod2);
|
|
2266
|
|
2267 if (!iod1.int_p || !iod2.int_p)
|
|
2268 {
|
|
2269 double dval1 = iod1.int_p ? (double) iod1.c.ival : iod1.c.dval;
|
|
2270 double dval2 = iod2.int_p ? (double) iod2.c.ival : iod2.c.dval;
|
|
2271 if (dval2 == 0) goto divide_by_zero;
|
|
2272 dval1 = fmod (dval1, dval2);
|
|
2273
|
|
2274 /* If the "remainder" comes out with the wrong sign, fix it. */
|
|
2275 if (dval2 < 0 ? dval1 > 0 : dval1 < 0)
|
|
2276 dval1 += dval2;
|
|
2277
|
|
2278 return make_float (dval1);
|
|
2279 }
|
1104
|
2280
|
428
|
2281 {
|
|
2282 EMACS_INT ival;
|
|
2283 if (iod2.c.ival == 0) goto divide_by_zero;
|
|
2284
|
|
2285 ival = iod1.c.ival % iod2.c.ival;
|
|
2286
|
|
2287 /* If the "remainder" comes out with the wrong sign, fix it. */
|
|
2288 if (iod2.c.ival < 0 ? ival > 0 : ival < 0)
|
|
2289 ival += iod2.c.ival;
|
|
2290
|
|
2291 return make_int (ival);
|
|
2292 }
|
1983
|
2293 #endif /* WITH_NUMBER_TYPES */
|
428
|
2294
|
|
2295 divide_by_zero:
|
|
2296 Fsignal (Qarith_error, Qnil);
|
801
|
2297 return Qnil; /* not (usually) reached */
|
428
|
2298 }
|
|
2299
|
|
2300 DEFUN ("ash", Fash, 2, 2, 0, /*
|
|
2301 Return VALUE with its bits shifted left by COUNT.
|
|
2302 If COUNT is negative, shifting is actually to the right.
|
|
2303 In this case, the sign bit is duplicated.
|
1983
|
2304 This function cannot be applied to bignums, as there is no leftmost sign bit
|
|
2305 to be duplicated. Use `lsh' instead.
|
428
|
2306 */
|
|
2307 (value, count))
|
|
2308 {
|
|
2309 CHECK_INT_COERCE_CHAR (value);
|
|
2310 CONCHECK_INT (count);
|
|
2311
|
|
2312 return make_int (XINT (count) > 0 ?
|
|
2313 XINT (value) << XINT (count) :
|
|
2314 XINT (value) >> -XINT (count));
|
|
2315 }
|
|
2316
|
|
2317 DEFUN ("lsh", Flsh, 2, 2, 0, /*
|
|
2318 Return VALUE with its bits shifted left by COUNT.
|
|
2319 If COUNT is negative, shifting is actually to the right.
|
|
2320 In this case, zeros are shifted in on the left.
|
|
2321 */
|
|
2322 (value, count))
|
|
2323 {
|
1983
|
2324 #ifdef HAVE_BIGNUM
|
|
2325 while (!(CHARP (value) || MARKERP (value) || INTEGERP (value)))
|
|
2326 wrong_type_argument (Qnumber_char_or_marker_p, value);
|
|
2327 CONCHECK_INTEGER (count);
|
|
2328
|
|
2329 if (promote_args (&value, &count) == FIXNUM_T)
|
|
2330 {
|
|
2331 if (XREALINT (count) <= 0)
|
|
2332 return make_int (XREALINT (value) >> -XREALINT (count));
|
|
2333 /* Use bignums to avoid overflow */
|
|
2334 bignum_set_long (scratch_bignum2, XREALINT (value));
|
|
2335 bignum_lshift (scratch_bignum, scratch_bignum2, XREALINT (count));
|
|
2336 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
2337 }
|
|
2338 else
|
|
2339 {
|
|
2340 if (bignum_sign (XBIGNUM_DATA (count)) <= 0)
|
|
2341 {
|
|
2342 bignum_neg (scratch_bignum, XBIGNUM_DATA (count));
|
|
2343 if (!bignum_fits_ulong_p (scratch_bignum))
|
|
2344 args_out_of_range (Qnumber_char_or_marker_p, count);
|
|
2345 bignum_rshift (scratch_bignum2, XBIGNUM_DATA (value),
|
|
2346 bignum_to_ulong (scratch_bignum));
|
|
2347 }
|
|
2348 else
|
|
2349 {
|
|
2350 if (!bignum_fits_ulong_p (XBIGNUM_DATA (count)))
|
|
2351 args_out_of_range (Qnumber_char_or_marker_p, count);
|
|
2352 bignum_lshift (scratch_bignum2, XBIGNUM_DATA (value),
|
|
2353 bignum_to_ulong (XBIGNUM_DATA (count)));
|
|
2354 }
|
|
2355 return Fcanonicalize_number (make_bignum_bg (scratch_bignum2));
|
|
2356 }
|
|
2357 #else /* !HAVE_BIGNUM */
|
428
|
2358 CHECK_INT_COERCE_CHAR (value);
|
|
2359 CONCHECK_INT (count);
|
|
2360
|
|
2361 return make_int (XINT (count) > 0 ?
|
|
2362 XUINT (value) << XINT (count) :
|
|
2363 XUINT (value) >> -XINT (count));
|
1983
|
2364 #endif /* HAVE_BIGNUM */
|
428
|
2365 }
|
|
2366
|
|
2367 DEFUN ("1+", Fadd1, 1, 1, 0, /*
|
|
2368 Return NUMBER plus one. NUMBER may be a number, character or marker.
|
|
2369 Markers and characters are converted to integers.
|
|
2370 */
|
|
2371 (number))
|
|
2372 {
|
|
2373 retry:
|
|
2374
|
1983
|
2375 if (INTP (number)) return make_integer (XINT (number) + 1);
|
|
2376 if (CHARP (number)) return make_integer (XCHAR (number) + 1);
|
|
2377 if (MARKERP (number)) return make_integer (marker_position (number) + 1);
|
428
|
2378 if (FLOATP (number)) return make_float (XFLOAT_DATA (number) + 1.0);
|
1983
|
2379 #ifdef HAVE_BIGNUM
|
|
2380 if (BIGNUMP (number))
|
|
2381 {
|
|
2382 bignum_set_long (scratch_bignum, 1L);
|
|
2383 bignum_add (scratch_bignum2, XBIGNUM_DATA (number), scratch_bignum);
|
|
2384 return Fcanonicalize_number (make_bignum_bg (scratch_bignum2));
|
|
2385 }
|
|
2386 #endif
|
|
2387 #ifdef HAVE_RATIO
|
|
2388 if (RATIOP (number))
|
|
2389 {
|
|
2390 ratio_set_long (scratch_ratio, 1L);
|
|
2391 ratio_add (scratch_ratio, XRATIO_DATA (number), scratch_ratio);
|
|
2392 /* No need to canonicalize after adding 1 */
|
|
2393 return make_ratio_rt (scratch_ratio);
|
|
2394 }
|
|
2395 #endif
|
|
2396 #ifdef HAVE_BIGFLOAT
|
|
2397 if (BIGFLOATP (number))
|
|
2398 {
|
|
2399 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (number));
|
|
2400 bigfloat_set_long (scratch_bigfloat, 1L);
|
|
2401 bigfloat_add (scratch_bigfloat, XBIGFLOAT_DATA (number),
|
|
2402 scratch_bigfloat);
|
|
2403 return make_bigfloat_bf (scratch_bigfloat);
|
|
2404 }
|
|
2405 #endif
|
428
|
2406
|
|
2407 number = wrong_type_argument (Qnumber_char_or_marker_p, number);
|
|
2408 goto retry;
|
|
2409 }
|
|
2410
|
|
2411 DEFUN ("1-", Fsub1, 1, 1, 0, /*
|
|
2412 Return NUMBER minus one. NUMBER may be a number, character or marker.
|
|
2413 Markers and characters are converted to integers.
|
|
2414 */
|
|
2415 (number))
|
|
2416 {
|
|
2417 retry:
|
|
2418
|
1983
|
2419 if (INTP (number)) return make_integer (XINT (number) - 1);
|
|
2420 if (CHARP (number)) return make_integer (XCHAR (number) - 1);
|
|
2421 if (MARKERP (number)) return make_integer (marker_position (number) - 1);
|
428
|
2422 if (FLOATP (number)) return make_float (XFLOAT_DATA (number) - 1.0);
|
1983
|
2423 #ifdef HAVE_BIGNUM
|
|
2424 if (BIGNUMP (number))
|
|
2425 {
|
|
2426 bignum_set_long (scratch_bignum, 1L);
|
|
2427 bignum_sub (scratch_bignum2, XBIGNUM_DATA (number), scratch_bignum);
|
|
2428 return Fcanonicalize_number (make_bignum_bg (scratch_bignum2));
|
|
2429 }
|
|
2430 #endif
|
|
2431 #ifdef HAVE_RATIO
|
|
2432 if (RATIOP (number))
|
|
2433 {
|
|
2434 ratio_set_long (scratch_ratio, 1L);
|
|
2435 ratio_sub (scratch_ratio, XRATIO_DATA (number), scratch_ratio);
|
|
2436 /* No need to canonicalize after subtracting 1 */
|
|
2437 return make_ratio_rt (scratch_ratio);
|
|
2438 }
|
|
2439 #endif
|
|
2440 #ifdef HAVE_BIGFLOAT
|
|
2441 if (BIGFLOATP (number))
|
|
2442 {
|
|
2443 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (number));
|
|
2444 bigfloat_set_long (scratch_bigfloat, 1L);
|
|
2445 bigfloat_sub (scratch_bigfloat, XBIGFLOAT_DATA (number),
|
|
2446 scratch_bigfloat);
|
|
2447 return make_bigfloat_bf (scratch_bigfloat);
|
|
2448 }
|
|
2449 #endif
|
428
|
2450
|
|
2451 number = wrong_type_argument (Qnumber_char_or_marker_p, number);
|
|
2452 goto retry;
|
|
2453 }
|
|
2454
|
|
2455
|
|
2456 /************************************************************************/
|
|
2457 /* weak lists */
|
|
2458 /************************************************************************/
|
|
2459
|
|
2460 /* A weak list is like a normal list except that elements automatically
|
|
2461 disappear when no longer in use, i.e. when no longer GC-protected.
|
|
2462 The basic idea is that we don't mark the elements during GC, but
|
|
2463 wait for them to be marked elsewhere. If they're not marked, we
|
|
2464 remove them. This is analogous to weak hash tables; see the explanation
|
|
2465 there for more info. */
|
|
2466
|
|
2467 static Lisp_Object Vall_weak_lists; /* Gemarke es nicht!!! */
|
|
2468
|
|
2469 static Lisp_Object encode_weak_list_type (enum weak_list_type type);
|
|
2470
|
|
2471 static Lisp_Object
|
|
2472 mark_weak_list (Lisp_Object obj)
|
|
2473 {
|
|
2474 return Qnil; /* nichts ist gemarkt */
|
|
2475 }
|
|
2476
|
|
2477 static void
|
|
2478 print_weak_list (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
2479 {
|
|
2480 if (print_readably)
|
563
|
2481 printing_unreadable_object ("#<weak-list>");
|
428
|
2482
|
800
|
2483 write_fmt_string_lisp (printcharfun, "#<weak-list %s %S>", 2,
|
|
2484 encode_weak_list_type (XWEAK_LIST (obj)->type),
|
|
2485 XWEAK_LIST (obj)->list);
|
428
|
2486 }
|
|
2487
|
|
2488 static int
|
|
2489 weak_list_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
2490 {
|
|
2491 struct weak_list *w1 = XWEAK_LIST (obj1);
|
|
2492 struct weak_list *w2 = XWEAK_LIST (obj2);
|
|
2493
|
|
2494 return ((w1->type == w2->type) &&
|
|
2495 internal_equal (w1->list, w2->list, depth + 1));
|
|
2496 }
|
|
2497
|
665
|
2498 static Hashcode
|
428
|
2499 weak_list_hash (Lisp_Object obj, int depth)
|
|
2500 {
|
|
2501 struct weak_list *w = XWEAK_LIST (obj);
|
|
2502
|
665
|
2503 return HASH2 ((Hashcode) w->type,
|
428
|
2504 internal_hash (w->list, depth + 1));
|
|
2505 }
|
|
2506
|
|
2507 Lisp_Object
|
|
2508 make_weak_list (enum weak_list_type type)
|
|
2509 {
|
|
2510 Lisp_Object result;
|
|
2511 struct weak_list *wl =
|
|
2512 alloc_lcrecord_type (struct weak_list, &lrecord_weak_list);
|
|
2513
|
|
2514 wl->list = Qnil;
|
|
2515 wl->type = type;
|
793
|
2516 result = wrap_weak_list (wl);
|
428
|
2517 wl->next_weak = Vall_weak_lists;
|
|
2518 Vall_weak_lists = result;
|
|
2519 return result;
|
|
2520 }
|
|
2521
|
1204
|
2522 static const struct memory_description weak_list_description[] = {
|
1598
|
2523 { XD_LISP_OBJECT, offsetof (struct weak_list, list),
|
|
2524 0, 0, XD_FLAG_NO_KKCC },
|
|
2525 { XD_LO_LINK, offsetof (struct weak_list, next_weak),
|
|
2526 0, 0, XD_FLAG_NO_KKCC },
|
428
|
2527 { XD_END }
|
|
2528 };
|
|
2529
|
934
|
2530 DEFINE_LRECORD_IMPLEMENTATION ("weak-list", weak_list,
|
|
2531 1, /*dumpable-flag*/
|
|
2532 mark_weak_list, print_weak_list,
|
|
2533 0, weak_list_equal, weak_list_hash,
|
|
2534 weak_list_description,
|
|
2535 struct weak_list);
|
428
|
2536 /*
|
|
2537 -- we do not mark the list elements (either the elements themselves
|
|
2538 or the cons cells that hold them) in the normal marking phase.
|
|
2539 -- at the end of marking, we go through all weak lists that are
|
|
2540 marked, and mark the cons cells that hold all marked
|
|
2541 objects, and possibly parts of the objects themselves.
|
|
2542 (See alloc.c, "after-mark".)
|
|
2543 -- after that, we prune away all the cons cells that are not marked.
|
|
2544
|
|
2545 WARNING WARNING WARNING WARNING WARNING:
|
|
2546
|
|
2547 The code in the following two functions is *unbelievably* tricky.
|
|
2548 Don't mess with it. You'll be sorry.
|
|
2549
|
|
2550 Linked lists just majorly suck, d'ya know?
|
|
2551 */
|
|
2552
|
|
2553 int
|
|
2554 finish_marking_weak_lists (void)
|
|
2555 {
|
|
2556 Lisp_Object rest;
|
|
2557 int did_mark = 0;
|
|
2558
|
|
2559 for (rest = Vall_weak_lists;
|
|
2560 !NILP (rest);
|
|
2561 rest = XWEAK_LIST (rest)->next_weak)
|
|
2562 {
|
|
2563 Lisp_Object rest2;
|
|
2564 enum weak_list_type type = XWEAK_LIST (rest)->type;
|
|
2565
|
|
2566 if (! marked_p (rest))
|
|
2567 /* The weak list is probably garbage. Ignore it. */
|
|
2568 continue;
|
|
2569
|
|
2570 for (rest2 = XWEAK_LIST (rest)->list;
|
|
2571 /* We need to be trickier since we're inside of GC;
|
|
2572 use CONSP instead of !NILP in case of user-visible
|
|
2573 imperfect lists */
|
|
2574 CONSP (rest2);
|
|
2575 rest2 = XCDR (rest2))
|
|
2576 {
|
|
2577 Lisp_Object elem;
|
|
2578 /* If the element is "marked" (meaning depends on the type
|
|
2579 of weak list), we need to mark the cons containing the
|
|
2580 element, and maybe the element itself (if only some part
|
|
2581 was already marked). */
|
|
2582 int need_to_mark_cons = 0;
|
|
2583 int need_to_mark_elem = 0;
|
|
2584
|
|
2585 /* If a cons is already marked, then its car is already marked
|
|
2586 (either because of an external pointer or because of
|
|
2587 a previous call to this function), and likewise for all
|
|
2588 the rest of the elements in the list, so we can stop now. */
|
|
2589 if (marked_p (rest2))
|
|
2590 break;
|
|
2591
|
|
2592 elem = XCAR (rest2);
|
|
2593
|
|
2594 switch (type)
|
|
2595 {
|
|
2596 case WEAK_LIST_SIMPLE:
|
|
2597 if (marked_p (elem))
|
|
2598 need_to_mark_cons = 1;
|
|
2599 break;
|
|
2600
|
|
2601 case WEAK_LIST_ASSOC:
|
|
2602 if (!CONSP (elem))
|
|
2603 {
|
|
2604 /* just leave bogus elements there */
|
|
2605 need_to_mark_cons = 1;
|
|
2606 need_to_mark_elem = 1;
|
|
2607 }
|
|
2608 else if (marked_p (XCAR (elem)) &&
|
|
2609 marked_p (XCDR (elem)))
|
|
2610 {
|
|
2611 need_to_mark_cons = 1;
|
|
2612 /* We still need to mark elem, because it's
|
|
2613 probably not marked. */
|
|
2614 need_to_mark_elem = 1;
|
|
2615 }
|
|
2616 break;
|
|
2617
|
|
2618 case WEAK_LIST_KEY_ASSOC:
|
|
2619 if (!CONSP (elem))
|
|
2620 {
|
|
2621 /* just leave bogus elements there */
|
|
2622 need_to_mark_cons = 1;
|
|
2623 need_to_mark_elem = 1;
|
|
2624 }
|
|
2625 else if (marked_p (XCAR (elem)))
|
|
2626 {
|
|
2627 need_to_mark_cons = 1;
|
|
2628 /* We still need to mark elem and XCDR (elem);
|
|
2629 marking elem does both */
|
|
2630 need_to_mark_elem = 1;
|
|
2631 }
|
|
2632 break;
|
|
2633
|
|
2634 case WEAK_LIST_VALUE_ASSOC:
|
|
2635 if (!CONSP (elem))
|
|
2636 {
|
|
2637 /* just leave bogus elements there */
|
|
2638 need_to_mark_cons = 1;
|
|
2639 need_to_mark_elem = 1;
|
|
2640 }
|
|
2641 else if (marked_p (XCDR (elem)))
|
|
2642 {
|
|
2643 need_to_mark_cons = 1;
|
|
2644 /* We still need to mark elem and XCAR (elem);
|
|
2645 marking elem does both */
|
|
2646 need_to_mark_elem = 1;
|
|
2647 }
|
|
2648 break;
|
|
2649
|
442
|
2650 case WEAK_LIST_FULL_ASSOC:
|
|
2651 if (!CONSP (elem))
|
|
2652 {
|
|
2653 /* just leave bogus elements there */
|
|
2654 need_to_mark_cons = 1;
|
|
2655 need_to_mark_elem = 1;
|
|
2656 }
|
|
2657 else if (marked_p (XCAR (elem)) ||
|
|
2658 marked_p (XCDR (elem)))
|
|
2659 {
|
|
2660 need_to_mark_cons = 1;
|
|
2661 /* We still need to mark elem and XCAR (elem);
|
|
2662 marking elem does both */
|
|
2663 need_to_mark_elem = 1;
|
|
2664 }
|
|
2665 break;
|
|
2666
|
428
|
2667 default:
|
|
2668 abort ();
|
|
2669 }
|
|
2670
|
|
2671 if (need_to_mark_elem && ! marked_p (elem))
|
|
2672 {
|
1598
|
2673 #ifdef USE_KKCC
|
|
2674 kkcc_gc_stack_push_lisp_object (elem);
|
|
2675 #else /* NOT USE_KKCC */
|
428
|
2676 mark_object (elem);
|
1598
|
2677 #endif /* NOT USE_KKCC */
|
428
|
2678 did_mark = 1;
|
|
2679 }
|
|
2680
|
|
2681 /* We also need to mark the cons that holds the elem or
|
|
2682 assoc-pair. We do *not* want to call (mark_object) here
|
|
2683 because that will mark the entire list; we just want to
|
|
2684 mark the cons itself.
|
|
2685 */
|
|
2686 if (need_to_mark_cons)
|
|
2687 {
|
|
2688 Lisp_Cons *c = XCONS (rest2);
|
|
2689 if (!CONS_MARKED_P (c))
|
|
2690 {
|
|
2691 MARK_CONS (c);
|
|
2692 did_mark = 1;
|
|
2693 }
|
|
2694 }
|
|
2695 }
|
|
2696
|
|
2697 /* In case of imperfect list, need to mark the final cons
|
|
2698 because we're not removing it */
|
|
2699 if (!NILP (rest2) && ! marked_p (rest2))
|
|
2700 {
|
1598
|
2701 #ifdef USE_KKCC
|
|
2702 kkcc_gc_stack_push_lisp_object (rest2);
|
|
2703 #else /* NOT USE_KKCC */
|
428
|
2704 mark_object (rest2);
|
1598
|
2705 #endif /* NOT USE_KKCC */
|
428
|
2706 did_mark = 1;
|
|
2707 }
|
|
2708 }
|
|
2709
|
|
2710 return did_mark;
|
|
2711 }
|
|
2712
|
|
2713 void
|
|
2714 prune_weak_lists (void)
|
|
2715 {
|
|
2716 Lisp_Object rest, prev = Qnil;
|
|
2717
|
|
2718 for (rest = Vall_weak_lists;
|
|
2719 !NILP (rest);
|
|
2720 rest = XWEAK_LIST (rest)->next_weak)
|
|
2721 {
|
|
2722 if (! (marked_p (rest)))
|
|
2723 {
|
|
2724 /* This weak list itself is garbage. Remove it from the list. */
|
|
2725 if (NILP (prev))
|
|
2726 Vall_weak_lists = XWEAK_LIST (rest)->next_weak;
|
|
2727 else
|
|
2728 XWEAK_LIST (prev)->next_weak =
|
|
2729 XWEAK_LIST (rest)->next_weak;
|
|
2730 }
|
|
2731 else
|
|
2732 {
|
|
2733 Lisp_Object rest2, prev2 = Qnil;
|
|
2734 Lisp_Object tortoise;
|
|
2735 int go_tortoise = 0;
|
|
2736
|
|
2737 for (rest2 = XWEAK_LIST (rest)->list, tortoise = rest2;
|
|
2738 /* We need to be trickier since we're inside of GC;
|
|
2739 use CONSP instead of !NILP in case of user-visible
|
|
2740 imperfect lists */
|
|
2741 CONSP (rest2);)
|
|
2742 {
|
|
2743 /* It suffices to check the cons for marking,
|
|
2744 regardless of the type of weak list:
|
|
2745
|
|
2746 -- if the cons is pointed to somewhere else,
|
|
2747 then it should stay around and will be marked.
|
|
2748 -- otherwise, if it should stay around, it will
|
|
2749 have been marked in finish_marking_weak_lists().
|
|
2750 -- otherwise, it's not marked and should disappear.
|
|
2751 */
|
|
2752 if (! marked_p (rest2))
|
|
2753 {
|
|
2754 /* bye bye :-( */
|
|
2755 if (NILP (prev2))
|
|
2756 XWEAK_LIST (rest)->list = XCDR (rest2);
|
|
2757 else
|
|
2758 XCDR (prev2) = XCDR (rest2);
|
|
2759 rest2 = XCDR (rest2);
|
|
2760 /* Ouch. Circularity checking is even trickier
|
|
2761 than I thought. When we cut out a link
|
|
2762 like this, we can't advance the turtle or
|
|
2763 it'll catch up to us. Imagine that we're
|
|
2764 standing on floor tiles and moving forward --
|
|
2765 what we just did here is as if the floor
|
|
2766 tile under us just disappeared and all the
|
|
2767 ones ahead of us slid one tile towards us.
|
|
2768 In other words, we didn't move at all;
|
|
2769 if the tortoise was one step behind us
|
|
2770 previously, it still is, and therefore
|
|
2771 it must not move. */
|
|
2772 }
|
|
2773 else
|
|
2774 {
|
|
2775 prev2 = rest2;
|
|
2776
|
|
2777 /* Implementing circularity checking is trickier here
|
|
2778 than in other places because we have to guarantee
|
|
2779 that we've processed all elements before exiting
|
|
2780 due to a circularity. (In most places, an error
|
|
2781 is issued upon encountering a circularity, so it
|
|
2782 doesn't really matter if all elements are processed.)
|
|
2783 The idea is that we process along with the hare
|
|
2784 rather than the tortoise. If at any point in
|
|
2785 our forward process we encounter the tortoise,
|
|
2786 we must have already visited the spot, so we exit.
|
|
2787 (If we process with the tortoise, we can fail to
|
|
2788 process cases where a cons points to itself, or
|
|
2789 where cons A points to cons B, which points to
|
|
2790 cons A.) */
|
|
2791
|
|
2792 rest2 = XCDR (rest2);
|
|
2793 if (go_tortoise)
|
|
2794 tortoise = XCDR (tortoise);
|
|
2795 go_tortoise = !go_tortoise;
|
|
2796 if (EQ (rest2, tortoise))
|
|
2797 break;
|
|
2798 }
|
|
2799 }
|
|
2800
|
|
2801 prev = rest;
|
|
2802 }
|
|
2803 }
|
|
2804 }
|
|
2805
|
|
2806 static enum weak_list_type
|
|
2807 decode_weak_list_type (Lisp_Object symbol)
|
|
2808 {
|
|
2809 CHECK_SYMBOL (symbol);
|
|
2810 if (EQ (symbol, Qsimple)) return WEAK_LIST_SIMPLE;
|
|
2811 if (EQ (symbol, Qassoc)) return WEAK_LIST_ASSOC;
|
|
2812 if (EQ (symbol, Qold_assoc)) return WEAK_LIST_ASSOC; /* EBOLA ALERT! */
|
|
2813 if (EQ (symbol, Qkey_assoc)) return WEAK_LIST_KEY_ASSOC;
|
|
2814 if (EQ (symbol, Qvalue_assoc)) return WEAK_LIST_VALUE_ASSOC;
|
442
|
2815 if (EQ (symbol, Qfull_assoc)) return WEAK_LIST_FULL_ASSOC;
|
428
|
2816
|
563
|
2817 invalid_constant ("Invalid weak list type", symbol);
|
1204
|
2818 RETURN_NOT_REACHED (WEAK_LIST_SIMPLE);
|
428
|
2819 }
|
|
2820
|
|
2821 static Lisp_Object
|
|
2822 encode_weak_list_type (enum weak_list_type type)
|
|
2823 {
|
|
2824 switch (type)
|
|
2825 {
|
|
2826 case WEAK_LIST_SIMPLE: return Qsimple;
|
|
2827 case WEAK_LIST_ASSOC: return Qassoc;
|
|
2828 case WEAK_LIST_KEY_ASSOC: return Qkey_assoc;
|
|
2829 case WEAK_LIST_VALUE_ASSOC: return Qvalue_assoc;
|
442
|
2830 case WEAK_LIST_FULL_ASSOC: return Qfull_assoc;
|
428
|
2831 default:
|
|
2832 abort ();
|
|
2833 }
|
|
2834
|
801
|
2835 return Qnil; /* not (usually) reached */
|
428
|
2836 }
|
|
2837
|
|
2838 DEFUN ("weak-list-p", Fweak_list_p, 1, 1, 0, /*
|
|
2839 Return non-nil if OBJECT is a weak list.
|
|
2840 */
|
|
2841 (object))
|
|
2842 {
|
|
2843 return WEAK_LISTP (object) ? Qt : Qnil;
|
|
2844 }
|
|
2845
|
|
2846 DEFUN ("make-weak-list", Fmake_weak_list, 0, 1, 0, /*
|
|
2847 Return a new weak list object of type TYPE.
|
|
2848 A weak list object is an object that contains a list. This list behaves
|
|
2849 like any other list except that its elements do not count towards
|
456
|
2850 garbage collection -- if the only pointer to an object is inside a weak
|
428
|
2851 list (other than pointers in similar objects such as weak hash tables),
|
|
2852 the object is garbage collected and automatically removed from the list.
|
|
2853 This is used internally, for example, to manage the list holding the
|
|
2854 children of an extent -- an extent that is unused but has a parent will
|
|
2855 still be reclaimed, and will automatically be removed from its parent's
|
|
2856 list of children.
|
|
2857
|
|
2858 Optional argument TYPE specifies the type of the weak list, and defaults
|
|
2859 to `simple'. Recognized types are
|
|
2860
|
|
2861 `simple' Objects in the list disappear if not pointed to.
|
|
2862 `assoc' Objects in the list disappear if they are conses
|
|
2863 and either the car or the cdr of the cons is not
|
|
2864 pointed to.
|
|
2865 `key-assoc' Objects in the list disappear if they are conses
|
|
2866 and the car is not pointed to.
|
|
2867 `value-assoc' Objects in the list disappear if they are conses
|
|
2868 and the cdr is not pointed to.
|
442
|
2869 `full-assoc' Objects in the list disappear if they are conses
|
|
2870 and neither the car nor the cdr is pointed to.
|
428
|
2871 */
|
|
2872 (type))
|
|
2873 {
|
|
2874 if (NILP (type))
|
|
2875 type = Qsimple;
|
|
2876
|
|
2877 return make_weak_list (decode_weak_list_type (type));
|
|
2878 }
|
|
2879
|
|
2880 DEFUN ("weak-list-type", Fweak_list_type, 1, 1, 0, /*
|
|
2881 Return the type of the given weak-list object.
|
|
2882 */
|
|
2883 (weak))
|
|
2884 {
|
|
2885 CHECK_WEAK_LIST (weak);
|
|
2886 return encode_weak_list_type (XWEAK_LIST (weak)->type);
|
|
2887 }
|
|
2888
|
|
2889 DEFUN ("weak-list-list", Fweak_list_list, 1, 1, 0, /*
|
|
2890 Return the list contained in a weak-list object.
|
|
2891 */
|
|
2892 (weak))
|
|
2893 {
|
|
2894 CHECK_WEAK_LIST (weak);
|
|
2895 return XWEAK_LIST_LIST (weak);
|
|
2896 }
|
|
2897
|
|
2898 DEFUN ("set-weak-list-list", Fset_weak_list_list, 2, 2, 0, /*
|
|
2899 Change the list contained in a weak-list object.
|
|
2900 */
|
|
2901 (weak, new_list))
|
|
2902 {
|
|
2903 CHECK_WEAK_LIST (weak);
|
|
2904 XWEAK_LIST_LIST (weak) = new_list;
|
|
2905 return new_list;
|
|
2906 }
|
|
2907
|
888
|
2908
|
858
|
2909 /************************************************************************/
|
|
2910 /* weak boxes */
|
|
2911 /************************************************************************/
|
|
2912
|
|
2913 static Lisp_Object Vall_weak_boxes; /* Gemarke es niemals ever!!! */
|
|
2914
|
|
2915 void
|
|
2916 prune_weak_boxes (void)
|
|
2917 {
|
|
2918 Lisp_Object rest, prev = Qnil;
|
888
|
2919 int removep = 0;
|
858
|
2920
|
|
2921 for (rest = Vall_weak_boxes;
|
|
2922 !NILP(rest);
|
|
2923 rest = XWEAK_BOX (rest)->next_weak_box)
|
|
2924 {
|
|
2925 if (! (marked_p (rest)))
|
888
|
2926 /* This weak box itself is garbage. */
|
|
2927 removep = 1;
|
|
2928
|
|
2929 if (! marked_p (XWEAK_BOX (rest)->value))
|
|
2930 {
|
|
2931 XSET_WEAK_BOX (rest, Qnil);
|
|
2932 removep = 1;
|
|
2933 }
|
|
2934
|
|
2935 if (removep)
|
|
2936 {
|
|
2937 /* Remove weak box from list. */
|
|
2938 if (NILP (prev))
|
|
2939 Vall_weak_boxes = XWEAK_BOX (rest)->next_weak_box;
|
|
2940 else
|
|
2941 XWEAK_BOX (prev)->next_weak_box = XWEAK_BOX (rest)->next_weak_box;
|
|
2942 removep = 0;
|
|
2943 }
|
|
2944 else
|
|
2945 prev = rest;
|
858
|
2946 }
|
|
2947 }
|
|
2948
|
|
2949 static Lisp_Object
|
888
|
2950 mark_weak_box (Lisp_Object obj)
|
858
|
2951 {
|
|
2952 return Qnil;
|
|
2953 }
|
|
2954
|
|
2955 static void
|
|
2956 print_weak_box (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
2957 {
|
|
2958 if (print_readably)
|
|
2959 printing_unreadable_object ("#<weak_box>");
|
|
2960 write_fmt_string (printcharfun, "#<weak_box>");
|
|
2961 }
|
|
2962
|
|
2963 static int
|
|
2964 weak_box_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
2965 {
|
888
|
2966 struct weak_box *wb1 = XWEAK_BOX (obj1);
|
|
2967 struct weak_box *wb2 = XWEAK_BOX (obj2);
|
858
|
2968
|
888
|
2969 return (internal_equal (wb1->value, wb2->value, depth + 1));
|
858
|
2970 }
|
|
2971
|
|
2972 static Hashcode
|
|
2973 weak_box_hash (Lisp_Object obj, int depth)
|
|
2974 {
|
888
|
2975 struct weak_box *wb = XWEAK_BOX (obj);
|
858
|
2976
|
888
|
2977 return internal_hash (wb->value, depth + 1);
|
858
|
2978 }
|
|
2979
|
|
2980 Lisp_Object
|
|
2981 make_weak_box (Lisp_Object value)
|
|
2982 {
|
|
2983 Lisp_Object result;
|
|
2984
|
|
2985 struct weak_box *wb =
|
|
2986 alloc_lcrecord_type (struct weak_box, &lrecord_weak_box);
|
|
2987
|
|
2988 wb->value = value;
|
|
2989 result = wrap_weak_box (wb);
|
|
2990 wb->next_weak_box = Vall_weak_boxes;
|
|
2991 Vall_weak_boxes = result;
|
|
2992 return result;
|
|
2993 }
|
|
2994
|
1204
|
2995 static const struct memory_description weak_box_description[] = {
|
858
|
2996 { XD_LO_LINK, offsetof (struct weak_box, value) },
|
888
|
2997 { XD_END}
|
858
|
2998 };
|
|
2999
|
934
|
3000 DEFINE_LRECORD_IMPLEMENTATION ("weak_box", weak_box,
|
|
3001 0, /*dumpable-flag*/
|
|
3002 mark_weak_box, print_weak_box,
|
|
3003 0, weak_box_equal, weak_box_hash,
|
|
3004 weak_box_description,
|
|
3005 struct weak_box);
|
858
|
3006
|
|
3007 DEFUN ("make-weak-box", Fmake_weak_box, 1, 1, 0, /*
|
|
3008 Return a new weak box from value CONTENTS.
|
|
3009 The weak box is a reference to CONTENTS which may be extracted with
|
|
3010 `weak-box-ref'. However, the weak box does not contribute to the
|
|
3011 reachability of CONTENTS. When CONTENTS is garbage-collected,
|
|
3012 `weak-box-ref' will return NIL.
|
|
3013 */
|
|
3014 (value))
|
|
3015 {
|
|
3016 return make_weak_box(value);
|
|
3017 }
|
|
3018
|
|
3019 DEFUN ("weak-box-ref", Fweak_box_ref, 1, 1, 0, /*
|
|
3020 Return the contents of weak box WEAK-BOX.
|
|
3021 If the contents have been GCed, return NIL.
|
|
3022 */
|
888
|
3023 (wb))
|
858
|
3024 {
|
888
|
3025 return XWEAK_BOX (wb)->value;
|
858
|
3026 }
|
|
3027
|
|
3028 DEFUN ("weak-box-p", Fweak_boxp, 1, 1, 0, /*
|
|
3029 Return non-nil if OBJECT is a weak box.
|
|
3030 */
|
|
3031 (object))
|
|
3032 {
|
|
3033 return WEAK_BOXP (object) ? Qt : Qnil;
|
|
3034 }
|
|
3035
|
888
|
3036 /************************************************************************/
|
|
3037 /* ephemerons */
|
|
3038 /************************************************************************/
|
|
3039
|
993
|
3040 /* The concept of ephemerons is due to:
|
|
3041 * Barry Hayes: Ephemerons: A New Finalization Mechanism. OOPSLA 1997: 176-183
|
|
3042 * The original idea is due to George Bosworth of Digitalk, Inc.
|
|
3043 *
|
|
3044 * For a discussion of finalization and weakness that also reviews
|
|
3045 * ephemerons, refer to:
|
|
3046 * Simon Peyton Jones, Simon Marlow, Conal Elliot:
|
|
3047 * Stretching the storage manager
|
|
3048 * Implementation of Functional Languages, 1999
|
|
3049 */
|
|
3050
|
888
|
3051 static Lisp_Object Vall_ephemerons; /* Gemarke es niemals ever!!! */
|
1590
|
3052 static Lisp_Object Vnew_all_ephemerons;
|
888
|
3053 static Lisp_Object Vfinalize_list;
|
|
3054
|
1590
|
3055 void
|
|
3056 init_marking_ephemerons(void)
|
|
3057 {
|
|
3058 Vnew_all_ephemerons = Qnil;
|
|
3059 }
|
|
3060
|
|
3061 /* Move all live ephemerons with live keys over to
|
|
3062 * Vnew_all_ephemerons, marking the values and finalizers along the
|
|
3063 * way. */
|
|
3064
|
|
3065 int
|
|
3066 continue_marking_ephemerons(void)
|
|
3067 {
|
|
3068 Lisp_Object rest = Vall_ephemerons, next, prev = Qnil;
|
|
3069 int did_mark = 0;
|
|
3070
|
|
3071 while (!NILP (rest))
|
|
3072 {
|
|
3073 next = XEPHEMERON_NEXT (rest);
|
|
3074
|
|
3075 if (marked_p (rest))
|
|
3076 {
|
|
3077 MARK_CONS (XCONS (XEPHEMERON (rest)->cons_chain));
|
|
3078 if (marked_p (XEPHEMERON (rest)->key))
|
|
3079 {
|
1598
|
3080 #ifdef USE_KKCC
|
|
3081 kkcc_gc_stack_push_lisp_object
|
|
3082 (XCAR (XEPHEMERON (rest)->cons_chain));
|
|
3083 #else /* NOT USE_KKCC */
|
1590
|
3084 mark_object (XCAR (XEPHEMERON (rest)->cons_chain));
|
1598
|
3085 #endif /* NOT USE_KKCC */
|
1590
|
3086 did_mark = 1;
|
|
3087 XSET_EPHEMERON_NEXT (rest, Vnew_all_ephemerons);
|
|
3088 Vnew_all_ephemerons = rest;
|
|
3089 if (NILP (prev))
|
|
3090 Vall_ephemerons = next;
|
|
3091 else
|
|
3092 XSET_EPHEMERON_NEXT (prev, next);
|
|
3093 }
|
|
3094 else
|
|
3095 prev = rest;
|
|
3096 }
|
|
3097 else
|
|
3098 prev = rest;
|
|
3099
|
|
3100 rest = next;
|
|
3101 }
|
|
3102
|
|
3103 return did_mark;
|
|
3104 }
|
|
3105
|
|
3106 /* At this point, everything that's in Vall_ephemerons is dead.
|
|
3107 * Well, almost: we still need to run the finalizers, so we need to
|
|
3108 * resurrect them.
|
|
3109 */
|
|
3110
|
888
|
3111 int
|
|
3112 finish_marking_ephemerons(void)
|
|
3113 {
|
1590
|
3114 Lisp_Object rest = Vall_ephemerons, next, prev = Qnil;
|
888
|
3115 int did_mark = 0;
|
|
3116
|
|
3117 while (! NILP (rest))
|
|
3118 {
|
|
3119 next = XEPHEMERON_NEXT (rest);
|
|
3120
|
|
3121 if (marked_p (rest))
|
1590
|
3122 /* The ephemeron itself is live, but its key is garbage */
|
888
|
3123 {
|
1590
|
3124 /* tombstone */
|
|
3125 XSET_EPHEMERON_VALUE (rest, Qnil);
|
|
3126
|
|
3127 if (! NILP (XEPHEMERON_FINALIZER (rest)))
|
888
|
3128 {
|
1590
|
3129 MARK_CONS (XCONS (XEPHEMERON (rest)->cons_chain));
|
1598
|
3130 #ifdef USE_KKCC
|
|
3131 kkcc_gc_stack_push_lisp_object
|
|
3132 (XCAR (XEPHEMERON (rest)->cons_chain));
|
|
3133 #else /* NOT USE_KKCC */
|
1590
|
3134 mark_object (XCAR (XEPHEMERON (rest)->cons_chain));
|
1598
|
3135 #endif /* NOT USE_KKCC */
|
1590
|
3136
|
|
3137 /* Register the finalizer */
|
|
3138 XSET_EPHEMERON_NEXT (rest, Vfinalize_list);
|
|
3139 Vfinalize_list = XEPHEMERON (rest)->cons_chain;
|
|
3140 did_mark = 1;
|
888
|
3141 }
|
|
3142
|
|
3143 /* Remove it from the list. */
|
|
3144 if (NILP (prev))
|
|
3145 Vall_ephemerons = next;
|
|
3146 else
|
|
3147 XSET_EPHEMERON_NEXT (prev, next);
|
|
3148 }
|
|
3149 else
|
|
3150 prev = rest;
|
|
3151
|
|
3152 rest = next;
|
|
3153 }
|
1590
|
3154
|
|
3155 return did_mark;
|
|
3156 }
|
|
3157
|
|
3158 void
|
|
3159 prune_ephemerons(void)
|
|
3160 {
|
|
3161 Vall_ephemerons = Vnew_all_ephemerons;
|
888
|
3162 }
|
|
3163
|
|
3164 Lisp_Object
|
|
3165 zap_finalize_list(void)
|
|
3166 {
|
|
3167 Lisp_Object finalizers = Vfinalize_list;
|
|
3168
|
|
3169 Vfinalize_list = Qnil;
|
|
3170
|
|
3171 return finalizers;
|
|
3172 }
|
|
3173
|
|
3174 static Lisp_Object
|
|
3175 mark_ephemeron (Lisp_Object obj)
|
|
3176 {
|
|
3177 return Qnil;
|
|
3178 }
|
|
3179
|
|
3180 static void
|
|
3181 print_ephemeron (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
3182 {
|
|
3183 if (print_readably)
|
|
3184 printing_unreadable_object ("#<ephemeron>");
|
|
3185 write_fmt_string (printcharfun, "#<ephemeron>");
|
|
3186 }
|
|
3187
|
|
3188 static int
|
|
3189 ephemeron_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
3190 {
|
|
3191 return
|
|
3192 internal_equal (XEPHEMERON_REF (obj1), XEPHEMERON_REF(obj2), depth + 1);
|
|
3193 }
|
|
3194
|
|
3195 static Hashcode
|
|
3196 ephemeron_hash(Lisp_Object obj, int depth)
|
|
3197 {
|
|
3198 return internal_hash (XEPHEMERON_REF (obj), depth + 1);
|
|
3199 }
|
|
3200
|
|
3201 Lisp_Object
|
|
3202 make_ephemeron(Lisp_Object key, Lisp_Object value, Lisp_Object finalizer)
|
|
3203 {
|
|
3204 Lisp_Object result, temp = Qnil;
|
|
3205 struct gcpro gcpro1, gcpro2;
|
|
3206
|
|
3207 struct ephemeron *eph =
|
|
3208 alloc_lcrecord_type (struct ephemeron, &lrecord_ephemeron);
|
|
3209
|
|
3210 eph->key = Qnil;
|
|
3211 eph->cons_chain = Qnil;
|
|
3212 eph->value = Qnil;
|
|
3213
|
|
3214 result = wrap_ephemeron(eph);
|
|
3215 GCPRO2 (result, temp);
|
|
3216
|
|
3217 eph->key = key;
|
|
3218 temp = Fcons(value, finalizer);
|
|
3219 eph->cons_chain = Fcons(temp, Vall_ephemerons);
|
|
3220 eph->value = value;
|
|
3221
|
|
3222 Vall_ephemerons = result;
|
|
3223
|
|
3224 UNGCPRO;
|
|
3225 return result;
|
|
3226 }
|
|
3227
|
1598
|
3228 /* Ephemerons are special cases in the KKCC mark algorithm, so nothing
|
|
3229 is marked here. */
|
1204
|
3230 static const struct memory_description ephemeron_description[] = {
|
|
3231 { XD_LISP_OBJECT, offsetof(struct ephemeron, key),
|
|
3232 0, 0, XD_FLAG_NO_KKCC },
|
|
3233 { XD_LISP_OBJECT, offsetof(struct ephemeron, cons_chain),
|
|
3234 0, 0, XD_FLAG_NO_KKCC },
|
|
3235 { XD_LISP_OBJECT, offsetof(struct ephemeron, value),
|
|
3236 0, 0, XD_FLAG_NO_KKCC },
|
888
|
3237 { XD_END }
|
|
3238 };
|
|
3239
|
934
|
3240 DEFINE_LRECORD_IMPLEMENTATION ("ephemeron", ephemeron,
|
|
3241 0, /*dumpable-flag*/
|
|
3242 mark_ephemeron, print_ephemeron,
|
|
3243 0, ephemeron_equal, ephemeron_hash,
|
|
3244 ephemeron_description,
|
|
3245 struct ephemeron);
|
888
|
3246
|
|
3247 DEFUN ("make-ephemeron", Fmake_ephemeron, 2, 3, 0, /*
|
1590
|
3248 Return a new ephemeron with key KEY, value VALUE, and finalizer FINALIZER.
|
|
3249 The ephemeron is a reference to VALUE which may be extracted with
|
|
3250 `ephemeron-ref'. VALUE is only reachable through the ephemeron as
|
888
|
3251 long as KEY is reachable; the ephemeron does not contribute to the
|
|
3252 reachability of KEY. When KEY becomes unreachable while the ephemeron
|
1590
|
3253 itself is still reachable, VALUE is queued for finalization: FINALIZER
|
|
3254 will possibly be called on VALUE some time in the future. Moreover,
|
888
|
3255 future calls to `ephemeron-ref' will return NIL.
|
|
3256 */
|
|
3257 (key, value, finalizer))
|
|
3258 {
|
|
3259 return make_ephemeron(key, value, finalizer);
|
|
3260 }
|
|
3261
|
|
3262 DEFUN ("ephemeron-ref", Fephemeron_ref, 1, 1, 0, /*
|
|
3263 Return the contents of ephemeron EPHEMERON.
|
|
3264 If the contents have been GCed, return NIL.
|
|
3265 */
|
|
3266 (eph))
|
|
3267 {
|
|
3268 return XEPHEMERON_REF (eph);
|
|
3269 }
|
|
3270
|
|
3271 DEFUN ("ephemeron-p", Fephemeronp, 1, 1, 0, /*
|
|
3272 Return non-nil if OBJECT is an ephemeron.
|
|
3273 */
|
|
3274 (object))
|
|
3275 {
|
|
3276 return EPHEMERONP (object) ? Qt : Qnil;
|
|
3277 }
|
428
|
3278
|
|
3279 /************************************************************************/
|
|
3280 /* initialization */
|
|
3281 /************************************************************************/
|
|
3282
|
|
3283 static SIGTYPE
|
|
3284 arith_error (int signo)
|
|
3285 {
|
|
3286 EMACS_REESTABLISH_SIGNAL (signo, arith_error);
|
|
3287 EMACS_UNBLOCK_SIGNAL (signo);
|
563
|
3288 signal_error (Qarith_error, 0, Qunbound);
|
428
|
3289 }
|
|
3290
|
|
3291 void
|
|
3292 init_data_very_early (void)
|
|
3293 {
|
|
3294 /* Don't do this if just dumping out.
|
|
3295 We don't want to call `signal' in this case
|
|
3296 so that we don't have trouble with dumping
|
|
3297 signal-delivering routines in an inconsistent state. */
|
|
3298 if (!initialized)
|
|
3299 return;
|
613
|
3300 EMACS_SIGNAL (SIGFPE, arith_error);
|
428
|
3301 #ifdef uts
|
613
|
3302 EMACS_SIGNAL (SIGEMT, arith_error);
|
428
|
3303 #endif /* uts */
|
|
3304 }
|
|
3305
|
|
3306 void
|
|
3307 init_errors_once_early (void)
|
|
3308 {
|
442
|
3309 DEFSYMBOL (Qerror_conditions);
|
|
3310 DEFSYMBOL (Qerror_message);
|
428
|
3311
|
|
3312 /* We declare the errors here because some other deferrors depend
|
|
3313 on some of the errors below. */
|
|
3314
|
|
3315 /* ERROR is used as a signaler for random errors for which nothing
|
|
3316 else is right */
|
|
3317
|
442
|
3318 DEFERROR (Qerror, "error", Qnil);
|
|
3319 DEFERROR_STANDARD (Qquit, Qnil);
|
428
|
3320
|
563
|
3321 DEFERROR_STANDARD (Qinvalid_argument, Qerror);
|
|
3322
|
|
3323 DEFERROR_STANDARD (Qsyntax_error, Qinvalid_argument);
|
442
|
3324 DEFERROR_STANDARD (Qinvalid_read_syntax, Qsyntax_error);
|
563
|
3325 DEFERROR_STANDARD (Qstructure_formation_error, Qsyntax_error);
|
|
3326 DEFERROR_STANDARD (Qlist_formation_error, Qstructure_formation_error);
|
442
|
3327 DEFERROR_STANDARD (Qmalformed_list, Qlist_formation_error);
|
|
3328 DEFERROR_STANDARD (Qmalformed_property_list, Qmalformed_list);
|
|
3329 DEFERROR_STANDARD (Qcircular_list, Qlist_formation_error);
|
|
3330 DEFERROR_STANDARD (Qcircular_property_list, Qcircular_list);
|
428
|
3331
|
442
|
3332 DEFERROR_STANDARD (Qwrong_type_argument, Qinvalid_argument);
|
|
3333 DEFERROR_STANDARD (Qargs_out_of_range, Qinvalid_argument);
|
|
3334 DEFERROR_STANDARD (Qwrong_number_of_arguments, Qinvalid_argument);
|
|
3335 DEFERROR_STANDARD (Qinvalid_function, Qinvalid_argument);
|
563
|
3336 DEFERROR_STANDARD (Qinvalid_constant, Qinvalid_argument);
|
442
|
3337 DEFERROR (Qno_catch, "No catch for tag", Qinvalid_argument);
|
|
3338
|
563
|
3339 DEFERROR_STANDARD (Qinvalid_state, Qerror);
|
442
|
3340 DEFERROR (Qvoid_function, "Symbol's function definition is void",
|
|
3341 Qinvalid_state);
|
|
3342 DEFERROR (Qcyclic_function_indirection,
|
|
3343 "Symbol's chain of function indirections contains a loop",
|
|
3344 Qinvalid_state);
|
|
3345 DEFERROR (Qvoid_variable, "Symbol's value as variable is void",
|
|
3346 Qinvalid_state);
|
|
3347 DEFERROR (Qcyclic_variable_indirection,
|
|
3348 "Symbol's chain of variable indirections contains a loop",
|
|
3349 Qinvalid_state);
|
563
|
3350 DEFERROR_STANDARD (Qstack_overflow, Qinvalid_state);
|
|
3351 DEFERROR_STANDARD (Qinternal_error, Qinvalid_state);
|
|
3352 DEFERROR_STANDARD (Qout_of_memory, Qinvalid_state);
|
428
|
3353
|
563
|
3354 DEFERROR_STANDARD (Qinvalid_operation, Qerror);
|
|
3355 DEFERROR_STANDARD (Qinvalid_change, Qinvalid_operation);
|
442
|
3356 DEFERROR (Qsetting_constant, "Attempt to set a constant symbol",
|
|
3357 Qinvalid_change);
|
563
|
3358 DEFERROR_STANDARD (Qprinting_unreadable_object, Qinvalid_operation);
|
|
3359 DEFERROR (Qunimplemented, "Feature not yet implemented", Qinvalid_operation);
|
442
|
3360
|
563
|
3361 DEFERROR_STANDARD (Qediting_error, Qinvalid_operation);
|
442
|
3362 DEFERROR_STANDARD (Qbeginning_of_buffer, Qediting_error);
|
|
3363 DEFERROR_STANDARD (Qend_of_buffer, Qediting_error);
|
|
3364 DEFERROR (Qbuffer_read_only, "Buffer is read-only", Qediting_error);
|
|
3365
|
|
3366 DEFERROR (Qio_error, "IO Error", Qinvalid_operation);
|
563
|
3367 DEFERROR_STANDARD (Qfile_error, Qio_error);
|
|
3368 DEFERROR (Qend_of_file, "End of file or stream", Qfile_error);
|
|
3369 DEFERROR_STANDARD (Qconversion_error, Qio_error);
|
580
|
3370 DEFERROR_STANDARD (Qtext_conversion_error, Qconversion_error);
|
442
|
3371
|
|
3372 DEFERROR (Qarith_error, "Arithmetic error", Qinvalid_operation);
|
|
3373 DEFERROR (Qrange_error, "Arithmetic range error", Qarith_error);
|
|
3374 DEFERROR (Qdomain_error, "Arithmetic domain error", Qarith_error);
|
|
3375 DEFERROR (Qsingularity_error, "Arithmetic singularity error", Qdomain_error);
|
|
3376 DEFERROR (Qoverflow_error, "Arithmetic overflow error", Qdomain_error);
|
|
3377 DEFERROR (Qunderflow_error, "Arithmetic underflow error", Qdomain_error);
|
428
|
3378 }
|
|
3379
|
|
3380 void
|
|
3381 syms_of_data (void)
|
|
3382 {
|
442
|
3383 INIT_LRECORD_IMPLEMENTATION (weak_list);
|
888
|
3384 INIT_LRECORD_IMPLEMENTATION (ephemeron);
|
858
|
3385 INIT_LRECORD_IMPLEMENTATION (weak_box);
|
442
|
3386
|
|
3387 DEFSYMBOL (Qquote);
|
|
3388 DEFSYMBOL (Qlambda);
|
|
3389 DEFSYMBOL (Qlistp);
|
|
3390 DEFSYMBOL (Qtrue_list_p);
|
|
3391 DEFSYMBOL (Qconsp);
|
|
3392 DEFSYMBOL (Qsubrp);
|
|
3393 DEFSYMBOL (Qsymbolp);
|
|
3394 DEFSYMBOL (Qintegerp);
|
|
3395 DEFSYMBOL (Qcharacterp);
|
|
3396 DEFSYMBOL (Qnatnump);
|
1983
|
3397 DEFSYMBOL (Qnonnegativep);
|
442
|
3398 DEFSYMBOL (Qstringp);
|
|
3399 DEFSYMBOL (Qarrayp);
|
|
3400 DEFSYMBOL (Qsequencep);
|
|
3401 DEFSYMBOL (Qbufferp);
|
|
3402 DEFSYMBOL (Qbitp);
|
|
3403 DEFSYMBOL_MULTIWORD_PREDICATE (Qbit_vectorp);
|
|
3404 DEFSYMBOL (Qvectorp);
|
|
3405 DEFSYMBOL (Qchar_or_string_p);
|
|
3406 DEFSYMBOL (Qmarkerp);
|
|
3407 DEFSYMBOL (Qinteger_or_marker_p);
|
|
3408 DEFSYMBOL (Qinteger_or_char_p);
|
|
3409 DEFSYMBOL (Qinteger_char_or_marker_p);
|
|
3410 DEFSYMBOL (Qnumberp);
|
|
3411 DEFSYMBOL (Qnumber_char_or_marker_p);
|
|
3412 DEFSYMBOL (Qcdr);
|
563
|
3413 DEFSYMBOL (Qerror_lacks_explanatory_string);
|
442
|
3414 DEFSYMBOL_MULTIWORD_PREDICATE (Qweak_listp);
|
|
3415 DEFSYMBOL (Qfloatp);
|
428
|
3416
|
|
3417 DEFSUBR (Fwrong_type_argument);
|
|
3418
|
1983
|
3419 #ifdef HAVE_RATIO
|
|
3420 DEFSUBR (Fdiv);
|
|
3421 #endif
|
428
|
3422 DEFSUBR (Feq);
|
|
3423 DEFSUBR (Fold_eq);
|
|
3424 DEFSUBR (Fnull);
|
|
3425 Ffset (intern ("not"), intern ("null"));
|
|
3426 DEFSUBR (Flistp);
|
|
3427 DEFSUBR (Fnlistp);
|
|
3428 DEFSUBR (Ftrue_list_p);
|
|
3429 DEFSUBR (Fconsp);
|
|
3430 DEFSUBR (Fatom);
|
|
3431 DEFSUBR (Fchar_or_string_p);
|
|
3432 DEFSUBR (Fcharacterp);
|
|
3433 DEFSUBR (Fchar_int_p);
|
|
3434 DEFSUBR (Fchar_to_int);
|
|
3435 DEFSUBR (Fint_to_char);
|
|
3436 DEFSUBR (Fchar_or_char_int_p);
|
1983
|
3437 #ifdef HAVE_BIGNUM
|
|
3438 DEFSUBR (Ffixnump);
|
|
3439 #else
|
428
|
3440 DEFSUBR (Fintegerp);
|
1983
|
3441 #endif
|
428
|
3442 DEFSUBR (Finteger_or_marker_p);
|
|
3443 DEFSUBR (Finteger_or_char_p);
|
|
3444 DEFSUBR (Finteger_char_or_marker_p);
|
|
3445 DEFSUBR (Fnumberp);
|
|
3446 DEFSUBR (Fnumber_or_marker_p);
|
|
3447 DEFSUBR (Fnumber_char_or_marker_p);
|
|
3448 DEFSUBR (Ffloatp);
|
|
3449 DEFSUBR (Fnatnump);
|
1983
|
3450 DEFSUBR (Fnonnegativep);
|
428
|
3451 DEFSUBR (Fsymbolp);
|
|
3452 DEFSUBR (Fkeywordp);
|
|
3453 DEFSUBR (Fstringp);
|
|
3454 DEFSUBR (Fvectorp);
|
|
3455 DEFSUBR (Fbitp);
|
|
3456 DEFSUBR (Fbit_vector_p);
|
|
3457 DEFSUBR (Farrayp);
|
|
3458 DEFSUBR (Fsequencep);
|
|
3459 DEFSUBR (Fmarkerp);
|
|
3460 DEFSUBR (Fsubrp);
|
|
3461 DEFSUBR (Fsubr_min_args);
|
|
3462 DEFSUBR (Fsubr_max_args);
|
|
3463 DEFSUBR (Fsubr_interactive);
|
|
3464 DEFSUBR (Ftype_of);
|
|
3465 DEFSUBR (Fcar);
|
|
3466 DEFSUBR (Fcdr);
|
|
3467 DEFSUBR (Fcar_safe);
|
|
3468 DEFSUBR (Fcdr_safe);
|
|
3469 DEFSUBR (Fsetcar);
|
|
3470 DEFSUBR (Fsetcdr);
|
|
3471 DEFSUBR (Findirect_function);
|
|
3472 DEFSUBR (Faref);
|
|
3473 DEFSUBR (Faset);
|
|
3474
|
|
3475 DEFSUBR (Fnumber_to_string);
|
|
3476 DEFSUBR (Fstring_to_number);
|
|
3477 DEFSUBR (Feqlsign);
|
|
3478 DEFSUBR (Flss);
|
|
3479 DEFSUBR (Fgtr);
|
|
3480 DEFSUBR (Fleq);
|
|
3481 DEFSUBR (Fgeq);
|
|
3482 DEFSUBR (Fneq);
|
|
3483 DEFSUBR (Fzerop);
|
|
3484 DEFSUBR (Fplus);
|
|
3485 DEFSUBR (Fminus);
|
|
3486 DEFSUBR (Ftimes);
|
|
3487 DEFSUBR (Fquo);
|
|
3488 DEFSUBR (Frem);
|
|
3489 DEFSUBR (Fmod);
|
|
3490 DEFSUBR (Fmax);
|
|
3491 DEFSUBR (Fmin);
|
|
3492 DEFSUBR (Flogand);
|
|
3493 DEFSUBR (Flogior);
|
|
3494 DEFSUBR (Flogxor);
|
|
3495 DEFSUBR (Flsh);
|
|
3496 DEFSUBR (Fash);
|
|
3497 DEFSUBR (Fadd1);
|
|
3498 DEFSUBR (Fsub1);
|
|
3499 DEFSUBR (Flognot);
|
|
3500
|
|
3501 DEFSUBR (Fweak_list_p);
|
|
3502 DEFSUBR (Fmake_weak_list);
|
|
3503 DEFSUBR (Fweak_list_type);
|
|
3504 DEFSUBR (Fweak_list_list);
|
|
3505 DEFSUBR (Fset_weak_list_list);
|
858
|
3506
|
888
|
3507 DEFSUBR (Fmake_ephemeron);
|
|
3508 DEFSUBR (Fephemeron_ref);
|
|
3509 DEFSUBR (Fephemeronp);
|
858
|
3510 DEFSUBR (Fmake_weak_box);
|
|
3511 DEFSUBR (Fweak_box_ref);
|
|
3512 DEFSUBR (Fweak_boxp);
|
428
|
3513 }
|
|
3514
|
|
3515 void
|
|
3516 vars_of_data (void)
|
|
3517 {
|
|
3518 /* This must not be staticpro'd */
|
|
3519 Vall_weak_lists = Qnil;
|
452
|
3520 dump_add_weak_object_chain (&Vall_weak_lists);
|
428
|
3521
|
888
|
3522 Vall_ephemerons = Qnil;
|
|
3523 dump_add_weak_object_chain (&Vall_ephemerons);
|
|
3524
|
|
3525 Vfinalize_list = Qnil;
|
|
3526 staticpro (&Vfinalize_list);
|
|
3527
|
858
|
3528 Vall_weak_boxes = Qnil;
|
|
3529 dump_add_weak_object_chain (&Vall_weak_boxes);
|
|
3530
|
428
|
3531 #ifdef DEBUG_XEMACS
|
|
3532 DEFVAR_BOOL ("debug-issue-ebola-notices", &debug_issue_ebola_notices /*
|
|
3533 If non-zero, note when your code may be suffering from char-int confoundance.
|
|
3534 That is to say, if XEmacs encounters a usage of `eq', `memq', `equal',
|
|
3535 etc. where an int and a char with the same value are being compared,
|
|
3536 it will issue a notice on stderr to this effect, along with a backtrace.
|
|
3537 In such situations, the result would be different in XEmacs 19 versus
|
|
3538 XEmacs 20, and you probably don't want this.
|
|
3539
|
|
3540 Note that in order to see these notices, you have to byte compile your
|
|
3541 code under XEmacs 20 -- any code byte-compiled under XEmacs 19 will
|
|
3542 have its chars and ints all confounded in the byte code, making it
|
|
3543 impossible to accurately determine Ebola infection.
|
|
3544 */ );
|
|
3545
|
|
3546 debug_issue_ebola_notices = 0;
|
|
3547
|
|
3548 DEFVAR_INT ("debug-ebola-backtrace-length",
|
|
3549 &debug_ebola_backtrace_length /*
|
|
3550 Length (in stack frames) of short backtrace printed out in Ebola notices.
|
|
3551 See `debug-issue-ebola-notices'.
|
|
3552 */ );
|
|
3553 debug_ebola_backtrace_length = 32;
|
|
3554
|
|
3555 #endif /* DEBUG_XEMACS */
|
|
3556 }
|