| ... | ... | @@ -603,16 +603,30 @@ static LLVMValueRef get_floor_ceil_fn(CodeGen *g, TypeTableEntry *type_entry, Zi |
| 603 | 603 | return fn_val; |
| 604 | 604 | } |
| 605 | 605 | |
| 606 | static LLVMValueRef gen_load_untyped(CodeGen *g, LLVMValueRef ptr, uint32_t alignment, bool is_volatile, |
| 607 | const char *name) |
| 608 | { |
| 609 | LLVMValueRef result = LLVMBuildLoad(g->builder, ptr, name); |
| 610 | if (is_volatile) LLVMSetVolatile(result, true); |
| 611 | if (alignment == 0) { |
| 612 | LLVMSetAlignment(result, LLVMABIAlignmentOfType(g->target_data_ref, LLVMGetElementType(LLVMTypeOf(ptr)))); |
| 613 | } else { |
| 614 | LLVMSetAlignment(result, alignment); |
| 615 | } |
| 616 | return result; |
| 617 | } |
| 618 | |
| 619 | static LLVMValueRef gen_load(CodeGen *g, LLVMValueRef ptr, TypeTableEntry *ptr_type, const char *name) { |
| 620 | return gen_load_untyped(g, ptr, ptr_type->data.pointer.alignment, ptr_type->data.pointer.is_volatile, name); |
| 621 | } |
| 622 | |
| 606 | 623 | static LLVMValueRef get_handle_value(CodeGen *g, LLVMValueRef ptr, TypeTableEntry *type, TypeTableEntry *ptr_type) { |
| 607 | 624 | if (type_has_bits(type)) { |
| 608 | 625 | if (handle_is_ptr(type)) { |
| 609 | 626 | return ptr; |
| 610 | 627 | } else { |
| 611 | 628 | assert(ptr_type->id == TypeTableEntryIdPointer); |
| 612 | | LLVMValueRef result = LLVMBuildLoad(g->builder, ptr, ""); |
| 613 | | LLVMSetVolatile(result, ptr_type->data.pointer.is_volatile); |
| 614 | | LLVMSetAlignment(result, ptr_type->data.pointer.alignment); |
| 615 | | return result; |
| 629 | return gen_load(g, ptr, ptr_type, ""); |
| 616 | 630 | } |
| 617 | 631 | } else { |
| 618 | 632 | return nullptr; |
| ... | ... | @@ -727,8 +741,8 @@ static void gen_panic(CodeGen *g, LLVMValueRef msg_arg) { |
| 727 | 741 | LLVMValueRef ptr_ptr = LLVMBuildStructGEP(g->builder, msg_arg, (unsigned)ptr_index, ""); |
| 728 | 742 | LLVMValueRef len_ptr = LLVMBuildStructGEP(g->builder, msg_arg, (unsigned)len_index, ""); |
| 729 | 743 | |
| 730 | | LLVMValueRef msg_ptr = LLVMBuildLoad(g->builder, ptr_ptr, ""); |
| 731 | | LLVMValueRef msg_len = LLVMBuildLoad(g->builder, len_ptr, ""); |
| 744 | LLVMValueRef msg_ptr = gen_load_untyped(g, ptr_ptr, 0, false, ""); |
| 745 | LLVMValueRef msg_len = gen_load_untyped(g, len_ptr, 0, false, ""); |
| 732 | 746 | gen_panic_raw(g, msg_ptr, msg_len); |
| 733 | 747 | } |
| 734 | 748 | |
| ... | ... | @@ -828,10 +842,10 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) { |
| 828 | 842 | LLVMValueRef err_name_val = LLVMBuildInBoundsGEP(g->builder, g->err_name_table, err_table_indices, 2, ""); |
| 829 | 843 | |
| 830 | 844 | LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, err_name_val, slice_ptr_index, ""); |
| 831 | | LLVMValueRef err_name_ptr = LLVMBuildLoad(g->builder, ptr_field_ptr, ""); |
| 845 | LLVMValueRef err_name_ptr = gen_load_untyped(g, ptr_field_ptr, 0, false, ""); |
| 832 | 846 | |
| 833 | 847 | LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, err_name_val, slice_len_index, ""); |
| 834 | | LLVMValueRef err_name_len = LLVMBuildLoad(g->builder, len_field_ptr, ""); |
| 848 | LLVMValueRef err_name_len = gen_load_untyped(g, len_field_ptr, 0, false, ""); |
| 835 | 849 | |
| 836 | 850 | LLVMValueRef params[] = { |
| 837 | 851 | offset_buf_ptr, // dest pointer |
| ... | ... | @@ -1081,7 +1095,7 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, TypeTableEntry |
| 1081 | 1095 | return nullptr; |
| 1082 | 1096 | } |
| 1083 | 1097 | |
| 1084 | | LLVMValueRef containing_int = LLVMBuildLoad(g->builder, ptr, ""); |
| 1098 | LLVMValueRef containing_int = gen_load(g, ptr, ptr_type, ""); |
| 1085 | 1099 | |
| 1086 | 1100 | uint32_t bit_offset = ptr_type->data.pointer.bit_offset; |
| 1087 | 1101 | uint32_t host_bit_count = LLVMGetIntTypeWidth(LLVMTypeOf(containing_int)); |
| ... | ... | @@ -1144,7 +1158,7 @@ static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrIns |
| 1144 | 1158 | gen_assign_raw(g, g->cur_ret_ptr, get_pointer_to_type(g, return_type, false), value); |
| 1145 | 1159 | LLVMBuildRetVoid(g->builder); |
| 1146 | 1160 | } else { |
| 1147 | | LLVMValueRef by_val_value = LLVMBuildLoad(g->builder, value, ""); |
| 1161 | LLVMValueRef by_val_value = gen_load_untyped(g, value, 0, false, ""); |
| 1148 | 1162 | LLVMBuildRet(g->builder, by_val_value); |
| 1149 | 1163 | } |
| 1150 | 1164 | } else { |
| ... | ... | @@ -1654,7 +1668,7 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable, |
| 1654 | 1668 | size_t wanted_len_index = wanted_type->data.structure.fields[slice_len_index].gen_index; |
| 1655 | 1669 | |
| 1656 | 1670 | LLVMValueRef src_ptr_ptr = LLVMBuildStructGEP(g->builder, expr_val, (unsigned)actual_ptr_index, ""); |
| 1657 | | LLVMValueRef src_ptr = LLVMBuildLoad(g->builder, src_ptr_ptr, ""); |
| 1671 | LLVMValueRef src_ptr = gen_load_untyped(g, src_ptr_ptr, 0, false, ""); |
| 1658 | 1672 | LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, src_ptr, |
| 1659 | 1673 | wanted_type->data.structure.fields[0].type_entry->type_ref, ""); |
| 1660 | 1674 | LLVMValueRef dest_ptr_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr, |
| ... | ... | @@ -1662,7 +1676,7 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable, |
| 1662 | 1676 | LLVMBuildStore(g->builder, src_ptr_casted, dest_ptr_ptr); |
| 1663 | 1677 | |
| 1664 | 1678 | LLVMValueRef src_len_ptr = LLVMBuildStructGEP(g->builder, expr_val, (unsigned)actual_len_index, ""); |
| 1665 | | LLVMValueRef src_len = LLVMBuildLoad(g->builder, src_len_ptr, ""); |
| 1679 | LLVMValueRef src_len = gen_load_untyped(g, src_len_ptr, 0, false, ""); |
| 1666 | 1680 | uint64_t src_size = type_size(g, actual_child_type); |
| 1667 | 1681 | uint64_t dest_size = type_size(g, wanted_child_type); |
| 1668 | 1682 | |
| ... | ... | @@ -2036,8 +2050,7 @@ static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrI |
| 2036 | 2050 | return get_handle_value(g, ptr, child_type, ptr_type); |
| 2037 | 2051 | |
| 2038 | 2052 | assert(!handle_is_ptr(child_type)); |
| 2039 | | LLVMValueRef containing_int = LLVMBuildLoad(g->builder, ptr, ""); |
| 2040 | | LLVMSetVolatile(containing_int, ptr_type->data.pointer.is_volatile); |
| 2053 | LLVMValueRef containing_int = gen_load(g, ptr, ptr_type, ""); |
| 2041 | 2054 | |
| 2042 | 2055 | uint32_t bit_offset = ptr_type->data.pointer.bit_offset; |
| 2043 | 2056 | uint32_t host_bit_count = LLVMGetIntTypeWidth(LLVMTypeOf(containing_int)); |
| ... | ... | @@ -2133,14 +2146,14 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI |
| 2133 | 2146 | size_t len_index = array_type->data.structure.fields[1].gen_index; |
| 2134 | 2147 | assert(len_index != SIZE_MAX); |
| 2135 | 2148 | LLVMValueRef len_ptr = LLVMBuildStructGEP(g->builder, array_ptr, (unsigned)len_index, ""); |
| 2136 | | LLVMValueRef len = LLVMBuildLoad(g->builder, len_ptr, ""); |
| 2149 | LLVMValueRef len = gen_load_untyped(g, len_ptr, 0, false, ""); |
| 2137 | 2150 | add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, len); |
| 2138 | 2151 | } |
| 2139 | 2152 | |
| 2140 | 2153 | size_t ptr_index = array_type->data.structure.fields[0].gen_index; |
| 2141 | 2154 | assert(ptr_index != SIZE_MAX); |
| 2142 | 2155 | LLVMValueRef ptr_ptr = LLVMBuildStructGEP(g->builder, array_ptr, (unsigned)ptr_index, ""); |
| 2143 | | LLVMValueRef ptr = LLVMBuildLoad(g->builder, ptr_ptr, ""); |
| 2156 | LLVMValueRef ptr = gen_load_untyped(g, ptr_ptr, 0, false, ""); |
| 2144 | 2157 | return LLVMBuildInBoundsGEP(g->builder, ptr, &subscript_value, 1, ""); |
| 2145 | 2158 | } else { |
| 2146 | 2159 | zig_unreachable(); |
| ... | ... | @@ -2388,7 +2401,7 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, TypeTableEntry *maybe_type, LLV |
| 2388 | 2401 | return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), ""); |
| 2389 | 2402 | } else { |
| 2390 | 2403 | LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, maybe_handle, maybe_null_index, ""); |
| 2391 | | return LLVMBuildLoad(g->builder, maybe_field_ptr, ""); |
| 2404 | return gen_load_untyped(g, maybe_field_ptr, 0, false, ""); |
| 2392 | 2405 | } |
| 2393 | 2406 | } |
| 2394 | 2407 | } |
| ... | ... | @@ -2607,12 +2620,6 @@ static LLVMValueRef ir_render_field_parent_ptr(CodeGen *g, IrExecutable *executa |
| 2607 | 2620 | } |
| 2608 | 2621 | } |
| 2609 | 2622 | |
| 2610 | | static LLVMValueRef get_default_aligned_load(CodeGen *g, LLVMValueRef ptr) { |
| 2611 | | LLVMValueRef result = LLVMBuildLoad(g->builder, ptr, ""); |
| 2612 | | LLVMSetAlignment(result, LLVMABIAlignmentOfType(g->target_data_ref, LLVMGetElementType(LLVMTypeOf(ptr)))); |
| 2613 | | return result; |
| 2614 | | } |
| 2615 | | |
| 2616 | 2623 | static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, IrInstructionAlignCast *instruction) { |
| 2617 | 2624 | LLVMValueRef target_val = ir_llvm_value(g, instruction->target); |
| 2618 | 2625 | assert(target_val); |
| ... | ... | @@ -2648,7 +2655,7 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I |
| 2648 | 2655 | |
| 2649 | 2656 | size_t ptr_index = target_type->data.structure.fields[slice_ptr_index].gen_index; |
| 2650 | 2657 | LLVMValueRef ptr_val_ptr = LLVMBuildStructGEP(g->builder, target_val, (unsigned)ptr_index, ""); |
| 2651 | | ptr_val = get_default_aligned_load(g, ptr_val_ptr); |
| 2658 | ptr_val = gen_load_untyped(g, ptr_val_ptr, 0, false, ""); |
| 2652 | 2659 | } else { |
| 2653 | 2660 | zig_unreachable(); |
| 2654 | 2661 | } |
| ... | ... | @@ -2858,7 +2865,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst |
| 2858 | 2865 | LLVMValueRef prev_end = nullptr; |
| 2859 | 2866 | if (!instruction->end || want_debug_safety) { |
| 2860 | 2867 | LLVMValueRef src_len_ptr = LLVMBuildStructGEP(g->builder, array_ptr, (unsigned)len_index, ""); |
| 2861 | | prev_end = LLVMBuildLoad(g->builder, src_len_ptr, ""); |
| 2868 | prev_end = gen_load_untyped(g, src_len_ptr, 0, false, ""); |
| 2862 | 2869 | } |
| 2863 | 2870 | |
| 2864 | 2871 | LLVMValueRef start_val = ir_llvm_value(g, instruction->start); |
| ... | ... | @@ -2878,7 +2885,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst |
| 2878 | 2885 | } |
| 2879 | 2886 | |
| 2880 | 2887 | LLVMValueRef src_ptr_ptr = LLVMBuildStructGEP(g->builder, array_ptr, (unsigned)ptr_index, ""); |
| 2881 | | LLVMValueRef src_ptr = LLVMBuildLoad(g->builder, src_ptr_ptr, ""); |
| 2888 | LLVMValueRef src_ptr = gen_load_untyped(g, src_ptr_ptr, 0, false, ""); |
| 2882 | 2889 | LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, (unsigned)ptr_index, ""); |
| 2883 | 2890 | LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, src_ptr, &start_val, (unsigned)len_index, ""); |
| 2884 | 2891 | LLVMBuildStore(g->builder, slice_start_ptr, ptr_field_ptr); |
| ... | ... | @@ -3023,7 +3030,7 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI |
| 3023 | 3030 | LLVMValueRef err_val; |
| 3024 | 3031 | if (type_has_bits(child_type)) { |
| 3025 | 3032 | LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, ""); |
| 3026 | | err_val = LLVMBuildLoad(g->builder, err_val_ptr, ""); |
| 3033 | err_val = gen_load_untyped(g, err_val_ptr, 0, false, ""); |
| 3027 | 3034 | } else { |
| 3028 | 3035 | err_val = err_union_handle; |
| 3029 | 3036 | } |
| ... | ... | @@ -3042,7 +3049,7 @@ static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executab |
| 3042 | 3049 | |
| 3043 | 3050 | if (type_has_bits(child_type)) { |
| 3044 | 3051 | LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, ""); |
| 3045 | | return LLVMBuildLoad(g->builder, err_val_ptr, ""); |
| 3052 | return gen_load_untyped(g, err_val_ptr, 0, false, ""); |
| 3046 | 3053 | } else { |
| 3047 | 3054 | return err_union_handle; |
| 3048 | 3055 | } |
| ... | ... | @@ -3060,7 +3067,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu |
| 3060 | 3067 | LLVMValueRef err_val; |
| 3061 | 3068 | if (type_has_bits(child_type)) { |
| 3062 | 3069 | LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, ""); |
| 3063 | | err_val = LLVMBuildLoad(g->builder, err_val_ptr, ""); |
| 3070 | err_val = gen_load_untyped(g, err_val_ptr, 0, false, ""); |
| 3064 | 3071 | } else { |
| 3065 | 3072 | err_val = err_union_handle; |
| 3066 | 3073 | } |
| ... | ... | @@ -3922,6 +3929,7 @@ static void generate_error_name_table(CodeGen *g) { |
| 3922 | 3929 | LLVMSetLinkage(str_global, LLVMPrivateLinkage); |
| 3923 | 3930 | LLVMSetGlobalConstant(str_global, true); |
| 3924 | 3931 | LLVMSetUnnamedAddr(str_global, true); |
| 3932 | LLVMSetAlignment(str_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(str_init))); |
| 3925 | 3933 | |
| 3926 | 3934 | LLVMValueRef fields[] = { |
| 3927 | 3935 | LLVMConstBitCast(str_global, u8_ptr_type->type_ref), |
| ... | ... | @@ -3938,6 +3946,7 @@ static void generate_error_name_table(CodeGen *g) { |
| 3938 | 3946 | LLVMSetLinkage(g->err_name_table, LLVMPrivateLinkage); |
| 3939 | 3947 | LLVMSetGlobalConstant(g->err_name_table, true); |
| 3940 | 3948 | LLVMSetUnnamedAddr(g->err_name_table, true); |
| 3949 | LLVMSetAlignment(g->err_name_table, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(err_name_table_init))); |
| 3941 | 3950 | } |
| 3942 | 3951 | |
| 3943 | 3952 | static void generate_enum_name_tables(CodeGen *g) { |
| ... | ... | @@ -3960,6 +3969,7 @@ static void generate_enum_name_tables(CodeGen *g) { |
| 3960 | 3969 | LLVMSetLinkage(str_global, LLVMPrivateLinkage); |
| 3961 | 3970 | LLVMSetGlobalConstant(str_global, true); |
| 3962 | 3971 | LLVMSetUnnamedAddr(str_global, true); |
| 3972 | LLVMSetAlignment(str_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(str_init))); |
| 3963 | 3973 | |
| 3964 | 3974 | LLVMValueRef fields[] = { |
| 3965 | 3975 | LLVMConstBitCast(str_global, u8_ptr_type->type_ref), |
| ... | ... | @@ -3976,6 +3986,7 @@ static void generate_enum_name_tables(CodeGen *g) { |
| 3976 | 3986 | LLVMSetLinkage(name_table, LLVMPrivateLinkage); |
| 3977 | 3987 | LLVMSetGlobalConstant(name_table, true); |
| 3978 | 3988 | LLVMSetUnnamedAddr(name_table, true); |
| 3989 | LLVMSetAlignment(name_table, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(name_table_init))); |
| 3979 | 3990 | enum_tag_type->data.enum_tag.name_table = name_table; |
| 3980 | 3991 | } |
| 3981 | 3992 | } |