Skip to content

Commit

Permalink
more renames, once per function code check
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawSumma committed Jul 24, 2024
1 parent 76dccc9 commit 230658e
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 150 deletions.
19 changes: 15 additions & 4 deletions vm/backend/backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,18 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) {

#define vm_run_repl_out(value) (regs[vm_run_repl_read(vm_interp_reg_t)] = (value))

void vm_interp_renumber_blocks(vm_t *vm, void **ptrs, vm_ir_block_t *block) {
if (block->code == NULL) {
block->code = vm_interp_renumber_block(vm, &ptrs[0], block);
if (block->branch.targets[0] != NULL) {
vm_interp_renumber_blocks(vm, ptrs, block->branch.targets[0]);
}
if (block->branch.targets[1] != NULL) {
vm_interp_renumber_blocks(vm, ptrs, block->branch.targets[1]);
}
}
}

vm_obj_t vm_run_repl_inner(vm_t *vm, vm_ir_block_t *block) {
vm_obj_t *regs = vm->regs;
static void *ptrs[VM_MAX_OP] = {
Expand Down Expand Up @@ -736,6 +748,9 @@ vm_obj_t vm_run_repl_inner(vm_t *vm, vm_ir_block_t *block) {
[VM_OP_GET] = &&VM_OP_GET,
[VM_OP_CALL] = &&VM_OP_CALL,
};

vm_interp_renumber_blocks(vm, ptrs, block);

vm_obj_t *next_regs = &regs[VM_NREGS];
#if VM_DEBUG_BACKEND_BLOCKS
{
Expand All @@ -759,10 +774,6 @@ new_block:;
new_block_no_print:;

uint8_t *code = block->code;
if (block->code == NULL) {
code = vm_interp_renumber_block(vm, &ptrs[0], block);
block->code = code;
}

goto *vm_run_repl_read(void *);

Expand Down
18 changes: 8 additions & 10 deletions vm/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct vm_gc_objs_t {
struct vm_gc_table_cache_t {
size_t alloc;
size_t len;
vm_table_t **tables;
vm_obj_table_t **tables;
};

struct vm_gc_t {
Expand Down Expand Up @@ -59,7 +59,7 @@ static void vm_gc_mark_obj(vm_obj_t obj) {
vm_io_buffer_t *buffer = vm_obj_get_string(obj);
buffer->mark = true;
} else if (vm_obj_is_closure(obj)) {
vm_closure_t *closure = vm_obj_get_closure(obj);
vm_obj_closure_t *closure = vm_obj_get_closure(obj);
if (!closure->mark) {
closure->mark = true;
for (size_t i = 0; i < closure->len; i++) {
Expand All @@ -68,7 +68,7 @@ static void vm_gc_mark_obj(vm_obj_t obj) {
}
vm_gc_mark_block(closure->block);
} else if (vm_obj_is_table(obj)) {
vm_table_t *table = vm_obj_get_table(obj);
vm_obj_table_t *table = vm_obj_get_table(obj);
if (!table->mark) {
table->mark = true;
for (size_t i = 0; i < (1 << table->alloc); i++) {
Expand Down Expand Up @@ -106,15 +106,15 @@ void vm_gc_sweep(vm_t *vm) {
gc->objs.objs[write++] = obj;
}
} else if (vm_obj_is_closure(obj)) {
vm_closure_t *closure = vm_obj_get_closure(obj);
vm_obj_closure_t *closure = vm_obj_get_closure(obj);
if (!closure->mark) {
vm_free(closure);
} else {
closure->mark = false;
gc->objs.objs[write++] = obj;
}
} else if (vm_obj_is_table(obj)) {
vm_table_t *table = vm_obj_get_table(obj);
vm_obj_table_t *table = vm_obj_get_table(obj);
if (!table->mark) {
if (!table->pairs_auto) {
vm_free(table->pairs);
Expand Down Expand Up @@ -148,11 +148,9 @@ void vm_gc_sweep(vm_t *vm) {
}
}

void vm_table_init_size(vm_table_t *ret, size_t pow2);

vm_table_t *vm_table_new_size(vm_t *vm, size_t pow2) {
vm_obj_table_t *vm_table_new_size(vm_t *vm, size_t pow2) {
vm_gc_t *gc = vm->gc;
vm_table_t *ret = vm_malloc(sizeof(vm_table_t) + sizeof(vm_table_pair_t) * (1 << pow2));
vm_obj_table_t *ret = vm_malloc(sizeof(vm_obj_table_t) + sizeof(vm_table_pair_t) * (1 << pow2));
ret->pairs = (vm_table_pair_t *)&ret[1];
memset(ret->pairs, VM_EMPTY_BYTE, sizeof(vm_table_pair_t) * (1 << pow2));
ret->alloc = pow2;
Expand All @@ -164,7 +162,7 @@ vm_table_t *vm_table_new_size(vm_t *vm, size_t pow2) {
return ret;
}

vm_table_t *vm_table_new(vm_t *vm) {
vm_obj_table_t *vm_table_new(vm_t *vm) {
return vm_table_new_size(vm, 2);
}

Expand Down
2 changes: 1 addition & 1 deletion vm/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ void vm_gc_deinit(vm_t *vm);

void vm_gc_add(vm_t *vm, vm_obj_t obj);

vm_table_t *vm_table_new_size(vm_t *vm, size_t pow2);
vm_obj_table_t *vm_table_new_size(vm_t *vm, size_t pow2);

#endif
4 changes: 2 additions & 2 deletions vm/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_obj_
vm_io_buffer_format(out, "<code: %p>\n", vm_obj_get_block(value));
}
if (vm_obj_is_table(value)) {
vm_table_t *tab = vm_obj_get_table(value);
vm_obj_table_t *tab = vm_obj_get_table(value);
if (tab == NULL) {
vm_io_indent(out, indent, prefix);
vm_io_buffer_format(out, "table(NULL)\n");
Expand Down Expand Up @@ -201,7 +201,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_obj_
}
}

void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value) {
void vm_obj_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value) {
if (vm_obj_is_nil(value)) {
vm_io_buffer_format(buf, "nil");
}
Expand Down
2 changes: 1 addition & 1 deletion vm/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void vm_io_buffer_vformat(vm_io_buffer_t *buf, const char *fmt, va_list ap);
void vm_io_buffer_format(vm_io_buffer_t *buf, const char *fmt, ...);
char *vm_io_vformat(const char *fmt, va_list ap);
char *vm_io_format(const char *fmt, ...);
void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value);
void vm_obj_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value);
vm_io_buffer_t *vm_io_buffer_from_str(const char *str);

#endif
2 changes: 1 addition & 1 deletion vm/lua/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void vm_lang_lua_repl_completer(ic_completion_env_t *cenv, const char *prefix) {
}
head += 1;
const char *last_word = &prefix[head];
vm_table_t *std = vm_obj_get_table(vm->std);
vm_obj_table_t *std = vm_obj_get_table(vm->std);
with_new_std:;
for (size_t i = 0; i < ((size_t)1 << std->alloc); i++) {
vm_table_pair_t *pair = &std->pairs[i];
Expand Down
34 changes: 15 additions & 19 deletions vm/obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bool vm_obj_eq(vm_obj_t v1, vm_obj_t v2) {
}
}

size_t vm_value_hash(vm_obj_t value) {
size_t vm_obj_hash(vm_obj_t value) {
if (vm_obj_is_nil(value)) {
return 0;
}
Expand Down Expand Up @@ -59,10 +59,10 @@ size_t vm_value_hash(vm_obj_t value) {
// return 0;
}

vm_table_pair_t *vm_table_lookup(vm_table_t *table, vm_obj_t key) {
vm_table_pair_t *vm_table_lookup(vm_obj_table_t *table, vm_obj_t key) {
size_t len = 1 << table->alloc;
size_t and = len - 1;
size_t stop = vm_value_hash(key) & and;
size_t stop = vm_obj_hash(key) & and;
size_t next = stop;
do {
vm_table_pair_t *pair = &table->pairs[next];
Expand All @@ -79,23 +79,13 @@ vm_table_pair_t *vm_table_lookup(vm_table_t *table, vm_obj_t key) {
return NULL;
}

void vm_table_init_size(vm_table_t *ret, size_t pow2) {
ret->pairs = vm_malloc(sizeof(vm_table_pair_t) * (1 << pow2));
memset(ret->pairs, VM_EMPTY_BYTE, sizeof(vm_table_pair_t) * (1 << pow2));
ret->alloc = pow2;
ret->used = 0;
ret->len = 0;
ret->mark = false;
ret->pairs_auto = false;
}

void vm_table_set(vm_table_t *restrict table, vm_obj_t key, vm_obj_t value) {
void vm_table_set(vm_obj_table_t *restrict table, vm_obj_t key, vm_obj_t value) {
if (table->alloc == 0) {
return;
}
size_t len = 1 << table->alloc;
size_t and = len - 1;
size_t stop = vm_value_hash(key) & and;
size_t stop = vm_obj_hash(key) & and;
size_t next = stop & and;
do {
vm_table_pair_t *pair = &table->pairs[next];
Expand Down Expand Up @@ -140,8 +130,14 @@ void vm_table_set(vm_table_t *restrict table, vm_obj_t key, vm_obj_t value) {
}
}
} else if ((table->used + 1) * 100 >= ((uint32_t)1 << table->alloc) * 76) {
vm_table_t ret;
vm_table_init_size(&ret, table->alloc + 1);
vm_obj_table_t ret;
ret.pairs = vm_malloc(sizeof(vm_table_pair_t) * (2 << table->alloc));
memset(ret.pairs, VM_EMPTY_BYTE, sizeof(vm_table_pair_t) * (2 << table->alloc));
ret.alloc = table->alloc + 1;
ret.used = 0;
ret.len = 0;
ret.mark = false;
ret.pairs_auto = false;
for (size_t i = 0; i < len; i++) {
vm_table_pair_t *in_pair = &table->pairs[i];
if (!vm_obj_is_empty(in_pair->key) && !vm_obj_is_nil(in_pair->key)) {
Expand Down Expand Up @@ -173,11 +169,11 @@ void vm_table_set(vm_table_t *restrict table, vm_obj_t key, vm_obj_t value) {
}
}

void vm_table_set_pair(vm_table_t *table, vm_table_pair_t *pair) {
void vm_table_set_pair(vm_obj_table_t *table, vm_table_pair_t *pair) {
vm_table_set(table, pair->key, pair->value);
}

void vm_table_get_pair(vm_table_t *table, vm_table_pair_t *out) {
void vm_table_get_pair(vm_obj_table_t *table, vm_table_pair_t *out) {
vm_table_pair_t *pair = vm_table_lookup(table, out->key);
if (pair != NULL) {
out->value = pair->value;
Expand Down
12 changes: 6 additions & 6 deletions vm/obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ struct vm_table_pair_t;
typedef struct vm_table_pair_t vm_table_pair_t;

bool vm_obj_eq(vm_obj_t lhs, vm_obj_t rhs);
bool vm_value_is_int(vm_obj_t val);
bool vm_obj_is_int(vm_obj_t val);

vm_table_t *vm_table_new(vm_t *vm);
vm_table_pair_t *vm_table_lookup(vm_table_t *table, vm_obj_t key);
void vm_table_set(vm_table_t *table, vm_obj_t key, vm_obj_t value);
void vm_table_set_pair(vm_table_t *table, vm_table_pair_t *pair);
void vm_table_get_pair(vm_table_t *table, vm_table_pair_t *pair);
vm_obj_table_t *vm_table_new(vm_t *vm);
vm_table_pair_t *vm_table_lookup(vm_obj_table_t *table, vm_obj_t key);
void vm_table_set(vm_obj_table_t *table, vm_obj_t key, vm_obj_t value);
void vm_table_set_pair(vm_obj_table_t *table, vm_table_pair_t *pair);
void vm_table_get_pair(vm_obj_table_t *table, vm_table_pair_t *pair);

#endif
34 changes: 17 additions & 17 deletions vm/std.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void vm_std_load(vm_t *vm, vm_obj_t *args) {
const char *str = vm_obj_get_string(args[0])->buf;
vm_ir_block_t *entry = vm_lang_lua_compile(vm, str, "__load__");

vm_closure_t *closure = vm_malloc(sizeof(vm_closure_t));
vm_obj_closure_t *closure = vm_malloc(sizeof(vm_obj_closure_t));
closure->block = entry;
closure->len = 0;
vm_obj_t ret = vm_obj_of_closure(closure);
Expand Down Expand Up @@ -86,7 +86,7 @@ void vm_std_vm_closure(vm_t *vm, vm_obj_t *args) {
*args = vm_obj_of_nil();
return;
}
vm_closure_t *closure = vm_malloc(sizeof(vm_closure_t) + sizeof(vm_obj_t) * (nargs - 1));
vm_obj_closure_t *closure = vm_malloc(sizeof(vm_obj_closure_t) + sizeof(vm_obj_t) * (nargs - 1));
closure->block = vm_obj_get_block(args[0]);
closure->len = nargs - 1;
for (size_t i = 1; !vm_obj_is_empty(args[i]); i++) {
Expand Down Expand Up @@ -162,7 +162,7 @@ void vm_std_type(vm_t *vm, vm_obj_t *args) {
void vm_std_tostring(vm_t *vm, vm_obj_t *args) {
(void)vm;
vm_io_buffer_t out = {0};
vm_value_buffer_tostring(&out, *args);
vm_obj_buffer_tostring(&out, *args);
*args = vm_str(vm, out.buf);
vm_free(out.buf);
}
Expand Down Expand Up @@ -190,7 +190,7 @@ void vm_std_print(vm_t *vm, vm_obj_t *args) {
if (!first) {
vm_io_buffer_format(&out, "\t");
}
vm_value_buffer_tostring(&out, *args++);
vm_obj_buffer_tostring(&out, *args++);
first = false;
}
fprintf(stdout, "%.*s\n", (int)out.len, out.buf);
Expand All @@ -204,7 +204,7 @@ void vm_std_io_write(vm_t *vm, vm_obj_t *args) {
vm_obj_t *ret = args;
vm_io_buffer_t out = {0};
while (!vm_obj_is_empty(args[0])) {
vm_value_buffer_tostring(&out, *args++);
vm_obj_buffer_tostring(&out, *args++);
}
fprintf(stdout, "%.*s", (int)out.len, out.buf);
*ret = vm_obj_of_nil();
Expand Down Expand Up @@ -378,7 +378,7 @@ void vm_std_string_format(vm_t *vm, vm_obj_t *args) {
}

void vm_std_set_arg(vm_t *vm, const char *prog, const char *file, int argc, char **argv) {
vm_table_t *arg = vm_table_new(vm);
vm_obj_table_t *arg = vm_table_new(vm);
if (prog != NULL) {
vm_table_set(arg, vm_obj_of_number(-1), vm_str(vm, prog));
}
Expand All @@ -397,8 +397,8 @@ void vm_std_table_keys(vm_t *vm, vm_obj_t *args) {
*args = vm_obj_of_error(vm_error_from_msg(VM_LOCATION_RANGE_FUNC, "table.values: expect a table"));
return;
}
vm_table_t *ret = vm_table_new(vm);
vm_table_t *tab = vm_obj_get_table(args[0]);
vm_obj_table_t *ret = vm_table_new(vm);
vm_obj_table_t *tab = vm_obj_get_table(args[0]);
size_t len = 1 << tab->alloc;
size_t write_head = 1;
for (size_t i = 0; i < len; i++) {
Expand All @@ -418,8 +418,8 @@ void vm_std_table_values(vm_t *vm, vm_obj_t *args) {
*args = vm_obj_of_error(vm_error_from_msg(VM_LOCATION_RANGE_FUNC, "table.values: expect a table"));
return;
}
vm_table_t *ret = vm_table_new(vm);
vm_table_t *tab = vm_obj_get_table(args[0]);
vm_obj_table_t *ret = vm_table_new(vm);
vm_obj_table_t *tab = vm_obj_get_table(args[0]);
size_t len = 1 << tab->alloc;
size_t write_head = 1;
for (size_t i = 0; i < len; i++) {
Expand Down Expand Up @@ -453,24 +453,24 @@ void vm_std_vm_import(vm_t *vm, vm_obj_t *args) {
}

void vm_std_new(vm_t *vm) {
vm_table_t *std = vm_table_new(vm);
vm_obj_table_t *std = vm_table_new(vm);

srand(0);

{
vm_table_t *io = vm_table_new(vm);
vm_obj_table_t *io = vm_table_new(vm);
vm_table_set(std, vm_str(vm, "io"), vm_obj_of_table(io));
vm_table_set(io, vm_str(vm, "write"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_io_write)));
}

{
vm_table_t *string = vm_table_new(vm);
vm_obj_table_t *string = vm_table_new(vm);
vm_table_set(std, vm_str(vm, "string"), vm_obj_of_table(string));
vm_table_set(string, vm_str(vm, "format"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_string_format)));
}

{
vm_table_t *tvm = vm_table_new(vm);
vm_obj_table_t *tvm = vm_table_new(vm);
vm_table_set(std, vm_str(vm, "vm"), vm_obj_of_table(tvm));
vm_table_set(tvm, vm_str(vm, "import"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_vm_import)));
vm_table_set(tvm, vm_str(vm, "gc"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_vm_gc)));
Expand All @@ -480,19 +480,19 @@ void vm_std_new(vm_t *vm) {
}

{
vm_table_t *math = vm_table_new(vm);
vm_obj_table_t *math = vm_table_new(vm);
vm_table_set(std, vm_str(vm, "math"), vm_obj_of_table(math));
vm_table_set(math, vm_str(vm, "randint"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_math_rand_int)));
}

{
vm_table_t *os = vm_table_new(vm);
vm_obj_table_t *os = vm_table_new(vm);
vm_table_set(std, vm_str(vm, "os"), vm_obj_of_table(os));
vm_table_set(os, vm_str(vm, "exit"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_os_exit)));
}

{
vm_table_t *table = vm_table_new(vm);
vm_obj_table_t *table = vm_table_new(vm);
vm_table_set(std, vm_str(vm, "table"), vm_obj_of_table(table));
vm_table_set(table, vm_str(vm, "keys"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_table_keys)));
vm_table_set(table, vm_str(vm, "values"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_table_values)));
Expand Down
2 changes: 1 addition & 1 deletion vm/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#define VM_STD_REF(vm, x) (vm_config_add_extern((vm), &(x)), &(x))

void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value);
void vm_obj_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value);
void vm_std_set_arg(vm_t *vm, const char *prog, const char *file, int argc, char **argv);
void vm_std_new(vm_t *vm);

Expand Down
Loading

0 comments on commit 230658e

Please sign in to comment.