Skip to content

Commit

Permalink
type change
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawSumma committed May 14, 2024
1 parent 7784ff7 commit 4ff9c36
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 49 deletions.
8 changes: 4 additions & 4 deletions core.mak
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ TB_OBJS = $(TB_SRCS:%.c=$(OBJ_DIR)/%.o)

OBJS = $(VM_OBJS) $(TB_OBJS) $(TCC_OBJS) $(VENDOR_OBJS)

LDFLAGS_GCCJIT_NO =
LDFLAGS_GCCJIT_NO =
LDFLAGS_GCCJIT_YES = -lgccjit

LDFLAGS_MACOS_GCCJIT_NO =
LDFLAGS_MACOS_GCCJIT_NO =
LDFLAGS_MACOS_GCCJIT_YES = -L/opt/homebrew/lib/gcc/current

LDFLAGS_WINDOWS =
Expand All @@ -75,10 +75,10 @@ LDFLAGS := $(LDFLAGS_$(OS)) $(LDFLAGS_GCCJIT_$(GCCJIT)) $(LDFLAGS)
CFLAGS_TB := -I$(CUIK_DIR)/tb/include -I$(CUIK_DIR)/c11threads -I$(CUIK_DIR)/common -DCUIK_USE_TB -DLOG_SUPPRESS -DTB_HAS_X64 $(CFLAGS_TB)
CFLAGS_VENDOR := -I$(TREE_SITTER_DIR)/lib/include -I$(TREE_SITTER_DIR)/lib/src $(CFLAGS_VENDOR)

CFLAGS_GCCJIT_NO =
CFLAGS_GCCJIT_NO =
CFLAGS_GCCJIT_YES = -DVM_USE_GCCJIT -DTB_USE_GCCJIT

CFLAGS_TB_WASM_NO =
CFLAGS_TB_WASM_NO =
CFLAGS_TB_WASM_YES = -DTB_HAS_WASM

