authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-10 11:15:32-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-10 11:15:32-04:00
log1a51bf63047e9a3cd6ae3273296fded82009235c
treea0ed33775740c091baad17c8dffdc5c9c272cefb
parent4e2b2822f18577edb614bdc3ec6808a0587662e5
signature Commit is signed but in an unrecognized format.

hook up result locations for union initializations

```zig export fn entry() void { var x = Foo{ .bar = bar() }; } ``` ```llvm define void @entry() #2 !dbg !44 { Entry: %x = alloca %Foo, align 4 %0 = getelementptr inbounds %Foo, %Foo* %x, i32 0, i32 1, !dbg !68 store i1 true, i1* %0, align 1, !dbg !68 %1 = getelementptr inbounds %Foo, %Foo* %x, i32 0, i32 0, !dbg !68 %2 = bitcast { i32, [4 x i8] }* %1 to %Bar*, !dbg !68 call fastcc void @bar(%Bar* sret %2), !dbg !68 call void @llvm.dbg.declare(metadata %Foo* %x, metadata !48, metadata !DIExpression()), !dbg !69 ret void, !dbg !70 } ```

5 files changed, 56 insertions(+), 128 deletions(-)

BRANCH_TODO-1
......@@ -1,7 +1,6 @@
11Scratch pad for stuff to do before merging master
22=================================================
33
4 * union initializations
54 * bitCast
65
76look at all the ir_gen_node ir_gen_node_extra calls and make sure result locations are properly propagated
src/all_types.hpp+2-11
......@@ -2204,7 +2204,6 @@ enum IrInstructionId {
22042204 IrInstructionIdResizeSlice,
22052205 IrInstructionIdContainerInitList,
22062206 IrInstructionIdContainerInitFields,
2207 IrInstructionIdUnionInit,
22082207 IrInstructionIdUnreachable,
22092208 IrInstructionIdTypeOf,
22102209 IrInstructionIdSetCold,
......@@ -2520,6 +2519,7 @@ struct IrInstructionStorePtr {
25202519struct IrInstructionFieldPtr {
25212520 IrInstruction base;
25222521
2522 bool initializing;
25232523 IrInstruction *container_ptr;
25242524 Buf *field_name_buffer;
25252525 IrInstruction *field_name_expr;
......@@ -2536,9 +2536,9 @@ struct IrInstructionStructFieldPtr {
25362536struct IrInstructionUnionFieldPtr {
25372537 IrInstruction base;
25382538
2539 bool initializing;
25392540 IrInstruction *union_ptr;
25402541 TypeUnionField *field;
2541 bool is_const;
25422542};
25432543
25442544struct IrInstructionElemPtr {
......@@ -2663,15 +2663,6 @@ struct IrInstructionContainerInitFields {
26632663 IrInstructionContainerInitFieldsField *fields;
26642664};
26652665
2666struct IrInstructionUnionInit {
2667 IrInstruction base;
2668
2669 ZigType *union_type;
2670 TypeUnionField *field;
2671 IrInstruction *init_value;
2672 LLVMValueRef tmp_ptr;
2673};
2674
26752666struct IrInstructionUnreachable {
26762667 IrInstruction base;
26772668};
src/codegen.cpp+6-43
......@@ -3859,7 +3859,12 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
38593859 return bitcasted_union_field_ptr;
38603860 }
38613861
3862 if (ir_want_runtime_safety(g, &instruction->base)) {
3862 if (instruction->initializing) {
3863 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr, union_type->data.unionation.gen_tag_index, "");
3864 LLVMValueRef tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
3865 &field->enum_field->value);
3866 gen_store_untyped(g, tag_value, tag_field_ptr, 0, false);
3867 } else if (ir_want_runtime_safety(g, &instruction->base)) {
38633868 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr, union_type->data.unionation.gen_tag_index, "");
38643869 LLVMValueRef tag_value = gen_load_untyped(g, tag_field_ptr, 0, false, "");
38653870
......@@ -5035,43 +5040,6 @@ static LLVMValueRef ir_render_union_tag(CodeGen *g, IrExecutable *executable, Ir
50355040 return get_handle_value(g, tag_field_ptr, tag_type, ptr_type);
50365041}
50375042
5038static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, IrInstructionUnionInit *instruction) {
5039 TypeUnionField *type_union_field = instruction->field;
5040
5041 if (!type_has_bits(type_union_field->type_entry))
5042 return nullptr;
5043
5044 uint32_t field_align_bytes = get_abi_alignment(g, type_union_field->type_entry);
5045 ZigType *ptr_type = get_pointer_to_type_extra(g, type_union_field->type_entry,
5046 false, false, PtrLenSingle, field_align_bytes,
5047 0, 0, false);
5048
5049 LLVMValueRef uncasted_union_ptr;
5050 // Even if safety is off in this block, if the union type has the safety field, we have to populate it
5051 // correctly. Otherwise safety code somewhere other than here could fail.
5052 ZigType *union_type = instruction->union_type;
5053 if (union_type->data.unionation.gen_tag_index != SIZE_MAX) {
5054 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
5055 union_type->data.unionation.gen_tag_index, "");
5056
5057 LLVMValueRef tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
5058 &type_union_field->enum_field->value);
5059 gen_store_untyped(g, tag_value, tag_field_ptr, 0, false);
5060
5061 uncasted_union_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
5062 (unsigned)union_type->data.unionation.gen_union_index, "");
5063 } else {
5064 uncasted_union_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, (unsigned)0, "");
5065 }
5066
5067 LLVMValueRef field_ptr = LLVMBuildBitCast(g->builder, uncasted_union_ptr, get_llvm_type(g, ptr_type), "");
5068 LLVMValueRef value = ir_llvm_value(g, instruction->init_value);
5069
5070 gen_assign_raw(g, field_ptr, ptr_type, value);
5071
5072 return instruction->tmp_ptr;
5073}
5074
50755043static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInstructionPanic *instruction) {
50765044 gen_panic(g, ir_llvm_value(g, instruction->msg), get_cur_err_ret_trace_val(g, instruction->base.scope));
50775045 return nullptr;
......@@ -5659,8 +5627,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
56595627 return ir_render_err_wrap_payload(g, executable, (IrInstructionErrWrapPayload *)instruction);
56605628 case IrInstructionIdUnionTag:
56615629 return ir_render_union_tag(g, executable, (IrInstructionUnionTag *)instruction);
5662 case IrInstructionIdUnionInit:
5663 return ir_render_union_init(g, executable, (IrInstructionUnionInit *)instruction);
56645630 case IrInstructionIdPtrCastGen:
56655631 return ir_render_ptr_cast(g, executable, (IrInstructionPtrCastGen *)instruction);
56665632 case IrInstructionIdBitCastGen:
......@@ -6874,9 +6840,6 @@ static void do_code_gen(CodeGen *g) {
68746840 } else if (instruction->id == IrInstructionIdContainerInitList) {
68756841 IrInstructionContainerInitList *container_init_list_instruction = (IrInstructionContainerInitList *)instruction;
68766842 slot = &container_init_list_instruction->tmp_ptr;
6877 } else if (instruction->id == IrInstructionIdUnionInit) {
6878 IrInstructionUnionInit *union_init_instruction = (IrInstructionUnionInit *)instruction;
6879 slot = &union_init_instruction->tmp_ptr;
68806843 } else if (instruction->id == IrInstructionIdSlice) {
68816844 IrInstructionSlice *slice_instruction = (IrInstructionSlice *)instruction;
68826845 slot = &slice_instruction->tmp_ptr;
src/ir.cpp+48-61
......@@ -164,7 +164,7 @@ static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, Zig
164164static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *ptr);
165165static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, Buf *msg);
166166static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,
167 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type);
167 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type, bool initializing);
168168static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction, ZigVar *var);
169169static ZigType *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstruction *op);
170170static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval, ResultLoc *result_loc);
......@@ -616,10 +616,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionRef *) {
616616 return IrInstructionIdRef;
617617}
618618
619static constexpr IrInstructionId ir_instruction_id(IrInstructionUnionInit *) {
620 return IrInstructionIdUnionInit;
621}
622
623619static constexpr IrInstructionId ir_instruction_id(IrInstructionCompileErr *) {
624620 return IrInstructionIdCompileErr;
625621}
......@@ -1312,12 +1308,13 @@ static IrInstruction *ir_build_field_ptr_instruction(IrBuilder *irb, Scope *scop
13121308}
13131309
13141310static IrInstruction *ir_build_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
1315 IrInstruction *container_ptr, Buf *field_name)
1311 IrInstruction *container_ptr, Buf *field_name, bool initializing)
13161312{
13171313 IrInstructionFieldPtr *instruction = ir_build_instruction<IrInstructionFieldPtr>(irb, scope, source_node);
13181314 instruction->container_ptr = container_ptr;
13191315 instruction->field_name_buffer = field_name;
13201316 instruction->field_name_expr = nullptr;
1317 instruction->initializing = initializing;
13211318
13221319 ir_ref_instruction(container_ptr, irb->current_basic_block);
13231320
......@@ -1337,9 +1334,10 @@ static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, Scope *scope, As
13371334}
13381335
13391336static IrInstruction *ir_build_union_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
1340 IrInstruction *union_ptr, TypeUnionField *field)
1337 IrInstruction *union_ptr, TypeUnionField *field, bool initializing)
13411338{
13421339 IrInstructionUnionFieldPtr *instruction = ir_build_instruction<IrInstructionUnionFieldPtr>(irb, scope, source_node);
1340 instruction->initializing = initializing;
13431341 instruction->union_ptr = union_ptr;
13441342 instruction->field = field;
13451343
......@@ -1517,19 +1515,6 @@ static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, Scope *scop
15171515 return &container_init_fields_instruction->base;
15181516}
15191517
1520static IrInstruction *ir_build_union_init(IrBuilder *irb, Scope *scope, AstNode *source_node,
1521 ZigType *union_type, TypeUnionField *field, IrInstruction *init_value)
1522{
1523 IrInstructionUnionInit *union_init_instruction = ir_build_instruction<IrInstructionUnionInit>(irb, scope, source_node);
1524 union_init_instruction->union_type = union_type;
1525 union_init_instruction->field = field;
1526 union_init_instruction->init_value = init_value;
1527
1528 ir_ref_instruction(init_value, irb->current_basic_block);
1529
1530 return &union_init_instruction->base;
1531}
1532
15331518static IrInstruction *ir_build_unreachable(IrBuilder *irb, Scope *scope, AstNode *source_node) {
15341519 IrInstructionUnreachable *unreachable_instruction =
15351520 ir_build_instruction<IrInstructionUnreachable>(irb, scope, source_node);
......@@ -4121,7 +4106,7 @@ static IrInstruction *ir_gen_field_access(IrBuilder *irb, Scope *scope, AstNode
41214106 if (container_ref_instruction == irb->codegen->invalid_instruction)
41224107 return container_ref_instruction;
41234108
4124 return ir_build_field_ptr(irb, scope, node, container_ref_instruction, field_name);
4109 return ir_build_field_ptr(irb, scope, node, container_ref_instruction, field_name, false);
41254110}
41264111
41274112static IrInstruction *ir_gen_overflow_op(IrBuilder *irb, Scope *scope, AstNode *node, IrOverflowOp op) {
......@@ -5594,7 +5579,7 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
55945579 AstNode *expr_node = entry_node->data.struct_val_field.expr;
55955580
55965581 IrInstruction *field_ptr = ir_build_field_ptr(irb, &result_loc->scope_elide->base, expr_node,
5597 container_ptr, name);
5582 container_ptr, name, true);
55985583 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
55995584 result_loc_inst->base.id = ResultLocIdInstruction;
56005585 result_loc_inst->base.source_instruction = field_ptr;
......@@ -6105,7 +6090,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
61056090 IrBasicBlock *continue_block = ir_create_basic_block(irb, parent_scope, "ForContinue");
61066091
61076092 Buf *len_field_name = buf_create_from_str("len");
6108 IrInstruction *len_ref = ir_build_field_ptr(irb, parent_scope, node, array_val_ptr, len_field_name);
6093 IrInstruction *len_ref = ir_build_field_ptr(irb, parent_scope, node, array_val_ptr, len_field_name, false);
61096094 IrInstruction *len_val = ir_build_load_ptr(irb, parent_scope, node, len_ref);
61106095 ir_build_br(irb, parent_scope, node, cond_block, is_comptime);
61116096
......@@ -7496,7 +7481,7 @@ static IrInstruction *ir_gen_cancel_target(IrBuilder *irb, Scope *scope, AstNode
74967481 IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, scope, node, casted_target_inst);
74977482 Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME);
74987483 IrInstruction *atomic_state_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr,
7499 atomic_state_field_name);
7484 atomic_state_field_name, false);
75007485
75017486 // set the is_canceled bit
75027487 IrInstruction *prev_atomic_value = ir_build_atomic_rmw(irb, scope, node,
......@@ -7575,7 +7560,7 @@ static IrInstruction *ir_gen_resume_target(IrBuilder *irb, Scope *scope, AstNode
75757560 IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, scope, node, casted_target_inst);
75767561 Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME);
75777562 IrInstruction *atomic_state_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr,
7578 atomic_state_field_name);
7563 atomic_state_field_name, false);
75797564
75807565 // clear the is_suspended bit
75817566 IrInstruction *prev_atomic_value = ir_build_atomic_rmw(irb, scope, node,
......@@ -7642,12 +7627,12 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *scope, AstNode *n
76427627
76437628 IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, scope, node, target_inst);
76447629 Buf *result_ptr_field_name = buf_create_from_str(RESULT_PTR_FIELD_NAME);
7645 IrInstruction *result_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_ptr_field_name);
7630 IrInstruction *result_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_ptr_field_name, false);
76467631
76477632 if (irb->codegen->have_err_ret_tracing) {
76487633 IrInstruction *err_ret_trace_ptr = ir_build_error_return_trace(irb, scope, node, IrInstructionErrorReturnTrace::NonNull);
76497634 Buf *err_ret_trace_ptr_field_name = buf_create_from_str(ERR_RET_TRACE_PTR_FIELD_NAME);
7650 IrInstruction *err_ret_trace_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_ptr_field_name);
7635 IrInstruction *err_ret_trace_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_ptr_field_name, false);
76517636 ir_build_store_ptr(irb, scope, node, err_ret_trace_ptr_field_ptr, err_ret_trace_ptr);
76527637 }
76537638
......@@ -7669,7 +7654,7 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *scope, AstNode *n
76697654
76707655 Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME);
76717656 IrInstruction *atomic_state_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr,
7672 atomic_state_field_name);
7657 atomic_state_field_name, false);
76737658
76747659 IrInstruction *promise_type_val = ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_promise);
76757660 IrInstruction *const_bool_false = ir_build_const_bool(irb, scope, node, false);
......@@ -7723,12 +7708,12 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *scope, AstNode *n
77237708 ir_set_cursor_at_end_and_append_block(irb, no_suspend_block);
77247709 if (irb->codegen->have_err_ret_tracing) {
77257710 Buf *err_ret_trace_field_name = buf_create_from_str(ERR_RET_TRACE_FIELD_NAME);
7726 IrInstruction *src_err_ret_trace_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_field_name);
7711 IrInstruction *src_err_ret_trace_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_field_name, false);
77277712 IrInstruction *dest_err_ret_trace_ptr = ir_build_error_return_trace(irb, scope, node, IrInstructionErrorReturnTrace::NonNull);
77287713 ir_build_merge_err_ret_traces(irb, scope, node, coro_promise_ptr, src_err_ret_trace_ptr, dest_err_ret_trace_ptr);
77297714 }
77307715 Buf *result_field_name = buf_create_from_str(RESULT_FIELD_NAME);
7731 IrInstruction *promise_result_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_field_name);
7716 IrInstruction *promise_result_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_field_name, false);
77327717 // If the type of the result handle_is_ptr then this does not actually perform a load. But we need it to,
77337718 // because we're about to destroy the memory. So we store it into our result variable.
77347719 IrInstruction *no_suspend_result = ir_build_load_ptr(irb, scope, node, promise_result_ptr);
......@@ -8152,7 +8137,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
81528137 build_decl_var_and_init(irb, coro_scope, node, irb->exec->coro_allocator_var, implicit_allocator_ptr,
81538138 "allocator", const_bool_false);
81548139 Buf *realloc_field_name = buf_create_from_str(ASYNC_REALLOC_FIELD_NAME);
8155 IrInstruction *realloc_fn_ptr = ir_build_field_ptr(irb, coro_scope, node, implicit_allocator_ptr, realloc_field_name);
8140 IrInstruction *realloc_fn_ptr = ir_build_field_ptr(irb, coro_scope, node, implicit_allocator_ptr, realloc_field_name, false);
81568141 IrInstruction *realloc_fn = ir_build_load_ptr(irb, coro_scope, node, realloc_fn_ptr);
81578142 IrInstruction *maybe_coro_mem_ptr = ir_build_coro_alloc_helper(irb, coro_scope, node, realloc_fn, coro_size);
81588143 IrInstruction *alloc_result_is_ok = ir_build_test_nonnull(irb, coro_scope, node, maybe_coro_mem_ptr);
......@@ -8172,30 +8157,30 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
81728157
81738158 Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME);
81748159 irb->exec->atomic_state_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr,
8175 atomic_state_field_name);
8160 atomic_state_field_name, false);
81768161 IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0);
81778162 ir_build_store_ptr(irb, scope, node, irb->exec->atomic_state_field_ptr, zero);
81788163 Buf *result_field_name = buf_create_from_str(RESULT_FIELD_NAME);
8179 irb->exec->coro_result_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_field_name);
8164 irb->exec->coro_result_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_field_name, false);
81808165 result_ptr_field_name = buf_create_from_str(RESULT_PTR_FIELD_NAME);
8181 irb->exec->coro_result_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_ptr_field_name);
8166 irb->exec->coro_result_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_ptr_field_name, false);
81828167 ir_build_store_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr, irb->exec->coro_result_field_ptr);
81838168 if (irb->codegen->have_err_ret_tracing) {
81848169 // initialize the error return trace
81858170 Buf *return_addresses_field_name = buf_create_from_str(RETURN_ADDRESSES_FIELD_NAME);
8186 IrInstruction *return_addresses_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, return_addresses_field_name);
8171 IrInstruction *return_addresses_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, return_addresses_field_name, false);
81878172
81888173 Buf *err_ret_trace_field_name = buf_create_from_str(ERR_RET_TRACE_FIELD_NAME);
8189 err_ret_trace_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_field_name);
8174 err_ret_trace_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_field_name, false);
81908175 ir_build_mark_err_ret_trace_ptr(irb, scope, node, err_ret_trace_ptr);
81918176
81928177 // coordinate with builtin.zig
81938178 Buf *index_name = buf_create_from_str("index");
8194 IrInstruction *index_ptr = ir_build_field_ptr(irb, scope, node, err_ret_trace_ptr, index_name);
8179 IrInstruction *index_ptr = ir_build_field_ptr(irb, scope, node, err_ret_trace_ptr, index_name, false);
81958180 ir_build_store_ptr(irb, scope, node, index_ptr, zero);
81968181
81978182 Buf *instruction_addresses_name = buf_create_from_str("instruction_addresses");
8198 IrInstruction *addrs_slice_ptr = ir_build_field_ptr(irb, scope, node, err_ret_trace_ptr, instruction_addresses_name);
8183 IrInstruction *addrs_slice_ptr = ir_build_field_ptr(irb, scope, node, err_ret_trace_ptr, instruction_addresses_name, false);
81998184
82008185 IrInstruction *slice_value = ir_build_slice(irb, scope, node, return_addresses_ptr, zero, nullptr, false);
82018186 ir_build_store_ptr(irb, scope, node, addrs_slice_ptr, slice_value);
......@@ -8256,7 +8241,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
82568241 }
82578242 if (irb->codegen->have_err_ret_tracing) {
82588243 Buf *err_ret_trace_ptr_field_name = buf_create_from_str(ERR_RET_TRACE_PTR_FIELD_NAME);
8259 IrInstruction *err_ret_trace_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_ptr_field_name);
8244 IrInstruction *err_ret_trace_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_ptr_field_name, false);
82608245 IrInstruction *dest_err_ret_trace_ptr = ir_build_load_ptr(irb, scope, node, err_ret_trace_ptr_field_ptr);
82618246 ir_build_merge_err_ret_traces(irb, scope, node, coro_promise_ptr, err_ret_trace_ptr, dest_err_ret_trace_ptr);
82628247 }
......@@ -8291,7 +8276,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
82918276 Buf *shrink_field_name = buf_create_from_str(ASYNC_SHRINK_FIELD_NAME);
82928277 IrInstruction *implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node,
82938278 ImplicitAllocatorIdLocalVar);
8294 IrInstruction *shrink_fn_ptr = ir_build_field_ptr(irb, scope, node, implicit_allocator_ptr, shrink_field_name);
8279 IrInstruction *shrink_fn_ptr = ir_build_field_ptr(irb, scope, node, implicit_allocator_ptr, shrink_field_name, false);
82958280 IrInstruction *shrink_fn = ir_build_load_ptr(irb, scope, node, shrink_fn_ptr);
82968281 IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0);
82978282 IrInstruction *coro_mem_ptr_maybe = ir_build_coro_free(irb, scope, node, coro_id, irb->exec->coro_handle);
......@@ -14722,7 +14707,7 @@ static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCallSrc
1472214707 ir_assert(async_allocator_inst->value.type->id == ZigTypeIdPointer, &call_instruction->base);
1472314708 ZigType *container_type = async_allocator_inst->value.type->data.pointer.child_type;
1472414709 IrInstruction *field_ptr_inst = ir_analyze_container_field_ptr(ira, realloc_field_name, &call_instruction->base,
14725 async_allocator_inst, container_type);
14710 async_allocator_inst, container_type, false);
1472614711 if (type_is_invalid(field_ptr_inst->value.type)) {
1472714712 return ira->codegen->invalid_instruction;
1472814713 }
......@@ -16580,7 +16565,7 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
1658016565}
1658116566
1658216567static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,
16583 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type)
16568 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type, bool initializing)
1658416569{
1658516570 Error err;
1658616571
......@@ -16664,15 +16649,19 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1666416649 if (type_is_invalid(union_val->type))
1666516650 return ira->codegen->invalid_instruction;
1666616651
16667 TypeUnionField *actual_field = find_union_field_by_tag(bare_type, &union_val->data.x_union.tag);
16668 if (actual_field == nullptr)
16669 zig_unreachable();
16652 if (initializing) {
16653 bigint_init_bigint(&union_val->data.x_union.tag, &field->enum_field->value);
16654 } else {
16655 TypeUnionField *actual_field = find_union_field_by_tag(bare_type, &union_val->data.x_union.tag);
16656 if (actual_field == nullptr)
16657 zig_unreachable();
1667016658
16671 if (field != actual_field) {
16672 ir_add_error_node(ira, source_instr->source_node,
16673 buf_sprintf("accessing union field '%s' while field '%s' is set", buf_ptr(field_name),
16674 buf_ptr(actual_field->name)));
16675 return ira->codegen->invalid_instruction;
16659 if (field != actual_field) {
16660 ir_add_error_node(ira, source_instr->source_node,
16661 buf_sprintf("accessing union field '%s' while field '%s' is set", buf_ptr(field_name),
16662 buf_ptr(actual_field->name)));
16663 return ira->codegen->invalid_instruction;
16664 }
1667616665 }
1667716666
1667816667 ConstExprValue *payload_val = union_val->data.x_union.payload;
......@@ -16690,7 +16679,8 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1669016679 }
1669116680 }
1669216681
16693 IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope, source_instr->source_node, container_ptr, field);
16682 IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope,
16683 source_instr->source_node, container_ptr, field, initializing);
1669416684 result->value.type = get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, is_volatile,
1669516685 PtrLenSingle, 0, 0, 0, false);
1669616686 return result;
......@@ -16826,10 +16816,10 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1682616816 if (container_type->id == ZigTypeIdPointer) {
1682716817 ZigType *bare_type = container_ref_type(container_type);
1682816818 IrInstruction *container_child = ir_get_deref(ira, &field_ptr_instruction->base, container_ptr);
16829 IrInstruction *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base, container_child, bare_type);
16819 IrInstruction *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base, container_child, bare_type, field_ptr_instruction->initializing);
1683016820 return result;
1683116821 } else {
16832 IrInstruction *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base, container_ptr, container_type);
16822 IrInstruction *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base, container_ptr, container_type, field_ptr_instruction->initializing);
1683316823 return result;
1683416824 }
1683516825 } else if (is_array_ref(container_type)) {
......@@ -18120,7 +18110,7 @@ static IrInstruction *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstru
1812018110 }
1812118111
1812218112 IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb,
18123 instruction->base.scope, instruction->base.source_node, target_value_ptr, field);
18113 instruction->base.scope, instruction->base.source_node, target_value_ptr, field, false);
1812418114 result->value.type = get_pointer_to_type(ira->codegen, field->type_entry,
1812518115 target_value_ptr->value.type->data.pointer.is_const);
1812618116 return result;
......@@ -18386,11 +18376,10 @@ static IrInstruction *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrI
1838618376 return result;
1838718377 }
1838818378
18389 IrInstruction *new_instruction = ir_build_union_init(&ira->new_irb,
18390 instruction->scope, instruction->source_node,
18391 container_type, type_field, casted_field_value);
18392 new_instruction->value.type = container_type;
18393 ir_add_alloca(ira, new_instruction, container_type);
18379 // this instruction should not get to codegen
18380 IrInstruction *new_instruction = ir_const(ira, instruction, container_type);
18381 // this is how we signal to EndExpr the value is not comptime known
18382 new_instruction->value.special = ConstValSpecialRuntime;
1839418383 return new_instruction;
1839518384}
1839618385
......@@ -23907,7 +23896,6 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2390723896 switch (instruction->id) {
2390823897 case IrInstructionIdInvalid:
2390923898 case IrInstructionIdWidenOrShorten:
23910 case IrInstructionIdUnionInit:
2391123899 case IrInstructionIdStructFieldPtr:
2391223900 case IrInstructionIdUnionFieldPtr:
2391323901 case IrInstructionIdOptionalWrap:
......@@ -24353,7 +24341,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2435324341 case IrInstructionIdCast:
2435424342 case IrInstructionIdContainerInitList:
2435524343 case IrInstructionIdContainerInitFields:
24356 case IrInstructionIdUnionInit:
2435724344 case IrInstructionIdFieldPtr:
2435824345 case IrInstructionIdElemPtr:
2435924346 case IrInstructionIdVarPtr:
src/ir_print.cpp-12
......@@ -360,15 +360,6 @@ static void ir_print_container_init_fields(IrPrint *irp, IrInstructionContainerI
360360 fprintf(irp->f, "} // container init");
361361}
362362
363static void ir_print_union_init(IrPrint *irp, IrInstructionUnionInit *instruction) {
364 Buf *field_name = instruction->field->enum_field->name;
365
366 fprintf(irp->f, "%s {", buf_ptr(&instruction->union_type->name));
367 fprintf(irp->f, ".%s = ", buf_ptr(field_name));
368 ir_print_other_instruction(irp, instruction->init_value);
369 fprintf(irp->f, "} // union init");
370}
371
372363static void ir_print_unreachable(IrPrint *irp, IrInstructionUnreachable *instruction) {
373364 fprintf(irp->f, "unreachable");
374365}
......@@ -1590,9 +1581,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
15901581 case IrInstructionIdContainerInitFields:
15911582 ir_print_container_init_fields(irp, (IrInstructionContainerInitFields *)instruction);
15921583 break;
1593 case IrInstructionIdUnionInit:
1594 ir_print_union_init(irp, (IrInstructionUnionInit *)instruction);
1595 break;
15961584 case IrInstructionIdUnreachable:
15971585 ir_print_unreachable(irp, (IrInstructionUnreachable *)instruction);
15981586 break;