Skip to content

Commit

Permalink
header reorder
Browse files Browse the repository at this point in the history
Signed-off-by: Shaw Summa <[email protected]>
  • Loading branch information
ShawSumma committed Nov 4, 2024
1 parent 8e2a4b5 commit 28bc155
Show file tree
Hide file tree
Showing 25 changed files with 417 additions and 343 deletions.
2 changes: 1 addition & 1 deletion main/minivm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../vm/backend/backend.h"
#include "../vm/vm.h"
#include "../vm/obj.h"
#include "../vm/ir.h"
#include "../vm/io.h"
#include "../vm/std.h"
Expand Down
2 changes: 1 addition & 1 deletion main/primes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
if i % n == n - 1:
f.write('\n ')
f.write('\n};\n\n')
f.write('static uint32_t vm_primes_mod(uint8_t p, uint32_t n) {\n')
f.write('static inline uint32_t vm_primes_mod(uint8_t p, uint32_t n) {\n')
f.write(' switch (p) {\n')
f.write(' default:\n')
f.write(' __builtin_unreachable();\n')
Expand Down
6 changes: 3 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ BASE_LDFLAGS := ${OPT} ${LDFLAGS} ${LIBM_FLAGS}
# object files and depends
MAIN_SRCS = main/minivm.c
VM_SRCS := $(shell find vm | grep \\.c)
MI_SRCS := vendor/mimalloc/src/prim/prim.c vendor/mimalloc/src/alloc-posix.c vendor/mimalloc/src/alloc-aligned.c vendor/mimalloc/src/alloc.c vendor/mimalloc/src/arena.c vendor/mimalloc/src/bitmap.c vendor/mimalloc/src/heap.c vendor/mimalloc/src/init.c vendor/mimalloc/src/libc.c vendor/mimalloc/src/options.c vendor/mimalloc/src/os.c vendor/mimalloc/src/page.c vendor/mimalloc/src/random.c vendor/mimalloc/src/segment-map.c vendor/mimalloc/src/segment.c vendor/mimalloc/src/stats.c
# MI_SRCS := vendor/mimalloc/src/prim/prim.c vendor/mimalloc/src/alloc-posix.c vendor/mimalloc/src/alloc-aligned.c vendor/mimalloc/src/alloc.c vendor/mimalloc/src/arena.c vendor/mimalloc/src/bitmap.c vendor/mimalloc/src/heap.c vendor/mimalloc/src/init.c vendor/mimalloc/src/libc.c vendor/mimalloc/src/options.c vendor/mimalloc/src/os.c vendor/mimalloc/src/page.c vendor/mimalloc/src/random.c vendor/mimalloc/src/segment-map.c vendor/mimalloc/src/segment.c vendor/mimalloc/src/stats.c
TS_SRCS += vendor/tree-sitter/lib/src/alloc.c vendor/tree-sitter/lib/src/get_changed_ranges.c vendor/tree-sitter/lib/src/language.c vendor/tree-sitter/lib/src/lexer.c vendor/tree-sitter/lib/src/node.c vendor/tree-sitter/lib/src/parser.c vendor/tree-sitter/lib/src/query.c vendor/tree-sitter/lib/src/stack.c vendor/tree-sitter/lib/src/subtree.c vendor/tree-sitter/lib/src/tree_cursor.c vendor/tree-sitter/lib/src/tree.c vendor/tree-sitter/lib/src/wasm_store.c
IC_SRCS := vendor/isocline/src/isocline.c
MAIN_SRCS += ${VM_SRCS} ${TS_SRCS} ${IC_SRCS} ${MI_SRCS}
Expand Down Expand Up @@ -50,12 +50,12 @@ clean: .dummy
rm -rf build

gcc-pgo: .dummy
$(MAKE) -Bj minivm OPT="$(OPT) -fgcse-sm -fgcse-las -fipa-pta -fdevirtualize-at-ltrans -fdevirtualize-speculatively -fno-exceptions -fomit-frame-pointer -fprofile-generate"
$(MAKE) -Bj minivm OPT="-g3 $(OPT) -fprofile-generate"
build/bin/minivm test/lua/fib/fib.lua
build/bin/minivm test/lua/tables/trees.lua
build/bin/minivm test/lua/closure/funcret.lua
build/bin/minivm test/lang/lr1.lua
$(MAKE) -Bj minivm OPT="$(OPT) -fgcse-sm -fgcse-las -fipa-pta -fdevirtualize-at-ltrans -fdevirtualize-speculatively -fno-exceptions -fomit-frame-pointer -fprofile-use"
$(MAKE) -Bj minivm OPT="$(OPT) -fgcse-sm -fgcse-las -fipa-pta -fomit-frame-pointer -fno-reorder-functions -fprofile-use"

