| author | |
| committer | |
| log | 756a2dbf1a5f8af7fe153960e332eaad2ab3bcd8 |
| tree | 15f74e430673e16884aec01aa8374eb35e214ac6 |
| parent | 941bc3719382a4f6245ad42175d911964f1bc9a4 |
11 files changed, 398 insertions(+), 403 deletions(-)
lib/std/io/tty.zig+4-7| ... | @@ -71,12 +71,9 @@ pub const Config = union(enum) { | ... | @@ -71,12 +71,9 @@ pub const Config = union(enum) { |
| 71 | reset_attributes: u16, | 71 | reset_attributes: u16, |
| 72 | }; | 72 | }; |
| 73 | 73 | ||
| 74 | pub fn setColor( | 74 | pub const SetColorError = std.os.windows.SetConsoleTextAttributeError || std.io.Writer.Error; |
| 75 | conf: Config, | 75 | |
| 76 | writer: anytype, | 76 | pub fn setColor(conf: Config, w: *std.io.Writer, color: Color) SetColorError!void { |
| 77 | color: Color, | ||
| 78 | ) (@typeInfo(@TypeOf(writer.writeAll(""))).error_union.error_set || | ||
| 79 | windows.SetConsoleTextAttributeError)!void { | ||
| 80 | nosuspend switch (conf) { | 77 | nosuspend switch (conf) { |
| 81 | .no_color => return, | 78 | .no_color => return, |
| 82 | .escape_codes => { | 79 | .escape_codes => { |
| ... | @@ -101,7 +98,7 @@ pub const Config = union(enum) { | ... | @@ -101,7 +98,7 @@ pub const Config = union(enum) { |
| 101 | .dim => "\x1b[2m", | 98 | .dim => "\x1b[2m", |
| 102 | .reset => "\x1b[0m", | 99 | .reset => "\x1b[0m", |
| 103 | }; | 100 | }; |
| 104 | try writer.writeAll(color_string); | 101 | try w.writeAll(color_string); |
| 105 | }, | 102 | }, |
| 106 | .windows_api => |ctx| if (native_os == .windows) { | 103 | .windows_api => |ctx| if (native_os == .windows) { |
| 107 | const attributes = switch (color) { | 104 | const attributes = switch (color) { |
src/Air/print.zig+102-96| ... | @@ -1,6 +1,5 @@ | ... | @@ -1,6 +1,5 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const Allocator = std.mem.Allocator; | 2 | const Allocator = std.mem.Allocator; |
| 3 | const fmtIntSizeBin = std.fmt.fmtIntSizeBin; | ||
| 4 | 3 | ||
| 5 | const build_options = @import("build_options"); | 4 | const build_options = @import("build_options"); |
| 6 | const Zcu = @import("../Zcu.zig"); | 5 | const Zcu = @import("../Zcu.zig"); |
| ... | @@ -9,7 +8,7 @@ const Type = @import("../Type.zig"); | ... | @@ -9,7 +8,7 @@ const Type = @import("../Type.zig"); |
| 9 | const Air = @import("../Air.zig"); | 8 | const Air = @import("../Air.zig"); |
| 10 | const InternPool = @import("../InternPool.zig"); | 9 | const InternPool = @import("../InternPool.zig"); |
| 11 | 10 | ||
| 12 | pub fn write(air: Air, stream: anytype, pt: Zcu.PerThread, liveness: ?Air.Liveness) void { | 11 | pub fn write(air: Air, stream: *std.io.Writer, pt: Zcu.PerThread, liveness: ?Air.Liveness) void { |
| 13 | comptime std.debug.assert(build_options.enable_debug_extensions); | 12 | comptime std.debug.assert(build_options.enable_debug_extensions); |
| 14 | const instruction_bytes = air.instructions.len * | 13 | const instruction_bytes = air.instructions.len * |
| 15 | // Here we don't use @sizeOf(Air.Inst.Data) because it would include | 14 | // Here we don't use @sizeOf(Air.Inst.Data) because it would include |
| ... | @@ -25,20 +24,20 @@ pub fn write(air: Air, stream: anytype, pt: Zcu.PerThread, liveness: ?Air.Livene | ... | @@ -25,20 +24,20 @@ pub fn write(air: Air, stream: anytype, pt: Zcu.PerThread, liveness: ?Air.Livene |
| 25 | 24 | ||
| 26 | // zig fmt: off | 25 | // zig fmt: off |
| 27 | stream.print( | 26 | stream.print( |
| 28 | \\# Total AIR+Liveness bytes: {} | 27 | \\# Total AIR+Liveness bytes: {Bi} |
| 29 | \\# AIR Instructions: {d} ({}) | 28 | \\# AIR Instructions: {d} ({Bi}) |
| 30 | \\# AIR Extra Data: {d} ({}) | 29 | \\# AIR Extra Data: {d} ({Bi}) |
| 31 | \\# Liveness tomb_bits: {} | 30 | \\# Liveness tomb_bits: {Bi} |
| 32 | \\# Liveness Extra Data: {d} ({}) | 31 | \\# Liveness Extra Data: {d} ({Bi}) |
| 33 | \\# Liveness special table: {d} ({}) | 32 | \\# Liveness special table: {d} ({Bi}) |
| 34 | \\ | 33 | \\ |
| 35 | , .{ | 34 | , .{ |
| 36 | fmtIntSizeBin(total_bytes), | 35 | total_bytes, |
| 37 | air.instructions.len, fmtIntSizeBin(instruction_bytes), | 36 | air.instructions.len, instruction_bytes, |
| 38 | air.extra.items.len, fmtIntSizeBin(extra_bytes), | 37 | air.extra.items.len, extra_bytes, |
| 39 | fmtIntSizeBin(tomb_bytes), | 38 | tomb_bytes, |
| 40 | if (liveness) |l| l.extra.len else 0, fmtIntSizeBin(liveness_extra_bytes), | 39 | if (liveness) |l| l.extra.len else 0, liveness_extra_bytes, |
| 41 | if (liveness) |l| l.special.count() else 0, fmtIntSizeBin(liveness_special_bytes), | 40 | if (liveness) |l| l.special.count() else 0, liveness_special_bytes, |
| 42 | }) catch return; | 41 | }) catch return; |
| 43 | // zig fmt: on | 42 | // zig fmt: on |
| 44 | 43 | ||
| ... | @@ -55,7 +54,7 @@ pub fn write(air: Air, stream: anytype, pt: Zcu.PerThread, liveness: ?Air.Livene | ... | @@ -55,7 +54,7 @@ pub fn write(air: Air, stream: anytype, pt: Zcu.PerThread, liveness: ?Air.Livene |
| 55 | 54 | ||
| 56 | pub fn writeInst( | 55 | pub fn writeInst( |
| 57 | air: Air, | 56 | air: Air, |
| 58 | stream: anytype, | 57 | stream: *std.io.Writer, |
| 59 | inst: Air.Inst.Index, | 58 | inst: Air.Inst.Index, |
| 60 | pt: Zcu.PerThread, | 59 | pt: Zcu.PerThread, |
| 61 | liveness: ?Air.Liveness, | 60 | liveness: ?Air.Liveness, |
| ... | @@ -73,11 +72,15 @@ pub fn writeInst( | ... | @@ -73,11 +72,15 @@ pub fn writeInst( |
| 73 | } | 72 | } |
| 74 | 73 | ||
| 75 | pub fn dump(air: Air, pt: Zcu.PerThread, liveness: ?Air.Liveness) void { | 74 | pub fn dump(air: Air, pt: Zcu.PerThread, liveness: ?Air.Liveness) void { |
| 76 | air.write(std.fs.File.stderr().deprecatedWriter(), pt, liveness); | 75 | const stderr_bw = std.debug.lockStderrWriter(&.{}); |
| 76 | defer std.debug.unlockStderrWriter(); | ||
| 77 | air.write(stderr_bw, pt, liveness); | ||
| 77 | } | 78 | } |
| 78 | 79 | ||
| 79 | pub fn dumpInst(air: Air, inst: Air.Inst.Index, pt: Zcu.PerThread, liveness: ?Air.Liveness) void { | 80 | pub fn dumpInst(air: Air, inst: Air.Inst.Index, pt: Zcu.PerThread, liveness: ?Air.Liveness) void { |
| 80 | air.writeInst(std.fs.File.stderr().deprecatedWriter(), inst, pt, liveness); | 81 | const stderr_bw = std.debug.lockStderrWriter(&.{}); |
| 82 | defer std.debug.unlockStderrWriter(); | ||
| 83 | air.writeInst(stderr_bw, inst, pt, liveness); | ||
| 81 | } | 84 | } |
| 82 | 85 | ||
| 83 | const Writer = struct { | 86 | const Writer = struct { |
| ... | @@ -88,17 +91,19 @@ const Writer = struct { | ... | @@ -88,17 +91,19 @@ const Writer = struct { |
| 88 | indent: usize, | 91 | indent: usize, |
| 89 | skip_body: bool, | 92 | skip_body: bool, |
| 90 | 93 | ||
| 91 | fn writeBody(w: *Writer, s: anytype, body: []const Air.Inst.Index) @TypeOf(s).Error!void { | 94 | const Error = std.io.Writer.Error; |
| 95 | |||
| 96 | fn writeBody(w: *Writer, s: *std.io.Writer, body: []const Air.Inst.Index) Error!void { | ||
| 92 | for (body) |inst| { | 97 | for (body) |inst| { |
| 93 | try w.writeInst(s, inst); | 98 | try w.writeInst(s, inst); |
| 94 | try s.writeByte('\n'); | 99 | try s.writeByte('\n'); |
| 95 | } | 100 | } |
| 96 | } | 101 | } |
| 97 | 102 | ||
| 98 | fn writeInst(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 103 | fn writeInst(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 99 | const tag = w.air.instructions.items(.tag)[@intFromEnum(inst)]; | 104 | const tag = w.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 100 | try s.writeByteNTimes(' ', w.indent); | 105 | try s.splatByteAll(' ', w.indent); |
| 101 | try s.print("{}{c}= {s}(", .{ | 106 | try s.print("{f}{c}= {s}(", .{ |
| 102 | inst, | 107 | inst, |
| 103 | @as(u8, if (if (w.liveness) |liveness| liveness.isUnused(inst) else false) '!' else ' '), | 108 | @as(u8, if (if (w.liveness) |liveness| liveness.isUnused(inst) else false) '!' else ' '), |
| 104 | @tagName(tag), | 109 | @tagName(tag), |
| ... | @@ -335,47 +340,48 @@ const Writer = struct { | ... | @@ -335,47 +340,48 @@ const Writer = struct { |
| 335 | try s.writeByte(')'); | 340 | try s.writeByte(')'); |
| 336 | } | 341 | } |
| 337 | 342 | ||
| 338 | fn writeBinOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 343 | fn writeBinOp(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 339 | const bin_op = w.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 344 | const bin_op = w.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 340 | try w.writeOperand(s, inst, 0, bin_op.lhs); | 345 | try w.writeOperand(s, inst, 0, bin_op.lhs); |
| 341 | try s.writeAll(", "); | 346 | try s.writeAll(", "); |
| 342 | try w.writeOperand(s, inst, 1, bin_op.rhs); | 347 | try w.writeOperand(s, inst, 1, bin_op.rhs); |
| 343 | } | 348 | } |
| 344 | 349 | ||
| 345 | fn writeUnOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 350 | fn writeUnOp(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 346 | const un_op = w.air.instructions.items(.data)[@intFromEnum(inst)].un_op; | 351 | const un_op = w.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 347 | try w.writeOperand(s, inst, 0, un_op); | 352 | try w.writeOperand(s, inst, 0, un_op); |
| 348 | } | 353 | } |
| 349 | 354 | ||
| 350 | fn writeNoOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 355 | fn writeNoOp(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 351 | _ = w; | 356 | _ = w; |
| 357 | _ = s; | ||
| 352 | _ = inst; | 358 | _ = inst; |
| 353 | // no-op, no argument to write | 359 | // no-op, no argument to write |
| 354 | } | 360 | } |
| 355 | 361 | ||
| 356 | fn writeType(w: *Writer, s: anytype, ty: Type) !void { | 362 | fn writeType(w: *Writer, s: *std.io.Writer, ty: Type) !void { |
| 357 | return ty.print(s, w.pt); | 363 | return ty.print(s, w.pt); |
| 358 | } | 364 | } |
| 359 | 365 | ||
| 360 | fn writeTy(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 366 | fn writeTy(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 361 | const ty = w.air.instructions.items(.data)[@intFromEnum(inst)].ty; | 367 | const ty = w.air.instructions.items(.data)[@intFromEnum(inst)].ty; |
| 362 | try w.writeType(s, ty); | 368 | try w.writeType(s, ty); |
| 363 | } | 369 | } |
| 364 | 370 | ||
| 365 | fn writeArg(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 371 | fn writeArg(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 366 | const arg = w.air.instructions.items(.data)[@intFromEnum(inst)].arg; | 372 | const arg = w.air.instructions.items(.data)[@intFromEnum(inst)].arg; |
| 367 | try w.writeType(s, arg.ty.toType()); | 373 | try w.writeType(s, arg.ty.toType()); |
| 368 | try s.print(", {d}", .{arg.zir_param_index}); | 374 | try s.print(", {d}", .{arg.zir_param_index}); |
| 369 | } | 375 | } |
| 370 | 376 | ||
| 371 | fn writeTyOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 377 | fn writeTyOp(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 372 | const ty_op = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 378 | const ty_op = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 373 | try w.writeType(s, ty_op.ty.toType()); | 379 | try w.writeType(s, ty_op.ty.toType()); |
| 374 | try s.writeAll(", "); | 380 | try s.writeAll(", "); |
| 375 | try w.writeOperand(s, inst, 0, ty_op.operand); | 381 | try w.writeOperand(s, inst, 0, ty_op.operand); |
| 376 | } | 382 | } |
| 377 | 383 | ||
| 378 | fn writeBlock(w: *Writer, s: anytype, tag: Air.Inst.Tag, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 384 | fn writeBlock(w: *Writer, s: *std.io.Writer, tag: Air.Inst.Tag, inst: Air.Inst.Index) Error!void { |
| 379 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 385 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 380 | try w.writeType(s, ty_pl.ty.toType()); | 386 | try w.writeType(s, ty_pl.ty.toType()); |
| 381 | const body: []const Air.Inst.Index = @ptrCast(switch (tag) { | 387 | const body: []const Air.Inst.Index = @ptrCast(switch (tag) { |
| ... | @@ -408,15 +414,15 @@ const Writer = struct { | ... | @@ -408,15 +414,15 @@ const Writer = struct { |
| 408 | w.indent += 2; | 414 | w.indent += 2; |
| 409 | try w.writeBody(s, body); | 415 | try w.writeBody(s, body); |
| 410 | w.indent = old_indent; | 416 | w.indent = old_indent; |
| 411 | try s.writeByteNTimes(' ', w.indent); | 417 | try s.splatByteAll(' ', w.indent); |
| 412 | try s.writeAll("}"); | 418 | try s.writeAll("}"); |
| 413 | 419 | ||
| 414 | for (liveness_block.deaths) |operand| { | 420 | for (liveness_block.deaths) |operand| { |
| 415 | try s.print(" {}!", .{operand}); | 421 | try s.print(" {f}!", .{operand}); |
| 416 | } | 422 | } |
| 417 | } | 423 | } |
| 418 | 424 | ||
| 419 | fn writeLoop(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 425 | fn writeLoop(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 420 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 426 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 421 | const extra = w.air.extraData(Air.Block, ty_pl.payload); | 427 | const extra = w.air.extraData(Air.Block, ty_pl.payload); |
| 422 | const body: []const Air.Inst.Index = @ptrCast(w.air.extra.items[extra.end..][0..extra.data.body_len]); | 428 | const body: []const Air.Inst.Index = @ptrCast(w.air.extra.items[extra.end..][0..extra.data.body_len]); |
| ... | @@ -428,11 +434,11 @@ const Writer = struct { | ... | @@ -428,11 +434,11 @@ const Writer = struct { |
| 428 | w.indent += 2; | 434 | w.indent += 2; |
| 429 | try w.writeBody(s, body); | 435 | try w.writeBody(s, body); |
| 430 | w.indent = old_indent; | 436 | w.indent = old_indent; |
| 431 | try s.writeByteNTimes(' ', w.indent); | 437 | try s.splatByteAll(' ', w.indent); |
| 432 | try s.writeAll("}"); | 438 | try s.writeAll("}"); |
| 433 | } | 439 | } |
| 434 | 440 | ||
| 435 | fn writeAggregateInit(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 441 | fn writeAggregateInit(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 436 | const zcu = w.pt.zcu; | 442 | const zcu = w.pt.zcu; |
| 437 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 443 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 438 | const vector_ty = ty_pl.ty.toType(); | 444 | const vector_ty = ty_pl.ty.toType(); |
| ... | @@ -448,7 +454,7 @@ const Writer = struct { | ... | @@ -448,7 +454,7 @@ const Writer = struct { |
| 448 | try s.writeAll("]"); | 454 | try s.writeAll("]"); |
| 449 | } | 455 | } |
| 450 | 456 | ||
| 451 | fn writeUnionInit(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 457 | fn writeUnionInit(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 452 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 458 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 453 | const extra = w.air.extraData(Air.UnionInit, ty_pl.payload).data; | 459 | const extra = w.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 454 | 460 | ||
| ... | @@ -456,7 +462,7 @@ const Writer = struct { | ... | @@ -456,7 +462,7 @@ const Writer = struct { |
| 456 | try w.writeOperand(s, inst, 0, extra.init); | 462 | try w.writeOperand(s, inst, 0, extra.init); |
| 457 | } | 463 | } |
| 458 | 464 | ||
| 459 | fn writeStructField(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 465 | fn writeStructField(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 460 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 466 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 461 | const extra = w.air.extraData(Air.StructField, ty_pl.payload).data; | 467 | const extra = w.air.extraData(Air.StructField, ty_pl.payload).data; |
| 462 | 468 | ||
| ... | @@ -464,7 +470,7 @@ const Writer = struct { | ... | @@ -464,7 +470,7 @@ const Writer = struct { |
| 464 | try s.print(", {d}", .{extra.field_index}); | 470 | try s.print(", {d}", .{extra.field_index}); |
| 465 | } | 471 | } |
| 466 | 472 | ||
| 467 | fn writeTyPlBin(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 473 | fn writeTyPlBin(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 468 | const data = w.air.instructions.items(.data); | 474 | const data = w.air.instructions.items(.data); |
| 469 | const ty_pl = data[@intFromEnum(inst)].ty_pl; | 475 | const ty_pl = data[@intFromEnum(inst)].ty_pl; |
| 470 | const extra = w.air.extraData(Air.Bin, ty_pl.payload).data; | 476 | const extra = w.air.extraData(Air.Bin, ty_pl.payload).data; |
| ... | @@ -477,7 +483,7 @@ const Writer = struct { | ... | @@ -477,7 +483,7 @@ const Writer = struct { |
| 477 | try w.writeOperand(s, inst, 1, extra.rhs); | 483 | try w.writeOperand(s, inst, 1, extra.rhs); |
| 478 | } | 484 | } |
| 479 | 485 | ||
| 480 | fn writeCmpxchg(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 486 | fn writeCmpxchg(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 481 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 487 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 482 | const extra = w.air.extraData(Air.Cmpxchg, ty_pl.payload).data; | 488 | const extra = w.air.extraData(Air.Cmpxchg, ty_pl.payload).data; |
| 483 | 489 | ||
| ... | @@ -491,7 +497,7 @@ const Writer = struct { | ... | @@ -491,7 +497,7 @@ const Writer = struct { |
| 491 | }); | 497 | }); |
| 492 | } | 498 | } |
| 493 | 499 | ||
| 494 | fn writeMulAdd(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 500 | fn writeMulAdd(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 495 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 501 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 496 | const extra = w.air.extraData(Air.Bin, pl_op.payload).data; | 502 | const extra = w.air.extraData(Air.Bin, pl_op.payload).data; |
| 497 | 503 | ||
| ... | @@ -502,7 +508,7 @@ const Writer = struct { | ... | @@ -502,7 +508,7 @@ const Writer = struct { |
| 502 | try w.writeOperand(s, inst, 2, pl_op.operand); | 508 | try w.writeOperand(s, inst, 2, pl_op.operand); |
| 503 | } | 509 | } |
| 504 | 510 | ||
| 505 | fn writeShuffleOne(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 511 | fn writeShuffleOne(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 506 | const unwrapped = w.air.unwrapShuffleOne(w.pt.zcu, inst); | 512 | const unwrapped = w.air.unwrapShuffleOne(w.pt.zcu, inst); |
| 507 | try w.writeType(s, unwrapped.result_ty); | 513 | try w.writeType(s, unwrapped.result_ty); |
| 508 | try s.writeAll(", "); | 514 | try s.writeAll(", "); |
| ... | @@ -518,7 +524,7 @@ const Writer = struct { | ... | @@ -518,7 +524,7 @@ const Writer = struct { |
| 518 | try s.writeByte(']'); | 524 | try s.writeByte(']'); |
| 519 | } | 525 | } |
| 520 | 526 | ||
| 521 | fn writeShuffleTwo(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 527 | fn writeShuffleTwo(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 522 | const unwrapped = w.air.unwrapShuffleTwo(w.pt.zcu, inst); | 528 | const unwrapped = w.air.unwrapShuffleTwo(w.pt.zcu, inst); |
| 523 | try w.writeType(s, unwrapped.result_ty); | 529 | try w.writeType(s, unwrapped.result_ty); |
| 524 | try s.writeAll(", "); | 530 | try s.writeAll(", "); |
| ... | @@ -537,7 +543,7 @@ const Writer = struct { | ... | @@ -537,7 +543,7 @@ const Writer = struct { |
| 537 | try s.writeByte(']'); | 543 | try s.writeByte(']'); |
| 538 | } | 544 | } |
| 539 | 545 | ||
| 540 | fn writeSelect(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 546 | fn writeSelect(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 541 | const zcu = w.pt.zcu; | 547 | const zcu = w.pt.zcu; |
| 542 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 548 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 543 | const extra = w.air.extraData(Air.Bin, pl_op.payload).data; | 549 | const extra = w.air.extraData(Air.Bin, pl_op.payload).data; |
| ... | @@ -552,14 +558,14 @@ const Writer = struct { | ... | @@ -552,14 +558,14 @@ const Writer = struct { |
| 552 | try w.writeOperand(s, inst, 2, extra.rhs); | 558 | try w.writeOperand(s, inst, 2, extra.rhs); |
| 553 | } | 559 | } |
| 554 | 560 | ||
| 555 | fn writeReduce(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 561 | fn writeReduce(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 556 | const reduce = w.air.instructions.items(.data)[@intFromEnum(inst)].reduce; | 562 | const reduce = w.air.instructions.items(.data)[@intFromEnum(inst)].reduce; |
| 557 | 563 | ||
| 558 | try w.writeOperand(s, inst, 0, reduce.operand); | 564 | try w.writeOperand(s, inst, 0, reduce.operand); |
| 559 | try s.print(", {s}", .{@tagName(reduce.operation)}); | 565 | try s.print(", {s}", .{@tagName(reduce.operation)}); |
| 560 | } | 566 | } |
| 561 | 567 | ||
| 562 | fn writeCmpVector(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 568 | fn writeCmpVector(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 563 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 569 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 564 | const extra = w.air.extraData(Air.VectorCmp, ty_pl.payload).data; | 570 | const extra = w.air.extraData(Air.VectorCmp, ty_pl.payload).data; |
| 565 | 571 | ||
| ... | @@ -569,7 +575,7 @@ const Writer = struct { | ... | @@ -569,7 +575,7 @@ const Writer = struct { |
| 569 | try w.writeOperand(s, inst, 1, extra.rhs); | 575 | try w.writeOperand(s, inst, 1, extra.rhs); |
| 570 | } | 576 | } |
| 571 | 577 | ||
| 572 | fn writeVectorStoreElem(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 578 | fn writeVectorStoreElem(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 573 | const data = w.air.instructions.items(.data)[@intFromEnum(inst)].vector_store_elem; | 579 | const data = w.air.instructions.items(.data)[@intFromEnum(inst)].vector_store_elem; |
| 574 | const extra = w.air.extraData(Air.VectorCmp, data.payload).data; | 580 | const extra = w.air.extraData(Air.VectorCmp, data.payload).data; |
| 575 | 581 | ||
| ... | @@ -580,21 +586,21 @@ const Writer = struct { | ... | @@ -580,21 +586,21 @@ const Writer = struct { |
| 580 | try w.writeOperand(s, inst, 2, extra.rhs); | 586 | try w.writeOperand(s, inst, 2, extra.rhs); |
| 581 | } | 587 | } |
| 582 | 588 | ||
| 583 | fn writeRuntimeNavPtr(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 589 | fn writeRuntimeNavPtr(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 584 | const ip = &w.pt.zcu.intern_pool; | 590 | const ip = &w.pt.zcu.intern_pool; |
| 585 | const ty_nav = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav; | 591 | const ty_nav = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav; |
| 586 | try w.writeType(s, .fromInterned(ty_nav.ty)); | 592 | try w.writeType(s, .fromInterned(ty_nav.ty)); |
| 587 | try s.print(", '{}'", .{ip.getNav(ty_nav.nav).fqn.fmt(ip)}); | 593 | try s.print(", '{}'", .{ip.getNav(ty_nav.nav).fqn.fmt(ip)}); |
| 588 | } | 594 | } |
| 589 | 595 | ||
| 590 | fn writeAtomicLoad(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 596 | fn writeAtomicLoad(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 591 | const atomic_load = w.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load; | 597 | const atomic_load = w.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load; |
| 592 | 598 | ||
| 593 | try w.writeOperand(s, inst, 0, atomic_load.ptr); | 599 | try w.writeOperand(s, inst, 0, atomic_load.ptr); |
| 594 | try s.print(", {s}", .{@tagName(atomic_load.order)}); | 600 | try s.print(", {s}", .{@tagName(atomic_load.order)}); |
| 595 | } | 601 | } |
| 596 | 602 | ||
| 597 | fn writePrefetch(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 603 | fn writePrefetch(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 598 | const prefetch = w.air.instructions.items(.data)[@intFromEnum(inst)].prefetch; | 604 | const prefetch = w.air.instructions.items(.data)[@intFromEnum(inst)].prefetch; |
| 599 | 605 | ||
| 600 | try w.writeOperand(s, inst, 0, prefetch.ptr); | 606 | try w.writeOperand(s, inst, 0, prefetch.ptr); |
| ... | @@ -605,10 +611,10 @@ const Writer = struct { | ... | @@ -605,10 +611,10 @@ const Writer = struct { |
| 605 | 611 | ||
| 606 | fn writeAtomicStore( | 612 | fn writeAtomicStore( |
| 607 | w: *Writer, | 613 | w: *Writer, |
| 608 | s: anytype, | 614 | s: *std.io.Writer, |
| 609 | inst: Air.Inst.Index, | 615 | inst: Air.Inst.Index, |
| 610 | order: std.builtin.AtomicOrder, | 616 | order: std.builtin.AtomicOrder, |
| 611 | ) @TypeOf(s).Error!void { | 617 | ) Error!void { |
| 612 | const bin_op = w.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 618 | const bin_op = w.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 613 | try w.writeOperand(s, inst, 0, bin_op.lhs); | 619 | try w.writeOperand(s, inst, 0, bin_op.lhs); |
| 614 | try s.writeAll(", "); | 620 | try s.writeAll(", "); |
| ... | @@ -616,7 +622,7 @@ const Writer = struct { | ... | @@ -616,7 +622,7 @@ const Writer = struct { |
| 616 | try s.print(", {s}", .{@tagName(order)}); | 622 | try s.print(", {s}", .{@tagName(order)}); |
| 617 | } | 623 | } |
| 618 | 624 | ||
| 619 | fn writeAtomicRmw(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 625 | fn writeAtomicRmw(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 620 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 626 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 621 | const extra = w.air.extraData(Air.AtomicRmw, pl_op.payload).data; | 627 | const extra = w.air.extraData(Air.AtomicRmw, pl_op.payload).data; |
| 622 | 628 | ||
| ... | @@ -626,7 +632,7 @@ const Writer = struct { | ... | @@ -626,7 +632,7 @@ const Writer = struct { |
| 626 | try s.print(", {s}, {s}", .{ @tagName(extra.op()), @tagName(extra.ordering()) }); | 632 | try s.print(", {s}, {s}", .{ @tagName(extra.op()), @tagName(extra.ordering()) }); |
| 627 | } | 633 | } |
| 628 | 634 | ||
| 629 | fn writeFieldParentPtr(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 635 | fn writeFieldParentPtr(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 630 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 636 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 631 | const extra = w.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; | 637 | const extra = w.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| 632 | 638 | ||
| ... | @@ -634,7 +640,7 @@ const Writer = struct { | ... | @@ -634,7 +640,7 @@ const Writer = struct { |
| 634 | try s.print(", {d}", .{extra.field_index}); | 640 | try s.print(", {d}", .{extra.field_index}); |
| 635 | } | 641 | } |
| 636 | 642 | ||
| 637 | fn writeAssembly(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 643 | fn writeAssembly(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 638 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 644 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 639 | const extra = w.air.extraData(Air.Asm, ty_pl.payload); | 645 | const extra = w.air.extraData(Air.Asm, ty_pl.payload); |
| 640 | const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0; | 646 | const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0; |
| ... | @@ -704,22 +710,22 @@ const Writer = struct { | ... | @@ -704,22 +710,22 @@ const Writer = struct { |
| 704 | } | 710 | } |
| 705 | } | 711 | } |
| 706 | const asm_source = std.mem.sliceAsBytes(w.air.extra.items[extra_i..])[0..extra.data.source_len]; | 712 | const asm_source = std.mem.sliceAsBytes(w.air.extra.items[extra_i..])[0..extra.data.source_len]; |
| 707 | try s.print(", \"{f}\"", .{std.zig.fmtString(asm_source)}); | 713 | try s.print(", \"{f}\"", .{std.zig.fmtEscapes(asm_source)}); |
| 708 | } | 714 | } |
| 709 | 715 | ||
| 710 | fn writeDbgStmt(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 716 | fn writeDbgStmt(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 711 | const dbg_stmt = w.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; | 717 | const dbg_stmt = w.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; |
| 712 | try s.print("{d}:{d}", .{ dbg_stmt.line + 1, dbg_stmt.column + 1 }); | 718 | try s.print("{d}:{d}", .{ dbg_stmt.line + 1, dbg_stmt.column + 1 }); |
| 713 | } | 719 | } |
| 714 | 720 | ||
| 715 | fn writeDbgVar(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 721 | fn writeDbgVar(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 716 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 722 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 717 | try w.writeOperand(s, inst, 0, pl_op.operand); | 723 | try w.writeOperand(s, inst, 0, pl_op.operand); |
| 718 | const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload); | 724 | const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload); |
| 719 | try s.print(", \"{f}\"", .{std.zig.fmtString(name.toSlice(w.air))}); | 725 | try s.print(", \"{f}\"", .{std.zig.fmtEscapes(name.toSlice(w.air))}); |
| 720 | } | 726 | } |
| 721 | 727 | ||
| 722 | fn writeCall(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 728 | fn writeCall(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 723 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 729 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 724 | const extra = w.air.extraData(Air.Call, pl_op.payload); | 730 | const extra = w.air.extraData(Air.Call, pl_op.payload); |
| 725 | const args = @as([]const Air.Inst.Ref, @ptrCast(w.air.extra.items[extra.end..][0..extra.data.args_len])); | 731 | const args = @as([]const Air.Inst.Ref, @ptrCast(w.air.extra.items[extra.end..][0..extra.data.args_len])); |
| ... | @@ -732,19 +738,19 @@ const Writer = struct { | ... | @@ -732,19 +738,19 @@ const Writer = struct { |
| 732 | try s.writeAll("]"); | 738 | try s.writeAll("]"); |
| 733 | } | 739 | } |
| 734 | 740 | ||
| 735 | fn writeBr(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 741 | fn writeBr(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 736 | const br = w.air.instructions.items(.data)[@intFromEnum(inst)].br; | 742 | const br = w.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 737 | try w.writeInstIndex(s, br.block_inst, false); | 743 | try w.writeInstIndex(s, br.block_inst, false); |
| 738 | try s.writeAll(", "); | 744 | try s.writeAll(", "); |
| 739 | try w.writeOperand(s, inst, 0, br.operand); | 745 | try w.writeOperand(s, inst, 0, br.operand); |
| 740 | } | 746 | } |
| 741 | 747 | ||
| 742 | fn writeRepeat(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 748 | fn writeRepeat(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 743 | const repeat = w.air.instructions.items(.data)[@intFromEnum(inst)].repeat; | 749 | const repeat = w.air.instructions.items(.data)[@intFromEnum(inst)].repeat; |
| 744 | try w.writeInstIndex(s, repeat.loop_inst, false); | 750 | try w.writeInstIndex(s, repeat.loop_inst, false); |
| 745 | } | 751 | } |
| 746 | 752 | ||
| 747 | fn writeTry(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 753 | fn writeTry(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 748 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 754 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 749 | const extra = w.air.extraData(Air.Try, pl_op.payload); | 755 | const extra = w.air.extraData(Air.Try, pl_op.payload); |
| 750 | const body: []const Air.Inst.Index = @ptrCast(w.air.extra.items[extra.end..][0..extra.data.body_len]); | 756 | const body: []const Air.Inst.Index = @ptrCast(w.air.extra.items[extra.end..][0..extra.data.body_len]); |
| ... | @@ -760,25 +766,25 @@ const Writer = struct { | ... | @@ -760,25 +766,25 @@ const Writer = struct { |
| 760 | w.indent += 2; | 766 | w.indent += 2; |
| 761 | 767 | ||
| 762 | if (liveness_condbr.else_deaths.len != 0) { | 768 | if (liveness_condbr.else_deaths.len != 0) { |
| 763 | try s.writeByteNTimes(' ', w.indent); | 769 | try s.splatByteAll(' ', w.indent); |
| 764 | for (liveness_condbr.else_deaths, 0..) |operand, i| { | 770 | for (liveness_condbr.else_deaths, 0..) |operand, i| { |
| 765 | if (i != 0) try s.writeAll(" "); | 771 | if (i != 0) try s.writeAll(" "); |
| 766 | try s.print("{}!", .{operand}); | 772 | try s.print("{f}!", .{operand}); |
| 767 | } | 773 | } |
| 768 | try s.writeAll("\n"); | 774 | try s.writeAll("\n"); |
| 769 | } | 775 | } |
| 770 | try w.writeBody(s, body); | 776 | try w.writeBody(s, body); |
| 771 | 777 | ||
| 772 | w.indent = old_indent; | 778 | w.indent = old_indent; |
| 773 | try s.writeByteNTimes(' ', w.indent); | 779 | try s.splatByteAll(' ', w.indent); |
| 774 | try s.writeAll("}"); | 780 | try s.writeAll("}"); |
| 775 | 781 | ||
| 776 | for (liveness_condbr.then_deaths) |operand| { | 782 | for (liveness_condbr.then_deaths) |operand| { |
| 777 | try s.print(" {}!", .{operand}); | 783 | try s.print(" {f}!", .{operand}); |
| 778 | } | 784 | } |
| 779 | } | 785 | } |
| 780 | 786 | ||
| 781 | fn writeTryPtr(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 787 | fn writeTryPtr(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 782 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 788 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 783 | const extra = w.air.extraData(Air.TryPtr, ty_pl.payload); | 789 | const extra = w.air.extraData(Air.TryPtr, ty_pl.payload); |
| 784 | const body: []const Air.Inst.Index = @ptrCast(w.air.extra.items[extra.end..][0..extra.data.body_len]); | 790 | const body: []const Air.Inst.Index = @ptrCast(w.air.extra.items[extra.end..][0..extra.data.body_len]); |
| ... | @@ -797,25 +803,25 @@ const Writer = struct { | ... | @@ -797,25 +803,25 @@ const Writer = struct { |
| 797 | w.indent += 2; | 803 | w.indent += 2; |
| 798 | 804 | ||
| 799 | if (liveness_condbr.else_deaths.len != 0) { | 805 | if (liveness_condbr.else_deaths.len != 0) { |
| 800 | try s.writeByteNTimes(' ', w.indent); | 806 | try s.splatByteAll(' ', w.indent); |
| 801 | for (liveness_condbr.else_deaths, 0..) |operand, i| { | 807 | for (liveness_condbr.else_deaths, 0..) |operand, i| { |
| 802 | if (i != 0) try s.writeAll(" "); | 808 | if (i != 0) try s.writeAll(" "); |
| 803 | try s.print("{}!", .{operand}); | 809 | try s.print("{f}!", .{operand}); |
| 804 | } | 810 | } |
| 805 | try s.writeAll("\n"); | 811 | try s.writeAll("\n"); |
| 806 | } | 812 | } |
| 807 | try w.writeBody(s, body); | 813 | try w.writeBody(s, body); |
| 808 | 814 | ||
| 809 | w.indent = old_indent; | 815 | w.indent = old_indent; |
| 810 | try s.writeByteNTimes(' ', w.indent); | 816 | try s.splatByteAll(' ', w.indent); |
| 811 | try s.writeAll("}"); | 817 | try s.writeAll("}"); |
| 812 | 818 | ||
| 813 | for (liveness_condbr.then_deaths) |operand| { | 819 | for (liveness_condbr.then_deaths) |operand| { |
| 814 | try s.print(" {}!", .{operand}); | 820 | try s.print(" {f}!", .{operand}); |
| 815 | } | 821 | } |
| 816 | } | 822 | } |
| 817 | 823 | ||
| 818 | fn writeCondBr(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 824 | fn writeCondBr(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 819 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 825 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 820 | const extra = w.air.extraData(Air.CondBr, pl_op.payload); | 826 | const extra = w.air.extraData(Air.CondBr, pl_op.payload); |
| 821 | const then_body: []const Air.Inst.Index = @ptrCast(w.air.extra.items[extra.end..][0..extra.data.then_body_len]); | 827 | const then_body: []const Air.Inst.Index = @ptrCast(w.air.extra.items[extra.end..][0..extra.data.then_body_len]); |
| ... | @@ -839,16 +845,16 @@ const Writer = struct { | ... | @@ -839,16 +845,16 @@ const Writer = struct { |
| 839 | w.indent += 2; | 845 | w.indent += 2; |
| 840 | 846 | ||
| 841 | if (liveness_condbr.then_deaths.len != 0) { | 847 | if (liveness_condbr.then_deaths.len != 0) { |
| 842 | try s.writeByteNTimes(' ', w.indent); | 848 | try s.splatByteAll(' ', w.indent); |
| 843 | for (liveness_condbr.then_deaths, 0..) |operand, i| { | 849 | for (liveness_condbr.then_deaths, 0..) |operand, i| { |
| 844 | if (i != 0) try s.writeAll(" "); | 850 | if (i != 0) try s.writeAll(" "); |
| 845 | try s.print("{}!", .{operand}); | 851 | try s.print("{f}!", .{operand}); |
| 846 | } | 852 | } |
| 847 | try s.writeAll("\n"); | 853 | try s.writeAll("\n"); |
| 848 | } | 854 | } |
| 849 | 855 | ||
| 850 | try w.writeBody(s, then_body); | 856 | try w.writeBody(s, then_body); |
| 851 | try s.writeByteNTimes(' ', old_indent); | 857 | try s.splatByteAll(' ', old_indent); |
| 852 | try s.writeAll("},"); | 858 | try s.writeAll("},"); |
| 853 | if (extra.data.branch_hints.false != .none) { | 859 | if (extra.data.branch_hints.false != .none) { |
| 854 | try s.print(" {s}", .{@tagName(extra.data.branch_hints.false)}); | 860 | try s.print(" {s}", .{@tagName(extra.data.branch_hints.false)}); |
| ... | @@ -859,10 +865,10 @@ const Writer = struct { | ... | @@ -859,10 +865,10 @@ const Writer = struct { |
| 859 | try s.writeAll(" {\n"); | 865 | try s.writeAll(" {\n"); |
| 860 | 866 | ||
| 861 | if (liveness_condbr.else_deaths.len != 0) { | 867 | if (liveness_condbr.else_deaths.len != 0) { |
| 862 | try s.writeByteNTimes(' ', w.indent); | 868 | try s.splatByteAll(' ', w.indent); |
| 863 | for (liveness_condbr.else_deaths, 0..) |operand, i| { | 869 | for (liveness_condbr.else_deaths, 0..) |operand, i| { |
| 864 | if (i != 0) try s.writeAll(" "); | 870 | if (i != 0) try s.writeAll(" "); |
| 865 | try s.print("{}!", .{operand}); | 871 | try s.print("{f}!", .{operand}); |
| 866 | } | 872 | } |
| 867 | try s.writeAll("\n"); | 873 | try s.writeAll("\n"); |
| 868 | } | 874 | } |
| ... | @@ -870,11 +876,11 @@ const Writer = struct { | ... | @@ -870,11 +876,11 @@ const Writer = struct { |
| 870 | try w.writeBody(s, else_body); | 876 | try w.writeBody(s, else_body); |
| 871 | w.indent = old_indent; | 877 | w.indent = old_indent; |
| 872 | 878 | ||
| 873 | try s.writeByteNTimes(' ', old_indent); | 879 | try s.splatByteAll(' ', old_indent); |
| 874 | try s.writeAll("}"); | 880 | try s.writeAll("}"); |
| 875 | } | 881 | } |
| 876 | 882 | ||
| 877 | fn writeSwitchBr(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 883 | fn writeSwitchBr(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 878 | const switch_br = w.air.unwrapSwitch(inst); | 884 | const switch_br = w.air.unwrapSwitch(inst); |
| 879 | 885 | ||
| 880 | const liveness: Air.Liveness.SwitchBrTable = if (w.liveness) |liveness| | 886 | const liveness: Air.Liveness.SwitchBrTable = if (w.liveness) |liveness| |
| ... | @@ -916,17 +922,17 @@ const Writer = struct { | ... | @@ -916,17 +922,17 @@ const Writer = struct { |
| 916 | 922 | ||
| 917 | const deaths = liveness.deaths[case.idx]; | 923 | const deaths = liveness.deaths[case.idx]; |
| 918 | if (deaths.len != 0) { | 924 | if (deaths.len != 0) { |
| 919 | try s.writeByteNTimes(' ', w.indent); | 925 | try s.splatByteAll(' ', w.indent); |
| 920 | for (deaths, 0..) |operand, i| { | 926 | for (deaths, 0..) |operand, i| { |
| 921 | if (i != 0) try s.writeAll(" "); | 927 | if (i != 0) try s.writeAll(" "); |
| 922 | try s.print("{}!", .{operand}); | 928 | try s.print("{f}!", .{operand}); |
| 923 | } | 929 | } |
| 924 | try s.writeAll("\n"); | 930 | try s.writeAll("\n"); |
| 925 | } | 931 | } |
| 926 | 932 | ||
| 927 | try w.writeBody(s, case.body); | 933 | try w.writeBody(s, case.body); |
| 928 | w.indent -= 2; | 934 | w.indent -= 2; |
| 929 | try s.writeByteNTimes(' ', w.indent); | 935 | try s.splatByteAll(' ', w.indent); |
| 930 | try s.writeAll("}"); | 936 | try s.writeAll("}"); |
| 931 | } | 937 | } |
| 932 | 938 | ||
| ... | @@ -942,47 +948,47 @@ const Writer = struct { | ... | @@ -942,47 +948,47 @@ const Writer = struct { |
| 942 | 948 | ||
| 943 | const deaths = liveness.deaths[liveness.deaths.len - 1]; | 949 | const deaths = liveness.deaths[liveness.deaths.len - 1]; |
| 944 | if (deaths.len != 0) { | 950 | if (deaths.len != 0) { |
| 945 | try s.writeByteNTimes(' ', w.indent); | 951 | try s.splatByteAll(' ', w.indent); |
| 946 | for (deaths, 0..) |operand, i| { | 952 | for (deaths, 0..) |operand, i| { |
| 947 | if (i != 0) try s.writeAll(" "); | 953 | if (i != 0) try s.writeAll(" "); |
| 948 | try s.print("{}!", .{operand}); | 954 | try s.print("{f}!", .{operand}); |
| 949 | } | 955 | } |
| 950 | try s.writeAll("\n"); | 956 | try s.writeAll("\n"); |
| 951 | } | 957 | } |
| 952 | 958 | ||
| 953 | try w.writeBody(s, else_body); | 959 | try w.writeBody(s, else_body); |
| 954 | w.indent -= 2; | 960 | w.indent -= 2; |
| 955 | try s.writeByteNTimes(' ', w.indent); | 961 | try s.splatByteAll(' ', w.indent); |
| 956 | try s.writeAll("}"); | 962 | try s.writeAll("}"); |
| 957 | } | 963 | } |
| 958 | 964 | ||
| 959 | try s.writeAll("\n"); | 965 | try s.writeAll("\n"); |
| 960 | try s.writeByteNTimes(' ', old_indent); | 966 | try s.splatByteAll(' ', old_indent); |
| 961 | } | 967 | } |
| 962 | 968 | ||
| 963 | fn writeWasmMemorySize(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 969 | fn writeWasmMemorySize(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 964 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 970 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 965 | try s.print("{d}", .{pl_op.payload}); | 971 | try s.print("{d}", .{pl_op.payload}); |
| 966 | } | 972 | } |
| 967 | 973 | ||
| 968 | fn writeWasmMemoryGrow(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 974 | fn writeWasmMemoryGrow(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 969 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 975 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 970 | try s.print("{d}, ", .{pl_op.payload}); | 976 | try s.print("{d}, ", .{pl_op.payload}); |
| 971 | try w.writeOperand(s, inst, 0, pl_op.operand); | 977 | try w.writeOperand(s, inst, 0, pl_op.operand); |
| 972 | } | 978 | } |
| 973 | 979 | ||
| 974 | fn writeWorkDimension(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { | 980 | fn writeWorkDimension(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void { |
| 975 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 981 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 976 | try s.print("{d}", .{pl_op.payload}); | 982 | try s.print("{d}", .{pl_op.payload}); |
| 977 | } | 983 | } |
| 978 | 984 | ||
| 979 | fn writeOperand( | 985 | fn writeOperand( |
| 980 | w: *Writer, | 986 | w: *Writer, |
| 981 | s: anytype, | 987 | s: *std.io.Writer, |
| 982 | inst: Air.Inst.Index, | 988 | inst: Air.Inst.Index, |
| 983 | op_index: usize, | 989 | op_index: usize, |
| 984 | operand: Air.Inst.Ref, | 990 | operand: Air.Inst.Ref, |
| 985 | ) @TypeOf(s).Error!void { | 991 | ) Error!void { |
| 986 | const small_tomb_bits = Air.Liveness.bpi - 1; | 992 | const small_tomb_bits = Air.Liveness.bpi - 1; |
| 987 | const dies = if (w.liveness) |liveness| blk: { | 993 | const dies = if (w.liveness) |liveness| blk: { |
| 988 | if (op_index < small_tomb_bits) | 994 | if (op_index < small_tomb_bits) |
| ... | @@ -1004,16 +1010,16 @@ const Writer = struct { | ... | @@ -1004,16 +1010,16 @@ const Writer = struct { |
| 1004 | 1010 | ||
| 1005 | fn writeInstRef( | 1011 | fn writeInstRef( |
| 1006 | w: *Writer, | 1012 | w: *Writer, |
| 1007 | s: anytype, | 1013 | s: *std.io.Writer, |
| 1008 | operand: Air.Inst.Ref, | 1014 | operand: Air.Inst.Ref, |
| 1009 | dies: bool, | 1015 | dies: bool, |
| 1010 | ) @TypeOf(s).Error!void { | 1016 | ) Error!void { |
| 1011 | if (@intFromEnum(operand) < InternPool.static_len) { | 1017 | if (@intFromEnum(operand) < InternPool.static_len) { |
| 1012 | return s.print("@{}", .{operand}); | 1018 | return s.print("@{}", .{operand}); |
| 1013 | } else if (operand.toInterned()) |ip_index| { | 1019 | } else if (operand.toInterned()) |ip_index| { |
| 1014 | const pt = w.pt; | 1020 | const pt = w.pt; |
| 1015 | const ty = Type.fromInterned(pt.zcu.intern_pool.indexToKey(ip_index).typeOf()); | 1021 | const ty = Type.fromInterned(pt.zcu.intern_pool.indexToKey(ip_index).typeOf()); |
| 1016 | try s.print("<{}, {}>", .{ | 1022 | try s.print("<{f}, {f}>", .{ |
| 1017 | ty.fmt(pt), | 1023 | ty.fmt(pt), |
| 1018 | Value.fromInterned(ip_index).fmtValue(pt), | 1024 | Value.fromInterned(ip_index).fmtValue(pt), |
| 1019 | }); | 1025 | }); |
| ... | @@ -1024,12 +1030,12 @@ const Writer = struct { | ... | @@ -1024,12 +1030,12 @@ const Writer = struct { |
| 1024 | 1030 | ||
| 1025 | fn writeInstIndex( | 1031 | fn writeInstIndex( |
| 1026 | w: *Writer, | 1032 | w: *Writer, |
| 1027 | s: anytype, | 1033 | s: *std.io.Writer, |
| 1028 | inst: Air.Inst.Index, | 1034 | inst: Air.Inst.Index, |
| 1029 | dies: bool, | 1035 | dies: bool, |
| 1030 | ) @TypeOf(s).Error!void { | 1036 | ) Error!void { |
| 1031 | _ = w; | 1037 | _ = w; |
| 1032 | try s.print("{}", .{inst}); | 1038 | try s.print("{f}", .{inst}); |
| 1033 | if (dies) try s.writeByte('!'); | 1039 | if (dies) try s.writeByte('!'); |
| 1034 | } | 1040 | } |
| 1035 | 1041 |
src/Compilation.zig+2-2| ... | @@ -5852,7 +5852,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr | ... | @@ -5852,7 +5852,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr |
| 5852 | 5852 | ||
| 5853 | try child.spawn(); | 5853 | try child.spawn(); |
| 5854 | 5854 | ||
| 5855 | const stderr = try child.stderr.?.reader().readAllAlloc(arena, std.math.maxInt(usize)); | 5855 | const stderr = try child.stderr.?.deprecatedReader().readAllAlloc(arena, std.math.maxInt(usize)); |
| 5856 | 5856 | ||
| 5857 | const term = child.wait() catch |err| { | 5857 | const term = child.wait() catch |err| { |
| 5858 | return comp.failCObj(c_object, "failed to spawn zig clang {s}: {s}", .{ argv.items[0], @errorName(err) }); | 5858 | return comp.failCObj(c_object, "failed to spawn zig clang {s}: {s}", .{ argv.items[0], @errorName(err) }); |
| ... | @@ -6249,7 +6249,7 @@ fn spawnZigRc( | ... | @@ -6249,7 +6249,7 @@ fn spawnZigRc( |
| 6249 | } | 6249 | } |
| 6250 | 6250 | ||
| 6251 | // Just in case there's a failure that didn't send an ErrorBundle (e.g. an error return trace) | 6251 | // Just in case there's a failure that didn't send an ErrorBundle (e.g. an error return trace) |
| 6252 | const stderr_reader = child.stderr.?.reader(); | 6252 | const stderr_reader = child.stderr.?.deprecatedReader(); |
| 6253 | const stderr = try stderr_reader.readAllAlloc(arena, 10 * 1024 * 1024); | 6253 | const stderr = try stderr_reader.readAllAlloc(arena, 10 * 1024 * 1024); |
| 6254 | 6254 | ||
| 6255 | const term = child.wait() catch |err| { | 6255 | const term = child.wait() catch |err| { |
src/Sema.zig+5-5| ... | @@ -3026,8 +3026,8 @@ pub fn createTypeName( | ... | @@ -3026,8 +3026,8 @@ pub fn createTypeName( |
| 3026 | 3026 | ||
| 3027 | var aw: std.io.Writer.Allocating = .init(gpa); | 3027 | var aw: std.io.Writer.Allocating = .init(gpa); |
| 3028 | defer aw.deinit(); | 3028 | defer aw.deinit(); |
| 3029 | const bw = &aw.writer; | 3029 | const w = &aw.writer; |
| 3030 | bw.print("{f}(", .{block.type_name_ctx.fmt(ip)}) catch return error.OutOfMemory; | 3030 | w.print("{f}(", .{block.type_name_ctx.fmt(ip)}) catch return error.OutOfMemory; |
| 3031 | 3031 | ||
| 3032 | var arg_i: usize = 0; | 3032 | var arg_i: usize = 0; |
| 3033 | for (fn_info.param_body) |zir_inst| switch (zir_tags[@intFromEnum(zir_inst)]) { | 3033 | for (fn_info.param_body) |zir_inst| switch (zir_tags[@intFromEnum(zir_inst)]) { |
| ... | @@ -3040,13 +3040,13 @@ pub fn createTypeName( | ... | @@ -3040,13 +3040,13 @@ pub fn createTypeName( |
| 3040 | // result in a compile error. | 3040 | // result in a compile error. |
| 3041 | const arg_val = try sema.resolveValue(arg) orelse break :func_strat; // fall through to anon strat | 3041 | const arg_val = try sema.resolveValue(arg) orelse break :func_strat; // fall through to anon strat |
| 3042 | 3042 | ||
| 3043 | if (arg_i != 0) bw.writeByte(',') catch return error.OutOfMemory; | 3043 | if (arg_i != 0) w.writeByte(',') catch return error.OutOfMemory; |
| 3044 | 3044 | ||
| 3045 | // Limiting the depth here helps avoid type names getting too long, which | 3045 | // Limiting the depth here helps avoid type names getting too long, which |
| 3046 | // in turn helps to avoid unreasonably long symbol names for namespaced | 3046 | // in turn helps to avoid unreasonably long symbol names for namespaced |
| 3047 | // symbols. Such names should ideally be human-readable, and additionally, | 3047 | // symbols. Such names should ideally be human-readable, and additionally, |
| 3048 | // some tooling may not support very long symbol names. | 3048 | // some tooling may not support very long symbol names. |
| 3049 | bw.print("{f}", .{Value.fmtValueSemaFull(.{ | 3049 | w.print("{f}", .{Value.fmtValueSemaFull(.{ |
| 3050 | .val = arg_val, | 3050 | .val = arg_val, |
| 3051 | .pt = pt, | 3051 | .pt = pt, |
| 3052 | .opt_sema = sema, | 3052 | .opt_sema = sema, |
| ... | @@ -3059,7 +3059,7 @@ pub fn createTypeName( | ... | @@ -3059,7 +3059,7 @@ pub fn createTypeName( |
| 3059 | else => continue, | 3059 | else => continue, |
| 3060 | }; | 3060 | }; |
| 3061 | 3061 | ||
| 3062 | try bw.writeByte(')'); | 3062 | w.writeByte(')') catch return error.OutOfMemory; |
| 3063 | return .{ | 3063 | return .{ |
| 3064 | .name = try ip.getOrPutString(gpa, pt.tid, aw.getWritten(), .no_embedded_nulls), | 3064 | .name = try ip.getOrPutString(gpa, pt.tid, aw.getWritten(), .no_embedded_nulls), |
| 3065 | .nav = .none, | 3065 | .nav = .none, |
src/arch/x86_64/CodeGen.zig+2-7| ... | @@ -1135,15 +1135,10 @@ fn formatWipMir(data: FormatWipMirData, w: *Writer) Writer.Error!void { | ... | @@ -1135,15 +1135,10 @@ fn formatWipMir(data: FormatWipMirData, w: *Writer) Writer.Error!void { |
| 1135 | try w.writeAll(lower.err_msg.?.msg); | 1135 | try w.writeAll(lower.err_msg.?.msg); |
| 1136 | return; | 1136 | return; |
| 1137 | }, | 1137 | }, |
| 1138 | error.OutOfMemory, error.InvalidInstruction, error.CannotEncode => |e| { | 1138 | else => |e| { |
| 1139 | try w.writeAll(switch (e) { | 1139 | try w.writeAll(@errorName(e)); |
| 1140 | error.OutOfMemory => "Out of memory", | ||
| 1141 | error.InvalidInstruction => "CodeGen failed to find a viable instruction.", | ||
| 1142 | error.CannotEncode => "CodeGen failed to encode the instruction.", | ||
| 1143 | }); | ||
| 1144 | return; | 1140 | return; |
| 1145 | }, | 1141 | }, |
| 1146 | else => |e| return e, | ||
| 1147 | }).insts) |lowered_inst| { | 1142 | }).insts) |lowered_inst| { |
| 1148 | if (!first) try w.writeAll("\ndebug(wip_mir): "); | 1143 | if (!first) try w.writeAll("\ndebug(wip_mir): "); |
| 1149 | try w.print(" | {f}", .{lowered_inst}); | 1144 | try w.print(" | {f}", .{lowered_inst}); |
src/arch/x86_64/encoder.zig+110-136| ... | @@ -3,6 +3,7 @@ const assert = std.debug.assert; | ... | @@ -3,6 +3,7 @@ const assert = std.debug.assert; |
| 3 | const log = std.log.scoped(.x86_64_encoder); | 3 | const log = std.log.scoped(.x86_64_encoder); |
| 4 | const math = std.math; | 4 | const math = std.math; |
| 5 | const testing = std.testing; | 5 | const testing = std.testing; |
| 6 | const Writer = std.io.Writer; | ||
| 6 | 7 | ||
| 7 | const bits = @import("bits.zig"); | 8 | const bits = @import("bits.zig"); |
| 8 | const Encoding = @import("Encoding.zig"); | 9 | const Encoding = @import("Encoding.zig"); |
| ... | @@ -226,101 +227,81 @@ pub const Instruction = struct { | ... | @@ -226,101 +227,81 @@ pub const Instruction = struct { |
| 226 | }; | 227 | }; |
| 227 | } | 228 | } |
| 228 | 229 | ||
| 229 | fn format( | 230 | const Format = struct { |
| 230 | op: Operand, | ||
| 231 | comptime unused_format_string: []const u8, | ||
| 232 | options: std.fmt.FormatOptions, | ||
| 233 | writer: anytype, | ||
| 234 | ) !void { | ||
| 235 | _ = op; | ||
| 236 | _ = unused_format_string; | ||
| 237 | _ = options; | ||
| 238 | _ = writer; | ||
| 239 | @compileError("do not format Operand directly; use fmt() instead"); | ||
| 240 | } | ||
| 241 | |||
| 242 | const FormatContext = struct { | ||
| 243 | op: Operand, | 231 | op: Operand, |
| 244 | enc_op: Encoding.Op, | 232 | enc_op: Encoding.Op, |
| 245 | }; | ||
| 246 | 233 | ||
| 247 | fn fmtContext( | 234 | fn default(f: Format, w: *Writer) Writer.Error!void { |
| 248 | ctx: FormatContext, | 235 | const op = f.op; |
| 249 | comptime unused_format_string: []const u8, | 236 | const enc_op = f.enc_op; |
| 250 | options: std.fmt.FormatOptions, | 237 | switch (op) { |
| 251 | writer: anytype, | 238 | .none => {}, |
| 252 | ) @TypeOf(writer).Error!void { | 239 | .reg => |reg| try w.writeAll(@tagName(reg)), |
| 253 | _ = unused_format_string; | 240 | .mem => |mem| switch (mem) { |
| 254 | _ = options; | 241 | .rip => |rip| { |
| 255 | const op = ctx.op; | 242 | try w.print("{f} [rip", .{rip.ptr_size}); |
| 256 | const enc_op = ctx.enc_op; | 243 | if (rip.disp != 0) try w.print(" {c} 0x{x}", .{ |
| 257 | switch (op) { | 244 | @as(u8, if (rip.disp < 0) '-' else '+'), |
| 258 | .none => {}, | 245 | @abs(rip.disp), |
| 259 | .reg => |reg| try writer.writeAll(@tagName(reg)), | 246 | }); |
| 260 | .mem => |mem| switch (mem) { | 247 | try w.writeByte(']'); |
| 261 | .rip => |rip| { | 248 | }, |
| 262 | try writer.print("{} [rip", .{rip.ptr_size}); | 249 | .sib => |sib| { |
| 263 | if (rip.disp != 0) try writer.print(" {c} 0x{x}", .{ | 250 | try w.print("{f} ", .{sib.ptr_size}); |
| 264 | @as(u8, if (rip.disp < 0) '-' else '+'), | ||
| 265 | @abs(rip.disp), | ||
| 266 | }); | ||
| 267 | try writer.writeByte(']'); | ||
| 268 | }, | ||
| 269 | .sib => |sib| { | ||
| 270 | try writer.print("{} ", .{sib.ptr_size}); | ||
| 271 | 251 | ||
| 272 | if (mem.isSegmentRegister()) { | 252 | if (mem.isSegmentRegister()) { |
| 273 | return writer.print("{s}:0x{x}", .{ @tagName(sib.base.reg), sib.disp }); | 253 | return w.print("{s}:0x{x}", .{ @tagName(sib.base.reg), sib.disp }); |
| 274 | } | 254 | } |
| 275 | 255 | ||
| 276 | try writer.writeByte('['); | 256 | try w.writeByte('['); |
| 277 | 257 | ||
| 278 | var any = true; | 258 | var any = true; |
| 279 | switch (sib.base) { | 259 | switch (sib.base) { |
| 280 | .none => any = false, | 260 | .none => any = false, |
| 281 | .reg => |reg| try writer.print("{s}", .{@tagName(reg)}), | 261 | .reg => |reg| try w.print("{s}", .{@tagName(reg)}), |
| 282 | .frame => |frame_index| try writer.print("{}", .{frame_index}), | 262 | .frame => |frame_index| try w.print("{}", .{frame_index}), |
| 283 | .table => try writer.print("Table", .{}), | 263 | .table => try w.print("Table", .{}), |
| 284 | .rip_inst => |inst_index| try writer.print("RipInst({d})", .{inst_index}), | 264 | .rip_inst => |inst_index| try w.print("RipInst({d})", .{inst_index}), |
| 285 | .nav => |nav| try writer.print("Nav({d})", .{@intFromEnum(nav)}), | 265 | .nav => |nav| try w.print("Nav({d})", .{@intFromEnum(nav)}), |
| 286 | .uav => |uav| try writer.print("Uav({d})", .{@intFromEnum(uav.val)}), | 266 | .uav => |uav| try w.print("Uav({d})", .{@intFromEnum(uav.val)}), |
| 287 | .lazy_sym => |lazy_sym| try writer.print("LazySym({s}, {d})", .{ | 267 | .lazy_sym => |lazy_sym| try w.print("LazySym({s}, {d})", .{ |
| 288 | @tagName(lazy_sym.kind), | 268 | @tagName(lazy_sym.kind), |
| 289 | @intFromEnum(lazy_sym.ty), | 269 | @intFromEnum(lazy_sym.ty), |
| 290 | }), | 270 | }), |
| 291 | .extern_func => |extern_func| try writer.print("ExternFunc({d})", .{@intFromEnum(extern_func)}), | 271 | .extern_func => |extern_func| try w.print("ExternFunc({d})", .{@intFromEnum(extern_func)}), |
| 292 | } | 272 | } |
| 293 | if (mem.scaleIndex()) |si| { | 273 | if (mem.scaleIndex()) |si| { |
| 294 | if (any) try writer.writeAll(" + "); | 274 | if (any) try w.writeAll(" + "); |
| 295 | try writer.print("{s} * {d}", .{ @tagName(si.index), si.scale }); | 275 | try w.print("{s} * {d}", .{ @tagName(si.index), si.scale }); |
| 296 | any = true; | 276 | any = true; |
| 297 | } | 277 | } |
| 298 | if (sib.disp != 0 or !any) { | 278 | if (sib.disp != 0 or !any) { |
| 299 | if (any) | 279 | if (any) |
| 300 | try writer.print(" {c} ", .{@as(u8, if (sib.disp < 0) '-' else '+')}) | 280 | try w.print(" {c} ", .{@as(u8, if (sib.disp < 0) '-' else '+')}) |
| 301 | else if (sib.disp < 0) | 281 | else if (sib.disp < 0) |
| 302 | try writer.writeByte('-'); | 282 | try w.writeByte('-'); |
| 303 | try writer.print("0x{x}", .{@abs(sib.disp)}); | 283 | try w.print("0x{x}", .{@abs(sib.disp)}); |
| 304 | any = true; | 284 | any = true; |
| 305 | } | 285 | } |
| 306 | 286 | ||
| 307 | try writer.writeByte(']'); | 287 | try w.writeByte(']'); |
| 288 | }, | ||
| 289 | .moffs => |moffs| try w.print("{s}:0x{x}", .{ | ||
| 290 | @tagName(moffs.seg), | ||
| 291 | moffs.offset, | ||
| 292 | }), | ||
| 308 | }, | 293 | }, |
| 309 | .moffs => |moffs| try writer.print("{s}:0x{x}", .{ | 294 | .imm => |imm| if (enc_op.isSigned()) { |
| 310 | @tagName(moffs.seg), | 295 | const imms = imm.asSigned(enc_op.immBitSize()); |
| 311 | moffs.offset, | 296 | if (imms < 0) try w.writeByte('-'); |
| 312 | }), | 297 | try w.print("0x{x}", .{@abs(imms)}); |
| 313 | }, | 298 | } else try w.print("0x{x}", .{imm.asUnsigned(enc_op.immBitSize())}), |
| 314 | .imm => |imm| if (enc_op.isSigned()) { | 299 | .bytes => unreachable, |
| 315 | const imms = imm.asSigned(enc_op.immBitSize()); | 300 | } |
| 316 | if (imms < 0) try writer.writeByte('-'); | ||
| 317 | try writer.print("0x{x}", .{@abs(imms)}); | ||
| 318 | } else try writer.print("0x{x}", .{imm.asUnsigned(enc_op.immBitSize())}), | ||
| 319 | .bytes => unreachable, | ||
| 320 | } | 301 | } |
| 321 | } | 302 | }; |
| 322 | 303 | ||
| 323 | pub fn fmt(op: Operand, enc_op: Encoding.Op) std.fmt.Formatter(fmtContext) { | 304 | pub fn fmt(op: Operand, enc_op: Encoding.Op) std.fmt.Formatter(Format, Format.default) { |
| 324 | return .{ .data = .{ .op = op, .enc_op = enc_op } }; | 305 | return .{ .data = .{ .op = op, .enc_op = enc_op } }; |
| 325 | } | 306 | } |
| 326 | }; | 307 | }; |
| ... | @@ -361,7 +342,7 @@ pub const Instruction = struct { | ... | @@ -361,7 +342,7 @@ pub const Instruction = struct { |
| 361 | }, | 342 | }, |
| 362 | }, | 343 | }, |
| 363 | }; | 344 | }; |
| 364 | log.debug("selected encoding: {}", .{encoding}); | 345 | log.debug("selected encoding: {f}", .{encoding}); |
| 365 | 346 | ||
| 366 | var inst: Instruction = .{ | 347 | var inst: Instruction = .{ |
| 367 | .prefix = prefix, | 348 | .prefix = prefix, |
| ... | @@ -372,30 +353,23 @@ pub const Instruction = struct { | ... | @@ -372,30 +353,23 @@ pub const Instruction = struct { |
| 372 | return inst; | 353 | return inst; |
| 373 | } | 354 | } |
| 374 | 355 | ||
| 375 | pub fn format( | 356 | pub fn format(inst: Instruction, w: *Writer, comptime unused_format_string: []const u8) Writer.Error!void { |
| 376 | inst: Instruction, | 357 | comptime assert(unused_format_string.len == 0); |
| 377 | comptime unused_format_string: []const u8, | ||
| 378 | options: std.fmt.FormatOptions, | ||
| 379 | writer: anytype, | ||
| 380 | ) @TypeOf(writer).Error!void { | ||
| 381 | _ = unused_format_string; | ||
| 382 | _ = options; | ||
| 383 | switch (inst.prefix) { | 358 | switch (inst.prefix) { |
| 384 | .none, .directive => {}, | 359 | .none, .directive => {}, |
| 385 | else => try writer.print("{s} ", .{@tagName(inst.prefix)}), | 360 | else => try w.print("{s} ", .{@tagName(inst.prefix)}), |
| 386 | } | 361 | } |
| 387 | try writer.print("{s}", .{@tagName(inst.encoding.mnemonic)}); | 362 | try w.print("{s}", .{@tagName(inst.encoding.mnemonic)}); |
| 388 | for (inst.ops, inst.encoding.data.ops, 0..) |op, enc, i| { | 363 | for (inst.ops, inst.encoding.data.ops, 0..) |op, enc, i| { |
| 389 | if (op == .none) break; | 364 | if (op == .none) break; |
| 390 | if (i > 0) try writer.writeByte(','); | 365 | if (i > 0) try w.writeByte(','); |
| 391 | try writer.writeByte(' '); | 366 | try w.print(" {f}", .{op.fmt(enc)}); |
| 392 | try writer.print("{}", .{op.fmt(enc)}); | ||
| 393 | } | 367 | } |
| 394 | } | 368 | } |
| 395 | 369 | ||
| 396 | pub fn encode(inst: Instruction, writer: anytype, comptime opts: Options) !void { | 370 | pub fn encode(inst: Instruction, w: *Writer, comptime opts: Options) !void { |
| 397 | assert(inst.prefix != .directive); | 371 | assert(inst.prefix != .directive); |
| 398 | const encoder = Encoder(@TypeOf(writer), opts){ .writer = writer }; | 372 | const encoder: Encoder(opts) = .{ .w = w }; |
| 399 | const enc = inst.encoding; | 373 | const enc = inst.encoding; |
| 400 | const data = enc.data; | 374 | const data = enc.data; |
| 401 | 375 | ||
| ... | @@ -801,9 +775,9 @@ pub const LegacyPrefixes = packed struct { | ... | @@ -801,9 +775,9 @@ pub const LegacyPrefixes = packed struct { |
| 801 | 775 | ||
| 802 | pub const Options = struct { allow_frame_locs: bool = false, allow_symbols: bool = false }; | 776 | pub const Options = struct { allow_frame_locs: bool = false, allow_symbols: bool = false }; |
| 803 | 777 | ||
| 804 | fn Encoder(comptime T: type, comptime opts: Options) type { | 778 | fn Encoder(comptime opts: Options) type { |
| 805 | return struct { | 779 | return struct { |
| 806 | writer: T, | 780 | w: *Writer, |
| 807 | 781 | ||
| 808 | const Self = @This(); | 782 | const Self = @This(); |
| 809 | pub const options = opts; | 783 | pub const options = opts; |
| ... | @@ -818,31 +792,31 @@ fn Encoder(comptime T: type, comptime opts: Options) type { | ... | @@ -818,31 +792,31 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 818 | // Hopefully this path isn't taken very often, so we'll do it the slow way for now | 792 | // Hopefully this path isn't taken very often, so we'll do it the slow way for now |
| 819 | 793 | ||
| 820 | // LOCK | 794 | // LOCK |
| 821 | if (prefixes.prefix_f0) try self.writer.writeByte(0xf0); | 795 | if (prefixes.prefix_f0) try self.w.writeByte(0xf0); |
| 822 | // REPNZ, REPNE, REP, Scalar Double-precision | 796 | // REPNZ, REPNE, REP, Scalar Double-precision |
| 823 | if (prefixes.prefix_f2) try self.writer.writeByte(0xf2); | 797 | if (prefixes.prefix_f2) try self.w.writeByte(0xf2); |
| 824 | // REPZ, REPE, REP, Scalar Single-precision | 798 | // REPZ, REPE, REP, Scalar Single-precision |
| 825 | if (prefixes.prefix_f3) try self.writer.writeByte(0xf3); | 799 | if (prefixes.prefix_f3) try self.w.writeByte(0xf3); |
| 826 | 800 | ||
| 827 | // CS segment override or Branch not taken | 801 | // CS segment override or Branch not taken |
| 828 | if (prefixes.prefix_2e) try self.writer.writeByte(0x2e); | 802 | if (prefixes.prefix_2e) try self.w.writeByte(0x2e); |
| 829 | // DS segment override | 803 | // DS segment override |
| 830 | if (prefixes.prefix_36) try self.writer.writeByte(0x36); | 804 | if (prefixes.prefix_36) try self.w.writeByte(0x36); |
| 831 | // ES segment override | 805 | // ES segment override |
| 832 | if (prefixes.prefix_26) try self.writer.writeByte(0x26); | 806 | if (prefixes.prefix_26) try self.w.writeByte(0x26); |
| 833 | // FS segment override | 807 | // FS segment override |
| 834 | if (prefixes.prefix_64) try self.writer.writeByte(0x64); | 808 | if (prefixes.prefix_64) try self.w.writeByte(0x64); |
| 835 | // GS segment override | 809 | // GS segment override |
| 836 | if (prefixes.prefix_65) try self.writer.writeByte(0x65); | 810 | if (prefixes.prefix_65) try self.w.writeByte(0x65); |
| 837 | 811 | ||
| 838 | // Branch taken | 812 | // Branch taken |
| 839 | if (prefixes.prefix_3e) try self.writer.writeByte(0x3e); | 813 | if (prefixes.prefix_3e) try self.w.writeByte(0x3e); |
| 840 | 814 | ||
| 841 | // Operand size override | 815 | // Operand size override |
| 842 | if (prefixes.prefix_66) try self.writer.writeByte(0x66); | 816 | if (prefixes.prefix_66) try self.w.writeByte(0x66); |
| 843 | 817 | ||
| 844 | // Address size override | 818 | // Address size override |
| 845 | if (prefixes.prefix_67) try self.writer.writeByte(0x67); | 819 | if (prefixes.prefix_67) try self.w.writeByte(0x67); |
| 846 | } | 820 | } |
| 847 | } | 821 | } |
| 848 | 822 | ||
| ... | @@ -850,7 +824,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { | ... | @@ -850,7 +824,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 850 | /// | 824 | /// |
| 851 | /// Note that this flag is overridden by REX.W, if both are present. | 825 | /// Note that this flag is overridden by REX.W, if both are present. |
| 852 | pub fn prefix16BitMode(self: Self) !void { | 826 | pub fn prefix16BitMode(self: Self) !void { |
| 853 | try self.writer.writeByte(0x66); | 827 | try self.w.writeByte(0x66); |
| 854 | } | 828 | } |
| 855 | 829 | ||
| 856 | /// Encodes a REX prefix byte given all the fields | 830 | /// Encodes a REX prefix byte given all the fields |
| ... | @@ -869,7 +843,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { | ... | @@ -869,7 +843,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 869 | if (fields.x) byte |= 0b0010; | 843 | if (fields.x) byte |= 0b0010; |
| 870 | if (fields.b) byte |= 0b0001; | 844 | if (fields.b) byte |= 0b0001; |
| 871 | 845 | ||
| 872 | try self.writer.writeByte(byte); | 846 | try self.w.writeByte(byte); |
| 873 | } | 847 | } |
| 874 | 848 | ||
| 875 | /// Encodes a VEX prefix given all the fields | 849 | /// Encodes a VEX prefix given all the fields |
| ... | @@ -877,24 +851,24 @@ fn Encoder(comptime T: type, comptime opts: Options) type { | ... | @@ -877,24 +851,24 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 877 | /// See struct `Vex` for a description of each field. | 851 | /// See struct `Vex` for a description of each field. |
| 878 | pub fn vex(self: Self, fields: Vex) !void { | 852 | pub fn vex(self: Self, fields: Vex) !void { |
| 879 | if (fields.is3Byte()) { | 853 | if (fields.is3Byte()) { |
| 880 | try self.writer.writeByte(0b1100_0100); | 854 | try self.w.writeByte(0b1100_0100); |
| 881 | 855 | ||
| 882 | try self.writer.writeByte( | 856 | try self.w.writeByte( |
| 883 | @as(u8, ~@intFromBool(fields.r)) << 7 | | 857 | @as(u8, ~@intFromBool(fields.r)) << 7 | |
| 884 | @as(u8, ~@intFromBool(fields.x)) << 6 | | 858 | @as(u8, ~@intFromBool(fields.x)) << 6 | |
| 885 | @as(u8, ~@intFromBool(fields.b)) << 5 | | 859 | @as(u8, ~@intFromBool(fields.b)) << 5 | |
| 886 | @as(u8, @intFromEnum(fields.m)) << 0, | 860 | @as(u8, @intFromEnum(fields.m)) << 0, |
| 887 | ); | 861 | ); |
| 888 | 862 | ||
| 889 | try self.writer.writeByte( | 863 | try self.w.writeByte( |
| 890 | @as(u8, @intFromBool(fields.w)) << 7 | | 864 | @as(u8, @intFromBool(fields.w)) << 7 | |
| 891 | @as(u8, ~@as(u4, @intCast(fields.v.enc()))) << 3 | | 865 | @as(u8, ~@as(u4, @intCast(fields.v.enc()))) << 3 | |
| 892 | @as(u8, @intFromBool(fields.l)) << 2 | | 866 | @as(u8, @intFromBool(fields.l)) << 2 | |
| 893 | @as(u8, @intFromEnum(fields.p)) << 0, | 867 | @as(u8, @intFromEnum(fields.p)) << 0, |
| 894 | ); | 868 | ); |
| 895 | } else { | 869 | } else { |
| 896 | try self.writer.writeByte(0b1100_0101); | 870 | try self.w.writeByte(0b1100_0101); |
| 897 | try self.writer.writeByte( | 871 | try self.w.writeByte( |
| 898 | @as(u8, ~@intFromBool(fields.r)) << 7 | | 872 | @as(u8, ~@intFromBool(fields.r)) << 7 | |
| 899 | @as(u8, ~@as(u4, @intCast(fields.v.enc()))) << 3 | | 873 | @as(u8, ~@as(u4, @intCast(fields.v.enc()))) << 3 | |
| 900 | @as(u8, @intFromBool(fields.l)) << 2 | | 874 | @as(u8, @intFromBool(fields.l)) << 2 | |
| ... | @@ -909,7 +883,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { | ... | @@ -909,7 +883,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 909 | 883 | ||
| 910 | /// Encodes a 1 byte opcode | 884 | /// Encodes a 1 byte opcode |
| 911 | pub fn opcode_1byte(self: Self, opcode: u8) !void { | 885 | pub fn opcode_1byte(self: Self, opcode: u8) !void { |
| 912 | try self.writer.writeByte(opcode); | 886 | try self.w.writeByte(opcode); |
| 913 | } | 887 | } |
| 914 | 888 | ||
| 915 | /// Encodes a 2 byte opcode | 889 | /// Encodes a 2 byte opcode |
| ... | @@ -918,7 +892,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { | ... | @@ -918,7 +892,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 918 | /// | 892 | /// |
| 919 | /// encoder.opcode_2byte(0x0f, 0xaf); | 893 | /// encoder.opcode_2byte(0x0f, 0xaf); |
| 920 | pub fn opcode_2byte(self: Self, prefix: u8, opcode: u8) !void { | 894 | pub fn opcode_2byte(self: Self, prefix: u8, opcode: u8) !void { |
| 921 | try self.writer.writeAll(&.{ prefix, opcode }); | 895 | try self.w.writeAll(&.{ prefix, opcode }); |
| 922 | } | 896 | } |
| 923 | 897 | ||
| 924 | /// Encodes a 3 byte opcode | 898 | /// Encodes a 3 byte opcode |
| ... | @@ -927,7 +901,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { | ... | @@ -927,7 +901,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 927 | /// | 901 | /// |
| 928 | /// encoder.opcode_3byte(0xf2, 0x0f, 0x10); | 902 | /// encoder.opcode_3byte(0xf2, 0x0f, 0x10); |
| 929 | pub fn opcode_3byte(self: Self, prefix_1: u8, prefix_2: u8, opcode: u8) !void { | 903 | pub fn opcode_3byte(self: Self, prefix_1: u8, prefix_2: u8, opcode: u8) !void { |
| 930 | try self.writer.writeAll(&.{ prefix_1, prefix_2, opcode }); | 904 | try self.w.writeAll(&.{ prefix_1, prefix_2, opcode }); |
| 931 | } | 905 | } |
| 932 | 906 | ||
| 933 | /// Encodes a 1 byte opcode with a reg field | 907 | /// Encodes a 1 byte opcode with a reg field |
| ... | @@ -935,7 +909,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { | ... | @@ -935,7 +909,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 935 | /// Remember to add a REX prefix byte if reg is extended! | 909 | /// Remember to add a REX prefix byte if reg is extended! |
| 936 | pub fn opcode_withReg(self: Self, opcode: u8, reg: u3) !void { | 910 | pub fn opcode_withReg(self: Self, opcode: u8, reg: u3) !void { |
| 937 | assert(opcode & 0b111 == 0); | 911 | assert(opcode & 0b111 == 0); |
| 938 | try self.writer.writeByte(opcode | reg); | 912 | try self.w.writeByte(opcode | reg); |
| 939 | } | 913 | } |
| 940 | 914 | ||
| 941 | // ------ | 915 | // ------ |
| ... | @@ -946,7 +920,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { | ... | @@ -946,7 +920,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 946 | /// | 920 | /// |
| 947 | /// Remember to add a REX prefix byte if reg or rm are extended! | 921 | /// Remember to add a REX prefix byte if reg or rm are extended! |
| 948 | pub fn modRm(self: Self, mod: u2, reg_or_opx: u3, rm: u3) !void { | 922 | pub fn modRm(self: Self, mod: u2, reg_or_opx: u3, rm: u3) !void { |
| 949 | try self.writer.writeByte(@as(u8, mod) << 6 | @as(u8, reg_or_opx) << 3 | rm); | 923 | try self.w.writeByte(@as(u8, mod) << 6 | @as(u8, reg_or_opx) << 3 | rm); |
| 950 | } | 924 | } |
| 951 | 925 | ||
| 952 | /// Construct a ModR/M byte using direct r/m addressing | 926 | /// Construct a ModR/M byte using direct r/m addressing |
| ... | @@ -1032,7 +1006,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { | ... | @@ -1032,7 +1006,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 1032 | /// | 1006 | /// |
| 1033 | /// Remember to add a REX prefix byte if index or base are extended! | 1007 | /// Remember to add a REX prefix byte if index or base are extended! |
| 1034 | pub fn sib(self: Self, scale: u2, index: u3, base: u3) !void { | 1008 | pub fn sib(self: Self, scale: u2, index: u3, base: u3) !void { |
| 1035 | try self.writer.writeByte(@as(u8, scale) << 6 | @as(u8, index) << 3 | base); | 1009 | try self.w.writeByte(@as(u8, scale) << 6 | @as(u8, index) << 3 | base); |
| 1036 | } | 1010 | } |
| 1037 | 1011 | ||
| 1038 | /// Construct a SIB byte with scale * index + base, no frills. | 1012 | /// Construct a SIB byte with scale * index + base, no frills. |
| ... | @@ -1124,42 +1098,42 @@ fn Encoder(comptime T: type, comptime opts: Options) type { | ... | @@ -1124,42 +1098,42 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 1124 | /// | 1098 | /// |
| 1125 | /// It is sign-extended to 64 bits by the cpu. | 1099 | /// It is sign-extended to 64 bits by the cpu. |
| 1126 | pub fn disp8(self: Self, disp: i8) !void { | 1100 | pub fn disp8(self: Self, disp: i8) !void { |
| 1127 | try self.writer.writeByte(@as(u8, @bitCast(disp))); | 1101 | try self.w.writeByte(@as(u8, @bitCast(disp))); |
| 1128 | } | 1102 | } |
| 1129 | 1103 | ||
| 1130 | /// Encode an 32 bit displacement | 1104 | /// Encode an 32 bit displacement |
| 1131 | /// | 1105 | /// |
| 1132 | /// It is sign-extended to 64 bits by the cpu. | 1106 | /// It is sign-extended to 64 bits by the cpu. |
| 1133 | pub fn disp32(self: Self, disp: i32) !void { | 1107 | pub fn disp32(self: Self, disp: i32) !void { |
| 1134 | try self.writer.writeInt(i32, disp, .little); | 1108 | try self.w.writeInt(i32, disp, .little); |
| 1135 | } | 1109 | } |
| 1136 | 1110 | ||
| 1137 | /// Encode an 8 bit immediate | 1111 | /// Encode an 8 bit immediate |
| 1138 | /// | 1112 | /// |
| 1139 | /// It is sign-extended to 64 bits by the cpu. | 1113 | /// It is sign-extended to 64 bits by the cpu. |
| 1140 | pub fn imm8(self: Self, imm: u8) !void { | 1114 | pub fn imm8(self: Self, imm: u8) !void { |
| 1141 | try self.writer.writeByte(imm); | 1115 | try self.w.writeByte(imm); |
| 1142 | } | 1116 | } |
| 1143 | 1117 | ||
| 1144 | /// Encode an 16 bit immediate | 1118 | /// Encode an 16 bit immediate |
| 1145 | /// | 1119 | /// |
| 1146 | /// It is sign-extended to 64 bits by the cpu. | 1120 | /// It is sign-extended to 64 bits by the cpu. |
| 1147 | pub fn imm16(self: Self, imm: u16) !void { | 1121 | pub fn imm16(self: Self, imm: u16) !void { |
| 1148 | try self.writer.writeInt(u16, imm, .little); | 1122 | try self.w.writeInt(u16, imm, .little); |
| 1149 | } | 1123 | } |
| 1150 | 1124 | ||
| 1151 | /// Encode an 32 bit immediate | 1125 | /// Encode an 32 bit immediate |
| 1152 | /// | 1126 | /// |
| 1153 | /// It is sign-extended to 64 bits by the cpu. | 1127 | /// It is sign-extended to 64 bits by the cpu. |
| 1154 | pub fn imm32(self: Self, imm: u32) !void { | 1128 | pub fn imm32(self: Self, imm: u32) !void { |
| 1155 | try self.writer.writeInt(u32, imm, .little); | 1129 | try self.w.writeInt(u32, imm, .little); |
| 1156 | } | 1130 | } |
| 1157 | 1131 | ||
| 1158 | /// Encode an 64 bit immediate | 1132 | /// Encode an 64 bit immediate |
| 1159 | /// | 1133 | /// |
| 1160 | /// It is sign-extended to 64 bits by the cpu. | 1134 | /// It is sign-extended to 64 bits by the cpu. |
| 1161 | pub fn imm64(self: Self, imm: u64) !void { | 1135 | pub fn imm64(self: Self, imm: u64) !void { |
| 1162 | try self.writer.writeInt(u64, imm, .little); | 1136 | try self.w.writeInt(u64, imm, .little); |
| 1163 | } | 1137 | } |
| 1164 | }; | 1138 | }; |
| 1165 | } | 1139 | } |
| ... | @@ -2217,10 +2191,10 @@ const Assembler = struct { | ... | @@ -2217,10 +2191,10 @@ const Assembler = struct { |
| 2217 | }; | 2191 | }; |
| 2218 | } | 2192 | } |
| 2219 | 2193 | ||
| 2220 | pub fn assemble(as: *Assembler, writer: anytype) !void { | 2194 | pub fn assemble(as: *Assembler, w: *Writer) !void { |
| 2221 | while (try as.next()) |parsed_inst| { | 2195 | while (try as.next()) |parsed_inst| { |
| 2222 | const inst: Instruction = try .new(.none, parsed_inst.mnemonic, &parsed_inst.ops); | 2196 | const inst: Instruction = try .new(.none, parsed_inst.mnemonic, &parsed_inst.ops); |
| 2223 | try inst.encode(writer, .{}); | 2197 | try inst.encode(w, .{}); |
| 2224 | } | 2198 | } |
| 2225 | } | 2199 | } |
| 2226 | 2200 |
src/codegen/c.zig+117-107| ... | @@ -604,8 +604,12 @@ pub const Function = struct { | ... | @@ -604,8 +604,12 @@ pub const Function = struct { |
| 604 | return f.object.dg.renderIntCast(w, dest_ty, .{ .c_value = .{ .f = f, .value = src, .v = v } }, src_ty, location); | 604 | return f.object.dg.renderIntCast(w, dest_ty, .{ .c_value = .{ .f = f, .value = src, .v = v } }, src_ty, location); |
| 605 | } | 605 | } |
| 606 | 606 | ||
| 607 | fn fmtIntLiteral(f: *Function, val: Value) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) { | 607 | fn fmtIntLiteralDec(f: *Function, val: Value) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) { |
| 608 | return f.object.dg.fmtIntLiteral(val, .Other); | 608 | return f.object.dg.fmtIntLiteralDec(val, .Other); |
| 609 | } | ||
| 610 | |||
| 611 | fn fmtIntLiteralHex(f: *Function, val: Value) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) { | ||
| 612 | return f.object.dg.fmtIntLiteralHex(val, .Other); | ||
| 609 | } | 613 | } |
| 610 | 614 | ||
| 611 | fn getLazyFnName(f: *Function, key: LazyFnKey) ![]const u8 { | 615 | fn getLazyFnName(f: *Function, key: LazyFnKey) ![]const u8 { |
| ... | @@ -629,7 +633,7 @@ pub const Function = struct { | ... | @@ -629,7 +633,7 @@ pub const Function = struct { |
| 629 | }), | 633 | }), |
| 630 | .never_tail, | 634 | .never_tail, |
| 631 | .never_inline, | 635 | .never_inline, |
| 632 | => |owner_nav| try ctype_pool.fmt(gpa, "zig_{s}_{}__{d}", .{ | 636 | => |owner_nav| try ctype_pool.fmt(gpa, "zig_{s}_{f}__{d}", .{ |
| 633 | @tagName(key), | 637 | @tagName(key), |
| 634 | fmtIdentUnsolo(ip.getNav(owner_nav).name.toSlice(ip)), | 638 | fmtIdentUnsolo(ip.getNav(owner_nav).name.toSlice(ip)), |
| 635 | @intFromEnum(owner_nav), | 639 | @intFromEnum(owner_nav), |
| ... | @@ -880,7 +884,7 @@ pub const DeclGen = struct { | ... | @@ -880,7 +884,7 @@ pub const DeclGen = struct { |
| 880 | const addr_val = try pt.intValue(.usize, int.addr); | 884 | const addr_val = try pt.intValue(.usize, int.addr); |
| 881 | try writer.writeByte('('); | 885 | try writer.writeByte('('); |
| 882 | try dg.renderCType(writer, ptr_ctype); | 886 | try dg.renderCType(writer, ptr_ctype); |
| 883 | try writer.print("){x}", .{try dg.fmtIntLiteral(addr_val, .Other)}); | 887 | try writer.print("){f}", .{try dg.fmtIntLiteralHex(addr_val, .Other)}); |
| 884 | }, | 888 | }, |
| 885 | 889 | ||
| 886 | .nav_ptr => |nav| try dg.renderNav(writer, nav, location), | 890 | .nav_ptr => |nav| try dg.renderNav(writer, nav, location), |
| ... | @@ -920,7 +924,7 @@ pub const DeclGen = struct { | ... | @@ -920,7 +924,7 @@ pub const DeclGen = struct { |
| 920 | const offset_val = try pt.intValue(.usize, byte_offset); | 924 | const offset_val = try pt.intValue(.usize, byte_offset); |
| 921 | try writer.writeAll("((char *)"); | 925 | try writer.writeAll("((char *)"); |
| 922 | try dg.renderPointer(writer, field.parent.*, location); | 926 | try dg.renderPointer(writer, field.parent.*, location); |
| 923 | try writer.print(" + {})", .{try dg.fmtIntLiteral(offset_val, .Other)}); | 927 | try writer.print(" + {f})", .{try dg.fmtIntLiteralDec(offset_val, .Other)}); |
| 924 | }, | 928 | }, |
| 925 | } | 929 | } |
| 926 | }, | 930 | }, |
| ... | @@ -942,7 +946,7 @@ pub const DeclGen = struct { | ... | @@ -942,7 +946,7 @@ pub const DeclGen = struct { |
| 942 | // The pointer already has an appropriate type - just do the arithmetic. | 946 | // The pointer already has an appropriate type - just do the arithmetic. |
| 943 | try writer.writeByte('('); | 947 | try writer.writeByte('('); |
| 944 | try dg.renderPointer(writer, elem.parent.*, location); | 948 | try dg.renderPointer(writer, elem.parent.*, location); |
| 945 | try writer.print(" + {})", .{try dg.fmtIntLiteral(index_val, .Other)}); | 949 | try writer.print(" + {f})", .{try dg.fmtIntLiteralDec(index_val, .Other)}); |
| 946 | } else { | 950 | } else { |
| 947 | // We probably have an array pointer `T (*)[n]`. Cast to an element pointer, | 951 | // We probably have an array pointer `T (*)[n]`. Cast to an element pointer, |
| 948 | // and *then* apply the index. | 952 | // and *then* apply the index. |
| ... | @@ -950,7 +954,7 @@ pub const DeclGen = struct { | ... | @@ -950,7 +954,7 @@ pub const DeclGen = struct { |
| 950 | try dg.renderCType(writer, result_ctype); | 954 | try dg.renderCType(writer, result_ctype); |
| 951 | try writer.writeByte(')'); | 955 | try writer.writeByte(')'); |
| 952 | try dg.renderPointer(writer, elem.parent.*, location); | 956 | try dg.renderPointer(writer, elem.parent.*, location); |
| 953 | try writer.print(" + {})", .{try dg.fmtIntLiteral(index_val, .Other)}); | 957 | try writer.print(" + {f})", .{try dg.fmtIntLiteralDec(index_val, .Other)}); |
| 954 | } | 958 | } |
| 955 | }, | 959 | }, |
| 956 | 960 | ||
| ... | @@ -965,7 +969,7 @@ pub const DeclGen = struct { | ... | @@ -965,7 +969,7 @@ pub const DeclGen = struct { |
| 965 | const offset_val = try pt.intValue(.usize, oac.byte_offset); | 969 | const offset_val = try pt.intValue(.usize, oac.byte_offset); |
| 966 | try writer.writeAll("((char *)"); | 970 | try writer.writeAll("((char *)"); |
| 967 | try dg.renderPointer(writer, oac.parent.*, location); | 971 | try dg.renderPointer(writer, oac.parent.*, location); |
| 968 | try writer.print(" + {})", .{try dg.fmtIntLiteral(offset_val, .Other)}); | 972 | try writer.print(" + {f})", .{try dg.fmtIntLiteralDec(offset_val, .Other)}); |
| 969 | } | 973 | } |
| 970 | }, | 974 | }, |
| 971 | } | 975 | } |
| ... | @@ -1037,11 +1041,11 @@ pub const DeclGen = struct { | ... | @@ -1037,11 +1041,11 @@ pub const DeclGen = struct { |
| 1037 | .empty_enum_value, | 1041 | .empty_enum_value, |
| 1038 | => unreachable, // non-runtime values | 1042 | => unreachable, // non-runtime values |
| 1039 | .int => |int| switch (int.storage) { | 1043 | .int => |int| switch (int.storage) { |
| 1040 | .u64, .i64, .big_int => try writer.print("{}", .{try dg.fmtIntLiteral(val, location)}), | 1044 | .u64, .i64, .big_int => try writer.print("{f}", .{try dg.fmtIntLiteralDec(val, location)}), |
| 1041 | .lazy_align, .lazy_size => { | 1045 | .lazy_align, .lazy_size => { |
| 1042 | try writer.writeAll("(("); | 1046 | try writer.writeAll("(("); |
| 1043 | try dg.renderCType(writer, ctype); | 1047 | try dg.renderCType(writer, ctype); |
| 1044 | try writer.print("){x})", .{try dg.fmtIntLiteral( | 1048 | try writer.print("){f})", .{try dg.fmtIntLiteralHex( |
| 1045 | try pt.intValue(.usize, val.toUnsignedInt(zcu)), | 1049 | try pt.intValue(.usize, val.toUnsignedInt(zcu)), |
| 1046 | .Other, | 1050 | .Other, |
| 1047 | )}); | 1051 | )}); |
| ... | @@ -1170,7 +1174,7 @@ pub const DeclGen = struct { | ... | @@ -1170,7 +1174,7 @@ pub const DeclGen = struct { |
| 1170 | try writer.writeAll(", "); | 1174 | try writer.writeAll(", "); |
| 1171 | empty = false; | 1175 | empty = false; |
| 1172 | } | 1176 | } |
| 1173 | try writer.print("{x}", .{try dg.fmtIntLiteral( | 1177 | try writer.print("{f}", .{try dg.fmtIntLiteralHex( |
| 1174 | try pt.intValue_big(repr_ty, repr_val_big.toConst()), | 1178 | try pt.intValue_big(repr_ty, repr_val_big.toConst()), |
| 1175 | location, | 1179 | location, |
| 1176 | )}); | 1180 | )}); |
| ... | @@ -1642,15 +1646,15 @@ pub const DeclGen = struct { | ... | @@ -1642,15 +1646,15 @@ pub const DeclGen = struct { |
| 1642 | .enum_type, | 1646 | .enum_type, |
| 1643 | .error_set_type, | 1647 | .error_set_type, |
| 1644 | .inferred_error_set_type, | 1648 | .inferred_error_set_type, |
| 1645 | => return writer.print("{x}", .{ | 1649 | => return writer.print("{f}", .{ |
| 1646 | try dg.fmtIntLiteral(try pt.undefValue(ty), location), | 1650 | try dg.fmtIntLiteralHex(try pt.undefValue(ty), location), |
| 1647 | }), | 1651 | }), |
| 1648 | .ptr_type => |ptr_type| switch (ptr_type.flags.size) { | 1652 | .ptr_type => |ptr_type| switch (ptr_type.flags.size) { |
| 1649 | .one, .many, .c => { | 1653 | .one, .many, .c => { |
| 1650 | try writer.writeAll("(("); | 1654 | try writer.writeAll("(("); |
| 1651 | try dg.renderCType(writer, ctype); | 1655 | try dg.renderCType(writer, ctype); |
| 1652 | return writer.print("){x})", .{ | 1656 | return writer.print("){f})", .{ |
| 1653 | try dg.fmtIntLiteral(.undef_usize, .Other), | 1657 | try dg.fmtIntLiteralHex(.undef_usize, .Other), |
| 1654 | }); | 1658 | }); |
| 1655 | }, | 1659 | }, |
| 1656 | .slice => { | 1660 | .slice => { |
| ... | @@ -1663,8 +1667,8 @@ pub const DeclGen = struct { | ... | @@ -1663,8 +1667,8 @@ pub const DeclGen = struct { |
| 1663 | try writer.writeAll("{("); | 1667 | try writer.writeAll("{("); |
| 1664 | const ptr_ty = ty.slicePtrFieldType(zcu); | 1668 | const ptr_ty = ty.slicePtrFieldType(zcu); |
| 1665 | try dg.renderType(writer, ptr_ty); | 1669 | try dg.renderType(writer, ptr_ty); |
| 1666 | return writer.print("){x}, {0x}}}", .{ | 1670 | return writer.print("){f}, {0x}}}", .{ |
| 1667 | try dg.fmtIntLiteral(.undef_usize, .Other), | 1671 | try dg.fmtIntLiteralHex(.undef_usize, .Other), |
| 1668 | }); | 1672 | }); |
| 1669 | }, | 1673 | }, |
| 1670 | }, | 1674 | }, |
| ... | @@ -1727,8 +1731,8 @@ pub const DeclGen = struct { | ... | @@ -1727,8 +1731,8 @@ pub const DeclGen = struct { |
| 1727 | } | 1731 | } |
| 1728 | return writer.writeByte('}'); | 1732 | return writer.writeByte('}'); |
| 1729 | }, | 1733 | }, |
| 1730 | .@"packed" => return writer.print("{x}", .{ | 1734 | .@"packed" => return writer.print("{f}", .{ |
| 1731 | try dg.fmtIntLiteral(try pt.undefValue(ty), .Other), | 1735 | try dg.fmtIntLiteralHex(try pt.undefValue(ty), .Other), |
| 1732 | }), | 1736 | }), |
| 1733 | } | 1737 | } |
| 1734 | }, | 1738 | }, |
| ... | @@ -1797,8 +1801,8 @@ pub const DeclGen = struct { | ... | @@ -1797,8 +1801,8 @@ pub const DeclGen = struct { |
| 1797 | } | 1801 | } |
| 1798 | if (has_tag) try writer.writeByte('}'); | 1802 | if (has_tag) try writer.writeByte('}'); |
| 1799 | }, | 1803 | }, |
| 1800 | .@"packed" => return writer.print("{x}", .{ | 1804 | .@"packed" => return writer.print("{f}", .{ |
| 1801 | try dg.fmtIntLiteral(try pt.undefValue(ty), .Other), | 1805 | try dg.fmtIntLiteralHex(try pt.undefValue(ty), .Other), |
| 1802 | }), | 1806 | }), |
| 1803 | } | 1807 | } |
| 1804 | }, | 1808 | }, |
| ... | @@ -1940,8 +1944,8 @@ pub const DeclGen = struct { | ... | @@ -1940,8 +1944,8 @@ pub const DeclGen = struct { |
| 1940 | try w.print("{}", .{trailing}); | 1944 | try w.print("{}", .{trailing}); |
| 1941 | switch (name) { | 1945 | switch (name) { |
| 1942 | .nav => |nav| try dg.renderNavName(w, nav), | 1946 | .nav => |nav| try dg.renderNavName(w, nav), |
| 1943 | .fmt_ctype_pool_string => |fmt| try w.print("{ }", .{fmt}), | 1947 | .fmt_ctype_pool_string => |fmt| try w.print("{f}", .{fmt}), |
| 1944 | .@"export" => |@"export"| try w.print("{ }", .{fmtIdentSolo(@"export".extern_name.toSlice(ip))}), | 1948 | .@"export" => |@"export"| try w.print("{f}", .{fmtIdentSolo(@"export".extern_name.toSlice(ip))}), |
| 1945 | } | 1949 | } |
| 1946 | 1950 | ||
| 1947 | try renderTypeSuffix( | 1951 | try renderTypeSuffix( |
| ... | @@ -2126,7 +2130,7 @@ pub const DeclGen = struct { | ... | @@ -2126,7 +2130,7 @@ pub const DeclGen = struct { |
| 2126 | } else if (dest_bits > 64 and src_bits <= 64) { | 2130 | } else if (dest_bits > 64 and src_bits <= 64) { |
| 2127 | try w.writeAll("zig_make_"); | 2131 | try w.writeAll("zig_make_"); |
| 2128 | try dg.renderTypeForBuiltinFnName(w, dest_ty); | 2132 | try dg.renderTypeForBuiltinFnName(w, dest_ty); |
| 2129 | try w.writeAll("(0, "); // TODO: Should the 0 go through fmtIntLiteral? | 2133 | try w.writeAll("(0, "); |
| 2130 | if (src_is_ptr) { | 2134 | if (src_is_ptr) { |
| 2131 | try w.writeByte('('); | 2135 | try w.writeByte('('); |
| 2132 | try dg.renderType(w, src_eff_ty); | 2136 | try dg.renderType(w, src_eff_ty); |
| ... | @@ -2398,7 +2402,7 @@ pub const DeclGen = struct { | ... | @@ -2398,7 +2402,7 @@ pub const DeclGen = struct { |
| 2398 | }; | 2402 | }; |
| 2399 | 2403 | ||
| 2400 | if (is_big) try writer.print(", {}", .{int_info.signedness == .signed}); | 2404 | if (is_big) try writer.print(", {}", .{int_info.signedness == .signed}); |
| 2401 | try writer.print(", {}", .{try dg.fmtIntLiteral( | 2405 | try writer.print(", {f}", .{try dg.fmtIntLiteralDec( |
| 2402 | try pt.intValue(if (is_big) .u16 else .u8, int_info.bits), | 2406 | try pt.intValue(if (is_big) .u16 else .u8, int_info.bits), |
| 2403 | .FunctionArgument, | 2407 | .FunctionArgument, |
| 2404 | )}); | 2408 | )}); |
| ... | @@ -2408,18 +2412,38 @@ pub const DeclGen = struct { | ... | @@ -2408,18 +2412,38 @@ pub const DeclGen = struct { |
| 2408 | dg: *DeclGen, | 2412 | dg: *DeclGen, |
| 2409 | val: Value, | 2413 | val: Value, |
| 2410 | loc: ValueRenderLocation, | 2414 | loc: ValueRenderLocation, |
| 2411 | ) !std.fmt.Formatter(formatIntLiteral) { | 2415 | base: u8, |
| 2416 | case: std.fmt.Case, | ||
| 2417 | ) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) { | ||
| 2412 | const zcu = dg.pt.zcu; | 2418 | const zcu = dg.pt.zcu; |
| 2413 | const kind = loc.toCTypeKind(); | 2419 | const kind = loc.toCTypeKind(); |
| 2414 | const ty = val.typeOf(zcu); | 2420 | const ty = val.typeOf(zcu); |
| 2415 | return std.fmt.Formatter(formatIntLiteral){ .data = .{ | 2421 | return .{ .data = .{ |
| 2416 | .dg = dg, | 2422 | .dg = dg, |
| 2417 | .int_info = ty.intInfo(zcu), | 2423 | .int_info = ty.intInfo(zcu), |
| 2418 | .kind = kind, | 2424 | .kind = kind, |
| 2419 | .ctype = try dg.ctypeFromType(ty, kind), | 2425 | .ctype = try dg.ctypeFromType(ty, kind), |
| 2420 | .val = val, | 2426 | .val = val, |
| 2427 | .base = base, | ||
| 2428 | .case = case, | ||
| 2421 | } }; | 2429 | } }; |
| 2422 | } | 2430 | } |
| 2431 | |||
| 2432 | fn fmtIntLiteralDec( | ||
| 2433 | dg: *DeclGen, | ||
| 2434 | val: Value, | ||
| 2435 | loc: ValueRenderLocation, | ||
| 2436 | ) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) { | ||
| 2437 | return fmtIntLiteral(dg, val, loc, 10, .lower); | ||
| 2438 | } | ||
| 2439 | |||
| 2440 | fn fmtIntLiteralHex( | ||
| 2441 | dg: *DeclGen, | ||
| 2442 | val: Value, | ||
| 2443 | loc: ValueRenderLocation, | ||
| 2444 | ) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) { | ||
| 2445 | return fmtIntLiteral(dg, val, loc, 16, .lower); | ||
| 2446 | } | ||
| 2423 | }; | 2447 | }; |
| 2424 | 2448 | ||
| 2425 | const CTypeFix = enum { prefix, suffix }; | 2449 | const CTypeFix = enum { prefix, suffix }; |
| ... | @@ -2848,9 +2872,9 @@ pub fn genErrDecls(o: *Object) !void { | ... | @@ -2848,9 +2872,9 @@ pub fn genErrDecls(o: *Object) !void { |
| 2848 | for (names, 1..) |name_nts, val| { | 2872 | for (names, 1..) |name_nts, val| { |
| 2849 | const name = name_nts.toSlice(ip); | 2873 | const name = name_nts.toSlice(ip); |
| 2850 | if (val > 1) try writer.writeAll(", "); | 2874 | if (val > 1) try writer.writeAll(", "); |
| 2851 | try writer.print("{{" ++ name_prefix ++ "{}, {}}}", .{ | 2875 | try writer.print("{{" ++ name_prefix ++ "{f}, {f}}}", .{ |
| 2852 | fmtIdentUnsolo(name), | 2876 | fmtIdentUnsolo(name), |
| 2853 | try o.dg.fmtIntLiteral(try pt.intValue(.usize, name.len), .StaticInitializer), | 2877 | try o.dg.fmtIntLiteralDec(try pt.intValue(.usize, name.len), .StaticInitializer), |
| 2854 | }); | 2878 | }); |
| 2855 | } | 2879 | } |
| 2856 | try writer.writeAll("};\n"); | 2880 | try writer.writeAll("};\n"); |
| ... | @@ -2890,17 +2914,17 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn | ... | @@ -2890,17 +2914,17 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn |
| 2890 | .storage = .{ .bytes = tag_name.toString() }, | 2914 | .storage = .{ .bytes = tag_name.toString() }, |
| 2891 | } }); | 2915 | } }); |
| 2892 | 2916 | ||
| 2893 | try w.print(" case {}: {{\n static ", .{ | 2917 | try w.print(" case {f}: {{\n static ", .{ |
| 2894 | try o.dg.fmtIntLiteral(try tag_val.intFromEnum(enum_ty, pt), .Other), | 2918 | try o.dg.fmtIntLiteralDec(try tag_val.intFromEnum(enum_ty, pt), .Other), |
| 2895 | }); | 2919 | }); |
| 2896 | try o.dg.renderTypeAndName(w, name_ty, .{ .identifier = "name" }, Const, .none, .complete); | 2920 | try o.dg.renderTypeAndName(w, name_ty, .{ .identifier = "name" }, Const, .none, .complete); |
| 2897 | try w.writeAll(" = "); | 2921 | try w.writeAll(" = "); |
| 2898 | try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer); | 2922 | try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer); |
| 2899 | try w.writeAll(";\n return ("); | 2923 | try w.writeAll(";\n return ("); |
| 2900 | try o.dg.renderType(w, name_slice_ty); | 2924 | try o.dg.renderType(w, name_slice_ty); |
| 2901 | try w.print("){{{}, {}}};\n", .{ | 2925 | try w.print("){{{f}, {f}}};\n", .{ |
| 2902 | fmtIdentUnsolo("name"), | 2926 | fmtIdentUnsolo("name"), |
| 2903 | try o.dg.fmtIntLiteral(try pt.intValue(.usize, tag_name_len), .Other), | 2927 | try o.dg.fmtIntLiteralDec(try pt.intValue(.usize, tag_name_len), .Other), |
| 2904 | }); | 2928 | }); |
| 2905 | 2929 | ||
| 2906 | try w.writeAll(" }\n"); | 2930 | try w.writeAll(" }\n"); |
| ... | @@ -2915,7 +2939,7 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn | ... | @@ -2915,7 +2939,7 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn |
| 2915 | const fn_val = zcu.navValue(fn_nav_index); | 2939 | const fn_val = zcu.navValue(fn_nav_index); |
| 2916 | const fn_ctype = try o.dg.ctypeFromType(fn_val.typeOf(zcu), .complete); | 2940 | const fn_ctype = try o.dg.ctypeFromType(fn_val.typeOf(zcu), .complete); |
| 2917 | const fn_info = fn_ctype.info(ctype_pool).function; | 2941 | const fn_info = fn_ctype.info(ctype_pool).function; |
| 2918 | const fn_name = fmtCTypePoolString(val.fn_name, lazy_ctype_pool); | 2942 | const fn_name = fmtCTypePoolString(val.fn_name, lazy_ctype_pool, true); |
| 2919 | 2943 | ||
| 2920 | const fwd = o.dg.fwdDeclWriter(); | 2944 | const fwd = o.dg.fwdDeclWriter(); |
| 2921 | try fwd.print("static zig_{s} ", .{@tagName(key)}); | 2945 | try fwd.print("static zig_{s} ", .{@tagName(key)}); |
| ... | @@ -3954,7 +3978,7 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3954,7 +3978,7 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3954 | try writer.writeByte('('); | 3978 | try writer.writeByte('('); |
| 3955 | try f.writeCValueDeref(writer, operand); | 3979 | try f.writeCValueDeref(writer, operand); |
| 3956 | try v.elem(f, writer); | 3980 | try v.elem(f, writer); |
| 3957 | try writer.print(", {})", .{try f.fmtIntLiteral(bit_offset_val)}); | 3981 | try writer.print(", {f})", .{try f.fmtIntLiteralDec(bit_offset_val)}); |
| 3958 | if (cant_cast) try writer.writeByte(')'); | 3982 | if (cant_cast) try writer.writeByte(')'); |
| 3959 | try f.object.dg.renderBuiltinInfo(writer, field_ty, .bits); | 3983 | try f.object.dg.renderBuiltinInfo(writer, field_ty, .bits); |
| 3960 | try writer.writeByte(')'); | 3984 | try writer.writeByte(')'); |
| ... | @@ -4102,8 +4126,8 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4102,8 +4126,8 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4102 | try writer.writeByte('('); | 4126 | try writer.writeByte('('); |
| 4103 | try f.writeCValue(writer, operand, .FunctionArgument); | 4127 | try f.writeCValue(writer, operand, .FunctionArgument); |
| 4104 | try v.elem(f, writer); | 4128 | try v.elem(f, writer); |
| 4105 | try writer.print(", {x})", .{ | 4129 | try writer.print(", {f})", .{ |
| 4106 | try f.fmtIntLiteral(try inst_scalar_ty.maxIntScalar(pt, scalar_ty)), | 4130 | try f.fmtIntLiteralHex(try inst_scalar_ty.maxIntScalar(pt, scalar_ty)), |
| 4107 | }); | 4131 | }); |
| 4108 | }, | 4132 | }, |
| 4109 | .signed => { | 4133 | .signed => { |
| ... | @@ -4127,9 +4151,9 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4127,9 +4151,9 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4127 | try f.writeCValue(writer, operand, .FunctionArgument); | 4151 | try f.writeCValue(writer, operand, .FunctionArgument); |
| 4128 | try v.elem(f, writer); | 4152 | try v.elem(f, writer); |
| 4129 | if (c_bits == 128) try writer.writeByte(')'); | 4153 | if (c_bits == 128) try writer.writeByte(')'); |
| 4130 | try writer.print(", {})", .{try f.fmtIntLiteral(shift_val)}); | 4154 | try writer.print(", {f})", .{try f.fmtIntLiteralDec(shift_val)}); |
| 4131 | if (c_bits == 128) try writer.writeByte(')'); | 4155 | if (c_bits == 128) try writer.writeByte(')'); |
| 4132 | try writer.print(", {})", .{try f.fmtIntLiteral(shift_val)}); | 4156 | try writer.print(", {f})", .{try f.fmtIntLiteralDec(shift_val)}); |
| 4133 | }, | 4157 | }, |
| 4134 | } | 4158 | } |
| 4135 | if (need_lo) try writer.writeByte(')'); | 4159 | if (need_lo) try writer.writeByte(')'); |
| ... | @@ -4244,7 +4268,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { | ... | @@ -4244,7 +4268,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4244 | try writer.writeByte('('); | 4268 | try writer.writeByte('('); |
| 4245 | try f.writeCValueDeref(writer, ptr_val); | 4269 | try f.writeCValueDeref(writer, ptr_val); |
| 4246 | try v.elem(f, writer); | 4270 | try v.elem(f, writer); |
| 4247 | try writer.print(", {x}), zig_shl_", .{try f.fmtIntLiteral(mask_val)}); | 4271 | try writer.print(", {f}), zig_shl_", .{try f.fmtIntLiteralHex(mask_val)}); |
| 4248 | try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty); | 4272 | try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty); |
| 4249 | try writer.writeByte('('); | 4273 | try writer.writeByte('('); |
| 4250 | const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64; | 4274 | 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 { | ... | @@ -4267,7 +4291,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4267 | try f.writeCValue(writer, src_val, .Other); | 4291 | try f.writeCValue(writer, src_val, .Other); |
| 4268 | try v.elem(f, writer); | 4292 | try v.elem(f, writer); |
| 4269 | if (cant_cast) try writer.writeByte(')'); | 4293 | if (cant_cast) try writer.writeByte(')'); |
| 4270 | try writer.print(", {}))", .{try f.fmtIntLiteral(bit_offset_val)}); | 4294 | try writer.print(", {f}))", .{try f.fmtIntLiteralDec(bit_offset_val)}); |
| 4271 | try a.end(f, writer); | 4295 | try a.end(f, writer); |
| 4272 | try v.end(f, inst, writer); | 4296 | try v.end(f, inst, writer); |
| 4273 | } else { | 4297 | } else { |
| ... | @@ -5348,7 +5372,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void | ... | @@ -5348,7 +5372,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5348 | write_val: { | 5372 | write_val: { |
| 5349 | if (condition_ty.isPtrAtRuntime(zcu)) { | 5373 | if (condition_ty.isPtrAtRuntime(zcu)) { |
| 5350 | if (item_value.?.getUnsignedInt(zcu)) |item_int| { | 5374 | if (item_value.?.getUnsignedInt(zcu)) |item_int| { |
| 5351 | try writer.print("{}", .{try f.fmtIntLiteral(try pt.intValue(lowered_condition_ty, item_int))}); | 5375 | try writer.print("{f}", .{try f.fmtIntLiteralDec(try pt.intValue(lowered_condition_ty, item_int))}); |
| 5352 | break :write_val; | 5376 | break :write_val; |
| 5353 | } | 5377 | } |
| 5354 | } | 5378 | } |
| ... | @@ -6004,8 +6028,8 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6004,8 +6028,8 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6004 | try f.renderType(writer, u8_ptr_ty); | 6028 | try f.renderType(writer, u8_ptr_ty); |
| 6005 | try writer.writeByte(')'); | 6029 | try writer.writeByte(')'); |
| 6006 | try f.writeCValue(writer, field_ptr_val, .Other); | 6030 | try f.writeCValue(writer, field_ptr_val, .Other); |
| 6007 | try writer.print(" - {})", .{ | 6031 | try writer.print(" - {f})", .{ |
| 6008 | try f.fmtIntLiteral(try pt.intValue(.usize, byte_offset)), | 6032 | try f.fmtIntLiteralDec(try pt.intValue(.usize, byte_offset)), |
| 6009 | }); | 6033 | }); |
| 6010 | }, | 6034 | }, |
| 6011 | } | 6035 | } |
| ... | @@ -6049,8 +6073,8 @@ fn fieldPtr( | ... | @@ -6049,8 +6073,8 @@ fn fieldPtr( |
| 6049 | try f.renderType(writer, u8_ptr_ty); | 6073 | try f.renderType(writer, u8_ptr_ty); |
| 6050 | try writer.writeByte(')'); | 6074 | try writer.writeByte(')'); |
| 6051 | try f.writeCValue(writer, container_ptr_val, .Other); | 6075 | try f.writeCValue(writer, container_ptr_val, .Other); |
| 6052 | try writer.print(" + {})", .{ | 6076 | try writer.print(" + {f})", .{ |
| 6053 | try f.fmtIntLiteral(try pt.intValue(.usize, byte_offset)), | 6077 | try f.fmtIntLiteralDec(try pt.intValue(.usize, byte_offset)), |
| 6054 | }); | 6078 | }); |
| 6055 | }, | 6079 | }, |
| 6056 | } | 6080 | } |
| ... | @@ -6121,8 +6145,8 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6121,8 +6145,8 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6121 | try writer.writeByte('('); | 6145 | try writer.writeByte('('); |
| 6122 | } | 6146 | } |
| 6123 | try f.writeCValue(writer, struct_byval, .Other); | 6147 | try f.writeCValue(writer, struct_byval, .Other); |
| 6124 | if (bit_offset > 0) try writer.print(", {})", .{ | 6148 | if (bit_offset > 0) try writer.print(", {f})", .{ |
| 6125 | try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)), | 6149 | try f.fmtIntLiteralDec(try pt.intValue(bit_offset_ty, bit_offset)), |
| 6126 | }); | 6150 | }); |
| 6127 | if (cant_cast) try writer.writeByte(')'); | 6151 | if (cant_cast) try writer.writeByte(')'); |
| 6128 | try f.object.dg.renderBuiltinInfo(writer, field_int_ty, .bits); | 6152 | try f.object.dg.renderBuiltinInfo(writer, field_int_ty, .bits); |
| ... | @@ -6227,8 +6251,8 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6227,8 +6251,8 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6227 | if (!payload_ty.hasRuntimeBits(zcu)) | 6251 | if (!payload_ty.hasRuntimeBits(zcu)) |
| 6228 | try f.writeCValue(writer, operand, .Other) | 6252 | try f.writeCValue(writer, operand, .Other) |
| 6229 | else if (error_ty.errorSetIsEmpty(zcu)) | 6253 | else if (error_ty.errorSetIsEmpty(zcu)) |
| 6230 | try writer.print("{}", .{ | 6254 | try writer.print("{f}", .{ |
| 6231 | try f.fmtIntLiteral(try pt.intValue(try pt.errorIntType(), 0)), | 6255 | try f.fmtIntLiteralDec(try pt.intValue(try pt.errorIntType(), 0)), |
| 6232 | }) | 6256 | }) |
| 6233 | else if (operand_is_ptr) | 6257 | else if (operand_is_ptr) |
| 6234 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" }) | 6258 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" }) |
| ... | @@ -6374,7 +6398,7 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6374,7 +6398,7 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6374 | const a = try Assignment.start(f, writer, try f.ctypeFromType(operand_ty, .complete)); | 6398 | const a = try Assignment.start(f, writer, try f.ctypeFromType(operand_ty, .complete)); |
| 6375 | try f.writeCValueDeref(writer, operand); | 6399 | try f.writeCValueDeref(writer, operand); |
| 6376 | try a.assign(f, writer); | 6400 | try a.assign(f, writer); |
| 6377 | try writer.print("{}", .{try f.fmtIntLiteral(no_err)}); | 6401 | try writer.print("{f}", .{try f.fmtIntLiteralDec(no_err)}); |
| 6378 | try a.end(f, writer); | 6402 | try a.end(f, writer); |
| 6379 | return .none; | 6403 | return .none; |
| 6380 | } | 6404 | } |
| ... | @@ -6382,7 +6406,7 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6382,7 +6406,7 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6382 | const a = try Assignment.start(f, writer, try f.ctypeFromType(err_int_ty, .complete)); | 6406 | const a = try Assignment.start(f, writer, try f.ctypeFromType(err_int_ty, .complete)); |
| 6383 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" }); | 6407 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" }); |
| 6384 | try a.assign(f, writer); | 6408 | try a.assign(f, writer); |
| 6385 | try writer.print("{}", .{try f.fmtIntLiteral(no_err)}); | 6409 | try writer.print("{f}", .{try f.fmtIntLiteralDec(no_err)}); |
| 6386 | try a.end(f, writer); | 6410 | try a.end(f, writer); |
| 6387 | } | 6411 | } |
| 6388 | 6412 | ||
| ... | @@ -6520,7 +6544,7 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6520,7 +6544,7 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6520 | if (operand_child_ctype.info(ctype_pool) == .array) { | 6544 | if (operand_child_ctype.info(ctype_pool) == .array) { |
| 6521 | try writer.writeByte('&'); | 6545 | try writer.writeByte('&'); |
| 6522 | try f.writeCValueDeref(writer, operand); | 6546 | try f.writeCValueDeref(writer, operand); |
| 6523 | try writer.print("[{}]", .{try f.fmtIntLiteral(.zero_usize)}); | 6547 | try writer.print("[{f}]", .{try f.fmtIntLiteralDec(.zero_usize)}); |
| 6524 | } else try f.writeCValue(writer, operand, .Other); | 6548 | } else try f.writeCValue(writer, operand, .Other); |
| 6525 | } | 6549 | } |
| 6526 | try a.end(f, writer); | 6550 | try a.end(f, writer); |
| ... | @@ -6529,8 +6553,8 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6529,8 +6553,8 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6529 | const a = try Assignment.start(f, writer, .usize); | 6553 | const a = try Assignment.start(f, writer, .usize); |
| 6530 | try f.writeCValueMember(writer, local, .{ .identifier = "len" }); | 6554 | try f.writeCValueMember(writer, local, .{ .identifier = "len" }); |
| 6531 | try a.assign(f, writer); | 6555 | try a.assign(f, writer); |
| 6532 | try writer.print("{}", .{ | 6556 | try writer.print("{f}", .{ |
| 6533 | try f.fmtIntLiteral(try pt.intValue(.usize, array_ty.arrayLen(zcu))), | 6557 | try f.fmtIntLiteralDec(try pt.intValue(.usize, array_ty.arrayLen(zcu))), |
| 6534 | }); | 6558 | }); |
| 6535 | try a.end(f, writer); | 6559 | try a.end(f, writer); |
| 6536 | } | 6560 | } |
| ... | @@ -6736,9 +6760,9 @@ fn airCmpBuiltinCall( | ... | @@ -6736,9 +6760,9 @@ fn airCmpBuiltinCall( |
| 6736 | try v.elem(f, writer); | 6760 | try v.elem(f, writer); |
| 6737 | try f.object.dg.renderBuiltinInfo(writer, scalar_ty, info); | 6761 | try f.object.dg.renderBuiltinInfo(writer, scalar_ty, info); |
| 6738 | try writer.writeByte(')'); | 6762 | try writer.writeByte(')'); |
| 6739 | if (!ref_ret) try writer.print("{s}{}", .{ | 6763 | if (!ref_ret) try writer.print("{s}{f}", .{ |
| 6740 | compareOperatorC(operator), | 6764 | compareOperatorC(operator), |
| 6741 | try f.fmtIntLiteral(try pt.intValue(.i32, 0)), | 6765 | try f.fmtIntLiteralDec(try pt.intValue(.i32, 0)), |
| 6742 | }); | 6766 | }); |
| 6743 | try writer.writeAll(";\n"); | 6767 | try writer.writeAll(";\n"); |
| 6744 | try v.end(f, inst, writer); | 6768 | try v.end(f, inst, writer); |
| ... | @@ -7148,8 +7172,8 @@ fn writeArrayLen(f: *Function, writer: ArrayListWriter, dest_ptr: CValue, dest_t | ... | @@ -7148,8 +7172,8 @@ fn writeArrayLen(f: *Function, writer: ArrayListWriter, dest_ptr: CValue, dest_t |
| 7148 | const pt = f.object.dg.pt; | 7172 | const pt = f.object.dg.pt; |
| 7149 | const zcu = pt.zcu; | 7173 | const zcu = pt.zcu; |
| 7150 | switch (dest_ty.ptrSize(zcu)) { | 7174 | switch (dest_ty.ptrSize(zcu)) { |
| 7151 | .one => try writer.print("{}", .{ | 7175 | .one => try writer.print("{f}", .{ |
| 7152 | try f.fmtIntLiteral(try pt.intValue(.usize, dest_ty.childType(zcu).arrayLen(zcu))), | 7176 | try f.fmtIntLiteralDec(try pt.intValue(.usize, dest_ty.childType(zcu).arrayLen(zcu))), |
| 7153 | }), | 7177 | }), |
| 7154 | .many, .c => unreachable, | 7178 | .many, .c => unreachable, |
| 7155 | .slice => try f.writeCValueMember(writer, dest_ptr, .{ .identifier = "len" }), | 7179 | .slice => try f.writeCValueMember(writer, dest_ptr, .{ .identifier = "len" }), |
| ... | @@ -7635,8 +7659,8 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7635,8 +7659,8 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7635 | try writer.writeByte(')'); | 7659 | try writer.writeByte(')'); |
| 7636 | } | 7660 | } |
| 7637 | 7661 | ||
| 7638 | try writer.print(", {}", .{ | 7662 | try writer.print(", {f}", .{ |
| 7639 | try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)), | 7663 | try f.fmtIntLiteralDec(try pt.intValue(bit_offset_ty, bit_offset)), |
| 7640 | }); | 7664 | }); |
| 7641 | try f.object.dg.renderBuiltinInfo(writer, inst_ty, .bits); | 7665 | try f.object.dg.renderBuiltinInfo(writer, inst_ty, .bits); |
| 7642 | try writer.writeByte(')'); | 7666 | try writer.writeByte(')'); |
| ... | @@ -7693,7 +7717,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7693,7 +7717,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7693 | const a = try Assignment.start(f, writer, try f.ctypeFromType(tag_ty, .complete)); | 7717 | const a = try Assignment.start(f, writer, try f.ctypeFromType(tag_ty, .complete)); |
| 7694 | try f.writeCValueMember(writer, local, .{ .identifier = "tag" }); | 7718 | try f.writeCValueMember(writer, local, .{ .identifier = "tag" }); |
| 7695 | try a.assign(f, writer); | 7719 | try a.assign(f, writer); |
| 7696 | try writer.print("{}", .{try f.fmtIntLiteral(try tag_val.intFromEnum(tag_ty, pt))}); | 7720 | try writer.print("{f}", .{try f.fmtIntLiteralDec(try tag_val.intFromEnum(tag_ty, pt))}); |
| 7697 | try a.end(f, writer); | 7721 | try a.end(f, writer); |
| 7698 | } | 7722 | } |
| 7699 | break :field .{ .payload_identifier = field_name.toSlice(ip) }; | 7723 | break :field .{ .payload_identifier = field_name.toSlice(ip) }; |
| ... | @@ -8207,14 +8231,12 @@ fn stringLiteral( | ... | @@ -8207,14 +8231,12 @@ fn stringLiteral( |
| 8207 | }; | 8231 | }; |
| 8208 | } | 8232 | } |
| 8209 | 8233 | ||
| 8210 | const FormatStringContext = struct { str: []const u8, sentinel: ?u8 }; | 8234 | const FormatStringContext = struct { |
| 8211 | fn formatStringLiteral( | 8235 | str: []const u8, |
| 8212 | data: FormatStringContext, | 8236 | sentinel: ?u8, |
| 8213 | writer: *std.io.Writer, | 8237 | }; |
| 8214 | comptime fmt: []const u8, // TODO move this state to FormatStringContext | ||
| 8215 | ) std.io.Writer.Error!void { | ||
| 8216 | if (fmt.len != 1 or fmt[0] != 's') @compileError("Invalid fmt: " ++ fmt); | ||
| 8217 | 8238 | ||
| 8239 | fn formatStringLiteral(data: FormatStringContext, writer: *std.io.Writer) std.io.Writer.Error!void { | ||
| 8218 | var literal = stringLiteral(writer, data.str.len + @intFromBool(data.sentinel != null)); | 8240 | var literal = stringLiteral(writer, data.str.len + @intFromBool(data.sentinel != null)); |
| 8219 | try literal.start(); | 8241 | try literal.start(); |
| 8220 | for (data.str) |c| try literal.writeChar(c); | 8242 | for (data.str) |c| try literal.writeChar(c); |
| ... | @@ -8238,12 +8260,10 @@ const FormatIntLiteralContext = struct { | ... | @@ -8238,12 +8260,10 @@ const FormatIntLiteralContext = struct { |
| 8238 | kind: CType.Kind, | 8260 | kind: CType.Kind, |
| 8239 | ctype: CType, | 8261 | ctype: CType, |
| 8240 | val: Value, | 8262 | val: Value, |
| 8263 | base: u8, | ||
| 8264 | case: std.fmt.Case, | ||
| 8241 | }; | 8265 | }; |
| 8242 | fn formatIntLiteral( | 8266 | fn formatIntLiteral(data: FormatIntLiteralContext, writer: *std.io.Writer) std.io.Writer.Error!void { |
| 8243 | data: FormatIntLiteralContext, | ||
| 8244 | writer: *std.io.Writer, | ||
| 8245 | comptime fmt: []const u8, // TODO move this state to FormatIntLiteralContext | ||
| 8246 | ) std.io.Writer.Error!void { | ||
| 8247 | const pt = data.dg.pt; | 8267 | const pt = data.dg.pt; |
| 8248 | const zcu = pt.zcu; | 8268 | const zcu = pt.zcu; |
| 8249 | const target = &data.dg.mod.resolved_target.result; | 8269 | const target = &data.dg.mod.resolved_target.result; |
| ... | @@ -8268,7 +8288,7 @@ fn formatIntLiteral( | ... | @@ -8268,7 +8288,7 @@ fn formatIntLiteral( |
| 8268 | 8288 | ||
| 8269 | var int_buf: Value.BigIntSpace = undefined; | 8289 | var int_buf: Value.BigIntSpace = undefined; |
| 8270 | const int = if (data.val.isUndefDeep(zcu)) blk: { | 8290 | const int = if (data.val.isUndefDeep(zcu)) blk: { |
| 8271 | undef_limbs = try allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(data.int_info.bits)); | 8291 | undef_limbs = try oom(allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(data.int_info.bits))); |
| 8272 | @memset(undef_limbs, undefPattern(BigIntLimb)); | 8292 | @memset(undef_limbs, undefPattern(BigIntLimb)); |
| 8273 | 8293 | ||
| 8274 | var undef_int = BigInt.Mutable{ | 8294 | var undef_int = BigInt.Mutable{ |
| ... | @@ -8286,7 +8306,7 @@ fn formatIntLiteral( | ... | @@ -8286,7 +8306,7 @@ fn formatIntLiteral( |
| 8286 | const one = BigInt.Mutable.init(&one_limbs, 1).toConst(); | 8306 | const one = BigInt.Mutable.init(&one_limbs, 1).toConst(); |
| 8287 | 8307 | ||
| 8288 | var wrap = BigInt.Mutable{ | 8308 | var wrap = BigInt.Mutable{ |
| 8289 | .limbs = try allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(c_bits)), | 8309 | .limbs = try oom(allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(c_bits))), |
| 8290 | .len = undefined, | 8310 | .len = undefined, |
| 8291 | .positive = undefined, | 8311 | .positive = undefined, |
| 8292 | }; | 8312 | }; |
| ... | @@ -8333,32 +8353,14 @@ fn formatIntLiteral( | ... | @@ -8333,32 +8353,14 @@ fn formatIntLiteral( |
| 8333 | if (!int.positive) try writer.writeByte('-'); | 8353 | if (!int.positive) try writer.writeByte('-'); |
| 8334 | try data.ctype.renderLiteralPrefix(writer, data.kind, ctype_pool); | 8354 | try data.ctype.renderLiteralPrefix(writer, data.kind, ctype_pool); |
| 8335 | 8355 | ||
| 8336 | const style: struct { base: u8, case: std.fmt.Case = undefined } = switch (fmt.len) { | 8356 | switch (data.base) { |
| 8337 | 0 => .{ .base = 10 }, | 8357 | 2 => try writer.writeAll("0b"), |
| 8338 | 1 => switch (fmt[0]) { | 8358 | 8 => try writer.writeByte('0'), |
| 8339 | 'b' => style: { | 8359 | 10 => {}, |
| 8340 | try writer.writeAll("0b"); | 8360 | 16 => try writer.writeAll("0x"), |
| 8341 | break :style .{ .base = 2 }; | 8361 | else => unreachable, |
| 8342 | }, | 8362 | } |
| 8343 | 'o' => style: { | 8363 | const string = try oom(int.abs().toStringAlloc(allocator, data.base, data.case)); |
| 8344 | try writer.writeByte('0'); | ||
| 8345 | break :style .{ .base = 8 }; | ||
| 8346 | }, | ||
| 8347 | 'd' => .{ .base = 10 }, | ||
| 8348 | 'x', 'X' => |base| style: { | ||
| 8349 | try writer.writeAll("0x"); | ||
| 8350 | break :style .{ .base = 16, .case = switch (base) { | ||
| 8351 | 'x' => .lower, | ||
| 8352 | 'X' => .upper, | ||
| 8353 | else => unreachable, | ||
| 8354 | } }; | ||
| 8355 | }, | ||
| 8356 | else => @compileError("Invalid fmt: " ++ fmt), | ||
| 8357 | }, | ||
| 8358 | else => @compileError("Invalid fmt: " ++ fmt), | ||
| 8359 | }; | ||
| 8360 | |||
| 8361 | const string = try int.abs().toStringAlloc(allocator, style.base, style.case); | ||
| 8362 | defer allocator.free(string); | 8364 | defer allocator.free(string); |
| 8363 | try writer.writeAll(string); | 8365 | try writer.writeAll(string); |
| 8364 | } else { | 8366 | } else { |
| ... | @@ -8411,8 +8413,10 @@ fn formatIntLiteral( | ... | @@ -8411,8 +8413,10 @@ fn formatIntLiteral( |
| 8411 | .int_info = c_limb_int_info, | 8413 | .int_info = c_limb_int_info, |
| 8412 | .kind = data.kind, | 8414 | .kind = data.kind, |
| 8413 | .ctype = c_limb_ctype, | 8415 | .ctype = c_limb_ctype, |
| 8414 | .val = try pt.intValue_big(.comptime_int, c_limb_mut.toConst()), | 8416 | .val = try oom(pt.intValue_big(.comptime_int, c_limb_mut.toConst())), |
| 8415 | }, fmt, writer); | 8417 | .base = data.base, |
| 8418 | .case = data.case, | ||
| 8419 | }, writer); | ||
| 8416 | } | 8420 | } |
| 8417 | } | 8421 | } |
| 8418 | try data.ctype.renderLiteralSuffix(writer, ctype_pool); | 8422 | try data.ctype.renderLiteralSuffix(writer, ctype_pool); |
| ... | @@ -8492,11 +8496,11 @@ const Vectorize = struct { | ... | @@ -8492,11 +8496,11 @@ const Vectorize = struct { |
| 8492 | 8496 | ||
| 8493 | try writer.writeAll("for ("); | 8497 | try writer.writeAll("for ("); |
| 8494 | try f.writeCValue(writer, local, .Other); | 8498 | try f.writeCValue(writer, local, .Other); |
| 8495 | try writer.print(" = {d}; ", .{try f.fmtIntLiteral(.zero_usize)}); | 8499 | try writer.print(" = {f}; ", .{try f.fmtIntLiteralDec(.zero_usize)}); |
| 8496 | try f.writeCValue(writer, local, .Other); | 8500 | try f.writeCValue(writer, local, .Other); |
| 8497 | try writer.print(" < {d}; ", .{try f.fmtIntLiteral(try pt.intValue(.usize, ty.vectorLen(zcu)))}); | 8501 | try writer.print(" < {f}; ", .{try f.fmtIntLiteralDec(try pt.intValue(.usize, ty.vectorLen(zcu)))}); |
| 8498 | try f.writeCValue(writer, local, .Other); | 8502 | try f.writeCValue(writer, local, .Other); |
| 8499 | try writer.print(" += {d}) {{\n", .{try f.fmtIntLiteral(.one_usize)}); | 8503 | try writer.print(" += {f}) {{\n", .{try f.fmtIntLiteralDec(.one_usize)}); |
| 8500 | f.object.indent_writer.pushIndent(); | 8504 | f.object.indent_writer.pushIndent(); |
| 8501 | 8505 | ||
| 8502 | break :index .{ .index = local }; | 8506 | break :index .{ .index = local }; |
| ... | @@ -8622,3 +8626,9 @@ fn deinitFreeLocalsMap(gpa: Allocator, map: *LocalsMap) void { | ... | @@ -8622,3 +8626,9 @@ fn deinitFreeLocalsMap(gpa: Allocator, map: *LocalsMap) void { |
| 8622 | } | 8626 | } |
| 8623 | map.deinit(gpa); | 8627 | map.deinit(gpa); |
| 8624 | } | 8628 | } |
| 8629 | |||
| 8630 | fn oom(x: anytype) error{WriteFailed}!@typeInfo(@TypeOf(x)).error_union.payload { | ||
| 8631 | return x catch |err| switch (err) { | ||
| 8632 | error.OutOfMemory => error.WriteFailed, | ||
| 8633 | }; | ||
| 8634 | } |
src/codegen/c/Type.zig+20-18| ... | @@ -209,7 +209,7 @@ pub fn getStandardDefineAbbrev(ctype: CType) ?[]const u8 { | ... | @@ -209,7 +209,7 @@ pub fn getStandardDefineAbbrev(ctype: CType) ?[]const u8 { |
| 209 | }; | 209 | }; |
| 210 | } | 210 | } |
| 211 | 211 | ||
| 212 | pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *const Pool) @TypeOf(writer).Error!void { | 212 | pub fn renderLiteralPrefix(ctype: CType, w: *Writer, kind: Kind, pool: *const Pool) Writer.Error!void { |
| 213 | switch (ctype.info(pool)) { | 213 | switch (ctype.info(pool)) { |
| 214 | .basic => |basic_info| switch (basic_info) { | 214 | .basic => |basic_info| switch (basic_info) { |
| 215 | .void => unreachable, | 215 | .void => unreachable, |
| ... | @@ -224,7 +224,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con | ... | @@ -224,7 +224,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con |
| 224 | .uintptr_t, | 224 | .uintptr_t, |
| 225 | .intptr_t, | 225 | .intptr_t, |
| 226 | => switch (kind) { | 226 | => switch (kind) { |
| 227 | else => try writer.print("({s})", .{@tagName(basic_info)}), | 227 | else => try w.print("({s})", .{@tagName(basic_info)}), |
| 228 | .global => {}, | 228 | .global => {}, |
| 229 | }, | 229 | }, |
| 230 | .int, | 230 | .int, |
| ... | @@ -246,7 +246,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con | ... | @@ -246,7 +246,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con |
| 246 | .int32_t, | 246 | .int32_t, |
| 247 | .uint64_t, | 247 | .uint64_t, |
| 248 | .int64_t, | 248 | .int64_t, |
| 249 | => try writer.print("{s}_C(", .{ctype.getStandardDefineAbbrev().?}), | 249 | => try w.print("{s}_C(", .{ctype.getStandardDefineAbbrev().?}), |
| 250 | .zig_u128, | 250 | .zig_u128, |
| 251 | .zig_i128, | 251 | .zig_i128, |
| 252 | .zig_f16, | 252 | .zig_f16, |
| ... | @@ -255,7 +255,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con | ... | @@ -255,7 +255,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con |
| 255 | .zig_f80, | 255 | .zig_f80, |
| 256 | .zig_f128, | 256 | .zig_f128, |
| 257 | .zig_c_longdouble, | 257 | .zig_c_longdouble, |
| 258 | => try writer.print("zig_{s}_{s}(", .{ | 258 | => try w.print("zig_{s}_{s}(", .{ |
| 259 | switch (kind) { | 259 | switch (kind) { |
| 260 | else => "make", | 260 | else => "make", |
| 261 | .global => "init", | 261 | .global => "init", |
| ... | @@ -265,12 +265,12 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con | ... | @@ -265,12 +265,12 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con |
| 265 | .va_list => unreachable, | 265 | .va_list => unreachable, |
| 266 | _ => unreachable, | 266 | _ => unreachable, |
| 267 | }, | 267 | }, |
| 268 | .array, .vector => try writer.writeByte('{'), | 268 | .array, .vector => try w.writeByte('{'), |
| 269 | else => unreachable, | 269 | else => unreachable, |
| 270 | } | 270 | } |
| 271 | } | 271 | } |
| 272 | 272 | ||
| 273 | pub fn renderLiteralSuffix(ctype: CType, writer: anytype, pool: *const Pool) @TypeOf(writer).Error!void { | 273 | pub fn renderLiteralSuffix(ctype: CType, w: *Writer, pool: *const Pool) Writer.Error!void { |
| 274 | switch (ctype.info(pool)) { | 274 | switch (ctype.info(pool)) { |
| 275 | .basic => |basic_info| switch (basic_info) { | 275 | .basic => |basic_info| switch (basic_info) { |
| 276 | .void => unreachable, | 276 | .void => unreachable, |
| ... | @@ -280,20 +280,20 @@ pub fn renderLiteralSuffix(ctype: CType, writer: anytype, pool: *const Pool) @Ty | ... | @@ -280,20 +280,20 @@ pub fn renderLiteralSuffix(ctype: CType, writer: anytype, pool: *const Pool) @Ty |
| 280 | .short, | 280 | .short, |
| 281 | .int, | 281 | .int, |
| 282 | => {}, | 282 | => {}, |
| 283 | .long => try writer.writeByte('l'), | 283 | .long => try w.writeByte('l'), |
| 284 | .@"long long" => try writer.writeAll("ll"), | 284 | .@"long long" => try w.writeAll("ll"), |
| 285 | .@"unsigned char", | 285 | .@"unsigned char", |
| 286 | .@"unsigned short", | 286 | .@"unsigned short", |
| 287 | .@"unsigned int", | 287 | .@"unsigned int", |
| 288 | => try writer.writeByte('u'), | 288 | => try w.writeByte('u'), |
| 289 | .@"unsigned long", | 289 | .@"unsigned long", |
| 290 | .size_t, | 290 | .size_t, |
| 291 | .uintptr_t, | 291 | .uintptr_t, |
| 292 | => try writer.writeAll("ul"), | 292 | => try w.writeAll("ul"), |
| 293 | .@"unsigned long long" => try writer.writeAll("ull"), | 293 | .@"unsigned long long" => try w.writeAll("ull"), |
| 294 | .float => try writer.writeByte('f'), | 294 | .float => try w.writeByte('f'), |
| 295 | .double => {}, | 295 | .double => {}, |
| 296 | .@"long double" => try writer.writeByte('l'), | 296 | .@"long double" => try w.writeByte('l'), |
| 297 | .bool, | 297 | .bool, |
| 298 | .ptrdiff_t, | 298 | .ptrdiff_t, |
| 299 | .intptr_t, | 299 | .intptr_t, |
| ... | @@ -314,11 +314,11 @@ pub fn renderLiteralSuffix(ctype: CType, writer: anytype, pool: *const Pool) @Ty | ... | @@ -314,11 +314,11 @@ pub fn renderLiteralSuffix(ctype: CType, writer: anytype, pool: *const Pool) @Ty |
| 314 | .zig_f80, | 314 | .zig_f80, |
| 315 | .zig_f128, | 315 | .zig_f128, |
| 316 | .zig_c_longdouble, | 316 | .zig_c_longdouble, |
| 317 | => try writer.writeByte(')'), | 317 | => try w.writeByte(')'), |
| 318 | .va_list => unreachable, | 318 | .va_list => unreachable, |
| 319 | _ => unreachable, | 319 | _ => unreachable, |
| 320 | }, | 320 | }, |
| 321 | .array, .vector => try writer.writeByte('}'), | 321 | .array, .vector => try w.writeByte('}'), |
| 322 | else => unreachable, | 322 | else => unreachable, |
| 323 | } | 323 | } |
| 324 | } | 324 | } |
| ... | @@ -938,7 +938,7 @@ pub const Pool = struct { | ... | @@ -938,7 +938,7 @@ pub const Pool = struct { |
| 938 | index: String.Index, | 938 | index: String.Index, |
| 939 | 939 | ||
| 940 | const FormatData = struct { string: String, pool: *const Pool }; | 940 | const FormatData = struct { string: String, pool: *const Pool }; |
| 941 | fn format(data: FormatData, writer: *std.io.Writer) std.io.Writer.Error!void { | 941 | fn format(data: FormatData, writer: *Writer) Writer.Error!void { |
| 942 | if (data.string.toSlice(data.pool)) |slice| | 942 | if (data.string.toSlice(data.pool)) |slice| |
| 943 | try writer.writeAll(slice) | 943 | try writer.writeAll(slice) |
| 944 | else | 944 | else |
| ... | @@ -2884,7 +2884,7 @@ pub const Pool = struct { | ... | @@ -2884,7 +2884,7 @@ pub const Pool = struct { |
| 2884 | comptime fmt_str: []const u8, | 2884 | comptime fmt_str: []const u8, |
| 2885 | fmt_args: anytype, | 2885 | fmt_args: anytype, |
| 2886 | ) !String { | 2886 | ) !String { |
| 2887 | try pool.string_bytes.writer(allocator).print(fmt_str, fmt_args); | 2887 | try pool.string_bytes.print(allocator, fmt_str, fmt_args); |
| 2888 | return pool.trailingString(allocator); | 2888 | return pool.trailingString(allocator); |
| 2889 | } | 2889 | } |
| 2890 | 2890 | ||
| ... | @@ -3275,10 +3275,12 @@ pub const AlignAs = packed struct { | ... | @@ -3275,10 +3275,12 @@ pub const AlignAs = packed struct { |
| 3275 | } | 3275 | } |
| 3276 | }; | 3276 | }; |
| 3277 | 3277 | ||
| 3278 | const std = @import("std"); | ||
| 3278 | const assert = std.debug.assert; | 3279 | const assert = std.debug.assert; |
| 3280 | const Writer = std.io.Writer; | ||
| 3281 | |||
| 3279 | const CType = @This(); | 3282 | const CType = @This(); |
| 3280 | const InternPool = @import("../../InternPool.zig"); | 3283 | const InternPool = @import("../../InternPool.zig"); |
| 3281 | const Module = @import("../../Package/Module.zig"); | 3284 | const Module = @import("../../Package/Module.zig"); |
| 3282 | const std = @import("std"); | ||
| 3283 | const Type = @import("../../Type.zig"); | 3285 | const Type = @import("../../Type.zig"); |
| 3284 | const Zcu = @import("../../Zcu.zig"); | 3286 | const Zcu = @import("../../Zcu.zig"); |
src/codegen/llvm.zig+28-18| ... | @@ -239,12 +239,12 @@ pub fn targetTriple(allocator: Allocator, target: *const std.Target) ![]const u8 | ... | @@ -239,12 +239,12 @@ pub fn targetTriple(allocator: Allocator, target: *const std.Target) ![]const u8 |
| 239 | .none, | 239 | .none, |
| 240 | .windows, | 240 | .windows, |
| 241 | => {}, | 241 | => {}, |
| 242 | .semver => |ver| try llvm_triple.writer().print("{d}.{d}.{d}", .{ | 242 | .semver => |ver| try llvm_triple.print("{d}.{d}.{d}", .{ |
| 243 | ver.min.major, | 243 | ver.min.major, |
| 244 | ver.min.minor, | 244 | ver.min.minor, |
| 245 | ver.min.patch, | 245 | ver.min.patch, |
| 246 | }), | 246 | }), |
| 247 | inline .linux, .hurd => |ver| try llvm_triple.writer().print("{d}.{d}.{d}", .{ | 247 | inline .linux, .hurd => |ver| try llvm_triple.print("{d}.{d}.{d}", .{ |
| 248 | ver.range.min.major, | 248 | ver.range.min.major, |
| 249 | ver.range.min.minor, | 249 | ver.range.min.minor, |
| 250 | ver.range.min.patch, | 250 | ver.range.min.patch, |
| ... | @@ -295,13 +295,13 @@ pub fn targetTriple(allocator: Allocator, target: *const std.Target) ![]const u8 | ... | @@ -295,13 +295,13 @@ pub fn targetTriple(allocator: Allocator, target: *const std.Target) ![]const u8 |
| 295 | .windows, | 295 | .windows, |
| 296 | => {}, | 296 | => {}, |
| 297 | inline .hurd, .linux => |ver| if (target.abi.isGnu()) { | 297 | inline .hurd, .linux => |ver| if (target.abi.isGnu()) { |
| 298 | try llvm_triple.writer().print("{d}.{d}.{d}", .{ | 298 | try llvm_triple.print("{d}.{d}.{d}", .{ |
| 299 | ver.glibc.major, | 299 | ver.glibc.major, |
| 300 | ver.glibc.minor, | 300 | ver.glibc.minor, |
| 301 | ver.glibc.patch, | 301 | ver.glibc.patch, |
| 302 | }); | 302 | }); |
| 303 | } else if (@TypeOf(ver) == std.Target.Os.LinuxVersionRange and target.abi.isAndroid()) { | 303 | } else if (@TypeOf(ver) == std.Target.Os.LinuxVersionRange and target.abi.isAndroid()) { |
| 304 | try llvm_triple.writer().print("{d}", .{ver.android}); | 304 | try llvm_triple.print("{d}", .{ver.android}); |
| 305 | }, | 305 | }, |
| 306 | } | 306 | } |
| 307 | 307 | ||
| ... | @@ -746,12 +746,18 @@ pub const Object = struct { | ... | @@ -746,12 +746,18 @@ pub const Object = struct { |
| 746 | try wip.finish(); | 746 | try wip.finish(); |
| 747 | } | 747 | } |
| 748 | 748 | ||
| 749 | fn genModuleLevelAssembly(object: *Object) !void { | 749 | fn genModuleLevelAssembly(object: *Object) Allocator.Error!void { |
| 750 | const writer = object.builder.setModuleAsm(); | 750 | const b = &object.builder; |
| 751 | const gpa = b.gpa; | ||
| 752 | b.module_asm.clearRetainingCapacity(); | ||
| 751 | for (object.pt.zcu.global_assembly.values()) |assembly| { | 753 | for (object.pt.zcu.global_assembly.values()) |assembly| { |
| 752 | try writer.print("{s}\n", .{assembly}); | 754 | try b.module_asm.ensureUnusedCapacity(gpa, assembly.len + 1); |
| 755 | b.module_asm.appendSliceAssumeCapacity(assembly); | ||
| 756 | b.module_asm.appendAssumeCapacity('\n'); | ||
| 757 | } | ||
| 758 | if (b.module_asm.getLastOrNull()) |last| { | ||
| 759 | if (last != '\n') try b.module_asm.append(gpa, '\n'); | ||
| 753 | } | 760 | } |
| 754 | try object.builder.finishModuleAsm(); | ||
| 755 | } | 761 | } |
| 756 | 762 | ||
| 757 | pub const EmitOptions = struct { | 763 | pub const EmitOptions = struct { |
| ... | @@ -939,7 +945,9 @@ pub const Object = struct { | ... | @@ -939,7 +945,9 @@ pub const Object = struct { |
| 939 | if (std.mem.eql(u8, path, "-")) { | 945 | if (std.mem.eql(u8, path, "-")) { |
| 940 | o.builder.dump(); | 946 | o.builder.dump(); |
| 941 | } else { | 947 | } else { |
| 942 | _ = try o.builder.printToFile(path); | 948 | o.builder.printToFilePath(std.fs.cwd(), path) catch |err| { |
| 949 | log.err("failed printing LLVM module to \"{s}\": {s}", .{ path, @errorName(err) }); | ||
| 950 | }; | ||
| 943 | } | 951 | } |
| 944 | } | 952 | } |
| 945 | 953 | ||
| ... | @@ -2680,10 +2688,12 @@ pub const Object = struct { | ... | @@ -2680,10 +2688,12 @@ pub const Object = struct { |
| 2680 | } | 2688 | } |
| 2681 | 2689 | ||
| 2682 | fn allocTypeName(o: *Object, ty: Type) Allocator.Error![:0]const u8 { | 2690 | fn allocTypeName(o: *Object, ty: Type) Allocator.Error![:0]const u8 { |
| 2683 | var buffer = std.ArrayList(u8).init(o.gpa); | 2691 | var aw: std.io.Writer.Allocating = .init(o.gpa); |
| 2684 | errdefer buffer.deinit(); | 2692 | defer aw.deinit(); |
| 2685 | try ty.print(buffer.writer(), o.pt); | 2693 | ty.print(&aw.writer, o.pt) catch |err| switch (err) { |
| 2686 | return buffer.toOwnedSliceSentinel(0); | 2694 | error.WriteFailed => return error.OutOfMemory, |
| 2695 | }; | ||
| 2696 | return aw.toOwnedSliceSentinel(0); | ||
| 2687 | } | 2697 | } |
| 2688 | 2698 | ||
| 2689 | /// If the llvm function does not exist, create it. | 2699 | /// If the llvm function does not exist, create it. |
| ... | @@ -4482,7 +4492,7 @@ pub const Object = struct { | ... | @@ -4482,7 +4492,7 @@ pub const Object = struct { |
| 4482 | const target = &zcu.root_mod.resolved_target.result; | 4492 | const target = &zcu.root_mod.resolved_target.result; |
| 4483 | const function_index = try o.builder.addFunction( | 4493 | const function_index = try o.builder.addFunction( |
| 4484 | try o.builder.fnType(ret_ty, &.{try o.lowerType(Type.fromInterned(enum_type.tag_ty))}, .normal), | 4494 | try o.builder.fnType(ret_ty, &.{try o.lowerType(Type.fromInterned(enum_type.tag_ty))}, .normal), |
| 4485 | try o.builder.strtabStringFmt("__zig_tag_name_{}", .{enum_type.name.fmt(ip)}), | 4495 | try o.builder.strtabStringFmt("__zig_tag_name_{f}", .{enum_type.name.fmt(ip)}), |
| 4486 | toLlvmAddressSpace(.generic, target), | 4496 | toLlvmAddressSpace(.generic, target), |
| 4487 | ); | 4497 | ); |
| 4488 | 4498 | ||
| ... | @@ -4633,7 +4643,7 @@ pub const NavGen = struct { | ... | @@ -4633,7 +4643,7 @@ pub const NavGen = struct { |
| 4633 | if (zcu.getTarget().cpu.arch.isWasm() and ty.zigTypeTag(zcu) == .@"fn") { | 4643 | if (zcu.getTarget().cpu.arch.isWasm() and ty.zigTypeTag(zcu) == .@"fn") { |
| 4634 | if (lib_name.toSlice(ip)) |lib_name_slice| { | 4644 | if (lib_name.toSlice(ip)) |lib_name_slice| { |
| 4635 | if (!std.mem.eql(u8, lib_name_slice, "c")) { | 4645 | if (!std.mem.eql(u8, lib_name_slice, "c")) { |
| 4636 | break :decl_name try o.builder.strtabStringFmt("{}|{s}", .{ nav.name.fmt(ip), lib_name_slice }); | 4646 | break :decl_name try o.builder.strtabStringFmt("{f}|{s}", .{ nav.name.fmt(ip), lib_name_slice }); |
| 4637 | } | 4647 | } |
| 4638 | } | 4648 | } |
| 4639 | } | 4649 | } |
| ... | @@ -7472,7 +7482,7 @@ pub const FuncGen = struct { | ... | @@ -7472,7 +7482,7 @@ pub const FuncGen = struct { |
| 7472 | llvm_param_types[llvm_param_i] = llvm_elem_ty; | 7482 | llvm_param_types[llvm_param_i] = llvm_elem_ty; |
| 7473 | } | 7483 | } |
| 7474 | 7484 | ||
| 7475 | try llvm_constraints.writer(self.gpa).print(",{d}", .{output_index}); | 7485 | try llvm_constraints.print(self.gpa, ",{d}", .{output_index}); |
| 7476 | 7486 | ||
| 7477 | // In the case of indirect inputs, LLVM requires the callsite to have | 7487 | // In the case of indirect inputs, LLVM requires the callsite to have |
| 7478 | // an elementtype(<ty>) attribute. | 7488 | // an elementtype(<ty>) attribute. |
| ... | @@ -7573,7 +7583,7 @@ pub const FuncGen = struct { | ... | @@ -7573,7 +7583,7 @@ pub const FuncGen = struct { |
| 7573 | // we should validate the assembly in Sema; by now it is too late | 7583 | // we should validate the assembly in Sema; by now it is too late |
| 7574 | return self.todo("unknown input or output name: '{s}'", .{name}); | 7584 | return self.todo("unknown input or output name: '{s}'", .{name}); |
| 7575 | }; | 7585 | }; |
| 7576 | try rendered_template.writer().print("{d}", .{index}); | 7586 | try rendered_template.print("{d}", .{index}); |
| 7577 | if (byte == ':') { | 7587 | if (byte == ':') { |
| 7578 | try rendered_template.append(':'); | 7588 | try rendered_template.append(':'); |
| 7579 | modifier_start = i + 1; | 7589 | modifier_start = i + 1; |
| ... | @@ -10370,7 +10380,7 @@ pub const FuncGen = struct { | ... | @@ -10370,7 +10380,7 @@ pub const FuncGen = struct { |
| 10370 | const target = &zcu.root_mod.resolved_target.result; | 10380 | const target = &zcu.root_mod.resolved_target.result; |
| 10371 | const function_index = try o.builder.addFunction( | 10381 | const function_index = try o.builder.addFunction( |
| 10372 | try o.builder.fnType(.i1, &.{try o.lowerType(Type.fromInterned(enum_type.tag_ty))}, .normal), | 10382 | try o.builder.fnType(.i1, &.{try o.lowerType(Type.fromInterned(enum_type.tag_ty))}, .normal), |
| 10373 | try o.builder.strtabStringFmt("__zig_is_named_enum_value_{}", .{enum_type.name.fmt(ip)}), | 10383 | try o.builder.strtabStringFmt("__zig_is_named_enum_value_{f}", .{enum_type.name.fmt(ip)}), |
| 10374 | toLlvmAddressSpace(.generic, target), | 10384 | toLlvmAddressSpace(.generic, target), |
| 10375 | ); | 10385 | ); |
| 10376 | 10386 |
src/codegen/spirv.zig+6-4| ... | @@ -1260,10 +1260,12 @@ const NavGen = struct { | ... | @@ -1260,10 +1260,12 @@ const NavGen = struct { |
| 1260 | 1260 | ||
| 1261 | // Turn a Zig type's name into a cache reference. | 1261 | // Turn a Zig type's name into a cache reference. |
| 1262 | fn resolveTypeName(self: *NavGen, ty: Type) ![]const u8 { | 1262 | fn resolveTypeName(self: *NavGen, ty: Type) ![]const u8 { |
| 1263 | var name = std.ArrayList(u8).init(self.gpa); | 1263 | var aw: std.io.Writer.Allocating = .init(self.gpa); |
| 1264 | defer name.deinit(); | 1264 | defer aw.deinit(); |
| 1265 | try ty.print(name.writer(), self.pt); | 1265 | ty.print(&aw.writer, self.pt) catch |err| switch (err) { |
| 1266 | return try name.toOwnedSlice(); | 1266 | error.WriteFailed => return error.OutOfMemory, |
| 1267 | }; | ||
| 1268 | return try aw.toOwnedSlice(); | ||
| 1267 | } | 1269 | } |
| 1268 | 1270 | ||
| 1269 | /// Create an integer type suitable for storing at least 'bits' bits. | 1271 | /// Create an integer type suitable for storing at least 'bits' bits. |
src/link/MachO/Atom.zig+2-3| ... | @@ -938,10 +938,9 @@ const x86_64 = struct { | ... | @@ -938,10 +938,9 @@ const x86_64 = struct { |
| 938 | } | 938 | } |
| 939 | 939 | ||
| 940 | fn encode(insts: []const Instruction, code: []u8) !void { | 940 | fn encode(insts: []const Instruction, code: []u8) !void { |
| 941 | var stream = std.io.fixedBufferStream(code); | 941 | var stream: std.io.Writer = .fixed(code); |
| 942 | const writer = stream.writer(); | ||
| 943 | for (insts) |inst| { | 942 | for (insts) |inst| { |
| 944 | try inst.encode(writer, .{}); | 943 | try inst.encode(&stream, .{}); |
| 945 | } | 944 | } |
| 946 | } | 945 | } |
| 947 | 946 |