Skip to content

Commit

Permalink
obj
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawSumma committed Jul 1, 2024
1 parent 46e9cfe commit 9e20183
Show file tree
Hide file tree
Showing 23 changed files with 439 additions and 418 deletions.
8 changes: 6 additions & 2 deletions main/minivm.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char **argv) {
vm_t *vm = vm_malloc(sizeof(vm_t));
*vm = (vm_t) {
.use_num = VM_USE_NUM_F64,
.regs = vm_malloc(sizeof(vm_std_value_t) * 65536),
.regs = vm_malloc(sizeof(vm_obj_t) * 65536),
};

vm_std_new(vm);
Expand All @@ -38,6 +38,10 @@ int main(int argc, char **argv) {
echo = true;
} else if (!strcmp(arg, "--no-echo")) {
echo = false;
} else if (!strcmp(arg, "--dump-ir")) {
vm->dump_ir = true;
} else if (!strcmp(arg, "--no-dump-ir")) {
vm->dump_ir = false;
} else if (!strcmp(arg, "--repl")) {
vm_repl(vm);
isrepl = false;
Expand Down Expand Up @@ -111,7 +115,7 @@ int main(int argc, char **argv) {

vm_block_t *entry = vm_compile(vm, src);

vm_std_value_t value = vm_run_main(vm, entry);
vm_obj_t value = vm_run_main(vm, entry);
if (value.tag == VM_TAG_ERROR) {
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ mac: .dummy
WINDOWS_RAYLIB_OBJS = $(RAYLIB_DIR)/rcore.o $(RAYLIB_DIR)/rglfw.o $(RAYLIB_DIR)/rshapes.o $(RAYLIB_DIR)/rtextures.o $(RAYLIB_DIR)/rtext.o $(RAYLIB_DIR)/rmodels.o $(RAYLIB_DIR)/raudio.o $(RAYLIB_DIR)/utils.o

windows: .dummy
$(PRE) make -Bj$(J) -C vendor/raylib/src CC="$(RAYLIB_CC)" LDFLAGS="$(OPT)" CFLAGS="-w -DPLATFORM_DESKTOP -DDWIN32_LEAN_AND_MEAN $(OPT) $(CLFAGS)" PLATFORM=PLATFORM_DESKTOP OS=WINDOWS_NT
$(PRE) make -Bj$(J) -f tool/core.mak $(TARGET) OS=WINDOWS CC="$(CC)" EXE=.exe TEST_LUA="$(TEST_LUA)" CFLAGS="-DVM_USE_RAYLIB -DVM_NO_GC -DWIN32_LEAN_AND_MEAN $(OPT) $(CFLAGS)" LDFLAGS="$(RAYLIB_DIR)/libraylib.a -lopengl32 -lgdi32 $(OPT) $(LDFLAGS)"
$(PRE) make -Bj$(J) -C vendor/raylib/src CC="$(RAYLIB_CC)" LDFLAGS="$(OPT)" CFLAGS="-w -DPLATFORM_DESKTOP -DWIN32_LEAN_AND_MEAN $(OPT) $(CLFAGS)" PLATFORM=PLATFORM_DESKTOP OS=WINDOWS_NT
$(PRE) make -Bj$(J) -f tool/core.mak $(TARGET) OS=WINDOWS CC="$(CC)" EXE=.exe TEST_LUA="$(TEST_LUA)" CFLAGS="-DVM_USE_RAYLIB -DVM_NO_GC -DWIN32_LEAN_AND_MEAN $(OPT) $(CFLAGS)" LDFLAGS="$(RAYLIB_DIR)/libraylib.a -lopengl32 -lgdi32 -lwinmm $(OPT) $(LDFLAGS)"

freebsd: .dummy
$(PRE) gmake -Bj$(J) -C vendor/raylib/src CC="$(RAYLIB_CC)" LDFLAGS="$(OPT)" CFLAGS="-w $(OPT) $(CLFAGS) -DPLATFORM_DESKTOP" PLATFORM=PLATFORM_DESKTOP
Expand Down
2 changes: 1 addition & 1 deletion vm/ast/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void vm_ast_free_ident(const char *ident) {
vm_free(ident);
}

void vm_ast_free_literal(vm_std_value_t literal) {
void vm_ast_free_literal(vm_obj_t literal) {
switch (literal.tag) {
case VM_TAG_STR: {
vm_free(literal.value.str);
Expand Down
4 changes: 2 additions & 2 deletions vm/ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct vm_ast_form_t {
union vm_ast_node_value_t {
vm_ast_form_t form;
const char *ident;
vm_std_value_t literal;
vm_obj_t literal;
};

struct vm_ast_node_t {
Expand All @@ -88,7 +88,7 @@ struct vm_ast_node_t {
const char *vm_ast_format(vm_ast_node_t *node);
void vm_ast_free_form(vm_ast_form_t node);
void vm_ast_free_ident(const char *node);
void vm_ast_free_literal(vm_std_value_t node);
void vm_ast_free_literal(vm_obj_t node);
void vm_ast_free_node(vm_ast_node_t node);

#endif
4 changes: 2 additions & 2 deletions vm/ast/build.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ vm_ast_node_t vm_ast_build_block(size_t len, ...) {
vm_ast_node_t vm_ast_build_error(const char *str) {
return (vm_ast_node_t){
.type = VM_AST_NODE_LITERAL,
.value.literal = (vm_std_value_t){
.value.literal = (vm_obj_t){
.tag = VM_TAG_ERROR,
.value.str = str,
},
Expand All @@ -252,7 +252,7 @@ vm_ast_node_t vm_ast_build_error(const char *str) {
vm_ast_node_t vm_ast_build_nil(void) {
return (vm_ast_node_t){
.type = VM_AST_NODE_LITERAL,
.value.literal = (vm_std_value_t){
.value.literal = (vm_obj_t){
.tag = VM_TAG_NIL,
},
};
Expand Down
2 changes: 1 addition & 1 deletion vm/ast/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ vm_ast_node_t vm_ast_build_nil(void);
#define vm_ast_build_literal(TYPE_, VALUE_) \
((vm_ast_node_t){ \
.type = VM_AST_NODE_LITERAL, \
.value.literal = VM_STD_VALUE_LITERAL(TYPE_, VALUE_), \
.value.literal = VM_OBJ_LITERAL(TYPE_, VALUE_), \
})

#define vm_ast_build_ident(STR_) \
Expand Down
42 changes: 21 additions & 21 deletions vm/ast/comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,50 +42,50 @@ struct vm_ast_comp_names_t {
vm_ast_comp_names_t *next;
};

void vm_lua_comp_op_std_pow(vm_t *vm, vm_std_value_t *args) {
vm_std_value_t *ret = args;
void vm_lua_comp_op_std_pow(vm_t *vm, vm_obj_t *args) {
vm_obj_t *ret = args;
double v = vm_value_to_f64(*args++);
while (args->tag != VM_TAG_UNK) {
v = pow(v, vm_value_to_f64(*args++));
}
switch (vm->use_num) {
case VM_USE_NUM_I8: {
*ret = (vm_std_value_t){
*ret = (vm_obj_t){
.tag = VM_TAG_I8,
.value.i8 = (int8_t)v,
};
return;
}
case VM_USE_NUM_I16: {
*ret = (vm_std_value_t){
*ret = (vm_obj_t){
.tag = VM_TAG_I16,
.value.i16 = (int16_t)v,
};
return;
}
case VM_USE_NUM_I32: {
*ret = (vm_std_value_t){
*ret = (vm_obj_t){
.tag = VM_TAG_I32,
.value.i32 = (int32_t)v,
};
return;
}
case VM_USE_NUM_I64: {
*ret = (vm_std_value_t){
*ret = (vm_obj_t){
.tag = VM_TAG_I64,
.value.i64 = (int64_t)v,
};
return;
}
case VM_USE_NUM_F32: {
*ret = (vm_std_value_t){
*ret = (vm_obj_t){
.tag = VM_TAG_F32,
.value.f32 = (float)v,
};
return;
}
case VM_USE_NUM_F64: {
*ret = (vm_std_value_t){
*ret = (vm_obj_t){
.tag = VM_TAG_F64,
.value.f64 = (double)v,
};
Expand Down Expand Up @@ -123,8 +123,8 @@ static void vm_ast_names_free(vm_ast_comp_names_t *names) {
static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node);
static void vm_ast_comp_br(vm_ast_comp_t *comp, vm_ast_node_t node, vm_block_t *iftrue, vm_block_t *iffalse);

extern void vm_std_vm_closure(vm_t *vm, vm_std_value_t *args);
extern void vm_std_vm_concat(vm_t *vm, vm_std_value_t *args);
extern void vm_std_vm_closure(vm_t *vm, vm_obj_t *args);
extern void vm_std_vm_concat(vm_t *vm, vm_obj_t *args);

static vm_arg_t *vm_ast_args(size_t nargs, ...) {
va_list ap;
Expand Down Expand Up @@ -234,7 +234,7 @@ static vm_arg_t vm_ast_comp_get_var(vm_ast_comp_t *comp, const char *name) {
vm_block_t *next = vm_ast_comp_new_block(comp);
vm_arg_t slot = (vm_arg_t){
.type = VM_ARG_LIT,
.lit = (vm_std_value_t){
.lit = (vm_obj_t){
.tag = VM_TAG_I32,
.value.i32 = slotnum,
}};
Expand Down Expand Up @@ -362,7 +362,7 @@ static void vm_ast_comp_br(vm_ast_comp_t *comp, vm_ast_node_t node, vm_block_t *
break;
}
case VM_AST_NODE_LITERAL: {
vm_std_value_t value = node.value.literal;
vm_obj_t value = node.value.literal;
if (value.tag == VM_TAG_ERROR) {
comp->is_error = true;
return;
Expand Down Expand Up @@ -524,7 +524,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) {
comp->cur = iftrue;
vm_arg_t true_value = (vm_arg_t){
.type = VM_ARG_LIT,
.lit = (vm_std_value_t){
.lit = (vm_obj_t){
.value.b = true,
.tag = VM_TAG_BOOL,
},
Expand All @@ -548,7 +548,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) {
comp->cur = iffalse;
vm_arg_t false_value = (vm_arg_t){
.type = VM_ARG_LIT,
.lit = (vm_std_value_t){
.lit = (vm_obj_t){
.value.b = false,
.tag = VM_TAG_BOOL,
},
Expand Down Expand Up @@ -602,7 +602,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) {
vm_ast_free_node(node);
vm_arg_t key_arg = (vm_arg_t){
.type = VM_ARG_LIT,
.lit = (vm_std_value_t){
.lit = (vm_obj_t){
.tag = VM_TAG_STR,
.value.str = vm_strdup(target.value.ident),
},
Expand Down Expand Up @@ -737,7 +737,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) {
vm_arg_t *call_args = vm_malloc(sizeof(vm_arg_t) * 4);
call_args[0] = (vm_arg_t){
.type = VM_ARG_LIT,
.lit = (vm_std_value_t){
.lit = (vm_obj_t){
.tag = VM_TAG_FFI,
.value.ffi = &vm_std_vm_concat,
},
Expand Down Expand Up @@ -770,7 +770,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) {
vm_arg_t *call_args = vm_malloc(sizeof(vm_arg_t) * 4);
call_args[0] = (vm_arg_t){
.type = VM_ARG_LIT,
.lit = (vm_std_value_t){
.lit = (vm_obj_t){
.tag = VM_TAG_FFI,
.value.ffi = &vm_lua_comp_op_std_pow,
},
Expand Down Expand Up @@ -934,7 +934,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) {
vm_arg_t *call_args = vm_malloc(sizeof(vm_arg_t) * (names->caps.len + 3));
call_args[0] = (vm_arg_t){
.type = VM_ARG_LIT,
.lit = (vm_std_value_t){
.lit = (vm_obj_t){
.tag = VM_TAG_FFI,
.value.ffi = &vm_std_vm_closure,
},
Expand Down Expand Up @@ -1026,13 +1026,13 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) {
break;
}
case VM_AST_NODE_LITERAL: {
vm_std_value_t num = node.value.literal;
vm_obj_t num = node.value.literal;
if (num.tag == VM_TAG_NIL) {
return vm_arg_nil();
} else if (num.tag == VM_TAG_STR) {
vm_arg_t str = (vm_arg_t){
.type = VM_ARG_LIT,
.lit = (vm_std_value_t){
.lit = (vm_obj_t){
.tag = VM_TAG_STR,
.value.str = vm_strdup(num.value.str),
},
Expand Down Expand Up @@ -1071,7 +1071,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) {
if (got.type != VM_ARG_NONE) {
vm_arg_t env_key = (vm_arg_t){
.type = VM_ARG_LIT,
.lit = (vm_std_value_t){
.lit = (vm_obj_t){
.tag = VM_TAG_STR,
.value.str = vm_strdup(lit),
},
Expand Down
Loading

0 comments on commit 9e20183

Please sign in to comment.