Skip to content

Commit 83f8906

Browse files
committed
codegen for coro_resume instruction
See #727
1 parent 4eac759 commit 83f8906

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/all_types.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,7 @@ struct CodeGen {
16171617
LLVMValueRef coro_suspend_fn_val;
16181618
LLVMValueRef coro_end_fn_val;
16191619
LLVMValueRef coro_free_fn_val;
1620+
LLVMValueRef coro_resume_fn_val;
16201621
bool error_during_imports;
16211622

16221623
const char **clang_argv;

src/codegen.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,21 @@ static LLVMValueRef get_coro_free_fn_val(CodeGen *g) {
10511051
return g->coro_free_fn_val;
10521052
}
10531053

1054+
static LLVMValueRef get_coro_resume_fn_val(CodeGen *g) {
1055+
if (g->coro_resume_fn_val)
1056+
return g->coro_resume_fn_val;
1057+
1058+
LLVMTypeRef param_types[] = {
1059+
LLVMPointerType(LLVMInt8Type(), 0),
1060+
};
1061+
LLVMTypeRef fn_type = LLVMFunctionType(LLVMVoidType(), param_types, 1, false);
1062+
Buf *name = buf_sprintf("llvm.coro.resume");
1063+
g->coro_resume_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
1064+
assert(LLVMGetIntrinsicID(g->coro_resume_fn_val));
1065+
1066+
return g->coro_resume_fn_val;
1067+
}
1068+
10541069
static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
10551070
if (g->return_address_fn_val)
10561071
return g->return_address_fn_val;
@@ -3935,7 +3950,8 @@ static LLVMValueRef ir_render_coro_free(CodeGen *g, IrExecutable *executable, Ir
39353950
}
39363951

39373952
static LLVMValueRef ir_render_coro_resume(CodeGen *g, IrExecutable *executable, IrInstructionCoroResume *instruction) {
3938-
zig_panic("TODO ir_render_coro_resume");
3953+
LLVMValueRef awaiter_handle = ir_llvm_value(g, instruction->awaiter_handle);
3954+
return LLVMBuildCall(g->builder, get_coro_resume_fn_val(g), &awaiter_handle, 1, "");
39393955
}
39403956

39413957
static void set_debug_location(CodeGen *g, IrInstruction *instruction) {

src/ir.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -6143,14 +6143,19 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
61436143

61446144
ir_set_cursor_at_end_and_append_block(irb, end_free_block);
61456145
IrBasicBlock *resume_block = ir_create_basic_block(irb, scope, "Resume");
6146-
ir_build_cond_br(irb, scope, node, resume_awaiter, resume_block, suspend_block, const_bool_false);
6146+
IrBasicBlock *return_block = ir_create_basic_block(irb, scope, "Return");
6147+
ir_build_cond_br(irb, scope, node, resume_awaiter, resume_block, return_block, const_bool_false);
61476148

61486149
ir_set_cursor_at_end_and_append_block(irb, resume_block);
61496150
IrInstruction *unwrapped_await_handle_ptr = ir_build_unwrap_maybe(irb, scope, node,
61506151
irb->exec->coro_awaiter_field_ptr, false);
61516152
IrInstruction *awaiter_handle = ir_build_load_ptr(irb, scope, node, unwrapped_await_handle_ptr);
61526153
ir_build_coro_resume(irb, scope, node, awaiter_handle);
6153-
ir_build_br(irb, scope, node, suspend_block, const_bool_false);
6154+
ir_build_br(irb, scope, node, return_block, const_bool_false);
6155+
6156+
ir_set_cursor_at_end_and_append_block(irb, return_block);
6157+
IrInstruction *undef = ir_build_const_undefined(irb, scope, node);
6158+
ir_build_return(irb, scope, node, undef);
61546159
}
61556160

61566161
return true;

0 commit comments

Comments
 (0)