From 4a8c37dd529e9f09583101f086833cec2c1b71ce Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 12 Jan 2023 21:45:02 +0000 Subject: [PATCH] Prepare for arbitrary separate modules --- src/llvm_backend.cpp | 16 ++++++++++++---- src/llvm_backend.hpp | 3 ++- src/llvm_backend_general.cpp | 16 ++++++++++++++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index f0d51be73..aa49fd055 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1488,7 +1488,12 @@ gb_internal String lb_filepath_ll_for_module(lbModule *m) { build_context.build_paths[BuildPath_Output].name ); - if (m->pkg) { + if (m->file) { + char buf[32] = {}; + isize n = gb_snprintf(buf, gb_size_of(buf), "-%u", m->file->id); + String suffix = make_string((u8 *)buf, n-1); + path = concatenate_strings(permanent_allocator(), path, suffix); + } else if (m->pkg) { path = concatenate3_strings(permanent_allocator(), path, STR_LIT("-"), m->pkg->name); } else if (USE_SEPARATE_MODULES) { path = concatenate_strings(permanent_allocator(), path, STR_LIT("-builtin")); @@ -1504,7 +1509,12 @@ gb_internal String lb_filepath_obj_for_module(lbModule *m) { build_context.build_paths[BuildPath_Output].name ); - if (m->pkg) { + if (m->file) { + char buf[32] = {}; + isize n = gb_snprintf(buf, gb_size_of(buf), "-%u", m->file->id); + String suffix = make_string((u8 *)buf, n-1); + path = concatenate_strings(permanent_allocator(), path, suffix); + } else if (m->pkg) { path = concatenate3_strings(permanent_allocator(), path, STR_LIT("-"), m->pkg->name); } @@ -1852,7 +1862,6 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { isize worker_count = thread_count-1; bool do_threading = !!(LLVMIsMultithreaded() && USE_SEPARATE_MODULES && MULTITHREAD_OBJECT_GENERATION && worker_count > 0); - do_threading = false; lbModule *default_module = &gen->default_module; CheckerInfo *info = gen->info; @@ -2388,7 +2397,6 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { if (lb_is_module_empty(m)) { continue; } - String filepath_ll = lb_filepath_ll_for_module(m); if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) { gb_printf_err("LLVM Error: %s\n", llvm_error); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 4288ae16e..beb9a99b6 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -135,7 +135,8 @@ struct lbModule { LLVMTargetMachineRef target_machine; CheckerInfo *info; - AstPackage *pkg; // associated + AstPackage *pkg; // possibly associated + AstFile *file; // possibly associated PtrMap types; PtrMap func_raw_types; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 516c46396..7eb024daf 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -19,7 +19,9 @@ gb_internal void lb_init_module(lbModule *m, Checker *c) { m->info = &c->info; gbString module_name = gb_string_make(heap_allocator(), "odin_package"); - if (m->pkg) { + if (m->file) { + module_name = gb_string_append_fmt(module_name, "-%u", m->file->id+1); + } else if (m->pkg) { module_name = gb_string_appendc(module_name, "-"); module_name = gb_string_append_length(module_name, m->pkg->name.text, m->pkg->name.len); } else if (USE_SEPARATE_MODULES) { @@ -139,12 +141,22 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) { if (USE_SEPARATE_MODULES) { for (auto const &entry : gen->info->packages) { AstPackage *pkg = entry.value; - + #if 1 auto m = gb_alloc_item(permanent_allocator(), lbModule); m->pkg = pkg; m->gen = gen; map_set(&gen->modules, cast(void *)pkg, m); lb_init_module(m, c); + #else + for (AstFile *file : pkg->files) { + auto m = gb_alloc_item(permanent_allocator(), lbModule); + m->file = file; + m->pkg = pkg; + m->gen = gen; + map_set(&gen->modules, cast(void *)file, m); + lb_init_module(m, c); + } + #endif } }