2018-03-04 23:13:08 +03:00
|
|
|
/* Runtime library */
|
|
|
|
|
|
2021-09-28 03:02:05 +03:00
|
|
|
# define _GNU_SOURCE 1
|
|
|
|
|
|
|
|
|
|
# include "runtime.h"
|
2023-03-27 10:09:54 +02:00
|
|
|
# include "runtime_common.h"
|
|
|
|
|
# include "gc.h"
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2022-01-31 23:46:18 +03:00
|
|
|
# define __ENABLE_GC__
|
2019-10-16 01:13:52 +03:00
|
|
|
# ifndef __ENABLE_GC__
|
|
|
|
|
# define alloc malloc
|
|
|
|
|
# endif
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
//# define DEBUG_PRINT 1
|
2019-10-16 01:13:52 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
/* GC memory_chunk structure and data; declared here in order to allow debug print */
|
|
|
|
|
static memory_chunk from_space;
|
|
|
|
|
static memory_chunk to_space;
|
2019-04-24 16:02:29 +03:00
|
|
|
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
|
|
|
|
2020-01-04 21:50:14 +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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 03:02:05 +03:00
|
|
|
void failure (char *s, ...) {
|
2020-01-04 21:50:14 +03:00
|
|
|
va_list args;
|
|
|
|
|
|
|
|
|
|
va_start (args, s);
|
|
|
|
|
vfailure (s, args);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-25 01:22:56 +03:00
|
|
|
void Lassert (void *f, char *s, ...) {
|
|
|
|
|
if (!UNBOX(f)) {
|
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
|
|
va_start (args, s);
|
|
|
|
|
vfailure (s, args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 17:05:53 +03:00
|
|
|
# define ASSERT_BOXED(memo, x) \
|
|
|
|
|
do if (UNBOXED(x)) failure ("boxed value expected in %s\n", memo); while (0)
|
|
|
|
|
# define ASSERT_UNBOXED(memo, x) \
|
|
|
|
|
do if (!UNBOXED(x)) failure ("unboxed value expected in %s\n", memo); while (0)
|
|
|
|
|
# define ASSERT_STRING(memo, x) \
|
2023-03-27 10:09:54 +02:00
|
|
|
do if (!UNBOXED(x) && TAG(TO_DATA(x)->data_header) \
|
2020-09-01 17:23:36 +03:00
|
|
|
!= STRING_TAG) failure ("string value expected in %s\n", memo); while (0)
|
2018-10-25 03:15:24 +03:00
|
|
|
|
2020-09-01 20:31:34 +03:00
|
|
|
extern void* alloc (size_t);
|
|
|
|
|
extern void* Bsexp (int n, ...);
|
|
|
|
|
extern int LtagHash (char*);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2020-01-26 07:58:11 +03:00
|
|
|
void *global_sysargs;
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
// Gets a raw data_header
|
2020-08-10 20:55:10 +03:00
|
|
|
extern int LkindOf (void *p) {
|
2020-08-04 15:48:20 +03:00
|
|
|
if (UNBOXED(p)) return UNBOXED_TAG;
|
2020-08-02 23:56:21 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
return TAG(TO_DATA(p)->data_header);
|
2020-08-02 23:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
2020-08-04 15:48:20 +03:00
|
|
|
// Compare sexprs tags
|
|
|
|
|
extern int LcompareTags (void *p, void *q) {
|
|
|
|
|
data *pd, *qd;
|
|
|
|
|
|
|
|
|
|
ASSERT_BOXED ("compareTags, 0", p);
|
|
|
|
|
ASSERT_BOXED ("compareTags, 1", q);
|
|
|
|
|
|
|
|
|
|
pd = TO_DATA(p);
|
|
|
|
|
qd = TO_DATA(q);
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
if (TAG(pd->data_header) == SEXP_TAG && TAG(qd->data_header) == SEXP_TAG) {
|
2020-08-04 15:48:20 +03:00
|
|
|
return
|
|
|
|
|
#ifndef DEBUG_PRINT
|
2020-08-07 21:40:51 +03:00
|
|
|
BOX((TO_SEXP(p)->tag) - (TO_SEXP(q)->tag));
|
2020-08-04 15:48:20 +03:00
|
|
|
#else
|
2023-03-27 10:09:54 +02:00
|
|
|
BOX((GET_SEXP_TAG(TO_SEXP(p)->data_header)) - (GET_SEXP_TAG(TO_SEXP(p)->data_header)));
|
2020-08-04 15:48:20 +03:00
|
|
|
#endif
|
|
|
|
|
}
|
2023-03-27 10:09:54 +02:00
|
|
|
else failure ("not a sexpr in compareTags: %d, %d\n", TAG(pd->data_header), TAG(qd->data_header));
|
2020-08-04 15:48:20 +03:00
|
|
|
|
|
|
|
|
return 0; // never happens
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-14 05:15:19 +03:00
|
|
|
// Functional synonym for built-in operator ":";
|
|
|
|
|
void* Ls__Infix_58 (void *p, void *q) {
|
|
|
|
|
void *res;
|
|
|
|
|
|
|
|
|
|
__pre_gc ();
|
|
|
|
|
|
2020-02-07 19:42:24 +03:00
|
|
|
push_extra_root(&p);
|
|
|
|
|
push_extra_root(&q);
|
2020-09-01 20:31:34 +03:00
|
|
|
res = Bsexp (BOX(3), p, q, LtagHash ("cons")); //BOX(848787));
|
2020-02-07 19:42:24 +03:00
|
|
|
pop_extra_root(&q);
|
|
|
|
|
pop_extra_root(&p);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
|
|
|
|
__post_gc ();
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "!!";
|
|
|
|
|
int Ls__Infix_3333 (void *p, void *q) {
|
|
|
|
|
ASSERT_UNBOXED("captured !!:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured !!:2", q);
|
|
|
|
|
|
|
|
|
|
return BOX(UNBOX(p) || UNBOX(q));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "&&";
|
|
|
|
|
int Ls__Infix_3838 (void *p, void *q) {
|
|
|
|
|
ASSERT_UNBOXED("captured &&:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured &&:2", q);
|
|
|
|
|
|
|
|
|
|
return BOX(UNBOX(p) && UNBOX(q));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "==";
|
|
|
|
|
int Ls__Infix_6161 (void *p, void *q) {
|
2020-08-06 14:56:41 +03:00
|
|
|
return BOX(p == q);
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "!=";
|
|
|
|
|
int Ls__Infix_3361 (void *p, void *q) {
|
|
|
|
|
ASSERT_UNBOXED("captured !=:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured !=:2", q);
|
|
|
|
|
|
|
|
|
|
return BOX(UNBOX(p) != UNBOX(q));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "<=";
|
|
|
|
|
int Ls__Infix_6061 (void *p, void *q) {
|
|
|
|
|
ASSERT_UNBOXED("captured <=:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured <=:2", q);
|
|
|
|
|
|
|
|
|
|
return BOX(UNBOX(p) <= UNBOX(q));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "<";
|
|
|
|
|
int Ls__Infix_60 (void *p, void *q) {
|
|
|
|
|
ASSERT_UNBOXED("captured <:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured <:2", q);
|
|
|
|
|
|
|
|
|
|
return BOX(UNBOX(p) < UNBOX(q));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator ">=";
|
|
|
|
|
int Ls__Infix_6261 (void *p, void *q) {
|
|
|
|
|
ASSERT_UNBOXED("captured >=:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured >=:2", q);
|
|
|
|
|
|
|
|
|
|
return BOX(UNBOX(p) >= UNBOX(q));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator ">";
|
|
|
|
|
int Ls__Infix_62 (void *p, void *q) {
|
|
|
|
|
ASSERT_UNBOXED("captured >:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured >:2", q);
|
|
|
|
|
|
|
|
|
|
return BOX(UNBOX(p) > UNBOX(q));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "+";
|
|
|
|
|
int Ls__Infix_43 (void *p, void *q) {
|
|
|
|
|
ASSERT_UNBOXED("captured +:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured +:2", q);
|
|
|
|
|
|
|
|
|
|
return BOX(UNBOX(p) + UNBOX(q));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "-";
|
|
|
|
|
int Ls__Infix_45 (void *p, void *q) {
|
2020-08-06 14:56:41 +03:00
|
|
|
if (UNBOXED(p)) {
|
|
|
|
|
ASSERT_UNBOXED("captured -:2", q);
|
|
|
|
|
return BOX(UNBOX(p) - UNBOX(q));
|
|
|
|
|
}
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2020-08-06 14:56:41 +03:00
|
|
|
ASSERT_BOXED("captured -:1", q);
|
|
|
|
|
return BOX(p - q);
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "*";
|
|
|
|
|
int Ls__Infix_42 (void *p, void *q) {
|
|
|
|
|
ASSERT_UNBOXED("captured *:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured *:2", q);
|
|
|
|
|
|
|
|
|
|
return BOX(UNBOX(p) * UNBOX(q));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "/";
|
|
|
|
|
int Ls__Infix_47 (void *p, void *q) {
|
|
|
|
|
ASSERT_UNBOXED("captured /:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured /:2", q);
|
|
|
|
|
|
|
|
|
|
return BOX(UNBOX(p) / UNBOX(q));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "%";
|
|
|
|
|
int Ls__Infix_37 (void *p, void *q) {
|
|
|
|
|
ASSERT_UNBOXED("captured %:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured %:2", q);
|
|
|
|
|
|
|
|
|
|
return BOX(UNBOX(p) % UNBOX(q));
|
|
|
|
|
}
|
2018-11-29 18:27:59 +03:00
|
|
|
|
2021-01-31 22:57:12 +03:00
|
|
|
extern int Llength (void *p) {
|
2019-04-24 17:34:53 +03:00
|
|
|
data *a = (data*) BOX (NULL);
|
2020-01-04 21:50:14 +03:00
|
|
|
|
|
|
|
|
ASSERT_BOXED(".length", p);
|
|
|
|
|
|
2018-12-11 10:22:23 +03:00
|
|
|
a = TO_DATA(p);
|
2023-03-27 10:09:54 +02:00
|
|
|
return BOX(LEN(a->data_header));
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2020-09-01 17:23:36 +03:00
|
|
|
static char* chars = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'";
|
|
|
|
|
|
2020-09-01 20:31:34 +03:00
|
|
|
extern char* de_hash (int);
|
|
|
|
|
|
2020-09-01 17:23:36 +03:00
|
|
|
extern int LtagHash (char *s) {
|
|
|
|
|
char *p;
|
2020-09-01 20:31:34 +03:00
|
|
|
int h = 0, limit = 0;
|
2020-09-01 17:23:36 +03:00
|
|
|
|
|
|
|
|
p = s;
|
|
|
|
|
|
2022-01-31 23:46:18 +03:00
|
|
|
while (*p && limit++ <= 4) {
|
2020-09-01 20:31:34 +03:00
|
|
|
char *q = chars;
|
|
|
|
|
int pos = 0;
|
|
|
|
|
|
2020-09-01 17:23:36 +03:00
|
|
|
for (; *q && *q != *p; q++, pos++);
|
|
|
|
|
|
|
|
|
|
if (*q) h = (h << 6) | pos;
|
|
|
|
|
else failure ("tagHash: character not found: %c\n", *p);
|
|
|
|
|
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 20:31:34 +03:00
|
|
|
if (strcmp (s, de_hash (h)) != 0) {
|
|
|
|
|
failure ("%s <-> %s\n", s, de_hash(h));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 17:23:36 +03:00
|
|
|
return BOX(h);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
char* de_hash (int n) {
|
2020-09-01 17:23:36 +03:00
|
|
|
// static char *chars = (char*) BOX (NULL);
|
2018-12-11 10:22:23 +03:00
|
|
|
static char buf[6] = {0,0,0,0,0,0};
|
2019-04-24 17:34:53 +03:00
|
|
|
char *p = (char *) BOX (NULL);
|
2018-12-11 10:22:23 +03:00
|
|
|
p = &buf[5];
|
2019-04-24 17:34:53 +03:00
|
|
|
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
indent++; print_indent ();
|
2023-03-27 10:09:54 +02:00
|
|
|
printf ("de_hash: data_header: %d\n", n); fflush (stdout);
|
2019-04-24 17:34:53 +03:00
|
|
|
#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
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2019-04-24 17:34:53 +03:00
|
|
|
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;
|
|
|
|
|
}
|
2020-02-07 19:42:24 +03:00
|
|
|
|
|
|
|
|
#ifdef DEBUG_PRINT
|
|
|
|
|
indent--;
|
|
|
|
|
#endif
|
2018-10-31 20:10:50 +03:00
|
|
|
|
|
|
|
|
return ++p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
char *contents;
|
|
|
|
|
int ptr;
|
2021-12-03 03:56:58 +03:00
|
|
|
int len;
|
2018-10-31 20:10:50 +03:00
|
|
|
} StringBuf;
|
|
|
|
|
|
|
|
|
|
static StringBuf stringBuf;
|
|
|
|
|
|
|
|
|
|
# define STRINGBUF_INIT 128
|
|
|
|
|
|
|
|
|
|
static void createStringBuf () {
|
|
|
|
|
stringBuf.contents = (char*) malloc (STRINGBUF_INIT);
|
2022-10-10 18:31:27 +03:00
|
|
|
memset(stringBuf.contents, 0, STRINGBUF_INIT);
|
2018-10-31 20:10:50 +03:00
|
|
|
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);
|
2021-12-03 03:56:58 +03:00
|
|
|
va_list vsnargs;
|
|
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
again:
|
2021-12-03 03:56:58 +03:00
|
|
|
va_copy (vsnargs, args);
|
|
|
|
|
|
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;
|
2021-12-03 03:56:58 +03:00
|
|
|
|
|
|
|
|
written = vsnprintf (buf, rest, fmt, vsnargs);
|
|
|
|
|
|
|
|
|
|
va_end(vsnargs);
|
2018-10-31 20:10:50 +03:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
//int is_valid_heap_pointer (void *p);
|
2020-02-19 15:28:29 +03:00
|
|
|
|
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 {
|
2020-02-19 15:28:29 +03:00
|
|
|
if (! is_valid_heap_pointer(p)) {
|
|
|
|
|
printStringBuf ("0x%x", p);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 23:04:45 +03:00
|
|
|
a = TO_DATA(p);
|
2018-10-31 20:10:50 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
switch (TAG(a->data_header)) {
|
2018-10-31 20:10:50 +03:00
|
|
|
case STRING_TAG:
|
|
|
|
|
printStringBuf ("\"%s\"", a->contents);
|
|
|
|
|
break;
|
2019-12-18 18:44:01 +03:00
|
|
|
|
|
|
|
|
case CLOSURE_TAG:
|
|
|
|
|
printStringBuf ("<closure ");
|
2023-03-27 10:09:54 +02:00
|
|
|
for (i = 0; i < LEN(a->data_header); i++) {
|
2019-12-18 18:44:01 +03:00
|
|
|
if (i) printValue ((void*)((int*) a->contents)[i]);
|
2020-02-19 15:28:29 +03:00
|
|
|
else printStringBuf ("0x%x", (void*)((int*) a->contents)[i]);
|
2019-12-18 18:44:01 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
if (i != LEN(a->data_header) - 1) printStringBuf (", ");
|
2019-12-18 18:44:01 +03:00
|
|
|
}
|
|
|
|
|
printStringBuf (">");
|
|
|
|
|
break;
|
2018-10-31 20:10:50 +03:00
|
|
|
|
|
|
|
|
case ARRAY_TAG:
|
|
|
|
|
printStringBuf ("[");
|
2023-03-27 10:09:54 +02:00
|
|
|
for (i = 0; i < LEN(a->data_header); i++) {
|
2018-10-31 20:10:50 +03:00
|
|
|
printValue ((void*)((int*) a->contents)[i]);
|
2023-03-27 10:09:54 +02:00
|
|
|
if (i != LEN(a->data_header) - 1) printStringBuf (", ");
|
2018-10-31 20:10:50 +03:00
|
|
|
}
|
|
|
|
|
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
|
2023-03-27 10:09:54 +02:00
|
|
|
char * data_header = de_hash (GET_SEXP_TAG(TO_SEXP(p)->data_header));
|
2019-04-24 17:34:53 +03:00
|
|
|
#endif
|
2019-03-07 21:12:43 +03:00
|
|
|
|
|
|
|
|
if (strcmp (tag, "cons") == 0) {
|
|
|
|
|
data *b = a;
|
|
|
|
|
|
|
|
|
|
printStringBuf ("{");
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
while (LEN(a->data_header)) {
|
2019-03-07 21:12:43 +03:00
|
|
|
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);
|
2023-03-27 10:09:54 +02:00
|
|
|
if (LEN(a->data_header)) {
|
2019-03-07 21:12:43 +03:00
|
|
|
printStringBuf (" (");
|
2023-03-27 10:09:54 +02:00
|
|
|
for (i = 0; i < LEN(a->data_header); i++) {
|
2019-03-07 21:12:43 +03:00
|
|
|
printValue ((void*)((int*) a->contents)[i]);
|
2023-03-27 10:09:54 +02:00
|
|
|
if (i != LEN(a->data_header) - 1) printStringBuf (", ");
|
2019-03-07 21:12:43 +03:00
|
|
|
}
|
|
|
|
|
printStringBuf (")");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2019-04-24 17:34:53 +03:00
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
default:
|
2023-03-27 10:09:54 +02:00
|
|
|
printStringBuf ("*** invalid data_header: 0x%x ***", TAG(a->data_header));
|
2018-10-31 20:10:50 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
switch (TAG(a->data_header)) {
|
2019-12-21 02:34:56 +03:00
|
|
|
case STRING_TAG:
|
|
|
|
|
printStringBuf ("%s", a->contents);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEXP_TAG: {
|
|
|
|
|
#ifndef DEBUG_PRINT
|
|
|
|
|
char * tag = de_hash (TO_SEXP(p)->tag);
|
|
|
|
|
#else
|
2023-03-27 10:09:54 +02:00
|
|
|
char * data_header = de_hash (GET_SEXP_TAG(TO_SEXP(p)->data_header));
|
2019-12-21 02:34:56 +03:00
|
|
|
#endif
|
|
|
|
|
if (strcmp (tag, "cons") == 0) {
|
|
|
|
|
data *b = a;
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
while (LEN(a->data_header)) {
|
2019-12-21 02:34:56 +03:00
|
|
|
stringcat ((void*)((int*) b->contents)[0]);
|
|
|
|
|
b = (data*)((int*) b->contents)[1];
|
|
|
|
|
if (! UNBOXED(b)) {
|
|
|
|
|
b = TO_DATA(b);
|
|
|
|
|
}
|
|
|
|
|
else break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-27 10:09:54 +02:00
|
|
|
else printStringBuf ("*** non-list data_header: %s ***", tag);
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2023-03-27 10:09:54 +02:00
|
|
|
printStringBuf ("*** invalid data_header: 0x%x ***", TAG(a->data_header));
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 15:34:34 +03:00
|
|
|
extern int Luppercase (void *v) {
|
|
|
|
|
ASSERT_UNBOXED("Luppercase:1", v);
|
|
|
|
|
return BOX(toupper ((int) UNBOX(v)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Llowercase (void *v) {
|
|
|
|
|
ASSERT_UNBOXED("Llowercase:1", v);
|
|
|
|
|
return BOX(tolower ((int) UNBOX(v)));
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-31 00:59:28 +03:00
|
|
|
extern int LmatchSubString (char *subj, char *patt, int pos) {
|
2020-03-13 19:41:14 +03:00
|
|
|
data *p = TO_DATA(patt), *s = TO_DATA(subj);
|
2020-01-04 21:50:14 +03:00
|
|
|
int n;
|
2019-12-31 00:59:28 +03:00
|
|
|
|
2020-01-04 21:50:14 +03:00
|
|
|
ASSERT_STRING("matchSubString:1", subj);
|
|
|
|
|
ASSERT_STRING("matchSubString:2", patt);
|
|
|
|
|
ASSERT_UNBOXED("matchSubString:3", pos);
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
n = LEN (p->data_header);
|
2020-03-13 19:41:14 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
if (n + UNBOX(pos) > LEN(s->data_header))
|
2020-03-13 19:41:14 +03:00
|
|
|
return BOX(0);
|
2020-01-04 21:50:14 +03:00
|
|
|
|
2019-12-31 00:59:28 +03:00
|
|
|
return BOX(strncmp (subj + UNBOX(pos), patt, n) == 0);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 01:38:49 +03:00
|
|
|
extern void* Lsubstring (void *subj, int p, int l) {
|
|
|
|
|
data *d = TO_DATA(subj);
|
|
|
|
|
int pp = UNBOX (p), ll = UNBOX (l);
|
2020-01-04 21:50:14 +03:00
|
|
|
|
|
|
|
|
ASSERT_STRING("substring:1", subj);
|
|
|
|
|
ASSERT_UNBOXED("substring:2", p);
|
|
|
|
|
ASSERT_UNBOXED("substring:3", l);
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
if (pp + ll <= LEN(d->data_header)) {
|
2020-01-03 01:38:49 +03:00
|
|
|
data *r;
|
|
|
|
|
|
|
|
|
|
__pre_gc ();
|
2020-02-07 19:42:24 +03:00
|
|
|
|
|
|
|
|
push_extra_root (&subj);
|
2020-01-03 01:38:49 +03:00
|
|
|
r = (data*) alloc (ll + 1 + sizeof (int));
|
2020-02-07 19:42:24 +03:00
|
|
|
pop_extra_root (&subj);
|
2020-01-03 01:38:49 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
r->data_header = STRING_TAG | (ll << 3);
|
2020-01-03 01:38:49 +03:00
|
|
|
|
|
|
|
|
strncpy (r->contents, (char*) subj + pp, ll);
|
|
|
|
|
|
|
|
|
|
__post_gc ();
|
|
|
|
|
|
|
|
|
|
return r->contents;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 17:05:53 +03:00
|
|
|
failure ("substring: index out of bounds (position=%d, length=%d, \
|
2023-03-27 10:09:54 +02:00
|
|
|
subject length=%d)", pp, ll, LEN(d->data_header));
|
2020-01-03 01:38:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern struct re_pattern_buffer *Lregexp (char *regexp) {
|
2020-03-21 13:05:14 +03:00
|
|
|
regex_t *b = (regex_t*) malloc (sizeof (regex_t));
|
2020-05-04 02:45:34 +03:00
|
|
|
|
2022-11-01 09:22:19 +03:00
|
|
|
/* printf ("regexp: %s,\t%x\n", regexp, b); */
|
|
|
|
|
|
2020-05-04 02:45:34 +03:00
|
|
|
memset (b, 0, sizeof (regex_t));
|
2020-03-13 19:41:14 +03:00
|
|
|
|
2020-02-27 18:11:35 +03:00
|
|
|
int n = (int) re_compile_pattern (regexp, strlen (regexp), b);
|
2020-05-04 02:45:34 +03:00
|
|
|
|
2020-01-03 01:38:49 +03:00
|
|
|
if (n != 0) {
|
|
|
|
|
failure ("%", strerror (n));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int LregexpMatch (struct re_pattern_buffer *b, char *s, int pos) {
|
2020-05-04 02:45:34 +03:00
|
|
|
int res;
|
|
|
|
|
|
2020-01-04 21:50:14 +03:00
|
|
|
ASSERT_BOXED("regexpMatch:1", b);
|
|
|
|
|
ASSERT_STRING("regexpMatch:2", s);
|
|
|
|
|
ASSERT_UNBOXED("regexpMatch:3", pos);
|
2020-05-04 02:45:34 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
res = re_match (b, s, LEN(TO_DATA(s)->data_header), UNBOX(pos), 0);
|
2020-05-04 02:45:34 +03:00
|
|
|
|
2022-11-01 09:22:19 +03:00
|
|
|
/* printf ("regexpMatch %x: %s, res=%d\n", b, s+UNBOX(pos), res); */
|
|
|
|
|
|
2020-05-04 02:45:34 +03:00
|
|
|
if (res) {
|
|
|
|
|
return BOX (res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BOX (res);
|
2020-01-03 01:38:49 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-28 03:01:54 +03:00
|
|
|
extern void* Bstring (void*);
|
|
|
|
|
|
2020-01-15 21:42:59 +03:00
|
|
|
void *Lclone (void *p) {
|
2020-02-27 18:11:35 +03:00
|
|
|
data *obj;
|
|
|
|
|
sexp *sobj;
|
|
|
|
|
void* res;
|
2020-01-26 06:06:14 +03:00
|
|
|
int n;
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
register int * ebp asm ("ebp");
|
|
|
|
|
indent++; print_indent ();
|
2020-01-28 16:24:26 +03:00
|
|
|
printf ("Lclone arg: %p %p\n", &p, p); fflush (stdout);
|
|
|
|
|
#endif
|
2020-01-15 21:42:59 +03:00
|
|
|
__pre_gc ();
|
|
|
|
|
|
|
|
|
|
if (UNBOXED(p)) return p;
|
|
|
|
|
else {
|
2020-01-15 22:33:46 +03:00
|
|
|
data *a = TO_DATA(p);
|
2023-03-27 10:09:54 +02:00
|
|
|
int t = TAG(a->data_header), l = LEN(a->data_header);
|
2020-01-15 21:42:59 +03:00
|
|
|
|
2020-01-29 14:21:17 +03:00
|
|
|
push_extra_root (&p);
|
2020-01-15 21:42:59 +03:00
|
|
|
switch (t) {
|
|
|
|
|
case STRING_TAG:
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("Lclone: string1 &p=%p p=%p\n", &p, p); fflush (stdout);
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2020-01-28 16:27:36 +03:00
|
|
|
res = Bstring (TO_DATA(p)->contents);
|
2020-01-28 18:06:09 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-28 18:06:09 +03:00
|
|
|
printf ("Lclone: string2 %p %p\n", &p, p); fflush (stdout);
|
2020-02-07 19:42:24 +03:00
|
|
|
#endif
|
|
|
|
|
break;
|
2020-01-15 21:42:59 +03:00
|
|
|
|
|
|
|
|
case ARRAY_TAG:
|
|
|
|
|
case CLOSURE_TAG:
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("Lclone: closure or array &p=%p p=%p ebp=%p\n", &p, p, ebp); fflush (stdout);
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2020-02-27 18:11:35 +03:00
|
|
|
obj = (data*) alloc (sizeof(int) * (l+1));
|
|
|
|
|
memcpy (obj, TO_DATA(p), sizeof(int) * (l+1));
|
|
|
|
|
res = (void*) (obj->contents);
|
2020-01-15 21:42:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEXP_TAG:
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent (); printf ("Lclone: sexp\n"); fflush (stdout);
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2020-02-27 18:11:35 +03:00
|
|
|
sobj = (sexp*) alloc (sizeof(int) * (l+2));
|
|
|
|
|
memcpy (sobj, TO_SEXP(p), sizeof(int) * (l+2));
|
|
|
|
|
res = (void*) sobj->contents.contents;
|
2020-01-15 21:42:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2023-03-27 10:09:54 +02:00
|
|
|
failure ("invalid data_header %d in clone *****\n", t);
|
2020-01-15 21:42:59 +03:00
|
|
|
}
|
2020-01-29 14:21:17 +03:00
|
|
|
pop_extra_root (&p);
|
2020-01-28 16:24:26 +03:00
|
|
|
}
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent (); printf ("Lclone ends1\n"); fflush (stdout);
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2020-01-15 21:42:59 +03:00
|
|
|
|
|
|
|
|
__post_gc ();
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-28 16:24:26 +03:00
|
|
|
printf ("Lclone ends2\n"); fflush (stdout);
|
2020-02-07 19:42:24 +03:00
|
|
|
indent--;
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2020-01-15 21:42:59 +03:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 03:38:43 +03:00
|
|
|
# define HASH_DEPTH 3
|
2020-03-13 19:41:14 +03:00
|
|
|
# define HASH_APPEND(acc, x) (((acc + (unsigned) x) << (WORD_SIZE / 2)) | ((acc + (unsigned) x) >> (WORD_SIZE / 2)))
|
2020-01-15 21:42:59 +03:00
|
|
|
|
|
|
|
|
int inner_hash (int depth, unsigned acc, void *p) {
|
|
|
|
|
if (depth > HASH_DEPTH) return acc;
|
|
|
|
|
|
|
|
|
|
if (UNBOXED(p)) return HASH_APPEND(acc, UNBOX(p));
|
2020-03-13 19:41:14 +03:00
|
|
|
else if (is_valid_heap_pointer (p)) {
|
2020-01-15 21:42:59 +03:00
|
|
|
data *a = TO_DATA(p);
|
2023-03-27 10:09:54 +02:00
|
|
|
int t = TAG(a->data_header), l = LEN(a->data_header), i;
|
2020-01-15 21:42:59 +03:00
|
|
|
|
|
|
|
|
acc = HASH_APPEND(acc, t);
|
|
|
|
|
acc = HASH_APPEND(acc, l);
|
|
|
|
|
|
|
|
|
|
switch (t) {
|
|
|
|
|
case STRING_TAG: {
|
|
|
|
|
char *p = a->contents;
|
|
|
|
|
|
2020-01-21 22:03:11 +03:00
|
|
|
while (*p) {
|
2020-03-13 19:41:14 +03:00
|
|
|
int n = (int) *p++;
|
|
|
|
|
acc = HASH_APPEND(acc, n);
|
2020-01-15 21:42:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case CLOSURE_TAG:
|
|
|
|
|
acc = HASH_APPEND(acc, ((void**) a->contents)[0]);
|
|
|
|
|
i = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ARRAY_TAG:
|
|
|
|
|
i = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEXP_TAG: {
|
|
|
|
|
#ifndef DEBUG_PRINT
|
|
|
|
|
int ta = TO_SEXP(p)->tag;
|
|
|
|
|
#else
|
2023-03-27 10:09:54 +02:00
|
|
|
int ta = GET_SEXP_TAG(TO_SEXP(p)->data_header);
|
2020-01-15 21:42:59 +03:00
|
|
|
#endif
|
|
|
|
|
acc = HASH_APPEND(acc, ta);
|
|
|
|
|
i = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
2023-03-27 10:09:54 +02:00
|
|
|
failure ("invalid data_header %d in hash *****\n", t);
|
2020-01-15 21:42:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (; i<l; i++)
|
|
|
|
|
acc = inner_hash (depth+1, acc, ((void**) a->contents)[i]);
|
|
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
|
}
|
2020-03-13 19:41:14 +03:00
|
|
|
else return HASH_APPEND(acc, p);
|
2020-01-15 21:42:59 +03:00
|
|
|
}
|
|
|
|
|
|
2020-02-23 01:36:30 +03:00
|
|
|
extern void* LstringInt (char *b) {
|
|
|
|
|
int n;
|
|
|
|
|
sscanf (b, "%d", &n);
|
2020-02-27 18:11:35 +03:00
|
|
|
return (void*) BOX(n);
|
2020-02-23 01:36:30 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-15 21:42:59 +03:00
|
|
|
extern int Lhash (void *p) {
|
2020-08-06 14:56:41 +03:00
|
|
|
return BOX(0x3fffff & inner_hash (0, 0, p));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int LflatCompare (void *p, void *q) {
|
|
|
|
|
if (UNBOXED(p)) {
|
|
|
|
|
if (UNBOXED(q)) {
|
|
|
|
|
return BOX (UNBOX(p) - UNBOX(q));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else if (~UNBOXED(q)) {
|
|
|
|
|
return BOX(p - q);
|
|
|
|
|
}
|
|
|
|
|
else BOX(1);
|
2020-01-15 21:42:59 +03:00
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
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)
|
2020-03-22 21:58:11 +03:00
|
|
|
|
2020-01-15 21:42:59 +03:00
|
|
|
if (p == q) return BOX(0);
|
2020-05-04 02:45:34 +03:00
|
|
|
|
2019-12-20 00:23:35 +03:00
|
|
|
if (UNBOXED(p)) {
|
|
|
|
|
if (UNBOXED(q)) return BOX(UNBOX(p) - UNBOX(q));
|
|
|
|
|
else return BOX(-1);
|
|
|
|
|
}
|
|
|
|
|
else if (UNBOXED(q)) return BOX(1);
|
|
|
|
|
else {
|
2020-05-04 02:45:34 +03:00
|
|
|
if (is_valid_heap_pointer (p)) {
|
|
|
|
|
if (is_valid_heap_pointer (q)) {
|
|
|
|
|
data *a = TO_DATA(p), *b = TO_DATA(q);
|
2023-03-27 10:09:54 +02:00
|
|
|
int ta = TAG(a->data_header), tb = TAG(b->data_header);
|
|
|
|
|
int la = LEN(a->data_header), lb = LEN(b->data_header);
|
2020-05-04 02:45:34 +03:00
|
|
|
int i;
|
2019-12-20 00:23:35 +03:00
|
|
|
|
2020-05-04 02:45:34 +03:00
|
|
|
COMPARE_AND_RETURN (ta, tb);
|
2019-12-20 00:23:35 +03:00
|
|
|
|
2020-05-04 02:45:34 +03:00
|
|
|
switch (ta) {
|
|
|
|
|
case STRING_TAG:
|
|
|
|
|
return BOX(strcmp (a->contents, b->contents));
|
2019-12-20 00:23:35 +03:00
|
|
|
|
2020-05-04 02:45:34 +03:00
|
|
|
case CLOSURE_TAG:
|
|
|
|
|
COMPARE_AND_RETURN (((void**) a->contents)[0], ((void**) b->contents)[0]);
|
|
|
|
|
COMPARE_AND_RETURN (la, lb);
|
|
|
|
|
i = 1;
|
|
|
|
|
break;
|
2019-12-20 00:23:35 +03:00
|
|
|
|
2020-05-04 02:45:34 +03:00
|
|
|
case ARRAY_TAG:
|
|
|
|
|
COMPARE_AND_RETURN (la, lb);
|
|
|
|
|
i = 0;
|
|
|
|
|
break;
|
2019-12-20 00:23:35 +03:00
|
|
|
|
2020-05-04 02:45:34 +03:00
|
|
|
case SEXP_TAG: {
|
2019-12-20 00:23:35 +03:00
|
|
|
#ifndef DEBUG_PRINT
|
2020-05-04 02:45:34 +03:00
|
|
|
int ta = TO_SEXP(p)->tag, tb = TO_SEXP(q)->tag;
|
2019-12-20 00:23:35 +03:00
|
|
|
#else
|
2023-03-27 10:09:54 +02:00
|
|
|
int ta = GET_SEXP_TAG(TO_SEXP(p)->data_header), tb = GET_SEXP_TAG(TO_SEXP(q)->data_header);
|
2019-12-20 00:23:35 +03:00
|
|
|
#endif
|
2020-05-04 02:45:34 +03:00
|
|
|
COMPARE_AND_RETURN (ta, tb);
|
|
|
|
|
COMPARE_AND_RETURN (la, lb);
|
|
|
|
|
i = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
2023-03-27 10:09:54 +02:00
|
|
|
failure ("invalid data_header %d in compare *****\n", ta);
|
2020-05-04 02:45:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (; i<la; i++) {
|
|
|
|
|
int c = Lcompare (((void**) a->contents)[i], ((void**) b->contents)[i]);
|
|
|
|
|
if (c != BOX(0)) return BOX(c);
|
|
|
|
|
}
|
2019-12-20 00:23:35 +03:00
|
|
|
|
2020-05-04 02:45:34 +03:00
|
|
|
return BOX(0);
|
|
|
|
|
}
|
|
|
|
|
else return BOX(-1);
|
|
|
|
|
}
|
|
|
|
|
else if (is_valid_heap_pointer (q)) return BOX(1);
|
|
|
|
|
else return BOX (p - q);
|
2019-12-20 00:23:35 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
2020-01-04 21:50:14 +03:00
|
|
|
|
|
|
|
|
ASSERT_BOXED(".elem:1", p);
|
|
|
|
|
ASSERT_UNBOXED(".elem:2", i);
|
|
|
|
|
|
2018-12-06 23:04:45 +03:00
|
|
|
a = TO_DATA(p);
|
2018-10-25 03:15:24 +03:00
|
|
|
i = UNBOX(i);
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
if (TAG(a->data_header) == STRING_TAG) {
|
2018-10-25 03:15:24 +03:00
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-16 06:59:34 +03:00
|
|
|
extern void* LmakeArray (int length) {
|
|
|
|
|
data *r;
|
2022-02-14 01:13:14 +03:00
|
|
|
int n, *p;
|
2020-01-16 06:59:34 +03:00
|
|
|
|
|
|
|
|
ASSERT_UNBOXED("makeArray:1", length);
|
|
|
|
|
|
|
|
|
|
__pre_gc ();
|
|
|
|
|
|
|
|
|
|
n = UNBOX(length);
|
|
|
|
|
r = (data*) alloc (sizeof(int) * (n+1));
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
r->data_header = ARRAY_TAG | (n << 3);
|
2020-01-16 06:59:34 +03:00
|
|
|
|
2022-02-14 01:13:14 +03:00
|
|
|
p = (int*) r->contents;
|
|
|
|
|
while (n--) *p++ = BOX(0);
|
2020-02-13 18:56:27 +03:00
|
|
|
|
2020-01-16 06:59:34 +03:00
|
|
|
__post_gc ();
|
|
|
|
|
|
|
|
|
|
return r->contents;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2020-01-26 06:06:14 +03:00
|
|
|
ASSERT_UNBOXED("makeString", length);
|
2020-01-04 21:50:14 +03:00
|
|
|
|
2018-12-11 10:22:23 +03:00
|
|
|
__pre_gc () ;
|
|
|
|
|
|
2020-01-24 17:05:53 +03:00
|
|
|
r = (data*) alloc (n + 1 + sizeof (int));
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
r->data_header = 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);
|
2020-01-28 16:24:26 +03:00
|
|
|
data *s = NULL;
|
2020-01-04 21:50:14 +03:00
|
|
|
|
2020-01-24 17:05:53 +03:00
|
|
|
__pre_gc ();
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
indent++; print_indent ();
|
2020-01-29 14:21:17 +03:00
|
|
|
printf ("Bstring: call LmakeString %s %p %p %p %i\n", p, &p, p, s, n);
|
|
|
|
|
fflush(stdout);
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2020-01-29 14:21:17 +03:00
|
|
|
push_extra_root (&p);
|
2020-01-28 03:01:54 +03:00
|
|
|
s = LmakeString (BOX(n));
|
2020-01-29 14:21:17 +03:00
|
|
|
pop_extra_root(&p);
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("\tBstring: call strncpy: %p %p %p %i\n", &p, p, s, n); fflush(stdout);
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2020-02-27 18:11:35 +03:00
|
|
|
strncpy ((char*)s, p, n + 1);
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("\tBstring: ends\n"); fflush(stdout);
|
|
|
|
|
indent--;
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2020-01-24 17:05:53 +03:00
|
|
|
__post_gc ();
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2020-01-28 03:01:54 +03:00
|
|
|
return s;
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void* Lstringcat (void *p) {
|
2020-01-28 03:01:54 +03:00
|
|
|
void *s;
|
|
|
|
|
|
2021-08-13 09:50:07 +03:00
|
|
|
/* ASSERT_BOXED("stringcat", p); */
|
2020-01-04 21:50:14 +03:00
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
__pre_gc ();
|
|
|
|
|
|
|
|
|
|
createStringBuf ();
|
|
|
|
|
stringcat (p);
|
|
|
|
|
|
2020-02-07 19:42:24 +03:00
|
|
|
push_extra_root(&p);
|
2020-01-28 03:01:54 +03:00
|
|
|
s = Bstring (stringBuf.contents);
|
2020-02-07 19:42:24 +03:00
|
|
|
pop_extra_root(&p);
|
2019-12-21 02:34:56 +03:00
|
|
|
|
|
|
|
|
deleteStringBuf ();
|
|
|
|
|
|
|
|
|
|
__post_gc ();
|
|
|
|
|
|
2020-01-28 03:01:54 +03:00
|
|
|
return s;
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
2021-01-31 22:57:12 +03:00
|
|
|
extern void* Lstring (void *p) {
|
2020-01-28 03:01:54 +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);
|
|
|
|
|
|
2020-02-07 19:42:24 +03:00
|
|
|
push_extra_root(&p);
|
2020-01-28 03:01:54 +03:00
|
|
|
s = Bstring (stringBuf.contents);
|
2020-02-07 19:42:24 +03:00
|
|
|
pop_extra_root(&p);
|
2018-10-31 20:10:50 +03:00
|
|
|
|
|
|
|
|
deleteStringBuf ();
|
|
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
__post_gc ();
|
|
|
|
|
|
2020-01-28 03:01:54 +03:00
|
|
|
return s;
|
2018-10-31 20:10:50 +03:00
|
|
|
}
|
|
|
|
|
|
2020-02-13 18:56:27 +03:00
|
|
|
extern void* Bclosure (int bn, void *entry, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
int i, ai;
|
2020-02-07 19:42:24 +03:00
|
|
|
register int * ebp asm ("ebp");
|
2020-02-13 18:56:27 +03:00
|
|
|
size_t *argss;
|
|
|
|
|
data *r;
|
|
|
|
|
int n = UNBOX(bn);
|
2020-01-04 21:50:14 +03:00
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
__pre_gc ();
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
indent++; print_indent ();
|
2019-10-16 01:13:52 +03:00
|
|
|
printf ("Bclosure: create n = %d\n", n); fflush(stdout);
|
|
|
|
|
#endif
|
2020-02-07 19:42:24 +03:00
|
|
|
argss = (ebp + 12);
|
|
|
|
|
for (i = 0; i<n; i++, argss++) {
|
2020-02-27 18:11:35 +03:00
|
|
|
push_extra_root ((void**)argss);
|
2020-02-07 19:42:24 +03:00
|
|
|
}
|
2019-10-16 01:13:52 +03:00
|
|
|
|
2020-02-07 19:42:24 +03:00
|
|
|
r = (data*) alloc (sizeof(int) * (n+2));
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
r->data_header = CLOSURE_TAG | ((n + 1) << 3);
|
2019-10-16 01:13:52 +03:00
|
|
|
((void**) r->contents)[0] = entry;
|
|
|
|
|
|
2020-01-24 17:05:53 +03:00
|
|
|
va_start(args, entry);
|
2019-10-16 01:13:52 +03:00
|
|
|
|
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();
|
|
|
|
|
|
2020-02-07 19:42:24 +03:00
|
|
|
argss--;
|
|
|
|
|
for (i = 0; i<n; i++, argss--) {
|
2020-02-27 18:11:35 +03:00
|
|
|
pop_extra_root ((void**)argss);
|
2020-02-07 19:42:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_PRINT
|
|
|
|
|
print_indent ();
|
|
|
|
|
printf ("Bclosure: ends\n", n); fflush(stdout);
|
|
|
|
|
indent--;
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
return r->contents;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 18:56:27 +03:00
|
|
|
extern void* Barray (int bn, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
int i, ai;
|
|
|
|
|
data *r;
|
|
|
|
|
int n = UNBOX(bn);
|
|
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
__pre_gc ();
|
|
|
|
|
|
2018-12-05 19:06:07 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
indent++; print_indent ();
|
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
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
r->data_header = ARRAY_TAG | (n << 3);
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2020-09-01 20:31:34 +03:00
|
|
|
va_start(args, bn);
|
2018-04-30 17:18:41 +03:00
|
|
|
|
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();
|
2020-02-07 19:42:24 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
|
|
|
|
indent--;
|
|
|
|
|
#endif
|
2018-04-30 17:18:41 +03:00
|
|
|
return r->contents;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 18:56:27 +03:00
|
|
|
extern void* Bsexp (int bn, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
int i;
|
|
|
|
|
int ai;
|
|
|
|
|
size_t *p;
|
|
|
|
|
sexp *r;
|
|
|
|
|
data *d;
|
2020-09-01 20:31:34 +03:00
|
|
|
int n = UNBOX(bn);
|
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
|
2020-02-07 19:42:24 +03:00
|
|
|
indent++; print_indent ();
|
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
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
d->data_header = SEXP_TAG | ((n - 1) << 3);
|
2018-05-16 09:24:40 +03:00
|
|
|
|
2020-09-01 20:31:34 +03:00
|
|
|
va_start(args, bn);
|
2018-05-16 09:24:40 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2020-09-01 20:31:34 +03:00
|
|
|
r->tag = UNBOX(va_arg(args, int));
|
2019-04-24 16:02:29 +03:00
|
|
|
|
2019-04-24 17:34:53 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-03-27 10:09:54 +02:00
|
|
|
r->data_header = SEXP_TAG | ((r->data_header) << 3);
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
|
|
|
|
printf("Bsexp: ends\n"); fflush (stdout);
|
|
|
|
|
indent--;
|
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) {
|
2020-02-13 18:56:27 +03:00
|
|
|
data *r;
|
2020-01-04 21:50:14 +03:00
|
|
|
|
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
|
2023-03-27 10:09:54 +02:00
|
|
|
return BOX(TAG(r->data_header) == SEXP_TAG && TO_SEXP(d)->tag == UNBOX(t) && LEN(r->data_header) == UNBOX(n));
|
2019-04-24 17:34:53 +03:00
|
|
|
#else
|
2023-03-27 10:09:54 +02:00
|
|
|
return BOX(TAG(r->data_header) == SEXP_TAG &&
|
|
|
|
|
GET_SEXP_TAG(TO_SEXP(d)->data_header) == UNBOX(t) && LEN(r->data_header) == UNBOX(n));
|
2019-04-24 17:34:53 +03:00
|
|
|
#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) {
|
2020-02-13 18:56:27 +03:00
|
|
|
data *r;
|
2020-01-04 21:50:14 +03:00
|
|
|
|
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);
|
2023-03-27 10:09:54 +02:00
|
|
|
return BOX(TAG(r->data_header) == ARRAY_TAG && LEN(r->data_header) == UNBOX(n));
|
2018-11-06 00:21:38 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2020-01-04 21:50:14 +03:00
|
|
|
|
|
|
|
|
ASSERT_STRING(".string_patt:2", y);
|
|
|
|
|
|
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
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
if (TAG(rx->data_header) != STRING_TAG) return BOX(0);
|
2018-11-06 00:21:38 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
return BOX(strcmp (rx->contents, ry->contents) == 0 ? 1 : 0); // TODO: ???
|
2018-11-06 00:21:38 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
extern int Bclosure_tag_patt (void *x) {
|
|
|
|
|
if (UNBOXED(x)) return BOX(0);
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
return BOX(TAG(TO_DATA(x)->data_header) == CLOSURE_TAG);
|
2019-10-16 01:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
return BOX(TAG(TO_DATA(x)->data_header) == ARRAY_TAG);
|
2018-11-06 00:21:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Bstring_tag_patt (void *x) {
|
|
|
|
|
if (UNBOXED(x)) return BOX(0);
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
return BOX(TAG(TO_DATA(x)->data_header) == STRING_TAG);
|
2018-11-06 00:21:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Bsexp_tag_patt (void *x) {
|
|
|
|
|
if (UNBOXED(x)) return BOX(0);
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
return BOX(TAG(TO_DATA(x)->data_header) == SEXP_TAG);
|
2018-11-06 00:21:38 +03:00
|
|
|
}
|
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) {
|
2020-11-24 23:11:17 +03:00
|
|
|
if (UNBOXED(i)) {
|
|
|
|
|
ASSERT_BOXED(".sta:3", x);
|
|
|
|
|
// ASSERT_UNBOXED(".sta:2", i);
|
2020-01-04 21:50:14 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
if (TAG(TO_DATA(x)->data_header) == STRING_TAG)((char*) x)[UNBOX(i)] = (char) UNBOX(v);
|
2020-11-24 23:11:17 +03:00
|
|
|
else ((int*) x)[UNBOX(i)] = (int) v;
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
* (void**) x = 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) {
|
2020-02-27 18:11:35 +03:00
|
|
|
size_t *p = (size_t*)va;
|
2019-12-21 02:34:56 +03:00
|
|
|
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++;
|
2021-12-03 03:56:58 +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-24 03:59:05 +03:00
|
|
|
extern void Bmatch_failure (void *v, char *fname, int line, int col) {
|
|
|
|
|
createStringBuf ();
|
|
|
|
|
printValue (v);
|
2020-01-24 17:05:53 +03:00
|
|
|
failure ("match failure at %s:%d:%d, value '%s'\n",
|
2020-02-23 01:36:30 +03:00
|
|
|
fname, UNBOX(line), UNBOX(col), stringBuf.contents);
|
2019-12-24 03:59:05 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-03 01:38:49 +03:00
|
|
|
extern void* /*Lstrcat*/ Li__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);
|
|
|
|
|
|
2020-01-04 21:50:14 +03:00
|
|
|
ASSERT_STRING("++:1", a);
|
|
|
|
|
ASSERT_STRING("++:2", b);
|
|
|
|
|
|
2018-12-06 23:04:45 +03:00
|
|
|
da = TO_DATA(a);
|
|
|
|
|
db = TO_DATA(b);
|
|
|
|
|
|
2018-12-11 10:22:23 +03:00
|
|
|
__pre_gc () ;
|
2020-02-07 19:42:24 +03:00
|
|
|
|
|
|
|
|
push_extra_root (&a);
|
|
|
|
|
push_extra_root (&b);
|
2023-03-27 10:09:54 +02:00
|
|
|
d = (data *) alloc (sizeof(int) + LEN(da->data_header) + LEN(db->data_header) + 1);
|
2020-02-07 19:42:24 +03:00
|
|
|
pop_extra_root (&b);
|
|
|
|
|
pop_extra_root (&a);
|
2018-05-25 09:53:10 +03:00
|
|
|
|
2020-02-07 20:10:16 +03:00
|
|
|
da = TO_DATA(a);
|
|
|
|
|
db = TO_DATA(b);
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
d->data_header = STRING_TAG | ((LEN(da->data_header) + LEN(db->data_header)) << 3);
|
2018-05-25 09:53:10 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
strncpy (d->contents , da->contents, LEN(da->data_header));
|
|
|
|
|
strncpy (d->contents + LEN(da->data_header), db->contents, LEN(db->data_header));
|
2020-01-03 01:38:49 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
d->contents[LEN(da->data_header) + LEN(db->data_header)] = 0;
|
2018-05-25 09:53:10 +03:00
|
|
|
|
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;
|
2020-01-28 03:01:54 +03:00
|
|
|
void *s;
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2020-01-04 21:50:14 +03:00
|
|
|
ASSERT_STRING("sprintf:1", fmt);
|
2019-12-21 02:34:56 +03:00
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
|
fix_unboxed (fmt, args);
|
|
|
|
|
|
|
|
|
|
createStringBuf ();
|
|
|
|
|
|
|
|
|
|
vprintStringBuf (fmt, args);
|
|
|
|
|
|
|
|
|
|
__pre_gc ();
|
|
|
|
|
|
2020-02-27 18:11:35 +03:00
|
|
|
push_extra_root ((void**)&fmt);
|
2020-01-28 03:01:54 +03:00
|
|
|
s = Bstring (stringBuf.contents);
|
2020-02-27 18:11:35 +03:00
|
|
|
pop_extra_root ((void**)&fmt);
|
2019-12-21 02:34:56 +03:00
|
|
|
|
|
|
|
|
__post_gc ();
|
|
|
|
|
|
|
|
|
|
deleteStringBuf ();
|
|
|
|
|
|
2020-01-28 03:01:54 +03:00
|
|
|
return s;
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
2020-03-13 19:41:14 +03:00
|
|
|
extern void* LgetEnv (char *var) {
|
|
|
|
|
char *e = getenv (var);
|
|
|
|
|
void *s;
|
|
|
|
|
|
|
|
|
|
if (e == NULL)
|
2023-03-27 10:09:54 +02:00
|
|
|
return BOX(0); // TODO add (void*) cast?
|
2020-03-13 19:41:14 +03:00
|
|
|
|
|
|
|
|
__pre_gc ();
|
|
|
|
|
|
|
|
|
|
s = Bstring (e);
|
|
|
|
|
|
|
|
|
|
__post_gc ();
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Lsystem (char *cmd) {
|
|
|
|
|
return BOX (system (cmd));
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2020-01-04 21:50:14 +03:00
|
|
|
ASSERT_BOXED("fprintf:1", f);
|
|
|
|
|
ASSERT_STRING("fprintf:2", s);
|
|
|
|
|
|
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);
|
|
|
|
|
|
2020-01-04 21:50:14 +03:00
|
|
|
ASSERT_STRING("printf:1", s);
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
va_start (args, s);
|
|
|
|
|
fix_unboxed (s, args);
|
|
|
|
|
|
|
|
|
|
if (vprintf (s, args) < 0) {
|
|
|
|
|
failure ("fprintf (...): %s\n", strerror (errno));
|
|
|
|
|
}
|
2020-01-15 05:24:35 +03:00
|
|
|
|
|
|
|
|
fflush (stdout);
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-23 23:18:00 +03:00
|
|
|
extern FILE* Lfopen (char *f, char *m) {
|
2020-01-04 21:50:14 +03:00
|
|
|
FILE* h;
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2020-01-04 21:50:14 +03:00
|
|
|
ASSERT_STRING("fopen:1", f);
|
|
|
|
|
ASSERT_STRING("fopen:2", m);
|
|
|
|
|
|
|
|
|
|
h = fopen (f, m);
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
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) {
|
2020-01-04 21:50:14 +03:00
|
|
|
ASSERT_BOXED("fclose", 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) {
|
2020-01-28 03:01:54 +03:00
|
|
|
void * s = Bstring (buf);
|
2020-02-25 01:19:15 +03:00
|
|
|
|
|
|
|
|
getchar ();
|
2019-12-21 02:34:56 +03:00
|
|
|
|
|
|
|
|
free (buf);
|
2020-01-28 03:01:54 +03:00
|
|
|
return s;
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errno != 0)
|
|
|
|
|
failure ("readLine (): %s\n", strerror (errno));
|
|
|
|
|
|
2020-02-27 18:11:35 +03:00
|
|
|
return (void*) BOX (0);
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void* Lfread (char *fname) {
|
2020-01-04 21:50:14 +03:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
|
|
ASSERT_STRING("fread", fname);
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2020-01-04 21:50:14 +03:00
|
|
|
f = fopen (fname, "r");
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
if (f) {
|
|
|
|
|
if (fseek (f, 0l, SEEK_END) >= 0) {
|
|
|
|
|
long size = ftell (f);
|
2020-01-30 23:36:15 +03:00
|
|
|
void *s = LmakeString (BOX(size));
|
2019-12-21 02:34:56 +03:00
|
|
|
|
|
|
|
|
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) {
|
2020-01-04 21:50:14 +03:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
|
|
ASSERT_STRING("fwrite:1", fname);
|
|
|
|
|
ASSERT_STRING("fwrite:2", contents);
|
|
|
|
|
|
|
|
|
|
f = fopen (fname, "w");
|
2019-12-21 02:34:56 +03:00
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
|
if (fprintf (f, "%s", contents) < 0);
|
|
|
|
|
else {
|
|
|
|
|
fclose (f);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
failure ("fwrite (\"%s\"): %s\n", fname, strerror (errno));
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 01:38:22 +03:00
|
|
|
extern void* Lfexists (char *fname) {
|
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
|
|
ASSERT_STRING("fexists", fname);
|
|
|
|
|
|
|
|
|
|
f = fopen (fname, "r");
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
if (f) return BOX(1); // (void*) cast?
|
2021-11-19 01:38:22 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
return BOX(0); // (void*) cast?
|
2021-11-19 01:38:22 +03:00
|
|
|
}
|
|
|
|
|
|
2019-12-24 03:59:05 +03:00
|
|
|
extern void* Lfst (void *v) {
|
|
|
|
|
return Belem (v, BOX(0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void* Lsnd (void *v) {
|
|
|
|
|
return Belem (v, BOX(1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void* Lhd (void *v) {
|
|
|
|
|
return Belem (v, BOX(0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void* Ltl (void *v) {
|
|
|
|
|
return Belem (v, BOX(1));
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2020-08-02 23:56:21 +03:00
|
|
|
extern int Lrandom (int n) {
|
|
|
|
|
ASSERT_UNBOXED("Lrandom, 0", n);
|
|
|
|
|
|
|
|
|
|
if (UNBOX(n) <= 0) {
|
|
|
|
|
failure ("invalid range in random: %d\n", UNBOX(n));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BOX (random () % UNBOX(n));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Ltime () {
|
|
|
|
|
struct timespec t;
|
|
|
|
|
|
|
|
|
|
clock_gettime (CLOCK_MONOTONIC_RAW, &t);
|
|
|
|
|
|
|
|
|
|
return BOX(t.tv_sec * 1000000 + t.tv_nsec / 1000);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 03:01:54 +03:00
|
|
|
extern void set_args (int argc, char *argv[]) {
|
|
|
|
|
data *a;
|
2020-01-28 16:24:26 +03:00
|
|
|
int n = argc, *p = NULL;
|
2020-01-28 03:01:54 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
__pre_gc ();
|
|
|
|
|
|
2020-01-28 18:06:09 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
indent++; print_indent ();
|
|
|
|
|
printf ("set_args: call: n=%i &p=%p p=%p: ", n, &p, p); fflush(stdout);
|
|
|
|
|
for (i = 0; i < n; i++)
|
2020-01-29 14:21:17 +03:00
|
|
|
printf("%s ", argv[i]);
|
2020-02-07 19:42:24 +03:00
|
|
|
printf("EE\n");
|
2020-01-28 18:06:09 +03:00
|
|
|
#endif
|
|
|
|
|
|
2020-01-28 16:24:26 +03:00
|
|
|
p = LmakeArray (BOX(n));
|
2020-02-27 18:11:35 +03:00
|
|
|
push_extra_root ((void**)&p);
|
2020-01-28 03:01:54 +03:00
|
|
|
|
2020-01-28 18:06:09 +03:00
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-28 18:06:09 +03:00
|
|
|
printf ("set_args: iteration %i %p %p ->\n", i, &p, p); fflush(stdout);
|
|
|
|
|
#endif
|
2020-02-27 18:11:35 +03:00
|
|
|
((int*)p) [i] = (int) Bstring (argv[i]);
|
2020-01-28 18:06:09 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-28 18:06:09 +03:00
|
|
|
printf ("set_args: iteration %i <- %p %p\n", i, &p, p); fflush(stdout);
|
|
|
|
|
#endif
|
2020-01-28 03:01:54 +03:00
|
|
|
}
|
2020-02-07 19:42:24 +03:00
|
|
|
|
2020-02-27 18:11:35 +03:00
|
|
|
pop_extra_root ((void**)&p);
|
2020-01-28 03:01:54 +03:00
|
|
|
__post_gc ();
|
|
|
|
|
|
2020-01-28 16:24:26 +03:00
|
|
|
global_sysargs = p;
|
2020-02-27 18:11:35 +03:00
|
|
|
push_extra_root ((void**)&global_sysargs);
|
2020-02-07 19:42:24 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
|
|
|
|
print_indent ();
|
|
|
|
|
printf ("set_args: end\n", n, &p, p); fflush(stdout);
|
|
|
|
|
indent--;
|
|
|
|
|
#endif
|
2020-01-28 03:01:54 +03:00
|
|
|
}
|
|
|
|
|
|
2018-11-06 16:18:09 +03:00
|
|
|
/* GC starts here */
|
|
|
|
|
|
2020-08-02 23:56:21 +03:00
|
|
|
static int enable_GC = 1;
|
|
|
|
|
|
|
|
|
|
extern void LenableGC () {
|
|
|
|
|
enable_GC = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void LdisableGC () {
|
|
|
|
|
enable_GC = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 20:29:53 +03:00
|
|
|
extern const size_t __start_custom_data, __stop_custom_data;
|
2018-11-21 14:23:35 +03:00
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
# ifdef __ENABLE_GC__
|
|
|
|
|
|
2020-08-02 23:56:21 +03:00
|
|
|
extern void __gc_init ();
|
2019-10-16 01:13:52 +03:00
|
|
|
|
|
|
|
|
# else
|
|
|
|
|
|
2020-08-02 23:56:21 +03:00
|
|
|
# define __gc_init __gc_init_subst
|
2019-10-16 01:13:52 +03:00
|
|
|
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 */
|
|
|
|
|
/* ======================================== */
|
|
|
|
|
|
2020-02-15 22:58:43 +03:00
|
|
|
//static size_t SPACE_SIZE = 16;
|
2020-08-22 20:11:41 +03:00
|
|
|
static size_t SPACE_SIZE = 256 * 1024 * 1024;
|
2020-01-14 17:09:43 +03:00
|
|
|
// static size_t SPACE_SIZE = 128;
|
|
|
|
|
// static size_t SPACE_SIZE = 1024 * 1024;
|
2018-11-29 18:27:59 +03:00
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
static int free_pool (memory_chunk * p) {
|
2020-01-14 17:09:43 +03:00
|
|
|
size_t *a = p->begin, b = p->size;
|
|
|
|
|
p->begin = NULL;
|
|
|
|
|
p->size = 0;
|
|
|
|
|
p->end = NULL;
|
|
|
|
|
p->current = NULL;
|
|
|
|
|
return munmap((void *)a, b);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-14 18:36:37 +03:00
|
|
|
static void init_to_space (int flag) {
|
|
|
|
|
size_t space_size = 0;
|
|
|
|
|
if (flag) SPACE_SIZE = SPACE_SIZE << 1;
|
|
|
|
|
space_size = SPACE_SIZE * sizeof(size_t);
|
2020-01-14 17:09:43 +03:00
|
|
|
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) {
|
|
|
|
|
perror ("EROOR: init_to_space: mmap failed\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
to_space.current = to_space.begin;
|
|
|
|
|
to_space.end = to_space.begin + SPACE_SIZE;
|
|
|
|
|
to_space.size = SPACE_SIZE;
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
static void gc_swap_spaces (void) {
|
2020-01-14 17:09:43 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
indent++; print_indent ();
|
2020-01-14 17:09:43 +03:00
|
|
|
printf ("gc_swap_spaces\n"); fflush (stdout);
|
|
|
|
|
#endif
|
|
|
|
|
free_pool (&from_space);
|
|
|
|
|
from_space.begin = to_space.begin;
|
2018-11-29 18:27:59 +03:00
|
|
|
from_space.current = current;
|
2020-01-14 17:09:43 +03:00
|
|
|
from_space.end = to_space.end;
|
|
|
|
|
from_space.size = to_space.size;
|
|
|
|
|
to_space.begin = NULL;
|
|
|
|
|
to_space.current = NULL;
|
|
|
|
|
to_space.end = NULL;
|
2020-02-27 18:11:35 +03:00
|
|
|
to_space.size = 0;
|
2020-02-07 19:42:24 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
|
|
|
|
indent--;
|
|
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# define IS_VALID_HEAP_POINTER(p)\
|
|
|
|
|
(!UNBOXED(p) && \
|
2020-02-27 18:11:35 +03:00
|
|
|
(size_t)from_space.begin <= (size_t)p && \
|
|
|
|
|
(size_t)from_space.end > (size_t)p)
|
2018-11-29 18:27:59 +03:00
|
|
|
|
|
|
|
|
# define IN_PASSIVE_SPACE(p) \
|
2020-02-27 18:11:35 +03:00
|
|
|
((size_t)to_space.begin <= (size_t)p && \
|
|
|
|
|
(size_t)to_space.end > (size_t)p)
|
2018-11-29 18:27:59 +03:00
|
|
|
|
|
|
|
|
# define IS_FORWARD_PTR(p) \
|
|
|
|
|
(!UNBOXED(p) && IN_PASSIVE_SPACE(p))
|
|
|
|
|
|
2020-02-19 15:28:29 +03:00
|
|
|
int is_valid_heap_pointer (void *p) {
|
|
|
|
|
return IS_VALID_HEAP_POINTER(p);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-29 18:27:59 +03:00
|
|
|
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;
|
2020-01-14 17:09:43 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
indent++; print_indent ();
|
2020-01-14 17:09:43 +03:00
|
|
|
printf ("copy_elements: start; len = %d\n", len); fflush (stdout);
|
|
|
|
|
#endif
|
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++;
|
2020-01-14 17:09:43 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("copy_elements: copy NON ptr: %zu %p \n", elem, elem); fflush (stdout);
|
2020-01-14 17:09:43 +03:00
|
|
|
#endif
|
2018-12-05 18:31:12 +03:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-24 17:05:53 +03:00
|
|
|
printf ("copy_elements: fix element: %p -> %p\n", elem, *where);
|
|
|
|
|
fflush (stdout);
|
2018-12-05 18:31:12 +03:00
|
|
|
#endif
|
2020-02-07 19:42:24 +03:00
|
|
|
p = gc_copy ((size_t*) elem);
|
2020-02-27 18:11:35 +03:00
|
|
|
*where = (size_t) p;
|
2018-12-05 18:31:12 +03:00
|
|
|
where ++;
|
|
|
|
|
}
|
2020-01-14 17:09:43 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-24 17:05:53 +03:00
|
|
|
printf ("copy_elements: iteration end: where = %p, *where = %p, i = %d, \
|
|
|
|
|
len = %d\n", where, *where, i, len); fflush (stdout);
|
2020-01-14 17:09:43 +03:00
|
|
|
#endif
|
|
|
|
|
|
2018-12-05 18:31:12 +03:00
|
|
|
}
|
2020-01-14 17:09:43 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("\tcopy_elements: end\n"); fflush (stdout);
|
|
|
|
|
indent--;
|
2020-01-14 17:09:43 +03:00
|
|
|
#endif
|
|
|
|
|
|
2018-12-05 18:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-14 18:36:37 +03:00
|
|
|
static int extend_spaces (void) {
|
2020-01-14 17:09:43 +03:00
|
|
|
void *p = (void *) BOX (NULL);
|
2019-04-24 16:02:29 +03:00
|
|
|
size_t old_space_size = SPACE_SIZE * sizeof(size_t),
|
|
|
|
|
new_space_size = (SPACE_SIZE << 1) * sizeof(size_t);
|
2020-01-14 17:09:43 +03:00
|
|
|
p = mremap(to_space.begin, old_space_size, new_space_size, 0);
|
2020-02-07 19:42:24 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
|
|
|
|
indent++; print_indent ();
|
|
|
|
|
#endif
|
2020-01-14 17:09:43 +03:00
|
|
|
if (p == MAP_FAILED) {
|
2020-01-14 18:36:37 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-14 18:36:37 +03:00
|
|
|
printf ("extend: extend_spaces: mremap failed\n"); fflush (stdout);
|
|
|
|
|
#endif
|
|
|
|
|
return 1;
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
2018-12-05 18:31:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-24 17:05:53 +03:00
|
|
|
printf ("extend: %p %p %p %p\n", p, to_space.begin, to_space.end, current);
|
|
|
|
|
fflush (stdout);
|
2020-02-07 19:42:24 +03:00
|
|
|
indent--;
|
2018-12-05 18:31:12 +03:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
to_space.size = SPACE_SIZE;
|
2020-01-14 18:36:37 +03:00
|
|
|
return 0;
|
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;
|
2020-02-07 19:42:24 +03:00
|
|
|
indent++; print_indent ();
|
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
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("gc_copy: invalid ptr: %p\n", obj); fflush (stdout);
|
2020-02-07 19:42:24 +03:00
|
|
|
indent--;
|
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
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-24 17:05: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
|
|
|
}
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
if (IS_FORWARD_PTR(d->data_header)) {
|
2018-12-05 19:06:07 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2023-03-27 10:09:54 +02:00
|
|
|
printf ("gc_copy: IS_FORWARD_PTR: return! %p -> %p\n", obj, (size_t *) d->data_header);
|
2018-12-05 18:31:12 +03:00
|
|
|
fflush(stdout);
|
2020-02-07 19:42:24 +03:00
|
|
|
indent--;
|
2018-12-05 19:06:07 +03:00
|
|
|
#endif
|
2023-03-27 10:09:54 +02:00
|
|
|
return (size_t *) d->data_header;
|
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
|
2023-03-27 10:09:54 +02:00
|
|
|
switch (TAG(d->data_header)) {
|
2019-10-16 01:13:52 +03:00
|
|
|
case CLOSURE_TAG:
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2023-03-27 10:09:54 +02:00
|
|
|
printf ("gc_copy:closure_tag; len = %zu\n", LEN(d->data_header)); fflush (stdout);
|
2019-10-16 01:13:52 +03:00
|
|
|
#endif
|
2023-03-27 10:09:54 +02:00
|
|
|
i = LEN(d->data_header);
|
|
|
|
|
// current += LEN(d->data_header) + 1;
|
|
|
|
|
// current += ((LEN(d->data_header) + 1) * sizeof(int) -1) / sizeof(size_t) + 1;
|
2020-02-07 19:42:24 +03:00
|
|
|
current += i+1;
|
2023-03-27 10:09:54 +02:00
|
|
|
*copy = d->data_header;
|
2019-10-16 01:13:52 +03:00
|
|
|
copy++;
|
2023-03-27 10:09:54 +02:00
|
|
|
d->data_header = (int) copy;
|
2020-01-14 17:09:43 +03:00
|
|
|
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
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2023-03-27 10:09:54 +02:00
|
|
|
printf ("gc_copy:array_tag; len = %zu\n", LEN(d->data_header)); fflush (stdout);
|
2018-12-05 19:06:07 +03:00
|
|
|
#endif
|
2023-03-27 10:09:54 +02:00
|
|
|
current += ((LEN(d->data_header) + 1) * sizeof (int) - 1) / sizeof (size_t) + 1;
|
|
|
|
|
*copy = d->data_header;
|
2018-11-29 18:27:59 +03:00
|
|
|
copy++;
|
2023-03-27 10:09:54 +02:00
|
|
|
i = LEN(d->data_header);
|
|
|
|
|
d->data_header = (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
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2023-03-27 10:09:54 +02:00
|
|
|
printf ("gc_copy:string_tag; len = %d\n", LEN(d->data_header) + 1); fflush (stdout);
|
2018-12-05 19:06:07 +03:00
|
|
|
#endif
|
2023-03-27 10:09:54 +02:00
|
|
|
current += (LEN(d->data_header) + sizeof(int)) / sizeof(size_t) + 1;
|
|
|
|
|
*copy = d->data_header;
|
2018-11-29 18:27:59 +03:00
|
|
|
copy++;
|
2023-03-27 10:09:54 +02:00
|
|
|
d->data_header = (int) copy;
|
2020-02-27 18:11:35 +03:00
|
|
|
strcpy ((char*)©[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;
|
2023-03-27 10:09:54 +02:00
|
|
|
len1 = LEN(s->contents.data_header);
|
|
|
|
|
len2 = LEN(s->data_header);
|
|
|
|
|
len3 = LEN(d->data_header);
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-24 17:05:53 +03:00
|
|
|
printf ("gc_copy:sexp_tag; len1 = %li, len2=%li, len3 = %li\n",
|
|
|
|
|
len1, len2, len3);
|
2019-04-24 17:34:53 +03:00
|
|
|
fflush (stdout);
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2023-03-27 10:09:54 +02:00
|
|
|
i = LEN(s->contents.data_header);
|
2020-01-24 17:05:53 +03:00
|
|
|
current += i + 2;
|
2018-11-29 18:27:59 +03:00
|
|
|
*copy = s->tag;
|
|
|
|
|
copy++;
|
2023-03-27 10:09:54 +02:00
|
|
|
*copy = d->data_header;
|
2018-11-29 18:27:59 +03:00
|
|
|
copy++;
|
2023-03-27 10:09:54 +02:00
|
|
|
d->data_header = (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
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2023-03-27 10:09:54 +02:00
|
|
|
printf ("ERROR: gc_copy: weird data_header: %p", TAG(d->data_header)); fflush (stdout);
|
2020-02-07 19:42:24 +03:00
|
|
|
indent--;
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2023-03-27 10:09:54 +02:00
|
|
|
perror ("ERROR: gc_copy: weird data_header");
|
2019-04-24 17:34:53 +03:00
|
|
|
exit (1);
|
2020-02-07 19:42:24 +03:00
|
|
|
return (obj);
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-24 17:05: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);
|
2020-02-07 19:42:24 +03:00
|
|
|
indent--;
|
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) {
|
2020-02-07 19:42:24 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
|
|
|
|
indent++;
|
|
|
|
|
#endif
|
2020-01-16 17:46:09 +03:00
|
|
|
if (IS_VALID_HEAP_POINTER(*root)) {
|
2018-12-05 18:31:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("gc_test_and_copy_root: root %p top=%p bot=%p *root %p \n", root, __gc_stack_top, __gc_stack_bottom, *root);
|
2020-01-24 17:05:53 +03:00
|
|
|
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
|
|
|
}
|
2020-02-07 19:42:24 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
|
|
|
|
else {
|
|
|
|
|
print_indent ();
|
|
|
|
|
printf ("gc_test_and_copy_root: INVALID HEAP POINTER root %p *root %p\n", root, *root);
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
}
|
|
|
|
|
indent--;
|
|
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
extern void gc_root_scan_data (void) {
|
2020-02-27 18:11:35 +03:00
|
|
|
size_t * p = (size_t*)&__start_custom_data;
|
|
|
|
|
while (p < (size_t*)&__stop_custom_data) {
|
|
|
|
|
gc_test_and_copy_root ((size_t**)p);
|
2018-11-29 18:27:59 +03:00
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 16:24:26 +03:00
|
|
|
static void* gc (size_t size) {
|
2020-08-02 23:56:21 +03:00
|
|
|
if (! enable_GC) {
|
|
|
|
|
Lfailure ("GC disabled");
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-29 18:27:59 +03:00
|
|
|
current = to_space.begin;
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("gc: current:%p; to_space.b =%p; to_space.e =%p; \
|
|
|
|
|
f_space.b = %p; f_space.e = %p; __gc_stack_top=%p; __gc_stack_bottom=%p\n",
|
|
|
|
|
current, to_space.begin, to_space.end, from_space.begin, from_space.end,
|
|
|
|
|
__gc_stack_top, __gc_stack_bottom);
|
2019-04-24 17:34:53 +03:00
|
|
|
fflush (stdout);
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2020-02-07 19:42:24 +03:00
|
|
|
gc_root_scan_data ();
|
2018-12-05 18:31:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
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 ();
|
2020-01-29 14:21:17 +03:00
|
|
|
for (int i = 0; i < extra_roots.current_free; i++) {
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-29 14:21:17 +03:00
|
|
|
printf ("gc: extra_root № %i: %p %p\n", i, extra_roots.roots[i],
|
|
|
|
|
(size_t*) extra_roots.roots[i]);
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
#endif
|
2020-02-27 18:11:35 +03:00
|
|
|
gc_test_and_copy_root ((size_t**)extra_roots.roots[i]);
|
2020-01-29 14:21:17 +03:00
|
|
|
}
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-29 14:21:17 +03:00
|
|
|
printf ("gc: no more extra roots\n"); fflush (stdout);
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-11-29 18:27:59 +03:00
|
|
|
if (!IN_PASSIVE_SPACE(current)) {
|
2020-01-24 17:05:53 +03:00
|
|
|
printf ("gc: ASSERT: !IN_PASSIVE_SPACE(current) to_begin = %p to_end = %p \
|
|
|
|
|
current = %p\n", to_space.begin, to_space.end, current);
|
|
|
|
|
fflush (stdout);
|
2018-11-29 18:27:59 +03:00
|
|
|
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
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("gc: pre-extend_spaces : %p %zu %p \n", current, size, to_space.end);
|
2019-04-24 17:34:53 +03:00
|
|
|
fflush (stdout);
|
2018-12-05 18:31:12 +03:00
|
|
|
#endif
|
2020-01-14 18:36:37 +03:00
|
|
|
if (extend_spaces ()) {
|
2020-01-23 20:11:31 +03:00
|
|
|
gc_swap_spaces ();
|
2020-01-14 18:36:37 +03:00
|
|
|
init_to_space (1);
|
|
|
|
|
return gc (size);
|
|
|
|
|
}
|
2018-12-05 18:31:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("gc: post-extend_spaces: %p %zu %p \n", current, size, to_space.end);
|
2019-04-24 17:34:53 +03:00
|
|
|
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
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2020-01-24 17:05:53 +03:00
|
|
|
printf ("gc: end: (allocate!) return %p; from_space.current %p; \
|
|
|
|
|
from_space.end %p \n\n",
|
2019-04-24 17:34:53 +03:00
|
|
|
current, from_space.current, from_space.end);
|
|
|
|
|
fflush (stdout);
|
2020-02-07 19:42:24 +03:00
|
|
|
indent--;
|
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;
|
2020-01-23 20:34:21 +03:00
|
|
|
size_t elem_number = 0;
|
|
|
|
|
|
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;
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
switch (TAG(d->data_header)) {
|
2019-04-24 16:02:29 +03:00
|
|
|
|
|
|
|
|
case STRING_TAG:
|
2020-01-23 20:34:21 +03:00
|
|
|
printf ("(=>%p): STRING\n\t%s; len = %i %zu\n",
|
|
|
|
|
d->contents, d->contents,
|
2023-03-27 10:09:54 +02:00
|
|
|
LEN(d->data_header), LEN(d->data_header) + 1 + sizeof(int));
|
2019-04-24 16:02:29 +03:00
|
|
|
fflush (stdout);
|
2023-03-27 10:09:54 +02:00
|
|
|
len = (LEN(d->data_header) + sizeof(int)) / sizeof(size_t) + 1;
|
2019-04-24 16:02:29 +03:00
|
|
|
break;
|
|
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
case CLOSURE_TAG:
|
|
|
|
|
printf ("(=>%p): CLOSURE\n\t", d->contents);
|
2023-03-27 10:09:54 +02:00
|
|
|
len = LEN(d->data_header);
|
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);
|
2023-03-27 10:09:54 +02:00
|
|
|
len = LEN(d->data_header);
|
2019-04-24 16:02:29 +03:00
|
|
|
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);
|
2023-03-27 10:09:54 +02:00
|
|
|
char * data_header = de_hash (GET_SEXP_TAG(s->data_header));
|
|
|
|
|
printf ("(=>%p): SEXP\n\tdata_header(%s) ", s->contents.contents, data_header);
|
|
|
|
|
len = LEN(d->data_header);
|
2019-04-24 16:02:29 +03:00
|
|
|
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:
|
2020-01-23 20:34:21 +03:00
|
|
|
printf ("\nprintFromSpace: end: %zu elements\n===================\n\n",
|
|
|
|
|
elem_number);
|
2019-04-24 16:02:29 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
default:
|
2023-03-27 10:09:54 +02:00
|
|
|
printf ("\nprintFromSpace: ERROR: bad data_header %d", TAG(d->data_header));
|
|
|
|
|
perror ("\nprintFromSpace: ERROR: bad data_header");
|
2019-04-24 16:02:29 +03:00
|
|
|
fflush (stdout);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
2020-01-24 17:05:53 +03:00
|
|
|
cur += len;
|
2019-04-24 16:02:29 +03:00
|
|
|
printf ("len = %zu, new cur = %p\n", len, cur);
|
2020-01-23 20:34:21 +03:00
|
|
|
elem_number++;
|
2019-04-24 16:02:29 +03:00
|
|
|
}
|
2020-01-24 17:05:53 +03:00
|
|
|
printf ("\nprintFromSpace: end: the whole space is printed:\
|
|
|
|
|
%zu elements\n===================\n\n", elem_number);
|
2020-01-14 17:09:43 +03:00
|
|
|
fflush (stdout);
|
2019-04-24 16:02:29 +03:00
|
|
|
}
|
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__
|
2020-01-24 17:05:53 +03:00
|
|
|
// alloc: allocates `size` bytes in heap
|
2018-12-11 10:22:23 +03:00
|
|
|
extern void * alloc (size_t size) {
|
|
|
|
|
void * p = (void*)BOX(NULL);
|
2020-01-24 17:05:53 +03:00
|
|
|
size = (size - 1) / sizeof(size_t) + 1; // convert bytes to words
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
indent++; print_indent ();
|
|
|
|
|
printf ("alloc: current: %p %zu words!", from_space.current, size);
|
|
|
|
|
fflush (stdout);
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2020-01-24 17:05:53 +03:00
|
|
|
if (from_space.current + size < from_space.end) {
|
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
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2019-04-24 17:34:53 +03:00
|
|
|
printf (";new current: %p \n", from_space.current); fflush (stdout);
|
2020-02-07 19:42:24 +03:00
|
|
|
indent--;
|
2018-11-30 16:18:12 +03:00
|
|
|
#endif
|
2018-11-29 18:27:59 +03:00
|
|
|
return p;
|
|
|
|
|
}
|
2022-01-31 23:46:18 +03:00
|
|
|
|
2020-01-23 20:11:31 +03:00
|
|
|
init_to_space (0);
|
2018-11-30 16:18:12 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("alloc: call gc: %zu\n", size); fflush (stdout);
|
|
|
|
|
printFromSpace(); fflush (stdout);
|
2020-01-23 20:11:31 +03:00
|
|
|
p = gc (size);
|
2020-02-07 19:42:24 +03:00
|
|
|
print_indent ();
|
|
|
|
|
printf("alloc: gc END %p %p %p %p\n\n", from_space.begin,
|
2020-01-28 16:24:26 +03:00
|
|
|
from_space.end, from_space.current, p); fflush (stdout);
|
2019-04-24 17:34:53 +03:00
|
|
|
printFromSpace(); fflush (stdout);
|
2020-02-07 19:42:24 +03:00
|
|
|
indent--;
|
2020-01-23 20:11:31 +03:00
|
|
|
return p;
|
|
|
|
|
#else
|
2019-04-24 17:34:53 +03:00
|
|
|
return gc (size);
|
2020-01-23 20:11:31 +03:00
|
|
|
#endif
|
2018-11-06 16:18:09 +03:00
|
|
|
}
|
2019-10-16 01:13:52 +03:00
|
|
|
# endif
|