aboutsummaryrefslogtreecommitdiff
path: root/src/llvm_backend_const.cpp
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2025-09-28 20:20:26 +0100
committergingerBill <gingerBill@users.noreply.github.com>2025-09-28 20:20:26 +0100
commitffdfbfe2c2d0e09087f166be79f3dbc2859844e6 (patch)
treed934e3db6ca1a7129ddac3654f8bc4af6114edc5 /src/llvm_backend_const.cpp
parenta974c51d573618c9cc4496a32885b7f871c317b2 (diff)
Begin to support constant array of unions
Diffstat (limited to 'src/llvm_backend_const.cpp')
-rw-r--r--src/llvm_backend_const.cpp18
1 files changed, 10 insertions, 8 deletions
diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp
index 415d6743b..8cdc889e0 100644
--- a/src/llvm_backend_const.cpp
+++ b/src/llvm_backend_const.cpp
@@ -96,10 +96,6 @@ gb_internal LLVMValueRef llvm_const_cast(LLVMValueRef val, LLVMTypeRef dst) {
case LLVMPointerTypeKind:
return LLVMConstPointerCast(val, dst);
case LLVMStructTypeKind:
- // GB_PANIC("%s -> %s", LLVMPrintValueToString(val), LLVMPrintTypeToString(dst));
- // NOTE(bill): It's not possible to do a bit cast on a struct, why was this code even here in the first place?
- // It seems mostly to exist to get around the "anonymous -> named" struct assignments
- // return LLVMConstBitCast(val, dst);
return val;
default:
GB_PANIC("Unhandled const cast %s to %s", LLVMPrintTypeToString(src), LLVMPrintTypeToString(dst));
@@ -199,11 +195,17 @@ gb_internal LLVMValueRef llvm_const_named_struct_internal(LLVMTypeRef t, LLVMVal
return LLVMConstNamedStruct(t, values, value_count);
}
-gb_internal LLVMValueRef llvm_const_array(LLVMTypeRef elem_type, LLVMValueRef *values, isize value_count_) {
+gb_internal LLVMValueRef llvm_const_array(lbModule *m, LLVMTypeRef elem_type, LLVMValueRef *values, isize value_count_) {
unsigned value_count = cast(unsigned)value_count_;
for (unsigned i = 0; i < value_count; i++) {
values[i] = llvm_const_cast(values[i], elem_type);
}
+ for (unsigned i = 0; i < value_count; i++) {
+ if (elem_type != LLVMTypeOf(values[i])) {
+ return LLVMConstStructInContext(m->ctx, values, value_count, false);
+ }
+ }
+
return LLVMConstArray(elem_type, values, value_count);
}
@@ -461,7 +463,7 @@ gb_internal LLVMValueRef lb_build_constant_array_values(lbModule *m, Type *type,
return lb_addr_load(p, v).value;
}
- return llvm_const_array(lb_type(m, elem_type), values, cast(unsigned int)count);
+ return llvm_const_array(m, lb_type(m, elem_type), values, cast(unsigned int)count);
}
gb_internal LLVMValueRef lb_big_int_to_llvm(lbModule *m, Type *original_type, BigInt const *a) {
@@ -1016,7 +1018,7 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb
}
GB_ASSERT(offset == s.len);
- res.value = llvm_const_array(et, elems, cast(unsigned)count);
+ res.value = llvm_const_array(m, et, elems, cast(unsigned)count);
return res;
}
// NOTE(bill, 2021-10-07): Allow for array programming value constants
@@ -1046,7 +1048,7 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb
elems[i] = single_elem.value;
}
- res.value = llvm_const_array(lb_type(m, elem), elems, cast(unsigned)count);
+ res.value = llvm_const_array(m, lb_type(m, elem), elems, cast(unsigned)count);
return res;
} else if (is_type_matrix(type) &&
value.kind != ExactValue_Invalid &&