authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-25 15:20:31-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-25 15:20:31-05:00
log93cbd4eeb97f30062311d6e083dd056b8fb8b021
treee8644b453c39df5ecb7c8aec88fd20a089930b56
parent9f6c5a20de03a59bfcaead703fe9490a6d622f84

codegen for coro_alloc and coro_size instructions

See #727

2 files changed, 32 insertions(+), 2 deletions(-)

src/all_types.hpp+2
...@@ -1611,6 +1611,8 @@ struct CodeGen {...@@ -1611,6 +1611,8 @@ struct CodeGen {
1611 LLVMValueRef frame_address_fn_val;1611 LLVMValueRef frame_address_fn_val;
1612 LLVMValueRef coro_destroy_fn_val;1612 LLVMValueRef coro_destroy_fn_val;
1613 LLVMValueRef coro_id_fn_val;1613 LLVMValueRef coro_id_fn_val;
1614 LLVMValueRef coro_alloc_fn_val;
1615 LLVMValueRef coro_size_fn_val;
1614 bool error_during_imports;1616 bool error_during_imports;
16151617
1616 const char **clang_argv;1618 const char **clang_argv;
src/codegen.cpp+30-2
...@@ -960,6 +960,33 @@ static LLVMValueRef get_coro_id_fn_val(CodeGen *g) {...@@ -960,6 +960,33 @@ static LLVMValueRef get_coro_id_fn_val(CodeGen *g) {
960 return g->coro_id_fn_val;960 return g->coro_id_fn_val;
961}961}
962962
963static 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
978static 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
963static LLVMValueRef get_return_address_fn_val(CodeGen *g) {990static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
964 if (g->return_address_fn_val)991 if (g->return_address_fn_val)
965 return g->return_address_fn_val;992 return g->return_address_fn_val;
...@@ -3762,11 +3789,12 @@ static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrIn...@@ -3762,11 +3789,12 @@ static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrIn
3762}3789}
37633790
3764static LLVMValueRef ir_render_coro_alloc(CodeGen *g, IrExecutable *executable, IrInstructionCoroAlloc *instruction) {3791static 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, "");
3766}3794}
37673795
3768static LLVMValueRef ir_render_coro_size(CodeGen *g, IrExecutable *executable, IrInstructionCoroSize *instruction) {3796static 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, "");
3770}3798}
37713799
3772static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, IrInstructionCoroBegin *instruction) {3800static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, IrInstructionCoroBegin *instruction) {