authorgravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2023-01-31 00:59:18+01:00
committergravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2023-01-31 00:59:18+01:00
log1f64432196e51785fe1dba442a0878c1b10f8b06
tree57612888269602d917a9d6a4b7e0ba2f97d30728
parent885d6968958e1cad4862ed1b6f1ea0b2d84c3845

wasm: correctly handle optional slices


1 files changed, 9 insertions(+), 4 deletions(-)

src/arch/wasm/CodeGen.zig+9-4
...@@ -1706,9 +1706,11 @@ fn isByRef(ty: Type, target: std.Target) bool {...@@ -1706,9 +1706,11 @@ fn isByRef(ty: Type, target: std.Target) bool {
1706 return true;1706 return true;
1707 },1707 },
1708 .Optional => {1708 .Optional => {
1709 if (ty.optionalReprIsPayload()) return false;1709 if (ty.isPtrLikeOptional()) return false;
1710 var buf: Type.Payload.ElemType = undefined;1710 var buf: Type.Payload.ElemType = undefined;
1711 return ty.optionalChild(&buf).hasRuntimeBitsIgnoreComptime();1711 const pl_type = ty.optionalChild(&buf);
1712 if (pl_type.zigTypeTag() == .ErrorSet) return false;
1713 return pl_type.hasRuntimeBitsIgnoreComptime();
1712 },1714 },
1713 .Pointer => {1715 .Pointer => {
1714 // Slices act like struct and will be passed by reference1716 // Slices act like struct and will be passed by reference
...@@ -3869,14 +3871,17 @@ fn airIsNull(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind:...@@ -3869,14 +3871,17 @@ fn airIsNull(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind:
3869/// NOTE: Leaves the result on the stack3871/// NOTE: Leaves the result on the stack
3870fn isNull(func: *CodeGen, operand: WValue, optional_ty: Type, opcode: wasm.Opcode) InnerError!WValue {3872fn isNull(func: *CodeGen, operand: WValue, optional_ty: Type, opcode: wasm.Opcode) InnerError!WValue {
3871 try func.emitWValue(operand);3873 try func.emitWValue(operand);
3874 var buf: Type.Payload.ElemType = undefined;
3875 const payload_ty = optional_ty.optionalChild(&buf);
3872 if (!optional_ty.optionalReprIsPayload()) {3876 if (!optional_ty.optionalReprIsPayload()) {
3873 var buf: Type.Payload.ElemType = undefined;
3874 const payload_ty = optional_ty.optionalChild(&buf);
3875 // When payload is zero-bits, we can treat operand as a value, rather than3877 // When payload is zero-bits, we can treat operand as a value, rather than
3876 // a pointer to the stack value3878 // a pointer to the stack value
3877 if (payload_ty.hasRuntimeBitsIgnoreComptime()) {3879 if (payload_ty.hasRuntimeBitsIgnoreComptime()) {
3878 try func.addMemArg(.i32_load8_u, .{ .offset = operand.offset(), .alignment = 1 });3880 try func.addMemArg(.i32_load8_u, .{ .offset = operand.offset(), .alignment = 1 });
3879 }3881 }
3882 } else if (payload_ty.isSlice()) {
3883 // move the ptr on top of the stack
3884 _ = try func.load(operand, Type.usize, 0);
3880 }3885 }
38813886
3882 // Compare the null value with '0'3887 // Compare the null value with '0'