diff options
| author | gingerBill <bill@gingerbill.org> | 2019-11-21 00:07:21 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2019-11-21 00:07:21 +0000 |
| commit | 2c5a84bb78b981b61c55e4a5afba303bfe2d8e01 (patch) | |
| tree | bd3fc46495a330ca9496e86eb546b84b9bd37286 /src | |
| parent | e01d8a04a959e4cb1fa99cb5591b09a0d254a571 (diff) | |
`#soa[]Type` (Experimental)
Diffstat (limited to 'src')
| -rw-r--r-- | src/check_expr.cpp | 26 | ||||
| -rw-r--r-- | src/check_type.cpp | 119 | ||||
| -rw-r--r-- | src/entity.cpp | 6 | ||||
| -rw-r--r-- | src/ir.cpp | 227 | ||||
| -rw-r--r-- | src/types.cpp | 6 |
5 files changed, 354 insertions, 30 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 85ac17300..54ac12aac 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -92,6 +92,10 @@ bool abi_compat_return_by_pointer(gbAllocator a, ProcCallingConvention cc, Type void set_procedure_abi_types(gbAllocator a, Type *type); void check_assignment_error_suggestion(CheckerContext *c, Operand *o, Type *type); + +Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem); + + Entity *entity_from_expr(Ast *expr) { expr = unparen_expr(expr); switch (expr->kind) { @@ -3228,6 +3232,8 @@ Entity *check_selector(CheckerContext *c, Operand *operand, Ast *node, Type *typ } } else if (operand->mode == Addressing_MapIndex) { operand->mode = Addressing_Value; + } else if (entity->flags & EntityFlag_SoaPtrField) { + operand->mode = Addressing_SoaVariable; } else if (sel.indirect || operand->mode != Addressing_Value || operand->mode == Addressing_SoaVariable) { operand->mode = Addressing_Variable; } else { @@ -6739,7 +6745,7 @@ void check_expr_with_type_hint(CheckerContext *c, Operand *o, Ast *e, Type *t) { } } -bool check_set_index_data(Operand *o, Type *t, bool indirection, i64 *max_count) { +bool check_set_index_data(Operand *o, Type *t, bool indirection, i64 *max_count, Type *original_type) { switch (t->kind) { case Type_Basic: if (t->Basic.kind == Basic_string) { @@ -6796,6 +6802,15 @@ bool check_set_index_data(Operand *o, Type *t, bool indirection, i64 *max_count) return false; } + if (is_type_pointer(original_type) && indirection) { + Type *ptr = base_type(original_type); + if (ptr->kind == Type_Pointer && o->mode == Addressing_SoaVariable) { + o->type = ptr->Pointer.elem; + o->mode = Addressing_Value; + return true; + } + } + return false; } @@ -7973,7 +7988,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type } i64 max_count = -1; - bool valid = check_set_index_data(o, t, is_ptr, &max_count); + bool valid = check_set_index_data(o, t, is_ptr, &max_count, o->type); if (is_const) { valid = false; @@ -8055,6 +8070,13 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type valid = true; o->type = alloc_type_slice(t->DynamicArray.elem); break; + + case Type_Struct: + if (is_type_soa_struct(t)) { + valid = true; + o->type = make_soa_struct_slice(c, nullptr, nullptr, t->Struct.soa_elem); + } + break; } if (!valid) { diff --git a/src/check_type.cpp b/src/check_type.cpp index aa5a32905..6a3a000c7 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -2702,6 +2702,108 @@ void check_map_type(CheckerContext *ctx, Type *type, Ast *node) { // error(node, "'map' types are not yet implemented"); } +Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem) { + Type *bt_elem = base_type(elem); + if (!is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) { + GB_ASSERT(elem_expr != nullptr); + + gbString str = type_to_string(elem); + error(elem_expr, "Invalid type for an #soa array, expected a struct or array of length 4 or below, got '%s'", str); + gb_string_free(str); + return alloc_type_slice(elem); + } + + Type *soa_struct = nullptr; + Scope *scope = nullptr; + + if (is_type_array(elem)) { + Type *old_array = base_type(elem); + isize field_count = old_array->Array.count; + + soa_struct = alloc_type_struct(); + soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+1); + soa_struct->Struct.tags = array_make<String>(heap_allocator(), field_count+1); + soa_struct->Struct.node = array_typ_expr; + soa_struct->Struct.soa_kind = StructSoa_Slice; + soa_struct->Struct.soa_elem = elem; + soa_struct->Struct.soa_count = 0; + + scope = create_scope(ctx->scope, ctx->allocator); + soa_struct->Struct.scope = scope; + + String params_xyzw[4] = { + str_lit("x"), + str_lit("y"), + str_lit("z"), + str_lit("w") + }; + + for (i64 i = 0; i < field_count; i++) { + Type *array_type = alloc_type_pointer(old_array->Array.elem); + Token token = {}; + token.string = params_xyzw[i]; + + Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i); + new_field->flags |= EntityFlag_SoaPtrField; + soa_struct->Struct.fields[i] = new_field; + add_entity(ctx->checker, scope, nullptr, new_field); + add_entity_use(ctx, nullptr, new_field); + } + + Entity *len_field = alloc_entity_field(scope, empty_token, t_int, false, cast(i32)field_count); + soa_struct->Struct.fields[field_count] = len_field; + add_entity(ctx->checker, scope, nullptr, len_field); + add_entity_use(ctx, nullptr, len_field); + + + } else { + GB_ASSERT(is_type_struct(elem)); + + Type *old_struct = base_type(elem); + isize field_count = old_struct->Struct.fields.count; + + soa_struct = alloc_type_struct(); + soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+1); + soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_struct->Struct.tags.count+1); + soa_struct->Struct.node = array_typ_expr; + soa_struct->Struct.soa_kind = StructSoa_Slice; + soa_struct->Struct.soa_elem = elem; + soa_struct->Struct.soa_count = 0; + + scope = create_scope(old_struct->Struct.scope->parent, ctx->allocator); + soa_struct->Struct.scope = scope; + + for_array(i, old_struct->Struct.fields) { + Entity *old_field = old_struct->Struct.fields[i]; + if (old_field->kind == Entity_Variable) { + Type *array_type = alloc_type_pointer(old_field->type); + Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_src_index); + new_field->flags |= EntityFlag_SoaPtrField; + soa_struct->Struct.fields[i] = new_field; + add_entity(ctx->checker, scope, nullptr, new_field); + } else { + soa_struct->Struct.fields[i] = old_field; + } + + soa_struct->Struct.tags[i] = old_struct->Struct.tags[i]; + } + + Entity *len_field = alloc_entity_field(scope, empty_token, t_int, false, cast(i32)field_count); + soa_struct->Struct.fields[field_count] = len_field; + add_entity(ctx->checker, scope, nullptr, len_field); + add_entity_use(ctx, nullptr, len_field); + } + + Token token = {}; + token.string = str_lit("Base_Type"); + Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved); + add_entity(ctx->checker, scope, nullptr, base_type_entity); + + add_type_info_type(ctx, soa_struct); + + return soa_struct; +} + bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_type) { @@ -2983,14 +3085,27 @@ bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_t *type = alloc_type_simd_vector(count, elem); } else { - GB_PANIC("Unhandled array type tag %.*s", LIT(name)); + error(at->tag, "Invalid tag applied to array, got #%.*s", LIT(name)); + *type = alloc_type_array(elem, count, generic_type); } } else { *type = alloc_type_array(elem, count, generic_type); } } else { Type *elem = check_type(ctx, at->elem); - *type = alloc_type_slice(elem); + + if (at->tag != nullptr) { + GB_ASSERT(at->tag->kind == Ast_BasicDirective); + String name = at->tag->BasicDirective.name; + if (name == "soa") { + *type = make_soa_struct_slice(ctx, e, at->elem, elem); + } else { + error(at->tag, "Invalid tag applied to array, got #%.*s", LIT(name)); + *type = alloc_type_slice(elem); + } + } else { + *type = alloc_type_slice(elem); + } } array_end: set_base_type(named_type, *type); diff --git a/src/entity.cpp b/src/entity.cpp index bbea68e8b..c433d98cb 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -51,8 +51,10 @@ enum EntityFlag { EntityFlag_ImplicitReference = 1<<17, // NOTE(bill): equivalent to `const &` in C++ - EntityFlag_CVarArg = 1<<20, - EntityFlag_AutoCast = 1<<21, + EntityFlag_SoaPtrField = 1<<19, // to allow s.x[0] where `s.x` is a pointer rather than a slice + + EntityFlag_CVarArg = 1<<21, + EntityFlag_AutoCast = 1<<22, }; enum EntityState { diff --git a/src/ir.cpp b/src/ir.cpp index 3343021f4..89f570659 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -3447,6 +3447,73 @@ irValue *ir_insert_dynamic_map_key_and_value(irProcedure *proc, irValue *addr, T +irValue *ir_soa_struct_len(irProcedure *proc, irValue *value) { + Type *t = base_type(ir_type(value)); + bool is_ptr = false; + if (is_type_pointer(t)) { + is_ptr = true; + t = base_type(type_deref(t)); + } + + + if (t->Struct.soa_kind == StructSoa_Fixed) { + return ir_const_int(t->Struct.soa_count); + } + + GB_ASSERT(t->Struct.soa_kind == StructSoa_Slice || + t->Struct.soa_kind == StructSoa_Dynamic); + + isize n = 0; + Type *elem = base_type(t->Struct.soa_elem); + if (elem->kind == Type_Struct) { + n = elem->Struct.fields.count; + } else if (elem->kind == Type_Array) { + n = elem->Array.count; + } else { + GB_PANIC("Unreachable"); + } + + if (is_ptr) { + irValue *v = ir_emit_struct_ep(proc, value, cast(i32)n); + return ir_emit_load(proc, v); + } + return ir_emit_struct_ev(proc, value, cast(i32)n); +} + +irValue *ir_soa_struct_cap(irProcedure *proc, irValue *value) { + Type *t = base_type(ir_type(value)); + + bool is_ptr = false; + if (is_type_pointer(t)) { + is_ptr = true; + t = base_type(type_deref(t)); + } + + if (t->Struct.soa_kind == StructSoa_Fixed) { + return ir_const_int(t->Struct.soa_count); + } + + GB_ASSERT(t->Struct.soa_kind == StructSoa_Dynamic); + + isize n = 0; + Type *elem = base_type(t->Struct.soa_elem); + if (elem->kind == Type_Struct) { + n = elem->Struct.fields.count+1; + } else if (elem->kind == Type_Array) { + n = elem->Array.count+1; + } else { + GB_PANIC("Unreachable"); + } + + if (is_ptr) { + irValue *v = ir_emit_struct_ep(proc, value, cast(i32)n); + return ir_emit_load(proc, v); + } + return ir_emit_struct_ev(proc, value, cast(i32)n); +} + + + void ir_addr_store(irProcedure *proc, irAddr const &addr, irValue *value) { if (addr.addr == nullptr) { return; @@ -3568,7 +3635,7 @@ void ir_addr_store(irProcedure *proc, irAddr const &addr, irValue *value) { value = ir_emit_conv(proc, value, t->Struct.soa_elem); irValue *index = addr.soa.index; - if (index->kind != irValue_Constant) { + if (index->kind != irValue_Constant || t->Struct.soa_kind != StructSoa_Fixed) { Type *t = base_type(type_deref(ir_type(addr.addr))); GB_ASSERT(t->kind == Type_Struct && t->Struct.soa_kind != StructSoa_None); i64 count = t->Struct.soa_count; @@ -3697,27 +3764,53 @@ irValue *ir_addr_load(irProcedure *proc, irAddr const &addr) { t = base_type(t); GB_ASSERT(t->kind == Type_Struct && t->Struct.soa_kind != StructSoa_None); Type *elem = t->Struct.soa_elem; - i32 count = cast(i32)t->Struct.soa_count; + + irValue *len = nullptr; + if (t->Struct.soa_kind == StructSoa_Fixed) { + len = ir_const_int(t->Struct.soa_count); + } else { + irValue *v = ir_emit_load(proc, addr.addr); + len = ir_soa_struct_len(proc, v); + } irValue *res = ir_add_local_generated(proc, elem, true); - if (addr.soa.index->kind != irValue_Constant) { - irValue *len = ir_const_int(count); + if (addr.soa.index->kind != irValue_Constant || t->Struct.soa_kind != StructSoa_Fixed) { ir_emit_bounds_check(proc, ast_token(addr.soa.index_expr), addr.soa.index, len); } - for_array(i, t->Struct.fields) { - Entity *field = t->Struct.fields[i]; - Type *base_type = field->type; - GB_ASSERT(base_type->kind == Type_Array); - Type *elem = base_type->Array.elem; + if (t->Struct.soa_kind == StructSoa_Fixed) { + for_array(i, t->Struct.fields) { + Entity *field = t->Struct.fields[i]; + Type *base_type = field->type; + GB_ASSERT(base_type->kind == Type_Array); + irValue *dst = ir_emit_struct_ep(proc, res, cast(i32)i); + irValue *src_ptr = ir_emit_struct_ep(proc, addr.addr, cast(i32)i); + src_ptr = ir_emit_array_ep(proc, src_ptr, addr.soa.index); + irValue *src = ir_emit_load(proc, src_ptr); + ir_emit_store(proc, dst, src); + } + } else { + isize field_count = t->Struct.fields.count; + if (t->Struct.soa_kind == StructSoa_Slice) { + field_count -= 1; + } else if (t->Struct.soa_kind == StructSoa_Dynamic) { + field_count -= 3; + } + for (isize i = 0; i < field_count; i++) { + Entity *field = t->Struct.fields[i]; + Type *base_type = field->type; + GB_ASSERT(base_type->kind == Type_Pointer); + Type *elem = base_type->Pointer.elem; - irValue *dst = ir_emit_struct_ep(proc, res, cast(i32)i); - irValue *src_ptr = ir_emit_struct_ep(proc, addr.addr, cast(i32)i); - src_ptr = ir_emit_array_ep(proc, src_ptr, addr.soa.index); - irValue *src = ir_emit_load(proc, src_ptr); - ir_emit_store(proc, dst, src); + irValue *dst = ir_emit_struct_ep(proc, res, cast(i32)i); + irValue *src_ptr = ir_emit_struct_ep(proc, addr.addr, cast(i32)i); + src_ptr = ir_emit_ptr_offset(proc, src_ptr, addr.soa.index); + irValue *src = ir_emit_load(proc, src_ptr); + src = ir_emit_load(proc, src); + ir_emit_store(proc, dst, src); + } } return ir_emit_load(proc, res); @@ -3796,6 +3889,7 @@ irValue *ir_map_cap(irProcedure *proc, irValue *value) { + struct irLoopData { irValue *idx_addr; irValue *idx; @@ -6297,6 +6391,8 @@ irValue *ir_build_builtin_proc(irProcedure *proc, Ast *expr, TypeAndValue tv, Bu return ir_dynamic_array_len(proc, v); } else if (is_type_map(t)) { return ir_map_len(proc, v); + } else if (is_type_soa_struct(t)) { + return ir_soa_struct_len(proc, v); } GB_PANIC("Unreachable"); @@ -6321,6 +6417,8 @@ irValue *ir_build_builtin_proc(irProcedure *proc, Ast *expr, TypeAndValue tv, Bu return ir_dynamic_array_cap(proc, v); } else if (is_type_map(t)) { return ir_map_cap(proc, v); + } else if (is_type_soa_struct(t)) { + return ir_soa_struct_cap(proc, v); } GB_PANIC("Unreachable"); @@ -7557,15 +7655,21 @@ irAddr ir_build_addr(irProcedure *proc, Ast *expr) { irValue *arr = ir_emit_struct_ep(proc, addr.addr, first_index); - if (addr.soa.index->kind != irValue_Constant) { - Type *t = base_type(type_deref(ir_type(addr.addr))); - GB_ASSERT(t->kind == Type_Struct && t->Struct.soa_kind != StructSoa_None); - i64 count = t->Struct.soa_count; - irValue *len = ir_const_int(count); + Type *t = base_type(type_deref(ir_type(addr.addr))); + GB_ASSERT(is_type_soa_struct(t)); + + if (addr.soa.index->kind != irValue_Constant || t->Struct.soa_kind != StructSoa_Fixed) { + irValue *len = ir_soa_struct_len(proc, addr.addr); ir_emit_bounds_check(proc, ast_token(addr.soa.index_expr), addr.soa.index, len); } - irValue *item = ir_emit_array_ep(proc, arr, index); + irValue *item = nullptr; + + if (t->Struct.soa_kind == StructSoa_Fixed) { + item = ir_emit_array_ep(proc, arr, index); + } else { + item = ir_emit_load(proc, ir_emit_ptr_offset(proc, arr, index)); + } if (sub_sel.index.count > 0) { item = ir_emit_deep_field_gep(proc, item, sub_sel); } @@ -7626,10 +7730,8 @@ irAddr ir_build_addr(irProcedure *proc, Ast *expr) { bool deref = is_type_pointer(t); t = base_type(type_deref(t)); - if (t->kind == Type_Struct && t->Struct.soa_kind != StructSoa_None) { + if (is_type_soa_struct(t)) { // SOA STRUCTURES!!!! - Type *elem = t->Struct.soa_elem; - irValue *val = ir_build_addr_ptr(proc, ie->expr); if (deref) { val = ir_emit_load(proc, val); @@ -7639,6 +7741,36 @@ irAddr ir_build_addr(irProcedure *proc, Ast *expr) { return ir_addr_soa_variable(val, index, ie->index); } + if (ie->expr->tav.mode == Addressing_SoaVariable) { + // SOA Structures for slices/dynamic arrays + GB_ASSERT(is_type_pointer(type_of_expr(ie->expr))); + + irValue *field = ir_build_expr(proc, ie->expr); + irValue *index = ir_build_expr(proc, ie->index); + + + if (!build_context.no_bounds_check) { + // TODO HACK(bill): Clean up this hack to get the length for bounds checking + GB_ASSERT(field->kind == irValue_Instr); + irInstr *instr = &field->Instr; + + GB_ASSERT(instr->kind == irInstr_Load); + irValue *a = instr->Load.address; + + GB_ASSERT(a->kind == irValue_Instr); + irInstr *b = &a->Instr; + GB_ASSERT(b->kind == irInstr_StructElementPtr); + irValue *base_struct = b->StructElementPtr.address; + + GB_ASSERT(is_type_soa_struct(type_deref(ir_type(base_struct)))); + irValue *len = ir_soa_struct_len(proc, base_struct); + ir_emit_bounds_check(proc, ast_token(ie->index), index, len); + } + + irValue *val = ir_emit_ptr_offset(proc, field, index); + return ir_addr(val); + } + GB_ASSERT_MSG(is_type_indexable(t), "%s %s", type_to_string(t), expr_to_string(expr)); if (is_type_map(t)) { @@ -7838,6 +7970,55 @@ irAddr ir_build_addr(irProcedure *proc, Ast *expr) { ir_fill_string(proc, str, elem, new_len); return ir_addr(str); } + + + case Type_Struct: + if (is_type_soa_struct(type)) { + irValue *len = ir_soa_struct_len(proc, addr); + if (high == nullptr) high = len; + + if (!no_indices) { + ir_emit_slice_bounds_check(proc, se->open, low, high, len, se->low != nullptr); + } + + irValue *dst = ir_add_local_generated(proc, type_of_expr(expr), true); + if (type->Struct.soa_kind == StructSoa_Fixed) { + i32 field_count = cast(i32)type->Struct.fields.count; + for (i32 i = 0; i < field_count; i++) { + irValue *field_dst = ir_emit_struct_ep(proc, dst, i); + irValue *field_src = ir_emit_struct_ep(proc, addr, i); + field_src = ir_emit_array_ep(proc, field_src, low); + ir_emit_store(proc, field_dst, field_src); + } + + irValue *len_dst = ir_emit_struct_ep(proc, dst, field_count); + irValue *new_len = ir_emit_arith(proc, Token_Sub, high, low, t_int); + ir_emit_store(proc, len_dst, new_len); + } else if (type->Struct.soa_kind == StructSoa_Slice) { + if (no_indices) { + ir_emit_store(proc, dst, base); + } else { + i32 field_count = cast(i32)type->Struct.fields.count - 1; + for (i32 i = 0; i < field_count; i++) { + irValue *field_dst = ir_emit_struct_ep(proc, dst, i); + irValue *field_src = ir_emit_struct_ev(proc, base, i); + field_src = ir_emit_ptr_offset(proc, field_src, low); + ir_emit_store(proc, field_dst, field_src); + } + + + irValue *len_dst = ir_emit_struct_ep(proc, dst, field_count); + irValue *new_len = ir_emit_arith(proc, Token_Sub, high, low, t_int); + ir_emit_store(proc, len_dst, new_len); + } + } else { + GB_PANIC("TODO #soa[dynamic]T"); + } + + return ir_addr(dst); + } + break; + } GB_PANIC("Unknown slicable type"); diff --git a/src/types.cpp b/src/types.cpp index 1d89f6e17..63647dcf3 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -811,6 +811,7 @@ Type *alloc_type_simd_vector(i64 count, Type *elem) { + //////////////////////////////////////////////////////////////// @@ -1757,7 +1758,10 @@ bool are_types_identical(Type *x, Type *y) { if (x->Struct.is_raw_union == y->Struct.is_raw_union && x->Struct.fields.count == y->Struct.fields.count && x->Struct.is_packed == y->Struct.is_packed && - x->Struct.custom_align == y->Struct.custom_align) { + x->Struct.custom_align == y->Struct.custom_align && + x->Struct.soa_kind == y->Struct.soa_kind && + x->Struct.soa_count == y->Struct.soa_count && + are_types_identical(x->Struct.soa_elem, y->Struct.soa_elem)) { // TODO(bill); Fix the custom alignment rule for_array(i, x->Struct.fields) { Entity *xf = x->Struct.fields[i]; |