Permalink
Browse files

Fix more things

  • Loading branch information...
1 parent 7087e54 commit 1b9b4d6994b0ca59efba79e9dce89c09f267dc11 @txus committed Mar 2, 2013
Showing with 40 additions and 23 deletions.
  1. +1 −1 Makefile
  2. +1 −0 src/terror/function.c
  3. +0 −3 src/terror/gc.c
  4. +1 −6 src/terror/input_reader.c
  5. +3 −1 src/terror/primitives.c
  6. +3 −1 src/terror/value.c
  7. +23 −4 src/terror/vm.c
  8. +2 −2 src/terror/vm.h
  9. +6 −5 tests/vm_tests.c
View
2 Makefile
@@ -4,7 +4,7 @@ LIBS=$(OPTLIBS)
LDFLAGS=deps/libtreadmill/build/libtreadmill.a
PREFIX?=/usr/local
-DEPS=gc_debug
+DEPS=gc
SOURCES=$(wildcard src/**/*.c src/*.c)
OBJECTS=$(patsubst %.c,%.o,$(SOURCES))
View
1 src/terror/function.c
@@ -67,6 +67,7 @@ Function_call(
// Native function dispatch
if(fn->c_fn) {
VALUE result = Function_native_call(state, fn, receiver, locals);
+ DArray_destroy(locals);
Stack_push(STACK, result);
return ret; // return where we were
}
View
3 src/terror/gc.c
@@ -87,9 +87,6 @@ GC_rootset(TmStateHeader *state_header)
for(int i=0; i < DArray_count(parent->locals); i++) {
ROOT(DArray_at(parent->locals, i));
}
- /* for(int i=0; i < DArray_count(parent->fn->literals); i++) { */
- /* ROOT(DArray_at(parent->fn->literals, i)); */
- /* } */
}
if(frame->fn->scope) {
View
7 src/terror/input_reader.c
@@ -111,12 +111,7 @@ BytecodeFile_destroy(BytecodeFile *file)
if(file->filename) bdestroy(file->filename);
if(file->compiled_filename) bdestroy(file->compiled_filename);
- for(int i = 0; i < DArray_count(file->function_names); i++) {
- bstring name = (bstring)DArray_at(file->function_names, i);
- /* Function *fn = (Function*)Hashmap_get(file->functions, name); */
- /* Function_destroy(fn); */
- bdestroy(name);
- }
+ Hashmap_traverse(file->functions, Hashmap_Function_destroy);
Hashmap_destroy(file->functions);
free(file);
View
4 src/terror/primitives.c
@@ -139,11 +139,13 @@ Primitive_require(STATE, void *a, void *_, void *__)
bstring fn_name = (bstring)DArray_at(file->function_names, j);
Function *fn = (Function*)Hashmap_get(file->functions, fn_name);
- if(bstrcmp(fn_name, bfromcstr("0_main")) == 0) {
+ bstring main_name = bfromcstr("0_main");
+ if(bstrcmp(fn_name, main_name) == 0) {
main = fn;
} else {
Hashmap_set(state->functions, fn_name, fn);
}
+ bdestroy(main_name);
}
CallFrame *frame = CallFrame_new(state->lobby, main, NULL);
View
4 src/terror/value.c
@@ -269,7 +269,9 @@ Value_each(VALUE obj, Slots_iter iter)
for(int i = 0; i < DArray_count(keys); i++) {
VALUE k = (VALUE)DArray_at(keys, i);
if(k) {
- VALUE v = (VALUE)Hashmap_get(hash, bfromcstr(VAL2STR(k)));
+ bstring str = bfromcstr(VAL2STR(k));
+ VALUE v = (VALUE)Hashmap_get(hash, str);
+ bdestroy(str);
iter(k, v);
}
}
View
27 src/terror/vm.c
@@ -30,7 +30,15 @@ void Stack_print(STATE, Stack* stack)
printf("\n");
}
-#define STATE_FN(A) (Function*)Hashmap_get(state->functions, bfromcstr((A)))
+void Value_print_all(STATE, DArray* objs)
+{
+ for(int i=0; i < DArray_count(objs); i++) {
+ Value_print(state, (VALUE)DArray_at(objs, i));
+ printf("\n");
+ }
+}
+
+#define STATE_FN(A) (Function*)Hashmap_get(state->functions, (A))
#define LITERAL(A) (VALUE)DArray_at(CURR_FRAME->fn->literals, (A))
#define LOCAL(A) CallFrame_getlocal(CURR_FRAME, (A))
#define LOCALSET(A, B) CallFrame_setlocal(CURR_FRAME, (A), (B))
@@ -49,7 +57,9 @@ void VM_start(bstring filename)
BytecodeFile *file = BytecodeFile_new(state, filename);
state->functions = file->functions;
- CallFrame *top_frame = CallFrame_new(lobby, STATE_FN("0_main"), NULL);
+ bstring main_fn = bfromcstr("0_main");
+ CallFrame *top_frame = CallFrame_new(lobby, STATE_FN(main_fn), NULL);
+ bdestroy(main_fn);
top_frame->name = "main";
Stack_push(FRAMES, top_frame);
@@ -186,7 +196,9 @@ VALUE VM_run(STATE)
ip++;
debugi("DEFN %i", *ip);
VALUE fn_name = LITERAL(*ip);
- VALUE closure = Closure_new(state, STATE_FN(VAL2STR(fn_name)), CURR_FRAME);
+ bstring state_fn = bfromcstr(VAL2STR(fn_name));
+ VALUE closure = Closure_new(state, STATE_FN(state_fn), CURR_FRAME);
+ bdestroy(state_fn);
Stack_push(STACK, closure);
break;
}
@@ -218,7 +230,7 @@ VALUE VM_run(STATE)
VALUE name = LITERAL(op1);
int argcount = op2;
- DArray *locals = DArray_create(sizeof(VALUE), 10);
+ DArray *locals = DArray_create(sizeof(VALUE), argcount);
while(argcount--) {
DArray_push(locals, Stack_pop(STACK));
}
@@ -347,6 +359,13 @@ VALUE VM_run(STATE)
Debugger_evaluate(state);
debugi("DUMP");
Stack_print(state, STACK);
+ DArray *literals = CURR_FRAME->fn->literals;
+ printf("--LITERALS (%i)--\n", DArray_count(literals));
+ Value_print_all(state, literals);
+
+ DArray *locals = CURR_FRAME->locals;
+ printf("--LOCALS (%i)--\n", DArray_count(locals));
+ Value_print_all(state, locals);
break;
}
}
View
4 src/terror/vm.h
@@ -10,15 +10,15 @@ VALUE VM_run(STATE);
void VM_start(bstring filename);
void Stack_print(STATE, Stack* stack);
+void Value_print_all(STATE, DArray *objs);
#define ABORT() \
CallFrame_print_backtrace(CURR_FRAME); \
exit(EXIT_FAILURE)
#ifdef DEBUGI
#define debugi(M, ...) debug(M, ##__VA_ARGS__)
#else
-#define debugi(M, ...) debug(M, ##__VA_ARGS__)
-// #define debugi(M, ...)
+#define debugi(M, ...)
#endif
#endif
View
11 tests/vm_tests.c
@@ -7,7 +7,6 @@
#include <terror/state.h>
#include <terror/bootstrap.h>
#include <terror/vm.h>
-#include <terror/state.h>
#include <assert.h>
// Extern global objects declared in runtime.h
@@ -20,12 +19,15 @@ VALUE NilObject;
#define PUSH_LOCAL(A, N) VALUE (N) = (A); DArray_push(locals, (N))
#define SETUP() \
+ STATE = State_new(); \
DArray *literals = DArray_create(sizeof(VALUE), 10); \
Hashmap *fns = Hashmap_create(NULL, NULL); \
- DArray *locals = DArray_create(sizeof(VALUE), 10); \
- STATE = State_new(); \
+ Function *fn = calloc(1, sizeof(Function)); \
+ fn->literals = literals; \
+ Hashmap_set(fns, bfromcstr("0_main"), fn); \
Hashmap_destroy(state->functions); \
state->functions = fns; \
+ DArray *locals = DArray_create(sizeof(VALUE), 10); \
Runtime_init(state); \
#define TEARDOWN() \
@@ -49,7 +51,6 @@ VALUE NilObject;
#define RUN(...) \
int code[] = (int[]){__VA_ARGS__}; \
\
- Function *fn = calloc(1, sizeof(Function)); \
fn->code = code; \
fn->literals = literals; \
\
@@ -59,7 +60,7 @@ VALUE NilObject;
top_frame->locals = locals; \
top_frame->name = "main"; \
Stack_push(FRAMES, top_frame); \
- State_bootstrap(state); \
+ /* State_bootstrap(state); \\ */ \
VALUE result = VM_run(state); \
char *test_pushself()

0 comments on commit 1b9b4d6

Please sign in to comment.