Commit 253d988e7c
Changed files (7)
src/all_types.hpp
@@ -1192,6 +1192,7 @@ struct TypeTableEntry {
TypeTableEntry *pointer_parent[2]; // [0 - mut, 1 - const]
TypeTableEntry *maybe_parent;
TypeTableEntry *promise_parent;
+ TypeTableEntry *promise_frame_parent;
// If we generate a constant name value for this type, we memoize it here.
// The type of this is array
ConstExprValue *cached_const_name_val;
@@ -1641,6 +1642,7 @@ struct CodeGen {
LLVMValueRef coro_free_fn_val;
LLVMValueRef coro_resume_fn_val;
LLVMValueRef coro_save_fn_val;
+ LLVMValueRef coro_promise_fn_val;
LLVMValueRef coro_alloc_helper_fn_val;
bool error_during_imports;
@@ -2025,8 +2027,10 @@ enum IrInstructionId {
IrInstructionIdCoroFree,
IrInstructionIdCoroResume,
IrInstructionIdCoroSave,
+ IrInstructionIdCoroPromise,
IrInstructionIdCoroAllocHelper,
IrInstructionIdAtomicRmw,
+ IrInstructionIdPromiseResultType,
};
struct IrInstruction {
@@ -2943,6 +2947,12 @@ struct IrInstructionCoroSave {
IrInstruction *coro_handle;
};
+struct IrInstructionCoroPromise {
+ IrInstruction base;
+
+ IrInstruction *coro_handle;
+};
+
struct IrInstructionCoroAllocHelper {
IrInstruction base;
@@ -2962,6 +2972,12 @@ struct IrInstructionAtomicRmw {
AtomicOrder resolved_ordering;
};
+struct IrInstructionPromiseResultType {
+ IrInstruction base;
+
+ IrInstruction *promise_type;
+};
+
static const size_t slice_ptr_index = 0;
static const size_t slice_len_index = 1;
@@ -2971,6 +2987,13 @@ static const size_t maybe_null_index = 1;
static const size_t err_union_err_index = 0;
static const size_t err_union_payload_index = 1;
+#define ASYNC_ALLOC_FIELD_NAME "allocFn"
+#define ASYNC_FREE_FIELD_NAME "freeFn"
+#define AWAITER_HANDLE_FIELD_NAME "awaiter_handle"
+#define RESULT_FIELD_NAME "result"
+#define RESULT_PTR_FIELD_NAME "result_ptr"
+
+
enum FloatMode {
FloatModeOptimized,
FloatModeStrict,
src/analyze.cpp
@@ -457,6 +457,23 @@ TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool
return get_pointer_to_type_extra(g, child_type, is_const, false, get_abi_alignment(g, child_type), 0, 0);
}
+TypeTableEntry *get_promise_frame_type(CodeGen *g, TypeTableEntry *return_type) {
+ if (return_type->promise_frame_parent != nullptr) {
+ return return_type->promise_frame_parent;
+ }
+
+ TypeTableEntry *awaiter_handle_type = get_maybe_type(g, g->builtin_types.entry_promise);
+ TypeTableEntry *result_ptr_type = get_pointer_to_type(g, return_type, false);
+ const char *field_names[] = {AWAITER_HANDLE_FIELD_NAME, RESULT_FIELD_NAME, RESULT_PTR_FIELD_NAME};
+ TypeTableEntry *field_types[] = {awaiter_handle_type, return_type, result_ptr_type};
+ size_t field_count = type_has_bits(result_ptr_type) ? 3 : 1;
+ Buf *name = buf_sprintf("AsyncFramePromise(%s)", buf_ptr(&return_type->name));
+ TypeTableEntry *entry = get_struct_type(g, buf_ptr(name), field_names, field_types, field_count);
+
+ return_type->promise_frame_parent = entry;
+ return entry;
+}
+
TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) {
if (child_type->maybe_parent) {
TypeTableEntry *entry = child_type->maybe_parent;
@@ -5800,3 +5817,4 @@ bool fn_type_can_fail(FnTypeId *fn_type_id) {
return return_type->id == TypeTableEntryIdErrorUnion || return_type->id == TypeTableEntryIdErrorSet ||
fn_type_id->cc == CallingConventionAsync;
}
+
src/analyze.hpp
@@ -36,6 +36,7 @@ TypeTableEntry *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node,
TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[],
TypeTableEntry *field_types[], size_t field_count);
TypeTableEntry *get_promise_type(CodeGen *g, TypeTableEntry *result_type);
+TypeTableEntry *get_promise_frame_type(CodeGen *g, TypeTableEntry *return_type);
TypeTableEntry *get_test_fn_type(CodeGen *g);
bool handle_is_ptr(TypeTableEntry *type_entry);
void find_libc_include_path(CodeGen *g);
src/codegen.cpp
@@ -1081,6 +1081,23 @@ static LLVMValueRef get_coro_save_fn_val(CodeGen *g) {
return g->coro_save_fn_val;
}
+static LLVMValueRef get_coro_promise_fn_val(CodeGen *g) {
+ if (g->coro_promise_fn_val)
+ return g->coro_promise_fn_val;
+
+ LLVMTypeRef param_types[] = {
+ LLVMPointerType(LLVMInt8Type(), 0),
+ LLVMInt32Type(),
+ LLVMInt1Type(),
+ };
+ LLVMTypeRef fn_type = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0), param_types, 3, false);
+ Buf *name = buf_sprintf("llvm.coro.promise");
+ g->coro_promise_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
+ assert(LLVMGetIntrinsicID(g->coro_promise_fn_val));
+
+ return g->coro_promise_fn_val;
+}
+
static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
if (g->return_address_fn_val)
return g->return_address_fn_val;
@@ -4002,6 +4019,16 @@ static LLVMValueRef ir_render_coro_save(CodeGen *g, IrExecutable *executable, Ir
return LLVMBuildCall(g->builder, get_coro_save_fn_val(g), &coro_handle, 1, "");
}
+static LLVMValueRef ir_render_coro_promise(CodeGen *g, IrExecutable *executable, IrInstructionCoroPromise *instruction) {
+ LLVMValueRef coro_handle = ir_llvm_value(g, instruction->coro_handle);
+ LLVMValueRef params[] = {
+ coro_handle,
+ LLVMConstInt(LLVMInt32Type(), get_coro_frame_align_bytes(g), false),
+ LLVMConstNull(LLVMInt1Type()),
+ };
+ return LLVMBuildCall(g->builder, get_coro_promise_fn_val(g), params, 3, "");
+}
+
static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_fn_type_ref, TypeTableEntry *fn_type) {
if (g->coro_alloc_helper_fn_val != nullptr)
return g->coro_alloc_helper_fn_val;
@@ -4064,7 +4091,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
LLVMValueRef coro_size = LLVMGetParam(fn_val, next_arg);
next_arg += 1;
LLVMValueRef alignment_val = LLVMConstInt(g->builtin_types.entry_u29->type_ref,
- 2 * g->pointer_size_bytes, false);
+ get_coro_frame_align_bytes(g), false);
ZigList<LLVMValueRef> args = {};
args.append(sret_ptr);
@@ -4218,6 +4245,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
case IrInstructionIdTagType:
case IrInstructionIdExport:
case IrInstructionIdErrorUnion:
+ case IrInstructionIdPromiseResultType:
zig_unreachable();
case IrInstructionIdReturn:
@@ -4360,6 +4388,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
return ir_render_coro_resume(g, executable, (IrInstructionCoroResume *)instruction);
case IrInstructionIdCoroSave:
return ir_render_coro_save(g, executable, (IrInstructionCoroSave *)instruction);
+ case IrInstructionIdCoroPromise:
+ return ir_render_coro_promise(g, executable, (IrInstructionCoroPromise *)instruction);
case IrInstructionIdCoroAllocHelper:
return ir_render_coro_alloc_helper(g, executable, (IrInstructionCoroAllocHelper *)instruction);
case IrInstructionIdAtomicRmw:
src/ir.cpp
@@ -45,12 +45,6 @@ static LVal make_lval_addr(bool is_const, bool is_volatile) {
return { true, is_const, is_volatile };
}
-static const char * ASYNC_ALLOC_FIELD_NAME = "allocFn";
-static const char * ASYNC_FREE_FIELD_NAME = "freeFn";
-static const char * AWAITER_HANDLE_FIELD_NAME = "awaiter_handle";
-static const char * RESULT_FIELD_NAME = "result";
-static const char * RESULT_PTR_FIELD_NAME = "result_ptr";
-
enum ConstCastResultId {
ConstCastResultIdOk,
ConstCastResultIdErrSet,
@@ -697,6 +691,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroSave *) {
return IrInstructionIdCoroSave;
}
+static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroPromise *) {
+ return IrInstructionIdCoroPromise;
+}
+
static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroAllocHelper *) {
return IrInstructionIdCoroAllocHelper;
}
@@ -705,6 +703,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionAtomicRmw *) {
return IrInstructionIdAtomicRmw;
}
+static constexpr IrInstructionId ir_instruction_id(IrInstructionPromiseResultType *) {
+ return IrInstructionIdPromiseResultType;
+}
+
template<typename T>
static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) {
T *special_instruction = allocate<T>(1);
@@ -937,25 +939,19 @@ static IrInstruction *ir_build_const_c_str_lit(IrBuilder *irb, Scope *scope, Ast
static IrInstruction *ir_build_const_promise_init(IrBuilder *irb, Scope *scope, AstNode *source_node,
TypeTableEntry *return_type)
{
- TypeTableEntry *awaiter_handle_type = get_maybe_type(irb->codegen, irb->codegen->builtin_types.entry_promise);
- TypeTableEntry *result_ptr_type = get_pointer_to_type(irb->codegen, return_type, false);
- const char *field_names[] = {AWAITER_HANDLE_FIELD_NAME, RESULT_FIELD_NAME, RESULT_PTR_FIELD_NAME};
- TypeTableEntry *field_types[] = {awaiter_handle_type, return_type, result_ptr_type};
- size_t field_count = type_has_bits(result_ptr_type) ? 3 : 1;
- TypeTableEntry *struct_type = get_struct_type(irb->codegen, "AsyncFramePromise", field_names, field_types,
- field_count);
+ TypeTableEntry *struct_type = get_promise_frame_type(irb->codegen, return_type);
IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
const_instruction->base.value.type = struct_type;
const_instruction->base.value.special = ConstValSpecialStatic;
- const_instruction->base.value.data.x_struct.fields = allocate<ConstExprValue>(field_count);
- const_instruction->base.value.data.x_struct.fields[0].type = awaiter_handle_type;
+ const_instruction->base.value.data.x_struct.fields = allocate<ConstExprValue>(struct_type->data.structure.src_field_count);
+ const_instruction->base.value.data.x_struct.fields[0].type = struct_type->data.structure.fields[0].type_entry;
const_instruction->base.value.data.x_struct.fields[0].special = ConstValSpecialStatic;
const_instruction->base.value.data.x_struct.fields[0].data.x_maybe = nullptr;
- if (field_count == 3) {
+ if (struct_type->data.structure.src_field_count > 1) {
const_instruction->base.value.data.x_struct.fields[1].type = return_type;
const_instruction->base.value.data.x_struct.fields[1].special = ConstValSpecialUndef;
- const_instruction->base.value.data.x_struct.fields[2].type = result_ptr_type;
+ const_instruction->base.value.data.x_struct.fields[2].type = struct_type->data.structure.fields[2].type_entry;
const_instruction->base.value.data.x_struct.fields[2].special = ConstValSpecialUndef;
}
return &const_instruction->base;
@@ -2605,6 +2601,17 @@ static IrInstruction *ir_build_coro_save(IrBuilder *irb, Scope *scope, AstNode *
return &instruction->base;
}
+static IrInstruction *ir_build_coro_promise(IrBuilder *irb, Scope *scope, AstNode *source_node,
+ IrInstruction *coro_handle)
+{
+ IrInstructionCoroPromise *instruction = ir_build_instruction<IrInstructionCoroPromise>(irb, scope, source_node);
+ instruction->coro_handle = coro_handle;
+
+ ir_ref_instruction(coro_handle, irb->current_basic_block);
+
+ return &instruction->base;
+}
+
static IrInstruction *ir_build_coro_alloc_helper(IrBuilder *irb, Scope *scope, AstNode *source_node,
IrInstruction *alloc_fn, IrInstruction *coro_size)
{
@@ -2640,6 +2647,17 @@ static IrInstruction *ir_build_atomic_rmw(IrBuilder *irb, Scope *scope, AstNode
return &instruction->base;
}
+static IrInstruction *ir_build_promise_result_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
+ IrInstruction *promise_type)
+{
+ IrInstructionPromiseResultType *instruction = ir_build_instruction<IrInstructionPromiseResultType>(irb, scope, source_node);
+ instruction->promise_type = promise_type;
+
+ ir_ref_instruction(promise_type, irb->current_basic_block);
+
+ return &instruction->base;
+}
+
static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) {
results[ReturnKindUnconditional] = 0;
results[ReturnKindError] = 0;
@@ -5944,7 +5962,93 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *parent_scope, Ast
if (target_inst == irb->codegen->invalid_instruction)
return irb->codegen->invalid_instruction;
- zig_panic("TODO: generate await expr");
+ FnTableEntry *fn_entry = exec_fn_entry(irb->exec);
+ if (!fn_entry) {
+ add_node_error(irb->codegen, node, buf_sprintf("await outside function definition"));
+ return irb->codegen->invalid_instruction;
+ }
+ if (fn_entry->type_entry->data.fn.fn_type_id.cc != CallingConventionAsync) {
+ add_node_error(irb->codegen, node, buf_sprintf("await in non-async function"));
+ return irb->codegen->invalid_instruction;
+ }
+
+ ScopeDeferExpr *scope_defer_expr = get_scope_defer_expr(parent_scope);
+ if (scope_defer_expr) {
+ if (!scope_defer_expr->reported_err) {
+ add_node_error(irb->codegen, node, buf_sprintf("cannot await inside defer expression"));
+ scope_defer_expr->reported_err = true;
+ }
+ return irb->codegen->invalid_instruction;
+ }
+
+ Scope *outer_scope = irb->exec->begin_scope;
+
+ IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, parent_scope, node, target_inst);
+ Buf *result_ptr_field_name = buf_create_from_str(RESULT_PTR_FIELD_NAME);
+ IrInstruction *result_ptr_field_ptr = ir_build_field_ptr(irb, parent_scope, node, coro_promise_ptr, result_ptr_field_name);
+
+ Buf *awaiter_handle_field_name = buf_create_from_str(AWAITER_HANDLE_FIELD_NAME);
+ IrInstruction *awaiter_field_ptr = ir_build_field_ptr(irb, parent_scope, node, coro_promise_ptr,
+ awaiter_handle_field_name);
+
+ IrInstruction *const_bool_false = ir_build_const_bool(irb, parent_scope, node, false);
+ VariableTableEntry *result_var = ir_create_var(irb, node, parent_scope, nullptr,
+ false, false, true, const_bool_false);
+ IrInstruction *undefined_value = ir_build_const_undefined(irb, parent_scope, node);
+ IrInstruction *target_promise_type = ir_build_typeof(irb, parent_scope, node, target_inst);
+ IrInstruction *promise_result_type = ir_build_promise_result_type(irb, parent_scope, node, target_promise_type);
+ ir_build_var_decl(irb, parent_scope, node, result_var, promise_result_type, nullptr, undefined_value);
+ IrInstruction *my_result_var_ptr = ir_build_var_ptr(irb, parent_scope, node, result_var, false, false);
+ ir_build_store_ptr(irb, parent_scope, node, result_ptr_field_ptr, my_result_var_ptr);
+ IrInstruction *save_token = ir_build_coro_save(irb, parent_scope, node, irb->exec->coro_handle);
+ IrInstruction *promise_type_val = ir_build_const_type(irb, parent_scope, node,
+ get_maybe_type(irb->codegen, irb->codegen->builtin_types.entry_promise));
+ IrInstruction *maybe_await_handle = ir_build_atomic_rmw(irb, parent_scope, node,
+ promise_type_val, awaiter_field_ptr, nullptr, irb->exec->coro_handle, nullptr,
+ AtomicRmwOp_xchg, AtomicOrderSeqCst);
+ IrInstruction *is_non_null = ir_build_test_nonnull(irb, parent_scope, node, maybe_await_handle);
+ IrBasicBlock *yes_suspend_block = ir_create_basic_block(irb, parent_scope, "YesSuspend");
+ IrBasicBlock *no_suspend_block = ir_create_basic_block(irb, parent_scope, "NoSuspend");
+ IrBasicBlock *merge_block = ir_create_basic_block(irb, parent_scope, "Merge");
+ ir_build_cond_br(irb, parent_scope, node, is_non_null, no_suspend_block, yes_suspend_block, const_bool_false);
+
+ ir_set_cursor_at_end_and_append_block(irb, no_suspend_block);
+ Buf *result_field_name = buf_create_from_str(RESULT_FIELD_NAME);
+ IrInstruction *promise_result_ptr = ir_build_field_ptr(irb, parent_scope, node, coro_promise_ptr, result_field_name);
+ IrInstruction *no_suspend_result = ir_build_load_ptr(irb, parent_scope, node, promise_result_ptr);
+ ir_build_cancel(irb, parent_scope, node, target_inst);
+ ir_build_br(irb, parent_scope, node, merge_block, const_bool_false);
+
+ ir_set_cursor_at_end_and_append_block(irb, yes_suspend_block);
+ ir_build_coro_resume(irb, parent_scope, node, target_inst);
+ IrInstruction *suspend_code = ir_build_coro_suspend(irb, parent_scope, node, save_token, const_bool_false);
+ IrBasicBlock *cleanup_block = ir_create_basic_block(irb, parent_scope, "SuspendCleanup");
+ IrBasicBlock *resume_block = ir_create_basic_block(irb, parent_scope, "SuspendResume");
+
+ IrInstructionSwitchBrCase *cases = allocate<IrInstructionSwitchBrCase>(2);
+ cases[0].value = ir_build_const_u8(irb, parent_scope, node, 0);
+ cases[0].block = resume_block;
+ cases[1].value = ir_build_const_u8(irb, parent_scope, node, 1);
+ cases[1].block = cleanup_block;
+ ir_build_switch_br(irb, parent_scope, node, suspend_code, irb->exec->coro_suspend_block,
+ 2, cases, const_bool_false);
+
+ ir_set_cursor_at_end_and_append_block(irb, cleanup_block);
+ ir_gen_defers_for_block(irb, parent_scope, outer_scope, true);
+ ir_build_br(irb, parent_scope, node, irb->exec->coro_final_cleanup_block, const_bool_false);
+
+ ir_set_cursor_at_end_and_append_block(irb, resume_block);
+ IrInstruction *yes_suspend_result = ir_build_load_ptr(irb, parent_scope, node, my_result_var_ptr);
+ ir_build_br(irb, parent_scope, node, merge_block, const_bool_false);
+
+ ir_set_cursor_at_end_and_append_block(irb, merge_block);
+ IrBasicBlock **incoming_blocks = allocate<IrBasicBlock *>(2);
+ IrInstruction **incoming_values = allocate<IrInstruction *>(2);
+ incoming_blocks[0] = resume_block;
+ incoming_values[0] = yes_suspend_result;
+ incoming_blocks[1] = no_suspend_block;
+ incoming_values[1] = no_suspend_result;
+ return ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values);
}
static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
@@ -17399,6 +17503,29 @@ static TypeTableEntry *ir_analyze_instruction_coro_save(IrAnalyze *ira, IrInstru
return result->value.type;
}
+static TypeTableEntry *ir_analyze_instruction_coro_promise(IrAnalyze *ira, IrInstructionCoroPromise *instruction) {
+ IrInstruction *coro_handle = instruction->coro_handle->other;
+ if (type_is_invalid(coro_handle->value.type))
+ return ira->codegen->builtin_types.entry_invalid;
+
+ if (coro_handle->value.type->id != TypeTableEntryIdPromise ||
+ coro_handle->value.type->data.promise.result_type == nullptr)
+ {
+ ir_add_error(ira, &instruction->base, buf_sprintf("expected promise->T, found '%s'",
+ buf_ptr(&coro_handle->value.type->name)));
+ return ira->codegen->builtin_types.entry_invalid;
+ }
+
+ TypeTableEntry *coro_frame_type = get_promise_frame_type(ira->codegen,
+ coro_handle->value.type->data.promise.result_type);
+
+ IrInstruction *result = ir_build_coro_promise(&ira->new_irb, instruction->base.scope,
+ instruction->base.source_node, coro_handle);
+ ir_link_new_instruction(result, &instruction->base);
+ result->value.type = get_pointer_to_type(ira->codegen, coro_frame_type, false);
+ return result->value.type;
+}
+
static TypeTableEntry *ir_analyze_instruction_coro_alloc_helper(IrAnalyze *ira, IrInstructionCoroAllocHelper *instruction) {
IrInstruction *alloc_fn = instruction->alloc_fn->other;
if (type_is_invalid(alloc_fn->value.type))
@@ -17492,6 +17619,22 @@ static TypeTableEntry *ir_analyze_instruction_atomic_rmw(IrAnalyze *ira, IrInstr
return result->value.type;
}
+static TypeTableEntry *ir_analyze_instruction_promise_result_type(IrAnalyze *ira, IrInstructionPromiseResultType *instruction) {
+ TypeTableEntry *promise_type = ir_resolve_type(ira, instruction->promise_type->other);
+ if (type_is_invalid(promise_type))
+ return ira->codegen->builtin_types.entry_invalid;
+
+ if (promise_type->id != TypeTableEntryIdPromise || promise_type->data.promise.result_type == nullptr) {
+ ir_add_error(ira, &instruction->base, buf_sprintf("expected promise->T, found '%s'",
+ buf_ptr(&promise_type->name)));
+ return ira->codegen->builtin_types.entry_invalid;
+ }
+
+ ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
+ out_val->data.x_type = promise_type->data.promise.result_type;
+ return ira->codegen->builtin_types.entry_type;
+}
+
static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {
switch (instruction->id) {
@@ -17719,10 +17862,14 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
return ir_analyze_instruction_coro_resume(ira, (IrInstructionCoroResume *)instruction);
case IrInstructionIdCoroSave:
return ir_analyze_instruction_coro_save(ira, (IrInstructionCoroSave *)instruction);
+ case IrInstructionIdCoroPromise:
+ return ir_analyze_instruction_coro_promise(ira, (IrInstructionCoroPromise *)instruction);
case IrInstructionIdCoroAllocHelper:
return ir_analyze_instruction_coro_alloc_helper(ira, (IrInstructionCoroAllocHelper *)instruction);
case IrInstructionIdAtomicRmw:
return ir_analyze_instruction_atomic_rmw(ira, (IrInstructionAtomicRmw *)instruction);
+ case IrInstructionIdPromiseResultType:
+ return ir_analyze_instruction_promise_result_type(ira, (IrInstructionPromiseResultType *)instruction);
}
zig_unreachable();
}
@@ -17927,6 +18074,8 @@ bool ir_has_side_effects(IrInstruction *instruction) {
case IrInstructionIdCoroSuspend:
case IrInstructionIdCoroFree:
case IrInstructionIdAtomicRmw:
+ case IrInstructionIdCoroPromise:
+ case IrInstructionIdPromiseResultType:
return false;
case IrInstructionIdAsm:
src/ir_print.cpp
@@ -839,7 +839,11 @@ static void ir_print_ptr_to_int(IrPrint *irp, IrInstructionPtrToInt *instruction
static void ir_print_int_to_ptr(IrPrint *irp, IrInstructionIntToPtr *instruction) {
fprintf(irp->f, "@intToPtr(");
- ir_print_other_instruction(irp, instruction->dest_type);
+ if (instruction->dest_type == nullptr) {
+ fprintf(irp->f, "(null)");
+ } else {
+ ir_print_other_instruction(irp, instruction->dest_type);
+ }
fprintf(irp->f, ",");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
@@ -1105,6 +1109,18 @@ static void ir_print_coro_save(IrPrint *irp, IrInstructionCoroSave *instruction)
fprintf(irp->f, ")");
}
+static void ir_print_coro_promise(IrPrint *irp, IrInstructionCoroPromise *instruction) {
+ fprintf(irp->f, "@coroPromise(");
+ ir_print_other_instruction(irp, instruction->coro_handle);
+ fprintf(irp->f, ")");
+}
+
+static void ir_print_promise_result_type(IrPrint *irp, IrInstructionPromiseResultType *instruction) {
+ fprintf(irp->f, "@PromiseResultType(");
+ ir_print_other_instruction(irp, instruction->promise_type);
+ fprintf(irp->f, ")");
+}
+
static void ir_print_coro_alloc_helper(IrPrint *irp, IrInstructionCoroAllocHelper *instruction) {
fprintf(irp->f, "@coroAllocHelper(");
ir_print_other_instruction(irp, instruction->alloc_fn);
@@ -1501,6 +1517,12 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
case IrInstructionIdAtomicRmw:
ir_print_atomic_rmw(irp, (IrInstructionAtomicRmw *)instruction);
break;
+ case IrInstructionIdCoroPromise:
+ ir_print_coro_promise(irp, (IrInstructionCoroPromise *)instruction);
+ break;
+ case IrInstructionIdPromiseResultType:
+ ir_print_promise_result_type(irp, (IrInstructionPromiseResultType *)instruction);
+ break;
}
fprintf(irp->f, "\n");
}
test/cases/coroutines.zig
@@ -4,7 +4,7 @@ const assert = std.debug.assert;
var x: i32 = 1;
test "create a coroutine and cancel it" {
- const p = try (async(std.debug.global_allocator) simpleAsyncFn());
+ const p = try async(std.debug.global_allocator) simpleAsyncFn();
cancel p;
assert(x == 2);
}
@@ -17,7 +17,7 @@ async fn simpleAsyncFn() void {
test "coroutine suspend, resume, cancel" {
seq('a');
- const p = (async(std.debug.global_allocator) testAsyncSeq()) catch unreachable;
+ const p = try async(std.debug.global_allocator) testAsyncSeq();
seq('c');
resume p;
seq('f');
@@ -43,7 +43,7 @@ fn seq(c: u8) void {
}
test "coroutine suspend with block" {
- const p = (async(std.debug.global_allocator) testSuspendBlock()) catch unreachable;
+ const p = try async(std.debug.global_allocator) testSuspendBlock();
std.debug.assert(!result);
resume a_promise;
std.debug.assert(result);