Commit e24f635c75

Andrew Kelley <andrew@ziglang.org>
2024-12-07 08:50:30
wasm: implement errors_len as a MIR opcode with no linker involvement
1 parent bffa148
Changed files (3)
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});
 }
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]));
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).