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 /**********************************************************************/
|
|
839 typedef struct
|
|
840 {
|
|
841 int int_p;
|
|
842 union
|
|
843 {
|
|
844 EMACS_INT ival;
|
|
845 double dval;
|
|
846 } c;
|
|
847 } int_or_double;
|
|
848
|
|
849 static void
|
|
850 number_char_or_marker_to_int_or_double (Lisp_Object obj, int_or_double *p)
|
|
851 {
|
|
852 retry:
|
|
853 p->int_p = 1;
|
|
854 if (INTP (obj)) p->c.ival = XINT (obj);
|
|
855 else if (CHARP (obj)) p->c.ival = XCHAR (obj);
|
|
856 else if (MARKERP (obj)) p->c.ival = marker_position (obj);
|
|
857 else if (FLOATP (obj)) p->c.dval = XFLOAT_DATA (obj), p->int_p = 0;
|
|
858 else
|
|
859 {
|
|
860 obj = wrong_type_argument (Qnumber_char_or_marker_p, obj);
|
|
861 goto retry;
|
|
862 }
|
|
863 }
|
|
864
|
|
865 static double
|
|
866 number_char_or_marker_to_double (Lisp_Object obj)
|
|
867 {
|
|
868 retry:
|
|
869 if (INTP (obj)) return (double) XINT (obj);
|
|
870 else if (CHARP (obj)) return (double) XCHAR (obj);
|
|
871 else if (MARKERP (obj)) return (double) marker_position (obj);
|
|
872 else if (FLOATP (obj)) return XFLOAT_DATA (obj);
|
|
873 else
|
|
874 {
|
|
875 obj = wrong_type_argument (Qnumber_char_or_marker_p, obj);
|
|
876 goto retry;
|
|
877 }
|
|
878 }
|
|
879
|
|
880 static EMACS_INT
|
|
881 integer_char_or_marker_to_int (Lisp_Object obj)
|
|
882 {
|
|
883 retry:
|
|
884 if (INTP (obj)) return XINT (obj);
|
|
885 else if (CHARP (obj)) return XCHAR (obj);
|
|
886 else if (MARKERP (obj)) return marker_position (obj);
|
|
887 else
|
|
888 {
|
|
889 obj = wrong_type_argument (Qinteger_char_or_marker_p, obj);
|
|
890 goto retry;
|
|
891 }
|
|
892 }
|
|
893
|
1983
|
894 #ifdef WITH_NUMBER_TYPES
|
|
895
|
|
896 #ifdef HAVE_BIGNUM
|
|
897 #define BIGNUM_CASE(op) \
|
|
898 case BIGNUM_T: \
|
|
899 if (!bignum_##op (XBIGNUM_DATA (obj1), XBIGNUM_DATA (obj2))) \
|
|
900 return Qnil; \
|
|
901 break;
|
|
902 #else
|
|
903 #define BIGNUM_CASE(op)
|
|
904 #endif /* HAVE_BIGNUM */
|
|
905
|
|
906 #ifdef HAVE_RATIO
|
|
907 #define RATIO_CASE(op) \
|
|
908 case RATIO_T: \
|
|
909 if (!ratio_##op (XRATIO_DATA (obj1), XRATIO_DATA (obj2))) \
|
|
910 return Qnil; \
|
|
911 break;
|
|
912 #else
|
|
913 #define RATIO_CASE(op)
|
|
914 #endif /* HAVE_RATIO */
|
|
915
|
|
916 #ifdef HAVE_BIGFLOAT
|
|
917 #define BIGFLOAT_CASE(op) \
|
|
918 case BIGFLOAT_T: \
|
|
919 if (!bigfloat_##op (XBIGFLOAT_DATA (obj1), XBIGFLOAT_DATA (obj2))) \
|
|
920 return Qnil; \
|
|
921 break;
|
|
922 #else
|
|
923 #define BIGFLOAT_CASE(op)
|
|
924 #endif /* HAVE_BIGFLOAT */
|
|
925
|
|
926 #define ARITHCOMPARE_MANY(c_op,op) \
|
|
927 { \
|
|
928 REGISTER int i; \
|
|
929 Lisp_Object obj1, obj2; \
|
|
930 \
|
|
931 for (i = 1; i < nargs; i++) \
|
|
932 { \
|
|
933 obj1 = args[i - 1]; \
|
|
934 obj2 = args[i]; \
|
|
935 switch (promote_args (&obj1, &obj2)) \
|
|
936 { \
|
|
937 case FIXNUM_T: \
|
|
938 if (!(XREALINT (obj1) c_op XREALINT (obj2))) \
|
|
939 return Qnil; \
|
|
940 break; \
|
|
941 BIGNUM_CASE (op) \
|
|
942 RATIO_CASE (op) \
|
|
943 case FLOAT_T: \
|
|
944 if (!(XFLOAT_DATA (obj1) c_op XFLOAT_DATA (obj2))) \
|
|
945 return Qnil; \
|
|
946 break; \
|
|
947 BIGFLOAT_CASE (op) \
|
|
948 } \
|
|
949 } \
|
|
950 return Qt; \
|
|
951 }
|
|
952 #else /* !WITH_NUMBER_TYPES */
|
|
953 #define ARITHCOMPARE_MANY(c_op,op) \
|
428
|
954 { \
|
|
955 int_or_double iod1, iod2, *p = &iod1, *q = &iod2; \
|
|
956 Lisp_Object *args_end = args + nargs; \
|
|
957 \
|
|
958 number_char_or_marker_to_int_or_double (*args++, p); \
|
|
959 \
|
|
960 while (args < args_end) \
|
|
961 { \
|
|
962 number_char_or_marker_to_int_or_double (*args++, q); \
|
|
963 \
|
|
964 if (!((p->int_p && q->int_p) ? \
|
1983
|
965 (p->c.ival c_op q->c.ival) : \
|
|
966 ((p->int_p ? (double) p->c.ival : p->c.dval) c_op \
|
428
|
967 (q->int_p ? (double) q->c.ival : q->c.dval)))) \
|
|
968 return Qnil; \
|
|
969 \
|
|
970 { /* swap */ int_or_double *r = p; p = q; q = r; } \
|
|
971 } \
|
|
972 return Qt; \
|
|
973 }
|
1983
|
974 #endif /* WITH_NUMBER_TYPES */
|
428
|
975
|
|
976 DEFUN ("=", Feqlsign, 1, MANY, 0, /*
|
|
977 Return t if all the arguments are numerically equal.
|
|
978 The arguments may be numbers, characters or markers.
|
|
979 */
|
|
980 (int nargs, Lisp_Object *args))
|
|
981 {
|
1983
|
982 ARITHCOMPARE_MANY (==, eql)
|
428
|
983 }
|
|
984
|
|
985 DEFUN ("<", Flss, 1, MANY, 0, /*
|
|
986 Return t if the sequence of arguments is monotonically increasing.
|
|
987 The arguments may be numbers, characters or markers.
|
|
988 */
|
|
989 (int nargs, Lisp_Object *args))
|
|
990 {
|
1983
|
991 ARITHCOMPARE_MANY (<, lt)
|
428
|
992 }
|
|
993
|
|
994 DEFUN (">", Fgtr, 1, MANY, 0, /*
|
|
995 Return t if the sequence of arguments is monotonically decreasing.
|
|
996 The arguments may be numbers, characters or markers.
|
|
997 */
|
|
998 (int nargs, Lisp_Object *args))
|
|
999 {
|
1983
|
1000 ARITHCOMPARE_MANY (>, gt)
|
428
|
1001 }
|
|
1002
|
|
1003 DEFUN ("<=", Fleq, 1, MANY, 0, /*
|
|
1004 Return t if the sequence of arguments is monotonically nondecreasing.
|
|
1005 The arguments may be numbers, characters or markers.
|
|
1006 */
|
|
1007 (int nargs, Lisp_Object *args))
|
|
1008 {
|
1983
|
1009 ARITHCOMPARE_MANY (<=, le)
|
428
|
1010 }
|
|
1011
|
|
1012 DEFUN (">=", Fgeq, 1, MANY, 0, /*
|
|
1013 Return t if the sequence of arguments is monotonically nonincreasing.
|
|
1014 The arguments may be numbers, characters or markers.
|
|
1015 */
|
|
1016 (int nargs, Lisp_Object *args))
|
|
1017 {
|
1983
|
1018 ARITHCOMPARE_MANY (>=, ge)
|
428
|
1019 }
|
|
1020
|
1983
|
1021 /* Unlike all the other comparisons, this is an O(N*N) algorithm. But who
|
|
1022 cares? Inspection of all elisp code distributed by xemacs.org shows that
|
|
1023 it is almost always called with 2 arguments, rarely with 3, and never with
|
|
1024 more than 3. The constant factors of algorithms with better asymptotic
|
|
1025 complexity are higher, which means that those algorithms will run SLOWER
|
|
1026 than this one in the common case. Optimize the common case! */
|
428
|
1027 DEFUN ("/=", Fneq, 1, MANY, 0, /*
|
|
1028 Return t if no two arguments are numerically equal.
|
|
1029 The arguments may be numbers, characters or markers.
|
|
1030 */
|
|
1031 (int nargs, Lisp_Object *args))
|
|
1032 {
|
1983
|
1033 #ifdef WITH_NUMBER_TYPES
|
|
1034 REGISTER int i, j;
|
|
1035 Lisp_Object obj1, obj2;
|
|
1036
|
|
1037 for (i = 0; i < nargs - 1; i++)
|
|
1038 {
|
|
1039 obj1 = args[i];
|
|
1040 for (j = i + 1; j < nargs; j++)
|
|
1041 {
|
|
1042 obj2 = args[j];
|
|
1043 switch (promote_args (&obj1, &obj2))
|
|
1044 {
|
|
1045 case FIXNUM_T:
|
|
1046 if (XREALINT (obj1) == XREALINT (obj2))
|
|
1047 return Qnil;
|
|
1048 break;
|
|
1049 #ifdef HAVE_BIGNUM
|
|
1050 case BIGNUM_T:
|
|
1051 if (bignum_eql (XBIGNUM_DATA (obj1), XBIGNUM_DATA (obj2)))
|
|
1052 return Qnil;
|
|
1053 break;
|
|
1054 #endif
|
|
1055 #ifdef HAVE_RATIO
|
|
1056 case RATIO_T:
|
|
1057 if (ratio_eql (XRATIO_DATA (obj1), XRATIO_DATA (obj2)))
|
|
1058 return Qnil;
|
|
1059 break;
|
|
1060 #endif
|
|
1061 case FLOAT_T:
|
|
1062 if (XFLOAT_DATA (obj1) == XFLOAT_DATA (obj2))
|
|
1063 return Qnil;
|
|
1064 break;
|
|
1065 #ifdef HAVE_BIGFLOAT
|
|
1066 case BIGFLOAT_T:
|
|
1067 if (bigfloat_eql (XBIGFLOAT_DATA (obj1), XBIGFLOAT_DATA (obj2)))
|
|
1068 return Qnil;
|
|
1069 break;
|
|
1070 #endif
|
|
1071 }
|
|
1072 }
|
|
1073 }
|
|
1074 return Qt;
|
|
1075 #else /* !WITH_NUMBER_TYPES */
|
428
|
1076 Lisp_Object *args_end = args + nargs;
|
|
1077 Lisp_Object *p, *q;
|
|
1078
|
|
1079 /* Unlike all the other comparisons, this is an N*N algorithm.
|
|
1080 We could use a hash table for nargs > 50 to make this linear. */
|
|
1081 for (p = args; p < args_end; p++)
|
|
1082 {
|
|
1083 int_or_double iod1, iod2;
|
|
1084 number_char_or_marker_to_int_or_double (*p, &iod1);
|
|
1085
|
|
1086 for (q = p + 1; q < args_end; q++)
|
|
1087 {
|
|
1088 number_char_or_marker_to_int_or_double (*q, &iod2);
|
|
1089
|
|
1090 if (!((iod1.int_p && iod2.int_p) ?
|
|
1091 (iod1.c.ival != iod2.c.ival) :
|
|
1092 ((iod1.int_p ? (double) iod1.c.ival : iod1.c.dval) !=
|
|
1093 (iod2.int_p ? (double) iod2.c.ival : iod2.c.dval))))
|
|
1094 return Qnil;
|
|
1095 }
|
|
1096 }
|
|
1097 return Qt;
|
1983
|
1098 #endif /* WITH_NUMBER_TYPES */
|
428
|
1099 }
|
|
1100
|
|
1101 DEFUN ("zerop", Fzerop, 1, 1, 0, /*
|
|
1102 Return t if NUMBER is zero.
|
|
1103 */
|
|
1104 (number))
|
|
1105 {
|
|
1106 retry:
|
|
1107 if (INTP (number))
|
|
1108 return EQ (number, Qzero) ? Qt : Qnil;
|
1983
|
1109 #ifdef HAVE_BIGNUM
|
|
1110 else if (BIGNUMP (number))
|
|
1111 return bignum_sign (XBIGNUM_DATA (number)) == 0 ? Qt : Qnil;
|
|
1112 #endif
|
|
1113 #ifdef HAVE_RATIO
|
|
1114 else if (RATIOP (number))
|
|
1115 return ratio_sign (XRATIO_DATA (number)) == 0 ? Qt : Qnil;
|
|
1116 #endif
|
428
|
1117 else if (FLOATP (number))
|
|
1118 return XFLOAT_DATA (number) == 0.0 ? Qt : Qnil;
|
1983
|
1119 #ifdef HAVE_BIGFLOAT
|
|
1120 else if (BIGFLOATP (number))
|
|
1121 return bigfloat_sign (XBIGFLOAT_DATA (number)) == 0 ? Qt : Qnil;
|
|
1122 #endif
|
428
|
1123 else
|
|
1124 {
|
|
1125 number = wrong_type_argument (Qnumberp, number);
|
|
1126 goto retry;
|
|
1127 }
|
|
1128 }
|
|
1129
|
|
1130 /* Convert between a 32-bit value and a cons of two 16-bit values.
|
|
1131 This is used to pass 32-bit integers to and from the user.
|
|
1132 Use time_to_lisp() and lisp_to_time() for time values.
|
|
1133
|
|
1134 If you're thinking of using this to store a pointer into a Lisp Object
|
|
1135 for internal purposes (such as when calling record_unwind_protect()),
|
|
1136 try using make_opaque_ptr()/get_opaque_ptr() instead. */
|
|
1137 Lisp_Object
|
|
1138 word_to_lisp (unsigned int item)
|
|
1139 {
|
|
1140 return Fcons (make_int (item >> 16), make_int (item & 0xffff));
|
|
1141 }
|
|
1142
|
|
1143 unsigned int
|
|
1144 lisp_to_word (Lisp_Object item)
|
|
1145 {
|
|
1146 if (INTP (item))
|
|
1147 return XINT (item);
|
|
1148 else
|
|
1149 {
|
|
1150 Lisp_Object top = Fcar (item);
|
|
1151 Lisp_Object bot = Fcdr (item);
|
|
1152 CHECK_INT (top);
|
|
1153 CHECK_INT (bot);
|
|
1154 return (XINT (top) << 16) | (XINT (bot) & 0xffff);
|
|
1155 }
|
|
1156 }
|
|
1157
|
|
1158
|
|
1159 DEFUN ("number-to-string", Fnumber_to_string, 1, 1, 0, /*
|
444
|
1160 Convert NUMBER to a string by printing it in decimal.
|
428
|
1161 Uses a minus sign if negative.
|
444
|
1162 NUMBER may be an integer or a floating point number.
|
1983
|
1163 If supported, it may also be a ratio.
|
428
|
1164 */
|
444
|
1165 (number))
|
428
|
1166 {
|
1983
|
1167 #ifdef WITH_NUMBER_TYPES
|
|
1168 CHECK_NUMBER (number);
|
|
1169 #else
|
444
|
1170 CHECK_INT_OR_FLOAT (number);
|
1983
|
1171 #endif
|
428
|
1172
|
444
|
1173 if (FLOATP (number))
|
428
|
1174 {
|
|
1175 char pigbuf[350]; /* see comments in float_to_string */
|
|
1176
|
444
|
1177 float_to_string (pigbuf, XFLOAT_DATA (number));
|
428
|
1178 return build_string (pigbuf);
|
|
1179 }
|
1983
|
1180 #ifdef HAVE_BIGNUM
|
|
1181 if (BIGNUMP (number))
|
|
1182 {
|
|
1183 char *str = bignum_to_string (XBIGNUM_DATA (number), 10);
|
|
1184 Lisp_Object retval = build_string (str);
|
|
1185 xfree (str, char *);
|
|
1186 return retval;
|
|
1187 }
|
|
1188 #endif
|
|
1189 #ifdef HAVE_RATIO
|
|
1190 if (RATIOP (number))
|
|
1191 {
|
|
1192 char *str = ratio_to_string (XRATIO_DATA (number), 10);
|
|
1193 Lisp_Object retval = build_string (str);
|
|
1194 xfree (str, char *);
|
|
1195 return retval;
|
|
1196 }
|
|
1197 #endif
|
|
1198 #ifdef HAVE_BIGFLOAT
|
|
1199 if (BIGFLOATP (number))
|
|
1200 {
|
|
1201 char *str = bigfloat_to_string (XBIGFLOAT_DATA (number), 10);
|
|
1202 Lisp_Object retval = build_string (str);
|
|
1203 xfree (str, char *);
|
|
1204 return retval;
|
|
1205 }
|
|
1206 #endif
|
428
|
1207
|
603
|
1208 {
|
|
1209 char buffer[DECIMAL_PRINT_SIZE (long)];
|
|
1210
|
|
1211 long_to_string (buffer, XINT (number));
|
|
1212 return build_string (buffer);
|
|
1213 }
|
428
|
1214 }
|
|
1215
|
|
1216 static int
|
|
1217 digit_to_number (int character, int base)
|
|
1218 {
|
|
1219 /* Assumes ASCII */
|
|
1220 int digit = ((character >= '0' && character <= '9') ? character - '0' :
|
|
1221 (character >= 'a' && character <= 'z') ? character - 'a' + 10 :
|
|
1222 (character >= 'A' && character <= 'Z') ? character - 'A' + 10 :
|
|
1223 -1);
|
|
1224
|
|
1225 return digit >= base ? -1 : digit;
|
|
1226 }
|
|
1227
|
|
1228 DEFUN ("string-to-number", Fstring_to_number, 1, 2, 0, /*
|
444
|
1229 Convert STRING to a number by parsing it as a number in base BASE.
|
428
|
1230 This parses both integers and floating point numbers.
|
1983
|
1231 If they are supported, it also reads ratios.
|
428
|
1232 It ignores leading spaces and tabs.
|
|
1233
|
444
|
1234 If BASE is nil or omitted, base 10 is used.
|
|
1235 BASE must be an integer between 2 and 16 (inclusive).
|
428
|
1236 Floating point numbers always use base 10.
|
|
1237 */
|
|
1238 (string, base))
|
|
1239 {
|
|
1240 char *p;
|
|
1241 int b;
|
|
1242
|
|
1243 CHECK_STRING (string);
|
|
1244
|
|
1245 if (NILP (base))
|
|
1246 b = 10;
|
|
1247 else
|
|
1248 {
|
|
1249 CHECK_INT (base);
|
|
1250 b = XINT (base);
|
|
1251 check_int_range (b, 2, 16);
|
|
1252 }
|
|
1253
|
|
1254 p = (char *) XSTRING_DATA (string);
|
|
1255
|
|
1256 /* Skip any whitespace at the front of the number. Some versions of
|
|
1257 atoi do this anyway, so we might as well make Emacs lisp consistent. */
|
|
1258 while (*p == ' ' || *p == '\t')
|
|
1259 p++;
|
|
1260
|
442
|
1261 if (isfloat_string (p) && b == 10)
|
1983
|
1262 {
|
|
1263 #ifdef HAVE_BIGFLOAT
|
|
1264 if (ZEROP (Vdefault_float_precision))
|
|
1265 #endif
|
|
1266 return make_float (atof (p));
|
|
1267 #ifdef HAVE_BIGFLOAT
|
|
1268 else
|
|
1269 {
|
|
1270 bigfloat_set_prec (scratch_bigfloat, bigfloat_get_default_prec ());
|
|
1271 bigfloat_set_string (scratch_bigfloat, p, b);
|
|
1272 return make_bigfloat_bf (scratch_bigfloat);
|
|
1273 }
|
|
1274 #endif
|
|
1275 }
|
|
1276
|
|
1277 #ifdef HAVE_RATIO
|
|
1278 if (qxestrchr (p, '/') != NULL)
|
|
1279 {
|
|
1280 ratio_set_string (scratch_ratio, p, b);
|
|
1281 return make_ratio_rt (scratch_ratio);
|
|
1282 }
|
|
1283 #endif /* HAVE_RATIO */
|
|
1284
|
|
1285 #ifdef HAVE_BIGNUM
|
|
1286 /* GMP bignum_set_string returns random values when fed an empty string */
|
|
1287 if (*p == '\0')
|
|
1288 return make_int (0);
|
|
1289 bignum_set_string (scratch_bignum, p, b);
|
|
1290 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
1291 #else
|
428
|
1292 if (b == 10)
|
|
1293 {
|
|
1294 /* Use the system-provided functions for base 10. */
|
|
1295 #if SIZEOF_EMACS_INT == SIZEOF_INT
|
|
1296 return make_int (atoi (p));
|
|
1297 #elif SIZEOF_EMACS_INT == SIZEOF_LONG
|
|
1298 return make_int (atol (p));
|
|
1299 #elif SIZEOF_EMACS_INT == SIZEOF_LONG_LONG
|
|
1300 return make_int (atoll (p));
|
|
1301 #endif
|
|
1302 }
|
|
1303 else
|
|
1304 {
|
444
|
1305 int negative = 1;
|
428
|
1306 EMACS_INT v = 0;
|
|
1307
|
|
1308 if (*p == '-')
|
|
1309 {
|
|
1310 negative = -1;
|
|
1311 p++;
|
|
1312 }
|
|
1313 else if (*p == '+')
|
|
1314 p++;
|
|
1315 while (1)
|
|
1316 {
|
444
|
1317 int digit = digit_to_number (*p++, b);
|
428
|
1318 if (digit < 0)
|
|
1319 break;
|
|
1320 v = v * b + digit;
|
|
1321 }
|
|
1322 return make_int (negative * v);
|
|
1323 }
|
1983
|
1324 #endif /* HAVE_BIGNUM */
|
428
|
1325 }
|
|
1326
|
|
1327
|
|
1328 DEFUN ("+", Fplus, 0, MANY, 0, /*
|
|
1329 Return sum of any number of arguments.
|
|
1330 The arguments should all be numbers, characters or markers.
|
|
1331 */
|
|
1332 (int nargs, Lisp_Object *args))
|
|
1333 {
|
1983
|
1334 #ifdef WITH_NUMBER_TYPES
|
|
1335 REGISTER int i;
|
|
1336 Lisp_Object accum = make_int (0), addend;
|
|
1337
|
|
1338 for (i = 0; i < nargs; i++)
|
|
1339 {
|
|
1340 addend = args[i];
|
|
1341 switch (promote_args (&accum, &addend))
|
|
1342 {
|
|
1343 case FIXNUM_T:
|
|
1344 accum = make_integer (XREALINT (accum) + XREALINT (addend));
|
|
1345 break;
|
|
1346 #ifdef HAVE_BIGNUM
|
|
1347 case BIGNUM_T:
|
|
1348 bignum_add (scratch_bignum, XBIGNUM_DATA (accum),
|
|
1349 XBIGNUM_DATA (addend));
|
|
1350 accum = make_bignum_bg (scratch_bignum);
|
|
1351 break;
|
|
1352 #endif
|
|
1353 #ifdef HAVE_RATIO
|
|
1354 case RATIO_T:
|
|
1355 ratio_add (scratch_ratio, XRATIO_DATA (accum),
|
|
1356 XRATIO_DATA (addend));
|
|
1357 accum = make_ratio_rt (scratch_ratio);
|
|
1358 break;
|
|
1359 #endif
|
|
1360 case FLOAT_T:
|
|
1361 accum = make_float (XFLOAT_DATA (accum) + XFLOAT_DATA (addend));
|
|
1362 break;
|
|
1363 #ifdef HAVE_BIGFLOAT
|
|
1364 case BIGFLOAT_T:
|
|
1365 bigfloat_set_prec (scratch_bigfloat,
|
|
1366 max (XBIGFLOAT_GET_PREC (addend),
|
|
1367 XBIGFLOAT_GET_PREC (accum)));
|
|
1368 bigfloat_add (scratch_bigfloat, XBIGFLOAT_DATA (accum),
|
|
1369 XBIGFLOAT_DATA (addend));
|
|
1370 accum = make_bigfloat_bf (scratch_bigfloat);
|
|
1371 break;
|
|
1372 #endif
|
|
1373 }
|
|
1374 }
|
|
1375 return Fcanonicalize_number (accum);
|
|
1376 #else /* !WITH_NUMBER_TYPES */
|
428
|
1377 EMACS_INT iaccum = 0;
|
|
1378 Lisp_Object *args_end = args + nargs;
|
|
1379
|
|
1380 while (args < args_end)
|
|
1381 {
|
|
1382 int_or_double iod;
|
|
1383 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1384 if (iod.int_p)
|
|
1385 iaccum += iod.c.ival;
|
|
1386 else
|
|
1387 {
|
|
1388 double daccum = (double) iaccum + iod.c.dval;
|
|
1389 while (args < args_end)
|
|
1390 daccum += number_char_or_marker_to_double (*args++);
|
|
1391 return make_float (daccum);
|
|
1392 }
|
|
1393 }
|
|
1394
|
|
1395 return make_int (iaccum);
|
1983
|
1396 #endif /* WITH_NUMBER_TYPES */
|
428
|
1397 }
|
|
1398
|
|
1399 DEFUN ("-", Fminus, 1, MANY, 0, /*
|
|
1400 Negate number or subtract numbers, characters or markers.
|
|
1401 With one arg, negates it. With more than one arg,
|
|
1402 subtracts all but the first from the first.
|
|
1403 */
|
|
1404 (int nargs, Lisp_Object *args))
|
|
1405 {
|
1983
|
1406 #ifdef WITH_NUMBER_TYPES
|
|
1407 REGISTER int i;
|
|
1408 Lisp_Object accum = args[0], subtrahend;
|
|
1409
|
|
1410 if (nargs == 1)
|
|
1411 {
|
|
1412 if (CHARP (accum))
|
|
1413 accum = make_int (XCHAR (accum));
|
|
1414 else if (MARKERP (accum))
|
|
1415 accum = make_int (marker_position (accum));
|
|
1416
|
|
1417 /* Invert the sign of accum */
|
|
1418 CHECK_NUMBER (accum);
|
|
1419 switch (get_number_type (accum))
|
|
1420 {
|
|
1421 case FIXNUM_T:
|
|
1422 return make_integer (-XREALINT (accum));
|
|
1423 #ifdef HAVE_BIGNUM
|
|
1424 case BIGNUM_T:
|
|
1425 bignum_neg (scratch_bignum, XBIGNUM_DATA (accum));
|
|
1426 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
1427 #endif
|
|
1428 #ifdef HAVE_RATIO
|
|
1429 case RATIO_T:
|
|
1430 ratio_neg (scratch_ratio, XRATIO_DATA (accum));
|
|
1431 return make_ratio_rt (scratch_ratio);
|
|
1432 #endif
|
|
1433 case FLOAT_T:
|
|
1434 return make_float (-XFLOAT_DATA (accum));
|
|
1435 #ifdef HAVE_BIGFLOAT
|
|
1436 case BIGFLOAT_T:
|
|
1437 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (accum));
|
|
1438 bigfloat_neg (scratch_bigfloat, XBIGFLOAT_DATA (accum));
|
|
1439 return make_bigfloat_bf (scratch_bigfloat);
|
|
1440 #endif
|
|
1441 }
|
|
1442 }
|
|
1443 else
|
|
1444 {
|
|
1445 /* Subtrace the remaining arguments from accum */
|
|
1446 for (i = 1; i < nargs; i++)
|
|
1447 {
|
|
1448 subtrahend = args[i];
|
|
1449 switch (promote_args (&accum, &subtrahend))
|
|
1450 {
|
|
1451 case FIXNUM_T:
|
|
1452 accum = make_integer (XREALINT (accum) - XREALINT (subtrahend));
|
|
1453 break;
|
|
1454 #ifdef HAVE_BIGNUM
|
|
1455 case BIGNUM_T:
|
|
1456 bignum_sub (scratch_bignum, XBIGNUM_DATA (accum),
|
|
1457 XBIGNUM_DATA (subtrahend));
|
|
1458 accum = make_bignum_bg (scratch_bignum);
|
|
1459 break;
|
|
1460 #endif
|
|
1461 #ifdef HAVE_RATIO
|
|
1462 case RATIO_T:
|
|
1463 ratio_sub (scratch_ratio, XRATIO_DATA (accum),
|
|
1464 XRATIO_DATA (subtrahend));
|
|
1465 accum = make_ratio_rt (scratch_ratio);
|
|
1466 break;
|
|
1467 #endif
|
|
1468 case FLOAT_T:
|
|
1469 accum =
|
|
1470 make_float (XFLOAT_DATA (accum) - XFLOAT_DATA (subtrahend));
|
|
1471 break;
|
|
1472 #ifdef HAVE_BIGFLOAT
|
|
1473 case BIGFLOAT_T:
|
|
1474 bigfloat_set_prec (scratch_bigfloat,
|
|
1475 max (XBIGFLOAT_GET_PREC (subtrahend),
|
|
1476 XBIGFLOAT_GET_PREC (accum)));
|
|
1477 bigfloat_sub (scratch_bigfloat, XBIGFLOAT_DATA (accum),
|
|
1478 XBIGFLOAT_DATA (subtrahend));
|
|
1479 accum = make_bigfloat_bf (scratch_bigfloat);
|
|
1480 break;
|
|
1481 #endif
|
|
1482 }
|
|
1483 }
|
|
1484 }
|
|
1485 return Fcanonicalize_number (accum);
|
|
1486 #else /* !WITH_NUMBER_TYPES */
|
428
|
1487 EMACS_INT iaccum;
|
|
1488 double daccum;
|
|
1489 Lisp_Object *args_end = args + nargs;
|
|
1490 int_or_double iod;
|
|
1491
|
|
1492 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1493 if (iod.int_p)
|
|
1494 iaccum = nargs > 1 ? iod.c.ival : - iod.c.ival;
|
|
1495 else
|
|
1496 {
|
|
1497 daccum = nargs > 1 ? iod.c.dval : - iod.c.dval;
|
|
1498 goto do_float;
|
|
1499 }
|
|
1500
|
|
1501 while (args < args_end)
|
|
1502 {
|
|
1503 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1504 if (iod.int_p)
|
|
1505 iaccum -= iod.c.ival;
|
|
1506 else
|
|
1507 {
|
|
1508 daccum = (double) iaccum - iod.c.dval;
|
|
1509 goto do_float;
|
|
1510 }
|
|
1511 }
|
|
1512
|
|
1513 return make_int (iaccum);
|
|
1514
|
|
1515 do_float:
|
|
1516 for (; args < args_end; args++)
|
|
1517 daccum -= number_char_or_marker_to_double (*args);
|
|
1518 return make_float (daccum);
|
1983
|
1519 #endif /* WITH_NUMBER_TYPES */
|
428
|
1520 }
|
|
1521
|
|
1522 DEFUN ("*", Ftimes, 0, MANY, 0, /*
|
|
1523 Return product of any number of arguments.
|
|
1524 The arguments should all be numbers, characters or markers.
|
|
1525 */
|
|
1526 (int nargs, Lisp_Object *args))
|
|
1527 {
|
1983
|
1528 #ifdef WITH_NUMBER_TYPES
|
|
1529 REGISTER int i;
|
|
1530 /* Start with a bignum to avoid overflow */
|
|
1531 Lisp_Object accum = make_bignum (1L), multiplier;
|
|
1532
|
|
1533 for (i = 0; i < nargs; i++)
|
|
1534 {
|
|
1535 multiplier = args[i];
|
|
1536 switch (promote_args (&accum, &multiplier))
|
|
1537 {
|
|
1538 #ifdef HAVE_BIGNUM
|
|
1539 case BIGNUM_T:
|
|
1540 bignum_mul (scratch_bignum, XBIGNUM_DATA (accum),
|
|
1541 XBIGNUM_DATA (multiplier));
|
|
1542 accum = make_bignum_bg (scratch_bignum);
|
|
1543 break;
|
|
1544 #endif
|
|
1545 #ifdef HAVE_RATIO
|
|
1546 case RATIO_T:
|
|
1547 ratio_mul (scratch_ratio, XRATIO_DATA (accum),
|
|
1548 XRATIO_DATA (multiplier));
|
|
1549 accum = make_ratio_rt (scratch_ratio);
|
|
1550 break;
|
|
1551 #endif
|
|
1552 case FLOAT_T:
|
|
1553 accum = make_float (XFLOAT_DATA (accum) * XFLOAT_DATA (multiplier));
|
|
1554 break;
|
|
1555 #ifdef HAVE_BIGFLOAT
|
|
1556 case BIGFLOAT_T:
|
|
1557 bigfloat_set_prec (scratch_bigfloat,
|
|
1558 max (XBIGFLOAT_GET_PREC (multiplier),
|
|
1559 XBIGFLOAT_GET_PREC (accum)));
|
|
1560 bigfloat_mul (scratch_bigfloat, XBIGFLOAT_DATA (accum),
|
|
1561 XBIGFLOAT_DATA (multiplier));
|
|
1562 accum = make_bigfloat_bf (scratch_bigfloat);
|
|
1563 break;
|
|
1564 #endif
|
|
1565 }
|
|
1566 }
|
|
1567 return Fcanonicalize_number (accum);
|
|
1568 #else /* !WITH_NUMBER_TYPES */
|
428
|
1569 EMACS_INT iaccum = 1;
|
|
1570 Lisp_Object *args_end = args + nargs;
|
|
1571
|
|
1572 while (args < args_end)
|
|
1573 {
|
|
1574 int_or_double iod;
|
|
1575 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1576 if (iod.int_p)
|
|
1577 iaccum *= iod.c.ival;
|
|
1578 else
|
|
1579 {
|
|
1580 double daccum = (double) iaccum * iod.c.dval;
|
|
1581 while (args < args_end)
|
|
1582 daccum *= number_char_or_marker_to_double (*args++);
|
|
1583 return make_float (daccum);
|
|
1584 }
|
|
1585 }
|
|
1586
|
|
1587 return make_int (iaccum);
|
1983
|
1588 #endif /* WITH_NUMBER_TYPES */
|
428
|
1589 }
|
|
1590
|
1983
|
1591 #ifdef HAVE_RATIO
|
|
1592 DEFUN ("div", Fdiv, 1, MANY, 0, /*
|
|
1593 Same as `/', but dividing integers creates a ratio instead of truncating.
|
|
1594 Note that this is a departure from Common Lisp, where / creates ratios when
|
|
1595 dividing integers. Having a separate function lets us avoid breaking existing
|
|
1596 Emacs Lisp code that expects / to do integer division.
|
|
1597 */
|
|
1598 (int nargs, Lisp_Object *args))
|
|
1599 {
|
|
1600 REGISTER int i;
|
|
1601 Lisp_Object accum, divisor;
|
|
1602
|
|
1603 if (nargs == 1)
|
|
1604 {
|
|
1605 i = 0;
|
|
1606 accum = make_int (1);
|
|
1607 }
|
|
1608 else
|
|
1609 {
|
|
1610 i = 1;
|
|
1611 accum = args[0];
|
|
1612 }
|
|
1613 for (; i < nargs; i++)
|
|
1614 {
|
|
1615 divisor = args[i];
|
|
1616 switch (promote_args (&accum, &divisor))
|
|
1617 {
|
|
1618 case FIXNUM_T:
|
|
1619 if (XREALINT (divisor) == 0) goto divide_by_zero;
|
|
1620 bignum_set_long (scratch_bignum, XREALINT (accum));
|
|
1621 bignum_set_long (scratch_bignum2, XREALINT (divisor));
|
|
1622 accum = make_ratio_bg (scratch_bignum, scratch_bignum2);
|
|
1623 break;
|
|
1624 case BIGNUM_T:
|
|
1625 if (bignum_sign (XBIGNUM_DATA (divisor)) == 0) goto divide_by_zero;
|
|
1626 accum = make_ratio_bg (XBIGNUM_DATA (accum), XBIGNUM_DATA (divisor));
|
|
1627 break;
|
|
1628 case RATIO_T:
|
|
1629 if (ratio_sign (XRATIO_DATA (divisor)) == 0) goto divide_by_zero;
|
|
1630 ratio_div (scratch_ratio, XRATIO_DATA (accum),
|
|
1631 XRATIO_DATA (divisor));
|
|
1632 accum = make_ratio_rt (scratch_ratio);
|
|
1633 break;
|
|
1634 case FLOAT_T:
|
|
1635 if (XFLOAT_DATA (divisor) == 0.0) goto divide_by_zero;
|
|
1636 accum = make_float (XFLOAT_DATA (accum) / XFLOAT_DATA (divisor));
|
|
1637 break;
|
|
1638 #ifdef HAVE_BIGFLOAT
|
|
1639 case BIGFLOAT_T:
|
|
1640 if (bigfloat_sign (XBIGFLOAT_DATA (divisor)) == 0)
|
|
1641 goto divide_by_zero;
|
|
1642 bigfloat_set_prec (scratch_bigfloat,
|
|
1643 max (XBIGFLOAT_GET_PREC (divisor),
|
|
1644 XBIGFLOAT_GET_PREC (accum)));
|
|
1645 bigfloat_div (scratch_bigfloat, XBIGFLOAT_DATA (accum),
|
|
1646 XBIGFLOAT_DATA (divisor));
|
|
1647 accum = make_bigfloat_bf (scratch_bigfloat);
|
|
1648 break;
|
|
1649 #endif
|
|
1650 }
|
|
1651 }
|
|
1652 return Fcanonicalize_number (accum);
|
|
1653
|
|
1654 divide_by_zero:
|
|
1655 Fsignal (Qarith_error, Qnil);
|
|
1656 return Qnil; /* not (usually) reached */
|
|
1657 }
|
|
1658 #endif /* HAVE_RATIO */
|
|
1659
|
428
|
1660 DEFUN ("/", Fquo, 1, MANY, 0, /*
|
|
1661 Return first argument divided by all the remaining arguments.
|
|
1662 The arguments must be numbers, characters or markers.
|
|
1663 With one argument, reciprocates the argument.
|
|
1664 */
|
|
1665 (int nargs, Lisp_Object *args))
|
|
1666 {
|
1983
|
1667 #ifdef WITH_NUMBER_TYPES
|
|
1668 REGISTER int i;
|
|
1669 Lisp_Object accum, divisor;
|
|
1670
|
|
1671 if (nargs == 1)
|
|
1672 {
|
|
1673 i = 0;
|
|
1674 accum = make_int (1);
|
|
1675 }
|
|
1676 else
|
|
1677 {
|
|
1678 i = 1;
|
|
1679 accum = args[0];
|
|
1680 }
|
|
1681 for (; i < nargs; i++)
|
|
1682 {
|
|
1683 divisor = args[i];
|
|
1684 switch (promote_args (&accum, &divisor))
|
|
1685 {
|
|
1686 case FIXNUM_T:
|
|
1687 if (XREALINT (divisor) == 0) goto divide_by_zero;
|
|
1688 accum = make_integer (XREALINT (accum) / XREALINT (divisor));
|
|
1689 break;
|
|
1690 #ifdef HAVE_BIGNUM
|
|
1691 case BIGNUM_T:
|
|
1692 if (bignum_sign (XBIGNUM_DATA (divisor)) == 0) goto divide_by_zero;
|
|
1693 bignum_div (scratch_bignum, XBIGNUM_DATA (accum),
|
|
1694 XBIGNUM_DATA (divisor));
|
|
1695 accum = make_bignum_bg (scratch_bignum);
|
|
1696 break;
|
|
1697 #endif
|
|
1698 #ifdef HAVE_RATIO
|
|
1699 case RATIO_T:
|
|
1700 if (ratio_sign (XRATIO_DATA (divisor)) == 0) goto divide_by_zero;
|
|
1701 ratio_div (scratch_ratio, XRATIO_DATA (accum),
|
|
1702 XRATIO_DATA (divisor));
|
|
1703 accum = make_ratio_rt (scratch_ratio);
|
|
1704 break;
|
|
1705 #endif
|
|
1706 case FLOAT_T:
|
|
1707 if (XFLOAT_DATA (divisor) == 0.0) goto divide_by_zero;
|
|
1708 accum = make_float (XFLOAT_DATA (accum) / XFLOAT_DATA (divisor));
|
|
1709 break;
|
|
1710 #ifdef HAVE_BIGFLOAT
|
|
1711 case BIGFLOAT_T:
|
|
1712 if (bigfloat_sign (XBIGFLOAT_DATA (divisor)) == 0)
|
|
1713 goto divide_by_zero;
|
|
1714 bigfloat_set_prec (scratch_bigfloat,
|
|
1715 max (XBIGFLOAT_GET_PREC (divisor),
|
|
1716 XBIGFLOAT_GET_PREC (accum)));
|
|
1717 bigfloat_div (scratch_bigfloat, XBIGFLOAT_DATA (accum),
|
|
1718 XBIGFLOAT_DATA (divisor));
|
|
1719 accum = make_bigfloat_bf (scratch_bigfloat);
|
|
1720 break;
|
|
1721 #endif
|
|
1722 }
|
|
1723 }
|
|
1724 return Fcanonicalize_number (accum);
|
|
1725 #else /* !WITH_NUMBER_TYPES */
|
428
|
1726 EMACS_INT iaccum;
|
|
1727 double daccum;
|
|
1728 Lisp_Object *args_end = args + nargs;
|
|
1729 int_or_double iod;
|
|
1730
|
|
1731 if (nargs == 1)
|
|
1732 iaccum = 1;
|
|
1733 else
|
|
1734 {
|
|
1735 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1736 if (iod.int_p)
|
|
1737 iaccum = iod.c.ival;
|
|
1738 else
|
|
1739 {
|
|
1740 daccum = iod.c.dval;
|
|
1741 goto divide_floats;
|
|
1742 }
|
|
1743 }
|
|
1744
|
|
1745 while (args < args_end)
|
|
1746 {
|
|
1747 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1748 if (iod.int_p)
|
|
1749 {
|
|
1750 if (iod.c.ival == 0) goto divide_by_zero;
|
|
1751 iaccum /= iod.c.ival;
|
|
1752 }
|
|
1753 else
|
|
1754 {
|
|
1755 if (iod.c.dval == 0) goto divide_by_zero;
|
|
1756 daccum = (double) iaccum / iod.c.dval;
|
|
1757 goto divide_floats;
|
|
1758 }
|
|
1759 }
|
|
1760
|
|
1761 return make_int (iaccum);
|
|
1762
|
|
1763 divide_floats:
|
|
1764 for (; args < args_end; args++)
|
|
1765 {
|
|
1766 double dval = number_char_or_marker_to_double (*args);
|
|
1767 if (dval == 0) goto divide_by_zero;
|
|
1768 daccum /= dval;
|
|
1769 }
|
|
1770 return make_float (daccum);
|
1983
|
1771 #endif /* WITH_NUMBER_TYPES */
|
428
|
1772
|
|
1773 divide_by_zero:
|
|
1774 Fsignal (Qarith_error, Qnil);
|
801
|
1775 return Qnil; /* not (usually) reached */
|
428
|
1776 }
|
|
1777
|
|
1778 DEFUN ("max", Fmax, 1, MANY, 0, /*
|
|
1779 Return largest of all the arguments.
|
1983
|
1780 All arguments must be real numbers, characters or markers.
|
428
|
1781 The value is always a number; markers and characters are converted
|
|
1782 to numbers.
|
|
1783 */
|
|
1784 (int nargs, Lisp_Object *args))
|
|
1785 {
|
1983
|
1786 #ifdef WITH_NUMBER_TYPES
|
|
1787 REGISTER int i, maxindex = 0;
|
|
1788 Lisp_Object comp1, comp2;
|
|
1789
|
|
1790 while (!(CHARP (args[0]) || MARKERP (args[0]) || REALP (args[0])))
|
|
1791 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]);
|
|
1792 if (CHARP (args[0]))
|
|
1793 args[0] = make_int (XCHAR (args[0]));
|
|
1794 else if (MARKERP (args[0]))
|
|
1795 args[0] = make_int (marker_position (args[0]));
|
|
1796 for (i = 1; i < nargs; i++)
|
|
1797 {
|
|
1798 retry:
|
|
1799 comp1 = args[maxindex];
|
|
1800 comp2 = args[i];
|
|
1801 switch (promote_args (&comp1, &comp2))
|
|
1802 {
|
|
1803 case FIXNUM_T:
|
|
1804 if (XREALINT (comp1) < XREALINT (comp2))
|
|
1805 maxindex = i;
|
|
1806 break;
|
|
1807 #ifdef HAVE_BIGNUM
|
|
1808 case BIGNUM_T:
|
|
1809 if (bignum_lt (XBIGNUM_DATA (comp1), XBIGNUM_DATA (comp2)))
|
|
1810 maxindex = i;
|
|
1811 break;
|
|
1812 #endif
|
|
1813 #ifdef HAVE_RATIO
|
|
1814 case RATIO_T:
|
|
1815 if (ratio_lt (XRATIO_DATA (comp1), XRATIO_DATA (comp2)))
|
|
1816 maxindex = i;
|
|
1817 break;
|
|
1818 #endif
|
|
1819 case FLOAT_T:
|
|
1820 if (XFLOAT_DATA (comp1) < XFLOAT_DATA (comp2))
|
|
1821 maxindex = i;
|
|
1822 break;
|
|
1823 #ifdef HAVE_BIGFLOAT
|
|
1824 case BIGFLOAT_T:
|
|
1825 if (bigfloat_lt (XBIGFLOAT_DATA (comp1), XBIGFLOAT_DATA (comp2)))
|
|
1826 maxindex = i;
|
|
1827 break;
|
|
1828 #endif
|
|
1829 }
|
|
1830 }
|
|
1831 return args[maxindex];
|
|
1832 #else /* !WITH_NUMBER_TYPES */
|
428
|
1833 EMACS_INT imax;
|
|
1834 double dmax;
|
|
1835 Lisp_Object *args_end = args + nargs;
|
|
1836 int_or_double iod;
|
|
1837
|
|
1838 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1839 if (iod.int_p)
|
|
1840 imax = iod.c.ival;
|
|
1841 else
|
|
1842 {
|
|
1843 dmax = iod.c.dval;
|
|
1844 goto max_floats;
|
|
1845 }
|
|
1846
|
|
1847 while (args < args_end)
|
|
1848 {
|
|
1849 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1850 if (iod.int_p)
|
|
1851 {
|
|
1852 if (imax < iod.c.ival) imax = iod.c.ival;
|
|
1853 }
|
|
1854 else
|
|
1855 {
|
|
1856 dmax = (double) imax;
|
|
1857 if (dmax < iod.c.dval) dmax = iod.c.dval;
|
|
1858 goto max_floats;
|
|
1859 }
|
|
1860 }
|
|
1861
|
|
1862 return make_int (imax);
|
|
1863
|
|
1864 max_floats:
|
|
1865 while (args < args_end)
|
|
1866 {
|
|
1867 double dval = number_char_or_marker_to_double (*args++);
|
|
1868 if (dmax < dval) dmax = dval;
|
|
1869 }
|
|
1870 return make_float (dmax);
|
1983
|
1871 #endif /* WITH_NUMBER_TYPES */
|
428
|
1872 }
|
|
1873
|
|
1874 DEFUN ("min", Fmin, 1, MANY, 0, /*
|
|
1875 Return smallest of all the arguments.
|
|
1876 All arguments must be numbers, characters or markers.
|
|
1877 The value is always a number; markers and characters are converted
|
|
1878 to numbers.
|
|
1879 */
|
|
1880 (int nargs, Lisp_Object *args))
|
|
1881 {
|
1983
|
1882 #ifdef WITH_NUMBER_TYPES
|
|
1883 REGISTER int i, minindex = 0;
|
|
1884 Lisp_Object comp1, comp2;
|
|
1885
|
|
1886 while (!(CHARP (args[0]) || MARKERP (args[0]) || REALP (args[0])))
|
|
1887 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]);
|
|
1888 if (CHARP (args[0]))
|
|
1889 args[0] = make_int (XCHAR (args[0]));
|
|
1890 else if (MARKERP (args[0]))
|
|
1891 args[0] = make_int (marker_position (args[0]));
|
|
1892 for (i = 1; i < nargs; i++)
|
|
1893 {
|
|
1894 comp1 = args[minindex];
|
|
1895 comp2 = args[i];
|
|
1896 switch (promote_args (&comp1, &comp2))
|
|
1897 {
|
|
1898 case FIXNUM_T:
|
|
1899 if (XREALINT (comp1) > XREALINT (comp2))
|
|
1900 minindex = i;
|
|
1901 break;
|
|
1902 #ifdef HAVE_BIGNUM
|
|
1903 case BIGNUM_T:
|
|
1904 if (bignum_gt (XBIGNUM_DATA (comp1), XBIGNUM_DATA (comp2)))
|
|
1905 minindex = i;
|
|
1906 break;
|
|
1907 #endif
|
|
1908 #ifdef HAVE_RATIO
|
|
1909 case RATIO_T:
|
|
1910 if (ratio_gt (XRATIO_DATA (comp1), XRATIO_DATA (comp2)))
|
|
1911 minindex = i;
|
|
1912 break;
|
|
1913 #endif
|
|
1914 case FLOAT_T:
|
|
1915 if (XFLOAT_DATA (comp1) > XFLOAT_DATA (comp2))
|
|
1916 minindex = i;
|
|
1917 break;
|
|
1918 #ifdef HAVE_BIGFLOAT
|
|
1919 case BIGFLOAT_T:
|
|
1920 if (bigfloat_gt (XBIGFLOAT_DATA (comp1), XBIGFLOAT_DATA (comp2)))
|
|
1921 minindex = i;
|
|
1922 break;
|
|
1923 #endif
|
|
1924 }
|
|
1925 }
|
|
1926 return args[minindex];
|
|
1927 #else /* !WITH_NUMBER_TYPES */
|
428
|
1928 EMACS_INT imin;
|
|
1929 double dmin;
|
|
1930 Lisp_Object *args_end = args + nargs;
|
|
1931 int_or_double iod;
|
|
1932
|
|
1933 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1934 if (iod.int_p)
|
|
1935 imin = iod.c.ival;
|
|
1936 else
|
|
1937 {
|
|
1938 dmin = iod.c.dval;
|
|
1939 goto min_floats;
|
|
1940 }
|
|
1941
|
|
1942 while (args < args_end)
|
|
1943 {
|
|
1944 number_char_or_marker_to_int_or_double (*args++, &iod);
|
|
1945 if (iod.int_p)
|
|
1946 {
|
|
1947 if (imin > iod.c.ival) imin = iod.c.ival;
|
|
1948 }
|
|
1949 else
|
|
1950 {
|
|
1951 dmin = (double) imin;
|
|
1952 if (dmin > iod.c.dval) dmin = iod.c.dval;
|
|
1953 goto min_floats;
|
|
1954 }
|
|
1955 }
|
|
1956
|
|
1957 return make_int (imin);
|
|
1958
|
|
1959 min_floats:
|
|
1960 while (args < args_end)
|
|
1961 {
|
|
1962 double dval = number_char_or_marker_to_double (*args++);
|
|
1963 if (dmin > dval) dmin = dval;
|
|
1964 }
|
|
1965 return make_float (dmin);
|
1983
|
1966 #endif /* WITH_NUMBER_TYPES */
|
428
|
1967 }
|
|
1968
|
|
1969 DEFUN ("logand", Flogand, 0, MANY, 0, /*
|
|
1970 Return bitwise-and of all the arguments.
|
|
1971 Arguments may be integers, or markers or characters converted to integers.
|
|
1972 */
|
|
1973 (int nargs, Lisp_Object *args))
|
|
1974 {
|
1983
|
1975 #ifdef HAVE_BIGNUM
|
|
1976 REGISTER int i;
|
|
1977 Lisp_Object result, other;
|
|
1978
|
|
1979 if (nargs == 0)
|
|
1980 return make_int (~0);
|
|
1981
|
|
1982 while (!(CHARP (args[0]) || MARKERP (args[0]) || INTEGERP (args[0])))
|
|
1983 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]);
|
|
1984
|
|
1985 result = args[0];
|
|
1986 if (CHARP (result))
|
|
1987 result = make_int (XCHAR (result));
|
|
1988 else if (MARKERP (result))
|
|
1989 result = make_int (marker_position (result));
|
|
1990 for (i = 1; i < nargs; i++)
|
|
1991 {
|
|
1992 while (!(CHARP (args[i]) || MARKERP (args[i]) || INTEGERP (args[i])))
|
|
1993 args[i] = wrong_type_argument (Qnumber_char_or_marker_p, args[i]);
|
|
1994 other = args[i];
|
1994
|
1995 switch (promote_args (&result & &other))
|
1983
|
1996 {
|
|
1997 case FIXNUM_T:
|
1992
|
1998 result = make_int (XREALINT (result), XREALINT (other));
|
1983
|
1999 break;
|
|
2000 case BIGNUM_T:
|
|
2001 bignum_and (scratch_bignum, XBIGNUM_DATA (result),
|
|
2002 XBIGNUM_DATA (other));
|
|
2003 result = make_bignum_bg (scratch_bignum);
|
|
2004 break;
|
|
2005 }
|
|
2006 }
|
|
2007 return Fcanonicalize_number (result);
|
|
2008 #else /* !HAVE_BIGNUM */
|
428
|
2009 EMACS_INT bits = ~0;
|
|
2010 Lisp_Object *args_end = args + nargs;
|
|
2011
|
|
2012 while (args < args_end)
|
|
2013 bits &= integer_char_or_marker_to_int (*args++);
|
|
2014
|
|
2015 return make_int (bits);
|
1983
|
2016 #endif /* HAVE_BIGNUM */
|
428
|
2017 }
|
|
2018
|
|
2019 DEFUN ("logior", Flogior, 0, MANY, 0, /*
|
|
2020 Return bitwise-or of all the arguments.
|
|
2021 Arguments may be integers, or markers or characters converted to integers.
|
|
2022 */
|
|
2023 (int nargs, Lisp_Object *args))
|
|
2024 {
|
1983
|
2025 #ifdef HAVE_BIGNUM
|
|
2026 REGISTER int i;
|
|
2027 Lisp_Object result, other;
|
|
2028
|
|
2029 if (nargs == 0)
|
|
2030 return make_int (0);
|
|
2031
|
|
2032 while (!(CHARP (args[0]) || MARKERP (args[0]) || INTEGERP (args[0])))
|
|
2033 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]);
|
|
2034
|
|
2035 result = args[0];
|
|
2036 if (CHARP (result))
|
|
2037 result = make_int (XCHAR (result));
|
|
2038 else if (MARKERP (result))
|
|
2039 result = make_int (marker_position (result));
|
|
2040 for (i = 1; i < nargs; i++)
|
|
2041 {
|
|
2042 while (!(CHARP (args[i]) || MARKERP (args[i]) || INTEGERP (args[i])))
|
|
2043 args[i] = wrong_type_argument (Qnumber_char_or_marker_p, args[i]);
|
|
2044 other = args[i];
|
|
2045 switch (promote_args (&result, &other))
|
|
2046 {
|
|
2047 case FIXNUM_T:
|
1992
|
2048 result = make_int (XREALINT (result) | XREALINT (other));
|
1983
|
2049 break;
|
|
2050 case BIGNUM_T:
|
|
2051 bignum_ior (scratch_bignum, XBIGNUM_DATA (result),
|
|
2052 XBIGNUM_DATA (other));
|
|
2053 result = make_bignum_bg (scratch_bignum);
|
|
2054 break;
|
|
2055 }
|
|
2056 }
|
|
2057 return Fcanonicalize_number (result);
|
|
2058 #else /* !HAVE_BIGNUM */
|
428
|
2059 EMACS_INT bits = 0;
|
|
2060 Lisp_Object *args_end = args + nargs;
|
|
2061
|
|
2062 while (args < args_end)
|
|
2063 bits |= integer_char_or_marker_to_int (*args++);
|
|
2064
|
|
2065 return make_int (bits);
|
1983
|
2066 #endif /* HAVE_BIGNUM */
|
428
|
2067 }
|
|
2068
|
|
2069 DEFUN ("logxor", Flogxor, 0, MANY, 0, /*
|
|
2070 Return bitwise-exclusive-or of all the arguments.
|
|
2071 Arguments may be integers, or markers or characters converted to integers.
|
|
2072 */
|
|
2073 (int nargs, Lisp_Object *args))
|
|
2074 {
|
1983
|
2075 #ifdef HAVE_BIGNUM
|
|
2076 REGISTER int i;
|
|
2077 Lisp_Object result, other;
|
|
2078
|
|
2079 if (nargs == 0)
|
|
2080 return make_int (0);
|
|
2081
|
|
2082 while (!(CHARP (args[0]) || MARKERP (args[0]) || INTEGERP (args[0])))
|
|
2083 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]);
|
|
2084
|
|
2085 result = args[0];
|
|
2086 if (CHARP (result))
|
|
2087 result = make_int (XCHAR (result));
|
|
2088 else if (MARKERP (result))
|
|
2089 result = make_int (marker_position (result));
|
|
2090 for (i = 1; i < nargs; i++)
|
|
2091 {
|
|
2092 while (!(CHARP (args[i]) || MARKERP (args[i]) || INTEGERP (args[i])))
|
|
2093 args[i] = wrong_type_argument (Qnumber_char_or_marker_p, args[i]);
|
|
2094 other = args[i];
|
|
2095 if (promote_args (&result, &other) == FIXNUM_T)
|
|
2096 {
|
|
2097 result = make_int (XREALINT (result) ^ XREALINT (other));
|
|
2098 }
|
|
2099 else
|
|
2100 {
|
|
2101 bignum_xor (scratch_bignum, XBIGNUM_DATA (result),
|
|
2102 XBIGNUM_DATA (other));
|
|
2103 result = make_bignum_bg (scratch_bignum);
|
|
2104 }
|
|
2105 }
|
|
2106 return Fcanonicalize_number (result);
|
|
2107 #else /* !HAVE_BIGNUM */
|
428
|
2108 EMACS_INT bits = 0;
|
|
2109 Lisp_Object *args_end = args + nargs;
|
|
2110
|
|
2111 while (args < args_end)
|
|
2112 bits ^= integer_char_or_marker_to_int (*args++);
|
|
2113
|
|
2114 return make_int (bits);
|
1983
|
2115 #endif /* !HAVE_BIGNUM */
|
428
|
2116 }
|
|
2117
|
|
2118 DEFUN ("lognot", Flognot, 1, 1, 0, /*
|
|
2119 Return the bitwise complement of NUMBER.
|
|
2120 NUMBER may be an integer, marker or character converted to integer.
|
|
2121 */
|
|
2122 (number))
|
|
2123 {
|
1983
|
2124 #ifdef HAVE_BIGNUM
|
|
2125 if (BIGNUMP (number))
|
|
2126 {
|
|
2127 bignum_not (scratch_bignum, XBIGNUM_DATA (number));
|
|
2128 return make_bignum_bg (scratch_bignum);
|
|
2129 }
|
|
2130 #endif /* HAVE_BIGNUM */
|
428
|
2131 return make_int (~ integer_char_or_marker_to_int (number));
|
|
2132 }
|
|
2133
|
|
2134 DEFUN ("%", Frem, 2, 2, 0, /*
|
|
2135 Return remainder of first arg divided by second.
|
|
2136 Both must be integers, characters or markers.
|
|
2137 */
|
444
|
2138 (number1, number2))
|
428
|
2139 {
|
1983
|
2140 #ifdef HAVE_BIGNUM
|
|
2141 while (!(CHARP (number1) || MARKERP (number1) || INTEGERP (number1)))
|
|
2142 number1 = wrong_type_argument (Qnumber_char_or_marker_p, number1);
|
|
2143 while (!(CHARP (number2) || MARKERP (number2) || INTEGERP (number2)))
|
|
2144 number2 = wrong_type_argument (Qnumber_char_or_marker_p, number2);
|
|
2145
|
|
2146 if (promote_args (&number1, &number2) == FIXNUM_T)
|
|
2147 {
|
|
2148 if (XREALINT (number2) == 0)
|
|
2149 Fsignal (Qarith_error, Qnil);
|
|
2150 return make_int (XREALINT (number1) % XREALINT (number2));
|
|
2151 }
|
|
2152 else
|
|
2153 {
|
|
2154 if (bignum_sign (XBIGNUM_DATA (number2)) == 0)
|
|
2155 Fsignal (Qarith_error, Qnil);
|
|
2156 bignum_mod (scratch_bignum, XBIGNUM_DATA (number1),
|
|
2157 XBIGNUM_DATA (number2));
|
|
2158 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
2159 }
|
|
2160 #else /* !HAVE_BIGNUM */
|
444
|
2161 EMACS_INT ival1 = integer_char_or_marker_to_int (number1);
|
|
2162 EMACS_INT ival2 = integer_char_or_marker_to_int (number2);
|
428
|
2163
|
|
2164 if (ival2 == 0)
|
|
2165 Fsignal (Qarith_error, Qnil);
|
|
2166
|
|
2167 return make_int (ival1 % ival2);
|
1983
|
2168 #endif /* HAVE_BIGNUM */
|
428
|
2169 }
|
|
2170
|
|
2171 /* Note, ANSI *requires* the presence of the fmod() library routine.
|
|
2172 If your system doesn't have it, complain to your vendor, because
|
|
2173 that is a bug. */
|
|
2174
|
|
2175 #ifndef HAVE_FMOD
|
|
2176 double
|
|
2177 fmod (double f1, double f2)
|
|
2178 {
|
|
2179 if (f2 < 0.0)
|
|
2180 f2 = -f2;
|
|
2181 return f1 - f2 * floor (f1/f2);
|
|
2182 }
|
|
2183 #endif /* ! HAVE_FMOD */
|
|
2184
|
|
2185
|
|
2186 DEFUN ("mod", Fmod, 2, 2, 0, /*
|
|
2187 Return X modulo Y.
|
|
2188 The result falls between zero (inclusive) and Y (exclusive).
|
|
2189 Both X and Y must be numbers, characters or markers.
|
|
2190 If either argument is a float, a float will be returned.
|
|
2191 */
|
|
2192 (x, y))
|
|
2193 {
|
1983
|
2194 #ifdef WITH_NUMBER_TYPES
|
|
2195 while (!(CHARP (x) || MARKERP (x) || REALP (x)))
|
|
2196 x = wrong_type_argument (Qnumber_char_or_marker_p, x);
|
|
2197 while (!(CHARP (y) || MARKERP (y) || REALP (y)))
|
|
2198 y = wrong_type_argument (Qnumber_char_or_marker_p, y);
|
|
2199 switch (promote_args (&x, &y))
|
|
2200 {
|
|
2201 case FIXNUM_T:
|
|
2202 {
|
|
2203 EMACS_INT ival;
|
|
2204 if (XREALINT (y) == 0) goto divide_by_zero;
|
|
2205 ival = XREALINT (x) % XREALINT (y);
|
|
2206 /* If the "remainder" comes out with the wrong sign, fix it. */
|
|
2207 if (XREALINT (y) < 0 ? ival > 0 : ival < 0)
|
|
2208 ival += XREALINT (y);
|
|
2209 return make_int (ival);
|
|
2210 }
|
|
2211 #ifdef HAVE_BIGNUM
|
|
2212 case BIGNUM_T:
|
|
2213 if (bignum_sign (XBIGNUM_DATA (y)) == 0) goto divide_by_zero;
|
|
2214 bignum_mod (scratch_bignum, XBIGNUM_DATA (x), XBIGNUM_DATA (y));
|
|
2215 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
2216 #endif
|
|
2217 #ifdef HAVE_RATIO
|
|
2218 case RATIO_T:
|
|
2219 if (ratio_sign (XRATIO_DATA (y)) == 0) goto divide_by_zero;
|
|
2220 ratio_div (scratch_ratio, XRATIO_DATA (x), XRATIO_DATA (y));
|
|
2221 bignum_div (scratch_bignum, ratio_numerator (scratch_ratio),
|
|
2222 ratio_denominator (scratch_ratio));
|
|
2223 ratio_set_bignum (scratch_ratio, scratch_bignum);
|
|
2224 ratio_mul (scratch_ratio, scratch_ratio, XRATIO_DATA (y));
|
|
2225 ratio_sub (scratch_ratio, XRATIO_DATA (x), scratch_ratio);
|
|
2226 return Fcanonicalize_number (make_ratio_rt (scratch_ratio));
|
|
2227 #endif
|
|
2228 case FLOAT_T:
|
|
2229 {
|
|
2230 double dval;
|
|
2231 if (XFLOAT_DATA (y) == 0.0) goto divide_by_zero;
|
|
2232 dval = fmod (XFLOAT_DATA (x), XFLOAT_DATA (y));
|
|
2233 /* If the "remainder" comes out with the wrong sign, fix it. */
|
|
2234 if (XFLOAT_DATA (y) < 0 ? dval > 0 : dval < 0)
|
|
2235 dval += XFLOAT_DATA (y);
|
|
2236 return make_float (dval);
|
|
2237 }
|
|
2238 #ifdef HAVE_BIGFLOAT
|
|
2239 case BIGFLOAT_T:
|
|
2240 bigfloat_set_prec (scratch_bigfloat,
|
|
2241 max (XBIGFLOAT_GET_PREC (x), XBIGFLOAT_GET_PREC (y)));
|
|
2242 bigfloat_div (scratch_bigfloat, XBIGFLOAT_DATA (x), XBIGFLOAT_DATA (y));
|
|
2243 bigfloat_trunc (scratch_bigfloat, scratch_bigfloat);
|
|
2244 bigfloat_mul (scratch_bigfloat, scratch_bigfloat, XBIGFLOAT_DATA (y));
|
|
2245 bigfloat_sub (scratch_bigfloat, XBIGFLOAT_DATA (x), scratch_bigfloat);
|
|
2246 return make_bigfloat_bf (scratch_bigfloat);
|
|
2247 #endif
|
|
2248 }
|
|
2249 #else /* !WITH_NUMBER_TYPES */
|
428
|
2250 int_or_double iod1, iod2;
|
|
2251 number_char_or_marker_to_int_or_double (x, &iod1);
|
|
2252 number_char_or_marker_to_int_or_double (y, &iod2);
|
|
2253
|
|
2254 if (!iod1.int_p || !iod2.int_p)
|
|
2255 {
|
|
2256 double dval1 = iod1.int_p ? (double) iod1.c.ival : iod1.c.dval;
|
|
2257 double dval2 = iod2.int_p ? (double) iod2.c.ival : iod2.c.dval;
|
|
2258 if (dval2 == 0) goto divide_by_zero;
|
|
2259 dval1 = fmod (dval1, dval2);
|
|
2260
|
|
2261 /* If the "remainder" comes out with the wrong sign, fix it. */
|
|
2262 if (dval2 < 0 ? dval1 > 0 : dval1 < 0)
|
|
2263 dval1 += dval2;
|
|
2264
|
|
2265 return make_float (dval1);
|
|
2266 }
|
1104
|
2267
|
428
|
2268 {
|
|
2269 EMACS_INT ival;
|
|
2270 if (iod2.c.ival == 0) goto divide_by_zero;
|
|
2271
|
|
2272 ival = iod1.c.ival % iod2.c.ival;
|
|
2273
|
|
2274 /* If the "remainder" comes out with the wrong sign, fix it. */
|
|
2275 if (iod2.c.ival < 0 ? ival > 0 : ival < 0)
|
|
2276 ival += iod2.c.ival;
|
|
2277
|
|
2278 return make_int (ival);
|
|
2279 }
|
1983
|
2280 #endif /* WITH_NUMBER_TYPES */
|
428
|
2281
|
|
2282 divide_by_zero:
|
|
2283 Fsignal (Qarith_error, Qnil);
|
801
|
2284 return Qnil; /* not (usually) reached */
|
428
|
2285 }
|
|
2286
|
|
2287 DEFUN ("ash", Fash, 2, 2, 0, /*
|
|
2288 Return VALUE with its bits shifted left by COUNT.
|
|
2289 If COUNT is negative, shifting is actually to the right.
|
|
2290 In this case, the sign bit is duplicated.
|
1983
|
2291 This function cannot be applied to bignums, as there is no leftmost sign bit
|
|
2292 to be duplicated. Use `lsh' instead.
|
428
|
2293 */
|
|
2294 (value, count))
|
|
2295 {
|
|
2296 CHECK_INT_COERCE_CHAR (value);
|
|
2297 CONCHECK_INT (count);
|
|
2298
|
|
2299 return make_int (XINT (count) > 0 ?
|
|
2300 XINT (value) << XINT (count) :
|
|
2301 XINT (value) >> -XINT (count));
|
|
2302 }
|
|
2303
|
|
2304 DEFUN ("lsh", Flsh, 2, 2, 0, /*
|
|
2305 Return VALUE with its bits shifted left by COUNT.
|
|
2306 If COUNT is negative, shifting is actually to the right.
|
|
2307 In this case, zeros are shifted in on the left.
|
|
2308 */
|
|
2309 (value, count))
|
|
2310 {
|
1983
|
2311 #ifdef HAVE_BIGNUM
|
|
2312 while (!(CHARP (value) || MARKERP (value) || INTEGERP (value)))
|
|
2313 wrong_type_argument (Qnumber_char_or_marker_p, value);
|
|
2314 CONCHECK_INTEGER (count);
|
|
2315
|
|
2316 if (promote_args (&value, &count) == FIXNUM_T)
|
|
2317 {
|
|
2318 if (XREALINT (count) <= 0)
|
|
2319 return make_int (XREALINT (value) >> -XREALINT (count));
|
|
2320 /* Use bignums to avoid overflow */
|
|
2321 bignum_set_long (scratch_bignum2, XREALINT (value));
|
|
2322 bignum_lshift (scratch_bignum, scratch_bignum2, XREALINT (count));
|
|
2323 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
2324 }
|
|
2325 else
|
|
2326 {
|
|
2327 if (bignum_sign (XBIGNUM_DATA (count)) <= 0)
|
|
2328 {
|
|
2329 bignum_neg (scratch_bignum, XBIGNUM_DATA (count));
|
|
2330 if (!bignum_fits_ulong_p (scratch_bignum))
|
|
2331 args_out_of_range (Qnumber_char_or_marker_p, count);
|
|
2332 bignum_rshift (scratch_bignum2, XBIGNUM_DATA (value),
|
|
2333 bignum_to_ulong (scratch_bignum));
|
|
2334 }
|
|
2335 else
|
|
2336 {
|
|
2337 if (!bignum_fits_ulong_p (XBIGNUM_DATA (count)))
|
|
2338 args_out_of_range (Qnumber_char_or_marker_p, count);
|
|
2339 bignum_lshift (scratch_bignum2, XBIGNUM_DATA (value),
|
|
2340 bignum_to_ulong (XBIGNUM_DATA (count)));
|
|
2341 }
|
|
2342 return Fcanonicalize_number (make_bignum_bg (scratch_bignum2));
|
|
2343 }
|
|
2344 #else /* !HAVE_BIGNUM */
|
428
|
2345 CHECK_INT_COERCE_CHAR (value);
|
|
2346 CONCHECK_INT (count);
|
|
2347
|
|
2348 return make_int (XINT (count) > 0 ?
|
|
2349 XUINT (value) << XINT (count) :
|
|
2350 XUINT (value) >> -XINT (count));
|
1983
|
2351 #endif /* HAVE_BIGNUM */
|
428
|
2352 }
|
|
2353
|
|
2354 DEFUN ("1+", Fadd1, 1, 1, 0, /*
|
|
2355 Return NUMBER plus one. NUMBER may be a number, character or marker.
|
|
2356 Markers and characters are converted to integers.
|
|
2357 */
|
|
2358 (number))
|
|
2359 {
|
|
2360 retry:
|
|
2361
|
1983
|
2362 if (INTP (number)) return make_integer (XINT (number) + 1);
|
|
2363 if (CHARP (number)) return make_integer (XCHAR (number) + 1);
|
|
2364 if (MARKERP (number)) return make_integer (marker_position (number) + 1);
|
428
|
2365 if (FLOATP (number)) return make_float (XFLOAT_DATA (number) + 1.0);
|
1983
|
2366 #ifdef HAVE_BIGNUM
|
|
2367 if (BIGNUMP (number))
|
|
2368 {
|
|
2369 bignum_set_long (scratch_bignum, 1L);
|
|
2370 bignum_add (scratch_bignum2, XBIGNUM_DATA (number), scratch_bignum);
|
|
2371 return Fcanonicalize_number (make_bignum_bg (scratch_bignum2));
|
|
2372 }
|
|
2373 #endif
|
|
2374 #ifdef HAVE_RATIO
|
|
2375 if (RATIOP (number))
|
|
2376 {
|
|
2377 ratio_set_long (scratch_ratio, 1L);
|
|
2378 ratio_add (scratch_ratio, XRATIO_DATA (number), scratch_ratio);
|
|
2379 /* No need to canonicalize after adding 1 */
|
|
2380 return make_ratio_rt (scratch_ratio);
|
|
2381 }
|
|
2382 #endif
|
|
2383 #ifdef HAVE_BIGFLOAT
|
|
2384 if (BIGFLOATP (number))
|
|
2385 {
|
|
2386 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (number));
|
|
2387 bigfloat_set_long (scratch_bigfloat, 1L);
|
|
2388 bigfloat_add (scratch_bigfloat, XBIGFLOAT_DATA (number),
|
|
2389 scratch_bigfloat);
|
|
2390 return make_bigfloat_bf (scratch_bigfloat);
|
|
2391 }
|
|
2392 #endif
|
428
|
2393
|
|
2394 number = wrong_type_argument (Qnumber_char_or_marker_p, number);
|
|
2395 goto retry;
|
|
2396 }
|
|
2397
|
|
2398 DEFUN ("1-", Fsub1, 1, 1, 0, /*
|
|
2399 Return NUMBER minus one. NUMBER may be a number, character or marker.
|
|
2400 Markers and characters are converted to integers.
|
|
2401 */
|
|
2402 (number))
|
|
2403 {
|
|
2404 retry:
|
|
2405
|
1983
|
2406 if (INTP (number)) return make_integer (XINT (number) - 1);
|
|
2407 if (CHARP (number)) return make_integer (XCHAR (number) - 1);
|
|
2408 if (MARKERP (number)) return make_integer (marker_position (number) - 1);
|
428
|
2409 if (FLOATP (number)) return make_float (XFLOAT_DATA (number) - 1.0);
|
1983
|
2410 #ifdef HAVE_BIGNUM
|
|
2411 if (BIGNUMP (number))
|
|
2412 {
|
|
2413 bignum_set_long (scratch_bignum, 1L);
|
|
2414 bignum_sub (scratch_bignum2, XBIGNUM_DATA (number), scratch_bignum);
|
|
2415 return Fcanonicalize_number (make_bignum_bg (scratch_bignum2));
|
|
2416 }
|
|
2417 #endif
|
|
2418 #ifdef HAVE_RATIO
|
|
2419 if (RATIOP (number))
|
|
2420 {
|
|
2421 ratio_set_long (scratch_ratio, 1L);
|
|
2422 ratio_sub (scratch_ratio, XRATIO_DATA (number), scratch_ratio);
|
|
2423 /* No need to canonicalize after subtracting 1 */
|
|
2424 return make_ratio_rt (scratch_ratio);
|
|
2425 }
|
|
2426 #endif
|
|
2427 #ifdef HAVE_BIGFLOAT
|
|
2428 if (BIGFLOATP (number))
|
|
2429 {
|
|
2430 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (number));
|
|
2431 bigfloat_set_long (scratch_bigfloat, 1L);
|
|
2432 bigfloat_sub (scratch_bigfloat, XBIGFLOAT_DATA (number),
|
|
2433 scratch_bigfloat);
|
|
2434 return make_bigfloat_bf (scratch_bigfloat);
|
|
2435 }
|
|
2436 #endif
|
428
|
2437
|
|
2438 number = wrong_type_argument (Qnumber_char_or_marker_p, number);
|
|
2439 goto retry;
|
|
2440 }
|
|
2441
|
|
2442
|
|
2443 /************************************************************************/
|
|
2444 /* weak lists */
|
|
2445 /************************************************************************/
|
|
2446
|
|
2447 /* A weak list is like a normal list except that elements automatically
|
|
2448 disappear when no longer in use, i.e. when no longer GC-protected.
|
|
2449 The basic idea is that we don't mark the elements during GC, but
|
|
2450 wait for them to be marked elsewhere. If they're not marked, we
|
|
2451 remove them. This is analogous to weak hash tables; see the explanation
|
|
2452 there for more info. */
|
|
2453
|
|
2454 static Lisp_Object Vall_weak_lists; /* Gemarke es nicht!!! */
|
|
2455
|
|
2456 static Lisp_Object encode_weak_list_type (enum weak_list_type type);
|
|
2457
|
|
2458 static Lisp_Object
|
|
2459 mark_weak_list (Lisp_Object obj)
|
|
2460 {
|
|
2461 return Qnil; /* nichts ist gemarkt */
|
|
2462 }
|
|
2463
|
|
2464 static void
|
|
2465 print_weak_list (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
2466 {
|
|
2467 if (print_readably)
|
563
|
2468 printing_unreadable_object ("#<weak-list>");
|
428
|
2469
|
800
|
2470 write_fmt_string_lisp (printcharfun, "#<weak-list %s %S>", 2,
|
|
2471 encode_weak_list_type (XWEAK_LIST (obj)->type),
|
|
2472 XWEAK_LIST (obj)->list);
|
428
|
2473 }
|
|
2474
|
|
2475 static int
|
|
2476 weak_list_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
2477 {
|
|
2478 struct weak_list *w1 = XWEAK_LIST (obj1);
|
|
2479 struct weak_list *w2 = XWEAK_LIST (obj2);
|
|
2480
|
|
2481 return ((w1->type == w2->type) &&
|
|
2482 internal_equal (w1->list, w2->list, depth + 1));
|
|
2483 }
|
|
2484
|
665
|
2485 static Hashcode
|
428
|
2486 weak_list_hash (Lisp_Object obj, int depth)
|
|
2487 {
|
|
2488 struct weak_list *w = XWEAK_LIST (obj);
|
|
2489
|
665
|
2490 return HASH2 ((Hashcode) w->type,
|
428
|
2491 internal_hash (w->list, depth + 1));
|
|
2492 }
|
|
2493
|
|
2494 Lisp_Object
|
|
2495 make_weak_list (enum weak_list_type type)
|
|
2496 {
|
|
2497 Lisp_Object result;
|
|
2498 struct weak_list *wl =
|
|
2499 alloc_lcrecord_type (struct weak_list, &lrecord_weak_list);
|
|
2500
|
|
2501 wl->list = Qnil;
|
|
2502 wl->type = type;
|
793
|
2503 result = wrap_weak_list (wl);
|
428
|
2504 wl->next_weak = Vall_weak_lists;
|
|
2505 Vall_weak_lists = result;
|
|
2506 return result;
|
|
2507 }
|
|
2508
|
1204
|
2509 static const struct memory_description weak_list_description[] = {
|
1598
|
2510 { XD_LISP_OBJECT, offsetof (struct weak_list, list),
|
|
2511 0, 0, XD_FLAG_NO_KKCC },
|
|
2512 { XD_LO_LINK, offsetof (struct weak_list, next_weak),
|
|
2513 0, 0, XD_FLAG_NO_KKCC },
|
428
|
2514 { XD_END }
|
|
2515 };
|
|
2516
|
934
|
2517 DEFINE_LRECORD_IMPLEMENTATION ("weak-list", weak_list,
|
|
2518 1, /*dumpable-flag*/
|
|
2519 mark_weak_list, print_weak_list,
|
|
2520 0, weak_list_equal, weak_list_hash,
|
|
2521 weak_list_description,
|
|
2522 struct weak_list);
|
428
|
2523 /*
|
|
2524 -- we do not mark the list elements (either the elements themselves
|
|
2525 or the cons cells that hold them) in the normal marking phase.
|
|
2526 -- at the end of marking, we go through all weak lists that are
|
|
2527 marked, and mark the cons cells that hold all marked
|
|
2528 objects, and possibly parts of the objects themselves.
|
|
2529 (See alloc.c, "after-mark".)
|
|
2530 -- after that, we prune away all the cons cells that are not marked.
|
|
2531
|
|
2532 WARNING WARNING WARNING WARNING WARNING:
|
|
2533
|
|
2534 The code in the following two functions is *unbelievably* tricky.
|
|
2535 Don't mess with it. You'll be sorry.
|
|
2536
|
|
2537 Linked lists just majorly suck, d'ya know?
|
|
2538 */
|
|
2539
|
|
2540 int
|
|
2541 finish_marking_weak_lists (void)
|
|
2542 {
|
|
2543 Lisp_Object rest;
|
|
2544 int did_mark = 0;
|
|
2545
|
|
2546 for (rest = Vall_weak_lists;
|
|
2547 !NILP (rest);
|
|
2548 rest = XWEAK_LIST (rest)->next_weak)
|
|
2549 {
|
|
2550 Lisp_Object rest2;
|
|
2551 enum weak_list_type type = XWEAK_LIST (rest)->type;
|
|
2552
|
|
2553 if (! marked_p (rest))
|
|
2554 /* The weak list is probably garbage. Ignore it. */
|
|
2555 continue;
|
|
2556
|
|
2557 for (rest2 = XWEAK_LIST (rest)->list;
|
|
2558 /* We need to be trickier since we're inside of GC;
|
|
2559 use CONSP instead of !NILP in case of user-visible
|
|
2560 imperfect lists */
|
|
2561 CONSP (rest2);
|
|
2562 rest2 = XCDR (rest2))
|
|
2563 {
|
|
2564 Lisp_Object elem;
|
|
2565 /* If the element is "marked" (meaning depends on the type
|
|
2566 of weak list), we need to mark the cons containing the
|
|
2567 element, and maybe the element itself (if only some part
|
|
2568 was already marked). */
|
|
2569 int need_to_mark_cons = 0;
|
|
2570 int need_to_mark_elem = 0;
|
|
2571
|
|
2572 /* If a cons is already marked, then its car is already marked
|
|
2573 (either because of an external pointer or because of
|
|
2574 a previous call to this function), and likewise for all
|
|
2575 the rest of the elements in the list, so we can stop now. */
|
|
2576 if (marked_p (rest2))
|
|
2577 break;
|
|
2578
|
|
2579 elem = XCAR (rest2);
|
|
2580
|
|
2581 switch (type)
|
|
2582 {
|
|
2583 case WEAK_LIST_SIMPLE:
|
|
2584 if (marked_p (elem))
|
|
2585 need_to_mark_cons = 1;
|
|
2586 break;
|
|
2587
|
|
2588 case WEAK_LIST_ASSOC:
|
|
2589 if (!CONSP (elem))
|
|
2590 {
|
|
2591 /* just leave bogus elements there */
|
|
2592 need_to_mark_cons = 1;
|
|
2593 need_to_mark_elem = 1;
|
|
2594 }
|
|
2595 else if (marked_p (XCAR (elem)) &&
|
|
2596 marked_p (XCDR (elem)))
|
|
2597 {
|
|
2598 need_to_mark_cons = 1;
|
|
2599 /* We still need to mark elem, because it's
|
|
2600 probably not marked. */
|
|
2601 need_to_mark_elem = 1;
|
|
2602 }
|
|
2603 break;
|
|
2604
|
|
2605 case WEAK_LIST_KEY_ASSOC:
|
|
2606 if (!CONSP (elem))
|
|
2607 {
|
|
2608 /* just leave bogus elements there */
|
|
2609 need_to_mark_cons = 1;
|
|
2610 need_to_mark_elem = 1;
|
|
2611 }
|
|
2612 else if (marked_p (XCAR (elem)))
|
|
2613 {
|
|
2614 need_to_mark_cons = 1;
|
|
2615 /* We still need to mark elem and XCDR (elem);
|
|
2616 marking elem does both */
|
|
2617 need_to_mark_elem = 1;
|
|
2618 }
|
|
2619 break;
|
|
2620
|
|
2621 case WEAK_LIST_VALUE_ASSOC:
|
|
2622 if (!CONSP (elem))
|
|
2623 {
|
|
2624 /* just leave bogus elements there */
|
|
2625 need_to_mark_cons = 1;
|
|
2626 need_to_mark_elem = 1;
|
|
2627 }
|
|
2628 else if (marked_p (XCDR (elem)))
|
|
2629 {
|
|
2630 need_to_mark_cons = 1;
|
|
2631 /* We still need to mark elem and XCAR (elem);
|
|
2632 marking elem does both */
|
|
2633 need_to_mark_elem = 1;
|
|
2634 }
|
|
2635 break;
|
|
2636
|
442
|
2637 case WEAK_LIST_FULL_ASSOC:
|
|
2638 if (!CONSP (elem))
|
|
2639 {
|
|
2640 /* just leave bogus elements there */
|
|
2641 need_to_mark_cons = 1;
|
|
2642 need_to_mark_elem = 1;
|
|
2643 }
|
|
2644 else if (marked_p (XCAR (elem)) ||
|
|
2645 marked_p (XCDR (elem)))
|
|
2646 {
|
|
2647 need_to_mark_cons = 1;
|
|
2648 /* We still need to mark elem and XCAR (elem);
|
|
2649 marking elem does both */
|
|
2650 need_to_mark_elem = 1;
|
|
2651 }
|
|
2652 break;
|
|
2653
|
428
|
2654 default:
|
|
2655 abort ();
|
|
2656 }
|
|
2657
|
|
2658 if (need_to_mark_elem && ! marked_p (elem))
|
|
2659 {
|
1598
|
2660 #ifdef USE_KKCC
|
|
2661 kkcc_gc_stack_push_lisp_object (elem);
|
|
2662 #else /* NOT USE_KKCC */
|
428
|
2663 mark_object (elem);
|
1598
|
2664 #endif /* NOT USE_KKCC */
|
428
|
2665 did_mark = 1;
|
|
2666 }
|
|
2667
|
|
2668 /* We also need to mark the cons that holds the elem or
|
|
2669 assoc-pair. We do *not* want to call (mark_object) here
|
|
2670 because that will mark the entire list; we just want to
|
|
2671 mark the cons itself.
|
|
2672 */
|
|
2673 if (need_to_mark_cons)
|
|
2674 {
|
|
2675 Lisp_Cons *c = XCONS (rest2);
|
|
2676 if (!CONS_MARKED_P (c))
|
|
2677 {
|
|
2678 MARK_CONS (c);
|
|
2679 did_mark = 1;
|
|
2680 }
|
|
2681 }
|
|
2682 }
|
|
2683
|
|
2684 /* In case of imperfect list, need to mark the final cons
|
|
2685 because we're not removing it */
|
|
2686 if (!NILP (rest2) && ! marked_p (rest2))
|
|
2687 {
|
1598
|
2688 #ifdef USE_KKCC
|
|
2689 kkcc_gc_stack_push_lisp_object (rest2);
|
|
2690 #else /* NOT USE_KKCC */
|
428
|
2691 mark_object (rest2);
|
1598
|
2692 #endif /* NOT USE_KKCC */
|
428
|
2693 did_mark = 1;
|
|
2694 }
|
|
2695 }
|
|
2696
|
|
2697 return did_mark;
|
|
2698 }
|
|
2699
|
|
2700 void
|
|
2701 prune_weak_lists (void)
|
|
2702 {
|
|
2703 Lisp_Object rest, prev = Qnil;
|
|
2704
|
|
2705 for (rest = Vall_weak_lists;
|
|
2706 !NILP (rest);
|
|
2707 rest = XWEAK_LIST (rest)->next_weak)
|
|
2708 {
|
|
2709 if (! (marked_p (rest)))
|
|
2710 {
|
|
2711 /* This weak list itself is garbage. Remove it from the list. */
|
|
2712 if (NILP (prev))
|
|
2713 Vall_weak_lists = XWEAK_LIST (rest)->next_weak;
|
|
2714 else
|
|
2715 XWEAK_LIST (prev)->next_weak =
|
|
2716 XWEAK_LIST (rest)->next_weak;
|
|
2717 }
|
|
2718 else
|
|
2719 {
|
|
2720 Lisp_Object rest2, prev2 = Qnil;
|
|
2721 Lisp_Object tortoise;
|
|
2722 int go_tortoise = 0;
|
|
2723
|
|
2724 for (rest2 = XWEAK_LIST (rest)->list, tortoise = rest2;
|
|
2725 /* We need to be trickier since we're inside of GC;
|
|
2726 use CONSP instead of !NILP in case of user-visible
|
|
2727 imperfect lists */
|
|
2728 CONSP (rest2);)
|
|
2729 {
|
|
2730 /* It suffices to check the cons for marking,
|
|
2731 regardless of the type of weak list:
|
|
2732
|
|
2733 -- if the cons is pointed to somewhere else,
|
|
2734 then it should stay around and will be marked.
|
|
2735 -- otherwise, if it should stay around, it will
|
|
2736 have been marked in finish_marking_weak_lists().
|
|
2737 -- otherwise, it's not marked and should disappear.
|
|
2738 */
|
|
2739 if (! marked_p (rest2))
|
|
2740 {
|
|
2741 /* bye bye :-( */
|
|
2742 if (NILP (prev2))
|
|
2743 XWEAK_LIST (rest)->list = XCDR (rest2);
|
|
2744 else
|
|
2745 XCDR (prev2) = XCDR (rest2);
|
|
2746 rest2 = XCDR (rest2);
|
|
2747 /* Ouch. Circularity checking is even trickier
|
|
2748 than I thought. When we cut out a link
|
|
2749 like this, we can't advance the turtle or
|
|
2750 it'll catch up to us. Imagine that we're
|
|
2751 standing on floor tiles and moving forward --
|
|
2752 what we just did here is as if the floor
|
|
2753 tile under us just disappeared and all the
|
|
2754 ones ahead of us slid one tile towards us.
|
|
2755 In other words, we didn't move at all;
|
|
2756 if the tortoise was one step behind us
|
|
2757 previously, it still is, and therefore
|
|
2758 it must not move. */
|
|
2759 }
|
|
2760 else
|
|
2761 {
|
|
2762 prev2 = rest2;
|
|
2763
|
|
2764 /* Implementing circularity checking is trickier here
|
|
2765 than in other places because we have to guarantee
|
|
2766 that we've processed all elements before exiting
|
|
2767 due to a circularity. (In most places, an error
|
|
2768 is issued upon encountering a circularity, so it
|
|
2769 doesn't really matter if all elements are processed.)
|
|
2770 The idea is that we process along with the hare
|
|
2771 rather than the tortoise. If at any point in
|
|
2772 our forward process we encounter the tortoise,
|
|
2773 we must have already visited the spot, so we exit.
|
|
2774 (If we process with the tortoise, we can fail to
|
|
2775 process cases where a cons points to itself, or
|
|
2776 where cons A points to cons B, which points to
|
|
2777 cons A.) */
|
|
2778
|
|
2779 rest2 = XCDR (rest2);
|
|
2780 if (go_tortoise)
|
|
2781 tortoise = XCDR (tortoise);
|
|
2782 go_tortoise = !go_tortoise;
|
|
2783 if (EQ (rest2, tortoise))
|
|
2784 break;
|
|
2785 }
|
|
2786 }
|
|
2787
|
|
2788 prev = rest;
|
|
2789 }
|
|
2790 }
|
|
2791 }
|
|
2792
|
|
2793 static enum weak_list_type
|
|
2794 decode_weak_list_type (Lisp_Object symbol)
|
|
2795 {
|
|
2796 CHECK_SYMBOL (symbol);
|
|
2797 if (EQ (symbol, Qsimple)) return WEAK_LIST_SIMPLE;
|
|
2798 if (EQ (symbol, Qassoc)) return WEAK_LIST_ASSOC;
|
|
2799 if (EQ (symbol, Qold_assoc)) return WEAK_LIST_ASSOC; /* EBOLA ALERT! */
|
|
2800 if (EQ (symbol, Qkey_assoc)) return WEAK_LIST_KEY_ASSOC;
|
|
2801 if (EQ (symbol, Qvalue_assoc)) return WEAK_LIST_VALUE_ASSOC;
|
442
|
2802 if (EQ (symbol, Qfull_assoc)) return WEAK_LIST_FULL_ASSOC;
|
428
|
2803
|
563
|
2804 invalid_constant ("Invalid weak list type", symbol);
|
1204
|
2805 RETURN_NOT_REACHED (WEAK_LIST_SIMPLE);
|
428
|
2806 }
|
|
2807
|
|
2808 static Lisp_Object
|
|
2809 encode_weak_list_type (enum weak_list_type type)
|
|
2810 {
|
|
2811 switch (type)
|
|
2812 {
|
|
2813 case WEAK_LIST_SIMPLE: return Qsimple;
|
|
2814 case WEAK_LIST_ASSOC: return Qassoc;
|
|
2815 case WEAK_LIST_KEY_ASSOC: return Qkey_assoc;
|
|
2816 case WEAK_LIST_VALUE_ASSOC: return Qvalue_assoc;
|
442
|
2817 case WEAK_LIST_FULL_ASSOC: return Qfull_assoc;
|
428
|
2818 default:
|
|
2819 abort ();
|
|
2820 }
|
|
2821
|
801
|
2822 return Qnil; /* not (usually) reached */
|
428
|
2823 }
|
|
2824
|
|
2825 DEFUN ("weak-list-p", Fweak_list_p, 1, 1, 0, /*
|
|
2826 Return non-nil if OBJECT is a weak list.
|
|
2827 */
|
|
2828 (object))
|
|
2829 {
|
|
2830 return WEAK_LISTP (object) ? Qt : Qnil;
|
|
2831 }
|
|
2832
|
|
2833 DEFUN ("make-weak-list", Fmake_weak_list, 0, 1, 0, /*
|
|
2834 Return a new weak list object of type TYPE.
|
|
2835 A weak list object is an object that contains a list. This list behaves
|
|
2836 like any other list except that its elements do not count towards
|
456
|
2837 garbage collection -- if the only pointer to an object is inside a weak
|
428
|
2838 list (other than pointers in similar objects such as weak hash tables),
|
|
2839 the object is garbage collected and automatically removed from the list.
|
|
2840 This is used internally, for example, to manage the list holding the
|
|
2841 children of an extent -- an extent that is unused but has a parent will
|
|
2842 still be reclaimed, and will automatically be removed from its parent's
|
|
2843 list of children.
|
|
2844
|
|
2845 Optional argument TYPE specifies the type of the weak list, and defaults
|
|
2846 to `simple'. Recognized types are
|
|
2847
|
|
2848 `simple' Objects in the list disappear if not pointed to.
|
|
2849 `assoc' Objects in the list disappear if they are conses
|
|
2850 and either the car or the cdr of the cons is not
|
|
2851 pointed to.
|
|
2852 `key-assoc' Objects in the list disappear if they are conses
|
|
2853 and the car is not pointed to.
|
|
2854 `value-assoc' Objects in the list disappear if they are conses
|
|
2855 and the cdr is not pointed to.
|
442
|
2856 `full-assoc' Objects in the list disappear if they are conses
|
|
2857 and neither the car nor the cdr is pointed to.
|
428
|
2858 */
|
|
2859 (type))
|
|
2860 {
|
|
2861 if (NILP (type))
|
|
2862 type = Qsimple;
|
|
2863
|
|
2864 return make_weak_list (decode_weak_list_type (type));
|
|
2865 }
|
|
2866
|
|
2867 DEFUN ("weak-list-type", Fweak_list_type, 1, 1, 0, /*
|
|
2868 Return the type of the given weak-list object.
|
|
2869 */
|
|
2870 (weak))
|
|
2871 {
|
|
2872 CHECK_WEAK_LIST (weak);
|
|
2873 return encode_weak_list_type (XWEAK_LIST (weak)->type);
|
|
2874 }
|
|
2875
|
|
2876 DEFUN ("weak-list-list", Fweak_list_list, 1, 1, 0, /*
|
|
2877 Return the list contained in a weak-list object.
|
|
2878 */
|
|
2879 (weak))
|
|
2880 {
|
|
2881 CHECK_WEAK_LIST (weak);
|
|
2882 return XWEAK_LIST_LIST (weak);
|
|
2883 }
|
|
2884
|
|
2885 DEFUN ("set-weak-list-list", Fset_weak_list_list, 2, 2, 0, /*
|
|
2886 Change the list contained in a weak-list object.
|
|
2887 */
|
|
2888 (weak, new_list))
|
|
2889 {
|
|
2890 CHECK_WEAK_LIST (weak);
|
|
2891 XWEAK_LIST_LIST (weak) = new_list;
|
|
2892 return new_list;
|
|
2893 }
|
|
2894
|
888
|
2895
|
858
|
2896 /************************************************************************/
|
|
2897 /* weak boxes */
|
|
2898 /************************************************************************/
|
|
2899
|
|
2900 static Lisp_Object Vall_weak_boxes; /* Gemarke es niemals ever!!! */
|
|
2901
|
|
2902 void
|
|
2903 prune_weak_boxes (void)
|
|
2904 {
|
|
2905 Lisp_Object rest, prev = Qnil;
|
888
|
2906 int removep = 0;
|
858
|
2907
|
|
2908 for (rest = Vall_weak_boxes;
|
|
2909 !NILP(rest);
|
|
2910 rest = XWEAK_BOX (rest)->next_weak_box)
|
|
2911 {
|
|
2912 if (! (marked_p (rest)))
|
888
|
2913 /* This weak box itself is garbage. */
|
|
2914 removep = 1;
|
|
2915
|
|
2916 if (! marked_p (XWEAK_BOX (rest)->value))
|
|
2917 {
|
|
2918 XSET_WEAK_BOX (rest, Qnil);
|
|
2919 removep = 1;
|
|
2920 }
|
|
2921
|
|
2922 if (removep)
|
|
2923 {
|
|
2924 /* Remove weak box from list. */
|
|
2925 if (NILP (prev))
|
|
2926 Vall_weak_boxes = XWEAK_BOX (rest)->next_weak_box;
|
|
2927 else
|
|
2928 XWEAK_BOX (prev)->next_weak_box = XWEAK_BOX (rest)->next_weak_box;
|
|
2929 removep = 0;
|
|
2930 }
|
|
2931 else
|
|
2932 prev = rest;
|
858
|
2933 }
|
|
2934 }
|
|
2935
|
|
2936 static Lisp_Object
|
888
|
2937 mark_weak_box (Lisp_Object obj)
|
858
|
2938 {
|
|
2939 return Qnil;
|
|
2940 }
|
|
2941
|
|
2942 static void
|
|
2943 print_weak_box (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
2944 {
|
|
2945 if (print_readably)
|
|
2946 printing_unreadable_object ("#<weak_box>");
|
|
2947 write_fmt_string (printcharfun, "#<weak_box>");
|
|
2948 }
|
|
2949
|
|
2950 static int
|
|
2951 weak_box_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
2952 {
|
888
|
2953 struct weak_box *wb1 = XWEAK_BOX (obj1);
|
|
2954 struct weak_box *wb2 = XWEAK_BOX (obj2);
|
858
|
2955
|
888
|
2956 return (internal_equal (wb1->value, wb2->value, depth + 1));
|
858
|
2957 }
|
|
2958
|
|
2959 static Hashcode
|
|
2960 weak_box_hash (Lisp_Object obj, int depth)
|
|
2961 {
|
888
|
2962 struct weak_box *wb = XWEAK_BOX (obj);
|
858
|
2963
|
888
|
2964 return internal_hash (wb->value, depth + 1);
|
858
|
2965 }
|
|
2966
|
|
2967 Lisp_Object
|
|
2968 make_weak_box (Lisp_Object value)
|
|
2969 {
|
|
2970 Lisp_Object result;
|
|
2971
|
|
2972 struct weak_box *wb =
|
|
2973 alloc_lcrecord_type (struct weak_box, &lrecord_weak_box);
|
|
2974
|
|
2975 wb->value = value;
|
|
2976 result = wrap_weak_box (wb);
|
|
2977 wb->next_weak_box = Vall_weak_boxes;
|
|
2978 Vall_weak_boxes = result;
|
|
2979 return result;
|
|
2980 }
|
|
2981
|
1204
|
2982 static const struct memory_description weak_box_description[] = {
|
858
|
2983 { XD_LO_LINK, offsetof (struct weak_box, value) },
|
888
|
2984 { XD_END}
|
858
|
2985 };
|
|
2986
|
934
|
2987 DEFINE_LRECORD_IMPLEMENTATION ("weak_box", weak_box,
|
|
2988 0, /*dumpable-flag*/
|
|
2989 mark_weak_box, print_weak_box,
|
|
2990 0, weak_box_equal, weak_box_hash,
|
|
2991 weak_box_description,
|
|
2992 struct weak_box);
|
858
|
2993
|
|
2994 DEFUN ("make-weak-box", Fmake_weak_box, 1, 1, 0, /*
|
|
2995 Return a new weak box from value CONTENTS.
|
|
2996 The weak box is a reference to CONTENTS which may be extracted with
|
|
2997 `weak-box-ref'. However, the weak box does not contribute to the
|
|
2998 reachability of CONTENTS. When CONTENTS is garbage-collected,
|
|
2999 `weak-box-ref' will return NIL.
|
|
3000 */
|
|
3001 (value))
|
|
3002 {
|
|
3003 return make_weak_box(value);
|
|
3004 }
|
|
3005
|
|
3006 DEFUN ("weak-box-ref", Fweak_box_ref, 1, 1, 0, /*
|
|
3007 Return the contents of weak box WEAK-BOX.
|
|
3008 If the contents have been GCed, return NIL.
|
|
3009 */
|
888
|
3010 (wb))
|
858
|
3011 {
|
888
|
3012 return XWEAK_BOX (wb)->value;
|
858
|
3013 }
|
|
3014
|
|
3015 DEFUN ("weak-box-p", Fweak_boxp, 1, 1, 0, /*
|
|
3016 Return non-nil if OBJECT is a weak box.
|
|
3017 */
|
|
3018 (object))
|
|
3019 {
|
|
3020 return WEAK_BOXP (object) ? Qt : Qnil;
|
|
3021 }
|
|
3022
|
888
|
3023 /************************************************************************/
|
|
3024 /* ephemerons */
|
|
3025 /************************************************************************/
|
|
3026
|
993
|
3027 /* The concept of ephemerons is due to:
|
|
3028 * Barry Hayes: Ephemerons: A New Finalization Mechanism. OOPSLA 1997: 176-183
|
|
3029 * The original idea is due to George Bosworth of Digitalk, Inc.
|
|
3030 *
|
|
3031 * For a discussion of finalization and weakness that also reviews
|
|
3032 * ephemerons, refer to:
|
|
3033 * Simon Peyton Jones, Simon Marlow, Conal Elliot:
|
|
3034 * Stretching the storage manager
|
|
3035 * Implementation of Functional Languages, 1999
|
|
3036 */
|
|
3037
|
888
|
3038 static Lisp_Object Vall_ephemerons; /* Gemarke es niemals ever!!! */
|
1590
|
3039 static Lisp_Object Vnew_all_ephemerons;
|
888
|
3040 static Lisp_Object Vfinalize_list;
|
|
3041
|
1590
|
3042 void
|
|
3043 init_marking_ephemerons(void)
|
|
3044 {
|
|
3045 Vnew_all_ephemerons = Qnil;
|
|
3046 }
|
|
3047
|
|
3048 /* Move all live ephemerons with live keys over to
|
|
3049 * Vnew_all_ephemerons, marking the values and finalizers along the
|
|
3050 * way. */
|
|
3051
|
|
3052 int
|
|
3053 continue_marking_ephemerons(void)
|
|
3054 {
|
|
3055 Lisp_Object rest = Vall_ephemerons, next, prev = Qnil;
|
|
3056 int did_mark = 0;
|
|
3057
|
|
3058 while (!NILP (rest))
|
|
3059 {
|
|
3060 next = XEPHEMERON_NEXT (rest);
|
|
3061
|
|
3062 if (marked_p (rest))
|
|
3063 {
|
|
3064 MARK_CONS (XCONS (XEPHEMERON (rest)->cons_chain));
|
|
3065 if (marked_p (XEPHEMERON (rest)->key))
|
|
3066 {
|
1598
|
3067 #ifdef USE_KKCC
|
|
3068 kkcc_gc_stack_push_lisp_object
|
|
3069 (XCAR (XEPHEMERON (rest)->cons_chain));
|
|
3070 #else /* NOT USE_KKCC */
|
1590
|
3071 mark_object (XCAR (XEPHEMERON (rest)->cons_chain));
|
1598
|
3072 #endif /* NOT USE_KKCC */
|
1590
|
3073 did_mark = 1;
|
|
3074 XSET_EPHEMERON_NEXT (rest, Vnew_all_ephemerons);
|
|
3075 Vnew_all_ephemerons = rest;
|
|
3076 if (NILP (prev))
|
|
3077 Vall_ephemerons = next;
|
|
3078 else
|
|
3079 XSET_EPHEMERON_NEXT (prev, next);
|
|
3080 }
|
|
3081 else
|
|
3082 prev = rest;
|
|
3083 }
|
|
3084 else
|
|
3085 prev = rest;
|
|
3086
|
|
3087 rest = next;
|
|
3088 }
|
|
3089
|
|
3090 return did_mark;
|
|
3091 }
|
|
3092
|
|
3093 /* At this point, everything that's in Vall_ephemerons is dead.
|
|
3094 * Well, almost: we still need to run the finalizers, so we need to
|
|
3095 * resurrect them.
|
|
3096 */
|
|
3097
|
888
|
3098 int
|
|
3099 finish_marking_ephemerons(void)
|
|
3100 {
|
1590
|
3101 Lisp_Object rest = Vall_ephemerons, next, prev = Qnil;
|
888
|
3102 int did_mark = 0;
|
|
3103
|
|
3104 while (! NILP (rest))
|
|
3105 {
|
|
3106 next = XEPHEMERON_NEXT (rest);
|
|
3107
|
|
3108 if (marked_p (rest))
|
1590
|
3109 /* The ephemeron itself is live, but its key is garbage */
|
888
|
3110 {
|
1590
|
3111 /* tombstone */
|
|
3112 XSET_EPHEMERON_VALUE (rest, Qnil);
|
|
3113
|
|
3114 if (! NILP (XEPHEMERON_FINALIZER (rest)))
|
888
|
3115 {
|
1590
|
3116 MARK_CONS (XCONS (XEPHEMERON (rest)->cons_chain));
|
1598
|
3117 #ifdef USE_KKCC
|
|
3118 kkcc_gc_stack_push_lisp_object
|
|
3119 (XCAR (XEPHEMERON (rest)->cons_chain));
|
|
3120 #else /* NOT USE_KKCC */
|
1590
|
3121 mark_object (XCAR (XEPHEMERON (rest)->cons_chain));
|
1598
|
3122 #endif /* NOT USE_KKCC */
|
1590
|
3123
|
|
3124 /* Register the finalizer */
|
|
3125 XSET_EPHEMERON_NEXT (rest, Vfinalize_list);
|
|
3126 Vfinalize_list = XEPHEMERON (rest)->cons_chain;
|
|
3127 did_mark = 1;
|
888
|
3128 }
|
|
3129
|
|
3130 /* Remove it from the list. */
|
|
3131 if (NILP (prev))
|
|
3132 Vall_ephemerons = next;
|
|
3133 else
|
|
3134 XSET_EPHEMERON_NEXT (prev, next);
|
|
3135 }
|
|
3136 else
|
|
3137 prev = rest;
|
|
3138
|
|
3139 rest = next;
|
|
3140 }
|
1590
|
3141
|
|
3142 return did_mark;
|
|
3143 }
|
|
3144
|
|
3145 void
|
|
3146 prune_ephemerons(void)
|
|
3147 {
|
|
3148 Vall_ephemerons = Vnew_all_ephemerons;
|
888
|
3149 }
|
|
3150
|
|
3151 Lisp_Object
|
|
3152 zap_finalize_list(void)
|
|
3153 {
|
|
3154 Lisp_Object finalizers = Vfinalize_list;
|
|
3155
|
|
3156 Vfinalize_list = Qnil;
|
|
3157
|
|
3158 return finalizers;
|
|
3159 }
|
|
3160
|
|
3161 static Lisp_Object
|
|
3162 mark_ephemeron (Lisp_Object obj)
|
|
3163 {
|
|
3164 return Qnil;
|
|
3165 }
|
|
3166
|
|
3167 static void
|
|
3168 print_ephemeron (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
3169 {
|
|
3170 if (print_readably)
|
|
3171 printing_unreadable_object ("#<ephemeron>");
|
|
3172 write_fmt_string (printcharfun, "#<ephemeron>");
|
|
3173 }
|
|
3174
|
|
3175 static int
|
|
3176 ephemeron_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
3177 {
|
|
3178 return
|
|
3179 internal_equal (XEPHEMERON_REF (obj1), XEPHEMERON_REF(obj2), depth + 1);
|
|
3180 }
|
|
3181
|
|
3182 static Hashcode
|
|
3183 ephemeron_hash(Lisp_Object obj, int depth)
|
|
3184 {
|
|
3185 return internal_hash (XEPHEMERON_REF (obj), depth + 1);
|
|
3186 }
|
|
3187
|
|
3188 Lisp_Object
|
|
3189 make_ephemeron(Lisp_Object key, Lisp_Object value, Lisp_Object finalizer)
|
|
3190 {
|
|
3191 Lisp_Object result, temp = Qnil;
|
|
3192 struct gcpro gcpro1, gcpro2;
|
|
3193
|
|
3194 struct ephemeron *eph =
|
|
3195 alloc_lcrecord_type (struct ephemeron, &lrecord_ephemeron);
|
|
3196
|
|
3197 eph->key = Qnil;
|
|
3198 eph->cons_chain = Qnil;
|
|
3199 eph->value = Qnil;
|
|
3200
|
|
3201 result = wrap_ephemeron(eph);
|
|
3202 GCPRO2 (result, temp);
|
|
3203
|
|
3204 eph->key = key;
|
|
3205 temp = Fcons(value, finalizer);
|
|
3206 eph->cons_chain = Fcons(temp, Vall_ephemerons);
|
|
3207 eph->value = value;
|
|
3208
|
|
3209 Vall_ephemerons = result;
|
|
3210
|
|
3211 UNGCPRO;
|
|
3212 return result;
|
|
3213 }
|
|
3214
|
1598
|
3215 /* Ephemerons are special cases in the KKCC mark algorithm, so nothing
|
|
3216 is marked here. */
|
1204
|
3217 static const struct memory_description ephemeron_description[] = {
|
|
3218 { XD_LISP_OBJECT, offsetof(struct ephemeron, key),
|
|
3219 0, 0, XD_FLAG_NO_KKCC },
|
|
3220 { XD_LISP_OBJECT, offsetof(struct ephemeron, cons_chain),
|
|
3221 0, 0, XD_FLAG_NO_KKCC },
|
|
3222 { XD_LISP_OBJECT, offsetof(struct ephemeron, value),
|
|
3223 0, 0, XD_FLAG_NO_KKCC },
|
888
|
3224 { XD_END }
|
|
3225 };
|
|
3226
|
934
|
3227 DEFINE_LRECORD_IMPLEMENTATION ("ephemeron", ephemeron,
|
|
3228 0, /*dumpable-flag*/
|
|
3229 mark_ephemeron, print_ephemeron,
|
|
3230 0, ephemeron_equal, ephemeron_hash,
|
|
3231 ephemeron_description,
|
|
3232 struct ephemeron);
|
888
|
3233
|
|
3234 DEFUN ("make-ephemeron", Fmake_ephemeron, 2, 3, 0, /*
|
1590
|
3235 Return a new ephemeron with key KEY, value VALUE, and finalizer FINALIZER.
|
|
3236 The ephemeron is a reference to VALUE which may be extracted with
|
|
3237 `ephemeron-ref'. VALUE is only reachable through the ephemeron as
|
888
|
3238 long as KEY is reachable; the ephemeron does not contribute to the
|
|
3239 reachability of KEY. When KEY becomes unreachable while the ephemeron
|
1590
|
3240 itself is still reachable, VALUE is queued for finalization: FINALIZER
|
|
3241 will possibly be called on VALUE some time in the future. Moreover,
|
888
|
3242 future calls to `ephemeron-ref' will return NIL.
|
|
3243 */
|
|
3244 (key, value, finalizer))
|
|
3245 {
|
|
3246 return make_ephemeron(key, value, finalizer);
|
|
3247 }
|
|
3248
|
|
3249 DEFUN ("ephemeron-ref", Fephemeron_ref, 1, 1, 0, /*
|
|
3250 Return the contents of ephemeron EPHEMERON.
|
|
3251 If the contents have been GCed, return NIL.
|
|
3252 */
|
|
3253 (eph))
|
|
3254 {
|
|
3255 return XEPHEMERON_REF (eph);
|
|
3256 }
|
|
3257
|
|
3258 DEFUN ("ephemeron-p", Fephemeronp, 1, 1, 0, /*
|
|
3259 Return non-nil if OBJECT is an ephemeron.
|
|
3260 */
|
|
3261 (object))
|
|
3262 {
|
|
3263 return EPHEMERONP (object) ? Qt : Qnil;
|
|
3264 }
|
428
|
3265
|
|
3266 /************************************************************************/
|
|
3267 /* initialization */
|
|
3268 /************************************************************************/
|
|
3269
|
|
3270 static SIGTYPE
|
|
3271 arith_error (int signo)
|
|
3272 {
|
|
3273 EMACS_REESTABLISH_SIGNAL (signo, arith_error);
|
|
3274 EMACS_UNBLOCK_SIGNAL (signo);
|
563
|
3275 signal_error (Qarith_error, 0, Qunbound);
|
428
|
3276 }
|
|
3277
|
|
3278 void
|
|
3279 init_data_very_early (void)
|
|
3280 {
|
|
3281 /* Don't do this if just dumping out.
|
|
3282 We don't want to call `signal' in this case
|
|
3283 so that we don't have trouble with dumping
|
|
3284 signal-delivering routines in an inconsistent state. */
|
|
3285 if (!initialized)
|
|
3286 return;
|
613
|
3287 EMACS_SIGNAL (SIGFPE, arith_error);
|
428
|
3288 #ifdef uts
|
613
|
3289 EMACS_SIGNAL (SIGEMT, arith_error);
|
428
|
3290 #endif /* uts */
|
|
3291 }
|
|
3292
|
|
3293 void
|
|
3294 init_errors_once_early (void)
|
|
3295 {
|
442
|
3296 DEFSYMBOL (Qerror_conditions);
|
|
3297 DEFSYMBOL (Qerror_message);
|
428
|
3298
|
|
3299 /* We declare the errors here because some other deferrors depend
|
|
3300 on some of the errors below. */
|
|
3301
|
|
3302 /* ERROR is used as a signaler for random errors for which nothing
|
|
3303 else is right */
|
|
3304
|
442
|
3305 DEFERROR (Qerror, "error", Qnil);
|
|
3306 DEFERROR_STANDARD (Qquit, Qnil);
|
428
|
3307
|
563
|
3308 DEFERROR_STANDARD (Qinvalid_argument, Qerror);
|
|
3309
|
|
3310 DEFERROR_STANDARD (Qsyntax_error, Qinvalid_argument);
|
442
|
3311 DEFERROR_STANDARD (Qinvalid_read_syntax, Qsyntax_error);
|
563
|
3312 DEFERROR_STANDARD (Qstructure_formation_error, Qsyntax_error);
|
|
3313 DEFERROR_STANDARD (Qlist_formation_error, Qstructure_formation_error);
|
442
|
3314 DEFERROR_STANDARD (Qmalformed_list, Qlist_formation_error);
|
|
3315 DEFERROR_STANDARD (Qmalformed_property_list, Qmalformed_list);
|
|
3316 DEFERROR_STANDARD (Qcircular_list, Qlist_formation_error);
|
|
3317 DEFERROR_STANDARD (Qcircular_property_list, Qcircular_list);
|
428
|
3318
|
442
|
3319 DEFERROR_STANDARD (Qwrong_type_argument, Qinvalid_argument);
|
|
3320 DEFERROR_STANDARD (Qargs_out_of_range, Qinvalid_argument);
|
|
3321 DEFERROR_STANDARD (Qwrong_number_of_arguments, Qinvalid_argument);
|
|
3322 DEFERROR_STANDARD (Qinvalid_function, Qinvalid_argument);
|
563
|
3323 DEFERROR_STANDARD (Qinvalid_constant, Qinvalid_argument);
|
442
|
3324 DEFERROR (Qno_catch, "No catch for tag", Qinvalid_argument);
|
|
3325
|
563
|
3326 DEFERROR_STANDARD (Qinvalid_state, Qerror);
|
442
|
3327 DEFERROR (Qvoid_function, "Symbol's function definition is void",
|
|
3328 Qinvalid_state);
|
|
3329 DEFERROR (Qcyclic_function_indirection,
|
|
3330 "Symbol's chain of function indirections contains a loop",
|
|
3331 Qinvalid_state);
|
|
3332 DEFERROR (Qvoid_variable, "Symbol's value as variable is void",
|
|
3333 Qinvalid_state);
|
|
3334 DEFERROR (Qcyclic_variable_indirection,
|
|
3335 "Symbol's chain of variable indirections contains a loop",
|
|
3336 Qinvalid_state);
|
563
|
3337 DEFERROR_STANDARD (Qstack_overflow, Qinvalid_state);
|
|
3338 DEFERROR_STANDARD (Qinternal_error, Qinvalid_state);
|
|
3339 DEFERROR_STANDARD (Qout_of_memory, Qinvalid_state);
|
428
|
3340
|
563
|
3341 DEFERROR_STANDARD (Qinvalid_operation, Qerror);
|
|
3342 DEFERROR_STANDARD (Qinvalid_change, Qinvalid_operation);
|
442
|
3343 DEFERROR (Qsetting_constant, "Attempt to set a constant symbol",
|
|
3344 Qinvalid_change);
|
563
|
3345 DEFERROR_STANDARD (Qprinting_unreadable_object, Qinvalid_operation);
|
|
3346 DEFERROR (Qunimplemented, "Feature not yet implemented", Qinvalid_operation);
|
442
|
3347
|
563
|
3348 DEFERROR_STANDARD (Qediting_error, Qinvalid_operation);
|
442
|
3349 DEFERROR_STANDARD (Qbeginning_of_buffer, Qediting_error);
|
|
3350 DEFERROR_STANDARD (Qend_of_buffer, Qediting_error);
|
|
3351 DEFERROR (Qbuffer_read_only, "Buffer is read-only", Qediting_error);
|
|
3352
|
|
3353 DEFERROR (Qio_error, "IO Error", Qinvalid_operation);
|
563
|
3354 DEFERROR_STANDARD (Qfile_error, Qio_error);
|
|
3355 DEFERROR (Qend_of_file, "End of file or stream", Qfile_error);
|
|
3356 DEFERROR_STANDARD (Qconversion_error, Qio_error);
|
580
|
3357 DEFERROR_STANDARD (Qtext_conversion_error, Qconversion_error);
|
442
|
3358
|
|
3359 DEFERROR (Qarith_error, "Arithmetic error", Qinvalid_operation);
|
|
3360 DEFERROR (Qrange_error, "Arithmetic range error", Qarith_error);
|
|
3361 DEFERROR (Qdomain_error, "Arithmetic domain error", Qarith_error);
|
|
3362 DEFERROR (Qsingularity_error, "Arithmetic singularity error", Qdomain_error);
|
|
3363 DEFERROR (Qoverflow_error, "Arithmetic overflow error", Qdomain_error);
|
|
3364 DEFERROR (Qunderflow_error, "Arithmetic underflow error", Qdomain_error);
|
428
|
3365 }
|
|
3366
|
|
3367 void
|
|
3368 syms_of_data (void)
|
|
3369 {
|
442
|
3370 INIT_LRECORD_IMPLEMENTATION (weak_list);
|
888
|
3371 INIT_LRECORD_IMPLEMENTATION (ephemeron);
|
858
|
3372 INIT_LRECORD_IMPLEMENTATION (weak_box);
|
442
|
3373
|
|
3374 DEFSYMBOL (Qquote);
|
|
3375 DEFSYMBOL (Qlambda);
|
|
3376 DEFSYMBOL (Qlistp);
|
|
3377 DEFSYMBOL (Qtrue_list_p);
|
|
3378 DEFSYMBOL (Qconsp);
|
|
3379 DEFSYMBOL (Qsubrp);
|
|
3380 DEFSYMBOL (Qsymbolp);
|
|
3381 DEFSYMBOL (Qintegerp);
|
|
3382 DEFSYMBOL (Qcharacterp);
|
|
3383 DEFSYMBOL (Qnatnump);
|
1983
|
3384 DEFSYMBOL (Qnonnegativep);
|
442
|
3385 DEFSYMBOL (Qstringp);
|
|
3386 DEFSYMBOL (Qarrayp);
|
|
3387 DEFSYMBOL (Qsequencep);
|
|
3388 DEFSYMBOL (Qbufferp);
|
|
3389 DEFSYMBOL (Qbitp);
|
|
3390 DEFSYMBOL_MULTIWORD_PREDICATE (Qbit_vectorp);
|
|
3391 DEFSYMBOL (Qvectorp);
|
|
3392 DEFSYMBOL (Qchar_or_string_p);
|
|
3393 DEFSYMBOL (Qmarkerp);
|
|
3394 DEFSYMBOL (Qinteger_or_marker_p);
|
|
3395 DEFSYMBOL (Qinteger_or_char_p);
|
|
3396 DEFSYMBOL (Qinteger_char_or_marker_p);
|
|
3397 DEFSYMBOL (Qnumberp);
|
|
3398 DEFSYMBOL (Qnumber_char_or_marker_p);
|
|
3399 DEFSYMBOL (Qcdr);
|
563
|
3400 DEFSYMBOL (Qerror_lacks_explanatory_string);
|
442
|
3401 DEFSYMBOL_MULTIWORD_PREDICATE (Qweak_listp);
|
|
3402 DEFSYMBOL (Qfloatp);
|
428
|
3403
|
|
3404 DEFSUBR (Fwrong_type_argument);
|
|
3405
|
1983
|
3406 #ifdef HAVE_RATIO
|
|
3407 DEFSUBR (Fdiv);
|
|
3408 #endif
|
428
|
3409 DEFSUBR (Feq);
|
|
3410 DEFSUBR (Fold_eq);
|
|
3411 DEFSUBR (Fnull);
|
|
3412 Ffset (intern ("not"), intern ("null"));
|
|
3413 DEFSUBR (Flistp);
|
|
3414 DEFSUBR (Fnlistp);
|
|
3415 DEFSUBR (Ftrue_list_p);
|
|
3416 DEFSUBR (Fconsp);
|
|
3417 DEFSUBR (Fatom);
|
|
3418 DEFSUBR (Fchar_or_string_p);
|
|
3419 DEFSUBR (Fcharacterp);
|
|
3420 DEFSUBR (Fchar_int_p);
|
|
3421 DEFSUBR (Fchar_to_int);
|
|
3422 DEFSUBR (Fint_to_char);
|
|
3423 DEFSUBR (Fchar_or_char_int_p);
|
1983
|
3424 #ifdef HAVE_BIGNUM
|
|
3425 DEFSUBR (Ffixnump);
|
|
3426 #else
|
428
|
3427 DEFSUBR (Fintegerp);
|
1983
|
3428 #endif
|
428
|
3429 DEFSUBR (Finteger_or_marker_p);
|
|
3430 DEFSUBR (Finteger_or_char_p);
|
|
3431 DEFSUBR (Finteger_char_or_marker_p);
|
|
3432 DEFSUBR (Fnumberp);
|
|
3433 DEFSUBR (Fnumber_or_marker_p);
|
|
3434 DEFSUBR (Fnumber_char_or_marker_p);
|
|
3435 DEFSUBR (Ffloatp);
|
|
3436 DEFSUBR (Fnatnump);
|
1983
|
3437 DEFSUBR (Fnonnegativep);
|
428
|
3438 DEFSUBR (Fsymbolp);
|
|
3439 DEFSUBR (Fkeywordp);
|
|
3440 DEFSUBR (Fstringp);
|
|
3441 DEFSUBR (Fvectorp);
|
|
3442 DEFSUBR (Fbitp);
|
|
3443 DEFSUBR (Fbit_vector_p);
|
|
3444 DEFSUBR (Farrayp);
|
|
3445 DEFSUBR (Fsequencep);
|
|
3446 DEFSUBR (Fmarkerp);
|
|
3447 DEFSUBR (Fsubrp);
|
|
3448 DEFSUBR (Fsubr_min_args);
|
|
3449 DEFSUBR (Fsubr_max_args);
|
|
3450 DEFSUBR (Fsubr_interactive);
|
|
3451 DEFSUBR (Ftype_of);
|
|
3452 DEFSUBR (Fcar);
|
|
3453 DEFSUBR (Fcdr);
|
|
3454 DEFSUBR (Fcar_safe);
|
|
3455 DEFSUBR (Fcdr_safe);
|
|
3456 DEFSUBR (Fsetcar);
|
|
3457 DEFSUBR (Fsetcdr);
|
|
3458 DEFSUBR (Findirect_function);
|
|
3459 DEFSUBR (Faref);
|
|
3460 DEFSUBR (Faset);
|
|
3461
|
|
3462 DEFSUBR (Fnumber_to_string);
|
|
3463 DEFSUBR (Fstring_to_number);
|
|
3464 DEFSUBR (Feqlsign);
|
|
3465 DEFSUBR (Flss);
|
|
3466 DEFSUBR (Fgtr);
|
|
3467 DEFSUBR (Fleq);
|
|
3468 DEFSUBR (Fgeq);
|
|
3469 DEFSUBR (Fneq);
|
|
3470 DEFSUBR (Fzerop);
|
|
3471 DEFSUBR (Fplus);
|
|
3472 DEFSUBR (Fminus);
|
|
3473 DEFSUBR (Ftimes);
|
|
3474 DEFSUBR (Fquo);
|
|
3475 DEFSUBR (Frem);
|
|
3476 DEFSUBR (Fmod);
|
|
3477 DEFSUBR (Fmax);
|
|
3478 DEFSUBR (Fmin);
|
|
3479 DEFSUBR (Flogand);
|
|
3480 DEFSUBR (Flogior);
|
|
3481 DEFSUBR (Flogxor);
|
|
3482 DEFSUBR (Flsh);
|
|
3483 DEFSUBR (Fash);
|
|
3484 DEFSUBR (Fadd1);
|
|
3485 DEFSUBR (Fsub1);
|
|
3486 DEFSUBR (Flognot);
|
|
3487
|
|
3488 DEFSUBR (Fweak_list_p);
|
|
3489 DEFSUBR (Fmake_weak_list);
|
|
3490 DEFSUBR (Fweak_list_type);
|
|
3491 DEFSUBR (Fweak_list_list);
|
|
3492 DEFSUBR (Fset_weak_list_list);
|
858
|
3493
|
888
|
3494 DEFSUBR (Fmake_ephemeron);
|
|
3495 DEFSUBR (Fephemeron_ref);
|
|
3496 DEFSUBR (Fephemeronp);
|
858
|
3497 DEFSUBR (Fmake_weak_box);
|
|
3498 DEFSUBR (Fweak_box_ref);
|
|
3499 DEFSUBR (Fweak_boxp);
|
428
|
3500 }
|
|
3501
|
|
3502 void
|
|
3503 vars_of_data (void)
|
|
3504 {
|
|
3505 /* This must not be staticpro'd */
|
|
3506 Vall_weak_lists = Qnil;
|
452
|
3507 dump_add_weak_object_chain (&Vall_weak_lists);
|
428
|
3508
|
888
|
3509 Vall_ephemerons = Qnil;
|
|
3510 dump_add_weak_object_chain (&Vall_ephemerons);
|
|
3511
|
|
3512 Vfinalize_list = Qnil;
|
|
3513 staticpro (&Vfinalize_list);
|
|
3514
|
858
|
3515 Vall_weak_boxes = Qnil;
|
|
3516 dump_add_weak_object_chain (&Vall_weak_boxes);
|
|
3517
|
428
|
3518 #ifdef DEBUG_XEMACS
|
|
3519 DEFVAR_BOOL ("debug-issue-ebola-notices", &debug_issue_ebola_notices /*
|
|
3520 If non-zero, note when your code may be suffering from char-int confoundance.
|
|
3521 That is to say, if XEmacs encounters a usage of `eq', `memq', `equal',
|
|
3522 etc. where an int and a char with the same value are being compared,
|
|
3523 it will issue a notice on stderr to this effect, along with a backtrace.
|
|
3524 In such situations, the result would be different in XEmacs 19 versus
|
|
3525 XEmacs 20, and you probably don't want this.
|
|
3526
|
|
3527 Note that in order to see these notices, you have to byte compile your
|
|
3528 code under XEmacs 20 -- any code byte-compiled under XEmacs 19 will
|
|
3529 have its chars and ints all confounded in the byte code, making it
|
|
3530 impossible to accurately determine Ebola infection.
|
|
3531 */ );
|
|
3532
|
|
3533 debug_issue_ebola_notices = 0;
|
|
3534
|
|
3535 DEFVAR_INT ("debug-ebola-backtrace-length",
|
|
3536 &debug_ebola_backtrace_length /*
|
|
3537 Length (in stack frames) of short backtrace printed out in Ebola notices.
|
|
3538 See `debug-issue-ebola-notices'.
|
|
3539 */ );
|
|
3540 debug_ebola_backtrace_length = 32;
|
|
3541
|
|
3542 #endif /* DEBUG_XEMACS */
|
|
3543 }
|