From 43cb19ea4da63dcaa8a18a06e3ab23f1c822c1fe Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Thu, 3 Mar 2022 19:10:58 +0100 Subject: wasm: Implement `@wasmMemoryGrow` builtin Similarly to the other wasm builtin, this implements the grow variation where the memory index is a comptime known value. The operand as well as the result are runtime values. This also verifies during semantic analysis the target we're building for is wasm, or else emits a compilation error. This means that other backends do not have to handle this AIR instruction, other than the wasm and LLVM backends. --- src/Sema.zig | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/Sema.zig') diff --git a/src/Sema.zig b/src/Sema.zig index 3dbbdaeb27..a8dc872a99 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -14034,6 +14034,9 @@ fn zirWasmMemorySize( ) CompileError!Air.Inst.Ref { const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; const src: LazySrcLoc = .{ .node_offset = extra.node }; + if (!sema.mod.getTarget().isWasm()) { + return sema.fail(block, src, "builtin '@wasmMemorySize' is a wasm feature only", .{}); + } const operand = try sema.resolveInt(block, src, extra.operand, Type.u32); const index = @intCast(u32, operand); @@ -14054,7 +14057,22 @@ fn zirWasmMemoryGrow( ) CompileError!Air.Inst.Ref { const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data; const src: LazySrcLoc = .{ .node_offset = extra.node }; - return sema.fail(block, src, "TODO: implement Sema.zirWasmMemoryGrow", .{}); + if (!sema.mod.getTarget().isWasm()) { + return sema.fail(block, src, "builtin '@wasmMemoryGrow' is a wasm feature only", .{}); + } + + const index_arg = try sema.resolveInt(block, src, extra.lhs, Type.u32); + const index = @intCast(u32, index_arg); + const delta_arg = sema.resolveInst(extra.rhs); + + try sema.requireRuntimeBlock(block, src); + return block.addInst(.{ + .tag = .wasm_memory_grow, + .data = .{ .pl_op = .{ + .operand = delta_arg, + .payload = try sema.addExtra(Air.WasmMemoryIndex{ .index = index }), + } }, + }); } fn zirPrefetch( -- cgit v1.2.3