authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-03 20:56:22-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-03 20:56:22-04:00
logd4054e35fe3a6ba4b4f0d1a0da0e0149f21b49a4
tree4318552d331be2a3b8465897dc5e18f52e18e501
parentb6108eed522b7a0122d305dc1a74de8f12f20d5b
signaturelock-open Commit is signed but in an unrecognized format.

while loops

Note that neither the payload capture variable nor the error capture variable require a stack allocation. ```zig export fn entry() void { var c: anyerror!i32 = 1234; while (c) |hi| {} else |e| {} } ``` ```llvm define void @entry() #2 !dbg !39 { Entry: %c = alloca { i16, i32 }, align 4 %0 = bitcast { i16, i32 }* %c to i8*, !dbg !52 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %0, i8* align 4 bitcast ({ i16, i32 }* @0 to i8*), i64 8, i1 false), !dbg !52 call void @llvm.dbg.declare(metadata { i16, i32 }* %c, metadata !43, metadata !DIExpression()), !dbg !52 br label %WhileCond, !dbg !53 WhileCond: ; preds = %WhileBody, %Entry %1 = getelementptr inbounds { i16, i32 }, { i16, i32 }* %c, i32 0, i32 0, !dbg !54 %2 = load i16, i16* %1, align 2, !dbg !54 %3 = icmp ne i16 %2, 0, !dbg !54 br i1 %3, label %WhileElse, label %WhileBody, !dbg !54 WhileBody: ; preds = %WhileCond %4 = getelementptr inbounds { i16, i32 }, { i16, i32 }* %c, i32 0, i32 1, !dbg !53 call void @llvm.dbg.declare(metadata i32* %4, metadata !50, metadata !DIExpression()), !dbg !53 br label %WhileCond, !dbg !53 WhileElse: ; preds = %WhileCond %5 = getelementptr inbounds { i16, i32 }, { i16, i32 }* %c, i32 0, i32 0, !dbg !55 call void @llvm.dbg.declare(metadata i16* %5, metadata !51, metadata !DIExpression()), !dbg !55 ret void, !dbg !56 } ```

5 files changed, 86 insertions(+), 82 deletions(-)

