authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-28 16:21:41-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-01-28 16:21:41-05:00
log76fba5baf9d582314b37c47561d76b7190f787a0
treece03f7e35e31167c3a5b821dcfff3a8191405b94
parent926a7adb3ac3e69569750a23382cf3dedd3605dd
parent3ed52e5453a6971c6a3db0846f503478922d4ea5
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4152 from ziglang/ir-clean-up-vars

pay off some result location technical debt with regards to "mem slots"

16 files changed, 868 insertions(+), 486 deletions(-)

lib/std/build.zig+3
...@@ -1978,6 +1978,9 @@ pub const LibExeObjStep = struct {...@@ -1978,6 +1978,9 @@ pub const LibExeObjStep = struct {
19781978
1979 const all_features = self.target.getArch().allFeaturesList();1979 const all_features = self.target.getArch().allFeaturesList();
1980 var populated_cpu_features = cross.cpu_features.cpu.features;1980 var populated_cpu_features = cross.cpu_features.cpu.features;
1981 if (self.target.getArch().subArchFeature()) |sub_arch_index| {
1982 populated_cpu_features.addFeature(sub_arch_index);
1983 }
1981 populated_cpu_features.populateDependencies(all_features);1984 populated_cpu_features.populateDependencies(all_features);
19821985
1983 if (populated_cpu_features.eql(cross.cpu_features.features)) {1986 if (populated_cpu_features.eql(cross.cpu_features.features)) {
lib/std/hash/murmur.zig+3-3
...@@ -15,7 +15,7 @@ pub const Murmur2_32 = struct {...@@ -15,7 +15,7 @@ pub const Murmur2_32 = struct {
15 const m: u32 = 0x5bd1e995;15 const m: u32 = 0x5bd1e995;
16 const len = @truncate(u32, str.len);16 const len = @truncate(u32, str.len);
17 var h1: u32 = seed ^ len;17 var h1: u32 = seed ^ len;
18 for (@ptrCast([*]allowzero align(1) const u32, str.ptr)[0..(len >> 2)]) |v| {18 for (@ptrCast([*]align(1) const u32, str.ptr)[0..(len >> 2)]) |v| {
19 var k1: u32 = v;19 var k1: u32 = v;
20 if (builtin.endian == builtin.Endian.Big)20 if (builtin.endian == builtin.Endian.Big)
21 k1 = @byteSwap(u32, k1);21 k1 = @byteSwap(u32, k1);
...@@ -100,7 +100,7 @@ pub const Murmur2_64 = struct {...@@ -100,7 +100,7 @@ pub const Murmur2_64 = struct {
100 const m: u64 = 0xc6a4a7935bd1e995;100 const m: u64 = 0xc6a4a7935bd1e995;
101 const len = @as(u64, str.len);101 const len = @as(u64, str.len);
102 var h1: u64 = seed ^ (len *% m);102 var h1: u64 = seed ^ (len *% m);
103 for (@ptrCast([*]allowzero align(1) const u64, str.ptr)[0..@intCast(usize, len >> 3)]) |v| {103 for (@ptrCast([*]align(1) const u64, str.ptr)[0..@intCast(usize, len >> 3)]) |v| {
104 var k1: u64 = v;104 var k1: u64 = v;
105 if (builtin.endian == builtin.Endian.Big)105 if (builtin.endian == builtin.Endian.Big)
106 k1 = @byteSwap(u64, k1);106 k1 = @byteSwap(u64, k1);
...@@ -180,7 +180,7 @@ pub const Murmur3_32 = struct {...@@ -180,7 +180,7 @@ pub const Murmur3_32 = struct {
180 const c2: u32 = 0x1b873593;180 const c2: u32 = 0x1b873593;
181 const len = @truncate(u32, str.len);181 const len = @truncate(u32, str.len);
182 var h1: u32 = seed;182 var h1: u32 = seed;
183 for (@ptrCast([*]allowzero align(1) const u32, str.ptr)[0..(len >> 2)]) |v| {183 for (@ptrCast([*]align(1) const u32, str.ptr)[0..(len >> 2)]) |v| {
184 var k1: u32 = v;184 var k1: u32 = v;
185 if (builtin.endian == builtin.Endian.Big)185 if (builtin.endian == builtin.Endian.Big)
186 k1 = @byteSwap(u32, k1);186 k1 = @byteSwap(u32, k1);
src/all_types.hpp+6-1
...@@ -317,6 +317,7 @@ struct ConstErrValue {...@@ -317,6 +317,7 @@ struct ConstErrValue {
317struct ConstBoundFnValue {317struct ConstBoundFnValue {
318 ZigFn *fn;318 ZigFn *fn;
319 IrInstGen *first_arg;319 IrInstGen *first_arg;
320 IrInst *first_arg_src;
320};321};
321322
322struct ConstArgTuple {323struct ConstArgTuple {
...@@ -502,6 +503,9 @@ struct ZigValue {...@@ -502,6 +503,9 @@ struct ZigValue {
502 // uncomment these to find bugs. can't leave them uncommented because of a gcc-9 warning503 // uncomment these to find bugs. can't leave them uncommented because of a gcc-9 warning
503 //ZigValue(const ZigValue &other) = delete; // plz zero initialize with {}504 //ZigValue(const ZigValue &other) = delete; // plz zero initialize with {}
504 //ZigValue& operator= (const ZigValue &other) = delete; // use copy_const_val505 //ZigValue& operator= (const ZigValue &other) = delete; // use copy_const_val
506
507 // for use in debuggers
508 void dump();
505};509};
506510
507enum ReturnKnowledge {511enum ReturnKnowledge {
...@@ -1256,6 +1260,7 @@ static const uint32_t VECTOR_INDEX_RUNTIME = UINT32_MAX - 1;...@@ -1256,6 +1260,7 @@ static const uint32_t VECTOR_INDEX_RUNTIME = UINT32_MAX - 1;
1256struct InferredStructField {1260struct InferredStructField {
1257 ZigType *inferred_struct_type;1261 ZigType *inferred_struct_type;
1258 Buf *field_name;1262 Buf *field_name;
1263 bool already_resolved;
1259};1264};
12601265
1261struct ZigTypePointer {1266struct ZigTypePointer {
...@@ -2262,7 +2267,6 @@ struct ZigVar {...@@ -2262,7 +2267,6 @@ struct ZigVar {
2262 Scope *parent_scope;2267 Scope *parent_scope;
2263 Scope *child_scope;2268 Scope *child_scope;
2264 LLVMValueRef param_value_ref;2269 LLVMValueRef param_value_ref;
2265 size_t mem_slot_index;
2266 IrExecutableSrc *owner_exec;2270 IrExecutableSrc *owner_exec;
22672271
2268 Buf *section_name;2272 Buf *section_name;
...@@ -2283,6 +2287,7 @@ struct ZigVar {...@@ -2283,6 +2287,7 @@ struct ZigVar {
2283 bool is_thread_local;2287 bool is_thread_local;
2284 bool is_comptime_memoized;2288 bool is_comptime_memoized;
2285 bool is_comptime_memoized_value;2289 bool is_comptime_memoized_value;
2290 bool did_the_decl_codegen;
2286};2291};
22872292
2288struct ErrorTableEntry {2293struct ErrorTableEntry {
src/analyze.cpp+236-10
...@@ -593,9 +593,9 @@ ZigType *get_pointer_to_type_extra2(CodeGen *g, ZigType *child_type, bool is_con...@@ -593,9 +593,9 @@ ZigType *get_pointer_to_type_extra2(CodeGen *g, ZigType *child_type, bool is_con
593 }593 }
594594
595 if (inferred_struct_field != nullptr) {595 if (inferred_struct_field != nullptr) {
596 entry->abi_size = g->builtin_types.entry_usize->abi_size;596 entry->abi_size = SIZE_MAX;
597 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;597 entry->size_in_bits = SIZE_MAX;
598 entry->abi_align = g->builtin_types.entry_usize->abi_align;598 entry->abi_align = UINT32_MAX;
599 } else if (type_is_resolved(child_type, ResolveStatusZeroBitsKnown)) {599 } else if (type_is_resolved(child_type, ResolveStatusZeroBitsKnown)) {
600 if (type_has_bits(child_type)) {600 if (type_has_bits(child_type)) {
601 entry->abi_size = g->builtin_types.entry_usize->abi_size;601 entry->abi_size = g->builtin_types.entry_usize->abi_size;
...@@ -1102,11 +1102,28 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind...@@ -1102,11 +1102,28 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
1102ZigValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,1102ZigValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
1103 Buf *type_name, UndefAllowed undef)1103 Buf *type_name, UndefAllowed undef)
1104{1104{
1105 Error err;
1106
1107 ZigValue *result = create_const_vals(1);
1108 ZigValue *result_ptr = create_const_vals(1);
1109 result->special = ConstValSpecialUndef;
1110 result->type = (type_entry == nullptr) ? g->builtin_types.entry_var : type_entry;
1111 result_ptr->special = ConstValSpecialStatic;
1112 result_ptr->type = get_pointer_to_type(g, result->type, false);
1113 result_ptr->data.x_ptr.mut = ConstPtrMutComptimeVar;
1114 result_ptr->data.x_ptr.special = ConstPtrSpecialRef;
1115 result_ptr->data.x_ptr.data.ref.pointee = result;
1116
1105 size_t backward_branch_count = 0;1117 size_t backward_branch_count = 0;
1106 size_t backward_branch_quota = default_backward_branch_quota;1118 size_t backward_branch_quota = default_backward_branch_quota;
1107 return ir_eval_const_value(g, scope, node, type_entry,1119 if ((err = ir_eval_const_value(g, scope, node, result_ptr,
1108 &backward_branch_count, &backward_branch_quota,1120 &backward_branch_count, &backward_branch_quota,
1109 nullptr, nullptr, node, type_name, nullptr, nullptr, undef);1121 nullptr, nullptr, node, type_name, nullptr, nullptr, undef)))
1122 {
1123 return g->invalid_inst_gen->value;
1124 }
1125 destroy(result_ptr, "ZigValue");
1126 return result;
1110}1127}
11111128
1112Error type_val_resolve_zero_bits(CodeGen *g, ZigValue *type_val, ZigType *parent_type,1129Error type_val_resolve_zero_bits(CodeGen *g, ZigValue *type_val, ZigType *parent_type,
...@@ -3829,7 +3846,6 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf...@@ -3829,7 +3846,6 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf
3829 variable_entry->var_type = var_type;3846 variable_entry->var_type = var_type;
3830 variable_entry->parent_scope = parent_scope;3847 variable_entry->parent_scope = parent_scope;
3831 variable_entry->shadowable = false;3848 variable_entry->shadowable = false;
3832 variable_entry->mem_slot_index = SIZE_MAX;
3833 variable_entry->src_arg_index = SIZE_MAX;3849 variable_entry->src_arg_index = SIZE_MAX;
38343850
3835 assert(name);3851 assert(name);
...@@ -3930,7 +3946,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {...@@ -3930,7 +3946,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
39303946
3931 // TODO more validation for types that can't be used for export/extern variables3947 // TODO more validation for types that can't be used for export/extern variables
3932 ZigType *implicit_type = nullptr;3948 ZigType *implicit_type = nullptr;
3933 if (explicit_type && explicit_type->id == ZigTypeIdInvalid) {3949 if (explicit_type != nullptr && explicit_type->id == ZigTypeIdInvalid) {
3934 implicit_type = explicit_type;3950 implicit_type = explicit_type;
3935 } else if (var_decl->expr) {3951 } else if (var_decl->expr) {
3936 init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type,3952 init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type,
...@@ -4718,8 +4734,14 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) {...@@ -4718,8 +4734,14 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) {
4718 assert(!fn_type->data.fn.is_generic);4734 assert(!fn_type->data.fn.is_generic);
4719 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;4735 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
47204736
4737 if (fn->analyzed_executable.begin_scope == nullptr) {
4738 fn->analyzed_executable.begin_scope = &fn->def_scope->base;
4739 }
4740 if (fn->analyzed_executable.source_node == nullptr) {
4741 fn->analyzed_executable.source_node = fn->body_node;
4742 }
4721 ZigType *block_return_type = ir_analyze(g, fn->ir_executable,4743 ZigType *block_return_type = ir_analyze(g, fn->ir_executable,
4722 &fn->analyzed_executable, fn_type_id->return_type, return_type_node);4744 &fn->analyzed_executable, fn_type_id->return_type, return_type_node, nullptr);
4723 fn->src_implicit_return_type = block_return_type;4745 fn->src_implicit_return_type = block_return_type;
47244746
4725 if (type_is_invalid(block_return_type) || fn->analyzed_executable.first_err_trace_msg != nullptr) {4747 if (type_is_invalid(block_return_type) || fn->analyzed_executable.first_err_trace_msg != nullptr) {
...@@ -5619,6 +5641,8 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {...@@ -5619,6 +5641,8 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
5619 return OnePossibleValueYes;5641 return OnePossibleValueYes;
5620 return type_has_one_possible_value(g, type_entry->data.array.child_type);5642 return type_has_one_possible_value(g, type_entry->data.array.child_type);
5621 case ZigTypeIdStruct:5643 case ZigTypeIdStruct:
5644 // If the recursive function call asks, then we are not one possible value.
5645 type_entry->one_possible_value = OnePossibleValueNo;
5622 for (size_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) {5646 for (size_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) {
5623 TypeStructField *field = type_entry->data.structure.fields[i];5647 TypeStructField *field = type_entry->data.structure.fields[i];
5624 OnePossibleValue opv = (field->type_entry != nullptr) ?5648 OnePossibleValue opv = (field->type_entry != nullptr) ?
...@@ -5626,6 +5650,7 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {...@@ -5626,6 +5650,7 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
5626 type_val_resolve_has_one_possible_value(g, field->type_val);5650 type_val_resolve_has_one_possible_value(g, field->type_val);
5627 switch (opv) {5651 switch (opv) {
5628 case OnePossibleValueInvalid:5652 case OnePossibleValueInvalid:
5653 type_entry->one_possible_value = OnePossibleValueInvalid;
5629 return OnePossibleValueInvalid;5654 return OnePossibleValueInvalid;
5630 case OnePossibleValueNo:5655 case OnePossibleValueNo:
5631 return OnePossibleValueNo;5656 return OnePossibleValueNo;
...@@ -5633,6 +5658,7 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {...@@ -5633,6 +5658,7 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
5633 continue;5658 continue;
5634 }5659 }
5635 }5660 }
5661 type_entry->one_possible_value = OnePossibleValueYes;
5636 return OnePossibleValueYes;5662 return OnePossibleValueYes;
5637 case ZigTypeIdErrorSet:5663 case ZigTypeIdErrorSet:
5638 case ZigTypeIdEnum:5664 case ZigTypeIdEnum:
...@@ -5678,6 +5704,9 @@ ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry) {...@@ -5678,6 +5704,9 @@ ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry) {
5678 assert(field_type != nullptr);5704 assert(field_type != nullptr);
5679 result->data.x_struct.fields[i] = get_the_one_possible_value(g, field_type);5705 result->data.x_struct.fields[i] = get_the_one_possible_value(g, field_type);
5680 }5706 }
5707 } else if (result->type->id == ZigTypeIdPointer) {
5708 result->data.x_ptr.special = ConstPtrSpecialRef;
5709 result->data.x_ptr.data.ref.pointee = get_the_one_possible_value(g, result->type->data.pointer.child_type);
5681 }5710 }
5682 g->one_possible_values.put(type_entry, result);5711 g->one_possible_values.put(type_entry, result);
5683 return result;5712 return result;
...@@ -6452,7 +6481,21 @@ static Error resolve_pointer_zero_bits(CodeGen *g, ZigType *ty) {...@@ -6452,7 +6481,21 @@ static Error resolve_pointer_zero_bits(CodeGen *g, ZigType *ty) {
6452 }6481 }
6453 ty->data.pointer.resolve_loop_flag_zero_bits = true;6482 ty->data.pointer.resolve_loop_flag_zero_bits = true;
64546483
6455 ZigType *elem_type = ty->data.pointer.child_type;6484 ZigType *elem_type;
6485 InferredStructField *isf = ty->data.pointer.inferred_struct_field;
6486 if (isf != nullptr) {
6487 TypeStructField *field = find_struct_type_field(isf->inferred_struct_type, isf->field_name);
6488 assert(field != nullptr);
6489 if (field->is_comptime) {
6490 ty->abi_size = 0;
6491 ty->size_in_bits = 0;
6492 ty->abi_align = 0;
6493 return ErrorNone;
6494 }
6495 elem_type = field->type_entry;
6496 } else {
6497 elem_type = ty->data.pointer.child_type;
6498 }
64566499
6457 bool has_bits;6500 bool has_bits;
6458 if ((err = type_has_bits2(g, elem_type, &has_bits)))6501 if ((err = type_has_bits2(g, elem_type, &has_bits)))
...@@ -6875,6 +6918,7 @@ static void render_const_val_array(CodeGen *g, Buf *buf, Buf *type_name, ZigValu...@@ -6875,6 +6918,7 @@ static void render_const_val_array(CodeGen *g, Buf *buf, Buf *type_name, ZigValu
6875 }6918 }
6876 case ConstArraySpecialNone: {6919 case ConstArraySpecialNone: {
6877 ZigValue *base = &array->data.s_none.elements[start];6920 ZigValue *base = &array->data.s_none.elements[start];
6921 assert(base != nullptr);
6878 assert(start + len <= const_val->type->data.array.len);6922 assert(start + len <= const_val->type->data.array.len);
68796923
6880 buf_appendf(buf, "%s{", buf_ptr(type_name));6924 buf_appendf(buf, "%s{", buf_ptr(type_name));
...@@ -6890,6 +6934,10 @@ static void render_const_val_array(CodeGen *g, Buf *buf, Buf *type_name, ZigValu...@@ -6890,6 +6934,10 @@ static void render_const_val_array(CodeGen *g, Buf *buf, Buf *type_name, ZigValu
6890}6934}
68916935
6892void render_const_value(CodeGen *g, Buf *buf, ZigValue *const_val) {6936void render_const_value(CodeGen *g, Buf *buf, ZigValue *const_val) {
6937 if (const_val == nullptr) {
6938 buf_appendf(buf, "(invalid nullptr value)");
6939 return;
6940 }
6893 switch (const_val->special) {6941 switch (const_val->special) {
6894 case ConstValSpecialRuntime:6942 case ConstValSpecialRuntime:
6895 buf_appendf(buf, "(runtime value)");6943 buf_appendf(buf, "(runtime value)");
...@@ -9295,10 +9343,13 @@ bool type_has_optional_repr(ZigType *ty) {...@@ -9295,10 +9343,13 @@ bool type_has_optional_repr(ZigType *ty) {
9295}9343}
92969344
9297void copy_const_val(ZigValue *dest, ZigValue *src) {9345void copy_const_val(ZigValue *dest, ZigValue *src) {
9346 uint32_t prev_align = dest->llvm_align;
9347 ConstParent prev_parent = dest->parent;
9298 memcpy(dest, src, sizeof(ZigValue));9348 memcpy(dest, src, sizeof(ZigValue));
9349 dest->llvm_align = prev_align;
9299 if (src->special != ConstValSpecialStatic)9350 if (src->special != ConstValSpecialStatic)
9300 return;9351 return;
9301 dest->parent.id = ConstParentIdNone;9352 dest->parent = prev_parent;
9302 if (dest->type->id == ZigTypeIdStruct) {9353 if (dest->type->id == ZigTypeIdStruct) {
9303 dest->data.x_struct.fields = alloc_const_vals_ptrs(dest->type->data.structure.src_field_count);9354 dest->data.x_struct.fields = alloc_const_vals_ptrs(dest->type->data.structure.src_field_count);
9304 for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) {9355 for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) {
...@@ -9307,6 +9358,16 @@ void copy_const_val(ZigValue *dest, ZigValue *src) {...@@ -9307,6 +9358,16 @@ void copy_const_val(ZigValue *dest, ZigValue *src) {
9307 dest->data.x_struct.fields[i]->parent.data.p_struct.struct_val = dest;9358 dest->data.x_struct.fields[i]->parent.data.p_struct.struct_val = dest;
9308 dest->data.x_struct.fields[i]->parent.data.p_struct.field_index = i;9359 dest->data.x_struct.fields[i]->parent.data.p_struct.field_index = i;
9309 }9360 }
9361 } else if (dest->type->id == ZigTypeIdArray) {
9362 if (dest->data.x_array.special == ConstArraySpecialNone) {
9363 dest->data.x_array.data.s_none.elements = create_const_vals(dest->type->data.array.len);
9364 for (uint64_t i = 0; i < dest->type->data.array.len; i += 1) {
9365 copy_const_val(&dest->data.x_array.data.s_none.elements[i], &src->data.x_array.data.s_none.elements[i]);
9366 dest->data.x_array.data.s_none.elements[i].parent.id = ConstParentIdArray;
9367 dest->data.x_array.data.s_none.elements[i].parent.data.p_array.array_val = dest;
9368 dest->data.x_array.data.s_none.elements[i].parent.data.p_array.elem_index = i;
9369 }
9370 }
9310 } else if (type_has_optional_repr(dest->type) && dest->data.x_optional != nullptr) {9371 } else if (type_has_optional_repr(dest->type) && dest->data.x_optional != nullptr) {
9311 dest->data.x_optional = create_const_vals(1);9372 dest->data.x_optional = create_const_vals(1);
9312 copy_const_val(dest->data.x_optional, src->data.x_optional);9373 copy_const_val(dest->data.x_optional, src->data.x_optional);
...@@ -9353,6 +9414,171 @@ bool type_is_numeric(ZigType *ty) {...@@ -9353,6 +9414,171 @@ bool type_is_numeric(ZigType *ty) {
9353 zig_unreachable();9414 zig_unreachable();
9354}9415}
93559416
9417static void dump_value_indent(ZigValue *val, int indent) {
9418 for (int i = 0; i < indent; i += 1) {
9419 fprintf(stderr, " ");
9420 }
9421 fprintf(stderr, "Value@%p(", val);
9422 if (val->type != nullptr) {
9423 fprintf(stderr, "%s)", buf_ptr(&val->type->name));
9424 } else {
9425 fprintf(stderr, "type=nullptr)");
9426 }
9427 switch (val->special) {
9428 case ConstValSpecialUndef:
9429 fprintf(stderr, "[undefined]\n");
9430 return;
9431 case ConstValSpecialLazy:
9432 fprintf(stderr, "[lazy]\n");
9433 return;
9434 case ConstValSpecialRuntime:
9435 fprintf(stderr, "[runtime]\n");
9436 return;
9437 case ConstValSpecialStatic:
9438 break;
9439 }
9440 if (val->type == nullptr)
9441 return;
9442 switch (val->type->id) {
9443 case ZigTypeIdInvalid:
9444 fprintf(stderr, "<invalid>\n");
9445 return;
9446 case ZigTypeIdUnreachable:
9447 fprintf(stderr, "<unreachable>\n");
9448 return;
9449 case ZigTypeIdUndefined:
9450 fprintf(stderr, "<undefined>\n");
9451 return;
9452 case ZigTypeIdVoid:
9453 fprintf(stderr, "<{}>\n");
9454 return;
9455 case ZigTypeIdMetaType:
9456 fprintf(stderr, "<%s>\n", buf_ptr(&val->data.x_type->name));
9457 return;
9458 case ZigTypeIdBool:
9459 fprintf(stderr, "<%s>\n", val->data.x_bool ? "true" : "false");
9460 return;
9461 case ZigTypeIdComptimeInt:
9462 case ZigTypeIdInt: {
9463 Buf *tmp_buf = buf_alloc();
9464 bigint_append_buf(tmp_buf, &val->data.x_bigint, 10);
9465 fprintf(stderr, "<%s>\n", buf_ptr(tmp_buf));
9466 buf_destroy(tmp_buf);
9467 return;
9468 }
9469 case ZigTypeIdComptimeFloat:
9470 case ZigTypeIdFloat:
9471 fprintf(stderr, "<TODO dump number>\n");
9472 return;
9473
9474 case ZigTypeIdStruct:
9475 fprintf(stderr, "<struct\n");
9476 for (size_t i = 0; i < val->type->data.structure.src_field_count; i += 1) {
9477 for (int j = 0; j < indent; j += 1) {
9478 fprintf(stderr, " ");
9479 }
9480 fprintf(stderr, "%s: ", buf_ptr(val->type->data.structure.fields[i]->name));
9481 if (val->data.x_struct.fields == nullptr) {
9482 fprintf(stderr, "<null>\n");
9483 } else {
9484 dump_value_indent(val->data.x_struct.fields[i], 1);
9485 }
9486 }
9487 for (int i = 0; i < indent; i += 1) {
9488 fprintf(stderr, " ");
9489 }
9490 fprintf(stderr, ">\n");
9491 return;
9492
9493 case ZigTypeIdOptional:
9494 fprintf(stderr, "<\n");
9495 dump_value_indent(val->data.x_optional, indent + 1);
9496
9497 for (int i = 0; i < indent; i += 1) {
9498 fprintf(stderr, " ");
9499 }
9500 fprintf(stderr, ">\n");
9501 return;
9502
9503 case ZigTypeIdErrorUnion:
9504 if (val->data.x_err_union.payload != nullptr) {
9505 fprintf(stderr, "<\n");
9506 dump_value_indent(val->data.x_err_union.payload, indent + 1);
9507 } else {
9508 fprintf(stderr, "<\n");
9509 dump_value_indent(val->data.x_err_union.error_set, 0);
9510 }
9511 for (int i = 0; i < indent; i += 1) {
9512 fprintf(stderr, " ");
9513 }
9514 fprintf(stderr, ">\n");
9515 return;
9516
9517 case ZigTypeIdPointer:
9518 switch (val->data.x_ptr.special) {
9519 case ConstPtrSpecialInvalid:
9520 fprintf(stderr, "<!invalid ptr!>\n");
9521 return;
9522 case ConstPtrSpecialRef:
9523 fprintf(stderr, "<ref\n");
9524 dump_value_indent(val->data.x_ptr.data.ref.pointee, indent + 1);
9525 break;
9526 case ConstPtrSpecialBaseStruct: {
9527 ZigValue *struct_val = val->data.x_ptr.data.base_struct.struct_val;
9528 size_t field_index = val->data.x_ptr.data.base_struct.field_index;
9529 fprintf(stderr, "<struct %p field %zu\n", struct_val, field_index);
9530 if (struct_val != nullptr) {
9531 ZigValue *field_val = struct_val->data.x_struct.fields[field_index];
9532 if (field_val != nullptr) {
9533 dump_value_indent(field_val, indent + 1);
9534 } else {
9535 for (int i = 0; i < indent; i += 1) {
9536 fprintf(stderr, " ");
9537 }
9538 fprintf(stderr, "(invalid null field)\n");
9539 }
9540 }
9541 break;
9542 }
9543 case ConstPtrSpecialBaseOptionalPayload: {
9544 ZigValue *optional_val = val->data.x_ptr.data.base_optional_payload.optional_val;
9545 fprintf(stderr, "<optional %p payload\n", optional_val);
9546 if (optional_val != nullptr) {
9547 dump_value_indent(optional_val, indent + 1);
9548 }
9549 break;
9550 }
9551 default:
9552 fprintf(stderr, "TODO dump more pointer things\n");
9553 }
9554 for (int i = 0; i < indent; i += 1) {
9555 fprintf(stderr, " ");
9556 }
9557 fprintf(stderr, ">\n");
9558 return;
9559
9560 case ZigTypeIdVector:
9561 case ZigTypeIdArray:
9562 case ZigTypeIdNull:
9563 case ZigTypeIdErrorSet:
9564 case ZigTypeIdEnum:
9565 case ZigTypeIdUnion:
9566 case ZigTypeIdFn:
9567 case ZigTypeIdBoundFn:
9568 case ZigTypeIdOpaque:
9569 case ZigTypeIdFnFrame:
9570 case ZigTypeIdAnyFrame:
9571 case ZigTypeIdEnumLiteral:
9572 fprintf(stderr, "<TODO dump value>\n");
9573 return;
9574 }
9575 zig_unreachable();
9576}
9577
9578void ZigValue::dump() {
9579 dump_value_indent(this, 0);
9580}
9581
9356// float ops that take a single argument9582// float ops that take a single argument
9357//TODO Powi, Pow, minnum, maxnum, maximum, minimum, copysign, lround, llround, lrint, llrint9583//TODO Powi, Pow, minnum, maxnum, maximum, minimum, copysign, lround, llround, lrint, llrint
9358const char *float_op_to_name(BuiltinFnId op) {9584const char *float_op_to_name(BuiltinFnId op) {
src/codegen.cpp+14-2
...@@ -20,6 +20,7 @@...@@ -20,6 +20,7 @@
20#include "zig_llvm.h"20#include "zig_llvm.h"
21#include "userland.h"21#include "userland.h"
22#include "dump_analysis.hpp"22#include "dump_analysis.hpp"
23#include "softfloat.hpp"
2324
24#include <stdio.h>25#include <stdio.h>
25#include <errno.h>26#include <errno.h>
...@@ -3119,8 +3120,14 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutableGen *executab...@@ -3119,8 +3120,14 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutableGen *executab
3119static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutableGen *executable,3120static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutableGen *executable,
3120 IrInstGenCast *cast_instruction)3121 IrInstGenCast *cast_instruction)
3121{3122{
3123 Error err;
3122 ZigType *actual_type = cast_instruction->value->value->type;3124 ZigType *actual_type = cast_instruction->value->value->type;
3123 ZigType *wanted_type = cast_instruction->base.value->type;3125 ZigType *wanted_type = cast_instruction->base.value->type;
3126 bool wanted_type_has_bits;
3127 if ((err = type_has_bits2(g, wanted_type, &wanted_type_has_bits)))
3128 codegen_report_errors_and_exit(g);
3129 if (!wanted_type_has_bits)
3130 return nullptr;
3124 LLVMValueRef expr_val = ir_llvm_value(g, cast_instruction->value);3131 LLVMValueRef expr_val = ir_llvm_value(g, cast_instruction->value);
3125 ir_assert(expr_val, &cast_instruction->base);3132 ir_assert(expr_val, &cast_instruction->base);
31263133
...@@ -3498,6 +3505,7 @@ static void render_decl_var(CodeGen *g, ZigVar *var) {...@@ -3498,6 +3505,7 @@ static void render_decl_var(CodeGen *g, ZigVar *var) {
34983505
3499static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutableGen *executable, IrInstGenDeclVar *instruction) {3506static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutableGen *executable, IrInstGenDeclVar *instruction) {
3500 instruction->var->ptr_instruction = instruction->var_ptr;3507 instruction->var->ptr_instruction = instruction->var_ptr;
3508 instruction->var->did_the_decl_codegen = true;
3501 render_decl_var(g, instruction->var);3509 render_decl_var(g, instruction->var);
3502 return nullptr;3510 return nullptr;
3503}3511}
...@@ -3966,7 +3974,7 @@ static void render_async_var_decls(CodeGen *g, Scope *scope) {...@@ -3966,7 +3974,7 @@ static void render_async_var_decls(CodeGen *g, Scope *scope) {
3966 return;3974 return;
3967 case ScopeIdVarDecl: {3975 case ScopeIdVarDecl: {
3968 ZigVar *var = reinterpret_cast<ScopeVarDecl *>(scope)->var;3976 ZigVar *var = reinterpret_cast<ScopeVarDecl *>(scope)->var;
3969 if (var->ptr_instruction != nullptr) {3977 if (var->did_the_decl_codegen) {
3970 render_decl_var(g, var);3978 render_decl_var(g, var);
3971 }3979 }
3972 // fallthrough3980 // fallthrough
...@@ -4723,6 +4731,10 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutableGen *e...@@ -4723,6 +4731,10 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutableGen *e
4723 LLVMPositionBuilderAtEnd(g->builder, ok_block);4731 LLVMPositionBuilderAtEnd(g->builder, ok_block);
4724 }4732 }
4725 if (!type_has_bits(child_type)) {4733 if (!type_has_bits(child_type)) {
4734 if (instruction->initializing) {
4735 LLVMValueRef non_null_bit = LLVMConstInt(LLVMInt1Type(), 1, false);
4736 gen_store_untyped(g, non_null_bit, base_ptr, 0, false);
4737 }
4726 return nullptr;4738 return nullptr;
4727 } else {4739 } else {
4728 bool is_scalar = !handle_is_ptr(maybe_type);4740 bool is_scalar = !handle_is_ptr(maybe_type);
...@@ -7529,7 +7541,7 @@ static void do_code_gen(CodeGen *g) {...@@ -7529,7 +7541,7 @@ static void do_code_gen(CodeGen *g) {
7529 } else {7541 } else {
7530 if (want_sret) {7542 if (want_sret) {
7531 g->cur_ret_ptr = LLVMGetParam(fn, 0);7543 g->cur_ret_ptr = LLVMGetParam(fn, 0);
7532 } else if (handle_is_ptr(fn_type_id->return_type)) {7544 } else if (type_has_bits(fn_type_id->return_type)) {
7533 g->cur_ret_ptr = build_alloca(g, fn_type_id->return_type, "result", 0);7545 g->cur_ret_ptr = build_alloca(g, fn_type_id->return_type, "result", 0);
7534 // TODO add debug info variable for this7546 // TODO add debug info variable for this
7535 } else {7547 } else {
src/ir.cpp+506-438
...@@ -17,10 +17,6 @@...@@ -17,10 +17,6 @@
1717
18#include <errno.h>18#include <errno.h>
1919
20struct IrExecContext {
21 ZigList<ZigValue *> mem_slot_list;
22};
23
24struct IrBuilderSrc {20struct IrBuilderSrc {
25 CodeGen *codegen;21 CodeGen *codegen;
26 IrExecutableSrc *exec;22 IrExecutableSrc *exec;
...@@ -38,7 +34,6 @@ struct IrAnalyze {...@@ -38,7 +34,6 @@ struct IrAnalyze {
38 CodeGen *codegen;34 CodeGen *codegen;
39 IrBuilderSrc old_irb;35 IrBuilderSrc old_irb;
40 IrBuilderGen new_irb;36 IrBuilderGen new_irb;
41 IrExecContext exec_context;
42 size_t old_bb_index;37 size_t old_bb_index;
43 size_t instruction_index;38 size_t instruction_index;
44 ZigType *explicit_return_type;39 ZigType *explicit_return_type;
...@@ -48,6 +43,7 @@ struct IrAnalyze {...@@ -48,6 +43,7 @@ struct IrAnalyze {
48 IrBasicBlockSrc *const_predecessor_bb;43 IrBasicBlockSrc *const_predecessor_bb;
49 size_t ref_count;44 size_t ref_count;
50 size_t break_debug_id; // for debugging purposes45 size_t break_debug_id; // for debugging purposes
46 IrInstGen *return_ptr;
5147
52 // For the purpose of using in a debugger48 // For the purpose of using in a debugger
53 void dump();49 void dump();
...@@ -222,7 +218,8 @@ static IrInstGen *ir_get_deref(IrAnalyze *ira, IrInst *source_instr, IrInstGen *...@@ -222,7 +218,8 @@ static IrInstGen *ir_get_deref(IrAnalyze *ira, IrInst *source_instr, IrInstGen *
222 ResultLoc *result_loc);218 ResultLoc *result_loc);
223static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutableSrc *exec, AstNode *source_node, Buf *msg);219static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutableSrc *exec, AstNode *source_node, Buf *msg);
224static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,220static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,
225 IrInst* source_instr, IrInstGen *container_ptr, ZigType *container_type, bool initializing);221 IrInst* source_instr, IrInstGen *container_ptr, IrInst *container_ptr_src,
222 ZigType *container_type, bool initializing);
226static void ir_assert(bool ok, IrInst* source_instruction);223static void ir_assert(bool ok, IrInst* source_instruction);
227static void ir_assert_gen(bool ok, IrInstGen *source_instruction);224static void ir_assert_gen(bool ok, IrInstGen *source_instruction);
228static IrInstGen *ir_get_var_ptr(IrAnalyze *ira, IrInst *source_instr, ZigVar *var);225static IrInstGen *ir_get_var_ptr(IrAnalyze *ira, IrInst *source_instr, ZigVar *var);
...@@ -236,7 +233,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ZigValue *val)...@@ -236,7 +233,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ZigValue *val)
236static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,233static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
237 ZigValue *out_val, ZigValue *ptr_val);234 ZigValue *out_val, ZigValue *ptr_val);
238static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrInstGen *ptr,235static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrInstGen *ptr,
239 ZigType *dest_type, IrInst *dest_type_src, bool safety_check_on);236 IrInst *ptr_src, ZigType *dest_type, IrInst *dest_type_src, bool safety_check_on);
240static ZigValue *ir_resolve_const(IrAnalyze *ira, IrInstGen *value, UndefAllowed undef_allowed);237static ZigValue *ir_resolve_const(IrAnalyze *ira, IrInstGen *value, UndefAllowed undef_allowed);
241static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align);238static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align);
242static IrInstGen *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInst* source_instr, IrInstGen *target,239static IrInstGen *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInst* source_instr, IrInstGen *target,
...@@ -244,11 +241,9 @@ static IrInstGen *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInst* source_instr, Ir...@@ -244,11 +241,9 @@ static IrInstGen *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInst* source_instr, Ir
244static IrInstGen *ir_analyze_bit_cast(IrAnalyze *ira, IrInst* source_instr, IrInstGen *value,241static IrInstGen *ir_analyze_bit_cast(IrAnalyze *ira, IrInst* source_instr, IrInstGen *value,
245 ZigType *dest_type);242 ZigType *dest_type);
246static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_instr,243static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_instr,
247 ResultLoc *result_loc, ZigType *value_type, IrInstGen *value, bool force_runtime,244 ResultLoc *result_loc, ZigType *value_type, IrInstGen *value, bool force_runtime, bool allow_discard);
248 bool non_null_comptime, bool allow_discard);
249static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr,245static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr,
250 ResultLoc *result_loc, ZigType *value_type, IrInstGen *value, bool force_runtime,246 ResultLoc *result_loc, ZigType *value_type, IrInstGen *value, bool force_runtime, bool allow_discard);
251 bool non_null_comptime, bool allow_discard);
252static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* source_instr,247static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* source_instr,
253 IrInstGen *base_ptr, bool safety_check_on, bool initializing);248 IrInstGen *base_ptr, bool safety_check_on, bool initializing);
254static IrInstGen *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInst* source_instr,249static IrInstGen *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInst* source_instr,
...@@ -773,7 +768,6 @@ static void ira_deref(IrAnalyze *ira) {...@@ -773,7 +768,6 @@ static void ira_deref(IrAnalyze *ira) {
773 //destroy(ira->old_irb.exec, "IrExecutableSrc");768 //destroy(ira->old_irb.exec, "IrExecutableSrc");
774 ira->src_implicit_return_type_list.deinit();769 ira->src_implicit_return_type_list.deinit();
775 ira->resume_stack.deinit();770 ira->resume_stack.deinit();
776 ira->exec_context.mem_slot_list.deinit();
777 destroy(ira, "IrAnalyze");771 destroy(ira, "IrAnalyze");
778}772}
779773
...@@ -887,7 +881,6 @@ static bool types_have_same_zig_comptime_repr(CodeGen *codegen, ZigType *expecte...@@ -887,7 +881,6 @@ static bool types_have_same_zig_comptime_repr(CodeGen *codegen, ZigType *expecte
887 case ZigTypeIdComptimeFloat:881 case ZigTypeIdComptimeFloat:
888 case ZigTypeIdComptimeInt:882 case ZigTypeIdComptimeInt:
889 case ZigTypeIdEnumLiteral:883 case ZigTypeIdEnumLiteral:
890 case ZigTypeIdPointer:
891 case ZigTypeIdUndefined:884 case ZigTypeIdUndefined:
892 case ZigTypeIdNull:885 case ZigTypeIdNull:
893 case ZigTypeIdBoundFn:886 case ZigTypeIdBoundFn:
...@@ -896,6 +889,8 @@ static bool types_have_same_zig_comptime_repr(CodeGen *codegen, ZigType *expecte...@@ -896,6 +889,8 @@ static bool types_have_same_zig_comptime_repr(CodeGen *codegen, ZigType *expecte
896 case ZigTypeIdAnyFrame:889 case ZigTypeIdAnyFrame:
897 case ZigTypeIdFn:890 case ZigTypeIdFn:
898 return true;891 return true;
892 case ZigTypeIdPointer:
893 return expected->data.pointer.inferred_struct_field == actual->data.pointer.inferred_struct_field;
899 case ZigTypeIdFloat:894 case ZigTypeIdFloat:
900 return expected->data.floating.bit_count == actual->data.floating.bit_count;895 return expected->data.floating.bit_count == actual->data.floating.bit_count;
901 case ZigTypeIdInt:896 case ZigTypeIdInt:
...@@ -958,12 +953,6 @@ static size_t exec_next_debug_id_gen(IrExecutableGen *exec) {...@@ -958,12 +953,6 @@ static size_t exec_next_debug_id_gen(IrExecutableGen *exec) {
958 return result;953 return result;
959}954}
960955
961static size_t exec_next_mem_slot(IrExecutableSrc *exec) {
962 size_t result = exec->mem_slot_count;
963 exec->mem_slot_count += 1;
964 return result;
965}
966
967static ZigFn *exec_fn_entry(IrExecutableSrc *exec) {956static ZigFn *exec_fn_entry(IrExecutableSrc *exec) {
968 return exec->fn_entry;957 return exec->fn_entry;
969}958}
...@@ -1018,16 +1007,46 @@ static void ir_ref_var(ZigVar *var) {...@@ -1018,16 +1007,46 @@ static void ir_ref_var(ZigVar *var) {
1018 var->ref_count += 1;1007 var->ref_count += 1;
1019}1008}
10201009
1010static void create_result_ptr(CodeGen *codegen, ZigType *expected_type,
1011 ZigValue **out_result, ZigValue **out_result_ptr)
1012{
1013 ZigValue *result = create_const_vals(1);
1014 ZigValue *result_ptr = create_const_vals(1);
1015 result->special = ConstValSpecialUndef;
1016 result->type = expected_type;
1017 result_ptr->special = ConstValSpecialStatic;
1018 result_ptr->type = get_pointer_to_type(codegen, result->type, false);
1019 result_ptr->data.x_ptr.mut = ConstPtrMutComptimeVar;
1020 result_ptr->data.x_ptr.special = ConstPtrSpecialRef;
1021 result_ptr->data.x_ptr.data.ref.pointee = result;
1022
1023 *out_result = result;
1024 *out_result_ptr = result_ptr;
1025}
1026
1021ZigType *ir_analyze_type_expr(IrAnalyze *ira, Scope *scope, AstNode *node) {1027ZigType *ir_analyze_type_expr(IrAnalyze *ira, Scope *scope, AstNode *node) {
1022 ZigValue *result = ir_eval_const_value(ira->codegen, scope, node, ira->codegen->builtin_types.entry_type,1028 Error err;
1023 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr, nullptr,
1024 node, nullptr, ira->new_irb.exec, nullptr, UndefBad);
10251029
1030 ZigValue *result;
1031 ZigValue *result_ptr;
1032 create_result_ptr(ira->codegen, ira->codegen->builtin_types.entry_type, &result, &result_ptr);
1033
1034 if ((err = ir_eval_const_value(ira->codegen, scope, node, result_ptr,
1035 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota,
1036 nullptr, nullptr, node, nullptr, ira->new_irb.exec, nullptr, UndefBad)))
1037 {
1038 return ira->codegen->builtin_types.entry_invalid;
1039 }
1026 if (type_is_invalid(result->type))1040 if (type_is_invalid(result->type))
1027 return ira->codegen->builtin_types.entry_invalid;1041 return ira->codegen->builtin_types.entry_invalid;
10281042
1029 assert(result->special != ConstValSpecialRuntime);1043 assert(result->special != ConstValSpecialRuntime);
1030 return result->data.x_type;1044 ZigType *res_type = result->data.x_type;
1045
1046 destroy(result_ptr, "ZigValue");
1047 destroy(result, "ZigValue");
1048
1049 return res_type;
1031}1050}
10321051
1033static IrBasicBlockSrc *ir_create_basic_block(IrBuilderSrc *irb, Scope *scope, const char *name_hint) {1052static IrBasicBlockSrc *ir_create_basic_block(IrBuilderSrc *irb, Scope *scope, const char *name_hint) {
...@@ -2304,9 +2323,8 @@ static IrInstGen *ir_build_var_ptr_gen(IrAnalyze *ira, IrInst *source_instr, Zig...@@ -2304,9 +2323,8 @@ static IrInstGen *ir_build_var_ptr_gen(IrAnalyze *ira, IrInst *source_instr, Zig
2304 return &instruction->base;2323 return &instruction->base;
2305}2324}
23062325
2307static IrInstGen *ir_build_return_ptr(IrAnalyze *ira, IrInst *source_instruction, ZigType *ty) {2326static IrInstGen *ir_build_return_ptr(IrAnalyze *ira, Scope *scope, AstNode *source_node, ZigType *ty) {
2308 IrInstGenReturnPtr *instruction = ir_build_inst_gen<IrInstGenReturnPtr>(&ira->new_irb,2327 IrInstGenReturnPtr *instruction = ir_build_inst_gen<IrInstGenReturnPtr>(&ira->new_irb, scope, source_node);
2309 source_instruction->scope, source_instruction->source_node);
2310 instruction->base.value->type = ty;2328 instruction->base.value->type = ty;
2311 return &instruction->base;2329 return &instruction->base;
2312}2330}
...@@ -4582,6 +4600,7 @@ static IrInstSrc *ir_build_reset_result(IrBuilderSrc *irb, Scope *scope, AstNode...@@ -4582,6 +4600,7 @@ static IrInstSrc *ir_build_reset_result(IrBuilderSrc *irb, Scope *scope, AstNode
4582{4600{
4583 IrInstSrcResetResult *instruction = ir_build_instruction<IrInstSrcResetResult>(irb, scope, source_node);4601 IrInstSrcResetResult *instruction = ir_build_instruction<IrInstSrcResetResult>(irb, scope, source_node);
4584 instruction->result_loc = result_loc;4602 instruction->result_loc = result_loc;
4603 instruction->base.is_gen = true;
45854604
4586 return &instruction->base;4605 return &instruction->base;
4587}4606}
...@@ -5203,12 +5222,6 @@ static ScopeDeferExpr *get_scope_defer_expr(Scope *scope) {...@@ -5203,12 +5222,6 @@ static ScopeDeferExpr *get_scope_defer_expr(Scope *scope) {
5203static IrInstSrc *ir_gen_return(IrBuilderSrc *irb, Scope *scope, AstNode *node, LVal lval, ResultLoc *result_loc) {5222static IrInstSrc *ir_gen_return(IrBuilderSrc *irb, Scope *scope, AstNode *node, LVal lval, ResultLoc *result_loc) {
5204 assert(node->type == NodeTypeReturnExpr);5223 assert(node->type == NodeTypeReturnExpr);
52055224
5206 ZigFn *fn_entry = exec_fn_entry(irb->exec);
5207 if (!fn_entry) {
5208 add_node_error(irb->codegen, node, buf_sprintf("return expression outside function definition"));
5209 return irb->codegen->invalid_inst_src;
5210 }
5211
5212 ScopeDeferExpr *scope_defer_expr = get_scope_defer_expr(scope);5225 ScopeDeferExpr *scope_defer_expr = get_scope_defer_expr(scope);
5213 if (scope_defer_expr) {5226 if (scope_defer_expr) {
5214 if (!scope_defer_expr->reported_err) {5227 if (!scope_defer_expr->reported_err) {
...@@ -5350,7 +5363,6 @@ static ZigVar *create_local_var(CodeGen *codegen, AstNode *node, Scope *parent_s...@@ -5350,7 +5363,6 @@ static ZigVar *create_local_var(CodeGen *codegen, AstNode *node, Scope *parent_s
5350 ZigVar *variable_entry = allocate<ZigVar>(1, "ZigVar");5363 ZigVar *variable_entry = allocate<ZigVar>(1, "ZigVar");
5351 variable_entry->parent_scope = parent_scope;5364 variable_entry->parent_scope = parent_scope;
5352 variable_entry->shadowable = is_shadowable;5365 variable_entry->shadowable = is_shadowable;
5353 variable_entry->mem_slot_index = SIZE_MAX;
5354 variable_entry->is_comptime = is_comptime;5366 variable_entry->is_comptime = is_comptime;
5355 variable_entry->src_arg_index = SIZE_MAX;5367 variable_entry->src_arg_index = SIZE_MAX;
5356 variable_entry->const_value = create_const_vals(1);5368 variable_entry->const_value = create_const_vals(1);
...@@ -5414,7 +5426,6 @@ static ZigVar *ir_create_var(IrBuilderSrc *irb, AstNode *node, Scope *scope, Buf...@@ -5414,7 +5426,6 @@ static ZigVar *ir_create_var(IrBuilderSrc *irb, AstNode *node, Scope *scope, Buf
5414 (is_underscored ? nullptr : name), src_is_const, gen_is_const,5426 (is_underscored ? nullptr : name), src_is_const, gen_is_const,
5415 (is_underscored ? true : is_shadowable), is_comptime, false);5427 (is_underscored ? true : is_shadowable), is_comptime, false);
5416 if (is_comptime != nullptr || gen_is_const) {5428 if (is_comptime != nullptr || gen_is_const) {
5417 var->mem_slot_index = exec_next_mem_slot(irb->exec);
5418 var->owner_exec = irb->exec;5429 var->owner_exec = irb->exec;
5419 }5430 }
5420 assert(var->child_scope);5431 assert(var->child_scope);
...@@ -5551,6 +5562,10 @@ static IrInstSrc *ir_gen_block(IrBuilderSrc *irb, Scope *parent_scope, AstNode *...@@ -5551,6 +5562,10 @@ static IrInstSrc *ir_gen_block(IrBuilderSrc *irb, Scope *parent_scope, AstNode *
5551 // only generate unconditional defers5562 // only generate unconditional defers
55525563
5553 ir_mark_gen(ir_build_add_implicit_return_type(irb, child_scope, block_node, result, nullptr));5564 ir_mark_gen(ir_build_add_implicit_return_type(irb, child_scope, block_node, result, nullptr));
5565 ResultLocReturn *result_loc_ret = allocate<ResultLocReturn>(1, "ResultLocReturn");
5566 result_loc_ret->base.id = ResultLocIdReturn;
5567 ir_build_reset_result(irb, parent_scope, block_node, &result_loc_ret->base);
5568 ir_mark_gen(ir_build_end_expr(irb, parent_scope, block_node, result, &result_loc_ret->base));
5554 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false);5569 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false);
5555 return ir_mark_gen(ir_build_return_src(irb, child_scope, result->base.source_node, result));5570 return ir_mark_gen(ir_build_return_src(irb, child_scope, result->base.source_node, result));
5556}5571}
...@@ -7001,6 +7016,7 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod...@@ -7001,6 +7016,7 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
7001 ResultLocBitCast *result_loc_bit_cast = allocate<ResultLocBitCast>(1);7016 ResultLocBitCast *result_loc_bit_cast = allocate<ResultLocBitCast>(1);
7002 result_loc_bit_cast->base.id = ResultLocIdBitCast;7017 result_loc_bit_cast->base.id = ResultLocIdBitCast;
7003 result_loc_bit_cast->base.source_instruction = dest_type;7018 result_loc_bit_cast->base.source_instruction = dest_type;
7019 result_loc_bit_cast->base.allow_write_through_const = result_loc->allow_write_through_const;
7004 ir_ref_instruction(dest_type, irb->current_basic_block);7020 ir_ref_instruction(dest_type, irb->current_basic_block);
7005 result_loc_bit_cast->parent = result_loc;7021 result_loc_bit_cast->parent = result_loc;
70067022
...@@ -8462,9 +8478,8 @@ static IrInstSrc *ir_gen_for_expr(IrBuilderSrc *irb, Scope *parent_scope, AstNod...@@ -8462,9 +8478,8 @@ static IrInstSrc *ir_gen_for_expr(IrBuilderSrc *irb, Scope *parent_scope, AstNod
8462 ResultLocPeerParent *peer_parent = ir_build_result_peers(irb, cond_br_inst, end_block, result_loc, is_comptime);8478 ResultLocPeerParent *peer_parent = ir_build_result_peers(irb, cond_br_inst, end_block, result_loc, is_comptime);
84638479
8464 ir_set_cursor_at_end_and_append_block(irb, body_block);8480 ir_set_cursor_at_end_and_append_block(irb, body_block);
8465 Scope *elem_ptr_scope = node->data.for_expr.elem_is_ptr ? parent_scope : &spill_scope->base;8481 IrInstSrc *elem_ptr = ir_build_elem_ptr(irb, &spill_scope->base, node, array_val_ptr, index_val,
8466 IrInstSrc *elem_ptr = ir_build_elem_ptr(irb, elem_ptr_scope, node, array_val_ptr, index_val, false,8482 false, PtrLenSingle, nullptr);
8467 PtrLenSingle, nullptr);
8468 // TODO make it an error to write to element variable or i variable.8483 // TODO make it an error to write to element variable or i variable.
8469 Buf *elem_var_name = elem_node->data.symbol_expr.symbol;8484 Buf *elem_var_name = elem_node->data.symbol_expr.symbol;
8470 ZigVar *elem_var = ir_create_var(irb, elem_node, parent_scope, elem_var_name, true, false, false, is_comptime);8485 ZigVar *elem_var = ir_create_var(irb, elem_node, parent_scope, elem_var_name, true, false, false, is_comptime);
...@@ -10255,6 +10270,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutableSrc *ir_e...@@ -10255,6 +10270,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutableSrc *ir_e
10255 if (!instr_is_unreachable(result)) {10270 if (!instr_is_unreachable(result)) {
10256 ir_mark_gen(ir_build_add_implicit_return_type(irb, scope, result->base.source_node, result, nullptr));10271 ir_mark_gen(ir_build_add_implicit_return_type(irb, scope, result->base.source_node, result, nullptr));
10257 // no need for save_err_ret_addr because this cannot return error10272 // no need for save_err_ret_addr because this cannot return error
10273 ResultLocReturn *result_loc_ret = allocate<ResultLocReturn>(1, "ResultLocReturn");
10274 result_loc_ret->base.id = ResultLocIdReturn;
10275 ir_build_reset_result(irb, scope, node, &result_loc_ret->base);
10276 ir_mark_gen(ir_build_end_expr(irb, scope, node, result, &result_loc_ret->base));
10258 ir_mark_gen(ir_build_return_src(irb, scope, result->base.source_node, result));10277 ir_mark_gen(ir_build_return_src(irb, scope, result->base.source_node, result));
10259 }10278 }
1026010279
...@@ -10374,23 +10393,17 @@ ZigValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ZigValue *const_va...@@ -10374,23 +10393,17 @@ ZigValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ZigValue *const_va
10374 return val;10393 return val;
10375}10394}
1037610395
10377static ZigValue *ir_exec_const_result(CodeGen *codegen, IrExecutableGen *exec) {10396static Error ir_exec_scan_for_side_effects(CodeGen *codegen, IrExecutableGen *exec) {
10378 IrBasicBlockGen *bb = exec->basic_block_list.at(0);10397 IrBasicBlockGen *bb = exec->basic_block_list.at(0);
10379 for (size_t i = 0; i < bb->instruction_list.length; i += 1) {10398 for (size_t i = 0; i < bb->instruction_list.length; i += 1) {
10380 IrInstGen *instruction = bb->instruction_list.at(i);10399 IrInstGen *instruction = bb->instruction_list.at(i);
10381 if (instruction->id == IrInstGenIdReturn) {10400 if (instruction->id == IrInstGenIdReturn) {
10382 IrInstGenReturn *ret_inst = (IrInstGenReturn *)instruction;10401 return ErrorNone;
10383 IrInstGen *operand = ret_inst->operand;
10384 if (operand->value->special == ConstValSpecialRuntime) {
10385 exec_add_error_node_gen(codegen, exec, operand->base.source_node,
10386 buf_sprintf("unable to evaluate constant expression"));
10387 return codegen->invalid_inst_gen->value;
10388 }
10389 return operand->value;
10390 } else if (ir_inst_gen_has_side_effects(instruction)) {10402 } else if (ir_inst_gen_has_side_effects(instruction)) {
10391 if (instr_is_comptime(instruction)) {10403 if (instr_is_comptime(instruction)) {
10392 switch (instruction->id) {10404 switch (instruction->id) {
10393 case IrInstGenIdUnwrapErrPayload:10405 case IrInstGenIdUnwrapErrPayload:
10406 case IrInstGenIdOptionalUnwrapPtr:
10394 case IrInstGenIdUnionFieldPtr:10407 case IrInstGenIdUnionFieldPtr:
10395 continue;10408 continue;
10396 default:10409 default:
...@@ -10403,7 +10416,7 @@ static ZigValue *ir_exec_const_result(CodeGen *codegen, IrExecutableGen *exec) {...@@ -10403,7 +10416,7 @@ static ZigValue *ir_exec_const_result(CodeGen *codegen, IrExecutableGen *exec) {
10403 }10416 }
10404 exec_add_error_node_gen(codegen, exec, instruction->base.source_node,10417 exec_add_error_node_gen(codegen, exec, instruction->base.source_node,
10405 buf_sprintf("unable to evaluate constant expression"));10418 buf_sprintf("unable to evaluate constant expression"));
10406 return codegen->invalid_inst_gen->value;10419 return ErrorSemanticAnalyzeFail;
10407 }10420 }
10408 }10421 }
10409 zig_unreachable();10422 zig_unreachable();
...@@ -12785,26 +12798,28 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc...@@ -12785,26 +12798,28 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc
12785 wanted_type = adjust_slice_align(ira->codegen, wanted_type, get_ptr_align(ira->codegen, array_ptr->value->type));12798 wanted_type = adjust_slice_align(ira->codegen, wanted_type, get_ptr_align(ira->codegen, array_ptr->value->type));
1278612799
12787 if (instr_is_comptime(array_ptr)) {12800 if (instr_is_comptime(array_ptr)) {
12788 ZigValue *pointee = const_ptr_pointee(ira, ira->codegen, array_ptr->value, source_instr->source_node);12801 ZigValue *array_ptr_val = ir_resolve_const(ira, array_ptr, UndefBad);
12802 if (array_ptr_val == nullptr)
12803 return ira->codegen->invalid_inst_gen;
12804 ZigValue *pointee = const_ptr_pointee(ira, ira->codegen, array_ptr_val, source_instr->source_node);
12789 if (pointee == nullptr)12805 if (pointee == nullptr)
12790 return ira->codegen->invalid_inst_gen;12806 return ira->codegen->invalid_inst_gen;
12791 if (pointee->special != ConstValSpecialRuntime) {12807 if (pointee->special != ConstValSpecialRuntime) {
12792 assert(array_ptr->value->type->id == ZigTypeIdPointer);12808 assert(array_ptr_val->type->id == ZigTypeIdPointer);
12793 ZigType *array_type = array_ptr->value->type->data.pointer.child_type;12809 ZigType *array_type = array_ptr_val->type->data.pointer.child_type;
12794 assert(is_slice(wanted_type));12810 assert(is_slice(wanted_type));
12795 bool is_const = wanted_type->data.structure.fields[slice_ptr_index]->type_entry->data.pointer.is_const;12811 bool is_const = wanted_type->data.structure.fields[slice_ptr_index]->type_entry->data.pointer.is_const;
1279612812
12797 IrInstGen *result = ir_const(ira, source_instr, wanted_type);12813 IrInstGen *result = ir_const(ira, source_instr, wanted_type);
12798 init_const_slice(ira->codegen, result->value, pointee, 0, array_type->data.array.len, is_const);12814 init_const_slice(ira->codegen, result->value, pointee, 0, array_type->data.array.len, is_const);
12799 result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = array_ptr->value->data.x_ptr.mut;12815 result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = array_ptr_val->data.x_ptr.mut;
12800 result->value->type = wanted_type;12816 result->value->type = wanted_type;
12801 return result;12817 return result;
12802 }12818 }
12803 }12819 }
1280412820
12805 if (result_loc == nullptr) result_loc = no_result_loc();12821 if (result_loc == nullptr) result_loc = no_result_loc();
12806 IrInstGen *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true,12822 IrInstGen *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, true);
12807 false, true);
12808 if (type_is_invalid(result_loc_inst->value->type) ||12823 if (type_is_invalid(result_loc_inst->value->type) ||
12809 result_loc_inst->value->type->id == ZigTypeIdUnreachable)12824 result_loc_inst->value->type->id == ZigTypeIdUnreachable)
12810 {12825 {
...@@ -13026,10 +13041,13 @@ static IrInstGen *ir_const_fn(IrAnalyze *ira, IrInst *source_instr, ZigFn *fn_en...@@ -13026,10 +13041,13 @@ static IrInstGen *ir_const_fn(IrAnalyze *ira, IrInst *source_instr, ZigFn *fn_en
13026 return result;13041 return result;
13027}13042}
1302813043
13029static IrInstGen *ir_const_bound_fn(IrAnalyze *ira, IrInst *src_inst, ZigFn *fn_entry, IrInstGen *first_arg) {13044static IrInstGen *ir_const_bound_fn(IrAnalyze *ira, IrInst *src_inst, ZigFn *fn_entry, IrInstGen *first_arg,
13045 IrInst *first_arg_src)
13046{
13030 IrInstGen *result = ir_const(ira, src_inst, get_bound_fn_type(ira->codegen, fn_entry));13047 IrInstGen *result = ir_const(ira, src_inst, get_bound_fn_type(ira->codegen, fn_entry));
13031 result->value->data.x_bound_fn.fn = fn_entry;13048 result->value->data.x_bound_fn.fn = fn_entry;
13032 result->value->data.x_bound_fn.first_arg = first_arg;13049 result->value->data.x_bound_fn.first_arg = first_arg;
13050 result->value->data.x_bound_fn.first_arg_src = first_arg_src;
13033 return result;13051 return result;
13034}13052}
1303513053
...@@ -13127,15 +13145,17 @@ static ZigValue *ir_resolve_const(IrAnalyze *ira, IrInstGen *value, UndefAllowed...@@ -13127,15 +13145,17 @@ static ZigValue *ir_resolve_const(IrAnalyze *ira, IrInstGen *value, UndefAllowed
13127 return value->value;13145 return value->value;
13128}13146}
1312913147
13130ZigValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,13148Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
13131 ZigType *expected_type, size_t *backward_branch_count, size_t *backward_branch_quota,13149 ZigValue *return_ptr, size_t *backward_branch_count, size_t *backward_branch_quota,
13132 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,13150 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
13133 IrExecutableGen *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef_allowed)13151 IrExecutableGen *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef_allowed)
13134{13152{
13135 Error err;13153 Error err;
1313613154
13137 if (expected_type != nullptr && type_is_invalid(expected_type))13155 src_assert(return_ptr->type->id == ZigTypeIdPointer, source_node);
13138 return codegen->invalid_inst_gen->value;13156
13157 if (type_is_invalid(return_ptr->type))
13158 return ErrorSemanticAnalyzeFail;
1313913159
13140 IrExecutableSrc *ir_executable = allocate<IrExecutableSrc>(1, "IrExecutableSrc");13160 IrExecutableSrc *ir_executable = allocate<IrExecutableSrc>(1, "IrExecutableSrc");
13141 ir_executable->source_node = source_node;13161 ir_executable->source_node = source_node;
...@@ -13147,11 +13167,11 @@ ZigValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,...@@ -13147,11 +13167,11 @@ ZigValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
13147 ir_executable->begin_scope = scope;13167 ir_executable->begin_scope = scope;
1314813168
13149 if (!ir_gen(codegen, node, scope, ir_executable))13169 if (!ir_gen(codegen, node, scope, ir_executable))
13150 return codegen->invalid_inst_gen->value;13170 return ErrorSemanticAnalyzeFail;
1315113171
13152 if (ir_executable->first_err_trace_msg != nullptr) {13172 if (ir_executable->first_err_trace_msg != nullptr) {
13153 codegen->trace_err = ir_executable->first_err_trace_msg;13173 codegen->trace_err = ir_executable->first_err_trace_msg;
13154 return codegen->invalid_inst_gen->value;13174 return ErrorSemanticAnalyzeFail;
13155 }13175 }
1315613176
13157 if (codegen->verbose_ir) {13177 if (codegen->verbose_ir) {
...@@ -13172,9 +13192,10 @@ ZigValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,...@@ -13172,9 +13192,10 @@ ZigValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
13172 analyzed_executable->backward_branch_count = backward_branch_count;13192 analyzed_executable->backward_branch_count = backward_branch_count;
13173 analyzed_executable->backward_branch_quota = backward_branch_quota;13193 analyzed_executable->backward_branch_quota = backward_branch_quota;
13174 analyzed_executable->begin_scope = scope;13194 analyzed_executable->begin_scope = scope;
13175 ZigType *result_type = ir_analyze(codegen, ir_executable, analyzed_executable, expected_type, expected_type_source_node);13195 ZigType *result_type = ir_analyze(codegen, ir_executable, analyzed_executable,
13196 return_ptr->type->data.pointer.child_type, expected_type_source_node, return_ptr);
13176 if (type_is_invalid(result_type)) {13197 if (type_is_invalid(result_type)) {
13177 return codegen->invalid_inst_gen->value;13198 return ErrorSemanticAnalyzeFail;
13178 }13199 }
1317913200
13180 if (codegen->verbose_ir) {13201 if (codegen->verbose_ir) {
...@@ -13183,14 +13204,16 @@ ZigValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,...@@ -13183,14 +13204,16 @@ ZigValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
13183 fprintf(stderr, "}\n");13204 fprintf(stderr, "}\n");
13184 }13205 }
1318513206
13186 ZigValue *result = ir_exec_const_result(codegen, analyzed_executable);13207 if ((err = ir_exec_scan_for_side_effects(codegen, analyzed_executable)))
13187 if (type_is_invalid(result->type))13208 return err;
13188 return codegen->invalid_inst_gen->value;
1318913209
13210 ZigValue *result = const_ptr_pointee(nullptr, codegen, return_ptr, source_node);
13211 if (result == nullptr)
13212 return ErrorSemanticAnalyzeFail;
13190 if ((err = ir_resolve_const_val(codegen, analyzed_executable, node, result, undef_allowed)))13213 if ((err = ir_resolve_const_val(codegen, analyzed_executable, node, result, undef_allowed)))
13191 return codegen->invalid_inst_gen->value;13214 return err;
1319213215
13193 return result;13216 return ErrorNone;
13194}13217}
1319513218
13196static ErrorTableEntry *ir_resolve_error(IrAnalyze *ira, IrInstGen *err_value) {13219static ErrorTableEntry *ir_resolve_error(IrAnalyze *ira, IrInstGen *err_value) {
...@@ -13372,7 +13395,7 @@ static IrInstGen *ir_analyze_optional_wrap(IrAnalyze *ira, IrInst* source_instr,...@@ -13372,7 +13395,7 @@ static IrInstGen *ir_analyze_optional_wrap(IrAnalyze *ira, IrInst* source_instr,
13372 }13395 }
13373 IrInstGen *result_loc_inst = nullptr;13396 IrInstGen *result_loc_inst = nullptr;
13374 if (result_loc != nullptr) {13397 if (result_loc != nullptr) {
13375 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false, true);13398 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, true);
13376 if (type_is_invalid(result_loc_inst->value->type) ||13399 if (type_is_invalid(result_loc_inst->value->type) ||
13377 result_loc_inst->value->type->id == ZigTypeIdUnreachable)13400 result_loc_inst->value->type->id == ZigTypeIdUnreachable)
13378 {13401 {
...@@ -13396,8 +13419,8 @@ static IrInstGen *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInst* source_ins...@@ -13396,8 +13419,8 @@ static IrInstGen *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInst* source_ins
13396 if (type_is_invalid(casted_payload->value->type))13419 if (type_is_invalid(casted_payload->value->type))
13397 return ira->codegen->invalid_inst_gen;13420 return ira->codegen->invalid_inst_gen;
1339813421
13399 ZigValue *val = ir_resolve_const(ira, casted_payload, UndefBad);13422 ZigValue *val = ir_resolve_const(ira, casted_payload, UndefOk);
13400 if (!val)13423 if (val == nullptr)
13401 return ira->codegen->invalid_inst_gen;13424 return ira->codegen->invalid_inst_gen;
1340213425
13403 ZigValue *err_set_val = create_const_vals(1);13426 ZigValue *err_set_val = create_const_vals(1);
...@@ -13417,7 +13440,7 @@ static IrInstGen *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInst* source_ins...@@ -13417,7 +13440,7 @@ static IrInstGen *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInst* source_ins
13417 IrInstGen *result_loc_inst;13440 IrInstGen *result_loc_inst;
13418 if (handle_is_ptr(wanted_type)) {13441 if (handle_is_ptr(wanted_type)) {
13419 if (result_loc == nullptr) result_loc = no_result_loc();13442 if (result_loc == nullptr) result_loc = no_result_loc();
13420 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false, true);13443 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, true);
13421 if (type_is_invalid(result_loc_inst->value->type) ||13444 if (type_is_invalid(result_loc_inst->value->type) ||
13422 result_loc_inst->value->type->id == ZigTypeIdUnreachable) {13445 result_loc_inst->value->type->id == ZigTypeIdUnreachable) {
13423 return result_loc_inst;13446 return result_loc_inst;
...@@ -13529,7 +13552,7 @@ static IrInstGen *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInst* source_instr,...@@ -13529,7 +13552,7 @@ static IrInstGen *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInst* source_instr,
13529 IrInstGen *result_loc_inst;13552 IrInstGen *result_loc_inst;
13530 if (handle_is_ptr(wanted_type)) {13553 if (handle_is_ptr(wanted_type)) {
13531 if (result_loc == nullptr) result_loc = no_result_loc();13554 if (result_loc == nullptr) result_loc = no_result_loc();
13532 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false, true);13555 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, true);
13533 if (type_is_invalid(result_loc_inst->value->type) ||13556 if (type_is_invalid(result_loc_inst->value->type) ||
13534 result_loc_inst->value->type->id == ZigTypeIdUnreachable)13557 result_loc_inst->value->type->id == ZigTypeIdUnreachable)
13535 {13558 {
...@@ -13580,32 +13603,31 @@ static IrInstGen *ir_analyze_null_to_c_pointer(IrAnalyze *ira, IrInst *source_in...@@ -13580,32 +13603,31 @@ static IrInstGen *ir_analyze_null_to_c_pointer(IrAnalyze *ira, IrInst *source_in
13580 return result;13603 return result;
13581}13604}
1358213605
13583static IrInstGen *ir_get_ref(IrAnalyze *ira, IrInst* source_instruction, IrInstGen *value,13606static IrInstGen *ir_get_ref2(IrAnalyze *ira, IrInst* source_instruction, IrInstGen *value,
13584 bool is_const, bool is_volatile)13607 ZigType *elem_type, bool is_const, bool is_volatile)
13585{13608{
13586 Error err;13609 Error err;
1358713610
13588 if (type_is_invalid(value->value->type))13611 if (type_is_invalid(elem_type))
13589 return ira->codegen->invalid_inst_gen;13612 return ira->codegen->invalid_inst_gen;
1359013613
13591 if (instr_is_comptime(value)) {13614 if (instr_is_comptime(value)) {
13592 ZigValue *val = ir_resolve_const(ira, value, LazyOk);13615 ZigValue *val = ir_resolve_const(ira, value, LazyOk);
13593 if (!val)13616 if (!val)
13594 return ira->codegen->invalid_inst_gen;13617 return ira->codegen->invalid_inst_gen;
13595 return ir_get_const_ptr(ira, source_instruction, val, value->value->type,13618 return ir_get_const_ptr(ira, source_instruction, val, elem_type,
13596 ConstPtrMutComptimeConst, is_const, is_volatile, 0);13619 ConstPtrMutComptimeConst, is_const, is_volatile, 0);
13597 }13620 }
1359813621
13599 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, value->value->type,13622 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, elem_type,
13600 is_const, is_volatile, PtrLenSingle, 0, 0, 0, false);13623 is_const, is_volatile, PtrLenSingle, 0, 0, 0, false);
1360113624
13602 if ((err = type_resolve(ira->codegen, ptr_type, ResolveStatusZeroBitsKnown)))13625 if ((err = type_resolve(ira->codegen, ptr_type, ResolveStatusZeroBitsKnown)))
13603 return ira->codegen->invalid_inst_gen;13626 return ira->codegen->invalid_inst_gen;
1360413627
13605 IrInstGen *result_loc;13628 IrInstGen *result_loc;
13606 if (type_has_bits(ptr_type) && !handle_is_ptr(value->value->type)) {13629 if (type_has_bits(ptr_type) && !handle_is_ptr(elem_type)) {
13607 result_loc = ir_resolve_result(ira, source_instruction, no_result_loc(), value->value->type, nullptr, true,13630 result_loc = ir_resolve_result(ira, source_instruction, no_result_loc(), elem_type, nullptr, true, true);
13608 false, true);
13609 } else {13631 } else {
13610 result_loc = nullptr;13632 result_loc = nullptr;
13611 }13633 }
...@@ -13615,6 +13637,12 @@ static IrInstGen *ir_get_ref(IrAnalyze *ira, IrInst* source_instruction, IrInstG...@@ -13615,6 +13637,12 @@ static IrInstGen *ir_get_ref(IrAnalyze *ira, IrInst* source_instruction, IrInstG
13615 return new_instruction;13637 return new_instruction;
13616}13638}
1361713639
13640static IrInstGen *ir_get_ref(IrAnalyze *ira, IrInst* source_instruction, IrInstGen *value,
13641 bool is_const, bool is_volatile)
13642{
13643 return ir_get_ref2(ira, source_instruction, value, value->value->type, is_const, is_volatile);
13644}
13645
13618static ZigType *ir_resolve_union_tag_type(IrAnalyze *ira, AstNode *source_node, ZigType *union_type) {13646static ZigType *ir_resolve_union_tag_type(IrAnalyze *ira, AstNode *source_node, ZigType *union_type) {
13619 assert(union_type->id == ZigTypeIdUnion);13647 assert(union_type->id == ZigTypeIdUnion);
1362013648
...@@ -14311,8 +14339,7 @@ static IrInstGen *ir_analyze_vector_to_array(IrAnalyze *ira, IrInst* source_inst...@@ -14311,8 +14339,7 @@ static IrInstGen *ir_analyze_vector_to_array(IrAnalyze *ira, IrInst* source_inst
14311 if (result_loc == nullptr) {14339 if (result_loc == nullptr) {
14312 result_loc = no_result_loc();14340 result_loc = no_result_loc();
14313 }14341 }
14314 IrInstGen *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, array_type, nullptr,14342 IrInstGen *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, array_type, nullptr, true, true);
14315 true, false, true);
14316 if (type_is_invalid(result_loc_inst->value->type) || result_loc_inst->value->type->id == ZigTypeIdUnreachable) {14343 if (type_is_invalid(result_loc_inst->value->type) || result_loc_inst->value->type->id == ZigTypeIdUnreachable) {
14317 return result_loc_inst;14344 return result_loc_inst;
14318 }14345 }
...@@ -14976,7 +15003,7 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,...@@ -14976,7 +15003,7 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,
14976 dest_ptr_type = wanted_type->data.maybe.child_type;15003 dest_ptr_type = wanted_type->data.maybe.child_type;
14977 }15004 }
14978 if (dest_ptr_type != nullptr) {15005 if (dest_ptr_type != nullptr) {
14979 return ir_analyze_ptr_cast(ira, source_instr, value, wanted_type, source_instr, true);15006 return ir_analyze_ptr_cast(ira, source_instr, value, source_instr, wanted_type, source_instr, true);
14980 }15007 }
14981 }15008 }
1498215009
...@@ -15018,7 +15045,7 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,...@@ -15018,7 +15045,7 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,
15018 actual_type->data.pointer.child_type, source_node,15045 actual_type->data.pointer.child_type, source_node,
15019 !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)15046 !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)
15020 {15047 {
15021 return ir_analyze_ptr_cast(ira, source_instr, value, wanted_type, source_instr, true);15048 return ir_analyze_ptr_cast(ira, source_instr, value, source_instr, wanted_type, source_instr, true);
15022 }15049 }
1502315050
15024 // cast from integer to C pointer15051 // cast from integer to C pointer
...@@ -15195,8 +15222,7 @@ static IrInstGen *ir_get_deref(IrAnalyze *ira, IrInst* source_instruction, IrIns...@@ -15195,8 +15222,7 @@ static IrInstGen *ir_get_deref(IrAnalyze *ira, IrInst* source_instruction, IrIns
15195 IrInstGen *result_loc_inst;15222 IrInstGen *result_loc_inst;
15196 if (ptr_type->data.pointer.host_int_bytes != 0 && handle_is_ptr(child_type)) {15223 if (ptr_type->data.pointer.host_int_bytes != 0 && handle_is_ptr(child_type)) {
15197 if (result_loc == nullptr) result_loc = no_result_loc();15224 if (result_loc == nullptr) result_loc = no_result_loc();
15198 result_loc_inst = ir_resolve_result(ira, source_instruction, result_loc, child_type, nullptr,15225 result_loc_inst = ir_resolve_result(ira, source_instruction, result_loc, child_type, nullptr, true, true);
15199 true, false, true);
15200 if (type_is_invalid(result_loc_inst->value->type) || result_loc_inst->value->type->id == ZigTypeIdUnreachable) {15226 if (type_is_invalid(result_loc_inst->value->type) || result_loc_inst->value->type->id == ZigTypeIdUnreachable) {
15201 return result_loc_inst;15227 return result_loc_inst;
15202 }15228 }
...@@ -15459,7 +15485,7 @@ static IrInstGen *ir_analyze_instruction_return(IrAnalyze *ira, IrInstSrcReturn...@@ -15459,7 +15485,7 @@ static IrInstGen *ir_analyze_instruction_return(IrAnalyze *ira, IrInstSrcReturn
15459 casted_operand->value->type->id == ZigTypeIdPointer &&15485 casted_operand->value->type->id == ZigTypeIdPointer &&
15460 casted_operand->value->data.rh_ptr == RuntimeHintPtrStack)15486 casted_operand->value->data.rh_ptr == RuntimeHintPtrStack)
15461 {15487 {
15462 ir_add_error(ira, &casted_operand->base, buf_sprintf("function returns address of local variable"));15488 ir_add_error(ira, &instruction->operand->base, buf_sprintf("function returns address of local variable"));
15463 return ir_unreach_error(ira);15489 return ir_unreach_error(ira);
15464 }15490 }
1546515491
...@@ -15622,9 +15648,27 @@ static IrInstGen *ir_evaluate_bin_op_cmp(IrAnalyze *ira, ZigType *resolved_type,...@@ -15622,9 +15648,27 @@ static IrInstGen *ir_evaluate_bin_op_cmp(IrAnalyze *ira, ZigType *resolved_type,
15622}15648}
1562315649
15624// Returns ErrorNotLazy when the value cannot be determined15650// Returns ErrorNotLazy when the value cannot be determined
15625static Error lazy_cmp_zero(AstNode *source_node, ZigValue *val, Cmp *result) {15651static Error lazy_cmp_zero(CodeGen *codegen, AstNode *source_node, ZigValue *val, Cmp *result) {
15626 Error err;15652 Error err;
1562715653
15654 switch (type_has_one_possible_value(codegen, val->type)) {
15655 case OnePossibleValueInvalid:
15656 return ErrorSemanticAnalyzeFail;
15657 case OnePossibleValueNo:
15658 break;
15659 case OnePossibleValueYes:
15660 switch (val->type->id) {
15661 case ZigTypeIdInt:
15662 src_assert(val->type->data.integral.bit_count == 0, source_node);
15663 *result = CmpEQ;
15664 return ErrorNone;
15665 case ZigTypeIdUndefined:
15666 return ErrorNotLazy;
15667 default:
15668 zig_unreachable();
15669 }
15670 }
15671
15628 switch (val->special) {15672 switch (val->special) {
15629 case ConstValSpecialRuntime:15673 case ConstValSpecialRuntime:
15630 case ConstValSpecialUndef:15674 case ConstValSpecialUndef:
...@@ -15678,12 +15722,12 @@ static ErrorMsg *ir_eval_bin_op_cmp_scalar(IrAnalyze *ira, IrInst* source_instr,...@@ -15678,12 +15722,12 @@ static ErrorMsg *ir_eval_bin_op_cmp_scalar(IrAnalyze *ira, IrInst* source_instr,
15678 // Before resolving the values, we special case comparisons against zero. These can often15722 // Before resolving the values, we special case comparisons against zero. These can often
15679 // be done without resolving lazy values, preventing potential dependency loops.15723 // be done without resolving lazy values, preventing potential dependency loops.
15680 Cmp op1_cmp_zero;15724 Cmp op1_cmp_zero;
15681 if ((err = lazy_cmp_zero(source_instr->source_node, op1_val, &op1_cmp_zero))) {15725 if ((err = lazy_cmp_zero(ira->codegen, source_instr->source_node, op1_val, &op1_cmp_zero))) {
15682 if (err == ErrorNotLazy) goto never_mind_just_calculate_it_normally;15726 if (err == ErrorNotLazy) goto never_mind_just_calculate_it_normally;
15683 return ira->codegen->trace_err;15727 return ira->codegen->trace_err;
15684 }15728 }
15685 Cmp op2_cmp_zero;15729 Cmp op2_cmp_zero;
15686 if ((err = lazy_cmp_zero(source_instr->source_node, op2_val, &op2_cmp_zero))) {15730 if ((err = lazy_cmp_zero(ira->codegen, source_instr->source_node, op2_val, &op2_cmp_zero))) {
15687 if (err == ErrorNotLazy) goto never_mind_just_calculate_it_normally;15731 if (err == ErrorNotLazy) goto never_mind_just_calculate_it_normally;
15688 return ira->codegen->trace_err;15732 return ira->codegen->trace_err;
15689 }15733 }
...@@ -15847,14 +15891,14 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i...@@ -15847,14 +15891,14 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i
15847 }15891 }
15848 Cmp op1_cmp_zero;15892 Cmp op1_cmp_zero;
15849 bool have_op1_cmp_zero = false;15893 bool have_op1_cmp_zero = false;
15850 if ((err = lazy_cmp_zero(source_instr->source_node, op1->value, &op1_cmp_zero))) {15894 if ((err = lazy_cmp_zero(ira->codegen, source_instr->source_node, op1->value, &op1_cmp_zero))) {
15851 if (err != ErrorNotLazy) return ira->codegen->invalid_inst_gen;15895 if (err != ErrorNotLazy) return ira->codegen->invalid_inst_gen;
15852 } else {15896 } else {
15853 have_op1_cmp_zero = true;15897 have_op1_cmp_zero = true;
15854 }15898 }
15855 Cmp op2_cmp_zero;15899 Cmp op2_cmp_zero;
15856 bool have_op2_cmp_zero = false;15900 bool have_op2_cmp_zero = false;
15857 if ((err = lazy_cmp_zero(source_instr->source_node, op2->value, &op2_cmp_zero))) {15901 if ((err = lazy_cmp_zero(ira->codegen, source_instr->source_node, op2->value, &op2_cmp_zero))) {
15858 if (err != ErrorNotLazy) return ira->codegen->invalid_inst_gen;15902 if (err != ErrorNotLazy) return ira->codegen->invalid_inst_gen;
15859 } else {15903 } else {
15860 have_op2_cmp_zero = true;15904 have_op2_cmp_zero = true;
...@@ -17083,7 +17127,7 @@ static IrInstGen *ir_analyze_tuple_cat(IrAnalyze *ira, IrInst* source_instr,...@@ -17083,7 +17127,7 @@ static IrInstGen *ir_analyze_tuple_cat(IrAnalyze *ira, IrInst* source_instr,
17083 bool is_comptime = ir_should_inline(ira->old_irb.exec, source_instr->scope);17127 bool is_comptime = ir_should_inline(ira->old_irb.exec, source_instr->scope);
1708417128
17085 IrInstGen *new_struct_ptr = ir_resolve_result(ira, source_instr, no_result_loc(),17129 IrInstGen *new_struct_ptr = ir_resolve_result(ira, source_instr, no_result_loc(),
17086 new_type, nullptr, false, false, true);17130 new_type, nullptr, false, true);
17087 uint32_t new_field_count = op1_field_count + op2_field_count;17131 uint32_t new_field_count = op1_field_count + op2_field_count;
1708817132
17089 new_type->data.structure.src_field_count = new_field_count;17133 new_type->data.structure.src_field_count = new_field_count;
...@@ -17669,29 +17713,14 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV...@@ -17669,29 +17713,14 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV
17669 break;17713 break;
17670 }17714 }
1767117715
17672 if (var->var_type != nullptr && !is_comptime_var) {17716 while (var->next_var != nullptr) {
17673 // This is at least the second time we've seen this variable declaration during analysis.17717 var = var->next_var;
17674 // This means that this is actually a different variable due to, e.g. an inline while loop.
17675 // We make a new variable so that it can hold a different type, and so the debug info can
17676 // be distinct.
17677 ZigVar *new_var = create_local_var(ira->codegen, var->decl_node, var->child_scope,
17678 buf_create_from_str(var->name), var->src_is_const, var->gen_is_const,
17679 var->shadowable, var->is_comptime, true);
17680 new_var->owner_exec = var->owner_exec;
17681 new_var->align_bytes = var->align_bytes;
17682 if (var->mem_slot_index != SIZE_MAX) {
17683 ZigValue *vals = create_const_vals(1);
17684 new_var->mem_slot_index = ira->exec_context.mem_slot_list.length;
17685 ira->exec_context.mem_slot_list.append(vals);
17686 }
17687
17688 var->next_var = new_var;
17689 var = new_var;
17690 }17718 }
1769117719
17692 // This must be done after possibly creating a new variable above17720 // This must be done after possibly creating a new variable above
17693 var->ref_count = 0;17721 var->ref_count = 0;
1769417722
17723 var->ptr_instruction = var_ptr;
17695 var->var_type = result_type;17724 var->var_type = result_type;
17696 assert(var->var_type);17725 assert(var->var_type);
1769717726
...@@ -17704,7 +17733,7 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV...@@ -17704,7 +17733,7 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV
17704 var->var_type = ira->codegen->builtin_types.entry_invalid;17733 var->var_type = ira->codegen->builtin_types.entry_invalid;
17705 return ir_const_void(ira, &decl_var_instruction->base.base);17734 return ir_const_void(ira, &decl_var_instruction->base.base);
17706 }17735 }
17707 var->align_bytes = get_abi_alignment(ira->codegen, result_type);17736 var->align_bytes = get_ptr_align(ira->codegen, var_ptr->value->type);
17708 } else {17737 } else {
17709 if (!ir_resolve_align(ira, decl_var_instruction->align_value->child, nullptr, &var->align_bytes)) {17738 if (!ir_resolve_align(ira, decl_var_instruction->align_value->child, nullptr, &var->align_bytes)) {
17710 var->var_type = ira->codegen->builtin_types.entry_invalid;17739 var->var_type = ira->codegen->builtin_types.entry_invalid;
...@@ -17733,16 +17762,8 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV...@@ -17733,16 +17762,8 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV
17733 var_ptr->value->special = ConstValSpecialRuntime;17762 var_ptr->value->special = ConstValSpecialRuntime;
17734 ir_analyze_store_ptr(ira, &var_ptr->base, var_ptr, deref, false);17763 ir_analyze_store_ptr(ira, &var_ptr->base, var_ptr, deref, false);
17735 }17764 }
1773617765 if (instr_is_comptime(var_ptr) && (is_comptime_var || (var_class_requires_const && var->gen_is_const))) {
17737 if (instr_is_comptime(var_ptr) && var->mem_slot_index != SIZE_MAX) {17766 return ir_const_void(ira, &decl_var_instruction->base.base);
17738 assert(var->mem_slot_index < ira->exec_context.mem_slot_list.length);
17739 ZigValue *mem_slot = ira->exec_context.mem_slot_list.at(var->mem_slot_index);
17740 copy_const_val(mem_slot, init_val);
17741 ira_ref(var->owner_exec->analysis);
17742
17743 if (is_comptime_var || (var_class_requires_const && var->gen_is_const)) {
17744 return ir_const_void(ira, &decl_var_instruction->base.base);
17745 }
17746 }17767 }
17747 } else if (is_comptime_var) {17768 } else if (is_comptime_var) {
17748 ir_add_error(ira, &decl_var_instruction->base.base,17769 ir_add_error(ira, &decl_var_instruction->base.base,
...@@ -18076,6 +18097,7 @@ static IrInstGen *ir_analyze_alloca(IrAnalyze *ira, IrInst *source_inst, ZigType...@@ -18076,6 +18097,7 @@ static IrInstGen *ir_analyze_alloca(IrAnalyze *ira, IrInst *source_inst, ZigType
1807618097
18077 ZigValue *pointee = create_const_vals(1);18098 ZigValue *pointee = create_const_vals(1);
18078 pointee->special = ConstValSpecialUndef;18099 pointee->special = ConstValSpecialUndef;
18100 pointee->llvm_align = align;
1807918101
18080 IrInstGenAlloca *result = ir_build_alloca_gen(ira, source_inst, align, name_hint);18102 IrInstGenAlloca *result = ir_build_alloca_gen(ira, source_inst, align, name_hint);
18081 result->base.value->special = ConstValSpecialStatic;18103 result->base.value->special = ConstValSpecialStatic;
...@@ -18102,9 +18124,11 @@ static IrInstGen *ir_analyze_alloca(IrAnalyze *ira, IrInst *source_inst, ZigType...@@ -18102,9 +18124,11 @@ static IrInstGen *ir_analyze_alloca(IrAnalyze *ira, IrInst *source_inst, ZigType
18102 result->base.value->type = get_pointer_to_type_extra(ira->codegen, var_type, false, false,18124 result->base.value->type = get_pointer_to_type_extra(ira->codegen, var_type, false, false,
18103 PtrLenSingle, align, 0, 0, false);18125 PtrLenSingle, align, 0, 0, false);
1810418126
18105 ZigFn *fn_entry = ira->new_irb.exec->fn_entry;18127 if (!force_comptime) {
18106 if (fn_entry != nullptr) {18128 ZigFn *fn_entry = ira->new_irb.exec->fn_entry;
18107 fn_entry->alloca_gen_list.append(result);18129 if (fn_entry != nullptr) {
18130 fn_entry->alloca_gen_list.append(result);
18131 }
18108 }18132 }
18109 return &result->base;18133 return &result->base;
18110}18134}
...@@ -18192,7 +18216,7 @@ static Error ir_result_has_type(IrAnalyze *ira, ResultLoc *result_loc, bool *out...@@ -18192,7 +18216,7 @@ static Error ir_result_has_type(IrAnalyze *ira, ResultLoc *result_loc, bool *out
18192}18216}
1819318217
18194static IrInstGen *ir_resolve_no_result_loc(IrAnalyze *ira, IrInst *suspend_source_instr,18218static IrInstGen *ir_resolve_no_result_loc(IrAnalyze *ira, IrInst *suspend_source_instr,
18195 ResultLoc *result_loc, ZigType *value_type, bool force_runtime, bool non_null_comptime)18219 ResultLoc *result_loc, ZigType *value_type)
18196{18220{
18197 if (type_is_invalid(value_type))18221 if (type_is_invalid(value_type))
18198 return ira->codegen->invalid_inst_gen;18222 return ira->codegen->invalid_inst_gen;
...@@ -18209,10 +18233,25 @@ static IrInstGen *ir_resolve_no_result_loc(IrAnalyze *ira, IrInst *suspend_sourc...@@ -18209,10 +18233,25 @@ static IrInstGen *ir_resolve_no_result_loc(IrAnalyze *ira, IrInst *suspend_sourc
18209 return result_loc->resolved_loc;18233 return result_loc->resolved_loc;
18210}18234}
1821118235
18236static bool result_loc_is_discard(ResultLoc *result_loc_pass1) {
18237 if (result_loc_pass1->id == ResultLocIdInstruction &&
18238 result_loc_pass1->source_instruction->id == IrInstSrcIdConst)
18239 {
18240 IrInstSrcConst *const_inst = reinterpret_cast<IrInstSrcConst *>(result_loc_pass1->source_instruction);
18241 if (value_is_comptime(const_inst->value) &&
18242 const_inst->value->type->id == ZigTypeIdPointer &&
18243 const_inst->value->data.x_ptr.special == ConstPtrSpecialDiscard)
18244 {
18245 return true;
18246 }
18247 }
18248 return false;
18249}
18250
18212// when calling this function, at the callsite must check for result type noreturn and propagate it up18251// when calling this function, at the callsite must check for result type noreturn and propagate it up
18213static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_instr,18252static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_instr,
18214 ResultLoc *result_loc, ZigType *value_type, IrInstGen *value, bool force_runtime,18253 ResultLoc *result_loc, ZigType *value_type, IrInstGen *value, bool force_runtime,
18215 bool non_null_comptime, bool allow_discard)18254 bool allow_discard)
18216{18255{
18217 Error err;18256 Error err;
18218 if (result_loc->resolved_loc != nullptr) {18257 if (result_loc->resolved_loc != nullptr) {
...@@ -18232,53 +18271,56 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i...@@ -18232,53 +18271,56 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
18232 return nullptr;18271 return nullptr;
18233 }18272 }
18234 // need to return a result location and don't have one. use a stack allocation18273 // need to return a result location and don't have one. use a stack allocation
18235 return ir_resolve_no_result_loc(ira, suspend_source_instr, result_loc, value_type,18274 return ir_resolve_no_result_loc(ira, suspend_source_instr, result_loc, value_type);
18236 force_runtime, non_null_comptime);
18237 }18275 }
18238 case ResultLocIdVar: {18276 case ResultLocIdVar: {
18239 ResultLocVar *result_loc_var = reinterpret_cast<ResultLocVar *>(result_loc);18277 ResultLocVar *result_loc_var = reinterpret_cast<ResultLocVar *>(result_loc);
18240 assert(result_loc->source_instruction->id == IrInstSrcIdAlloca);18278 assert(result_loc->source_instruction->id == IrInstSrcIdAlloca);
18279 IrInstSrcAlloca *alloca_src = reinterpret_cast<IrInstSrcAlloca *>(result_loc->source_instruction);
1824118280
18281 ZigVar *var = result_loc_var->var;
18282 if (var->var_type != nullptr && !ir_get_var_is_comptime(var)) {
18283 // This is at least the second time we've seen this variable declaration during analysis.
18284 // This means that this is actually a different variable due to, e.g. an inline while loop.
18285 // We make a new variable so that it can hold a different type, and so the debug info can
18286 // be distinct.
18287 ZigVar *new_var = create_local_var(ira->codegen, var->decl_node, var->child_scope,
18288 buf_create_from_str(var->name), var->src_is_const, var->gen_is_const,
18289 var->shadowable, var->is_comptime, true);
18290 new_var->owner_exec = var->owner_exec;
18291 new_var->align_bytes = var->align_bytes;
18292
18293 var->next_var = new_var;
18294 var = new_var;
18295 }
18242 if (value_type->id == ZigTypeIdUnreachable || value_type->id == ZigTypeIdOpaque) {18296 if (value_type->id == ZigTypeIdUnreachable || value_type->id == ZigTypeIdOpaque) {
18243 ir_add_error(ira, &result_loc->source_instruction->base,18297 ir_add_error(ira, &result_loc->source_instruction->base,
18244 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&value_type->name)));18298 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&value_type->name)));
18245 return ira->codegen->invalid_inst_gen;18299 return ira->codegen->invalid_inst_gen;
18246 }18300 }
1824718301 if (alloca_src->base.child == nullptr || var->ptr_instruction == nullptr) {
18248 IrInstSrcAlloca *alloca_src = reinterpret_cast<IrInstSrcAlloca *>(result_loc->source_instruction);18302 bool force_comptime;
18249 bool force_comptime;18303 if (!ir_resolve_comptime(ira, alloca_src->is_comptime->child, &force_comptime))
18250 if (!ir_resolve_comptime(ira, alloca_src->is_comptime->child, &force_comptime))18304 return ira->codegen->invalid_inst_gen;
18251 return ira->codegen->invalid_inst_gen;
18252 bool is_comptime = force_comptime || (!force_runtime && value != nullptr &&
18253 value->value->special != ConstValSpecialRuntime && result_loc_var->var->gen_is_const);
18254
18255 if (alloca_src->base.child == nullptr || is_comptime) {
18256 uint32_t align = 0;18305 uint32_t align = 0;
18257 if (alloca_src->align != nullptr && !ir_resolve_align(ira, alloca_src->align->child, nullptr, &align)) {18306 if (alloca_src->align != nullptr && !ir_resolve_align(ira, alloca_src->align->child, nullptr, &align)) {
18258 return ira->codegen->invalid_inst_gen;18307 return ira->codegen->invalid_inst_gen;
18259 }18308 }
18260 IrInstGen *alloca_gen;18309 IrInstGen *alloca_gen = ir_analyze_alloca(ira, &result_loc->source_instruction->base, value_type,
18261 if (is_comptime && value != nullptr) {18310 align, alloca_src->name_hint, force_comptime);
18262 if (align > value->value->llvm_align) {18311 if (force_runtime) {
18263 value->value->llvm_align = align;18312 alloca_gen->value->data.x_ptr.mut = ConstPtrMutRuntimeVar;
18264 }18313 alloca_gen->value->special = ConstValSpecialRuntime;
18265 alloca_gen = ir_get_ref(ira, &result_loc->source_instruction->base, value, true, false);
18266 } else {
18267 alloca_gen = ir_analyze_alloca(ira, &result_loc->source_instruction->base, value_type, align,
18268 alloca_src->name_hint, force_comptime);
18269 if (force_runtime) {
18270 alloca_gen->value->data.x_ptr.mut = ConstPtrMutRuntimeVar;
18271 alloca_gen->value->special = ConstValSpecialRuntime;
18272 }
18273 }18314 }
18274 if (alloca_src->base.child != nullptr && !result_loc->written) {18315 if (alloca_src->base.child != nullptr && !result_loc->written) {
18275 alloca_src->base.child->base.ref_count = 0;18316 alloca_src->base.child->base.ref_count = 0;
18276 }18317 }
18277 alloca_src->base.child = alloca_gen;18318 alloca_src->base.child = alloca_gen;
18319 var->ptr_instruction = alloca_gen;
18278 }18320 }
18279 result_loc->written = true;18321 result_loc->written = true;
18280 result_loc->resolved_loc = is_comptime ? nullptr : alloca_src->base.child;18322 result_loc->resolved_loc = alloca_src->base.child;
18281 return result_loc->resolved_loc;18323 return alloca_src->base.child;
18282 }18324 }
18283 case ResultLocIdInstruction: {18325 case ResultLocIdInstruction: {
18284 result_loc->written = true;18326 result_loc->written = true;
...@@ -18290,27 +18332,8 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i...@@ -18290,27 +18332,8 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
18290 reinterpret_cast<ResultLocReturn *>(result_loc)->implicit_return_type_done = true;18332 reinterpret_cast<ResultLocReturn *>(result_loc)->implicit_return_type_done = true;
18291 ira->src_implicit_return_type_list.append(value);18333 ira->src_implicit_return_type_list.append(value);
18292 }18334 }
18293 if (!non_null_comptime) {
18294 bool is_comptime = value != nullptr && value->value->special != ConstValSpecialRuntime;
18295 if (is_comptime)
18296 return nullptr;
18297 }
18298 bool has_bits;
18299 if ((err = type_has_bits2(ira->codegen, ira->explicit_return_type, &has_bits)))
18300 return ira->codegen->invalid_inst_gen;
18301 if (!has_bits || !handle_is_ptr(ira->explicit_return_type)) {
18302 ZigFn *fn_entry = ira->new_irb.exec->fn_entry;
18303 if (fn_entry == nullptr || fn_entry->inferred_async_node == nullptr) {
18304 return nullptr;
18305 }
18306 }
18307
18308 ZigType *ptr_return_type = get_pointer_to_type(ira->codegen, ira->explicit_return_type, false);
18309 result_loc->written = true;18335 result_loc->written = true;
18310 result_loc->resolved_loc = ir_build_return_ptr(ira, &result_loc->source_instruction->base, ptr_return_type);18336 result_loc->resolved_loc = ira->return_ptr;
18311 if (ir_should_inline(ira->old_irb.exec, result_loc->source_instruction->base.scope)) {
18312 set_up_result_loc_for_inferred_comptime(result_loc->resolved_loc);
18313 }
18314 return result_loc->resolved_loc;18337 return result_loc->resolved_loc;
18315 }18338 }
18316 case ResultLocIdPeer: {18339 case ResultLocIdPeer: {
...@@ -18319,7 +18342,7 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i...@@ -18319,7 +18342,7 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
1831918342
18320 if (peer_parent->peers.length == 1) {18343 if (peer_parent->peers.length == 1) {
18321 IrInstGen *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,18344 IrInstGen *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
18322 value_type, value, force_runtime, non_null_comptime, true);18345 value_type, value, force_runtime, true);
18323 result_peer->suspend_pos.basic_block_index = SIZE_MAX;18346 result_peer->suspend_pos.basic_block_index = SIZE_MAX;
18324 result_peer->suspend_pos.instruction_index = SIZE_MAX;18347 result_peer->suspend_pos.instruction_index = SIZE_MAX;
18325 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||18348 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||
...@@ -18337,11 +18360,8 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i...@@ -18337,11 +18360,8 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
18337 return ira->codegen->invalid_inst_gen;18360 return ira->codegen->invalid_inst_gen;
18338 if (is_condition_comptime) {18361 if (is_condition_comptime) {
18339 peer_parent->skipped = true;18362 peer_parent->skipped = true;
18340 if (non_null_comptime) {18363 return ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
18341 return ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,18364 value_type, value, force_runtime, true);
18342 value_type, value, force_runtime, non_null_comptime, true);
18343 }
18344 return nullptr;
18345 }18365 }
18346 bool peer_parent_has_type;18366 bool peer_parent_has_type;
18347 if ((err = ir_result_has_type(ira, peer_parent->parent, &peer_parent_has_type)))18367 if ((err = ir_result_has_type(ira, peer_parent->parent, &peer_parent_has_type)))
...@@ -18349,7 +18369,7 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i...@@ -18349,7 +18369,7 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
18349 if (peer_parent_has_type) {18369 if (peer_parent_has_type) {
18350 peer_parent->skipped = true;18370 peer_parent->skipped = true;
18351 IrInstGen *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,18371 IrInstGen *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
18352 value_type, value, force_runtime || !is_condition_comptime, true, true);18372 value_type, value, force_runtime || !is_condition_comptime, true);
18353 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||18373 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||
18354 parent_result_loc->value->type->id == ZigTypeIdUnreachable)18374 parent_result_loc->value->type->id == ZigTypeIdUnreachable)
18355 {18375 {
...@@ -18374,7 +18394,7 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i...@@ -18374,7 +18394,7 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
18374 }18394 }
1837518395
18376 IrInstGen *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,18396 IrInstGen *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
18377 peer_parent->resolved_type, nullptr, force_runtime, non_null_comptime, true);18397 peer_parent->resolved_type, nullptr, force_runtime, true);
18378 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||18398 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||
18379 parent_result_loc->value->type->id == ZigTypeIdUnreachable)18399 parent_result_loc->value->type->id == ZigTypeIdUnreachable)
18380 {18400 {
...@@ -18387,21 +18407,18 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i...@@ -18387,21 +18407,18 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
18387 return result_loc->resolved_loc;18407 return result_loc->resolved_loc;
18388 }18408 }
18389 case ResultLocIdCast: {18409 case ResultLocIdCast: {
18390 if (value != nullptr && value->value->special != ConstValSpecialRuntime && !non_null_comptime)
18391 return nullptr;
18392 ResultLocCast *result_cast = reinterpret_cast<ResultLocCast *>(result_loc);18410 ResultLocCast *result_cast = reinterpret_cast<ResultLocCast *>(result_loc);
18393 ZigType *dest_type = ir_resolve_type(ira, result_cast->base.source_instruction->child);18411 ZigType *dest_type = ir_resolve_type(ira, result_cast->base.source_instruction->child);
18394 if (type_is_invalid(dest_type))18412 if (type_is_invalid(dest_type))
18395 return ira->codegen->invalid_inst_gen;18413 return ira->codegen->invalid_inst_gen;
1839618414
18397 if (dest_type == ira->codegen->builtin_types.entry_var) {18415 if (dest_type == ira->codegen->builtin_types.entry_var) {
18398 return ir_resolve_no_result_loc(ira, suspend_source_instr, result_loc, value_type,18416 return ir_resolve_no_result_loc(ira, suspend_source_instr, result_loc, value_type);
18399 force_runtime, non_null_comptime);
18400 }18417 }
1840118418
18402 IrInstGen *casted_value;18419 IrInstGen *casted_value;
18403 if (value != nullptr) {18420 if (value != nullptr) {
18404 casted_value = ir_implicit_cast(ira, value, dest_type);18421 casted_value = ir_implicit_cast2(ira, suspend_source_instr, value, dest_type);
18405 if (type_is_invalid(casted_value->value->type))18422 if (type_is_invalid(casted_value->value->type))
18406 return ira->codegen->invalid_inst_gen;18423 return ira->codegen->invalid_inst_gen;
18407 dest_type = casted_value->value->type;18424 dest_type = casted_value->value->type;
...@@ -18410,7 +18427,7 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i...@@ -18410,7 +18427,7 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
18410 }18427 }
1841118428
18412 IrInstGen *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, result_cast->parent,18429 IrInstGen *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, result_cast->parent,
18413 dest_type, casted_value, force_runtime, non_null_comptime, true);18430 dest_type, casted_value, force_runtime, true);
18414 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||18431 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||
18415 parent_result_loc->value->type->id == ZigTypeIdUnreachable)18432 parent_result_loc->value->type->id == ZigTypeIdUnreachable)
18416 {18433 {
...@@ -18460,13 +18477,12 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i...@@ -18460,13 +18477,12 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
18460 // We will not be able to provide a result location for this value. Create18477 // We will not be able to provide a result location for this value. Create
18461 // a new result location.18478 // a new result location.
18462 result_cast->parent->written = false;18479 result_cast->parent->written = false;
18463 return ir_resolve_no_result_loc(ira, suspend_source_instr, result_loc, value_type,18480 return ir_resolve_no_result_loc(ira, suspend_source_instr, result_loc, value_type);
18464 force_runtime, non_null_comptime);
18465 }18481 }
1846618482
18467 result_loc->written = true;18483 result_loc->written = true;
18468 result_loc->resolved_loc = ir_analyze_ptr_cast(ira, suspend_source_instr, parent_result_loc,18484 result_loc->resolved_loc = ir_analyze_ptr_cast(ira, suspend_source_instr, parent_result_loc,
18469 ptr_type, &result_cast->base.source_instruction->base, false);18485 &parent_result_loc->base, ptr_type, &result_cast->base.source_instruction->base, false);
18470 return result_loc->resolved_loc;18486 return result_loc->resolved_loc;
18471 }18487 }
18472 case ResultLocIdBitCast: {18488 case ResultLocIdBitCast: {
...@@ -18507,12 +18523,13 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i...@@ -18507,12 +18523,13 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
18507 bitcasted_value = nullptr;18523 bitcasted_value = nullptr;
18508 }18524 }
1850918525
18510 if (bitcasted_value == nullptr || type_is_invalid(bitcasted_value->value->type)) {18526 if (bitcasted_value != nullptr && type_is_invalid(bitcasted_value->value->type)) {
18511 return bitcasted_value;18527 return bitcasted_value;
18512 }18528 }
1851318529
18530 bool parent_was_written = result_bit_cast->parent->written;
18514 IrInstGen *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, result_bit_cast->parent,18531 IrInstGen *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, result_bit_cast->parent,
18515 dest_type, bitcasted_value, force_runtime, non_null_comptime, true);18532 dest_type, bitcasted_value, force_runtime, true);
18516 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||18533 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||
18517 parent_result_loc->value->type->id == ZigTypeIdUnreachable)18534 parent_result_loc->value->type->id == ZigTypeIdUnreachable)
18518 {18535 {
...@@ -18522,32 +18539,38 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i...@@ -18522,32 +18539,38 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
18522 assert(parent_ptr_type->id == ZigTypeIdPointer);18539 assert(parent_ptr_type->id == ZigTypeIdPointer);
18523 ZigType *child_type = parent_ptr_type->data.pointer.child_type;18540 ZigType *child_type = parent_ptr_type->data.pointer.child_type;
1852418541
18525 bool has_bits;18542 if (result_loc_is_discard(result_bit_cast->parent)) {
18526 if ((err = type_has_bits2(ira->codegen, child_type, &has_bits))) {
18527 return ira->codegen->invalid_inst_gen;
18528 }
18529
18530 // This happens when the bitCast result is assigned to _
18531 if (!has_bits) {
18532 assert(allow_discard);18543 assert(allow_discard);
18533 return parent_result_loc;18544 return parent_result_loc;
18534 }18545 }
1853518546
18536 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusAlignmentKnown))) {18547 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown))) {
18537 return ira->codegen->invalid_inst_gen;18548 return ira->codegen->invalid_inst_gen;
18538 }18549 }
1853918550
18540 uint64_t parent_ptr_align = get_ptr_align(ira->codegen, parent_ptr_type);18551 if ((err = type_resolve(ira->codegen, value_type, ResolveStatusSizeKnown))) {
18541 if ((err = type_resolve(ira->codegen, value_type, ResolveStatusAlignmentKnown))) {
18542 return ira->codegen->invalid_inst_gen;18552 return ira->codegen->invalid_inst_gen;
18543 }18553 }
18554
18555 if (child_type != ira->codegen->builtin_types.entry_var) {
18556 if (type_size(ira->codegen, child_type) != type_size(ira->codegen, value_type)) {
18557 // pointer cast won't work; we need a temporary location.
18558 result_bit_cast->parent->written = parent_was_written;
18559 result_loc->written = true;
18560 result_loc->resolved_loc = ir_resolve_result(ira, suspend_source_instr, no_result_loc(),
18561 value_type, bitcasted_value, force_runtime, true);
18562 return result_loc->resolved_loc;
18563 }
18564 }
18565 uint64_t parent_ptr_align = 0;
18566 if (type_has_bits(value_type)) parent_ptr_align = get_ptr_align(ira->codegen, parent_ptr_type);
18544 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, value_type,18567 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, value_type,
18545 parent_ptr_type->data.pointer.is_const, parent_ptr_type->data.pointer.is_volatile, PtrLenSingle,18568 parent_ptr_type->data.pointer.is_const, parent_ptr_type->data.pointer.is_volatile, PtrLenSingle,
18546 parent_ptr_align, 0, 0, parent_ptr_type->data.pointer.allow_zero);18569 parent_ptr_align, 0, 0, parent_ptr_type->data.pointer.allow_zero);
1854718570
18548 result_loc->written = true;18571 result_loc->written = true;
18549 result_loc->resolved_loc = ir_analyze_ptr_cast(ira, suspend_source_instr, parent_result_loc,18572 result_loc->resolved_loc = ir_analyze_ptr_cast(ira, suspend_source_instr, parent_result_loc,
18550 ptr_type, &result_bit_cast->base.source_instruction->base, false);18573 &parent_result_loc->base, ptr_type, &result_bit_cast->base.source_instruction->base, false);
18551 return result_loc->resolved_loc;18574 return result_loc->resolved_loc;
18552 }18575 }
18553 }18576 }
...@@ -18556,25 +18579,19 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i...@@ -18556,25 +18579,19 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
1855618579
18557static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr,18580static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr,
18558 ResultLoc *result_loc_pass1, ZigType *value_type, IrInstGen *value, bool force_runtime,18581 ResultLoc *result_loc_pass1, ZigType *value_type, IrInstGen *value, bool force_runtime,
18559 bool non_null_comptime, bool allow_discard)18582 bool allow_discard)
18560{18583{
18561 Error err;18584 if (!allow_discard && result_loc_is_discard(result_loc_pass1)) {
18562 if (!allow_discard && result_loc_pass1->id == ResultLocIdInstruction &&18585 result_loc_pass1 = no_result_loc();
18563 result_loc_pass1->source_instruction->id == IrInstSrcIdConst)
18564 {
18565 IrInstSrcConst *const_inst = reinterpret_cast<IrInstSrcConst *>(result_loc_pass1->source_instruction);
18566 if (value_is_comptime(const_inst->value) &&
18567 const_inst->value->type->id == ZigTypeIdPointer &&
18568 const_inst->value->data.x_ptr.special == ConstPtrSpecialDiscard)
18569 {
18570 result_loc_pass1 = no_result_loc();
18571 }
18572 }18586 }
18573 bool was_already_resolved = result_loc_pass1->resolved_loc != nullptr;18587 bool was_written = result_loc_pass1->written;
18574 IrInstGen *result_loc = ir_resolve_result_raw(ira, suspend_source_instr, result_loc_pass1, value_type,18588 IrInstGen *result_loc = ir_resolve_result_raw(ira, suspend_source_instr, result_loc_pass1, value_type,
18575 value, force_runtime, non_null_comptime, allow_discard);18589 value, force_runtime, allow_discard);
18576 if (result_loc == nullptr || (result_loc->value->type->id == ZigTypeIdUnreachable || type_is_invalid(result_loc->value->type)))18590 if (result_loc == nullptr || result_loc->value->type->id == ZigTypeIdUnreachable ||
18591 type_is_invalid(result_loc->value->type))
18592 {
18577 return result_loc;18593 return result_loc;
18594 }
1857818595
18579 if ((force_runtime || (value != nullptr && !instr_is_comptime(value))) &&18596 if ((force_runtime || (value != nullptr && !instr_is_comptime(value))) &&
18580 result_loc_pass1->written && result_loc->value->data.x_ptr.mut == ConstPtrMutInfer)18597 result_loc_pass1->written && result_loc->value->data.x_ptr.mut == ConstPtrMutInfer)
...@@ -18583,56 +18600,63 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr...@@ -18583,56 +18600,63 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr
18583 }18600 }
1858418601
18585 InferredStructField *isf = result_loc->value->type->data.pointer.inferred_struct_field;18602 InferredStructField *isf = result_loc->value->type->data.pointer.inferred_struct_field;
18586 if (!was_already_resolved && isf != nullptr) {18603 if (isf != nullptr) {
18587 // Now it's time to add the field to the struct type.18604 TypeStructField *field;
18588 uint32_t old_field_count = isf->inferred_struct_type->data.structure.src_field_count;
18589 uint32_t new_field_count = old_field_count + 1;
18590 isf->inferred_struct_type->data.structure.src_field_count = new_field_count;
18591 isf->inferred_struct_type->data.structure.fields = realloc_type_struct_fields(
18592 isf->inferred_struct_type->data.structure.fields, old_field_count, new_field_count);
18593
18594 TypeStructField *field = isf->inferred_struct_type->data.structure.fields[old_field_count];
18595 field->name = isf->field_name;
18596 field->type_entry = value_type;
18597 field->type_val = create_const_type(ira->codegen, field->type_entry);
18598 field->src_index = old_field_count;
18599 field->decl_node = value ? value->base.source_node : suspend_source_instr->source_node;
18600 if (value && instr_is_comptime(value)) {
18601 ZigValue *val = ir_resolve_const(ira, value, UndefOk);
18602 if (!val)
18603 return ira->codegen->invalid_inst_gen;
18604 field->is_comptime = true;
18605 field->init_val = create_const_vals(1);
18606 copy_const_val(field->init_val, val);
18607 return result_loc;
18608 }
18609
18610 ZigType *struct_ptr_type = get_pointer_to_type(ira->codegen, isf->inferred_struct_type, false);
18611 IrInstGen *casted_ptr;18605 IrInstGen *casted_ptr;
18612 if (instr_is_comptime(result_loc)) {18606 if (isf->already_resolved) {
18613 casted_ptr = ir_const(ira, suspend_source_instr, struct_ptr_type);18607 field = find_struct_type_field(isf->inferred_struct_type, isf->field_name);
18614 copy_const_val(casted_ptr->value, result_loc->value);
18615 casted_ptr->value->type = struct_ptr_type;
18616 } else {
18617 casted_ptr = result_loc;18608 casted_ptr = result_loc;
18618 }18609 } else {
18619 if (instr_is_comptime(casted_ptr)) {18610 isf->already_resolved = true;
18620 ZigValue *ptr_val = ir_resolve_const(ira, casted_ptr, UndefBad);18611 // Now it's time to add the field to the struct type.
18621 if (!ptr_val)18612 uint32_t old_field_count = isf->inferred_struct_type->data.structure.src_field_count;
18622 return ira->codegen->invalid_inst_gen;18613 uint32_t new_field_count = old_field_count + 1;
18623 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {18614 isf->inferred_struct_type->data.structure.src_field_count = new_field_count;
18624 ZigValue *struct_val = const_ptr_pointee(ira, ira->codegen, ptr_val,18615 isf->inferred_struct_type->data.structure.fields = realloc_type_struct_fields(
18625 suspend_source_instr->source_node);18616 isf->inferred_struct_type->data.structure.fields, old_field_count, new_field_count);
18626 struct_val->special = ConstValSpecialStatic;18617
18627 struct_val->data.x_struct.fields = realloc_const_vals_ptrs(struct_val->data.x_struct.fields,18618 field = isf->inferred_struct_type->data.structure.fields[old_field_count];
18628 old_field_count, new_field_count);18619 field->name = isf->field_name;
18620 field->type_entry = value_type;
18621 field->type_val = create_const_type(ira->codegen, field->type_entry);
18622 field->src_index = old_field_count;
18623 field->decl_node = value ? value->base.source_node : suspend_source_instr->source_node;
18624 if (value && instr_is_comptime(value)) {
18625 ZigValue *val = ir_resolve_const(ira, value, UndefOk);
18626 if (!val)
18627 return ira->codegen->invalid_inst_gen;
18628 field->is_comptime = true;
18629 field->init_val = create_const_vals(1);
18630 copy_const_val(field->init_val, val);
18631 return result_loc;
18632 }
1862918633
18630 ZigValue *field_val = struct_val->data.x_struct.fields[old_field_count];18634 ZigType *struct_ptr_type = get_pointer_to_type(ira->codegen, isf->inferred_struct_type, false);
18631 field_val->special = ConstValSpecialUndef;18635 if (instr_is_comptime(result_loc)) {
18632 field_val->type = field->type_entry;18636 casted_ptr = ir_const(ira, suspend_source_instr, struct_ptr_type);
18633 field_val->parent.id = ConstParentIdStruct;18637 copy_const_val(casted_ptr->value, result_loc->value);
18634 field_val->parent.data.p_struct.struct_val = struct_val;18638 casted_ptr->value->type = struct_ptr_type;
18635 field_val->parent.data.p_struct.field_index = old_field_count;18639 } else {
18640 casted_ptr = result_loc;
18641 }
18642 if (instr_is_comptime(casted_ptr)) {
18643 ZigValue *ptr_val = ir_resolve_const(ira, casted_ptr, UndefBad);
18644 if (!ptr_val)
18645 return ira->codegen->invalid_inst_gen;
18646 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
18647 ZigValue *struct_val = const_ptr_pointee(ira, ira->codegen, ptr_val,
18648 suspend_source_instr->source_node);
18649 struct_val->special = ConstValSpecialStatic;
18650 struct_val->data.x_struct.fields = realloc_const_vals_ptrs(struct_val->data.x_struct.fields,
18651 old_field_count, new_field_count);
18652
18653 ZigValue *field_val = struct_val->data.x_struct.fields[old_field_count];
18654 field_val->special = ConstValSpecialUndef;
18655 field_val->type = field->type_entry;
18656 field_val->parent.id = ConstParentIdStruct;
18657 field_val->parent.data.p_struct.struct_val = struct_val;
18658 field_val->parent.data.p_struct.field_index = old_field_count;
18659 }
18636 }18660 }
18637 }18661 }
1863818662
...@@ -18641,36 +18665,35 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr...@@ -18641,36 +18665,35 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr
18641 result_loc_pass1->resolved_loc = result_loc;18665 result_loc_pass1->resolved_loc = result_loc;
18642 }18666 }
1864318667
18668 if (was_written) {
18669 return result_loc;
18670 }
1864418671
18645 ir_assert(result_loc->value->type->id == ZigTypeIdPointer, suspend_source_instr);18672 ir_assert(result_loc->value->type->id == ZigTypeIdPointer, suspend_source_instr);
18646 ZigType *actual_elem_type = result_loc->value->type->data.pointer.child_type;18673 ZigType *actual_elem_type = result_loc->value->type->data.pointer.child_type;
18647 if (actual_elem_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional &&18674 if (actual_elem_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional &&
18648 value_type->id != ZigTypeIdNull)18675 value_type->id != ZigTypeIdNull && value_type->id != ZigTypeIdUndefined)
18649 {18676 {
18650 bool has_bits;18677 bool same_comptime_repr = types_have_same_zig_comptime_repr(ira->codegen, actual_elem_type, value_type);
18651 if ((err = type_has_bits2(ira->codegen, value_type, &has_bits)))18678 if (!same_comptime_repr) {
18652 return ira->codegen->invalid_inst_gen;18679 result_loc_pass1->written = was_written;
18653 if (has_bits) {
18654 result_loc_pass1->written = false;
18655 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, result_loc, false, true);18680 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, result_loc, false, true);
18656 }18681 }
18657 } else if (actual_elem_type->id == ZigTypeIdErrorUnion && value_type->id != ZigTypeIdErrorUnion) {18682 } else if (actual_elem_type->id == ZigTypeIdErrorUnion && value_type->id != ZigTypeIdErrorUnion &&
18658 bool has_bits;18683 value_type->id != ZigTypeIdUndefined)
18659 if ((err = type_has_bits2(ira->codegen, value_type, &has_bits)))18684 {
18660 return ira->codegen->invalid_inst_gen;18685 if (value_type->id == ZigTypeIdErrorSet) {
18661 if (has_bits) {18686 return ir_analyze_unwrap_err_code(ira, suspend_source_instr, result_loc, true);
18662 if (value_type->id == ZigTypeIdErrorSet) {18687 } else {
18663 return ir_analyze_unwrap_err_code(ira, suspend_source_instr, result_loc, true);18688 IrInstGen *unwrapped_err_ptr = ir_analyze_unwrap_error_payload(ira, suspend_source_instr,
18689 result_loc, false, true);
18690 ZigType *actual_payload_type = actual_elem_type->data.error_union.payload_type;
18691 if (actual_payload_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional &&
18692 value_type->id != ZigTypeIdNull && value_type->id != ZigTypeIdUndefined)
18693 {
18694 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, unwrapped_err_ptr, false, true);
18664 } else {18695 } else {
18665 IrInstGen *unwrapped_err_ptr = ir_analyze_unwrap_error_payload(ira, suspend_source_instr,18696 return unwrapped_err_ptr;
18666 result_loc, false, true);
18667 ZigType *actual_payload_type = actual_elem_type->data.error_union.payload_type;
18668 if (actual_payload_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional &&
18669 value_type->id != ZigTypeIdNull) {
18670 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, unwrapped_err_ptr, false, true);
18671 } else {
18672 return unwrapped_err_ptr;
18673 }
18674 }18697 }
18675 }18698 }
18676 }18699 }
...@@ -18717,7 +18740,7 @@ static IrInstGen *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrInstSr...@@ -18717,7 +18740,7 @@ static IrInstGen *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrInstSr
18717 return ira->codegen->invalid_inst_gen;18740 return ira->codegen->invalid_inst_gen;
18718 }18741 }
18719 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,18742 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
18720 implicit_elem_type, nullptr, false, true, true);18743 implicit_elem_type, nullptr, false, true);
18721 if (result_loc != nullptr)18744 if (result_loc != nullptr)
18722 return result_loc;18745 return result_loc;
1872318746
...@@ -18726,7 +18749,7 @@ static IrInstGen *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrInstSr...@@ -18726,7 +18749,7 @@ static IrInstGen *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrInstSr
18726 instruction->result_loc->id == ResultLocIdReturn)18749 instruction->result_loc->id == ResultLocIdReturn)
18727 {18750 {
18728 result_loc = ir_resolve_result(ira, &instruction->base.base, no_result_loc(),18751 result_loc = ir_resolve_result(ira, &instruction->base.base, no_result_loc(),
18729 implicit_elem_type, nullptr, false, true, true);18752 implicit_elem_type, nullptr, false, true);
18730 if (result_loc != nullptr &&18753 if (result_loc != nullptr &&
18731 (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable))18754 (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable))
18732 {18755 {
...@@ -18829,11 +18852,12 @@ static IrInstGen *ir_analyze_async_call(IrAnalyze *ira, IrInst* source_instr, Zi...@@ -18829,11 +18852,12 @@ static IrInstGen *ir_analyze_async_call(IrAnalyze *ira, IrInst* source_instr, Zi
18829 } else {18852 } else {
18830 ZigType *frame_type = get_fn_frame_type(ira->codegen, fn_entry);18853 ZigType *frame_type = get_fn_frame_type(ira->codegen, fn_entry);
18831 IrInstGen *result_loc = ir_resolve_result(ira, source_instr, call_result_loc,18854 IrInstGen *result_loc = ir_resolve_result(ira, source_instr, call_result_loc,
18832 frame_type, nullptr, true, true, false);18855 frame_type, nullptr, true, false);
18833 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {18856 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
18834 return result_loc;18857 return result_loc;
18835 }18858 }
18836 result_loc = ir_implicit_cast(ira, result_loc, get_pointer_to_type(ira->codegen, frame_type, false));18859 result_loc = ir_implicit_cast2(ira, &call_result_loc->source_instruction->base, result_loc,
18860 get_pointer_to_type(ira->codegen, frame_type, false));
18837 if (type_is_invalid(result_loc->value->type))18861 if (type_is_invalid(result_loc->value->type))
18838 return ira->codegen->invalid_inst_gen;18862 return ira->codegen->invalid_inst_gen;
18839 return &ir_build_call_gen(ira, source_instr, fn_entry, fn_ref, arg_count,18863 return &ir_build_call_gen(ira, source_instr, fn_entry, fn_ref, arg_count,
...@@ -18875,7 +18899,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node...@@ -18875,7 +18899,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node
18875}18899}
1887618900
18877static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_node,18901static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_node,
18878 IrInstGen *arg, Scope **child_scope, size_t *next_proto_i,18902 IrInstGen *arg, IrInst *arg_src, Scope **child_scope, size_t *next_proto_i,
18879 GenericFnTypeId *generic_id, FnTypeId *fn_type_id, IrInstGen **casted_args,18903 GenericFnTypeId *generic_id, FnTypeId *fn_type_id, IrInstGen **casted_args,
18880 ZigFn *impl_fn)18904 ZigFn *impl_fn)
18881{18905{
...@@ -18894,7 +18918,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod...@@ -18894,7 +18918,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
18894 if (type_is_invalid(param_type))18918 if (type_is_invalid(param_type))
18895 return false;18919 return false;
1889618920
18897 casted_arg = ir_implicit_cast(ira, arg, param_type);18921 casted_arg = ir_implicit_cast2(ira, arg_src, arg, param_type);
18898 if (type_is_invalid(casted_arg->value->type))18922 if (type_is_invalid(casted_arg->value->type))
18899 return false;18923 return false;
18900 } else {18924 } else {
...@@ -18965,41 +18989,28 @@ static IrInstGen *ir_get_var_ptr(IrAnalyze *ira, IrInst *source_instr, ZigVar *v...@@ -18965,41 +18989,28 @@ static IrInstGen *ir_get_var_ptr(IrAnalyze *ira, IrInst *source_instr, ZigVar *v
18965 var = var->next_var;18989 var = var->next_var;
18966 }18990 }
1896718991
18968 if (var->mem_slot_index != SIZE_MAX && var->owner_exec->analysis == nullptr) {
18969 assert(ira->codegen->errors.length != 0);
18970 return ira->codegen->invalid_inst_gen;
18971 }
18972 if (var->var_type == nullptr || type_is_invalid(var->var_type))18992 if (var->var_type == nullptr || type_is_invalid(var->var_type))
18973 return ira->codegen->invalid_inst_gen;18993 return ira->codegen->invalid_inst_gen;
1897418994
18975 ZigValue *mem_slot = nullptr;18995 bool is_volatile = false;
18996 ZigType *var_ptr_type = get_pointer_to_type_extra(ira->codegen, var->var_type,
18997 var->src_is_const, is_volatile, PtrLenSingle, var->align_bytes, 0, 0, false);
18998
18999 if (var->ptr_instruction != nullptr) {
19000 return ir_implicit_cast(ira, var->ptr_instruction, var_ptr_type);
19001 }
1897619002
18977 bool comptime_var_mem = ir_get_var_is_comptime(var);19003 bool comptime_var_mem = ir_get_var_is_comptime(var);
18978 bool linkage_makes_it_runtime = var->decl_node->data.variable_declaration.is_extern;19004 bool linkage_makes_it_runtime = var->decl_node->data.variable_declaration.is_extern;
18979 bool is_volatile = false;
1898019005
18981 IrInstGen *result = ir_build_var_ptr_gen(ira, source_instr, var);19006 IrInstGen *result = ir_build_var_ptr_gen(ira, source_instr, var);
18982 result->value->type = get_pointer_to_type_extra(ira->codegen, var->var_type,19007 result->value->type = var_ptr_type;
18983 var->src_is_const, is_volatile, PtrLenSingle, var->align_bytes, 0, 0, false);
1898419008
18985 if (linkage_makes_it_runtime || var->is_thread_local)19009 if (!linkage_makes_it_runtime && !var->is_thread_local && value_is_comptime(var->const_value)) {
18986 goto no_mem_slot;19010 ZigValue *val = var->const_value;
1898719011 switch (val->special) {
18988 if (value_is_comptime(var->const_value)) {
18989 mem_slot = var->const_value;
18990 } else if (var->mem_slot_index != SIZE_MAX && (comptime_var_mem || var->gen_is_const)) {
18991 // find the relevant exec_context
18992 assert(var->owner_exec != nullptr);
18993 assert(var->owner_exec->analysis != nullptr);
18994 IrExecContext *exec_context = &var->owner_exec->analysis->exec_context;
18995 assert(var->mem_slot_index < exec_context->mem_slot_list.length);
18996 mem_slot = exec_context->mem_slot_list.at(var->mem_slot_index);
18997 }
18998
18999 if (mem_slot != nullptr) {
19000 switch (mem_slot->special) {
19001 case ConstValSpecialRuntime:19012 case ConstValSpecialRuntime:
19002 goto no_mem_slot;19013 break;
19003 case ConstValSpecialStatic: // fallthrough19014 case ConstValSpecialStatic: // fallthrough
19004 case ConstValSpecialLazy: // fallthrough19015 case ConstValSpecialLazy: // fallthrough
19005 case ConstValSpecialUndef: {19016 case ConstValSpecialUndef: {
...@@ -19015,15 +19026,12 @@ static IrInstGen *ir_get_var_ptr(IrAnalyze *ira, IrInst *source_instr, ZigVar *v...@@ -19015,15 +19026,12 @@ static IrInstGen *ir_get_var_ptr(IrAnalyze *ira, IrInst *source_instr, ZigVar *v
19015 result->value->special = ConstValSpecialStatic;19026 result->value->special = ConstValSpecialStatic;
19016 result->value->data.x_ptr.mut = ptr_mut;19027 result->value->data.x_ptr.mut = ptr_mut;
19017 result->value->data.x_ptr.special = ConstPtrSpecialRef;19028 result->value->data.x_ptr.special = ConstPtrSpecialRef;
19018 result->value->data.x_ptr.data.ref.pointee = mem_slot;19029 result->value->data.x_ptr.data.ref.pointee = val;
19019 return result;19030 return result;
19020 }19031 }
19021 }19032 }
19022 zig_unreachable();
19023 }19033 }
1902419034
19025no_mem_slot:
19026
19027 bool in_fn_scope = (scope_fn_entry(var->parent_scope) != nullptr);19035 bool in_fn_scope = (scope_fn_entry(var->parent_scope) != nullptr);
19028 result->value->data.rh_ptr = in_fn_scope ? RuntimeHintPtrStack : RuntimeHintPtrNonStack;19036 result->value->data.rh_ptr = in_fn_scope ? RuntimeHintPtrStack : RuntimeHintPtrNonStack;
1902919037
...@@ -19166,7 +19174,7 @@ static IrInstGen *ir_analyze_store_ptr(IrAnalyze *ira, IrInst* source_instr,...@@ -19166,7 +19174,7 @@ static IrInstGen *ir_analyze_store_ptr(IrAnalyze *ira, IrInst* source_instr,
19166}19174}
1916719175
19168static IrInstGen *analyze_casted_new_stack(IrAnalyze *ira, IrInst* source_instr,19176static IrInstGen *analyze_casted_new_stack(IrAnalyze *ira, IrInst* source_instr,
19169 IrInstGen *new_stack, bool is_async_call_builtin, ZigFn *fn_entry)19177 IrInstGen *new_stack, IrInst *new_stack_src, bool is_async_call_builtin, ZigFn *fn_entry)
19170{19178{
19171 if (new_stack == nullptr)19179 if (new_stack == nullptr)
19172 return nullptr;19180 return nullptr;
...@@ -19191,14 +19199,14 @@ static IrInstGen *analyze_casted_new_stack(IrAnalyze *ira, IrInst* source_instr,...@@ -19191,14 +19199,14 @@ static IrInstGen *analyze_casted_new_stack(IrAnalyze *ira, IrInst* source_instr,
19191 false, false, PtrLenUnknown, target_fn_align(ira->codegen->zig_target), 0, 0, false);19199 false, false, PtrLenUnknown, target_fn_align(ira->codegen->zig_target), 0, 0, false);
19192 ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);19200 ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);
19193 ira->codegen->need_frame_size_prefix_data = true;19201 ira->codegen->need_frame_size_prefix_data = true;
19194 return ir_implicit_cast(ira, new_stack, u8_slice);19202 return ir_implicit_cast2(ira, new_stack_src, new_stack, u8_slice);
19195 }19203 }
19196}19204}
1919719205
19198static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,19206static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19199 ZigFn *fn_entry, ZigType *fn_type, IrInstGen *fn_ref,19207 ZigFn *fn_entry, ZigType *fn_type, IrInstGen *fn_ref,
19200 IrInstGen *first_arg_ptr, CallModifier modifier,19208 IrInstGen *first_arg_ptr, IrInst *first_arg_ptr_src, CallModifier modifier,
19201 IrInstGen *new_stack, bool is_async_call_builtin,19209 IrInstGen *new_stack, IrInst *new_stack_src, bool is_async_call_builtin,
19202 IrInstGen **args_ptr, size_t args_len, IrInstGen *ret_ptr, ResultLoc *call_result_loc)19210 IrInstGen **args_ptr, size_t args_len, IrInstGen *ret_ptr, ResultLoc *call_result_loc)
19203{19211{
19204 Error err;19212 Error err;
...@@ -19285,13 +19293,6 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19285,13 +19293,6 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19285 return ira->codegen->invalid_inst_gen;19293 return ira->codegen->invalid_inst_gen;
19286 }19294 }
1928719295
19288 if (fn_proto_node->data.fn_proto.is_var_args) {
19289 ir_add_error(ira, source_instr,
19290 buf_sprintf("compiler bug: unable to call var args function at compile time. https://github.com/ziglang/zig/issues/313"));
19291 return ira->codegen->invalid_inst_gen;
19292 }
19293
19294
19295 for (size_t call_i = 0; call_i < args_len; call_i += 1) {19296 for (size_t call_i = 0; call_i < args_len; call_i += 1) {
19296 IrInstGen *old_arg = args_ptr[call_i];19297 IrInstGen *old_arg = args_ptr[call_i];
1929719298
...@@ -19325,10 +19326,17 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19325,10 +19326,17 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19325 if (result == nullptr) {19326 if (result == nullptr) {
19326 // Analyze the fn body block like any other constant expression.19327 // Analyze the fn body block like any other constant expression.
19327 AstNode *body_node = fn_entry->body_node;19328 AstNode *body_node = fn_entry->body_node;
19328 result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type,19329 ZigValue *result_ptr;
19329 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry,19330 create_result_ptr(ira->codegen, return_type, &result, &result_ptr);
19330 nullptr, source_instr->source_node, nullptr, ira->new_irb.exec, return_type_node,19331 if ((err = ir_eval_const_value(ira->codegen, exec_scope, body_node, result_ptr,
19331 UndefOk);19332 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota,
19333 fn_entry, nullptr, source_instr->source_node, nullptr, ira->new_irb.exec, return_type_node,
19334 UndefOk)))
19335 {
19336 return ira->codegen->invalid_inst_gen;
19337 }
19338 destroy(result_ptr, "ZigValue");
19339 result_ptr = nullptr;
1933219340
19333 if (inferred_err_set_type != nullptr) {19341 if (inferred_err_set_type != nullptr) {
19334 inferred_err_set_type->data.error_set.incomplete = false;19342 inferred_err_set_type->data.error_set.incomplete = false;
...@@ -19412,8 +19420,8 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19412,8 +19420,8 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19412 return ira->codegen->invalid_inst_gen;19420 return ira->codegen->invalid_inst_gen;
19413 }19421 }
1941419422
19415 if (!ir_analyze_fn_call_generic_arg(ira, fn_proto_node, first_arg, &impl_fn->child_scope,19423 if (!ir_analyze_fn_call_generic_arg(ira, fn_proto_node, first_arg, first_arg_ptr_src,
19416 &next_proto_i, generic_id, &inst_fn_type_id, casted_args, impl_fn))19424 &impl_fn->child_scope, &next_proto_i, generic_id, &inst_fn_type_id, casted_args, impl_fn))
19417 {19425 {
19418 return ira->codegen->invalid_inst_gen;19426 return ira->codegen->invalid_inst_gen;
19419 }19427 }
...@@ -19427,7 +19435,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19427,7 +19435,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19427 AstNode *param_decl_node = fn_proto_node->data.fn_proto.params.at(next_proto_i);19435 AstNode *param_decl_node = fn_proto_node->data.fn_proto.params.at(next_proto_i);
19428 assert(param_decl_node->type == NodeTypeParamDecl);19436 assert(param_decl_node->type == NodeTypeParamDecl);
1942919437
19430 if (!ir_analyze_fn_call_generic_arg(ira, fn_proto_node, arg, &impl_fn->child_scope,19438 if (!ir_analyze_fn_call_generic_arg(ira, fn_proto_node, arg, &arg->base, &impl_fn->child_scope,
19431 &next_proto_i, generic_id, &inst_fn_type_id, casted_args, impl_fn))19439 &next_proto_i, generic_id, &inst_fn_type_id, casted_args, impl_fn))
19432 {19440 {
19433 return ira->codegen->invalid_inst_gen;19441 return ira->codegen->invalid_inst_gen;
...@@ -19435,14 +19443,21 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19435,14 +19443,21 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19435 }19443 }
1943619444
19437 if (fn_proto_node->data.fn_proto.align_expr != nullptr) {19445 if (fn_proto_node->data.fn_proto.align_expr != nullptr) {
19438 ZigValue *align_result = ir_eval_const_value(ira->codegen, impl_fn->child_scope,19446 ZigValue *align_result;
19439 fn_proto_node->data.fn_proto.align_expr, get_align_amt_type(ira->codegen),19447 ZigValue *result_ptr;
19440 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota,19448 create_result_ptr(ira->codegen, get_align_amt_type(ira->codegen), &align_result, &result_ptr);
19441 nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec,19449 if ((err = ir_eval_const_value(ira->codegen, impl_fn->child_scope,
19442 nullptr, UndefBad);19450 fn_proto_node->data.fn_proto.align_expr, result_ptr,
19443 IrInstGenConst *const_instruction = ir_create_inst_gen<IrInstGenConst>(&ira->new_irb,19451 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota,
19452 nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec,
19453 nullptr, UndefBad)))
19454 {
19455 return ira->codegen->invalid_inst_gen;
19456 }
19457 IrInstGenConst *const_instruction = ir_create_inst_noval<IrInstGenConst>(&ira->new_irb,
19444 impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr);19458 impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr);
19445 copy_const_val(const_instruction->base.value, align_result);19459 const_instruction->base.value = align_result;
19460 destroy(result_ptr, "ZigValue");
1944619461
19447 uint32_t align_bytes = 0;19462 uint32_t align_bytes = 0;
19448 ir_resolve_align(ira, &const_instruction->base, nullptr, &align_bytes);19463 ir_resolve_align(ira, &const_instruction->base, nullptr, &align_bytes);
...@@ -19468,8 +19483,8 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19468,8 +19483,8 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19468 case ReqCompTimeYes:19483 case ReqCompTimeYes:
19469 // Throw out our work and call the function as if it were comptime.19484 // Throw out our work and call the function as if it were comptime.
19470 return ir_analyze_fn_call(ira, source_instr, fn_entry, fn_type, fn_ref, first_arg_ptr,19485 return ir_analyze_fn_call(ira, source_instr, fn_entry, fn_type, fn_ref, first_arg_ptr,
19471 CallModifierCompileTime, new_stack, is_async_call_builtin, args_ptr, args_len,19486 first_arg_ptr_src, CallModifierCompileTime, new_stack, new_stack_src, is_async_call_builtin,
19472 ret_ptr, call_result_loc);19487 args_ptr, args_len, ret_ptr, call_result_loc);
19473 case ReqCompTimeInvalid:19488 case ReqCompTimeInvalid:
19474 return ira->codegen->invalid_inst_gen;19489 return ira->codegen->invalid_inst_gen;
19475 case ReqCompTimeNo:19490 case ReqCompTimeNo:
...@@ -19504,7 +19519,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19504,7 +19519,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19504 }19519 }
1950519520
19506 IrInstGen *casted_new_stack = analyze_casted_new_stack(ira, source_instr, new_stack,19521 IrInstGen *casted_new_stack = analyze_casted_new_stack(ira, source_instr, new_stack,
19507 is_async_call_builtin, impl_fn);19522 new_stack_src, is_async_call_builtin, impl_fn);
19508 if (casted_new_stack != nullptr && type_is_invalid(casted_new_stack->value->type))19523 if (casted_new_stack != nullptr && type_is_invalid(casted_new_stack->value->type))
19509 return ira->codegen->invalid_inst_gen;19524 return ira->codegen->invalid_inst_gen;
1951019525
...@@ -19519,7 +19534,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19519,7 +19534,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19519 IrInstGen *result_loc;19534 IrInstGen *result_loc;
19520 if (handle_is_ptr(impl_fn_type_id->return_type)) {19535 if (handle_is_ptr(impl_fn_type_id->return_type)) {
19521 result_loc = ir_resolve_result(ira, source_instr, call_result_loc,19536 result_loc = ir_resolve_result(ira, source_instr, call_result_loc,
19522 impl_fn_type_id->return_type, nullptr, true, true, false);19537 impl_fn_type_id->return_type, nullptr, true, false);
19523 if (result_loc != nullptr) {19538 if (result_loc != nullptr) {
19524 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {19539 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
19525 return result_loc;19540 return result_loc;
...@@ -19589,7 +19604,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19589,7 +19604,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19589 return ira->codegen->invalid_inst_gen;19604 return ira->codegen->invalid_inst_gen;
19590 }19605 }
1959119606
19592 IrInstGen *casted_arg = ir_implicit_cast(ira, first_arg, param_type);19607 IrInstGen *casted_arg = ir_implicit_cast2(ira, first_arg_ptr_src, first_arg, param_type);
19593 if (type_is_invalid(casted_arg->value->type))19608 if (type_is_invalid(casted_arg->value->type))
19594 return ira->codegen->invalid_inst_gen;19609 return ira->codegen->invalid_inst_gen;
1959519610
...@@ -19629,7 +19644,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19629,7 +19644,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19629 return ira->codegen->invalid_inst_gen;19644 return ira->codegen->invalid_inst_gen;
19630 }19645 }
1963119646
19632 IrInstGen *casted_new_stack = analyze_casted_new_stack(ira, source_instr, new_stack,19647 IrInstGen *casted_new_stack = analyze_casted_new_stack(ira, source_instr, new_stack, new_stack_src,
19633 is_async_call_builtin, fn_entry);19648 is_async_call_builtin, fn_entry);
19634 if (casted_new_stack != nullptr && type_is_invalid(casted_new_stack->value->type))19649 if (casted_new_stack != nullptr && type_is_invalid(casted_new_stack->value->type))
19635 return ira->codegen->invalid_inst_gen;19650 return ira->codegen->invalid_inst_gen;
...@@ -19651,7 +19666,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19651,7 +19666,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19651 IrInstGen *result_loc;19666 IrInstGen *result_loc;
19652 if (handle_is_ptr(return_type)) {19667 if (handle_is_ptr(return_type)) {
19653 result_loc = ir_resolve_result(ira, source_instr, call_result_loc,19668 result_loc = ir_resolve_result(ira, source_instr, call_result_loc,
19654 return_type, nullptr, true, true, false);19669 return_type, nullptr, true, false);
19655 if (result_loc != nullptr) {19670 if (result_loc != nullptr) {
19656 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {19671 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
19657 return result_loc;19672 return result_loc;
...@@ -19685,13 +19700,15 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19685,13 +19700,15 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1968519700
19686static IrInstGen *ir_analyze_fn_call_src(IrAnalyze *ira, IrInstSrcCall *call_instruction,19701static IrInstGen *ir_analyze_fn_call_src(IrAnalyze *ira, IrInstSrcCall *call_instruction,
19687 ZigFn *fn_entry, ZigType *fn_type, IrInstGen *fn_ref,19702 ZigFn *fn_entry, ZigType *fn_type, IrInstGen *fn_ref,
19688 IrInstGen *first_arg_ptr, CallModifier modifier)19703 IrInstGen *first_arg_ptr, IrInst *first_arg_ptr_src, CallModifier modifier)
19689{19704{
19690 IrInstGen *new_stack = nullptr;19705 IrInstGen *new_stack = nullptr;
19706 IrInst *new_stack_src = nullptr;
19691 if (call_instruction->new_stack) {19707 if (call_instruction->new_stack) {
19692 new_stack = call_instruction->new_stack->child;19708 new_stack = call_instruction->new_stack->child;
19693 if (type_is_invalid(new_stack->value->type))19709 if (type_is_invalid(new_stack->value->type))
19694 return ira->codegen->invalid_inst_gen;19710 return ira->codegen->invalid_inst_gen;
19711 new_stack_src = &call_instruction->new_stack->base;
19695 }19712 }
19696 IrInstGen **args_ptr = allocate<IrInstGen *>(call_instruction->arg_count, "IrInstGen *");19713 IrInstGen **args_ptr = allocate<IrInstGen *>(call_instruction->arg_count, "IrInstGen *");
19697 for (size_t i = 0; i < call_instruction->arg_count; i += 1) {19714 for (size_t i = 0; i < call_instruction->arg_count; i += 1) {
...@@ -19706,8 +19723,9 @@ static IrInstGen *ir_analyze_fn_call_src(IrAnalyze *ira, IrInstSrcCall *call_ins...@@ -19706,8 +19723,9 @@ static IrInstGen *ir_analyze_fn_call_src(IrAnalyze *ira, IrInstSrcCall *call_ins
19706 return ira->codegen->invalid_inst_gen;19723 return ira->codegen->invalid_inst_gen;
19707 }19724 }
19708 IrInstGen *result = ir_analyze_fn_call(ira, &call_instruction->base.base, fn_entry, fn_type, fn_ref,19725 IrInstGen *result = ir_analyze_fn_call(ira, &call_instruction->base.base, fn_entry, fn_type, fn_ref,
19709 first_arg_ptr, modifier, new_stack, call_instruction->is_async_call_builtin,19726 first_arg_ptr, first_arg_ptr_src, modifier, new_stack, new_stack_src,
19710 args_ptr, call_instruction->arg_count, ret_ptr, call_instruction->result_loc);19727 call_instruction->is_async_call_builtin, args_ptr, call_instruction->arg_count, ret_ptr,
19728 call_instruction->result_loc);
19711 deallocate(args_ptr, call_instruction->arg_count, "IrInstGen *");19729 deallocate(args_ptr, call_instruction->arg_count, "IrInstGen *");
19712 return result;19730 return result;
19713}19731}
...@@ -19758,12 +19776,14 @@ static IrInstGen *ir_analyze_call_extra(IrAnalyze *ira, IrInst* source_instr,...@@ -19758,12 +19776,14 @@ static IrInstGen *ir_analyze_call_extra(IrAnalyze *ira, IrInst* source_instr,
19758 }19776 }
1975919777
19760 IrInstGen *first_arg_ptr = nullptr;19778 IrInstGen *first_arg_ptr = nullptr;
19779 IrInst *first_arg_ptr_src = nullptr;
19761 ZigFn *fn = nullptr;19780 ZigFn *fn = nullptr;
19762 if (instr_is_comptime(fn_ref)) {19781 if (instr_is_comptime(fn_ref)) {
19763 if (fn_ref->value->type->id == ZigTypeIdBoundFn) {19782 if (fn_ref->value->type->id == ZigTypeIdBoundFn) {
19764 assert(fn_ref->value->special == ConstValSpecialStatic);19783 assert(fn_ref->value->special == ConstValSpecialStatic);
19765 fn = fn_ref->value->data.x_bound_fn.fn;19784 fn = fn_ref->value->data.x_bound_fn.fn;
19766 first_arg_ptr = fn_ref->value->data.x_bound_fn.first_arg;19785 first_arg_ptr = fn_ref->value->data.x_bound_fn.first_arg;
19786 first_arg_ptr_src = fn_ref->value->data.x_bound_fn.first_arg_src;
19767 if (type_is_invalid(first_arg_ptr->value->type))19787 if (type_is_invalid(first_arg_ptr->value->type))
19768 return ira->codegen->invalid_inst_gen;19788 return ira->codegen->invalid_inst_gen;
19769 } else {19789 } else {
...@@ -19805,8 +19825,8 @@ static IrInstGen *ir_analyze_call_extra(IrAnalyze *ira, IrInst* source_instr,...@@ -19805,8 +19825,8 @@ static IrInstGen *ir_analyze_call_extra(IrAnalyze *ira, IrInst* source_instr,
19805 return ira->codegen->invalid_inst_gen;19825 return ira->codegen->invalid_inst_gen;
19806 }19826 }
1980719827
19808 return ir_analyze_fn_call(ira, source_instr, fn, fn_type, fn_ref, first_arg_ptr,19828 return ir_analyze_fn_call(ira, source_instr, fn, fn_type, fn_ref, first_arg_ptr, first_arg_ptr_src,
19809 modifier, stack, false, args_ptr, args_len, nullptr, result_loc);19829 modifier, stack, &stack->base, false, args_ptr, args_len, nullptr, result_loc);
19810}19830}
1981119831
19812static IrInstGen *ir_analyze_instruction_call_extra(IrAnalyze *ira, IrInstSrcCallExtra *instruction) {19832static IrInstGen *ir_analyze_instruction_call_extra(IrAnalyze *ira, IrInstSrcCallExtra *instruction) {
...@@ -19881,14 +19901,15 @@ static IrInstGen *ir_analyze_instruction_call(IrAnalyze *ira, IrInstSrcCall *cal...@@ -19881,14 +19901,15 @@ static IrInstGen *ir_analyze_instruction_call(IrAnalyze *ira, IrInstSrcCall *cal
19881 ZigType *fn_type = fn_table_entry ? fn_table_entry->type_entry : fn_ref->value->type;19901 ZigType *fn_type = fn_table_entry ? fn_table_entry->type_entry : fn_ref->value->type;
19882 CallModifier modifier = is_comptime ? CallModifierCompileTime : call_instruction->modifier;19902 CallModifier modifier = is_comptime ? CallModifierCompileTime : call_instruction->modifier;
19883 return ir_analyze_fn_call_src(ira, call_instruction, fn_table_entry, fn_type,19903 return ir_analyze_fn_call_src(ira, call_instruction, fn_table_entry, fn_type,
19884 fn_ref, nullptr, modifier);19904 fn_ref, nullptr, nullptr, modifier);
19885 } else if (fn_ref->value->type->id == ZigTypeIdBoundFn) {19905 } else if (fn_ref->value->type->id == ZigTypeIdBoundFn) {
19886 assert(fn_ref->value->special == ConstValSpecialStatic);19906 assert(fn_ref->value->special == ConstValSpecialStatic);
19887 ZigFn *fn_table_entry = fn_ref->value->data.x_bound_fn.fn;19907 ZigFn *fn_table_entry = fn_ref->value->data.x_bound_fn.fn;
19888 IrInstGen *first_arg_ptr = fn_ref->value->data.x_bound_fn.first_arg;19908 IrInstGen *first_arg_ptr = fn_ref->value->data.x_bound_fn.first_arg;
19909 IrInst *first_arg_ptr_src = fn_ref->value->data.x_bound_fn.first_arg_src;
19889 CallModifier modifier = is_comptime ? CallModifierCompileTime : call_instruction->modifier;19910 CallModifier modifier = is_comptime ? CallModifierCompileTime : call_instruction->modifier;
19890 return ir_analyze_fn_call_src(ira, call_instruction, fn_table_entry, fn_table_entry->type_entry,19911 return ir_analyze_fn_call_src(ira, call_instruction, fn_table_entry, fn_table_entry->type_entry,
19891 fn_ref, first_arg_ptr, modifier);19912 fn_ref, first_arg_ptr, first_arg_ptr_src, modifier);
19892 } else {19913 } else {
19893 ir_add_error(ira, &fn_ref->base,19914 ir_add_error(ira, &fn_ref->base,
19894 buf_sprintf("type '%s' not a function", buf_ptr(&fn_ref->value->type->name)));19915 buf_sprintf("type '%s' not a function", buf_ptr(&fn_ref->value->type->name)));
...@@ -19898,7 +19919,7 @@ static IrInstGen *ir_analyze_instruction_call(IrAnalyze *ira, IrInstSrcCall *cal...@@ -19898,7 +19919,7 @@ static IrInstGen *ir_analyze_instruction_call(IrAnalyze *ira, IrInstSrcCall *cal
1989819919
19899 if (fn_ref->value->type->id == ZigTypeIdFn) {19920 if (fn_ref->value->type->id == ZigTypeIdFn) {
19900 return ir_analyze_fn_call_src(ira, call_instruction, nullptr, fn_ref->value->type,19921 return ir_analyze_fn_call_src(ira, call_instruction, nullptr, fn_ref->value->type,
19901 fn_ref, nullptr, modifier);19922 fn_ref, nullptr, nullptr, modifier);
19902 } else {19923 } else {
19903 ir_add_error(ira, &fn_ref->base,19924 ir_add_error(ira, &fn_ref->base,
19904 buf_sprintf("type '%s' not a function", buf_ptr(&fn_ref->value->type->name)));19925 buf_sprintf("type '%s' not a function", buf_ptr(&fn_ref->value->type->name)));
...@@ -20324,7 +20345,7 @@ static IrInstGen *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstSrcPhi *phi_i...@@ -20324,7 +20345,7 @@ static IrInstGen *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstSrcPhi *phi_i
2032420345
20325 // In case resolving the parent activates a suspend, do it now20346 // In case resolving the parent activates a suspend, do it now
20326 IrInstGen *parent_result_loc = ir_resolve_result(ira, &phi_instruction->base.base, peer_parent->parent,20347 IrInstGen *parent_result_loc = ir_resolve_result(ira, &phi_instruction->base.base, peer_parent->parent,
20327 peer_parent->resolved_type, nullptr, false, false, true);20348 peer_parent->resolved_type, nullptr, false, true);
20328 if (parent_result_loc != nullptr &&20349 if (parent_result_loc != nullptr &&
20329 (type_is_invalid(parent_result_loc->value->type) || parent_result_loc->value->type->id == ZigTypeIdUnreachable))20350 (type_is_invalid(parent_result_loc->value->type) || parent_result_loc->value->type->id == ZigTypeIdUnreachable))
20330 {20351 {
...@@ -20713,10 +20734,12 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP...@@ -20713,10 +20734,12 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
20713 return_type = adjust_ptr_align(ira->codegen, return_type, chosen_align);20734 return_type = adjust_ptr_align(ira->codegen, return_type, chosen_align);
20714 }20735 }
2071520736
20737 // TODO The `array_type->id == ZigTypeIdArray` exception here should not be an exception;
20738 // the `orig_array_ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar` clause should be omitted completely.
20739 // However there are bugs to fix before this improvement can be made.
20716 if (orig_array_ptr_val->special != ConstValSpecialRuntime &&20740 if (orig_array_ptr_val->special != ConstValSpecialRuntime &&
20717 orig_array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&20741 orig_array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&
20718 (orig_array_ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar ||20742 (orig_array_ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar || array_type->id == ZigTypeIdArray))
20719 array_type->id == ZigTypeIdArray))
20720 {20743 {
20721 ZigValue *array_ptr_val = const_ptr_pointee(ira, ira->codegen, orig_array_ptr_val,20744 ZigValue *array_ptr_val = const_ptr_pointee(ira, ira->codegen, orig_array_ptr_val,
20722 elem_ptr_instruction->base.base.source_node);20745 elem_ptr_instruction->base.base.source_node);
...@@ -20781,6 +20804,11 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP...@@ -20781,6 +20804,11 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
20781 (array_type->id != ZigTypeIdPointer ||20804 (array_type->id != ZigTypeIdPointer ||
20782 array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr))20805 array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr))
20783 {20806 {
20807 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec,
20808 elem_ptr_instruction->base.base.source_node, array_ptr_val, UndefOk)))
20809 {
20810 return ira->codegen->invalid_inst_gen;
20811 }
20784 if (array_type->id == ZigTypeIdPointer) {20812 if (array_type->id == ZigTypeIdPointer) {
20785 IrInstGen *result = ir_const(ira, &elem_ptr_instruction->base.base, return_type);20813 IrInstGen *result = ir_const(ira, &elem_ptr_instruction->base.base, return_type);
20786 ZigValue *out_val = result->value;20814 ZigValue *out_val = result->value;
...@@ -20989,7 +21017,7 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP...@@ -20989,7 +21017,7 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
2098921017
20990static IrInstGen *ir_analyze_container_member_access_inner(IrAnalyze *ira,21018static IrInstGen *ir_analyze_container_member_access_inner(IrAnalyze *ira,
20991 ZigType *bare_struct_type, Buf *field_name, IrInst* source_instr,21019 ZigType *bare_struct_type, Buf *field_name, IrInst* source_instr,
20992 IrInstGen *container_ptr, ZigType *container_type)21020 IrInstGen *container_ptr, IrInst *container_ptr_src, ZigType *container_type)
20993{21021{
20994 if (!is_slice(bare_struct_type)) {21022 if (!is_slice(bare_struct_type)) {
20995 ScopeDecls *container_scope = get_container_scope(bare_struct_type);21023 ScopeDecls *container_scope = get_container_scope(bare_struct_type);
...@@ -21010,7 +21038,8 @@ static IrInstGen *ir_analyze_container_member_access_inner(IrAnalyze *ira,...@@ -21010,7 +21038,8 @@ static IrInstGen *ir_analyze_container_member_access_inner(IrAnalyze *ira,
21010 if (type_is_invalid(fn_entry->type_entry))21038 if (type_is_invalid(fn_entry->type_entry))
21011 return ira->codegen->invalid_inst_gen;21039 return ira->codegen->invalid_inst_gen;
2101221040
21013 IrInstGen *bound_fn_value = ir_const_bound_fn(ira, source_instr, fn_entry, container_ptr);21041 IrInstGen *bound_fn_value = ir_const_bound_fn(ira, source_instr, fn_entry, container_ptr,
21042 container_ptr_src);
21014 return ir_get_ref(ira, source_instr, bound_fn_value, true, false);21043 return ir_get_ref(ira, source_instr, bound_fn_value, true, false);
21015 } else if (tld->id == TldIdVar) {21044 } else if (tld->id == TldIdVar) {
21016 resolve_top_level_decl(ira->codegen, tld, source_instr->source_node, false);21045 resolve_top_level_decl(ira->codegen, tld, source_instr->source_node, false);
...@@ -21029,7 +21058,8 @@ static IrInstGen *ir_analyze_container_member_access_inner(IrAnalyze *ira,...@@ -21029,7 +21058,8 @@ static IrInstGen *ir_analyze_container_member_access_inner(IrAnalyze *ira,
21029 if (var->const_value->type->id == ZigTypeIdFn) {21058 if (var->const_value->type->id == ZigTypeIdFn) {
21030 ir_assert(var->const_value->data.x_ptr.special == ConstPtrSpecialFunction, source_instr);21059 ir_assert(var->const_value->data.x_ptr.special == ConstPtrSpecialFunction, source_instr);
21031 ZigFn *fn = var->const_value->data.x_ptr.data.fn.fn_entry;21060 ZigFn *fn = var->const_value->data.x_ptr.data.fn.fn_entry;
21032 IrInstGen *bound_fn_value = ir_const_bound_fn(ira, source_instr, fn, container_ptr);21061 IrInstGen *bound_fn_value = ir_const_bound_fn(ira, source_instr, fn, container_ptr,
21062 container_ptr_src);
21033 return ir_get_ref(ira, source_instr, bound_fn_value, true, false);21063 return ir_get_ref(ira, source_instr, bound_fn_value, true, false);
21034 }21064 }
21035 }21065 }
...@@ -21075,7 +21105,7 @@ static IrInstGen *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInst* source_ins...@@ -21075,7 +21105,7 @@ static IrInstGen *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInst* source_ins
21075 IrInstGen *elem = ir_const(ira, source_instr, field_type);21105 IrInstGen *elem = ir_const(ira, source_instr, field_type);
21076 memoize_field_init_val(ira->codegen, struct_type, field);21106 memoize_field_init_val(ira->codegen, struct_type, field);
21077 copy_const_val(elem->value, field->init_val);21107 copy_const_val(elem->value, field->init_val);
21078 return ir_get_ref(ira, source_instr, elem, true, false);21108 return ir_get_ref2(ira, source_instr, elem, field_type, true, false);
21079 }21109 }
21080 switch (type_has_one_possible_value(ira->codegen, field_type)) {21110 switch (type_has_one_possible_value(ira->codegen, field_type)) {
21081 case OnePossibleValueInvalid:21111 case OnePossibleValueInvalid:
...@@ -21193,7 +21223,8 @@ static IrInstGen *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_name,...@@ -21193,7 +21223,8 @@ static IrInstGen *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_name,
21193}21223}
2119421224
21195static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,21225static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,
21196 IrInst* source_instr, IrInstGen *container_ptr, ZigType *container_type, bool initializing)21226 IrInst* source_instr, IrInstGen *container_ptr, IrInst *container_ptr_src,
21227 ZigType *container_type, bool initializing)
21197{21228{
21198 Error err;21229 Error err;
2119921230
...@@ -21215,13 +21246,13 @@ static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name...@@ -21215,13 +21246,13 @@ static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name
21215 return ir_analyze_struct_field_ptr(ira, source_instr, field, container_ptr, bare_type, initializing);21246 return ir_analyze_struct_field_ptr(ira, source_instr, field, container_ptr, bare_type, initializing);
21216 } else {21247 } else {
21217 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,21248 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
21218 source_instr, container_ptr, container_type);21249 source_instr, container_ptr, container_ptr_src, container_type);
21219 }21250 }
21220 }21251 }
2122121252
21222 if (bare_type->id == ZigTypeIdEnum) {21253 if (bare_type->id == ZigTypeIdEnum) {
21223 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,21254 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
21224 source_instr, container_ptr, container_type);21255 source_instr, container_ptr, container_ptr_src, container_type);
21225 }21256 }
2122621257
21227 if (bare_type->id == ZigTypeIdUnion) {21258 if (bare_type->id == ZigTypeIdUnion) {
...@@ -21231,7 +21262,7 @@ static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name...@@ -21231,7 +21262,7 @@ static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name
21231 TypeUnionField *field = find_union_type_field(bare_type, field_name);21262 TypeUnionField *field = find_union_type_field(bare_type, field_name);
21232 if (field == nullptr) {21263 if (field == nullptr) {
21233 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,21264 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
21234 source_instr, container_ptr, container_type);21265 source_instr, container_ptr, container_ptr_src, container_type);
21235 }21266 }
2123621267
21237 ZigType *field_type = resolve_union_field_type(ira->codegen, field);21268 ZigType *field_type = resolve_union_field_type(ira->codegen, field);
...@@ -21425,10 +21456,14 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel...@@ -21425,10 +21456,14 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel
21425 if (container_type->id == ZigTypeIdPointer) {21456 if (container_type->id == ZigTypeIdPointer) {
21426 ZigType *bare_type = container_ref_type(container_type);21457 ZigType *bare_type = container_ref_type(container_type);
21427 IrInstGen *container_child = ir_get_deref(ira, &field_ptr_instruction->base.base, container_ptr, nullptr);21458 IrInstGen *container_child = ir_get_deref(ira, &field_ptr_instruction->base.base, container_ptr, nullptr);
21428 IrInstGen *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base.base, container_child, bare_type, field_ptr_instruction->initializing);21459 IrInstGen *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base.base,
21460 container_child, &field_ptr_instruction->container_ptr->base, bare_type,
21461 field_ptr_instruction->initializing);
21429 return result;21462 return result;
21430 } else {21463 } else {
21431 IrInstGen *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base.base, container_ptr, container_type, field_ptr_instruction->initializing);21464 IrInstGen *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base.base,
21465 container_ptr, &field_ptr_instruction->container_ptr->base, container_type,
21466 field_ptr_instruction->initializing);
21432 return result;21467 return result;
21433 }21468 }
21434 } else if (is_array_ref(container_type) && !field_ptr_instruction->initializing) {21469 } else if (is_array_ref(container_type) && !field_ptr_instruction->initializing) {
...@@ -22116,6 +22151,8 @@ static IrInstGen *ir_analyze_instruction_test_non_null(IrAnalyze *ira, IrInstSrc...@@ -22116,6 +22151,8 @@ static IrInstGen *ir_analyze_instruction_test_non_null(IrAnalyze *ira, IrInstSrc
22116static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* source_instr,22151static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* source_instr,
22117 IrInstGen *base_ptr, bool safety_check_on, bool initializing)22152 IrInstGen *base_ptr, bool safety_check_on, bool initializing)
22118{22153{
22154 Error err;
22155
22119 ZigType *type_entry = get_ptr_elem_type(ira->codegen, base_ptr);22156 ZigType *type_entry = get_ptr_elem_type(ira->codegen, base_ptr);
22120 if (type_is_invalid(type_entry))22157 if (type_is_invalid(type_entry))
22121 return ira->codegen->invalid_inst_gen;22158 return ira->codegen->invalid_inst_gen;
...@@ -22161,7 +22198,7 @@ static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* sou...@@ -22161,7 +22198,7 @@ static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* sou
2216122198
22162 if (instr_is_comptime(base_ptr)) {22199 if (instr_is_comptime(base_ptr)) {
22163 ZigValue *ptr_val = ir_resolve_const(ira, base_ptr, UndefBad);22200 ZigValue *ptr_val = ir_resolve_const(ira, base_ptr, UndefBad);
22164 if (!ptr_val)22201 if (ptr_val == nullptr)
22165 return ira->codegen->invalid_inst_gen;22202 return ira->codegen->invalid_inst_gen;
22166 if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {22203 if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
22167 ZigValue *optional_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);22204 ZigValue *optional_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
...@@ -22185,20 +22222,19 @@ static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* sou...@@ -22185,20 +22222,19 @@ static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* sou
22185 }22222 }
22186 break;22223 break;
22187 case OnePossibleValueYes: {22224 case OnePossibleValueYes: {
22188 ZigValue *pointee = create_const_vals(1);
22189 pointee->special = ConstValSpecialStatic;
22190 pointee->type = child_type;
22191 pointee->parent.id = ConstParentIdOptionalPayload;
22192 pointee->parent.data.p_optional_payload.optional_val = optional_val;
22193
22194 optional_val->special = ConstValSpecialStatic;22225 optional_val->special = ConstValSpecialStatic;
22195 optional_val->data.x_optional = pointee;22226 optional_val->data.x_optional = get_the_one_possible_value(ira->codegen, child_type);
22196 break;22227 break;
22197 }22228 }
22198 }22229 }
22199 } else if (optional_value_is_null(optional_val)) {22230 } else {
22200 ir_add_error(ira, source_instr, buf_sprintf("unable to unwrap null"));22231 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec,
22201 return ira->codegen->invalid_inst_gen;22232 source_instr->source_node, optional_val, UndefBad)))
22233 return ira->codegen->invalid_inst_gen;
22234 if (optional_value_is_null(optional_val)) {
22235 ir_add_error(ira, source_instr, buf_sprintf("unable to unwrap null"));
22236 return ira->codegen->invalid_inst_gen;
22237 }
22202 }22238 }
2220322239
22204 IrInstGen *result;22240 IrInstGen *result;
...@@ -22697,8 +22733,8 @@ static IrInstGen *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstSrcSwi...@@ -22697,8 +22733,8 @@ static IrInstGen *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstSrcSwi
22697 ref_type->data.pointer.explicit_alignment,22733 ref_type->data.pointer.explicit_alignment,
22698 ref_type->data.pointer.bit_offset_in_host, ref_type->data.pointer.host_int_bytes,22734 ref_type->data.pointer.bit_offset_in_host, ref_type->data.pointer.host_int_bytes,
22699 ref_type->data.pointer.allow_zero);22735 ref_type->data.pointer.allow_zero);
22700 return ir_analyze_ptr_cast(ira, &instruction->base.base, target_value_ptr, new_target_value_ptr_type,22736 return ir_analyze_ptr_cast(ira, &instruction->base.base, target_value_ptr,
22701 &instruction->base.base, false);22737 &instruction->target_value_ptr->base, new_target_value_ptr_type, &instruction->base.base, false);
22702 } else {22738 } else {
22703 ir_add_error(ira, &instruction->base.base,22739 ir_add_error(ira, &instruction->base.base,
22704 buf_sprintf("switch on type '%s' provides no expression parameter", buf_ptr(&target_type->name)));22740 buf_sprintf("switch on type '%s' provides no expression parameter", buf_ptr(&target_type->name)));
...@@ -22778,8 +22814,8 @@ static IrInstGen *ir_analyze_instruction_switch_else_var(IrAnalyze *ira,...@@ -22778,8 +22814,8 @@ static IrInstGen *ir_analyze_instruction_switch_else_var(IrAnalyze *ira,
22778 ref_type->data.pointer.explicit_alignment,22814 ref_type->data.pointer.explicit_alignment,
22779 ref_type->data.pointer.bit_offset_in_host, ref_type->data.pointer.host_int_bytes,22815 ref_type->data.pointer.bit_offset_in_host, ref_type->data.pointer.host_int_bytes,
22780 ref_type->data.pointer.allow_zero);22816 ref_type->data.pointer.allow_zero);
22781 return ir_analyze_ptr_cast(ira, &instruction->base.base, target_value_ptr, new_target_value_ptr_type,22817 return ir_analyze_ptr_cast(ira, &instruction->base.base, target_value_ptr,
22782 &instruction->base.base, false);22818 &instruction->target_value_ptr->base, new_target_value_ptr_type, &instruction->base.base, false);
22783 }22819 }
2278422820
22785 return target_value_ptr;22821 return target_value_ptr;
...@@ -24775,11 +24811,18 @@ static IrInstGen *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstSrcCImpo...@@ -24775,11 +24811,18 @@ static IrInstGen *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstSrcCImpo
2477524811
24776 // Execute the C import block like an inline function24812 // Execute the C import block like an inline function
24777 ZigType *void_type = ira->codegen->builtin_types.entry_void;24813 ZigType *void_type = ira->codegen->builtin_types.entry_void;
24778 ZigValue *cimport_result = ir_eval_const_value(ira->codegen, &cimport_scope->base, block_node, void_type,24814 ZigValue *cimport_result;
24815 ZigValue *result_ptr;
24816 create_result_ptr(ira->codegen, void_type, &cimport_result, &result_ptr);
24817 if ((err = ir_eval_const_value(ira->codegen, &cimport_scope->base, block_node, result_ptr,
24779 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr,24818 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr,
24780 &cimport_scope->buf, block_node, nullptr, nullptr, nullptr, UndefBad);24819 &cimport_scope->buf, block_node, nullptr, nullptr, nullptr, UndefBad)))
24820 {
24821 return ira->codegen->invalid_inst_gen;
24822 }
24781 if (type_is_invalid(cimport_result->type))24823 if (type_is_invalid(cimport_result->type))
24782 return ira->codegen->invalid_inst_gen;24824 return ira->codegen->invalid_inst_gen;
24825 destroy(result_ptr, "ZigValue");
2478324826
24784 ZigPackage *cur_scope_pkg = scope_package(instruction->base.base.scope);24827 ZigPackage *cur_scope_pkg = scope_package(instruction->base.base.scope);
24785 Buf *namespace_name = buf_sprintf("%s.cimport:%" ZIG_PRI_usize ":%" ZIG_PRI_usize,24828 Buf *namespace_name = buf_sprintf("%s.cimport:%" ZIG_PRI_usize ":%" ZIG_PRI_usize,
...@@ -25068,7 +25111,7 @@ static IrInstGen *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstSrcCmpxch...@@ -25068,7 +25111,7 @@ static IrInstGen *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstSrcCmpxch
2506825111
25069 // TODO let this be volatile25112 // TODO let this be volatile
25070 ZigType *ptr_type = get_pointer_to_type(ira->codegen, operand_type, false);25113 ZigType *ptr_type = get_pointer_to_type(ira->codegen, operand_type, false);
25071 IrInstGen *casted_ptr = ir_implicit_cast(ira, ptr, ptr_type);25114 IrInstGen *casted_ptr = ir_implicit_cast2(ira, &instruction->ptr->base, ptr, ptr_type);
25072 if (type_is_invalid(casted_ptr->value->type))25115 if (type_is_invalid(casted_ptr->value->type))
25073 return ira->codegen->invalid_inst_gen;25116 return ira->codegen->invalid_inst_gen;
2507425117
...@@ -25096,11 +25139,11 @@ static IrInstGen *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstSrcCmpxch...@@ -25096,11 +25139,11 @@ static IrInstGen *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstSrcCmpxch
25096 if (!ir_resolve_atomic_order(ira, failure_order_value, &failure_order))25139 if (!ir_resolve_atomic_order(ira, failure_order_value, &failure_order))
25097 return ira->codegen->invalid_inst_gen;25140 return ira->codegen->invalid_inst_gen;
2509825141
25099 IrInstGen *casted_cmp_value = ir_implicit_cast(ira, cmp_value, operand_type);25142 IrInstGen *casted_cmp_value = ir_implicit_cast2(ira, &instruction->cmp_value->base, cmp_value, operand_type);
25100 if (type_is_invalid(casted_cmp_value->value->type))25143 if (type_is_invalid(casted_cmp_value->value->type))
25101 return ira->codegen->invalid_inst_gen;25144 return ira->codegen->invalid_inst_gen;
2510225145
25103 IrInstGen *casted_new_value = ir_implicit_cast(ira, new_value, operand_type);25146 IrInstGen *casted_new_value = ir_implicit_cast2(ira, &instruction->new_value->base, new_value, operand_type);
25104 if (type_is_invalid(casted_new_value->value->type))25147 if (type_is_invalid(casted_new_value->value->type))
25105 return ira->codegen->invalid_inst_gen;25148 return ira->codegen->invalid_inst_gen;
2510625149
...@@ -25134,7 +25177,7 @@ static IrInstGen *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstSrcCmpxch...@@ -25134,7 +25177,7 @@ static IrInstGen *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstSrcCmpxch
25134 IrInstGen *result_loc;25177 IrInstGen *result_loc;
25135 if (handle_is_ptr(result_type)) {25178 if (handle_is_ptr(result_type)) {
25136 result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,25179 result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
25137 result_type, nullptr, true, false, true);25180 result_type, nullptr, true, true);
25138 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {25181 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
25139 return result_loc;25182 return result_loc;
25140 }25183 }
...@@ -25191,7 +25234,7 @@ static IrInstGen *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstSrcTrunc...@@ -25191,7 +25234,7 @@ static IrInstGen *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstSrcTrunc
25191 }25234 }
2519225235
25193 if (dest_type->id == ZigTypeIdComptimeInt) {25236 if (dest_type->id == ZigTypeIdComptimeInt) {
25194 return ir_implicit_cast(ira, target, dest_type);25237 return ir_implicit_cast2(ira, &instruction->target->base, target, dest_type);
25195 }25238 }
2519625239
25197 if (instr_is_comptime(target)) {25240 if (instr_is_comptime(target)) {
...@@ -25245,7 +25288,7 @@ static IrInstGen *ir_analyze_instruction_int_cast(IrAnalyze *ira, IrInstSrcIntCa...@@ -25245,7 +25288,7 @@ static IrInstGen *ir_analyze_instruction_int_cast(IrAnalyze *ira, IrInstSrcIntCa
25245 }25288 }
2524625289
25247 if (instr_is_comptime(target)) {25290 if (instr_is_comptime(target)) {
25248 return ir_implicit_cast(ira, target, dest_type);25291 return ir_implicit_cast2(ira, &instruction->target->base, target, dest_type);
25249 }25292 }
2525025293
25251 if (dest_type->id == ZigTypeIdComptimeInt) {25294 if (dest_type->id == ZigTypeIdComptimeInt) {
...@@ -25373,7 +25416,7 @@ static IrInstGen *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstSrcFro...@@ -25373,7 +25416,7 @@ static IrInstGen *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstSrcFro
25373 src_ptr_align, 0, 0, false);25416 src_ptr_align, 0, 0, false);
25374 ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);25417 ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);
2537525418
25376 IrInstGen *casted_value = ir_implicit_cast(ira, target, u8_slice);25419 IrInstGen *casted_value = ir_implicit_cast2(ira, &instruction->target->base, target, u8_slice);
25377 if (type_is_invalid(casted_value->value->type))25420 if (type_is_invalid(casted_value->value->type))
25378 return ira->codegen->invalid_inst_gen;25421 return ira->codegen->invalid_inst_gen;
2537925422
...@@ -25393,7 +25436,7 @@ static IrInstGen *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstSrcFro...@@ -25393,7 +25436,7 @@ static IrInstGen *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstSrcFro
25393 }25436 }
2539425437
25395 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,25438 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
25396 dest_slice_type, nullptr, true, false, true);25439 dest_slice_type, nullptr, true, true);
25397 if (result_loc != nullptr && (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable)) {25440 if (result_loc != nullptr && (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable)) {
25398 return result_loc;25441 return result_loc;
25399 }25442 }
...@@ -25478,7 +25521,7 @@ static IrInstGen *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstSrcToByt...@@ -25478,7 +25521,7 @@ static IrInstGen *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstSrcToByt
25478 }25521 }
2547925522
25480 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,25523 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
25481 dest_slice_type, nullptr, true, false, true);25524 dest_slice_type, nullptr, true, true);
25482 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {25525 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
25483 return result_loc;25526 return result_loc;
25484 }25527 }
...@@ -26508,7 +26551,7 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i...@@ -26508,7 +26551,7 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
26508 }26551 }
2650926552
26510 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,26553 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
26511 return_type, nullptr, true, false, true);26554 return_type, nullptr, true, true);
26512 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {26555 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
26513 return result_loc;26556 return result_loc;
26514 }26557 }
...@@ -27662,7 +27705,7 @@ static IrInstGen *ir_align_cast(IrAnalyze *ira, IrInstGen *target, uint32_t alig...@@ -27662,7 +27705,7 @@ static IrInstGen *ir_align_cast(IrAnalyze *ira, IrInstGen *target, uint32_t alig
27662}27705}
2766327706
27664static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrInstGen *ptr,27707static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrInstGen *ptr,
27665 ZigType *dest_type, IrInst *dest_type_src, bool safety_check_on)27708 IrInst *ptr_src, ZigType *dest_type, IrInst *dest_type_src, bool safety_check_on)
27666{27709{
27667 Error err;27710 Error err;
2766827711
...@@ -27678,7 +27721,7 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn...@@ -27678,7 +27721,7 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
2767827721
27679 ZigType *src_ptr_type = get_src_ptr_type(src_type);27722 ZigType *src_ptr_type = get_src_ptr_type(src_type);
27680 if (src_ptr_type == nullptr) {27723 if (src_ptr_type == nullptr) {
27681 ir_add_error(ira, &ptr->base, buf_sprintf("expected pointer, found '%s'", buf_ptr(&src_type->name)));27724 ir_add_error(ira, ptr_src, buf_sprintf("expected pointer, found '%s'", buf_ptr(&src_type->name)));
27682 return ira->codegen->invalid_inst_gen;27725 return ira->codegen->invalid_inst_gen;
27683 }27726 }
2768427727
...@@ -27707,11 +27750,11 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn...@@ -27707,11 +27750,11 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
27707 if ((err = type_resolve(ira->codegen, src_type, ResolveStatusZeroBitsKnown)))27750 if ((err = type_resolve(ira->codegen, src_type, ResolveStatusZeroBitsKnown)))
27708 return ira->codegen->invalid_inst_gen;27751 return ira->codegen->invalid_inst_gen;
2770927752
27710 if (type_has_bits(dest_type) && !type_has_bits(src_type)) {27753 if (type_has_bits(dest_type) && !type_has_bits(src_type) && safety_check_on) {
27711 ErrorMsg *msg = ir_add_error(ira, source_instr,27754 ErrorMsg *msg = ir_add_error(ira, source_instr,
27712 buf_sprintf("'%s' and '%s' do not have the same in-memory representation",27755 buf_sprintf("'%s' and '%s' do not have the same in-memory representation",
27713 buf_ptr(&src_type->name), buf_ptr(&dest_type->name)));27756 buf_ptr(&src_type->name), buf_ptr(&dest_type->name)));
27714 add_error_note(ira->codegen, msg, ptr->base.source_node,27757 add_error_note(ira->codegen, msg, ptr_src->source_node,
27715 buf_sprintf("'%s' has no in-memory bits", buf_ptr(&src_type->name)));27758 buf_sprintf("'%s' has no in-memory bits", buf_ptr(&src_type->name)));
27716 add_error_note(ira->codegen, msg, dest_type_src->source_node,27759 add_error_note(ira->codegen, msg, dest_type_src->source_node,
27717 buf_sprintf("'%s' has in-memory bits", buf_ptr(&dest_type->name)));27760 buf_sprintf("'%s' has in-memory bits", buf_ptr(&dest_type->name)));
...@@ -27722,7 +27765,7 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn...@@ -27722,7 +27765,7 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
27722 bool dest_allows_addr_zero = ptr_allows_addr_zero(dest_type);27765 bool dest_allows_addr_zero = ptr_allows_addr_zero(dest_type);
27723 UndefAllowed is_undef_allowed = dest_allows_addr_zero ? UndefOk : UndefBad;27766 UndefAllowed is_undef_allowed = dest_allows_addr_zero ? UndefOk : UndefBad;
27724 ZigValue *val = ir_resolve_const(ira, ptr, is_undef_allowed);27767 ZigValue *val = ir_resolve_const(ira, ptr, is_undef_allowed);
27725 if (!val)27768 if (val == nullptr)
27726 return ira->codegen->invalid_inst_gen;27769 return ira->codegen->invalid_inst_gen;
2772727770
27728 if (value_is_comptime(val) && val->special != ConstValSpecialUndef) {27771 if (value_is_comptime(val) && val->special != ConstValSpecialUndef) {
...@@ -27737,15 +27780,31 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn...@@ -27737,15 +27780,31 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
27737 }27780 }
2773827781
27739 IrInstGen *result;27782 IrInstGen *result;
27740 if (ptr->value->data.x_ptr.mut == ConstPtrMutInfer) {27783 if (val->data.x_ptr.mut == ConstPtrMutInfer) {
27741 result = ir_build_ptr_cast_gen(ira, source_instr, dest_type, ptr, safety_check_on);27784 result = ir_build_ptr_cast_gen(ira, source_instr, dest_type, ptr, safety_check_on);
27742
27743 if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusZeroBitsKnown)))
27744 return ira->codegen->invalid_inst_gen;
27745 } else {27785 } else {
27746 result = ir_const(ira, source_instr, dest_type);27786 result = ir_const(ira, source_instr, dest_type);
27747 }27787 }
27748 copy_const_val(result->value, val);27788 InferredStructField *isf = (val->type->id == ZigTypeIdPointer) ?
27789 val->type->data.pointer.inferred_struct_field : nullptr;
27790 if (isf == nullptr) {
27791 copy_const_val(result->value, val);
27792 } else {
27793 // The destination value should have x_ptr struct pointing to underlying struct value
27794 result->value->data.x_ptr.mut = val->data.x_ptr.mut;
27795 TypeStructField *field = find_struct_type_field(isf->inferred_struct_type, isf->field_name);
27796 assert(field != nullptr);
27797 if (field->is_comptime) {
27798 result->value->data.x_ptr.special = ConstPtrSpecialRef;
27799 result->value->data.x_ptr.data.ref.pointee = field->init_val;
27800 } else {
27801 assert(val->data.x_ptr.special == ConstPtrSpecialRef);
27802 result->value->data.x_ptr.special = ConstPtrSpecialBaseStruct;
27803 result->value->data.x_ptr.data.base_struct.struct_val = val->data.x_ptr.data.ref.pointee;
27804 result->value->data.x_ptr.data.base_struct.field_index = field->src_index;
27805 }
27806 result->value->special = ConstValSpecialStatic;
27807 }
27749 result->value->type = dest_type;27808 result->value->type = dest_type;
2775027809
27751 // Keep the bigger alignment, it can only help-27810 // Keep the bigger alignment, it can only help-
...@@ -27759,7 +27818,7 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn...@@ -27759,7 +27818,7 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
2775927818
27760 if (dest_align_bytes > src_align_bytes) {27819 if (dest_align_bytes > src_align_bytes) {
27761 ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("cast increases pointer alignment"));27820 ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("cast increases pointer alignment"));
27762 add_error_note(ira->codegen, msg, ptr->base.source_node,27821 add_error_note(ira->codegen, msg, ptr_src->source_node,
27763 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&src_type->name), src_align_bytes));27822 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&src_type->name), src_align_bytes));
27764 add_error_note(ira->codegen, msg, dest_type_src->source_node,27823 add_error_note(ira->codegen, msg, dest_type_src->source_node,
27765 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&dest_type->name), dest_align_bytes));27824 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&dest_type->name), dest_align_bytes));
...@@ -27792,8 +27851,8 @@ static IrInstGen *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstSrcPtrCa...@@ -27792,8 +27851,8 @@ static IrInstGen *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstSrcPtrCa
27792 if (type_is_invalid(src_type))27851 if (type_is_invalid(src_type))
27793 return ira->codegen->invalid_inst_gen;27852 return ira->codegen->invalid_inst_gen;
2779427853
27795 return ir_analyze_ptr_cast(ira, &instruction->base.base, ptr, dest_type, &dest_type_value->base,27854 return ir_analyze_ptr_cast(ira, &instruction->base.base, ptr, &instruction->ptr->base,
27796 instruction->safety_check_on);27855 dest_type, &dest_type_value->base, instruction->safety_check_on);
27797}27856}
2779827857
27799static void buf_write_value_bytes_array(CodeGen *codegen, uint8_t *buf, ZigValue *val, size_t len) {27858static void buf_write_value_bytes_array(CodeGen *codegen, uint8_t *buf, ZigValue *val, size_t len) {
...@@ -29206,7 +29265,7 @@ static IrInstGen *ir_analyze_instruction_end_expr(IrAnalyze *ira, IrInstSrcEndEx...@@ -29206,7 +29265,7 @@ static IrInstGen *ir_analyze_instruction_end_expr(IrAnalyze *ira, IrInstSrcEndEx
2920629265
29207 bool was_written = instruction->result_loc->written;29266 bool was_written = instruction->result_loc->written;
29208 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,29267 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
29209 value->value->type, value, false, false, true);29268 value->value->type, value, false, true);
29210 if (result_loc != nullptr) {29269 if (result_loc != nullptr) {
29211 if (type_is_invalid(result_loc->value->type))29270 if (type_is_invalid(result_loc->value->type))
29212 return ira->codegen->invalid_inst_gen;29271 return ira->codegen->invalid_inst_gen;
...@@ -29240,11 +29299,6 @@ static IrInstGen *ir_analyze_instruction_implicit_cast(IrAnalyze *ira, IrInstSrc...@@ -29240,11 +29299,6 @@ static IrInstGen *ir_analyze_instruction_implicit_cast(IrAnalyze *ira, IrInstSrc
29240 if (type_is_invalid(operand->value->type))29299 if (type_is_invalid(operand->value->type))
29241 return operand;29300 return operand;
2924229301
29243 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base,
29244 &instruction->result_loc_cast->base, operand->value->type, operand, false, false, true);
29245 if (result_loc != nullptr && (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable))
29246 return result_loc;
29247
29248 ZigType *dest_type = ir_resolve_type(ira, instruction->result_loc_cast->base.source_instruction->child);29302 ZigType *dest_type = ir_resolve_type(ira, instruction->result_loc_cast->base.source_instruction->child);
29249 if (type_is_invalid(dest_type))29303 if (type_is_invalid(dest_type))
29250 return ira->codegen->invalid_inst_gen;29304 return ira->codegen->invalid_inst_gen;
...@@ -29257,15 +29311,18 @@ static IrInstGen *ir_analyze_instruction_bit_cast_src(IrAnalyze *ira, IrInstSrcB...@@ -29257,15 +29311,18 @@ static IrInstGen *ir_analyze_instruction_bit_cast_src(IrAnalyze *ira, IrInstSrcB
29257 return operand;29311 return operand;
2925829312
29259 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base,29313 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base,
29260 &instruction->result_loc_bit_cast->base, operand->value->type, operand, false, false, true);29314 &instruction->result_loc_bit_cast->base, operand->value->type, operand, false, true);
29261 if (result_loc != nullptr && (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable))29315 if (result_loc != nullptr &&
29316 (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable))
29317 {
29262 return result_loc;29318 return result_loc;
29263
29264 if (instruction->result_loc_bit_cast->parent->gen_instruction != nullptr) {
29265 return instruction->result_loc_bit_cast->parent->gen_instruction;
29266 }29319 }
2926729320
29268 return result_loc;29321 ZigType *dest_type = ir_resolve_type(ira,
29322 instruction->result_loc_bit_cast->base.source_instruction->child);
29323 if (type_is_invalid(dest_type))
29324 return ira->codegen->invalid_inst_gen;
29325 return ir_analyze_bit_cast(ira, &instruction->base.base, operand, dest_type);
29269}29326}
2927029327
29271static IrInstGen *ir_analyze_instruction_union_init_named_field(IrAnalyze *ira,29328static IrInstGen *ir_analyze_instruction_union_init_named_field(IrAnalyze *ira,
...@@ -29392,9 +29449,12 @@ static IrInstGen *ir_analyze_instruction_await(IrAnalyze *ira, IrInstSrcAwait *i...@@ -29392,9 +29449,12 @@ static IrInstGen *ir_analyze_instruction_await(IrAnalyze *ira, IrInstSrcAwait *i
29392 IrInstGen *result_loc;29449 IrInstGen *result_loc;
29393 if (type_has_bits(result_type)) {29450 if (type_has_bits(result_type)) {
29394 result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,29451 result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
29395 result_type, nullptr, true, true, true);29452 result_type, nullptr, true, true);
29396 if (result_loc != nullptr && (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable))29453 if (result_loc != nullptr &&
29454 (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable))
29455 {
29397 return result_loc;29456 return result_loc;
29457 }
29398 } else {29458 } else {
29399 result_loc = nullptr;29459 result_loc = nullptr;
29400 }29460 }
...@@ -29421,7 +29481,7 @@ static IrInstGen *ir_analyze_instruction_resume(IrAnalyze *ira, IrInstSrcResume...@@ -29421,7 +29481,7 @@ static IrInstGen *ir_analyze_instruction_resume(IrAnalyze *ira, IrInstSrcResume
29421 }29481 }
2942229482
29423 ZigType *any_frame_type = get_any_frame_type(ira->codegen, nullptr);29483 ZigType *any_frame_type = get_any_frame_type(ira->codegen, nullptr);
29424 IrInstGen *casted_frame = ir_implicit_cast(ira, frame, any_frame_type);29484 IrInstGen *casted_frame = ir_implicit_cast2(ira, &instruction->frame->base, frame, any_frame_type);
29425 if (type_is_invalid(casted_frame->value->type))29485 if (type_is_invalid(casted_frame->value->type))
29426 return ira->codegen->invalid_inst_gen;29486 return ira->codegen->invalid_inst_gen;
2942729487
...@@ -29747,7 +29807,7 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc...@@ -29747,7 +29807,7 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc
29747// This function attempts to evaluate IR code while doing type checking and other analysis.29807// This function attempts to evaluate IR code while doing type checking and other analysis.
29748// It emits to a new IrExecutableGen which is partially evaluated IR code.29808// It emits to a new IrExecutableGen which is partially evaluated IR code.
29749ZigType *ir_analyze(CodeGen *codegen, IrExecutableSrc *old_exec, IrExecutableGen *new_exec,29809ZigType *ir_analyze(CodeGen *codegen, IrExecutableSrc *old_exec, IrExecutableGen *new_exec,
29750 ZigType *expected_type, AstNode *expected_type_source_node)29810 ZigType *expected_type, AstNode *expected_type_source_node, ZigValue *result_ptr)
29751{29811{
29752 assert(old_exec->first_err_trace_msg == nullptr);29812 assert(old_exec->first_err_trace_msg == nullptr);
29753 assert(expected_type == nullptr || !type_is_invalid(expected_type));29813 assert(expected_type == nullptr || !type_is_invalid(expected_type));
...@@ -29766,12 +29826,6 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutableSrc *old_exec, IrExecutableGen...@@ -29766,12 +29826,6 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutableSrc *old_exec, IrExecutableGen
29766 ira->new_irb.codegen = codegen;29826 ira->new_irb.codegen = codegen;
29767 ira->new_irb.exec = new_exec;29827 ira->new_irb.exec = new_exec;
2976829828
29769 ZigValue *vals = create_const_vals(ira->old_irb.exec->mem_slot_count);
29770 ira->exec_context.mem_slot_list.resize(ira->old_irb.exec->mem_slot_count);
29771 for (size_t i = 0; i < ira->exec_context.mem_slot_list.length; i += 1) {
29772 ira->exec_context.mem_slot_list.items[i] = &vals[i];
29773 }
29774
29775 IrBasicBlockSrc *old_entry_bb = ira->old_irb.exec->basic_block_list.at(0);29829 IrBasicBlockSrc *old_entry_bb = ira->old_irb.exec->basic_block_list.at(0);
29776 IrBasicBlockGen *new_entry_bb = ir_get_new_bb(ira, old_entry_bb, nullptr);29830 IrBasicBlockGen *new_entry_bb = ir_get_new_bb(ira, old_entry_bb, nullptr);
29777 ir_ref_bb_gen(new_entry_bb);29831 ir_ref_bb_gen(new_entry_bb);
...@@ -29780,6 +29834,19 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutableSrc *old_exec, IrExecutableGen...@@ -29780,6 +29834,19 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutableSrc *old_exec, IrExecutableGen
2978029834
29781 ir_start_bb(ira, old_entry_bb, nullptr);29835 ir_start_bb(ira, old_entry_bb, nullptr);
2978229836
29837 if (result_ptr != nullptr) {
29838 assert(result_ptr->type->id == ZigTypeIdPointer);
29839 IrInstGenConst *const_inst = ir_create_inst_noval<IrInstGenConst>(
29840 &ira->new_irb, new_exec->begin_scope, new_exec->source_node);
29841 const_inst->base.value = result_ptr;
29842 ira->return_ptr = &const_inst->base;
29843 } else {
29844 assert(new_exec->begin_scope != nullptr);
29845 assert(new_exec->source_node != nullptr);
29846 ira->return_ptr = ir_build_return_ptr(ira, new_exec->begin_scope, new_exec->source_node,
29847 get_pointer_to_type(codegen, expected_type, false));
29848 }
29849
29783 while (ira->old_bb_index < ira->old_irb.exec->basic_block_list.length) {29850 while (ira->old_bb_index < ira->old_irb.exec->basic_block_list.length) {
29784 IrInstSrc *old_instruction = ira->old_irb.current_basic_block->instruction_list.at(ira->instruction_index);29851 IrInstSrc *old_instruction = ira->old_irb.current_basic_block->instruction_list.at(ira->instruction_index);
2978529852
...@@ -29922,7 +29989,6 @@ bool ir_inst_gen_has_side_effects(IrInstGen *instruction) {...@@ -29922,7 +29989,6 @@ bool ir_inst_gen_has_side_effects(IrInstGen *instruction) {
29922 case IrInstGenIdReturnPtr:29989 case IrInstGenIdReturnPtr:
29923 case IrInstGenIdStructFieldPtr:29990 case IrInstGenIdStructFieldPtr:
29924 case IrInstGenIdTestNonNull:29991 case IrInstGenIdTestNonNull:
29925 case IrInstGenIdOptionalUnwrapPtr:
29926 case IrInstGenIdClz:29992 case IrInstGenIdClz:
29927 case IrInstGenIdCtz:29993 case IrInstGenIdCtz:
29928 case IrInstGenIdPopCount:29994 case IrInstGenIdPopCount:
...@@ -29979,6 +30045,8 @@ bool ir_inst_gen_has_side_effects(IrInstGen *instruction) {...@@ -29979,6 +30045,8 @@ bool ir_inst_gen_has_side_effects(IrInstGen *instruction) {
29979 return reinterpret_cast<IrInstGenUnwrapErrCode *>(instruction)->initializing;30045 return reinterpret_cast<IrInstGenUnwrapErrCode *>(instruction)->initializing;
29980 case IrInstGenIdUnionFieldPtr:30046 case IrInstGenIdUnionFieldPtr:
29981 return reinterpret_cast<IrInstGenUnionFieldPtr *>(instruction)->initializing;30047 return reinterpret_cast<IrInstGenUnionFieldPtr *>(instruction)->initializing;
30048 case IrInstGenIdOptionalUnwrapPtr:
30049 return reinterpret_cast<IrInstGenOptionalUnwrapPtr *>(instruction)->initializing;
29982 case IrInstGenIdErrWrapPayload:30050 case IrInstGenIdErrWrapPayload:
29983 return reinterpret_cast<IrInstGenErrWrapPayload *>(instruction)->result_loc != nullptr;30051 return reinterpret_cast<IrInstGenErrWrapPayload *>(instruction)->result_loc != nullptr;
29984 case IrInstGenIdErrWrapCode:30052 case IrInstGenIdErrWrapCode:
src/ir.hpp+3-3
...@@ -16,15 +16,15 @@ bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);...@@ -16,15 +16,15 @@ bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);
16IrInstGen *ir_create_alloca(CodeGen *g, Scope *scope, AstNode *source_node, ZigFn *fn,16IrInstGen *ir_create_alloca(CodeGen *g, Scope *scope, AstNode *source_node, ZigFn *fn,
17 ZigType *var_type, const char *name_hint);17 ZigType *var_type, const char *name_hint);
1818
19ZigValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,19Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
20 ZigType *expected_type, size_t *backward_branch_count, size_t *backward_branch_quota,20 ZigValue *return_ptr, size_t *backward_branch_count, size_t *backward_branch_quota,
21 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,21 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
22 IrExecutableGen *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef);22 IrExecutableGen *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef);
2323
24Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ZigValue *val);24Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ZigValue *val);
2525
26ZigType *ir_analyze(CodeGen *g, IrExecutableSrc *old_executable, IrExecutableGen *new_executable,26ZigType *ir_analyze(CodeGen *g, IrExecutableSrc *old_executable, IrExecutableGen *new_executable,
27 ZigType *expected_type, AstNode *expected_type_source_node);27 ZigType *expected_type, AstNode *expected_type_source_node, ZigValue *return_ptr);
2828
29bool ir_inst_gen_has_side_effects(IrInstGen *inst);29bool ir_inst_gen_has_side_effects(IrInstGen *inst);
30bool ir_inst_src_has_side_effects(IrInstSrc *inst);30bool ir_inst_src_has_side_effects(IrInstSrc *inst);
src/softfloat.hpp+17
...@@ -12,4 +12,21 @@ extern "C" {...@@ -12,4 +12,21 @@ extern "C" {
12#include "softfloat.h"12#include "softfloat.h"
13}13}
1414
15static inline float16_t zig_double_to_f16(double x) {
16 float64_t y;
17 static_assert(sizeof(x) == sizeof(y), "");
18 memcpy(&y, &x, sizeof(x));
19 return f64_to_f16(y);
20}
21
22
23// Return value is safe to coerce to float even when |x| is NaN or Infinity.
24static inline double zig_f16_to_double(float16_t x) {
25 float64_t y = f16_to_f64(x);
26 double z;
27 static_assert(sizeof(y) == sizeof(z), "");
28 memcpy(&z, &y, sizeof(y));
29 return z;
30}
31
15#endif32#endif
src/util.hpp+2-19
...@@ -38,6 +38,8 @@...@@ -38,6 +38,8 @@
3838
39#if defined(__MINGW32__) || defined(__MINGW64__)39#if defined(__MINGW32__) || defined(__MINGW64__)
40#define BREAKPOINT __debugbreak()40#define BREAKPOINT __debugbreak()
41#elif defined(__i386__) || defined(__x86_64__)
42#define BREAKPOINT __asm__ volatile("int $0x03");
41#elif defined(__clang__)43#elif defined(__clang__)
42#define BREAKPOINT __builtin_debugtrap()44#define BREAKPOINT __builtin_debugtrap()
43#elif defined(__GNUC__)45#elif defined(__GNUC__)
...@@ -49,8 +51,6 @@...@@ -49,8 +51,6 @@
4951
50#endif52#endif
5153
52#include "softfloat.hpp"
53
54ATTRIBUTE_COLD54ATTRIBUTE_COLD
55ATTRIBUTE_NORETURN55ATTRIBUTE_NORETURN
56ATTRIBUTE_PRINTF(1, 2)56ATTRIBUTE_PRINTF(1, 2)
...@@ -244,23 +244,6 @@ static inline uint8_t log2_u64(uint64_t x) {...@@ -244,23 +244,6 @@ static inline uint8_t log2_u64(uint64_t x) {
244 return (63 - clzll(x));244 return (63 - clzll(x));
245}245}
246246
247static inline float16_t zig_double_to_f16(double x) {
248 float64_t y;
249 static_assert(sizeof(x) == sizeof(y), "");
250 memcpy(&y, &x, sizeof(x));
251 return f64_to_f16(y);
252}
253
254
255// Return value is safe to coerce to float even when |x| is NaN or Infinity.
256static inline double zig_f16_to_double(float16_t x) {
257 float64_t y = f16_to_f64(x);
258 double z;
259 static_assert(sizeof(y) == sizeof(z), "");
260 memcpy(&z, &y, sizeof(y));
261 return z;
262}
263
264void zig_pretty_print_bytes(FILE *f, double n);247void zig_pretty_print_bytes(FILE *f, double n);
265248
266template<typename T>249template<typename T>
test/compile_errors.zig+6-7
...@@ -1658,7 +1658,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -1658,7 +1658,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
1658 cases.addTest("return invalid type from test",1658 cases.addTest("return invalid type from test",
1659 \\test "example" { return 1; }1659 \\test "example" { return 1; }
1660 , &[_][]const u8{1660 , &[_][]const u8{
1661 "tmp.zig:1:25: error: integer value 1 cannot be coerced to type 'void'",1661 "tmp.zig:1:25: error: expected type 'void', found 'comptime_int'",
1662 });1662 });
16631663
1664 cases.add("threadlocal qualifier on const",1664 cases.add("threadlocal qualifier on const",
...@@ -2487,7 +2487,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -2487,7 +2487,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
2487 \\ var rule_set = try Foo.init();2487 \\ var rule_set = try Foo.init();
2488 \\}2488 \\}
2489 , &[_][]const u8{2489 , &[_][]const u8{
2490 "tmp.zig:2:10: error: expected type 'i32', found 'type'",2490 "tmp.zig:2:19: error: expected type 'i32', found 'type'",
2491 });2491 });
24922492
2493 cases.add("slicing single-item pointer",2493 cases.add("slicing single-item pointer",
...@@ -3393,7 +3393,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -3393,7 +3393,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
3393 \\3393 \\
3394 \\fn b() void {}3394 \\fn b() void {}
3395 , &[_][]const u8{3395 , &[_][]const u8{
3396 "tmp.zig:3:6: error: unreachable code",3396 "tmp.zig:3:5: error: unreachable code",
3397 });3397 });
33983398
3399 cases.add("bad import",3399 cases.add("bad import",
...@@ -4011,8 +4011,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -4011,8 +4011,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
4011 \\4011 \\
4012 \\export fn entry() usize { return @sizeOf(@TypeOf(Foo)); }4012 \\export fn entry() usize { return @sizeOf(@TypeOf(Foo)); }
4013 , &[_][]const u8{4013 , &[_][]const u8{
4014 "tmp.zig:5:25: error: unable to evaluate constant expression",4014 "tmp.zig:5:25: error: cannot store runtime value in compile time variable",
4015 "tmp.zig:2:12: note: referenced here",4015 "tmp.zig:2:12: note: called from here",
4016 });4016 });
40174017
4018 cases.add("addition with non numbers",4018 cases.add("addition with non numbers",
...@@ -4652,7 +4652,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -4652,7 +4652,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
4652 \\fn something() anyerror!void { }4652 \\fn something() anyerror!void { }
4653 , &[_][]const u8{4653 , &[_][]const u8{
4654 "tmp.zig:2:5: error: expected type 'void', found 'anyerror'",4654 "tmp.zig:2:5: error: expected type 'void', found 'anyerror'",
4655 "tmp.zig:1:15: note: return type declared here",
4656 });4655 });
46574656
4658 cases.add("invalid pointer for var type",4657 cases.add("invalid pointer for var type",
...@@ -5743,7 +5742,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5743,7 +5742,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5743 \\ @export(entry, .{.name = "entry", .linkage = @as(u32, 1234) });5742 \\ @export(entry, .{.name = "entry", .linkage = @as(u32, 1234) });
5744 \\}5743 \\}
5745 , &[_][]const u8{5744 , &[_][]const u8{
5746 "tmp.zig:3:50: error: expected type 'std.builtin.GlobalLinkage', found 'u32'",5745 "tmp.zig:3:59: error: expected type 'std.builtin.GlobalLinkage', found 'comptime_int'",
5747 });5746 });
57485747
5749 cases.add("struct with invalid field",5748 cases.add("struct with invalid field",
test/stage1/behavior/bitcast.zig+20
...@@ -167,3 +167,23 @@ test "nested bitcast" {...@@ -167,3 +167,23 @@ test "nested bitcast" {
167 S.foo(42);167 S.foo(42);
168 comptime S.foo(42);168 comptime S.foo(42);
169}169}
170
171test "bitcast passed as tuple element" {
172 const S = struct {
173 fn foo(args: var) void {
174 comptime expect(@TypeOf(args[0]) == f32);
175 expect(args[0] == 12.34);
176 }
177 };
178 S.foo(.{@bitCast(f32, @as(u32, 0x414570A4))});
179}
180
181test "triple level result location with bitcast sandwich passed as tuple element" {
182 const S = struct {
183 fn foo(args: var) void {
184 comptime expect(@TypeOf(args[0]) == f64);
185 expect(args[0] > 12.33 and args[0] < 12.35);
186 }
187 };
188 S.foo(.{@as(f64, @bitCast(f32, @as(u32, 0x414570A4)))});
189}
test/stage1/behavior/eval.zig+13
...@@ -804,3 +804,16 @@ test "comptime assign int to optional int" {...@@ -804,3 +804,16 @@ test "comptime assign int to optional int" {
804 expectEqual(20, x.?);804 expectEqual(20, x.?);
805 }805 }
806}806}
807
808test "return 0 from function that has u0 return type" {
809 const S = struct {
810 fn foo_zero() u0 {
811 return 0;
812 }
813 };
814 comptime {
815 if (S.foo_zero() != 0) {
816 @compileError("test failed");
817 }
818 }
819}
test/stage1/behavior/if.zig+1-1
...@@ -72,7 +72,7 @@ test "const result loc, runtime if cond, else unreachable" {...@@ -72,7 +72,7 @@ test "const result loc, runtime if cond, else unreachable" {
7272
73 var t = true;73 var t = true;
74 const x = if (t) Num.Two else unreachable;74 const x = if (t) Num.Two else unreachable;
75 if (x != .Two) @compileError("bad");75 expect(x == .Two);
76}76}
7777
78test "if prongs cast to expected type instead of peer type resolution" {78test "if prongs cast to expected type instead of peer type resolution" {
test/stage1/behavior/misc.zig+13
...@@ -781,3 +781,16 @@ test "pointer to thread local array" {...@@ -781,3 +781,16 @@ test "pointer to thread local array" {
781 std.mem.copy(u8, buffer[0..], s);781 std.mem.copy(u8, buffer[0..], s);
782 std.testing.expectEqualSlices(u8, buffer[0..], s);782 std.testing.expectEqualSlices(u8, buffer[0..], s);
783}783}
784
785test "auto created variables have correct alignment" {
786 const S = struct {
787 fn foo(str: [*]const u8) u32 {
788 for (@ptrCast([*]align(1) const u32, str)[0..1]) |v| {
789 return v;
790 }
791 return 0;
792 }
793 };
794 expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
795 comptime expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
796}
test/stage1/behavior/optional.zig+22
...@@ -153,3 +153,25 @@ test "optional with void type" {...@@ -153,3 +153,25 @@ test "optional with void type" {
153 var x = Foo{ .x = null };153 var x = Foo{ .x = null };
154 expect(x.x == null);154 expect(x.x == null);
155}155}
156
157test "0-bit child type coerced to optional return ptr result location" {
158 const S = struct {
159 fn doTheTest() void {
160 var y = Foo{};
161 var z = y.thing();
162 expect(z != null);
163 }
164
165 const Foo = struct {
166 pub const Bar = struct {
167 field: *Foo,
168 };
169
170 pub fn thing(self: *Foo) ?Bar {
171 return Bar{ .field = self };
172 }
173 };
174 };
175 S.doTheTest();
176 comptime S.doTheTest();
177}
test/stage1/behavior/undefined.zig+3-2
...@@ -1,5 +1,6 @@...@@ -1,5 +1,6 @@
1const expect = @import("std").testing.expect;1const std = @import("std");
2const mem = @import("std").mem;2const expect = std.testing.expect;
3const mem = std.mem;
34
4fn initStaticArray() [10]i32 {5fn initStaticArray() [10]i32 {
5 var array: [10]i32 = undefined;6 var array: [10]i32 = undefined;