Skip to content

Commit

Permalink
snapshot code
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawSumma committed May 16, 2024
1 parent 3ec4e49 commit 4560870
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 80 deletions.
11 changes: 9 additions & 2 deletions core.mak
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ GC_OBJS += $(GC_SRCS:%.c=$(OBJ_DIR)/%.o)
VENDOR_SRCS += $(ISOCLINE_SRCS) $(XXH_SRCS) $(TREES_SRCS) $(GC_OBJS)
VENDOR_OBJS = $(VENDOR_SRCS:%.c=$(OBJ_DIR)/%.o)

VM_SRCS += vm/std/io.c vm/std/std.c vm/save/write.c vm/save/read.c vm/ir/ir.c vm/lib.c vm/ir/type.c vm/ast/build.c vm/ast/ast.c vm/ast/comp.c vm/ast/print.c vm/obj.c vm/backend/tb.c vm/backend/exec.c vm/ir/check.c vm/ir/rblock.c vm/lua/parser/parser.c vm/lua/parser/scan.c vm/lua/ast.c vm/lua/repl.c $(STD_SRCS)
VM_AST_SRCS += vm/ast/build.c vm/ast/ast.c vm/ast/comp.c vm/ast/print.c
VM_BACKEND_SRCS += vm/backend/tb.c vm/backend/exec.c
VM_BASE_SRCS += vm/lib.c vm/obj.c
VM_DATA_SRCS += vm/save/io.c vm/save/write.c vm/save/read.c
VM_IR_SRCS += vm/ir/ir.c vm/ir/type.c vm/ir/rblock.c vm/ir/check.c
VM_LUA_SRCS += vm/lua/parser/parser.c vm/lua/parser/scan.c vm/lua/repl.c vm/lua/ast.c
VM_STD_SRCS += vm/std/io.c vm/std/std.c
VM_SRCS += $(VM_AST_SRCS) $(VM_BASE_SRCS) $(VM_BACKEND_SRCS) $(VM_DATA_SRCS) $(VM_IR_SRCS) $(VM_LUA_SRCS) $(VM_STD_SRCS)
VM_OBJS = $(VM_SRCS:%.c=$(OBJ_DIR)/%.o)

TCC_SRCS ?= $(TCC_DIR)/libtcc.c
Expand Down Expand Up @@ -83,7 +90,7 @@ CFLAGS_TB_WASM_YES = -DTB_HAS_WASM
CFLAGS_MACOS = -I/opt/homebrew/include
CFLAGS := $(CFLAGS_$(OS)) $(CFLAGS_GCCJIT_$(GCCJIT)) $(CFLAGS_TB_WASM_$(TB_WASM)) $(CFLAGS)

LDFLAGS := -no-pie $(FLAGS) $(LDFLAGS)
LDFLAGS := $(FLAGS) $(LDFLAGS)
CFLAGS := $(FLAGS) $(CFLAGS)

MKDIR = @mkdir -p
Expand Down
5 changes: 1 addition & 4 deletions main/minivm.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,7 @@ int main(int argc, char **argv) {
}

vm_ast_node_t node = vm_lang_lua_parse(config, src);

if (name != NULL) {
vm_free(src);
}
vm_blocks_add_src(blocks, src);

