added library binary resolving

This commit is contained in:
Sven Vogel 2024-07-18 19:55:17 +02:00
parent 938a5c7fdf
commit c4c422d899
12 changed files with 62 additions and 21 deletions

View File

@ -16,10 +16,16 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/../bin/std")
# add native module libraries # add native module libraries
file(GLOB_RECURSE STDLIB_IO_SOURCE_FILES src/io/*.c) 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) 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) 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})

View File

@ -2,9 +2,6 @@
// Created by servostar on 6/10/24. // Created by servostar on 6/10/24.
// //
extern void entry(void);
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
entry();
return 0; return 0;
} }

View File

@ -6,7 +6,7 @@
# | Generic Input/Output | # | Generic Input/Output |
# `----------------------------------------` # `----------------------------------------`
import "def.gsc" include "def.gsc"
# platform specific handle to an I/O device # platform specific handle to an I/O device
# can be a file, buffer, window or something else # can be a file, buffer, window or something else

View File

@ -6,7 +6,7 @@
# | Memory Management | # | Memory Management |
# `----------------------------------------` # `----------------------------------------`
import "def.gsc" include "def.gsc"
# Allocate `len` bytes of heap memory # Allocate `len` bytes of heap memory
# Returns a pointer to the memory as `ptr` # Returns a pointer to the memory as `ptr`

View File

@ -6,7 +6,7 @@
# | Operating System | # | Operating System |
# `----------------------------------------` # `----------------------------------------`
import "def.gsc" include "def.gsc"
# Return a hard coded C string identifying the underlying operating system # Return a hard coded C string identifying the underlying operating system
# Will return one of the following: # Will return one of the following:

View File

@ -7,10 +7,10 @@
# `----------------------------------------` # `----------------------------------------`
# standard type definitions # standard type definitions
import "def.gsc" include "def.gsc"
# I/O operations # I/O operations
import "io.gsc" include "io.gsc"
# memory management # memory management
import "mem.gsc" include "mem.gsc"

View File

@ -101,6 +101,7 @@ const char* AST_node_to_string(const struct AST_Node_t* node) {
case AST_Ident: case AST_Ident:
case AST_Macro: case AST_Macro:
case AST_Import: case AST_Import:
case AST_Include:
case AST_Storage: case AST_Storage:
case AST_Typekind: case AST_Typekind:
case AST_Sign: case AST_Sign:

View File

@ -212,6 +212,36 @@ TargetConfig* default_target_config_from_args() {
char* default_import_path = mem_strdup(MemoryNamespaceOpt, "."); char* default_import_path = mem_strdup(MemoryNamespaceOpt, ".");
g_array_append_val(config->import_paths, default_import_path); 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; return config;
} }

View File

@ -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) { 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); 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++) { for (guint i = 0; i < config->import_paths->len; i++) {
const char* import_directory_path = g_array_index(config->import_paths, char*, 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++) { for (size_t i = 0; i < AST_get_child_count(root_module); i++) {
AST_NODE_PTR child = AST_get_node(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); const char* path = get_absolute_import_path(target, child->value);
if (path == NULL) { if (path == NULL) {

View File

@ -65,20 +65,16 @@ TargetLinkConfig* lld_create_link_config(__attribute__((unused)) const Target* t
g_array_append_val(config->object_file_names, target_object); g_array_append_val(config->object_file_names, target_object);
INFO("resolved path of target object: %s", 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 // resolve absolute paths to dependent library object files
DEBUG("resolving target dependencies..."); DEBUG("resolving target dependencies...");
for (guint i = 0; i < module->imports->len; i++) { for (guint i = 0; i < module->imports->len; i++) {
const char* dependency = g_array_index(module->imports, const char*, 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) { 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); lld_delete_link_config(config);
return NULL; return NULL;
} }

View File

@ -2379,12 +2379,14 @@ Module *create_set(AST_NODE_PTR currentNode) {
GHashTable *functions = g_hash_table_new(g_str_hash, g_str_equal); GHashTable *functions = g_hash_table_new(g_str_hash, g_str_equal);
GHashTable *variables = 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 *imports = g_array_new(FALSE, FALSE, sizeof(const char *));
GArray *includes = g_array_new(FALSE, FALSE, sizeof(const char *));
rootModule->boxes = boxes; rootModule->boxes = boxes;
rootModule->types = types; rootModule->types = types;
rootModule->functions = functions; rootModule->functions = functions;
rootModule->variables = variables; rootModule->variables = variables;
rootModule->imports = imports; rootModule->imports = imports;
rootModule->includes = includes;
DEBUG("created Module struct"); DEBUG("created Module struct");
@ -2456,6 +2458,10 @@ Module *create_set(AST_NODE_PTR currentNode) {
DEBUG("create Import"); DEBUG("create Import");
g_array_append_val(imports, AST_get_node(currentNode, i)->value); g_array_append_val(imports, AST_get_node(currentNode, i)->value);
break; break;
case AST_Include:
DEBUG("create Include");
g_array_append_val(includes, AST_get_node(currentNode, i)->value);
break;
default: default:
INFO("Provided source file could not be parsed because of semantic error."); INFO("Provided source file could not be parsed because of semantic error.");
break; break;

View File

@ -580,6 +580,7 @@ typedef struct Module_t {
GHashTable* variables; GHashTable* variables;
// to be resolved after the module has been parsed completely // to be resolved after the module has been parsed completely
GArray* imports; GArray* imports;
GArray* includes;
} Module; } Module;