| 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 | 71 | reset_attributes: u16, |
| 72 | 72 | }; |
| 73 | 73 | |
| 74 | pub fn setColor( | |
| 75 | conf: Config, | |
| 76 | writer: anytype, | |
| 77 | color: Color, | |
| 78 | ) (@typeInfo(@TypeOf(writer.writeAll(""))).error_union.error_set || | |
| 79 | windows.SetConsoleTextAttributeError)!void { | |
| 74 | pub const SetColorError = std.os.windows.SetConsoleTextAttributeError || std.io.Writer.Error; | |
| 75 | ||
| 76 | pub fn setColor(conf: Config, w: *std.io.Writer, color: Color) SetColorError!void { | |
| 80 | 77 | nosuspend switch (conf) { |
| 81 | 78 | .no_color => return, |
| 82 | 79 | .escape_codes => { |
| ... | ... | @@ -101,7 +98,7 @@ pub const Config = union(enum) { |
| 101 | 98 | .dim => "\x1b[2m", |
| 102 | 99 | .reset => "\x1b[0m", |
| 103 | 100 | }; |
| 104 | try writer.writeAll(color_string); | |
| 101 | try w.writeAll(color_string); | |
| 105 | 102 | }, |
| 106 | 103 | .windows_api => |ctx| if (native_os == .windows) { |
| 107 | 104 | const attributes = switch (color) { |
src/Air/print.zig+102-96| ... | ... | @@ -1,6 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const Allocator = std.mem.Allocator; |
| 3 | const fmtIntSizeBin = std.fmt.fmtIntSizeBin; | |
| 4 | 3 | |
| 5 | 4 | const build_options = @import("build_options"); |
| 6 | 5 | const Zcu = @import("../Zcu.zig"); |
| ... | ... | @@ -9,7 +8,7 @@ const Type = @import("../Type.zig"); |
| 9 | 8 | const Air = @import("../Air.zig"); |
| 10 | 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 | 12 | comptime std.debug.assert(build_options.enable_debug_extensions); |
| 14 | 13 | const instruction_bytes = air.instructions.len * |
| 15 | 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 | 24 | |
| 26 | 25 | // zig fmt: off |
| 27 | 26 | stream.print( |
| 28 | \\# Total AIR+Liveness bytes: {} | |
| 29 | \\# AIR Instructions: {d} ({}) | |
| 30 | \\# AIR Extra Data: {d} ({}) | |
| 31 | \\# Liveness tomb_bits: {} | |
| 32 | \\# Liveness Extra Data: {d} ({}) | |
| 33 | \\# Liveness special table: {d} ({}) | |
| 27 | \\# Total AIR+Liveness bytes: {Bi} | |
| 28 | \\# AIR Instructions: {d} ({Bi}) | |
| 29 | \\# AIR Extra Data: {d} ({Bi}) | |
| 30 | \\# Liveness tomb_bits: {Bi} | |
| 31 | \\# Liveness Extra Data: {d} ({Bi}) | |
| 32 | \\# Liveness special table: {d} ({Bi}) | |
| 34 | 33 | \\ |
| 35 | 34 | , .{ |
| 36 | fmtIntSizeBin(total_bytes), | |
| 37 | air.instructions.len, fmtIntSizeBin(instruction_bytes), | |
| 38 | air.extra.items.len, fmtIntSizeBin(extra_bytes), | |
| 39 | fmtIntSizeBin(tomb_bytes), | |
| 40 | if (liveness) |l| l.extra.len else 0, fmtIntSizeBin(liveness_extra_bytes), | |
| 41 | if (liveness) |l| l.special.count() else 0, fmtIntSizeBin(liveness_special_bytes), | |
| 35 | total_bytes, | |
| 36 | air.instructions.len, instruction_bytes, | |
| 37 | air.extra.items.len, extra_bytes, | |
| 38 | tomb_bytes, | |
| 39 | if (liveness) |l| l.extra.len else 0, liveness_extra_bytes, | |
| 40 | if (liveness) |l| l.special.count() else 0, liveness_special_bytes, | |
| 42 | 41 | }) catch return; |
| 43 | 42 | // zig fmt: on |
| 44 | 43 | |
| ... | ... | @@ -55,7 +54,7 @@ pub fn write(air: Air, stream: anytype, pt: Zcu.PerThread, liveness: ?Air.Livene |
| 55 | 54 | |
| 56 | 55 | pub fn writeInst( |
| 57 | 56 | air: Air, |
| 58 | stream: anytype, | |
| 57 | stream: *std.io.Writer, | |
| 59 | 58 | inst: Air.Inst.Index, |
| 60 | 59 | pt: Zcu.PerThread, |
| 61 | 60 | liveness: ?Air.Liveness, |
| ... | ... | @@ -73,11 +72,15 @@ pub fn writeInst( |
| 73 | 72 | } |
| 74 | 73 | |
| 75 | 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 | 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 | 86 | const Writer = struct { |
| ... | ... | @@ -88,17 +91,19 @@ const Writer = struct { |
| 88 | 91 | indent: usize, |
| 89 | 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 | 97 | for (body) |inst| { |
| 93 | 98 | try w.writeInst(s, inst); |
| 94 | 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 | 104 | const tag = w.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 100 | try s.writeByteNTimes(' ', w.indent); | |
| 101 | try s.print("{}{c}= {s}(", .{ | |
| 105 | try s.splatByteAll(' ', w.indent); | |
| 106 | try s.print("{f}{c}= {s}(", .{ | |
| 102 | 107 | inst, |
| 103 | 108 | @as(u8, if (if (w.liveness) |liveness| liveness.isUnused(inst) else false) '!' else ' '), |
| 104 | 109 | @tagName(tag), |
| ... | ... | @@ -335,47 +340,48 @@ const Writer = struct { |
| 335 | 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 | 344 | const bin_op = w.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 340 | 345 | try w.writeOperand(s, inst, 0, bin_op.lhs); |
| 341 | 346 | try s.writeAll(", "); |
| 342 | 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 | 351 | const un_op = w.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 347 | 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 | 356 | _ = w; |
| 357 | _ = s; | |
| 352 | 358 | _ = inst; |
| 353 | 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 | 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 | 367 | const ty = w.air.instructions.items(.data)[@intFromEnum(inst)].ty; |
| 362 | 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 | 372 | const arg = w.air.instructions.items(.data)[@intFromEnum(inst)].arg; |
| 367 | 373 | try w.writeType(s, arg.ty.toType()); |
| 368 | 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 | 378 | const ty_op = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 373 | 379 | try w.writeType(s, ty_op.ty.toType()); |
| 374 | 380 | try s.writeAll(", "); |
| 375 | 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 | 385 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 380 | 386 | try w.writeType(s, ty_pl.ty.toType()); |
| 381 | 387 | const body: []const Air.Inst.Index = @ptrCast(switch (tag) { |
| ... | ... | @@ -408,15 +414,15 @@ const Writer = struct { |
| 408 | 414 | w.indent += 2; |
| 409 | 415 | try w.writeBody(s, body); |
| 410 | 416 | w.indent = old_indent; |
| 411 | try s.writeByteNTimes(' ', w.indent); | |
| 417 | try s.splatByteAll(' ', w.indent); | |
| 412 | 418 | try s.writeAll("}"); |
| 413 | 419 | |
| 414 | 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 | 426 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 421 | 427 | const extra = w.air.extraData(Air.Block, ty_pl.payload); |
| 422 | 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 | 434 | w.indent += 2; |
| 429 | 435 | try w.writeBody(s, body); |
| 430 | 436 | w.indent = old_indent; |
| 431 | try s.writeByteNTimes(' ', w.indent); | |
| 437 | try s.splatByteAll(' ', w.indent); | |
| 432 | 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 | 442 | const zcu = w.pt.zcu; |
| 437 | 443 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 438 | 444 | const vector_ty = ty_pl.ty.toType(); |
| ... | ... | @@ -448,7 +454,7 @@ const Writer = struct { |
| 448 | 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 | 458 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 453 | 459 | const extra = w.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 454 | 460 | |
| ... | ... | @@ -456,7 +462,7 @@ const Writer = struct { |
| 456 | 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 | 466 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 461 | 467 | const extra = w.air.extraData(Air.StructField, ty_pl.payload).data; |
| 462 | 468 | |
| ... | ... | @@ -464,7 +470,7 @@ const Writer = struct { |
| 464 | 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 | 474 | const data = w.air.instructions.items(.data); |
| 469 | 475 | const ty_pl = data[@intFromEnum(inst)].ty_pl; |
| 470 | 476 | const extra = w.air.extraData(Air.Bin, ty_pl.payload).data; |
| ... | ... | @@ -477,7 +483,7 @@ const Writer = struct { |
| 477 | 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 | 487 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 482 | 488 | const extra = w.air.extraData(Air.Cmpxchg, ty_pl.payload).data; |
| 483 | 489 | |
| ... | ... | @@ -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 | 501 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 496 | 502 | const extra = w.air.extraData(Air.Bin, pl_op.payload).data; |
| 497 | 503 | |
| ... | ... | @@ -502,7 +508,7 @@ const Writer = struct { |
| 502 | 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 | 512 | const unwrapped = w.air.unwrapShuffleOne(w.pt.zcu, inst); |
| 507 | 513 | try w.writeType(s, unwrapped.result_ty); |
| 508 | 514 | try s.writeAll(", "); |
| ... | ... | @@ -518,7 +524,7 @@ const Writer = struct { |
| 518 | 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 | 528 | const unwrapped = w.air.unwrapShuffleTwo(w.pt.zcu, inst); |
| 523 | 529 | try w.writeType(s, unwrapped.result_ty); |
| 524 | 530 | try s.writeAll(", "); |
| ... | ... | @@ -537,7 +543,7 @@ const Writer = struct { |
| 537 | 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 | 547 | const zcu = w.pt.zcu; |
| 542 | 548 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 543 | 549 | const extra = w.air.extraData(Air.Bin, pl_op.payload).data; |
| ... | ... | @@ -552,14 +558,14 @@ const Writer = struct { |
| 552 | 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 | 562 | const reduce = w.air.instructions.items(.data)[@intFromEnum(inst)].reduce; |
| 557 | 563 | |
| 558 | 564 | try w.writeOperand(s, inst, 0, reduce.operand); |
| 559 | 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 | 569 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 564 | 570 | const extra = w.air.extraData(Air.VectorCmp, ty_pl.payload).data; |
| 565 | 571 | |
| ... | ... | @@ -569,7 +575,7 @@ const Writer = struct { |
| 569 | 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 | 579 | const data = w.air.instructions.items(.data)[@intFromEnum(inst)].vector_store_elem; |
| 574 | 580 | const extra = w.air.extraData(Air.VectorCmp, data.payload).data; |
| 575 | 581 | |
| ... | ... | @@ -580,21 +586,21 @@ const Writer = struct { |
| 580 | 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 | 590 | const ip = &w.pt.zcu.intern_pool; |
| 585 | 591 | const ty_nav = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav; |
| 586 | 592 | try w.writeType(s, .fromInterned(ty_nav.ty)); |
| 587 | 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 | 597 | const atomic_load = w.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load; |
| 592 | 598 | |
| 593 | 599 | try w.writeOperand(s, inst, 0, atomic_load.ptr); |
| 594 | 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 | 604 | const prefetch = w.air.instructions.items(.data)[@intFromEnum(inst)].prefetch; |
| 599 | 605 | |
| 600 | 606 | try w.writeOperand(s, inst, 0, prefetch.ptr); |
| ... | ... | @@ -605,10 +611,10 @@ const Writer = struct { |
| 605 | 611 | |
| 606 | 612 | fn writeAtomicStore( |
| 607 | 613 | w: *Writer, |
| 608 | s: anytype, | |
| 614 | s: *std.io.Writer, | |
| 609 | 615 | inst: Air.Inst.Index, |
| 610 | 616 | order: std.builtin.AtomicOrder, |
| 611 | ) @TypeOf(s).Error!void { | |
| 617 | ) Error!void { | |
| 612 | 618 | const bin_op = w.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 613 | 619 | try w.writeOperand(s, inst, 0, bin_op.lhs); |
| 614 | 620 | try s.writeAll(", "); |
| ... | ... | @@ -616,7 +622,7 @@ const Writer = struct { |
| 616 | 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 | 626 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 621 | 627 | const extra = w.air.extraData(Air.AtomicRmw, pl_op.payload).data; |
| 622 | 628 | |
| ... | ... | @@ -626,7 +632,7 @@ const Writer = struct { |
| 626 | 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 | 636 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 631 | 637 | const extra = w.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| 632 | 638 | |
| ... | ... | @@ -634,7 +640,7 @@ const Writer = struct { |
| 634 | 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 | 644 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 639 | 645 | const extra = w.air.extraData(Air.Asm, ty_pl.payload); |
| 640 | 646 | const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0; |
| ... | ... | @@ -704,22 +710,22 @@ const Writer = struct { |
| 704 | 710 | } |
| 705 | 711 | } |
| 706 | 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 | 717 | const dbg_stmt = w.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; |
| 712 | 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 | 722 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 717 | 723 | try w.writeOperand(s, inst, 0, pl_op.operand); |
| 718 | 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 | 729 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 724 | 730 | const extra = w.air.extraData(Air.Call, pl_op.payload); |
| 725 | 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 | 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 | 742 | const br = w.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 737 | 743 | try w.writeInstIndex(s, br.block_inst, false); |
| 738 | 744 | try s.writeAll(", "); |
| 739 | 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 | 749 | const repeat = w.air.instructions.items(.data)[@intFromEnum(inst)].repeat; |
| 744 | 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 | 754 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 749 | 755 | const extra = w.air.extraData(Air.Try, pl_op.payload); |
| 750 | 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 | 766 | w.indent += 2; |
| 761 | 767 | |
| 762 | 768 | if (liveness_condbr.else_deaths.len != 0) { |
| 763 | try s.writeByteNTimes(' ', w.indent); | |
| 769 | try s.splatByteAll(' ', w.indent); | |
| 764 | 770 | for (liveness_condbr.else_deaths, 0..) |operand, i| { |
| 765 | 771 | if (i != 0) try s.writeAll(" "); |
| 766 | try s.print("{}!", .{operand}); | |
| 772 | try s.print("{f}!", .{operand}); | |
| 767 | 773 | } |
| 768 | 774 | try s.writeAll("\n"); |
| 769 | 775 | } |
| 770 | 776 | try w.writeBody(s, body); |
| 771 | 777 | |
| 772 | 778 | w.indent = old_indent; |
| 773 | try s.writeByteNTimes(' ', w.indent); | |
| 779 | try s.splatByteAll(' ', w.indent); | |
| 774 | 780 | try s.writeAll("}"); |
| 775 | 781 | |
| 776 | 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 | 788 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 783 | 789 | const extra = w.air.extraData(Air.TryPtr, ty_pl.payload); |
| 784 | 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 | 803 | w.indent += 2; |
| 798 | 804 | |
| 799 | 805 | if (liveness_condbr.else_deaths.len != 0) { |
| 800 | try s.writeByteNTimes(' ', w.indent); | |
| 806 | try s.splatByteAll(' ', w.indent); | |
| 801 | 807 | for (liveness_condbr.else_deaths, 0..) |operand, i| { |
| 802 | 808 | if (i != 0) try s.writeAll(" "); |
| 803 | try s.print("{}!", .{operand}); | |
| 809 | try s.print("{f}!", .{operand}); | |
| 804 | 810 | } |
| 805 | 811 | try s.writeAll("\n"); |
| 806 | 812 | } |
| 807 | 813 | try w.writeBody(s, body); |
| 808 | 814 | |
| 809 | 815 | w.indent = old_indent; |
| 810 | try s.writeByteNTimes(' ', w.indent); | |
| 816 | try s.splatByteAll(' ', w.indent); | |
| 811 | 817 | try s.writeAll("}"); |
| 812 | 818 | |
| 813 | 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 | 825 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 820 | 826 | const extra = w.air.extraData(Air.CondBr, pl_op.payload); |
| 821 | 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 | 845 | w.indent += 2; |
| 840 | 846 | |
| 841 | 847 | if (liveness_condbr.then_deaths.len != 0) { |
| 842 | try s.writeByteNTimes(' ', w.indent); | |
| 848 | try s.splatByteAll(' ', w.indent); | |
| 843 | 849 | for (liveness_condbr.then_deaths, 0..) |operand, i| { |
| 844 | 850 | if (i != 0) try s.writeAll(" "); |
| 845 | try s.print("{}!", .{operand}); | |
| 851 | try s.print("{f}!", .{operand}); | |
| 846 | 852 | } |
| 847 | 853 | try s.writeAll("\n"); |
| 848 | 854 | } |
| 849 | 855 | |
| 850 | 856 | try w.writeBody(s, then_body); |
| 851 | try s.writeByteNTimes(' ', old_indent); | |
| 857 | try s.splatByteAll(' ', old_indent); | |
| 852 | 858 | try s.writeAll("},"); |
| 853 | 859 | if (extra.data.branch_hints.false != .none) { |
| 854 | 860 | try s.print(" {s}", .{@tagName(extra.data.branch_hints.false)}); |
| ... | ... | @@ -859,10 +865,10 @@ const Writer = struct { |
| 859 | 865 | try s.writeAll(" {\n"); |
| 860 | 866 | |
| 861 | 867 | if (liveness_condbr.else_deaths.len != 0) { |
| 862 | try s.writeByteNTimes(' ', w.indent); | |
| 868 | try s.splatByteAll(' ', w.indent); | |
| 863 | 869 | for (liveness_condbr.else_deaths, 0..) |operand, i| { |
| 864 | 870 | if (i != 0) try s.writeAll(" "); |
| 865 | try s.print("{}!", .{operand}); | |
| 871 | try s.print("{f}!", .{operand}); | |
| 866 | 872 | } |
| 867 | 873 | try s.writeAll("\n"); |
| 868 | 874 | } |
| ... | ... | @@ -870,11 +876,11 @@ const Writer = struct { |
| 870 | 876 | try w.writeBody(s, else_body); |
| 871 | 877 | w.indent = old_indent; |
| 872 | 878 | |
| 873 | try s.writeByteNTimes(' ', old_indent); | |
| 879 | try s.splatByteAll(' ', old_indent); | |
| 874 | 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 | 884 | const switch_br = w.air.unwrapSwitch(inst); |
| 879 | 885 | |
| 880 | 886 | const liveness: Air.Liveness.SwitchBrTable = if (w.liveness) |liveness| |
| ... | ... | @@ -916,17 +922,17 @@ const Writer = struct { |
| 916 | 922 | |
| 917 | 923 | const deaths = liveness.deaths[case.idx]; |
| 918 | 924 | if (deaths.len != 0) { |
| 919 | try s.writeByteNTimes(' ', w.indent); | |
| 925 | try s.splatByteAll(' ', w.indent); | |
| 920 | 926 | for (deaths, 0..) |operand, i| { |
| 921 | 927 | if (i != 0) try s.writeAll(" "); |
| 922 | try s.print("{}!", .{operand}); | |
| 928 | try s.print("{f}!", .{operand}); | |
| 923 | 929 | } |
| 924 | 930 | try s.writeAll("\n"); |
| 925 | 931 | } |
| 926 | 932 | |
| 927 | 933 | try w.writeBody(s, case.body); |
| 928 | 934 | w.indent -= 2; |
| 929 | try s.writeByteNTimes(' ', w.indent); | |
| 935 | try s.splatByteAll(' ', w.indent); | |
| 930 | 936 | try s.writeAll("}"); |
| 931 | 937 | } |
| 932 | 938 | |
| ... | ... | @@ -942,47 +948,47 @@ const Writer = struct { |
| 942 | 948 | |
| 943 | 949 | const deaths = liveness.deaths[liveness.deaths.len - 1]; |
| 944 | 950 | if (deaths.len != 0) { |
| 945 | try s.writeByteNTimes(' ', w.indent); | |
| 951 | try s.splatByteAll(' ', w.indent); | |
| 946 | 952 | for (deaths, 0..) |operand, i| { |
| 947 | 953 | if (i != 0) try s.writeAll(" "); |
| 948 | try s.print("{}!", .{operand}); | |
| 954 | try s.print("{f}!", .{operand}); | |
| 949 | 955 | } |
| 950 | 956 | try s.writeAll("\n"); |
| 951 | 957 | } |
| 952 | 958 | |
| 953 | 959 | try w.writeBody(s, else_body); |
| 954 | 960 | w.indent -= 2; |
| 955 | try s.writeByteNTimes(' ', w.indent); | |
| 961 | try s.splatByteAll(' ', w.indent); | |
| 956 | 962 | try s.writeAll("}"); |
| 957 | 963 | } |
| 958 | 964 | |
| 959 | 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 | 970 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 965 | 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 | 975 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 970 | 976 | try s.print("{d}, ", .{pl_op.payload}); |
| 971 | 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 | 981 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 976 | 982 | try s.print("{d}", .{pl_op.payload}); |
| 977 | 983 | } |
| 978 | 984 | |
| 979 | 985 | fn writeOperand( |
| 980 | 986 | w: *Writer, |
| 981 | s: anytype, | |
| 987 | s: *std.io.Writer, | |
| 982 | 988 | inst: Air.Inst.Index, |
| 983 | 989 | op_index: usize, |
| 984 | 990 | operand: Air.Inst.Ref, |
| 985 | ) @TypeOf(s).Error!void { | |
| 991 | ) Error!void { | |
| 986 | 992 | const small_tomb_bits = Air.Liveness.bpi - 1; |
| 987 | 993 | const dies = if (w.liveness) |liveness| blk: { |
| 988 | 994 | if (op_index < small_tomb_bits) |
| ... | ... | @@ -1004,16 +1010,16 @@ const Writer = struct { |
| 1004 | 1010 | |
| 1005 | 1011 | fn writeInstRef( |
| 1006 | 1012 | w: *Writer, |
| 1007 | s: anytype, | |
| 1013 | s: *std.io.Writer, | |
| 1008 | 1014 | operand: Air.Inst.Ref, |
| 1009 | 1015 | dies: bool, |
| 1010 | ) @TypeOf(s).Error!void { | |
| 1016 | ) Error!void { | |
| 1011 | 1017 | if (@intFromEnum(operand) < InternPool.static_len) { |
| 1012 | 1018 | return s.print("@{}", .{operand}); |
| 1013 | 1019 | } else if (operand.toInterned()) |ip_index| { |
| 1014 | 1020 | const pt = w.pt; |
| 1015 | 1021 | const ty = Type.fromInterned(pt.zcu.intern_pool.indexToKey(ip_index).typeOf()); |
| 1016 | try s.print("<{}, {}>", .{ | |
| 1022 | try s.print("<{f}, {f}>", .{ | |
| 1017 | 1023 | ty.fmt(pt), |
| 1018 | 1024 | Value.fromInterned(ip_index).fmtValue(pt), |
| 1019 | 1025 | }); |
| ... | ... | @@ -1024,12 +1030,12 @@ const Writer = struct { |
| 1024 | 1030 | |
| 1025 | 1031 | fn writeInstIndex( |
| 1026 | 1032 | w: *Writer, |
| 1027 | s: anytype, | |
| 1033 | s: *std.io.Writer, | |
| 1028 | 1034 | inst: Air.Inst.Index, |
| 1029 | 1035 | dies: bool, |
| 1030 | ) @TypeOf(s).Error!void { | |
| 1036 | ) Error!void { | |
| 1031 | 1037 | _ = w; |
| 1032 | try s.print("{}", .{inst}); | |
| 1038 | try s.print("{f}", .{inst}); | |
| 1033 | 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 | 5852 | |
| 5853 | 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 | 5857 | const term = child.wait() catch |err| { |
| 5858 | 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 | 6249 | } |
| 6250 | 6250 | |
| 6251 | 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 | 6253 | const stderr = try stderr_reader.readAllAlloc(arena, 10 * 1024 * 1024); |
| 6254 | 6254 | |
| 6255 | 6255 | const term = child.wait() catch |err| { |
src/Sema.zig+5-5| ... | ... | @@ -3026,8 +3026,8 @@ pub fn createTypeName( |
| 3026 | 3026 | |
| 3027 | 3027 | var aw: std.io.Writer.Allocating = .init(gpa); |
| 3028 | 3028 | defer aw.deinit(); |
| 3029 | const bw = &aw.writer; | |
| 3030 | bw.print("{f}(", .{block.type_name_ctx.fmt(ip)}) catch return error.OutOfMemory; | |
| 3029 | const w = &aw.writer; | |
| 3030 | w.print("{f}(", .{block.type_name_ctx.fmt(ip)}) catch return error.OutOfMemory; | |
| 3031 | 3031 | |
| 3032 | 3032 | var arg_i: usize = 0; |
| 3033 | 3033 | for (fn_info.param_body) |zir_inst| switch (zir_tags[@intFromEnum(zir_inst)]) { |
| ... | ... | @@ -3040,13 +3040,13 @@ pub fn createTypeName( |
| 3040 | 3040 | // result in a compile error. |
| 3041 | 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 | 3045 | // Limiting the depth here helps avoid type names getting too long, which |
| 3046 | 3046 | // in turn helps to avoid unreasonably long symbol names for namespaced |
| 3047 | 3047 | // symbols. Such names should ideally be human-readable, and additionally, |
| 3048 | 3048 | // some tooling may not support very long symbol names. |
| 3049 | bw.print("{f}", .{Value.fmtValueSemaFull(.{ | |
| 3049 | w.print("{f}", .{Value.fmtValueSemaFull(.{ | |
| 3050 | 3050 | .val = arg_val, |
| 3051 | 3051 | .pt = pt, |
| 3052 | 3052 | .opt_sema = sema, |
| ... | ... | @@ -3059,7 +3059,7 @@ pub fn createTypeName( |
| 3059 | 3059 | else => continue, |
| 3060 | 3060 | }; |
| 3061 | 3061 | |
| 3062 | try bw.writeByte(')'); | |
| 3062 | w.writeByte(')') catch return error.OutOfMemory; | |
| 3063 | 3063 | return .{ |
| 3064 | 3064 | .name = try ip.getOrPutString(gpa, pt.tid, aw.getWritten(), .no_embedded_nulls), |
| 3065 | 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 | 1135 | try w.writeAll(lower.err_msg.?.msg); |
| 1136 | 1136 | return; |
| 1137 | 1137 | }, |
| 1138 | error.OutOfMemory, error.InvalidInstruction, error.CannotEncode => |e| { | |
| 1139 | try w.writeAll(switch (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 | }); | |
| 1138 | else => |e| { | |
| 1139 | try w.writeAll(@errorName(e)); | |
| 1144 | 1140 | return; |
| 1145 | 1141 | }, |
| 1146 | else => |e| return e, | |
| 1147 | 1142 | }).insts) |lowered_inst| { |
| 1148 | 1143 | if (!first) try w.writeAll("\ndebug(wip_mir): "); |
| 1149 | 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 | 3 | const log = std.log.scoped(.x86_64_encoder); |
| 4 | 4 | const math = std.math; |
| 5 | 5 | const testing = std.testing; |
| 6 | const Writer = std.io.Writer; | |
| 6 | 7 | |
| 7 | 8 | const bits = @import("bits.zig"); |
| 8 | 9 | const Encoding = @import("Encoding.zig"); |
| ... | ... | @@ -226,101 +227,81 @@ pub const Instruction = struct { |
| 226 | 227 | }; |
| 227 | 228 | } |
| 228 | 229 | |
| 229 | fn format( | |
| 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 { | |
| 230 | const Format = struct { | |
| 243 | 231 | op: Operand, |
| 244 | 232 | enc_op: Encoding.Op, |
| 245 | }; | |
| 246 | 233 | |
| 247 | fn fmtContext( | |
| 248 | ctx: FormatContext, | |
| 249 | comptime unused_format_string: []const u8, | |
| 250 | options: std.fmt.FormatOptions, | |
| 251 | writer: anytype, | |
| 252 | ) @TypeOf(writer).Error!void { | |
| 253 | _ = unused_format_string; | |
| 254 | _ = options; | |
| 255 | const op = ctx.op; | |
| 256 | const enc_op = ctx.enc_op; | |
| 257 | switch (op) { | |
| 258 | .none => {}, | |
| 259 | .reg => |reg| try writer.writeAll(@tagName(reg)), | |
| 260 | .mem => |mem| switch (mem) { | |
| 261 | .rip => |rip| { | |
| 262 | try writer.print("{} [rip", .{rip.ptr_size}); | |
| 263 | if (rip.disp != 0) try writer.print(" {c} 0x{x}", .{ | |
| 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}); | |
| 234 | fn default(f: Format, w: *Writer) Writer.Error!void { | |
| 235 | const op = f.op; | |
| 236 | const enc_op = f.enc_op; | |
| 237 | switch (op) { | |
| 238 | .none => {}, | |
| 239 | .reg => |reg| try w.writeAll(@tagName(reg)), | |
| 240 | .mem => |mem| switch (mem) { | |
| 241 | .rip => |rip| { | |
| 242 | try w.print("{f} [rip", .{rip.ptr_size}); | |
| 243 | if (rip.disp != 0) try w.print(" {c} 0x{x}", .{ | |
| 244 | @as(u8, if (rip.disp < 0) '-' else '+'), | |
| 245 | @abs(rip.disp), | |
| 246 | }); | |
| 247 | try w.writeByte(']'); | |
| 248 | }, | |
| 249 | .sib => |sib| { | |
| 250 | try w.print("{f} ", .{sib.ptr_size}); | |
| 271 | 251 | |
| 272 | if (mem.isSegmentRegister()) { | |
| 273 | return writer.print("{s}:0x{x}", .{ @tagName(sib.base.reg), sib.disp }); | |
| 274 | } | |
| 252 | if (mem.isSegmentRegister()) { | |
| 253 | return w.print("{s}:0x{x}", .{ @tagName(sib.base.reg), sib.disp }); | |
| 254 | } | |
| 275 | 255 | |
| 276 | try writer.writeByte('['); | |
| 277 | ||
| 278 | var any = true; | |
| 279 | switch (sib.base) { | |
| 280 | .none => any = false, | |
| 281 | .reg => |reg| try writer.print("{s}", .{@tagName(reg)}), | |
| 282 | .frame => |frame_index| try writer.print("{}", .{frame_index}), | |
| 283 | .table => try writer.print("Table", .{}), | |
| 284 | .rip_inst => |inst_index| try writer.print("RipInst({d})", .{inst_index}), | |
| 285 | .nav => |nav| try writer.print("Nav({d})", .{@intFromEnum(nav)}), | |
| 286 | .uav => |uav| try writer.print("Uav({d})", .{@intFromEnum(uav.val)}), | |
| 287 | .lazy_sym => |lazy_sym| try writer.print("LazySym({s}, {d})", .{ | |
| 288 | @tagName(lazy_sym.kind), | |
| 289 | @intFromEnum(lazy_sym.ty), | |
| 290 | }), | |
| 291 | .extern_func => |extern_func| try writer.print("ExternFunc({d})", .{@intFromEnum(extern_func)}), | |
| 292 | } | |
| 293 | if (mem.scaleIndex()) |si| { | |
| 294 | if (any) try writer.writeAll(" + "); | |
| 295 | try writer.print("{s} * {d}", .{ @tagName(si.index), si.scale }); | |
| 296 | any = true; | |
| 297 | } | |
| 298 | if (sib.disp != 0 or !any) { | |
| 299 | if (any) | |
| 300 | try writer.print(" {c} ", .{@as(u8, if (sib.disp < 0) '-' else '+')}) | |
| 301 | else if (sib.disp < 0) | |
| 302 | try writer.writeByte('-'); | |
| 303 | try writer.print("0x{x}", .{@abs(sib.disp)}); | |
| 304 | any = true; | |
| 305 | } | |
| 256 | try w.writeByte('['); | |
| 257 | ||
| 258 | var any = true; | |
| 259 | switch (sib.base) { | |
| 260 | .none => any = false, | |
| 261 | .reg => |reg| try w.print("{s}", .{@tagName(reg)}), | |
| 262 | .frame => |frame_index| try w.print("{}", .{frame_index}), | |
| 263 | .table => try w.print("Table", .{}), | |
| 264 | .rip_inst => |inst_index| try w.print("RipInst({d})", .{inst_index}), | |
| 265 | .nav => |nav| try w.print("Nav({d})", .{@intFromEnum(nav)}), | |
| 266 | .uav => |uav| try w.print("Uav({d})", .{@intFromEnum(uav.val)}), | |
| 267 | .lazy_sym => |lazy_sym| try w.print("LazySym({s}, {d})", .{ | |
| 268 | @tagName(lazy_sym.kind), | |
| 269 | @intFromEnum(lazy_sym.ty), | |
| 270 | }), | |
| 271 | .extern_func => |extern_func| try w.print("ExternFunc({d})", .{@intFromEnum(extern_func)}), | |
| 272 | } | |
| 273 | if (mem.scaleIndex()) |si| { | |
| 274 | if (any) try w.writeAll(" + "); | |
| 275 | try w.print("{s} * {d}", .{ @tagName(si.index), si.scale }); | |
| 276 | any = true; | |
| 277 | } | |
| 278 | if (sib.disp != 0 or !any) { | |
| 279 | if (any) | |
| 280 | try w.print(" {c} ", .{@as(u8, if (sib.disp < 0) '-' else '+')}) | |
| 281 | else if (sib.disp < 0) | |
| 282 | try w.writeByte('-'); | |
| 283 | try w.print("0x{x}", .{@abs(sib.disp)}); | |
| 284 | any = true; | |
| 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}", .{ | |
| 310 | @tagName(moffs.seg), | |
| 311 | moffs.offset, | |
| 312 | }), | |
| 313 | }, | |
| 314 | .imm => |imm| if (enc_op.isSigned()) { | |
| 315 | const imms = imm.asSigned(enc_op.immBitSize()); | |
| 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, | |
| 294 | .imm => |imm| if (enc_op.isSigned()) { | |
| 295 | const imms = imm.asSigned(enc_op.immBitSize()); | |
| 296 | if (imms < 0) try w.writeByte('-'); | |
| 297 | try w.print("0x{x}", .{@abs(imms)}); | |
| 298 | } else try w.print("0x{x}", .{imm.asUnsigned(enc_op.immBitSize())}), | |
| 299 | .bytes => unreachable, | |
| 300 | } | |
| 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 | 305 | return .{ .data = .{ .op = op, .enc_op = enc_op } }; |
| 325 | 306 | } |
| 326 | 307 | }; |
| ... | ... | @@ -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 | 347 | var inst: Instruction = .{ |
| 367 | 348 | .prefix = prefix, |
| ... | ... | @@ -372,30 +353,23 @@ pub const Instruction = struct { |
| 372 | 353 | return inst; |
| 373 | 354 | } |
| 374 | 355 | |
| 375 | pub fn format( | |
| 376 | inst: Instruction, | |
| 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; | |
| 356 | pub fn format(inst: Instruction, w: *Writer, comptime unused_format_string: []const u8) Writer.Error!void { | |
| 357 | comptime assert(unused_format_string.len == 0); | |
| 383 | 358 | switch (inst.prefix) { |
| 384 | 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 | 363 | for (inst.ops, inst.encoding.data.ops, 0..) |op, enc, i| { |
| 389 | 364 | if (op == .none) break; |
| 390 | if (i > 0) try writer.writeByte(','); | |
| 391 | try writer.writeByte(' '); | |
| 392 | try writer.print("{}", .{op.fmt(enc)}); | |
| 365 | if (i > 0) try w.writeByte(','); | |
| 366 | try w.print(" {f}", .{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 | 371 | assert(inst.prefix != .directive); |
| 398 | const encoder = Encoder(@TypeOf(writer), opts){ .writer = writer }; | |
| 372 | const encoder: Encoder(opts) = .{ .w = w }; | |
| 399 | 373 | const enc = inst.encoding; |
| 400 | 374 | const data = enc.data; |
| 401 | 375 | |
| ... | ... | @@ -801,9 +775,9 @@ pub const LegacyPrefixes = packed struct { |
| 801 | 775 | |
| 802 | 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 | 779 | return struct { |
| 806 | writer: T, | |
| 780 | w: *Writer, | |
| 807 | 781 | |
| 808 | 782 | const Self = @This(); |
| 809 | 783 | pub const options = opts; |
| ... | ... | @@ -818,31 +792,31 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 818 | 792 | // Hopefully this path isn't taken very often, so we'll do it the slow way for now |
| 819 | 793 | |
| 820 | 794 | // LOCK |
| 821 | if (prefixes.prefix_f0) try self.writer.writeByte(0xf0); | |
| 795 | if (prefixes.prefix_f0) try self.w.writeByte(0xf0); | |
| 822 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 824 | /// |
| 851 | 825 | /// Note that this flag is overridden by REX.W, if both are present. |
| 852 | 826 | pub fn prefix16BitMode(self: Self) !void { |
| 853 | try self.writer.writeByte(0x66); | |
| 827 | try self.w.writeByte(0x66); | |
| 854 | 828 | } |
| 855 | 829 | |
| 856 | 830 | /// Encodes a REX prefix byte given all the fields |
| ... | ... | @@ -869,7 +843,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 869 | 843 | if (fields.x) byte |= 0b0010; |
| 870 | 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 | 849 | /// Encodes a VEX prefix given all the fields |
| ... | ... | @@ -877,24 +851,24 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 877 | 851 | /// See struct `Vex` for a description of each field. |
| 878 | 852 | pub fn vex(self: Self, fields: Vex) !void { |
| 879 | 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 | 857 | @as(u8, ~@intFromBool(fields.r)) << 7 | |
| 884 | 858 | @as(u8, ~@intFromBool(fields.x)) << 6 | |
| 885 | 859 | @as(u8, ~@intFromBool(fields.b)) << 5 | |
| 886 | 860 | @as(u8, @intFromEnum(fields.m)) << 0, |
| 887 | 861 | ); |
| 888 | 862 | |
| 889 | try self.writer.writeByte( | |
| 863 | try self.w.writeByte( | |
| 890 | 864 | @as(u8, @intFromBool(fields.w)) << 7 | |
| 891 | 865 | @as(u8, ~@as(u4, @intCast(fields.v.enc()))) << 3 | |
| 892 | 866 | @as(u8, @intFromBool(fields.l)) << 2 | |
| 893 | 867 | @as(u8, @intFromEnum(fields.p)) << 0, |
| 894 | 868 | ); |
| 895 | 869 | } else { |
| 896 | try self.writer.writeByte(0b1100_0101); | |
| 897 | try self.writer.writeByte( | |
| 870 | try self.w.writeByte(0b1100_0101); | |
| 871 | try self.w.writeByte( | |
| 898 | 872 | @as(u8, ~@intFromBool(fields.r)) << 7 | |
| 899 | 873 | @as(u8, ~@as(u4, @intCast(fields.v.enc()))) << 3 | |
| 900 | 874 | @as(u8, @intFromBool(fields.l)) << 2 | |
| ... | ... | @@ -909,7 +883,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 909 | 883 | |
| 910 | 884 | /// Encodes a 1 byte opcode |
| 911 | 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 | 889 | /// Encodes a 2 byte opcode |
| ... | ... | @@ -918,7 +892,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 918 | 892 | /// |
| 919 | 893 | /// encoder.opcode_2byte(0x0f, 0xaf); |
| 920 | 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 | 898 | /// Encodes a 3 byte opcode |
| ... | ... | @@ -927,7 +901,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 927 | 901 | /// |
| 928 | 902 | /// encoder.opcode_3byte(0xf2, 0x0f, 0x10); |
| 929 | 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 | 907 | /// Encodes a 1 byte opcode with a reg field |
| ... | ... | @@ -935,7 +909,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 935 | 909 | /// Remember to add a REX prefix byte if reg is extended! |
| 936 | 910 | pub fn opcode_withReg(self: Self, opcode: u8, reg: u3) !void { |
| 937 | 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 | 920 | /// |
| 947 | 921 | /// Remember to add a REX prefix byte if reg or rm are extended! |
| 948 | 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 | 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 | 1006 | /// |
| 1033 | 1007 | /// Remember to add a REX prefix byte if index or base are extended! |
| 1034 | 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 | 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 | 1098 | /// |
| 1125 | 1099 | /// It is sign-extended to 64 bits by the cpu. |
| 1126 | 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 | 1104 | /// Encode an 32 bit displacement |
| 1131 | 1105 | /// |
| 1132 | 1106 | /// It is sign-extended to 64 bits by the cpu. |
| 1133 | 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 | 1111 | /// Encode an 8 bit immediate |
| 1138 | 1112 | /// |
| 1139 | 1113 | /// It is sign-extended to 64 bits by the cpu. |
| 1140 | 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 | 1118 | /// Encode an 16 bit immediate |
| 1145 | 1119 | /// |
| 1146 | 1120 | /// It is sign-extended to 64 bits by the cpu. |
| 1147 | 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 | 1125 | /// Encode an 32 bit immediate |
| 1152 | 1126 | /// |
| 1153 | 1127 | /// It is sign-extended to 64 bits by the cpu. |
| 1154 | 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 | 1132 | /// Encode an 64 bit immediate |
| 1159 | 1133 | /// |
| 1160 | 1134 | /// It is sign-extended to 64 bits by the cpu. |
| 1161 | 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 | 2191 | }; |
| 2218 | 2192 | } |
| 2219 | 2193 | |
| 2220 | pub fn assemble(as: *Assembler, writer: anytype) !void { | |
| 2194 | pub fn assemble(as: *Assembler, w: *Writer) !void { | |
| 2221 | 2195 | while (try as.next()) |parsed_inst| { |
| 2222 | 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 | 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) { | |
| 608 | return f.object.dg.fmtIntLiteral(val, .Other); | |
| 607 | fn fmtIntLiteralDec(f: *Function, val: Value) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) { | |
| 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 | 615 | fn getLazyFnName(f: *Function, key: LazyFnKey) ![]const u8 { |
| ... | ... | @@ -629,7 +633,7 @@ pub const Function = struct { |
| 629 | 633 | }), |
| 630 | 634 | .never_tail, |
| 631 | 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 | 637 | @tagName(key), |
| 634 | 638 | fmtIdentUnsolo(ip.getNav(owner_nav).name.toSlice(ip)), |
| 635 | 639 | @intFromEnum(owner_nav), |
| ... | ... | @@ -880,7 +884,7 @@ pub const DeclGen = struct { |
| 880 | 884 | const addr_val = try pt.intValue(.usize, int.addr); |
| 881 | 885 | try writer.writeByte('('); |
| 882 | 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 | 890 | .nav_ptr => |nav| try dg.renderNav(writer, nav, location), |
| ... | ... | @@ -920,7 +924,7 @@ pub const DeclGen = struct { |
| 920 | 924 | const offset_val = try pt.intValue(.usize, byte_offset); |
| 921 | 925 | try writer.writeAll("((char *)"); |
| 922 | 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 | 946 | // The pointer already has an appropriate type - just do the arithmetic. |
| 943 | 947 | try writer.writeByte('('); |
| 944 | 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 | 950 | } else { |
| 947 | 951 | // We probably have an array pointer `T (*)[n]`. Cast to an element pointer, |
| 948 | 952 | // and *then* apply the index. |
| ... | ... | @@ -950,7 +954,7 @@ pub const DeclGen = struct { |
| 950 | 954 | try dg.renderCType(writer, result_ctype); |
| 951 | 955 | try writer.writeByte(')'); |
| 952 | 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 | 969 | const offset_val = try pt.intValue(.usize, oac.byte_offset); |
| 966 | 970 | try writer.writeAll("((char *)"); |
| 967 | 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 | 1041 | .empty_enum_value, |
| 1038 | 1042 | => unreachable, // non-runtime values |
| 1039 | 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 | 1045 | .lazy_align, .lazy_size => { |
| 1042 | 1046 | try writer.writeAll("(("); |
| 1043 | 1047 | try dg.renderCType(writer, ctype); |
| 1044 | try writer.print("){x})", .{try dg.fmtIntLiteral( | |
| 1048 | try writer.print("){f})", .{try dg.fmtIntLiteralHex( | |
| 1045 | 1049 | try pt.intValue(.usize, val.toUnsignedInt(zcu)), |
| 1046 | 1050 | .Other, |
| 1047 | 1051 | )}); |
| ... | ... | @@ -1170,7 +1174,7 @@ pub const DeclGen = struct { |
| 1170 | 1174 | try writer.writeAll(", "); |
| 1171 | 1175 | empty = false; |
| 1172 | 1176 | } |
| 1173 | try writer.print("{x}", .{try dg.fmtIntLiteral( | |
| 1177 | try writer.print("{f}", .{try dg.fmtIntLiteralHex( | |
| 1174 | 1178 | try pt.intValue_big(repr_ty, repr_val_big.toConst()), |
| 1175 | 1179 | location, |
| 1176 | 1180 | )}); |
| ... | ... | @@ -1642,15 +1646,15 @@ pub const DeclGen = struct { |
| 1642 | 1646 | .enum_type, |
| 1643 | 1647 | .error_set_type, |
| 1644 | 1648 | .inferred_error_set_type, |
| 1645 | => return writer.print("{x}", .{ | |
| 1646 | try dg.fmtIntLiteral(try pt.undefValue(ty), location), | |
| 1649 | => return writer.print("{f}", .{ | |
| 1650 | try dg.fmtIntLiteralHex(try pt.undefValue(ty), location), | |
| 1647 | 1651 | }), |
| 1648 | 1652 | .ptr_type => |ptr_type| switch (ptr_type.flags.size) { |
| 1649 | 1653 | .one, .many, .c => { |
| 1650 | 1654 | try writer.writeAll("(("); |
| 1651 | 1655 | try dg.renderCType(writer, ctype); |
| 1652 | return writer.print("){x})", .{ | |
| 1653 | try dg.fmtIntLiteral(.undef_usize, .Other), | |
| 1656 | return writer.print("){f})", .{ | |
| 1657 | try dg.fmtIntLiteralHex(.undef_usize, .Other), | |
| 1654 | 1658 | }); |
| 1655 | 1659 | }, |
| 1656 | 1660 | .slice => { |
| ... | ... | @@ -1663,8 +1667,8 @@ pub const DeclGen = struct { |
| 1663 | 1667 | try writer.writeAll("{("); |
| 1664 | 1668 | const ptr_ty = ty.slicePtrFieldType(zcu); |
| 1665 | 1669 | try dg.renderType(writer, ptr_ty); |
| 1666 | return writer.print("){x}, {0x}}}", .{ | |
| 1667 | try dg.fmtIntLiteral(.undef_usize, .Other), | |
| 1670 | return writer.print("){f}, {0x}}}", .{ | |
| 1671 | try dg.fmtIntLiteralHex(.undef_usize, .Other), | |
| 1668 | 1672 | }); |
| 1669 | 1673 | }, |
| 1670 | 1674 | }, |
| ... | ... | @@ -1727,8 +1731,8 @@ pub const DeclGen = struct { |
| 1727 | 1731 | } |
| 1728 | 1732 | return writer.writeByte('}'); |
| 1729 | 1733 | }, |
| 1730 | .@"packed" => return writer.print("{x}", .{ | |
| 1731 | try dg.fmtIntLiteral(try pt.undefValue(ty), .Other), | |
| 1734 | .@"packed" => return writer.print("{f}", .{ | |
| 1735 | try dg.fmtIntLiteralHex(try pt.undefValue(ty), .Other), | |
| 1732 | 1736 | }), |
| 1733 | 1737 | } |
| 1734 | 1738 | }, |
| ... | ... | @@ -1797,8 +1801,8 @@ pub const DeclGen = struct { |
| 1797 | 1801 | } |
| 1798 | 1802 | if (has_tag) try writer.writeByte('}'); |
| 1799 | 1803 | }, |
| 1800 | .@"packed" => return writer.print("{x}", .{ | |
| 1801 | try dg.fmtIntLiteral(try pt.undefValue(ty), .Other), | |
| 1804 | .@"packed" => return writer.print("{f}", .{ | |
| 1805 | try dg.fmtIntLiteralHex(try pt.undefValue(ty), .Other), | |
| 1802 | 1806 | }), |
| 1803 | 1807 | } |
| 1804 | 1808 | }, |
| ... | ... | @@ -1940,8 +1944,8 @@ pub const DeclGen = struct { |
| 1940 | 1944 | try w.print("{}", .{trailing}); |
| 1941 | 1945 | switch (name) { |
| 1942 | 1946 | .nav => |nav| try dg.renderNavName(w, nav), |
| 1943 | .fmt_ctype_pool_string => |fmt| try w.print("{ }", .{fmt}), | |
| 1944 | .@"export" => |@"export"| try w.print("{ }", .{fmtIdentSolo(@"export".extern_name.toSlice(ip))}), | |
| 1947 | .fmt_ctype_pool_string => |fmt| try w.print("{f}", .{fmt}), | |
| 1948 | .@"export" => |@"export"| try w.print("{f}", .{fmtIdentSolo(@"export".extern_name.toSlice(ip))}), | |
| 1945 | 1949 | } |
| 1946 | 1950 | |
| 1947 | 1951 | try renderTypeSuffix( |
| ... | ... | @@ -2126,7 +2130,7 @@ pub const DeclGen = struct { |
| 2126 | 2130 | } else if (dest_bits > 64 and src_bits <= 64) { |
| 2127 | 2131 | try w.writeAll("zig_make_"); |
| 2128 | 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 | 2134 | if (src_is_ptr) { |
| 2131 | 2135 | try w.writeByte('('); |
| 2132 | 2136 | try dg.renderType(w, src_eff_ty); |
| ... | ... | @@ -2398,7 +2402,7 @@ pub const DeclGen = struct { |
| 2398 | 2402 | }; |
| 2399 | 2403 | |
| 2400 | 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 | 2406 | try pt.intValue(if (is_big) .u16 else .u8, int_info.bits), |
| 2403 | 2407 | .FunctionArgument, |
| 2404 | 2408 | )}); |
| ... | ... | @@ -2408,18 +2412,38 @@ pub const DeclGen = struct { |
| 2408 | 2412 | dg: *DeclGen, |
| 2409 | 2413 | val: Value, |
| 2410 | 2414 | loc: ValueRenderLocation, |
| 2411 | ) !std.fmt.Formatter(formatIntLiteral) { | |
| 2415 | base: u8, | |
| 2416 | case: std.fmt.Case, | |
| 2417 | ) !std.fmt.Formatter(FormatIntLiteralContext, formatIntLiteral) { | |
| 2412 | 2418 | const zcu = dg.pt.zcu; |
| 2413 | 2419 | const kind = loc.toCTypeKind(); |
| 2414 | 2420 | const ty = val.typeOf(zcu); |
| 2415 | return std.fmt.Formatter(formatIntLiteral){ .data = .{ | |
| 2421 | return .{ .data = .{ | |
| 2416 | 2422 | .dg = dg, |
| 2417 | 2423 | .int_info = ty.intInfo(zcu), |
| 2418 | 2424 | .kind = kind, |
| 2419 | 2425 | .ctype = try dg.ctypeFromType(ty, kind), |
| 2420 | 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 | 2449 | const CTypeFix = enum { prefix, suffix }; |
| ... | ... | @@ -2848,9 +2872,9 @@ pub fn genErrDecls(o: *Object) !void { |
| 2848 | 2872 | for (names, 1..) |name_nts, val| { |
| 2849 | 2873 | const name = name_nts.toSlice(ip); |
| 2850 | 2874 | if (val > 1) try writer.writeAll(", "); |
| 2851 | try writer.print("{{" ++ name_prefix ++ "{}, {}}}", .{ | |
| 2875 | try writer.print("{{" ++ name_prefix ++ "{f}, {f}}}", .{ | |
| 2852 | 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 | 2880 | try writer.writeAll("};\n"); |
| ... | ... | @@ -2890,17 +2914,17 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn |
| 2890 | 2914 | .storage = .{ .bytes = tag_name.toString() }, |
| 2891 | 2915 | } }); |
| 2892 | 2916 | |
| 2893 | try w.print(" case {}: {{\n static ", .{ | |
| 2894 | try o.dg.fmtIntLiteral(try tag_val.intFromEnum(enum_ty, pt), .Other), | |
| 2917 | try w.print(" case {f}: {{\n static ", .{ | |
| 2918 | try o.dg.fmtIntLiteralDec(try tag_val.intFromEnum(enum_ty, pt), .Other), | |
| 2895 | 2919 | }); |
| 2896 | 2920 | try o.dg.renderTypeAndName(w, name_ty, .{ .identifier = "name" }, Const, .none, .complete); |
| 2897 | 2921 | try w.writeAll(" = "); |
| 2898 | 2922 | try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer); |
| 2899 | 2923 | try w.writeAll(";\n return ("); |
| 2900 | 2924 | try o.dg.renderType(w, name_slice_ty); |
| 2901 | try w.print("){{{}, {}}};\n", .{ | |
| 2925 | try w.print("){{{f}, {f}}};\n", .{ | |
| 2902 | 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 | 2930 | try w.writeAll(" }\n"); |
| ... | ... | @@ -2915,7 +2939,7 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn |
| 2915 | 2939 | const fn_val = zcu.navValue(fn_nav_index); |
| 2916 | 2940 | const fn_ctype = try o.dg.ctypeFromType(fn_val.typeOf(zcu), .complete); |
| 2917 | 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 | 2944 | const fwd = o.dg.fwdDeclWriter(); |
| 2921 | 2945 | try fwd.print("static zig_{s} ", .{@tagName(key)}); |
| ... | ... | @@ -3954,7 +3978,7 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3954 | 3978 | try writer.writeByte('('); |
| 3955 | 3979 | try f.writeCValueDeref(writer, operand); |
| 3956 | 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 | 3982 | if (cant_cast) try writer.writeByte(')'); |
| 3959 | 3983 | try f.object.dg.renderBuiltinInfo(writer, field_ty, .bits); |
| 3960 | 3984 | try writer.writeByte(')'); |
| ... | ... | @@ -4102,8 +4126,8 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4102 | 4126 | try writer.writeByte('('); |
| 4103 | 4127 | try f.writeCValue(writer, operand, .FunctionArgument); |
| 4104 | 4128 | try v.elem(f, writer); |
| 4105 | try writer.print(", {x})", .{ | |
| 4106 | try f.fmtIntLiteral(try inst_scalar_ty.maxIntScalar(pt, scalar_ty)), | |
| 4129 | try writer.print(", {f})", .{ | |
| 4130 | try f.fmtIntLiteralHex(try inst_scalar_ty.maxIntScalar(pt, scalar_ty)), | |
| 4107 | 4131 | }); |
| 4108 | 4132 | }, |
| 4109 | 4133 | .signed => { |
| ... | ... | @@ -4127,9 +4151,9 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4127 | 4151 | try f.writeCValue(writer, operand, .FunctionArgument); |
| 4128 | 4152 | try v.elem(f, writer); |
| 4129 | 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 | 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 | 4159 | if (need_lo) try writer.writeByte(')'); |
| ... | ... | @@ -4244,7 +4268,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4244 | 4268 | try writer.writeByte('('); |
| 4245 | 4269 | try f.writeCValueDeref(writer, ptr_val); |
| 4246 | 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 | 4272 | try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty); |
| 4249 | 4273 | try writer.writeByte('('); |
| 4250 | 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 | 4291 | try f.writeCValue(writer, src_val, .Other); |
| 4268 | 4292 | try v.elem(f, writer); |
| 4269 | 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 | 4295 | try a.end(f, writer); |
| 4272 | 4296 | try v.end(f, inst, writer); |
| 4273 | 4297 | } else { |
| ... | ... | @@ -5348,7 +5372,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5348 | 5372 | write_val: { |
| 5349 | 5373 | if (condition_ty.isPtrAtRuntime(zcu)) { |
| 5350 | 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 | 5376 | break :write_val; |
| 5353 | 5377 | } |
| 5354 | 5378 | } |
| ... | ... | @@ -6004,8 +6028,8 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6004 | 6028 | try f.renderType(writer, u8_ptr_ty); |
| 6005 | 6029 | try writer.writeByte(')'); |
| 6006 | 6030 | try f.writeCValue(writer, field_ptr_val, .Other); |
| 6007 | try writer.print(" - {})", .{ | |
| 6008 | try f.fmtIntLiteral(try pt.intValue(.usize, byte_offset)), | |
| 6031 | try writer.print(" - {f})", .{ | |
| 6032 | try f.fmtIntLiteralDec(try pt.intValue(.usize, byte_offset)), | |
| 6009 | 6033 | }); |
| 6010 | 6034 | }, |
| 6011 | 6035 | } |
| ... | ... | @@ -6049,8 +6073,8 @@ fn fieldPtr( |
| 6049 | 6073 | try f.renderType(writer, u8_ptr_ty); |
| 6050 | 6074 | try writer.writeByte(')'); |
| 6051 | 6075 | try f.writeCValue(writer, container_ptr_val, .Other); |
| 6052 | try writer.print(" + {})", .{ | |
| 6053 | try f.fmtIntLiteral(try pt.intValue(.usize, byte_offset)), | |
| 6076 | try writer.print(" + {f})", .{ | |
| 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 | 6145 | try writer.writeByte('('); |
| 6122 | 6146 | } |
| 6123 | 6147 | try f.writeCValue(writer, struct_byval, .Other); |
| 6124 | if (bit_offset > 0) try writer.print(", {})", .{ | |
| 6125 | try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)), | |
| 6148 | if (bit_offset > 0) try writer.print(", {f})", .{ | |
| 6149 | try f.fmtIntLiteralDec(try pt.intValue(bit_offset_ty, bit_offset)), | |
| 6126 | 6150 | }); |
| 6127 | 6151 | if (cant_cast) try writer.writeByte(')'); |
| 6128 | 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 | 6251 | if (!payload_ty.hasRuntimeBits(zcu)) |
| 6228 | 6252 | try f.writeCValue(writer, operand, .Other) |
| 6229 | 6253 | else if (error_ty.errorSetIsEmpty(zcu)) |
| 6230 | try writer.print("{}", .{ | |
| 6231 | try f.fmtIntLiteral(try pt.intValue(try pt.errorIntType(), 0)), | |
| 6254 | try writer.print("{f}", .{ | |
| 6255 | try f.fmtIntLiteralDec(try pt.intValue(try pt.errorIntType(), 0)), | |
| 6232 | 6256 | }) |
| 6233 | 6257 | else if (operand_is_ptr) |
| 6234 | 6258 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" }) |
| ... | ... | @@ -6374,7 +6398,7 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6374 | 6398 | const a = try Assignment.start(f, writer, try f.ctypeFromType(operand_ty, .complete)); |
| 6375 | 6399 | try f.writeCValueDeref(writer, operand); |
| 6376 | 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 | 6402 | try a.end(f, writer); |
| 6379 | 6403 | return .none; |
| 6380 | 6404 | } |
| ... | ... | @@ -6382,7 +6406,7 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6382 | 6406 | const a = try Assignment.start(f, writer, try f.ctypeFromType(err_int_ty, .complete)); |
| 6383 | 6407 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" }); |
| 6384 | 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 | 6410 | try a.end(f, writer); |
| 6387 | 6411 | } |
| 6388 | 6412 | |
| ... | ... | @@ -6520,7 +6544,7 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6520 | 6544 | if (operand_child_ctype.info(ctype_pool) == .array) { |
| 6521 | 6545 | try writer.writeByte('&'); |
| 6522 | 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 | 6548 | } else try f.writeCValue(writer, operand, .Other); |
| 6525 | 6549 | } |
| 6526 | 6550 | try a.end(f, writer); |
| ... | ... | @@ -6529,8 +6553,8 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6529 | 6553 | const a = try Assignment.start(f, writer, .usize); |
| 6530 | 6554 | try f.writeCValueMember(writer, local, .{ .identifier = "len" }); |
| 6531 | 6555 | try a.assign(f, writer); |
| 6532 | try writer.print("{}", .{ | |
| 6533 | try f.fmtIntLiteral(try pt.intValue(.usize, array_ty.arrayLen(zcu))), | |
| 6556 | try writer.print("{f}", .{ | |
| 6557 | try f.fmtIntLiteralDec(try pt.intValue(.usize, array_ty.arrayLen(zcu))), | |
| 6534 | 6558 | }); |
| 6535 | 6559 | try a.end(f, writer); |
| 6536 | 6560 | } |
| ... | ... | @@ -6736,9 +6760,9 @@ fn airCmpBuiltinCall( |
| 6736 | 6760 | try v.elem(f, writer); |
| 6737 | 6761 | try f.object.dg.renderBuiltinInfo(writer, scalar_ty, info); |
| 6738 | 6762 | try writer.writeByte(')'); |
| 6739 | if (!ref_ret) try writer.print("{s}{}", .{ | |
| 6763 | if (!ref_ret) try writer.print("{s}{f}", .{ | |
| 6740 | 6764 | compareOperatorC(operator), |
| 6741 | try f.fmtIntLiteral(try pt.intValue(.i32, 0)), | |
| 6765 | try f.fmtIntLiteralDec(try pt.intValue(.i32, 0)), | |
| 6742 | 6766 | }); |
| 6743 | 6767 | try writer.writeAll(";\n"); |
| 6744 | 6768 | try v.end(f, inst, writer); |
| ... | ... | @@ -7148,8 +7172,8 @@ fn writeArrayLen(f: *Function, writer: ArrayListWriter, dest_ptr: CValue, dest_t |
| 7148 | 7172 | const pt = f.object.dg.pt; |
| 7149 | 7173 | const zcu = pt.zcu; |
| 7150 | 7174 | switch (dest_ty.ptrSize(zcu)) { |
| 7151 | .one => try writer.print("{}", .{ | |
| 7152 | try f.fmtIntLiteral(try pt.intValue(.usize, dest_ty.childType(zcu).arrayLen(zcu))), | |
| 7175 | .one => try writer.print("{f}", .{ | |
| 7176 | try f.fmtIntLiteralDec(try pt.intValue(.usize, dest_ty.childType(zcu).arrayLen(zcu))), | |
| 7153 | 7177 | }), |
| 7154 | 7178 | .many, .c => unreachable, |
| 7155 | 7179 | .slice => try f.writeCValueMember(writer, dest_ptr, .{ .identifier = "len" }), |
| ... | ... | @@ -7635,8 +7659,8 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7635 | 7659 | try writer.writeByte(')'); |
| 7636 | 7660 | } |
| 7637 | 7661 | |
| 7638 | try writer.print(", {}", .{ | |
| 7639 | try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)), | |
| 7662 | try writer.print(", {f}", .{ | |
| 7663 | try f.fmtIntLiteralDec(try pt.intValue(bit_offset_ty, bit_offset)), | |
| 7640 | 7664 | }); |
| 7641 | 7665 | try f.object.dg.renderBuiltinInfo(writer, inst_ty, .bits); |
| 7642 | 7666 | try writer.writeByte(')'); |
| ... | ... | @@ -7693,7 +7717,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7693 | 7717 | const a = try Assignment.start(f, writer, try f.ctypeFromType(tag_ty, .complete)); |
| 7694 | 7718 | try f.writeCValueMember(writer, local, .{ .identifier = "tag" }); |
| 7695 | 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 | 7721 | try a.end(f, writer); |
| 7698 | 7722 | } |
| 7699 | 7723 | break :field .{ .payload_identifier = field_name.toSlice(ip) }; |
| ... | ... | @@ -8207,14 +8231,12 @@ fn stringLiteral( |
| 8207 | 8231 | }; |
| 8208 | 8232 | } |
| 8209 | 8233 | |
| 8210 | const FormatStringContext = struct { str: []const u8, sentinel: ?u8 }; | |
| 8211 | fn formatStringLiteral( | |
| 8212 | data: FormatStringContext, | |
| 8213 | writer: *std.io.Writer, | |
| 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); | |
| 8234 | const FormatStringContext = struct { | |
| 8235 | str: []const u8, | |
| 8236 | sentinel: ?u8, | |
| 8237 | }; | |
| 8217 | 8238 | |
| 8239 | fn formatStringLiteral(data: FormatStringContext, writer: *std.io.Writer) std.io.Writer.Error!void { | |
| 8218 | 8240 | var literal = stringLiteral(writer, data.str.len + @intFromBool(data.sentinel != null)); |
| 8219 | 8241 | try literal.start(); |
| 8220 | 8242 | for (data.str) |c| try literal.writeChar(c); |
| ... | ... | @@ -8238,12 +8260,10 @@ const FormatIntLiteralContext = struct { |
| 8238 | 8260 | kind: CType.Kind, |
| 8239 | 8261 | ctype: CType, |
| 8240 | 8262 | val: Value, |
| 8263 | base: u8, | |
| 8264 | case: std.fmt.Case, | |
| 8241 | 8265 | }; |
| 8242 | fn formatIntLiteral( | |
| 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 { | |
| 8266 | fn formatIntLiteral(data: FormatIntLiteralContext, writer: *std.io.Writer) std.io.Writer.Error!void { | |
| 8247 | 8267 | const pt = data.dg.pt; |
| 8248 | 8268 | const zcu = pt.zcu; |
| 8249 | 8269 | const target = &data.dg.mod.resolved_target.result; |
| ... | ... | @@ -8268,7 +8288,7 @@ fn formatIntLiteral( |
| 8268 | 8288 | |
| 8269 | 8289 | var int_buf: Value.BigIntSpace = undefined; |
| 8270 | 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 | 8292 | @memset(undef_limbs, undefPattern(BigIntLimb)); |
| 8273 | 8293 | |
| 8274 | 8294 | var undef_int = BigInt.Mutable{ |
| ... | ... | @@ -8286,7 +8306,7 @@ fn formatIntLiteral( |
| 8286 | 8306 | const one = BigInt.Mutable.init(&one_limbs, 1).toConst(); |
| 8287 | 8307 | |
| 8288 | 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 | 8310 | .len = undefined, |
| 8291 | 8311 | .positive = undefined, |
| 8292 | 8312 | }; |
| ... | ... | @@ -8333,32 +8353,14 @@ fn formatIntLiteral( |
| 8333 | 8353 | if (!int.positive) try writer.writeByte('-'); |
| 8334 | 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) { | |
| 8337 | 0 => .{ .base = 10 }, | |
| 8338 | 1 => switch (fmt[0]) { | |
| 8339 | 'b' => style: { | |
| 8340 | try writer.writeAll("0b"); | |
| 8341 | break :style .{ .base = 2 }; | |
| 8342 | }, | |
| 8343 | 'o' => style: { | |
| 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); | |
| 8356 | switch (data.base) { | |
| 8357 | 2 => try writer.writeAll("0b"), | |
| 8358 | 8 => try writer.writeByte('0'), | |
| 8359 | 10 => {}, | |
| 8360 | 16 => try writer.writeAll("0x"), | |
| 8361 | else => unreachable, | |
| 8362 | } | |
| 8363 | const string = try oom(int.abs().toStringAlloc(allocator, data.base, data.case)); | |
| 8362 | 8364 | defer allocator.free(string); |
| 8363 | 8365 | try writer.writeAll(string); |
| 8364 | 8366 | } else { |
| ... | ... | @@ -8411,8 +8413,10 @@ fn formatIntLiteral( |
| 8411 | 8413 | .int_info = c_limb_int_info, |
| 8412 | 8414 | .kind = data.kind, |
| 8413 | 8415 | .ctype = c_limb_ctype, |
| 8414 | .val = try pt.intValue_big(.comptime_int, c_limb_mut.toConst()), | |
| 8415 | }, fmt, writer); | |
| 8416 | .val = try oom(pt.intValue_big(.comptime_int, c_limb_mut.toConst())), | |
| 8417 | .base = data.base, | |
| 8418 | .case = data.case, | |
| 8419 | }, writer); | |
| 8416 | 8420 | } |
| 8417 | 8421 | } |
| 8418 | 8422 | try data.ctype.renderLiteralSuffix(writer, ctype_pool); |
| ... | ... | @@ -8492,11 +8496,11 @@ const Vectorize = struct { |
| 8492 | 8496 | |
| 8493 | 8497 | try writer.writeAll("for ("); |
| 8494 | 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 | 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 | 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 | 8504 | f.object.indent_writer.pushIndent(); |
| 8501 | 8505 | |
| 8502 | 8506 | break :index .{ .index = local }; |
| ... | ... | @@ -8622,3 +8626,9 @@ fn deinitFreeLocalsMap(gpa: Allocator, map: *LocalsMap) void { |
| 8622 | 8626 | } |
| 8623 | 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 | 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 | 213 | switch (ctype.info(pool)) { |
| 214 | 214 | .basic => |basic_info| switch (basic_info) { |
| 215 | 215 | .void => unreachable, |
| ... | ... | @@ -224,7 +224,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con |
| 224 | 224 | .uintptr_t, |
| 225 | 225 | .intptr_t, |
| 226 | 226 | => switch (kind) { |
| 227 | else => try writer.print("({s})", .{@tagName(basic_info)}), | |
| 227 | else => try w.print("({s})", .{@tagName(basic_info)}), | |
| 228 | 228 | .global => {}, |
| 229 | 229 | }, |
| 230 | 230 | .int, |
| ... | ... | @@ -246,7 +246,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con |
| 246 | 246 | .int32_t, |
| 247 | 247 | .uint64_t, |
| 248 | 248 | .int64_t, |
| 249 | => try writer.print("{s}_C(", .{ctype.getStandardDefineAbbrev().?}), | |
| 249 | => try w.print("{s}_C(", .{ctype.getStandardDefineAbbrev().?}), | |
| 250 | 250 | .zig_u128, |
| 251 | 251 | .zig_i128, |
| 252 | 252 | .zig_f16, |
| ... | ... | @@ -255,7 +255,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con |
| 255 | 255 | .zig_f80, |
| 256 | 256 | .zig_f128, |
| 257 | 257 | .zig_c_longdouble, |
| 258 | => try writer.print("zig_{s}_{s}(", .{ | |
| 258 | => try w.print("zig_{s}_{s}(", .{ | |
| 259 | 259 | switch (kind) { |
| 260 | 260 | else => "make", |
| 261 | 261 | .global => "init", |
| ... | ... | @@ -265,12 +265,12 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con |
| 265 | 265 | .va_list => unreachable, |
| 266 | 266 | _ => unreachable, |
| 267 | 267 | }, |
| 268 | .array, .vector => try writer.writeByte('{'), | |
| 268 | .array, .vector => try w.writeByte('{'), | |
| 269 | 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 | 274 | switch (ctype.info(pool)) { |
| 275 | 275 | .basic => |basic_info| switch (basic_info) { |
| 276 | 276 | .void => unreachable, |
| ... | ... | @@ -280,20 +280,20 @@ pub fn renderLiteralSuffix(ctype: CType, writer: anytype, pool: *const Pool) @Ty |
| 280 | 280 | .short, |
| 281 | 281 | .int, |
| 282 | 282 | => {}, |
| 283 | .long => try writer.writeByte('l'), | |
| 284 | .@"long long" => try writer.writeAll("ll"), | |
| 283 | .long => try w.writeByte('l'), | |
| 284 | .@"long long" => try w.writeAll("ll"), | |
| 285 | 285 | .@"unsigned char", |
| 286 | 286 | .@"unsigned short", |
| 287 | 287 | .@"unsigned int", |
| 288 | => try writer.writeByte('u'), | |
| 288 | => try w.writeByte('u'), | |
| 289 | 289 | .@"unsigned long", |
| 290 | 290 | .size_t, |
| 291 | 291 | .uintptr_t, |
| 292 | => try writer.writeAll("ul"), | |
| 293 | .@"unsigned long long" => try writer.writeAll("ull"), | |
| 294 | .float => try writer.writeByte('f'), | |
| 292 | => try w.writeAll("ul"), | |
| 293 | .@"unsigned long long" => try w.writeAll("ull"), | |
| 294 | .float => try w.writeByte('f'), | |
| 295 | 295 | .double => {}, |
| 296 | .@"long double" => try writer.writeByte('l'), | |
| 296 | .@"long double" => try w.writeByte('l'), | |
| 297 | 297 | .bool, |
| 298 | 298 | .ptrdiff_t, |
| 299 | 299 | .intptr_t, |
| ... | ... | @@ -314,11 +314,11 @@ pub fn renderLiteralSuffix(ctype: CType, writer: anytype, pool: *const Pool) @Ty |
| 314 | 314 | .zig_f80, |
| 315 | 315 | .zig_f128, |
| 316 | 316 | .zig_c_longdouble, |
| 317 | => try writer.writeByte(')'), | |
| 317 | => try w.writeByte(')'), | |
| 318 | 318 | .va_list => unreachable, |
| 319 | 319 | _ => unreachable, |
| 320 | 320 | }, |
| 321 | .array, .vector => try writer.writeByte('}'), | |
| 321 | .array, .vector => try w.writeByte('}'), | |
| 322 | 322 | else => unreachable, |
| 323 | 323 | } |
| 324 | 324 | } |
| ... | ... | @@ -938,7 +938,7 @@ pub const Pool = struct { |
| 938 | 938 | index: String.Index, |
| 939 | 939 | |
| 940 | 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 | 942 | if (data.string.toSlice(data.pool)) |slice| |
| 943 | 943 | try writer.writeAll(slice) |
| 944 | 944 | else |
| ... | ... | @@ -2884,7 +2884,7 @@ pub const Pool = struct { |
| 2884 | 2884 | comptime fmt_str: []const u8, |
| 2885 | 2885 | fmt_args: anytype, |
| 2886 | 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 | 2888 | return pool.trailingString(allocator); |
| 2889 | 2889 | } |
| 2890 | 2890 | |
| ... | ... | @@ -3275,10 +3275,12 @@ pub const AlignAs = packed struct { |
| 3275 | 3275 | } |
| 3276 | 3276 | }; |
| 3277 | 3277 | |
| 3278 | const std = @import("std"); | |
| 3278 | 3279 | const assert = std.debug.assert; |
| 3280 | const Writer = std.io.Writer; | |
| 3281 | ||
| 3279 | 3282 | const CType = @This(); |
| 3280 | 3283 | const InternPool = @import("../../InternPool.zig"); |
| 3281 | 3284 | const Module = @import("../../Package/Module.zig"); |
| 3282 | const std = @import("std"); | |
| 3283 | 3285 | const Type = @import("../../Type.zig"); |
| 3284 | 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 | 239 | .none, |
| 240 | 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 | 243 | ver.min.major, |
| 244 | 244 | ver.min.minor, |
| 245 | 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 | 248 | ver.range.min.major, |
| 249 | 249 | ver.range.min.minor, |
| 250 | 250 | ver.range.min.patch, |
| ... | ... | @@ -295,13 +295,13 @@ pub fn targetTriple(allocator: Allocator, target: *const std.Target) ![]const u8 |
| 295 | 295 | .windows, |
| 296 | 296 | => {}, |
| 297 | 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 | 299 | ver.glibc.major, |
| 300 | 300 | ver.glibc.minor, |
| 301 | 301 | ver.glibc.patch, |
| 302 | 302 | }); |
| 303 | 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 | 746 | try wip.finish(); |
| 747 | 747 | } |
| 748 | 748 | |
| 749 | fn genModuleLevelAssembly(object: *Object) !void { | |
| 750 | const writer = object.builder.setModuleAsm(); | |
| 749 | fn genModuleLevelAssembly(object: *Object) Allocator.Error!void { | |
| 750 | const b = &object.builder; | |
| 751 | const gpa = b.gpa; | |
| 752 | b.module_asm.clearRetainingCapacity(); | |
| 751 | 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 | 763 | pub const EmitOptions = struct { |
| ... | ... | @@ -939,7 +945,9 @@ pub const Object = struct { |
| 939 | 945 | if (std.mem.eql(u8, path, "-")) { |
| 940 | 946 | o.builder.dump(); |
| 941 | 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 | 2688 | } |
| 2681 | 2689 | |
| 2682 | 2690 | fn allocTypeName(o: *Object, ty: Type) Allocator.Error![:0]const u8 { |
| 2683 | var buffer = std.ArrayList(u8).init(o.gpa); | |
| 2684 | errdefer buffer.deinit(); | |
| 2685 | try ty.print(buffer.writer(), o.pt); | |
| 2686 | return buffer.toOwnedSliceSentinel(0); | |
| 2691 | var aw: std.io.Writer.Allocating = .init(o.gpa); | |
| 2692 | defer aw.deinit(); | |
| 2693 | ty.print(&aw.writer, o.pt) catch |err| switch (err) { | |
| 2694 | error.WriteFailed => return error.OutOfMemory, | |
| 2695 | }; | |
| 2696 | return aw.toOwnedSliceSentinel(0); | |
| 2687 | 2697 | } |
| 2688 | 2698 | |
| 2689 | 2699 | /// If the llvm function does not exist, create it. |
| ... | ... | @@ -4482,7 +4492,7 @@ pub const Object = struct { |
| 4482 | 4492 | const target = &zcu.root_mod.resolved_target.result; |
| 4483 | 4493 | const function_index = try o.builder.addFunction( |
| 4484 | 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 | 4496 | toLlvmAddressSpace(.generic, target), |
| 4487 | 4497 | ); |
| 4488 | 4498 | |
| ... | ... | @@ -4633,7 +4643,7 @@ pub const NavGen = struct { |
| 4633 | 4643 | if (zcu.getTarget().cpu.arch.isWasm() and ty.zigTypeTag(zcu) == .@"fn") { |
| 4634 | 4644 | if (lib_name.toSlice(ip)) |lib_name_slice| { |
| 4635 | 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 | 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 | 7487 | // In the case of indirect inputs, LLVM requires the callsite to have |
| 7478 | 7488 | // an elementtype(<ty>) attribute. |
| ... | ... | @@ -7573,7 +7583,7 @@ pub const FuncGen = struct { |
| 7573 | 7583 | // we should validate the assembly in Sema; by now it is too late |
| 7574 | 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 | 7587 | if (byte == ':') { |
| 7578 | 7588 | try rendered_template.append(':'); |
| 7579 | 7589 | modifier_start = i + 1; |
| ... | ... | @@ -10370,7 +10380,7 @@ pub const FuncGen = struct { |
| 10370 | 10380 | const target = &zcu.root_mod.resolved_target.result; |
| 10371 | 10381 | const function_index = try o.builder.addFunction( |
| 10372 | 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 | 10384 | toLlvmAddressSpace(.generic, target), |
| 10375 | 10385 | ); |
| 10376 | 10386 |
src/codegen/spirv.zig+6-4| ... | ... | @@ -1260,10 +1260,12 @@ const NavGen = struct { |
| 1260 | 1260 | |
| 1261 | 1261 | // Turn a Zig type's name into a cache reference. |
| 1262 | 1262 | fn resolveTypeName(self: *NavGen, ty: Type) ![]const u8 { |
| 1263 | var name = std.ArrayList(u8).init(self.gpa); | |
| 1264 | defer name.deinit(); | |
| 1265 | try ty.print(name.writer(), self.pt); | |
| 1266 | return try name.toOwnedSlice(); | |
| 1263 | var aw: std.io.Writer.Allocating = .init(self.gpa); | |
| 1264 | defer aw.deinit(); | |
| 1265 | ty.print(&aw.writer, self.pt) catch |err| switch (err) { | |
| 1266 | error.WriteFailed => return error.OutOfMemory, | |
| 1267 | }; | |
| 1268 | return try aw.toOwnedSlice(); | |
| 1267 | 1269 | } |
| 1268 | 1270 | |
| 1269 | 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 | 938 | } |
| 939 | 939 | |
| 940 | 940 | fn encode(insts: []const Instruction, code: []u8) !void { |
| 941 | var stream = std.io.fixedBufferStream(code); | |
| 942 | const writer = stream.writer(); | |
| 941 | var stream: std.io.Writer = .fixed(code); | |
| 943 | 942 | for (insts) |inst| { |
| 944 | try inst.encode(writer, .{}); | |
| 943 | try inst.encode(&stream, .{}); | |
| 945 | 944 | } |
| 946 | 945 | } |
| 947 | 946 |