Browse Source

Begin to generalize modules away from `AstPackage *` in `-use-separate-modules`

gingerBill 2 years ago
parent
commit
3b22c6620c

+ 1 - 1
src/llvm_backend.cpp

@@ -1222,7 +1222,7 @@ gb_internal void lb_create_global_procedures_and_types(lbGenerator *gen, Checker
 
 
 		lbModule *m = &gen->default_module;
 		lbModule *m = &gen->default_module;
 		if (USE_SEPARATE_MODULES) {
 		if (USE_SEPARATE_MODULES) {
-			m = lb_pkg_module(gen, e->pkg);
+			m = lb_module_of_entity(gen, e);
 		}
 		}
 
 
 		array_add(&m->global_procedures_and_types_to_create, e);
 		array_add(&m->global_procedures_and_types_to_create, e);

+ 1 - 1
src/llvm_backend.hpp

@@ -191,7 +191,7 @@ struct lbGenerator {
 	Array<String> output_temp_paths;
 	Array<String> output_temp_paths;
 	String   output_base;
 	String   output_base;
 	String   output_name;
 	String   output_name;
-	PtrMap<AstPackage *, lbModule *> modules; 
+	PtrMap<void *, lbModule *> modules; // key is `AstPackage *` (`void *` is used for future use)
 	PtrMap<LLVMContextRef, lbModule *> modules_through_ctx; 
 	PtrMap<LLVMContextRef, lbModule *> modules_through_ctx; 
 	lbModule default_module;
 	lbModule default_module;
 
 

+ 1 - 1
src/llvm_backend_debug.cpp

@@ -1177,7 +1177,7 @@ gb_internal void add_debug_info_for_global_constant_from_entity(lbGenerator *gen
 	}
 	}
 	lbModule *m = &gen->default_module;
 	lbModule *m = &gen->default_module;
 	if (USE_SEPARATE_MODULES) {
 	if (USE_SEPARATE_MODULES) {
-		m = lb_pkg_module(gen, e->pkg);
+		m = lb_module_of_entity(gen, e);
 	}
 	}
 
 
 	if (is_type_integer(e->type)) {
 	if (is_type_integer(e->type)) {

+ 17 - 12
src/llvm_backend_general.cpp

@@ -143,13 +143,13 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) {
 			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, pkg, m);
+			map_set(&gen->modules, cast(void *)pkg, m);
 			lb_init_module(m, c);
 			lb_init_module(m, c);
 		}
 		}
 	}
 	}
 
 
 	gen->default_module.gen = gen;
 	gen->default_module.gen = gen;
-	map_set(&gen->modules, cast(AstPackage *)nullptr, &gen->default_module);
+	map_set(&gen->modules, cast(void *)nullptr, &gen->default_module);
 	lb_init_module(&gen->default_module, c);
 	lb_init_module(&gen->default_module, c);
 
 
 
 
@@ -315,17 +315,22 @@ gb_internal bool lb_is_instr_terminating(LLVMValueRef instr) {
 }
 }
 
 
 
 
-
-gb_internal lbModule *lb_pkg_module(lbGenerator *gen, AstPackage *pkg) {
-	// NOTE(bill): no need for a mutex since it's immutable
-	auto *found = map_get(&gen->modules, pkg);
-	if (found) {
-		return *found;
+gb_internal lbModule *lb_module_of_entity(lbGenerator *gen, Entity *e) {
+	GB_ASSERT(e != nullptr);
+	if (e->pkg) {
+		lbModule **found = nullptr;
+		found = map_get(&gen->modules, cast(void *)e->file);
+		if (found) {
+			return *found;
+		}
+		found = map_get(&gen->modules, cast(void *)e->pkg);
+		if (found) {
+			return *found;
+		}
 	}
 	}
 	return &gen->default_module;
 	return &gen->default_module;
 }
 }
 
 
-
 gb_internal lbAddr lb_addr(lbValue addr) {
 gb_internal lbAddr lb_addr(lbValue addr) {
 	lbAddr v = {lbAddr_Default, addr};
 	lbAddr v = {lbAddr_Default, addr};
 	if (addr.type != nullptr && is_type_relative_pointer(type_deref(addr.type))) {
 	if (addr.type != nullptr && is_type_relative_pointer(type_deref(addr.type))) {
@@ -2546,7 +2551,7 @@ gb_internal lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *e
 		return lb_find_procedure_value_from_entity(m, e);
 		return lb_find_procedure_value_from_entity(m, e);
 	}
 	}
 	if (USE_SEPARATE_MODULES) {
 	if (USE_SEPARATE_MODULES) {
-		lbModule *other_module = lb_pkg_module(m->gen, e->pkg);
+		lbModule *other_module = lb_module_of_entity(m->gen, e);
 		if (other_module != m) {
 		if (other_module != m) {
 			String name = lb_get_entity_name(other_module, e);
 			String name = lb_get_entity_name(other_module, e);
 
 
@@ -2590,7 +2595,7 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e)
 
 
 	lbModule *other_module = m;
 	lbModule *other_module = m;
 	if (USE_SEPARATE_MODULES) {
 	if (USE_SEPARATE_MODULES) {
-		other_module = lb_pkg_module(m->gen, e->pkg);
+		other_module = lb_module_of_entity(m->gen, e);
 	}
 	}
 	if (other_module == m) {
 	if (other_module == m) {
 		debugf("Missing Procedure (lb_find_procedure_value_from_entity): %.*s\n", LIT(e->token.string));
 		debugf("Missing Procedure (lb_find_procedure_value_from_entity): %.*s\n", LIT(e->token.string));
@@ -2687,7 +2692,7 @@ gb_internal lbValue lb_find_value_from_entity(lbModule *m, Entity *e) {
 	}
 	}
 
 
 	if (USE_SEPARATE_MODULES) {
 	if (USE_SEPARATE_MODULES) {
-		lbModule *other_module = lb_pkg_module(m->gen, e->pkg);
+		lbModule *other_module = lb_module_of_entity(m->gen, e);
 
 
 		// TODO(bill): correct this logic
 		// TODO(bill): correct this logic
 		bool is_external = other_module != m;
 		bool is_external = other_module != m;

+ 1 - 1
src/llvm_backend_proc.cpp

@@ -74,7 +74,7 @@ gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool i
 	String link_name = {};
 	String link_name = {};
 
 
 	if (ignore_body) {
 	if (ignore_body) {
-		lbModule *other_module = lb_pkg_module(m->gen, entity->pkg);
+		lbModule *other_module = lb_module_of_entity(m->gen, entity);
 		link_name = lb_get_entity_name(other_module, entity);
 		link_name = lb_get_entity_name(other_module, entity);
 	} else {
 	} else {
 		link_name = lb_get_entity_name(m, entity);
 		link_name = lb_get_entity_name(m, entity);