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 {...@@ -3634,14 +3634,12 @@ fn airCmpVector(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
3634fn airCmpLtErrorsLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {3634fn airCmpLtErrorsLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
3635 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;3635 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
3636 const operand = try func.resolveInst(un_op);3636 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
3640 try func.emitWValue(operand);3638 try func.emitWValue(operand);
3641 const pt = func.pt;3639 const pt = func.pt;
3642 const err_int_ty = try pt.errorIntType();3640 const err_int_ty = try pt.errorIntType();
3643 const errors_len_val = try func.load(errors_len, err_int_ty, 0);3641 try func.addTag(.errors_len);
3644 const result = try func.cmp(.stack, errors_len_val, err_int_ty, .lt);3642 const result = try func.cmp(.stack, .stack, err_int_ty, .lt);
36453643
3646 return func.finishAir(inst, result, &.{un_op});3644 return func.finishAir(inst, result, &.{un_op});
3647}3645}
src/arch/wasm/Emit.zig+10
...@@ -70,6 +70,16 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -70,6 +70,16 @@ pub fn lowerToCode(emit: *Emit) Error!void {
70 inst += 1;70 inst += 1;
71 continue :loop tags[inst];71 continue :loop tags[inst];
72 },72 },
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 },
73 .br_if, .br, .memory_grow, .memory_size => {83 .br_if, .br, .memory_grow, .memory_size => {
74 try code.ensureUnusedCapacity(gpa, 11);84 try code.ensureUnusedCapacity(gpa, 11);
75 code.appendAssumeCapacity(@intFromEnum(tags[inst]));85 code.appendAssumeCapacity(@intFromEnum(tags[inst]));
src/arch/wasm/Mir.zig+4
...@@ -84,6 +84,10 @@ pub const Inst = struct {...@@ -84,6 +84,10 @@ pub const Inst = struct {
84 ///84 ///
85 /// Uses `payload` of which the payload type is `DbgLineColumn`85 /// Uses `payload` of which the payload type is `DbgLineColumn`
86 dbg_line,86 dbg_line,
87 /// Lowers to an i32_const containing the number of unique Zig error
88 /// names.
89 /// Uses `tag`.
90 errors_len,
87 /// Represents the end of a function body or an initialization expression91 /// Represents the end of a function body or an initialization expression
88 ///92 ///
89 /// Uses `tag` (no additional data).93 /// Uses `tag` (no additional data).