| ... | @@ -6663,13 +6663,7 @@ static IrInstruction *ir_gen_fn_proto(IrBuilder *irb, Scope *parent_scope, AstNo | ... | @@ -6663,13 +6663,7 @@ static IrInstruction *ir_gen_fn_proto(IrBuilder *irb, Scope *parent_scope, AstNo |
| 6663 | async_allocator_type_value, is_var_args); | 6663 | async_allocator_type_value, is_var_args); |
| 6664 | } | 6664 | } |
| 6665 | | 6665 | |
| 6666 | static IrInstruction *ir_gen_cancel(IrBuilder *irb, Scope *scope, AstNode *node) { | 6666 | static IrInstruction *ir_gen_cancel_target(IrBuilder *irb, Scope *scope, AstNode *node, IrInstruction *target_inst) { |
| 6667 | assert(node->type == NodeTypeCancel); | | |
| 6668 | | | |
| 6669 | IrInstruction *target_inst = ir_gen_node(irb, node->data.cancel_expr.expr, scope); | | |
| 6670 | if (target_inst == irb->codegen->invalid_instruction) | | |
| 6671 | return irb->codegen->invalid_instruction; | | |
| 6672 | | | |
| 6673 | IrBasicBlock *done_block = ir_create_basic_block(irb, scope, "CancelDone"); | 6667 | IrBasicBlock *done_block = ir_create_basic_block(irb, scope, "CancelDone"); |
| 6674 | IrBasicBlock *not_canceled_block = ir_create_basic_block(irb, scope, "NotCanceled"); | 6668 | IrBasicBlock *not_canceled_block = ir_create_basic_block(irb, scope, "NotCanceled"); |
| 6675 | IrBasicBlock *pre_return_block = ir_create_basic_block(irb, scope, "PreReturn"); | 6669 | IrBasicBlock *pre_return_block = ir_create_basic_block(irb, scope, "PreReturn"); |
| ... | @@ -6726,6 +6720,16 @@ static IrInstruction *ir_gen_cancel(IrBuilder *irb, Scope *scope, AstNode *node) | ... | @@ -6726,6 +6720,16 @@ static IrInstruction *ir_gen_cancel(IrBuilder *irb, Scope *scope, AstNode *node) |
| 6726 | return ir_build_const_void(irb, scope, node); | 6720 | return ir_build_const_void(irb, scope, node); |
| 6727 | } | 6721 | } |
| 6728 | | 6722 | |
| | 6723 | static IrInstruction *ir_gen_cancel(IrBuilder *irb, Scope *scope, AstNode *node) { |
| | 6724 | assert(node->type == NodeTypeCancel); |
| | 6725 | |
| | 6726 | IrInstruction *target_inst = ir_gen_node(irb, node->data.cancel_expr.expr, scope); |
| | 6727 | if (target_inst == irb->codegen->invalid_instruction) |
| | 6728 | return irb->codegen->invalid_instruction; |
| | 6729 | |
| | 6730 | return ir_gen_cancel_target(irb, scope, node, target_inst); |
| | 6731 | } |
| | 6732 | |
| 6729 | static IrInstruction *ir_gen_resume(IrBuilder *irb, Scope *scope, AstNode *node) { | 6733 | static IrInstruction *ir_gen_resume(IrBuilder *irb, Scope *scope, AstNode *node) { |
| 6730 | assert(node->type == NodeTypeResume); | 6734 | assert(node->type == NodeTypeResume); |
| 6731 | | 6735 | |
| ... | @@ -6941,14 +6945,19 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod | ... | @@ -6941,14 +6945,19 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod |
| 6941 | IrBasicBlock *cleanup_block = ir_create_basic_block(irb, parent_scope, "SuspendCleanup"); | 6945 | IrBasicBlock *cleanup_block = ir_create_basic_block(irb, parent_scope, "SuspendCleanup"); |
| 6942 | IrBasicBlock *resume_block = ir_create_basic_block(irb, parent_scope, "SuspendResume"); | 6946 | IrBasicBlock *resume_block = ir_create_basic_block(irb, parent_scope, "SuspendResume"); |
| 6943 | IrBasicBlock *suspended_block = ir_create_basic_block(irb, parent_scope, "AlreadySuspended"); | 6947 | IrBasicBlock *suspended_block = ir_create_basic_block(irb, parent_scope, "AlreadySuspended"); |
| | 6948 | IrBasicBlock *canceled_block = ir_create_basic_block(irb, parent_scope, "IsCanceled"); |
| 6944 | IrBasicBlock *not_canceled_block = ir_create_basic_block(irb, parent_scope, "NotCanceled"); | 6949 | IrBasicBlock *not_canceled_block = ir_create_basic_block(irb, parent_scope, "NotCanceled"); |
| 6945 | IrBasicBlock *not_suspended_block = ir_create_basic_block(irb, parent_scope, "NotAlreadySuspended"); | 6950 | IrBasicBlock *not_suspended_block = ir_create_basic_block(irb, parent_scope, "NotAlreadySuspended"); |
| | 6951 | IrBasicBlock *cancel_awaiter_block = ir_create_basic_block(irb, parent_scope, "CancelAwaiter"); |
| 6946 | | 6952 | |
| | 6953 | IrInstruction *promise_type_val = ir_build_const_type(irb, parent_scope, node, irb->codegen->builtin_types.entry_promise); |
| 6947 | IrInstruction *const_bool_false = ir_build_const_bool(irb, parent_scope, node, false); | 6954 | IrInstruction *const_bool_false = ir_build_const_bool(irb, parent_scope, node, false); |
| 6948 | IrInstruction *usize_type_val = ir_build_const_type(irb, parent_scope, node, irb->codegen->builtin_types.entry_usize); | 6955 | IrInstruction *usize_type_val = ir_build_const_type(irb, parent_scope, node, irb->codegen->builtin_types.entry_usize); |
| 6949 | IrInstruction *is_canceled_mask = ir_build_const_usize(irb, parent_scope, node, 0x1); // 0b001 | 6956 | IrInstruction *is_canceled_mask = ir_build_const_usize(irb, parent_scope, node, 0x1); // 0b001 |
| 6950 | IrInstruction *is_suspended_mask = ir_build_const_usize(irb, parent_scope, node, 0x2); // 0b010 | 6957 | IrInstruction *is_suspended_mask = ir_build_const_usize(irb, parent_scope, node, 0x2); // 0b010 |
| 6951 | IrInstruction *zero = ir_build_const_usize(irb, parent_scope, node, 0); | 6958 | IrInstruction *zero = ir_build_const_usize(irb, parent_scope, node, 0); |
| | 6959 | IrInstruction *inverted_ptr_mask = ir_build_const_usize(irb, parent_scope, node, 0x7); // 0b111 |
| | 6960 | IrInstruction *ptr_mask = ir_build_un_op(irb, parent_scope, node, IrUnOpBinNot, inverted_ptr_mask); // 0b111...000 |
| 6952 | | 6961 | |
| 6953 | IrInstruction *prev_atomic_value = ir_build_atomic_rmw(irb, parent_scope, node, | 6962 | IrInstruction *prev_atomic_value = ir_build_atomic_rmw(irb, parent_scope, node, |
| 6954 | usize_type_val, irb->exec->atomic_state_field_ptr, nullptr, is_suspended_mask, nullptr, | 6963 | usize_type_val, irb->exec->atomic_state_field_ptr, nullptr, is_suspended_mask, nullptr, |
| ... | @@ -6956,7 +6965,17 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod | ... | @@ -6956,7 +6965,17 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod |
| 6956 | | 6965 | |
| 6957 | IrInstruction *is_canceled_value = ir_build_bin_op(irb, parent_scope, node, IrBinOpBinAnd, prev_atomic_value, is_canceled_mask, false); | 6966 | IrInstruction *is_canceled_value = ir_build_bin_op(irb, parent_scope, node, IrBinOpBinAnd, prev_atomic_value, is_canceled_mask, false); |
| 6958 | IrInstruction *is_canceled_bool = ir_build_bin_op(irb, parent_scope, node, IrBinOpCmpNotEq, is_canceled_value, zero, false); | 6967 | IrInstruction *is_canceled_bool = ir_build_bin_op(irb, parent_scope, node, IrBinOpCmpNotEq, is_canceled_value, zero, false); |
| 6959 | ir_build_cond_br(irb, parent_scope, node, is_canceled_bool, cleanup_block, not_canceled_block, const_bool_false); | 6968 | ir_build_cond_br(irb, parent_scope, node, is_canceled_bool, canceled_block, not_canceled_block, const_bool_false); |
| | 6969 | |
| | 6970 | ir_set_cursor_at_end_and_append_block(irb, canceled_block); |
| | 6971 | IrInstruction *await_handle_addr = ir_build_bin_op(irb, parent_scope, node, IrBinOpBinAnd, prev_atomic_value, ptr_mask, false); |
| | 6972 | IrInstruction *have_await_handle = ir_build_bin_op(irb, parent_scope, node, IrBinOpCmpNotEq, await_handle_addr, zero, false); |
| | 6973 | ir_build_cond_br(irb, parent_scope, node, have_await_handle, cancel_awaiter_block, cleanup_block, const_bool_false); |
| | 6974 | |
| | 6975 | ir_set_cursor_at_end_and_append_block(irb, cancel_awaiter_block); |
| | 6976 | IrInstruction *await_handle = ir_build_int_to_ptr(irb, parent_scope, node, promise_type_val, await_handle_addr); |
| | 6977 | ir_gen_cancel_target(irb, parent_scope, node, await_handle); |
| | 6978 | ir_build_br(irb, parent_scope, node, cleanup_block, const_bool_false); |
| 6960 | | 6979 | |
| 6961 | ir_set_cursor_at_end_and_append_block(irb, not_canceled_block); | 6980 | ir_set_cursor_at_end_and_append_block(irb, not_canceled_block); |
| 6962 | IrInstruction *is_suspended_value = ir_build_bin_op(irb, parent_scope, node, IrBinOpBinAnd, prev_atomic_value, is_suspended_mask, false); | 6981 | IrInstruction *is_suspended_value = ir_build_bin_op(irb, parent_scope, node, IrBinOpBinAnd, prev_atomic_value, is_suspended_mask, false); |
| ... | @@ -6995,7 +7014,7 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod | ... | @@ -6995,7 +7014,7 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod |
| 6995 | cases[0].value = ir_mark_gen(ir_build_const_u8(irb, parent_scope, node, 0)); | 7014 | cases[0].value = ir_mark_gen(ir_build_const_u8(irb, parent_scope, node, 0)); |
| 6996 | cases[0].block = resume_block; | 7015 | cases[0].block = resume_block; |
| 6997 | cases[1].value = ir_mark_gen(ir_build_const_u8(irb, parent_scope, node, 1)); | 7016 | cases[1].value = ir_mark_gen(ir_build_const_u8(irb, parent_scope, node, 1)); |
| 6998 | cases[1].block = cleanup_block; | 7017 | cases[1].block = canceled_block; |
| 6999 | ir_mark_gen(ir_build_switch_br(irb, parent_scope, node, suspend_code, irb->exec->coro_suspend_block, | 7018 | ir_mark_gen(ir_build_switch_br(irb, parent_scope, node, suspend_code, irb->exec->coro_suspend_block, |
| 7000 | 2, cases, const_bool_false, nullptr)); | 7019 | 2, cases, const_bool_false, nullptr)); |
| 7001 | | 7020 | |