#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
+
+//-------------------------------------------------------------------------------------
+// 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);
+
+
+
+
+
+
+
+
+
+
+
+
+
//-------------------------------------------------------------------------------------
+//-------------------------------------------------------------------------------------
+// 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
+
+
+
+
+
+
+
+
+
+
+
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;
}
OnyxWasmModule wasm_mod = onyx_wasm_generate_module(alloc, program);
-#if 1
+#if 0
// NOTE: Ensure type table made correctly
bh_printf("Type map:\n");
}
#endif
-#if 1
+#if 0
// NOTE: Ensure the export table was built correctly
bh_printf("Function types:\n");
+#define BH_DEBUG
#include "onyxwasm.h"
// NOTE: Allows easier testing of types since most of the characters
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);
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) {
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);
}
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) {
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);
}