From: Brendan Hansen Date: Fri, 12 Jun 2020 03:44:13 +0000 (-0500) Subject: Added flexible buffer implementation X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=6c104d7b2db23648554e15d0cccca21e5537e1fe;p=onyx.git Added flexible buffer implementation --- diff --git a/include/bh.h b/include/bh.h index dc8a93a3..9424690c 100644 --- a/include/bh.h +++ b/include/bh.h @@ -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 1be388ef..552631e5 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/minimal.onyx b/progs/minimal.onyx index 1356f4ba..215f6fdf 100644 --- a/progs/minimal.onyx +++ b/progs/minimal.onyx @@ -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; } diff --git a/src/onyx.c b/src/onyx.c index f986faca..73eef378 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -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"); diff --git a/src/onyxwasm.c b/src/onyxwasm.c index d19909e5..a410d635 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -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); }