From d9791cc0a2c35c1159619c269d950392db852e9d Mon Sep 17 00:00:00 2001 From: servostar Date: Sun, 21 Jul 2024 17:02:16 +0200 Subject: [PATCH] fixed string literals --- src/llvm/llvm-ir/types.c | 6 +++++- src/llvm/llvm-ir/variables.c | 11 ++++++----- tests/hello_world/main.gsc | 3 +-- tests/stdlib/src/echo.gsc | 4 +--- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/llvm/llvm-ir/types.c b/src/llvm/llvm-ir/types.c index e791bc5..d5d2efa 100644 --- a/src/llvm/llvm-ir/types.c +++ b/src/llvm/llvm-ir/types.c @@ -55,7 +55,11 @@ BackendError impl_reference_const(LLVMBackendCompileUnit* unit, TypeValue* value LLVMSetUnnamedAddress(string_global, LLVMGlobalUnnamedAddr); LLVMSetAlignment(string_global, 1); - *llvm_value = string_global; + // Cast the global variable to a pointer type if needed + LLVMTypeRef i8_ptr_type = LLVMPointerType(LLVMInt8TypeInContext(unit->context), 0); + LLVMValueRef global_str_ptr = LLVMConstBitCast(string_global, i8_ptr_type); + + *llvm_value = global_str_ptr; } else { err = new_backend_impl_error(Implementation, value->nodePtr, "reference initializer can only be string literals"); } diff --git a/src/llvm/llvm-ir/variables.c b/src/llvm/llvm-ir/variables.c index f5bac19..2765fed 100644 --- a/src/llvm/llvm-ir/variables.c +++ b/src/llvm/llvm-ir/variables.c @@ -1,11 +1,13 @@ #include -#include -#include -#include #include #include #include +#include +#include +#include + +#include "expr.h" BackendError impl_global_declaration(LLVMBackendCompileUnit* unit, LLVMGlobalScope* scope, @@ -54,8 +56,7 @@ BackendError impl_global_definiton(LLVMBackendCompileUnit* unit, // FIXME: resolve initializer expression! LLVMValueRef initial_value = NULL; - err = get_type_default_value(unit, scope, def->declaration.type, - &initial_value); + err = get_const_type_value(unit, scope, &def->initializer->impl.constant, &initial_value); if (err.kind == Success) { DEBUG("setting default value"); diff --git a/tests/hello_world/main.gsc b/tests/hello_world/main.gsc index 70314a2..843f6e4 100644 --- a/tests/hello_world/main.gsc +++ b/tests/hello_world/main.gsc @@ -23,6 +23,5 @@ fun printcstr(in cstr: msg) { } fun main() { - cstr: msg = "Hello, world!\n" - printcstr(msg) + printcstr("Hello, world!\n") } diff --git a/tests/stdlib/src/echo.gsc b/tests/stdlib/src/echo.gsc index 8b0095b..9253222 100644 --- a/tests/stdlib/src/echo.gsc +++ b/tests/stdlib/src/echo.gsc @@ -3,7 +3,7 @@ import "std" cstr: EOL = "\n" -fun main(out u32: e) { +fun main() { handle: stdin = nullHandle getStdinHandle(stdin) @@ -22,6 +22,4 @@ fun main(out u32: e) { writeBytes(stdout, EOL, 1)(bytesWritten) heapFree(buffer) - - e = 0 as u32 }