Skip to content

Commit 93cbd4e

Browse files
committed
codegen for coro_alloc and coro_size instructions
See #727
1 parent 9f6c5a2 commit 93cbd4e

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/all_types.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,8 @@ struct CodeGen {
16111611
LLVMValueRef frame_address_fn_val;
16121612
LLVMValueRef coro_destroy_fn_val;
16131613
LLVMValueRef coro_id_fn_val;
1614+
LLVMValueRef coro_alloc_fn_val;
1615+
LLVMValueRef coro_size_fn_val;
16141616
bool error_during_imports;
16151617

16161618
const char **clang_argv;

src/codegen.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,33 @@ static LLVMValueRef get_coro_id_fn_val(CodeGen *g) {
960960
return g->coro_id_fn_val;
961961
}
962962

963+
static LLVMValueRef get_coro_alloc_fn_val(CodeGen *g) {
964+
if (g->coro_alloc_fn_val)
965+
return g->coro_alloc_fn_val;
966+
967+
LLVMTypeRef param_types[] = {
968+
ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()),
969+
};
970+
LLVMTypeRef fn_type = LLVMFunctionType(LLVMInt1Type(), param_types, 1, false);
971+
Buf *name = buf_sprintf("llvm.coro.alloc");
972+
g->coro_alloc_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
973+
assert(LLVMGetIntrinsicID(g->coro_alloc_fn_val));
974+
975+
return g->coro_alloc_fn_val;
976+
}
977+
978+
static LLVMValueRef get_coro_size_fn_val(CodeGen *g) {
979+
if (g->coro_size_fn_val)
980+
return g->coro_size_fn_val;
981+
982+
LLVMTypeRef fn_type = LLVMFunctionType(g->builtin_types.entry_usize->type_ref, nullptr, 0, false);
983+
Buf *name = buf_sprintf("llvm.coro.size.i%d", g->pointer_size_bytes * 8);
984+
g->coro_size_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
985+
assert(LLVMGetIntrinsicID(g->coro_size_fn_val));
986+
987+
return g->coro_size_fn_val;
988+
}
989+
963990
static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
964991
if (g->return_address_fn_val)
965992
return g->return_address_fn_val;
@@ -3762,11 +3789,12 @@ static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrIn
37623789
}
37633790

37643791
static LLVMValueRef ir_render_coro_alloc(CodeGen *g, IrExecutable *executable, IrInstructionCoroAlloc *instruction) {
3765-
zig_panic("TODO ir_render_coro_alloc");
3792+
LLVMValueRef token = ir_llvm_value(g, instruction->coro_id);
3793+
return LLVMBuildCall(g->builder, get_coro_alloc_fn_val(g), &token, 1, "");
37663794
}
37673795

37683796
static LLVMValueRef ir_render_coro_size(CodeGen *g, IrExecutable *executable, IrInstructionCoroSize *instruction) {
3769-
zig_panic("TODO ir_render_coro_size");
3797+
return LLVMBuildCall(g->builder, get_coro_size_fn_val(g), nullptr, 0, "");
37703798
}
37713799

37723800
static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, IrInstructionCoroBegin *instruction) {

0 commit comments

Comments
 (0)