authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-05-24 19:48:51+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-05-24 15:34:52-07:00
log26376c9fda910e28a686d3f772dbda4319abc16d
tree189747a20037c569d755d4c3e1827026862dc40b
parent8c49420928b29271429cc09b5d5f1447a942f8d6

wasm: use errUnionPayloadOffset and errUnionErrOffset from codegen.zig


1 files changed, 11 insertions(+), 23 deletions(-)

src/arch/wasm/CodeGen.zig+11-23
...@@ -22,6 +22,8 @@ const Liveness = @import("../../Liveness.zig");...@@ -22,6 +22,8 @@ const Liveness = @import("../../Liveness.zig");
22const Mir = @import("Mir.zig");22const Mir = @import("Mir.zig");
23const Emit = @import("Emit.zig");23const Emit = @import("Emit.zig");
24const abi = @import("abi.zig");24const abi = @import("abi.zig");
25const errUnionPayloadOffset = codegen.errUnionPayloadOffset;
26const errUnionErrOffset = codegen.errUnionErrOffset;
2527
26/// Wasm Value, created when generating an instruction28/// Wasm Value, created when generating an instruction
27const WValue = union(enum) {29const WValue = union(enum) {
...@@ -2931,7 +2933,7 @@ fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!W...@@ -2931,7 +2933,7 @@ fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!W
2931 try self.emitWValue(operand);2933 try self.emitWValue(operand);
2932 if (pl_ty.hasRuntimeBitsIgnoreComptime()) {2934 if (pl_ty.hasRuntimeBitsIgnoreComptime()) {
2933 try self.addMemArg(.i32_load16_u, .{2935 try self.addMemArg(.i32_load16_u, .{
2934 .offset = operand.offset() + errUnionErrorOffset(pl_ty, self.target),2936 .offset = operand.offset() + @intCast(u32, errUnionErrOffset(pl_ty, self.target)),
2935 .alignment = Type.anyerror.abiAlignment(self.target),2937 .alignment = Type.anyerror.abiAlignment(self.target),
2936 });2938 });
2937 }2939 }
...@@ -2959,7 +2961,7 @@ fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool)...@@ -2959,7 +2961,7 @@ fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool)
29592961
2960 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) return WValue{ .none = {} };2962 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) return WValue{ .none = {} };
29612963
2962 const pl_offset = errUnionPayloadOffset(payload_ty, self.target);2964 const pl_offset = @intCast(u32, errUnionPayloadOffset(payload_ty, self.target));
2963 if (op_is_ptr or isByRef(payload_ty, self.target)) {2965 if (op_is_ptr or isByRef(payload_ty, self.target)) {
2964 return self.buildPointerOffset(operand, pl_offset, .new);2966 return self.buildPointerOffset(operand, pl_offset, .new);
2965 }2967 }
...@@ -2983,7 +2985,7 @@ fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) In...@@ -2983,7 +2985,7 @@ fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) In
2983 return operand;2985 return operand;
2984 }2986 }
29852987
2986 return self.load(operand, Type.anyerror, errUnionErrorOffset(payload_ty, self.target));2988 return self.load(operand, Type.anyerror, @intCast(u32, errUnionErrOffset(payload_ty, self.target)));
2987}2989}
29882990
2989fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {2991fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
...@@ -3003,13 +3005,13 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {...@@ -3003,13 +3005,13 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3003 }3005 }
30043006
3005 const err_union = try self.allocStack(err_ty);3007 const err_union = try self.allocStack(err_ty);
3006 const payload_ptr = try self.buildPointerOffset(err_union, errUnionPayloadOffset(pl_ty, self.target), .new);3008 const payload_ptr = try self.buildPointerOffset(err_union, @intCast(u32, errUnionPayloadOffset(pl_ty, self.target)), .new);
3007 try self.store(payload_ptr, operand, pl_ty, 0);3009 try self.store(payload_ptr, operand, pl_ty, 0);
30083010
3009 // ensure we also write '0' to the error part, so any present stack value gets overwritten by it.3011 // ensure we also write '0' to the error part, so any present stack value gets overwritten by it.
3010 try self.emitWValue(err_union);3012 try self.emitWValue(err_union);
3011 try self.addImm32(0);3013 try self.addImm32(0);
3012 const err_val_offset = errUnionErrorOffset(pl_ty, self.target);3014 const err_val_offset = @intCast(u32, errUnionErrOffset(pl_ty, self.target));
3013 try self.addMemArg(.i32_store16, .{ .offset = err_union.offset() + err_val_offset, .alignment = 2 });3015 try self.addMemArg(.i32_store16, .{ .offset = err_union.offset() + err_val_offset, .alignment = 2 });
30143016
3015 return err_union;3017 return err_union;
...@@ -3029,10 +3031,10 @@ fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {...@@ -3029,10 +3031,10 @@ fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
30293031
3030 const err_union = try self.allocStack(err_ty);3032 const err_union = try self.allocStack(err_ty);
3031 // store error value3033 // store error value
3032 try self.store(err_union, operand, Type.anyerror, errUnionErrorOffset(pl_ty, self.target));3034 try self.store(err_union, operand, Type.anyerror, @intCast(u32, errUnionErrOffset(pl_ty, self.target)));
30333035
3034 // write 'undefined' to the payload3036 // write 'undefined' to the payload
3035 const payload_ptr = try self.buildPointerOffset(err_union, errUnionPayloadOffset(pl_ty, self.target), .new);3037 const payload_ptr = try self.buildPointerOffset(err_union, @intCast(u32, errUnionPayloadOffset(pl_ty, self.target)), .new);
3036 const len = @intCast(u32, err_ty.errorUnionPayload().abiSize(self.target));3038 const len = @intCast(u32, err_ty.errorUnionPayload().abiSize(self.target));
3037 try self.memset(payload_ptr, .{ .imm32 = len }, .{ .imm32 = 0xaaaaaaaa });3039 try self.memset(payload_ptr, .{ .imm32 = len }, .{ .imm32 = 0xaaaaaaaa });
30383040
...@@ -3984,7 +3986,7 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue...@@ -3984,7 +3986,7 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue
3984 operand,3986 operand,
3985 .{ .imm32 = 0 },3987 .{ .imm32 = 0 },
3986 Type.anyerror,3988 Type.anyerror,
3987 errUnionErrorOffset(payload_ty, self.target),3989 @intCast(u32, errUnionErrOffset(payload_ty, self.target)),
3988 );3990 );
39893991
3990 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };3992 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
...@@ -3993,7 +3995,7 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue...@@ -3993,7 +3995,7 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue
3993 return operand;3995 return operand;
3994 }3996 }
39953997
3996 return self.buildPointerOffset(operand, errUnionPayloadOffset(payload_ty, self.target), .new);3998 return self.buildPointerOffset(operand, @intCast(u32, errUnionPayloadOffset(payload_ty, self.target)), .new);
3997}3999}
39984000
3999fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {4001fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
...@@ -4621,17 +4623,3 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !WValue {...@@ -4621,17 +4623,3 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !WValue {
4621 } });4623 } });
4622 return WValue{ .none = {} };4624 return WValue{ .none = {} };
4623}4625}
4624
4625fn errUnionPayloadOffset(payload_ty: Type, target: std.Target) u32 {
4626 if (Type.anyerror.abiAlignment(target) > payload_ty.abiAlignment(target)) {
4627 return @intCast(u32, Type.anyerror.abiSize(target));
4628 }
4629 return 0;
4630}
4631
4632fn errUnionErrorOffset(payload_ty: Type, target: std.Target) u32 {
4633 if (Type.anyerror.abiAlignment(target) > payload_ty.abiAlignment(target)) {
4634 return 0;
4635 }
4636 return @intCast(u32, payload_ty.abiSize(target));
4637}