85 lines
1.2 KiB
C
85 lines
1.2 KiB
C
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
// common types
|
|
typedef uintptr_t U;
|
|
typedef intptr_t I;
|
|
|
|
typedef uint8_t U8;
|
|
typedef uint32_t U32;
|
|
typedef int32_t I32;
|
|
typedef size_t Z;
|
|
|
|
// objects
|
|
typedef uintptr_t O;
|
|
|
|
// cons pair
|
|
typedef struct C C;
|
|
struct C {
|
|
O head, tail;
|
|
};
|
|
|
|
// symbol
|
|
typedef struct S S;
|
|
struct S {
|
|
U8 *data;
|
|
U32 hash;
|
|
Z len;
|
|
};
|
|
|
|
// symbol table
|
|
typedef struct St St;
|
|
struct St {
|
|
I count;
|
|
Z capacity;
|
|
S **data;
|
|
};
|
|
|
|
// gc header
|
|
enum { OBJ_CONS, OBJ_SYM, OBJ_FWD };
|
|
|
|
typedef struct H H;
|
|
struct H {
|
|
I type;
|
|
Z size;
|
|
};
|
|
|
|
// heap
|
|
typedef struct E E;
|
|
struct E {
|
|
struct {
|
|
U8 *start, *end;
|
|
U8 *free;
|
|
} from, to;
|
|
|
|
I root_count;
|
|
Z root_capacity;
|
|
O **roots;
|
|
};
|
|
|
|
extern E heap;
|
|
extern St syms;
|
|
|
|
#define IMM(x) ((x) & 1)
|
|
#define NUM(x) (((O)((I)(x) << 1)) | (V)1)
|
|
#define ORD(x) ((I)(x) >> 1)
|
|
#define BOX(x) ((O)(x))
|
|
#define UNBOX(x) ((H *)(x))
|
|
|
|
#define NIL ((O)0)
|
|
#define GC_HEAP_BYTES (1024 * 1024)
|
|
|
|
// GC
|
|
void addroot(O *ptr);
|
|
I rootmark(void);
|
|
void rootreset(I mark);
|
|
void collect(void);
|
|
H *alloc(Z sz);
|
|
void gcinit(void);
|
|
void gcfinalize(void);
|
|
|
|
S *intern(const char *str, I len);
|
|
O mksym(const char *str);
|
|
|
|
O cons(O head, O tail);
|
|
C *uncons(O obj);
|