Skip to content

Commit

Permalink
dynamic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawSumma committed May 19, 2024
1 parent a934455 commit 8a3c99c
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 73 deletions.
2 changes: 1 addition & 1 deletion core.mak
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ CFLAGS_TB := -I$(CUIK_DIR)/tb/include -I$(CUIK_DIR)/c11threads -I$(CUIK_DIR)/com
CFLAGS_VENDOR := -I$(TREE_SITTER_DIR)/lib/include -I$(TREE_SITTER_DIR)/lib/src $(CFLAGS_VENDOR)

CFLAGS_GCCJIT_NO =
CFLAGS_GCCJIT_YES = -DVM_USE_GCCJIT -DTB_USE_GCCJIT
CFLAGS_GCCJIT_YES = -DTB_USE_GCCJIT

CFLAGS_TB_WASM_NO =
CFLAGS_TB_WASM_YES = -DTB_HAS_WASM
Expand Down
4 changes: 0 additions & 4 deletions main/minivm.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ int main(int argc, char **argv) {
#if defined(VM_USE_TCC)
} else if (!strcmp(arg, "tb-tcc")) {
config->target = VM_TARGET_TB_TCC;
#endif
#if defined(VM_USE_GCCJIT)
} else if (!strcmp(arg, "tb-gccjit")) {
config->target = VM_TARGET_TB_GCCJIT;
#endif
} else if (!strcmp(arg, "tb-cc")) {
config->target = VM_TARGET_TB_CC;
Expand Down
22 changes: 22 additions & 0 deletions test/loop/squares.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

local function sqrt(n)
local f = 1
local i = 3
while true do
f = f + i
if f > n then
return (i - 1) / 2
end
i = i + 2
end
end

local function check(i)
print('sqrt(' .. tostring(i) .. ') = ' .. tostring(sqrt(i)))
end

for i=2, 10 do
assert(sqrt(i*i-1) == i-1)
assert(sqrt(i*i) == i)
assert(sqrt(i*i+1) == i)
end
34 changes: 22 additions & 12 deletions vm/backend/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

#include "../../vendor/xxhash/xxhash.h"

void *vm_cache_comp(const char *comp, const char *flags, const char *src, const char *entry) {
void *vm_cache_dlsym(void *handle, const char *name) {
return GetProcAddress(handle, entry);
}

void *vm_cache_comp(const char *comp, const char *flags, const char *src) {
if (flags == NULL) {
flags = "";
}
Expand All @@ -37,9 +41,8 @@ void *vm_cache_comp(const char *comp, const char *flags, const char *src, const
remove(c_file);
}
void *handle = LoadLibrary(so_file);
void *sym = GetProcAddress(handle, entry);
remove(so_file);
return sym;
return handle;
}
#else
#include <dlfcn.h>
Expand All @@ -54,7 +57,11 @@ EM_JS(void, vm_compile_c_to_wasm, (int n), {
Module._vm_compile_c_to_wasm(n);
});

void *vm_cache_comp(const char *comp, const char *flags, const char *src, const char *entry) {
void *vm_cache_dlsym(void *handle, const char *name) {
return dlsym(handle, name);
}

void *vm_cache_comp(const char *comp, const char *flags, const char *src) {
static int n = 0;
n += 1;
struct stat st = {0};
Expand All @@ -69,16 +76,20 @@ void *vm_cache_comp(const char *comp, const char *flags, const char *src, const
fclose(out);
vm_compile_c_to_wasm(n);
void *handle = dlopen(so_file, RTLD_LAZY);
void *sym = dlsym(handle, entry);
// void *sym = dlsym(handle, entry);
remove(c_file);
remove(so_file);
return sym;
return handle;
}
#else

#include "../../vendor/xxhash/xxhash.h"

void *vm_cache_comp(const char *comp, const char *flags, const char *src, const char *entry) {
void *vm_cache_dlsym(void *handle, const char *name) {
return dlsym(handle, name);
}

void *vm_cache_comp(const char *comp, const char *flags, const char *src) {
if (flags == NULL) {
flags = "";
}
Expand All @@ -97,18 +108,17 @@ void *vm_cache_comp(const char *comp, const char *flags, const char *src, const
fwrite(src, len, 1, out);
fclose(out);
vm_io_buffer_t *cmd_buf = vm_io_buffer_new();
vm_io_buffer_format(cmd_buf, "%s -shared -O2 -foptimize-sibling-calls -fPIC %s %s -o %s -w -pipe", comp, flags, c_file, so_file);
// vm_io_buffer_format(cmd_buf, "%s -shared -g3 -fsanitize=memory -foptimize-sibling-calls -fPIC %s %s -o %s -w -pipe", comp, flags, c_file, so_file);
// vm_io_buffer_format(cmd_buf, "%s -shared -O2 -foptimize-sibling-calls -fPIC %s %s -o %s -w -pipe", comp, flags, c_file, so_file);
vm_io_buffer_format(cmd_buf, "%s -shared -g3 -foptimize-sibling-calls -fPIC %s %s -o %s -w -pipe", comp, flags, c_file, so_file);
int res = system(cmd_buf->buf);
if (res) {
return NULL;
}
remove(c_file);
// remove(c_file);
}
void *handle = dlopen(so_file, RTLD_LAZY);
void *sym = dlsym(handle, entry);
remove(so_file);
return sym;
return handle;
}
#endif

Expand Down
3 changes: 2 additions & 1 deletion vm/backend/exec.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#if !defined(VM_HEADER_BE_EXEC)
#define VM_HEADER_BE_EXEC

void *vm_cache_comp(const char *comp, const char *flags, const char *src, const char *entry);
void *vm_cache_dlsym(void *handle, const char *name);
void *vm_cache_comp(const char *comp, const char *flags, const char *src);

#endif
75 changes: 42 additions & 33 deletions vm/backend/tb_dyn.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ struct vm_tb_dyn_state_t {
size_t nlocals;
TB_Node *locals;
vm_tb_dyn_pair_t *regs;

void **codes;
};

TB_Node *vm_tb_dyn_ptr(vm_tb_dyn_state_t *state, const void *ptr) {
Expand Down Expand Up @@ -152,7 +154,7 @@ vm_tb_dyn_pair_t vm_tb_dyn_arg(vm_tb_dyn_state_t *state, vm_arg_t arg) {
return vm_tb_dyn_pair_of(
state,
VM_TAG_FUN,
tb_inst_get_symbol_address(state->func, (TB_Symbol *)state->funcs[arg.func->id])
tb_inst_uint(state->func, TB_TYPE_I32, arg.func->id)
);
}
default: {
Expand Down Expand Up @@ -937,20 +939,27 @@ TB_Node *vm_tb_dyn_block(vm_tb_dyn_state_t *state, vm_block_t *block) {
TB_MultiOutput out = tb_inst_call(
state->func,
call_proto,
tb_inst_bitcast(
tb_inst_load(
state->func,
tb_inst_load(
TB_TYPE_PTR,
tb_inst_array_access(
state->func,
VM_TB_TYPE_VALUE,
tb_inst_member_access(
vm_tb_dyn_ptr(state, state->codes),
tb_inst_load(
state->func,
run.val,
offsetof(vm_std_value_t, value)
TB_TYPE_I32,
tb_inst_member_access(
state->func,
run.val,
offsetof(vm_std_value_t, value)
),
4,
false
),
4,
false
sizeof(void *)
),
TB_TYPE_PTR
4,
false
),
1,
&call_arg
Expand Down Expand Up @@ -1139,6 +1148,8 @@ vm_tb_dyn_func_t *vm_tb_dyn_comp(vm_tb_dyn_state_t *state, vm_block_t *entry) {
state->funcs = vm_malloc(sizeof(TB_Function *) * max);
memset(state->funcs, 0, sizeof(TB_Function *) * max);

state->codes = vm_malloc(sizeof(void *) * max);

state->compiled = vm_malloc(sizeof(TB_Node *) * max);
memset(state->compiled, 0, sizeof(TB_Node *) * max);

Expand All @@ -1149,7 +1160,6 @@ vm_tb_dyn_func_t *vm_tb_dyn_comp(vm_tb_dyn_state_t *state, vm_block_t *entry) {
tb_symbol_bind_ptr(state->vm_table_set_pair, (void *)&vm_table_set_pair);
tb_symbol_bind_ptr(state->vm_table_get_pair, (void *)&vm_table_get_pair);


char entry_buf[64];
TB_Function *entry_func = NULL;

Expand Down Expand Up @@ -1243,28 +1253,12 @@ vm_tb_dyn_func_t *vm_tb_dyn_comp(vm_tb_dyn_state_t *state, vm_block_t *entry) {
}
}
const char *buf = tb_c_buf_to_data(cbuf);
void *code = vm_cache_comp("emcc", state->config->cflags, buf, entry_buf);
void *handle = vm_cache_comp("emcc", state->config->cflags, buf);
tb_c_data_free(buf);
ret = code;
ret = vm_cache_dlsym(handle, entry_buf);
break;
}
#else
#if defined(VM_USE_GCCJIT)
case VM_TARGET_TB_GCCJIT: {
TB_GCCJIT_Module *mod = tb_gcc_module_new(state->mod);
for (size_t block_num = 0; block_num < state->blocks->len; block_num++) {
TB_Function *func = state->funcs[block_num];
if (func != NULL && func != entry_func) {
TB_Worklist *worklist = tb_worklist_alloc();
tb_gcc_module_function(mod, func, worklist, tmp_arena);
}
}
TB_Worklist *worklist = tb_worklist_alloc();
TB_GCCJIT_Function *func = tb_gcc_module_function(mod, entry_func, worklist, tmp_arena);
ret = tb_gcc_function_ptr(func);
break;
}
#endif
#if defined(VM_USE_TCC)
case VM_TARGET_TB_TCC: {
TB_CBuffer *cbuf = tb_c_buf_new();
Expand Down Expand Up @@ -1319,9 +1313,18 @@ vm_tb_dyn_func_t *vm_tb_dyn_comp(vm_tb_dyn_state_t *state, vm_block_t *entry) {
default:
break;
}
void *code = vm_cache_comp(cc_name, state->config->cflags, buf, entry_buf);
void *handle = vm_cache_comp(cc_name, state->config->cflags, buf);
tb_c_data_free(buf);
ret = code;

for (size_t block_num = 0; block_num < state->blocks->len; block_num++) {
vm_block_t *block = state->blocks->blocks[block_num];
TB_Function *func = state->funcs[block_num];
if (block->isfunc) {
state->codes[block->id] = vm_cache_dlsym(handle, ((TB_Symbol *)func)->name);
}
}

ret = vm_cache_dlsym(handle, entry_buf);
break;
}

Expand All @@ -1345,9 +1348,15 @@ vm_tb_dyn_func_t *vm_tb_dyn_comp(vm_tb_dyn_state_t *state, vm_block_t *entry) {
}
}
}
void *code = tb_jit_place_function(jit, entry_func);
for (size_t block_num = 0; block_num < state->blocks->len; block_num++) {
vm_block_t *block = state->blocks->blocks[block_num];
TB_Function *func = state->funcs[block_num];
if (block->isfunc) {
state->codes[block->id] = tb_jit_place_function(jit, func);
}
}

ret = code;
ret = tb_jit_place_function(jit, entry_func);
break;
}
#endif
Expand Down
12 changes: 2 additions & 10 deletions vm/backend/tb_ver.h
Original file line number Diff line number Diff line change
Expand Up @@ -1860,14 +1860,6 @@ static void *vm_tb_ver_rfunc_comp(vm_rblock_t *rblock) {
ret = code;
break;
}
#endif
#if defined(VM_USE_GCCJIT)
case VM_TARGET_TB_GCCJIT: {
TB_GCCJIT_Module *mod = tb_gcc_module_new(state->module);
TB_GCCJIT_Function *func = tb_gcc_module_function(mod, f, worklist, state->tmp_arena);
ret = tb_gcc_function_ptr(func);
break;
}
#endif
case VM_TARGET_TB_CC:
case VM_TARGET_TB_GCC:
Expand All @@ -1890,9 +1882,9 @@ static void *vm_tb_ver_rfunc_comp(vm_rblock_t *rblock) {
default:
break;
}
void *code = vm_cache_comp(cc_name, state->config->cflags, buf, name);
void *handle = vm_cache_comp(cc_name, state->config->cflags, buf);
tb_c_data_free(buf);
ret = code;
ret = vm_cache_dlsym(handle, name);
break;
}
case VM_TARGET_TB: {
Expand Down
3 changes: 0 additions & 3 deletions vm/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ enum {
#else
#if defined(VM_USE_TCC)
VM_TARGET_TB_TCC,
#endif
#if defined(VM_USE_GCCJIT)
VM_TARGET_TB_GCCJIT,
#endif
VM_TARGET_TB_CC,
VM_TARGET_TB_GCC,
Expand Down
5 changes: 1 addition & 4 deletions vm/std/std.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,7 @@ vm_table_t *vm_std_new(vm_config_t *config) {
vm_table_t *vm = vm_table_new();
VM_TABLE_SET(std, str, "vm", table, vm);
VM_TABLE_SET(vm, str, "print", ffi, VM_STD_REF(config, vm_std_vm_print));
{
vm_table_t *vm_ver = vm_table_new();
VM_TABLE_SET(vm, str, "version", table, vm_ver);
}
VM_TABLE_SET(vm, str, "version", str, "0.0.3");
}

{
Expand Down
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"build:watch": "concurrently -n make,vite 'npm run make:watch' 'npm run vite:watch'",

"make:watch": "npx nodemon --watch ../vm --watch ../vendor --watch ../main --ext '*' --exec 'npm run make'",
"make": "make -C .. -f web.mak -Bj CFLAGS='-DNDEBUG -fPIC' OPT='-g3'",
"make": "make -C .. -f web.mak -Bj CFLAGS='-DNDEBUG -fPIC' OPT='-Os -flto'",

"vite:watch": "npx vite build --watch",
"vite": "npx vite build"
Expand Down
2 changes: 1 addition & 1 deletion web/src/app/NewTerm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
let div;
const term = new Terminal(new FitAddon());
const term = new Terminal();
const fit = new FitAddon();
Expand Down
9 changes: 6 additions & 3 deletions web/src/lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ export const repl = ({putchar}) => {
case 'get-buffer': {
let sync = null;
try {
sync = Uint8Array.from(JSON.parse(localStorage.getItem('minivm.state')));
if (localStorage.getItem('minivm.clear') != null) {
localStorage.removeItem('minivm.clear');
} else if (localStorage.getItem('minivm.state') != null) {
sync = Uint8Array.from(JSON.parse(localStorage.getItem('minivm.state')));
}
} catch (e) {
console.error(e);
}
Expand All @@ -123,8 +127,7 @@ export const repl = ({putchar}) => {
break;
}
case 'sync': {
const buf = data.buf;
localStorage.setItem('minivm.state', JSON.stringify(Array.from(buf)));
localStorage.setItem('minivm.state', JSON.stringify(Array.from(data.buf)));
break;
}
}
Expand Down

0 comments on commit 8a3c99c

Please sign in to comment.