authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-04-19 12:40:24-07:00
committergravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-06-13 02:19:38-07:00
log004d0c8978d4b5e4212c06abb33d7a594930f8c5
treebfce4fb25e6e6b220f50e06a121c6e528d7a7180
parent4aa15440c7a12bcc6bc0cd589ade02295549d48c
signaturelock-open Commit is signed but in an unrecognized format.

riscv: switch progress + by-ref return progress


10 files changed, 158 insertions(+), 55 deletions(-)

src/arch/riscv64/CodeGen.zig+148-11
......@@ -1223,7 +1223,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
12231223
12241224 .field_parent_ptr => try self.airFieldParentPtr(inst),
12251225
1226 .switch_br => try self.airSwitch(inst),
1226 .switch_br => try self.airSwitchBr(inst),
12271227 .slice_ptr => try self.airSlicePtr(inst),
12281228 .slice_len => try self.airSliceLen(inst),
12291229
......@@ -1960,7 +1960,7 @@ fn binOp(
19601960 switch (lhs_ty.zigTypeTag(zcu)) {
19611961 .Float => return self.fail("TODO binary operations on floats", .{}),
19621962 .Vector => return self.fail("TODO binary operations on vectors", .{}),
1963 .Int => {
1963 .Int, .Enum => {
19641964 assert(lhs_ty.eql(rhs_ty, zcu));
19651965 const int_info = lhs_ty.intInfo(zcu);
19661966 if (int_info.bits <= 64) {
......@@ -3682,7 +3682,6 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void {
36823682 switch (self.ret_mcv.short) {
36833683 .none => {},
36843684 .register, .register_pair => try self.load(self.ret_mcv.short, ptr, ptr_ty),
3685 .indirect => |reg_off| try self.genSetReg(ptr_ty, reg_off.reg, ptr),
36863685 else => unreachable,
36873686 }
36883687 self.ret_mcv.liveOut(self, inst);
......@@ -4160,12 +4159,97 @@ fn lowerBlock(self: *Self, inst: Air.Inst.Index, body: []const Air.Inst.Index) !
41604159 self.finishAirBookkeeping();
41614160}
41624161
4163fn airSwitch(self: *Self, inst: Air.Inst.Index) !void {
4162fn airSwitchBr(self: *Self, inst: Air.Inst.Index) !void {
41644163 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
4165 const condition = pl_op.operand;
4166 _ = condition;
4167 return self.fail("TODO airSwitch for {}", .{self.target.cpu.arch});
4168 // return self.finishAir(inst, .dead, .{ condition, .none, .none });
4164 const condition = try self.resolveInst(pl_op.operand);
4165 const condition_ty = self.typeOf(pl_op.operand);
4166 const switch_br = self.air.extraData(Air.SwitchBr, pl_op.payload);
4167 var extra_index: usize = switch_br.end;
4168 var case_i: u32 = 0;
4169 const liveness = try self.liveness.getSwitchBr(self.gpa, inst, switch_br.data.cases_len + 1);
4170 defer self.gpa.free(liveness.deaths);
4171
4172 // If the condition dies here in this switch instruction, process
4173 // that death now instead of later as this has an effect on
4174 // whether it needs to be spilled in the branches
4175 if (self.liveness.operandDies(inst, 0)) {
4176 if (pl_op.operand.toIndex()) |op_inst| try self.processDeath(op_inst);
4177 }
4178
4179 self.scope_generation += 1;
4180 const state = try self.saveState();
4181
4182 while (case_i < switch_br.data.cases_len) : (case_i += 1) {
4183 const case = self.air.extraData(Air.SwitchBr.Case, extra_index);
4184 const items: []const Air.Inst.Ref =
4185 @ptrCast(self.air.extra[case.end..][0..case.data.items_len]);
4186 const case_body: []const Air.Inst.Index =
4187 @ptrCast(self.air.extra[case.end + items.len ..][0..case.data.body_len]);
4188 extra_index = case.end + items.len + case_body.len;
4189
4190 var relocs = try self.gpa.alloc(Mir.Inst.Index, items.len);
4191 defer self.gpa.free(relocs);
4192
4193 for (items, relocs, 0..) |item, *reloc, i| {
4194 // switch branches must be comptime-known, so this is stored in an immediate
4195 const item_mcv = try self.resolveInst(item);
4196
4197 const cmp_mcv: MCValue = try self.binOp(
4198 .cmp_neq,
4199 condition,
4200 condition_ty,
4201 item_mcv,
4202 condition_ty,
4203 );
4204
4205 const cmp_reg = try self.copyToTmpRegister(Type.bool, cmp_mcv);
4206
4207 if (!(i < relocs.len - 1)) {
4208 _ = try self.addInst(.{
4209 .tag = .pseudo,
4210 .ops = .pseudo_not,
4211 .data = .{ .rr = .{
4212 .rd = cmp_reg,
4213 .rs = cmp_reg,
4214 } },
4215 });
4216 }
4217
4218 reloc.* = try self.condBr(condition_ty, .{ .register = cmp_reg });
4219 }
4220
4221 for (liveness.deaths[case_i]) |operand| try self.processDeath(operand);
4222
4223 for (relocs[0 .. relocs.len - 1]) |reloc| self.performReloc(reloc);
4224 try self.genBody(case_body);
4225 try self.restoreState(state, &.{}, .{
4226 .emit_instructions = false,
4227 .update_tracking = true,
4228 .resurrect = true,
4229 .close_scope = true,
4230 });
4231
4232 self.performReloc(relocs[relocs.len - 1]);
4233 }
4234
4235 if (switch_br.data.else_body_len > 0) {
4236 const else_body: []const Air.Inst.Index =
4237 @ptrCast(self.air.extra[extra_index..][0..switch_br.data.else_body_len]);
4238
4239 const else_deaths = liveness.deaths.len - 1;
4240 for (liveness.deaths[else_deaths]) |operand| try self.processDeath(operand);
4241
4242 try self.genBody(else_body);
4243 try self.restoreState(state, &.{}, .{
4244 .emit_instructions = false,
4245 .update_tracking = true,
4246 .resurrect = true,
4247 .close_scope = true,
4248 });
4249 }
4250
4251 // We already took care of pl_op.operand earlier, so there's nothing left to do
4252 self.finishAirBookkeeping();
41694253}
41704254
41714255fn performReloc(self: *Self, inst: Mir.Inst.Index) void {
......@@ -4249,9 +4333,60 @@ fn airBr(self: *Self, inst: Air.Inst.Index) !void {
42494333
42504334fn airBoolOp(self: *Self, inst: Air.Inst.Index) !void {
42514335 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
4252 const air_tags = self.air.instructions.items(.tag);
4253 _ = air_tags;
4254 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement boolean operations for {}", .{self.target.cpu.arch});
4336 const tag: Air.Inst.Tag = self.air.instructions.items(.tag)[@intFromEnum(inst)];
4337
4338 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
4339 const lhs = try self.resolveInst(bin_op.lhs);
4340 const rhs = try self.resolveInst(bin_op.rhs);
4341 const lhs_ty = Type.bool;
4342 const rhs_ty = Type.bool;
4343
4344 const lhs_reg, const lhs_lock = blk: {
4345 if (lhs == .register) break :blk .{ lhs.register, null };
4346
4347 const lhs_reg, const lhs_lock = try self.allocReg();
4348 try self.genSetReg(lhs_ty, lhs_reg, lhs);
4349 break :blk .{ lhs_reg, lhs_lock };
4350 };
4351 defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock);
4352
4353 const rhs_reg, const rhs_lock = blk: {
4354 if (rhs == .register) break :blk .{ rhs.register, null };
4355
4356 const rhs_reg, const rhs_lock = try self.allocReg();
4357 try self.genSetReg(rhs_ty, rhs_reg, rhs);
4358 break :blk .{ rhs_reg, rhs_lock };
4359 };
4360 defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock);
4361
4362 const result_reg, const result_lock = try self.allocReg();
4363 defer self.register_manager.unlockReg(result_lock);
4364
4365 _ = try self.addInst(.{
4366 .tag = if (tag == .bool_or) .@"or" else .@"and",
4367 .ops = .rrr,
4368 .data = .{ .r_type = .{
4369 .rd = result_reg,
4370 .rs1 = lhs_reg,
4371 .rs2 = rhs_reg,
4372 } },
4373 });
4374
4375 // safety truncate
4376 if (self.wantSafety()) {
4377 _ = try self.addInst(.{
4378 .tag = .andi,
4379 .ops = .rri,
4380 .data = .{ .i_type = .{
4381 .rd = result_reg,
4382 .rs1 = result_reg,
4383 .imm12 = Immediate.s(1),
4384 } },
4385 });
4386 }
4387
4388 break :result .{ .register = result_reg };
4389 };
42554390 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
42564391}
42574392
......@@ -5265,7 +5400,9 @@ fn resolveCallingConventionValues(
52655400 },
52665401 .memory => {
52675402 const param_int_regs = abi.function_arg_regs;
5403
52685404 const param_int_reg = param_int_regs[param_int_reg_i];
5405 param_int_reg_i += 1;
52695406
52705407 arg_mcv[arg_mcv_i] = .{ .indirect = .{ .reg = param_int_reg } };
52715408 arg_mcv_i += 1;
src/arch/riscv64/Encoding.zig+3
......@@ -38,6 +38,7 @@ pub const Mnemonic = enum {
3838 // R Type
3939 add,
4040 @"and",
41 @"or",
4142 sub,
4243 slt,
4344 mul,
......@@ -55,6 +56,7 @@ pub const Mnemonic = enum {
5556 .add => .{ .opcode = 0b0110011, .funct3 = 0b000, .funct7 = 0b0000000 },
5657 .sltu => .{ .opcode = 0b0110011, .funct3 = 0b011, .funct7 = 0b0000000 },
5758 .@"and" => .{ .opcode = 0b0110011, .funct3 = 0b111, .funct7 = 0b0000000 },
59 .@"or" => .{ .opcode = 0b0110011, .funct3 = 0b110, .funct7 = 0b0000000 },
5860 .sub => .{ .opcode = 0b0110011, .funct3 = 0b000, .funct7 = 0b0100000 },
5961
6062 .ld => .{ .opcode = 0b0000011, .funct3 = 0b011, .funct7 = null },
......@@ -152,6 +154,7 @@ pub const InstEnc = enum {
152154 .add,
153155 .sub,
154156 .@"and",
157 .@"or",
155158 => .R,
156159
157160 .ecall,
src/arch/riscv64/Mir.zig-3
......@@ -80,9 +80,6 @@ pub const Inst = struct {
8080 /// Branch if not equal, Uses b_type
8181 bne,
8282
83 /// Boolean NOT, Uses rr payload
84 not,
85
8683 /// Generates a NO-OP, uses nop payload
8784 nop,
8885
test/behavior/align.zig-1
......@@ -624,7 +624,6 @@ test "alignment of slice element" {
624624}
625625
626626test "sub-aligned pointer field access" {
627 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; // TODO
628627 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
629628 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
630629 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
test/behavior/cast.zig-1
......@@ -881,7 +881,6 @@ test "peer resolution of string literals" {
881881 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
882882 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
883883 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
884 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
885884
886885 const S = struct {
887886 const E = enum { a, b, c, d };
test/behavior/enum.zig-2
......@@ -610,7 +610,6 @@ fn testEnumWithSpecifiedTagValues(x: MultipleChoice) !void {
610610test "enum with specified tag values" {
611611 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
612612 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
613 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
614613
615614 try testEnumWithSpecifiedTagValues(MultipleChoice.C);
616615 try comptime testEnumWithSpecifiedTagValues(MultipleChoice.C);
......@@ -749,7 +748,6 @@ test "cast integer literal to enum" {
749748test "enum with specified and unspecified tag values" {
750749 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
751750 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
752 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
753751
754752 try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
755753 try comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
test/behavior/eval.zig-5
......@@ -1088,7 +1088,6 @@ test "comptime break operand passing through runtime condition converted to runt
10881088test "comptime break operand passing through runtime switch converted to runtime break" {
10891089 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
10901090 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1091 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
10921091
10931092 const S = struct {
10941093 fn doTheTest(runtime: u8) !void {
......@@ -1631,8 +1630,6 @@ test "struct in comptime false branch is not evaluated" {
16311630}
16321631
16331632test "result of nested switch assigned to variable" {
1634 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
1635
16361633 var zds: u32 = 0;
16371634 zds = switch (zds) {
16381635 0 => switch (zds) {
......@@ -1667,8 +1664,6 @@ test "inline for loop of functions returning error unions" {
16671664}
16681665
16691666test "if inside a switch" {
1670 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
1671
16721667 var condition = true;
16731668 var wave_type: u32 = 0;
16741669 _ = .{ &condition, &wave_type };
test/behavior/inline_switch.zig-6
......@@ -5,7 +5,6 @@ const builtin = @import("builtin");
55test "inline scalar prongs" {
66 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
77 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
8 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
98
109 var x: usize = 0;
1110 switch (x) {
......@@ -21,7 +20,6 @@ test "inline scalar prongs" {
2120test "inline prong ranges" {
2221 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
2322 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
24 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
2523
2624 var x: usize = 0;
2725 _ = &x;
......@@ -37,7 +35,6 @@ const E = enum { a, b, c, d };
3735test "inline switch enums" {
3836 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
3937 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
40 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
4138
4239 var x: E = .a;
4340 _ = &x;
......@@ -106,7 +103,6 @@ test "inline else error" {
106103test "inline else enum" {
107104 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
108105 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
109 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
110106
111107 const E2 = enum(u8) { a = 2, b = 3, c = 4, d = 5 };
112108 var a: E2 = .a;
......@@ -120,7 +116,6 @@ test "inline else enum" {
120116test "inline else int with gaps" {
121117 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
122118 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
123 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
124119
125120 var a: u8 = 0;
126121 _ = &a;
......@@ -139,7 +134,6 @@ test "inline else int with gaps" {
139134test "inline else int all values" {
140135 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
141136 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
142 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
143137
144138 var a: u2 = 0;
145139 _ = &a;
test/behavior/ref_var_in_if_after_if_2nd_switch_prong.zig-1
......@@ -8,7 +8,6 @@ test "reference a variable in an if after an if in the 2nd switch prong" {
88 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
99 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
1010 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
11 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
1211
1312 try foo(true, Num.Two, false, "aoeu");
1413 try expect(!ok);
test/behavior/switch.zig+7-25
......@@ -7,7 +7,6 @@ const expectEqual = std.testing.expectEqual;
77
88test "switch with numbers" {
99 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
10 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
1110
1211 try testSwitchWithNumbers(13);
1312}
......@@ -23,7 +22,6 @@ fn testSwitchWithNumbers(x: u32) !void {
2322
2423test "switch with all ranges" {
2524 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
26 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
2725
2826 try expect(testSwitchWithAllRanges(50, 3) == 1);
2927 try expect(testSwitchWithAllRanges(101, 0) == 2);
......@@ -57,27 +55,25 @@ test "implicit comptime switch" {
5755
5856test "switch on enum" {
5957 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
60 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
6158
6259 const fruit = Fruit.Orange;
63 nonConstSwitchOnEnum(fruit);
60 try expect(nonConstSwitchOnEnum(fruit));
6461}
6562const Fruit = enum {
6663 Apple,
6764 Orange,
6865 Banana,
6966};
70fn nonConstSwitchOnEnum(fruit: Fruit) void {
71 switch (fruit) {
72 Fruit.Apple => unreachable,
73 Fruit.Orange => {},
74 Fruit.Banana => unreachable,
75 }
67fn nonConstSwitchOnEnum(fruit: Fruit) bool {
68 return switch (fruit) {
69 Fruit.Apple => false,
70 Fruit.Orange => true,
71 Fruit.Banana => false,
72 };
7673}
7774
7875test "switch statement" {
7976 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
80 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
8177
8278 try nonConstSwitch(SwitchStatementFoo.C);
8379}
......@@ -94,7 +90,6 @@ const SwitchStatementFoo = enum { A, B, C, D };
9490
9591test "switch with multiple expressions" {
9692 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
97 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
9893
9994 const x = switch (returnsFive()) {
10095 1, 2, 3 => 1,
......@@ -179,7 +174,6 @@ test "undefined.u0" {
179174
180175test "switch with disjoint range" {
181176 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
182 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
183177
184178 var q: u8 = 0;
185179 _ = &q;
......@@ -191,8 +185,6 @@ test "switch with disjoint range" {
191185}
192186
193187test "switch variable for range and multiple prongs" {
194 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
195
196188 const S = struct {
197189 fn doTheTest() !void {
198190 try doTheSwitch(16);
......@@ -382,7 +374,6 @@ test "anon enum literal used in switch on union enum" {
382374
383375test "switch all prongs unreachable" {
384376 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
385 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
386377
387378 try testAllProngsUnreachable();
388379 try comptime testAllProngsUnreachable();
......@@ -420,7 +411,6 @@ fn return_a_number() anyerror!i32 {
420411
421412test "switch on integer with else capturing expr" {
422413 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
423 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
424414
425415 const S = struct {
426416 fn doTheTest() !void {
......@@ -735,7 +725,6 @@ test "switch capture copies its payload" {
735725
736726test "capture of integer forwards the switch condition directly" {
737727 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
738 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
739728
740729 const S = struct {
741730 fn foo(x: u8) !void {
......@@ -757,7 +746,6 @@ test "capture of integer forwards the switch condition directly" {
757746
758747test "enum value without tag name used as switch item" {
759748 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
760 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
761749
762750 const E = enum(u32) {
763751 a = 1,
......@@ -775,8 +763,6 @@ test "enum value without tag name used as switch item" {
775763}
776764
777765test "switch item sizeof" {
778 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
779
780766 const S = struct {
781767 fn doTheTest() !void {
782768 var a: usize = 0;
......@@ -873,8 +859,6 @@ test "switch pointer capture peer type resolution" {
873859}
874860
875861test "inline switch range that includes the maximum value of the switched type" {
876 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
877
878862 const inputs: [3]u8 = .{ 0, 254, 255 };
879863 for (inputs) |input| {
880864 switch (input) {
......@@ -970,8 +954,6 @@ test "prong with inline call to unreachable" {
970954}
971955
972956test "block error return trace index is reset between prongs" {
973 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
974
975957 const S = struct {
976958 fn returnError() error{TestFailed} {
977959 return error.TestFailed;