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/Sema.zig | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/Sema.zig') diff --git a/src/Sema.zig b/src/Sema.zig index fa25e1e403..3dbbdaeb27 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -14034,7 +14034,17 @@ fn zirWasmMemorySize( ) CompileError!Air.Inst.Ref { const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; const src: LazySrcLoc = .{ .node_offset = extra.node }; - return sema.fail(block, src, "TODO: implement Sema.zirWasmMemorySize", .{}); + + const operand = try sema.resolveInt(block, src, extra.operand, Type.u32); + const index = @intCast(u32, operand); + try sema.requireRuntimeBlock(block, src); + return block.addInst(.{ + .tag = .wasm_memory_size, + .data = .{ .ty_pl = .{ + .ty = try sema.addType(Type.u32), + .payload = try sema.addExtra(Air.WasmMemoryIndex{ .index = index }), + } }, + }); } fn zirWasmMemoryGrow( -- cgit v1.2.3