authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-07 14:42:30-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:35-08:00
logc443a7a57fab7118a9793b7f77b7ab817235a896
tree66e61304cc2a267ef0a4eee82ba1d15ad3968967
parentd45e5ac5eb125c0dc69d29e8a0fbb363a18ebeb6

wasm: move error_name lowering to Emit phase


3 files changed, 37 insertions(+), 16 deletions(-)

src/arch/wasm/CodeGen.zig+2-8
......@@ -5900,12 +5900,7 @@ fn airBitReverse(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
59005900
59015901fn airErrorName(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
59025902 const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
5903
59045903 const operand = try cg.resolveInst(un_op);
5905 // First retrieve the symbol index to the error name table
5906 // that will be used to emit a relocation for the pointer
5907 // to the error name table.
5908 //
59095904 // Each entry to this table is a slice (ptr+len).
59105905 // The operand in this instruction represents the index within this table.
59115906 // This means to get the final name, we emit the base pointer and then perform
......@@ -5914,12 +5909,11 @@ fn airErrorName(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
59145909 // As the names are global and the slice elements are constant, we do not have
59155910 // to make a copy of the ptr+value but can point towards them directly.
59165911 const pt = cg.pt;
5917 const error_table_symbol = try cg.wasm.getErrorTableSymbol(pt);
59185912 const name_ty = Type.slice_const_u8_sentinel_0;
59195913 const abi_size = name_ty.abiSize(pt.zcu);
59205914
5921 const error_name_value: WValue = .{ .memory = error_table_symbol }; // emitting this will create a relocation
5922 try cg.emitWValue(error_name_value);
5915 // Lowers to a i32.const or i64.const with the error table memory address.
5916 try cg.addTag(.error_name_table_ref);
59235917 try cg.emitWValue(operand);
59245918 switch (cg.ptr_size) {
59255919 .wasm32 => {
src/arch/wasm/Emit.zig+29-8
......@@ -27,6 +27,8 @@ pub fn lowerToCode(emit: *Emit) Error!void {
2727 const comp = wasm.base.comp;
2828 const gpa = comp.gpa;
2929 const is_obj = comp.config.output_mode == .Obj;
30 const target = &comp.root_mod.resolved_target.result;
31 const is_wasm32 = target.cpu.arch == .wasm32;
3032
3133 const tags = mir.instructions.items(.tag);
3234 const datas = mir.instructions.items(.data);
......@@ -56,12 +58,12 @@ pub fn lowerToCode(emit: *Emit) Error!void {
5658 continue :loop tags[inst];
5759 },
5860 .nav_ref => {
59 try navRefOff(wasm, code, .{ .ip_index = datas[inst].ip_index, .offset = 0 });
61 try navRefOff(wasm, code, .{ .ip_index = datas[inst].ip_index, .offset = 0 }, is_wasm32);
6062 inst += 1;
6163 continue :loop tags[inst];
6264 },
6365 .nav_ref_off => {
64 try navRefOff(wasm, code, mir.extraData(Mir.NavRefOff, datas[inst].payload).data);
66 try navRefOff(wasm, code, mir.extraData(Mir.NavRefOff, datas[inst].payload).data, is_wasm32);
6567 inst += 1;
6668 continue :loop tags[inst];
6769 },
......@@ -80,6 +82,29 @@ pub fn lowerToCode(emit: *Emit) Error!void {
8082 inst += 1;
8183 continue :loop tags[inst];
8284 },
85 .error_name_table_ref => {
86 try code.ensureUnusedCapacity(gpa, 11);
87 const opcode: std.wasm.Opcode = if (is_wasm32) .i32_const else .i64_const;
88 code.appendAssumeCapacity(@intFromEnum(opcode));
89 if (is_obj) {
90 try wasm.out_relocs.append(gpa, .{
91 .offset = @intCast(code.items.len),
92 .index = try wasm.errorNameTableSymbolIndex(),
93 .tag = if (is_wasm32) .MEMORY_ADDR_LEB else .MEMORY_ADDR_LEB64,
94 .addend = 0,
95 });
96 code.appendNTimesAssumeCapacity(0, if (is_wasm32) 5 else 10);
97
98 inst += 1;
99 continue :loop tags[inst];
100 } else {
101 const addr = try wasm.errorNameTableAddr();
102 leb.writeIleb128(code.fixedWriter(), addr) catch unreachable;
103
104 inst += 1;
105 continue :loop tags[inst];
106 }
107 },
83108 .br_if, .br, .memory_grow, .memory_size => {
84109 try code.ensureUnusedCapacity(gpa, 11);
85110 code.appendAssumeCapacity(@intFromEnum(tags[inst]));
......@@ -607,11 +632,9 @@ fn encodeMemArg(code: *std.ArrayListUnmanaged(u8), mem_arg: Mir.MemArg) void {
607632 leb.writeUleb128(code.fixedWriter(), mem_arg.offset) catch unreachable;
608633}
609634
610fn uavRefOff(wasm: *link.File.Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir.UavRefOff) !void {
635fn uavRefOff(wasm: *link.File.Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir.UavRefOff, is_wasm32: bool) !void {
611636 const comp = wasm.base.comp;
612637 const gpa = comp.gpa;
613 const target = comp.root_mod.resolved_target.result;
614 const is_wasm32 = target.cpu.arch == .wasm32;
615638 const is_obj = comp.config.output_mode == .Obj;
616639 const opcode: std.wasm.Opcode = if (is_wasm32) .i32_const else .i64_const;
617640
......@@ -636,13 +659,12 @@ fn uavRefOff(wasm: *link.File.Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir
636659 leb.writeUleb128(code.fixedWriter(), addr + data.offset) catch unreachable;
637660}
638661
639fn navRefOff(wasm: *link.File.Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir.NavRefOff) !void {
662fn navRefOff(wasm: *link.File.Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir.NavRefOff, is_wasm32: bool) !void {
640663 const comp = wasm.base.comp;
641664 const zcu = comp.zcu.?;
642665 const ip = &zcu.intern_pool;
643666 const gpa = comp.gpa;
644667 const is_obj = comp.config.output_mode == .Obj;
645 const target = &comp.root_mod.resolved_target.result;
646668 const nav_ty = ip.getNav(data.nav_index).typeOf(ip);
647669
648670 try code.ensureUnusedCapacity(gpa, 11);
......@@ -663,7 +685,6 @@ fn navRefOff(wasm: *link.File.Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir
663685 leb.writeUleb128(code.fixedWriter(), addr + data.offset) catch unreachable;
664686 }
665687 } else {
666 const is_wasm32 = target.cpu.arch == .wasm32;
667688 const opcode: std.wasm.Opcode = if (is_wasm32) .i32_const else .i64_const;
668689 code.appendAssumeCapacity(@intFromEnum(opcode));
669690 if (is_obj) {
src/arch/wasm/Mir.zig+6
......@@ -88,6 +88,12 @@ pub const Inst = struct {
8888 /// names.
8989 /// Uses `tag`.
9090 errors_len,
91 /// Lowers to an i32_const (wasm32) or i64_const (wasm64) containing
92 /// the base address of the table of error code names, with each
93 /// element being a null-terminated slice.
94 ///
95 /// Uses `tag`.
96 error_name_table_ref,
9197 /// Represents the end of a function body or an initialization expression
9298 ///
9399 /// Uses `tag` (no additional data).