From ec4c30ae483e6700a1fd1d5edaadbb042790c52e Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Wed, 2 Mar 2022 22:18:45 +0100 Subject: wasm: Implement `@wasmMemorySize()` builtin This implements the `wasmMemorySize` builtin, in Sema and the Wasm backend. The Stage2 implementation differs from stage1 in the way that `index` must be a comptime value. The stage1 variant is incorrect, as the index is part of the instruction encoding, and therefore, cannot be a runtime value. --- src/arch/wasm/CodeGen.zig | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/arch/wasm/CodeGen.zig') diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig index 4f19aeca6b..0ea0418d7f 100644 --- a/src/arch/wasm/CodeGen.zig +++ b/src/arch/wasm/CodeGen.zig @@ -1671,6 +1671,8 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { .wrap_errunion_payload => self.airWrapErrUnionPayload(inst), .wrap_errunion_err => self.airWrapErrUnionErr(inst), + .wasm_memory_size => self.airWasmMemorySize(inst), + .add_sat, .sub_sat, .mul_sat, @@ -3426,6 +3428,16 @@ fn airPrefetch(self: *Self, inst: Air.Inst.Index) InnerError!WValue { return WValue{ .none = {} }; } +fn airWasmMemorySize(self: *Self, inst: Air.Inst.Index) !WValue { + const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; + const extra = self.air.extraData(Air.WasmMemoryIndex, ty_pl.payload).data; + + const result = try self.allocLocal(Type.usize); + try self.addLabel(.memory_size, extra.index); + try self.addLabel(.local_set, result.local); + return result; +} + fn cmpOptionals(self: *Self, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.math.CompareOperator) InnerError!WValue { assert(operand_ty.hasRuntimeBits()); assert(op == .eq or op == .neq); -- cgit v1.2.3