Skip to content

Commit

Permalink
compiler: eliminate TypedValue
Browse files Browse the repository at this point in the history
The only logic which remained in this file was the Value printing logic.
This has been moved into a new `print_value.zig`.
  • Loading branch information
mlugg committed Mar 26, 2024
1 parent 4cdb4bb commit f78af2f
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 137 deletions.
106 changes: 52 additions & 54 deletions src/Sema.zig

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/Value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const Target = std.Target;
const Allocator = std.mem.Allocator;
const Zcu = @import("Module.zig");
const Module = Zcu;
const TypedValue = @import("TypedValue.zig");
const Sema = @import("Sema.zig");
const InternPool = @import("InternPool.zig");
const print_value = @import("print_value.zig");
const Value = @This();

ip_index: InternPool.Index,
Expand Down Expand Up @@ -39,9 +39,9 @@ pub fn fmtDebug(val: Value) std.fmt.Formatter(dump) {
return .{ .data = val };
}

pub fn fmtValue(val: Value, ty: Type, mod: *Module) std.fmt.Formatter(TypedValue.format) {
pub fn fmtValue(val: Value, mod: *Module) std.fmt.Formatter(print_value.format) {
return .{ .data = .{
.tv = .{ .ty = ty, .val = val },
.val = val,
.mod = mod,
} };
}
Expand Down
7 changes: 2 additions & 5 deletions src/arch/x86_64/CodeGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17876,11 +17876,8 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void {

break :result null;
}) orelse return self.fail("TODO implement airShuffle from {} and {} to {} with {}", .{
lhs_ty.fmt(mod), rhs_ty.fmt(mod), dst_ty.fmt(mod),
Value.fromInterned(extra.mask).fmtValue(
Type.fromInterned(mod.intern_pool.typeOf(extra.mask)),
mod,
),
lhs_ty.fmt(mod), rhs_ty.fmt(mod), dst_ty.fmt(mod),
Value.fromInterned(extra.mask).fmtValue(mod),
});
return self.finishAir(inst, result, .{ extra.a, extra.b, .none });
}
Expand Down
10 changes: 4 additions & 6 deletions src/codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ pub fn generateSymbol(
const target = mod.getTarget();
const endian = target.cpu.arch.endian();

log.debug("generateSymbol: val = {}", .{
val.fmtValue(ty, mod),
});
log.debug("generateSymbol: val = {}", .{val.fmtValue(mod)});

if (val.isUndefDeep(mod)) {
const abi_size = math.cast(usize, ty.abiSize(mod)) orelse return error.Overflow;
Expand Down Expand Up @@ -862,7 +860,7 @@ fn genDeclRef(
) CodeGenError!GenResult {
const zcu = lf.comp.module.?;
const ty = val.typeOf(zcu);
log.debug("genDeclRef: val = {}", .{val.fmtValue(ty, zcu)});
log.debug("genDeclRef: val = {}", .{val.fmtValue(zcu)});

const ptr_decl = zcu.declPtr(ptr_decl_index);
const namespace = zcu.namespacePtr(ptr_decl.src_namespace);
Expand Down Expand Up @@ -976,7 +974,7 @@ fn genUnnamedConst(
) CodeGenError!GenResult {
const zcu = lf.comp.module.?;
const gpa = lf.comp.gpa;
log.debug("genUnnamedConst: val = {}", .{val.fmtValue(val.typeOf(zcu), zcu)});
log.debug("genUnnamedConst: val = {}", .{val.fmtValue(zcu)});

const local_sym_index = lf.lowerUnnamedConst(val, owner_decl_index) catch |err| {
return GenResult.fail(gpa, src_loc, "lowering unnamed constant failed: {s}", .{@errorName(err)});
Expand Down Expand Up @@ -1016,7 +1014,7 @@ pub fn genTypedValue(
const zcu = lf.comp.module.?;
const ty = val.typeOf(zcu);

log.debug("genTypedValue: val = {}", .{val.fmtValue(ty, zcu)});
log.debug("genTypedValue: val = {}", .{val.fmtValue(zcu)});

if (val.isUndef(zcu))
return GenResult.mcv(.undef);
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/spirv.zig
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ const DeclGen = struct {

const val = arg_val;

log.debug("constant: ty = {}, val = {}", .{ ty.fmt(mod), val.fmtValue(ty, mod) });
log.debug("constant: ty = {}, val = {}", .{ ty.fmt(mod), val.fmtValue(mod) });
if (val.isUndefDeep(mod)) {
return self.spv.constUndef(result_ty_ref);
}
Expand Down
2 changes: 1 addition & 1 deletion src/print_air.zig
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ const Writer = struct {
const ty = Type.fromInterned(mod.intern_pool.indexToKey(ip_index).typeOf());
try s.print("<{}, {}>", .{
ty.fmt(mod),
Value.fromInterned(ip_index).fmtValue(ty, mod),
Value.fromInterned(ip_index).fmtValue(mod),
});
} else {
return w.writeInstIndex(s, operand.toIndex().?, dies);
Expand Down
80 changes: 17 additions & 63 deletions src/TypedValue.zig → src/print_value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ const Module = Zcu;
const Sema = @import("Sema.zig");
const InternPool = @import("InternPool.zig");
const Allocator = std.mem.Allocator;
const TypedValue = @This();
const Target = std.Target;

ty: Type,
val: Value,

const max_aggregate_items = 100;
const max_string_len = 256;

const FormatContext = struct {
tv: TypedValue,
val: Value,
mod: *Module,
};

Expand All @@ -31,7 +27,7 @@ pub fn format(
) !void {
_ = options;
comptime std.debug.assert(fmt.len == 0);
return ctx.tv.print(writer, 3, ctx.mod, null) catch |err| switch (err) {
return print(ctx.val, writer, 3, ctx.mod, null) catch |err| switch (err) {
error.OutOfMemory => @panic("OOM"), // We're not allowed to return this from a format function
error.ComptimeBreak, error.ComptimeReturn => unreachable,
error.AnalysisFail, error.NeededSourceLocation => unreachable, // TODO: re-evaluate when we actually pass `opt_sema`
Expand All @@ -40,15 +36,14 @@ pub fn format(
}

pub fn print(
tv: TypedValue,
val: Value,
writer: anytype,
level: u8,
mod: *Module,
/// If this `Sema` is provided, we will recurse through pointers where possible to provide friendly output.
opt_sema: ?*Sema,
) (@TypeOf(writer).Error || Module.CompileError)!void {
const ip = &mod.intern_pool;
const val = tv.val;
switch (ip.indexToKey(val.toIntern())) {
.int_type,
.ptr_type,
Expand Down Expand Up @@ -99,10 +94,7 @@ pub fn print(
.err_name => |err_name| try writer.print("error.{}", .{
err_name.fmt(ip),
}),
.payload => |payload| try print(.{
.ty = tv.ty.errorUnionPayload(mod),
.val = Value.fromInterned(payload),
}, writer, level, mod, opt_sema),
.payload => |payload| try print(Value.fromInterned(payload), writer, level, mod, opt_sema),
},
.enum_literal => |enum_literal| try writer.print(".{}", .{
enum_literal.fmt(ip),
Expand All @@ -117,10 +109,7 @@ pub fn print(
try writer.writeAll("@enumFromInt(...)");
}
try writer.writeAll("@enumFromInt(");
try print(.{
.ty = Type.fromInterned(ip.typeOf(enum_tag.int)),
.val = Value.fromInterned(enum_tag.int),
}, writer, level - 1, mod, opt_sema);
try print(Value.fromInterned(enum_tag.int), writer, level - 1, mod, opt_sema);
try writer.writeAll(")");
},
.empty_enum_value => try writer.writeAll("(empty enum value)"),
Expand All @@ -139,10 +128,7 @@ pub fn print(
}
try printPtr(slice.ptr, writer, false, false, 0, level, mod, opt_sema);
try writer.writeAll("[0..");
try print(.{
.ty = Type.usize,
.val = Value.fromInterned(slice.len),
}, writer, level - 1, mod, opt_sema);
try print(Value.fromInterned(slice.len), writer, level - 1, mod, opt_sema);
try writer.writeAll("]");
},
.ptr => {
Expand All @@ -159,10 +145,7 @@ pub fn print(
},
.opt => |opt| switch (opt.val) {
.none => try writer.writeAll("null"),
else => |payload| try print(.{
.ty = tv.ty.childType(mod),
.val = Value.fromInterned(payload),
}, writer, level, mod, opt_sema),
else => |payload| try print(Value.fromInterned(payload), writer, level, mod, opt_sema),
},
.aggregate => |aggregate| try printAggregate(val, aggregate, writer, level, mod, opt_sema),
.un => |un| {
Expand All @@ -171,25 +154,15 @@ pub fn print(
return;
}
if (un.tag == .none) {
const backing_ty = try tv.ty.unionBackingType(mod);
const backing_ty = try val.typeOf(mod).unionBackingType(mod);
try writer.print("@bitCast(@as({}, ", .{backing_ty.fmt(mod)});
try print(.{
.ty = backing_ty,
.val = Value.fromInterned(un.val),
}, writer, level - 1, mod, opt_sema);
try print(Value.fromInterned(un.val), writer, level - 1, mod, opt_sema);
try writer.writeAll("))");
} else {
try writer.writeAll(".{ ");
try print(.{
.ty = tv.ty.unionTagTypeHypothetical(mod),
.val = Value.fromInterned(un.tag),
}, writer, level - 1, mod, opt_sema);
try print(Value.fromInterned(un.tag), writer, level - 1, mod, opt_sema);
try writer.writeAll(" = ");
const field_ty = tv.ty.unionFieldType(Value.fromInterned(un.tag), mod).?;
try print(.{
.ty = field_ty,
.val = Value.fromInterned(un.val),
}, writer, level - 1, mod, opt_sema);
try print(Value.fromInterned(un.val), writer, level - 1, mod, opt_sema);
try writer.writeAll(" }");
}
},
Expand Down Expand Up @@ -221,10 +194,7 @@ fn printAggregate(
if (i != 0) try writer.writeAll(", ");
const field_name = ty.structFieldName(@intCast(i), zcu).unwrap().?;
try writer.print(".{i} = ", .{field_name.fmt(ip)});
try print(.{
.ty = ty.structFieldType(i, zcu),
.val = try val.fieldValue(zcu, i),
}, writer, level - 1, zcu, opt_sema);
try print(try val.fieldValue(zcu, i), writer, level - 1, zcu, opt_sema);
}
try writer.writeAll(" }");
return;
Expand All @@ -240,18 +210,14 @@ fn printAggregate(
else => unreachable,
}

const elem_ty = ty.childType(zcu);
const len = ty.arrayLen(zcu);

try writer.writeAll(".{ ");

const max_len = @min(len, max_aggregate_items);
for (0..max_len) |i| {
if (i != 0) try writer.writeAll(", ");
try print(.{
.ty = elem_ty,
.val = try val.fieldValue(zcu, i),
}, writer, level - 1, zcu, opt_sema);
try print(try val.fieldValue(zcu, i), writer, level - 1, zcu, opt_sema);
}
if (len > max_aggregate_items) {
try writer.writeAll(", ...");
Expand Down Expand Up @@ -286,17 +252,11 @@ fn printPtr(
try writer.writeByteNTimes('(', leading_parens);
if (force_type) {
try writer.print("@as({}, @ptrFromInt(", .{Type.fromInterned(ptr.ty).fmt(zcu)});
try print(.{
.ty = Type.usize,
.val = Value.fromInterned(int),
}, writer, level - 1, zcu, opt_sema);
try print(Value.fromInterned(int), writer, level - 1, zcu, opt_sema);
try writer.writeAll("))");
} else {
try writer.writeAll("@ptrFromInt(");
try print(.{
.ty = Type.usize,
.val = Value.fromInterned(int),
}, writer, level - 1, zcu, opt_sema);
try print(Value.fromInterned(int), writer, level - 1, zcu, opt_sema);
try writer.writeAll(")");
}
},
Expand All @@ -308,19 +268,13 @@ fn printPtr(
.anon_decl => |anon| {
const ty = Type.fromInterned(ip.typeOf(anon.val));
try writer.print("&@as({}, ", .{ty.fmt(zcu)});
try print(.{
.ty = ty,
.val = Value.fromInterned(anon.val),
}, writer, level - 1, zcu, opt_sema);
try print(Value.fromInterned(anon.val), writer, level - 1, zcu, opt_sema);
try writer.writeAll(")");
},
.comptime_field => |val| {
const ty = Type.fromInterned(ip.typeOf(val));
try writer.print("&@as({}, ", .{ty.fmt(zcu)});
try print(.{
.ty = ty,
.val = Value.fromInterned(val),
}, writer, level - 1, zcu, opt_sema);
try print(Value.fromInterned(val), writer, level - 1, zcu, opt_sema);
try writer.writeAll(")");
},
.eu_payload => |base| {
Expand Down
8 changes: 4 additions & 4 deletions src/type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ pub const Type = struct {

if (info.sentinel != .none) switch (info.flags.size) {
.One, .C => unreachable,
.Many => try writer.print("[*:{}]", .{Value.fromInterned(info.sentinel).fmtValue(Type.fromInterned(info.child), mod)}),
.Slice => try writer.print("[:{}]", .{Value.fromInterned(info.sentinel).fmtValue(Type.fromInterned(info.child), mod)}),
.Many => try writer.print("[*:{}]", .{Value.fromInterned(info.sentinel).fmtValue(mod)}),
.Slice => try writer.print("[:{}]", .{Value.fromInterned(info.sentinel).fmtValue(mod)}),
} else switch (info.flags.size) {
.One => try writer.writeAll("*"),
.Many => try writer.writeAll("[*]"),
Expand Down Expand Up @@ -234,7 +234,7 @@ pub const Type = struct {
} else {
try writer.print("[{d}:{}]", .{
array_type.len,
Value.fromInterned(array_type.sentinel).fmtValue(Type.fromInterned(array_type.child), mod),
Value.fromInterned(array_type.sentinel).fmtValue(mod),
});
try print(Type.fromInterned(array_type.child), writer, mod);
}
Expand Down Expand Up @@ -352,7 +352,7 @@ pub const Type = struct {
try print(Type.fromInterned(field_ty), writer, mod);

if (val != .none) {
try writer.print(" = {}", .{Value.fromInterned(val).fmtValue(Type.fromInterned(field_ty), mod)});
try writer.print(" = {}", .{Value.fromInterned(val).fmtValue(mod)});
}
}
try writer.writeAll("}");
Expand Down

0 comments on commit f78af2f

Please sign in to comment.