mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-05 22:38:44 +00:00
crap code
This commit is contained in:
parent
b22daf080d
commit
3fdc3e7f2a
16 changed files with 7066 additions and 248 deletions
|
|
@ -1,5 +1,5 @@
|
|||
CC=gcc
|
||||
COMMON_FLAGS=-m32 -g2 -fstack-protector-all
|
||||
COMMON_FLAGS=-g2 -fstack-protector-all
|
||||
PROD_FLAGS=$(COMMON_FLAGS) -DLAMA_ENV
|
||||
TEST_FLAGS=$(COMMON_FLAGS) -DDEBUG_VERSION
|
||||
UNIT_TESTS_FLAGS=$(TEST_FLAGS)
|
||||
|
|
|
|||
18
runtime/gc.h
18
runtime/gc.h
|
|
@ -37,11 +37,12 @@
|
|||
#define SET_FORWARD_ADDRESS(x, addr) (x = ((x & 3) | ((int)(addr))))
|
||||
// if heap is full after gc shows in how many times it has to be extended
|
||||
#define EXTRA_ROOM_HEAP_COEFFICIENT 2
|
||||
#ifdef DEBUG_VERSION
|
||||
# define MINIMUM_HEAP_CAPACITY (8)
|
||||
#else
|
||||
# define MINIMUM_HEAP_CAPACITY (1 << 2)
|
||||
#endif
|
||||
// #ifdef DEBUG_VERSION
|
||||
// # define MINIMUM_HEAP_CAPACITY (8)
|
||||
// #else
|
||||
// # define MINIMUM_HEAP_CAPACITY (1 << 2)
|
||||
#define MINIMUM_HEAP_CAPACITY (1 << 16)
|
||||
// #endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
|
@ -66,7 +67,6 @@ typedef struct {
|
|||
size_t size;
|
||||
} memory_chunk;
|
||||
|
||||
|
||||
// the only GC-related function that should be exposed, others are useful for tests and internal implementation
|
||||
// allocates object of the given size on the heap
|
||||
void *alloc(size_t);
|
||||
|
|
@ -91,7 +91,6 @@ size_t compute_locations ();
|
|||
void update_references (memory_chunk *);
|
||||
void physically_relocate (memory_chunk *);
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// GC extra roots
|
||||
// ============================================================================
|
||||
|
|
@ -112,7 +111,6 @@ void clear_extra_roots (void);
|
|||
void push_extra_root (void **p);
|
||||
void pop_extra_root (void **p);
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// Implemented in GASM: see gc_runtime.s
|
||||
// ============================================================================
|
||||
|
|
@ -128,7 +126,6 @@ void __init (void);
|
|||
// to deallocate all object allocated via GC
|
||||
extern void __shutdown (void);
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// invoked from GASM: see gc_runtime.s
|
||||
// ============================================================================
|
||||
|
|
@ -136,7 +133,6 @@ extern void gc_test_and_mark_root (size_t **root);
|
|||
bool is_valid_heap_pointer (const size_t *);
|
||||
static inline bool is_valid_pointer (const size_t *);
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// Auxiliary functions for tests
|
||||
// ============================================================================
|
||||
|
|
@ -148,7 +144,6 @@ static inline bool is_valid_pointer (const size_t *);
|
|||
size_t objects_snapshot (int *object_ids_buf, size_t object_ids_buf_size);
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef DEBUG_VERSION
|
||||
// essential function to mock program stack
|
||||
void set_stack (size_t stack_top, size_t stack_bottom);
|
||||
|
|
@ -157,7 +152,6 @@ void set_stack (size_t stack_top, size_t stack_bottom);
|
|||
void set_extra_roots (size_t extra_roots_size, void **extra_roots_ptr);
|
||||
#endif
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// Utility functions
|
||||
// ============================================================================
|
||||
|
|
|
|||
|
|
@ -672,7 +672,7 @@ extern int Lcompare (void *p, void *q) {
|
|||
}
|
||||
}
|
||||
|
||||
extern void *Belem (void *p, int i) {
|
||||
extern void *Belem (void *p, long i) {
|
||||
data *a = (data *)BOX(NULL);
|
||||
|
||||
if (UNBOXED(p)) { ASSERT_BOXED(".elem:1", p); }
|
||||
|
|
@ -682,7 +682,7 @@ extern void *Belem (void *p, int i) {
|
|||
i = UNBOX(i);
|
||||
|
||||
switch (TAG(a->data_header)) {
|
||||
case STRING_TAG: return (void *)BOX(a->contents[i]);
|
||||
case STRING_TAG: return (void *)BOX((char)a->contents[i]);
|
||||
case SEXP_TAG: return (void *)((int *)a->contents)[i + 1];
|
||||
default: return (void *)((int *)a->contents)[i];
|
||||
}
|
||||
|
|
@ -866,17 +866,19 @@ extern void *Bsexp (int bn, ...) {
|
|||
va_end(args);
|
||||
|
||||
POST_GC();
|
||||
return (int *)r->contents;
|
||||
// printf("bsexp: %ld %p", r->contents, r->contents);
|
||||
// fflush(stdout);
|
||||
return (void *)r->contents;
|
||||
}
|
||||
|
||||
extern int Btag (void *d, int t, int n) {
|
||||
extern long Btag (void *d, int t, int n) {
|
||||
data *r;
|
||||
|
||||
if (UNBOXED(d)) return BOX(0);
|
||||
else {
|
||||
r = TO_DATA(d);
|
||||
return BOX(TAG(r->data_header) == SEXP_TAG && TO_SEXP(d)->tag == UNBOX(t)
|
||||
&& LEN(r->data_header) == UNBOX(n));
|
||||
return (long)BOX(TAG(r->data_header) == SEXP_TAG && TO_SEXP(d)->tag == UNBOX(t)
|
||||
&& LEN(r->data_header) == UNBOX(n));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -938,7 +940,7 @@ extern int Bsexp_tag_patt (void *x) {
|
|||
return BOX(TAG(TO_DATA(x)->data_header) == SEXP_TAG);
|
||||
}
|
||||
|
||||
extern void *Bsta (void *v, int i, void *x) {
|
||||
extern void *Bsta (void *v, long i, void *x) {
|
||||
if (UNBOXED(i)) {
|
||||
ASSERT_BOXED(".sta:3", x);
|
||||
data *d = TO_DATA(x);
|
||||
|
|
@ -1070,7 +1072,7 @@ extern void *LgetEnv (char *var) {
|
|||
extern int Lsystem (char *cmd) { return BOX(system(cmd)); }
|
||||
|
||||
extern void Lfprintf (FILE *f, char *s, ...) {
|
||||
va_list args = (va_list)BOX(NULL);
|
||||
va_list args; // = (va_list)BOX(NULL);
|
||||
|
||||
ASSERT_BOXED("fprintf:1", f);
|
||||
ASSERT_STRING("fprintf:2", s);
|
||||
|
|
@ -1082,7 +1084,7 @@ extern void Lfprintf (FILE *f, char *s, ...) {
|
|||
}
|
||||
|
||||
extern void Lprintf (char *s, ...) {
|
||||
va_list args = (va_list)BOX(NULL);
|
||||
va_list args; // = (va_list)BOX(NULL);
|
||||
|
||||
ASSERT_STRING("printf:1", s);
|
||||
|
||||
|
|
@ -1188,12 +1190,13 @@ extern void *Lhd (void *v) { return Belem(v, BOX(0)); }
|
|||
extern void *Ltl (void *v) { return Belem(v, BOX(1)); }
|
||||
|
||||
/* Lread is an implementation of the "read" construct */
|
||||
extern int Lread () {
|
||||
int result = BOX(0);
|
||||
extern long Lread () {
|
||||
// int result = BOX(0);
|
||||
int64_t result = BOX(0);
|
||||
|
||||
printf("> ");
|
||||
fflush(stdout);
|
||||
scanf("%d", &result);
|
||||
scanf("%li", &result);
|
||||
|
||||
return BOX(result);
|
||||
}
|
||||
|
|
@ -1209,8 +1212,8 @@ extern int Lbinoperror2 (void) {
|
|||
}
|
||||
|
||||
/* Lwrite is an implementation of the "write" construct */
|
||||
extern int Lwrite (int n) {
|
||||
printf("%d\n", UNBOX(n));
|
||||
extern long Lwrite (long n) {
|
||||
printf("%ld\n", UNBOX(n));
|
||||
fflush(stdout);
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
6568
runtime/runtime.s
Normal file
6568
runtime/runtime.s
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -12,13 +12,14 @@
|
|||
#define CLOSURE_TAG 0x00000007
|
||||
#define UNBOXED_TAG 0x00000009 // Not actually a data_header; used to return from LkindOf
|
||||
|
||||
#define LEN(x) ((x & 0xFFFFFFF8) >> 3)
|
||||
#define LEN(x) (long)(((int)x & 0xFFFFFFF8) >> 3)
|
||||
#define TAG(x) (x & 0x00000007)
|
||||
|
||||
#define SEXP_ONLY_HEADER_SZ (sizeof(int))
|
||||
|
||||
#ifndef DEBUG_VERSION
|
||||
# define DATA_HEADER_SZ (sizeof(size_t) + sizeof(int))
|
||||
// # define DATA_HEADER_SZ (sizeof(size_t) + sizeof(int))
|
||||
# define DATA_HEADER_SZ (sizeof(size_t) + sizeof(long))
|
||||
#else
|
||||
# define DATA_HEADER_SZ (sizeof(size_t) + sizeof(size_t) + sizeof(int))
|
||||
#endif
|
||||
|
|
@ -28,9 +29,9 @@
|
|||
#define TO_DATA(x) ((data *)((char *)(x)-DATA_HEADER_SZ))
|
||||
#define TO_SEXP(x) ((sexp *)((char *)(x)-DATA_HEADER_SZ))
|
||||
|
||||
#define UNBOXED(x) (((int)(x)) & 0x0001)
|
||||
#define UNBOX(x) (((int)(x)) >> 1)
|
||||
#define BOX(x) ((((int)(x)) << 1) | 0x0001)
|
||||
#define UNBOXED(x) (((long)(x)) & 0x0001)
|
||||
#define UNBOX(x) (((long)(x)) >> 1)
|
||||
#define BOX(x) ((((long)(x)) << 1) | 0x0001)
|
||||
|
||||
#define BYTES_TO_WORDS(bytes) (((bytes)-1) / sizeof(size_t) + 1)
|
||||
#define WORDS_TO_BYTES(words) ((words) * sizeof(size_t))
|
||||
|
|
@ -42,7 +43,7 @@
|
|||
typedef struct {
|
||||
// store tag in the last three bits to understand what structure this is, other bits are filled with
|
||||
// other utility info (i.e., size for array, number of fields for s-expression)
|
||||
int data_header;
|
||||
long data_header;
|
||||
|
||||
#ifdef DEBUG_VERSION
|
||||
size_t id;
|
||||
|
|
@ -57,7 +58,7 @@ typedef struct {
|
|||
typedef struct {
|
||||
// store tag in the last three bits to understand what structure this is, other bits are filled with
|
||||
// other utility info (i.e., size for array, number of fields for s-expression)
|
||||
int data_header;
|
||||
long data_header;
|
||||
|
||||
#ifdef DEBUG_VERSION
|
||||
size_t id;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue