Skip to content

Commit 7567448

Browse files
committed
codegen for cancel
See #727
1 parent 05bf666 commit 7567448

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/all_types.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,7 @@ struct CodeGen {
16091609
LLVMValueRef trap_fn_val;
16101610
LLVMValueRef return_address_fn_val;
16111611
LLVMValueRef frame_address_fn_val;
1612+
LLVMValueRef coro_destroy_fn_val;
16121613
bool error_during_imports;
16131614

16141615
const char **clang_argv;

src/codegen.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,21 @@ static LLVMValueRef get_memcpy_fn_val(CodeGen *g) {
927927
return g->memcpy_fn_val;
928928
}
929929

930+
static LLVMValueRef get_coro_destroy_fn_val(CodeGen *g) {
931+
if (g->coro_destroy_fn_val)
932+
return g->coro_destroy_fn_val;
933+
934+
LLVMTypeRef param_types[] = {
935+
LLVMPointerType(LLVMInt8Type(), 0),
936+
};
937+
LLVMTypeRef fn_type = LLVMFunctionType(LLVMVoidType(), param_types, 1, false);
938+
Buf *name = buf_sprintf("llvm.coro.destroy");
939+
g->coro_destroy_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
940+
assert(LLVMGetIntrinsicID(g->coro_destroy_fn_val));
941+
942+
return g->coro_destroy_fn_val;
943+
}
944+
930945
static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
931946
if (g->return_address_fn_val)
932947
return g->return_address_fn_val;
@@ -3113,7 +3128,9 @@ static LLVMValueRef ir_render_error_return_trace(CodeGen *g, IrExecutable *execu
31133128
}
31143129

31153130
static LLVMValueRef ir_render_cancel(CodeGen *g, IrExecutable *executable, IrInstructionCancel *instruction) {
3116-
zig_panic("TODO ir_render_cancel");
3131+
LLVMValueRef target_handle = ir_llvm_value(g, instruction->target);
3132+
LLVMBuildCall(g->builder, get_coro_destroy_fn_val(g), &target_handle, 1, "");
3133+
return nullptr;
31173134
}
31183135

31193136
static LLVMValueRef ir_render_get_implicit_allocator(CodeGen *g, IrExecutable *executable, IrInstructionGetImplicitAllocator *instruction) {

0 commit comments

Comments
 (0)