CFLAGS_MACOS = -I/opt/homebrew/include
Expand Down
2 changes: 0 additions & 2 deletions main/minivm.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ int main(int argc, char **argv) {
config->dump_asm = true;
} else if (!strcmp(arg, "c")) {
config->dump_c = true;
} else if (!strcmp(arg, "args")) {
config->dump_args = true;
} else if (!strcmp(arg, "time")) {
config->dump_time = true;
} else {
Expand Down
4 changes: 0 additions & 4 deletions main/test/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ int main(int argc, char **argv) {
.dump_tb = false,
.dump_tb_opt = false,
.dump_x86 = false,
.dump_args = false,
.dump_time = true,
};
vm_config_t *config = &val_config;
Expand Down Expand Up @@ -219,9 +218,6 @@ int main(int argc, char **argv) {
if (!strcmp(arg, "x86")) {
config->dump_x86 = true;
}
if (!strcmp(arg, "args")) {
config->dump_args = true;
}
}
}
vm_test(
Expand Down
20 changes: 20 additions & 0 deletions test/fib/fib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

int printf(const char *fmt, ...);
int sscanf(const char *str, const char *fmt, ...);

int fib(int n) {
if (n < 2) {
return n;
} else {
return fib(n-1) + fib(n-2);
}
}

int main(int argc, char **argv) {
int n = 35;
if (argv[1]) {
sscanf(argv[1], "%i", &n);
}
printf("%i\n", fib(n));
return 0;
}
2 changes: 1 addition & 1 deletion vendor/tcc
Submodule tcc updated 1 files
+1 −1 tccpp.c
2 changes: 1 addition & 1 deletion vm/backend/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ 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 -O3 -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;
Expand Down
1 change: 1 addition & 0 deletions vm/backend/tb.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ vm_std_value_t vm_tb_run_repl(vm_config_t *config, vm_block_t *entry, vm_blocks_
tcc_set_options(state, "-nostdlib");
tcc_set_output_type(state, TCC_OUTPUT_MEMORY);
tcc_compile_string(state, buf);
tcc_add_symbol(state, "memmove", &memmove);
tcc_relocate(state);
tb_c_data_free(buf);
caller = tcc_get_symbol(state, "caller");
Expand Down
127 changes: 100 additions & 27 deletions vm/backend/tb_dyn.h
Original file line number Diff line number Diff line change
Expand Up @@ -952,27 +952,56 @@ TB_Node *vm_tb_dyn_block(vm_tb_dyn_state_t *state, vm_block_t *block) {
{
tb_inst_set_control(state->func, is_closure);

TB_PrototypeParam *call_proto_params = vm_malloc(sizeof(TB_PrototypeParam) * num_args);
// TB_PrototypeParam *call_proto_params = vm_malloc(sizeof(TB_PrototypeParam) * num_args);

for (size_t arg = 0; branch.args[arg].type != VM_ARG_NONE; arg++) {
call_proto_params[arg * 2 + 0] = (TB_PrototypeParam){VM_TB_TYPE_VALUE};
call_proto_params[arg * 2 + 1] = (TB_PrototypeParam){VM_TB_TYPE_TAG};
}
// for (size_t arg = 0; branch.args[arg].type != VM_ARG_NONE; arg++) {
// call_proto_params[arg * 2 + 0] = (TB_PrototypeParam){VM_TB_TYPE_VALUE};
// call_proto_params[arg * 2 + 1] = (TB_PrototypeParam){VM_TB_TYPE_TAG};
// }

TB_PrototypeParam call_proto_params[1] = {
{TB_TYPE_PTR},
};

TB_PrototypeParam call_proto_rets[2] = {
{VM_TB_TYPE_VALUE},
{VM_TB_TYPE_TAG},
};

TB_FunctionPrototype *call_proto = tb_prototype_create(state->mod, VM_TB_CC, num_args, call_proto_params, 2, call_proto_rets, false);
TB_FunctionPrototype *call_proto = tb_prototype_create(state->mod, VM_TB_CC, 1, call_proto_params, 2, call_proto_rets, false);

TB_Node **call_args = vm_malloc(sizeof(TB_Node *) * num_args);
TB_Node *call_arg = tb_inst_local(state->func, sizeof(vm_std_value_t) * (num_args + 1), 4);
for (size_t arg = 0; branch.args[arg].type != VM_ARG_NONE; arg++) {
vm_tb_dyn_pair_t val = vm_tb_dyn_arg(state, branch.args[arg]);
call_args[arg * 2 + 0] = val.val;
call_args[arg * 2 + 1] = val.tag;
vm_tb_dyn_pair_t pair = vm_tb_dyn_arg(state, branch.args[arg]);
size_t local = sizeof(vm_std_value_t) * arg;
tb_inst_store(
state->func,
VM_TB_TYPE_VALUE,
tb_inst_member_access(state->func, call_arg, local + offsetof(vm_std_value_t, value)),
pair.val,
4,
false
);
tb_inst_store(
state->func,
VM_TB_TYPE_TAG,
tb_inst_member_access(state->func, call_arg, local + offsetof(vm_std_value_t, tag)),
pair.tag,
4,
false
);
}

size_t local = sizeof(vm_std_value_t) * num_args;
tb_inst_store(
state->func,
VM_TB_TYPE_TAG,
tb_inst_member_access(state->func, call_arg, local + offsetof(vm_std_value_t, tag)),
tb_inst_uint(state->func, VM_TB_TYPE_TAG, VM_TAG_UNK),
4,
false
);

TB_MultiOutput out = tb_inst_call(
state->func,
call_proto,
Expand All @@ -991,8 +1020,8 @@ TB_Node *vm_tb_dyn_block(vm_tb_dyn_state_t *state, vm_block_t *block) {
),
TB_TYPE_PTR
),
num_args,
call_args
1,
&call_arg
);

vm_tb_dyn_set(
Expand Down Expand Up @@ -1036,19 +1065,15 @@ TB_Node *vm_tb_dyn_block(vm_tb_dyn_state_t *state, vm_block_t *block) {

void vm_tb_dyn_func(vm_tb_dyn_state_t *state, TB_Function *xfunc, vm_block_t *entry) {
state->func = xfunc;
size_t num_params = entry->nargs * 2;
TB_PrototypeParam *param_types = vm_malloc(sizeof(TB_PrototypeParam) * num_params);
for (size_t i = 0; i < entry->nargs; i++) {
param_types[i * 2 + 0] = (TB_PrototypeParam){VM_TB_TYPE_VALUE};
param_types[i * 2 + 1] = (TB_PrototypeParam){VM_TB_TYPE_TAG};
}
TB_PrototypeParam param_types[1] = {
{TB_TYPE_PTR},
};

size_t num_returns = 2;
TB_PrototypeParam return_types[2] = {
{VM_TB_TYPE_VALUE},
{VM_TB_TYPE_TAG},
};
TB_FunctionPrototype *proto = tb_prototype_create(state->mod, VM_TB_CC, num_params, param_types, num_returns, return_types, false);
TB_FunctionPrototype *proto = tb_prototype_create(state->mod, VM_TB_CC, 1, param_types, 2, return_types, false);
tb_function_set_prototype(state->func, -1, proto);

TB_Node *compiled_start = tb_inst_region(state->func);
Expand All @@ -1073,17 +1098,64 @@ void vm_tb_dyn_func(vm_tb_dyn_state_t *state, TB_Function *xfunc, vm_block_t *en
state->regs[i] = ptr;
}

TB_Node *head = tb_inst_param(state->func, 0);
TB_Node *nil_tag = tb_inst_uint(state->func, VM_TB_TYPE_TAG, VM_TAG_UNK);

for (size_t i = 0; i < entry->nargs; i++) {
TB_Node *on_nil = tb_inst_region(state->func);
TB_Node *on_nonnil = tb_inst_region(state->func);
TB_Node *on_end = tb_inst_region(state->func);

vm_arg_t arg = entry->args[i];

vm_tb_dyn_set(
state,
arg,
(vm_tb_dyn_pair_t){
.val = tb_inst_param(state->func, i * 2 + 0),
.tag = tb_inst_param(state->func, i * 2 + 1),
}
TB_Node *tag = tb_inst_load(
state->func,
VM_TB_TYPE_TAG,
tb_inst_member_access(state->func, head, offsetof(vm_std_value_t, tag)),
4,
false
);

tb_inst_if(state->func, tb_inst_cmp_eq(state->func, tag, nil_tag), on_nil, on_nonnil);

{
tb_inst_set_control(state->func, on_nil);

vm_tb_dyn_set(
state,
arg,
(vm_tb_dyn_pair_t){
.val = tb_inst_uint(state->func, VM_TB_TYPE_VALUE, 0),
.tag = nil_tag,
}
);

tb_inst_goto(state->func, on_end);
}

{
tb_inst_set_control(state->func, on_nonnil);

vm_tb_dyn_set(
state,
arg,
(vm_tb_dyn_pair_t){
.val = tb_inst_load(
state->func,
VM_TB_TYPE_VALUE,
tb_inst_member_access(state->func, head, offsetof(vm_std_value_t, value)),
4,
false
),
.tag = tag,
}
);

head = tb_inst_member_access(state->func, head, sizeof(vm_std_value_t));
tb_inst_goto(state->func, on_end);
}

tb_inst_set_control(state->func, on_end);
}

tb_inst_goto(state->func, compiled_start);
Expand Down Expand Up @@ -1298,6 +1370,7 @@ vm_tb_dyn_func_t *vm_tb_dyn_comp(vm_tb_dyn_state_t *state, vm_block_t *entry) {
tcc_set_options(state, "-nostdlib");
tcc_set_output_type(state, TCC_OUTPUT_MEMORY);
tcc_compile_string(state, buf);
tcc_add_symbol(state, "memmove", &memmove);
tcc_relocate(state);
tb_c_data_free(buf);
ret = tcc_get_symbol(state, entry_buf);
Expand Down
8 changes: 1 addition & 7 deletions vm/backend/tb_ver.h
Original file line number Diff line number Diff line change
Expand Up @@ -1785,13 +1785,6 @@ static void *vm_tb_ver_rfunc_comp(vm_rblock_t *rblock) {
vm_type_to_tb_type(rblock->regs->tags[block->args[i].reg])
)
);
if (state->config->dump_args) {
vm_tb_ver_func_print_value(
state,
rblock->regs->tags[block->args[i].reg],
tb_inst_param(state->fun, i)
);
}
}
TB_Node *main = tb_inst_region(state->fun);
tb_inst_set_region_name(state->fun, main, -1, "entry");
Expand Down Expand Up @@ -1863,6 +1856,7 @@ static void *vm_tb_ver_rfunc_comp(vm_rblock_t *rblock) {
tcc_set_options(state, "-nostdlib");
tcc_set_output_type(state, TCC_OUTPUT_MEMORY);
tcc_compile_string(state, buf);
tcc_add_symbol(state, "memmove", &memmove);
tcc_relocate(state);
tb_c_data_free(buf);
void *code = tcc_get_symbol(state, name);
Expand Down
1 change: 0 additions & 1 deletion vm/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ struct vm_config_t {
bool dump_tb_opt : 1;
bool dump_asm : 1;
bool dump_c : 1;
bool dump_args : 1;
bool dump_time : 1;

bool is_repl : 1;
Expand Down
2 changes: 0 additions & 2 deletions vm/lua/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ void vm_lang_lua_repl_table_get_config(vm_table_t *table, vm_config_t *config) {
config->dump_tb = vm_lang_lua_repl_table_get_bool(dump, "tb");
config->dump_tb_opt = vm_lang_lua_repl_table_get_bool(dump, "tb_opt");
config->dump_asm = vm_lang_lua_repl_table_get_bool(dump, "asm");
config->dump_args = vm_lang_lua_repl_table_get_bool(dump, "args");
config->dump_time = vm_lang_lua_repl_table_get_bool(dump, "time");
}

Expand All @@ -58,7 +57,6 @@ void vm_lang_lua_repl_table_set_config(vm_table_t *table, vm_config_t *config) {
VM_TABLE_SET(dump, str, "tb", b, config->dump_tb);
VM_TABLE_SET(dump, str, "tb_opt", b, config->dump_tb_opt);
VM_TABLE_SET(dump, str, "asm", b, config->dump_asm);
VM_TABLE_SET(dump, str, "args", b, config->dump_args);
VM_TABLE_SET(dump, str, "time", b, config->dump_time);
}

Expand Down

0 comments on commit 4ff9c36

Please sign in to comment.