authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-11 13:13:45-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-11 13:13:45-05:00
log39287d7346436a87ce785866befa77351bf2fc85
tree0c3e20f4bd3c0dea8f42486177f88f49b71be09c
parent12049aa43b154f75445e616c2699aaff017d8228

rework compile-time known pointer values

See #257

6 files changed, 629 insertions(+), 298 deletions(-)

src/all_types.hpp+55-22
......@@ -67,6 +67,27 @@ enum OutType {
6767 OutTypeObj,
6868};
6969
70enum ConstParentId {
71 ConstParentIdNone,
72 ConstParentIdStruct,
73 ConstParentIdArray,
74};
75
76struct ConstParent {
77 ConstParentId id;
78
79 union {
80 struct {
81 ConstExprValue *array_val;
82 size_t elem_index;
83 } p_array;
84 struct {
85 ConstExprValue *struct_val;
86 size_t field_index;
87 } p_struct;
88 } data;
89};
90
7091struct ConstEnumValue {
7192 uint64_t tag;
7293 ConstExprValue *payload;
......@@ -74,43 +95,55 @@ struct ConstEnumValue {
7495
7596struct ConstStructValue {
7697 ConstExprValue *fields;
98 ConstParent parent;
7799};
78100
79101struct ConstArrayValue {
80102 ConstExprValue *elements;
81 // This will be the same as `len` from the type, but we duplicate the information
82 // in the constant value so that pointers pointing to arrays can see this size.
83 // TODO now that ConstExprValue has the type field we can use that instead of this.
84 size_t size;
85 // If the data for this array is supposed to be contained in a different constant
86 // value, we link to the parent here. This way getting a pointer to this constant
87 // value can return a pointer into the parent data structure.
88 ConstExprValue *parent_array;
89 size_t parent_array_index;
103 ConstParent parent;
90104};
91105
92106enum ConstPtrSpecial {
93 ConstPtrSpecialNone,
94 // This helps us preserve the null byte when performing compile-time
95 // concatenation on C strings.
96 ConstPtrSpecialCStr,
97 // This means that the pointer points to inline memory, so attempting
98 // to write a non-compile-time known value is an error
99 ConstPtrSpecialInline,
107 // Enforce explicitly setting this ID by making the zero value invalid.
108 ConstPtrSpecialInvalid,
109 // The pointer is a reference to a single object.
110 ConstPtrSpecialRef,
111 // The pointer points to an element in an underlying array.
112 ConstPtrSpecialBaseArray,
113 // The pointer points to a field in an underlying struct.
114 ConstPtrSpecialBaseStruct,
100115 // This means that we did a compile-time pointer reinterpret and we cannot
101116 // understand the value of pointee at compile time. However, we will still
102117 // emit a binary with a compile time known address.
103118 // In this case index is the numeric address value.
104 ConstPtrSpecialRuntime,
119 ConstPtrSpecialHardCodedAddr,
105120};
106121
107122struct ConstPtrValue {
108 ConstExprValue *base_ptr;
109 // If index is SIZE_MAX, then base_ptr points directly to child type.
110 // Otherwise base_ptr points to an array const val and index is offset
111 // in object units from base_ptr into the block of memory pointed to
112 size_t index;
113123 ConstPtrSpecial special;
124 // This means that the pointer points to memory used by a comptime variable,
125 // so attempting to write a non-compile-time known value is an error
126 bool comptime_var_mem;
127
128 union {
129 struct {
130 ConstExprValue *pointee;
131 } ref;
132 struct {
133 ConstExprValue *array_val;
134 size_t elem_index;
135 // This helps us preserve the null byte when performing compile-time
136 // concatenation on C strings.
137 bool is_cstr;
138 } base_array;
139 struct {
140 ConstExprValue *struct_val;
141 size_t field_index;
142 } base_struct;
143 struct {
144 uint64_t addr;
145 } hard_coded_addr;
146 } data;
114147};
115148
116149struct ConstErrValue {
src/analyze.cpp+110-32
......@@ -2832,7 +2832,33 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
28322832 return const_val->data.x_arg_tuple.start_index * 281907309 +
28332833 const_val->data.x_arg_tuple.end_index * 2290442768;
28342834 case TypeTableEntryIdPointer:
2835 return hash_ptr(const_val->data.x_ptr.base_ptr) + hash_size(const_val->data.x_ptr.index);
2835 {
2836 uint32_t hash_val = const_val->data.x_ptr.comptime_var_mem ? 2216297012 : 170810250;
2837 switch (const_val->data.x_ptr.special) {
2838 case ConstPtrSpecialInvalid:
2839 zig_unreachable();
2840 case ConstPtrSpecialRef:
2841 hash_val += 2478261866;
2842 hash_val += hash_ptr(const_val->data.x_ptr.data.ref.pointee);
2843 return hash_val;
2844 case ConstPtrSpecialBaseArray:
2845 hash_val += 1764906839;
2846 hash_val += hash_ptr(const_val->data.x_ptr.data.base_array.array_val);
2847 hash_val += hash_size(const_val->data.x_ptr.data.base_array.elem_index);
2848 hash_val += const_val->data.x_ptr.data.base_array.is_cstr ? 1297263887 : 200363492;
2849 return hash_val;
2850 case ConstPtrSpecialBaseStruct:
2851 hash_val += 3518317043;
2852 hash_val += hash_ptr(const_val->data.x_ptr.data.base_struct.struct_val);
2853 hash_val += hash_size(const_val->data.x_ptr.data.base_struct.field_index);
2854 return hash_val;
2855 case ConstPtrSpecialHardCodedAddr:
2856 hash_val += 4048518294;
2857 hash_val += hash_size(const_val->data.x_ptr.data.hard_coded_addr.addr);
2858 return hash_val;
2859 }
2860 zig_unreachable();
2861 }
28362862 case TypeTableEntryIdUndefLit:
28372863 return 162837799;
28382864 case TypeTableEntryIdNullLit:
......@@ -3007,7 +3033,6 @@ void init_const_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
30073033 const_val->special = ConstValSpecialStatic;
30083034 const_val->type = get_array_type(g, g->builtin_types.entry_u8, buf_len(str));
30093035 const_val->data.x_array.elements = allocate<ConstExprValue>(buf_len(str));
3010 const_val->data.x_array.size = buf_len(str);
30113036
30123037 for (size_t i = 0; i < buf_len(str); i += 1) {
30133038 ConstExprValue *this_char = &const_val->data.x_array.elements[i];
......@@ -3030,7 +3055,6 @@ void init_const_c_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
30303055 array_val->special = ConstValSpecialStatic;
30313056 array_val->type = get_array_type(g, g->builtin_types.entry_u8, len_with_null);
30323057 array_val->data.x_array.elements = allocate<ConstExprValue>(len_with_null);
3033 array_val->data.x_array.size = len_with_null;
30343058 for (size_t i = 0; i < buf_len(str); i += 1) {
30353059 ConstExprValue *this_char = &array_val->data.x_array.elements[i];
30363060 this_char->special = ConstValSpecialStatic;
......@@ -3045,9 +3069,10 @@ void init_const_c_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
30453069 // then make the pointer point to it
30463070 const_val->special = ConstValSpecialStatic;
30473071 const_val->type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
3048 const_val->data.x_ptr.base_ptr = array_val;
3049 const_val->data.x_ptr.index = 0;
3050 const_val->data.x_ptr.special = ConstPtrSpecialCStr;
3072 const_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
3073 const_val->data.x_ptr.data.base_array.array_val = array_val;
3074 const_val->data.x_ptr.data.base_array.elem_index = 0;
3075 const_val->data.x_ptr.data.base_array.is_cstr = true;
30513076}
30523077ConstExprValue *create_const_c_str_lit(CodeGen *g, Buf *str) {
30533078 ConstExprValue *const_val = allocate<ConstExprValue>(1);
......@@ -3156,7 +3181,7 @@ void init_const_slice(CodeGen *g, ConstExprValue *const_val, ConstExprValue *arr
31563181 const_val->type = get_slice_type(g, array_val->type->data.array.child_type, is_const);
31573182 const_val->data.x_struct.fields = allocate<ConstExprValue>(2);
31583183
3159 init_const_ptr(g, &const_val->data.x_struct.fields[slice_ptr_index], array_val, start, is_const);
3184 init_const_ptr_array(g, &const_val->data.x_struct.fields[slice_ptr_index], array_val, start, is_const);
31603185 init_const_usize(g, &const_val->data.x_struct.fields[slice_len_index], len);
31613186}
31623187
......@@ -3166,24 +3191,35 @@ ConstExprValue *create_const_slice(CodeGen *g, ConstExprValue *array_val, size_t
31663191 return const_val;
31673192}
31683193
3169void init_const_ptr(CodeGen *g, ConstExprValue *const_val, ConstExprValue *base_ptr, size_t index, bool is_const) {
3170 TypeTableEntry *child_type;
3171 if (index == SIZE_MAX) {
3172 child_type = base_ptr->type;
3173 } else {
3174 assert(base_ptr->type->id == TypeTableEntryIdArray);
3175 child_type = base_ptr->type->data.array.child_type;
3176 }
3194void init_const_ptr_array(CodeGen *g, ConstExprValue *const_val, ConstExprValue *array_val,
3195 size_t elem_index, bool is_const)
3196{
3197 assert(array_val->type->id == TypeTableEntryIdArray);
3198 TypeTableEntry *child_type = array_val->type->data.array.child_type;
31773199
31783200 const_val->special = ConstValSpecialStatic;
31793201 const_val->type = get_pointer_to_type(g, child_type, is_const);
3180 const_val->data.x_ptr.base_ptr = base_ptr;
3181 const_val->data.x_ptr.index = index;
3202 const_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
3203 const_val->data.x_ptr.data.base_array.array_val = array_val;
3204 const_val->data.x_ptr.data.base_array.elem_index = elem_index;
3205}
3206
3207ConstExprValue *create_const_ptr_array(CodeGen *g, ConstExprValue *array_val, size_t elem_index, bool is_const) {
3208 ConstExprValue *const_val = allocate<ConstExprValue>(1);
3209 init_const_ptr_array(g, const_val, array_val, elem_index, is_const);
3210 return const_val;
3211}
3212
3213void init_const_ptr_ref(CodeGen *g, ConstExprValue *const_val, ConstExprValue *pointee_val, bool is_const) {
3214 const_val->special = ConstValSpecialStatic;
3215 const_val->type = get_pointer_to_type(g, pointee_val->type, is_const);
3216 const_val->data.x_ptr.special = ConstPtrSpecialRef;
3217 const_val->data.x_ptr.data.ref.pointee = pointee_val;
31823218}
31833219
3184ConstExprValue *create_const_ptr(CodeGen *g, ConstExprValue *base_ptr, size_t index, bool is_const) {
3220ConstExprValue *create_const_ptr_ref(CodeGen *g, ConstExprValue *pointee_val, bool is_const) {
31853221 ConstExprValue *const_val = allocate<ConstExprValue>(1);
3186 init_const_ptr(g, const_val, base_ptr, index, is_const);
3222 init_const_ptr_ref(g, const_val, pointee_val, is_const);
31873223 return const_val;
31883224}
31893225
......@@ -3207,14 +3243,14 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
32073243 const_val->special = ConstValSpecialStatic;
32083244 size_t elem_count = canon_wanted_type->data.array.len;
32093245 const_val->data.x_array.elements = allocate<ConstExprValue>(elem_count);
3210 const_val->data.x_array.size = elem_count;
32113246 for (size_t i = 0; i < elem_count; i += 1) {
32123247 ConstExprValue *element_val = &const_val->data.x_array.elements[i];
32133248 element_val->type = canon_wanted_type->data.array.child_type;
32143249 init_const_undefined(g, element_val);
32153250 if (get_underlying_type(element_val->type)->id == TypeTableEntryIdArray) {
3216 element_val->data.x_array.parent_array = const_val;
3217 element_val->data.x_array.parent_array_index = i;
3251 element_val->data.x_array.parent.id = ConstParentIdArray;
3252 element_val->data.x_array.parent.data.p_array.array_val = const_val;
3253 element_val->data.x_array.parent.data.p_array.elem_index = i;
32183254 }
32193255 }
32203256 } else if (canon_wanted_type->id == TypeTableEntryIdStruct) {
......@@ -3301,9 +3337,37 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
33013337 case TypeTableEntryIdEnumTag:
33023338 return bignum_cmp_eq(&a->data.x_bignum, &b->data.x_bignum);
33033339 case TypeTableEntryIdPointer:
3304 if (a->data.x_ptr.index != b->data.x_ptr.index)
3340 if (a->data.x_ptr.special != b->data.x_ptr.special)
3341 return false;
3342 if (a->data.x_ptr.comptime_var_mem != b->data.x_ptr.comptime_var_mem)
33053343 return false;
3306 return a->data.x_ptr.base_ptr == b->data.x_ptr.base_ptr;
3344 switch (a->data.x_ptr.special) {
3345 case ConstPtrSpecialInvalid:
3346 zig_unreachable();
3347 case ConstPtrSpecialRef:
3348 if (a->data.x_ptr.data.ref.pointee != b->data.x_ptr.data.ref.pointee)
3349 return false;
3350 return true;
3351 case ConstPtrSpecialBaseArray:
3352 if (a->data.x_ptr.data.base_array.array_val != b->data.x_ptr.data.base_array.array_val)
3353 return false;
3354 if (a->data.x_ptr.data.base_array.elem_index != b->data.x_ptr.data.base_array.elem_index)
3355 return false;
3356 if (a->data.x_ptr.data.base_array.is_cstr != b->data.x_ptr.data.base_array.is_cstr)
3357 return false;
3358 return true;
3359 case ConstPtrSpecialBaseStruct:
3360 if (a->data.x_ptr.data.base_struct.struct_val != b->data.x_ptr.data.base_struct.struct_val)
3361 return false;
3362 if (a->data.x_ptr.data.base_struct.field_index != b->data.x_ptr.data.base_struct.field_index)
3363 return false;
3364 return true;
3365 case ConstPtrSpecialHardCodedAddr:
3366 if (a->data.x_ptr.data.hard_coded_addr.addr != b->data.x_ptr.data.hard_coded_addr.addr)
3367 return false;
3368 return true;
3369 }
3370 zig_unreachable();
33073371 case TypeTableEntryIdArray:
33083372 zig_panic("TODO");
33093373 case TypeTableEntryIdStruct:
......@@ -3482,15 +3546,29 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
34823546 return;
34833547 }
34843548 case TypeTableEntryIdPointer:
3485 buf_appendf(buf, "&");
3486 if (const_val->data.x_ptr.special == ConstPtrSpecialRuntime) {
3487 buf_appendf(buf, "(runtime pointer value)");
3488 } else if (const_val->data.x_ptr.special == ConstPtrSpecialCStr) {
3489 buf_appendf(buf, "(c str lit)");
3490 } else {
3491 render_const_value(buf, const_ptr_pointee(const_val));
3549 switch (const_val->data.x_ptr.special) {
3550 case ConstPtrSpecialInvalid:
3551 zig_unreachable();
3552 case ConstPtrSpecialRef:
3553 case ConstPtrSpecialBaseStruct:
3554 buf_appendf(buf, "&");
3555 render_const_value(buf, const_ptr_pointee(const_val));
3556 return;
3557 case ConstPtrSpecialBaseArray:
3558 if (const_val->data.x_ptr.data.base_array.is_cstr) {
3559 buf_appendf(buf, "&(c str lit)");
3560 return;
3561 } else {
3562 buf_appendf(buf, "&");
3563 render_const_value(buf, const_ptr_pointee(const_val));
3564 return;
3565 }
3566 case ConstPtrSpecialHardCodedAddr:
3567 buf_appendf(buf, "(&%s)(%" PRIx64 ")", buf_ptr(&canon_type->data.pointer.child_type->name),
3568 const_val->data.x_ptr.data.hard_coded_addr.addr);
3569 return;
34923570 }
3493 return;
3571 zig_unreachable();
34943572 case TypeTableEntryIdFn:
34953573 {
34963574 FnTableEntry *fn_entry = const_val->data.x_fn;
src/analyze.hpp+6-3
......@@ -126,9 +126,12 @@ ConstExprValue *create_const_type(CodeGen *g, TypeTableEntry *type_value);
126126void init_const_runtime(ConstExprValue *const_val, TypeTableEntry *type);
127127ConstExprValue *create_const_runtime(TypeTableEntry *type);
128128
129void init_const_ptr(CodeGen *g, ConstExprValue *const_val, ConstExprValue *base_ptr, size_t index, bool is_const);
130ConstExprValue *create_const_ptr(CodeGen *g, ConstExprValue *const_val, ConstExprValue *base_ptr,
131 size_t index, bool is_const);
129void init_const_ptr_ref(CodeGen *g, ConstExprValue *const_val, ConstExprValue *pointee_val, bool is_const);
130ConstExprValue *create_const_ptr_ref(CodeGen *g, ConstExprValue *pointee_val, bool is_const);
131
132void init_const_ptr_array(CodeGen *g, ConstExprValue *const_val, ConstExprValue *array_val,
133 size_t elem_index, bool is_const);
134ConstExprValue *create_const_ptr_array(CodeGen *g, ConstExprValue *array_val, size_t elem_index, bool is_const);
132135
133136void init_const_slice(CodeGen *g, ConstExprValue *const_val, ConstExprValue *array_val,
134137 size_t start, size_t len, bool is_const);
src/codegen.cpp+94-37
......@@ -2527,17 +2527,29 @@ static void ir_render(CodeGen *g, FnTableEntry *fn_entry) {
25272527 }
25282528}
25292529
2530static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *array_const_val, size_t index) {
2531 ConstExprValue *parent_array = array_const_val->data.x_array.parent_array;
2532 LLVMValueRef base_ptr;
2533 if (parent_array) {
2534 size_t parent_array_index = array_const_val->data.x_array.parent_array_index;
2535 base_ptr = gen_const_ptr_array_recursive(g, parent_array, parent_array_index);
2536 } else {
2537 render_const_val(g, array_const_val);
2538 render_const_val_global(g, array_const_val, "");
2539 base_ptr = array_const_val->llvm_global;
2530static LLVMValueRef gen_const_ptr_struct_recursive(CodeGen *g, ConstExprValue *struct_const_val, size_t field_index);
2531static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *array_const_val, size_t index);
2532
2533static LLVMValueRef gen_parent_ptr(CodeGen *g, ConstExprValue *val, ConstParent *parent) {
2534 switch (parent->id) {
2535 case ConstParentIdNone:
2536 render_const_val(g, val);
2537 render_const_val_global(g, val, "");
2538 return val->llvm_global;
2539 case ConstParentIdStruct:
2540 return gen_const_ptr_struct_recursive(g, parent->data.p_struct.struct_val,
2541 parent->data.p_struct.field_index);
2542 case ConstParentIdArray:
2543 return gen_const_ptr_array_recursive(g, parent->data.p_array.array_val,
2544 parent->data.p_array.elem_index);
25402545 }
2546 zig_unreachable();
2547}
2548
2549static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *array_const_val, size_t index) {
2550 ConstParent *parent = &array_const_val->data.x_array.parent;
2551 LLVMValueRef base_ptr = gen_parent_ptr(g, array_const_val, parent);
2552
25412553 TypeTableEntry *usize = g->builtin_types.entry_usize;
25422554 LLVMValueRef indices[] = {
25432555 LLVMConstNull(usize->type_ref),
......@@ -2546,6 +2558,19 @@ static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *ar
25462558 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
25472559}
25482560
2561static LLVMValueRef gen_const_ptr_struct_recursive(CodeGen *g, ConstExprValue *struct_const_val, size_t field_index) {
2562 ConstParent *parent = &struct_const_val->data.x_struct.parent;
2563 LLVMValueRef base_ptr = gen_parent_ptr(g, struct_const_val, parent);
2564
2565 TypeTableEntry *u32 = g->builtin_types.entry_u32;
2566 LLVMValueRef indices[] = {
2567 LLVMConstNull(u32->type_ref),
2568 LLVMConstInt(u32->type_ref, field_index, false),
2569 };
2570 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
2571}
2572
2573
25492574static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
25502575 TypeTableEntry *canon_type = get_underlying_type(const_val->type);
25512576 assert(!canon_type->zero_bits);
......@@ -2684,38 +2709,70 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
26842709 case TypeTableEntryIdPointer:
26852710 {
26862711 render_const_val_global(g, const_val, "");
2687 size_t index = const_val->data.x_ptr.index;
2688 ConstExprValue *base_ptr = const_val->data.x_ptr.base_ptr;
2689 if (base_ptr) {
2690 if (index == SIZE_MAX) {
2691 render_const_val(g, base_ptr);
2692 render_const_val_global(g, base_ptr, "");
2693 ConstExprValue *other_val = base_ptr;
2694 const_val->llvm_value = LLVMConstBitCast(other_val->llvm_global, const_val->type->type_ref);
2695 render_const_val_global(g, const_val, "");
2696 return const_val->llvm_value;
2697 } else {
2698 ConstExprValue *array_const_val = base_ptr;
2699 assert(array_const_val->type->id == TypeTableEntryIdArray);
2700 if (array_const_val->type->zero_bits) {
2701 // make this a null pointer
2712 switch (const_val->data.x_ptr.special) {
2713 case ConstPtrSpecialInvalid:
2714 zig_unreachable();
2715 case ConstPtrSpecialRef:
2716 {
2717 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;
2718 render_const_val(g, pointee);
2719 render_const_val_global(g, pointee, "");
2720 ConstExprValue *other_val = pointee;
2721 const_val->llvm_value = LLVMConstBitCast(other_val->llvm_global, const_val->type->type_ref);
2722 render_const_val_global(g, const_val, "");
2723 return const_val->llvm_value;
2724 }
2725 case ConstPtrSpecialBaseArray:
2726 {
2727 ConstExprValue *array_const_val = const_val->data.x_ptr.data.base_array.array_val;
2728 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;
2729 assert(array_const_val->type->id == TypeTableEntryIdArray);
2730 if (array_const_val->type->zero_bits) {
2731 // make this a null pointer
2732 TypeTableEntry *usize = g->builtin_types.entry_usize;
2733 const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
2734 const_val->type->type_ref);
2735 render_const_val_global(g, const_val, "");
2736 return const_val->llvm_value;
2737 }
2738 LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val,
2739 elem_index);
2740 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
2741 const_val->llvm_value = ptr_val;
2742 render_const_val_global(g, const_val, "");
2743 return ptr_val;
2744 }
2745 case ConstPtrSpecialBaseStruct:
2746 {
2747 ConstExprValue *struct_const_val = const_val->data.x_ptr.data.base_struct.struct_val;
2748 assert(struct_const_val->type->id == TypeTableEntryIdStruct);
2749 if (struct_const_val->type->zero_bits) {
2750 // make this a null pointer
2751 TypeTableEntry *usize = g->builtin_types.entry_usize;
2752 const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
2753 const_val->type->type_ref);
2754 render_const_val_global(g, const_val, "");
2755 return const_val->llvm_value;
2756 }
2757 size_t src_field_index = const_val->data.x_ptr.data.base_struct.field_index;
2758 size_t gen_field_index =
2759 struct_const_val->type->data.structure.fields[src_field_index].gen_index;
2760 LLVMValueRef uncasted_ptr_val = gen_const_ptr_struct_recursive(g, struct_const_val,
2761 gen_field_index);
2762 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
2763 const_val->llvm_value = ptr_val;
2764 render_const_val_global(g, const_val, "");
2765 return ptr_val;
2766 }
2767 case ConstPtrSpecialHardCodedAddr:
2768 {
2769 uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;
27022770 TypeTableEntry *usize = g->builtin_types.entry_usize;
2703 const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
2771 const_val->llvm_value = LLVMConstIntToPtr(LLVMConstInt(usize->type_ref, addr_value, false),
27042772 const_val->type->type_ref);
27052773 render_const_val_global(g, const_val, "");
27062774 return const_val->llvm_value;
27072775 }
2708 LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val, index);
2709 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
2710 const_val->llvm_value = ptr_val;
2711 render_const_val_global(g, const_val, "");
2712 return ptr_val;
2713 }
2714 } else {
2715 TypeTableEntry *usize = g->builtin_types.entry_usize;
2716 const_val->llvm_value = LLVMConstIntToPtr(LLVMConstInt(usize->type_ref, index, false), const_val->type->type_ref);
2717 render_const_val_global(g, const_val, "");
2718 return const_val->llvm_value;
27192776 }
27202777 }
27212778 case TypeTableEntryIdErrorUnion:
src/ir.cpp+344-204
......@@ -64,16 +64,21 @@ static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, Typ
6464
6565ConstExprValue *const_ptr_pointee(ConstExprValue *const_val) {
6666 assert(const_val->special == ConstValSpecialStatic);
67 assert(const_val->data.x_ptr.special != ConstPtrSpecialRuntime);
68 ConstExprValue *base_ptr = const_val->data.x_ptr.base_ptr;
69 size_t index = const_val->data.x_ptr.index;
70
71 if (index == SIZE_MAX) {
72 return base_ptr;
73 } else {
74 assert(index < base_ptr->data.x_array.size);
75 return &base_ptr->data.x_array.elements[index];
67 switch (const_val->data.x_ptr.special) {
68 case ConstPtrSpecialInvalid:
69 zig_unreachable();
70 case ConstPtrSpecialRef:
71 return const_val->data.x_ptr.data.ref.pointee;
72 case ConstPtrSpecialBaseArray:
73 return &const_val->data.x_ptr.data.base_array.array_val->data.x_array.elements[
74 const_val->data.x_ptr.data.base_array.elem_index];
75 case ConstPtrSpecialBaseStruct:
76 return &const_val->data.x_ptr.data.base_struct.struct_val->data.x_struct.fields[
77 const_val->data.x_ptr.data.base_struct.field_index];
78 case ConstPtrSpecialHardCodedAddr:
79 zig_unreachable();
7680 }
81 zig_unreachable();
7782}
7883
7984static bool ir_should_inline(IrExecutable *exec, Scope *scope) {
......@@ -6209,7 +6214,7 @@ static TypeTableEntry *ir_analyze_void(IrAnalyze *ira, IrInstruction *instructio
62096214
62106215static TypeTableEntry *ir_analyze_const_ptr(IrAnalyze *ira, IrInstruction *instruction,
62116216 ConstExprValue *pointee, TypeTableEntry *pointee_type,
6212 ConstPtrSpecial special, bool ptr_is_const, bool ptr_is_volatile)
6217 bool comptime_var_mem, bool ptr_is_const, bool ptr_is_volatile)
62136218{
62146219 if (pointee_type->id == TypeTableEntryIdMetaType) {
62156220 TypeTableEntry *type_entry = pointee->data.x_type;
......@@ -6227,9 +6232,9 @@ static TypeTableEntry *ir_analyze_const_ptr(IrAnalyze *ira, IrInstruction *instr
62276232 TypeTableEntry *ptr_type = get_pointer_to_type_volatile(ira->codegen, pointee_type,
62286233 ptr_is_const, ptr_is_volatile);
62296234 ConstExprValue *const_val = ir_build_const_from(ira, instruction);
6230 const_val->data.x_ptr.base_ptr = pointee;
6231 const_val->data.x_ptr.index = SIZE_MAX;
6232 const_val->data.x_ptr.special = special;
6235 const_val->data.x_ptr.special = ConstPtrSpecialRef;
6236 const_val->data.x_ptr.comptime_var_mem = comptime_var_mem;
6237 const_val->data.x_ptr.data.ref.pointee = pointee;
62336238 return ptr_type;
62346239 }
62356240}
......@@ -6471,8 +6476,8 @@ static IrInstruction *ir_analyze_cast_ref(IrAnalyze *ira, IrInstruction *source_
64716476 source_instr->scope, source_instr->source_node);
64726477 const_instruction->base.value.type = wanted_type;
64736478 const_instruction->base.value.special = ConstValSpecialStatic;
6474 const_instruction->base.value.data.x_ptr.base_ptr = val;
6475 const_instruction->base.value.data.x_ptr.index = SIZE_MAX;
6479 const_instruction->base.value.data.x_ptr.special = ConstPtrSpecialRef;
6480 const_instruction->base.value.data.x_ptr.data.ref.pointee = val;
64766481 return &const_instruction->base;
64776482 }
64786483
......@@ -6608,10 +6613,10 @@ static IrInstruction *ir_analyze_ptr_to_int(IrAnalyze *ira, IrInstruction *sourc
66086613 ConstExprValue *val = ir_resolve_const(ira, target, UndefBad);
66096614 if (!val)
66106615 return ira->codegen->invalid_instruction;
6611 if (val->data.x_ptr.special == ConstPtrSpecialRuntime) {
6616 if (val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
66126617 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
66136618 source_instr->source_node, wanted_type);
6614 bignum_init_unsigned(&result->value.data.x_bignum, val->data.x_ptr.index);
6619 bignum_init_unsigned(&result->value.data.x_bignum, val->data.x_ptr.data.hard_coded_addr.addr);
66156620 return result;
66166621 }
66176622 }
......@@ -6633,9 +6638,8 @@ static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *sourc
66336638 return ira->codegen->invalid_instruction;
66346639 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
66356640 source_instr->source_node, wanted_type);
6636 result->value.data.x_ptr.base_ptr = nullptr;
6637 result->value.data.x_ptr.index = bignum_to_twos_complement(&val->data.x_bignum);
6638 result->value.data.x_ptr.special = ConstPtrSpecialRuntime;
6641 result->value.data.x_ptr.special = ConstPtrSpecialHardCodedAddr;
6642 result->value.data.x_ptr.data.hard_coded_addr.addr = bignum_to_twos_complement(&val->data.x_bignum);
66396643 return result;
66406644 }
66416645
......@@ -6660,9 +6664,8 @@ static IrInstruction *ir_analyze_int_lit_to_ptr(IrAnalyze *ira, IrInstruction *s
66606664
66616665 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
66626666 source_instr->source_node, wanted_type);
6663 result->value.data.x_ptr.base_ptr = nullptr;
6664 result->value.data.x_ptr.index = bignum_to_twos_complement(&val->data.x_bignum);
6665 result->value.data.x_ptr.special = ConstPtrSpecialRuntime;
6667 result->value.data.x_ptr.special = ConstPtrSpecialHardCodedAddr;
6668 result->value.data.x_ptr.data.hard_coded_addr.addr = bignum_to_twos_complement(&val->data.x_bignum);
66666669 return result;
66676670}
66686671
......@@ -7006,17 +7009,22 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
70067009 return ira->codegen->invalid_instruction;
70077010 } else if (type_entry->id == TypeTableEntryIdPointer) {
70087011 TypeTableEntry *child_type = type_entry->data.pointer.child_type;
7009 if (ptr->value.special != ConstValSpecialRuntime) {
7010 ConstExprValue *pointee = const_ptr_pointee(&ptr->value);
7011 if (pointee->special != ConstValSpecialRuntime) {
7012 IrInstruction *result = ir_create_const(&ira->new_irb, source_instruction->scope,
7013 source_instruction->source_node, child_type);
7014 result->value = *pointee;
7015 return result;
7012 if (instr_is_comptime(ptr)) {
7013 // Dereferencing a mutable pointer at compile time is not allowed
7014 // unless that pointer is from a comptime variable
7015 if (type_entry->data.pointer.is_const || ptr->value.data.x_ptr.comptime_var_mem) {
7016 ConstExprValue *pointee = const_ptr_pointee(&ptr->value);
7017 if (pointee->special != ConstValSpecialRuntime) {
7018 IrInstruction *result = ir_create_const(&ira->new_irb, source_instruction->scope,
7019 source_instruction->source_node, child_type);
7020 result->value = *pointee;
7021 return result;
7022 }
70167023 }
70177024 }
7018 // TODO if the instruction is a get pointer instruction we can skip it
7019 IrInstruction *load_ptr_instruction = ir_build_load_ptr(&ira->new_irb, source_instruction->scope, source_instruction->source_node, ptr);
7025 // TODO if the instruction is a const ref instruction we can skip it
7026 IrInstruction *load_ptr_instruction = ir_build_load_ptr(&ira->new_irb, source_instruction->scope,
7027 source_instruction->source_node, ptr);
70207028 load_ptr_instruction->value.type = child_type;
70217029 return load_ptr_instruction;
70227030 } else if (type_entry->id == TypeTableEntryIdMetaType) {
......@@ -7052,8 +7060,7 @@ static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_inst
70527060 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
70537061 if (!val)
70547062 return ira->codegen->builtin_types.entry_invalid;
7055 return ir_analyze_const_ptr(ira, source_instruction, val, value->value.type,
7056 ConstPtrSpecialNone, is_const, is_volatile);
7063 return ir_analyze_const_ptr(ira, source_instruction, val, value->value.type, false, is_const, is_volatile);
70577064 }
70587065
70597066 TypeTableEntry *ptr_type = get_pointer_to_type_volatile(ira->codegen, value->value.type, is_const, is_volatile);
......@@ -7136,13 +7143,14 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
71367143
71377144 ConstExprValue *ptr_field = &const_val->data.x_struct.fields[slice_ptr_index];
71387145 ConstExprValue *len_field = &const_val->data.x_struct.fields[slice_len_index];
7139 ConstExprValue *array_val = ptr_field->data.x_ptr.base_ptr;
7140 assert(ptr_field->data.x_ptr.index != SIZE_MAX);
7146
7147 assert(ptr_field->data.x_ptr.special == ConstPtrSpecialBaseArray);
7148 ConstExprValue *array_val = ptr_field->data.x_ptr.data.base_array.array_val;
71417149 size_t len = len_field->data.x_bignum.data.x_uint;
71427150 Buf *result = buf_alloc();
71437151 buf_resize(result, len);
71447152 for (size_t i = 0; i < len; i += 1) {
7145 size_t new_index = ptr_field->data.x_ptr.index + i;
7153 size_t new_index = ptr_field->data.x_ptr.data.base_array.elem_index + i;
71467154 ConstExprValue *char_val = &array_val->data.x_array.elements[new_index];
71477155 uint64_t big_c = char_val->data.x_bignum.data.x_uint;
71487156 assert(big_c <= UINT8_MAX);
......@@ -7591,22 +7599,24 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
75917599 child_type = op1_canon_type->data.array.child_type;
75927600 op1_array_val = op1_val;
75937601 op1_array_index = 0;
7594 op1_array_end = op1_val->data.x_array.size;
7602 op1_array_end = op1_canon_type->data.array.len;
75957603 } else if (op1_canon_type->id == TypeTableEntryIdPointer &&
75967604 op1_canon_type->data.pointer.child_type == ira->codegen->builtin_types.entry_u8 &&
7597 op1_val->data.x_ptr.special == ConstPtrSpecialCStr)
7605 op1_val->data.x_ptr.special == ConstPtrSpecialBaseArray &&
7606 op1_val->data.x_ptr.data.base_array.is_cstr)
75987607 {
75997608 child_type = op1_canon_type->data.pointer.child_type;
7600 op1_array_val = op1_val->data.x_ptr.base_ptr;
7601 op1_array_index = op1_val->data.x_ptr.index;
7602 op1_array_end = op1_array_val->data.x_array.size - 1;
7609 op1_array_val = op1_val->data.x_ptr.data.base_array.array_val;
7610 op1_array_index = op1_val->data.x_ptr.data.base_array.elem_index;
7611 op1_array_end = op1_array_val->type->data.array.len - 1;
76037612 } else if (is_slice(op1_canon_type)) {
76047613 TypeTableEntry *ptr_type = op1_canon_type->data.structure.fields[slice_ptr_index].type_entry;
76057614 child_type = ptr_type->data.pointer.child_type;
76067615 ConstExprValue *ptr_val = &op1_val->data.x_struct.fields[slice_ptr_index];
7607 op1_array_val = ptr_val->data.x_ptr.base_ptr;
7608 op1_array_index = ptr_val->data.x_ptr.index;
7609 op1_array_end = op1_array_val->data.x_array.size;
7616 assert(ptr_val->data.x_ptr.special == ConstPtrSpecialBaseArray);
7617 op1_array_val = ptr_val->data.x_ptr.data.base_array.array_val;
7618 op1_array_index = ptr_val->data.x_ptr.data.base_array.elem_index;
7619 op1_array_end = op1_array_val->type->data.array.len;
76107620 } else {
76117621 ir_add_error(ira, op1,
76127622 buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op1->value.type->name)));
......@@ -7626,10 +7636,11 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
76267636 }
76277637 op2_array_val = op2_val;
76287638 op2_array_index = 0;
7629 op2_array_end = op2_array_val->data.x_array.size;
7639 op2_array_end = op2_array_val->type->data.array.len;
76307640 } else if (op2_canon_type->id == TypeTableEntryIdPointer &&
76317641 op2_canon_type->data.pointer.child_type == ira->codegen->builtin_types.entry_u8 &&
7632 op2_val->data.x_ptr.special == ConstPtrSpecialCStr)
7642 op2_val->data.x_ptr.special == ConstPtrSpecialBaseArray &&
7643 op2_val->data.x_ptr.data.base_array.is_cstr)
76337644 {
76347645 if (child_type != ira->codegen->builtin_types.entry_u8) {
76357646 ir_add_error(ira, op2, buf_sprintf("expected array of type '%s', found '%s'",
......@@ -7637,9 +7648,9 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
76377648 buf_ptr(&op2->value.type->name)));
76387649 return ira->codegen->builtin_types.entry_invalid;
76397650 }
7640 op2_array_val = op2_val->data.x_ptr.base_ptr;
7641 op2_array_index = op2_val->data.x_ptr.index;
7642 op2_array_end = op2_array_val->data.x_array.size - 1;
7651 op2_array_val = op2_val->data.x_ptr.data.base_array.array_val;
7652 op2_array_index = op2_val->data.x_ptr.data.base_array.elem_index;
7653 op2_array_end = op2_array_val->type->data.array.len - 1;
76437654 } else if (is_slice(op2_canon_type)) {
76447655 TypeTableEntry *ptr_type = op2_canon_type->data.structure.fields[slice_ptr_index].type_entry;
76457656 if (ptr_type->data.pointer.child_type != child_type) {
......@@ -7649,9 +7660,10 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
76497660 return ira->codegen->builtin_types.entry_invalid;
76507661 }
76517662 ConstExprValue *ptr_val = &op2_val->data.x_struct.fields[slice_ptr_index];
7652 op2_array_val = ptr_val->data.x_ptr.base_ptr;
7653 op2_array_index = ptr_val->data.x_ptr.index;
7654 op2_array_end = op2_array_val->data.x_array.size;
7663 assert(ptr_val->data.x_ptr.special == ConstPtrSpecialBaseArray);
7664 op2_array_val = ptr_val->data.x_ptr.data.base_array.array_val;
7665 op2_array_index = ptr_val->data.x_ptr.data.base_array.elem_index;
7666 op2_array_end = op2_array_val->type->data.array.len;
76557667 } else {
76567668 ir_add_error(ira, op2,
76577669 buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op2->value.type->name)));
......@@ -7676,12 +7688,12 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
76767688 out_array_val = allocate<ConstExprValue>(1);
76777689 out_array_val->special = ConstValSpecialStatic;
76787690 out_array_val->type = get_array_type(ira->codegen, child_type, new_len);
7679 out_val->data.x_ptr.base_ptr = out_array_val;
7680 out_val->data.x_ptr.index = 0;
7681 out_val->data.x_ptr.special = ConstPtrSpecialCStr;
7691 out_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
7692 out_val->data.x_ptr.data.base_array.is_cstr = true;
7693 out_val->data.x_ptr.data.base_array.array_val = out_array_val;
7694 out_val->data.x_ptr.data.base_array.elem_index = 0;
76827695 }
76837696 out_array_val->data.x_array.elements = allocate<ConstExprValue>(new_len);
7684 out_array_val->data.x_array.size = new_len;
76857697
76867698 size_t next_index = 0;
76877699 for (size_t i = op1_array_index; i < op1_array_end; i += 1, next_index += 1) {
......@@ -7736,7 +7748,6 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp
77367748 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
77377749
77387750 uint64_t new_array_len = array_len.data.x_uint;
7739 out_val->data.x_array.size = new_array_len;
77407751 out_val->data.x_array.elements = allocate<ConstExprValue>(new_array_len);
77417752
77427753 uint64_t i = 0;
......@@ -8721,24 +8732,23 @@ static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruc
87218732 if (var->value.type->id == TypeTableEntryIdInvalid)
87228733 return var->value.type;
87238734
8724 bool is_comptime = ir_get_var_is_comptime(var);
8735 bool comptime_var_mem = ir_get_var_is_comptime(var);
87258736
87268737 ConstExprValue *mem_slot = nullptr;
87278738 FnTableEntry *fn_entry = scope_fn_entry(var->parent_scope);
8728 if (var->src_is_const && var->value.special == ConstValSpecialStatic) {
8739 if (var->value.special == ConstValSpecialStatic) {
87298740 mem_slot = &var->value;
8730 assert(mem_slot->special != ConstValSpecialRuntime);
87318741 } else if (fn_entry) {
87328742 // TODO once the analyze code is fully ported over to IR we won't need this SIZE_MAX thing.
8733 if (var->mem_slot_index != SIZE_MAX && (is_comptime || var->gen_is_const))
8743 if (var->mem_slot_index != SIZE_MAX && (comptime_var_mem || var->gen_is_const))
87348744 mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];
87358745 }
87368746
87378747 bool is_const = (var->value.type->id == TypeTableEntryIdMetaType) ? is_const_ptr : var->src_is_const;
87388748 bool is_volatile = (var->value.type->id == TypeTableEntryIdMetaType) ? is_volatile_ptr : false;
87398749 if (mem_slot && mem_slot->special != ConstValSpecialRuntime) {
8740 ConstPtrSpecial ptr_special = is_comptime ? ConstPtrSpecialInline : ConstPtrSpecialNone;
8741 return ir_analyze_const_ptr(ira, instruction, mem_slot, var->value.type, ptr_special, is_const, is_volatile);
8750 return ir_analyze_const_ptr(ira, instruction, mem_slot, var->value.type,
8751 comptime_var_mem, is_const, is_volatile);
87428752 } else {
87438753 ir_build_var_ptr_from(&ira->new_irb, instruction, var, is_const, is_volatile);
87448754 type_ensure_zero_bits_known(ira->codegen, var->value.type);
......@@ -8793,7 +8803,8 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
87938803 buf_sprintf("index 0 outside array of size 0"));
87948804 }
87958805 TypeTableEntry *child_type = array_type->data.array.child_type;
8796 return_type = get_pointer_to_type(ira->codegen, child_type, false);
8806 return_type = get_pointer_to_type_volatile(ira->codegen, child_type,
8807 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile);
87978808 } else if (array_type->id == TypeTableEntryIdPointer) {
87988809 return_type = array_type;
87998810 } else if (is_slice(array_type)) {
......@@ -8826,8 +8837,7 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
88268837 is_const, is_volatile);
88278838 } else {
88288839 return ir_analyze_const_ptr(ira, &elem_ptr_instruction->base, &ira->codegen->const_void_val,
8829 ira->codegen->builtin_types.entry_void, ConstPtrSpecialNone,
8830 is_const, is_volatile);
8840 ira->codegen->builtin_types.entry_void, false, is_const, is_volatile);
88318841 }
88328842 } else {
88338843 ir_add_error_node(ira, elem_ptr_instruction->base.source_node,
......@@ -8858,30 +8868,54 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
88588868 if (array_ptr->value.special != ConstValSpecialRuntime &&
88598869 (array_ptr_val = const_ptr_pointee(&array_ptr->value)) &&
88608870 array_ptr_val->special != ConstValSpecialRuntime &&
8861 (array_type->id != TypeTableEntryIdPointer || array_ptr_val->data.x_ptr.special != ConstPtrSpecialRuntime))
8871 (array_type->id != TypeTableEntryIdPointer ||
8872 array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr))
88628873 {
88638874 ConstExprValue *out_val = ir_build_const_from(ira, &elem_ptr_instruction->base);
8875 out_val->data.x_ptr.comptime_var_mem = array_ptr->value.data.x_ptr.comptime_var_mem;
88648876 if (array_type->id == TypeTableEntryIdPointer) {
8865 size_t offset = array_ptr_val->data.x_ptr.index;
88668877 size_t new_index;
88678878 size_t mem_size;
88688879 size_t old_size;
8869 if (offset == SIZE_MAX) {
8870 new_index = SIZE_MAX;
8871 mem_size = 1;
8872 old_size = 1;
8873 } else {
8874 new_index = offset + index;
8875 mem_size = array_ptr_val->data.x_ptr.base_ptr->data.x_array.size;
8876 old_size = mem_size - offset;
8880 switch (array_ptr_val->data.x_ptr.special) {
8881 case ConstPtrSpecialInvalid:
8882 zig_unreachable();
8883 case ConstPtrSpecialRef:
8884 mem_size = 1;
8885 old_size = 1;
8886 new_index = index;
8887
8888 out_val->data.x_ptr.special = ConstPtrSpecialRef;
8889 out_val->data.x_ptr.data.ref.pointee = array_ptr_val->data.x_ptr.data.ref.pointee;
8890 break;
8891 case ConstPtrSpecialBaseArray:
8892 {
8893 size_t offset = array_ptr_val->data.x_ptr.data.base_array.elem_index;
8894 new_index = offset + index;
8895 mem_size = array_ptr_val->data.x_ptr.data.base_array.array_val->type->data.array.len;
8896 old_size = mem_size - offset;
8897
8898 assert(array_ptr_val->data.x_ptr.data.base_array.array_val);
8899
8900 out_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
8901 out_val->data.x_ptr.data.base_array.array_val =
8902 array_ptr_val->data.x_ptr.data.base_array.array_val;
8903 out_val->data.x_ptr.data.base_array.elem_index = new_index;
8904 out_val->data.x_ptr.data.base_array.is_cstr =
8905 array_ptr_val->data.x_ptr.data.base_array.is_cstr;
8906
8907 break;
8908 }
8909 case ConstPtrSpecialBaseStruct:
8910 zig_panic("TODO elem ptr on a const inner struct");
8911 case ConstPtrSpecialHardCodedAddr:
8912 zig_unreachable();
88778913 }
88788914 if (new_index >= mem_size) {
88798915 ir_add_error_node(ira, elem_ptr_instruction->base.source_node,
88808916 buf_sprintf("index %" PRIu64 " outside pointer of size %" PRIu64, index, old_size));
88818917 return ira->codegen->builtin_types.entry_invalid;
88828918 }
8883 out_val->data.x_ptr.base_ptr = array_ptr_val->data.x_ptr.base_ptr;
8884 out_val->data.x_ptr.index = new_index;
88858919 } else if (is_slice(array_type)) {
88868920 ConstExprValue *ptr_field = &array_ptr_val->data.x_struct.fields[slice_ptr_index];
88878921 ConstExprValue *len_field = &array_ptr_val->data.x_struct.fields[slice_len_index];
......@@ -8892,18 +8926,35 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
88928926 index, slice_len));
88938927 return ira->codegen->builtin_types.entry_invalid;
88948928 }
8895 out_val->data.x_ptr.base_ptr = ptr_field->data.x_ptr.base_ptr;
8896 size_t offset = ptr_field->data.x_ptr.index;
8897 if (offset == SIZE_MAX) {
8898 out_val->data.x_ptr.index = SIZE_MAX;
8899 } else {
8900 uint64_t new_index = offset + index;
8901 assert(new_index < ptr_field->data.x_ptr.base_ptr->data.x_array.size);
8902 out_val->data.x_ptr.index = new_index;
8929 switch (ptr_field->data.x_ptr.special) {
8930 case ConstPtrSpecialInvalid:
8931 zig_unreachable();
8932 case ConstPtrSpecialRef:
8933 out_val->data.x_ptr.special = ConstPtrSpecialRef;
8934 out_val->data.x_ptr.data.ref.pointee = ptr_field->data.x_ptr.data.ref.pointee;
8935 break;
8936 case ConstPtrSpecialBaseArray:
8937 {
8938 size_t offset = ptr_field->data.x_ptr.data.base_array.elem_index;
8939 uint64_t new_index = offset + index;
8940 assert(new_index < ptr_field->data.x_ptr.data.base_array.array_val->type->data.array.len);
8941 out_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
8942 out_val->data.x_ptr.data.base_array.array_val =
8943 ptr_field->data.x_ptr.data.base_array.array_val;
8944 out_val->data.x_ptr.data.base_array.elem_index = new_index;
8945 out_val->data.x_ptr.data.base_array.is_cstr =
8946 ptr_field->data.x_ptr.data.base_array.is_cstr;
8947 break;
8948 }
8949 case ConstPtrSpecialBaseStruct:
8950 zig_panic("TODO elem ptr on a slice backed by const inner struct");
8951 case ConstPtrSpecialHardCodedAddr:
8952 zig_unreachable();
89038953 }
89048954 } else if (array_type->id == TypeTableEntryIdArray) {
8905 out_val->data.x_ptr.base_ptr = array_ptr_val;
8906 out_val->data.x_ptr.index = index;
8955 out_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
8956 out_val->data.x_ptr.data.base_array.array_val = array_ptr_val;
8957 out_val->data.x_ptr.data.base_array.elem_index = index;
89078958 } else {
89088959 zig_unreachable();
89098960 }
......@@ -8931,6 +8982,9 @@ static TypeTableEntry *ir_analyze_container_member_access_inner(IrAnalyze *ira,
89318982 return ira->codegen->builtin_types.entry_invalid;
89328983 TldFn *tld_fn = (TldFn *)tld;
89338984 FnTableEntry *fn_entry = tld_fn->fn_entry;
8985 if (fn_entry->type_entry->id == TypeTableEntryIdInvalid)
8986 return ira->codegen->builtin_types.entry_invalid;
8987
89348988 IrInstruction *bound_fn_value = ir_build_const_bound_fn(&ira->new_irb, field_ptr_instruction->base.scope,
89358989 field_ptr_instruction->base.source_node, fn_entry, container_ptr);
89368990 return ir_analyze_ref(ira, &field_ptr_instruction->base, bound_fn_value, true, false);
......@@ -8962,15 +9016,17 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field
89629016 if (!ptr_val)
89639017 return ira->codegen->builtin_types.entry_invalid;
89649018
8965 if (ptr_val->data.x_ptr.special != ConstPtrSpecialRuntime) {
9019 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
89669020 ConstExprValue *struct_val = const_ptr_pointee(ptr_val);
8967 if (value_is_comptime(struct_val)) {
8968 ConstExprValue *field_val = &struct_val->data.x_struct.fields[field->src_index];
8969 if (value_is_comptime(field_val)) {
8970 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base, field_val,
8971 field_val->type, ConstPtrSpecialNone, is_const, is_volatile);
8972 }
8973 }
9021 ConstExprValue *field_val = &struct_val->data.x_struct.fields[field->src_index];
9022 TypeTableEntry *ptr_type = get_pointer_to_type_volatile(ira->codegen, field_val->type,
9023 is_const, is_volatile);
9024 ConstExprValue *const_val = ir_build_const_from(ira, &field_ptr_instruction->base);
9025 const_val->data.x_ptr.special = ConstPtrSpecialBaseStruct;
9026 const_val->data.x_ptr.comptime_var_mem = container_ptr->value.data.x_ptr.comptime_var_mem;
9027 const_val->data.x_ptr.data.base_struct.struct_val = struct_val;
9028 const_val->data.x_ptr.data.base_struct.field_index = field->src_index;
9029 return ptr_type;
89749030 }
89759031 }
89769032 ir_build_struct_field_ptr_from(&ira->new_irb, &field_ptr_instruction->base, container_ptr, field);
......@@ -9032,7 +9088,7 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source
90329088 bool ptr_is_const = true;
90339089 bool ptr_is_volatile = false;
90349090 return ir_analyze_const_ptr(ira, source_instruction, const_val, fn_entry->type_entry,
9035 ConstPtrSpecialNone, ptr_is_const, ptr_is_volatile);
9091 false, ptr_is_const, ptr_is_volatile);
90369092 }
90379093 case TldIdTypeDef:
90389094 {
......@@ -9049,7 +9105,7 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source
90499105 bool ptr_is_const = true;
90509106 bool ptr_is_volatile = false;
90519107 return ir_analyze_const_ptr(ira, source_instruction, const_val, ira->codegen->builtin_types.entry_type,
9052 ConstPtrSpecialNone, ptr_is_const, ptr_is_volatile);
9108 false, ptr_is_const, ptr_is_volatile);
90539109 }
90549110 }
90559111 zig_unreachable();
......@@ -9092,7 +9148,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
90929148 bool ptr_is_const = true;
90939149 bool ptr_is_volatile = false;
90949150 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base, len_val,
9095 usize, ConstPtrSpecialNone, ptr_is_const, ptr_is_volatile);
9151 usize, false, ptr_is_const, ptr_is_volatile);
90969152 } else {
90979153 ir_add_error_node(ira, source_node,
90989154 buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name),
......@@ -9116,7 +9172,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
91169172 bool ptr_is_const = true;
91179173 bool ptr_is_volatile = false;
91189174 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base, len_val,
9119 usize, ConstPtrSpecialNone, ptr_is_const, ptr_is_volatile);
9175 usize, false, ptr_is_const, ptr_is_volatile);
91209176 } else {
91219177 ir_add_error_node(ira, source_node,
91229178 buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name),
......@@ -9155,14 +9211,14 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
91559211 bool ptr_is_volatile = false;
91569212 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
91579213 create_const_enum_tag(child_type, field->value), child_type,
9158 ConstPtrSpecialNone, ptr_is_const, ptr_is_volatile);
9214 false, ptr_is_const, ptr_is_volatile);
91599215 } else {
91609216 bool ptr_is_const = true;
91619217 bool ptr_is_volatile = false;
91629218 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
91639219 create_const_unsigned_negative(child_type->data.enumeration.tag_type, field->value, false),
91649220 child_type->data.enumeration.tag_type,
9165 ConstPtrSpecialNone, ptr_is_const, ptr_is_volatile);
9221 false, ptr_is_const, ptr_is_volatile);
91669222 }
91679223 }
91689224 }
......@@ -9187,7 +9243,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
91879243 bool ptr_is_const = true;
91889244 bool ptr_is_volatile = false;
91899245 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base, const_val,
9190 child_type, ConstPtrSpecialNone, ptr_is_const, ptr_is_volatile);
9246 child_type, false, ptr_is_const, ptr_is_volatile);
91919247 }
91929248
91939249 ir_add_error(ira, &field_ptr_instruction->base,
......@@ -9201,14 +9257,14 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
92019257 create_const_unsigned_negative(ira->codegen->builtin_types.entry_num_lit_int,
92029258 child_type->data.integral.bit_count, false),
92039259 ira->codegen->builtin_types.entry_num_lit_int,
9204 ConstPtrSpecialNone, ptr_is_const, ptr_is_volatile);
9260 false, ptr_is_const, ptr_is_volatile);
92059261 } else if (buf_eql_str(field_name, "is_signed")) {
92069262 bool ptr_is_const = true;
92079263 bool ptr_is_volatile = false;
92089264 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
92099265 create_const_bool(ira->codegen, child_type->data.integral.is_signed),
92109266 ira->codegen->builtin_types.entry_bool,
9211 ConstPtrSpecialNone, ptr_is_const, ptr_is_volatile);
9267 false, ptr_is_const, ptr_is_volatile);
92129268 } else {
92139269 ir_add_error(ira, &field_ptr_instruction->base,
92149270 buf_sprintf("type '%s' has no member called '%s'",
......@@ -9295,16 +9351,16 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru
92959351 if (casted_value == ira->codegen->invalid_instruction)
92969352 return ira->codegen->builtin_types.entry_invalid;
92979353
9298 if (ptr->value.special != ConstValSpecialRuntime && ptr->value.data.x_ptr.special != ConstPtrSpecialRuntime) {
9299 bool is_inline = (ptr->value.data.x_ptr.special == ConstPtrSpecialInline);
9300 if (casted_value->value.special != ConstValSpecialRuntime) {
9301 ConstExprValue *dest_val = const_ptr_pointee(&ptr->value);
9302 if (dest_val->special != ConstValSpecialRuntime) {
9303 *dest_val = casted_value->value;
9304 return ir_analyze_void(ira, &store_ptr_instruction->base);
9354 if (instr_is_comptime(ptr) && ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
9355 bool comptime_var_mem = ptr->value.data.x_ptr.comptime_var_mem;
9356 if (comptime_var_mem) {
9357 if (instr_is_comptime(casted_value)) {
9358 ConstExprValue *dest_val = const_ptr_pointee(&ptr->value);
9359 if (dest_val->special != ConstValSpecialRuntime) {
9360 *dest_val = casted_value->value;
9361 return ir_analyze_void(ira, &store_ptr_instruction->base);
9362 }
93059363 }
9306 }
9307 if (is_inline) {
93089364 ir_add_error(ira, &store_ptr_instruction->base,
93099365 buf_sprintf("cannot store runtime value in compile time variable"));
93109366 return ira->codegen->builtin_types.entry_invalid;
......@@ -9906,14 +9962,15 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,
99069962 return ira->codegen->builtin_types.entry_invalid;
99079963 }
99089964 TypeTableEntry *child_type = type_entry->data.maybe.child_type;
9909 TypeTableEntry *result_type = get_pointer_to_type(ira->codegen, child_type, false);
9965 TypeTableEntry *result_type = get_pointer_to_type_volatile(ira->codegen, child_type,
9966 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile);
99109967
99119968 if (instr_is_comptime(value)) {
99129969 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
99139970 if (!val)
99149971 return ira->codegen->builtin_types.entry_invalid;
9915 ConstExprValue *maybe_val = val->data.x_ptr.base_ptr;
9916 assert(val->data.x_ptr.index == SIZE_MAX);
9972 assert(val->data.x_ptr.special == ConstPtrSpecialRef);
9973 ConstExprValue *maybe_val = val->data.x_ptr.data.ref.pointee;
99179974
99189975 if (maybe_val->special != ConstValSpecialRuntime) {
99199976 if (!maybe_val->data.x_maybe) {
......@@ -9921,8 +9978,8 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,
99219978 return ira->codegen->builtin_types.entry_invalid;
99229979 }
99239980 ConstExprValue *out_val = ir_build_const_from(ira, &unwrap_maybe_instruction->base);
9924 out_val->data.x_ptr.base_ptr = maybe_val->data.x_maybe;
9925 out_val->data.x_ptr.index = SIZE_MAX;
9981 out_val->data.x_ptr.special = ConstPtrSpecialRef;
9982 out_val->data.x_ptr.data.ref.pointee = maybe_val->data.x_maybe;
99269983 return result_type;
99279984 }
99289985 }
......@@ -10215,9 +10272,10 @@ static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstr
1021510272 ConstExprValue *pointee_val = const_ptr_pointee(target_val_ptr);
1021610273 if (pointee_val->type->id == TypeTableEntryIdEnum) {
1021710274 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
10218 out_val->data.x_ptr.base_ptr = pointee_val->data.x_enum.payload;
10219 out_val->data.x_ptr.index = SIZE_MAX;
10220 return get_pointer_to_type(ira->codegen, pointee_val->type, target_value_ptr->value.type->data.pointer.is_const);
10275 out_val->data.x_ptr.special = ConstPtrSpecialRef;
10276 out_val->data.x_ptr.data.ref.pointee = pointee_val->data.x_enum.payload;
10277 return get_pointer_to_type(ira->codegen, pointee_val->type,
10278 target_value_ptr->value.type->data.pointer.is_const);
1022110279 } else {
1022210280 zig_panic("TODO comptime switch var");
1022310281 }
......@@ -10501,7 +10559,6 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
1050110559 const_val.special = ConstValSpecialStatic;
1050210560 const_val.type = fixed_size_array_type;
1050310561 const_val.data.x_array.elements = allocate<ConstExprValue>(elem_count);
10504 const_val.data.x_array.size = elem_count;
1050510562
1050610563 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->base.scope);
1050710564
......@@ -10540,8 +10597,9 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
1054010597 for (size_t i = 0; i < elem_count; i += 1) {
1054110598 ConstExprValue *elem_val = &out_val->data.x_array.elements[i];
1054210599 if (elem_val->type->id == TypeTableEntryIdArray) {
10543 elem_val->data.x_array.parent_array = out_val;
10544 elem_val->data.x_array.parent_array_index = i;
10600 elem_val->data.x_array.parent.id = ConstParentIdArray;
10601 elem_val->data.x_array.parent.data.p_array.array_val = out_val;
10602 elem_val->data.x_array.parent.data.p_array.elem_index = i;
1054510603 }
1054610604 }
1054710605 return fixed_size_array_type;
......@@ -11227,22 +11285,34 @@ static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructi
1122711285
1122811286 if (casted_dest_ptr->value.special == ConstValSpecialStatic &&
1122911287 casted_byte->value.special == ConstValSpecialStatic &&
11230 casted_count->value.special == ConstValSpecialStatic)
11288 casted_count->value.special == ConstValSpecialStatic &&
11289 casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr)
1123111290 {
1123211291 ConstExprValue *dest_ptr_val = &casted_dest_ptr->value;
1123311292
1123411293 ConstExprValue *dest_elements;
1123511294 size_t start;
1123611295 size_t bound_end;
11237 if (dest_ptr_val->data.x_ptr.index == SIZE_MAX) {
11238 dest_elements = dest_ptr_val->data.x_ptr.base_ptr;
11239 start = 0;
11240 bound_end = 1;
11241 } else {
11242 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.base_ptr;
11243 dest_elements = array_val->data.x_array.elements;
11244 start = dest_ptr_val->data.x_ptr.index;
11245 bound_end = array_val->data.x_array.size;
11296 switch (dest_ptr_val->data.x_ptr.special) {
11297 case ConstPtrSpecialInvalid:
11298 zig_unreachable();
11299 case ConstPtrSpecialRef:
11300 dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee;
11301 start = 0;
11302 bound_end = 1;
11303 break;
11304 case ConstPtrSpecialBaseArray:
11305 {
11306 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
11307 dest_elements = array_val->data.x_array.elements;
11308 start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
11309 bound_end = array_val->type->data.array.len;
11310 break;
11311 }
11312 case ConstPtrSpecialBaseStruct:
11313 zig_panic("TODO memset on const inner struct");
11314 case ConstPtrSpecialHardCodedAddr:
11315 zig_unreachable();
1124611316 }
1124711317
1124811318 size_t count = casted_count->value.data.x_bignum.data.x_uint;
......@@ -11304,7 +11374,8 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi
1130411374
1130511375 if (casted_dest_ptr->value.special == ConstValSpecialStatic &&
1130611376 casted_src_ptr->value.special == ConstValSpecialStatic &&
11307 casted_count->value.special == ConstValSpecialStatic)
11377 casted_count->value.special == ConstValSpecialStatic &&
11378 casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr)
1130811379 {
1130911380 size_t count = casted_count->value.data.x_bignum.data.x_uint;
1131011381
......@@ -11312,15 +11383,26 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi
1131211383 ConstExprValue *dest_elements;
1131311384 size_t dest_start;
1131411385 size_t dest_end;
11315 if (dest_ptr_val->data.x_ptr.index == SIZE_MAX) {
11316 dest_elements = dest_ptr_val->data.x_ptr.base_ptr;
11317 dest_start = 0;
11318 dest_end = 1;
11319 } else {
11320 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.base_ptr;
11321 dest_elements = array_val->data.x_array.elements;
11322 dest_start = dest_ptr_val->data.x_ptr.index;
11323 dest_end = array_val->data.x_array.size;
11386 switch (dest_ptr_val->data.x_ptr.special) {
11387 case ConstPtrSpecialInvalid:
11388 zig_unreachable();
11389 case ConstPtrSpecialRef:
11390 dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee;
11391 dest_start = 0;
11392 dest_end = 1;
11393 break;
11394 case ConstPtrSpecialBaseArray:
11395 {
11396 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
11397 dest_elements = array_val->data.x_array.elements;
11398 dest_start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
11399 dest_end = array_val->type->data.array.len;
11400 break;
11401 }
11402 case ConstPtrSpecialBaseStruct:
11403 zig_panic("TODO memcpy on const inner struct");
11404 case ConstPtrSpecialHardCodedAddr:
11405 zig_unreachable();
1132411406 }
1132511407
1132611408 if (dest_start + count > dest_end) {
......@@ -11332,15 +11414,27 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi
1133211414 ConstExprValue *src_elements;
1133311415 size_t src_start;
1133411416 size_t src_end;
11335 if (src_ptr_val->data.x_ptr.index == SIZE_MAX) {
11336 src_elements = src_ptr_val->data.x_ptr.base_ptr;
11337 src_start = 0;
11338 src_end = 1;
11339 } else {
11340 ConstExprValue *array_val = src_ptr_val->data.x_ptr.base_ptr;
11341 src_elements = array_val->data.x_array.elements;
11342 src_start = src_ptr_val->data.x_ptr.index;
11343 src_end = array_val->data.x_array.size;
11417
11418 switch (src_ptr_val->data.x_ptr.special) {
11419 case ConstPtrSpecialInvalid:
11420 zig_unreachable();
11421 case ConstPtrSpecialRef:
11422 src_elements = src_ptr_val->data.x_ptr.data.ref.pointee;
11423 src_start = 0;
11424 src_end = 1;
11425 break;
11426 case ConstPtrSpecialBaseArray:
11427 {
11428 ConstExprValue *array_val = src_ptr_val->data.x_ptr.data.base_array.array_val;
11429 src_elements = array_val->data.x_array.elements;
11430 src_start = src_ptr_val->data.x_ptr.data.base_array.elem_index;
11431 src_end = array_val->type->data.array.len;
11432 break;
11433 }
11434 case ConstPtrSpecialBaseStruct:
11435 zig_panic("TODO memcpy on const inner struct");
11436 case ConstPtrSpecialHardCodedAddr:
11437 zig_unreachable();
1134411438 }
1134511439
1134611440 if (src_start + count > src_end) {
......@@ -11415,68 +11509,115 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
1141511509 casted_start->value.special == ConstValSpecialStatic &&
1141611510 (!end || end->value.special == ConstValSpecialStatic))
1141711511 {
11418 ConstExprValue *base_ptr;
11512 ConstExprValue *array_val;
11513 ConstExprValue *parent_ptr;
1141911514 size_t abs_offset;
1142011515 size_t rel_end;
1142111516 if (array_type->id == TypeTableEntryIdArray) {
11422 base_ptr = &ptr->value;
11517 array_val = &ptr->value;
1142311518 abs_offset = 0;
1142411519 rel_end = array_type->data.array.len;
11520 parent_ptr = nullptr;
1142511521 } else if (array_type->id == TypeTableEntryIdPointer) {
11426 base_ptr = ptr->value.data.x_ptr.base_ptr;
11427 abs_offset = ptr->value.data.x_ptr.index;
11428 if (abs_offset == SIZE_MAX) {
11429 rel_end = 1;
11430 } else {
11431 rel_end = base_ptr->data.x_array.size - abs_offset;
11522 parent_ptr = &ptr->value;
11523 switch (parent_ptr->data.x_ptr.special) {
11524 case ConstPtrSpecialInvalid:
11525 zig_unreachable();
11526 case ConstPtrSpecialRef:
11527 array_val = nullptr;
11528 abs_offset = SIZE_MAX;
11529 rel_end = 1;
11530 break;
11531 case ConstPtrSpecialBaseArray:
11532 array_val = parent_ptr->data.x_ptr.data.base_array.array_val;
11533 abs_offset = parent_ptr->data.x_ptr.data.base_array.elem_index;
11534 rel_end = array_val->type->data.array.len - abs_offset;
11535 break;
11536 case ConstPtrSpecialBaseStruct:
11537 zig_panic("TODO slice const inner struct");
11538 case ConstPtrSpecialHardCodedAddr:
11539 array_val = nullptr;
11540 break;
1143211541 }
1143311542 } else if (is_slice(array_type)) {
11434 ConstExprValue *ptr_val = &ptr->value.data.x_struct.fields[slice_ptr_index];
11543 parent_ptr = &ptr->value.data.x_struct.fields[slice_ptr_index];
1143511544 ConstExprValue *len_val = &ptr->value.data.x_struct.fields[slice_len_index];
11436 base_ptr = ptr_val->data.x_ptr.base_ptr;
11437 abs_offset = ptr_val->data.x_ptr.index;
1143811545
11439 if (ptr_val->data.x_ptr.index == SIZE_MAX) {
11440 rel_end = 1;
11441 } else {
11442 rel_end = len_val->data.x_bignum.data.x_uint;
11546 switch (parent_ptr->data.x_ptr.special) {
11547 case ConstPtrSpecialInvalid:
11548 zig_unreachable();
11549 case ConstPtrSpecialRef:
11550 array_val = nullptr;
11551 abs_offset = SIZE_MAX;
11552 rel_end = 1;
11553 break;
11554 case ConstPtrSpecialBaseArray:
11555 array_val = parent_ptr->data.x_ptr.data.base_array.array_val;
11556 abs_offset = parent_ptr->data.x_ptr.data.base_array.elem_index;
11557 rel_end = len_val->data.x_bignum.data.x_uint;
11558 break;
11559 case ConstPtrSpecialBaseStruct:
11560 zig_panic("TODO slice const inner struct");
11561 case ConstPtrSpecialHardCodedAddr:
11562 array_val = nullptr;
11563 break;
1144311564 }
1144411565 } else {
1144511566 zig_unreachable();
1144611567 }
1144711568
11448 uint64_t start_scalar = casted_start->value.data.x_bignum.data.x_uint;
11449 if (start_scalar > rel_end) {
11450 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds slice"));
11451 return ira->codegen->builtin_types.entry_invalid;
11452 }
11569 if (array_val || parent_ptr->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
11570 uint64_t start_scalar = casted_start->value.data.x_bignum.data.x_uint;
11571 if (start_scalar > rel_end) {
11572 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds slice"));
11573 return ira->codegen->builtin_types.entry_invalid;
11574 }
1145311575
11454 uint64_t end_scalar;
11455 if (end) {
11456 end_scalar = end->value.data.x_bignum.data.x_uint;
11457 } else {
11458 end_scalar = rel_end;
11459 }
11460 if (end_scalar > rel_end) {
11461 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds slice"));
11462 return ira->codegen->builtin_types.entry_invalid;
11463 }
11464 if (start_scalar > end_scalar) {
11465 ir_add_error(ira, &instruction->base, buf_sprintf("slice start is greater than end"));
11466 return ira->codegen->builtin_types.entry_invalid;
11467 }
11576 uint64_t end_scalar;
11577 if (end) {
11578 end_scalar = end->value.data.x_bignum.data.x_uint;
11579 } else {
11580 end_scalar = rel_end;
11581 }
11582 if (end_scalar > rel_end) {
11583 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds slice"));
11584 return ira->codegen->builtin_types.entry_invalid;
11585 }
11586 if (start_scalar > end_scalar) {
11587 ir_add_error(ira, &instruction->base, buf_sprintf("slice start is greater than end"));
11588 return ira->codegen->builtin_types.entry_invalid;
11589 }
1146811590
11469 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
11470 out_val->data.x_struct.fields = allocate<ConstExprValue>(2);
11591 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
11592 out_val->data.x_struct.fields = allocate<ConstExprValue>(2);
1147111593
11472 ConstExprValue *ptr_val = &out_val->data.x_struct.fields[slice_ptr_index];
11473 size_t index = (abs_offset != SIZE_MAX) ? (abs_offset + start_scalar) : SIZE_MAX;
11474 init_const_ptr(ira->codegen, ptr_val, base_ptr, index, instruction->is_const);
11594 ConstExprValue *ptr_val = &out_val->data.x_struct.fields[slice_ptr_index];
11595
11596 if (array_val) {
11597 size_t index = abs_offset + start_scalar;
11598 init_const_ptr_array(ira->codegen, ptr_val, array_val, index, instruction->is_const);
11599 } else {
11600 switch (parent_ptr->data.x_ptr.special) {
11601 case ConstPtrSpecialInvalid:
11602 zig_unreachable();
11603 case ConstPtrSpecialRef:
11604 init_const_ptr_ref(ira->codegen, ptr_val,
11605 parent_ptr->data.x_ptr.data.ref.pointee, instruction->is_const);
11606 break;
11607 case ConstPtrSpecialBaseArray:
11608 zig_unreachable();
11609 case ConstPtrSpecialBaseStruct:
11610 zig_panic("TODO");
11611 case ConstPtrSpecialHardCodedAddr:
11612 zig_unreachable();
11613 }
11614 }
1147511615
11476 ConstExprValue *len_val = &out_val->data.x_struct.fields[slice_len_index];
11477 init_const_usize(ira->codegen, len_val, end_scalar - start_scalar);
11616 ConstExprValue *len_val = &out_val->data.x_struct.fields[slice_len_index];
11617 init_const_usize(ira->codegen, len_val, end_scalar - start_scalar);
1147811618
11479 return return_type;
11619 return return_type;
11620 }
1148011621 }
1148111622
1148211623 IrInstruction *new_instruction = ir_build_slice_from(&ira->new_irb, &instruction->base, ptr,
......@@ -11688,8 +11829,7 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira,
1168811829 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
1168911830 if (!ptr_val)
1169011831 return ira->codegen->builtin_types.entry_invalid;
11691 ConstExprValue *err_union_val = ptr_val->data.x_ptr.base_ptr;
11692 assert(ptr_val->data.x_ptr.index == SIZE_MAX);
11832 ConstExprValue *err_union_val = const_ptr_pointee(ptr_val);
1169311833 if (err_union_val->special != ConstValSpecialRuntime) {
1169411834 ErrorTableEntry *err = err_union_val->data.x_err_union.err;
1169511835 assert(err);
......@@ -11728,13 +11868,13 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
1172811868 return ira->codegen->builtin_types.entry_invalid;
1172911869 } else if (canon_type->id == TypeTableEntryIdErrorUnion) {
1173011870 TypeTableEntry *child_type = canon_type->data.error.child_type;
11731 TypeTableEntry *result_type = get_pointer_to_type(ira->codegen, child_type, false);
11871 TypeTableEntry *result_type = get_pointer_to_type_volatile(ira->codegen, child_type,
11872 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile);
1173211873 if (instr_is_comptime(value)) {
1173311874 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
1173411875 if (!ptr_val)
1173511876 return ira->codegen->builtin_types.entry_invalid;
11736 ConstExprValue *err_union_val = ptr_val->data.x_ptr.base_ptr;
11737 assert(ptr_val->data.x_ptr.index == SIZE_MAX);
11877 ConstExprValue *err_union_val = const_ptr_pointee(ptr_val);
1173811878 if (err_union_val->special != ConstValSpecialRuntime) {
1173911879 ErrorTableEntry *err = err_union_val->data.x_err_union.err;
1174011880 if (err != nullptr) {
......@@ -11744,8 +11884,8 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
1174411884 }
1174511885
1174611886 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
11747 out_val->data.x_ptr.base_ptr = err_union_val->data.x_err_union.payload;
11748 out_val->data.x_ptr.index = SIZE_MAX;
11887 out_val->data.x_ptr.special = ConstPtrSpecialRef;
11888 out_val->data.x_ptr.data.ref.pointee = err_union_val->data.x_err_union.payload;
1174911889 return result_type;
1175011890 }
1175111891 }
test/cases/eval.zig+20
......@@ -263,3 +263,23 @@ fn fnWithSetDebugSafety() -> i32{
263263 @setDebugSafety(this, true);
264264 return 1234;
265265}
266
267
268
269const SimpleStruct = struct {
270 field: i32,
271
272 fn method(self: &const SimpleStruct) -> i32 {
273 return self.field + 3;
274 }
275};
276
277var simple_struct = SimpleStruct{ .field = 1234, };
278
279const bound_fn = simple_struct.method;
280
281fn callMethodOnBoundFnReferringToVarInstance() {
282 @setFnTest(this);
283
284 assert(bound_fn() == 1237);
285}