comparison src/alloc.c @ 5560:58b38d5b32d0

Implement print-circle, allowing recursive and circular structures to be read. src/ChangeLog addition: 2011-09-04 Aidan Kehoe <kehoea@parhasard.net> * alloc.c: * alloc.c (ALLOC_FROB_BLOCK_LISP_OBJECT_1): * alloc.c (ALLOC_FROB_BLOCK_LISP_OBJECT): * alloc.c (cons_print_preprocess): * alloc.c (vector_print_preprocess): * alloc.c (vector_nsubst_structures_descend): * alloc.c (Fmake_symbol): * alloc.c (UNMARK_symbol): * alloc.c (sweep_symbols): * alloc.c (reinit_alloc_objects_early): * alloc.c (reinit_alloc_early): * bytecode.c: * bytecode.c (compiled_function_print_preprocess): * bytecode.c (compiled_function_nsubst_structures_descend): * bytecode.c (set_compiled_function_arglist): * bytecode.c (set_compiled_function_interactive): * bytecode.c (bytecode_objects_create): * chartab.c: * chartab.c (print_preprocess_mapper): * chartab.c (nsubst_structures_mapper): * chartab.c (char_table_nsubst_structures_descend): * chartab.c (chartab_objects_create): * elhash.c: * elhash.c (nsubst_structures_map_hash_table): * elhash.c (hash_table_nsubst_structures_descend): * elhash.c (print_preprocess_mapper): * elhash.c (hash_table_print_preprocess): * elhash.c (inchash_eq): * elhash.c (hash_table_objects_create): * elhash.c (syms_of_elhash): * elhash.h: * emacs.c (main_1): * fns.c: * fns.c (check_eq_nokey): * fns.c (Fnsubst): * fns.c (syms_of_fns): * lisp.h: * lisp.h (struct Lisp_Symbol): * lisp.h (IN_OBARRAY): * lisp.h (struct): * lisp.h (PRINT_PREPROCESS): * lread.c (read1): * lrecord.h: * lrecord.h (struct lrecord_implementation): * lrecord.h (DEFINE_DUMPABLE_MODULE_LISP_OBJECT): * print.c: * print.c (PRINT_CIRCLE_LIMIT): * print.c (print_continuous_numbering_changed): * print.c (print_prepare): * print.c (print_finish): * print.c (Fprin1_to_string): * print.c (print_cons): * print.c (print_preprocess_inchash_eq): * print.c (print_preprocess): * print.c (print_sort_get_numbers): * print.c (print_sort_compare_ordinals): * print.c (print_gensym_or_circle): * print.c (nsubst_structures_descend): * print.c (nsubst_structures): * print.c (print_internal): * print.c (print_symbol): * print.c (vars_of_print): * rangetab.c: * rangetab.c (range_table_print_preprocess): * rangetab.c (range_table_nsubst_structures_descend): * rangetab.c (rangetab_objects_create): * rangetab.c (syms_of_rangetab): * symbols.c: * symbols.c (symbol_print_preprocess): * symbols.c (Fintern): * symbols.c (Funintern): * symbols.c (reinit_symbol_objects_early): * symbols.c (init_symbols_once_early): * symsinit.h: Implement print-circle, printing circular structures in a readable fashion, and treating them appropriately on read. This is by means of two new object methods, print_preprocess (detecting circularities), and nsubst_structures_descend (replacing placeholders with the read objects). Expose the substitution to Lisp via #'nsubst and its new :descend-structures keyword. Store information as to whether symbols are interned in obarray or not in their header, making checking for keywords and uninterned symbols (and thus printing) cheaper. Default print_gensym to t, as Common Lisp does, and as a more-than-decade old comment suggests. lisp/ChangeLog addition: 2011-09-04 Aidan Kehoe <kehoea@parhasard.net> * bytecomp.el (byte-compile-output-file-form): * bytecomp.el (byte-compile-output-docform): Bind print-circle, print-continuous-numbering in these functions, now those variables are available. * lisp.el (forward-sexp): * lisp.el (backward-sexp): Recognise leading #N= as being part of an expression. tests/ChangeLog addition: 2011-09-04 Aidan Kehoe <kehoea@parhasard.net> * automated/lisp-reader-tests.el: * automated/lisp-tests.el (literal-with-uninterned): * automated/symbol-tests.el (foo): Test print-circle, for printing (mutually-)recursive and circular structures. Bind print-continuous-numbering where appropriate.
author Aidan Kehoe <kehoea@parhasard.net>
date Sun, 04 Sep 2011 19:51:35 +0100
parents 248176c74e6b
children 56144c8593a8
comparison
equal deleted inserted replaced
5559:f3ab0c29c246 5560:58b38d5b32d0
1264 #define FREE_FIXED_TYPE_WHEN_NOT_IN_GC(lo, type, structtype, ptr) 1264 #define FREE_FIXED_TYPE_WHEN_NOT_IN_GC(lo, type, structtype, ptr)
1265 #endif 1265 #endif
1266 #endif /* (not) NEW_GC */ 1266 #endif /* (not) NEW_GC */
1267 1267
1268 #ifdef NEW_GC 1268 #ifdef NEW_GC
1269 #define ALLOC_FROB_BLOCK_LISP_OBJECT(type, lisp_type, var, lrec_ptr)\ 1269 #define ALLOC_FROB_BLOCK_LISP_OBJECT_1(type, lisp_type, var, lrec_ptr, \
1270 lheader) \
1270 do { \ 1271 do { \
1271 (var) = (lisp_type *) XPNTR (ALLOC_NORMAL_LISP_OBJECT (type)); \ 1272 (var) = (lisp_type *) XPNTR (ALLOC_NORMAL_LISP_OBJECT (type)); \
1272 } while (0) 1273 } while (0)
1273 #define NOSEEUM_ALLOC_FROB_BLOCK_LISP_OBJECT(type, lisp_type, var, \ 1274 #define NOSEEUM_ALLOC_FROB_BLOCK_LISP_OBJECT_1(type, lisp_type, var, \
1274 lrec_ptr) \ 1275 lrec_ptr, lheader) \
1275 do { \ 1276 do { \
1276 (var) = (lisp_type *) XPNTR (noseeum_alloc_lrecord (lrec_ptr)); \ 1277 (var) = (lisp_type *) XPNTR (noseeum_alloc_lrecord (lrec_ptr)); \
1277 } while (0) 1278 } while (0)
1278 #else /* not NEW_GC */ 1279 #else /* not NEW_GC */
1279 #define ALLOC_FROB_BLOCK_LISP_OBJECT(type, lisp_type, var, lrec_ptr) \ 1280 #define ALLOC_FROB_BLOCK_LISP_OBJECT_1(type, lisp_type, var, lrec_ptr, \
1281 lheader) \
1280 do \ 1282 do \
1281 { \ 1283 { \
1282 ALLOCATE_FIXED_TYPE (type, lisp_type, var); \ 1284 ALLOCATE_FIXED_TYPE (type, lisp_type, var); \
1283 set_lheader_implementation (&(var)->lheader, lrec_ptr); \ 1285 set_lheader_implementation (&(var)->lheader, lrec_ptr); \
1284 } while (0) 1286 } while (0)
1285 #define NOSEEUM_ALLOC_FROB_BLOCK_LISP_OBJECT(type, lisp_type, var, \ 1287 #define NOSEEUM_ALLOC_FROB_BLOCK_LISP_OBJECT_1(type, lisp_type, var, \
1286 lrec_ptr) \ 1288 lrec_ptr, lheader) \
1287 do \ 1289 do \
1288 { \ 1290 { \
1289 NOSEEUM_ALLOCATE_FIXED_TYPE (type, lisp_type, var); \ 1291 NOSEEUM_ALLOCATE_FIXED_TYPE (type, lisp_type, var); \
1290 set_lheader_implementation (&(var)->lheader, lrec_ptr); \ 1292 set_lheader_implementation (&(var)->lheader, lrec_ptr); \
1291 } while (0) 1293 } while (0)
1292 #endif /* not NEW_GC */ 1294 #endif /* not NEW_GC */
1293 1295
1294 1296 #define ALLOC_FROB_BLOCK_LISP_OBJECT(type, lisp_type, var, lrec_ptr) \
1297 ALLOC_FROB_BLOCK_LISP_OBJECT_1(type, lisp_type, var, lrec_ptr, lheader)
1298
1299 #define NOSEEUM_ALLOC_FROB_BLOCK_LISP_OBJECT(type, lisp_type, var, lrec_ptr) \
1300 NOSEEUM_ALLOC_FROB_BLOCK_LISP_OBJECT_1(type, lisp_type, var, lrec_ptr, \
1301 lheader)
1295 1302
1296 /************************************************************************/ 1303 /************************************************************************/
1297 /* Cons allocation */ 1304 /* Cons allocation */
1298 /************************************************************************/ 1305 /************************************************************************/
1299 1306
1322 ob2 = XCDR (ob2); 1329 ob2 = XCDR (ob2);
1323 if (! CONSP (ob1) || ! CONSP (ob2)) 1330 if (! CONSP (ob1) || ! CONSP (ob2))
1324 return internal_equal_0 (ob1, ob2, depth, foldcase); 1331 return internal_equal_0 (ob1, ob2, depth, foldcase);
1325 } 1332 }
1326 return 0; 1333 return 0;
1334 }
1335
1336 extern Elemcount
1337 print_preprocess_inchash_eq (Lisp_Object obj, Lisp_Object table,
1338 Elemcount *seen_object_count);
1339
1340 static void
1341 cons_print_preprocess (Lisp_Object object, Lisp_Object print_number_table,
1342 Elemcount *seen_object_count)
1343 {
1344 /* Special-case conses, don't recurse down the cdr if the cdr is a cons. */
1345 for (;;)
1346 {
1347 PRINT_PREPROCESS (XCAR (object), print_number_table, seen_object_count);
1348 object = XCDR (object);
1349
1350 if (!CONSP (object))
1351 {
1352 break;
1353 }
1354
1355 if (print_preprocess_inchash_eq (object, print_number_table,
1356 seen_object_count) > 1)
1357 {
1358 return;
1359 }
1360 }
1361
1362 PRINT_PREPROCESS (object, print_number_table, seen_object_count);
1363 }
1364
1365 static void
1366 cons_nsubst_structures_descend (Lisp_Object new_, Lisp_Object old,
1367 Lisp_Object object,
1368 Lisp_Object number_table,
1369 Boolint test_not_unboundp)
1370 {
1371 /* No need for a special case, nsubst_structures_descend is called much
1372 less frequently than is print_preprocess. */
1373 if (EQ (old, XCAR (object)) == test_not_unboundp)
1374 {
1375 XSETCAR (object, new_);
1376 }
1377 else if (LRECORDP (XCAR (object)) &&
1378 HAS_OBJECT_METH_P (XCAR (object), nsubst_structures_descend))
1379 {
1380 nsubst_structures_descend (new_, old, XCAR (object), number_table,
1381 test_not_unboundp);
1382 }
1383
1384 if (EQ (old, XCDR (object)) == test_not_unboundp)
1385 {
1386 XSETCDR (object, new_);
1387 }
1388 else if (LRECORDP (XCDR (object)) &&
1389 HAS_OBJECT_METH_P (XCDR (object), nsubst_structures_descend))
1390 {
1391 nsubst_structures_descend (new_, old, XCDR (object), number_table,
1392 test_not_unboundp);
1393 }
1327 } 1394 }
1328 1395
1329 static const struct memory_description cons_description[] = { 1396 static const struct memory_description cons_description[] = {
1330 { XD_LISP_OBJECT, offsetof (Lisp_Cons, car_) }, 1397 { XD_LISP_OBJECT, offsetof (Lisp_Cons, car_) },
1331 { XD_LISP_OBJECT, offsetof (Lisp_Cons, cdr_) }, 1398 { XD_LISP_OBJECT, offsetof (Lisp_Cons, cdr_) },
1709 { 1776 {
1710 return HASH2 (XVECTOR_LENGTH (obj), 1777 return HASH2 (XVECTOR_LENGTH (obj),
1711 internal_array_hash (XVECTOR_DATA (obj), 1778 internal_array_hash (XVECTOR_DATA (obj),
1712 XVECTOR_LENGTH (obj), 1779 XVECTOR_LENGTH (obj),
1713 depth + 1, equalp)); 1780 depth + 1, equalp));
1781 }
1782
1783 static void
1784 vector_print_preprocess (Lisp_Object object, Lisp_Object print_number_table,
1785 Elemcount *seen_object_count)
1786 {
1787 Elemcount ii, len;
1788
1789 for (ii = 0, len = XVECTOR_LENGTH (object); ii < len; ii++)
1790 {
1791 PRINT_PREPROCESS (XVECTOR_DATA (object)[ii], print_number_table,
1792 seen_object_count);
1793 }
1794 }
1795
1796 static void
1797 vector_nsubst_structures_descend (Lisp_Object new_, Lisp_Object old,
1798 Lisp_Object object, Lisp_Object number_table,
1799 Boolint test_not_unboundp)
1800 {
1801 Elemcount ii = XVECTOR_LENGTH (object);
1802 Lisp_Object *vdata = XVECTOR_DATA (object);
1803
1804 while (ii > 0)
1805 {
1806 --ii;
1807
1808 if (EQ (vdata[ii], old) == test_not_unboundp)
1809 {
1810 vdata[ii] = new_;
1811 }
1812 else if (LRECORDP (vdata[ii]) &&
1813 HAS_OBJECT_METH_P (vdata[ii], nsubst_structures_descend))
1814 {
1815 nsubst_structures_descend (new_, old, vdata[ii], number_table,
1816 test_not_unboundp);
1817 }
1818 }
1714 } 1819 }
1715 1820
1716 static const struct memory_description vector_description[] = { 1821 static const struct memory_description vector_description[] = {
1717 { XD_LONG, offsetof (Lisp_Vector, size) }, 1822 { XD_LONG, offsetof (Lisp_Vector, size) },
1718 { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Vector, contents), XD_INDIRECT(0, 0) }, 1823 { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Vector, contents), XD_INDIRECT(0, 0) },
2113 { 2218 {
2114 Lisp_Symbol *p; 2219 Lisp_Symbol *p;
2115 2220
2116 CHECK_STRING (name); 2221 CHECK_STRING (name);
2117 2222
2118 ALLOC_FROB_BLOCK_LISP_OBJECT (symbol, Lisp_Symbol, p, &lrecord_symbol); 2223 ALLOC_FROB_BLOCK_LISP_OBJECT_1 (symbol, Lisp_Symbol, p, &lrecord_symbol,
2224 u.lheader);
2225 p->u.v.package_count = 0;
2226 p->u.v.first_package_id = 0;
2227
2119 p->name = name; 2228 p->name = name;
2120 p->plist = Qnil; 2229 p->plist = Qnil;
2121 p->value = Qunbound; 2230 p->value = Qunbound;
2122 p->function = Qunbound; 2231 p->function = Qunbound;
2123 symbol_next (p) = 0; 2232 symbol_next (p) = 0;
4787 #endif 4896 #endif
4788 4897
4789 static void 4898 static void
4790 sweep_symbols (void) 4899 sweep_symbols (void)
4791 { 4900 {
4792 #define UNMARK_symbol(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) 4901 #define UNMARK_symbol(ptr) UNMARK_RECORD_HEADER (&(((ptr)->u.lheader)))
4793 #define ADDITIONAL_FREE_symbol(ptr) 4902 #define ADDITIONAL_FREE_symbol(ptr)
4794 4903
4795 SWEEP_FIXED_TYPE_BLOCK (symbol, Lisp_Symbol); 4904 SWEEP_FIXED_TYPE_BLOCK_1 (symbol, Lisp_Symbol, u.lheader);
4796 } 4905 }
4797 4906
4798 static void 4907 static void
4799 sweep_extents (void) 4908 sweep_extents (void)
4800 { 4909 {
5629 { 5738 {
5630 OBJECT_HAS_METHOD (string, getprop); 5739 OBJECT_HAS_METHOD (string, getprop);
5631 OBJECT_HAS_METHOD (string, putprop); 5740 OBJECT_HAS_METHOD (string, putprop);
5632 OBJECT_HAS_METHOD (string, remprop); 5741 OBJECT_HAS_METHOD (string, remprop);
5633 OBJECT_HAS_METHOD (string, plist); 5742 OBJECT_HAS_METHOD (string, plist);
5743
5744 OBJECT_HAS_METHOD (cons, print_preprocess);
5745 OBJECT_HAS_METHOD (cons, nsubst_structures_descend);
5746 OBJECT_HAS_METHOD (vector, print_preprocess);
5747 OBJECT_HAS_METHOD (vector, nsubst_structures_descend);
5634 } 5748 }
5635 5749
5636 void 5750 void
5637 reinit_alloc_early (void) 5751 reinit_alloc_early (void)
5638 { 5752 {