authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-25 17:34:05-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-25 17:34:05-05:00
log83f89064490350991806aea02ea6ba4b948c0376
tree974070d5e0d3cd4251a699de9476b60e57502ce7
parent4eac75914bcdf9648518d1837f48e07e35744dc1

codegen for coro_resume instruction

See #727

3 files changed, 25 insertions(+), 3 deletions(-)

src/all_types.hpp+1
...@@ -1617,6 +1617,7 @@ struct CodeGen {...@@ -1617,6 +1617,7 @@ struct CodeGen {
1617 LLVMValueRef coro_suspend_fn_val;1617 LLVMValueRef coro_suspend_fn_val;
1618 LLVMValueRef coro_end_fn_val;1618 LLVMValueRef coro_end_fn_val;
1619 LLVMValueRef coro_free_fn_val;1619 LLVMValueRef coro_free_fn_val;
1620 LLVMValueRef coro_resume_fn_val;
1620 bool error_during_imports;1621 bool error_during_imports;
16211622
1622 const char **clang_argv;1623 const char **clang_argv;
src/codegen.cpp+17-1
...@@ -1051,6 +1051,21 @@ static LLVMValueRef get_coro_free_fn_val(CodeGen *g) {...@@ -1051,6 +1051,21 @@ static LLVMValueRef get_coro_free_fn_val(CodeGen *g) {
1051 return g->coro_free_fn_val;1051 return g->coro_free_fn_val;
1052}1052}
10531053
1054static 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
1054static LLVMValueRef get_return_address_fn_val(CodeGen *g) {1069static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
1055 if (g->return_address_fn_val)1070 if (g->return_address_fn_val)
1056 return g->return_address_fn_val;1071 return g->return_address_fn_val;
...@@ -3935,7 +3950,8 @@ static LLVMValueRef ir_render_coro_free(CodeGen *g, IrExecutable *executable, Ir...@@ -3935,7 +3950,8 @@ static LLVMValueRef ir_render_coro_free(CodeGen *g, IrExecutable *executable, Ir
3935}3950}
39363951
3937static LLVMValueRef ir_render_coro_resume(CodeGen *g, IrExecutable *executable, IrInstructionCoroResume *instruction) {3952static 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, "");
3939}3955}
39403956
3941static void set_debug_location(CodeGen *g, IrInstruction *instruction) {3957static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
src/ir.cpp+7-2
...@@ -6143,14 +6143,19 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec...@@ -6143,14 +6143,19 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
61436143
6144 ir_set_cursor_at_end_and_append_block(irb, end_free_block);6144 ir_set_cursor_at_end_and_append_block(irb, end_free_block);
6145 IrBasicBlock *resume_block = ir_create_basic_block(irb, scope, "Resume");6145 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
6148 ir_set_cursor_at_end_and_append_block(irb, resume_block);6149 ir_set_cursor_at_end_and_append_block(irb, resume_block);
6149 IrInstruction *unwrapped_await_handle_ptr = ir_build_unwrap_maybe(irb, scope, node,6150 IrInstruction *unwrapped_await_handle_ptr = ir_build_unwrap_maybe(irb, scope, node,
6150 irb->exec->coro_awaiter_field_ptr, false);6151 irb->exec->coro_awaiter_field_ptr, false);
6151 IrInstruction *awaiter_handle = ir_build_load_ptr(irb, scope, node, unwrapped_await_handle_ptr);6152 IrInstruction *awaiter_handle = ir_build_load_ptr(irb, scope, node, unwrapped_await_handle_ptr);
6152 ir_build_coro_resume(irb, scope, node, awaiter_handle);6153 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);
6154 }6159 }
61556160
6156 return true;6161 return true;