Skip to content

Commit

Permalink
load kernel files
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Dec 20, 2012
1 parent d5aada7 commit 15e9f25
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 25 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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 $@
Expand Down
46 changes: 46 additions & 0 deletions src/forkix/bootstrap.c
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

9 changes: 9 additions & 0 deletions src/forkix/bootstrap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _fx_bootstrap_h_
#define _fx_bootstrap_h_

#include <forkix/state.h>

void State_bootstrap(STATE state);

#endif

2 changes: 0 additions & 2 deletions src/forkix/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ VALUE
gc_alloc(size_t size)
{
VALUE val = calloc(1, size);
val->gc.refcount = 0;
return val;
}

Expand All @@ -15,4 +14,3 @@ gc_dealloc(VALUE obj)
free(obj);
obj = NULL;
}

2 changes: 1 addition & 1 deletion src/forkix/gc_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define _fx_gc_header_h_

struct gc_header_s {
int refcount;
short int marked;
};

typedef struct gc_header_s GCHeader;
Expand Down
2 changes: 2 additions & 0 deletions src/forkix/input_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand Down
2 changes: 2 additions & 0 deletions src/forkix/input_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
22 changes: 22 additions & 0 deletions src/forkix/opcode.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions src/forkix/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
6 changes: 5 additions & 1 deletion src/forkix/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down
18 changes: 0 additions & 18 deletions src/forkix/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion tests/input_reader_tests.c
Original file line number Diff line number Diff line change
@@ -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>

Expand Down

0 comments on commit 15e9f25

Please sign in to comment.