Skip to content

Commit

Permalink
So close...!
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Distad committed Feb 15, 2012
1 parent f44764c commit 3244b72
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -8,7 +8,7 @@ DEVICE = atmega328p
# DEVICE = atmega128
CLOCK = 16000000UL
# OBJECTS = src/arduino_io.o src/alist.o src/main.o src/print_form.o src/read_form.o src/types.o
OBJECTS = src/arduino_io.o src/print_form.o src/read_form.o src/types.o src/alist.o src/symbols.o src/gc.o src/main.o
OBJECTS = src/arduino_io.o src/print_form.o src/read_form.o src/types.o src/alist.o src/symbols.o src/gc.o src/debug.o src/main.o

UNOTTY = /dev/ttyACM0
UNOBAUD = 115200
Expand Down
12 changes: 12 additions & 0 deletions include/uberlisp/debug.h
@@ -0,0 +1,12 @@
#ifndef _UBERLISP_DEBUG_H
#define _UBERLISP_DEBUG_H

#include <stdio.h>
#include <avr/pgmspace.h>
#include <uberlisp/types.h>
#include <uberlisp/print_form.h>

void print_mem();
void print_env(uptr_t env);

#endif
1 change: 1 addition & 0 deletions include/uberlisp/gc.h
Expand Up @@ -4,6 +4,7 @@
#include <stdio.h>
#include <avr/pgmspace.h>
#include <uberlisp/types.h>
#include <uberlisp/debug.h>

void __GC__();

Expand Down
12 changes: 12 additions & 0 deletions src/debug.c
@@ -0,0 +1,12 @@
#include <uberlisp/debug.h>

void print_mem() {
printf_P(PSTR("Total mem:\t%dB\nFree mem:\t%dB\tUsed mem:\t%dB\nCons mem:\t%dB\tSymbol mem:\t%dB\n"),
TOTALMEM(), FREEMEM(), USEDMEM(), CONSMEM(), SYMMEM());
}

void print_env(uptr_t env) {
printf_P(PSTR("env: "));
print_form(env);
printf_P(PSTR("\n"));
}
40 changes: 29 additions & 11 deletions src/gc.c
Expand Up @@ -6,10 +6,15 @@ void __GC__() {
do {
printf_P(PSTR("Top of garbage loop.\n"));
garbage = 0;

uptr_t *cur = UPTR_PTR(CSTART_p);
printf_P(PSTR("Marking..."));
while (cur < PTREND_p) {
if (IS_CONS(*cur)) *UPTR_PTR(*cur) |= GC_FLAG;
if (IS_CONS(*cur)) {
Cons *cons = CONS_PTR(*cur);
cons->car |= GC_FLAG;
if (! IS_CADR(cons->cdr)) cons->cdr |= GC_FLAG;
}
cur++;
}
printf_P(PSTR("Done!\n"));
Expand All @@ -18,31 +23,44 @@ void __GC__() {
cur = UPTR_PTR(CSTART_p);
printf_P(PSTR("Sweeping...\n"));
while (cur < UPTR_PTR(CEND_p)) {
if (! (*cur & GC_FLAG)) {
if (! ((*cur & (GC_FLAG | CADR_FLAG)) || IS_NIL(*cur))) {
garbage = 1;
free_st = cur;
do {
++cur;
} while (IS_CADR(*cur));
++cur;

while (! (*cur & GC_FLAG) && cur < UPTR_PTR(CEND_p)) cur++;

if (IS_CADR(*cur)) *cur &= ~CADR_FLAG;
else while (IS_NIL(*cur) && cur < UPTR_PTR(CEND_p)) cur++;

free_end = cur;
int free_bytes = (intptr_t)free_end - (intptr_t)free_st;

int free_bytes = (intptr_t)free_end - (intptr_t)free_st,
kept_bytes = (intptr_t)free_st - CSTART_p;

printf_P(PSTR("Freeing %i bytes...\n"), free_bytes);
memmove(CPTR(CSTART_p + free_bytes), CPTR(CSTART_p), free_bytes);
printf_P(PSTR("free_st: %p\t free_end: %p\n"), free_st, free_end);

memmove(CPTR(CSTART_p + free_bytes), CPTR(CSTART_p), kept_bytes);
memset(CPTR(CSTART_p), 0, free_bytes);
CSTART_p += free_bytes;
for (; cur < PTREND_p; ++cur)
if (IS_CONS(*cur) && TO_PTR(*cur) < CSTART_p) *cur += free_bytes;

for (cur = UPTR_PTR(CSTART_p); cur < PTREND_p; ++cur)
if (IS_CONS(*cur) && TO_PTR(*cur) < UPTR(free_end)) *cur += free_bytes;
cur = free_end;

} else
++cur;
cur++;
}
printf_P(PSTR("Done!\n"));

printf_P(PSTR("Clearing GC bits..."));
for (cur = UPTR_PTR(CSTART_p); cur < UPTR_PTR(CEND_p); ++cur)
*cur &= ~GC_FLAG;
printf_P(PSTR("Done!\n"));

} while (garbage);
printf_P(PSTR("GC Complete!\n"));
print_mem();
print_form(*UPTR_PTR(CEND_p + 2));
printf_P(PSTR("\n"));
}
14 changes: 7 additions & 7 deletions src/main.c
Expand Up @@ -8,6 +8,7 @@
#include <uberlisp/read_form.h>
#include <uberlisp/print_form.h>
#include <uberlisp/gc.h>
#include <uberlisp/debug.h>

#include <stdio.h>

Expand Down Expand Up @@ -262,20 +263,19 @@ int main() {
uptr_t *env = refer(NIL);
init_syms(env);

printf_P(PSTR("env: "));
print_form(*env);
printf_P(PSTR("\n"));

uptr_t *form_p = refer(NIL);
while(1) {
printf_P(PSTR("Total mem:\t%dB\nFree mem:\t%dB\tUsed mem:\t%dB\nCons mem:\t%dB\tSymbol mem:\t%dB\n"),
TOTALMEM(), FREEMEM(), USEDMEM(), CONSMEM(), SYMMEM());
print_env(*env);
print_mem();

printf_P(PSTR("> "));
*form_p = read_form(stdin);
while(getc(stdin) != '\r');
print_form(eval(env, *form_p));
printf_P(PSTR("\n"));
//__GC__();

print_mem();
__GC__();
}

release(2); // Just a formality really...
Expand Down
2 changes: 1 addition & 1 deletion src/types.c
Expand Up @@ -104,7 +104,7 @@ void unhash_sym(char *buf, uptr_t sym_p) {
uptr_t *refer(uptr_t uptr) {
*PTREND_p = uptr;
PTREND_p++;
printf_P(PSTR("SP: %p\tPTREND_p %p\n"), SP, PTREND_p);
// printf_P(PSTR("SP: %p\tPTREND_p %p\n"), SP, PTREND_p);
return PTREND_p - 1;
}

Expand Down

0 comments on commit 3244b72

Please sign in to comment.