| author | |
| committer | |
| log | 78811ff4acf6d05ae9e6d9acafe58f40ae3ae952 |
| tree | c8fd7b39f969c342cf2cc4cb5001d45bfd3914a8 |
| parent | b36c07a95a6cf9b2cc120133b44cbd0673e6823a |
| parent | b5df18c8fd725a2993c520ddc8777ecad71e3d11 |
| signature | Commit is signed but in an unrecognized format. |
4 files changed, 100 insertions(+), 161 deletions(-)
src/all_types.hpp+3-7| ... | ... | @@ -313,12 +313,6 @@ struct RuntimeHintSlice { |
| 313 | 313 | uint64_t len; |
| 314 | 314 | }; |
| 315 | 315 | |
| 316 | struct ConstGlobalRefs { | |
| 317 | LLVMValueRef llvm_value; | |
| 318 | LLVMValueRef llvm_global; | |
| 319 | uint32_t align; | |
| 320 | }; | |
| 321 | ||
| 322 | 316 | enum LazyValueId { |
| 323 | 317 | LazyValueIdInvalid, |
| 324 | 318 | LazyValueIdAlignOf, |
| ... | ... | @@ -409,8 +403,10 @@ struct LazyValueErrUnionType { |
| 409 | 403 | struct ZigValue { |
| 410 | 404 | ZigType *type; |
| 411 | 405 | ConstValSpecial special; |
| 406 | uint32_t llvm_align; | |
| 412 | 407 | ConstParent parent; |
| 413 | ConstGlobalRefs *global_refs; | |
| 408 | LLVMValueRef llvm_value; | |
| 409 | LLVMValueRef llvm_global; | |
| 414 | 410 | |
| 415 | 411 | union { |
| 416 | 412 | // populated if special == ConstValSpecialStatic |
src/analyze.cpp+6-23| ... | ... | @@ -5909,12 +5909,7 @@ ZigValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_t arg_ |
| 5909 | 5909 | |
| 5910 | 5910 | |
| 5911 | 5911 | ZigValue *create_const_vals(size_t count) { |
| 5912 | ConstGlobalRefs *global_refs = allocate<ConstGlobalRefs>(count, "ConstGlobalRefs"); | |
| 5913 | ZigValue *vals = allocate<ZigValue>(count, "ZigValue"); | |
| 5914 | for (size_t i = 0; i < count; i += 1) { | |
| 5915 | vals[i].global_refs = &global_refs[i]; | |
| 5916 | } | |
| 5917 | return vals; | |
| 5912 | return allocate<ZigValue>(count, "ZigValue"); | |
| 5918 | 5913 | } |
| 5919 | 5914 | |
| 5920 | 5915 | ZigValue **alloc_const_vals_ptrs(size_t count) { |
| ... | ... | @@ -6492,20 +6487,14 @@ bool const_values_equal_ptr(ZigValue *a, ZigValue *b) { |
| 6492 | 6487 | return false; |
| 6493 | 6488 | return true; |
| 6494 | 6489 | case ConstPtrSpecialBaseArray: |
| 6495 | if (a->data.x_ptr.data.base_array.array_val != b->data.x_ptr.data.base_array.array_val && | |
| 6496 | a->data.x_ptr.data.base_array.array_val->global_refs != | |
| 6497 | b->data.x_ptr.data.base_array.array_val->global_refs) | |
| 6498 | { | |
| 6490 | if (a->data.x_ptr.data.base_array.array_val != b->data.x_ptr.data.base_array.array_val) { | |
| 6499 | 6491 | return false; |
| 6500 | 6492 | } |
| 6501 | 6493 | if (a->data.x_ptr.data.base_array.elem_index != b->data.x_ptr.data.base_array.elem_index) |
| 6502 | 6494 | return false; |
| 6503 | 6495 | return true; |
| 6504 | 6496 | case ConstPtrSpecialBaseStruct: |
| 6505 | if (a->data.x_ptr.data.base_struct.struct_val != b->data.x_ptr.data.base_struct.struct_val && | |
| 6506 | a->data.x_ptr.data.base_struct.struct_val->global_refs != | |
| 6507 | b->data.x_ptr.data.base_struct.struct_val->global_refs) | |
| 6508 | { | |
| 6497 | if (a->data.x_ptr.data.base_struct.struct_val != b->data.x_ptr.data.base_struct.struct_val) { | |
| 6509 | 6498 | return false; |
| 6510 | 6499 | } |
| 6511 | 6500 | if (a->data.x_ptr.data.base_struct.field_index != b->data.x_ptr.data.base_struct.field_index) |
| ... | ... | @@ -6513,27 +6502,21 @@ bool const_values_equal_ptr(ZigValue *a, ZigValue *b) { |
| 6513 | 6502 | return true; |
| 6514 | 6503 | case ConstPtrSpecialBaseErrorUnionCode: |
| 6515 | 6504 | if (a->data.x_ptr.data.base_err_union_code.err_union_val != |
| 6516 | b->data.x_ptr.data.base_err_union_code.err_union_val && | |
| 6517 | a->data.x_ptr.data.base_err_union_code.err_union_val->global_refs != | |
| 6518 | b->data.x_ptr.data.base_err_union_code.err_union_val->global_refs) | |
| 6505 | b->data.x_ptr.data.base_err_union_code.err_union_val) | |
| 6519 | 6506 | { |
| 6520 | 6507 | return false; |
| 6521 | 6508 | } |
| 6522 | 6509 | return true; |
| 6523 | 6510 | case ConstPtrSpecialBaseErrorUnionPayload: |
| 6524 | 6511 | if (a->data.x_ptr.data.base_err_union_payload.err_union_val != |
| 6525 | b->data.x_ptr.data.base_err_union_payload.err_union_val && | |
| 6526 | a->data.x_ptr.data.base_err_union_payload.err_union_val->global_refs != | |
| 6527 | b->data.x_ptr.data.base_err_union_payload.err_union_val->global_refs) | |
| 6512 | b->data.x_ptr.data.base_err_union_payload.err_union_val) | |
| 6528 | 6513 | { |
| 6529 | 6514 | return false; |
| 6530 | 6515 | } |
| 6531 | 6516 | return true; |
| 6532 | 6517 | case ConstPtrSpecialBaseOptionalPayload: |
| 6533 | 6518 | if (a->data.x_ptr.data.base_optional_payload.optional_val != |
| 6534 | b->data.x_ptr.data.base_optional_payload.optional_val && | |
| 6535 | a->data.x_ptr.data.base_optional_payload.optional_val->global_refs != | |
| 6536 | b->data.x_ptr.data.base_optional_payload.optional_val->global_refs) | |
| 6519 | b->data.x_ptr.data.base_optional_payload.optional_val) | |
| 6537 | 6520 | { |
| 6538 | 6521 | return false; |
| 6539 | 6522 | } |
src/codegen.cpp+42-68| ... | ... | @@ -946,7 +946,7 @@ static Buf *panic_msg_buf(PanicMsgId msg_id) { |
| 946 | 946 | |
| 947 | 947 | static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) { |
| 948 | 948 | ZigValue *val = &g->panic_msg_vals[msg_id]; |
| 949 | if (!val->global_refs->llvm_global) { | |
| 949 | if (!val->llvm_global) { | |
| 950 | 950 | |
| 951 | 951 | Buf *buf_msg = panic_msg_buf(msg_id); |
| 952 | 952 | ZigValue *array_val = create_const_str_lit(g, buf_msg)->data.x_ptr.data.ref.pointee; |
| ... | ... | @@ -955,13 +955,13 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) { |
| 955 | 955 | render_const_val(g, val, ""); |
| 956 | 956 | render_const_val_global(g, val, ""); |
| 957 | 957 | |
| 958 | assert(val->global_refs->llvm_global); | |
| 958 | assert(val->llvm_global); | |
| 959 | 959 | } |
| 960 | 960 | |
| 961 | 961 | ZigType *u8_ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false, |
| 962 | 962 | PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false); |
| 963 | 963 | ZigType *str_type = get_slice_type(g, u8_ptr_type); |
| 964 | return LLVMConstBitCast(val->global_refs->llvm_global, LLVMPointerType(get_llvm_type(g, str_type), 0)); | |
| 964 | return LLVMConstBitCast(val->llvm_global, LLVMPointerType(get_llvm_type(g, str_type), 0)); | |
| 965 | 965 | } |
| 966 | 966 | |
| 967 | 967 | static ZigType *ptr_to_stack_trace_type(CodeGen *g) { |
| ... | ... | @@ -1727,9 +1727,9 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) { |
| 1727 | 1727 | if (handle_is_ptr(instruction->value->type)) { |
| 1728 | 1728 | render_const_val_global(g, instruction->value, ""); |
| 1729 | 1729 | ZigType *ptr_type = get_pointer_to_type(g, instruction->value->type, true); |
| 1730 | instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value->global_refs->llvm_global, get_llvm_type(g, ptr_type), ""); | |
| 1730 | instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value->llvm_global, get_llvm_type(g, ptr_type), ""); | |
| 1731 | 1731 | } else { |
| 1732 | instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value->global_refs->llvm_value, | |
| 1732 | instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value->llvm_value, | |
| 1733 | 1733 | get_llvm_type(g, instruction->value->type), ""); |
| 1734 | 1734 | } |
| 1735 | 1735 | assert(instruction->llvm_value); |
| ... | ... | @@ -6374,7 +6374,7 @@ static LLVMValueRef gen_parent_ptr(CodeGen *g, ZigValue *val, ConstParent *paren |
| 6374 | 6374 | case ConstParentIdNone: |
| 6375 | 6375 | render_const_val(g, val, ""); |
| 6376 | 6376 | render_const_val_global(g, val, ""); |
| 6377 | return val->global_refs->llvm_global; | |
| 6377 | return val->llvm_global; | |
| 6378 | 6378 | case ConstParentIdStruct: |
| 6379 | 6379 | return gen_const_ptr_struct_recursive(g, parent->data.p_struct.struct_val, |
| 6380 | 6380 | parent->data.p_struct.field_index); |
| ... | ... | @@ -6392,7 +6392,7 @@ static LLVMValueRef gen_parent_ptr(CodeGen *g, ZigValue *val, ConstParent *paren |
| 6392 | 6392 | case ConstParentIdScalar: |
| 6393 | 6393 | render_const_val(g, parent->data.p_scalar.scalar_val, ""); |
| 6394 | 6394 | render_const_val_global(g, parent->data.p_scalar.scalar_val, ""); |
| 6395 | return parent->data.p_scalar.scalar_val->global_refs->llvm_global; | |
| 6395 | return parent->data.p_scalar.scalar_val->llvm_global; | |
| 6396 | 6396 | } |
| 6397 | 6397 | zig_unreachable(); |
| 6398 | 6398 | } |
| ... | ... | @@ -6623,17 +6623,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ZigValue *const_val, const cha |
| 6623 | 6623 | zig_unreachable(); |
| 6624 | 6624 | case ConstPtrSpecialRef: |
| 6625 | 6625 | { |
| 6626 | assert(const_val->global_refs != nullptr); | |
| 6627 | 6626 | ZigValue *pointee = const_val->data.x_ptr.data.ref.pointee; |
| 6628 | 6627 | render_const_val(g, pointee, ""); |
| 6629 | 6628 | render_const_val_global(g, pointee, ""); |
| 6630 | const_val->global_refs->llvm_value = LLVMConstBitCast(pointee->global_refs->llvm_global, | |
| 6629 | const_val->llvm_value = LLVMConstBitCast(pointee->llvm_global, | |
| 6631 | 6630 | get_llvm_type(g, const_val->type)); |
| 6632 | return const_val->global_refs->llvm_value; | |
| 6631 | return const_val->llvm_value; | |
| 6633 | 6632 | } |
| 6634 | 6633 | case ConstPtrSpecialBaseArray: |
| 6635 | 6634 | { |
| 6636 | assert(const_val->global_refs != nullptr); | |
| 6637 | 6635 | ZigValue *array_const_val = const_val->data.x_ptr.data.base_array.array_val; |
| 6638 | 6636 | assert(array_const_val->type->id == ZigTypeIdArray); |
| 6639 | 6637 | if (!type_has_bits(array_const_val->type)) { |
| ... | ... | @@ -6641,102 +6639,97 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ZigValue *const_val, const cha |
| 6641 | 6639 | ZigValue *pointee = array_const_val->type->data.array.sentinel; |
| 6642 | 6640 | render_const_val(g, pointee, ""); |
| 6643 | 6641 | render_const_val_global(g, pointee, ""); |
| 6644 | const_val->global_refs->llvm_value = LLVMConstBitCast(pointee->global_refs->llvm_global, | |
| 6642 | const_val->llvm_value = LLVMConstBitCast(pointee->llvm_global, | |
| 6645 | 6643 | get_llvm_type(g, const_val->type)); |
| 6646 | return const_val->global_refs->llvm_value; | |
| 6644 | return const_val->llvm_value; | |
| 6647 | 6645 | } else { |
| 6648 | 6646 | // make this a null pointer |
| 6649 | 6647 | ZigType *usize = g->builtin_types.entry_usize; |
| 6650 | const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), | |
| 6648 | const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), | |
| 6651 | 6649 | get_llvm_type(g, const_val->type)); |
| 6652 | return const_val->global_refs->llvm_value; | |
| 6650 | return const_val->llvm_value; | |
| 6653 | 6651 | } |
| 6654 | 6652 | } |
| 6655 | 6653 | size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index; |
| 6656 | 6654 | LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val, elem_index); |
| 6657 | 6655 | LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type)); |
| 6658 | const_val->global_refs->llvm_value = ptr_val; | |
| 6656 | const_val->llvm_value = ptr_val; | |
| 6659 | 6657 | return ptr_val; |
| 6660 | 6658 | } |
| 6661 | 6659 | case ConstPtrSpecialBaseStruct: |
| 6662 | 6660 | { |
| 6663 | assert(const_val->global_refs != nullptr); | |
| 6664 | 6661 | ZigValue *struct_const_val = const_val->data.x_ptr.data.base_struct.struct_val; |
| 6665 | 6662 | assert(struct_const_val->type->id == ZigTypeIdStruct); |
| 6666 | 6663 | if (!type_has_bits(struct_const_val->type)) { |
| 6667 | 6664 | // make this a null pointer |
| 6668 | 6665 | ZigType *usize = g->builtin_types.entry_usize; |
| 6669 | const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), | |
| 6666 | const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), | |
| 6670 | 6667 | get_llvm_type(g, const_val->type)); |
| 6671 | return const_val->global_refs->llvm_value; | |
| 6668 | return const_val->llvm_value; | |
| 6672 | 6669 | } |
| 6673 | 6670 | size_t src_field_index = const_val->data.x_ptr.data.base_struct.field_index; |
| 6674 | 6671 | size_t gen_field_index = struct_const_val->type->data.structure.fields[src_field_index]->gen_index; |
| 6675 | 6672 | LLVMValueRef uncasted_ptr_val = gen_const_ptr_struct_recursive(g, struct_const_val, |
| 6676 | 6673 | gen_field_index); |
| 6677 | 6674 | LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type)); |
| 6678 | const_val->global_refs->llvm_value = ptr_val; | |
| 6675 | const_val->llvm_value = ptr_val; | |
| 6679 | 6676 | return ptr_val; |
| 6680 | 6677 | } |
| 6681 | 6678 | case ConstPtrSpecialBaseErrorUnionCode: |
| 6682 | 6679 | { |
| 6683 | assert(const_val->global_refs != nullptr); | |
| 6684 | 6680 | ZigValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_code.err_union_val; |
| 6685 | 6681 | assert(err_union_const_val->type->id == ZigTypeIdErrorUnion); |
| 6686 | 6682 | if (!type_has_bits(err_union_const_val->type)) { |
| 6687 | 6683 | // make this a null pointer |
| 6688 | 6684 | ZigType *usize = g->builtin_types.entry_usize; |
| 6689 | const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), | |
| 6685 | const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), | |
| 6690 | 6686 | get_llvm_type(g, const_val->type)); |
| 6691 | return const_val->global_refs->llvm_value; | |
| 6687 | return const_val->llvm_value; | |
| 6692 | 6688 | } |
| 6693 | 6689 | LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_code_recursive(g, err_union_const_val); |
| 6694 | 6690 | LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type)); |
| 6695 | const_val->global_refs->llvm_value = ptr_val; | |
| 6691 | const_val->llvm_value = ptr_val; | |
| 6696 | 6692 | return ptr_val; |
| 6697 | 6693 | } |
| 6698 | 6694 | case ConstPtrSpecialBaseErrorUnionPayload: |
| 6699 | 6695 | { |
| 6700 | assert(const_val->global_refs != nullptr); | |
| 6701 | 6696 | ZigValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_payload.err_union_val; |
| 6702 | 6697 | assert(err_union_const_val->type->id == ZigTypeIdErrorUnion); |
| 6703 | 6698 | if (!type_has_bits(err_union_const_val->type)) { |
| 6704 | 6699 | // make this a null pointer |
| 6705 | 6700 | ZigType *usize = g->builtin_types.entry_usize; |
| 6706 | const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), | |
| 6701 | const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), | |
| 6707 | 6702 | get_llvm_type(g, const_val->type)); |
| 6708 | return const_val->global_refs->llvm_value; | |
| 6703 | return const_val->llvm_value; | |
| 6709 | 6704 | } |
| 6710 | 6705 | LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_payload_recursive(g, err_union_const_val); |
| 6711 | 6706 | LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type)); |
| 6712 | const_val->global_refs->llvm_value = ptr_val; | |
| 6707 | const_val->llvm_value = ptr_val; | |
| 6713 | 6708 | return ptr_val; |
| 6714 | 6709 | } |
| 6715 | 6710 | case ConstPtrSpecialBaseOptionalPayload: |
| 6716 | 6711 | { |
| 6717 | assert(const_val->global_refs != nullptr); | |
| 6718 | 6712 | ZigValue *optional_const_val = const_val->data.x_ptr.data.base_optional_payload.optional_val; |
| 6719 | 6713 | assert(optional_const_val->type->id == ZigTypeIdOptional); |
| 6720 | 6714 | if (!type_has_bits(optional_const_val->type)) { |
| 6721 | 6715 | // make this a null pointer |
| 6722 | 6716 | ZigType *usize = g->builtin_types.entry_usize; |
| 6723 | const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), | |
| 6717 | const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), | |
| 6724 | 6718 | get_llvm_type(g, const_val->type)); |
| 6725 | return const_val->global_refs->llvm_value; | |
| 6719 | return const_val->llvm_value; | |
| 6726 | 6720 | } |
| 6727 | 6721 | LLVMValueRef uncasted_ptr_val = gen_const_ptr_optional_payload_recursive(g, optional_const_val); |
| 6728 | 6722 | LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type)); |
| 6729 | const_val->global_refs->llvm_value = ptr_val; | |
| 6723 | const_val->llvm_value = ptr_val; | |
| 6730 | 6724 | return ptr_val; |
| 6731 | 6725 | } |
| 6732 | 6726 | case ConstPtrSpecialHardCodedAddr: |
| 6733 | 6727 | { |
| 6734 | assert(const_val->global_refs != nullptr); | |
| 6735 | 6728 | uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr; |
| 6736 | 6729 | ZigType *usize = g->builtin_types.entry_usize; |
| 6737 | const_val->global_refs->llvm_value = LLVMConstIntToPtr( | |
| 6730 | const_val->llvm_value = LLVMConstIntToPtr( | |
| 6738 | 6731 | LLVMConstInt(usize->llvm_type, addr_value, false), get_llvm_type(g, const_val->type)); |
| 6739 | return const_val->global_refs->llvm_value; | |
| 6732 | return const_val->llvm_value; | |
| 6740 | 6733 | } |
| 6741 | 6734 | case ConstPtrSpecialFunction: |
| 6742 | 6735 | return LLVMConstBitCast(fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry), |
| ... | ... | @@ -7175,34 +7168,29 @@ check: switch (const_val->special) { |
| 7175 | 7168 | } |
| 7176 | 7169 | |
| 7177 | 7170 | static void render_const_val(CodeGen *g, ZigValue *const_val, const char *name) { |
| 7178 | if (!const_val->global_refs) | |
| 7179 | const_val->global_refs = allocate<ConstGlobalRefs>(1); | |
| 7180 | if (!const_val->global_refs->llvm_value) | |
| 7181 | const_val->global_refs->llvm_value = gen_const_val(g, const_val, name); | |
| 7171 | if (!const_val->llvm_value) | |
| 7172 | const_val->llvm_value = gen_const_val(g, const_val, name); | |
| 7182 | 7173 | |
| 7183 | if (const_val->global_refs->llvm_global) | |
| 7184 | LLVMSetInitializer(const_val->global_refs->llvm_global, const_val->global_refs->llvm_value); | |
| 7174 | if (const_val->llvm_global) | |
| 7175 | LLVMSetInitializer(const_val->llvm_global, const_val->llvm_value); | |
| 7185 | 7176 | } |
| 7186 | 7177 | |
| 7187 | 7178 | static void render_const_val_global(CodeGen *g, ZigValue *const_val, const char *name) { |
| 7188 | if (!const_val->global_refs) | |
| 7189 | const_val->global_refs = allocate<ConstGlobalRefs>(1); | |
| 7190 | ||
| 7191 | if (!const_val->global_refs->llvm_global) { | |
| 7192 | LLVMTypeRef type_ref = const_val->global_refs->llvm_value ? | |
| 7193 | LLVMTypeOf(const_val->global_refs->llvm_value) : get_llvm_type(g, const_val->type); | |
| 7179 | if (!const_val->llvm_global) { | |
| 7180 | LLVMTypeRef type_ref = const_val->llvm_value ? | |
| 7181 | LLVMTypeOf(const_val->llvm_value) : get_llvm_type(g, const_val->type); | |
| 7194 | 7182 | LLVMValueRef global_value = LLVMAddGlobal(g->module, type_ref, name); |
| 7195 | 7183 | LLVMSetLinkage(global_value, LLVMInternalLinkage); |
| 7196 | 7184 | LLVMSetGlobalConstant(global_value, true); |
| 7197 | 7185 | LLVMSetUnnamedAddr(global_value, true); |
| 7198 | LLVMSetAlignment(global_value, (const_val->global_refs->align == 0) ? | |
| 7199 | get_abi_alignment(g, const_val->type) : const_val->global_refs->align); | |
| 7186 | LLVMSetAlignment(global_value, (const_val->llvm_align == 0) ? | |
| 7187 | get_abi_alignment(g, const_val->type) : const_val->llvm_align); | |
| 7200 | 7188 | |
| 7201 | const_val->global_refs->llvm_global = global_value; | |
| 7189 | const_val->llvm_global = global_value; | |
| 7202 | 7190 | } |
| 7203 | 7191 | |
| 7204 | if (const_val->global_refs->llvm_value) | |
| 7205 | LLVMSetInitializer(const_val->global_refs->llvm_global, const_val->global_refs->llvm_value); | |
| 7192 | if (const_val->llvm_value) | |
| 7193 | LLVMSetInitializer(const_val->llvm_global, const_val->llvm_value); | |
| 7206 | 7194 | } |
| 7207 | 7195 | |
| 7208 | 7196 | static void generate_error_name_table(CodeGen *g) { |
| ... | ... | @@ -7403,7 +7391,7 @@ static void do_code_gen(CodeGen *g) { |
| 7403 | 7391 | bool exported = (linkage != GlobalLinkageIdInternal); |
| 7404 | 7392 | render_const_val(g, var->const_value, symbol_name); |
| 7405 | 7393 | render_const_val_global(g, var->const_value, symbol_name); |
| 7406 | global_value = var->const_value->global_refs->llvm_global; | |
| 7394 | global_value = var->const_value->llvm_global; | |
| 7407 | 7395 | |
| 7408 | 7396 | if (exported) { |
| 7409 | 7397 | LLVMSetLinkage(global_value, to_llvm_linkage(linkage)); |
| ... | ... | @@ -7418,7 +7406,7 @@ static void do_code_gen(CodeGen *g) { |
| 7418 | 7406 | // Here we use const_value->type because that's the type of the llvm global, |
| 7419 | 7407 | // which we const ptr cast upon use to whatever it needs to be. |
| 7420 | 7408 | if (var->gen_is_const && var->const_value->type->id != ZigTypeIdFn) { |
| 7421 | gen_global_var(g, var, var->const_value->global_refs->llvm_value, var->const_value->type); | |
| 7409 | gen_global_var(g, var, var->const_value->llvm_value, var->const_value->type); | |
| 7422 | 7410 | } |
| 7423 | 7411 | |
| 7424 | 7412 | LLVMSetGlobalConstant(global_value, var->gen_is_const); |
| ... | ... | @@ -8012,31 +8000,26 @@ static void define_intern_values(CodeGen *g) { |
| 8012 | 8000 | { |
| 8013 | 8001 | auto& value = g->intern.x_undefined; |
| 8014 | 8002 | value.type = g->builtin_types.entry_undef; |
| 8015 | value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.undefined"); | |
| 8016 | 8003 | value.special = ConstValSpecialStatic; |
| 8017 | 8004 | } |
| 8018 | 8005 | { |
| 8019 | 8006 | auto& value = g->intern.x_void; |
| 8020 | 8007 | value.type = g->builtin_types.entry_void; |
| 8021 | value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.void"); | |
| 8022 | 8008 | value.special = ConstValSpecialStatic; |
| 8023 | 8009 | } |
| 8024 | 8010 | { |
| 8025 | 8011 | auto& value = g->intern.x_null; |
| 8026 | 8012 | value.type = g->builtin_types.entry_null; |
| 8027 | value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.null"); | |
| 8028 | 8013 | value.special = ConstValSpecialStatic; |
| 8029 | 8014 | } |
| 8030 | 8015 | { |
| 8031 | 8016 | auto& value = g->intern.x_unreachable; |
| 8032 | 8017 | value.type = g->builtin_types.entry_unreachable; |
| 8033 | value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.unreachable"); | |
| 8034 | 8018 | value.special = ConstValSpecialStatic; |
| 8035 | 8019 | } |
| 8036 | 8020 | { |
| 8037 | 8021 | auto& value = g->intern.zero_byte; |
| 8038 | 8022 | value.type = g->builtin_types.entry_u8; |
| 8039 | value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.zero_byte"); | |
| 8040 | 8023 | value.special = ConstValSpecialStatic; |
| 8041 | 8024 | bigint_init_unsigned(&value.data.x_bigint, 0); |
| 8042 | 8025 | } |
| ... | ... | @@ -8669,19 +8652,10 @@ static void init(CodeGen *g) { |
| 8669 | 8652 | g->invalid_instruction = &sentinel_instructions[0]; |
| 8670 | 8653 | g->invalid_instruction->value = allocate<ZigValue>(1, "ZigValue"); |
| 8671 | 8654 | g->invalid_instruction->value->type = g->builtin_types.entry_invalid; |
| 8672 | g->invalid_instruction->value->global_refs = allocate<ConstGlobalRefs>(1); | |
| 8673 | 8655 | |
| 8674 | 8656 | g->unreach_instruction = &sentinel_instructions[1]; |
| 8675 | 8657 | g->unreach_instruction->value = allocate<ZigValue>(1, "ZigValue"); |
| 8676 | 8658 | g->unreach_instruction->value->type = g->builtin_types.entry_unreachable; |
| 8677 | g->unreach_instruction->value->global_refs = allocate<ConstGlobalRefs>(1); | |
| 8678 | ||
| 8679 | { | |
| 8680 | ConstGlobalRefs *global_refs = allocate<ConstGlobalRefs>(PanicMsgIdCount); | |
| 8681 | for (size_t i = 0; i < PanicMsgIdCount; i += 1) { | |
| 8682 | g->panic_msg_vals[i].global_refs = &global_refs[i]; | |
| 8683 | } | |
| 8684 | } | |
| 8685 | 8659 | |
| 8686 | 8660 | define_builtin_fns(g); |
| 8687 | 8661 | Error err; |
src/ir.cpp+49-63| ... | ... | @@ -223,7 +223,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source |
| 223 | 223 | static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *ptr, |
| 224 | 224 | ZigType *dest_type, IrInstruction *dest_type_src, bool safety_check_on); |
| 225 | 225 | static ZigValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed); |
| 226 | static void copy_const_val(ZigValue *dest, ZigValue *src, bool same_global_refs); | |
| 226 | static void copy_const_val(ZigValue *dest, ZigValue *src); | |
| 227 | 227 | static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align); |
| 228 | 228 | static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *target, |
| 229 | 229 | ZigType *ptr_type); |
| ... | ... | @@ -1545,7 +1545,6 @@ static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_no |
| 1545 | 1545 | special_instruction->base.debug_id = exec_next_debug_id(irb->exec); |
| 1546 | 1546 | special_instruction->base.owner_bb = irb->current_basic_block; |
| 1547 | 1547 | special_instruction->base.value = allocate<ZigValue>(1, "ZigValue"); |
| 1548 | special_instruction->base.value->global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs"); | |
| 1549 | 1548 | return special_instruction; |
| 1550 | 1549 | } |
| 1551 | 1550 | |
| ... | ... | @@ -9122,7 +9121,7 @@ static Error eval_comptime_ptr_reinterpret(IrAnalyze *ira, CodeGen *codegen, Ast |
| 9122 | 9121 | if ((err = ir_read_const_ptr(ira, codegen, source_node, &tmp, ptr_val))) |
| 9123 | 9122 | return err; |
| 9124 | 9123 | ZigValue *child_val = const_ptr_pointee_unchecked(codegen, ptr_val); |
| 9125 | copy_const_val(child_val, &tmp, false); | |
| 9124 | copy_const_val(child_val, &tmp); | |
| 9126 | 9125 | return ErrorNone; |
| 9127 | 9126 | } |
| 9128 | 9127 | |
| ... | ... | @@ -11348,18 +11347,14 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT |
| 11348 | 11347 | } |
| 11349 | 11348 | } |
| 11350 | 11349 | |
| 11351 | static void copy_const_val(ZigValue *dest, ZigValue *src, bool same_global_refs) { | |
| 11352 | ConstGlobalRefs *global_refs = dest->global_refs; | |
| 11350 | static void copy_const_val(ZigValue *dest, ZigValue *src) { | |
| 11353 | 11351 | memcpy(dest, src, sizeof(ZigValue)); |
| 11354 | if (!same_global_refs) { | |
| 11355 | dest->global_refs = global_refs; | |
| 11356 | if (src->special != ConstValSpecialStatic) | |
| 11357 | return; | |
| 11358 | if (dest->type->id == ZigTypeIdStruct) { | |
| 11359 | dest->data.x_struct.fields = alloc_const_vals_ptrs(dest->type->data.structure.src_field_count); | |
| 11360 | for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) { | |
| 11361 | copy_const_val(dest->data.x_struct.fields[i], src->data.x_struct.fields[i], false); | |
| 11362 | } | |
| 11352 | if (src->special != ConstValSpecialStatic) | |
| 11353 | return; | |
| 11354 | if (dest->type->id == ZigTypeIdStruct) { | |
| 11355 | dest->data.x_struct.fields = alloc_const_vals_ptrs(dest->type->data.structure.src_field_count); | |
| 11356 | for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) { | |
| 11357 | copy_const_val(dest->data.x_struct.fields[i], src->data.x_struct.fields[i]); | |
| 11363 | 11358 | } |
| 11364 | 11359 | } |
| 11365 | 11360 | } |
| ... | ... | @@ -11378,13 +11373,11 @@ static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInstruction *source_ |
| 11378 | 11373 | case CastOpErrSet: |
| 11379 | 11374 | case CastOpBitCast: |
| 11380 | 11375 | zig_panic("TODO"); |
| 11381 | case CastOpNoop: | |
| 11382 | { | |
| 11383 | bool same_global_refs = other_val->special == ConstValSpecialStatic; | |
| 11384 | copy_const_val(const_val, other_val, same_global_refs); | |
| 11385 | const_val->type = new_type; | |
| 11386 | break; | |
| 11387 | } | |
| 11376 | case CastOpNoop: { | |
| 11377 | copy_const_val(const_val, other_val); | |
| 11378 | const_val->type = new_type; | |
| 11379 | break; | |
| 11380 | } | |
| 11388 | 11381 | case CastOpNumLitToConcrete: |
| 11389 | 11382 | if (other_val->type->id == ZigTypeIdComptimeFloat) { |
| 11390 | 11383 | assert(new_type->id == ZigTypeIdFloat); |
| ... | ... | @@ -12105,7 +12098,7 @@ static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *so |
| 12105 | 12098 | source_instr->scope, source_instr->source_node); |
| 12106 | 12099 | const_instruction->base.value->special = ConstValSpecialStatic; |
| 12107 | 12100 | if (types_have_same_zig_comptime_repr(ira->codegen, wanted_type, payload_type)) { |
| 12108 | copy_const_val(const_instruction->base.value, val, val->data.x_ptr.mut == ConstPtrMutComptimeConst); | |
| 12101 | copy_const_val(const_instruction->base.value, val); | |
| 12109 | 12102 | } else { |
| 12110 | 12103 | const_instruction->base.value->data.x_optional = val; |
| 12111 | 12104 | } |
| ... | ... | @@ -13063,7 +13056,7 @@ static IrInstruction *ir_analyze_array_to_vector(IrAnalyze *ira, IrInstruction * |
| 13063 | 13056 | if (instr_is_comptime(array)) { |
| 13064 | 13057 | // arrays and vectors have the same ZigValue representation |
| 13065 | 13058 | IrInstruction *result = ir_const(ira, source_instr, vector_type); |
| 13066 | copy_const_val(result->value, array->value, false); | |
| 13059 | copy_const_val(result->value, array->value); | |
| 13067 | 13060 | result->value->type = vector_type; |
| 13068 | 13061 | return result; |
| 13069 | 13062 | } |
| ... | ... | @@ -13076,7 +13069,7 @@ static IrInstruction *ir_analyze_vector_to_array(IrAnalyze *ira, IrInstruction * |
| 13076 | 13069 | if (instr_is_comptime(vector)) { |
| 13077 | 13070 | // arrays and vectors have the same ZigValue representation |
| 13078 | 13071 | IrInstruction *result = ir_const(ira, source_instr, array_type); |
| 13079 | copy_const_val(result->value, vector->value, false); | |
| 13072 | copy_const_val(result->value, vector->value); | |
| 13080 | 13073 | result->value->type = array_type; |
| 13081 | 13074 | return result; |
| 13082 | 13075 | } |
| ... | ... | @@ -13364,7 +13357,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst |
| 13364 | 13357 | if (wanted_type->id == ZigTypeIdComptimeInt || wanted_type->id == ZigTypeIdInt) { |
| 13365 | 13358 | IrInstruction *result = ir_const(ira, source_instr, wanted_type); |
| 13366 | 13359 | if (actual_type->id == ZigTypeIdComptimeInt || actual_type->id == ZigTypeIdInt) { |
| 13367 | copy_const_val(result->value, value->value, false); | |
| 13360 | copy_const_val(result->value, value->value); | |
| 13368 | 13361 | result->value->type = wanted_type; |
| 13369 | 13362 | } else { |
| 13370 | 13363 | float_init_bigint(&result->value->data.x_bigint, value->value); |
| ... | ... | @@ -14224,7 +14217,7 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio |
| 14224 | 14217 | |
| 14225 | 14218 | static IrInstruction *ir_analyze_instruction_const(IrAnalyze *ira, IrInstructionConst *instruction) { |
| 14226 | 14219 | IrInstruction *result = ir_const(ira, &instruction->base, nullptr); |
| 14227 | copy_const_val(result->value, instruction->base.value, true); | |
| 14220 | copy_const_val(result->value, instruction->base.value); | |
| 14228 | 14221 | return result; |
| 14229 | 14222 | } |
| 14230 | 14223 | |
| ... | ... | @@ -14763,7 +14756,7 @@ never_mind_just_calculate_it_normally: |
| 14763 | 14756 | &op1_val->data.x_array.data.s_none.elements[i], |
| 14764 | 14757 | &op2_val->data.x_array.data.s_none.elements[i], |
| 14765 | 14758 | bin_op_instruction, op_id, one_possible_value); |
| 14766 | copy_const_val(&result->value->data.x_array.data.s_none.elements[i], cur_res->value, false); | |
| 14759 | copy_const_val(&result->value->data.x_array.data.s_none.elements[i], cur_res->value); | |
| 14767 | 14760 | } |
| 14768 | 14761 | return result; |
| 14769 | 14762 | } |
| ... | ... | @@ -15629,21 +15622,21 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i |
| 15629 | 15622 | size_t next_index = 0; |
| 15630 | 15623 | for (size_t i = op1_array_index; i < op1_array_end; i += 1, next_index += 1) { |
| 15631 | 15624 | ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index]; |
| 15632 | copy_const_val(elem_dest_val, &op1_array_val->data.x_array.data.s_none.elements[i], false); | |
| 15625 | copy_const_val(elem_dest_val, &op1_array_val->data.x_array.data.s_none.elements[i]); | |
| 15633 | 15626 | elem_dest_val->parent.id = ConstParentIdArray; |
| 15634 | 15627 | elem_dest_val->parent.data.p_array.array_val = out_array_val; |
| 15635 | 15628 | elem_dest_val->parent.data.p_array.elem_index = next_index; |
| 15636 | 15629 | } |
| 15637 | 15630 | for (size_t i = op2_array_index; i < op2_array_end; i += 1, next_index += 1) { |
| 15638 | 15631 | ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index]; |
| 15639 | copy_const_val(elem_dest_val, &op2_array_val->data.x_array.data.s_none.elements[i], false); | |
| 15632 | copy_const_val(elem_dest_val, &op2_array_val->data.x_array.data.s_none.elements[i]); | |
| 15640 | 15633 | elem_dest_val->parent.id = ConstParentIdArray; |
| 15641 | 15634 | elem_dest_val->parent.data.p_array.array_val = out_array_val; |
| 15642 | 15635 | elem_dest_val->parent.data.p_array.elem_index = next_index; |
| 15643 | 15636 | } |
| 15644 | 15637 | if (next_index < full_len) { |
| 15645 | 15638 | ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index]; |
| 15646 | copy_const_val(elem_dest_val, sentinel, false); | |
| 15639 | copy_const_val(elem_dest_val, sentinel); | |
| 15647 | 15640 | elem_dest_val->parent.id = ConstParentIdArray; |
| 15648 | 15641 | elem_dest_val->parent.data.p_array.array_val = out_array_val; |
| 15649 | 15642 | elem_dest_val->parent.data.p_array.elem_index = next_index; |
| ... | ... | @@ -15728,7 +15721,7 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp * |
| 15728 | 15721 | for (uint64_t x = 0; x < mult_amt; x += 1) { |
| 15729 | 15722 | for (uint64_t y = 0; y < old_array_len; y += 1) { |
| 15730 | 15723 | ZigValue *elem_dest_val = &out_val->data.x_array.data.s_none.elements[i]; |
| 15731 | copy_const_val(elem_dest_val, &array_val->data.x_array.data.s_none.elements[y], false); | |
| 15724 | copy_const_val(elem_dest_val, &array_val->data.x_array.data.s_none.elements[y]); | |
| 15732 | 15725 | elem_dest_val->parent.id = ConstParentIdArray; |
| 15733 | 15726 | elem_dest_val->parent.data.p_array.array_val = out_val; |
| 15734 | 15727 | elem_dest_val->parent.data.p_array.elem_index = i; |
| ... | ... | @@ -15739,7 +15732,7 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp * |
| 15739 | 15732 | |
| 15740 | 15733 | if (array_type->data.array.sentinel != nullptr) { |
| 15741 | 15734 | ZigValue *elem_dest_val = &out_val->data.x_array.data.s_none.elements[i]; |
| 15742 | copy_const_val(elem_dest_val, array_type->data.array.sentinel, false); | |
| 15735 | copy_const_val(elem_dest_val, array_type->data.array.sentinel); | |
| 15743 | 15736 | elem_dest_val->parent.id = ConstParentIdArray; |
| 15744 | 15737 | elem_dest_val->parent.data.p_array.array_val = out_val; |
| 15745 | 15738 | elem_dest_val->parent.data.p_array.elem_index = i; |
| ... | ... | @@ -15889,7 +15882,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira, |
| 15889 | 15882 | var->const_value = init_val; |
| 15890 | 15883 | } else { |
| 15891 | 15884 | var->const_value = create_const_vals(1); |
| 15892 | copy_const_val(var->const_value, init_val, false); | |
| 15885 | copy_const_val(var->const_value, init_val); | |
| 15893 | 15886 | } |
| 15894 | 15887 | } |
| 15895 | 15888 | } |
| ... | ... | @@ -15999,7 +15992,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira, |
| 15999 | 15992 | if (instr_is_comptime(var_ptr) && var->mem_slot_index != SIZE_MAX) { |
| 16000 | 15993 | assert(var->mem_slot_index < ira->exec_context.mem_slot_list.length); |
| 16001 | 15994 | ZigValue *mem_slot = ira->exec_context.mem_slot_list.at(var->mem_slot_index); |
| 16002 | copy_const_val(mem_slot, init_val, !is_comptime_var || var->gen_is_const); | |
| 15995 | copy_const_val(mem_slot, init_val); | |
| 16003 | 15996 | ira_ref(var->owner_exec->analysis); |
| 16004 | 15997 | |
| 16005 | 15998 | if (is_comptime_var || (var_class_requires_const && var->gen_is_const)) { |
| ... | ... | @@ -16480,8 +16473,8 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe |
| 16480 | 16473 | } |
| 16481 | 16474 | IrInstruction *alloca_gen; |
| 16482 | 16475 | if (is_comptime && value != nullptr) { |
| 16483 | if (align > value->value->global_refs->align) { | |
| 16484 | value->value->global_refs->align = align; | |
| 16476 | if (align > value->value->llvm_align) { | |
| 16477 | value->value->llvm_align = align; | |
| 16485 | 16478 | } |
| 16486 | 16479 | alloca_gen = ir_get_ref(ira, result_loc->source_instruction, value, true, false); |
| 16487 | 16480 | } else { |
| ... | ... | @@ -17045,7 +17038,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod |
| 17045 | 17038 | arg_val = create_const_runtime(casted_arg->value->type); |
| 17046 | 17039 | } |
| 17047 | 17040 | if (arg_part_of_generic_id) { |
| 17048 | copy_const_val(&generic_id->params[generic_id->param_count], arg_val, true); | |
| 17041 | copy_const_val(&generic_id->params[generic_id->param_count], arg_val); | |
| 17049 | 17042 | generic_id->param_count += 1; |
| 17050 | 17043 | } |
| 17051 | 17044 | |
| ... | ... | @@ -17226,7 +17219,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source |
| 17226 | 17219 | IrInstruction *casted_ptr; |
| 17227 | 17220 | if (instr_is_comptime(ptr)) { |
| 17228 | 17221 | casted_ptr = ir_const(ira, source_instr, struct_ptr_type); |
| 17229 | copy_const_val(casted_ptr->value, ptr->value, false); | |
| 17222 | copy_const_val(casted_ptr->value, ptr->value); | |
| 17230 | 17223 | casted_ptr->value->type = struct_ptr_type; |
| 17231 | 17224 | } else { |
| 17232 | 17225 | casted_ptr = ir_build_cast(&ira->new_irb, source_instr->scope, |
| ... | ... | @@ -17289,14 +17282,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source |
| 17289 | 17282 | if (dest_val == nullptr) |
| 17290 | 17283 | return ira->codegen->invalid_instruction; |
| 17291 | 17284 | if (dest_val->special != ConstValSpecialRuntime) { |
| 17292 | // TODO this allows a value stored to have the original value modified and then | |
| 17293 | // have that affect what should be a copy. We need some kind of advanced copy-on-write | |
| 17294 | // system to make these two tests pass at the same time: | |
| 17295 | // * "string literal used as comptime slice is memoized" | |
| 17296 | // * "comptime modification of const struct field" - except modified to avoid | |
| 17297 | // ConstPtrMutComptimeVar, thus defeating the logic below. | |
| 17298 | bool same_global_refs = ptr->value->data.x_ptr.mut != ConstPtrMutComptimeVar; | |
| 17299 | copy_const_val(dest_val, value->value, same_global_refs); | |
| 17285 | copy_const_val(dest_val, value->value); | |
| 17300 | 17286 | if (ptr->value->data.x_ptr.mut == ConstPtrMutComptimeVar && |
| 17301 | 17287 | !ira->new_irb.current_basic_block->must_be_comptime_source_instr) |
| 17302 | 17288 | { |
| ... | ... | @@ -17571,7 +17557,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c |
| 17571 | 17557 | } |
| 17572 | 17558 | |
| 17573 | 17559 | IrInstruction *new_instruction = ir_const(ira, &call_instruction->base, result->type); |
| 17574 | copy_const_val(new_instruction->value, result, true); | |
| 17560 | copy_const_val(new_instruction->value, result); | |
| 17575 | 17561 | new_instruction->value->type = return_type; |
| 17576 | 17562 | return ir_finish_anal(ira, new_instruction); |
| 17577 | 17563 | } |
| ... | ... | @@ -17728,7 +17714,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c |
| 17728 | 17714 | nullptr, UndefBad); |
| 17729 | 17715 | IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb, |
| 17730 | 17716 | impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr); |
| 17731 | copy_const_val(const_instruction->base.value, align_result, true); | |
| 17717 | copy_const_val(const_instruction->base.value, align_result); | |
| 17732 | 17718 | |
| 17733 | 17719 | uint32_t align_bytes = 0; |
| 17734 | 17720 | ir_resolve_align(ira, &const_instruction->base, nullptr, &align_bytes); |
| ... | ... | @@ -18058,7 +18044,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source |
| 18058 | 18044 | |
| 18059 | 18045 | if (dst_size <= src_size) { |
| 18060 | 18046 | if (src_size == dst_size && types_have_same_zig_comptime_repr(codegen, out_val->type, pointee->type)) { |
| 18061 | copy_const_val(out_val, pointee, ptr_val->data.x_ptr.mut != ConstPtrMutComptimeVar); | |
| 18047 | copy_const_val(out_val, pointee); | |
| 18062 | 18048 | return ErrorNone; |
| 18063 | 18049 | } |
| 18064 | 18050 | Buf buf = BUF_INIT; |
| ... | ... | @@ -18421,7 +18407,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh |
| 18421 | 18407 | |
| 18422 | 18408 | if (value->value->special != ConstValSpecialRuntime) { |
| 18423 | 18409 | IrInstruction *result = ir_const(ira, &phi_instruction->base, nullptr); |
| 18424 | copy_const_val(result->value, value->value, true); | |
| 18410 | copy_const_val(result->value, value->value); | |
| 18425 | 18411 | return result; |
| 18426 | 18412 | } else { |
| 18427 | 18413 | return value; |
| ... | ... | @@ -18814,7 +18800,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct |
| 18814 | 18800 | if (index == array_len && array_type->data.array.sentinel != nullptr) { |
| 18815 | 18801 | ZigType *elem_type = array_type->data.array.child_type; |
| 18816 | 18802 | IrInstruction *sentinel_elem = ir_const(ira, &elem_ptr_instruction->base, elem_type); |
| 18817 | copy_const_val(sentinel_elem->value, array_type->data.array.sentinel, false); | |
| 18803 | copy_const_val(sentinel_elem->value, array_type->data.array.sentinel); | |
| 18818 | 18804 | return ir_get_ref(ira, &elem_ptr_instruction->base, sentinel_elem, true, false); |
| 18819 | 18805 | } |
| 18820 | 18806 | if (index >= array_len) { |
| ... | ... | @@ -19305,7 +19291,7 @@ static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_n |
| 19305 | 19291 | |
| 19306 | 19292 | if (instr_is_comptime(container_ptr)) { |
| 19307 | 19293 | IrInstruction *result = ir_const(ira, source_instr, field_ptr_type); |
| 19308 | copy_const_val(result->value, container_ptr->value, false); | |
| 19294 | copy_const_val(result->value, container_ptr->value); | |
| 19309 | 19295 | result->value->type = field_ptr_type; |
| 19310 | 19296 | return result; |
| 19311 | 19297 | } |
| ... | ... | @@ -20737,7 +20723,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira, |
| 20737 | 20723 | case ZigTypeIdErrorSet: { |
| 20738 | 20724 | if (pointee_val) { |
| 20739 | 20725 | IrInstruction *result = ir_const(ira, &switch_target_instruction->base, nullptr); |
| 20740 | copy_const_val(result->value, pointee_val, true); | |
| 20726 | copy_const_val(result->value, pointee_val); | |
| 20741 | 20727 | result->value->type = target_type; |
| 20742 | 20728 | return result; |
| 20743 | 20729 | } |
| ... | ... | @@ -21233,7 +21219,7 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc |
| 21233 | 21219 | return ira->codegen->invalid_instruction; |
| 21234 | 21220 | |
| 21235 | 21221 | IrInstruction *runtime_inst = ir_const(ira, instruction, field->init_val->type); |
| 21236 | copy_const_val(runtime_inst->value, field->init_val, true); | |
| 21222 | copy_const_val(runtime_inst->value, field->init_val); | |
| 21237 | 21223 | |
| 21238 | 21224 | IrInstruction *field_ptr = ir_analyze_struct_field_ptr(ira, instruction, field, result_loc, |
| 21239 | 21225 | container_type, true); |
| ... | ... | @@ -21493,7 +21479,7 @@ static IrInstruction *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstruct |
| 21493 | 21479 | err->cached_error_name_val = create_const_slice(ira->codegen, array_val, 0, buf_len(&err->name), true); |
| 21494 | 21480 | } |
| 21495 | 21481 | IrInstruction *result = ir_const(ira, &instruction->base, nullptr); |
| 21496 | copy_const_val(result->value, err->cached_error_name_val, true); | |
| 21482 | copy_const_val(result->value, err->cached_error_name_val); | |
| 21497 | 21483 | result->value->type = str_type; |
| 21498 | 21484 | return result; |
| 21499 | 21485 | } |
| ... | ... | @@ -22978,7 +22964,7 @@ static IrInstruction *ir_analyze_instruction_type_name(IrAnalyze *ira, IrInstruc |
| 22978 | 22964 | type_entry->cached_const_name_val = create_const_str_lit(ira->codegen, type_bare_name(type_entry)); |
| 22979 | 22965 | } |
| 22980 | 22966 | IrInstruction *result = ir_const(ira, &instruction->base, nullptr); |
| 22981 | copy_const_val(result->value, type_entry->cached_const_name_val, true); | |
| 22967 | copy_const_val(result->value, type_entry->cached_const_name_val); | |
| 22982 | 22968 | return result; |
| 22983 | 22969 | } |
| 22984 | 22970 | |
| ... | ... | @@ -23678,7 +23664,7 @@ static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstruct |
| 23678 | 23664 | |
| 23679 | 23665 | ZigValue *ptr_val = result->value->data.x_struct.fields[slice_ptr_index]; |
| 23680 | 23666 | ZigValue *target_ptr_val = target_val->data.x_struct.fields[slice_ptr_index]; |
| 23681 | copy_const_val(ptr_val, target_ptr_val, false); | |
| 23667 | copy_const_val(ptr_val, target_ptr_val); | |
| 23682 | 23668 | ptr_val->type = dest_ptr_type; |
| 23683 | 23669 | |
| 23684 | 23670 | ZigValue *len_val = result->value->data.x_struct.fields[slice_len_index]; |
| ... | ... | @@ -23971,7 +23957,7 @@ static IrInstruction *ir_analyze_shuffle_vector(IrAnalyze *ira, IrInstruction *s |
| 23971 | 23957 | ZigValue *src_elem_val = (v >= 0) ? |
| 23972 | 23958 | &a->value->data.x_array.data.s_none.elements[v] : |
| 23973 | 23959 | &b->value->data.x_array.data.s_none.elements[~v]; |
| 23974 | copy_const_val(result_elem_val, src_elem_val, false); | |
| 23960 | copy_const_val(result_elem_val, src_elem_val); | |
| 23975 | 23961 | |
| 23976 | 23962 | ir_assert(result_elem_val->special == ConstValSpecialStatic, source_instr); |
| 23977 | 23963 | } |
| ... | ... | @@ -24066,7 +24052,7 @@ static IrInstruction *ir_analyze_instruction_splat(IrAnalyze *ira, IrInstruction |
| 24066 | 24052 | IrInstruction *result = ir_const(ira, &instruction->base, return_type); |
| 24067 | 24053 | result->value->data.x_array.data.s_none.elements = create_const_vals(len_int); |
| 24068 | 24054 | for (uint32_t i = 0; i < len_int; i += 1) { |
| 24069 | copy_const_val(&result->value->data.x_array.data.s_none.elements[i], scalar_val, false); | |
| 24055 | copy_const_val(&result->value->data.x_array.data.s_none.elements[i], scalar_val); | |
| 24070 | 24056 | } |
| 24071 | 24057 | return result; |
| 24072 | 24058 | } |
| ... | ... | @@ -24207,7 +24193,7 @@ static IrInstruction *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructio |
| 24207 | 24193 | } |
| 24208 | 24194 | |
| 24209 | 24195 | for (size_t i = start; i < end; i += 1) { |
| 24210 | copy_const_val(&dest_elements[i], byte_val, true); | |
| 24196 | copy_const_val(&dest_elements[i], byte_val); | |
| 24211 | 24197 | } |
| 24212 | 24198 | |
| 24213 | 24199 | return ir_const_void(ira, &instruction->base); |
| ... | ... | @@ -24386,7 +24372,7 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio |
| 24386 | 24372 | // TODO check for noalias violations - this should be generalized to work for any function |
| 24387 | 24373 | |
| 24388 | 24374 | for (size_t i = 0; i < count; i += 1) { |
| 24389 | copy_const_val(&dest_elements[dest_start + i], &src_elements[src_start + i], true); | |
| 24375 | copy_const_val(&dest_elements[dest_start + i], &src_elements[src_start + i]); | |
| 24390 | 24376 | } |
| 24391 | 24377 | |
| 24392 | 24378 | return ir_const_void(ira, &instruction->base); |
| ... | ... | @@ -25828,7 +25814,7 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3 |
| 25828 | 25814 | } |
| 25829 | 25815 | |
| 25830 | 25816 | IrInstruction *result = ir_const(ira, target, result_type); |
| 25831 | copy_const_val(result->value, val, true); | |
| 25817 | copy_const_val(result->value, val); | |
| 25832 | 25818 | result->value->type = result_type; |
| 25833 | 25819 | return result; |
| 25834 | 25820 | } |
| ... | ... | @@ -25910,7 +25896,7 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_ |
| 25910 | 25896 | } else { |
| 25911 | 25897 | result = ir_const(ira, source_instr, dest_type); |
| 25912 | 25898 | } |
| 25913 | copy_const_val(result->value, val, true); | |
| 25899 | copy_const_val(result->value, val); | |
| 25914 | 25900 | result->value->type = dest_type; |
| 25915 | 25901 | |
| 25916 | 25902 | // Keep the bigger alignment, it can only help- |