working towards Windows compilation
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 4 Jan 2021 01:45:14 +0000 (19:45 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 4 Jan 2021 01:45:14 +0000 (19:45 -0600)
bin/onyx
build.sh [new file with mode: 0644]
include/bh.h
include/onyxastnodes.h
src/onyx.c

index a49b7e574240b4e23a7eb14382048b80bf57533c..8e103c115496b2d9d4639f9cc2e9bae3decdc6b9 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
diff --git a/build.sh b/build.sh
new file mode 100644 (file)
index 0000000..69b090a
--- /dev/null
+++ b/build.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+C_FILES="onyx onyxbuiltins onyxchecker onyxclone onyxdoc onyxentities onyxerrors onyxlex onyxparser onyxsempass onyxsymres onyxtypes onyxutils onyxwasm"
+TARGET='./bin/onyx'
+CC='gcc'
+FLAGS='-O3 -I./include'
+BUILD_DIR='./build'
+
+mkdir -p '$BUILD_DIR'
+
+for file in $C_FILES ; do
+    echo "Compiling $file.c"
+    $CC -o build/$file.o $FLAGS -c src/$file.c
+done
+
+ALL_FILES="$(for file in $C_FILES ; do printf "$BUILD_DIR/%s.o " $file ; done)"
+echo "Linking $TARGET"
+$CC -o $TARGET $FLAGS $ALL_FILES
+
+
+CORE_DIR='/usr/share/onyx'
+sudo mkdir -p "$CORE_DIR"
+echo "Installing core libs"
+sudo cp -r ./core/ "$CORE_DIR"
+
+
+# Otherwise the prompt ends on the same line
+printf "\n"
\ No newline at end of file
index 1e39372eb587f1a66c9199bcc607c9690a49bd93..60687e9903c2b914420853a1572d6dbb3a1240aa 100644 (file)
@@ -5,17 +5,21 @@
 #define _LARGEFILE64_SOURCE
 
 #include <sys/stat.h>
-#include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <malloc.h>
 #include <time.h>
 
+#ifdef __unix__
+    #include <unistd.h>
+#endif
+
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h> // TODO: Replace with needed functions
 #include <stdint.h>
 #include <assert.h>
+#include <stdio.h>
 
 //-------------------------------------------------------------------------------------
 // Better types
@@ -421,7 +425,7 @@ isize bh_snprintf_va(char *str, isize n, char const *fmt, va_list va);
 #ifdef BH_DEBUG
 
 void* bh__debug_malloc(size_t size, const char* file, u64 line);
-void* bh__debug_memalign(size_t alignment, size_t size, const char* file, u64 line);
+i32   bh__debug_posix_memalign(void** ret, size_t alignment, size_t size, const char* file, u64 line);
 void  bh__debug_free(void* ptr, const char* file, u64 line);
 void* bh__debug_realloc(void* ptr, size_t size, const char* file, u64 line);
 
@@ -433,10 +437,10 @@ void* bh__debug_malloc(size_t size, const char* file, u64 line) {
     return p;
 }
 
-void* bh__debug_memalign(size_t alignment, size_t size, const char* file, u64 line) {
-    void* p = memalign(alignment, size);
-    bh_printf("[DEBUG] %p = memalign(%d, %d) at %s:%d\n", p, alignment, size, file, line);
-    return p;
+i32 bh__debug_posix_memalign(void** ret, size_t alignment, size_t size, const char* file, u64 line) {
+    i32 success = posix_memalign(ret, alignment, size);
+    bh_printf("[DEBUG] %p = posix_memalign(%d, %d) at %s:%d\n", *ret, alignment, size, file, line);
+    return success;
 }
 
 void bh__debug_free(void* ptr, const char* file, u64 line) {
@@ -452,10 +456,10 @@ void* bh__debug_realloc(void* ptr, size_t size, const char* file, u64 line) {
 
 #endif
 
-#define malloc(size)                    (bh__debug_malloc(size, __FILE__, __LINE__))
-#define memalign(alignment, size)    (bh__debug_memalign(alignment, size, __FILE__, __LINE__))
-#define free(ptr)                        (bh__debug_free(ptr, __FILE__, __LINE__))
-#define realloc(ptr, size)                (bh__debug_realloc(ptr, size, __FILE__, __LINE__))
+#define malloc(size)                            (bh__debug_malloc(size, __FILE__, __LINE__))
+#define posix_memalign(ret, alignment, size)    (bh__debug_posix_memalign(ret, alignment, size, __FILE__, __LINE__))
+#define free(ptr)                               (bh__debug_free(ptr, __FILE__, __LINE__))
+#define realloc(ptr, size)                      (bh__debug_realloc(ptr, size, __FILE__, __LINE__))
 
 #endif
 
@@ -876,7 +880,7 @@ BH_ALLOCATOR_PROC(bh_heap_allocator_proc) {
 
     switch (action) {
     case bh_allocator_action_alloc: {
-        retval = memalign(alignment, size);
+        i32 success = posix_memalign(&retval, alignment, size);
 
         if (flags & bh_allocator_flag_clear && retval != NULL) {
             memset(retval, 0, size);
@@ -927,7 +931,7 @@ BH_ALLOCATOR_PROC(bh_managed_heap_allocator_proc) {
 
     switch (action) {
     case bh_allocator_action_alloc: {
-        retval = memalign(alignment, size);
+        i32 success = posix_memalign(&retval, alignment, size);
 
         if (flags & bh_allocator_flag_clear && retval != NULL) {
             memset(retval, 0, size);
@@ -1050,7 +1054,7 @@ void bh_scratch_init(bh_scratch* scratch, bh_allocator backing, isize scratch_si
     scratch->backing = backing;
     scratch->memory = memory;
     scratch->curr = memory;
-    scratch->end = memory + scratch_size;
+    scratch->end = bh_pointer_add(memory, scratch_size);
 }
 
 void bh_scratch_free(bh_scratch* scratch) {
@@ -1074,12 +1078,12 @@ BH_ALLOCATOR_PROC(bh_scratch_allocator_proc) {
 
     switch (action) {
     case bh_allocator_action_alloc: {
-        if (size > scratch->end - scratch->memory) {
+        if (size > ((u8 *) scratch->end) - ((u8 *) scratch->memory)) {
             return NULL;
         }
 
         retval = scratch->curr;
-        scratch->curr += size;
+        scratch->curr = bh_pointer_add(scratch->curr, size);
 
         if (scratch->curr >= scratch->end) {
             scratch->curr = scratch->memory;
@@ -1090,12 +1094,12 @@ BH_ALLOCATOR_PROC(bh_scratch_allocator_proc) {
     case bh_allocator_action_free: break;
 
     case bh_allocator_action_resize: {
-        if (size > scratch->end - scratch->memory) {
+        if (size > ((u8 *) scratch->end) - ((u8 *) scratch->memory)) {
             return NULL;
         }
 
         retval = scratch->curr;
-        scratch->curr += size;
+        scratch->curr = bh_pointer_add(scratch->curr, size);
 
         if (scratch->curr >= scratch->end) {
             scratch->curr = scratch->memory;
index 5c319b368eeb9f83c4a6cc004d247f3c98e75bc6..315ab55b7f910fcbdc1f986d102344013e02d037 100644 (file)
@@ -426,8 +426,8 @@ typedef enum VarArgKind {
     AstKind kind;             \
     u32 flags;                \
     OnyxToken *token;         \
-    AstNode *next;
-struct AstNode { AstNode_base };
+    AstNode *next
+struct AstNode { AstNode_base; };
 
 // NOTE: 'type_node' is filled out by the parser.
 // For a type such as '^^i32', the tree would look something like
@@ -443,8 +443,8 @@ struct AstNode { AstNode_base };
 #define AstTyped_base       \
     AstNode_base;           \
     AstType *type_node;     \
-    Type *type;
-struct AstTyped { AstTyped_base };
+    Type *type
+struct AstTyped { AstTyped_base; };
 
 // Expression Nodes
 struct AstBinaryOp      { AstTyped_base; BinaryOp operation; AstTyped *left, *right; };
@@ -605,8 +605,8 @@ struct AstSwitch {
     AstKind kind;       \
     u32 flags;          \
     OnyxToken* token;   \
-    char* name;
-struct AstType { AstType_base };
+    char* name
+struct AstType { AstType_base; };
 
 struct AstBasicType     { AstType_base; Type* type; };
 struct AstPointerType   { AstType_base; AstType* elem; };
index 1b015aa1b79980a3b4a124fea097f3eb6791e244..a300ba85bce05c76c6e563cff66281f2efd383fa 100644 (file)
@@ -16,6 +16,8 @@
 #ifndef CORE_INSTALLATION
     #ifdef __unix__
     #define CORE_INSTALLATION "/usr/share/onyx"
+    #elif _WIN32
+    #define CORE_INSTALLATION "C:\\Program Files\\Onyx"
     #endif
 #endif
 
@@ -224,9 +226,15 @@ static char* lookup_included_file(CompilerState* cs, char* filename) {
         bh_snprintf(fn, 128, "%s", filename);
     }
 
+#ifdef __unix__
+    #define DIR_SEPARATOR '/'
+#elif _WIN32
+    #define DIR_SEPARATOR '\\'
+#endif
+
     bh_arr_each(const char *, folder, cs->options->included_folders) {
-        if ((*folder)[strlen(*folder) - 1] != '/')
-            bh_snprintf(path, 256, "%s/%s", *folder, fn);
+        if ((*folder)[strlen(*folder) - 1] != DIR_SEPARATOR)
+            bh_snprintf(path, 256, "%s%c%s", *folder, DIR_SEPARATOR, fn);
         else
             bh_snprintf(path, 256, "%s%s", *folder, fn);
 
@@ -234,6 +242,8 @@ static char* lookup_included_file(CompilerState* cs, char* filename) {
     }
 
     return fn;
+
+#undef DIR_SEPARATOR
 }
 
 static ParseResults parse_source_file(CompilerState* compiler_state, bh_file_contents* file_contents) {