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

This commit is contained in:
Andrew Kelley 2024-12-06 23:50:30 -08:00
parent bffa148600
commit e24f635c75
3 changed files with 16 additions and 4 deletions

View File

@ -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});
}

View File

@ -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]));

View File

@ -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).