authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-20 00:13:48-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-20 00:13:48-04:00
log33fbd8c1d333922efa696b5b9384859a2664f8de
tree235f2f240dec9bf68d793a0edf2b258f591b22d8
parentd9fc14975232c0aa09e6498763f60515100770b9

self-hosted: convert some stuff to async/await


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

src-self-hosted/compilation.zig+7-2
......@@ -478,7 +478,7 @@ pub const Compilation = struct {
478478 }
479479
480480 /// it does ref the result because it could be an arbitrary integer size
481 pub fn getPrimitiveType(comp: *Compilation, name: []const u8) !?*Type {
481 pub async fn getPrimitiveType(comp: *Compilation, name: []const u8) !?*Type {
482482 if (name.len >= 2) {
483483 switch (name[0]) {
484484 'i', 'u' => blk: {
......@@ -492,7 +492,12 @@ pub const Compilation = struct {
492492 error.Overflow => return error.Overflow,
493493 error.InvalidCharacter => unreachable, // we just checked the characters above
494494 };
495 @panic("get int type - need to make everything async");
495 const int_type = try await (async Type.Int.get(comp, Type.Int.Key{
496 .bit_count = bit_count,
497 .is_signed = is_signed,
498 }) catch unreachable);
499 errdefer int_type.base.base.deref();
500 return &int_type.base;
496501 },
497502 else => {},
498503 }
src-self-hosted/ir.zig+23-21
......@@ -708,7 +708,7 @@ pub const Builder = struct {
708708 self.current_basic_block = basic_block;
709709 }
710710
711 pub fn genNode(irb: *Builder, node: *ast.Node, scope: *Scope, lval: LVal) Error!*Inst {
711 pub async fn genNode(irb: *Builder, node: *ast.Node, scope: *Scope, lval: LVal) Error!*Inst {
712712 switch (node.id) {
713713 ast.Node.Id.Root => unreachable,
714714 ast.Node.Id.Use => unreachable,
......@@ -724,7 +724,7 @@ pub const Builder = struct {
724724 ast.Node.Id.If => return error.Unimplemented,
725725 ast.Node.Id.ControlFlowExpression => {
726726 const control_flow_expr = @fieldParentPtr(ast.Node.ControlFlowExpression, "base", node);
727 return irb.genControlFlowExpr(control_flow_expr, scope, lval);
727 return await (async irb.genControlFlowExpr(control_flow_expr, scope, lval) catch unreachable);
728728 },
729729 ast.Node.Id.Suspend => return error.Unimplemented,
730730 ast.Node.Id.VarType => return error.Unimplemented,
......@@ -746,11 +746,11 @@ pub const Builder = struct {
746746 ast.Node.Id.Unreachable => return error.Unimplemented,
747747 ast.Node.Id.Identifier => {
748748 const identifier = @fieldParentPtr(ast.Node.Identifier, "base", node);
749 return irb.genIdentifier(identifier, scope, lval);
749 return await (async irb.genIdentifier(identifier, scope, lval) catch unreachable);
750750 },
751751 ast.Node.Id.GroupedExpression => {
752752 const grouped_expr = @fieldParentPtr(ast.Node.GroupedExpression, "base", node);
753 return irb.genNode(grouped_expr.expr, scope, lval);
753 return await (async irb.genNode(grouped_expr.expr, scope, lval) catch unreachable);
754754 },
755755 ast.Node.Id.BuiltinCall => return error.Unimplemented,
756756 ast.Node.Id.ErrorSetDecl => return error.Unimplemented,
......@@ -759,7 +759,8 @@ pub const Builder = struct {
759759 ast.Node.Id.Comptime => return error.Unimplemented,
760760 ast.Node.Id.Block => {
761761 const block = @fieldParentPtr(ast.Node.Block, "base", node);
762 return irb.lvalWrap(scope, try irb.genBlock(block, scope), lval);
762 const inst = try await (async irb.genBlock(block, scope) catch unreachable);
763 return irb.lvalWrap(scope, inst, lval);
763764 },
764765 ast.Node.Id.DocComment => return error.Unimplemented,
765766 ast.Node.Id.SwitchCase => return error.Unimplemented,
......@@ -838,7 +839,7 @@ pub const Builder = struct {
838839 return inst;
839840 }
840841
841 pub fn genBlock(irb: *Builder, block: *ast.Node.Block, parent_scope: *Scope) !*Inst {
842 pub async fn genBlock(irb: *Builder, block: *ast.Node.Block, parent_scope: *Scope) !*Inst {
842843 const block_scope = try Scope.Block.create(irb.comp, parent_scope);
843844
844845 const outer_block_scope = &block_scope.base;
......@@ -886,7 +887,7 @@ pub const Builder = struct {
886887 child_scope = &defer_child_scope.base;
887888 continue;
888889 }
889 const statement_value = try irb.genNode(statement_node, child_scope, LVal.None);
890 const statement_value = try await (async irb.genNode(statement_node, child_scope, LVal.None) catch unreachable);
890891
891892 is_continuation_unreachable = statement_value.isNoReturn();
892893 if (is_continuation_unreachable) {
......@@ -926,7 +927,7 @@ pub const Builder = struct {
926927 try block_scope.incoming_values.append(
927928 try irb.buildConstVoid(parent_scope, Span.token(block.rbrace), true),
928929 );
929 _ = try irb.genDefersForBlock(child_scope, outer_block_scope, Scope.Defer.Kind.ScopeExit);
930 _ = try await (async irb.genDefersForBlock(child_scope, outer_block_scope, Scope.Defer.Kind.ScopeExit) catch unreachable);
930931
931932 _ = try irb.buildGen(Inst.Br, parent_scope, Span.token(block.rbrace), Inst.Br.Params{
932933 .dest_block = block_scope.end_block,
......@@ -941,11 +942,11 @@ pub const Builder = struct {
941942 });
942943 }
943944
944 _ = try irb.genDefersForBlock(child_scope, outer_block_scope, Scope.Defer.Kind.ScopeExit);
945 _ = try await (async irb.genDefersForBlock(child_scope, outer_block_scope, Scope.Defer.Kind.ScopeExit) catch unreachable);
945946 return irb.buildConstVoid(child_scope, Span.token(block.rbrace), true);
946947 }
947948
948 pub fn genControlFlowExpr(
949 pub async fn genControlFlowExpr(
949950 irb: *Builder,
950951 control_flow_expr: *ast.Node.ControlFlowExpression,
951952 scope: *Scope,
......@@ -979,7 +980,7 @@ pub const Builder = struct {
979980
980981 const outer_scope = irb.begin_scope.?;
981982 const return_value = if (control_flow_expr.rhs) |rhs| blk: {
982 break :blk try irb.genNode(rhs, scope, LVal.None);
983 break :blk try await (async irb.genNode(rhs, scope, LVal.None) catch unreachable);
983984 } else blk: {
984985 break :blk try irb.buildConstVoid(scope, src_span, true);
985986 };
......@@ -990,7 +991,7 @@ pub const Builder = struct {
990991 const err_block = try irb.createBasicBlock(scope, c"ErrRetErr");
991992 const ok_block = try irb.createBasicBlock(scope, c"ErrRetOk");
992993 if (!have_err_defers) {
993 _ = try irb.genDefersForBlock(scope, outer_scope, Scope.Defer.Kind.ScopeExit);
994 _ = try await (async irb.genDefersForBlock(scope, outer_scope, Scope.Defer.Kind.ScopeExit) catch unreachable);
994995 }
995996
996997 const is_err = try irb.build(
......@@ -1013,7 +1014,7 @@ pub const Builder = struct {
10131014
10141015 try irb.setCursorAtEndAndAppendBlock(err_block);
10151016 if (have_err_defers) {
1016 _ = try irb.genDefersForBlock(scope, outer_scope, Scope.Defer.Kind.ErrorExit);
1017 _ = try await (async irb.genDefersForBlock(scope, outer_scope, Scope.Defer.Kind.ErrorExit) catch unreachable);
10171018 }
10181019 if (irb.comp.have_err_ret_tracing and !irb.isCompTime(scope)) {
10191020 _ = try irb.build(Inst.SaveErrRetAddr, scope, src_span, Inst.SaveErrRetAddr.Params{});
......@@ -1025,7 +1026,7 @@ pub const Builder = struct {
10251026
10261027 try irb.setCursorAtEndAndAppendBlock(ok_block);
10271028 if (have_err_defers) {
1028 _ = try irb.genDefersForBlock(scope, outer_scope, Scope.Defer.Kind.ScopeExit);
1029 _ = try await (async irb.genDefersForBlock(scope, outer_scope, Scope.Defer.Kind.ScopeExit) catch unreachable);
10291030 }
10301031 _ = try irb.build(Inst.Br, scope, src_span, Inst.Br.Params{
10311032 .dest_block = ret_stmt_block,
......@@ -1035,14 +1036,14 @@ pub const Builder = struct {
10351036 try irb.setCursorAtEndAndAppendBlock(ret_stmt_block);
10361037 return irb.genAsyncReturn(scope, src_span, return_value, false);
10371038 } else {
1038 _ = try irb.genDefersForBlock(scope, outer_scope, Scope.Defer.Kind.ScopeExit);
1039 _ = try await (async irb.genDefersForBlock(scope, outer_scope, Scope.Defer.Kind.ScopeExit) catch unreachable);
10391040 return irb.genAsyncReturn(scope, src_span, return_value, false);
10401041 }
10411042 },
10421043 }
10431044 }
10441045
1045 pub fn genIdentifier(irb: *Builder, identifier: *ast.Node.Identifier, scope: *Scope, lval: LVal) !*Inst {
1046 pub async fn genIdentifier(irb: *Builder, identifier: *ast.Node.Identifier, scope: *Scope, lval: LVal) !*Inst {
10461047 const src_span = Span.token(identifier.token);
10471048 const name = irb.root_scope.tree.tokenSlice(identifier.token);
10481049
......@@ -1055,7 +1056,7 @@ pub const Builder = struct {
10551056 // return &const_instruction->base;
10561057 //}
10571058
1058 if (irb.comp.getPrimitiveType(name)) |result| {
1059 if (await (async irb.comp.getPrimitiveType(name) catch unreachable)) |result| {
10591060 if (result) |primitive_type| {
10601061 defer primitive_type.base.deref(irb.comp);
10611062 switch (lval) {
......@@ -1068,6 +1069,7 @@ pub const Builder = struct {
10681069 try irb.comp.addCompileError(irb.root_scope, src_span, "integer too large");
10691070 return error.SemanticAnalysisFailed;
10701071 },
1072 error.OutOfMemory => return error.OutOfMemory,
10711073 }
10721074 //TypeTableEntry *primitive_type = get_primitive_type(irb->codegen, variable_name);
10731075 //if (primitive_type != nullptr) {
......@@ -1138,7 +1140,7 @@ pub const Builder = struct {
11381140 return result;
11391141 }
11401142
1141 fn genDefersForBlock(
1143 async fn genDefersForBlock(
11421144 irb: *Builder,
11431145 inner_scope: *Scope,
11441146 outer_scope: *Scope,
......@@ -1156,11 +1158,11 @@ pub const Builder = struct {
11561158 };
11571159 if (generate) {
11581160 const defer_expr_scope = defer_scope.defer_expr_scope;
1159 const instruction = try irb.genNode(
1161 const instruction = try await (async irb.genNode(
11601162 defer_expr_scope.expr_node,
11611163 &defer_expr_scope.base,
11621164 LVal.None,
1163 );
1165 ) catch unreachable);
11641166 if (instruction.isNoReturn()) {
11651167 is_noreturn = true;
11661168 } else {
......@@ -1909,7 +1911,7 @@ pub async fn gen(
19091911 entry_block.ref(); // Entry block gets a reference because we enter it to begin.
19101912 try irb.setCursorAtEndAndAppendBlock(entry_block);
19111913
1912 const result = try irb.genNode(body_node, scope, LVal.None);
1914 const result = try await (async irb.genNode(body_node, scope, LVal.None) catch unreachable);
19131915 if (!result.isNoReturn()) {
19141916 // no need for save_err_ret_addr because this cannot return error
19151917 _ = try irb.genAsyncReturn(scope, Span.token(body_node.lastToken()), result, true);
src-self-hosted/type.zig+3-2
......@@ -324,6 +324,7 @@ pub const Type = struct {
324324 defer held.release();
325325
326326 if (held.value.get(&key)) |entry| {
327 entry.value.base.base.ref();
327328 return entry.value;
328329 }
329330 }
......@@ -336,7 +337,7 @@ pub const Type = struct {
336337 errdefer comp.gpa().destroy(self);
337338
338339 const u_or_i = "ui"[@boolToInt(key.is_signed)];
339 const name = std.fmt.allocPrint(comp.gpa(), "{c}{}", u_or_i, key.bit_count);
340 const name = try std.fmt.allocPrint(comp.gpa(), "{c}{}", u_or_i, key.bit_count);
340341 errdefer comp.gpa().free(name);
341342
342343 self.base.init(comp, Id.Int, name);
......@@ -345,7 +346,7 @@ pub const Type = struct {
345346 const held = await (async comp.int_type_table.acquire() catch unreachable);
346347 defer held.release();
347348
348 held.value.put(&self.key, self);
349 _ = try held.value.put(&self.key, self);
349350 }
350351 return self;
351352 }