From e24f635c758242b9c8d8aacac668d31dead7623e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 6 Dec 2024 23:50:30 -0800 Subject: [PATCH] wasm: implement errors_len as a MIR opcode with no linker involvement --- src/arch/wasm/CodeGen.zig | 6 ++---- src/arch/wasm/Emit.zig | 10 ++++++++++ src/arch/wasm/Mir.zig | 4 ++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig index 5990e683c9..2f23f5267c 100644 --- a/src/arch/wasm/CodeGen.zig +++ b/src/arch/wasm/CodeGen.zig @@ -3634,14 +3634,12 @@ fn airCmpVector(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { fn airCmpLtErrorsLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; const operand = try func.resolveInst(un_op); - const sym_index = try func.wasm.getGlobalSymbol("__zig_errors_len", null); - const errors_len: WValue = .{ .memory = @intFromEnum(sym_index) }; try func.emitWValue(operand); const pt = func.pt; const err_int_ty = try pt.errorIntType(); - const errors_len_val = try func.load(errors_len, err_int_ty, 0); - const result = try func.cmp(.stack, errors_len_val, err_int_ty, .lt); + try func.addTag(.errors_len); + const result = try func.cmp(.stack, .stack, err_int_ty, .lt); return func.finishAir(inst, result, &.{un_op}); } diff --git a/src/arch/wasm/Emit.zig b/src/arch/wasm/Emit.zig index 46ae20ccbf..55138a5dbe 100644 --- a/src/arch/wasm/Emit.zig +++ b/src/arch/wasm/Emit.zig @@ -70,6 +70,16 @@ pub fn lowerToCode(emit: *Emit) Error!void { inst += 1; continue :loop tags[inst]; }, + .errors_len => { + try code.ensureUnusedCapacity(gpa, 6); + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const)); + // MIR is lowered during flush, so there is indeed only one thread at this time. + const errors_len = 1 + comp.zcu.?.intern_pool.global_error_set.getNamesFromMainThread().len; + leb.writeIleb128(code.fixedWriter(), errors_len) catch unreachable; + + inst += 1; + continue :loop tags[inst]; + }, .br_if, .br, .memory_grow, .memory_size => { try code.ensureUnusedCapacity(gpa, 11); code.appendAssumeCapacity(@intFromEnum(tags[inst])); diff --git a/src/arch/wasm/Mir.zig b/src/arch/wasm/Mir.zig index 9009056e2a..fa74c0ee7e 100644 --- a/src/arch/wasm/Mir.zig +++ b/src/arch/wasm/Mir.zig @@ -84,6 +84,10 @@ pub const Inst = struct { /// /// Uses `payload` of which the payload type is `DbgLineColumn` dbg_line, + /// Lowers to an i32_const containing the number of unique Zig error + /// names. + /// Uses `tag`. + errors_len, /// Represents the end of a function body or an initialization expression /// /// Uses `tag` (no additional data).