| ... | ... | @@ -6663,14 +6663,45 @@ static IrInstruction *ir_gen_fn_proto(IrBuilder *irb, Scope *parent_scope, AstNo |
| 6663 | 6663 | async_allocator_type_value, is_var_args); |
| 6664 | 6664 | } |
| 6665 | 6665 | |
| 6666 | | static IrInstruction *ir_gen_cancel(IrBuilder *irb, Scope *parent_scope, AstNode *node) { |
| 6666 | static IrInstruction *ir_gen_cancel(IrBuilder *irb, Scope *scope, AstNode *node) { |
| 6667 | 6667 | assert(node->type == NodeTypeCancel); |
| 6668 | 6668 | |
| 6669 | | IrInstruction *target_inst = ir_gen_node(irb, node->data.cancel_expr.expr, parent_scope); |
| 6669 | IrInstruction *target_inst = ir_gen_node(irb, node->data.cancel_expr.expr, scope); |
| 6670 | 6670 | if (target_inst == irb->codegen->invalid_instruction) |
| 6671 | 6671 | return irb->codegen->invalid_instruction; |
| 6672 | 6672 | |
| 6673 | | return ir_build_cancel(irb, parent_scope, node, target_inst); |
| 6673 | IrBasicBlock *done_block = ir_create_basic_block(irb, scope, "CancelDone"); |
| 6674 | IrBasicBlock *not_canceled_block = ir_create_basic_block(irb, scope, "NotCanceled"); |
| 6675 | |
| 6676 | IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0); |
| 6677 | IrInstruction *usize_type_val = ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_usize); |
| 6678 | IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node, false); |
| 6679 | IrInstruction *is_canceled_mask = ir_build_const_usize(irb, scope, node, 0x1); // 0b001 |
| 6680 | IrInstruction *promise_T_type_val = ir_build_const_type(irb, scope, node, |
| 6681 | get_promise_type(irb->codegen, irb->codegen->builtin_types.entry_void)); |
| 6682 | |
| 6683 | // TODO relies on Zig not re-ordering fields |
| 6684 | IrInstruction *casted_target_inst = ir_build_ptr_cast(irb, scope, node, promise_T_type_val, target_inst); |
| 6685 | IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, scope, node, casted_target_inst); |
| 6686 | Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME); |
| 6687 | IrInstruction *atomic_state_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, |
| 6688 | atomic_state_field_name); |
| 6689 | |
| 6690 | // set the is_canceled bit |
| 6691 | IrInstruction *prev_atomic_value = ir_build_atomic_rmw(irb, scope, node, |
| 6692 | usize_type_val, atomic_state_ptr, nullptr, is_canceled_mask, nullptr, |
| 6693 | AtomicRmwOp_and, AtomicOrderSeqCst); |
| 6694 | |
| 6695 | IrInstruction *is_canceled_value = ir_build_bin_op(irb, scope, node, IrBinOpBinAnd, prev_atomic_value, is_canceled_mask, false); |
| 6696 | IrInstruction *is_canceled_bool = ir_build_bin_op(irb, scope, node, IrBinOpCmpNotEq, is_canceled_value, zero, false); |
| 6697 | ir_build_cond_br(irb, scope, node, is_canceled_bool, done_block, not_canceled_block, is_comptime); |
| 6698 | |
| 6699 | ir_set_cursor_at_end_and_append_block(irb, not_canceled_block); |
| 6700 | ir_build_cancel(irb, scope, node, target_inst); |
| 6701 | ir_build_br(irb, scope, node, done_block, is_comptime); |
| 6702 | |
| 6703 | ir_set_cursor_at_end_and_append_block(irb, done_block); |
| 6704 | return ir_build_const_void(irb, scope, node); |
| 6674 | 6705 | } |
| 6675 | 6706 | |
| 6676 | 6707 | static IrInstruction *ir_gen_resume(IrBuilder *irb, Scope *scope, AstNode *node) { |