authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-01-18 01:25:48+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-01-18 01:25:48+01:00
loge69cb9105a716dfd4a8cc2684417545b2438f606
treed80540e1ff8f325f753ee3537daac3d8cbf0f975
parentf4e051e35d8019c9a8d99ccae8f2e9d8f032629a
parent3145fa97c21704d8822db928e5f988f22497b1b8
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10616 from ziglang/stage2-x86_64-array-to-slice

stage2: implement airArrayToSlice for x86_64

4 files changed, 137 insertions(+), 46 deletions(-)

src/arch/x86_64/CodeGen.zig+27-6
......@@ -1659,6 +1659,18 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
16591659 },
16601660 }
16611661 },
1662 .register => |src_reg| {
1663 const abi_size = value_ty.abiSize(self.target.*);
1664 _ = try self.addInst(.{
1665 .tag = .mov,
1666 .ops = (Mir.Ops{
1667 .reg1 = reg.to64(),
1668 .reg2 = registerAlias(src_reg, @intCast(u32, abi_size)),
1669 .flags = 0b10,
1670 }).encode(),
1671 .data = .{ .imm = 0 },
1672 });
1673 },
16621674 else => |other| {
16631675 return self.fail("TODO implement set pointee with {}", .{other});
16641676 },
......@@ -1822,7 +1834,7 @@ fn genBinMathOp(self: *Self, inst: Air.Inst.Index, op_lhs: Air.Inst.Ref, op_rhs:
18221834 const dst_ty = self.air.typeOfIndex(inst);
18231835 const air_tags = self.air.instructions.items(.tag);
18241836 switch (air_tags[inst]) {
1825 .add, .addwrap => try self.genBinMathOpMir(.add, dst_ty, .unsigned, dst_mcv, src_mcv),
1837 .add, .addwrap, .ptr_add => try self.genBinMathOpMir(.add, dst_ty, .unsigned, dst_mcv, src_mcv),
18261838 .bool_or, .bit_or => try self.genBinMathOpMir(.@"or", dst_ty, .unsigned, dst_mcv, src_mcv),
18271839 .bool_and, .bit_and => try self.genBinMathOpMir(.@"and", dst_ty, .unsigned, dst_mcv, src_mcv),
18281840 .sub, .subwrap => try self.genBinMathOpMir(.sub, dst_ty, .unsigned, dst_mcv, src_mcv),
......@@ -3125,7 +3137,6 @@ fn setRegOrMem(self: *Self, ty: Type, loc: MCValue, val: MCValue) !void {
31253137fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerError!void {
31263138 switch (mcv) {
31273139 .dead => unreachable,
3128 .ptr_stack_offset => unreachable,
31293140 .ptr_embedded_in_code => unreachable,
31303141 .unreach, .none => return, // Nothing to do.
31313142 .undef => {
......@@ -3241,6 +3252,10 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
32413252 }
32423253 return self.fail("TODO implement memcpy for setting stack from {}", .{mcv});
32433254 },
3255 .ptr_stack_offset => {
3256 const reg = try self.copyToTmpRegister(ty, mcv);
3257 return self.genSetStack(ty, stack_offset, MCValue{ .register = reg });
3258 },
32443259 .stack_offset => |off| {
32453260 if (stack_offset == off) {
32463261 // Copy stack variable to itself; nothing to do.
......@@ -3618,10 +3633,16 @@ fn airBitCast(self: *Self, inst: Air.Inst.Index) !void {
36183633
36193634fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void {
36203635 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3621 const result: MCValue = if (self.liveness.isUnused(inst))
3622 .dead
3623 else
3624 return self.fail("TODO implement airArrayToSlice for {}", .{self.target.cpu.arch});
3636 const ptr_ty = self.air.typeOf(ty_op.operand);
3637 const ptr = try self.resolveInst(ty_op.operand);
3638 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else blk: {
3639 const stack_offset = try self.allocMem(inst, 16, 16);
3640 const array_ty = ptr_ty.childType();
3641 const array_len = array_ty.arrayLenIncludingSentinel();
3642 try self.genSetStack(Type.initTag(.usize), stack_offset + 8, ptr);
3643 try self.genSetStack(Type.initTag(.u64), stack_offset + 16, .{ .immediate = array_len });
3644 break :blk .{ .stack_offset = stack_offset };
3645 };
36253646 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
36263647}
36273648
test/behavior.zig+2-2
......@@ -16,11 +16,11 @@ test {
1616 _ = @import("behavior/type.zig");
1717 _ = @import("behavior/bugs/655.zig");
1818 _ = @import("behavior/bool.zig");
19 _ = @import("behavior/align.zig");
20 _ = @import("behavior/array.zig");
1921
2022 if (builtin.zig_backend != .stage2_arm and builtin.zig_backend != .stage2_x86_64) {
2123 // Tests that pass for stage1, llvm backend, C backend, wasm backend.
22 _ = @import("behavior/align.zig");
23 _ = @import("behavior/array.zig");
2424 _ = @import("behavior/basic.zig");
2525 _ = @import("behavior/bitcast.zig");
2626 _ = @import("behavior/bugs/624.zig");
test/behavior/align.zig+66-38
......@@ -6,6 +6,8 @@ const native_arch = builtin.target.cpu.arch;
66var foo: u8 align(4) = 100;
77
88test "global variable alignment" {
9 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
10
911 comptime try expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
1012 comptime try expect(@TypeOf(&foo) == *align(4) u8);
1113 {
......@@ -20,10 +22,14 @@ test "global variable alignment" {
2022}
2123
2224test "default alignment allows unspecified in type syntax" {
25 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
26
2327 try expect(*u32 == *align(@alignOf(u32)) u32);
2428}
2529
2630test "implicitly decreasing pointer alignment" {
31 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
32
2733 const a: u32 align(4) = 3;
2834 const b: u32 align(8) = 4;
2935 try expect(addUnaligned(&a, &b) == 7);
......@@ -33,16 +39,9 @@ fn addUnaligned(a: *align(1) const u32, b: *align(1) const u32) u32 {
3339 return a.* + b.*;
3440}
3541
36test "implicitly decreasing slice alignment" {
37 const a: u32 align(4) = 3;
38 const b: u32 align(8) = 4;
39 try expect(addUnalignedSlice(@as(*const [1]u32, &a)[0..], @as(*const [1]u32, &b)[0..]) == 7);
40}
41fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 {
42 return a[0] + b[0];
43}
44
4542test "@alignCast pointers" {
43 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
44
4645 var x: u32 align(4) = 1;
4746 expectsOnly1(&x);
4847 try expect(x == 2);
......@@ -54,48 +53,25 @@ fn expects4(x: *align(4) u32) void {
5453 x.* += 1;
5554}
5655
57test "specifying alignment allows pointer cast" {
58 try testBytesAlign(0x33);
59}
60fn testBytesAlign(b: u8) !void {
61 var bytes align(4) = [_]u8{ b, b, b, b };
62 const ptr = @ptrCast(*u32, &bytes[0]);
63 try expect(ptr.* == 0x33333333);
64}
65
66test "@alignCast slices" {
67 var array align(4) = [_]u32{ 1, 1 };
68 const slice = array[0..];
69 sliceExpectsOnly1(slice);
70 try expect(slice[0] == 2);
71}
72fn sliceExpectsOnly1(slice: []align(1) u32) void {
73 sliceExpects4(@alignCast(4, slice));
74}
75fn sliceExpects4(slice: []align(4) u32) void {
76 slice[0] += 1;
77}
78
7956test "alignment of structs" {
57 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
58
8059 try expect(@alignOf(struct {
8160 a: i32,
8261 b: *i32,
8362 }) == @alignOf(usize));
8463}
8564
86test "return error union with 128-bit integer" {
87 try expect(3 == try give());
88}
89fn give() anyerror!u128 {
90 return 3;
91}
92
9365test "alignment of >= 128-bit integer type" {
66 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
67
9468 try expect(@alignOf(u128) == 16);
9569 try expect(@alignOf(u129) == 16);
9670}
9771
9872test "alignment of struct with 128-bit field" {
73 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
74
9975 try expect(@alignOf(struct {
10076 x: u128,
10177 }) == 16);
......@@ -108,6 +84,8 @@ test "alignment of struct with 128-bit field" {
10884}
10985
11086test "size of extern struct with 128-bit field" {
87 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
88
11189 try expect(@sizeOf(extern struct {
11290 x: u128,
11391 y: u8,
......@@ -122,12 +100,16 @@ test "size of extern struct with 128-bit field" {
122100}
123101
124102test "@ptrCast preserves alignment of bigger source" {
103 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
104
125105 var x: u32 align(16) = 1234;
126106 const ptr = @ptrCast(*u8, &x);
127107 try expect(@TypeOf(ptr) == *align(16) u8);
128108}
129109
130110test "alignstack" {
111 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
112
131113 try expect(fnWithAlignedStack() == 1234);
132114}
133115
......@@ -135,3 +117,49 @@ fn fnWithAlignedStack() i32 {
135117 @setAlignStack(256);
136118 return 1234;
137119}
120
121test "implicitly decreasing slice alignment" {
122 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
123
124 const a: u32 align(4) = 3;
125 const b: u32 align(8) = 4;
126 try expect(addUnalignedSlice(@as(*const [1]u32, &a)[0..], @as(*const [1]u32, &b)[0..]) == 7);
127}
128fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 {
129 return a[0] + b[0];
130}
131
132test "specifying alignment allows pointer cast" {
133 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
134
135 try testBytesAlign(0x33);
136}
137fn testBytesAlign(b: u8) !void {
138 var bytes align(4) = [_]u8{ b, b, b, b };
139 const ptr = @ptrCast(*u32, &bytes[0]);
140 try expect(ptr.* == 0x33333333);
141}
142
143test "@alignCast slices" {
144 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
145
146 var array align(4) = [_]u32{ 1, 1 };
147 const slice = array[0..];
148 sliceExpectsOnly1(slice);
149 try expect(slice[0] == 2);
150}
151fn sliceExpectsOnly1(slice: []align(1) u32) void {
152 sliceExpects4(@alignCast(4, slice));
153}
154fn sliceExpects4(slice: []align(4) u32) void {
155 slice[0] += 1;
156}
157
158test "return error union with 128-bit integer" {
159 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
160
161 try expect(3 == try give());
162}
163fn give() anyerror!u128 {
164 return 3;
165}
test/behavior/array.zig+42
......@@ -5,7 +5,23 @@ const mem = std.mem;
55const expect = testing.expect;
66const expectEqual = testing.expectEqual;
77
8test "array to slice" {
9 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
10
11 const a: u32 align(4) = 3;
12 const b: u32 align(8) = 4;
13 const a_slice: []align(1) const u32 = @as(*const [1]u32, &a)[0..];
14 const b_slice: []align(1) const u32 = @as(*const [1]u32, &b)[0..];
15 try expect(a_slice[0] + b_slice[0] == 7);
16
17 const d: []const u32 = &[2]u32{ 1, 2 };
18 const e: []const u32 = &[3]u32{ 3, 4, 5 };
19 try expect(d[0] + e[0] + d[1] + e[1] == 10);
20}
21
822test "arrays" {
23 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
24
925 var array: [5]u32 = undefined;
1026
1127 var i: u32 = 0;
......@@ -30,6 +46,8 @@ fn getArrayLen(a: []const u32) usize {
3046}
3147
3248test "array init with mult" {
49 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
50
3351 const a = 'a';
3452 var i: [8]u8 = [2]u8{ a, 'b' } ** 4;
3553 try expect(std.mem.eql(u8, &i, "abababab"));
......@@ -39,6 +57,8 @@ test "array init with mult" {
3957}
4058
4159test "array literal with explicit type" {
60 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
61
4262 const hex_mult: [4]u16 = .{ 4096, 256, 16, 1 };
4363
4464 try expect(hex_mult.len == 4);
......@@ -46,6 +66,8 @@ test "array literal with explicit type" {
4666}
4767
4868test "array literal with inferred length" {
69 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
70
4971 const hex_mult = [_]u16{ 4096, 256, 16, 1 };
5072
5173 try expect(hex_mult.len == 4);
......@@ -53,6 +75,8 @@ test "array literal with inferred length" {
5375}
5476
5577test "array dot len const expr" {
78 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
79
5680 try expect(comptime x: {
5781 break :x some_array.len == 4;
5882 });
......@@ -64,12 +88,16 @@ const ArrayDotLenConstExpr = struct {
6488const some_array = [_]u8{ 0, 1, 2, 3 };
6589
6690test "array literal with specified size" {
91 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
92
6793 var array = [2]u8{ 1, 2 };
6894 try expect(array[0] == 1);
6995 try expect(array[1] == 2);
7096}
7197
7298test "array len field" {
99 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
100
73101 var arr = [4]u8{ 0, 0, 0, 0 };
74102 var ptr = &arr;
75103 try expect(arr.len == 4);
......@@ -79,6 +107,8 @@ test "array len field" {
79107}
80108
81109test "array with sentinels" {
110 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
111
82112 const S = struct {
83113 fn doTheTest(is_ct: bool) !void {
84114 if (is_ct or builtin.zig_is_stage2) {
......@@ -106,6 +136,8 @@ test "array with sentinels" {
106136}
107137
108138test "void arrays" {
139 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
140
109141 var array: [4]void = undefined;
110142 array[0] = void{};
111143 array[1] = array[2];
......@@ -114,6 +146,8 @@ test "void arrays" {
114146}
115147
116148test "nested arrays" {
149 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
150
117151 if (builtin.zig_backend == .stage2_wasm) {
118152 // TODO this is a recent stage2 test case regression due to an enhancement;
119153 // now arrays are properly detected as comptime. This exercised a new code
......@@ -132,6 +166,8 @@ test "nested arrays" {
132166}
133167
134168test "implicit comptime in array type size" {
169 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
170
135171 var arr: [plusOne(10)]bool = undefined;
136172 try expect(arr.len == 11);
137173}
......@@ -141,6 +177,8 @@ fn plusOne(x: u32) u32 {
141177}
142178
143179test "single-item pointer to array indexing and slicing" {
180 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
181
144182 try testSingleItemPtrArrayIndexSlice();
145183 comptime try testSingleItemPtrArrayIndexSlice();
146184}
......@@ -164,6 +202,8 @@ fn doSomeMangling(array: *[4]u8) void {
164202}
165203
166204test "implicit cast zero sized array ptr to slice" {
205 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
206
167207 {
168208 var b = "".*;
169209 const c: []const u8 = &b;
......@@ -177,6 +217,8 @@ test "implicit cast zero sized array ptr to slice" {
177217}
178218
179219test "anonymous list literal syntax" {
220 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
221
180222 const S = struct {
181223 fn doTheTest() !void {
182224 var array: [4]u8 = .{ 1, 2, 3, 4 };