aboutsummaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2025-07-02 07:21:51 -0700
committerAndrew Kelley <andrew@ziglang.org>2025-07-07 22:43:52 -0700
commit756a2dbf1a5f8af7fe153960e332eaad2ab3bcd8 (patch)
tree15f74e430673e16884aec01aa8374eb35e214ac6 /src/codegen
parent941bc3719382a4f6245ad42175d911964f1bc9a4 (diff)
downloadzig-756a2dbf1a5f8af7fe153960e332eaad2ab3bcd8.tar.gz
zig-756a2dbf1a5f8af7fe153960e332eaad2ab3bcd8.zip
compiler: upgrade various std.io API usage
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/c.zig224
-rw-r--r--src/codegen/c/Type.zig38
-rw-r--r--src/codegen/llvm.zig46
-rw-r--r--src/codegen/spirv.zig10
4 files changed, 171 insertions, 147 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index 224f86a78a..85cbd8c96f 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -604,8 +604,12 @@ pub const Function = struct {
return f.object.dg.renderIntCast(w, dest_ty, .{ .c_value = .{ .f = f, .value = src, .v = v } }, src_ty, location);
}
- fn fmtIntLiteral(f: *Function, val: Value) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) {
- return f.object.dg.fmtIntLiteral(val, .Other);
+ fn fmtIntLiteralDec(f: *Function, val: Value) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) {
+ return f.object.dg.fmtIntLiteralDec(val, .Other);
+ }
+
+ fn fmtIntLiteralHex(f: *Function, val: Value) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) {
+ return f.object.dg.fmtIntLiteralHex(val, .Other);
}
fn getLazyFnName(f: *Function, key: LazyFnKey) ![]const u8 {
@@ -629,7 +633,7 @@ pub const Function = struct {
}),
.never_tail,
.never_inline,
- => |owner_nav| try ctype_pool.fmt(gpa, "zig_{s}_{}__{d}", .{
+ => |owner_nav| try ctype_pool.fmt(gpa, "zig_{s}_{f}__{d}", .{
@tagName(key),
fmtIdentUnsolo(ip.getNav(owner_nav).name.toSlice(ip)),
@intFromEnum(owner_nav),
@@ -880,7 +884,7 @@ pub const DeclGen = struct {
const addr_val = try pt.intValue(.usize, int.addr);
try writer.writeByte('(');
try dg.renderCType(writer, ptr_ctype);
- try writer.print("){x}", .{try dg.fmtIntLiteral(addr_val, .Other)});
+ try writer.print("){f}", .{try dg.fmtIntLiteralHex(addr_val, .Other)});
},
.nav_ptr => |nav| try dg.renderNav(writer, nav, location),
@@ -920,7 +924,7 @@ pub const DeclGen = struct {
const offset_val = try pt.intValue(.usize, byte_offset);
try writer.writeAll("((char *)");
try dg.renderPointer(writer, field.parent.*, location);
- try writer.print(" + {})", .{try dg.fmtIntLiteral(offset_val, .Other)});
+ try writer.print(" + {f})", .{try dg.fmtIntLiteralDec(offset_val, .Other)});
},
}
},
@@ -942,7 +946,7 @@ pub const DeclGen = struct {
// The pointer already has an appropriate type - just do the arithmetic.
try writer.writeByte('(');
try dg.renderPointer(writer, elem.parent.*, location);
- try writer.print(" + {})", .{try dg.fmtIntLiteral(index_val, .Other)});
+ try writer.print(" + {f})", .{try dg.fmtIntLiteralDec(index_val, .Other)});
} else {
// We probably have an array pointer `T (*)[n]`. Cast to an element pointer,
// and *then* apply the index.
@@ -950,7 +954,7 @@ pub const DeclGen = struct {
try dg.renderCType(writer, result_ctype);
try writer.writeByte(')');
try dg.renderPointer(writer, elem.parent.*, location);
- try writer.print(" + {})", .{try dg.fmtIntLiteral(index_val, .Other)});
+ try writer.print(" + {f})", .{try dg.fmtIntLiteralDec(index_val, .Other)});
}
},
@@ -965,7 +969,7 @@ pub const DeclGen = struct {
const offset_val = try pt.intValue(.usize, oac.byte_offset);
try writer.writeAll("((char *)");
try dg.renderPointer(writer, oac.parent.*, location);
- try writer.print(" + {})", .{try dg.fmtIntLiteral(offset_val, .Other)});
+ try writer.print(" + {f})", .{try dg.fmtIntLiteralDec(offset_val, .Other)});
}
},
}
@@ -1037,11 +1041,11 @@ pub const DeclGen = struct {
.empty_enum_value,
=> unreachable, // non-runtime values
.int => |int| switch (int.storage) {
- .u64, .i64, .big_int => try writer.print("{}", .{try dg.fmtIntLiteral(val, location)}),
+ .u64, .i64, .big_int => try writer.print("{f}", .{try dg.fmtIntLiteralDec(val, location)}),
.lazy_align, .lazy_size => {
try writer.writeAll("((");
try dg.renderCType(writer, ctype);
- try writer.print("){x})", .{try dg.fmtIntLiteral(
+ try writer.print("){f})", .{try dg.fmtIntLiteralHex(
try pt.intValue(.usize, val.toUnsignedInt(zcu)),
.Other,
)});
@@ -1170,7 +1174,7 @@ pub const DeclGen = struct {
try writer.writeAll(", ");
empty = false;
}
- try writer.print("{x}", .{try dg.fmtIntLiteral(
+ try writer.print("{f}", .{try dg.fmtIntLiteralHex(
try pt.intValue_big(repr_ty, repr_val_big.toConst()),
location,
)});
@@ -1642,15 +1646,15 @@ pub const DeclGen = struct {
.enum_type,
.error_set_type,
.inferred_error_set_type,
- => return writer.print("{x}", .{
- try dg.fmtIntLiteral(try pt.undefValue(ty), location),
+ => return writer.print("{f}", .{
+ try dg.fmtIntLiteralHex(try pt.undefValue(ty), location),
}),
.ptr_type => |ptr_type| switch (ptr_type.flags.size) {
.one, .many, .c => {
try writer.writeAll("((");
try dg.renderCType(writer, ctype);
- return writer.print("){x})", .{
- try dg.fmtIntLiteral(.undef_usize, .Other),
+ return writer.print("){f})", .{
+ try dg.fmtIntLiteralHex(.undef_usize, .Other),
});
},
.slice => {
@@ -1663,8 +1667,8 @@ pub const DeclGen = struct {
try writer.writeAll("{(");
const ptr_ty = ty.slicePtrFieldType(zcu);
try dg.renderType(writer, ptr_ty);
- return writer.print("){x}, {0x}}}", .{
- try dg.fmtIntLiteral(.undef_usize, .Other),
+ return writer.print("){f}, {0x}}}", .{
+ try dg.fmtIntLiteralHex(.undef_usize, .Other),
});
},
},
@@ -1727,8 +1731,8 @@ pub const DeclGen = struct {
}
return writer.writeByte('}');
},
- .@"packed" => return writer.print("{x}", .{
- try dg.fmtIntLiteral(try pt.undefValue(ty), .Other),
+ .@"packed" => return writer.print("{f}", .{
+ try dg.fmtIntLiteralHex(try pt.undefValue(ty), .Other),
}),
}
},
@@ -1797,8 +1801,8 @@ pub const DeclGen = struct {
}
if (has_tag) try writer.writeByte('}');
},
- .@"packed" => return writer.print("{x}", .{
- try dg.fmtIntLiteral(try pt.undefValue(ty), .Other),
+ .@"packed" => return writer.print("{f}", .{
+ try dg.fmtIntLiteralHex(try pt.undefValue(ty), .Other),
}),
}
},
@@ -1940,8 +1944,8 @@ pub const DeclGen = struct {
try w.print("{}", .{trailing});
switch (name) {
.nav => |nav| try dg.renderNavName(w, nav),
- .fmt_ctype_pool_string => |fmt| try w.print("{ }", .{fmt}),
- .@"export" => |@"export"| try w.print("{ }", .{fmtIdentSolo(@"export".extern_name.toSlice(ip))}),
+ .fmt_ctype_pool_string => |fmt| try w.print("{f}", .{fmt}),
+ .@"export" => |@"export"| try w.print("{f}", .{fmtIdentSolo(@"export".extern_name.toSlice(ip))}),
}
try renderTypeSuffix(
@@ -2126,7 +2130,7 @@ pub const DeclGen = struct {
} else if (dest_bits > 64 and src_bits <= 64) {
try w.writeAll("zig_make_");
try dg.renderTypeForBuiltinFnName(w, dest_ty);
- try w.writeAll("(0, "); // TODO: Should the 0 go through fmtIntLiteral?
+ try w.writeAll("(0, ");
if (src_is_ptr) {
try w.writeByte('(');
try dg.renderType(w, src_eff_ty);
@@ -2398,7 +2402,7 @@ pub const DeclGen = struct {
};
if (is_big) try writer.print(", {}", .{int_info.signedness == .signed});
- try writer.print(", {}", .{try dg.fmtIntLiteral(
+ try writer.print(", {f}", .{try dg.fmtIntLiteralDec(
try pt.intValue(if (is_big) .u16 else .u8, int_info.bits),
.FunctionArgument,
)});
@@ -2408,18 +2412,38 @@ pub const DeclGen = struct {
dg: *DeclGen,
val: Value,
loc: ValueRenderLocation,
- ) !std.fmt.Formatter(formatIntLiteral) {
+ base: u8,
+ case: std.fmt.Case,
+ ) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) {
const zcu = dg.pt.zcu;
const kind = loc.toCTypeKind();
const ty = val.typeOf(zcu);
- return std.fmt.Formatter(formatIntLiteral){ .data = .{
+ return .{ .data = .{
.dg = dg,
.int_info = ty.intInfo(zcu),
.kind = kind,
.ctype = try dg.ctypeFromType(ty, kind),
.val = val,
+ .base = base,
+ .case = case,
} };
}
+
+ fn fmtIntLiteralDec(
+ dg: *DeclGen,
+ val: Value,
+ loc: ValueRenderLocation,
+ ) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) {
+ return fmtIntLiteral(dg, val, loc, 10, .lower);
+ }
+
+ fn fmtIntLiteralHex(
+ dg: *DeclGen,
+ val: Value,
+ loc: ValueRenderLocation,
+ ) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) {
+ return fmtIntLiteral(dg, val, loc, 16, .lower);
+ }
};
const CTypeFix = enum { prefix, suffix };
@@ -2848,9 +2872,9 @@ pub fn genErrDecls(o: *Object) !void {
for (names, 1..) |name_nts, val| {
const name = name_nts.toSlice(ip);
if (val > 1) try writer.writeAll(", ");
- try writer.print("{{" ++ name_prefix ++ "{}, {}}}", .{
+ try writer.print("{{" ++ name_prefix ++ "{f}, {f}}}", .{
fmtIdentUnsolo(name),
- try o.dg.fmtIntLiteral(try pt.intValue(.usize, name.len), .StaticInitializer),
+ try o.dg.fmtIntLiteralDec(try pt.intValue(.usize, name.len), .StaticInitializer),
});
}
try writer.writeAll("};\n");
@@ -2890,17 +2914,17 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn
.storage = .{ .bytes = tag_name.toString() },
} });
- try w.print(" case {}: {{\n static ", .{
- try o.dg.fmtIntLiteral(try tag_val.intFromEnum(enum_ty, pt), .Other),
+ try w.print(" case {f}: {{\n static ", .{
+ try o.dg.fmtIntLiteralDec(try tag_val.intFromEnum(enum_ty, pt), .Other),
});
try o.dg.renderTypeAndName(w, name_ty, .{ .identifier = "name" }, Const, .none, .complete);
try w.writeAll(" = ");
try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer);
try w.writeAll(";\n return (");
try o.dg.renderType(w, name_slice_ty);
- try w.print("){{{}, {}}};\n", .{
+ try w.print("){{{f}, {f}}};\n", .{
fmtIdentUnsolo("name"),
- try o.dg.fmtIntLiteral(try pt.intValue(.usize, tag_name_len), .Other),
+ try o.dg.fmtIntLiteralDec(try pt.intValue(.usize, tag_name_len), .Other),
});
try w.writeAll(" }\n");
@@ -2915,7 +2939,7 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn
const fn_val = zcu.navValue(fn_nav_index);
const fn_ctype = try o.dg.ctypeFromType(fn_val.typeOf(zcu), .complete);
const fn_info = fn_ctype.info(ctype_pool).function;
- const fn_name = fmtCTypePoolString(val.fn_name, lazy_ctype_pool);
+ const fn_name = fmtCTypePoolString(val.fn_name, lazy_ctype_pool, true);
const fwd = o.dg.fwdDeclWriter();
try fwd.print("static zig_{s} ", .{@tagName(key)});
@@ -3954,7 +3978,7 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue {
try writer.writeByte('(');
try f.writeCValueDeref(writer, operand);
try v.elem(f, writer);
- try writer.print(", {})", .{try f.fmtIntLiteral(bit_offset_val)});
+ try writer.print(", {f})", .{try f.fmtIntLiteralDec(bit_offset_val)});
if (cant_cast) try writer.writeByte(')');
try f.object.dg.renderBuiltinInfo(writer, field_ty, .bits);
try writer.writeByte(')');
@@ -4102,8 +4126,8 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue {
try writer.writeByte('(');
try f.writeCValue(writer, operand, .FunctionArgument);
try v.elem(f, writer);
- try writer.print(", {x})", .{
- try f.fmtIntLiteral(try inst_scalar_ty.maxIntScalar(pt, scalar_ty)),
+ try writer.print(", {f})", .{
+ try f.fmtIntLiteralHex(try inst_scalar_ty.maxIntScalar(pt, scalar_ty)),
});
},
.signed => {
@@ -4127,9 +4151,9 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue {
try f.writeCValue(writer, operand, .FunctionArgument);
try v.elem(f, writer);
if (c_bits == 128) try writer.writeByte(')');
- try writer.print(", {})", .{try f.fmtIntLiteral(shift_val)});
+ try writer.print(", {f})", .{try f.fmtIntLiteralDec(shift_val)});
if (c_bits == 128) try writer.writeByte(')');
- try writer.print(", {})", .{try f.fmtIntLiteral(shift_val)});
+ try writer.print(", {f})", .{try f.fmtIntLiteralDec(shift_val)});
},
}
if (need_lo) try writer.writeByte(')');
@@ -4244,7 +4268,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
try writer.writeByte('(');
try f.writeCValueDeref(writer, ptr_val);
try v.elem(f, writer);
- try writer.print(", {x}), zig_shl_", .{try f.fmtIntLiteral(mask_val)});
+ try writer.print(", {f}), zig_shl_", .{try f.fmtIntLiteralHex(mask_val)});
try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty);
try writer.writeByte('(');
const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64;
@@ -4267,7 +4291,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
try f.writeCValue(writer, src_val, .Other);
try v.elem(f, writer);
if (cant_cast) try writer.writeByte(')');
- try writer.print(", {}))", .{try f.fmtIntLiteral(bit_offset_val)});
+ try writer.print(", {f}))", .{try f.fmtIntLiteralDec(bit_offset_val)});
try a.end(f, writer);
try v.end(f, inst, writer);
} else {
@@ -5348,7 +5372,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
write_val: {
if (condition_ty.isPtrAtRuntime(zcu)) {
if (item_value.?.getUnsignedInt(zcu)) |item_int| {
- try writer.print("{}", .{try f.fmtIntLiteral(try pt.intValue(lowered_condition_ty, item_int))});
+ try writer.print("{f}", .{try f.fmtIntLiteralDec(try pt.intValue(lowered_condition_ty, item_int))});
break :write_val;
}
}
@@ -6004,8 +6028,8 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue {
try f.renderType(writer, u8_ptr_ty);
try writer.writeByte(')');
try f.writeCValue(writer, field_ptr_val, .Other);
- try writer.print(" - {})", .{
- try f.fmtIntLiteral(try pt.intValue(.usize, byte_offset)),
+ try writer.print(" - {f})", .{
+ try f.fmtIntLiteralDec(try pt.intValue(.usize, byte_offset)),
});
},
}
@@ -6049,8 +6073,8 @@ fn fieldPtr(
try f.renderType(writer, u8_ptr_ty);
try writer.writeByte(')');
try f.writeCValue(writer, container_ptr_val, .Other);
- try writer.print(" + {})", .{
- try f.fmtIntLiteral(try pt.intValue(.usize, byte_offset)),
+ try writer.print(" + {f})", .{
+ try f.fmtIntLiteralDec(try pt.intValue(.usize, byte_offset)),
});
},
}
@@ -6121,8 +6145,8 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
try writer.writeByte('(');
}
try f.writeCValue(writer, struct_byval, .Other);
- if (bit_offset > 0) try writer.print(", {})", .{
- try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)),
+ if (bit_offset > 0) try writer.print(", {f})", .{
+ try f.fmtIntLiteralDec(try pt.intValue(bit_offset_ty, bit_offset)),
});
if (cant_cast) try writer.writeByte(')');
try f.object.dg.renderBuiltinInfo(writer, field_int_ty, .bits);
@@ -6227,8 +6251,8 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
if (!payload_ty.hasRuntimeBits(zcu))
try f.writeCValue(writer, operand, .Other)
else if (error_ty.errorSetIsEmpty(zcu))
- try writer.print("{}", .{
- try f.fmtIntLiteral(try pt.intValue(try pt.errorIntType(), 0)),
+ try writer.print("{f}", .{
+ try f.fmtIntLiteralDec(try pt.intValue(try pt.errorIntType(), 0)),
})
else if (operand_is_ptr)
try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" })
@@ -6374,7 +6398,7 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
const a = try Assignment.start(f, writer, try f.ctypeFromType(operand_ty, .complete));
try f.writeCValueDeref(writer, operand);
try a.assign(f, writer);
- try writer.print("{}", .{try f.fmtIntLiteral(no_err)});
+ try writer.print("{f}", .{try f.fmtIntLiteralDec(no_err)});
try a.end(f, writer);
return .none;
}
@@ -6382,7 +6406,7 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
const a = try Assignment.start(f, writer, try f.ctypeFromType(err_int_ty, .complete));
try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" });
try a.assign(f, writer);
- try writer.print("{}", .{try f.fmtIntLiteral(no_err)});
+ try writer.print("{f}", .{try f.fmtIntLiteralDec(no_err)});
try a.end(f, writer);
}
@@ -6520,7 +6544,7 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
if (operand_child_ctype.info(ctype_pool) == .array) {
try writer.writeByte('&');
try f.writeCValueDeref(writer, operand);
- try writer.print("[{}]", .{try f.fmtIntLiteral(.zero_usize)});
+ try writer.print("[{f}]", .{try f.fmtIntLiteralDec(.zero_usize)});
} else try f.writeCValue(writer, operand, .Other);
}
try a.end(f, writer);
@@ -6529,8 +6553,8 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
const a = try Assignment.start(f, writer, .usize);
try f.writeCValueMember(writer, local, .{ .identifier = "len" });
try a.assign(f, writer);
- try writer.print("{}", .{
- try f.fmtIntLiteral(try pt.intValue(.usize, array_ty.arrayLen(zcu))),
+ try writer.print("{f}", .{
+ try f.fmtIntLiteralDec(try pt.intValue(.usize, array_ty.arrayLen(zcu))),
});
try a.end(f, writer);
}
@@ -6736,9 +6760,9 @@ fn airCmpBuiltinCall(
try v.elem(f, writer);
try f.object.dg.renderBuiltinInfo(writer, scalar_ty, info);
try writer.writeByte(')');
- if (!ref_ret) try writer.print("{s}{}", .{
+ if (!ref_ret) try writer.print("{s}{f}", .{
compareOperatorC(operator),
- try f.fmtIntLiteral(try pt.intValue(.i32, 0)),
+ try f.fmtIntLiteralDec(try pt.intValue(.i32, 0)),
});
try writer.writeAll(";\n");
try v.end(f, inst, writer);
@@ -7148,8 +7172,8 @@ fn writeArrayLen(f: *Function, writer: ArrayListWriter, dest_ptr: CValue, dest_t
const pt = f.object.dg.pt;
const zcu = pt.zcu;
switch (dest_ty.ptrSize(zcu)) {
- .one => try writer.print("{}", .{
- try f.fmtIntLiteral(try pt.intValue(.usize, dest_ty.childType(zcu).arrayLen(zcu))),
+ .one => try writer.print("{f}", .{
+ try f.fmtIntLiteralDec(try pt.intValue(.usize, dest_ty.childType(zcu).arrayLen(zcu))),
}),
.many, .c => unreachable,
.slice => try f.writeCValueMember(writer, dest_ptr, .{ .identifier = "len" }),
@@ -7635,8 +7659,8 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
try writer.writeByte(')');
}
- try writer.print(", {}", .{
- try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)),
+ try writer.print(", {f}", .{
+ try f.fmtIntLiteralDec(try pt.intValue(bit_offset_ty, bit_offset)),
});
try f.object.dg.renderBuiltinInfo(writer, inst_ty, .bits);
try writer.writeByte(')');
@@ -7693,7 +7717,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
const a = try Assignment.start(f, writer, try f.ctypeFromType(tag_ty, .complete));
try f.writeCValueMember(writer, local, .{ .identifier = "tag" });
try a.assign(f, writer);
- try writer.print("{}", .{try f.fmtIntLiteral(try tag_val.intFromEnum(tag_ty, pt))});
+ try writer.print("{f}", .{try f.fmtIntLiteralDec(try tag_val.intFromEnum(tag_ty, pt))});
try a.end(f, writer);
}
break :field .{ .payload_identifier = field_name.toSlice(ip) };
@@ -8207,14 +8231,12 @@ fn stringLiteral(
};
}
-const FormatStringContext = struct { str: []const u8, sentinel: ?u8 };
-fn formatStringLiteral(
- data: FormatStringContext,
- writer: *std.io.Writer,
- comptime fmt: []const u8, // TODO move this state to FormatStringContext
-) std.io.Writer.Error!void {
- if (fmt.len != 1 or fmt[0] != 's') @compileError("Invalid fmt: " ++ fmt);
+const FormatStringContext = struct {
+ str: []const u8,
+ sentinel: ?u8,
+};
+fn formatStringLiteral(data: FormatStringContext, writer: *std.io.Writer) std.io.Writer.Error!void {
var literal = stringLiteral(writer, data.str.len + @intFromBool(data.sentinel != null));
try literal.start();
for (data.str) |c| try literal.writeChar(c);
@@ -8238,12 +8260,10 @@ const FormatIntLiteralContext = struct {
kind: CType.Kind,
ctype: CType,
val: Value,
+ base: u8,
+ case: std.fmt.Case,
};
-fn formatIntLiteral(
- data: FormatIntLiteralContext,
- writer: *std.io.Writer,
- comptime fmt: []const u8, // TODO move this state to FormatIntLiteralContext
-) std.io.Writer.Error!void {
+fn formatIntLiteral(data: FormatIntLiteralContext, writer: *std.io.Writer) std.io.Writer.Error!void {
const pt = data.dg.pt;
const zcu = pt.zcu;
const target = &data.dg.mod.resolved_target.result;
@@ -8268,7 +8288,7 @@ fn formatIntLiteral(
var int_buf: Value.BigIntSpace = undefined;
const int = if (data.val.isUndefDeep(zcu)) blk: {
- undef_limbs = try allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(data.int_info.bits));
+ undef_limbs = try oom(allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(data.int_info.bits)));
@memset(undef_limbs, undefPattern(BigIntLimb));
var undef_int = BigInt.Mutable{
@@ -8286,7 +8306,7 @@ fn formatIntLiteral(
const one = BigInt.Mutable.init(&one_limbs, 1).toConst();
var wrap = BigInt.Mutable{
- .limbs = try allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(c_bits)),
+ .limbs = try oom(allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(c_bits))),
.len = undefined,
.positive = undefined,
};
@@ -8333,32 +8353,14 @@ fn formatIntLiteral(
if (!int.positive) try writer.writeByte('-');
try data.ctype.renderLiteralPrefix(writer, data.kind, ctype_pool);
- const style: struct { base: u8, case: std.fmt.Case = undefined } = switch (fmt.len) {
- 0 => .{ .base = 10 },
- 1 => switch (fmt[0]) {
- 'b' => style: {
- try writer.writeAll("0b");
- break :style .{ .base = 2 };
- },
- 'o' => style: {
- try writer.writeByte('0');
- break :style .{ .base = 8 };
- },
- 'd' => .{ .base = 10 },
- 'x', 'X' => |base| style: {
- try writer.writeAll("0x");
- break :style .{ .base = 16, .case = switch (base) {
- 'x' => .lower,
- 'X' => .upper,
- else => unreachable,
- } };
- },
- else => @compileError("Invalid fmt: " ++ fmt),
- },
- else => @compileError("Invalid fmt: " ++ fmt),
- };
-
- const string = try int.abs().toStringAlloc(allocator, style.base, style.case);
+ switch (data.base) {
+ 2 => try writer.writeAll("0b"),
+ 8 => try writer.writeByte('0'),
+ 10 => {},
+ 16 => try writer.writeAll("0x"),
+ else => unreachable,
+ }
+ const string = try oom(int.abs().toStringAlloc(allocator, data.base, data.case));
defer allocator.free(string);
try writer.writeAll(string);
} else {
@@ -8411,8 +8413,10 @@ fn formatIntLiteral(
.int_info = c_limb_int_info,
.kind = data.kind,
.ctype = c_limb_ctype,
- .val = try pt.intValue_big(.comptime_int, c_limb_mut.toConst()),
- }, fmt, writer);
+ .val = try oom(pt.intValue_big(.comptime_int, c_limb_mut.toConst())),
+ .base = data.base,
+ .case = data.case,
+ }, writer);
}
}
try data.ctype.renderLiteralSuffix(writer, ctype_pool);
@@ -8492,11 +8496,11 @@ const Vectorize = struct {
try writer.writeAll("for (");
try f.writeCValue(writer, local, .Other);
- try writer.print(" = {d}; ", .{try f.fmtIntLiteral(.zero_usize)});
+ try writer.print(" = {f}; ", .{try f.fmtIntLiteralDec(.zero_usize)});
try f.writeCValue(writer, local, .Other);
- try writer.print(" < {d}; ", .{try f.fmtIntLiteral(try pt.intValue(.usize, ty.vectorLen(zcu)))});
+ try writer.print(" < {f}; ", .{try f.fmtIntLiteralDec(try pt.intValue(.usize, ty.vectorLen(zcu)))});
try f.writeCValue(writer, local, .Other);
- try writer.print(" += {d}) {{\n", .{try f.fmtIntLiteral(.one_usize)});
+ try writer.print(" += {f}) {{\n", .{try f.fmtIntLiteralDec(.one_usize)});
f.object.indent_writer.pushIndent();
break :index .{ .index = local };
@@ -8622,3 +8626,9 @@ fn deinitFreeLocalsMap(gpa: Allocator, map: *LocalsMap) void {
}
map.deinit(gpa);
}
+
+fn oom(x: anytype) error{WriteFailed}!@typeInfo(@TypeOf(x)).error_union.payload {
+ return x catch |err| switch (err) {
+ error.OutOfMemory => error.WriteFailed,
+ };
+}
diff --git a/src/codegen/c/Type.zig b/src/codegen/c/Type.zig
index ad27832e77..77555d773c 100644
--- a/src/codegen/c/Type.zig
+++ b/src/codegen/c/Type.zig
@@ -209,7 +209,7 @@ pub fn getStandardDefineAbbrev(ctype: CType) ?[]const u8 {
};
}
-pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *const Pool) @TypeOf(writer).Error!void {
+pub fn renderLiteralPrefix(ctype: CType, w: *Writer, kind: Kind, pool: *const Pool) Writer.Error!void {
switch (ctype.info(pool)) {
.basic => |basic_info| switch (basic_info) {
.void => unreachable,
@@ -224,7 +224,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con
.uintptr_t,
.intptr_t,
=> switch (kind) {
- else => try writer.print("({s})", .{@tagName(basic_info)}),
+ else => try w.print("({s})", .{@tagName(basic_info)}),
.global => {},
},
.int,
@@ -246,7 +246,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con
.int32_t,
.uint64_t,
.int64_t,
- => try writer.print("{s}_C(", .{ctype.getStandardDefineAbbrev().?}),
+ => try w.print("{s}_C(", .{ctype.getStandardDefineAbbrev().?}),
.zig_u128,
.zig_i128,
.zig_f16,
@@ -255,7 +255,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con
.zig_f80,
.zig_f128,
.zig_c_longdouble,
- => try writer.print("zig_{s}_{s}(", .{
+ => try w.print("zig_{s}_{s}(", .{
switch (kind) {
else => "make",
.global => "init",
@@ -265,12 +265,12 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con
.va_list => unreachable,
_ => unreachable,
},
- .array, .vector => try writer.writeByte('{'),
+ .array, .vector => try w.writeByte('{'),
else => unreachable,
}
}
-pub fn renderLiteralSuffix(ctype: CType, writer: anytype, pool: *const Pool) @TypeOf(writer).Error!void {
+pub fn renderLiteralSuffix(ctype: CType, w: *Writer, pool: *const Pool) Writer.Error!void {
switch (ctype.info(pool)) {
.basic => |basic_info| switch (basic_info) {
.void => unreachable,
@@ -280,20 +280,20 @@ pub fn renderLiteralSuffix(ctype: CType, writer: anytype, pool: *const Pool) @Ty
.short,
.int,
=> {},
- .long => try writer.writeByte('l'),
- .@"long long" => try writer.writeAll("ll"),
+ .long => try w.writeByte('l'),
+ .@"long long" => try w.writeAll("ll"),
.@"unsigned char",
.@"unsigned short",
.@"unsigned int",
- => try writer.writeByte('u'),
+ => try w.writeByte('u'),
.@"unsigned long",
.size_t,
.uintptr_t,
- => try writer.writeAll("ul"),
- .@"unsigned long long" => try writer.writeAll("ull"),
- .float => try writer.writeByte('f'),
+ => try w.writeAll("ul"),
+ .@"unsigned long long" => try w.writeAll("ull"),
+ .float => try w.writeByte('f'),
.double => {},
- .@"long double" => try writer.writeByte('l'),
+ .@"long double" => try w.writeByte('l'),
.bool,
.ptrdiff_t,
.intptr_t,
@@ -314,11 +314,11 @@ pub fn renderLiteralSuffix(ctype: CType, writer: anytype, pool: *const Pool) @Ty
.zig_f80,
.zig_f128,
.zig_c_longdouble,
- => try writer.writeByte(')'),
+ => try w.writeByte(')'),
.va_list => unreachable,
_ => unreachable,
},
- .array, .vector => try writer.writeByte('}'),
+ .array, .vector => try w.writeByte('}'),
else => unreachable,
}
}
@@ -938,7 +938,7 @@ pub const Pool = struct {
index: String.Index,
const FormatData = struct { string: String, pool: *const Pool };
- fn format(data: FormatData, writer: *std.io.Writer) std.io.Writer.Error!void {
+ fn format(data: FormatData, writer: *Writer) Writer.Error!void {
if (data.string.toSlice(data.pool)) |slice|
try writer.writeAll(slice)
else
@@ -2884,7 +2884,7 @@ pub const Pool = struct {
comptime fmt_str: []const u8,
fmt_args: anytype,
) !String {
- try pool.string_bytes.writer(allocator).print(fmt_str, fmt_args);
+ try pool.string_bytes.print(allocator, fmt_str, fmt_args);
return pool.trailingString(allocator);
}
@@ -3275,10 +3275,12 @@ pub const AlignAs = packed struct {
}
};
+const std = @import("std");
const assert = std.debug.assert;
+const Writer = std.io.Writer;
+
const CType = @This();
const InternPool = @import("../../InternPool.zig");
const Module = @import("../../Package/Module.zig");
-const std = @import("std");
const Type = @import("../../Type.zig");
const Zcu = @import("../../Zcu.zig");
diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig
index 40c5c234d3..3d670dce83 100644
--- a/src/codegen/llvm.zig
+++ b/src/codegen/llvm.zig
@@ -239,12 +239,12 @@ pub fn targetTriple(allocator: Allocator, target: *const std.Target) ![]const u8
.none,
.windows,
=> {},
- .semver => |ver| try llvm_triple.writer().print("{d}.{d}.{d}", .{
+ .semver => |ver| try llvm_triple.print("{d}.{d}.{d}", .{
ver.min.major,
ver.min.minor,
ver.min.patch,
}),
- inline .linux, .hurd => |ver| try llvm_triple.writer().print("{d}.{d}.{d}", .{
+ inline .linux, .hurd => |ver| try llvm_triple.print("{d}.{d}.{d}", .{
ver.range.min.major,
ver.range.min.minor,
ver.range.min.patch,
@@ -295,13 +295,13 @@ pub fn targetTriple(allocator: Allocator, target: *const std.Target) ![]const u8
.windows,
=> {},
inline .hurd, .linux => |ver| if (target.abi.isGnu()) {
- try llvm_triple.writer().print("{d}.{d}.{d}", .{
+ try llvm_triple.print("{d}.{d}.{d}", .{
ver.glibc.major,
ver.glibc.minor,
ver.glibc.patch,
});
} else if (@TypeOf(ver) == std.Target.Os.LinuxVersionRange and target.abi.isAndroid()) {
- try llvm_triple.writer().print("{d}", .{ver.android});
+ try llvm_triple.print("{d}", .{ver.android});
},
}
@@ -746,12 +746,18 @@ pub const Object = struct {
try wip.finish();
}
- fn genModuleLevelAssembly(object: *Object) !void {
- const writer = object.builder.setModuleAsm();
+ fn genModuleLevelAssembly(object: *Object) Allocator.Error!void {
+ const b = &object.builder;
+ const gpa = b.gpa;
+ b.module_asm.clearRetainingCapacity();
for (object.pt.zcu.global_assembly.values()) |assembly| {
- try writer.print("{s}\n", .{assembly});
+ try b.module_asm.ensureUnusedCapacity(gpa, assembly.len + 1);
+ b.module_asm.appendSliceAssumeCapacity(assembly);
+ b.module_asm.appendAssumeCapacity('\n');
+ }
+ if (b.module_asm.getLastOrNull()) |last| {
+ if (last != '\n') try b.module_asm.append(gpa, '\n');
}
- try object.builder.finishModuleAsm();
}
pub const EmitOptions = struct {
@@ -939,7 +945,9 @@ pub const Object = struct {
if (std.mem.eql(u8, path, "-")) {
o.builder.dump();
} else {
- _ = try o.builder.printToFile(path);
+ o.builder.printToFilePath(std.fs.cwd(), path) catch |err| {
+ log.err("failed printing LLVM module to \"{s}\": {s}", .{ path, @errorName(err) });
+ };
}
}
@@ -2680,10 +2688,12 @@ pub const Object = struct {
}
fn allocTypeName(o: *Object, ty: Type) Allocator.Error![:0]const u8 {
- var buffer = std.ArrayList(u8).init(o.gpa);
- errdefer buffer.deinit();
- try ty.print(buffer.writer(), o.pt);
- return buffer.toOwnedSliceSentinel(0);
+ var aw: std.io.Writer.Allocating = .init(o.gpa);
+ defer aw.deinit();
+ ty.print(&aw.writer, o.pt) catch |err| switch (err) {
+ error.WriteFailed => return error.OutOfMemory,
+ };
+ return aw.toOwnedSliceSentinel(0);
}
/// If the llvm function does not exist, create it.
@@ -4482,7 +4492,7 @@ pub const Object = struct {
const target = &zcu.root_mod.resolved_target.result;
const function_index = try o.builder.addFunction(
try o.builder.fnType(ret_ty, &.{try o.lowerType(Type.fromInterned(enum_type.tag_ty))}, .normal),
- try o.builder.strtabStringFmt("__zig_tag_name_{}", .{enum_type.name.fmt(ip)}),
+ try o.builder.strtabStringFmt("__zig_tag_name_{f}", .{enum_type.name.fmt(ip)}),
toLlvmAddressSpace(.generic, target),
);
@@ -4633,7 +4643,7 @@ pub const NavGen = struct {
if (zcu.getTarget().cpu.arch.isWasm() and ty.zigTypeTag(zcu) == .@"fn") {
if (lib_name.toSlice(ip)) |lib_name_slice| {
if (!std.mem.eql(u8, lib_name_slice, "c")) {
- break :decl_name try o.builder.strtabStringFmt("{}|{s}", .{ nav.name.fmt(ip), lib_name_slice });
+ break :decl_name try o.builder.strtabStringFmt("{f}|{s}", .{ nav.name.fmt(ip), lib_name_slice });
}
}
}
@@ -7472,7 +7482,7 @@ pub const FuncGen = struct {
llvm_param_types[llvm_param_i] = llvm_elem_ty;
}
- try llvm_constraints.writer(self.gpa).print(",{d}", .{output_index});
+ try llvm_constraints.print(self.gpa, ",{d}", .{output_index});
// In the case of indirect inputs, LLVM requires the callsite to have
// an elementtype(<ty>) attribute.
@@ -7573,7 +7583,7 @@ pub const FuncGen = struct {
// we should validate the assembly in Sema; by now it is too late
return self.todo("unknown input or output name: '{s}'", .{name});
};
- try rendered_template.writer().print("{d}", .{index});
+ try rendered_template.print("{d}", .{index});
if (byte == ':') {
try rendered_template.append(':');
modifier_start = i + 1;
@@ -10370,7 +10380,7 @@ pub const FuncGen = struct {
const target = &zcu.root_mod.resolved_target.result;
const function_index = try o.builder.addFunction(
try o.builder.fnType(.i1, &.{try o.lowerType(Type.fromInterned(enum_type.tag_ty))}, .normal),
- try o.builder.strtabStringFmt("__zig_is_named_enum_value_{}", .{enum_type.name.fmt(ip)}),
+ try o.builder.strtabStringFmt("__zig_is_named_enum_value_{f}", .{enum_type.name.fmt(ip)}),
toLlvmAddressSpace(.generic, target),
);
diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig
index 8a782c54aa..901687d40b 100644
--- a/src/codegen/spirv.zig
+++ b/src/codegen/spirv.zig
@@ -1260,10 +1260,12 @@ const NavGen = struct {
// Turn a Zig type's name into a cache reference.
fn resolveTypeName(self: *NavGen, ty: Type) ![]const u8 {
- var name = std.ArrayList(u8).init(self.gpa);
- defer name.deinit();
- try ty.print(name.writer(), self.pt);
- return try name.toOwnedSlice();
+ var aw: std.io.Writer.Allocating = .init(self.gpa);
+ defer aw.deinit();
+ ty.print(&aw.writer, self.pt) catch |err| switch (err) {
+ error.WriteFailed => return error.OutOfMemory,
+ };
+ return try aw.toOwnedSlice();
}
/// Create an integer type suitable for storing at least 'bits' bits.