ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/microscheme/scheme.c
(Generate patch)

Comparing cvsroot/microscheme/scheme.c (file contents):
Revision 1.42 by root, Mon Nov 30 06:19:18 2015 UTC vs.
Revision 1.48 by root, Mon Nov 30 13:07:34 2015 UTC

16 * (MINISCM) This is a revised and modified version by Akira KIDA. 16 * (MINISCM) This is a revised and modified version by Akira KIDA.
17 * (MINISCM) current version is 0.85k4 (15 May 1994) 17 * (MINISCM) current version is 0.85k4 (15 May 1994)
18 * 18 *
19 */ 19 */
20 20
21#define EXPERIMENT 1
22
21#define PAGE_SIZE 4096 /* does not work on sparc/alpha */ 23#define PAGE_SIZE 4096 /* does not work on sparc/alpha */
22#include "malloc.c" 24#include "malloc.c"
23 25
24#define SCHEME_SOURCE 26#define SCHEME_SOURCE
25#include "scheme-private.h" 27#include "scheme-private.h"
411{ 413{
412 return strvalue (p); 414 return strvalue (p);
413} 415}
414 416
415#if USE_PLIST 417#if USE_PLIST
418#error plists are broken because symbols are no longer pairs
419#define symprop(p) cdr(p)
416SCHEME_EXPORT int 420SCHEME_EXPORT int
417hasprop (pointer p) 421hasprop (pointer p)
418{ 422{
419 return typeflag (p) & T_SYMBOL; 423 return typeflag (p) & T_SYMBOL;
420} 424}
421
422# define symprop(p) cdr(p)
423#endif 425#endif
424 426
425INTERFACE int 427INTERFACE int
426is_syntax (pointer p) 428is_syntax (pointer p)
427{ 429{
968 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 970 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
969 return S_SINK; 971 return S_SINK;
970 972
971 if (SCHEME_V->free_cell == NIL) 973 if (SCHEME_V->free_cell == NIL)
972 { 974 {
973 const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; 975 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1;
974 976
975 gc (SCHEME_A_ a, b); 977 gc (SCHEME_A_ a, b);
976 978
977 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) 979 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL)
978 { 980 {
1029} 1031}
1030 1032
1031static pointer 1033static pointer
1032get_vector_object (SCHEME_P_ uint32_t len, pointer init) 1034get_vector_object (SCHEME_P_ uint32_t len, pointer init)
1033{ 1035{
1034 pointer v = get_cell_x (SCHEME_A_ 0, 0); 1036 pointer v = get_cell_x (SCHEME_A_ NIL, NIL);
1035 pointer *e = malloc (len * sizeof (pointer)); 1037 pointer *e = malloc (len * sizeof (pointer));
1036 1038
1037 if (!e && USE_ERROR_CHECKING) 1039 if (!e && USE_ERROR_CHECKING)
1038 return S_SINK; 1040 return S_SINK;
1039 1041
1093 set_cdr (x, b); 1095 set_cdr (x, b);
1094 1096
1095 return x; 1097 return x;
1096} 1098}
1097 1099
1098/* ========== oblist implementation ========== */
1099
1100static pointer 1100static pointer
1101generate_symbol (SCHEME_P_ const char *name) 1101generate_symbol (SCHEME_P_ const char *name)
1102{ 1102{
1103 pointer x = mk_string (SCHEME_A_ name); 1103 pointer x = mk_string (SCHEME_A_ name);
1104 setimmutable (x); 1104 setimmutable (x);
1105 set_typeflag (x, T_SYMBOL | T_ATOM); 1105 set_typeflag (x, T_SYMBOL | T_ATOM);
1106 return x; 1106 return x;
1107} 1107}
1108 1108
1109/* ========== oblist implementation ========== */
1110
1109#ifndef USE_OBJECT_LIST 1111#ifndef USE_OBJECT_LIST
1110 1112
1111static int 1113static int
1112hash_fn (const char *key, int table_size) 1114hash_fn (const char *key, int table_size)
1113{ 1115{
1199 1201
1200/* returns the new symbol */ 1202/* returns the new symbol */
1201static pointer 1203static pointer
1202oblist_add_by_name (SCHEME_P_ const char *name) 1204oblist_add_by_name (SCHEME_P_ const char *name)
1203{ 1205{
1204 pointer x = mk_string (SCHEME_A_ name); 1206 pointer x = generate_symbol (SCHEME_A_ name);
1205 set_typeflag (x, T_SYMBOL);
1206 setimmutable (x);
1207 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1207 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1208 return x; 1208 return x;
1209} 1209}
1210 1210
1211static pointer 1211static pointer
1232pointer 1232pointer
1233mk_foreign_func (SCHEME_P_ foreign_func f) 1233mk_foreign_func (SCHEME_P_ foreign_func f)
1234{ 1234{
1235 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1235 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1236 1236
1237 set_typeflag (x, (T_FOREIGN | T_ATOM)); 1237 set_typeflag (x, T_FOREIGN | T_ATOM);
1238 x->object.ff = f; 1238 x->object.ff = f;
1239 1239
1240 return x; 1240 return x;
1241} 1241}
1242 1242
1243INTERFACE pointer 1243INTERFACE pointer
1244mk_character (SCHEME_P_ int c) 1244mk_character (SCHEME_P_ int c)
1245{ 1245{
1246 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1246 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1247 1247
1248 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1248 set_typeflag (x, T_CHARACTER | T_ATOM);
1249 set_ivalue (x, c & 0xff); 1249 set_ivalue (x, c & 0xff);
1250 1250
1251 return x; 1251 return x;
1252} 1252}
1253 1253
1254/* get number atom (integer) */ 1254/* get number atom (integer) */
1255INTERFACE pointer 1255INTERFACE pointer
1256mk_integer (SCHEME_P_ long n) 1256mk_integer (SCHEME_P_ long n)
1257{ 1257{
1258 pointer p = 0;
1259 pointer *pp = &p;
1260
1261#if USE_INTCACHE
1262 if (n >= INTCACHE_MIN && n <= INTCACHE_MAX)
1263 pp = &SCHEME_V->intcache[n - INTCACHE_MIN];
1264#endif
1265
1266 if (!*pp)
1267 {
1258 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1268 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1259 1269
1260 set_typeflag (x, (T_INTEGER | T_ATOM)); 1270 set_typeflag (x, T_INTEGER | T_ATOM);
1271 setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */
1261 set_ivalue (x, n); 1272 set_ivalue (x, n);
1262 1273
1274 *pp = x;
1275 }
1276
1263 return x; 1277 return *pp;
1264} 1278}
1265 1279
1266INTERFACE pointer 1280INTERFACE pointer
1267mk_real (SCHEME_P_ RVALUE n) 1281mk_real (SCHEME_P_ RVALUE n)
1268{ 1282{
1269#if USE_REAL 1283#if USE_REAL
1270 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1284 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1271 1285
1272 set_typeflag (x, (T_REAL | T_ATOM)); 1286 set_typeflag (x, T_REAL | T_ATOM);
1273 set_rvalue (x, n); 1287 set_rvalue (x, n);
1274 1288
1275 return x; 1289 return x;
1276#else 1290#else
1277 return mk_integer (SCHEME_A_ n); 1291 return mk_integer (SCHEME_A_ n);
1396 pointer x; 1410 pointer x;
1397 char name[40] = "gensym-"; 1411 char name[40] = "gensym-";
1398 xnum (name + 7, ++SCHEME_V->gensym_cnt); 1412 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1399 1413
1400 return generate_symbol (SCHEME_A_ name); 1414 return generate_symbol (SCHEME_A_ name);
1415}
1416
1417static int
1418is_gensym (SCHEME_P_ pointer x)
1419{
1420 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1401} 1421}
1402 1422
1403/* make symbol or number atom from string */ 1423/* make symbol or number atom from string */
1404static pointer 1424static pointer
1405mk_atom (SCHEME_P_ char *q) 1425mk_atom (SCHEME_P_ char *q)
1640 /* Mark recent objects the interpreter doesn't know about yet. */ 1660 /* Mark recent objects the interpreter doesn't know about yet. */
1641 mark (car (S_SINK)); 1661 mark (car (S_SINK));
1642 /* Mark any older stuff above nested C calls */ 1662 /* Mark any older stuff above nested C calls */
1643 mark (SCHEME_V->c_nest); 1663 mark (SCHEME_V->c_nest);
1644 1664
1665#if USE_INTCACHE
1666 /* mark intcache */
1667 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1668 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1669 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1670#endif
1671
1645 /* mark variables a, b */ 1672 /* mark variables a, b */
1646 mark (a); 1673 mark (a);
1647 mark (b); 1674 mark (b);
1648 1675
1649 /* garbage collect */ 1676 /* garbage collect */
1650 clrmark (NIL); 1677 clrmark (NIL);
1651 SCHEME_V->fcells = 0; 1678 SCHEME_V->fcells = 0;
1652 SCHEME_V->free_cell = NIL; 1679 SCHEME_V->free_cell = NIL;
1653 1680
1654 /* free-list is kept sorted by address so as to maintain consecutive 1681 if (SCHEME_V->gc_verbose)
1655 ranges, if possible, for use with vectors. Here we scan the cells 1682 xwrstr ("freeing...");
1656 (which are also kept sorted by address) downwards to build the 1683
1657 free-list in sorted order. 1684 uint32_t total = 0;
1658 */ 1685
1686 /* Here we scan the cells to build the free-list. */
1659 for (i = SCHEME_V->last_cell_seg; i >= 0; i--) 1687 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1660 { 1688 {
1661 p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; 1689 pointer end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1690 total += SCHEME_V->cell_segsize [i];
1662 1691
1663 while (--p >= SCHEME_V->cell_seg[i]) 1692 for (p = SCHEME_V->cell_seg[i]; p < end; ++p)
1664 { 1693 {
1665 if (is_mark (p)) 1694 if (is_mark (p))
1666 clrmark (p); 1695 clrmark (p);
1667 else 1696 else
1668 { 1697 {
1681 } 1710 }
1682 } 1711 }
1683 1712
1684 if (SCHEME_V->gc_verbose) 1713 if (SCHEME_V->gc_verbose)
1685 { 1714 {
1686 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1715 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" out of "); xwrnum (total); xwrstr (" cells were recovered.\n");
1687 } 1716 }
1688} 1717}
1689 1718
1690static void 1719static void
1691finalize_cell (SCHEME_P_ pointer a) 1720finalize_cell (SCHEME_P_ pointer a)
3241 3270
3242#endif 3271#endif
3243 3272
3244#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3273#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3245 3274
3246#if 1 3275#if EXPERIMENT
3247static int 3276static int
3248debug (SCHEME_P_ int indent, pointer x) 3277debug (SCHEME_P_ int indent, pointer x)
3249{ 3278{
3250 int c; 3279 int c;
3251 3280
3307 pointer args = SCHEME_V->args; 3336 pointer args = SCHEME_V->args;
3308 pointer x, y; 3337 pointer x, y;
3309 3338
3310 switch (op) 3339 switch (op)
3311 { 3340 {
3312#if 1 //D 3341#if EXPERIMENT //D
3313 case OP_DEBUG: 3342 case OP_DEBUG:
3314 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); 3343 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8);
3315 printf ("\n"); 3344 printf ("\n");
3316 s_return (S_T); 3345 s_return (S_T);
3317#endif 3346#endif
3556 3585
3557 case OP_DOMACRO: /* do macro */ 3586 case OP_DOMACRO: /* do macro */
3558 SCHEME_V->code = SCHEME_V->value; 3587 SCHEME_V->code = SCHEME_V->value;
3559 s_goto (OP_EVAL); 3588 s_goto (OP_EVAL);
3560 3589
3561#if 1
3562
3563 case OP_LAMBDA: /* lambda */ 3590 case OP_LAMBDA: /* lambda */
3564 /* If the hook is defined, apply it to SCHEME_V->code, otherwise 3591 /* If the hook is defined, apply it to SCHEME_V->code, otherwise
3565 set SCHEME_V->value fall thru */ 3592 set SCHEME_V->value fall thru */
3566 { 3593 {
3567 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); 3594 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1);
3573 SCHEME_V->code = slot_value_in_env (f); 3600 SCHEME_V->code = slot_value_in_env (f);
3574 s_goto (OP_APPLY); 3601 s_goto (OP_APPLY);
3575 } 3602 }
3576 3603
3577 SCHEME_V->value = SCHEME_V->code; 3604 SCHEME_V->value = SCHEME_V->code;
3578 /* Fallthru */
3579 } 3605 }
3606 /* Fallthru */
3580 3607
3581 case OP_LAMBDA1: 3608 case OP_LAMBDA1:
3582 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); 3609 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir));
3583
3584#else
3585
3586 case OP_LAMBDA: /* lambda */
3587 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3588
3589#endif
3590 3610
3591 case OP_MKCLOSURE: /* make-closure */ 3611 case OP_MKCLOSURE: /* make-closure */
3592 x = car (args); 3612 x = car (args);
3593 3613
3594 if (car (x) == SCHEME_V->LAMBDA) 3614 if (car (x) == SCHEME_V->LAMBDA)
4190 else 4210 else
4191 Error_0 ("modulo: division by zero"); 4211 Error_0 ("modulo: division by zero");
4192 4212
4193 s_return (mk_number (SCHEME_A_ v)); 4213 s_return (mk_number (SCHEME_A_ v));
4194 4214
4195 case OP_CAR: /* car */ 4215 /* the compiler will optimize this mess... */
4196 s_return (caar (args)); 4216 case OP_CAR: op_car: s_return (car (x));
4197 4217 case OP_CDR: op_cdr: s_return (cdr (x));
4198 case OP_CDR: /* cdr */ 4218 case OP_CAAR: op_caar: x = car (x); goto op_car;
4199 s_return (cdar (args)); 4219 case OP_CADR: op_cadr: x = cdr (x); goto op_car;
4220 case OP_CDAR: op_cdar: x = car (x); goto op_cdr;
4221 case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr;
4222 case OP_CAAAR: op_caaar: x = car (x); goto op_caar;
4223 case OP_CAADR: op_caadr: x = cdr (x); goto op_caar;
4224 case OP_CADAR: op_cadar: x = car (x); goto op_cadr;
4225 case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr;
4226 case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar;
4227 case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar;
4228 case OP_CDDAR: op_cddar: x = car (x); goto op_cddr;
4229 case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr;
4230 case OP_CAAAAR: x = car (x); goto op_caaar;
4231 case OP_CAAADR: x = cdr (x); goto op_caaar;
4232 case OP_CAADAR: x = car (x); goto op_caadr;
4233 case OP_CAADDR: x = cdr (x); goto op_caadr;
4234 case OP_CADAAR: x = car (x); goto op_cadar;
4235 case OP_CADADR: x = cdr (x); goto op_cadar;
4236 case OP_CADDAR: x = car (x); goto op_caddr;
4237 case OP_CADDDR: x = cdr (x); goto op_caddr;
4238 case OP_CDAAAR: x = car (x); goto op_cdaar;
4239 case OP_CDAADR: x = cdr (x); goto op_cdaar;
4240 case OP_CDADAR: x = car (x); goto op_cdadr;
4241 case OP_CDADDR: x = cdr (x); goto op_cdadr;
4242 case OP_CDDAAR: x = car (x); goto op_cddar;
4243 case OP_CDDADR: x = cdr (x); goto op_cddar;
4244 case OP_CDDDAR: x = car (x); goto op_cdddr;
4245 case OP_CDDDDR: x = cdr (x); goto op_cdddr;
4200 4246
4201 case OP_CONS: /* cons */ 4247 case OP_CONS: /* cons */
4202 set_cdr (args, cadr (args)); 4248 set_cdr (args, cadr (args));
4203 s_return (args); 4249 s_return (args);
4204 4250
4526 pointer d = cdr (args); 4572 pointer d = cdr (args);
4527 int r; 4573 int r;
4528 4574
4529 switch (op) 4575 switch (op)
4530 { 4576 {
4531 case OP_NOT: /* not */ r = is_false (a) ; break; 4577 case OP_NOT: /* not */ r = is_false (a) ; break;
4532 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; 4578 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break;
4533 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; 4579 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4534 case OP_NULLP: /* null? */ r = a == NIL ; break; 4580 case OP_NULLP: /* null? */ r = a == NIL ; break;
4535 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; 4581 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4582 case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break;
4536 case OP_NUMBERP: /* number? */ r = is_number (a) ; break; 4583 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4537 case OP_STRINGP: /* string? */ r = is_string (a) ; break; 4584 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4538 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4585 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4539 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4586 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4540 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4587 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4541 4588
4542#if USE_CHAR_CLASSIFIERS 4589#if USE_CHAR_CLASSIFIERS
4543 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; 4590 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4544 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; 4591 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4545 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; 4592 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
5626scheme_init (SCHEME_P) 5673scheme_init (SCHEME_P)
5627{ 5674{
5628 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5675 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5629 pointer x; 5676 pointer x;
5630 5677
5678 memset (SCHEME_V, 0, sizeof (*SCHEME_V));//TODO !iso c
5679
5631 num_set_fixnum (num_zero, 1); 5680 num_set_fixnum (num_zero, 1);
5632 num_set_ivalue (num_zero, 0); 5681 num_set_ivalue (num_zero, 0);
5633 num_set_fixnum (num_one, 1); 5682 num_set_fixnum (num_one, 1);
5634 num_set_ivalue (num_one, 1); 5683 num_set_ivalue (num_one, 1);
5635 5684

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines