Correct failure check for const cast

This commit is contained in:
gingerBill
2025-09-28 23:19:43 +01:00
parent cbab97fbd7
commit 5b88d2363d
+42 -31
View File
@@ -96,6 +96,9 @@ gb_internal LLVMValueRef llvm_const_cast(LLVMValueRef val, LLVMTypeRef dst, bool
case LLVMPointerTypeKind: case LLVMPointerTypeKind:
return LLVMConstPointerCast(val, dst); return LLVMConstPointerCast(val, dst);
case LLVMStructTypeKind: case LLVMStructTypeKind:
if (LLVMTypeOf(val) != dst) {
if (failure_) *failure_ = true;
}
return val; return val;
} }
if (failure_) *failure_ = true; if (failure_) *failure_ = true;
@@ -796,6 +799,39 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb
LLVMValueRef array_data = nullptr; LLVMValueRef array_data = nullptr;
if (is_local) {
// NOTE(bill, 2020-06-08): This is a bit of a hack but a "constant" slice needs
// its backing data on the stack
lbProcedure *p = m->curr_procedure;
LLVMTypeRef llvm_type = lb_type(m, t);
unsigned alignment = cast(unsigned)gb_max(type_align_of(t), 16);
LLVMValueRef local_copy = llvm_alloca(p, LLVMTypeOf(backing_array.value), alignment);
LLVMBuildStore(p->builder, backing_array.value, local_copy);
array_data = llvm_alloca(p, llvm_type, alignment);
LLVMBuildMemCpy(p->builder,
array_data, alignment,
local_copy, alignment,
LLVMConstInt(lb_type(m, t_int), type_size_of(t), false)
);
{
LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
LLVMValueRef ptr = LLVMBuildInBoundsGEP2(p->builder, llvm_type, array_data, indices, 2, "");
LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
lbAddr slice = lb_add_local_generated(p, original_type, false);
map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, slice);
lb_fill_slice(p, slice, {ptr, alloc_type_pointer(elem)}, {len, t_int});
return lb_addr_load(p, slice);
}
} else {
u32 id = m->global_array_index.fetch_add(1); u32 id = m->global_array_index.fetch_add(1);
gbString str = gb_string_make(temporary_allocator(), "csba$"); gbString str = gb_string_make(temporary_allocator(), "csba$");
str = gb_string_appendc(str, m->module_name); str = gb_string_appendc(str, m->module_name);
@@ -807,36 +843,6 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb
array_data = LLVMAddGlobal(m->mod, LLVMTypeOf(backing_array.value), str); array_data = LLVMAddGlobal(m->mod, LLVMTypeOf(backing_array.value), str);
LLVMSetInitializer(array_data, backing_array.value); LLVMSetInitializer(array_data, backing_array.value);
if (is_local) {
LLVMSetGlobalConstant(array_data, true);
// NOTE(bill, 2020-06-08): This is a bit of a hack but a "constant" slice needs
// its backing data on the stack
lbProcedure *p = m->curr_procedure;
// NOTE(bill, 2025-09-28): make the array data global BUT memcpy it
// to make a local copy
LLVMTypeRef llvm_type = lb_type(m, t);
LLVMValueRef local_copy = llvm_alloca(p, llvm_type, 16);
LLVMBuildMemCpy(p->builder,
local_copy, 16,
array_data, 1,
LLVMConstInt(lb_type(m, t_int), type_size_of(t), false));
{
LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
LLVMValueRef ptr = LLVMBuildInBoundsGEP2(p->builder, llvm_type, local_copy, indices, 2, "");
LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
lbAddr slice = lb_add_local_generated(p, original_type, false);
map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, slice);
lb_fill_slice(p, slice, {ptr, alloc_type_pointer(elem)}, {len, t_int});
return lb_addr_load(p, slice);
}
} else {
if (cc.link_section.len > 0) { if (cc.link_section.len > 0) {
LLVMSetSection(array_data, alloc_cstring(permanent_allocator(), cc.link_section)); LLVMSetSection(array_data, alloc_cstring(permanent_allocator(), cc.link_section));
} }
@@ -1673,7 +1679,10 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb
i32 index = field_remapping[f->Variable.field_index]; i32 index = field_remapping[f->Variable.field_index];
if (elem_type_can_be_constant(f->type)) { if (elem_type_can_be_constant(f->type)) {
values[index] = lb_const_value(m, f->type, val, cc, tav.type).value; lbValue value = lb_const_value(m, f->type, tav.value, cc, tav.type);
LLVMTypeRef value_type = LLVMTypeOf(value.value);
GB_ASSERT_MSG(lb_sizeof(value_type) == type_size_of(f->type), "%s vs %s", LLVMPrintTypeToString(value_type), type_to_string(f->type));
values[index] = value.value;
visited[index] = true; visited[index] = true;
} }
} }
@@ -1700,6 +1709,8 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb
if (is_constant) { if (is_constant) {
res.value = llvm_const_named_struct_internal(m, struct_type, values, cast(unsigned)value_count); res.value = llvm_const_named_struct_internal(m, struct_type, values, cast(unsigned)value_count);
LLVMTypeRef res_type = LLVMTypeOf(res.value);
GB_ASSERT(lb_sizeof(res_type) == lb_sizeof(struct_type));
return res; return res;
} else { } else {
// TODO(bill): THIS IS HACK BUT IT WORKS FOR WHAT I NEED // TODO(bill): THIS IS HACK BUT IT WORKS FOR WHAT I NEED