From fb8fa5217d4a5081dacc0a74a786cd2efc964fdb Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 13 Sep 2021 00:58:39 +0100 Subject: Begin minimize `Type` size by replacing `Array` with `Slice` etc --- src/types.cpp | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) (limited to 'src/types.cpp') diff --git a/src/types.cpp b/src/types.cpp index 37a05d5a6..4362ae45b 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -129,9 +129,9 @@ enum StructSoaKind { }; struct TypeStruct { - Array fields; - Array tags; - Array offsets; + Slice fields; + Slice tags; + Slice offsets; Ast * node; Scope * scope; @@ -145,12 +145,12 @@ struct TypeStruct { i64 soa_count; StructSoaKind soa_kind; - bool are_offsets_set; - bool are_offsets_being_processed; - bool is_packed; - bool is_raw_union; bool is_polymorphic; - bool is_poly_specialized; + bool are_offsets_set : 1; + bool are_offsets_being_processed : 1; + bool is_packed : 1; + bool is_raw_union : 1; + bool is_poly_specialized : 1; }; struct TypeUnion { @@ -216,8 +216,8 @@ struct TypeProc { TYPE_KIND(EnumeratedArray, struct { \ Type *elem; \ Type *index; \ - ExactValue min_value; \ - ExactValue max_value; \ + ExactValue *min_value; \ + ExactValue *max_value; \ i64 count; \ TokenKind op; \ }) \ @@ -239,14 +239,14 @@ struct TypeProc { Scope * scope; \ Entity * names; \ Type * base_type; \ - ExactValue min_value; \ - ExactValue max_value; \ + ExactValue *min_value; \ + ExactValue *max_value; \ isize min_value_index; \ isize max_value_index; \ }) \ TYPE_KIND(Tuple, struct { \ - Array variables; /* Entity_Variable */ \ - Array offsets; \ + Slice variables; /* Entity_Variable */ \ + Slice offsets; \ bool are_offsets_being_processed; \ bool are_offsets_set; \ bool is_packed; \ @@ -803,15 +803,17 @@ Type *alloc_type_array(Type *elem, i64 count, Type *generic_count = nullptr) { return t; } -Type *alloc_type_enumerated_array(Type *elem, Type *index, ExactValue min_value, ExactValue max_value, TokenKind op) { +Type *alloc_type_enumerated_array(Type *elem, Type *index, ExactValue const *min_value, ExactValue const *max_value, TokenKind op) { Type *t = alloc_type(Type_EnumeratedArray); t->EnumeratedArray.elem = elem; t->EnumeratedArray.index = index; - t->EnumeratedArray.min_value = min_value; - t->EnumeratedArray.max_value = max_value; + t->EnumeratedArray.min_value = gb_alloc_item(permanent_allocator(), ExactValue); + t->EnumeratedArray.max_value = gb_alloc_item(permanent_allocator(), ExactValue); + gb_memmove(t->EnumeratedArray.min_value, min_value, gb_size_of(ExactValue)); + gb_memmove(t->EnumeratedArray.max_value, max_value, gb_size_of(ExactValue)); t->EnumeratedArray.op = op; - t->EnumeratedArray.count = 1 + exact_value_to_i64(exact_value_sub(max_value, min_value)); + t->EnumeratedArray.count = 1 + exact_value_to_i64(exact_value_sub(*max_value, *min_value)); return t; } @@ -841,6 +843,8 @@ Type *alloc_type_union() { Type *alloc_type_enum() { Type *t = alloc_type(Type_Enum); + t->Enum.min_value = gb_alloc_item(permanent_allocator(), ExactValue); + t->Enum.max_value = gb_alloc_item(permanent_allocator(), ExactValue); return t; } @@ -3080,9 +3084,9 @@ i64 type_align_of_internal(Type *t, TypePath *path) { return gb_clamp(next_pow2(type_size_of_internal(t, path)), 1, build_context.word_size); } -Array type_set_offsets_of(Array const &fields, bool is_packed, bool is_raw_union) { +Slice type_set_offsets_of(Slice const &fields, bool is_packed, bool is_raw_union) { gbAllocator a = permanent_allocator(); - auto offsets = array_make(a, fields.count); + auto offsets = slice_make(a, fields.count); i64 curr_offset = 0; if (is_raw_union) { for_array(i, fields) { @@ -3463,7 +3467,7 @@ Type *reduce_tuple_to_single_type(Type *original_type) { Type *alloc_type_struct_from_field_types(Type **field_types, isize field_count, bool is_packed) { Type *t = alloc_type_struct(); - t->Struct.fields = array_make(heap_allocator(), field_count); + t->Struct.fields = slice_make(heap_allocator(), field_count); Scope *scope = nullptr; for_array(i, t->Struct.fields) { @@ -3483,7 +3487,7 @@ Type *alloc_type_tuple_from_field_types(Type **field_types, isize field_count, b } Type *t = alloc_type_tuple(); - t->Tuple.variables = array_make(heap_allocator(), field_count); + t->Tuple.variables = slice_make(heap_allocator(), field_count); Scope *scope = nullptr; for_array(i, t->Tuple.variables) { -- cgit v1.2.3 From f5bc95eb349c75c8378a0a35104fd763db7742a1 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 13 Sep 2021 01:07:24 +0100 Subject: More culling --- src/check_type.cpp | 13 ++---------- src/main.cpp | 5 +++++ src/types.cpp | 60 ++++++++++++++++++------------------------------------ 3 files changed, 27 insertions(+), 51 deletions(-) (limited to 'src/types.cpp') diff --git a/src/check_type.cpp b/src/check_type.cpp index 2c9f589c7..32a40de12 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -188,13 +188,6 @@ void check_struct_fields(CheckerContext *ctx, Ast *node, Slice *fields } -Entity *make_names_field_for_struct(CheckerContext *ctx, Scope *scope) { - Entity *e = alloc_entity_field(scope, make_token_ident(str_lit("names")), t_string_slice, false, 0); - e->flags |= EntityFlag_TypeField; - e->flags |= EntityFlag_Value; - return e; -} - bool check_custom_align(CheckerContext *ctx, Ast *node, i64 *align_) { GB_ASSERT(align_ != nullptr); Operand o = {}; @@ -565,8 +558,7 @@ void check_struct_type(CheckerContext *ctx, Type *struct_type, Ast *node, Array< case_end; } } - struct_type->Struct.names = make_names_field_for_struct(ctx, ctx->scope); - + scope_reserve(ctx->scope, min_field_count); if (st->is_raw_union && min_field_count > 1) { @@ -658,7 +650,7 @@ void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, ArrayUnion.variants = variants; + union_type->Union.variants = slice_from_array(variants); union_type->Union.no_nil = ut->no_nil; union_type->Union.maybe = ut->maybe; if (union_type->Union.no_nil) { @@ -818,7 +810,6 @@ void check_enum_type(CheckerContext *ctx, Type *enum_type, Type *named_type, Ast enum_type->Enum.fields = fields; - enum_type->Enum.names = make_names_field_for_struct(ctx, ctx->scope); *enum_type->Enum.min_value = min_value; *enum_type->Enum.max_value = max_value; diff --git a/src/main.cpp b/src/main.cpp index b38a23b5f..d9630a38a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2221,6 +2221,11 @@ int strip_semicolons(Parser *parser) { int main(int arg_count, char const **arg_ptr) { #define TIME_SECTION(str) do { debugf("[Section] %s\n", str); timings_start_section(&global_timings, str_lit(str)); } while (0) + #define TYPE_KIND(k, ...) gb_printf("%s %td\n", #k, sizeof(Type##k)); + TYPE_KINDS + #undef TYPE_KIND + gb_printf("Type %td\n", sizeof(Type)); + if (arg_count < 2) { usage(make_string_c(arg_ptr[0])); return 1; diff --git a/src/types.cpp b/src/types.cpp index 4362ae45b..47a949699 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -121,7 +121,7 @@ struct BasicType { String name; }; -enum StructSoaKind { +enum StructSoaKind : u8 { StructSoa_None = 0, StructSoa_Fixed = 1, StructSoa_Slice = 2, @@ -135,38 +135,37 @@ struct TypeStruct { Ast * node; Scope * scope; - Type * polymorphic_params; // Type_Tuple - Type * polymorphic_parent; + i64 custom_align; + Type * polymorphic_params; // Type_Tuple + Type * polymorphic_parent; - i64 custom_align; - Entity * names; - Type * soa_elem; - i64 soa_count; - StructSoaKind soa_kind; + Type * soa_elem; + i64 soa_count; + StructSoaKind soa_kind; - bool is_polymorphic; - bool are_offsets_set : 1; - bool are_offsets_being_processed : 1; - bool is_packed : 1; - bool is_raw_union : 1; - bool is_poly_specialized : 1; + bool is_polymorphic; + bool are_offsets_set : 1; + bool are_offsets_being_processed : 1; + bool is_packed : 1; + bool is_raw_union : 1; + bool is_poly_specialized : 1; }; struct TypeUnion { - Array variants; + Slice variants; Ast * node; Scope * scope; i64 variant_block_size; i64 custom_align; - i64 tag_size; Type * polymorphic_params; // Type_Tuple Type * polymorphic_parent; - bool no_nil; - bool maybe; + i16 tag_size; bool is_polymorphic; - bool is_poly_specialized; + bool is_poly_specialized : 1; + bool no_nil : 1; + bool maybe : 1; }; struct TypeProc { @@ -237,7 +236,6 @@ struct TypeProc { Array fields; \ Ast *node; \ Scope * scope; \ - Entity * names; \ Type * base_type; \ ExactValue *min_value; \ ExactValue *max_value; \ @@ -2311,7 +2309,7 @@ i64 union_tag_size(Type *u) { } } - u->Union.tag_size = gb_min3(max_align, build_context.max_align, 8); + u->Union.tag_size = cast(i16)gb_min3(max_align, build_context.max_align, 8); return u->Union.tag_size; } @@ -2478,24 +2476,6 @@ Selection lookup_field_with_selection(Type *type_, String field_name, bool is_ty type = base_type(type); if (is_type) { - switch (type->kind) { - case Type_Struct: - if (type->Struct.names != nullptr && - field_name == "names") { - sel.entity = type->Struct.names; - return sel; - } - break; - case Type_Enum: - if (type->Enum.names != nullptr && - field_name == "names") { - sel.entity = type->Enum.names; - return sel; - } - break; - } - - if (is_type_enum(type)) { // NOTE(bill): These may not have been added yet, so check in case for_array(i, type->Enum.fields) { @@ -3269,7 +3249,7 @@ i64 type_size_of_internal(Type *t, TypePath *path) { i64 tag_size = union_tag_size(t); size = align_formula(max, tag_size); // NOTE(bill): Calculate the padding between the common fields and the tag - t->Union.tag_size = tag_size; + t->Union.tag_size = cast(i16)tag_size; t->Union.variant_block_size = size - field_size; size += tag_size; -- cgit v1.2.3 From 71bffd46dc9717ded66b3db353084871a11563e6 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 13 Sep 2021 01:14:17 +0100 Subject: Reduce size of `Type` --- src/check_builtin.cpp | 8 ++++++-- src/check_type.cpp | 12 ++++++++++-- src/types.cpp | 24 ++++++++++-------------- 3 files changed, 26 insertions(+), 18 deletions(-) (limited to 'src/types.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 1f9eea45d..4e8eed1fc 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1943,7 +1943,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 soa_struct->Struct.node = operand->expr; soa_struct->Struct.soa_kind = StructSoa_Fixed; soa_struct->Struct.soa_elem = elem; - soa_struct->Struct.soa_count = count; + soa_struct->Struct.soa_count = cast(i32)count; scope = create_scope(c->info, c->scope); soa_struct->Struct.scope = scope; @@ -1976,7 +1976,11 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 soa_struct->Struct.node = operand->expr; soa_struct->Struct.soa_kind = StructSoa_Fixed; soa_struct->Struct.soa_elem = elem; - soa_struct->Struct.soa_count = count; + if (count > I32_MAX) { + count = I32_MAX; + error(call, "Array count too large for an #soa struct, got %lld", cast(long long)count); + } + soa_struct->Struct.soa_count = cast(i32)count; scope = create_scope(c->info, old_struct->Struct.scope->parent); soa_struct->Struct.scope = scope; diff --git a/src/check_type.cpp b/src/check_type.cpp index 32a40de12..55c931ab4 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -2253,7 +2253,11 @@ Type *make_soa_struct_internal(CheckerContext *ctx, Ast *array_typ_expr, Ast *el soa_struct->Struct.node = array_typ_expr; soa_struct->Struct.soa_kind = soa_kind; soa_struct->Struct.soa_elem = elem; - soa_struct->Struct.soa_count = count; + if (count > I32_MAX) { + count = I32_MAX; + error(array_typ_expr, "Array count too large for an #soa struct, got %lld", cast(long long)count); + } + soa_struct->Struct.soa_count = cast(i32)count; scope = create_scope(ctx->info, ctx->scope, 8); soa_struct->Struct.scope = scope; @@ -2295,7 +2299,11 @@ Type *make_soa_struct_internal(CheckerContext *ctx, Ast *array_typ_expr, Ast *el soa_struct->Struct.node = array_typ_expr; soa_struct->Struct.soa_kind = soa_kind; soa_struct->Struct.soa_elem = elem; - soa_struct->Struct.soa_count = count; + if (count > I32_MAX) { + count = I32_MAX; + error(array_typ_expr, "Array count too large for an #soa struct, got %lld", cast(long long)count); + } + soa_struct->Struct.soa_count = cast(i32)count; scope = create_scope(ctx->info, old_struct->Struct.scope->parent); soa_struct->Struct.scope = scope; diff --git a/src/types.cpp b/src/types.cpp index 47a949699..570851124 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -131,7 +131,8 @@ enum StructSoaKind : u8 { struct TypeStruct { Slice fields; Slice tags; - Slice offsets; + i64 * offsets; // count == fields.count + Ast * node; Scope * scope; @@ -141,7 +142,7 @@ struct TypeStruct { Type * soa_elem; - i64 soa_count; + i32 soa_count; StructSoaKind soa_kind; bool is_polymorphic; @@ -154,8 +155,10 @@ struct TypeStruct { struct TypeUnion { Slice variants; + Ast * node; Scope * scope; + i64 variant_block_size; i64 custom_align; Type * polymorphic_params; // Type_Tuple @@ -244,7 +247,7 @@ struct TypeProc { }) \ TYPE_KIND(Tuple, struct { \ Slice variables; /* Entity_Variable */ \ - Slice offsets; \ + i64 * offsets; \ bool are_offsets_being_processed; \ bool are_offsets_set; \ bool is_packed; \ @@ -3064,9 +3067,9 @@ i64 type_align_of_internal(Type *t, TypePath *path) { return gb_clamp(next_pow2(type_size_of_internal(t, path)), 1, build_context.word_size); } -Slice type_set_offsets_of(Slice const &fields, bool is_packed, bool is_raw_union) { +i64 *type_set_offsets_of(Slice const &fields, bool is_packed, bool is_raw_union) { gbAllocator a = permanent_allocator(); - auto offsets = slice_make(a, fields.count); + auto offsets = gb_alloc_array(a, i64, fields.count); i64 curr_offset = 0; if (is_raw_union) { for_array(i, fields) { @@ -3100,7 +3103,6 @@ bool type_set_offsets(Type *t) { if (!t->Struct.are_offsets_set) { t->Struct.are_offsets_being_processed = true; t->Struct.offsets = type_set_offsets_of(t->Struct.fields, t->Struct.is_packed, t->Struct.is_raw_union); - GB_ASSERT(t->Struct.offsets.count == t->Struct.fields.count); t->Struct.are_offsets_being_processed = false; t->Struct.are_offsets_set = true; return true; @@ -3285,18 +3287,12 @@ i64 type_size_of_internal(Type *t, TypePath *path) { if (path->failure) { return FAILURE_SIZE; } - if (t->Struct.are_offsets_being_processed && t->Struct.offsets.data == nullptr) { + if (t->Struct.are_offsets_being_processed && t->Struct.offsets == nullptr) { type_path_print_illegal_cycle(path, path->path.count-1); return FAILURE_SIZE; } - if (t->Struct.are_offsets_set && t->Struct.offsets.count != t->Struct.fields.count) { - // TODO(bill, 2019-04-28): Determine exactly why the offsets length is different thatn the field length - // Are the the same at some point and then the struct length is increased? - // Why is this not handled by the type cycle checker? - t->Struct.are_offsets_set = false; - } type_set_offsets(t); - GB_ASSERT_MSG(t->Struct.offsets.count == t->Struct.fields.count, "%s", type_to_string(t)); + GB_ASSERT(t->Struct.fields.count == 0 || t->Struct.offsets != nullptr); size = t->Struct.offsets[cast(isize)count-1] + type_size_of_internal(t->Struct.fields[cast(isize)count-1]->type, path); return align_formula(size, align); } -- cgit v1.2.3 From 2d7aea79b94721362f4fc5285c2a99ab37f52a58 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 13 Sep 2021 01:23:37 +0100 Subject: Make `TypeStructl.tags` a pointer from a slice (reduce memory usage) --- src/check_builtin.cpp | 6 +++--- src/check_type.cpp | 11 +++++------ src/llvm_backend_type.cpp | 2 +- src/types.cpp | 9 +-------- 4 files changed, 10 insertions(+), 18 deletions(-) (limited to 'src/types.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 4e8eed1fc..96feb6701 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1789,7 +1789,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 elem = alloc_type_struct(); elem->Struct.scope = s; elem->Struct.fields = slice_from_array(fields); - elem->Struct.tags = slice_make(permanent_allocator(), fields.count); + elem->Struct.tags = gb_alloc_array(permanent_allocator(), String, fields.count); elem->Struct.node = dummy_node_struct; type_set_offsets(elem); } @@ -1939,7 +1939,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 Type *old_array = base_type(elem); soa_struct = alloc_type_struct(); soa_struct->Struct.fields = slice_make(heap_allocator(), cast(isize)old_array->Array.count); - soa_struct->Struct.tags = slice_make(heap_allocator(), cast(isize)old_array->Array.count); + soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, cast(isize)old_array->Array.count); soa_struct->Struct.node = operand->expr; soa_struct->Struct.soa_kind = StructSoa_Fixed; soa_struct->Struct.soa_elem = elem; @@ -1972,7 +1972,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 Type *old_struct = base_type(elem); soa_struct = alloc_type_struct(); soa_struct->Struct.fields = slice_make(heap_allocator(), old_struct->Struct.fields.count); - soa_struct->Struct.tags = slice_make(heap_allocator(), old_struct->Struct.tags.count); + soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, old_struct->Struct.fields.count); soa_struct->Struct.node = operand->expr; soa_struct->Struct.soa_kind = StructSoa_Fixed; soa_struct->Struct.soa_elem = elem; diff --git a/src/check_type.cpp b/src/check_type.cpp index 55c931ab4..ccd426322 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -92,7 +92,7 @@ bool does_field_type_allow_using(Type *t) { return false; } -void check_struct_fields(CheckerContext *ctx, Ast *node, Slice *fields, Slice *tags, Slice const ¶ms, +void check_struct_fields(CheckerContext *ctx, Ast *node, Slice *fields, String **tags, Slice const ¶ms, isize init_field_capacity, Type *struct_type, String context) { auto fields_array = array_make(heap_allocator(), 0, init_field_capacity); auto tags_array = array_make(heap_allocator(), 0, init_field_capacity); @@ -184,7 +184,7 @@ void check_struct_fields(CheckerContext *ctx, Ast *node, Slice *fields } *fields = slice_from_array(fields_array); - *tags = slice_from_array(tags_array); + *tags = tags_array.data; } @@ -2234,7 +2234,7 @@ Type *make_soa_struct_internal(CheckerContext *ctx, Ast *array_typ_expr, Ast *el soa_struct = alloc_type_struct(); soa_struct->Struct.fields = slice_make(heap_allocator(), field_count+extra_field_count); - soa_struct->Struct.tags = slice_make(heap_allocator(), field_count+extra_field_count); + soa_struct->Struct.tags = gb_alloc_array(heap_allocator(), String, field_count+extra_field_count); soa_struct->Struct.node = array_typ_expr; soa_struct->Struct.soa_kind = soa_kind; soa_struct->Struct.soa_elem = elem; @@ -2249,7 +2249,7 @@ Type *make_soa_struct_internal(CheckerContext *ctx, Ast *array_typ_expr, Ast *el soa_struct = alloc_type_struct(); soa_struct->Struct.fields = slice_make(heap_allocator(), field_count+extra_field_count); - soa_struct->Struct.tags = slice_make(heap_allocator(), field_count+extra_field_count); + soa_struct->Struct.tags = gb_alloc_array(heap_allocator(), String, field_count+extra_field_count); soa_struct->Struct.node = array_typ_expr; soa_struct->Struct.soa_kind = soa_kind; soa_struct->Struct.soa_elem = elem; @@ -2291,11 +2291,10 @@ Type *make_soa_struct_internal(CheckerContext *ctx, Ast *array_typ_expr, Ast *el Type *old_struct = base_type(elem); field_count = old_struct->Struct.fields.count; - GB_ASSERT(old_struct->Struct.tags.count == field_count); soa_struct = alloc_type_struct(); soa_struct->Struct.fields = slice_make(heap_allocator(), field_count+extra_field_count); - soa_struct->Struct.tags = slice_make(heap_allocator(), field_count+extra_field_count); + soa_struct->Struct.tags = gb_alloc_array(heap_allocator(), String, field_count+extra_field_count); soa_struct->Struct.node = array_typ_expr; soa_struct->Struct.soa_kind = soa_kind; soa_struct->Struct.soa_elem = elem; diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index f5665c718..f17b8df6a 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -721,7 +721,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da lb_emit_store(p, offset, lb_const_int(m, t_uintptr, foffset)); lb_emit_store(p, is_using, lb_const_bool(m, t_bool, (f->flags&EntityFlag_Using) != 0)); - if (t->Struct.tags.count > 0) { + if (t->Struct.tags != nullptr) { String tag_string = t->Struct.tags[source_index]; if (tag_string.len > 0) { lbValue tag_ptr = lb_emit_ptr_offset(p, memory_tags, index); diff --git a/src/types.cpp b/src/types.cpp index 570851124..23834bfc1 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -130,7 +130,7 @@ enum StructSoaKind : u8 { struct TypeStruct { Slice fields; - Slice tags; + String * tags; // count == fields.count i64 * offsets; // count == fields.count Ast * node; @@ -140,7 +140,6 @@ struct TypeStruct { Type * polymorphic_params; // Type_Tuple Type * polymorphic_parent; - Type * soa_elem; i32 soa_count; StructSoaKind soa_kind; @@ -2174,12 +2173,6 @@ bool are_types_identical(Type *x, Type *y) { if (xf_is_using ^ yf_is_using) { return false; } - if (x->Struct.tags.count != y->Struct.tags.count) { - return false; - } - if (x->Struct.tags.count > 0 && x->Struct.tags[i] != y->Struct.tags[i]) { - return false; - } } return true; } -- cgit v1.2.3 From be68bf9f26122b764a43cf61369ca54c203d1df3 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 13 Sep 2021 11:29:46 +0100 Subject: 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(-) (limited to 'src/types.cpp') 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); -- cgit v1.2.3 From 526a42c6caac9bc39b9217e58c297d084c3d694a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 13 Sep 2021 16:44:01 +0100 Subject: Remove custom alignment limit --- src/check_type.cpp | 8 +------- src/types.cpp | 4 ++-- 2 files changed, 3 insertions(+), 9 deletions(-) (limited to 'src/types.cpp') diff --git a/src/check_type.cpp b/src/check_type.cpp index 8d129eb68..00a4c4ab2 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -215,13 +215,7 @@ bool check_custom_align(CheckerContext *ctx, Ast *node, i64 *align_) { error(node, "#align must be a power of 2, got %lld", align); return false; } - - // NOTE(bill): Success!!! - i64 custom_align = gb_clamp(align, 1, build_context.max_align); - if (custom_align < align) { - warning(node, "Custom alignment has been clamped to %lld from %lld", align, custom_align); - } - *align_ = custom_align; + *align_ = align; return true; } } diff --git a/src/types.cpp b/src/types.cpp index 8eb505287..7a5ea489b 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -2972,7 +2972,7 @@ i64 type_align_of_internal(Type *t, TypePath *path) { return 1; } if (t->Union.custom_align > 0) { - return gb_clamp(t->Union.custom_align, 1, build_context.max_align); + return gb_max(t->Union.custom_align, 1); } i64 max = 1; @@ -2993,7 +2993,7 @@ i64 type_align_of_internal(Type *t, TypePath *path) { case Type_Struct: { if (t->Struct.custom_align > 0) { - return gb_clamp(t->Struct.custom_align, 1, build_context.max_align); + return gb_max(t->Struct.custom_align, 1); } if (t->Struct.is_raw_union) { i64 max = 1; -- cgit v1.2.3