From be68bf9f26122b764a43cf61369ca54c203d1df3 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 13 Sep 2021 11:29:46 +0100 Subject: [PATCH] Only store `field_index` remove `field_src_index` (for the time being) --- src/check_builtin.cpp | 2 +- src/check_type.cpp | 2 +- src/entity.cpp | 13 +++++-------- src/llvm_backend_expr.cpp | 2 +- src/llvm_backend_utility.cpp | 20 ++++++++++++-------- src/types.cpp | 2 +- 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 96feb6701..399de98a0 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1989,7 +1989,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 Entity *old_field = old_struct->Struct.fields[i]; if (old_field->kind == Entity_Variable) { Type *array_type = alloc_type_array(old_field->type, count); - Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_src_index); + Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_index); soa_struct->Struct.fields[i] = new_field; add_entity(c, scope, nullptr, new_field); } else { diff --git a/src/check_type.cpp b/src/check_type.cpp index b80d6c05e..8d129eb68 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -2317,7 +2317,7 @@ Type *make_soa_struct_internal(CheckerContext *ctx, Ast *array_typ_expr, Ast *el } else { field_type = alloc_type_pointer(old_field->type); } - Entity *new_field = alloc_entity_field(scope, old_field->token, field_type, false, old_field->Variable.field_src_index); + Entity *new_field = alloc_entity_field(scope, old_field->token, field_type, false, old_field->Variable.field_index); soa_struct->Struct.fields[i] = new_field; add_entity(ctx, scope, nullptr, new_field); add_entity_use(ctx, nullptr, new_field); diff --git a/src/entity.cpp b/src/entity.cpp index 8343ba557..11954113d 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -155,8 +155,7 @@ struct Entity { } Constant; struct { Ast *init_expr; // only used for some variables within procedure bodies - i32 field_index; - i32 field_src_index; + i32 field_index; ParameterValue param_value; Ast * param_expr; @@ -319,20 +318,18 @@ Entity *alloc_entity_const_param(Scope *scope, Token token, Type *type, ExactVal } -Entity *alloc_entity_field(Scope *scope, Token token, Type *type, bool is_using, i32 field_src_index, EntityState state = EntityState_Unresolved) { +Entity *alloc_entity_field(Scope *scope, Token token, Type *type, bool is_using, i32 field_index, EntityState state = EntityState_Unresolved) { Entity *entity = alloc_entity_variable(scope, token, type); - entity->Variable.field_src_index = field_src_index; - entity->Variable.field_index = field_src_index; + entity->Variable.field_index = field_index; if (is_using) entity->flags |= EntityFlag_Using; entity->flags |= EntityFlag_Field; entity->state = state; return entity; } -Entity *alloc_entity_array_elem(Scope *scope, Token token, Type *type, i32 field_src_index) { +Entity *alloc_entity_array_elem(Scope *scope, Token token, Type *type, i32 field_index) { Entity *entity = alloc_entity_variable(scope, token, type); - entity->Variable.field_src_index = field_src_index; - entity->Variable.field_index = field_src_index; + entity->Variable.field_index = field_index; entity->flags |= EntityFlag_Field; entity->flags |= EntityFlag_ArrayElem; entity->state = EntityState_Resolved; diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index efd0eaf40..a34e98f2b 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -3259,7 +3259,7 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) { TypeAndValue tav = type_and_value_of_expr(elem); } else { TypeAndValue tav = type_and_value_of_expr(elem); - Selection sel = lookup_field_from_index(bt, st->fields[field_index]->Variable.field_src_index); + Selection sel = lookup_field_from_index(bt, st->fields[field_index]->Variable.field_index); index = sel.index[0]; } diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index db3cb443e..63e27f428 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -807,6 +807,13 @@ lbValue lb_address_from_load(lbProcedure *p, lbValue value) { return {}; } +i32 lb_convert_struct_index(Type *t, i32 index) { + if (t->kind == Type_Struct && t->Struct.custom_align != 0) { + index += 1; + } + return index; +} + lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) { GB_ASSERT(is_type_pointer(s.type)); Type *t = base_type(type_deref(s.type)); @@ -883,10 +890,9 @@ lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) { } GB_ASSERT_MSG(result_type != nullptr, "%s %d", type_to_string(t), index); - - if (t->kind == Type_Struct && t->Struct.custom_align != 0) { - index += 1; - } + + index = lb_convert_struct_index(t, index); + if (lb_is_const(s)) { lbModule *m = p->module; lbValue res = {}; @@ -1006,10 +1012,8 @@ lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index) { } GB_ASSERT_MSG(result_type != nullptr, "%s, %d", type_to_string(s.type), index); - - if (t->kind == Type_Struct && t->Struct.custom_align != 0) { - index += 1; - } + + index = lb_convert_struct_index(t, index); lbValue res = {}; res.value = LLVMBuildExtractValue(p->builder, s.value, cast(unsigned)index, ""); diff --git a/src/types.cpp b/src/types.cpp index 23834bfc1..8eb505287 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -2432,7 +2432,7 @@ Selection lookup_field_from_index(Type *type, i64 index) { for (isize i = 0; i < max_count; i++) { Entity *f = type->Struct.fields[i]; if (f->kind == Entity_Variable) { - if (f->Variable.field_src_index == index) { + if (f->Variable.field_index == index) { auto sel_array = array_make(a, 1); sel_array[0] = cast(i32)i; return make_selection(f, sel_array, false);