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
signaturebadge-question-mark 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 {
19781978
19791979 const all_features = self.target.getArch().allFeaturesList();
19801980 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 }
19811984 populated_cpu_features.populateDependencies(all_features);
19821985
19831986 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 {
1515 const m: u32 = 0x5bd1e995;
1616 const len = @truncate(u32, str.len);
1717 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| {
1919 var k1: u32 = v;
2020 if (builtin.endian == builtin.Endian.Big)
2121 k1 = @byteSwap(u32, k1);
......@@ -100,7 +100,7 @@ pub const Murmur2_64 = struct {
100100 const m: u64 = 0xc6a4a7935bd1e995;
101101 const len = @as(u64, str.len);
102102 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| {
104104 var k1: u64 = v;
105105 if (builtin.endian == builtin.Endian.Big)
106106 k1 = @byteSwap(u64, k1);
......@@ -180,7 +180,7 @@ pub const Murmur3_32 = struct {
180180 const c2: u32 = 0x1b873593;
181181 const len = @truncate(u32, str.len);
182182 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| {
184184 var k1: u32 = v;
185185 if (builtin.endian == builtin.Endian.Big)
186186 k1 = @byteSwap(u32, k1);
src/all_types.hpp+6-1
......@@ -317,6 +317,7 @@ struct ConstErrValue {
317317struct ConstBoundFnValue {
318318 ZigFn *fn;
319319 IrInstGen *first_arg;
320 IrInst *first_arg_src;
320321};
321322
322323struct ConstArgTuple {
......@@ -502,6 +503,9 @@ struct ZigValue {
502503 // uncomment these to find bugs. can't leave them uncommented because of a gcc-9 warning
503504 //ZigValue(const ZigValue &other) = delete; // plz zero initialize with {}
504505 //ZigValue& operator= (const ZigValue &other) = delete; // use copy_const_val
506
507 // for use in debuggers
508 void dump();
505509};
506510
507511enum ReturnKnowledge {
......@@ -1256,6 +1260,7 @@ static const uint32_t VECTOR_INDEX_RUNTIME = UINT32_MAX - 1;
12561260struct InferredStructField {
12571261 ZigType *inferred_struct_type;
12581262 Buf *field_name;
1263 bool already_resolved;
12591264};
12601265
12611266struct ZigTypePointer {
......@@ -2262,7 +2267,6 @@ struct ZigVar {
22622267 Scope *parent_scope;
22632268 Scope *child_scope;
22642269 LLVMValueRef param_value_ref;
2265 size_t mem_slot_index;
22662270 IrExecutableSrc *owner_exec;
22672271
22682272 Buf *section_name;
......@@ -2283,6 +2287,7 @@ struct ZigVar {
22832287 bool is_thread_local;
22842288 bool is_comptime_memoized;
22852289 bool is_comptime_memoized_value;
2290 bool did_the_decl_codegen;
22862291};
22872292
22882293struct ErrorTableEntry {
src/analyze.cpp+236-10
......@@ -593,9 +593,9 @@ ZigType *get_pointer_to_type_extra2(CodeGen *g, ZigType *child_type, bool is_con
593593 }
594594
595595 if (inferred_struct_field != nullptr) {
596 entry->abi_size = g->builtin_types.entry_usize->abi_size;
597 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
598 entry->abi_align = g->builtin_types.entry_usize->abi_align;
596 entry->abi_size = SIZE_MAX;
597 entry->size_in_bits = SIZE_MAX;
598 entry->abi_align = UINT32_MAX;
599599 } else if (type_is_resolved(child_type, ResolveStatusZeroBitsKnown)) {
600600 if (type_has_bits(child_type)) {
601601 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
11021102ZigValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
11031103 Buf *type_name, UndefAllowed undef)
11041104{
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
11051117 size_t backward_branch_count = 0;
11061118 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,
11081120 &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;
11101127}
11111128
11121129Error 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
38293846 variable_entry->var_type = var_type;
38303847 variable_entry->parent_scope = parent_scope;
38313848 variable_entry->shadowable = false;
3832 variable_entry->mem_slot_index = SIZE_MAX;
38333849 variable_entry->src_arg_index = SIZE_MAX;
38343850
38353851 assert(name);
......@@ -3930,7 +3946,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
39303946
39313947 // TODO more validation for types that can't be used for export/extern variables
39323948 ZigType *implicit_type = nullptr;
3933 if (explicit_type && explicit_type->id == ZigTypeIdInvalid) {
3949 if (explicit_type != nullptr && explicit_type->id == ZigTypeIdInvalid) {
39343950 implicit_type = explicit_type;
39353951 } else if (var_decl->expr) {
39363952 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) {
47184734 assert(!fn_type->data.fn.is_generic);
47194735 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 }
47214743 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);
47234745 fn->src_implicit_return_type = block_return_type;
47244746
47254747 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) {
56195641 return OnePossibleValueYes;
56205642 return type_has_one_possible_value(g, type_entry->data.array.child_type);
56215643 case ZigTypeIdStruct:
5644 // If the recursive function call asks, then we are not one possible value.
5645 type_entry->one_possible_value = OnePossibleValueNo;
56225646 for (size_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) {
56235647 TypeStructField *field = type_entry->data.structure.fields[i];
56245648 OnePossibleValue opv = (field->type_entry != nullptr) ?
......@@ -5626,6 +5650,7 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
56265650 type_val_resolve_has_one_possible_value(g, field->type_val);
56275651 switch (opv) {
56285652 case OnePossibleValueInvalid:
5653 type_entry->one_possible_value = OnePossibleValueInvalid;
56295654 return OnePossibleValueInvalid;
56305655 case OnePossibleValueNo:
56315656 return OnePossibleValueNo;
......@@ -5633,6 +5658,7 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
56335658 continue;
56345659 }
56355660 }
5661 type_entry->one_possible_value = OnePossibleValueYes;
56365662 return OnePossibleValueYes;
56375663 case ZigTypeIdErrorSet:
56385664 case ZigTypeIdEnum:
......@@ -5678,6 +5704,9 @@ ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry) {
56785704 assert(field_type != nullptr);
56795705 result->data.x_struct.fields[i] = get_the_one_possible_value(g, field_type);
56805706 }
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);
56815710 }
56825711 g->one_possible_values.put(type_entry, result);
56835712 return result;
......@@ -6452,7 +6481,21 @@ static Error resolve_pointer_zero_bits(CodeGen *g, ZigType *ty) {
64526481 }
64536482 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
64576500 bool has_bits;
64586501 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
68756918 }
68766919 case ConstArraySpecialNone: {
68776920 ZigValue *base = &array->data.s_none.elements[start];
6921 assert(base != nullptr);
68786922 assert(start + len <= const_val->type->data.array.len);
68796923
68806924 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
68906934}
68916935
68926936void 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 }
68936941 switch (const_val->special) {
68946942 case ConstValSpecialRuntime:
68956943 buf_appendf(buf, "(runtime value)");
......@@ -9295,10 +9343,13 @@ bool type_has_optional_repr(ZigType *ty) {
92959343}
92969344
92979345void copy_const_val(ZigValue *dest, ZigValue *src) {
9346 uint32_t prev_align = dest->llvm_align;
9347 ConstParent prev_parent = dest->parent;
92989348 memcpy(dest, src, sizeof(ZigValue));
9349 dest->llvm_align = prev_align;
92999350 if (src->special != ConstValSpecialStatic)
93009351 return;
9301 dest->parent.id = ConstParentIdNone;
9352 dest->parent = prev_parent;
93029353 if (dest->type->id == ZigTypeIdStruct) {
93039354 dest->data.x_struct.fields = alloc_const_vals_ptrs(dest->type->data.structure.src_field_count);
93049355 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) {
93079358 dest->data.x_struct.fields[i]->parent.data.p_struct.struct_val = dest;
93089359 dest->data.x_struct.fields[i]->parent.data.p_struct.field_index = i;
93099360 }
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 }
93109371 } else if (type_has_optional_repr(dest->type) && dest->data.x_optional != nullptr) {
93119372 dest->data.x_optional = create_const_vals(1);
93129373 copy_const_val(dest->data.x_optional, src->data.x_optional);
......@@ -9353,6 +9414,171 @@ bool type_is_numeric(ZigType *ty) {
93539414 zig_unreachable();
93549415}
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
93569582// float ops that take a single argument
93579583//TODO Powi, Pow, minnum, maxnum, maximum, minimum, copysign, lround, llround, lrint, llrint
93589584const char *float_op_to_name(BuiltinFnId op) {
src/codegen.cpp+14-2
......@@ -20,6 +20,7 @@
2020#include "zig_llvm.h"
2121#include "userland.h"
2222#include "dump_analysis.hpp"
23#include "softfloat.hpp"
2324
2425#include <stdio.h>
2526#include <errno.h>
......@@ -3119,8 +3120,14 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutableGen *executab
31193120static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutableGen *executable,
31203121 IrInstGenCast *cast_instruction)
31213122{
3123 Error err;
31223124 ZigType *actual_type = cast_instruction->value->value->type;
31233125 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;
31243131 LLVMValueRef expr_val = ir_llvm_value(g, cast_instruction->value);
31253132 ir_assert(expr_val, &cast_instruction->base);
31263133
......@@ -3498,6 +3505,7 @@ static void render_decl_var(CodeGen *g, ZigVar *var) {
34983505
34993506static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutableGen *executable, IrInstGenDeclVar *instruction) {
35003507 instruction->var->ptr_instruction = instruction->var_ptr;
3508 instruction->var->did_the_decl_codegen = true;
35013509 render_decl_var(g, instruction->var);
35023510 return nullptr;
35033511}
......@@ -3966,7 +3974,7 @@ static void render_async_var_decls(CodeGen *g, Scope *scope) {
39663974 return;
39673975 case ScopeIdVarDecl: {
39683976 ZigVar *var = reinterpret_cast<ScopeVarDecl *>(scope)->var;
3969 if (var->ptr_instruction != nullptr) {
3977 if (var->did_the_decl_codegen) {
39703978 render_decl_var(g, var);
39713979 }
39723980 // fallthrough
......@@ -4723,6 +4731,10 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutableGen *e
47234731 LLVMPositionBuilderAtEnd(g->builder, ok_block);
47244732 }
47254733 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 }
47264738 return nullptr;
47274739 } else {
47284740 bool is_scalar = !handle_is_ptr(maybe_type);
......@@ -7529,7 +7541,7 @@ static void do_code_gen(CodeGen *g) {
75297541 } else {
75307542 if (want_sret) {
75317543 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)) {
75337545 g->cur_ret_ptr = build_alloca(g, fn_type_id->return_type, "result", 0);
75347546 // TODO add debug info variable for this
75357547 } else {
src/ir.cpp+506-438
......@@ -17,10 +17,6 @@
1717
1818#include <errno.h>
1919
20struct IrExecContext {
21 ZigList<ZigValue *> mem_slot_list;
22};
23
2420struct IrBuilderSrc {
2521 CodeGen *codegen;
2622 IrExecutableSrc *exec;
......@@ -38,7 +34,6 @@ struct IrAnalyze {
3834 CodeGen *codegen;
3935 IrBuilderSrc old_irb;
4036 IrBuilderGen new_irb;
41 IrExecContext exec_context;
4237 size_t old_bb_index;
4338 size_t instruction_index;
4439 ZigType *explicit_return_type;
......@@ -48,6 +43,7 @@ struct IrAnalyze {
4843 IrBasicBlockSrc *const_predecessor_bb;
4944 size_t ref_count;
5045 size_t break_debug_id; // for debugging purposes
46 IrInstGen *return_ptr;
5147
5248 // For the purpose of using in a debugger
5349 void dump();
......@@ -222,7 +218,8 @@ static IrInstGen *ir_get_deref(IrAnalyze *ira, IrInst *source_instr, IrInstGen *
222218 ResultLoc *result_loc);
223219static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutableSrc *exec, AstNode *source_node, Buf *msg);
224220static 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);
226223static void ir_assert(bool ok, IrInst* source_instruction);
227224static void ir_assert_gen(bool ok, IrInstGen *source_instruction);
228225static 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)
236233static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
237234 ZigValue *out_val, ZigValue *ptr_val);
238235static 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);
240237static ZigValue *ir_resolve_const(IrAnalyze *ira, IrInstGen *value, UndefAllowed undef_allowed);
241238static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align);
242239static 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
244241static IrInstGen *ir_analyze_bit_cast(IrAnalyze *ira, IrInst* source_instr, IrInstGen *value,
245242 ZigType *dest_type);
246243static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_instr,
247 ResultLoc *result_loc, ZigType *value_type, IrInstGen *value, bool force_runtime,
248 bool non_null_comptime, bool allow_discard);
244 ResultLoc *result_loc, ZigType *value_type, IrInstGen *value, bool force_runtime, bool allow_discard);
249245static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr,
250 ResultLoc *result_loc, ZigType *value_type, IrInstGen *value, bool force_runtime,
251 bool non_null_comptime, bool allow_discard);
246 ResultLoc *result_loc, ZigType *value_type, IrInstGen *value, bool force_runtime, bool allow_discard);
252247static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* source_instr,
253248 IrInstGen *base_ptr, bool safety_check_on, bool initializing);
254249static IrInstGen *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInst* source_instr,
......@@ -773,7 +768,6 @@ static void ira_deref(IrAnalyze *ira) {
773768 //destroy(ira->old_irb.exec, "IrExecutableSrc");
774769 ira->src_implicit_return_type_list.deinit();
775770 ira->resume_stack.deinit();
776 ira->exec_context.mem_slot_list.deinit();
777771 destroy(ira, "IrAnalyze");
778772}
779773
......@@ -887,7 +881,6 @@ static bool types_have_same_zig_comptime_repr(CodeGen *codegen, ZigType *expecte
887881 case ZigTypeIdComptimeFloat:
888882 case ZigTypeIdComptimeInt:
889883 case ZigTypeIdEnumLiteral:
890 case ZigTypeIdPointer:
891884 case ZigTypeIdUndefined:
892885 case ZigTypeIdNull:
893886 case ZigTypeIdBoundFn:
......@@ -896,6 +889,8 @@ static bool types_have_same_zig_comptime_repr(CodeGen *codegen, ZigType *expecte
896889 case ZigTypeIdAnyFrame:
897890 case ZigTypeIdFn:
898891 return true;
892 case ZigTypeIdPointer:
893 return expected->data.pointer.inferred_struct_field == actual->data.pointer.inferred_struct_field;
899894 case ZigTypeIdFloat:
900895 return expected->data.floating.bit_count == actual->data.floating.bit_count;
901896 case ZigTypeIdInt:
......@@ -958,12 +953,6 @@ static size_t exec_next_debug_id_gen(IrExecutableGen *exec) {
958953 return result;
959954}
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
967956static ZigFn *exec_fn_entry(IrExecutableSrc *exec) {
968957 return exec->fn_entry;
969958}
......@@ -1018,16 +1007,46 @@ static void ir_ref_var(ZigVar *var) {
10181007 var->ref_count += 1;
10191008}
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
10211027ZigType *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,
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);
1028 Error err;
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 }
10261040 if (type_is_invalid(result->type))
10271041 return ira->codegen->builtin_types.entry_invalid;
10281042
10291043 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;
10311050}
10321051
10331052static 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
23042323 return &instruction->base;
23052324}
23062325
2307static IrInstGen *ir_build_return_ptr(IrAnalyze *ira, IrInst *source_instruction, ZigType *ty) {
2308 IrInstGenReturnPtr *instruction = ir_build_inst_gen<IrInstGenReturnPtr>(&ira->new_irb,
2309 source_instruction->scope, source_instruction->source_node);
2326static IrInstGen *ir_build_return_ptr(IrAnalyze *ira, Scope *scope, AstNode *source_node, ZigType *ty) {
2327 IrInstGenReturnPtr *instruction = ir_build_inst_gen<IrInstGenReturnPtr>(&ira->new_irb, scope, source_node);
23102328 instruction->base.value->type = ty;
23112329 return &instruction->base;
23122330}
......@@ -4582,6 +4600,7 @@ static IrInstSrc *ir_build_reset_result(IrBuilderSrc *irb, Scope *scope, AstNode
45824600{
45834601 IrInstSrcResetResult *instruction = ir_build_instruction<IrInstSrcResetResult>(irb, scope, source_node);
45844602 instruction->result_loc = result_loc;
4603 instruction->base.is_gen = true;
45854604
45864605 return &instruction->base;
45874606}
......@@ -5203,12 +5222,6 @@ static ScopeDeferExpr *get_scope_defer_expr(Scope *scope) {
52035222static IrInstSrc *ir_gen_return(IrBuilderSrc *irb, Scope *scope, AstNode *node, LVal lval, ResultLoc *result_loc) {
52045223 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
52125225 ScopeDeferExpr *scope_defer_expr = get_scope_defer_expr(scope);
52135226 if (scope_defer_expr) {
52145227 if (!scope_defer_expr->reported_err) {
......@@ -5350,7 +5363,6 @@ static ZigVar *create_local_var(CodeGen *codegen, AstNode *node, Scope *parent_s
53505363 ZigVar *variable_entry = allocate<ZigVar>(1, "ZigVar");
53515364 variable_entry->parent_scope = parent_scope;
53525365 variable_entry->shadowable = is_shadowable;
5353 variable_entry->mem_slot_index = SIZE_MAX;
53545366 variable_entry->is_comptime = is_comptime;
53555367 variable_entry->src_arg_index = SIZE_MAX;
53565368 variable_entry->const_value = create_const_vals(1);
......@@ -5414,7 +5426,6 @@ static ZigVar *ir_create_var(IrBuilderSrc *irb, AstNode *node, Scope *scope, Buf
54145426 (is_underscored ? nullptr : name), src_is_const, gen_is_const,
54155427 (is_underscored ? true : is_shadowable), is_comptime, false);
54165428 if (is_comptime != nullptr || gen_is_const) {
5417 var->mem_slot_index = exec_next_mem_slot(irb->exec);
54185429 var->owner_exec = irb->exec;
54195430 }
54205431 assert(var->child_scope);
......@@ -5551,6 +5562,10 @@ static IrInstSrc *ir_gen_block(IrBuilderSrc *irb, Scope *parent_scope, AstNode *
55515562 // only generate unconditional defers
55525563
55535564 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));
55545569 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false);
55555570 return ir_mark_gen(ir_build_return_src(irb, child_scope, result->base.source_node, result));
55565571}
......@@ -7001,6 +7016,7 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
70017016 ResultLocBitCast *result_loc_bit_cast = allocate<ResultLocBitCast>(1);
70027017 result_loc_bit_cast->base.id = ResultLocIdBitCast;
70037018 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;
70047020 ir_ref_instruction(dest_type, irb->current_basic_block);
70057021 result_loc_bit_cast->parent = result_loc;
70067022
......@@ -8462,9 +8478,8 @@ static IrInstSrc *ir_gen_for_expr(IrBuilderSrc *irb, Scope *parent_scope, AstNod
84628478 ResultLocPeerParent *peer_parent = ir_build_result_peers(irb, cond_br_inst, end_block, result_loc, is_comptime);
84638479
84648480 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;
8466 IrInstSrc *elem_ptr = ir_build_elem_ptr(irb, elem_ptr_scope, node, array_val_ptr, index_val, false,
8467 PtrLenSingle, nullptr);
8481 IrInstSrc *elem_ptr = ir_build_elem_ptr(irb, &spill_scope->base, node, array_val_ptr, index_val,
8482 false, PtrLenSingle, nullptr);
84688483 // TODO make it an error to write to element variable or i variable.
84698484 Buf *elem_var_name = elem_node->data.symbol_expr.symbol;
84708485 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
1025510270 if (!instr_is_unreachable(result)) {
1025610271 ir_mark_gen(ir_build_add_implicit_return_type(irb, scope, result->base.source_node, result, nullptr));
1025710272 // 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));
1025810277 ir_mark_gen(ir_build_return_src(irb, scope, result->base.source_node, result));
1025910278 }
1026010279
......@@ -10374,23 +10393,17 @@ ZigValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ZigValue *const_va
1037410393 return val;
1037510394}
1037610395
10377static ZigValue *ir_exec_const_result(CodeGen *codegen, IrExecutableGen *exec) {
10396static Error ir_exec_scan_for_side_effects(CodeGen *codegen, IrExecutableGen *exec) {
1037810397 IrBasicBlockGen *bb = exec->basic_block_list.at(0);
1037910398 for (size_t i = 0; i < bb->instruction_list.length; i += 1) {
1038010399 IrInstGen *instruction = bb->instruction_list.at(i);
1038110400 if (instruction->id == IrInstGenIdReturn) {
10382 IrInstGenReturn *ret_inst = (IrInstGenReturn *)instruction;
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;
10401 return ErrorNone;
1039010402 } else if (ir_inst_gen_has_side_effects(instruction)) {
1039110403 if (instr_is_comptime(instruction)) {
1039210404 switch (instruction->id) {
1039310405 case IrInstGenIdUnwrapErrPayload:
10406 case IrInstGenIdOptionalUnwrapPtr:
1039410407 case IrInstGenIdUnionFieldPtr:
1039510408 continue;
1039610409 default:
......@@ -10403,7 +10416,7 @@ static ZigValue *ir_exec_const_result(CodeGen *codegen, IrExecutableGen *exec) {
1040310416 }
1040410417 exec_add_error_node_gen(codegen, exec, instruction->base.source_node,
1040510418 buf_sprintf("unable to evaluate constant expression"));
10406 return codegen->invalid_inst_gen->value;
10419 return ErrorSemanticAnalyzeFail;
1040710420 }
1040810421 }
1040910422 zig_unreachable();
......@@ -12785,26 +12798,28 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc
1278512798 wanted_type = adjust_slice_align(ira->codegen, wanted_type, get_ptr_align(ira->codegen, array_ptr->value->type));
1278612799
1278712800 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);
1278912805 if (pointee == nullptr)
1279012806 return ira->codegen->invalid_inst_gen;
1279112807 if (pointee->special != ConstValSpecialRuntime) {
12792 assert(array_ptr->value->type->id == ZigTypeIdPointer);
12793 ZigType *array_type = array_ptr->value->type->data.pointer.child_type;
12808 assert(array_ptr_val->type->id == ZigTypeIdPointer);
12809 ZigType *array_type = array_ptr_val->type->data.pointer.child_type;
1279412810 assert(is_slice(wanted_type));
1279512811 bool is_const = wanted_type->data.structure.fields[slice_ptr_index]->type_entry->data.pointer.is_const;
1279612812
1279712813 IrInstGen *result = ir_const(ira, source_instr, wanted_type);
1279812814 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;
1280012816 result->value->type = wanted_type;
1280112817 return result;
1280212818 }
1280312819 }
1280412820
1280512821 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,
12807 false, true);
12822 IrInstGen *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, true);
1280812823 if (type_is_invalid(result_loc_inst->value->type) ||
1280912824 result_loc_inst->value->type->id == ZigTypeIdUnreachable)
1281012825 {
......@@ -13026,10 +13041,13 @@ static IrInstGen *ir_const_fn(IrAnalyze *ira, IrInst *source_instr, ZigFn *fn_en
1302613041 return result;
1302713042}
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{
1303013047 IrInstGen *result = ir_const(ira, src_inst, get_bound_fn_type(ira->codegen, fn_entry));
1303113048 result->value->data.x_bound_fn.fn = fn_entry;
1303213049 result->value->data.x_bound_fn.first_arg = first_arg;
13050 result->value->data.x_bound_fn.first_arg_src = first_arg_src;
1303313051 return result;
1303413052}
1303513053
......@@ -13127,15 +13145,17 @@ static ZigValue *ir_resolve_const(IrAnalyze *ira, IrInstGen *value, UndefAllowed
1312713145 return value->value;
1312813146}
1312913147
13130ZigValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
13131 ZigType *expected_type, size_t *backward_branch_count, size_t *backward_branch_quota,
13148Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
13149 ZigValue *return_ptr, size_t *backward_branch_count, size_t *backward_branch_quota,
1313213150 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
1313313151 IrExecutableGen *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef_allowed)
1313413152{
1313513153 Error err;
1313613154
13137 if (expected_type != nullptr && type_is_invalid(expected_type))
13138 return codegen->invalid_inst_gen->value;
13155 src_assert(return_ptr->type->id == ZigTypeIdPointer, source_node);
13156
13157 if (type_is_invalid(return_ptr->type))
13158 return ErrorSemanticAnalyzeFail;
1313913159
1314013160 IrExecutableSrc *ir_executable = allocate<IrExecutableSrc>(1, "IrExecutableSrc");
1314113161 ir_executable->source_node = source_node;
......@@ -13147,11 +13167,11 @@ ZigValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1314713167 ir_executable->begin_scope = scope;
1314813168
1314913169 if (!ir_gen(codegen, node, scope, ir_executable))
13150 return codegen->invalid_inst_gen->value;
13170 return ErrorSemanticAnalyzeFail;
1315113171
1315213172 if (ir_executable->first_err_trace_msg != nullptr) {
1315313173 codegen->trace_err = ir_executable->first_err_trace_msg;
13154 return codegen->invalid_inst_gen->value;
13174 return ErrorSemanticAnalyzeFail;
1315513175 }
1315613176
1315713177 if (codegen->verbose_ir) {
......@@ -13172,9 +13192,10 @@ ZigValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1317213192 analyzed_executable->backward_branch_count = backward_branch_count;
1317313193 analyzed_executable->backward_branch_quota = backward_branch_quota;
1317413194 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);
1317613197 if (type_is_invalid(result_type)) {
13177 return codegen->invalid_inst_gen->value;
13198 return ErrorSemanticAnalyzeFail;
1317813199 }
1317913200
1318013201 if (codegen->verbose_ir) {
......@@ -13183,14 +13204,16 @@ ZigValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1318313204 fprintf(stderr, "}\n");
1318413205 }
1318513206
13186 ZigValue *result = ir_exec_const_result(codegen, analyzed_executable);
13187 if (type_is_invalid(result->type))
13188 return codegen->invalid_inst_gen->value;
13207 if ((err = ir_exec_scan_for_side_effects(codegen, analyzed_executable)))
13208 return err;
1318913209
13210 ZigValue *result = const_ptr_pointee(nullptr, codegen, return_ptr, source_node);
13211 if (result == nullptr)
13212 return ErrorSemanticAnalyzeFail;
1319013213 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;
1319413217}
1319513218
1319613219static ErrorTableEntry *ir_resolve_error(IrAnalyze *ira, IrInstGen *err_value) {
......@@ -13372,7 +13395,7 @@ static IrInstGen *ir_analyze_optional_wrap(IrAnalyze *ira, IrInst* source_instr,
1337213395 }
1337313396 IrInstGen *result_loc_inst = nullptr;
1337413397 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);
1337613399 if (type_is_invalid(result_loc_inst->value->type) ||
1337713400 result_loc_inst->value->type->id == ZigTypeIdUnreachable)
1337813401 {
......@@ -13396,8 +13419,8 @@ static IrInstGen *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInst* source_ins
1339613419 if (type_is_invalid(casted_payload->value->type))
1339713420 return ira->codegen->invalid_inst_gen;
1339813421
13399 ZigValue *val = ir_resolve_const(ira, casted_payload, UndefBad);
13400 if (!val)
13422 ZigValue *val = ir_resolve_const(ira, casted_payload, UndefOk);
13423 if (val == nullptr)
1340113424 return ira->codegen->invalid_inst_gen;
1340213425
1340313426 ZigValue *err_set_val = create_const_vals(1);
......@@ -13417,7 +13440,7 @@ static IrInstGen *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInst* source_ins
1341713440 IrInstGen *result_loc_inst;
1341813441 if (handle_is_ptr(wanted_type)) {
1341913442 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);
1342113444 if (type_is_invalid(result_loc_inst->value->type) ||
1342213445 result_loc_inst->value->type->id == ZigTypeIdUnreachable) {
1342313446 return result_loc_inst;
......@@ -13529,7 +13552,7 @@ static IrInstGen *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInst* source_instr,
1352913552 IrInstGen *result_loc_inst;
1353013553 if (handle_is_ptr(wanted_type)) {
1353113554 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);
1353313556 if (type_is_invalid(result_loc_inst->value->type) ||
1353413557 result_loc_inst->value->type->id == ZigTypeIdUnreachable)
1353513558 {
......@@ -13580,32 +13603,31 @@ static IrInstGen *ir_analyze_null_to_c_pointer(IrAnalyze *ira, IrInst *source_in
1358013603 return result;
1358113604}
1358213605
13583static IrInstGen *ir_get_ref(IrAnalyze *ira, IrInst* source_instruction, IrInstGen *value,
13584 bool is_const, bool is_volatile)
13606static IrInstGen *ir_get_ref2(IrAnalyze *ira, IrInst* source_instruction, IrInstGen *value,
13607 ZigType *elem_type, bool is_const, bool is_volatile)
1358513608{
1358613609 Error err;
1358713610
13588 if (type_is_invalid(value->value->type))
13611 if (type_is_invalid(elem_type))
1358913612 return ira->codegen->invalid_inst_gen;
1359013613
1359113614 if (instr_is_comptime(value)) {
1359213615 ZigValue *val = ir_resolve_const(ira, value, LazyOk);
1359313616 if (!val)
1359413617 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,
1359613619 ConstPtrMutComptimeConst, is_const, is_volatile, 0);
1359713620 }
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,
1360013623 is_const, is_volatile, PtrLenSingle, 0, 0, 0, false);
1360113624
1360213625 if ((err = type_resolve(ira->codegen, ptr_type, ResolveStatusZeroBitsKnown)))
1360313626 return ira->codegen->invalid_inst_gen;
1360413627
1360513628 IrInstGen *result_loc;
13606 if (type_has_bits(ptr_type) && !handle_is_ptr(value->value->type)) {
13607 result_loc = ir_resolve_result(ira, source_instruction, no_result_loc(), value->value->type, nullptr, true,
13608 false, true);
13629 if (type_has_bits(ptr_type) && !handle_is_ptr(elem_type)) {
13630 result_loc = ir_resolve_result(ira, source_instruction, no_result_loc(), elem_type, nullptr, true, true);
1360913631 } else {
1361013632 result_loc = nullptr;
1361113633 }
......@@ -13615,6 +13637,12 @@ static IrInstGen *ir_get_ref(IrAnalyze *ira, IrInst* source_instruction, IrInstG
1361513637 return new_instruction;
1361613638}
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
1361813646static ZigType *ir_resolve_union_tag_type(IrAnalyze *ira, AstNode *source_node, ZigType *union_type) {
1361913647 assert(union_type->id == ZigTypeIdUnion);
1362013648
......@@ -14311,8 +14339,7 @@ static IrInstGen *ir_analyze_vector_to_array(IrAnalyze *ira, IrInst* source_inst
1431114339 if (result_loc == nullptr) {
1431214340 result_loc = no_result_loc();
1431314341 }
14314 IrInstGen *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, array_type, nullptr,
14315 true, false, true);
14342 IrInstGen *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, array_type, nullptr, true, true);
1431614343 if (type_is_invalid(result_loc_inst->value->type) || result_loc_inst->value->type->id == ZigTypeIdUnreachable) {
1431714344 return result_loc_inst;
1431814345 }
......@@ -14976,7 +15003,7 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,
1497615003 dest_ptr_type = wanted_type->data.maybe.child_type;
1497715004 }
1497815005 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);
1498015007 }
1498115008 }
1498215009
......@@ -15018,7 +15045,7 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,
1501815045 actual_type->data.pointer.child_type, source_node,
1501915046 !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)
1502015047 {
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);
1502215049 }
1502315050
1502415051 // cast from integer to C pointer
......@@ -15195,8 +15222,7 @@ static IrInstGen *ir_get_deref(IrAnalyze *ira, IrInst* source_instruction, IrIns
1519515222 IrInstGen *result_loc_inst;
1519615223 if (ptr_type->data.pointer.host_int_bytes != 0 && handle_is_ptr(child_type)) {
1519715224 if (result_loc == nullptr) result_loc = no_result_loc();
15198 result_loc_inst = ir_resolve_result(ira, source_instruction, result_loc, child_type, nullptr,
15199 true, false, true);
15225 result_loc_inst = ir_resolve_result(ira, source_instruction, result_loc, child_type, nullptr, true, true);
1520015226 if (type_is_invalid(result_loc_inst->value->type) || result_loc_inst->value->type->id == ZigTypeIdUnreachable) {
1520115227 return result_loc_inst;
1520215228 }
......@@ -15459,7 +15485,7 @@ static IrInstGen *ir_analyze_instruction_return(IrAnalyze *ira, IrInstSrcReturn
1545915485 casted_operand->value->type->id == ZigTypeIdPointer &&
1546015486 casted_operand->value->data.rh_ptr == RuntimeHintPtrStack)
1546115487 {
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"));
1546315489 return ir_unreach_error(ira);
1546415490 }
1546515491
......@@ -15622,9 +15648,27 @@ static IrInstGen *ir_evaluate_bin_op_cmp(IrAnalyze *ira, ZigType *resolved_type,
1562215648}
1562315649
1562415650// 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) {
1562615652 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
1562815672 switch (val->special) {
1562915673 case ConstValSpecialRuntime:
1563015674 case ConstValSpecialUndef:
......@@ -15678,12 +15722,12 @@ static ErrorMsg *ir_eval_bin_op_cmp_scalar(IrAnalyze *ira, IrInst* source_instr,
1567815722 // Before resolving the values, we special case comparisons against zero. These can often
1567915723 // be done without resolving lazy values, preventing potential dependency loops.
1568015724 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))) {
1568215726 if (err == ErrorNotLazy) goto never_mind_just_calculate_it_normally;
1568315727 return ira->codegen->trace_err;
1568415728 }
1568515729 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))) {
1568715731 if (err == ErrorNotLazy) goto never_mind_just_calculate_it_normally;
1568815732 return ira->codegen->trace_err;
1568915733 }
......@@ -15847,14 +15891,14 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i
1584715891 }
1584815892 Cmp op1_cmp_zero;
1584915893 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))) {
1585115895 if (err != ErrorNotLazy) return ira->codegen->invalid_inst_gen;
1585215896 } else {
1585315897 have_op1_cmp_zero = true;
1585415898 }
1585515899 Cmp op2_cmp_zero;
1585615900 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))) {
1585815902 if (err != ErrorNotLazy) return ira->codegen->invalid_inst_gen;
1585915903 } else {
1586015904 have_op2_cmp_zero = true;
......@@ -17083,7 +17127,7 @@ static IrInstGen *ir_analyze_tuple_cat(IrAnalyze *ira, IrInst* source_instr,
1708317127 bool is_comptime = ir_should_inline(ira->old_irb.exec, source_instr->scope);
1708417128
1708517129 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);
1708717131 uint32_t new_field_count = op1_field_count + op2_field_count;
1708817132
1708917133 new_type->data.structure.src_field_count = new_field_count;
......@@ -17669,29 +17713,14 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV
1766917713 break;
1767017714 }
1767117715
17672 if (var->var_type != nullptr && !is_comptime_var) {
17673 // This is at least the second time we've seen this variable declaration during analysis.
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;
17716 while (var->next_var != nullptr) {
17717 var = var->next_var;
1769017718 }
1769117719
1769217720 // This must be done after possibly creating a new variable above
1769317721 var->ref_count = 0;
1769417722
17723 var->ptr_instruction = var_ptr;
1769517724 var->var_type = result_type;
1769617725 assert(var->var_type);
1769717726
......@@ -17704,7 +17733,7 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV
1770417733 var->var_type = ira->codegen->builtin_types.entry_invalid;
1770517734 return ir_const_void(ira, &decl_var_instruction->base.base);
1770617735 }
17707 var->align_bytes = get_abi_alignment(ira->codegen, result_type);
17736 var->align_bytes = get_ptr_align(ira->codegen, var_ptr->value->type);
1770817737 } else {
1770917738 if (!ir_resolve_align(ira, decl_var_instruction->align_value->child, nullptr, &var->align_bytes)) {
1771017739 var->var_type = ira->codegen->builtin_types.entry_invalid;
......@@ -17733,16 +17762,8 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV
1773317762 var_ptr->value->special = ConstValSpecialRuntime;
1773417763 ir_analyze_store_ptr(ira, &var_ptr->base, var_ptr, deref, false);
1773517764 }
17736
17737 if (instr_is_comptime(var_ptr) && var->mem_slot_index != SIZE_MAX) {
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 }
17765 if (instr_is_comptime(var_ptr) && (is_comptime_var || (var_class_requires_const && var->gen_is_const))) {
17766 return ir_const_void(ira, &decl_var_instruction->base.base);
1774617767 }
1774717768 } else if (is_comptime_var) {
1774817769 ir_add_error(ira, &decl_var_instruction->base.base,
......@@ -18076,6 +18097,7 @@ static IrInstGen *ir_analyze_alloca(IrAnalyze *ira, IrInst *source_inst, ZigType
1807618097
1807718098 ZigValue *pointee = create_const_vals(1);
1807818099 pointee->special = ConstValSpecialUndef;
18100 pointee->llvm_align = align;
1807918101
1808018102 IrInstGenAlloca *result = ir_build_alloca_gen(ira, source_inst, align, name_hint);
1808118103 result->base.value->special = ConstValSpecialStatic;
......@@ -18102,9 +18124,11 @@ static IrInstGen *ir_analyze_alloca(IrAnalyze *ira, IrInst *source_inst, ZigType
1810218124 result->base.value->type = get_pointer_to_type_extra(ira->codegen, var_type, false, false,
1810318125 PtrLenSingle, align, 0, 0, false);
1810418126
18105 ZigFn *fn_entry = ira->new_irb.exec->fn_entry;
18106 if (fn_entry != nullptr) {
18107 fn_entry->alloca_gen_list.append(result);
18127 if (!force_comptime) {
18128 ZigFn *fn_entry = ira->new_irb.exec->fn_entry;
18129 if (fn_entry != nullptr) {
18130 fn_entry->alloca_gen_list.append(result);
18131 }
1810818132 }
1810918133 return &result->base;
1811018134}
......@@ -18192,7 +18216,7 @@ static Error ir_result_has_type(IrAnalyze *ira, ResultLoc *result_loc, bool *out
1819218216}
1819318217
1819418218static 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)
1819618220{
1819718221 if (type_is_invalid(value_type))
1819818222 return ira->codegen->invalid_inst_gen;
......@@ -18209,10 +18233,25 @@ static IrInstGen *ir_resolve_no_result_loc(IrAnalyze *ira, IrInst *suspend_sourc
1820918233 return result_loc->resolved_loc;
1821018234}
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
1821218251// when calling this function, at the callsite must check for result type noreturn and propagate it up
1821318252static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_instr,
1821418253 ResultLoc *result_loc, ZigType *value_type, IrInstGen *value, bool force_runtime,
18215 bool non_null_comptime, bool allow_discard)
18254 bool allow_discard)
1821618255{
1821718256 Error err;
1821818257 if (result_loc->resolved_loc != nullptr) {
......@@ -18232,53 +18271,56 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
1823218271 return nullptr;
1823318272 }
1823418273 // 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,
18236 force_runtime, non_null_comptime);
18274 return ir_resolve_no_result_loc(ira, suspend_source_instr, result_loc, value_type);
1823718275 }
1823818276 case ResultLocIdVar: {
1823918277 ResultLocVar *result_loc_var = reinterpret_cast<ResultLocVar *>(result_loc);
1824018278 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 }
1824218296 if (value_type->id == ZigTypeIdUnreachable || value_type->id == ZigTypeIdOpaque) {
1824318297 ir_add_error(ira, &result_loc->source_instruction->base,
1824418298 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&value_type->name)));
1824518299 return ira->codegen->invalid_inst_gen;
1824618300 }
18247
18248 IrInstSrcAlloca *alloca_src = reinterpret_cast<IrInstSrcAlloca *>(result_loc->source_instruction);
18249 bool force_comptime;
18250 if (!ir_resolve_comptime(ira, alloca_src->is_comptime->child, &force_comptime))
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) {
18301 if (alloca_src->base.child == nullptr || var->ptr_instruction == nullptr) {
18302 bool force_comptime;
18303 if (!ir_resolve_comptime(ira, alloca_src->is_comptime->child, &force_comptime))
18304 return ira->codegen->invalid_inst_gen;
1825618305 uint32_t align = 0;
1825718306 if (alloca_src->align != nullptr && !ir_resolve_align(ira, alloca_src->align->child, nullptr, &align)) {
1825818307 return ira->codegen->invalid_inst_gen;
1825918308 }
18260 IrInstGen *alloca_gen;
18261 if (is_comptime && value != nullptr) {
18262 if (align > value->value->llvm_align) {
18263 value->value->llvm_align = align;
18264 }
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 }
18309 IrInstGen *alloca_gen = ir_analyze_alloca(ira, &result_loc->source_instruction->base, value_type,
18310 align, alloca_src->name_hint, force_comptime);
18311 if (force_runtime) {
18312 alloca_gen->value->data.x_ptr.mut = ConstPtrMutRuntimeVar;
18313 alloca_gen->value->special = ConstValSpecialRuntime;
1827318314 }
1827418315 if (alloca_src->base.child != nullptr && !result_loc->written) {
1827518316 alloca_src->base.child->base.ref_count = 0;
1827618317 }
1827718318 alloca_src->base.child = alloca_gen;
18319 var->ptr_instruction = alloca_gen;
1827818320 }
1827918321 result_loc->written = true;
18280 result_loc->resolved_loc = is_comptime ? nullptr : alloca_src->base.child;
18281 return result_loc->resolved_loc;
18322 result_loc->resolved_loc = alloca_src->base.child;
18323 return alloca_src->base.child;
1828218324 }
1828318325 case ResultLocIdInstruction: {
1828418326 result_loc->written = true;
......@@ -18290,27 +18332,8 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
1829018332 reinterpret_cast<ResultLocReturn *>(result_loc)->implicit_return_type_done = true;
1829118333 ira->src_implicit_return_type_list.append(value);
1829218334 }
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);
1830918335 result_loc->written = true;
18310 result_loc->resolved_loc = ir_build_return_ptr(ira, &result_loc->source_instruction->base, ptr_return_type);
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 }
18336 result_loc->resolved_loc = ira->return_ptr;
1831418337 return result_loc->resolved_loc;
1831518338 }
1831618339 case ResultLocIdPeer: {
......@@ -18319,7 +18342,7 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
1831918342
1832018343 if (peer_parent->peers.length == 1) {
1832118344 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);
1832318346 result_peer->suspend_pos.basic_block_index = SIZE_MAX;
1832418347 result_peer->suspend_pos.instruction_index = SIZE_MAX;
1832518348 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
1833718360 return ira->codegen->invalid_inst_gen;
1833818361 if (is_condition_comptime) {
1833918362 peer_parent->skipped = true;
18340 if (non_null_comptime) {
18341 return ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
18342 value_type, value, force_runtime, non_null_comptime, true);
18343 }
18344 return nullptr;
18363 return ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
18364 value_type, value, force_runtime, true);
1834518365 }
1834618366 bool peer_parent_has_type;
1834718367 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
1834918369 if (peer_parent_has_type) {
1835018370 peer_parent->skipped = true;
1835118371 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);
1835318373 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||
1835418374 parent_result_loc->value->type->id == ZigTypeIdUnreachable)
1835518375 {
......@@ -18374,7 +18394,7 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
1837418394 }
1837518395
1837618396 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);
1837818398 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||
1837918399 parent_result_loc->value->type->id == ZigTypeIdUnreachable)
1838018400 {
......@@ -18387,21 +18407,18 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
1838718407 return result_loc->resolved_loc;
1838818408 }
1838918409 case ResultLocIdCast: {
18390 if (value != nullptr && value->value->special != ConstValSpecialRuntime && !non_null_comptime)
18391 return nullptr;
1839218410 ResultLocCast *result_cast = reinterpret_cast<ResultLocCast *>(result_loc);
1839318411 ZigType *dest_type = ir_resolve_type(ira, result_cast->base.source_instruction->child);
1839418412 if (type_is_invalid(dest_type))
1839518413 return ira->codegen->invalid_inst_gen;
1839618414
1839718415 if (dest_type == ira->codegen->builtin_types.entry_var) {
18398 return ir_resolve_no_result_loc(ira, suspend_source_instr, result_loc, value_type,
18399 force_runtime, non_null_comptime);
18416 return ir_resolve_no_result_loc(ira, suspend_source_instr, result_loc, value_type);
1840018417 }
1840118418
1840218419 IrInstGen *casted_value;
1840318420 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);
1840518422 if (type_is_invalid(casted_value->value->type))
1840618423 return ira->codegen->invalid_inst_gen;
1840718424 dest_type = casted_value->value->type;
......@@ -18410,7 +18427,7 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
1841018427 }
1841118428
1841218429 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);
1841418431 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||
1841518432 parent_result_loc->value->type->id == ZigTypeIdUnreachable)
1841618433 {
......@@ -18460,13 +18477,12 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
1846018477 // We will not be able to provide a result location for this value. Create
1846118478 // a new result location.
1846218479 result_cast->parent->written = false;
18463 return ir_resolve_no_result_loc(ira, suspend_source_instr, result_loc, value_type,
18464 force_runtime, non_null_comptime);
18480 return ir_resolve_no_result_loc(ira, suspend_source_instr, result_loc, value_type);
1846518481 }
1846618482
1846718483 result_loc->written = true;
1846818484 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);
1847018486 return result_loc->resolved_loc;
1847118487 }
1847218488 case ResultLocIdBitCast: {
......@@ -18507,12 +18523,13 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
1850718523 bitcasted_value = nullptr;
1850818524 }
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)) {
1851118527 return bitcasted_value;
1851218528 }
1851318529
18530 bool parent_was_written = result_bit_cast->parent->written;
1851418531 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);
1851618533 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||
1851718534 parent_result_loc->value->type->id == ZigTypeIdUnreachable)
1851818535 {
......@@ -18522,32 +18539,38 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
1852218539 assert(parent_ptr_type->id == ZigTypeIdPointer);
1852318540 ZigType *child_type = parent_ptr_type->data.pointer.child_type;
1852418541
18525 bool has_bits;
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) {
18542 if (result_loc_is_discard(result_bit_cast->parent)) {
1853218543 assert(allow_discard);
1853318544 return parent_result_loc;
1853418545 }
1853518546
18536 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusAlignmentKnown))) {
18547 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown))) {
1853718548 return ira->codegen->invalid_inst_gen;
1853818549 }
1853918550
18540 uint64_t parent_ptr_align = get_ptr_align(ira->codegen, parent_ptr_type);
18541 if ((err = type_resolve(ira->codegen, value_type, ResolveStatusAlignmentKnown))) {
18551 if ((err = type_resolve(ira->codegen, value_type, ResolveStatusSizeKnown))) {
1854218552 return ira->codegen->invalid_inst_gen;
1854318553 }
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);
1854418567 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, value_type,
1854518568 parent_ptr_type->data.pointer.is_const, parent_ptr_type->data.pointer.is_volatile, PtrLenSingle,
1854618569 parent_ptr_align, 0, 0, parent_ptr_type->data.pointer.allow_zero);
1854718570
1854818571 result_loc->written = true;
1854918572 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);
1855118574 return result_loc->resolved_loc;
1855218575 }
1855318576 }
......@@ -18556,25 +18579,19 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
1855618579
1855718580static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr,
1855818581 ResultLoc *result_loc_pass1, ZigType *value_type, IrInstGen *value, bool force_runtime,
18559 bool non_null_comptime, bool allow_discard)
18582 bool allow_discard)
1856018583{
18561 Error err;
18562 if (!allow_discard && result_loc_pass1->id == ResultLocIdInstruction &&
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 }
18584 if (!allow_discard && result_loc_is_discard(result_loc_pass1)) {
18585 result_loc_pass1 = no_result_loc();
1857218586 }
18573 bool was_already_resolved = result_loc_pass1->resolved_loc != nullptr;
18587 bool was_written = result_loc_pass1->written;
1857418588 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);
18576 if (result_loc == nullptr || (result_loc->value->type->id == ZigTypeIdUnreachable || type_is_invalid(result_loc->value->type)))
18589 value, force_runtime, allow_discard);
18590 if (result_loc == nullptr || result_loc->value->type->id == ZigTypeIdUnreachable ||
18591 type_is_invalid(result_loc->value->type))
18592 {
1857718593 return result_loc;
18594 }
1857818595
1857918596 if ((force_runtime || (value != nullptr && !instr_is_comptime(value))) &&
1858018597 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
1858318600 }
1858418601
1858518602 InferredStructField *isf = result_loc->value->type->data.pointer.inferred_struct_field;
18586 if (!was_already_resolved && isf != nullptr) {
18587 // Now it's time to add the field to the struct type.
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);
18603 if (isf != nullptr) {
18604 TypeStructField *field;
1861118605 IrInstGen *casted_ptr;
18612 if (instr_is_comptime(result_loc)) {
18613 casted_ptr = ir_const(ira, suspend_source_instr, struct_ptr_type);
18614 copy_const_val(casted_ptr->value, result_loc->value);
18615 casted_ptr->value->type = struct_ptr_type;
18616 } else {
18606 if (isf->already_resolved) {
18607 field = find_struct_type_field(isf->inferred_struct_type, isf->field_name);
1861718608 casted_ptr = result_loc;
18618 }
18619 if (instr_is_comptime(casted_ptr)) {
18620 ZigValue *ptr_val = ir_resolve_const(ira, casted_ptr, UndefBad);
18621 if (!ptr_val)
18622 return ira->codegen->invalid_inst_gen;
18623 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
18624 ZigValue *struct_val = const_ptr_pointee(ira, ira->codegen, ptr_val,
18625 suspend_source_instr->source_node);
18626 struct_val->special = ConstValSpecialStatic;
18627 struct_val->data.x_struct.fields = realloc_const_vals_ptrs(struct_val->data.x_struct.fields,
18628 old_field_count, new_field_count);
18609 } else {
18610 isf->already_resolved = true;
18611 // Now it's time to add the field to the struct type.
18612 uint32_t old_field_count = isf->inferred_struct_type->data.structure.src_field_count;
18613 uint32_t new_field_count = old_field_count + 1;
18614 isf->inferred_struct_type->data.structure.src_field_count = new_field_count;
18615 isf->inferred_struct_type->data.structure.fields = realloc_type_struct_fields(
18616 isf->inferred_struct_type->data.structure.fields, old_field_count, new_field_count);
18617
18618 field = isf->inferred_struct_type->data.structure.fields[old_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];
18631 field_val->special = ConstValSpecialUndef;
18632 field_val->type = field->type_entry;
18633 field_val->parent.id = ConstParentIdStruct;
18634 field_val->parent.data.p_struct.struct_val = struct_val;
18635 field_val->parent.data.p_struct.field_index = old_field_count;
18634 ZigType *struct_ptr_type = get_pointer_to_type(ira->codegen, isf->inferred_struct_type, false);
18635 if (instr_is_comptime(result_loc)) {
18636 casted_ptr = ir_const(ira, suspend_source_instr, struct_ptr_type);
18637 copy_const_val(casted_ptr->value, result_loc->value);
18638 casted_ptr->value->type = struct_ptr_type;
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 }
1863618660 }
1863718661 }
1863818662
......@@ -18641,36 +18665,35 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr
1864118665 result_loc_pass1->resolved_loc = result_loc;
1864218666 }
1864318667
18668 if (was_written) {
18669 return result_loc;
18670 }
1864418671
1864518672 ir_assert(result_loc->value->type->id == ZigTypeIdPointer, suspend_source_instr);
1864618673 ZigType *actual_elem_type = result_loc->value->type->data.pointer.child_type;
1864718674 if (actual_elem_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional &&
18648 value_type->id != ZigTypeIdNull)
18675 value_type->id != ZigTypeIdNull && value_type->id != ZigTypeIdUndefined)
1864918676 {
18650 bool has_bits;
18651 if ((err = type_has_bits2(ira->codegen, value_type, &has_bits)))
18652 return ira->codegen->invalid_inst_gen;
18653 if (has_bits) {
18654 result_loc_pass1->written = false;
18677 bool same_comptime_repr = types_have_same_zig_comptime_repr(ira->codegen, actual_elem_type, value_type);
18678 if (!same_comptime_repr) {
18679 result_loc_pass1->written = was_written;
1865518680 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, result_loc, false, true);
1865618681 }
18657 } else if (actual_elem_type->id == ZigTypeIdErrorUnion && value_type->id != ZigTypeIdErrorUnion) {
18658 bool has_bits;
18659 if ((err = type_has_bits2(ira->codegen, value_type, &has_bits)))
18660 return ira->codegen->invalid_inst_gen;
18661 if (has_bits) {
18662 if (value_type->id == ZigTypeIdErrorSet) {
18663 return ir_analyze_unwrap_err_code(ira, suspend_source_instr, result_loc, true);
18682 } else if (actual_elem_type->id == ZigTypeIdErrorUnion && value_type->id != ZigTypeIdErrorUnion &&
18683 value_type->id != ZigTypeIdUndefined)
18684 {
18685 if (value_type->id == ZigTypeIdErrorSet) {
18686 return ir_analyze_unwrap_err_code(ira, suspend_source_instr, result_loc, true);
18687 } else {
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);
1866418695 } else {
18665 IrInstGen *unwrapped_err_ptr = ir_analyze_unwrap_error_payload(ira, suspend_source_instr,
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 }
18696 return unwrapped_err_ptr;
1867418697 }
1867518698 }
1867618699 }
......@@ -18717,7 +18740,7 @@ static IrInstGen *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrInstSr
1871718740 return ira->codegen->invalid_inst_gen;
1871818741 }
1871918742 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);
1872118744 if (result_loc != nullptr)
1872218745 return result_loc;
1872318746
......@@ -18726,7 +18749,7 @@ static IrInstGen *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrInstSr
1872618749 instruction->result_loc->id == ResultLocIdReturn)
1872718750 {
1872818751 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);
1873018753 if (result_loc != nullptr &&
1873118754 (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable))
1873218755 {
......@@ -18829,11 +18852,12 @@ static IrInstGen *ir_analyze_async_call(IrAnalyze *ira, IrInst* source_instr, Zi
1882918852 } else {
1883018853 ZigType *frame_type = get_fn_frame_type(ira->codegen, fn_entry);
1883118854 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);
1883318856 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
1883418857 return result_loc;
1883518858 }
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));
1883718861 if (type_is_invalid(result_loc->value->type))
1883818862 return ira->codegen->invalid_inst_gen;
1883918863 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
1887518899}
1887618900
1887718901static 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,
1887918903 GenericFnTypeId *generic_id, FnTypeId *fn_type_id, IrInstGen **casted_args,
1888018904 ZigFn *impl_fn)
1888118905{
......@@ -18894,7 +18918,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
1889418918 if (type_is_invalid(param_type))
1889518919 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);
1889818922 if (type_is_invalid(casted_arg->value->type))
1889918923 return false;
1890018924 } else {
......@@ -18965,41 +18989,28 @@ static IrInstGen *ir_get_var_ptr(IrAnalyze *ira, IrInst *source_instr, ZigVar *v
1896518989 var = var->next_var;
1896618990 }
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 }
1897218992 if (var->var_type == nullptr || type_is_invalid(var->var_type))
1897318993 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
1897719003 bool comptime_var_mem = ir_get_var_is_comptime(var);
1897819004 bool linkage_makes_it_runtime = var->decl_node->data.variable_declaration.is_extern;
18979 bool is_volatile = false;
1898019005
1898119006 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,
18983 var->src_is_const, is_volatile, PtrLenSingle, var->align_bytes, 0, 0, false);
19007 result->value->type = var_ptr_type;
1898419008
18985 if (linkage_makes_it_runtime || var->is_thread_local)
18986 goto no_mem_slot;
18987
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) {
19009 if (!linkage_makes_it_runtime && !var->is_thread_local && value_is_comptime(var->const_value)) {
19010 ZigValue *val = var->const_value;
19011 switch (val->special) {
1900119012 case ConstValSpecialRuntime:
19002 goto no_mem_slot;
19013 break;
1900319014 case ConstValSpecialStatic: // fallthrough
1900419015 case ConstValSpecialLazy: // fallthrough
1900519016 case ConstValSpecialUndef: {
......@@ -19015,15 +19026,12 @@ static IrInstGen *ir_get_var_ptr(IrAnalyze *ira, IrInst *source_instr, ZigVar *v
1901519026 result->value->special = ConstValSpecialStatic;
1901619027 result->value->data.x_ptr.mut = ptr_mut;
1901719028 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;
1901919030 return result;
1902019031 }
1902119032 }
19022 zig_unreachable();
1902319033 }
1902419034
19025no_mem_slot:
19026
1902719035 bool in_fn_scope = (scope_fn_entry(var->parent_scope) != nullptr);
1902819036 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,
1916619174}
1916719175
1916819176static 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)
1917019178{
1917119179 if (new_stack == nullptr)
1917219180 return nullptr;
......@@ -19191,14 +19199,14 @@ static IrInstGen *analyze_casted_new_stack(IrAnalyze *ira, IrInst* source_instr,
1919119199 false, false, PtrLenUnknown, target_fn_align(ira->codegen->zig_target), 0, 0, false);
1919219200 ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);
1919319201 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);
1919519203 }
1919619204}
1919719205
1919819206static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1919919207 ZigFn *fn_entry, ZigType *fn_type, IrInstGen *fn_ref,
19200 IrInstGen *first_arg_ptr, CallModifier modifier,
19201 IrInstGen *new_stack, bool is_async_call_builtin,
19208 IrInstGen *first_arg_ptr, IrInst *first_arg_ptr_src, CallModifier modifier,
19209 IrInstGen *new_stack, IrInst *new_stack_src, bool is_async_call_builtin,
1920219210 IrInstGen **args_ptr, size_t args_len, IrInstGen *ret_ptr, ResultLoc *call_result_loc)
1920319211{
1920419212 Error err;
......@@ -19285,13 +19293,6 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1928519293 return ira->codegen->invalid_inst_gen;
1928619294 }
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
1929519296 for (size_t call_i = 0; call_i < args_len; call_i += 1) {
1929619297 IrInstGen *old_arg = args_ptr[call_i];
1929719298
......@@ -19325,10 +19326,17 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1932519326 if (result == nullptr) {
1932619327 // Analyze the fn body block like any other constant expression.
1932719328 AstNode *body_node = fn_entry->body_node;
19328 result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type,
19329 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry,
19330 nullptr, source_instr->source_node, nullptr, ira->new_irb.exec, return_type_node,
19331 UndefOk);
19329 ZigValue *result_ptr;
19330 create_result_ptr(ira->codegen, return_type, &result, &result_ptr);
19331 if ((err = ir_eval_const_value(ira->codegen, exec_scope, body_node, result_ptr,
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
1933319341 if (inferred_err_set_type != nullptr) {
1933419342 inferred_err_set_type->data.error_set.incomplete = false;
......@@ -19412,8 +19420,8 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1941219420 return ira->codegen->invalid_inst_gen;
1941319421 }
1941419422
19415 if (!ir_analyze_fn_call_generic_arg(ira, fn_proto_node, first_arg, &impl_fn->child_scope,
19416 &next_proto_i, generic_id, &inst_fn_type_id, casted_args, impl_fn))
19423 if (!ir_analyze_fn_call_generic_arg(ira, fn_proto_node, first_arg, first_arg_ptr_src,
19424 &impl_fn->child_scope, &next_proto_i, generic_id, &inst_fn_type_id, casted_args, impl_fn))
1941719425 {
1941819426 return ira->codegen->invalid_inst_gen;
1941919427 }
......@@ -19427,7 +19435,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1942719435 AstNode *param_decl_node = fn_proto_node->data.fn_proto.params.at(next_proto_i);
1942819436 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,
1943119439 &next_proto_i, generic_id, &inst_fn_type_id, casted_args, impl_fn))
1943219440 {
1943319441 return ira->codegen->invalid_inst_gen;
......@@ -19435,14 +19443,21 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1943519443 }
1943619444
1943719445 if (fn_proto_node->data.fn_proto.align_expr != nullptr) {
19438 ZigValue *align_result = ir_eval_const_value(ira->codegen, impl_fn->child_scope,
19439 fn_proto_node->data.fn_proto.align_expr, get_align_amt_type(ira->codegen),
19440 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota,
19441 nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec,
19442 nullptr, UndefBad);
19443 IrInstGenConst *const_instruction = ir_create_inst_gen<IrInstGenConst>(&ira->new_irb,
19446 ZigValue *align_result;
19447 ZigValue *result_ptr;
19448 create_result_ptr(ira->codegen, get_align_amt_type(ira->codegen), &align_result, &result_ptr);
19449 if ((err = ir_eval_const_value(ira->codegen, impl_fn->child_scope,
19450 fn_proto_node->data.fn_proto.align_expr, result_ptr,
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,
1944419458 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
1944719462 uint32_t align_bytes = 0;
1944819463 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,
1946819483 case ReqCompTimeYes:
1946919484 // Throw out our work and call the function as if it were comptime.
1947019485 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,
19472 ret_ptr, call_result_loc);
19486 first_arg_ptr_src, CallModifierCompileTime, new_stack, new_stack_src, is_async_call_builtin,
19487 args_ptr, args_len, ret_ptr, call_result_loc);
1947319488 case ReqCompTimeInvalid:
1947419489 return ira->codegen->invalid_inst_gen;
1947519490 case ReqCompTimeNo:
......@@ -19504,7 +19519,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1950419519 }
1950519520
1950619521 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);
1950819523 if (casted_new_stack != nullptr && type_is_invalid(casted_new_stack->value->type))
1950919524 return ira->codegen->invalid_inst_gen;
1951019525
......@@ -19519,7 +19534,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1951919534 IrInstGen *result_loc;
1952019535 if (handle_is_ptr(impl_fn_type_id->return_type)) {
1952119536 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);
1952319538 if (result_loc != nullptr) {
1952419539 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
1952519540 return result_loc;
......@@ -19589,7 +19604,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1958919604 return ira->codegen->invalid_inst_gen;
1959019605 }
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);
1959319608 if (type_is_invalid(casted_arg->value->type))
1959419609 return ira->codegen->invalid_inst_gen;
1959519610
......@@ -19629,7 +19644,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1962919644 return ira->codegen->invalid_inst_gen;
1963019645 }
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,
1963319648 is_async_call_builtin, fn_entry);
1963419649 if (casted_new_stack != nullptr && type_is_invalid(casted_new_stack->value->type))
1963519650 return ira->codegen->invalid_inst_gen;
......@@ -19651,7 +19666,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1965119666 IrInstGen *result_loc;
1965219667 if (handle_is_ptr(return_type)) {
1965319668 result_loc = ir_resolve_result(ira, source_instr, call_result_loc,
19654 return_type, nullptr, true, true, false);
19669 return_type, nullptr, true, false);
1965519670 if (result_loc != nullptr) {
1965619671 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
1965719672 return result_loc;
......@@ -19685,13 +19700,15 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1968519700
1968619701static IrInstGen *ir_analyze_fn_call_src(IrAnalyze *ira, IrInstSrcCall *call_instruction,
1968719702 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)
1968919704{
1969019705 IrInstGen *new_stack = nullptr;
19706 IrInst *new_stack_src = nullptr;
1969119707 if (call_instruction->new_stack) {
1969219708 new_stack = call_instruction->new_stack->child;
1969319709 if (type_is_invalid(new_stack->value->type))
1969419710 return ira->codegen->invalid_inst_gen;
19711 new_stack_src = &call_instruction->new_stack->base;
1969519712 }
1969619713 IrInstGen **args_ptr = allocate<IrInstGen *>(call_instruction->arg_count, "IrInstGen *");
1969719714 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
1970619723 return ira->codegen->invalid_inst_gen;
1970719724 }
1970819725 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,
19710 args_ptr, call_instruction->arg_count, ret_ptr, call_instruction->result_loc);
19726 first_arg_ptr, first_arg_ptr_src, modifier, new_stack, new_stack_src,
19727 call_instruction->is_async_call_builtin, args_ptr, call_instruction->arg_count, ret_ptr,
19728 call_instruction->result_loc);
1971119729 deallocate(args_ptr, call_instruction->arg_count, "IrInstGen *");
1971219730 return result;
1971319731}
......@@ -19758,12 +19776,14 @@ static IrInstGen *ir_analyze_call_extra(IrAnalyze *ira, IrInst* source_instr,
1975819776 }
1975919777
1976019778 IrInstGen *first_arg_ptr = nullptr;
19779 IrInst *first_arg_ptr_src = nullptr;
1976119780 ZigFn *fn = nullptr;
1976219781 if (instr_is_comptime(fn_ref)) {
1976319782 if (fn_ref->value->type->id == ZigTypeIdBoundFn) {
1976419783 assert(fn_ref->value->special == ConstValSpecialStatic);
1976519784 fn = fn_ref->value->data.x_bound_fn.fn;
1976619785 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;
1976719787 if (type_is_invalid(first_arg_ptr->value->type))
1976819788 return ira->codegen->invalid_inst_gen;
1976919789 } else {
......@@ -19805,8 +19825,8 @@ static IrInstGen *ir_analyze_call_extra(IrAnalyze *ira, IrInst* source_instr,
1980519825 return ira->codegen->invalid_inst_gen;
1980619826 }
1980719827
19808 return ir_analyze_fn_call(ira, source_instr, fn, fn_type, fn_ref, first_arg_ptr,
19809 modifier, stack, false, args_ptr, args_len, nullptr, result_loc);
19828 return ir_analyze_fn_call(ira, source_instr, fn, fn_type, fn_ref, first_arg_ptr, first_arg_ptr_src,
19829 modifier, stack, &stack->base, false, args_ptr, args_len, nullptr, result_loc);
1981019830}
1981119831
1981219832static IrInstGen *ir_analyze_instruction_call_extra(IrAnalyze *ira, IrInstSrcCallExtra *instruction) {
......@@ -19881,14 +19901,15 @@ static IrInstGen *ir_analyze_instruction_call(IrAnalyze *ira, IrInstSrcCall *cal
1988119901 ZigType *fn_type = fn_table_entry ? fn_table_entry->type_entry : fn_ref->value->type;
1988219902 CallModifier modifier = is_comptime ? CallModifierCompileTime : call_instruction->modifier;
1988319903 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);
1988519905 } else if (fn_ref->value->type->id == ZigTypeIdBoundFn) {
1988619906 assert(fn_ref->value->special == ConstValSpecialStatic);
1988719907 ZigFn *fn_table_entry = fn_ref->value->data.x_bound_fn.fn;
1988819908 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;
1988919910 CallModifier modifier = is_comptime ? CallModifierCompileTime : call_instruction->modifier;
1989019911 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);
1989219913 } else {
1989319914 ir_add_error(ira, &fn_ref->base,
1989419915 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
1989819919
1989919920 if (fn_ref->value->type->id == ZigTypeIdFn) {
1990019921 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);
1990219923 } else {
1990319924 ir_add_error(ira, &fn_ref->base,
1990419925 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
2032420345
2032520346 // In case resolving the parent activates a suspend, do it now
2032620347 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);
2032820349 if (parent_result_loc != nullptr &&
2032920350 (type_is_invalid(parent_result_loc->value->type) || parent_result_loc->value->type->id == ZigTypeIdUnreachable))
2033020351 {
......@@ -20713,10 +20734,12 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
2071320734 return_type = adjust_ptr_align(ira->codegen, return_type, chosen_align);
2071420735 }
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.
2071620740 if (orig_array_ptr_val->special != ConstValSpecialRuntime &&
2071720741 orig_array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&
20718 (orig_array_ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar ||
20719 array_type->id == ZigTypeIdArray))
20742 (orig_array_ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar || array_type->id == ZigTypeIdArray))
2072020743 {
2072120744 ZigValue *array_ptr_val = const_ptr_pointee(ira, ira->codegen, orig_array_ptr_val,
2072220745 elem_ptr_instruction->base.base.source_node);
......@@ -20781,6 +20804,11 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
2078120804 (array_type->id != ZigTypeIdPointer ||
2078220805 array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr))
2078320806 {
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 }
2078420812 if (array_type->id == ZigTypeIdPointer) {
2078520813 IrInstGen *result = ir_const(ira, &elem_ptr_instruction->base.base, return_type);
2078620814 ZigValue *out_val = result->value;
......@@ -20989,7 +21017,7 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
2098921017
2099021018static IrInstGen *ir_analyze_container_member_access_inner(IrAnalyze *ira,
2099121019 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)
2099321021{
2099421022 if (!is_slice(bare_struct_type)) {
2099521023 ScopeDecls *container_scope = get_container_scope(bare_struct_type);
......@@ -21010,7 +21038,8 @@ static IrInstGen *ir_analyze_container_member_access_inner(IrAnalyze *ira,
2101021038 if (type_is_invalid(fn_entry->type_entry))
2101121039 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);
2101421043 return ir_get_ref(ira, source_instr, bound_fn_value, true, false);
2101521044 } else if (tld->id == TldIdVar) {
2101621045 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,
2102921058 if (var->const_value->type->id == ZigTypeIdFn) {
2103021059 ir_assert(var->const_value->data.x_ptr.special == ConstPtrSpecialFunction, source_instr);
2103121060 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);
2103321063 return ir_get_ref(ira, source_instr, bound_fn_value, true, false);
2103421064 }
2103521065 }
......@@ -21075,7 +21105,7 @@ static IrInstGen *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInst* source_ins
2107521105 IrInstGen *elem = ir_const(ira, source_instr, field_type);
2107621106 memoize_field_init_val(ira->codegen, struct_type, field);
2107721107 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);
2107921109 }
2108021110 switch (type_has_one_possible_value(ira->codegen, field_type)) {
2108121111 case OnePossibleValueInvalid:
......@@ -21193,7 +21223,8 @@ static IrInstGen *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_name,
2119321223}
2119421224
2119521225static 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)
2119721228{
2119821229 Error err;
2119921230
......@@ -21215,13 +21246,13 @@ static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name
2121521246 return ir_analyze_struct_field_ptr(ira, source_instr, field, container_ptr, bare_type, initializing);
2121621247 } else {
2121721248 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);
2121921250 }
2122021251 }
2122121252
2122221253 if (bare_type->id == ZigTypeIdEnum) {
2122321254 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);
2122521256 }
2122621257
2122721258 if (bare_type->id == ZigTypeIdUnion) {
......@@ -21231,7 +21262,7 @@ static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name
2123121262 TypeUnionField *field = find_union_type_field(bare_type, field_name);
2123221263 if (field == nullptr) {
2123321264 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);
2123521266 }
2123621267
2123721268 ZigType *field_type = resolve_union_field_type(ira->codegen, field);
......@@ -21425,10 +21456,14 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel
2142521456 if (container_type->id == ZigTypeIdPointer) {
2142621457 ZigType *bare_type = container_ref_type(container_type);
2142721458 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);
2142921462 return result;
2143021463 } 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);
2143221467 return result;
2143321468 }
2143421469 } 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
2211622151static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* source_instr,
2211722152 IrInstGen *base_ptr, bool safety_check_on, bool initializing)
2211822153{
22154 Error err;
22155
2211922156 ZigType *type_entry = get_ptr_elem_type(ira->codegen, base_ptr);
2212022157 if (type_is_invalid(type_entry))
2212122158 return ira->codegen->invalid_inst_gen;
......@@ -22161,7 +22198,7 @@ static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* sou
2216122198
2216222199 if (instr_is_comptime(base_ptr)) {
2216322200 ZigValue *ptr_val = ir_resolve_const(ira, base_ptr, UndefBad);
22164 if (!ptr_val)
22201 if (ptr_val == nullptr)
2216522202 return ira->codegen->invalid_inst_gen;
2216622203 if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
2216722204 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
2218522222 }
2218622223 break;
2218722224 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
2219422225 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);
2219622227 break;
2219722228 }
2219822229 }
22199 } else if (optional_value_is_null(optional_val)) {
22200 ir_add_error(ira, source_instr, buf_sprintf("unable to unwrap null"));
22201 return ira->codegen->invalid_inst_gen;
22230 } else {
22231 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec,
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 }
2220222238 }
2220322239
2220422240 IrInstGen *result;
......@@ -22697,8 +22733,8 @@ static IrInstGen *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstSrcSwi
2269722733 ref_type->data.pointer.explicit_alignment,
2269822734 ref_type->data.pointer.bit_offset_in_host, ref_type->data.pointer.host_int_bytes,
2269922735 ref_type->data.pointer.allow_zero);
22700 return ir_analyze_ptr_cast(ira, &instruction->base.base, target_value_ptr, new_target_value_ptr_type,
22701 &instruction->base.base, false);
22736 return ir_analyze_ptr_cast(ira, &instruction->base.base, target_value_ptr,
22737 &instruction->target_value_ptr->base, new_target_value_ptr_type, &instruction->base.base, false);
2270222738 } else {
2270322739 ir_add_error(ira, &instruction->base.base,
2270422740 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,
2277822814 ref_type->data.pointer.explicit_alignment,
2277922815 ref_type->data.pointer.bit_offset_in_host, ref_type->data.pointer.host_int_bytes,
2278022816 ref_type->data.pointer.allow_zero);
22781 return ir_analyze_ptr_cast(ira, &instruction->base.base, target_value_ptr, new_target_value_ptr_type,
22782 &instruction->base.base, false);
22817 return ir_analyze_ptr_cast(ira, &instruction->base.base, target_value_ptr,
22818 &instruction->target_value_ptr->base, new_target_value_ptr_type, &instruction->base.base, false);
2278322819 }
2278422820
2278522821 return target_value_ptr;
......@@ -24775,11 +24811,18 @@ static IrInstGen *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstSrcCImpo
2477524811
2477624812 // Execute the C import block like an inline function
2477724813 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,
2477924818 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 }
2478124823 if (type_is_invalid(cimport_result->type))
2478224824 return ira->codegen->invalid_inst_gen;
24825 destroy(result_ptr, "ZigValue");
2478324826
2478424827 ZigPackage *cur_scope_pkg = scope_package(instruction->base.base.scope);
2478524828 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
2506825111
2506925112 // TODO let this be volatile
2507025113 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);
2507225115 if (type_is_invalid(casted_ptr->value->type))
2507325116 return ira->codegen->invalid_inst_gen;
2507425117
......@@ -25096,11 +25139,11 @@ static IrInstGen *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstSrcCmpxch
2509625139 if (!ir_resolve_atomic_order(ira, failure_order_value, &failure_order))
2509725140 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);
2510025143 if (type_is_invalid(casted_cmp_value->value->type))
2510125144 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);
2510425147 if (type_is_invalid(casted_new_value->value->type))
2510525148 return ira->codegen->invalid_inst_gen;
2510625149
......@@ -25134,7 +25177,7 @@ static IrInstGen *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstSrcCmpxch
2513425177 IrInstGen *result_loc;
2513525178 if (handle_is_ptr(result_type)) {
2513625179 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);
2513825181 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
2513925182 return result_loc;
2514025183 }
......@@ -25191,7 +25234,7 @@ static IrInstGen *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstSrcTrunc
2519125234 }
2519225235
2519325236 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);
2519525238 }
2519625239
2519725240 if (instr_is_comptime(target)) {
......@@ -25245,7 +25288,7 @@ static IrInstGen *ir_analyze_instruction_int_cast(IrAnalyze *ira, IrInstSrcIntCa
2524525288 }
2524625289
2524725290 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);
2524925292 }
2525025293
2525125294 if (dest_type->id == ZigTypeIdComptimeInt) {
......@@ -25373,7 +25416,7 @@ static IrInstGen *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstSrcFro
2537325416 src_ptr_align, 0, 0, false);
2537425417 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);
2537725420 if (type_is_invalid(casted_value->value->type))
2537825421 return ira->codegen->invalid_inst_gen;
2537925422
......@@ -25393,7 +25436,7 @@ static IrInstGen *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstSrcFro
2539325436 }
2539425437
2539525438 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);
2539725440 if (result_loc != nullptr && (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable)) {
2539825441 return result_loc;
2539925442 }
......@@ -25478,7 +25521,7 @@ static IrInstGen *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstSrcToByt
2547825521 }
2547925522
2548025523 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);
2548225525 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
2548325526 return result_loc;
2548425527 }
......@@ -26508,7 +26551,7 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2650826551 }
2650926552
2651026553 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);
2651226555 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
2651326556 return result_loc;
2651426557 }
......@@ -27662,7 +27705,7 @@ static IrInstGen *ir_align_cast(IrAnalyze *ira, IrInstGen *target, uint32_t alig
2766227705}
2766327706
2766427707static 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)
2766627709{
2766727710 Error err;
2766827711
......@@ -27678,7 +27721,7 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
2767827721
2767927722 ZigType *src_ptr_type = get_src_ptr_type(src_type);
2768027723 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)));
2768227725 return ira->codegen->invalid_inst_gen;
2768327726 }
2768427727
......@@ -27707,11 +27750,11 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
2770727750 if ((err = type_resolve(ira->codegen, src_type, ResolveStatusZeroBitsKnown)))
2770827751 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) {
2771127754 ErrorMsg *msg = ir_add_error(ira, source_instr,
2771227755 buf_sprintf("'%s' and '%s' do not have the same in-memory representation",
2771327756 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,
2771527758 buf_sprintf("'%s' has no in-memory bits", buf_ptr(&src_type->name)));
2771627759 add_error_note(ira->codegen, msg, dest_type_src->source_node,
2771727760 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
2772227765 bool dest_allows_addr_zero = ptr_allows_addr_zero(dest_type);
2772327766 UndefAllowed is_undef_allowed = dest_allows_addr_zero ? UndefOk : UndefBad;
2772427767 ZigValue *val = ir_resolve_const(ira, ptr, is_undef_allowed);
27725 if (!val)
27768 if (val == nullptr)
2772627769 return ira->codegen->invalid_inst_gen;
2772727770
2772827771 if (value_is_comptime(val) && val->special != ConstValSpecialUndef) {
......@@ -27737,15 +27780,31 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
2773727780 }
2773827781
2773927782 IrInstGen *result;
27740 if (ptr->value->data.x_ptr.mut == ConstPtrMutInfer) {
27783 if (val->data.x_ptr.mut == ConstPtrMutInfer) {
2774127784 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;
2774527785 } else {
2774627786 result = ir_const(ira, source_instr, dest_type);
2774727787 }
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 }
2774927808 result->value->type = dest_type;
2775027809
2775127810 // Keep the bigger alignment, it can only help-
......@@ -27759,7 +27818,7 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
2775927818
2776027819 if (dest_align_bytes > src_align_bytes) {
2776127820 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,
2776327822 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&src_type->name), src_align_bytes));
2776427823 add_error_note(ira->codegen, msg, dest_type_src->source_node,
2776527824 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
2779227851 if (type_is_invalid(src_type))
2779327852 return ira->codegen->invalid_inst_gen;
2779427853
27795 return ir_analyze_ptr_cast(ira, &instruction->base.base, ptr, dest_type, &dest_type_value->base,
27796 instruction->safety_check_on);
27854 return ir_analyze_ptr_cast(ira, &instruction->base.base, ptr, &instruction->ptr->base,
27855 dest_type, &dest_type_value->base, instruction->safety_check_on);
2779727856}
2779827857
2779927858static 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
2920629265
2920729266 bool was_written = instruction->result_loc->written;
2920829267 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);
2921029269 if (result_loc != nullptr) {
2921129270 if (type_is_invalid(result_loc->value->type))
2921229271 return ira->codegen->invalid_inst_gen;
......@@ -29240,11 +29299,6 @@ static IrInstGen *ir_analyze_instruction_implicit_cast(IrAnalyze *ira, IrInstSrc
2924029299 if (type_is_invalid(operand->value->type))
2924129300 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
2924829302 ZigType *dest_type = ir_resolve_type(ira, instruction->result_loc_cast->base.source_instruction->child);
2924929303 if (type_is_invalid(dest_type))
2925029304 return ira->codegen->invalid_inst_gen;
......@@ -29257,15 +29311,18 @@ static IrInstGen *ir_analyze_instruction_bit_cast_src(IrAnalyze *ira, IrInstSrcB
2925729311 return operand;
2925829312
2925929313 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base,
29260 &instruction->result_loc_bit_cast->base, operand->value->type, operand, false, false, true);
29261 if (result_loc != nullptr && (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable))
29314 &instruction->result_loc_bit_cast->base, operand->value->type, operand, false, true);
29315 if (result_loc != nullptr &&
29316 (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable))
29317 {
2926229318 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;
2926629319 }
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);
2926929326}
2927029327
2927129328static IrInstGen *ir_analyze_instruction_union_init_named_field(IrAnalyze *ira,
......@@ -29392,9 +29449,12 @@ static IrInstGen *ir_analyze_instruction_await(IrAnalyze *ira, IrInstSrcAwait *i
2939229449 IrInstGen *result_loc;
2939329450 if (type_has_bits(result_type)) {
2939429451 result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
29395 result_type, nullptr, true, true, true);
29396 if (result_loc != nullptr && (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable))
29452 result_type, nullptr, true, true);
29453 if (result_loc != nullptr &&
29454 (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable))
29455 {
2939729456 return result_loc;
29457 }
2939829458 } else {
2939929459 result_loc = nullptr;
2940029460 }
......@@ -29421,7 +29481,7 @@ static IrInstGen *ir_analyze_instruction_resume(IrAnalyze *ira, IrInstSrcResume
2942129481 }
2942229482
2942329483 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);
2942529485 if (type_is_invalid(casted_frame->value->type))
2942629486 return ira->codegen->invalid_inst_gen;
2942729487
......@@ -29747,7 +29807,7 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc
2974729807// This function attempts to evaluate IR code while doing type checking and other analysis.
2974829808// It emits to a new IrExecutableGen which is partially evaluated IR code.
2974929809ZigType *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)
2975129811{
2975229812 assert(old_exec->first_err_trace_msg == nullptr);
2975329813 assert(expected_type == nullptr || !type_is_invalid(expected_type));
......@@ -29766,12 +29826,6 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutableSrc *old_exec, IrExecutableGen
2976629826 ira->new_irb.codegen = codegen;
2976729827 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
2977529829 IrBasicBlockSrc *old_entry_bb = ira->old_irb.exec->basic_block_list.at(0);
2977629830 IrBasicBlockGen *new_entry_bb = ir_get_new_bb(ira, old_entry_bb, nullptr);
2977729831 ir_ref_bb_gen(new_entry_bb);
......@@ -29780,6 +29834,19 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutableSrc *old_exec, IrExecutableGen
2978029834
2978129835 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
2978329850 while (ira->old_bb_index < ira->old_irb.exec->basic_block_list.length) {
2978429851 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) {
2992229989 case IrInstGenIdReturnPtr:
2992329990 case IrInstGenIdStructFieldPtr:
2992429991 case IrInstGenIdTestNonNull:
29925 case IrInstGenIdOptionalUnwrapPtr:
2992629992 case IrInstGenIdClz:
2992729993 case IrInstGenIdCtz:
2992829994 case IrInstGenIdPopCount:
......@@ -29979,6 +30045,8 @@ bool ir_inst_gen_has_side_effects(IrInstGen *instruction) {
2997930045 return reinterpret_cast<IrInstGenUnwrapErrCode *>(instruction)->initializing;
2998030046 case IrInstGenIdUnionFieldPtr:
2998130047 return reinterpret_cast<IrInstGenUnionFieldPtr *>(instruction)->initializing;
30048 case IrInstGenIdOptionalUnwrapPtr:
30049 return reinterpret_cast<IrInstGenOptionalUnwrapPtr *>(instruction)->initializing;
2998230050 case IrInstGenIdErrWrapPayload:
2998330051 return reinterpret_cast<IrInstGenErrWrapPayload *>(instruction)->result_loc != nullptr;
2998430052 case IrInstGenIdErrWrapCode:
src/ir.hpp+3-3
......@@ -16,15 +16,15 @@ bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);
1616IrInstGen *ir_create_alloca(CodeGen *g, Scope *scope, AstNode *source_node, ZigFn *fn,
1717 ZigType *var_type, const char *name_hint);
1818
19ZigValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
20 ZigType *expected_type, size_t *backward_branch_count, size_t *backward_branch_quota,
19Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
20 ZigValue *return_ptr, size_t *backward_branch_count, size_t *backward_branch_quota,
2121 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
2222 IrExecutableGen *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef);
2323
2424Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ZigValue *val);
2525
2626ZigType *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
2929bool ir_inst_gen_has_side_effects(IrInstGen *inst);
3030bool ir_inst_src_has_side_effects(IrInstSrc *inst);
src/softfloat.hpp+17
......@@ -12,4 +12,21 @@ extern "C" {
1212#include "softfloat.h"
1313}
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
1532#endif
src/util.hpp+2-19
......@@ -38,6 +38,8 @@
3838
3939#if defined(__MINGW32__) || defined(__MINGW64__)
4040#define BREAKPOINT __debugbreak()
41#elif defined(__i386__) || defined(__x86_64__)
42#define BREAKPOINT __asm__ volatile("int $0x03");
4143#elif defined(__clang__)
4244#define BREAKPOINT __builtin_debugtrap()
4345#elif defined(__GNUC__)
......@@ -49,8 +51,6 @@
4951
5052#endif
5153
52#include "softfloat.hpp"
53
5454ATTRIBUTE_COLD
5555ATTRIBUTE_NORETURN
5656ATTRIBUTE_PRINTF(1, 2)
......@@ -244,23 +244,6 @@ static inline uint8_t log2_u64(uint64_t x) {
244244 return (63 - clzll(x));
245245}
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
264247void zig_pretty_print_bytes(FILE *f, double n);
265248
266249template<typename T>
test/compile_errors.zig+6-7
......@@ -1658,7 +1658,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
16581658 cases.addTest("return invalid type from test",
16591659 \\test "example" { return 1; }
16601660 , &[_][]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'",
16621662 });
16631663
16641664 cases.add("threadlocal qualifier on const",
......@@ -2487,7 +2487,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
24872487 \\ var rule_set = try Foo.init();
24882488 \\}
24892489 , &[_][]const u8{
2490 "tmp.zig:2:10: error: expected type 'i32', found 'type'",
2490 "tmp.zig:2:19: error: expected type 'i32', found 'type'",
24912491 });
24922492
24932493 cases.add("slicing single-item pointer",
......@@ -3393,7 +3393,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
33933393 \\
33943394 \\fn b() void {}
33953395 , &[_][]const u8{
3396 "tmp.zig:3:6: error: unreachable code",
3396 "tmp.zig:3:5: error: unreachable code",
33973397 });
33983398
33993399 cases.add("bad import",
......@@ -4011,8 +4011,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
40114011 \\
40124012 \\export fn entry() usize { return @sizeOf(@TypeOf(Foo)); }
40134013 , &[_][]const u8{
4014 "tmp.zig:5:25: error: unable to evaluate constant expression",
4015 "tmp.zig:2:12: note: referenced here",
4014 "tmp.zig:5:25: error: cannot store runtime value in compile time variable",
4015 "tmp.zig:2:12: note: called from here",
40164016 });
40174017
40184018 cases.add("addition with non numbers",
......@@ -4652,7 +4652,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
46524652 \\fn something() anyerror!void { }
46534653 , &[_][]const u8{
46544654 "tmp.zig:2:5: error: expected type 'void', found 'anyerror'",
4655 "tmp.zig:1:15: note: return type declared here",
46564655 });
46574656
46584657 cases.add("invalid pointer for var type",
......@@ -5743,7 +5742,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
57435742 \\ @export(entry, .{.name = "entry", .linkage = @as(u32, 1234) });
57445743 \\}
57455744 , &[_][]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'",
57475746 });
57485747
57495748 cases.add("struct with invalid field",
test/stage1/behavior/bitcast.zig+20
......@@ -167,3 +167,23 @@ test "nested bitcast" {
167167 S.foo(42);
168168 comptime S.foo(42);
169169}
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" {
804804 expectEqual(20, x.?);
805805 }
806806}
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" {
7272
7373 var t = true;
7474 const x = if (t) Num.Two else unreachable;
75 if (x != .Two) @compileError("bad");
75 expect(x == .Two);
7676}
7777
7878test "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" {
781781 std.mem.copy(u8, buffer[0..], s);
782782 std.testing.expectEqualSlices(u8, buffer[0..], s);
783783}
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" {
153153 var x = Foo{ .x = null };
154154 expect(x.x == null);
155155}
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 @@
1const expect = @import("std").testing.expect;
2const mem = @import("std").mem;
1const std = @import("std");
2const expect = std.testing.expect;
3const mem = std.mem;
34
45fn initStaticArray() [10]i32 {
56 var array: [10]i32 = undefined;