| ... | ... | @@ -71,9 +71,9 @@ pub const Instruction = union(enum) { |
| 71 | 71 | .opcode = op, |
| 72 | 72 | .funct3 = fn3, |
| 73 | 73 | .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(), |
| 77 | 77 | }, |
| 78 | 78 | }; |
| 79 | 79 | } |
| ... | ... | @@ -86,8 +86,8 @@ pub const Instruction = union(enum) { |
| 86 | 86 | .I = .{ |
| 87 | 87 | .opcode = op, |
| 88 | 88 | .funct3 = fn3, |
| 89 | | .rd = @enumToInt(rd), |
| 90 | | .rs1 = @enumToInt(r1), |
| 89 | .rd = rd.id(), |
| 90 | .rs1 = r1.id(), |
| 91 | 91 | .imm0_11 = umm, |
| 92 | 92 | }, |
| 93 | 93 | }; |
| ... | ... | @@ -100,8 +100,8 @@ pub const Instruction = union(enum) { |
| 100 | 100 | .S = .{ |
| 101 | 101 | .opcode = op, |
| 102 | 102 | .funct3 = fn3, |
| 103 | | .rs1 = @enumToInt(r1), |
| 104 | | .rs2 = @enumToInt(r2), |
| 103 | .rs1 = r1.id(), |
| 104 | .rs2 = r2.id(), |
| 105 | 105 | .imm0_4 = @truncate(u5, umm), |
| 106 | 106 | .imm5_11 = @truncate(u7, umm >> 5), |
| 107 | 107 | }, |
| ... | ... | @@ -118,8 +118,8 @@ pub const Instruction = union(enum) { |
| 118 | 118 | .B = .{ |
| 119 | 119 | .opcode = op, |
| 120 | 120 | .funct3 = fn3, |
| 121 | | .rs1 = @enumToInt(r1), |
| 122 | | .rs2 = @enumToInt(r2), |
| 121 | .rs1 = r1.id(), |
| 122 | .rs2 = r2.id(), |
| 123 | 123 | .imm1_4 = @truncate(u4, umm >> 1), |
| 124 | 124 | .imm5_10 = @truncate(u6, umm >> 5), |
| 125 | 125 | .imm11 = @truncate(u1, umm >> 11), |
| ... | ... | @@ -135,7 +135,7 @@ pub const Instruction = union(enum) { |
| 135 | 135 | return Instruction{ |
| 136 | 136 | .U = .{ |
| 137 | 137 | .opcode = op, |
| 138 | | .rd = @enumToInt(rd), |
| 138 | .rd = rd.id(), |
| 139 | 139 | .imm12_31 = umm, |
| 140 | 140 | }, |
| 141 | 141 | }; |
| ... | ... | @@ -148,7 +148,7 @@ pub const Instruction = union(enum) { |
| 148 | 148 | return Instruction{ |
| 149 | 149 | .J = .{ |
| 150 | 150 | .opcode = op, |
| 151 | | .rd = @enumToInt(rd), |
| 151 | .rd = rd.id(), |
| 152 | 152 | .imm1_10 = @truncate(u10, umm >> 1), |
| 153 | 153 | .imm11 = @truncate(u1, umm >> 11), |
| 154 | 154 | .imm12_19 = @truncate(u8, umm >> 12), |
| ... | ... | @@ -382,20 +382,13 @@ pub const Instruction = union(enum) { |
| 382 | 382 | pub const ebreak = iType(0b1110011, 0b000, .zero, .zero, 0x001); |
| 383 | 383 | }; |
| 384 | 384 | |
| 385 | | // zig fmt: off |
| 386 | | pub const RawRegister = enum(u5) { |
| 385 | pub const Register = enum(u6) { |
| 386 | // zig fmt: off |
| 387 | 387 | x0, x1, x2, x3, x4, x5, x6, x7, |
| 388 | 388 | x8, x9, x10, x11, x12, x13, x14, x15, |
| 389 | 389 | x16, x17, x18, x19, x20, x21, x22, x23, |
| 390 | 390 | x24, x25, x26, x27, x28, x29, x30, x31, |
| 391 | 391 | |
| 392 | | pub fn dwarfLocOp(reg: RawRegister) u8 { |
| 393 | | return @enumToInt(reg) + DW.OP.reg0; |
| 394 | | } |
| 395 | | }; |
| 396 | | |
| 397 | | pub const Register = enum(u5) { |
| 398 | | // 64 bit registers |
| 399 | 392 | zero, // zero |
| 400 | 393 | ra, // return address. caller saved |
| 401 | 394 | sp, // stack pointer. callee saved. |
| ... | ... | @@ -408,23 +401,24 @@ pub const Register = enum(u5) { |
| 408 | 401 | a2, a3, a4, a5, a6, a7, // fn args. caller saved. |
| 409 | 402 | s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, // saved registers. callee saved. |
| 410 | 403 | 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)); |
| 416 | 410 | } |
| 417 | 411 | |
| 418 | 412 | /// Returns the index into `callee_preserved_regs`. |
| 419 | 413 | 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; |
| 422 | 416 | } |
| 423 | 417 | return null; |
| 424 | 418 | } |
| 425 | 419 | |
| 426 | 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 | }; |
| 430 | 424 | |