Commit 333a577d73

Andrew Kelley <andrew@ziglang.org>
2021-04-17 00:34:09
AstGen: put decls into blocks to be evaluated independently
1 parent 01b4bf3
Changed files (3)
src/AstGen.zig
@@ -2121,7 +2121,7 @@ fn globalVarDecl(
         return astgen.failNode(var_decl.ast.section_node, "TODO linksection on globals", .{});
     }
 
-    const var_inst: Zir.Inst.Ref = if (var_decl.ast.init_node != 0) vi: {
+    const var_inst: Zir.Inst.Index = if (var_decl.ast.init_node != 0) vi: {
         if (is_extern) {
             return astgen.failNode(
                 var_decl.ast.init_node,
@@ -2130,16 +2130,37 @@ fn globalVarDecl(
             );
         }
 
+        var block_scope: GenZir = .{
+            .parent = scope,
+            .decl_node_index = node,
+            .astgen = astgen,
+            .force_comptime = true,
+        };
+        defer block_scope.instructions.deinit(gpa);
+
         const init_result_loc: AstGen.ResultLoc = if (var_decl.ast.type_node != 0) .{
-            .ty = try expr(gz, scope, .{ .ty = .type_type }, var_decl.ast.type_node),
+            .ty = try expr(
+                &block_scope,
+                &block_scope.base,
+                .{ .ty = .type_type },
+                var_decl.ast.type_node,
+            ),
         } else .none;
 
-        const init_inst = try expr(gz, scope, init_result_loc, var_decl.ast.init_node);
+        const init_inst = try expr(
+            &block_scope,
+            &block_scope.base,
+            init_result_loc,
+            var_decl.ast.init_node,
+        );
 
         if (!is_mutable) {
             // const globals are just their instruction. mutable globals have
             // a special ZIR form.
-            break :vi init_inst;
+            const block_inst = try gz.addBlock(.block_inline, node);
+            _ = try block_scope.addBreak(.break_inline, block_inst, init_inst);
+            try block_scope.setBlockBody(block_inst);
+            break :vi block_inst;
         }
 
         @panic("TODO astgen global variable");
@@ -2160,7 +2181,7 @@ fn globalVarDecl(
 
     try wip_decls.name_and_value.ensureCapacity(gpa, wip_decls.name_and_value.items.len + 2);
     wip_decls.name_and_value.appendAssumeCapacity(name_str_index);
-    wip_decls.name_and_value.appendAssumeCapacity(@enumToInt(var_inst));
+    wip_decls.name_and_value.appendAssumeCapacity(var_inst);
 }
 
 fn comptimeDecl(
src/Zir.zig
@@ -1557,7 +1557,7 @@ pub const Inst = struct {
     ///      0bX0: whether corresponding decl is exported
     /// 4. decl: { // for every decls_len
     ///        name: u32, // null terminated string index
-    ///        value: Ref,
+    ///        value: Index,
     ///    }
     pub const StructDecl = struct {
         body_len: u32,
@@ -2044,6 +2044,12 @@ const Writer = struct {
     }
 
     fn writePlNodeBlock(self: *Writer, stream: anytype, inst: Inst.Index) !void {
+        const inst_data = self.code.instructions.items(.data)[inst].pl_node;
+        try self.writePlNodeBlockWithoutSrc(stream, inst);
+        try self.writeSrc(stream, inst_data.src());
+    }
+
+    fn writePlNodeBlockWithoutSrc(self: *Writer, stream: anytype, inst: Inst.Index) !void {
         const inst_data = self.code.instructions.items(.data)[inst].pl_node;
         const extra = self.code.extraData(Inst.Block, inst_data.payload_index);
         const body = self.code.extra[extra.end..][0..extra.data.body_len];
@@ -2053,7 +2059,6 @@ const Writer = struct {
         self.indent -= 2;
         try stream.writeByteNTimes(' ', self.indent);
         try stream.writeAll("}) ");
-        try self.writeSrc(stream, inst_data.src());
     }
 
     fn writePlNodeCondBr(self: *Writer, stream: anytype, inst: Inst.Index) !void {
@@ -2083,9 +2088,6 @@ const Writer = struct {
         const fields_len = extra.data.fields_len;
         const decls_len = extra.data.decls_len;
 
-        const prev_parent_decl_node = self.parent_decl_node;
-        self.parent_decl_node = self.relativeToNodeIndex(inst_data.src_node);
-
         var extra_index: usize = undefined;
 
         if (fields_len == 0) {
@@ -2157,11 +2159,11 @@ const Writer = struct {
             try stream.writeByteNTimes(' ', self.indent);
             try stream.writeAll("}) ");
         }
-        self.parent_decl_node = prev_parent_decl_node;
         try self.writeSrc(stream, inst_data.src());
     }
 
     fn writeDecls(self: *Writer, stream: anytype, decls_len: u32, extra_start: usize) !void {
+        const parent_decl_node = self.parent_decl_node;
         const bit_bags_count = std.math.divCeil(usize, decls_len, 16) catch unreachable;
         var extra_index = extra_start + bit_bags_count;
         var bit_bag_index: usize = extra_start;
@@ -2179,14 +2181,23 @@ const Writer = struct {
 
             const decl_name = self.code.nullTerminatedString(self.code.extra[extra_index]);
             extra_index += 1;
-            const decl_value = @intToEnum(Inst.Ref, self.code.extra[extra_index]);
+            const decl_index = self.code.extra[extra_index];
             extra_index += 1;
 
+            const tag = self.code.instructions.items(.tag)[decl_index];
             const pub_str = if (is_pub) "pub " else "";
             const export_str = if (is_exported) "export " else "";
             try stream.writeByteNTimes(' ', self.indent);
-            try stream.print("{s}{s}{} = ", .{ pub_str, export_str, std.zig.fmtId(decl_name) });
-            try self.writeInstRef(stream, decl_value);
+            try stream.print("{s}{s}{}: %{d} = {s}(", .{
+                pub_str, export_str, std.zig.fmtId(decl_name), decl_index, @tagName(tag),
+            });
+
+            const decl_block_inst_data = self.code.instructions.items(.data)[decl_index].pl_node;
+            const sub_decl_node_off = decl_block_inst_data.src_node;
+            self.parent_decl_node = self.relativeToNodeIndex(sub_decl_node_off);
+            try self.writePlNodeBlockWithoutSrc(stream, decl_index);
+            self.parent_decl_node = parent_decl_node;
+            try self.writeSrc(stream, decl_block_inst_data.src());
             try stream.writeAll("\n");
         }
     }
BRANCH_TODO
@@ -1,4 +1,3 @@
- * AstGen decls into blocks so we can evaluate them independently
  * look for cached zir code
  * save zir code to cache
  * store list of imported strings
@@ -740,3 +739,5 @@ fn errorSetDecl(
             try mod.analyzeExport(&decl_scope.base, export_src, name, decl);
         }
     }
+
+