Permalink
Browse files

load kernel files

  • Loading branch information...
txus committed Dec 20, 2012
1 parent d5aada7 commit 15e9f25f296cb1fbd70d0f2a5b80952c23c6ba47
View
@@ -1,7 +1,8 @@
CC=clang
CFLAGS=-g -O3 -Wall -Werror -Isrc -DNDEBUG $(OPTFLAGS)
-LIBS=-ldl $(OPTLIBS)
+LIBS=$(OPTLIBS)
PREFIX?=/usr/local
+VPATH=vendor
SOURCES=$(wildcard src/**/*.c src/*.c)
OBJECTS=$(patsubst %.c,%.o,$(SOURCES))
@@ -15,13 +16,14 @@ PROGRAMS=$(patsubst %.c,%,$(PROGRAMS_SRC))
TARGET=build/libforkix.a
SO_TARGET=$(patsubst %.a,%.so,$(TARGET))
+
# The Target Build
all: $(TARGET) $(SO_TARGET) tests $(PROGRAMS)
dev: CFLAGS=-g -Wall -Isrc -Wall -Werror $(OPTFLAGS)
dev: all
-$(TARGET): CFLAGS += -fPIC
+$(TARGET): CFLAGS += -fPIC $(LIBS)
$(TARGET): build $(OBJECTS)
ar rcs $@ $(OBJECTS)
ranlib $@
View
@@ -0,0 +1,46 @@
+#include <forkix/bootstrap.h>
+#include <forkix/darray.h>
+#include <forkix/bstrlib.h>
+#include <forkix/input_reader.h>
+#include <dirent.h>
+
+static inline DArray*
+kernel_files()
+{
+ DArray *entries = DArray_create(sizeof(bstring), 10);
+
+ DIR *dirp = opendir("kernel"); // todo -- make it an absolute path
+ struct dirent *dp;
+ readdir(dirp); // .
+ readdir(dirp); // ..
+
+ while ((dp = readdir(dirp)) != NULL) {
+ DArray_push(entries, bfromcstr(dp->d_name));
+ }
+ (void)closedir(dirp);
+
+ return entries;
+}
+
+void
+State_bootstrap(STATE state)
+{
+ DArray *filenames = kernel_files();
+ int count = DArray_count(filenames);
+
+ Hashmap *fns = state->functions;
+
+ for(int i=0; i < count; i++) {
+ bstring filename = (bstring)DArray_at(filenames, i);
+ debug("[BOOTSTRAP] Loading %s...", bdata(filename));
+
+ BytecodeFile *file = BytecodeFile_new(filename);
+ int fn_count = DArray_count(file->function_names);
+ for(int j=0; i < fn_count; i++) {
+ bstring fn_name = (bstring)DArray_at(file->function_names, j);
+ Function *fn = (Function*)Hashmap_get(file->functions, fn_name);
+ Hashmap_set(fns, fn_name, fn);
+ }
+ }
+}
+
View
@@ -0,0 +1,9 @@
+#ifndef _fx_bootstrap_h_
+#define _fx_bootstrap_h_
+
+#include <forkix/state.h>
+
+void State_bootstrap(STATE state);
+
+#endif
+
View
@@ -5,7 +5,6 @@ VALUE
gc_alloc(size_t size)
{
VALUE val = calloc(1, size);
- val->gc.refcount = 0;
return val;
}
@@ -15,4 +14,3 @@ gc_dealloc(VALUE obj)
free(obj);
obj = NULL;
}
-
View
@@ -2,7 +2,7 @@
#define _fx_gc_header_h_
struct gc_header_s {
- int refcount;
+ short int marked;
};
typedef struct gc_header_s GCHeader;
@@ -103,6 +103,7 @@ parse_string(bstring buf, BytecodeFile *file)
}
Function *fn = Function_new(instructions, literals);
+ DArray_push(file->function_names, method);
Hashmap_set(file->functions, method, fn);
if(cnt >= lines->qty) break; // EOF
@@ -118,6 +119,7 @@ BytecodeFile *BytecodeFile_new(bstring filename)
file->filename = filename;
file->functions = Hashmap_create(NULL, NULL);
+ file->function_names = DArray_create(sizeof(bstring), 10);
bstring buf = file_read(filename);
check(buf, "Cannot read file %s", bdata(filename));
@@ -3,11 +3,13 @@
#include <forkix/bstrlib.h>
#include <forkix/function.h>
+#include <forkix/darray.h>
#include <forkix/hashmap.h>
typedef struct {
bstring filename;
int *code;
+ DArray *function_names;
Hashmap *functions;
} BytecodeFile;
View
@@ -0,0 +1,22 @@
+#ifndef _fx_opcode_h_
+#define _fx_opcode_h_
+
+typedef enum {
+ NOOP = 0,
+
+ PUSHSELF = 0x10, // 16
+ PUSHINT, // 17
+
+ PUSHLOCAL = 0x20, // 32
+
+ ADD, // 33
+
+ POP,
+
+ SEND = 0x80, // 128
+ RET = 0x90, // 144
+
+ DUMP, // 145
+} OpCode;
+
+#endif
View
@@ -2,9 +2,11 @@
#define _fx_state_h_
#include <forkix/hashmap.h>
+#include <forkix/darray.h>
struct state_s {
Hashmap *functions;
+ DArray *heap;
};
typedef struct state_s State;
#define STATE State*
View
@@ -3,11 +3,13 @@
#include <forkix/stack.h>
#include <forkix/call_frame.h>
#include <forkix/vm.h>
+#include <forkix/opcode.h>
#include <forkix/state.h>
+#include <forkix/bootstrap.h>
#include <forkix/bstrlib.h>
#include <forkix/function.h>
-static void dump(Stack* stack)
+static inline void dump(Stack* stack)
{
#ifndef NDEBUG
printf("---STACK---\n");
@@ -32,6 +34,8 @@ void VM_start(BytecodeFile *file)
VALUE main = Value_new(MainType); // toplevel object
+ State_bootstrap(state);
+
Stack *frames = Stack_create();
CallFrame *top_frame = CallFrame_new(main, STATE_FN("main"), NULL);
Stack_push(frames, top_frame);
View
@@ -5,24 +5,6 @@
#include <forkix/state.h>
#include <forkix/input_reader.h>
-typedef enum {
- NOOP = 0,
-
- PUSHSELF = 0x10, // 16
- PUSHINT, // 17
-
- PUSHLOCAL = 0x20, // 32
-
- ADD, // 33
-
- POP,
-
- SEND = 0x80, // 128
- RET = 0x90, // 144
-
- DUMP, // 145
-} OpCode;
-
void VM_run(STATE state, Stack *frames);
void VM_start(BytecodeFile *file);
@@ -1,6 +1,6 @@
#include "minunit.h"
#include <forkix/input_reader.h>
-#include <forkix/vm.h>
+#include <forkix/opcode.h>
#include <forkix/value.h>
#include <assert.h>

0 comments on commit 15e9f25

Please sign in to comment.