Skip to content

Commit 0cf327e

Browse files
committed
codegen for coro_suspend instruction
See #727
1 parent d0f2eca commit 0cf327e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/all_types.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,7 @@ struct CodeGen {
16141614
LLVMValueRef coro_alloc_fn_val;
16151615
LLVMValueRef coro_size_fn_val;
16161616
LLVMValueRef coro_begin_fn_val;
1617+
LLVMValueRef coro_suspend_fn_val;
16171618
bool error_during_imports;
16181619

16191620
const char **clang_argv;

src/codegen.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,22 @@ static LLVMValueRef get_coro_begin_fn_val(CodeGen *g) {
10031003
return g->coro_begin_fn_val;
10041004
}
10051005

1006+
static LLVMValueRef get_coro_suspend_fn_val(CodeGen *g) {
1007+
if (g->coro_suspend_fn_val)
1008+
return g->coro_suspend_fn_val;
1009+
1010+
LLVMTypeRef param_types[] = {
1011+
ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()),
1012+
LLVMInt1Type(),
1013+
};
1014+
LLVMTypeRef fn_type = LLVMFunctionType(LLVMInt8Type(), param_types, 2, false);
1015+
Buf *name = buf_sprintf("llvm.coro.suspend");
1016+
g->coro_suspend_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
1017+
assert(LLVMGetIntrinsicID(g->coro_suspend_fn_val));
1018+
1019+
return g->coro_suspend_fn_val;
1020+
}
1021+
10061022
static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
10071023
if (g->return_address_fn_val)
10081024
return g->return_address_fn_val;
@@ -3854,7 +3870,18 @@ static LLVMValueRef ir_render_coro_alloc_fail(CodeGen *g, IrExecutable *executab
38543870
}
38553871

38563872
static LLVMValueRef ir_render_coro_suspend(CodeGen *g, IrExecutable *executable, IrInstructionCoroSuspend *instruction) {
3857-
zig_panic("TODO ir_render_coro_suspend");
3873+
LLVMValueRef save_point;
3874+
if (instruction->save_point == nullptr) {
3875+
save_point = LLVMConstNull(ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()));
3876+
} else {
3877+
save_point = ir_llvm_value(g, instruction->save_point);
3878+
}
3879+
LLVMValueRef is_final = ir_llvm_value(g, instruction->is_final);
3880+
LLVMValueRef params[] = {
3881+
save_point,
3882+
is_final,
3883+
};
3884+
return LLVMBuildCall(g->builder, get_coro_suspend_fn_val(g), params, 2, "");
38583885
}
38593886

38603887
static LLVMValueRef ir_render_coro_end(CodeGen *g, IrExecutable *executable, IrInstructionCoroEnd *instruction) {

0 commit comments

Comments
 (0)