This commit is contained in:
danyabeerzun 2019-04-24 17:34:53 +03:00
parent 23697829b9
commit 49d86298a1
7 changed files with 73 additions and 1041 deletions

View file

@ -23,7 +23,7 @@ size_t *current;
/* end */
/* GC extern invariant for built-in functions */
extern void __pre_gc ();
extern void __pre_gc ();
extern void __post_gc ();
/* end */
@ -32,16 +32,16 @@ extern void __post_gc ();
# define SEXP_TAG 0x00000005
# define LEN(x) ((x & 0xFFFFFFF8) >> 3)
# define TAG(x) (x & 0x00000007)
# define TAG(x) (x & 0x00000007)
# define TO_DATA(x) ((data*)((char*)(x)-sizeof(int)))
# define TO_SEXP(x) ((sexp*)((char*)(x)-2*sizeof(int)))
#ifdef DEBUG_PRINT // GET_SEXP_TAG is necessary for printing from space
# define GET_SEXP_TAG(x) (LEN(x))
#endif
//new
# define GET_TAG_FROM_SEXP(x) (LEN(x))
# define UNBOXED(x) (((int) (x)) & 0x0001)
# define UNBOX(x) (((int) (x)) >> 1)
# define UNBOXED(x) (((int) (x)) & 0x0001)
# define UNBOX(x) (((int) (x)) >> 1)
# define BOX(x) ((((int) (x)) << 1) | 0x0001)
typedef struct {
@ -57,7 +57,7 @@ typedef struct {
extern void* alloc (size_t);
extern int Blength (void *p) {
data *a = (char*) BOX (NULL);
data *a = (data*) BOX (NULL);
a = TO_DATA(p);
return BOX(LEN(a->tag));
}
@ -65,13 +65,20 @@ extern int Blength (void *p) {
char* de_hash (int n) {
static char *chars = (char*) BOX (NULL);
static char buf[6] = {0,0,0,0,0,0};
char *p = (char*) BOX (NULL);
char *p = (char *) BOX (NULL);
chars = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNJPQRSTUVWXYZ";
p = &buf[5];
#ifdef DEBUG_PRINT
printf ("de_hash: tag: %d\n", n); fflush (stdout);
#endif
*p-- = 0;
while (n != 0) {
#ifdef DEBUG_PRINT
printf ("char: %c\n", chars [n & 0x003F]); fflush (stdout);
#endif
*p-- = chars [n & 0x003F];
n = n >> 6;
}
@ -148,8 +155,11 @@ static void printValue (void *p) {
break;
case SEXP_TAG: {
// char * tag = de_hash (TO_SEXP(p)->tag);
char * tag = de_hash (GET_TAG_FROM_SEXP(TO_SEXP(p)->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;
@ -181,7 +191,7 @@ static void printValue (void *p) {
}
}
break;
default:
printStringBuf ("*** invalid tag: %x ***", TAG(a->tag));
}
@ -218,7 +228,7 @@ extern void* Bstring (void *p) {
}
extern void* Bstringval (void *p) {
void *s = BOX(NULL);
void *s = (void *) BOX (NULL);
__pre_gc () ;
@ -243,8 +253,7 @@ extern void* Barray (int n, ...) {
__pre_gc ();
#ifdef DEBUG_PRINT
printf ("Barray: create n = %d\n", n);
fflush(stdout);
printf ("Barray: create n = %d\n", n); fflush(stdout);
#endif
r = (data*) alloc (sizeof(int) * (n+1));
@ -270,12 +279,12 @@ extern void* Bsexp (int n, ...) {
int ai = BOX(0);
size_t * p = NULL;
sexp *r = (sexp*) BOX (NULL);
data *d = (sexp*) BOX (NULL);
data *d = (data *) BOX (NULL);
__pre_gc () ;
#ifdef DEBUG_PRINT
printf("Bsexp: allocate %zu!\n",sizeof(int) * (n+1));
printf("Bsexp: allocate %zu!\n",sizeof(int) * (n+1)); fflush (stdout);
#endif
r = (sexp*) alloc (sizeof(int) * (n+1));
d = &(r->contents);
@ -294,9 +303,10 @@ extern void* Bsexp (int n, ...) {
r->tag = va_arg(args, int);
// new line (next)
#ifdef DEBUG_PRINT
r->tag = SEXP_TAG | ((r->tag) << 3);
#endif
va_end(args);
__post_gc();
@ -305,10 +315,14 @@ extern void* Bsexp (int n, ...) {
}
extern int Btag (void *d, int t, int n) {
data *r = (data*) BOX (NULL);
data *r = (data *) BOX (NULL);
r = TO_DATA(d);
#ifndef DEBUG_PRINT
return BOX(TAG(r->tag) == SEXP_TAG && TO_SEXP(d)->tag == t && LEN(r->tag) == n);
#else
return BOX(TAG(r->tag) == SEXP_TAG &&
GET_TAG_FROM_SEXP(TO_SEXP(d)->tag) == t && LEN(r->tag) == n);
GET_SEXP_TAG(TO_SEXP(d)->tag) == t && LEN(r->tag) == n);
#endif
}
extern int Barray_patt (void *d, int n) {
@ -490,7 +504,7 @@ static void copy_elements (size_t *where, size_t *from, int len) {
p = gc_copy ((size_t*) elem);
*where = p;
#ifdef DEBUG_PRINT
printf ("copy_elements: fix %x: %x\n", from, *where);
printf ("copy_elements: fix %p: %p\n", from, *where); fflush (stdout);
#endif
where ++;
}
@ -528,19 +542,20 @@ extern size_t * gc_copy (size_t *obj) {
int len1, len2, len3;
void * objj;
void * newobjj = (void*)current;
printf("gc_copy: %x cur = %x starts\n", obj, current);
printf ("gc_copy: %p cur = %p starts\n", obj, current);
fflush (stdout);
#endif
if (!IS_VALID_HEAP_POINTER(obj)) {
#ifdef DEBUG_PRINT
printf ("gc_copy: invalid ptr: %x\n", obj);
printf ("gc_copy: invalid ptr: %p\n", obj); fflush (stdout);
#endif
return obj;
}
if (!IN_PASSIVE_SPACE(current) && current != to_space.end) {
#ifdef DEBUG_PRINT
printf("ERROR: gc_copy: out-of-space %x %x %x\n", current, to_space.begin, to_space.end);
printf("ERROR: gc_copy: out-of-space %p %p %p\n", current, to_space.begin, to_space.end);
fflush(stdout);
#endif
perror("ERROR: gc_copy: out-of-space\n");
@ -549,7 +564,7 @@ extern size_t * gc_copy (size_t *obj) {
if (IS_FORWARD_PTR(d->tag)) {
#ifdef DEBUG_PRINT
printf ("gc_copy: IS_FORWARD_PTR: return! %x\n", (size_t *) d->tag);
printf ("gc_copy: IS_FORWARD_PTR: return! %p\n", (size_t *) d->tag);
fflush(stdout);
#endif
return (size_t *) d->tag;
@ -562,8 +577,7 @@ extern size_t * gc_copy (size_t *obj) {
switch (TAG(d->tag)) {
case ARRAY_TAG:
#ifdef DEBUG_PRINT
printf ("gc_copy:array_tag; len = %zu\n", LEN(d->tag));
fflush(stdout);
printf ("gc_copy:array_tag; len = %zu\n", LEN(d->tag)); fflush (stdout);
#endif
current += (LEN(d->tag) + 1) * sizeof (int);
*copy = d->tag;
@ -575,8 +589,7 @@ extern size_t * gc_copy (size_t *obj) {
case STRING_TAG:
#ifdef DEBUG_PRINT
printf ("gc_copy:string_tag; len = %d\n", LEN(d->tag) + 1);
fflush(stdout);
printf ("gc_copy:string_tag; len = %d\n", LEN(d->tag) + 1); fflush (stdout);
#endif
current += LEN(d->tag) * sizeof(char) + sizeof (int);
*copy = d->tag;
@ -592,7 +605,8 @@ extern size_t * gc_copy (size_t *obj) {
len1 = LEN(s->contents.tag);
len2 = LEN(s->tag);
len3 = LEN(d->tag);
printf("len1 = %li, len2=%li, len3 = %li\n",len1,len2,len3);
printf ("len1 = %li, len2=%li, len3 = %li\n", len1, len2, len3);
fflush (stdout);
#endif
current += (LEN(s->contents.tag) + 2) * sizeof (int);
*copy = s->tag;
@ -606,14 +620,13 @@ extern size_t * gc_copy (size_t *obj) {
default:
#ifdef DEBUG_PRINT
printf ("ERROR: gc_copy: weird tag: %x", TAG(d->tag));
fflush (stdout);
printf ("ERROR: gc_copy: weird tag: %p", TAG(d->tag)); fflush (stdout);
#endif
perror ("ERROR: gc_copy: weird tag");
exit (1);
exit (1);
}
#ifdef DEBUG_PRINT
printf ("gc_copy: %x (%x) -> %x (%x); new-current = %x\n", obj, objj, copy, newobjj, current);
printf ("gc_copy: %p(%p) -> %p (%p); new-current = %p\n", obj, objj, copy, newobjj, current);
fflush (stdout);
#endif
return copy;
@ -622,14 +635,10 @@ extern size_t * gc_copy (size_t *obj) {
extern void gc_test_and_copy_root (size_t ** root) {
if (IS_VALID_HEAP_POINTER(*root)) {
#ifdef DEBUG_PRINT
printf ("gc_test_and_copy_root: root %p %p\n", root, *root);
fflush (stdout);
printf ("gc_test_and_copy_root: root %p %p\n", root, *root); fflush (stdout);
#endif
*root = gc_copy (*root);
}
/* #ifdef DEBUG_PRINT */
/* else { printf ("gc_test_and_copy_root: NOT A PTR %p %p\n", root, *root); fflush (stdout); } */
/* #endif */
}
extern void gc_root_scan_data (void) {
@ -665,27 +674,29 @@ static int free_pool (pool * p) {
static void * gc (size_t size) {
current = to_space.begin;
#ifdef DEBUG_PRINT
printf("\ngc: current: %x; to_space.b = %x; to_space.e = %x; f_space.b = %x; f_space.e = %x\n",
current, to_space.begin, to_space.end, from_space.begin, from_space.end);
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);
#endif
gc_root_scan_data ();
gc_root_scan_data ();
#ifdef DEBUG_PRINT
printf("gc: data is scanned\n");
printf ("gc: data is scanned\n"); fflush (stdout);
#endif
__gc_trace_registers ();
__gc_root_scan_stack ();
if (!IN_PASSIVE_SPACE(current)) {
perror ("ASSERT: !IN_PASSIVE_SPACE(current)\n");
exit (1);
exit (1);
}
while (current + size >= to_space.end) {
#ifdef DEBUG_PRINT
printf ("gc pre-extend_spaces : %x %x %x \n", current, size, to_space.end);
printf ("gc pre-extend_spaces : %p %zu %p \n", current, size, to_space.end);
fflush (stdout);
#endif
extend_spaces ();
#ifdef DEBUG_PRINT
printf ("gc post-extend_spaces: %x %x %x \n", current, size, to_space.end);
printf ("gc post-extend_spaces: %p %zu %p \n", current, size, to_space.end);
fflush (stdout);
#endif
}
assert (IN_PASSIVE_SPACE(current));
@ -694,18 +705,20 @@ static void * gc (size_t size) {
gc_swap_spaces ();
from_space.current = current + size;
#ifdef DEBUG_PRINT
printf ("gc: end: (allocate!) return %x; from_space.current %x; from_space.end \n\n",
current, from_space.current, from_space.end);
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);
#endif
return (void *) current;
}
#ifdef DEBUG_PRINT
static void printFromSpace (void) {
size_t * cur = from_space.begin, *tmp = NULL;
data * d = NULL;
sexp * s = NULL;
size_t len = 0;
#ifdef DEBUG_PRINT
printf ("\nHEAP SNAPSHOT\n===================\n");
printf ("f_begin = %p, f_end = %p,\n", from_space.begin, from_space.end);
while (cur < from_space.current) {
@ -736,7 +749,7 @@ static void printFromSpace (void) {
case SEXP_TAG:
s = (sexp *) d;
d = (data *) &(s->contents);
char * tag = de_hash (GET_TAG_FROM_SEXP(s->tag));
char * tag = de_hash (GET_SEXP_TAG(s->tag));
printf ("(=>%p): SEXP\n\ttag(%s) ", s->contents.contents, tag);
len = LEN(d->tag);
tmp = (s->contents.contents);
@ -762,31 +775,30 @@ static void printFromSpace (void) {
cur += len * sizeof(int);
printf ("len = %zu, new cur = %p\n", len, cur);
}
#endif
}
#endif
extern void * alloc (size_t size) {
void * p = (void*)BOX(NULL);
if (from_space.current + size < from_space.end) {
#ifdef DEBUG_PRINT
printf("alloc: current: %x %zu", from_space.current, size);
printf ("alloc: current: %p %zu", from_space.current, size); fflush (stdout);
#endif
p = (void*) from_space.current;
from_space.current += size;
#ifdef DEBUG_PRINT
printf(";new current: %x \n", from_space.current);
printf (";new current: %p \n", from_space.current); fflush (stdout);
#endif
return p;
}
#ifdef DEBUG_PRINT
printf("alloc: call gc: %zu\n", size);
fflush (stdout);
printf ("alloc: call gc: %zu\n", size); fflush (stdout);
printFromSpace(); fflush (stdout);
#endif
printFromSpace();
p = gc (size);
#ifdef DEBUG_PRINT
printf("gc END\n\n");
printf("gc END\n\n"); fflush (stdout);
printFromSpace(); fflush (stdout);
#endif
printFromSpace();
return p;
return gc (size);
}