Skip to content

Commit

Permalink
Fixes print zir for imports, better encoding of instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
MasonRemaley committed Jul 1, 2024
1 parent 3d5ec4c commit 07ec8e7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
15 changes: 11 additions & 4 deletions lib/std/zig/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9130,10 +9130,17 @@ fn builtinCall(
} else if (str.len == 0) {
return astgen.failTok(str_lit_token, "import path cannot be empty", .{});
}
const result_ty = try ri.rl.resultType(gz, node) orelse .none;
const result = try gz.addPlNode(.import, node, Zir.Inst.Import{
.result_ty = result_ty,
.name = str.index,
const res_ty = try ri.rl.resultType(gz, node) orelse .none;
const payload_index = try addExtra(gz.astgen, Zir.Inst.Import{
.res_ty = res_ty,
.path = str.index,
});
const result = try gz.add(.{
.tag = .import,
.data = .{ .pl_tok = .{
.src_tok = gz.tokenIndexToRelative(str_lit_token),
.payload_index = payload_index,
} },
});
const gop = try astgen.imports.getOrPut(astgen.gpa, str.index);
if (!gop.found_existing) {
Expand Down
8 changes: 5 additions & 3 deletions lib/std/zig/Zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,7 @@ pub const Inst = struct {
.func = .pl_node,
.func_inferred = .pl_node,
.func_fancy = .pl_node,
.import = .pl_node,
.import = .pl_tok,
.int = .int,
.int_big = .str,
.float = .float,
Expand Down Expand Up @@ -3476,8 +3476,10 @@ pub const Inst = struct {
};

pub const Import = struct {
result_ty: Ref,
name: NullTerminatedString,
/// The result type of the import, or `.none` if none was available.
res_ty: Ref,
/// The import path.
path: NullTerminatedString,
};
};

Expand Down
8 changes: 5 additions & 3 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13896,11 +13896,13 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
defer tracy.end();

const mod = sema.mod;
const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_tok;
const extra = sema.code.extraData(Zir.Inst.Import, inst_data.payload_index).data;
const operand = sema.code.nullTerminatedString(extra.name);
const operand_src = block.tokenOffset(inst_data.src_tok);
const operand = sema.code.nullTerminatedString(extra.path);

_ = extra.res_ty;

const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);
const result = mod.importFile(block.getFileScope(mod), operand) catch |err| switch (err) {
error.ImportOutsideModulePath => {
return sema.fail(block, operand_src, "import of file outside module path: '{s}'", .{operand});
Expand Down
12 changes: 11 additions & 1 deletion src/print_zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ const Writer = struct {
.elem_val,
.array_type,
.coerce_ptr_elem_ty,
.import,
=> try self.writePlNodeBin(stream, inst),

.for_len => try self.writePlNodeMultiOp(stream, inst),
Expand Down Expand Up @@ -512,6 +511,8 @@ const Writer = struct {
.declaration => try self.writeDeclaration(stream, inst),

.extended => try self.writeExtended(stream, inst),

.import => try self.writeImport(stream, inst),
}
}

Expand Down Expand Up @@ -2951,4 +2952,13 @@ const Writer = struct {
try stream.writeByte('\n');
}
}

fn writeImport(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_tok;
const extra = self.code.extraData(Zir.Inst.Import, inst_data.payload_index).data;
try self.writeInstRef(stream, extra.res_ty);
const import_path = self.code.nullTerminatedString(extra.path);
try stream.print(", \"{}\") ", .{std.zig.fmtEscapes(import_path)});
try self.writeSrcTok(stream, inst_data.src_tok);
}
};

0 comments on commit 07ec8e7

Please sign in to comment.