authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-25 01:47:56-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-25 01:47:56-04:00
log70bced5dcffccc2f8029d8c3d7f2d18b48d993f5
tree17672a7a6787d9de275d6cad00b9854ea168e409
parentead2d32be871411685f846e604ec7e4253b9f25a
signature Commit is signed but in an unrecognized format.

implement `@frame` and `@Frame`


6 files changed, 96 insertions(+), 36 deletions(-)

BRANCH_TODO created+4
......@@ -0,0 +1,4 @@
1 * await
2 * await of a non async function
3 * async call on a non async function
4 * safety for resuming when it is awaiting
src/all_types.hpp+12-4
......@@ -1435,8 +1435,6 @@ enum BuiltinFnId {
14351435 BuiltinFnIdErrName,
14361436 BuiltinFnIdBreakpoint,
14371437 BuiltinFnIdReturnAddress,
1438 BuiltinFnIdFrameAddress,
1439 BuiltinFnIdHandle,
14401438 BuiltinFnIdEmbedFile,
14411439 BuiltinFnIdCmpxchgWeak,
14421440 BuiltinFnIdCmpxchgStrong,
......@@ -1507,6 +1505,9 @@ enum BuiltinFnId {
15071505 BuiltinFnIdAtomicLoad,
15081506 BuiltinFnIdHasDecl,
15091507 BuiltinFnIdUnionInit,
1508 BuiltinFnIdFrameAddress,
1509 BuiltinFnIdFrameType,
1510 BuiltinFnIdFrameHandle,
15101511};
15111512
15121513struct BuiltinFnEntry {
......@@ -2252,7 +2253,8 @@ enum IrInstructionId {
22522253 IrInstructionIdBreakpoint,
22532254 IrInstructionIdReturnAddress,
22542255 IrInstructionIdFrameAddress,
2255 IrInstructionIdHandle,
2256 IrInstructionIdFrameHandle,
2257 IrInstructionIdFrameType,
22562258 IrInstructionIdAlignOf,
22572259 IrInstructionIdOverflowOp,
22582260 IrInstructionIdTestErrSrc,
......@@ -3038,8 +3040,14 @@ struct IrInstructionFrameAddress {
30383040 IrInstruction base;
30393041};
30403042
3041struct IrInstructionHandle {
3043struct IrInstructionFrameHandle {
3044 IrInstruction base;
3045};
3046
3047struct IrInstructionFrameType {
30423048 IrInstruction base;
3049
3050 IrInstruction *fn;
30433051};
30443052
30453053enum IrOverflowOp {
src/codegen.cpp+8-8
......@@ -4457,10 +4457,8 @@ static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable
44574457 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->llvm_type, "");
44584458}
44594459
4460static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable,
4461 IrInstructionHandle *instruction)
4462{
4463 zig_panic("TODO @handle() codegen");
4460static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable, IrInstructionFrameHandle *instruction) {
4461 return g->cur_ret_ptr;
44644462}
44654463
44664464static LLVMValueRef render_shl_with_overflow(CodeGen *g, IrInstructionOverflowOp *instruction) {
......@@ -5008,6 +5006,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
50085006 case IrInstructionIdBitCastSrc:
50095007 case IrInstructionIdTestErrSrc:
50105008 case IrInstructionIdUnionInitNamedField:
5009 case IrInstructionIdFrameType:
50115010 zig_unreachable();
50125011
50135012 case IrInstructionIdDeclVarGen:
......@@ -5086,8 +5085,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
50865085 return ir_render_return_address(g, executable, (IrInstructionReturnAddress *)instruction);
50875086 case IrInstructionIdFrameAddress:
50885087 return ir_render_frame_address(g, executable, (IrInstructionFrameAddress *)instruction);
5089 case IrInstructionIdHandle:
5090 return ir_render_handle(g, executable, (IrInstructionHandle *)instruction);
5088 case IrInstructionIdFrameHandle:
5089 return ir_render_handle(g, executable, (IrInstructionFrameHandle *)instruction);
50915090 case IrInstructionIdOverflowOp:
50925091 return ir_render_overflow_op(g, executable, (IrInstructionOverflowOp *)instruction);
50935092 case IrInstructionIdTestErrGen:
......@@ -6754,8 +6753,6 @@ static BuiltinFnEntry *create_builtin_fn(CodeGen *g, BuiltinFnId id, const char
67546753static void define_builtin_fns(CodeGen *g) {
67556754 create_builtin_fn(g, BuiltinFnIdBreakpoint, "breakpoint", 0);
67566755 create_builtin_fn(g, BuiltinFnIdReturnAddress, "returnAddress", 0);
6757 create_builtin_fn(g, BuiltinFnIdFrameAddress, "frameAddress", 0);
6758 create_builtin_fn(g, BuiltinFnIdHandle, "handle", 0);
67596756 create_builtin_fn(g, BuiltinFnIdMemcpy, "memcpy", 3);
67606757 create_builtin_fn(g, BuiltinFnIdMemset, "memset", 3);
67616758 create_builtin_fn(g, BuiltinFnIdSizeof, "sizeOf", 1);
......@@ -6856,6 +6853,9 @@ static void define_builtin_fns(CodeGen *g) {
68566853 create_builtin_fn(g, BuiltinFnIdThis, "This", 0);
68576854 create_builtin_fn(g, BuiltinFnIdHasDecl, "hasDecl", 2);
68586855 create_builtin_fn(g, BuiltinFnIdUnionInit, "unionInit", 3);
6856 create_builtin_fn(g, BuiltinFnIdFrameHandle, "frame", 0);
6857 create_builtin_fn(g, BuiltinFnIdFrameType, "Frame", 1);
6858 create_builtin_fn(g, BuiltinFnIdFrameAddress, "frameAddress", 0);
68596859}
68606860
68616861static const char *bool_to_str(bool b) {
src/ir.cpp+51-20
......@@ -755,8 +755,12 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionFrameAddress *)
755755 return IrInstructionIdFrameAddress;
756756}
757757
758static constexpr IrInstructionId ir_instruction_id(IrInstructionHandle *) {
759 return IrInstructionIdHandle;
758static constexpr IrInstructionId ir_instruction_id(IrInstructionFrameHandle *) {
759 return IrInstructionIdFrameHandle;
760}
761
762static constexpr IrInstructionId ir_instruction_id(IrInstructionFrameType *) {
763 return IrInstructionIdFrameType;
760764}
761765
762766static constexpr IrInstructionId ir_instruction_id(IrInstructionAlignOf *) {
......@@ -2362,7 +2366,16 @@ static IrInstruction *ir_build_frame_address(IrBuilder *irb, Scope *scope, AstNo
23622366}
23632367
23642368static IrInstruction *ir_build_handle(IrBuilder *irb, Scope *scope, AstNode *source_node) {
2365 IrInstructionHandle *instruction = ir_build_instruction<IrInstructionHandle>(irb, scope, source_node);
2369 IrInstructionFrameHandle *instruction = ir_build_instruction<IrInstructionFrameHandle>(irb, scope, source_node);
2370 return &instruction->base;
2371}
2372
2373static IrInstruction *ir_build_frame_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *fn) {
2374 IrInstructionFrameType *instruction = ir_build_instruction<IrInstructionFrameType>(irb, scope, source_node);
2375 instruction->fn = fn;
2376
2377 ir_ref_instruction(fn, irb->current_basic_block);
2378
23662379 return &instruction->base;
23672380}
23682381
......@@ -3358,11 +3371,6 @@ static ScopeDeferExpr *get_scope_defer_expr(Scope *scope) {
33583371 return nullptr;
33593372}
33603373
3361static bool exec_is_async(IrExecutable *exec) {
3362 ZigFn *fn_entry = exec_fn_entry(exec);
3363 return fn_entry != nullptr && fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync;
3364}
3365
33663374static IrInstruction *ir_gen_async_return(IrBuilder *irb, Scope *scope, AstNode *node, IrInstruction *return_value,
33673375 bool is_generated_code)
33683376{
......@@ -4278,8 +4286,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
42784286 return irb->codegen->invalid_instruction;
42794287 }
42804288
4281 bool is_async = exec_is_async(irb->exec);
4282
42834289 switch (builtin_fn->id) {
42844290 case BuiltinFnIdInvalid:
42854291 zig_unreachable();
......@@ -4902,16 +4908,21 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
49024908 return ir_lval_wrap(irb, scope, ir_build_return_address(irb, scope, node), lval, result_loc);
49034909 case BuiltinFnIdFrameAddress:
49044910 return ir_lval_wrap(irb, scope, ir_build_frame_address(irb, scope, node), lval, result_loc);
4905 case BuiltinFnIdHandle:
4911 case BuiltinFnIdFrameHandle:
49064912 if (!irb->exec->fn_entry) {
49074913 add_node_error(irb->codegen, node, buf_sprintf("@handle() called outside of function definition"));
49084914 return irb->codegen->invalid_instruction;
49094915 }
4910 if (!is_async) {
4911 add_node_error(irb->codegen, node, buf_sprintf("@handle() in non-async function"));
4912 return irb->codegen->invalid_instruction;
4913 }
49144916 return ir_lval_wrap(irb, scope, ir_build_handle(irb, scope, node), lval, result_loc);
4917 case BuiltinFnIdFrameType: {
4918 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4919 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
4920 if (arg0_value == irb->codegen->invalid_instruction)
4921 return arg0_value;
4922
4923 IrInstruction *frame_type = ir_build_frame_type(irb, scope, node, arg0_value);
4924 return ir_lval_wrap(irb, scope, frame_type, lval, result_loc);
4925 }
49154926 case BuiltinFnIdAlignOf:
49164927 {
49174928 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
......@@ -21726,8 +21737,25 @@ static IrInstruction *ir_analyze_instruction_frame_address(IrAnalyze *ira, IrIns
2172621737 return result;
2172721738}
2172821739
21729static IrInstruction *ir_analyze_instruction_handle(IrAnalyze *ira, IrInstructionHandle *instruction) {
21730 zig_panic("TODO anlayze @handle()");
21740static IrInstruction *ir_analyze_instruction_frame_handle(IrAnalyze *ira, IrInstructionFrameHandle *instruction) {
21741 ZigFn *fn = exec_fn_entry(ira->new_irb.exec);
21742 ir_assert(fn != nullptr, &instruction->base);
21743
21744 ZigType *frame_type = get_coro_frame_type(ira->codegen, fn);
21745 ZigType *ptr_frame_type = get_pointer_to_type(ira->codegen, frame_type, false);
21746
21747 IrInstruction *result = ir_build_handle(&ira->new_irb, instruction->base.scope, instruction->base.source_node);
21748 result->value.type = ptr_frame_type;
21749 return result;
21750}
21751
21752static IrInstruction *ir_analyze_instruction_frame_type(IrAnalyze *ira, IrInstructionFrameType *instruction) {
21753 ZigFn *fn = ir_resolve_fn(ira, instruction->fn->child);
21754 if (fn == nullptr)
21755 return ira->codegen->invalid_instruction;
21756
21757 ZigType *ty = get_coro_frame_type(ira->codegen, fn);
21758 return ir_const_type(ira, &instruction->base, ty);
2173121759}
2173221760
2173321761static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAlignOf *instruction) {
......@@ -24355,8 +24383,10 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2435524383 return ir_analyze_instruction_return_address(ira, (IrInstructionReturnAddress *)instruction);
2435624384 case IrInstructionIdFrameAddress:
2435724385 return ir_analyze_instruction_frame_address(ira, (IrInstructionFrameAddress *)instruction);
24358 case IrInstructionIdHandle:
24359 return ir_analyze_instruction_handle(ira, (IrInstructionHandle *)instruction);
24386 case IrInstructionIdFrameHandle:
24387 return ir_analyze_instruction_frame_handle(ira, (IrInstructionFrameHandle *)instruction);
24388 case IrInstructionIdFrameType:
24389 return ir_analyze_instruction_frame_type(ira, (IrInstructionFrameType *)instruction);
2436024390 case IrInstructionIdAlignOf:
2436124391 return ir_analyze_instruction_align_of(ira, (IrInstructionAlignOf *)instruction);
2436224392 case IrInstructionIdOverflowOp:
......@@ -24650,7 +24680,8 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2465024680 case IrInstructionIdAlignOf:
2465124681 case IrInstructionIdReturnAddress:
2465224682 case IrInstructionIdFrameAddress:
24653 case IrInstructionIdHandle:
24683 case IrInstructionIdFrameHandle:
24684 case IrInstructionIdFrameType:
2465424685 case IrInstructionIdTestErrSrc:
2465524686 case IrInstructionIdTestErrGen:
2465624687 case IrInstructionIdFnProto:
src/ir_print.cpp+13-4
......@@ -906,8 +906,14 @@ static void ir_print_frame_address(IrPrint *irp, IrInstructionFrameAddress *inst
906906 fprintf(irp->f, "@frameAddress()");
907907}
908908
909static void ir_print_handle(IrPrint *irp, IrInstructionHandle *instruction) {
910 fprintf(irp->f, "@handle()");
909static void ir_print_handle(IrPrint *irp, IrInstructionFrameHandle *instruction) {
910 fprintf(irp->f, "@frame()");
911}
912
913static void ir_print_frame_type(IrPrint *irp, IrInstructionFrameType *instruction) {
914 fprintf(irp->f, "@Frame(");
915 ir_print_other_instruction(irp, instruction->fn);
916 fprintf(irp->f, ")");
911917}
912918
913919static void ir_print_return_address(IrPrint *irp, IrInstructionReturnAddress *instruction) {
......@@ -1764,8 +1770,11 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
17641770 case IrInstructionIdFrameAddress:
17651771 ir_print_frame_address(irp, (IrInstructionFrameAddress *)instruction);
17661772 break;
1767 case IrInstructionIdHandle:
1768 ir_print_handle(irp, (IrInstructionHandle *)instruction);
1773 case IrInstructionIdFrameHandle:
1774 ir_print_handle(irp, (IrInstructionFrameHandle *)instruction);
1775 break;
1776 case IrInstructionIdFrameType:
1777 ir_print_frame_type(irp, (IrInstructionFrameType *)instruction);
17691778 break;
17701779 case IrInstructionIdAlignOf:
17711780 ir_print_align_of(irp, (IrInstructionAlignOf *)instruction);
test/stage1/behavior/coroutines.zig+8
......@@ -79,15 +79,23 @@ test "local variable in async function" {
7979
8080test "calling an inferred async function" {
8181 const S = struct {
82 var x: i32 = 1;
83 var other_frame: *@Frame(other) = undefined;
84
8285 fn doTheTest() void {
8386 const p = async first();
87 expect(x == 1);
88 resume other_frame.*;
89 expect(x == 2);
8490 }
8591
8692 fn first() void {
8793 other();
8894 }
8995 fn other() void {
96 other_frame = @frame();
9097 suspend;
98 x += 1;
9199 }
92100 };
93101 S.doTheTest();