Skip to content

Commit

Permalink
small bugfix and vm bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Glowman554 committed Jan 21, 2024
1 parent ec1d417 commit 263798f
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ src/explorer/res
upload.sh
/flvm
modules
src/backend/config
src/backend/config
*.so
27 changes: 27 additions & 0 deletions examples/vm/main.fl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
$include <std.fl>
$include <vm.fl>

function spark(int argc, str[] argv) -> int {
int vm = vm_load("../../tests/args.fl.flbb");

str arg0 = "args";
str arg1 = "hello";
str arg2 = "world";

str[] args = allocate(8 * 4);
args[0] = arg0;
args[1] = arg1;
args[2] = arg2;
args[3] = 0;

stack_push(vm, 3);
stack_push(vm, args);

int spark = memory_read_64(vm + vm_instance_offset_spark);
invoke(vm, spark);

vm_destroy(vm);
deallocate(args);

return 0;
}
13 changes: 13 additions & 0 deletions examples/vm/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "vm_test",
"version": "1.0.0",
"main": "main.fl",
"type": "executable",
"compiler": {
"target": "bytecode",
"mode": "flbb"
},
"dependencies": [
"vm@1.0.0-x64"
]
}
Binary file removed libs/native_test/native.so
Binary file not shown.
1 change: 1 addition & 0 deletions libs/vm/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gcc native.c -o native.so -fpic --shared
40 changes: 40 additions & 0 deletions libs/vm/native.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "../../src/flvm/vm.h"

void native_stack_push(struct vm_instance* vm) {
int64_t value = (int64_t) stack_pop(vm);
struct vm_instance* vm2 = (struct vm_instance*) stack_pop(vm);
stack_push(vm2, value);
stack_push(vm, 0);
}

void native_stack_pop(struct vm_instance* vm) {
struct vm_instance* vm2 = (struct vm_instance*) stack_pop(vm);
stack_push(vm, stack_pop(vm2));
}

void native_invoke(struct vm_instance* vm) {
uint64_t location = (uint64_t) stack_pop(vm);
struct vm_instance* vm2 = (struct vm_instance*) stack_pop(vm);
invoke(vm2, location);
stack_push(vm, 0);
}

void native_vm_load(struct vm_instance* vm) {
const char* file = (const char*) stack_pop(vm);
stack_push(vm, vm_load(file));
}

void native_vm_destroy(struct vm_instance* vm) {
struct vm_instance* vm2 = (struct vm_instance*) stack_pop(vm);
vm_destroy(vm2);
stack_push(vm, 0);
}


void init() {
vm_native_register(48327, native_stack_push);
vm_native_register(48328, native_stack_pop);
vm_native_register(48329, native_invoke);
vm_native_register(48330, native_vm_load);
vm_native_register(48331, native_vm_destroy);
}
5 changes: 5 additions & 0 deletions libs/vm/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "vm",
"version": "1.0.0-x64",
"type": "module"
}
29 changes: 29 additions & 0 deletions libs/vm/vm.fl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
$define struct vm_instance_offset_code 0
$define struct vm_instance_offset_stack 8
$define struct vm_instance_offset_stack_ptr 16
$define struct vm_instance_offset_max_stack 20
$define struct vm_instance_offset_global_variables 24
$define struct vm_instance_offset_global_variable_types 32
$define struct vm_instance_offset_global_variable_size 40
$define struct vm_instance_offset_spark 48
$define struct vm_instance_size 56

function(assembly) stack_push(int vm, int value) -> void {
"48327"
}

function(assembly) stack_pop(int vm) -> int {
"48328"
}

function(assembly) invoke(int vm, int location) -> void {
"48329"
}

function(assembly) vm_load(str file) -> int {
"48330"
}

function(assembly) vm_destroy(int vm) -> void {
"48331"
}
8 changes: 2 additions & 6 deletions src/flvm/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,12 @@ void load_native_extensions(const char* folder) {
continue;
}
load_native_extensions(full_path);
free(full_path);
} else if (entry->d_type == DT_REG) {
if (strstr(entry->d_name, ".so") != NULL) {
load_so(full_path);
}
} else {
free(full_path);
}


}
free(full_path);
}

closedir(dir);
Expand Down
2 changes: 1 addition & 1 deletion src/flvm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ void vm_destroy(struct vm_instance* vm) {
}

void vm_native_register(int id, NativeFunction function) {
natives = realloc(natives, sizeof(struct vm_native) * num_natives);
natives = realloc(natives, sizeof(struct vm_native) * (num_natives + 1));
natives[num_natives].function = function;
natives[num_natives].id = id;
num_natives++;
Expand Down

0 comments on commit 263798f

Please sign in to comment.