mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-19 13:00:28 +00:00
Support compound literals for fixed capacity dynamic arrays
This commit is contained in:
@@ -10321,6 +10321,7 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast *
|
||||
case Type_DynamicArray:
|
||||
case Type_SimdVector:
|
||||
case Type_Matrix:
|
||||
case Type_FixedCapacityDynamicArray:
|
||||
{
|
||||
Type *elem_type = nullptr;
|
||||
String context_name = {};
|
||||
@@ -10351,6 +10352,10 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast *
|
||||
elem_type = t->DynamicArray.elem;
|
||||
context_name = str_lit("dynamic array literal");
|
||||
is_constant = false;
|
||||
} else if (t->kind == Type_FixedCapacityDynamicArray) {
|
||||
elem_type = t->FixedCapacityDynamicArray.elem;
|
||||
context_name = str_lit("fixed capacity dynamic array literal");
|
||||
max_type_count = t->FixedCapacityDynamicArray.capacity;
|
||||
} else if (t->kind == Type_SimdVector) {
|
||||
elem_type = t->SimdVector.elem;
|
||||
context_name = str_lit("simd vector literal");
|
||||
|
||||
@@ -694,6 +694,28 @@ gb_internal void lb_const_array_spread(lbModule *m, lbConstContext cc, Type *arr
|
||||
res->value = llvm_const_array(m, lb_type(m, elem), elems, cast(unsigned)count);
|
||||
}
|
||||
|
||||
gb_internal LLVMValueRef lb_fill_fixed_capacity_dynamic_array(lbModule *m, i64 elem_count, Type *original_type, LLVMValueRef *values, lbConstContext cc) {
|
||||
Type *bt = base_type(original_type);
|
||||
GB_ASSERT(bt->kind == Type_FixedCapacityDynamicArray);
|
||||
Type *elem_type = bt->FixedCapacityDynamicArray.elem;
|
||||
i64 capacity = bt->FixedCapacityDynamicArray.capacity;
|
||||
|
||||
Type *array_backing_type = alloc_type_array(elem_type, capacity);
|
||||
LLVMValueRef array_backing = lb_build_constant_array_values(m, array_backing_type, elem_type, cast(isize)capacity, values, cc);
|
||||
LLVMValueRef array_len = lb_const_int(m, t_int, elem_count).value;
|
||||
|
||||
isize svalue_count = 0;
|
||||
LLVMValueRef svalues[3] = {};
|
||||
svalues[svalue_count++] = array_backing;
|
||||
i64 padding = bt->FixedCapacityDynamicArray.padding_needed;
|
||||
if (padding > 0) {
|
||||
svalues[svalue_count++] = LLVMConstNull(lb_type_padding_filler(m, padding, 1));
|
||||
}
|
||||
svalues[svalue_count++] = array_len;
|
||||
|
||||
return llvm_const_named_struct(m, original_type, svalues, svalue_count);
|
||||
}
|
||||
|
||||
gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lbConstContext cc, Type *value_type) {
|
||||
if (cc.allow_local) {
|
||||
cc.is_rodata = false;
|
||||
@@ -1554,6 +1576,108 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb
|
||||
res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, cc);
|
||||
return res;
|
||||
}
|
||||
} else if (is_type_fixed_capacity_dynamic_array(type)) {
|
||||
ast_node(cl, CompoundLit, value.value_compound);
|
||||
Type *elem_type = type->FixedCapacityDynamicArray.elem;
|
||||
i64 capacity = type->FixedCapacityDynamicArray.capacity;
|
||||
isize elem_count = cl->elems.count;
|
||||
if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
|
||||
return lb_const_nil(m, original_type);
|
||||
}
|
||||
if (cl->elems[0]->kind == Ast_FieldValue) {
|
||||
// TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
|
||||
LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)capacity);
|
||||
|
||||
i64 max_index = -1;
|
||||
isize value_index = 0;
|
||||
for (i64 i = 0; i < capacity; i++) {
|
||||
bool found = false;
|
||||
|
||||
for (isize j = 0; j < elem_count; j++) {
|
||||
Ast *elem = cl->elems[j];
|
||||
ast_node(fv, FieldValue, elem);
|
||||
if (is_ast_range(fv->field)) {
|
||||
ast_node(ie, BinaryExpr, fv->field);
|
||||
TypeAndValue lo_tav = ie->left->tav;
|
||||
TypeAndValue hi_tav = ie->right->tav;
|
||||
GB_ASSERT(lo_tav.mode == Addressing_Constant);
|
||||
GB_ASSERT(hi_tav.mode == Addressing_Constant);
|
||||
|
||||
TokenKind op = ie->op.kind;
|
||||
i64 lo = exact_value_to_i64(lo_tav.value);
|
||||
i64 hi = exact_value_to_i64(hi_tav.value);
|
||||
if (op != Token_RangeHalf) {
|
||||
hi += 1;
|
||||
}
|
||||
max_index = gb_max(max_index, hi-1);
|
||||
|
||||
if (lo == i) {
|
||||
TypeAndValue tav = fv->value->tav;
|
||||
LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
|
||||
for (i64 k = lo; k < hi; k++) {
|
||||
values[value_index++] = val;
|
||||
}
|
||||
|
||||
found = true;
|
||||
i += (hi-lo-1);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
TypeAndValue index_tav = fv->field->tav;
|
||||
GB_ASSERT(index_tav.mode == Addressing_Constant);
|
||||
i64 index = exact_value_to_i64(index_tav.value);
|
||||
|
||||
max_index = gb_max(max_index, index);
|
||||
|
||||
if (index == i) {
|
||||
TypeAndValue tav = fv->value->tav;
|
||||
LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
|
||||
values[value_index++] = val;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
|
||||
}
|
||||
}
|
||||
|
||||
i64 count = max_index+1;
|
||||
GB_ASSERT(0 < count);
|
||||
GB_ASSERT(count <= capacity);
|
||||
|
||||
res.value = lb_fill_fixed_capacity_dynamic_array(m, count, original_type, values, cc);
|
||||
return res;
|
||||
} else if (are_types_identical(value.value_compound->tav.type, elem_type)) {
|
||||
// Compound is of array item type; expand its value to all items in array.
|
||||
LLVMValueRef* values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)capacity);
|
||||
|
||||
for (isize i = 0; i < capacity; i++) {
|
||||
values[i] = lb_const_value(m, elem_type, value, cc, elem_type).value;
|
||||
}
|
||||
|
||||
res.value = lb_fill_fixed_capacity_dynamic_array(m, capacity, original_type, values, cc);
|
||||
return res;
|
||||
} else {
|
||||
// Assume that compound value is an array literal
|
||||
GB_ASSERT_MSG(elem_count <= capacity, "%td <= %td", elem_count, capacity);
|
||||
|
||||
LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)capacity);
|
||||
|
||||
for (isize i = 0; i < elem_count; i++) {
|
||||
TypeAndValue tav = cl->elems[i]->tav;
|
||||
GB_ASSERT(tav.mode != Addressing_Invalid);
|
||||
values[i] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
|
||||
}
|
||||
for (isize i = elem_count; i < capacity; i++) {
|
||||
values[i] = LLVMConstNull(lb_type(m, elem_type));
|
||||
}
|
||||
|
||||
res.value = lb_fill_fixed_capacity_dynamic_array(m, elem_count, original_type, values, cc);
|
||||
return res;
|
||||
}
|
||||
} else if (is_type_simd_vector(type)) {
|
||||
ast_node(cl, CompoundLit, value.value_compound);
|
||||
|
||||
|
||||
@@ -4495,6 +4495,7 @@ gb_internal void lb_build_addr_compound_lit_populate(lbProcedure *p, Slice<Ast *
|
||||
case Type_DynamicArray: et = bt->DynamicArray.elem; break;
|
||||
case Type_SimdVector: et = bt->SimdVector.elem; break;
|
||||
case Type_Matrix: et = bt->Matrix.elem; break;
|
||||
case Type_FixedCapacityDynamicArray: et = bt->FixedCapacityDynamicArray.elem; break;
|
||||
}
|
||||
GB_ASSERT(et != nullptr);
|
||||
|
||||
@@ -5102,12 +5103,13 @@ gb_internal lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) {
|
||||
|
||||
Type *et = nullptr;
|
||||
switch (bt->kind) {
|
||||
case Type_Array: et = bt->Array.elem; break;
|
||||
case Type_EnumeratedArray: et = bt->EnumeratedArray.elem; break;
|
||||
case Type_Slice: et = bt->Slice.elem; break;
|
||||
case Type_BitSet: et = bt->BitSet.elem; break;
|
||||
case Type_SimdVector: et = bt->SimdVector.elem; break;
|
||||
case Type_Matrix: et = bt->Matrix.elem; break;
|
||||
case Type_Array: et = bt->Array.elem; break;
|
||||
case Type_EnumeratedArray: et = bt->EnumeratedArray.elem; break;
|
||||
case Type_Slice: et = bt->Slice.elem; break;
|
||||
case Type_BitSet: et = bt->BitSet.elem; break;
|
||||
case Type_SimdVector: et = bt->SimdVector.elem; break;
|
||||
case Type_Matrix: et = bt->Matrix.elem; break;
|
||||
case Type_FixedCapacityDynamicArray: et = bt->FixedCapacityDynamicArray.elem; break;
|
||||
}
|
||||
|
||||
String proc_name = {};
|
||||
@@ -5489,6 +5491,25 @@ gb_internal lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) {
|
||||
break;
|
||||
}
|
||||
|
||||
case Type_FixedCapacityDynamicArray: {
|
||||
if (cl->elems.count > 0) {
|
||||
lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr)));
|
||||
|
||||
auto temp_data = array_make<lbCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
|
||||
|
||||
lb_build_addr_compound_lit_populate(p, cl->elems, &temp_data, type);
|
||||
|
||||
lbValue dst_ptr = lb_addr_get_ptr(p, v);
|
||||
for_array(i, temp_data) {
|
||||
i32 index = cast(i32)(temp_data[i].elem_index);
|
||||
temp_data[i].gep = lb_emit_array_epi(p, dst_ptr, index);
|
||||
}
|
||||
|
||||
lb_build_addr_compound_lit_assign_array(p, temp_data);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case Type_DynamicArray: {
|
||||
if (cl->elems.count == 0) {
|
||||
break;
|
||||
|
||||
@@ -1567,16 +1567,27 @@ gb_internal lbValue lb_emit_array_epi(lbProcedure *p, lbValue s, isize index) {
|
||||
Type *t = s.type;
|
||||
GB_ASSERT(is_type_pointer(t));
|
||||
Type *st = base_type(type_deref(t));
|
||||
GB_ASSERT_MSG(is_type_array(st) || is_type_enumerated_array(st) || is_type_matrix(st), "%s", type_to_string(st));
|
||||
|
||||
GB_ASSERT(0 <= index);
|
||||
if (is_type_fixed_capacity_dynamic_array(st)) {
|
||||
lbValue data = lb_emit_struct_ep(p, s, 0);
|
||||
return lb_emit_epi(p, data, index);
|
||||
}
|
||||
|
||||
GB_ASSERT_MSG(is_type_array(st) || is_type_enumerated_array(st) || is_type_matrix(st), "%s", type_to_string(st));
|
||||
return lb_emit_epi(p, s, index);
|
||||
}
|
||||
gb_internal lbValue lb_emit_array_epi(lbModule *m, lbValue s, isize index) {
|
||||
Type *t = s.type;
|
||||
GB_ASSERT(is_type_pointer(t));
|
||||
Type *st = base_type(type_deref(t));
|
||||
GB_ASSERT_MSG(is_type_array(st) || is_type_enumerated_array(st) || is_type_matrix(st), "%s", type_to_string(st));
|
||||
GB_ASSERT(0 <= index);
|
||||
if (is_type_fixed_capacity_dynamic_array(st)) {
|
||||
lbValue data = lb_emit_epi(m, s, 0);
|
||||
return lb_emit_epi(m, data, index);
|
||||
}
|
||||
|
||||
GB_ASSERT_MSG(is_type_array(st) || is_type_enumerated_array(st) || is_type_matrix(st), "%s", type_to_string(st));
|
||||
return lb_emit_epi(m, s, index);
|
||||
}
|
||||
|
||||
|
||||
@@ -1109,13 +1109,13 @@ gb_internal Type *alloc_type_fixed_capacity_dynamic_array(Type *elem, i64 capaci
|
||||
t->FixedCapacityDynamicArray.elem = elem;
|
||||
t->FixedCapacityDynamicArray.capacity = capacity;
|
||||
t->FixedCapacityDynamicArray.generic_capacity = generic_capacity;
|
||||
t->FixedCapacityDynamicArray.padding_needed = 0;
|
||||
t->FixedCapacityDynamicArray.padding_needed = -1;
|
||||
return t;
|
||||
}
|
||||
Type *t = alloc_type(Type_FixedCapacityDynamicArray);
|
||||
t->FixedCapacityDynamicArray.elem = elem;
|
||||
t->FixedCapacityDynamicArray.capacity = capacity;
|
||||
t->FixedCapacityDynamicArray.padding_needed = 0;
|
||||
t->FixedCapacityDynamicArray.padding_needed = -1;
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -4513,9 +4513,6 @@ gb_internal i64 type_size_of_internal(Type *t, TypePath *path) {
|
||||
size = align_formula(size, build_context.int_size);
|
||||
|
||||
i64 padding = size - old_size;
|
||||
if (t->FixedCapacityDynamicArray.padding_needed >= 0) {
|
||||
GB_ASSERT(t->FixedCapacityDynamicArray.padding_needed == padding);
|
||||
}
|
||||
t->FixedCapacityDynamicArray.padding_needed = padding;
|
||||
|
||||
size += 1*build_context.int_size;
|
||||
|
||||
Reference in New Issue
Block a user