| ... | @@ -5507,34 +5507,48 @@ bool handle_is_ptr(CodeGen *g, ZigType *type_entry) { | ... | @@ -5507,34 +5507,48 @@ bool handle_is_ptr(CodeGen *g, ZigType *type_entry) { |
| 5507 | zig_unreachable(); | 5507 | zig_unreachable(); |
| 5508 | } | 5508 | } |
| 5509 | | 5509 | |
| 5510 | static uint32_t hash_ptr(void *ptr) { | 5510 | static const uint32_t HASH_INIT = 0x811c9dc5U; |
| 5511 | return (uint32_t)(((uintptr_t)ptr) % UINT32_MAX); | 5511 | |
| | 5512 | template<typename T> |
| | 5513 | static uint32_t hash_combine(uint32_t hash, const T *value, size_t count = 1) { |
| | 5514 | // Simple FNV32 hash |
| | 5515 | size_t len = sizeof(T) * count; |
| | 5516 | const unsigned char *char_bytes = (const unsigned char*)value; |
| | 5517 | for (size_t c = 0; c < len; ++c) { |
| | 5518 | hash ^= char_bytes[c]; |
| | 5519 | hash *= 0x01000193U; |
| | 5520 | } |
| | 5521 | return hash; |
| | 5522 | } |
| | 5523 | |
| | 5524 | static uint32_t hash_combine_bigint(uint32_t hash, const BigInt *value) { |
| | 5525 | return hash_combine(hash, bigint_ptr(value), value->digit_count); |
| 5512 | } | 5526 | } |
| 5513 | | 5527 | |
| 5514 | static uint32_t hash_size(size_t x) { | 5528 | static uint32_t hash_combine_buf(uint32_t hash, const Buf *buf) { |
| 5515 | return (uint32_t)(x % UINT32_MAX); | 5529 | return hash_combine(hash, buf_ptr(buf), buf_len(buf)); |
| 5516 | } | 5530 | } |
| 5517 | | 5531 | |
| 5518 | uint32_t fn_table_entry_hash(ZigFn* value) { | 5532 | uint32_t fn_table_entry_hash(ZigFn* value) { |
| 5519 | return ptr_hash(value); | 5533 | return hash_combine(HASH_INIT, &value); |
| 5520 | } | 5534 | } |
| 5521 | | 5535 | |
| 5522 | bool fn_table_entry_eql(ZigFn *a, ZigFn *b) { | 5536 | bool fn_table_entry_eql(ZigFn *a, ZigFn *b) { |
| 5523 | return ptr_eq(a, b); | 5537 | return a == b; |
| 5524 | } | 5538 | } |
| 5525 | | 5539 | |
| 5526 | uint32_t fn_type_id_hash(FnTypeId *id) { | 5540 | uint32_t fn_type_id_hash(FnTypeId *id) { |
| 5527 | uint32_t result = 0; | 5541 | uint32_t hash = HASH_INIT; |
| 5528 | result += ((uint32_t)(id->cc)) * (uint32_t)3349388391; | 5542 | hash = hash_combine(hash, &id->cc); |
| 5529 | result += id->is_var_args ? (uint32_t)1931444534 : 0; | 5543 | hash = hash_combine(hash, &id->is_var_args); |
| 5530 | result += hash_ptr(id->return_type); | 5544 | hash = hash_combine(hash, &id->return_type); |
| 5531 | result += id->alignment * 0xd3b3f3e2; | 5545 | hash = hash_combine(hash, &id->alignment); |
| 5532 | for (size_t i = 0; i < id->param_count; i += 1) { | 5546 | for (size_t i = 0; i < id->param_count; i += 1) { |
| 5533 | FnTypeParamInfo *info = &id->param_info[i]; | 5547 | FnTypeParamInfo *info = &id->param_info[i]; |
| 5534 | result += info->is_noalias ? (uint32_t)892356923 : 0; | 5548 | hash = hash_combine(hash, &info->is_noalias); |
| 5535 | result += hash_ptr(info->type); | 5549 | hash = hash_combine(hash, &info->type); |
| 5536 | } | 5550 | } |
| 5537 | return result; | 5551 | return hash; |
| 5538 | } | 5552 | } |
| 5539 | | 5553 | |
| 5540 | bool fn_type_id_eql(FnTypeId *a, FnTypeId *b) { | 5554 | bool fn_type_id_eql(FnTypeId *a, FnTypeId *b) { |
| ... | @@ -5559,194 +5573,200 @@ bool fn_type_id_eql(FnTypeId *a, FnTypeId *b) { | ... | @@ -5559,194 +5573,200 @@ bool fn_type_id_eql(FnTypeId *a, FnTypeId *b) { |
| 5559 | return true; | 5573 | return true; |
| 5560 | } | 5574 | } |
| 5561 | | 5575 | |
| 5562 | static uint32_t hash_const_val_error_set(ZigValue *const_val) { | 5576 | static uint32_t hash_combine_const_val_error_set(uint32_t hash_val, ZigValue *const_val) { |
| 5563 | assert(const_val->data.x_err_set != nullptr); | 5577 | assert(const_val->data.x_err_set != nullptr); |
| 5564 | return const_val->data.x_err_set->value ^ 2630160122; | 5578 | return hash_combine(hash_val, &const_val->data.x_err_set->value); |
| 5565 | } | 5579 | } |
| 5566 | | 5580 | |
| 5567 | static uint32_t hash_const_val_ptr(ZigValue *const_val) { | 5581 | static uint32_t hash_combine_const_val_ptr(uint32_t hash_val, ZigValue *const_val) { |
| 5568 | uint32_t hash_val = 0; | 5582 | hash_val = hash_combine(hash_val, &const_val->data.x_ptr.special); |
| 5569 | switch (const_val->data.x_ptr.mut) { | | |
| 5570 | case ConstPtrMutRuntimeVar: | | |
| 5571 | hash_val += (uint32_t)3500721036; | | |
| 5572 | break; | | |
| 5573 | case ConstPtrMutComptimeConst: | | |
| 5574 | hash_val += (uint32_t)4214318515; | | |
| 5575 | break; | | |
| 5576 | case ConstPtrMutInfer: | | |
| 5577 | case ConstPtrMutComptimeVar: | | |
| 5578 | hash_val += (uint32_t)1103195694; | | |
| 5579 | break; | | |
| 5580 | } | | |
| 5581 | switch (const_val->data.x_ptr.special) { | 5583 | switch (const_val->data.x_ptr.special) { |
| 5582 | case ConstPtrSpecialInvalid: | 5584 | case ConstPtrSpecialInvalid: |
| 5583 | zig_unreachable(); | 5585 | zig_unreachable(); |
| 5584 | case ConstPtrSpecialRef: | 5586 | case ConstPtrSpecialRef: |
| 5585 | hash_val += (uint32_t)2478261866; | 5587 | hash_val = hash_combine(hash_val, &const_val->data.x_ptr.data.ref.pointee); |
| 5586 | hash_val += hash_ptr(const_val->data.x_ptr.data.ref.pointee); | | |
| 5587 | return hash_val; | 5588 | return hash_val; |
| 5588 | case ConstPtrSpecialBaseArray: | 5589 | case ConstPtrSpecialBaseArray: |
| 5589 | hash_val += (uint32_t)1764906839; | 5590 | hash_val = hash_combine(hash_val, &const_val->data.x_ptr.data.base_array.array_val); |
| 5590 | hash_val += hash_ptr(const_val->data.x_ptr.data.base_array.array_val); | 5591 | hash_val = hash_combine(hash_val, &const_val->data.x_ptr.data.base_array.elem_index); |
| 5591 | hash_val += hash_size(const_val->data.x_ptr.data.base_array.elem_index); | | |
| 5592 | return hash_val; | 5592 | return hash_val; |
| 5593 | case ConstPtrSpecialSubArray: | 5593 | case ConstPtrSpecialSubArray: |
| 5594 | hash_val += (uint32_t)2643358777; | 5594 | hash_val = hash_combine(hash_val, &const_val->data.x_ptr.data.base_array.array_val); |
| 5595 | hash_val += hash_ptr(const_val->data.x_ptr.data.base_array.array_val); | 5595 | hash_val = hash_combine(hash_val, &const_val->data.x_ptr.data.base_array.elem_index); |
| 5596 | hash_val += hash_size(const_val->data.x_ptr.data.base_array.elem_index); | | |
| 5597 | return hash_val; | 5596 | return hash_val; |
| 5598 | case ConstPtrSpecialBaseStruct: | 5597 | case ConstPtrSpecialBaseStruct: |
| 5599 | hash_val += (uint32_t)3518317043; | 5598 | hash_val = hash_combine(hash_val, &const_val->data.x_ptr.data.base_struct.struct_val); |
| 5600 | hash_val += hash_ptr(const_val->data.x_ptr.data.base_struct.struct_val); | 5599 | hash_val = hash_combine(hash_val, &const_val->data.x_ptr.data.base_struct.field_index); |
| 5601 | hash_val += hash_size(const_val->data.x_ptr.data.base_struct.field_index); | | |
| 5602 | return hash_val; | 5600 | return hash_val; |
| 5603 | case ConstPtrSpecialBaseErrorUnionCode: | 5601 | case ConstPtrSpecialBaseErrorUnionCode: |
| 5604 | hash_val += (uint32_t)2994743799; | 5602 | hash_val = hash_combine(hash_val, &const_val->data.x_ptr.data.base_err_union_code.err_union_val); |
| 5605 | hash_val += hash_ptr(const_val->data.x_ptr.data.base_err_union_code.err_union_val); | | |
| 5606 | return hash_val; | 5603 | return hash_val; |
| 5607 | case ConstPtrSpecialBaseErrorUnionPayload: | 5604 | case ConstPtrSpecialBaseErrorUnionPayload: |
| 5608 | hash_val += (uint32_t)3456080131; | 5605 | hash_val = hash_combine(hash_val, &const_val->data.x_ptr.data.base_err_union_payload.err_union_val); |
| 5609 | hash_val += hash_ptr(const_val->data.x_ptr.data.base_err_union_payload.err_union_val); | | |
| 5610 | return hash_val; | 5606 | return hash_val; |
| 5611 | case ConstPtrSpecialBaseOptionalPayload: | 5607 | case ConstPtrSpecialBaseOptionalPayload: |
| 5612 | hash_val += (uint32_t)3163140517; | 5608 | hash_val = hash_combine(hash_val, &const_val->data.x_ptr.data.base_optional_payload.optional_val); |
| 5613 | hash_val += hash_ptr(const_val->data.x_ptr.data.base_optional_payload.optional_val); | | |
| 5614 | return hash_val; | 5609 | return hash_val; |
| 5615 | case ConstPtrSpecialHardCodedAddr: | 5610 | case ConstPtrSpecialHardCodedAddr: |
| 5616 | hash_val += (uint32_t)4048518294; | 5611 | hash_val = hash_combine(hash_val, &const_val->data.x_ptr.data.hard_coded_addr.addr); |
| 5617 | hash_val += hash_size(const_val->data.x_ptr.data.hard_coded_addr.addr); | | |
| 5618 | return hash_val; | | |
| 5619 | case ConstPtrSpecialDiscard: | | |
| 5620 | hash_val += 2010123162; | | |
| 5621 | return hash_val; | 5612 | return hash_val; |
| 5622 | case ConstPtrSpecialFunction: | 5613 | case ConstPtrSpecialFunction: |
| 5623 | hash_val += (uint32_t)2590901619; | 5614 | hash_val = hash_combine(hash_val, &const_val->data.x_ptr.data.fn.fn_entry); |
| 5624 | hash_val += hash_ptr(const_val->data.x_ptr.data.fn.fn_entry); | | |
| 5625 | return hash_val; | 5615 | return hash_val; |
| | 5616 | case ConstPtrSpecialDiscard: |
| 5626 | case ConstPtrSpecialNull: | 5617 | case ConstPtrSpecialNull: |
| 5627 | hash_val += (uint32_t)1486246455; | 5618 | // No fields to hash |
| 5628 | return hash_val; | 5619 | return hash_val; |
| 5629 | } | 5620 | } |
| 5630 | zig_unreachable(); | 5621 | zig_unreachable(); |
| 5631 | } | 5622 | } |
| 5632 | | 5623 | |
| 5633 | static uint32_t hash_const_val(ZigValue *const_val) { | 5624 | static uint32_t hash_combine_const_val(uint32_t hash_val, ZigValue *const_val); |
| | 5625 | static uint32_t hash_combine_const_val_array(uint32_t hash_val, ZigValue *array, size_t len) { |
| | 5626 | if (array->data.x_array.special == ConstArraySpecialUndef) { |
| | 5627 | char undef_tag = 56; |
| | 5628 | return hash_combine(hash_val, &undef_tag); |
| | 5629 | } else if (array->data.x_array.special == ConstArraySpecialBuf) { |
| | 5630 | // Hash in a way that is compatible with standard byte arrays |
| | 5631 | // If any of these asserts fails, the if after this needs to be modified |
| | 5632 | // to handle the new type in SpecialBuf. |
| | 5633 | assert(array->type->data.array.child_type->id == ZigTypeIdInt); |
| | 5634 | assert(array->type->data.array.child_type->data.integral.bit_count == 8); |
| | 5635 | assert(array->type->data.array.child_type->data.integral.is_signed == false); |
| | 5636 | const char *buf_pos = buf_ptr(array->data.x_array.data.s_buf); |
| | 5637 | const char *buf_end = buf_pos + buf_len(array->data.x_array.data.s_buf); |
| | 5638 | while (buf_pos < buf_end) { |
| | 5639 | hash_val = hash_combine(hash_val, buf_pos); |
| | 5640 | buf_pos++; |
| | 5641 | } |
| | 5642 | return hash_val; |
| | 5643 | } else if (array->type->data.array.child_type->id == ZigTypeIdInt && |
| | 5644 | array->type->data.array.child_type->data.integral.bit_count == 8 && |
| | 5645 | array->type->data.array.child_type->data.integral.is_signed == false) { |
| | 5646 | // If the type is u8, we hash it as if it's a ConstArraySpecialBuf, |
| | 5647 | // to maintain compatibility. |
| | 5648 | ZigValue *elems = array->data.x_array.data.s_none.elements; |
| | 5649 | for (size_t i = 0; i < len; i += 1) { |
| | 5650 | ZigValue *value = &elems[i]; |
| | 5651 | assert(value->type == array->type->data.array.child_type); |
| | 5652 | // N.B. Using char here instead of uint8_t to match the const char* |
| | 5653 | // returned by buf_ptr. |
| | 5654 | const char byte_value = (char) bigint_as_u8(&value->data.x_bigint); |
| | 5655 | hash_val = hash_combine(hash_val, &byte_value); |
| | 5656 | } |
| | 5657 | return hash_val; |
| | 5658 | } else { |
| | 5659 | ZigValue *elems = array->data.x_array.data.s_none.elements; |
| | 5660 | for (size_t i = 0; i < len; i += 1) { |
| | 5661 | hash_val = hash_combine_const_val(hash_val, &elems[i]); |
| | 5662 | } |
| | 5663 | return hash_val; |
| | 5664 | } |
| | 5665 | } |
| | 5666 | static uint32_t hash_combine_const_val(uint32_t hash_val, ZigValue *const_val) { |
| | 5667 | hash_val = hash_combine(hash_val, &const_val->special); |
| | 5668 | if (const_val->special == ConstValSpecialUndef) { |
| | 5669 | return hash_val; |
| | 5670 | } |
| 5634 | assert(const_val->special == ConstValSpecialStatic); | 5671 | assert(const_val->special == ConstValSpecialStatic); |
| | 5672 | hash_val = hash_combine(hash_val, &const_val->type->id); |
| 5635 | switch (const_val->type->id) { | 5673 | switch (const_val->type->id) { |
| 5636 | case ZigTypeIdOpaque: | 5674 | case ZigTypeIdOpaque: |
| 5637 | zig_unreachable(); | 5675 | zig_unreachable(); |
| 5638 | case ZigTypeIdBool: | 5676 | case ZigTypeIdBool: |
| 5639 | return const_val->data.x_bool ? (uint32_t)127863866 : (uint32_t)215080464; | 5677 | return hash_combine(hash_val, &const_val->data.x_bool); |
| 5640 | case ZigTypeIdMetaType: | 5678 | case ZigTypeIdMetaType: |
| 5641 | return hash_ptr(const_val->data.x_type); | 5679 | return hash_combine(hash_val, &const_val->data.x_type); |
| 5642 | case ZigTypeIdVoid: | | |
| 5643 | return (uint32_t)4149439618; | | |
| 5644 | case ZigTypeIdInt: | 5680 | case ZigTypeIdInt: |
| 5645 | case ZigTypeIdComptimeInt: | 5681 | case ZigTypeIdComptimeInt: |
| 5646 | { | 5682 | return hash_combine_bigint(hash_val, &const_val->data.x_bigint); |
| 5647 | uint32_t result = 1331471175; | | |
| 5648 | for (size_t i = 0; i < const_val->data.x_bigint.digit_count; i += 1) { | | |
| 5649 | uint64_t digit = bigint_ptr(&const_val->data.x_bigint)[i]; | | |
| 5650 | result ^= ((uint32_t)(digit >> 32)) ^ (uint32_t)(result); | | |
| 5651 | } | | |
| 5652 | return result; | | |
| 5653 | } | | |
| 5654 | case ZigTypeIdEnumLiteral: | 5683 | case ZigTypeIdEnumLiteral: |
| 5655 | return buf_hash(const_val->data.x_enum_literal) * (uint32_t)2691276464; | 5684 | return hash_combine_buf(hash_val, const_val->data.x_enum_literal); |
| 5656 | case ZigTypeIdEnum: | 5685 | case ZigTypeIdEnum: |
| 5657 | { | 5686 | return hash_combine_bigint(hash_val, &const_val->data.x_enum_tag); |
| 5658 | uint32_t result = 31643936; | | |
| 5659 | for (size_t i = 0; i < const_val->data.x_enum_tag.digit_count; i += 1) { | | |
| 5660 | uint64_t digit = bigint_ptr(&const_val->data.x_enum_tag)[i]; | | |
| 5661 | result ^= ((uint32_t)(digit >> 32)) ^ (uint32_t)(result); | | |
| 5662 | } | | |
| 5663 | return result; | | |
| 5664 | } | | |
| 5665 | case ZigTypeIdFloat: | 5687 | case ZigTypeIdFloat: |
| | 5688 | hash_val = hash_combine(hash_val, &const_val->type->data.floating.bit_count); |
| 5666 | switch (const_val->type->data.floating.bit_count) { | 5689 | switch (const_val->type->data.floating.bit_count) { |
| 5667 | case 16: | 5690 | case 16: return hash_combine(hash_val, &const_val->data.x_f16); |
| 5668 | { | 5691 | case 32: return hash_combine(hash_val, &const_val->data.x_f32); |
| 5669 | uint16_t result; | 5692 | case 64: return hash_combine(hash_val, &const_val->data.x_f64); |
| 5670 | static_assert(sizeof(result) == sizeof(const_val->data.x_f16), ""); | 5693 | case 128: return hash_combine(hash_val, &const_val->data.x_f128); |
| 5671 | memcpy(&result, &const_val->data.x_f16, sizeof(result)); | 5694 | default: zig_unreachable(); |
| 5672 | return result * 65537u; | | |
| 5673 | } | | |
| 5674 | case 32: | | |
| 5675 | { | | |
| 5676 | uint32_t result; | | |
| 5677 | memcpy(&result, &const_val->data.x_f32, 4); | | |
| 5678 | return result ^ 4084870010; | | |
| 5679 | } | | |
| 5680 | case 64: | | |
| 5681 | { | | |
| 5682 | uint32_t ints[2]; | | |
| 5683 | memcpy(&ints[0], &const_val->data.x_f64, 8); | | |
| 5684 | return ints[0] ^ ints[1] ^ 0x22ed43c6; | | |
| 5685 | } | | |
| 5686 | case 128: | | |
| 5687 | { | | |
| 5688 | uint32_t ints[4]; | | |
| 5689 | memcpy(&ints[0], &const_val->data.x_f128, 16); | | |
| 5690 | return ints[0] ^ ints[1] ^ ints[2] ^ ints[3] ^ 0xb5ffef27; | | |
| 5691 | } | | |
| 5692 | default: | | |
| 5693 | zig_unreachable(); | | |
| 5694 | } | 5695 | } |
| 5695 | case ZigTypeIdComptimeFloat: | 5696 | case ZigTypeIdComptimeFloat: |
| 5696 | { | 5697 | return hash_combine(hash_val, &const_val->data.x_bigfloat.value); |
| 5697 | float128_t f128 = bigfloat_to_f128(&const_val->data.x_bigfloat); | | |
| 5698 | uint32_t ints[4]; | | |
| 5699 | memcpy(&ints[0], &f128, 16); | | |
| 5700 | return ints[0] ^ ints[1] ^ ints[2] ^ ints[3] ^ 0xed8b3dfb; | | |
| 5701 | } | | |
| 5702 | case ZigTypeIdFn: | 5698 | case ZigTypeIdFn: |
| 5703 | assert(const_val->data.x_ptr.mut == ConstPtrMutComptimeConst); | 5699 | assert(const_val->data.x_ptr.mut == ConstPtrMutComptimeConst); |
| 5704 | assert(const_val->data.x_ptr.special == ConstPtrSpecialFunction); | 5700 | assert(const_val->data.x_ptr.special == ConstPtrSpecialFunction); |
| 5705 | return 3677364617 ^ hash_ptr(const_val->data.x_ptr.data.fn.fn_entry); | 5701 | return hash_combine(hash_val, &const_val->data.x_ptr.data.fn.fn_entry); |
| 5706 | case ZigTypeIdPointer: | 5702 | case ZigTypeIdPointer: |
| 5707 | return hash_const_val_ptr(const_val); | 5703 | return hash_combine_const_val_ptr(hash_val, const_val); |
| | 5704 | case ZigTypeIdVoid: |
| 5708 | case ZigTypeIdUndefined: | 5705 | case ZigTypeIdUndefined: |
| 5709 | return 162837799; | | |
| 5710 | case ZigTypeIdNull: | 5706 | case ZigTypeIdNull: |
| 5711 | return 844854567; | 5707 | return hash_val; |
| 5712 | case ZigTypeIdArray: | 5708 | case ZigTypeIdArray: |
| 5713 | // TODO better hashing algorithm | 5709 | return hash_combine_const_val_array(hash_val, const_val, const_val->type->data.array.len); |
| 5714 | return 1166190605; | 5710 | case ZigTypeIdStruct: { |
| 5715 | case ZigTypeIdStruct: | 5711 | size_t field_count = const_val->type->data.structure.src_field_count; |
| 5716 | // TODO better hashing algorithm | 5712 | for (size_t i = 0; i < field_count; i += 1) { |
| 5717 | return 1532530855; | 5713 | if (const_val->type->data.structure.fields[i]->is_comptime) { |
| 5718 | case ZigTypeIdUnion: | 5714 | // The values of comptime struct fields are part of the |
| 5719 | // TODO better hashing algorithm | 5715 | // type, not the value, so they do not participate in equality |
| 5720 | return 2709806591; | 5716 | // or hash of comptime values. |
| | 5717 | continue; |
| | 5718 | } |
| | 5719 | ZigValue *field = const_val->data.x_struct.fields[i]; |
| | 5720 | hash_val = hash_combine_const_val(hash_val, field); |
| | 5721 | } |
| | 5722 | return hash_val; |
| | 5723 | } |
| | 5724 | case ZigTypeIdUnion: { |
| | 5725 | ConstUnionValue *union_value = &const_val->data.x_union; |
| | 5726 | hash_val = hash_combine_bigint(hash_val, &union_value->tag); |
| | 5727 | return hash_combine_const_val(hash_val, union_value->payload); |
| | 5728 | } |
| 5721 | case ZigTypeIdOptional: | 5729 | case ZigTypeIdOptional: |
| 5722 | if (get_src_ptr_type(const_val->type) != nullptr) { | 5730 | if (get_src_ptr_type(const_val->type) != nullptr) { |
| 5723 | return hash_const_val_ptr(const_val) * (uint32_t)1992916303; | 5731 | char tag = 1; |
| | 5732 | hash_val = hash_combine(hash_val, &tag); |
| | 5733 | return hash_combine_const_val_ptr(hash_val, const_val); |
| 5724 | } else if (const_val->type->data.maybe.child_type->id == ZigTypeIdErrorSet) { | 5734 | } else if (const_val->type->data.maybe.child_type->id == ZigTypeIdErrorSet) { |
| 5725 | return hash_const_val_error_set(const_val) * (uint32_t)3147031929; | 5735 | char tag = 2; |
| | 5736 | hash_val = hash_combine(hash_val, &tag); |
| | 5737 | return hash_combine_const_val_error_set(hash_val, const_val); |
| | 5738 | } else if (const_val->data.x_optional) { |
| | 5739 | char tag = 3; |
| | 5740 | hash_val = hash_combine(hash_val, &tag); |
| | 5741 | return hash_combine_const_val(hash_val, const_val->data.x_optional); |
| 5726 | } else { | 5742 | } else { |
| 5727 | if (const_val->data.x_optional) { | 5743 | char tag = 4; |
| 5728 | return hash_const_val(const_val->data.x_optional) * (uint32_t)1992916303; | 5744 | hash_val = hash_combine(hash_val, &tag); |
| 5729 | } else { | 5745 | return hash_val; |
| 5730 | return 4016830364; | | |
| 5731 | } | | |
| 5732 | } | 5746 | } |
| 5733 | case ZigTypeIdErrorUnion: | 5747 | case ZigTypeIdErrorUnion: { |
| 5734 | // TODO better hashing algorithm | 5748 | bool is_err = const_val->data.x_err_union.error_set->data.x_err_set != nullptr; |
| 5735 | return 3415065496; | 5749 | hash_val = hash_combine(hash_val, &is_err); |
| | 5750 | if (is_err) { |
| | 5751 | hash_val = hash_combine_const_val(hash_val, const_val->data.x_err_union.error_set); |
| | 5752 | } else { |
| | 5753 | hash_val = hash_combine_const_val(hash_val, const_val->data.x_err_union.payload); |
| | 5754 | } |
| | 5755 | return hash_val; |
| | 5756 | } |
| 5736 | case ZigTypeIdErrorSet: | 5757 | case ZigTypeIdErrorSet: |
| 5737 | return hash_const_val_error_set(const_val); | 5758 | return hash_combine_const_val_error_set(hash_val, const_val); |
| 5738 | case ZigTypeIdVector: | 5759 | case ZigTypeIdVector: |
| 5739 | // TODO better hashing algorithm | 5760 | return hash_combine_const_val_array(hash_val, const_val, const_val->type->data.vector.len); |
| 5740 | return 3647867726; | | |
| 5741 | case ZigTypeIdFnFrame: | 5761 | case ZigTypeIdFnFrame: |
| 5742 | // TODO better hashing algorithm | 5762 | // TODO better hashing algorithm |
| 5743 | return 675741936; | 5763 | return hash_val; |
| 5744 | case ZigTypeIdAnyFrame: | 5764 | case ZigTypeIdAnyFrame: |
| 5745 | // TODO better hashing algorithm | 5765 | // TODO better hashing algorithm |
| 5746 | return 3747294894; | 5766 | return hash_val; |
| 5747 | case ZigTypeIdBoundFn: { | 5767 | case ZigTypeIdBoundFn: { |
| 5748 | assert(const_val->data.x_bound_fn.fn != nullptr); | 5768 | assert(const_val->data.x_bound_fn.fn != nullptr); |
| 5749 | return 3677364617 ^ hash_ptr(const_val->data.x_bound_fn.fn); | 5769 | return hash_combine(hash_val, &const_val->data.x_bound_fn.fn); |
| 5750 | } | 5770 | } |
| 5751 | case ZigTypeIdInvalid: | 5771 | case ZigTypeIdInvalid: |
| 5752 | case ZigTypeIdUnreachable: | 5772 | case ZigTypeIdUnreachable: |
| ... | @@ -5756,13 +5776,13 @@ static uint32_t hash_const_val(ZigValue *const_val) { | ... | @@ -5756,13 +5776,13 @@ static uint32_t hash_const_val(ZigValue *const_val) { |
| 5756 | } | 5776 | } |
| 5757 | | 5777 | |
| 5758 | uint32_t generic_fn_type_id_hash(GenericFnTypeId *id) { | 5778 | uint32_t generic_fn_type_id_hash(GenericFnTypeId *id) { |
| 5759 | uint32_t result = 0; | 5779 | uint32_t result = HASH_INIT; |
| 5760 | result += hash_ptr(id->fn_entry); | 5780 | result = hash_combine(result, &id->fn_entry); |
| 5761 | for (size_t i = 0; i < id->param_count; i += 1) { | 5781 | for (size_t i = 0; i < id->param_count; i += 1) { |
| 5762 | ZigValue *generic_param = &id->params[i]; | 5782 | ZigValue *generic_param = &id->params[i]; |
| 5763 | if (generic_param->special != ConstValSpecialRuntime) { | 5783 | if (generic_param->special != ConstValSpecialRuntime) { |
| 5764 | result += hash_const_val(generic_param); | 5784 | result = hash_combine_const_val(result, generic_param); |
| 5765 | result += hash_ptr(generic_param->type); | 5785 | result = hash_combine(result, &generic_param->type); |
| 5766 | } | 5786 | } |
| 5767 | } | 5787 | } |
| 5768 | return result; | 5788 | return result; |
| ... | @@ -5957,15 +5977,15 @@ bool fn_eval_cacheable(Scope *scope, ZigType *return_type) { | ... | @@ -5957,15 +5977,15 @@ bool fn_eval_cacheable(Scope *scope, ZigType *return_type) { |
| 5957 | } | 5977 | } |
| 5958 | | 5978 | |
| 5959 | uint32_t fn_eval_hash(Scope* scope) { | 5979 | uint32_t fn_eval_hash(Scope* scope) { |
| 5960 | uint32_t result = 0; | 5980 | uint32_t hash = HASH_INIT; |
| 5961 | while (scope) { | 5981 | while (scope) { |
| 5962 | if (scope->id == ScopeIdVarDecl) { | 5982 | if (scope->id == ScopeIdVarDecl) { |
| 5963 | ScopeVarDecl *var_scope = (ScopeVarDecl *)scope; | 5983 | ScopeVarDecl *var_scope = (ScopeVarDecl *)scope; |
| 5964 | result += hash_const_val(var_scope->var->const_value); | 5984 | hash = hash_combine_const_val(hash, var_scope->var->const_value); |
| 5965 | } else if (scope->id == ScopeIdFnDef) { | 5985 | } else if (scope->id == ScopeIdFnDef) { |
| 5966 | ScopeFnDef *fn_scope = (ScopeFnDef *)scope; | 5986 | ScopeFnDef *fn_scope = (ScopeFnDef *)scope; |
| 5967 | result += hash_ptr(fn_scope->fn_entry); | 5987 | hash = hash_combine(hash, &fn_scope->fn_entry); |
| 5968 | return result; | 5988 | return hash; |
| 5969 | } else { | 5989 | } else { |
| 5970 | zig_unreachable(); | 5990 | zig_unreachable(); |
| 5971 | } | 5991 | } |
| ... | @@ -6151,7 +6171,8 @@ ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry) { | ... | @@ -6151,7 +6171,8 @@ ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry) { |
| 6151 | for (size_t i = 0; i < field_count; i += 1) { | 6171 | for (size_t i = 0; i < field_count; i += 1) { |
| 6152 | TypeStructField *field = struct_type->data.structure.fields[i]; | 6172 | TypeStructField *field = struct_type->data.structure.fields[i]; |
| 6153 | if (field->is_comptime) { | 6173 | if (field->is_comptime) { |
| 6154 | copy_const_val(g, result->data.x_struct.fields[i], field->init_val); | 6174 | // Comptime fields are part of the type, and do not need to |
| | 6175 | // be initialized. |
| 6155 | continue; | 6176 | continue; |
| 6156 | } | 6177 | } |
| 6157 | ZigType *field_type = resolve_struct_field_type(g, field); | 6178 | ZigType *field_type = resolve_struct_field_type(g, field); |
| ... | @@ -7260,6 +7281,8 @@ bool const_values_equal(CodeGen *g, ZigValue *a, ZigValue *b) { | ... | @@ -7260,6 +7281,8 @@ bool const_values_equal(CodeGen *g, ZigValue *a, ZigValue *b) { |
| 7260 | case ZigTypeIdMetaType: | 7281 | case ZigTypeIdMetaType: |
| 7261 | return a->data.x_type == b->data.x_type; | 7282 | return a->data.x_type == b->data.x_type; |
| 7262 | case ZigTypeIdVoid: | 7283 | case ZigTypeIdVoid: |
| | 7284 | case ZigTypeIdUndefined: |
| | 7285 | case ZigTypeIdNull: |
| 7263 | return true; | 7286 | return true; |
| 7264 | case ZigTypeIdErrorSet: | 7287 | case ZigTypeIdErrorSet: |
| 7265 | return a->data.x_err_set->value == b->data.x_err_set->value; | 7288 | return a->data.x_err_set->value == b->data.x_err_set->value; |
| ... | @@ -7292,12 +7315,17 @@ bool const_values_equal(CodeGen *g, ZigValue *a, ZigValue *b) { | ... | @@ -7292,12 +7315,17 @@ bool const_values_equal(CodeGen *g, ZigValue *a, ZigValue *b) { |
| 7292 | case ZigTypeIdVector: | 7315 | case ZigTypeIdVector: |
| 7293 | assert(a->type->data.vector.len == b->type->data.vector.len); | 7316 | assert(a->type->data.vector.len == b->type->data.vector.len); |
| 7294 | return const_values_equal_array(g, a, b, a->type->data.vector.len); | 7317 | return const_values_equal_array(g, a, b, a->type->data.vector.len); |
| 7295 | case ZigTypeIdArray: { | 7318 | case ZigTypeIdArray: |
| 7296 | assert(a->type->data.array.len == b->type->data.array.len); | 7319 | assert(a->type->data.array.len == b->type->data.array.len); |
| 7297 | return const_values_equal_array(g, a, b, a->type->data.array.len); | 7320 | return const_values_equal_array(g, a, b, a->type->data.array.len); |
| 7298 | } | | |
| 7299 | case ZigTypeIdStruct: | 7321 | case ZigTypeIdStruct: |
| 7300 | for (size_t i = 0; i < a->type->data.structure.src_field_count; i += 1) { | 7322 | for (size_t i = 0; i < a->type->data.structure.src_field_count; i += 1) { |
| | 7323 | if (a->type->data.structure.fields[i]->is_comptime) { |
| | 7324 | // The values of comptime struct fields are part of the |
| | 7325 | // type, not the value, so they do not participate in equality |
| | 7326 | // or hash of comptime values. |
| | 7327 | continue; |
| | 7328 | } |
| 7301 | ZigValue *field_a = a->data.x_struct.fields[i]; | 7329 | ZigValue *field_a = a->data.x_struct.fields[i]; |
| 7302 | ZigValue *field_b = b->data.x_struct.fields[i]; | 7330 | ZigValue *field_b = b->data.x_struct.fields[i]; |
| 7303 | if (!const_values_equal(g, field_a, field_b)) | 7331 | if (!const_values_equal(g, field_a, field_b)) |
| ... | @@ -7308,10 +7336,6 @@ bool const_values_equal(CodeGen *g, ZigValue *a, ZigValue *b) { | ... | @@ -7308,10 +7336,6 @@ bool const_values_equal(CodeGen *g, ZigValue *a, ZigValue *b) { |
| 7308 | zig_panic("TODO: const_values_equal ZigTypeIdFnFrame"); | 7336 | zig_panic("TODO: const_values_equal ZigTypeIdFnFrame"); |
| 7309 | case ZigTypeIdAnyFrame: | 7337 | case ZigTypeIdAnyFrame: |
| 7310 | zig_panic("TODO: const_values_equal ZigTypeIdAnyFrame"); | 7338 | zig_panic("TODO: const_values_equal ZigTypeIdAnyFrame"); |
| 7311 | case ZigTypeIdUndefined: | | |
| 7312 | zig_panic("TODO: const_values_equal ZigTypeIdUndefined"); | | |
| 7313 | case ZigTypeIdNull: | | |
| 7314 | zig_panic("TODO: const_values_equal ZigTypeIdNull"); | | |
| 7315 | case ZigTypeIdOptional: | 7339 | case ZigTypeIdOptional: |
| 7316 | if (get_src_ptr_type(a->type) != nullptr) | 7340 | if (get_src_ptr_type(a->type) != nullptr) |
| 7317 | return const_values_equal_ptr(a, b); | 7341 | return const_values_equal_ptr(a, b); |
| ... | @@ -7718,8 +7742,9 @@ ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) { | ... | @@ -7718,8 +7742,9 @@ ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) { |
| 7718 | return entry; | 7742 | return entry; |
| 7719 | } | 7743 | } |
| 7720 | | 7744 | |
| 7721 | uint32_t type_id_hash(TypeId x) { | 7745 | uint32_t type_id_hash(TypeId const *x) { |
| 7722 | switch (x.id) { | 7746 | uint32_t hash = hash_combine(HASH_INIT, &x->id); |
| | 7747 | switch (x->id) { |
| 7723 | case ZigTypeIdInvalid: | 7748 | case ZigTypeIdInvalid: |
| 7724 | case ZigTypeIdOpaque: | 7749 | case ZigTypeIdOpaque: |
| 7725 | case ZigTypeIdMetaType: | 7750 | case ZigTypeIdMetaType: |
| ... | @@ -7743,35 +7768,50 @@ uint32_t type_id_hash(TypeId x) { | ... | @@ -7743,35 +7768,50 @@ uint32_t type_id_hash(TypeId x) { |
| 7743 | case ZigTypeIdAnyFrame: | 7768 | case ZigTypeIdAnyFrame: |
| 7744 | zig_unreachable(); | 7769 | zig_unreachable(); |
| 7745 | case ZigTypeIdErrorUnion: | 7770 | case ZigTypeIdErrorUnion: |
| 7746 | return hash_ptr(x.data.error_union.err_set_type) ^ hash_ptr(x.data.error_union.payload_type); | 7771 | hash = hash_combine(hash, &x->data.error_union.err_set_type); |
| | 7772 | hash = hash_combine(hash, &x->data.error_union.payload_type); |
| | 7773 | return hash; |
| 7747 | case ZigTypeIdPointer: | 7774 | case ZigTypeIdPointer: |
| 7748 | return hash_ptr(x.data.pointer.child_type) + | 7775 | hash = hash_combine(hash, &x->data.pointer.child_type); |
| 7749 | (uint32_t)x.data.pointer.ptr_len * 1120226602u + | 7776 | hash = hash_combine(hash, &x->data.pointer.ptr_len); |
| 7750 | (x.data.pointer.is_const ? (uint32_t)2749109194 : (uint32_t)4047371087) + | 7777 | hash = hash_combine(hash, &x->data.pointer.is_const); |
| 7751 | (x.data.pointer.is_volatile ? (uint32_t)536730450 : (uint32_t)1685612214) + | 7778 | hash = hash_combine(hash, &x->data.pointer.is_volatile); |
| 7752 | (x.data.pointer.allow_zero ? (uint32_t)3324284834 : (uint32_t)3584904923) + | 7779 | hash = hash_combine(hash, &x->data.pointer.allow_zero); |
| 7753 | (((uint32_t)x.data.pointer.alignment) ^ (uint32_t)0x777fbe0e) + | 7780 | hash = hash_combine(hash, &x->data.pointer.alignment); |
| 7754 | (((uint32_t)x.data.pointer.bit_offset_in_host) ^ (uint32_t)2639019452) + | 7781 | hash = hash_combine(hash, &x->data.pointer.bit_offset_in_host); |
| 7755 | (((uint32_t)x.data.pointer.vector_index) ^ (uint32_t)0x19199716) + | 7782 | hash = hash_combine(hash, &x->data.pointer.vector_index); |
| 7756 | (((uint32_t)x.data.pointer.host_int_bytes) ^ (uint32_t)529908881) * | 7783 | hash = hash_combine(hash, &x->data.pointer.host_int_bytes); |
| 7757 | (x.data.pointer.sentinel ? hash_const_val(x.data.pointer.sentinel) : (uint32_t)2955491856); | 7784 | if (x->data.pointer.sentinel != nullptr) { |
| | 7785 | hash = hash_combine_const_val(hash, x->data.pointer.sentinel); |
| | 7786 | } |
| | 7787 | if (x->data.pointer.inferred_struct_field) { |
| | 7788 | hash = hash_combine(hash, &x->data.pointer.inferred_struct_field->inferred_struct_type); |
| | 7789 | hash = hash_combine_buf(hash, x->data.pointer.inferred_struct_field->field_name); |
| | 7790 | } |
| | 7791 | return hash; |
| 7758 | case ZigTypeIdArray: | 7792 | case ZigTypeIdArray: |
| 7759 | return hash_ptr(x.data.array.child_type) * | 7793 | hash = hash_combine(hash, &x->data.array.child_type); |
| 7760 | ((uint32_t)x.data.array.size ^ (uint32_t)2122979968) * | 7794 | hash = hash_combine(hash, &x->data.array.size); |
| 7761 | (x.data.array.sentinel ? hash_const_val(x.data.array.sentinel) : (uint32_t)1927201585); | 7795 | if (x->data.array.sentinel != nullptr) { |
| | 7796 | hash = hash_combine_const_val(hash, x->data.array.sentinel); |
| | 7797 | } |
| | 7798 | return hash; |
| 7762 | case ZigTypeIdInt: | 7799 | case ZigTypeIdInt: |
| 7763 | return (x.data.integer.is_signed ? (uint32_t)2652528194 : (uint32_t)163929201) + | 7800 | hash = hash_combine(hash, &x->data.integer.is_signed); |
| 7764 | (((uint32_t)x.data.integer.bit_count) ^ (uint32_t)2998081557); | 7801 | hash = hash_combine(hash, &x->data.integer.bit_count); |
| | 7802 | return hash; |
| 7765 | case ZigTypeIdVector: | 7803 | case ZigTypeIdVector: |
| 7766 | return hash_ptr(x.data.vector.elem_type) * (x.data.vector.len * 526582681); | 7804 | hash = hash_combine(hash, &x->data.vector.elem_type); |
| | 7805 | hash = hash_combine(hash, &x->data.vector.len); |
| | 7806 | return hash; |
| 7767 | } | 7807 | } |
| 7768 | zig_unreachable(); | 7808 | zig_unreachable(); |
| 7769 | } | 7809 | } |
| 7770 | | 7810 | |
| 7771 | bool type_id_eql(TypeId a, TypeId b) { | 7811 | bool type_id_eql(TypeId const *a, TypeId const *b) { |
| 7772 | if (a.id != b.id) | 7812 | if (a->id != b->id) |
| 7773 | return false; | 7813 | return false; |
| 7774 | switch (a.id) { | 7814 | switch (a->id) { |
| 7775 | case ZigTypeIdInvalid: | 7815 | case ZigTypeIdInvalid: |
| 7776 | case ZigTypeIdMetaType: | 7816 | case ZigTypeIdMetaType: |
| 7777 | case ZigTypeIdVoid: | 7817 | case ZigTypeIdVoid: |
| ... | @@ -7795,107 +7835,107 @@ bool type_id_eql(TypeId a, TypeId b) { | ... | @@ -7795,107 +7835,107 @@ bool type_id_eql(TypeId a, TypeId b) { |
| 7795 | case ZigTypeIdAnyFrame: | 7835 | case ZigTypeIdAnyFrame: |
| 7796 | zig_unreachable(); | 7836 | zig_unreachable(); |
| 7797 | case ZigTypeIdErrorUnion: | 7837 | case ZigTypeIdErrorUnion: |
| 7798 | return a.data.error_union.err_set_type == b.data.error_union.err_set_type && | 7838 | return a->data.error_union.err_set_type == b->data.error_union.err_set_type && |
| 7799 | a.data.error_union.payload_type == b.data.error_union.payload_type; | 7839 | a->data.error_union.payload_type == b->data.error_union.payload_type; |
| 7800 | | 7840 | |
| 7801 | case ZigTypeIdPointer: | 7841 | case ZigTypeIdPointer: |
| 7802 | return a.data.pointer.child_type == b.data.pointer.child_type && | 7842 | return a->data.pointer.child_type == b->data.pointer.child_type && |
| 7803 | a.data.pointer.ptr_len == b.data.pointer.ptr_len && | 7843 | a->data.pointer.ptr_len == b->data.pointer.ptr_len && |
| 7804 | a.data.pointer.is_const == b.data.pointer.is_const && | 7844 | a->data.pointer.is_const == b->data.pointer.is_const && |
| 7805 | a.data.pointer.is_volatile == b.data.pointer.is_volatile && | 7845 | a->data.pointer.is_volatile == b->data.pointer.is_volatile && |
| 7806 | a.data.pointer.allow_zero == b.data.pointer.allow_zero && | 7846 | a->data.pointer.allow_zero == b->data.pointer.allow_zero && |
| 7807 | a.data.pointer.alignment == b.data.pointer.alignment && | 7847 | a->data.pointer.alignment == b->data.pointer.alignment && |
| 7808 | a.data.pointer.bit_offset_in_host == b.data.pointer.bit_offset_in_host && | 7848 | a->data.pointer.bit_offset_in_host == b->data.pointer.bit_offset_in_host && |
| 7809 | a.data.pointer.vector_index == b.data.pointer.vector_index && | 7849 | a->data.pointer.vector_index == b->data.pointer.vector_index && |
| 7810 | a.data.pointer.host_int_bytes == b.data.pointer.host_int_bytes && | 7850 | a->data.pointer.host_int_bytes == b->data.pointer.host_int_bytes && |
| 7811 | ( | 7851 | ( |
| 7812 | a.data.pointer.sentinel == b.data.pointer.sentinel || | 7852 | a->data.pointer.sentinel == b->data.pointer.sentinel || |
| 7813 | (a.data.pointer.sentinel != nullptr && b.data.pointer.sentinel != nullptr && | 7853 | (a->data.pointer.sentinel != nullptr && b->data.pointer.sentinel != nullptr && |
| 7814 | const_values_equal(a.data.pointer.codegen, a.data.pointer.sentinel, b.data.pointer.sentinel)) | 7854 | const_values_equal(a->data.pointer.codegen, a->data.pointer.sentinel, b->data.pointer.sentinel)) |
| 7815 | ) && | 7855 | ) && |
| 7816 | ( | 7856 | ( |
| 7817 | a.data.pointer.inferred_struct_field == b.data.pointer.inferred_struct_field || | 7857 | a->data.pointer.inferred_struct_field == b->data.pointer.inferred_struct_field || |
| 7818 | (a.data.pointer.inferred_struct_field != nullptr && | 7858 | (a->data.pointer.inferred_struct_field != nullptr && |
| 7819 | b.data.pointer.inferred_struct_field != nullptr && | 7859 | b->data.pointer.inferred_struct_field != nullptr && |
| 7820 | a.data.pointer.inferred_struct_field->inferred_struct_type == | 7860 | a->data.pointer.inferred_struct_field->inferred_struct_type == |
| 7821 | b.data.pointer.inferred_struct_field->inferred_struct_type && | 7861 | b->data.pointer.inferred_struct_field->inferred_struct_type && |
| 7822 | buf_eql_buf(a.data.pointer.inferred_struct_field->field_name, | 7862 | buf_eql_buf(a->data.pointer.inferred_struct_field->field_name, |
| 7823 | b.data.pointer.inferred_struct_field->field_name)) | 7863 | b->data.pointer.inferred_struct_field->field_name)) |
| 7824 | ); | 7864 | ); |
| 7825 | case ZigTypeIdArray: | 7865 | case ZigTypeIdArray: |
| 7826 | return a.data.array.child_type == b.data.array.child_type && | 7866 | return a->data.array.child_type == b->data.array.child_type && |
| 7827 | a.data.array.size == b.data.array.size && | 7867 | a->data.array.size == b->data.array.size && |
| 7828 | ( | 7868 | ( |
| 7829 | a.data.array.sentinel == b.data.array.sentinel || | 7869 | a->data.array.sentinel == b->data.array.sentinel || |
| 7830 | (a.data.array.sentinel != nullptr && b.data.array.sentinel != nullptr && | 7870 | (a->data.array.sentinel != nullptr && b->data.array.sentinel != nullptr && |
| 7831 | const_values_equal(a.data.array.codegen, a.data.array.sentinel, b.data.array.sentinel)) | 7871 | const_values_equal(a->data.array.codegen, a->data.array.sentinel, b->data.array.sentinel)) |
| 7832 | ); | 7872 | ); |
| 7833 | case ZigTypeIdInt: | 7873 | case ZigTypeIdInt: |
| 7834 | return a.data.integer.is_signed == b.data.integer.is_signed && | 7874 | return a->data.integer.is_signed == b->data.integer.is_signed && |
| 7835 | a.data.integer.bit_count == b.data.integer.bit_count; | 7875 | a->data.integer.bit_count == b->data.integer.bit_count; |
| 7836 | case ZigTypeIdVector: | 7876 | case ZigTypeIdVector: |
| 7837 | return a.data.vector.elem_type == b.data.vector.elem_type && | 7877 | return a->data.vector.elem_type == b->data.vector.elem_type && |
| 7838 | a.data.vector.len == b.data.vector.len; | 7878 | a->data.vector.len == b->data.vector.len; |
| 7839 | } | 7879 | } |
| 7840 | zig_unreachable(); | 7880 | zig_unreachable(); |
| 7841 | } | 7881 | } |
| 7842 | | 7882 | |
| 7843 | uint32_t zig_llvm_fn_key_hash(ZigLLVMFnKey x) { | 7883 | uint32_t zig_llvm_fn_key_hash(ZigLLVMFnKey const *x) { |
| 7844 | switch (x.id) { | 7884 | switch (x->id) { |
| 7845 | case ZigLLVMFnIdCtz: | 7885 | case ZigLLVMFnIdCtz: |
| 7846 | return (uint32_t)(x.data.ctz.bit_count) * (uint32_t)810453934; | 7886 | return (uint32_t)(x->data.ctz.bit_count) * (uint32_t)810453934; |
| 7847 | case ZigLLVMFnIdClz: | 7887 | case ZigLLVMFnIdClz: |
| 7848 | return (uint32_t)(x.data.clz.bit_count) * (uint32_t)2428952817; | 7888 | return (uint32_t)(x->data.clz.bit_count) * (uint32_t)2428952817; |
| 7849 | case ZigLLVMFnIdPopCount: | 7889 | case ZigLLVMFnIdPopCount: |
| 7850 | return (uint32_t)(x.data.clz.bit_count) * (uint32_t)101195049; | 7890 | return (uint32_t)(x->data.clz.bit_count) * (uint32_t)101195049; |
| 7851 | case ZigLLVMFnIdFloatOp: | 7891 | case ZigLLVMFnIdFloatOp: |
| 7852 | return (uint32_t)(x.data.floating.bit_count) * ((uint32_t)x.id + 1025) + | 7892 | return (uint32_t)(x->data.floating.bit_count) * ((uint32_t)x->id + 1025) + |
| 7853 | (uint32_t)(x.data.floating.vector_len) * (((uint32_t)x.id << 5) + 1025) + | 7893 | (uint32_t)(x->data.floating.vector_len) * (((uint32_t)x->id << 5) + 1025) + |
| 7854 | (uint32_t)(x.data.floating.op) * (uint32_t)43789879; | 7894 | (uint32_t)(x->data.floating.op) * (uint32_t)43789879; |
| 7855 | case ZigLLVMFnIdFMA: | 7895 | case ZigLLVMFnIdFMA: |
| 7856 | return (uint32_t)(x.data.floating.bit_count) * ((uint32_t)x.id + 1025) + | 7896 | return (uint32_t)(x->data.floating.bit_count) * ((uint32_t)x->id + 1025) + |
| 7857 | (uint32_t)(x.data.floating.vector_len) * (((uint32_t)x.id << 5) + 1025); | 7897 | (uint32_t)(x->data.floating.vector_len) * (((uint32_t)x->id << 5) + 1025); |
| 7858 | case ZigLLVMFnIdBswap: | 7898 | case ZigLLVMFnIdBswap: |
| 7859 | return (uint32_t)(x.data.bswap.bit_count) * ((uint32_t)3661994335) + | 7899 | return (uint32_t)(x->data.bswap.bit_count) * ((uint32_t)3661994335) + |
| 7860 | (uint32_t)(x.data.bswap.vector_len) * (((uint32_t)x.id << 5) + 1025); | 7900 | (uint32_t)(x->data.bswap.vector_len) * (((uint32_t)x->id << 5) + 1025); |
| 7861 | case ZigLLVMFnIdBitReverse: | 7901 | case ZigLLVMFnIdBitReverse: |
| 7862 | return (uint32_t)(x.data.bit_reverse.bit_count) * (uint32_t)2621398431; | 7902 | return (uint32_t)(x->data.bit_reverse.bit_count) * (uint32_t)2621398431; |
| 7863 | case ZigLLVMFnIdOverflowArithmetic: | 7903 | case ZigLLVMFnIdOverflowArithmetic: |
| 7864 | return ((uint32_t)(x.data.overflow_arithmetic.bit_count) * 87135777) + | 7904 | return ((uint32_t)(x->data.overflow_arithmetic.bit_count) * 87135777) + |
| 7865 | ((uint32_t)(x.data.overflow_arithmetic.add_sub_mul) * 31640542) + | 7905 | ((uint32_t)(x->data.overflow_arithmetic.add_sub_mul) * 31640542) + |
| 7866 | ((uint32_t)(x.data.overflow_arithmetic.is_signed) ? 1062315172 : 314955820) + | 7906 | ((uint32_t)(x->data.overflow_arithmetic.is_signed) ? 1062315172 : 314955820) + |
| 7867 | x.data.overflow_arithmetic.vector_len * 1435156945; | 7907 | x->data.overflow_arithmetic.vector_len * 1435156945; |
| 7868 | } | 7908 | } |
| 7869 | zig_unreachable(); | 7909 | zig_unreachable(); |
| 7870 | } | 7910 | } |
| 7871 | | 7911 | |
| 7872 | bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) { | 7912 | bool zig_llvm_fn_key_eql(ZigLLVMFnKey const *a, ZigLLVMFnKey const *b) { |
| 7873 | if (a.id != b.id) | 7913 | if (a->id != b->id) |
| 7874 | return false; | 7914 | return false; |
| 7875 | switch (a.id) { | 7915 | switch (a->id) { |
| 7876 | case ZigLLVMFnIdCtz: | 7916 | case ZigLLVMFnIdCtz: |
| 7877 | return a.data.ctz.bit_count == b.data.ctz.bit_count; | 7917 | return a->data.ctz.bit_count == b->data.ctz.bit_count; |
| 7878 | case ZigLLVMFnIdClz: | 7918 | case ZigLLVMFnIdClz: |
| 7879 | return a.data.clz.bit_count == b.data.clz.bit_count; | 7919 | return a->data.clz.bit_count == b->data.clz.bit_count; |
| 7880 | case ZigLLVMFnIdPopCount: | 7920 | case ZigLLVMFnIdPopCount: |
| 7881 | return a.data.pop_count.bit_count == b.data.pop_count.bit_count; | 7921 | return a->data.pop_count.bit_count == b->data.pop_count.bit_count; |
| 7882 | case ZigLLVMFnIdBswap: | 7922 | case ZigLLVMFnIdBswap: |
| 7883 | return a.data.bswap.bit_count == b.data.bswap.bit_count && | 7923 | return a->data.bswap.bit_count == b->data.bswap.bit_count && |
| 7884 | a.data.bswap.vector_len == b.data.bswap.vector_len; | 7924 | a->data.bswap.vector_len == b->data.bswap.vector_len; |
| 7885 | case ZigLLVMFnIdBitReverse: | 7925 | case ZigLLVMFnIdBitReverse: |
| 7886 | return a.data.bit_reverse.bit_count == b.data.bit_reverse.bit_count; | 7926 | return a->data.bit_reverse.bit_count == b->data.bit_reverse.bit_count; |
| 7887 | case ZigLLVMFnIdFloatOp: | 7927 | case ZigLLVMFnIdFloatOp: |
| 7888 | return a.data.floating.bit_count == b.data.floating.bit_count && | 7928 | return a->data.floating.bit_count == b->data.floating.bit_count && |
| 7889 | a.data.floating.vector_len == b.data.floating.vector_len && | 7929 | a->data.floating.vector_len == b->data.floating.vector_len && |
| 7890 | a.data.floating.op == b.data.floating.op; | 7930 | a->data.floating.op == b->data.floating.op; |
| 7891 | case ZigLLVMFnIdFMA: | 7931 | case ZigLLVMFnIdFMA: |
| 7892 | return a.data.floating.bit_count == b.data.floating.bit_count && | 7932 | return a->data.floating.bit_count == b->data.floating.bit_count && |
| 7893 | a.data.floating.vector_len == b.data.floating.vector_len; | 7933 | a->data.floating.vector_len == b->data.floating.vector_len; |
| 7894 | case ZigLLVMFnIdOverflowArithmetic: | 7934 | case ZigLLVMFnIdOverflowArithmetic: |
| 7895 | return (a.data.overflow_arithmetic.bit_count == b.data.overflow_arithmetic.bit_count) && | 7935 | return (a->data.overflow_arithmetic.bit_count == b->data.overflow_arithmetic.bit_count) && |
| 7896 | (a.data.overflow_arithmetic.add_sub_mul == b.data.overflow_arithmetic.add_sub_mul) && | 7936 | (a->data.overflow_arithmetic.add_sub_mul == b->data.overflow_arithmetic.add_sub_mul) && |
| 7897 | (a.data.overflow_arithmetic.is_signed == b.data.overflow_arithmetic.is_signed) && | 7937 | (a->data.overflow_arithmetic.is_signed == b->data.overflow_arithmetic.is_signed) && |
| 7898 | (a.data.overflow_arithmetic.vector_len == b.data.overflow_arithmetic.vector_len); | 7938 | (a->data.overflow_arithmetic.vector_len == b->data.overflow_arithmetic.vector_len); |
| 7899 | } | 7939 | } |
| 7900 | zig_unreachable(); | 7940 | zig_unreachable(); |
| 7901 | } | 7941 | } |
| ... | @@ -7915,6 +7955,13 @@ static void init_const_undefined(CodeGen *g, ZigValue *const_val) { | ... | @@ -7915,6 +7955,13 @@ static void init_const_undefined(CodeGen *g, ZigValue *const_val) { |
| 7915 | size_t field_count = wanted_type->data.structure.src_field_count; | 7955 | size_t field_count = wanted_type->data.structure.src_field_count; |
| 7916 | const_val->data.x_struct.fields = alloc_const_vals_ptrs(g, field_count); | 7956 | const_val->data.x_struct.fields = alloc_const_vals_ptrs(g, field_count); |
| 7917 | for (size_t i = 0; i < field_count; i += 1) { | 7957 | for (size_t i = 0; i < field_count; i += 1) { |
| | 7958 | TypeStructField *field = wanted_type->data.structure.fields[i]; |
| | 7959 | if (field->is_comptime) { |
| | 7960 | // Comptime fields are part of the type, and do not need to |
| | 7961 | // be initialized. |
| | 7962 | continue; |
| | 7963 | } |
| | 7964 | |
| 7918 | ZigValue *field_val = const_val->data.x_struct.fields[i]; | 7965 | ZigValue *field_val = const_val->data.x_struct.fields[i]; |
| 7919 | field_val->type = resolve_struct_field_type(g, wanted_type->data.structure.fields[i]); | 7966 | field_val->type = resolve_struct_field_type(g, wanted_type->data.structure.fields[i]); |
| 7920 | assert(field_val->type); | 7967 | assert(field_val->type); |
| ... | @@ -8155,7 +8202,7 @@ ZigType *get_align_amt_type(CodeGen *g) { | ... | @@ -8155,7 +8202,7 @@ ZigType *get_align_amt_type(CodeGen *g) { |
| 8155 | } | 8202 | } |
| 8156 | | 8203 | |
| 8157 | uint32_t type_ptr_hash(const ZigType *ptr) { | 8204 | uint32_t type_ptr_hash(const ZigType *ptr) { |
| 8158 | return hash_ptr((void*)ptr); | 8205 | return hash_combine(HASH_INIT, &ptr); |
| 8159 | } | 8206 | } |
| 8160 | | 8207 | |
| 8161 | bool type_ptr_eql(const ZigType *a, const ZigType *b) { | 8208 | bool type_ptr_eql(const ZigType *a, const ZigType *b) { |
| ... | @@ -8163,7 +8210,7 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b) { | ... | @@ -8163,7 +8210,7 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b) { |
| 8163 | } | 8210 | } |
| 8164 | | 8211 | |
| 8165 | uint32_t pkg_ptr_hash(const ZigPackage *ptr) { | 8212 | uint32_t pkg_ptr_hash(const ZigPackage *ptr) { |
| 8166 | return hash_ptr((void*)ptr); | 8213 | return hash_combine(HASH_INIT, &ptr); |
| 8167 | } | 8214 | } |
| 8168 | | 8215 | |
| 8169 | bool pkg_ptr_eql(const ZigPackage *a, const ZigPackage *b) { | 8216 | bool pkg_ptr_eql(const ZigPackage *a, const ZigPackage *b) { |
| ... | @@ -8171,7 +8218,7 @@ bool pkg_ptr_eql(const ZigPackage *a, const ZigPackage *b) { | ... | @@ -8171,7 +8218,7 @@ bool pkg_ptr_eql(const ZigPackage *a, const ZigPackage *b) { |
| 8171 | } | 8218 | } |
| 8172 | | 8219 | |
| 8173 | uint32_t tld_ptr_hash(const Tld *ptr) { | 8220 | uint32_t tld_ptr_hash(const Tld *ptr) { |
| 8174 | return hash_ptr((void*)ptr); | 8221 | return hash_combine(HASH_INIT, &ptr); |
| 8175 | } | 8222 | } |
| 8176 | | 8223 | |
| 8177 | bool tld_ptr_eql(const Tld *a, const Tld *b) { | 8224 | bool tld_ptr_eql(const Tld *a, const Tld *b) { |
| ... | @@ -8179,7 +8226,7 @@ bool tld_ptr_eql(const Tld *a, const Tld *b) { | ... | @@ -8179,7 +8226,7 @@ bool tld_ptr_eql(const Tld *a, const Tld *b) { |
| 8179 | } | 8226 | } |
| 8180 | | 8227 | |
| 8181 | uint32_t node_ptr_hash(const AstNode *ptr) { | 8228 | uint32_t node_ptr_hash(const AstNode *ptr) { |
| 8182 | return hash_ptr((void*)ptr); | 8229 | return hash_combine(HASH_INIT, &ptr); |
| 8183 | } | 8230 | } |
| 8184 | | 8231 | |
| 8185 | bool node_ptr_eql(const AstNode *a, const AstNode *b) { | 8232 | bool node_ptr_eql(const AstNode *a, const AstNode *b) { |
| ... | @@ -8187,7 +8234,7 @@ bool node_ptr_eql(const AstNode *a, const AstNode *b) { | ... | @@ -8187,7 +8234,7 @@ bool node_ptr_eql(const AstNode *a, const AstNode *b) { |
| 8187 | } | 8234 | } |
| 8188 | | 8235 | |
| 8189 | uint32_t fn_ptr_hash(const ZigFn *ptr) { | 8236 | uint32_t fn_ptr_hash(const ZigFn *ptr) { |
| 8190 | return hash_ptr((void*)ptr); | 8237 | return hash_combine(HASH_INIT, &ptr); |
| 8191 | } | 8238 | } |
| 8192 | | 8239 | |
| 8193 | bool fn_ptr_eql(const ZigFn *a, const ZigFn *b) { | 8240 | bool fn_ptr_eql(const ZigFn *a, const ZigFn *b) { |
| ... | @@ -8195,7 +8242,7 @@ bool fn_ptr_eql(const ZigFn *a, const ZigFn *b) { | ... | @@ -8195,7 +8242,7 @@ bool fn_ptr_eql(const ZigFn *a, const ZigFn *b) { |
| 8195 | } | 8242 | } |
| 8196 | | 8243 | |
| 8197 | uint32_t err_ptr_hash(const ErrorTableEntry *ptr) { | 8244 | uint32_t err_ptr_hash(const ErrorTableEntry *ptr) { |
| 8198 | return hash_ptr((void*)ptr); | 8245 | return hash_combine(HASH_INIT, &ptr); |
| 8199 | } | 8246 | } |
| 8200 | | 8247 | |
| 8201 | bool err_ptr_eql(const ErrorTableEntry *a, const ErrorTableEntry *b) { | 8248 | bool err_ptr_eql(const ErrorTableEntry *a, const ErrorTableEntry *b) { |
| ... | @@ -9914,10 +9961,13 @@ void copy_const_val(CodeGen *g, ZigValue *dest, ZigValue *src) { | ... | @@ -9914,10 +9961,13 @@ void copy_const_val(CodeGen *g, ZigValue *dest, ZigValue *src) { |
| 9914 | dest->data.x_struct.fields = alloc_const_vals_ptrs(g, dest->type->data.structure.src_field_count); | 9961 | dest->data.x_struct.fields = alloc_const_vals_ptrs(g, dest->type->data.structure.src_field_count); |
| 9915 | for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) { | 9962 | for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) { |
| 9916 | TypeStructField *type_struct_field = dest->type->data.structure.fields[i]; | 9963 | TypeStructField *type_struct_field = dest->type->data.structure.fields[i]; |
| 9917 | // comptime-known values are stored in the field init_val inside | 9964 | if (type_struct_field->is_comptime) { |
| 9918 | // the struct type. | 9965 | // comptime-known values are stored in the field init_val inside |
| 9919 | if (type_struct_field->is_comptime) | 9966 | // the struct type. The data stored here is not supposed to be read |
| | 9967 | // at all; the code should look at the type system and notice the field |
| | 9968 | // is comptime and look at the type to learn the value. |
| 9920 | continue; | 9969 | continue; |
| | 9970 | } |
| 9921 | copy_const_val(g, dest->data.x_struct.fields[i], src->data.x_struct.fields[i]); | 9971 | copy_const_val(g, dest->data.x_struct.fields[i], src->data.x_struct.fields[i]); |
| 9922 | dest->data.x_struct.fields[i]->parent.id = ConstParentIdStruct; | 9972 | dest->data.x_struct.fields[i]->parent.id = ConstParentIdStruct; |
| 9923 | dest->data.x_struct.fields[i]->parent.data.p_struct.struct_val = dest; | 9973 | dest->data.x_struct.fields[i]->parent.data.p_struct.struct_val = dest; |
| ... | @@ -10128,8 +10178,11 @@ static void dump_value_indent(ZigValue *val, int indent) { | ... | @@ -10128,8 +10178,11 @@ static void dump_value_indent(ZigValue *val, int indent) { |
| 10128 | for (int j = 0; j < indent; j += 1) { | 10178 | for (int j = 0; j < indent; j += 1) { |
| 10129 | fprintf(stderr, " "); | 10179 | fprintf(stderr, " "); |
| 10130 | } | 10180 | } |
| 10131 | fprintf(stderr, "%s: ", buf_ptr(val->type->data.structure.fields[i]->name)); | 10181 | TypeStructField *field = val->type->data.structure.fields[i]; |
| 10132 | if (val->data.x_struct.fields == nullptr) { | 10182 | fprintf(stderr, "%s: ", buf_ptr(field->name)); |
| | 10183 | if (field->is_comptime) { |
| | 10184 | fprintf(stderr, "<comptime field>"); |
| | 10185 | } else if (val->data.x_struct.fields == nullptr) { |
| 10133 | fprintf(stderr, "<null>\n"); | 10186 | fprintf(stderr, "<null>\n"); |
| 10134 | } else { | 10187 | } else { |
| 10135 | dump_value_indent(val->data.x_struct.fields[i], 1); | 10188 | dump_value_indent(val->data.x_struct.fields[i], 1); |