Browse Source

Correct global constant procedure initialization

gingerBill 3 years ago
parent
commit
697c839c84
3 changed files with 12 additions and 8 deletions
  1. 12 0
      src/llvm_backend.cpp
  2. 0 7
      src/llvm_backend_general.cpp
  3. 0 1
      src/llvm_backend_type.cpp

+ 12 - 0
src/llvm_backend.cpp

@@ -758,6 +758,10 @@ lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *start
 		GB_ASSERT(e->kind == Entity_Variable);
 		e->code_gen_module = entity_module;
 
+		if (e->token.string == "XXH3_init_custom_secret") {
+			gb_printf_err("HERE1\n");
+		}
+
 		Ast *init_expr = var->decl->init_expr;
 		if (init_expr != nullptr)  {
 			lbValue init = lb_build_expr(p, init_expr);
@@ -780,6 +784,10 @@ lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *start
 				var->init = init;
 			} else if (lb_is_const_or_global(init)) {
 				if (!var->is_initialized) {
+					if (is_type_proc(init.type)) {
+						LLVMTypeRef global_type = llvm_addr_type(p->module, var->var);
+						init.value = LLVMConstPointerCast(init.value, global_type);
+					}
 					LLVMSetInitializer(var->var.value, init.value);
 					var->is_initialized = true;
 					continue;
@@ -1649,6 +1657,10 @@ void lb_generate_code(lbGenerator *gen) {
 					if (tav.value.kind != ExactValue_Invalid) {
 						ExactValue v = tav.value;
 						lbValue init = lb_const_value(m, tav.type, v);
+						if (is_type_proc(init.type)) {
+							LLVMTypeRef global_type = llvm_addr_type(m, var.var);
+							init.value = LLVMConstPointerCast(init.value, global_type);
+						}
 						LLVMSetInitializer(g.value, init.value);
 						var.is_initialized = true;
 					}

+ 0 - 7
src/llvm_backend_general.cpp

@@ -854,13 +854,6 @@ void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) {
 	lb_emit_store(p, addr.addr, value);
 }
 
-void lb_const_store(lbValue ptr, lbValue value) {
-	GB_ASSERT(lb_is_const(ptr));
-	GB_ASSERT(lb_is_const(value));
-	GB_ASSERT(is_type_pointer(ptr.type));
-	LLVMSetInitializer(ptr.value, value.value);
-}
-
 void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) {
 	GB_ASSERT(value.value != nullptr);
 	Type *a = type_deref(ptr.type);

+ 0 - 1
src/llvm_backend_type.cpp

@@ -554,7 +554,6 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da
 				lbValue index     = lb_const_int(m, t_int, i);
 				lbValue type_info = lb_emit_ptr_offset(p, memory_types, index);
 
-				// TODO(bill): Make this constant if possible, 'lb_const_store' does not work
 				lb_emit_store(p, type_info, lb_type_info(m, f->type));
 				if (f->token.string.len > 0) {
 					lbValue name = lb_emit_ptr_offset(p, memory_names, index);