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