get libovmwasm to build on darwin (x86_64/arm64)
authorJudah Caruso <judah@tuta.io>
Mon, 4 Dec 2023 07:26:52 +0000 (00:26 -0700)
committerJudah Caruso <judah@tuta.io>
Mon, 4 Dec 2023 07:26:52 +0000 (00:26 -0700)
14 files changed:
build.sh
compiler/src/onyx.c
interpreter/build.sh
interpreter/include/ovm_debug.h
interpreter/include/vm.h
interpreter/src/vm/vm.c
interpreter/src/vm/vm_instrs.h
shared/include/bh.h
shared/lib/darwin_arm64/lib/libdyncall_s.a [new file with mode: 0644]
shared/lib/darwin_arm64/lib/libdyncallback_s.a [new file with mode: 0644]
shared/lib/darwin_arm64/lib/libovmwasm.a [new file with mode: 0644]
shared/lib/darwin_x86_64/lib/libdyncall_s.a [new file with mode: 0644]
shared/lib/darwin_x86_64/lib/libdyncallback_s.a [new file with mode: 0644]
shared/lib/darwin_x86_64/lib/libovmwasm.a [new file with mode: 0644]

index 871e42bb5eb30d9ff878879111e03fe85b9ce994..185df5ab1be07eb85a26ce84faa54a21c942fadc 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -1,5 +1,7 @@
 #!/bin/sh
 
+set -e
+
 DIST_DIR="./dist"
 
 compile_all() {
index a8ae029290962d1d732e1272d158e99d252328f6..ea72c96893d1283f574b0ddc34068870af07af1c 100644 (file)
@@ -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");
 }
 
index 289f6c8b5f572b3bff67d02dce91c6bc550d0166..86f12b9657c370d6e0b619a982b3e03efe482861 100755 (executable)
@@ -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"
index 1d872cc07287711aacd9d41622b4b5956f569eaa..366b659f4b5c1a73ddb960566d5986182cb39ec4 100644 (file)
@@ -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);
index 6ebaa4749242392b92eb013579d5715f8cd40db9..a9ff8f5791df34efbbf59d977f8d029cb7208020 100644 (file)
@@ -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
 
index 7f0495f91d8eb4a7bbe71e683fdb13e336425c54..147aa2f9c07bcda99b5e0bad7e8eae8335b48354 100644 (file)
@@ -4,7 +4,22 @@
 
 #include <sys/mman.h>
 #include <signal.h>
-#include <x86intrin.h>
+#if defined(__arm64__)
+    #include <arm_neon.h>
+#elif defined(__x86_64__)
+    #include <x86intrin.h>
+#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 <math.h> // REMOVE THIS!!!  only needed for sqrt
 #include <pthread.h>
 
@@ -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) {
index a6c615856010335d073ee207d80254fa25431cb7..be464ed4418f43649132582884e0697908882646 100644 (file)
@@ -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;
 }
index 1aa320542097197d5e28680fe9033c79321f5f4a..70e6bf08562be4a286f4fdd32c1092f7743315df 100644 (file)
@@ -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 (file)
index 0000000..9494594
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 (file)
index 0000000..f6866f7
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 (file)
index 0000000..8dc528a
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 (file)
index 0000000..0c8a92d
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 (file)
index 0000000..d700e46
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 (file)
index 0000000..3268760
Binary files /dev/null and b/shared/lib/darwin_x86_64/lib/libovmwasm.a differ