if (config->dump_ast) {
vm_io_buffer_t buf = {0};
Expand Down
8 changes: 6 additions & 2 deletions vm/backend/tb.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,17 @@ vm_std_value_t vm_tb_run_repl(vm_config_t *config, vm_block_t *entry, vm_blocks_

vm_tb_dyn_func_t *fn = (vm_tb_dyn_func_t *)vm_tb_dyn_comp(state, entry);

vm_std_value_t values[1] = {
[0].tag = VM_TAG_UNK,
};

#if defined(_WIN32)
caller(&value, fn);
#else
value = fn();
value = fn(&values[0]);
#endif

blocks->len = 0;
// blocks->len = 0;

vm_free(state);
}
Expand Down
2 changes: 1 addition & 1 deletion vm/backend/tb_dyn.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct vm_tb_dyn_state_t;
typedef struct vm_tb_dyn_pair_t vm_tb_dyn_pair_t;
typedef struct vm_tb_dyn_state_t vm_tb_dyn_state_t;

typedef vm_std_value_t VM_CDECL vm_tb_dyn_func_t(void);
typedef vm_std_value_t VM_CDECL vm_tb_dyn_func_t(vm_std_value_t *values);

struct vm_tb_dyn_pair_t {
TB_Node *val;
Expand Down
11 changes: 11 additions & 0 deletions vm/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#define VM_USE_DUMP 1
#define VM_FORMAT_FLOAT "%.14g"

struct vm_externs_t;
typedef struct vm_externs_t vm_externs_t;

struct vm_config_t;
typedef struct vm_config_t vm_config_t;

Expand Down Expand Up @@ -42,7 +45,15 @@ enum {
VM_USE_VERSION_COUNT_FINE,
};

struct vm_externs_t {
size_t id;
void *value;
vm_externs_t *last;
};

struct vm_config_t {
vm_externs_t *externs;

const char *cflags;

unsigned int target : 4;
Expand Down
15 changes: 15 additions & 0 deletions vm/ir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct vm_instr_t;
struct vm_block_t;
struct vm_rblock_t;
struct vm_cache_t;
struct vm_blocks_srcs_t;
struct vm_blocks_t;

typedef struct vm_rblock_t vm_rblock_t;
Expand All @@ -16,6 +17,7 @@ typedef struct vm_arg_t vm_arg_t;
typedef struct vm_branch_t vm_branch_t;
typedef struct vm_instr_t vm_instr_t;
typedef struct vm_block_t vm_block_t;
typedef struct vm_blocks_srcs_t vm_blocks_srcs_t;
typedef struct vm_blocks_t vm_blocks_t;

#include "../lib.h"
Expand Down Expand Up @@ -157,11 +159,17 @@ struct vm_block_t {
bool checked : 1;
};

struct vm_blocks_srcs_t {
const char *src;
vm_blocks_srcs_t *last;
};

struct vm_blocks_t {
size_t len;
vm_block_t **blocks;
size_t alloc;
vm_block_t *entry;
vm_blocks_srcs_t *srcs;
};

void vm_block_realloc(vm_block_t *block, vm_instr_t instr);
Expand All @@ -181,4 +189,11 @@ void vm_free_block(vm_block_t *block);

#define vm_arg_nil() ((vm_arg_t){.type = (VM_ARG_LIT), .lit.tag = (VM_TAG_NIL)})

static inline void vm_blocks_add_src(vm_blocks_t *blocks, const char *src) {
vm_blocks_srcs_t *next = vm_malloc(sizeof(vm_blocks_srcs_t));
next->last = blocks->srcs;
next->src = src;
blocks->srcs = next;
}

#endif
14 changes: 14 additions & 0 deletions vm/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,18 @@ static char *vm_strdup(const char *str) {
#define VM_CDECL
#endif

static inline void vm_config_add_extern(vm_config_t *config, void *value) {
vm_externs_t *last = config->externs;
for (vm_externs_t *cur = last; cur; cur = cur->last) {
if (cur->value == value) {
return;
}
}
vm_externs_t *next = vm_malloc(sizeof(vm_externs_t));
next->id = last == NULL ? 0 : last->id + 1;
next->value = value;
next->last = last;
config->externs = next;
}

#endif
28 changes: 22 additions & 6 deletions vm/lua/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#include "../backend/tb.h"
#include "../ir/ir.h"
#include "../std/io.h"
#include "../save/value.h"

#include "../../vendor/tree-sitter/lib/include/tree_sitter/api.h"

#if defined(EMSCRIPTEN)
#include <emscripten.h>
#endif

#include "../save/save.h"

const TSLanguage *tree_sitter_lua(void);
vm_ast_node_t vm_lang_lua_parse(vm_config_t *config, const char *str);
Expand Down Expand Up @@ -241,9 +241,13 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)
if (f != NULL) {
vm_save_t save = vm_save_load(f);
fclose(f);
vm_std_value_t val = vm_load_value(save);
if (val.tag == VM_TAG_TAB) {
std = val.value.table;
vm_save_loaded_t ld = vm_load_value(config, save);
if (ld.blocks != NULL) {
*blocks = *ld.blocks;
*std = *ld.env.value.table;
vm_io_buffer_t *buf = vm_io_buffer_new();
vm_io_format_blocks(buf, blocks);
printf("%s", buf->buf);
}
}
}
Expand All @@ -254,14 +258,25 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)
// setvbuf(stderr, NULL, _IONBF, 0);
// #endif

const char *arr[] = {
// "f = function() return 2 end",
"f()",
NULL,
};
const char **inputs = &arr[0];

while (true) {
#if 0
const char *input = *inputs++;
#else
char *input = ic_readline_ex(
"lua",
vm_lang_lua_repl_completer,
&complete_state,
vm_lang_lua_repl_highlight,
&highlight_state
);
#endif
if (input == NULL) {
break;
}
Expand All @@ -274,14 +289,15 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)
}

vm_ast_node_t node = vm_lang_lua_parse(config, input);
free(input);
vm_blocks_add_src(blocks, input);

if (config->dump_ast) {
vm_io_buffer_t buf = {0};
vm_ast_print_node(&buf, 0, "", node);
printf("\n--- ast ---\n%.*s", (int)buf.len, buf.buf);
}


vm_ast_comp_more(node, blocks);
vm_ast_free_node(node);

Expand Down Expand Up @@ -311,7 +327,7 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)
printf("took: %.3fms\n", diff);
}

