authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-05-19 13:04:53+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-05-20 17:30:22+02:00
log77b8bf2b82ffa71fc8ecf85f6780130f27dbeaf3
tree5353b57dfd93f5c7fee8b6e3b22975904866cac2
parent091595ac3711fcbc372ffe6d002890f9dd897598
signaturelock-open Commit is signed but in an unrecognized format.

spirv: ptr_sub

Implments the ptr_sub air tag. The code is unified with that of ptr_add.

2 files changed, 34 insertions(+), 9 deletions(-)

src/codegen/spirv.zig+34-8
...@@ -1743,6 +1743,7 @@ pub const DeclGen = struct {...@@ -1743,6 +1743,7 @@ pub const DeclGen = struct {
1743 .shuffle => try self.airShuffle(inst),1743 .shuffle => try self.airShuffle(inst),
17441744
1745 .ptr_add => try self.airPtrAdd(inst),1745 .ptr_add => try self.airPtrAdd(inst),
1746 .ptr_sub => try self.airPtrSub(inst),
17461747
1747 .bit_and => try self.airBinOpSimple(inst, .OpBitwiseAnd),1748 .bit_and => try self.airBinOpSimple(inst, .OpBitwiseAnd),
1748 .bit_or => try self.airBinOpSimple(inst, .OpBitwiseOr),1749 .bit_or => try self.airBinOpSimple(inst, .OpBitwiseOr),
...@@ -2126,14 +2127,7 @@ pub const DeclGen = struct {...@@ -2126,14 +2127,7 @@ pub const DeclGen = struct {
2126 return result_id;2127 return result_id;
2127 }2128 }
21282129
2129 fn airPtrAdd(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {2130 fn ptrAdd(self: *DeclGen, result_ty: Type, ptr_ty: Type, ptr_id: IdRef, offset_id: IdRef) !IdRef {
2130 if (self.liveness.isUnused(inst)) return null;
2131 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
2132 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
2133 const ptr_id = try self.resolve(bin_op.lhs);
2134 const offset_id = try self.resolve(bin_op.rhs);
2135 const ptr_ty = self.air.typeOf(bin_op.lhs);
2136 const result_ty = self.air.typeOfIndex(inst);
2137 const result_ty_ref = try self.resolveType(result_ty, .direct);2131 const result_ty_ref = try self.resolveType(result_ty, .direct);
21382132
2139 switch (ptr_ty.ptrSize()) {2133 switch (ptr_ty.ptrSize()) {
...@@ -2153,6 +2147,38 @@ pub const DeclGen = struct {...@@ -2153,6 +2147,38 @@ pub const DeclGen = struct {
2153 }2147 }
2154 }2148 }
21552149
2150 fn airPtrAdd(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2151 if (self.liveness.isUnused(inst)) return null;
2152 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
2153 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
2154 const ptr_id = try self.resolve(bin_op.lhs);
2155 const offset_id = try self.resolve(bin_op.rhs);
2156 const ptr_ty = self.air.typeOf(bin_op.lhs);
2157 const result_ty = self.air.typeOfIndex(inst);
2158
2159 return try self.ptrAdd(result_ty, ptr_ty, ptr_id, offset_id);
2160 }
2161
2162 fn airPtrSub(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2163 if (self.liveness.isUnused(inst)) return null;
2164 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
2165 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
2166 const ptr_id = try self.resolve(bin_op.lhs);
2167 const ptr_ty = self.air.typeOf(bin_op.lhs);
2168 const offset_id = try self.resolve(bin_op.rhs);
2169 const offset_ty = self.air.typeOf(bin_op.rhs);
2170 const offset_ty_ref = try self.resolveType(offset_ty, .direct);
2171 const result_ty = self.air.typeOfIndex(inst);
2172
2173 const negative_offset_id = self.spv.allocId();
2174 try self.func.body.emit(self.spv.gpa, .OpSNegate, .{
2175 .id_result_type = self.typeId(offset_ty_ref),
2176 .id_result = negative_offset_id,
2177 .operand = offset_id,
2178 });
2179 return try self.ptrAdd(result_ty, ptr_ty, ptr_id, negative_offset_id);
2180 }
2181
2156 fn cmp(2182 fn cmp(
2157 self: *DeclGen,2183 self: *DeclGen,
2158 comptime op: std.math.CompareOperator,2184 comptime op: std.math.CompareOperator,
test/behavior/pointers.zig-1
...@@ -84,7 +84,6 @@ test "assigning integer to C pointer" {...@@ -84,7 +84,6 @@ test "assigning integer to C pointer" {
8484
85test "C pointer comparison and arithmetic" {85test "C pointer comparison and arithmetic" {
86 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO86 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
87 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
8887
89 const S = struct {88 const S = struct {
90 fn doTheTest() !void {89 fn doTheTest() !void {