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
67056705}
67066706
67076707pub 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 = .{
67096709 .signedness = signedness,
67106710 .bits = bits,
6711 } });
6712 return i.toType();
6711 } })).toType();
67136712}
67146713
67156714pub 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 }
67206715 const i = try intern(mod, .{ .array_type = info });
67216716 return i.toType();
67226717}
......@@ -6732,12 +6727,31 @@ pub fn optionalType(mod: *Module, child_type: InternPool.Index) Allocator.Error!
67326727}
67336728
67346729pub fn ptrType(mod: *Module, info: InternPool.Key.PtrType) Allocator.Error!Type {
6735 if (std.debug.runtime_safety and info.sentinel != .none) {
6736 const sent_ty = mod.intern_pool.typeOf(info.sentinel);
6737 assert(sent_ty == info.elem_type);
6730 var canon_info = info;
6731
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 }
67386741 }
6739 const i = try intern(mod, .{ .ptr_type = info });
6740 return i.toType();
6742
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();
67416755}
67426756
67436757pub 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 {
3333}
3434
3535pub 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;
3737 return a.val.eql(b.val, a.ty, mod);
3838}
3939
......@@ -76,11 +76,7 @@ pub fn print(
7676) (@TypeOf(writer).Error || Allocator.Error)!void {
7777 var val = tv.val;
7878 var ty = tv.ty;
79 if (val.isVariable(mod))
80 return writer.writeAll("(variable)");
81
8279 while (true) switch (val.ip_index) {
83 .empty_struct => return printAggregate(ty, val, writer, level, mod),
8480 .none => switch (val.tag()) {
8581 .aggregate => return printAggregate(ty, val, writer, level, mod),
8682 .@"union" => {
......@@ -91,7 +87,7 @@ pub fn print(
9187 try writer.writeAll(".{ ");
9288
9389 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,
9591 .val = union_val.tag,
9692 }, writer, level - 1, mod);
9793 try writer.writeAll(" = ");
......@@ -176,47 +172,112 @@ pub fn print(
176172 .opt_payload => {
177173 val = val.castTag(.opt_payload).?.data;
178174 ty = ty.optionalChild(mod);
179 return print(.{ .ty = ty, .val = val }, writer, level, mod);
180175 },
181176 },
182 else => {
183 const key = mod.intern_pool.indexToKey(val.ip_index);
184 if (key.typeOf() == .type_type) {
185 return Type.print(val.toType(), writer, mod);
186 }
187 switch (key) {
188 .int => |int| switch (int.storage) {
189 inline .u64, .i64, .big_int => |x| return writer.print("{}", .{x}),
190 .lazy_align => |lazy_ty| return writer.print("{d}", .{
191 lazy_ty.toType().abiAlignment(mod),
192 }),
193 .lazy_size => |lazy_ty| return writer.print("{d}", .{
194 lazy_ty.toType().abiSize(mod),
195 }),
177 else => switch (mod.intern_pool.indexToKey(val.toIntern())) {
178 .int_type,
179 .ptr_type,
180 .array_type,
181 .vector_type,
182 .opt_type,
183 .anyframe_type,
184 .error_union_type,
185 .simple_type,
186 .struct_type,
187 .anon_struct_type,
188 .union_type,
189 .opaque_type,
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);
196228 },
197 .enum_tag => |enum_tag| {
198 if (level == 0) {
199 return writer.writeAll("(enum)");
200 }
229 },
230 .enum_literal => |enum_literal| return writer.print(".{s}", .{
231 mod.intern_pool.stringToSlice(enum_literal),
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) {
203269 try print(.{
204 .ty = Type.type,
205 .val = enum_tag.ty.toValue(),
270 .ty = ty.unionTagTypeHypothetical(mod),
271 .val = un.tag.toValue(),
206272 }, writer, level - 1, mod);
207 try writer.writeAll(", ");
273 try writer.writeAll(" = ");
208274 try print(.{
209 .ty = mod.intern_pool.typeOf(enum_tag.int).toType(),
210 .val = enum_tag.int.toValue(),
275 .ty = ty.unionFieldType(un.tag.toValue(), mod),
276 .val = un.val.toValue(),
211277 }, writer, level - 1, mod);
212 try writer.writeAll(")");
213 return;
214 },
215 .float => |float| switch (float.storage) {
216 inline else => |x| return writer.print("{}", .{x}),
217 },
218 else => return writer.print("{}", .{val.ip_index}),
219 }
278 } else try writer.writeAll("...");
279 return writer.writeAll(" }");
280 },
220281 },
221282 };
222283}
......@@ -238,12 +299,9 @@ fn printAggregate(
238299 var i: u32 = 0;
239300 while (i < max_len) : (i += 1) {
240301 if (i != 0) try writer.writeAll(", ");
241 switch (ty.ip_index) {
242 .none => {}, // TODO make this unreachable after finishing InternPool migration
243 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
244 .struct_type, .anon_struct_type => try writer.print(".{s} = ", .{ty.structFieldName(i, mod)}),
245 else => {},
246 },
302 switch (mod.intern_pool.indexToKey(ty.toIntern())) {
303 .struct_type, .anon_struct_type => try writer.print(".{s} = ", .{ty.structFieldName(i, mod)}),
304 else => {},
247305 }
248306 try print(.{
249307 .ty = ty.structFieldType(i, mod),
src/codegen/llvm.zig+2-2
......@@ -3255,9 +3255,9 @@ pub const DeclGen = struct {
32553255 try dg.module.markDeclAlive(fn_decl);
32563256 return dg.resolveLlvmFunction(fn_decl_index);
32573257 },
3258 .int => |int| {
3258 .int => {
32593259 var bigint_space: Value.BigIntSpace = undefined;
3260 const bigint = int.storage.toBigInt(&bigint_space);
3260 const bigint = tv.val.toBigInt(&bigint_space, mod);
32613261 return lowerBigInt(dg, tv.ty, bigint);
32623262 },
32633263 .err => |err| {