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 {...@@ -478,7 +478,7 @@ pub const Compilation = struct {
478 }478 }
479479
480 /// it does ref the result because it could be an arbitrary integer size480 /// 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 {
482 if (name.len >= 2) {482 if (name.len >= 2) {
483 switch (name[0]) {483 switch (name[0]) {
484 'i', 'u' => blk: {484 'i', 'u' => blk: {
...@@ -492,7 +492,12 @@ pub const Compilation = struct {...@@ -492,7 +492,12 @@ pub const Compilation = struct {
492 error.Overflow => return error.Overflow,492 error.Overflow => return error.Overflow,
493 error.InvalidCharacter => unreachable, // we just checked the characters above493 error.InvalidCharacter => unreachable, // we just checked the characters above
494 };494 };
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;
496 },501 },
497 else => {},502 else => {},
498 }503 }
src-self-hosted/ir.zig+23-21
...@@ -708,7 +708,7 @@ pub const Builder = struct {...@@ -708,7 +708,7 @@ pub const Builder = struct {
708 self.current_basic_block = basic_block;708 self.current_basic_block = basic_block;
709 }709 }
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 {
712 switch (node.id) {712 switch (node.id) {
713 ast.Node.Id.Root => unreachable,713 ast.Node.Id.Root => unreachable,
714 ast.Node.Id.Use => unreachable,714 ast.Node.Id.Use => unreachable,
...@@ -724,7 +724,7 @@ pub const Builder = struct {...@@ -724,7 +724,7 @@ pub const Builder = struct {
724 ast.Node.Id.If => return error.Unimplemented,724 ast.Node.Id.If => return error.Unimplemented,
725 ast.Node.Id.ControlFlowExpression => {725 ast.Node.Id.ControlFlowExpression => {
726 const control_flow_expr = @fieldParentPtr(ast.Node.ControlFlowExpression, "base", node);726 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);
728 },728 },
729 ast.Node.Id.Suspend => return error.Unimplemented,729 ast.Node.Id.Suspend => return error.Unimplemented,
730 ast.Node.Id.VarType => return error.Unimplemented,730 ast.Node.Id.VarType => return error.Unimplemented,
...@@ -746,11 +746,11 @@ pub const Builder = struct {...@@ -746,11 +746,11 @@ pub const Builder = struct {
746 ast.Node.Id.Unreachable => return error.Unimplemented,746 ast.Node.Id.Unreachable => return error.Unimplemented,
747 ast.Node.Id.Identifier => {747 ast.Node.Id.Identifier => {
748 const identifier = @fieldParentPtr(ast.Node.Identifier, "base", node);748 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);
750 },750 },
751 ast.Node.Id.GroupedExpression => {751 ast.Node.Id.GroupedExpression => {
752 const grouped_expr = @fieldParentPtr(ast.Node.GroupedExpression, "base", node);752 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);
754 },754 },
755 ast.Node.Id.BuiltinCall => return error.Unimplemented,755 ast.Node.Id.BuiltinCall => return error.Unimplemented,
756 ast.Node.Id.ErrorSetDecl => return error.Unimplemented,756 ast.Node.Id.ErrorSetDecl => return error.Unimplemented,
...@@ -759,7 +759,8 @@ pub const Builder = struct {...@@ -759,7 +759,8 @@ pub const Builder = struct {
759 ast.Node.Id.Comptime => return error.Unimplemented,759 ast.Node.Id.Comptime => return error.Unimplemented,
760 ast.Node.Id.Block => {760 ast.Node.Id.Block => {
761 const block = @fieldParentPtr(ast.Node.Block, "base", node);761 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);
763 },764 },
764 ast.Node.Id.DocComment => return error.Unimplemented,765 ast.Node.Id.DocComment => return error.Unimplemented,
765 ast.Node.Id.SwitchCase => return error.Unimplemented,766 ast.Node.Id.SwitchCase => return error.Unimplemented,
...@@ -838,7 +839,7 @@ pub const Builder = struct {...@@ -838,7 +839,7 @@ pub const Builder = struct {
838 return inst;839 return inst;
839 }840 }
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 {
842 const block_scope = try Scope.Block.create(irb.comp, parent_scope);843 const block_scope = try Scope.Block.create(irb.comp, parent_scope);
843844
844 const outer_block_scope = &block_scope.base;845 const outer_block_scope = &block_scope.base;
...@@ -886,7 +887,7 @@ pub const Builder = struct {...@@ -886,7 +887,7 @@ pub const Builder = struct {
886 child_scope = &defer_child_scope.base;887 child_scope = &defer_child_scope.base;
887 continue;888 continue;
888 }889 }
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
891 is_continuation_unreachable = statement_value.isNoReturn();892 is_continuation_unreachable = statement_value.isNoReturn();
892 if (is_continuation_unreachable) {893 if (is_continuation_unreachable) {
...@@ -926,7 +927,7 @@ pub const Builder = struct {...@@ -926,7 +927,7 @@ pub const Builder = struct {
926 try block_scope.incoming_values.append(927 try block_scope.incoming_values.append(
927 try irb.buildConstVoid(parent_scope, Span.token(block.rbrace), true),928 try irb.buildConstVoid(parent_scope, Span.token(block.rbrace), true),
928 );929 );
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
931 _ = try irb.buildGen(Inst.Br, parent_scope, Span.token(block.rbrace), Inst.Br.Params{932 _ = try irb.buildGen(Inst.Br, parent_scope, Span.token(block.rbrace), Inst.Br.Params{
932 .dest_block = block_scope.end_block,933 .dest_block = block_scope.end_block,
...@@ -941,11 +942,11 @@ pub const Builder = struct {...@@ -941,11 +942,11 @@ pub const Builder = struct {
941 });942 });
942 }943 }
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);
945 return irb.buildConstVoid(child_scope, Span.token(block.rbrace), true);946 return irb.buildConstVoid(child_scope, Span.token(block.rbrace), true);
946 }947 }
947948
948 pub fn genControlFlowExpr(949 pub async fn genControlFlowExpr(
949 irb: *Builder,950 irb: *Builder,
950 control_flow_expr: *ast.Node.ControlFlowExpression,951 control_flow_expr: *ast.Node.ControlFlowExpression,
951 scope: *Scope,952 scope: *Scope,
...@@ -979,7 +980,7 @@ pub const Builder = struct {...@@ -979,7 +980,7 @@ pub const Builder = struct {
979980
980 const outer_scope = irb.begin_scope.?;981 const outer_scope = irb.begin_scope.?;
981 const return_value = if (control_flow_expr.rhs) |rhs| blk: {982 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);
983 } else blk: {984 } else blk: {
984 break :blk try irb.buildConstVoid(scope, src_span, true);985 break :blk try irb.buildConstVoid(scope, src_span, true);
985 };986 };
...@@ -990,7 +991,7 @@ pub const Builder = struct {...@@ -990,7 +991,7 @@ pub const Builder = struct {
990 const err_block = try irb.createBasicBlock(scope, c"ErrRetErr");991 const err_block = try irb.createBasicBlock(scope, c"ErrRetErr");
991 const ok_block = try irb.createBasicBlock(scope, c"ErrRetOk");992 const ok_block = try irb.createBasicBlock(scope, c"ErrRetOk");
992 if (!have_err_defers) {993 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);
994 }995 }
995996
996 const is_err = try irb.build(997 const is_err = try irb.build(
...@@ -1013,7 +1014,7 @@ pub const Builder = struct {...@@ -1013,7 +1014,7 @@ pub const Builder = struct {
10131014
1014 try irb.setCursorAtEndAndAppendBlock(err_block);1015 try irb.setCursorAtEndAndAppendBlock(err_block);
1015 if (have_err_defers) {1016 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);
1017 }1018 }
1018 if (irb.comp.have_err_ret_tracing and !irb.isCompTime(scope)) {1019 if (irb.comp.have_err_ret_tracing and !irb.isCompTime(scope)) {
1019 _ = try irb.build(Inst.SaveErrRetAddr, scope, src_span, Inst.SaveErrRetAddr.Params{});1020 _ = try irb.build(Inst.SaveErrRetAddr, scope, src_span, Inst.SaveErrRetAddr.Params{});
...@@ -1025,7 +1026,7 @@ pub const Builder = struct {...@@ -1025,7 +1026,7 @@ pub const Builder = struct {
10251026
1026 try irb.setCursorAtEndAndAppendBlock(ok_block);1027 try irb.setCursorAtEndAndAppendBlock(ok_block);
1027 if (have_err_defers) {1028 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);
1029 }1030 }
1030 _ = try irb.build(Inst.Br, scope, src_span, Inst.Br.Params{1031 _ = try irb.build(Inst.Br, scope, src_span, Inst.Br.Params{
1031 .dest_block = ret_stmt_block,1032 .dest_block = ret_stmt_block,
...@@ -1035,14 +1036,14 @@ pub const Builder = struct {...@@ -1035,14 +1036,14 @@ pub const Builder = struct {
1035 try irb.setCursorAtEndAndAppendBlock(ret_stmt_block);1036 try irb.setCursorAtEndAndAppendBlock(ret_stmt_block);
1036 return irb.genAsyncReturn(scope, src_span, return_value, false);1037 return irb.genAsyncReturn(scope, src_span, return_value, false);
1037 } else {1038 } 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);
1039 return irb.genAsyncReturn(scope, src_span, return_value, false);1040 return irb.genAsyncReturn(scope, src_span, return_value, false);
1040 }1041 }
1041 },1042 },
1042 }1043 }
1043 }1044 }
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 {
1046 const src_span = Span.token(identifier.token);1047 const src_span = Span.token(identifier.token);
1047 const name = irb.root_scope.tree.tokenSlice(identifier.token);1048 const name = irb.root_scope.tree.tokenSlice(identifier.token);
10481049
...@@ -1055,7 +1056,7 @@ pub const Builder = struct {...@@ -1055,7 +1056,7 @@ pub const Builder = struct {
1055 // return &const_instruction->base;1056 // return &const_instruction->base;
1056 //}1057 //}
10571058
1058 if (irb.comp.getPrimitiveType(name)) |result| {1059 if (await (async irb.comp.getPrimitiveType(name) catch unreachable)) |result| {
1059 if (result) |primitive_type| {1060 if (result) |primitive_type| {
1060 defer primitive_type.base.deref(irb.comp);1061 defer primitive_type.base.deref(irb.comp);
1061 switch (lval) {1062 switch (lval) {
...@@ -1068,6 +1069,7 @@ pub const Builder = struct {...@@ -1068,6 +1069,7 @@ pub const Builder = struct {
1068 try irb.comp.addCompileError(irb.root_scope, src_span, "integer too large");1069 try irb.comp.addCompileError(irb.root_scope, src_span, "integer too large");
1069 return error.SemanticAnalysisFailed;1070 return error.SemanticAnalysisFailed;
1070 },1071 },
1072 error.OutOfMemory => return error.OutOfMemory,
1071 }1073 }
1072 //TypeTableEntry *primitive_type = get_primitive_type(irb->codegen, variable_name);1074 //TypeTableEntry *primitive_type = get_primitive_type(irb->codegen, variable_name);
1073 //if (primitive_type != nullptr) {1075 //if (primitive_type != nullptr) {
...@@ -1138,7 +1140,7 @@ pub const Builder = struct {...@@ -1138,7 +1140,7 @@ pub const Builder = struct {
1138 return result;1140 return result;
1139 }1141 }
11401142
1141 fn genDefersForBlock(1143 async fn genDefersForBlock(
1142 irb: *Builder,1144 irb: *Builder,
1143 inner_scope: *Scope,1145 inner_scope: *Scope,
1144 outer_scope: *Scope,1146 outer_scope: *Scope,
...@@ -1156,11 +1158,11 @@ pub const Builder = struct {...@@ -1156,11 +1158,11 @@ pub const Builder = struct {
1156 };1158 };
1157 if (generate) {1159 if (generate) {
1158 const defer_expr_scope = defer_scope.defer_expr_scope;1160 const defer_expr_scope = defer_scope.defer_expr_scope;
1159 const instruction = try irb.genNode(1161 const instruction = try await (async irb.genNode(
1160 defer_expr_scope.expr_node,1162 defer_expr_scope.expr_node,
1161 &defer_expr_scope.base,1163 &defer_expr_scope.base,
1162 LVal.None,1164 LVal.None,
1163 );1165 ) catch unreachable);
1164 if (instruction.isNoReturn()) {1166 if (instruction.isNoReturn()) {
1165 is_noreturn = true;1167 is_noreturn = true;
1166 } else {1168 } else {
...@@ -1909,7 +1911,7 @@ pub async fn gen(...@@ -1909,7 +1911,7 @@ pub async fn gen(
1909 entry_block.ref(); // Entry block gets a reference because we enter it to begin.1911 entry_block.ref(); // Entry block gets a reference because we enter it to begin.
1910 try irb.setCursorAtEndAndAppendBlock(entry_block);1912 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);
1913 if (!result.isNoReturn()) {1915 if (!result.isNoReturn()) {
1914 // no need for save_err_ret_addr because this cannot return error1916 // no need for save_err_ret_addr because this cannot return error
1915 _ = try irb.genAsyncReturn(scope, Span.token(body_node.lastToken()), result, true);1917 _ = 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 {...@@ -324,6 +324,7 @@ pub const Type = struct {
324 defer held.release();324 defer held.release();
325325
326 if (held.value.get(&key)) |entry| {326 if (held.value.get(&key)) |entry| {
327 entry.value.base.base.ref();
327 return entry.value;328 return entry.value;
328 }329 }
329 }330 }
...@@ -336,7 +337,7 @@ pub const Type = struct {...@@ -336,7 +337,7 @@ pub const Type = struct {
336 errdefer comp.gpa().destroy(self);337 errdefer comp.gpa().destroy(self);
337338
338 const u_or_i = "ui"[@boolToInt(key.is_signed)];339 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);
340 errdefer comp.gpa().free(name);341 errdefer comp.gpa().free(name);
341342
342 self.base.init(comp, Id.Int, name);343 self.base.init(comp, Id.Int, name);
...@@ -345,7 +346,7 @@ pub const Type = struct {...@@ -345,7 +346,7 @@ pub const Type = struct {
345 const held = await (async comp.int_type_table.acquire() catch unreachable);346 const held = await (async comp.int_type_table.acquire() catch unreachable);
346 defer held.release();347 defer held.release();
347348
348 held.value.put(&self.key, self);349 _ = try held.value.put(&self.key, self);
349 }350 }
350 return self;351 return self;
351 }352 }