From b1bdd95f19f5e472200954dd7c312a3fd48ae84d Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 16 Oct 2020 15:32:09 +0100 Subject: [PATCH] Begin work on making packages import assembly sort files (.S) --- src/build_settings.cpp | 4 +--- src/parser.cpp | 49 +++++++++++++++++++++++++++++++++++++++++- src/parser.hpp | 32 +++++++++++++++++++++------ 3 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 084a7eddd..0429ca307 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -291,10 +291,8 @@ TargetArchKind get_target_arch_from_string(String str) { bool is_excluded_target_filename(String name) { - String const ext = str_lit(".odin"); String original_name = name; - GB_ASSERT(string_ends_with(name, ext)); - name = substring(name, 0, name.len-ext.len); + name = remove_extension_from_path(name); String str1 = {}; String str2 = {}; diff --git a/src/parser.cpp b/src/parser.cpp index b0ef534a9..de5c48d20 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -4430,6 +4430,7 @@ void destroy_parser(Parser *p) { destroy_ast_file(pkg->files[j]); } array_free(&pkg->files); + array_free(&pkg->foreign_files); } #if 0 for_array(i, p->package_imports) { @@ -4482,6 +4483,45 @@ void parser_add_file_to_process(Parser *p, AstPackage *pkg, FileInfo fi, TokenPo thread_pool_add_task(&parser_thread_pool, parser_worker_proc, wd); } +WORKER_TASK_PROC(foreign_file_worker_proc) { + ForeignFileWorkerData *wd = cast(ForeignFileWorkerData *)data; + Parser *p = wd->parser; + ImportedFile *imp = &wd->imported_file; + AstPackage *pkg = imp->pkg; + + AstForeignFile foreign_file = {wd->foreign_kind}; + + String fullpath = string_trim_whitespace(imp->fi.fullpath); // Just in case + + char *c_str = alloc_cstring(heap_allocator(), fullpath); + defer (gb_free(heap_allocator(), c_str)); + + gbFileContents fc = gb_file_read_contents(heap_allocator(), true, c_str); + foreign_file.source.text = (u8 *)fc.data; + foreign_file.source.len = fc.size; + + switch (wd->foreign_kind) { + case AstForeignFile_S: + // TODO(bill): Actually do something with it + break; + } + gb_mutex_lock(&p->file_add_mutex); + array_add(&pkg->foreign_files, foreign_file); + gb_mutex_unlock(&p->file_add_mutex); + return 0; +} + + +void parser_add_foreign_file_to_process(Parser *p, AstPackage *pkg, AstForeignFileKind kind, FileInfo fi, TokenPos pos) { + // TODO(bill): Use a better allocator + ImportedFile f = {pkg, fi, pos, p->file_to_process_count++}; + auto wd = gb_alloc_item(heap_allocator(), ForeignFileWorkerData); + wd->parser = p; + wd->imported_file = f; + wd->foreign_kind = kind; + thread_pool_add_task(&parser_thread_pool, foreign_file_worker_proc, wd); +} + // NOTE(bill): Returns true if it's added bool try_add_import_path(Parser *p, String const &path, String const &rel_path, TokenPos pos, PackageKind kind = Package_Normal) { @@ -4504,6 +4544,7 @@ bool try_add_import_path(Parser *p, String const &path, String const &rel_path, pkg->kind = kind; pkg->fullpath = path; array_init(&pkg->files, heap_allocator()); + pkg->foreign_files.allocator = heap_allocator(); // NOTE(bill): Single file initial package if (kind == Package_Init && string_ends_with(path, FILE_EXT)) { @@ -4554,11 +4595,17 @@ bool try_add_import_path(Parser *p, String const &path, String const &rel_path, for_array(list_index, list) { FileInfo fi = list[list_index]; String name = fi.name; - if (string_ends_with(name, FILE_EXT)) { + String ext = path_extension(name); + if (ext == FILE_EXT) { if (is_excluded_target_filename(name)) { continue; } parser_add_file_to_process(p, pkg, fi, pos); + } else if (ext == ".S" || ext ==".s") { + if (is_excluded_target_filename(name)) { + continue; + } + parser_add_foreign_file_to_process(p, pkg, AstForeignFile_S, fi, pos); } } diff --git a/src/parser.hpp b/src/parser.hpp index 9e20ffc1b..ff47d1887 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -121,14 +121,28 @@ struct AstFile { struct LLVMOpaqueMetadata *llvm_metadata_scope; }; +enum AstForeignFileKind { + AstForeignFile_Invalid, + + AstForeignFile_S, // Source, + + AstForeignFile_COUNT +}; + +struct AstForeignFile { + AstForeignFileKind kind; + String source; +}; + struct AstPackage { - PackageKind kind; - isize id; - String name; - String fullpath; - Array files; - bool is_single_file; + PackageKind kind; + isize id; + String name; + String fullpath; + Array files; + Array foreign_files; + bool is_single_file; // NOTE(bill): Created/set in checker Scope * scope; @@ -158,6 +172,12 @@ struct ParserWorkerData { ImportedFile imported_file; }; +struct ForeignFileWorkerData { + Parser *parser; + ImportedFile imported_file; + AstForeignFileKind foreign_kind; +}; +