From: Brendan Hansen Date: Mon, 10 Aug 2020 15:27:14 +0000 (-0500) Subject: string literal data section improvement X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=3761036d68086fd0df27c3528ced7b1fdf1e6d27;p=onyx.git string literal data section improvement --- diff --git a/include/onyxwasm.h b/include/onyxwasm.h index e0b74e71..3ee2c499 100644 --- a/include/onyxwasm.h +++ b/include/onyxwasm.h @@ -312,6 +312,7 @@ typedef struct OnyxWasmModule { bh_table(i32) type_map; bh_table(u32) loaded_file_offsets; + bh_table(u32) string_literals; bh_arr(u8) structured_jump_target; diff --git a/onyx b/onyx index 75bf841a..bedfe214 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/print_funcs.onyx b/progs/print_funcs.onyx index 8abf5672..a9a63b57 100644 --- a/progs/print_funcs.onyx +++ b/progs/print_funcs.onyx @@ -59,22 +59,24 @@ print_u64_with_base :: proc (n_: u64, base: u64) { m :: n % base; ch := cast(u8) 0; - if m == 0l do ch = #char "0"; - if m == 1l do ch = #char "1"; - if m == 2l do ch = #char "2"; - if m == 3l do ch = #char "3"; - if m == 4l do ch = #char "4"; - if m == 5l do ch = #char "5"; - if m == 6l do ch = #char "6"; - if m == 7l do ch = #char "7"; - if m == 8l do ch = #char "8"; - if m == 9l do ch = #char "9"; - if m == 10l do ch = #char "A"; - if m == 11l do ch = #char "B"; - if m == 12l do ch = #char "C"; - if m == 13l do ch = #char "D"; - if m == 14l do ch = #char "E"; - if m == 15l do ch = #char "F"; + + // TODO: Replace with array lookup when array literals are added + if m == 0l do ch = #char "0"; + elseif m == 1l do ch = #char "1"; + elseif m == 2l do ch = #char "2"; + elseif m == 3l do ch = #char "3"; + elseif m == 4l do ch = #char "4"; + elseif m == 5l do ch = #char "5"; + elseif m == 6l do ch = #char "6"; + elseif m == 7l do ch = #char "7"; + elseif m == 8l do ch = #char "8"; + elseif m == 9l do ch = #char "9"; + elseif m == 10l do ch = #char "A"; + elseif m == 11l do ch = #char "B"; + elseif m == 12l do ch = #char "C"; + elseif m == 13l do ch = #char "D"; + elseif m == 14l do ch = #char "E"; + elseif m == 15l do ch = #char "F"; *c = ch; c -= 1; diff --git a/progs/stack_based.onyx b/progs/stack_based.onyx index b8f409ec..d3a048c1 100644 --- a/progs/stack_based.onyx +++ b/progs/stack_based.onyx @@ -47,6 +47,7 @@ vec_add :: proc (v: Vec3, u: Vec3, use out: ^Vec3) { start :: proc #export { heap_init(); print("Hello, World!"); + print("Hello, World!"); print_ptr(__heap_start); print_ptr(__stack_base); print_ptr(__stack_top); diff --git a/src/onyxchecker.c b/src/onyxchecker.c index ac964dca..3fb2967e 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -504,6 +504,20 @@ CHECK(binaryop, AstBinaryOp* binop, b32 assignment_is_ok) { || binop->operation == Binary_Op_Bool_Or) return check_binaryop_bool(binop); + if (binop->left->type == NULL) { + onyx_message_add(Msg_Type_Unresolved_Type, + binop->token->pos, + binop->left->token->text, binop->left->token->length); + return 1; + } + + if (binop->right->type == NULL) { + onyx_message_add(Msg_Type_Unresolved_Type, + binop->token->pos, + binop->right->token->text, binop->right->token->length); + return 1; + } + if (!type_is_numeric(binop->left->type) && !type_is_pointer(binop->left->type)) { onyx_message_add(Msg_Type_Literal, binop->token->pos, @@ -542,20 +556,6 @@ CHECK(binaryop, AstBinaryOp* binop, b32 assignment_is_ok) { return 1; } - if (binop->left->type == NULL) { - onyx_message_add(Msg_Type_Unresolved_Type, - binop->token->pos, - binop->left->token->text, binop->left->token->length); - return 1; - } - - if (binop->right->type == NULL) { - onyx_message_add(Msg_Type_Unresolved_Type, - binop->token->pos, - binop->right->token->text, binop->right->token->length); - return 1; - } - if (lptr) { if (!type_is_integer(binop->right->type)) { onyx_message_add(Msg_Type_Literal, diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 75fc778b..dc821569 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -1624,6 +1624,11 @@ static void compile_string_literal(OnyxWasmModule* mod, AstStrLit* strlit) { } *des++ = '\0'; + if (bh_table_has(u32, mod->string_literals, (char *) strdata)) { + strlit->addr = bh_table_get(u32, mod->string_literals, (char *) strdata); + return; + } + u32 length = (u32) (des - strdata); WasmDatum datum = { @@ -1635,6 +1640,8 @@ static void compile_string_literal(OnyxWasmModule* mod, AstStrLit* strlit) { strlit->addr = (u32) mod->next_datum_offset, mod->next_datum_offset += length; + bh_table_put(u32, mod->string_literals, (char *) strdata, strlit->addr); + bh_arr_push(mod->data, datum); } @@ -1748,6 +1755,7 @@ OnyxWasmModule onyx_wasm_module_create(bh_allocator alloc) { bh_table_init(global_heap_allocator, module.type_map, 61); bh_table_init(global_heap_allocator, module.exports, 61); bh_table_init(global_heap_allocator, module.loaded_file_offsets, 7); + bh_table_init(global_heap_allocator, module.string_literals, 16); bh_imap_init(&module.index_map, global_heap_allocator, 128); bh_imap_init(&module.local_map, global_heap_allocator, 16);