zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 93cbd4eeb97f30062311d6e083dd056b8fb8b021 (tree)
parent 9f6c5a20de03a59bfcaead703fe9490a6d622f84
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Sun, 25 Feb 2018 15:20:31 -0500

codegen for coro_alloc and coro_size instructions

See #727

Diffstat:
Msrc/all_types.hpp | 2++
Msrc/codegen.cpp | 32++++++++++++++++++++++++++++++--
2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/src/all_types.hpp b/src/all_types.hpp @@ -1611,6 +1611,8 @@ struct CodeGen { LLVMValueRef frame_address_fn_val; LLVMValueRef coro_destroy_fn_val; LLVMValueRef coro_id_fn_val; + LLVMValueRef coro_alloc_fn_val; + LLVMValueRef coro_size_fn_val; bool error_during_imports; const char **clang_argv; diff --git a/src/codegen.cpp b/src/codegen.cpp @@ -960,6 +960,33 @@ static LLVMValueRef get_coro_id_fn_val(CodeGen *g) { return g->coro_id_fn_val; } +static LLVMValueRef get_coro_alloc_fn_val(CodeGen *g) { + if (g->coro_alloc_fn_val) + return g->coro_alloc_fn_val; + + LLVMTypeRef param_types[] = { + ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()), + }; + LLVMTypeRef fn_type = LLVMFunctionType(LLVMInt1Type(), param_types, 1, false); + Buf *name = buf_sprintf("llvm.coro.alloc"); + g->coro_alloc_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_alloc_fn_val)); + + return g->coro_alloc_fn_val; +} + +static LLVMValueRef get_coro_size_fn_val(CodeGen *g) { + if (g->coro_size_fn_val) + return g->coro_size_fn_val; + + LLVMTypeRef fn_type = LLVMFunctionType(g->builtin_types.entry_usize->type_ref, nullptr, 0, false); + Buf *name = buf_sprintf("llvm.coro.size.i%d", g->pointer_size_bytes * 8); + g->coro_size_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_size_fn_val)); + + return g->coro_size_fn_val; +} + static LLVMValueRef get_return_address_fn_val(CodeGen *g) { if (g->return_address_fn_val) return g->return_address_fn_val; @@ -3762,11 +3789,12 @@ static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrIn } static LLVMValueRef ir_render_coro_alloc(CodeGen *g, IrExecutable *executable, IrInstructionCoroAlloc *instruction) { - zig_panic("TODO ir_render_coro_alloc"); + LLVMValueRef token = ir_llvm_value(g, instruction->coro_id); + return LLVMBuildCall(g->builder, get_coro_alloc_fn_val(g), &token, 1, ""); } static LLVMValueRef ir_render_coro_size(CodeGen *g, IrExecutable *executable, IrInstructionCoroSize *instruction) { - zig_panic("TODO ir_render_coro_size"); + return LLVMBuildCall(g->builder, get_coro_size_fn_val(g), nullptr, 0, ""); } static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, IrInstructionCoroBegin *instruction) {