authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-26 14:58:07-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:55-07:00
logabded5cbb0b8c0b383b2362a2b89447528fe536c
tree21a9072ce6cbe36e24618191f1f3d23308396ff0
parentf2c716187cf486e519482ef014b34f7271cee3cf

TypedValue: implement more prints


3 files changed, 131 insertions(+), 59 deletions(-)

src/Module.zig+26-12
...@@ -6705,18 +6705,13 @@ pub fn getCoerced(mod: *Module, val: Value, new_ty: Type) Allocator.Error!Value...@@ -6705,18 +6705,13 @@ pub fn getCoerced(mod: *Module, val: Value, new_ty: Type) Allocator.Error!Value
6705}6705}
67066706
6707pub fn intType(mod: *Module, signedness: std.builtin.Signedness, bits: u16) Allocator.Error!Type {6707pub fn intType(mod: *Module, signedness: std.builtin.Signedness, bits: u16) Allocator.Error!Type {
6708 const i = try intern(mod, .{ .int_type = .{6708 return (try intern(mod, .{ .int_type = .{
6709 .signedness = signedness,6709 .signedness = signedness,
6710 .bits = bits,6710 .bits = bits,
6711 } });6711 } })).toType();
6712 return i.toType();
6713}6712}
67146713
6715pub fn arrayType(mod: *Module, info: InternPool.Key.ArrayType) Allocator.Error!Type {6714pub fn arrayType(mod: *Module, info: InternPool.Key.ArrayType) Allocator.Error!Type {
6716 if (std.debug.runtime_safety and info.sentinel != .none) {
6717 const sent_ty = mod.intern_pool.typeOf(info.sentinel);
6718 assert(sent_ty == info.child);
6719 }
6720 const i = try intern(mod, .{ .array_type = info });6715 const i = try intern(mod, .{ .array_type = info });
6721 return i.toType();6716 return i.toType();
6722}6717}
...@@ -6732,12 +6727,31 @@ pub fn optionalType(mod: *Module, child_type: InternPool.Index) Allocator.Error!...@@ -6732,12 +6727,31 @@ pub fn optionalType(mod: *Module, child_type: InternPool.Index) Allocator.Error!
6732}6727}
67336728
6734pub fn ptrType(mod: *Module, info: InternPool.Key.PtrType) Allocator.Error!Type {6729pub fn ptrType(mod: *Module, info: InternPool.Key.PtrType) Allocator.Error!Type {
6735 if (std.debug.runtime_safety and info.sentinel != .none) {6730 var canon_info = info;
6736 const sent_ty = mod.intern_pool.typeOf(info.sentinel);6731
6737 assert(sent_ty == info.elem_type);6732 // Canonicalize non-zero alignment. If it matches the ABI alignment of the pointee
6733 // type, we change it to 0 here. If this causes an assertion trip because the
6734 // pointee type needs to be resolved more, that needs to be done before calling
6735 // this ptr() function.
6736 if (info.alignment.toByteUnitsOptional()) |info_align| {
6737 const elem_align = info.elem_type.toType().abiAlignment(mod);
6738 if (info.elem_type.toType().layoutIsResolved(mod) and info_align == elem_align) {
6739 canon_info.alignment = .none;
6740 }
6738 }6741 }
6739 const i = try intern(mod, .{ .ptr_type = info });6742
6740 return i.toType();6743 // Canonicalize host_size. If it matches the bit size of the pointee type,
6744 // we change it to 0 here. If this causes an assertion trip, the pointee type
6745 // needs to be resolved before calling this ptr() function.
6746 if (info.host_size != 0) {
6747 const elem_bit_size = info.elem_type.toType().bitSize(mod);
6748 assert(info.bit_offset + elem_bit_size <= info.host_size * 8);
6749 if (info.host_size * 8 == elem_bit_size) {
6750 canon_info.host_size = 0;
6751 }
6752 }
6753
6754 return (try intern(mod, .{ .ptr_type = canon_info })).toType();
6741}6755}
67426756
6743pub fn singleMutPtrType(mod: *Module, child_type: Type) Allocator.Error!Type {6757pub fn singleMutPtrType(mod: *Module, child_type: Type) Allocator.Error!Type {
src/TypedValue.zig+103-45
...@@ -33,7 +33,7 @@ pub fn copy(self: TypedValue, arena: Allocator) error{OutOfMemory}!TypedValue {...@@ -33,7 +33,7 @@ pub fn copy(self: TypedValue, arena: Allocator) error{OutOfMemory}!TypedValue {
33}33}
3434
35pub fn eql(a: TypedValue, b: TypedValue, mod: *Module) bool {35pub fn eql(a: TypedValue, b: TypedValue, mod: *Module) bool {
36 if (a.ty.ip_index != b.ty.ip_index) return false;36 if (a.ty.toIntern() != b.ty.toIntern()) return false;
37 return a.val.eql(b.val, a.ty, mod);37 return a.val.eql(b.val, a.ty, mod);
38}38}
3939
...@@ -76,11 +76,7 @@ pub fn print(...@@ -76,11 +76,7 @@ pub fn print(
76) (@TypeOf(writer).Error || Allocator.Error)!void {76) (@TypeOf(writer).Error || Allocator.Error)!void {
77 var val = tv.val;77 var val = tv.val;
78 var ty = tv.ty;78 var ty = tv.ty;
79 if (val.isVariable(mod))
80 return writer.writeAll("(variable)");
81
82 while (true) switch (val.ip_index) {79 while (true) switch (val.ip_index) {
83 .empty_struct => return printAggregate(ty, val, writer, level, mod),
84 .none => switch (val.tag()) {80 .none => switch (val.tag()) {
85 .aggregate => return printAggregate(ty, val, writer, level, mod),81 .aggregate => return printAggregate(ty, val, writer, level, mod),
86 .@"union" => {82 .@"union" => {
...@@ -91,7 +87,7 @@ pub fn print(...@@ -91,7 +87,7 @@ pub fn print(
91 try writer.writeAll(".{ ");87 try writer.writeAll(".{ ");
9288
93 try print(.{89 try print(.{
94 .ty = mod.unionPtr(mod.intern_pool.indexToKey(ty.ip_index).union_type.index).tag_ty,90 .ty = mod.unionPtr(mod.intern_pool.indexToKey(ty.toIntern()).union_type.index).tag_ty,
95 .val = union_val.tag,91 .val = union_val.tag,
96 }, writer, level - 1, mod);92 }, writer, level - 1, mod);
97 try writer.writeAll(" = ");93 try writer.writeAll(" = ");
...@@ -176,47 +172,112 @@ pub fn print(...@@ -176,47 +172,112 @@ pub fn print(
176 .opt_payload => {172 .opt_payload => {
177 val = val.castTag(.opt_payload).?.data;173 val = val.castTag(.opt_payload).?.data;
178 ty = ty.optionalChild(mod);174 ty = ty.optionalChild(mod);
179 return print(.{ .ty = ty, .val = val }, writer, level, mod);
180 },175 },
181 },176 },
182 else => {177 else => switch (mod.intern_pool.indexToKey(val.toIntern())) {
183 const key = mod.intern_pool.indexToKey(val.ip_index);178 .int_type,
184 if (key.typeOf() == .type_type) {179 .ptr_type,
185 return Type.print(val.toType(), writer, mod);180 .array_type,
186 }181 .vector_type,
187 switch (key) {182 .opt_type,
188 .int => |int| switch (int.storage) {183 .anyframe_type,
189 inline .u64, .i64, .big_int => |x| return writer.print("{}", .{x}),184 .error_union_type,
190 .lazy_align => |lazy_ty| return writer.print("{d}", .{185 .simple_type,
191 lazy_ty.toType().abiAlignment(mod),186 .struct_type,
192 }),187 .anon_struct_type,
193 .lazy_size => |lazy_ty| return writer.print("{d}", .{188 .union_type,
194 lazy_ty.toType().abiSize(mod),189 .opaque_type,
195 }),190 .enum_type,
191 .func_type,
192 .error_set_type,
193 .inferred_error_set_type,
194 => return Type.print(val.toType(), writer, mod),
195 .undef => return writer.writeAll("undefined"),
196 .runtime_value => return writer.writeAll("(runtime value)"),
197 .simple_value => |simple_value| switch (simple_value) {
198 .empty_struct => return printAggregate(ty, val, writer, level, mod),
199 .generic_poison => return writer.writeAll("(generic poison)"),
200 else => return writer.writeAll(@tagName(simple_value)),
201 },
202 .variable => return writer.writeAll("(variable)"),
203 .extern_func => |extern_func| return writer.print("(extern function '{s}')", .{
204 mod.declPtr(extern_func.decl).name,
205 }),
206 .func => |func| return writer.print("(function '{s}')", .{
207 mod.declPtr(mod.funcPtr(func.index).owner_decl).name,
208 }),
209 .int => |int| switch (int.storage) {
210 inline .u64, .i64, .big_int => |x| return writer.print("{}", .{x}),
211 .lazy_align => |lazy_ty| return writer.print("{d}", .{
212 lazy_ty.toType().abiAlignment(mod),
213 }),
214 .lazy_size => |lazy_ty| return writer.print("{d}", .{
215 lazy_ty.toType().abiSize(mod),
216 }),
217 },
218 .err => |err| return writer.print("error.{s}", .{
219 mod.intern_pool.stringToSlice(err.name),
220 }),
221 .error_union => |error_union| switch (error_union.val) {
222 .err_name => |err_name| return writer.print("error.{s}", .{
223 mod.intern_pool.stringToSlice(err_name),
224 }),
225 .payload => |payload| {
226 val = payload.toValue();
227 ty = ty.errorUnionPayload(mod);
196 },228 },
197 .enum_tag => |enum_tag| {229 },
198 if (level == 0) {230 .enum_literal => |enum_literal| return writer.print(".{s}", .{
199 return writer.writeAll("(enum)");231 mod.intern_pool.stringToSlice(enum_literal),
200 }232 }),
233 .enum_tag => |enum_tag| {
234 if (level == 0) {
235 return writer.writeAll("(enum)");
236 }
201237
202 try writer.writeAll("@intToEnum(");238 try writer.writeAll("@intToEnum(");
239 try print(.{
240 .ty = Type.type,
241 .val = enum_tag.ty.toValue(),
242 }, writer, level - 1, mod);
243 try writer.writeAll(", ");
244 try print(.{
245 .ty = mod.intern_pool.typeOf(enum_tag.int).toType(),
246 .val = enum_tag.int.toValue(),
247 }, writer, level - 1, mod);
248 try writer.writeAll(")");
249 return;
250 },
251 .float => |float| switch (float.storage) {
252 inline else => |x| return writer.print("{}", .{x}),
253 },
254 .ptr => return writer.writeAll("(ptr)"),
255 .opt => |opt| switch (opt.val) {
256 .none => return writer.writeAll("null"),
257 else => {
258 val = opt.val.toValue();
259 ty = ty.optionalChild(mod);
260 },
261 },
262 .aggregate => |aggregate| switch (aggregate.storage) {
263 .bytes => |bytes| return writer.print("\"{}\"", .{std.zig.fmtEscapes(bytes)}),
264 .elems, .repeated_elem => return printAggregate(ty, val, writer, level, mod),
265 },
266 .un => |un| {
267 try writer.writeAll(".{ ");
268 if (level > 0) {
203 try print(.{269 try print(.{
204 .ty = Type.type,270 .ty = ty.unionTagTypeHypothetical(mod),
205 .val = enum_tag.ty.toValue(),271 .val = un.tag.toValue(),
206 }, writer, level - 1, mod);272 }, writer, level - 1, mod);
207 try writer.writeAll(", ");273 try writer.writeAll(" = ");
208 try print(.{274 try print(.{
209 .ty = mod.intern_pool.typeOf(enum_tag.int).toType(),275 .ty = ty.unionFieldType(un.tag.toValue(), mod),
210 .val = enum_tag.int.toValue(),276 .val = un.val.toValue(),
211 }, writer, level - 1, mod);277 }, writer, level - 1, mod);
212 try writer.writeAll(")");278 } else try writer.writeAll("...");
213 return;279 return writer.writeAll(" }");
214 },280 },
215 .float => |float| switch (float.storage) {
216 inline else => |x| return writer.print("{}", .{x}),
217 },
218 else => return writer.print("{}", .{val.ip_index}),
219 }
220 },281 },
221 };282 };
222}283}
...@@ -238,12 +299,9 @@ fn printAggregate(...@@ -238,12 +299,9 @@ fn printAggregate(
238 var i: u32 = 0;299 var i: u32 = 0;
239 while (i < max_len) : (i += 1) {300 while (i < max_len) : (i += 1) {
240 if (i != 0) try writer.writeAll(", ");301 if (i != 0) try writer.writeAll(", ");
241 switch (ty.ip_index) {302 switch (mod.intern_pool.indexToKey(ty.toIntern())) {
242 .none => {}, // TODO make this unreachable after finishing InternPool migration303 .struct_type, .anon_struct_type => try writer.print(".{s} = ", .{ty.structFieldName(i, mod)}),
243 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {304 else => {},
244 .struct_type, .anon_struct_type => try writer.print(".{s} = ", .{ty.structFieldName(i, mod)}),
245 else => {},
246 },
247 }305 }
248 try print(.{306 try print(.{
249 .ty = ty.structFieldType(i, mod),307 .ty = ty.structFieldType(i, mod),
src/codegen/llvm.zig+2-2
...@@ -3255,9 +3255,9 @@ pub const DeclGen = struct {...@@ -3255,9 +3255,9 @@ pub const DeclGen = struct {
3255 try dg.module.markDeclAlive(fn_decl);3255 try dg.module.markDeclAlive(fn_decl);
3256 return dg.resolveLlvmFunction(fn_decl_index);3256 return dg.resolveLlvmFunction(fn_decl_index);
3257 },3257 },
3258 .int => |int| {3258 .int => {
3259 var bigint_space: Value.BigIntSpace = undefined;3259 var bigint_space: Value.BigIntSpace = undefined;
3260 const bigint = int.storage.toBigInt(&bigint_space);3260 const bigint = tv.val.toBigInt(&bigint_space, mod);
3261 return lowerBigInt(dg, tv.ty, bigint);3261 return lowerBigInt(dg, tv.ty, bigint);
3262 },3262 },
3263 .err => |err| {3263 .err => |err| {