From cd18186715bf532715c03d8e67096606fe0f2bee Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Fri, 27 Jul 2018 19:18:29 +0900 Subject: src/codegen.cpp: base handle builtin on `@frameAddress()`; Tracking Issue #1296 ; --- src/codegen.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 7420da9797..336aded82c 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -4146,6 +4146,15 @@ static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable return LLVMBuildCall(g->builder, get_frame_address_fn_val(g), &zero, 1, ""); } +static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable, + IrInstructionHandle *instruction) +{ + // @andrewrk, not sure what to place here ? + // `get_promise_frame_type` ? + LLVMValueRef handle = LLVMConstNull(g->builtin_types.entry_promise->type_ref); + return LLVMBuildRet(g->builder, handle); +} + static LLVMValueRef render_shl_with_overflow(CodeGen *g, IrInstructionOverflowOp *instruction) { TypeTableEntry *int_type = instruction->result_ptr_type; assert(int_type->id == TypeTableEntryIdInt); @@ -4910,6 +4919,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_return_address(g, executable, (IrInstructionReturnAddress *)instruction); case IrInstructionIdFrameAddress: return ir_render_frame_address(g, executable, (IrInstructionFrameAddress *)instruction); + case IrInstructionIdHandle: + return ir_render_handle(g, executable, (IrInstructionHandle *)instruction); case IrInstructionIdOverflowOp: return ir_render_overflow_op(g, executable, (IrInstructionOverflowOp *)instruction); case IrInstructionIdTestErr: @@ -6344,6 +6355,7 @@ static void define_builtin_fns(CodeGen *g) { create_builtin_fn(g, BuiltinFnIdBreakpoint, "breakpoint", 0); create_builtin_fn(g, BuiltinFnIdReturnAddress, "returnAddress", 0); create_builtin_fn(g, BuiltinFnIdFrameAddress, "frameAddress", 0); + create_builtin_fn(g, BuiltinFnIdHandle, "handle", 0); create_builtin_fn(g, BuiltinFnIdMemcpy, "memcpy", 3); create_builtin_fn(g, BuiltinFnIdMemset, "memset", 3); create_builtin_fn(g, BuiltinFnIdSizeof, "sizeOf", 1); -- cgit v1.2.3 From a2e5691228c61e5c56220fc8f5f72e47b0611000 Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Sat, 28 Jul 2018 11:46:31 +0900 Subject: src/codegen.cpp: return null if calling convention is not async; Tracking Issue #1296 ; --- src/codegen.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 336aded82c..cbd1955839 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -4149,9 +4149,15 @@ static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable, IrInstructionHandle *instruction) { - // @andrewrk, not sure what to place here ? - // `get_promise_frame_type` ? - LLVMValueRef handle = LLVMConstNull(g->builtin_types.entry_promise->type_ref); + + bool is_async = executable->fn_entry != nullptr && + executable->fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync; + + if (!is_async || !executable->coro_handle) { + return LLVMConstNull(g->builtin_types.entry_promise->type_ref); + } + + LLVMValueRef handle = ir_llvm_value(g, executable->coro_handle); return LLVMBuildRet(g->builder, handle); } -- cgit v1.2.3 From 81f463626ad09dd09e525cda140fb63baf11bc73 Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Sat, 28 Jul 2018 14:13:26 +0900 Subject: src/codegen.cpp: add/throw error for @handle() in a non async context; Tracking Issue #1296 ; I removed/commented-out the assert checking for no errors since we now have some errors rendered. --- src/codegen.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index cbd1955839..43e2a0b694 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -4154,6 +4154,7 @@ static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable, executable->fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync; if (!is_async || !executable->coro_handle) { + add_node_error(g, instruction->base.source_node, buf_sprintf("@handle() in non-async function")); return LLVMConstNull(g->builtin_types.entry_promise->type_ref); } @@ -6022,7 +6023,8 @@ static void do_code_gen(CodeGen *g) { ir_render(g, fn_table_entry); } - assert(!g->errors.length); + + //assert(!g->errors.length); if (buf_len(&g->global_asm) != 0) { LLVMSetModuleInlineAsm(g->module, buf_ptr(&g->global_asm)); -- cgit v1.2.3 From 0ee65025623c0440ed655d68b579f17e6d6e5f5c Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Sat, 28 Jul 2018 15:20:56 +0900 Subject: src/codegen.cpp: remove `add_node_error` from `ir_render_handle`; Tracking Issue #1296 ; Thanks @andrewrk ; --- src/codegen.cpp | 12 +----------- test/cases/coroutines.zig | 3 +++ 2 files changed, 4 insertions(+), 11 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 43e2a0b694..8a18a3f8dd 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -4149,17 +4149,7 @@ static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable, IrInstructionHandle *instruction) { - - bool is_async = executable->fn_entry != nullptr && - executable->fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync; - - if (!is_async || !executable->coro_handle) { - add_node_error(g, instruction->base.source_node, buf_sprintf("@handle() in non-async function")); - return LLVMConstNull(g->builtin_types.entry_promise->type_ref); - } - - LLVMValueRef handle = ir_llvm_value(g, executable->coro_handle); - return LLVMBuildRet(g->builder, handle); + return LLVMConstNull(g->builtin_types.entry_promise->type_ref); } static LLVMValueRef render_shl_with_overflow(CodeGen *g, IrInstructionOverflowOp *instruction) { diff --git a/test/cases/coroutines.zig b/test/cases/coroutines.zig index 53c5c3f906..deb4aa1b24 100644 --- a/test/cases/coroutines.zig +++ b/test/cases/coroutines.zig @@ -57,6 +57,9 @@ test "coroutine suspend with block" { resume a_promise; std.debug.assert(result); cancel p; + + assert( @handle() ); + } var a_promise: promise = undefined; -- cgit v1.2.3 From db362bec185d2eeb5a4dd018deb2a988479f0f1a Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Sat, 28 Jul 2018 15:22:00 +0900 Subject: src/codegen.cpp: reassert that there are no generated errors in codegen; Tracking Issue #1296 ; Thanks @andrewrk ; --- src/codegen.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 8a18a3f8dd..54effb9480 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -6014,7 +6014,7 @@ static void do_code_gen(CodeGen *g) { } - //assert(!g->errors.length); + assert(!g->errors.length); if (buf_len(&g->global_asm) != 0) { LLVMSetModuleInlineAsm(g->module, buf_ptr(&g->global_asm)); -- cgit v1.2.3 From 104bdb03d6b5906716efeb84045079a424bf650a Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Sat, 28 Jul 2018 23:29:40 +0900 Subject: src/codegen.cpp: return promise instead of null promise; Tracking Issue #1296 ; --- src/codegen.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 54effb9480..bd708e3824 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -4149,7 +4149,8 @@ static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable, IrInstructionHandle *instruction) { - return LLVMConstNull(g->builtin_types.entry_promise->type_ref); + LLVMValueRef ptr = ir_llvm_value(g, executable->fn_entry->ir_executable.coro_handle->other); + return LLVMBuildBitCast(g->builder, ptr, g->builtin_types.entry_promise->type_ref, ""); } static LLVMValueRef render_shl_with_overflow(CodeGen *g, IrInstructionOverflowOp *instruction) { -- cgit v1.2.3 From 92cb330e160388bca7ddd7ceecbd7157ce92247b Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Sun, 29 Jul 2018 12:27:50 +0900 Subject: src/codegen.cpp: @handle(): replace hacky ref chain with llvm intrinsic; Tracking Issue #1296 ; --- src/all_types.hpp | 1 + src/codegen.cpp | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index f03a250aea..8d55a75f9f 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1717,6 +1717,7 @@ struct CodeGen { LLVMValueRef coro_save_fn_val; LLVMValueRef coro_promise_fn_val; LLVMValueRef coro_alloc_helper_fn_val; + LLVMValueRef coro_frame_fn_val; LLVMValueRef merge_err_ret_traces_fn_val; LLVMValueRef add_error_return_trace_addr_fn_val; LLVMValueRef stacksave_fn_val; diff --git a/src/codegen.cpp b/src/codegen.cpp index bd708e3824..539356ef2f 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -4146,11 +4146,24 @@ static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable return LLVMBuildCall(g->builder, get_frame_address_fn_val(g), &zero, 1, ""); } +static LLVMValueRef get_handle_fn_val(CodeGen *g) { + if (g->coro_frame_fn_val) + return g->coro_frame_fn_val; + + LLVMTypeRef fn_type = LLVMFunctionType( LLVMPointerType(LLVMInt8Type(), 0) + , nullptr, 0, false); + Buf *name = buf_sprintf("llvm.coro.frame"); + g->coro_frame_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_frame_fn_val)); + + return g->coro_frame_fn_val; +} + static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable, IrInstructionHandle *instruction) { - LLVMValueRef ptr = ir_llvm_value(g, executable->fn_entry->ir_executable.coro_handle->other); - return LLVMBuildBitCast(g->builder, ptr, g->builtin_types.entry_promise->type_ref, ""); + LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_promise->type_ref); + return LLVMBuildCall(g->builder, get_handle_fn_val(g), &zero, 0, ""); } static LLVMValueRef render_shl_with_overflow(CodeGen *g, IrInstructionOverflowOp *instruction) { -- cgit v1.2.3