From ceeae865ac11854d2513418ad43cd63903a3767a Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Wed, 8 Dec 2021 12:46:12 -0600 Subject: [PATCH] added '#cstr' strings for easier c-strings --- build.sh | 2 +- include/astnodes.h | 3 ++- include/onyx_library.h | 2 +- modules/glfw3/module.onyx | 12 ++++++------ src/builtins.c | 7 +++++++ src/parser.c | 15 ++++++++++++++- src/symres.c | 14 +++++++++++--- src/wasm_emit.c | 11 +++++++---- 8 files changed, 49 insertions(+), 17 deletions(-) diff --git a/build.sh b/build.sh index e7a2bedb..5defd7c1 100755 --- a/build.sh +++ b/build.sh @@ -44,7 +44,7 @@ else fi C_FILES="$C_FILES wasm_runtime" -FLAGS="$FLAGS -DENABLE_RUN_WITH_WASMER -rdynamic" +FLAGS="$FLAGS -DENABLE_RUN_WITH_WASMER" LIBS="-L$CORE_DIR/lib -lwasmer -Wl,-rpath=$CORE_DIR/lib:./ -lpthread -ldl" INCLUDES="-I$WASMER_INCLUDE_DIR" diff --git a/include/astnodes.h b/include/astnodes.h index d31f57e8..fdf729c8 100644 --- a/include/astnodes.h +++ b/include/astnodes.h @@ -583,7 +583,7 @@ struct AstTyped { AstTyped_base; }; struct AstNamedValue { AstTyped_base; AstTyped* value; }; struct AstUnaryOp { AstTyped_base; UnaryOp operation; AstTyped *expr; }; struct AstNumLit { AstTyped_base; union { i32 i; i64 l; f32 f; f64 d; } value; }; -struct AstStrLit { AstTyped_base; u64 addr; u64 length; }; +struct AstStrLit { AstTyped_base; u64 addr; u64 length; b32 is_cstr: 1; }; struct AstLocal { AstTyped_base; }; struct AstDereference { AstTyped_base; AstTyped *expr; }; struct AstSizeOf { AstTyped_base; AstType *so_ast_type; Type *so_type; u64 size; }; @@ -1537,6 +1537,7 @@ extern AstGlobal builtin_stack_top; extern AstGlobal builtin_tls_base; extern AstGlobal builtin_tls_size; extern AstType *builtin_string_type; +extern AstType *builtin_cstring_type; extern AstType *builtin_range_type; extern Type *builtin_range_type_type; extern AstType *builtin_vararg_type; diff --git a/include/onyx_library.h b/include/onyx_library.h index 2c717573..c5bc9c22 100644 --- a/include/onyx_library.h +++ b/include/onyx_library.h @@ -24,7 +24,7 @@ typedef struct OnyxRuntime { char* (*wasm_memory_data)(wasm_memory_t *wasm_memory); wasm_extern_t* (*wasm_extern_lookup_by_name)(wasm_module_t* module, wasm_instance_t* instance, const char* name); wasm_func_t* (*wasm_extern_as_func)(wasm_extern_t* ext); - wasm_trap_t* (*wasm_func_call)(wasm_func_t* wasm_func, wasm_val_vec_t* args, wasm_val_vec_t* results); + wasm_trap_t* (*wasm_func_call)(const wasm_func_t* wasm_func, const wasm_val_vec_t* args, wasm_val_vec_t* results); } OnyxRuntime; OnyxRuntime* runtime; diff --git a/modules/glfw3/module.onyx b/modules/glfw3/module.onyx index f293f82c..f8922c0c 100644 --- a/modules/glfw3/module.onyx +++ b/modules/glfw3/module.onyx @@ -57,12 +57,12 @@ GLFWgammaramp :: struct { glfwDestroyCursor :: (cursor: GLFWcursor_p) -> void --- glfwSetCursor :: (window: GLFWwindow_p, cursor: GLFWcursor_p) -> void --- glfwSetKeyCallback :: (window: GLFWwindow_p, export_name: str) -> void --- - // glfwSetCharCallback - // glfwSetCharModsCallback - // glfwSetMouseButtonCallback - // glfwSetCursorPosCallback - // glfwSetCursorEnterCallback - // glfwSetScrollCallback + glfwSetCharCallback :: (window: GLFWwindow_p, export_name: str) -> void --- + glfwSetCharModsCallback :: (window: GLFWwindow_p, export_name: str) -> void --- + glfwSetMouseButtonCallback :: (window: GLFWwindow_p, export_name: str) -> void --- + glfwSetCursorPosCallback :: (window: GLFWwindow_p, export_name: str) -> void --- + glfwSetCursorEnterCallback :: (window: GLFWwindow_p, export_name: str) -> void --- + glfwSetScrollCallback :: (window: GLFWwindow_p, export_name: str) -> void --- // glfwSetDropCallback glfwJoystickPresent :: (jid: i32) -> i32 --- // glfwGetJoystickAxes :: (jid: i32, count: ^i32) -> ^f32 --- diff --git a/src/builtins.c b/src/builtins.c index 515c133f..b1ded77d 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -48,6 +48,7 @@ AstGlobal builtin_tls_base = { Ast_Kind_Global, 0, &builtin_tls_base_token, N AstGlobal builtin_tls_size = { Ast_Kind_Global, 0, &builtin_tls_size_token, NULL, NULL, (AstType *) &basic_type_u32, NULL }; AstType *builtin_string_type; +AstType *builtin_cstring_type; AstType *builtin_range_type; Type *builtin_range_type_type; AstType *builtin_vararg_type; @@ -386,6 +387,12 @@ void initialize_builtins(bh_allocator a) { return; } + builtin_cstring_type = (AstType *) symbol_raw_resolve(p->scope, "cstr"); + if (builtin_cstring_type == NULL) { + onyx_report_error((OnyxFilePos) { 0 }, "'cstr' type not found in builtin package."); + return; + } + builtin_range_type = (AstType *) symbol_raw_resolve(p->scope, "range"); if (builtin_range_type == NULL) { onyx_report_error((OnyxFilePos) { 0 }, "'range' struct not found in builtin package."); diff --git a/src/parser.c b/src/parser.c index c7d7a2e6..81cfee8f 100644 --- a/src/parser.c +++ b/src/parser.c @@ -665,7 +665,7 @@ static AstTyped* parse_factor(OnyxParser* parser) { assert(builtin_code_type != NULL); code_block->type_node = builtin_code_type; - + code_block->code = (AstNode *) parse_expression(parser, 0); expect_token(parser, ')'); @@ -681,6 +681,19 @@ static AstTyped* parse_factor(OnyxParser* parser) { retval = (AstTyped *) insert; break; } + else if (parse_possible_directive(parser, "cstr")) { + // Copy pasted from above. + AstStrLit* str_node = make_node(AstStrLit, Ast_Kind_StrLit); + str_node->token = expect_token(parser, Token_Type_Literal_String); + str_node->addr = 0; + str_node->flags |= Ast_Flag_Comptime; + str_node->is_cstr = 1; + + ENTITY_SUBMIT(str_node); + + retval = (AstTyped *) str_node; + break; + } onyx_report_error(parser->curr->pos, "Invalid directive in expression."); return NULL; diff --git a/src/symres.c b/src/symres.c index d58e802a..9c99f22a 100644 --- a/src/symres.c +++ b/src/symres.c @@ -476,10 +476,18 @@ static SymresStatus symres_expression(AstTyped** expr) { SYMRES(type, &(*expr)->type_node); break; - case Ast_Kind_StrLit: - SYMRES(type, &builtin_string_type); - (*expr)->type_node = builtin_string_type; + case Ast_Kind_StrLit: { + AstStrLit* str = (AstStrLit *) *expr; + if (str->is_cstr) { + SYMRES(type, &builtin_cstring_type); + str->type_node = builtin_cstring_type; + + } else { + SYMRES(type, &builtin_string_type); + str->type_node = builtin_string_type; + } break; + } case Ast_Kind_Slice: case Ast_Kind_Subscript: diff --git a/src/wasm_emit.c b/src/wasm_emit.c index d8cd5c68..7a4f2d11 100644 --- a/src/wasm_emit.c +++ b/src/wasm_emit.c @@ -2676,7 +2676,9 @@ EMIT_FUNC(expression, AstTyped* expr) { case Ast_Kind_StrLit: { WID(WI_PTR_CONST, ((AstStrLit *) expr)->addr); - WID(WI_I32_CONST, ((AstStrLit *) expr)->length); + + if (((AstStrLit *) expr)->is_cstr == 0) + WID(WI_I32_CONST, ((AstStrLit *) expr)->length); break; } @@ -3424,21 +3426,22 @@ static void emit_string_literal(OnyxWasmModule* mod, AstStrLit* strlit) { if (index != -1) { StrLitInfo sti = mod->string_literals[index].value; strlit->addr = sti.addr; - strlit->length = sti.len; + strlit->length = sti.len + (strlit->is_cstr ? 1 : 0); bh_free(global_heap_allocator, strdata); return; } + u32 actual_length = length + (strlit->is_cstr ? 1 : 0); WasmDatum datum = { .offset = mod->next_datum_offset, - .length = length, + .length = actual_length, .data = strdata, }; strlit->addr = (u32) mod->next_datum_offset, strlit->length = length; - mod->next_datum_offset += length; + mod->next_datum_offset += actual_length; shput(mod->string_literals, (char *) strdata, ((StrLitInfo) { strlit->addr, strlit->length })); -- 2.25.1