2018-03-04 23:13:08 +03:00
|
|
|
/* Runtime library */
|
|
|
|
|
|
|
|
|
|
# include <stdio.h>
|
2018-04-30 17:18:41 +03:00
|
|
|
# include <stdio.h>
|
|
|
|
|
# include <string.h>
|
|
|
|
|
# include <stdarg.h>
|
2018-10-31 20:10:50 +03:00
|
|
|
# include <stdlib.h>
|
2018-11-29 18:27:59 +03:00
|
|
|
# include <sys/mman.h>
|
|
|
|
|
# include <assert.h>
|
2019-12-21 02:34:56 +03:00
|
|
|
# include <errno.h>
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2019-10-16 21:07:27 +03:00
|
|
|
# define __ENABLE_GC__
|
2019-10-16 01:13:52 +03:00
|
|
|
# ifndef __ENABLE_GC__
|
|
|
|
|
# define alloc malloc
|
|
|
|
|
# endif
|
|
|
|
|
|
2019-12-20 00:23:35 +03:00
|
|
|
/*# define DEBUG_PRINT 1 */
|
2019-10-16 01:13:52 +03:00
|
|
|
|
2019-04-24 16:02:29 +03:00
|
|
|
/* GC pool structure and data; declared here in order to allow debug print */
|
|
|
|
|
typedef struct {
|
|
|
|
|
size_t * begin;
|
|
|
|
|
size_t * end;
|
|
|
|
|
size_t * current;
|
|
|
|
|
size_t size;
|
|
|
|
|
} pool;
|
|
|
|
|
|
|
|
|
|
static pool from_space;
|
|
|
|
|
static pool to_space;
|
|
|
|
|
size_t *current;
|
|
|
|
|
/* end */
|
|
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
# ifdef __ENABLE_GC__
|
|
|
|
|
|
2019-04-24 16:02:29 +03:00
|
|
|
/* GC extern invariant for built-in functions */
|
2019-04-24 17:34:53 +03:00
|
|
|
extern void __pre_gc ();
|
2019-04-24 16:02:29 +03:00
|
|
|
extern void __post_gc ();
|
2019-10-16 01:13:52 +03:00
|
|
|
|
|
|
|
|
# else
|
|
|
|
|
|
|
|
|
|
# define __pre_gc __pre_gc_subst
|
|
|
|
|
# define __post_gc __post_gc_subst
|
|
|
|
|
|
|
|
|
|
void __pre_gc_subst () {}
|
|
|
|
|
void __post_gc_subst () {}
|
|
|
|
|
|
|
|
|
|
# endif
|
2019-04-24 16:02:29 +03:00
|
|
|
/* end */
|
2018-11-30 16:18:12 +03:00
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
# define STRING_TAG 0x00000001
|
|
|
|
|
# define ARRAY_TAG 0x00000003
|
|
|
|
|
# define SEXP_TAG 0x00000005
|
|
|
|
|
# define CLOSURE_TAG 0x00000007
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2018-11-29 18:27:59 +03:00
|
|
|
# define LEN(x) ((x & 0xFFFFFFF8) >> 3)
|
2019-04-24 17:34:53 +03:00
|
|
|
# define TAG(x) (x & 0x00000007)
|
2018-04-30 17:18:41 +03:00
|
|
|
|
|
|
|
|
# define TO_DATA(x) ((data*)((char*)(x)-sizeof(int)))
|
2018-05-16 09:24:40 +03:00
|
|
|
# define TO_SEXP(x) ((sexp*)((char*)(x)-2*sizeof(int)))
|
2019-04-24 17:34:53 +03:00
|
|
|
#ifdef DEBUG_PRINT // GET_SEXP_TAG is necessary for printing from space
|
|
|
|
|
# define GET_SEXP_TAG(x) (LEN(x))
|
|
|
|
|
#endif
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2019-04-24 17:34:53 +03:00
|
|
|
# define UNBOXED(x) (((int) (x)) & 0x0001)
|
|
|
|
|
# define UNBOX(x) (((int) (x)) >> 1)
|
2018-10-31 20:10:50 +03:00
|
|
|
# define BOX(x) ((((int) (x)) << 1) | 0x0001)
|
2018-10-25 03:15:24 +03:00
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
typedef struct {
|
|
|
|
|
int tag;
|
|
|
|
|
char contents[0];
|
|
|
|
|
} data;
|
|
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
typedef struct {
|
|
|
|
|
int tag;
|
|
|
|
|
data contents;
|
|
|
|
|
} sexp;
|
|
|
|
|
|
2018-12-11 10:22:23 +03:00
|
|
|
extern void* alloc (size_t);
|
2018-11-29 18:27:59 +03:00
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
extern int Blength (void *p) {
|
2019-04-24 17:34:53 +03:00
|
|
|
data *a = (data*) BOX (NULL);
|
2018-12-11 10:22:23 +03:00
|
|
|
a = TO_DATA(p);
|
2018-10-25 03:15:24 +03:00
|
|
|
return BOX(LEN(a->tag));
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
char* de_hash (int n) {
|
2018-12-11 10:22:23 +03:00
|
|
|
static char *chars = (char*) BOX (NULL);
|
|
|
|
|
static char buf[6] = {0,0,0,0,0,0};
|
2019-04-24 17:34:53 +03:00
|
|
|
char *p = (char *) BOX (NULL);
|
2019-08-26 15:51:12 +03:00
|
|
|
chars = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
2018-12-11 10:22:23 +03:00
|
|
|
p = &buf[5];
|
2019-04-24 17:34:53 +03:00
|
|
|
|
|
|
|
|
#ifdef DEBUG_PRINT
|
|
|
|
|
printf ("de_hash: tag: %d\n", n); fflush (stdout);
|
|
|
|
|
#endif
|
2018-10-31 20:10:50 +03:00
|
|
|
|
|
|
|
|
*p-- = 0;
|
|
|
|
|
|
|
|
|
|
while (n != 0) {
|
2019-04-24 17:34:53 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
|
|
|
|
printf ("char: %c\n", chars [n & 0x003F]); fflush (stdout);
|
|
|
|
|
#endif
|
2018-10-31 20:10:50 +03:00
|
|
|
*p-- = chars [n & 0x003F];
|
|
|
|
|
n = n >> 6;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ++p;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
static void vfailure (char *s, va_list args) {
|
|
|
|
|
fprintf (stderr, "*** FAILURE: ");
|
|
|
|
|
vfprintf (stderr, s, args); // vprintf (char *, va_list) <-> printf (char *, ...)
|
|
|
|
|
exit (255);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void failure (char *s, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
|
|
va_start (args, s);
|
|
|
|
|
vfailure (s, args);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
typedef struct {
|
|
|
|
|
char *contents;
|
|
|
|
|
int ptr;
|
|
|
|
|
int len;
|
|
|
|
|
} StringBuf;
|
|
|
|
|
|
|
|
|
|
static StringBuf stringBuf;
|
|
|
|
|
|
|
|
|
|
# define STRINGBUF_INIT 128
|
|
|
|
|
|
|
|
|
|
static void createStringBuf () {
|
|
|
|
|
stringBuf.contents = (char*) malloc (STRINGBUF_INIT);
|
|
|
|
|
stringBuf.ptr = 0;
|
|
|
|
|
stringBuf.len = STRINGBUF_INIT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void deleteStringBuf () {
|
|
|
|
|
free (stringBuf.contents);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void extendStringBuf () {
|
|
|
|
|
int len = stringBuf.len << 1;
|
|
|
|
|
|
|
|
|
|
stringBuf.contents = (char*) realloc (stringBuf.contents, len);
|
|
|
|
|
stringBuf.len = len;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
static void vprintStringBuf (char *fmt, va_list args) {
|
2018-12-06 23:04:45 +03:00
|
|
|
int written = 0,
|
|
|
|
|
rest = 0;
|
|
|
|
|
char *buf = (char*) BOX(NULL);
|
2018-10-31 20:10:50 +03:00
|
|
|
|
|
|
|
|
again:
|
2018-11-06 14:25:28 +03:00
|
|
|
buf = &stringBuf.contents[stringBuf.ptr];
|
2018-10-31 20:10:50 +03:00
|
|
|
rest = stringBuf.len - stringBuf.ptr;
|
|
|
|
|
written = vsnprintf (buf, rest, fmt, args);
|
|
|
|
|
|
|
|
|
|
if (written >= rest) {
|
|
|
|
|
extendStringBuf ();
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stringBuf.ptr += written;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
static void printStringBuf (char *fmt, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
|
vprintStringBuf (fmt, args);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
static void printValue (void *p) {
|
2018-12-06 23:04:45 +03:00
|
|
|
data *a = (data*) BOX(NULL);
|
|
|
|
|
int i = BOX(0);
|
2018-10-31 20:10:50 +03:00
|
|
|
if (UNBOXED(p)) printStringBuf ("%d", UNBOX(p));
|
|
|
|
|
else {
|
2018-12-06 23:04:45 +03:00
|
|
|
a = TO_DATA(p);
|
2018-10-31 20:10:50 +03:00
|
|
|
|
|
|
|
|
switch (TAG(a->tag)) {
|
|
|
|
|
case STRING_TAG:
|
|
|
|
|
printStringBuf ("\"%s\"", a->contents);
|
|
|
|
|
break;
|
2019-12-18 18:44:01 +03:00
|
|
|
|
|
|
|
|
case CLOSURE_TAG:
|
|
|
|
|
printStringBuf ("<closure ");
|
|
|
|
|
for (i = 0; i < LEN(a->tag); i++) {
|
|
|
|
|
if (i) printValue ((void*)((int*) a->contents)[i]);
|
|
|
|
|
else printStringBuf ("%x", (void*)((int*) a->contents)[i]);
|
|
|
|
|
|
|
|
|
|
if (i != LEN(a->tag) - 1) printStringBuf (", ");
|
|
|
|
|
}
|
|
|
|
|
printStringBuf (">");
|
|
|
|
|
break;
|
2018-10-31 20:10:50 +03:00
|
|
|
|
|
|
|
|
case ARRAY_TAG:
|
|
|
|
|
printStringBuf ("[");
|
2018-12-06 23:04:45 +03:00
|
|
|
for (i = 0; i < LEN(a->tag); i++) {
|
2018-10-31 20:10:50 +03:00
|
|
|
printValue ((void*)((int*) a->contents)[i]);
|
|
|
|
|
if (i != LEN(a->tag) - 1) printStringBuf (", ");
|
|
|
|
|
}
|
|
|
|
|
printStringBuf ("]");
|
|
|
|
|
break;
|
|
|
|
|
|
2019-03-07 21:12:43 +03:00
|
|
|
case SEXP_TAG: {
|
2019-04-24 17:34:53 +03:00
|
|
|
#ifndef DEBUG_PRINT
|
|
|
|
|
char * tag = de_hash (TO_SEXP(p)->tag);
|
|
|
|
|
#else
|
|
|
|
|
char * tag = de_hash (GET_SEXP_TAG(TO_SEXP(p)->tag));
|
|
|
|
|
#endif
|
2019-03-07 21:12:43 +03:00
|
|
|
|
|
|
|
|
if (strcmp (tag, "cons") == 0) {
|
|
|
|
|
data *b = a;
|
|
|
|
|
|
|
|
|
|
printStringBuf ("{");
|
|
|
|
|
|
|
|
|
|
while (LEN(a->tag)) {
|
|
|
|
|
printValue ((void*)((int*) b->contents)[0]);
|
|
|
|
|
b = (data*)((int*) b->contents)[1];
|
|
|
|
|
if (! UNBOXED(b)) {
|
|
|
|
|
printStringBuf (", ");
|
|
|
|
|
b = TO_DATA(b);
|
|
|
|
|
}
|
|
|
|
|
else break;
|
2018-10-31 21:48:44 +03:00
|
|
|
}
|
2019-03-07 21:12:43 +03:00
|
|
|
|
|
|
|
|
printStringBuf ("}");
|
2018-10-31 20:10:50 +03:00
|
|
|
}
|
2019-03-07 21:12:43 +03:00
|
|
|
else {
|
|
|
|
|
printStringBuf ("%s", tag);
|
|
|
|
|
if (LEN(a->tag)) {
|
|
|
|
|
printStringBuf (" (");
|
|
|
|
|
for (i = 0; i < LEN(a->tag); i++) {
|
|
|
|
|
printValue ((void*)((int*) a->contents)[i]);
|
|
|
|
|
if (i != LEN(a->tag) - 1) printStringBuf (", ");
|
|
|
|
|
}
|
|
|
|
|
printStringBuf (")");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2019-04-24 17:34:53 +03:00
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
default:
|
|
|
|
|
printStringBuf ("*** invalid tag: %x ***", TAG(a->tag));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
static void stringcat (void *p) {
|
|
|
|
|
data *a;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (UNBOXED(p)) ;
|
|
|
|
|
else {
|
|
|
|
|
a = TO_DATA(p);
|
|
|
|
|
|
|
|
|
|
switch (TAG(a->tag)) {
|
|
|
|
|
case STRING_TAG:
|
|
|
|
|
printStringBuf ("%s", a->contents);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEXP_TAG: {
|
|
|
|
|
#ifndef DEBUG_PRINT
|
|
|
|
|
char * tag = de_hash (TO_SEXP(p)->tag);
|
|
|
|
|
#else
|
|
|
|
|
char * tag = de_hash (GET_SEXP_TAG(TO_SEXP(p)->tag));
|
|
|
|
|
#endif
|
|
|
|
|
if (strcmp (tag, "cons") == 0) {
|
|
|
|
|
data *b = a;
|
|
|
|
|
|
|
|
|
|
while (LEN(a->tag)) {
|
|
|
|
|
stringcat ((void*)((int*) b->contents)[0]);
|
|
|
|
|
b = (data*)((int*) b->contents)[1];
|
|
|
|
|
if (! UNBOXED(b)) {
|
|
|
|
|
b = TO_DATA(b);
|
|
|
|
|
}
|
|
|
|
|
else break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else printStringBuf ("*** non-list tag: %s ***", tag);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
printStringBuf ("*** invalid tag: %x ***", TAG(a->tag));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Lcompare (void *p, void *q) {
|
2019-12-20 00:23:35 +03:00
|
|
|
# define COMPARE_AND_RETURN(x,y) do if (x != y) return BOX(x - y); while (0)
|
|
|
|
|
if (UNBOXED(p)) {
|
|
|
|
|
if (UNBOXED(q)) return BOX(UNBOX(p) - UNBOX(q));
|
|
|
|
|
else return BOX(-1);
|
|
|
|
|
}
|
|
|
|
|
else if (UNBOXED(q)) return BOX(1);
|
|
|
|
|
else {
|
|
|
|
|
data *a = TO_DATA(p), *b = TO_DATA(q);
|
|
|
|
|
int ta = TAG(a->tag), tb = TAG(b->tag);
|
|
|
|
|
int la = LEN(a->tag), lb = LEN(b->tag);
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
COMPARE_AND_RETURN (ta, tb);
|
|
|
|
|
|
|
|
|
|
switch (ta) {
|
|
|
|
|
case STRING_TAG:
|
|
|
|
|
return BOX(strcmp (a->contents, b->contents));
|
|
|
|
|
|
|
|
|
|
case CLOSURE_TAG:
|
|
|
|
|
COMPARE_AND_RETURN (((void**) a->contents)[0], ((void**) b->contents)[0]);
|
|
|
|
|
COMPARE_AND_RETURN (la, lb);
|
|
|
|
|
i = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ARRAY_TAG:
|
|
|
|
|
COMPARE_AND_RETURN (la, lb);
|
|
|
|
|
i = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEXP_TAG: {
|
|
|
|
|
#ifndef DEBUG_PRINT
|
|
|
|
|
int ta = TO_SEXP(p)->tag, tb = TO_SEXP(q)->tag;
|
|
|
|
|
#else
|
|
|
|
|
int ta = GET_SEXP_TAG(TO_SEXP(p)->tag), tb = GET_SEXP_TAG(TO_SEXP(q)->tag);
|
|
|
|
|
#endif
|
|
|
|
|
COMPARE_AND_RETURN (ta, tb);
|
|
|
|
|
COMPARE_AND_RETURN (la, lb);
|
|
|
|
|
i = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
2019-12-21 02:34:56 +03:00
|
|
|
failure ("invalid tag %d in compare *****\n", ta);
|
2019-12-20 00:23:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (; i<la; i++) {
|
|
|
|
|
int c = Lcompare (((void**) a->contents)[i], ((void**) b->contents)[i]);
|
|
|
|
|
if (c != BOX(0)) return BOX(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BOX(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
extern void* Belem (void *p, int i) {
|
2018-12-06 23:04:45 +03:00
|
|
|
data *a = (data *)BOX(NULL);
|
|
|
|
|
a = TO_DATA(p);
|
2018-10-25 03:15:24 +03:00
|
|
|
i = UNBOX(i);
|
|
|
|
|
|
|
|
|
|
if (TAG(a->tag) == STRING_TAG) {
|
|
|
|
|
return (void*) BOX(a->contents[i]);
|
|
|
|
|
}
|
2018-05-16 09:24:40 +03:00
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
return (void*) ((int*) a->contents)[i];
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
extern void* LmakeString (int length) {
|
|
|
|
|
int n = UNBOX(length);
|
|
|
|
|
data *r;
|
2018-12-06 23:04:45 +03:00
|
|
|
|
2018-12-11 10:22:23 +03:00
|
|
|
__pre_gc () ;
|
|
|
|
|
|
2018-12-06 22:53:04 +03:00
|
|
|
r = (data*) alloc (n + 1 + sizeof (int));
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2018-11-29 18:27:59 +03:00
|
|
|
r->tag = STRING_TAG | (n << 3);
|
2018-12-12 12:42:04 +03:00
|
|
|
|
|
|
|
|
__post_gc();
|
2018-04-30 17:18:41 +03:00
|
|
|
|
|
|
|
|
return r->contents;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
extern void* Bstring (void *p) {
|
|
|
|
|
int n = strlen (p);
|
|
|
|
|
void *s;
|
|
|
|
|
|
|
|
|
|
__pre_gc ();
|
|
|
|
|
|
|
|
|
|
s = LmakeString (BOX(n));
|
|
|
|
|
strncpy (s, p, n + 1);
|
|
|
|
|
|
|
|
|
|
__post_gc ();
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void* Lstringcat (void *p) {
|
|
|
|
|
void *s;
|
|
|
|
|
|
|
|
|
|
__pre_gc ();
|
|
|
|
|
|
|
|
|
|
createStringBuf ();
|
|
|
|
|
stringcat (p);
|
|
|
|
|
|
|
|
|
|
s = Bstring (stringBuf.contents);
|
|
|
|
|
|
|
|
|
|
deleteStringBuf ();
|
|
|
|
|
|
|
|
|
|
__post_gc ();
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
extern void* Bstringval (void *p) {
|
2019-04-24 17:34:53 +03:00
|
|
|
void *s = (void *) BOX (NULL);
|
2018-12-12 12:42:04 +03:00
|
|
|
|
|
|
|
|
__pre_gc () ;
|
2018-10-31 20:10:50 +03:00
|
|
|
|
|
|
|
|
createStringBuf ();
|
|
|
|
|
printValue (p);
|
|
|
|
|
|
|
|
|
|
s = Bstring (stringBuf.contents);
|
|
|
|
|
|
|
|
|
|
deleteStringBuf ();
|
|
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
__post_gc ();
|
|
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
extern void* Bclosure (int n, void *entry, ...) {
|
|
|
|
|
va_list args = (va_list) BOX (NULL);
|
|
|
|
|
int i = BOX(0),
|
|
|
|
|
ai = BOX(0);
|
|
|
|
|
data *r = (data*) BOX (NULL);
|
|
|
|
|
|
|
|
|
|
__pre_gc ();
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_PRINT
|
|
|
|
|
printf ("Bclosure: create n = %d\n", n); fflush(stdout);
|
|
|
|
|
#endif
|
|
|
|
|
r = (data*) alloc (sizeof(int) * (n+2));
|
|
|
|
|
|
2019-10-16 22:01:57 +03:00
|
|
|
r->tag = CLOSURE_TAG | ((n + 1) << 3);
|
2019-10-16 01:13:52 +03:00
|
|
|
((void**) r->contents)[0] = entry;
|
|
|
|
|
|
|
|
|
|
va_start(args, n);
|
|
|
|
|
|
2019-10-16 21:07:27 +03:00
|
|
|
for (i = 0; i<n; i++) {
|
2019-10-16 01:13:52 +03:00
|
|
|
ai = va_arg(args, int);
|
2019-10-16 21:07:27 +03:00
|
|
|
((int*)r->contents)[i+1] = ai;
|
2019-10-16 01:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
__post_gc();
|
|
|
|
|
|
|
|
|
|
return r->contents;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
extern void* Barray (int n, ...) {
|
2018-12-06 23:04:45 +03:00
|
|
|
va_list args = (va_list) BOX (NULL);
|
|
|
|
|
int i = BOX(0),
|
|
|
|
|
ai = BOX(0);
|
|
|
|
|
data *r = (data*) BOX (NULL);
|
2018-12-11 10:22:23 +03:00
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
__pre_gc ();
|
|
|
|
|
|
2018-12-05 19:06:07 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("Barray: create n = %d\n", n); fflush(stdout);
|
2018-12-05 19:06:07 +03:00
|
|
|
#endif
|
2018-12-05 18:31:12 +03:00
|
|
|
r = (data*) alloc (sizeof(int) * (n+1));
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2018-11-29 18:27:59 +03:00
|
|
|
r->tag = ARRAY_TAG | (n << 3);
|
2018-04-30 17:18:41 +03:00
|
|
|
|
|
|
|
|
va_start(args, n);
|
|
|
|
|
|
2018-12-06 23:04:45 +03:00
|
|
|
for (i = 0; i<n; i++) {
|
|
|
|
|
ai = va_arg(args, int);
|
2019-04-11 16:24:57 +03:00
|
|
|
((int*)r->contents)[i] = ai;
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
__post_gc();
|
2019-04-11 16:24:57 +03:00
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
return r->contents;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
extern void* Bsexp (int n, ...) {
|
2018-12-06 23:04:45 +03:00
|
|
|
va_list args = (va_list) BOX (NULL);
|
2018-12-03 17:01:32 +03:00
|
|
|
int i = BOX(0);
|
2018-12-12 12:42:04 +03:00
|
|
|
int ai = BOX(0);
|
|
|
|
|
size_t * p = NULL;
|
|
|
|
|
sexp *r = (sexp*) BOX (NULL);
|
2019-04-24 17:34:53 +03:00
|
|
|
data *d = (data *) BOX (NULL);
|
2018-11-29 18:27:59 +03:00
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
__pre_gc () ;
|
2018-12-11 10:22:23 +03:00
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf("Bsexp: allocate %zu!\n",sizeof(int) * (n+1)); fflush (stdout);
|
2018-12-12 12:42:04 +03:00
|
|
|
#endif
|
|
|
|
|
r = (sexp*) alloc (sizeof(int) * (n+1));
|
|
|
|
|
d = &(r->contents);
|
2018-12-12 19:32:46 +03:00
|
|
|
r->tag = 0;
|
2018-12-12 12:42:04 +03:00
|
|
|
|
|
|
|
|
d->tag = SEXP_TAG | ((n-1) << 3);
|
2018-05-16 09:24:40 +03:00
|
|
|
|
|
|
|
|
va_start(args, n);
|
|
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
for (i=0; i<n-1; i++) {
|
2018-12-03 17:01:32 +03:00
|
|
|
ai = va_arg(args, int);
|
2018-12-12 19:32:46 +03:00
|
|
|
|
|
|
|
|
p = (size_t*) ai;
|
2018-12-12 12:42:04 +03:00
|
|
|
((int*)d->contents)[i] = ai;
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
r->tag = va_arg(args, int);
|
2019-04-24 16:02:29 +03:00
|
|
|
|
2019-04-24 17:34:53 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 16:02:29 +03:00
|
|
|
r->tag = SEXP_TAG | ((r->tag) << 3);
|
2019-04-24 17:34:53 +03:00
|
|
|
#endif
|
|
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
va_end(args);
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
__post_gc();
|
|
|
|
|
|
|
|
|
|
return d->contents;
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-11-05 20:17:11 +03:00
|
|
|
extern int Btag (void *d, int t, int n) {
|
2019-04-24 17:34:53 +03:00
|
|
|
data *r = (data *) BOX (NULL);
|
2019-08-21 18:47:25 +03:00
|
|
|
if (UNBOXED(d)) return BOX(0);
|
|
|
|
|
else {
|
|
|
|
|
r = TO_DATA(d);
|
2019-04-24 17:34:53 +03:00
|
|
|
#ifndef DEBUG_PRINT
|
2019-08-21 18:47:25 +03:00
|
|
|
return BOX(TAG(r->tag) == SEXP_TAG && TO_SEXP(d)->tag == t && LEN(r->tag) == n);
|
2019-04-24 17:34:53 +03:00
|
|
|
#else
|
2019-08-21 18:47:25 +03:00
|
|
|
return BOX(TAG(r->tag) == SEXP_TAG &&
|
2019-04-24 17:34:53 +03:00
|
|
|
GET_SEXP_TAG(TO_SEXP(d)->tag) == t && LEN(r->tag) == n);
|
|
|
|
|
#endif
|
2019-08-21 18:47:25 +03:00
|
|
|
}
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
2018-11-06 00:21:38 +03:00
|
|
|
|
|
|
|
|
extern int Barray_patt (void *d, int n) {
|
2018-12-06 23:04:45 +03:00
|
|
|
data *r = BOX(NULL);
|
2018-11-06 00:21:38 +03:00
|
|
|
if (UNBOXED(d)) return BOX(0);
|
|
|
|
|
else {
|
2018-12-06 23:04:45 +03:00
|
|
|
r = TO_DATA(d);
|
2018-11-06 00:21:38 +03:00
|
|
|
return BOX(TAG(r->tag) == ARRAY_TAG && LEN(r->tag) == n);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Bstring_patt (void *x, void *y) {
|
2018-12-06 23:04:45 +03:00
|
|
|
data *rx = (data *) BOX (NULL),
|
|
|
|
|
*ry = (data *) BOX (NULL);
|
2018-11-06 00:21:38 +03:00
|
|
|
if (UNBOXED(x)) return BOX(0);
|
|
|
|
|
else {
|
2018-12-06 23:04:45 +03:00
|
|
|
rx = TO_DATA(x); ry = TO_DATA(y);
|
2018-11-06 00:21:38 +03:00
|
|
|
|
|
|
|
|
if (TAG(rx->tag) != STRING_TAG) return BOX(0);
|
|
|
|
|
|
|
|
|
|
return BOX(strcmp (rx->contents, ry->contents) == 0 ? 1 : 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
extern int Bclosure_tag_patt (void *x) {
|
|
|
|
|
if (UNBOXED(x)) return BOX(0);
|
|
|
|
|
|
|
|
|
|
return BOX(TAG(TO_DATA(x)->tag) == CLOSURE_TAG);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 00:21:38 +03:00
|
|
|
extern int Bboxed_patt (void *x) {
|
|
|
|
|
return BOX(UNBOXED(x) ? 0 : 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Bunboxed_patt (void *x) {
|
|
|
|
|
return BOX(UNBOXED(x) ? 1 : 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Barray_tag_patt (void *x) {
|
|
|
|
|
if (UNBOXED(x)) return BOX(0);
|
|
|
|
|
|
|
|
|
|
return BOX(TAG(TO_DATA(x)->tag) == ARRAY_TAG);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Bstring_tag_patt (void *x) {
|
|
|
|
|
if (UNBOXED(x)) return BOX(0);
|
|
|
|
|
|
|
|
|
|
return BOX(TAG(TO_DATA(x)->tag) == STRING_TAG);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Bsexp_tag_patt (void *x) {
|
|
|
|
|
if (UNBOXED(x)) return BOX(0);
|
|
|
|
|
|
|
|
|
|
return BOX(TAG(TO_DATA(x)->tag) == SEXP_TAG);
|
|
|
|
|
}
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2019-04-11 16:24:57 +03:00
|
|
|
extern void* Bsta (void *v, int i, void *x) {
|
2019-04-10 22:15:08 +03:00
|
|
|
if (TAG(TO_DATA(x)->tag) == STRING_TAG)((char*) x)[UNBOX(i)] = (char) UNBOX(v);
|
|
|
|
|
else ((int*) x)[UNBOX(i)] = v;
|
2019-04-11 16:24:57 +03:00
|
|
|
|
|
|
|
|
return v;
|
2019-04-10 22:15:08 +03:00
|
|
|
}
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
static void fix_unboxed (char *s, va_list va) {
|
|
|
|
|
size_t *p = va;
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
while (*s) {
|
|
|
|
|
if (*s == '%') {
|
|
|
|
|
size_t n = p [i];
|
|
|
|
|
if (UNBOXED (n)) {
|
|
|
|
|
p[i] = UNBOX(n);
|
|
|
|
|
}
|
|
|
|
|
i++;
|
2019-12-18 18:44:01 +03:00
|
|
|
}
|
2019-12-21 02:34:56 +03:00
|
|
|
s++;
|
2019-12-18 18:44:01 +03:00
|
|
|
}
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void Lfailure (char *s, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
|
|
va_start (args, s);
|
|
|
|
|
fix_unboxed (s, args);
|
|
|
|
|
vfailure (s, args);
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2019-12-18 18:44:01 +03:00
|
|
|
extern void* /*Lstrcat*/ i__Infix_4343 (void *a, void *b) {
|
2018-12-06 23:04:45 +03:00
|
|
|
data *da = (data*) BOX (NULL);
|
|
|
|
|
data *db = (data*) BOX (NULL);
|
|
|
|
|
data *d = (data*) BOX (NULL);
|
|
|
|
|
|
|
|
|
|
da = TO_DATA(a);
|
|
|
|
|
db = TO_DATA(b);
|
|
|
|
|
|
2018-12-11 10:22:23 +03:00
|
|
|
__pre_gc () ;
|
|
|
|
|
|
2018-12-06 23:04:45 +03:00
|
|
|
d = (data *) alloc (sizeof(int) + LEN(da->tag) + LEN(db->tag) + 1);
|
2018-05-25 09:53:10 +03:00
|
|
|
|
|
|
|
|
d->tag = LEN(da->tag) + LEN(db->tag);
|
|
|
|
|
|
|
|
|
|
strcpy (d->contents, da->contents);
|
|
|
|
|
strcat (d->contents, db->contents);
|
|
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
__post_gc();
|
|
|
|
|
|
2018-05-25 09:53:10 +03:00
|
|
|
return d->contents;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
extern void* Lsprintf (char * fmt, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
void *s;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
|
fix_unboxed (fmt, args);
|
|
|
|
|
|
|
|
|
|
createStringBuf ();
|
|
|
|
|
|
|
|
|
|
vprintStringBuf (fmt, args);
|
|
|
|
|
|
|
|
|
|
__pre_gc ();
|
|
|
|
|
|
|
|
|
|
s = Bstring (stringBuf.contents);
|
|
|
|
|
|
|
|
|
|
__post_gc ();
|
|
|
|
|
|
|
|
|
|
deleteStringBuf ();
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-23 23:18:00 +03:00
|
|
|
extern void Lfprintf (FILE *f, char *s, ...) {
|
2018-12-06 23:04:45 +03:00
|
|
|
va_list args = (va_list) BOX (NULL);
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
va_start (args, s);
|
|
|
|
|
fix_unboxed (s, args);
|
|
|
|
|
|
|
|
|
|
if (vfprintf (f, s, args) < 0) {
|
|
|
|
|
failure ("fprintf (...): %s\n", strerror (errno));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void Lprintf (char *s, ...) {
|
|
|
|
|
va_list args = (va_list) BOX (NULL);
|
|
|
|
|
|
|
|
|
|
va_start (args, s);
|
|
|
|
|
fix_unboxed (s, args);
|
|
|
|
|
|
|
|
|
|
if (vprintf (s, args) < 0) {
|
|
|
|
|
failure ("fprintf (...): %s\n", strerror (errno));
|
|
|
|
|
}
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-23 23:18:00 +03:00
|
|
|
extern FILE* Lfopen (char *f, char *m) {
|
2019-12-21 02:34:56 +03:00
|
|
|
FILE* h = fopen (f, m);
|
|
|
|
|
|
|
|
|
|
if (h)
|
|
|
|
|
return h;
|
|
|
|
|
|
|
|
|
|
failure ("fopen (\"%s\", \"%s\"): %s, %s, %s\n", f, m, strerror (errno));
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-23 23:18:00 +03:00
|
|
|
extern void Lfclose (FILE *f) {
|
2018-05-16 09:24:40 +03:00
|
|
|
fclose (f);
|
|
|
|
|
}
|
2019-12-21 02:34:56 +03:00
|
|
|
|
|
|
|
|
extern void* LreadLine () {
|
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
|
|
if (scanf ("%m[^\n]", &buf) == 1) {
|
|
|
|
|
void * s = Bstring (buf);
|
|
|
|
|
|
|
|
|
|
free (buf);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errno != 0)
|
|
|
|
|
failure ("readLine (): %s\n", strerror (errno));
|
|
|
|
|
|
|
|
|
|
return LmakeString (0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void* Lfread (char *fname) {
|
|
|
|
|
FILE *f = fopen (fname, "r");
|
|
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
|
if (fseek (f, 0l, SEEK_END) >= 0) {
|
|
|
|
|
long size = ftell (f);
|
|
|
|
|
void *s = LmakeString (size);
|
|
|
|
|
|
|
|
|
|
rewind (f);
|
|
|
|
|
|
|
|
|
|
if (fread (s, 1, size, f) == size) {
|
|
|
|
|
fclose (f);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
failure ("fread (\"%s\"): %s\n", fname, strerror (errno));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void Lfwrite (char *fname, char *contents) {
|
|
|
|
|
FILE *f = fopen (fname, "w");
|
|
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
|
if (fprintf (f, "%s", contents) < 0);
|
|
|
|
|
else {
|
|
|
|
|
fclose (f);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
failure ("fwrite (\"%s\"): %s\n", fname, strerror (errno));
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-04 23:13:08 +03:00
|
|
|
/* Lread is an implementation of the "read" construct */
|
|
|
|
|
extern int Lread () {
|
2018-12-06 23:04:45 +03:00
|
|
|
int result = BOX(0);
|
2018-03-04 23:13:08 +03:00
|
|
|
|
|
|
|
|
printf ("> ");
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
scanf ("%d", &result);
|
|
|
|
|
|
2018-10-25 03:15:24 +03:00
|
|
|
return BOX(result);
|
2018-03-04 23:13:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Lwrite is an implementation of the "write" construct */
|
|
|
|
|
extern int Lwrite (int n) {
|
2018-10-25 03:15:24 +03:00
|
|
|
printf ("%d\n", UNBOX(n));
|
2018-03-04 23:13:08 +03:00
|
|
|
fflush (stdout);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2018-11-06 16:18:09 +03:00
|
|
|
/* GC starts here */
|
|
|
|
|
|
|
|
|
|
extern const size_t __gc_data_end, __gc_data_start;
|
2018-11-21 14:23:35 +03:00
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
# ifdef __ENABLE_GC__
|
|
|
|
|
|
2018-11-21 14:23:35 +03:00
|
|
|
extern void L__gc_init ();
|
2019-10-16 01:13:52 +03:00
|
|
|
|
|
|
|
|
# else
|
|
|
|
|
|
|
|
|
|
# define L__gc_init __gc_init_subst
|
|
|
|
|
void __gc_init_subst () {}
|
|
|
|
|
|
|
|
|
|
# endif
|
|
|
|
|
|
2018-11-21 14:23:35 +03:00
|
|
|
extern void __gc_root_scan_stack ();
|
|
|
|
|
|
2018-11-29 18:27:59 +03:00
|
|
|
/* ======================================== */
|
|
|
|
|
/* Mark-and-copy */
|
|
|
|
|
/* ======================================== */
|
|
|
|
|
|
2019-12-20 00:23:35 +03:00
|
|
|
//static size_t SPACE_SIZE = 128;
|
|
|
|
|
static size_t SPACE_SIZE = 1280;
|
2018-11-29 18:27:59 +03:00
|
|
|
# define POOL_SIZE (2*SPACE_SIZE)
|
|
|
|
|
|
|
|
|
|
static void swap (size_t ** a, size_t ** b) {
|
|
|
|
|
size_t * t = *a;
|
|
|
|
|
*a = *b;
|
|
|
|
|
*b = t;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
static void gc_swap_spaces (void) {
|
2018-11-29 18:27:59 +03:00
|
|
|
swap (&from_space.begin, &to_space.begin);
|
|
|
|
|
swap (&from_space.end , &to_space.end );
|
|
|
|
|
from_space.current = current;
|
|
|
|
|
to_space.current = to_space.begin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# define IS_VALID_HEAP_POINTER(p)\
|
|
|
|
|
(!UNBOXED(p) && \
|
|
|
|
|
from_space.begin <= p && \
|
|
|
|
|
from_space.end > p)
|
|
|
|
|
|
|
|
|
|
# define IN_PASSIVE_SPACE(p) \
|
|
|
|
|
(to_space.begin <= p && \
|
|
|
|
|
to_space.end > p)
|
|
|
|
|
|
|
|
|
|
# define IS_FORWARD_PTR(p) \
|
|
|
|
|
(!UNBOXED(p) && IN_PASSIVE_SPACE(p))
|
|
|
|
|
|
|
|
|
|
extern size_t * gc_copy (size_t *obj);
|
|
|
|
|
|
|
|
|
|
static void copy_elements (size_t *where, size_t *from, int len) {
|
2018-12-06 23:04:45 +03:00
|
|
|
int i = 0;
|
2018-12-05 18:31:12 +03:00
|
|
|
void * p = NULL;
|
2018-11-29 18:27:59 +03:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
|
size_t elem = from[i];
|
2018-12-05 18:31:12 +03:00
|
|
|
if (!IS_VALID_HEAP_POINTER(elem)) {
|
|
|
|
|
*where = elem;
|
|
|
|
|
where++;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
p = gc_copy ((size_t*) elem);
|
|
|
|
|
*where = p;
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("copy_elements: fix %p: %p\n", from, *where); fflush (stdout);
|
2018-12-05 18:31:12 +03:00
|
|
|
#endif
|
|
|
|
|
where ++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void extend_spaces (void) {
|
2019-04-24 16:02:29 +03:00
|
|
|
void *p1 = (void *) BOX (NULL), *p2 = (void *) BOX (NULL);
|
|
|
|
|
size_t old_space_size = SPACE_SIZE * sizeof(size_t),
|
|
|
|
|
new_space_size = (SPACE_SIZE << 1) * sizeof(size_t);
|
|
|
|
|
p1 = mremap(from_space.begin, old_space_size, new_space_size, 0);
|
|
|
|
|
p2 = mremap(to_space.begin , old_space_size, new_space_size, 0);
|
|
|
|
|
if (p1 == MAP_FAILED || p2 == MAP_FAILED) {
|
|
|
|
|
perror("EROOR: extend_spaces: mmap failed\n");
|
2018-12-12 12:42:04 +03:00
|
|
|
exit (1);
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
2018-12-05 18:31:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 16:02:29 +03:00
|
|
|
printf ("extend: %p %p %p %p\n", p1, p2, from_space.begin, to_space.begin);
|
|
|
|
|
printf ("extend: %p %p %p\n" , from_space.end, to_space.end, current);
|
|
|
|
|
fflush (stdout);
|
2018-12-05 18:31:12 +03:00
|
|
|
#endif
|
|
|
|
|
from_space.end += SPACE_SIZE;
|
|
|
|
|
to_space.end += SPACE_SIZE;
|
2019-04-24 16:02:29 +03:00
|
|
|
SPACE_SIZE = SPACE_SIZE << 1;
|
2018-12-05 18:31:12 +03:00
|
|
|
from_space.size = SPACE_SIZE;
|
|
|
|
|
to_space.size = SPACE_SIZE;
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern size_t * gc_copy (size_t *obj) {
|
|
|
|
|
data *d = TO_DATA(obj);
|
|
|
|
|
sexp *s = NULL;
|
|
|
|
|
size_t *copy = NULL;
|
|
|
|
|
int i = 0;
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2018-11-29 18:27:59 +03:00
|
|
|
int len1, len2, len3;
|
|
|
|
|
void * objj;
|
|
|
|
|
void * newobjj = (void*)current;
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("gc_copy: %p cur = %p starts\n", obj, current);
|
|
|
|
|
fflush (stdout);
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
|
|
|
|
|
if (!IS_VALID_HEAP_POINTER(obj)) {
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("gc_copy: invalid ptr: %p\n", obj); fflush (stdout);
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 10:22:23 +03:00
|
|
|
if (!IN_PASSIVE_SPACE(current) && current != to_space.end) {
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf("ERROR: gc_copy: out-of-space %p %p %p\n", current, to_space.begin, to_space.end);
|
2018-11-29 18:27:59 +03:00
|
|
|
fflush(stdout);
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
perror("ERROR: gc_copy: out-of-space\n");
|
2018-12-12 12:42:04 +03:00
|
|
|
exit (1);
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
2018-12-05 18:31:12 +03:00
|
|
|
if (IS_FORWARD_PTR(d->tag)) {
|
2018-12-05 19:06:07 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("gc_copy: IS_FORWARD_PTR: return! %p\n", (size_t *) d->tag);
|
2018-12-05 18:31:12 +03:00
|
|
|
fflush(stdout);
|
2018-12-05 19:06:07 +03:00
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
return (size_t *) d->tag;
|
2018-12-05 18:31:12 +03:00
|
|
|
}
|
2018-11-29 18:27:59 +03:00
|
|
|
|
|
|
|
|
copy = current;
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2018-11-29 18:27:59 +03:00
|
|
|
objj = d;
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
switch (TAG(d->tag)) {
|
2019-10-16 01:13:52 +03:00
|
|
|
case CLOSURE_TAG:
|
|
|
|
|
#ifdef DEBUG_PRINT
|
|
|
|
|
printf ("gc_copy:closure_tag; len = %zu\n", LEN(d->tag)); fflush (stdout);
|
|
|
|
|
#endif
|
|
|
|
|
current += (LEN(d->tag) + 1) * sizeof (int);
|
|
|
|
|
*copy = d->tag;
|
|
|
|
|
copy++;
|
|
|
|
|
i = LEN(d->tag) - 1;
|
2019-10-16 22:01:57 +03:00
|
|
|
d->tag = (int) copy;
|
|
|
|
|
*copy = obj[0];
|
|
|
|
|
copy_elements (copy, obj++, i);
|
2019-10-16 01:13:52 +03:00
|
|
|
break;
|
|
|
|
|
|
2018-11-29 18:27:59 +03:00
|
|
|
case ARRAY_TAG:
|
2018-12-05 19:06:07 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("gc_copy:array_tag; len = %zu\n", LEN(d->tag)); fflush (stdout);
|
2018-12-05 19:06:07 +03:00
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
current += (LEN(d->tag) + 1) * sizeof (int);
|
|
|
|
|
*copy = d->tag;
|
|
|
|
|
copy++;
|
2018-12-12 12:42:04 +03:00
|
|
|
i = LEN(d->tag);
|
2018-11-29 18:27:59 +03:00
|
|
|
d->tag = (int) copy;
|
2018-12-12 12:42:04 +03:00
|
|
|
copy_elements (copy, obj, i);
|
2018-11-29 18:27:59 +03:00
|
|
|
break;
|
2018-12-06 22:53:04 +03:00
|
|
|
|
2018-11-29 18:27:59 +03:00
|
|
|
case STRING_TAG:
|
2018-12-05 19:06:07 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("gc_copy:string_tag; len = %d\n", LEN(d->tag) + 1); fflush (stdout);
|
2018-12-05 19:06:07 +03:00
|
|
|
#endif
|
2018-12-06 22:53:04 +03:00
|
|
|
current += LEN(d->tag) * sizeof(char) + sizeof (int);
|
2018-11-29 18:27:59 +03:00
|
|
|
*copy = d->tag;
|
|
|
|
|
copy++;
|
|
|
|
|
d->tag = (int) copy;
|
2018-12-12 19:32:46 +03:00
|
|
|
strcpy (©[0], (char*) obj);
|
2018-11-29 18:27:59 +03:00
|
|
|
break;
|
2018-12-06 22:53:04 +03:00
|
|
|
|
|
|
|
|
case SEXP_TAG :
|
2018-11-29 18:27:59 +03:00
|
|
|
s = TO_SEXP(obj);
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2018-11-29 18:27:59 +03:00
|
|
|
objj = s;
|
|
|
|
|
len1 = LEN(s->contents.tag);
|
|
|
|
|
len2 = LEN(s->tag);
|
|
|
|
|
len3 = LEN(d->tag);
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("len1 = %li, len2=%li, len3 = %li\n", len1, len2, len3);
|
|
|
|
|
fflush (stdout);
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
current += (LEN(s->contents.tag) + 2) * sizeof (int);
|
|
|
|
|
*copy = s->tag;
|
|
|
|
|
copy++;
|
|
|
|
|
*copy = s->contents.tag;
|
|
|
|
|
copy++;
|
2018-11-30 16:18:12 +03:00
|
|
|
i = LEN(s->contents.tag);
|
2018-12-05 18:31:12 +03:00
|
|
|
d->tag = (int) copy;
|
2018-11-30 16:18:12 +03:00
|
|
|
copy_elements (copy, obj, i);
|
2018-11-29 18:27:59 +03:00
|
|
|
break;
|
2018-12-06 22:53:04 +03:00
|
|
|
|
2018-11-29 18:27:59 +03:00
|
|
|
default:
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("ERROR: gc_copy: weird tag: %p", TAG(d->tag)); fflush (stdout);
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
perror ("ERROR: gc_copy: weird tag");
|
2019-04-24 17:34:53 +03:00
|
|
|
exit (1);
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("gc_copy: %p(%p) -> %p (%p); new-current = %p\n", obj, objj, copy, newobjj, current);
|
2019-04-24 16:46:58 +03:00
|
|
|
fflush (stdout);
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
return copy;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
extern void gc_test_and_copy_root (size_t ** root) {
|
2018-12-05 18:31:12 +03:00
|
|
|
if (IS_VALID_HEAP_POINTER(*root)) {
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("gc_test_and_copy_root: root %p %p\n", root, *root); fflush (stdout);
|
2018-12-05 18:31:12 +03:00
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
*root = gc_copy (*root);
|
2018-12-05 18:31:12 +03:00
|
|
|
}
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
extern void gc_root_scan_data (void) {
|
2018-11-29 18:27:59 +03:00
|
|
|
size_t * p = &__gc_data_start;
|
|
|
|
|
while (p != &__gc_data_end) {
|
2018-12-12 12:42:04 +03:00
|
|
|
gc_test_and_copy_root (p);
|
2018-11-29 18:27:59 +03:00
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void init_pool (void) {
|
2019-04-24 16:02:29 +03:00
|
|
|
size_t space_size = SPACE_SIZE * sizeof(size_t);
|
|
|
|
|
from_space.begin = mmap (NULL, space_size, PROT_READ | PROT_WRITE,
|
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
|
|
|
|
|
to_space.begin = mmap (NULL, space_size, PROT_READ | PROT_WRITE,
|
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
|
|
|
|
|
if (to_space.begin == MAP_FAILED || from_space.begin == MAP_FAILED) {
|
|
|
|
|
perror ("EROOR: init_pool: mmap failed\n");
|
|
|
|
|
exit (1);
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
|
|
|
|
from_space.current = from_space.begin;
|
|
|
|
|
from_space.end = from_space.begin + SPACE_SIZE;
|
|
|
|
|
from_space.size = SPACE_SIZE;
|
|
|
|
|
to_space.current = to_space.begin;
|
|
|
|
|
to_space.end = to_space.begin + SPACE_SIZE;
|
|
|
|
|
to_space.size = SPACE_SIZE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int free_pool (pool * p) {
|
|
|
|
|
return munmap((void *)p->begin, p->size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void * gc (size_t size) {
|
|
|
|
|
current = to_space.begin;
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("\ngc: current:%p; to_space.b =%p; to_space.e =%p; f_space.b = %p; f_space.e = %p\n",
|
|
|
|
|
current, to_space.begin, to_space.end, from_space.begin, from_space.end);
|
|
|
|
|
fflush (stdout);
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2019-04-24 17:34:53 +03:00
|
|
|
gc_root_scan_data ();
|
2018-12-05 18:31:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("gc: data is scanned\n"); fflush (stdout);
|
2018-12-05 18:31:12 +03:00
|
|
|
#endif
|
2018-11-21 14:23:35 +03:00
|
|
|
__gc_root_scan_stack ();
|
2018-11-29 18:27:59 +03:00
|
|
|
if (!IN_PASSIVE_SPACE(current)) {
|
|
|
|
|
perror ("ASSERT: !IN_PASSIVE_SPACE(current)\n");
|
2019-04-24 17:34:53 +03:00
|
|
|
exit (1);
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
2018-12-12 19:39:29 +03:00
|
|
|
while (current + size >= to_space.end) {
|
2018-12-05 18:31:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("gc pre-extend_spaces : %p %zu %p \n", current, size, to_space.end);
|
|
|
|
|
fflush (stdout);
|
2018-12-05 18:31:12 +03:00
|
|
|
#endif
|
|
|
|
|
extend_spaces ();
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("gc post-extend_spaces: %p %zu %p \n", current, size, to_space.end);
|
|
|
|
|
fflush (stdout);
|
2018-12-05 18:31:12 +03:00
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
2018-12-12 19:39:29 +03:00
|
|
|
assert (IN_PASSIVE_SPACE(current));
|
|
|
|
|
assert (current + size < to_space.end);
|
2018-11-21 14:23:35 +03:00
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
gc_swap_spaces ();
|
2018-11-29 18:27:59 +03:00
|
|
|
from_space.current = current + size;
|
2018-12-12 12:42:04 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("gc: end: (allocate!) return %p; from_space.current %p; from_space.end %p \n\n",
|
|
|
|
|
current, from_space.current, from_space.end);
|
|
|
|
|
fflush (stdout);
|
2018-12-12 12:42:04 +03:00
|
|
|
#endif
|
2018-12-11 10:22:23 +03:00
|
|
|
return (void *) current;
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
2019-04-24 17:34:53 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 16:02:29 +03:00
|
|
|
static void printFromSpace (void) {
|
|
|
|
|
size_t * cur = from_space.begin, *tmp = NULL;
|
|
|
|
|
data * d = NULL;
|
|
|
|
|
sexp * s = NULL;
|
|
|
|
|
size_t len = 0;
|
2019-04-24 17:34:53 +03:00
|
|
|
|
2019-04-24 16:02:29 +03:00
|
|
|
printf ("\nHEAP SNAPSHOT\n===================\n");
|
|
|
|
|
printf ("f_begin = %p, f_end = %p,\n", from_space.begin, from_space.end);
|
|
|
|
|
while (cur < from_space.current) {
|
|
|
|
|
printf ("data at %p", cur);
|
|
|
|
|
d = (data *) cur;
|
|
|
|
|
|
|
|
|
|
switch (TAG(d->tag)) {
|
|
|
|
|
|
|
|
|
|
case STRING_TAG:
|
|
|
|
|
printf ("(=>%p): STRING\n\t%s\n", d->contents, d->contents);
|
|
|
|
|
len = LEN(d->tag) + 1;
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
break;
|
|
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
case CLOSURE_TAG:
|
|
|
|
|
printf ("(=>%p): CLOSURE\n\t", d->contents);
|
|
|
|
|
len = LEN(d->tag);
|
2019-10-16 22:01:57 +03:00
|
|
|
for (int i = 0; i < len; i++) {
|
2019-10-16 01:13:52 +03:00
|
|
|
int elem = ((int*)d->contents)[i];
|
|
|
|
|
if (UNBOXED(elem)) printf ("%d ", elem);
|
|
|
|
|
else printf ("%p ", elem);
|
|
|
|
|
}
|
|
|
|
|
len += 1;
|
|
|
|
|
printf ("\n");
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
break;
|
|
|
|
|
|
2019-04-24 16:02:29 +03:00
|
|
|
case ARRAY_TAG:
|
|
|
|
|
printf ("(=>%p): ARRAY\n\t", d->contents);
|
|
|
|
|
len = LEN(d->tag);
|
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
|
int elem = ((int*)d->contents)[i];
|
|
|
|
|
if (UNBOXED(elem)) printf ("%d ", elem);
|
|
|
|
|
else printf ("%p ", elem);
|
|
|
|
|
}
|
|
|
|
|
len += 1;
|
|
|
|
|
printf ("\n");
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEXP_TAG:
|
|
|
|
|
s = (sexp *) d;
|
|
|
|
|
d = (data *) &(s->contents);
|
2019-04-24 17:34:53 +03:00
|
|
|
char * tag = de_hash (GET_SEXP_TAG(s->tag));
|
2019-04-24 16:02:29 +03:00
|
|
|
printf ("(=>%p): SEXP\n\ttag(%s) ", s->contents.contents, tag);
|
|
|
|
|
len = LEN(d->tag);
|
|
|
|
|
tmp = (s->contents.contents);
|
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
|
int elem = ((int*)tmp)[i];
|
|
|
|
|
if (UNBOXED(elem)) printf ("%d ", UNBOX(elem));
|
|
|
|
|
else printf ("%p ", elem);
|
|
|
|
|
}
|
|
|
|
|
len += 2;
|
|
|
|
|
printf ("\n");
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
|
printf ("\nprintFromSpace: end!\n===================\n\n");
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
printf ("\nprintFromSpace: ERROR: bad tag %d", TAG(d->tag));
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
cur += len * sizeof(int);
|
|
|
|
|
printf ("len = %zu, new cur = %p\n", len, cur);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-24 17:34:53 +03:00
|
|
|
#endif
|
2019-04-24 16:02:29 +03:00
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
#ifdef __ENABLE_GC__
|
2018-12-11 10:22:23 +03:00
|
|
|
extern void * alloc (size_t size) {
|
|
|
|
|
void * p = (void*)BOX(NULL);
|
2018-11-29 18:27:59 +03:00
|
|
|
if (from_space.current + size < from_space.end) {
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("alloc: current: %p %zu", from_space.current, size); fflush (stdout);
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2018-12-03 17:01:32 +03:00
|
|
|
p = (void*) from_space.current;
|
2018-11-29 18:27:59 +03:00
|
|
|
from_space.current += size;
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf (";new current: %p \n", from_space.current); fflush (stdout);
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
return p;
|
|
|
|
|
}
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("alloc: call gc: %zu\n", size); fflush (stdout);
|
|
|
|
|
printFromSpace(); fflush (stdout);
|
2019-04-24 16:02:29 +03:00
|
|
|
#endif
|
|
|
|
|
p = gc (size);
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2019-04-24 17:34:53 +03:00
|
|
|
printf("gc END\n\n"); fflush (stdout);
|
|
|
|
|
printFromSpace(); fflush (stdout);
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2019-04-24 17:34:53 +03:00
|
|
|
return gc (size);
|
2018-11-06 16:18:09 +03:00
|
|
|
}
|
2019-10-16 01:13:52 +03:00
|
|
|
# endif
|