authorgravatar for koachan@protonmail.comKoakuma <koachan@protonmail.com> 2022-04-20 21:16:17+07:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-05-05 19:34:03+02:00
logb6d7f63f34bbdb7c67abecbcd47389d2a3d28743
treeaee14c6f05d58cf1777847747a97d5a3c79070e9
parent59905a62f9da9946a797cc35a2523c9929663600

stage2: sparcv9: Implement jmpl lowering


4 files changed, 35 insertions(+), 18 deletions(-)

src/arch/sparcv9/CodeGen.zig+18-2
......@@ -1,5 +1,7 @@
11//! SPARCv9 codegen.
22//! This lowers AIR into MIR.
3//! For now this only implements medium/low code model with absolute addressing.
4//! TODO add support for other code models.
35const std = @import("std");
46const assert = std.debug.assert;
57const log = std.log.scoped(.codegen);
......@@ -884,7 +886,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
884886
885887 _ = try self.addInst(.{
886888 .tag = .jmpl,
887 .data = .{ .branch_link_indirect = .{ .reg = .o7 } },
889 .data = .{
890 .arithmetic_3op = .{
891 .is_imm = false,
892 .rd = .o7,
893 .rs1 = .o7,
894 .rs2_or_imm = .{ .rs2 = .g0 },
895 },
896 },
888897 });
889898 } else if (func_value.castTag(.extern_fn)) |_| {
890899 return self.fail("TODO implement calling extern functions", .{});
......@@ -899,7 +908,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
899908
900909 _ = try self.addInst(.{
901910 .tag = .jmpl,
902 .data = .{ .branch_link_indirect = .{ .reg = .o7 } },
911 .data = .{
912 .arithmetic_3op = .{
913 .is_imm = false,
914 .rd = .o7,
915 .rs1 = .o7,
916 .rs2_or_imm = .{ .rs2 = .g0 },
917 },
918 },
903919 });
904920 }
905921
src/arch/sparcv9/Emit.zig+3-2
......@@ -56,8 +56,7 @@ pub fn emitMir(
5656
5757 .call => @panic("TODO implement sparcv9 call"),
5858
59 .jmpl => @panic("TODO implement sparcv9 jmpl"),
60 .jmpl_i => @panic("TODO implement sparcv9 jmpl to reg"),
59 .jmpl => try emit.mirArithmetic3Op(inst),
6160
6261 .ldub => try emit.mirArithmetic3Op(inst),
6362 .lduh => try emit.mirArithmetic3Op(inst),
......@@ -163,6 +162,7 @@ fn mirArithmetic3Op(emit: *Emit, inst: Mir.Inst.Index) !void {
163162 const imm = data.rs2_or_imm.imm;
164163 switch (tag) {
165164 .add => try emit.writeInstruction(Instruction.add(i13, rs1, imm, rd)),
165 .jmpl => try emit.writeInstruction(Instruction.jmpl(i13, rs1, imm, rd)),
166166 .ldub => try emit.writeInstruction(Instruction.ldub(i13, rs1, imm, rd)),
167167 .lduh => try emit.writeInstruction(Instruction.lduh(i13, rs1, imm, rd)),
168168 .lduw => try emit.writeInstruction(Instruction.lduw(i13, rs1, imm, rd)),
......@@ -177,6 +177,7 @@ fn mirArithmetic3Op(emit: *Emit, inst: Mir.Inst.Index) !void {
177177 const rs2 = data.rs2_or_imm.rs2;
178178 switch (tag) {
179179 .add => try emit.writeInstruction(Instruction.add(Register, rs1, rs2, rd)),
180 .jmpl => try emit.writeInstruction(Instruction.jmpl(Register, rs1, rs2, rd)),
180181 .ldub => try emit.writeInstruction(Instruction.ldub(Register, rs1, rs2, rd)),
181182 .lduh => try emit.writeInstruction(Instruction.lduh(Register, rs1, rs2, rd)),
182183 .lduw => try emit.writeInstruction(Instruction.lduw(Register, rs1, rs2, rd)),
src/arch/sparcv9/Mir.zig+1-11
......@@ -54,11 +54,8 @@ pub const Inst = struct {
5454 call,
5555
5656 /// A.24 Jump and Link
57 /// jmpl (far direct jump) uses the branch_link field,
58 /// while jmpl_i (indirect jump) uses the branch_link_indirect field.
59 /// Those two MIR instructions will be lowered into SPARCv9 jmpl instruction.
57 /// It uses the arithmetic_3op field.
6058 jmpl,
61 jmpl_i,
6259
6360 /// A.27 Load Integer
6461 /// Those uses the arithmetic_3op field.
......@@ -167,13 +164,6 @@ pub const Inst = struct {
167164 link: Register = .o7,
168165 },
169166
170 /// Indirect branch and link (always unconditional).
171 /// Used by e.g. jmpl_i
172 branch_link_indirect: struct {
173 reg: Register,
174 link: Register = .o7,
175 },
176
177167 /// Branch with prediction.
178168 /// Used by e.g. bpcc
179169 branch_predict: struct {
src/arch/sparcv9/bits.zig+13-3
......@@ -271,6 +271,8 @@ pub const Instruction = union(enum) {
271271 rd: u5,
272272 op3: u6,
273273 rs1: u5,
274 // See Errata 58 of SPARCv9 specification
275 // https://sparc.org/errata-for-v9/#58
274276 i: u1 = 0b1,
275277 reserved: u8 = 0b00000000,
276278 rs2: u5,
......@@ -977,10 +979,10 @@ pub const Instruction = union(enum) {
977979 };
978980 }
979981
980 pub fn @"or"(comptime s2: type, rs1: Register, rs2: s2, rd: Register) Instruction {
982 pub fn jmpl(comptime s2: type, rs1: Register, rs2: s2, rd: Register) Instruction {
981983 return switch (s2) {
982 Register => format3a(0b10, 0b00_0010, rs1, rs2, rd),
983 i13 => format3b(0b10, 0b00_0010, rs1, rs2, rd),
984 Register => format3a(0b10, 0b11_1000, rs1, rs2, rd),
985 i13 => format3b(0b10, 0b11_1000, rs1, rs2, rd),
984986 else => unreachable,
985987 };
986988 }
......@@ -1017,6 +1019,14 @@ pub const Instruction = union(enum) {
10171019 };
10181020 }
10191021
1022 pub fn @"or"(comptime s2: type, rs1: Register, rs2: s2, rd: Register) Instruction {
1023 return switch (s2) {
1024 Register => format3a(0b10, 0b00_0010, rs1, rs2, rd),
1025 i13 => format3b(0b10, 0b00_0010, rs1, rs2, rd),
1026 else => unreachable,
1027 };
1028 }
1029
10201030 pub fn nop() Instruction {
10211031 return sethi(0, .g0);
10221032 }