authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2022-01-29 13:01:28+01:00
committergravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2022-01-30 10:16:13+01:00
log2a1727e93c4ea654cc8f3295728a9dd378eb044e
tree5a1bb90aa79ee4dad8ddd26c4275746085114ad6
parentf8e418c47d13189674bdf5f131c19fd811964579
signaturelock-open Commit is signed but in an unrecognized format.

stage2 RISCV64: Merge Register and RawRegister enums


1 files changed, 22 insertions(+), 28 deletions(-)

src/arch/riscv64/bits.zig+22-28
...@@ -71,9 +71,9 @@ pub const Instruction = union(enum) {...@@ -71,9 +71,9 @@ pub const Instruction = union(enum) {
71 .opcode = op,71 .opcode = op,
72 .funct3 = fn3,72 .funct3 = fn3,
73 .funct7 = fn7,73 .funct7 = fn7,
74 .rd = @enumToInt(rd),74 .rd = rd.id(),
75 .rs1 = @enumToInt(r1),75 .rs1 = r1.id(),
76 .rs2 = @enumToInt(r2),76 .rs2 = r2.id(),
77 },77 },
78 };78 };
79 }79 }
...@@ -86,8 +86,8 @@ pub const Instruction = union(enum) {...@@ -86,8 +86,8 @@ pub const Instruction = union(enum) {
86 .I = .{86 .I = .{
87 .opcode = op,87 .opcode = op,
88 .funct3 = fn3,88 .funct3 = fn3,
89 .rd = @enumToInt(rd),89 .rd = rd.id(),
90 .rs1 = @enumToInt(r1),90 .rs1 = r1.id(),
91 .imm0_11 = umm,91 .imm0_11 = umm,
92 },92 },
93 };93 };
...@@ -100,8 +100,8 @@ pub const Instruction = union(enum) {...@@ -100,8 +100,8 @@ pub const Instruction = union(enum) {
100 .S = .{100 .S = .{
101 .opcode = op,101 .opcode = op,
102 .funct3 = fn3,102 .funct3 = fn3,
103 .rs1 = @enumToInt(r1),103 .rs1 = r1.id(),
104 .rs2 = @enumToInt(r2),104 .rs2 = r2.id(),
105 .imm0_4 = @truncate(u5, umm),105 .imm0_4 = @truncate(u5, umm),
106 .imm5_11 = @truncate(u7, umm >> 5),106 .imm5_11 = @truncate(u7, umm >> 5),
107 },107 },
...@@ -118,8 +118,8 @@ pub const Instruction = union(enum) {...@@ -118,8 +118,8 @@ pub const Instruction = union(enum) {
118 .B = .{118 .B = .{
119 .opcode = op,119 .opcode = op,
120 .funct3 = fn3,120 .funct3 = fn3,
121 .rs1 = @enumToInt(r1),121 .rs1 = r1.id(),
122 .rs2 = @enumToInt(r2),122 .rs2 = r2.id(),
123 .imm1_4 = @truncate(u4, umm >> 1),123 .imm1_4 = @truncate(u4, umm >> 1),
124 .imm5_10 = @truncate(u6, umm >> 5),124 .imm5_10 = @truncate(u6, umm >> 5),
125 .imm11 = @truncate(u1, umm >> 11),125 .imm11 = @truncate(u1, umm >> 11),
...@@ -135,7 +135,7 @@ pub const Instruction = union(enum) {...@@ -135,7 +135,7 @@ pub const Instruction = union(enum) {
135 return Instruction{135 return Instruction{
136 .U = .{136 .U = .{
137 .opcode = op,137 .opcode = op,
138 .rd = @enumToInt(rd),138 .rd = rd.id(),
139 .imm12_31 = umm,139 .imm12_31 = umm,
140 },140 },
141 };141 };
...@@ -148,7 +148,7 @@ pub const Instruction = union(enum) {...@@ -148,7 +148,7 @@ pub const Instruction = union(enum) {
148 return Instruction{148 return Instruction{
149 .J = .{149 .J = .{
150 .opcode = op,150 .opcode = op,
151 .rd = @enumToInt(rd),151 .rd = rd.id(),
152 .imm1_10 = @truncate(u10, umm >> 1),152 .imm1_10 = @truncate(u10, umm >> 1),
153 .imm11 = @truncate(u1, umm >> 11),153 .imm11 = @truncate(u1, umm >> 11),
154 .imm12_19 = @truncate(u8, umm >> 12),154 .imm12_19 = @truncate(u8, umm >> 12),
...@@ -382,20 +382,13 @@ pub const Instruction = union(enum) {...@@ -382,20 +382,13 @@ pub const Instruction = union(enum) {
382 pub const ebreak = iType(0b1110011, 0b000, .zero, .zero, 0x001);382 pub const ebreak = iType(0b1110011, 0b000, .zero, .zero, 0x001);
383};383};
384384
385// zig fmt: off385pub const Register = enum(u6) {
386pub const RawRegister = enum(u5) {386 // zig fmt: off
387 x0, x1, x2, x3, x4, x5, x6, x7,387 x0, x1, x2, x3, x4, x5, x6, x7,
388 x8, x9, x10, x11, x12, x13, x14, x15,388 x8, x9, x10, x11, x12, x13, x14, x15,
389 x16, x17, x18, x19, x20, x21, x22, x23,389 x16, x17, x18, x19, x20, x21, x22, x23,
390 x24, x25, x26, x27, x28, x29, x30, x31,390 x24, x25, x26, x27, x28, x29, x30, x31,
391391
392 pub fn dwarfLocOp(reg: RawRegister) u8 {
393 return @enumToInt(reg) + DW.OP.reg0;
394 }
395};
396
397pub const Register = enum(u5) {
398 // 64 bit registers
399 zero, // zero392 zero, // zero
400 ra, // return address. caller saved393 ra, // return address. caller saved
401 sp, // stack pointer. callee saved.394 sp, // stack pointer. callee saved.
...@@ -408,23 +401,24 @@ pub const Register = enum(u5) {...@@ -408,23 +401,24 @@ pub const Register = enum(u5) {
408 a2, a3, a4, a5, a6, a7, // fn args. caller saved.401 a2, a3, a4, a5, a6, a7, // fn args. caller saved.
409 s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, // saved registers. callee saved.402 s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, // saved registers. callee saved.
410 t3, t4, t5, t6, // caller saved403 t3, t4, t5, t6, // caller saved
411 404 // zig fmt: on
412 pub fn parseRegName(name: []const u8) ?Register {405
413 if(std.meta.stringToEnum(Register, name)) |reg| return reg;406 /// Returns the unique 4-bit ID of this register which is used in
414 if(std.meta.stringToEnum(RawRegister, name)) |rawreg| return @intToEnum(Register, @enumToInt(rawreg));407 /// the machine code
415 return null;408 pub fn id(self: Register) u5 {
409 return @truncate(u5, @enumToInt(self));
416 }410 }
417411
418 /// Returns the index into `callee_preserved_regs`.412 /// Returns the index into `callee_preserved_regs`.
419 pub fn allocIndex(self: Register) ?u4 {413 pub fn allocIndex(self: Register) ?u4 {
420 inline for(callee_preserved_regs) |cpreg, i| {414 inline for (callee_preserved_regs) |cpreg, i| {
421 if(self == cpreg) return i;415 if (self.id() == cpreg.id()) return i;
422 }416 }
423 return null;417 return null;
424 }418 }
425419
426 pub fn dwarfLocOp(reg: Register) u8 {420 pub fn dwarfLocOp(reg: Register) u8 {
427 return @as(u8, @enumToInt(reg)) + DW.OP.reg0;421 return @as(u8, reg.id()) + DW.OP.reg0;
428 }422 }
429};423};
430424