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/arch/wasm/CodeGen.zig | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/arch/wasm/CodeGen.zig') diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig index 0ea0418d7f..b4917738dd 100644 --- a/src/arch/wasm/CodeGen.zig +++ b/src/arch/wasm/CodeGen.zig @@ -1672,6 +1672,7 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { .wrap_errunion_err => self.airWrapErrUnionErr(inst), .wasm_memory_size => self.airWasmMemorySize(inst), + .wasm_memory_grow => self.airWasmMemoryGrow(inst), .add_sat, .sub_sat, @@ -3438,6 +3439,18 @@ fn airWasmMemorySize(self: *Self, inst: Air.Inst.Index) !WValue { return result; } +fn airWasmMemoryGrow(self: *Self, inst: Air.Inst.Index) !WValue { + const pl_op = self.air.instructions.items(.data)[inst].pl_op; + const extra = self.air.extraData(Air.WasmMemoryIndex, pl_op.payload).data; + const operand = try self.resolveInst(pl_op.operand); + + const result = try self.allocLocal(Type.usize); + try self.emitWValue(operand); + try self.addLabel(.memory_grow, 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