clang-pgo: .dummy
$(MAKE) -Bj minivm OPT="$(OPT) -mllvm -polly -fno-exceptions -fprofile-instr-generate"
Expand Down
1 change: 1 addition & 0 deletions vm/ast/ast.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ast.h"
#include "../lib.h"

void vm_ast_free_form(vm_ast_form_t form) {
for (size_t i = 0; i < form.len; i++) {
Expand Down
2 changes: 0 additions & 2 deletions vm/ast/ast.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#if !defined(VM_HEADER_AST_AST)
#define VM_HEADER_AST_AST

#include "../lib.h"
#include "../obj.h"
#include "../errors.h"

struct vm_ast_form_t;
Expand Down
2 changes: 2 additions & 0 deletions vm/ast/build.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#include <stdarg.h>
#include "build.h"
#include "../lib.h"
#include "../obj.h"

#define VM_MACRO_SELECT(_0, _1, _2, NAME, ...) NAME
#define vm_ast_form(TYPE_, ...) \
Expand Down
1 change: 1 addition & 0 deletions vm/ast/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define VM_HEADER_LANG_BUILD

#include "ast.h"
#include "../vm.h"

vm_ast_node_t vm_ast_build_empty(vm_ast_node_t lhs, vm_ast_node_t rhs);
// blocks
Expand Down
2 changes: 2 additions & 0 deletions vm/ast/comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "../ir.h"
#include "../gc.h"
#include "../errors.h"
#include "../lib.h"
#include "../obj.h"

struct vm_ast_comp_t;
typedef struct vm_ast_comp_t vm_ast_comp_t;
Expand Down
1 change: 0 additions & 1 deletion vm/ast/comp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#define VM_HEADER_AST_COMP

#include "ast.h"
#include "../ir.h"

vm_ir_block_t *vm_ast_comp_more(vm_t *vm, vm_ast_node_t node);

Expand Down
5 changes: 2 additions & 3 deletions vm/backend/backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
#include "../gc.h"
#include "../ir.h"
#include "../obj.h"
#include "../vm.h"
#include "../tables.h"
#include "../math.h"
#include "../io.h"

#define VM_INLINE inline

#if VM_USE_SPALL_INSTR

#include "../../vendor/spall/auto.h"

#define VM_OPCODE_SPALL_BEGIN(s) ({ \
Expand Down Expand Up @@ -182,7 +181,7 @@ static VM_INLINE vm_obj_t vm_interp_pow(vm_t *vm, vm_obj_t v1, vm_obj_t v2) {
}

static VM_INLINE vm_obj_t vm_interp_concat(vm_t *vm, vm_obj_t v1, vm_obj_t v2) {
if (vm_obj_is_string(v1) && vm_obj_is_string(v2)) {
if (vm_obj_is_buffer(v1) && vm_obj_is_buffer(v2)) {
vm_io_buffer_t *buf = vm_io_buffer_new();
vm_io_buffer_object_tostring(buf, v1);
vm_io_buffer_object_tostring(buf, v2);
Expand Down
15 changes: 9 additions & 6 deletions vm/gc.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

#include "gc.h"
#include "obj.h"
#include "ir.h"
#include "lib.h"
#include "tables.h"

#include <stddef.h>

struct vm_gc_objs_t;
Expand Down Expand Up @@ -81,8 +84,8 @@ static inline void vm_gc_mark_block(vm_ir_block_t *restrict block) {


static inline void vm_gc_mark_obj(vm_obj_t obj) {
if (vm_obj_is_string(obj)) {
vm_io_buffer_t *buffer = vm_obj_get_string(obj);
if (vm_obj_is_buffer(obj)) {
vm_io_buffer_t *buffer = vm_obj_get_buffer(obj);
buffer->mark = true;
} else if (vm_obj_is_table(obj)) {
vm_obj_table_t *restrict table = vm_obj_get_table(obj);
Expand Down Expand Up @@ -112,7 +115,7 @@ void vm_gc_mark(vm_t *vm, vm_obj_t *top) {
vm_gc_mark_block(blocks->block);
}
vm_gc_mark_obj(vm->std);
for (vm_obj_t *head = vm->base; head < top; head++) {
for (vm_obj_t *head = vm->base; head <= top; head++) {
vm_gc_mark_obj(*head);
}
}
Expand All @@ -128,8 +131,8 @@ void vm_gc_sweep(vm_t *vm) {
#endif
for (size_t i = 0; i < gc->objs.len; i++) {
vm_obj_t obj = gc->objs.objs[i];
if (vm_obj_is_string(obj)) {
vm_io_buffer_t *buffer = vm_obj_get_string(obj);
if (vm_obj_is_buffer(obj)) {
vm_io_buffer_t *buffer = vm_obj_get_buffer(obj);
if (!buffer->mark) {
vm_free(buffer->buf);
vm_free(buffer);
Expand Down Expand Up @@ -246,7 +249,7 @@ void vm_gc_add(vm_t *vm, vm_obj_t obj) {
vm_gc_t *restrict gc = vm->gc;
vm_gc_objs_add(&gc->objs, obj);
#if VM_GC_STATS
if (vm_obj_is_string(obj)) {
if (vm_obj_is_buffer(obj)) {
gc->stats.by_type.string.count += 1;
} else if (vm_obj_is_table(obj)) {
gc->stats.by_type.table.count += 1;
Expand Down
18 changes: 10 additions & 8 deletions vm/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "io.h"
#include "math.h"
#include "lib.h"
#include "obj.h"
#include "tables.h"

#include <stdio.h>

Expand Down Expand Up @@ -105,8 +107,8 @@ void vm_io_buffer_print_lit(vm_io_buffer_t *out, vm_obj_t value) {
vm_io_buffer_format(out, VM_FORMAT_FLOAT, vm_obj_get_number(value));
} else if (vm_obj_is_ffi(value)) {
vm_io_buffer_format(out, "<function: %p>", vm_obj_get_ffi(value));
} else if (vm_obj_is_string(value)) {
vm_io_buffer_format(out, "\"%s\"", vm_obj_get_string(value)->buf);
} else if (vm_obj_is_buffer(value)) {
vm_io_buffer_format(out, "\"%s\"", vm_obj_get_buffer(value)->buf);
} else if (vm_obj_is_table(value)) {
vm_io_buffer_format(out, "<table: %p>", vm_obj_get_table(value));
} else if (vm_obj_is_closure(value)) {
Expand Down Expand Up @@ -147,9 +149,9 @@ void vm_io_buffer_obj_debug(vm_io_buffer_t *out, size_t indent, const char *pref
vm_io_indent(out, indent, prefix);
vm_io_buffer_format(out, VM_FORMAT_FLOAT "\n", vm_obj_get_number(value));
}
if (vm_obj_is_string(value)) {
if (vm_obj_is_buffer(value)) {
vm_io_indent(out, indent, prefix);
vm_io_buffer_format(out, "\"%s\"\n", vm_obj_get_string(value)->buf);
vm_io_buffer_format(out, "\"%s\"\n", vm_obj_get_buffer(value)->buf);
}
if (vm_obj_is_closure(value)) {
vm_io_indent(out, indent, prefix);
Expand Down Expand Up @@ -183,9 +185,9 @@ void vm_io_buffer_obj_debug(vm_io_buffer_t *out, size_t indent, const char *pref
snprintf(buf, 63, "[[" VM_FORMAT_FLOAT "]] = ", vm_obj_get_number(key));
vm_io_buffer_obj_debug(out, indent + 1, buf, tab->entries[vm_primes_table[tab->size] + i], &next);
}
else if (vm_obj_is_string(key)) {
else if (vm_obj_is_buffer(key)) {
vm_io_buffer_t *buf = vm_io_buffer_new();
vm_io_buffer_format(buf, "%s = ", vm_obj_get_string(key)->buf);
vm_io_buffer_format(buf, "%s = ", vm_obj_get_buffer(key)->buf);
vm_io_buffer_obj_debug(out, indent + 1, buf->buf, tab->entries[vm_primes_table[tab->size] + i], &next);
vm_free(buf->buf);
vm_free(buf);
Expand Down Expand Up @@ -218,8 +220,8 @@ void vm_io_buffer_object_tostring(vm_io_buffer_t *buf, vm_obj_t value) {
if (vm_obj_is_number(value)) {
vm_io_buffer_format(buf, VM_FORMAT_FLOAT, vm_obj_get_number(value));
}
if (vm_obj_is_string(value)) {
vm_io_buffer_format(buf, "%s", vm_obj_get_string(value)->buf);
if (vm_obj_is_buffer(value)) {
vm_io_buffer_format(buf, "%s", vm_obj_get_buffer(value)->buf);
}
if (vm_obj_is_closure(value)) {
vm_io_buffer_format(buf, "<function: %p>", vm_obj_get_closure(value));
Expand Down
32 changes: 27 additions & 5 deletions vm/lib.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

#include "vm.h"
#include "lib.h"
#include <assert.h>

#if VM_MALLOC_MI
#include "../vendor/mimalloc/include/mimalloc.h"
Expand Down Expand Up @@ -29,22 +31,42 @@ char *vm_strdup(const char *x) {
#include <stdlib.h>

void *vm_malloc(size_t x) {
return malloc(x);
if (x == 0) {
return NULL;
}
void *ret = malloc(x);
assert(ret != NULL);
return ret;
}

void *vm_calloc(size_t x) {
return calloc(x, 1);
if (x == 0) {
return NULL;
}
void *ret = calloc(x, 1);
assert(ret != NULL);
return ret;
}

void *vm_realloc(void *x, size_t y) {
return realloc(x, y);
if (y == 0) {
vm_free(x);
}
void *ret = realloc(x, y);
assert(ret != NULL);
return ret;
}

void vm_free(const void *x) {
free((void *) (x));
if (x != NULL) {
free((void *) (x));
}
}

char *vm_strdup(const char *x) {
return strdup(x);
assert(x != NULL);
char *ret = strdup(x);
assert(ret != NULL);
return ret;
}
#endif
1 change: 0 additions & 1 deletion vm/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ void *vm_realloc(void *ptr, size_t size);
void vm_free(const void *ptr);
char *vm_strdup(const char *str);


#endif
4 changes: 3 additions & 1 deletion vm/lua/ast.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

#include "../ast/ast.h"
#include "../../vendor/tree-sitter/lib/include/tree_sitter/api.h"
#include "../ast/build.h"
#include "../ast/comp.h"
#include "../io.h"
#include "../ir.h"
#include "../lib.h"
#include "../obj.h"
#include "../tables.h"

const TSLanguage *tree_sitter_lua(void);

Expand Down
5 changes: 4 additions & 1 deletion vm/lua/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "../ir.h"
#include "../io.h"
#include "../vm.h"
#include "../lib.h"
#include "../obj.h"
#include "../tables.h"
#include "../ast/ast.h"
#include "../backend/backend.h"

Expand Down Expand Up @@ -33,7 +36,7 @@ with_new_std:;
for (size_t i = 0; i < len; i++) {
vm_obj_t std_key = std->entries[i];
if (vm_obj_is_table(std_key)) {
const char *got = vm_obj_get_string(std_key)->buf;
const char *got = vm_obj_get_buffer(std_key)->buf;
size_t i = 0;
while (got[i] != '\0') {
if (last_word[i] == '\0') {
Expand Down
28 changes: 16 additions & 12 deletions vm/math.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "math.h"
#include "obj.h"
#include "errors.h"

bool vm_obj_unsafe_eq(vm_obj_t v1, vm_obj_t v2) {
Expand All @@ -9,8 +10,8 @@ bool vm_obj_unsafe_eq(vm_obj_t v1, vm_obj_t v2) {
return vm_obj_get_boolean(v1) == vm_obj_get_boolean(v2);
} else if (vm_obj_is_number(v1) && vm_obj_is_number(v2)) {
return vm_obj_get_number(v1) == vm_obj_get_number(v2);
} else if (vm_obj_is_string(v1) && vm_obj_is_string(v2)) {
return strcmp(vm_obj_get_string(v1)->buf, vm_obj_get_string(v2)->buf) == 0;
} else if (vm_obj_is_buffer(v1) && vm_obj_is_buffer(v2)) {
return strcmp(vm_obj_get_buffer(v1)->buf, vm_obj_get_buffer(v2)->buf) == 0;
} else if (vm_obj_is_table(v1) && vm_obj_is_table(v2)) {
return vm_obj_get_table(v1) == vm_obj_get_table(v2);
} else if (vm_obj_is_closure(v1) && vm_obj_is_closure(v2)) {
Expand All @@ -25,8 +26,8 @@ bool vm_obj_unsafe_eq(vm_obj_t v1, vm_obj_t v2) {
bool vm_obj_unsafe_lt(vm_obj_t v1, vm_obj_t v2) {
if (vm_obj_is_number(v1) && vm_obj_is_number(v2)) {
return vm_obj_get_number(v1) < vm_obj_get_number(v2);
} else if (vm_obj_is_string(v1) && vm_obj_is_string(v2)) {
return strcmp(vm_obj_get_string(v1)->buf, vm_obj_get_string(v2)->buf) < 0;
} else if (vm_obj_is_buffer(v1) && vm_obj_is_buffer(v2)) {
return strcmp(vm_obj_get_buffer(v1)->buf, vm_obj_get_buffer(v2)->buf) < 0;
} else {
return false;
}
Expand All @@ -35,22 +36,23 @@ bool vm_obj_unsafe_lt(vm_obj_t v1, vm_obj_t v2) {
bool vm_obj_unsafe_le(vm_obj_t v1, vm_obj_t v2) {
if (vm_obj_is_number(v1) && vm_obj_is_number(v2)) {
return vm_obj_get_number(v1) <= vm_obj_get_number(v2);
} else if (vm_obj_is_string(v1) && vm_obj_is_string(v2)) {
return strcmp(vm_obj_get_string(v1)->buf, vm_obj_get_string(v2)->buf) <= 0;
} else if (vm_obj_is_buffer(v1) && vm_obj_is_buffer(v2)) {
return strcmp(vm_obj_get_buffer(v1)->buf, vm_obj_get_buffer(v2)->buf) <= 0;
} else {
return false;
}
}

vm_obj_t vm_obj_eq(vm_t *vm, vm_obj_t v1, vm_obj_t v2) {
(void) vm;
if (vm_obj_is_nil(v1) && vm_obj_is_nil(v2)) {
return vm_obj_of_boolean(true);
} else if (vm_obj_is_boolean(v1) && vm_obj_is_boolean(v2)) {
return vm_obj_of_boolean(vm_obj_get_boolean(v1) == vm_obj_get_boolean(v2));
} else if (vm_obj_is_number(v1) && vm_obj_is_number(v2)) {
return vm_obj_of_boolean(vm_obj_get_number(v1) == vm_obj_get_number(v2));
} else if (vm_obj_is_string(v1) && vm_obj_is_string(v2)) {
return vm_obj_of_boolean(strcmp(vm_obj_get_string(v1)->buf, vm_obj_get_string(v2)->buf) == 0);
} else if (vm_obj_is_buffer(v1) && vm_obj_is_buffer(v2)) {
return vm_obj_of_boolean(strcmp(vm_obj_get_buffer(v1)->buf, vm_obj_get_buffer(v2)->buf) == 0);
} else if (vm_obj_is_table(v1) && vm_obj_is_table(v2)) {
// TODO: use metamethod
return vm_obj_of_boolean(vm_obj_get_table(v1) == vm_obj_get_table(v2));
Expand All @@ -64,20 +66,22 @@ vm_obj_t vm_obj_eq(vm_t *vm, vm_obj_t v1, vm_obj_t v2) {
}

vm_obj_t vm_obj_lt(vm_t *vm, vm_obj_t v1, vm_obj_t v2) {
(void) vm;
if (vm_obj_is_number(v1) && vm_obj_is_number(v2)) {
return vm_obj_of_boolean(vm_obj_get_number(v1) < vm_obj_get_number(v2));
} else if (vm_obj_is_string(v1) && vm_obj_is_string(v2)) {
return vm_obj_of_boolean(strcmp(vm_obj_get_string(v1)->buf, vm_obj_get_string(v2)->buf) < 0);
} else if (vm_obj_is_buffer(v1) && vm_obj_is_buffer(v2)) {
return vm_obj_of_boolean(strcmp(vm_obj_get_buffer(v1)->buf, vm_obj_get_buffer(v2)->buf) < 0);
} else {
return vm_obj_of_error(vm_error_from_msg(VM_LOCATION_RANGE_FUNC, "not comparable"));
}
}

vm_obj_t vm_obj_le(vm_t *vm, vm_obj_t v1, vm_obj_t v2) {
(void) vm;
if (vm_obj_is_number(v1) && vm_obj_is_number(v2)) {
return vm_obj_of_boolean(vm_obj_get_number(v1) < vm_obj_get_number(v2));
} else if (vm_obj_is_string(v1) && vm_obj_is_string(v2)) {
return vm_obj_of_boolean(strcmp(vm_obj_get_string(v1)->buf, vm_obj_get_string(v2)->buf) < 0);
} else if (vm_obj_is_buffer(v1) && vm_obj_is_buffer(v2)) {
return vm_obj_of_boolean(strcmp(vm_obj_get_buffer(v1)->buf, vm_obj_get_buffer(v2)->buf) < 0);
} else {
return vm_obj_of_error(vm_error_from_msg(VM_LOCATION_RANGE_FUNC, "not comparable"));
}
Expand Down
Loading

0 comments on commit 28bc155

Please sign in to comment.