Added flexible buffer implementation
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 12 Jun 2020 03:44:13 +0000 (22:44 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 12 Jun 2020 03:44:13 +0000 (22:44 -0500)
include/bh.h
onyx
progs/minimal.onyx
src/onyx.c
src/onyxwasm.c

index dc8a93a3364b4aad36886c7e7e823656a86dea14..9424690c27618da2ba4d02230a856a456f8d5a1f 100644 (file)
@@ -115,6 +115,12 @@ inline i64 chars_match(char* ptr1, char* ptr2) {
 #define fori(var, lo, hi)                              for (i64 var = (lo); var <= (hi); var++)
 #define forll(T, var, start, step)             for (T* var = (start); var != NULL; var = var->step)
 
+#ifdef BH_DEBUG
+       #define DEBUG_HERE                                              __asm("int $3")
+#else
+       #define DEBUG_HERE
+#endif
+
 
 
 
@@ -401,6 +407,37 @@ void* bh__debug_realloc(void* ptr, size_t size, const char* file, u64 line) {
 
 
 
+//-------------------------------------------------------------------------------------
+// Flexible buffer
+//-------------------------------------------------------------------------------------
+
+typedef struct bh_buffer {
+       bh_allocator allocator;
+       i32 length, capacity;
+       ptr data;
+} bh_buffer;
+
+#ifndef BH_BUFFER_GROW_FORMULA
+#define BH_BUFFER_GROW_FORMULA(x)                      ((x) > 0 ? ((x) << 1) : 16)
+#endif
+
+void bh_buffer_init(bh_buffer* buffer, bh_allocator alloc, i32 length);
+void bh_buffer_grow(bh_buffer* buffer, i32 length);
+void bh_buffer_append(bh_buffer* buffer, ptr data, i32 length);
+void bh_buffer_concat(bh_buffer* buffer, bh_buffer other);
+
+
+
+
+
+
+
+
+
+
+
+
+
 
 
 //-------------------------------------------------------------------------------------
@@ -1286,6 +1323,67 @@ end_of_format:
 
 
 
+//-------------------------------------------------------------------------------------
+// FLEXIBLE BUFFER IMPLEMENTATION
+//-------------------------------------------------------------------------------------
+#ifndef BH_NO_BUFFER
+
+void bh_buffer_init(bh_buffer* buffer, bh_allocator alloc, i32 init_size) {
+       buffer->allocator = alloc;
+       buffer->length = 0;
+       buffer->capacity = init_size;
+       buffer->data = bh_alloc(alloc, init_size);
+}
+
+void bh_buffer_grow(bh_buffer* buffer, i32 length) {
+       if (buffer == NULL) return;
+
+       if (buffer->capacity >= length) {
+               // NOTE: Already have enough room
+               return;
+       }
+
+       i32 newcap = buffer->capacity;
+       while (newcap < length) newcap = BH_BUFFER_GROW_FORMULA(newcap);
+
+       ptr new_data = bh_resize(buffer->allocator, buffer->data, newcap);
+       if (new_data == NULL) return;
+
+       buffer->capacity = newcap;
+       buffer->data = new_data;
+}
+
+void bh_buffer_append(bh_buffer* buffer, ptr data, i32 length) {
+       if (buffer == NULL) return;
+
+       if (buffer->length + length > buffer->capacity) {
+               bh_buffer_grow(buffer, buffer->length + length);
+       }
+
+       memcpy(bh_pointer_add(buffer->data, buffer->length), data, length);
+       buffer->length += length;
+}
+
+void bh_buffer_concat(bh_buffer* buffer, bh_buffer other) {
+       bh_buffer_append(buffer, other.data, other.length);
+}
+
+
+
+
+
+#endif
+
+
+
+
+
+
+
+
+
+
+
 
 
 
diff --git a/onyx b/onyx
index 1be388ef9e86b7705ff8d8bc501ddb7d0dcf39f9..552631e52fb05489e67e55946bd09f677b6a05eb 100755 (executable)
Binary files a/onyx and b/onyx differ
index 1356f4ba2b6b1a68cb52ef06e30935a161c0ece0..215f6fdfdfbcb9e723825ad62dd5dc48f64516f4 100644 (file)
@@ -7,12 +7,14 @@ export foo :: proc (foo i32, bar i32) -> i32 {
        return 10 as i32;
 }
 
-export diff_square :: proc (a i64, b i32) -> i64 {
+export diff_square :: proc (a i32, b i32) -> i64 {
        // Typechecked
-       c := a - (b as i64); // Mutable
-       d :: (a as i32) + b; // Constant
+       c := a - b; // Mutable
+       d :: a + b; // Constant
 
-       { f :: 10 as i32; };
+       {
+               f :: 10;
+       };
 
-       return (c * (d as i64));
+       return (c * d) as i64;
 }
index f986facaea23d942008770f622b2b7060078bd56..73eef378c977e16739ac7bc982d85d35699f358f 100644 (file)
@@ -62,7 +62,7 @@ int main(int argc, char *argv[]) {
 
        OnyxWasmModule wasm_mod = onyx_wasm_generate_module(alloc, program);
 
-#if 1
+#if 0
        // NOTE: Ensure type table made correctly
 
        bh_printf("Type map:\n");
@@ -83,7 +83,7 @@ int main(int argc, char *argv[]) {
        }
 #endif
 
-#if 1
+#if 0
        // NOTE: Ensure the export table was built correctly
 
        bh_printf("Function types:\n");
index d19909e5a59b2a7f2b14156e071b7c571fc41850..a410d635f7b58826064f2508814757c8301e9a0e 100644 (file)
@@ -1,3 +1,4 @@
+#define BH_DEBUG
 #include "onyxwasm.h"
 
 // NOTE: Allows easier testing of types since most of the characters
@@ -206,7 +207,8 @@ static WasmType onyx_type_to_wasm_type(OnyxTypeInfo* type) {
        return WASM_TYPE_INT32;
 }
 
-static void process_block(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNodeBlock* block, b32 top_level);
+static void process_function_body(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNodeFuncDef* fd);
+static void process_block(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNodeBlock* block);
 static void process_statement(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNode* stmt);
 static void process_assign_lval(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNode* lval);
 static void process_assignment(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNode* assign);
@@ -214,18 +216,22 @@ static void process_expression(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNode*
 static void process_cast(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNode* cast);
 static void process_return(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNode* ret);
 
-static void process_block(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNodeBlock* block, b32 top_level) {
-       if (!top_level) {
-               bh_arr_push(func->code, ((WasmInstruction){ WI_BLOCK_START, 0x40 }));
+static void process_function_body(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNodeFuncDef* fd) {
+       if (fd->body == NULL) return;
+
+       forll (OnyxAstNode, stmt, fd->body->body, next) {
+               process_statement(mod, func, stmt);
        }
+}
+
+static void process_block(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNodeBlock* block) {
+       bh_arr_push(func->code, ((WasmInstruction){ WI_BLOCK_START, 0x40 }));
 
        forll (OnyxAstNode, stmt, block->body, next) {
                process_statement(mod, func, stmt);
        }
 
-       if (!top_level) {
-               bh_arr_push(func->code, ((WasmInstruction){ WI_BLOCK_END, 0x00 }));
-       }
+       bh_arr_push(func->code, ((WasmInstruction){ WI_BLOCK_END, 0x00 }));
 }
 
 static void process_statement(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNode* stmt) {
@@ -343,9 +349,10 @@ static void process_expression(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNode*
                                break;
                        }
 
-               case ONYX_AST_NODE_KIND_BLOCK: process_block(mod, func, (OnyxAstNodeBlock *) expr, 0); break;
+               case ONYX_AST_NODE_KIND_BLOCK: process_block(mod, func, (OnyxAstNodeBlock *) expr); break;
 
                default:
+                       DEBUG_HERE;
                        bh_printf("Unhandled case: %d\n", expr->kind);
                        assert(0);
        }
@@ -483,7 +490,7 @@ static void process_function_definition(OnyxWasmModule* mod, OnyxAstNodeFuncDef*
        bh_hash_each_end;
 
        // Generate code
-       process_block(mod, &wasm_func, fd->body, 1);
+       process_function_body(mod, &wasm_func, fd);
 
        bh_printf("Code for function:\n");
        bh_arr_each(WasmInstruction, instr, wasm_func.code) {
@@ -533,4 +540,7 @@ OnyxWasmModule onyx_wasm_generate_module(bh_allocator alloc, OnyxAstNode* progra
 void onyx_wasm_module_free(OnyxWasmModule* module) {
        bh_arr_free(module->functypes);
        bh_arr_free(module->funcs);
+       bh_hash_free(module->local_map);
+       bh_hash_free(module->type_map);
+       bh_hash_free(module->exports);
 }