Permalink
Browse files

oh yeah

  • Loading branch information...
Josep M. Bach
Josep M. Bach committed Dec 29, 2012
1 parent 256d2bd commit 93f95edbdc0b4c015d5ec4f1af29f17ff8dba43d
View
@@ -1,4 +1,4 @@
-CC=clang
+Cn=clang
CFLAGS=-g -std=c11 -O3 -Wall -Werror -Isrc -DNDEBUG $(OPTFLAGS)
LIBS=$(OPTLIBS)
PREFIX?=/usr/local
@@ -23,6 +23,15 @@ all: $(TARGET) $(SO_TARGET) tests $(PROGRAMS)
dev: CFLAGS=-g -std=c11 -Wall -Isrc -Wall -Werror $(OPTFLAGS)
dev: all
+rubinius:
+ which rbx || (echo '\nYou need rubinius to compile the kernel.\n'; which rbx)
+kernel: rubinius
+ rm -rf `find kernel -name "*.tvm" -print`
+ cd compiler && rake kernel
+examples: rubinius
+ rm -rf `find examples -name "*.tvm" -print`
+ cd compiler && rake examples
+
$(TARGET): CFLAGS += -fPIC $(LIBS)
$(TARGET): build $(OBJECTS)
ar rcs $@ $(OBJECTS)
@@ -38,7 +47,7 @@ build:
@mkdir -p bin
# The Unit Tests
-.PHONY: tests
+.PHONY: tests kernel rubinius
tests: CFLAGS += $(TARGET)
tests: $(TESTS)
sh ./tests/runtests.sh
View
@@ -5,4 +5,22 @@ Rake::TestTask.new do |t|
t.verbose = true
end
+desc 'Compiles kernel files'
+task :kernel do
+ Dir['kernel/*.rb'].each do |src|
+ dest = "../#{src.gsub('rb','tvm')}"
+ puts "Compiling #{src} to #{dest}..."
+ `bin/terror #{src} > #{dest}`
+ end
+end
+
+desc 'Compiles example files'
+task :examples do
+ Dir['examples/*.rb'].each do |src|
+ dest = "../#{src.gsub('rb','tvm')}"
+ puts "Compiling #{src} to #{dest}..."
+ `bin/terror #{src} > #{dest}`
+ end
+end
+
task :default => :test
@@ -1,4 +1,4 @@
-parent = Object.new
+parent = {}
parent.age = 50
parent.money = 1000
child = parent.clone()
@@ -1,4 +1,4 @@
-puts VM.primitive[:+].apply(3, 4)
-puts VM.primitive[:-].apply(4, 3)
-puts VM.primitive[:*].apply(4, 3)
-puts VM.primitive[:/].apply(4, 2)
+puts VM.primitives[:+].apply(3, 4)
+puts VM.primitives[:-].apply(4, 3)
+puts VM.primitives[:*].apply(4, 3)
+puts VM.primitives[:/].apply(4, 2)
View
@@ -0,0 +1 @@
+puts VM.primitives
@@ -1,3 +1,2 @@
-def new
- VM.new_object
-end
+self.print = VM.primitives[:print]
+self.puts = VM.primitives[:puts]
@@ -114,7 +114,11 @@ def slot_retrieval(node, parent)
end
def attribute_assignment(node, parent)
- receiver_name = node.receiver.name
+ receiver_name = if node.receiver.is_a?(Rubinius::AST::Self)
+ 'self'
+ else
+ node.receiver.name
+ end
attribute_name = node.name[0..-2].to_sym
@slots[receiver_name] ||= []
@slots[receiver_name] << attribute_name
@@ -185,5 +185,15 @@ def compiles(code, &block)
end
end
end
+
+ describe 'assigning stuff to self' do
+ it 'is compiled' do
+ compiles("self.foo = 'bar'") do
+ _pushself
+ _push 0
+ _setslot 1
+ end
+ end
+ end
end
end
View
@@ -1,8 +0,0 @@
-_main
-:2:4
-"puts
-23
-17
-1
-145
-144
@@ -1,11 +1,11 @@
_main
:1:4
-"block_472
+"block_508
67 DEFN
0
20 PUSHNIL
144 RET
-_block_472
+_block_508
:1:3
3
17 PUSH
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,7 +1,7 @@
_main
:12:90
"VM
-"primitive
+"primitives
"+
"[]
3
File renamed without changes.
View
@@ -0,0 +1,18 @@
+_main
+:3:12
+"VM
+"primitives
+"puts
+16 PUSHSELF
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+128 SEND
+2
+1
+20 PUSHNIL
+144 RET
+
View
@@ -0,0 +1,38 @@
+_main
+:5:30
+"VM
+"primitives
+"print
+"[]
+"puts
+16 PUSHSELF
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+17 PUSH
+2
+128 SEND
+3
+1
+65 SETSLOT
+2
+16 PUSHSELF
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+17 PUSH
+4
+128 SEND
+3
+1
+65 SETSLOT
+4
+20 PUSHNIL
+144 RET
+
View
@@ -1,9 +1,16 @@
#include <forkix/bootstrap.h>
#include <forkix/darray.h>
#include <forkix/bstrlib.h>
-#include <forkix/input_reader.h>
+#include <forkix/runtime.h>
+#include <forkix/primitives.h>
#include <dirent.h>
+VALUE Integer_bp;
+VALUE String_bp;
+VALUE Vector_bp;
+VALUE Map_bp;
+VALUE Closure_bp;
+
static inline DArray*
kernel_files()
{
@@ -22,26 +29,51 @@ kernel_files()
return entries;
}
+#define DEFPRIM(M, N, F) DArray_push((M), String_new((N))); DArray_push((M), Closure_new(Function_native_new((F))))
+#define DEFVALUE(M, N, V) DArray_push((M), String_new((N))); DArray_push((M), (V));
+
+static inline void
+expose_VM(VALUE lobby)
+{
+ VALUE vm = Value_new(ObjectType);
+ Value_set(lobby, "VM", vm);
+
+ // VM.primitives map
+ DArray *primitives = DArray_create(sizeof(VALUE), 10);
+
+ DEFPRIM(primitives, "print", Primitive_print);
+ DEFPRIM(primitives, "puts", Primitive_puts);
+ DEFPRIM(primitives, "require", Primitive_require);
+
+ Value_set(vm, "primitives", Map_new(primitives));
+
+ // VM.types map
+ DArray *types = DArray_create(sizeof(VALUE), 10);
+
+ DEFVALUE(types, "integer", Integer_bp);
+ DEFVALUE(types, "string", String_bp);
+ DEFVALUE(types, "vector", Vector_bp);
+ DEFVALUE(types, "map", Map_bp);
+ DEFVALUE(types, "closure", Closure_bp);
+
+ Value_set(vm, "types", Map_new(primitives));
+}
+
void
State_bootstrap(STATE)
{
DArray *filenames = kernel_files();
int count = DArray_count(filenames);
- Hashmap *fns = state->functions;
+ // Expose toplevel constants
+ expose_VM(state->lobby);
// Load all files.
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);
- }
+ bstring path = bfromcstr("kernel/");
+ bconcat(path, filename);
+ debug("[BOOTSTRAP] Loading %s...", bdata(path));
+ Primitive_require(state, String_new(bdata(path)), NULL, NULL);
}
}
-
View
@@ -1,6 +1,7 @@
#include <forkix/function.h>
#include <forkix/value.h>
#include <forkix/stack.h>
+#include <forkix/state.h>
#include <forkix/call_frame.h>
Function*
@@ -39,9 +40,10 @@ Function_call(
STATE, // the State
Function *fn, // the Function we're calling
VALUE receiver, // the Receiver value
- DArray *locals, // the arguments to the function call
- int *ret) // where to return
+ DArray *locals) // the arguments to the function call
{
+ int *ret = state->ret;
+
// Native function dispatch
if(fn->c_fn) {
VALUE result = Function_native_call(state, fn, receiver, locals);
View
@@ -3,10 +3,10 @@
#include <forkix/darray.h>
#include <forkix/stack.h>
-#include <forkix/state.h>
+struct state_s;
struct val_s;
-typedef struct val_s* (*native_fn)(STATE, void*, void*, void*);
+typedef struct val_s* (*native_fn)(struct state_s*, void*, void*, void*);
struct function_s {
int *code;
@@ -17,7 +17,8 @@ typedef struct function_s Function;
Function* Function_new(int*, DArray*);
Function* Function_native_new(native_fn);
-struct val_s* Function_native_call(STATE, Function *fn, struct val_s*, DArray *locals);
-int* Function_call(STATE, Function *fn, struct val_s* receiver, DArray *locals, int *ret);
+struct val_s* Function_native_call(struct state_s*, Function*, struct val_s*, DArray*);
+int* Function_call(struct state_s*, Function*, struct val_s*, DArray*);
#endif
+
View
@@ -1,5 +1,10 @@
#include <forkix/primitives.h>
#include <forkix/runtime.h>
+#include <forkix/bstrlib.h>
+#include <forkix/state.h>
+#include <forkix/call_frame.h>
+#include <forkix/input_reader.h>
+#include <forkix/vm.h>
#include <assert.h>
/*
@@ -21,6 +26,35 @@ Primitive_puts(STATE, void *a, void *b, void *c)
return NilObject;
}
+VALUE
+Primitive_require(STATE, void *a, void *_, void *__)
+{
+ VALUE filename = (VALUE)a;
+ CHECK_TYPE(filename, StringType);
+ bstring filename_str = bfromcstr(VAL2STR(filename));
+
+ Hashmap *fns = state->functions;
+
+ Function *main = NULL; // entry point
+
+ BytecodeFile *file = BytecodeFile_new(filename_str);
+ int fn_count = DArray_count(file->function_names);
+ for(int j=0; j < fn_count; j++) {
+ bstring fn_name = (bstring)DArray_at(file->function_names, j);
+ Function *fn = (Function*)Hashmap_get(file->functions, fn_name);
+
+ if(bstrcmp(fn_name, bfromcstr("main")) == 0) {
+ main = fn;
+ } else {
+ Hashmap_set(fns, fn_name, fn);
+ }
+ }
+
+ CallFrame *frame = CallFrame_new(state->lobby, main, NULL);
+ Stack_push(FRAMES, frame);
+
+ return VM_run(state);
+}
/*
* Integer primitives
@@ -76,22 +110,6 @@ Primitive_Integer_div(STATE, void *a, void *b, void *_)
return Integer_new(VAL2INT(left) / VAL2INT(right));
}
-/*
- * Closure primitives
- */
-
-VALUE
-Primitive_Closure_apply(STATE, void *a, void *b, void *_)
-{
- VALUE left = (VALUE)a;
- VALUE right = (VALUE)b;
-
- CHECK_TYPE(left, IntegerType);
- CHECK_TYPE(right, IntegerType);
-
- return Integer_new(VAL2INT(left) + VAL2INT(right));
-}
-
/*
* Vector primitives
*/
Oops, something went wrong.

0 comments on commit 93f95ed

Please sign in to comment.