refactor: use bitflags-based Extensions for RISC-V decoder#40
refactor: use bitflags-based Extensions for RISC-V decoder#40ChenMiaoi merged 2 commits intohust-open-atom-club:mainfrom
Conversation
manchangfengxu
commented
Nov 20, 2025
- Replace the old u32-based extension_masks with a bitflags::bitflags Extensions type in riscv::extensions
- Update RiscVDecoder, RiscVHandler, and all RISC-V extension is_enabled implementations to use Extensions::contains()
- Keep the existing RiscVExtensions type in riscv::arch for CLI-level configuration unchange
robustone-core/src/riscv/decoder.rs
Outdated
| pub fn rv32gc() -> Self { | ||
| Self::new( | ||
| Xlen::X32, | ||
| extension_masks::I | ||
| | extension_masks::M | ||
| | extension_masks::A | ||
| | extension_masks::F | ||
| | extension_masks::C | ||
| | extension_masks::XTHEADCONDMOV, | ||
| Extensions::I | ||
| | Extensions::M | ||
| | Extensions::A | ||
| | Extensions::F | ||
| | Extensions::C | ||
| | Extensions::XTHEADCONDMOV, |
There was a problem hiding this comment.
The standard rv32gc instruction set does not include XThead. For the instruction sets defined by the manufacturer, we need to handle them in a separate function.
There was a problem hiding this comment.
issue10 mentions that the final goal is to distinguish at the CLI level, but I'm not quite clear on how to refactor it yet. I need to discuss it with ChenMiao.
+thead Enables T-Head instruction sets. (only: RISC-V)
There was a problem hiding this comment.
issue10 mentions that the final goal is to distinguish at the CLI level, but I'm not quite clear on how to refactor it yet. I need to discuss it with ChenMiao.
+thead Enables T-Head instruction sets. (only: RISC-V)
For this, in fact, I think the T-Head instruction is a single path and should come from RISC-V? The T-Head is a complete RISC-V instruction set — it should be at the same level as the standard instruction set, rather than being an extension of RISC-V. That’s exactly why I haven’t started working on it yet.
- Replace the old u32-based extension_masks with a bitflags::bitflags Extensions type in riscv::extensions - Update RiscVDecoder, RiscVHandler, and all RISC-V extension is_enabled implementations to use Extensions::contains() - Keep the existing RiscVExtensions type in riscv::arch for CLI-level configuration unchange Signed-off-by: manchangfengxu <manchangfengxu@openatom.club>
1fd0516 to
08cb4c5
Compare
| /// Bitflags representing enabled RISC-V extensions. | ||
| #[derive(Clone, Copy)] | ||
| pub struct Extensions: u32 { | ||
| const G = Self::I.bits() | Self::M.bits() | Self::A.bits() | Self::F.bits() | Self::D.bits(); |
There was a problem hiding this comment.
can you move the line Extensions::G below the single extension?
| const F = 1 << 3; | ||
| const D = 1 << 4; | ||
| const C = 1 << 5; | ||
| const XTHEADCONDMOV = 1 << 6; |
There was a problem hiding this comment.
XTHEADCONDMOV is too long. Its meaning is T-head conditional move extension. The manufacturer name can be removed. Just call it CMOV to keep the name similar to x86. Then add annotations for explanation.
As a custom extension, this flag should not be placed together with the standard instruction set. Because there is a possibility of conflict among different custom extenstions. So you should define your own Extensions separately for T-Head.
6a1040d to
60b4290
Compare
| pub(crate) standard: StandardExtensions, | ||
| pub(crate) thead: THeadExtensions, | ||
| } | ||
|
|
There was a problem hiding this comment.
In extensions module, This struct is called Extensions. So StandardExtensions, this Extensions is redundant. Just call it Standard
There was a problem hiding this comment.
Okay, I will also change THeadExtensions to THead
| /// Enables all available T-Head custom extensions on this configuration. | ||
| pub fn set_thead(mut self) -> Self { | ||
| self.thead |= THeadExtensions::all(); | ||
| self | ||
| } | ||
| } |
There was a problem hiding this comment.
This function just crate a Extansions witch THead instruction set. It did not perform any operation on thead. So rename it to thead.
| // RVA - Atomic Instructions | ||
| // RVC - Compressed Instructions | ||
| // RVD - Double-Precision Floating-Point | ||
| // RVF - Single-Precision Floating-Point | ||
| // RV32I/RV64I - Base Integer Instruction Set | ||
| // RVM - Multiply and Divide Instructions |
There was a problem hiding this comment.
Why not remove these comments
There was a problem hiding this comment.
Okay, I will delete it
| // XuanTie custom extension modules | ||
| // XTheadCondMov - Conditional Move Instructions |
There was a problem hiding this comment.
This comment is missing context, so remove it.
60b4290 to
214354a
Compare
|
|
||
| /// XTheadCondMov Conditional Move Extension | ||
| pub struct XTheadCondMovExtension { | ||
| pub struct CondMovExtension { |
There was a problem hiding this comment.
Maybe it's not very good? Do I need to change it back?
There was a problem hiding this comment.
You need to maintain consistency throughout the entire code style. In this module, Extension is redundant
There was a problem hiding this comment.
extensions.standard.contains(Standard::A) This is more clear than before.
extensions.thead.contains(THead::CondMovExtension) This code contains two extensions, which are obviously redundant.
There was a problem hiding this comment.
The CMOV constant is in THead(bitflags).
bitflags! {
/// Bitflags representing enabled T-Head custom extensions.
pub struct THead: u32 {
/// Conditional move extension (XTheadCondMov).
const CMOV = 1;
}
}CondMovExtension is used to implement pub trait InstructionExtension: Sync
and other instruction sets under standard are currently in this form
impl InstructionExtension for RvaExtension{...}
pub fn rv32gc() -> Self {
Self {
standard: Standard::G | Standard::C,
thead: THead::empty(),
}
}Should it be changed to pub fn rv32gc() -> Self {
Self {
standard: Standard::all(),
thead: THead::empty(),
}
} |
| Box::new(RvdExtension::new()), | ||
| Box::new(RvcExtension::new()), | ||
| Box::new(XTheadCondMovExtension::new()), | ||
| Box::new(CondMovExtension::new()), |
There was a problem hiding this comment.
The Extension here is also redundant. The Extension needs to be removed. use standard::Rva,Thead::CMov to improve readability.
- Move standard and T-Head extensions into standard/ and thead/ modules - Update decoder, handler and tests to use the new Extensions API Signed-off-by: manchangfengxu <manchangfengxu@openatom.club>
214354a to
e5b66d4
Compare