Merge remote-tracking branch 'origin/124-compile-exectuable-with-clang' into 124-compile-exectuable-with-clang

# Conflicts:
#	src/llvm/link/lld.c
This commit is contained in:
Sven Vogel 2024-07-18 19:59:38 +02:00
commit 46d47ce9d7
15 changed files with 73 additions and 17 deletions

View File

@ -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})

View File

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

View File

@ -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

View File

@ -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`

View File

@ -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:

View File

@ -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"

View File

@ -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:

View File

@ -79,6 +79,7 @@ enum AST_SyntaxElement_t {
AST_AddressOf,
AST_Dereference,
AST_Reference,
AST_Include,
AST_ELEMENT_COUNT
};

View File

@ -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;
}

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) {
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) {

View File

@ -81,6 +81,7 @@
"!" {DEBUG("\"%s\" tokenized with \'OpBitnot\'", yytext); return(OpBitnot);};
"^" {DEBUG("\"%s\" tokenized with \'OpBitxor\'", yytext); return(OpBitxor);};
"import" {DEBUG("\"%s\" tokenized with \'KeyImport\'", yytext); return(KeyImport);};
"include" {DEBUG("\"%s\" tokenized with \'KeyInclude\'", yytext); return(KeyInclude);};
"silent" {DEBUG("\"%s\" tokenized with \'KeySilent\'", yytext); return(KeySilent);};
"box" {DEBUG("\"%s\" tokenized with \'KeyBox\'", yytext); return(KeyBox);};
"typeof" {DEBUG("\"%s\" tokenized with \'FunTypeof\'", yytext); return(FunTypeof);};

View File

@ -70,11 +70,14 @@ TargetLinkConfig* lld_create_link_config(__attribute__((unused)) const Target* t
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) {
INFO("failed to resolve path to dependency object: %s", dependency);
print_message(Warning, "failed to resolve path to dependency object: %s", dependency);
continue;
ERROR("failed to resolve path to dependency object: %s", library);
print_message(Warning, "failed to resolve path to dependency object: %s", dependency); lld_delete_link_config(config);
lld_delete_link_config(config);
return NULL;
}
g_array_append_val(config->object_file_names, dependency_object);
INFO("resolved path of target object: %s", dependency_object);

View File

@ -2423,12 +2423,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");
@ -2500,6 +2502,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;

View File

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

View File

@ -53,6 +53,7 @@
%type <node_ptr> opbool
%type <node_ptr> opbit
%type <node_ptr> moduleimport
%type <node_ptr> moduleinclude
%type <node_ptr> programbody
%type <node_ptr> fundef
%type <node_ptr> fundecl
@ -109,6 +110,7 @@
%token OpBitnot
%token OpBitxor
%token KeyImport
%token KeyInclude
%token KeySilent
%token KeyBox
%token FunTypeof
@ -142,6 +144,7 @@ program: program programbody {AST_push_node(root, $2);
| programbody {AST_push_node(root, $1);};
programbody: moduleimport {$$ = $1;}
| moduleinclude {$$ = $1;}
| fundef{$$ = $1;}
| fundecl{$$ = $1;}
| box{$$ = $1;}
@ -328,6 +331,9 @@ funcall: Ident argumentlist {AST_NODE_PTR funcall = AST_new_node(new_loc(), AST_
moduleimport: KeyImport ValStr {$$ = AST_new_node(new_loc(), AST_Import, $2);
DEBUG("Module-Import"); };
moduleinclude: KeyInclude ValStr {$$ = AST_new_node(new_loc(), AST_Include, $2);
DEBUG("Module-Include"); };
statementlist: statementlist statement {AST_push_node($1, $2);
$$ = $1;}
| statement {AST_NODE_PTR list = AST_new_node(new_loc(), AST_StmtList, NULL);