vm_save_t save = vm_save_value((vm_std_value_t) {.tag = VM_TAG_TAB, .value.table = std});
vm_save_t save = vm_save_value(config, blocks, (vm_std_value_t) {.tag = VM_TAG_TAB, .value.table = std});
FILE *f = fopen("out.bin", "wb");
if (f != NULL) {
fwrite(save.buf, 1, save.len, f);
Expand Down
3 changes: 3 additions & 0 deletions vm/obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ bool vm_value_eq(vm_std_value_t lhs, vm_std_value_t rhs) {
case VM_TAG_STR: {
return vm_type_eq(rhs.tag, VM_TAG_STR) && !strcmp(lhs.value.str, rhs.value.str);
}
case VM_TAG_FUN: {
return rhs.tag == VM_TAG_FUN && lhs.value.i32 == rhs.value.i32;
}
default: {
return vm_type_eq(lhs.tag, rhs.tag) && lhs.value.all == rhs.value.all;
}
Expand Down
24 changes: 24 additions & 0 deletions vm/save/io.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

#include "io.h"

vm_save_t vm_save_load(FILE *in) {
size_t nalloc = 512;
uint8_t *ops = vm_malloc(sizeof(char) * nalloc);
size_t nops = 0;
size_t size;
while (true) {
if (nops + 256 >= nalloc) {
nalloc = (nops + 256) * 2;
ops = vm_realloc(ops, sizeof(char) * nalloc);
}
size = fread(&ops[nops], 1, 256, in);
nops += size;
if (size < 256) {
break;
}
}
return (vm_save_t) {
.len = nops,
.buf = ops,
};
}
7 changes: 2 additions & 5 deletions vm/save/save.h → vm/save/io.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#if !defined(VM_HEADER_SAVE_SAVE)
#define VM_HEADER_SAVE_SAVE
#if !defined(VM_HEADER_SAVE_IO)
#define VM_HEADER_SAVE_IO

#include "../lib.h"
#include "../std/io.h"

struct vm_save_t;
typedef struct vm_save_t vm_save_t;
Expand All @@ -13,7 +12,5 @@ struct vm_save_t {
};

vm_save_t vm_save_load(FILE *file);
vm_std_value_t vm_load_value(vm_save_t arg);
vm_save_t vm_save_value(vm_std_value_t arg);

#endif
Loading

0 comments on commit 4560870

Please sign in to comment.