From c4c422d89970b3eba86eb4672727eb91f7e2a4a4 Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 18 Jul 2024 19:55:17 +0200 Subject: [PATCH] added library binary resolving --- lib/CMakeLists.txt | 12 +++++++++--- lib/src/entry/entrypoint.c | 3 --- lib/src/io.gsc | 2 +- lib/src/mem.gsc | 2 +- lib/src/os.gsc | 2 +- lib/src/std.gsc | 6 +++--- src/ast/ast.c | 1 + src/cfg/opt.c | 30 ++++++++++++++++++++++++++++++ src/compiler.c | 6 +++++- src/llvm/link/lld.c | 12 ++++-------- src/set/set.c | 6 ++++++ src/set/types.h | 1 + 12 files changed, 62 insertions(+), 21 deletions(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 8be6998..097546c 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -16,10 +16,16 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/../bin/std") # add native module libraries file(GLOB_RECURSE STDLIB_IO_SOURCE_FILES src/io/*.c) -add_library(io ${STDLIB_IO_SOURCE_FILES}) +add_library(gscio ${STDLIB_IO_SOURCE_FILES}) file(GLOB_RECURSE STDLIB_MEM_SOURCE_FILES src/mem/*.c) -add_library(mem ${STDLIB_MEM_SOURCE_FILES}) +add_library(gscmem ${STDLIB_MEM_SOURCE_FILES}) file(GLOB_RECURSE STDLIB_OS_SOURCE_FILES src/os/*.c) -add_library(os ${STDLIB_OS_SOURCE_FILES}) +add_library(gscos ${STDLIB_OS_SOURCE_FILES}) + +# Complete standard library +add_library(gscstd + ${STDLIB_IO_SOURCE_FILES} + ${STDLIB_MEM_SOURCE_FILES} + ${STDLIB_OS_SOURCE_FILES}) diff --git a/lib/src/entry/entrypoint.c b/lib/src/entry/entrypoint.c index c41e482..75cf927 100644 --- a/lib/src/entry/entrypoint.c +++ b/lib/src/entry/entrypoint.c @@ -2,9 +2,6 @@ // Created by servostar on 6/10/24. // -extern void entry(void); - int main(int argc, char* argv[]) { - entry(); return 0; } diff --git a/lib/src/io.gsc b/lib/src/io.gsc index db8de9b..c729644 100644 --- a/lib/src/io.gsc +++ b/lib/src/io.gsc @@ -6,7 +6,7 @@ # | Generic Input/Output | # `----------------------------------------` -import "def.gsc" +include "def.gsc" # platform specific handle to an I/O device # can be a file, buffer, window or something else diff --git a/lib/src/mem.gsc b/lib/src/mem.gsc index 0307666..b9df686 100644 --- a/lib/src/mem.gsc +++ b/lib/src/mem.gsc @@ -6,7 +6,7 @@ # | Memory Management | # `----------------------------------------` -import "def.gsc" +include "def.gsc" # Allocate `len` bytes of heap memory # Returns a pointer to the memory as `ptr` diff --git a/lib/src/os.gsc b/lib/src/os.gsc index bfd6958..5f7a190 100644 --- a/lib/src/os.gsc +++ b/lib/src/os.gsc @@ -6,7 +6,7 @@ # | Operating System | # `----------------------------------------` -import "def.gsc" +include "def.gsc" # Return a hard coded C string identifying the underlying operating system # Will return one of the following: diff --git a/lib/src/std.gsc b/lib/src/std.gsc index cf690e3..36d6a75 100644 --- a/lib/src/std.gsc +++ b/lib/src/std.gsc @@ -7,10 +7,10 @@ # `----------------------------------------` # standard type definitions -import "def.gsc" +include "def.gsc" # I/O operations -import "io.gsc" +include "io.gsc" # memory management -import "mem.gsc" +include "mem.gsc" diff --git a/src/ast/ast.c b/src/ast/ast.c index 7da6bf5..9f00f56 100644 --- a/src/ast/ast.c +++ b/src/ast/ast.c @@ -101,6 +101,7 @@ const char* AST_node_to_string(const struct AST_Node_t* node) { case AST_Ident: case AST_Macro: case AST_Import: + case AST_Include: case AST_Storage: case AST_Typekind: case AST_Sign: diff --git a/src/cfg/opt.c b/src/cfg/opt.c index 57ed113..22f0e98 100644 --- a/src/cfg/opt.c +++ b/src/cfg/opt.c @@ -212,6 +212,36 @@ TargetConfig* default_target_config_from_args() { char* default_import_path = mem_strdup(MemoryNamespaceOpt, "."); g_array_append_val(config->import_paths, default_import_path); + if (is_option_set("import-paths")) { + const Option* opt = get_option("import-paths"); + + if (opt->value != NULL) { + + const char* start = opt->value; + const char* end = NULL; + while((end = strchr(start, ',')) != NULL) { + + const int len = end - start; + char* import_path = malloc(len + 1); + memcpy(import_path, start, len); + import_path[len] = 0; + + g_array_append_val(config->import_paths, import_path); + + start = end; + } + + const int len = strlen(start); + if (len > 0) { + char* import_path = malloc(len + 1); + memcpy(import_path, start, len); + import_path[len] = 0; + + g_array_append_val(config->import_paths, import_path); + } + } + } + return config; } diff --git a/src/compiler.c b/src/compiler.c index 289daf7..7e85543 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -170,6 +170,10 @@ static void run_backend_codegen(const Module* module, const TargetConfig* target const char* get_absolute_import_path(const TargetConfig* config, const char* import_target_name) { INFO("resolving absolute path for import target: %s", import_target_name); + if (!g_str_has_suffix(import_target_name, ".gsc")) { + import_target_name = g_strjoin("", import_target_name, ".gsc", NULL); + } + for (guint i = 0; i < config->import_paths->len; i++) { const char* import_directory_path = g_array_index(config->import_paths, char*, i); @@ -204,7 +208,7 @@ static int compile_module_with_dependencies(ModuleFileStack *unit, ModuleFile* f for (size_t i = 0; i < AST_get_child_count(root_module); i++) { AST_NODE_PTR child = AST_get_node(root_module, i); - if (child->kind == AST_Import) { + if (child->kind == AST_Import || child->kind == AST_Include) { const char* path = get_absolute_import_path(target, child->value); if (path == NULL) { diff --git a/src/llvm/link/lld.c b/src/llvm/link/lld.c index e1a2175..687dd3a 100644 --- a/src/llvm/link/lld.c +++ b/src/llvm/link/lld.c @@ -65,20 +65,16 @@ TargetLinkConfig* lld_create_link_config(__attribute__((unused)) const Target* t g_array_append_val(config->object_file_names, target_object); INFO("resolved path of target object: %s", target_object); - // if it is an app, add entrypoint library - if (target_config->mode == Application) { - char* entrypoint = g_strdup("libentrypoint.a"); - g_array_append_val(module->imports, entrypoint); - } - // resolve absolute paths to dependent library object files DEBUG("resolving target dependencies..."); for (guint i = 0; i < module->imports->len; i++) { const char* dependency = g_array_index(module->imports, const char*, i); - const char* dependency_object = get_absolute_link_path(target_config, dependency); + const char* library = g_strjoin("", "libgsc", dependency, ".a", NULL); + + const char* dependency_object = get_absolute_link_path(target_config, library); if (dependency_object == NULL) { - ERROR("failed to resolve path to dependency object: %s", dependency); + ERROR("failed to resolve path to dependency object: %s", library); lld_delete_link_config(config); return NULL; } diff --git a/src/set/set.c b/src/set/set.c index 1ad9652..e10a4c2 100644 --- a/src/set/set.c +++ b/src/set/set.c @@ -2379,12 +2379,14 @@ Module *create_set(AST_NODE_PTR currentNode) { GHashTable *functions = g_hash_table_new(g_str_hash, g_str_equal); GHashTable *variables = g_hash_table_new(g_str_hash, g_str_equal); GArray *imports = g_array_new(FALSE, FALSE, sizeof(const char *)); + GArray *includes = g_array_new(FALSE, FALSE, sizeof(const char *)); rootModule->boxes = boxes; rootModule->types = types; rootModule->functions = functions; rootModule->variables = variables; rootModule->imports = imports; + rootModule->includes = includes; DEBUG("created Module struct"); @@ -2456,6 +2458,10 @@ Module *create_set(AST_NODE_PTR currentNode) { DEBUG("create Import"); g_array_append_val(imports, AST_get_node(currentNode, i)->value); break; + case AST_Include: + DEBUG("create Include"); + g_array_append_val(includes, AST_get_node(currentNode, i)->value); + break; default: INFO("Provided source file could not be parsed because of semantic error."); break; diff --git a/src/set/types.h b/src/set/types.h index 2eb6e44..98e9f97 100644 --- a/src/set/types.h +++ b/src/set/types.h @@ -580,6 +580,7 @@ typedef struct Module_t { GHashTable* variables; // to be resolved after the module has been parsed completely GArray* imports; + GArray* includes; } Module;