Browse Source

Prepare for arbitrary separate modules

gingerBill 2 years ago
parent
commit
4a8c37dd52
3 changed files with 28 additions and 7 deletions
  1. 12 4
      src/llvm_backend.cpp
  2. 2 1
      src/llvm_backend.hpp
  3. 14 2
      src/llvm_backend_general.cpp

+ 12 - 4
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
 		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);
 		path = concatenate3_strings(permanent_allocator(), path, STR_LIT("-"), m->pkg->name);
 	} else if (USE_SEPARATE_MODULES) {
 	} else if (USE_SEPARATE_MODULES) {
 		path = concatenate_strings(permanent_allocator(), path, STR_LIT("-builtin"));
 		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
 		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);
 		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;
 	isize worker_count = thread_count-1;
 
 
 	bool do_threading = !!(LLVMIsMultithreaded() && USE_SEPARATE_MODULES && MULTITHREAD_OBJECT_GENERATION && worker_count > 0);
 	bool do_threading = !!(LLVMIsMultithreaded() && USE_SEPARATE_MODULES && MULTITHREAD_OBJECT_GENERATION && worker_count > 0);
-	do_threading = false;
 
 
 	lbModule *default_module = &gen->default_module;
 	lbModule *default_module = &gen->default_module;
 	CheckerInfo *info = gen->info;
 	CheckerInfo *info = gen->info;
@@ -2388,7 +2397,6 @@ gb_internal bool lb_generate_code(lbGenerator *gen) {
 			if (lb_is_module_empty(m)) {
 			if (lb_is_module_empty(m)) {
 				continue;
 				continue;
 			}
 			}
-
 			String filepath_ll = lb_filepath_ll_for_module(m);
 			String filepath_ll = lb_filepath_ll_for_module(m);
 			if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) {
 			if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) {
 				gb_printf_err("LLVM Error: %s\n", llvm_error);
 				gb_printf_err("LLVM Error: %s\n", llvm_error);

+ 2 - 1
src/llvm_backend.hpp

@@ -135,7 +135,8 @@ struct lbModule {
 	LLVMTargetMachineRef target_machine;
 	LLVMTargetMachineRef target_machine;
 
 
 	CheckerInfo *info;
 	CheckerInfo *info;
-	AstPackage *pkg; // associated
+	AstPackage *pkg; // possibly associated
+	AstFile *file;   // possibly associated
 
 
 	PtrMap<Type *, LLVMTypeRef> types;
 	PtrMap<Type *, LLVMTypeRef> types;
 	PtrMap<Type *, LLVMTypeRef> func_raw_types;
 	PtrMap<Type *, LLVMTypeRef> func_raw_types;

+ 14 - 2
src/llvm_backend_general.cpp

@@ -19,7 +19,9 @@ gb_internal void lb_init_module(lbModule *m, Checker *c) {
 	m->info = &c->info;
 	m->info = &c->info;
 
 
 	gbString module_name = gb_string_make(heap_allocator(), "odin_package");
 	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_appendc(module_name, "-");
 		module_name = gb_string_append_length(module_name, m->pkg->name.text, m->pkg->name.len);
 		module_name = gb_string_append_length(module_name, m->pkg->name.text, m->pkg->name.len);
 	} else if (USE_SEPARATE_MODULES) {
 	} else if (USE_SEPARATE_MODULES) {
@@ -139,12 +141,22 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) {
 	if (USE_SEPARATE_MODULES) {
 	if (USE_SEPARATE_MODULES) {
 		for (auto const &entry : gen->info->packages) {
 		for (auto const &entry : gen->info->packages) {
 			AstPackage *pkg = entry.value;
 			AstPackage *pkg = entry.value;
-
+		#if 1
 			auto m = gb_alloc_item(permanent_allocator(), lbModule);
 			auto m = gb_alloc_item(permanent_allocator(), lbModule);
 			m->pkg = pkg;
 			m->pkg = pkg;
 			m->gen = gen;
 			m->gen = gen;
 			map_set(&gen->modules, cast(void *)pkg, m);
 			map_set(&gen->modules, cast(void *)pkg, m);
 			lb_init_module(m, c);
 			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
 		}
 		}
 	}
 	}