authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-06 23:50:30-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:35-08:00
loge24f635c758242b9c8d8aacac668d31dead7623e
tree6c6261da339a1710f83290531d1ea5137e924b5e
parentbffa14860078fe466e9f005dfc56869e195281b5

wasm: implement errors_len as a MIR opcode with no linker involvement


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

src/arch/wasm/CodeGen.zig+2-4
......@@ -3634,14 +3634,12 @@ fn airCmpVector(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
36343634fn airCmpLtErrorsLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
36353635 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
36363636 const operand = try func.resolveInst(un_op);
3637 const sym_index = try func.wasm.getGlobalSymbol("__zig_errors_len", null);
3638 const errors_len: WValue = .{ .memory = @intFromEnum(sym_index) };
36393637
36403638 try func.emitWValue(operand);
36413639 const pt = func.pt;
36423640 const err_int_ty = try pt.errorIntType();
3643 const errors_len_val = try func.load(errors_len, err_int_ty, 0);
3644 const result = try func.cmp(.stack, errors_len_val, err_int_ty, .lt);
3641 try func.addTag(.errors_len);
3642 const result = try func.cmp(.stack, .stack, err_int_ty, .lt);
36453643
36463644 return func.finishAir(inst, result, &.{un_op});
36473645}
src/arch/wasm/Emit.zig+10
......@@ -70,6 +70,16 @@ pub fn lowerToCode(emit: *Emit) Error!void {
7070 inst += 1;
7171 continue :loop tags[inst];
7272 },
73 .errors_len => {
74 try code.ensureUnusedCapacity(gpa, 6);
75 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
76 // MIR is lowered during flush, so there is indeed only one thread at this time.
77 const errors_len = 1 + comp.zcu.?.intern_pool.global_error_set.getNamesFromMainThread().len;
78 leb.writeIleb128(code.fixedWriter(), errors_len) catch unreachable;
79
80 inst += 1;
81 continue :loop tags[inst];
82 },
7383 .br_if, .br, .memory_grow, .memory_size => {
7484 try code.ensureUnusedCapacity(gpa, 11);
7585 code.appendAssumeCapacity(@intFromEnum(tags[inst]));
src/arch/wasm/Mir.zig+4
......@@ -84,6 +84,10 @@ pub const Inst = struct {
8484 ///
8585 /// Uses `payload` of which the payload type is `DbgLineColumn`
8686 dbg_line,
87 /// Lowers to an i32_const containing the number of unique Zig error
88 /// names.
89 /// Uses `tag`.
90 errors_len,
8791 /// Represents the end of a function body or an initialization expression
8892 ///
8993 /// Uses `tag` (no additional data).