From: Judah Caruso Date: Mon, 4 Dec 2023 07:26:52 +0000 (-0700) Subject: get libovmwasm to build on darwin (x86_64/arm64) X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=118138ee7c53c26b7077da51e387a0e8f19997ae;p=onyx.git get libovmwasm to build on darwin (x86_64/arm64) --- diff --git a/build.sh b/build.sh index 871e42bb..185df5ab 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,7 @@ #!/bin/sh +set -e + DIST_DIR="./dist" compile_all() { diff --git a/compiler/src/onyx.c b/compiler/src/onyx.c index a8ae0292..ea72c968 100644 --- a/compiler/src/onyx.c +++ b/compiler/src/onyx.c @@ -658,7 +658,7 @@ static b32 process_load_entity(Entity* ent) { bh_path_convert_separators(folder); // This does not take into account #load_path'd folders... - + bh_arr(char *) folders_to_process = NULL; bh_arr_new(global_heap_allocator, folders_to_process, 2); @@ -1079,7 +1079,7 @@ static b32 onyx_run() { onyx_wasm_module_write_to_buffer(context.wasm_module, &code_buffer); return onyx_run_module(code_buffer); - + } #endif @@ -1155,7 +1155,7 @@ static void onyx_watch(CompileOptions *compile_opts) { bh_file_watch_free(&watches); } while(running_watch); - + bh_printf("\e[2J\e[1;1H\e[?25h\n"); } diff --git a/interpreter/build.sh b/interpreter/build.sh index 289f6c8b..86f12b96 100755 --- a/interpreter/build.sh +++ b/interpreter/build.sh @@ -15,6 +15,6 @@ for c_file in $C_FILES; do $ONYX_CC $FLAGS $INCLUDES -fPIC -o $(mktemp -p build_tmp -t XXXXXXX.o) -c $c_file $LIBS done -ar cr "$TARGET" build_tmp/*.o +ar crs "$TARGET" build_tmp/*.o* rm -r "build_tmp" diff --git a/interpreter/include/ovm_debug.h b/interpreter/include/ovm_debug.h index 1d872cc0..366b659f 100644 --- a/interpreter/include/ovm_debug.h +++ b/interpreter/include/ovm_debug.h @@ -120,7 +120,7 @@ typedef struct debug_type_function_t { } debug_type_function_t; typedef struct debug_type_slice_t { - u32 type; + u32 type; } debug_type_slice_t; typedef struct debug_type_enum_value_t { @@ -315,7 +315,7 @@ typedef struct debug_state_t { u32 client_fd; bh_buffer send_buffer; - + u32 state_change_pipes[2]; } debug_state_t; @@ -339,7 +339,7 @@ typedef struct debug_runtime_value_builder_t { debug_func_info_t func_info; debug_file_info_t file_info; debug_loc_info_t loc_info; - + // "base_" refers to the top symbol. In a layered // query, this information is not outputted, only // used to lookup the values inside of it. @@ -355,7 +355,7 @@ typedef struct debug_runtime_value_builder_t { u32 it_loc; u32 it_type; char *it_name; - bool it_has_children; + bool it_has_children; } debug_runtime_value_builder_t; void debug_runtime_value_build_init(debug_runtime_value_builder_t *builder, bh_allocator alloc); diff --git a/interpreter/include/vm.h b/interpreter/include/vm.h index 6ebaa474..a9ff8f57 100644 --- a/interpreter/include/vm.h +++ b/interpreter/include/vm.h @@ -106,7 +106,7 @@ void ovm_program_modify_static_int(ovm_program_t *program, int arr, int idx, int // Represents the running configuration and static // data needed by the VM. This is for more "global" data. // If multiple threads are used, only one engine is needed. -// +// struct ovm_engine_t { ovm_store_t *store; @@ -129,7 +129,7 @@ bool ovm_program_load_from_file(ovm_program_t *program, ovm_engine_t *engine, ch // // Represents ephemeral state / execution context. // If multiple threads are used, multiple states are needed. -// +// #define OVM_MAX_PARAM_COUNT 64 @@ -140,7 +140,7 @@ struct ovm_state_t { i32 pc; i32 value_number_offset; - + bh_arr(ovm_value_t) numbered_values; bh_arr(ovm_stack_frame_t) stack_frames; bh_arr(ovm_value_t) registers; @@ -221,7 +221,7 @@ struct ovm_linkable_func_t { i32 param_count; ovm_external_func_t func; }; - + ovm_func_t *ovm_func_new(); ovm_instr_t *ovm_func_add_instruction(ovm_func_t *func, ovm_instr_kind_t instr, ovm_valtype_t type); void ovm_func_delete(ovm_func_t *func); @@ -352,7 +352,7 @@ struct ovm_instr_t { #define OVMI_TRANSMUTE_F32 0x4a // %r = *(t *) &%a (reinterpret bytes) #define OVMI_TRANSMUTE_F64 0x4b // %r = *(t *) &%a (reinterpret bytes) -#define OVMI_CMPXCHG 0x4c // %r = %r == %a ? %b : %r +#define OVMI_CMPXCHG 0x4c // %r = %r == %a ? %b : %r #define OVMI_BREAK 0x4d diff --git a/interpreter/src/vm/vm.c b/interpreter/src/vm/vm.c index 7f0495f9..147aa2f9 100644 --- a/interpreter/src/vm/vm.c +++ b/interpreter/src/vm/vm.c @@ -4,7 +4,22 @@ #include #include -#include +#if defined(__arm64__) + #include +#elif defined(__x86_64__) + #include +#else + #error "Unsupported architecture" +#endif + +// @todo(judah): this may need to change in the future. +#define __ovm_clz(v) __builtin_clz(v) +#define __ovm_clzll(v) __builtin_clzll(v) +#define __ovm_ctz(v) __builtin_ctz(v) +#define __ovm_ctzll(v) __builtin_ctzll(v) +#define __ovm_popcount(v) __builtin_popcount(v) +#define __ovm_popcountll(v) __builtin_popcount(v) + #include // REMOVE THIS!!! only needed for sqrt #include @@ -169,7 +184,7 @@ void ovm_engine_delete(ovm_engine_t *engine) { if (engine->memory) { munmap(engine->memory, engine->memory_size); } - + bh_free(store->heap_allocator, engine); } @@ -204,7 +219,9 @@ bool ovm_engine_memory_ensure_capacity(ovm_engine_t *engine, i64 minimum_size) { if (engine->memory == NULL) { new_addr = mmap(NULL, minimum_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); } else { - new_addr = mremap(engine->memory, engine->memory_size, minimum_size, MREMAP_MAYMOVE); + munmap(engine->memory, engine->memory_size); + new_addr = mmap(NULL, minimum_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + // new_addr = mremap(engine->memory, engine->memory_size, minimum_size, MREMAP_MAYMOVE); } if (new_addr == MAP_FAILED) { diff --git a/interpreter/src/vm/vm_instrs.h b/interpreter/src/vm/vm_instrs.h index a6c61585..be464ed4 100644 --- a/interpreter/src/vm/vm_instrs.h +++ b/interpreter/src/vm/vm_instrs.h @@ -156,12 +156,12 @@ OVM_OP_FLOAT_EXEC(copysign, __ovm_copysign) VAL(instr->r).type = t; \ VAL(instr->r).ctype = (ctype) op (VAL(instr->a).ctype); -OVMI_INSTR_EXEC(clz_i32) { OVM_OP(OVM_TYPE_I32, __builtin_clz, u32); NEXT_OP; } -OVMI_INSTR_EXEC(clz_i64) { OVM_OP(OVM_TYPE_I64, __builtin_clzll, u64); NEXT_OP; } -OVMI_INSTR_EXEC(ctz_i32) { OVM_OP(OVM_TYPE_I32, __builtin_ctz, u32); NEXT_OP; } -OVMI_INSTR_EXEC(ctz_i64) { OVM_OP(OVM_TYPE_I64, __builtin_ctzll, u64); NEXT_OP; } -OVMI_INSTR_EXEC(popcount_i32) { OVM_OP(OVM_TYPE_I32, __builtin_popcount, u32); NEXT_OP; } -OVMI_INSTR_EXEC(popcount_i64) { OVM_OP(OVM_TYPE_I64, __builtin_popcountll, u64); NEXT_OP; } +OVMI_INSTR_EXEC(clz_i32) { OVM_OP(OVM_TYPE_I32, __ovm_clz, u32); NEXT_OP; } +OVMI_INSTR_EXEC(clz_i64) { OVM_OP(OVM_TYPE_I64, __ovm_clzll, u64); NEXT_OP; } +OVMI_INSTR_EXEC(ctz_i32) { OVM_OP(OVM_TYPE_I32, __ovm_ctz, u32); NEXT_OP; } +OVMI_INSTR_EXEC(ctz_i64) { OVM_OP(OVM_TYPE_I64, __ovm_ctzll, u64); NEXT_OP; } +OVMI_INSTR_EXEC(popcount_i32) { OVM_OP(OVM_TYPE_I32, __ovm_popcount, u32); NEXT_OP; } +OVMI_INSTR_EXEC(popcount_i64) { OVM_OP(OVM_TYPE_I64, __ovm_popcountll, u64); NEXT_OP; } OVM_OP_FLOAT_EXEC(abs, __ovm_abs) OVM_OP_FLOAT_EXEC(neg, -) @@ -525,7 +525,7 @@ OVMI_INSTR_EXEC(mem_grow) { state->engine->memory_size + VAL(instr->a).u32 * 65536)) { VAL(instr->r).i32 = -1; } - + memory = state->engine->memory; NEXT_OP; } diff --git a/shared/include/bh.h b/shared/include/bh.h index 1aa32054..70e6bf08 100644 --- a/shared/include/bh.h +++ b/shared/include/bh.h @@ -304,8 +304,8 @@ BH_DEF BH_ALLOCATOR_PROC(bh_arena_allocator_proc); // ATOMIC ARENA ALLOCATOR -// Currently, this is only available on Linux, as it is using pthreads. -#ifdef _BH_LINUX +// Currently, this is only available on Linux/MacOS, as it is using pthreads. +#if defined(_BH_LINUX) || defined(_BH_DARWIN) typedef struct bh_atomic_arena { bh_allocator backing; @@ -1102,7 +1102,7 @@ BH_ALLOCATOR_PROC(bh_managed_heap_allocator_proc) { } bh_managed_heap__link *newptr = bh_heap_allocator_proc(NULL, action, size + sizeof(*old), alignment, old, flags); - + if (action == bh_allocator_action_alloc || action == bh_allocator_action_resize) { if (newptr) { newptr->magic_number = bh_managed_heap_magic_number; @@ -1152,7 +1152,7 @@ BH_DEF void bh_arena_clear(bh_arena* alloc) { trailer = walker; } } - + alloc->current_arena = alloc->first_arena; alloc->size = sizeof(ptr); } @@ -1226,7 +1226,7 @@ BH_ALLOCATOR_PROC(bh_arena_allocator_proc) { // ATOMIC ARENA ALLOCATOR IMPLEMENTATION -#ifdef _BH_LINUX +#if defined(_BH_LINUX) || defined(_BH_DARWIN) BH_DEF void bh_atomic_arena_init(bh_atomic_arena* alloc, bh_allocator backing, isize arena_size) { arena_size = bh_max(arena_size, size_of(ptr)); ptr data = bh_alloc(backing, arena_size); @@ -1510,7 +1510,7 @@ BH_DEF i64 leb128_to_int(u8* bytes, i32 *byte_count) { zero_shifted = zero_shifted << shift; return res | zero_shifted; } - + return res; } @@ -1950,12 +1950,12 @@ char* bh_path_get_full_name(char const* filename, bh_allocator a) { return result; #elif defined(_BH_LINUX) || defined (_BH_DARWIN) - char* p = realpath(filename, NULL); + char* p = realpath(filename, NULL); // Check if the file did not exists. // :Cleanup should this return NULL? if (p == NULL) return (char *) filename; - + i32 len = strlen(p); char* result = bh_alloc_array(a, char, len + 1); memmove(result, p, len); @@ -2169,7 +2169,7 @@ b32 bh_file_watch_wait(bh_file_watch *w) { (void) read(w->kill_pipe[0], &buf, sizeof(buf)); return 0; } - + FD_ZERO(&w->fds); FD_SET(w->inotify_fd, &w->fds); FD_SET(w->kill_pipe[0], &w->fds); @@ -3087,7 +3087,7 @@ u64 bh_time_duration(u64 old) { #if defined(_BH_WINDOWS) u64 curr = bh_time_curr(); u64 duration = curr - old; - + LARGE_INTEGER freq; QueryPerformanceFrequency(&freq); duration *= 1000; diff --git a/shared/lib/darwin_arm64/lib/libdyncall_s.a b/shared/lib/darwin_arm64/lib/libdyncall_s.a new file mode 100644 index 00000000..94945940 Binary files /dev/null and b/shared/lib/darwin_arm64/lib/libdyncall_s.a differ diff --git a/shared/lib/darwin_arm64/lib/libdyncallback_s.a b/shared/lib/darwin_arm64/lib/libdyncallback_s.a new file mode 100644 index 00000000..f6866f75 Binary files /dev/null and b/shared/lib/darwin_arm64/lib/libdyncallback_s.a differ diff --git a/shared/lib/darwin_arm64/lib/libovmwasm.a b/shared/lib/darwin_arm64/lib/libovmwasm.a new file mode 100644 index 00000000..8dc528a0 Binary files /dev/null and b/shared/lib/darwin_arm64/lib/libovmwasm.a differ diff --git a/shared/lib/darwin_x86_64/lib/libdyncall_s.a b/shared/lib/darwin_x86_64/lib/libdyncall_s.a new file mode 100644 index 00000000..0c8a92d0 Binary files /dev/null and b/shared/lib/darwin_x86_64/lib/libdyncall_s.a differ diff --git a/shared/lib/darwin_x86_64/lib/libdyncallback_s.a b/shared/lib/darwin_x86_64/lib/libdyncallback_s.a new file mode 100644 index 00000000..d700e466 Binary files /dev/null and b/shared/lib/darwin_x86_64/lib/libdyncallback_s.a differ diff --git a/shared/lib/darwin_x86_64/lib/libovmwasm.a b/shared/lib/darwin_x86_64/lib/libovmwasm.a new file mode 100644 index 00000000..32687601 Binary files /dev/null and b/shared/lib/darwin_x86_64/lib/libovmwasm.a differ