zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 2c2bc9c8df3d8205caf4e49f0fcf7496b3d71444 (tree)
parent 7c5d01b95e3cb47187726c52dae4abb2e5b4faaf
Author: Luuk de Gram <luuk@degram.dev>
Date:   Wed,  1 Nov 2023 19:43:03 +0100

wasm: fix bitcasting to -and from arrays

Arrays are currently always passed by reference, this means that we
always keep the value in linear memory and never load it to Wasm's
stack. Scalar values however do get lowered to Wasm's stack.
This means when bitcasting from an array to a scalar value, we must
load the memory of the array as such scalar type. To bitcast
a scalar type to an array, we allocate a new temporary in the
linear data segment, and then store the scalar value there.

Diffstat:
Msrc/arch/wasm/CodeGen.zig | 10++++++++++
1 file changed, 10 insertions(+), 0 deletions(-)

diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig @@ -3814,6 +3814,16 @@ fn airBitcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { const bitcast_result = try func.bitcast(wanted_ty, given_ty, operand); break :result try bitcast_result.toLocal(func, wanted_ty); } + const mod = func.bin_file.base.options.module.?; + if (isByRef(given_ty, mod) and !isByRef(wanted_ty, mod)) { + const loaded_memory = try func.load(operand, wanted_ty, 0); + break :result try loaded_memory.toLocal(func, wanted_ty); + } + if (!isByRef(given_ty, mod) and isByRef(wanted_ty, mod)) { + const stack_memory = try func.allocStack(wanted_ty); + try func.store(stack_memory, operand, given_ty, 0); + break :result stack_memory; + } break :result func.reuseOperand(ty_op.operand, operand); }; func.finishAir(inst, result, &.{ty_op.operand});