-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix RiscvRegId's from_raw_id for RISCV64 #44
Conversation
Ah, good catch! Your implementation is actually quite reasonable for someone who's new to Rust. Admittedly, it is possible to use some Rust type-system shenanigans to get a slightly more elegant solution with /// RISC-V Register identifier.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum RiscvRegId<U> {
/// General Purpose Register (x0-x31).
Gpr(u8),
/// Floating Point Register (f0-f31).
Fpr(u8),
/// Program Counter.
Pc,
/// Control and Status Register.
Csr(u16),
/// Privilege level.
Priv,
#[doc(hidden)]
_Marker(core::marker::PhantomData<U>)
}
// use a macro to keep the two implementations in-sync
macro_rules! impl_riscv_reg_id {
($usize:ty) => {
impl RegId for RiscvRegId<$usize> {
fn from_raw_id(id: usize) -> Option<(Self, usize)> {
const USIZE: usize = core::mem::size_of::<$usize>();
let reg_size = match id {
0..=31 => (Self::Gpr(id as u8), USIZE),
32 => (Self::Pc, USIZE),
33..=64 => (Self::Fpr((id - 33) as u8), USIZE),
65..=4160 => (Self::Csr((id - 65) as u16), USIZE),
4161 => (Self::Priv, 1),
_ => return None,
};
Some(reg_size)
}
}
};
}
impl_riscv_reg_id!(u32);
impl_riscv_reg_id!(u64); Essentially, we use a Can you update your PR to use this generic implementation instead? It should compile, though I haven't actually checked. Unfortunately the tricky thing with this PR + bugfix is that it is technically a semver breaking change (you're changing the associated type of the Riscv64 Arch), and as such, it can't be released as part of a non-breaking I have an idea for how to avoid this in the future, but for now, here's what we are going to have to do:
Unfortunately, I'm a bit busy with work / life at the moment, so I don't have a firm timetable on when In the meantime, you'll just have to maintain a custom in-tree |
Thank you for your guidance. I learned a lot from it. I've reimplemented the code as you suggested. Then, I retarget this PR to the dev/0.5 branch(I follow the instructions at Github's official doc). GitHub suggests that there are some conflicting files. Do I need to do something? This is my first time retargeting a PR. I'm unfamiliar with it. |
Glad I could help! You've currently got your changes on a fork of the The next part is a bit sketchy, and I'm not sure if it'll work... Basically, you'll want to force-push your local Untested, but something like this should work: # in the master branch
git checkout dev/0.5
git cherry-pick 12bd701
git cherry-pick 1c7797e
git push
# it should give you a command with `--set-upstream`. swap out `dev/0.5` with `master`
<run that command to push the changes> Let me know if that works! I'll be out today, but I can help more tomorrow :) |
by using the suggested implementation
Thanks for your help. The detailed explanation you gave was very helpful. I think that works! I executed the following instructions. git checkout dev/0.5
git cherry-pick 12bd701
# after fix the conflict files
git commit -a
git push origin dev/0.5:master -f Then, Github said |
Fantastic work! Thanks again for the contribution 😄 As I mentioned before, while I can't commit to a solid release date for Also, if you get the chance, do consider providing any feedback you might have about |
Hello. For RISC-V 64, the size of Csr and Gpr is 64bit. However, the RiscvRegId's
from_raw_id
returns 4 bytes for both RV32 and RV64. Thus I create a new type for RV64 and re-implementfrom_raw_id
. Now, theRegId::from_raw_id()
works correctly on RV64 architecture.I am new Rust. I think my implementation is a little ugly but I have no idea how to implement this elegantly. The
RiscvRegId64
andRiscvRegId
have same definition. I define this new type only for overridefrom_raw_id
. I have tried to define something likeRiscvRegId<U>
. But the compiler complains thatU
is an unused parameter.