BRANCH_TODO+2-41
...@@ -1,7 +1,8 @@...@@ -1,7 +1,8 @@
1Scratch pad for stuff to do before merging master1Scratch pad for stuff to do before merging master
2=================================================2=================================================
33
4 * implicit casts4migrate ir_build_var_decl_src to use ir_build_alloca_src and explicitly initialize
5
5 * for loops6 * for loops
6 * switch expression7 * switch expression
7 * struct initializations8 * struct initializations
...@@ -12,8 +13,6 @@ look at all the ir_gen_node ir_gen_node_extra calls and make sure result locatio...@@ -12,8 +13,6 @@ look at all the ir_gen_node ir_gen_node_extra calls and make sure result locatio
1213
13migrate all the alloca_list to alloca_gen_list14migrate all the alloca_list to alloca_gen_list
1415
15migrate ir_build_var_decl_src to use ir_build_alloca_src and explicitly initialize
16
17inferred comptime16inferred comptime
1817
1918
...@@ -30,41 +29,3 @@ inferred comptime...@@ -30,41 +29,3 @@ inferred comptime
3029
31handle if with no else30handle if with no else
3231
33for loops:
34 IrInstruction *pointer_type = ir_build_to_ptr_type(irb, parent_scope, array_node, array_val_ptr);
35
36 IrInstruction *elem_var_type;
37 if (node->data.for_expr.elem_is_ptr) {
38 elem_var_type = pointer_type;
39 } else {
40 elem_var_type = ir_build_ptr_type_child(irb, parent_scope, elem_node, pointer_type);
41 }
42 IrInstruction *usize = ir_build_const_type(irb, child_scope, node, irb->codegen->builtin_types.entry_usize);
43- do they need to have an implicit cast in there for the elem variable?
44static IrInstruction *ir_build_to_ptr_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *ptr) {
45 IrInstructionToPtrType *instruction = ir_build_instruction<IrInstructionToPtrType>(irb, scope, source_node);
46 instruction->ptr = ptr;
47
48 ir_ref_instruction(ptr, irb->current_basic_block);
49
50 return &instruction->base;
51}
52
53static IrInstruction *ir_build_ptr_type_child(IrBuilder *irb, Scope *scope, AstNode *source_node,
54 IrInstruction *value)
55{
56 IrInstructionPtrTypeChild *instruction = ir_build_instruction<IrInstructionPtrTypeChild>(
57 irb, scope, source_node);
58 instruction->value = value;
59
60 ir_ref_instruction(value, irb->current_basic_block);
61
62 return &instruction->base;
63}
64
65coroutines
66 ZigType *coro_frame_type = get_promise_frame_type(irb->codegen, return_type);
67 IrInstruction *coro_frame_type_value = ir_build_const_type(irb, coro_scope, node, coro_frame_type);
68- implicit cast for the var decl
69 IrInstruction *await_handle_type_val = ir_build_const_type(irb, coro_scope, node,
70 get_optional_type(irb->codegen, irb->codegen->builtin_types.entry_promise));
src/all_types.hpp+2-1
...@@ -3087,10 +3087,11 @@ struct IrInstructionTestErr {...@@ -3087,10 +3087,11 @@ struct IrInstructionTestErr {
3087 IrInstruction *value;3087 IrInstruction *value;
3088};3088};
30893089
3090// Takes an error union pointer, returns a pointer to the error code.
3090struct IrInstructionUnwrapErrCode {3091struct IrInstructionUnwrapErrCode {
3091 IrInstruction base;3092 IrInstruction base;
30923093
3093 IrInstruction *err_union;3094 IrInstruction *err_union_ptr;
3094};3095};
30953096
3096struct IrInstructionUnwrapErrPayload {3097struct IrInstructionUnwrapErrPayload {
src/codegen.cpp+6-8
...@@ -4878,18 +4878,16 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI...@@ -4878,18 +4878,16 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI
4878static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executable,4878static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executable,
4879 IrInstructionUnwrapErrCode *instruction)4879 IrInstructionUnwrapErrCode *instruction)
4880{4880{
4881 ZigType *ptr_type = instruction->err_union->value.type;4881 ZigType *ptr_type = instruction->err_union_ptr->value.type;
4882 assert(ptr_type->id == ZigTypeIdPointer);4882 assert(ptr_type->id == ZigTypeIdPointer);
4883 ZigType *err_union_type = ptr_type->data.pointer.child_type;4883 ZigType *err_union_type = ptr_type->data.pointer.child_type;
4884 ZigType *payload_type = err_union_type->data.error_union.payload_type;4884 ZigType *payload_type = err_union_type->data.error_union.payload_type;
4885 LLVMValueRef err_union_ptr = ir_llvm_value(g, instruction->err_union);4885 LLVMValueRef err_union_ptr = ir_llvm_value(g, instruction->err_union_ptr);
4886 LLVMValueRef err_union_handle = get_handle_value(g, err_union_ptr, err_union_type, ptr_type);4886 if (!type_has_bits(payload_type)) {
48874887 return err_union_ptr;
4888 if (type_has_bits(payload_type)) {
4889 LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, "");
4890 return gen_load_untyped(g, err_val_ptr, 0, false, "");
4891 } else {4888 } else {
4892 return err_union_handle;4889 LLVMValueRef err_union_handle = get_handle_value(g, err_union_ptr, err_union_type, ptr_type);
4890 return LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, "");
4893 }4891 }
4894}4892}
48954893
src/ir.cpp+75-31
...@@ -1657,6 +1657,27 @@ static IrInstruction *ir_build_typeof(IrBuilder *irb, Scope *scope, AstNode *sou...@@ -1657,6 +1657,27 @@ static IrInstruction *ir_build_typeof(IrBuilder *irb, Scope *scope, AstNode *sou
1657 return &instruction->base;1657 return &instruction->base;
1658}1658}
16591659
1660static IrInstruction *ir_build_to_ptr_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *ptr) {
1661 IrInstructionToPtrType *instruction = ir_build_instruction<IrInstructionToPtrType>(irb, scope, source_node);
1662 instruction->ptr = ptr;
1663
1664 ir_ref_instruction(ptr, irb->current_basic_block);
1665
1666 return &instruction->base;
1667}
1668
1669static IrInstruction *ir_build_ptr_type_child(IrBuilder *irb, Scope *scope, AstNode *source_node,
1670 IrInstruction *value)
1671{
1672 IrInstructionPtrTypeChild *instruction = ir_build_instruction<IrInstructionPtrTypeChild>(
1673 irb, scope, source_node);
1674 instruction->value = value;
1675
1676 ir_ref_instruction(value, irb->current_basic_block);
1677
1678 return &instruction->base;
1679}
1680
1660static IrInstruction *ir_build_set_cold(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *is_cold) {1681static IrInstruction *ir_build_set_cold(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *is_cold) {
1661 IrInstructionSetCold *instruction = ir_build_instruction<IrInstructionSetCold>(irb, scope, source_node);1682 IrInstructionSetCold *instruction = ir_build_instruction<IrInstructionSetCold>(irb, scope, source_node);
1662 instruction->is_cold = is_cold;1683 instruction->is_cold = is_cold;
...@@ -2371,12 +2392,12 @@ static IrInstruction *ir_build_test_err(IrBuilder *irb, Scope *scope, AstNode *s...@@ -2371,12 +2392,12 @@ static IrInstruction *ir_build_test_err(IrBuilder *irb, Scope *scope, AstNode *s
2371}2392}
23722393
2373static IrInstruction *ir_build_unwrap_err_code(IrBuilder *irb, Scope *scope, AstNode *source_node,2394static IrInstruction *ir_build_unwrap_err_code(IrBuilder *irb, Scope *scope, AstNode *source_node,
2374 IrInstruction *err_union)2395 IrInstruction *err_union_ptr)
2375{2396{
2376 IrInstructionUnwrapErrCode *instruction = ir_build_instruction<IrInstructionUnwrapErrCode>(irb, scope, source_node);2397 IrInstructionUnwrapErrCode *instruction = ir_build_instruction<IrInstructionUnwrapErrCode>(irb, scope, source_node);
2377 instruction->err_union = err_union;2398 instruction->err_union_ptr = err_union_ptr;
23782399
2379 ir_ref_instruction(err_union, irb->current_basic_block);2400 ir_ref_instruction(err_union_ptr, irb->current_basic_block);
23802401
2381 return &instruction->base;2402 return &instruction->base;
2382}2403}
...@@ -3505,7 +3526,8 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,...@@ -3505,7 +3526,8 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,
35053526
3506 ir_set_cursor_at_end_and_append_block(irb, return_block);3527 ir_set_cursor_at_end_and_append_block(irb, return_block);
3507 if (!ir_gen_defers_for_block(irb, scope, outer_scope, true)) {3528 if (!ir_gen_defers_for_block(irb, scope, outer_scope, true)) {
3508 IrInstruction *err_val = ir_build_unwrap_err_code(irb, scope, node, err_union_ptr);3529 IrInstruction *err_val_ptr = ir_build_unwrap_err_code(irb, scope, node, err_union_ptr);
3530 IrInstruction *err_val = ir_build_load_ptr(irb, scope, node, err_val_ptr);
3509 if (irb->codegen->have_err_ret_tracing && !should_inline) {3531 if (irb->codegen->have_err_ret_tracing && !should_inline) {
3510 ir_build_save_err_ret_addr(irb, scope, node);3532 ir_build_save_err_ret_addr(irb, scope, node);
3511 }3533 }
...@@ -5721,11 +5743,11 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n...@@ -5721,11 +5743,11 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
57215743
5722 ir_set_cursor_at_end_and_append_block(irb, body_block);5744 ir_set_cursor_at_end_and_append_block(irb, body_block);
5723 if (var_symbol) {5745 if (var_symbol) {
5724 IrInstruction *var_ptr_value = ir_build_unwrap_err_payload(irb, payload_scope, symbol_node,5746 IrInstruction *payload_ptr = ir_build_unwrap_err_payload(irb, payload_scope, symbol_node,
5725 err_val_ptr, false);5747 err_val_ptr, false);
5726 IrInstruction *var_value = node->data.while_expr.var_is_ptr ?5748 IrInstruction *var_ptr = node->data.while_expr.var_is_ptr ?
5727 var_ptr_value : ir_build_load_ptr(irb, payload_scope, symbol_node, var_ptr_value);5749 ir_build_ref(irb, payload_scope, symbol_node, payload_ptr, true, false) : payload_ptr;
5728 ir_build_var_decl_src(irb, payload_scope, symbol_node, payload_var, nullptr, var_value);5750 ir_build_var_decl_src(irb, payload_scope, symbol_node, payload_var, nullptr, var_ptr);
5729 }5751 }
57305752
5731 ZigList<IrInstruction *> incoming_values = {0};5753 ZigList<IrInstruction *> incoming_values = {0};
...@@ -5766,8 +5788,8 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n...@@ -5766,8 +5788,8 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
5766 ZigVar *err_var = ir_create_var(irb, err_symbol_node, scope, err_symbol,5788 ZigVar *err_var = ir_create_var(irb, err_symbol_node, scope, err_symbol,
5767 true, false, false, is_comptime);5789 true, false, false, is_comptime);
5768 Scope *err_scope = err_var->child_scope;5790 Scope *err_scope = err_var->child_scope;
5769 IrInstruction *err_var_value = ir_build_unwrap_err_code(irb, err_scope, err_symbol_node, err_val_ptr);5791 IrInstruction *err_ptr = ir_build_unwrap_err_code(irb, err_scope, err_symbol_node, err_val_ptr);
5770 ir_build_var_decl_src(irb, err_scope, symbol_node, err_var, nullptr, err_var_value);5792 ir_build_var_decl_src(irb, err_scope, symbol_node, err_var, nullptr, err_ptr);
57715793
5772 IrInstruction *else_result = ir_gen_node(irb, else_node, err_scope);5794 IrInstruction *else_result = ir_gen_node(irb, else_node, err_scope);
5773 if (else_result == irb->codegen->invalid_instruction)5795 if (else_result == irb->codegen->invalid_instruction)
...@@ -5808,10 +5830,10 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n...@@ -5808,10 +5830,10 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
5808 }5830 }
58095831
5810 ir_set_cursor_at_end_and_append_block(irb, body_block);5832 ir_set_cursor_at_end_and_append_block(irb, body_block);
5811 IrInstruction *var_ptr_value = ir_build_optional_unwrap_ptr(irb, child_scope, symbol_node, maybe_val_ptr, false);5833 IrInstruction *payload_ptr = ir_build_optional_unwrap_ptr(irb, child_scope, symbol_node, maybe_val_ptr, false);
5812 IrInstruction *var_value = node->data.while_expr.var_is_ptr ?5834 IrInstruction *var_ptr = node->data.while_expr.var_is_ptr ?
5813 var_ptr_value : ir_build_load_ptr(irb, child_scope, symbol_node, var_ptr_value);5835 ir_build_ref(irb, child_scope, symbol_node, payload_ptr, true, false) : payload_ptr;
5814 ir_build_var_decl_src(irb, child_scope, symbol_node, payload_var, nullptr, var_value);5836 ir_build_var_decl_src(irb, child_scope, symbol_node, payload_var, nullptr, var_ptr);
58155837
5816 ZigList<IrInstruction *> incoming_values = {0};5838 ZigList<IrInstruction *> incoming_values = {0};
5817 ZigList<IrBasicBlock *> incoming_blocks = {0};5839 ZigList<IrBasicBlock *> incoming_blocks = {0};
...@@ -5953,6 +5975,15 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo...@@ -5953,6 +5975,15 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
5953 if (array_val_ptr == irb->codegen->invalid_instruction)5975 if (array_val_ptr == irb->codegen->invalid_instruction)
5954 return array_val_ptr;5976 return array_val_ptr;
59555977
5978 IrInstruction *pointer_type = ir_build_to_ptr_type(irb, parent_scope, array_node, array_val_ptr);
5979
5980 IrInstruction *elem_var_type;
5981 if (node->data.for_expr.elem_is_ptr) {
5982 elem_var_type = pointer_type;
5983 } else {
5984 elem_var_type = ir_build_ptr_type_child(irb, parent_scope, elem_node, pointer_type);
5985 }
5986
5956 IrInstruction *is_comptime = ir_build_const_bool(irb, parent_scope, node,5987 IrInstruction *is_comptime = ir_build_const_bool(irb, parent_scope, node,
5957 ir_should_inline(irb->exec, parent_scope) || node->data.for_expr.is_inline);5988 ir_should_inline(irb->exec, parent_scope) || node->data.for_expr.is_inline);
59585989
...@@ -5961,8 +5992,9 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo...@@ -5961,8 +5992,9 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
5961 ZigVar *elem_var = ir_create_var(irb, elem_node, parent_scope, elem_var_name, true, false, false, is_comptime);5992 ZigVar *elem_var = ir_create_var(irb, elem_node, parent_scope, elem_var_name, true, false, false, is_comptime);
5962 Scope *child_scope = elem_var->child_scope;5993 Scope *child_scope = elem_var->child_scope;
59635994
5964 IrInstruction *undefined_value = ir_build_const_undefined(irb, child_scope, elem_node);5995 IrInstruction *undef = ir_build_const_undefined(irb, parent_scope, elem_node);
5965 ir_build_var_decl_src(irb, child_scope, elem_node, elem_var, nullptr, undefined_value);5996 IrInstruction *undef_elem_var_type = ir_build_implicit_cast(irb, parent_scope, elem_node, elem_var_type, undef);
5997 ir_build_var_decl_src(irb, child_scope, elem_node, elem_var, nullptr, undef_elem_var_type);
5966 IrInstruction *elem_var_ptr = ir_build_var_ptr(irb, child_scope, node, elem_var);5998 IrInstruction *elem_var_ptr = ir_build_var_ptr(irb, child_scope, node, elem_var);
59675999
5968 AstNode *index_var_source_node;6000 AstNode *index_var_source_node;
...@@ -6475,8 +6507,8 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *...@@ -6475,8 +6507,8 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
6475 ZigVar *var = ir_create_var(irb, node, subexpr_scope,6507 ZigVar *var = ir_create_var(irb, node, subexpr_scope,
6476 err_symbol, is_const, is_const, is_shadowable, is_comptime);6508 err_symbol, is_const, is_const, is_shadowable, is_comptime);
64776509
6478 IrInstruction *var_value = ir_build_unwrap_err_code(irb, subexpr_scope, node, err_val_ptr);6510 IrInstruction *err_ptr = ir_build_unwrap_err_code(irb, subexpr_scope, node, err_val_ptr);
6479 ir_build_var_decl_src(irb, subexpr_scope, node, var, nullptr, var_value);6511 ir_build_var_decl_src(irb, subexpr_scope, node, var, nullptr, err_ptr);
6480 err_var_scope = var->child_scope;6512 err_var_scope = var->child_scope;
6481 } else {6513 } else {
6482 err_var_scope = subexpr_scope;6514 err_var_scope = subexpr_scope;
...@@ -7004,8 +7036,8 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode...@@ -7004,8 +7036,8 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
7004 ZigVar *var = ir_create_var(irb, node, parent_scope, var_name,7036 ZigVar *var = ir_create_var(irb, node, parent_scope, var_name,
7005 is_const, is_const, is_shadowable, is_comptime);7037 is_const, is_const, is_shadowable, is_comptime);
7006 err_scope = var->child_scope;7038 err_scope = var->child_scope;
7007 IrInstruction *err_val = ir_build_unwrap_err_code(irb, err_scope, node, err_union_ptr);7039 IrInstruction *err_ptr = ir_build_unwrap_err_code(irb, err_scope, node, err_union_ptr);
7008 ir_build_var_decl_src(irb, err_scope, var_node, var, nullptr, err_val);7040 ir_build_var_decl_src(irb, err_scope, var_node, var, nullptr, err_ptr);
7009 } else {7041 } else {
7010 err_scope = parent_scope;7042 err_scope = parent_scope;
7011 }7043 }
...@@ -7482,7 +7514,7 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *scope, AstNode *n...@@ -7482,7 +7514,7 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *scope, AstNode *n
74827514
7483 IrInstruction *promise_type_val = ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_promise);7515 IrInstruction *promise_type_val = ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_promise);
7484 IrInstruction *const_bool_false = ir_build_const_bool(irb, scope, node, false);7516 IrInstruction *const_bool_false = ir_build_const_bool(irb, scope, node, false);
7485 IrInstruction *undefined_value = ir_build_const_undefined(irb, scope, node);7517 IrInstruction *undef = ir_build_const_undefined(irb, scope, node);
7486 IrInstruction *usize_type_val = ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_usize);7518 IrInstruction *usize_type_val = ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_usize);
7487 IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0);7519 IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0);
7488 IrInstruction *inverted_ptr_mask = ir_build_const_usize(irb, scope, node, 0x7); // 0b1117520 IrInstruction *inverted_ptr_mask = ir_build_const_usize(irb, scope, node, 0x7); // 0b111
...@@ -7496,7 +7528,8 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *scope, AstNode *n...@@ -7496,7 +7528,8 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *scope, AstNode *n
7496 IrInstruction *target_promise_type = ir_build_typeof(irb, scope, node, target_inst);7528 IrInstruction *target_promise_type = ir_build_typeof(irb, scope, node, target_inst);
7497 IrInstruction *promise_result_type = ir_build_promise_result_type(irb, scope, node, target_promise_type);7529 IrInstruction *promise_result_type = ir_build_promise_result_type(irb, scope, node, target_promise_type);
7498 ir_build_await_bookkeeping(irb, scope, node, promise_result_type);7530 ir_build_await_bookkeeping(irb, scope, node, promise_result_type);
7499 ir_build_var_decl_src(irb, scope, node, result_var, nullptr, undefined_value);7531 IrInstruction *undef_promise_result = ir_build_implicit_cast(irb, scope, node, promise_result_type, undef);
7532 ir_build_var_decl_src(irb, scope, node, result_var, nullptr, undef_promise_result);
7500 IrInstruction *my_result_var_ptr = ir_build_var_ptr(irb, scope, node, result_var);7533 IrInstruction *my_result_var_ptr = ir_build_var_ptr(irb, scope, node, result_var);
7501 ir_build_store_ptr(irb, scope, node, result_ptr_field_ptr, my_result_var_ptr);7534 ir_build_store_ptr(irb, scope, node, result_ptr_field_ptr, my_result_var_ptr);
7502 IrInstruction *save_token = ir_build_coro_save(irb, scope, node, irb->exec->coro_handle);7535 IrInstruction *save_token = ir_build_coro_save(irb, scope, node, irb->exec->coro_handle);
...@@ -7928,12 +7961,18 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec...@@ -7928,12 +7961,18 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
7928 return_type = fn_entry->type_entry->data.fn.fn_type_id.return_type;7961 return_type = fn_entry->type_entry->data.fn.fn_type_id.return_type;
7929 IrInstruction *undef = ir_build_const_undefined(irb, coro_scope, node);7962 IrInstruction *undef = ir_build_const_undefined(irb, coro_scope, node);
7930 // TODO mark this var decl as "no safety" e.g. disable initializing the undef value to 0xaa7963 // TODO mark this var decl as "no safety" e.g. disable initializing the undef value to 0xaa
7931 ir_build_var_decl_src(irb, coro_scope, node, promise_var, nullptr, undef);7964 ZigType *coro_frame_type = get_promise_frame_type(irb->codegen, return_type);
7965 IrInstruction *coro_frame_type_value = ir_build_const_type(irb, coro_scope, node, coro_frame_type);
7966 IrInstruction *undef_coro_frame = ir_build_implicit_cast(irb, coro_scope, node, coro_frame_type_value, undef);
7967 ir_build_var_decl_src(irb, coro_scope, node, promise_var, nullptr, undef_coro_frame);
7932 coro_promise_ptr = ir_build_var_ptr(irb, coro_scope, node, promise_var);7968 coro_promise_ptr = ir_build_var_ptr(irb, coro_scope, node, promise_var);
79337969
7934 ZigVar *await_handle_var = ir_create_var(irb, node, coro_scope, nullptr, false, false, true, const_bool_false);7970 ZigVar *await_handle_var = ir_create_var(irb, node, coro_scope, nullptr, false, false, true, const_bool_false);
7935 IrInstruction *null_value = ir_build_const_null(irb, coro_scope, node);7971 IrInstruction *null_value = ir_build_const_null(irb, coro_scope, node);
7936 ir_build_var_decl_src(irb, coro_scope, node, await_handle_var, nullptr, null_value);7972 IrInstruction *await_handle_type_val = ir_build_const_type(irb, coro_scope, node,
7973 get_optional_type(irb->codegen, irb->codegen->builtin_types.entry_promise));
7974 IrInstruction *null_await_handle = ir_build_implicit_cast(irb, coro_scope, node, await_handle_type_val, null_value);
7975 ir_build_var_decl_src(irb, coro_scope, node, await_handle_var, nullptr, null_await_handle);
7937 irb->exec->await_handle_var_ptr = ir_build_var_ptr(irb, coro_scope, node, await_handle_var);7976 irb->exec->await_handle_var_ptr = ir_build_var_ptr(irb, coro_scope, node, await_handle_var);
79387977
7939 u8_ptr_type = ir_build_const_type(irb, coro_scope, node,7978 u8_ptr_type = ir_build_const_type(irb, coro_scope, node,
...@@ -13872,7 +13911,8 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,...@@ -13872,7 +13911,8 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
13872 return ira->codegen->invalid_instruction;13911 return ira->codegen->invalid_instruction;
13873 }13912 }
1387413913
13875 assert(var_ptr->value.type->id == ZigTypeIdPointer);13914 // The ir_build_var_decl_src call is supposed to pass a pointer to the allocation, not an initialization value.
13915 ir_assert(var_ptr->value.type->id == ZigTypeIdPointer, &decl_var_instruction->base);
1387613916
13877 ZigType *result_type = var_ptr->value.type->data.pointer.child_type;13917 ZigType *result_type = var_ptr->value.type->data.pointer.child_type;
13878 if (type_is_invalid(result_type)) {13918 if (type_is_invalid(result_type)) {
...@@ -21526,13 +21566,14 @@ static IrInstruction *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstruct...@@ -21526,13 +21566,14 @@ static IrInstruction *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstruct
21526}21566}
2152721567
21528static IrInstruction *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira, IrInstructionUnwrapErrCode *instruction) {21568static IrInstruction *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira, IrInstructionUnwrapErrCode *instruction) {
21529 IrInstruction *base_ptr = instruction->err_union->child;21569 IrInstruction *base_ptr = instruction->err_union_ptr->child;
21530 if (type_is_invalid(base_ptr->value.type))21570 if (type_is_invalid(base_ptr->value.type))
21531 return ira->codegen->invalid_instruction;21571 return ira->codegen->invalid_instruction;
21532 ZigType *ptr_type = base_ptr->value.type;21572 ZigType *ptr_type = base_ptr->value.type;
2153321573
21534 // This will be a pointer type because unwrap err payload IR instruction operates on a pointer to a thing.21574 // This will be a pointer type because unwrap err payload IR instruction operates on a pointer to a thing.
21535 assert(ptr_type->id == ZigTypeIdPointer);21575 assert(ptr_type->id == ZigTypeIdPointer);
21576 bool is_ptr_const = ptr_type->data.pointer.is_const;
2153621577
21537 ZigType *type_entry = ptr_type->data.pointer.child_type;21578 ZigType *type_entry = ptr_type->data.pointer.child_type;
21538 if (type_is_invalid(type_entry))21579 if (type_is_invalid(type_entry))
...@@ -21554,19 +21595,22 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira, IrI...@@ -21554,19 +21595,22 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira, IrI
21554 return ira->codegen->invalid_instruction;21595 return ira->codegen->invalid_instruction;
21555 if (err_union_val->special != ConstValSpecialRuntime) {21596 if (err_union_val->special != ConstValSpecialRuntime) {
21556 ErrorTableEntry *err = err_union_val->data.x_err_union.error_set->data.x_err_set;21597 ErrorTableEntry *err = err_union_val->data.x_err_union.error_set->data.x_err_set;
21557 assert(err);21598 assert(err != nullptr);
2155821599
21559 IrInstruction *result = ir_const(ira, &instruction->base,21600 IrInstruction *err_set_val = ir_const(ira, &instruction->base,
21560 type_entry->data.error_union.err_set_type);21601 type_entry->data.error_union.err_set_type);
21561 result->value.data.x_err_set = err;21602 err_set_val->value.data.x_err_set = err;
21562 return result;21603 err_set_val->value.parent.id = ConstParentIdErrUnionCode;
21604 err_set_val->value.parent.data.p_err_union_code.err_union_val = err_union_val;
21605
21606 return ir_get_ref(ira, &instruction->base, err_set_val, is_ptr_const, false);
21563 }21607 }
21564 }21608 }
21565 }21609 }
2156621610
21567 IrInstruction *result = ir_build_unwrap_err_code(&ira->new_irb,21611 IrInstruction *result = ir_build_unwrap_err_code(&ira->new_irb,
21568 instruction->base.scope, instruction->base.source_node, base_ptr);21612 instruction->base.scope, instruction->base.source_node, base_ptr);
21569 result->value.type = type_entry->data.error_union.err_set_type;21613 result->value.type = get_pointer_to_type(ira->codegen, type_entry->data.error_union.err_set_type, is_ptr_const);
21570 return result;21614 return result;
21571}21615}
2157221616
src/ir_print.cpp+1-1
...@@ -957,7 +957,7 @@ static void ir_print_test_err(IrPrint *irp, IrInstructionTestErr *instruction) {...@@ -957,7 +957,7 @@ static void ir_print_test_err(IrPrint *irp, IrInstructionTestErr *instruction) {
957957
958static void ir_print_unwrap_err_code(IrPrint *irp, IrInstructionUnwrapErrCode *instruction) {958static void ir_print_unwrap_err_code(IrPrint *irp, IrInstructionUnwrapErrCode *instruction) {
959 fprintf(irp->f, "UnwrapErrorCode(");959 fprintf(irp->f, "UnwrapErrorCode(");
960 ir_print_other_instruction(irp, instruction->err_union);960 ir_print_other_instruction(irp, instruction->err_union_ptr);
961 fprintf(irp->f, ")");961 fprintf(irp->f, ")");
962}962}
963963