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) {
7171 .opcode = op,
7272 .funct3 = fn3,
7373 .funct7 = fn7,
74 .rd = @enumToInt(rd),
75 .rs1 = @enumToInt(r1),
76 .rs2 = @enumToInt(r2),
74 .rd = rd.id(),
75 .rs1 = r1.id(),
76 .rs2 = r2.id(),
7777 },
7878 };
7979 }
......@@ -86,8 +86,8 @@ pub const Instruction = union(enum) {
8686 .I = .{
8787 .opcode = op,
8888 .funct3 = fn3,
89 .rd = @enumToInt(rd),
90 .rs1 = @enumToInt(r1),
89 .rd = rd.id(),
90 .rs1 = r1.id(),
9191 .imm0_11 = umm,
9292 },
9393 };
......@@ -100,8 +100,8 @@ pub const Instruction = union(enum) {
100100 .S = .{
101101 .opcode = op,
102102 .funct3 = fn3,
103 .rs1 = @enumToInt(r1),
104 .rs2 = @enumToInt(r2),
103 .rs1 = r1.id(),
104 .rs2 = r2.id(),
105105 .imm0_4 = @truncate(u5, umm),
106106 .imm5_11 = @truncate(u7, umm >> 5),
107107 },
......@@ -118,8 +118,8 @@ pub const Instruction = union(enum) {
118118 .B = .{
119119 .opcode = op,
120120 .funct3 = fn3,
121 .rs1 = @enumToInt(r1),
122 .rs2 = @enumToInt(r2),
121 .rs1 = r1.id(),
122 .rs2 = r2.id(),
123123 .imm1_4 = @truncate(u4, umm >> 1),
124124 .imm5_10 = @truncate(u6, umm >> 5),
125125 .imm11 = @truncate(u1, umm >> 11),
......@@ -135,7 +135,7 @@ pub const Instruction = union(enum) {
135135 return Instruction{
136136 .U = .{
137137 .opcode = op,
138 .rd = @enumToInt(rd),
138 .rd = rd.id(),
139139 .imm12_31 = umm,
140140 },
141141 };
......@@ -148,7 +148,7 @@ pub const Instruction = union(enum) {
148148 return Instruction{
149149 .J = .{
150150 .opcode = op,
151 .rd = @enumToInt(rd),
151 .rd = rd.id(),
152152 .imm1_10 = @truncate(u10, umm >> 1),
153153 .imm11 = @truncate(u1, umm >> 11),
154154 .imm12_19 = @truncate(u8, umm >> 12),
......@@ -382,20 +382,13 @@ pub const Instruction = union(enum) {
382382 pub const ebreak = iType(0b1110011, 0b000, .zero, .zero, 0x001);
383383};
384384
385// zig fmt: off
386pub const RawRegister = enum(u5) {
385pub const Register = enum(u6) {
386 // zig fmt: off
387387 x0, x1, x2, x3, x4, x5, x6, x7,
388388 x8, x9, x10, x11, x12, x13, x14, x15,
389389 x16, x17, x18, x19, x20, x21, x22, x23,
390390 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
399392 zero, // zero
400393 ra, // return address. caller saved
401394 sp, // stack pointer. callee saved.
......@@ -408,23 +401,24 @@ pub const Register = enum(u5) {
408401 a2, a3, a4, a5, a6, a7, // fn args. caller saved.
409402 s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, // saved registers. callee saved.
410403 t3, t4, t5, t6, // caller saved
411
412 pub fn parseRegName(name: []const u8) ?Register {
413 if(std.meta.stringToEnum(Register, name)) |reg| return reg;
414 if(std.meta.stringToEnum(RawRegister, name)) |rawreg| return @intToEnum(Register, @enumToInt(rawreg));
415 return null;
404 // zig fmt: on
405
406 /// Returns the unique 4-bit ID of this register which is used in
407 /// the machine code
408 pub fn id(self: Register) u5 {
409 return @truncate(u5, @enumToInt(self));
416410 }
417411
418412 /// Returns the index into `callee_preserved_regs`.
419413 pub fn allocIndex(self: Register) ?u4 {
420 inline for(callee_preserved_regs) |cpreg, i| {
421 if(self == cpreg) return i;
414 inline for (callee_preserved_regs) |cpreg, i| {
415 if (self.id() == cpreg.id()) return i;
422416 }
423417 return null;
424418 }
425419
426420 pub fn dwarfLocOp(reg: Register) u8 {
427 return @as(u8, @enumToInt(reg)) + DW.OP.reg0;
421 return @as(u8, reg.id()) + DW.OP.reg0;
428422 }
429423};
430424