aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-04-16 15:34:09 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-04-16 15:34:09 -0700
commit333a577d73cdbac420d25167a3955956af91b2eb (patch)
tree761c225918892672d4e551aa26fcb7eee2fee40b /src
parent01b4bf34ea4f37d8b778bb2413ac1fc445f8bead (diff)
downloadzig-333a577d73cdbac420d25167a3955956af91b2eb.tar.gz
zig-333a577d73cdbac420d25167a3955956af91b2eb.zip
AstGen: put decls into blocks to be evaluated independently
Diffstat (limited to 'src')
-rw-r--r--src/AstGen.zig31
-rw-r--r--src/Zir.zig29
2 files changed, 46 insertions, 14 deletions
diff --git a/src/AstGen.zig b/src/AstGen.zig
index 1091517494..b0f554df5c 100644
--- a/src/AstGen.zig
+++ b/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(
diff --git a/src/Zir.zig b/src/Zir.zig
index f3be0e2c64..b036c00269 100644
--- a/src/Zir.zig
+++ b/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,
@@ -2045,6 +2045,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];
try stream.writeAll("{\n");
@@ -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");
}
}