authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-24 14:39:44-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-24 14:39:44-07:00
log15922069651ce964b6d12b9ad83ae02aa2266e89
tree459f2572e09a0c475aee7e51c179e0e787c8ea5f
parente86cee258cb0eefca14a94f6b3abb39e8a5f2ef9

AstGen: implement await and resume

based on @Vexu's code from before

3 files changed, 52 insertions(+), 2 deletions(-)

src/AstGen.zig+22-2
...@@ -959,7 +959,19 @@ pub fn awaitExpr(...@@ -959,7 +959,19 @@ pub fn awaitExpr(
959 node: ast.Node.Index,959 node: ast.Node.Index,
960) InnerError!Zir.Inst.Ref {960) InnerError!Zir.Inst.Ref {
961 const astgen = gz.astgen;961 const astgen = gz.astgen;
962 return astgen.failNode(node, "TODO AstGen awaitExpr", .{});962 const tree = &astgen.file.tree;
963 const node_datas = tree.nodes.items(.data);
964 const rhs_node = node_datas[node].lhs;
965
966 if (gz.suspend_node != 0) {
967 return astgen.failNodeNotes(node, "cannot await inside suspend block", .{}, &[_]u32{
968 try astgen.errNoteNode(gz.suspend_node, "suspend block here", .{}),
969 });
970 }
971 const operand = try expr(gz, scope, .none, rhs_node);
972 const tag: Zir.Inst.Tag = if (gz.nosuspend_node != 0) .await_nosuspend else .@"await";
973 const result = try gz.addUnNode(tag, operand, node);
974 return rvalue(gz, scope, rl, result, node);
963}975}
964976
965pub fn resumeExpr(977pub fn resumeExpr(
...@@ -969,7 +981,12 @@ pub fn resumeExpr(...@@ -969,7 +981,12 @@ pub fn resumeExpr(
969 node: ast.Node.Index,981 node: ast.Node.Index,
970) InnerError!Zir.Inst.Ref {982) InnerError!Zir.Inst.Ref {
971 const astgen = gz.astgen;983 const astgen = gz.astgen;
972 return astgen.failNode(node, "TODO AstGen resumeExpr", .{});984 const tree = &astgen.file.tree;
985 const node_datas = tree.nodes.items(.data);
986 const rhs_node = node_datas[node].lhs;
987 const operand = try expr(gz, scope, .none, rhs_node);
988 const result = try gz.addUnNode(.@"resume", operand, node);
989 return rvalue(gz, scope, rl, result, node);
973}990}
974991
975pub fn fnProtoExpr(992pub fn fnProtoExpr(
...@@ -2005,6 +2022,9 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner...@@ -2005,6 +2022,9 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner
2005 .memset,2022 .memset,
2006 .builtin_async_call,2023 .builtin_async_call,
2007 .c_import,2024 .c_import,
2025 .@"resume",
2026 .@"await",
2027 .await_nosuspend,
2008 .extended,2028 .extended,
2009 => break :b false,2029 => break :b false,
20102030
src/Sema.zig+20
...@@ -317,6 +317,9 @@ pub fn analyzeBody(...@@ -317,6 +317,9 @@ pub fn analyzeBody(
317 .memcpy => try sema.zirMemcpy(block, inst),317 .memcpy => try sema.zirMemcpy(block, inst),
318 .memset => try sema.zirMemset(block, inst),318 .memset => try sema.zirMemset(block, inst),
319 .builtin_async_call => try sema.zirBuiltinAsyncCall(block, inst),319 .builtin_async_call => try sema.zirBuiltinAsyncCall(block, inst),
320 .@"resume" => try sema.zirResume(block, inst),
321 .@"await" => try sema.zirAwait(block, inst, false),
322 .await_nosuspend => try sema.zirAwait(block, inst, true),
320 .extended => try sema.zirExtended(block, inst),323 .extended => try sema.zirExtended(block, inst),
321324
322 .sqrt => try sema.zirUnaryMath(block, inst),325 .sqrt => try sema.zirUnaryMath(block, inst),
...@@ -5413,6 +5416,23 @@ fn zirBuiltinAsyncCall(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) I...@@ -5413,6 +5416,23 @@ fn zirBuiltinAsyncCall(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) I
5413 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBuiltinAsyncCall", .{});5416 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBuiltinAsyncCall", .{});
5414}5417}
54155418
5419fn zirResume(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5420 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5421 const src = inst_data.src();
5422 return sema.mod.fail(&block.base, src, "TODO: Sema.zirResume", .{});
5423}
5424
5425fn zirAwait(
5426 sema: *Sema,
5427 block: *Scope.Block,
5428 inst: Zir.Inst.Index,
5429 is_nosuspend: bool,
5430) InnerError!*Inst {
5431 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5432 const src = inst_data.src();
5433 return sema.mod.fail(&block.base, src, "TODO: Sema.zirAwait", .{});
5434}
5435
5416fn zirFuncExtended(5436fn zirFuncExtended(
5417 sema: *Sema,5437 sema: *Sema,
5418 block: *Scope.Block,5438 block: *Scope.Block,
src/Zir.zig+10
...@@ -936,6 +936,10 @@ pub const Inst = struct {...@@ -936,6 +936,10 @@ pub const Inst = struct {
936 /// Uses the `un_node` field. The AST node is the var decl.936 /// Uses the `un_node` field. The AST node is the var decl.
937 resolve_inferred_alloc,937 resolve_inferred_alloc,
938938
939 @"resume",
940 @"await",
941 await_nosuspend,
942
939 /// The ZIR instruction tag is one of the `Extended` ones.943 /// The ZIR instruction tag is one of the `Extended` ones.
940 /// Uses the `extended` union field.944 /// Uses the `extended` union field.
941 extended,945 extended,
...@@ -1185,6 +1189,9 @@ pub const Inst = struct {...@@ -1185,6 +1189,9 @@ pub const Inst = struct {
1185 .memset,1189 .memset,
1186 .builtin_async_call,1190 .builtin_async_call,
1187 .c_import,1191 .c_import,
1192 .@"resume",
1193 .@"await",
1194 .await_nosuspend,
1188 .extended,1195 .extended,
1189 => false,1196 => false,
11901197
...@@ -2416,6 +2423,9 @@ const Writer = struct {...@@ -2416,6 +2423,9 @@ const Writer = struct {
2416 .byte_swap,2423 .byte_swap,
2417 .bit_reverse,2424 .bit_reverse,
2418 .elem_type,2425 .elem_type,
2426 .@"resume",
2427 .@"await",
2428 .await_nosuspend,
2419 => try self.writeUnNode(stream, inst),2429 => try self.writeUnNode(stream, inst),
24202430
2421 .ref,2431 .ref,